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