]> git.ipfire.org Git - thirdparty/mdadm.git/blame - super-intel.c
mdadm: Move pr_vrb define to mdadm.h
[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"
420dafcd 23#include "dlink.h"
51006d85 24#include "sha1.h"
88c32bb1 25#include "platform-intel.h"
cdddbdbc
DW
26#include <values.h>
27#include <scsi/sg.h>
28#include <ctype.h>
d665cc31 29#include <dirent.h>
cdddbdbc
DW
30
31/* MPB == Metadata Parameter Block */
32#define MPB_SIGNATURE "Intel Raid ISM Cfg Sig. "
33#define MPB_SIG_LEN (strlen(MPB_SIGNATURE))
34#define MPB_VERSION_RAID0 "1.0.00"
35#define MPB_VERSION_RAID1 "1.1.00"
fe7ed8cb
DW
36#define MPB_VERSION_MANY_VOLUMES_PER_ARRAY "1.2.00"
37#define MPB_VERSION_3OR4_DISK_ARRAY "1.2.01"
cdddbdbc 38#define MPB_VERSION_RAID5 "1.2.02"
fe7ed8cb
DW
39#define MPB_VERSION_5OR6_DISK_ARRAY "1.2.04"
40#define MPB_VERSION_CNG "1.2.06"
41#define MPB_VERSION_ATTRIBS "1.3.00"
cdddbdbc
DW
42#define MAX_SIGNATURE_LENGTH 32
43#define MAX_RAID_SERIAL_LEN 16
fe7ed8cb 44
19482bcc
AK
45/* supports RAID0 */
46#define MPB_ATTRIB_RAID0 __cpu_to_le32(0x00000001)
47/* supports RAID1 */
48#define MPB_ATTRIB_RAID1 __cpu_to_le32(0x00000002)
49/* supports RAID10 */
50#define MPB_ATTRIB_RAID10 __cpu_to_le32(0x00000004)
51/* supports RAID1E */
52#define MPB_ATTRIB_RAID1E __cpu_to_le32(0x00000008)
53/* supports RAID5 */
54#define MPB_ATTRIB_RAID5 __cpu_to_le32(0x00000010)
55/* supports RAID CNG */
56#define MPB_ATTRIB_RAIDCNG __cpu_to_le32(0x00000020)
57/* supports expanded stripe sizes of 256K, 512K and 1MB */
58#define MPB_ATTRIB_EXP_STRIPE_SIZE __cpu_to_le32(0x00000040)
59
60/* The OROM Support RST Caching of Volumes */
61#define MPB_ATTRIB_NVM __cpu_to_le32(0x02000000)
62/* The OROM supports creating disks greater than 2TB */
63#define MPB_ATTRIB_2TB_DISK __cpu_to_le32(0x04000000)
64/* The OROM supports Bad Block Management */
65#define MPB_ATTRIB_BBM __cpu_to_le32(0x08000000)
66
67/* THe OROM Supports NVM Caching of Volumes */
68#define MPB_ATTRIB_NEVER_USE2 __cpu_to_le32(0x10000000)
69/* The OROM supports creating volumes greater than 2TB */
70#define MPB_ATTRIB_2TB __cpu_to_le32(0x20000000)
71/* originally for PMP, now it's wasted b/c. Never use this bit! */
72#define MPB_ATTRIB_NEVER_USE __cpu_to_le32(0x40000000)
73/* Verify MPB contents against checksum after reading MPB */
74#define MPB_ATTRIB_CHECKSUM_VERIFY __cpu_to_le32(0x80000000)
75
76/* Define all supported attributes that have to be accepted by mdadm
77 */
418f9b36 78#define MPB_ATTRIB_SUPPORTED (MPB_ATTRIB_CHECKSUM_VERIFY | \
19482bcc
AK
79 MPB_ATTRIB_2TB | \
80 MPB_ATTRIB_2TB_DISK | \
81 MPB_ATTRIB_RAID0 | \
82 MPB_ATTRIB_RAID1 | \
83 MPB_ATTRIB_RAID10 | \
84 MPB_ATTRIB_RAID5 | \
bbab0940
TM
85 MPB_ATTRIB_EXP_STRIPE_SIZE | \
86 MPB_ATTRIB_BBM)
418f9b36
N
87
88/* Define attributes that are unused but not harmful */
89#define MPB_ATTRIB_IGNORED (MPB_ATTRIB_NEVER_USE)
fe7ed8cb 90
8e59f3d8 91#define MPB_SECTOR_CNT 2210
611d9529
MD
92#define IMSM_RESERVED_SECTORS 8192
93#define NUM_BLOCKS_DIRTY_STRIPE_REGION 2048
979d38be 94#define SECT_PER_MB_SHIFT 11
f36a9ecd 95#define MAX_SECTOR_SIZE 4096
c2462068
PB
96#define MULTIPLE_PPL_AREA_SIZE_IMSM (1024 * 1024) /* Size of the whole
97 * mutliple PPL area
98 */
cdddbdbc 99
fbc42556
JR
100/*
101 * Internal Write-intent bitmap is stored in the same area where PPL.
102 * Both features are mutually exclusive, so it is not an issue.
103 * The first 8KiB of the area are reserved and shall not be used.
104 */
105#define IMSM_BITMAP_AREA_RESERVED_SIZE 8192
106
107#define IMSM_BITMAP_HEADER_OFFSET (IMSM_BITMAP_AREA_RESERVED_SIZE)
108#define IMSM_BITMAP_HEADER_SIZE MAX_SECTOR_SIZE
109
110#define IMSM_BITMAP_START_OFFSET (IMSM_BITMAP_HEADER_OFFSET + IMSM_BITMAP_HEADER_SIZE)
111#define IMSM_BITMAP_AREA_SIZE (MULTIPLE_PPL_AREA_SIZE_IMSM - IMSM_BITMAP_START_OFFSET)
112#define IMSM_BITMAP_AND_HEADER_SIZE (IMSM_BITMAP_AREA_SIZE + IMSM_BITMAP_HEADER_SIZE)
113
114#define IMSM_DEFAULT_BITMAP_CHUNKSIZE (64 * 1024 * 1024)
115#define IMSM_DEFAULT_BITMAP_DAEMON_SLEEP 5
116
761e3bd9
N
117/*
118 * This macro let's us ensure that no-one accidentally
119 * changes the size of a struct
120 */
121#define ASSERT_SIZE(_struct, size) \
122static inline void __assert_size_##_struct(void) \
123{ \
124 switch (0) { \
125 case 0: break; \
126 case (sizeof(struct _struct) == size): break; \
127 } \
128}
129
cdddbdbc
DW
130/* Disk configuration info. */
131#define IMSM_MAX_DEVICES 255
132struct imsm_disk {
133 __u8 serial[MAX_RAID_SERIAL_LEN];/* 0xD8 - 0xE7 ascii serial number */
5551b113 134 __u32 total_blocks_lo; /* 0xE8 - 0xEB total blocks lo */
cdddbdbc 135 __u32 scsi_id; /* 0xEC - 0xEF scsi ID */
f2f27e63
DW
136#define SPARE_DISK __cpu_to_le32(0x01) /* Spare */
137#define CONFIGURED_DISK __cpu_to_le32(0x02) /* Member of some RaidDev */
138#define FAILED_DISK __cpu_to_le32(0x04) /* Permanent failure */
2432ce9b 139#define JOURNAL_DISK __cpu_to_le32(0x2000000) /* Device marked as Journaling Drive */
cdddbdbc 140 __u32 status; /* 0xF0 - 0xF3 */
1011e834 141 __u32 owner_cfg_num; /* which config 0,1,2... owns this disk */
5551b113
CA
142 __u32 total_blocks_hi; /* 0xF4 - 0xF5 total blocks hi */
143#define IMSM_DISK_FILLERS 3
144 __u32 filler[IMSM_DISK_FILLERS]; /* 0xF5 - 0x107 MPB_DISK_FILLERS for future expansion */
cdddbdbc 145};
761e3bd9 146ASSERT_SIZE(imsm_disk, 48)
cdddbdbc 147
3b451610
AK
148/* map selector for map managment
149 */
238c0a71
AK
150#define MAP_0 0
151#define MAP_1 1
152#define MAP_X -1
3b451610 153
cdddbdbc
DW
154/* RAID map configuration infos. */
155struct imsm_map {
5551b113
CA
156 __u32 pba_of_lba0_lo; /* start address of partition */
157 __u32 blocks_per_member_lo;/* blocks per member */
158 __u32 num_data_stripes_lo; /* number of data stripes */
cdddbdbc
DW
159 __u16 blocks_per_strip;
160 __u8 map_state; /* Normal, Uninitialized, Degraded, Failed */
161#define IMSM_T_STATE_NORMAL 0
162#define IMSM_T_STATE_UNINITIALIZED 1
e3bba0e0
DW
163#define IMSM_T_STATE_DEGRADED 2
164#define IMSM_T_STATE_FAILED 3
cdddbdbc
DW
165 __u8 raid_level;
166#define IMSM_T_RAID0 0
167#define IMSM_T_RAID1 1
168#define IMSM_T_RAID5 5 /* since metadata version 1.2.02 ? */
169 __u8 num_members; /* number of member disks */
fe7ed8cb
DW
170 __u8 num_domains; /* number of parity domains */
171 __u8 failed_disk_num; /* valid only when state is degraded */
252d23c0 172 __u8 ddf;
5551b113
CA
173 __u32 pba_of_lba0_hi;
174 __u32 blocks_per_member_hi;
175 __u32 num_data_stripes_hi;
176 __u32 filler[4]; /* expansion area */
7eef0453 177#define IMSM_ORD_REBUILD (1 << 24)
cdddbdbc 178 __u32 disk_ord_tbl[1]; /* disk_ord_tbl[num_members],
7eef0453
DW
179 * top byte contains some flags
180 */
761e3bd9
N
181};
182ASSERT_SIZE(imsm_map, 52)
cdddbdbc
DW
183
184struct imsm_vol {
4036e7ee 185 __u32 curr_migr_unit_lo;
fe7ed8cb 186 __u32 checkpoint_id; /* id to access curr_migr_unit */
cdddbdbc 187 __u8 migr_state; /* Normal or Migrating */
e3bba0e0
DW
188#define MIGR_INIT 0
189#define MIGR_REBUILD 1
190#define MIGR_VERIFY 2 /* analagous to echo check > sync_action */
191#define MIGR_GEN_MIGR 3
192#define MIGR_STATE_CHANGE 4
1484e727 193#define MIGR_REPAIR 5
cdddbdbc 194 __u8 migr_type; /* Initializing, Rebuilding, ... */
2432ce9b
AP
195#define RAIDVOL_CLEAN 0
196#define RAIDVOL_DIRTY 1
197#define RAIDVOL_DSRECORD_VALID 2
cdddbdbc 198 __u8 dirty;
fe7ed8cb
DW
199 __u8 fs_state; /* fast-sync state for CnG (0xff == disabled) */
200 __u16 verify_errors; /* number of mismatches */
201 __u16 bad_blocks; /* number of bad blocks during verify */
4036e7ee
MT
202 __u32 curr_migr_unit_hi;
203 __u32 filler[3];
cdddbdbc
DW
204 struct imsm_map map[1];
205 /* here comes another one if migr_state */
761e3bd9
N
206};
207ASSERT_SIZE(imsm_vol, 84)
cdddbdbc
DW
208
209struct imsm_dev {
fe7ed8cb 210 __u8 volume[MAX_RAID_SERIAL_LEN];
cdddbdbc
DW
211 __u32 size_low;
212 __u32 size_high;
fe7ed8cb
DW
213#define DEV_BOOTABLE __cpu_to_le32(0x01)
214#define DEV_BOOT_DEVICE __cpu_to_le32(0x02)
215#define DEV_READ_COALESCING __cpu_to_le32(0x04)
216#define DEV_WRITE_COALESCING __cpu_to_le32(0x08)
217#define DEV_LAST_SHUTDOWN_DIRTY __cpu_to_le32(0x10)
218#define DEV_HIDDEN_AT_BOOT __cpu_to_le32(0x20)
219#define DEV_CURRENTLY_HIDDEN __cpu_to_le32(0x40)
220#define DEV_VERIFY_AND_FIX __cpu_to_le32(0x80)
221#define DEV_MAP_STATE_UNINIT __cpu_to_le32(0x100)
222#define DEV_NO_AUTO_RECOVERY __cpu_to_le32(0x200)
223#define DEV_CLONE_N_GO __cpu_to_le32(0x400)
224#define DEV_CLONE_MAN_SYNC __cpu_to_le32(0x800)
225#define DEV_CNG_MASTER_DISK_NUM __cpu_to_le32(0x1000)
cdddbdbc
DW
226 __u32 status; /* Persistent RaidDev status */
227 __u32 reserved_blocks; /* Reserved blocks at beginning of volume */
fe7ed8cb
DW
228 __u8 migr_priority;
229 __u8 num_sub_vols;
230 __u8 tid;
231 __u8 cng_master_disk;
232 __u16 cache_policy;
233 __u8 cng_state;
234 __u8 cng_sub_state;
2432ce9b
AP
235 __u16 my_vol_raid_dev_num; /* Used in Unique volume Id for this RaidDev */
236
237 /* NVM_EN */
238 __u8 nv_cache_mode;
239 __u8 nv_cache_flags;
240
241 /* Unique Volume Id of the NvCache Volume associated with this volume */
242 __u32 nvc_vol_orig_family_num;
243 __u16 nvc_vol_raid_dev_num;
244
245#define RWH_OFF 0
246#define RWH_DISTRIBUTED 1
247#define RWH_JOURNALING_DRIVE 2
c2462068
PB
248#define RWH_MULTIPLE_DISTRIBUTED 3
249#define RWH_MULTIPLE_PPLS_JOURNALING_DRIVE 4
250#define RWH_MULTIPLE_OFF 5
fbc42556 251#define RWH_BITMAP 6
2432ce9b
AP
252 __u8 rwh_policy; /* Raid Write Hole Policy */
253 __u8 jd_serial[MAX_RAID_SERIAL_LEN]; /* Journal Drive serial number */
254 __u8 filler1;
255
256#define IMSM_DEV_FILLERS 3
cdddbdbc
DW
257 __u32 filler[IMSM_DEV_FILLERS];
258 struct imsm_vol vol;
761e3bd9
N
259};
260ASSERT_SIZE(imsm_dev, 164)
cdddbdbc
DW
261
262struct imsm_super {
263 __u8 sig[MAX_SIGNATURE_LENGTH]; /* 0x00 - 0x1F */
264 __u32 check_sum; /* 0x20 - 0x23 MPB Checksum */
265 __u32 mpb_size; /* 0x24 - 0x27 Size of MPB */
266 __u32 family_num; /* 0x28 - 0x2B Checksum from first time this config was written */
267 __u32 generation_num; /* 0x2C - 0x2F Incremented each time this array's MPB is written */
604b746f
JD
268 __u32 error_log_size; /* 0x30 - 0x33 in bytes */
269 __u32 attributes; /* 0x34 - 0x37 */
cdddbdbc
DW
270 __u8 num_disks; /* 0x38 Number of configured disks */
271 __u8 num_raid_devs; /* 0x39 Number of configured volumes */
604b746f
JD
272 __u8 error_log_pos; /* 0x3A */
273 __u8 fill[1]; /* 0x3B */
274 __u32 cache_size; /* 0x3c - 0x40 in mb */
275 __u32 orig_family_num; /* 0x40 - 0x43 original family num */
276 __u32 pwr_cycle_count; /* 0x44 - 0x47 simulated power cycle count for array */
277 __u32 bbm_log_size; /* 0x48 - 0x4B - size of bad Block Mgmt Log in bytes */
2a24dc1b
PB
278 __u16 num_raid_devs_created; /* 0x4C - 0x4D Used for generating unique
279 * volume IDs for raid_dev created in this array
280 * (starts at 1)
281 */
282 __u16 filler1; /* 0x4E - 0x4F */
e48aed3c
AP
283 __u64 creation_time; /* 0x50 - 0x57 Array creation time */
284#define IMSM_FILLERS 32
285 __u32 filler[IMSM_FILLERS]; /* 0x58 - 0xD7 RAID_MPB_FILLERS */
cdddbdbc
DW
286 struct imsm_disk disk[1]; /* 0xD8 diskTbl[numDisks] */
287 /* here comes imsm_dev[num_raid_devs] */
604b746f 288 /* here comes BBM logs */
761e3bd9
N
289};
290ASSERT_SIZE(imsm_super, 264)
cdddbdbc 291
604b746f 292#define BBM_LOG_MAX_ENTRIES 254
8d67477f
TM
293#define BBM_LOG_MAX_LBA_ENTRY_VAL 256 /* Represents 256 LBAs */
294#define BBM_LOG_SIGNATURE 0xabadb10c
295
296struct bbm_log_block_addr {
297 __u16 w1;
298 __u32 dw1;
299} __attribute__ ((__packed__));
604b746f
JD
300
301struct bbm_log_entry {
8d67477f
TM
302 __u8 marked_count; /* Number of blocks marked - 1 */
303 __u8 disk_ordinal; /* Disk entry within the imsm_super */
304 struct bbm_log_block_addr defective_block_start;
604b746f
JD
305} __attribute__ ((__packed__));
306
307struct bbm_log {
308 __u32 signature; /* 0xABADB10C */
309 __u32 entry_count;
8d67477f 310 struct bbm_log_entry marked_block_entries[BBM_LOG_MAX_ENTRIES];
761e3bd9
N
311};
312ASSERT_SIZE(bbm_log, 2040)
604b746f 313
cdddbdbc 314static char *map_state_str[] = { "normal", "uninitialized", "degraded", "failed" };
cdddbdbc 315
b53bfba6
TM
316#define BLOCKS_PER_KB (1024/512)
317
8e59f3d8
AK
318#define RAID_DISK_RESERVED_BLOCKS_IMSM_HI 2209
319
320#define GEN_MIGR_AREA_SIZE 2048 /* General Migration Copy Area size in blocks */
321
de44e46f
PB
322#define MIGR_REC_BUF_SECTORS 1 /* size of migr_record i/o buffer in sectors */
323#define MIGR_REC_SECTOR_POSITION 1 /* migr_record position offset on disk,
324 * MIGR_REC_BUF_SECTORS <= MIGR_REC_SECTOR_POS
17a4eaf9
AK
325 */
326
8e59f3d8
AK
327#define UNIT_SRC_NORMAL 0 /* Source data for curr_migr_unit must
328 * be recovered using srcMap */
329#define UNIT_SRC_IN_CP_AREA 1 /* Source data for curr_migr_unit has
330 * already been migrated and must
331 * be recovered from checkpoint area */
2432ce9b 332
c2462068 333#define PPL_ENTRY_SPACE (128 * 1024) /* Size of single PPL, without the header */
2432ce9b 334
8e59f3d8
AK
335struct migr_record {
336 __u32 rec_status; /* Status used to determine how to restart
337 * migration in case it aborts
338 * in some fashion */
9f421827 339 __u32 curr_migr_unit_lo; /* 0..numMigrUnits-1 */
8e59f3d8
AK
340 __u32 family_num; /* Family number of MPB
341 * containing the RaidDev
342 * that is migrating */
343 __u32 ascending_migr; /* True if migrating in increasing
344 * order of lbas */
345 __u32 blocks_per_unit; /* Num disk blocks per unit of operation */
346 __u32 dest_depth_per_unit; /* Num member blocks each destMap
347 * member disk
348 * advances per unit-of-operation */
9f421827
PB
349 __u32 ckpt_area_pba_lo; /* Pba of first block of ckpt copy area */
350 __u32 dest_1st_member_lba_lo; /* First member lba on first
351 * stripe of destination */
352 __u32 num_migr_units_lo; /* Total num migration units-of-op */
8e59f3d8
AK
353 __u32 post_migr_vol_cap; /* Size of volume after
354 * migration completes */
355 __u32 post_migr_vol_cap_hi; /* Expansion space for LBA64 */
356 __u32 ckpt_read_disk_num; /* Which member disk in destSubMap[0] the
357 * migration ckpt record was read from
358 * (for recovered migrations) */
9f421827
PB
359 __u32 curr_migr_unit_hi; /* 0..numMigrUnits-1 high order 32 bits */
360 __u32 ckpt_area_pba_hi; /* Pba of first block of ckpt copy area
361 * high order 32 bits */
362 __u32 dest_1st_member_lba_hi; /* First member lba on first stripe of
363 * destination - high order 32 bits */
364 __u32 num_migr_units_hi; /* Total num migration units-of-op
365 * high order 32 bits */
4036e7ee 366 __u32 filler[16];
761e3bd9 367};
4036e7ee 368ASSERT_SIZE(migr_record, 128)
8e59f3d8 369
76c152ca
MT
370/**
371 * enum imsm_status - internal IMSM return values representation.
372 * @STATUS_OK: function succeeded.
373 * @STATUS_ERROR: General error ocurred (not specified).
374 *
375 * Typedefed to imsm_status_t.
376 */
377typedef enum imsm_status {
378 IMSM_STATUS_ERROR = -1,
379 IMSM_STATUS_OK = 0,
380} imsm_status_t;
381
ec50f7b6
LM
382struct md_list {
383 /* usage marker:
384 * 1: load metadata
385 * 2: metadata does not match
386 * 4: already checked
387 */
388 int used;
389 char *devname;
390 int found;
391 int container;
392 dev_t st_rdev;
393 struct md_list *next;
394};
395
1484e727
DW
396static __u8 migr_type(struct imsm_dev *dev)
397{
398 if (dev->vol.migr_type == MIGR_VERIFY &&
399 dev->status & DEV_VERIFY_AND_FIX)
400 return MIGR_REPAIR;
401 else
402 return dev->vol.migr_type;
403}
404
405static void set_migr_type(struct imsm_dev *dev, __u8 migr_type)
406{
407 /* for compatibility with older oroms convert MIGR_REPAIR, into
408 * MIGR_VERIFY w/ DEV_VERIFY_AND_FIX status
409 */
410 if (migr_type == MIGR_REPAIR) {
411 dev->vol.migr_type = MIGR_VERIFY;
412 dev->status |= DEV_VERIFY_AND_FIX;
413 } else {
414 dev->vol.migr_type = migr_type;
415 dev->status &= ~DEV_VERIFY_AND_FIX;
416 }
417}
418
f36a9ecd 419static unsigned int sector_count(__u32 bytes, unsigned int sector_size)
cdddbdbc 420{
f36a9ecd 421 return ROUND_UP(bytes, sector_size) / sector_size;
87eb16df 422}
cdddbdbc 423
f36a9ecd
PB
424static unsigned int mpb_sectors(struct imsm_super *mpb,
425 unsigned int sector_size)
87eb16df 426{
f36a9ecd 427 return sector_count(__le32_to_cpu(mpb->mpb_size), sector_size);
cdddbdbc
DW
428}
429
ba2de7ba
DW
430struct intel_dev {
431 struct imsm_dev *dev;
432 struct intel_dev *next;
f21e18ca 433 unsigned index;
ba2de7ba
DW
434};
435
88654014
LM
436struct intel_hba {
437 enum sys_dev_type type;
438 char *path;
439 char *pci_id;
440 struct intel_hba *next;
441};
442
1a64be56
LM
443enum action {
444 DISK_REMOVE = 1,
445 DISK_ADD
446};
cdddbdbc
DW
447/* internal representation of IMSM metadata */
448struct intel_super {
449 union {
949c47a0
DW
450 void *buf; /* O_DIRECT buffer for reading/writing metadata */
451 struct imsm_super *anchor; /* immovable parameters */
cdddbdbc 452 };
8e59f3d8
AK
453 union {
454 void *migr_rec_buf; /* buffer for I/O operations */
455 struct migr_record *migr_rec; /* migration record */
456 };
51d83f5d
AK
457 int clean_migration_record_by_mdmon; /* when reshape is switched to next
458 array, it indicates that mdmon is allowed to clean migration
459 record */
949c47a0 460 size_t len; /* size of the 'buf' allocation */
bbab0940 461 size_t extra_space; /* extra space in 'buf' that is not used yet */
4d7b1503
DW
462 void *next_buf; /* for realloc'ing buf from the manager */
463 size_t next_len;
c2c087e6 464 int updates_pending; /* count of pending updates for mdmon */
bf5a934a 465 int current_vol; /* index of raid device undergoing creation */
5551b113 466 unsigned long long create_offset; /* common start for 'current_vol' */
148acb7b 467 __u32 random; /* random data for seeding new family numbers */
ba2de7ba 468 struct intel_dev *devlist;
fa7bb6f8 469 unsigned int sector_size; /* sector size of used member drives */
cdddbdbc
DW
470 struct dl {
471 struct dl *next;
472 int index;
473 __u8 serial[MAX_RAID_SERIAL_LEN];
474 int major, minor;
475 char *devname;
b9f594fe 476 struct imsm_disk disk;
cdddbdbc 477 int fd;
0dcecb2e
DW
478 int extent_cnt;
479 struct extent *e; /* for determining freespace @ create */
efb30e7f 480 int raiddisk; /* slot to fill in autolayout */
1a64be56 481 enum action action;
ca0748fa 482 } *disks, *current_disk;
1a64be56
LM
483 struct dl *disk_mgmt_list; /* list of disks to add/remove while mdmon
484 active */
47ee5a45 485 struct dl *missing; /* disks removed while we weren't looking */
43dad3d6 486 struct bbm_log *bbm_log;
88654014 487 struct intel_hba *hba; /* device path of the raid controller for this metadata */
88c32bb1 488 const struct imsm_orom *orom; /* platform firmware support */
a2b97981 489 struct intel_super *next; /* (temp) list for disambiguating family_num */
928f1424 490 struct md_bb bb; /* memory for get_bad_blocks call */
a2b97981
DW
491};
492
493struct intel_disk {
494 struct imsm_disk disk;
495 #define IMSM_UNKNOWN_OWNER (-1)
496 int owner;
497 struct intel_disk *next;
cdddbdbc
DW
498};
499
aa19fdd4
MT
500/**
501 * struct extent - reserved space details.
502 * @start: start offset.
503 * @size: size of reservation, set to 0 for metadata reservation.
504 * @vol: index of the volume, meaningful if &size is set.
505 */
c2c087e6
DW
506struct extent {
507 unsigned long long start, size;
aa19fdd4 508 int vol;
c2c087e6
DW
509};
510
694575e7
KW
511/* definitions of reshape process types */
512enum imsm_reshape_type {
513 CH_TAKEOVER,
b5347799 514 CH_MIGRATION,
7abc9871 515 CH_ARRAY_SIZE,
694575e7
KW
516};
517
88758e9d
DW
518/* definition of messages passed to imsm_process_update */
519enum imsm_update_type {
520 update_activate_spare,
8273f55e 521 update_create_array,
33414a01 522 update_kill_array,
aa534678 523 update_rename_array,
1a64be56 524 update_add_remove_disk,
78b10e66 525 update_reshape_container_disks,
48c5303a 526 update_reshape_migration,
2d40f3a1
AK
527 update_takeover,
528 update_general_migration_checkpoint,
f3871fdc 529 update_size_change,
bbab0940 530 update_prealloc_badblocks_mem,
e6e9dd3f 531 update_rwh_policy,
88758e9d
DW
532};
533
534struct imsm_update_activate_spare {
535 enum imsm_update_type type;
d23fe947 536 struct dl *dl;
88758e9d
DW
537 int slot;
538 int array;
539 struct imsm_update_activate_spare *next;
540};
541
78b10e66 542struct geo_params {
4dd2df09 543 char devnm[32];
78b10e66 544 char *dev_name;
d04f65f4 545 unsigned long long size;
78b10e66
N
546 int level;
547 int layout;
548 int chunksize;
549 int raid_disks;
550};
551
bb025c2f
KW
552enum takeover_direction {
553 R10_TO_R0,
554 R0_TO_R10
555};
556struct imsm_update_takeover {
557 enum imsm_update_type type;
558 int subarray;
559 enum takeover_direction direction;
560};
78b10e66
N
561
562struct imsm_update_reshape {
563 enum imsm_update_type type;
564 int old_raid_disks;
565 int new_raid_disks;
48c5303a
PC
566
567 int new_disks[1]; /* new_raid_disks - old_raid_disks makedev number */
568};
569
570struct imsm_update_reshape_migration {
571 enum imsm_update_type type;
572 int old_raid_disks;
573 int new_raid_disks;
574 /* fields for array migration changes
575 */
576 int subdev;
577 int new_level;
578 int new_layout;
4bba0439 579 int new_chunksize;
48c5303a 580
d195167d 581 int new_disks[1]; /* new_raid_disks - old_raid_disks makedev number */
78b10e66
N
582};
583
f3871fdc
AK
584struct imsm_update_size_change {
585 enum imsm_update_type type;
586 int subdev;
587 long long new_size;
588};
589
2d40f3a1
AK
590struct imsm_update_general_migration_checkpoint {
591 enum imsm_update_type type;
4036e7ee 592 __u64 curr_migr_unit;
2d40f3a1
AK
593};
594
54c2c1ea
DW
595struct disk_info {
596 __u8 serial[MAX_RAID_SERIAL_LEN];
597};
598
8273f55e
DW
599struct imsm_update_create_array {
600 enum imsm_update_type type;
8273f55e 601 int dev_idx;
6a3e913e 602 struct imsm_dev dev;
8273f55e
DW
603};
604
33414a01
DW
605struct imsm_update_kill_array {
606 enum imsm_update_type type;
607 int dev_idx;
608};
609
aa534678
DW
610struct imsm_update_rename_array {
611 enum imsm_update_type type;
612 __u8 name[MAX_RAID_SERIAL_LEN];
613 int dev_idx;
614};
615
1a64be56 616struct imsm_update_add_remove_disk {
43dad3d6
DW
617 enum imsm_update_type type;
618};
619
bbab0940
TM
620struct imsm_update_prealloc_bb_mem {
621 enum imsm_update_type type;
622};
623
e6e9dd3f
AP
624struct imsm_update_rwh_policy {
625 enum imsm_update_type type;
626 int new_policy;
627 int dev_idx;
628};
629
88654014
LM
630static const char *_sys_dev_type[] = {
631 [SYS_DEV_UNKNOWN] = "Unknown",
632 [SYS_DEV_SAS] = "SAS",
614902f6 633 [SYS_DEV_SATA] = "SATA",
60f0f54d 634 [SYS_DEV_NVME] = "NVMe",
75350d87
KF
635 [SYS_DEV_VMD] = "VMD",
636 [SYS_DEV_SATA_VMD] = "SATA VMD"
88654014
LM
637};
638
420dafcd
N
639static int no_platform = -1;
640
641static int check_no_platform(void)
642{
643 static const char search[] = "mdadm.imsm.test=1";
644 FILE *fp;
645
646 if (no_platform >= 0)
647 return no_platform;
648
649 if (check_env("IMSM_NO_PLATFORM")) {
650 no_platform = 1;
651 return 1;
652 }
653 fp = fopen("/proc/cmdline", "r");
654 if (fp) {
655 char *l = conf_line(fp);
656 char *w = l;
657
cf1577bf
MG
658 if (l == NULL) {
659 fclose(fp);
660 return 0;
661 }
662
420dafcd
N
663 do {
664 if (strcmp(w, search) == 0)
665 no_platform = 1;
666 w = dl_next(w);
667 } while (w != l);
668 free_line(l);
669 fclose(fp);
670 if (no_platform >= 0)
671 return no_platform;
672 }
673 no_platform = 0;
674 return 0;
675}
676
677void imsm_set_no_platform(int v)
678{
679 no_platform = v;
680}
681
88654014
LM
682const char *get_sys_dev_type(enum sys_dev_type type)
683{
684 if (type >= SYS_DEV_MAX)
685 type = SYS_DEV_UNKNOWN;
686
687 return _sys_dev_type[type];
688}
689
690static struct intel_hba * alloc_intel_hba(struct sys_dev *device)
691{
503975b9
N
692 struct intel_hba *result = xmalloc(sizeof(*result));
693
694 result->type = device->type;
695 result->path = xstrdup(device->path);
696 result->next = NULL;
697 if (result->path && (result->pci_id = strrchr(result->path, '/')) != NULL)
698 result->pci_id++;
699
88654014
LM
700 return result;
701}
702
703static struct intel_hba * find_intel_hba(struct intel_hba *hba, struct sys_dev *device)
704{
594dc1b8
JS
705 struct intel_hba *result;
706
88654014
LM
707 for (result = hba; result; result = result->next) {
708 if (result->type == device->type && strcmp(result->path, device->path) == 0)
709 break;
710 }
711 return result;
712}
713
b4cf4cba 714static int attach_hba_to_super(struct intel_super *super, struct sys_dev *device)
88654014
LM
715{
716 struct intel_hba *hba;
717
718 /* check if disk attached to Intel HBA */
719 hba = find_intel_hba(super->hba, device);
720 if (hba != NULL)
721 return 1;
722 /* Check if HBA is already attached to super */
723 if (super->hba == NULL) {
724 super->hba = alloc_intel_hba(device);
725 return 1;
6b781d33
AP
726 }
727
728 hba = super->hba;
729 /* Intel metadata allows for all disks attached to the same type HBA.
614902f6 730 * Do not support HBA types mixing
6b781d33
AP
731 */
732 if (device->type != hba->type)
88654014 733 return 2;
6b781d33
AP
734
735 /* Multiple same type HBAs can be used if they share the same OROM */
736 const struct imsm_orom *device_orom = get_orom_by_device_id(device->dev_id);
737
738 if (device_orom != super->orom)
739 return 2;
740
741 while (hba->next)
742 hba = hba->next;
743
744 hba->next = alloc_intel_hba(device);
745 return 1;
88654014
LM
746}
747
748static struct sys_dev* find_disk_attached_hba(int fd, const char *devname)
749{
9bc4ae77 750 struct sys_dev *list, *elem;
88654014
LM
751 char *disk_path;
752
753 if ((list = find_intel_devices()) == NULL)
754 return 0;
755
4389ce73 756 if (!is_fd_valid(fd))
88654014
LM
757 disk_path = (char *) devname;
758 else
7c798f87 759 disk_path = diskfd_to_devpath(fd, 1, NULL);
88654014 760
9bc4ae77 761 if (!disk_path)
88654014 762 return 0;
88654014 763
9bc4ae77
N
764 for (elem = list; elem; elem = elem->next)
765 if (path_attached_to_hba(disk_path, elem->path))
5d2434d1 766 break;
9bc4ae77 767
88654014
LM
768 if (disk_path != devname)
769 free(disk_path);
88654014 770
5d2434d1 771 return elem;
88654014
LM
772}
773
d424212e
N
774static int find_intel_hba_capability(int fd, struct intel_super *super,
775 char *devname);
f2f5c343 776
cdddbdbc
DW
777static struct supertype *match_metadata_desc_imsm(char *arg)
778{
779 struct supertype *st;
780
781 if (strcmp(arg, "imsm") != 0 &&
782 strcmp(arg, "default") != 0
783 )
784 return NULL;
785
503975b9 786 st = xcalloc(1, sizeof(*st));
cdddbdbc
DW
787 st->ss = &super_imsm;
788 st->max_devs = IMSM_MAX_DEVICES;
789 st->minor_version = 0;
790 st->sb = NULL;
791 return st;
792}
793
cdddbdbc
DW
794static __u8 *get_imsm_version(struct imsm_super *mpb)
795{
796 return &mpb->sig[MPB_SIG_LEN];
797}
798
949c47a0
DW
799/* retrieve a disk directly from the anchor when the anchor is known to be
800 * up-to-date, currently only at load time
801 */
802static struct imsm_disk *__get_imsm_disk(struct imsm_super *mpb, __u8 index)
cdddbdbc 803{
949c47a0 804 if (index >= mpb->num_disks)
cdddbdbc
DW
805 return NULL;
806 return &mpb->disk[index];
807}
808
95d07a2c
LM
809/* retrieve the disk description based on a index of the disk
810 * in the sub-array
811 */
812static struct dl *get_imsm_dl_disk(struct intel_super *super, __u8 index)
949c47a0 813{
b9f594fe
DW
814 struct dl *d;
815
816 for (d = super->disks; d; d = d->next)
817 if (d->index == index)
95d07a2c
LM
818 return d;
819
820 return NULL;
821}
822/* retrieve a disk from the parsed metadata */
823static struct imsm_disk *get_imsm_disk(struct intel_super *super, __u8 index)
824{
825 struct dl *dl;
826
827 dl = get_imsm_dl_disk(super, index);
828 if (dl)
829 return &dl->disk;
830
b9f594fe 831 return NULL;
949c47a0
DW
832}
833
834/* generate a checksum directly from the anchor when the anchor is known to be
835 * up-to-date, currently only at load or write_super after coalescing
836 */
837static __u32 __gen_imsm_checksum(struct imsm_super *mpb)
cdddbdbc
DW
838{
839 __u32 end = mpb->mpb_size / sizeof(end);
840 __u32 *p = (__u32 *) mpb;
841 __u32 sum = 0;
842
5d500228
N
843 while (end--) {
844 sum += __le32_to_cpu(*p);
97f734fd
N
845 p++;
846 }
cdddbdbc 847
5d500228 848 return sum - __le32_to_cpu(mpb->check_sum);
cdddbdbc
DW
849}
850
a965f303
DW
851static size_t sizeof_imsm_map(struct imsm_map *map)
852{
853 return sizeof(struct imsm_map) + sizeof(__u32) * (map->num_members - 1);
854}
855
856struct imsm_map *get_imsm_map(struct imsm_dev *dev, int second_map)
cdddbdbc 857{
5e7b0330
AK
858 /* A device can have 2 maps if it is in the middle of a migration.
859 * If second_map is:
238c0a71
AK
860 * MAP_0 - we return the first map
861 * MAP_1 - we return the second map if it exists, else NULL
862 * MAP_X - we return the second map if it exists, else the first
5e7b0330 863 */
a965f303 864 struct imsm_map *map = &dev->vol.map[0];
9535fc47 865 struct imsm_map *map2 = NULL;
a965f303 866
9535fc47
AK
867 if (dev->vol.migr_state)
868 map2 = (void *)map + sizeof_imsm_map(map);
a965f303 869
9535fc47 870 switch (second_map) {
3b451610 871 case MAP_0:
9535fc47 872 break;
3b451610 873 case MAP_1:
9535fc47
AK
874 map = map2;
875 break;
238c0a71 876 case MAP_X:
9535fc47
AK
877 if (map2)
878 map = map2;
879 break;
9535fc47
AK
880 default:
881 map = NULL;
882 }
883 return map;
5e7b0330 884
a965f303 885}
cdddbdbc 886
3393c6af
DW
887/* return the size of the device.
888 * migr_state increases the returned size if map[0] were to be duplicated
889 */
890static size_t sizeof_imsm_dev(struct imsm_dev *dev, int migr_state)
a965f303
DW
891{
892 size_t size = sizeof(*dev) - sizeof(struct imsm_map) +
238c0a71 893 sizeof_imsm_map(get_imsm_map(dev, MAP_0));
cdddbdbc
DW
894
895 /* migrating means an additional map */
a965f303 896 if (dev->vol.migr_state)
238c0a71 897 size += sizeof_imsm_map(get_imsm_map(dev, MAP_1));
3393c6af 898 else if (migr_state)
238c0a71 899 size += sizeof_imsm_map(get_imsm_map(dev, MAP_0));
cdddbdbc
DW
900
901 return size;
902}
903
54c2c1ea
DW
904/* retrieve disk serial number list from a metadata update */
905static struct disk_info *get_disk_info(struct imsm_update_create_array *update)
906{
907 void *u = update;
908 struct disk_info *inf;
909
910 inf = u + sizeof(*update) - sizeof(struct imsm_dev) +
911 sizeof_imsm_dev(&update->dev, 0);
912
913 return inf;
914}
54c2c1ea 915
756a15f3
MG
916/**
917 * __get_imsm_dev() - Get device with index from imsm_super.
918 * @mpb: &imsm_super pointer, not NULL.
919 * @index: Device index.
920 *
921 * Function works as non-NULL, aborting in such a case,
922 * when NULL would be returned.
923 *
924 * Device index should be in range 0 up to num_raid_devs.
925 * Function assumes the index was already verified.
926 * Index must be valid, otherwise abort() is called.
927 *
928 * Return: Pointer to corresponding imsm_dev.
929 *
930 */
949c47a0 931static struct imsm_dev *__get_imsm_dev(struct imsm_super *mpb, __u8 index)
cdddbdbc
DW
932{
933 int offset;
934 int i;
935 void *_mpb = mpb;
936
949c47a0 937 if (index >= mpb->num_raid_devs)
756a15f3 938 goto error;
cdddbdbc
DW
939
940 /* devices start after all disks */
941 offset = ((void *) &mpb->disk[mpb->num_disks]) - _mpb;
942
756a15f3 943 for (i = 0; i <= index; i++, offset += sizeof_imsm_dev(_mpb + offset, 0))
cdddbdbc
DW
944 if (i == index)
945 return _mpb + offset;
756a15f3
MG
946error:
947 pr_err("cannot find imsm_dev with index %u in imsm_super\n", index);
948 abort();
cdddbdbc
DW
949}
950
756a15f3
MG
951/**
952 * get_imsm_dev() - Get device with index from intel_super.
953 * @super: &intel_super pointer, not NULL.
954 * @index: Device index.
955 *
956 * Function works as non-NULL, aborting in such a case,
957 * when NULL would be returned.
958 *
959 * Device index should be in range 0 up to num_raid_devs.
960 * Function assumes the index was already verified.
961 * Index must be valid, otherwise abort() is called.
962 *
963 * Return: Pointer to corresponding imsm_dev.
964 *
965 */
949c47a0
DW
966static struct imsm_dev *get_imsm_dev(struct intel_super *super, __u8 index)
967{
ba2de7ba
DW
968 struct intel_dev *dv;
969
949c47a0 970 if (index >= super->anchor->num_raid_devs)
756a15f3
MG
971 goto error;
972
ba2de7ba
DW
973 for (dv = super->devlist; dv; dv = dv->next)
974 if (dv->index == index)
975 return dv->dev;
756a15f3
MG
976error:
977 pr_err("cannot find imsm_dev with index %u in intel_super\n", index);
978 abort();
949c47a0
DW
979}
980
8d67477f
TM
981static inline unsigned long long __le48_to_cpu(const struct bbm_log_block_addr
982 *addr)
983{
984 return ((((__u64)__le32_to_cpu(addr->dw1)) << 16) |
985 __le16_to_cpu(addr->w1));
986}
987
988static inline struct bbm_log_block_addr __cpu_to_le48(unsigned long long sec)
989{
990 struct bbm_log_block_addr addr;
991
992 addr.w1 = __cpu_to_le16((__u16)(sec & 0xffff));
993 addr.dw1 = __cpu_to_le32((__u32)(sec >> 16) & 0xffffffff);
994 return addr;
995}
996
8d67477f
TM
997/* get size of the bbm log */
998static __u32 get_imsm_bbm_log_size(struct bbm_log *log)
999{
1000 if (!log || log->entry_count == 0)
1001 return 0;
1002
1003 return sizeof(log->signature) +
1004 sizeof(log->entry_count) +
1005 log->entry_count * sizeof(struct bbm_log_entry);
1006}
6f50473f
TM
1007
1008/* check if bad block is not partially stored in bbm log */
1009static int is_stored_in_bbm(struct bbm_log *log, const __u8 idx, const unsigned
1010 long long sector, const int length, __u32 *pos)
1011{
1012 __u32 i;
1013
1014 for (i = *pos; i < log->entry_count; i++) {
1015 struct bbm_log_entry *entry = &log->marked_block_entries[i];
1016 unsigned long long bb_start;
1017 unsigned long long bb_end;
1018
1019 bb_start = __le48_to_cpu(&entry->defective_block_start);
1020 bb_end = bb_start + (entry->marked_count + 1);
1021
1022 if ((entry->disk_ordinal == idx) && (bb_start >= sector) &&
1023 (bb_end <= sector + length)) {
1024 *pos = i;
1025 return 1;
1026 }
1027 }
1028 return 0;
1029}
1030
1031/* record new bad block in bbm log */
1032static int record_new_badblock(struct bbm_log *log, const __u8 idx, unsigned
1033 long long sector, int length)
1034{
1035 int new_bb = 0;
1036 __u32 pos = 0;
1037 struct bbm_log_entry *entry = NULL;
1038
1039 while (is_stored_in_bbm(log, idx, sector, length, &pos)) {
1040 struct bbm_log_entry *e = &log->marked_block_entries[pos];
1041
1042 if ((e->marked_count + 1 == BBM_LOG_MAX_LBA_ENTRY_VAL) &&
1043 (__le48_to_cpu(&e->defective_block_start) == sector)) {
1044 sector += BBM_LOG_MAX_LBA_ENTRY_VAL;
1045 length -= BBM_LOG_MAX_LBA_ENTRY_VAL;
1046 pos = pos + 1;
1047 continue;
1048 }
1049 entry = e;
1050 break;
1051 }
1052
1053 if (entry) {
1054 int cnt = (length <= BBM_LOG_MAX_LBA_ENTRY_VAL) ? length :
1055 BBM_LOG_MAX_LBA_ENTRY_VAL;
1056 entry->defective_block_start = __cpu_to_le48(sector);
1057 entry->marked_count = cnt - 1;
1058 if (cnt == length)
1059 return 1;
1060 sector += cnt;
1061 length -= cnt;
1062 }
1063
1064 new_bb = ROUND_UP(length, BBM_LOG_MAX_LBA_ENTRY_VAL) /
1065 BBM_LOG_MAX_LBA_ENTRY_VAL;
1066 if (log->entry_count + new_bb > BBM_LOG_MAX_ENTRIES)
1067 return 0;
1068
1069 while (length > 0) {
1070 int cnt = (length <= BBM_LOG_MAX_LBA_ENTRY_VAL) ? length :
1071 BBM_LOG_MAX_LBA_ENTRY_VAL;
1072 struct bbm_log_entry *entry =
1073 &log->marked_block_entries[log->entry_count];
1074
1075 entry->defective_block_start = __cpu_to_le48(sector);
1076 entry->marked_count = cnt - 1;
1077 entry->disk_ordinal = idx;
1078
1079 sector += cnt;
1080 length -= cnt;
1081
1082 log->entry_count++;
1083 }
1084
1085 return new_bb;
1086}
c07a5a4f 1087
4c9e8c1e
TM
1088/* clear all bad blocks for given disk */
1089static void clear_disk_badblocks(struct bbm_log *log, const __u8 idx)
1090{
1091 __u32 i = 0;
1092
1093 while (i < log->entry_count) {
1094 struct bbm_log_entry *entries = log->marked_block_entries;
1095
1096 if (entries[i].disk_ordinal == idx) {
1097 if (i < log->entry_count - 1)
1098 entries[i] = entries[log->entry_count - 1];
1099 log->entry_count--;
1100 } else {
1101 i++;
1102 }
1103 }
1104}
1105
c07a5a4f
TM
1106/* clear given bad block */
1107static int clear_badblock(struct bbm_log *log, const __u8 idx, const unsigned
1108 long long sector, const int length) {
1109 __u32 i = 0;
1110
1111 while (i < log->entry_count) {
1112 struct bbm_log_entry *entries = log->marked_block_entries;
1113
1114 if ((entries[i].disk_ordinal == idx) &&
1115 (__le48_to_cpu(&entries[i].defective_block_start) ==
1116 sector) && (entries[i].marked_count + 1 == length)) {
1117 if (i < log->entry_count - 1)
1118 entries[i] = entries[log->entry_count - 1];
1119 log->entry_count--;
1120 break;
1121 }
1122 i++;
1123 }
1124
1125 return 1;
1126}
8d67477f
TM
1127
1128/* allocate and load BBM log from metadata */
1129static int load_bbm_log(struct intel_super *super)
1130{
1131 struct imsm_super *mpb = super->anchor;
1132 __u32 bbm_log_size = __le32_to_cpu(mpb->bbm_log_size);
1133
1134 super->bbm_log = xcalloc(1, sizeof(struct bbm_log));
1135 if (!super->bbm_log)
1136 return 1;
1137
1138 if (bbm_log_size) {
1139 struct bbm_log *log = (void *)mpb +
1140 __le32_to_cpu(mpb->mpb_size) - bbm_log_size;
1141
1142 __u32 entry_count;
1143
1144 if (bbm_log_size < sizeof(log->signature) +
1145 sizeof(log->entry_count))
1146 return 2;
1147
1148 entry_count = __le32_to_cpu(log->entry_count);
1149 if ((__le32_to_cpu(log->signature) != BBM_LOG_SIGNATURE) ||
1150 (entry_count > BBM_LOG_MAX_ENTRIES))
1151 return 3;
1152
1153 if (bbm_log_size !=
1154 sizeof(log->signature) + sizeof(log->entry_count) +
1155 entry_count * sizeof(struct bbm_log_entry))
1156 return 4;
1157
1158 memcpy(super->bbm_log, log, bbm_log_size);
1159 } else {
1160 super->bbm_log->signature = __cpu_to_le32(BBM_LOG_SIGNATURE);
1161 super->bbm_log->entry_count = 0;
1162 }
1163
1164 return 0;
1165}
1166
b12796be
TM
1167/* checks if bad block is within volume boundaries */
1168static int is_bad_block_in_volume(const struct bbm_log_entry *entry,
1169 const unsigned long long start_sector,
1170 const unsigned long long size)
1171{
1172 unsigned long long bb_start;
1173 unsigned long long bb_end;
1174
1175 bb_start = __le48_to_cpu(&entry->defective_block_start);
1176 bb_end = bb_start + (entry->marked_count + 1);
1177
1178 if (((bb_start >= start_sector) && (bb_start < start_sector + size)) ||
1179 ((bb_end >= start_sector) && (bb_end <= start_sector + size)))
1180 return 1;
1181
1182 return 0;
1183}
1184
1185/* get list of bad blocks on a drive for a volume */
1186static void get_volume_badblocks(const struct bbm_log *log, const __u8 idx,
1187 const unsigned long long start_sector,
1188 const unsigned long long size,
1189 struct md_bb *bbs)
1190{
1191 __u32 count = 0;
1192 __u32 i;
1193
1194 for (i = 0; i < log->entry_count; i++) {
1195 const struct bbm_log_entry *ent =
1196 &log->marked_block_entries[i];
1197 struct md_bb_entry *bb;
1198
1199 if ((ent->disk_ordinal == idx) &&
1200 is_bad_block_in_volume(ent, start_sector, size)) {
1201
1202 if (!bbs->entries) {
1203 bbs->entries = xmalloc(BBM_LOG_MAX_ENTRIES *
1204 sizeof(*bb));
1205 if (!bbs->entries)
1206 break;
1207 }
1208
1209 bb = &bbs->entries[count++];
1210 bb->sector = __le48_to_cpu(&ent->defective_block_start);
1211 bb->length = ent->marked_count + 1;
1212 }
1213 }
1214 bbs->count = count;
1215}
1216
98130f40
AK
1217/*
1218 * for second_map:
238c0a71
AK
1219 * == MAP_0 get first map
1220 * == MAP_1 get second map
1221 * == MAP_X than get map according to the current migr_state
98130f40
AK
1222 */
1223static __u32 get_imsm_ord_tbl_ent(struct imsm_dev *dev,
1224 int slot,
1225 int second_map)
7eef0453
DW
1226{
1227 struct imsm_map *map;
1228
5e7b0330 1229 map = get_imsm_map(dev, second_map);
7eef0453 1230
ff077194
DW
1231 /* top byte identifies disk under rebuild */
1232 return __le32_to_cpu(map->disk_ord_tbl[slot]);
1233}
1234
1235#define ord_to_idx(ord) (((ord) << 8) >> 8)
98130f40 1236static __u32 get_imsm_disk_idx(struct imsm_dev *dev, int slot, int second_map)
ff077194 1237{
98130f40 1238 __u32 ord = get_imsm_ord_tbl_ent(dev, slot, second_map);
ff077194
DW
1239
1240 return ord_to_idx(ord);
7eef0453
DW
1241}
1242
be73972f
DW
1243static void set_imsm_ord_tbl_ent(struct imsm_map *map, int slot, __u32 ord)
1244{
1245 map->disk_ord_tbl[slot] = __cpu_to_le32(ord);
1246}
1247
76c152ca 1248static int get_imsm_disk_slot(struct imsm_map *map, const unsigned int idx)
620b1713
DW
1249{
1250 int slot;
1251 __u32 ord;
1252
1253 for (slot = 0; slot < map->num_members; slot++) {
1254 ord = __le32_to_cpu(map->disk_ord_tbl[slot]);
1255 if (ord_to_idx(ord) == idx)
1256 return slot;
1257 }
1258
76c152ca 1259 return IMSM_STATUS_ERROR;
620b1713
DW
1260}
1261
cdddbdbc
DW
1262static int get_imsm_raid_level(struct imsm_map *map)
1263{
1264 if (map->raid_level == 1) {
1265 if (map->num_members == 2)
1266 return 1;
1267 else
1268 return 10;
1269 }
1270
1271 return map->raid_level;
1272}
1273
76c152ca
MT
1274/**
1275 * get_disk_slot_in_dev() - retrieve disk slot from &imsm_dev.
1276 * @super: &intel_super pointer, not NULL.
1277 * @dev_idx: imsm device index.
1278 * @idx: disk index.
1279 *
1280 * Return: Slot on success, IMSM_STATUS_ERROR otherwise.
1281 */
1282static int get_disk_slot_in_dev(struct intel_super *super, const __u8 dev_idx,
1283 const unsigned int idx)
1284{
1285 struct imsm_dev *dev = get_imsm_dev(super, dev_idx);
1286 struct imsm_map *map = get_imsm_map(dev, MAP_0);
1287
1288 return get_imsm_disk_slot(map, idx);
1289}
1290
c2c087e6
DW
1291static int cmp_extent(const void *av, const void *bv)
1292{
1293 const struct extent *a = av;
1294 const struct extent *b = bv;
1295 if (a->start < b->start)
1296 return -1;
1297 if (a->start > b->start)
1298 return 1;
1299 return 0;
1300}
1301
0dcecb2e 1302static int count_memberships(struct dl *dl, struct intel_super *super)
c2c087e6 1303{
c2c087e6 1304 int memberships = 0;
620b1713 1305 int i;
c2c087e6 1306
76c152ca
MT
1307 for (i = 0; i < super->anchor->num_raid_devs; i++)
1308 if (get_disk_slot_in_dev(super, i, dl->index) >= 0)
620b1713 1309 memberships++;
0dcecb2e
DW
1310
1311 return memberships;
1312}
1313
b81221b7
CA
1314static __u32 imsm_min_reserved_sectors(struct intel_super *super);
1315
486720e0 1316static int split_ull(unsigned long long n, void *lo, void *hi)
5551b113
CA
1317{
1318 if (lo == 0 || hi == 0)
1319 return 1;
486720e0
JS
1320 __put_unaligned32(__cpu_to_le32((__u32)n), lo);
1321 __put_unaligned32(__cpu_to_le32((n >> 32)), hi);
5551b113
CA
1322 return 0;
1323}
1324
1325static unsigned long long join_u32(__u32 lo, __u32 hi)
1326{
1327 return (unsigned long long)__le32_to_cpu(lo) |
1328 (((unsigned long long)__le32_to_cpu(hi)) << 32);
1329}
1330
1331static unsigned long long total_blocks(struct imsm_disk *disk)
1332{
1333 if (disk == NULL)
1334 return 0;
1335 return join_u32(disk->total_blocks_lo, disk->total_blocks_hi);
1336}
1337
1c275381
MT
1338/**
1339 * imsm_num_data_members() - get data drives count for an array.
1340 * @map: Map to analyze.
1341 *
1342 * num_data_members value represents minimal count of drives for level.
1343 * The name of the property could be misleading for RAID5 with asymmetric layout
1344 * because some data required to be calculated from parity.
1345 * The property is extracted from level and num_members value.
1346 *
1347 * Return: num_data_members value on success, zero otherwise.
1348 */
1349static __u8 imsm_num_data_members(struct imsm_map *map)
1350{
1351 switch (get_imsm_raid_level(map)) {
1352 case 0:
1353 return map->num_members;
1354 case 1:
1355 case 10:
1356 return map->num_members / 2;
1357 case 5:
1358 return map->num_members - 1;
1359 default:
1360 dprintf("unsupported raid level\n");
1361 return 0;
1362 }
1363}
1364
5551b113
CA
1365static unsigned long long pba_of_lba0(struct imsm_map *map)
1366{
1367 if (map == NULL)
1368 return 0;
1369 return join_u32(map->pba_of_lba0_lo, map->pba_of_lba0_hi);
1370}
1371
1372static unsigned long long blocks_per_member(struct imsm_map *map)
1373{
1374 if (map == NULL)
1375 return 0;
1376 return join_u32(map->blocks_per_member_lo, map->blocks_per_member_hi);
1377}
1378
1379static unsigned long long num_data_stripes(struct imsm_map *map)
1380{
1381 if (map == NULL)
1382 return 0;
1383 return join_u32(map->num_data_stripes_lo, map->num_data_stripes_hi);
1384}
1385
4036e7ee
MT
1386static unsigned long long vol_curr_migr_unit(struct imsm_dev *dev)
1387{
1388 if (dev == NULL)
1389 return 0;
1390
1391 return join_u32(dev->vol.curr_migr_unit_lo, dev->vol.curr_migr_unit_hi);
1392}
1393
fcc2c9da
MD
1394static unsigned long long imsm_dev_size(struct imsm_dev *dev)
1395{
1396 if (dev == NULL)
1397 return 0;
1398 return join_u32(dev->size_low, dev->size_high);
1399}
1400
9f421827
PB
1401static unsigned long long migr_chkp_area_pba(struct migr_record *migr_rec)
1402{
1403 if (migr_rec == NULL)
1404 return 0;
1405 return join_u32(migr_rec->ckpt_area_pba_lo,
1406 migr_rec->ckpt_area_pba_hi);
1407}
1408
1409static unsigned long long current_migr_unit(struct migr_record *migr_rec)
1410{
1411 if (migr_rec == NULL)
1412 return 0;
1413 return join_u32(migr_rec->curr_migr_unit_lo,
1414 migr_rec->curr_migr_unit_hi);
1415}
1416
1417static unsigned long long migr_dest_1st_member_lba(struct migr_record *migr_rec)
1418{
1419 if (migr_rec == NULL)
1420 return 0;
1421 return join_u32(migr_rec->dest_1st_member_lba_lo,
1422 migr_rec->dest_1st_member_lba_hi);
1423}
1424
1425static unsigned long long get_num_migr_units(struct migr_record *migr_rec)
1426{
1427 if (migr_rec == NULL)
1428 return 0;
1429 return join_u32(migr_rec->num_migr_units_lo,
1430 migr_rec->num_migr_units_hi);
1431}
1432
5551b113
CA
1433static void set_total_blocks(struct imsm_disk *disk, unsigned long long n)
1434{
1435 split_ull(n, &disk->total_blocks_lo, &disk->total_blocks_hi);
1436}
1437
1c275381
MT
1438/**
1439 * set_num_domains() - Set number of domains for an array.
1440 * @map: Map to be updated.
1441 *
1442 * num_domains property represents copies count of each data drive, thus make
1443 * it meaningful only for RAID1 and RAID10. IMSM supports two domains for
1444 * raid1 and raid10.
1445 */
1446static void set_num_domains(struct imsm_map *map)
1447{
1448 int level = get_imsm_raid_level(map);
1449
1450 if (level == 1 || level == 10)
1451 map->num_domains = 2;
1452 else
1453 map->num_domains = 1;
1454}
1455
5551b113
CA
1456static void set_pba_of_lba0(struct imsm_map *map, unsigned long long n)
1457{
1458 split_ull(n, &map->pba_of_lba0_lo, &map->pba_of_lba0_hi);
1459}
1460
1461static void set_blocks_per_member(struct imsm_map *map, unsigned long long n)
1462{
1463 split_ull(n, &map->blocks_per_member_lo, &map->blocks_per_member_hi);
1464}
1465
1466static void set_num_data_stripes(struct imsm_map *map, unsigned long long n)
1467{
1468 split_ull(n, &map->num_data_stripes_lo, &map->num_data_stripes_hi);
1469}
1470
1c275381
MT
1471/**
1472 * update_num_data_stripes() - Calculate and update num_data_stripes value.
1473 * @map: map to be updated.
1474 * @dev_size: size of volume.
1475 *
1476 * num_data_stripes value is addictionally divided by num_domains, therefore for
1477 * levels where num_domains is not 1, nds is a part of real value.
1478 */
1479static void update_num_data_stripes(struct imsm_map *map,
1480 unsigned long long dev_size)
1481{
1482 unsigned long long nds = dev_size / imsm_num_data_members(map);
1483
1484 nds /= map->num_domains;
1485 nds /= map->blocks_per_strip;
1486 set_num_data_stripes(map, nds);
1487}
1488
4036e7ee
MT
1489static void set_vol_curr_migr_unit(struct imsm_dev *dev, unsigned long long n)
1490{
1491 if (dev == NULL)
1492 return;
1493
1494 split_ull(n, &dev->vol.curr_migr_unit_lo, &dev->vol.curr_migr_unit_hi);
1495}
1496
fcc2c9da
MD
1497static void set_imsm_dev_size(struct imsm_dev *dev, unsigned long long n)
1498{
1499 split_ull(n, &dev->size_low, &dev->size_high);
1500}
1501
9f421827
PB
1502static void set_migr_chkp_area_pba(struct migr_record *migr_rec,
1503 unsigned long long n)
1504{
1505 split_ull(n, &migr_rec->ckpt_area_pba_lo, &migr_rec->ckpt_area_pba_hi);
1506}
1507
1508static void set_current_migr_unit(struct migr_record *migr_rec,
1509 unsigned long long n)
1510{
1511 split_ull(n, &migr_rec->curr_migr_unit_lo,
1512 &migr_rec->curr_migr_unit_hi);
1513}
1514
1515static void set_migr_dest_1st_member_lba(struct migr_record *migr_rec,
1516 unsigned long long n)
1517{
1518 split_ull(n, &migr_rec->dest_1st_member_lba_lo,
1519 &migr_rec->dest_1st_member_lba_hi);
1520}
1521
1522static void set_num_migr_units(struct migr_record *migr_rec,
1523 unsigned long long n)
1524{
1525 split_ull(n, &migr_rec->num_migr_units_lo,
1526 &migr_rec->num_migr_units_hi);
1527}
1528
44490938
MD
1529static unsigned long long per_dev_array_size(struct imsm_map *map)
1530{
1531 unsigned long long array_size = 0;
1532
1533 if (map == NULL)
1534 return array_size;
1535
1536 array_size = num_data_stripes(map) * map->blocks_per_strip;
1537 if (get_imsm_raid_level(map) == 1 || get_imsm_raid_level(map) == 10)
1538 array_size *= 2;
1539
1540 return array_size;
1541}
1542
05501181
PB
1543static struct extent *get_extents(struct intel_super *super, struct dl *dl,
1544 int get_minimal_reservation)
0dcecb2e
DW
1545{
1546 /* find a list of used extents on the given physical device */
0dcecb2e 1547 int memberships = count_memberships(dl, super);
aa19fdd4
MT
1548 struct extent *rv = xcalloc(memberships + 1, sizeof(struct extent));
1549 struct extent *e = rv;
1550 int i;
b276dd33
DW
1551 __u32 reservation;
1552
1553 /* trim the reserved area for spares, so they can join any array
1554 * regardless of whether the OROM has assigned sectors from the
1555 * IMSM_RESERVED_SECTORS region
1556 */
05501181 1557 if (dl->index == -1 || get_minimal_reservation)
b81221b7 1558 reservation = imsm_min_reserved_sectors(super);
b276dd33
DW
1559 else
1560 reservation = MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS;
0dcecb2e 1561
949c47a0
DW
1562 for (i = 0; i < super->anchor->num_raid_devs; i++) {
1563 struct imsm_dev *dev = get_imsm_dev(super, i);
238c0a71 1564 struct imsm_map *map = get_imsm_map(dev, MAP_0);
c2c087e6 1565
620b1713 1566 if (get_imsm_disk_slot(map, dl->index) >= 0) {
5551b113 1567 e->start = pba_of_lba0(map);
44490938 1568 e->size = per_dev_array_size(map);
aa19fdd4 1569 e->vol = i;
620b1713 1570 e++;
c2c087e6
DW
1571 }
1572 }
1573 qsort(rv, memberships, sizeof(*rv), cmp_extent);
1574
1011e834 1575 /* determine the start of the metadata
14e8215b
DW
1576 * when no raid devices are defined use the default
1577 * ...otherwise allow the metadata to truncate the value
1578 * as is the case with older versions of imsm
1579 */
1580 if (memberships) {
1581 struct extent *last = &rv[memberships - 1];
5551b113 1582 unsigned long long remainder;
14e8215b 1583
5551b113 1584 remainder = total_blocks(&dl->disk) - (last->start + last->size);
dda5855f
DW
1585 /* round down to 1k block to satisfy precision of the kernel
1586 * 'size' interface
1587 */
1588 remainder &= ~1UL;
1589 /* make sure remainder is still sane */
f21e18ca 1590 if (remainder < (unsigned)ROUND_UP(super->len, 512) >> 9)
dda5855f 1591 remainder = ROUND_UP(super->len, 512) >> 9;
14e8215b
DW
1592 if (reservation > remainder)
1593 reservation = remainder;
1594 }
5551b113 1595 e->start = total_blocks(&dl->disk) - reservation;
c2c087e6
DW
1596 e->size = 0;
1597 return rv;
1598}
1599
14e8215b
DW
1600/* try to determine how much space is reserved for metadata from
1601 * the last get_extents() entry, otherwise fallback to the
1602 * default
1603 */
1604static __u32 imsm_reserved_sectors(struct intel_super *super, struct dl *dl)
1605{
1606 struct extent *e;
1607 int i;
1608 __u32 rv;
1609
1610 /* for spares just return a minimal reservation which will grow
1611 * once the spare is picked up by an array
1612 */
1613 if (dl->index == -1)
1614 return MPB_SECTOR_CNT;
1615
05501181 1616 e = get_extents(super, dl, 0);
14e8215b
DW
1617 if (!e)
1618 return MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS;
1619
1620 /* scroll to last entry */
1621 for (i = 0; e[i].size; i++)
1622 continue;
1623
5551b113 1624 rv = total_blocks(&dl->disk) - e[i].start;
14e8215b
DW
1625
1626 free(e);
1627
1628 return rv;
1629}
1630
25ed7e59
DW
1631static int is_spare(struct imsm_disk *disk)
1632{
1633 return (disk->status & SPARE_DISK) == SPARE_DISK;
1634}
1635
1636static int is_configured(struct imsm_disk *disk)
1637{
1638 return (disk->status & CONFIGURED_DISK) == CONFIGURED_DISK;
1639}
1640
1641static int is_failed(struct imsm_disk *disk)
1642{
1643 return (disk->status & FAILED_DISK) == FAILED_DISK;
1644}
1645
2432ce9b
AP
1646static int is_journal(struct imsm_disk *disk)
1647{
1648 return (disk->status & JOURNAL_DISK) == JOURNAL_DISK;
1649}
1650
78c8028b
MT
1651/**
1652 * round_member_size_to_mb()- Round given size to closest MiB.
1653 * @size: size to round in sectors.
b53bfba6 1654 */
78c8028b 1655static inline unsigned long long round_member_size_to_mb(unsigned long long size)
b53bfba6 1656{
78c8028b
MT
1657 return (size >> SECT_PER_MB_SHIFT) << SECT_PER_MB_SHIFT;
1658}
b53bfba6 1659
78c8028b
MT
1660/**
1661 * round_size_to_mb()- Round given size.
1662 * @array_size: size to round in sectors.
1663 * @disk_count: count of data members.
1664 *
1665 * Get size per each data member and round it to closest MiB to ensure that data
1666 * splits evenly between members.
1667 *
1668 * Return: Array size, rounded down.
1669 */
1670static inline unsigned long long round_size_to_mb(unsigned long long array_size,
1671 unsigned int disk_count)
1672{
1673 return round_member_size_to_mb(array_size / disk_count) * disk_count;
b53bfba6
TM
1674}
1675
8b9cd157
MK
1676static int able_to_resync(int raid_level, int missing_disks)
1677{
1678 int max_missing_disks = 0;
1679
1680 switch (raid_level) {
1681 case 10:
1682 max_missing_disks = 1;
1683 break;
1684 default:
1685 max_missing_disks = 0;
1686 }
1687 return missing_disks <= max_missing_disks;
1688}
1689
b81221b7
CA
1690/* try to determine how much space is reserved for metadata from
1691 * the last get_extents() entry on the smallest active disk,
1692 * otherwise fallback to the default
1693 */
1694static __u32 imsm_min_reserved_sectors(struct intel_super *super)
1695{
1696 struct extent *e;
1697 int i;
5551b113
CA
1698 unsigned long long min_active;
1699 __u32 remainder;
b81221b7
CA
1700 __u32 rv = MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS;
1701 struct dl *dl, *dl_min = NULL;
1702
1703 if (!super)
1704 return rv;
1705
1706 min_active = 0;
1707 for (dl = super->disks; dl; dl = dl->next) {
1708 if (dl->index < 0)
1709 continue;
5551b113
CA
1710 unsigned long long blocks = total_blocks(&dl->disk);
1711 if (blocks < min_active || min_active == 0) {
b81221b7 1712 dl_min = dl;
5551b113 1713 min_active = blocks;
b81221b7
CA
1714 }
1715 }
1716 if (!dl_min)
1717 return rv;
1718
1719 /* find last lba used by subarrays on the smallest active disk */
05501181 1720 e = get_extents(super, dl_min, 0);
b81221b7
CA
1721 if (!e)
1722 return rv;
1723 for (i = 0; e[i].size; i++)
1724 continue;
1725
1726 remainder = min_active - e[i].start;
1727 free(e);
1728
1729 /* to give priority to recovery we should not require full
1730 IMSM_RESERVED_SECTORS from the spare */
1731 rv = MPB_SECTOR_CNT + NUM_BLOCKS_DIRTY_STRIPE_REGION;
1732
1733 /* if real reservation is smaller use that value */
1734 return (remainder < rv) ? remainder : rv;
1735}
1736
195d1d76 1737static bool is_gen_migration(struct imsm_dev *dev);
d1e02575 1738
f36a9ecd
PB
1739#define IMSM_4K_DIV 8
1740
c47b0ff6
AK
1741static __u64 blocks_per_migr_unit(struct intel_super *super,
1742 struct imsm_dev *dev);
1e5c6983 1743
c47b0ff6
AK
1744static void print_imsm_dev(struct intel_super *super,
1745 struct imsm_dev *dev,
1746 char *uuid,
1747 int disk_idx)
cdddbdbc
DW
1748{
1749 __u64 sz;
0d80bb2f 1750 int slot, i;
238c0a71
AK
1751 struct imsm_map *map = get_imsm_map(dev, MAP_0);
1752 struct imsm_map *map2 = get_imsm_map(dev, MAP_1);
b10b37b8 1753 __u32 ord;
cdddbdbc
DW
1754
1755 printf("\n");
1e7bc0ed 1756 printf("[%.16s]:\n", dev->volume);
ba1b3bc8 1757 printf(" Subarray : %d\n", super->current_vol);
44470971 1758 printf(" UUID : %s\n", uuid);
dd8bcb3b
AK
1759 printf(" RAID Level : %d", get_imsm_raid_level(map));
1760 if (map2)
1761 printf(" <-- %d", get_imsm_raid_level(map2));
1762 printf("\n");
1763 printf(" Members : %d", map->num_members);
1764 if (map2)
1765 printf(" <-- %d", map2->num_members);
1766 printf("\n");
0d80bb2f
DW
1767 printf(" Slots : [");
1768 for (i = 0; i < map->num_members; i++) {
238c0a71 1769 ord = get_imsm_ord_tbl_ent(dev, i, MAP_0);
0d80bb2f
DW
1770 printf("%s", ord & IMSM_ORD_REBUILD ? "_" : "U");
1771 }
dd8bcb3b
AK
1772 printf("]");
1773 if (map2) {
1774 printf(" <-- [");
1775 for (i = 0; i < map2->num_members; i++) {
238c0a71 1776 ord = get_imsm_ord_tbl_ent(dev, i, MAP_1);
dd8bcb3b
AK
1777 printf("%s", ord & IMSM_ORD_REBUILD ? "_" : "U");
1778 }
1779 printf("]");
1780 }
1781 printf("\n");
7095bccb
AK
1782 printf(" Failed disk : ");
1783 if (map->failed_disk_num == 0xff)
b823c8f9 1784 printf(STR_COMMON_NONE);
7095bccb
AK
1785 else
1786 printf("%i", map->failed_disk_num);
1787 printf("\n");
620b1713
DW
1788 slot = get_imsm_disk_slot(map, disk_idx);
1789 if (slot >= 0) {
238c0a71 1790 ord = get_imsm_ord_tbl_ent(dev, slot, MAP_X);
b10b37b8
DW
1791 printf(" This Slot : %d%s\n", slot,
1792 ord & IMSM_ORD_REBUILD ? " (out-of-sync)" : "");
1793 } else
cdddbdbc 1794 printf(" This Slot : ?\n");
84918897 1795 printf(" Sector Size : %u\n", super->sector_size);
fcc2c9da 1796 sz = imsm_dev_size(dev);
84918897
MK
1797 printf(" Array Size : %llu%s\n",
1798 (unsigned long long)sz * 512 / super->sector_size,
cdddbdbc 1799 human_size(sz * 512));
5551b113 1800 sz = blocks_per_member(map);
84918897
MK
1801 printf(" Per Dev Size : %llu%s\n",
1802 (unsigned long long)sz * 512 / super->sector_size,
cdddbdbc 1803 human_size(sz * 512));
5551b113 1804 printf(" Sector Offset : %llu\n",
7d8935cb 1805 pba_of_lba0(map) * 512 / super->sector_size);
5551b113
CA
1806 printf(" Num Stripes : %llu\n",
1807 num_data_stripes(map));
dd8bcb3b 1808 printf(" Chunk Size : %u KiB",
cdddbdbc 1809 __le16_to_cpu(map->blocks_per_strip) / 2);
dd8bcb3b
AK
1810 if (map2)
1811 printf(" <-- %u KiB",
1812 __le16_to_cpu(map2->blocks_per_strip) / 2);
1813 printf("\n");
cdddbdbc 1814 printf(" Reserved : %d\n", __le32_to_cpu(dev->reserved_blocks));
8655a7b1 1815 printf(" Migrate State : ");
1484e727
DW
1816 if (dev->vol.migr_state) {
1817 if (migr_type(dev) == MIGR_INIT)
8655a7b1 1818 printf("initialize\n");
1484e727 1819 else if (migr_type(dev) == MIGR_REBUILD)
8655a7b1 1820 printf("rebuild\n");
1484e727 1821 else if (migr_type(dev) == MIGR_VERIFY)
8655a7b1 1822 printf("check\n");
1484e727 1823 else if (migr_type(dev) == MIGR_GEN_MIGR)
8655a7b1 1824 printf("general migration\n");
1484e727 1825 else if (migr_type(dev) == MIGR_STATE_CHANGE)
8655a7b1 1826 printf("state change\n");
1484e727 1827 else if (migr_type(dev) == MIGR_REPAIR)
8655a7b1 1828 printf("repair\n");
1484e727 1829 else
8655a7b1
DW
1830 printf("<unknown:%d>\n", migr_type(dev));
1831 } else
1832 printf("idle\n");
3393c6af
DW
1833 printf(" Map State : %s", map_state_str[map->map_state]);
1834 if (dev->vol.migr_state) {
238c0a71 1835 struct imsm_map *map = get_imsm_map(dev, MAP_1);
1e5c6983 1836
b10b37b8 1837 printf(" <-- %s", map_state_str[map->map_state]);
4036e7ee 1838 printf("\n Checkpoint : %llu ", vol_curr_migr_unit(dev));
089f9d79 1839 if (is_gen_migration(dev) && (slot > 1 || slot < 0))
464d40e8
LD
1840 printf("(N/A)");
1841 else
1842 printf("(%llu)", (unsigned long long)
1843 blocks_per_migr_unit(super, dev));
3393c6af
DW
1844 }
1845 printf("\n");
2432ce9b
AP
1846 printf(" Dirty State : %s\n", (dev->vol.dirty & RAIDVOL_DIRTY) ?
1847 "dirty" : "clean");
1848 printf(" RWH Policy : ");
c2462068 1849 if (dev->rwh_policy == RWH_OFF || dev->rwh_policy == RWH_MULTIPLE_OFF)
2432ce9b
AP
1850 printf("off\n");
1851 else if (dev->rwh_policy == RWH_DISTRIBUTED)
1852 printf("PPL distributed\n");
1853 else if (dev->rwh_policy == RWH_JOURNALING_DRIVE)
1854 printf("PPL journaling drive\n");
c2462068
PB
1855 else if (dev->rwh_policy == RWH_MULTIPLE_DISTRIBUTED)
1856 printf("Multiple distributed PPLs\n");
1857 else if (dev->rwh_policy == RWH_MULTIPLE_PPLS_JOURNALING_DRIVE)
1858 printf("Multiple PPLs on journaling drive\n");
fbc42556
JR
1859 else if (dev->rwh_policy == RWH_BITMAP)
1860 printf("Write-intent bitmap\n");
2432ce9b
AP
1861 else
1862 printf("<unknown:%d>\n", dev->rwh_policy);
ba1b3bc8
AP
1863
1864 printf(" Volume ID : %u\n", dev->my_vol_raid_dev_num);
cdddbdbc
DW
1865}
1866
ef5c214e
MK
1867static void print_imsm_disk(struct imsm_disk *disk,
1868 int index,
1869 __u32 reserved,
1870 unsigned int sector_size) {
1f24f035 1871 char str[MAX_RAID_SERIAL_LEN + 1];
cdddbdbc
DW
1872 __u64 sz;
1873
0ec1f4e8 1874 if (index < -1 || !disk)
e9d82038
DW
1875 return;
1876
cdddbdbc 1877 printf("\n");
1f24f035 1878 snprintf(str, MAX_RAID_SERIAL_LEN + 1, "%s", disk->serial);
0ec1f4e8
DW
1879 if (index >= 0)
1880 printf(" Disk%02d Serial : %s\n", index, str);
1881 else
1882 printf(" Disk Serial : %s\n", str);
2432ce9b
AP
1883 printf(" State :%s%s%s%s\n", is_spare(disk) ? " spare" : "",
1884 is_configured(disk) ? " active" : "",
1885 is_failed(disk) ? " failed" : "",
1886 is_journal(disk) ? " journal" : "");
cdddbdbc 1887 printf(" Id : %08x\n", __le32_to_cpu(disk->scsi_id));
5551b113 1888 sz = total_blocks(disk) - reserved;
ef5c214e
MK
1889 printf(" Usable Size : %llu%s\n",
1890 (unsigned long long)sz * 512 / sector_size,
cdddbdbc
DW
1891 human_size(sz * 512));
1892}
1893
de44e46f
PB
1894void convert_to_4k_imsm_migr_rec(struct intel_super *super)
1895{
1896 struct migr_record *migr_rec = super->migr_rec;
1897
1898 migr_rec->blocks_per_unit /= IMSM_4K_DIV;
de44e46f
PB
1899 migr_rec->dest_depth_per_unit /= IMSM_4K_DIV;
1900 split_ull((join_u32(migr_rec->post_migr_vol_cap,
1901 migr_rec->post_migr_vol_cap_hi) / IMSM_4K_DIV),
1902 &migr_rec->post_migr_vol_cap, &migr_rec->post_migr_vol_cap_hi);
9f421827
PB
1903 set_migr_chkp_area_pba(migr_rec,
1904 migr_chkp_area_pba(migr_rec) / IMSM_4K_DIV);
1905 set_migr_dest_1st_member_lba(migr_rec,
1906 migr_dest_1st_member_lba(migr_rec) / IMSM_4K_DIV);
de44e46f
PB
1907}
1908
f36a9ecd
PB
1909void convert_to_4k_imsm_disk(struct imsm_disk *disk)
1910{
1911 set_total_blocks(disk, (total_blocks(disk)/IMSM_4K_DIV));
1912}
1913
1914void convert_to_4k(struct intel_super *super)
1915{
1916 struct imsm_super *mpb = super->anchor;
1917 struct imsm_disk *disk;
1918 int i;
e4467bc7 1919 __u32 bbm_log_size = __le32_to_cpu(mpb->bbm_log_size);
f36a9ecd
PB
1920
1921 for (i = 0; i < mpb->num_disks ; i++) {
1922 disk = __get_imsm_disk(mpb, i);
1923 /* disk */
1924 convert_to_4k_imsm_disk(disk);
1925 }
1926 for (i = 0; i < mpb->num_raid_devs; i++) {
1927 struct imsm_dev *dev = __get_imsm_dev(mpb, i);
1928 struct imsm_map *map = get_imsm_map(dev, MAP_0);
1929 /* dev */
fcc2c9da 1930 set_imsm_dev_size(dev, imsm_dev_size(dev)/IMSM_4K_DIV);
4036e7ee
MT
1931 set_vol_curr_migr_unit(dev,
1932 vol_curr_migr_unit(dev) / IMSM_4K_DIV);
f36a9ecd
PB
1933
1934 /* map0 */
1935 set_blocks_per_member(map, blocks_per_member(map)/IMSM_4K_DIV);
1936 map->blocks_per_strip /= IMSM_4K_DIV;
1937 set_pba_of_lba0(map, pba_of_lba0(map)/IMSM_4K_DIV);
1938
1939 if (dev->vol.migr_state) {
1940 /* map1 */
1941 map = get_imsm_map(dev, MAP_1);
1942 set_blocks_per_member(map,
1943 blocks_per_member(map)/IMSM_4K_DIV);
1944 map->blocks_per_strip /= IMSM_4K_DIV;
1945 set_pba_of_lba0(map, pba_of_lba0(map)/IMSM_4K_DIV);
1946 }
1947 }
e4467bc7
TM
1948 if (bbm_log_size) {
1949 struct bbm_log *log = (void *)mpb +
1950 __le32_to_cpu(mpb->mpb_size) - bbm_log_size;
1951 __u32 i;
1952
1953 for (i = 0; i < log->entry_count; i++) {
1954 struct bbm_log_entry *entry =
1955 &log->marked_block_entries[i];
1956
1957 __u8 count = entry->marked_count + 1;
1958 unsigned long long sector =
1959 __le48_to_cpu(&entry->defective_block_start);
1960
1961 entry->defective_block_start =
1962 __cpu_to_le48(sector/IMSM_4K_DIV);
1963 entry->marked_count = max(count/IMSM_4K_DIV, 1) - 1;
1964 }
1965 }
f36a9ecd
PB
1966
1967 mpb->check_sum = __gen_imsm_checksum(mpb);
1968}
1969
520e69e2
AK
1970void examine_migr_rec_imsm(struct intel_super *super)
1971{
1972 struct migr_record *migr_rec = super->migr_rec;
1973 struct imsm_super *mpb = super->anchor;
1974 int i;
1975
1976 for (i = 0; i < mpb->num_raid_devs; i++) {
1977 struct imsm_dev *dev = __get_imsm_dev(mpb, i);
3136abe5 1978 struct imsm_map *map;
b4ab44d8 1979 int slot = -1;
3136abe5 1980
195d1d76 1981 if (is_gen_migration(dev) == false)
520e69e2
AK
1982 continue;
1983
1984 printf("\nMigration Record Information:");
3136abe5 1985
44bfe6df
AK
1986 /* first map under migration */
1987 map = get_imsm_map(dev, MAP_0);
76c152ca 1988
3136abe5
AK
1989 if (map)
1990 slot = get_imsm_disk_slot(map, super->disks->index);
089f9d79 1991 if (map == NULL || slot > 1 || slot < 0) {
520e69e2
AK
1992 printf(" Empty\n ");
1993 printf("Examine one of first two disks in array\n");
1994 break;
1995 }
1996 printf("\n Status : ");
1997 if (__le32_to_cpu(migr_rec->rec_status) == UNIT_SRC_NORMAL)
1998 printf("Normal\n");
1999 else
2000 printf("Contains Data\n");
9f421827
PB
2001 printf(" Current Unit : %llu\n",
2002 current_migr_unit(migr_rec));
520e69e2
AK
2003 printf(" Family : %u\n",
2004 __le32_to_cpu(migr_rec->family_num));
2005 printf(" Ascending : %u\n",
2006 __le32_to_cpu(migr_rec->ascending_migr));
2007 printf(" Blocks Per Unit : %u\n",
2008 __le32_to_cpu(migr_rec->blocks_per_unit));
2009 printf(" Dest. Depth Per Unit : %u\n",
2010 __le32_to_cpu(migr_rec->dest_depth_per_unit));
9f421827
PB
2011 printf(" Checkpoint Area pba : %llu\n",
2012 migr_chkp_area_pba(migr_rec));
2013 printf(" First member lba : %llu\n",
2014 migr_dest_1st_member_lba(migr_rec));
2015 printf(" Total Number of Units : %llu\n",
2016 get_num_migr_units(migr_rec));
2017 printf(" Size of volume : %llu\n",
2018 join_u32(migr_rec->post_migr_vol_cap,
2019 migr_rec->post_migr_vol_cap_hi));
520e69e2
AK
2020 printf(" Record was read from : %u\n",
2021 __le32_to_cpu(migr_rec->ckpt_read_disk_num));
2022
2023 break;
2024 }
2025}
f36a9ecd 2026
de44e46f
PB
2027void convert_from_4k_imsm_migr_rec(struct intel_super *super)
2028{
2029 struct migr_record *migr_rec = super->migr_rec;
2030
2031 migr_rec->blocks_per_unit *= IMSM_4K_DIV;
de44e46f
PB
2032 migr_rec->dest_depth_per_unit *= IMSM_4K_DIV;
2033 split_ull((join_u32(migr_rec->post_migr_vol_cap,
2034 migr_rec->post_migr_vol_cap_hi) * IMSM_4K_DIV),
2035 &migr_rec->post_migr_vol_cap,
2036 &migr_rec->post_migr_vol_cap_hi);
9f421827
PB
2037 set_migr_chkp_area_pba(migr_rec,
2038 migr_chkp_area_pba(migr_rec) * IMSM_4K_DIV);
2039 set_migr_dest_1st_member_lba(migr_rec,
2040 migr_dest_1st_member_lba(migr_rec) * IMSM_4K_DIV);
de44e46f
PB
2041}
2042
f36a9ecd
PB
2043void convert_from_4k(struct intel_super *super)
2044{
2045 struct imsm_super *mpb = super->anchor;
2046 struct imsm_disk *disk;
2047 int i;
e4467bc7 2048 __u32 bbm_log_size = __le32_to_cpu(mpb->bbm_log_size);
f36a9ecd
PB
2049
2050 for (i = 0; i < mpb->num_disks ; i++) {
2051 disk = __get_imsm_disk(mpb, i);
2052 /* disk */
2053 set_total_blocks(disk, (total_blocks(disk)*IMSM_4K_DIV));
2054 }
2055
2056 for (i = 0; i < mpb->num_raid_devs; i++) {
2057 struct imsm_dev *dev = __get_imsm_dev(mpb, i);
2058 struct imsm_map *map = get_imsm_map(dev, MAP_0);
2059 /* dev */
fcc2c9da 2060 set_imsm_dev_size(dev, imsm_dev_size(dev)*IMSM_4K_DIV);
4036e7ee
MT
2061 set_vol_curr_migr_unit(dev,
2062 vol_curr_migr_unit(dev) * IMSM_4K_DIV);
f36a9ecd
PB
2063
2064 /* map0 */
2065 set_blocks_per_member(map, blocks_per_member(map)*IMSM_4K_DIV);
2066 map->blocks_per_strip *= IMSM_4K_DIV;
2067 set_pba_of_lba0(map, pba_of_lba0(map)*IMSM_4K_DIV);
2068
2069 if (dev->vol.migr_state) {
2070 /* map1 */
2071 map = get_imsm_map(dev, MAP_1);
2072 set_blocks_per_member(map,
2073 blocks_per_member(map)*IMSM_4K_DIV);
2074 map->blocks_per_strip *= IMSM_4K_DIV;
2075 set_pba_of_lba0(map, pba_of_lba0(map)*IMSM_4K_DIV);
2076 }
2077 }
e4467bc7
TM
2078 if (bbm_log_size) {
2079 struct bbm_log *log = (void *)mpb +
2080 __le32_to_cpu(mpb->mpb_size) - bbm_log_size;
2081 __u32 i;
2082
2083 for (i = 0; i < log->entry_count; i++) {
2084 struct bbm_log_entry *entry =
2085 &log->marked_block_entries[i];
2086
2087 __u8 count = entry->marked_count + 1;
2088 unsigned long long sector =
2089 __le48_to_cpu(&entry->defective_block_start);
2090
2091 entry->defective_block_start =
2092 __cpu_to_le48(sector*IMSM_4K_DIV);
2093 entry->marked_count = count*IMSM_4K_DIV - 1;
2094 }
2095 }
f36a9ecd
PB
2096
2097 mpb->check_sum = __gen_imsm_checksum(mpb);
2098}
2099
19482bcc
AK
2100/*******************************************************************************
2101 * function: imsm_check_attributes
2102 * Description: Function checks if features represented by attributes flags
1011e834 2103 * are supported by mdadm.
19482bcc
AK
2104 * Parameters:
2105 * attributes - Attributes read from metadata
2106 * Returns:
1011e834
N
2107 * 0 - passed attributes contains unsupported features flags
2108 * 1 - all features are supported
19482bcc
AK
2109 ******************************************************************************/
2110static int imsm_check_attributes(__u32 attributes)
2111{
2112 int ret_val = 1;
418f9b36
N
2113 __u32 not_supported = MPB_ATTRIB_SUPPORTED^0xffffffff;
2114
2115 not_supported &= ~MPB_ATTRIB_IGNORED;
19482bcc
AK
2116
2117 not_supported &= attributes;
2118 if (not_supported) {
e7b84f9d 2119 pr_err("(IMSM): Unsupported attributes : %x\n",
418f9b36 2120 (unsigned)__le32_to_cpu(not_supported));
19482bcc
AK
2121 if (not_supported & MPB_ATTRIB_CHECKSUM_VERIFY) {
2122 dprintf("\t\tMPB_ATTRIB_CHECKSUM_VERIFY \n");
2123 not_supported ^= MPB_ATTRIB_CHECKSUM_VERIFY;
2124 }
2125 if (not_supported & MPB_ATTRIB_2TB) {
2126 dprintf("\t\tMPB_ATTRIB_2TB\n");
2127 not_supported ^= MPB_ATTRIB_2TB;
2128 }
2129 if (not_supported & MPB_ATTRIB_RAID0) {
2130 dprintf("\t\tMPB_ATTRIB_RAID0\n");
2131 not_supported ^= MPB_ATTRIB_RAID0;
2132 }
2133 if (not_supported & MPB_ATTRIB_RAID1) {
2134 dprintf("\t\tMPB_ATTRIB_RAID1\n");
2135 not_supported ^= MPB_ATTRIB_RAID1;
2136 }
2137 if (not_supported & MPB_ATTRIB_RAID10) {
2138 dprintf("\t\tMPB_ATTRIB_RAID10\n");
2139 not_supported ^= MPB_ATTRIB_RAID10;
2140 }
2141 if (not_supported & MPB_ATTRIB_RAID1E) {
2142 dprintf("\t\tMPB_ATTRIB_RAID1E\n");
2143 not_supported ^= MPB_ATTRIB_RAID1E;
2144 }
2145 if (not_supported & MPB_ATTRIB_RAID5) {
2146 dprintf("\t\tMPB_ATTRIB_RAID5\n");
2147 not_supported ^= MPB_ATTRIB_RAID5;
2148 }
2149 if (not_supported & MPB_ATTRIB_RAIDCNG) {
2150 dprintf("\t\tMPB_ATTRIB_RAIDCNG\n");
2151 not_supported ^= MPB_ATTRIB_RAIDCNG;
2152 }
2153 if (not_supported & MPB_ATTRIB_BBM) {
2154 dprintf("\t\tMPB_ATTRIB_BBM\n");
2155 not_supported ^= MPB_ATTRIB_BBM;
2156 }
2157 if (not_supported & MPB_ATTRIB_CHECKSUM_VERIFY) {
2158 dprintf("\t\tMPB_ATTRIB_CHECKSUM_VERIFY (== MPB_ATTRIB_LEGACY)\n");
2159 not_supported ^= MPB_ATTRIB_CHECKSUM_VERIFY;
2160 }
2161 if (not_supported & MPB_ATTRIB_EXP_STRIPE_SIZE) {
2162 dprintf("\t\tMPB_ATTRIB_EXP_STRIP_SIZE\n");
2163 not_supported ^= MPB_ATTRIB_EXP_STRIPE_SIZE;
2164 }
2165 if (not_supported & MPB_ATTRIB_2TB_DISK) {
2166 dprintf("\t\tMPB_ATTRIB_2TB_DISK\n");
2167 not_supported ^= MPB_ATTRIB_2TB_DISK;
2168 }
2169 if (not_supported & MPB_ATTRIB_NEVER_USE2) {
2170 dprintf("\t\tMPB_ATTRIB_NEVER_USE2\n");
2171 not_supported ^= MPB_ATTRIB_NEVER_USE2;
2172 }
2173 if (not_supported & MPB_ATTRIB_NEVER_USE) {
2174 dprintf("\t\tMPB_ATTRIB_NEVER_USE\n");
2175 not_supported ^= MPB_ATTRIB_NEVER_USE;
2176 }
2177
2178 if (not_supported)
1ade5cc1 2179 dprintf("(IMSM): Unknown attributes : %x\n", not_supported);
19482bcc
AK
2180
2181 ret_val = 0;
2182 }
2183
2184 return ret_val;
2185}
2186
a5d85af7 2187static void getinfo_super_imsm(struct supertype *st, struct mdinfo *info, char *map);
44470971 2188
cdddbdbc
DW
2189static void examine_super_imsm(struct supertype *st, char *homehost)
2190{
2191 struct intel_super *super = st->sb;
949c47a0 2192 struct imsm_super *mpb = super->anchor;
cdddbdbc
DW
2193 char str[MAX_SIGNATURE_LENGTH];
2194 int i;
27fd6274
DW
2195 struct mdinfo info;
2196 char nbuf[64];
cdddbdbc 2197 __u32 sum;
14e8215b 2198 __u32 reserved = imsm_reserved_sectors(super, super->disks);
94827db3 2199 struct dl *dl;
e48aed3c 2200 time_t creation_time;
27fd6274 2201
618f4e6d
XN
2202 strncpy(str, (char *)mpb->sig, MPB_SIG_LEN);
2203 str[MPB_SIG_LEN-1] = '\0';
cdddbdbc 2204 printf(" Magic : %s\n", str);
cdddbdbc 2205 printf(" Version : %s\n", get_imsm_version(mpb));
148acb7b 2206 printf(" Orig Family : %08x\n", __le32_to_cpu(mpb->orig_family_num));
cdddbdbc
DW
2207 printf(" Family : %08x\n", __le32_to_cpu(mpb->family_num));
2208 printf(" Generation : %08x\n", __le32_to_cpu(mpb->generation_num));
e48aed3c
AP
2209 creation_time = __le64_to_cpu(mpb->creation_time);
2210 printf(" Creation Time : %.24s\n",
2211 creation_time ? ctime(&creation_time) : "Unknown");
19482bcc
AK
2212 printf(" Attributes : ");
2213 if (imsm_check_attributes(mpb->attributes))
2214 printf("All supported\n");
2215 else
2216 printf("not supported\n");
a5d85af7 2217 getinfo_super_imsm(st, &info, NULL);
ba65d917 2218 fname_from_uuid(&info, nbuf);
27fd6274 2219 printf(" UUID : %s\n", nbuf + 5);
cdddbdbc
DW
2220 sum = __le32_to_cpu(mpb->check_sum);
2221 printf(" Checksum : %08x %s\n", sum,
949c47a0 2222 __gen_imsm_checksum(mpb) == sum ? "correct" : "incorrect");
f36a9ecd 2223 printf(" MPB Sectors : %d\n", mpb_sectors(mpb, super->sector_size));
cdddbdbc
DW
2224 printf(" Disks : %d\n", mpb->num_disks);
2225 printf(" RAID Devices : %d\n", mpb->num_raid_devs);
ef5c214e
MK
2226 print_imsm_disk(__get_imsm_disk(mpb, super->disks->index),
2227 super->disks->index, reserved, super->sector_size);
8d67477f 2228 if (get_imsm_bbm_log_size(super->bbm_log)) {
604b746f
JD
2229 struct bbm_log *log = super->bbm_log;
2230
2231 printf("\n");
2232 printf("Bad Block Management Log:\n");
2233 printf(" Log Size : %d\n", __le32_to_cpu(mpb->bbm_log_size));
2234 printf(" Signature : %x\n", __le32_to_cpu(log->signature));
2235 printf(" Entry Count : %d\n", __le32_to_cpu(log->entry_count));
604b746f 2236 }
44470971
DW
2237 for (i = 0; i < mpb->num_raid_devs; i++) {
2238 struct mdinfo info;
2239 struct imsm_dev *dev = __get_imsm_dev(mpb, i);
2240
2241 super->current_vol = i;
a5d85af7 2242 getinfo_super_imsm(st, &info, NULL);
ba65d917 2243 fname_from_uuid(&info, nbuf);
c47b0ff6 2244 print_imsm_dev(super, dev, nbuf + 5, super->disks->index);
44470971 2245 }
cdddbdbc
DW
2246 for (i = 0; i < mpb->num_disks; i++) {
2247 if (i == super->disks->index)
2248 continue;
ef5c214e
MK
2249 print_imsm_disk(__get_imsm_disk(mpb, i), i, reserved,
2250 super->sector_size);
cdddbdbc 2251 }
94827db3 2252
0ec1f4e8
DW
2253 for (dl = super->disks; dl; dl = dl->next)
2254 if (dl->index == -1)
ef5c214e
MK
2255 print_imsm_disk(&dl->disk, -1, reserved,
2256 super->sector_size);
520e69e2
AK
2257
2258 examine_migr_rec_imsm(super);
cdddbdbc
DW
2259}
2260
061f2c6a 2261static void brief_examine_super_imsm(struct supertype *st, int verbose)
cdddbdbc 2262{
27fd6274 2263 /* We just write a generic IMSM ARRAY entry */
ff54de6e
N
2264 struct mdinfo info;
2265 char nbuf[64];
2266
a5d85af7 2267 getinfo_super_imsm(st, &info, NULL);
ba65d917 2268 fname_from_uuid(&info, nbuf);
4737ae25
N
2269 printf("ARRAY metadata=imsm UUID=%s\n", nbuf + 5);
2270}
2271
2272static void brief_examine_subarrays_imsm(struct supertype *st, int verbose)
2273{
2274 /* We just write a generic IMSM ARRAY entry */
2275 struct mdinfo info;
2276 char nbuf[64];
2277 char nbuf1[64];
2278 struct intel_super *super = st->sb;
2279 int i;
2280
2281 if (!super->anchor->num_raid_devs)
2282 return;
2283
a5d85af7 2284 getinfo_super_imsm(st, &info, NULL);
ba65d917 2285 fname_from_uuid(&info, nbuf);
1e7bc0ed
DW
2286 for (i = 0; i < super->anchor->num_raid_devs; i++) {
2287 struct imsm_dev *dev = get_imsm_dev(super, i);
2288
2289 super->current_vol = i;
a5d85af7 2290 getinfo_super_imsm(st, &info, NULL);
ba65d917 2291 fname_from_uuid(&info, nbuf1);
b9ce7ab0 2292 printf("ARRAY " DEV_MD_DIR "%.16s container=%s member=%d UUID=%s\n",
cf8de691 2293 dev->volume, nbuf + 5, i, nbuf1 + 5);
1e7bc0ed 2294 }
cdddbdbc
DW
2295}
2296
9d84c8ea
DW
2297static void export_examine_super_imsm(struct supertype *st)
2298{
2299 struct intel_super *super = st->sb;
2300 struct imsm_super *mpb = super->anchor;
2301 struct mdinfo info;
2302 char nbuf[64];
2303
a5d85af7 2304 getinfo_super_imsm(st, &info, NULL);
ba65d917 2305 fname_from_uuid(&info, nbuf);
9d84c8ea
DW
2306 printf("MD_METADATA=imsm\n");
2307 printf("MD_LEVEL=container\n");
2308 printf("MD_UUID=%s\n", nbuf+5);
2309 printf("MD_DEVICES=%u\n", mpb->num_disks);
e48aed3c 2310 printf("MD_CREATION_TIME=%llu\n", __le64_to_cpu(mpb->creation_time));
9d84c8ea
DW
2311}
2312
b771faef
BK
2313static void detail_super_imsm(struct supertype *st, char *homehost,
2314 char *subarray)
cdddbdbc 2315{
3ebe00a1
DW
2316 struct mdinfo info;
2317 char nbuf[64];
b771faef
BK
2318 struct intel_super *super = st->sb;
2319 int temp_vol = super->current_vol;
2320
2321 if (subarray)
2322 super->current_vol = strtoul(subarray, NULL, 10);
3ebe00a1 2323
a5d85af7 2324 getinfo_super_imsm(st, &info, NULL);
ba65d917 2325 fname_from_uuid(&info, nbuf);
65884368 2326 printf("\n UUID : %s\n", nbuf + 5);
b771faef
BK
2327
2328 super->current_vol = temp_vol;
cdddbdbc
DW
2329}
2330
b771faef 2331static void brief_detail_super_imsm(struct supertype *st, char *subarray)
cdddbdbc 2332{
ff54de6e
N
2333 struct mdinfo info;
2334 char nbuf[64];
b771faef
BK
2335 struct intel_super *super = st->sb;
2336 int temp_vol = super->current_vol;
2337
2338 if (subarray)
2339 super->current_vol = strtoul(subarray, NULL, 10);
2340
a5d85af7 2341 getinfo_super_imsm(st, &info, NULL);
ba65d917 2342 fname_from_uuid(&info, nbuf);
ff54de6e 2343 printf(" UUID=%s", nbuf + 5);
b771faef
BK
2344
2345 super->current_vol = temp_vol;
cdddbdbc 2346}
d665cc31 2347
6da53c0e
BK
2348static int imsm_read_serial(int fd, char *devname, __u8 *serial,
2349 size_t serial_buf_len);
d665cc31
DW
2350static void fd2devname(int fd, char *name);
2351
120dc887 2352static int ahci_enumerate_ports(const char *hba_path, int port_count, int host_base, int verbose)
d665cc31 2353{
120dc887
LM
2354 /* dump an unsorted list of devices attached to AHCI Intel storage
2355 * controller, as well as non-connected ports
d665cc31
DW
2356 */
2357 int hba_len = strlen(hba_path) + 1;
2358 struct dirent *ent;
2359 DIR *dir;
2360 char *path = NULL;
2361 int err = 0;
2362 unsigned long port_mask = (1 << port_count) - 1;
2363
f21e18ca 2364 if (port_count > (int)sizeof(port_mask) * 8) {
ba728be7 2365 if (verbose > 0)
e7b84f9d 2366 pr_err("port_count %d out of range\n", port_count);
d665cc31
DW
2367 return 2;
2368 }
2369
2370 /* scroll through /sys/dev/block looking for devices attached to
2371 * this hba
2372 */
2373 dir = opendir("/sys/dev/block");
1a6dd6b9
PB
2374 if (!dir)
2375 return 1;
2376
2377 for (ent = readdir(dir); ent; ent = readdir(dir)) {
d665cc31
DW
2378 int fd;
2379 char model[64];
2380 char vendor[64];
2381 char buf[1024];
2382 int major, minor;
fcebeb77 2383 char device[PATH_MAX];
d665cc31
DW
2384 char *c;
2385 int port;
2386 int type;
2387
2388 if (sscanf(ent->d_name, "%d:%d", &major, &minor) != 2)
2389 continue;
7c798f87 2390 path = devt_to_devpath(makedev(major, minor), 1, NULL);
d665cc31
DW
2391 if (!path)
2392 continue;
2393 if (!path_attached_to_hba(path, hba_path)) {
2394 free(path);
2395 path = NULL;
2396 continue;
2397 }
2398
fcebeb77
MT
2399 /* retrieve the scsi device */
2400 if (!devt_to_devpath(makedev(major, minor), 1, device)) {
ba728be7 2401 if (verbose > 0)
fcebeb77 2402 pr_err("failed to get device\n");
d665cc31
DW
2403 err = 2;
2404 break;
2405 }
fcebeb77 2406 if (devpath_to_char(device, "type", buf, sizeof(buf), 0)) {
d665cc31 2407 err = 2;
d665cc31
DW
2408 break;
2409 }
2410 type = strtoul(buf, NULL, 10);
2411
2412 /* if it's not a disk print the vendor and model */
2413 if (!(type == 0 || type == 7 || type == 14)) {
2414 vendor[0] = '\0';
2415 model[0] = '\0';
fcebeb77
MT
2416
2417 if (devpath_to_char(device, "vendor", buf,
2418 sizeof(buf), 0) == 0) {
d665cc31
DW
2419 strncpy(vendor, buf, sizeof(vendor));
2420 vendor[sizeof(vendor) - 1] = '\0';
2421 c = (char *) &vendor[sizeof(vendor) - 1];
2422 while (isspace(*c) || *c == '\0')
2423 *c-- = '\0';
2424
2425 }
fcebeb77
MT
2426
2427 if (devpath_to_char(device, "model", buf,
2428 sizeof(buf), 0) == 0) {
d665cc31
DW
2429 strncpy(model, buf, sizeof(model));
2430 model[sizeof(model) - 1] = '\0';
2431 c = (char *) &model[sizeof(model) - 1];
2432 while (isspace(*c) || *c == '\0')
2433 *c-- = '\0';
2434 }
2435
2436 if (vendor[0] && model[0])
2437 sprintf(buf, "%.64s %.64s", vendor, model);
2438 else
2439 switch (type) { /* numbers from hald/linux/device.c */
2440 case 1: sprintf(buf, "tape"); break;
2441 case 2: sprintf(buf, "printer"); break;
2442 case 3: sprintf(buf, "processor"); break;
2443 case 4:
2444 case 5: sprintf(buf, "cdrom"); break;
2445 case 6: sprintf(buf, "scanner"); break;
2446 case 8: sprintf(buf, "media_changer"); break;
2447 case 9: sprintf(buf, "comm"); break;
2448 case 12: sprintf(buf, "raid"); break;
2449 default: sprintf(buf, "unknown");
2450 }
2451 } else
2452 buf[0] = '\0';
d665cc31
DW
2453
2454 /* chop device path to 'host%d' and calculate the port number */
2455 c = strchr(&path[hba_len], '/');
4e5e717d 2456 if (!c) {
ba728be7 2457 if (verbose > 0)
e7b84f9d 2458 pr_err("%s - invalid path name\n", path + hba_len);
4e5e717d
AW
2459 err = 2;
2460 break;
2461 }
d665cc31 2462 *c = '\0';
0858eccf
AP
2463 if ((sscanf(&path[hba_len], "ata%d", &port) == 1) ||
2464 ((sscanf(&path[hba_len], "host%d", &port) == 1)))
d665cc31
DW
2465 port -= host_base;
2466 else {
ba728be7 2467 if (verbose > 0) {
d665cc31 2468 *c = '/'; /* repair the full string */
e7b84f9d 2469 pr_err("failed to determine port number for %s\n",
d665cc31
DW
2470 path);
2471 }
2472 err = 2;
2473 break;
2474 }
2475
2476 /* mark this port as used */
2477 port_mask &= ~(1 << port);
2478
2479 /* print out the device information */
2480 if (buf[0]) {
2481 printf(" Port%d : - non-disk device (%s) -\n", port, buf);
2482 continue;
2483 }
2484
2485 fd = dev_open(ent->d_name, O_RDONLY);
4389ce73 2486 if (!is_fd_valid(fd))
d665cc31
DW
2487 printf(" Port%d : - disk info unavailable -\n", port);
2488 else {
2489 fd2devname(fd, buf);
2490 printf(" Port%d : %s", port, buf);
6da53c0e
BK
2491 if (imsm_read_serial(fd, NULL, (__u8 *)buf,
2492 sizeof(buf)) == 0)
2493 printf(" (%s)\n", buf);
d665cc31 2494 else
664d5325 2495 printf(" ()\n");
4dab422a 2496 close(fd);
d665cc31 2497 }
d665cc31
DW
2498 free(path);
2499 path = NULL;
2500 }
2501 if (path)
2502 free(path);
2503 if (dir)
2504 closedir(dir);
2505 if (err == 0) {
2506 int i;
2507
2508 for (i = 0; i < port_count; i++)
2509 if (port_mask & (1 << i))
2510 printf(" Port%d : - no device attached -\n", i);
2511 }
2512
2513 return err;
2514}
2515
6da53c0e 2516static int print_nvme_info(struct sys_dev *hba)
60f0f54d
PB
2517{
2518 struct dirent *ent;
2519 DIR *dir;
60f0f54d 2520
6da53c0e 2521 dir = opendir("/sys/block/");
b9135011 2522 if (!dir)
b5eece69 2523 return 1;
b9135011
JS
2524
2525 for (ent = readdir(dir); ent; ent = readdir(dir)) {
8662f92d
MT
2526 char ns_path[PATH_MAX];
2527 char cntrl_path[PATH_MAX];
2528 char buf[PATH_MAX];
2529 int fd = -1;
60f0f54d 2530
8662f92d
MT
2531 if (!strstr(ent->d_name, "nvme"))
2532 goto skip;
d835518b 2533
8662f92d 2534 fd = open_dev(ent->d_name);
4389ce73 2535 if (!is_fd_valid(fd))
8662f92d 2536 goto skip;
d835518b 2537
8662f92d
MT
2538 if (!diskfd_to_devpath(fd, 0, ns_path) ||
2539 !diskfd_to_devpath(fd, 1, cntrl_path))
2540 goto skip;
2541
2542 if (!path_attached_to_hba(cntrl_path, hba->path))
2543 goto skip;
2544
2545 if (!imsm_is_nvme_namespace_supported(fd, 0))
2546 goto skip;
2547
2548 fd2devname(fd, buf);
2549 if (hba->type == SYS_DEV_VMD)
2550 printf(" NVMe under VMD : %s", buf);
2551 else if (hba->type == SYS_DEV_NVME)
2552 printf(" NVMe Device : %s", buf);
2553
2554 if (!imsm_read_serial(fd, NULL, (__u8 *)buf,
2555 sizeof(buf)))
2556 printf(" (%s)\n", buf);
2557 else
2558 printf("()\n");
2559
2560skip:
4389ce73 2561 close_fd(&fd);
60f0f54d
PB
2562 }
2563
b9135011 2564 closedir(dir);
b5eece69 2565 return 0;
60f0f54d
PB
2566}
2567
120dc887
LM
2568static void print_found_intel_controllers(struct sys_dev *elem)
2569{
2570 for (; elem; elem = elem->next) {
e7b84f9d 2571 pr_err("found Intel(R) ");
120dc887
LM
2572 if (elem->type == SYS_DEV_SATA)
2573 fprintf(stderr, "SATA ");
155cbb4c
LM
2574 else if (elem->type == SYS_DEV_SAS)
2575 fprintf(stderr, "SAS ");
0858eccf
AP
2576 else if (elem->type == SYS_DEV_NVME)
2577 fprintf(stderr, "NVMe ");
60f0f54d
PB
2578
2579 if (elem->type == SYS_DEV_VMD)
2580 fprintf(stderr, "VMD domain");
75350d87
KF
2581 else if (elem->type == SYS_DEV_SATA_VMD)
2582 fprintf(stderr, "SATA VMD domain");
60f0f54d
PB
2583 else
2584 fprintf(stderr, "RAID controller");
2585
120dc887
LM
2586 if (elem->pci_id)
2587 fprintf(stderr, " at %s", elem->pci_id);
2588 fprintf(stderr, ".\n");
2589 }
2590 fflush(stderr);
2591}
2592
120dc887
LM
2593static int ahci_get_port_count(const char *hba_path, int *port_count)
2594{
2595 struct dirent *ent;
2596 DIR *dir;
2597 int host_base = -1;
2598
2599 *port_count = 0;
2600 if ((dir = opendir(hba_path)) == NULL)
2601 return -1;
2602
2603 for (ent = readdir(dir); ent; ent = readdir(dir)) {
2604 int host;
2605
0858eccf
AP
2606 if ((sscanf(ent->d_name, "ata%d", &host) != 1) &&
2607 ((sscanf(ent->d_name, "host%d", &host) != 1)))
120dc887
LM
2608 continue;
2609 if (*port_count == 0)
2610 host_base = host;
2611 else if (host < host_base)
2612 host_base = host;
2613
2614 if (host + 1 > *port_count + host_base)
2615 *port_count = host + 1 - host_base;
2616 }
2617 closedir(dir);
2618 return host_base;
2619}
2620
a891a3c2
LM
2621static void print_imsm_capability(const struct imsm_orom *orom)
2622{
0858eccf
AP
2623 printf(" Platform : Intel(R) ");
2624 if (orom->capabilities == 0 && orom->driver_features == 0)
2625 printf("Matrix Storage Manager\n");
ab0c6bb9
AP
2626 else if (imsm_orom_is_enterprise(orom) && orom->major_ver >= 6)
2627 printf("Virtual RAID on CPU\n");
0858eccf
AP
2628 else
2629 printf("Rapid Storage Technology%s\n",
2630 imsm_orom_is_enterprise(orom) ? " enterprise" : "");
8d1114be
MG
2631 if (orom->major_ver || orom->minor_ver || orom->hotfix_ver || orom->build) {
2632 if (imsm_orom_is_vmd_without_efi(orom))
2633 printf(" Version : %d.%d\n", orom->major_ver,
2634 orom->minor_ver);
2635 else
2636 printf(" Version : %d.%d.%d.%d\n", orom->major_ver,
2637 orom->minor_ver, orom->hotfix_ver, orom->build);
2638 }
a891a3c2
LM
2639 printf(" RAID Levels :%s%s%s%s%s\n",
2640 imsm_orom_has_raid0(orom) ? " raid0" : "",
2641 imsm_orom_has_raid1(orom) ? " raid1" : "",
2642 imsm_orom_has_raid1e(orom) ? " raid1e" : "",
2643 imsm_orom_has_raid10(orom) ? " raid10" : "",
2644 imsm_orom_has_raid5(orom) ? " raid5" : "");
2645 printf(" Chunk Sizes :%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
2646 imsm_orom_has_chunk(orom, 2) ? " 2k" : "",
2647 imsm_orom_has_chunk(orom, 4) ? " 4k" : "",
2648 imsm_orom_has_chunk(orom, 8) ? " 8k" : "",
2649 imsm_orom_has_chunk(orom, 16) ? " 16k" : "",
2650 imsm_orom_has_chunk(orom, 32) ? " 32k" : "",
2651 imsm_orom_has_chunk(orom, 64) ? " 64k" : "",
2652 imsm_orom_has_chunk(orom, 128) ? " 128k" : "",
2653 imsm_orom_has_chunk(orom, 256) ? " 256k" : "",
2654 imsm_orom_has_chunk(orom, 512) ? " 512k" : "",
2655 imsm_orom_has_chunk(orom, 1024*1) ? " 1M" : "",
2656 imsm_orom_has_chunk(orom, 1024*2) ? " 2M" : "",
2657 imsm_orom_has_chunk(orom, 1024*4) ? " 4M" : "",
2658 imsm_orom_has_chunk(orom, 1024*8) ? " 8M" : "",
2659 imsm_orom_has_chunk(orom, 1024*16) ? " 16M" : "",
2660 imsm_orom_has_chunk(orom, 1024*32) ? " 32M" : "",
2661 imsm_orom_has_chunk(orom, 1024*64) ? " 64M" : "");
29cd0821
CA
2662 printf(" 2TB volumes :%s supported\n",
2663 (orom->attr & IMSM_OROM_ATTR_2TB)?"":" not");
2664 printf(" 2TB disks :%s supported\n",
2665 (orom->attr & IMSM_OROM_ATTR_2TB_DISK)?"":" not");
0e7f69a8 2666 printf(" Max Disks : %d\n", orom->tds);
0858eccf
AP
2667 printf(" Max Volumes : %d per array, %d per %s\n",
2668 orom->vpa, orom->vphba,
2669 imsm_orom_is_nvme(orom) ? "platform" : "controller");
a891a3c2
LM
2670 return;
2671}
2672
e50cf220
MN
2673static void print_imsm_capability_export(const struct imsm_orom *orom)
2674{
2675 printf("MD_FIRMWARE_TYPE=imsm\n");
0858eccf
AP
2676 if (orom->major_ver || orom->minor_ver || orom->hotfix_ver || orom->build)
2677 printf("IMSM_VERSION=%d.%d.%d.%d\n", orom->major_ver, orom->minor_ver,
2678 orom->hotfix_ver, orom->build);
e50cf220
MN
2679 printf("IMSM_SUPPORTED_RAID_LEVELS=%s%s%s%s%s\n",
2680 imsm_orom_has_raid0(orom) ? "raid0 " : "",
2681 imsm_orom_has_raid1(orom) ? "raid1 " : "",
2682 imsm_orom_has_raid1e(orom) ? "raid1e " : "",
2683 imsm_orom_has_raid5(orom) ? "raid10 " : "",
2684 imsm_orom_has_raid10(orom) ? "raid5 " : "");
2685 printf("IMSM_SUPPORTED_CHUNK_SIZES=%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
2686 imsm_orom_has_chunk(orom, 2) ? "2k " : "",
2687 imsm_orom_has_chunk(orom, 4) ? "4k " : "",
2688 imsm_orom_has_chunk(orom, 8) ? "8k " : "",
2689 imsm_orom_has_chunk(orom, 16) ? "16k " : "",
2690 imsm_orom_has_chunk(orom, 32) ? "32k " : "",
2691 imsm_orom_has_chunk(orom, 64) ? "64k " : "",
2692 imsm_orom_has_chunk(orom, 128) ? "128k " : "",
2693 imsm_orom_has_chunk(orom, 256) ? "256k " : "",
2694 imsm_orom_has_chunk(orom, 512) ? "512k " : "",
2695 imsm_orom_has_chunk(orom, 1024*1) ? "1M " : "",
2696 imsm_orom_has_chunk(orom, 1024*2) ? "2M " : "",
2697 imsm_orom_has_chunk(orom, 1024*4) ? "4M " : "",
2698 imsm_orom_has_chunk(orom, 1024*8) ? "8M " : "",
2699 imsm_orom_has_chunk(orom, 1024*16) ? "16M " : "",
2700 imsm_orom_has_chunk(orom, 1024*32) ? "32M " : "",
2701 imsm_orom_has_chunk(orom, 1024*64) ? "64M " : "");
2702 printf("IMSM_2TB_VOLUMES=%s\n",(orom->attr & IMSM_OROM_ATTR_2TB) ? "yes" : "no");
2703 printf("IMSM_2TB_DISKS=%s\n",(orom->attr & IMSM_OROM_ATTR_2TB_DISK) ? "yes" : "no");
2704 printf("IMSM_MAX_DISKS=%d\n",orom->tds);
2705 printf("IMSM_MAX_VOLUMES_PER_ARRAY=%d\n",orom->vpa);
2706 printf("IMSM_MAX_VOLUMES_PER_CONTROLLER=%d\n",orom->vphba);
2707}
2708
9eafa1de 2709static int detail_platform_imsm(int verbose, int enumerate_only, char *controller_path)
d665cc31
DW
2710{
2711 /* There are two components to imsm platform support, the ahci SATA
2712 * controller and the option-rom. To find the SATA controller we
2713 * simply look in /sys/bus/pci/drivers/ahci to see if an ahci
2714 * controller with the Intel vendor id is present. This approach
2715 * allows mdadm to leverage the kernel's ahci detection logic, with the
2716 * caveat that if ahci.ko is not loaded mdadm will not be able to
2717 * detect platform raid capabilities. The option-rom resides in a
2718 * platform "Adapter ROM". We scan for its signature to retrieve the
2719 * platform capabilities. If raid support is disabled in the BIOS the
2720 * option-rom capability structure will not be available.
2721 */
d665cc31 2722 struct sys_dev *list, *hba;
d665cc31
DW
2723 int host_base = 0;
2724 int port_count = 0;
9eafa1de 2725 int result=1;
d665cc31 2726
5615172f 2727 if (enumerate_only) {
420dafcd 2728 if (check_no_platform())
5615172f 2729 return 0;
a891a3c2
LM
2730 list = find_intel_devices();
2731 if (!list)
2732 return 2;
2733 for (hba = list; hba; hba = hba->next) {
6b781d33
AP
2734 if (find_imsm_capability(hba)) {
2735 result = 0;
a891a3c2
LM
2736 break;
2737 }
9eafa1de 2738 else
6b781d33 2739 result = 2;
a891a3c2 2740 }
a891a3c2 2741 return result;
5615172f
DW
2742 }
2743
155cbb4c
LM
2744 list = find_intel_devices();
2745 if (!list) {
ba728be7 2746 if (verbose > 0)
7a862a02 2747 pr_err("no active Intel(R) RAID controller found.\n");
d665cc31 2748 return 2;
ba728be7 2749 } else if (verbose > 0)
155cbb4c 2750 print_found_intel_controllers(list);
d665cc31 2751
a891a3c2 2752 for (hba = list; hba; hba = hba->next) {
0858eccf 2753 if (controller_path && (compare_paths(hba->path, controller_path) != 0))
9eafa1de 2754 continue;
0858eccf 2755 if (!find_imsm_capability(hba)) {
60f0f54d 2756 char buf[PATH_MAX];
e7b84f9d 2757 pr_err("imsm capabilities not found for controller: %s (type %s)\n",
75350d87
KF
2758 hba->type == SYS_DEV_VMD || hba->type == SYS_DEV_SATA_VMD ?
2759 vmd_domain_to_controller(hba, buf) :
2760 hba->path, get_sys_dev_type(hba->type));
0858eccf
AP
2761 continue;
2762 }
2763 result = 0;
2764 }
2765
2766 if (controller_path && result == 1) {
2767 pr_err("no active Intel(R) RAID controller found under %s\n",
2768 controller_path);
2769 return result;
2770 }
2771
5e1d6128 2772 const struct orom_entry *entry;
0858eccf 2773
5e1d6128 2774 for (entry = orom_entries; entry; entry = entry->next) {
60f0f54d 2775 if (entry->type == SYS_DEV_VMD) {
07cb1e57 2776 print_imsm_capability(&entry->orom);
32716c51
PB
2777 printf(" 3rd party NVMe :%s supported\n",
2778 imsm_orom_has_tpv_support(&entry->orom)?"":" not");
60f0f54d
PB
2779 for (hba = list; hba; hba = hba->next) {
2780 if (hba->type == SYS_DEV_VMD) {
2781 char buf[PATH_MAX];
60f0f54d
PB
2782 printf(" I/O Controller : %s (%s)\n",
2783 vmd_domain_to_controller(hba, buf), get_sys_dev_type(hba->type));
6da53c0e 2784 if (print_nvme_info(hba)) {
b5eece69
PB
2785 if (verbose > 0)
2786 pr_err("failed to get devices attached to VMD domain.\n");
2787 result |= 2;
2788 }
60f0f54d
PB
2789 }
2790 }
07cb1e57 2791 printf("\n");
60f0f54d
PB
2792 continue;
2793 }
0858eccf 2794
60f0f54d
PB
2795 print_imsm_capability(&entry->orom);
2796 if (entry->type == SYS_DEV_NVME) {
0858eccf
AP
2797 for (hba = list; hba; hba = hba->next) {
2798 if (hba->type == SYS_DEV_NVME)
6da53c0e 2799 print_nvme_info(hba);
0858eccf 2800 }
60f0f54d 2801 printf("\n");
0858eccf
AP
2802 continue;
2803 }
2804
2805 struct devid_list *devid;
5e1d6128 2806 for (devid = entry->devid_list; devid; devid = devid->next) {
0858eccf
AP
2807 hba = device_by_id(devid->devid);
2808 if (!hba)
2809 continue;
2810
9eafa1de
MN
2811 printf(" I/O Controller : %s (%s)\n",
2812 hba->path, get_sys_dev_type(hba->type));
75350d87 2813 if (hba->type == SYS_DEV_SATA || hba->type == SYS_DEV_SATA_VMD) {
9eafa1de
MN
2814 host_base = ahci_get_port_count(hba->path, &port_count);
2815 if (ahci_enumerate_ports(hba->path, port_count, host_base, verbose)) {
2816 if (verbose > 0)
75350d87
KF
2817 pr_err("failed to enumerate ports on %s controller at %s.\n",
2818 get_sys_dev_type(hba->type), hba->pci_id);
9eafa1de
MN
2819 result |= 2;
2820 }
120dc887
LM
2821 }
2822 }
0858eccf 2823 printf("\n");
d665cc31 2824 }
155cbb4c 2825
120dc887 2826 return result;
d665cc31 2827}
e50cf220 2828
9eafa1de 2829static int export_detail_platform_imsm(int verbose, char *controller_path)
e50cf220 2830{
e50cf220
MN
2831 struct sys_dev *list, *hba;
2832 int result=1;
2833
2834 list = find_intel_devices();
2835 if (!list) {
2836 if (verbose > 0)
2837 pr_err("IMSM_DETAIL_PLATFORM_ERROR=NO_INTEL_DEVICES\n");
2838 result = 2;
e50cf220
MN
2839 return result;
2840 }
2841
2842 for (hba = list; hba; hba = hba->next) {
9eafa1de
MN
2843 if (controller_path && (compare_paths(hba->path,controller_path) != 0))
2844 continue;
60f0f54d
PB
2845 if (!find_imsm_capability(hba) && verbose > 0) {
2846 char buf[PATH_MAX];
2847 pr_err("IMSM_DETAIL_PLATFORM_ERROR=NO_IMSM_CAPABLE_DEVICE_UNDER_%s\n",
75350d87
KF
2848 hba->type == SYS_DEV_VMD || hba->type == SYS_DEV_SATA_VMD ?
2849 vmd_domain_to_controller(hba, buf) : hba->path);
60f0f54d 2850 }
0858eccf 2851 else
e50cf220 2852 result = 0;
e50cf220
MN
2853 }
2854
5e1d6128 2855 const struct orom_entry *entry;
0858eccf 2856
60f0f54d 2857 for (entry = orom_entries; entry; entry = entry->next) {
75350d87 2858 if (entry->type == SYS_DEV_VMD || entry->type == SYS_DEV_SATA_VMD) {
60f0f54d
PB
2859 for (hba = list; hba; hba = hba->next)
2860 print_imsm_capability_export(&entry->orom);
2861 continue;
2862 }
5e1d6128 2863 print_imsm_capability_export(&entry->orom);
60f0f54d 2864 }
0858eccf 2865
e50cf220
MN
2866 return result;
2867}
2868
cdddbdbc
DW
2869static int match_home_imsm(struct supertype *st, char *homehost)
2870{
5115ca67
DW
2871 /* the imsm metadata format does not specify any host
2872 * identification information. We return -1 since we can never
2873 * confirm nor deny whether a given array is "meant" for this
148acb7b 2874 * host. We rely on compare_super and the 'family_num' fields to
5115ca67
DW
2875 * exclude member disks that do not belong, and we rely on
2876 * mdadm.conf to specify the arrays that should be assembled.
2877 * Auto-assembly may still pick up "foreign" arrays.
2878 */
cdddbdbc 2879
9362c1c8 2880 return -1;
cdddbdbc
DW
2881}
2882
2883static void uuid_from_super_imsm(struct supertype *st, int uuid[4])
2884{
51006d85
N
2885 /* The uuid returned here is used for:
2886 * uuid to put into bitmap file (Create, Grow)
2887 * uuid for backup header when saving critical section (Grow)
2888 * comparing uuids when re-adding a device into an array
2889 * In these cases the uuid required is that of the data-array,
2890 * not the device-set.
2891 * uuid to recognise same set when adding a missing device back
2892 * to an array. This is a uuid for the device-set.
1011e834 2893 *
51006d85
N
2894 * For each of these we can make do with a truncated
2895 * or hashed uuid rather than the original, as long as
2896 * everyone agrees.
2897 * In each case the uuid required is that of the data-array,
2898 * not the device-set.
43dad3d6 2899 */
51006d85
N
2900 /* imsm does not track uuid's so we synthesis one using sha1 on
2901 * - The signature (Which is constant for all imsm array, but no matter)
148acb7b 2902 * - the orig_family_num of the container
51006d85
N
2903 * - the index number of the volume
2904 * - the 'serial' number of the volume.
2905 * Hopefully these are all constant.
2906 */
2907 struct intel_super *super = st->sb;
43dad3d6 2908
51006d85
N
2909 char buf[20];
2910 struct sha1_ctx ctx;
2911 struct imsm_dev *dev = NULL;
148acb7b 2912 __u32 family_num;
51006d85 2913
148acb7b
DW
2914 /* some mdadm versions failed to set ->orig_family_num, in which
2915 * case fall back to ->family_num. orig_family_num will be
2916 * fixed up with the first metadata update.
2917 */
2918 family_num = super->anchor->orig_family_num;
2919 if (family_num == 0)
2920 family_num = super->anchor->family_num;
51006d85 2921 sha1_init_ctx(&ctx);
92bd8f8d 2922 sha1_process_bytes(super->anchor->sig, MPB_SIG_LEN, &ctx);
148acb7b 2923 sha1_process_bytes(&family_num, sizeof(__u32), &ctx);
51006d85
N
2924 if (super->current_vol >= 0)
2925 dev = get_imsm_dev(super, super->current_vol);
2926 if (dev) {
2927 __u32 vol = super->current_vol;
2928 sha1_process_bytes(&vol, sizeof(vol), &ctx);
2929 sha1_process_bytes(dev->volume, MAX_RAID_SERIAL_LEN, &ctx);
2930 }
2931 sha1_finish_ctx(&ctx, buf);
2932 memcpy(uuid, buf, 4*4);
cdddbdbc
DW
2933}
2934
1e5c6983
DW
2935static __u32 migr_strip_blocks_resync(struct imsm_dev *dev)
2936{
2937 /* migr_strip_size when repairing or initializing parity */
238c0a71 2938 struct imsm_map *map = get_imsm_map(dev, MAP_0);
1e5c6983
DW
2939 __u32 chunk = __le32_to_cpu(map->blocks_per_strip);
2940
2941 switch (get_imsm_raid_level(map)) {
2942 case 5:
2943 case 10:
2944 return chunk;
2945 default:
2946 return 128*1024 >> 9;
2947 }
2948}
2949
2950static __u32 migr_strip_blocks_rebuild(struct imsm_dev *dev)
2951{
2952 /* migr_strip_size when rebuilding a degraded disk, no idea why
2953 * this is different than migr_strip_size_resync(), but it's good
2954 * to be compatible
2955 */
238c0a71 2956 struct imsm_map *map = get_imsm_map(dev, MAP_1);
1e5c6983
DW
2957 __u32 chunk = __le32_to_cpu(map->blocks_per_strip);
2958
2959 switch (get_imsm_raid_level(map)) {
2960 case 1:
2961 case 10:
2962 if (map->num_members % map->num_domains == 0)
2963 return 128*1024 >> 9;
2964 else
2965 return chunk;
2966 case 5:
2967 return max((__u32) 64*1024 >> 9, chunk);
2968 default:
2969 return 128*1024 >> 9;
2970 }
2971}
2972
2973static __u32 num_stripes_per_unit_resync(struct imsm_dev *dev)
2974{
238c0a71
AK
2975 struct imsm_map *lo = get_imsm_map(dev, MAP_0);
2976 struct imsm_map *hi = get_imsm_map(dev, MAP_1);
1e5c6983
DW
2977 __u32 lo_chunk = __le32_to_cpu(lo->blocks_per_strip);
2978 __u32 hi_chunk = __le32_to_cpu(hi->blocks_per_strip);
2979
2980 return max((__u32) 1, hi_chunk / lo_chunk);
2981}
2982
2983static __u32 num_stripes_per_unit_rebuild(struct imsm_dev *dev)
2984{
238c0a71 2985 struct imsm_map *lo = get_imsm_map(dev, MAP_0);
1e5c6983
DW
2986 int level = get_imsm_raid_level(lo);
2987
2988 if (level == 1 || level == 10) {
238c0a71 2989 struct imsm_map *hi = get_imsm_map(dev, MAP_1);
1e5c6983
DW
2990
2991 return hi->num_domains;
2992 } else
2993 return num_stripes_per_unit_resync(dev);
2994}
2995
44490938
MD
2996static unsigned long long calc_component_size(struct imsm_map *map,
2997 struct imsm_dev *dev)
2998{
2999 unsigned long long component_size;
3000 unsigned long long dev_size = imsm_dev_size(dev);
a4f7290c 3001 long long calc_dev_size = 0;
44490938
MD
3002 unsigned int member_disks = imsm_num_data_members(map);
3003
3004 if (member_disks == 0)
3005 return 0;
3006
3007 component_size = per_dev_array_size(map);
3008 calc_dev_size = component_size * member_disks;
3009
3010 /* Component size is rounded to 1MB so difference between size from
3011 * metadata and size calculated from num_data_stripes equals up to
3012 * 2048 blocks per each device. If the difference is higher it means
3013 * that array size was expanded and num_data_stripes was not updated.
3014 */
a4f7290c 3015 if (llabs(calc_dev_size - (long long)dev_size) >
44490938
MD
3016 (1 << SECT_PER_MB_SHIFT) * member_disks) {
3017 component_size = dev_size / member_disks;
3018 dprintf("Invalid num_data_stripes in metadata; expected=%llu, found=%llu\n",
3019 component_size / map->blocks_per_strip,
3020 num_data_stripes(map));
3021 }
3022
3023 return component_size;
3024}
3025
1e5c6983
DW
3026static __u32 parity_segment_depth(struct imsm_dev *dev)
3027{
238c0a71 3028 struct imsm_map *map = get_imsm_map(dev, MAP_0);
1e5c6983
DW
3029 __u32 chunk = __le32_to_cpu(map->blocks_per_strip);
3030
3031 switch(get_imsm_raid_level(map)) {
3032 case 1:
3033 case 10:
3034 return chunk * map->num_domains;
3035 case 5:
3036 return chunk * map->num_members;
3037 default:
3038 return chunk;
3039 }
3040}
3041
3042static __u32 map_migr_block(struct imsm_dev *dev, __u32 block)
3043{
238c0a71 3044 struct imsm_map *map = get_imsm_map(dev, MAP_1);
1e5c6983
DW
3045 __u32 chunk = __le32_to_cpu(map->blocks_per_strip);
3046 __u32 strip = block / chunk;
3047
3048 switch (get_imsm_raid_level(map)) {
3049 case 1:
3050 case 10: {
3051 __u32 vol_strip = (strip * map->num_domains) + 1;
3052 __u32 vol_stripe = vol_strip / map->num_members;
3053
3054 return vol_stripe * chunk + block % chunk;
3055 } case 5: {
3056 __u32 stripe = strip / (map->num_members - 1);
3057
3058 return stripe * chunk + block % chunk;
3059 }
3060 default:
3061 return 0;
3062 }
3063}
3064
c47b0ff6
AK
3065static __u64 blocks_per_migr_unit(struct intel_super *super,
3066 struct imsm_dev *dev)
1e5c6983
DW
3067{
3068 /* calculate the conversion factor between per member 'blocks'
3069 * (md/{resync,rebuild}_start) and imsm migration units, return
3070 * 0 for the 'not migrating' and 'unsupported migration' cases
3071 */
3072 if (!dev->vol.migr_state)
3073 return 0;
3074
3075 switch (migr_type(dev)) {
c47b0ff6
AK
3076 case MIGR_GEN_MIGR: {
3077 struct migr_record *migr_rec = super->migr_rec;
3078 return __le32_to_cpu(migr_rec->blocks_per_unit);
3079 }
1e5c6983
DW
3080 case MIGR_VERIFY:
3081 case MIGR_REPAIR:
3082 case MIGR_INIT: {
238c0a71 3083 struct imsm_map *map = get_imsm_map(dev, MAP_0);
1e5c6983
DW
3084 __u32 stripes_per_unit;
3085 __u32 blocks_per_unit;
3086 __u32 parity_depth;
3087 __u32 migr_chunk;
3088 __u32 block_map;
3089 __u32 block_rel;
3090 __u32 segment;
3091 __u32 stripe;
3092 __u8 disks;
3093
3094 /* yes, this is really the translation of migr_units to
3095 * per-member blocks in the 'resync' case
3096 */
3097 stripes_per_unit = num_stripes_per_unit_resync(dev);
3098 migr_chunk = migr_strip_blocks_resync(dev);
9529d343 3099 disks = imsm_num_data_members(map);
1e5c6983 3100 blocks_per_unit = stripes_per_unit * migr_chunk * disks;
7b1ab482 3101 stripe = __le16_to_cpu(map->blocks_per_strip) * disks;
1e5c6983
DW
3102 segment = blocks_per_unit / stripe;
3103 block_rel = blocks_per_unit - segment * stripe;
3104 parity_depth = parity_segment_depth(dev);
3105 block_map = map_migr_block(dev, block_rel);
3106 return block_map + parity_depth * segment;
3107 }
3108 case MIGR_REBUILD: {
3109 __u32 stripes_per_unit;
3110 __u32 migr_chunk;
3111
3112 stripes_per_unit = num_stripes_per_unit_rebuild(dev);
3113 migr_chunk = migr_strip_blocks_rebuild(dev);
3114 return migr_chunk * stripes_per_unit;
3115 }
1e5c6983
DW
3116 case MIGR_STATE_CHANGE:
3117 default:
3118 return 0;
3119 }
3120}
3121
c2c087e6
DW
3122static int imsm_level_to_layout(int level)
3123{
3124 switch (level) {
3125 case 0:
3126 case 1:
3127 return 0;
3128 case 5:
3129 case 6:
a380c027 3130 return ALGORITHM_LEFT_ASYMMETRIC;
c2c087e6 3131 case 10:
c92a2527 3132 return 0x102;
c2c087e6 3133 }
a18a888e 3134 return UnSet;
c2c087e6
DW
3135}
3136
8e59f3d8
AK
3137/*******************************************************************************
3138 * Function: read_imsm_migr_rec
3139 * Description: Function reads imsm migration record from last sector of disk
3140 * Parameters:
3141 * fd : disk descriptor
3142 * super : metadata info
3143 * Returns:
3144 * 0 : success,
3145 * -1 : fail
3146 ******************************************************************************/
3147static int read_imsm_migr_rec(int fd, struct intel_super *super)
3148{
3149 int ret_val = -1;
de44e46f 3150 unsigned int sector_size = super->sector_size;
8e59f3d8
AK
3151 unsigned long long dsize;
3152
3153 get_dev_size(fd, NULL, &dsize);
de44e46f
PB
3154 if (lseek64(fd, dsize - (sector_size*MIGR_REC_SECTOR_POSITION),
3155 SEEK_SET) < 0) {
e7b84f9d
N
3156 pr_err("Cannot seek to anchor block: %s\n",
3157 strerror(errno));
8e59f3d8
AK
3158 goto out;
3159 }
466070ad 3160 if ((unsigned int)read(fd, super->migr_rec_buf,
de44e46f
PB
3161 MIGR_REC_BUF_SECTORS*sector_size) !=
3162 MIGR_REC_BUF_SECTORS*sector_size) {
e7b84f9d
N
3163 pr_err("Cannot read migr record block: %s\n",
3164 strerror(errno));
8e59f3d8
AK
3165 goto out;
3166 }
3167 ret_val = 0;
de44e46f
PB
3168 if (sector_size == 4096)
3169 convert_from_4k_imsm_migr_rec(super);
8e59f3d8
AK
3170
3171out:
3172 return ret_val;
3173}
3174
3136abe5
AK
3175static struct imsm_dev *imsm_get_device_during_migration(
3176 struct intel_super *super)
3177{
3178
3179 struct intel_dev *dv;
3180
3181 for (dv = super->devlist; dv; dv = dv->next) {
3182 if (is_gen_migration(dv->dev))
3183 return dv->dev;
3184 }
3185 return NULL;
3186}
3187
8e59f3d8
AK
3188/*******************************************************************************
3189 * Function: load_imsm_migr_rec
3190 * Description: Function reads imsm migration record (it is stored at the last
3191 * sector of disk)
3192 * Parameters:
3193 * super : imsm internal array info
8e59f3d8
AK
3194 * Returns:
3195 * 0 : success
3196 * -1 : fail
4c965cc9 3197 * -2 : no migration in progress
8e59f3d8 3198 ******************************************************************************/
2f86fda3 3199static int load_imsm_migr_rec(struct intel_super *super)
8e59f3d8 3200{
594dc1b8 3201 struct dl *dl;
8e59f3d8
AK
3202 char nm[30];
3203 int retval = -1;
3204 int fd = -1;
3136abe5 3205 struct imsm_dev *dev;
594dc1b8 3206 struct imsm_map *map;
b4ab44d8 3207 int slot = -1;
2f86fda3 3208 int keep_fd = 1;
3136abe5
AK
3209
3210 /* find map under migration */
3211 dev = imsm_get_device_during_migration(super);
3212 /* nothing to load,no migration in progress?
3213 */
3214 if (dev == NULL)
4c965cc9 3215 return -2;
8e59f3d8 3216
2f86fda3
MT
3217 map = get_imsm_map(dev, MAP_0);
3218 if (!map)
3219 return -1;
3136abe5 3220
2f86fda3
MT
3221 for (dl = super->disks; dl; dl = dl->next) {
3222 /* skip spare and failed disks
3223 */
3224 if (dl->index < 0)
3225 continue;
3226 /* read only from one of the first two slots
3227 */
3228 slot = get_imsm_disk_slot(map, dl->index);
3229 if (slot > 1 || slot < 0)
3230 continue;
3231
4389ce73 3232 if (!is_fd_valid(dl->fd)) {
8e59f3d8
AK
3233 sprintf(nm, "%d:%d", dl->major, dl->minor);
3234 fd = dev_open(nm, O_RDONLY);
4389ce73
MT
3235
3236 if (is_fd_valid(fd)) {
2f86fda3 3237 keep_fd = 0;
8e59f3d8 3238 break;
2f86fda3
MT
3239 }
3240 } else {
3241 fd = dl->fd;
3242 break;
8e59f3d8
AK
3243 }
3244 }
2f86fda3 3245
4389ce73 3246 if (!is_fd_valid(fd))
2f86fda3 3247 return retval;
8e59f3d8 3248 retval = read_imsm_migr_rec(fd, super);
2f86fda3 3249 if (!keep_fd)
8e59f3d8 3250 close(fd);
2f86fda3 3251
8e59f3d8
AK
3252 return retval;
3253}
3254
c17608ea
AK
3255/*******************************************************************************
3256 * function: imsm_create_metadata_checkpoint_update
3257 * Description: It creates update for checkpoint change.
3258 * Parameters:
3259 * super : imsm internal array info
3260 * u : pointer to prepared update
3261 * Returns:
3262 * Uptate length.
3263 * If length is equal to 0, input pointer u contains no update
3264 ******************************************************************************/
3265static int imsm_create_metadata_checkpoint_update(
3266 struct intel_super *super,
3267 struct imsm_update_general_migration_checkpoint **u)
3268{
3269
3270 int update_memory_size = 0;
3271
1ade5cc1 3272 dprintf("(enter)\n");
c17608ea
AK
3273
3274 if (u == NULL)
3275 return 0;
3276 *u = NULL;
3277
3278 /* size of all update data without anchor */
3279 update_memory_size =
3280 sizeof(struct imsm_update_general_migration_checkpoint);
3281
503975b9 3282 *u = xcalloc(1, update_memory_size);
c17608ea 3283 if (*u == NULL) {
1ade5cc1 3284 dprintf("error: cannot get memory\n");
c17608ea
AK
3285 return 0;
3286 }
3287 (*u)->type = update_general_migration_checkpoint;
9f421827 3288 (*u)->curr_migr_unit = current_migr_unit(super->migr_rec);
83b3de77 3289 dprintf("prepared for %llu\n", (unsigned long long)(*u)->curr_migr_unit);
c17608ea
AK
3290
3291 return update_memory_size;
3292}
3293
c17608ea
AK
3294static void imsm_update_metadata_locally(struct supertype *st,
3295 void *buf, int len);
3296
687629c2
AK
3297/*******************************************************************************
3298 * Function: write_imsm_migr_rec
3299 * Description: Function writes imsm migration record
3300 * (at the last sector of disk)
3301 * Parameters:
3302 * super : imsm internal array info
3303 * Returns:
3304 * 0 : success
3305 * -1 : if fail
3306 ******************************************************************************/
3307static int write_imsm_migr_rec(struct supertype *st)
3308{
3309 struct intel_super *super = st->sb;
de44e46f 3310 unsigned int sector_size = super->sector_size;
687629c2 3311 unsigned long long dsize;
687629c2
AK
3312 int retval = -1;
3313 struct dl *sd;
c17608ea
AK
3314 int len;
3315 struct imsm_update_general_migration_checkpoint *u;
3136abe5 3316 struct imsm_dev *dev;
594dc1b8 3317 struct imsm_map *map;
3136abe5
AK
3318
3319 /* find map under migration */
3320 dev = imsm_get_device_during_migration(super);
3321 /* if no migration, write buffer anyway to clear migr_record
3322 * on disk based on first available device
3323 */
3324 if (dev == NULL)
3325 dev = get_imsm_dev(super, super->current_vol < 0 ? 0 :
3326 super->current_vol);
3327
44bfe6df 3328 map = get_imsm_map(dev, MAP_0);
687629c2 3329
de44e46f
PB
3330 if (sector_size == 4096)
3331 convert_to_4k_imsm_migr_rec(super);
687629c2 3332 for (sd = super->disks ; sd ; sd = sd->next) {
b4ab44d8 3333 int slot = -1;
3136abe5
AK
3334
3335 /* skip failed and spare devices */
3336 if (sd->index < 0)
3337 continue;
687629c2 3338 /* write to 2 first slots only */
3136abe5
AK
3339 if (map)
3340 slot = get_imsm_disk_slot(map, sd->index);
089f9d79 3341 if (map == NULL || slot > 1 || slot < 0)
687629c2 3342 continue;
3136abe5 3343
2f86fda3
MT
3344 get_dev_size(sd->fd, NULL, &dsize);
3345 if (lseek64(sd->fd, dsize - (MIGR_REC_SECTOR_POSITION *
3346 sector_size),
de44e46f 3347 SEEK_SET) < 0) {
e7b84f9d
N
3348 pr_err("Cannot seek to anchor block: %s\n",
3349 strerror(errno));
687629c2
AK
3350 goto out;
3351 }
2f86fda3 3352 if ((unsigned int)write(sd->fd, super->migr_rec_buf,
de44e46f
PB
3353 MIGR_REC_BUF_SECTORS*sector_size) !=
3354 MIGR_REC_BUF_SECTORS*sector_size) {
e7b84f9d
N
3355 pr_err("Cannot write migr record block: %s\n",
3356 strerror(errno));
687629c2
AK
3357 goto out;
3358 }
687629c2 3359 }
de44e46f
PB
3360 if (sector_size == 4096)
3361 convert_from_4k_imsm_migr_rec(super);
c17608ea
AK
3362 /* update checkpoint information in metadata */
3363 len = imsm_create_metadata_checkpoint_update(super, &u);
c17608ea
AK
3364 if (len <= 0) {
3365 dprintf("imsm: Cannot prepare update\n");
3366 goto out;
3367 }
3368 /* update metadata locally */
3369 imsm_update_metadata_locally(st, u, len);
3370 /* and possibly remotely */
3371 if (st->update_tail) {
3372 append_metadata_update(st, u, len);
3373 /* during reshape we do all work inside metadata handler
3374 * manage_reshape(), so metadata update has to be triggered
3375 * insida it
3376 */
3377 flush_metadata_updates(st);
3378 st->update_tail = &st->updates;
3379 } else
3380 free(u);
687629c2
AK
3381
3382 retval = 0;
3383 out:
687629c2
AK
3384 return retval;
3385}
3386
e2962bfc
AK
3387/* spare/missing disks activations are not allowe when
3388 * array/container performs reshape operation, because
3389 * all arrays in container works on the same disks set
3390 */
3391int imsm_reshape_blocks_arrays_changes(struct intel_super *super)
3392{
3393 int rv = 0;
3394 struct intel_dev *i_dev;
3395 struct imsm_dev *dev;
3396
3397 /* check whole container
3398 */
3399 for (i_dev = super->devlist; i_dev; i_dev = i_dev->next) {
3400 dev = i_dev->dev;
3ad25638 3401 if (is_gen_migration(dev)) {
e2962bfc
AK
3402 /* No repair during any migration in container
3403 */
3404 rv = 1;
3405 break;
3406 }
3407 }
3408 return rv;
3409}
3e684231 3410static unsigned long long imsm_component_size_alignment_check(int level,
c41e00b2 3411 int chunk_size,
f36a9ecd 3412 unsigned int sector_size,
c41e00b2
AK
3413 unsigned long long component_size)
3414{
3e684231 3415 unsigned int component_size_alignment;
c41e00b2 3416
3e684231 3417 /* check component size alignment
c41e00b2 3418 */
3e684231 3419 component_size_alignment = component_size % (chunk_size/sector_size);
c41e00b2 3420
3e684231 3421 dprintf("(Level: %i, chunk_size = %i, component_size = %llu), component_size_alignment = %u\n",
c41e00b2 3422 level, chunk_size, component_size,
3e684231 3423 component_size_alignment);
c41e00b2 3424
3e684231
MZ
3425 if (component_size_alignment && (level != 1) && (level != UnSet)) {
3426 dprintf("imsm: reported component size aligned from %llu ",
c41e00b2 3427 component_size);
3e684231 3428 component_size -= component_size_alignment;
1ade5cc1 3429 dprintf_cont("to %llu (%i).\n",
3e684231 3430 component_size, component_size_alignment);
c41e00b2
AK
3431 }
3432
3433 return component_size;
3434}
e2962bfc 3435
fbc42556
JR
3436/*******************************************************************************
3437 * Function: get_bitmap_header_sector
3438 * Description: Returns the sector where the bitmap header is placed.
3439 * Parameters:
3440 * st : supertype information
3441 * dev_idx : index of the device with bitmap
3442 *
3443 * Returns:
3444 * The sector where the bitmap header is placed
3445 ******************************************************************************/
3446static unsigned long long get_bitmap_header_sector(struct intel_super *super,
3447 int dev_idx)
3448{
3449 struct imsm_dev *dev = get_imsm_dev(super, dev_idx);
3450 struct imsm_map *map = get_imsm_map(dev, MAP_0);
3451
3452 if (!super->sector_size) {
3453 dprintf("sector size is not set\n");
3454 return 0;
3455 }
3456
3457 return pba_of_lba0(map) + calc_component_size(map, dev) +
3458 (IMSM_BITMAP_HEADER_OFFSET / super->sector_size);
3459}
3460
3461/*******************************************************************************
3462 * Function: get_bitmap_sector
3463 * Description: Returns the sector where the bitmap is placed.
3464 * Parameters:
3465 * st : supertype information
3466 * dev_idx : index of the device with bitmap
3467 *
3468 * Returns:
3469 * The sector where the bitmap is placed
3470 ******************************************************************************/
3471static unsigned long long get_bitmap_sector(struct intel_super *super,
3472 int dev_idx)
3473{
3474 if (!super->sector_size) {
3475 dprintf("sector size is not set\n");
3476 return 0;
3477 }
3478
3479 return get_bitmap_header_sector(super, dev_idx) +
3480 (IMSM_BITMAP_HEADER_SIZE / super->sector_size);
3481}
3482
2432ce9b
AP
3483static unsigned long long get_ppl_sector(struct intel_super *super, int dev_idx)
3484{
3485 struct imsm_dev *dev = get_imsm_dev(super, dev_idx);
3486 struct imsm_map *map = get_imsm_map(dev, MAP_0);
3487
3488 return pba_of_lba0(map) +
3489 (num_data_stripes(map) * map->blocks_per_strip);
3490}
3491
a5d85af7 3492static void getinfo_super_imsm_volume(struct supertype *st, struct mdinfo *info, char *dmap)
bf5a934a
DW
3493{
3494 struct intel_super *super = st->sb;
c47b0ff6 3495 struct migr_record *migr_rec = super->migr_rec;
949c47a0 3496 struct imsm_dev *dev = get_imsm_dev(super, super->current_vol);
238c0a71
AK
3497 struct imsm_map *map = get_imsm_map(dev, MAP_0);
3498 struct imsm_map *prev_map = get_imsm_map(dev, MAP_1);
b335e593 3499 struct imsm_map *map_to_analyse = map;
efb30e7f 3500 struct dl *dl;
a5d85af7 3501 int map_disks = info->array.raid_disks;
bf5a934a 3502
95eeceeb 3503 memset(info, 0, sizeof(*info));
b335e593
AK
3504 if (prev_map)
3505 map_to_analyse = prev_map;
3506
ca0748fa 3507 dl = super->current_disk;
9894ec0d 3508
bf5a934a 3509 info->container_member = super->current_vol;
cd0430a1 3510 info->array.raid_disks = map->num_members;
b335e593 3511 info->array.level = get_imsm_raid_level(map_to_analyse);
bf5a934a
DW
3512 info->array.layout = imsm_level_to_layout(info->array.level);
3513 info->array.md_minor = -1;
3514 info->array.ctime = 0;
3515 info->array.utime = 0;
b335e593
AK
3516 info->array.chunk_size =
3517 __le16_to_cpu(map_to_analyse->blocks_per_strip) << 9;
2432ce9b 3518 info->array.state = !(dev->vol.dirty & RAIDVOL_DIRTY);
fcc2c9da 3519 info->custom_array_size = imsm_dev_size(dev);
3ad25638
AK
3520 info->recovery_blocked = imsm_reshape_blocks_arrays_changes(st->sb);
3521
3f510843 3522 if (is_gen_migration(dev)) {
195d1d76
PP
3523 /*
3524 * device prev_map should be added if it is in the middle
3525 * of migration
3526 */
3527 assert(prev_map);
3528
3f83228a 3529 info->reshape_active = 1;
b335e593
AK
3530 info->new_level = get_imsm_raid_level(map);
3531 info->new_layout = imsm_level_to_layout(info->new_level);
3532 info->new_chunk = __le16_to_cpu(map->blocks_per_strip) << 9;
3f83228a 3533 info->delta_disks = map->num_members - prev_map->num_members;
493f5dd6
N
3534 if (info->delta_disks) {
3535 /* this needs to be applied to every array
3536 * in the container.
3537 */
81219e70 3538 info->reshape_active = CONTAINER_RESHAPE;
493f5dd6 3539 }
3f83228a
N
3540 /* We shape information that we give to md might have to be
3541 * modify to cope with md's requirement for reshaping arrays.
3542 * For example, when reshaping a RAID0, md requires it to be
3543 * presented as a degraded RAID4.
3544 * Also if a RAID0 is migrating to a RAID5 we need to specify
3545 * the array as already being RAID5, but the 'before' layout
3546 * is a RAID4-like layout.
3547 */
3548 switch (info->array.level) {
3549 case 0:
3550 switch(info->new_level) {
3551 case 0:
3552 /* conversion is happening as RAID4 */
3553 info->array.level = 4;
3554 info->array.raid_disks += 1;
3555 break;
3556 case 5:
3557 /* conversion is happening as RAID5 */
3558 info->array.level = 5;
3559 info->array.layout = ALGORITHM_PARITY_N;
3f83228a
N
3560 info->delta_disks -= 1;
3561 break;
3562 default:
3563 /* FIXME error message */
3564 info->array.level = UnSet;
3565 break;
3566 }
3567 break;
3568 }
b335e593
AK
3569 } else {
3570 info->new_level = UnSet;
3571 info->new_layout = UnSet;
3572 info->new_chunk = info->array.chunk_size;
3f83228a 3573 info->delta_disks = 0;
b335e593 3574 }
ca0748fa 3575
efb30e7f
DW
3576 if (dl) {
3577 info->disk.major = dl->major;
3578 info->disk.minor = dl->minor;
ca0748fa 3579 info->disk.number = dl->index;
656b6b5a
N
3580 info->disk.raid_disk = get_imsm_disk_slot(map_to_analyse,
3581 dl->index);
efb30e7f 3582 }
bf5a934a 3583
5551b113 3584 info->data_offset = pba_of_lba0(map_to_analyse);
44490938 3585 info->component_size = calc_component_size(map, dev);
3e684231 3586 info->component_size = imsm_component_size_alignment_check(
c41e00b2
AK
3587 info->array.level,
3588 info->array.chunk_size,
f36a9ecd 3589 super->sector_size,
c41e00b2 3590 info->component_size);
5e46202e 3591 info->bb.supported = 1;
139dae11 3592
301406c9 3593 memset(info->uuid, 0, sizeof(info->uuid));
921d9e16 3594 info->recovery_start = MaxSector;
bf5a934a 3595
c2462068
PB
3596 if (info->array.level == 5 &&
3597 (dev->rwh_policy == RWH_DISTRIBUTED ||
3598 dev->rwh_policy == RWH_MULTIPLE_DISTRIBUTED)) {
2432ce9b
AP
3599 info->consistency_policy = CONSISTENCY_POLICY_PPL;
3600 info->ppl_sector = get_ppl_sector(super, super->current_vol);
c2462068
PB
3601 if (dev->rwh_policy == RWH_MULTIPLE_DISTRIBUTED)
3602 info->ppl_size = MULTIPLE_PPL_AREA_SIZE_IMSM >> 9;
3603 else
3604 info->ppl_size = (PPL_HEADER_SIZE + PPL_ENTRY_SPACE)
3605 >> 9;
2432ce9b
AP
3606 } else if (info->array.level <= 0) {
3607 info->consistency_policy = CONSISTENCY_POLICY_NONE;
3608 } else {
fbc42556
JR
3609 if (dev->rwh_policy == RWH_BITMAP) {
3610 info->bitmap_offset = get_bitmap_sector(super, super->current_vol);
3611 info->consistency_policy = CONSISTENCY_POLICY_BITMAP;
3612 } else {
3613 info->consistency_policy = CONSISTENCY_POLICY_RESYNC;
3614 }
2432ce9b
AP
3615 }
3616
d2e6d5d6 3617 info->reshape_progress = 0;
b6796ce1 3618 info->resync_start = MaxSector;
b9172665 3619 if ((map_to_analyse->map_state == IMSM_T_STATE_UNINITIALIZED ||
2432ce9b 3620 !(info->array.state & 1)) &&
b9172665 3621 imsm_reshape_blocks_arrays_changes(super) == 0) {
301406c9 3622 info->resync_start = 0;
b6796ce1
AK
3623 }
3624 if (dev->vol.migr_state) {
1e5c6983
DW
3625 switch (migr_type(dev)) {
3626 case MIGR_REPAIR:
3627 case MIGR_INIT: {
c47b0ff6
AK
3628 __u64 blocks_per_unit = blocks_per_migr_unit(super,
3629 dev);
4036e7ee 3630 __u64 units = vol_curr_migr_unit(dev);
1e5c6983
DW
3631
3632 info->resync_start = blocks_per_unit * units;
3633 break;
3634 }
d2e6d5d6 3635 case MIGR_GEN_MIGR: {
c47b0ff6
AK
3636 __u64 blocks_per_unit = blocks_per_migr_unit(super,
3637 dev);
9f421827 3638 __u64 units = current_migr_unit(migr_rec);
04fa9523 3639 int used_disks;
d2e6d5d6 3640
befb629b
AK
3641 if (__le32_to_cpu(migr_rec->ascending_migr) &&
3642 (units <
9f421827 3643 (get_num_migr_units(migr_rec)-1)) &&
befb629b
AK
3644 (super->migr_rec->rec_status ==
3645 __cpu_to_le32(UNIT_SRC_IN_CP_AREA)))
3646 units++;
3647
d2e6d5d6 3648 info->reshape_progress = blocks_per_unit * units;
6289d1e0 3649
7a862a02 3650 dprintf("IMSM: General Migration checkpoint : %llu (%llu) -> read reshape progress : %llu\n",
19986c72
MB
3651 (unsigned long long)units,
3652 (unsigned long long)blocks_per_unit,
3653 info->reshape_progress);
75156c46 3654
9529d343 3655 used_disks = imsm_num_data_members(prev_map);
75156c46 3656 if (used_disks > 0) {
895ffd99 3657 info->custom_array_size = per_dev_array_size(map) *
75156c46 3658 used_disks;
75156c46 3659 }
d2e6d5d6 3660 }
1e5c6983
DW
3661 case MIGR_VERIFY:
3662 /* we could emulate the checkpointing of
3663 * 'sync_action=check' migrations, but for now
3664 * we just immediately complete them
3665 */
3666 case MIGR_REBUILD:
3667 /* this is handled by container_content_imsm() */
1e5c6983
DW
3668 case MIGR_STATE_CHANGE:
3669 /* FIXME handle other migrations */
3670 default:
3671 /* we are not dirty, so... */
3672 info->resync_start = MaxSector;
3673 }
b6796ce1 3674 }
301406c9
DW
3675
3676 strncpy(info->name, (char *) dev->volume, MAX_RAID_SERIAL_LEN);
3677 info->name[MAX_RAID_SERIAL_LEN] = 0;
bf5a934a 3678
f35f2525
N
3679 info->array.major_version = -1;
3680 info->array.minor_version = -2;
4dd2df09 3681 sprintf(info->text_version, "/%s/%d", st->container_devnm, info->container_member);
a67dd8cc 3682 info->safe_mode_delay = 4000; /* 4 secs like the Matrix driver */
51006d85 3683 uuid_from_super_imsm(st, info->uuid);
a5d85af7
N
3684
3685 if (dmap) {
3686 int i, j;
3687 for (i=0; i<map_disks; i++) {
3688 dmap[i] = 0;
3689 if (i < info->array.raid_disks) {
3690 struct imsm_disk *dsk;
238c0a71 3691 j = get_imsm_disk_idx(dev, i, MAP_X);
a5d85af7
N
3692 dsk = get_imsm_disk(super, j);
3693 if (dsk && (dsk->status & CONFIGURED_DISK))
3694 dmap[i] = 1;
3695 }
3696 }
3697 }
81ac8b4d 3698}
bf5a934a 3699
3b451610
AK
3700static __u8 imsm_check_degraded(struct intel_super *super, struct imsm_dev *dev,
3701 int failed, int look_in_map);
3702
3703static int imsm_count_failed(struct intel_super *super, struct imsm_dev *dev,
3704 int look_in_map);
3705
3706static void manage_second_map(struct intel_super *super, struct imsm_dev *dev)
3707{
3708 if (is_gen_migration(dev)) {
3709 int failed;
3710 __u8 map_state;
3711 struct imsm_map *map2 = get_imsm_map(dev, MAP_1);
3712
3713 failed = imsm_count_failed(super, dev, MAP_1);
238c0a71 3714 map_state = imsm_check_degraded(super, dev, failed, MAP_1);
3b451610
AK
3715 if (map2->map_state != map_state) {
3716 map2->map_state = map_state;
3717 super->updates_pending++;
3718 }
3719 }
3720}
97b4d0e9
DW
3721
3722static struct imsm_disk *get_imsm_missing(struct intel_super *super, __u8 index)
3723{
3724 struct dl *d;
3725
3726 for (d = super->missing; d; d = d->next)
3727 if (d->index == index)
3728 return &d->disk;
3729 return NULL;
3730}
3731
a5d85af7 3732static void getinfo_super_imsm(struct supertype *st, struct mdinfo *info, char *map)
4f5bc454
DW
3733{
3734 struct intel_super *super = st->sb;
4f5bc454 3735 struct imsm_disk *disk;
a5d85af7 3736 int map_disks = info->array.raid_disks;
476b00bd 3737 int max_enough = -1;
ab3cb6b3
N
3738 int i;
3739 struct imsm_super *mpb;
4f5bc454 3740
bf5a934a 3741 if (super->current_vol >= 0) {
a5d85af7 3742 getinfo_super_imsm_volume(st, info, map);
bf5a934a
DW
3743 return;
3744 }
95eeceeb 3745 memset(info, 0, sizeof(*info));
d23fe947
DW
3746
3747 /* Set raid_disks to zero so that Assemble will always pull in valid
3748 * spares
3749 */
3750 info->array.raid_disks = 0;
cdddbdbc
DW
3751 info->array.level = LEVEL_CONTAINER;
3752 info->array.layout = 0;
3753 info->array.md_minor = -1;
1011e834 3754 info->array.ctime = 0; /* N/A for imsm */
cdddbdbc
DW
3755 info->array.utime = 0;
3756 info->array.chunk_size = 0;
3757
3758 info->disk.major = 0;
3759 info->disk.minor = 0;
cdddbdbc 3760 info->disk.raid_disk = -1;
c2c087e6 3761 info->reshape_active = 0;
f35f2525
N
3762 info->array.major_version = -1;
3763 info->array.minor_version = -2;
c2c087e6 3764 strcpy(info->text_version, "imsm");
a67dd8cc 3765 info->safe_mode_delay = 0;
c2c087e6
DW
3766 info->disk.number = -1;
3767 info->disk.state = 0;
c5afc314 3768 info->name[0] = 0;
921d9e16 3769 info->recovery_start = MaxSector;
3ad25638 3770 info->recovery_blocked = imsm_reshape_blocks_arrays_changes(st->sb);
5e46202e 3771 info->bb.supported = 1;
c2c087e6 3772
97b4d0e9 3773 /* do we have the all the insync disks that we expect? */
ab3cb6b3 3774 mpb = super->anchor;
b7d81a38 3775 info->events = __le32_to_cpu(mpb->generation_num);
97b4d0e9 3776
ab3cb6b3
N
3777 for (i = 0; i < mpb->num_raid_devs; i++) {
3778 struct imsm_dev *dev = get_imsm_dev(super, i);
476b00bd 3779 int failed, enough, j, missing = 0;
ab3cb6b3 3780 struct imsm_map *map;
476b00bd 3781 __u8 state;
97b4d0e9 3782
476b00bd
MT
3783 failed = imsm_count_failed(super, dev, MAP_0);
3784 state = imsm_check_degraded(super, dev, failed, MAP_0);
238c0a71 3785 map = get_imsm_map(dev, MAP_0);
ab3cb6b3
N
3786
3787 /* any newly missing disks?
3788 * (catches single-degraded vs double-degraded)
3789 */
3790 for (j = 0; j < map->num_members; j++) {
238c0a71 3791 __u32 ord = get_imsm_ord_tbl_ent(dev, j, MAP_0);
ab3cb6b3
N
3792 __u32 idx = ord_to_idx(ord);
3793
20dc76d1
MT
3794 if (super->disks && super->disks->index == (int)idx)
3795 info->disk.raid_disk = j;
3796
ab3cb6b3
N
3797 if (!(ord & IMSM_ORD_REBUILD) &&
3798 get_imsm_missing(super, idx)) {
476b00bd 3799 missing = 1;
ab3cb6b3
N
3800 break;
3801 }
97b4d0e9 3802 }
476b00bd
MT
3803
3804 if (state == IMSM_T_STATE_FAILED)
3805 enough = -1;
3806 else if (state == IMSM_T_STATE_DEGRADED &&
3807 (state != map->map_state || missing))
3808 enough = 0;
3809 else /* we're normal, or already degraded */
3810 enough = 1;
3811 if (is_gen_migration(dev) && missing) {
3812 /* during general migration we need all disks
3813 * that process is running on.
3814 * No new missing disk is allowed.
3815 */
3816 max_enough = -1;
3817 enough = -1;
3818 /* no more checks necessary
3819 */
3820 break;
3821 }
3822 /* in the missing/failed disk case check to see
3823 * if at least one array is runnable
3824 */
3825 max_enough = max(max_enough, enough);
ab3cb6b3 3826 }
97b4d0e9 3827
476b00bd
MT
3828 info->container_enough = max_enough;
3829
4a04ec6c 3830 if (super->disks) {
14e8215b
DW
3831 __u32 reserved = imsm_reserved_sectors(super, super->disks);
3832
b9f594fe 3833 disk = &super->disks->disk;
5551b113 3834 info->data_offset = total_blocks(&super->disks->disk) - reserved;
14e8215b 3835 info->component_size = reserved;
25ed7e59 3836 info->disk.state = is_configured(disk) ? (1 << MD_DISK_ACTIVE) : 0;
df474657
DW
3837 /* we don't change info->disk.raid_disk here because
3838 * this state will be finalized in mdmon after we have
3839 * found the 'most fresh' version of the metadata
3840 */
25ed7e59 3841 info->disk.state |= is_failed(disk) ? (1 << MD_DISK_FAULTY) : 0;
2432ce9b
AP
3842 info->disk.state |= (is_spare(disk) || is_journal(disk)) ?
3843 0 : (1 << MD_DISK_SYNC);
cdddbdbc 3844 }
a575e2a7
DW
3845
3846 /* only call uuid_from_super_imsm when this disk is part of a populated container,
3847 * ->compare_super may have updated the 'num_raid_devs' field for spares
3848 */
3849 if (info->disk.state & (1 << MD_DISK_SYNC) || super->anchor->num_raid_devs)
36ba7d48 3850 uuid_from_super_imsm(st, info->uuid);
22e263f6
AC
3851 else
3852 memcpy(info->uuid, uuid_zero, sizeof(uuid_zero));
a5d85af7
N
3853
3854 /* I don't know how to compute 'map' on imsm, so use safe default */
3855 if (map) {
3856 int i;
3857 for (i = 0; i < map_disks; i++)
3858 map[i] = 1;
3859 }
3860
cdddbdbc
DW
3861}
3862
5c4cd5da
AC
3863/* allocates memory and fills disk in mdinfo structure
3864 * for each disk in array */
3865struct mdinfo *getinfo_super_disks_imsm(struct supertype *st)
3866{
594dc1b8 3867 struct mdinfo *mddev;
5c4cd5da
AC
3868 struct intel_super *super = st->sb;
3869 struct imsm_disk *disk;
3870 int count = 0;
3871 struct dl *dl;
3872 if (!super || !super->disks)
3873 return NULL;
3874 dl = super->disks;
503975b9 3875 mddev = xcalloc(1, sizeof(*mddev));
5c4cd5da
AC
3876 while (dl) {
3877 struct mdinfo *tmp;
3878 disk = &dl->disk;
503975b9 3879 tmp = xcalloc(1, sizeof(*tmp));
5c4cd5da
AC
3880 if (mddev->devs)
3881 tmp->next = mddev->devs;
3882 mddev->devs = tmp;
3883 tmp->disk.number = count++;
3884 tmp->disk.major = dl->major;
3885 tmp->disk.minor = dl->minor;
3886 tmp->disk.state = is_configured(disk) ?
3887 (1 << MD_DISK_ACTIVE) : 0;
3888 tmp->disk.state |= is_failed(disk) ? (1 << MD_DISK_FAULTY) : 0;
3889 tmp->disk.state |= is_spare(disk) ? 0 : (1 << MD_DISK_SYNC);
3890 tmp->disk.raid_disk = -1;
3891 dl = dl->next;
3892 }
3893 return mddev;
3894}
3895
cdddbdbc 3896static int update_super_imsm(struct supertype *st, struct mdinfo *info,
03312b52
MK
3897 enum update_opt update, char *devname,
3898 int verbose, int uuid_set, char *homehost)
cdddbdbc 3899{
f352c545
DW
3900 /* For 'assemble' and 'force' we need to return non-zero if any
3901 * change was made. For others, the return value is ignored.
3902 * Update options are:
3903 * force-one : This device looks a bit old but needs to be included,
3904 * update age info appropriately.
3905 * assemble: clear any 'faulty' flag to allow this device to
3906 * be assembled.
3907 * force-array: Array is degraded but being forced, mark it clean
3908 * if that will be needed to assemble it.
3909 *
3910 * newdev: not used ????
3911 * grow: Array has gained a new device - this is currently for
3912 * linear only
3913 * resync: mark as dirty so a resync will happen.
3914 * name: update the name - preserving the homehost
6e46bf34 3915 * uuid: Change the uuid of the array to match watch is given
f352c545
DW
3916 *
3917 * Following are not relevant for this imsm:
3918 * sparc2.2 : update from old dodgey metadata
3919 * super-minor: change the preferred_minor number
3920 * summaries: update redundant counters.
f352c545
DW
3921 * homehost: update the recorded homehost
3922 * _reshape_progress: record new reshape_progress position.
3923 */
6e46bf34
DW
3924 int rv = 1;
3925 struct intel_super *super = st->sb;
3926 struct imsm_super *mpb;
f352c545 3927
6e46bf34
DW
3928 /* we can only update container info */
3929 if (!super || super->current_vol >= 0 || !super->anchor)
3930 return 1;
3931
3932 mpb = super->anchor;
3933
03312b52 3934 switch (update) {
4345e135 3935 case UOPT_UUID:
81a5b4f5
N
3936 /* We take this to mean that the family_num should be updated.
3937 * However that is much smaller than the uuid so we cannot really
3938 * allow an explicit uuid to be given. And it is hard to reliably
3939 * know if one was.
3940 * So if !uuid_set we know the current uuid is random and just used
3941 * the first 'int' and copy it to the other 3 positions.
3942 * Otherwise we require the 4 'int's to be the same as would be the
3943 * case if we are using a random uuid. So an explicit uuid will be
3944 * accepted as long as all for ints are the same... which shouldn't hurt
6e46bf34 3945 */
81a5b4f5
N
3946 if (!uuid_set) {
3947 info->uuid[1] = info->uuid[2] = info->uuid[3] = info->uuid[0];
6e46bf34 3948 rv = 0;
81a5b4f5
N
3949 } else {
3950 if (info->uuid[0] != info->uuid[1] ||
3951 info->uuid[1] != info->uuid[2] ||
3952 info->uuid[2] != info->uuid[3])
3953 rv = -1;
3954 else
3955 rv = 0;
6e46bf34 3956 }
81a5b4f5
N
3957 if (rv == 0)
3958 mpb->orig_family_num = info->uuid[0];
4345e135
MK
3959 break;
3960 case UOPT_SPEC_ASSEMBLE:
6e46bf34 3961 rv = 0;
4345e135
MK
3962 break;
3963 default:
1e2b2765 3964 rv = -1;
4345e135
MK
3965 break;
3966 }
f352c545 3967
6e46bf34
DW
3968 /* successful update? recompute checksum */
3969 if (rv == 0)
3970 mpb->check_sum = __le32_to_cpu(__gen_imsm_checksum(mpb));
f352c545
DW
3971
3972 return rv;
cdddbdbc
DW
3973}
3974
c2c087e6 3975static size_t disks_to_mpb_size(int disks)
cdddbdbc 3976{
c2c087e6 3977 size_t size;
cdddbdbc 3978
c2c087e6
DW
3979 size = sizeof(struct imsm_super);
3980 size += (disks - 1) * sizeof(struct imsm_disk);
3981 size += 2 * sizeof(struct imsm_dev);
3982 /* up to 2 maps per raid device (-2 for imsm_maps in imsm_dev */
3983 size += (4 - 2) * sizeof(struct imsm_map);
3984 /* 4 possible disk_ord_tbl's */
3985 size += 4 * (disks - 1) * sizeof(__u32);
bbab0940
TM
3986 /* maximum bbm log */
3987 size += sizeof(struct bbm_log);
c2c087e6
DW
3988
3989 return size;
3990}
3991
387fcd59
N
3992static __u64 avail_size_imsm(struct supertype *st, __u64 devsize,
3993 unsigned long long data_offset)
c2c087e6
DW
3994{
3995 if (devsize < (MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS))
3996 return 0;
3997
3998 return devsize - (MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS);
cdddbdbc
DW
3999}
4000
ba2de7ba
DW
4001static void free_devlist(struct intel_super *super)
4002{
4003 struct intel_dev *dv;
4004
4005 while (super->devlist) {
4006 dv = super->devlist->next;
4007 free(super->devlist->dev);
4008 free(super->devlist);
4009 super->devlist = dv;
4010 }
4011}
4012
4013static void imsm_copy_dev(struct imsm_dev *dest, struct imsm_dev *src)
4014{
4015 memcpy(dest, src, sizeof_imsm_dev(src, 0));
4016}
4017
c7b8547c
MT
4018static int compare_super_imsm(struct supertype *st, struct supertype *tst,
4019 int verbose)
cdddbdbc 4020{
601ffa78 4021 /* return:
cdddbdbc 4022 * 0 same, or first was empty, and second was copied
601ffa78 4023 * 1 sb are different
cdddbdbc
DW
4024 */
4025 struct intel_super *first = st->sb;
4026 struct intel_super *sec = tst->sb;
4027
5d500228
N
4028 if (!first) {
4029 st->sb = tst->sb;
4030 tst->sb = NULL;
4031 return 0;
4032 }
601ffa78 4033
8603ea6f
LM
4034 /* in platform dependent environment test if the disks
4035 * use the same Intel hba
601ffa78
OS
4036 * if not on Intel hba at all, allow anything.
4037 * doesn't check HBAs if num_raid_devs is not set, as it means
4038 * it is a free floating spare, and all spares regardless of HBA type
4039 * will fall into separate container during the assembly
8603ea6f 4040 */
601ffa78 4041 if (first->hba && sec->hba && first->anchor->num_raid_devs != 0) {
6b781d33 4042 if (first->hba->type != sec->hba->type) {
c7b8547c
MT
4043 if (verbose)
4044 pr_err("HBAs of devices do not match %s != %s\n",
4045 get_sys_dev_type(first->hba->type),
4046 get_sys_dev_type(sec->hba->type));
601ffa78 4047 return 1;
6b781d33
AP
4048 }
4049 if (first->orom != sec->orom) {
c7b8547c
MT
4050 if (verbose)
4051 pr_err("HBAs of devices do not match %s != %s\n",
4052 first->hba->pci_id, sec->hba->pci_id);
601ffa78 4053 return 1;
8603ea6f
LM
4054 }
4055 }
cdddbdbc 4056
d23fe947
DW
4057 if (first->anchor->num_raid_devs > 0 &&
4058 sec->anchor->num_raid_devs > 0) {
a2b97981
DW
4059 /* Determine if these disks might ever have been
4060 * related. Further disambiguation can only take place
4061 * in load_super_imsm_all
4062 */
4063 __u32 first_family = first->anchor->orig_family_num;
4064 __u32 sec_family = sec->anchor->orig_family_num;
4065
f796af5d
DW
4066 if (memcmp(first->anchor->sig, sec->anchor->sig,
4067 MAX_SIGNATURE_LENGTH) != 0)
601ffa78 4068 return 1;
f796af5d 4069
a2b97981
DW
4070 if (first_family == 0)
4071 first_family = first->anchor->family_num;
4072 if (sec_family == 0)
4073 sec_family = sec->anchor->family_num;
4074
4075 if (first_family != sec_family)
601ffa78 4076 return 1;
f796af5d 4077
d23fe947 4078 }
cdddbdbc 4079
601ffa78
OS
4080 /* if an anchor does not have num_raid_devs set then it is a free
4081 * floating spare. don't assosiate spare with any array, as during assembly
4082 * spares shall fall into separate container, from which they can be moved
4083 * when necessary
4084 */
4085 if (first->anchor->num_raid_devs ^ sec->anchor->num_raid_devs)
4086 return 1;
3e372e5a 4087
cdddbdbc
DW
4088 return 0;
4089}
4090
0030e8d6
DW
4091static void fd2devname(int fd, char *name)
4092{
0030e8d6 4093 char *nm;
0030e8d6 4094
7c798f87
MT
4095 nm = fd2kname(fd);
4096 if (!nm)
0030e8d6 4097 return;
9587c373 4098
7c798f87 4099 snprintf(name, MAX_RAID_SERIAL_LEN, "/dev/%s", nm);
0030e8d6
DW
4100}
4101
21e9380b
AP
4102static int nvme_get_serial(int fd, void *buf, size_t buf_len)
4103{
fcebeb77 4104 char path[PATH_MAX];
21e9380b
AP
4105 char *name = fd2kname(fd);
4106
4107 if (!name)
4108 return 1;
4109
4110 if (strncmp(name, "nvme", 4) != 0)
4111 return 1;
4112
fcebeb77
MT
4113 if (!diskfd_to_devpath(fd, 1, path))
4114 return 1;
21e9380b 4115
fcebeb77 4116 return devpath_to_char(path, "serial", buf, buf_len, 0);
21e9380b
AP
4117}
4118
cdddbdbc
DW
4119extern int scsi_get_serial(int fd, void *buf, size_t buf_len);
4120
4121static int imsm_read_serial(int fd, char *devname,
6da53c0e 4122 __u8 *serial, size_t serial_buf_len)
cdddbdbc 4123{
21e9380b 4124 char buf[50];
cdddbdbc 4125 int rv;
6da53c0e 4126 size_t len;
316e2bf4
DW
4127 char *dest;
4128 char *src;
21e9380b
AP
4129 unsigned int i;
4130
4131 memset(buf, 0, sizeof(buf));
cdddbdbc 4132
d1cd231a
KT
4133 if (check_env("IMSM_DEVNAME_AS_SERIAL")) {
4134 memset(serial, 0, serial_buf_len);
4135 fd2devname(fd, (char *) serial);
4136 return 0;
4137 }
4138
21e9380b 4139 rv = nvme_get_serial(fd, buf, sizeof(buf));
cdddbdbc 4140
21e9380b
AP
4141 if (rv)
4142 rv = scsi_get_serial(fd, buf, sizeof(buf));
f9ba0ff1 4143
cdddbdbc
DW
4144 if (rv != 0) {
4145 if (devname)
e7b84f9d
N
4146 pr_err("Failed to retrieve serial for %s\n",
4147 devname);
cdddbdbc
DW
4148 return rv;
4149 }
4150
316e2bf4
DW
4151 /* trim all whitespace and non-printable characters and convert
4152 * ':' to ';'
4153 */
21e9380b
AP
4154 for (i = 0, dest = buf; i < sizeof(buf) && buf[i]; i++) {
4155 src = &buf[i];
316e2bf4
DW
4156 if (*src > 0x20) {
4157 /* ':' is reserved for use in placeholder serial
4158 * numbers for missing disks
4159 */
4160 if (*src == ':')
4161 *dest++ = ';';
4162 else
4163 *dest++ = *src;
4164 }
4165 }
21e9380b
AP
4166 len = dest - buf;
4167 dest = buf;
316e2bf4 4168
6da53c0e
BK
4169 if (len > serial_buf_len) {
4170 /* truncate leading characters */
4171 dest += len - serial_buf_len;
4172 len = serial_buf_len;
316e2bf4 4173 }
5c3db629 4174
6da53c0e 4175 memset(serial, 0, serial_buf_len);
316e2bf4 4176 memcpy(serial, dest, len);
cdddbdbc
DW
4177
4178 return 0;
4179}
4180
1f24f035
DW
4181static int serialcmp(__u8 *s1, __u8 *s2)
4182{
4183 return strncmp((char *) s1, (char *) s2, MAX_RAID_SERIAL_LEN);
4184}
4185
4186static void serialcpy(__u8 *dest, __u8 *src)
4187{
4188 strncpy((char *) dest, (char *) src, MAX_RAID_SERIAL_LEN);
4189}
4190
54c2c1ea
DW
4191static struct dl *serial_to_dl(__u8 *serial, struct intel_super *super)
4192{
4193 struct dl *dl;
4194
4195 for (dl = super->disks; dl; dl = dl->next)
4196 if (serialcmp(dl->serial, serial) == 0)
4197 break;
4198
4199 return dl;
4200}
4201
a2b97981
DW
4202static struct imsm_disk *
4203__serial_to_disk(__u8 *serial, struct imsm_super *mpb, int *idx)
4204{
4205 int i;
4206
4207 for (i = 0; i < mpb->num_disks; i++) {
4208 struct imsm_disk *disk = __get_imsm_disk(mpb, i);
4209
4210 if (serialcmp(disk->serial, serial) == 0) {
4211 if (idx)
4212 *idx = i;
4213 return disk;
4214 }
4215 }
4216
4217 return NULL;
4218}
4219
cdddbdbc
DW
4220static int
4221load_imsm_disk(int fd, struct intel_super *super, char *devname, int keep_fd)
4222{
a2b97981 4223 struct imsm_disk *disk;
cdddbdbc
DW
4224 struct dl *dl;
4225 struct stat stb;
cdddbdbc 4226 int rv;
a2b97981 4227 char name[40];
d23fe947
DW
4228 __u8 serial[MAX_RAID_SERIAL_LEN];
4229
6da53c0e 4230 rv = imsm_read_serial(fd, devname, serial, MAX_RAID_SERIAL_LEN);
d23fe947
DW
4231
4232 if (rv != 0)
4233 return 2;
4234
503975b9 4235 dl = xcalloc(1, sizeof(*dl));
cdddbdbc 4236
a2b97981
DW
4237 fstat(fd, &stb);
4238 dl->major = major(stb.st_rdev);
4239 dl->minor = minor(stb.st_rdev);
4240 dl->next = super->disks;
4241 dl->fd = keep_fd ? fd : -1;
4242 assert(super->disks == NULL);
4243 super->disks = dl;
4244 serialcpy(dl->serial, serial);
4245 dl->index = -2;
4246 dl->e = NULL;
4247 fd2devname(fd, name);
4248 if (devname)
503975b9 4249 dl->devname = xstrdup(devname);
a2b97981 4250 else
503975b9 4251 dl->devname = xstrdup(name);
cdddbdbc 4252
d23fe947 4253 /* look up this disk's index in the current anchor */
a2b97981
DW
4254 disk = __serial_to_disk(dl->serial, super->anchor, &dl->index);
4255 if (disk) {
4256 dl->disk = *disk;
4257 /* only set index on disks that are a member of a
4258 * populated contianer, i.e. one with raid_devs
4259 */
4260 if (is_failed(&dl->disk))
3f6efecc 4261 dl->index = -2;
2432ce9b 4262 else if (is_spare(&dl->disk) || is_journal(&dl->disk))
a2b97981 4263 dl->index = -1;
3f6efecc
DW
4264 }
4265
949c47a0
DW
4266 return 0;
4267}
4268
0c046afd
DW
4269/* When migrating map0 contains the 'destination' state while map1
4270 * contains the current state. When not migrating map0 contains the
4271 * current state. This routine assumes that map[0].map_state is set to
4272 * the current array state before being called.
4273 *
4274 * Migration is indicated by one of the following states
4275 * 1/ Idle (migr_state=0 map0state=normal||unitialized||degraded||failed)
e3bba0e0 4276 * 2/ Initialize (migr_state=1 migr_type=MIGR_INIT map0state=normal
0c046afd 4277 * map1state=unitialized)
1484e727 4278 * 3/ Repair (Resync) (migr_state=1 migr_type=MIGR_REPAIR map0state=normal
0c046afd 4279 * map1state=normal)
e3bba0e0 4280 * 4/ Rebuild (migr_state=1 migr_type=MIGR_REBUILD map0state=normal
0c046afd 4281 * map1state=degraded)
8e59f3d8
AK
4282 * 5/ Migration (mig_state=1 migr_type=MIGR_GEN_MIGR map0state=normal
4283 * map1state=normal)
0c046afd 4284 */
8e59f3d8
AK
4285static void migrate(struct imsm_dev *dev, struct intel_super *super,
4286 __u8 to_state, int migr_type)
3393c6af 4287{
0c046afd 4288 struct imsm_map *dest;
238c0a71 4289 struct imsm_map *src = get_imsm_map(dev, MAP_0);
3393c6af 4290
0c046afd 4291 dev->vol.migr_state = 1;
1484e727 4292 set_migr_type(dev, migr_type);
4036e7ee 4293 set_vol_curr_migr_unit(dev, 0);
238c0a71 4294 dest = get_imsm_map(dev, MAP_1);
0c046afd 4295
0556e1a2 4296 /* duplicate and then set the target end state in map[0] */
3393c6af 4297 memcpy(dest, src, sizeof_imsm_map(src));
fb12a745 4298 if (migr_type == MIGR_GEN_MIGR) {
0556e1a2
DW
4299 __u32 ord;
4300 int i;
4301
4302 for (i = 0; i < src->num_members; i++) {
4303 ord = __le32_to_cpu(src->disk_ord_tbl[i]);
4304 set_imsm_ord_tbl_ent(src, i, ord_to_idx(ord));
4305 }
4306 }
4307
8e59f3d8
AK
4308 if (migr_type == MIGR_GEN_MIGR)
4309 /* Clear migration record */
4310 memset(super->migr_rec, 0, sizeof(struct migr_record));
4311
0c046afd 4312 src->map_state = to_state;
949c47a0 4313}
f8f603f1 4314
809da78e
AK
4315static void end_migration(struct imsm_dev *dev, struct intel_super *super,
4316 __u8 map_state)
f8f603f1 4317{
238c0a71
AK
4318 struct imsm_map *map = get_imsm_map(dev, MAP_0);
4319 struct imsm_map *prev = get_imsm_map(dev, dev->vol.migr_state == 0 ?
4320 MAP_0 : MAP_1);
28bce06f 4321 int i, j;
0556e1a2
DW
4322
4323 /* merge any IMSM_ORD_REBUILD bits that were not successfully
4324 * completed in the last migration.
4325 *
28bce06f 4326 * FIXME add support for raid-level-migration
0556e1a2 4327 */
195d1d76 4328 if (map_state != map->map_state && (is_gen_migration(dev) == false) &&
089f9d79 4329 prev->map_state != IMSM_T_STATE_UNINITIALIZED) {
809da78e
AK
4330 /* when final map state is other than expected
4331 * merge maps (not for migration)
4332 */
4333 int failed;
4334
4335 for (i = 0; i < prev->num_members; i++)
4336 for (j = 0; j < map->num_members; j++)
4337 /* during online capacity expansion
4338 * disks position can be changed
4339 * if takeover is used
4340 */
4341 if (ord_to_idx(map->disk_ord_tbl[j]) ==
4342 ord_to_idx(prev->disk_ord_tbl[i])) {
4343 map->disk_ord_tbl[j] |=
4344 prev->disk_ord_tbl[i];
4345 break;
4346 }
4347 failed = imsm_count_failed(super, dev, MAP_0);
4348 map_state = imsm_check_degraded(super, dev, failed, MAP_0);
4349 }
f8f603f1
DW
4350
4351 dev->vol.migr_state = 0;
ea672ee1 4352 set_migr_type(dev, 0);
4036e7ee 4353 set_vol_curr_migr_unit(dev, 0);
f8f603f1
DW
4354 map->map_state = map_state;
4355}
949c47a0
DW
4356
4357static int parse_raid_devices(struct intel_super *super)
4358{
4359 int i;
4360 struct imsm_dev *dev_new;
4d7b1503 4361 size_t len, len_migr;
401d313b 4362 size_t max_len = 0;
4d7b1503
DW
4363 size_t space_needed = 0;
4364 struct imsm_super *mpb = super->anchor;
949c47a0
DW
4365
4366 for (i = 0; i < super->anchor->num_raid_devs; i++) {
4367 struct imsm_dev *dev_iter = __get_imsm_dev(super->anchor, i);
ba2de7ba 4368 struct intel_dev *dv;
949c47a0 4369
4d7b1503
DW
4370 len = sizeof_imsm_dev(dev_iter, 0);
4371 len_migr = sizeof_imsm_dev(dev_iter, 1);
4372 if (len_migr > len)
4373 space_needed += len_migr - len;
ca9de185 4374
503975b9 4375 dv = xmalloc(sizeof(*dv));
401d313b
AK
4376 if (max_len < len_migr)
4377 max_len = len_migr;
4378 if (max_len > len_migr)
4379 space_needed += max_len - len_migr;
503975b9 4380 dev_new = xmalloc(max_len);
949c47a0 4381 imsm_copy_dev(dev_new, dev_iter);
ba2de7ba
DW
4382 dv->dev = dev_new;
4383 dv->index = i;
4384 dv->next = super->devlist;
4385 super->devlist = dv;
949c47a0 4386 }
cdddbdbc 4387
4d7b1503
DW
4388 /* ensure that super->buf is large enough when all raid devices
4389 * are migrating
4390 */
4391 if (__le32_to_cpu(mpb->mpb_size) + space_needed > super->len) {
4392 void *buf;
4393
f36a9ecd
PB
4394 len = ROUND_UP(__le32_to_cpu(mpb->mpb_size) + space_needed,
4395 super->sector_size);
4396 if (posix_memalign(&buf, MAX_SECTOR_SIZE, len) != 0)
4d7b1503
DW
4397 return 1;
4398
1f45a8ad
DW
4399 memcpy(buf, super->buf, super->len);
4400 memset(buf + super->len, 0, len - super->len);
4d7b1503
DW
4401 free(super->buf);
4402 super->buf = buf;
4403 super->len = len;
4404 }
ca9de185 4405
bbab0940
TM
4406 super->extra_space += space_needed;
4407
cdddbdbc
DW
4408 return 0;
4409}
4410
e2f41b2c
AK
4411/*******************************************************************************
4412 * Function: check_mpb_migr_compatibility
4413 * Description: Function checks for unsupported migration features:
4414 * - migration optimization area (pba_of_lba0)
4415 * - descending reshape (ascending_migr)
4416 * Parameters:
4417 * super : imsm metadata information
4418 * Returns:
4419 * 0 : migration is compatible
4420 * -1 : migration is not compatible
4421 ******************************************************************************/
4422int check_mpb_migr_compatibility(struct intel_super *super)
4423{
4424 struct imsm_map *map0, *map1;
4425 struct migr_record *migr_rec = super->migr_rec;
4426 int i;
4427
4428 for (i = 0; i < super->anchor->num_raid_devs; i++) {
4429 struct imsm_dev *dev_iter = __get_imsm_dev(super->anchor, i);
4430
756a15f3 4431 if (dev_iter->vol.migr_state == 1 &&
e2f41b2c
AK
4432 dev_iter->vol.migr_type == MIGR_GEN_MIGR) {
4433 /* This device is migrating */
238c0a71
AK
4434 map0 = get_imsm_map(dev_iter, MAP_0);
4435 map1 = get_imsm_map(dev_iter, MAP_1);
5551b113 4436 if (pba_of_lba0(map0) != pba_of_lba0(map1))
e2f41b2c
AK
4437 /* migration optimization area was used */
4438 return -1;
fc54fe7a
JS
4439 if (migr_rec->ascending_migr == 0 &&
4440 migr_rec->dest_depth_per_unit > 0)
e2f41b2c
AK
4441 /* descending reshape not supported yet */
4442 return -1;
4443 }
4444 }
4445 return 0;
4446}
4447
d23fe947 4448static void __free_imsm(struct intel_super *super, int free_disks);
9ca2c81c 4449
cdddbdbc 4450/* load_imsm_mpb - read matrix metadata
f2f5c343 4451 * allocates super->mpb to be freed by free_imsm
cdddbdbc
DW
4452 */
4453static int load_imsm_mpb(int fd, struct intel_super *super, char *devname)
4454{
4455 unsigned long long dsize;
cdddbdbc 4456 unsigned long long sectors;
f36a9ecd 4457 unsigned int sector_size = super->sector_size;
cdddbdbc 4458 struct stat;
6416d527 4459 struct imsm_super *anchor;
cdddbdbc
DW
4460 __u32 check_sum;
4461
cdddbdbc 4462 get_dev_size(fd, NULL, &dsize);
f36a9ecd 4463 if (dsize < 2*sector_size) {
64436f06 4464 if (devname)
e7b84f9d
N
4465 pr_err("%s: device to small for imsm\n",
4466 devname);
64436f06
N
4467 return 1;
4468 }
cdddbdbc 4469
f36a9ecd 4470 if (lseek64(fd, dsize - (sector_size * 2), SEEK_SET) < 0) {
cdddbdbc 4471 if (devname)
e7b84f9d
N
4472 pr_err("Cannot seek to anchor block on %s: %s\n",
4473 devname, strerror(errno));
cdddbdbc
DW
4474 return 1;
4475 }
4476
f36a9ecd 4477 if (posix_memalign((void **)&anchor, sector_size, sector_size) != 0) {
ad97895e 4478 if (devname)
7a862a02 4479 pr_err("Failed to allocate imsm anchor buffer on %s\n", devname);
ad97895e
DW
4480 return 1;
4481 }
466070ad 4482 if ((unsigned int)read(fd, anchor, sector_size) != sector_size) {
cdddbdbc 4483 if (devname)
e7b84f9d
N
4484 pr_err("Cannot read anchor block on %s: %s\n",
4485 devname, strerror(errno));
6416d527 4486 free(anchor);
cdddbdbc
DW
4487 return 1;
4488 }
4489
6416d527 4490 if (strncmp((char *) anchor->sig, MPB_SIGNATURE, MPB_SIG_LEN) != 0) {
cdddbdbc 4491 if (devname)
e7b84f9d 4492 pr_err("no IMSM anchor on %s\n", devname);
6416d527 4493 free(anchor);
cdddbdbc
DW
4494 return 2;
4495 }
4496
d23fe947 4497 __free_imsm(super, 0);
f2f5c343
LM
4498 /* reload capability and hba */
4499
4500 /* capability and hba must be updated with new super allocation */
d424212e 4501 find_intel_hba_capability(fd, super, devname);
f36a9ecd
PB
4502 super->len = ROUND_UP(anchor->mpb_size, sector_size);
4503 if (posix_memalign(&super->buf, MAX_SECTOR_SIZE, super->len) != 0) {
cdddbdbc 4504 if (devname)
e7b84f9d
N
4505 pr_err("unable to allocate %zu byte mpb buffer\n",
4506 super->len);
6416d527 4507 free(anchor);
cdddbdbc
DW
4508 return 2;
4509 }
f36a9ecd 4510 memcpy(super->buf, anchor, sector_size);
cdddbdbc 4511
f36a9ecd 4512 sectors = mpb_sectors(anchor, sector_size) - 1;
6416d527 4513 free(anchor);
8e59f3d8 4514
85337573
AO
4515 if (posix_memalign(&super->migr_rec_buf, MAX_SECTOR_SIZE,
4516 MIGR_REC_BUF_SECTORS*MAX_SECTOR_SIZE) != 0) {
1ade5cc1 4517 pr_err("could not allocate migr_rec buffer\n");
8e59f3d8 4518 free(super->buf);
50cd06b4 4519 super->buf = NULL;
8e59f3d8
AK
4520 return 2;
4521 }
51d83f5d 4522 super->clean_migration_record_by_mdmon = 0;
8e59f3d8 4523
949c47a0 4524 if (!sectors) {
ecf45690
DW
4525 check_sum = __gen_imsm_checksum(super->anchor);
4526 if (check_sum != __le32_to_cpu(super->anchor->check_sum)) {
4527 if (devname)
e7b84f9d
N
4528 pr_err("IMSM checksum %x != %x on %s\n",
4529 check_sum,
4530 __le32_to_cpu(super->anchor->check_sum),
4531 devname);
ecf45690
DW
4532 return 2;
4533 }
4534
a2b97981 4535 return 0;
949c47a0 4536 }
cdddbdbc
DW
4537
4538 /* read the extended mpb */
f36a9ecd 4539 if (lseek64(fd, dsize - (sector_size * (2 + sectors)), SEEK_SET) < 0) {
cdddbdbc 4540 if (devname)
e7b84f9d
N
4541 pr_err("Cannot seek to extended mpb on %s: %s\n",
4542 devname, strerror(errno));
cdddbdbc
DW
4543 return 1;
4544 }
4545
f36a9ecd
PB
4546 if ((unsigned int)read(fd, super->buf + sector_size,
4547 super->len - sector_size) != super->len - sector_size) {
cdddbdbc 4548 if (devname)
e7b84f9d
N
4549 pr_err("Cannot read extended mpb on %s: %s\n",
4550 devname, strerror(errno));
cdddbdbc
DW
4551 return 2;
4552 }
4553
949c47a0
DW
4554 check_sum = __gen_imsm_checksum(super->anchor);
4555 if (check_sum != __le32_to_cpu(super->anchor->check_sum)) {
cdddbdbc 4556 if (devname)
e7b84f9d
N
4557 pr_err("IMSM checksum %x != %x on %s\n",
4558 check_sum, __le32_to_cpu(super->anchor->check_sum),
4559 devname);
db575f3b 4560 return 3;
cdddbdbc
DW
4561 }
4562
a2b97981
DW
4563 return 0;
4564}
4565
8e59f3d8
AK
4566static int read_imsm_migr_rec(int fd, struct intel_super *super);
4567
97f81ee2
CA
4568/* clears hi bits in metadata if MPB_ATTRIB_2TB_DISK not set */
4569static void clear_hi(struct intel_super *super)
4570{
4571 struct imsm_super *mpb = super->anchor;
4572 int i, n;
4573 if (mpb->attributes & MPB_ATTRIB_2TB_DISK)
4574 return;
4575 for (i = 0; i < mpb->num_disks; ++i) {
4576 struct imsm_disk *disk = &mpb->disk[i];
4577 disk->total_blocks_hi = 0;
4578 }
4579 for (i = 0; i < mpb->num_raid_devs; ++i) {
4580 struct imsm_dev *dev = get_imsm_dev(super, i);
97f81ee2
CA
4581 for (n = 0; n < 2; ++n) {
4582 struct imsm_map *map = get_imsm_map(dev, n);
4583 if (!map)
4584 continue;
4585 map->pba_of_lba0_hi = 0;
4586 map->blocks_per_member_hi = 0;
4587 map->num_data_stripes_hi = 0;
4588 }
4589 }
4590}
4591
a2b97981
DW
4592static int
4593load_and_parse_mpb(int fd, struct intel_super *super, char *devname, int keep_fd)
4594{
4595 int err;
4596
4597 err = load_imsm_mpb(fd, super, devname);
4598 if (err)
4599 return err;
f36a9ecd
PB
4600 if (super->sector_size == 4096)
4601 convert_from_4k(super);
a2b97981
DW
4602 err = load_imsm_disk(fd, super, devname, keep_fd);
4603 if (err)
4604 return err;
4605 err = parse_raid_devices(super);
8d67477f
TM
4606 if (err)
4607 return err;
4608 err = load_bbm_log(super);
97f81ee2 4609 clear_hi(super);
a2b97981 4610 return err;
cdddbdbc
DW
4611}
4612
4389ce73 4613static void __free_imsm_disk(struct dl *d, int do_close)
ae6aad82 4614{
4389ce73
MT
4615 if (do_close)
4616 close_fd(&d->fd);
ae6aad82
DW
4617 if (d->devname)
4618 free(d->devname);
0dcecb2e
DW
4619 if (d->e)
4620 free(d->e);
ae6aad82
DW
4621 free(d);
4622
4623}
1a64be56 4624
cdddbdbc
DW
4625static void free_imsm_disks(struct intel_super *super)
4626{
47ee5a45 4627 struct dl *d;
cdddbdbc 4628
47ee5a45
DW
4629 while (super->disks) {
4630 d = super->disks;
cdddbdbc 4631 super->disks = d->next;
3a85bf0e 4632 __free_imsm_disk(d, 1);
cdddbdbc 4633 }
cb82edca
AK
4634 while (super->disk_mgmt_list) {
4635 d = super->disk_mgmt_list;
4636 super->disk_mgmt_list = d->next;
3a85bf0e 4637 __free_imsm_disk(d, 1);
cb82edca 4638 }
47ee5a45
DW
4639 while (super->missing) {
4640 d = super->missing;
4641 super->missing = d->next;
3a85bf0e 4642 __free_imsm_disk(d, 1);
47ee5a45
DW
4643 }
4644
cdddbdbc
DW
4645}
4646
9ca2c81c 4647/* free all the pieces hanging off of a super pointer */
d23fe947 4648static void __free_imsm(struct intel_super *super, int free_disks)
cdddbdbc 4649{
88654014
LM
4650 struct intel_hba *elem, *next;
4651
9ca2c81c 4652 if (super->buf) {
949c47a0 4653 free(super->buf);
9ca2c81c
DW
4654 super->buf = NULL;
4655 }
f2f5c343
LM
4656 /* unlink capability description */
4657 super->orom = NULL;
8e59f3d8
AK
4658 if (super->migr_rec_buf) {
4659 free(super->migr_rec_buf);
4660 super->migr_rec_buf = NULL;
4661 }
d23fe947
DW
4662 if (free_disks)
4663 free_imsm_disks(super);
ba2de7ba 4664 free_devlist(super);
88654014
LM
4665 elem = super->hba;
4666 while (elem) {
4667 if (elem->path)
4668 free((void *)elem->path);
4669 next = elem->next;
4670 free(elem);
4671 elem = next;
88c32bb1 4672 }
8d67477f
TM
4673 if (super->bbm_log)
4674 free(super->bbm_log);
88654014 4675 super->hba = NULL;
cdddbdbc
DW
4676}
4677
9ca2c81c
DW
4678static void free_imsm(struct intel_super *super)
4679{
d23fe947 4680 __free_imsm(super, 1);
928f1424 4681 free(super->bb.entries);
9ca2c81c
DW
4682 free(super);
4683}
cdddbdbc
DW
4684
4685static void free_super_imsm(struct supertype *st)
4686{
4687 struct intel_super *super = st->sb;
4688
4689 if (!super)
4690 return;
4691
4692 free_imsm(super);
4693 st->sb = NULL;
4694}
4695
49133e57 4696static struct intel_super *alloc_super(void)
c2c087e6 4697{
503975b9 4698 struct intel_super *super = xcalloc(1, sizeof(*super));
c2c087e6 4699
503975b9
N
4700 super->current_vol = -1;
4701 super->create_offset = ~((unsigned long long) 0);
928f1424
TM
4702
4703 super->bb.entries = xmalloc(BBM_LOG_MAX_ENTRIES *
4704 sizeof(struct md_bb_entry));
4705 if (!super->bb.entries) {
4706 free(super);
4707 return NULL;
4708 }
4709
c2c087e6
DW
4710 return super;
4711}
4712
f0f5a016
LM
4713/*
4714 * find and allocate hba and OROM/EFI based on valid fd of RAID component device
4715 */
d424212e 4716static int find_intel_hba_capability(int fd, struct intel_super *super, char *devname)
f0f5a016
LM
4717{
4718 struct sys_dev *hba_name;
4719 int rv = 0;
4720
4389ce73 4721 if (is_fd_valid(fd) && test_partition(fd)) {
3a30e28e
MT
4722 pr_err("imsm: %s is a partition, cannot be used in IMSM\n",
4723 devname);
4724 return 1;
4725 }
420dafcd 4726 if (!is_fd_valid(fd) || check_no_platform()) {
f2f5c343 4727 super->orom = NULL;
f0f5a016
LM
4728 super->hba = NULL;
4729 return 0;
4730 }
4731 hba_name = find_disk_attached_hba(fd, NULL);
4732 if (!hba_name) {
d424212e 4733 if (devname)
e7b84f9d
N
4734 pr_err("%s is not attached to Intel(R) RAID controller.\n",
4735 devname);
f0f5a016
LM
4736 return 1;
4737 }
4738 rv = attach_hba_to_super(super, hba_name);
4739 if (rv == 2) {
d424212e
N
4740 if (devname) {
4741 struct intel_hba *hba = super->hba;
f0f5a016 4742
60f0f54d
PB
4743 pr_err("%s is attached to Intel(R) %s %s (%s),\n"
4744 " but the container is assigned to Intel(R) %s %s (",
d424212e 4745 devname,
614902f6 4746 get_sys_dev_type(hba_name->type),
75350d87
KF
4747 hba_name->type == SYS_DEV_VMD || hba_name->type == SYS_DEV_SATA_VMD ?
4748 "domain" : "RAID controller",
f0f5a016 4749 hba_name->pci_id ? : "Err!",
60f0f54d 4750 get_sys_dev_type(super->hba->type),
75350d87
KF
4751 hba->type == SYS_DEV_VMD || hba_name->type == SYS_DEV_SATA_VMD ?
4752 "domain" : "RAID controller");
f0f5a016 4753
f0f5a016
LM
4754 while (hba) {
4755 fprintf(stderr, "%s", hba->pci_id ? : "Err!");
4756 if (hba->next)
4757 fprintf(stderr, ", ");
4758 hba = hba->next;
4759 }
6b781d33 4760 fprintf(stderr, ").\n"
cca67208 4761 " Mixing devices attached to different controllers is not allowed.\n");
f0f5a016 4762 }
f0f5a016
LM
4763 return 2;
4764 }
6b781d33 4765 super->orom = find_imsm_capability(hba_name);
f2f5c343
LM
4766 if (!super->orom)
4767 return 3;
614902f6 4768
f0f5a016
LM
4769 return 0;
4770}
4771
47ee5a45
DW
4772/* find_missing - helper routine for load_super_imsm_all that identifies
4773 * disks that have disappeared from the system. This routine relies on
4774 * the mpb being uptodate, which it is at load time.
4775 */
4776static int find_missing(struct intel_super *super)
4777{
4778 int i;
4779 struct imsm_super *mpb = super->anchor;
4780 struct dl *dl;
4781 struct imsm_disk *disk;
47ee5a45
DW
4782
4783 for (i = 0; i < mpb->num_disks; i++) {
4784 disk = __get_imsm_disk(mpb, i);
54c2c1ea 4785 dl = serial_to_dl(disk->serial, super);
47ee5a45
DW
4786 if (dl)
4787 continue;
47ee5a45 4788
503975b9 4789 dl = xmalloc(sizeof(*dl));
47ee5a45
DW
4790 dl->major = 0;
4791 dl->minor = 0;
4792 dl->fd = -1;
503975b9 4793 dl->devname = xstrdup("missing");
47ee5a45
DW
4794 dl->index = i;
4795 serialcpy(dl->serial, disk->serial);
4796 dl->disk = *disk;
689c9bf3 4797 dl->e = NULL;
47ee5a45
DW
4798 dl->next = super->missing;
4799 super->missing = dl;
4800 }
4801
4802 return 0;
4803}
4804
a2b97981
DW
4805static struct intel_disk *disk_list_get(__u8 *serial, struct intel_disk *disk_list)
4806{
4807 struct intel_disk *idisk = disk_list;
4808
4809 while (idisk) {
4810 if (serialcmp(idisk->disk.serial, serial) == 0)
4811 break;
4812 idisk = idisk->next;
4813 }
4814
4815 return idisk;
4816}
4817
4818static int __prep_thunderdome(struct intel_super **table, int tbl_size,
4819 struct intel_super *super,
4820 struct intel_disk **disk_list)
4821{
4822 struct imsm_disk *d = &super->disks->disk;
4823 struct imsm_super *mpb = super->anchor;
4824 int i, j;
4825
4826 for (i = 0; i < tbl_size; i++) {
4827 struct imsm_super *tbl_mpb = table[i]->anchor;
4828 struct imsm_disk *tbl_d = &table[i]->disks->disk;
4829
4830 if (tbl_mpb->family_num == mpb->family_num) {
4831 if (tbl_mpb->check_sum == mpb->check_sum) {
1ade5cc1
N
4832 dprintf("mpb from %d:%d matches %d:%d\n",
4833 super->disks->major,
a2b97981
DW
4834 super->disks->minor,
4835 table[i]->disks->major,
4836 table[i]->disks->minor);
4837 break;
4838 }
4839
4840 if (((is_configured(d) && !is_configured(tbl_d)) ||
4841 is_configured(d) == is_configured(tbl_d)) &&
4842 tbl_mpb->generation_num < mpb->generation_num) {
4843 /* current version of the mpb is a
4844 * better candidate than the one in
4845 * super_table, but copy over "cross
4846 * generational" status
4847 */
4848 struct intel_disk *idisk;
4849
1ade5cc1
N
4850 dprintf("mpb from %d:%d replaces %d:%d\n",
4851 super->disks->major,
a2b97981
DW
4852 super->disks->minor,
4853 table[i]->disks->major,
4854 table[i]->disks->minor);
4855
4856 idisk = disk_list_get(tbl_d->serial, *disk_list);
4857 if (idisk && is_failed(&idisk->disk))
4858 tbl_d->status |= FAILED_DISK;
4859 break;
4860 } else {
4861 struct intel_disk *idisk;
4862 struct imsm_disk *disk;
4863
4864 /* tbl_mpb is more up to date, but copy
4865 * over cross generational status before
4866 * returning
4867 */
4868 disk = __serial_to_disk(d->serial, mpb, NULL);
4869 if (disk && is_failed(disk))
4870 d->status |= FAILED_DISK;
4871
4872 idisk = disk_list_get(d->serial, *disk_list);
4873 if (idisk) {
4874 idisk->owner = i;
4875 if (disk && is_configured(disk))
4876 idisk->disk.status |= CONFIGURED_DISK;
4877 }
4878
1ade5cc1
N
4879 dprintf("mpb from %d:%d prefer %d:%d\n",
4880 super->disks->major,
a2b97981
DW
4881 super->disks->minor,
4882 table[i]->disks->major,
4883 table[i]->disks->minor);
4884
4885 return tbl_size;
4886 }
4887 }
4888 }
4889
4890 if (i >= tbl_size)
4891 table[tbl_size++] = super;
4892 else
4893 table[i] = super;
4894
4895 /* update/extend the merged list of imsm_disk records */
4896 for (j = 0; j < mpb->num_disks; j++) {
4897 struct imsm_disk *disk = __get_imsm_disk(mpb, j);
4898 struct intel_disk *idisk;
4899
4900 idisk = disk_list_get(disk->serial, *disk_list);
4901 if (idisk) {
4902 idisk->disk.status |= disk->status;
4903 if (is_configured(&idisk->disk) ||
4904 is_failed(&idisk->disk))
4905 idisk->disk.status &= ~(SPARE_DISK);
4906 } else {
503975b9 4907 idisk = xcalloc(1, sizeof(*idisk));
a2b97981
DW
4908 idisk->owner = IMSM_UNKNOWN_OWNER;
4909 idisk->disk = *disk;
4910 idisk->next = *disk_list;
4911 *disk_list = idisk;
4912 }
4913
4914 if (serialcmp(idisk->disk.serial, d->serial) == 0)
4915 idisk->owner = i;
4916 }
4917
4918 return tbl_size;
4919}
4920
4921static struct intel_super *
4922validate_members(struct intel_super *super, struct intel_disk *disk_list,
4923 const int owner)
4924{
4925 struct imsm_super *mpb = super->anchor;
4926 int ok_count = 0;
4927 int i;
4928
4929 for (i = 0; i < mpb->num_disks; i++) {
4930 struct imsm_disk *disk = __get_imsm_disk(mpb, i);
4931 struct intel_disk *idisk;
4932
4933 idisk = disk_list_get(disk->serial, disk_list);
4934 if (idisk) {
4935 if (idisk->owner == owner ||
4936 idisk->owner == IMSM_UNKNOWN_OWNER)
4937 ok_count++;
4938 else
1ade5cc1
N
4939 dprintf("'%.16s' owner %d != %d\n",
4940 disk->serial, idisk->owner,
a2b97981
DW
4941 owner);
4942 } else {
1ade5cc1
N
4943 dprintf("unknown disk %x [%d]: %.16s\n",
4944 __le32_to_cpu(mpb->family_num), i,
a2b97981
DW
4945 disk->serial);
4946 break;
4947 }
4948 }
4949
4950 if (ok_count == mpb->num_disks)
4951 return super;
4952 return NULL;
4953}
4954
4955static void show_conflicts(__u32 family_num, struct intel_super *super_list)
4956{
4957 struct intel_super *s;
4958
4959 for (s = super_list; s; s = s->next) {
4960 if (family_num != s->anchor->family_num)
4961 continue;
e12b3daa 4962 pr_err("Conflict, offlining family %#x on '%s'\n",
a2b97981
DW
4963 __le32_to_cpu(family_num), s->disks->devname);
4964 }
4965}
4966
4967static struct intel_super *
4968imsm_thunderdome(struct intel_super **super_list, int len)
4969{
4970 struct intel_super *super_table[len];
4971 struct intel_disk *disk_list = NULL;
4972 struct intel_super *champion, *spare;
4973 struct intel_super *s, **del;
4974 int tbl_size = 0;
4975 int conflict;
4976 int i;
4977
4978 memset(super_table, 0, sizeof(super_table));
4979 for (s = *super_list; s; s = s->next)
4980 tbl_size = __prep_thunderdome(super_table, tbl_size, s, &disk_list);
4981
4982 for (i = 0; i < tbl_size; i++) {
4983 struct imsm_disk *d;
4984 struct intel_disk *idisk;
4985 struct imsm_super *mpb = super_table[i]->anchor;
4986
4987 s = super_table[i];
4988 d = &s->disks->disk;
4989
4990 /* 'd' must appear in merged disk list for its
4991 * configuration to be valid
4992 */
4993 idisk = disk_list_get(d->serial, disk_list);
4994 if (idisk && idisk->owner == i)
4995 s = validate_members(s, disk_list, i);
4996 else
4997 s = NULL;
4998
4999 if (!s)
1ade5cc1
N
5000 dprintf("marking family: %#x from %d:%d offline\n",
5001 mpb->family_num,
a2b97981
DW
5002 super_table[i]->disks->major,
5003 super_table[i]->disks->minor);
5004 super_table[i] = s;
5005 }
5006
5007 /* This is where the mdadm implementation differs from the Windows
5008 * driver which has no strict concept of a container. We can only
5009 * assemble one family from a container, so when returning a prodigal
5010 * array member to this system the code will not be able to disambiguate
5011 * the container contents that should be assembled ("foreign" versus
5012 * "local"). It requires user intervention to set the orig_family_num
5013 * to a new value to establish a new container. The Windows driver in
5014 * this situation fixes up the volume name in place and manages the
5015 * foreign array as an independent entity.
5016 */
5017 s = NULL;
5018 spare = NULL;
5019 conflict = 0;
5020 for (i = 0; i < tbl_size; i++) {
5021 struct intel_super *tbl_ent = super_table[i];
5022 int is_spare = 0;
5023
5024 if (!tbl_ent)
5025 continue;
5026
5027 if (tbl_ent->anchor->num_raid_devs == 0) {
5028 spare = tbl_ent;
5029 is_spare = 1;
5030 }
5031
5032 if (s && !is_spare) {
5033 show_conflicts(tbl_ent->anchor->family_num, *super_list);
5034 conflict++;
5035 } else if (!s && !is_spare)
5036 s = tbl_ent;
5037 }
5038
5039 if (!s)
5040 s = spare;
5041 if (!s) {
5042 champion = NULL;
5043 goto out;
5044 }
5045 champion = s;
5046
5047 if (conflict)
7a862a02 5048 pr_err("Chose family %#x on '%s', assemble conflicts to new container with '--update=uuid'\n",
a2b97981
DW
5049 __le32_to_cpu(s->anchor->family_num), s->disks->devname);
5050
5051 /* collect all dl's onto 'champion', and update them to
5052 * champion's version of the status
5053 */
5054 for (s = *super_list; s; s = s->next) {
5055 struct imsm_super *mpb = champion->anchor;
5056 struct dl *dl = s->disks;
5057
5058 if (s == champion)
5059 continue;
5060
5d7b407a
CA
5061 mpb->attributes |= s->anchor->attributes & MPB_ATTRIB_2TB_DISK;
5062
a2b97981
DW
5063 for (i = 0; i < mpb->num_disks; i++) {
5064 struct imsm_disk *disk;
5065
5066 disk = __serial_to_disk(dl->serial, mpb, &dl->index);
5067 if (disk) {
5068 dl->disk = *disk;
5069 /* only set index on disks that are a member of
5070 * a populated contianer, i.e. one with
5071 * raid_devs
5072 */
5073 if (is_failed(&dl->disk))
5074 dl->index = -2;
5075 else if (is_spare(&dl->disk))
5076 dl->index = -1;
5077 break;
5078 }
5079 }
5080
5081 if (i >= mpb->num_disks) {
5082 struct intel_disk *idisk;
5083
5084 idisk = disk_list_get(dl->serial, disk_list);
ecf408e9 5085 if (idisk && is_spare(&idisk->disk) &&
a2b97981
DW
5086 !is_failed(&idisk->disk) && !is_configured(&idisk->disk))
5087 dl->index = -1;
5088 else {
5089 dl->index = -2;
5090 continue;
5091 }
5092 }
5093
5094 dl->next = champion->disks;
5095 champion->disks = dl;
5096 s->disks = NULL;
5097 }
5098
5099 /* delete 'champion' from super_list */
5100 for (del = super_list; *del; ) {
5101 if (*del == champion) {
5102 *del = (*del)->next;
5103 break;
5104 } else
5105 del = &(*del)->next;
5106 }
5107 champion->next = NULL;
5108
5109 out:
5110 while (disk_list) {
5111 struct intel_disk *idisk = disk_list;
5112
5113 disk_list = disk_list->next;
5114 free(idisk);
5115 }
5116
5117 return champion;
5118}
5119
9587c373
LM
5120static int
5121get_sra_super_block(int fd, struct intel_super **super_list, char *devname, int *max, int keep_fd);
4dd2df09 5122static int get_super_block(struct intel_super **super_list, char *devnm, char *devname,
9587c373 5123 int major, int minor, int keep_fd);
ec50f7b6
LM
5124static int
5125get_devlist_super_block(struct md_list *devlist, struct intel_super **super_list,
5126 int *max, int keep_fd);
5127
cdddbdbc 5128static int load_super_imsm_all(struct supertype *st, int fd, void **sbp,
ec50f7b6
LM
5129 char *devname, struct md_list *devlist,
5130 int keep_fd)
cdddbdbc 5131{
a2b97981
DW
5132 struct intel_super *super_list = NULL;
5133 struct intel_super *super = NULL;
a2b97981 5134 int err = 0;
9587c373 5135 int i = 0;
dab4a513 5136
4389ce73 5137 if (is_fd_valid(fd))
9587c373
LM
5138 /* 'fd' is an opened container */
5139 err = get_sra_super_block(fd, &super_list, devname, &i, keep_fd);
5140 else
ec50f7b6
LM
5141 /* get super block from devlist devices */
5142 err = get_devlist_super_block(devlist, &super_list, &i, keep_fd);
9587c373 5143 if (err)
1602d52c 5144 goto error;
a2b97981
DW
5145 /* all mpbs enter, maybe one leaves */
5146 super = imsm_thunderdome(&super_list, i);
5147 if (!super) {
5148 err = 1;
5149 goto error;
cdddbdbc
DW
5150 }
5151
47ee5a45
DW
5152 if (find_missing(super) != 0) {
5153 free_imsm(super);
a2b97981
DW
5154 err = 2;
5155 goto error;
47ee5a45 5156 }
8e59f3d8
AK
5157
5158 /* load migration record */
2f86fda3 5159 err = load_imsm_migr_rec(super);
4c965cc9
AK
5160 if (err == -1) {
5161 /* migration is in progress,
5162 * but migr_rec cannot be loaded,
5163 */
8e59f3d8
AK
5164 err = 4;
5165 goto error;
5166 }
e2f41b2c
AK
5167
5168 /* Check migration compatibility */
089f9d79 5169 if (err == 0 && check_mpb_migr_compatibility(super) != 0) {
e7b84f9d 5170 pr_err("Unsupported migration detected");
e2f41b2c
AK
5171 if (devname)
5172 fprintf(stderr, " on %s\n", devname);
5173 else
5174 fprintf(stderr, " (IMSM).\n");
5175
5176 err = 5;
5177 goto error;
5178 }
5179
a2b97981
DW
5180 err = 0;
5181
5182 error:
5183 while (super_list) {
5184 struct intel_super *s = super_list;
5185
5186 super_list = super_list->next;
5187 free_imsm(s);
5188 }
9587c373 5189
a2b97981
DW
5190 if (err)
5191 return err;
f7e7067b 5192
cdddbdbc 5193 *sbp = super;
4389ce73 5194 if (is_fd_valid(fd))
4dd2df09 5195 strcpy(st->container_devnm, fd2devnm(fd));
9587c373 5196 else
4dd2df09 5197 st->container_devnm[0] = 0;
a2b97981 5198 if (err == 0 && st->ss == NULL) {
bf5a934a 5199 st->ss = &super_imsm;
cdddbdbc
DW
5200 st->minor_version = 0;
5201 st->max_devs = IMSM_MAX_DEVICES;
5202 }
cdddbdbc
DW
5203 return 0;
5204}
2b959fbf 5205
ec50f7b6
LM
5206static int
5207get_devlist_super_block(struct md_list *devlist, struct intel_super **super_list,
5208 int *max, int keep_fd)
5209{
5210 struct md_list *tmpdev;
5211 int err = 0;
5212 int i = 0;
9587c373 5213
ec50f7b6
LM
5214 for (i = 0, tmpdev = devlist; tmpdev; tmpdev = tmpdev->next) {
5215 if (tmpdev->used != 1)
5216 continue;
5217 if (tmpdev->container == 1) {
ca9de185 5218 int lmax = 0;
ec50f7b6 5219 int fd = dev_open(tmpdev->devname, O_RDONLY|O_EXCL);
4389ce73 5220 if (!is_fd_valid(fd)) {
e7b84f9d 5221 pr_err("cannot open device %s: %s\n",
ec50f7b6
LM
5222 tmpdev->devname, strerror(errno));
5223 err = 8;
5224 goto error;
5225 }
5226 err = get_sra_super_block(fd, super_list,
5227 tmpdev->devname, &lmax,
5228 keep_fd);
5229 i += lmax;
5230 close(fd);
5231 if (err) {
5232 err = 7;
5233 goto error;
5234 }
5235 } else {
5236 int major = major(tmpdev->st_rdev);
5237 int minor = minor(tmpdev->st_rdev);
5238 err = get_super_block(super_list,
4dd2df09 5239 NULL,
ec50f7b6
LM
5240 tmpdev->devname,
5241 major, minor,
5242 keep_fd);
5243 i++;
5244 if (err) {
5245 err = 6;
5246 goto error;
5247 }
5248 }
5249 }
5250 error:
5251 *max = i;
5252 return err;
5253}
9587c373 5254
4dd2df09 5255static int get_super_block(struct intel_super **super_list, char *devnm, char *devname,
9587c373
LM
5256 int major, int minor, int keep_fd)
5257{
594dc1b8 5258 struct intel_super *s;
9587c373
LM
5259 char nm[32];
5260 int dfd = -1;
9587c373
LM
5261 int err = 0;
5262 int retry;
5263
5264 s = alloc_super();
5265 if (!s) {
5266 err = 1;
5267 goto error;
5268 }
5269
5270 sprintf(nm, "%d:%d", major, minor);
5271 dfd = dev_open(nm, O_RDWR);
4389ce73 5272 if (!is_fd_valid(dfd)) {
9587c373
LM
5273 err = 2;
5274 goto error;
5275 }
5276
aec01630
JS
5277 if (!get_dev_sector_size(dfd, NULL, &s->sector_size)) {
5278 err = 2;
5279 goto error;
5280 }
cb8f6859 5281 find_intel_hba_capability(dfd, s, devname);
9587c373
LM
5282 err = load_and_parse_mpb(dfd, s, NULL, keep_fd);
5283
5284 /* retry the load if we might have raced against mdmon */
4dd2df09 5285 if (err == 3 && devnm && mdmon_running(devnm))
9587c373 5286 for (retry = 0; retry < 3; retry++) {
239b3cc0 5287 sleep_for(0, MSEC_TO_NSEC(3), true);
9587c373
LM
5288 err = load_and_parse_mpb(dfd, s, NULL, keep_fd);
5289 if (err != 3)
5290 break;
5291 }
5292 error:
5293 if (!err) {
5294 s->next = *super_list;
5295 *super_list = s;
5296 } else {
5297 if (s)
8d67477f 5298 free_imsm(s);
4389ce73 5299 close_fd(&dfd);
9587c373 5300 }
4389ce73
MT
5301 if (!keep_fd)
5302 close_fd(&dfd);
9587c373
LM
5303 return err;
5304
5305}
5306
5307static int
5308get_sra_super_block(int fd, struct intel_super **super_list, char *devname, int *max, int keep_fd)
5309{
5310 struct mdinfo *sra;
4dd2df09 5311 char *devnm;
9587c373
LM
5312 struct mdinfo *sd;
5313 int err = 0;
5314 int i = 0;
4dd2df09 5315 sra = sysfs_read(fd, NULL, GET_LEVEL|GET_VERSION|GET_DEVS|GET_STATE);
9587c373
LM
5316 if (!sra)
5317 return 1;
5318
5319 if (sra->array.major_version != -1 ||
5320 sra->array.minor_version != -2 ||
5321 strcmp(sra->text_version, "imsm") != 0) {
5322 err = 1;
5323 goto error;
5324 }
5325 /* load all mpbs */
4dd2df09 5326 devnm = fd2devnm(fd);
9587c373 5327 for (sd = sra->devs, i = 0; sd; sd = sd->next, i++) {
4dd2df09 5328 if (get_super_block(super_list, devnm, devname,
9587c373
LM
5329 sd->disk.major, sd->disk.minor, keep_fd) != 0) {
5330 err = 7;
5331 goto error;
5332 }
5333 }
5334 error:
5335 sysfs_free(sra);
5336 *max = i;
5337 return err;
5338}
5339
2b959fbf
N
5340static int load_container_imsm(struct supertype *st, int fd, char *devname)
5341{
ec50f7b6 5342 return load_super_imsm_all(st, fd, &st->sb, devname, NULL, 1);
2b959fbf 5343}
cdddbdbc
DW
5344
5345static int load_super_imsm(struct supertype *st, int fd, char *devname)
5346{
5347 struct intel_super *super;
5348 int rv;
8a3544f8 5349 int retry;
cdddbdbc 5350
357ac106 5351 if (test_partition(fd))
691c6ee1
N
5352 /* IMSM not allowed on partitions */
5353 return 1;
5354
37424f13
DW
5355 free_super_imsm(st);
5356
49133e57 5357 super = alloc_super();
8d67477f
TM
5358 if (!super)
5359 return 1;
3a85bf0e
MG
5360
5361 if (!get_dev_sector_size(fd, NULL, &super->sector_size)) {
5362 free_imsm(super);
5363 return 1;
5364 }
ea2bc72b
LM
5365 /* Load hba and capabilities if they exist.
5366 * But do not preclude loading metadata in case capabilities or hba are
5367 * non-compliant and ignore_hw_compat is set.
5368 */
d424212e 5369 rv = find_intel_hba_capability(fd, super, devname);
f2f5c343 5370 /* no orom/efi or non-intel hba of the disk */
089f9d79 5371 if (rv != 0 && st->ignore_hw_compat == 0) {
f2f5c343 5372 if (devname)
e7b84f9d 5373 pr_err("No OROM/EFI properties for %s\n", devname);
f2f5c343
LM
5374 free_imsm(super);
5375 return 2;
5376 }
a2b97981 5377 rv = load_and_parse_mpb(fd, super, devname, 0);
cdddbdbc 5378
8a3544f8
AP
5379 /* retry the load if we might have raced against mdmon */
5380 if (rv == 3) {
f96b1302
AP
5381 struct mdstat_ent *mdstat = NULL;
5382 char *name = fd2kname(fd);
5383
5384 if (name)
5385 mdstat = mdstat_by_component(name);
8a3544f8
AP
5386
5387 if (mdstat && mdmon_running(mdstat->devnm) && getpid() != mdmon_pid(mdstat->devnm)) {
5388 for (retry = 0; retry < 3; retry++) {
239b3cc0 5389 sleep_for(0, MSEC_TO_NSEC(3), true);
8a3544f8
AP
5390 rv = load_and_parse_mpb(fd, super, devname, 0);
5391 if (rv != 3)
5392 break;
5393 }
5394 }
5395
5396 free_mdstat(mdstat);
5397 }
5398
cdddbdbc
DW
5399 if (rv) {
5400 if (devname)
7a862a02 5401 pr_err("Failed to load all information sections on %s\n", devname);
cdddbdbc
DW
5402 free_imsm(super);
5403 return rv;
5404 }
5405
5406 st->sb = super;
5407 if (st->ss == NULL) {
5408 st->ss = &super_imsm;
5409 st->minor_version = 0;
5410 st->max_devs = IMSM_MAX_DEVICES;
5411 }
8e59f3d8
AK
5412
5413 /* load migration record */
2f86fda3 5414 if (load_imsm_migr_rec(super) == 0) {
2e062e82
AK
5415 /* Check for unsupported migration features */
5416 if (check_mpb_migr_compatibility(super) != 0) {
e7b84f9d 5417 pr_err("Unsupported migration detected");
2e062e82
AK
5418 if (devname)
5419 fprintf(stderr, " on %s\n", devname);
5420 else
5421 fprintf(stderr, " (IMSM).\n");
5422 return 3;
5423 }
e2f41b2c
AK
5424 }
5425
cdddbdbc
DW
5426 return 0;
5427}
5428
ef6ffade
DW
5429static __u16 info_to_blocks_per_strip(mdu_array_info_t *info)
5430{
5431 if (info->level == 1)
5432 return 128;
5433 return info->chunk_size >> 9;
5434}
5435
5551b113
CA
5436static unsigned long long info_to_blocks_per_member(mdu_array_info_t *info,
5437 unsigned long long size)
fcfd9599 5438{
4025c288 5439 if (info->level == 1)
5551b113 5440 return size * 2;
4025c288 5441 else
5551b113 5442 return (size * 2) & ~(info_to_blocks_per_strip(info) - 1);
fcfd9599
DW
5443}
5444
4d1313e9
DW
5445static void imsm_update_version_info(struct intel_super *super)
5446{
5447 /* update the version and attributes */
5448 struct imsm_super *mpb = super->anchor;
5449 char *version;
5450 struct imsm_dev *dev;
5451 struct imsm_map *map;
5452 int i;
5453
5454 for (i = 0; i < mpb->num_raid_devs; i++) {
5455 dev = get_imsm_dev(super, i);
238c0a71 5456 map = get_imsm_map(dev, MAP_0);
4d1313e9
DW
5457 if (__le32_to_cpu(dev->size_high) > 0)
5458 mpb->attributes |= MPB_ATTRIB_2TB;
5459
5460 /* FIXME detect when an array spans a port multiplier */
5461 #if 0
5462 mpb->attributes |= MPB_ATTRIB_PM;
5463 #endif
5464
5465 if (mpb->num_raid_devs > 1 ||
5466 mpb->attributes != MPB_ATTRIB_CHECKSUM_VERIFY) {
5467 version = MPB_VERSION_ATTRIBS;
5468 switch (get_imsm_raid_level(map)) {
5469 case 0: mpb->attributes |= MPB_ATTRIB_RAID0; break;
5470 case 1: mpb->attributes |= MPB_ATTRIB_RAID1; break;
5471 case 10: mpb->attributes |= MPB_ATTRIB_RAID10; break;
5472 case 5: mpb->attributes |= MPB_ATTRIB_RAID5; break;
5473 }
5474 } else {
5475 if (map->num_members >= 5)
5476 version = MPB_VERSION_5OR6_DISK_ARRAY;
5477 else if (dev->status == DEV_CLONE_N_GO)
5478 version = MPB_VERSION_CNG;
5479 else if (get_imsm_raid_level(map) == 5)
5480 version = MPB_VERSION_RAID5;
5481 else if (map->num_members >= 3)
5482 version = MPB_VERSION_3OR4_DISK_ARRAY;
5483 else if (get_imsm_raid_level(map) == 1)
5484 version = MPB_VERSION_RAID1;
5485 else
5486 version = MPB_VERSION_RAID0;
5487 }
5488 strcpy(((char *) mpb->sig) + strlen(MPB_SIGNATURE), version);
5489 }
5490}
5491
e2eb503b
MT
5492/**
5493 * imsm_check_name() - check imsm naming criteria.
5494 * @super: &intel_super pointer, not NULL.
5495 * @name: name to check.
5496 * @verbose: verbose level.
5497 *
5498 * Name must be no longer than &MAX_RAID_SERIAL_LEN and must be unique across volumes.
5499 *
5500 * Returns: &true if @name matches, &false otherwise.
5501 */
5502static bool imsm_is_name_allowed(struct intel_super *super, const char * const name,
5503 const int verbose)
aa534678
DW
5504{
5505 struct imsm_super *mpb = super->anchor;
aa534678
DW
5506 int i;
5507
e2eb503b
MT
5508 if (is_string_lq(name, MAX_RAID_SERIAL_LEN + 1) == false) {
5509 pr_vrb("imsm: Name \"%s\" is too long\n", name);
5510 return false;
9bd99a90
RS
5511 }
5512
aa534678
DW
5513 for (i = 0; i < mpb->num_raid_devs; i++) {
5514 struct imsm_dev *dev = get_imsm_dev(super, i);
5515
5516 if (strncmp((char *) dev->volume, name, MAX_RAID_SERIAL_LEN) == 0) {
e2eb503b
MT
5517 pr_vrb("imsm: Name \"%s\" already exists\n", name);
5518 return false;
aa534678
DW
5519 }
5520 }
5521
e2eb503b 5522 return true;
aa534678
DW
5523}
5524
8b353278 5525static int init_super_imsm_volume(struct supertype *st, mdu_array_info_t *info,
5308f117 5526 struct shape *s, char *name,
83cd1e97
N
5527 char *homehost, int *uuid,
5528 long long data_offset)
cdddbdbc 5529{
c2c087e6
DW
5530 /* We are creating a volume inside a pre-existing container.
5531 * so st->sb is already set.
5532 */
5533 struct intel_super *super = st->sb;
f36a9ecd 5534 unsigned int sector_size = super->sector_size;
949c47a0 5535 struct imsm_super *mpb = super->anchor;
ba2de7ba 5536 struct intel_dev *dv;
c2c087e6
DW
5537 struct imsm_dev *dev;
5538 struct imsm_vol *vol;
5539 struct imsm_map *map;
5540 int idx = mpb->num_raid_devs;
5541 int i;
760365f9 5542 int namelen;
c2c087e6 5543 unsigned long long array_blocks;
2c092cad 5544 size_t size_old, size_new;
b53bfba6
TM
5545 unsigned int data_disks;
5546 unsigned long long size_per_member;
cdddbdbc 5547
88c32bb1 5548 if (super->orom && mpb->num_raid_devs >= super->orom->vpa) {
7a862a02 5549 pr_err("This imsm-container already has the maximum of %d volumes\n", super->orom->vpa);
c2c087e6
DW
5550 return 0;
5551 }
5552
2c092cad
DW
5553 /* ensure the mpb is large enough for the new data */
5554 size_old = __le32_to_cpu(mpb->mpb_size);
5555 size_new = disks_to_mpb_size(info->nr_disks);
5556 if (size_new > size_old) {
5557 void *mpb_new;
f36a9ecd 5558 size_t size_round = ROUND_UP(size_new, sector_size);
2c092cad 5559
f36a9ecd 5560 if (posix_memalign(&mpb_new, sector_size, size_round) != 0) {
e7b84f9d 5561 pr_err("could not allocate new mpb\n");
2c092cad
DW
5562 return 0;
5563 }
85337573
AO
5564 if (posix_memalign(&super->migr_rec_buf, MAX_SECTOR_SIZE,
5565 MIGR_REC_BUF_SECTORS*
5566 MAX_SECTOR_SIZE) != 0) {
1ade5cc1 5567 pr_err("could not allocate migr_rec buffer\n");
8e59f3d8
AK
5568 free(super->buf);
5569 free(super);
ea944c8f 5570 free(mpb_new);
8e59f3d8
AK
5571 return 0;
5572 }
2c092cad
DW
5573 memcpy(mpb_new, mpb, size_old);
5574 free(mpb);
5575 mpb = mpb_new;
949c47a0 5576 super->anchor = mpb_new;
2c092cad
DW
5577 mpb->mpb_size = __cpu_to_le32(size_new);
5578 memset(mpb_new + size_old, 0, size_round - size_old);
bbab0940 5579 super->len = size_round;
2c092cad 5580 }
bf5a934a 5581 super->current_vol = idx;
3960e579
DW
5582
5583 /* handle 'failed_disks' by either:
5584 * a) create dummy disk entries in the table if this the first
5585 * volume in the array. We add them here as this is the only
5586 * opportunity to add them. add_to_super_imsm_volume()
5587 * handles the non-failed disks and continues incrementing
5588 * mpb->num_disks.
5589 * b) validate that 'failed_disks' matches the current number
5590 * of missing disks if the container is populated
d23fe947 5591 */
3960e579 5592 if (super->current_vol == 0) {
d23fe947 5593 mpb->num_disks = 0;
3960e579
DW
5594 for (i = 0; i < info->failed_disks; i++) {
5595 struct imsm_disk *disk;
5596
5597 mpb->num_disks++;
5598 disk = __get_imsm_disk(mpb, i);
5599 disk->status = CONFIGURED_DISK | FAILED_DISK;
5600 disk->scsi_id = __cpu_to_le32(~(__u32)0);
5601 snprintf((char *) disk->serial, MAX_RAID_SERIAL_LEN,
5b975129 5602 "missing:%d", (__u8)i);
3960e579
DW
5603 }
5604 find_missing(super);
5605 } else {
5606 int missing = 0;
5607 struct dl *d;
5608
5609 for (d = super->missing; d; d = d->next)
5610 missing++;
5611 if (info->failed_disks > missing) {
e7b84f9d 5612 pr_err("unable to add 'missing' disk to container\n");
3960e579
DW
5613 return 0;
5614 }
5615 }
5a038140 5616
e2eb503b 5617 if (imsm_is_name_allowed(super, name, 1) == false)
aa534678 5618 return 0;
e2eb503b 5619
503975b9
N
5620 dv = xmalloc(sizeof(*dv));
5621 dev = xcalloc(1, sizeof(*dev) + sizeof(__u32) * (info->raid_disks - 1));
760365f9
JS
5622 /*
5623 * Explicitly allow truncating to not confuse gcc's
5624 * -Werror=stringop-truncation
5625 */
5626 namelen = min((int) strlen(name), MAX_RAID_SERIAL_LEN);
5627 memcpy(dev->volume, name, namelen);
e03640bd 5628 array_blocks = calc_array_size(info->level, info->raid_disks,
03bcbc65 5629 info->layout, info->chunk_size,
b53bfba6
TM
5630 s->size * BLOCKS_PER_KB);
5631 data_disks = get_data_disks(info->level, info->layout,
5632 info->raid_disks);
5633 array_blocks = round_size_to_mb(array_blocks, data_disks);
5634 size_per_member = array_blocks / data_disks;
979d38be 5635
fcc2c9da 5636 set_imsm_dev_size(dev, array_blocks);
1a2487c2 5637 dev->status = (DEV_READ_COALESCING | DEV_WRITE_COALESCING);
c2c087e6
DW
5638 vol = &dev->vol;
5639 vol->migr_state = 0;
1484e727 5640 set_migr_type(dev, MIGR_INIT);
3960e579 5641 vol->dirty = !info->state;
4036e7ee 5642 set_vol_curr_migr_unit(dev, 0);
238c0a71 5643 map = get_imsm_map(dev, MAP_0);
5551b113 5644 set_pba_of_lba0(map, super->create_offset);
ef6ffade 5645 map->blocks_per_strip = __cpu_to_le16(info_to_blocks_per_strip(info));
0556e1a2 5646 map->failed_disk_num = ~0;
bf4442ab 5647 if (info->level > 0)
fffaf1ff
N
5648 map->map_state = (info->state ? IMSM_T_STATE_NORMAL
5649 : IMSM_T_STATE_UNINITIALIZED);
bf4442ab
AK
5650 else
5651 map->map_state = info->failed_disks ? IMSM_T_STATE_FAILED :
5652 IMSM_T_STATE_NORMAL;
252d23c0 5653 map->ddf = 1;
ef6ffade
DW
5654
5655 if (info->level == 1 && info->raid_disks > 2) {
38950822
AW
5656 free(dev);
5657 free(dv);
7a862a02 5658 pr_err("imsm does not support more than 2 disksin a raid1 volume\n");
ef6ffade
DW
5659 return 0;
5660 }
81062a36
DW
5661
5662 map->raid_level = info->level;
1c275381 5663 if (info->level == 10)
c2c087e6 5664 map->raid_level = 1;
1c275381 5665 set_num_domains(map);
ef6ffade 5666
44490938
MD
5667 size_per_member += NUM_BLOCKS_DIRTY_STRIPE_REGION;
5668 set_blocks_per_member(map, info_to_blocks_per_member(info,
5669 size_per_member /
5670 BLOCKS_PER_KB));
5671
c2c087e6 5672 map->num_members = info->raid_disks;
1c275381 5673 update_num_data_stripes(map, array_blocks);
c2c087e6
DW
5674 for (i = 0; i < map->num_members; i++) {
5675 /* initialized in add_to_super */
4eb26970 5676 set_imsm_ord_tbl_ent(map, i, IMSM_ORD_REBUILD);
c2c087e6 5677 }
949c47a0 5678 mpb->num_raid_devs++;
2a24dc1b
PB
5679 mpb->num_raid_devs_created++;
5680 dev->my_vol_raid_dev_num = mpb->num_raid_devs_created;
ba2de7ba 5681
b7580566 5682 if (s->consistency_policy <= CONSISTENCY_POLICY_RESYNC) {
c2462068 5683 dev->rwh_policy = RWH_MULTIPLE_OFF;
2432ce9b 5684 } else if (s->consistency_policy == CONSISTENCY_POLICY_PPL) {
c2462068 5685 dev->rwh_policy = RWH_MULTIPLE_DISTRIBUTED;
2432ce9b
AP
5686 } else {
5687 free(dev);
5688 free(dv);
5689 pr_err("imsm does not support consistency policy %s\n",
5f21d674 5690 map_num_s(consistency_policies, s->consistency_policy));
2432ce9b
AP
5691 return 0;
5692 }
5693
ba2de7ba
DW
5694 dv->dev = dev;
5695 dv->index = super->current_vol;
5696 dv->next = super->devlist;
5697 super->devlist = dv;
c2c087e6 5698
4d1313e9
DW
5699 imsm_update_version_info(super);
5700
c2c087e6 5701 return 1;
cdddbdbc
DW
5702}
5703
bf5a934a 5704static int init_super_imsm(struct supertype *st, mdu_array_info_t *info,
5308f117 5705 struct shape *s, char *name,
83cd1e97
N
5706 char *homehost, int *uuid,
5707 unsigned long long data_offset)
bf5a934a
DW
5708{
5709 /* This is primarily called by Create when creating a new array.
5710 * We will then get add_to_super called for each component, and then
5711 * write_init_super called to write it out to each device.
5712 * For IMSM, Create can create on fresh devices or on a pre-existing
5713 * array.
5714 * To create on a pre-existing array a different method will be called.
5715 * This one is just for fresh drives.
5716 */
5717 struct intel_super *super;
5718 struct imsm_super *mpb;
5719 size_t mpb_size;
4d1313e9 5720 char *version;
bf5a934a 5721
83cd1e97 5722 if (data_offset != INVALID_SECTORS) {
ed503f89 5723 pr_err("data-offset not supported by imsm\n");
83cd1e97
N
5724 return 0;
5725 }
5726
bf5a934a 5727 if (st->sb)
5308f117 5728 return init_super_imsm_volume(st, info, s, name, homehost, uuid,
83cd1e97 5729 data_offset);
e683ca88
DW
5730
5731 if (info)
5732 mpb_size = disks_to_mpb_size(info->nr_disks);
5733 else
f36a9ecd 5734 mpb_size = MAX_SECTOR_SIZE;
bf5a934a 5735
49133e57 5736 super = alloc_super();
f36a9ecd
PB
5737 if (super &&
5738 posix_memalign(&super->buf, MAX_SECTOR_SIZE, mpb_size) != 0) {
8d67477f 5739 free_imsm(super);
e683ca88
DW
5740 super = NULL;
5741 }
5742 if (!super) {
1ade5cc1 5743 pr_err("could not allocate superblock\n");
bf5a934a
DW
5744 return 0;
5745 }
de44e46f
PB
5746 if (posix_memalign(&super->migr_rec_buf, MAX_SECTOR_SIZE,
5747 MIGR_REC_BUF_SECTORS*MAX_SECTOR_SIZE) != 0) {
1ade5cc1 5748 pr_err("could not allocate migr_rec buffer\n");
8e59f3d8 5749 free(super->buf);
8d67477f 5750 free_imsm(super);
8e59f3d8
AK
5751 return 0;
5752 }
e683ca88 5753 memset(super->buf, 0, mpb_size);
ef649044 5754 mpb = super->buf;
e683ca88
DW
5755 mpb->mpb_size = __cpu_to_le32(mpb_size);
5756 st->sb = super;
5757
5758 if (info == NULL) {
5759 /* zeroing superblock */
5760 return 0;
5761 }
bf5a934a 5762
4d1313e9
DW
5763 mpb->attributes = MPB_ATTRIB_CHECKSUM_VERIFY;
5764
5765 version = (char *) mpb->sig;
5766 strcpy(version, MPB_SIGNATURE);
5767 version += strlen(MPB_SIGNATURE);
5768 strcpy(version, MPB_VERSION_RAID0);
bf5a934a 5769
bf5a934a
DW
5770 return 1;
5771}
5772
f2cc4f7d
AO
5773static int drive_validate_sector_size(struct intel_super *super, struct dl *dl)
5774{
5775 unsigned int member_sector_size;
5776
4389ce73 5777 if (!is_fd_valid(dl->fd)) {
f2cc4f7d
AO
5778 pr_err("Invalid file descriptor for %s\n", dl->devname);
5779 return 0;
5780 }
5781
5782 if (!get_dev_sector_size(dl->fd, dl->devname, &member_sector_size))
5783 return 0;
5784 if (member_sector_size != super->sector_size)
5785 return 0;
5786 return 1;
5787}
5788
f20c3968 5789static int add_to_super_imsm_volume(struct supertype *st, mdu_disk_info_t *dk,
bf5a934a
DW
5790 int fd, char *devname)
5791{
5792 struct intel_super *super = st->sb;
d23fe947 5793 struct imsm_super *mpb = super->anchor;
3960e579 5794 struct imsm_disk *_disk;
bf5a934a
DW
5795 struct imsm_dev *dev;
5796 struct imsm_map *map;
3960e579 5797 struct dl *dl, *df;
4eb26970 5798 int slot;
9a7df595
MT
5799 int autolayout = 0;
5800
5801 if (!is_fd_valid(fd))
5802 autolayout = 1;
bf5a934a 5803
949c47a0 5804 dev = get_imsm_dev(super, super->current_vol);
238c0a71 5805 map = get_imsm_map(dev, MAP_0);
bf5a934a 5806
208933a7 5807 if (! (dk->state & (1<<MD_DISK_SYNC))) {
e7b84f9d 5808 pr_err("%s: Cannot add spare devices to IMSM volume\n",
208933a7
N
5809 devname);
5810 return 1;
5811 }
5812
9a7df595
MT
5813 for (dl = super->disks; dl ; dl = dl->next) {
5814 if (autolayout) {
efb30e7f
DW
5815 if (dl->raiddisk == dk->raid_disk)
5816 break;
9a7df595
MT
5817 } else if (dl->major == dk->major && dl->minor == dk->minor)
5818 break;
efb30e7f 5819 }
d23fe947 5820
208933a7 5821 if (!dl) {
9a7df595
MT
5822 if (!autolayout)
5823 pr_err("%s is not a member of the same container.\n",
5824 devname);
f20c3968 5825 return 1;
208933a7 5826 }
bf5a934a 5827
9a7df595
MT
5828 if (!autolayout && super->current_vol > 0) {
5829 int _slot = get_disk_slot_in_dev(super, 0, dl->index);
5830
5831 if (_slot != dk->raid_disk) {
5832 pr_err("Member %s is in %d slot for the first volume, but is in %d slot for a new volume.\n",
5833 dl->devname, _slot, dk->raid_disk);
5834 pr_err("Raid members are in different order than for the first volume, aborting.\n");
5835 return 1;
5836 }
5837 }
5838
59632db9
MZ
5839 if (mpb->num_disks == 0)
5840 if (!get_dev_sector_size(dl->fd, dl->devname,
5841 &super->sector_size))
5842 return 1;
5843
f2cc4f7d
AO
5844 if (!drive_validate_sector_size(super, dl)) {
5845 pr_err("Combining drives of different sector size in one volume is not allowed\n");
5846 return 1;
5847 }
5848
d23fe947
DW
5849 /* add a pristine spare to the metadata */
5850 if (dl->index < 0) {
5851 dl->index = super->anchor->num_disks;
5852 super->anchor->num_disks++;
5853 }
4eb26970
DW
5854 /* Check the device has not already been added */
5855 slot = get_imsm_disk_slot(map, dl->index);
5856 if (slot >= 0 &&
238c0a71 5857 (get_imsm_ord_tbl_ent(dev, slot, MAP_X) & IMSM_ORD_REBUILD) == 0) {
e7b84f9d 5858 pr_err("%s has been included in this array twice\n",
4eb26970
DW
5859 devname);
5860 return 1;
5861 }
656b6b5a 5862 set_imsm_ord_tbl_ent(map, dk->raid_disk, dl->index);
ee5aad5a 5863 dl->disk.status = CONFIGURED_DISK;
d23fe947 5864
3960e579
DW
5865 /* update size of 'missing' disks to be at least as large as the
5866 * largest acitve member (we only have dummy missing disks when
5867 * creating the first volume)
5868 */
5869 if (super->current_vol == 0) {
5870 for (df = super->missing; df; df = df->next) {
5551b113
CA
5871 if (total_blocks(&dl->disk) > total_blocks(&df->disk))
5872 set_total_blocks(&df->disk, total_blocks(&dl->disk));
3960e579
DW
5873 _disk = __get_imsm_disk(mpb, df->index);
5874 *_disk = df->disk;
5875 }
5876 }
5877
5878 /* refresh unset/failed slots to point to valid 'missing' entries */
5879 for (df = super->missing; df; df = df->next)
5880 for (slot = 0; slot < mpb->num_disks; slot++) {
238c0a71 5881 __u32 ord = get_imsm_ord_tbl_ent(dev, slot, MAP_X);
3960e579
DW
5882
5883 if ((ord & IMSM_ORD_REBUILD) == 0)
5884 continue;
5885 set_imsm_ord_tbl_ent(map, slot, df->index | IMSM_ORD_REBUILD);
1ace8403 5886 if (is_gen_migration(dev)) {
238c0a71
AK
5887 struct imsm_map *map2 = get_imsm_map(dev,
5888 MAP_1);
0a108d63 5889 int slot2 = get_imsm_disk_slot(map2, df->index);
089f9d79 5890 if (slot2 < map2->num_members && slot2 >= 0) {
1ace8403 5891 __u32 ord2 = get_imsm_ord_tbl_ent(dev,
238c0a71
AK
5892 slot2,
5893 MAP_1);
1ace8403
AK
5894 if ((unsigned)df->index ==
5895 ord_to_idx(ord2))
5896 set_imsm_ord_tbl_ent(map2,
0a108d63 5897 slot2,
1ace8403
AK
5898 df->index |
5899 IMSM_ORD_REBUILD);
5900 }
5901 }
3960e579
DW
5902 dprintf("set slot:%d to missing disk:%d\n", slot, df->index);
5903 break;
5904 }
5905
d23fe947
DW
5906 /* if we are creating the first raid device update the family number */
5907 if (super->current_vol == 0) {
5908 __u32 sum;
5909 struct imsm_dev *_dev = __get_imsm_dev(mpb, 0);
d23fe947 5910
3960e579 5911 _disk = __get_imsm_disk(mpb, dl->index);
756a15f3 5912 if (!_disk) {
e7b84f9d 5913 pr_err("BUG mpb setup error\n");
791b666a
AW
5914 return 1;
5915 }
d23fe947
DW
5916 *_dev = *dev;
5917 *_disk = dl->disk;
148acb7b
DW
5918 sum = random32();
5919 sum += __gen_imsm_checksum(mpb);
d23fe947 5920 mpb->family_num = __cpu_to_le32(sum);
148acb7b 5921 mpb->orig_family_num = mpb->family_num;
e48aed3c 5922 mpb->creation_time = __cpu_to_le64((__u64)time(NULL));
d23fe947 5923 }
ca0748fa 5924 super->current_disk = dl;
f20c3968 5925 return 0;
bf5a934a
DW
5926}
5927
a8619d23
AK
5928/* mark_spare()
5929 * Function marks disk as spare and restores disk serial
5930 * in case it was previously marked as failed by takeover operation
5931 * reruns:
5932 * -1 : critical error
5933 * 0 : disk is marked as spare but serial is not set
5934 * 1 : success
5935 */
5936int mark_spare(struct dl *disk)
5937{
5938 __u8 serial[MAX_RAID_SERIAL_LEN];
5939 int ret_val = -1;
5940
5941 if (!disk)
5942 return ret_val;
5943
5944 ret_val = 0;
6da53c0e 5945 if (!imsm_read_serial(disk->fd, NULL, serial, MAX_RAID_SERIAL_LEN)) {
a8619d23
AK
5946 /* Restore disk serial number, because takeover marks disk
5947 * as failed and adds to serial ':0' before it becomes
5948 * a spare disk.
5949 */
5950 serialcpy(disk->serial, serial);
5951 serialcpy(disk->disk.serial, serial);
5952 ret_val = 1;
5953 }
5954 disk->disk.status = SPARE_DISK;
5955 disk->index = -1;
5956
5957 return ret_val;
5958}
88654014 5959
12724c01
TM
5960
5961static int write_super_imsm_spare(struct intel_super *super, struct dl *d);
5962
f20c3968 5963static int add_to_super_imsm(struct supertype *st, mdu_disk_info_t *dk,
72ca9bcf
N
5964 int fd, char *devname,
5965 unsigned long long data_offset)
cdddbdbc 5966{
c2c087e6 5967 struct intel_super *super = st->sb;
c2c087e6
DW
5968 struct dl *dd;
5969 unsigned long long size;
fa7bb6f8 5970 unsigned int member_sector_size;
f2f27e63 5971 __u32 id;
c2c087e6
DW
5972 int rv;
5973 struct stat stb;
5974
88654014
LM
5975 /* If we are on an RAID enabled platform check that the disk is
5976 * attached to the raid controller.
5977 * We do not need to test disks attachment for container based additions,
5978 * they shall be already tested when container was created/assembled.
88c32bb1 5979 */
d424212e 5980 rv = find_intel_hba_capability(fd, super, devname);
f2f5c343 5981 /* no orom/efi or non-intel hba of the disk */
f0f5a016
LM
5982 if (rv != 0) {
5983 dprintf("capability: %p fd: %d ret: %d\n",
5984 super->orom, fd, rv);
5985 return 1;
88c32bb1
DW
5986 }
5987
f20c3968
DW
5988 if (super->current_vol >= 0)
5989 return add_to_super_imsm_volume(st, dk, fd, devname);
bf5a934a 5990
c2c087e6 5991 fstat(fd, &stb);
503975b9 5992 dd = xcalloc(sizeof(*dd), 1);
c2c087e6
DW
5993 dd->major = major(stb.st_rdev);
5994 dd->minor = minor(stb.st_rdev);
503975b9 5995 dd->devname = devname ? xstrdup(devname) : NULL;
c2c087e6 5996 dd->fd = fd;
689c9bf3 5997 dd->e = NULL;
1a64be56 5998 dd->action = DISK_ADD;
6da53c0e 5999 rv = imsm_read_serial(fd, devname, dd->serial, MAX_RAID_SERIAL_LEN);
32ba9157 6000 if (rv) {
e7b84f9d 6001 pr_err("failed to retrieve scsi serial, aborting\n");
3a85bf0e 6002 __free_imsm_disk(dd, 0);
0030e8d6 6003 abort();
c2c087e6 6004 }
7c798f87 6005
20bee0f8
PB
6006 if (super->hba && ((super->hba->type == SYS_DEV_NVME) ||
6007 (super->hba->type == SYS_DEV_VMD))) {
6008 int i;
7c798f87
MT
6009 char cntrl_path[PATH_MAX];
6010 char *cntrl_name;
6011 char pci_dev_path[PATH_MAX];
20bee0f8 6012
7c798f87
MT
6013 if (!diskfd_to_devpath(fd, 2, pci_dev_path) ||
6014 !diskfd_to_devpath(fd, 1, cntrl_path)) {
8662f92d 6015 pr_err("failed to get dev paths, aborting\n");
3a85bf0e 6016 __free_imsm_disk(dd, 0);
a8f3cfd5
MT
6017 return 1;
6018 }
6019
7c798f87
MT
6020 cntrl_name = basename(cntrl_path);
6021 if (is_multipath_nvme(fd))
6022 pr_err("%s controller supports Multi-Path I/O, Intel (R) VROC does not support multipathing\n",
6023 cntrl_name);
6024
6025 if (devpath_to_vendor(pci_dev_path) == 0x8086) {
20bee0f8
PB
6026 /*
6027 * If Intel's NVMe drive has serial ended with
6028 * "-A","-B","-1" or "-2" it means that this is "x8"
6029 * device (double drive on single PCIe card).
6030 * User should be warned about potential data loss.
6031 */
6032 for (i = MAX_RAID_SERIAL_LEN-1; i > 0; i--) {
6033 /* Skip empty character at the end */
6034 if (dd->serial[i] == 0)
6035 continue;
6036
6037 if (((dd->serial[i] == 'A') ||
6038 (dd->serial[i] == 'B') ||
6039 (dd->serial[i] == '1') ||
6040 (dd->serial[i] == '2')) &&
6041 (dd->serial[i-1] == '-'))
6042 pr_err("\tThe action you are about to take may put your data at risk.\n"
6043 "\tPlease note that x8 devices may consist of two separate x4 devices "
6044 "located on a single PCIe port.\n"
6045 "\tRAID 0 is the only supported configuration for this type of x8 device.\n");
6046 break;
6047 }
32716c51
PB
6048 } else if (super->hba->type == SYS_DEV_VMD && super->orom &&
6049 !imsm_orom_has_tpv_support(super->orom)) {
6050 pr_err("\tPlatform configuration does not support non-Intel NVMe drives.\n"
8b751247 6051 "\tPlease refer to Intel(R) RSTe/VROC user guide.\n");
3a85bf0e 6052 __free_imsm_disk(dd, 0);
32716c51 6053 return 1;
20bee0f8
PB
6054 }
6055 }
c2c087e6 6056
c2c087e6 6057 get_dev_size(fd, NULL, &size);
3a85bf0e
MG
6058 if (!get_dev_sector_size(fd, NULL, &member_sector_size)) {
6059 __free_imsm_disk(dd, 0);
aec01630 6060 return 1;
3a85bf0e 6061 }
fa7bb6f8
PB
6062
6063 if (super->sector_size == 0) {
6064 /* this a first device, so sector_size is not set yet */
6065 super->sector_size = member_sector_size;
fa7bb6f8
PB
6066 }
6067
71e5411e 6068 /* clear migr_rec when adding disk to container */
85337573
AO
6069 memset(super->migr_rec_buf, 0, MIGR_REC_BUF_SECTORS*MAX_SECTOR_SIZE);
6070 if (lseek64(fd, size - MIGR_REC_SECTOR_POSITION*member_sector_size,
de44e46f 6071 SEEK_SET) >= 0) {
466070ad 6072 if ((unsigned int)write(fd, super->migr_rec_buf,
85337573
AO
6073 MIGR_REC_BUF_SECTORS*member_sector_size) !=
6074 MIGR_REC_BUF_SECTORS*member_sector_size)
71e5411e
PB
6075 perror("Write migr_rec failed");
6076 }
6077
c2c087e6 6078 size /= 512;
1f24f035 6079 serialcpy(dd->disk.serial, dd->serial);
5551b113
CA
6080 set_total_blocks(&dd->disk, size);
6081 if (__le32_to_cpu(dd->disk.total_blocks_hi) > 0) {
6082 struct imsm_super *mpb = super->anchor;
6083 mpb->attributes |= MPB_ATTRIB_2TB_DISK;
6084 }
a8619d23 6085 mark_spare(dd);
c2c087e6 6086 if (sysfs_disk_to_scsi_id(fd, &id) == 0)
b9f594fe 6087 dd->disk.scsi_id = __cpu_to_le32(id);
c2c087e6 6088 else
b9f594fe 6089 dd->disk.scsi_id = __cpu_to_le32(0);
43dad3d6
DW
6090
6091 if (st->update_tail) {
1a64be56
LM
6092 dd->next = super->disk_mgmt_list;
6093 super->disk_mgmt_list = dd;
43dad3d6 6094 } else {
12724c01
TM
6095 /* this is called outside of mdmon
6096 * write initial spare metadata
6097 * mdmon will overwrite it.
6098 */
43dad3d6
DW
6099 dd->next = super->disks;
6100 super->disks = dd;
12724c01 6101 write_super_imsm_spare(super, dd);
43dad3d6 6102 }
f20c3968
DW
6103
6104 return 0;
cdddbdbc
DW
6105}
6106
1a64be56
LM
6107static int remove_from_super_imsm(struct supertype *st, mdu_disk_info_t *dk)
6108{
6109 struct intel_super *super = st->sb;
6110 struct dl *dd;
6111
6112 /* remove from super works only in mdmon - for communication
6113 * manager - monitor. Check if communication memory buffer
6114 * is prepared.
6115 */
6116 if (!st->update_tail) {
1ade5cc1 6117 pr_err("shall be used in mdmon context only\n");
1a64be56
LM
6118 return 1;
6119 }
503975b9 6120 dd = xcalloc(1, sizeof(*dd));
1a64be56
LM
6121 dd->major = dk->major;
6122 dd->minor = dk->minor;
1a64be56 6123 dd->fd = -1;
a8619d23 6124 mark_spare(dd);
1a64be56
LM
6125 dd->action = DISK_REMOVE;
6126
6127 dd->next = super->disk_mgmt_list;
6128 super->disk_mgmt_list = dd;
6129
1a64be56
LM
6130 return 0;
6131}
6132
f796af5d
DW
6133static int store_imsm_mpb(int fd, struct imsm_super *mpb);
6134
6135static union {
f36a9ecd 6136 char buf[MAX_SECTOR_SIZE];
f796af5d 6137 struct imsm_super anchor;
f36a9ecd 6138} spare_record __attribute__ ((aligned(MAX_SECTOR_SIZE)));
c2c087e6 6139
12724c01
TM
6140
6141static int write_super_imsm_spare(struct intel_super *super, struct dl *d)
d23fe947 6142{
d23fe947 6143 struct imsm_super *mpb = super->anchor;
f796af5d 6144 struct imsm_super *spare = &spare_record.anchor;
d23fe947 6145 __u32 sum;
12724c01
TM
6146
6147 if (d->index != -1)
6148 return 1;
d23fe947 6149
68641cdb
JS
6150 spare->mpb_size = __cpu_to_le32(sizeof(struct imsm_super));
6151 spare->generation_num = __cpu_to_le32(1UL);
f796af5d 6152 spare->attributes = MPB_ATTRIB_CHECKSUM_VERIFY;
68641cdb
JS
6153 spare->num_disks = 1;
6154 spare->num_raid_devs = 0;
6155 spare->cache_size = mpb->cache_size;
6156 spare->pwr_cycle_count = __cpu_to_le32(1);
f796af5d
DW
6157
6158 snprintf((char *) spare->sig, MAX_SIGNATURE_LENGTH,
6159 MPB_SIGNATURE MPB_VERSION_RAID0);
d23fe947 6160
12724c01
TM
6161 spare->disk[0] = d->disk;
6162 if (__le32_to_cpu(d->disk.total_blocks_hi) > 0)
6163 spare->attributes |= MPB_ATTRIB_2TB_DISK;
6164
6165 if (super->sector_size == 4096)
6166 convert_to_4k_imsm_disk(&spare->disk[0]);
d23fe947 6167
12724c01
TM
6168 sum = __gen_imsm_checksum(spare);
6169 spare->family_num = __cpu_to_le32(sum);
6170 spare->orig_family_num = 0;
6171 sum = __gen_imsm_checksum(spare);
6172 spare->check_sum = __cpu_to_le32(sum);
027c374f 6173
12724c01
TM
6174 if (store_imsm_mpb(d->fd, spare)) {
6175 pr_err("failed for device %d:%d %s\n",
6176 d->major, d->minor, strerror(errno));
6177 return 1;
6178 }
6179
6180 return 0;
6181}
6182/* spare records have their own family number and do not have any defined raid
6183 * devices
6184 */
6185static int write_super_imsm_spares(struct intel_super *super, int doclose)
6186{
6187 struct dl *d;
f36a9ecd 6188
12724c01
TM
6189 for (d = super->disks; d; d = d->next) {
6190 if (d->index != -1)
6191 continue;
d23fe947 6192
12724c01 6193 if (write_super_imsm_spare(super, d))
e74255d9 6194 return 1;
12724c01 6195
4389ce73
MT
6196 if (doclose)
6197 close_fd(&d->fd);
d23fe947
DW
6198 }
6199
e74255d9 6200 return 0;
d23fe947
DW
6201}
6202
36988a3d 6203static int write_super_imsm(struct supertype *st, int doclose)
cdddbdbc 6204{
36988a3d 6205 struct intel_super *super = st->sb;
f36a9ecd 6206 unsigned int sector_size = super->sector_size;
949c47a0 6207 struct imsm_super *mpb = super->anchor;
c2c087e6
DW
6208 struct dl *d;
6209 __u32 generation;
6210 __u32 sum;
d23fe947 6211 int spares = 0;
949c47a0 6212 int i;
a48ac0a8 6213 __u32 mpb_size = sizeof(struct imsm_super) - sizeof(struct imsm_disk);
36988a3d 6214 int num_disks = 0;
146c6260 6215 int clear_migration_record = 1;
bbab0940 6216 __u32 bbm_log_size;
cdddbdbc 6217
c2c087e6
DW
6218 /* 'generation' is incremented everytime the metadata is written */
6219 generation = __le32_to_cpu(mpb->generation_num);
6220 generation++;
6221 mpb->generation_num = __cpu_to_le32(generation);
6222
148acb7b
DW
6223 /* fix up cases where previous mdadm releases failed to set
6224 * orig_family_num
6225 */
6226 if (mpb->orig_family_num == 0)
6227 mpb->orig_family_num = mpb->family_num;
6228
d23fe947 6229 for (d = super->disks; d; d = d->next) {
8796fdc4 6230 if (d->index == -1)
d23fe947 6231 spares++;
36988a3d 6232 else {
d23fe947 6233 mpb->disk[d->index] = d->disk;
36988a3d
AK
6234 num_disks++;
6235 }
d23fe947 6236 }
36988a3d 6237 for (d = super->missing; d; d = d->next) {
47ee5a45 6238 mpb->disk[d->index] = d->disk;
36988a3d
AK
6239 num_disks++;
6240 }
6241 mpb->num_disks = num_disks;
6242 mpb_size += sizeof(struct imsm_disk) * mpb->num_disks;
b9f594fe 6243
949c47a0
DW
6244 for (i = 0; i < mpb->num_raid_devs; i++) {
6245 struct imsm_dev *dev = __get_imsm_dev(mpb, i);
36988a3d 6246 struct imsm_dev *dev2 = get_imsm_dev(super, i);
756a15f3
MG
6247
6248 imsm_copy_dev(dev, dev2);
6249 mpb_size += sizeof_imsm_dev(dev, 0);
6250
146c6260
AK
6251 if (is_gen_migration(dev2))
6252 clear_migration_record = 0;
949c47a0 6253 }
bbab0940
TM
6254
6255 bbm_log_size = get_imsm_bbm_log_size(super->bbm_log);
6256
6257 if (bbm_log_size) {
6258 memcpy((void *)mpb + mpb_size, super->bbm_log, bbm_log_size);
6259 mpb->attributes |= MPB_ATTRIB_BBM;
6260 } else
6261 mpb->attributes &= ~MPB_ATTRIB_BBM;
6262
6263 super->anchor->bbm_log_size = __cpu_to_le32(bbm_log_size);
6264 mpb_size += bbm_log_size;
a48ac0a8 6265 mpb->mpb_size = __cpu_to_le32(mpb_size);
949c47a0 6266
bbab0940
TM
6267#ifdef DEBUG
6268 assert(super->len == 0 || mpb_size <= super->len);
6269#endif
6270
c2c087e6 6271 /* recalculate checksum */
949c47a0 6272 sum = __gen_imsm_checksum(mpb);
c2c087e6
DW
6273 mpb->check_sum = __cpu_to_le32(sum);
6274
51d83f5d
AK
6275 if (super->clean_migration_record_by_mdmon) {
6276 clear_migration_record = 1;
6277 super->clean_migration_record_by_mdmon = 0;
6278 }
146c6260 6279 if (clear_migration_record)
de44e46f 6280 memset(super->migr_rec_buf, 0,
85337573 6281 MIGR_REC_BUF_SECTORS*MAX_SECTOR_SIZE);
146c6260 6282
f36a9ecd
PB
6283 if (sector_size == 4096)
6284 convert_to_4k(super);
6285
d23fe947 6286 /* write the mpb for disks that compose raid devices */
c2c087e6 6287 for (d = super->disks; d ; d = d->next) {
86c54047 6288 if (d->index < 0 || is_failed(&d->disk))
d23fe947 6289 continue;
30602f53 6290
146c6260
AK
6291 if (clear_migration_record) {
6292 unsigned long long dsize;
6293
6294 get_dev_size(d->fd, NULL, &dsize);
de44e46f
PB
6295 if (lseek64(d->fd, dsize - sector_size,
6296 SEEK_SET) >= 0) {
466070ad
PB
6297 if ((unsigned int)write(d->fd,
6298 super->migr_rec_buf,
de44e46f
PB
6299 MIGR_REC_BUF_SECTORS*sector_size) !=
6300 MIGR_REC_BUF_SECTORS*sector_size)
9e2d750d 6301 perror("Write migr_rec failed");
146c6260
AK
6302 }
6303 }
51d83f5d
AK
6304
6305 if (store_imsm_mpb(d->fd, mpb))
6306 fprintf(stderr,
1ade5cc1
N
6307 "failed for device %d:%d (fd: %d)%s\n",
6308 d->major, d->minor,
51d83f5d
AK
6309 d->fd, strerror(errno));
6310
4389ce73
MT
6311 if (doclose)
6312 close_fd(&d->fd);
c2c087e6
DW
6313 }
6314
d23fe947
DW
6315 if (spares)
6316 return write_super_imsm_spares(super, doclose);
6317
e74255d9 6318 return 0;
c2c087e6
DW
6319}
6320
9b1fb677 6321static int create_array(struct supertype *st, int dev_idx)
43dad3d6
DW
6322{
6323 size_t len;
6324 struct imsm_update_create_array *u;
6325 struct intel_super *super = st->sb;
9b1fb677 6326 struct imsm_dev *dev = get_imsm_dev(super, dev_idx);
238c0a71 6327 struct imsm_map *map = get_imsm_map(dev, MAP_0);
54c2c1ea
DW
6328 struct disk_info *inf;
6329 struct imsm_disk *disk;
6330 int i;
43dad3d6 6331
54c2c1ea
DW
6332 len = sizeof(*u) - sizeof(*dev) + sizeof_imsm_dev(dev, 0) +
6333 sizeof(*inf) * map->num_members;
503975b9 6334 u = xmalloc(len);
43dad3d6 6335 u->type = update_create_array;
9b1fb677 6336 u->dev_idx = dev_idx;
43dad3d6 6337 imsm_copy_dev(&u->dev, dev);
54c2c1ea
DW
6338 inf = get_disk_info(u);
6339 for (i = 0; i < map->num_members; i++) {
238c0a71 6340 int idx = get_imsm_disk_idx(dev, i, MAP_X);
9b1fb677 6341
54c2c1ea 6342 disk = get_imsm_disk(super, idx);
1ca5c8e0
N
6343 if (!disk)
6344 disk = get_imsm_missing(super, idx);
54c2c1ea
DW
6345 serialcpy(inf[i].serial, disk->serial);
6346 }
43dad3d6
DW
6347 append_metadata_update(st, u, len);
6348
6349 return 0;
6350}
6351
1a64be56 6352static int mgmt_disk(struct supertype *st)
43dad3d6
DW
6353{
6354 struct intel_super *super = st->sb;
6355 size_t len;
1a64be56 6356 struct imsm_update_add_remove_disk *u;
43dad3d6 6357
1a64be56 6358 if (!super->disk_mgmt_list)
43dad3d6
DW
6359 return 0;
6360
6361 len = sizeof(*u);
503975b9 6362 u = xmalloc(len);
1a64be56 6363 u->type = update_add_remove_disk;
43dad3d6
DW
6364 append_metadata_update(st, u, len);
6365
6366 return 0;
6367}
2432ce9b
AP
6368
6369__u32 crc32c_le(__u32 crc, unsigned char const *p, size_t len);
6370
e397cefe
AP
6371static int write_ppl_header(unsigned long long ppl_sector, int fd, void *buf)
6372{
6373 struct ppl_header *ppl_hdr = buf;
6374 int ret;
6375
6376 ppl_hdr->checksum = __cpu_to_le32(~crc32c_le(~0, buf, PPL_HEADER_SIZE));
6377
6378 if (lseek64(fd, ppl_sector * 512, SEEK_SET) < 0) {
6379 ret = -errno;
6380 perror("Failed to seek to PPL header location");
6381 return ret;
6382 }
6383
6384 if (write(fd, buf, PPL_HEADER_SIZE) != PPL_HEADER_SIZE) {
6385 ret = -errno;
6386 perror("Write PPL header failed");
6387 return ret;
6388 }
6389
6390 fsync(fd);
6391
6392 return 0;
6393}
6394
2432ce9b
AP
6395static int write_init_ppl_imsm(struct supertype *st, struct mdinfo *info, int fd)
6396{
6397 struct intel_super *super = st->sb;
6398 void *buf;
6399 struct ppl_header *ppl_hdr;
6400 int ret;
6401
b2514242
PB
6402 /* first clear entire ppl space */
6403 ret = zero_disk_range(fd, info->ppl_sector, info->ppl_size);
6404 if (ret)
6405 return ret;
6406
6407 ret = posix_memalign(&buf, MAX_SECTOR_SIZE, PPL_HEADER_SIZE);
2432ce9b
AP
6408 if (ret) {
6409 pr_err("Failed to allocate PPL header buffer\n");
e397cefe 6410 return -ret;
2432ce9b
AP
6411 }
6412
6413 memset(buf, 0, PPL_HEADER_SIZE);
6414 ppl_hdr = buf;
6415 memset(ppl_hdr->reserved, 0xff, PPL_HDR_RESERVED);
6416 ppl_hdr->signature = __cpu_to_le32(super->anchor->orig_family_num);
b23d0750
AP
6417
6418 if (info->mismatch_cnt) {
6419 /*
6420 * We are overwriting an invalid ppl. Make one entry with wrong
6421 * checksum to prevent the kernel from skipping resync.
6422 */
6423 ppl_hdr->entries_count = __cpu_to_le32(1);
6424 ppl_hdr->entries[0].checksum = ~0;
6425 }
6426
e397cefe 6427 ret = write_ppl_header(info->ppl_sector, fd, buf);
2432ce9b
AP
6428
6429 free(buf);
6430 return ret;
6431}
6432
e397cefe
AP
6433static int is_rebuilding(struct imsm_dev *dev);
6434
2432ce9b
AP
6435static int validate_ppl_imsm(struct supertype *st, struct mdinfo *info,
6436 struct mdinfo *disk)
6437{
6438 struct intel_super *super = st->sb;
6439 struct dl *d;
e397cefe 6440 void *buf_orig, *buf, *buf_prev = NULL;
2432ce9b 6441 int ret = 0;
e397cefe 6442 struct ppl_header *ppl_hdr = NULL;
2432ce9b
AP
6443 __u32 crc;
6444 struct imsm_dev *dev;
2432ce9b 6445 __u32 idx;
44b6b876
PB
6446 unsigned int i;
6447 unsigned long long ppl_offset = 0;
6448 unsigned long long prev_gen_num = 0;
2432ce9b
AP
6449
6450 if (disk->disk.raid_disk < 0)
6451 return 0;
6452
2432ce9b 6453 dev = get_imsm_dev(super, info->container_member);
2fc0fc63 6454 idx = get_imsm_disk_idx(dev, disk->disk.raid_disk, MAP_0);
2432ce9b
AP
6455 d = get_imsm_dl_disk(super, idx);
6456
6457 if (!d || d->index < 0 || is_failed(&d->disk))
e397cefe
AP
6458 return 0;
6459
6460 if (posix_memalign(&buf_orig, MAX_SECTOR_SIZE, PPL_HEADER_SIZE * 2)) {
6461 pr_err("Failed to allocate PPL header buffer\n");
6462 return -1;
6463 }
6464 buf = buf_orig;
2432ce9b 6465
44b6b876
PB
6466 ret = 1;
6467 while (ppl_offset < MULTIPLE_PPL_AREA_SIZE_IMSM) {
e397cefe
AP
6468 void *tmp;
6469
44b6b876 6470 dprintf("Checking potential PPL at offset: %llu\n", ppl_offset);
2432ce9b 6471
44b6b876
PB
6472 if (lseek64(d->fd, info->ppl_sector * 512 + ppl_offset,
6473 SEEK_SET) < 0) {
6474 perror("Failed to seek to PPL header location");
6475 ret = -1;
e397cefe 6476 break;
44b6b876 6477 }
2432ce9b 6478
44b6b876
PB
6479 if (read(d->fd, buf, PPL_HEADER_SIZE) != PPL_HEADER_SIZE) {
6480 perror("Read PPL header failed");
6481 ret = -1;
e397cefe 6482 break;
44b6b876 6483 }
2432ce9b 6484
44b6b876 6485 ppl_hdr = buf;
2432ce9b 6486
44b6b876
PB
6487 crc = __le32_to_cpu(ppl_hdr->checksum);
6488 ppl_hdr->checksum = 0;
6489
6490 if (crc != ~crc32c_le(~0, buf, PPL_HEADER_SIZE)) {
6491 dprintf("Wrong PPL header checksum on %s\n",
6492 d->devname);
e397cefe 6493 break;
44b6b876
PB
6494 }
6495
6496 if (prev_gen_num > __le64_to_cpu(ppl_hdr->generation)) {
6497 /* previous was newest, it was already checked */
e397cefe 6498 break;
44b6b876
PB
6499 }
6500
6501 if ((__le32_to_cpu(ppl_hdr->signature) !=
6502 super->anchor->orig_family_num)) {
6503 dprintf("Wrong PPL header signature on %s\n",
6504 d->devname);
6505 ret = 1;
e397cefe 6506 break;
44b6b876
PB
6507 }
6508
6509 ret = 0;
6510 prev_gen_num = __le64_to_cpu(ppl_hdr->generation);
2432ce9b 6511
44b6b876
PB
6512 ppl_offset += PPL_HEADER_SIZE;
6513 for (i = 0; i < __le32_to_cpu(ppl_hdr->entries_count); i++)
6514 ppl_offset +=
6515 __le32_to_cpu(ppl_hdr->entries[i].pp_size);
e397cefe
AP
6516
6517 if (!buf_prev)
6518 buf_prev = buf + PPL_HEADER_SIZE;
6519 tmp = buf_prev;
6520 buf_prev = buf;
6521 buf = tmp;
2432ce9b
AP
6522 }
6523
e397cefe
AP
6524 if (buf_prev) {
6525 buf = buf_prev;
6526 ppl_hdr = buf_prev;
6527 }
2432ce9b 6528
54148aba
PB
6529 /*
6530 * Update metadata to use mutliple PPLs area (1MB).
6531 * This is done once for all RAID members
6532 */
6533 if (info->consistency_policy == CONSISTENCY_POLICY_PPL &&
6534 info->ppl_size != (MULTIPLE_PPL_AREA_SIZE_IMSM >> 9)) {
6535 char subarray[20];
6536 struct mdinfo *member_dev;
6537
6538 sprintf(subarray, "%d", info->container_member);
6539
6540 if (mdmon_running(st->container_devnm))
6541 st->update_tail = &st->updates;
6542
03312b52 6543 if (st->ss->update_subarray(st, subarray, UOPT_PPL, NULL)) {
54148aba
PB
6544 pr_err("Failed to update subarray %s\n",
6545 subarray);
6546 } else {
6547 if (st->update_tail)
6548 flush_metadata_updates(st);
6549 else
6550 st->ss->sync_metadata(st);
6551 info->ppl_size = (MULTIPLE_PPL_AREA_SIZE_IMSM >> 9);
6552 for (member_dev = info->devs; member_dev;
6553 member_dev = member_dev->next)
6554 member_dev->ppl_size =
6555 (MULTIPLE_PPL_AREA_SIZE_IMSM >> 9);
6556 }
6557 }
6558
b23d0750 6559 if (ret == 1) {
2fc0fc63
AP
6560 struct imsm_map *map = get_imsm_map(dev, MAP_X);
6561
50b9c10d
PB
6562 if (map->map_state == IMSM_T_STATE_UNINITIALIZED ||
6563 (map->map_state == IMSM_T_STATE_NORMAL &&
2ec9d182 6564 !(dev->vol.dirty & RAIDVOL_DIRTY)) ||
e397cefe 6565 (is_rebuilding(dev) &&
4036e7ee 6566 vol_curr_migr_unit(dev) == 0 &&
2ec9d182 6567 get_imsm_disk_idx(dev, disk->disk.raid_disk, MAP_1) != idx))
b23d0750
AP
6568 ret = st->ss->write_init_ppl(st, info, d->fd);
6569 else
6570 info->mismatch_cnt++;
e397cefe
AP
6571 } else if (ret == 0 &&
6572 ppl_hdr->entries_count == 0 &&
6573 is_rebuilding(dev) &&
6574 info->resync_start == 0) {
6575 /*
6576 * The header has no entries - add a single empty entry and
6577 * rewrite the header to prevent the kernel from going into
6578 * resync after an interrupted rebuild.
6579 */
6580 ppl_hdr->entries_count = __cpu_to_le32(1);
6581 ret = write_ppl_header(info->ppl_sector, d->fd, buf);
b23d0750 6582 }
2432ce9b 6583
e397cefe
AP
6584 free(buf_orig);
6585
2432ce9b
AP
6586 return ret;
6587}
6588
2432ce9b
AP
6589static int write_init_ppl_imsm_all(struct supertype *st, struct mdinfo *info)
6590{
6591 struct intel_super *super = st->sb;
6592 struct dl *d;
6593 int ret = 0;
6594
6595 if (info->consistency_policy != CONSISTENCY_POLICY_PPL ||
6596 info->array.level != 5)
6597 return 0;
6598
6599 for (d = super->disks; d ; d = d->next) {
6600 if (d->index < 0 || is_failed(&d->disk))
6601 continue;
6602
6603 ret = st->ss->write_init_ppl(st, info, d->fd);
6604 if (ret)
6605 break;
6606 }
6607
6608 return ret;
6609}
43dad3d6 6610
fbc42556
JR
6611/*******************************************************************************
6612 * Function: write_init_bitmap_imsm_vol
6613 * Description: Write a bitmap header and prepares the area for the bitmap.
6614 * Parameters:
6615 * st : supertype information
6616 * vol_idx : the volume index to use
6617 *
6618 * Returns:
6619 * 0 : success
6620 * -1 : fail
6621 ******************************************************************************/
6622static int write_init_bitmap_imsm_vol(struct supertype *st, int vol_idx)
6623{
6624 struct intel_super *super = st->sb;
6625 int prev_current_vol = super->current_vol;
6626 struct dl *d;
6627 int ret = 0;
6628
6629 super->current_vol = vol_idx;
6630 for (d = super->disks; d; d = d->next) {
6631 if (d->index < 0 || is_failed(&d->disk))
6632 continue;
6633 ret = st->ss->write_bitmap(st, d->fd, NoUpdate);
6634 if (ret)
6635 break;
6636 }
6637 super->current_vol = prev_current_vol;
6638 return ret;
6639}
6640
6641/*******************************************************************************
6642 * Function: write_init_bitmap_imsm_all
6643 * Description: Write a bitmap header and prepares the area for the bitmap.
6644 * Operation is executed for volumes with CONSISTENCY_POLICY_BITMAP.
6645 * Parameters:
6646 * st : supertype information
6647 * info : info about the volume where the bitmap should be written
6648 * vol_idx : the volume index to use
6649 *
6650 * Returns:
6651 * 0 : success
6652 * -1 : fail
6653 ******************************************************************************/
6654static int write_init_bitmap_imsm_all(struct supertype *st, struct mdinfo *info,
6655 int vol_idx)
6656{
6657 int ret = 0;
6658
6659 if (info && (info->consistency_policy == CONSISTENCY_POLICY_BITMAP))
6660 ret = write_init_bitmap_imsm_vol(st, vol_idx);
6661
6662 return ret;
6663}
6664
c2c087e6
DW
6665static int write_init_super_imsm(struct supertype *st)
6666{
9b1fb677
DW
6667 struct intel_super *super = st->sb;
6668 int current_vol = super->current_vol;
2432ce9b
AP
6669 int rv = 0;
6670 struct mdinfo info;
6671
6672 getinfo_super_imsm(st, &info, NULL);
9b1fb677
DW
6673
6674 /* we are done with current_vol reset it to point st at the container */
6675 super->current_vol = -1;
6676
8273f55e 6677 if (st->update_tail) {
43dad3d6
DW
6678 /* queue the recently created array / added disk
6679 * as a metadata update */
8273f55e 6680
43dad3d6 6681 /* determine if we are creating a volume or adding a disk */
9b1fb677 6682 if (current_vol < 0) {
1a64be56
LM
6683 /* in the mgmt (add/remove) disk case we are running
6684 * in mdmon context, so don't close fd's
43dad3d6 6685 */
2432ce9b
AP
6686 rv = mgmt_disk(st);
6687 } else {
fbc42556 6688 /* adding the second volume to the array */
2432ce9b 6689 rv = write_init_ppl_imsm_all(st, &info);
fbc42556
JR
6690 if (!rv)
6691 rv = write_init_bitmap_imsm_all(st, &info, current_vol);
2432ce9b
AP
6692 if (!rv)
6693 rv = create_array(st, current_vol);
6694 }
d682f344
N
6695 } else {
6696 struct dl *d;
6697 for (d = super->disks; d; d = d->next)
ba728be7 6698 Kill(d->devname, NULL, 0, -1, 1);
fbc42556 6699 if (current_vol >= 0) {
2432ce9b 6700 rv = write_init_ppl_imsm_all(st, &info);
fbc42556
JR
6701 if (!rv)
6702 rv = write_init_bitmap_imsm_all(st, &info, current_vol);
6703 }
6704
2432ce9b
AP
6705 if (!rv)
6706 rv = write_super_imsm(st, 1);
d682f344 6707 }
2432ce9b
AP
6708
6709 return rv;
cdddbdbc
DW
6710}
6711
e683ca88 6712static int store_super_imsm(struct supertype *st, int fd)
cdddbdbc 6713{
e683ca88
DW
6714 struct intel_super *super = st->sb;
6715 struct imsm_super *mpb = super ? super->anchor : NULL;
551c80c1 6716
e683ca88 6717 if (!mpb)
ad97895e
DW
6718 return 1;
6719
f36a9ecd
PB
6720 if (super->sector_size == 4096)
6721 convert_to_4k(super);
e683ca88 6722 return store_imsm_mpb(fd, mpb);
cdddbdbc
DW
6723}
6724
cdddbdbc 6725static int validate_geometry_imsm_container(struct supertype *st, int level,
1f5d54a0 6726 int raiddisks,
af4348dd
N
6727 unsigned long long data_offset,
6728 char *dev,
2c514b71
NB
6729 unsigned long long *freesize,
6730 int verbose)
cdddbdbc 6731{
c2c087e6
DW
6732 int fd;
6733 unsigned long long ldsize;
8662f92d 6734 struct intel_super *super = NULL;
f2f5c343 6735 int rv = 0;
cdddbdbc 6736
6f2af6a4 6737 if (!is_container(level))
c2c087e6
DW
6738 return 0;
6739 if (!dev)
6740 return 1;
6741
dca80fcd 6742 fd = dev_open(dev, O_RDONLY|O_EXCL);
4389ce73
MT
6743 if (!is_fd_valid(fd)) {
6744 pr_vrb("imsm: Cannot open %s: %s\n", dev, strerror(errno));
c2c087e6
DW
6745 return 0;
6746 }
8662f92d
MT
6747 if (!get_dev_size(fd, dev, &ldsize))
6748 goto exit;
f2f5c343
LM
6749
6750 /* capabilities retrieve could be possible
6751 * note that there is no fd for the disks in array.
6752 */
6753 super = alloc_super();
8662f92d
MT
6754 if (!super)
6755 goto exit;
6756
6757 if (!get_dev_sector_size(fd, NULL, &super->sector_size))
6758 goto exit;
fa7bb6f8 6759
ba728be7 6760 rv = find_intel_hba_capability(fd, super, verbose > 0 ? dev : NULL);
f2f5c343
LM
6761 if (rv != 0) {
6762#if DEBUG
6763 char str[256];
6764 fd2devname(fd, str);
1ade5cc1 6765 dprintf("fd: %d %s orom: %p rv: %d raiddisk: %d\n",
f2f5c343
LM
6766 fd, str, super->orom, rv, raiddisks);
6767#endif
6768 /* no orom/efi or non-intel hba of the disk */
8662f92d
MT
6769 rv = 0;
6770 goto exit;
f2f5c343 6771 }
9126b9a8
CA
6772 if (super->orom) {
6773 if (raiddisks > super->orom->tds) {
6774 if (verbose)
7a862a02 6775 pr_err("%d exceeds maximum number of platform supported disks: %d\n",
9126b9a8 6776 raiddisks, super->orom->tds);
8662f92d 6777 goto exit;
9126b9a8
CA
6778 }
6779 if ((super->orom->attr & IMSM_OROM_ATTR_2TB_DISK) == 0 &&
6780 (ldsize >> 9) >> 32 > 0) {
6781 if (verbose)
e7b84f9d 6782 pr_err("%s exceeds maximum platform supported size\n", dev);
8662f92d
MT
6783 goto exit;
6784 }
6785
6786 if (super->hba->type == SYS_DEV_VMD ||
6787 super->hba->type == SYS_DEV_NVME) {
6788 if (!imsm_is_nvme_namespace_supported(fd, 1)) {
6789 if (verbose)
6790 pr_err("NVMe namespace %s is not supported by IMSM\n",
6791 basename(dev));
6792 goto exit;
6793 }
9126b9a8 6794 }
f2f5c343 6795 }
1f5d54a0
MT
6796 if (freesize)
6797 *freesize = avail_size_imsm(st, ldsize >> 9, data_offset);
8662f92d
MT
6798 rv = 1;
6799exit:
6800 if (super)
6801 free_imsm(super);
6802 close(fd);
c2c087e6 6803
8662f92d 6804 return rv;
cdddbdbc
DW
6805}
6806
0dcecb2e
DW
6807static unsigned long long find_size(struct extent *e, int *idx, int num_extents)
6808{
6809 const unsigned long long base_start = e[*idx].start;
6810 unsigned long long end = base_start + e[*idx].size;
6811 int i;
6812
6813 if (base_start == end)
6814 return 0;
6815
6816 *idx = *idx + 1;
6817 for (i = *idx; i < num_extents; i++) {
6818 /* extend overlapping extents */
6819 if (e[i].start >= base_start &&
6820 e[i].start <= end) {
6821 if (e[i].size == 0)
6822 return 0;
6823 if (e[i].start + e[i].size > end)
6824 end = e[i].start + e[i].size;
6825 } else if (e[i].start > end) {
6826 *idx = i;
6827 break;
6828 }
6829 }
6830
6831 return end - base_start;
6832}
6833
aa19fdd4 6834/** merge_extents() - analyze extents and get free size.
9bc426fa 6835 * @super: Intel metadata, not NULL.
aa19fdd4 6836 * @expanding: if set, we are expanding &super->current_vol.
9bc426fa 6837 *
aa19fdd4
MT
6838 * Build a composite disk with all known extents and generate a size given the
6839 * "all disks in an array must share a common start offset" constraint.
6840 * If a volume is expanded, then return free space after the volume.
9bc426fa 6841 *
aa19fdd4 6842 * Return: Free space or 0 on failure.
9bc426fa 6843 */
aa19fdd4 6844static unsigned long long merge_extents(struct intel_super *super, const bool expanding)
0dcecb2e 6845{
9bc426fa 6846 struct extent *e;
0dcecb2e 6847 struct dl *dl;
aa19fdd4
MT
6848 int i, j, pos_vol_idx = -1;
6849 int extent_idx = 0;
6850 int sum_extents = 0;
6851 unsigned long long pos = 0;
b9d77223 6852 unsigned long long start = 0;
1dea84ae
MT
6853 unsigned long long free_size = 0;
6854
6855 unsigned long pre_reservation = 0;
6856 unsigned long post_reservation = IMSM_RESERVED_SECTORS;
6857 unsigned long reservation_size;
0dcecb2e 6858
9bc426fa
MT
6859 for (dl = super->disks; dl; dl = dl->next)
6860 if (dl->e)
6861 sum_extents += dl->extent_cnt;
6862 e = xcalloc(sum_extents, sizeof(struct extent));
6863
0dcecb2e
DW
6864 /* coalesce and sort all extents. also, check to see if we need to
6865 * reserve space between member arrays
6866 */
6867 j = 0;
6868 for (dl = super->disks; dl; dl = dl->next) {
6869 if (!dl->e)
6870 continue;
6871 for (i = 0; i < dl->extent_cnt; i++)
6872 e[j++] = dl->e[i];
6873 }
6874 qsort(e, sum_extents, sizeof(*e), cmp_extent);
6875
6876 /* merge extents */
6877 i = 0;
6878 j = 0;
6879 while (i < sum_extents) {
6880 e[j].start = e[i].start;
aa19fdd4 6881 e[j].vol = e[i].vol;
0dcecb2e
DW
6882 e[j].size = find_size(e, &i, sum_extents);
6883 j++;
6884 if (e[j-1].size == 0)
6885 break;
6886 }
6887
0dcecb2e
DW
6888 i = 0;
6889 do {
aa19fdd4 6890 unsigned long long esize = e[i].start - pos;
0dcecb2e 6891
1dea84ae
MT
6892 if (expanding ? pos_vol_idx == super->current_vol : esize >= free_size) {
6893 free_size = esize;
0dcecb2e 6894 start = pos;
aa19fdd4 6895 extent_idx = i;
0dcecb2e 6896 }
aa19fdd4 6897
0dcecb2e 6898 pos = e[i].start + e[i].size;
aa19fdd4
MT
6899 pos_vol_idx = e[i].vol;
6900
0dcecb2e
DW
6901 i++;
6902 } while (e[i-1].size);
0dcecb2e 6903
1dea84ae
MT
6904 if (free_size == 0) {
6905 dprintf("imsm: Cannot find free size.\n");
6906 free(e);
a7dd165b 6907 return 0;
1dea84ae 6908 }
a7dd165b 6909
1dea84ae
MT
6910 if (!expanding && extent_idx != 0)
6911 /*
6912 * Not a real first volume in a container is created, pre_reservation is needed.
6913 */
6914 pre_reservation = IMSM_RESERVED_SECTORS;
0dcecb2e 6915
1dea84ae
MT
6916 if (e[extent_idx].size == 0)
6917 /*
6918 * extent_idx points to the metadata, post_reservation is allready done.
6919 */
6920 post_reservation = 0;
6921 free(e);
0dcecb2e 6922
1dea84ae
MT
6923 reservation_size = pre_reservation + post_reservation;
6924
6925 if (free_size < reservation_size) {
6926 dprintf("imsm: Reservation size is greater than free space.\n");
6927 return 0;
6928 }
0dcecb2e 6929
1dea84ae
MT
6930 super->create_offset = start + pre_reservation;
6931 return free_size - reservation_size;
0dcecb2e
DW
6932}
6933
88c32bb1
DW
6934static int is_raid_level_supported(const struct imsm_orom *orom, int level, int raiddisks)
6935{
6936 if (level < 0 || level == 6 || level == 4)
6937 return 0;
6938
6939 /* if we have an orom prevent invalid raid levels */
6940 if (orom)
6941 switch (level) {
6942 case 0: return imsm_orom_has_raid0(orom);
6943 case 1:
6944 if (raiddisks > 2)
6945 return imsm_orom_has_raid1e(orom);
1c556e92
DW
6946 return imsm_orom_has_raid1(orom) && raiddisks == 2;
6947 case 10: return imsm_orom_has_raid10(orom) && raiddisks == 4;
6948 case 5: return imsm_orom_has_raid5(orom) && raiddisks > 2;
88c32bb1
DW
6949 }
6950 else
6951 return 1; /* not on an Intel RAID platform so anything goes */
6952
6953 return 0;
6954}
6955
ca9de185
LM
6956static int
6957active_arrays_by_format(char *name, char* hba, struct md_list **devlist,
6958 int dpa, int verbose)
6959{
6960 struct mdstat_ent *mdstat = mdstat_read(0, 0);
594dc1b8 6961 struct mdstat_ent *memb;
ca9de185
LM
6962 int count = 0;
6963 int num = 0;
594dc1b8 6964 struct md_list *dv;
ca9de185
LM
6965 int found;
6966
6967 for (memb = mdstat ; memb ; memb = memb->next) {
6968 if (memb->metadata_version &&
fc54fe7a 6969 (strncmp(memb->metadata_version, "external:", 9) == 0) &&
ca9de185
LM
6970 (strcmp(&memb->metadata_version[9], name) == 0) &&
6971 !is_subarray(memb->metadata_version+9) &&
6972 memb->members) {
6973 struct dev_member *dev = memb->members;
6974 int fd = -1;
4389ce73 6975 while (dev && !is_fd_valid(fd)) {
503975b9 6976 char *path = xmalloc(strlen(dev->name) + strlen("/dev/") + 1);
dd5ab402 6977 num = snprintf(path, PATH_MAX, "%s%s", "/dev/", dev->name);
503975b9
N
6978 if (num > 0)
6979 fd = open(path, O_RDONLY, 0);
4389ce73 6980 if (num <= 0 || !is_fd_valid(fd)) {
676e87a8 6981 pr_vrb("Cannot open %s: %s\n",
503975b9 6982 dev->name, strerror(errno));
ca9de185 6983 }
503975b9 6984 free(path);
ca9de185
LM
6985 dev = dev->next;
6986 }
6987 found = 0;
4389ce73 6988 if (is_fd_valid(fd) && disk_attached_to_hba(fd, hba)) {
ca9de185
LM
6989 struct mdstat_ent *vol;
6990 for (vol = mdstat ; vol ; vol = vol->next) {
089f9d79 6991 if (vol->active > 0 &&
ca9de185 6992 vol->metadata_version &&
9581efb1 6993 is_container_member(vol, memb->devnm)) {
ca9de185
LM
6994 found++;
6995 count++;
6996 }
6997 }
6998 if (*devlist && (found < dpa)) {
503975b9 6999 dv = xcalloc(1, sizeof(*dv));
9581efb1
N
7000 dv->devname = xmalloc(strlen(memb->devnm) + strlen("/dev/") + 1);
7001 sprintf(dv->devname, "%s%s", "/dev/", memb->devnm);
503975b9
N
7002 dv->found = found;
7003 dv->used = 0;
7004 dv->next = *devlist;
7005 *devlist = dv;
ca9de185
LM
7006 }
7007 }
4389ce73 7008 close_fd(&fd);
ca9de185
LM
7009 }
7010 }
7011 free_mdstat(mdstat);
7012 return count;
7013}
7014
7015#ifdef DEBUG_LOOP
7016static struct md_list*
7017get_loop_devices(void)
7018{
7019 int i;
7020 struct md_list *devlist = NULL;
594dc1b8 7021 struct md_list *dv;
ca9de185
LM
7022
7023 for(i = 0; i < 12; i++) {
503975b9
N
7024 dv = xcalloc(1, sizeof(*dv));
7025 dv->devname = xmalloc(40);
ca9de185
LM
7026 sprintf(dv->devname, "/dev/loop%d", i);
7027 dv->next = devlist;
7028 devlist = dv;
7029 }
7030 return devlist;
7031}
7032#endif
7033
7034static struct md_list*
7035get_devices(const char *hba_path)
7036{
7037 struct md_list *devlist = NULL;
594dc1b8 7038 struct md_list *dv;
ca9de185
LM
7039 struct dirent *ent;
7040 DIR *dir;
7041 int err = 0;
7042
7043#if DEBUG_LOOP
7044 devlist = get_loop_devices();
7045 return devlist;
7046#endif
7047 /* scroll through /sys/dev/block looking for devices attached to
7048 * this hba
7049 */
7050 dir = opendir("/sys/dev/block");
7051 for (ent = dir ? readdir(dir) : NULL; ent; ent = readdir(dir)) {
7052 int fd;
7053 char buf[1024];
7054 int major, minor;
7055 char *path = NULL;
7056 if (sscanf(ent->d_name, "%d:%d", &major, &minor) != 2)
7057 continue;
7c798f87 7058 path = devt_to_devpath(makedev(major, minor), 1, NULL);
ca9de185
LM
7059 if (!path)
7060 continue;
7061 if (!path_attached_to_hba(path, hba_path)) {
7062 free(path);
7063 path = NULL;
7064 continue;
7065 }
7066 free(path);
7067 path = NULL;
7068 fd = dev_open(ent->d_name, O_RDONLY);
4389ce73 7069 if (is_fd_valid(fd)) {
ca9de185
LM
7070 fd2devname(fd, buf);
7071 close(fd);
7072 } else {
e7b84f9d 7073 pr_err("cannot open device: %s\n",
ca9de185
LM
7074 ent->d_name);
7075 continue;
7076 }
7077
503975b9
N
7078 dv = xcalloc(1, sizeof(*dv));
7079 dv->devname = xstrdup(buf);
ca9de185
LM
7080 dv->next = devlist;
7081 devlist = dv;
7082 }
7083 if (err) {
7084 while(devlist) {
7085 dv = devlist;
7086 devlist = devlist->next;
7087 free(dv->devname);
7088 free(dv);
7089 }
7090 }
562aa102 7091 closedir(dir);
ca9de185
LM
7092 return devlist;
7093}
7094
7095static int
7096count_volumes_list(struct md_list *devlist, char *homehost,
7097 int verbose, int *found)
7098{
7099 struct md_list *tmpdev;
7100 int count = 0;
594dc1b8 7101 struct supertype *st;
ca9de185
LM
7102
7103 /* first walk the list of devices to find a consistent set
7104 * that match the criterea, if that is possible.
7105 * We flag the ones we like with 'used'.
7106 */
7107 *found = 0;
7108 st = match_metadata_desc_imsm("imsm");
7109 if (st == NULL) {
676e87a8 7110 pr_vrb("cannot allocate memory for imsm supertype\n");
ca9de185
LM
7111 return 0;
7112 }
7113
7114 for (tmpdev = devlist; tmpdev; tmpdev = tmpdev->next) {
7115 char *devname = tmpdev->devname;
0a6bff09 7116 dev_t rdev;
ca9de185
LM
7117 struct supertype *tst;
7118 int dfd;
7119 if (tmpdev->used > 1)
7120 continue;
7121 tst = dup_super(st);
7122 if (tst == NULL) {
676e87a8 7123 pr_vrb("cannot allocate memory for imsm supertype\n");
ca9de185
LM
7124 goto err_1;
7125 }
7126 tmpdev->container = 0;
7127 dfd = dev_open(devname, O_RDONLY|O_EXCL);
4389ce73 7128 if (!is_fd_valid(dfd)) {
1ade5cc1 7129 dprintf("cannot open device %s: %s\n",
ca9de185
LM
7130 devname, strerror(errno));
7131 tmpdev->used = 2;
0a6bff09 7132 } else if (!fstat_is_blkdev(dfd, devname, &rdev)) {
ca9de185
LM
7133 tmpdev->used = 2;
7134 } else if (must_be_container(dfd)) {
7135 struct supertype *cst;
7136 cst = super_by_fd(dfd, NULL);
7137 if (cst == NULL) {
1ade5cc1 7138 dprintf("cannot recognize container type %s\n",
ca9de185
LM
7139 devname);
7140 tmpdev->used = 2;
7141 } else if (tst->ss != st->ss) {
1ade5cc1 7142 dprintf("non-imsm container - ignore it: %s\n",
ca9de185
LM
7143 devname);
7144 tmpdev->used = 2;
7145 } else if (!tst->ss->load_container ||
7146 tst->ss->load_container(tst, dfd, NULL))
7147 tmpdev->used = 2;
7148 else {
7149 tmpdev->container = 1;
7150 }
7151 if (cst)
7152 cst->ss->free_super(cst);
7153 } else {
0a6bff09 7154 tmpdev->st_rdev = rdev;
ca9de185 7155 if (tst->ss->load_super(tst,dfd, NULL)) {
1ade5cc1 7156 dprintf("no RAID superblock on %s\n",
ca9de185
LM
7157 devname);
7158 tmpdev->used = 2;
7159 } else if (tst->ss->compare_super == NULL) {
1ade5cc1 7160 dprintf("Cannot assemble %s metadata on %s\n",
ca9de185
LM
7161 tst->ss->name, devname);
7162 tmpdev->used = 2;
7163 }
7164 }
4389ce73
MT
7165 close_fd(&dfd);
7166
ca9de185
LM
7167 if (tmpdev->used == 2 || tmpdev->used == 4) {
7168 /* Ignore unrecognised devices during auto-assembly */
7169 goto loop;
7170 }
7171 else {
7172 struct mdinfo info;
7173 tst->ss->getinfo_super(tst, &info, NULL);
7174
7175 if (st->minor_version == -1)
7176 st->minor_version = tst->minor_version;
7177
7178 if (memcmp(info.uuid, uuid_zero,
7179 sizeof(int[4])) == 0) {
7180 /* this is a floating spare. It cannot define
7181 * an array unless there are no more arrays of
7182 * this type to be found. It can be included
7183 * in an array of this type though.
7184 */
7185 tmpdev->used = 3;
7186 goto loop;
7187 }
7188
7189 if (st->ss != tst->ss ||
7190 st->minor_version != tst->minor_version ||
c7b8547c 7191 st->ss->compare_super(st, tst, 1) != 0) {
ca9de185
LM
7192 /* Some mismatch. If exactly one array matches this host,
7193 * we can resolve on that one.
7194 * Or, if we are auto assembling, we just ignore the second
7195 * for now.
7196 */
1ade5cc1 7197 dprintf("superblock on %s doesn't match others - assembly aborted\n",
ca9de185
LM
7198 devname);
7199 goto loop;
7200 }
7201 tmpdev->used = 1;
7202 *found = 1;
7203 dprintf("found: devname: %s\n", devname);
7204 }
7205 loop:
7206 if (tst)
7207 tst->ss->free_super(tst);
7208 }
7209 if (*found != 0) {
7210 int err;
7211 if ((err = load_super_imsm_all(st, -1, &st->sb, NULL, devlist, 0)) == 0) {
7212 struct mdinfo *iter, *head = st->ss->container_content(st, NULL);
7213 for (iter = head; iter; iter = iter->next) {
7214 dprintf("content->text_version: %s vol\n",
7215 iter->text_version);
7216 if (iter->array.state & (1<<MD_SB_BLOCK_VOLUME)) {
7217 /* do not assemble arrays with unsupported
7218 configurations */
1ade5cc1 7219 dprintf("Cannot activate member %s.\n",
ca9de185
LM
7220 iter->text_version);
7221 } else
7222 count++;
7223 }
7224 sysfs_free(head);
7225
7226 } else {
1ade5cc1 7227 dprintf("No valid super block on device list: err: %d %p\n",
ca9de185
LM
7228 err, st->sb);
7229 }
7230 } else {
1ade5cc1 7231 dprintf("no more devices to examine\n");
ca9de185
LM
7232 }
7233
7234 for (tmpdev = devlist; tmpdev; tmpdev = tmpdev->next) {
089f9d79 7235 if (tmpdev->used == 1 && tmpdev->found) {
ca9de185
LM
7236 if (count) {
7237 if (count < tmpdev->found)
7238 count = 0;
7239 else
7240 count -= tmpdev->found;
7241 }
7242 }
7243 if (tmpdev->used == 1)
7244 tmpdev->used = 4;
7245 }
7246 err_1:
7247 if (st)
7248 st->ss->free_super(st);
7249 return count;
7250}
7251
d3c11416
AO
7252static int __count_volumes(char *hba_path, int dpa, int verbose,
7253 int cmp_hba_path)
ca9de185 7254{
72a45777 7255 struct sys_dev *idev, *intel_devices = find_intel_devices();
ca9de185 7256 int count = 0;
72a45777
PB
7257 const struct orom_entry *entry;
7258 struct devid_list *dv, *devid_list;
ca9de185 7259
d3c11416 7260 if (!hba_path)
ca9de185
LM
7261 return 0;
7262
72a45777 7263 for (idev = intel_devices; idev; idev = idev->next) {
d3c11416
AO
7264 if (strstr(idev->path, hba_path))
7265 break;
72a45777
PB
7266 }
7267
7268 if (!idev || !idev->dev_id)
ca9de185 7269 return 0;
72a45777
PB
7270
7271 entry = get_orom_entry_by_device_id(idev->dev_id);
7272
7273 if (!entry || !entry->devid_list)
7274 return 0;
7275
7276 devid_list = entry->devid_list;
7277 for (dv = devid_list; dv; dv = dv->next) {
594dc1b8 7278 struct md_list *devlist;
d3c11416
AO
7279 struct sys_dev *device = NULL;
7280 char *hpath;
72a45777
PB
7281 int found = 0;
7282
d3c11416
AO
7283 if (cmp_hba_path)
7284 device = device_by_id_and_path(dv->devid, hba_path);
7285 else
7286 device = device_by_id(dv->devid);
7287
72a45777 7288 if (device)
d3c11416 7289 hpath = device->path;
72a45777
PB
7290 else
7291 return 0;
7292
d3c11416 7293 devlist = get_devices(hpath);
72a45777
PB
7294 /* if no intel devices return zero volumes */
7295 if (devlist == NULL)
7296 return 0;
7297
d3c11416
AO
7298 count += active_arrays_by_format("imsm", hpath, &devlist, dpa,
7299 verbose);
7300 dprintf("path: %s active arrays: %d\n", hpath, count);
72a45777
PB
7301 if (devlist == NULL)
7302 return 0;
7303 do {
7304 found = 0;
7305 count += count_volumes_list(devlist,
7306 NULL,
7307 verbose,
7308 &found);
7309 dprintf("found %d count: %d\n", found, count);
7310 } while (found);
7311
d3c11416 7312 dprintf("path: %s total number of volumes: %d\n", hpath, count);
72a45777
PB
7313
7314 while (devlist) {
7315 struct md_list *dv = devlist;
7316 devlist = devlist->next;
7317 free(dv->devname);
7318 free(dv);
7319 }
ca9de185
LM
7320 }
7321 return count;
7322}
7323
d3c11416
AO
7324static int count_volumes(struct intel_hba *hba, int dpa, int verbose)
7325{
7326 if (!hba)
7327 return 0;
7328 if (hba->type == SYS_DEV_VMD) {
7329 struct sys_dev *dev;
7330 int count = 0;
7331
7332 for (dev = find_intel_devices(); dev; dev = dev->next) {
7333 if (dev->type == SYS_DEV_VMD)
7334 count += __count_volumes(dev->path, dpa,
7335 verbose, 1);
7336 }
7337 return count;
7338 }
7339 return __count_volumes(hba->path, dpa, verbose, 0);
7340}
7341
cd9d1ac7
DW
7342static int imsm_default_chunk(const struct imsm_orom *orom)
7343{
7344 /* up to 512 if the plaform supports it, otherwise the platform max.
7345 * 128 if no platform detected
7346 */
7347 int fs = max(7, orom ? fls(orom->sss) : 0);
7348
7349 return min(512, (1 << fs));
7350}
73408129 7351
6592ce37
DW
7352static int
7353validate_geometry_imsm_orom(struct intel_super *super, int level, int layout,
2cc699af 7354 int raiddisks, int *chunk, unsigned long long size, int verbose)
6592ce37 7355{
660260d0
DW
7356 /* check/set platform and metadata limits/defaults */
7357 if (super->orom && raiddisks > super->orom->dpa) {
676e87a8 7358 pr_vrb("platform supports a maximum of %d disks per array\n",
660260d0 7359 super->orom->dpa);
73408129
LM
7360 return 0;
7361 }
7362
5d500228 7363 /* capabilities of OROM tested - copied from validate_geometry_imsm_volume */
660260d0 7364 if (!is_raid_level_supported(super->orom, level, raiddisks)) {
676e87a8 7365 pr_vrb("platform does not support raid%d with %d disk%s\n",
6592ce37
DW
7366 level, raiddisks, raiddisks > 1 ? "s" : "");
7367 return 0;
7368 }
cd9d1ac7 7369
7ccc4cc4 7370 if (*chunk == 0 || *chunk == UnSet)
cd9d1ac7
DW
7371 *chunk = imsm_default_chunk(super->orom);
7372
7ccc4cc4 7373 if (super->orom && !imsm_orom_has_chunk(super->orom, *chunk)) {
676e87a8 7374 pr_vrb("platform does not support a chunk size of: %d\n", *chunk);
cd9d1ac7 7375 return 0;
6592ce37 7376 }
cd9d1ac7 7377
6592ce37
DW
7378 if (layout != imsm_level_to_layout(level)) {
7379 if (level == 5)
676e87a8 7380 pr_vrb("imsm raid 5 only supports the left-asymmetric layout\n");
6592ce37 7381 else if (level == 10)
676e87a8 7382 pr_vrb("imsm raid 10 only supports the n2 layout\n");
6592ce37 7383 else
676e87a8 7384 pr_vrb("imsm unknown layout %#x for this raid level %d\n",
6592ce37
DW
7385 layout, level);
7386 return 0;
7387 }
2cc699af 7388
7ccc4cc4 7389 if (super->orom && (super->orom->attr & IMSM_OROM_ATTR_2TB) == 0 &&
2cc699af 7390 (calc_array_size(level, raiddisks, layout, *chunk, size) >> 32) > 0) {
676e87a8 7391 pr_vrb("platform does not support a volume size over 2TB\n");
2cc699af
CA
7392 return 0;
7393 }
614902f6 7394
6592ce37
DW
7395 return 1;
7396}
7397
1011e834 7398/* validate_geometry_imsm_volume - lifted from validate_geometry_ddf_bvd
c2c087e6
DW
7399 * FIX ME add ahci details
7400 */
8b353278 7401static int validate_geometry_imsm_volume(struct supertype *st, int level,
c21e737b 7402 int layout, int raiddisks, int *chunk,
af4348dd
N
7403 unsigned long long size,
7404 unsigned long long data_offset,
7405 char *dev,
2c514b71
NB
7406 unsigned long long *freesize,
7407 int verbose)
cdddbdbc 7408{
9e04ac1c 7409 dev_t rdev;
c2c087e6 7410 struct intel_super *super = st->sb;
b2916f25 7411 struct imsm_super *mpb;
c2c087e6
DW
7412 struct dl *dl;
7413 unsigned long long pos = 0;
7414 unsigned long long maxsize;
7415 struct extent *e;
7416 int i;
cdddbdbc 7417
88c32bb1
DW
7418 /* We must have the container info already read in. */
7419 if (!super)
c2c087e6
DW
7420 return 0;
7421
b2916f25
JS
7422 mpb = super->anchor;
7423
2cc699af 7424 if (!validate_geometry_imsm_orom(super, level, layout, raiddisks, chunk, size, verbose)) {
3e684231 7425 pr_err("RAID geometry validation failed. Cannot proceed with the action(s).\n");
c2c087e6 7426 return 0;
d54559f0 7427 }
c2c087e6
DW
7428 if (!dev) {
7429 /* General test: make sure there is space for
2da8544a
DW
7430 * 'raiddisks' device extents of size 'size' at a given
7431 * offset
c2c087e6 7432 */
e46273eb 7433 unsigned long long minsize = size;
b7528a20 7434 unsigned long long start_offset = MaxSector;
c2c087e6
DW
7435 int dcnt = 0;
7436 if (minsize == 0)
7437 minsize = MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS;
7438 for (dl = super->disks; dl ; dl = dl->next) {
7439 int found = 0;
7440
bf5a934a 7441 pos = 0;
c2c087e6 7442 i = 0;
05501181 7443 e = get_extents(super, dl, 0);
c2c087e6
DW
7444 if (!e) continue;
7445 do {
7446 unsigned long long esize;
7447 esize = e[i].start - pos;
7448 if (esize >= minsize)
7449 found = 1;
b7528a20 7450 if (found && start_offset == MaxSector) {
2da8544a
DW
7451 start_offset = pos;
7452 break;
7453 } else if (found && pos != start_offset) {
7454 found = 0;
7455 break;
7456 }
c2c087e6
DW
7457 pos = e[i].start + e[i].size;
7458 i++;
7459 } while (e[i-1].size);
7460 if (found)
7461 dcnt++;
7462 free(e);
7463 }
7464 if (dcnt < raiddisks) {
2c514b71 7465 if (verbose)
7a862a02 7466 pr_err("imsm: Not enough devices with space for this array (%d < %d)\n",
2c514b71 7467 dcnt, raiddisks);
c2c087e6
DW
7468 return 0;
7469 }
7470 return 1;
7471 }
0dcecb2e 7472
c2c087e6 7473 /* This device must be a member of the set */
9e04ac1c 7474 if (!stat_is_blkdev(dev, &rdev))
c2c087e6
DW
7475 return 0;
7476 for (dl = super->disks ; dl ; dl = dl->next) {
9e04ac1c
ZL
7477 if (dl->major == (int)major(rdev) &&
7478 dl->minor == (int)minor(rdev))
c2c087e6
DW
7479 break;
7480 }
7481 if (!dl) {
2c514b71 7482 if (verbose)
7a862a02 7483 pr_err("%s is not in the same imsm set\n", dev);
c2c087e6 7484 return 0;
a20d2ba5
DW
7485 } else if (super->orom && dl->index < 0 && mpb->num_raid_devs) {
7486 /* If a volume is present then the current creation attempt
7487 * cannot incorporate new spares because the orom may not
7488 * understand this configuration (all member disks must be
7489 * members of each array in the container).
7490 */
7a862a02
N
7491 pr_err("%s is a spare and a volume is already defined for this container\n", dev);
7492 pr_err("The option-rom requires all member disks to be a member of all volumes\n");
a20d2ba5 7493 return 0;
5fe62b94
WD
7494 } else if (super->orom && mpb->num_raid_devs > 0 &&
7495 mpb->num_disks != raiddisks) {
7a862a02 7496 pr_err("The option-rom requires all member disks to be a member of all volumes\n");
5fe62b94 7497 return 0;
c2c087e6 7498 }
0dcecb2e
DW
7499
7500 /* retrieve the largest free space block */
05501181 7501 e = get_extents(super, dl, 0);
c2c087e6
DW
7502 maxsize = 0;
7503 i = 0;
0dcecb2e
DW
7504 if (e) {
7505 do {
7506 unsigned long long esize;
7507
7508 esize = e[i].start - pos;
7509 if (esize >= maxsize)
7510 maxsize = esize;
7511 pos = e[i].start + e[i].size;
7512 i++;
7513 } while (e[i-1].size);
7514 dl->e = e;
7515 dl->extent_cnt = i;
7516 } else {
7517 if (verbose)
e7b84f9d 7518 pr_err("unable to determine free space for: %s\n",
0dcecb2e
DW
7519 dev);
7520 return 0;
7521 }
7522 if (maxsize < size) {
7523 if (verbose)
e7b84f9d 7524 pr_err("%s not enough space (%llu < %llu)\n",
0dcecb2e
DW
7525 dev, maxsize, size);
7526 return 0;
7527 }
7528
aa19fdd4 7529 maxsize = merge_extents(super, false);
3baa56ab 7530
1a1ced1e
KS
7531 if (mpb->num_raid_devs > 0 && size && size != maxsize)
7532 pr_err("attempting to create a second volume with size less then remaining space.\n");
3baa56ab 7533
a7dd165b 7534 if (maxsize < size || maxsize == 0) {
b3071342
LD
7535 if (verbose) {
7536 if (maxsize == 0)
7a862a02 7537 pr_err("no free space left on device. Aborting...\n");
b3071342 7538 else
7a862a02 7539 pr_err("not enough space to create volume of given size (%llu < %llu). Aborting...\n",
b3071342
LD
7540 maxsize, size);
7541 }
0dcecb2e 7542 return 0;
0dcecb2e
DW
7543 }
7544
c2c087e6
DW
7545 *freesize = maxsize;
7546
ca9de185 7547 if (super->orom) {
72a45777 7548 int count = count_volumes(super->hba,
ca9de185
LM
7549 super->orom->dpa, verbose);
7550 if (super->orom->vphba <= count) {
676e87a8 7551 pr_vrb("platform does not support more than %d raid volumes.\n",
ca9de185
LM
7552 super->orom->vphba);
7553 return 0;
7554 }
7555 }
c2c087e6 7556 return 1;
cdddbdbc
DW
7557}
7558
6d4d9ab2
MT
7559/**
7560 * imsm_get_free_size() - get the biggest, common free space from members.
7561 * @super: &intel_super pointer, not NULL.
7562 * @raiddisks: number of raid disks.
7563 * @size: requested size, could be 0 (means max size).
5f027b93 7564 * @chunk: requested chunk size in KiB.
6d4d9ab2
MT
7565 * @freesize: pointer for returned size value.
7566 *
7567 * Return: &IMSM_STATUS_OK or &IMSM_STATUS_ERROR.
7568 *
7569 * @freesize is set to meaningful value, this can be @size, or calculated
7570 * max free size.
7571 * super->create_offset value is modified and set appropriately in
7572 * merge_extends() for further creation.
7573 */
7574static imsm_status_t imsm_get_free_size(struct intel_super *super,
7575 const int raiddisks,
7576 unsigned long long size,
7577 const int chunk,
aa19fdd4
MT
7578 unsigned long long *freesize,
7579 bool expanding)
efb30e7f 7580{
efb30e7f
DW
7581 struct imsm_super *mpb = super->anchor;
7582 struct dl *dl;
7583 int i;
efb30e7f 7584 struct extent *e;
5f027b93
MT
7585 int cnt = 0;
7586 int used = 0;
efb30e7f 7587 unsigned long long maxsize;
5f027b93
MT
7588 unsigned long long minsize = size;
7589
7590 if (minsize == 0)
7591 minsize = chunk * 2;
efb30e7f
DW
7592
7593 /* find the largest common start free region of the possible disks */
efb30e7f
DW
7594 for (dl = super->disks; dl; dl = dl->next) {
7595 dl->raiddisk = -1;
7596
7597 if (dl->index >= 0)
7598 used++;
7599
7600 /* don't activate new spares if we are orom constrained
7601 * and there is already a volume active in the container
7602 */
7603 if (super->orom && dl->index < 0 && mpb->num_raid_devs)
7604 continue;
7605
05501181 7606 e = get_extents(super, dl, 0);
efb30e7f
DW
7607 if (!e)
7608 continue;
7609 for (i = 1; e[i-1].size; i++)
7610 ;
7611 dl->e = e;
7612 dl->extent_cnt = i;
efb30e7f
DW
7613 cnt++;
7614 }
7615
aa19fdd4 7616 maxsize = merge_extents(super, expanding);
5f027b93
MT
7617 if (maxsize < minsize) {
7618 pr_err("imsm: Free space is %llu but must be equal or larger than %llu.\n",
7619 maxsize, minsize);
7620 return IMSM_STATUS_ERROR;
7621 }
efb30e7f 7622
5f027b93
MT
7623 if (cnt < raiddisks || (super->orom && used && used != raiddisks)) {
7624 pr_err("imsm: Not enough devices with space to create array.\n");
6d4d9ab2 7625 return IMSM_STATUS_ERROR;
efb30e7f
DW
7626 }
7627
7628 if (size == 0) {
7629 size = maxsize;
7630 if (chunk) {
612e59d8
CA
7631 size /= 2 * chunk;
7632 size *= 2 * chunk;
efb30e7f 7633 }
f878b242
LM
7634 maxsize = size;
7635 }
1a1ced1e
KS
7636 if (mpb->num_raid_devs > 0 && size && size != maxsize)
7637 pr_err("attempting to create a second volume with size less then remaining space.\n");
efb30e7f
DW
7638 *freesize = size;
7639
13bcac90
AK
7640 dprintf("imsm: imsm_get_free_size() returns : %llu\n", size);
7641
6d4d9ab2 7642 return IMSM_STATUS_OK;
efb30e7f
DW
7643}
7644
6d4d9ab2
MT
7645/**
7646 * autolayout_imsm() - automatically layout a new volume.
7647 * @super: &intel_super pointer, not NULL.
7648 * @raiddisks: number of raid disks.
7649 * @size: requested size, could be 0 (means max size).
7650 * @chunk: requested chunk.
7651 * @freesize: pointer for returned size value.
7652 *
7653 * We are being asked to automatically layout a new volume based on the current
7654 * contents of the container. If the parameters can be satisfied autolayout_imsm
7655 * will record the disks, start offset, and will return size of the volume to
7656 * be created. See imsm_get_free_size() for details.
7657 * add_to_super() and getinfo_super() detect when autolayout is in progress.
7658 * If first volume exists, slots are set consistently to it.
7659 *
7660 * Return: &IMSM_STATUS_OK on success, &IMSM_STATUS_ERROR otherwise.
7661 *
7662 * Disks are marked for creation via dl->raiddisk.
7663 */
7664static imsm_status_t autolayout_imsm(struct intel_super *super,
7665 const int raiddisks,
7666 unsigned long long size, const int chunk,
7667 unsigned long long *freesize)
13bcac90 7668{
6d4d9ab2
MT
7669 int curr_slot = 0;
7670 struct dl *disk;
7671 int vol_cnt = super->anchor->num_raid_devs;
7672 imsm_status_t rv;
13bcac90 7673
aa19fdd4 7674 rv = imsm_get_free_size(super, raiddisks, size, chunk, freesize, false);
6d4d9ab2
MT
7675 if (rv != IMSM_STATUS_OK)
7676 return IMSM_STATUS_ERROR;
7677
7678 for (disk = super->disks; disk; disk = disk->next) {
7679 if (!disk->e)
7680 continue;
7681
7682 if (curr_slot == raiddisks)
7683 break;
7684
7685 if (vol_cnt == 0) {
7686 disk->raiddisk = curr_slot;
7687 } else {
7688 int _slot = get_disk_slot_in_dev(super, 0, disk->index);
7689
7690 if (_slot == -1) {
7691 pr_err("Disk %s is not used in first volume, aborting\n",
7692 disk->devname);
7693 return IMSM_STATUS_ERROR;
7694 }
7695 disk->raiddisk = _slot;
7696 }
7697 curr_slot++;
13bcac90
AK
7698 }
7699
6d4d9ab2 7700 return IMSM_STATUS_OK;
13bcac90
AK
7701}
7702
bf5a934a 7703static int validate_geometry_imsm(struct supertype *st, int level, int layout,
c21e737b 7704 int raiddisks, int *chunk, unsigned long long size,
af4348dd 7705 unsigned long long data_offset,
bf5a934a 7706 char *dev, unsigned long long *freesize,
5308f117 7707 int consistency_policy, int verbose)
bf5a934a
DW
7708{
7709 int fd, cfd;
7710 struct mdinfo *sra;
20cbe8d2 7711 int is_member = 0;
bf5a934a 7712
d54559f0
LM
7713 /* load capability
7714 * if given unused devices create a container
bf5a934a
DW
7715 * if given given devices in a container create a member volume
7716 */
6f2af6a4 7717 if (is_container(level))
bf5a934a 7718 /* Must be a fresh device to add to a container */
1f5d54a0
MT
7719 return validate_geometry_imsm_container(st, level, raiddisks,
7720 data_offset, dev,
7721 freesize, verbose);
9587c373 7722
06a6101c
BK
7723 /*
7724 * Size is given in sectors.
7725 */
7726 if (size && (size < 2048)) {
22dc741f 7727 pr_err("Given size must be greater than 1M.\n");
54865c30
RS
7728 /* Depends on algorithm in Create.c :
7729 * if container was given (dev == NULL) return -1,
7730 * if block device was given ( dev != NULL) return 0.
7731 */
7732 return dev ? -1 : 0;
7733 }
7734
8592f29d 7735 if (!dev) {
6d4d9ab2
MT
7736 struct intel_super *super = st->sb;
7737
7738 /*
071f839e 7739 * Autolayout mode, st->sb must be set.
6d4d9ab2 7740 */
071f839e
KT
7741 if (!super) {
7742 pr_vrb("superblock must be set for autolayout, aborting\n");
7743 return 0;
6d4d9ab2
MT
7744 }
7745
7746 if (!validate_geometry_imsm_orom(st->sb, level, layout,
7747 raiddisks, chunk, size,
7748 verbose))
7749 return 0;
7750
071f839e 7751 if (super->orom && freesize) {
6d4d9ab2
MT
7752 imsm_status_t rv;
7753 int count = count_volumes(super->hba, super->orom->dpa,
7754 verbose);
7755 if (super->orom->vphba <= count) {
7756 pr_vrb("platform does not support more than %d raid volumes.\n",
7757 super->orom->vphba);
e91a3bad 7758 return 0;
ca9de185 7759 }
6d4d9ab2
MT
7760
7761 rv = autolayout_imsm(super, raiddisks, size, *chunk,
7762 freesize);
7763 if (rv != IMSM_STATUS_OK)
7764 return 0;
8592f29d
N
7765 }
7766 return 1;
7767 }
bf5a934a
DW
7768 if (st->sb) {
7769 /* creating in a given container */
7770 return validate_geometry_imsm_volume(st, level, layout,
7771 raiddisks, chunk, size,
af4348dd 7772 data_offset,
bf5a934a
DW
7773 dev, freesize, verbose);
7774 }
7775
bf5a934a
DW
7776 /* This device needs to be a device in an 'imsm' container */
7777 fd = open(dev, O_RDONLY|O_EXCL, 0);
4389ce73
MT
7778
7779 if (is_fd_valid(fd)) {
7780 pr_vrb("Cannot create this array on device %s\n", dev);
bf5a934a
DW
7781 close(fd);
7782 return 0;
7783 }
4389ce73
MT
7784 if (errno == EBUSY)
7785 fd = open(dev, O_RDONLY, 0);
7786
7787 if (!is_fd_valid(fd)) {
7788 pr_vrb("Cannot open %s: %s\n", dev, strerror(errno));
bf5a934a
DW
7789 return 0;
7790 }
4389ce73 7791
bf5a934a
DW
7792 /* Well, it is in use by someone, maybe an 'imsm' container. */
7793 cfd = open_container(fd);
4389ce73
MT
7794 close_fd(&fd);
7795
7796 if (!is_fd_valid(cfd)) {
7797 pr_vrb("Cannot use %s: It is busy\n", dev);
bf5a934a
DW
7798 return 0;
7799 }
4dd2df09 7800 sra = sysfs_read(cfd, NULL, GET_VERSION);
bf5a934a 7801 if (sra && sra->array.major_version == -1 &&
20cbe8d2
AW
7802 strcmp(sra->text_version, "imsm") == 0)
7803 is_member = 1;
7804 sysfs_free(sra);
7805 if (is_member) {
bf5a934a
DW
7806 /* This is a member of a imsm container. Load the container
7807 * and try to create a volume
7808 */
7809 struct intel_super *super;
7810
ec50f7b6 7811 if (load_super_imsm_all(st, cfd, (void **) &super, NULL, NULL, 1) == 0) {
bf5a934a 7812 st->sb = super;
4dd2df09 7813 strcpy(st->container_devnm, fd2devnm(cfd));
bf5a934a
DW
7814 close(cfd);
7815 return validate_geometry_imsm_volume(st, level, layout,
7816 raiddisks, chunk,
af4348dd 7817 size, data_offset, dev,
ecbd9e81
N
7818 freesize, 1)
7819 ? 1 : -1;
bf5a934a 7820 }
20cbe8d2 7821 }
bf5a934a 7822
20cbe8d2 7823 if (verbose)
e7b84f9d 7824 pr_err("failed container membership check\n");
20cbe8d2
AW
7825
7826 close(cfd);
7827 return 0;
bf5a934a 7828}
0bd16cf2 7829
30f58b22 7830static void default_geometry_imsm(struct supertype *st, int *level, int *layout, int *chunk)
0bd16cf2
DJ
7831{
7832 struct intel_super *super = st->sb;
7833
30f58b22
DW
7834 if (level && *level == UnSet)
7835 *level = LEVEL_CONTAINER;
7836
7837 if (level && layout && *layout == UnSet)
7838 *layout = imsm_level_to_layout(*level);
0bd16cf2 7839
cd9d1ac7
DW
7840 if (chunk && (*chunk == UnSet || *chunk == 0))
7841 *chunk = imsm_default_chunk(super->orom);
0bd16cf2
DJ
7842}
7843
33414a01
DW
7844static void handle_missing(struct intel_super *super, struct imsm_dev *dev);
7845
3364781b 7846static int kill_subarray_imsm(struct supertype *st, char *subarray_id)
33414a01 7847{
3364781b 7848 /* remove the subarray currently referenced by subarray_id */
33414a01
DW
7849 __u8 i;
7850 struct intel_dev **dp;
7851 struct intel_super *super = st->sb;
3364781b 7852 __u8 current_vol = strtoul(subarray_id, NULL, 10);
33414a01
DW
7853 struct imsm_super *mpb = super->anchor;
7854
3364781b 7855 if (mpb->num_raid_devs == 0)
33414a01 7856 return 2;
33414a01
DW
7857
7858 /* block deletions that would change the uuid of active subarrays
7859 *
7860 * FIXME when immutable ids are available, but note that we'll
7861 * also need to fixup the invalidated/active subarray indexes in
7862 * mdstat
7863 */
7864 for (i = 0; i < mpb->num_raid_devs; i++) {
7865 char subarray[4];
7866
7867 if (i < current_vol)
7868 continue;
dd5ab402 7869 snprintf(subarray, sizeof(subarray), "%u", i);
4dd2df09 7870 if (is_subarray_active(subarray, st->devnm)) {
e7b84f9d
N
7871 pr_err("deleting subarray-%d would change the UUID of active subarray-%d, aborting\n",
7872 current_vol, i);
33414a01
DW
7873
7874 return 2;
7875 }
7876 }
7877
7878 if (st->update_tail) {
503975b9 7879 struct imsm_update_kill_array *u = xmalloc(sizeof(*u));
33414a01 7880
33414a01
DW
7881 u->type = update_kill_array;
7882 u->dev_idx = current_vol;
7883 append_metadata_update(st, u, sizeof(*u));
7884
7885 return 0;
7886 }
7887
7888 for (dp = &super->devlist; *dp;)
7889 if ((*dp)->index == current_vol) {
7890 *dp = (*dp)->next;
7891 } else {
7892 handle_missing(super, (*dp)->dev);
7893 if ((*dp)->index > current_vol)
7894 (*dp)->index--;
7895 dp = &(*dp)->next;
7896 }
7897
7898 /* no more raid devices, all active components are now spares,
7899 * but of course failed are still failed
7900 */
7901 if (--mpb->num_raid_devs == 0) {
7902 struct dl *d;
7903
7904 for (d = super->disks; d; d = d->next)
a8619d23
AK
7905 if (d->index > -2)
7906 mark_spare(d);
33414a01
DW
7907 }
7908
7909 super->updates_pending++;
7910
7911 return 0;
7912}
aa534678 7913
4345e135
MK
7914/**
7915 * get_rwh_policy_from_update() - Get the rwh policy for update option.
7916 * @update: Update option.
7917 */
7918static int get_rwh_policy_from_update(enum update_opt update)
19ad203e 7919{
4345e135
MK
7920 switch (update) {
7921 case UOPT_PPL:
19ad203e 7922 return RWH_MULTIPLE_DISTRIBUTED;
4345e135 7923 case UOPT_NO_PPL:
19ad203e 7924 return RWH_MULTIPLE_OFF;
4345e135 7925 case UOPT_BITMAP:
19ad203e 7926 return RWH_BITMAP;
4345e135 7927 case UOPT_NO_BITMAP:
19ad203e 7928 return RWH_OFF;
4345e135
MK
7929 default:
7930 break;
7931 }
7932 return UOPT_UNDEFINED;
19ad203e
JR
7933}
7934
a951a4f7 7935static int update_subarray_imsm(struct supertype *st, char *subarray,
03312b52 7936 enum update_opt update, struct mddev_ident *ident)
aa534678
DW
7937{
7938 /* update the subarray currently referenced by ->current_vol */
7939 struct intel_super *super = st->sb;
7940 struct imsm_super *mpb = super->anchor;
7941
03312b52 7942 if (update == UOPT_NAME) {
aa534678 7943 char *name = ident->name;
a951a4f7
N
7944 char *ep;
7945 int vol;
aa534678 7946
e2eb503b 7947 if (imsm_is_name_allowed(super, name, 1) == false)
aa534678
DW
7948 return 2;
7949
a951a4f7
N
7950 vol = strtoul(subarray, &ep, 10);
7951 if (*ep != '\0' || vol >= super->anchor->num_raid_devs)
7952 return 2;
7953
aa534678 7954 if (st->update_tail) {
503975b9 7955 struct imsm_update_rename_array *u = xmalloc(sizeof(*u));
aa534678 7956
aa534678 7957 u->type = update_rename_array;
a951a4f7 7958 u->dev_idx = vol;
618f4e6d
XN
7959 strncpy((char *) u->name, name, MAX_RAID_SERIAL_LEN);
7960 u->name[MAX_RAID_SERIAL_LEN-1] = '\0';
aa534678
DW
7961 append_metadata_update(st, u, sizeof(*u));
7962 } else {
7963 struct imsm_dev *dev;
ebad3af2 7964 int i, namelen;
aa534678 7965
a951a4f7 7966 dev = get_imsm_dev(super, vol);
ebad3af2
JS
7967 memset(dev->volume, '\0', MAX_RAID_SERIAL_LEN);
7968 namelen = min((int)strlen(name), MAX_RAID_SERIAL_LEN);
7969 memcpy(dev->volume, name, namelen);
aa534678
DW
7970 for (i = 0; i < mpb->num_raid_devs; i++) {
7971 dev = get_imsm_dev(super, i);
7972 handle_missing(super, dev);
7973 }
7974 super->updates_pending++;
7975 }
03312b52 7976 } else if (get_rwh_policy_from_update(update) != UOPT_UNDEFINED) {
e6e9dd3f
AP
7977 int new_policy;
7978 char *ep;
7979 int vol = strtoul(subarray, &ep, 10);
7980
7981 if (*ep != '\0' || vol >= super->anchor->num_raid_devs)
7982 return 2;
7983
03312b52 7984 new_policy = get_rwh_policy_from_update(update);
e6e9dd3f
AP
7985
7986 if (st->update_tail) {
7987 struct imsm_update_rwh_policy *u = xmalloc(sizeof(*u));
7988
7989 u->type = update_rwh_policy;
7990 u->dev_idx = vol;
7991 u->new_policy = new_policy;
7992 append_metadata_update(st, u, sizeof(*u));
7993 } else {
7994 struct imsm_dev *dev;
7995
7996 dev = get_imsm_dev(super, vol);
7997 dev->rwh_policy = new_policy;
7998 super->updates_pending++;
7999 }
19ad203e
JR
8000 if (new_policy == RWH_BITMAP)
8001 return write_init_bitmap_imsm_vol(st, vol);
aa534678
DW
8002 } else
8003 return 2;
8004
8005 return 0;
8006}
bf5a934a 8007
195d1d76 8008static bool is_gen_migration(struct imsm_dev *dev)
28bce06f 8009{
195d1d76
PP
8010 if (dev && dev->vol.migr_state &&
8011 migr_type(dev) == MIGR_GEN_MIGR)
8012 return true;
28bce06f 8013
195d1d76 8014 return false;
28bce06f
AK
8015}
8016
1e5c6983
DW
8017static int is_rebuilding(struct imsm_dev *dev)
8018{
8019 struct imsm_map *migr_map;
8020
8021 if (!dev->vol.migr_state)
8022 return 0;
8023
8024 if (migr_type(dev) != MIGR_REBUILD)
8025 return 0;
8026
238c0a71 8027 migr_map = get_imsm_map(dev, MAP_1);
1e5c6983
DW
8028
8029 if (migr_map->map_state == IMSM_T_STATE_DEGRADED)
8030 return 1;
8031 else
8032 return 0;
8033}
8034
6ce1fbf1
AK
8035static int is_initializing(struct imsm_dev *dev)
8036{
8037 struct imsm_map *migr_map;
8038
8039 if (!dev->vol.migr_state)
8040 return 0;
8041
8042 if (migr_type(dev) != MIGR_INIT)
8043 return 0;
8044
238c0a71 8045 migr_map = get_imsm_map(dev, MAP_1);
6ce1fbf1
AK
8046
8047 if (migr_map->map_state == IMSM_T_STATE_UNINITIALIZED)
8048 return 1;
8049
8050 return 0;
6ce1fbf1
AK
8051}
8052
c47b0ff6
AK
8053static void update_recovery_start(struct intel_super *super,
8054 struct imsm_dev *dev,
8055 struct mdinfo *array)
1e5c6983
DW
8056{
8057 struct mdinfo *rebuild = NULL;
8058 struct mdinfo *d;
8059 __u32 units;
8060
8061 if (!is_rebuilding(dev))
8062 return;
8063
8064 /* Find the rebuild target, but punt on the dual rebuild case */
8065 for (d = array->devs; d; d = d->next)
8066 if (d->recovery_start == 0) {
8067 if (rebuild)
8068 return;
8069 rebuild = d;
8070 }
8071
4363fd80
DW
8072 if (!rebuild) {
8073 /* (?) none of the disks are marked with
8074 * IMSM_ORD_REBUILD, so assume they are missing and the
8075 * disk_ord_tbl was not correctly updated
8076 */
1ade5cc1 8077 dprintf("failed to locate out-of-sync disk\n");
4363fd80
DW
8078 return;
8079 }
8080
4036e7ee 8081 units = vol_curr_migr_unit(dev);
c47b0ff6 8082 rebuild->recovery_start = units * blocks_per_migr_unit(super, dev);
1e5c6983
DW
8083}
8084
276d77db 8085static int recover_backup_imsm(struct supertype *st, struct mdinfo *info);
1e5c6983 8086
00bbdbda 8087static struct mdinfo *container_content_imsm(struct supertype *st, char *subarray)
cdddbdbc 8088{
4f5bc454
DW
8089 /* Given a container loaded by load_super_imsm_all,
8090 * extract information about all the arrays into
8091 * an mdinfo tree.
00bbdbda 8092 * If 'subarray' is given, just extract info about that array.
4f5bc454
DW
8093 *
8094 * For each imsm_dev create an mdinfo, fill it in,
8095 * then look for matching devices in super->disks
8096 * and create appropriate device mdinfo.
8097 */
8098 struct intel_super *super = st->sb;
949c47a0 8099 struct imsm_super *mpb = super->anchor;
4f5bc454 8100 struct mdinfo *rest = NULL;
00bbdbda 8101 unsigned int i;
81219e70 8102 int sb_errors = 0;
abef11a3
AK
8103 struct dl *d;
8104 int spare_disks = 0;
b6180160 8105 int current_vol = super->current_vol;
cdddbdbc 8106
19482bcc
AK
8107 /* do not assemble arrays when not all attributes are supported */
8108 if (imsm_check_attributes(mpb->attributes) == 0) {
81219e70 8109 sb_errors = 1;
7a862a02 8110 pr_err("Unsupported attributes in IMSM metadata.Arrays activation is blocked.\n");
19482bcc
AK
8111 }
8112
abef11a3
AK
8113 /* count spare devices, not used in maps
8114 */
8115 for (d = super->disks; d; d = d->next)
8116 if (d->index == -1)
8117 spare_disks++;
8118
4f5bc454 8119 for (i = 0; i < mpb->num_raid_devs; i++) {
00bbdbda
N
8120 struct imsm_dev *dev;
8121 struct imsm_map *map;
86e3692b 8122 struct imsm_map *map2;
4f5bc454 8123 struct mdinfo *this;
a6482415 8124 int slot;
a6482415 8125 int chunk;
00bbdbda 8126 char *ep;
8b9cd157 8127 int level;
00bbdbda
N
8128
8129 if (subarray &&
8130 (i != strtoul(subarray, &ep, 10) || *ep != '\0'))
8131 continue;
8132
8133 dev = get_imsm_dev(super, i);
238c0a71
AK
8134 map = get_imsm_map(dev, MAP_0);
8135 map2 = get_imsm_map(dev, MAP_1);
8b9cd157 8136 level = get_imsm_raid_level(map);
4f5bc454 8137
1ce0101c
DW
8138 /* do not publish arrays that are in the middle of an
8139 * unsupported migration
8140 */
8141 if (dev->vol.migr_state &&
28bce06f 8142 (migr_type(dev) == MIGR_STATE_CHANGE)) {
7a862a02 8143 pr_err("cannot assemble volume '%.16s': unsupported migration in progress\n",
1ce0101c
DW
8144 dev->volume);
8145 continue;
8146 }
2db86302
LM
8147 /* do not publish arrays that are not support by controller's
8148 * OROM/EFI
8149 */
1ce0101c 8150
503975b9 8151 this = xmalloc(sizeof(*this));
4f5bc454 8152
301406c9 8153 super->current_vol = i;
a5d85af7 8154 getinfo_super_imsm_volume(st, this, NULL);
9894ec0d 8155 this->next = rest;
a6482415 8156 chunk = __le16_to_cpu(map->blocks_per_strip) >> 1;
81219e70
LM
8157 /* mdadm does not support all metadata features- set the bit in all arrays state */
8158 if (!validate_geometry_imsm_orom(super,
8b9cd157
MK
8159 level, /* RAID level */
8160 imsm_level_to_layout(level),
81219e70 8161 map->num_members, /* raid disks */
fcc2c9da 8162 &chunk, imsm_dev_size(dev),
81219e70 8163 1 /* verbose */)) {
7a862a02 8164 pr_err("IMSM RAID geometry validation failed. Array %s activation is blocked.\n",
81219e70
LM
8165 dev->volume);
8166 this->array.state |=
8167 (1<<MD_SB_BLOCK_CONTAINER_RESHAPE) |
8168 (1<<MD_SB_BLOCK_VOLUME);
8169 }
81219e70
LM
8170
8171 /* if array has bad blocks, set suitable bit in all arrays state */
8172 if (sb_errors)
8173 this->array.state |=
8174 (1<<MD_SB_BLOCK_CONTAINER_RESHAPE) |
8175 (1<<MD_SB_BLOCK_VOLUME);
8176
4f5bc454 8177 for (slot = 0 ; slot < map->num_members; slot++) {
1e5c6983 8178 unsigned long long recovery_start;
4f5bc454
DW
8179 struct mdinfo *info_d;
8180 struct dl *d;
8181 int idx;
9a1608e5 8182 int skip;
7eef0453 8183 __u32 ord;
8b9cd157 8184 int missing = 0;
4f5bc454 8185
9a1608e5 8186 skip = 0;
238c0a71
AK
8187 idx = get_imsm_disk_idx(dev, slot, MAP_0);
8188 ord = get_imsm_ord_tbl_ent(dev, slot, MAP_X);
4f5bc454
DW
8189 for (d = super->disks; d ; d = d->next)
8190 if (d->index == idx)
0fbd635c 8191 break;
4f5bc454 8192
1e5c6983 8193 recovery_start = MaxSector;
4f5bc454 8194 if (d == NULL)
9a1608e5 8195 skip = 1;
25ed7e59 8196 if (d && is_failed(&d->disk))
9a1608e5 8197 skip = 1;
8b9cd157 8198 if (!skip && (ord & IMSM_ORD_REBUILD))
1e5c6983 8199 recovery_start = 0;
1e93d0d1
BK
8200 if (!(ord & IMSM_ORD_REBUILD))
8201 this->array.working_disks++;
1011e834 8202 /*
9a1608e5 8203 * if we skip some disks the array will be assmebled degraded;
1e5c6983
DW
8204 * reset resync start to avoid a dirty-degraded
8205 * situation when performing the intial sync
9a1608e5 8206 */
8b9cd157
MK
8207 if (skip)
8208 missing++;
8209
8210 if (!(dev->vol.dirty & RAIDVOL_DIRTY)) {
8211 if ((!able_to_resync(level, missing) ||
8212 recovery_start == 0))
8213 this->resync_start = MaxSector;
8b9cd157
MK
8214 }
8215
9a1608e5
DW
8216 if (skip)
8217 continue;
4f5bc454 8218
503975b9 8219 info_d = xcalloc(1, sizeof(*info_d));
4f5bc454
DW
8220 info_d->next = this->devs;
8221 this->devs = info_d;
8222
4f5bc454
DW
8223 info_d->disk.number = d->index;
8224 info_d->disk.major = d->major;
8225 info_d->disk.minor = d->minor;
8226 info_d->disk.raid_disk = slot;
1e5c6983 8227 info_d->recovery_start = recovery_start;
86e3692b
AK
8228 if (map2) {
8229 if (slot < map2->num_members)
8230 info_d->disk.state = (1 << MD_DISK_ACTIVE);
04c3c514
AK
8231 else
8232 this->array.spare_disks++;
86e3692b
AK
8233 } else {
8234 if (slot < map->num_members)
8235 info_d->disk.state = (1 << MD_DISK_ACTIVE);
04c3c514
AK
8236 else
8237 this->array.spare_disks++;
86e3692b 8238 }
4f5bc454
DW
8239
8240 info_d->events = __le32_to_cpu(mpb->generation_num);
5551b113 8241 info_d->data_offset = pba_of_lba0(map);
44490938 8242 info_d->component_size = calc_component_size(map, dev);
06fb291a
PB
8243
8244 if (map->raid_level == 5) {
2432ce9b
AP
8245 info_d->ppl_sector = this->ppl_sector;
8246 info_d->ppl_size = this->ppl_size;
98e96bdb
AP
8247 if (this->consistency_policy == CONSISTENCY_POLICY_PPL &&
8248 recovery_start == 0)
8249 this->resync_start = 0;
06fb291a 8250 }
b12796be 8251
5e46202e 8252 info_d->bb.supported = 1;
b12796be
TM
8253 get_volume_badblocks(super->bbm_log, ord_to_idx(ord),
8254 info_d->data_offset,
8255 info_d->component_size,
8256 &info_d->bb);
4f5bc454 8257 }
1e5c6983 8258 /* now that the disk list is up-to-date fixup recovery_start */
c47b0ff6 8259 update_recovery_start(super, dev, this);
abef11a3 8260 this->array.spare_disks += spare_disks;
276d77db
AK
8261
8262 /* check for reshape */
8263 if (this->reshape_active == 1)
8264 recover_backup_imsm(st, this);
9a1608e5 8265 rest = this;
4f5bc454
DW
8266 }
8267
b6180160 8268 super->current_vol = current_vol;
4f5bc454 8269 return rest;
cdddbdbc
DW
8270}
8271
3b451610
AK
8272static __u8 imsm_check_degraded(struct intel_super *super, struct imsm_dev *dev,
8273 int failed, int look_in_map)
c2a1e7da 8274{
3b451610
AK
8275 struct imsm_map *map;
8276
8277 map = get_imsm_map(dev, look_in_map);
c2a1e7da
DW
8278
8279 if (!failed)
1011e834 8280 return map->map_state == IMSM_T_STATE_UNINITIALIZED ?
3393c6af 8281 IMSM_T_STATE_UNINITIALIZED : IMSM_T_STATE_NORMAL;
c2a1e7da
DW
8282
8283 switch (get_imsm_raid_level(map)) {
8284 case 0:
8285 return IMSM_T_STATE_FAILED;
8286 break;
8287 case 1:
8288 if (failed < map->num_members)
8289 return IMSM_T_STATE_DEGRADED;
8290 else
8291 return IMSM_T_STATE_FAILED;
8292 break;
8293 case 10:
8294 {
8295 /**
c92a2527
DW
8296 * check to see if any mirrors have failed, otherwise we
8297 * are degraded. Even numbered slots are mirrored on
8298 * slot+1
c2a1e7da 8299 */
c2a1e7da 8300 int i;
d9b420a5
N
8301 /* gcc -Os complains that this is unused */
8302 int insync = insync;
c2a1e7da
DW
8303
8304 for (i = 0; i < map->num_members; i++) {
238c0a71 8305 __u32 ord = get_imsm_ord_tbl_ent(dev, i, MAP_X);
c92a2527
DW
8306 int idx = ord_to_idx(ord);
8307 struct imsm_disk *disk;
c2a1e7da 8308
c92a2527 8309 /* reset the potential in-sync count on even-numbered
1011e834 8310 * slots. num_copies is always 2 for imsm raid10
c92a2527
DW
8311 */
8312 if ((i & 1) == 0)
8313 insync = 2;
c2a1e7da 8314
c92a2527 8315 disk = get_imsm_disk(super, idx);
25ed7e59 8316 if (!disk || is_failed(disk) || ord & IMSM_ORD_REBUILD)
c92a2527 8317 insync--;
c2a1e7da 8318
c92a2527
DW
8319 /* no in-sync disks left in this mirror the
8320 * array has failed
8321 */
8322 if (insync == 0)
8323 return IMSM_T_STATE_FAILED;
c2a1e7da
DW
8324 }
8325
8326 return IMSM_T_STATE_DEGRADED;
8327 }
8328 case 5:
8329 if (failed < 2)
8330 return IMSM_T_STATE_DEGRADED;
8331 else
8332 return IMSM_T_STATE_FAILED;
8333 break;
8334 default:
8335 break;
8336 }
8337
8338 return map->map_state;
8339}
8340
3b451610
AK
8341static int imsm_count_failed(struct intel_super *super, struct imsm_dev *dev,
8342 int look_in_map)
c2a1e7da
DW
8343{
8344 int i;
8345 int failed = 0;
8346 struct imsm_disk *disk;
d5985138
AK
8347 struct imsm_map *map = get_imsm_map(dev, MAP_0);
8348 struct imsm_map *prev = get_imsm_map(dev, MAP_1);
68fe4598 8349 struct imsm_map *map_for_loop;
0556e1a2
DW
8350 __u32 ord;
8351 int idx;
d5985138 8352 int idx_1;
c2a1e7da 8353
0556e1a2
DW
8354 /* at the beginning of migration we set IMSM_ORD_REBUILD on
8355 * disks that are being rebuilt. New failures are recorded to
8356 * map[0]. So we look through all the disks we started with and
8357 * see if any failures are still present, or if any new ones
8358 * have arrived
0556e1a2 8359 */
d5985138
AK
8360 map_for_loop = map;
8361 if (prev && (map->num_members < prev->num_members))
8362 map_for_loop = prev;
68fe4598
LD
8363
8364 for (i = 0; i < map_for_loop->num_members; i++) {
d5985138 8365 idx_1 = -255;
238c0a71
AK
8366 /* when MAP_X is passed both maps failures are counted
8367 */
d5985138 8368 if (prev &&
089f9d79
JS
8369 (look_in_map == MAP_1 || look_in_map == MAP_X) &&
8370 i < prev->num_members) {
d5985138
AK
8371 ord = __le32_to_cpu(prev->disk_ord_tbl[i]);
8372 idx_1 = ord_to_idx(ord);
c2a1e7da 8373
d5985138
AK
8374 disk = get_imsm_disk(super, idx_1);
8375 if (!disk || is_failed(disk) || ord & IMSM_ORD_REBUILD)
8376 failed++;
8377 }
089f9d79
JS
8378 if ((look_in_map == MAP_0 || look_in_map == MAP_X) &&
8379 i < map->num_members) {
d5985138
AK
8380 ord = __le32_to_cpu(map->disk_ord_tbl[i]);
8381 idx = ord_to_idx(ord);
8382
8383 if (idx != idx_1) {
8384 disk = get_imsm_disk(super, idx);
8385 if (!disk || is_failed(disk) ||
8386 ord & IMSM_ORD_REBUILD)
8387 failed++;
8388 }
8389 }
c2a1e7da
DW
8390 }
8391
8392 return failed;
845dea95
NB
8393}
8394
97b4d0e9 8395static int imsm_open_new(struct supertype *c, struct active_array *a,
60815698 8396 int inst)
97b4d0e9
DW
8397{
8398 struct intel_super *super = c->sb;
8399 struct imsm_super *mpb = super->anchor;
bbab0940 8400 struct imsm_update_prealloc_bb_mem u;
9587c373 8401
60815698
MG
8402 if (inst >= mpb->num_raid_devs) {
8403 pr_err("subarry index %d, out of range\n", inst);
97b4d0e9
DW
8404 return -ENODEV;
8405 }
8406
60815698
MG
8407 dprintf("imsm: open_new %d\n", inst);
8408 a->info.container_member = inst;
bbab0940
TM
8409
8410 u.type = update_prealloc_badblocks_mem;
8411 imsm_update_metadata_locally(c, &u, sizeof(u));
8412
97b4d0e9
DW
8413 return 0;
8414}
8415
0c046afd
DW
8416static int is_resyncing(struct imsm_dev *dev)
8417{
8418 struct imsm_map *migr_map;
8419
8420 if (!dev->vol.migr_state)
8421 return 0;
8422
1484e727
DW
8423 if (migr_type(dev) == MIGR_INIT ||
8424 migr_type(dev) == MIGR_REPAIR)
0c046afd
DW
8425 return 1;
8426
4c9bc37b
AK
8427 if (migr_type(dev) == MIGR_GEN_MIGR)
8428 return 0;
8429
238c0a71 8430 migr_map = get_imsm_map(dev, MAP_1);
0c046afd 8431
089f9d79
JS
8432 if (migr_map->map_state == IMSM_T_STATE_NORMAL &&
8433 dev->vol.migr_type != MIGR_GEN_MIGR)
0c046afd
DW
8434 return 1;
8435 else
8436 return 0;
8437}
8438
0556e1a2 8439/* return true if we recorded new information */
4c9e8c1e
TM
8440static int mark_failure(struct intel_super *super,
8441 struct imsm_dev *dev, struct imsm_disk *disk, int idx)
47ee5a45 8442{
0556e1a2
DW
8443 __u32 ord;
8444 int slot;
8445 struct imsm_map *map;
86c54047
DW
8446 char buf[MAX_RAID_SERIAL_LEN+3];
8447 unsigned int len, shift = 0;
0556e1a2
DW
8448
8449 /* new failures are always set in map[0] */
238c0a71 8450 map = get_imsm_map(dev, MAP_0);
0556e1a2
DW
8451
8452 slot = get_imsm_disk_slot(map, idx);
8453 if (slot < 0)
8454 return 0;
8455
8456 ord = __le32_to_cpu(map->disk_ord_tbl[slot]);
25ed7e59 8457 if (is_failed(disk) && (ord & IMSM_ORD_REBUILD))
0556e1a2
DW
8458 return 0;
8459
7d0c5e24
LD
8460 memcpy(buf, disk->serial, MAX_RAID_SERIAL_LEN);
8461 buf[MAX_RAID_SERIAL_LEN] = '\000';
8462 strcat(buf, ":0");
86c54047
DW
8463 if ((len = strlen(buf)) >= MAX_RAID_SERIAL_LEN)
8464 shift = len - MAX_RAID_SERIAL_LEN + 1;
167d8bb8 8465 memcpy(disk->serial, &buf[shift], len + 1 - shift);
86c54047 8466
f2f27e63 8467 disk->status |= FAILED_DISK;
0556e1a2 8468 set_imsm_ord_tbl_ent(map, slot, idx | IMSM_ORD_REBUILD);
17788994
AK
8469 /* mark failures in second map if second map exists and this disk
8470 * in this slot.
8471 * This is valid for migration, initialization and rebuild
8472 */
8473 if (dev->vol.migr_state) {
238c0a71 8474 struct imsm_map *map2 = get_imsm_map(dev, MAP_1);
0a108d63
AK
8475 int slot2 = get_imsm_disk_slot(map2, idx);
8476
089f9d79 8477 if (slot2 < map2->num_members && slot2 >= 0)
0a108d63 8478 set_imsm_ord_tbl_ent(map2, slot2,
1ace8403
AK
8479 idx | IMSM_ORD_REBUILD);
8480 }
d7a1fda2
MT
8481 if (map->failed_disk_num == 0xff ||
8482 (!is_rebuilding(dev) && map->failed_disk_num > slot))
0556e1a2 8483 map->failed_disk_num = slot;
4c9e8c1e
TM
8484
8485 clear_disk_badblocks(super->bbm_log, ord_to_idx(ord));
8486
0556e1a2
DW
8487 return 1;
8488}
8489
4c9e8c1e
TM
8490static void mark_missing(struct intel_super *super,
8491 struct imsm_dev *dev, struct imsm_disk *disk, int idx)
0556e1a2 8492{
4c9e8c1e 8493 mark_failure(super, dev, disk, idx);
0556e1a2
DW
8494
8495 if (disk->scsi_id == __cpu_to_le32(~(__u32)0))
8496 return;
8497
47ee5a45
DW
8498 disk->scsi_id = __cpu_to_le32(~(__u32)0);
8499 memmove(&disk->serial[0], &disk->serial[1], MAX_RAID_SERIAL_LEN - 1);
8500}
8501
33414a01
DW
8502static void handle_missing(struct intel_super *super, struct imsm_dev *dev)
8503{
33414a01 8504 struct dl *dl;
33414a01
DW
8505
8506 if (!super->missing)
8507 return;
33414a01 8508
79b68f1b
PC
8509 /* When orom adds replacement for missing disk it does
8510 * not remove entry of missing disk, but just updates map with
8511 * new added disk. So it is not enough just to test if there is
8512 * any missing disk, we have to look if there are any failed disks
8513 * in map to stop migration */
8514
33414a01 8515 dprintf("imsm: mark missing\n");
3d59f0c0
AK
8516 /* end process for initialization and rebuild only
8517 */
195d1d76 8518 if (is_gen_migration(dev) == false) {
fb12a745 8519 int failed = imsm_count_failed(super, dev, MAP_0);
3d59f0c0 8520
fb12a745
TM
8521 if (failed) {
8522 __u8 map_state;
8523 struct imsm_map *map = get_imsm_map(dev, MAP_0);
8524 struct imsm_map *map1;
8525 int i, ord, ord_map1;
8526 int rebuilt = 1;
3d59f0c0 8527
fb12a745
TM
8528 for (i = 0; i < map->num_members; i++) {
8529 ord = get_imsm_ord_tbl_ent(dev, i, MAP_0);
8530 if (!(ord & IMSM_ORD_REBUILD))
8531 continue;
8532
8533 map1 = get_imsm_map(dev, MAP_1);
8534 if (!map1)
8535 continue;
8536
8537 ord_map1 = __le32_to_cpu(map1->disk_ord_tbl[i]);
8538 if (ord_map1 & IMSM_ORD_REBUILD)
8539 rebuilt = 0;
8540 }
8541
8542 if (rebuilt) {
8543 map_state = imsm_check_degraded(super, dev,
8544 failed, MAP_0);
8545 end_migration(dev, super, map_state);
8546 }
8547 }
3d59f0c0 8548 }
33414a01 8549 for (dl = super->missing; dl; dl = dl->next)
4c9e8c1e 8550 mark_missing(super, dev, &dl->disk, dl->index);
33414a01
DW
8551 super->updates_pending++;
8552}
8553
f3871fdc
AK
8554static unsigned long long imsm_set_array_size(struct imsm_dev *dev,
8555 long long new_size)
70bdf0dc 8556{
70bdf0dc 8557 unsigned long long array_blocks;
9529d343
MD
8558 struct imsm_map *map = get_imsm_map(dev, MAP_0);
8559 int used_disks = imsm_num_data_members(map);
70bdf0dc
AK
8560
8561 if (used_disks == 0) {
8562 /* when problems occures
8563 * return current array_blocks value
8564 */
fcc2c9da 8565 array_blocks = imsm_dev_size(dev);
70bdf0dc
AK
8566
8567 return array_blocks;
8568 }
8569
8570 /* set array size in metadata
8571 */
9529d343 8572 if (new_size <= 0)
f3871fdc
AK
8573 /* OLCE size change is caused by added disks
8574 */
44490938 8575 array_blocks = per_dev_array_size(map) * used_disks;
9529d343 8576 else
f3871fdc
AK
8577 /* Online Volume Size Change
8578 * Using available free space
8579 */
8580 array_blocks = new_size;
70bdf0dc 8581
b53bfba6 8582 array_blocks = round_size_to_mb(array_blocks, used_disks);
fcc2c9da 8583 set_imsm_dev_size(dev, array_blocks);
70bdf0dc
AK
8584
8585 return array_blocks;
8586}
8587
28bce06f
AK
8588static void imsm_set_disk(struct active_array *a, int n, int state);
8589
0e2d1a4e
AK
8590static void imsm_progress_container_reshape(struct intel_super *super)
8591{
8592 /* if no device has a migr_state, but some device has a
8593 * different number of members than the previous device, start
8594 * changing the number of devices in this device to match
8595 * previous.
8596 */
8597 struct imsm_super *mpb = super->anchor;
8598 int prev_disks = -1;
8599 int i;
1dfaa380 8600 int copy_map_size;
0e2d1a4e
AK
8601
8602 for (i = 0; i < mpb->num_raid_devs; i++) {
8603 struct imsm_dev *dev = get_imsm_dev(super, i);
238c0a71 8604 struct imsm_map *map = get_imsm_map(dev, MAP_0);
0e2d1a4e
AK
8605 struct imsm_map *map2;
8606 int prev_num_members;
0e2d1a4e
AK
8607
8608 if (dev->vol.migr_state)
8609 return;
8610
8611 if (prev_disks == -1)
8612 prev_disks = map->num_members;
8613 if (prev_disks == map->num_members)
8614 continue;
8615
8616 /* OK, this array needs to enter reshape mode.
8617 * i.e it needs a migr_state
8618 */
8619
1dfaa380 8620 copy_map_size = sizeof_imsm_map(map);
0e2d1a4e
AK
8621 prev_num_members = map->num_members;
8622 map->num_members = prev_disks;
8623 dev->vol.migr_state = 1;
4036e7ee 8624 set_vol_curr_migr_unit(dev, 0);
ea672ee1 8625 set_migr_type(dev, MIGR_GEN_MIGR);
0e2d1a4e
AK
8626 for (i = prev_num_members;
8627 i < map->num_members; i++)
8628 set_imsm_ord_tbl_ent(map, i, i);
238c0a71 8629 map2 = get_imsm_map(dev, MAP_1);
0e2d1a4e 8630 /* Copy the current map */
1dfaa380 8631 memcpy(map2, map, copy_map_size);
0e2d1a4e
AK
8632 map2->num_members = prev_num_members;
8633
f3871fdc 8634 imsm_set_array_size(dev, -1);
51d83f5d 8635 super->clean_migration_record_by_mdmon = 1;
0e2d1a4e
AK
8636 super->updates_pending++;
8637 }
8638}
8639
aad6f216 8640/* Handle dirty -> clean transititions, resync and reshape. Degraded and rebuild
0c046afd
DW
8641 * states are handled in imsm_set_disk() with one exception, when a
8642 * resync is stopped due to a new failure this routine will set the
8643 * 'degraded' state for the array.
8644 */
01f157d7 8645static int imsm_set_array_state(struct active_array *a, int consistent)
a862209d
DW
8646{
8647 int inst = a->info.container_member;
8648 struct intel_super *super = a->container->sb;
949c47a0 8649 struct imsm_dev *dev = get_imsm_dev(super, inst);
238c0a71 8650 struct imsm_map *map = get_imsm_map(dev, MAP_0);
3b451610
AK
8651 int failed = imsm_count_failed(super, dev, MAP_0);
8652 __u8 map_state = imsm_check_degraded(super, dev, failed, MAP_0);
1e5c6983 8653 __u32 blocks_per_unit;
a862209d 8654
1af97990
AK
8655 if (dev->vol.migr_state &&
8656 dev->vol.migr_type == MIGR_GEN_MIGR) {
8657 /* array state change is blocked due to reshape action
aad6f216
N
8658 * We might need to
8659 * - abort the reshape (if last_checkpoint is 0 and action!= reshape)
8660 * - finish the reshape (if last_checkpoint is big and action != reshape)
4036e7ee 8661 * - update vol_curr_migr_unit
1af97990 8662 */
aad6f216 8663 if (a->curr_action == reshape) {
4036e7ee 8664 /* still reshaping, maybe update vol_curr_migr_unit */
633b5610 8665 goto mark_checkpoint;
aad6f216 8666 } else {
aad6f216
N
8667 if (a->last_checkpoint >= a->info.component_size) {
8668 unsigned long long array_blocks;
8669 int used_disks;
e154ced3 8670 struct mdinfo *mdi;
aad6f216 8671
9529d343 8672 used_disks = imsm_num_data_members(map);
d55adef9
AK
8673 if (used_disks > 0) {
8674 array_blocks =
44490938 8675 per_dev_array_size(map) *
d55adef9 8676 used_disks;
b53bfba6
TM
8677 array_blocks =
8678 round_size_to_mb(array_blocks,
8679 used_disks);
d55adef9
AK
8680 a->info.custom_array_size = array_blocks;
8681 /* encourage manager to update array
8682 * size
8683 */
e154ced3 8684
d55adef9 8685 a->check_reshape = 1;
633b5610 8686 }
e154ced3
AK
8687 /* finalize online capacity expansion/reshape */
8688 for (mdi = a->info.devs; mdi; mdi = mdi->next)
8689 imsm_set_disk(a,
8690 mdi->disk.raid_disk,
8691 mdi->curr_state);
8692
0e2d1a4e 8693 imsm_progress_container_reshape(super);
e154ced3 8694 }
aad6f216 8695 }
1af97990
AK
8696 }
8697
47ee5a45 8698 /* before we activate this array handle any missing disks */
33414a01
DW
8699 if (consistent == 2)
8700 handle_missing(super, dev);
1e5c6983 8701
0c046afd 8702 if (consistent == 2 &&
b7941fd6 8703 (!is_resync_complete(&a->info) ||
0c046afd
DW
8704 map_state != IMSM_T_STATE_NORMAL ||
8705 dev->vol.migr_state))
01f157d7 8706 consistent = 0;
272906ef 8707
b7941fd6 8708 if (is_resync_complete(&a->info)) {
0c046afd 8709 /* complete intialization / resync,
0556e1a2
DW
8710 * recovery and interrupted recovery is completed in
8711 * ->set_disk
0c046afd
DW
8712 */
8713 if (is_resyncing(dev)) {
8714 dprintf("imsm: mark resync done\n");
809da78e 8715 end_migration(dev, super, map_state);
115c3803 8716 super->updates_pending++;
484240d8 8717 a->last_checkpoint = 0;
115c3803 8718 }
b9172665
AK
8719 } else if ((!is_resyncing(dev) && !failed) &&
8720 (imsm_reshape_blocks_arrays_changes(super) == 0)) {
0c046afd 8721 /* mark the start of the init process if nothing is failed */
b7941fd6 8722 dprintf("imsm: mark resync start\n");
1484e727 8723 if (map->map_state == IMSM_T_STATE_UNINITIALIZED)
8e59f3d8 8724 migrate(dev, super, IMSM_T_STATE_NORMAL, MIGR_INIT);
1484e727 8725 else
8e59f3d8 8726 migrate(dev, super, IMSM_T_STATE_NORMAL, MIGR_REPAIR);
3393c6af 8727 super->updates_pending++;
115c3803 8728 }
a862209d 8729
fdb7e802
MK
8730 if (a->prev_action == idle)
8731 goto skip_mark_checkpoint;
8732
633b5610 8733mark_checkpoint:
5b83bacf
AK
8734 /* skip checkpointing for general migration,
8735 * it is controlled in mdadm
8736 */
8737 if (is_gen_migration(dev))
8738 goto skip_mark_checkpoint;
8739
4036e7ee
MT
8740 /* check if we can update vol_curr_migr_unit from resync_start,
8741 * recovery_start
8742 */
c47b0ff6 8743 blocks_per_unit = blocks_per_migr_unit(super, dev);
4f0a7acc 8744 if (blocks_per_unit) {
4036e7ee
MT
8745 set_vol_curr_migr_unit(dev,
8746 a->last_checkpoint / blocks_per_unit);
8747 dprintf("imsm: mark checkpoint (%llu)\n",
8748 vol_curr_migr_unit(dev));
8749 super->updates_pending++;
1e5c6983 8750 }
f8f603f1 8751
5b83bacf 8752skip_mark_checkpoint:
3393c6af 8753 /* mark dirty / clean */
2432ce9b
AP
8754 if (((dev->vol.dirty & RAIDVOL_DIRTY) && consistent) ||
8755 (!(dev->vol.dirty & RAIDVOL_DIRTY) && !consistent)) {
b7941fd6 8756 dprintf("imsm: mark '%s'\n", consistent ? "clean" : "dirty");
2432ce9b
AP
8757 if (consistent) {
8758 dev->vol.dirty = RAIDVOL_CLEAN;
8759 } else {
8760 dev->vol.dirty = RAIDVOL_DIRTY;
c2462068
PB
8761 if (dev->rwh_policy == RWH_DISTRIBUTED ||
8762 dev->rwh_policy == RWH_MULTIPLE_DISTRIBUTED)
2432ce9b
AP
8763 dev->vol.dirty |= RAIDVOL_DSRECORD_VALID;
8764 }
a862209d
DW
8765 super->updates_pending++;
8766 }
28bce06f 8767
01f157d7 8768 return consistent;
a862209d
DW
8769}
8770
6f50473f
TM
8771static int imsm_disk_slot_to_ord(struct active_array *a, int slot)
8772{
8773 int inst = a->info.container_member;
8774 struct intel_super *super = a->container->sb;
8775 struct imsm_dev *dev = get_imsm_dev(super, inst);
8776 struct imsm_map *map = get_imsm_map(dev, MAP_0);
8777
8778 if (slot > map->num_members) {
8779 pr_err("imsm: imsm_disk_slot_to_ord %d out of range 0..%d\n",
8780 slot, map->num_members - 1);
8781 return -1;
8782 }
8783
8784 if (slot < 0)
8785 return -1;
8786
8787 return get_imsm_ord_tbl_ent(dev, slot, MAP_0);
8788}
8789
8d45d196 8790static void imsm_set_disk(struct active_array *a, int n, int state)
845dea95 8791{
8d45d196
DW
8792 int inst = a->info.container_member;
8793 struct intel_super *super = a->container->sb;
949c47a0 8794 struct imsm_dev *dev = get_imsm_dev(super, inst);
238c0a71 8795 struct imsm_map *map = get_imsm_map(dev, MAP_0);
8d45d196 8796 struct imsm_disk *disk;
7ce05701
LD
8797 struct mdinfo *mdi;
8798 int recovery_not_finished = 0;
0c046afd 8799 int failed;
6f50473f 8800 int ord;
0c046afd 8801 __u8 map_state;
fb12a745
TM
8802 int rebuild_done = 0;
8803 int i;
8d45d196 8804
fb12a745 8805 ord = get_imsm_ord_tbl_ent(dev, n, MAP_X);
6f50473f 8806 if (ord < 0)
8d45d196
DW
8807 return;
8808
4e6e574a 8809 dprintf("imsm: set_disk %d:%x\n", n, state);
b10b37b8 8810 disk = get_imsm_disk(super, ord_to_idx(ord));
8d45d196 8811
5802a811 8812 /* check for new failures */
ae7d61e3 8813 if (disk && (state & DS_FAULTY)) {
4c9e8c1e 8814 if (mark_failure(super, dev, disk, ord_to_idx(ord)))
0556e1a2 8815 super->updates_pending++;
8d45d196 8816 }
47ee5a45 8817
19859edc 8818 /* check if in_sync */
0556e1a2 8819 if (state & DS_INSYNC && ord & IMSM_ORD_REBUILD && is_rebuilding(dev)) {
238c0a71 8820 struct imsm_map *migr_map = get_imsm_map(dev, MAP_1);
b10b37b8
DW
8821
8822 set_imsm_ord_tbl_ent(migr_map, n, ord_to_idx(ord));
fb12a745 8823 rebuild_done = 1;
19859edc
DW
8824 super->updates_pending++;
8825 }
8d45d196 8826
3b451610
AK
8827 failed = imsm_count_failed(super, dev, MAP_0);
8828 map_state = imsm_check_degraded(super, dev, failed, MAP_0);
5802a811 8829
0c046afd 8830 /* check if recovery complete, newly degraded, or failed */
94002678
AK
8831 dprintf("imsm: Detected transition to state ");
8832 switch (map_state) {
8833 case IMSM_T_STATE_NORMAL: /* transition to normal state */
8834 dprintf("normal: ");
8835 if (is_rebuilding(dev)) {
1ade5cc1 8836 dprintf_cont("while rebuilding");
7ce05701
LD
8837 /* check if recovery is really finished */
8838 for (mdi = a->info.devs; mdi ; mdi = mdi->next)
8839 if (mdi->recovery_start != MaxSector) {
8840 recovery_not_finished = 1;
8841 break;
8842 }
8843 if (recovery_not_finished) {
1ade5cc1
N
8844 dprintf_cont("\n");
8845 dprintf("Rebuild has not finished yet, state not changed");
7ce05701
LD
8846 if (a->last_checkpoint < mdi->recovery_start) {
8847 a->last_checkpoint = mdi->recovery_start;
8848 super->updates_pending++;
8849 }
8850 break;
8851 }
94002678 8852 end_migration(dev, super, map_state);
94002678
AK
8853 map->failed_disk_num = ~0;
8854 super->updates_pending++;
8855 a->last_checkpoint = 0;
8856 break;
8857 }
8858 if (is_gen_migration(dev)) {
1ade5cc1 8859 dprintf_cont("while general migration");
bf2f0071 8860 if (a->last_checkpoint >= a->info.component_size)
809da78e 8861 end_migration(dev, super, map_state);
94002678
AK
8862 else
8863 map->map_state = map_state;
28bce06f 8864 map->failed_disk_num = ~0;
94002678 8865 super->updates_pending++;
bf2f0071 8866 break;
94002678
AK
8867 }
8868 break;
8869 case IMSM_T_STATE_DEGRADED: /* transition to degraded state */
1ade5cc1 8870 dprintf_cont("degraded: ");
089f9d79 8871 if (map->map_state != map_state && !dev->vol.migr_state) {
1ade5cc1 8872 dprintf_cont("mark degraded");
94002678
AK
8873 map->map_state = map_state;
8874 super->updates_pending++;
8875 a->last_checkpoint = 0;
8876 break;
8877 }
8878 if (is_rebuilding(dev)) {
d7a1fda2 8879 dprintf_cont("while rebuilding ");
a4e96fd8
MT
8880 if (state & DS_FAULTY) {
8881 dprintf_cont("removing failed drive ");
d7a1fda2
MT
8882 if (n == map->failed_disk_num) {
8883 dprintf_cont("end migration");
8884 end_migration(dev, super, map_state);
a4e96fd8 8885 a->last_checkpoint = 0;
d7a1fda2 8886 } else {
a4e96fd8 8887 dprintf_cont("fail detected during rebuild, changing map state");
d7a1fda2
MT
8888 map->map_state = map_state;
8889 }
94002678 8890 super->updates_pending++;
fb12a745
TM
8891 }
8892
a4e96fd8
MT
8893 if (!rebuild_done)
8894 break;
8895
fb12a745
TM
8896 /* check if recovery is really finished */
8897 for (mdi = a->info.devs; mdi ; mdi = mdi->next)
8898 if (mdi->recovery_start != MaxSector) {
8899 recovery_not_finished = 1;
8900 break;
8901 }
8902 if (recovery_not_finished) {
8903 dprintf_cont("\n");
a4e96fd8 8904 dprintf_cont("Rebuild has not finished yet");
fb12a745
TM
8905 if (a->last_checkpoint < mdi->recovery_start) {
8906 a->last_checkpoint =
8907 mdi->recovery_start;
8908 super->updates_pending++;
8909 }
8910 break;
94002678 8911 }
fb12a745
TM
8912
8913 dprintf_cont(" Rebuild done, still degraded");
a4e96fd8
MT
8914 end_migration(dev, super, map_state);
8915 a->last_checkpoint = 0;
8916 super->updates_pending++;
fb12a745
TM
8917
8918 for (i = 0; i < map->num_members; i++) {
8919 int idx = get_imsm_ord_tbl_ent(dev, i, MAP_0);
8920
8921 if (idx & IMSM_ORD_REBUILD)
8922 map->failed_disk_num = i;
8923 }
8924 super->updates_pending++;
94002678
AK
8925 break;
8926 }
8927 if (is_gen_migration(dev)) {
1ade5cc1 8928 dprintf_cont("while general migration");
bf2f0071 8929 if (a->last_checkpoint >= a->info.component_size)
809da78e 8930 end_migration(dev, super, map_state);
94002678
AK
8931 else {
8932 map->map_state = map_state;
3b451610 8933 manage_second_map(super, dev);
94002678
AK
8934 }
8935 super->updates_pending++;
bf2f0071 8936 break;
28bce06f 8937 }
6ce1fbf1 8938 if (is_initializing(dev)) {
1ade5cc1 8939 dprintf_cont("while initialization.");
6ce1fbf1
AK
8940 map->map_state = map_state;
8941 super->updates_pending++;
8942 break;
8943 }
94002678
AK
8944 break;
8945 case IMSM_T_STATE_FAILED: /* transition to failed state */
1ade5cc1 8946 dprintf_cont("failed: ");
94002678 8947 if (is_gen_migration(dev)) {
1ade5cc1 8948 dprintf_cont("while general migration");
94002678
AK
8949 map->map_state = map_state;
8950 super->updates_pending++;
8951 break;
8952 }
8953 if (map->map_state != map_state) {
1ade5cc1 8954 dprintf_cont("mark failed");
94002678
AK
8955 end_migration(dev, super, map_state);
8956 super->updates_pending++;
8957 a->last_checkpoint = 0;
8958 break;
8959 }
8960 break;
8961 default:
1ade5cc1 8962 dprintf_cont("state %i\n", map_state);
5802a811 8963 }
1ade5cc1 8964 dprintf_cont("\n");
845dea95
NB
8965}
8966
f796af5d 8967static int store_imsm_mpb(int fd, struct imsm_super *mpb)
c2a1e7da 8968{
f796af5d 8969 void *buf = mpb;
c2a1e7da
DW
8970 __u32 mpb_size = __le32_to_cpu(mpb->mpb_size);
8971 unsigned long long dsize;
8972 unsigned long long sectors;
f36a9ecd 8973 unsigned int sector_size;
c2a1e7da 8974
aec01630
JS
8975 if (!get_dev_sector_size(fd, NULL, &sector_size))
8976 return 1;
c2a1e7da
DW
8977 get_dev_size(fd, NULL, &dsize);
8978
f36a9ecd 8979 if (mpb_size > sector_size) {
272f648f 8980 /* -1 to account for anchor */
f36a9ecd 8981 sectors = mpb_sectors(mpb, sector_size) - 1;
c2a1e7da 8982
272f648f 8983 /* write the extended mpb to the sectors preceeding the anchor */
f36a9ecd
PB
8984 if (lseek64(fd, dsize - (sector_size * (2 + sectors)),
8985 SEEK_SET) < 0)
272f648f 8986 return 1;
c2a1e7da 8987
f36a9ecd
PB
8988 if ((unsigned long long)write(fd, buf + sector_size,
8989 sector_size * sectors) != sector_size * sectors)
272f648f
DW
8990 return 1;
8991 }
c2a1e7da 8992
272f648f 8993 /* first block is stored on second to last sector of the disk */
f36a9ecd 8994 if (lseek64(fd, dsize - (sector_size * 2), SEEK_SET) < 0)
c2a1e7da
DW
8995 return 1;
8996
466070ad 8997 if ((unsigned int)write(fd, buf, sector_size) != sector_size)
c2a1e7da
DW
8998 return 1;
8999
c2a1e7da
DW
9000 return 0;
9001}
9002
2e735d19 9003static void imsm_sync_metadata(struct supertype *container)
845dea95 9004{
2e735d19 9005 struct intel_super *super = container->sb;
c2a1e7da 9006
1a64be56 9007 dprintf("sync metadata: %d\n", super->updates_pending);
c2a1e7da
DW
9008 if (!super->updates_pending)
9009 return;
9010
36988a3d 9011 write_super_imsm(container, 0);
c2a1e7da
DW
9012
9013 super->updates_pending = 0;
845dea95
NB
9014}
9015
272906ef
DW
9016static struct dl *imsm_readd(struct intel_super *super, int idx, struct active_array *a)
9017{
9018 struct imsm_dev *dev = get_imsm_dev(super, a->info.container_member);
238c0a71 9019 int i = get_imsm_disk_idx(dev, idx, MAP_X);
272906ef
DW
9020 struct dl *dl;
9021
9022 for (dl = super->disks; dl; dl = dl->next)
9023 if (dl->index == i)
9024 break;
9025
25ed7e59 9026 if (dl && is_failed(&dl->disk))
272906ef
DW
9027 dl = NULL;
9028
9029 if (dl)
1ade5cc1 9030 dprintf("found %x:%x\n", dl->major, dl->minor);
272906ef
DW
9031
9032 return dl;
9033}
9034
a20d2ba5 9035static struct dl *imsm_add_spare(struct intel_super *super, int slot,
8ba77d32
AK
9036 struct active_array *a, int activate_new,
9037 struct mdinfo *additional_test_list)
272906ef
DW
9038{
9039 struct imsm_dev *dev = get_imsm_dev(super, a->info.container_member);
238c0a71 9040 int idx = get_imsm_disk_idx(dev, slot, MAP_X);
a20d2ba5
DW
9041 struct imsm_super *mpb = super->anchor;
9042 struct imsm_map *map;
272906ef
DW
9043 unsigned long long pos;
9044 struct mdinfo *d;
9045 struct extent *ex;
a20d2ba5 9046 int i, j;
272906ef 9047 int found;
569cc43f
DW
9048 __u32 array_start = 0;
9049 __u32 array_end = 0;
272906ef 9050 struct dl *dl;
6c932028 9051 struct mdinfo *test_list;
272906ef
DW
9052
9053 for (dl = super->disks; dl; dl = dl->next) {
9054 /* If in this array, skip */
9055 for (d = a->info.devs ; d ; d = d->next)
4389ce73 9056 if (is_fd_valid(d->state_fd) &&
e553d2a4 9057 d->disk.major == dl->major &&
272906ef 9058 d->disk.minor == dl->minor) {
8ba77d32
AK
9059 dprintf("%x:%x already in array\n",
9060 dl->major, dl->minor);
272906ef
DW
9061 break;
9062 }
9063 if (d)
9064 continue;
6c932028
AK
9065 test_list = additional_test_list;
9066 while (test_list) {
9067 if (test_list->disk.major == dl->major &&
9068 test_list->disk.minor == dl->minor) {
8ba77d32
AK
9069 dprintf("%x:%x already in additional test list\n",
9070 dl->major, dl->minor);
9071 break;
9072 }
6c932028 9073 test_list = test_list->next;
8ba77d32 9074 }
6c932028 9075 if (test_list)
8ba77d32 9076 continue;
272906ef 9077
e553d2a4 9078 /* skip in use or failed drives */
25ed7e59 9079 if (is_failed(&dl->disk) || idx == dl->index ||
df474657
DW
9080 dl->index == -2) {
9081 dprintf("%x:%x status (failed: %d index: %d)\n",
25ed7e59 9082 dl->major, dl->minor, is_failed(&dl->disk), idx);
9a1608e5
DW
9083 continue;
9084 }
9085
a20d2ba5
DW
9086 /* skip pure spares when we are looking for partially
9087 * assimilated drives
9088 */
9089 if (dl->index == -1 && !activate_new)
9090 continue;
9091
f2cc4f7d
AO
9092 if (!drive_validate_sector_size(super, dl))
9093 continue;
9094
272906ef 9095 /* Does this unused device have the requisite free space?
a20d2ba5 9096 * It needs to be able to cover all member volumes
272906ef 9097 */
05501181 9098 ex = get_extents(super, dl, 1);
272906ef
DW
9099 if (!ex) {
9100 dprintf("cannot get extents\n");
9101 continue;
9102 }
a20d2ba5
DW
9103 for (i = 0; i < mpb->num_raid_devs; i++) {
9104 dev = get_imsm_dev(super, i);
238c0a71 9105 map = get_imsm_map(dev, MAP_0);
272906ef 9106
a20d2ba5
DW
9107 /* check if this disk is already a member of
9108 * this array
272906ef 9109 */
620b1713 9110 if (get_imsm_disk_slot(map, dl->index) >= 0)
a20d2ba5
DW
9111 continue;
9112
9113 found = 0;
9114 j = 0;
9115 pos = 0;
5551b113 9116 array_start = pba_of_lba0(map);
329c8278 9117 array_end = array_start +
44490938 9118 per_dev_array_size(map) - 1;
a20d2ba5
DW
9119
9120 do {
9121 /* check that we can start at pba_of_lba0 with
44490938 9122 * num_data_stripes*blocks_per_stripe of space
a20d2ba5 9123 */
329c8278 9124 if (array_start >= pos && array_end < ex[j].start) {
a20d2ba5
DW
9125 found = 1;
9126 break;
9127 }
9128 pos = ex[j].start + ex[j].size;
9129 j++;
9130 } while (ex[j-1].size);
9131
9132 if (!found)
272906ef 9133 break;
a20d2ba5 9134 }
272906ef
DW
9135
9136 free(ex);
a20d2ba5 9137 if (i < mpb->num_raid_devs) {
329c8278
DW
9138 dprintf("%x:%x does not have %u to %u available\n",
9139 dl->major, dl->minor, array_start, array_end);
272906ef
DW
9140 /* No room */
9141 continue;
a20d2ba5
DW
9142 }
9143 return dl;
272906ef
DW
9144 }
9145
9146 return dl;
9147}
9148
95d07a2c
LM
9149static int imsm_rebuild_allowed(struct supertype *cont, int dev_idx, int failed)
9150{
9151 struct imsm_dev *dev2;
9152 struct imsm_map *map;
9153 struct dl *idisk;
9154 int slot;
9155 int idx;
9156 __u8 state;
9157
9158 dev2 = get_imsm_dev(cont->sb, dev_idx);
756a15f3
MG
9159
9160 state = imsm_check_degraded(cont->sb, dev2, failed, MAP_0);
9161 if (state == IMSM_T_STATE_FAILED) {
9162 map = get_imsm_map(dev2, MAP_0);
9163 for (slot = 0; slot < map->num_members; slot++) {
9164 /*
9165 * Check if failed disks are deleted from intel
9166 * disk list or are marked to be deleted
9167 */
9168 idx = get_imsm_disk_idx(dev2, slot, MAP_X);
9169 idisk = get_imsm_dl_disk(cont->sb, idx);
9170 /*
9171 * Do not rebuild the array if failed disks
9172 * from failed sub-array are not removed from
9173 * container.
9174 */
9175 if (idisk &&
9176 is_failed(&idisk->disk) &&
9177 (idisk->action != DISK_REMOVE))
9178 return 0;
95d07a2c
LM
9179 }
9180 }
9181 return 1;
9182}
9183
88758e9d
DW
9184static struct mdinfo *imsm_activate_spare(struct active_array *a,
9185 struct metadata_update **updates)
9186{
9187 /**
d23fe947
DW
9188 * Find a device with unused free space and use it to replace a
9189 * failed/vacant region in an array. We replace failed regions one a
9190 * array at a time. The result is that a new spare disk will be added
9191 * to the first failed array and after the monitor has finished
9192 * propagating failures the remainder will be consumed.
88758e9d 9193 *
d23fe947
DW
9194 * FIXME add a capability for mdmon to request spares from another
9195 * container.
88758e9d
DW
9196 */
9197
9198 struct intel_super *super = a->container->sb;
88758e9d 9199 int inst = a->info.container_member;
949c47a0 9200 struct imsm_dev *dev = get_imsm_dev(super, inst);
238c0a71 9201 struct imsm_map *map = get_imsm_map(dev, MAP_0);
88758e9d
DW
9202 int failed = a->info.array.raid_disks;
9203 struct mdinfo *rv = NULL;
9204 struct mdinfo *d;
9205 struct mdinfo *di;
9206 struct metadata_update *mu;
9207 struct dl *dl;
9208 struct imsm_update_activate_spare *u;
9209 int num_spares = 0;
9210 int i;
95d07a2c 9211 int allowed;
88758e9d 9212
4389ce73
MT
9213 for (d = a->info.devs ; d; d = d->next) {
9214 if (!is_fd_valid(d->state_fd))
9215 continue;
9216
9217 if (d->curr_state & DS_FAULTY)
88758e9d
DW
9218 /* wait for Removal to happen */
9219 return NULL;
4389ce73
MT
9220
9221 failed--;
88758e9d
DW
9222 }
9223
9224 dprintf("imsm: activate spare: inst=%d failed=%d (%d) level=%d\n",
9225 inst, failed, a->info.array.raid_disks, a->info.array.level);
1af97990 9226
e2962bfc
AK
9227 if (imsm_reshape_blocks_arrays_changes(super))
9228 return NULL;
1af97990 9229
fc8ca064
AK
9230 /* Cannot activate another spare if rebuild is in progress already
9231 */
9232 if (is_rebuilding(dev)) {
7a862a02 9233 dprintf("imsm: No spare activation allowed. Rebuild in progress already.\n");
fc8ca064
AK
9234 return NULL;
9235 }
9236
89c67882
AK
9237 if (a->info.array.level == 4)
9238 /* No repair for takeovered array
9239 * imsm doesn't support raid4
9240 */
9241 return NULL;
9242
3b451610
AK
9243 if (imsm_check_degraded(super, dev, failed, MAP_0) !=
9244 IMSM_T_STATE_DEGRADED)
88758e9d
DW
9245 return NULL;
9246
83ca7d45
AP
9247 if (get_imsm_map(dev, MAP_0)->map_state == IMSM_T_STATE_UNINITIALIZED) {
9248 dprintf("imsm: No spare activation allowed. Volume is not initialized.\n");
9249 return NULL;
9250 }
9251
95d07a2c
LM
9252 /*
9253 * If there are any failed disks check state of the other volume.
9254 * Block rebuild if the another one is failed until failed disks
9255 * are removed from container.
9256 */
9257 if (failed) {
7a862a02 9258 dprintf("found failed disks in %.*s, check if there anotherfailed sub-array.\n",
c4acd1e5 9259 MAX_RAID_SERIAL_LEN, dev->volume);
95d07a2c
LM
9260 /* check if states of the other volumes allow for rebuild */
9261 for (i = 0; i < super->anchor->num_raid_devs; i++) {
9262 if (i != inst) {
9263 allowed = imsm_rebuild_allowed(a->container,
9264 i, failed);
9265 if (!allowed)
9266 return NULL;
9267 }
9268 }
9269 }
9270
88758e9d 9271 /* For each slot, if it is not working, find a spare */
88758e9d
DW
9272 for (i = 0; i < a->info.array.raid_disks; i++) {
9273 for (d = a->info.devs ; d ; d = d->next)
9274 if (d->disk.raid_disk == i)
9275 break;
9276 dprintf("found %d: %p %x\n", i, d, d?d->curr_state:0);
4389ce73 9277 if (d && is_fd_valid(d->state_fd))
88758e9d
DW
9278 continue;
9279
272906ef 9280 /*
a20d2ba5
DW
9281 * OK, this device needs recovery. Try to re-add the
9282 * previous occupant of this slot, if this fails see if
9283 * we can continue the assimilation of a spare that was
9284 * partially assimilated, finally try to activate a new
9285 * spare.
272906ef
DW
9286 */
9287 dl = imsm_readd(super, i, a);
9288 if (!dl)
b303fe21 9289 dl = imsm_add_spare(super, i, a, 0, rv);
a20d2ba5 9290 if (!dl)
b303fe21 9291 dl = imsm_add_spare(super, i, a, 1, rv);
272906ef
DW
9292 if (!dl)
9293 continue;
1011e834 9294
272906ef 9295 /* found a usable disk with enough space */
503975b9 9296 di = xcalloc(1, sizeof(*di));
272906ef
DW
9297
9298 /* dl->index will be -1 in the case we are activating a
9299 * pristine spare. imsm_process_update() will create a
9300 * new index in this case. Once a disk is found to be
9301 * failed in all member arrays it is kicked from the
9302 * metadata
9303 */
9304 di->disk.number = dl->index;
d23fe947 9305
272906ef
DW
9306 /* (ab)use di->devs to store a pointer to the device
9307 * we chose
9308 */
9309 di->devs = (struct mdinfo *) dl;
9310
9311 di->disk.raid_disk = i;
9312 di->disk.major = dl->major;
9313 di->disk.minor = dl->minor;
9314 di->disk.state = 0;
d23534e4 9315 di->recovery_start = 0;
5551b113 9316 di->data_offset = pba_of_lba0(map);
272906ef
DW
9317 di->component_size = a->info.component_size;
9318 di->container_member = inst;
5e46202e 9319 di->bb.supported = 1;
2c8890e9 9320 if (a->info.consistency_policy == CONSISTENCY_POLICY_PPL) {
2432ce9b 9321 di->ppl_sector = get_ppl_sector(super, inst);
c2462068 9322 di->ppl_size = MULTIPLE_PPL_AREA_SIZE_IMSM >> 9;
2432ce9b 9323 }
148acb7b 9324 super->random = random32();
272906ef
DW
9325 di->next = rv;
9326 rv = di;
9327 num_spares++;
9328 dprintf("%x:%x to be %d at %llu\n", dl->major, dl->minor,
9329 i, di->data_offset);
88758e9d
DW
9330 }
9331
9332 if (!rv)
9333 /* No spares found */
9334 return rv;
9335 /* Now 'rv' has a list of devices to return.
9336 * Create a metadata_update record to update the
9337 * disk_ord_tbl for the array
9338 */
503975b9 9339 mu = xmalloc(sizeof(*mu));
1011e834 9340 mu->buf = xcalloc(num_spares,
503975b9 9341 sizeof(struct imsm_update_activate_spare));
88758e9d 9342 mu->space = NULL;
cb23f1f4 9343 mu->space_list = NULL;
88758e9d
DW
9344 mu->len = sizeof(struct imsm_update_activate_spare) * num_spares;
9345 mu->next = *updates;
9346 u = (struct imsm_update_activate_spare *) mu->buf;
9347
9348 for (di = rv ; di ; di = di->next) {
9349 u->type = update_activate_spare;
d23fe947
DW
9350 u->dl = (struct dl *) di->devs;
9351 di->devs = NULL;
88758e9d
DW
9352 u->slot = di->disk.raid_disk;
9353 u->array = inst;
9354 u->next = u + 1;
9355 u++;
9356 }
9357 (u-1)->next = NULL;
9358 *updates = mu;
9359
9360 return rv;
9361}
9362
54c2c1ea 9363static int disks_overlap(struct intel_super *super, int idx, struct imsm_update_create_array *u)
8273f55e 9364{
54c2c1ea 9365 struct imsm_dev *dev = get_imsm_dev(super, idx);
238c0a71
AK
9366 struct imsm_map *map = get_imsm_map(dev, MAP_0);
9367 struct imsm_map *new_map = get_imsm_map(&u->dev, MAP_0);
54c2c1ea
DW
9368 struct disk_info *inf = get_disk_info(u);
9369 struct imsm_disk *disk;
8273f55e
DW
9370 int i;
9371 int j;
8273f55e 9372
54c2c1ea 9373 for (i = 0; i < map->num_members; i++) {
238c0a71 9374 disk = get_imsm_disk(super, get_imsm_disk_idx(dev, i, MAP_X));
54c2c1ea
DW
9375 for (j = 0; j < new_map->num_members; j++)
9376 if (serialcmp(disk->serial, inf[j].serial) == 0)
8273f55e
DW
9377 return 1;
9378 }
9379
9380 return 0;
9381}
9382
1a64be56
LM
9383static struct dl *get_disk_super(struct intel_super *super, int major, int minor)
9384{
594dc1b8
JS
9385 struct dl *dl;
9386
1a64be56 9387 for (dl = super->disks; dl; dl = dl->next)
089f9d79 9388 if (dl->major == major && dl->minor == minor)
1a64be56
LM
9389 return dl;
9390 return NULL;
9391}
9392
9393static int remove_disk_super(struct intel_super *super, int major, int minor)
9394{
594dc1b8 9395 struct dl *prev;
1a64be56
LM
9396 struct dl *dl;
9397
9398 prev = NULL;
9399 for (dl = super->disks; dl; dl = dl->next) {
089f9d79 9400 if (dl->major == major && dl->minor == minor) {
1a64be56
LM
9401 /* remove */
9402 if (prev)
9403 prev->next = dl->next;
9404 else
9405 super->disks = dl->next;
9406 dl->next = NULL;
3a85bf0e 9407 __free_imsm_disk(dl, 1);
1ade5cc1 9408 dprintf("removed %x:%x\n", major, minor);
1a64be56
LM
9409 break;
9410 }
9411 prev = dl;
9412 }
9413 return 0;
9414}
9415
f21e18ca 9416static void imsm_delete(struct intel_super *super, struct dl **dlp, unsigned index);
ae6aad82 9417
1a64be56
LM
9418static int add_remove_disk_update(struct intel_super *super)
9419{
9420 int check_degraded = 0;
594dc1b8
JS
9421 struct dl *disk;
9422
1a64be56
LM
9423 /* add/remove some spares to/from the metadata/contrainer */
9424 while (super->disk_mgmt_list) {
9425 struct dl *disk_cfg;
9426
9427 disk_cfg = super->disk_mgmt_list;
9428 super->disk_mgmt_list = disk_cfg->next;
9429 disk_cfg->next = NULL;
9430
9431 if (disk_cfg->action == DISK_ADD) {
9432 disk_cfg->next = super->disks;
9433 super->disks = disk_cfg;
9434 check_degraded = 1;
1ade5cc1
N
9435 dprintf("added %x:%x\n",
9436 disk_cfg->major, disk_cfg->minor);
1a64be56
LM
9437 } else if (disk_cfg->action == DISK_REMOVE) {
9438 dprintf("Disk remove action processed: %x.%x\n",
9439 disk_cfg->major, disk_cfg->minor);
9440 disk = get_disk_super(super,
9441 disk_cfg->major,
9442 disk_cfg->minor);
9443 if (disk) {
9444 /* store action status */
9445 disk->action = DISK_REMOVE;
9446 /* remove spare disks only */
9447 if (disk->index == -1) {
9448 remove_disk_super(super,
9449 disk_cfg->major,
9450 disk_cfg->minor);
91c97c54
MT
9451 } else {
9452 disk_cfg->fd = disk->fd;
9453 disk->fd = -1;
1a64be56
LM
9454 }
9455 }
9456 /* release allocate disk structure */
3a85bf0e 9457 __free_imsm_disk(disk_cfg, 1);
1a64be56
LM
9458 }
9459 }
9460 return check_degraded;
9461}
9462
a29911da
PC
9463static int apply_reshape_migration_update(struct imsm_update_reshape_migration *u,
9464 struct intel_super *super,
9465 void ***space_list)
9466{
9467 struct intel_dev *id;
9468 void **tofree = NULL;
9469 int ret_val = 0;
9470
1ade5cc1 9471 dprintf("(enter)\n");
089f9d79 9472 if (u->subdev < 0 || u->subdev > 1) {
a29911da
PC
9473 dprintf("imsm: Error: Wrong subdev: %i\n", u->subdev);
9474 return ret_val;
9475 }
089f9d79 9476 if (space_list == NULL || *space_list == NULL) {
a29911da
PC
9477 dprintf("imsm: Error: Memory is not allocated\n");
9478 return ret_val;
9479 }
9480
9481 for (id = super->devlist ; id; id = id->next) {
9482 if (id->index == (unsigned)u->subdev) {
9483 struct imsm_dev *dev = get_imsm_dev(super, u->subdev);
9484 struct imsm_map *map;
9485 struct imsm_dev *new_dev =
9486 (struct imsm_dev *)*space_list;
238c0a71 9487 struct imsm_map *migr_map = get_imsm_map(dev, MAP_1);
a29911da
PC
9488 int to_state;
9489 struct dl *new_disk;
9490
9491 if (new_dev == NULL)
9492 return ret_val;
9493 *space_list = **space_list;
9494 memcpy(new_dev, dev, sizeof_imsm_dev(dev, 0));
238c0a71 9495 map = get_imsm_map(new_dev, MAP_0);
a29911da
PC
9496 if (migr_map) {
9497 dprintf("imsm: Error: migration in progress");
9498 return ret_val;
9499 }
9500
9501 to_state = map->map_state;
9502 if ((u->new_level == 5) && (map->raid_level == 0)) {
9503 map->num_members++;
9504 /* this should not happen */
9505 if (u->new_disks[0] < 0) {
9506 map->failed_disk_num =
9507 map->num_members - 1;
9508 to_state = IMSM_T_STATE_DEGRADED;
9509 } else
9510 to_state = IMSM_T_STATE_NORMAL;
9511 }
8e59f3d8 9512 migrate(new_dev, super, to_state, MIGR_GEN_MIGR);
a29911da
PC
9513 if (u->new_level > -1)
9514 map->raid_level = u->new_level;
238c0a71 9515 migr_map = get_imsm_map(new_dev, MAP_1);
a29911da
PC
9516 if ((u->new_level == 5) &&
9517 (migr_map->raid_level == 0)) {
9518 int ord = map->num_members - 1;
9519 migr_map->num_members--;
9520 if (u->new_disks[0] < 0)
9521 ord |= IMSM_ORD_REBUILD;
9522 set_imsm_ord_tbl_ent(map,
9523 map->num_members - 1,
9524 ord);
9525 }
9526 id->dev = new_dev;
9527 tofree = (void **)dev;
9528
4bba0439
PC
9529 /* update chunk size
9530 */
06fb291a 9531 if (u->new_chunksize > 0) {
9529d343
MD
9532 struct imsm_map *dest_map =
9533 get_imsm_map(dev, MAP_0);
06fb291a 9534 int used_disks =
9529d343 9535 imsm_num_data_members(dest_map);
06fb291a
PB
9536
9537 if (used_disks == 0)
9538 return ret_val;
9539
4bba0439
PC
9540 map->blocks_per_strip =
9541 __cpu_to_le16(u->new_chunksize * 2);
1c275381 9542 update_num_data_stripes(map, imsm_dev_size(dev));
06fb291a 9543 }
4bba0439 9544
44490938
MD
9545 /* ensure blocks_per_member has valid value
9546 */
9547 set_blocks_per_member(map,
9548 per_dev_array_size(map) +
9549 NUM_BLOCKS_DIRTY_STRIPE_REGION);
9550
a29911da
PC
9551 /* add disk
9552 */
089f9d79
JS
9553 if (u->new_level != 5 || migr_map->raid_level != 0 ||
9554 migr_map->raid_level == map->raid_level)
a29911da
PC
9555 goto skip_disk_add;
9556
9557 if (u->new_disks[0] >= 0) {
9558 /* use passes spare
9559 */
9560 new_disk = get_disk_super(super,
9561 major(u->new_disks[0]),
9562 minor(u->new_disks[0]));
7a862a02 9563 dprintf("imsm: new disk for reshape is: %i:%i (%p, index = %i)\n",
a29911da
PC
9564 major(u->new_disks[0]),
9565 minor(u->new_disks[0]),
9566 new_disk, new_disk->index);
9567 if (new_disk == NULL)
9568 goto error_disk_add;
9569
9570 new_disk->index = map->num_members - 1;
9571 /* slot to fill in autolayout
9572 */
9573 new_disk->raiddisk = new_disk->index;
9574 new_disk->disk.status |= CONFIGURED_DISK;
9575 new_disk->disk.status &= ~SPARE_DISK;
9576 } else
9577 goto error_disk_add;
9578
9579skip_disk_add:
9580 *tofree = *space_list;
9581 /* calculate new size
9582 */
f3871fdc 9583 imsm_set_array_size(new_dev, -1);
a29911da
PC
9584
9585 ret_val = 1;
9586 }
9587 }
9588
9589 if (tofree)
9590 *space_list = tofree;
9591 return ret_val;
9592
9593error_disk_add:
9594 dprintf("Error: imsm: Cannot find disk.\n");
9595 return ret_val;
9596}
9597
f3871fdc
AK
9598static int apply_size_change_update(struct imsm_update_size_change *u,
9599 struct intel_super *super)
9600{
9601 struct intel_dev *id;
9602 int ret_val = 0;
9603
1ade5cc1 9604 dprintf("(enter)\n");
089f9d79 9605 if (u->subdev < 0 || u->subdev > 1) {
f3871fdc
AK
9606 dprintf("imsm: Error: Wrong subdev: %i\n", u->subdev);
9607 return ret_val;
9608 }
9609
9610 for (id = super->devlist ; id; id = id->next) {
9611 if (id->index == (unsigned)u->subdev) {
9612 struct imsm_dev *dev = get_imsm_dev(super, u->subdev);
9613 struct imsm_map *map = get_imsm_map(dev, MAP_0);
9529d343 9614 int used_disks = imsm_num_data_members(map);
f3871fdc 9615 unsigned long long blocks_per_member;
44490938
MD
9616 unsigned long long new_size_per_disk;
9617
9618 if (used_disks == 0)
9619 return 0;
f3871fdc
AK
9620
9621 /* calculate new size
9622 */
44490938
MD
9623 new_size_per_disk = u->new_size / used_disks;
9624 blocks_per_member = new_size_per_disk +
9625 NUM_BLOCKS_DIRTY_STRIPE_REGION;
f3871fdc 9626
1c275381
MT
9627 imsm_set_array_size(dev, u->new_size);
9628 set_blocks_per_member(map, blocks_per_member);
9629 update_num_data_stripes(map, u->new_size);
f3871fdc
AK
9630 ret_val = 1;
9631 break;
9632 }
9633 }
9634
9635 return ret_val;
9636}
9637
69d40de4
JR
9638static int prepare_spare_to_activate(struct supertype *st,
9639 struct imsm_update_activate_spare *u)
9640{
9641 struct intel_super *super = st->sb;
9642 int prev_current_vol = super->current_vol;
9643 struct active_array *a;
9644 int ret = 1;
9645
9646 for (a = st->arrays; a; a = a->next)
9647 /*
9648 * Additional initialization (adding bitmap header, filling
9649 * the bitmap area with '1's to force initial rebuild for a whole
9650 * data-area) is required when adding the spare to the volume
9651 * with write-intent bitmap.
9652 */
9653 if (a->info.container_member == u->array &&
9654 a->info.consistency_policy == CONSISTENCY_POLICY_BITMAP) {
9655 struct dl *dl;
9656
9657 for (dl = super->disks; dl; dl = dl->next)
9658 if (dl == u->dl)
9659 break;
9660 if (!dl)
9661 break;
9662
9663 super->current_vol = u->array;
9664 if (st->ss->write_bitmap(st, dl->fd, NoUpdate))
9665 ret = 0;
9666 super->current_vol = prev_current_vol;
9667 }
9668 return ret;
9669}
9670
061d7da3 9671static int apply_update_activate_spare(struct imsm_update_activate_spare *u,
ca9de185 9672 struct intel_super *super,
061d7da3
LO
9673 struct active_array *active_array)
9674{
9675 struct imsm_super *mpb = super->anchor;
9676 struct imsm_dev *dev = get_imsm_dev(super, u->array);
238c0a71 9677 struct imsm_map *map = get_imsm_map(dev, MAP_0);
061d7da3
LO
9678 struct imsm_map *migr_map;
9679 struct active_array *a;
9680 struct imsm_disk *disk;
9681 __u8 to_state;
9682 struct dl *dl;
9683 unsigned int found;
9684 int failed;
5961eeec 9685 int victim;
061d7da3 9686 int i;
5961eeec 9687 int second_map_created = 0;
061d7da3 9688
5961eeec 9689 for (; u; u = u->next) {
238c0a71 9690 victim = get_imsm_disk_idx(dev, u->slot, MAP_X);
061d7da3 9691
5961eeec 9692 if (victim < 0)
9693 return 0;
061d7da3 9694
5961eeec 9695 for (dl = super->disks; dl; dl = dl->next)
9696 if (dl == u->dl)
9697 break;
061d7da3 9698
5961eeec 9699 if (!dl) {
7a862a02 9700 pr_err("error: imsm_activate_spare passed an unknown disk (index: %d)\n",
5961eeec 9701 u->dl->index);
9702 return 0;
9703 }
061d7da3 9704
5961eeec 9705 /* count failures (excluding rebuilds and the victim)
9706 * to determine map[0] state
9707 */
9708 failed = 0;
9709 for (i = 0; i < map->num_members; i++) {
9710 if (i == u->slot)
9711 continue;
9712 disk = get_imsm_disk(super,
238c0a71 9713 get_imsm_disk_idx(dev, i, MAP_X));
5961eeec 9714 if (!disk || is_failed(disk))
9715 failed++;
9716 }
061d7da3 9717
5961eeec 9718 /* adding a pristine spare, assign a new index */
9719 if (dl->index < 0) {
9720 dl->index = super->anchor->num_disks;
9721 super->anchor->num_disks++;
9722 }
9723 disk = &dl->disk;
9724 disk->status |= CONFIGURED_DISK;
9725 disk->status &= ~SPARE_DISK;
9726
9727 /* mark rebuild */
238c0a71 9728 to_state = imsm_check_degraded(super, dev, failed, MAP_0);
5961eeec 9729 if (!second_map_created) {
9730 second_map_created = 1;
9731 map->map_state = IMSM_T_STATE_DEGRADED;
9732 migrate(dev, super, to_state, MIGR_REBUILD);
9733 } else
9734 map->map_state = to_state;
238c0a71 9735 migr_map = get_imsm_map(dev, MAP_1);
5961eeec 9736 set_imsm_ord_tbl_ent(map, u->slot, dl->index);
9737 set_imsm_ord_tbl_ent(migr_map, u->slot,
9738 dl->index | IMSM_ORD_REBUILD);
9739
9740 /* update the family_num to mark a new container
9741 * generation, being careful to record the existing
9742 * family_num in orig_family_num to clean up after
9743 * earlier mdadm versions that neglected to set it.
9744 */
9745 if (mpb->orig_family_num == 0)
9746 mpb->orig_family_num = mpb->family_num;
9747 mpb->family_num += super->random;
9748
9749 /* count arrays using the victim in the metadata */
9750 found = 0;
9751 for (a = active_array; a ; a = a->next) {
76c152ca 9752 int dev_idx = a->info.container_member;
061d7da3 9753
76c152ca 9754 if (get_disk_slot_in_dev(super, dev_idx, victim) >= 0)
5961eeec 9755 found++;
9756 }
061d7da3 9757
5961eeec 9758 /* delete the victim if it is no longer being
9759 * utilized anywhere
061d7da3 9760 */
5961eeec 9761 if (!found) {
9762 struct dl **dlp;
061d7da3 9763
5961eeec 9764 /* We know that 'manager' isn't touching anything,
9765 * so it is safe to delete
9766 */
9767 for (dlp = &super->disks; *dlp; dlp = &(*dlp)->next)
061d7da3
LO
9768 if ((*dlp)->index == victim)
9769 break;
5961eeec 9770
9771 /* victim may be on the missing list */
9772 if (!*dlp)
9773 for (dlp = &super->missing; *dlp;
9774 dlp = &(*dlp)->next)
9775 if ((*dlp)->index == victim)
9776 break;
9777 imsm_delete(super, dlp, victim);
9778 }
061d7da3
LO
9779 }
9780
9781 return 1;
9782}
a29911da 9783
2e5dc010
N
9784static int apply_reshape_container_disks_update(struct imsm_update_reshape *u,
9785 struct intel_super *super,
9786 void ***space_list)
9787{
9788 struct dl *new_disk;
9789 struct intel_dev *id;
9790 int i;
9791 int delta_disks = u->new_raid_disks - u->old_raid_disks;
ee4beede 9792 int disk_count = u->old_raid_disks;
2e5dc010
N
9793 void **tofree = NULL;
9794 int devices_to_reshape = 1;
9795 struct imsm_super *mpb = super->anchor;
9796 int ret_val = 0;
d098291a 9797 unsigned int dev_id;
2e5dc010 9798
1ade5cc1 9799 dprintf("(enter)\n");
2e5dc010
N
9800
9801 /* enable spares to use in array */
9802 for (i = 0; i < delta_disks; i++) {
9803 new_disk = get_disk_super(super,
9804 major(u->new_disks[i]),
9805 minor(u->new_disks[i]));
7a862a02 9806 dprintf("imsm: new disk for reshape is: %i:%i (%p, index = %i)\n",
2e5dc010
N
9807 major(u->new_disks[i]), minor(u->new_disks[i]),
9808 new_disk, new_disk->index);
089f9d79
JS
9809 if (new_disk == NULL ||
9810 (new_disk->index >= 0 &&
9811 new_disk->index < u->old_raid_disks))
2e5dc010 9812 goto update_reshape_exit;
ee4beede 9813 new_disk->index = disk_count++;
2e5dc010
N
9814 /* slot to fill in autolayout
9815 */
9816 new_disk->raiddisk = new_disk->index;
9817 new_disk->disk.status |=
9818 CONFIGURED_DISK;
9819 new_disk->disk.status &= ~SPARE_DISK;
9820 }
9821
ed7333bd
AK
9822 dprintf("imsm: volume set mpb->num_raid_devs = %i\n",
9823 mpb->num_raid_devs);
2e5dc010
N
9824 /* manage changes in volume
9825 */
d098291a 9826 for (dev_id = 0; dev_id < mpb->num_raid_devs; dev_id++) {
2e5dc010
N
9827 void **sp = *space_list;
9828 struct imsm_dev *newdev;
9829 struct imsm_map *newmap, *oldmap;
9830
d098291a
AK
9831 for (id = super->devlist ; id; id = id->next) {
9832 if (id->index == dev_id)
9833 break;
9834 }
9835 if (id == NULL)
9836 break;
2e5dc010
N
9837 if (!sp)
9838 continue;
9839 *space_list = *sp;
9840 newdev = (void*)sp;
9841 /* Copy the dev, but not (all of) the map */
9842 memcpy(newdev, id->dev, sizeof(*newdev));
238c0a71
AK
9843 oldmap = get_imsm_map(id->dev, MAP_0);
9844 newmap = get_imsm_map(newdev, MAP_0);
2e5dc010
N
9845 /* Copy the current map */
9846 memcpy(newmap, oldmap, sizeof_imsm_map(oldmap));
9847 /* update one device only
9848 */
9849 if (devices_to_reshape) {
ed7333bd
AK
9850 dprintf("imsm: modifying subdev: %i\n",
9851 id->index);
2e5dc010
N
9852 devices_to_reshape--;
9853 newdev->vol.migr_state = 1;
4036e7ee 9854 set_vol_curr_migr_unit(newdev, 0);
ea672ee1 9855 set_migr_type(newdev, MIGR_GEN_MIGR);
2e5dc010
N
9856 newmap->num_members = u->new_raid_disks;
9857 for (i = 0; i < delta_disks; i++) {
9858 set_imsm_ord_tbl_ent(newmap,
9859 u->old_raid_disks + i,
9860 u->old_raid_disks + i);
9861 }
9862 /* New map is correct, now need to save old map
9863 */
238c0a71 9864 newmap = get_imsm_map(newdev, MAP_1);
2e5dc010
N
9865 memcpy(newmap, oldmap, sizeof_imsm_map(oldmap));
9866
f3871fdc 9867 imsm_set_array_size(newdev, -1);
2e5dc010
N
9868 }
9869
9870 sp = (void **)id->dev;
9871 id->dev = newdev;
9872 *sp = tofree;
9873 tofree = sp;
8e59f3d8
AK
9874
9875 /* Clear migration record */
9876 memset(super->migr_rec, 0, sizeof(struct migr_record));
2e5dc010 9877 }
819bc634
AK
9878 if (tofree)
9879 *space_list = tofree;
2e5dc010
N
9880 ret_val = 1;
9881
9882update_reshape_exit:
9883
9884 return ret_val;
9885}
9886
bb025c2f 9887static int apply_takeover_update(struct imsm_update_takeover *u,
8ca6df95
KW
9888 struct intel_super *super,
9889 void ***space_list)
bb025c2f
KW
9890{
9891 struct imsm_dev *dev = NULL;
8ca6df95
KW
9892 struct intel_dev *dv;
9893 struct imsm_dev *dev_new;
bb025c2f
KW
9894 struct imsm_map *map;
9895 struct dl *dm, *du;
8ca6df95 9896 int i;
bb025c2f
KW
9897
9898 for (dv = super->devlist; dv; dv = dv->next)
9899 if (dv->index == (unsigned int)u->subarray) {
9900 dev = dv->dev;
9901 break;
9902 }
9903
9904 if (dev == NULL)
9905 return 0;
9906
238c0a71 9907 map = get_imsm_map(dev, MAP_0);
bb025c2f
KW
9908
9909 if (u->direction == R10_TO_R0) {
43d5ec18 9910 /* Number of failed disks must be half of initial disk number */
3b451610
AK
9911 if (imsm_count_failed(super, dev, MAP_0) !=
9912 (map->num_members / 2))
43d5ec18
KW
9913 return 0;
9914
bb025c2f
KW
9915 /* iterate through devices to mark removed disks as spare */
9916 for (dm = super->disks; dm; dm = dm->next) {
9917 if (dm->disk.status & FAILED_DISK) {
9918 int idx = dm->index;
9919 /* update indexes on the disk list */
9920/* FIXME this loop-with-the-loop looks wrong, I'm not convinced
9921 the index values will end up being correct.... NB */
9922 for (du = super->disks; du; du = du->next)
9923 if (du->index > idx)
9924 du->index--;
9925 /* mark as spare disk */
a8619d23 9926 mark_spare(dm);
bb025c2f
KW
9927 }
9928 }
bb025c2f 9929 /* update map */
1c275381 9930 map->num_members /= map->num_domains;
bb025c2f 9931 map->map_state = IMSM_T_STATE_NORMAL;
bb025c2f 9932 map->raid_level = 0;
1c275381
MT
9933 set_num_domains(map);
9934 update_num_data_stripes(map, imsm_dev_size(dev));
bb025c2f
KW
9935 map->failed_disk_num = -1;
9936 }
9937
8ca6df95
KW
9938 if (u->direction == R0_TO_R10) {
9939 void **space;
4a353e6e 9940
8ca6df95
KW
9941 /* update slots in current disk list */
9942 for (dm = super->disks; dm; dm = dm->next) {
9943 if (dm->index >= 0)
9944 dm->index *= 2;
9945 }
9946 /* create new *missing* disks */
9947 for (i = 0; i < map->num_members; i++) {
9948 space = *space_list;
9949 if (!space)
9950 continue;
9951 *space_list = *space;
9952 du = (void *)space;
9953 memcpy(du, super->disks, sizeof(*du));
8ca6df95
KW
9954 du->fd = -1;
9955 du->minor = 0;
9956 du->major = 0;
9957 du->index = (i * 2) + 1;
9958 sprintf((char *)du->disk.serial,
9959 " MISSING_%d", du->index);
9960 sprintf((char *)du->serial,
9961 "MISSING_%d", du->index);
9962 du->next = super->missing;
9963 super->missing = du;
9964 }
9965 /* create new dev and map */
9966 space = *space_list;
9967 if (!space)
9968 return 0;
9969 *space_list = *space;
9970 dev_new = (void *)space;
9971 memcpy(dev_new, dev, sizeof(*dev));
9972 /* update new map */
238c0a71 9973 map = get_imsm_map(dev_new, MAP_0);
1c275381 9974
1a2487c2 9975 map->map_state = IMSM_T_STATE_DEGRADED;
8ca6df95 9976 map->raid_level = 1;
1c275381
MT
9977 set_num_domains(map);
9978 map->num_members = map->num_members * map->num_domains;
9979 update_num_data_stripes(map, imsm_dev_size(dev));
4a353e6e 9980
8ca6df95
KW
9981 /* replace dev<->dev_new */
9982 dv->dev = dev_new;
9983 }
bb025c2f
KW
9984 /* update disk order table */
9985 for (du = super->disks; du; du = du->next)
9986 if (du->index >= 0)
9987 set_imsm_ord_tbl_ent(map, du->index, du->index);
8ca6df95 9988 for (du = super->missing; du; du = du->next)
1a2487c2
KW
9989 if (du->index >= 0) {
9990 set_imsm_ord_tbl_ent(map, du->index, du->index);
4c9e8c1e 9991 mark_missing(super, dv->dev, &du->disk, du->index);
1a2487c2 9992 }
bb025c2f
KW
9993
9994 return 1;
9995}
9996
e8319a19
DW
9997static void imsm_process_update(struct supertype *st,
9998 struct metadata_update *update)
9999{
10000 /**
10001 * crack open the metadata_update envelope to find the update record
10002 * update can be one of:
d195167d
AK
10003 * update_reshape_container_disks - all the arrays in the container
10004 * are being reshaped to have more devices. We need to mark
10005 * the arrays for general migration and convert selected spares
10006 * into active devices.
10007 * update_activate_spare - a spare device has replaced a failed
1011e834
N
10008 * device in an array, update the disk_ord_tbl. If this disk is
10009 * present in all member arrays then also clear the SPARE_DISK
10010 * flag
d195167d
AK
10011 * update_create_array
10012 * update_kill_array
10013 * update_rename_array
10014 * update_add_remove_disk
e8319a19
DW
10015 */
10016 struct intel_super *super = st->sb;
4d7b1503 10017 struct imsm_super *mpb;
e8319a19
DW
10018 enum imsm_update_type type = *(enum imsm_update_type *) update->buf;
10019
4d7b1503
DW
10020 /* update requires a larger buf but the allocation failed */
10021 if (super->next_len && !super->next_buf) {
10022 super->next_len = 0;
10023 return;
10024 }
10025
10026 if (super->next_buf) {
10027 memcpy(super->next_buf, super->buf, super->len);
10028 free(super->buf);
10029 super->len = super->next_len;
10030 super->buf = super->next_buf;
10031
10032 super->next_len = 0;
10033 super->next_buf = NULL;
10034 }
10035
10036 mpb = super->anchor;
10037
e8319a19 10038 switch (type) {
0ec5d470
AK
10039 case update_general_migration_checkpoint: {
10040 struct intel_dev *id;
10041 struct imsm_update_general_migration_checkpoint *u =
10042 (void *)update->buf;
10043
1ade5cc1 10044 dprintf("called for update_general_migration_checkpoint\n");
0ec5d470
AK
10045
10046 /* find device under general migration */
10047 for (id = super->devlist ; id; id = id->next) {
10048 if (is_gen_migration(id->dev)) {
4036e7ee
MT
10049 set_vol_curr_migr_unit(id->dev,
10050 u->curr_migr_unit);
0ec5d470
AK
10051 super->updates_pending++;
10052 }
10053 }
10054 break;
10055 }
bb025c2f
KW
10056 case update_takeover: {
10057 struct imsm_update_takeover *u = (void *)update->buf;
1a2487c2
KW
10058 if (apply_takeover_update(u, super, &update->space_list)) {
10059 imsm_update_version_info(super);
bb025c2f 10060 super->updates_pending++;
1a2487c2 10061 }
bb025c2f
KW
10062 break;
10063 }
10064
78b10e66 10065 case update_reshape_container_disks: {
d195167d 10066 struct imsm_update_reshape *u = (void *)update->buf;
2e5dc010
N
10067 if (apply_reshape_container_disks_update(
10068 u, super, &update->space_list))
10069 super->updates_pending++;
78b10e66
N
10070 break;
10071 }
48c5303a 10072 case update_reshape_migration: {
a29911da
PC
10073 struct imsm_update_reshape_migration *u = (void *)update->buf;
10074 if (apply_reshape_migration_update(
10075 u, super, &update->space_list))
10076 super->updates_pending++;
48c5303a
PC
10077 break;
10078 }
f3871fdc
AK
10079 case update_size_change: {
10080 struct imsm_update_size_change *u = (void *)update->buf;
10081 if (apply_size_change_update(u, super))
10082 super->updates_pending++;
10083 break;
10084 }
e8319a19 10085 case update_activate_spare: {
1011e834 10086 struct imsm_update_activate_spare *u = (void *) update->buf;
69d40de4
JR
10087
10088 if (prepare_spare_to_activate(st, u) &&
10089 apply_update_activate_spare(u, super, st->arrays))
061d7da3 10090 super->updates_pending++;
8273f55e
DW
10091 break;
10092 }
10093 case update_create_array: {
10094 /* someone wants to create a new array, we need to be aware of
10095 * a few races/collisions:
10096 * 1/ 'Create' called by two separate instances of mdadm
10097 * 2/ 'Create' versus 'activate_spare': mdadm has chosen
10098 * devices that have since been assimilated via
10099 * activate_spare.
10100 * In the event this update can not be carried out mdadm will
10101 * (FIX ME) notice that its update did not take hold.
10102 */
10103 struct imsm_update_create_array *u = (void *) update->buf;
ba2de7ba 10104 struct intel_dev *dv;
8273f55e
DW
10105 struct imsm_dev *dev;
10106 struct imsm_map *map, *new_map;
10107 unsigned long long start, end;
10108 unsigned long long new_start, new_end;
10109 int i;
54c2c1ea
DW
10110 struct disk_info *inf;
10111 struct dl *dl;
8273f55e
DW
10112
10113 /* handle racing creates: first come first serve */
10114 if (u->dev_idx < mpb->num_raid_devs) {
1ade5cc1 10115 dprintf("subarray %d already defined\n", u->dev_idx);
ba2de7ba 10116 goto create_error;
8273f55e
DW
10117 }
10118
10119 /* check update is next in sequence */
10120 if (u->dev_idx != mpb->num_raid_devs) {
1ade5cc1
N
10121 dprintf("can not create array %d expected index %d\n",
10122 u->dev_idx, mpb->num_raid_devs);
ba2de7ba 10123 goto create_error;
8273f55e
DW
10124 }
10125
238c0a71 10126 new_map = get_imsm_map(&u->dev, MAP_0);
5551b113 10127 new_start = pba_of_lba0(new_map);
44490938 10128 new_end = new_start + per_dev_array_size(new_map);
54c2c1ea 10129 inf = get_disk_info(u);
8273f55e
DW
10130
10131 /* handle activate_spare versus create race:
10132 * check to make sure that overlapping arrays do not include
10133 * overalpping disks
10134 */
10135 for (i = 0; i < mpb->num_raid_devs; i++) {
949c47a0 10136 dev = get_imsm_dev(super, i);
238c0a71 10137 map = get_imsm_map(dev, MAP_0);
5551b113 10138 start = pba_of_lba0(map);
44490938 10139 end = start + per_dev_array_size(map);
8273f55e
DW
10140 if ((new_start >= start && new_start <= end) ||
10141 (start >= new_start && start <= new_end))
54c2c1ea
DW
10142 /* overlap */;
10143 else
10144 continue;
10145
10146 if (disks_overlap(super, i, u)) {
1ade5cc1 10147 dprintf("arrays overlap\n");
ba2de7ba 10148 goto create_error;
8273f55e
DW
10149 }
10150 }
8273f55e 10151
949c47a0
DW
10152 /* check that prepare update was successful */
10153 if (!update->space) {
1ade5cc1 10154 dprintf("prepare update failed\n");
ba2de7ba 10155 goto create_error;
949c47a0
DW
10156 }
10157
54c2c1ea
DW
10158 /* check that all disks are still active before committing
10159 * changes. FIXME: could we instead handle this by creating a
10160 * degraded array? That's probably not what the user expects,
10161 * so better to drop this update on the floor.
10162 */
10163 for (i = 0; i < new_map->num_members; i++) {
10164 dl = serial_to_dl(inf[i].serial, super);
10165 if (!dl) {
1ade5cc1 10166 dprintf("disk disappeared\n");
ba2de7ba 10167 goto create_error;
54c2c1ea 10168 }
949c47a0
DW
10169 }
10170
8273f55e 10171 super->updates_pending++;
54c2c1ea
DW
10172
10173 /* convert spares to members and fixup ord_tbl */
10174 for (i = 0; i < new_map->num_members; i++) {
10175 dl = serial_to_dl(inf[i].serial, super);
10176 if (dl->index == -1) {
10177 dl->index = mpb->num_disks;
10178 mpb->num_disks++;
10179 dl->disk.status |= CONFIGURED_DISK;
10180 dl->disk.status &= ~SPARE_DISK;
10181 }
10182 set_imsm_ord_tbl_ent(new_map, i, dl->index);
10183 }
10184
ba2de7ba
DW
10185 dv = update->space;
10186 dev = dv->dev;
949c47a0
DW
10187 update->space = NULL;
10188 imsm_copy_dev(dev, &u->dev);
ba2de7ba
DW
10189 dv->index = u->dev_idx;
10190 dv->next = super->devlist;
10191 super->devlist = dv;
8273f55e 10192 mpb->num_raid_devs++;
8273f55e 10193
4d1313e9 10194 imsm_update_version_info(super);
8273f55e 10195 break;
ba2de7ba
DW
10196 create_error:
10197 /* mdmon knows how to release update->space, but not
10198 * ((struct intel_dev *) update->space)->dev
10199 */
10200 if (update->space) {
10201 dv = update->space;
10202 free(dv->dev);
10203 }
8273f55e 10204 break;
e8319a19 10205 }
33414a01
DW
10206 case update_kill_array: {
10207 struct imsm_update_kill_array *u = (void *) update->buf;
10208 int victim = u->dev_idx;
10209 struct active_array *a;
10210 struct intel_dev **dp;
33414a01
DW
10211
10212 /* sanity check that we are not affecting the uuid of
10213 * active arrays, or deleting an active array
10214 *
10215 * FIXME when immutable ids are available, but note that
10216 * we'll also need to fixup the invalidated/active
10217 * subarray indexes in mdstat
10218 */
10219 for (a = st->arrays; a; a = a->next)
10220 if (a->info.container_member >= victim)
10221 break;
10222 /* by definition if mdmon is running at least one array
10223 * is active in the container, so checking
10224 * mpb->num_raid_devs is just extra paranoia
10225 */
756a15f3 10226 if (a || mpb->num_raid_devs == 1 || victim >= super->anchor->num_raid_devs) {
33414a01
DW
10227 dprintf("failed to delete subarray-%d\n", victim);
10228 break;
10229 }
10230
10231 for (dp = &super->devlist; *dp;)
f21e18ca 10232 if ((*dp)->index == (unsigned)super->current_vol) {
33414a01
DW
10233 *dp = (*dp)->next;
10234 } else {
f21e18ca 10235 if ((*dp)->index > (unsigned)victim)
33414a01
DW
10236 (*dp)->index--;
10237 dp = &(*dp)->next;
10238 }
10239 mpb->num_raid_devs--;
10240 super->updates_pending++;
10241 break;
10242 }
aa534678
DW
10243 case update_rename_array: {
10244 struct imsm_update_rename_array *u = (void *) update->buf;
10245 char name[MAX_RAID_SERIAL_LEN+1];
10246 int target = u->dev_idx;
10247 struct active_array *a;
10248 struct imsm_dev *dev;
10249
10250 /* sanity check that we are not affecting the uuid of
10251 * an active array
10252 */
40659392 10253 memset(name, 0, sizeof(name));
aa534678
DW
10254 snprintf(name, MAX_RAID_SERIAL_LEN, "%s", (char *) u->name);
10255 name[MAX_RAID_SERIAL_LEN] = '\0';
10256 for (a = st->arrays; a; a = a->next)
10257 if (a->info.container_member == target)
10258 break;
10259 dev = get_imsm_dev(super, u->dev_idx);
e2eb503b
MT
10260
10261 if (a || !dev || imsm_is_name_allowed(super, name, 0) == false) {
aa534678
DW
10262 dprintf("failed to rename subarray-%d\n", target);
10263 break;
10264 }
10265
40659392 10266 memcpy(dev->volume, name, MAX_RAID_SERIAL_LEN);
aa534678
DW
10267 super->updates_pending++;
10268 break;
10269 }
1a64be56 10270 case update_add_remove_disk: {
43dad3d6 10271 /* we may be able to repair some arrays if disks are
095b8088 10272 * being added, check the status of add_remove_disk
1a64be56
LM
10273 * if discs has been added.
10274 */
10275 if (add_remove_disk_update(super)) {
43dad3d6 10276 struct active_array *a;
072b727f
DW
10277
10278 super->updates_pending++;
1a64be56 10279 for (a = st->arrays; a; a = a->next)
43dad3d6
DW
10280 a->check_degraded = 1;
10281 }
43dad3d6 10282 break;
e8319a19 10283 }
bbab0940
TM
10284 case update_prealloc_badblocks_mem:
10285 break;
e6e9dd3f
AP
10286 case update_rwh_policy: {
10287 struct imsm_update_rwh_policy *u = (void *)update->buf;
10288 int target = u->dev_idx;
10289 struct imsm_dev *dev = get_imsm_dev(super, target);
e6e9dd3f
AP
10290
10291 if (dev->rwh_policy != u->new_policy) {
10292 dev->rwh_policy = u->new_policy;
10293 super->updates_pending++;
10294 }
10295 break;
10296 }
1a64be56 10297 default:
ebf3be99 10298 pr_err("error: unsupported process update type:(type: %d)\n", type);
1a64be56 10299 }
e8319a19 10300}
88758e9d 10301
bc0b9d34
PC
10302static struct mdinfo *get_spares_for_grow(struct supertype *st);
10303
5fe6f031
N
10304static int imsm_prepare_update(struct supertype *st,
10305 struct metadata_update *update)
8273f55e 10306{
949c47a0 10307 /**
4d7b1503
DW
10308 * Allocate space to hold new disk entries, raid-device entries or a new
10309 * mpb if necessary. The manager synchronously waits for updates to
10310 * complete in the monitor, so new mpb buffers allocated here can be
10311 * integrated by the monitor thread without worrying about live pointers
10312 * in the manager thread.
8273f55e 10313 */
095b8088 10314 enum imsm_update_type type;
4d7b1503 10315 struct intel_super *super = st->sb;
f36a9ecd 10316 unsigned int sector_size = super->sector_size;
4d7b1503
DW
10317 struct imsm_super *mpb = super->anchor;
10318 size_t buf_len;
10319 size_t len = 0;
949c47a0 10320
095b8088
N
10321 if (update->len < (int)sizeof(type))
10322 return 0;
10323
10324 type = *(enum imsm_update_type *) update->buf;
10325
949c47a0 10326 switch (type) {
0ec5d470 10327 case update_general_migration_checkpoint:
095b8088
N
10328 if (update->len < (int)sizeof(struct imsm_update_general_migration_checkpoint))
10329 return 0;
1ade5cc1 10330 dprintf("called for update_general_migration_checkpoint\n");
0ec5d470 10331 break;
abedf5fc
KW
10332 case update_takeover: {
10333 struct imsm_update_takeover *u = (void *)update->buf;
095b8088
N
10334 if (update->len < (int)sizeof(*u))
10335 return 0;
abedf5fc
KW
10336 if (u->direction == R0_TO_R10) {
10337 void **tail = (void **)&update->space_list;
10338 struct imsm_dev *dev = get_imsm_dev(super, u->subarray);
238c0a71 10339 struct imsm_map *map = get_imsm_map(dev, MAP_0);
abedf5fc
KW
10340 int num_members = map->num_members;
10341 void *space;
10342 int size, i;
abedf5fc
KW
10343 /* allocate memory for added disks */
10344 for (i = 0; i < num_members; i++) {
10345 size = sizeof(struct dl);
503975b9 10346 space = xmalloc(size);
abedf5fc
KW
10347 *tail = space;
10348 tail = space;
10349 *tail = NULL;
10350 }
10351 /* allocate memory for new device */
10352 size = sizeof_imsm_dev(super->devlist->dev, 0) +
10353 (num_members * sizeof(__u32));
503975b9
N
10354 space = xmalloc(size);
10355 *tail = space;
10356 tail = space;
10357 *tail = NULL;
10358 len = disks_to_mpb_size(num_members * 2);
abedf5fc
KW
10359 }
10360
10361 break;
10362 }
78b10e66 10363 case update_reshape_container_disks: {
d195167d
AK
10364 /* Every raid device in the container is about to
10365 * gain some more devices, and we will enter a
10366 * reconfiguration.
10367 * So each 'imsm_map' will be bigger, and the imsm_vol
10368 * will now hold 2 of them.
10369 * Thus we need new 'struct imsm_dev' allocations sized
10370 * as sizeof_imsm_dev but with more devices in both maps.
10371 */
10372 struct imsm_update_reshape *u = (void *)update->buf;
10373 struct intel_dev *dl;
10374 void **space_tail = (void**)&update->space_list;
10375
095b8088
N
10376 if (update->len < (int)sizeof(*u))
10377 return 0;
10378
1ade5cc1 10379 dprintf("for update_reshape\n");
d195167d
AK
10380
10381 for (dl = super->devlist; dl; dl = dl->next) {
10382 int size = sizeof_imsm_dev(dl->dev, 1);
10383 void *s;
d677e0b8
AK
10384 if (u->new_raid_disks > u->old_raid_disks)
10385 size += sizeof(__u32)*2*
10386 (u->new_raid_disks - u->old_raid_disks);
503975b9 10387 s = xmalloc(size);
d195167d
AK
10388 *space_tail = s;
10389 space_tail = s;
10390 *space_tail = NULL;
10391 }
10392
10393 len = disks_to_mpb_size(u->new_raid_disks);
10394 dprintf("New anchor length is %llu\n", (unsigned long long)len);
78b10e66
N
10395 break;
10396 }
48c5303a 10397 case update_reshape_migration: {
bc0b9d34
PC
10398 /* for migration level 0->5 we need to add disks
10399 * so the same as for container operation we will copy
10400 * device to the bigger location.
10401 * in memory prepared device and new disk area are prepared
10402 * for usage in process update
10403 */
10404 struct imsm_update_reshape_migration *u = (void *)update->buf;
10405 struct intel_dev *id;
10406 void **space_tail = (void **)&update->space_list;
10407 int size;
10408 void *s;
10409 int current_level = -1;
10410
095b8088
N
10411 if (update->len < (int)sizeof(*u))
10412 return 0;
10413
1ade5cc1 10414 dprintf("for update_reshape\n");
bc0b9d34
PC
10415
10416 /* add space for bigger array in update
10417 */
10418 for (id = super->devlist; id; id = id->next) {
10419 if (id->index == (unsigned)u->subdev) {
10420 size = sizeof_imsm_dev(id->dev, 1);
10421 if (u->new_raid_disks > u->old_raid_disks)
10422 size += sizeof(__u32)*2*
10423 (u->new_raid_disks - u->old_raid_disks);
503975b9 10424 s = xmalloc(size);
bc0b9d34
PC
10425 *space_tail = s;
10426 space_tail = s;
10427 *space_tail = NULL;
10428 break;
10429 }
10430 }
10431 if (update->space_list == NULL)
10432 break;
10433
10434 /* add space for disk in update
10435 */
10436 size = sizeof(struct dl);
503975b9 10437 s = xmalloc(size);
bc0b9d34
PC
10438 *space_tail = s;
10439 space_tail = s;
10440 *space_tail = NULL;
10441
10442 /* add spare device to update
10443 */
10444 for (id = super->devlist ; id; id = id->next)
10445 if (id->index == (unsigned)u->subdev) {
10446 struct imsm_dev *dev;
10447 struct imsm_map *map;
10448
10449 dev = get_imsm_dev(super, u->subdev);
238c0a71 10450 map = get_imsm_map(dev, MAP_0);
bc0b9d34
PC
10451 current_level = map->raid_level;
10452 break;
10453 }
089f9d79 10454 if (u->new_level == 5 && u->new_level != current_level) {
bc0b9d34
PC
10455 struct mdinfo *spares;
10456
10457 spares = get_spares_for_grow(st);
10458 if (spares) {
10459 struct dl *dl;
10460 struct mdinfo *dev;
10461
10462 dev = spares->devs;
10463 if (dev) {
10464 u->new_disks[0] =
10465 makedev(dev->disk.major,
10466 dev->disk.minor);
10467 dl = get_disk_super(super,
10468 dev->disk.major,
10469 dev->disk.minor);
10470 dl->index = u->old_raid_disks;
10471 dev = dev->next;
10472 }
10473 sysfs_free(spares);
10474 }
10475 }
10476 len = disks_to_mpb_size(u->new_raid_disks);
10477 dprintf("New anchor length is %llu\n", (unsigned long long)len);
48c5303a
PC
10478 break;
10479 }
f3871fdc 10480 case update_size_change: {
095b8088
N
10481 if (update->len < (int)sizeof(struct imsm_update_size_change))
10482 return 0;
10483 break;
10484 }
10485 case update_activate_spare: {
10486 if (update->len < (int)sizeof(struct imsm_update_activate_spare))
10487 return 0;
f3871fdc
AK
10488 break;
10489 }
949c47a0
DW
10490 case update_create_array: {
10491 struct imsm_update_create_array *u = (void *) update->buf;
ba2de7ba 10492 struct intel_dev *dv;
54c2c1ea 10493 struct imsm_dev *dev = &u->dev;
238c0a71 10494 struct imsm_map *map = get_imsm_map(dev, MAP_0);
54c2c1ea
DW
10495 struct dl *dl;
10496 struct disk_info *inf;
10497 int i;
10498 int activate = 0;
949c47a0 10499
095b8088
N
10500 if (update->len < (int)sizeof(*u))
10501 return 0;
10502
54c2c1ea
DW
10503 inf = get_disk_info(u);
10504 len = sizeof_imsm_dev(dev, 1);
ba2de7ba 10505 /* allocate a new super->devlist entry */
503975b9
N
10506 dv = xmalloc(sizeof(*dv));
10507 dv->dev = xmalloc(len);
10508 update->space = dv;
949c47a0 10509
54c2c1ea
DW
10510 /* count how many spares will be converted to members */
10511 for (i = 0; i < map->num_members; i++) {
10512 dl = serial_to_dl(inf[i].serial, super);
10513 if (!dl) {
10514 /* hmm maybe it failed?, nothing we can do about
10515 * it here
10516 */
10517 continue;
10518 }
10519 if (count_memberships(dl, super) == 0)
10520 activate++;
10521 }
10522 len += activate * sizeof(struct imsm_disk);
949c47a0 10523 break;
095b8088
N
10524 }
10525 case update_kill_array: {
10526 if (update->len < (int)sizeof(struct imsm_update_kill_array))
10527 return 0;
949c47a0
DW
10528 break;
10529 }
095b8088
N
10530 case update_rename_array: {
10531 if (update->len < (int)sizeof(struct imsm_update_rename_array))
10532 return 0;
10533 break;
10534 }
10535 case update_add_remove_disk:
10536 /* no update->len needed */
10537 break;
bbab0940
TM
10538 case update_prealloc_badblocks_mem:
10539 super->extra_space += sizeof(struct bbm_log) -
10540 get_imsm_bbm_log_size(super->bbm_log);
10541 break;
e6e9dd3f
AP
10542 case update_rwh_policy: {
10543 if (update->len < (int)sizeof(struct imsm_update_rwh_policy))
10544 return 0;
10545 break;
10546 }
095b8088
N
10547 default:
10548 return 0;
949c47a0 10549 }
8273f55e 10550
4d7b1503
DW
10551 /* check if we need a larger metadata buffer */
10552 if (super->next_buf)
10553 buf_len = super->next_len;
10554 else
10555 buf_len = super->len;
10556
bbab0940 10557 if (__le32_to_cpu(mpb->mpb_size) + super->extra_space + len > buf_len) {
4d7b1503
DW
10558 /* ok we need a larger buf than what is currently allocated
10559 * if this allocation fails process_update will notice that
10560 * ->next_len is set and ->next_buf is NULL
10561 */
bbab0940
TM
10562 buf_len = ROUND_UP(__le32_to_cpu(mpb->mpb_size) +
10563 super->extra_space + len, sector_size);
4d7b1503
DW
10564 if (super->next_buf)
10565 free(super->next_buf);
10566
10567 super->next_len = buf_len;
f36a9ecd 10568 if (posix_memalign(&super->next_buf, sector_size, buf_len) == 0)
1f45a8ad
DW
10569 memset(super->next_buf, 0, buf_len);
10570 else
4d7b1503
DW
10571 super->next_buf = NULL;
10572 }
5fe6f031 10573 return 1;
8273f55e
DW
10574}
10575
ae6aad82 10576/* must be called while manager is quiesced */
f21e18ca 10577static void imsm_delete(struct intel_super *super, struct dl **dlp, unsigned index)
ae6aad82
DW
10578{
10579 struct imsm_super *mpb = super->anchor;
ae6aad82
DW
10580 struct dl *iter;
10581 struct imsm_dev *dev;
10582 struct imsm_map *map;
4c9e8c1e 10583 unsigned int i, j, num_members;
fb12a745 10584 __u32 ord, ord_map0;
4c9e8c1e 10585 struct bbm_log *log = super->bbm_log;
ae6aad82 10586
1ade5cc1 10587 dprintf("deleting device[%d] from imsm_super\n", index);
ae6aad82
DW
10588
10589 /* shift all indexes down one */
10590 for (iter = super->disks; iter; iter = iter->next)
f21e18ca 10591 if (iter->index > (int)index)
ae6aad82 10592 iter->index--;
47ee5a45 10593 for (iter = super->missing; iter; iter = iter->next)
f21e18ca 10594 if (iter->index > (int)index)
47ee5a45 10595 iter->index--;
ae6aad82
DW
10596
10597 for (i = 0; i < mpb->num_raid_devs; i++) {
10598 dev = get_imsm_dev(super, i);
238c0a71 10599 map = get_imsm_map(dev, MAP_0);
24565c9a
DW
10600 num_members = map->num_members;
10601 for (j = 0; j < num_members; j++) {
10602 /* update ord entries being careful not to propagate
10603 * ord-flags to the first map
10604 */
238c0a71 10605 ord = get_imsm_ord_tbl_ent(dev, j, MAP_X);
fb12a745 10606 ord_map0 = get_imsm_ord_tbl_ent(dev, j, MAP_0);
ae6aad82 10607
24565c9a
DW
10608 if (ord_to_idx(ord) <= index)
10609 continue;
ae6aad82 10610
238c0a71 10611 map = get_imsm_map(dev, MAP_0);
fb12a745 10612 set_imsm_ord_tbl_ent(map, j, ord_map0 - 1);
238c0a71 10613 map = get_imsm_map(dev, MAP_1);
24565c9a
DW
10614 if (map)
10615 set_imsm_ord_tbl_ent(map, j, ord - 1);
ae6aad82
DW
10616 }
10617 }
10618
4c9e8c1e
TM
10619 for (i = 0; i < log->entry_count; i++) {
10620 struct bbm_log_entry *entry = &log->marked_block_entries[i];
10621
10622 if (entry->disk_ordinal <= index)
10623 continue;
10624 entry->disk_ordinal--;
10625 }
10626
ae6aad82
DW
10627 mpb->num_disks--;
10628 super->updates_pending++;
24565c9a
DW
10629 if (*dlp) {
10630 struct dl *dl = *dlp;
10631
10632 *dlp = (*dlp)->next;
3a85bf0e 10633 __free_imsm_disk(dl, 1);
24565c9a 10634 }
ae6aad82 10635}
9a717282 10636
9a717282
AK
10637static int imsm_get_allowed_degradation(int level, int raid_disks,
10638 struct intel_super *super,
10639 struct imsm_dev *dev)
10640{
10641 switch (level) {
bf5cf7c7 10642 case 1:
9a717282
AK
10643 case 10:{
10644 int ret_val = 0;
10645 struct imsm_map *map;
10646 int i;
10647
10648 ret_val = raid_disks/2;
10649 /* check map if all disks pairs not failed
10650 * in both maps
10651 */
238c0a71 10652 map = get_imsm_map(dev, MAP_0);
9a717282
AK
10653 for (i = 0; i < ret_val; i++) {
10654 int degradation = 0;
10655 if (get_imsm_disk(super, i) == NULL)
10656 degradation++;
10657 if (get_imsm_disk(super, i + 1) == NULL)
10658 degradation++;
10659 if (degradation == 2)
10660 return 0;
10661 }
238c0a71 10662 map = get_imsm_map(dev, MAP_1);
9a717282
AK
10663 /* if there is no second map
10664 * result can be returned
10665 */
10666 if (map == NULL)
10667 return ret_val;
10668 /* check degradation in second map
10669 */
10670 for (i = 0; i < ret_val; i++) {
10671 int degradation = 0;
10672 if (get_imsm_disk(super, i) == NULL)
10673 degradation++;
10674 if (get_imsm_disk(super, i + 1) == NULL)
10675 degradation++;
10676 if (degradation == 2)
10677 return 0;
10678 }
10679 return ret_val;
10680 }
10681 case 5:
10682 return 1;
10683 case 6:
10684 return 2;
10685 default:
10686 return 0;
10687 }
10688}
10689
d31ad643
PB
10690/*******************************************************************************
10691 * Function: validate_container_imsm
10692 * Description: This routine validates container after assemble,
10693 * eg. if devices in container are under the same controller.
10694 *
10695 * Parameters:
10696 * info : linked list with info about devices used in array
10697 * Returns:
10698 * 1 : HBA mismatch
10699 * 0 : Success
10700 ******************************************************************************/
10701int validate_container_imsm(struct mdinfo *info)
10702{
420dafcd 10703 if (check_no_platform())
6b781d33 10704 return 0;
d31ad643 10705
6b781d33
AP
10706 struct sys_dev *idev;
10707 struct sys_dev *hba = NULL;
10708 struct sys_dev *intel_devices = find_intel_devices();
10709 char *dev_path = devt_to_devpath(makedev(info->disk.major,
7c798f87 10710 info->disk.minor), 1, NULL);
6b781d33
AP
10711
10712 for (idev = intel_devices; idev; idev = idev->next) {
10713 if (dev_path && strstr(dev_path, idev->path)) {
10714 hba = idev;
10715 break;
d31ad643 10716 }
6b781d33
AP
10717 }
10718 if (dev_path)
d31ad643
PB
10719 free(dev_path);
10720
6b781d33
AP
10721 if (!hba) {
10722 pr_err("WARNING - Cannot detect HBA for device %s!\n",
10723 devid2kname(makedev(info->disk.major, info->disk.minor)));
10724 return 1;
10725 }
10726
10727 const struct imsm_orom *orom = get_orom_by_device_id(hba->dev_id);
10728 struct mdinfo *dev;
10729
10730 for (dev = info->next; dev; dev = dev->next) {
7c798f87
MT
10731 dev_path = devt_to_devpath(makedev(dev->disk.major,
10732 dev->disk.minor), 1, NULL);
6b781d33
AP
10733
10734 struct sys_dev *hba2 = NULL;
10735 for (idev = intel_devices; idev; idev = idev->next) {
10736 if (dev_path && strstr(dev_path, idev->path)) {
10737 hba2 = idev;
10738 break;
d31ad643
PB
10739 }
10740 }
6b781d33
AP
10741 if (dev_path)
10742 free(dev_path);
10743
10744 const struct imsm_orom *orom2 = hba2 == NULL ? NULL :
10745 get_orom_by_device_id(hba2->dev_id);
10746
10747 if (hba2 && hba->type != hba2->type) {
10748 pr_err("WARNING - HBAs of devices do not match %s != %s\n",
10749 get_sys_dev_type(hba->type), get_sys_dev_type(hba2->type));
10750 return 1;
10751 }
10752
07cb1e57 10753 if (orom != orom2) {
6b781d33
AP
10754 pr_err("WARNING - IMSM container assembled with disks under different HBAs!\n"
10755 " This operation is not supported and can lead to data loss.\n");
10756 return 1;
10757 }
10758
10759 if (!orom) {
10760 pr_err("WARNING - IMSM container assembled with disks under HBAs without IMSM platform support!\n"
10761 " This operation is not supported and can lead to data loss.\n");
10762 return 1;
10763 }
d31ad643 10764 }
6b781d33 10765
d31ad643
PB
10766 return 0;
10767}
32141c17 10768
6f50473f
TM
10769/*******************************************************************************
10770* Function: imsm_record_badblock
10771* Description: This routine stores new bad block record in BBM log
10772*
10773* Parameters:
10774* a : array containing a bad block
10775* slot : disk number containing a bad block
10776* sector : bad block sector
10777* length : bad block sectors range
10778* Returns:
10779* 1 : Success
10780* 0 : Error
10781******************************************************************************/
10782static int imsm_record_badblock(struct active_array *a, int slot,
10783 unsigned long long sector, int length)
10784{
10785 struct intel_super *super = a->container->sb;
10786 int ord;
10787 int ret;
10788
10789 ord = imsm_disk_slot_to_ord(a, slot);
10790 if (ord < 0)
10791 return 0;
10792
10793 ret = record_new_badblock(super->bbm_log, ord_to_idx(ord), sector,
10794 length);
10795 if (ret)
10796 super->updates_pending++;
10797
10798 return ret;
10799}
c07a5a4f
TM
10800/*******************************************************************************
10801* Function: imsm_clear_badblock
10802* Description: This routine clears bad block record from BBM log
10803*
10804* Parameters:
10805* a : array containing a bad block
10806* slot : disk number containing a bad block
10807* sector : bad block sector
10808* length : bad block sectors range
10809* Returns:
10810* 1 : Success
10811* 0 : Error
10812******************************************************************************/
10813static int imsm_clear_badblock(struct active_array *a, int slot,
10814 unsigned long long sector, int length)
10815{
10816 struct intel_super *super = a->container->sb;
10817 int ord;
10818 int ret;
10819
10820 ord = imsm_disk_slot_to_ord(a, slot);
10821 if (ord < 0)
10822 return 0;
10823
10824 ret = clear_badblock(super->bbm_log, ord_to_idx(ord), sector, length);
10825 if (ret)
10826 super->updates_pending++;
10827
10828 return ret;
10829}
928f1424
TM
10830/*******************************************************************************
10831* Function: imsm_get_badblocks
10832* Description: This routine get list of bad blocks for an array
10833*
10834* Parameters:
10835* a : array
10836* slot : disk number
10837* Returns:
10838* bb : structure containing bad blocks
10839* NULL : error
10840******************************************************************************/
10841static struct md_bb *imsm_get_badblocks(struct active_array *a, int slot)
10842{
10843 int inst = a->info.container_member;
10844 struct intel_super *super = a->container->sb;
10845 struct imsm_dev *dev = get_imsm_dev(super, inst);
10846 struct imsm_map *map = get_imsm_map(dev, MAP_0);
10847 int ord;
10848
10849 ord = imsm_disk_slot_to_ord(a, slot);
10850 if (ord < 0)
10851 return NULL;
10852
10853 get_volume_badblocks(super->bbm_log, ord_to_idx(ord), pba_of_lba0(map),
44490938 10854 per_dev_array_size(map), &super->bb);
928f1424
TM
10855
10856 return &super->bb;
10857}
27156a57
TM
10858/*******************************************************************************
10859* Function: examine_badblocks_imsm
10860* Description: Prints list of bad blocks on a disk to the standard output
10861*
10862* Parameters:
10863* st : metadata handler
10864* fd : open file descriptor for device
10865* devname : device name
10866* Returns:
10867* 0 : Success
10868* 1 : Error
10869******************************************************************************/
10870static int examine_badblocks_imsm(struct supertype *st, int fd, char *devname)
10871{
10872 struct intel_super *super = st->sb;
10873 struct bbm_log *log = super->bbm_log;
10874 struct dl *d = NULL;
10875 int any = 0;
10876
10877 for (d = super->disks; d ; d = d->next) {
10878 if (strcmp(d->devname, devname) == 0)
10879 break;
10880 }
10881
10882 if ((d == NULL) || (d->index < 0)) { /* serial mismatch probably */
10883 pr_err("%s doesn't appear to be part of a raid array\n",
10884 devname);
10885 return 1;
10886 }
10887
10888 if (log != NULL) {
10889 unsigned int i;
10890 struct bbm_log_entry *entry = &log->marked_block_entries[0];
10891
10892 for (i = 0; i < log->entry_count; i++) {
10893 if (entry[i].disk_ordinal == d->index) {
10894 unsigned long long sector = __le48_to_cpu(
10895 &entry[i].defective_block_start);
10896 int cnt = entry[i].marked_count + 1;
10897
10898 if (!any) {
10899 printf("Bad-blocks on %s:\n", devname);
10900 any = 1;
10901 }
10902
10903 printf("%20llu for %d sectors\n", sector, cnt);
10904 }
10905 }
10906 }
10907
10908 if (!any)
10909 printf("No bad-blocks list configured on %s\n", devname);
10910
10911 return 0;
10912}
687629c2
AK
10913/*******************************************************************************
10914 * Function: init_migr_record_imsm
10915 * Description: Function inits imsm migration record
10916 * Parameters:
10917 * super : imsm internal array info
10918 * dev : device under migration
10919 * info : general array info to find the smallest device
10920 * Returns:
10921 * none
10922 ******************************************************************************/
10923void init_migr_record_imsm(struct supertype *st, struct imsm_dev *dev,
10924 struct mdinfo *info)
10925{
10926 struct intel_super *super = st->sb;
10927 struct migr_record *migr_rec = super->migr_rec;
10928 int new_data_disks;
10929 unsigned long long dsize, dev_sectors;
10930 long long unsigned min_dev_sectors = -1LLU;
238c0a71
AK
10931 struct imsm_map *map_dest = get_imsm_map(dev, MAP_0);
10932 struct imsm_map *map_src = get_imsm_map(dev, MAP_1);
687629c2 10933 unsigned long long num_migr_units;
3ef4403c 10934 unsigned long long array_blocks;
2f86fda3 10935 struct dl *dl_disk = NULL;
687629c2
AK
10936
10937 memset(migr_rec, 0, sizeof(struct migr_record));
10938 migr_rec->family_num = __cpu_to_le32(super->anchor->family_num);
10939
10940 /* only ascending reshape supported now */
10941 migr_rec->ascending_migr = __cpu_to_le32(1);
10942
10943 migr_rec->dest_depth_per_unit = GEN_MIGR_AREA_SIZE /
10944 max(map_dest->blocks_per_strip, map_src->blocks_per_strip);
e1742195
AK
10945 migr_rec->dest_depth_per_unit *=
10946 max(map_dest->blocks_per_strip, map_src->blocks_per_strip);
9529d343 10947 new_data_disks = imsm_num_data_members(map_dest);
687629c2
AK
10948 migr_rec->blocks_per_unit =
10949 __cpu_to_le32(migr_rec->dest_depth_per_unit * new_data_disks);
10950 migr_rec->dest_depth_per_unit =
10951 __cpu_to_le32(migr_rec->dest_depth_per_unit);
3ef4403c 10952 array_blocks = info->component_size * new_data_disks;
687629c2
AK
10953 num_migr_units =
10954 array_blocks / __le32_to_cpu(migr_rec->blocks_per_unit);
10955
10956 if (array_blocks % __le32_to_cpu(migr_rec->blocks_per_unit))
10957 num_migr_units++;
9f421827 10958 set_num_migr_units(migr_rec, num_migr_units);
687629c2
AK
10959
10960 migr_rec->post_migr_vol_cap = dev->size_low;
10961 migr_rec->post_migr_vol_cap_hi = dev->size_high;
10962
687629c2 10963 /* Find the smallest dev */
2f86fda3
MT
10964 for (dl_disk = super->disks; dl_disk ; dl_disk = dl_disk->next) {
10965 /* ignore spares in container */
10966 if (dl_disk->index < 0)
687629c2 10967 continue;
2f86fda3 10968 get_dev_size(dl_disk->fd, NULL, &dsize);
687629c2
AK
10969 dev_sectors = dsize / 512;
10970 if (dev_sectors < min_dev_sectors)
10971 min_dev_sectors = dev_sectors;
687629c2 10972 }
9f421827 10973 set_migr_chkp_area_pba(migr_rec, min_dev_sectors -
687629c2
AK
10974 RAID_DISK_RESERVED_BLOCKS_IMSM_HI);
10975
10976 write_imsm_migr_rec(st);
10977
10978 return;
10979}
10980
10981/*******************************************************************************
10982 * Function: save_backup_imsm
10983 * Description: Function saves critical data stripes to Migration Copy Area
10984 * and updates the current migration unit status.
10985 * Use restore_stripes() to form a destination stripe,
10986 * and to write it to the Copy Area.
10987 * Parameters:
10988 * st : supertype information
aea93171 10989 * dev : imsm device that backup is saved for
687629c2
AK
10990 * info : general array info
10991 * buf : input buffer
687629c2
AK
10992 * length : length of data to backup (blocks_per_unit)
10993 * Returns:
10994 * 0 : success
10995 *, -1 : fail
10996 ******************************************************************************/
10997int save_backup_imsm(struct supertype *st,
10998 struct imsm_dev *dev,
10999 struct mdinfo *info,
11000 void *buf,
687629c2
AK
11001 int length)
11002{
11003 int rv = -1;
11004 struct intel_super *super = st->sb;
687629c2 11005 int i;
238c0a71 11006 struct imsm_map *map_dest = get_imsm_map(dev, MAP_0);
687629c2 11007 int new_disks = map_dest->num_members;
ab724b98 11008 int dest_layout = 0;
4389ce73
MT
11009 int dest_chunk, targets[new_disks];
11010 unsigned long long start, target_offsets[new_disks];
9529d343 11011 int data_disks = imsm_num_data_members(map_dest);
687629c2 11012
2f86fda3
MT
11013 for (i = 0; i < new_disks; i++) {
11014 struct dl *dl_disk = get_imsm_dl_disk(super, i);
4389ce73
MT
11015 if (dl_disk && is_fd_valid(dl_disk->fd))
11016 targets[i] = dl_disk->fd;
11017 else
11018 goto abort;
2f86fda3 11019 }
7e45b550 11020
d1877f69 11021 start = info->reshape_progress * 512;
687629c2 11022 for (i = 0; i < new_disks; i++) {
9f421827 11023 target_offsets[i] = migr_chkp_area_pba(super->migr_rec) * 512;
d1877f69
AK
11024 /* move back copy area adderss, it will be moved forward
11025 * in restore_stripes() using start input variable
11026 */
11027 target_offsets[i] -= start/data_disks;
687629c2
AK
11028 }
11029
68eb8bc6 11030 dest_layout = imsm_level_to_layout(map_dest->raid_level);
ab724b98
AK
11031 dest_chunk = __le16_to_cpu(map_dest->blocks_per_strip) * 512;
11032
687629c2
AK
11033 if (restore_stripes(targets, /* list of dest devices */
11034 target_offsets, /* migration record offsets */
11035 new_disks,
ab724b98
AK
11036 dest_chunk,
11037 map_dest->raid_level,
11038 dest_layout,
11039 -1, /* source backup file descriptor */
11040 0, /* input buf offset
11041 * always 0 buf is already offseted */
d1877f69 11042 start,
687629c2
AK
11043 length,
11044 buf) != 0) {
e7b84f9d 11045 pr_err("Error restoring stripes\n");
687629c2
AK
11046 goto abort;
11047 }
11048
11049 rv = 0;
11050
11051abort:
687629c2
AK
11052 return rv;
11053}
11054
11055/*******************************************************************************
11056 * Function: save_checkpoint_imsm
11057 * Description: Function called for current unit status update
11058 * in the migration record. It writes it to disk.
11059 * Parameters:
11060 * super : imsm internal array info
11061 * info : general array info
11062 * Returns:
11063 * 0: success
11064 * 1: failure
0228d92c
AK
11065 * 2: failure, means no valid migration record
11066 * / no general migration in progress /
687629c2
AK
11067 ******************************************************************************/
11068int save_checkpoint_imsm(struct supertype *st, struct mdinfo *info, int state)
11069{
11070 struct intel_super *super = st->sb;
f8b72ef5
AK
11071 unsigned long long blocks_per_unit;
11072 unsigned long long curr_migr_unit;
11073
2f86fda3 11074 if (load_imsm_migr_rec(super) != 0) {
7a862a02 11075 dprintf("imsm: ERROR: Cannot read migration record for checkpoint save.\n");
2e062e82
AK
11076 return 1;
11077 }
11078
f8b72ef5
AK
11079 blocks_per_unit = __le32_to_cpu(super->migr_rec->blocks_per_unit);
11080 if (blocks_per_unit == 0) {
0228d92c
AK
11081 dprintf("imsm: no migration in progress.\n");
11082 return 2;
687629c2 11083 }
f8b72ef5
AK
11084 curr_migr_unit = info->reshape_progress / blocks_per_unit;
11085 /* check if array is alligned to copy area
11086 * if it is not alligned, add one to current migration unit value
11087 * this can happend on array reshape finish only
11088 */
11089 if (info->reshape_progress % blocks_per_unit)
11090 curr_migr_unit++;
687629c2 11091
9f421827 11092 set_current_migr_unit(super->migr_rec, curr_migr_unit);
687629c2 11093 super->migr_rec->rec_status = __cpu_to_le32(state);
9f421827
PB
11094 set_migr_dest_1st_member_lba(super->migr_rec,
11095 super->migr_rec->dest_depth_per_unit * curr_migr_unit);
11096
687629c2 11097 if (write_imsm_migr_rec(st) < 0) {
7a862a02 11098 dprintf("imsm: Cannot write migration record outside backup area\n");
687629c2
AK
11099 return 1;
11100 }
11101
11102 return 0;
11103}
11104
276d77db
AK
11105/*******************************************************************************
11106 * Function: recover_backup_imsm
11107 * Description: Function recovers critical data from the Migration Copy Area
11108 * while assembling an array.
11109 * Parameters:
11110 * super : imsm internal array info
11111 * info : general array info
11112 * Returns:
11113 * 0 : success (or there is no data to recover)
11114 * 1 : fail
11115 ******************************************************************************/
11116int recover_backup_imsm(struct supertype *st, struct mdinfo *info)
11117{
11118 struct intel_super *super = st->sb;
11119 struct migr_record *migr_rec = super->migr_rec;
594dc1b8 11120 struct imsm_map *map_dest;
276d77db
AK
11121 struct intel_dev *id = NULL;
11122 unsigned long long read_offset;
11123 unsigned long long write_offset;
11124 unsigned unit_len;
2f86fda3 11125 int new_disks, err;
276d77db
AK
11126 char *buf = NULL;
11127 int retval = 1;
f36a9ecd 11128 unsigned int sector_size = super->sector_size;
4036e7ee
MT
11129 unsigned long long curr_migr_unit = current_migr_unit(migr_rec);
11130 unsigned long long num_migr_units = get_num_migr_units(migr_rec);
90fd7001 11131 char buffer[SYSFS_MAX_BUF_SIZE];
6c3560c0 11132 int skipped_disks = 0;
2f86fda3 11133 struct dl *dl_disk;
276d77db 11134
90fd7001 11135 err = sysfs_get_str(info, NULL, "array_state", (char *)buffer, sizeof(buffer));
276d77db
AK
11136 if (err < 1)
11137 return 1;
11138
11139 /* recover data only during assemblation */
11140 if (strncmp(buffer, "inactive", 8) != 0)
11141 return 0;
11142 /* no data to recover */
11143 if (__le32_to_cpu(migr_rec->rec_status) == UNIT_SRC_NORMAL)
11144 return 0;
11145 if (curr_migr_unit >= num_migr_units)
11146 return 1;
11147
11148 /* find device during reshape */
11149 for (id = super->devlist; id; id = id->next)
11150 if (is_gen_migration(id->dev))
11151 break;
11152 if (id == NULL)
11153 return 1;
11154
238c0a71 11155 map_dest = get_imsm_map(id->dev, MAP_0);
276d77db
AK
11156 new_disks = map_dest->num_members;
11157
9f421827 11158 read_offset = migr_chkp_area_pba(migr_rec) * 512;
276d77db 11159
9f421827 11160 write_offset = (migr_dest_1st_member_lba(migr_rec) +
5551b113 11161 pba_of_lba0(map_dest)) * 512;
276d77db
AK
11162
11163 unit_len = __le32_to_cpu(migr_rec->dest_depth_per_unit) * 512;
f36a9ecd 11164 if (posix_memalign((void **)&buf, sector_size, unit_len) != 0)
276d77db 11165 goto abort;
276d77db 11166
2f86fda3
MT
11167 for (dl_disk = super->disks; dl_disk; dl_disk = dl_disk->next) {
11168 if (dl_disk->index < 0)
11169 continue;
276d77db 11170
4389ce73 11171 if (!is_fd_valid(dl_disk->fd)) {
6c3560c0
AK
11172 skipped_disks++;
11173 continue;
11174 }
2f86fda3 11175 if (lseek64(dl_disk->fd, read_offset, SEEK_SET) < 0) {
e7b84f9d
N
11176 pr_err("Cannot seek to block: %s\n",
11177 strerror(errno));
137debce
AK
11178 skipped_disks++;
11179 continue;
276d77db 11180 }
83b3de77 11181 if (read(dl_disk->fd, buf, unit_len) != (ssize_t)unit_len) {
e7b84f9d
N
11182 pr_err("Cannot read copy area block: %s\n",
11183 strerror(errno));
137debce
AK
11184 skipped_disks++;
11185 continue;
276d77db 11186 }
2f86fda3 11187 if (lseek64(dl_disk->fd, write_offset, SEEK_SET) < 0) {
e7b84f9d
N
11188 pr_err("Cannot seek to block: %s\n",
11189 strerror(errno));
137debce
AK
11190 skipped_disks++;
11191 continue;
276d77db 11192 }
83b3de77 11193 if (write(dl_disk->fd, buf, unit_len) != (ssize_t)unit_len) {
e7b84f9d
N
11194 pr_err("Cannot restore block: %s\n",
11195 strerror(errno));
137debce
AK
11196 skipped_disks++;
11197 continue;
276d77db
AK
11198 }
11199 }
11200
137debce
AK
11201 if (skipped_disks > imsm_get_allowed_degradation(info->new_level,
11202 new_disks,
11203 super,
11204 id->dev)) {
7a862a02 11205 pr_err("Cannot restore data from backup. Too many failed disks\n");
6c3560c0
AK
11206 goto abort;
11207 }
11208
befb629b
AK
11209 if (save_checkpoint_imsm(st, info, UNIT_SRC_NORMAL)) {
11210 /* ignore error == 2, this can mean end of reshape here
11211 */
7a862a02 11212 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL) during restart\n");
befb629b 11213 } else
276d77db 11214 retval = 0;
276d77db
AK
11215
11216abort:
276d77db
AK
11217 free(buf);
11218 return retval;
11219}
11220
e21aea08
MT
11221/**
11222 * test_and_add_drive_controller_policy_imsm() - add disk controller to policies list.
11223 * @type: Policy type to search on list.
11224 * @pols: List of currently recorded policies.
11225 * @disk_fd: File descriptor of the device to check.
11226 * @hba: The hba disk is attached, could be NULL if verification is disabled.
11227 * @verbose: verbose flag.
11228 *
11229 * IMSM cares about drive physical placement. If @hba is not set, it adds unknown policy.
11230 * If there is no controller policy on pols we are free to add first one. If there is a policy then,
11231 * new must be the same - no controller mixing allowed.
11232 */
11233static mdadm_status_t
11234test_and_add_drive_controller_policy_imsm(const char * const type, dev_policy_t **pols, int disk_fd,
11235 struct sys_dev *hba, const int verbose)
11236{
11237 const char *controller_policy = get_sys_dev_type(SYS_DEV_UNKNOWN);
11238 struct dev_policy *pol = pol_find(*pols, (char *)type);
11239 char devname[MAX_RAID_SERIAL_LEN];
11240
11241 if (hba)
11242 controller_policy = get_sys_dev_type(hba->type);
11243
11244 if (!pol) {
11245 pol_add(pols, (char *)type, (char *)controller_policy, "imsm");
11246 return MDADM_STATUS_SUCCESS;
2cda7640 11247 }
e21aea08
MT
11248
11249 if (strcmp(pol->value, controller_policy) == 0)
11250 return MDADM_STATUS_SUCCESS;
11251
11252 fd2devname(disk_fd, devname);
11253 pr_vrb("Intel(R) raid controller \"%s\" found for %s, but \"%s\" was detected earlier\n",
11254 controller_policy, devname, pol->value);
11255 pr_vrb("Disks under different controllers cannot be used, aborting\n");
11256
11257 return MDADM_STATUS_ERROR;
11258}
11259
11260struct imsm_drive_policy {
11261 char *type;
11262 mdadm_status_t (*test_and_add_drive_policy)(const char * const type,
11263 struct dev_policy **pols, int disk_fd,
11264 struct sys_dev *hba, const int verbose);
11265};
11266
11267struct imsm_drive_policy imsm_policies[] = {
11268 {"controller", test_and_add_drive_controller_policy_imsm},
11269};
11270
11271mdadm_status_t test_and_add_drive_policies_imsm(struct dev_policy **pols, int disk_fd,
11272 const int verbose)
11273{
11274 struct imsm_drive_policy *imsm_pol;
11275 struct sys_dev *hba = NULL;
11276 char path[PATH_MAX];
11277 mdadm_status_t ret;
11278 unsigned int i;
11279
11280 /* If imsm platform verification is disabled, do not search for hba. */
11281 if (check_no_platform() != 1) {
11282 if (!diskfd_to_devpath(disk_fd, 1, path)) {
11283 pr_vrb("IMSM: Failed to retrieve device path by file descriptor.\n");
11284 return MDADM_STATUS_ERROR;
11285 }
11286
11287 hba = find_disk_attached_hba(disk_fd, path);
11288 if (!hba) {
11289 pr_vrb("IMSM: Failed to find hba for %s\n", path);
11290 return MDADM_STATUS_ERROR;
11291 }
11292 }
11293
11294 for (i = 0; i < ARRAY_SIZE(imsm_policies); i++) {
11295 imsm_pol = &imsm_policies[i];
11296
11297 ret = imsm_pol->test_and_add_drive_policy(imsm_pol->type, pols, disk_fd, hba,
11298 verbose);
11299 if (ret != MDADM_STATUS_SUCCESS)
11300 /* Inherit error code */
11301 return ret;
11302 }
11303
11304 return MDADM_STATUS_SUCCESS;
2cda7640
ML
11305}
11306
f6562011
MT
11307/**
11308 * get_spare_criteria_imsm() - set spare criteria.
11309 * @st: supertype.
11310 * @mddev_path: path to md device devnode, it must be container.
11311 * @c: spare_criteria struct to fill, not NULL.
11312 *
11313 * If superblock is not loaded, use mddev_path to load_container. It must be given in this case.
11314 * Filles size and sector size accordingly to superblock.
11315 */
11316mdadm_status_t get_spare_criteria_imsm(struct supertype *st, char *mddev_path,
11317 struct spare_criteria *c)
11318{
11319 mdadm_status_t ret = MDADM_STATUS_ERROR;
11320 bool free_superblock = false;
11321 unsigned long long size = 0;
11322 struct intel_super *super;
11323 struct extent *e;
11324 struct dl *dl;
11325 int i;
11326
11327 /* If no superblock and no mddev_path, we cannot load superblock. */
11328 assert(st->sb || mddev_path);
11329
11330 if (mddev_path) {
11331 int fd = open(mddev_path, O_RDONLY);
e21aea08 11332 mdadm_status_t rv;
f6562011
MT
11333
11334 if (!is_fd_valid(fd))
11335 return MDADM_STATUS_ERROR;
11336
11337 if (!st->sb) {
11338 if (load_container_imsm(st, fd, st->devnm)) {
11339 close(fd);
11340 return MDADM_STATUS_ERROR;
11341 }
11342 free_superblock = true;
11343 }
e21aea08
MT
11344
11345 rv = mddev_test_and_add_drive_policies(st, &c->pols, fd, 0);
f6562011 11346 close(fd);
e21aea08
MT
11347
11348 if (rv != MDADM_STATUS_SUCCESS)
11349 goto out;
f6562011
MT
11350 }
11351
11352 super = st->sb;
11353
11354 /* find first active disk in array */
11355 dl = super->disks;
11356 while (dl && (is_failed(&dl->disk) || dl->index == -1))
11357 dl = dl->next;
11358
11359 if (!dl)
11360 goto out;
11361
11362 /* find last lba used by subarrays */
11363 e = get_extents(super, dl, 0);
11364 if (!e)
11365 goto out;
11366
11367 for (i = 0; e[i].size; i++)
11368 continue;
11369 if (i > 0)
11370 size = e[i - 1].start + e[i - 1].size;
11371 free(e);
11372
11373 /* add the amount of space needed for metadata */
11374 size += imsm_min_reserved_sectors(super);
11375
11376 c->min_size = size * 512;
11377 c->sector_size = super->sector_size;
11378 c->criteria_set = true;
11379 ret = MDADM_STATUS_SUCCESS;
11380
11381out:
11382 if (free_superblock)
11383 free_super_imsm(st);
11384
11385 if (ret != MDADM_STATUS_SUCCESS)
11386 c->criteria_set = false;
11387
11388 return ret;
11389}
11390
4dd2df09 11391static char *imsm_find_array_devnm_by_subdev(int subdev, char *container)
78b10e66 11392{
4dd2df09 11393 static char devnm[32];
78b10e66
N
11394 char subdev_name[20];
11395 struct mdstat_ent *mdstat;
11396
11397 sprintf(subdev_name, "%d", subdev);
11398 mdstat = mdstat_by_subdev(subdev_name, container);
11399 if (!mdstat)
4dd2df09 11400 return NULL;
78b10e66 11401
4dd2df09 11402 strcpy(devnm, mdstat->devnm);
78b10e66 11403 free_mdstat(mdstat);
4dd2df09 11404 return devnm;
78b10e66
N
11405}
11406
11407static int imsm_reshape_is_allowed_on_container(struct supertype *st,
11408 struct geo_params *geo,
fbf3d202
AK
11409 int *old_raid_disks,
11410 int direction)
78b10e66 11411{
694575e7
KW
11412 /* currently we only support increasing the number of devices
11413 * for a container. This increases the number of device for each
11414 * member array. They must all be RAID0 or RAID5.
11415 */
78b10e66
N
11416 int ret_val = 0;
11417 struct mdinfo *info, *member;
11418 int devices_that_can_grow = 0;
11419
7a862a02 11420 dprintf("imsm: imsm_reshape_is_allowed_on_container(ENTER): st->devnm = (%s)\n", st->devnm);
78b10e66 11421
d04f65f4 11422 if (geo->size > 0 ||
78b10e66
N
11423 geo->level != UnSet ||
11424 geo->layout != UnSet ||
11425 geo->chunksize != 0 ||
11426 geo->raid_disks == UnSet) {
7a862a02 11427 dprintf("imsm: Container operation is allowed for raid disks number change only.\n");
78b10e66
N
11428 return ret_val;
11429 }
11430
fbf3d202 11431 if (direction == ROLLBACK_METADATA_CHANGES) {
7a862a02 11432 dprintf("imsm: Metadata changes rollback is not supported for container operation.\n");
fbf3d202
AK
11433 return ret_val;
11434 }
11435
78b10e66
N
11436 info = container_content_imsm(st, NULL);
11437 for (member = info; member; member = member->next) {
4dd2df09 11438 char *result;
78b10e66
N
11439
11440 dprintf("imsm: checking device_num: %i\n",
11441 member->container_member);
11442
d7d205bd 11443 if (geo->raid_disks <= member->array.raid_disks) {
78b10e66
N
11444 /* we work on container for Online Capacity Expansion
11445 * only so raid_disks has to grow
11446 */
7a862a02 11447 dprintf("imsm: for container operation raid disks increase is required\n");
78b10e66
N
11448 break;
11449 }
11450
089f9d79 11451 if (info->array.level != 0 && info->array.level != 5) {
78b10e66
N
11452 /* we cannot use this container with other raid level
11453 */
7a862a02 11454 dprintf("imsm: for container operation wrong raid level (%i) detected\n",
78b10e66
N
11455 info->array.level);
11456 break;
11457 } else {
11458 /* check for platform support
11459 * for this raid level configuration
11460 */
11461 struct intel_super *super = st->sb;
11462 if (!is_raid_level_supported(super->orom,
11463 member->array.level,
11464 geo->raid_disks)) {
7a862a02 11465 dprintf("platform does not support raid%d with %d disk%s\n",
78b10e66
N
11466 info->array.level,
11467 geo->raid_disks,
11468 geo->raid_disks > 1 ? "s" : "");
11469 break;
11470 }
2a4a08e7
AK
11471 /* check if component size is aligned to chunk size
11472 */
11473 if (info->component_size %
11474 (info->array.chunk_size/512)) {
7a862a02 11475 dprintf("Component size is not aligned to chunk size\n");
2a4a08e7
AK
11476 break;
11477 }
78b10e66
N
11478 }
11479
11480 if (*old_raid_disks &&
11481 info->array.raid_disks != *old_raid_disks)
11482 break;
11483 *old_raid_disks = info->array.raid_disks;
11484
11485 /* All raid5 and raid0 volumes in container
11486 * have to be ready for Online Capacity Expansion
11487 * so they need to be assembled. We have already
11488 * checked that no recovery etc is happening.
11489 */
4dd2df09
N
11490 result = imsm_find_array_devnm_by_subdev(member->container_member,
11491 st->container_devnm);
11492 if (result == NULL) {
78b10e66
N
11493 dprintf("imsm: cannot find array\n");
11494 break;
11495 }
11496 devices_that_can_grow++;
11497 }
11498 sysfs_free(info);
11499 if (!member && devices_that_can_grow)
11500 ret_val = 1;
11501
11502 if (ret_val)
1ade5cc1 11503 dprintf("Container operation allowed\n");
78b10e66 11504 else
1ade5cc1 11505 dprintf("Error: %i\n", ret_val);
78b10e66
N
11506
11507 return ret_val;
11508}
11509
11510/* Function: get_spares_for_grow
11511 * Description: Allocates memory and creates list of spare devices
1011e834 11512 * avaliable in container. Checks if spare drive size is acceptable.
78b10e66
N
11513 * Parameters: Pointer to the supertype structure
11514 * Returns: Pointer to the list of spare devices (mdinfo structure) on success,
1011e834 11515 * NULL if fail
78b10e66
N
11516 */
11517static struct mdinfo *get_spares_for_grow(struct supertype *st)
11518{
ae996e81
MT
11519 struct spare_criteria sc = {0};
11520 struct mdinfo *spares;
fbfdcb06 11521
f6562011 11522 get_spare_criteria_imsm(st, NULL, &sc);
ae996e81
MT
11523 spares = container_choose_spares(st, &sc, NULL, NULL, NULL, 0);
11524
11525 dev_policy_free(sc.pols);
11526
11527 return spares;
78b10e66
N
11528}
11529
11530/******************************************************************************
11531 * function: imsm_create_metadata_update_for_reshape
11532 * Function creates update for whole IMSM container.
11533 *
11534 ******************************************************************************/
11535static int imsm_create_metadata_update_for_reshape(
11536 struct supertype *st,
11537 struct geo_params *geo,
11538 int old_raid_disks,
11539 struct imsm_update_reshape **updatep)
11540{
11541 struct intel_super *super = st->sb;
11542 struct imsm_super *mpb = super->anchor;
594dc1b8
JS
11543 int update_memory_size;
11544 struct imsm_update_reshape *u;
11545 struct mdinfo *spares;
78b10e66 11546 int i;
594dc1b8 11547 int delta_disks;
bbd24d86 11548 struct mdinfo *dev;
78b10e66 11549
1ade5cc1 11550 dprintf("(enter) raid_disks = %i\n", geo->raid_disks);
78b10e66
N
11551
11552 delta_disks = geo->raid_disks - old_raid_disks;
11553
11554 /* size of all update data without anchor */
11555 update_memory_size = sizeof(struct imsm_update_reshape);
11556
11557 /* now add space for spare disks that we need to add. */
11558 update_memory_size += sizeof(u->new_disks[0]) * (delta_disks - 1);
11559
503975b9 11560 u = xcalloc(1, update_memory_size);
78b10e66
N
11561 u->type = update_reshape_container_disks;
11562 u->old_raid_disks = old_raid_disks;
11563 u->new_raid_disks = geo->raid_disks;
11564
11565 /* now get spare disks list
11566 */
11567 spares = get_spares_for_grow(st);
11568
d7be7d87 11569 if (spares == NULL || delta_disks > spares->array.spare_disks) {
7a862a02 11570 pr_err("imsm: ERROR: Cannot get spare devices for %s.\n", geo->dev_name);
e4c72d1d 11571 i = -1;
78b10e66
N
11572 goto abort;
11573 }
11574
11575 /* we have got spares
11576 * update disk list in imsm_disk list table in anchor
11577 */
11578 dprintf("imsm: %i spares are available.\n\n",
11579 spares->array.spare_disks);
11580
bbd24d86 11581 dev = spares->devs;
78b10e66 11582 for (i = 0; i < delta_disks; i++) {
78b10e66
N
11583 struct dl *dl;
11584
bbd24d86
AK
11585 if (dev == NULL)
11586 break;
78b10e66
N
11587 u->new_disks[i] = makedev(dev->disk.major,
11588 dev->disk.minor);
11589 dl = get_disk_super(super, dev->disk.major, dev->disk.minor);
ee4beede
AK
11590 dl->index = mpb->num_disks;
11591 mpb->num_disks++;
bbd24d86 11592 dev = dev->next;
78b10e66 11593 }
78b10e66
N
11594
11595abort:
11596 /* free spares
11597 */
11598 sysfs_free(spares);
11599
d677e0b8 11600 dprintf("imsm: reshape update preparation :");
78b10e66 11601 if (i == delta_disks) {
1ade5cc1 11602 dprintf_cont(" OK\n");
78b10e66
N
11603 *updatep = u;
11604 return update_memory_size;
11605 }
11606 free(u);
1ade5cc1 11607 dprintf_cont(" Error\n");
78b10e66
N
11608
11609 return 0;
11610}
11611
f3871fdc
AK
11612/******************************************************************************
11613 * function: imsm_create_metadata_update_for_size_change()
11614 * Creates update for IMSM array for array size change.
11615 *
11616 ******************************************************************************/
11617static int imsm_create_metadata_update_for_size_change(
11618 struct supertype *st,
11619 struct geo_params *geo,
11620 struct imsm_update_size_change **updatep)
11621{
11622 struct intel_super *super = st->sb;
594dc1b8
JS
11623 int update_memory_size;
11624 struct imsm_update_size_change *u;
f3871fdc 11625
1ade5cc1 11626 dprintf("(enter) New size = %llu\n", geo->size);
f3871fdc
AK
11627
11628 /* size of all update data without anchor */
11629 update_memory_size = sizeof(struct imsm_update_size_change);
11630
503975b9 11631 u = xcalloc(1, update_memory_size);
f3871fdc
AK
11632 u->type = update_size_change;
11633 u->subdev = super->current_vol;
11634 u->new_size = geo->size;
11635
11636 dprintf("imsm: reshape update preparation : OK\n");
11637 *updatep = u;
11638
11639 return update_memory_size;
11640}
11641
48c5303a
PC
11642/******************************************************************************
11643 * function: imsm_create_metadata_update_for_migration()
11644 * Creates update for IMSM array.
11645 *
11646 ******************************************************************************/
11647static int imsm_create_metadata_update_for_migration(
11648 struct supertype *st,
11649 struct geo_params *geo,
11650 struct imsm_update_reshape_migration **updatep)
11651{
11652 struct intel_super *super = st->sb;
594dc1b8 11653 int update_memory_size;
756a15f3 11654 int current_chunk_size;
594dc1b8 11655 struct imsm_update_reshape_migration *u;
756a15f3
MG
11656 struct imsm_dev *dev = get_imsm_dev(super, super->current_vol);
11657 struct imsm_map *map = get_imsm_map(dev, MAP_0);
48c5303a
PC
11658 int previous_level = -1;
11659
1ade5cc1 11660 dprintf("(enter) New Level = %i\n", geo->level);
48c5303a
PC
11661
11662 /* size of all update data without anchor */
11663 update_memory_size = sizeof(struct imsm_update_reshape_migration);
11664
503975b9 11665 u = xcalloc(1, update_memory_size);
48c5303a
PC
11666 u->type = update_reshape_migration;
11667 u->subdev = super->current_vol;
11668 u->new_level = geo->level;
11669 u->new_layout = geo->layout;
11670 u->new_raid_disks = u->old_raid_disks = geo->raid_disks;
11671 u->new_disks[0] = -1;
4bba0439 11672 u->new_chunksize = -1;
48c5303a 11673
756a15f3 11674 current_chunk_size = __le16_to_cpu(map->blocks_per_strip) / 2;
48c5303a 11675
756a15f3
MG
11676 if (geo->chunksize != current_chunk_size) {
11677 u->new_chunksize = geo->chunksize / 1024;
11678 dprintf("imsm: chunk size change from %i to %i\n",
11679 current_chunk_size, u->new_chunksize);
48c5303a 11680 }
756a15f3
MG
11681 previous_level = map->raid_level;
11682
089f9d79 11683 if (geo->level == 5 && previous_level == 0) {
48c5303a
PC
11684 struct mdinfo *spares = NULL;
11685
11686 u->new_raid_disks++;
11687 spares = get_spares_for_grow(st);
089f9d79 11688 if (spares == NULL || spares->array.spare_disks < 1) {
48c5303a
PC
11689 free(u);
11690 sysfs_free(spares);
11691 update_memory_size = 0;
565cc99e 11692 pr_err("cannot get spare device for requested migration\n");
48c5303a
PC
11693 return 0;
11694 }
11695 sysfs_free(spares);
11696 }
11697 dprintf("imsm: reshape update preparation : OK\n");
11698 *updatep = u;
11699
11700 return update_memory_size;
11701}
11702
8dd70bce
AK
11703static void imsm_update_metadata_locally(struct supertype *st,
11704 void *buf, int len)
11705{
11706 struct metadata_update mu;
11707
11708 mu.buf = buf;
11709 mu.len = len;
11710 mu.space = NULL;
11711 mu.space_list = NULL;
11712 mu.next = NULL;
5fe6f031
N
11713 if (imsm_prepare_update(st, &mu))
11714 imsm_process_update(st, &mu);
8dd70bce
AK
11715
11716 while (mu.space_list) {
11717 void **space = mu.space_list;
11718 mu.space_list = *space;
11719 free(space);
11720 }
11721}
78b10e66 11722
cbaa7904
MT
11723/**
11724 * imsm_analyze_expand() - check expand properties and calculate new size.
11725 * @st: imsm supertype.
11726 * @geo: new geometry params.
11727 * @array: array info.
11728 * @direction: reshape direction.
11729 *
11730 * Obtain free space after the &array and verify if expand to requested size is
11731 * possible. If geo->size is set to %MAX_SIZE, assume that max free size is
11732 * requested.
11733 *
11734 * Return:
11735 * On success %IMSM_STATUS_OK is returned, geo->size and geo->raid_disks are
11736 * updated.
11737 * On error, %IMSM_STATUS_ERROR is returned.
11738 */
11739static imsm_status_t imsm_analyze_expand(struct supertype *st,
11740 struct geo_params *geo,
11741 struct mdinfo *array,
11742 int direction)
11743{
11744 struct intel_super *super = st->sb;
11745 struct imsm_dev *dev = get_imsm_dev(super, super->current_vol);
11746 struct imsm_map *map = get_imsm_map(dev, MAP_0);
11747 int data_disks = imsm_num_data_members(map);
11748
11749 unsigned long long current_size;
11750 unsigned long long free_size;
11751 unsigned long long new_size;
11752 unsigned long long max_size;
11753
11754 const int chunk_kib = geo->chunksize / 1024;
11755 imsm_status_t rv;
11756
11757 if (direction == ROLLBACK_METADATA_CHANGES) {
11758 /**
11759 * Accept size for rollback only.
11760 */
11761 new_size = geo->size * 2;
11762 goto success;
11763 }
11764
cbaa7904
MT
11765 if (data_disks == 0) {
11766 pr_err("imsm: Cannot retrieve data disks.\n");
11767 return IMSM_STATUS_ERROR;
11768 }
11769 current_size = array->custom_array_size / data_disks;
11770
aa19fdd4 11771 rv = imsm_get_free_size(super, dev->vol.map->num_members, 0, chunk_kib, &free_size, true);
cbaa7904
MT
11772 if (rv != IMSM_STATUS_OK) {
11773 pr_err("imsm: Cannot find free space for expand.\n");
11774 return IMSM_STATUS_ERROR;
11775 }
11776 max_size = round_member_size_to_mb(free_size + current_size);
11777
11778 if (geo->size == MAX_SIZE)
11779 new_size = max_size;
11780 else
11781 new_size = round_member_size_to_mb(geo->size * 2);
11782
11783 if (new_size == 0) {
11784 pr_err("imsm: Rounded requested size is 0.\n");
11785 return IMSM_STATUS_ERROR;
11786 }
11787
11788 if (new_size > max_size) {
11789 pr_err("imsm: Rounded requested size (%llu) is larger than free space available (%llu).\n",
11790 new_size, max_size);
11791 return IMSM_STATUS_ERROR;
11792 }
11793
11794 if (new_size == current_size) {
11795 pr_err("imsm: Rounded requested size (%llu) is same as current size (%llu).\n",
11796 new_size, current_size);
11797 return IMSM_STATUS_ERROR;
11798 }
11799
11800 if (new_size < current_size) {
11801 pr_err("imsm: Size reduction is not supported, rounded requested size (%llu) is smaller than current (%llu).\n",
11802 new_size, current_size);
11803 return IMSM_STATUS_ERROR;
11804 }
11805
11806success:
11807 dprintf("imsm: New size per member is %llu.\n", new_size);
11808 geo->size = data_disks * new_size;
11809 geo->raid_disks = dev->vol.map->num_members;
11810 return IMSM_STATUS_OK;
11811}
11812
471bceb6 11813/***************************************************************************
694575e7 11814* Function: imsm_analyze_change
471bceb6 11815* Description: Function analyze change for single volume
1011e834 11816* and validate if transition is supported
fbf3d202
AK
11817* Parameters: Geometry parameters, supertype structure,
11818* metadata change direction (apply/rollback)
694575e7 11819* Returns: Operation type code on success, -1 if fail
471bceb6
KW
11820****************************************************************************/
11821enum imsm_reshape_type imsm_analyze_change(struct supertype *st,
fbf3d202
AK
11822 struct geo_params *geo,
11823 int direction)
694575e7 11824{
471bceb6
KW
11825 struct mdinfo info;
11826 int change = -1;
11827 int check_devs = 0;
c21e737b 11828 int chunk;
67a2db32
AK
11829 /* number of added/removed disks in operation result */
11830 int devNumChange = 0;
11831 /* imsm compatible layout value for array geometry verification */
11832 int imsm_layout = -1;
6d4d9ab2 11833 imsm_status_t rv;
471bceb6
KW
11834
11835 getinfo_super_imsm_volume(st, &info, NULL);
089f9d79
JS
11836 if (geo->level != info.array.level && geo->level >= 0 &&
11837 geo->level != UnSet) {
471bceb6
KW
11838 switch (info.array.level) {
11839 case 0:
11840 if (geo->level == 5) {
b5347799 11841 change = CH_MIGRATION;
e13ce846 11842 if (geo->layout != ALGORITHM_LEFT_ASYMMETRIC) {
7a862a02 11843 pr_err("Error. Requested Layout not supported (left-asymmetric layout is supported only)!\n");
e13ce846
AK
11844 change = -1;
11845 goto analyse_change_exit;
11846 }
67a2db32 11847 imsm_layout = geo->layout;
471bceb6 11848 check_devs = 1;
e91a3bad
LM
11849 devNumChange = 1; /* parity disk added */
11850 } else if (geo->level == 10) {
471bceb6
KW
11851 change = CH_TAKEOVER;
11852 check_devs = 1;
e91a3bad 11853 devNumChange = 2; /* two mirrors added */
67a2db32 11854 imsm_layout = 0x102; /* imsm supported layout */
471bceb6 11855 }
dfe77a9e
KW
11856 break;
11857 case 1:
471bceb6
KW
11858 case 10:
11859 if (geo->level == 0) {
11860 change = CH_TAKEOVER;
11861 check_devs = 1;
e91a3bad 11862 devNumChange = -(geo->raid_disks/2);
67a2db32 11863 imsm_layout = 0; /* imsm raid0 layout */
471bceb6
KW
11864 }
11865 break;
11866 }
11867 if (change == -1) {
7a862a02 11868 pr_err("Error. Level Migration from %d to %d not supported!\n",
e7b84f9d 11869 info.array.level, geo->level);
471bceb6
KW
11870 goto analyse_change_exit;
11871 }
11872 } else
11873 geo->level = info.array.level;
11874
089f9d79
JS
11875 if (geo->layout != info.array.layout &&
11876 (geo->layout != UnSet && geo->layout != -1)) {
b5347799 11877 change = CH_MIGRATION;
089f9d79
JS
11878 if (info.array.layout == 0 && info.array.level == 5 &&
11879 geo->layout == 5) {
471bceb6 11880 /* reshape 5 -> 4 */
089f9d79
JS
11881 } else if (info.array.layout == 5 && info.array.level == 5 &&
11882 geo->layout == 0) {
471bceb6
KW
11883 /* reshape 4 -> 5 */
11884 geo->layout = 0;
11885 geo->level = 5;
11886 } else {
7a862a02 11887 pr_err("Error. Layout Migration from %d to %d not supported!\n",
e7b84f9d 11888 info.array.layout, geo->layout);
471bceb6
KW
11889 change = -1;
11890 goto analyse_change_exit;
11891 }
67a2db32 11892 } else {
471bceb6 11893 geo->layout = info.array.layout;
67a2db32
AK
11894 if (imsm_layout == -1)
11895 imsm_layout = info.array.layout;
11896 }
471bceb6 11897
089f9d79
JS
11898 if (geo->chunksize > 0 && geo->chunksize != UnSet &&
11899 geo->chunksize != info.array.chunk_size) {
2d2b0eb7
MD
11900 if (info.array.level == 10) {
11901 pr_err("Error. Chunk size change for RAID 10 is not supported.\n");
11902 change = -1;
11903 goto analyse_change_exit;
1e9b2c3f
PB
11904 } else if (info.component_size % (geo->chunksize/512)) {
11905 pr_err("New chunk size (%dK) does not evenly divide device size (%lluk). Aborting...\n",
11906 geo->chunksize/1024, info.component_size/2);
11907 change = -1;
11908 goto analyse_change_exit;
2d2b0eb7 11909 }
b5347799 11910 change = CH_MIGRATION;
2d2b0eb7 11911 } else {
471bceb6 11912 geo->chunksize = info.array.chunk_size;
2d2b0eb7 11913 }
471bceb6 11914
cbaa7904 11915 if (geo->size > 0) {
7abc9871 11916 if (change != -1) {
7a862a02 11917 pr_err("Error. Size change should be the only one at a time.\n");
7abc9871
AK
11918 change = -1;
11919 goto analyse_change_exit;
11920 }
6d4d9ab2 11921
cbaa7904 11922 rv = imsm_analyze_expand(st, geo, &info, direction);
6d4d9ab2 11923 if (rv != IMSM_STATUS_OK)
cbaa7904 11924 goto analyse_change_exit;
7abc9871
AK
11925 change = CH_ARRAY_SIZE;
11926 }
cbaa7904
MT
11927
11928 chunk = geo->chunksize / 1024;
471bceb6
KW
11929 if (!validate_geometry_imsm(st,
11930 geo->level,
67a2db32 11931 imsm_layout,
e91a3bad 11932 geo->raid_disks + devNumChange,
c21e737b 11933 &chunk,
af4348dd 11934 geo->size, INVALID_SECTORS,
5308f117 11935 0, 0, info.consistency_policy, 1))
471bceb6
KW
11936 change = -1;
11937
11938 if (check_devs) {
11939 struct intel_super *super = st->sb;
11940 struct imsm_super *mpb = super->anchor;
11941
11942 if (mpb->num_raid_devs > 1) {
f1cc8ab9
LF
11943 pr_err("Error. Cannot perform operation on %s- for this operation "
11944 "it MUST be single array in container\n", geo->dev_name);
471bceb6
KW
11945 change = -1;
11946 }
11947 }
11948
11949analyse_change_exit:
089f9d79
JS
11950 if (direction == ROLLBACK_METADATA_CHANGES &&
11951 (change == CH_MIGRATION || change == CH_TAKEOVER)) {
7a862a02 11952 dprintf("imsm: Metadata changes rollback is not supported for migration and takeover operations.\n");
fbf3d202
AK
11953 change = -1;
11954 }
471bceb6 11955 return change;
694575e7
KW
11956}
11957
bb025c2f
KW
11958int imsm_takeover(struct supertype *st, struct geo_params *geo)
11959{
11960 struct intel_super *super = st->sb;
11961 struct imsm_update_takeover *u;
11962
503975b9 11963 u = xmalloc(sizeof(struct imsm_update_takeover));
bb025c2f
KW
11964
11965 u->type = update_takeover;
11966 u->subarray = super->current_vol;
11967
11968 /* 10->0 transition */
11969 if (geo->level == 0)
11970 u->direction = R10_TO_R0;
11971
0529c688
KW
11972 /* 0->10 transition */
11973 if (geo->level == 10)
11974 u->direction = R0_TO_R10;
11975
bb025c2f
KW
11976 /* update metadata locally */
11977 imsm_update_metadata_locally(st, u,
11978 sizeof(struct imsm_update_takeover));
11979 /* and possibly remotely */
11980 if (st->update_tail)
11981 append_metadata_update(st, u,
11982 sizeof(struct imsm_update_takeover));
11983 else
11984 free(u);
11985
11986 return 0;
11987}
11988
895ffd99
MT
11989/* Flush size update if size calculated by num_data_stripes is higher than
11990 * imsm_dev_size to eliminate differences during reshape.
11991 * Mdmon will recalculate them correctly.
11992 * If subarray index is not set then check whole container.
11993 * Returns:
11994 * 0 - no error occurred
11995 * 1 - error detected
11996 */
11997static int imsm_fix_size_mismatch(struct supertype *st, int subarray_index)
11998{
11999 struct intel_super *super = st->sb;
12000 int tmp = super->current_vol;
12001 int ret_val = 1;
12002 int i;
12003
12004 for (i = 0; i < super->anchor->num_raid_devs; i++) {
12005 if (subarray_index >= 0 && i != subarray_index)
12006 continue;
12007 super->current_vol = i;
12008 struct imsm_dev *dev = get_imsm_dev(super, super->current_vol);
12009 struct imsm_map *map = get_imsm_map(dev, MAP_0);
12010 unsigned int disc_count = imsm_num_data_members(map);
12011 struct geo_params geo;
12012 struct imsm_update_size_change *update;
12013 unsigned long long calc_size = per_dev_array_size(map) * disc_count;
12014 unsigned long long d_size = imsm_dev_size(dev);
12015 int u_size;
12016
42e02e61 12017 if (calc_size == d_size)
895ffd99
MT
12018 continue;
12019
ff904202
MT
12020 /* There is a difference, confirm that imsm_dev_size is
12021 * smaller and push update.
895ffd99 12022 */
ff904202
MT
12023 if (d_size > calc_size) {
12024 pr_err("imsm: dev size of subarray %d is incorrect\n",
12025 i);
895ffd99
MT
12026 goto exit;
12027 }
12028 memset(&geo, 0, sizeof(struct geo_params));
12029 geo.size = d_size;
12030 u_size = imsm_create_metadata_update_for_size_change(st, &geo,
12031 &update);
895ffd99
MT
12032 imsm_update_metadata_locally(st, update, u_size);
12033 if (st->update_tail) {
12034 append_metadata_update(st, update, u_size);
12035 flush_metadata_updates(st);
12036 st->update_tail = &st->updates;
12037 } else {
12038 imsm_sync_metadata(st);
5ce5a15f 12039 free(update);
895ffd99
MT
12040 }
12041 }
12042 ret_val = 0;
12043exit:
12044 super->current_vol = tmp;
12045 return ret_val;
12046}
12047
d04f65f4
N
12048static int imsm_reshape_super(struct supertype *st, unsigned long long size,
12049 int level,
78b10e66 12050 int layout, int chunksize, int raid_disks,
41784c88 12051 int delta_disks, char *backup, char *dev,
016e00f5 12052 int direction, int verbose)
78b10e66 12053{
78b10e66
N
12054 int ret_val = 1;
12055 struct geo_params geo;
12056
1ade5cc1 12057 dprintf("(enter)\n");
78b10e66 12058
71204a50 12059 memset(&geo, 0, sizeof(struct geo_params));
78b10e66
N
12060
12061 geo.dev_name = dev;
4dd2df09 12062 strcpy(geo.devnm, st->devnm);
78b10e66
N
12063 geo.size = size;
12064 geo.level = level;
12065 geo.layout = layout;
12066 geo.chunksize = chunksize;
12067 geo.raid_disks = raid_disks;
41784c88
AK
12068 if (delta_disks != UnSet)
12069 geo.raid_disks += delta_disks;
78b10e66 12070
1ade5cc1
N
12071 dprintf("for level : %i\n", geo.level);
12072 dprintf("for raid_disks : %i\n", geo.raid_disks);
78b10e66 12073
4dd2df09 12074 if (strcmp(st->container_devnm, st->devnm) == 0) {
694575e7
KW
12075 /* On container level we can only increase number of devices. */
12076 dprintf("imsm: info: Container operation\n");
78b10e66 12077 int old_raid_disks = 0;
6dc0be30 12078
78b10e66 12079 if (imsm_reshape_is_allowed_on_container(
fbf3d202 12080 st, &geo, &old_raid_disks, direction)) {
78b10e66
N
12081 struct imsm_update_reshape *u = NULL;
12082 int len;
12083
895ffd99
MT
12084 if (imsm_fix_size_mismatch(st, -1)) {
12085 dprintf("imsm: Cannot fix size mismatch\n");
12086 goto exit_imsm_reshape_super;
12087 }
12088
78b10e66
N
12089 len = imsm_create_metadata_update_for_reshape(
12090 st, &geo, old_raid_disks, &u);
12091
ed08d51c
AK
12092 if (len <= 0) {
12093 dprintf("imsm: Cannot prepare update\n");
12094 goto exit_imsm_reshape_super;
12095 }
12096
8dd70bce
AK
12097 ret_val = 0;
12098 /* update metadata locally */
12099 imsm_update_metadata_locally(st, u, len);
12100 /* and possibly remotely */
12101 if (st->update_tail)
12102 append_metadata_update(st, u, len);
12103 else
ed08d51c 12104 free(u);
8dd70bce 12105
694575e7 12106 } else {
7a862a02 12107 pr_err("(imsm) Operation is not allowed on this container\n");
694575e7
KW
12108 }
12109 } else {
12110 /* On volume level we support following operations
471bceb6
KW
12111 * - takeover: raid10 -> raid0; raid0 -> raid10
12112 * - chunk size migration
12113 * - migration: raid5 -> raid0; raid0 -> raid5
12114 */
12115 struct intel_super *super = st->sb;
12116 struct intel_dev *dev = super->devlist;
4dd2df09 12117 int change;
694575e7 12118 dprintf("imsm: info: Volume operation\n");
471bceb6
KW
12119 /* find requested device */
12120 while (dev) {
1011e834 12121 char *devnm =
4dd2df09
N
12122 imsm_find_array_devnm_by_subdev(
12123 dev->index, st->container_devnm);
12124 if (devnm && strcmp(devnm, geo.devnm) == 0)
471bceb6
KW
12125 break;
12126 dev = dev->next;
12127 }
12128 if (dev == NULL) {
4dd2df09
N
12129 pr_err("Cannot find %s (%s) subarray\n",
12130 geo.dev_name, geo.devnm);
471bceb6
KW
12131 goto exit_imsm_reshape_super;
12132 }
12133 super->current_vol = dev->index;
fbf3d202 12134 change = imsm_analyze_change(st, &geo, direction);
694575e7 12135 switch (change) {
471bceb6 12136 case CH_TAKEOVER:
bb025c2f 12137 ret_val = imsm_takeover(st, &geo);
694575e7 12138 break;
48c5303a
PC
12139 case CH_MIGRATION: {
12140 struct imsm_update_reshape_migration *u = NULL;
12141 int len =
12142 imsm_create_metadata_update_for_migration(
12143 st, &geo, &u);
12144 if (len < 1) {
7a862a02 12145 dprintf("imsm: Cannot prepare update\n");
48c5303a
PC
12146 break;
12147 }
471bceb6 12148 ret_val = 0;
48c5303a
PC
12149 /* update metadata locally */
12150 imsm_update_metadata_locally(st, u, len);
12151 /* and possibly remotely */
12152 if (st->update_tail)
12153 append_metadata_update(st, u, len);
12154 else
12155 free(u);
12156 }
12157 break;
7abc9871 12158 case CH_ARRAY_SIZE: {
f3871fdc
AK
12159 struct imsm_update_size_change *u = NULL;
12160 int len =
12161 imsm_create_metadata_update_for_size_change(
12162 st, &geo, &u);
12163 if (len < 1) {
7a862a02 12164 dprintf("imsm: Cannot prepare update\n");
f3871fdc
AK
12165 break;
12166 }
12167 ret_val = 0;
12168 /* update metadata locally */
12169 imsm_update_metadata_locally(st, u, len);
12170 /* and possibly remotely */
12171 if (st->update_tail)
12172 append_metadata_update(st, u, len);
12173 else
12174 free(u);
7abc9871
AK
12175 }
12176 break;
471bceb6
KW
12177 default:
12178 ret_val = 1;
694575e7 12179 }
694575e7 12180 }
78b10e66 12181
ed08d51c 12182exit_imsm_reshape_super:
78b10e66
N
12183 dprintf("imsm: reshape_super Exit code = %i\n", ret_val);
12184 return ret_val;
12185}
2cda7640 12186
0febb20c
AO
12187#define COMPLETED_OK 0
12188#define COMPLETED_NONE 1
12189#define COMPLETED_DELAYED 2
12190
12191static int read_completed(int fd, unsigned long long *val)
12192{
12193 int ret;
90fd7001 12194 char buf[SYSFS_MAX_BUF_SIZE];
0febb20c 12195
90fd7001 12196 ret = sysfs_fd_get_str(fd, buf, sizeof(buf));
0febb20c
AO
12197 if (ret < 0)
12198 return ret;
12199
12200 ret = COMPLETED_OK;
b823c8f9 12201 if (str_is_none(buf) == true) {
0febb20c
AO
12202 ret = COMPLETED_NONE;
12203 } else if (strncmp(buf, "delayed", 7) == 0) {
12204 ret = COMPLETED_DELAYED;
12205 } else {
12206 char *ep;
12207 *val = strtoull(buf, &ep, 0);
12208 if (ep == buf || (*ep != 0 && *ep != '\n' && *ep != ' '))
12209 ret = -1;
12210 }
12211 return ret;
12212}
12213
eee67a47
AK
12214/*******************************************************************************
12215 * Function: wait_for_reshape_imsm
12216 * Description: Function writes new sync_max value and waits until
12217 * reshape process reach new position
12218 * Parameters:
12219 * sra : general array info
eee67a47
AK
12220 * ndata : number of disks in new array's layout
12221 * Returns:
12222 * 0 : success,
12223 * 1 : there is no reshape in progress,
12224 * -1 : fail
12225 ******************************************************************************/
ae9f01f8 12226int wait_for_reshape_imsm(struct mdinfo *sra, int ndata)
eee67a47 12227{
85ca499c 12228 int fd = sysfs_get_fd(sra, NULL, "sync_completed");
df2647fa 12229 int retry = 3;
eee67a47 12230 unsigned long long completed;
ae9f01f8
AK
12231 /* to_complete : new sync_max position */
12232 unsigned long long to_complete = sra->reshape_progress;
12233 unsigned long long position_to_set = to_complete / ndata;
eee67a47 12234
4389ce73 12235 if (!is_fd_valid(fd)) {
1ade5cc1 12236 dprintf("cannot open reshape_position\n");
eee67a47 12237 return 1;
ae9f01f8 12238 }
eee67a47 12239
df2647fa
PB
12240 do {
12241 if (sysfs_fd_get_ll(fd, &completed) < 0) {
12242 if (!retry) {
12243 dprintf("cannot read reshape_position (no reshape in progres)\n");
12244 close(fd);
12245 return 1;
12246 }
239b3cc0 12247 sleep_for(0, MSEC_TO_NSEC(30), true);
df2647fa
PB
12248 } else
12249 break;
12250 } while (retry--);
eee67a47 12251
85ca499c 12252 if (completed > position_to_set) {
1ade5cc1 12253 dprintf("wrong next position to set %llu (%llu)\n",
85ca499c 12254 to_complete, position_to_set);
ae9f01f8
AK
12255 close(fd);
12256 return -1;
12257 }
12258 dprintf("Position set: %llu\n", position_to_set);
12259 if (sysfs_set_num(sra, NULL, "sync_max",
12260 position_to_set) != 0) {
1ade5cc1 12261 dprintf("cannot set reshape position to %llu\n",
ae9f01f8
AK
12262 position_to_set);
12263 close(fd);
12264 return -1;
eee67a47
AK
12265 }
12266
eee67a47 12267 do {
0febb20c 12268 int rc;
90fd7001 12269 char action[SYSFS_MAX_BUF_SIZE];
5ff3a780 12270 int timeout = 3000;
0febb20c 12271
5ff3a780 12272 sysfs_wait(fd, &timeout);
a47e44fb 12273 if (sysfs_get_str(sra, NULL, "sync_action",
90fd7001 12274 action, sizeof(action)) > 0 &&
d7d3809a 12275 strncmp(action, "reshape", 7) != 0) {
b2be2b62
AO
12276 if (strncmp(action, "idle", 4) == 0)
12277 break;
d7d3809a
AP
12278 close(fd);
12279 return -1;
12280 }
0febb20c
AO
12281
12282 rc = read_completed(fd, &completed);
12283 if (rc < 0) {
1ade5cc1 12284 dprintf("cannot read reshape_position (in loop)\n");
eee67a47
AK
12285 close(fd);
12286 return 1;
0febb20c
AO
12287 } else if (rc == COMPLETED_NONE)
12288 break;
85ca499c 12289 } while (completed < position_to_set);
b2be2b62 12290
eee67a47
AK
12291 close(fd);
12292 return 0;
eee67a47
AK
12293}
12294
b915c95f
AK
12295/*******************************************************************************
12296 * Function: check_degradation_change
12297 * Description: Check that array hasn't become failed.
12298 * Parameters:
12299 * info : for sysfs access
12300 * sources : source disks descriptors
12301 * degraded: previous degradation level
12302 * Returns:
12303 * degradation level
12304 ******************************************************************************/
12305int check_degradation_change(struct mdinfo *info,
12306 int *sources,
12307 int degraded)
12308{
12309 unsigned long long new_degraded;
e1993023
LD
12310 int rv;
12311
12312 rv = sysfs_get_ll(info, NULL, "degraded", &new_degraded);
089f9d79 12313 if (rv == -1 || (new_degraded != (unsigned long long)degraded)) {
b915c95f
AK
12314 /* check each device to ensure it is still working */
12315 struct mdinfo *sd;
12316 new_degraded = 0;
12317 for (sd = info->devs ; sd ; sd = sd->next) {
12318 if (sd->disk.state & (1<<MD_DISK_FAULTY))
12319 continue;
12320 if (sd->disk.state & (1<<MD_DISK_SYNC)) {
90fd7001 12321 char sbuf[SYSFS_MAX_BUF_SIZE];
4389ce73 12322 int raid_disk = sd->disk.raid_disk;
cf52eff5 12323
b915c95f 12324 if (sysfs_get_str(info,
cf52eff5 12325 sd, "state", sbuf, sizeof(sbuf)) < 0 ||
b915c95f
AK
12326 strstr(sbuf, "faulty") ||
12327 strstr(sbuf, "in_sync") == NULL) {
12328 /* this device is dead */
12329 sd->disk.state = (1<<MD_DISK_FAULTY);
4389ce73
MT
12330 if (raid_disk >= 0)
12331 close_fd(&sources[raid_disk]);
b915c95f
AK
12332 new_degraded++;
12333 }
12334 }
12335 }
12336 }
12337
12338 return new_degraded;
12339}
12340
10f22854
AK
12341/*******************************************************************************
12342 * Function: imsm_manage_reshape
12343 * Description: Function finds array under reshape and it manages reshape
12344 * process. It creates stripes backups (if required) and sets
942e1cdb 12345 * checkpoints.
10f22854
AK
12346 * Parameters:
12347 * afd : Backup handle (nattive) - not used
12348 * sra : general array info
12349 * reshape : reshape parameters - not used
12350 * st : supertype structure
12351 * blocks : size of critical section [blocks]
12352 * fds : table of source device descriptor
12353 * offsets : start of array (offest per devices)
12354 * dests : not used
12355 * destfd : table of destination device descriptor
12356 * destoffsets : table of destination offsets (per device)
12357 * Returns:
12358 * 1 : success, reshape is done
12359 * 0 : fail
12360 ******************************************************************************/
999b4972
N
12361static int imsm_manage_reshape(
12362 int afd, struct mdinfo *sra, struct reshape *reshape,
10f22854 12363 struct supertype *st, unsigned long backup_blocks,
999b4972
N
12364 int *fds, unsigned long long *offsets,
12365 int dests, int *destfd, unsigned long long *destoffsets)
12366{
10f22854
AK
12367 int ret_val = 0;
12368 struct intel_super *super = st->sb;
594dc1b8 12369 struct intel_dev *dv;
de44e46f 12370 unsigned int sector_size = super->sector_size;
10f22854 12371 struct imsm_dev *dev = NULL;
9529d343 12372 struct imsm_map *map_src, *map_dest;
10f22854
AK
12373 int migr_vol_qan = 0;
12374 int ndata, odata; /* [bytes] */
12375 int chunk; /* [bytes] */
12376 struct migr_record *migr_rec;
12377 char *buf = NULL;
12378 unsigned int buf_size; /* [bytes] */
12379 unsigned long long max_position; /* array size [bytes] */
12380 unsigned long long next_step; /* [blocks]/[bytes] */
12381 unsigned long long old_data_stripe_length;
10f22854
AK
12382 unsigned long long start_src; /* [bytes] */
12383 unsigned long long start; /* [bytes] */
12384 unsigned long long start_buf_shift; /* [bytes] */
b915c95f 12385 int degraded = 0;
ab724b98 12386 int source_layout = 0;
895ffd99 12387 int subarray_index = -1;
10f22854 12388
79a16a9b
JS
12389 if (!sra)
12390 return ret_val;
12391
12392 if (!fds || !offsets)
10f22854
AK
12393 goto abort;
12394
12395 /* Find volume during the reshape */
12396 for (dv = super->devlist; dv; dv = dv->next) {
fc54fe7a
JS
12397 if (dv->dev->vol.migr_type == MIGR_GEN_MIGR &&
12398 dv->dev->vol.migr_state == 1) {
10f22854
AK
12399 dev = dv->dev;
12400 migr_vol_qan++;
895ffd99 12401 subarray_index = dv->index;
10f22854
AK
12402 }
12403 }
12404 /* Only one volume can migrate at the same time */
12405 if (migr_vol_qan != 1) {
676e87a8 12406 pr_err("%s", migr_vol_qan ?
10f22854
AK
12407 "Number of migrating volumes greater than 1\n" :
12408 "There is no volume during migrationg\n");
12409 goto abort;
12410 }
12411
9529d343 12412 map_dest = get_imsm_map(dev, MAP_0);
238c0a71 12413 map_src = get_imsm_map(dev, MAP_1);
10f22854
AK
12414 if (map_src == NULL)
12415 goto abort;
10f22854 12416
9529d343
MD
12417 ndata = imsm_num_data_members(map_dest);
12418 odata = imsm_num_data_members(map_src);
10f22854 12419
7b1ab482 12420 chunk = __le16_to_cpu(map_src->blocks_per_strip) * 512;
10f22854
AK
12421 old_data_stripe_length = odata * chunk;
12422
12423 migr_rec = super->migr_rec;
12424
10f22854
AK
12425 /* initialize migration record for start condition */
12426 if (sra->reshape_progress == 0)
12427 init_migr_record_imsm(st, dev, sra);
b2c59438
AK
12428 else {
12429 if (__le32_to_cpu(migr_rec->rec_status) != UNIT_SRC_NORMAL) {
7a862a02 12430 dprintf("imsm: cannot restart migration when data are present in copy area.\n");
b2c59438
AK
12431 goto abort;
12432 }
6a75c8ca
AK
12433 /* Save checkpoint to update migration record for current
12434 * reshape position (in md). It can be farther than current
12435 * reshape position in metadata.
12436 */
12437 if (save_checkpoint_imsm(st, sra, UNIT_SRC_NORMAL) == 1) {
12438 /* ignore error == 2, this can mean end of reshape here
12439 */
7a862a02 12440 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL, initial save)\n");
6a75c8ca
AK
12441 goto abort;
12442 }
b2c59438 12443 }
10f22854
AK
12444
12445 /* size for data */
12446 buf_size = __le32_to_cpu(migr_rec->blocks_per_unit) * 512;
12447 /* extend buffer size for parity disk */
12448 buf_size += __le32_to_cpu(migr_rec->dest_depth_per_unit) * 512;
3e684231 12449 /* add space for stripe alignment */
10f22854 12450 buf_size += old_data_stripe_length;
de44e46f
PB
12451 if (posix_memalign((void **)&buf, MAX_SECTOR_SIZE, buf_size)) {
12452 dprintf("imsm: Cannot allocate checkpoint buffer\n");
10f22854
AK
12453 goto abort;
12454 }
12455
3ef4403c 12456 max_position = sra->component_size * ndata;
68eb8bc6 12457 source_layout = imsm_level_to_layout(map_src->raid_level);
10f22854 12458
9f421827
PB
12459 while (current_migr_unit(migr_rec) <
12460 get_num_migr_units(migr_rec)) {
10f22854
AK
12461 /* current reshape position [blocks] */
12462 unsigned long long current_position =
12463 __le32_to_cpu(migr_rec->blocks_per_unit)
9f421827 12464 * current_migr_unit(migr_rec);
10f22854
AK
12465 unsigned long long border;
12466
b915c95f
AK
12467 /* Check that array hasn't become failed.
12468 */
12469 degraded = check_degradation_change(sra, fds, degraded);
12470 if (degraded > 1) {
7a862a02 12471 dprintf("imsm: Abort reshape due to degradation level (%i)\n", degraded);
b915c95f
AK
12472 goto abort;
12473 }
12474
10f22854
AK
12475 next_step = __le32_to_cpu(migr_rec->blocks_per_unit);
12476
12477 if ((current_position + next_step) > max_position)
12478 next_step = max_position - current_position;
12479
92144abf 12480 start = current_position * 512;
10f22854 12481
942e1cdb 12482 /* align reading start to old geometry */
10f22854
AK
12483 start_buf_shift = start % old_data_stripe_length;
12484 start_src = start - start_buf_shift;
12485
12486 border = (start_src / odata) - (start / ndata);
12487 border /= 512;
12488 if (border <= __le32_to_cpu(migr_rec->dest_depth_per_unit)) {
12489 /* save critical stripes to buf
12490 * start - start address of current unit
12491 * to backup [bytes]
12492 * start_src - start address of current unit
12493 * to backup alligned to source array
12494 * [bytes]
12495 */
594dc1b8 12496 unsigned long long next_step_filler;
10f22854
AK
12497 unsigned long long copy_length = next_step * 512;
12498
12499 /* allign copy area length to stripe in old geometry */
12500 next_step_filler = ((copy_length + start_buf_shift)
12501 % old_data_stripe_length);
12502 if (next_step_filler)
12503 next_step_filler = (old_data_stripe_length
12504 - next_step_filler);
7a862a02 12505 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
12506 start, start_src, copy_length,
12507 start_buf_shift, next_step_filler);
12508
12509 if (save_stripes(fds, offsets, map_src->num_members,
ab724b98
AK
12510 chunk, map_src->raid_level,
12511 source_layout, 0, NULL, start_src,
10f22854
AK
12512 copy_length +
12513 next_step_filler + start_buf_shift,
12514 buf)) {
7a862a02 12515 dprintf("imsm: Cannot save stripes to buffer\n");
10f22854
AK
12516 goto abort;
12517 }
12518 /* Convert data to destination format and store it
12519 * in backup general migration area
12520 */
12521 if (save_backup_imsm(st, dev, sra,
aea93171 12522 buf + start_buf_shift, copy_length)) {
7a862a02 12523 dprintf("imsm: Cannot save stripes to target devices\n");
10f22854
AK
12524 goto abort;
12525 }
12526 if (save_checkpoint_imsm(st, sra,
12527 UNIT_SRC_IN_CP_AREA)) {
7a862a02 12528 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_IN_CP_AREA)\n");
10f22854
AK
12529 goto abort;
12530 }
8016a6d4
AK
12531 } else {
12532 /* set next step to use whole border area */
12533 border /= next_step;
12534 if (border > 1)
12535 next_step *= border;
10f22854
AK
12536 }
12537 /* When data backed up, checkpoint stored,
12538 * kick the kernel to reshape unit of data
12539 */
12540 next_step = next_step + sra->reshape_progress;
8016a6d4
AK
12541 /* limit next step to array max position */
12542 if (next_step > max_position)
12543 next_step = max_position;
10f22854
AK
12544 sysfs_set_num(sra, NULL, "suspend_lo", sra->reshape_progress);
12545 sysfs_set_num(sra, NULL, "suspend_hi", next_step);
ae9f01f8 12546 sra->reshape_progress = next_step;
10f22854
AK
12547
12548 /* wait until reshape finish */
c85338c6 12549 if (wait_for_reshape_imsm(sra, ndata)) {
c47b0ff6
AK
12550 dprintf("wait_for_reshape_imsm returned error!\n");
12551 goto abort;
12552 }
84d11e6c
N
12553 if (sigterm)
12554 goto abort;
10f22854 12555
0228d92c
AK
12556 if (save_checkpoint_imsm(st, sra, UNIT_SRC_NORMAL) == 1) {
12557 /* ignore error == 2, this can mean end of reshape here
12558 */
7a862a02 12559 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL)\n");
10f22854
AK
12560 goto abort;
12561 }
12562
12563 }
12564
71e5411e
PB
12565 /* clear migr_rec on disks after successful migration */
12566 struct dl *d;
12567
85337573 12568 memset(super->migr_rec_buf, 0, MIGR_REC_BUF_SECTORS*MAX_SECTOR_SIZE);
71e5411e
PB
12569 for (d = super->disks; d; d = d->next) {
12570 if (d->index < 0 || is_failed(&d->disk))
12571 continue;
12572 unsigned long long dsize;
12573
12574 get_dev_size(d->fd, NULL, &dsize);
de44e46f 12575 if (lseek64(d->fd, dsize - MIGR_REC_SECTOR_POSITION*sector_size,
71e5411e 12576 SEEK_SET) >= 0) {
466070ad 12577 if ((unsigned int)write(d->fd, super->migr_rec_buf,
de44e46f
PB
12578 MIGR_REC_BUF_SECTORS*sector_size) !=
12579 MIGR_REC_BUF_SECTORS*sector_size)
71e5411e
PB
12580 perror("Write migr_rec failed");
12581 }
12582 }
12583
10f22854
AK
12584 /* return '1' if done */
12585 ret_val = 1;
895ffd99
MT
12586
12587 /* After the reshape eliminate size mismatch in metadata.
12588 * Don't update md/component_size here, volume hasn't
12589 * to take whole space. It is allowed by kernel.
12590 * md/component_size will be set propoperly after next assembly.
12591 */
12592 imsm_fix_size_mismatch(st, subarray_index);
12593
10f22854
AK
12594abort:
12595 free(buf);
942e1cdb
N
12596 /* See Grow.c: abort_reshape() for further explanation */
12597 sysfs_set_num(sra, NULL, "suspend_lo", 0x7FFFFFFFFFFFFFFFULL);
12598 sysfs_set_num(sra, NULL, "suspend_hi", 0);
12599 sysfs_set_num(sra, NULL, "suspend_lo", 0);
10f22854
AK
12600
12601 return ret_val;
999b4972 12602}
0c21b485 12603
fbc42556
JR
12604/*******************************************************************************
12605 * Function: calculate_bitmap_min_chunksize
12606 * Description: Calculates the minimal valid bitmap chunk size
12607 * Parameters:
12608 * max_bits : indicate how many bits can be used for the bitmap
12609 * data_area_size : the size of the data area covered by the bitmap
12610 *
12611 * Returns:
12612 * The bitmap chunk size
12613 ******************************************************************************/
12614static unsigned long long
12615calculate_bitmap_min_chunksize(unsigned long long max_bits,
12616 unsigned long long data_area_size)
12617{
12618 unsigned long long min_chunk =
12619 4096; /* sub-page chunks don't work yet.. */
12620 unsigned long long bits = data_area_size / min_chunk + 1;
12621
12622 while (bits > max_bits) {
12623 min_chunk *= 2;
12624 bits = (bits + 1) / 2;
12625 }
12626 return min_chunk;
12627}
12628
12629/*******************************************************************************
12630 * Function: calculate_bitmap_chunksize
12631 * Description: Calculates the bitmap chunk size for the given device
12632 * Parameters:
12633 * st : supertype information
12634 * dev : device for the bitmap
12635 *
12636 * Returns:
12637 * The bitmap chunk size
12638 ******************************************************************************/
12639static unsigned long long calculate_bitmap_chunksize(struct supertype *st,
12640 struct imsm_dev *dev)
12641{
12642 struct intel_super *super = st->sb;
12643 unsigned long long min_chunksize;
12644 unsigned long long result = IMSM_DEFAULT_BITMAP_CHUNKSIZE;
12645 size_t dev_size = imsm_dev_size(dev);
12646
12647 min_chunksize = calculate_bitmap_min_chunksize(
12648 IMSM_BITMAP_AREA_SIZE * super->sector_size, dev_size);
12649
12650 if (result < min_chunksize)
12651 result = min_chunksize;
12652
12653 return result;
12654}
12655
12656/*******************************************************************************
12657 * Function: init_bitmap_header
12658 * Description: Initialize the bitmap header structure
12659 * Parameters:
12660 * st : supertype information
12661 * bms : bitmap header struct to initialize
12662 * dev : device for the bitmap
12663 *
12664 * Returns:
12665 * 0 : success
12666 * -1 : fail
12667 ******************************************************************************/
12668static int init_bitmap_header(struct supertype *st, struct bitmap_super_s *bms,
12669 struct imsm_dev *dev)
12670{
12671 int vol_uuid[4];
12672
12673 if (!bms || !dev)
12674 return -1;
12675
12676 bms->magic = __cpu_to_le32(BITMAP_MAGIC);
12677 bms->version = __cpu_to_le32(BITMAP_MAJOR_HI);
12678 bms->daemon_sleep = __cpu_to_le32(IMSM_DEFAULT_BITMAP_DAEMON_SLEEP);
12679 bms->sync_size = __cpu_to_le64(IMSM_BITMAP_AREA_SIZE);
12680 bms->write_behind = __cpu_to_le32(0);
12681
12682 uuid_from_super_imsm(st, vol_uuid);
12683 memcpy(bms->uuid, vol_uuid, 16);
12684
12685 bms->chunksize = calculate_bitmap_chunksize(st, dev);
12686
12687 return 0;
12688}
12689
12690/*******************************************************************************
12691 * Function: validate_internal_bitmap_for_drive
12692 * Description: Verify if the bitmap header for a given drive.
12693 * Parameters:
12694 * st : supertype information
12695 * offset : The offset from the beginning of the drive where to look for
12696 * the bitmap header.
12697 * d : the drive info
12698 *
12699 * Returns:
12700 * 0 : success
12701 * -1 : fail
12702 ******************************************************************************/
12703static int validate_internal_bitmap_for_drive(struct supertype *st,
12704 unsigned long long offset,
12705 struct dl *d)
12706{
12707 struct intel_super *super = st->sb;
12708 int ret = -1;
12709 int vol_uuid[4];
12710 bitmap_super_t *bms;
12711 int fd;
12712
12713 if (!d)
12714 return -1;
12715
12716 void *read_buf;
12717
12718 if (posix_memalign(&read_buf, MAX_SECTOR_SIZE, IMSM_BITMAP_HEADER_SIZE))
12719 return -1;
12720
12721 fd = d->fd;
4389ce73 12722 if (!is_fd_valid(fd)) {
fbc42556 12723 fd = open(d->devname, O_RDONLY, 0);
4389ce73
MT
12724
12725 if (!is_fd_valid(fd)) {
fbc42556
JR
12726 dprintf("cannot open the device %s\n", d->devname);
12727 goto abort;
12728 }
12729 }
12730
12731 if (lseek64(fd, offset * super->sector_size, SEEK_SET) < 0)
12732 goto abort;
12733 if (read(fd, read_buf, IMSM_BITMAP_HEADER_SIZE) !=
12734 IMSM_BITMAP_HEADER_SIZE)
12735 goto abort;
12736
12737 uuid_from_super_imsm(st, vol_uuid);
12738
12739 bms = read_buf;
12740 if ((bms->magic != __cpu_to_le32(BITMAP_MAGIC)) ||
12741 (bms->version != __cpu_to_le32(BITMAP_MAJOR_HI)) ||
12742 (!same_uuid((int *)bms->uuid, vol_uuid, st->ss->swapuuid))) {
12743 dprintf("wrong bitmap header detected\n");
12744 goto abort;
12745 }
12746
12747 ret = 0;
12748abort:
4389ce73
MT
12749 if (!is_fd_valid(d->fd))
12750 close_fd(&fd);
12751
fbc42556
JR
12752 if (read_buf)
12753 free(read_buf);
12754
12755 return ret;
12756}
12757
12758/*******************************************************************************
12759 * Function: validate_internal_bitmap_imsm
12760 * Description: Verify if the bitmap header is in place and with proper data.
12761 * Parameters:
12762 * st : supertype information
12763 *
12764 * Returns:
12765 * 0 : success or device w/o RWH_BITMAP
12766 * -1 : fail
12767 ******************************************************************************/
12768static int validate_internal_bitmap_imsm(struct supertype *st)
12769{
12770 struct intel_super *super = st->sb;
12771 struct imsm_dev *dev = get_imsm_dev(super, super->current_vol);
12772 unsigned long long offset;
12773 struct dl *d;
12774
fbc42556
JR
12775 if (dev->rwh_policy != RWH_BITMAP)
12776 return 0;
12777
12778 offset = get_bitmap_header_sector(super, super->current_vol);
12779 for (d = super->disks; d; d = d->next) {
12780 if (d->index < 0 || is_failed(&d->disk))
12781 continue;
12782
12783 if (validate_internal_bitmap_for_drive(st, offset, d)) {
12784 pr_err("imsm: bitmap validation failed\n");
12785 return -1;
12786 }
12787 }
12788 return 0;
12789}
12790
12791/*******************************************************************************
12792 * Function: add_internal_bitmap_imsm
12793 * Description: Mark the volume to use the bitmap and updates the chunk size value.
12794 * Parameters:
12795 * st : supertype information
12796 * chunkp : bitmap chunk size
12797 * delay : not used for imsm
12798 * write_behind : not used for imsm
12799 * size : not used for imsm
12800 * may_change : not used for imsm
12801 * amajor : not used for imsm
12802 *
12803 * Returns:
12804 * 0 : success
12805 * -1 : fail
12806 ******************************************************************************/
12807static int add_internal_bitmap_imsm(struct supertype *st, int *chunkp,
12808 int delay, int write_behind,
12809 unsigned long long size, int may_change,
12810 int amajor)
12811{
12812 struct intel_super *super = st->sb;
12813 int vol_idx = super->current_vol;
12814 struct imsm_dev *dev;
12815
12816 if (!super->devlist || vol_idx == -1 || !chunkp)
12817 return -1;
12818
12819 dev = get_imsm_dev(super, vol_idx);
fbc42556 12820 dev->rwh_policy = RWH_BITMAP;
fbc42556 12821 *chunkp = calculate_bitmap_chunksize(st, dev);
fbc42556
JR
12822 return 0;
12823}
12824
12825/*******************************************************************************
12826 * Function: locate_bitmap_imsm
12827 * Description: Seek 'fd' to start of write-intent-bitmap.
12828 * Parameters:
12829 * st : supertype information
12830 * fd : file descriptor for the device
12831 * node_num : not used for imsm
12832 *
12833 * Returns:
12834 * 0 : success
12835 * -1 : fail
12836 ******************************************************************************/
12837static int locate_bitmap_imsm(struct supertype *st, int fd, int node_num)
12838{
12839 struct intel_super *super = st->sb;
12840 unsigned long long offset;
12841 int vol_idx = super->current_vol;
12842
12843 if (!super->devlist || vol_idx == -1)
12844 return -1;
12845
12846 offset = get_bitmap_header_sector(super, super->current_vol);
12847 dprintf("bitmap header offset is %llu\n", offset);
12848
12849 lseek64(fd, offset << 9, 0);
12850
12851 return 0;
12852}
12853
12854/*******************************************************************************
12855 * Function: write_init_bitmap_imsm
12856 * Description: Write a bitmap header and prepares the area for the bitmap.
12857 * Parameters:
12858 * st : supertype information
12859 * fd : file descriptor for the device
12860 * update : not used for imsm
12861 *
12862 * Returns:
12863 * 0 : success
12864 * -1 : fail
12865 ******************************************************************************/
12866static int write_init_bitmap_imsm(struct supertype *st, int fd,
12867 enum bitmap_update update)
12868{
12869 struct intel_super *super = st->sb;
12870 int vol_idx = super->current_vol;
12871 int ret = 0;
12872 unsigned long long offset;
12873 bitmap_super_t bms = { 0 };
12874 size_t written = 0;
12875 size_t to_write;
12876 ssize_t rv_num;
12877 void *buf;
12878
12879 if (!super->devlist || !super->sector_size || vol_idx == -1)
12880 return -1;
12881
12882 struct imsm_dev *dev = get_imsm_dev(super, vol_idx);
12883
12884 /* first clear the space for bitmap header */
12885 unsigned long long bitmap_area_start =
12886 get_bitmap_header_sector(super, vol_idx);
12887
12888 dprintf("zeroing area start (%llu) and size (%u)\n", bitmap_area_start,
12889 IMSM_BITMAP_AND_HEADER_SIZE / super->sector_size);
12890 if (zero_disk_range(fd, bitmap_area_start,
12891 IMSM_BITMAP_HEADER_SIZE / super->sector_size)) {
12892 pr_err("imsm: cannot zeroing the space for the bitmap\n");
12893 return -1;
12894 }
12895
12896 /* The bitmap area should be filled with "1"s to perform initial
12897 * synchronization.
12898 */
12899 if (posix_memalign(&buf, MAX_SECTOR_SIZE, MAX_SECTOR_SIZE))
12900 return -1;
12901 memset(buf, 0xFF, MAX_SECTOR_SIZE);
12902 offset = get_bitmap_sector(super, vol_idx);
12903 lseek64(fd, offset << 9, 0);
12904 while (written < IMSM_BITMAP_AREA_SIZE) {
12905 to_write = IMSM_BITMAP_AREA_SIZE - written;
12906 if (to_write > MAX_SECTOR_SIZE)
12907 to_write = MAX_SECTOR_SIZE;
12908 rv_num = write(fd, buf, MAX_SECTOR_SIZE);
12909 if (rv_num != MAX_SECTOR_SIZE) {
12910 ret = -1;
12911 dprintf("cannot initialize bitmap area\n");
12912 goto abort;
12913 }
12914 written += rv_num;
12915 }
12916
12917 /* write a bitmap header */
12918 init_bitmap_header(st, &bms, dev);
12919 memset(buf, 0, MAX_SECTOR_SIZE);
12920 memcpy(buf, &bms, sizeof(bitmap_super_t));
12921 if (locate_bitmap_imsm(st, fd, 0)) {
12922 ret = -1;
12923 dprintf("cannot locate the bitmap\n");
12924 goto abort;
12925 }
12926 if (write(fd, buf, MAX_SECTOR_SIZE) != MAX_SECTOR_SIZE) {
12927 ret = -1;
12928 dprintf("cannot write the bitmap header\n");
12929 goto abort;
12930 }
12931 fsync(fd);
12932
12933abort:
12934 free(buf);
12935
12936 return ret;
12937}
12938
12939/*******************************************************************************
12940 * Function: is_vol_to_setup_bitmap
12941 * Description: Checks if a bitmap should be activated on the dev.
12942 * Parameters:
12943 * info : info about the volume to setup the bitmap
12944 * dev : the device to check against bitmap creation
12945 *
12946 * Returns:
12947 * 0 : bitmap should be set up on the device
12948 * -1 : otherwise
12949 ******************************************************************************/
12950static int is_vol_to_setup_bitmap(struct mdinfo *info, struct imsm_dev *dev)
12951{
12952 if (!dev || !info)
12953 return -1;
12954
12955 if ((strcmp((char *)dev->volume, info->name) == 0) &&
12956 (dev->rwh_policy == RWH_BITMAP))
12957 return -1;
12958
12959 return 0;
12960}
12961
12962/*******************************************************************************
12963 * Function: set_bitmap_sysfs
12964 * Description: Set the sysfs atributes of a given volume to activate the bitmap.
12965 * Parameters:
12966 * info : info about the volume where the bitmap should be setup
12967 * chunksize : bitmap chunk size
12968 * location : location of the bitmap
12969 *
12970 * Returns:
12971 * 0 : success
12972 * -1 : fail
12973 ******************************************************************************/
12974static int set_bitmap_sysfs(struct mdinfo *info, unsigned long long chunksize,
12975 char *location)
12976{
12977 /* The bitmap/metadata is set to external to allow changing of value for
12978 * bitmap/location. When external is used, the kernel will treat an offset
12979 * related to the device's first lba (in opposition to the "internal" case
12980 * when this value is related to the beginning of the superblock).
12981 */
12982 if (sysfs_set_str(info, NULL, "bitmap/metadata", "external")) {
12983 dprintf("failed to set bitmap/metadata\n");
12984 return -1;
12985 }
12986
12987 /* It can only be changed when no bitmap is active.
12988 * Should be bigger than 512 and must be power of 2.
12989 * It is expecting the value in bytes.
12990 */
12991 if (sysfs_set_num(info, NULL, "bitmap/chunksize",
12992 __cpu_to_le32(chunksize))) {
12993 dprintf("failed to set bitmap/chunksize\n");
12994 return -1;
12995 }
12996
12997 /* It is expecting the value in sectors. */
12998 if (sysfs_set_num(info, NULL, "bitmap/space",
12999 __cpu_to_le64(IMSM_BITMAP_AREA_SIZE))) {
13000 dprintf("failed to set bitmap/space\n");
13001 return -1;
13002 }
13003
13004 /* Determines the delay between the bitmap updates.
13005 * It is expecting the value in seconds.
13006 */
13007 if (sysfs_set_num(info, NULL, "bitmap/time_base",
13008 __cpu_to_le64(IMSM_DEFAULT_BITMAP_DAEMON_SLEEP))) {
13009 dprintf("failed to set bitmap/time_base\n");
13010 return -1;
13011 }
13012
13013 /* It is expecting the value in sectors with a sign at the beginning. */
13014 if (sysfs_set_str(info, NULL, "bitmap/location", location)) {
13015 dprintf("failed to set bitmap/location\n");
13016 return -1;
13017 }
13018
13019 return 0;
13020}
13021
13022/*******************************************************************************
13023 * Function: set_bitmap_imsm
13024 * Description: Setup the bitmap for the given volume
13025 * Parameters:
13026 * st : supertype information
13027 * info : info about the volume where the bitmap should be setup
13028 *
13029 * Returns:
13030 * 0 : success
13031 * -1 : fail
13032 ******************************************************************************/
13033static int set_bitmap_imsm(struct supertype *st, struct mdinfo *info)
13034{
13035 struct intel_super *super = st->sb;
13036 int prev_current_vol = super->current_vol;
13037 struct imsm_dev *dev;
13038 int ret = -1;
13039 char location[16] = "";
13040 unsigned long long chunksize;
13041 struct intel_dev *dev_it;
13042
13043 for (dev_it = super->devlist; dev_it; dev_it = dev_it->next) {
13044 super->current_vol = dev_it->index;
13045 dev = get_imsm_dev(super, super->current_vol);
13046
13047 if (is_vol_to_setup_bitmap(info, dev)) {
13048 if (validate_internal_bitmap_imsm(st)) {
13049 dprintf("bitmap header validation failed\n");
13050 goto abort;
13051 }
13052
13053 chunksize = calculate_bitmap_chunksize(st, dev);
13054 dprintf("chunk size is %llu\n", chunksize);
13055
13056 snprintf(location, sizeof(location), "+%llu",
13057 get_bitmap_sector(super, super->current_vol));
13058 dprintf("bitmap offset is %s\n", location);
13059
13060 if (set_bitmap_sysfs(info, chunksize, location)) {
13061 dprintf("cannot setup the bitmap\n");
13062 goto abort;
13063 }
13064 }
13065 }
13066 ret = 0;
13067abort:
13068 super->current_vol = prev_current_vol;
13069 return ret;
13070}
13071
cdddbdbc 13072struct superswitch super_imsm = {
cdddbdbc
DW
13073 .examine_super = examine_super_imsm,
13074 .brief_examine_super = brief_examine_super_imsm,
4737ae25 13075 .brief_examine_subarrays = brief_examine_subarrays_imsm,
9d84c8ea 13076 .export_examine_super = export_examine_super_imsm,
cdddbdbc
DW
13077 .detail_super = detail_super_imsm,
13078 .brief_detail_super = brief_detail_super_imsm,
bf5a934a 13079 .write_init_super = write_init_super_imsm,
0e600426
N
13080 .validate_geometry = validate_geometry_imsm,
13081 .add_to_super = add_to_super_imsm,
1a64be56 13082 .remove_from_super = remove_from_super_imsm,
d665cc31 13083 .detail_platform = detail_platform_imsm,
e50cf220 13084 .export_detail_platform = export_detail_platform_imsm,
33414a01 13085 .kill_subarray = kill_subarray_imsm,
aa534678 13086 .update_subarray = update_subarray_imsm,
2b959fbf 13087 .load_container = load_container_imsm,
71204a50 13088 .default_geometry = default_geometry_imsm,
e21aea08 13089 .test_and_add_drive_policies = test_and_add_drive_policies_imsm,
71204a50
N
13090 .reshape_super = imsm_reshape_super,
13091 .manage_reshape = imsm_manage_reshape,
9e2d750d 13092 .recover_backup = recover_backup_imsm,
27156a57 13093 .examine_badblocks = examine_badblocks_imsm,
cdddbdbc
DW
13094 .match_home = match_home_imsm,
13095 .uuid_from_super= uuid_from_super_imsm,
13096 .getinfo_super = getinfo_super_imsm,
5c4cd5da 13097 .getinfo_super_disks = getinfo_super_disks_imsm,
cdddbdbc
DW
13098 .update_super = update_super_imsm,
13099
13100 .avail_size = avail_size_imsm,
fbfdcb06 13101 .get_spare_criteria = get_spare_criteria_imsm,
cdddbdbc
DW
13102
13103 .compare_super = compare_super_imsm,
13104
13105 .load_super = load_super_imsm,
bf5a934a 13106 .init_super = init_super_imsm,
e683ca88 13107 .store_super = store_super_imsm,
cdddbdbc
DW
13108 .free_super = free_super_imsm,
13109 .match_metadata_desc = match_metadata_desc_imsm,
bf5a934a 13110 .container_content = container_content_imsm,
0c21b485 13111 .validate_container = validate_container_imsm,
cdddbdbc 13112
fbc42556
JR
13113 .add_internal_bitmap = add_internal_bitmap_imsm,
13114 .locate_bitmap = locate_bitmap_imsm,
13115 .write_bitmap = write_init_bitmap_imsm,
13116 .set_bitmap = set_bitmap_imsm,
13117
2432ce9b
AP
13118 .write_init_ppl = write_init_ppl_imsm,
13119 .validate_ppl = validate_ppl_imsm,
13120
cdddbdbc 13121 .external = 1,
1c832795 13122 .swapuuid = 0,
4cce4069 13123 .name = "imsm",
845dea95
NB
13124
13125/* for mdmon */
13126 .open_new = imsm_open_new,
ed9d66aa 13127 .set_array_state= imsm_set_array_state,
845dea95
NB
13128 .set_disk = imsm_set_disk,
13129 .sync_metadata = imsm_sync_metadata,
88758e9d 13130 .activate_spare = imsm_activate_spare,
e8319a19 13131 .process_update = imsm_process_update,
8273f55e 13132 .prepare_update = imsm_prepare_update,
6f50473f 13133 .record_bad_block = imsm_record_badblock,
c07a5a4f 13134 .clear_bad_block = imsm_clear_badblock,
928f1424 13135 .get_bad_blocks = imsm_get_badblocks,
cdddbdbc 13136};