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