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