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