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