]> git.ipfire.org Git - thirdparty/mdadm.git/blame - super-intel.c
udev rules: use $tempnode, check for supported types, comments
[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
88654014 4410
f20c3968 4411static int add_to_super_imsm(struct supertype *st, mdu_disk_info_t *dk,
88654014 4412 int fd, char *devname)
cdddbdbc 4413{
c2c087e6 4414 struct intel_super *super = st->sb;
c2c087e6
DW
4415 struct dl *dd;
4416 unsigned long long size;
f2f27e63 4417 __u32 id;
c2c087e6
DW
4418 int rv;
4419 struct stat stb;
4420
88654014
LM
4421 /* If we are on an RAID enabled platform check that the disk is
4422 * attached to the raid controller.
4423 * We do not need to test disks attachment for container based additions,
4424 * they shall be already tested when container was created/assembled.
88c32bb1 4425 */
d424212e 4426 rv = find_intel_hba_capability(fd, super, devname);
f2f5c343 4427 /* no orom/efi or non-intel hba of the disk */
f0f5a016
LM
4428 if (rv != 0) {
4429 dprintf("capability: %p fd: %d ret: %d\n",
4430 super->orom, fd, rv);
4431 return 1;
88c32bb1
DW
4432 }
4433
f20c3968
DW
4434 if (super->current_vol >= 0)
4435 return add_to_super_imsm_volume(st, dk, fd, devname);
bf5a934a 4436
c2c087e6
DW
4437 fstat(fd, &stb);
4438 dd = malloc(sizeof(*dd));
b9f594fe 4439 if (!dd) {
c2c087e6
DW
4440 fprintf(stderr,
4441 Name ": malloc failed %s:%d.\n", __func__, __LINE__);
f20c3968 4442 return 1;
c2c087e6
DW
4443 }
4444 memset(dd, 0, sizeof(*dd));
4445 dd->major = major(stb.st_rdev);
4446 dd->minor = minor(stb.st_rdev);
b9f594fe 4447 dd->index = -1;
c2c087e6 4448 dd->devname = devname ? strdup(devname) : NULL;
c2c087e6 4449 dd->fd = fd;
689c9bf3 4450 dd->e = NULL;
1a64be56 4451 dd->action = DISK_ADD;
c2c087e6 4452 rv = imsm_read_serial(fd, devname, dd->serial);
32ba9157 4453 if (rv) {
c2c087e6 4454 fprintf(stderr,
0030e8d6 4455 Name ": failed to retrieve scsi serial, aborting\n");
949c47a0 4456 free(dd);
0030e8d6 4457 abort();
c2c087e6
DW
4458 }
4459
c2c087e6
DW
4460 get_dev_size(fd, NULL, &size);
4461 size /= 512;
1f24f035 4462 serialcpy(dd->disk.serial, dd->serial);
b9f594fe 4463 dd->disk.total_blocks = __cpu_to_le32(size);
ee5aad5a 4464 dd->disk.status = SPARE_DISK;
c2c087e6 4465 if (sysfs_disk_to_scsi_id(fd, &id) == 0)
b9f594fe 4466 dd->disk.scsi_id = __cpu_to_le32(id);
c2c087e6 4467 else
b9f594fe 4468 dd->disk.scsi_id = __cpu_to_le32(0);
43dad3d6
DW
4469
4470 if (st->update_tail) {
1a64be56
LM
4471 dd->next = super->disk_mgmt_list;
4472 super->disk_mgmt_list = dd;
43dad3d6
DW
4473 } else {
4474 dd->next = super->disks;
4475 super->disks = dd;
ceaf0ee1 4476 super->updates_pending++;
43dad3d6 4477 }
f20c3968
DW
4478
4479 return 0;
cdddbdbc
DW
4480}
4481
1a64be56
LM
4482
4483static int remove_from_super_imsm(struct supertype *st, mdu_disk_info_t *dk)
4484{
4485 struct intel_super *super = st->sb;
4486 struct dl *dd;
4487
4488 /* remove from super works only in mdmon - for communication
4489 * manager - monitor. Check if communication memory buffer
4490 * is prepared.
4491 */
4492 if (!st->update_tail) {
4493 fprintf(stderr,
4494 Name ": %s shall be used in mdmon context only"
4495 "(line %d).\n", __func__, __LINE__);
4496 return 1;
4497 }
4498 dd = malloc(sizeof(*dd));
4499 if (!dd) {
4500 fprintf(stderr,
4501 Name ": malloc failed %s:%d.\n", __func__, __LINE__);
4502 return 1;
4503 }
4504 memset(dd, 0, sizeof(*dd));
4505 dd->major = dk->major;
4506 dd->minor = dk->minor;
4507 dd->index = -1;
4508 dd->fd = -1;
4509 dd->disk.status = SPARE_DISK;
4510 dd->action = DISK_REMOVE;
4511
4512 dd->next = super->disk_mgmt_list;
4513 super->disk_mgmt_list = dd;
4514
4515
4516 return 0;
4517}
4518
f796af5d
DW
4519static int store_imsm_mpb(int fd, struct imsm_super *mpb);
4520
4521static union {
4522 char buf[512];
4523 struct imsm_super anchor;
4524} spare_record __attribute__ ((aligned(512)));
c2c087e6 4525
d23fe947
DW
4526/* spare records have their own family number and do not have any defined raid
4527 * devices
4528 */
4529static int write_super_imsm_spares(struct intel_super *super, int doclose)
4530{
d23fe947 4531 struct imsm_super *mpb = super->anchor;
f796af5d 4532 struct imsm_super *spare = &spare_record.anchor;
d23fe947
DW
4533 __u32 sum;
4534 struct dl *d;
4535
f796af5d
DW
4536 spare->mpb_size = __cpu_to_le32(sizeof(struct imsm_super)),
4537 spare->generation_num = __cpu_to_le32(1UL),
4538 spare->attributes = MPB_ATTRIB_CHECKSUM_VERIFY;
4539 spare->num_disks = 1,
4540 spare->num_raid_devs = 0,
4541 spare->cache_size = mpb->cache_size,
4542 spare->pwr_cycle_count = __cpu_to_le32(1),
4543
4544 snprintf((char *) spare->sig, MAX_SIGNATURE_LENGTH,
4545 MPB_SIGNATURE MPB_VERSION_RAID0);
d23fe947
DW
4546
4547 for (d = super->disks; d; d = d->next) {
8796fdc4 4548 if (d->index != -1)
d23fe947
DW
4549 continue;
4550
f796af5d
DW
4551 spare->disk[0] = d->disk;
4552 sum = __gen_imsm_checksum(spare);
4553 spare->family_num = __cpu_to_le32(sum);
4554 spare->orig_family_num = 0;
4555 sum = __gen_imsm_checksum(spare);
4556 spare->check_sum = __cpu_to_le32(sum);
d23fe947 4557
f796af5d 4558 if (store_imsm_mpb(d->fd, spare)) {
d23fe947
DW
4559 fprintf(stderr, "%s: failed for device %d:%d %s\n",
4560 __func__, d->major, d->minor, strerror(errno));
e74255d9 4561 return 1;
d23fe947
DW
4562 }
4563 if (doclose) {
4564 close(d->fd);
4565 d->fd = -1;
4566 }
4567 }
4568
e74255d9 4569 return 0;
d23fe947
DW
4570}
4571
36988a3d 4572static int write_super_imsm(struct supertype *st, int doclose)
cdddbdbc 4573{
36988a3d 4574 struct intel_super *super = st->sb;
949c47a0 4575 struct imsm_super *mpb = super->anchor;
c2c087e6
DW
4576 struct dl *d;
4577 __u32 generation;
4578 __u32 sum;
d23fe947 4579 int spares = 0;
949c47a0 4580 int i;
a48ac0a8 4581 __u32 mpb_size = sizeof(struct imsm_super) - sizeof(struct imsm_disk);
36988a3d 4582 int num_disks = 0;
146c6260 4583 int clear_migration_record = 1;
cdddbdbc 4584
c2c087e6
DW
4585 /* 'generation' is incremented everytime the metadata is written */
4586 generation = __le32_to_cpu(mpb->generation_num);
4587 generation++;
4588 mpb->generation_num = __cpu_to_le32(generation);
4589
148acb7b
DW
4590 /* fix up cases where previous mdadm releases failed to set
4591 * orig_family_num
4592 */
4593 if (mpb->orig_family_num == 0)
4594 mpb->orig_family_num = mpb->family_num;
4595
d23fe947 4596 for (d = super->disks; d; d = d->next) {
8796fdc4 4597 if (d->index == -1)
d23fe947 4598 spares++;
36988a3d 4599 else {
d23fe947 4600 mpb->disk[d->index] = d->disk;
36988a3d
AK
4601 num_disks++;
4602 }
d23fe947 4603 }
36988a3d 4604 for (d = super->missing; d; d = d->next) {
47ee5a45 4605 mpb->disk[d->index] = d->disk;
36988a3d
AK
4606 num_disks++;
4607 }
4608 mpb->num_disks = num_disks;
4609 mpb_size += sizeof(struct imsm_disk) * mpb->num_disks;
b9f594fe 4610
949c47a0
DW
4611 for (i = 0; i < mpb->num_raid_devs; i++) {
4612 struct imsm_dev *dev = __get_imsm_dev(mpb, i);
36988a3d
AK
4613 struct imsm_dev *dev2 = get_imsm_dev(super, i);
4614 if (dev && dev2) {
4615 imsm_copy_dev(dev, dev2);
4616 mpb_size += sizeof_imsm_dev(dev, 0);
4617 }
146c6260
AK
4618 if (is_gen_migration(dev2))
4619 clear_migration_record = 0;
949c47a0 4620 }
a48ac0a8
DW
4621 mpb_size += __le32_to_cpu(mpb->bbm_log_size);
4622 mpb->mpb_size = __cpu_to_le32(mpb_size);
949c47a0 4623
c2c087e6 4624 /* recalculate checksum */
949c47a0 4625 sum = __gen_imsm_checksum(mpb);
c2c087e6
DW
4626 mpb->check_sum = __cpu_to_le32(sum);
4627
146c6260
AK
4628 if (clear_migration_record)
4629 memset(super->migr_rec_buf, 0, 512);
4630
d23fe947 4631 /* write the mpb for disks that compose raid devices */
c2c087e6 4632 for (d = super->disks; d ; d = d->next) {
86c54047 4633 if (d->index < 0 || is_failed(&d->disk))
d23fe947 4634 continue;
f796af5d 4635 if (store_imsm_mpb(d->fd, mpb))
c2c087e6
DW
4636 fprintf(stderr, "%s: failed for device %d:%d %s\n",
4637 __func__, d->major, d->minor, strerror(errno));
146c6260
AK
4638 if (clear_migration_record) {
4639 unsigned long long dsize;
4640
4641 get_dev_size(d->fd, NULL, &dsize);
4642 if (lseek64(d->fd, dsize - 512, SEEK_SET) >= 0) {
9e2d750d
N
4643 if (write(d->fd, super->migr_rec_buf, 512) != 512)
4644 perror("Write migr_rec failed");
146c6260
AK
4645 }
4646 }
c2c087e6
DW
4647 if (doclose) {
4648 close(d->fd);
4649 d->fd = -1;
4650 }
4651 }
4652
d23fe947
DW
4653 if (spares)
4654 return write_super_imsm_spares(super, doclose);
4655
e74255d9 4656 return 0;
c2c087e6
DW
4657}
4658
0e600426 4659
9b1fb677 4660static int create_array(struct supertype *st, int dev_idx)
43dad3d6
DW
4661{
4662 size_t len;
4663 struct imsm_update_create_array *u;
4664 struct intel_super *super = st->sb;
9b1fb677 4665 struct imsm_dev *dev = get_imsm_dev(super, dev_idx);
54c2c1ea
DW
4666 struct imsm_map *map = get_imsm_map(dev, 0);
4667 struct disk_info *inf;
4668 struct imsm_disk *disk;
4669 int i;
43dad3d6 4670
54c2c1ea
DW
4671 len = sizeof(*u) - sizeof(*dev) + sizeof_imsm_dev(dev, 0) +
4672 sizeof(*inf) * map->num_members;
43dad3d6
DW
4673 u = malloc(len);
4674 if (!u) {
4675 fprintf(stderr, "%s: failed to allocate update buffer\n",
4676 __func__);
4677 return 1;
4678 }
4679
4680 u->type = update_create_array;
9b1fb677 4681 u->dev_idx = dev_idx;
43dad3d6 4682 imsm_copy_dev(&u->dev, dev);
54c2c1ea
DW
4683 inf = get_disk_info(u);
4684 for (i = 0; i < map->num_members; i++) {
98130f40 4685 int idx = get_imsm_disk_idx(dev, i, -1);
9b1fb677 4686
54c2c1ea
DW
4687 disk = get_imsm_disk(super, idx);
4688 serialcpy(inf[i].serial, disk->serial);
4689 }
43dad3d6
DW
4690 append_metadata_update(st, u, len);
4691
4692 return 0;
4693}
4694
1a64be56 4695static int mgmt_disk(struct supertype *st)
43dad3d6
DW
4696{
4697 struct intel_super *super = st->sb;
4698 size_t len;
1a64be56 4699 struct imsm_update_add_remove_disk *u;
43dad3d6 4700
1a64be56 4701 if (!super->disk_mgmt_list)
43dad3d6
DW
4702 return 0;
4703
4704 len = sizeof(*u);
4705 u = malloc(len);
4706 if (!u) {
4707 fprintf(stderr, "%s: failed to allocate update buffer\n",
4708 __func__);
4709 return 1;
4710 }
4711
1a64be56 4712 u->type = update_add_remove_disk;
43dad3d6
DW
4713 append_metadata_update(st, u, len);
4714
4715 return 0;
4716}
4717
c2c087e6
DW
4718static int write_init_super_imsm(struct supertype *st)
4719{
9b1fb677
DW
4720 struct intel_super *super = st->sb;
4721 int current_vol = super->current_vol;
4722
4723 /* we are done with current_vol reset it to point st at the container */
4724 super->current_vol = -1;
4725
8273f55e 4726 if (st->update_tail) {
43dad3d6
DW
4727 /* queue the recently created array / added disk
4728 * as a metadata update */
43dad3d6 4729 int rv;
8273f55e 4730
43dad3d6 4731 /* determine if we are creating a volume or adding a disk */
9b1fb677 4732 if (current_vol < 0) {
1a64be56
LM
4733 /* in the mgmt (add/remove) disk case we are running
4734 * in mdmon context, so don't close fd's
43dad3d6 4735 */
1a64be56 4736 return mgmt_disk(st);
43dad3d6 4737 } else
9b1fb677 4738 rv = create_array(st, current_vol);
8273f55e 4739
43dad3d6 4740 return rv;
d682f344
N
4741 } else {
4742 struct dl *d;
4743 for (d = super->disks; d; d = d->next)
4744 Kill(d->devname, NULL, 0, 1, 1);
36988a3d 4745 return write_super_imsm(st, 1);
d682f344 4746 }
cdddbdbc 4747}
0e600426 4748#endif
cdddbdbc 4749
e683ca88 4750static int store_super_imsm(struct supertype *st, int fd)
cdddbdbc 4751{
e683ca88
DW
4752 struct intel_super *super = st->sb;
4753 struct imsm_super *mpb = super ? super->anchor : NULL;
551c80c1 4754
e683ca88 4755 if (!mpb)
ad97895e
DW
4756 return 1;
4757
1799c9e8 4758#ifndef MDASSEMBLE
e683ca88 4759 return store_imsm_mpb(fd, mpb);
1799c9e8
N
4760#else
4761 return 1;
4762#endif
cdddbdbc
DW
4763}
4764
0e600426
N
4765static int imsm_bbm_log_size(struct imsm_super *mpb)
4766{
4767 return __le32_to_cpu(mpb->bbm_log_size);
4768}
4769
4770#ifndef MDASSEMBLE
cdddbdbc
DW
4771static int validate_geometry_imsm_container(struct supertype *st, int level,
4772 int layout, int raiddisks, int chunk,
c2c087e6 4773 unsigned long long size, char *dev,
2c514b71
NB
4774 unsigned long long *freesize,
4775 int verbose)
cdddbdbc 4776{
c2c087e6
DW
4777 int fd;
4778 unsigned long long ldsize;
f2f5c343
LM
4779 struct intel_super *super=NULL;
4780 int rv = 0;
cdddbdbc 4781
c2c087e6
DW
4782 if (level != LEVEL_CONTAINER)
4783 return 0;
4784 if (!dev)
4785 return 1;
4786
4787 fd = open(dev, O_RDONLY|O_EXCL, 0);
4788 if (fd < 0) {
2c514b71
NB
4789 if (verbose)
4790 fprintf(stderr, Name ": imsm: Cannot open %s: %s\n",
4791 dev, strerror(errno));
c2c087e6
DW
4792 return 0;
4793 }
4794 if (!get_dev_size(fd, dev, &ldsize)) {
4795 close(fd);
4796 return 0;
4797 }
f2f5c343
LM
4798
4799 /* capabilities retrieve could be possible
4800 * note that there is no fd for the disks in array.
4801 */
4802 super = alloc_super();
4803 if (!super) {
4804 fprintf(stderr,
4805 Name ": malloc of %zu failed.\n",
4806 sizeof(*super));
4807 close(fd);
4808 return 0;
4809 }
4810
d424212e 4811 rv = find_intel_hba_capability(fd, super, verbose ? dev : NULL);
f2f5c343
LM
4812 if (rv != 0) {
4813#if DEBUG
4814 char str[256];
4815 fd2devname(fd, str);
4816 dprintf("validate_geometry_imsm_container: fd: %d %s orom: %p rv: %d raiddisk: %d\n",
4817 fd, str, super->orom, rv, raiddisks);
4818#endif
4819 /* no orom/efi or non-intel hba of the disk */
4820 close(fd);
4821 free_imsm(super);
4822 return 0;
4823 }
c2c087e6 4824 close(fd);
f2f5c343
LM
4825 if (super->orom && raiddisks > super->orom->tds) {
4826 if (verbose)
4827 fprintf(stderr, Name ": %d exceeds maximum number of"
4828 " platform supported disks: %d\n",
4829 raiddisks, super->orom->tds);
4830
4831 free_imsm(super);
4832 return 0;
4833 }
c2c087e6
DW
4834
4835 *freesize = avail_size_imsm(st, ldsize >> 9);
f2f5c343 4836 free_imsm(super);
c2c087e6
DW
4837
4838 return 1;
cdddbdbc
DW
4839}
4840
0dcecb2e
DW
4841static unsigned long long find_size(struct extent *e, int *idx, int num_extents)
4842{
4843 const unsigned long long base_start = e[*idx].start;
4844 unsigned long long end = base_start + e[*idx].size;
4845 int i;
4846
4847 if (base_start == end)
4848 return 0;
4849
4850 *idx = *idx + 1;
4851 for (i = *idx; i < num_extents; i++) {
4852 /* extend overlapping extents */
4853 if (e[i].start >= base_start &&
4854 e[i].start <= end) {
4855 if (e[i].size == 0)
4856 return 0;
4857 if (e[i].start + e[i].size > end)
4858 end = e[i].start + e[i].size;
4859 } else if (e[i].start > end) {
4860 *idx = i;
4861 break;
4862 }
4863 }
4864
4865 return end - base_start;
4866}
4867
4868static unsigned long long merge_extents(struct intel_super *super, int sum_extents)
4869{
4870 /* build a composite disk with all known extents and generate a new
4871 * 'maxsize' given the "all disks in an array must share a common start
4872 * offset" constraint
4873 */
4874 struct extent *e = calloc(sum_extents, sizeof(*e));
4875 struct dl *dl;
4876 int i, j;
4877 int start_extent;
4878 unsigned long long pos;
b9d77223 4879 unsigned long long start = 0;
0dcecb2e
DW
4880 unsigned long long maxsize;
4881 unsigned long reserve;
4882
4883 if (!e)
a7dd165b 4884 return 0;
0dcecb2e
DW
4885
4886 /* coalesce and sort all extents. also, check to see if we need to
4887 * reserve space between member arrays
4888 */
4889 j = 0;
4890 for (dl = super->disks; dl; dl = dl->next) {
4891 if (!dl->e)
4892 continue;
4893 for (i = 0; i < dl->extent_cnt; i++)
4894 e[j++] = dl->e[i];
4895 }
4896 qsort(e, sum_extents, sizeof(*e), cmp_extent);
4897
4898 /* merge extents */
4899 i = 0;
4900 j = 0;
4901 while (i < sum_extents) {
4902 e[j].start = e[i].start;
4903 e[j].size = find_size(e, &i, sum_extents);
4904 j++;
4905 if (e[j-1].size == 0)
4906 break;
4907 }
4908
4909 pos = 0;
4910 maxsize = 0;
4911 start_extent = 0;
4912 i = 0;
4913 do {
4914 unsigned long long esize;
4915
4916 esize = e[i].start - pos;
4917 if (esize >= maxsize) {
4918 maxsize = esize;
4919 start = pos;
4920 start_extent = i;
4921 }
4922 pos = e[i].start + e[i].size;
4923 i++;
4924 } while (e[i-1].size);
4925 free(e);
4926
a7dd165b
DW
4927 if (maxsize == 0)
4928 return 0;
4929
4930 /* FIXME assumes volume at offset 0 is the first volume in a
4931 * container
4932 */
0dcecb2e
DW
4933 if (start_extent > 0)
4934 reserve = IMSM_RESERVED_SECTORS; /* gap between raid regions */
4935 else
4936 reserve = 0;
4937
4938 if (maxsize < reserve)
a7dd165b 4939 return 0;
0dcecb2e
DW
4940
4941 super->create_offset = ~((__u32) 0);
4942 if (start + reserve > super->create_offset)
a7dd165b 4943 return 0; /* start overflows create_offset */
0dcecb2e
DW
4944 super->create_offset = start + reserve;
4945
4946 return maxsize - reserve;
4947}
4948
88c32bb1
DW
4949static int is_raid_level_supported(const struct imsm_orom *orom, int level, int raiddisks)
4950{
4951 if (level < 0 || level == 6 || level == 4)
4952 return 0;
4953
4954 /* if we have an orom prevent invalid raid levels */
4955 if (orom)
4956 switch (level) {
4957 case 0: return imsm_orom_has_raid0(orom);
4958 case 1:
4959 if (raiddisks > 2)
4960 return imsm_orom_has_raid1e(orom);
1c556e92
DW
4961 return imsm_orom_has_raid1(orom) && raiddisks == 2;
4962 case 10: return imsm_orom_has_raid10(orom) && raiddisks == 4;
4963 case 5: return imsm_orom_has_raid5(orom) && raiddisks > 2;
88c32bb1
DW
4964 }
4965 else
4966 return 1; /* not on an Intel RAID platform so anything goes */
4967
4968 return 0;
4969}
4970
cd9d1ac7
DW
4971static int imsm_default_chunk(const struct imsm_orom *orom)
4972{
4973 /* up to 512 if the plaform supports it, otherwise the platform max.
4974 * 128 if no platform detected
4975 */
4976 int fs = max(7, orom ? fls(orom->sss) : 0);
4977
4978 return min(512, (1 << fs));
4979}
73408129 4980
35f81cbb 4981#define pr_vrb(fmt, arg...) (void) (verbose && fprintf(stderr, Name fmt, ##arg))
6592ce37
DW
4982static int
4983validate_geometry_imsm_orom(struct intel_super *super, int level, int layout,
c21e737b 4984 int raiddisks, int *chunk, int verbose)
6592ce37 4985{
660260d0
DW
4986 /* check/set platform and metadata limits/defaults */
4987 if (super->orom && raiddisks > super->orom->dpa) {
4988 pr_vrb(": platform supports a maximum of %d disks per array\n",
4989 super->orom->dpa);
73408129
LM
4990 return 0;
4991 }
4992
4993 /* capabilities of OROM tested - copied from validate_geometry_imsm_volume */
660260d0 4994 if (!is_raid_level_supported(super->orom, level, raiddisks)) {
6592ce37
DW
4995 pr_vrb(": platform does not support raid%d with %d disk%s\n",
4996 level, raiddisks, raiddisks > 1 ? "s" : "");
4997 return 0;
4998 }
cd9d1ac7
DW
4999
5000 if (chunk && (*chunk == 0 || *chunk == UnSet))
5001 *chunk = imsm_default_chunk(super->orom);
5002
5003 if (super->orom && chunk && !imsm_orom_has_chunk(super->orom, *chunk)) {
5004 pr_vrb(": platform does not support a chunk size of: "
5005 "%d\n", *chunk);
5006 return 0;
6592ce37 5007 }
cd9d1ac7 5008
6592ce37
DW
5009 if (layout != imsm_level_to_layout(level)) {
5010 if (level == 5)
5011 pr_vrb(": imsm raid 5 only supports the left-asymmetric layout\n");
5012 else if (level == 10)
5013 pr_vrb(": imsm raid 10 only supports the n2 layout\n");
5014 else
5015 pr_vrb(": imsm unknown layout %#x for this raid level %d\n",
5016 layout, level);
5017 return 0;
5018 }
6592ce37
DW
5019 return 1;
5020}
5021
c2c087e6
DW
5022/* validate_geometry_imsm_volume - lifted from validate_geometry_ddf_bvd
5023 * FIX ME add ahci details
5024 */
8b353278 5025static int validate_geometry_imsm_volume(struct supertype *st, int level,
c21e737b 5026 int layout, int raiddisks, int *chunk,
c2c087e6 5027 unsigned long long size, char *dev,
2c514b71
NB
5028 unsigned long long *freesize,
5029 int verbose)
cdddbdbc 5030{
c2c087e6
DW
5031 struct stat stb;
5032 struct intel_super *super = st->sb;
a20d2ba5 5033 struct imsm_super *mpb = super->anchor;
c2c087e6
DW
5034 struct dl *dl;
5035 unsigned long long pos = 0;
5036 unsigned long long maxsize;
5037 struct extent *e;
5038 int i;
cdddbdbc 5039
88c32bb1
DW
5040 /* We must have the container info already read in. */
5041 if (!super)
c2c087e6
DW
5042 return 0;
5043
d54559f0
LM
5044 if (!validate_geometry_imsm_orom(super, level, layout, raiddisks, chunk, verbose)) {
5045 fprintf(stderr, Name ": RAID gemetry validation failed. "
5046 "Cannot proceed with the action(s).\n");
c2c087e6 5047 return 0;
d54559f0 5048 }
c2c087e6
DW
5049 if (!dev) {
5050 /* General test: make sure there is space for
2da8544a
DW
5051 * 'raiddisks' device extents of size 'size' at a given
5052 * offset
c2c087e6 5053 */
e46273eb 5054 unsigned long long minsize = size;
b7528a20 5055 unsigned long long start_offset = MaxSector;
c2c087e6
DW
5056 int dcnt = 0;
5057 if (minsize == 0)
5058 minsize = MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS;
5059 for (dl = super->disks; dl ; dl = dl->next) {
5060 int found = 0;
5061
bf5a934a 5062 pos = 0;
c2c087e6
DW
5063 i = 0;
5064 e = get_extents(super, dl);
5065 if (!e) continue;
5066 do {
5067 unsigned long long esize;
5068 esize = e[i].start - pos;
5069 if (esize >= minsize)
5070 found = 1;
b7528a20 5071 if (found && start_offset == MaxSector) {
2da8544a
DW
5072 start_offset = pos;
5073 break;
5074 } else if (found && pos != start_offset) {
5075 found = 0;
5076 break;
5077 }
c2c087e6
DW
5078 pos = e[i].start + e[i].size;
5079 i++;
5080 } while (e[i-1].size);
5081 if (found)
5082 dcnt++;
5083 free(e);
5084 }
5085 if (dcnt < raiddisks) {
2c514b71
NB
5086 if (verbose)
5087 fprintf(stderr, Name ": imsm: Not enough "
5088 "devices with space for this array "
5089 "(%d < %d)\n",
5090 dcnt, raiddisks);
c2c087e6
DW
5091 return 0;
5092 }
5093 return 1;
5094 }
0dcecb2e 5095
c2c087e6
DW
5096 /* This device must be a member of the set */
5097 if (stat(dev, &stb) < 0)
5098 return 0;
5099 if ((S_IFMT & stb.st_mode) != S_IFBLK)
5100 return 0;
5101 for (dl = super->disks ; dl ; dl = dl->next) {
f21e18ca
N
5102 if (dl->major == (int)major(stb.st_rdev) &&
5103 dl->minor == (int)minor(stb.st_rdev))
c2c087e6
DW
5104 break;
5105 }
5106 if (!dl) {
2c514b71
NB
5107 if (verbose)
5108 fprintf(stderr, Name ": %s is not in the "
5109 "same imsm set\n", dev);
c2c087e6 5110 return 0;
a20d2ba5
DW
5111 } else if (super->orom && dl->index < 0 && mpb->num_raid_devs) {
5112 /* If a volume is present then the current creation attempt
5113 * cannot incorporate new spares because the orom may not
5114 * understand this configuration (all member disks must be
5115 * members of each array in the container).
5116 */
5117 fprintf(stderr, Name ": %s is a spare and a volume"
5118 " is already defined for this container\n", dev);
5119 fprintf(stderr, Name ": The option-rom requires all member"
5120 " disks to be a member of all volumes\n");
5121 return 0;
c2c087e6 5122 }
0dcecb2e
DW
5123
5124 /* retrieve the largest free space block */
c2c087e6
DW
5125 e = get_extents(super, dl);
5126 maxsize = 0;
5127 i = 0;
0dcecb2e
DW
5128 if (e) {
5129 do {
5130 unsigned long long esize;
5131
5132 esize = e[i].start - pos;
5133 if (esize >= maxsize)
5134 maxsize = esize;
5135 pos = e[i].start + e[i].size;
5136 i++;
5137 } while (e[i-1].size);
5138 dl->e = e;
5139 dl->extent_cnt = i;
5140 } else {
5141 if (verbose)
5142 fprintf(stderr, Name ": unable to determine free space for: %s\n",
5143 dev);
5144 return 0;
5145 }
5146 if (maxsize < size) {
5147 if (verbose)
5148 fprintf(stderr, Name ": %s not enough space (%llu < %llu)\n",
5149 dev, maxsize, size);
5150 return 0;
5151 }
5152
5153 /* count total number of extents for merge */
5154 i = 0;
5155 for (dl = super->disks; dl; dl = dl->next)
5156 if (dl->e)
5157 i += dl->extent_cnt;
5158
5159 maxsize = merge_extents(super, i);
a7dd165b 5160 if (maxsize < size || maxsize == 0) {
0dcecb2e
DW
5161 if (verbose)
5162 fprintf(stderr, Name ": not enough space after merge (%llu < %llu)\n",
5163 maxsize, size);
5164 return 0;
0dcecb2e
DW
5165 }
5166
c2c087e6
DW
5167 *freesize = maxsize;
5168
5169 return 1;
cdddbdbc
DW
5170}
5171
efb30e7f
DW
5172static int reserve_space(struct supertype *st, int raiddisks,
5173 unsigned long long size, int chunk,
5174 unsigned long long *freesize)
5175{
5176 struct intel_super *super = st->sb;
5177 struct imsm_super *mpb = super->anchor;
5178 struct dl *dl;
5179 int i;
5180 int extent_cnt;
5181 struct extent *e;
5182 unsigned long long maxsize;
5183 unsigned long long minsize;
5184 int cnt;
5185 int used;
5186
5187 /* find the largest common start free region of the possible disks */
5188 used = 0;
5189 extent_cnt = 0;
5190 cnt = 0;
5191 for (dl = super->disks; dl; dl = dl->next) {
5192 dl->raiddisk = -1;
5193
5194 if (dl->index >= 0)
5195 used++;
5196
5197 /* don't activate new spares if we are orom constrained
5198 * and there is already a volume active in the container
5199 */
5200 if (super->orom && dl->index < 0 && mpb->num_raid_devs)
5201 continue;
5202
5203 e = get_extents(super, dl);
5204 if (!e)
5205 continue;
5206 for (i = 1; e[i-1].size; i++)
5207 ;
5208 dl->e = e;
5209 dl->extent_cnt = i;
5210 extent_cnt += i;
5211 cnt++;
5212 }
5213
5214 maxsize = merge_extents(super, extent_cnt);
5215 minsize = size;
5216 if (size == 0)
612e59d8
CA
5217 /* chunk is in K */
5218 minsize = chunk * 2;
efb30e7f
DW
5219
5220 if (cnt < raiddisks ||
5221 (super->orom && used && used != raiddisks) ||
a7dd165b
DW
5222 maxsize < minsize ||
5223 maxsize == 0) {
efb30e7f
DW
5224 fprintf(stderr, Name ": not enough devices with space to create array.\n");
5225 return 0; /* No enough free spaces large enough */
5226 }
5227
5228 if (size == 0) {
5229 size = maxsize;
5230 if (chunk) {
612e59d8
CA
5231 size /= 2 * chunk;
5232 size *= 2 * chunk;
efb30e7f
DW
5233 }
5234 }
5235
5236 cnt = 0;
5237 for (dl = super->disks; dl; dl = dl->next)
5238 if (dl->e)
5239 dl->raiddisk = cnt++;
5240
5241 *freesize = size;
5242
5243 return 1;
5244}
5245
bf5a934a 5246static int validate_geometry_imsm(struct supertype *st, int level, int layout,
c21e737b 5247 int raiddisks, int *chunk, unsigned long long size,
bf5a934a
DW
5248 char *dev, unsigned long long *freesize,
5249 int verbose)
5250{
5251 int fd, cfd;
5252 struct mdinfo *sra;
20cbe8d2 5253 int is_member = 0;
bf5a934a 5254
d54559f0
LM
5255 /* load capability
5256 * if given unused devices create a container
bf5a934a
DW
5257 * if given given devices in a container create a member volume
5258 */
5259 if (level == LEVEL_CONTAINER) {
5260 /* Must be a fresh device to add to a container */
5261 return validate_geometry_imsm_container(st, level, layout,
c21e737b
CA
5262 raiddisks,
5263 chunk?*chunk:0, size,
bf5a934a
DW
5264 dev, freesize,
5265 verbose);
5266 }
5267
8592f29d
N
5268 if (!dev) {
5269 if (st->sb && freesize) {
efb30e7f
DW
5270 /* we are being asked to automatically layout a
5271 * new volume based on the current contents of
5272 * the container. If the the parameters can be
5273 * satisfied reserve_space will record the disks,
5274 * start offset, and size of the volume to be
5275 * created. add_to_super and getinfo_super
5276 * detect when autolayout is in progress.
5277 */
6592ce37
DW
5278 if (!validate_geometry_imsm_orom(st->sb, level, layout,
5279 raiddisks, chunk,
5280 verbose))
5281 return 0;
c21e737b
CA
5282 return reserve_space(st, raiddisks, size,
5283 chunk?*chunk:0, freesize);
8592f29d
N
5284 }
5285 return 1;
5286 }
bf5a934a
DW
5287 if (st->sb) {
5288 /* creating in a given container */
5289 return validate_geometry_imsm_volume(st, level, layout,
5290 raiddisks, chunk, size,
5291 dev, freesize, verbose);
5292 }
5293
bf5a934a
DW
5294 /* This device needs to be a device in an 'imsm' container */
5295 fd = open(dev, O_RDONLY|O_EXCL, 0);
5296 if (fd >= 0) {
5297 if (verbose)
5298 fprintf(stderr,
5299 Name ": Cannot create this array on device %s\n",
5300 dev);
5301 close(fd);
5302 return 0;
5303 }
5304 if (errno != EBUSY || (fd = open(dev, O_RDONLY, 0)) < 0) {
5305 if (verbose)
5306 fprintf(stderr, Name ": Cannot open %s: %s\n",
5307 dev, strerror(errno));
5308 return 0;
5309 }
5310 /* Well, it is in use by someone, maybe an 'imsm' container. */
5311 cfd = open_container(fd);
20cbe8d2 5312 close(fd);
bf5a934a 5313 if (cfd < 0) {
bf5a934a
DW
5314 if (verbose)
5315 fprintf(stderr, Name ": Cannot use %s: It is busy\n",
5316 dev);
5317 return 0;
5318 }
5319 sra = sysfs_read(cfd, 0, GET_VERSION);
bf5a934a 5320 if (sra && sra->array.major_version == -1 &&
20cbe8d2
AW
5321 strcmp(sra->text_version, "imsm") == 0)
5322 is_member = 1;
5323 sysfs_free(sra);
5324 if (is_member) {
bf5a934a
DW
5325 /* This is a member of a imsm container. Load the container
5326 * and try to create a volume
5327 */
5328 struct intel_super *super;
5329
e1902a7b 5330 if (load_super_imsm_all(st, cfd, (void **) &super, NULL) == 0) {
bf5a934a
DW
5331 st->sb = super;
5332 st->container_dev = fd2devnum(cfd);
5333 close(cfd);
5334 return validate_geometry_imsm_volume(st, level, layout,
5335 raiddisks, chunk,
5336 size, dev,
5337 freesize, verbose);
5338 }
20cbe8d2 5339 }
bf5a934a 5340
20cbe8d2
AW
5341 if (verbose)
5342 fprintf(stderr, Name ": failed container membership check\n");
5343
5344 close(cfd);
5345 return 0;
bf5a934a 5346}
0bd16cf2 5347
30f58b22 5348static void default_geometry_imsm(struct supertype *st, int *level, int *layout, int *chunk)
0bd16cf2
DJ
5349{
5350 struct intel_super *super = st->sb;
5351
30f58b22
DW
5352 if (level && *level == UnSet)
5353 *level = LEVEL_CONTAINER;
5354
5355 if (level && layout && *layout == UnSet)
5356 *layout = imsm_level_to_layout(*level);
0bd16cf2 5357
cd9d1ac7
DW
5358 if (chunk && (*chunk == UnSet || *chunk == 0))
5359 *chunk = imsm_default_chunk(super->orom);
0bd16cf2
DJ
5360}
5361
33414a01
DW
5362static void handle_missing(struct intel_super *super, struct imsm_dev *dev);
5363
5364static int kill_subarray_imsm(struct supertype *st)
5365{
5366 /* remove the subarray currently referenced by ->current_vol */
5367 __u8 i;
5368 struct intel_dev **dp;
5369 struct intel_super *super = st->sb;
5370 __u8 current_vol = super->current_vol;
5371 struct imsm_super *mpb = super->anchor;
5372
5373 if (super->current_vol < 0)
5374 return 2;
5375 super->current_vol = -1; /* invalidate subarray cursor */
5376
5377 /* block deletions that would change the uuid of active subarrays
5378 *
5379 * FIXME when immutable ids are available, but note that we'll
5380 * also need to fixup the invalidated/active subarray indexes in
5381 * mdstat
5382 */
5383 for (i = 0; i < mpb->num_raid_devs; i++) {
5384 char subarray[4];
5385
5386 if (i < current_vol)
5387 continue;
5388 sprintf(subarray, "%u", i);
5389 if (is_subarray_active(subarray, st->devname)) {
5390 fprintf(stderr,
5391 Name ": deleting subarray-%d would change the UUID of active subarray-%d, aborting\n",
5392 current_vol, i);
5393
5394 return 2;
5395 }
5396 }
5397
5398 if (st->update_tail) {
5399 struct imsm_update_kill_array *u = malloc(sizeof(*u));
5400
5401 if (!u)
5402 return 2;
5403 u->type = update_kill_array;
5404 u->dev_idx = current_vol;
5405 append_metadata_update(st, u, sizeof(*u));
5406
5407 return 0;
5408 }
5409
5410 for (dp = &super->devlist; *dp;)
5411 if ((*dp)->index == current_vol) {
5412 *dp = (*dp)->next;
5413 } else {
5414 handle_missing(super, (*dp)->dev);
5415 if ((*dp)->index > current_vol)
5416 (*dp)->index--;
5417 dp = &(*dp)->next;
5418 }
5419
5420 /* no more raid devices, all active components are now spares,
5421 * but of course failed are still failed
5422 */
5423 if (--mpb->num_raid_devs == 0) {
5424 struct dl *d;
5425
5426 for (d = super->disks; d; d = d->next)
5427 if (d->index > -2) {
5428 d->index = -1;
5429 d->disk.status = SPARE_DISK;
5430 }
5431 }
5432
5433 super->updates_pending++;
5434
5435 return 0;
5436}
aa534678 5437
a951a4f7 5438static int update_subarray_imsm(struct supertype *st, char *subarray,
fa56eddb 5439 char *update, struct mddev_ident *ident)
aa534678
DW
5440{
5441 /* update the subarray currently referenced by ->current_vol */
5442 struct intel_super *super = st->sb;
5443 struct imsm_super *mpb = super->anchor;
5444
aa534678
DW
5445 if (strcmp(update, "name") == 0) {
5446 char *name = ident->name;
a951a4f7
N
5447 char *ep;
5448 int vol;
aa534678 5449
a951a4f7 5450 if (is_subarray_active(subarray, st->devname)) {
aa534678
DW
5451 fprintf(stderr,
5452 Name ": Unable to update name of active subarray\n");
5453 return 2;
5454 }
5455
5456 if (!check_name(super, name, 0))
5457 return 2;
5458
a951a4f7
N
5459 vol = strtoul(subarray, &ep, 10);
5460 if (*ep != '\0' || vol >= super->anchor->num_raid_devs)
5461 return 2;
5462
aa534678
DW
5463 if (st->update_tail) {
5464 struct imsm_update_rename_array *u = malloc(sizeof(*u));
5465
5466 if (!u)
5467 return 2;
5468 u->type = update_rename_array;
a951a4f7 5469 u->dev_idx = vol;
aa534678
DW
5470 snprintf((char *) u->name, MAX_RAID_SERIAL_LEN, "%s", name);
5471 append_metadata_update(st, u, sizeof(*u));
5472 } else {
5473 struct imsm_dev *dev;
5474 int i;
5475
a951a4f7 5476 dev = get_imsm_dev(super, vol);
aa534678
DW
5477 snprintf((char *) dev->volume, MAX_RAID_SERIAL_LEN, "%s", name);
5478 for (i = 0; i < mpb->num_raid_devs; i++) {
5479 dev = get_imsm_dev(super, i);
5480 handle_missing(super, dev);
5481 }
5482 super->updates_pending++;
5483 }
5484 } else
5485 return 2;
5486
5487 return 0;
5488}
bf5a934a 5489
28bce06f
AK
5490static int is_gen_migration(struct imsm_dev *dev)
5491{
7534230b
AK
5492 if (dev == NULL)
5493 return 0;
5494
28bce06f
AK
5495 if (!dev->vol.migr_state)
5496 return 0;
5497
5498 if (migr_type(dev) == MIGR_GEN_MIGR)
5499 return 1;
5500
5501 return 0;
5502}
71204a50 5503#endif /* MDASSEMBLE */
28bce06f 5504
1e5c6983
DW
5505static int is_rebuilding(struct imsm_dev *dev)
5506{
5507 struct imsm_map *migr_map;
5508
5509 if (!dev->vol.migr_state)
5510 return 0;
5511
5512 if (migr_type(dev) != MIGR_REBUILD)
5513 return 0;
5514
5515 migr_map = get_imsm_map(dev, 1);
5516
5517 if (migr_map->map_state == IMSM_T_STATE_DEGRADED)
5518 return 1;
5519 else
5520 return 0;
5521}
5522
c47b0ff6
AK
5523static void update_recovery_start(struct intel_super *super,
5524 struct imsm_dev *dev,
5525 struct mdinfo *array)
1e5c6983
DW
5526{
5527 struct mdinfo *rebuild = NULL;
5528 struct mdinfo *d;
5529 __u32 units;
5530
5531 if (!is_rebuilding(dev))
5532 return;
5533
5534 /* Find the rebuild target, but punt on the dual rebuild case */
5535 for (d = array->devs; d; d = d->next)
5536 if (d->recovery_start == 0) {
5537 if (rebuild)
5538 return;
5539 rebuild = d;
5540 }
5541
4363fd80
DW
5542 if (!rebuild) {
5543 /* (?) none of the disks are marked with
5544 * IMSM_ORD_REBUILD, so assume they are missing and the
5545 * disk_ord_tbl was not correctly updated
5546 */
5547 dprintf("%s: failed to locate out-of-sync disk\n", __func__);
5548 return;
5549 }
5550
1e5c6983 5551 units = __le32_to_cpu(dev->vol.curr_migr_unit);
c47b0ff6 5552 rebuild->recovery_start = units * blocks_per_migr_unit(super, dev);
1e5c6983
DW
5553}
5554
9e2d750d 5555#ifndef MDASSEMBLE
276d77db 5556static int recover_backup_imsm(struct supertype *st, struct mdinfo *info);
9e2d750d 5557#endif
1e5c6983 5558
00bbdbda 5559static struct mdinfo *container_content_imsm(struct supertype *st, char *subarray)
cdddbdbc 5560{
4f5bc454
DW
5561 /* Given a container loaded by load_super_imsm_all,
5562 * extract information about all the arrays into
5563 * an mdinfo tree.
00bbdbda 5564 * If 'subarray' is given, just extract info about that array.
4f5bc454
DW
5565 *
5566 * For each imsm_dev create an mdinfo, fill it in,
5567 * then look for matching devices in super->disks
5568 * and create appropriate device mdinfo.
5569 */
5570 struct intel_super *super = st->sb;
949c47a0 5571 struct imsm_super *mpb = super->anchor;
4f5bc454 5572 struct mdinfo *rest = NULL;
00bbdbda 5573 unsigned int i;
a06d022d 5574 int bbm_errors = 0;
abef11a3
AK
5575 struct dl *d;
5576 int spare_disks = 0;
cdddbdbc 5577
19482bcc
AK
5578 /* do not assemble arrays when not all attributes are supported */
5579 if (imsm_check_attributes(mpb->attributes) == 0) {
5580 fprintf(stderr, Name ": IMSM metadata loading not allowed "
5581 "due to attributes incompatibility.\n");
5582 return NULL;
5583 }
5584
a06d022d
KW
5585 /* check for bad blocks */
5586 if (imsm_bbm_log_size(super->anchor))
5587 bbm_errors = 1;
604b746f 5588
abef11a3
AK
5589 /* count spare devices, not used in maps
5590 */
5591 for (d = super->disks; d; d = d->next)
5592 if (d->index == -1)
5593 spare_disks++;
5594
4f5bc454 5595 for (i = 0; i < mpb->num_raid_devs; i++) {
00bbdbda
N
5596 struct imsm_dev *dev;
5597 struct imsm_map *map;
86e3692b 5598 struct imsm_map *map2;
4f5bc454 5599 struct mdinfo *this;
2db86302 5600 int slot, chunk;
00bbdbda
N
5601 char *ep;
5602
5603 if (subarray &&
5604 (i != strtoul(subarray, &ep, 10) || *ep != '\0'))
5605 continue;
5606
5607 dev = get_imsm_dev(super, i);
5608 map = get_imsm_map(dev, 0);
86e3692b 5609 map2 = get_imsm_map(dev, 1);
4f5bc454 5610
1ce0101c
DW
5611 /* do not publish arrays that are in the middle of an
5612 * unsupported migration
5613 */
5614 if (dev->vol.migr_state &&
28bce06f 5615 (migr_type(dev) == MIGR_STATE_CHANGE)) {
1ce0101c
DW
5616 fprintf(stderr, Name ": cannot assemble volume '%.16s':"
5617 " unsupported migration in progress\n",
5618 dev->volume);
5619 continue;
5620 }
2db86302
LM
5621 /* do not publish arrays that are not support by controller's
5622 * OROM/EFI
5623 */
1ce0101c 5624
2db86302 5625 chunk = __le16_to_cpu(map->blocks_per_strip) >> 1;
7b0bbd0f 5626#ifndef MDASSEMBLE
2db86302
LM
5627 if (!validate_geometry_imsm_orom(super,
5628 get_imsm_raid_level(map), /* RAID level */
5629 imsm_level_to_layout(get_imsm_raid_level(map)),
5630 map->num_members, /* raid disks */
5631 &chunk,
5632 1 /* verbose */)) {
5633 fprintf(stderr, Name ": RAID gemetry validation failed. "
5634 "Cannot proceed with the action(s).\n");
5635 continue;
5636 }
7b0bbd0f 5637#endif /* MDASSEMBLE */
4f5bc454 5638 this = malloc(sizeof(*this));
0fbd635c 5639 if (!this) {
cf1be220 5640 fprintf(stderr, Name ": failed to allocate %zu bytes\n",
0fbd635c
AW
5641 sizeof(*this));
5642 break;
5643 }
4f5bc454 5644
301406c9 5645 super->current_vol = i;
a5d85af7 5646 getinfo_super_imsm_volume(st, this, NULL);
9894ec0d 5647 this->next = rest;
4f5bc454 5648 for (slot = 0 ; slot < map->num_members; slot++) {
1e5c6983 5649 unsigned long long recovery_start;
4f5bc454
DW
5650 struct mdinfo *info_d;
5651 struct dl *d;
5652 int idx;
9a1608e5 5653 int skip;
7eef0453 5654 __u32 ord;
4f5bc454 5655
9a1608e5 5656 skip = 0;
98130f40 5657 idx = get_imsm_disk_idx(dev, slot, 0);
196b0d44 5658 ord = get_imsm_ord_tbl_ent(dev, slot, -1);
4f5bc454
DW
5659 for (d = super->disks; d ; d = d->next)
5660 if (d->index == idx)
0fbd635c 5661 break;
4f5bc454 5662
1e5c6983 5663 recovery_start = MaxSector;
4f5bc454 5664 if (d == NULL)
9a1608e5 5665 skip = 1;
25ed7e59 5666 if (d && is_failed(&d->disk))
9a1608e5 5667 skip = 1;
7eef0453 5668 if (ord & IMSM_ORD_REBUILD)
1e5c6983 5669 recovery_start = 0;
9a1608e5
DW
5670
5671 /*
5672 * if we skip some disks the array will be assmebled degraded;
1e5c6983
DW
5673 * reset resync start to avoid a dirty-degraded
5674 * situation when performing the intial sync
9a1608e5
DW
5675 *
5676 * FIXME handle dirty degraded
5677 */
1e5c6983 5678 if ((skip || recovery_start == 0) && !dev->vol.dirty)
b7528a20 5679 this->resync_start = MaxSector;
9a1608e5
DW
5680 if (skip)
5681 continue;
4f5bc454 5682
1e5c6983 5683 info_d = calloc(1, sizeof(*info_d));
9a1608e5
DW
5684 if (!info_d) {
5685 fprintf(stderr, Name ": failed to allocate disk"
1ce0101c 5686 " for volume %.16s\n", dev->volume);
1e5c6983
DW
5687 info_d = this->devs;
5688 while (info_d) {
5689 struct mdinfo *d = info_d->next;
5690
5691 free(info_d);
5692 info_d = d;
5693 }
9a1608e5
DW
5694 free(this);
5695 this = rest;
5696 break;
5697 }
4f5bc454
DW
5698 info_d->next = this->devs;
5699 this->devs = info_d;
5700
4f5bc454
DW
5701 info_d->disk.number = d->index;
5702 info_d->disk.major = d->major;
5703 info_d->disk.minor = d->minor;
5704 info_d->disk.raid_disk = slot;
1e5c6983 5705 info_d->recovery_start = recovery_start;
86e3692b
AK
5706 if (map2) {
5707 if (slot < map2->num_members)
5708 info_d->disk.state = (1 << MD_DISK_ACTIVE);
04c3c514
AK
5709 else
5710 this->array.spare_disks++;
86e3692b
AK
5711 } else {
5712 if (slot < map->num_members)
5713 info_d->disk.state = (1 << MD_DISK_ACTIVE);
04c3c514
AK
5714 else
5715 this->array.spare_disks++;
86e3692b 5716 }
1e5c6983
DW
5717 if (info_d->recovery_start == MaxSector)
5718 this->array.working_disks++;
4f5bc454
DW
5719
5720 info_d->events = __le32_to_cpu(mpb->generation_num);
5721 info_d->data_offset = __le32_to_cpu(map->pba_of_lba0);
5722 info_d->component_size = __le32_to_cpu(map->blocks_per_member);
4f5bc454 5723 }
1e5c6983 5724 /* now that the disk list is up-to-date fixup recovery_start */
c47b0ff6 5725 update_recovery_start(super, dev, this);
abef11a3 5726 this->array.spare_disks += spare_disks;
276d77db 5727
9e2d750d 5728#ifndef MDASSEMBLE
276d77db
AK
5729 /* check for reshape */
5730 if (this->reshape_active == 1)
5731 recover_backup_imsm(st, this);
9e2d750d 5732#endif
9a1608e5 5733 rest = this;
4f5bc454
DW
5734 }
5735
a06d022d
KW
5736 /* if array has bad blocks, set suitable bit in array status */
5737 if (bbm_errors)
5738 rest->array.state |= (1<<MD_SB_BBM_ERRORS);
5739
4f5bc454 5740 return rest;
cdddbdbc
DW
5741}
5742
845dea95 5743
fb49eef2 5744static __u8 imsm_check_degraded(struct intel_super *super, struct imsm_dev *dev, int failed)
c2a1e7da 5745{
a965f303 5746 struct imsm_map *map = get_imsm_map(dev, 0);
c2a1e7da
DW
5747
5748 if (!failed)
3393c6af
DW
5749 return map->map_state == IMSM_T_STATE_UNINITIALIZED ?
5750 IMSM_T_STATE_UNINITIALIZED : IMSM_T_STATE_NORMAL;
c2a1e7da
DW
5751
5752 switch (get_imsm_raid_level(map)) {
5753 case 0:
5754 return IMSM_T_STATE_FAILED;
5755 break;
5756 case 1:
5757 if (failed < map->num_members)
5758 return IMSM_T_STATE_DEGRADED;
5759 else
5760 return IMSM_T_STATE_FAILED;
5761 break;
5762 case 10:
5763 {
5764 /**
c92a2527
DW
5765 * check to see if any mirrors have failed, otherwise we
5766 * are degraded. Even numbered slots are mirrored on
5767 * slot+1
c2a1e7da 5768 */
c2a1e7da 5769 int i;
d9b420a5
N
5770 /* gcc -Os complains that this is unused */
5771 int insync = insync;
c2a1e7da
DW
5772
5773 for (i = 0; i < map->num_members; i++) {
98130f40 5774 __u32 ord = get_imsm_ord_tbl_ent(dev, i, -1);
c92a2527
DW
5775 int idx = ord_to_idx(ord);
5776 struct imsm_disk *disk;
c2a1e7da 5777
c92a2527
DW
5778 /* reset the potential in-sync count on even-numbered
5779 * slots. num_copies is always 2 for imsm raid10
5780 */
5781 if ((i & 1) == 0)
5782 insync = 2;
c2a1e7da 5783
c92a2527 5784 disk = get_imsm_disk(super, idx);
25ed7e59 5785 if (!disk || is_failed(disk) || ord & IMSM_ORD_REBUILD)
c92a2527 5786 insync--;
c2a1e7da 5787
c92a2527
DW
5788 /* no in-sync disks left in this mirror the
5789 * array has failed
5790 */
5791 if (insync == 0)
5792 return IMSM_T_STATE_FAILED;
c2a1e7da
DW
5793 }
5794
5795 return IMSM_T_STATE_DEGRADED;
5796 }
5797 case 5:
5798 if (failed < 2)
5799 return IMSM_T_STATE_DEGRADED;
5800 else
5801 return IMSM_T_STATE_FAILED;
5802 break;
5803 default:
5804 break;
5805 }
5806
5807 return map->map_state;
5808}
5809
ff077194 5810static int imsm_count_failed(struct intel_super *super, struct imsm_dev *dev)
c2a1e7da
DW
5811{
5812 int i;
5813 int failed = 0;
5814 struct imsm_disk *disk;
ff077194 5815 struct imsm_map *map = get_imsm_map(dev, 0);
0556e1a2
DW
5816 struct imsm_map *prev = get_imsm_map(dev, dev->vol.migr_state);
5817 __u32 ord;
5818 int idx;
c2a1e7da 5819
0556e1a2
DW
5820 /* at the beginning of migration we set IMSM_ORD_REBUILD on
5821 * disks that are being rebuilt. New failures are recorded to
5822 * map[0]. So we look through all the disks we started with and
5823 * see if any failures are still present, or if any new ones
5824 * have arrived
5825 *
5826 * FIXME add support for online capacity expansion and
5827 * raid-level-migration
5828 */
5829 for (i = 0; i < prev->num_members; i++) {
5830 ord = __le32_to_cpu(prev->disk_ord_tbl[i]);
5831 ord |= __le32_to_cpu(map->disk_ord_tbl[i]);
5832 idx = ord_to_idx(ord);
c2a1e7da 5833
949c47a0 5834 disk = get_imsm_disk(super, idx);
25ed7e59 5835 if (!disk || is_failed(disk) || ord & IMSM_ORD_REBUILD)
fcb84475 5836 failed++;
c2a1e7da
DW
5837 }
5838
5839 return failed;
845dea95
NB
5840}
5841
97b4d0e9
DW
5842#ifndef MDASSEMBLE
5843static int imsm_open_new(struct supertype *c, struct active_array *a,
5844 char *inst)
5845{
5846 struct intel_super *super = c->sb;
5847 struct imsm_super *mpb = super->anchor;
5848
5849 if (atoi(inst) >= mpb->num_raid_devs) {
5850 fprintf(stderr, "%s: subarry index %d, out of range\n",
5851 __func__, atoi(inst));
5852 return -ENODEV;
5853 }
5854
5855 dprintf("imsm: open_new %s\n", inst);
5856 a->info.container_member = atoi(inst);
5857 return 0;
5858}
5859
0c046afd
DW
5860static int is_resyncing(struct imsm_dev *dev)
5861{
5862 struct imsm_map *migr_map;
5863
5864 if (!dev->vol.migr_state)
5865 return 0;
5866
1484e727
DW
5867 if (migr_type(dev) == MIGR_INIT ||
5868 migr_type(dev) == MIGR_REPAIR)
0c046afd
DW
5869 return 1;
5870
4c9bc37b
AK
5871 if (migr_type(dev) == MIGR_GEN_MIGR)
5872 return 0;
5873
0c046afd
DW
5874 migr_map = get_imsm_map(dev, 1);
5875
4c9bc37b
AK
5876 if ((migr_map->map_state == IMSM_T_STATE_NORMAL) &&
5877 (dev->vol.migr_type != MIGR_GEN_MIGR))
0c046afd
DW
5878 return 1;
5879 else
5880 return 0;
5881}
5882
0556e1a2
DW
5883/* return true if we recorded new information */
5884static int mark_failure(struct imsm_dev *dev, struct imsm_disk *disk, int idx)
47ee5a45 5885{
0556e1a2
DW
5886 __u32 ord;
5887 int slot;
5888 struct imsm_map *map;
86c54047
DW
5889 char buf[MAX_RAID_SERIAL_LEN+3];
5890 unsigned int len, shift = 0;
0556e1a2
DW
5891
5892 /* new failures are always set in map[0] */
5893 map = get_imsm_map(dev, 0);
5894
5895 slot = get_imsm_disk_slot(map, idx);
5896 if (slot < 0)
5897 return 0;
5898
5899 ord = __le32_to_cpu(map->disk_ord_tbl[slot]);
25ed7e59 5900 if (is_failed(disk) && (ord & IMSM_ORD_REBUILD))
0556e1a2
DW
5901 return 0;
5902
86c54047
DW
5903 sprintf(buf, "%s:0", disk->serial);
5904 if ((len = strlen(buf)) >= MAX_RAID_SERIAL_LEN)
5905 shift = len - MAX_RAID_SERIAL_LEN + 1;
5906 strncpy((char *)disk->serial, &buf[shift], MAX_RAID_SERIAL_LEN);
5907
f2f27e63 5908 disk->status |= FAILED_DISK;
0556e1a2 5909 set_imsm_ord_tbl_ent(map, slot, idx | IMSM_ORD_REBUILD);
f21e18ca 5910 if (map->failed_disk_num == 0xff)
0556e1a2
DW
5911 map->failed_disk_num = slot;
5912 return 1;
5913}
5914
5915static void mark_missing(struct imsm_dev *dev, struct imsm_disk *disk, int idx)
5916{
5917 mark_failure(dev, disk, idx);
5918
5919 if (disk->scsi_id == __cpu_to_le32(~(__u32)0))
5920 return;
5921
47ee5a45
DW
5922 disk->scsi_id = __cpu_to_le32(~(__u32)0);
5923 memmove(&disk->serial[0], &disk->serial[1], MAX_RAID_SERIAL_LEN - 1);
5924}
5925
33414a01
DW
5926static void handle_missing(struct intel_super *super, struct imsm_dev *dev)
5927{
5928 __u8 map_state;
5929 struct dl *dl;
5930 int failed;
5931
5932 if (!super->missing)
5933 return;
5934 failed = imsm_count_failed(super, dev);
5935 map_state = imsm_check_degraded(super, dev, failed);
5936
5937 dprintf("imsm: mark missing\n");
5938 end_migration(dev, map_state);
5939 for (dl = super->missing; dl; dl = dl->next)
5940 mark_missing(dev, &dl->disk, dl->index);
5941 super->updates_pending++;
5942}
5943
70bdf0dc
AK
5944static unsigned long long imsm_set_array_size(struct imsm_dev *dev)
5945{
5946 int used_disks = imsm_num_data_members(dev, 0);
5947 unsigned long long array_blocks;
5948 struct imsm_map *map;
5949
5950 if (used_disks == 0) {
5951 /* when problems occures
5952 * return current array_blocks value
5953 */
5954 array_blocks = __le32_to_cpu(dev->size_high);
5955 array_blocks = array_blocks << 32;
5956 array_blocks += __le32_to_cpu(dev->size_low);
5957
5958 return array_blocks;
5959 }
5960
5961 /* set array size in metadata
5962 */
5963 map = get_imsm_map(dev, 0);
5964 array_blocks = map->blocks_per_member * used_disks;
5965
5966 /* round array size down to closest MB
5967 */
5968 array_blocks = (array_blocks >> SECT_PER_MB_SHIFT) << SECT_PER_MB_SHIFT;
5969 dev->size_low = __cpu_to_le32((__u32)array_blocks);
5970 dev->size_high = __cpu_to_le32((__u32)(array_blocks >> 32));
5971
5972 return array_blocks;
5973}
5974
28bce06f
AK
5975static void imsm_set_disk(struct active_array *a, int n, int state);
5976
0e2d1a4e
AK
5977static void imsm_progress_container_reshape(struct intel_super *super)
5978{
5979 /* if no device has a migr_state, but some device has a
5980 * different number of members than the previous device, start
5981 * changing the number of devices in this device to match
5982 * previous.
5983 */
5984 struct imsm_super *mpb = super->anchor;
5985 int prev_disks = -1;
5986 int i;
1dfaa380 5987 int copy_map_size;
0e2d1a4e
AK
5988
5989 for (i = 0; i < mpb->num_raid_devs; i++) {
5990 struct imsm_dev *dev = get_imsm_dev(super, i);
5991 struct imsm_map *map = get_imsm_map(dev, 0);
5992 struct imsm_map *map2;
5993 int prev_num_members;
0e2d1a4e
AK
5994
5995 if (dev->vol.migr_state)
5996 return;
5997
5998 if (prev_disks == -1)
5999 prev_disks = map->num_members;
6000 if (prev_disks == map->num_members)
6001 continue;
6002
6003 /* OK, this array needs to enter reshape mode.
6004 * i.e it needs a migr_state
6005 */
6006
1dfaa380 6007 copy_map_size = sizeof_imsm_map(map);
0e2d1a4e
AK
6008 prev_num_members = map->num_members;
6009 map->num_members = prev_disks;
6010 dev->vol.migr_state = 1;
6011 dev->vol.curr_migr_unit = 0;
6012 dev->vol.migr_type = MIGR_GEN_MIGR;
6013 for (i = prev_num_members;
6014 i < map->num_members; i++)
6015 set_imsm_ord_tbl_ent(map, i, i);
6016 map2 = get_imsm_map(dev, 1);
6017 /* Copy the current map */
1dfaa380 6018 memcpy(map2, map, copy_map_size);
0e2d1a4e
AK
6019 map2->num_members = prev_num_members;
6020
70bdf0dc 6021 imsm_set_array_size(dev);
0e2d1a4e
AK
6022 super->updates_pending++;
6023 }
6024}
6025
aad6f216 6026/* Handle dirty -> clean transititions, resync and reshape. Degraded and rebuild
0c046afd
DW
6027 * states are handled in imsm_set_disk() with one exception, when a
6028 * resync is stopped due to a new failure this routine will set the
6029 * 'degraded' state for the array.
6030 */
01f157d7 6031static int imsm_set_array_state(struct active_array *a, int consistent)
a862209d
DW
6032{
6033 int inst = a->info.container_member;
6034 struct intel_super *super = a->container->sb;
949c47a0 6035 struct imsm_dev *dev = get_imsm_dev(super, inst);
a965f303 6036 struct imsm_map *map = get_imsm_map(dev, 0);
0c046afd
DW
6037 int failed = imsm_count_failed(super, dev);
6038 __u8 map_state = imsm_check_degraded(super, dev, failed);
1e5c6983 6039 __u32 blocks_per_unit;
a862209d 6040
1af97990
AK
6041 if (dev->vol.migr_state &&
6042 dev->vol.migr_type == MIGR_GEN_MIGR) {
6043 /* array state change is blocked due to reshape action
aad6f216
N
6044 * We might need to
6045 * - abort the reshape (if last_checkpoint is 0 and action!= reshape)
6046 * - finish the reshape (if last_checkpoint is big and action != reshape)
6047 * - update curr_migr_unit
1af97990 6048 */
aad6f216
N
6049 if (a->curr_action == reshape) {
6050 /* still reshaping, maybe update curr_migr_unit */
633b5610 6051 goto mark_checkpoint;
aad6f216
N
6052 } else {
6053 if (a->last_checkpoint == 0 && a->prev_action == reshape) {
6054 /* for some reason we aborted the reshape.
b66e591b
AK
6055 *
6056 * disable automatic metadata rollback
6057 * user action is required to recover process
aad6f216 6058 */
b66e591b 6059 if (0) {
aad6f216
N
6060 struct imsm_map *map2 = get_imsm_map(dev, 1);
6061 dev->vol.migr_state = 0;
6062 dev->vol.migr_type = 0;
6063 dev->vol.curr_migr_unit = 0;
6064 memcpy(map, map2, sizeof_imsm_map(map2));
6065 super->updates_pending++;
b66e591b 6066 }
aad6f216
N
6067 }
6068 if (a->last_checkpoint >= a->info.component_size) {
6069 unsigned long long array_blocks;
6070 int used_disks;
e154ced3 6071 struct mdinfo *mdi;
aad6f216 6072
9653001d 6073 used_disks = imsm_num_data_members(dev, 0);
d55adef9
AK
6074 if (used_disks > 0) {
6075 array_blocks =
6076 map->blocks_per_member *
6077 used_disks;
6078 /* round array size down to closest MB
6079 */
6080 array_blocks = (array_blocks
6081 >> SECT_PER_MB_SHIFT)
6082 << SECT_PER_MB_SHIFT;
d55adef9
AK
6083 a->info.custom_array_size = array_blocks;
6084 /* encourage manager to update array
6085 * size
6086 */
e154ced3 6087
d55adef9 6088 a->check_reshape = 1;
633b5610 6089 }
e154ced3
AK
6090 /* finalize online capacity expansion/reshape */
6091 for (mdi = a->info.devs; mdi; mdi = mdi->next)
6092 imsm_set_disk(a,
6093 mdi->disk.raid_disk,
6094 mdi->curr_state);
6095
0e2d1a4e 6096 imsm_progress_container_reshape(super);
e154ced3 6097 }
aad6f216 6098 }
1af97990
AK
6099 }
6100
47ee5a45 6101 /* before we activate this array handle any missing disks */
33414a01
DW
6102 if (consistent == 2)
6103 handle_missing(super, dev);
1e5c6983 6104
0c046afd 6105 if (consistent == 2 &&
b7941fd6 6106 (!is_resync_complete(&a->info) ||
0c046afd
DW
6107 map_state != IMSM_T_STATE_NORMAL ||
6108 dev->vol.migr_state))
01f157d7 6109 consistent = 0;
272906ef 6110
b7941fd6 6111 if (is_resync_complete(&a->info)) {
0c046afd 6112 /* complete intialization / resync,
0556e1a2
DW
6113 * recovery and interrupted recovery is completed in
6114 * ->set_disk
0c046afd
DW
6115 */
6116 if (is_resyncing(dev)) {
6117 dprintf("imsm: mark resync done\n");
f8f603f1 6118 end_migration(dev, map_state);
115c3803 6119 super->updates_pending++;
484240d8 6120 a->last_checkpoint = 0;
115c3803 6121 }
0c046afd
DW
6122 } else if (!is_resyncing(dev) && !failed) {
6123 /* mark the start of the init process if nothing is failed */
b7941fd6 6124 dprintf("imsm: mark resync start\n");
1484e727 6125 if (map->map_state == IMSM_T_STATE_UNINITIALIZED)
8e59f3d8 6126 migrate(dev, super, IMSM_T_STATE_NORMAL, MIGR_INIT);
1484e727 6127 else
8e59f3d8 6128 migrate(dev, super, IMSM_T_STATE_NORMAL, MIGR_REPAIR);
3393c6af 6129 super->updates_pending++;
115c3803 6130 }
a862209d 6131
633b5610 6132mark_checkpoint:
5b83bacf
AK
6133 /* skip checkpointing for general migration,
6134 * it is controlled in mdadm
6135 */
6136 if (is_gen_migration(dev))
6137 goto skip_mark_checkpoint;
6138
1e5c6983 6139 /* check if we can update curr_migr_unit from resync_start, recovery_start */
c47b0ff6 6140 blocks_per_unit = blocks_per_migr_unit(super, dev);
4f0a7acc 6141 if (blocks_per_unit) {
1e5c6983
DW
6142 __u32 units32;
6143 __u64 units;
6144
4f0a7acc 6145 units = a->last_checkpoint / blocks_per_unit;
1e5c6983
DW
6146 units32 = units;
6147
6148 /* check that we did not overflow 32-bits, and that
6149 * curr_migr_unit needs updating
6150 */
6151 if (units32 == units &&
bfd80a56 6152 units32 != 0 &&
1e5c6983
DW
6153 __le32_to_cpu(dev->vol.curr_migr_unit) != units32) {
6154 dprintf("imsm: mark checkpoint (%u)\n", units32);
6155 dev->vol.curr_migr_unit = __cpu_to_le32(units32);
6156 super->updates_pending++;
6157 }
6158 }
f8f603f1 6159
5b83bacf 6160skip_mark_checkpoint:
3393c6af 6161 /* mark dirty / clean */
0c046afd 6162 if (dev->vol.dirty != !consistent) {
b7941fd6 6163 dprintf("imsm: mark '%s'\n", consistent ? "clean" : "dirty");
0c046afd
DW
6164 if (consistent)
6165 dev->vol.dirty = 0;
6166 else
6167 dev->vol.dirty = 1;
a862209d
DW
6168 super->updates_pending++;
6169 }
28bce06f 6170
01f157d7 6171 return consistent;
a862209d
DW
6172}
6173
8d45d196 6174static void imsm_set_disk(struct active_array *a, int n, int state)
845dea95 6175{
8d45d196
DW
6176 int inst = a->info.container_member;
6177 struct intel_super *super = a->container->sb;
949c47a0 6178 struct imsm_dev *dev = get_imsm_dev(super, inst);
a965f303 6179 struct imsm_map *map = get_imsm_map(dev, 0);
8d45d196 6180 struct imsm_disk *disk;
0c046afd 6181 int failed;
b10b37b8 6182 __u32 ord;
0c046afd 6183 __u8 map_state;
8d45d196
DW
6184
6185 if (n > map->num_members)
6186 fprintf(stderr, "imsm: set_disk %d out of range 0..%d\n",
6187 n, map->num_members - 1);
6188
6189 if (n < 0)
6190 return;
6191
4e6e574a 6192 dprintf("imsm: set_disk %d:%x\n", n, state);
8d45d196 6193
98130f40 6194 ord = get_imsm_ord_tbl_ent(dev, n, -1);
b10b37b8 6195 disk = get_imsm_disk(super, ord_to_idx(ord));
8d45d196 6196
5802a811 6197 /* check for new failures */
0556e1a2
DW
6198 if (state & DS_FAULTY) {
6199 if (mark_failure(dev, disk, ord_to_idx(ord)))
6200 super->updates_pending++;
8d45d196 6201 }
47ee5a45 6202
19859edc 6203 /* check if in_sync */
0556e1a2 6204 if (state & DS_INSYNC && ord & IMSM_ORD_REBUILD && is_rebuilding(dev)) {
b10b37b8
DW
6205 struct imsm_map *migr_map = get_imsm_map(dev, 1);
6206
6207 set_imsm_ord_tbl_ent(migr_map, n, ord_to_idx(ord));
19859edc
DW
6208 super->updates_pending++;
6209 }
8d45d196 6210
0c046afd
DW
6211 failed = imsm_count_failed(super, dev);
6212 map_state = imsm_check_degraded(super, dev, failed);
5802a811 6213
0c046afd
DW
6214 /* check if recovery complete, newly degraded, or failed */
6215 if (map_state == IMSM_T_STATE_NORMAL && is_rebuilding(dev)) {
f8f603f1 6216 end_migration(dev, map_state);
0556e1a2
DW
6217 map = get_imsm_map(dev, 0);
6218 map->failed_disk_num = ~0;
0c046afd 6219 super->updates_pending++;
484240d8 6220 a->last_checkpoint = 0;
0c046afd
DW
6221 } else if (map_state == IMSM_T_STATE_DEGRADED &&
6222 map->map_state != map_state &&
6223 !dev->vol.migr_state) {
6224 dprintf("imsm: mark degraded\n");
6225 map->map_state = map_state;
6226 super->updates_pending++;
484240d8 6227 a->last_checkpoint = 0;
0c046afd
DW
6228 } else if (map_state == IMSM_T_STATE_FAILED &&
6229 map->map_state != map_state) {
6230 dprintf("imsm: mark failed\n");
f8f603f1 6231 end_migration(dev, map_state);
0c046afd 6232 super->updates_pending++;
484240d8 6233 a->last_checkpoint = 0;
28bce06f
AK
6234 } else if (is_gen_migration(dev)) {
6235 dprintf("imsm: Detected General Migration in state: ");
6236 if (map_state == IMSM_T_STATE_NORMAL) {
6237 end_migration(dev, map_state);
6238 map = get_imsm_map(dev, 0);
6239 map->failed_disk_num = ~0;
6240 dprintf("normal\n");
6241 } else {
6242 if (map_state == IMSM_T_STATE_DEGRADED) {
6243 printf("degraded\n");
6244 end_migration(dev, map_state);
6245 } else {
6246 dprintf("failed\n");
6247 }
6248 map->map_state = map_state;
6249 }
6250 super->updates_pending++;
5802a811 6251 }
845dea95
NB
6252}
6253
f796af5d 6254static int store_imsm_mpb(int fd, struct imsm_super *mpb)
c2a1e7da 6255{
f796af5d 6256 void *buf = mpb;
c2a1e7da
DW
6257 __u32 mpb_size = __le32_to_cpu(mpb->mpb_size);
6258 unsigned long long dsize;
6259 unsigned long long sectors;
6260
6261 get_dev_size(fd, NULL, &dsize);
6262
272f648f
DW
6263 if (mpb_size > 512) {
6264 /* -1 to account for anchor */
6265 sectors = mpb_sectors(mpb) - 1;
c2a1e7da 6266
272f648f
DW
6267 /* write the extended mpb to the sectors preceeding the anchor */
6268 if (lseek64(fd, dsize - (512 * (2 + sectors)), SEEK_SET) < 0)
6269 return 1;
c2a1e7da 6270
f21e18ca
N
6271 if ((unsigned long long)write(fd, buf + 512, 512 * sectors)
6272 != 512 * sectors)
272f648f
DW
6273 return 1;
6274 }
c2a1e7da 6275
272f648f
DW
6276 /* first block is stored on second to last sector of the disk */
6277 if (lseek64(fd, dsize - (512 * 2), SEEK_SET) < 0)
c2a1e7da
DW
6278 return 1;
6279
f796af5d 6280 if (write(fd, buf, 512) != 512)
c2a1e7da
DW
6281 return 1;
6282
c2a1e7da
DW
6283 return 0;
6284}
6285
2e735d19 6286static void imsm_sync_metadata(struct supertype *container)
845dea95 6287{
2e735d19 6288 struct intel_super *super = container->sb;
c2a1e7da 6289
1a64be56 6290 dprintf("sync metadata: %d\n", super->updates_pending);
c2a1e7da
DW
6291 if (!super->updates_pending)
6292 return;
6293
36988a3d 6294 write_super_imsm(container, 0);
c2a1e7da
DW
6295
6296 super->updates_pending = 0;
845dea95
NB
6297}
6298
272906ef
DW
6299static struct dl *imsm_readd(struct intel_super *super, int idx, struct active_array *a)
6300{
6301 struct imsm_dev *dev = get_imsm_dev(super, a->info.container_member);
98130f40 6302 int i = get_imsm_disk_idx(dev, idx, -1);
272906ef
DW
6303 struct dl *dl;
6304
6305 for (dl = super->disks; dl; dl = dl->next)
6306 if (dl->index == i)
6307 break;
6308
25ed7e59 6309 if (dl && is_failed(&dl->disk))
272906ef
DW
6310 dl = NULL;
6311
6312 if (dl)
6313 dprintf("%s: found %x:%x\n", __func__, dl->major, dl->minor);
6314
6315 return dl;
6316}
6317
a20d2ba5 6318static struct dl *imsm_add_spare(struct intel_super *super, int slot,
8ba77d32
AK
6319 struct active_array *a, int activate_new,
6320 struct mdinfo *additional_test_list)
272906ef
DW
6321{
6322 struct imsm_dev *dev = get_imsm_dev(super, a->info.container_member);
98130f40 6323 int idx = get_imsm_disk_idx(dev, slot, -1);
a20d2ba5
DW
6324 struct imsm_super *mpb = super->anchor;
6325 struct imsm_map *map;
272906ef
DW
6326 unsigned long long pos;
6327 struct mdinfo *d;
6328 struct extent *ex;
a20d2ba5 6329 int i, j;
272906ef 6330 int found;
569cc43f
DW
6331 __u32 array_start = 0;
6332 __u32 array_end = 0;
272906ef 6333 struct dl *dl;
6c932028 6334 struct mdinfo *test_list;
272906ef
DW
6335
6336 for (dl = super->disks; dl; dl = dl->next) {
6337 /* If in this array, skip */
6338 for (d = a->info.devs ; d ; d = d->next)
e553d2a4
DW
6339 if (d->state_fd >= 0 &&
6340 d->disk.major == dl->major &&
272906ef 6341 d->disk.minor == dl->minor) {
8ba77d32
AK
6342 dprintf("%x:%x already in array\n",
6343 dl->major, dl->minor);
272906ef
DW
6344 break;
6345 }
6346 if (d)
6347 continue;
6c932028
AK
6348 test_list = additional_test_list;
6349 while (test_list) {
6350 if (test_list->disk.major == dl->major &&
6351 test_list->disk.minor == dl->minor) {
8ba77d32
AK
6352 dprintf("%x:%x already in additional test list\n",
6353 dl->major, dl->minor);
6354 break;
6355 }
6c932028 6356 test_list = test_list->next;
8ba77d32 6357 }
6c932028 6358 if (test_list)
8ba77d32 6359 continue;
272906ef 6360
e553d2a4 6361 /* skip in use or failed drives */
25ed7e59 6362 if (is_failed(&dl->disk) || idx == dl->index ||
df474657
DW
6363 dl->index == -2) {
6364 dprintf("%x:%x status (failed: %d index: %d)\n",
25ed7e59 6365 dl->major, dl->minor, is_failed(&dl->disk), idx);
9a1608e5
DW
6366 continue;
6367 }
6368
a20d2ba5
DW
6369 /* skip pure spares when we are looking for partially
6370 * assimilated drives
6371 */
6372 if (dl->index == -1 && !activate_new)
6373 continue;
6374
272906ef 6375 /* Does this unused device have the requisite free space?
a20d2ba5 6376 * It needs to be able to cover all member volumes
272906ef
DW
6377 */
6378 ex = get_extents(super, dl);
6379 if (!ex) {
6380 dprintf("cannot get extents\n");
6381 continue;
6382 }
a20d2ba5
DW
6383 for (i = 0; i < mpb->num_raid_devs; i++) {
6384 dev = get_imsm_dev(super, i);
6385 map = get_imsm_map(dev, 0);
272906ef 6386
a20d2ba5
DW
6387 /* check if this disk is already a member of
6388 * this array
272906ef 6389 */
620b1713 6390 if (get_imsm_disk_slot(map, dl->index) >= 0)
a20d2ba5
DW
6391 continue;
6392
6393 found = 0;
6394 j = 0;
6395 pos = 0;
6396 array_start = __le32_to_cpu(map->pba_of_lba0);
329c8278
DW
6397 array_end = array_start +
6398 __le32_to_cpu(map->blocks_per_member) - 1;
a20d2ba5
DW
6399
6400 do {
6401 /* check that we can start at pba_of_lba0 with
6402 * blocks_per_member of space
6403 */
329c8278 6404 if (array_start >= pos && array_end < ex[j].start) {
a20d2ba5
DW
6405 found = 1;
6406 break;
6407 }
6408 pos = ex[j].start + ex[j].size;
6409 j++;
6410 } while (ex[j-1].size);
6411
6412 if (!found)
272906ef 6413 break;
a20d2ba5 6414 }
272906ef
DW
6415
6416 free(ex);
a20d2ba5 6417 if (i < mpb->num_raid_devs) {
329c8278
DW
6418 dprintf("%x:%x does not have %u to %u available\n",
6419 dl->major, dl->minor, array_start, array_end);
272906ef
DW
6420 /* No room */
6421 continue;
a20d2ba5
DW
6422 }
6423 return dl;
272906ef
DW
6424 }
6425
6426 return dl;
6427}
6428
95d07a2c
LM
6429
6430static int imsm_rebuild_allowed(struct supertype *cont, int dev_idx, int failed)
6431{
6432 struct imsm_dev *dev2;
6433 struct imsm_map *map;
6434 struct dl *idisk;
6435 int slot;
6436 int idx;
6437 __u8 state;
6438
6439 dev2 = get_imsm_dev(cont->sb, dev_idx);
6440 if (dev2) {
6441 state = imsm_check_degraded(cont->sb, dev2, failed);
6442 if (state == IMSM_T_STATE_FAILED) {
6443 map = get_imsm_map(dev2, 0);
6444 if (!map)
6445 return 1;
6446 for (slot = 0; slot < map->num_members; slot++) {
6447 /*
6448 * Check if failed disks are deleted from intel
6449 * disk list or are marked to be deleted
6450 */
98130f40 6451 idx = get_imsm_disk_idx(dev2, slot, -1);
95d07a2c
LM
6452 idisk = get_imsm_dl_disk(cont->sb, idx);
6453 /*
6454 * Do not rebuild the array if failed disks
6455 * from failed sub-array are not removed from
6456 * container.
6457 */
6458 if (idisk &&
6459 is_failed(&idisk->disk) &&
6460 (idisk->action != DISK_REMOVE))
6461 return 0;
6462 }
6463 }
6464 }
6465 return 1;
6466}
6467
88758e9d
DW
6468static struct mdinfo *imsm_activate_spare(struct active_array *a,
6469 struct metadata_update **updates)
6470{
6471 /**
d23fe947
DW
6472 * Find a device with unused free space and use it to replace a
6473 * failed/vacant region in an array. We replace failed regions one a
6474 * array at a time. The result is that a new spare disk will be added
6475 * to the first failed array and after the monitor has finished
6476 * propagating failures the remainder will be consumed.
88758e9d 6477 *
d23fe947
DW
6478 * FIXME add a capability for mdmon to request spares from another
6479 * container.
88758e9d
DW
6480 */
6481
6482 struct intel_super *super = a->container->sb;
88758e9d 6483 int inst = a->info.container_member;
949c47a0 6484 struct imsm_dev *dev = get_imsm_dev(super, inst);
a965f303 6485 struct imsm_map *map = get_imsm_map(dev, 0);
88758e9d
DW
6486 int failed = a->info.array.raid_disks;
6487 struct mdinfo *rv = NULL;
6488 struct mdinfo *d;
6489 struct mdinfo *di;
6490 struct metadata_update *mu;
6491 struct dl *dl;
6492 struct imsm_update_activate_spare *u;
6493 int num_spares = 0;
6494 int i;
95d07a2c 6495 int allowed;
88758e9d
DW
6496
6497 for (d = a->info.devs ; d ; d = d->next) {
6498 if ((d->curr_state & DS_FAULTY) &&
6499 d->state_fd >= 0)
6500 /* wait for Removal to happen */
6501 return NULL;
6502 if (d->state_fd >= 0)
6503 failed--;
6504 }
6505
6506 dprintf("imsm: activate spare: inst=%d failed=%d (%d) level=%d\n",
6507 inst, failed, a->info.array.raid_disks, a->info.array.level);
1af97990
AK
6508
6509 if (dev->vol.migr_state &&
6510 dev->vol.migr_type == MIGR_GEN_MIGR)
6511 /* No repair during migration */
6512 return NULL;
6513
89c67882
AK
6514 if (a->info.array.level == 4)
6515 /* No repair for takeovered array
6516 * imsm doesn't support raid4
6517 */
6518 return NULL;
6519
fb49eef2 6520 if (imsm_check_degraded(super, dev, failed) != IMSM_T_STATE_DEGRADED)
88758e9d
DW
6521 return NULL;
6522
95d07a2c
LM
6523 /*
6524 * If there are any failed disks check state of the other volume.
6525 * Block rebuild if the another one is failed until failed disks
6526 * are removed from container.
6527 */
6528 if (failed) {
6529 dprintf("found failed disks in %s, check if there another"
6530 "failed sub-array.\n",
6531 dev->volume);
6532 /* check if states of the other volumes allow for rebuild */
6533 for (i = 0; i < super->anchor->num_raid_devs; i++) {
6534 if (i != inst) {
6535 allowed = imsm_rebuild_allowed(a->container,
6536 i, failed);
6537 if (!allowed)
6538 return NULL;
6539 }
6540 }
6541 }
6542
88758e9d 6543 /* For each slot, if it is not working, find a spare */
88758e9d
DW
6544 for (i = 0; i < a->info.array.raid_disks; i++) {
6545 for (d = a->info.devs ; d ; d = d->next)
6546 if (d->disk.raid_disk == i)
6547 break;
6548 dprintf("found %d: %p %x\n", i, d, d?d->curr_state:0);
6549 if (d && (d->state_fd >= 0))
6550 continue;
6551
272906ef 6552 /*
a20d2ba5
DW
6553 * OK, this device needs recovery. Try to re-add the
6554 * previous occupant of this slot, if this fails see if
6555 * we can continue the assimilation of a spare that was
6556 * partially assimilated, finally try to activate a new
6557 * spare.
272906ef
DW
6558 */
6559 dl = imsm_readd(super, i, a);
6560 if (!dl)
8ba77d32 6561 dl = imsm_add_spare(super, i, a, 0, NULL);
a20d2ba5 6562 if (!dl)
8ba77d32 6563 dl = imsm_add_spare(super, i, a, 1, NULL);
272906ef
DW
6564 if (!dl)
6565 continue;
6566
6567 /* found a usable disk with enough space */
6568 di = malloc(sizeof(*di));
79244939
DW
6569 if (!di)
6570 continue;
272906ef
DW
6571 memset(di, 0, sizeof(*di));
6572
6573 /* dl->index will be -1 in the case we are activating a
6574 * pristine spare. imsm_process_update() will create a
6575 * new index in this case. Once a disk is found to be
6576 * failed in all member arrays it is kicked from the
6577 * metadata
6578 */
6579 di->disk.number = dl->index;
d23fe947 6580
272906ef
DW
6581 /* (ab)use di->devs to store a pointer to the device
6582 * we chose
6583 */
6584 di->devs = (struct mdinfo *) dl;
6585
6586 di->disk.raid_disk = i;
6587 di->disk.major = dl->major;
6588 di->disk.minor = dl->minor;
6589 di->disk.state = 0;
d23534e4 6590 di->recovery_start = 0;
272906ef
DW
6591 di->data_offset = __le32_to_cpu(map->pba_of_lba0);
6592 di->component_size = a->info.component_size;
6593 di->container_member = inst;
148acb7b 6594 super->random = random32();
272906ef
DW
6595 di->next = rv;
6596 rv = di;
6597 num_spares++;
6598 dprintf("%x:%x to be %d at %llu\n", dl->major, dl->minor,
6599 i, di->data_offset);
88758e9d 6600
272906ef 6601 break;
88758e9d
DW
6602 }
6603
6604 if (!rv)
6605 /* No spares found */
6606 return rv;
6607 /* Now 'rv' has a list of devices to return.
6608 * Create a metadata_update record to update the
6609 * disk_ord_tbl for the array
6610 */
6611 mu = malloc(sizeof(*mu));
79244939
DW
6612 if (mu) {
6613 mu->buf = malloc(sizeof(struct imsm_update_activate_spare) * num_spares);
6614 if (mu->buf == NULL) {
6615 free(mu);
6616 mu = NULL;
6617 }
6618 }
6619 if (!mu) {
6620 while (rv) {
6621 struct mdinfo *n = rv->next;
6622
6623 free(rv);
6624 rv = n;
6625 }
6626 return NULL;
6627 }
6628
88758e9d 6629 mu->space = NULL;
cb23f1f4 6630 mu->space_list = NULL;
88758e9d
DW
6631 mu->len = sizeof(struct imsm_update_activate_spare) * num_spares;
6632 mu->next = *updates;
6633 u = (struct imsm_update_activate_spare *) mu->buf;
6634
6635 for (di = rv ; di ; di = di->next) {
6636 u->type = update_activate_spare;
d23fe947
DW
6637 u->dl = (struct dl *) di->devs;
6638 di->devs = NULL;
88758e9d
DW
6639 u->slot = di->disk.raid_disk;
6640 u->array = inst;
6641 u->next = u + 1;
6642 u++;
6643 }
6644 (u-1)->next = NULL;
6645 *updates = mu;
6646
6647 return rv;
6648}
6649
54c2c1ea 6650static int disks_overlap(struct intel_super *super, int idx, struct imsm_update_create_array *u)
8273f55e 6651{
54c2c1ea
DW
6652 struct imsm_dev *dev = get_imsm_dev(super, idx);
6653 struct imsm_map *map = get_imsm_map(dev, 0);
6654 struct imsm_map *new_map = get_imsm_map(&u->dev, 0);
6655 struct disk_info *inf = get_disk_info(u);
6656 struct imsm_disk *disk;
8273f55e
DW
6657 int i;
6658 int j;
8273f55e 6659
54c2c1ea 6660 for (i = 0; i < map->num_members; i++) {
98130f40 6661 disk = get_imsm_disk(super, get_imsm_disk_idx(dev, i, -1));
54c2c1ea
DW
6662 for (j = 0; j < new_map->num_members; j++)
6663 if (serialcmp(disk->serial, inf[j].serial) == 0)
8273f55e
DW
6664 return 1;
6665 }
6666
6667 return 0;
6668}
6669
1a64be56
LM
6670
6671static struct dl *get_disk_super(struct intel_super *super, int major, int minor)
6672{
6673 struct dl *dl = NULL;
6674 for (dl = super->disks; dl; dl = dl->next)
6675 if ((dl->major == major) && (dl->minor == minor))
6676 return dl;
6677 return NULL;
6678}
6679
6680static int remove_disk_super(struct intel_super *super, int major, int minor)
6681{
6682 struct dl *prev = NULL;
6683 struct dl *dl;
6684
6685 prev = NULL;
6686 for (dl = super->disks; dl; dl = dl->next) {
6687 if ((dl->major == major) && (dl->minor == minor)) {
6688 /* remove */
6689 if (prev)
6690 prev->next = dl->next;
6691 else
6692 super->disks = dl->next;
6693 dl->next = NULL;
6694 __free_imsm_disk(dl);
6695 dprintf("%s: removed %x:%x\n",
6696 __func__, major, minor);
6697 break;
6698 }
6699 prev = dl;
6700 }
6701 return 0;
6702}
6703
f21e18ca 6704static void imsm_delete(struct intel_super *super, struct dl **dlp, unsigned index);
ae6aad82 6705
1a64be56
LM
6706static int add_remove_disk_update(struct intel_super *super)
6707{
6708 int check_degraded = 0;
6709 struct dl *disk = NULL;
6710 /* add/remove some spares to/from the metadata/contrainer */
6711 while (super->disk_mgmt_list) {
6712 struct dl *disk_cfg;
6713
6714 disk_cfg = super->disk_mgmt_list;
6715 super->disk_mgmt_list = disk_cfg->next;
6716 disk_cfg->next = NULL;
6717
6718 if (disk_cfg->action == DISK_ADD) {
6719 disk_cfg->next = super->disks;
6720 super->disks = disk_cfg;
6721 check_degraded = 1;
6722 dprintf("%s: added %x:%x\n",
6723 __func__, disk_cfg->major,
6724 disk_cfg->minor);
6725 } else if (disk_cfg->action == DISK_REMOVE) {
6726 dprintf("Disk remove action processed: %x.%x\n",
6727 disk_cfg->major, disk_cfg->minor);
6728 disk = get_disk_super(super,
6729 disk_cfg->major,
6730 disk_cfg->minor);
6731 if (disk) {
6732 /* store action status */
6733 disk->action = DISK_REMOVE;
6734 /* remove spare disks only */
6735 if (disk->index == -1) {
6736 remove_disk_super(super,
6737 disk_cfg->major,
6738 disk_cfg->minor);
6739 }
6740 }
6741 /* release allocate disk structure */
6742 __free_imsm_disk(disk_cfg);
6743 }
6744 }
6745 return check_degraded;
6746}
6747
a29911da
PC
6748
6749static int apply_reshape_migration_update(struct imsm_update_reshape_migration *u,
6750 struct intel_super *super,
6751 void ***space_list)
6752{
6753 struct intel_dev *id;
6754 void **tofree = NULL;
6755 int ret_val = 0;
6756
6757 dprintf("apply_reshape_migration_update()\n");
6758 if ((u->subdev < 0) ||
6759 (u->subdev > 1)) {
6760 dprintf("imsm: Error: Wrong subdev: %i\n", u->subdev);
6761 return ret_val;
6762 }
6763 if ((space_list == NULL) || (*space_list == NULL)) {
6764 dprintf("imsm: Error: Memory is not allocated\n");
6765 return ret_val;
6766 }
6767
6768 for (id = super->devlist ; id; id = id->next) {
6769 if (id->index == (unsigned)u->subdev) {
6770 struct imsm_dev *dev = get_imsm_dev(super, u->subdev);
6771 struct imsm_map *map;
6772 struct imsm_dev *new_dev =
6773 (struct imsm_dev *)*space_list;
6774 struct imsm_map *migr_map = get_imsm_map(dev, 1);
6775 int to_state;
6776 struct dl *new_disk;
6777
6778 if (new_dev == NULL)
6779 return ret_val;
6780 *space_list = **space_list;
6781 memcpy(new_dev, dev, sizeof_imsm_dev(dev, 0));
6782 map = get_imsm_map(new_dev, 0);
6783 if (migr_map) {
6784 dprintf("imsm: Error: migration in progress");
6785 return ret_val;
6786 }
6787
6788 to_state = map->map_state;
6789 if ((u->new_level == 5) && (map->raid_level == 0)) {
6790 map->num_members++;
6791 /* this should not happen */
6792 if (u->new_disks[0] < 0) {
6793 map->failed_disk_num =
6794 map->num_members - 1;
6795 to_state = IMSM_T_STATE_DEGRADED;
6796 } else
6797 to_state = IMSM_T_STATE_NORMAL;
6798 }
8e59f3d8 6799 migrate(new_dev, super, to_state, MIGR_GEN_MIGR);
a29911da
PC
6800 if (u->new_level > -1)
6801 map->raid_level = u->new_level;
6802 migr_map = get_imsm_map(new_dev, 1);
6803 if ((u->new_level == 5) &&
6804 (migr_map->raid_level == 0)) {
6805 int ord = map->num_members - 1;
6806 migr_map->num_members--;
6807 if (u->new_disks[0] < 0)
6808 ord |= IMSM_ORD_REBUILD;
6809 set_imsm_ord_tbl_ent(map,
6810 map->num_members - 1,
6811 ord);
6812 }
6813 id->dev = new_dev;
6814 tofree = (void **)dev;
6815
4bba0439
PC
6816 /* update chunk size
6817 */
6818 if (u->new_chunksize > 0)
6819 map->blocks_per_strip =
6820 __cpu_to_le16(u->new_chunksize * 2);
6821
a29911da
PC
6822 /* add disk
6823 */
6824 if ((u->new_level != 5) ||
6825 (migr_map->raid_level != 0) ||
6826 (migr_map->raid_level == map->raid_level))
6827 goto skip_disk_add;
6828
6829 if (u->new_disks[0] >= 0) {
6830 /* use passes spare
6831 */
6832 new_disk = get_disk_super(super,
6833 major(u->new_disks[0]),
6834 minor(u->new_disks[0]));
6835 dprintf("imsm: new disk for reshape is: %i:%i "
6836 "(%p, index = %i)\n",
6837 major(u->new_disks[0]),
6838 minor(u->new_disks[0]),
6839 new_disk, new_disk->index);
6840 if (new_disk == NULL)
6841 goto error_disk_add;
6842
6843 new_disk->index = map->num_members - 1;
6844 /* slot to fill in autolayout
6845 */
6846 new_disk->raiddisk = new_disk->index;
6847 new_disk->disk.status |= CONFIGURED_DISK;
6848 new_disk->disk.status &= ~SPARE_DISK;
6849 } else
6850 goto error_disk_add;
6851
6852skip_disk_add:
6853 *tofree = *space_list;
6854 /* calculate new size
6855 */
6856 imsm_set_array_size(new_dev);
6857
6858 ret_val = 1;
6859 }
6860 }
6861
6862 if (tofree)
6863 *space_list = tofree;
6864 return ret_val;
6865
6866error_disk_add:
6867 dprintf("Error: imsm: Cannot find disk.\n");
6868 return ret_val;
6869}
6870
6871
2e5dc010
N
6872static int apply_reshape_container_disks_update(struct imsm_update_reshape *u,
6873 struct intel_super *super,
6874 void ***space_list)
6875{
6876 struct dl *new_disk;
6877 struct intel_dev *id;
6878 int i;
6879 int delta_disks = u->new_raid_disks - u->old_raid_disks;
ee4beede 6880 int disk_count = u->old_raid_disks;
2e5dc010
N
6881 void **tofree = NULL;
6882 int devices_to_reshape = 1;
6883 struct imsm_super *mpb = super->anchor;
6884 int ret_val = 0;
d098291a 6885 unsigned int dev_id;
2e5dc010 6886
ed7333bd 6887 dprintf("imsm: apply_reshape_container_disks_update()\n");
2e5dc010
N
6888
6889 /* enable spares to use in array */
6890 for (i = 0; i < delta_disks; i++) {
6891 new_disk = get_disk_super(super,
6892 major(u->new_disks[i]),
6893 minor(u->new_disks[i]));
ed7333bd
AK
6894 dprintf("imsm: new disk for reshape is: %i:%i "
6895 "(%p, index = %i)\n",
2e5dc010
N
6896 major(u->new_disks[i]), minor(u->new_disks[i]),
6897 new_disk, new_disk->index);
6898 if ((new_disk == NULL) ||
6899 ((new_disk->index >= 0) &&
6900 (new_disk->index < u->old_raid_disks)))
6901 goto update_reshape_exit;
ee4beede 6902 new_disk->index = disk_count++;
2e5dc010
N
6903 /* slot to fill in autolayout
6904 */
6905 new_disk->raiddisk = new_disk->index;
6906 new_disk->disk.status |=
6907 CONFIGURED_DISK;
6908 new_disk->disk.status &= ~SPARE_DISK;
6909 }
6910
ed7333bd
AK
6911 dprintf("imsm: volume set mpb->num_raid_devs = %i\n",
6912 mpb->num_raid_devs);
2e5dc010
N
6913 /* manage changes in volume
6914 */
d098291a 6915 for (dev_id = 0; dev_id < mpb->num_raid_devs; dev_id++) {
2e5dc010
N
6916 void **sp = *space_list;
6917 struct imsm_dev *newdev;
6918 struct imsm_map *newmap, *oldmap;
6919
d098291a
AK
6920 for (id = super->devlist ; id; id = id->next) {
6921 if (id->index == dev_id)
6922 break;
6923 }
6924 if (id == NULL)
6925 break;
2e5dc010
N
6926 if (!sp)
6927 continue;
6928 *space_list = *sp;
6929 newdev = (void*)sp;
6930 /* Copy the dev, but not (all of) the map */
6931 memcpy(newdev, id->dev, sizeof(*newdev));
6932 oldmap = get_imsm_map(id->dev, 0);
6933 newmap = get_imsm_map(newdev, 0);
6934 /* Copy the current map */
6935 memcpy(newmap, oldmap, sizeof_imsm_map(oldmap));
6936 /* update one device only
6937 */
6938 if (devices_to_reshape) {
ed7333bd
AK
6939 dprintf("imsm: modifying subdev: %i\n",
6940 id->index);
2e5dc010
N
6941 devices_to_reshape--;
6942 newdev->vol.migr_state = 1;
6943 newdev->vol.curr_migr_unit = 0;
6944 newdev->vol.migr_type = MIGR_GEN_MIGR;
6945 newmap->num_members = u->new_raid_disks;
6946 for (i = 0; i < delta_disks; i++) {
6947 set_imsm_ord_tbl_ent(newmap,
6948 u->old_raid_disks + i,
6949 u->old_raid_disks + i);
6950 }
6951 /* New map is correct, now need to save old map
6952 */
6953 newmap = get_imsm_map(newdev, 1);
6954 memcpy(newmap, oldmap, sizeof_imsm_map(oldmap));
6955
70bdf0dc 6956 imsm_set_array_size(newdev);
2e5dc010
N
6957 }
6958
6959 sp = (void **)id->dev;
6960 id->dev = newdev;
6961 *sp = tofree;
6962 tofree = sp;
8e59f3d8
AK
6963
6964 /* Clear migration record */
6965 memset(super->migr_rec, 0, sizeof(struct migr_record));
2e5dc010 6966 }
819bc634
AK
6967 if (tofree)
6968 *space_list = tofree;
2e5dc010
N
6969 ret_val = 1;
6970
6971update_reshape_exit:
6972
6973 return ret_val;
6974}
6975
bb025c2f 6976static int apply_takeover_update(struct imsm_update_takeover *u,
8ca6df95
KW
6977 struct intel_super *super,
6978 void ***space_list)
bb025c2f
KW
6979{
6980 struct imsm_dev *dev = NULL;
8ca6df95
KW
6981 struct intel_dev *dv;
6982 struct imsm_dev *dev_new;
bb025c2f
KW
6983 struct imsm_map *map;
6984 struct dl *dm, *du;
8ca6df95 6985 int i;
bb025c2f
KW
6986
6987 for (dv = super->devlist; dv; dv = dv->next)
6988 if (dv->index == (unsigned int)u->subarray) {
6989 dev = dv->dev;
6990 break;
6991 }
6992
6993 if (dev == NULL)
6994 return 0;
6995
6996 map = get_imsm_map(dev, 0);
6997
6998 if (u->direction == R10_TO_R0) {
43d5ec18
KW
6999 /* Number of failed disks must be half of initial disk number */
7000 if (imsm_count_failed(super, dev) != (map->num_members / 2))
7001 return 0;
7002
bb025c2f
KW
7003 /* iterate through devices to mark removed disks as spare */
7004 for (dm = super->disks; dm; dm = dm->next) {
7005 if (dm->disk.status & FAILED_DISK) {
7006 int idx = dm->index;
7007 /* update indexes on the disk list */
7008/* FIXME this loop-with-the-loop looks wrong, I'm not convinced
7009 the index values will end up being correct.... NB */
7010 for (du = super->disks; du; du = du->next)
7011 if (du->index > idx)
7012 du->index--;
7013 /* mark as spare disk */
7014 dm->disk.status = SPARE_DISK;
7015 dm->index = -1;
7016 }
7017 }
bb025c2f
KW
7018 /* update map */
7019 map->num_members = map->num_members / 2;
7020 map->map_state = IMSM_T_STATE_NORMAL;
7021 map->num_domains = 1;
7022 map->raid_level = 0;
7023 map->failed_disk_num = -1;
7024 }
7025
8ca6df95
KW
7026 if (u->direction == R0_TO_R10) {
7027 void **space;
7028 /* update slots in current disk list */
7029 for (dm = super->disks; dm; dm = dm->next) {
7030 if (dm->index >= 0)
7031 dm->index *= 2;
7032 }
7033 /* create new *missing* disks */
7034 for (i = 0; i < map->num_members; i++) {
7035 space = *space_list;
7036 if (!space)
7037 continue;
7038 *space_list = *space;
7039 du = (void *)space;
7040 memcpy(du, super->disks, sizeof(*du));
8ca6df95
KW
7041 du->fd = -1;
7042 du->minor = 0;
7043 du->major = 0;
7044 du->index = (i * 2) + 1;
7045 sprintf((char *)du->disk.serial,
7046 " MISSING_%d", du->index);
7047 sprintf((char *)du->serial,
7048 "MISSING_%d", du->index);
7049 du->next = super->missing;
7050 super->missing = du;
7051 }
7052 /* create new dev and map */
7053 space = *space_list;
7054 if (!space)
7055 return 0;
7056 *space_list = *space;
7057 dev_new = (void *)space;
7058 memcpy(dev_new, dev, sizeof(*dev));
7059 /* update new map */
7060 map = get_imsm_map(dev_new, 0);
8ca6df95 7061 map->num_members = map->num_members * 2;
1a2487c2 7062 map->map_state = IMSM_T_STATE_DEGRADED;
8ca6df95
KW
7063 map->num_domains = 2;
7064 map->raid_level = 1;
7065 /* replace dev<->dev_new */
7066 dv->dev = dev_new;
7067 }
bb025c2f
KW
7068 /* update disk order table */
7069 for (du = super->disks; du; du = du->next)
7070 if (du->index >= 0)
7071 set_imsm_ord_tbl_ent(map, du->index, du->index);
8ca6df95 7072 for (du = super->missing; du; du = du->next)
1a2487c2
KW
7073 if (du->index >= 0) {
7074 set_imsm_ord_tbl_ent(map, du->index, du->index);
e4c72d1d 7075 mark_missing(dv->dev, &du->disk, du->index);
1a2487c2 7076 }
bb025c2f
KW
7077
7078 return 1;
7079}
7080
e8319a19
DW
7081static void imsm_process_update(struct supertype *st,
7082 struct metadata_update *update)
7083{
7084 /**
7085 * crack open the metadata_update envelope to find the update record
7086 * update can be one of:
d195167d
AK
7087 * update_reshape_container_disks - all the arrays in the container
7088 * are being reshaped to have more devices. We need to mark
7089 * the arrays for general migration and convert selected spares
7090 * into active devices.
7091 * update_activate_spare - a spare device has replaced a failed
e8319a19
DW
7092 * device in an array, update the disk_ord_tbl. If this disk is
7093 * present in all member arrays then also clear the SPARE_DISK
7094 * flag
d195167d
AK
7095 * update_create_array
7096 * update_kill_array
7097 * update_rename_array
7098 * update_add_remove_disk
e8319a19
DW
7099 */
7100 struct intel_super *super = st->sb;
4d7b1503 7101 struct imsm_super *mpb;
e8319a19
DW
7102 enum imsm_update_type type = *(enum imsm_update_type *) update->buf;
7103
4d7b1503
DW
7104 /* update requires a larger buf but the allocation failed */
7105 if (super->next_len && !super->next_buf) {
7106 super->next_len = 0;
7107 return;
7108 }
7109
7110 if (super->next_buf) {
7111 memcpy(super->next_buf, super->buf, super->len);
7112 free(super->buf);
7113 super->len = super->next_len;
7114 super->buf = super->next_buf;
7115
7116 super->next_len = 0;
7117 super->next_buf = NULL;
7118 }
7119
7120 mpb = super->anchor;
7121
e8319a19 7122 switch (type) {
0ec5d470
AK
7123 case update_general_migration_checkpoint: {
7124 struct intel_dev *id;
7125 struct imsm_update_general_migration_checkpoint *u =
7126 (void *)update->buf;
7127
7128 dprintf("imsm: process_update() "
7129 "for update_general_migration_checkpoint called\n");
7130
7131 /* find device under general migration */
7132 for (id = super->devlist ; id; id = id->next) {
7133 if (is_gen_migration(id->dev)) {
7134 id->dev->vol.curr_migr_unit =
7135 __cpu_to_le32(u->curr_migr_unit);
7136 super->updates_pending++;
7137 }
7138 }
7139 break;
7140 }
bb025c2f
KW
7141 case update_takeover: {
7142 struct imsm_update_takeover *u = (void *)update->buf;
1a2487c2
KW
7143 if (apply_takeover_update(u, super, &update->space_list)) {
7144 imsm_update_version_info(super);
bb025c2f 7145 super->updates_pending++;
1a2487c2 7146 }
bb025c2f
KW
7147 break;
7148 }
7149
78b10e66 7150 case update_reshape_container_disks: {
d195167d 7151 struct imsm_update_reshape *u = (void *)update->buf;
2e5dc010
N
7152 if (apply_reshape_container_disks_update(
7153 u, super, &update->space_list))
7154 super->updates_pending++;
78b10e66
N
7155 break;
7156 }
48c5303a 7157 case update_reshape_migration: {
a29911da
PC
7158 struct imsm_update_reshape_migration *u = (void *)update->buf;
7159 if (apply_reshape_migration_update(
7160 u, super, &update->space_list))
7161 super->updates_pending++;
48c5303a
PC
7162 break;
7163 }
e8319a19
DW
7164 case update_activate_spare: {
7165 struct imsm_update_activate_spare *u = (void *) update->buf;
949c47a0 7166 struct imsm_dev *dev = get_imsm_dev(super, u->array);
a965f303 7167 struct imsm_map *map = get_imsm_map(dev, 0);
0c046afd 7168 struct imsm_map *migr_map;
e8319a19
DW
7169 struct active_array *a;
7170 struct imsm_disk *disk;
0c046afd 7171 __u8 to_state;
e8319a19 7172 struct dl *dl;
e8319a19 7173 unsigned int found;
0c046afd 7174 int failed;
98130f40 7175 int victim = get_imsm_disk_idx(dev, u->slot, -1);
e8319a19
DW
7176 int i;
7177
7178 for (dl = super->disks; dl; dl = dl->next)
d23fe947 7179 if (dl == u->dl)
e8319a19
DW
7180 break;
7181
7182 if (!dl) {
7183 fprintf(stderr, "error: imsm_activate_spare passed "
1f24f035
DW
7184 "an unknown disk (index: %d)\n",
7185 u->dl->index);
e8319a19
DW
7186 return;
7187 }
7188
7189 super->updates_pending++;
0c046afd
DW
7190 /* count failures (excluding rebuilds and the victim)
7191 * to determine map[0] state
7192 */
7193 failed = 0;
7194 for (i = 0; i < map->num_members; i++) {
7195 if (i == u->slot)
7196 continue;
98130f40
AK
7197 disk = get_imsm_disk(super,
7198 get_imsm_disk_idx(dev, i, -1));
25ed7e59 7199 if (!disk || is_failed(disk))
0c046afd
DW
7200 failed++;
7201 }
7202
d23fe947
DW
7203 /* adding a pristine spare, assign a new index */
7204 if (dl->index < 0) {
7205 dl->index = super->anchor->num_disks;
7206 super->anchor->num_disks++;
7207 }
d23fe947 7208 disk = &dl->disk;
f2f27e63
DW
7209 disk->status |= CONFIGURED_DISK;
7210 disk->status &= ~SPARE_DISK;
e8319a19 7211
0c046afd
DW
7212 /* mark rebuild */
7213 to_state = imsm_check_degraded(super, dev, failed);
7214 map->map_state = IMSM_T_STATE_DEGRADED;
8e59f3d8 7215 migrate(dev, super, to_state, MIGR_REBUILD);
0c046afd
DW
7216 migr_map = get_imsm_map(dev, 1);
7217 set_imsm_ord_tbl_ent(map, u->slot, dl->index);
7218 set_imsm_ord_tbl_ent(migr_map, u->slot, dl->index | IMSM_ORD_REBUILD);
7219
148acb7b
DW
7220 /* update the family_num to mark a new container
7221 * generation, being careful to record the existing
7222 * family_num in orig_family_num to clean up after
7223 * earlier mdadm versions that neglected to set it.
7224 */
7225 if (mpb->orig_family_num == 0)
7226 mpb->orig_family_num = mpb->family_num;
7227 mpb->family_num += super->random;
7228
e8319a19
DW
7229 /* count arrays using the victim in the metadata */
7230 found = 0;
7231 for (a = st->arrays; a ; a = a->next) {
949c47a0 7232 dev = get_imsm_dev(super, a->info.container_member);
620b1713
DW
7233 map = get_imsm_map(dev, 0);
7234
7235 if (get_imsm_disk_slot(map, victim) >= 0)
7236 found++;
e8319a19
DW
7237 }
7238
24565c9a 7239 /* delete the victim if it is no longer being
e8319a19
DW
7240 * utilized anywhere
7241 */
e8319a19 7242 if (!found) {
ae6aad82 7243 struct dl **dlp;
24565c9a 7244
47ee5a45
DW
7245 /* We know that 'manager' isn't touching anything,
7246 * so it is safe to delete
7247 */
24565c9a 7248 for (dlp = &super->disks; *dlp; dlp = &(*dlp)->next)
ae6aad82
DW
7249 if ((*dlp)->index == victim)
7250 break;
47ee5a45
DW
7251
7252 /* victim may be on the missing list */
7253 if (!*dlp)
7254 for (dlp = &super->missing; *dlp; dlp = &(*dlp)->next)
7255 if ((*dlp)->index == victim)
7256 break;
24565c9a 7257 imsm_delete(super, dlp, victim);
e8319a19 7258 }
8273f55e
DW
7259 break;
7260 }
7261 case update_create_array: {
7262 /* someone wants to create a new array, we need to be aware of
7263 * a few races/collisions:
7264 * 1/ 'Create' called by two separate instances of mdadm
7265 * 2/ 'Create' versus 'activate_spare': mdadm has chosen
7266 * devices that have since been assimilated via
7267 * activate_spare.
7268 * In the event this update can not be carried out mdadm will
7269 * (FIX ME) notice that its update did not take hold.
7270 */
7271 struct imsm_update_create_array *u = (void *) update->buf;
ba2de7ba 7272 struct intel_dev *dv;
8273f55e
DW
7273 struct imsm_dev *dev;
7274 struct imsm_map *map, *new_map;
7275 unsigned long long start, end;
7276 unsigned long long new_start, new_end;
7277 int i;
54c2c1ea
DW
7278 struct disk_info *inf;
7279 struct dl *dl;
8273f55e
DW
7280
7281 /* handle racing creates: first come first serve */
7282 if (u->dev_idx < mpb->num_raid_devs) {
7283 dprintf("%s: subarray %d already defined\n",
7284 __func__, u->dev_idx);
ba2de7ba 7285 goto create_error;
8273f55e
DW
7286 }
7287
7288 /* check update is next in sequence */
7289 if (u->dev_idx != mpb->num_raid_devs) {
6a3e913e
DW
7290 dprintf("%s: can not create array %d expected index %d\n",
7291 __func__, u->dev_idx, mpb->num_raid_devs);
ba2de7ba 7292 goto create_error;
8273f55e
DW
7293 }
7294
a965f303 7295 new_map = get_imsm_map(&u->dev, 0);
8273f55e
DW
7296 new_start = __le32_to_cpu(new_map->pba_of_lba0);
7297 new_end = new_start + __le32_to_cpu(new_map->blocks_per_member);
54c2c1ea 7298 inf = get_disk_info(u);
8273f55e
DW
7299
7300 /* handle activate_spare versus create race:
7301 * check to make sure that overlapping arrays do not include
7302 * overalpping disks
7303 */
7304 for (i = 0; i < mpb->num_raid_devs; i++) {
949c47a0 7305 dev = get_imsm_dev(super, i);
a965f303 7306 map = get_imsm_map(dev, 0);
8273f55e
DW
7307 start = __le32_to_cpu(map->pba_of_lba0);
7308 end = start + __le32_to_cpu(map->blocks_per_member);
7309 if ((new_start >= start && new_start <= end) ||
7310 (start >= new_start && start <= new_end))
54c2c1ea
DW
7311 /* overlap */;
7312 else
7313 continue;
7314
7315 if (disks_overlap(super, i, u)) {
8273f55e 7316 dprintf("%s: arrays overlap\n", __func__);
ba2de7ba 7317 goto create_error;
8273f55e
DW
7318 }
7319 }
8273f55e 7320
949c47a0
DW
7321 /* check that prepare update was successful */
7322 if (!update->space) {
7323 dprintf("%s: prepare update failed\n", __func__);
ba2de7ba 7324 goto create_error;
949c47a0
DW
7325 }
7326
54c2c1ea
DW
7327 /* check that all disks are still active before committing
7328 * changes. FIXME: could we instead handle this by creating a
7329 * degraded array? That's probably not what the user expects,
7330 * so better to drop this update on the floor.
7331 */
7332 for (i = 0; i < new_map->num_members; i++) {
7333 dl = serial_to_dl(inf[i].serial, super);
7334 if (!dl) {
7335 dprintf("%s: disk disappeared\n", __func__);
ba2de7ba 7336 goto create_error;
54c2c1ea 7337 }
949c47a0
DW
7338 }
7339
8273f55e 7340 super->updates_pending++;
54c2c1ea
DW
7341
7342 /* convert spares to members and fixup ord_tbl */
7343 for (i = 0; i < new_map->num_members; i++) {
7344 dl = serial_to_dl(inf[i].serial, super);
7345 if (dl->index == -1) {
7346 dl->index = mpb->num_disks;
7347 mpb->num_disks++;
7348 dl->disk.status |= CONFIGURED_DISK;
7349 dl->disk.status &= ~SPARE_DISK;
7350 }
7351 set_imsm_ord_tbl_ent(new_map, i, dl->index);
7352 }
7353
ba2de7ba
DW
7354 dv = update->space;
7355 dev = dv->dev;
949c47a0
DW
7356 update->space = NULL;
7357 imsm_copy_dev(dev, &u->dev);
ba2de7ba
DW
7358 dv->index = u->dev_idx;
7359 dv->next = super->devlist;
7360 super->devlist = dv;
8273f55e 7361 mpb->num_raid_devs++;
8273f55e 7362
4d1313e9 7363 imsm_update_version_info(super);
8273f55e 7364 break;
ba2de7ba
DW
7365 create_error:
7366 /* mdmon knows how to release update->space, but not
7367 * ((struct intel_dev *) update->space)->dev
7368 */
7369 if (update->space) {
7370 dv = update->space;
7371 free(dv->dev);
7372 }
8273f55e 7373 break;
e8319a19 7374 }
33414a01
DW
7375 case update_kill_array: {
7376 struct imsm_update_kill_array *u = (void *) update->buf;
7377 int victim = u->dev_idx;
7378 struct active_array *a;
7379 struct intel_dev **dp;
7380 struct imsm_dev *dev;
7381
7382 /* sanity check that we are not affecting the uuid of
7383 * active arrays, or deleting an active array
7384 *
7385 * FIXME when immutable ids are available, but note that
7386 * we'll also need to fixup the invalidated/active
7387 * subarray indexes in mdstat
7388 */
7389 for (a = st->arrays; a; a = a->next)
7390 if (a->info.container_member >= victim)
7391 break;
7392 /* by definition if mdmon is running at least one array
7393 * is active in the container, so checking
7394 * mpb->num_raid_devs is just extra paranoia
7395 */
7396 dev = get_imsm_dev(super, victim);
7397 if (a || !dev || mpb->num_raid_devs == 1) {
7398 dprintf("failed to delete subarray-%d\n", victim);
7399 break;
7400 }
7401
7402 for (dp = &super->devlist; *dp;)
f21e18ca 7403 if ((*dp)->index == (unsigned)super->current_vol) {
33414a01
DW
7404 *dp = (*dp)->next;
7405 } else {
f21e18ca 7406 if ((*dp)->index > (unsigned)victim)
33414a01
DW
7407 (*dp)->index--;
7408 dp = &(*dp)->next;
7409 }
7410 mpb->num_raid_devs--;
7411 super->updates_pending++;
7412 break;
7413 }
aa534678
DW
7414 case update_rename_array: {
7415 struct imsm_update_rename_array *u = (void *) update->buf;
7416 char name[MAX_RAID_SERIAL_LEN+1];
7417 int target = u->dev_idx;
7418 struct active_array *a;
7419 struct imsm_dev *dev;
7420
7421 /* sanity check that we are not affecting the uuid of
7422 * an active array
7423 */
7424 snprintf(name, MAX_RAID_SERIAL_LEN, "%s", (char *) u->name);
7425 name[MAX_RAID_SERIAL_LEN] = '\0';
7426 for (a = st->arrays; a; a = a->next)
7427 if (a->info.container_member == target)
7428 break;
7429 dev = get_imsm_dev(super, u->dev_idx);
7430 if (a || !dev || !check_name(super, name, 1)) {
7431 dprintf("failed to rename subarray-%d\n", target);
7432 break;
7433 }
7434
cdbe98cd 7435 snprintf((char *) dev->volume, MAX_RAID_SERIAL_LEN, "%s", name);
aa534678
DW
7436 super->updates_pending++;
7437 break;
7438 }
1a64be56 7439 case update_add_remove_disk: {
43dad3d6 7440 /* we may be able to repair some arrays if disks are
1a64be56
LM
7441 * being added, check teh status of add_remove_disk
7442 * if discs has been added.
7443 */
7444 if (add_remove_disk_update(super)) {
43dad3d6 7445 struct active_array *a;
072b727f
DW
7446
7447 super->updates_pending++;
1a64be56 7448 for (a = st->arrays; a; a = a->next)
43dad3d6
DW
7449 a->check_degraded = 1;
7450 }
43dad3d6 7451 break;
e8319a19 7452 }
1a64be56
LM
7453 default:
7454 fprintf(stderr, "error: unsuported process update type:"
7455 "(type: %d)\n", type);
7456 }
e8319a19 7457}
88758e9d 7458
bc0b9d34
PC
7459static struct mdinfo *get_spares_for_grow(struct supertype *st);
7460
8273f55e
DW
7461static void imsm_prepare_update(struct supertype *st,
7462 struct metadata_update *update)
7463{
949c47a0 7464 /**
4d7b1503
DW
7465 * Allocate space to hold new disk entries, raid-device entries or a new
7466 * mpb if necessary. The manager synchronously waits for updates to
7467 * complete in the monitor, so new mpb buffers allocated here can be
7468 * integrated by the monitor thread without worrying about live pointers
7469 * in the manager thread.
8273f55e 7470 */
949c47a0 7471 enum imsm_update_type type = *(enum imsm_update_type *) update->buf;
4d7b1503
DW
7472 struct intel_super *super = st->sb;
7473 struct imsm_super *mpb = super->anchor;
7474 size_t buf_len;
7475 size_t len = 0;
949c47a0
DW
7476
7477 switch (type) {
0ec5d470
AK
7478 case update_general_migration_checkpoint:
7479 dprintf("imsm: prepare_update() "
7480 "for update_general_migration_checkpoint called\n");
7481 break;
abedf5fc
KW
7482 case update_takeover: {
7483 struct imsm_update_takeover *u = (void *)update->buf;
7484 if (u->direction == R0_TO_R10) {
7485 void **tail = (void **)&update->space_list;
7486 struct imsm_dev *dev = get_imsm_dev(super, u->subarray);
7487 struct imsm_map *map = get_imsm_map(dev, 0);
7488 int num_members = map->num_members;
7489 void *space;
7490 int size, i;
7491 int err = 0;
7492 /* allocate memory for added disks */
7493 for (i = 0; i < num_members; i++) {
7494 size = sizeof(struct dl);
7495 space = malloc(size);
7496 if (!space) {
7497 err++;
7498 break;
7499 }
7500 *tail = space;
7501 tail = space;
7502 *tail = NULL;
7503 }
7504 /* allocate memory for new device */
7505 size = sizeof_imsm_dev(super->devlist->dev, 0) +
7506 (num_members * sizeof(__u32));
7507 space = malloc(size);
7508 if (!space)
7509 err++;
7510 else {
7511 *tail = space;
7512 tail = space;
7513 *tail = NULL;
7514 }
7515 if (!err) {
7516 len = disks_to_mpb_size(num_members * 2);
7517 } else {
7518 /* if allocation didn't success, free buffer */
7519 while (update->space_list) {
7520 void **sp = update->space_list;
7521 update->space_list = *sp;
7522 free(sp);
7523 }
7524 }
7525 }
7526
7527 break;
7528 }
78b10e66 7529 case update_reshape_container_disks: {
d195167d
AK
7530 /* Every raid device in the container is about to
7531 * gain some more devices, and we will enter a
7532 * reconfiguration.
7533 * So each 'imsm_map' will be bigger, and the imsm_vol
7534 * will now hold 2 of them.
7535 * Thus we need new 'struct imsm_dev' allocations sized
7536 * as sizeof_imsm_dev but with more devices in both maps.
7537 */
7538 struct imsm_update_reshape *u = (void *)update->buf;
7539 struct intel_dev *dl;
7540 void **space_tail = (void**)&update->space_list;
7541
7542 dprintf("imsm: imsm_prepare_update() for update_reshape\n");
7543
7544 for (dl = super->devlist; dl; dl = dl->next) {
7545 int size = sizeof_imsm_dev(dl->dev, 1);
7546 void *s;
d677e0b8
AK
7547 if (u->new_raid_disks > u->old_raid_disks)
7548 size += sizeof(__u32)*2*
7549 (u->new_raid_disks - u->old_raid_disks);
d195167d
AK
7550 s = malloc(size);
7551 if (!s)
7552 break;
7553 *space_tail = s;
7554 space_tail = s;
7555 *space_tail = NULL;
7556 }
7557
7558 len = disks_to_mpb_size(u->new_raid_disks);
7559 dprintf("New anchor length is %llu\n", (unsigned long long)len);
78b10e66
N
7560 break;
7561 }
48c5303a 7562 case update_reshape_migration: {
bc0b9d34
PC
7563 /* for migration level 0->5 we need to add disks
7564 * so the same as for container operation we will copy
7565 * device to the bigger location.
7566 * in memory prepared device and new disk area are prepared
7567 * for usage in process update
7568 */
7569 struct imsm_update_reshape_migration *u = (void *)update->buf;
7570 struct intel_dev *id;
7571 void **space_tail = (void **)&update->space_list;
7572 int size;
7573 void *s;
7574 int current_level = -1;
7575
7576 dprintf("imsm: imsm_prepare_update() for update_reshape\n");
7577
7578 /* add space for bigger array in update
7579 */
7580 for (id = super->devlist; id; id = id->next) {
7581 if (id->index == (unsigned)u->subdev) {
7582 size = sizeof_imsm_dev(id->dev, 1);
7583 if (u->new_raid_disks > u->old_raid_disks)
7584 size += sizeof(__u32)*2*
7585 (u->new_raid_disks - u->old_raid_disks);
7586 s = malloc(size);
7587 if (!s)
7588 break;
7589 *space_tail = s;
7590 space_tail = s;
7591 *space_tail = NULL;
7592 break;
7593 }
7594 }
7595 if (update->space_list == NULL)
7596 break;
7597
7598 /* add space for disk in update
7599 */
7600 size = sizeof(struct dl);
7601 s = malloc(size);
7602 if (!s) {
7603 free(update->space_list);
7604 update->space_list = NULL;
7605 break;
7606 }
7607 *space_tail = s;
7608 space_tail = s;
7609 *space_tail = NULL;
7610
7611 /* add spare device to update
7612 */
7613 for (id = super->devlist ; id; id = id->next)
7614 if (id->index == (unsigned)u->subdev) {
7615 struct imsm_dev *dev;
7616 struct imsm_map *map;
7617
7618 dev = get_imsm_dev(super, u->subdev);
7619 map = get_imsm_map(dev, 0);
7620 current_level = map->raid_level;
7621 break;
7622 }
7623 if ((u->new_level == 5) && (u->new_level != current_level)) {
7624 struct mdinfo *spares;
7625
7626 spares = get_spares_for_grow(st);
7627 if (spares) {
7628 struct dl *dl;
7629 struct mdinfo *dev;
7630
7631 dev = spares->devs;
7632 if (dev) {
7633 u->new_disks[0] =
7634 makedev(dev->disk.major,
7635 dev->disk.minor);
7636 dl = get_disk_super(super,
7637 dev->disk.major,
7638 dev->disk.minor);
7639 dl->index = u->old_raid_disks;
7640 dev = dev->next;
7641 }
7642 sysfs_free(spares);
7643 }
7644 }
7645 len = disks_to_mpb_size(u->new_raid_disks);
7646 dprintf("New anchor length is %llu\n", (unsigned long long)len);
48c5303a
PC
7647 break;
7648 }
949c47a0
DW
7649 case update_create_array: {
7650 struct imsm_update_create_array *u = (void *) update->buf;
ba2de7ba 7651 struct intel_dev *dv;
54c2c1ea
DW
7652 struct imsm_dev *dev = &u->dev;
7653 struct imsm_map *map = get_imsm_map(dev, 0);
7654 struct dl *dl;
7655 struct disk_info *inf;
7656 int i;
7657 int activate = 0;
949c47a0 7658
54c2c1ea
DW
7659 inf = get_disk_info(u);
7660 len = sizeof_imsm_dev(dev, 1);
ba2de7ba
DW
7661 /* allocate a new super->devlist entry */
7662 dv = malloc(sizeof(*dv));
7663 if (dv) {
7664 dv->dev = malloc(len);
7665 if (dv->dev)
7666 update->space = dv;
7667 else {
7668 free(dv);
7669 update->space = NULL;
7670 }
7671 }
949c47a0 7672
54c2c1ea
DW
7673 /* count how many spares will be converted to members */
7674 for (i = 0; i < map->num_members; i++) {
7675 dl = serial_to_dl(inf[i].serial, super);
7676 if (!dl) {
7677 /* hmm maybe it failed?, nothing we can do about
7678 * it here
7679 */
7680 continue;
7681 }
7682 if (count_memberships(dl, super) == 0)
7683 activate++;
7684 }
7685 len += activate * sizeof(struct imsm_disk);
949c47a0
DW
7686 break;
7687 default:
7688 break;
7689 }
7690 }
8273f55e 7691
4d7b1503
DW
7692 /* check if we need a larger metadata buffer */
7693 if (super->next_buf)
7694 buf_len = super->next_len;
7695 else
7696 buf_len = super->len;
7697
7698 if (__le32_to_cpu(mpb->mpb_size) + len > buf_len) {
7699 /* ok we need a larger buf than what is currently allocated
7700 * if this allocation fails process_update will notice that
7701 * ->next_len is set and ->next_buf is NULL
7702 */
7703 buf_len = ROUND_UP(__le32_to_cpu(mpb->mpb_size) + len, 512);
7704 if (super->next_buf)
7705 free(super->next_buf);
7706
7707 super->next_len = buf_len;
1f45a8ad
DW
7708 if (posix_memalign(&super->next_buf, 512, buf_len) == 0)
7709 memset(super->next_buf, 0, buf_len);
7710 else
4d7b1503
DW
7711 super->next_buf = NULL;
7712 }
8273f55e
DW
7713}
7714
ae6aad82 7715/* must be called while manager is quiesced */
f21e18ca 7716static void imsm_delete(struct intel_super *super, struct dl **dlp, unsigned index)
ae6aad82
DW
7717{
7718 struct imsm_super *mpb = super->anchor;
ae6aad82
DW
7719 struct dl *iter;
7720 struct imsm_dev *dev;
7721 struct imsm_map *map;
24565c9a
DW
7722 int i, j, num_members;
7723 __u32 ord;
ae6aad82 7724
24565c9a
DW
7725 dprintf("%s: deleting device[%d] from imsm_super\n",
7726 __func__, index);
ae6aad82
DW
7727
7728 /* shift all indexes down one */
7729 for (iter = super->disks; iter; iter = iter->next)
f21e18ca 7730 if (iter->index > (int)index)
ae6aad82 7731 iter->index--;
47ee5a45 7732 for (iter = super->missing; iter; iter = iter->next)
f21e18ca 7733 if (iter->index > (int)index)
47ee5a45 7734 iter->index--;
ae6aad82
DW
7735
7736 for (i = 0; i < mpb->num_raid_devs; i++) {
7737 dev = get_imsm_dev(super, i);
7738 map = get_imsm_map(dev, 0);
24565c9a
DW
7739 num_members = map->num_members;
7740 for (j = 0; j < num_members; j++) {
7741 /* update ord entries being careful not to propagate
7742 * ord-flags to the first map
7743 */
98130f40 7744 ord = get_imsm_ord_tbl_ent(dev, j, -1);
ae6aad82 7745
24565c9a
DW
7746 if (ord_to_idx(ord) <= index)
7747 continue;
ae6aad82 7748
24565c9a
DW
7749 map = get_imsm_map(dev, 0);
7750 set_imsm_ord_tbl_ent(map, j, ord_to_idx(ord - 1));
7751 map = get_imsm_map(dev, 1);
7752 if (map)
7753 set_imsm_ord_tbl_ent(map, j, ord - 1);
ae6aad82
DW
7754 }
7755 }
7756
7757 mpb->num_disks--;
7758 super->updates_pending++;
24565c9a
DW
7759 if (*dlp) {
7760 struct dl *dl = *dlp;
7761
7762 *dlp = (*dlp)->next;
7763 __free_imsm_disk(dl);
7764 }
ae6aad82 7765}
9e2d750d 7766#endif /* MDASSEMBLE */
687629c2
AK
7767/*******************************************************************************
7768 * Function: open_backup_targets
7769 * Description: Function opens file descriptors for all devices given in
7770 * info->devs
7771 * Parameters:
7772 * info : general array info
7773 * raid_disks : number of disks
7774 * raid_fds : table of device's file descriptors
7775 * Returns:
7776 * 0 : success
7777 * -1 : fail
7778 ******************************************************************************/
7779int open_backup_targets(struct mdinfo *info, int raid_disks, int *raid_fds)
7780{
7781 struct mdinfo *sd;
7782
7783 for (sd = info->devs ; sd ; sd = sd->next) {
7784 char *dn;
7785
7786 if (sd->disk.state & (1<<MD_DISK_FAULTY)) {
7787 dprintf("disk is faulty!!\n");
7788 continue;
7789 }
7790
7791 if ((sd->disk.raid_disk >= raid_disks) ||
7792 (sd->disk.raid_disk < 0))
7793 continue;
7794
7795 dn = map_dev(sd->disk.major,
7796 sd->disk.minor, 1);
7797 raid_fds[sd->disk.raid_disk] = dev_open(dn, O_RDWR);
7798 if (raid_fds[sd->disk.raid_disk] < 0) {
7799 fprintf(stderr, "cannot open component\n");
7800 return -1;
7801 }
7802 }
7803 return 0;
7804}
7805
9e2d750d 7806#ifndef MDASSEMBLE
687629c2
AK
7807/*******************************************************************************
7808 * Function: init_migr_record_imsm
7809 * Description: Function inits imsm migration record
7810 * Parameters:
7811 * super : imsm internal array info
7812 * dev : device under migration
7813 * info : general array info to find the smallest device
7814 * Returns:
7815 * none
7816 ******************************************************************************/
7817void init_migr_record_imsm(struct supertype *st, struct imsm_dev *dev,
7818 struct mdinfo *info)
7819{
7820 struct intel_super *super = st->sb;
7821 struct migr_record *migr_rec = super->migr_rec;
7822 int new_data_disks;
7823 unsigned long long dsize, dev_sectors;
7824 long long unsigned min_dev_sectors = -1LLU;
7825 struct mdinfo *sd;
7826 char nm[30];
7827 int fd;
7828 struct imsm_map *map_dest = get_imsm_map(dev, 0);
7829 struct imsm_map *map_src = get_imsm_map(dev, 1);
7830 unsigned long long num_migr_units;
3ef4403c 7831 unsigned long long array_blocks;
687629c2
AK
7832
7833 memset(migr_rec, 0, sizeof(struct migr_record));
7834 migr_rec->family_num = __cpu_to_le32(super->anchor->family_num);
7835
7836 /* only ascending reshape supported now */
7837 migr_rec->ascending_migr = __cpu_to_le32(1);
7838
7839 migr_rec->dest_depth_per_unit = GEN_MIGR_AREA_SIZE /
7840 max(map_dest->blocks_per_strip, map_src->blocks_per_strip);
7841 migr_rec->dest_depth_per_unit *= map_dest->blocks_per_strip;
7842 new_data_disks = imsm_num_data_members(dev, 0);
7843 migr_rec->blocks_per_unit =
7844 __cpu_to_le32(migr_rec->dest_depth_per_unit * new_data_disks);
7845 migr_rec->dest_depth_per_unit =
7846 __cpu_to_le32(migr_rec->dest_depth_per_unit);
3ef4403c 7847 array_blocks = info->component_size * new_data_disks;
687629c2
AK
7848 num_migr_units =
7849 array_blocks / __le32_to_cpu(migr_rec->blocks_per_unit);
7850
7851 if (array_blocks % __le32_to_cpu(migr_rec->blocks_per_unit))
7852 num_migr_units++;
7853 migr_rec->num_migr_units = __cpu_to_le32(num_migr_units);
7854
7855 migr_rec->post_migr_vol_cap = dev->size_low;
7856 migr_rec->post_migr_vol_cap_hi = dev->size_high;
7857
7858
7859 /* Find the smallest dev */
7860 for (sd = info->devs ; sd ; sd = sd->next) {
7861 sprintf(nm, "%d:%d", sd->disk.major, sd->disk.minor);
7862 fd = dev_open(nm, O_RDONLY);
7863 if (fd < 0)
7864 continue;
7865 get_dev_size(fd, NULL, &dsize);
7866 dev_sectors = dsize / 512;
7867 if (dev_sectors < min_dev_sectors)
7868 min_dev_sectors = dev_sectors;
7869 close(fd);
7870 }
7871 migr_rec->ckpt_area_pba = __cpu_to_le32(min_dev_sectors -
7872 RAID_DISK_RESERVED_BLOCKS_IMSM_HI);
7873
7874 write_imsm_migr_rec(st);
7875
7876 return;
7877}
7878
7879/*******************************************************************************
7880 * Function: save_backup_imsm
7881 * Description: Function saves critical data stripes to Migration Copy Area
7882 * and updates the current migration unit status.
7883 * Use restore_stripes() to form a destination stripe,
7884 * and to write it to the Copy Area.
7885 * Parameters:
7886 * st : supertype information
aea93171 7887 * dev : imsm device that backup is saved for
687629c2
AK
7888 * info : general array info
7889 * buf : input buffer
687629c2
AK
7890 * length : length of data to backup (blocks_per_unit)
7891 * Returns:
7892 * 0 : success
7893 *, -1 : fail
7894 ******************************************************************************/
7895int save_backup_imsm(struct supertype *st,
7896 struct imsm_dev *dev,
7897 struct mdinfo *info,
7898 void *buf,
687629c2
AK
7899 int length)
7900{
7901 int rv = -1;
7902 struct intel_super *super = st->sb;
7903 unsigned long long *target_offsets = NULL;
7904 int *targets = NULL;
7905 int i;
7906 struct imsm_map *map_dest = get_imsm_map(dev, 0);
7907 int new_disks = map_dest->num_members;
ab724b98
AK
7908 int dest_layout = 0;
7909 int dest_chunk;
d1877f69
AK
7910 unsigned long long start;
7911 int data_disks = imsm_num_data_members(dev, 0);
687629c2
AK
7912
7913 targets = malloc(new_disks * sizeof(int));
7914 if (!targets)
7915 goto abort;
7916
7e45b550
AK
7917 for (i = 0; i < new_disks; i++)
7918 targets[i] = -1;
7919
687629c2
AK
7920 target_offsets = malloc(new_disks * sizeof(unsigned long long));
7921 if (!target_offsets)
7922 goto abort;
7923
d1877f69 7924 start = info->reshape_progress * 512;
687629c2 7925 for (i = 0; i < new_disks; i++) {
687629c2
AK
7926 target_offsets[i] = (unsigned long long)
7927 __le32_to_cpu(super->migr_rec->ckpt_area_pba) * 512;
d1877f69
AK
7928 /* move back copy area adderss, it will be moved forward
7929 * in restore_stripes() using start input variable
7930 */
7931 target_offsets[i] -= start/data_disks;
687629c2
AK
7932 }
7933
7934 if (open_backup_targets(info, new_disks, targets))
7935 goto abort;
7936
68eb8bc6 7937 dest_layout = imsm_level_to_layout(map_dest->raid_level);
ab724b98
AK
7938 dest_chunk = __le16_to_cpu(map_dest->blocks_per_strip) * 512;
7939
687629c2
AK
7940 if (restore_stripes(targets, /* list of dest devices */
7941 target_offsets, /* migration record offsets */
7942 new_disks,
ab724b98
AK
7943 dest_chunk,
7944 map_dest->raid_level,
7945 dest_layout,
7946 -1, /* source backup file descriptor */
7947 0, /* input buf offset
7948 * always 0 buf is already offseted */
d1877f69 7949 start,
687629c2
AK
7950 length,
7951 buf) != 0) {
7952 fprintf(stderr, Name ": Error restoring stripes\n");
7953 goto abort;
7954 }
7955
7956 rv = 0;
7957
7958abort:
7959 if (targets) {
7960 for (i = 0; i < new_disks; i++)
7961 if (targets[i] >= 0)
7962 close(targets[i]);
7963 free(targets);
7964 }
7965 free(target_offsets);
7966
7967 return rv;
7968}
7969
7970/*******************************************************************************
7971 * Function: save_checkpoint_imsm
7972 * Description: Function called for current unit status update
7973 * in the migration record. It writes it to disk.
7974 * Parameters:
7975 * super : imsm internal array info
7976 * info : general array info
7977 * Returns:
7978 * 0: success
7979 * 1: failure
0228d92c
AK
7980 * 2: failure, means no valid migration record
7981 * / no general migration in progress /
687629c2
AK
7982 ******************************************************************************/
7983int save_checkpoint_imsm(struct supertype *st, struct mdinfo *info, int state)
7984{
7985 struct intel_super *super = st->sb;
f8b72ef5
AK
7986 unsigned long long blocks_per_unit;
7987 unsigned long long curr_migr_unit;
7988
2e062e82
AK
7989 if (load_imsm_migr_rec(super, info) != 0) {
7990 dprintf("imsm: ERROR: Cannot read migration record "
7991 "for checkpoint save.\n");
7992 return 1;
7993 }
7994
f8b72ef5
AK
7995 blocks_per_unit = __le32_to_cpu(super->migr_rec->blocks_per_unit);
7996 if (blocks_per_unit == 0) {
0228d92c
AK
7997 dprintf("imsm: no migration in progress.\n");
7998 return 2;
687629c2 7999 }
f8b72ef5
AK
8000 curr_migr_unit = info->reshape_progress / blocks_per_unit;
8001 /* check if array is alligned to copy area
8002 * if it is not alligned, add one to current migration unit value
8003 * this can happend on array reshape finish only
8004 */
8005 if (info->reshape_progress % blocks_per_unit)
8006 curr_migr_unit++;
687629c2
AK
8007
8008 super->migr_rec->curr_migr_unit =
f8b72ef5 8009 __cpu_to_le32(curr_migr_unit);
687629c2
AK
8010 super->migr_rec->rec_status = __cpu_to_le32(state);
8011 super->migr_rec->dest_1st_member_lba =
f8b72ef5
AK
8012 __cpu_to_le32(curr_migr_unit *
8013 __le32_to_cpu(super->migr_rec->dest_depth_per_unit));
687629c2
AK
8014 if (write_imsm_migr_rec(st) < 0) {
8015 dprintf("imsm: Cannot write migration record "
8016 "outside backup area\n");
8017 return 1;
8018 }
8019
8020 return 0;
8021}
8022
276d77db
AK
8023/*******************************************************************************
8024 * Function: recover_backup_imsm
8025 * Description: Function recovers critical data from the Migration Copy Area
8026 * while assembling an array.
8027 * Parameters:
8028 * super : imsm internal array info
8029 * info : general array info
8030 * Returns:
8031 * 0 : success (or there is no data to recover)
8032 * 1 : fail
8033 ******************************************************************************/
8034int recover_backup_imsm(struct supertype *st, struct mdinfo *info)
8035{
8036 struct intel_super *super = st->sb;
8037 struct migr_record *migr_rec = super->migr_rec;
8038 struct imsm_map *map_dest = NULL;
8039 struct intel_dev *id = NULL;
8040 unsigned long long read_offset;
8041 unsigned long long write_offset;
8042 unsigned unit_len;
8043 int *targets = NULL;
8044 int new_disks, i, err;
8045 char *buf = NULL;
8046 int retval = 1;
8047 unsigned long curr_migr_unit = __le32_to_cpu(migr_rec->curr_migr_unit);
8048 unsigned long num_migr_units = __le32_to_cpu(migr_rec->num_migr_units);
276d77db 8049 char buffer[20];
6c3560c0
AK
8050 int skipped_disks = 0;
8051 int max_degradation;
276d77db
AK
8052
8053 err = sysfs_get_str(info, NULL, "array_state", (char *)buffer, 20);
8054 if (err < 1)
8055 return 1;
8056
8057 /* recover data only during assemblation */
8058 if (strncmp(buffer, "inactive", 8) != 0)
8059 return 0;
8060 /* no data to recover */
8061 if (__le32_to_cpu(migr_rec->rec_status) == UNIT_SRC_NORMAL)
8062 return 0;
8063 if (curr_migr_unit >= num_migr_units)
8064 return 1;
8065
8066 /* find device during reshape */
8067 for (id = super->devlist; id; id = id->next)
8068 if (is_gen_migration(id->dev))
8069 break;
8070 if (id == NULL)
8071 return 1;
8072
8073 map_dest = get_imsm_map(id->dev, 0);
8074 new_disks = map_dest->num_members;
6c3560c0 8075 max_degradation = new_disks - imsm_num_data_members(id->dev, 0);
276d77db
AK
8076
8077 read_offset = (unsigned long long)
8078 __le32_to_cpu(migr_rec->ckpt_area_pba) * 512;
8079
8080 write_offset = ((unsigned long long)
8081 __le32_to_cpu(migr_rec->dest_1st_member_lba) +
75b69ea4 8082 __le32_to_cpu(map_dest->pba_of_lba0)) * 512;
276d77db
AK
8083
8084 unit_len = __le32_to_cpu(migr_rec->dest_depth_per_unit) * 512;
8085 if (posix_memalign((void **)&buf, 512, unit_len) != 0)
8086 goto abort;
8087 targets = malloc(new_disks * sizeof(int));
8088 if (!targets)
8089 goto abort;
8090
8091 open_backup_targets(info, new_disks, targets);
8092
8093 for (i = 0; i < new_disks; i++) {
6c3560c0
AK
8094 if (targets[i] < 0) {
8095 skipped_disks++;
8096 continue;
8097 }
276d77db
AK
8098 if (lseek64(targets[i], read_offset, SEEK_SET) < 0) {
8099 fprintf(stderr,
8100 Name ": Cannot seek to block: %s\n",
8101 strerror(errno));
8102 goto abort;
8103 }
9ec11d1a 8104 if ((unsigned)read(targets[i], buf, unit_len) != unit_len) {
276d77db
AK
8105 fprintf(stderr,
8106 Name ": Cannot read copy area block: %s\n",
8107 strerror(errno));
8108 goto abort;
8109 }
8110 if (lseek64(targets[i], write_offset, SEEK_SET) < 0) {
8111 fprintf(stderr,
8112 Name ": Cannot seek to block: %s\n",
8113 strerror(errno));
8114 goto abort;
8115 }
9ec11d1a 8116 if ((unsigned)write(targets[i], buf, unit_len) != unit_len) {
276d77db
AK
8117 fprintf(stderr,
8118 Name ": Cannot restore block: %s\n",
8119 strerror(errno));
8120 goto abort;
8121 }
8122 }
8123
6c3560c0
AK
8124 if (skipped_disks > max_degradation) {
8125 fprintf(stderr,
8126 Name ": Cannot restore data from backup."
8127 " Too many failed disks\n");
8128 goto abort;
8129 }
8130
befb629b
AK
8131 if (save_checkpoint_imsm(st, info, UNIT_SRC_NORMAL)) {
8132 /* ignore error == 2, this can mean end of reshape here
8133 */
8134 dprintf("imsm: Cannot write checkpoint to "
8135 "migration record (UNIT_SRC_NORMAL) during restart\n");
8136 } else
276d77db 8137 retval = 0;
276d77db
AK
8138
8139abort:
8140 if (targets) {
8141 for (i = 0; i < new_disks; i++)
8142 if (targets[i])
8143 close(targets[i]);
8144 free(targets);
8145 }
8146 free(buf);
8147 return retval;
8148}
8149
2cda7640
ML
8150static char disk_by_path[] = "/dev/disk/by-path/";
8151
8152static const char *imsm_get_disk_controller_domain(const char *path)
8153{
2cda7640 8154 char disk_path[PATH_MAX];
96234762
LM
8155 char *drv=NULL;
8156 struct stat st;
2cda7640 8157
96234762
LM
8158 strncpy(disk_path, disk_by_path, PATH_MAX - 1);
8159 strncat(disk_path, path, PATH_MAX - strlen(disk_path) - 1);
8160 if (stat(disk_path, &st) == 0) {
8161 struct sys_dev* hba;
8162 char *path=NULL;
8163
8164 path = devt_to_devpath(st.st_rdev);
8165 if (path == NULL)
8166 return "unknown";
8167 hba = find_disk_attached_hba(-1, path);
8168 if (hba && hba->type == SYS_DEV_SAS)
8169 drv = "isci";
8170 else if (hba && hba->type == SYS_DEV_SATA)
8171 drv = "ahci";
8172 else
8173 drv = "unknown";
8174 dprintf("path: %s hba: %s attached: %s\n",
8175 path, (hba) ? hba->path : "NULL", drv);
8176 free(path);
8177 if (hba)
8178 free_sys_dev(&hba);
2cda7640 8179 }
96234762 8180 return drv;
2cda7640
ML
8181}
8182
78b10e66
N
8183static int imsm_find_array_minor_by_subdev(int subdev, int container, int *minor)
8184{
8185 char subdev_name[20];
8186 struct mdstat_ent *mdstat;
8187
8188 sprintf(subdev_name, "%d", subdev);
8189 mdstat = mdstat_by_subdev(subdev_name, container);
8190 if (!mdstat)
8191 return -1;
8192
8193 *minor = mdstat->devnum;
8194 free_mdstat(mdstat);
8195 return 0;
8196}
8197
8198static int imsm_reshape_is_allowed_on_container(struct supertype *st,
8199 struct geo_params *geo,
8200 int *old_raid_disks)
8201{
694575e7
KW
8202 /* currently we only support increasing the number of devices
8203 * for a container. This increases the number of device for each
8204 * member array. They must all be RAID0 or RAID5.
8205 */
78b10e66
N
8206 int ret_val = 0;
8207 struct mdinfo *info, *member;
8208 int devices_that_can_grow = 0;
8209
8210 dprintf("imsm: imsm_reshape_is_allowed_on_container(ENTER): "
8211 "st->devnum = (%i)\n",
8212 st->devnum);
8213
8214 if (geo->size != -1 ||
8215 geo->level != UnSet ||
8216 geo->layout != UnSet ||
8217 geo->chunksize != 0 ||
8218 geo->raid_disks == UnSet) {
8219 dprintf("imsm: Container operation is allowed for "
8220 "raid disks number change only.\n");
8221 return ret_val;
8222 }
8223
8224 info = container_content_imsm(st, NULL);
8225 for (member = info; member; member = member->next) {
8226 int result;
8227 int minor;
8228
8229 dprintf("imsm: checking device_num: %i\n",
8230 member->container_member);
8231
d7d205bd 8232 if (geo->raid_disks <= member->array.raid_disks) {
78b10e66
N
8233 /* we work on container for Online Capacity Expansion
8234 * only so raid_disks has to grow
8235 */
8236 dprintf("imsm: for container operation raid disks "
8237 "increase is required\n");
8238 break;
8239 }
8240
8241 if ((info->array.level != 0) &&
8242 (info->array.level != 5)) {
8243 /* we cannot use this container with other raid level
8244 */
690aae1a 8245 dprintf("imsm: for container operation wrong"
78b10e66
N
8246 " raid level (%i) detected\n",
8247 info->array.level);
8248 break;
8249 } else {
8250 /* check for platform support
8251 * for this raid level configuration
8252 */
8253 struct intel_super *super = st->sb;
8254 if (!is_raid_level_supported(super->orom,
8255 member->array.level,
8256 geo->raid_disks)) {
690aae1a 8257 dprintf("platform does not support raid%d with"
78b10e66
N
8258 " %d disk%s\n",
8259 info->array.level,
8260 geo->raid_disks,
8261 geo->raid_disks > 1 ? "s" : "");
8262 break;
8263 }
2a4a08e7
AK
8264 /* check if component size is aligned to chunk size
8265 */
8266 if (info->component_size %
8267 (info->array.chunk_size/512)) {
8268 dprintf("Component size is not aligned to "
8269 "chunk size\n");
8270 break;
8271 }
78b10e66
N
8272 }
8273
8274 if (*old_raid_disks &&
8275 info->array.raid_disks != *old_raid_disks)
8276 break;
8277 *old_raid_disks = info->array.raid_disks;
8278
8279 /* All raid5 and raid0 volumes in container
8280 * have to be ready for Online Capacity Expansion
8281 * so they need to be assembled. We have already
8282 * checked that no recovery etc is happening.
8283 */
8284 result = imsm_find_array_minor_by_subdev(member->container_member,
8285 st->container_dev,
8286 &minor);
8287 if (result < 0) {
8288 dprintf("imsm: cannot find array\n");
8289 break;
8290 }
8291 devices_that_can_grow++;
8292 }
8293 sysfs_free(info);
8294 if (!member && devices_that_can_grow)
8295 ret_val = 1;
8296
8297 if (ret_val)
8298 dprintf("\tContainer operation allowed\n");
8299 else
8300 dprintf("\tError: %i\n", ret_val);
8301
8302 return ret_val;
8303}
8304
8305/* Function: get_spares_for_grow
8306 * Description: Allocates memory and creates list of spare devices
8307 * avaliable in container. Checks if spare drive size is acceptable.
8308 * Parameters: Pointer to the supertype structure
8309 * Returns: Pointer to the list of spare devices (mdinfo structure) on success,
8310 * NULL if fail
8311 */
8312static struct mdinfo *get_spares_for_grow(struct supertype *st)
8313{
78b10e66 8314 unsigned long long min_size = min_acceptable_spare_size_imsm(st);
326727d9 8315 return container_choose_spares(st, min_size, NULL, NULL, NULL, 0);
78b10e66
N
8316}
8317
8318/******************************************************************************
8319 * function: imsm_create_metadata_update_for_reshape
8320 * Function creates update for whole IMSM container.
8321 *
8322 ******************************************************************************/
8323static int imsm_create_metadata_update_for_reshape(
8324 struct supertype *st,
8325 struct geo_params *geo,
8326 int old_raid_disks,
8327 struct imsm_update_reshape **updatep)
8328{
8329 struct intel_super *super = st->sb;
8330 struct imsm_super *mpb = super->anchor;
8331 int update_memory_size = 0;
8332 struct imsm_update_reshape *u = NULL;
8333 struct mdinfo *spares = NULL;
8334 int i;
8335 int delta_disks = 0;
bbd24d86 8336 struct mdinfo *dev;
78b10e66
N
8337
8338 dprintf("imsm_update_metadata_for_reshape(enter) raid_disks = %i\n",
8339 geo->raid_disks);
8340
8341 delta_disks = geo->raid_disks - old_raid_disks;
8342
8343 /* size of all update data without anchor */
8344 update_memory_size = sizeof(struct imsm_update_reshape);
8345
8346 /* now add space for spare disks that we need to add. */
8347 update_memory_size += sizeof(u->new_disks[0]) * (delta_disks - 1);
8348
8349 u = calloc(1, update_memory_size);
8350 if (u == NULL) {
8351 dprintf("error: "
8352 "cannot get memory for imsm_update_reshape update\n");
8353 return 0;
8354 }
8355 u->type = update_reshape_container_disks;
8356 u->old_raid_disks = old_raid_disks;
8357 u->new_raid_disks = geo->raid_disks;
8358
8359 /* now get spare disks list
8360 */
8361 spares = get_spares_for_grow(st);
8362
8363 if (spares == NULL
8364 || delta_disks > spares->array.spare_disks) {
e14e5960
KW
8365 fprintf(stderr, Name ": imsm: ERROR: Cannot get spare devices "
8366 "for %s.\n", geo->dev_name);
e4c72d1d 8367 i = -1;
78b10e66
N
8368 goto abort;
8369 }
8370
8371 /* we have got spares
8372 * update disk list in imsm_disk list table in anchor
8373 */
8374 dprintf("imsm: %i spares are available.\n\n",
8375 spares->array.spare_disks);
8376
bbd24d86 8377 dev = spares->devs;
78b10e66 8378 for (i = 0; i < delta_disks; i++) {
78b10e66
N
8379 struct dl *dl;
8380
bbd24d86
AK
8381 if (dev == NULL)
8382 break;
78b10e66
N
8383 u->new_disks[i] = makedev(dev->disk.major,
8384 dev->disk.minor);
8385 dl = get_disk_super(super, dev->disk.major, dev->disk.minor);
ee4beede
AK
8386 dl->index = mpb->num_disks;
8387 mpb->num_disks++;
bbd24d86 8388 dev = dev->next;
78b10e66 8389 }
78b10e66
N
8390
8391abort:
8392 /* free spares
8393 */
8394 sysfs_free(spares);
8395
d677e0b8 8396 dprintf("imsm: reshape update preparation :");
78b10e66 8397 if (i == delta_disks) {
d677e0b8 8398 dprintf(" OK\n");
78b10e66
N
8399 *updatep = u;
8400 return update_memory_size;
8401 }
8402 free(u);
d677e0b8 8403 dprintf(" Error\n");
78b10e66
N
8404
8405 return 0;
8406}
8407
48c5303a
PC
8408/******************************************************************************
8409 * function: imsm_create_metadata_update_for_migration()
8410 * Creates update for IMSM array.
8411 *
8412 ******************************************************************************/
8413static int imsm_create_metadata_update_for_migration(
8414 struct supertype *st,
8415 struct geo_params *geo,
8416 struct imsm_update_reshape_migration **updatep)
8417{
8418 struct intel_super *super = st->sb;
8419 int update_memory_size = 0;
8420 struct imsm_update_reshape_migration *u = NULL;
8421 struct imsm_dev *dev;
8422 int previous_level = -1;
8423
8424 dprintf("imsm_create_metadata_update_for_migration(enter)"
8425 " New Level = %i\n", geo->level);
8426
8427 /* size of all update data without anchor */
8428 update_memory_size = sizeof(struct imsm_update_reshape_migration);
8429
8430 u = calloc(1, update_memory_size);
8431 if (u == NULL) {
8432 dprintf("error: cannot get memory for "
8433 "imsm_create_metadata_update_for_migration\n");
8434 return 0;
8435 }
8436 u->type = update_reshape_migration;
8437 u->subdev = super->current_vol;
8438 u->new_level = geo->level;
8439 u->new_layout = geo->layout;
8440 u->new_raid_disks = u->old_raid_disks = geo->raid_disks;
8441 u->new_disks[0] = -1;
4bba0439 8442 u->new_chunksize = -1;
48c5303a
PC
8443
8444 dev = get_imsm_dev(super, u->subdev);
8445 if (dev) {
8446 struct imsm_map *map;
8447
8448 map = get_imsm_map(dev, 0);
4bba0439
PC
8449 if (map) {
8450 int current_chunk_size =
8451 __le16_to_cpu(map->blocks_per_strip) / 2;
8452
8453 if (geo->chunksize != current_chunk_size) {
8454 u->new_chunksize = geo->chunksize / 1024;
8455 dprintf("imsm: "
8456 "chunk size change from %i to %i\n",
8457 current_chunk_size, u->new_chunksize);
8458 }
48c5303a 8459 previous_level = map->raid_level;
4bba0439 8460 }
48c5303a
PC
8461 }
8462 if ((geo->level == 5) && (previous_level == 0)) {
8463 struct mdinfo *spares = NULL;
8464
8465 u->new_raid_disks++;
8466 spares = get_spares_for_grow(st);
8467 if ((spares == NULL) || (spares->array.spare_disks < 1)) {
8468 free(u);
8469 sysfs_free(spares);
8470 update_memory_size = 0;
8471 dprintf("error: cannot get spare device "
8472 "for requested migration");
8473 return 0;
8474 }
8475 sysfs_free(spares);
8476 }
8477 dprintf("imsm: reshape update preparation : OK\n");
8478 *updatep = u;
8479
8480 return update_memory_size;
8481}
8482
8dd70bce
AK
8483static void imsm_update_metadata_locally(struct supertype *st,
8484 void *buf, int len)
8485{
8486 struct metadata_update mu;
8487
8488 mu.buf = buf;
8489 mu.len = len;
8490 mu.space = NULL;
8491 mu.space_list = NULL;
8492 mu.next = NULL;
8493 imsm_prepare_update(st, &mu);
8494 imsm_process_update(st, &mu);
8495
8496 while (mu.space_list) {
8497 void **space = mu.space_list;
8498 mu.space_list = *space;
8499 free(space);
8500 }
8501}
78b10e66 8502
471bceb6 8503/***************************************************************************
694575e7 8504* Function: imsm_analyze_change
471bceb6
KW
8505* Description: Function analyze change for single volume
8506* and validate if transition is supported
694575e7
KW
8507* Parameters: Geometry parameters, supertype structure
8508* Returns: Operation type code on success, -1 if fail
471bceb6
KW
8509****************************************************************************/
8510enum imsm_reshape_type imsm_analyze_change(struct supertype *st,
8511 struct geo_params *geo)
694575e7 8512{
471bceb6
KW
8513 struct mdinfo info;
8514 int change = -1;
8515 int check_devs = 0;
c21e737b 8516 int chunk;
471bceb6
KW
8517
8518 getinfo_super_imsm_volume(st, &info, NULL);
471bceb6
KW
8519 if ((geo->level != info.array.level) &&
8520 (geo->level >= 0) &&
8521 (geo->level != UnSet)) {
8522 switch (info.array.level) {
8523 case 0:
8524 if (geo->level == 5) {
b5347799 8525 change = CH_MIGRATION;
e13ce846
AK
8526 if (geo->layout != ALGORITHM_LEFT_ASYMMETRIC) {
8527 fprintf(stderr,
8528 Name " Error. Requested Layout "
8529 "not supported (left-asymmetric layout "
8530 "is supported only)!\n");
8531 change = -1;
8532 goto analyse_change_exit;
8533 }
471bceb6
KW
8534 check_devs = 1;
8535 }
8536 if (geo->level == 10) {
8537 change = CH_TAKEOVER;
8538 check_devs = 1;
8539 }
dfe77a9e
KW
8540 break;
8541 case 1:
8542 if (geo->level == 0) {
8543 change = CH_TAKEOVER;
8544 check_devs = 1;
8545 }
471bceb6 8546 break;
471bceb6
KW
8547 case 10:
8548 if (geo->level == 0) {
8549 change = CH_TAKEOVER;
8550 check_devs = 1;
8551 }
8552 break;
8553 }
8554 if (change == -1) {
8555 fprintf(stderr,
8556 Name " Error. Level Migration from %d to %d "
8557 "not supported!\n",
8558 info.array.level, geo->level);
8559 goto analyse_change_exit;
8560 }
8561 } else
8562 geo->level = info.array.level;
8563
8564 if ((geo->layout != info.array.layout)
8565 && ((geo->layout != UnSet) && (geo->layout != -1))) {
b5347799 8566 change = CH_MIGRATION;
471bceb6
KW
8567 if ((info.array.layout == 0)
8568 && (info.array.level == 5)
8569 && (geo->layout == 5)) {
8570 /* reshape 5 -> 4 */
8571 } else if ((info.array.layout == 5)
8572 && (info.array.level == 5)
8573 && (geo->layout == 0)) {
8574 /* reshape 4 -> 5 */
8575 geo->layout = 0;
8576 geo->level = 5;
8577 } else {
8578 fprintf(stderr,
8579 Name " Error. Layout Migration from %d to %d "
8580 "not supported!\n",
8581 info.array.layout, geo->layout);
8582 change = -1;
8583 goto analyse_change_exit;
8584 }
8585 } else
8586 geo->layout = info.array.layout;
8587
8588 if ((geo->chunksize > 0) && (geo->chunksize != UnSet)
8589 && (geo->chunksize != info.array.chunk_size))
b5347799 8590 change = CH_MIGRATION;
471bceb6
KW
8591 else
8592 geo->chunksize = info.array.chunk_size;
8593
c21e737b 8594 chunk = geo->chunksize / 1024;
471bceb6
KW
8595 if (!validate_geometry_imsm(st,
8596 geo->level,
8597 geo->layout,
8598 geo->raid_disks,
c21e737b 8599 &chunk,
471bceb6
KW
8600 geo->size,
8601 0, 0, 1))
8602 change = -1;
8603
8604 if (check_devs) {
8605 struct intel_super *super = st->sb;
8606 struct imsm_super *mpb = super->anchor;
8607
8608 if (mpb->num_raid_devs > 1) {
8609 fprintf(stderr,
8610 Name " Error. Cannot perform operation on %s"
8611 "- for this operation it MUST be single "
8612 "array in container\n",
8613 geo->dev_name);
8614 change = -1;
8615 }
8616 }
8617
8618analyse_change_exit:
8619
8620 return change;
694575e7
KW
8621}
8622
bb025c2f
KW
8623int imsm_takeover(struct supertype *st, struct geo_params *geo)
8624{
8625 struct intel_super *super = st->sb;
8626 struct imsm_update_takeover *u;
8627
8628 u = malloc(sizeof(struct imsm_update_takeover));
8629 if (u == NULL)
8630 return 1;
8631
8632 u->type = update_takeover;
8633 u->subarray = super->current_vol;
8634
8635 /* 10->0 transition */
8636 if (geo->level == 0)
8637 u->direction = R10_TO_R0;
8638
0529c688
KW
8639 /* 0->10 transition */
8640 if (geo->level == 10)
8641 u->direction = R0_TO_R10;
8642
bb025c2f
KW
8643 /* update metadata locally */
8644 imsm_update_metadata_locally(st, u,
8645 sizeof(struct imsm_update_takeover));
8646 /* and possibly remotely */
8647 if (st->update_tail)
8648 append_metadata_update(st, u,
8649 sizeof(struct imsm_update_takeover));
8650 else
8651 free(u);
8652
8653 return 0;
8654}
8655
78b10e66
N
8656static int imsm_reshape_super(struct supertype *st, long long size, int level,
8657 int layout, int chunksize, int raid_disks,
41784c88
AK
8658 int delta_disks, char *backup, char *dev,
8659 int verbose)
78b10e66 8660{
78b10e66
N
8661 int ret_val = 1;
8662 struct geo_params geo;
8663
8664 dprintf("imsm: reshape_super called.\n");
8665
71204a50 8666 memset(&geo, 0, sizeof(struct geo_params));
78b10e66
N
8667
8668 geo.dev_name = dev;
694575e7 8669 geo.dev_id = st->devnum;
78b10e66
N
8670 geo.size = size;
8671 geo.level = level;
8672 geo.layout = layout;
8673 geo.chunksize = chunksize;
8674 geo.raid_disks = raid_disks;
41784c88
AK
8675 if (delta_disks != UnSet)
8676 geo.raid_disks += delta_disks;
78b10e66
N
8677
8678 dprintf("\tfor level : %i\n", geo.level);
8679 dprintf("\tfor raid_disks : %i\n", geo.raid_disks);
8680
8681 if (experimental() == 0)
8682 return ret_val;
8683
78b10e66 8684 if (st->container_dev == st->devnum) {
694575e7
KW
8685 /* On container level we can only increase number of devices. */
8686 dprintf("imsm: info: Container operation\n");
78b10e66 8687 int old_raid_disks = 0;
6dc0be30 8688
78b10e66
N
8689 if (imsm_reshape_is_allowed_on_container(
8690 st, &geo, &old_raid_disks)) {
8691 struct imsm_update_reshape *u = NULL;
8692 int len;
8693
8694 len = imsm_create_metadata_update_for_reshape(
8695 st, &geo, old_raid_disks, &u);
8696
ed08d51c
AK
8697 if (len <= 0) {
8698 dprintf("imsm: Cannot prepare update\n");
8699 goto exit_imsm_reshape_super;
8700 }
8701
8dd70bce
AK
8702 ret_val = 0;
8703 /* update metadata locally */
8704 imsm_update_metadata_locally(st, u, len);
8705 /* and possibly remotely */
8706 if (st->update_tail)
8707 append_metadata_update(st, u, len);
8708 else
ed08d51c 8709 free(u);
8dd70bce 8710
694575e7 8711 } else {
e7ff7e40
AK
8712 fprintf(stderr, Name ": (imsm) Operation "
8713 "is not allowed on this container\n");
694575e7
KW
8714 }
8715 } else {
8716 /* On volume level we support following operations
471bceb6
KW
8717 * - takeover: raid10 -> raid0; raid0 -> raid10
8718 * - chunk size migration
8719 * - migration: raid5 -> raid0; raid0 -> raid5
8720 */
8721 struct intel_super *super = st->sb;
8722 struct intel_dev *dev = super->devlist;
8723 int change, devnum;
694575e7 8724 dprintf("imsm: info: Volume operation\n");
471bceb6
KW
8725 /* find requested device */
8726 while (dev) {
19986c72
MB
8727 if (imsm_find_array_minor_by_subdev(
8728 dev->index, st->container_dev, &devnum) == 0
8729 && devnum == geo.dev_id)
471bceb6
KW
8730 break;
8731 dev = dev->next;
8732 }
8733 if (dev == NULL) {
8734 fprintf(stderr, Name " Cannot find %s (%i) subarray\n",
8735 geo.dev_name, geo.dev_id);
8736 goto exit_imsm_reshape_super;
8737 }
8738 super->current_vol = dev->index;
694575e7
KW
8739 change = imsm_analyze_change(st, &geo);
8740 switch (change) {
471bceb6 8741 case CH_TAKEOVER:
bb025c2f 8742 ret_val = imsm_takeover(st, &geo);
694575e7 8743 break;
48c5303a
PC
8744 case CH_MIGRATION: {
8745 struct imsm_update_reshape_migration *u = NULL;
8746 int len =
8747 imsm_create_metadata_update_for_migration(
8748 st, &geo, &u);
8749 if (len < 1) {
8750 dprintf("imsm: "
8751 "Cannot prepare update\n");
8752 break;
8753 }
471bceb6 8754 ret_val = 0;
48c5303a
PC
8755 /* update metadata locally */
8756 imsm_update_metadata_locally(st, u, len);
8757 /* and possibly remotely */
8758 if (st->update_tail)
8759 append_metadata_update(st, u, len);
8760 else
8761 free(u);
8762 }
8763 break;
471bceb6
KW
8764 default:
8765 ret_val = 1;
694575e7 8766 }
694575e7 8767 }
78b10e66 8768
ed08d51c 8769exit_imsm_reshape_super:
78b10e66
N
8770 dprintf("imsm: reshape_super Exit code = %i\n", ret_val);
8771 return ret_val;
8772}
2cda7640 8773
eee67a47
AK
8774/*******************************************************************************
8775 * Function: wait_for_reshape_imsm
8776 * Description: Function writes new sync_max value and waits until
8777 * reshape process reach new position
8778 * Parameters:
8779 * sra : general array info
eee67a47
AK
8780 * ndata : number of disks in new array's layout
8781 * Returns:
8782 * 0 : success,
8783 * 1 : there is no reshape in progress,
8784 * -1 : fail
8785 ******************************************************************************/
ae9f01f8 8786int wait_for_reshape_imsm(struct mdinfo *sra, int ndata)
eee67a47
AK
8787{
8788 int fd = sysfs_get_fd(sra, NULL, "reshape_position");
8789 unsigned long long completed;
ae9f01f8
AK
8790 /* to_complete : new sync_max position */
8791 unsigned long long to_complete = sra->reshape_progress;
8792 unsigned long long position_to_set = to_complete / ndata;
eee67a47 8793
ae9f01f8
AK
8794 if (fd < 0) {
8795 dprintf("imsm: wait_for_reshape_imsm() "
8796 "cannot open reshape_position\n");
eee67a47 8797 return 1;
ae9f01f8 8798 }
eee67a47 8799
ae9f01f8
AK
8800 if (sysfs_fd_get_ll(fd, &completed) < 0) {
8801 dprintf("imsm: wait_for_reshape_imsm() "
8802 "cannot read reshape_position (no reshape in progres)\n");
8803 close(fd);
8804 return 0;
8805 }
eee67a47 8806
ae9f01f8
AK
8807 if (completed > to_complete) {
8808 dprintf("imsm: wait_for_reshape_imsm() "
8809 "wrong next position to set %llu (%llu)\n",
8810 to_complete, completed);
8811 close(fd);
8812 return -1;
8813 }
8814 dprintf("Position set: %llu\n", position_to_set);
8815 if (sysfs_set_num(sra, NULL, "sync_max",
8816 position_to_set) != 0) {
8817 dprintf("imsm: wait_for_reshape_imsm() "
8818 "cannot set reshape position to %llu\n",
8819 position_to_set);
8820 close(fd);
8821 return -1;
eee67a47
AK
8822 }
8823
eee67a47
AK
8824 do {
8825 char action[20];
8826 fd_set rfds;
8827 FD_ZERO(&rfds);
8828 FD_SET(fd, &rfds);
a47e44fb
AK
8829 select(fd+1, &rfds, NULL, NULL, NULL);
8830 if (sysfs_get_str(sra, NULL, "sync_action",
8831 action, 20) > 0 &&
8832 strncmp(action, "reshape", 7) != 0)
8833 break;
eee67a47 8834 if (sysfs_fd_get_ll(fd, &completed) < 0) {
ae9f01f8
AK
8835 dprintf("imsm: wait_for_reshape_imsm() "
8836 "cannot read reshape_position (in loop)\n");
eee67a47
AK
8837 close(fd);
8838 return 1;
8839 }
eee67a47
AK
8840 } while (completed < to_complete);
8841 close(fd);
8842 return 0;
8843
8844}
8845
b915c95f
AK
8846/*******************************************************************************
8847 * Function: check_degradation_change
8848 * Description: Check that array hasn't become failed.
8849 * Parameters:
8850 * info : for sysfs access
8851 * sources : source disks descriptors
8852 * degraded: previous degradation level
8853 * Returns:
8854 * degradation level
8855 ******************************************************************************/
8856int check_degradation_change(struct mdinfo *info,
8857 int *sources,
8858 int degraded)
8859{
8860 unsigned long long new_degraded;
8861 sysfs_get_ll(info, NULL, "degraded", &new_degraded);
8862 if (new_degraded != (unsigned long long)degraded) {
8863 /* check each device to ensure it is still working */
8864 struct mdinfo *sd;
8865 new_degraded = 0;
8866 for (sd = info->devs ; sd ; sd = sd->next) {
8867 if (sd->disk.state & (1<<MD_DISK_FAULTY))
8868 continue;
8869 if (sd->disk.state & (1<<MD_DISK_SYNC)) {
8870 char sbuf[20];
8871 if (sysfs_get_str(info,
8872 sd, "state", sbuf, 20) < 0 ||
8873 strstr(sbuf, "faulty") ||
8874 strstr(sbuf, "in_sync") == NULL) {
8875 /* this device is dead */
8876 sd->disk.state = (1<<MD_DISK_FAULTY);
8877 if (sd->disk.raid_disk >= 0 &&
8878 sources[sd->disk.raid_disk] >= 0) {
8879 close(sources[
8880 sd->disk.raid_disk]);
8881 sources[sd->disk.raid_disk] =
8882 -1;
8883 }
8884 new_degraded++;
8885 }
8886 }
8887 }
8888 }
8889
8890 return new_degraded;
8891}
8892
10f22854
AK
8893/*******************************************************************************
8894 * Function: imsm_manage_reshape
8895 * Description: Function finds array under reshape and it manages reshape
8896 * process. It creates stripes backups (if required) and sets
8897 * checheckpoits.
8898 * Parameters:
8899 * afd : Backup handle (nattive) - not used
8900 * sra : general array info
8901 * reshape : reshape parameters - not used
8902 * st : supertype structure
8903 * blocks : size of critical section [blocks]
8904 * fds : table of source device descriptor
8905 * offsets : start of array (offest per devices)
8906 * dests : not used
8907 * destfd : table of destination device descriptor
8908 * destoffsets : table of destination offsets (per device)
8909 * Returns:
8910 * 1 : success, reshape is done
8911 * 0 : fail
8912 ******************************************************************************/
999b4972
N
8913static int imsm_manage_reshape(
8914 int afd, struct mdinfo *sra, struct reshape *reshape,
10f22854 8915 struct supertype *st, unsigned long backup_blocks,
999b4972
N
8916 int *fds, unsigned long long *offsets,
8917 int dests, int *destfd, unsigned long long *destoffsets)
8918{
10f22854
AK
8919 int ret_val = 0;
8920 struct intel_super *super = st->sb;
8921 struct intel_dev *dv = NULL;
8922 struct imsm_dev *dev = NULL;
a6b6d984 8923 struct imsm_map *map_src;
10f22854
AK
8924 int migr_vol_qan = 0;
8925 int ndata, odata; /* [bytes] */
8926 int chunk; /* [bytes] */
8927 struct migr_record *migr_rec;
8928 char *buf = NULL;
8929 unsigned int buf_size; /* [bytes] */
8930 unsigned long long max_position; /* array size [bytes] */
8931 unsigned long long next_step; /* [blocks]/[bytes] */
8932 unsigned long long old_data_stripe_length;
10f22854
AK
8933 unsigned long long start_src; /* [bytes] */
8934 unsigned long long start; /* [bytes] */
8935 unsigned long long start_buf_shift; /* [bytes] */
b915c95f 8936 int degraded = 0;
ab724b98 8937 int source_layout = 0;
10f22854 8938
1ab242d8 8939 if (!fds || !offsets || !sra)
10f22854
AK
8940 goto abort;
8941
8942 /* Find volume during the reshape */
8943 for (dv = super->devlist; dv; dv = dv->next) {
8944 if (dv->dev->vol.migr_type == MIGR_GEN_MIGR
8945 && dv->dev->vol.migr_state == 1) {
8946 dev = dv->dev;
8947 migr_vol_qan++;
8948 }
8949 }
8950 /* Only one volume can migrate at the same time */
8951 if (migr_vol_qan != 1) {
8952 fprintf(stderr, Name " : %s", migr_vol_qan ?
8953 "Number of migrating volumes greater than 1\n" :
8954 "There is no volume during migrationg\n");
8955 goto abort;
8956 }
8957
8958 map_src = get_imsm_map(dev, 1);
8959 if (map_src == NULL)
8960 goto abort;
10f22854
AK
8961
8962 ndata = imsm_num_data_members(dev, 0);
8963 odata = imsm_num_data_members(dev, 1);
8964
7b1ab482 8965 chunk = __le16_to_cpu(map_src->blocks_per_strip) * 512;
10f22854
AK
8966 old_data_stripe_length = odata * chunk;
8967
8968 migr_rec = super->migr_rec;
8969
10f22854
AK
8970 /* initialize migration record for start condition */
8971 if (sra->reshape_progress == 0)
8972 init_migr_record_imsm(st, dev, sra);
b2c59438
AK
8973 else {
8974 if (__le32_to_cpu(migr_rec->rec_status) != UNIT_SRC_NORMAL) {
8975 dprintf("imsm: cannot restart migration when data "
8976 "are present in copy area.\n");
8977 goto abort;
8978 }
8979 }
10f22854
AK
8980
8981 /* size for data */
8982 buf_size = __le32_to_cpu(migr_rec->blocks_per_unit) * 512;
8983 /* extend buffer size for parity disk */
8984 buf_size += __le32_to_cpu(migr_rec->dest_depth_per_unit) * 512;
8985 /* add space for stripe aligment */
8986 buf_size += old_data_stripe_length;
8987 if (posix_memalign((void **)&buf, 4096, buf_size)) {
8988 dprintf("imsm: Cannot allocate checpoint buffer\n");
8989 goto abort;
8990 }
8991
3ef4403c 8992 max_position = sra->component_size * ndata;
68eb8bc6 8993 source_layout = imsm_level_to_layout(map_src->raid_level);
10f22854
AK
8994
8995 while (__le32_to_cpu(migr_rec->curr_migr_unit) <
8996 __le32_to_cpu(migr_rec->num_migr_units)) {
8997 /* current reshape position [blocks] */
8998 unsigned long long current_position =
8999 __le32_to_cpu(migr_rec->blocks_per_unit)
9000 * __le32_to_cpu(migr_rec->curr_migr_unit);
9001 unsigned long long border;
9002
b915c95f
AK
9003 /* Check that array hasn't become failed.
9004 */
9005 degraded = check_degradation_change(sra, fds, degraded);
9006 if (degraded > 1) {
9007 dprintf("imsm: Abort reshape due to degradation"
9008 " level (%i)\n", degraded);
9009 goto abort;
9010 }
9011
10f22854
AK
9012 next_step = __le32_to_cpu(migr_rec->blocks_per_unit);
9013
9014 if ((current_position + next_step) > max_position)
9015 next_step = max_position - current_position;
9016
92144abf 9017 start = current_position * 512;
10f22854
AK
9018
9019 /* allign reading start to old geometry */
9020 start_buf_shift = start % old_data_stripe_length;
9021 start_src = start - start_buf_shift;
9022
9023 border = (start_src / odata) - (start / ndata);
9024 border /= 512;
9025 if (border <= __le32_to_cpu(migr_rec->dest_depth_per_unit)) {
9026 /* save critical stripes to buf
9027 * start - start address of current unit
9028 * to backup [bytes]
9029 * start_src - start address of current unit
9030 * to backup alligned to source array
9031 * [bytes]
9032 */
9033 unsigned long long next_step_filler = 0;
9034 unsigned long long copy_length = next_step * 512;
9035
9036 /* allign copy area length to stripe in old geometry */
9037 next_step_filler = ((copy_length + start_buf_shift)
9038 % old_data_stripe_length);
9039 if (next_step_filler)
9040 next_step_filler = (old_data_stripe_length
9041 - next_step_filler);
9042 dprintf("save_stripes() parameters: start = %llu,"
9043 "\tstart_src = %llu,\tnext_step*512 = %llu,"
9044 "\tstart_in_buf_shift = %llu,"
9045 "\tnext_step_filler = %llu\n",
9046 start, start_src, copy_length,
9047 start_buf_shift, next_step_filler);
9048
9049 if (save_stripes(fds, offsets, map_src->num_members,
ab724b98
AK
9050 chunk, map_src->raid_level,
9051 source_layout, 0, NULL, start_src,
10f22854
AK
9052 copy_length +
9053 next_step_filler + start_buf_shift,
9054 buf)) {
9055 dprintf("imsm: Cannot save stripes"
9056 " to buffer\n");
9057 goto abort;
9058 }
9059 /* Convert data to destination format and store it
9060 * in backup general migration area
9061 */
9062 if (save_backup_imsm(st, dev, sra,
aea93171 9063 buf + start_buf_shift, copy_length)) {
10f22854
AK
9064 dprintf("imsm: Cannot save stripes to "
9065 "target devices\n");
9066 goto abort;
9067 }
9068 if (save_checkpoint_imsm(st, sra,
9069 UNIT_SRC_IN_CP_AREA)) {
9070 dprintf("imsm: Cannot write checkpoint to "
9071 "migration record (UNIT_SRC_IN_CP_AREA)\n");
9072 goto abort;
9073 }
8016a6d4
AK
9074 } else {
9075 /* set next step to use whole border area */
9076 border /= next_step;
9077 if (border > 1)
9078 next_step *= border;
10f22854
AK
9079 }
9080 /* When data backed up, checkpoint stored,
9081 * kick the kernel to reshape unit of data
9082 */
9083 next_step = next_step + sra->reshape_progress;
8016a6d4
AK
9084 /* limit next step to array max position */
9085 if (next_step > max_position)
9086 next_step = max_position;
10f22854
AK
9087 sysfs_set_num(sra, NULL, "suspend_lo", sra->reshape_progress);
9088 sysfs_set_num(sra, NULL, "suspend_hi", next_step);
ae9f01f8 9089 sra->reshape_progress = next_step;
10f22854
AK
9090
9091 /* wait until reshape finish */
ae9f01f8 9092 if (wait_for_reshape_imsm(sra, ndata) < 0) {
c47b0ff6
AK
9093 dprintf("wait_for_reshape_imsm returned error!\n");
9094 goto abort;
9095 }
10f22854 9096
0228d92c
AK
9097 if (save_checkpoint_imsm(st, sra, UNIT_SRC_NORMAL) == 1) {
9098 /* ignore error == 2, this can mean end of reshape here
9099 */
10f22854
AK
9100 dprintf("imsm: Cannot write checkpoint to "
9101 "migration record (UNIT_SRC_NORMAL)\n");
9102 goto abort;
9103 }
9104
9105 }
9106
9107 /* return '1' if done */
9108 ret_val = 1;
9109abort:
9110 free(buf);
9111 abort_reshape(sra);
9112
9113 return ret_val;
999b4972 9114}
71204a50 9115#endif /* MDASSEMBLE */
999b4972 9116
cdddbdbc
DW
9117struct superswitch super_imsm = {
9118#ifndef MDASSEMBLE
9119 .examine_super = examine_super_imsm,
9120 .brief_examine_super = brief_examine_super_imsm,
4737ae25 9121 .brief_examine_subarrays = brief_examine_subarrays_imsm,
9d84c8ea 9122 .export_examine_super = export_examine_super_imsm,
cdddbdbc
DW
9123 .detail_super = detail_super_imsm,
9124 .brief_detail_super = brief_detail_super_imsm,
bf5a934a 9125 .write_init_super = write_init_super_imsm,
0e600426
N
9126 .validate_geometry = validate_geometry_imsm,
9127 .add_to_super = add_to_super_imsm,
1a64be56 9128 .remove_from_super = remove_from_super_imsm,
d665cc31 9129 .detail_platform = detail_platform_imsm,
33414a01 9130 .kill_subarray = kill_subarray_imsm,
aa534678 9131 .update_subarray = update_subarray_imsm,
2b959fbf 9132 .load_container = load_container_imsm,
71204a50
N
9133 .default_geometry = default_geometry_imsm,
9134 .get_disk_controller_domain = imsm_get_disk_controller_domain,
9135 .reshape_super = imsm_reshape_super,
9136 .manage_reshape = imsm_manage_reshape,
9e2d750d 9137 .recover_backup = recover_backup_imsm,
cdddbdbc
DW
9138#endif
9139 .match_home = match_home_imsm,
9140 .uuid_from_super= uuid_from_super_imsm,
9141 .getinfo_super = getinfo_super_imsm,
5c4cd5da 9142 .getinfo_super_disks = getinfo_super_disks_imsm,
cdddbdbc
DW
9143 .update_super = update_super_imsm,
9144
9145 .avail_size = avail_size_imsm,
80e7f8c3 9146 .min_acceptable_spare_size = min_acceptable_spare_size_imsm,
cdddbdbc
DW
9147
9148 .compare_super = compare_super_imsm,
9149
9150 .load_super = load_super_imsm,
bf5a934a 9151 .init_super = init_super_imsm,
e683ca88 9152 .store_super = store_super_imsm,
cdddbdbc
DW
9153 .free_super = free_super_imsm,
9154 .match_metadata_desc = match_metadata_desc_imsm,
bf5a934a 9155 .container_content = container_content_imsm,
cdddbdbc 9156
276d77db 9157
cdddbdbc 9158 .external = 1,
4cce4069 9159 .name = "imsm",
845dea95 9160
0e600426 9161#ifndef MDASSEMBLE
845dea95
NB
9162/* for mdmon */
9163 .open_new = imsm_open_new,
ed9d66aa 9164 .set_array_state= imsm_set_array_state,
845dea95
NB
9165 .set_disk = imsm_set_disk,
9166 .sync_metadata = imsm_sync_metadata,
88758e9d 9167 .activate_spare = imsm_activate_spare,
e8319a19 9168 .process_update = imsm_process_update,
8273f55e 9169 .prepare_update = imsm_prepare_update,
0e600426 9170#endif /* MDASSEMBLE */
cdddbdbc 9171};