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