]> git.ipfire.org Git - thirdparty/mdadm.git/blame - super-intel.c
tests: Gate tests for linear flavor with variable LINEAR
[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
e7b84f9d 396#define pr_vrb(fmt, arg...) (void) (verbose && pr_err(fmt, ##arg))
ec50f7b6 397
1484e727
DW
398static __u8 migr_type(struct imsm_dev *dev)
399{
400 if (dev->vol.migr_type == MIGR_VERIFY &&
401 dev->status & DEV_VERIFY_AND_FIX)
402 return MIGR_REPAIR;
403 else
404 return dev->vol.migr_type;
405}
406
407static void set_migr_type(struct imsm_dev *dev, __u8 migr_type)
408{
409 /* for compatibility with older oroms convert MIGR_REPAIR, into
410 * MIGR_VERIFY w/ DEV_VERIFY_AND_FIX status
411 */
412 if (migr_type == MIGR_REPAIR) {
413 dev->vol.migr_type = MIGR_VERIFY;
414 dev->status |= DEV_VERIFY_AND_FIX;
415 } else {
416 dev->vol.migr_type = migr_type;
417 dev->status &= ~DEV_VERIFY_AND_FIX;
418 }
419}
420
f36a9ecd 421static unsigned int sector_count(__u32 bytes, unsigned int sector_size)
cdddbdbc 422{
f36a9ecd 423 return ROUND_UP(bytes, sector_size) / sector_size;
87eb16df 424}
cdddbdbc 425
f36a9ecd
PB
426static unsigned int mpb_sectors(struct imsm_super *mpb,
427 unsigned int sector_size)
87eb16df 428{
f36a9ecd 429 return sector_count(__le32_to_cpu(mpb->mpb_size), sector_size);
cdddbdbc
DW
430}
431
ba2de7ba
DW
432struct intel_dev {
433 struct imsm_dev *dev;
434 struct intel_dev *next;
f21e18ca 435 unsigned index;
ba2de7ba
DW
436};
437
88654014
LM
438struct intel_hba {
439 enum sys_dev_type type;
440 char *path;
441 char *pci_id;
442 struct intel_hba *next;
443};
444
1a64be56
LM
445enum action {
446 DISK_REMOVE = 1,
447 DISK_ADD
448};
cdddbdbc
DW
449/* internal representation of IMSM metadata */
450struct intel_super {
451 union {
949c47a0
DW
452 void *buf; /* O_DIRECT buffer for reading/writing metadata */
453 struct imsm_super *anchor; /* immovable parameters */
cdddbdbc 454 };
8e59f3d8
AK
455 union {
456 void *migr_rec_buf; /* buffer for I/O operations */
457 struct migr_record *migr_rec; /* migration record */
458 };
51d83f5d
AK
459 int clean_migration_record_by_mdmon; /* when reshape is switched to next
460 array, it indicates that mdmon is allowed to clean migration
461 record */
949c47a0 462 size_t len; /* size of the 'buf' allocation */
bbab0940 463 size_t extra_space; /* extra space in 'buf' that is not used yet */
4d7b1503
DW
464 void *next_buf; /* for realloc'ing buf from the manager */
465 size_t next_len;
c2c087e6 466 int updates_pending; /* count of pending updates for mdmon */
bf5a934a 467 int current_vol; /* index of raid device undergoing creation */
5551b113 468 unsigned long long create_offset; /* common start for 'current_vol' */
148acb7b 469 __u32 random; /* random data for seeding new family numbers */
ba2de7ba 470 struct intel_dev *devlist;
fa7bb6f8 471 unsigned int sector_size; /* sector size of used member drives */
cdddbdbc
DW
472 struct dl {
473 struct dl *next;
474 int index;
475 __u8 serial[MAX_RAID_SERIAL_LEN];
476 int major, minor;
477 char *devname;
b9f594fe 478 struct imsm_disk disk;
cdddbdbc 479 int fd;
0dcecb2e
DW
480 int extent_cnt;
481 struct extent *e; /* for determining freespace @ create */
efb30e7f 482 int raiddisk; /* slot to fill in autolayout */
1a64be56 483 enum action action;
ca0748fa 484 } *disks, *current_disk;
1a64be56
LM
485 struct dl *disk_mgmt_list; /* list of disks to add/remove while mdmon
486 active */
47ee5a45 487 struct dl *missing; /* disks removed while we weren't looking */
43dad3d6 488 struct bbm_log *bbm_log;
88654014 489 struct intel_hba *hba; /* device path of the raid controller for this metadata */
88c32bb1 490 const struct imsm_orom *orom; /* platform firmware support */
a2b97981 491 struct intel_super *next; /* (temp) list for disambiguating family_num */
928f1424 492 struct md_bb bb; /* memory for get_bad_blocks call */
a2b97981
DW
493};
494
495struct intel_disk {
496 struct imsm_disk disk;
497 #define IMSM_UNKNOWN_OWNER (-1)
498 int owner;
499 struct intel_disk *next;
cdddbdbc
DW
500};
501
aa19fdd4
MT
502/**
503 * struct extent - reserved space details.
504 * @start: start offset.
505 * @size: size of reservation, set to 0 for metadata reservation.
506 * @vol: index of the volume, meaningful if &size is set.
507 */
c2c087e6
DW
508struct extent {
509 unsigned long long start, size;
aa19fdd4 510 int vol;
c2c087e6
DW
511};
512
694575e7
KW
513/* definitions of reshape process types */
514enum imsm_reshape_type {
515 CH_TAKEOVER,
b5347799 516 CH_MIGRATION,
7abc9871 517 CH_ARRAY_SIZE,
694575e7
KW
518};
519
88758e9d
DW
520/* definition of messages passed to imsm_process_update */
521enum imsm_update_type {
522 update_activate_spare,
8273f55e 523 update_create_array,
33414a01 524 update_kill_array,
aa534678 525 update_rename_array,
1a64be56 526 update_add_remove_disk,
78b10e66 527 update_reshape_container_disks,
48c5303a 528 update_reshape_migration,
2d40f3a1
AK
529 update_takeover,
530 update_general_migration_checkpoint,
f3871fdc 531 update_size_change,
bbab0940 532 update_prealloc_badblocks_mem,
e6e9dd3f 533 update_rwh_policy,
88758e9d
DW
534};
535
536struct imsm_update_activate_spare {
537 enum imsm_update_type type;
d23fe947 538 struct dl *dl;
88758e9d
DW
539 int slot;
540 int array;
541 struct imsm_update_activate_spare *next;
542};
543
78b10e66 544struct geo_params {
4dd2df09 545 char devnm[32];
78b10e66 546 char *dev_name;
d04f65f4 547 unsigned long long size;
78b10e66
N
548 int level;
549 int layout;
550 int chunksize;
551 int raid_disks;
552};
553
bb025c2f
KW
554enum takeover_direction {
555 R10_TO_R0,
556 R0_TO_R10
557};
558struct imsm_update_takeover {
559 enum imsm_update_type type;
560 int subarray;
561 enum takeover_direction direction;
562};
78b10e66
N
563
564struct imsm_update_reshape {
565 enum imsm_update_type type;
566 int old_raid_disks;
567 int new_raid_disks;
48c5303a
PC
568
569 int new_disks[1]; /* new_raid_disks - old_raid_disks makedev number */
570};
571
572struct imsm_update_reshape_migration {
573 enum imsm_update_type type;
574 int old_raid_disks;
575 int new_raid_disks;
576 /* fields for array migration changes
577 */
578 int subdev;
579 int new_level;
580 int new_layout;
4bba0439 581 int new_chunksize;
48c5303a 582
d195167d 583 int new_disks[1]; /* new_raid_disks - old_raid_disks makedev number */
78b10e66
N
584};
585
f3871fdc
AK
586struct imsm_update_size_change {
587 enum imsm_update_type type;
588 int subdev;
589 long long new_size;
590};
591
2d40f3a1
AK
592struct imsm_update_general_migration_checkpoint {
593 enum imsm_update_type type;
4036e7ee 594 __u64 curr_migr_unit;
2d40f3a1
AK
595};
596
54c2c1ea
DW
597struct disk_info {
598 __u8 serial[MAX_RAID_SERIAL_LEN];
599};
600
8273f55e
DW
601struct imsm_update_create_array {
602 enum imsm_update_type type;
8273f55e 603 int dev_idx;
6a3e913e 604 struct imsm_dev dev;
8273f55e
DW
605};
606
33414a01
DW
607struct imsm_update_kill_array {
608 enum imsm_update_type type;
609 int dev_idx;
610};
611
aa534678
DW
612struct imsm_update_rename_array {
613 enum imsm_update_type type;
614 __u8 name[MAX_RAID_SERIAL_LEN];
615 int dev_idx;
616};
617
1a64be56 618struct imsm_update_add_remove_disk {
43dad3d6
DW
619 enum imsm_update_type type;
620};
621
bbab0940
TM
622struct imsm_update_prealloc_bb_mem {
623 enum imsm_update_type type;
624};
625
e6e9dd3f
AP
626struct imsm_update_rwh_policy {
627 enum imsm_update_type type;
628 int new_policy;
629 int dev_idx;
630};
631
88654014
LM
632static const char *_sys_dev_type[] = {
633 [SYS_DEV_UNKNOWN] = "Unknown",
634 [SYS_DEV_SAS] = "SAS",
614902f6 635 [SYS_DEV_SATA] = "SATA",
60f0f54d 636 [SYS_DEV_NVME] = "NVMe",
75350d87
KF
637 [SYS_DEV_VMD] = "VMD",
638 [SYS_DEV_SATA_VMD] = "SATA VMD"
88654014
LM
639};
640
420dafcd
N
641static int no_platform = -1;
642
643static int check_no_platform(void)
644{
645 static const char search[] = "mdadm.imsm.test=1";
646 FILE *fp;
647
648 if (no_platform >= 0)
649 return no_platform;
650
651 if (check_env("IMSM_NO_PLATFORM")) {
652 no_platform = 1;
653 return 1;
654 }
655 fp = fopen("/proc/cmdline", "r");
656 if (fp) {
657 char *l = conf_line(fp);
658 char *w = l;
659
cf1577bf
MG
660 if (l == NULL) {
661 fclose(fp);
662 return 0;
663 }
664
420dafcd
N
665 do {
666 if (strcmp(w, search) == 0)
667 no_platform = 1;
668 w = dl_next(w);
669 } while (w != l);
670 free_line(l);
671 fclose(fp);
672 if (no_platform >= 0)
673 return no_platform;
674 }
675 no_platform = 0;
676 return 0;
677}
678
679void imsm_set_no_platform(int v)
680{
681 no_platform = v;
682}
683
88654014
LM
684const char *get_sys_dev_type(enum sys_dev_type type)
685{
686 if (type >= SYS_DEV_MAX)
687 type = SYS_DEV_UNKNOWN;
688
689 return _sys_dev_type[type];
690}
691
692static struct intel_hba * alloc_intel_hba(struct sys_dev *device)
693{
503975b9
N
694 struct intel_hba *result = xmalloc(sizeof(*result));
695
696 result->type = device->type;
697 result->path = xstrdup(device->path);
698 result->next = NULL;
699 if (result->path && (result->pci_id = strrchr(result->path, '/')) != NULL)
700 result->pci_id++;
701
88654014
LM
702 return result;
703}
704
705static struct intel_hba * find_intel_hba(struct intel_hba *hba, struct sys_dev *device)
706{
594dc1b8
JS
707 struct intel_hba *result;
708
88654014
LM
709 for (result = hba; result; result = result->next) {
710 if (result->type == device->type && strcmp(result->path, device->path) == 0)
711 break;
712 }
713 return result;
714}
715
b4cf4cba 716static int attach_hba_to_super(struct intel_super *super, struct sys_dev *device)
88654014
LM
717{
718 struct intel_hba *hba;
719
720 /* check if disk attached to Intel HBA */
721 hba = find_intel_hba(super->hba, device);
722 if (hba != NULL)
723 return 1;
724 /* Check if HBA is already attached to super */
725 if (super->hba == NULL) {
726 super->hba = alloc_intel_hba(device);
727 return 1;
6b781d33
AP
728 }
729
730 hba = super->hba;
731 /* Intel metadata allows for all disks attached to the same type HBA.
614902f6 732 * Do not support HBA types mixing
6b781d33
AP
733 */
734 if (device->type != hba->type)
88654014 735 return 2;
6b781d33
AP
736
737 /* Multiple same type HBAs can be used if they share the same OROM */
738 const struct imsm_orom *device_orom = get_orom_by_device_id(device->dev_id);
739
740 if (device_orom != super->orom)
741 return 2;
742
743 while (hba->next)
744 hba = hba->next;
745
746 hba->next = alloc_intel_hba(device);
747 return 1;
88654014
LM
748}
749
750static struct sys_dev* find_disk_attached_hba(int fd, const char *devname)
751{
9bc4ae77 752 struct sys_dev *list, *elem;
88654014
LM
753 char *disk_path;
754
755 if ((list = find_intel_devices()) == NULL)
756 return 0;
757
4389ce73 758 if (!is_fd_valid(fd))
88654014
LM
759 disk_path = (char *) devname;
760 else
7c798f87 761 disk_path = diskfd_to_devpath(fd, 1, NULL);
88654014 762
9bc4ae77 763 if (!disk_path)
88654014 764 return 0;
88654014 765
9bc4ae77
N
766 for (elem = list; elem; elem = elem->next)
767 if (path_attached_to_hba(disk_path, elem->path))
5d2434d1 768 break;
9bc4ae77 769
88654014
LM
770 if (disk_path != devname)
771 free(disk_path);
88654014 772
5d2434d1 773 return elem;
88654014
LM
774}
775
d424212e
N
776static int find_intel_hba_capability(int fd, struct intel_super *super,
777 char *devname);
f2f5c343 778
cdddbdbc
DW
779static struct supertype *match_metadata_desc_imsm(char *arg)
780{
781 struct supertype *st;
782
783 if (strcmp(arg, "imsm") != 0 &&
784 strcmp(arg, "default") != 0
785 )
786 return NULL;
787
503975b9 788 st = xcalloc(1, sizeof(*st));
cdddbdbc
DW
789 st->ss = &super_imsm;
790 st->max_devs = IMSM_MAX_DEVICES;
791 st->minor_version = 0;
792 st->sb = NULL;
793 return st;
794}
795
cdddbdbc
DW
796static __u8 *get_imsm_version(struct imsm_super *mpb)
797{
798 return &mpb->sig[MPB_SIG_LEN];
799}
800
949c47a0
DW
801/* retrieve a disk directly from the anchor when the anchor is known to be
802 * up-to-date, currently only at load time
803 */
804static struct imsm_disk *__get_imsm_disk(struct imsm_super *mpb, __u8 index)
cdddbdbc 805{
949c47a0 806 if (index >= mpb->num_disks)
cdddbdbc
DW
807 return NULL;
808 return &mpb->disk[index];
809}
810
95d07a2c
LM
811/* retrieve the disk description based on a index of the disk
812 * in the sub-array
813 */
814static struct dl *get_imsm_dl_disk(struct intel_super *super, __u8 index)
949c47a0 815{
b9f594fe
DW
816 struct dl *d;
817
818 for (d = super->disks; d; d = d->next)
819 if (d->index == index)
95d07a2c
LM
820 return d;
821
822 return NULL;
823}
824/* retrieve a disk from the parsed metadata */
825static struct imsm_disk *get_imsm_disk(struct intel_super *super, __u8 index)
826{
827 struct dl *dl;
828
829 dl = get_imsm_dl_disk(super, index);
830 if (dl)
831 return &dl->disk;
832
b9f594fe 833 return NULL;
949c47a0
DW
834}
835
836/* generate a checksum directly from the anchor when the anchor is known to be
837 * up-to-date, currently only at load or write_super after coalescing
838 */
839static __u32 __gen_imsm_checksum(struct imsm_super *mpb)
cdddbdbc
DW
840{
841 __u32 end = mpb->mpb_size / sizeof(end);
842 __u32 *p = (__u32 *) mpb;
843 __u32 sum = 0;
844
5d500228
N
845 while (end--) {
846 sum += __le32_to_cpu(*p);
97f734fd
N
847 p++;
848 }
cdddbdbc 849
5d500228 850 return sum - __le32_to_cpu(mpb->check_sum);
cdddbdbc
DW
851}
852
a965f303
DW
853static size_t sizeof_imsm_map(struct imsm_map *map)
854{
855 return sizeof(struct imsm_map) + sizeof(__u32) * (map->num_members - 1);
856}
857
858struct imsm_map *get_imsm_map(struct imsm_dev *dev, int second_map)
cdddbdbc 859{
5e7b0330
AK
860 /* A device can have 2 maps if it is in the middle of a migration.
861 * If second_map is:
238c0a71
AK
862 * MAP_0 - we return the first map
863 * MAP_1 - we return the second map if it exists, else NULL
864 * MAP_X - we return the second map if it exists, else the first
5e7b0330 865 */
a965f303 866 struct imsm_map *map = &dev->vol.map[0];
9535fc47 867 struct imsm_map *map2 = NULL;
a965f303 868
9535fc47
AK
869 if (dev->vol.migr_state)
870 map2 = (void *)map + sizeof_imsm_map(map);
a965f303 871
9535fc47 872 switch (second_map) {
3b451610 873 case MAP_0:
9535fc47 874 break;
3b451610 875 case MAP_1:
9535fc47
AK
876 map = map2;
877 break;
238c0a71 878 case MAP_X:
9535fc47
AK
879 if (map2)
880 map = map2;
881 break;
9535fc47
AK
882 default:
883 map = NULL;
884 }
885 return map;
5e7b0330 886
a965f303 887}
cdddbdbc 888
3393c6af
DW
889/* return the size of the device.
890 * migr_state increases the returned size if map[0] were to be duplicated
891 */
892static size_t sizeof_imsm_dev(struct imsm_dev *dev, int migr_state)
a965f303
DW
893{
894 size_t size = sizeof(*dev) - sizeof(struct imsm_map) +
238c0a71 895 sizeof_imsm_map(get_imsm_map(dev, MAP_0));
cdddbdbc
DW
896
897 /* migrating means an additional map */
a965f303 898 if (dev->vol.migr_state)
238c0a71 899 size += sizeof_imsm_map(get_imsm_map(dev, MAP_1));
3393c6af 900 else if (migr_state)
238c0a71 901 size += sizeof_imsm_map(get_imsm_map(dev, MAP_0));
cdddbdbc
DW
902
903 return size;
904}
905
54c2c1ea
DW
906/* retrieve disk serial number list from a metadata update */
907static struct disk_info *get_disk_info(struct imsm_update_create_array *update)
908{
909 void *u = update;
910 struct disk_info *inf;
911
912 inf = u + sizeof(*update) - sizeof(struct imsm_dev) +
913 sizeof_imsm_dev(&update->dev, 0);
914
915 return inf;
916}
54c2c1ea 917
756a15f3
MG
918/**
919 * __get_imsm_dev() - Get device with index from imsm_super.
920 * @mpb: &imsm_super pointer, not NULL.
921 * @index: Device index.
922 *
923 * Function works as non-NULL, aborting in such a case,
924 * when NULL would be returned.
925 *
926 * Device index should be in range 0 up to num_raid_devs.
927 * Function assumes the index was already verified.
928 * Index must be valid, otherwise abort() is called.
929 *
930 * Return: Pointer to corresponding imsm_dev.
931 *
932 */
949c47a0 933static struct imsm_dev *__get_imsm_dev(struct imsm_super *mpb, __u8 index)
cdddbdbc
DW
934{
935 int offset;
936 int i;
937 void *_mpb = mpb;
938
949c47a0 939 if (index >= mpb->num_raid_devs)
756a15f3 940 goto error;
cdddbdbc
DW
941
942 /* devices start after all disks */
943 offset = ((void *) &mpb->disk[mpb->num_disks]) - _mpb;
944
756a15f3 945 for (i = 0; i <= index; i++, offset += sizeof_imsm_dev(_mpb + offset, 0))
cdddbdbc
DW
946 if (i == index)
947 return _mpb + offset;
756a15f3
MG
948error:
949 pr_err("cannot find imsm_dev with index %u in imsm_super\n", index);
950 abort();
cdddbdbc
DW
951}
952
756a15f3
MG
953/**
954 * get_imsm_dev() - Get device with index from intel_super.
955 * @super: &intel_super pointer, not NULL.
956 * @index: Device index.
957 *
958 * Function works as non-NULL, aborting in such a case,
959 * when NULL would be returned.
960 *
961 * Device index should be in range 0 up to num_raid_devs.
962 * Function assumes the index was already verified.
963 * Index must be valid, otherwise abort() is called.
964 *
965 * Return: Pointer to corresponding imsm_dev.
966 *
967 */
949c47a0
DW
968static struct imsm_dev *get_imsm_dev(struct intel_super *super, __u8 index)
969{
ba2de7ba
DW
970 struct intel_dev *dv;
971
949c47a0 972 if (index >= super->anchor->num_raid_devs)
756a15f3
MG
973 goto error;
974
ba2de7ba
DW
975 for (dv = super->devlist; dv; dv = dv->next)
976 if (dv->index == index)
977 return dv->dev;
756a15f3
MG
978error:
979 pr_err("cannot find imsm_dev with index %u in intel_super\n", index);
980 abort();
949c47a0
DW
981}
982
8d67477f
TM
983static inline unsigned long long __le48_to_cpu(const struct bbm_log_block_addr
984 *addr)
985{
986 return ((((__u64)__le32_to_cpu(addr->dw1)) << 16) |
987 __le16_to_cpu(addr->w1));
988}
989
990static inline struct bbm_log_block_addr __cpu_to_le48(unsigned long long sec)
991{
992 struct bbm_log_block_addr addr;
993
994 addr.w1 = __cpu_to_le16((__u16)(sec & 0xffff));
995 addr.dw1 = __cpu_to_le32((__u32)(sec >> 16) & 0xffffffff);
996 return addr;
997}
998
8d67477f
TM
999/* get size of the bbm log */
1000static __u32 get_imsm_bbm_log_size(struct bbm_log *log)
1001{
1002 if (!log || log->entry_count == 0)
1003 return 0;
1004
1005 return sizeof(log->signature) +
1006 sizeof(log->entry_count) +
1007 log->entry_count * sizeof(struct bbm_log_entry);
1008}
6f50473f
TM
1009
1010/* check if bad block is not partially stored in bbm log */
1011static int is_stored_in_bbm(struct bbm_log *log, const __u8 idx, const unsigned
1012 long long sector, const int length, __u32 *pos)
1013{
1014 __u32 i;
1015
1016 for (i = *pos; i < log->entry_count; i++) {
1017 struct bbm_log_entry *entry = &log->marked_block_entries[i];
1018 unsigned long long bb_start;
1019 unsigned long long bb_end;
1020
1021 bb_start = __le48_to_cpu(&entry->defective_block_start);
1022 bb_end = bb_start + (entry->marked_count + 1);
1023
1024 if ((entry->disk_ordinal == idx) && (bb_start >= sector) &&
1025 (bb_end <= sector + length)) {
1026 *pos = i;
1027 return 1;
1028 }
1029 }
1030 return 0;
1031}
1032
1033/* record new bad block in bbm log */
1034static int record_new_badblock(struct bbm_log *log, const __u8 idx, unsigned
1035 long long sector, int length)
1036{
1037 int new_bb = 0;
1038 __u32 pos = 0;
1039 struct bbm_log_entry *entry = NULL;
1040
1041 while (is_stored_in_bbm(log, idx, sector, length, &pos)) {
1042 struct bbm_log_entry *e = &log->marked_block_entries[pos];
1043
1044 if ((e->marked_count + 1 == BBM_LOG_MAX_LBA_ENTRY_VAL) &&
1045 (__le48_to_cpu(&e->defective_block_start) == sector)) {
1046 sector += BBM_LOG_MAX_LBA_ENTRY_VAL;
1047 length -= BBM_LOG_MAX_LBA_ENTRY_VAL;
1048 pos = pos + 1;
1049 continue;
1050 }
1051 entry = e;
1052 break;
1053 }
1054
1055 if (entry) {
1056 int cnt = (length <= BBM_LOG_MAX_LBA_ENTRY_VAL) ? length :
1057 BBM_LOG_MAX_LBA_ENTRY_VAL;
1058 entry->defective_block_start = __cpu_to_le48(sector);
1059 entry->marked_count = cnt - 1;
1060 if (cnt == length)
1061 return 1;
1062 sector += cnt;
1063 length -= cnt;
1064 }
1065
1066 new_bb = ROUND_UP(length, BBM_LOG_MAX_LBA_ENTRY_VAL) /
1067 BBM_LOG_MAX_LBA_ENTRY_VAL;
1068 if (log->entry_count + new_bb > BBM_LOG_MAX_ENTRIES)
1069 return 0;
1070
1071 while (length > 0) {
1072 int cnt = (length <= BBM_LOG_MAX_LBA_ENTRY_VAL) ? length :
1073 BBM_LOG_MAX_LBA_ENTRY_VAL;
1074 struct bbm_log_entry *entry =
1075 &log->marked_block_entries[log->entry_count];
1076
1077 entry->defective_block_start = __cpu_to_le48(sector);
1078 entry->marked_count = cnt - 1;
1079 entry->disk_ordinal = idx;
1080
1081 sector += cnt;
1082 length -= cnt;
1083
1084 log->entry_count++;
1085 }
1086
1087 return new_bb;
1088}
c07a5a4f 1089
4c9e8c1e
TM
1090/* clear all bad blocks for given disk */
1091static void clear_disk_badblocks(struct bbm_log *log, const __u8 idx)
1092{
1093 __u32 i = 0;
1094
1095 while (i < log->entry_count) {
1096 struct bbm_log_entry *entries = log->marked_block_entries;
1097
1098 if (entries[i].disk_ordinal == idx) {
1099 if (i < log->entry_count - 1)
1100 entries[i] = entries[log->entry_count - 1];
1101 log->entry_count--;
1102 } else {
1103 i++;
1104 }
1105 }
1106}
1107
c07a5a4f
TM
1108/* clear given bad block */
1109static int clear_badblock(struct bbm_log *log, const __u8 idx, const unsigned
1110 long long sector, const int length) {
1111 __u32 i = 0;
1112
1113 while (i < log->entry_count) {
1114 struct bbm_log_entry *entries = log->marked_block_entries;
1115
1116 if ((entries[i].disk_ordinal == idx) &&
1117 (__le48_to_cpu(&entries[i].defective_block_start) ==
1118 sector) && (entries[i].marked_count + 1 == length)) {
1119 if (i < log->entry_count - 1)
1120 entries[i] = entries[log->entry_count - 1];
1121 log->entry_count--;
1122 break;
1123 }
1124 i++;
1125 }
1126
1127 return 1;
1128}
8d67477f
TM
1129
1130/* allocate and load BBM log from metadata */
1131static int load_bbm_log(struct intel_super *super)
1132{
1133 struct imsm_super *mpb = super->anchor;
1134 __u32 bbm_log_size = __le32_to_cpu(mpb->bbm_log_size);
1135
1136 super->bbm_log = xcalloc(1, sizeof(struct bbm_log));
1137 if (!super->bbm_log)
1138 return 1;
1139
1140 if (bbm_log_size) {
1141 struct bbm_log *log = (void *)mpb +
1142 __le32_to_cpu(mpb->mpb_size) - bbm_log_size;
1143
1144 __u32 entry_count;
1145
1146 if (bbm_log_size < sizeof(log->signature) +
1147 sizeof(log->entry_count))
1148 return 2;
1149
1150 entry_count = __le32_to_cpu(log->entry_count);
1151 if ((__le32_to_cpu(log->signature) != BBM_LOG_SIGNATURE) ||
1152 (entry_count > BBM_LOG_MAX_ENTRIES))
1153 return 3;
1154
1155 if (bbm_log_size !=
1156 sizeof(log->signature) + sizeof(log->entry_count) +
1157 entry_count * sizeof(struct bbm_log_entry))
1158 return 4;
1159
1160 memcpy(super->bbm_log, log, bbm_log_size);
1161 } else {
1162 super->bbm_log->signature = __cpu_to_le32(BBM_LOG_SIGNATURE);
1163 super->bbm_log->entry_count = 0;
1164 }
1165
1166 return 0;
1167}
1168
b12796be
TM
1169/* checks if bad block is within volume boundaries */
1170static int is_bad_block_in_volume(const struct bbm_log_entry *entry,
1171 const unsigned long long start_sector,
1172 const unsigned long long size)
1173{
1174 unsigned long long bb_start;
1175 unsigned long long bb_end;
1176
1177 bb_start = __le48_to_cpu(&entry->defective_block_start);
1178 bb_end = bb_start + (entry->marked_count + 1);
1179
1180 if (((bb_start >= start_sector) && (bb_start < start_sector + size)) ||
1181 ((bb_end >= start_sector) && (bb_end <= start_sector + size)))
1182 return 1;
1183
1184 return 0;
1185}
1186
1187/* get list of bad blocks on a drive for a volume */
1188static void get_volume_badblocks(const struct bbm_log *log, const __u8 idx,
1189 const unsigned long long start_sector,
1190 const unsigned long long size,
1191 struct md_bb *bbs)
1192{
1193 __u32 count = 0;
1194 __u32 i;
1195
1196 for (i = 0; i < log->entry_count; i++) {
1197 const struct bbm_log_entry *ent =
1198 &log->marked_block_entries[i];
1199 struct md_bb_entry *bb;
1200
1201 if ((ent->disk_ordinal == idx) &&
1202 is_bad_block_in_volume(ent, start_sector, size)) {
1203
1204 if (!bbs->entries) {
1205 bbs->entries = xmalloc(BBM_LOG_MAX_ENTRIES *
1206 sizeof(*bb));
1207 if (!bbs->entries)
1208 break;
1209 }
1210
1211 bb = &bbs->entries[count++];
1212 bb->sector = __le48_to_cpu(&ent->defective_block_start);
1213 bb->length = ent->marked_count + 1;
1214 }
1215 }
1216 bbs->count = count;
1217}
1218
98130f40
AK
1219/*
1220 * for second_map:
238c0a71
AK
1221 * == MAP_0 get first map
1222 * == MAP_1 get second map
1223 * == MAP_X than get map according to the current migr_state
98130f40
AK
1224 */
1225static __u32 get_imsm_ord_tbl_ent(struct imsm_dev *dev,
1226 int slot,
1227 int second_map)
7eef0453
DW
1228{
1229 struct imsm_map *map;
1230
5e7b0330 1231 map = get_imsm_map(dev, second_map);
7eef0453 1232
ff077194
DW
1233 /* top byte identifies disk under rebuild */
1234 return __le32_to_cpu(map->disk_ord_tbl[slot]);
1235}
1236
1237#define ord_to_idx(ord) (((ord) << 8) >> 8)
98130f40 1238static __u32 get_imsm_disk_idx(struct imsm_dev *dev, int slot, int second_map)
ff077194 1239{
98130f40 1240 __u32 ord = get_imsm_ord_tbl_ent(dev, slot, second_map);
ff077194
DW
1241
1242 return ord_to_idx(ord);
7eef0453
DW
1243}
1244
be73972f
DW
1245static void set_imsm_ord_tbl_ent(struct imsm_map *map, int slot, __u32 ord)
1246{
1247 map->disk_ord_tbl[slot] = __cpu_to_le32(ord);
1248}
1249
76c152ca 1250static int get_imsm_disk_slot(struct imsm_map *map, const unsigned int idx)
620b1713
DW
1251{
1252 int slot;
1253 __u32 ord;
1254
1255 for (slot = 0; slot < map->num_members; slot++) {
1256 ord = __le32_to_cpu(map->disk_ord_tbl[slot]);
1257 if (ord_to_idx(ord) == idx)
1258 return slot;
1259 }
1260
76c152ca 1261 return IMSM_STATUS_ERROR;
620b1713
DW
1262}
1263
cdddbdbc
DW
1264static int get_imsm_raid_level(struct imsm_map *map)
1265{
1266 if (map->raid_level == 1) {
1267 if (map->num_members == 2)
1268 return 1;
1269 else
1270 return 10;
1271 }
1272
1273 return map->raid_level;
1274}
1275
76c152ca
MT
1276/**
1277 * get_disk_slot_in_dev() - retrieve disk slot from &imsm_dev.
1278 * @super: &intel_super pointer, not NULL.
1279 * @dev_idx: imsm device index.
1280 * @idx: disk index.
1281 *
1282 * Return: Slot on success, IMSM_STATUS_ERROR otherwise.
1283 */
1284static int get_disk_slot_in_dev(struct intel_super *super, const __u8 dev_idx,
1285 const unsigned int idx)
1286{
1287 struct imsm_dev *dev = get_imsm_dev(super, dev_idx);
1288 struct imsm_map *map = get_imsm_map(dev, MAP_0);
1289
1290 return get_imsm_disk_slot(map, idx);
1291}
1292
c2c087e6
DW
1293static int cmp_extent(const void *av, const void *bv)
1294{
1295 const struct extent *a = av;
1296 const struct extent *b = bv;
1297 if (a->start < b->start)
1298 return -1;
1299 if (a->start > b->start)
1300 return 1;
1301 return 0;
1302}
1303
0dcecb2e 1304static int count_memberships(struct dl *dl, struct intel_super *super)
c2c087e6 1305{
c2c087e6 1306 int memberships = 0;
620b1713 1307 int i;
c2c087e6 1308
76c152ca
MT
1309 for (i = 0; i < super->anchor->num_raid_devs; i++)
1310 if (get_disk_slot_in_dev(super, i, dl->index) >= 0)
620b1713 1311 memberships++;
0dcecb2e
DW
1312
1313 return memberships;
1314}
1315
b81221b7
CA
1316static __u32 imsm_min_reserved_sectors(struct intel_super *super);
1317
486720e0 1318static int split_ull(unsigned long long n, void *lo, void *hi)
5551b113
CA
1319{
1320 if (lo == 0 || hi == 0)
1321 return 1;
486720e0
JS
1322 __put_unaligned32(__cpu_to_le32((__u32)n), lo);
1323 __put_unaligned32(__cpu_to_le32((n >> 32)), hi);
5551b113
CA
1324 return 0;
1325}
1326
1327static unsigned long long join_u32(__u32 lo, __u32 hi)
1328{
1329 return (unsigned long long)__le32_to_cpu(lo) |
1330 (((unsigned long long)__le32_to_cpu(hi)) << 32);
1331}
1332
1333static unsigned long long total_blocks(struct imsm_disk *disk)
1334{
1335 if (disk == NULL)
1336 return 0;
1337 return join_u32(disk->total_blocks_lo, disk->total_blocks_hi);
1338}
1339
1c275381
MT
1340/**
1341 * imsm_num_data_members() - get data drives count for an array.
1342 * @map: Map to analyze.
1343 *
1344 * num_data_members value represents minimal count of drives for level.
1345 * The name of the property could be misleading for RAID5 with asymmetric layout
1346 * because some data required to be calculated from parity.
1347 * The property is extracted from level and num_members value.
1348 *
1349 * Return: num_data_members value on success, zero otherwise.
1350 */
1351static __u8 imsm_num_data_members(struct imsm_map *map)
1352{
1353 switch (get_imsm_raid_level(map)) {
1354 case 0:
1355 return map->num_members;
1356 case 1:
1357 case 10:
1358 return map->num_members / 2;
1359 case 5:
1360 return map->num_members - 1;
1361 default:
1362 dprintf("unsupported raid level\n");
1363 return 0;
1364 }
1365}
1366
5551b113
CA
1367static unsigned long long pba_of_lba0(struct imsm_map *map)
1368{
1369 if (map == NULL)
1370 return 0;
1371 return join_u32(map->pba_of_lba0_lo, map->pba_of_lba0_hi);
1372}
1373
1374static unsigned long long blocks_per_member(struct imsm_map *map)
1375{
1376 if (map == NULL)
1377 return 0;
1378 return join_u32(map->blocks_per_member_lo, map->blocks_per_member_hi);
1379}
1380
1381static unsigned long long num_data_stripes(struct imsm_map *map)
1382{
1383 if (map == NULL)
1384 return 0;
1385 return join_u32(map->num_data_stripes_lo, map->num_data_stripes_hi);
1386}
1387
4036e7ee
MT
1388static unsigned long long vol_curr_migr_unit(struct imsm_dev *dev)
1389{
1390 if (dev == NULL)
1391 return 0;
1392
1393 return join_u32(dev->vol.curr_migr_unit_lo, dev->vol.curr_migr_unit_hi);
1394}
1395
fcc2c9da
MD
1396static unsigned long long imsm_dev_size(struct imsm_dev *dev)
1397{
1398 if (dev == NULL)
1399 return 0;
1400 return join_u32(dev->size_low, dev->size_high);
1401}
1402
9f421827
PB
1403static unsigned long long migr_chkp_area_pba(struct migr_record *migr_rec)
1404{
1405 if (migr_rec == NULL)
1406 return 0;
1407 return join_u32(migr_rec->ckpt_area_pba_lo,
1408 migr_rec->ckpt_area_pba_hi);
1409}
1410
1411static unsigned long long current_migr_unit(struct migr_record *migr_rec)
1412{
1413 if (migr_rec == NULL)
1414 return 0;
1415 return join_u32(migr_rec->curr_migr_unit_lo,
1416 migr_rec->curr_migr_unit_hi);
1417}
1418
1419static unsigned long long migr_dest_1st_member_lba(struct migr_record *migr_rec)
1420{
1421 if (migr_rec == NULL)
1422 return 0;
1423 return join_u32(migr_rec->dest_1st_member_lba_lo,
1424 migr_rec->dest_1st_member_lba_hi);
1425}
1426
1427static unsigned long long get_num_migr_units(struct migr_record *migr_rec)
1428{
1429 if (migr_rec == NULL)
1430 return 0;
1431 return join_u32(migr_rec->num_migr_units_lo,
1432 migr_rec->num_migr_units_hi);
1433}
1434
5551b113
CA
1435static void set_total_blocks(struct imsm_disk *disk, unsigned long long n)
1436{
1437 split_ull(n, &disk->total_blocks_lo, &disk->total_blocks_hi);
1438}
1439
1c275381
MT
1440/**
1441 * set_num_domains() - Set number of domains for an array.
1442 * @map: Map to be updated.
1443 *
1444 * num_domains property represents copies count of each data drive, thus make
1445 * it meaningful only for RAID1 and RAID10. IMSM supports two domains for
1446 * raid1 and raid10.
1447 */
1448static void set_num_domains(struct imsm_map *map)
1449{
1450 int level = get_imsm_raid_level(map);
1451
1452 if (level == 1 || level == 10)
1453 map->num_domains = 2;
1454 else
1455 map->num_domains = 1;
1456}
1457
5551b113
CA
1458static void set_pba_of_lba0(struct imsm_map *map, unsigned long long n)
1459{
1460 split_ull(n, &map->pba_of_lba0_lo, &map->pba_of_lba0_hi);
1461}
1462
1463static void set_blocks_per_member(struct imsm_map *map, unsigned long long n)
1464{
1465 split_ull(n, &map->blocks_per_member_lo, &map->blocks_per_member_hi);
1466}
1467
1468static void set_num_data_stripes(struct imsm_map *map, unsigned long long n)
1469{
1470 split_ull(n, &map->num_data_stripes_lo, &map->num_data_stripes_hi);
1471}
1472
1c275381
MT
1473/**
1474 * update_num_data_stripes() - Calculate and update num_data_stripes value.
1475 * @map: map to be updated.
1476 * @dev_size: size of volume.
1477 *
1478 * num_data_stripes value is addictionally divided by num_domains, therefore for
1479 * levels where num_domains is not 1, nds is a part of real value.
1480 */
1481static void update_num_data_stripes(struct imsm_map *map,
1482 unsigned long long dev_size)
1483{
1484 unsigned long long nds = dev_size / imsm_num_data_members(map);
1485
1486 nds /= map->num_domains;
1487 nds /= map->blocks_per_strip;
1488 set_num_data_stripes(map, nds);
1489}
1490
4036e7ee
MT
1491static void set_vol_curr_migr_unit(struct imsm_dev *dev, unsigned long long n)
1492{
1493 if (dev == NULL)
1494 return;
1495
1496 split_ull(n, &dev->vol.curr_migr_unit_lo, &dev->vol.curr_migr_unit_hi);
1497}
1498
fcc2c9da
MD
1499static void set_imsm_dev_size(struct imsm_dev *dev, unsigned long long n)
1500{
1501 split_ull(n, &dev->size_low, &dev->size_high);
1502}
1503
9f421827
PB
1504static void set_migr_chkp_area_pba(struct migr_record *migr_rec,
1505 unsigned long long n)
1506{
1507 split_ull(n, &migr_rec->ckpt_area_pba_lo, &migr_rec->ckpt_area_pba_hi);
1508}
1509
1510static void set_current_migr_unit(struct migr_record *migr_rec,
1511 unsigned long long n)
1512{
1513 split_ull(n, &migr_rec->curr_migr_unit_lo,
1514 &migr_rec->curr_migr_unit_hi);
1515}
1516
1517static void set_migr_dest_1st_member_lba(struct migr_record *migr_rec,
1518 unsigned long long n)
1519{
1520 split_ull(n, &migr_rec->dest_1st_member_lba_lo,
1521 &migr_rec->dest_1st_member_lba_hi);
1522}
1523
1524static void set_num_migr_units(struct migr_record *migr_rec,
1525 unsigned long long n)
1526{
1527 split_ull(n, &migr_rec->num_migr_units_lo,
1528 &migr_rec->num_migr_units_hi);
1529}
1530
44490938
MD
1531static unsigned long long per_dev_array_size(struct imsm_map *map)
1532{
1533 unsigned long long array_size = 0;
1534
1535 if (map == NULL)
1536 return array_size;
1537
1538 array_size = num_data_stripes(map) * map->blocks_per_strip;
1539 if (get_imsm_raid_level(map) == 1 || get_imsm_raid_level(map) == 10)
1540 array_size *= 2;
1541
1542 return array_size;
1543}
1544
05501181
PB
1545static struct extent *get_extents(struct intel_super *super, struct dl *dl,
1546 int get_minimal_reservation)
0dcecb2e
DW
1547{
1548 /* find a list of used extents on the given physical device */
0dcecb2e 1549 int memberships = count_memberships(dl, super);
aa19fdd4
MT
1550 struct extent *rv = xcalloc(memberships + 1, sizeof(struct extent));
1551 struct extent *e = rv;
1552 int i;
b276dd33
DW
1553 __u32 reservation;
1554
1555 /* trim the reserved area for spares, so they can join any array
1556 * regardless of whether the OROM has assigned sectors from the
1557 * IMSM_RESERVED_SECTORS region
1558 */
05501181 1559 if (dl->index == -1 || get_minimal_reservation)
b81221b7 1560 reservation = imsm_min_reserved_sectors(super);
b276dd33
DW
1561 else
1562 reservation = MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS;
0dcecb2e 1563
949c47a0
DW
1564 for (i = 0; i < super->anchor->num_raid_devs; i++) {
1565 struct imsm_dev *dev = get_imsm_dev(super, i);
238c0a71 1566 struct imsm_map *map = get_imsm_map(dev, MAP_0);
c2c087e6 1567
620b1713 1568 if (get_imsm_disk_slot(map, dl->index) >= 0) {
5551b113 1569 e->start = pba_of_lba0(map);
44490938 1570 e->size = per_dev_array_size(map);
aa19fdd4 1571 e->vol = i;
620b1713 1572 e++;
c2c087e6
DW
1573 }
1574 }
1575 qsort(rv, memberships, sizeof(*rv), cmp_extent);
1576
1011e834 1577 /* determine the start of the metadata
14e8215b
DW
1578 * when no raid devices are defined use the default
1579 * ...otherwise allow the metadata to truncate the value
1580 * as is the case with older versions of imsm
1581 */
1582 if (memberships) {
1583 struct extent *last = &rv[memberships - 1];
5551b113 1584 unsigned long long remainder;
14e8215b 1585
5551b113 1586 remainder = total_blocks(&dl->disk) - (last->start + last->size);
dda5855f
DW
1587 /* round down to 1k block to satisfy precision of the kernel
1588 * 'size' interface
1589 */
1590 remainder &= ~1UL;
1591 /* make sure remainder is still sane */
f21e18ca 1592 if (remainder < (unsigned)ROUND_UP(super->len, 512) >> 9)
dda5855f 1593 remainder = ROUND_UP(super->len, 512) >> 9;
14e8215b
DW
1594 if (reservation > remainder)
1595 reservation = remainder;
1596 }
5551b113 1597 e->start = total_blocks(&dl->disk) - reservation;
c2c087e6
DW
1598 e->size = 0;
1599 return rv;
1600}
1601
14e8215b
DW
1602/* try to determine how much space is reserved for metadata from
1603 * the last get_extents() entry, otherwise fallback to the
1604 * default
1605 */
1606static __u32 imsm_reserved_sectors(struct intel_super *super, struct dl *dl)
1607{
1608 struct extent *e;
1609 int i;
1610 __u32 rv;
1611
1612 /* for spares just return a minimal reservation which will grow
1613 * once the spare is picked up by an array
1614 */
1615 if (dl->index == -1)
1616 return MPB_SECTOR_CNT;
1617
05501181 1618 e = get_extents(super, dl, 0);
14e8215b
DW
1619 if (!e)
1620 return MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS;
1621
1622 /* scroll to last entry */
1623 for (i = 0; e[i].size; i++)
1624 continue;
1625
5551b113 1626 rv = total_blocks(&dl->disk) - e[i].start;
14e8215b
DW
1627
1628 free(e);
1629
1630 return rv;
1631}
1632
25ed7e59
DW
1633static int is_spare(struct imsm_disk *disk)
1634{
1635 return (disk->status & SPARE_DISK) == SPARE_DISK;
1636}
1637
1638static int is_configured(struct imsm_disk *disk)
1639{
1640 return (disk->status & CONFIGURED_DISK) == CONFIGURED_DISK;
1641}
1642
1643static int is_failed(struct imsm_disk *disk)
1644{
1645 return (disk->status & FAILED_DISK) == FAILED_DISK;
1646}
1647
2432ce9b
AP
1648static int is_journal(struct imsm_disk *disk)
1649{
1650 return (disk->status & JOURNAL_DISK) == JOURNAL_DISK;
1651}
1652
78c8028b
MT
1653/**
1654 * round_member_size_to_mb()- Round given size to closest MiB.
1655 * @size: size to round in sectors.
b53bfba6 1656 */
78c8028b 1657static inline unsigned long long round_member_size_to_mb(unsigned long long size)
b53bfba6 1658{
78c8028b
MT
1659 return (size >> SECT_PER_MB_SHIFT) << SECT_PER_MB_SHIFT;
1660}
b53bfba6 1661
78c8028b
MT
1662/**
1663 * round_size_to_mb()- Round given size.
1664 * @array_size: size to round in sectors.
1665 * @disk_count: count of data members.
1666 *
1667 * Get size per each data member and round it to closest MiB to ensure that data
1668 * splits evenly between members.
1669 *
1670 * Return: Array size, rounded down.
1671 */
1672static inline unsigned long long round_size_to_mb(unsigned long long array_size,
1673 unsigned int disk_count)
1674{
1675 return round_member_size_to_mb(array_size / disk_count) * disk_count;
b53bfba6
TM
1676}
1677
8b9cd157
MK
1678static int able_to_resync(int raid_level, int missing_disks)
1679{
1680 int max_missing_disks = 0;
1681
1682 switch (raid_level) {
1683 case 10:
1684 max_missing_disks = 1;
1685 break;
1686 default:
1687 max_missing_disks = 0;
1688 }
1689 return missing_disks <= max_missing_disks;
1690}
1691
b81221b7
CA
1692/* try to determine how much space is reserved for metadata from
1693 * the last get_extents() entry on the smallest active disk,
1694 * otherwise fallback to the default
1695 */
1696static __u32 imsm_min_reserved_sectors(struct intel_super *super)
1697{
1698 struct extent *e;
1699 int i;
5551b113
CA
1700 unsigned long long min_active;
1701 __u32 remainder;
b81221b7
CA
1702 __u32 rv = MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS;
1703 struct dl *dl, *dl_min = NULL;
1704
1705 if (!super)
1706 return rv;
1707
1708 min_active = 0;
1709 for (dl = super->disks; dl; dl = dl->next) {
1710 if (dl->index < 0)
1711 continue;
5551b113
CA
1712 unsigned long long blocks = total_blocks(&dl->disk);
1713 if (blocks < min_active || min_active == 0) {
b81221b7 1714 dl_min = dl;
5551b113 1715 min_active = blocks;
b81221b7
CA
1716 }
1717 }
1718 if (!dl_min)
1719 return rv;
1720
1721 /* find last lba used by subarrays on the smallest active disk */
05501181 1722 e = get_extents(super, dl_min, 0);
b81221b7
CA
1723 if (!e)
1724 return rv;
1725 for (i = 0; e[i].size; i++)
1726 continue;
1727
1728 remainder = min_active - e[i].start;
1729 free(e);
1730
1731 /* to give priority to recovery we should not require full
1732 IMSM_RESERVED_SECTORS from the spare */
1733 rv = MPB_SECTOR_CNT + NUM_BLOCKS_DIRTY_STRIPE_REGION;
1734
1735 /* if real reservation is smaller use that value */
1736 return (remainder < rv) ? remainder : rv;
1737}
1738
fbfdcb06
AO
1739/*
1740 * Return minimum size of a spare and sector size
1741 * that can be used in this array
1742 */
1743int get_spare_criteria_imsm(struct supertype *st, struct spare_criteria *c)
80e7f8c3
AC
1744{
1745 struct intel_super *super = st->sb;
1746 struct dl *dl;
1747 struct extent *e;
1748 int i;
fbfdcb06
AO
1749 unsigned long long size = 0;
1750
1751 c->min_size = 0;
4b57ecf6 1752 c->sector_size = 0;
80e7f8c3
AC
1753
1754 if (!super)
fbfdcb06 1755 return -EINVAL;
80e7f8c3
AC
1756 /* find first active disk in array */
1757 dl = super->disks;
1758 while (dl && (is_failed(&dl->disk) || dl->index == -1))
1759 dl = dl->next;
1760 if (!dl)
fbfdcb06 1761 return -EINVAL;
80e7f8c3 1762 /* find last lba used by subarrays */
05501181 1763 e = get_extents(super, dl, 0);
80e7f8c3 1764 if (!e)
fbfdcb06 1765 return -EINVAL;
80e7f8c3
AC
1766 for (i = 0; e[i].size; i++)
1767 continue;
1768 if (i > 0)
fbfdcb06 1769 size = e[i-1].start + e[i-1].size;
80e7f8c3 1770 free(e);
b81221b7 1771
80e7f8c3 1772 /* add the amount of space needed for metadata */
fbfdcb06
AO
1773 size += imsm_min_reserved_sectors(super);
1774
1775 c->min_size = size * 512;
4b57ecf6 1776 c->sector_size = super->sector_size;
b81221b7 1777
fbfdcb06 1778 return 0;
80e7f8c3
AC
1779}
1780
195d1d76 1781static bool is_gen_migration(struct imsm_dev *dev);
d1e02575 1782
f36a9ecd
PB
1783#define IMSM_4K_DIV 8
1784
c47b0ff6
AK
1785static __u64 blocks_per_migr_unit(struct intel_super *super,
1786 struct imsm_dev *dev);
1e5c6983 1787
c47b0ff6
AK
1788static void print_imsm_dev(struct intel_super *super,
1789 struct imsm_dev *dev,
1790 char *uuid,
1791 int disk_idx)
cdddbdbc
DW
1792{
1793 __u64 sz;
0d80bb2f 1794 int slot, i;
238c0a71
AK
1795 struct imsm_map *map = get_imsm_map(dev, MAP_0);
1796 struct imsm_map *map2 = get_imsm_map(dev, MAP_1);
b10b37b8 1797 __u32 ord;
cdddbdbc
DW
1798
1799 printf("\n");
1e7bc0ed 1800 printf("[%.16s]:\n", dev->volume);
ba1b3bc8 1801 printf(" Subarray : %d\n", super->current_vol);
44470971 1802 printf(" UUID : %s\n", uuid);
dd8bcb3b
AK
1803 printf(" RAID Level : %d", get_imsm_raid_level(map));
1804 if (map2)
1805 printf(" <-- %d", get_imsm_raid_level(map2));
1806 printf("\n");
1807 printf(" Members : %d", map->num_members);
1808 if (map2)
1809 printf(" <-- %d", map2->num_members);
1810 printf("\n");
0d80bb2f
DW
1811 printf(" Slots : [");
1812 for (i = 0; i < map->num_members; i++) {
238c0a71 1813 ord = get_imsm_ord_tbl_ent(dev, i, MAP_0);
0d80bb2f
DW
1814 printf("%s", ord & IMSM_ORD_REBUILD ? "_" : "U");
1815 }
dd8bcb3b
AK
1816 printf("]");
1817 if (map2) {
1818 printf(" <-- [");
1819 for (i = 0; i < map2->num_members; i++) {
238c0a71 1820 ord = get_imsm_ord_tbl_ent(dev, i, MAP_1);
dd8bcb3b
AK
1821 printf("%s", ord & IMSM_ORD_REBUILD ? "_" : "U");
1822 }
1823 printf("]");
1824 }
1825 printf("\n");
7095bccb
AK
1826 printf(" Failed disk : ");
1827 if (map->failed_disk_num == 0xff)
1828 printf("none");
1829 else
1830 printf("%i", map->failed_disk_num);
1831 printf("\n");
620b1713
DW
1832 slot = get_imsm_disk_slot(map, disk_idx);
1833 if (slot >= 0) {
238c0a71 1834 ord = get_imsm_ord_tbl_ent(dev, slot, MAP_X);
b10b37b8
DW
1835 printf(" This Slot : %d%s\n", slot,
1836 ord & IMSM_ORD_REBUILD ? " (out-of-sync)" : "");
1837 } else
cdddbdbc 1838 printf(" This Slot : ?\n");
84918897 1839 printf(" Sector Size : %u\n", super->sector_size);
fcc2c9da 1840 sz = imsm_dev_size(dev);
84918897
MK
1841 printf(" Array Size : %llu%s\n",
1842 (unsigned long long)sz * 512 / super->sector_size,
cdddbdbc 1843 human_size(sz * 512));
5551b113 1844 sz = blocks_per_member(map);
84918897
MK
1845 printf(" Per Dev Size : %llu%s\n",
1846 (unsigned long long)sz * 512 / super->sector_size,
cdddbdbc 1847 human_size(sz * 512));
5551b113 1848 printf(" Sector Offset : %llu\n",
7d8935cb 1849 pba_of_lba0(map) * 512 / super->sector_size);
5551b113
CA
1850 printf(" Num Stripes : %llu\n",
1851 num_data_stripes(map));
dd8bcb3b 1852 printf(" Chunk Size : %u KiB",
cdddbdbc 1853 __le16_to_cpu(map->blocks_per_strip) / 2);
dd8bcb3b
AK
1854 if (map2)
1855 printf(" <-- %u KiB",
1856 __le16_to_cpu(map2->blocks_per_strip) / 2);
1857 printf("\n");
cdddbdbc 1858 printf(" Reserved : %d\n", __le32_to_cpu(dev->reserved_blocks));
8655a7b1 1859 printf(" Migrate State : ");
1484e727
DW
1860 if (dev->vol.migr_state) {
1861 if (migr_type(dev) == MIGR_INIT)
8655a7b1 1862 printf("initialize\n");
1484e727 1863 else if (migr_type(dev) == MIGR_REBUILD)
8655a7b1 1864 printf("rebuild\n");
1484e727 1865 else if (migr_type(dev) == MIGR_VERIFY)
8655a7b1 1866 printf("check\n");
1484e727 1867 else if (migr_type(dev) == MIGR_GEN_MIGR)
8655a7b1 1868 printf("general migration\n");
1484e727 1869 else if (migr_type(dev) == MIGR_STATE_CHANGE)
8655a7b1 1870 printf("state change\n");
1484e727 1871 else if (migr_type(dev) == MIGR_REPAIR)
8655a7b1 1872 printf("repair\n");
1484e727 1873 else
8655a7b1
DW
1874 printf("<unknown:%d>\n", migr_type(dev));
1875 } else
1876 printf("idle\n");
3393c6af
DW
1877 printf(" Map State : %s", map_state_str[map->map_state]);
1878 if (dev->vol.migr_state) {
238c0a71 1879 struct imsm_map *map = get_imsm_map(dev, MAP_1);
1e5c6983 1880
b10b37b8 1881 printf(" <-- %s", map_state_str[map->map_state]);
4036e7ee 1882 printf("\n Checkpoint : %llu ", vol_curr_migr_unit(dev));
089f9d79 1883 if (is_gen_migration(dev) && (slot > 1 || slot < 0))
464d40e8
LD
1884 printf("(N/A)");
1885 else
1886 printf("(%llu)", (unsigned long long)
1887 blocks_per_migr_unit(super, dev));
3393c6af
DW
1888 }
1889 printf("\n");
2432ce9b
AP
1890 printf(" Dirty State : %s\n", (dev->vol.dirty & RAIDVOL_DIRTY) ?
1891 "dirty" : "clean");
1892 printf(" RWH Policy : ");
c2462068 1893 if (dev->rwh_policy == RWH_OFF || dev->rwh_policy == RWH_MULTIPLE_OFF)
2432ce9b
AP
1894 printf("off\n");
1895 else if (dev->rwh_policy == RWH_DISTRIBUTED)
1896 printf("PPL distributed\n");
1897 else if (dev->rwh_policy == RWH_JOURNALING_DRIVE)
1898 printf("PPL journaling drive\n");
c2462068
PB
1899 else if (dev->rwh_policy == RWH_MULTIPLE_DISTRIBUTED)
1900 printf("Multiple distributed PPLs\n");
1901 else if (dev->rwh_policy == RWH_MULTIPLE_PPLS_JOURNALING_DRIVE)
1902 printf("Multiple PPLs on journaling drive\n");
fbc42556
JR
1903 else if (dev->rwh_policy == RWH_BITMAP)
1904 printf("Write-intent bitmap\n");
2432ce9b
AP
1905 else
1906 printf("<unknown:%d>\n", dev->rwh_policy);
ba1b3bc8
AP
1907
1908 printf(" Volume ID : %u\n", dev->my_vol_raid_dev_num);
cdddbdbc
DW
1909}
1910
ef5c214e
MK
1911static void print_imsm_disk(struct imsm_disk *disk,
1912 int index,
1913 __u32 reserved,
1914 unsigned int sector_size) {
1f24f035 1915 char str[MAX_RAID_SERIAL_LEN + 1];
cdddbdbc
DW
1916 __u64 sz;
1917
0ec1f4e8 1918 if (index < -1 || !disk)
e9d82038
DW
1919 return;
1920
cdddbdbc 1921 printf("\n");
1f24f035 1922 snprintf(str, MAX_RAID_SERIAL_LEN + 1, "%s", disk->serial);
0ec1f4e8
DW
1923 if (index >= 0)
1924 printf(" Disk%02d Serial : %s\n", index, str);
1925 else
1926 printf(" Disk Serial : %s\n", str);
2432ce9b
AP
1927 printf(" State :%s%s%s%s\n", is_spare(disk) ? " spare" : "",
1928 is_configured(disk) ? " active" : "",
1929 is_failed(disk) ? " failed" : "",
1930 is_journal(disk) ? " journal" : "");
cdddbdbc 1931 printf(" Id : %08x\n", __le32_to_cpu(disk->scsi_id));
5551b113 1932 sz = total_blocks(disk) - reserved;
ef5c214e
MK
1933 printf(" Usable Size : %llu%s\n",
1934 (unsigned long long)sz * 512 / sector_size,
cdddbdbc
DW
1935 human_size(sz * 512));
1936}
1937
de44e46f
PB
1938void convert_to_4k_imsm_migr_rec(struct intel_super *super)
1939{
1940 struct migr_record *migr_rec = super->migr_rec;
1941
1942 migr_rec->blocks_per_unit /= IMSM_4K_DIV;
de44e46f
PB
1943 migr_rec->dest_depth_per_unit /= IMSM_4K_DIV;
1944 split_ull((join_u32(migr_rec->post_migr_vol_cap,
1945 migr_rec->post_migr_vol_cap_hi) / IMSM_4K_DIV),
1946 &migr_rec->post_migr_vol_cap, &migr_rec->post_migr_vol_cap_hi);
9f421827
PB
1947 set_migr_chkp_area_pba(migr_rec,
1948 migr_chkp_area_pba(migr_rec) / IMSM_4K_DIV);
1949 set_migr_dest_1st_member_lba(migr_rec,
1950 migr_dest_1st_member_lba(migr_rec) / IMSM_4K_DIV);
de44e46f
PB
1951}
1952
f36a9ecd
PB
1953void convert_to_4k_imsm_disk(struct imsm_disk *disk)
1954{
1955 set_total_blocks(disk, (total_blocks(disk)/IMSM_4K_DIV));
1956}
1957
1958void convert_to_4k(struct intel_super *super)
1959{
1960 struct imsm_super *mpb = super->anchor;
1961 struct imsm_disk *disk;
1962 int i;
e4467bc7 1963 __u32 bbm_log_size = __le32_to_cpu(mpb->bbm_log_size);
f36a9ecd
PB
1964
1965 for (i = 0; i < mpb->num_disks ; i++) {
1966 disk = __get_imsm_disk(mpb, i);
1967 /* disk */
1968 convert_to_4k_imsm_disk(disk);
1969 }
1970 for (i = 0; i < mpb->num_raid_devs; i++) {
1971 struct imsm_dev *dev = __get_imsm_dev(mpb, i);
1972 struct imsm_map *map = get_imsm_map(dev, MAP_0);
1973 /* dev */
fcc2c9da 1974 set_imsm_dev_size(dev, imsm_dev_size(dev)/IMSM_4K_DIV);
4036e7ee
MT
1975 set_vol_curr_migr_unit(dev,
1976 vol_curr_migr_unit(dev) / IMSM_4K_DIV);
f36a9ecd
PB
1977
1978 /* map0 */
1979 set_blocks_per_member(map, blocks_per_member(map)/IMSM_4K_DIV);
1980 map->blocks_per_strip /= IMSM_4K_DIV;
1981 set_pba_of_lba0(map, pba_of_lba0(map)/IMSM_4K_DIV);
1982
1983 if (dev->vol.migr_state) {
1984 /* map1 */
1985 map = get_imsm_map(dev, MAP_1);
1986 set_blocks_per_member(map,
1987 blocks_per_member(map)/IMSM_4K_DIV);
1988 map->blocks_per_strip /= IMSM_4K_DIV;
1989 set_pba_of_lba0(map, pba_of_lba0(map)/IMSM_4K_DIV);
1990 }
1991 }
e4467bc7
TM
1992 if (bbm_log_size) {
1993 struct bbm_log *log = (void *)mpb +
1994 __le32_to_cpu(mpb->mpb_size) - bbm_log_size;
1995 __u32 i;
1996
1997 for (i = 0; i < log->entry_count; i++) {
1998 struct bbm_log_entry *entry =
1999 &log->marked_block_entries[i];
2000
2001 __u8 count = entry->marked_count + 1;
2002 unsigned long long sector =
2003 __le48_to_cpu(&entry->defective_block_start);
2004
2005 entry->defective_block_start =
2006 __cpu_to_le48(sector/IMSM_4K_DIV);
2007 entry->marked_count = max(count/IMSM_4K_DIV, 1) - 1;
2008 }
2009 }
f36a9ecd
PB
2010
2011 mpb->check_sum = __gen_imsm_checksum(mpb);
2012}
2013
520e69e2
AK
2014void examine_migr_rec_imsm(struct intel_super *super)
2015{
2016 struct migr_record *migr_rec = super->migr_rec;
2017 struct imsm_super *mpb = super->anchor;
2018 int i;
2019
2020 for (i = 0; i < mpb->num_raid_devs; i++) {
2021 struct imsm_dev *dev = __get_imsm_dev(mpb, i);
3136abe5 2022 struct imsm_map *map;
b4ab44d8 2023 int slot = -1;
3136abe5 2024
195d1d76 2025 if (is_gen_migration(dev) == false)
520e69e2
AK
2026 continue;
2027
2028 printf("\nMigration Record Information:");
3136abe5 2029
44bfe6df
AK
2030 /* first map under migration */
2031 map = get_imsm_map(dev, MAP_0);
76c152ca 2032
3136abe5
AK
2033 if (map)
2034 slot = get_imsm_disk_slot(map, super->disks->index);
089f9d79 2035 if (map == NULL || slot > 1 || slot < 0) {
520e69e2
AK
2036 printf(" Empty\n ");
2037 printf("Examine one of first two disks in array\n");
2038 break;
2039 }
2040 printf("\n Status : ");
2041 if (__le32_to_cpu(migr_rec->rec_status) == UNIT_SRC_NORMAL)
2042 printf("Normal\n");
2043 else
2044 printf("Contains Data\n");
9f421827
PB
2045 printf(" Current Unit : %llu\n",
2046 current_migr_unit(migr_rec));
520e69e2
AK
2047 printf(" Family : %u\n",
2048 __le32_to_cpu(migr_rec->family_num));
2049 printf(" Ascending : %u\n",
2050 __le32_to_cpu(migr_rec->ascending_migr));
2051 printf(" Blocks Per Unit : %u\n",
2052 __le32_to_cpu(migr_rec->blocks_per_unit));
2053 printf(" Dest. Depth Per Unit : %u\n",
2054 __le32_to_cpu(migr_rec->dest_depth_per_unit));
9f421827
PB
2055 printf(" Checkpoint Area pba : %llu\n",
2056 migr_chkp_area_pba(migr_rec));
2057 printf(" First member lba : %llu\n",
2058 migr_dest_1st_member_lba(migr_rec));
2059 printf(" Total Number of Units : %llu\n",
2060 get_num_migr_units(migr_rec));
2061 printf(" Size of volume : %llu\n",
2062 join_u32(migr_rec->post_migr_vol_cap,
2063 migr_rec->post_migr_vol_cap_hi));
520e69e2
AK
2064 printf(" Record was read from : %u\n",
2065 __le32_to_cpu(migr_rec->ckpt_read_disk_num));
2066
2067 break;
2068 }
2069}
f36a9ecd 2070
de44e46f
PB
2071void convert_from_4k_imsm_migr_rec(struct intel_super *super)
2072{
2073 struct migr_record *migr_rec = super->migr_rec;
2074
2075 migr_rec->blocks_per_unit *= IMSM_4K_DIV;
de44e46f
PB
2076 migr_rec->dest_depth_per_unit *= IMSM_4K_DIV;
2077 split_ull((join_u32(migr_rec->post_migr_vol_cap,
2078 migr_rec->post_migr_vol_cap_hi) * IMSM_4K_DIV),
2079 &migr_rec->post_migr_vol_cap,
2080 &migr_rec->post_migr_vol_cap_hi);
9f421827
PB
2081 set_migr_chkp_area_pba(migr_rec,
2082 migr_chkp_area_pba(migr_rec) * IMSM_4K_DIV);
2083 set_migr_dest_1st_member_lba(migr_rec,
2084 migr_dest_1st_member_lba(migr_rec) * IMSM_4K_DIV);
de44e46f
PB
2085}
2086
f36a9ecd
PB
2087void convert_from_4k(struct intel_super *super)
2088{
2089 struct imsm_super *mpb = super->anchor;
2090 struct imsm_disk *disk;
2091 int i;
e4467bc7 2092 __u32 bbm_log_size = __le32_to_cpu(mpb->bbm_log_size);
f36a9ecd
PB
2093
2094 for (i = 0; i < mpb->num_disks ; i++) {
2095 disk = __get_imsm_disk(mpb, i);
2096 /* disk */
2097 set_total_blocks(disk, (total_blocks(disk)*IMSM_4K_DIV));
2098 }
2099
2100 for (i = 0; i < mpb->num_raid_devs; i++) {
2101 struct imsm_dev *dev = __get_imsm_dev(mpb, i);
2102 struct imsm_map *map = get_imsm_map(dev, MAP_0);
2103 /* dev */
fcc2c9da 2104 set_imsm_dev_size(dev, imsm_dev_size(dev)*IMSM_4K_DIV);
4036e7ee
MT
2105 set_vol_curr_migr_unit(dev,
2106 vol_curr_migr_unit(dev) * IMSM_4K_DIV);
f36a9ecd
PB
2107
2108 /* map0 */
2109 set_blocks_per_member(map, blocks_per_member(map)*IMSM_4K_DIV);
2110 map->blocks_per_strip *= IMSM_4K_DIV;
2111 set_pba_of_lba0(map, pba_of_lba0(map)*IMSM_4K_DIV);
2112
2113 if (dev->vol.migr_state) {
2114 /* map1 */
2115 map = get_imsm_map(dev, MAP_1);
2116 set_blocks_per_member(map,
2117 blocks_per_member(map)*IMSM_4K_DIV);
2118 map->blocks_per_strip *= IMSM_4K_DIV;
2119 set_pba_of_lba0(map, pba_of_lba0(map)*IMSM_4K_DIV);
2120 }
2121 }
e4467bc7
TM
2122 if (bbm_log_size) {
2123 struct bbm_log *log = (void *)mpb +
2124 __le32_to_cpu(mpb->mpb_size) - bbm_log_size;
2125 __u32 i;
2126
2127 for (i = 0; i < log->entry_count; i++) {
2128 struct bbm_log_entry *entry =
2129 &log->marked_block_entries[i];
2130
2131 __u8 count = entry->marked_count + 1;
2132 unsigned long long sector =
2133 __le48_to_cpu(&entry->defective_block_start);
2134
2135 entry->defective_block_start =
2136 __cpu_to_le48(sector*IMSM_4K_DIV);
2137 entry->marked_count = count*IMSM_4K_DIV - 1;
2138 }
2139 }
f36a9ecd
PB
2140
2141 mpb->check_sum = __gen_imsm_checksum(mpb);
2142}
2143
19482bcc
AK
2144/*******************************************************************************
2145 * function: imsm_check_attributes
2146 * Description: Function checks if features represented by attributes flags
1011e834 2147 * are supported by mdadm.
19482bcc
AK
2148 * Parameters:
2149 * attributes - Attributes read from metadata
2150 * Returns:
1011e834
N
2151 * 0 - passed attributes contains unsupported features flags
2152 * 1 - all features are supported
19482bcc
AK
2153 ******************************************************************************/
2154static int imsm_check_attributes(__u32 attributes)
2155{
2156 int ret_val = 1;
418f9b36
N
2157 __u32 not_supported = MPB_ATTRIB_SUPPORTED^0xffffffff;
2158
2159 not_supported &= ~MPB_ATTRIB_IGNORED;
19482bcc
AK
2160
2161 not_supported &= attributes;
2162 if (not_supported) {
e7b84f9d 2163 pr_err("(IMSM): Unsupported attributes : %x\n",
418f9b36 2164 (unsigned)__le32_to_cpu(not_supported));
19482bcc
AK
2165 if (not_supported & MPB_ATTRIB_CHECKSUM_VERIFY) {
2166 dprintf("\t\tMPB_ATTRIB_CHECKSUM_VERIFY \n");
2167 not_supported ^= MPB_ATTRIB_CHECKSUM_VERIFY;
2168 }
2169 if (not_supported & MPB_ATTRIB_2TB) {
2170 dprintf("\t\tMPB_ATTRIB_2TB\n");
2171 not_supported ^= MPB_ATTRIB_2TB;
2172 }
2173 if (not_supported & MPB_ATTRIB_RAID0) {
2174 dprintf("\t\tMPB_ATTRIB_RAID0\n");
2175 not_supported ^= MPB_ATTRIB_RAID0;
2176 }
2177 if (not_supported & MPB_ATTRIB_RAID1) {
2178 dprintf("\t\tMPB_ATTRIB_RAID1\n");
2179 not_supported ^= MPB_ATTRIB_RAID1;
2180 }
2181 if (not_supported & MPB_ATTRIB_RAID10) {
2182 dprintf("\t\tMPB_ATTRIB_RAID10\n");
2183 not_supported ^= MPB_ATTRIB_RAID10;
2184 }
2185 if (not_supported & MPB_ATTRIB_RAID1E) {
2186 dprintf("\t\tMPB_ATTRIB_RAID1E\n");
2187 not_supported ^= MPB_ATTRIB_RAID1E;
2188 }
2189 if (not_supported & MPB_ATTRIB_RAID5) {
2190 dprintf("\t\tMPB_ATTRIB_RAID5\n");
2191 not_supported ^= MPB_ATTRIB_RAID5;
2192 }
2193 if (not_supported & MPB_ATTRIB_RAIDCNG) {
2194 dprintf("\t\tMPB_ATTRIB_RAIDCNG\n");
2195 not_supported ^= MPB_ATTRIB_RAIDCNG;
2196 }
2197 if (not_supported & MPB_ATTRIB_BBM) {
2198 dprintf("\t\tMPB_ATTRIB_BBM\n");
2199 not_supported ^= MPB_ATTRIB_BBM;
2200 }
2201 if (not_supported & MPB_ATTRIB_CHECKSUM_VERIFY) {
2202 dprintf("\t\tMPB_ATTRIB_CHECKSUM_VERIFY (== MPB_ATTRIB_LEGACY)\n");
2203 not_supported ^= MPB_ATTRIB_CHECKSUM_VERIFY;
2204 }
2205 if (not_supported & MPB_ATTRIB_EXP_STRIPE_SIZE) {
2206 dprintf("\t\tMPB_ATTRIB_EXP_STRIP_SIZE\n");
2207 not_supported ^= MPB_ATTRIB_EXP_STRIPE_SIZE;
2208 }
2209 if (not_supported & MPB_ATTRIB_2TB_DISK) {
2210 dprintf("\t\tMPB_ATTRIB_2TB_DISK\n");
2211 not_supported ^= MPB_ATTRIB_2TB_DISK;
2212 }
2213 if (not_supported & MPB_ATTRIB_NEVER_USE2) {
2214 dprintf("\t\tMPB_ATTRIB_NEVER_USE2\n");
2215 not_supported ^= MPB_ATTRIB_NEVER_USE2;
2216 }
2217 if (not_supported & MPB_ATTRIB_NEVER_USE) {
2218 dprintf("\t\tMPB_ATTRIB_NEVER_USE\n");
2219 not_supported ^= MPB_ATTRIB_NEVER_USE;
2220 }
2221
2222 if (not_supported)
1ade5cc1 2223 dprintf("(IMSM): Unknown attributes : %x\n", not_supported);
19482bcc
AK
2224
2225 ret_val = 0;
2226 }
2227
2228 return ret_val;
2229}
2230
a5d85af7 2231static void getinfo_super_imsm(struct supertype *st, struct mdinfo *info, char *map);
44470971 2232
cdddbdbc
DW
2233static void examine_super_imsm(struct supertype *st, char *homehost)
2234{
2235 struct intel_super *super = st->sb;
949c47a0 2236 struct imsm_super *mpb = super->anchor;
cdddbdbc
DW
2237 char str[MAX_SIGNATURE_LENGTH];
2238 int i;
27fd6274
DW
2239 struct mdinfo info;
2240 char nbuf[64];
cdddbdbc 2241 __u32 sum;
14e8215b 2242 __u32 reserved = imsm_reserved_sectors(super, super->disks);
94827db3 2243 struct dl *dl;
e48aed3c 2244 time_t creation_time;
27fd6274 2245
618f4e6d
XN
2246 strncpy(str, (char *)mpb->sig, MPB_SIG_LEN);
2247 str[MPB_SIG_LEN-1] = '\0';
cdddbdbc 2248 printf(" Magic : %s\n", str);
cdddbdbc 2249 printf(" Version : %s\n", get_imsm_version(mpb));
148acb7b 2250 printf(" Orig Family : %08x\n", __le32_to_cpu(mpb->orig_family_num));
cdddbdbc
DW
2251 printf(" Family : %08x\n", __le32_to_cpu(mpb->family_num));
2252 printf(" Generation : %08x\n", __le32_to_cpu(mpb->generation_num));
e48aed3c
AP
2253 creation_time = __le64_to_cpu(mpb->creation_time);
2254 printf(" Creation Time : %.24s\n",
2255 creation_time ? ctime(&creation_time) : "Unknown");
19482bcc
AK
2256 printf(" Attributes : ");
2257 if (imsm_check_attributes(mpb->attributes))
2258 printf("All supported\n");
2259 else
2260 printf("not supported\n");
a5d85af7 2261 getinfo_super_imsm(st, &info, NULL);
ae2bfd4e 2262 fname_from_uuid(st, &info, nbuf, ':');
27fd6274 2263 printf(" UUID : %s\n", nbuf + 5);
cdddbdbc
DW
2264 sum = __le32_to_cpu(mpb->check_sum);
2265 printf(" Checksum : %08x %s\n", sum,
949c47a0 2266 __gen_imsm_checksum(mpb) == sum ? "correct" : "incorrect");
f36a9ecd 2267 printf(" MPB Sectors : %d\n", mpb_sectors(mpb, super->sector_size));
cdddbdbc
DW
2268 printf(" Disks : %d\n", mpb->num_disks);
2269 printf(" RAID Devices : %d\n", mpb->num_raid_devs);
ef5c214e
MK
2270 print_imsm_disk(__get_imsm_disk(mpb, super->disks->index),
2271 super->disks->index, reserved, super->sector_size);
8d67477f 2272 if (get_imsm_bbm_log_size(super->bbm_log)) {
604b746f
JD
2273 struct bbm_log *log = super->bbm_log;
2274
2275 printf("\n");
2276 printf("Bad Block Management Log:\n");
2277 printf(" Log Size : %d\n", __le32_to_cpu(mpb->bbm_log_size));
2278 printf(" Signature : %x\n", __le32_to_cpu(log->signature));
2279 printf(" Entry Count : %d\n", __le32_to_cpu(log->entry_count));
604b746f 2280 }
44470971
DW
2281 for (i = 0; i < mpb->num_raid_devs; i++) {
2282 struct mdinfo info;
2283 struct imsm_dev *dev = __get_imsm_dev(mpb, i);
2284
2285 super->current_vol = i;
a5d85af7 2286 getinfo_super_imsm(st, &info, NULL);
ae2bfd4e 2287 fname_from_uuid(st, &info, nbuf, ':');
c47b0ff6 2288 print_imsm_dev(super, dev, nbuf + 5, super->disks->index);
44470971 2289 }
cdddbdbc
DW
2290 for (i = 0; i < mpb->num_disks; i++) {
2291 if (i == super->disks->index)
2292 continue;
ef5c214e
MK
2293 print_imsm_disk(__get_imsm_disk(mpb, i), i, reserved,
2294 super->sector_size);
cdddbdbc 2295 }
94827db3 2296
0ec1f4e8
DW
2297 for (dl = super->disks; dl; dl = dl->next)
2298 if (dl->index == -1)
ef5c214e
MK
2299 print_imsm_disk(&dl->disk, -1, reserved,
2300 super->sector_size);
520e69e2
AK
2301
2302 examine_migr_rec_imsm(super);
cdddbdbc
DW
2303}
2304
061f2c6a 2305static void brief_examine_super_imsm(struct supertype *st, int verbose)
cdddbdbc 2306{
27fd6274 2307 /* We just write a generic IMSM ARRAY entry */
ff54de6e
N
2308 struct mdinfo info;
2309 char nbuf[64];
2310
a5d85af7 2311 getinfo_super_imsm(st, &info, NULL);
4737ae25
N
2312 fname_from_uuid(st, &info, nbuf, ':');
2313 printf("ARRAY metadata=imsm UUID=%s\n", nbuf + 5);
2314}
2315
2316static void brief_examine_subarrays_imsm(struct supertype *st, int verbose)
2317{
2318 /* We just write a generic IMSM ARRAY entry */
2319 struct mdinfo info;
2320 char nbuf[64];
2321 char nbuf1[64];
2322 struct intel_super *super = st->sb;
2323 int i;
2324
2325 if (!super->anchor->num_raid_devs)
2326 return;
2327
a5d85af7 2328 getinfo_super_imsm(st, &info, NULL);
ae2bfd4e 2329 fname_from_uuid(st, &info, nbuf, ':');
1e7bc0ed
DW
2330 for (i = 0; i < super->anchor->num_raid_devs; i++) {
2331 struct imsm_dev *dev = get_imsm_dev(super, i);
2332
2333 super->current_vol = i;
a5d85af7 2334 getinfo_super_imsm(st, &info, NULL);
ae2bfd4e 2335 fname_from_uuid(st, &info, nbuf1, ':');
b9ce7ab0 2336 printf("ARRAY " DEV_MD_DIR "%.16s container=%s member=%d UUID=%s\n",
cf8de691 2337 dev->volume, nbuf + 5, i, nbuf1 + 5);
1e7bc0ed 2338 }
cdddbdbc
DW
2339}
2340
9d84c8ea
DW
2341static void export_examine_super_imsm(struct supertype *st)
2342{
2343 struct intel_super *super = st->sb;
2344 struct imsm_super *mpb = super->anchor;
2345 struct mdinfo info;
2346 char nbuf[64];
2347
a5d85af7 2348 getinfo_super_imsm(st, &info, NULL);
9d84c8ea
DW
2349 fname_from_uuid(st, &info, nbuf, ':');
2350 printf("MD_METADATA=imsm\n");
2351 printf("MD_LEVEL=container\n");
2352 printf("MD_UUID=%s\n", nbuf+5);
2353 printf("MD_DEVICES=%u\n", mpb->num_disks);
e48aed3c 2354 printf("MD_CREATION_TIME=%llu\n", __le64_to_cpu(mpb->creation_time));
9d84c8ea
DW
2355}
2356
b771faef
BK
2357static void detail_super_imsm(struct supertype *st, char *homehost,
2358 char *subarray)
cdddbdbc 2359{
3ebe00a1
DW
2360 struct mdinfo info;
2361 char nbuf[64];
b771faef
BK
2362 struct intel_super *super = st->sb;
2363 int temp_vol = super->current_vol;
2364
2365 if (subarray)
2366 super->current_vol = strtoul(subarray, NULL, 10);
3ebe00a1 2367
a5d85af7 2368 getinfo_super_imsm(st, &info, NULL);
ae2bfd4e 2369 fname_from_uuid(st, &info, nbuf, ':');
65884368 2370 printf("\n UUID : %s\n", nbuf + 5);
b771faef
BK
2371
2372 super->current_vol = temp_vol;
cdddbdbc
DW
2373}
2374
b771faef 2375static void brief_detail_super_imsm(struct supertype *st, char *subarray)
cdddbdbc 2376{
ff54de6e
N
2377 struct mdinfo info;
2378 char nbuf[64];
b771faef
BK
2379 struct intel_super *super = st->sb;
2380 int temp_vol = super->current_vol;
2381
2382 if (subarray)
2383 super->current_vol = strtoul(subarray, NULL, 10);
2384
a5d85af7 2385 getinfo_super_imsm(st, &info, NULL);
ae2bfd4e 2386 fname_from_uuid(st, &info, nbuf, ':');
ff54de6e 2387 printf(" UUID=%s", nbuf + 5);
b771faef
BK
2388
2389 super->current_vol = temp_vol;
cdddbdbc 2390}
d665cc31 2391
6da53c0e
BK
2392static int imsm_read_serial(int fd, char *devname, __u8 *serial,
2393 size_t serial_buf_len);
d665cc31
DW
2394static void fd2devname(int fd, char *name);
2395
120dc887 2396static int ahci_enumerate_ports(const char *hba_path, int port_count, int host_base, int verbose)
d665cc31 2397{
120dc887
LM
2398 /* dump an unsorted list of devices attached to AHCI Intel storage
2399 * controller, as well as non-connected ports
d665cc31
DW
2400 */
2401 int hba_len = strlen(hba_path) + 1;
2402 struct dirent *ent;
2403 DIR *dir;
2404 char *path = NULL;
2405 int err = 0;
2406 unsigned long port_mask = (1 << port_count) - 1;
2407
f21e18ca 2408 if (port_count > (int)sizeof(port_mask) * 8) {
ba728be7 2409 if (verbose > 0)
e7b84f9d 2410 pr_err("port_count %d out of range\n", port_count);
d665cc31
DW
2411 return 2;
2412 }
2413
2414 /* scroll through /sys/dev/block looking for devices attached to
2415 * this hba
2416 */
2417 dir = opendir("/sys/dev/block");
1a6dd6b9
PB
2418 if (!dir)
2419 return 1;
2420
2421 for (ent = readdir(dir); ent; ent = readdir(dir)) {
d665cc31
DW
2422 int fd;
2423 char model[64];
2424 char vendor[64];
2425 char buf[1024];
2426 int major, minor;
fcebeb77 2427 char device[PATH_MAX];
d665cc31
DW
2428 char *c;
2429 int port;
2430 int type;
2431
2432 if (sscanf(ent->d_name, "%d:%d", &major, &minor) != 2)
2433 continue;
7c798f87 2434 path = devt_to_devpath(makedev(major, minor), 1, NULL);
d665cc31
DW
2435 if (!path)
2436 continue;
2437 if (!path_attached_to_hba(path, hba_path)) {
2438 free(path);
2439 path = NULL;
2440 continue;
2441 }
2442
fcebeb77
MT
2443 /* retrieve the scsi device */
2444 if (!devt_to_devpath(makedev(major, minor), 1, device)) {
ba728be7 2445 if (verbose > 0)
fcebeb77 2446 pr_err("failed to get device\n");
d665cc31
DW
2447 err = 2;
2448 break;
2449 }
fcebeb77 2450 if (devpath_to_char(device, "type", buf, sizeof(buf), 0)) {
d665cc31 2451 err = 2;
d665cc31
DW
2452 break;
2453 }
2454 type = strtoul(buf, NULL, 10);
2455
2456 /* if it's not a disk print the vendor and model */
2457 if (!(type == 0 || type == 7 || type == 14)) {
2458 vendor[0] = '\0';
2459 model[0] = '\0';
fcebeb77
MT
2460
2461 if (devpath_to_char(device, "vendor", buf,
2462 sizeof(buf), 0) == 0) {
d665cc31
DW
2463 strncpy(vendor, buf, sizeof(vendor));
2464 vendor[sizeof(vendor) - 1] = '\0';
2465 c = (char *) &vendor[sizeof(vendor) - 1];
2466 while (isspace(*c) || *c == '\0')
2467 *c-- = '\0';
2468
2469 }
fcebeb77
MT
2470
2471 if (devpath_to_char(device, "model", buf,
2472 sizeof(buf), 0) == 0) {
d665cc31
DW
2473 strncpy(model, buf, sizeof(model));
2474 model[sizeof(model) - 1] = '\0';
2475 c = (char *) &model[sizeof(model) - 1];
2476 while (isspace(*c) || *c == '\0')
2477 *c-- = '\0';
2478 }
2479
2480 if (vendor[0] && model[0])
2481 sprintf(buf, "%.64s %.64s", vendor, model);
2482 else
2483 switch (type) { /* numbers from hald/linux/device.c */
2484 case 1: sprintf(buf, "tape"); break;
2485 case 2: sprintf(buf, "printer"); break;
2486 case 3: sprintf(buf, "processor"); break;
2487 case 4:
2488 case 5: sprintf(buf, "cdrom"); break;
2489 case 6: sprintf(buf, "scanner"); break;
2490 case 8: sprintf(buf, "media_changer"); break;
2491 case 9: sprintf(buf, "comm"); break;
2492 case 12: sprintf(buf, "raid"); break;
2493 default: sprintf(buf, "unknown");
2494 }
2495 } else
2496 buf[0] = '\0';
d665cc31
DW
2497
2498 /* chop device path to 'host%d' and calculate the port number */
2499 c = strchr(&path[hba_len], '/');
4e5e717d 2500 if (!c) {
ba728be7 2501 if (verbose > 0)
e7b84f9d 2502 pr_err("%s - invalid path name\n", path + hba_len);
4e5e717d
AW
2503 err = 2;
2504 break;
2505 }
d665cc31 2506 *c = '\0';
0858eccf
AP
2507 if ((sscanf(&path[hba_len], "ata%d", &port) == 1) ||
2508 ((sscanf(&path[hba_len], "host%d", &port) == 1)))
d665cc31
DW
2509 port -= host_base;
2510 else {
ba728be7 2511 if (verbose > 0) {
d665cc31 2512 *c = '/'; /* repair the full string */
e7b84f9d 2513 pr_err("failed to determine port number for %s\n",
d665cc31
DW
2514 path);
2515 }
2516 err = 2;
2517 break;
2518 }
2519
2520 /* mark this port as used */
2521 port_mask &= ~(1 << port);
2522
2523 /* print out the device information */
2524 if (buf[0]) {
2525 printf(" Port%d : - non-disk device (%s) -\n", port, buf);
2526 continue;
2527 }
2528
2529 fd = dev_open(ent->d_name, O_RDONLY);
4389ce73 2530 if (!is_fd_valid(fd))
d665cc31
DW
2531 printf(" Port%d : - disk info unavailable -\n", port);
2532 else {
2533 fd2devname(fd, buf);
2534 printf(" Port%d : %s", port, buf);
6da53c0e
BK
2535 if (imsm_read_serial(fd, NULL, (__u8 *)buf,
2536 sizeof(buf)) == 0)
2537 printf(" (%s)\n", buf);
d665cc31 2538 else
664d5325 2539 printf(" ()\n");
4dab422a 2540 close(fd);
d665cc31 2541 }
d665cc31
DW
2542 free(path);
2543 path = NULL;
2544 }
2545 if (path)
2546 free(path);
2547 if (dir)
2548 closedir(dir);
2549 if (err == 0) {
2550 int i;
2551
2552 for (i = 0; i < port_count; i++)
2553 if (port_mask & (1 << i))
2554 printf(" Port%d : - no device attached -\n", i);
2555 }
2556
2557 return err;
2558}
2559
6da53c0e 2560static int print_nvme_info(struct sys_dev *hba)
60f0f54d
PB
2561{
2562 struct dirent *ent;
2563 DIR *dir;
60f0f54d 2564
6da53c0e 2565 dir = opendir("/sys/block/");
b9135011 2566 if (!dir)
b5eece69 2567 return 1;
b9135011
JS
2568
2569 for (ent = readdir(dir); ent; ent = readdir(dir)) {
8662f92d
MT
2570 char ns_path[PATH_MAX];
2571 char cntrl_path[PATH_MAX];
2572 char buf[PATH_MAX];
2573 int fd = -1;
60f0f54d 2574
8662f92d
MT
2575 if (!strstr(ent->d_name, "nvme"))
2576 goto skip;
d835518b 2577
8662f92d 2578 fd = open_dev(ent->d_name);
4389ce73 2579 if (!is_fd_valid(fd))
8662f92d 2580 goto skip;
d835518b 2581
8662f92d
MT
2582 if (!diskfd_to_devpath(fd, 0, ns_path) ||
2583 !diskfd_to_devpath(fd, 1, cntrl_path))
2584 goto skip;
2585
2586 if (!path_attached_to_hba(cntrl_path, hba->path))
2587 goto skip;
2588
2589 if (!imsm_is_nvme_namespace_supported(fd, 0))
2590 goto skip;
2591
2592 fd2devname(fd, buf);
2593 if (hba->type == SYS_DEV_VMD)
2594 printf(" NVMe under VMD : %s", buf);
2595 else if (hba->type == SYS_DEV_NVME)
2596 printf(" NVMe Device : %s", buf);
2597
2598 if (!imsm_read_serial(fd, NULL, (__u8 *)buf,
2599 sizeof(buf)))
2600 printf(" (%s)\n", buf);
2601 else
2602 printf("()\n");
2603
2604skip:
4389ce73 2605 close_fd(&fd);
60f0f54d
PB
2606 }
2607
b9135011 2608 closedir(dir);
b5eece69 2609 return 0;
60f0f54d
PB
2610}
2611
120dc887
LM
2612static void print_found_intel_controllers(struct sys_dev *elem)
2613{
2614 for (; elem; elem = elem->next) {
e7b84f9d 2615 pr_err("found Intel(R) ");
120dc887
LM
2616 if (elem->type == SYS_DEV_SATA)
2617 fprintf(stderr, "SATA ");
155cbb4c
LM
2618 else if (elem->type == SYS_DEV_SAS)
2619 fprintf(stderr, "SAS ");
0858eccf
AP
2620 else if (elem->type == SYS_DEV_NVME)
2621 fprintf(stderr, "NVMe ");
60f0f54d
PB
2622
2623 if (elem->type == SYS_DEV_VMD)
2624 fprintf(stderr, "VMD domain");
75350d87
KF
2625 else if (elem->type == SYS_DEV_SATA_VMD)
2626 fprintf(stderr, "SATA VMD domain");
60f0f54d
PB
2627 else
2628 fprintf(stderr, "RAID controller");
2629
120dc887
LM
2630 if (elem->pci_id)
2631 fprintf(stderr, " at %s", elem->pci_id);
2632 fprintf(stderr, ".\n");
2633 }
2634 fflush(stderr);
2635}
2636
120dc887
LM
2637static int ahci_get_port_count(const char *hba_path, int *port_count)
2638{
2639 struct dirent *ent;
2640 DIR *dir;
2641 int host_base = -1;
2642
2643 *port_count = 0;
2644 if ((dir = opendir(hba_path)) == NULL)
2645 return -1;
2646
2647 for (ent = readdir(dir); ent; ent = readdir(dir)) {
2648 int host;
2649
0858eccf
AP
2650 if ((sscanf(ent->d_name, "ata%d", &host) != 1) &&
2651 ((sscanf(ent->d_name, "host%d", &host) != 1)))
120dc887
LM
2652 continue;
2653 if (*port_count == 0)
2654 host_base = host;
2655 else if (host < host_base)
2656 host_base = host;
2657
2658 if (host + 1 > *port_count + host_base)
2659 *port_count = host + 1 - host_base;
2660 }
2661 closedir(dir);
2662 return host_base;
2663}
2664
a891a3c2
LM
2665static void print_imsm_capability(const struct imsm_orom *orom)
2666{
0858eccf
AP
2667 printf(" Platform : Intel(R) ");
2668 if (orom->capabilities == 0 && orom->driver_features == 0)
2669 printf("Matrix Storage Manager\n");
ab0c6bb9
AP
2670 else if (imsm_orom_is_enterprise(orom) && orom->major_ver >= 6)
2671 printf("Virtual RAID on CPU\n");
0858eccf
AP
2672 else
2673 printf("Rapid Storage Technology%s\n",
2674 imsm_orom_is_enterprise(orom) ? " enterprise" : "");
8d1114be
MG
2675 if (orom->major_ver || orom->minor_ver || orom->hotfix_ver || orom->build) {
2676 if (imsm_orom_is_vmd_without_efi(orom))
2677 printf(" Version : %d.%d\n", orom->major_ver,
2678 orom->minor_ver);
2679 else
2680 printf(" Version : %d.%d.%d.%d\n", orom->major_ver,
2681 orom->minor_ver, orom->hotfix_ver, orom->build);
2682 }
a891a3c2
LM
2683 printf(" RAID Levels :%s%s%s%s%s\n",
2684 imsm_orom_has_raid0(orom) ? " raid0" : "",
2685 imsm_orom_has_raid1(orom) ? " raid1" : "",
2686 imsm_orom_has_raid1e(orom) ? " raid1e" : "",
2687 imsm_orom_has_raid10(orom) ? " raid10" : "",
2688 imsm_orom_has_raid5(orom) ? " raid5" : "");
2689 printf(" Chunk Sizes :%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
2690 imsm_orom_has_chunk(orom, 2) ? " 2k" : "",
2691 imsm_orom_has_chunk(orom, 4) ? " 4k" : "",
2692 imsm_orom_has_chunk(orom, 8) ? " 8k" : "",
2693 imsm_orom_has_chunk(orom, 16) ? " 16k" : "",
2694 imsm_orom_has_chunk(orom, 32) ? " 32k" : "",
2695 imsm_orom_has_chunk(orom, 64) ? " 64k" : "",
2696 imsm_orom_has_chunk(orom, 128) ? " 128k" : "",
2697 imsm_orom_has_chunk(orom, 256) ? " 256k" : "",
2698 imsm_orom_has_chunk(orom, 512) ? " 512k" : "",
2699 imsm_orom_has_chunk(orom, 1024*1) ? " 1M" : "",
2700 imsm_orom_has_chunk(orom, 1024*2) ? " 2M" : "",
2701 imsm_orom_has_chunk(orom, 1024*4) ? " 4M" : "",
2702 imsm_orom_has_chunk(orom, 1024*8) ? " 8M" : "",
2703 imsm_orom_has_chunk(orom, 1024*16) ? " 16M" : "",
2704 imsm_orom_has_chunk(orom, 1024*32) ? " 32M" : "",
2705 imsm_orom_has_chunk(orom, 1024*64) ? " 64M" : "");
29cd0821
CA
2706 printf(" 2TB volumes :%s supported\n",
2707 (orom->attr & IMSM_OROM_ATTR_2TB)?"":" not");
2708 printf(" 2TB disks :%s supported\n",
2709 (orom->attr & IMSM_OROM_ATTR_2TB_DISK)?"":" not");
0e7f69a8 2710 printf(" Max Disks : %d\n", orom->tds);
0858eccf
AP
2711 printf(" Max Volumes : %d per array, %d per %s\n",
2712 orom->vpa, orom->vphba,
2713 imsm_orom_is_nvme(orom) ? "platform" : "controller");
a891a3c2
LM
2714 return;
2715}
2716
e50cf220
MN
2717static void print_imsm_capability_export(const struct imsm_orom *orom)
2718{
2719 printf("MD_FIRMWARE_TYPE=imsm\n");
0858eccf
AP
2720 if (orom->major_ver || orom->minor_ver || orom->hotfix_ver || orom->build)
2721 printf("IMSM_VERSION=%d.%d.%d.%d\n", orom->major_ver, orom->minor_ver,
2722 orom->hotfix_ver, orom->build);
e50cf220
MN
2723 printf("IMSM_SUPPORTED_RAID_LEVELS=%s%s%s%s%s\n",
2724 imsm_orom_has_raid0(orom) ? "raid0 " : "",
2725 imsm_orom_has_raid1(orom) ? "raid1 " : "",
2726 imsm_orom_has_raid1e(orom) ? "raid1e " : "",
2727 imsm_orom_has_raid5(orom) ? "raid10 " : "",
2728 imsm_orom_has_raid10(orom) ? "raid5 " : "");
2729 printf("IMSM_SUPPORTED_CHUNK_SIZES=%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
2730 imsm_orom_has_chunk(orom, 2) ? "2k " : "",
2731 imsm_orom_has_chunk(orom, 4) ? "4k " : "",
2732 imsm_orom_has_chunk(orom, 8) ? "8k " : "",
2733 imsm_orom_has_chunk(orom, 16) ? "16k " : "",
2734 imsm_orom_has_chunk(orom, 32) ? "32k " : "",
2735 imsm_orom_has_chunk(orom, 64) ? "64k " : "",
2736 imsm_orom_has_chunk(orom, 128) ? "128k " : "",
2737 imsm_orom_has_chunk(orom, 256) ? "256k " : "",
2738 imsm_orom_has_chunk(orom, 512) ? "512k " : "",
2739 imsm_orom_has_chunk(orom, 1024*1) ? "1M " : "",
2740 imsm_orom_has_chunk(orom, 1024*2) ? "2M " : "",
2741 imsm_orom_has_chunk(orom, 1024*4) ? "4M " : "",
2742 imsm_orom_has_chunk(orom, 1024*8) ? "8M " : "",
2743 imsm_orom_has_chunk(orom, 1024*16) ? "16M " : "",
2744 imsm_orom_has_chunk(orom, 1024*32) ? "32M " : "",
2745 imsm_orom_has_chunk(orom, 1024*64) ? "64M " : "");
2746 printf("IMSM_2TB_VOLUMES=%s\n",(orom->attr & IMSM_OROM_ATTR_2TB) ? "yes" : "no");
2747 printf("IMSM_2TB_DISKS=%s\n",(orom->attr & IMSM_OROM_ATTR_2TB_DISK) ? "yes" : "no");
2748 printf("IMSM_MAX_DISKS=%d\n",orom->tds);
2749 printf("IMSM_MAX_VOLUMES_PER_ARRAY=%d\n",orom->vpa);
2750 printf("IMSM_MAX_VOLUMES_PER_CONTROLLER=%d\n",orom->vphba);
2751}
2752
9eafa1de 2753static int detail_platform_imsm(int verbose, int enumerate_only, char *controller_path)
d665cc31
DW
2754{
2755 /* There are two components to imsm platform support, the ahci SATA
2756 * controller and the option-rom. To find the SATA controller we
2757 * simply look in /sys/bus/pci/drivers/ahci to see if an ahci
2758 * controller with the Intel vendor id is present. This approach
2759 * allows mdadm to leverage the kernel's ahci detection logic, with the
2760 * caveat that if ahci.ko is not loaded mdadm will not be able to
2761 * detect platform raid capabilities. The option-rom resides in a
2762 * platform "Adapter ROM". We scan for its signature to retrieve the
2763 * platform capabilities. If raid support is disabled in the BIOS the
2764 * option-rom capability structure will not be available.
2765 */
d665cc31 2766 struct sys_dev *list, *hba;
d665cc31
DW
2767 int host_base = 0;
2768 int port_count = 0;
9eafa1de 2769 int result=1;
d665cc31 2770
5615172f 2771 if (enumerate_only) {
420dafcd 2772 if (check_no_platform())
5615172f 2773 return 0;
a891a3c2
LM
2774 list = find_intel_devices();
2775 if (!list)
2776 return 2;
2777 for (hba = list; hba; hba = hba->next) {
6b781d33
AP
2778 if (find_imsm_capability(hba)) {
2779 result = 0;
a891a3c2
LM
2780 break;
2781 }
9eafa1de 2782 else
6b781d33 2783 result = 2;
a891a3c2 2784 }
a891a3c2 2785 return result;
5615172f
DW
2786 }
2787
155cbb4c
LM
2788 list = find_intel_devices();
2789 if (!list) {
ba728be7 2790 if (verbose > 0)
7a862a02 2791 pr_err("no active Intel(R) RAID controller found.\n");
d665cc31 2792 return 2;
ba728be7 2793 } else if (verbose > 0)
155cbb4c 2794 print_found_intel_controllers(list);
d665cc31 2795
a891a3c2 2796 for (hba = list; hba; hba = hba->next) {
0858eccf 2797 if (controller_path && (compare_paths(hba->path, controller_path) != 0))
9eafa1de 2798 continue;
0858eccf 2799 if (!find_imsm_capability(hba)) {
60f0f54d 2800 char buf[PATH_MAX];
e7b84f9d 2801 pr_err("imsm capabilities not found for controller: %s (type %s)\n",
75350d87
KF
2802 hba->type == SYS_DEV_VMD || hba->type == SYS_DEV_SATA_VMD ?
2803 vmd_domain_to_controller(hba, buf) :
2804 hba->path, get_sys_dev_type(hba->type));
0858eccf
AP
2805 continue;
2806 }
2807 result = 0;
2808 }
2809
2810 if (controller_path && result == 1) {
2811 pr_err("no active Intel(R) RAID controller found under %s\n",
2812 controller_path);
2813 return result;
2814 }
2815
5e1d6128 2816 const struct orom_entry *entry;
0858eccf 2817
5e1d6128 2818 for (entry = orom_entries; entry; entry = entry->next) {
60f0f54d 2819 if (entry->type == SYS_DEV_VMD) {
07cb1e57 2820 print_imsm_capability(&entry->orom);
32716c51
PB
2821 printf(" 3rd party NVMe :%s supported\n",
2822 imsm_orom_has_tpv_support(&entry->orom)?"":" not");
60f0f54d
PB
2823 for (hba = list; hba; hba = hba->next) {
2824 if (hba->type == SYS_DEV_VMD) {
2825 char buf[PATH_MAX];
60f0f54d
PB
2826 printf(" I/O Controller : %s (%s)\n",
2827 vmd_domain_to_controller(hba, buf), get_sys_dev_type(hba->type));
6da53c0e 2828 if (print_nvme_info(hba)) {
b5eece69
PB
2829 if (verbose > 0)
2830 pr_err("failed to get devices attached to VMD domain.\n");
2831 result |= 2;
2832 }
60f0f54d
PB
2833 }
2834 }
07cb1e57 2835 printf("\n");
60f0f54d
PB
2836 continue;
2837 }
0858eccf 2838
60f0f54d
PB
2839 print_imsm_capability(&entry->orom);
2840 if (entry->type == SYS_DEV_NVME) {
0858eccf
AP
2841 for (hba = list; hba; hba = hba->next) {
2842 if (hba->type == SYS_DEV_NVME)
6da53c0e 2843 print_nvme_info(hba);
0858eccf 2844 }
60f0f54d 2845 printf("\n");
0858eccf
AP
2846 continue;
2847 }
2848
2849 struct devid_list *devid;
5e1d6128 2850 for (devid = entry->devid_list; devid; devid = devid->next) {
0858eccf
AP
2851 hba = device_by_id(devid->devid);
2852 if (!hba)
2853 continue;
2854
9eafa1de
MN
2855 printf(" I/O Controller : %s (%s)\n",
2856 hba->path, get_sys_dev_type(hba->type));
75350d87 2857 if (hba->type == SYS_DEV_SATA || hba->type == SYS_DEV_SATA_VMD) {
9eafa1de
MN
2858 host_base = ahci_get_port_count(hba->path, &port_count);
2859 if (ahci_enumerate_ports(hba->path, port_count, host_base, verbose)) {
2860 if (verbose > 0)
75350d87
KF
2861 pr_err("failed to enumerate ports on %s controller at %s.\n",
2862 get_sys_dev_type(hba->type), hba->pci_id);
9eafa1de
MN
2863 result |= 2;
2864 }
120dc887
LM
2865 }
2866 }
0858eccf 2867 printf("\n");
d665cc31 2868 }
155cbb4c 2869
120dc887 2870 return result;
d665cc31 2871}
e50cf220 2872
9eafa1de 2873static int export_detail_platform_imsm(int verbose, char *controller_path)
e50cf220 2874{
e50cf220
MN
2875 struct sys_dev *list, *hba;
2876 int result=1;
2877
2878 list = find_intel_devices();
2879 if (!list) {
2880 if (verbose > 0)
2881 pr_err("IMSM_DETAIL_PLATFORM_ERROR=NO_INTEL_DEVICES\n");
2882 result = 2;
e50cf220
MN
2883 return result;
2884 }
2885
2886 for (hba = list; hba; hba = hba->next) {
9eafa1de
MN
2887 if (controller_path && (compare_paths(hba->path,controller_path) != 0))
2888 continue;
60f0f54d
PB
2889 if (!find_imsm_capability(hba) && verbose > 0) {
2890 char buf[PATH_MAX];
2891 pr_err("IMSM_DETAIL_PLATFORM_ERROR=NO_IMSM_CAPABLE_DEVICE_UNDER_%s\n",
75350d87
KF
2892 hba->type == SYS_DEV_VMD || hba->type == SYS_DEV_SATA_VMD ?
2893 vmd_domain_to_controller(hba, buf) : hba->path);
60f0f54d 2894 }
0858eccf 2895 else
e50cf220 2896 result = 0;
e50cf220
MN
2897 }
2898
5e1d6128 2899 const struct orom_entry *entry;
0858eccf 2900
60f0f54d 2901 for (entry = orom_entries; entry; entry = entry->next) {
75350d87 2902 if (entry->type == SYS_DEV_VMD || entry->type == SYS_DEV_SATA_VMD) {
60f0f54d
PB
2903 for (hba = list; hba; hba = hba->next)
2904 print_imsm_capability_export(&entry->orom);
2905 continue;
2906 }
5e1d6128 2907 print_imsm_capability_export(&entry->orom);
60f0f54d 2908 }
0858eccf 2909
e50cf220
MN
2910 return result;
2911}
2912
cdddbdbc
DW
2913static int match_home_imsm(struct supertype *st, char *homehost)
2914{
5115ca67
DW
2915 /* the imsm metadata format does not specify any host
2916 * identification information. We return -1 since we can never
2917 * confirm nor deny whether a given array is "meant" for this
148acb7b 2918 * host. We rely on compare_super and the 'family_num' fields to
5115ca67
DW
2919 * exclude member disks that do not belong, and we rely on
2920 * mdadm.conf to specify the arrays that should be assembled.
2921 * Auto-assembly may still pick up "foreign" arrays.
2922 */
cdddbdbc 2923
9362c1c8 2924 return -1;
cdddbdbc
DW
2925}
2926
2927static void uuid_from_super_imsm(struct supertype *st, int uuid[4])
2928{
51006d85
N
2929 /* The uuid returned here is used for:
2930 * uuid to put into bitmap file (Create, Grow)
2931 * uuid for backup header when saving critical section (Grow)
2932 * comparing uuids when re-adding a device into an array
2933 * In these cases the uuid required is that of the data-array,
2934 * not the device-set.
2935 * uuid to recognise same set when adding a missing device back
2936 * to an array. This is a uuid for the device-set.
1011e834 2937 *
51006d85
N
2938 * For each of these we can make do with a truncated
2939 * or hashed uuid rather than the original, as long as
2940 * everyone agrees.
2941 * In each case the uuid required is that of the data-array,
2942 * not the device-set.
43dad3d6 2943 */
51006d85
N
2944 /* imsm does not track uuid's so we synthesis one using sha1 on
2945 * - The signature (Which is constant for all imsm array, but no matter)
148acb7b 2946 * - the orig_family_num of the container
51006d85
N
2947 * - the index number of the volume
2948 * - the 'serial' number of the volume.
2949 * Hopefully these are all constant.
2950 */
2951 struct intel_super *super = st->sb;
43dad3d6 2952
51006d85
N
2953 char buf[20];
2954 struct sha1_ctx ctx;
2955 struct imsm_dev *dev = NULL;
148acb7b 2956 __u32 family_num;
51006d85 2957
148acb7b
DW
2958 /* some mdadm versions failed to set ->orig_family_num, in which
2959 * case fall back to ->family_num. orig_family_num will be
2960 * fixed up with the first metadata update.
2961 */
2962 family_num = super->anchor->orig_family_num;
2963 if (family_num == 0)
2964 family_num = super->anchor->family_num;
51006d85 2965 sha1_init_ctx(&ctx);
92bd8f8d 2966 sha1_process_bytes(super->anchor->sig, MPB_SIG_LEN, &ctx);
148acb7b 2967 sha1_process_bytes(&family_num, sizeof(__u32), &ctx);
51006d85
N
2968 if (super->current_vol >= 0)
2969 dev = get_imsm_dev(super, super->current_vol);
2970 if (dev) {
2971 __u32 vol = super->current_vol;
2972 sha1_process_bytes(&vol, sizeof(vol), &ctx);
2973 sha1_process_bytes(dev->volume, MAX_RAID_SERIAL_LEN, &ctx);
2974 }
2975 sha1_finish_ctx(&ctx, buf);
2976 memcpy(uuid, buf, 4*4);
cdddbdbc
DW
2977}
2978
1e5c6983
DW
2979static __u32 migr_strip_blocks_resync(struct imsm_dev *dev)
2980{
2981 /* migr_strip_size when repairing or initializing parity */
238c0a71 2982 struct imsm_map *map = get_imsm_map(dev, MAP_0);
1e5c6983
DW
2983 __u32 chunk = __le32_to_cpu(map->blocks_per_strip);
2984
2985 switch (get_imsm_raid_level(map)) {
2986 case 5:
2987 case 10:
2988 return chunk;
2989 default:
2990 return 128*1024 >> 9;
2991 }
2992}
2993
2994static __u32 migr_strip_blocks_rebuild(struct imsm_dev *dev)
2995{
2996 /* migr_strip_size when rebuilding a degraded disk, no idea why
2997 * this is different than migr_strip_size_resync(), but it's good
2998 * to be compatible
2999 */
238c0a71 3000 struct imsm_map *map = get_imsm_map(dev, MAP_1);
1e5c6983
DW
3001 __u32 chunk = __le32_to_cpu(map->blocks_per_strip);
3002
3003 switch (get_imsm_raid_level(map)) {
3004 case 1:
3005 case 10:
3006 if (map->num_members % map->num_domains == 0)
3007 return 128*1024 >> 9;
3008 else
3009 return chunk;
3010 case 5:
3011 return max((__u32) 64*1024 >> 9, chunk);
3012 default:
3013 return 128*1024 >> 9;
3014 }
3015}
3016
3017static __u32 num_stripes_per_unit_resync(struct imsm_dev *dev)
3018{
238c0a71
AK
3019 struct imsm_map *lo = get_imsm_map(dev, MAP_0);
3020 struct imsm_map *hi = get_imsm_map(dev, MAP_1);
1e5c6983
DW
3021 __u32 lo_chunk = __le32_to_cpu(lo->blocks_per_strip);
3022 __u32 hi_chunk = __le32_to_cpu(hi->blocks_per_strip);
3023
3024 return max((__u32) 1, hi_chunk / lo_chunk);
3025}
3026
3027static __u32 num_stripes_per_unit_rebuild(struct imsm_dev *dev)
3028{
238c0a71 3029 struct imsm_map *lo = get_imsm_map(dev, MAP_0);
1e5c6983
DW
3030 int level = get_imsm_raid_level(lo);
3031
3032 if (level == 1 || level == 10) {
238c0a71 3033 struct imsm_map *hi = get_imsm_map(dev, MAP_1);
1e5c6983
DW
3034
3035 return hi->num_domains;
3036 } else
3037 return num_stripes_per_unit_resync(dev);
3038}
3039
44490938
MD
3040static unsigned long long calc_component_size(struct imsm_map *map,
3041 struct imsm_dev *dev)
3042{
3043 unsigned long long component_size;
3044 unsigned long long dev_size = imsm_dev_size(dev);
a4f7290c 3045 long long calc_dev_size = 0;
44490938
MD
3046 unsigned int member_disks = imsm_num_data_members(map);
3047
3048 if (member_disks == 0)
3049 return 0;
3050
3051 component_size = per_dev_array_size(map);
3052 calc_dev_size = component_size * member_disks;
3053
3054 /* Component size is rounded to 1MB so difference between size from
3055 * metadata and size calculated from num_data_stripes equals up to
3056 * 2048 blocks per each device. If the difference is higher it means
3057 * that array size was expanded and num_data_stripes was not updated.
3058 */
a4f7290c 3059 if (llabs(calc_dev_size - (long long)dev_size) >
44490938
MD
3060 (1 << SECT_PER_MB_SHIFT) * member_disks) {
3061 component_size = dev_size / member_disks;
3062 dprintf("Invalid num_data_stripes in metadata; expected=%llu, found=%llu\n",
3063 component_size / map->blocks_per_strip,
3064 num_data_stripes(map));
3065 }
3066
3067 return component_size;
3068}
3069
1e5c6983
DW
3070static __u32 parity_segment_depth(struct imsm_dev *dev)
3071{
238c0a71 3072 struct imsm_map *map = get_imsm_map(dev, MAP_0);
1e5c6983
DW
3073 __u32 chunk = __le32_to_cpu(map->blocks_per_strip);
3074
3075 switch(get_imsm_raid_level(map)) {
3076 case 1:
3077 case 10:
3078 return chunk * map->num_domains;
3079 case 5:
3080 return chunk * map->num_members;
3081 default:
3082 return chunk;
3083 }
3084}
3085
3086static __u32 map_migr_block(struct imsm_dev *dev, __u32 block)
3087{
238c0a71 3088 struct imsm_map *map = get_imsm_map(dev, MAP_1);
1e5c6983
DW
3089 __u32 chunk = __le32_to_cpu(map->blocks_per_strip);
3090 __u32 strip = block / chunk;
3091
3092 switch (get_imsm_raid_level(map)) {
3093 case 1:
3094 case 10: {
3095 __u32 vol_strip = (strip * map->num_domains) + 1;
3096 __u32 vol_stripe = vol_strip / map->num_members;
3097
3098 return vol_stripe * chunk + block % chunk;
3099 } case 5: {
3100 __u32 stripe = strip / (map->num_members - 1);
3101
3102 return stripe * chunk + block % chunk;
3103 }
3104 default:
3105 return 0;
3106 }
3107}
3108
c47b0ff6
AK
3109static __u64 blocks_per_migr_unit(struct intel_super *super,
3110 struct imsm_dev *dev)
1e5c6983
DW
3111{
3112 /* calculate the conversion factor between per member 'blocks'
3113 * (md/{resync,rebuild}_start) and imsm migration units, return
3114 * 0 for the 'not migrating' and 'unsupported migration' cases
3115 */
3116 if (!dev->vol.migr_state)
3117 return 0;
3118
3119 switch (migr_type(dev)) {
c47b0ff6
AK
3120 case MIGR_GEN_MIGR: {
3121 struct migr_record *migr_rec = super->migr_rec;
3122 return __le32_to_cpu(migr_rec->blocks_per_unit);
3123 }
1e5c6983
DW
3124 case MIGR_VERIFY:
3125 case MIGR_REPAIR:
3126 case MIGR_INIT: {
238c0a71 3127 struct imsm_map *map = get_imsm_map(dev, MAP_0);
1e5c6983
DW
3128 __u32 stripes_per_unit;
3129 __u32 blocks_per_unit;
3130 __u32 parity_depth;
3131 __u32 migr_chunk;
3132 __u32 block_map;
3133 __u32 block_rel;
3134 __u32 segment;
3135 __u32 stripe;
3136 __u8 disks;
3137
3138 /* yes, this is really the translation of migr_units to
3139 * per-member blocks in the 'resync' case
3140 */
3141 stripes_per_unit = num_stripes_per_unit_resync(dev);
3142 migr_chunk = migr_strip_blocks_resync(dev);
9529d343 3143 disks = imsm_num_data_members(map);
1e5c6983 3144 blocks_per_unit = stripes_per_unit * migr_chunk * disks;
7b1ab482 3145 stripe = __le16_to_cpu(map->blocks_per_strip) * disks;
1e5c6983
DW
3146 segment = blocks_per_unit / stripe;
3147 block_rel = blocks_per_unit - segment * stripe;
3148 parity_depth = parity_segment_depth(dev);
3149 block_map = map_migr_block(dev, block_rel);
3150 return block_map + parity_depth * segment;
3151 }
3152 case MIGR_REBUILD: {
3153 __u32 stripes_per_unit;
3154 __u32 migr_chunk;
3155
3156 stripes_per_unit = num_stripes_per_unit_rebuild(dev);
3157 migr_chunk = migr_strip_blocks_rebuild(dev);
3158 return migr_chunk * stripes_per_unit;
3159 }
1e5c6983
DW
3160 case MIGR_STATE_CHANGE:
3161 default:
3162 return 0;
3163 }
3164}
3165
c2c087e6
DW
3166static int imsm_level_to_layout(int level)
3167{
3168 switch (level) {
3169 case 0:
3170 case 1:
3171 return 0;
3172 case 5:
3173 case 6:
a380c027 3174 return ALGORITHM_LEFT_ASYMMETRIC;
c2c087e6 3175 case 10:
c92a2527 3176 return 0x102;
c2c087e6 3177 }
a18a888e 3178 return UnSet;
c2c087e6
DW
3179}
3180
8e59f3d8
AK
3181/*******************************************************************************
3182 * Function: read_imsm_migr_rec
3183 * Description: Function reads imsm migration record from last sector of disk
3184 * Parameters:
3185 * fd : disk descriptor
3186 * super : metadata info
3187 * Returns:
3188 * 0 : success,
3189 * -1 : fail
3190 ******************************************************************************/
3191static int read_imsm_migr_rec(int fd, struct intel_super *super)
3192{
3193 int ret_val = -1;
de44e46f 3194 unsigned int sector_size = super->sector_size;
8e59f3d8
AK
3195 unsigned long long dsize;
3196
3197 get_dev_size(fd, NULL, &dsize);
de44e46f
PB
3198 if (lseek64(fd, dsize - (sector_size*MIGR_REC_SECTOR_POSITION),
3199 SEEK_SET) < 0) {
e7b84f9d
N
3200 pr_err("Cannot seek to anchor block: %s\n",
3201 strerror(errno));
8e59f3d8
AK
3202 goto out;
3203 }
466070ad 3204 if ((unsigned int)read(fd, super->migr_rec_buf,
de44e46f
PB
3205 MIGR_REC_BUF_SECTORS*sector_size) !=
3206 MIGR_REC_BUF_SECTORS*sector_size) {
e7b84f9d
N
3207 pr_err("Cannot read migr record block: %s\n",
3208 strerror(errno));
8e59f3d8
AK
3209 goto out;
3210 }
3211 ret_val = 0;
de44e46f
PB
3212 if (sector_size == 4096)
3213 convert_from_4k_imsm_migr_rec(super);
8e59f3d8
AK
3214
3215out:
3216 return ret_val;
3217}
3218
3136abe5
AK
3219static struct imsm_dev *imsm_get_device_during_migration(
3220 struct intel_super *super)
3221{
3222
3223 struct intel_dev *dv;
3224
3225 for (dv = super->devlist; dv; dv = dv->next) {
3226 if (is_gen_migration(dv->dev))
3227 return dv->dev;
3228 }
3229 return NULL;
3230}
3231
8e59f3d8
AK
3232/*******************************************************************************
3233 * Function: load_imsm_migr_rec
3234 * Description: Function reads imsm migration record (it is stored at the last
3235 * sector of disk)
3236 * Parameters:
3237 * super : imsm internal array info
8e59f3d8
AK
3238 * Returns:
3239 * 0 : success
3240 * -1 : fail
4c965cc9 3241 * -2 : no migration in progress
8e59f3d8 3242 ******************************************************************************/
2f86fda3 3243static int load_imsm_migr_rec(struct intel_super *super)
8e59f3d8 3244{
594dc1b8 3245 struct dl *dl;
8e59f3d8
AK
3246 char nm[30];
3247 int retval = -1;
3248 int fd = -1;
3136abe5 3249 struct imsm_dev *dev;
594dc1b8 3250 struct imsm_map *map;
b4ab44d8 3251 int slot = -1;
2f86fda3 3252 int keep_fd = 1;
3136abe5
AK
3253
3254 /* find map under migration */
3255 dev = imsm_get_device_during_migration(super);
3256 /* nothing to load,no migration in progress?
3257 */
3258 if (dev == NULL)
4c965cc9 3259 return -2;
8e59f3d8 3260
2f86fda3
MT
3261 map = get_imsm_map(dev, MAP_0);
3262 if (!map)
3263 return -1;
3136abe5 3264
2f86fda3
MT
3265 for (dl = super->disks; dl; dl = dl->next) {
3266 /* skip spare and failed disks
3267 */
3268 if (dl->index < 0)
3269 continue;
3270 /* read only from one of the first two slots
3271 */
3272 slot = get_imsm_disk_slot(map, dl->index);
3273 if (slot > 1 || slot < 0)
3274 continue;
3275
4389ce73 3276 if (!is_fd_valid(dl->fd)) {
8e59f3d8
AK
3277 sprintf(nm, "%d:%d", dl->major, dl->minor);
3278 fd = dev_open(nm, O_RDONLY);
4389ce73
MT
3279
3280 if (is_fd_valid(fd)) {
2f86fda3 3281 keep_fd = 0;
8e59f3d8 3282 break;
2f86fda3
MT
3283 }
3284 } else {
3285 fd = dl->fd;
3286 break;
8e59f3d8
AK
3287 }
3288 }
2f86fda3 3289
4389ce73 3290 if (!is_fd_valid(fd))
2f86fda3 3291 return retval;
8e59f3d8 3292 retval = read_imsm_migr_rec(fd, super);
2f86fda3 3293 if (!keep_fd)
8e59f3d8 3294 close(fd);
2f86fda3 3295
8e59f3d8
AK
3296 return retval;
3297}
3298
c17608ea
AK
3299/*******************************************************************************
3300 * function: imsm_create_metadata_checkpoint_update
3301 * Description: It creates update for checkpoint change.
3302 * Parameters:
3303 * super : imsm internal array info
3304 * u : pointer to prepared update
3305 * Returns:
3306 * Uptate length.
3307 * If length is equal to 0, input pointer u contains no update
3308 ******************************************************************************/
3309static int imsm_create_metadata_checkpoint_update(
3310 struct intel_super *super,
3311 struct imsm_update_general_migration_checkpoint **u)
3312{
3313
3314 int update_memory_size = 0;
3315
1ade5cc1 3316 dprintf("(enter)\n");
c17608ea
AK
3317
3318 if (u == NULL)
3319 return 0;
3320 *u = NULL;
3321
3322 /* size of all update data without anchor */
3323 update_memory_size =
3324 sizeof(struct imsm_update_general_migration_checkpoint);
3325
503975b9 3326 *u = xcalloc(1, update_memory_size);
c17608ea 3327 if (*u == NULL) {
1ade5cc1 3328 dprintf("error: cannot get memory\n");
c17608ea
AK
3329 return 0;
3330 }
3331 (*u)->type = update_general_migration_checkpoint;
9f421827 3332 (*u)->curr_migr_unit = current_migr_unit(super->migr_rec);
83b3de77 3333 dprintf("prepared for %llu\n", (unsigned long long)(*u)->curr_migr_unit);
c17608ea
AK
3334
3335 return update_memory_size;
3336}
3337
c17608ea
AK
3338static void imsm_update_metadata_locally(struct supertype *st,
3339 void *buf, int len);
3340
687629c2
AK
3341/*******************************************************************************
3342 * Function: write_imsm_migr_rec
3343 * Description: Function writes imsm migration record
3344 * (at the last sector of disk)
3345 * Parameters:
3346 * super : imsm internal array info
3347 * Returns:
3348 * 0 : success
3349 * -1 : if fail
3350 ******************************************************************************/
3351static int write_imsm_migr_rec(struct supertype *st)
3352{
3353 struct intel_super *super = st->sb;
de44e46f 3354 unsigned int sector_size = super->sector_size;
687629c2 3355 unsigned long long dsize;
687629c2
AK
3356 int retval = -1;
3357 struct dl *sd;
c17608ea
AK
3358 int len;
3359 struct imsm_update_general_migration_checkpoint *u;
3136abe5 3360 struct imsm_dev *dev;
594dc1b8 3361 struct imsm_map *map;
3136abe5
AK
3362
3363 /* find map under migration */
3364 dev = imsm_get_device_during_migration(super);
3365 /* if no migration, write buffer anyway to clear migr_record
3366 * on disk based on first available device
3367 */
3368 if (dev == NULL)
3369 dev = get_imsm_dev(super, super->current_vol < 0 ? 0 :
3370 super->current_vol);
3371
44bfe6df 3372 map = get_imsm_map(dev, MAP_0);
687629c2 3373
de44e46f
PB
3374 if (sector_size == 4096)
3375 convert_to_4k_imsm_migr_rec(super);
687629c2 3376 for (sd = super->disks ; sd ; sd = sd->next) {
b4ab44d8 3377 int slot = -1;
3136abe5
AK
3378
3379 /* skip failed and spare devices */
3380 if (sd->index < 0)
3381 continue;
687629c2 3382 /* write to 2 first slots only */
3136abe5
AK
3383 if (map)
3384 slot = get_imsm_disk_slot(map, sd->index);
089f9d79 3385 if (map == NULL || slot > 1 || slot < 0)
687629c2 3386 continue;
3136abe5 3387
2f86fda3
MT
3388 get_dev_size(sd->fd, NULL, &dsize);
3389 if (lseek64(sd->fd, dsize - (MIGR_REC_SECTOR_POSITION *
3390 sector_size),
de44e46f 3391 SEEK_SET) < 0) {
e7b84f9d
N
3392 pr_err("Cannot seek to anchor block: %s\n",
3393 strerror(errno));
687629c2
AK
3394 goto out;
3395 }
2f86fda3 3396 if ((unsigned int)write(sd->fd, super->migr_rec_buf,
de44e46f
PB
3397 MIGR_REC_BUF_SECTORS*sector_size) !=
3398 MIGR_REC_BUF_SECTORS*sector_size) {
e7b84f9d
N
3399 pr_err("Cannot write migr record block: %s\n",
3400 strerror(errno));
687629c2
AK
3401 goto out;
3402 }
687629c2 3403 }
de44e46f
PB
3404 if (sector_size == 4096)
3405 convert_from_4k_imsm_migr_rec(super);
c17608ea
AK
3406 /* update checkpoint information in metadata */
3407 len = imsm_create_metadata_checkpoint_update(super, &u);
c17608ea
AK
3408 if (len <= 0) {
3409 dprintf("imsm: Cannot prepare update\n");
3410 goto out;
3411 }
3412 /* update metadata locally */
3413 imsm_update_metadata_locally(st, u, len);
3414 /* and possibly remotely */
3415 if (st->update_tail) {
3416 append_metadata_update(st, u, len);
3417 /* during reshape we do all work inside metadata handler
3418 * manage_reshape(), so metadata update has to be triggered
3419 * insida it
3420 */
3421 flush_metadata_updates(st);
3422 st->update_tail = &st->updates;
3423 } else
3424 free(u);
687629c2
AK
3425
3426 retval = 0;
3427 out:
687629c2
AK
3428 return retval;
3429}
3430
e2962bfc
AK
3431/* spare/missing disks activations are not allowe when
3432 * array/container performs reshape operation, because
3433 * all arrays in container works on the same disks set
3434 */
3435int imsm_reshape_blocks_arrays_changes(struct intel_super *super)
3436{
3437 int rv = 0;
3438 struct intel_dev *i_dev;
3439 struct imsm_dev *dev;
3440
3441 /* check whole container
3442 */
3443 for (i_dev = super->devlist; i_dev; i_dev = i_dev->next) {
3444 dev = i_dev->dev;
3ad25638 3445 if (is_gen_migration(dev)) {
e2962bfc
AK
3446 /* No repair during any migration in container
3447 */
3448 rv = 1;
3449 break;
3450 }
3451 }
3452 return rv;
3453}
3e684231 3454static unsigned long long imsm_component_size_alignment_check(int level,
c41e00b2 3455 int chunk_size,
f36a9ecd 3456 unsigned int sector_size,
c41e00b2
AK
3457 unsigned long long component_size)
3458{
3e684231 3459 unsigned int component_size_alignment;
c41e00b2 3460
3e684231 3461 /* check component size alignment
c41e00b2 3462 */
3e684231 3463 component_size_alignment = component_size % (chunk_size/sector_size);
c41e00b2 3464
3e684231 3465 dprintf("(Level: %i, chunk_size = %i, component_size = %llu), component_size_alignment = %u\n",
c41e00b2 3466 level, chunk_size, component_size,
3e684231 3467 component_size_alignment);
c41e00b2 3468
3e684231
MZ
3469 if (component_size_alignment && (level != 1) && (level != UnSet)) {
3470 dprintf("imsm: reported component size aligned from %llu ",
c41e00b2 3471 component_size);
3e684231 3472 component_size -= component_size_alignment;
1ade5cc1 3473 dprintf_cont("to %llu (%i).\n",
3e684231 3474 component_size, component_size_alignment);
c41e00b2
AK
3475 }
3476
3477 return component_size;
3478}
e2962bfc 3479
fbc42556
JR
3480/*******************************************************************************
3481 * Function: get_bitmap_header_sector
3482 * Description: Returns the sector where the bitmap header is placed.
3483 * Parameters:
3484 * st : supertype information
3485 * dev_idx : index of the device with bitmap
3486 *
3487 * Returns:
3488 * The sector where the bitmap header is placed
3489 ******************************************************************************/
3490static unsigned long long get_bitmap_header_sector(struct intel_super *super,
3491 int dev_idx)
3492{
3493 struct imsm_dev *dev = get_imsm_dev(super, dev_idx);
3494 struct imsm_map *map = get_imsm_map(dev, MAP_0);
3495
3496 if (!super->sector_size) {
3497 dprintf("sector size is not set\n");
3498 return 0;
3499 }
3500
3501 return pba_of_lba0(map) + calc_component_size(map, dev) +
3502 (IMSM_BITMAP_HEADER_OFFSET / super->sector_size);
3503}
3504
3505/*******************************************************************************
3506 * Function: get_bitmap_sector
3507 * Description: Returns the sector where the bitmap is placed.
3508 * Parameters:
3509 * st : supertype information
3510 * dev_idx : index of the device with bitmap
3511 *
3512 * Returns:
3513 * The sector where the bitmap is placed
3514 ******************************************************************************/
3515static unsigned long long get_bitmap_sector(struct intel_super *super,
3516 int dev_idx)
3517{
3518 if (!super->sector_size) {
3519 dprintf("sector size is not set\n");
3520 return 0;
3521 }
3522
3523 return get_bitmap_header_sector(super, dev_idx) +
3524 (IMSM_BITMAP_HEADER_SIZE / super->sector_size);
3525}
3526
2432ce9b
AP
3527static unsigned long long get_ppl_sector(struct intel_super *super, int dev_idx)
3528{
3529 struct imsm_dev *dev = get_imsm_dev(super, dev_idx);
3530 struct imsm_map *map = get_imsm_map(dev, MAP_0);
3531
3532 return pba_of_lba0(map) +
3533 (num_data_stripes(map) * map->blocks_per_strip);
3534}
3535
a5d85af7 3536static void getinfo_super_imsm_volume(struct supertype *st, struct mdinfo *info, char *dmap)
bf5a934a
DW
3537{
3538 struct intel_super *super = st->sb;
c47b0ff6 3539 struct migr_record *migr_rec = super->migr_rec;
949c47a0 3540 struct imsm_dev *dev = get_imsm_dev(super, super->current_vol);
238c0a71
AK
3541 struct imsm_map *map = get_imsm_map(dev, MAP_0);
3542 struct imsm_map *prev_map = get_imsm_map(dev, MAP_1);
b335e593 3543 struct imsm_map *map_to_analyse = map;
efb30e7f 3544 struct dl *dl;
a5d85af7 3545 int map_disks = info->array.raid_disks;
bf5a934a 3546
95eeceeb 3547 memset(info, 0, sizeof(*info));
b335e593
AK
3548 if (prev_map)
3549 map_to_analyse = prev_map;
3550
ca0748fa 3551 dl = super->current_disk;
9894ec0d 3552
bf5a934a 3553 info->container_member = super->current_vol;
cd0430a1 3554 info->array.raid_disks = map->num_members;
b335e593 3555 info->array.level = get_imsm_raid_level(map_to_analyse);
bf5a934a
DW
3556 info->array.layout = imsm_level_to_layout(info->array.level);
3557 info->array.md_minor = -1;
3558 info->array.ctime = 0;
3559 info->array.utime = 0;
b335e593
AK
3560 info->array.chunk_size =
3561 __le16_to_cpu(map_to_analyse->blocks_per_strip) << 9;
2432ce9b 3562 info->array.state = !(dev->vol.dirty & RAIDVOL_DIRTY);
fcc2c9da 3563 info->custom_array_size = imsm_dev_size(dev);
3ad25638
AK
3564 info->recovery_blocked = imsm_reshape_blocks_arrays_changes(st->sb);
3565
3f510843 3566 if (is_gen_migration(dev)) {
195d1d76
PP
3567 /*
3568 * device prev_map should be added if it is in the middle
3569 * of migration
3570 */
3571 assert(prev_map);
3572
3f83228a 3573 info->reshape_active = 1;
b335e593
AK
3574 info->new_level = get_imsm_raid_level(map);
3575 info->new_layout = imsm_level_to_layout(info->new_level);
3576 info->new_chunk = __le16_to_cpu(map->blocks_per_strip) << 9;
3f83228a 3577 info->delta_disks = map->num_members - prev_map->num_members;
493f5dd6
N
3578 if (info->delta_disks) {
3579 /* this needs to be applied to every array
3580 * in the container.
3581 */
81219e70 3582 info->reshape_active = CONTAINER_RESHAPE;
493f5dd6 3583 }
3f83228a
N
3584 /* We shape information that we give to md might have to be
3585 * modify to cope with md's requirement for reshaping arrays.
3586 * For example, when reshaping a RAID0, md requires it to be
3587 * presented as a degraded RAID4.
3588 * Also if a RAID0 is migrating to a RAID5 we need to specify
3589 * the array as already being RAID5, but the 'before' layout
3590 * is a RAID4-like layout.
3591 */
3592 switch (info->array.level) {
3593 case 0:
3594 switch(info->new_level) {
3595 case 0:
3596 /* conversion is happening as RAID4 */
3597 info->array.level = 4;
3598 info->array.raid_disks += 1;
3599 break;
3600 case 5:
3601 /* conversion is happening as RAID5 */
3602 info->array.level = 5;
3603 info->array.layout = ALGORITHM_PARITY_N;
3f83228a
N
3604 info->delta_disks -= 1;
3605 break;
3606 default:
3607 /* FIXME error message */
3608 info->array.level = UnSet;
3609 break;
3610 }
3611 break;
3612 }
b335e593
AK
3613 } else {
3614 info->new_level = UnSet;
3615 info->new_layout = UnSet;
3616 info->new_chunk = info->array.chunk_size;
3f83228a 3617 info->delta_disks = 0;
b335e593 3618 }
ca0748fa 3619
efb30e7f
DW
3620 if (dl) {
3621 info->disk.major = dl->major;
3622 info->disk.minor = dl->minor;
ca0748fa 3623 info->disk.number = dl->index;
656b6b5a
N
3624 info->disk.raid_disk = get_imsm_disk_slot(map_to_analyse,
3625 dl->index);
efb30e7f 3626 }
bf5a934a 3627
5551b113 3628 info->data_offset = pba_of_lba0(map_to_analyse);
44490938 3629 info->component_size = calc_component_size(map, dev);
3e684231 3630 info->component_size = imsm_component_size_alignment_check(
c41e00b2
AK
3631 info->array.level,
3632 info->array.chunk_size,
f36a9ecd 3633 super->sector_size,
c41e00b2 3634 info->component_size);
5e46202e 3635 info->bb.supported = 1;
139dae11 3636
301406c9 3637 memset(info->uuid, 0, sizeof(info->uuid));
921d9e16 3638 info->recovery_start = MaxSector;
bf5a934a 3639
c2462068
PB
3640 if (info->array.level == 5 &&
3641 (dev->rwh_policy == RWH_DISTRIBUTED ||
3642 dev->rwh_policy == RWH_MULTIPLE_DISTRIBUTED)) {
2432ce9b
AP
3643 info->consistency_policy = CONSISTENCY_POLICY_PPL;
3644 info->ppl_sector = get_ppl_sector(super, super->current_vol);
c2462068
PB
3645 if (dev->rwh_policy == RWH_MULTIPLE_DISTRIBUTED)
3646 info->ppl_size = MULTIPLE_PPL_AREA_SIZE_IMSM >> 9;
3647 else
3648 info->ppl_size = (PPL_HEADER_SIZE + PPL_ENTRY_SPACE)
3649 >> 9;
2432ce9b
AP
3650 } else if (info->array.level <= 0) {
3651 info->consistency_policy = CONSISTENCY_POLICY_NONE;
3652 } else {
fbc42556
JR
3653 if (dev->rwh_policy == RWH_BITMAP) {
3654 info->bitmap_offset = get_bitmap_sector(super, super->current_vol);
3655 info->consistency_policy = CONSISTENCY_POLICY_BITMAP;
3656 } else {
3657 info->consistency_policy = CONSISTENCY_POLICY_RESYNC;
3658 }
2432ce9b
AP
3659 }
3660
d2e6d5d6 3661 info->reshape_progress = 0;
b6796ce1 3662 info->resync_start = MaxSector;
b9172665 3663 if ((map_to_analyse->map_state == IMSM_T_STATE_UNINITIALIZED ||
2432ce9b 3664 !(info->array.state & 1)) &&
b9172665 3665 imsm_reshape_blocks_arrays_changes(super) == 0) {
301406c9 3666 info->resync_start = 0;
b6796ce1
AK
3667 }
3668 if (dev->vol.migr_state) {
1e5c6983
DW
3669 switch (migr_type(dev)) {
3670 case MIGR_REPAIR:
3671 case MIGR_INIT: {
c47b0ff6
AK
3672 __u64 blocks_per_unit = blocks_per_migr_unit(super,
3673 dev);
4036e7ee 3674 __u64 units = vol_curr_migr_unit(dev);
1e5c6983
DW
3675
3676 info->resync_start = blocks_per_unit * units;
3677 break;
3678 }
d2e6d5d6 3679 case MIGR_GEN_MIGR: {
c47b0ff6
AK
3680 __u64 blocks_per_unit = blocks_per_migr_unit(super,
3681 dev);
9f421827 3682 __u64 units = current_migr_unit(migr_rec);
04fa9523 3683 int used_disks;
d2e6d5d6 3684
befb629b
AK
3685 if (__le32_to_cpu(migr_rec->ascending_migr) &&
3686 (units <
9f421827 3687 (get_num_migr_units(migr_rec)-1)) &&
befb629b
AK
3688 (super->migr_rec->rec_status ==
3689 __cpu_to_le32(UNIT_SRC_IN_CP_AREA)))
3690 units++;
3691
d2e6d5d6 3692 info->reshape_progress = blocks_per_unit * units;
6289d1e0 3693
7a862a02 3694 dprintf("IMSM: General Migration checkpoint : %llu (%llu) -> read reshape progress : %llu\n",
19986c72
MB
3695 (unsigned long long)units,
3696 (unsigned long long)blocks_per_unit,
3697 info->reshape_progress);
75156c46 3698
9529d343 3699 used_disks = imsm_num_data_members(prev_map);
75156c46 3700 if (used_disks > 0) {
895ffd99 3701 info->custom_array_size = per_dev_array_size(map) *
75156c46 3702 used_disks;
75156c46 3703 }
d2e6d5d6 3704 }
1e5c6983
DW
3705 case MIGR_VERIFY:
3706 /* we could emulate the checkpointing of
3707 * 'sync_action=check' migrations, but for now
3708 * we just immediately complete them
3709 */
3710 case MIGR_REBUILD:
3711 /* this is handled by container_content_imsm() */
1e5c6983
DW
3712 case MIGR_STATE_CHANGE:
3713 /* FIXME handle other migrations */
3714 default:
3715 /* we are not dirty, so... */
3716 info->resync_start = MaxSector;
3717 }
b6796ce1 3718 }
301406c9
DW
3719
3720 strncpy(info->name, (char *) dev->volume, MAX_RAID_SERIAL_LEN);
3721 info->name[MAX_RAID_SERIAL_LEN] = 0;
bf5a934a 3722
f35f2525
N
3723 info->array.major_version = -1;
3724 info->array.minor_version = -2;
4dd2df09 3725 sprintf(info->text_version, "/%s/%d", st->container_devnm, info->container_member);
a67dd8cc 3726 info->safe_mode_delay = 4000; /* 4 secs like the Matrix driver */
51006d85 3727 uuid_from_super_imsm(st, info->uuid);
a5d85af7
N
3728
3729 if (dmap) {
3730 int i, j;
3731 for (i=0; i<map_disks; i++) {
3732 dmap[i] = 0;
3733 if (i < info->array.raid_disks) {
3734 struct imsm_disk *dsk;
238c0a71 3735 j = get_imsm_disk_idx(dev, i, MAP_X);
a5d85af7
N
3736 dsk = get_imsm_disk(super, j);
3737 if (dsk && (dsk->status & CONFIGURED_DISK))
3738 dmap[i] = 1;
3739 }
3740 }
3741 }
81ac8b4d 3742}
bf5a934a 3743
3b451610
AK
3744static __u8 imsm_check_degraded(struct intel_super *super, struct imsm_dev *dev,
3745 int failed, int look_in_map);
3746
3747static int imsm_count_failed(struct intel_super *super, struct imsm_dev *dev,
3748 int look_in_map);
3749
3750static void manage_second_map(struct intel_super *super, struct imsm_dev *dev)
3751{
3752 if (is_gen_migration(dev)) {
3753 int failed;
3754 __u8 map_state;
3755 struct imsm_map *map2 = get_imsm_map(dev, MAP_1);
3756
3757 failed = imsm_count_failed(super, dev, MAP_1);
238c0a71 3758 map_state = imsm_check_degraded(super, dev, failed, MAP_1);
3b451610
AK
3759 if (map2->map_state != map_state) {
3760 map2->map_state = map_state;
3761 super->updates_pending++;
3762 }
3763 }
3764}
97b4d0e9
DW
3765
3766static struct imsm_disk *get_imsm_missing(struct intel_super *super, __u8 index)
3767{
3768 struct dl *d;
3769
3770 for (d = super->missing; d; d = d->next)
3771 if (d->index == index)
3772 return &d->disk;
3773 return NULL;
3774}
3775
a5d85af7 3776static void getinfo_super_imsm(struct supertype *st, struct mdinfo *info, char *map)
4f5bc454
DW
3777{
3778 struct intel_super *super = st->sb;
4f5bc454 3779 struct imsm_disk *disk;
a5d85af7 3780 int map_disks = info->array.raid_disks;
ab3cb6b3
N
3781 int i;
3782 struct imsm_super *mpb;
4f5bc454 3783
bf5a934a 3784 if (super->current_vol >= 0) {
a5d85af7 3785 getinfo_super_imsm_volume(st, info, map);
bf5a934a
DW
3786 return;
3787 }
95eeceeb 3788 memset(info, 0, sizeof(*info));
d23fe947
DW
3789
3790 /* Set raid_disks to zero so that Assemble will always pull in valid
3791 * spares
3792 */
3793 info->array.raid_disks = 0;
cdddbdbc
DW
3794 info->array.level = LEVEL_CONTAINER;
3795 info->array.layout = 0;
3796 info->array.md_minor = -1;
1011e834 3797 info->array.ctime = 0; /* N/A for imsm */
cdddbdbc
DW
3798 info->array.utime = 0;
3799 info->array.chunk_size = 0;
3800
3801 info->disk.major = 0;
3802 info->disk.minor = 0;
cdddbdbc 3803 info->disk.raid_disk = -1;
c2c087e6 3804 info->reshape_active = 0;
f35f2525
N
3805 info->array.major_version = -1;
3806 info->array.minor_version = -2;
c2c087e6 3807 strcpy(info->text_version, "imsm");
a67dd8cc 3808 info->safe_mode_delay = 0;
c2c087e6
DW
3809 info->disk.number = -1;
3810 info->disk.state = 0;
c5afc314 3811 info->name[0] = 0;
921d9e16 3812 info->recovery_start = MaxSector;
3ad25638 3813 info->recovery_blocked = imsm_reshape_blocks_arrays_changes(st->sb);
5e46202e 3814 info->bb.supported = 1;
c2c087e6 3815
97b4d0e9 3816 /* do we have the all the insync disks that we expect? */
ab3cb6b3 3817 mpb = super->anchor;
b7d81a38 3818 info->events = __le32_to_cpu(mpb->generation_num);
97b4d0e9 3819
ab3cb6b3
N
3820 for (i = 0; i < mpb->num_raid_devs; i++) {
3821 struct imsm_dev *dev = get_imsm_dev(super, i);
4dde420f 3822 int j = 0;
ab3cb6b3 3823 struct imsm_map *map;
97b4d0e9 3824
238c0a71 3825 map = get_imsm_map(dev, MAP_0);
ab3cb6b3
N
3826
3827 /* any newly missing disks?
3828 * (catches single-degraded vs double-degraded)
3829 */
3830 for (j = 0; j < map->num_members; j++) {
238c0a71 3831 __u32 ord = get_imsm_ord_tbl_ent(dev, j, MAP_0);
ab3cb6b3
N
3832 __u32 idx = ord_to_idx(ord);
3833
20dc76d1
MT
3834 if (super->disks && super->disks->index == (int)idx)
3835 info->disk.raid_disk = j;
3836
ab3cb6b3
N
3837 if (!(ord & IMSM_ORD_REBUILD) &&
3838 get_imsm_missing(super, idx)) {
ab3cb6b3
N
3839 break;
3840 }
97b4d0e9 3841 }
ab3cb6b3 3842 }
97b4d0e9 3843
4a04ec6c 3844 if (super->disks) {
14e8215b
DW
3845 __u32 reserved = imsm_reserved_sectors(super, super->disks);
3846
b9f594fe 3847 disk = &super->disks->disk;
5551b113 3848 info->data_offset = total_blocks(&super->disks->disk) - reserved;
14e8215b 3849 info->component_size = reserved;
25ed7e59 3850 info->disk.state = is_configured(disk) ? (1 << MD_DISK_ACTIVE) : 0;
df474657
DW
3851 /* we don't change info->disk.raid_disk here because
3852 * this state will be finalized in mdmon after we have
3853 * found the 'most fresh' version of the metadata
3854 */
25ed7e59 3855 info->disk.state |= is_failed(disk) ? (1 << MD_DISK_FAULTY) : 0;
2432ce9b
AP
3856 info->disk.state |= (is_spare(disk) || is_journal(disk)) ?
3857 0 : (1 << MD_DISK_SYNC);
cdddbdbc 3858 }
a575e2a7
DW
3859
3860 /* only call uuid_from_super_imsm when this disk is part of a populated container,
3861 * ->compare_super may have updated the 'num_raid_devs' field for spares
3862 */
3863 if (info->disk.state & (1 << MD_DISK_SYNC) || super->anchor->num_raid_devs)
36ba7d48 3864 uuid_from_super_imsm(st, info->uuid);
22e263f6
AC
3865 else
3866 memcpy(info->uuid, uuid_zero, sizeof(uuid_zero));
a5d85af7
N
3867
3868 /* I don't know how to compute 'map' on imsm, so use safe default */
3869 if (map) {
3870 int i;
3871 for (i = 0; i < map_disks; i++)
3872 map[i] = 1;
3873 }
3874
cdddbdbc
DW
3875}
3876
5c4cd5da
AC
3877/* allocates memory and fills disk in mdinfo structure
3878 * for each disk in array */
3879struct mdinfo *getinfo_super_disks_imsm(struct supertype *st)
3880{
594dc1b8 3881 struct mdinfo *mddev;
5c4cd5da
AC
3882 struct intel_super *super = st->sb;
3883 struct imsm_disk *disk;
3884 int count = 0;
3885 struct dl *dl;
3886 if (!super || !super->disks)
3887 return NULL;
3888 dl = super->disks;
503975b9 3889 mddev = xcalloc(1, sizeof(*mddev));
5c4cd5da
AC
3890 while (dl) {
3891 struct mdinfo *tmp;
3892 disk = &dl->disk;
503975b9 3893 tmp = xcalloc(1, sizeof(*tmp));
5c4cd5da
AC
3894 if (mddev->devs)
3895 tmp->next = mddev->devs;
3896 mddev->devs = tmp;
3897 tmp->disk.number = count++;
3898 tmp->disk.major = dl->major;
3899 tmp->disk.minor = dl->minor;
3900 tmp->disk.state = is_configured(disk) ?
3901 (1 << MD_DISK_ACTIVE) : 0;
3902 tmp->disk.state |= is_failed(disk) ? (1 << MD_DISK_FAULTY) : 0;
3903 tmp->disk.state |= is_spare(disk) ? 0 : (1 << MD_DISK_SYNC);
3904 tmp->disk.raid_disk = -1;
3905 dl = dl->next;
3906 }
3907 return mddev;
3908}
3909
cdddbdbc 3910static int update_super_imsm(struct supertype *st, struct mdinfo *info,
03312b52
MK
3911 enum update_opt update, char *devname,
3912 int verbose, int uuid_set, char *homehost)
cdddbdbc 3913{
f352c545
DW
3914 /* For 'assemble' and 'force' we need to return non-zero if any
3915 * change was made. For others, the return value is ignored.
3916 * Update options are:
3917 * force-one : This device looks a bit old but needs to be included,
3918 * update age info appropriately.
3919 * assemble: clear any 'faulty' flag to allow this device to
3920 * be assembled.
3921 * force-array: Array is degraded but being forced, mark it clean
3922 * if that will be needed to assemble it.
3923 *
3924 * newdev: not used ????
3925 * grow: Array has gained a new device - this is currently for
3926 * linear only
3927 * resync: mark as dirty so a resync will happen.
3928 * name: update the name - preserving the homehost
6e46bf34 3929 * uuid: Change the uuid of the array to match watch is given
f352c545
DW
3930 *
3931 * Following are not relevant for this imsm:
3932 * sparc2.2 : update from old dodgey metadata
3933 * super-minor: change the preferred_minor number
3934 * summaries: update redundant counters.
f352c545
DW
3935 * homehost: update the recorded homehost
3936 * _reshape_progress: record new reshape_progress position.
3937 */
6e46bf34
DW
3938 int rv = 1;
3939 struct intel_super *super = st->sb;
3940 struct imsm_super *mpb;
f352c545 3941
6e46bf34
DW
3942 /* we can only update container info */
3943 if (!super || super->current_vol >= 0 || !super->anchor)
3944 return 1;
3945
3946 mpb = super->anchor;
3947
03312b52 3948 switch (update) {
4345e135 3949 case UOPT_UUID:
81a5b4f5
N
3950 /* We take this to mean that the family_num should be updated.
3951 * However that is much smaller than the uuid so we cannot really
3952 * allow an explicit uuid to be given. And it is hard to reliably
3953 * know if one was.
3954 * So if !uuid_set we know the current uuid is random and just used
3955 * the first 'int' and copy it to the other 3 positions.
3956 * Otherwise we require the 4 'int's to be the same as would be the
3957 * case if we are using a random uuid. So an explicit uuid will be
3958 * accepted as long as all for ints are the same... which shouldn't hurt
6e46bf34 3959 */
81a5b4f5
N
3960 if (!uuid_set) {
3961 info->uuid[1] = info->uuid[2] = info->uuid[3] = info->uuid[0];
6e46bf34 3962 rv = 0;
81a5b4f5
N
3963 } else {
3964 if (info->uuid[0] != info->uuid[1] ||
3965 info->uuid[1] != info->uuid[2] ||
3966 info->uuid[2] != info->uuid[3])
3967 rv = -1;
3968 else
3969 rv = 0;
6e46bf34 3970 }
81a5b4f5
N
3971 if (rv == 0)
3972 mpb->orig_family_num = info->uuid[0];
4345e135
MK
3973 break;
3974 case UOPT_SPEC_ASSEMBLE:
6e46bf34 3975 rv = 0;
4345e135
MK
3976 break;
3977 default:
1e2b2765 3978 rv = -1;
4345e135
MK
3979 break;
3980 }
f352c545 3981
6e46bf34
DW
3982 /* successful update? recompute checksum */
3983 if (rv == 0)
3984 mpb->check_sum = __le32_to_cpu(__gen_imsm_checksum(mpb));
f352c545
DW
3985
3986 return rv;
cdddbdbc
DW
3987}
3988
c2c087e6 3989static size_t disks_to_mpb_size(int disks)
cdddbdbc 3990{
c2c087e6 3991 size_t size;
cdddbdbc 3992
c2c087e6
DW
3993 size = sizeof(struct imsm_super);
3994 size += (disks - 1) * sizeof(struct imsm_disk);
3995 size += 2 * sizeof(struct imsm_dev);
3996 /* up to 2 maps per raid device (-2 for imsm_maps in imsm_dev */
3997 size += (4 - 2) * sizeof(struct imsm_map);
3998 /* 4 possible disk_ord_tbl's */
3999 size += 4 * (disks - 1) * sizeof(__u32);
bbab0940
TM
4000 /* maximum bbm log */
4001 size += sizeof(struct bbm_log);
c2c087e6
DW
4002
4003 return size;
4004}
4005
387fcd59
N
4006static __u64 avail_size_imsm(struct supertype *st, __u64 devsize,
4007 unsigned long long data_offset)
c2c087e6
DW
4008{
4009 if (devsize < (MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS))
4010 return 0;
4011
4012 return devsize - (MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS);
cdddbdbc
DW
4013}
4014
ba2de7ba
DW
4015static void free_devlist(struct intel_super *super)
4016{
4017 struct intel_dev *dv;
4018
4019 while (super->devlist) {
4020 dv = super->devlist->next;
4021 free(super->devlist->dev);
4022 free(super->devlist);
4023 super->devlist = dv;
4024 }
4025}
4026
4027static void imsm_copy_dev(struct imsm_dev *dest, struct imsm_dev *src)
4028{
4029 memcpy(dest, src, sizeof_imsm_dev(src, 0));
4030}
4031
c7b8547c
MT
4032static int compare_super_imsm(struct supertype *st, struct supertype *tst,
4033 int verbose)
cdddbdbc 4034{
601ffa78 4035 /* return:
cdddbdbc 4036 * 0 same, or first was empty, and second was copied
601ffa78 4037 * 1 sb are different
cdddbdbc
DW
4038 */
4039 struct intel_super *first = st->sb;
4040 struct intel_super *sec = tst->sb;
4041
5d500228
N
4042 if (!first) {
4043 st->sb = tst->sb;
4044 tst->sb = NULL;
4045 return 0;
4046 }
601ffa78 4047
8603ea6f
LM
4048 /* in platform dependent environment test if the disks
4049 * use the same Intel hba
601ffa78
OS
4050 * if not on Intel hba at all, allow anything.
4051 * doesn't check HBAs if num_raid_devs is not set, as it means
4052 * it is a free floating spare, and all spares regardless of HBA type
4053 * will fall into separate container during the assembly
8603ea6f 4054 */
601ffa78 4055 if (first->hba && sec->hba && first->anchor->num_raid_devs != 0) {
6b781d33 4056 if (first->hba->type != sec->hba->type) {
c7b8547c
MT
4057 if (verbose)
4058 pr_err("HBAs of devices do not match %s != %s\n",
4059 get_sys_dev_type(first->hba->type),
4060 get_sys_dev_type(sec->hba->type));
601ffa78 4061 return 1;
6b781d33
AP
4062 }
4063 if (first->orom != sec->orom) {
c7b8547c
MT
4064 if (verbose)
4065 pr_err("HBAs of devices do not match %s != %s\n",
4066 first->hba->pci_id, sec->hba->pci_id);
601ffa78 4067 return 1;
8603ea6f
LM
4068 }
4069 }
cdddbdbc 4070
d23fe947
DW
4071 if (first->anchor->num_raid_devs > 0 &&
4072 sec->anchor->num_raid_devs > 0) {
a2b97981
DW
4073 /* Determine if these disks might ever have been
4074 * related. Further disambiguation can only take place
4075 * in load_super_imsm_all
4076 */
4077 __u32 first_family = first->anchor->orig_family_num;
4078 __u32 sec_family = sec->anchor->orig_family_num;
4079
f796af5d
DW
4080 if (memcmp(first->anchor->sig, sec->anchor->sig,
4081 MAX_SIGNATURE_LENGTH) != 0)
601ffa78 4082 return 1;
f796af5d 4083
a2b97981
DW
4084 if (first_family == 0)
4085 first_family = first->anchor->family_num;
4086 if (sec_family == 0)
4087 sec_family = sec->anchor->family_num;
4088
4089 if (first_family != sec_family)
601ffa78 4090 return 1;
f796af5d 4091
d23fe947 4092 }
cdddbdbc 4093
601ffa78
OS
4094 /* if an anchor does not have num_raid_devs set then it is a free
4095 * floating spare. don't assosiate spare with any array, as during assembly
4096 * spares shall fall into separate container, from which they can be moved
4097 * when necessary
4098 */
4099 if (first->anchor->num_raid_devs ^ sec->anchor->num_raid_devs)
4100 return 1;
3e372e5a 4101
cdddbdbc
DW
4102 return 0;
4103}
4104
0030e8d6
DW
4105static void fd2devname(int fd, char *name)
4106{
0030e8d6 4107 char *nm;
0030e8d6 4108
7c798f87
MT
4109 nm = fd2kname(fd);
4110 if (!nm)
0030e8d6 4111 return;
9587c373 4112
7c798f87 4113 snprintf(name, MAX_RAID_SERIAL_LEN, "/dev/%s", nm);
0030e8d6
DW
4114}
4115
21e9380b
AP
4116static int nvme_get_serial(int fd, void *buf, size_t buf_len)
4117{
fcebeb77 4118 char path[PATH_MAX];
21e9380b
AP
4119 char *name = fd2kname(fd);
4120
4121 if (!name)
4122 return 1;
4123
4124 if (strncmp(name, "nvme", 4) != 0)
4125 return 1;
4126
fcebeb77
MT
4127 if (!diskfd_to_devpath(fd, 1, path))
4128 return 1;
21e9380b 4129
fcebeb77 4130 return devpath_to_char(path, "serial", buf, buf_len, 0);
21e9380b
AP
4131}
4132
cdddbdbc
DW
4133extern int scsi_get_serial(int fd, void *buf, size_t buf_len);
4134
4135static int imsm_read_serial(int fd, char *devname,
6da53c0e 4136 __u8 *serial, size_t serial_buf_len)
cdddbdbc 4137{
21e9380b 4138 char buf[50];
cdddbdbc 4139 int rv;
6da53c0e 4140 size_t len;
316e2bf4
DW
4141 char *dest;
4142 char *src;
21e9380b
AP
4143 unsigned int i;
4144
4145 memset(buf, 0, sizeof(buf));
cdddbdbc 4146
21e9380b 4147 rv = nvme_get_serial(fd, buf, sizeof(buf));
cdddbdbc 4148
21e9380b
AP
4149 if (rv)
4150 rv = scsi_get_serial(fd, buf, sizeof(buf));
f9ba0ff1 4151
40ebbb9c 4152 if (rv && check_env("IMSM_DEVNAME_AS_SERIAL")) {
f9ba0ff1
DW
4153 memset(serial, 0, MAX_RAID_SERIAL_LEN);
4154 fd2devname(fd, (char *) serial);
0030e8d6
DW
4155 return 0;
4156 }
4157
cdddbdbc
DW
4158 if (rv != 0) {
4159 if (devname)
e7b84f9d
N
4160 pr_err("Failed to retrieve serial for %s\n",
4161 devname);
cdddbdbc
DW
4162 return rv;
4163 }
4164
316e2bf4
DW
4165 /* trim all whitespace and non-printable characters and convert
4166 * ':' to ';'
4167 */
21e9380b
AP
4168 for (i = 0, dest = buf; i < sizeof(buf) && buf[i]; i++) {
4169 src = &buf[i];
316e2bf4
DW
4170 if (*src > 0x20) {
4171 /* ':' is reserved for use in placeholder serial
4172 * numbers for missing disks
4173 */
4174 if (*src == ':')
4175 *dest++ = ';';
4176 else
4177 *dest++ = *src;
4178 }
4179 }
21e9380b
AP
4180 len = dest - buf;
4181 dest = buf;
316e2bf4 4182
6da53c0e
BK
4183 if (len > serial_buf_len) {
4184 /* truncate leading characters */
4185 dest += len - serial_buf_len;
4186 len = serial_buf_len;
316e2bf4 4187 }
5c3db629 4188
6da53c0e 4189 memset(serial, 0, serial_buf_len);
316e2bf4 4190 memcpy(serial, dest, len);
cdddbdbc
DW
4191
4192 return 0;
4193}
4194
1f24f035
DW
4195static int serialcmp(__u8 *s1, __u8 *s2)
4196{
4197 return strncmp((char *) s1, (char *) s2, MAX_RAID_SERIAL_LEN);
4198}
4199
4200static void serialcpy(__u8 *dest, __u8 *src)
4201{
4202 strncpy((char *) dest, (char *) src, MAX_RAID_SERIAL_LEN);
4203}
4204
54c2c1ea
DW
4205static struct dl *serial_to_dl(__u8 *serial, struct intel_super *super)
4206{
4207 struct dl *dl;
4208
4209 for (dl = super->disks; dl; dl = dl->next)
4210 if (serialcmp(dl->serial, serial) == 0)
4211 break;
4212
4213 return dl;
4214}
4215
a2b97981
DW
4216static struct imsm_disk *
4217__serial_to_disk(__u8 *serial, struct imsm_super *mpb, int *idx)
4218{
4219 int i;
4220
4221 for (i = 0; i < mpb->num_disks; i++) {
4222 struct imsm_disk *disk = __get_imsm_disk(mpb, i);
4223
4224 if (serialcmp(disk->serial, serial) == 0) {
4225 if (idx)
4226 *idx = i;
4227 return disk;
4228 }
4229 }
4230
4231 return NULL;
4232}
4233
cdddbdbc
DW
4234static int
4235load_imsm_disk(int fd, struct intel_super *super, char *devname, int keep_fd)
4236{
a2b97981 4237 struct imsm_disk *disk;
cdddbdbc
DW
4238 struct dl *dl;
4239 struct stat stb;
cdddbdbc 4240 int rv;
a2b97981 4241 char name[40];
d23fe947
DW
4242 __u8 serial[MAX_RAID_SERIAL_LEN];
4243
6da53c0e 4244 rv = imsm_read_serial(fd, devname, serial, MAX_RAID_SERIAL_LEN);
d23fe947
DW
4245
4246 if (rv != 0)
4247 return 2;
4248
503975b9 4249 dl = xcalloc(1, sizeof(*dl));
cdddbdbc 4250
a2b97981
DW
4251 fstat(fd, &stb);
4252 dl->major = major(stb.st_rdev);
4253 dl->minor = minor(stb.st_rdev);
4254 dl->next = super->disks;
4255 dl->fd = keep_fd ? fd : -1;
4256 assert(super->disks == NULL);
4257 super->disks = dl;
4258 serialcpy(dl->serial, serial);
4259 dl->index = -2;
4260 dl->e = NULL;
4261 fd2devname(fd, name);
4262 if (devname)
503975b9 4263 dl->devname = xstrdup(devname);
a2b97981 4264 else
503975b9 4265 dl->devname = xstrdup(name);
cdddbdbc 4266
d23fe947 4267 /* look up this disk's index in the current anchor */
a2b97981
DW
4268 disk = __serial_to_disk(dl->serial, super->anchor, &dl->index);
4269 if (disk) {
4270 dl->disk = *disk;
4271 /* only set index on disks that are a member of a
4272 * populated contianer, i.e. one with raid_devs
4273 */
4274 if (is_failed(&dl->disk))
3f6efecc 4275 dl->index = -2;
2432ce9b 4276 else if (is_spare(&dl->disk) || is_journal(&dl->disk))
a2b97981 4277 dl->index = -1;
3f6efecc
DW
4278 }
4279
949c47a0
DW
4280 return 0;
4281}
4282
0c046afd
DW
4283/* When migrating map0 contains the 'destination' state while map1
4284 * contains the current state. When not migrating map0 contains the
4285 * current state. This routine assumes that map[0].map_state is set to
4286 * the current array state before being called.
4287 *
4288 * Migration is indicated by one of the following states
4289 * 1/ Idle (migr_state=0 map0state=normal||unitialized||degraded||failed)
e3bba0e0 4290 * 2/ Initialize (migr_state=1 migr_type=MIGR_INIT map0state=normal
0c046afd 4291 * map1state=unitialized)
1484e727 4292 * 3/ Repair (Resync) (migr_state=1 migr_type=MIGR_REPAIR map0state=normal
0c046afd 4293 * map1state=normal)
e3bba0e0 4294 * 4/ Rebuild (migr_state=1 migr_type=MIGR_REBUILD map0state=normal
0c046afd 4295 * map1state=degraded)
8e59f3d8
AK
4296 * 5/ Migration (mig_state=1 migr_type=MIGR_GEN_MIGR map0state=normal
4297 * map1state=normal)
0c046afd 4298 */
8e59f3d8
AK
4299static void migrate(struct imsm_dev *dev, struct intel_super *super,
4300 __u8 to_state, int migr_type)
3393c6af 4301{
0c046afd 4302 struct imsm_map *dest;
238c0a71 4303 struct imsm_map *src = get_imsm_map(dev, MAP_0);
3393c6af 4304
0c046afd 4305 dev->vol.migr_state = 1;
1484e727 4306 set_migr_type(dev, migr_type);
4036e7ee 4307 set_vol_curr_migr_unit(dev, 0);
238c0a71 4308 dest = get_imsm_map(dev, MAP_1);
0c046afd 4309
0556e1a2 4310 /* duplicate and then set the target end state in map[0] */
3393c6af 4311 memcpy(dest, src, sizeof_imsm_map(src));
fb12a745 4312 if (migr_type == MIGR_GEN_MIGR) {
0556e1a2
DW
4313 __u32 ord;
4314 int i;
4315
4316 for (i = 0; i < src->num_members; i++) {
4317 ord = __le32_to_cpu(src->disk_ord_tbl[i]);
4318 set_imsm_ord_tbl_ent(src, i, ord_to_idx(ord));
4319 }
4320 }
4321
8e59f3d8
AK
4322 if (migr_type == MIGR_GEN_MIGR)
4323 /* Clear migration record */
4324 memset(super->migr_rec, 0, sizeof(struct migr_record));
4325
0c046afd 4326 src->map_state = to_state;
949c47a0 4327}
f8f603f1 4328
809da78e
AK
4329static void end_migration(struct imsm_dev *dev, struct intel_super *super,
4330 __u8 map_state)
f8f603f1 4331{
238c0a71
AK
4332 struct imsm_map *map = get_imsm_map(dev, MAP_0);
4333 struct imsm_map *prev = get_imsm_map(dev, dev->vol.migr_state == 0 ?
4334 MAP_0 : MAP_1);
28bce06f 4335 int i, j;
0556e1a2
DW
4336
4337 /* merge any IMSM_ORD_REBUILD bits that were not successfully
4338 * completed in the last migration.
4339 *
28bce06f 4340 * FIXME add support for raid-level-migration
0556e1a2 4341 */
195d1d76 4342 if (map_state != map->map_state && (is_gen_migration(dev) == false) &&
089f9d79 4343 prev->map_state != IMSM_T_STATE_UNINITIALIZED) {
809da78e
AK
4344 /* when final map state is other than expected
4345 * merge maps (not for migration)
4346 */
4347 int failed;
4348
4349 for (i = 0; i < prev->num_members; i++)
4350 for (j = 0; j < map->num_members; j++)
4351 /* during online capacity expansion
4352 * disks position can be changed
4353 * if takeover is used
4354 */
4355 if (ord_to_idx(map->disk_ord_tbl[j]) ==
4356 ord_to_idx(prev->disk_ord_tbl[i])) {
4357 map->disk_ord_tbl[j] |=
4358 prev->disk_ord_tbl[i];
4359 break;
4360 }
4361 failed = imsm_count_failed(super, dev, MAP_0);
4362 map_state = imsm_check_degraded(super, dev, failed, MAP_0);
4363 }
f8f603f1
DW
4364
4365 dev->vol.migr_state = 0;
ea672ee1 4366 set_migr_type(dev, 0);
4036e7ee 4367 set_vol_curr_migr_unit(dev, 0);
f8f603f1
DW
4368 map->map_state = map_state;
4369}
949c47a0
DW
4370
4371static int parse_raid_devices(struct intel_super *super)
4372{
4373 int i;
4374 struct imsm_dev *dev_new;
4d7b1503 4375 size_t len, len_migr;
401d313b 4376 size_t max_len = 0;
4d7b1503
DW
4377 size_t space_needed = 0;
4378 struct imsm_super *mpb = super->anchor;
949c47a0
DW
4379
4380 for (i = 0; i < super->anchor->num_raid_devs; i++) {
4381 struct imsm_dev *dev_iter = __get_imsm_dev(super->anchor, i);
ba2de7ba 4382 struct intel_dev *dv;
949c47a0 4383
4d7b1503
DW
4384 len = sizeof_imsm_dev(dev_iter, 0);
4385 len_migr = sizeof_imsm_dev(dev_iter, 1);
4386 if (len_migr > len)
4387 space_needed += len_migr - len;
ca9de185 4388
503975b9 4389 dv = xmalloc(sizeof(*dv));
401d313b
AK
4390 if (max_len < len_migr)
4391 max_len = len_migr;
4392 if (max_len > len_migr)
4393 space_needed += max_len - len_migr;
503975b9 4394 dev_new = xmalloc(max_len);
949c47a0 4395 imsm_copy_dev(dev_new, dev_iter);
ba2de7ba
DW
4396 dv->dev = dev_new;
4397 dv->index = i;
4398 dv->next = super->devlist;
4399 super->devlist = dv;
949c47a0 4400 }
cdddbdbc 4401
4d7b1503
DW
4402 /* ensure that super->buf is large enough when all raid devices
4403 * are migrating
4404 */
4405 if (__le32_to_cpu(mpb->mpb_size) + space_needed > super->len) {
4406 void *buf;
4407
f36a9ecd
PB
4408 len = ROUND_UP(__le32_to_cpu(mpb->mpb_size) + space_needed,
4409 super->sector_size);
4410 if (posix_memalign(&buf, MAX_SECTOR_SIZE, len) != 0)
4d7b1503
DW
4411 return 1;
4412
1f45a8ad
DW
4413 memcpy(buf, super->buf, super->len);
4414 memset(buf + super->len, 0, len - super->len);
4d7b1503
DW
4415 free(super->buf);
4416 super->buf = buf;
4417 super->len = len;
4418 }
ca9de185 4419
bbab0940
TM
4420 super->extra_space += space_needed;
4421
cdddbdbc
DW
4422 return 0;
4423}
4424
e2f41b2c
AK
4425/*******************************************************************************
4426 * Function: check_mpb_migr_compatibility
4427 * Description: Function checks for unsupported migration features:
4428 * - migration optimization area (pba_of_lba0)
4429 * - descending reshape (ascending_migr)
4430 * Parameters:
4431 * super : imsm metadata information
4432 * Returns:
4433 * 0 : migration is compatible
4434 * -1 : migration is not compatible
4435 ******************************************************************************/
4436int check_mpb_migr_compatibility(struct intel_super *super)
4437{
4438 struct imsm_map *map0, *map1;
4439 struct migr_record *migr_rec = super->migr_rec;
4440 int i;
4441
4442 for (i = 0; i < super->anchor->num_raid_devs; i++) {
4443 struct imsm_dev *dev_iter = __get_imsm_dev(super->anchor, i);
4444
756a15f3 4445 if (dev_iter->vol.migr_state == 1 &&
e2f41b2c
AK
4446 dev_iter->vol.migr_type == MIGR_GEN_MIGR) {
4447 /* This device is migrating */
238c0a71
AK
4448 map0 = get_imsm_map(dev_iter, MAP_0);
4449 map1 = get_imsm_map(dev_iter, MAP_1);
5551b113 4450 if (pba_of_lba0(map0) != pba_of_lba0(map1))
e2f41b2c
AK
4451 /* migration optimization area was used */
4452 return -1;
fc54fe7a
JS
4453 if (migr_rec->ascending_migr == 0 &&
4454 migr_rec->dest_depth_per_unit > 0)
e2f41b2c
AK
4455 /* descending reshape not supported yet */
4456 return -1;
4457 }
4458 }
4459 return 0;
4460}
4461
d23fe947 4462static void __free_imsm(struct intel_super *super, int free_disks);
9ca2c81c 4463
cdddbdbc 4464/* load_imsm_mpb - read matrix metadata
f2f5c343 4465 * allocates super->mpb to be freed by free_imsm
cdddbdbc
DW
4466 */
4467static int load_imsm_mpb(int fd, struct intel_super *super, char *devname)
4468{
4469 unsigned long long dsize;
cdddbdbc 4470 unsigned long long sectors;
f36a9ecd 4471 unsigned int sector_size = super->sector_size;
cdddbdbc 4472 struct stat;
6416d527 4473 struct imsm_super *anchor;
cdddbdbc
DW
4474 __u32 check_sum;
4475
cdddbdbc 4476 get_dev_size(fd, NULL, &dsize);
f36a9ecd 4477 if (dsize < 2*sector_size) {
64436f06 4478 if (devname)
e7b84f9d
N
4479 pr_err("%s: device to small for imsm\n",
4480 devname);
64436f06
N
4481 return 1;
4482 }
cdddbdbc 4483
f36a9ecd 4484 if (lseek64(fd, dsize - (sector_size * 2), SEEK_SET) < 0) {
cdddbdbc 4485 if (devname)
e7b84f9d
N
4486 pr_err("Cannot seek to anchor block on %s: %s\n",
4487 devname, strerror(errno));
cdddbdbc
DW
4488 return 1;
4489 }
4490
f36a9ecd 4491 if (posix_memalign((void **)&anchor, sector_size, sector_size) != 0) {
ad97895e 4492 if (devname)
7a862a02 4493 pr_err("Failed to allocate imsm anchor buffer on %s\n", devname);
ad97895e
DW
4494 return 1;
4495 }
466070ad 4496 if ((unsigned int)read(fd, anchor, sector_size) != sector_size) {
cdddbdbc 4497 if (devname)
e7b84f9d
N
4498 pr_err("Cannot read anchor block on %s: %s\n",
4499 devname, strerror(errno));
6416d527 4500 free(anchor);
cdddbdbc
DW
4501 return 1;
4502 }
4503
6416d527 4504 if (strncmp((char *) anchor->sig, MPB_SIGNATURE, MPB_SIG_LEN) != 0) {
cdddbdbc 4505 if (devname)
e7b84f9d 4506 pr_err("no IMSM anchor on %s\n", devname);
6416d527 4507 free(anchor);
cdddbdbc
DW
4508 return 2;
4509 }
4510
d23fe947 4511 __free_imsm(super, 0);
f2f5c343
LM
4512 /* reload capability and hba */
4513
4514 /* capability and hba must be updated with new super allocation */
d424212e 4515 find_intel_hba_capability(fd, super, devname);
f36a9ecd
PB
4516 super->len = ROUND_UP(anchor->mpb_size, sector_size);
4517 if (posix_memalign(&super->buf, MAX_SECTOR_SIZE, super->len) != 0) {
cdddbdbc 4518 if (devname)
e7b84f9d
N
4519 pr_err("unable to allocate %zu byte mpb buffer\n",
4520 super->len);
6416d527 4521 free(anchor);
cdddbdbc
DW
4522 return 2;
4523 }
f36a9ecd 4524 memcpy(super->buf, anchor, sector_size);
cdddbdbc 4525
f36a9ecd 4526 sectors = mpb_sectors(anchor, sector_size) - 1;
6416d527 4527 free(anchor);
8e59f3d8 4528
85337573
AO
4529 if (posix_memalign(&super->migr_rec_buf, MAX_SECTOR_SIZE,
4530 MIGR_REC_BUF_SECTORS*MAX_SECTOR_SIZE) != 0) {
1ade5cc1 4531 pr_err("could not allocate migr_rec buffer\n");
8e59f3d8 4532 free(super->buf);
50cd06b4 4533 super->buf = NULL;
8e59f3d8
AK
4534 return 2;
4535 }
51d83f5d 4536 super->clean_migration_record_by_mdmon = 0;
8e59f3d8 4537
949c47a0 4538 if (!sectors) {
ecf45690
DW
4539 check_sum = __gen_imsm_checksum(super->anchor);
4540 if (check_sum != __le32_to_cpu(super->anchor->check_sum)) {
4541 if (devname)
e7b84f9d
N
4542 pr_err("IMSM checksum %x != %x on %s\n",
4543 check_sum,
4544 __le32_to_cpu(super->anchor->check_sum),
4545 devname);
ecf45690
DW
4546 return 2;
4547 }
4548
a2b97981 4549 return 0;
949c47a0 4550 }
cdddbdbc
DW
4551
4552 /* read the extended mpb */
f36a9ecd 4553 if (lseek64(fd, dsize - (sector_size * (2 + sectors)), SEEK_SET) < 0) {
cdddbdbc 4554 if (devname)
e7b84f9d
N
4555 pr_err("Cannot seek to extended mpb on %s: %s\n",
4556 devname, strerror(errno));
cdddbdbc
DW
4557 return 1;
4558 }
4559
f36a9ecd
PB
4560 if ((unsigned int)read(fd, super->buf + sector_size,
4561 super->len - sector_size) != super->len - sector_size) {
cdddbdbc 4562 if (devname)
e7b84f9d
N
4563 pr_err("Cannot read extended mpb on %s: %s\n",
4564 devname, strerror(errno));
cdddbdbc
DW
4565 return 2;
4566 }
4567
949c47a0
DW
4568 check_sum = __gen_imsm_checksum(super->anchor);
4569 if (check_sum != __le32_to_cpu(super->anchor->check_sum)) {
cdddbdbc 4570 if (devname)
e7b84f9d
N
4571 pr_err("IMSM checksum %x != %x on %s\n",
4572 check_sum, __le32_to_cpu(super->anchor->check_sum),
4573 devname);
db575f3b 4574 return 3;
cdddbdbc
DW
4575 }
4576
a2b97981
DW
4577 return 0;
4578}
4579
8e59f3d8
AK
4580static int read_imsm_migr_rec(int fd, struct intel_super *super);
4581
97f81ee2
CA
4582/* clears hi bits in metadata if MPB_ATTRIB_2TB_DISK not set */
4583static void clear_hi(struct intel_super *super)
4584{
4585 struct imsm_super *mpb = super->anchor;
4586 int i, n;
4587 if (mpb->attributes & MPB_ATTRIB_2TB_DISK)
4588 return;
4589 for (i = 0; i < mpb->num_disks; ++i) {
4590 struct imsm_disk *disk = &mpb->disk[i];
4591 disk->total_blocks_hi = 0;
4592 }
4593 for (i = 0; i < mpb->num_raid_devs; ++i) {
4594 struct imsm_dev *dev = get_imsm_dev(super, i);
97f81ee2
CA
4595 for (n = 0; n < 2; ++n) {
4596 struct imsm_map *map = get_imsm_map(dev, n);
4597 if (!map)
4598 continue;
4599 map->pba_of_lba0_hi = 0;
4600 map->blocks_per_member_hi = 0;
4601 map->num_data_stripes_hi = 0;
4602 }
4603 }
4604}
4605
a2b97981
DW
4606static int
4607load_and_parse_mpb(int fd, struct intel_super *super, char *devname, int keep_fd)
4608{
4609 int err;
4610
4611 err = load_imsm_mpb(fd, super, devname);
4612 if (err)
4613 return err;
f36a9ecd
PB
4614 if (super->sector_size == 4096)
4615 convert_from_4k(super);
a2b97981
DW
4616 err = load_imsm_disk(fd, super, devname, keep_fd);
4617 if (err)
4618 return err;
4619 err = parse_raid_devices(super);
8d67477f
TM
4620 if (err)
4621 return err;
4622 err = load_bbm_log(super);
97f81ee2 4623 clear_hi(super);
a2b97981 4624 return err;
cdddbdbc
DW
4625}
4626
4389ce73 4627static void __free_imsm_disk(struct dl *d, int do_close)
ae6aad82 4628{
4389ce73
MT
4629 if (do_close)
4630 close_fd(&d->fd);
ae6aad82
DW
4631 if (d->devname)
4632 free(d->devname);
0dcecb2e
DW
4633 if (d->e)
4634 free(d->e);
ae6aad82
DW
4635 free(d);
4636
4637}
1a64be56 4638
cdddbdbc
DW
4639static void free_imsm_disks(struct intel_super *super)
4640{
47ee5a45 4641 struct dl *d;
cdddbdbc 4642
47ee5a45
DW
4643 while (super->disks) {
4644 d = super->disks;
cdddbdbc 4645 super->disks = d->next;
3a85bf0e 4646 __free_imsm_disk(d, 1);
cdddbdbc 4647 }
cb82edca
AK
4648 while (super->disk_mgmt_list) {
4649 d = super->disk_mgmt_list;
4650 super->disk_mgmt_list = d->next;
3a85bf0e 4651 __free_imsm_disk(d, 1);
cb82edca 4652 }
47ee5a45
DW
4653 while (super->missing) {
4654 d = super->missing;
4655 super->missing = d->next;
3a85bf0e 4656 __free_imsm_disk(d, 1);
47ee5a45
DW
4657 }
4658
cdddbdbc
DW
4659}
4660
9ca2c81c 4661/* free all the pieces hanging off of a super pointer */
d23fe947 4662static void __free_imsm(struct intel_super *super, int free_disks)
cdddbdbc 4663{
88654014
LM
4664 struct intel_hba *elem, *next;
4665
9ca2c81c 4666 if (super->buf) {
949c47a0 4667 free(super->buf);
9ca2c81c
DW
4668 super->buf = NULL;
4669 }
f2f5c343
LM
4670 /* unlink capability description */
4671 super->orom = NULL;
8e59f3d8
AK
4672 if (super->migr_rec_buf) {
4673 free(super->migr_rec_buf);
4674 super->migr_rec_buf = NULL;
4675 }
d23fe947
DW
4676 if (free_disks)
4677 free_imsm_disks(super);
ba2de7ba 4678 free_devlist(super);
88654014
LM
4679 elem = super->hba;
4680 while (elem) {
4681 if (elem->path)
4682 free((void *)elem->path);
4683 next = elem->next;
4684 free(elem);
4685 elem = next;
88c32bb1 4686 }
8d67477f
TM
4687 if (super->bbm_log)
4688 free(super->bbm_log);
88654014 4689 super->hba = NULL;
cdddbdbc
DW
4690}
4691
9ca2c81c
DW
4692static void free_imsm(struct intel_super *super)
4693{
d23fe947 4694 __free_imsm(super, 1);
928f1424 4695 free(super->bb.entries);
9ca2c81c
DW
4696 free(super);
4697}
cdddbdbc
DW
4698
4699static void free_super_imsm(struct supertype *st)
4700{
4701 struct intel_super *super = st->sb;
4702
4703 if (!super)
4704 return;
4705
4706 free_imsm(super);
4707 st->sb = NULL;
4708}
4709
49133e57 4710static struct intel_super *alloc_super(void)
c2c087e6 4711{
503975b9 4712 struct intel_super *super = xcalloc(1, sizeof(*super));
c2c087e6 4713
503975b9
N
4714 super->current_vol = -1;
4715 super->create_offset = ~((unsigned long long) 0);
928f1424
TM
4716
4717 super->bb.entries = xmalloc(BBM_LOG_MAX_ENTRIES *
4718 sizeof(struct md_bb_entry));
4719 if (!super->bb.entries) {
4720 free(super);
4721 return NULL;
4722 }
4723
c2c087e6
DW
4724 return super;
4725}
4726
f0f5a016
LM
4727/*
4728 * find and allocate hba and OROM/EFI based on valid fd of RAID component device
4729 */
d424212e 4730static int find_intel_hba_capability(int fd, struct intel_super *super, char *devname)
f0f5a016
LM
4731{
4732 struct sys_dev *hba_name;
4733 int rv = 0;
4734
4389ce73 4735 if (is_fd_valid(fd) && test_partition(fd)) {
3a30e28e
MT
4736 pr_err("imsm: %s is a partition, cannot be used in IMSM\n",
4737 devname);
4738 return 1;
4739 }
420dafcd 4740 if (!is_fd_valid(fd) || check_no_platform()) {
f2f5c343 4741 super->orom = NULL;
f0f5a016
LM
4742 super->hba = NULL;
4743 return 0;
4744 }
4745 hba_name = find_disk_attached_hba(fd, NULL);
4746 if (!hba_name) {
d424212e 4747 if (devname)
e7b84f9d
N
4748 pr_err("%s is not attached to Intel(R) RAID controller.\n",
4749 devname);
f0f5a016
LM
4750 return 1;
4751 }
4752 rv = attach_hba_to_super(super, hba_name);
4753 if (rv == 2) {
d424212e
N
4754 if (devname) {
4755 struct intel_hba *hba = super->hba;
f0f5a016 4756
60f0f54d
PB
4757 pr_err("%s is attached to Intel(R) %s %s (%s),\n"
4758 " but the container is assigned to Intel(R) %s %s (",
d424212e 4759 devname,
614902f6 4760 get_sys_dev_type(hba_name->type),
75350d87
KF
4761 hba_name->type == SYS_DEV_VMD || hba_name->type == SYS_DEV_SATA_VMD ?
4762 "domain" : "RAID controller",
f0f5a016 4763 hba_name->pci_id ? : "Err!",
60f0f54d 4764 get_sys_dev_type(super->hba->type),
75350d87
KF
4765 hba->type == SYS_DEV_VMD || hba_name->type == SYS_DEV_SATA_VMD ?
4766 "domain" : "RAID controller");
f0f5a016 4767
f0f5a016
LM
4768 while (hba) {
4769 fprintf(stderr, "%s", hba->pci_id ? : "Err!");
4770 if (hba->next)
4771 fprintf(stderr, ", ");
4772 hba = hba->next;
4773 }
6b781d33 4774 fprintf(stderr, ").\n"
cca67208 4775 " Mixing devices attached to different controllers is not allowed.\n");
f0f5a016 4776 }
f0f5a016
LM
4777 return 2;
4778 }
6b781d33 4779 super->orom = find_imsm_capability(hba_name);
f2f5c343
LM
4780 if (!super->orom)
4781 return 3;
614902f6 4782
f0f5a016
LM
4783 return 0;
4784}
4785
47ee5a45
DW
4786/* find_missing - helper routine for load_super_imsm_all that identifies
4787 * disks that have disappeared from the system. This routine relies on
4788 * the mpb being uptodate, which it is at load time.
4789 */
4790static int find_missing(struct intel_super *super)
4791{
4792 int i;
4793 struct imsm_super *mpb = super->anchor;
4794 struct dl *dl;
4795 struct imsm_disk *disk;
47ee5a45
DW
4796
4797 for (i = 0; i < mpb->num_disks; i++) {
4798 disk = __get_imsm_disk(mpb, i);
54c2c1ea 4799 dl = serial_to_dl(disk->serial, super);
47ee5a45
DW
4800 if (dl)
4801 continue;
47ee5a45 4802
503975b9 4803 dl = xmalloc(sizeof(*dl));
47ee5a45
DW
4804 dl->major = 0;
4805 dl->minor = 0;
4806 dl->fd = -1;
503975b9 4807 dl->devname = xstrdup("missing");
47ee5a45
DW
4808 dl->index = i;
4809 serialcpy(dl->serial, disk->serial);
4810 dl->disk = *disk;
689c9bf3 4811 dl->e = NULL;
47ee5a45
DW
4812 dl->next = super->missing;
4813 super->missing = dl;
4814 }
4815
4816 return 0;
4817}
4818
a2b97981
DW
4819static struct intel_disk *disk_list_get(__u8 *serial, struct intel_disk *disk_list)
4820{
4821 struct intel_disk *idisk = disk_list;
4822
4823 while (idisk) {
4824 if (serialcmp(idisk->disk.serial, serial) == 0)
4825 break;
4826 idisk = idisk->next;
4827 }
4828
4829 return idisk;
4830}
4831
4832static int __prep_thunderdome(struct intel_super **table, int tbl_size,
4833 struct intel_super *super,
4834 struct intel_disk **disk_list)
4835{
4836 struct imsm_disk *d = &super->disks->disk;
4837 struct imsm_super *mpb = super->anchor;
4838 int i, j;
4839
4840 for (i = 0; i < tbl_size; i++) {
4841 struct imsm_super *tbl_mpb = table[i]->anchor;
4842 struct imsm_disk *tbl_d = &table[i]->disks->disk;
4843
4844 if (tbl_mpb->family_num == mpb->family_num) {
4845 if (tbl_mpb->check_sum == mpb->check_sum) {
1ade5cc1
N
4846 dprintf("mpb from %d:%d matches %d:%d\n",
4847 super->disks->major,
a2b97981
DW
4848 super->disks->minor,
4849 table[i]->disks->major,
4850 table[i]->disks->minor);
4851 break;
4852 }
4853
4854 if (((is_configured(d) && !is_configured(tbl_d)) ||
4855 is_configured(d) == is_configured(tbl_d)) &&
4856 tbl_mpb->generation_num < mpb->generation_num) {
4857 /* current version of the mpb is a
4858 * better candidate than the one in
4859 * super_table, but copy over "cross
4860 * generational" status
4861 */
4862 struct intel_disk *idisk;
4863
1ade5cc1
N
4864 dprintf("mpb from %d:%d replaces %d:%d\n",
4865 super->disks->major,
a2b97981
DW
4866 super->disks->minor,
4867 table[i]->disks->major,
4868 table[i]->disks->minor);
4869
4870 idisk = disk_list_get(tbl_d->serial, *disk_list);
4871 if (idisk && is_failed(&idisk->disk))
4872 tbl_d->status |= FAILED_DISK;
4873 break;
4874 } else {
4875 struct intel_disk *idisk;
4876 struct imsm_disk *disk;
4877
4878 /* tbl_mpb is more up to date, but copy
4879 * over cross generational status before
4880 * returning
4881 */
4882 disk = __serial_to_disk(d->serial, mpb, NULL);
4883 if (disk && is_failed(disk))
4884 d->status |= FAILED_DISK;
4885
4886 idisk = disk_list_get(d->serial, *disk_list);
4887 if (idisk) {
4888 idisk->owner = i;
4889 if (disk && is_configured(disk))
4890 idisk->disk.status |= CONFIGURED_DISK;
4891 }
4892
1ade5cc1
N
4893 dprintf("mpb from %d:%d prefer %d:%d\n",
4894 super->disks->major,
a2b97981
DW
4895 super->disks->minor,
4896 table[i]->disks->major,
4897 table[i]->disks->minor);
4898
4899 return tbl_size;
4900 }
4901 }
4902 }
4903
4904 if (i >= tbl_size)
4905 table[tbl_size++] = super;
4906 else
4907 table[i] = super;
4908
4909 /* update/extend the merged list of imsm_disk records */
4910 for (j = 0; j < mpb->num_disks; j++) {
4911 struct imsm_disk *disk = __get_imsm_disk(mpb, j);
4912 struct intel_disk *idisk;
4913
4914 idisk = disk_list_get(disk->serial, *disk_list);
4915 if (idisk) {
4916 idisk->disk.status |= disk->status;
4917 if (is_configured(&idisk->disk) ||
4918 is_failed(&idisk->disk))
4919 idisk->disk.status &= ~(SPARE_DISK);
4920 } else {
503975b9 4921 idisk = xcalloc(1, sizeof(*idisk));
a2b97981
DW
4922 idisk->owner = IMSM_UNKNOWN_OWNER;
4923 idisk->disk = *disk;
4924 idisk->next = *disk_list;
4925 *disk_list = idisk;
4926 }
4927
4928 if (serialcmp(idisk->disk.serial, d->serial) == 0)
4929 idisk->owner = i;
4930 }
4931
4932 return tbl_size;
4933}
4934
4935static struct intel_super *
4936validate_members(struct intel_super *super, struct intel_disk *disk_list,
4937 const int owner)
4938{
4939 struct imsm_super *mpb = super->anchor;
4940 int ok_count = 0;
4941 int i;
4942
4943 for (i = 0; i < mpb->num_disks; i++) {
4944 struct imsm_disk *disk = __get_imsm_disk(mpb, i);
4945 struct intel_disk *idisk;
4946
4947 idisk = disk_list_get(disk->serial, disk_list);
4948 if (idisk) {
4949 if (idisk->owner == owner ||
4950 idisk->owner == IMSM_UNKNOWN_OWNER)
4951 ok_count++;
4952 else
1ade5cc1
N
4953 dprintf("'%.16s' owner %d != %d\n",
4954 disk->serial, idisk->owner,
a2b97981
DW
4955 owner);
4956 } else {
1ade5cc1
N
4957 dprintf("unknown disk %x [%d]: %.16s\n",
4958 __le32_to_cpu(mpb->family_num), i,
a2b97981
DW
4959 disk->serial);
4960 break;
4961 }
4962 }
4963
4964 if (ok_count == mpb->num_disks)
4965 return super;
4966 return NULL;
4967}
4968
4969static void show_conflicts(__u32 family_num, struct intel_super *super_list)
4970{
4971 struct intel_super *s;
4972
4973 for (s = super_list; s; s = s->next) {
4974 if (family_num != s->anchor->family_num)
4975 continue;
e12b3daa 4976 pr_err("Conflict, offlining family %#x on '%s'\n",
a2b97981
DW
4977 __le32_to_cpu(family_num), s->disks->devname);
4978 }
4979}
4980
4981static struct intel_super *
4982imsm_thunderdome(struct intel_super **super_list, int len)
4983{
4984 struct intel_super *super_table[len];
4985 struct intel_disk *disk_list = NULL;
4986 struct intel_super *champion, *spare;
4987 struct intel_super *s, **del;
4988 int tbl_size = 0;
4989 int conflict;
4990 int i;
4991
4992 memset(super_table, 0, sizeof(super_table));
4993 for (s = *super_list; s; s = s->next)
4994 tbl_size = __prep_thunderdome(super_table, tbl_size, s, &disk_list);
4995
4996 for (i = 0; i < tbl_size; i++) {
4997 struct imsm_disk *d;
4998 struct intel_disk *idisk;
4999 struct imsm_super *mpb = super_table[i]->anchor;
5000
5001 s = super_table[i];
5002 d = &s->disks->disk;
5003
5004 /* 'd' must appear in merged disk list for its
5005 * configuration to be valid
5006 */
5007 idisk = disk_list_get(d->serial, disk_list);
5008 if (idisk && idisk->owner == i)
5009 s = validate_members(s, disk_list, i);
5010 else
5011 s = NULL;
5012
5013 if (!s)
1ade5cc1
N
5014 dprintf("marking family: %#x from %d:%d offline\n",
5015 mpb->family_num,
a2b97981
DW
5016 super_table[i]->disks->major,
5017 super_table[i]->disks->minor);
5018 super_table[i] = s;
5019 }
5020
5021 /* This is where the mdadm implementation differs from the Windows
5022 * driver which has no strict concept of a container. We can only
5023 * assemble one family from a container, so when returning a prodigal
5024 * array member to this system the code will not be able to disambiguate
5025 * the container contents that should be assembled ("foreign" versus
5026 * "local"). It requires user intervention to set the orig_family_num
5027 * to a new value to establish a new container. The Windows driver in
5028 * this situation fixes up the volume name in place and manages the
5029 * foreign array as an independent entity.
5030 */
5031 s = NULL;
5032 spare = NULL;
5033 conflict = 0;
5034 for (i = 0; i < tbl_size; i++) {
5035 struct intel_super *tbl_ent = super_table[i];
5036 int is_spare = 0;
5037
5038 if (!tbl_ent)
5039 continue;
5040
5041 if (tbl_ent->anchor->num_raid_devs == 0) {
5042 spare = tbl_ent;
5043 is_spare = 1;
5044 }
5045
5046 if (s && !is_spare) {
5047 show_conflicts(tbl_ent->anchor->family_num, *super_list);
5048 conflict++;
5049 } else if (!s && !is_spare)
5050 s = tbl_ent;
5051 }
5052
5053 if (!s)
5054 s = spare;
5055 if (!s) {
5056 champion = NULL;
5057 goto out;
5058 }
5059 champion = s;
5060
5061 if (conflict)
7a862a02 5062 pr_err("Chose family %#x on '%s', assemble conflicts to new container with '--update=uuid'\n",
a2b97981
DW
5063 __le32_to_cpu(s->anchor->family_num), s->disks->devname);
5064
5065 /* collect all dl's onto 'champion', and update them to
5066 * champion's version of the status
5067 */
5068 for (s = *super_list; s; s = s->next) {
5069 struct imsm_super *mpb = champion->anchor;
5070 struct dl *dl = s->disks;
5071
5072 if (s == champion)
5073 continue;
5074
5d7b407a
CA
5075 mpb->attributes |= s->anchor->attributes & MPB_ATTRIB_2TB_DISK;
5076
a2b97981
DW
5077 for (i = 0; i < mpb->num_disks; i++) {
5078 struct imsm_disk *disk;
5079
5080 disk = __serial_to_disk(dl->serial, mpb, &dl->index);
5081 if (disk) {
5082 dl->disk = *disk;
5083 /* only set index on disks that are a member of
5084 * a populated contianer, i.e. one with
5085 * raid_devs
5086 */
5087 if (is_failed(&dl->disk))
5088 dl->index = -2;
5089 else if (is_spare(&dl->disk))
5090 dl->index = -1;
5091 break;
5092 }
5093 }
5094
5095 if (i >= mpb->num_disks) {
5096 struct intel_disk *idisk;
5097
5098 idisk = disk_list_get(dl->serial, disk_list);
ecf408e9 5099 if (idisk && is_spare(&idisk->disk) &&
a2b97981
DW
5100 !is_failed(&idisk->disk) && !is_configured(&idisk->disk))
5101 dl->index = -1;
5102 else {
5103 dl->index = -2;
5104 continue;
5105 }
5106 }
5107
5108 dl->next = champion->disks;
5109 champion->disks = dl;
5110 s->disks = NULL;
5111 }
5112
5113 /* delete 'champion' from super_list */
5114 for (del = super_list; *del; ) {
5115 if (*del == champion) {
5116 *del = (*del)->next;
5117 break;
5118 } else
5119 del = &(*del)->next;
5120 }
5121 champion->next = NULL;
5122
5123 out:
5124 while (disk_list) {
5125 struct intel_disk *idisk = disk_list;
5126
5127 disk_list = disk_list->next;
5128 free(idisk);
5129 }
5130
5131 return champion;
5132}
5133
9587c373
LM
5134static int
5135get_sra_super_block(int fd, struct intel_super **super_list, char *devname, int *max, int keep_fd);
4dd2df09 5136static int get_super_block(struct intel_super **super_list, char *devnm, char *devname,
9587c373 5137 int major, int minor, int keep_fd);
ec50f7b6
LM
5138static int
5139get_devlist_super_block(struct md_list *devlist, struct intel_super **super_list,
5140 int *max, int keep_fd);
5141
cdddbdbc 5142static int load_super_imsm_all(struct supertype *st, int fd, void **sbp,
ec50f7b6
LM
5143 char *devname, struct md_list *devlist,
5144 int keep_fd)
cdddbdbc 5145{
a2b97981
DW
5146 struct intel_super *super_list = NULL;
5147 struct intel_super *super = NULL;
a2b97981 5148 int err = 0;
9587c373 5149 int i = 0;
dab4a513 5150
4389ce73 5151 if (is_fd_valid(fd))
9587c373
LM
5152 /* 'fd' is an opened container */
5153 err = get_sra_super_block(fd, &super_list, devname, &i, keep_fd);
5154 else
ec50f7b6
LM
5155 /* get super block from devlist devices */
5156 err = get_devlist_super_block(devlist, &super_list, &i, keep_fd);
9587c373 5157 if (err)
1602d52c 5158 goto error;
a2b97981
DW
5159 /* all mpbs enter, maybe one leaves */
5160 super = imsm_thunderdome(&super_list, i);
5161 if (!super) {
5162 err = 1;
5163 goto error;
cdddbdbc
DW
5164 }
5165
47ee5a45
DW
5166 if (find_missing(super) != 0) {
5167 free_imsm(super);
a2b97981
DW
5168 err = 2;
5169 goto error;
47ee5a45 5170 }
8e59f3d8
AK
5171
5172 /* load migration record */
2f86fda3 5173 err = load_imsm_migr_rec(super);
4c965cc9
AK
5174 if (err == -1) {
5175 /* migration is in progress,
5176 * but migr_rec cannot be loaded,
5177 */
8e59f3d8
AK
5178 err = 4;
5179 goto error;
5180 }
e2f41b2c
AK
5181
5182 /* Check migration compatibility */
089f9d79 5183 if (err == 0 && check_mpb_migr_compatibility(super) != 0) {
e7b84f9d 5184 pr_err("Unsupported migration detected");
e2f41b2c
AK
5185 if (devname)
5186 fprintf(stderr, " on %s\n", devname);
5187 else
5188 fprintf(stderr, " (IMSM).\n");
5189
5190 err = 5;
5191 goto error;
5192 }
5193
a2b97981
DW
5194 err = 0;
5195
5196 error:
5197 while (super_list) {
5198 struct intel_super *s = super_list;
5199
5200 super_list = super_list->next;
5201 free_imsm(s);
5202 }
9587c373 5203
a2b97981
DW
5204 if (err)
5205 return err;
f7e7067b 5206
cdddbdbc 5207 *sbp = super;
4389ce73 5208 if (is_fd_valid(fd))
4dd2df09 5209 strcpy(st->container_devnm, fd2devnm(fd));
9587c373 5210 else
4dd2df09 5211 st->container_devnm[0] = 0;
a2b97981 5212 if (err == 0 && st->ss == NULL) {
bf5a934a 5213 st->ss = &super_imsm;
cdddbdbc
DW
5214 st->minor_version = 0;
5215 st->max_devs = IMSM_MAX_DEVICES;
5216 }
cdddbdbc
DW
5217 return 0;
5218}
2b959fbf 5219
ec50f7b6
LM
5220static int
5221get_devlist_super_block(struct md_list *devlist, struct intel_super **super_list,
5222 int *max, int keep_fd)
5223{
5224 struct md_list *tmpdev;
5225 int err = 0;
5226 int i = 0;
9587c373 5227
ec50f7b6
LM
5228 for (i = 0, tmpdev = devlist; tmpdev; tmpdev = tmpdev->next) {
5229 if (tmpdev->used != 1)
5230 continue;
5231 if (tmpdev->container == 1) {
ca9de185 5232 int lmax = 0;
ec50f7b6 5233 int fd = dev_open(tmpdev->devname, O_RDONLY|O_EXCL);
4389ce73 5234 if (!is_fd_valid(fd)) {
e7b84f9d 5235 pr_err("cannot open device %s: %s\n",
ec50f7b6
LM
5236 tmpdev->devname, strerror(errno));
5237 err = 8;
5238 goto error;
5239 }
5240 err = get_sra_super_block(fd, super_list,
5241 tmpdev->devname, &lmax,
5242 keep_fd);
5243 i += lmax;
5244 close(fd);
5245 if (err) {
5246 err = 7;
5247 goto error;
5248 }
5249 } else {
5250 int major = major(tmpdev->st_rdev);
5251 int minor = minor(tmpdev->st_rdev);
5252 err = get_super_block(super_list,
4dd2df09 5253 NULL,
ec50f7b6
LM
5254 tmpdev->devname,
5255 major, minor,
5256 keep_fd);
5257 i++;
5258 if (err) {
5259 err = 6;
5260 goto error;
5261 }
5262 }
5263 }
5264 error:
5265 *max = i;
5266 return err;
5267}
9587c373 5268
4dd2df09 5269static int get_super_block(struct intel_super **super_list, char *devnm, char *devname,
9587c373
LM
5270 int major, int minor, int keep_fd)
5271{
594dc1b8 5272 struct intel_super *s;
9587c373
LM
5273 char nm[32];
5274 int dfd = -1;
9587c373
LM
5275 int err = 0;
5276 int retry;
5277
5278 s = alloc_super();
5279 if (!s) {
5280 err = 1;
5281 goto error;
5282 }
5283
5284 sprintf(nm, "%d:%d", major, minor);
5285 dfd = dev_open(nm, O_RDWR);
4389ce73 5286 if (!is_fd_valid(dfd)) {
9587c373
LM
5287 err = 2;
5288 goto error;
5289 }
5290
aec01630
JS
5291 if (!get_dev_sector_size(dfd, NULL, &s->sector_size)) {
5292 err = 2;
5293 goto error;
5294 }
cb8f6859 5295 find_intel_hba_capability(dfd, s, devname);
9587c373
LM
5296 err = load_and_parse_mpb(dfd, s, NULL, keep_fd);
5297
5298 /* retry the load if we might have raced against mdmon */
4dd2df09 5299 if (err == 3 && devnm && mdmon_running(devnm))
9587c373 5300 for (retry = 0; retry < 3; retry++) {
239b3cc0 5301 sleep_for(0, MSEC_TO_NSEC(3), true);
9587c373
LM
5302 err = load_and_parse_mpb(dfd, s, NULL, keep_fd);
5303 if (err != 3)
5304 break;
5305 }
5306 error:
5307 if (!err) {
5308 s->next = *super_list;
5309 *super_list = s;
5310 } else {
5311 if (s)
8d67477f 5312 free_imsm(s);
4389ce73 5313 close_fd(&dfd);
9587c373 5314 }
4389ce73
MT
5315 if (!keep_fd)
5316 close_fd(&dfd);
9587c373
LM
5317 return err;
5318
5319}
5320
5321static int
5322get_sra_super_block(int fd, struct intel_super **super_list, char *devname, int *max, int keep_fd)
5323{
5324 struct mdinfo *sra;
4dd2df09 5325 char *devnm;
9587c373
LM
5326 struct mdinfo *sd;
5327 int err = 0;
5328 int i = 0;
4dd2df09 5329 sra = sysfs_read(fd, NULL, GET_LEVEL|GET_VERSION|GET_DEVS|GET_STATE);
9587c373
LM
5330 if (!sra)
5331 return 1;
5332
5333 if (sra->array.major_version != -1 ||
5334 sra->array.minor_version != -2 ||
5335 strcmp(sra->text_version, "imsm") != 0) {
5336 err = 1;
5337 goto error;
5338 }
5339 /* load all mpbs */
4dd2df09 5340 devnm = fd2devnm(fd);
9587c373 5341 for (sd = sra->devs, i = 0; sd; sd = sd->next, i++) {
4dd2df09 5342 if (get_super_block(super_list, devnm, devname,
9587c373
LM
5343 sd->disk.major, sd->disk.minor, keep_fd) != 0) {
5344 err = 7;
5345 goto error;
5346 }
5347 }
5348 error:
5349 sysfs_free(sra);
5350 *max = i;
5351 return err;
5352}
5353
2b959fbf
N
5354static int load_container_imsm(struct supertype *st, int fd, char *devname)
5355{
ec50f7b6 5356 return load_super_imsm_all(st, fd, &st->sb, devname, NULL, 1);
2b959fbf 5357}
cdddbdbc
DW
5358
5359static int load_super_imsm(struct supertype *st, int fd, char *devname)
5360{
5361 struct intel_super *super;
5362 int rv;
8a3544f8 5363 int retry;
cdddbdbc 5364
357ac106 5365 if (test_partition(fd))
691c6ee1
N
5366 /* IMSM not allowed on partitions */
5367 return 1;
5368
37424f13
DW
5369 free_super_imsm(st);
5370
49133e57 5371 super = alloc_super();
8d67477f
TM
5372 if (!super)
5373 return 1;
3a85bf0e
MG
5374
5375 if (!get_dev_sector_size(fd, NULL, &super->sector_size)) {
5376 free_imsm(super);
5377 return 1;
5378 }
ea2bc72b
LM
5379 /* Load hba and capabilities if they exist.
5380 * But do not preclude loading metadata in case capabilities or hba are
5381 * non-compliant and ignore_hw_compat is set.
5382 */
d424212e 5383 rv = find_intel_hba_capability(fd, super, devname);
f2f5c343 5384 /* no orom/efi or non-intel hba of the disk */
089f9d79 5385 if (rv != 0 && st->ignore_hw_compat == 0) {
f2f5c343 5386 if (devname)
e7b84f9d 5387 pr_err("No OROM/EFI properties for %s\n", devname);
f2f5c343
LM
5388 free_imsm(super);
5389 return 2;
5390 }
a2b97981 5391 rv = load_and_parse_mpb(fd, super, devname, 0);
cdddbdbc 5392
8a3544f8
AP
5393 /* retry the load if we might have raced against mdmon */
5394 if (rv == 3) {
f96b1302
AP
5395 struct mdstat_ent *mdstat = NULL;
5396 char *name = fd2kname(fd);
5397
5398 if (name)
5399 mdstat = mdstat_by_component(name);
8a3544f8
AP
5400
5401 if (mdstat && mdmon_running(mdstat->devnm) && getpid() != mdmon_pid(mdstat->devnm)) {
5402 for (retry = 0; retry < 3; retry++) {
239b3cc0 5403 sleep_for(0, MSEC_TO_NSEC(3), true);
8a3544f8
AP
5404 rv = load_and_parse_mpb(fd, super, devname, 0);
5405 if (rv != 3)
5406 break;
5407 }
5408 }
5409
5410 free_mdstat(mdstat);
5411 }
5412
cdddbdbc
DW
5413 if (rv) {
5414 if (devname)
7a862a02 5415 pr_err("Failed to load all information sections on %s\n", devname);
cdddbdbc
DW
5416 free_imsm(super);
5417 return rv;
5418 }
5419
5420 st->sb = super;
5421 if (st->ss == NULL) {
5422 st->ss = &super_imsm;
5423 st->minor_version = 0;
5424 st->max_devs = IMSM_MAX_DEVICES;
5425 }
8e59f3d8
AK
5426
5427 /* load migration record */
2f86fda3 5428 if (load_imsm_migr_rec(super) == 0) {
2e062e82
AK
5429 /* Check for unsupported migration features */
5430 if (check_mpb_migr_compatibility(super) != 0) {
e7b84f9d 5431 pr_err("Unsupported migration detected");
2e062e82
AK
5432 if (devname)
5433 fprintf(stderr, " on %s\n", devname);
5434 else
5435 fprintf(stderr, " (IMSM).\n");
5436 return 3;
5437 }
e2f41b2c
AK
5438 }
5439
cdddbdbc
DW
5440 return 0;
5441}
5442
ef6ffade
DW
5443static __u16 info_to_blocks_per_strip(mdu_array_info_t *info)
5444{
5445 if (info->level == 1)
5446 return 128;
5447 return info->chunk_size >> 9;
5448}
5449
5551b113
CA
5450static unsigned long long info_to_blocks_per_member(mdu_array_info_t *info,
5451 unsigned long long size)
fcfd9599 5452{
4025c288 5453 if (info->level == 1)
5551b113 5454 return size * 2;
4025c288 5455 else
5551b113 5456 return (size * 2) & ~(info_to_blocks_per_strip(info) - 1);
fcfd9599
DW
5457}
5458
4d1313e9
DW
5459static void imsm_update_version_info(struct intel_super *super)
5460{
5461 /* update the version and attributes */
5462 struct imsm_super *mpb = super->anchor;
5463 char *version;
5464 struct imsm_dev *dev;
5465 struct imsm_map *map;
5466 int i;
5467
5468 for (i = 0; i < mpb->num_raid_devs; i++) {
5469 dev = get_imsm_dev(super, i);
238c0a71 5470 map = get_imsm_map(dev, MAP_0);
4d1313e9
DW
5471 if (__le32_to_cpu(dev->size_high) > 0)
5472 mpb->attributes |= MPB_ATTRIB_2TB;
5473
5474 /* FIXME detect when an array spans a port multiplier */
5475 #if 0
5476 mpb->attributes |= MPB_ATTRIB_PM;
5477 #endif
5478
5479 if (mpb->num_raid_devs > 1 ||
5480 mpb->attributes != MPB_ATTRIB_CHECKSUM_VERIFY) {
5481 version = MPB_VERSION_ATTRIBS;
5482 switch (get_imsm_raid_level(map)) {
5483 case 0: mpb->attributes |= MPB_ATTRIB_RAID0; break;
5484 case 1: mpb->attributes |= MPB_ATTRIB_RAID1; break;
5485 case 10: mpb->attributes |= MPB_ATTRIB_RAID10; break;
5486 case 5: mpb->attributes |= MPB_ATTRIB_RAID5; break;
5487 }
5488 } else {
5489 if (map->num_members >= 5)
5490 version = MPB_VERSION_5OR6_DISK_ARRAY;
5491 else if (dev->status == DEV_CLONE_N_GO)
5492 version = MPB_VERSION_CNG;
5493 else if (get_imsm_raid_level(map) == 5)
5494 version = MPB_VERSION_RAID5;
5495 else if (map->num_members >= 3)
5496 version = MPB_VERSION_3OR4_DISK_ARRAY;
5497 else if (get_imsm_raid_level(map) == 1)
5498 version = MPB_VERSION_RAID1;
5499 else
5500 version = MPB_VERSION_RAID0;
5501 }
5502 strcpy(((char *) mpb->sig) + strlen(MPB_SIGNATURE), version);
5503 }
5504}
5505
e2eb503b
MT
5506/**
5507 * imsm_check_name() - check imsm naming criteria.
5508 * @super: &intel_super pointer, not NULL.
5509 * @name: name to check.
5510 * @verbose: verbose level.
5511 *
5512 * Name must be no longer than &MAX_RAID_SERIAL_LEN and must be unique across volumes.
5513 *
5514 * Returns: &true if @name matches, &false otherwise.
5515 */
5516static bool imsm_is_name_allowed(struct intel_super *super, const char * const name,
5517 const int verbose)
aa534678
DW
5518{
5519 struct imsm_super *mpb = super->anchor;
aa534678
DW
5520 int i;
5521
e2eb503b
MT
5522 if (is_string_lq(name, MAX_RAID_SERIAL_LEN + 1) == false) {
5523 pr_vrb("imsm: Name \"%s\" is too long\n", name);
5524 return false;
9bd99a90
RS
5525 }
5526
aa534678
DW
5527 for (i = 0; i < mpb->num_raid_devs; i++) {
5528 struct imsm_dev *dev = get_imsm_dev(super, i);
5529
5530 if (strncmp((char *) dev->volume, name, MAX_RAID_SERIAL_LEN) == 0) {
e2eb503b
MT
5531 pr_vrb("imsm: Name \"%s\" already exists\n", name);
5532 return false;
aa534678
DW
5533 }
5534 }
5535
e2eb503b 5536 return true;
aa534678
DW
5537}
5538
8b353278 5539static int init_super_imsm_volume(struct supertype *st, mdu_array_info_t *info,
5308f117 5540 struct shape *s, char *name,
83cd1e97
N
5541 char *homehost, int *uuid,
5542 long long data_offset)
cdddbdbc 5543{
c2c087e6
DW
5544 /* We are creating a volume inside a pre-existing container.
5545 * so st->sb is already set.
5546 */
5547 struct intel_super *super = st->sb;
f36a9ecd 5548 unsigned int sector_size = super->sector_size;
949c47a0 5549 struct imsm_super *mpb = super->anchor;
ba2de7ba 5550 struct intel_dev *dv;
c2c087e6
DW
5551 struct imsm_dev *dev;
5552 struct imsm_vol *vol;
5553 struct imsm_map *map;
5554 int idx = mpb->num_raid_devs;
5555 int i;
760365f9 5556 int namelen;
c2c087e6 5557 unsigned long long array_blocks;
2c092cad 5558 size_t size_old, size_new;
b53bfba6
TM
5559 unsigned int data_disks;
5560 unsigned long long size_per_member;
cdddbdbc 5561
88c32bb1 5562 if (super->orom && mpb->num_raid_devs >= super->orom->vpa) {
7a862a02 5563 pr_err("This imsm-container already has the maximum of %d volumes\n", super->orom->vpa);
c2c087e6
DW
5564 return 0;
5565 }
5566
2c092cad
DW
5567 /* ensure the mpb is large enough for the new data */
5568 size_old = __le32_to_cpu(mpb->mpb_size);
5569 size_new = disks_to_mpb_size(info->nr_disks);
5570 if (size_new > size_old) {
5571 void *mpb_new;
f36a9ecd 5572 size_t size_round = ROUND_UP(size_new, sector_size);
2c092cad 5573
f36a9ecd 5574 if (posix_memalign(&mpb_new, sector_size, size_round) != 0) {
e7b84f9d 5575 pr_err("could not allocate new mpb\n");
2c092cad
DW
5576 return 0;
5577 }
85337573
AO
5578 if (posix_memalign(&super->migr_rec_buf, MAX_SECTOR_SIZE,
5579 MIGR_REC_BUF_SECTORS*
5580 MAX_SECTOR_SIZE) != 0) {
1ade5cc1 5581 pr_err("could not allocate migr_rec buffer\n");
8e59f3d8
AK
5582 free(super->buf);
5583 free(super);
ea944c8f 5584 free(mpb_new);
8e59f3d8
AK
5585 return 0;
5586 }
2c092cad
DW
5587 memcpy(mpb_new, mpb, size_old);
5588 free(mpb);
5589 mpb = mpb_new;
949c47a0 5590 super->anchor = mpb_new;
2c092cad
DW
5591 mpb->mpb_size = __cpu_to_le32(size_new);
5592 memset(mpb_new + size_old, 0, size_round - size_old);
bbab0940 5593 super->len = size_round;
2c092cad 5594 }
bf5a934a 5595 super->current_vol = idx;
3960e579
DW
5596
5597 /* handle 'failed_disks' by either:
5598 * a) create dummy disk entries in the table if this the first
5599 * volume in the array. We add them here as this is the only
5600 * opportunity to add them. add_to_super_imsm_volume()
5601 * handles the non-failed disks and continues incrementing
5602 * mpb->num_disks.
5603 * b) validate that 'failed_disks' matches the current number
5604 * of missing disks if the container is populated
d23fe947 5605 */
3960e579 5606 if (super->current_vol == 0) {
d23fe947 5607 mpb->num_disks = 0;
3960e579
DW
5608 for (i = 0; i < info->failed_disks; i++) {
5609 struct imsm_disk *disk;
5610
5611 mpb->num_disks++;
5612 disk = __get_imsm_disk(mpb, i);
5613 disk->status = CONFIGURED_DISK | FAILED_DISK;
5614 disk->scsi_id = __cpu_to_le32(~(__u32)0);
5615 snprintf((char *) disk->serial, MAX_RAID_SERIAL_LEN,
5b975129 5616 "missing:%d", (__u8)i);
3960e579
DW
5617 }
5618 find_missing(super);
5619 } else {
5620 int missing = 0;
5621 struct dl *d;
5622
5623 for (d = super->missing; d; d = d->next)
5624 missing++;
5625 if (info->failed_disks > missing) {
e7b84f9d 5626 pr_err("unable to add 'missing' disk to container\n");
3960e579
DW
5627 return 0;
5628 }
5629 }
5a038140 5630
e2eb503b 5631 if (imsm_is_name_allowed(super, name, 1) == false)
aa534678 5632 return 0;
e2eb503b 5633
503975b9
N
5634 dv = xmalloc(sizeof(*dv));
5635 dev = xcalloc(1, sizeof(*dev) + sizeof(__u32) * (info->raid_disks - 1));
760365f9
JS
5636 /*
5637 * Explicitly allow truncating to not confuse gcc's
5638 * -Werror=stringop-truncation
5639 */
5640 namelen = min((int) strlen(name), MAX_RAID_SERIAL_LEN);
5641 memcpy(dev->volume, name, namelen);
e03640bd 5642 array_blocks = calc_array_size(info->level, info->raid_disks,
03bcbc65 5643 info->layout, info->chunk_size,
b53bfba6
TM
5644 s->size * BLOCKS_PER_KB);
5645 data_disks = get_data_disks(info->level, info->layout,
5646 info->raid_disks);
5647 array_blocks = round_size_to_mb(array_blocks, data_disks);
5648 size_per_member = array_blocks / data_disks;
979d38be 5649
fcc2c9da 5650 set_imsm_dev_size(dev, array_blocks);
1a2487c2 5651 dev->status = (DEV_READ_COALESCING | DEV_WRITE_COALESCING);
c2c087e6
DW
5652 vol = &dev->vol;
5653 vol->migr_state = 0;
1484e727 5654 set_migr_type(dev, MIGR_INIT);
3960e579 5655 vol->dirty = !info->state;
4036e7ee 5656 set_vol_curr_migr_unit(dev, 0);
238c0a71 5657 map = get_imsm_map(dev, MAP_0);
5551b113 5658 set_pba_of_lba0(map, super->create_offset);
ef6ffade 5659 map->blocks_per_strip = __cpu_to_le16(info_to_blocks_per_strip(info));
0556e1a2 5660 map->failed_disk_num = ~0;
bf4442ab 5661 if (info->level > 0)
fffaf1ff
N
5662 map->map_state = (info->state ? IMSM_T_STATE_NORMAL
5663 : IMSM_T_STATE_UNINITIALIZED);
bf4442ab
AK
5664 else
5665 map->map_state = info->failed_disks ? IMSM_T_STATE_FAILED :
5666 IMSM_T_STATE_NORMAL;
252d23c0 5667 map->ddf = 1;
ef6ffade
DW
5668
5669 if (info->level == 1 && info->raid_disks > 2) {
38950822
AW
5670 free(dev);
5671 free(dv);
7a862a02 5672 pr_err("imsm does not support more than 2 disksin a raid1 volume\n");
ef6ffade
DW
5673 return 0;
5674 }
81062a36
DW
5675
5676 map->raid_level = info->level;
1c275381 5677 if (info->level == 10)
c2c087e6 5678 map->raid_level = 1;
1c275381 5679 set_num_domains(map);
ef6ffade 5680
44490938
MD
5681 size_per_member += NUM_BLOCKS_DIRTY_STRIPE_REGION;
5682 set_blocks_per_member(map, info_to_blocks_per_member(info,
5683 size_per_member /
5684 BLOCKS_PER_KB));
5685
c2c087e6 5686 map->num_members = info->raid_disks;
1c275381 5687 update_num_data_stripes(map, array_blocks);
c2c087e6
DW
5688 for (i = 0; i < map->num_members; i++) {
5689 /* initialized in add_to_super */
4eb26970 5690 set_imsm_ord_tbl_ent(map, i, IMSM_ORD_REBUILD);
c2c087e6 5691 }
949c47a0 5692 mpb->num_raid_devs++;
2a24dc1b
PB
5693 mpb->num_raid_devs_created++;
5694 dev->my_vol_raid_dev_num = mpb->num_raid_devs_created;
ba2de7ba 5695
b7580566 5696 if (s->consistency_policy <= CONSISTENCY_POLICY_RESYNC) {
c2462068 5697 dev->rwh_policy = RWH_MULTIPLE_OFF;
2432ce9b 5698 } else if (s->consistency_policy == CONSISTENCY_POLICY_PPL) {
c2462068 5699 dev->rwh_policy = RWH_MULTIPLE_DISTRIBUTED;
2432ce9b
AP
5700 } else {
5701 free(dev);
5702 free(dv);
5703 pr_err("imsm does not support consistency policy %s\n",
5f21d674 5704 map_num_s(consistency_policies, s->consistency_policy));
2432ce9b
AP
5705 return 0;
5706 }
5707
ba2de7ba
DW
5708 dv->dev = dev;
5709 dv->index = super->current_vol;
5710 dv->next = super->devlist;
5711 super->devlist = dv;
c2c087e6 5712
4d1313e9
DW
5713 imsm_update_version_info(super);
5714
c2c087e6 5715 return 1;
cdddbdbc
DW
5716}
5717
bf5a934a 5718static int init_super_imsm(struct supertype *st, mdu_array_info_t *info,
5308f117 5719 struct shape *s, char *name,
83cd1e97
N
5720 char *homehost, int *uuid,
5721 unsigned long long data_offset)
bf5a934a
DW
5722{
5723 /* This is primarily called by Create when creating a new array.
5724 * We will then get add_to_super called for each component, and then
5725 * write_init_super called to write it out to each device.
5726 * For IMSM, Create can create on fresh devices or on a pre-existing
5727 * array.
5728 * To create on a pre-existing array a different method will be called.
5729 * This one is just for fresh drives.
5730 */
5731 struct intel_super *super;
5732 struct imsm_super *mpb;
5733 size_t mpb_size;
4d1313e9 5734 char *version;
bf5a934a 5735
83cd1e97 5736 if (data_offset != INVALID_SECTORS) {
ed503f89 5737 pr_err("data-offset not supported by imsm\n");
83cd1e97
N
5738 return 0;
5739 }
5740
bf5a934a 5741 if (st->sb)
5308f117 5742 return init_super_imsm_volume(st, info, s, name, homehost, uuid,
83cd1e97 5743 data_offset);
e683ca88
DW
5744
5745 if (info)
5746 mpb_size = disks_to_mpb_size(info->nr_disks);
5747 else
f36a9ecd 5748 mpb_size = MAX_SECTOR_SIZE;
bf5a934a 5749
49133e57 5750 super = alloc_super();
f36a9ecd
PB
5751 if (super &&
5752 posix_memalign(&super->buf, MAX_SECTOR_SIZE, mpb_size) != 0) {
8d67477f 5753 free_imsm(super);
e683ca88
DW
5754 super = NULL;
5755 }
5756 if (!super) {
1ade5cc1 5757 pr_err("could not allocate superblock\n");
bf5a934a
DW
5758 return 0;
5759 }
de44e46f
PB
5760 if (posix_memalign(&super->migr_rec_buf, MAX_SECTOR_SIZE,
5761 MIGR_REC_BUF_SECTORS*MAX_SECTOR_SIZE) != 0) {
1ade5cc1 5762 pr_err("could not allocate migr_rec buffer\n");
8e59f3d8 5763 free(super->buf);
8d67477f 5764 free_imsm(super);
8e59f3d8
AK
5765 return 0;
5766 }
e683ca88 5767 memset(super->buf, 0, mpb_size);
ef649044 5768 mpb = super->buf;
e683ca88
DW
5769 mpb->mpb_size = __cpu_to_le32(mpb_size);
5770 st->sb = super;
5771
5772 if (info == NULL) {
5773 /* zeroing superblock */
5774 return 0;
5775 }
bf5a934a 5776
4d1313e9
DW
5777 mpb->attributes = MPB_ATTRIB_CHECKSUM_VERIFY;
5778
5779 version = (char *) mpb->sig;
5780 strcpy(version, MPB_SIGNATURE);
5781 version += strlen(MPB_SIGNATURE);
5782 strcpy(version, MPB_VERSION_RAID0);
bf5a934a 5783
bf5a934a
DW
5784 return 1;
5785}
5786
f2cc4f7d
AO
5787static int drive_validate_sector_size(struct intel_super *super, struct dl *dl)
5788{
5789 unsigned int member_sector_size;
5790
4389ce73 5791 if (!is_fd_valid(dl->fd)) {
f2cc4f7d
AO
5792 pr_err("Invalid file descriptor for %s\n", dl->devname);
5793 return 0;
5794 }
5795
5796 if (!get_dev_sector_size(dl->fd, dl->devname, &member_sector_size))
5797 return 0;
5798 if (member_sector_size != super->sector_size)
5799 return 0;
5800 return 1;
5801}
5802
f20c3968 5803static int add_to_super_imsm_volume(struct supertype *st, mdu_disk_info_t *dk,
bf5a934a
DW
5804 int fd, char *devname)
5805{
5806 struct intel_super *super = st->sb;
d23fe947 5807 struct imsm_super *mpb = super->anchor;
3960e579 5808 struct imsm_disk *_disk;
bf5a934a
DW
5809 struct imsm_dev *dev;
5810 struct imsm_map *map;
3960e579 5811 struct dl *dl, *df;
4eb26970 5812 int slot;
9a7df595
MT
5813 int autolayout = 0;
5814
5815 if (!is_fd_valid(fd))
5816 autolayout = 1;
bf5a934a 5817
949c47a0 5818 dev = get_imsm_dev(super, super->current_vol);
238c0a71 5819 map = get_imsm_map(dev, MAP_0);
bf5a934a 5820
208933a7 5821 if (! (dk->state & (1<<MD_DISK_SYNC))) {
e7b84f9d 5822 pr_err("%s: Cannot add spare devices to IMSM volume\n",
208933a7
N
5823 devname);
5824 return 1;
5825 }
5826
9a7df595
MT
5827 for (dl = super->disks; dl ; dl = dl->next) {
5828 if (autolayout) {
efb30e7f
DW
5829 if (dl->raiddisk == dk->raid_disk)
5830 break;
9a7df595
MT
5831 } else if (dl->major == dk->major && dl->minor == dk->minor)
5832 break;
efb30e7f 5833 }
d23fe947 5834
208933a7 5835 if (!dl) {
9a7df595
MT
5836 if (!autolayout)
5837 pr_err("%s is not a member of the same container.\n",
5838 devname);
f20c3968 5839 return 1;
208933a7 5840 }
bf5a934a 5841
9a7df595
MT
5842 if (!autolayout && super->current_vol > 0) {
5843 int _slot = get_disk_slot_in_dev(super, 0, dl->index);
5844
5845 if (_slot != dk->raid_disk) {
5846 pr_err("Member %s is in %d slot for the first volume, but is in %d slot for a new volume.\n",
5847 dl->devname, _slot, dk->raid_disk);
5848 pr_err("Raid members are in different order than for the first volume, aborting.\n");
5849 return 1;
5850 }
5851 }
5852
59632db9
MZ
5853 if (mpb->num_disks == 0)
5854 if (!get_dev_sector_size(dl->fd, dl->devname,
5855 &super->sector_size))
5856 return 1;
5857
f2cc4f7d
AO
5858 if (!drive_validate_sector_size(super, dl)) {
5859 pr_err("Combining drives of different sector size in one volume is not allowed\n");
5860 return 1;
5861 }
5862
d23fe947
DW
5863 /* add a pristine spare to the metadata */
5864 if (dl->index < 0) {
5865 dl->index = super->anchor->num_disks;
5866 super->anchor->num_disks++;
5867 }
4eb26970
DW
5868 /* Check the device has not already been added */
5869 slot = get_imsm_disk_slot(map, dl->index);
5870 if (slot >= 0 &&
238c0a71 5871 (get_imsm_ord_tbl_ent(dev, slot, MAP_X) & IMSM_ORD_REBUILD) == 0) {
e7b84f9d 5872 pr_err("%s has been included in this array twice\n",
4eb26970
DW
5873 devname);
5874 return 1;
5875 }
656b6b5a 5876 set_imsm_ord_tbl_ent(map, dk->raid_disk, dl->index);
ee5aad5a 5877 dl->disk.status = CONFIGURED_DISK;
d23fe947 5878
3960e579
DW
5879 /* update size of 'missing' disks to be at least as large as the
5880 * largest acitve member (we only have dummy missing disks when
5881 * creating the first volume)
5882 */
5883 if (super->current_vol == 0) {
5884 for (df = super->missing; df; df = df->next) {
5551b113
CA
5885 if (total_blocks(&dl->disk) > total_blocks(&df->disk))
5886 set_total_blocks(&df->disk, total_blocks(&dl->disk));
3960e579
DW
5887 _disk = __get_imsm_disk(mpb, df->index);
5888 *_disk = df->disk;
5889 }
5890 }
5891
5892 /* refresh unset/failed slots to point to valid 'missing' entries */
5893 for (df = super->missing; df; df = df->next)
5894 for (slot = 0; slot < mpb->num_disks; slot++) {
238c0a71 5895 __u32 ord = get_imsm_ord_tbl_ent(dev, slot, MAP_X);
3960e579
DW
5896
5897 if ((ord & IMSM_ORD_REBUILD) == 0)
5898 continue;
5899 set_imsm_ord_tbl_ent(map, slot, df->index | IMSM_ORD_REBUILD);
1ace8403 5900 if (is_gen_migration(dev)) {
238c0a71
AK
5901 struct imsm_map *map2 = get_imsm_map(dev,
5902 MAP_1);
0a108d63 5903 int slot2 = get_imsm_disk_slot(map2, df->index);
089f9d79 5904 if (slot2 < map2->num_members && slot2 >= 0) {
1ace8403 5905 __u32 ord2 = get_imsm_ord_tbl_ent(dev,
238c0a71
AK
5906 slot2,
5907 MAP_1);
1ace8403
AK
5908 if ((unsigned)df->index ==
5909 ord_to_idx(ord2))
5910 set_imsm_ord_tbl_ent(map2,
0a108d63 5911 slot2,
1ace8403
AK
5912 df->index |
5913 IMSM_ORD_REBUILD);
5914 }
5915 }
3960e579
DW
5916 dprintf("set slot:%d to missing disk:%d\n", slot, df->index);
5917 break;
5918 }
5919
d23fe947
DW
5920 /* if we are creating the first raid device update the family number */
5921 if (super->current_vol == 0) {
5922 __u32 sum;
5923 struct imsm_dev *_dev = __get_imsm_dev(mpb, 0);
d23fe947 5924
3960e579 5925 _disk = __get_imsm_disk(mpb, dl->index);
756a15f3 5926 if (!_disk) {
e7b84f9d 5927 pr_err("BUG mpb setup error\n");
791b666a
AW
5928 return 1;
5929 }
d23fe947
DW
5930 *_dev = *dev;
5931 *_disk = dl->disk;
148acb7b
DW
5932 sum = random32();
5933 sum += __gen_imsm_checksum(mpb);
d23fe947 5934 mpb->family_num = __cpu_to_le32(sum);
148acb7b 5935 mpb->orig_family_num = mpb->family_num;
e48aed3c 5936 mpb->creation_time = __cpu_to_le64((__u64)time(NULL));
d23fe947 5937 }
ca0748fa 5938 super->current_disk = dl;
f20c3968 5939 return 0;
bf5a934a
DW
5940}
5941
a8619d23
AK
5942/* mark_spare()
5943 * Function marks disk as spare and restores disk serial
5944 * in case it was previously marked as failed by takeover operation
5945 * reruns:
5946 * -1 : critical error
5947 * 0 : disk is marked as spare but serial is not set
5948 * 1 : success
5949 */
5950int mark_spare(struct dl *disk)
5951{
5952 __u8 serial[MAX_RAID_SERIAL_LEN];
5953 int ret_val = -1;
5954
5955 if (!disk)
5956 return ret_val;
5957
5958 ret_val = 0;
6da53c0e 5959 if (!imsm_read_serial(disk->fd, NULL, serial, MAX_RAID_SERIAL_LEN)) {
a8619d23
AK
5960 /* Restore disk serial number, because takeover marks disk
5961 * as failed and adds to serial ':0' before it becomes
5962 * a spare disk.
5963 */
5964 serialcpy(disk->serial, serial);
5965 serialcpy(disk->disk.serial, serial);
5966 ret_val = 1;
5967 }
5968 disk->disk.status = SPARE_DISK;
5969 disk->index = -1;
5970
5971 return ret_val;
5972}
88654014 5973
12724c01
TM
5974
5975static int write_super_imsm_spare(struct intel_super *super, struct dl *d);
5976
f20c3968 5977static int add_to_super_imsm(struct supertype *st, mdu_disk_info_t *dk,
72ca9bcf
N
5978 int fd, char *devname,
5979 unsigned long long data_offset)
cdddbdbc 5980{
c2c087e6 5981 struct intel_super *super = st->sb;
c2c087e6
DW
5982 struct dl *dd;
5983 unsigned long long size;
fa7bb6f8 5984 unsigned int member_sector_size;
f2f27e63 5985 __u32 id;
c2c087e6
DW
5986 int rv;
5987 struct stat stb;
5988
88654014
LM
5989 /* If we are on an RAID enabled platform check that the disk is
5990 * attached to the raid controller.
5991 * We do not need to test disks attachment for container based additions,
5992 * they shall be already tested when container was created/assembled.
88c32bb1 5993 */
d424212e 5994 rv = find_intel_hba_capability(fd, super, devname);
f2f5c343 5995 /* no orom/efi or non-intel hba of the disk */
f0f5a016
LM
5996 if (rv != 0) {
5997 dprintf("capability: %p fd: %d ret: %d\n",
5998 super->orom, fd, rv);
5999 return 1;
88c32bb1
DW
6000 }
6001
f20c3968
DW
6002 if (super->current_vol >= 0)
6003 return add_to_super_imsm_volume(st, dk, fd, devname);
bf5a934a 6004
c2c087e6 6005 fstat(fd, &stb);
503975b9 6006 dd = xcalloc(sizeof(*dd), 1);
c2c087e6
DW
6007 dd->major = major(stb.st_rdev);
6008 dd->minor = minor(stb.st_rdev);
503975b9 6009 dd->devname = devname ? xstrdup(devname) : NULL;
c2c087e6 6010 dd->fd = fd;
689c9bf3 6011 dd->e = NULL;
1a64be56 6012 dd->action = DISK_ADD;
6da53c0e 6013 rv = imsm_read_serial(fd, devname, dd->serial, MAX_RAID_SERIAL_LEN);
32ba9157 6014 if (rv) {
e7b84f9d 6015 pr_err("failed to retrieve scsi serial, aborting\n");
3a85bf0e 6016 __free_imsm_disk(dd, 0);
0030e8d6 6017 abort();
c2c087e6 6018 }
7c798f87 6019
20bee0f8
PB
6020 if (super->hba && ((super->hba->type == SYS_DEV_NVME) ||
6021 (super->hba->type == SYS_DEV_VMD))) {
6022 int i;
7c798f87
MT
6023 char cntrl_path[PATH_MAX];
6024 char *cntrl_name;
6025 char pci_dev_path[PATH_MAX];
20bee0f8 6026
7c798f87
MT
6027 if (!diskfd_to_devpath(fd, 2, pci_dev_path) ||
6028 !diskfd_to_devpath(fd, 1, cntrl_path)) {
8662f92d 6029 pr_err("failed to get dev paths, aborting\n");
3a85bf0e 6030 __free_imsm_disk(dd, 0);
a8f3cfd5
MT
6031 return 1;
6032 }
6033
7c798f87
MT
6034 cntrl_name = basename(cntrl_path);
6035 if (is_multipath_nvme(fd))
6036 pr_err("%s controller supports Multi-Path I/O, Intel (R) VROC does not support multipathing\n",
6037 cntrl_name);
6038
6039 if (devpath_to_vendor(pci_dev_path) == 0x8086) {
20bee0f8
PB
6040 /*
6041 * If Intel's NVMe drive has serial ended with
6042 * "-A","-B","-1" or "-2" it means that this is "x8"
6043 * device (double drive on single PCIe card).
6044 * User should be warned about potential data loss.
6045 */
6046 for (i = MAX_RAID_SERIAL_LEN-1; i > 0; i--) {
6047 /* Skip empty character at the end */
6048 if (dd->serial[i] == 0)
6049 continue;
6050
6051 if (((dd->serial[i] == 'A') ||
6052 (dd->serial[i] == 'B') ||
6053 (dd->serial[i] == '1') ||
6054 (dd->serial[i] == '2')) &&
6055 (dd->serial[i-1] == '-'))
6056 pr_err("\tThe action you are about to take may put your data at risk.\n"
6057 "\tPlease note that x8 devices may consist of two separate x4 devices "
6058 "located on a single PCIe port.\n"
6059 "\tRAID 0 is the only supported configuration for this type of x8 device.\n");
6060 break;
6061 }
32716c51
PB
6062 } else if (super->hba->type == SYS_DEV_VMD && super->orom &&
6063 !imsm_orom_has_tpv_support(super->orom)) {
6064 pr_err("\tPlatform configuration does not support non-Intel NVMe drives.\n"
8b751247 6065 "\tPlease refer to Intel(R) RSTe/VROC user guide.\n");
3a85bf0e 6066 __free_imsm_disk(dd, 0);
32716c51 6067 return 1;
20bee0f8
PB
6068 }
6069 }
c2c087e6 6070
c2c087e6 6071 get_dev_size(fd, NULL, &size);
3a85bf0e
MG
6072 if (!get_dev_sector_size(fd, NULL, &member_sector_size)) {
6073 __free_imsm_disk(dd, 0);
aec01630 6074 return 1;
3a85bf0e 6075 }
fa7bb6f8
PB
6076
6077 if (super->sector_size == 0) {
6078 /* this a first device, so sector_size is not set yet */
6079 super->sector_size = member_sector_size;
fa7bb6f8
PB
6080 }
6081
71e5411e 6082 /* clear migr_rec when adding disk to container */
85337573
AO
6083 memset(super->migr_rec_buf, 0, MIGR_REC_BUF_SECTORS*MAX_SECTOR_SIZE);
6084 if (lseek64(fd, size - MIGR_REC_SECTOR_POSITION*member_sector_size,
de44e46f 6085 SEEK_SET) >= 0) {
466070ad 6086 if ((unsigned int)write(fd, super->migr_rec_buf,
85337573
AO
6087 MIGR_REC_BUF_SECTORS*member_sector_size) !=
6088 MIGR_REC_BUF_SECTORS*member_sector_size)
71e5411e
PB
6089 perror("Write migr_rec failed");
6090 }
6091
c2c087e6 6092 size /= 512;
1f24f035 6093 serialcpy(dd->disk.serial, dd->serial);
5551b113
CA
6094 set_total_blocks(&dd->disk, size);
6095 if (__le32_to_cpu(dd->disk.total_blocks_hi) > 0) {
6096 struct imsm_super *mpb = super->anchor;
6097 mpb->attributes |= MPB_ATTRIB_2TB_DISK;
6098 }
a8619d23 6099 mark_spare(dd);
c2c087e6 6100 if (sysfs_disk_to_scsi_id(fd, &id) == 0)
b9f594fe 6101 dd->disk.scsi_id = __cpu_to_le32(id);
c2c087e6 6102 else
b9f594fe 6103 dd->disk.scsi_id = __cpu_to_le32(0);
43dad3d6
DW
6104
6105 if (st->update_tail) {
1a64be56
LM
6106 dd->next = super->disk_mgmt_list;
6107 super->disk_mgmt_list = dd;
43dad3d6 6108 } else {
12724c01
TM
6109 /* this is called outside of mdmon
6110 * write initial spare metadata
6111 * mdmon will overwrite it.
6112 */
43dad3d6
DW
6113 dd->next = super->disks;
6114 super->disks = dd;
12724c01 6115 write_super_imsm_spare(super, dd);
43dad3d6 6116 }
f20c3968
DW
6117
6118 return 0;
cdddbdbc
DW
6119}
6120
1a64be56
LM
6121static int remove_from_super_imsm(struct supertype *st, mdu_disk_info_t *dk)
6122{
6123 struct intel_super *super = st->sb;
6124 struct dl *dd;
6125
6126 /* remove from super works only in mdmon - for communication
6127 * manager - monitor. Check if communication memory buffer
6128 * is prepared.
6129 */
6130 if (!st->update_tail) {
1ade5cc1 6131 pr_err("shall be used in mdmon context only\n");
1a64be56
LM
6132 return 1;
6133 }
503975b9 6134 dd = xcalloc(1, sizeof(*dd));
1a64be56
LM
6135 dd->major = dk->major;
6136 dd->minor = dk->minor;
1a64be56 6137 dd->fd = -1;
a8619d23 6138 mark_spare(dd);
1a64be56
LM
6139 dd->action = DISK_REMOVE;
6140
6141 dd->next = super->disk_mgmt_list;
6142 super->disk_mgmt_list = dd;
6143
1a64be56
LM
6144 return 0;
6145}
6146
f796af5d
DW
6147static int store_imsm_mpb(int fd, struct imsm_super *mpb);
6148
6149static union {
f36a9ecd 6150 char buf[MAX_SECTOR_SIZE];
f796af5d 6151 struct imsm_super anchor;
f36a9ecd 6152} spare_record __attribute__ ((aligned(MAX_SECTOR_SIZE)));
c2c087e6 6153
12724c01
TM
6154
6155static int write_super_imsm_spare(struct intel_super *super, struct dl *d)
d23fe947 6156{
d23fe947 6157 struct imsm_super *mpb = super->anchor;
f796af5d 6158 struct imsm_super *spare = &spare_record.anchor;
d23fe947 6159 __u32 sum;
12724c01
TM
6160
6161 if (d->index != -1)
6162 return 1;
d23fe947 6163
68641cdb
JS
6164 spare->mpb_size = __cpu_to_le32(sizeof(struct imsm_super));
6165 spare->generation_num = __cpu_to_le32(1UL);
f796af5d 6166 spare->attributes = MPB_ATTRIB_CHECKSUM_VERIFY;
68641cdb
JS
6167 spare->num_disks = 1;
6168 spare->num_raid_devs = 0;
6169 spare->cache_size = mpb->cache_size;
6170 spare->pwr_cycle_count = __cpu_to_le32(1);
f796af5d
DW
6171
6172 snprintf((char *) spare->sig, MAX_SIGNATURE_LENGTH,
6173 MPB_SIGNATURE MPB_VERSION_RAID0);
d23fe947 6174
12724c01
TM
6175 spare->disk[0] = d->disk;
6176 if (__le32_to_cpu(d->disk.total_blocks_hi) > 0)
6177 spare->attributes |= MPB_ATTRIB_2TB_DISK;
6178
6179 if (super->sector_size == 4096)
6180 convert_to_4k_imsm_disk(&spare->disk[0]);
d23fe947 6181
12724c01
TM
6182 sum = __gen_imsm_checksum(spare);
6183 spare->family_num = __cpu_to_le32(sum);
6184 spare->orig_family_num = 0;
6185 sum = __gen_imsm_checksum(spare);
6186 spare->check_sum = __cpu_to_le32(sum);
027c374f 6187
12724c01
TM
6188 if (store_imsm_mpb(d->fd, spare)) {
6189 pr_err("failed for device %d:%d %s\n",
6190 d->major, d->minor, strerror(errno));
6191 return 1;
6192 }
6193
6194 return 0;
6195}
6196/* spare records have their own family number and do not have any defined raid
6197 * devices
6198 */
6199static int write_super_imsm_spares(struct intel_super *super, int doclose)
6200{
6201 struct dl *d;
f36a9ecd 6202
12724c01
TM
6203 for (d = super->disks; d; d = d->next) {
6204 if (d->index != -1)
6205 continue;
d23fe947 6206
12724c01 6207 if (write_super_imsm_spare(super, d))
e74255d9 6208 return 1;
12724c01 6209
4389ce73
MT
6210 if (doclose)
6211 close_fd(&d->fd);
d23fe947
DW
6212 }
6213
e74255d9 6214 return 0;
d23fe947
DW
6215}
6216
36988a3d 6217static int write_super_imsm(struct supertype *st, int doclose)
cdddbdbc 6218{
36988a3d 6219 struct intel_super *super = st->sb;
f36a9ecd 6220 unsigned int sector_size = super->sector_size;
949c47a0 6221 struct imsm_super *mpb = super->anchor;
c2c087e6
DW
6222 struct dl *d;
6223 __u32 generation;
6224 __u32 sum;
d23fe947 6225 int spares = 0;
949c47a0 6226 int i;
a48ac0a8 6227 __u32 mpb_size = sizeof(struct imsm_super) - sizeof(struct imsm_disk);
36988a3d 6228 int num_disks = 0;
146c6260 6229 int clear_migration_record = 1;
bbab0940 6230 __u32 bbm_log_size;
cdddbdbc 6231
c2c087e6
DW
6232 /* 'generation' is incremented everytime the metadata is written */
6233 generation = __le32_to_cpu(mpb->generation_num);
6234 generation++;
6235 mpb->generation_num = __cpu_to_le32(generation);
6236
148acb7b
DW
6237 /* fix up cases where previous mdadm releases failed to set
6238 * orig_family_num
6239 */
6240 if (mpb->orig_family_num == 0)
6241 mpb->orig_family_num = mpb->family_num;
6242
d23fe947 6243 for (d = super->disks; d; d = d->next) {
8796fdc4 6244 if (d->index == -1)
d23fe947 6245 spares++;
36988a3d 6246 else {
d23fe947 6247 mpb->disk[d->index] = d->disk;
36988a3d
AK
6248 num_disks++;
6249 }
d23fe947 6250 }
36988a3d 6251 for (d = super->missing; d; d = d->next) {
47ee5a45 6252 mpb->disk[d->index] = d->disk;
36988a3d
AK
6253 num_disks++;
6254 }
6255 mpb->num_disks = num_disks;
6256 mpb_size += sizeof(struct imsm_disk) * mpb->num_disks;
b9f594fe 6257
949c47a0
DW
6258 for (i = 0; i < mpb->num_raid_devs; i++) {
6259 struct imsm_dev *dev = __get_imsm_dev(mpb, i);
36988a3d 6260 struct imsm_dev *dev2 = get_imsm_dev(super, i);
756a15f3
MG
6261
6262 imsm_copy_dev(dev, dev2);
6263 mpb_size += sizeof_imsm_dev(dev, 0);
6264
146c6260
AK
6265 if (is_gen_migration(dev2))
6266 clear_migration_record = 0;
949c47a0 6267 }
bbab0940
TM
6268
6269 bbm_log_size = get_imsm_bbm_log_size(super->bbm_log);
6270
6271 if (bbm_log_size) {
6272 memcpy((void *)mpb + mpb_size, super->bbm_log, bbm_log_size);
6273 mpb->attributes |= MPB_ATTRIB_BBM;
6274 } else
6275 mpb->attributes &= ~MPB_ATTRIB_BBM;
6276
6277 super->anchor->bbm_log_size = __cpu_to_le32(bbm_log_size);
6278 mpb_size += bbm_log_size;
a48ac0a8 6279 mpb->mpb_size = __cpu_to_le32(mpb_size);
949c47a0 6280
bbab0940
TM
6281#ifdef DEBUG
6282 assert(super->len == 0 || mpb_size <= super->len);
6283#endif
6284
c2c087e6 6285 /* recalculate checksum */
949c47a0 6286 sum = __gen_imsm_checksum(mpb);
c2c087e6
DW
6287 mpb->check_sum = __cpu_to_le32(sum);
6288
51d83f5d
AK
6289 if (super->clean_migration_record_by_mdmon) {
6290 clear_migration_record = 1;
6291 super->clean_migration_record_by_mdmon = 0;
6292 }
146c6260 6293 if (clear_migration_record)
de44e46f 6294 memset(super->migr_rec_buf, 0,
85337573 6295 MIGR_REC_BUF_SECTORS*MAX_SECTOR_SIZE);
146c6260 6296
f36a9ecd
PB
6297 if (sector_size == 4096)
6298 convert_to_4k(super);
6299
d23fe947 6300 /* write the mpb for disks that compose raid devices */
c2c087e6 6301 for (d = super->disks; d ; d = d->next) {
86c54047 6302 if (d->index < 0 || is_failed(&d->disk))
d23fe947 6303 continue;
30602f53 6304
146c6260
AK
6305 if (clear_migration_record) {
6306 unsigned long long dsize;
6307
6308 get_dev_size(d->fd, NULL, &dsize);
de44e46f
PB
6309 if (lseek64(d->fd, dsize - sector_size,
6310 SEEK_SET) >= 0) {
466070ad
PB
6311 if ((unsigned int)write(d->fd,
6312 super->migr_rec_buf,
de44e46f
PB
6313 MIGR_REC_BUF_SECTORS*sector_size) !=
6314 MIGR_REC_BUF_SECTORS*sector_size)
9e2d750d 6315 perror("Write migr_rec failed");
146c6260
AK
6316 }
6317 }
51d83f5d
AK
6318
6319 if (store_imsm_mpb(d->fd, mpb))
6320 fprintf(stderr,
1ade5cc1
N
6321 "failed for device %d:%d (fd: %d)%s\n",
6322 d->major, d->minor,
51d83f5d
AK
6323 d->fd, strerror(errno));
6324
4389ce73
MT
6325 if (doclose)
6326 close_fd(&d->fd);
c2c087e6
DW
6327 }
6328
d23fe947
DW
6329 if (spares)
6330 return write_super_imsm_spares(super, doclose);
6331
e74255d9 6332 return 0;
c2c087e6
DW
6333}
6334
9b1fb677 6335static int create_array(struct supertype *st, int dev_idx)
43dad3d6
DW
6336{
6337 size_t len;
6338 struct imsm_update_create_array *u;
6339 struct intel_super *super = st->sb;
9b1fb677 6340 struct imsm_dev *dev = get_imsm_dev(super, dev_idx);
238c0a71 6341 struct imsm_map *map = get_imsm_map(dev, MAP_0);
54c2c1ea
DW
6342 struct disk_info *inf;
6343 struct imsm_disk *disk;
6344 int i;
43dad3d6 6345
54c2c1ea
DW
6346 len = sizeof(*u) - sizeof(*dev) + sizeof_imsm_dev(dev, 0) +
6347 sizeof(*inf) * map->num_members;
503975b9 6348 u = xmalloc(len);
43dad3d6 6349 u->type = update_create_array;
9b1fb677 6350 u->dev_idx = dev_idx;
43dad3d6 6351 imsm_copy_dev(&u->dev, dev);
54c2c1ea
DW
6352 inf = get_disk_info(u);
6353 for (i = 0; i < map->num_members; i++) {
238c0a71 6354 int idx = get_imsm_disk_idx(dev, i, MAP_X);
9b1fb677 6355
54c2c1ea 6356 disk = get_imsm_disk(super, idx);
1ca5c8e0
N
6357 if (!disk)
6358 disk = get_imsm_missing(super, idx);
54c2c1ea
DW
6359 serialcpy(inf[i].serial, disk->serial);
6360 }
43dad3d6
DW
6361 append_metadata_update(st, u, len);
6362
6363 return 0;
6364}
6365
1a64be56 6366static int mgmt_disk(struct supertype *st)
43dad3d6
DW
6367{
6368 struct intel_super *super = st->sb;
6369 size_t len;
1a64be56 6370 struct imsm_update_add_remove_disk *u;
43dad3d6 6371
1a64be56 6372 if (!super->disk_mgmt_list)
43dad3d6
DW
6373 return 0;
6374
6375 len = sizeof(*u);
503975b9 6376 u = xmalloc(len);
1a64be56 6377 u->type = update_add_remove_disk;
43dad3d6
DW
6378 append_metadata_update(st, u, len);
6379
6380 return 0;
6381}
2432ce9b
AP
6382
6383__u32 crc32c_le(__u32 crc, unsigned char const *p, size_t len);
6384
e397cefe
AP
6385static int write_ppl_header(unsigned long long ppl_sector, int fd, void *buf)
6386{
6387 struct ppl_header *ppl_hdr = buf;
6388 int ret;
6389
6390 ppl_hdr->checksum = __cpu_to_le32(~crc32c_le(~0, buf, PPL_HEADER_SIZE));
6391
6392 if (lseek64(fd, ppl_sector * 512, SEEK_SET) < 0) {
6393 ret = -errno;
6394 perror("Failed to seek to PPL header location");
6395 return ret;
6396 }
6397
6398 if (write(fd, buf, PPL_HEADER_SIZE) != PPL_HEADER_SIZE) {
6399 ret = -errno;
6400 perror("Write PPL header failed");
6401 return ret;
6402 }
6403
6404 fsync(fd);
6405
6406 return 0;
6407}
6408
2432ce9b
AP
6409static int write_init_ppl_imsm(struct supertype *st, struct mdinfo *info, int fd)
6410{
6411 struct intel_super *super = st->sb;
6412 void *buf;
6413 struct ppl_header *ppl_hdr;
6414 int ret;
6415
b2514242
PB
6416 /* first clear entire ppl space */
6417 ret = zero_disk_range(fd, info->ppl_sector, info->ppl_size);
6418 if (ret)
6419 return ret;
6420
6421 ret = posix_memalign(&buf, MAX_SECTOR_SIZE, PPL_HEADER_SIZE);
2432ce9b
AP
6422 if (ret) {
6423 pr_err("Failed to allocate PPL header buffer\n");
e397cefe 6424 return -ret;
2432ce9b
AP
6425 }
6426
6427 memset(buf, 0, PPL_HEADER_SIZE);
6428 ppl_hdr = buf;
6429 memset(ppl_hdr->reserved, 0xff, PPL_HDR_RESERVED);
6430 ppl_hdr->signature = __cpu_to_le32(super->anchor->orig_family_num);
b23d0750
AP
6431
6432 if (info->mismatch_cnt) {
6433 /*
6434 * We are overwriting an invalid ppl. Make one entry with wrong
6435 * checksum to prevent the kernel from skipping resync.
6436 */
6437 ppl_hdr->entries_count = __cpu_to_le32(1);
6438 ppl_hdr->entries[0].checksum = ~0;
6439 }
6440
e397cefe 6441 ret = write_ppl_header(info->ppl_sector, fd, buf);
2432ce9b
AP
6442
6443 free(buf);
6444 return ret;
6445}
6446
e397cefe
AP
6447static int is_rebuilding(struct imsm_dev *dev);
6448
2432ce9b
AP
6449static int validate_ppl_imsm(struct supertype *st, struct mdinfo *info,
6450 struct mdinfo *disk)
6451{
6452 struct intel_super *super = st->sb;
6453 struct dl *d;
e397cefe 6454 void *buf_orig, *buf, *buf_prev = NULL;
2432ce9b 6455 int ret = 0;
e397cefe 6456 struct ppl_header *ppl_hdr = NULL;
2432ce9b
AP
6457 __u32 crc;
6458 struct imsm_dev *dev;
2432ce9b 6459 __u32 idx;
44b6b876
PB
6460 unsigned int i;
6461 unsigned long long ppl_offset = 0;
6462 unsigned long long prev_gen_num = 0;
2432ce9b
AP
6463
6464 if (disk->disk.raid_disk < 0)
6465 return 0;
6466
2432ce9b 6467 dev = get_imsm_dev(super, info->container_member);
2fc0fc63 6468 idx = get_imsm_disk_idx(dev, disk->disk.raid_disk, MAP_0);
2432ce9b
AP
6469 d = get_imsm_dl_disk(super, idx);
6470
6471 if (!d || d->index < 0 || is_failed(&d->disk))
e397cefe
AP
6472 return 0;
6473
6474 if (posix_memalign(&buf_orig, MAX_SECTOR_SIZE, PPL_HEADER_SIZE * 2)) {
6475 pr_err("Failed to allocate PPL header buffer\n");
6476 return -1;
6477 }
6478 buf = buf_orig;
2432ce9b 6479
44b6b876
PB
6480 ret = 1;
6481 while (ppl_offset < MULTIPLE_PPL_AREA_SIZE_IMSM) {
e397cefe
AP
6482 void *tmp;
6483
44b6b876 6484 dprintf("Checking potential PPL at offset: %llu\n", ppl_offset);
2432ce9b 6485
44b6b876
PB
6486 if (lseek64(d->fd, info->ppl_sector * 512 + ppl_offset,
6487 SEEK_SET) < 0) {
6488 perror("Failed to seek to PPL header location");
6489 ret = -1;
e397cefe 6490 break;
44b6b876 6491 }
2432ce9b 6492
44b6b876
PB
6493 if (read(d->fd, buf, PPL_HEADER_SIZE) != PPL_HEADER_SIZE) {
6494 perror("Read PPL header failed");
6495 ret = -1;
e397cefe 6496 break;
44b6b876 6497 }
2432ce9b 6498
44b6b876 6499 ppl_hdr = buf;
2432ce9b 6500
44b6b876
PB
6501 crc = __le32_to_cpu(ppl_hdr->checksum);
6502 ppl_hdr->checksum = 0;
6503
6504 if (crc != ~crc32c_le(~0, buf, PPL_HEADER_SIZE)) {
6505 dprintf("Wrong PPL header checksum on %s\n",
6506 d->devname);
e397cefe 6507 break;
44b6b876
PB
6508 }
6509
6510 if (prev_gen_num > __le64_to_cpu(ppl_hdr->generation)) {
6511 /* previous was newest, it was already checked */
e397cefe 6512 break;
44b6b876
PB
6513 }
6514
6515 if ((__le32_to_cpu(ppl_hdr->signature) !=
6516 super->anchor->orig_family_num)) {
6517 dprintf("Wrong PPL header signature on %s\n",
6518 d->devname);
6519 ret = 1;
e397cefe 6520 break;
44b6b876
PB
6521 }
6522
6523 ret = 0;
6524 prev_gen_num = __le64_to_cpu(ppl_hdr->generation);
2432ce9b 6525
44b6b876
PB
6526 ppl_offset += PPL_HEADER_SIZE;
6527 for (i = 0; i < __le32_to_cpu(ppl_hdr->entries_count); i++)
6528 ppl_offset +=
6529 __le32_to_cpu(ppl_hdr->entries[i].pp_size);
e397cefe
AP
6530
6531 if (!buf_prev)
6532 buf_prev = buf + PPL_HEADER_SIZE;
6533 tmp = buf_prev;
6534 buf_prev = buf;
6535 buf = tmp;
2432ce9b
AP
6536 }
6537
e397cefe
AP
6538 if (buf_prev) {
6539 buf = buf_prev;
6540 ppl_hdr = buf_prev;
6541 }
2432ce9b 6542
54148aba
PB
6543 /*
6544 * Update metadata to use mutliple PPLs area (1MB).
6545 * This is done once for all RAID members
6546 */
6547 if (info->consistency_policy == CONSISTENCY_POLICY_PPL &&
6548 info->ppl_size != (MULTIPLE_PPL_AREA_SIZE_IMSM >> 9)) {
6549 char subarray[20];
6550 struct mdinfo *member_dev;
6551
6552 sprintf(subarray, "%d", info->container_member);
6553
6554 if (mdmon_running(st->container_devnm))
6555 st->update_tail = &st->updates;
6556
03312b52 6557 if (st->ss->update_subarray(st, subarray, UOPT_PPL, NULL)) {
54148aba
PB
6558 pr_err("Failed to update subarray %s\n",
6559 subarray);
6560 } else {
6561 if (st->update_tail)
6562 flush_metadata_updates(st);
6563 else
6564 st->ss->sync_metadata(st);
6565 info->ppl_size = (MULTIPLE_PPL_AREA_SIZE_IMSM >> 9);
6566 for (member_dev = info->devs; member_dev;
6567 member_dev = member_dev->next)
6568 member_dev->ppl_size =
6569 (MULTIPLE_PPL_AREA_SIZE_IMSM >> 9);
6570 }
6571 }
6572
b23d0750 6573 if (ret == 1) {
2fc0fc63
AP
6574 struct imsm_map *map = get_imsm_map(dev, MAP_X);
6575
50b9c10d
PB
6576 if (map->map_state == IMSM_T_STATE_UNINITIALIZED ||
6577 (map->map_state == IMSM_T_STATE_NORMAL &&
2ec9d182 6578 !(dev->vol.dirty & RAIDVOL_DIRTY)) ||
e397cefe 6579 (is_rebuilding(dev) &&
4036e7ee 6580 vol_curr_migr_unit(dev) == 0 &&
2ec9d182 6581 get_imsm_disk_idx(dev, disk->disk.raid_disk, MAP_1) != idx))
b23d0750
AP
6582 ret = st->ss->write_init_ppl(st, info, d->fd);
6583 else
6584 info->mismatch_cnt++;
e397cefe
AP
6585 } else if (ret == 0 &&
6586 ppl_hdr->entries_count == 0 &&
6587 is_rebuilding(dev) &&
6588 info->resync_start == 0) {
6589 /*
6590 * The header has no entries - add a single empty entry and
6591 * rewrite the header to prevent the kernel from going into
6592 * resync after an interrupted rebuild.
6593 */
6594 ppl_hdr->entries_count = __cpu_to_le32(1);
6595 ret = write_ppl_header(info->ppl_sector, d->fd, buf);
b23d0750 6596 }
2432ce9b 6597
e397cefe
AP
6598 free(buf_orig);
6599
2432ce9b
AP
6600 return ret;
6601}
6602
2432ce9b
AP
6603static int write_init_ppl_imsm_all(struct supertype *st, struct mdinfo *info)
6604{
6605 struct intel_super *super = st->sb;
6606 struct dl *d;
6607 int ret = 0;
6608
6609 if (info->consistency_policy != CONSISTENCY_POLICY_PPL ||
6610 info->array.level != 5)
6611 return 0;
6612
6613 for (d = super->disks; d ; d = d->next) {
6614 if (d->index < 0 || is_failed(&d->disk))
6615 continue;
6616
6617 ret = st->ss->write_init_ppl(st, info, d->fd);
6618 if (ret)
6619 break;
6620 }
6621
6622 return ret;
6623}
43dad3d6 6624
fbc42556
JR
6625/*******************************************************************************
6626 * Function: write_init_bitmap_imsm_vol
6627 * Description: Write a bitmap header and prepares the area for the bitmap.
6628 * Parameters:
6629 * st : supertype information
6630 * vol_idx : the volume index to use
6631 *
6632 * Returns:
6633 * 0 : success
6634 * -1 : fail
6635 ******************************************************************************/
6636static int write_init_bitmap_imsm_vol(struct supertype *st, int vol_idx)
6637{
6638 struct intel_super *super = st->sb;
6639 int prev_current_vol = super->current_vol;
6640 struct dl *d;
6641 int ret = 0;
6642
6643 super->current_vol = vol_idx;
6644 for (d = super->disks; d; d = d->next) {
6645 if (d->index < 0 || is_failed(&d->disk))
6646 continue;
6647 ret = st->ss->write_bitmap(st, d->fd, NoUpdate);
6648 if (ret)
6649 break;
6650 }
6651 super->current_vol = prev_current_vol;
6652 return ret;
6653}
6654
6655/*******************************************************************************
6656 * Function: write_init_bitmap_imsm_all
6657 * Description: Write a bitmap header and prepares the area for the bitmap.
6658 * Operation is executed for volumes with CONSISTENCY_POLICY_BITMAP.
6659 * Parameters:
6660 * st : supertype information
6661 * info : info about the volume where the bitmap should be written
6662 * vol_idx : the volume index to use
6663 *
6664 * Returns:
6665 * 0 : success
6666 * -1 : fail
6667 ******************************************************************************/
6668static int write_init_bitmap_imsm_all(struct supertype *st, struct mdinfo *info,
6669 int vol_idx)
6670{
6671 int ret = 0;
6672
6673 if (info && (info->consistency_policy == CONSISTENCY_POLICY_BITMAP))
6674 ret = write_init_bitmap_imsm_vol(st, vol_idx);
6675
6676 return ret;
6677}
6678
c2c087e6
DW
6679static int write_init_super_imsm(struct supertype *st)
6680{
9b1fb677
DW
6681 struct intel_super *super = st->sb;
6682 int current_vol = super->current_vol;
2432ce9b
AP
6683 int rv = 0;
6684 struct mdinfo info;
6685
6686 getinfo_super_imsm(st, &info, NULL);
9b1fb677
DW
6687
6688 /* we are done with current_vol reset it to point st at the container */
6689 super->current_vol = -1;
6690
8273f55e 6691 if (st->update_tail) {
43dad3d6
DW
6692 /* queue the recently created array / added disk
6693 * as a metadata update */
8273f55e 6694
43dad3d6 6695 /* determine if we are creating a volume or adding a disk */
9b1fb677 6696 if (current_vol < 0) {
1a64be56
LM
6697 /* in the mgmt (add/remove) disk case we are running
6698 * in mdmon context, so don't close fd's
43dad3d6 6699 */
2432ce9b
AP
6700 rv = mgmt_disk(st);
6701 } else {
fbc42556 6702 /* adding the second volume to the array */
2432ce9b 6703 rv = write_init_ppl_imsm_all(st, &info);
fbc42556
JR
6704 if (!rv)
6705 rv = write_init_bitmap_imsm_all(st, &info, current_vol);
2432ce9b
AP
6706 if (!rv)
6707 rv = create_array(st, current_vol);
6708 }
d682f344
N
6709 } else {
6710 struct dl *d;
6711 for (d = super->disks; d; d = d->next)
ba728be7 6712 Kill(d->devname, NULL, 0, -1, 1);
fbc42556 6713 if (current_vol >= 0) {
2432ce9b 6714 rv = write_init_ppl_imsm_all(st, &info);
fbc42556
JR
6715 if (!rv)
6716 rv = write_init_bitmap_imsm_all(st, &info, current_vol);
6717 }
6718
2432ce9b
AP
6719 if (!rv)
6720 rv = write_super_imsm(st, 1);
d682f344 6721 }
2432ce9b
AP
6722
6723 return rv;
cdddbdbc
DW
6724}
6725
e683ca88 6726static int store_super_imsm(struct supertype *st, int fd)
cdddbdbc 6727{
e683ca88
DW
6728 struct intel_super *super = st->sb;
6729 struct imsm_super *mpb = super ? super->anchor : NULL;
551c80c1 6730
e683ca88 6731 if (!mpb)
ad97895e
DW
6732 return 1;
6733
f36a9ecd
PB
6734 if (super->sector_size == 4096)
6735 convert_to_4k(super);
e683ca88 6736 return store_imsm_mpb(fd, mpb);
cdddbdbc
DW
6737}
6738
cdddbdbc 6739static int validate_geometry_imsm_container(struct supertype *st, int level,
1f5d54a0 6740 int raiddisks,
af4348dd
N
6741 unsigned long long data_offset,
6742 char *dev,
2c514b71
NB
6743 unsigned long long *freesize,
6744 int verbose)
cdddbdbc 6745{
c2c087e6
DW
6746 int fd;
6747 unsigned long long ldsize;
8662f92d 6748 struct intel_super *super = NULL;
f2f5c343 6749 int rv = 0;
cdddbdbc 6750
6f2af6a4 6751 if (!is_container(level))
c2c087e6
DW
6752 return 0;
6753 if (!dev)
6754 return 1;
6755
dca80fcd 6756 fd = dev_open(dev, O_RDONLY|O_EXCL);
4389ce73
MT
6757 if (!is_fd_valid(fd)) {
6758 pr_vrb("imsm: Cannot open %s: %s\n", dev, strerror(errno));
c2c087e6
DW
6759 return 0;
6760 }
8662f92d
MT
6761 if (!get_dev_size(fd, dev, &ldsize))
6762 goto exit;
f2f5c343
LM
6763
6764 /* capabilities retrieve could be possible
6765 * note that there is no fd for the disks in array.
6766 */
6767 super = alloc_super();
8662f92d
MT
6768 if (!super)
6769 goto exit;
6770
6771 if (!get_dev_sector_size(fd, NULL, &super->sector_size))
6772 goto exit;
fa7bb6f8 6773
ba728be7 6774 rv = find_intel_hba_capability(fd, super, verbose > 0 ? dev : NULL);
f2f5c343
LM
6775 if (rv != 0) {
6776#if DEBUG
6777 char str[256];
6778 fd2devname(fd, str);
1ade5cc1 6779 dprintf("fd: %d %s orom: %p rv: %d raiddisk: %d\n",
f2f5c343
LM
6780 fd, str, super->orom, rv, raiddisks);
6781#endif
6782 /* no orom/efi or non-intel hba of the disk */
8662f92d
MT
6783 rv = 0;
6784 goto exit;
f2f5c343 6785 }
9126b9a8
CA
6786 if (super->orom) {
6787 if (raiddisks > super->orom->tds) {
6788 if (verbose)
7a862a02 6789 pr_err("%d exceeds maximum number of platform supported disks: %d\n",
9126b9a8 6790 raiddisks, super->orom->tds);
8662f92d 6791 goto exit;
9126b9a8
CA
6792 }
6793 if ((super->orom->attr & IMSM_OROM_ATTR_2TB_DISK) == 0 &&
6794 (ldsize >> 9) >> 32 > 0) {
6795 if (verbose)
e7b84f9d 6796 pr_err("%s exceeds maximum platform supported size\n", dev);
8662f92d
MT
6797 goto exit;
6798 }
6799
6800 if (super->hba->type == SYS_DEV_VMD ||
6801 super->hba->type == SYS_DEV_NVME) {
6802 if (!imsm_is_nvme_namespace_supported(fd, 1)) {
6803 if (verbose)
6804 pr_err("NVMe namespace %s is not supported by IMSM\n",
6805 basename(dev));
6806 goto exit;
6807 }
9126b9a8 6808 }
f2f5c343 6809 }
1f5d54a0
MT
6810 if (freesize)
6811 *freesize = avail_size_imsm(st, ldsize >> 9, data_offset);
8662f92d
MT
6812 rv = 1;
6813exit:
6814 if (super)
6815 free_imsm(super);
6816 close(fd);
c2c087e6 6817
8662f92d 6818 return rv;
cdddbdbc
DW
6819}
6820
0dcecb2e
DW
6821static unsigned long long find_size(struct extent *e, int *idx, int num_extents)
6822{
6823 const unsigned long long base_start = e[*idx].start;
6824 unsigned long long end = base_start + e[*idx].size;
6825 int i;
6826
6827 if (base_start == end)
6828 return 0;
6829
6830 *idx = *idx + 1;
6831 for (i = *idx; i < num_extents; i++) {
6832 /* extend overlapping extents */
6833 if (e[i].start >= base_start &&
6834 e[i].start <= end) {
6835 if (e[i].size == 0)
6836 return 0;
6837 if (e[i].start + e[i].size > end)
6838 end = e[i].start + e[i].size;
6839 } else if (e[i].start > end) {
6840 *idx = i;
6841 break;
6842 }
6843 }
6844
6845 return end - base_start;
6846}
6847
aa19fdd4 6848/** merge_extents() - analyze extents and get free size.
9bc426fa 6849 * @super: Intel metadata, not NULL.
aa19fdd4 6850 * @expanding: if set, we are expanding &super->current_vol.
9bc426fa 6851 *
aa19fdd4
MT
6852 * Build a composite disk with all known extents and generate a size given the
6853 * "all disks in an array must share a common start offset" constraint.
6854 * If a volume is expanded, then return free space after the volume.
9bc426fa 6855 *
aa19fdd4 6856 * Return: Free space or 0 on failure.
9bc426fa 6857 */
aa19fdd4 6858static unsigned long long merge_extents(struct intel_super *super, const bool expanding)
0dcecb2e 6859{
9bc426fa 6860 struct extent *e;
0dcecb2e 6861 struct dl *dl;
aa19fdd4
MT
6862 int i, j, pos_vol_idx = -1;
6863 int extent_idx = 0;
6864 int sum_extents = 0;
6865 unsigned long long pos = 0;
b9d77223 6866 unsigned long long start = 0;
1dea84ae
MT
6867 unsigned long long free_size = 0;
6868
6869 unsigned long pre_reservation = 0;
6870 unsigned long post_reservation = IMSM_RESERVED_SECTORS;
6871 unsigned long reservation_size;
0dcecb2e 6872
9bc426fa
MT
6873 for (dl = super->disks; dl; dl = dl->next)
6874 if (dl->e)
6875 sum_extents += dl->extent_cnt;
6876 e = xcalloc(sum_extents, sizeof(struct extent));
6877
0dcecb2e
DW
6878 /* coalesce and sort all extents. also, check to see if we need to
6879 * reserve space between member arrays
6880 */
6881 j = 0;
6882 for (dl = super->disks; dl; dl = dl->next) {
6883 if (!dl->e)
6884 continue;
6885 for (i = 0; i < dl->extent_cnt; i++)
6886 e[j++] = dl->e[i];
6887 }
6888 qsort(e, sum_extents, sizeof(*e), cmp_extent);
6889
6890 /* merge extents */
6891 i = 0;
6892 j = 0;
6893 while (i < sum_extents) {
6894 e[j].start = e[i].start;
aa19fdd4 6895 e[j].vol = e[i].vol;
0dcecb2e
DW
6896 e[j].size = find_size(e, &i, sum_extents);
6897 j++;
6898 if (e[j-1].size == 0)
6899 break;
6900 }
6901
0dcecb2e
DW
6902 i = 0;
6903 do {
aa19fdd4 6904 unsigned long long esize = e[i].start - pos;
0dcecb2e 6905
1dea84ae
MT
6906 if (expanding ? pos_vol_idx == super->current_vol : esize >= free_size) {
6907 free_size = esize;
0dcecb2e 6908 start = pos;
aa19fdd4 6909 extent_idx = i;
0dcecb2e 6910 }
aa19fdd4 6911
0dcecb2e 6912 pos = e[i].start + e[i].size;
aa19fdd4
MT
6913 pos_vol_idx = e[i].vol;
6914
0dcecb2e
DW
6915 i++;
6916 } while (e[i-1].size);
0dcecb2e 6917
1dea84ae
MT
6918 if (free_size == 0) {
6919 dprintf("imsm: Cannot find free size.\n");
6920 free(e);
a7dd165b 6921 return 0;
1dea84ae 6922 }
a7dd165b 6923
1dea84ae
MT
6924 if (!expanding && extent_idx != 0)
6925 /*
6926 * Not a real first volume in a container is created, pre_reservation is needed.
6927 */
6928 pre_reservation = IMSM_RESERVED_SECTORS;
0dcecb2e 6929
1dea84ae
MT
6930 if (e[extent_idx].size == 0)
6931 /*
6932 * extent_idx points to the metadata, post_reservation is allready done.
6933 */
6934 post_reservation = 0;
6935 free(e);
0dcecb2e 6936
1dea84ae
MT
6937 reservation_size = pre_reservation + post_reservation;
6938
6939 if (free_size < reservation_size) {
6940 dprintf("imsm: Reservation size is greater than free space.\n");
6941 return 0;
6942 }
0dcecb2e 6943
1dea84ae
MT
6944 super->create_offset = start + pre_reservation;
6945 return free_size - reservation_size;
0dcecb2e
DW
6946}
6947
88c32bb1
DW
6948static int is_raid_level_supported(const struct imsm_orom *orom, int level, int raiddisks)
6949{
6950 if (level < 0 || level == 6 || level == 4)
6951 return 0;
6952
6953 /* if we have an orom prevent invalid raid levels */
6954 if (orom)
6955 switch (level) {
6956 case 0: return imsm_orom_has_raid0(orom);
6957 case 1:
6958 if (raiddisks > 2)
6959 return imsm_orom_has_raid1e(orom);
1c556e92
DW
6960 return imsm_orom_has_raid1(orom) && raiddisks == 2;
6961 case 10: return imsm_orom_has_raid10(orom) && raiddisks == 4;
6962 case 5: return imsm_orom_has_raid5(orom) && raiddisks > 2;
88c32bb1
DW
6963 }
6964 else
6965 return 1; /* not on an Intel RAID platform so anything goes */
6966
6967 return 0;
6968}
6969
ca9de185
LM
6970static int
6971active_arrays_by_format(char *name, char* hba, struct md_list **devlist,
6972 int dpa, int verbose)
6973{
6974 struct mdstat_ent *mdstat = mdstat_read(0, 0);
594dc1b8 6975 struct mdstat_ent *memb;
ca9de185
LM
6976 int count = 0;
6977 int num = 0;
594dc1b8 6978 struct md_list *dv;
ca9de185
LM
6979 int found;
6980
6981 for (memb = mdstat ; memb ; memb = memb->next) {
6982 if (memb->metadata_version &&
fc54fe7a 6983 (strncmp(memb->metadata_version, "external:", 9) == 0) &&
ca9de185
LM
6984 (strcmp(&memb->metadata_version[9], name) == 0) &&
6985 !is_subarray(memb->metadata_version+9) &&
6986 memb->members) {
6987 struct dev_member *dev = memb->members;
6988 int fd = -1;
4389ce73 6989 while (dev && !is_fd_valid(fd)) {
503975b9 6990 char *path = xmalloc(strlen(dev->name) + strlen("/dev/") + 1);
dd5ab402 6991 num = snprintf(path, PATH_MAX, "%s%s", "/dev/", dev->name);
503975b9
N
6992 if (num > 0)
6993 fd = open(path, O_RDONLY, 0);
4389ce73 6994 if (num <= 0 || !is_fd_valid(fd)) {
676e87a8 6995 pr_vrb("Cannot open %s: %s\n",
503975b9 6996 dev->name, strerror(errno));
ca9de185 6997 }
503975b9 6998 free(path);
ca9de185
LM
6999 dev = dev->next;
7000 }
7001 found = 0;
4389ce73 7002 if (is_fd_valid(fd) && disk_attached_to_hba(fd, hba)) {
ca9de185
LM
7003 struct mdstat_ent *vol;
7004 for (vol = mdstat ; vol ; vol = vol->next) {
089f9d79 7005 if (vol->active > 0 &&
ca9de185 7006 vol->metadata_version &&
9581efb1 7007 is_container_member(vol, memb->devnm)) {
ca9de185
LM
7008 found++;
7009 count++;
7010 }
7011 }
7012 if (*devlist && (found < dpa)) {
503975b9 7013 dv = xcalloc(1, sizeof(*dv));
9581efb1
N
7014 dv->devname = xmalloc(strlen(memb->devnm) + strlen("/dev/") + 1);
7015 sprintf(dv->devname, "%s%s", "/dev/", memb->devnm);
503975b9
N
7016 dv->found = found;
7017 dv->used = 0;
7018 dv->next = *devlist;
7019 *devlist = dv;
ca9de185
LM
7020 }
7021 }
4389ce73 7022 close_fd(&fd);
ca9de185
LM
7023 }
7024 }
7025 free_mdstat(mdstat);
7026 return count;
7027}
7028
7029#ifdef DEBUG_LOOP
7030static struct md_list*
7031get_loop_devices(void)
7032{
7033 int i;
7034 struct md_list *devlist = NULL;
594dc1b8 7035 struct md_list *dv;
ca9de185
LM
7036
7037 for(i = 0; i < 12; i++) {
503975b9
N
7038 dv = xcalloc(1, sizeof(*dv));
7039 dv->devname = xmalloc(40);
ca9de185
LM
7040 sprintf(dv->devname, "/dev/loop%d", i);
7041 dv->next = devlist;
7042 devlist = dv;
7043 }
7044 return devlist;
7045}
7046#endif
7047
7048static struct md_list*
7049get_devices(const char *hba_path)
7050{
7051 struct md_list *devlist = NULL;
594dc1b8 7052 struct md_list *dv;
ca9de185
LM
7053 struct dirent *ent;
7054 DIR *dir;
7055 int err = 0;
7056
7057#if DEBUG_LOOP
7058 devlist = get_loop_devices();
7059 return devlist;
7060#endif
7061 /* scroll through /sys/dev/block looking for devices attached to
7062 * this hba
7063 */
7064 dir = opendir("/sys/dev/block");
7065 for (ent = dir ? readdir(dir) : NULL; ent; ent = readdir(dir)) {
7066 int fd;
7067 char buf[1024];
7068 int major, minor;
7069 char *path = NULL;
7070 if (sscanf(ent->d_name, "%d:%d", &major, &minor) != 2)
7071 continue;
7c798f87 7072 path = devt_to_devpath(makedev(major, minor), 1, NULL);
ca9de185
LM
7073 if (!path)
7074 continue;
7075 if (!path_attached_to_hba(path, hba_path)) {
7076 free(path);
7077 path = NULL;
7078 continue;
7079 }
7080 free(path);
7081 path = NULL;
7082 fd = dev_open(ent->d_name, O_RDONLY);
4389ce73 7083 if (is_fd_valid(fd)) {
ca9de185
LM
7084 fd2devname(fd, buf);
7085 close(fd);
7086 } else {
e7b84f9d 7087 pr_err("cannot open device: %s\n",
ca9de185
LM
7088 ent->d_name);
7089 continue;
7090 }
7091
503975b9
N
7092 dv = xcalloc(1, sizeof(*dv));
7093 dv->devname = xstrdup(buf);
ca9de185
LM
7094 dv->next = devlist;
7095 devlist = dv;
7096 }
7097 if (err) {
7098 while(devlist) {
7099 dv = devlist;
7100 devlist = devlist->next;
7101 free(dv->devname);
7102 free(dv);
7103 }
7104 }
562aa102 7105 closedir(dir);
ca9de185
LM
7106 return devlist;
7107}
7108
7109static int
7110count_volumes_list(struct md_list *devlist, char *homehost,
7111 int verbose, int *found)
7112{
7113 struct md_list *tmpdev;
7114 int count = 0;
594dc1b8 7115 struct supertype *st;
ca9de185
LM
7116
7117 /* first walk the list of devices to find a consistent set
7118 * that match the criterea, if that is possible.
7119 * We flag the ones we like with 'used'.
7120 */
7121 *found = 0;
7122 st = match_metadata_desc_imsm("imsm");
7123 if (st == NULL) {
676e87a8 7124 pr_vrb("cannot allocate memory for imsm supertype\n");
ca9de185
LM
7125 return 0;
7126 }
7127
7128 for (tmpdev = devlist; tmpdev; tmpdev = tmpdev->next) {
7129 char *devname = tmpdev->devname;
0a6bff09 7130 dev_t rdev;
ca9de185
LM
7131 struct supertype *tst;
7132 int dfd;
7133 if (tmpdev->used > 1)
7134 continue;
7135 tst = dup_super(st);
7136 if (tst == NULL) {
676e87a8 7137 pr_vrb("cannot allocate memory for imsm supertype\n");
ca9de185
LM
7138 goto err_1;
7139 }
7140 tmpdev->container = 0;
7141 dfd = dev_open(devname, O_RDONLY|O_EXCL);
4389ce73 7142 if (!is_fd_valid(dfd)) {
1ade5cc1 7143 dprintf("cannot open device %s: %s\n",
ca9de185
LM
7144 devname, strerror(errno));
7145 tmpdev->used = 2;
0a6bff09 7146 } else if (!fstat_is_blkdev(dfd, devname, &rdev)) {
ca9de185
LM
7147 tmpdev->used = 2;
7148 } else if (must_be_container(dfd)) {
7149 struct supertype *cst;
7150 cst = super_by_fd(dfd, NULL);
7151 if (cst == NULL) {
1ade5cc1 7152 dprintf("cannot recognize container type %s\n",
ca9de185
LM
7153 devname);
7154 tmpdev->used = 2;
7155 } else if (tst->ss != st->ss) {
1ade5cc1 7156 dprintf("non-imsm container - ignore it: %s\n",
ca9de185
LM
7157 devname);
7158 tmpdev->used = 2;
7159 } else if (!tst->ss->load_container ||
7160 tst->ss->load_container(tst, dfd, NULL))
7161 tmpdev->used = 2;
7162 else {
7163 tmpdev->container = 1;
7164 }
7165 if (cst)
7166 cst->ss->free_super(cst);
7167 } else {
0a6bff09 7168 tmpdev->st_rdev = rdev;
ca9de185 7169 if (tst->ss->load_super(tst,dfd, NULL)) {
1ade5cc1 7170 dprintf("no RAID superblock on %s\n",
ca9de185
LM
7171 devname);
7172 tmpdev->used = 2;
7173 } else if (tst->ss->compare_super == NULL) {
1ade5cc1 7174 dprintf("Cannot assemble %s metadata on %s\n",
ca9de185
LM
7175 tst->ss->name, devname);
7176 tmpdev->used = 2;
7177 }
7178 }
4389ce73
MT
7179 close_fd(&dfd);
7180
ca9de185
LM
7181 if (tmpdev->used == 2 || tmpdev->used == 4) {
7182 /* Ignore unrecognised devices during auto-assembly */
7183 goto loop;
7184 }
7185 else {
7186 struct mdinfo info;
7187 tst->ss->getinfo_super(tst, &info, NULL);
7188
7189 if (st->minor_version == -1)
7190 st->minor_version = tst->minor_version;
7191
7192 if (memcmp(info.uuid, uuid_zero,
7193 sizeof(int[4])) == 0) {
7194 /* this is a floating spare. It cannot define
7195 * an array unless there are no more arrays of
7196 * this type to be found. It can be included
7197 * in an array of this type though.
7198 */
7199 tmpdev->used = 3;
7200 goto loop;
7201 }
7202
7203 if (st->ss != tst->ss ||
7204 st->minor_version != tst->minor_version ||
c7b8547c 7205 st->ss->compare_super(st, tst, 1) != 0) {
ca9de185
LM
7206 /* Some mismatch. If exactly one array matches this host,
7207 * we can resolve on that one.
7208 * Or, if we are auto assembling, we just ignore the second
7209 * for now.
7210 */
1ade5cc1 7211 dprintf("superblock on %s doesn't match others - assembly aborted\n",
ca9de185
LM
7212 devname);
7213 goto loop;
7214 }
7215 tmpdev->used = 1;
7216 *found = 1;
7217 dprintf("found: devname: %s\n", devname);
7218 }
7219 loop:
7220 if (tst)
7221 tst->ss->free_super(tst);
7222 }
7223 if (*found != 0) {
7224 int err;
7225 if ((err = load_super_imsm_all(st, -1, &st->sb, NULL, devlist, 0)) == 0) {
7226 struct mdinfo *iter, *head = st->ss->container_content(st, NULL);
7227 for (iter = head; iter; iter = iter->next) {
7228 dprintf("content->text_version: %s vol\n",
7229 iter->text_version);
7230 if (iter->array.state & (1<<MD_SB_BLOCK_VOLUME)) {
7231 /* do not assemble arrays with unsupported
7232 configurations */
1ade5cc1 7233 dprintf("Cannot activate member %s.\n",
ca9de185
LM
7234 iter->text_version);
7235 } else
7236 count++;
7237 }
7238 sysfs_free(head);
7239
7240 } else {
1ade5cc1 7241 dprintf("No valid super block on device list: err: %d %p\n",
ca9de185
LM
7242 err, st->sb);
7243 }
7244 } else {
1ade5cc1 7245 dprintf("no more devices to examine\n");
ca9de185
LM
7246 }
7247
7248 for (tmpdev = devlist; tmpdev; tmpdev = tmpdev->next) {
089f9d79 7249 if (tmpdev->used == 1 && tmpdev->found) {
ca9de185
LM
7250 if (count) {
7251 if (count < tmpdev->found)
7252 count = 0;
7253 else
7254 count -= tmpdev->found;
7255 }
7256 }
7257 if (tmpdev->used == 1)
7258 tmpdev->used = 4;
7259 }
7260 err_1:
7261 if (st)
7262 st->ss->free_super(st);
7263 return count;
7264}
7265
d3c11416
AO
7266static int __count_volumes(char *hba_path, int dpa, int verbose,
7267 int cmp_hba_path)
ca9de185 7268{
72a45777 7269 struct sys_dev *idev, *intel_devices = find_intel_devices();
ca9de185 7270 int count = 0;
72a45777
PB
7271 const struct orom_entry *entry;
7272 struct devid_list *dv, *devid_list;
ca9de185 7273
d3c11416 7274 if (!hba_path)
ca9de185
LM
7275 return 0;
7276
72a45777 7277 for (idev = intel_devices; idev; idev = idev->next) {
d3c11416
AO
7278 if (strstr(idev->path, hba_path))
7279 break;
72a45777
PB
7280 }
7281
7282 if (!idev || !idev->dev_id)
ca9de185 7283 return 0;
72a45777
PB
7284
7285 entry = get_orom_entry_by_device_id(idev->dev_id);
7286
7287 if (!entry || !entry->devid_list)
7288 return 0;
7289
7290 devid_list = entry->devid_list;
7291 for (dv = devid_list; dv; dv = dv->next) {
594dc1b8 7292 struct md_list *devlist;
d3c11416
AO
7293 struct sys_dev *device = NULL;
7294 char *hpath;
72a45777
PB
7295 int found = 0;
7296
d3c11416
AO
7297 if (cmp_hba_path)
7298 device = device_by_id_and_path(dv->devid, hba_path);
7299 else
7300 device = device_by_id(dv->devid);
7301
72a45777 7302 if (device)
d3c11416 7303 hpath = device->path;
72a45777
PB
7304 else
7305 return 0;
7306
d3c11416 7307 devlist = get_devices(hpath);
72a45777
PB
7308 /* if no intel devices return zero volumes */
7309 if (devlist == NULL)
7310 return 0;
7311
d3c11416
AO
7312 count += active_arrays_by_format("imsm", hpath, &devlist, dpa,
7313 verbose);
7314 dprintf("path: %s active arrays: %d\n", hpath, count);
72a45777
PB
7315 if (devlist == NULL)
7316 return 0;
7317 do {
7318 found = 0;
7319 count += count_volumes_list(devlist,
7320 NULL,
7321 verbose,
7322 &found);
7323 dprintf("found %d count: %d\n", found, count);
7324 } while (found);
7325
d3c11416 7326 dprintf("path: %s total number of volumes: %d\n", hpath, count);
72a45777
PB
7327
7328 while (devlist) {
7329 struct md_list *dv = devlist;
7330 devlist = devlist->next;
7331 free(dv->devname);
7332 free(dv);
7333 }
ca9de185
LM
7334 }
7335 return count;
7336}
7337
d3c11416
AO
7338static int count_volumes(struct intel_hba *hba, int dpa, int verbose)
7339{
7340 if (!hba)
7341 return 0;
7342 if (hba->type == SYS_DEV_VMD) {
7343 struct sys_dev *dev;
7344 int count = 0;
7345
7346 for (dev = find_intel_devices(); dev; dev = dev->next) {
7347 if (dev->type == SYS_DEV_VMD)
7348 count += __count_volumes(dev->path, dpa,
7349 verbose, 1);
7350 }
7351 return count;
7352 }
7353 return __count_volumes(hba->path, dpa, verbose, 0);
7354}
7355
cd9d1ac7
DW
7356static int imsm_default_chunk(const struct imsm_orom *orom)
7357{
7358 /* up to 512 if the plaform supports it, otherwise the platform max.
7359 * 128 if no platform detected
7360 */
7361 int fs = max(7, orom ? fls(orom->sss) : 0);
7362
7363 return min(512, (1 << fs));
7364}
73408129 7365
6592ce37
DW
7366static int
7367validate_geometry_imsm_orom(struct intel_super *super, int level, int layout,
2cc699af 7368 int raiddisks, int *chunk, unsigned long long size, int verbose)
6592ce37 7369{
660260d0
DW
7370 /* check/set platform and metadata limits/defaults */
7371 if (super->orom && raiddisks > super->orom->dpa) {
676e87a8 7372 pr_vrb("platform supports a maximum of %d disks per array\n",
660260d0 7373 super->orom->dpa);
73408129
LM
7374 return 0;
7375 }
7376
5d500228 7377 /* capabilities of OROM tested - copied from validate_geometry_imsm_volume */
660260d0 7378 if (!is_raid_level_supported(super->orom, level, raiddisks)) {
676e87a8 7379 pr_vrb("platform does not support raid%d with %d disk%s\n",
6592ce37
DW
7380 level, raiddisks, raiddisks > 1 ? "s" : "");
7381 return 0;
7382 }
cd9d1ac7 7383
7ccc4cc4 7384 if (*chunk == 0 || *chunk == UnSet)
cd9d1ac7
DW
7385 *chunk = imsm_default_chunk(super->orom);
7386
7ccc4cc4 7387 if (super->orom && !imsm_orom_has_chunk(super->orom, *chunk)) {
676e87a8 7388 pr_vrb("platform does not support a chunk size of: %d\n", *chunk);
cd9d1ac7 7389 return 0;
6592ce37 7390 }
cd9d1ac7 7391
6592ce37
DW
7392 if (layout != imsm_level_to_layout(level)) {
7393 if (level == 5)
676e87a8 7394 pr_vrb("imsm raid 5 only supports the left-asymmetric layout\n");
6592ce37 7395 else if (level == 10)
676e87a8 7396 pr_vrb("imsm raid 10 only supports the n2 layout\n");
6592ce37 7397 else
676e87a8 7398 pr_vrb("imsm unknown layout %#x for this raid level %d\n",
6592ce37
DW
7399 layout, level);
7400 return 0;
7401 }
2cc699af 7402
7ccc4cc4 7403 if (super->orom && (super->orom->attr & IMSM_OROM_ATTR_2TB) == 0 &&
2cc699af 7404 (calc_array_size(level, raiddisks, layout, *chunk, size) >> 32) > 0) {
676e87a8 7405 pr_vrb("platform does not support a volume size over 2TB\n");
2cc699af
CA
7406 return 0;
7407 }
614902f6 7408
6592ce37
DW
7409 return 1;
7410}
7411
1011e834 7412/* validate_geometry_imsm_volume - lifted from validate_geometry_ddf_bvd
c2c087e6
DW
7413 * FIX ME add ahci details
7414 */
8b353278 7415static int validate_geometry_imsm_volume(struct supertype *st, int level,
c21e737b 7416 int layout, int raiddisks, int *chunk,
af4348dd
N
7417 unsigned long long size,
7418 unsigned long long data_offset,
7419 char *dev,
2c514b71
NB
7420 unsigned long long *freesize,
7421 int verbose)
cdddbdbc 7422{
9e04ac1c 7423 dev_t rdev;
c2c087e6 7424 struct intel_super *super = st->sb;
b2916f25 7425 struct imsm_super *mpb;
c2c087e6
DW
7426 struct dl *dl;
7427 unsigned long long pos = 0;
7428 unsigned long long maxsize;
7429 struct extent *e;
7430 int i;
cdddbdbc 7431
88c32bb1
DW
7432 /* We must have the container info already read in. */
7433 if (!super)
c2c087e6
DW
7434 return 0;
7435
b2916f25
JS
7436 mpb = super->anchor;
7437
2cc699af 7438 if (!validate_geometry_imsm_orom(super, level, layout, raiddisks, chunk, size, verbose)) {
3e684231 7439 pr_err("RAID geometry validation failed. Cannot proceed with the action(s).\n");
c2c087e6 7440 return 0;
d54559f0 7441 }
c2c087e6
DW
7442 if (!dev) {
7443 /* General test: make sure there is space for
2da8544a
DW
7444 * 'raiddisks' device extents of size 'size' at a given
7445 * offset
c2c087e6 7446 */
e46273eb 7447 unsigned long long minsize = size;
b7528a20 7448 unsigned long long start_offset = MaxSector;
c2c087e6
DW
7449 int dcnt = 0;
7450 if (minsize == 0)
7451 minsize = MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS;
7452 for (dl = super->disks; dl ; dl = dl->next) {
7453 int found = 0;
7454
bf5a934a 7455 pos = 0;
c2c087e6 7456 i = 0;
05501181 7457 e = get_extents(super, dl, 0);
c2c087e6
DW
7458 if (!e) continue;
7459 do {
7460 unsigned long long esize;
7461 esize = e[i].start - pos;
7462 if (esize >= minsize)
7463 found = 1;
b7528a20 7464 if (found && start_offset == MaxSector) {
2da8544a
DW
7465 start_offset = pos;
7466 break;
7467 } else if (found && pos != start_offset) {
7468 found = 0;
7469 break;
7470 }
c2c087e6
DW
7471 pos = e[i].start + e[i].size;
7472 i++;
7473 } while (e[i-1].size);
7474 if (found)
7475 dcnt++;
7476 free(e);
7477 }
7478 if (dcnt < raiddisks) {
2c514b71 7479 if (verbose)
7a862a02 7480 pr_err("imsm: Not enough devices with space for this array (%d < %d)\n",
2c514b71 7481 dcnt, raiddisks);
c2c087e6
DW
7482 return 0;
7483 }
7484 return 1;
7485 }
0dcecb2e 7486
c2c087e6 7487 /* This device must be a member of the set */
9e04ac1c 7488 if (!stat_is_blkdev(dev, &rdev))
c2c087e6
DW
7489 return 0;
7490 for (dl = super->disks ; dl ; dl = dl->next) {
9e04ac1c
ZL
7491 if (dl->major == (int)major(rdev) &&
7492 dl->minor == (int)minor(rdev))
c2c087e6
DW
7493 break;
7494 }
7495 if (!dl) {
2c514b71 7496 if (verbose)
7a862a02 7497 pr_err("%s is not in the same imsm set\n", dev);
c2c087e6 7498 return 0;
a20d2ba5
DW
7499 } else if (super->orom && dl->index < 0 && mpb->num_raid_devs) {
7500 /* If a volume is present then the current creation attempt
7501 * cannot incorporate new spares because the orom may not
7502 * understand this configuration (all member disks must be
7503 * members of each array in the container).
7504 */
7a862a02
N
7505 pr_err("%s is a spare and a volume is already defined for this container\n", dev);
7506 pr_err("The option-rom requires all member disks to be a member of all volumes\n");
a20d2ba5 7507 return 0;
5fe62b94
WD
7508 } else if (super->orom && mpb->num_raid_devs > 0 &&
7509 mpb->num_disks != raiddisks) {
7a862a02 7510 pr_err("The option-rom requires all member disks to be a member of all volumes\n");
5fe62b94 7511 return 0;
c2c087e6 7512 }
0dcecb2e
DW
7513
7514 /* retrieve the largest free space block */
05501181 7515 e = get_extents(super, dl, 0);
c2c087e6
DW
7516 maxsize = 0;
7517 i = 0;
0dcecb2e
DW
7518 if (e) {
7519 do {
7520 unsigned long long esize;
7521
7522 esize = e[i].start - pos;
7523 if (esize >= maxsize)
7524 maxsize = esize;
7525 pos = e[i].start + e[i].size;
7526 i++;
7527 } while (e[i-1].size);
7528 dl->e = e;
7529 dl->extent_cnt = i;
7530 } else {
7531 if (verbose)
e7b84f9d 7532 pr_err("unable to determine free space for: %s\n",
0dcecb2e
DW
7533 dev);
7534 return 0;
7535 }
7536 if (maxsize < size) {
7537 if (verbose)
e7b84f9d 7538 pr_err("%s not enough space (%llu < %llu)\n",
0dcecb2e
DW
7539 dev, maxsize, size);
7540 return 0;
7541 }
7542
aa19fdd4 7543 maxsize = merge_extents(super, false);
3baa56ab 7544
1a1ced1e
KS
7545 if (mpb->num_raid_devs > 0 && size && size != maxsize)
7546 pr_err("attempting to create a second volume with size less then remaining space.\n");
3baa56ab 7547
a7dd165b 7548 if (maxsize < size || maxsize == 0) {
b3071342
LD
7549 if (verbose) {
7550 if (maxsize == 0)
7a862a02 7551 pr_err("no free space left on device. Aborting...\n");
b3071342 7552 else
7a862a02 7553 pr_err("not enough space to create volume of given size (%llu < %llu). Aborting...\n",
b3071342
LD
7554 maxsize, size);
7555 }
0dcecb2e 7556 return 0;
0dcecb2e
DW
7557 }
7558
c2c087e6
DW
7559 *freesize = maxsize;
7560
ca9de185 7561 if (super->orom) {
72a45777 7562 int count = count_volumes(super->hba,
ca9de185
LM
7563 super->orom->dpa, verbose);
7564 if (super->orom->vphba <= count) {
676e87a8 7565 pr_vrb("platform does not support more than %d raid volumes.\n",
ca9de185
LM
7566 super->orom->vphba);
7567 return 0;
7568 }
7569 }
c2c087e6 7570 return 1;
cdddbdbc
DW
7571}
7572
6d4d9ab2
MT
7573/**
7574 * imsm_get_free_size() - get the biggest, common free space from members.
7575 * @super: &intel_super pointer, not NULL.
7576 * @raiddisks: number of raid disks.
7577 * @size: requested size, could be 0 (means max size).
5f027b93 7578 * @chunk: requested chunk size in KiB.
6d4d9ab2
MT
7579 * @freesize: pointer for returned size value.
7580 *
7581 * Return: &IMSM_STATUS_OK or &IMSM_STATUS_ERROR.
7582 *
7583 * @freesize is set to meaningful value, this can be @size, or calculated
7584 * max free size.
7585 * super->create_offset value is modified and set appropriately in
7586 * merge_extends() for further creation.
7587 */
7588static imsm_status_t imsm_get_free_size(struct intel_super *super,
7589 const int raiddisks,
7590 unsigned long long size,
7591 const int chunk,
aa19fdd4
MT
7592 unsigned long long *freesize,
7593 bool expanding)
efb30e7f 7594{
efb30e7f
DW
7595 struct imsm_super *mpb = super->anchor;
7596 struct dl *dl;
7597 int i;
efb30e7f 7598 struct extent *e;
5f027b93
MT
7599 int cnt = 0;
7600 int used = 0;
efb30e7f 7601 unsigned long long maxsize;
5f027b93
MT
7602 unsigned long long minsize = size;
7603
7604 if (minsize == 0)
7605 minsize = chunk * 2;
efb30e7f
DW
7606
7607 /* find the largest common start free region of the possible disks */
efb30e7f
DW
7608 for (dl = super->disks; dl; dl = dl->next) {
7609 dl->raiddisk = -1;
7610
7611 if (dl->index >= 0)
7612 used++;
7613
7614 /* don't activate new spares if we are orom constrained
7615 * and there is already a volume active in the container
7616 */
7617 if (super->orom && dl->index < 0 && mpb->num_raid_devs)
7618 continue;
7619
05501181 7620 e = get_extents(super, dl, 0);
efb30e7f
DW
7621 if (!e)
7622 continue;
7623 for (i = 1; e[i-1].size; i++)
7624 ;
7625 dl->e = e;
7626 dl->extent_cnt = i;
efb30e7f
DW
7627 cnt++;
7628 }
7629
aa19fdd4 7630 maxsize = merge_extents(super, expanding);
5f027b93
MT
7631 if (maxsize < minsize) {
7632 pr_err("imsm: Free space is %llu but must be equal or larger than %llu.\n",
7633 maxsize, minsize);
7634 return IMSM_STATUS_ERROR;
7635 }
efb30e7f 7636
5f027b93
MT
7637 if (cnt < raiddisks || (super->orom && used && used != raiddisks)) {
7638 pr_err("imsm: Not enough devices with space to create array.\n");
6d4d9ab2 7639 return IMSM_STATUS_ERROR;
efb30e7f
DW
7640 }
7641
7642 if (size == 0) {
7643 size = maxsize;
7644 if (chunk) {
612e59d8
CA
7645 size /= 2 * chunk;
7646 size *= 2 * chunk;
efb30e7f 7647 }
f878b242
LM
7648 maxsize = size;
7649 }
1a1ced1e
KS
7650 if (mpb->num_raid_devs > 0 && size && size != maxsize)
7651 pr_err("attempting to create a second volume with size less then remaining space.\n");
efb30e7f
DW
7652 *freesize = size;
7653
13bcac90
AK
7654 dprintf("imsm: imsm_get_free_size() returns : %llu\n", size);
7655
6d4d9ab2 7656 return IMSM_STATUS_OK;
efb30e7f
DW
7657}
7658
6d4d9ab2
MT
7659/**
7660 * autolayout_imsm() - automatically layout a new volume.
7661 * @super: &intel_super pointer, not NULL.
7662 * @raiddisks: number of raid disks.
7663 * @size: requested size, could be 0 (means max size).
7664 * @chunk: requested chunk.
7665 * @freesize: pointer for returned size value.
7666 *
7667 * We are being asked to automatically layout a new volume based on the current
7668 * contents of the container. If the parameters can be satisfied autolayout_imsm
7669 * will record the disks, start offset, and will return size of the volume to
7670 * be created. See imsm_get_free_size() for details.
7671 * add_to_super() and getinfo_super() detect when autolayout is in progress.
7672 * If first volume exists, slots are set consistently to it.
7673 *
7674 * Return: &IMSM_STATUS_OK on success, &IMSM_STATUS_ERROR otherwise.
7675 *
7676 * Disks are marked for creation via dl->raiddisk.
7677 */
7678static imsm_status_t autolayout_imsm(struct intel_super *super,
7679 const int raiddisks,
7680 unsigned long long size, const int chunk,
7681 unsigned long long *freesize)
13bcac90 7682{
6d4d9ab2
MT
7683 int curr_slot = 0;
7684 struct dl *disk;
7685 int vol_cnt = super->anchor->num_raid_devs;
7686 imsm_status_t rv;
13bcac90 7687
aa19fdd4 7688 rv = imsm_get_free_size(super, raiddisks, size, chunk, freesize, false);
6d4d9ab2
MT
7689 if (rv != IMSM_STATUS_OK)
7690 return IMSM_STATUS_ERROR;
7691
7692 for (disk = super->disks; disk; disk = disk->next) {
7693 if (!disk->e)
7694 continue;
7695
7696 if (curr_slot == raiddisks)
7697 break;
7698
7699 if (vol_cnt == 0) {
7700 disk->raiddisk = curr_slot;
7701 } else {
7702 int _slot = get_disk_slot_in_dev(super, 0, disk->index);
7703
7704 if (_slot == -1) {
7705 pr_err("Disk %s is not used in first volume, aborting\n",
7706 disk->devname);
7707 return IMSM_STATUS_ERROR;
7708 }
7709 disk->raiddisk = _slot;
7710 }
7711 curr_slot++;
13bcac90
AK
7712 }
7713
6d4d9ab2 7714 return IMSM_STATUS_OK;
13bcac90
AK
7715}
7716
bf5a934a 7717static int validate_geometry_imsm(struct supertype *st, int level, int layout,
c21e737b 7718 int raiddisks, int *chunk, unsigned long long size,
af4348dd 7719 unsigned long long data_offset,
bf5a934a 7720 char *dev, unsigned long long *freesize,
5308f117 7721 int consistency_policy, int verbose)
bf5a934a
DW
7722{
7723 int fd, cfd;
7724 struct mdinfo *sra;
20cbe8d2 7725 int is_member = 0;
bf5a934a 7726
d54559f0
LM
7727 /* load capability
7728 * if given unused devices create a container
bf5a934a
DW
7729 * if given given devices in a container create a member volume
7730 */
6f2af6a4 7731 if (is_container(level))
bf5a934a 7732 /* Must be a fresh device to add to a container */
1f5d54a0
MT
7733 return validate_geometry_imsm_container(st, level, raiddisks,
7734 data_offset, dev,
7735 freesize, verbose);
9587c373 7736
06a6101c
BK
7737 /*
7738 * Size is given in sectors.
7739 */
7740 if (size && (size < 2048)) {
22dc741f 7741 pr_err("Given size must be greater than 1M.\n");
54865c30
RS
7742 /* Depends on algorithm in Create.c :
7743 * if container was given (dev == NULL) return -1,
7744 * if block device was given ( dev != NULL) return 0.
7745 */
7746 return dev ? -1 : 0;
7747 }
7748
8592f29d 7749 if (!dev) {
6d4d9ab2
MT
7750 struct intel_super *super = st->sb;
7751
7752 /*
071f839e 7753 * Autolayout mode, st->sb must be set.
6d4d9ab2 7754 */
071f839e
KT
7755 if (!super) {
7756 pr_vrb("superblock must be set for autolayout, aborting\n");
7757 return 0;
6d4d9ab2
MT
7758 }
7759
7760 if (!validate_geometry_imsm_orom(st->sb, level, layout,
7761 raiddisks, chunk, size,
7762 verbose))
7763 return 0;
7764
071f839e 7765 if (super->orom && freesize) {
6d4d9ab2
MT
7766 imsm_status_t rv;
7767 int count = count_volumes(super->hba, super->orom->dpa,
7768 verbose);
7769 if (super->orom->vphba <= count) {
7770 pr_vrb("platform does not support more than %d raid volumes.\n",
7771 super->orom->vphba);
e91a3bad 7772 return 0;
ca9de185 7773 }
6d4d9ab2
MT
7774
7775 rv = autolayout_imsm(super, raiddisks, size, *chunk,
7776 freesize);
7777 if (rv != IMSM_STATUS_OK)
7778 return 0;
8592f29d
N
7779 }
7780 return 1;
7781 }
bf5a934a
DW
7782 if (st->sb) {
7783 /* creating in a given container */
7784 return validate_geometry_imsm_volume(st, level, layout,
7785 raiddisks, chunk, size,
af4348dd 7786 data_offset,
bf5a934a
DW
7787 dev, freesize, verbose);
7788 }
7789
bf5a934a
DW
7790 /* This device needs to be a device in an 'imsm' container */
7791 fd = open(dev, O_RDONLY|O_EXCL, 0);
4389ce73
MT
7792
7793 if (is_fd_valid(fd)) {
7794 pr_vrb("Cannot create this array on device %s\n", dev);
bf5a934a
DW
7795 close(fd);
7796 return 0;
7797 }
4389ce73
MT
7798 if (errno == EBUSY)
7799 fd = open(dev, O_RDONLY, 0);
7800
7801 if (!is_fd_valid(fd)) {
7802 pr_vrb("Cannot open %s: %s\n", dev, strerror(errno));
bf5a934a
DW
7803 return 0;
7804 }
4389ce73 7805
bf5a934a
DW
7806 /* Well, it is in use by someone, maybe an 'imsm' container. */
7807 cfd = open_container(fd);
4389ce73
MT
7808 close_fd(&fd);
7809
7810 if (!is_fd_valid(cfd)) {
7811 pr_vrb("Cannot use %s: It is busy\n", dev);
bf5a934a
DW
7812 return 0;
7813 }
4dd2df09 7814 sra = sysfs_read(cfd, NULL, GET_VERSION);
bf5a934a 7815 if (sra && sra->array.major_version == -1 &&
20cbe8d2
AW
7816 strcmp(sra->text_version, "imsm") == 0)
7817 is_member = 1;
7818 sysfs_free(sra);
7819 if (is_member) {
bf5a934a
DW
7820 /* This is a member of a imsm container. Load the container
7821 * and try to create a volume
7822 */
7823 struct intel_super *super;
7824
ec50f7b6 7825 if (load_super_imsm_all(st, cfd, (void **) &super, NULL, NULL, 1) == 0) {
bf5a934a 7826 st->sb = super;
4dd2df09 7827 strcpy(st->container_devnm, fd2devnm(cfd));
bf5a934a
DW
7828 close(cfd);
7829 return validate_geometry_imsm_volume(st, level, layout,
7830 raiddisks, chunk,
af4348dd 7831 size, data_offset, dev,
ecbd9e81
N
7832 freesize, 1)
7833 ? 1 : -1;
bf5a934a 7834 }
20cbe8d2 7835 }
bf5a934a 7836
20cbe8d2 7837 if (verbose)
e7b84f9d 7838 pr_err("failed container membership check\n");
20cbe8d2
AW
7839
7840 close(cfd);
7841 return 0;
bf5a934a 7842}
0bd16cf2 7843
30f58b22 7844static void default_geometry_imsm(struct supertype *st, int *level, int *layout, int *chunk)
0bd16cf2
DJ
7845{
7846 struct intel_super *super = st->sb;
7847
30f58b22
DW
7848 if (level && *level == UnSet)
7849 *level = LEVEL_CONTAINER;
7850
7851 if (level && layout && *layout == UnSet)
7852 *layout = imsm_level_to_layout(*level);
0bd16cf2 7853
cd9d1ac7
DW
7854 if (chunk && (*chunk == UnSet || *chunk == 0))
7855 *chunk = imsm_default_chunk(super->orom);
0bd16cf2
DJ
7856}
7857
33414a01
DW
7858static void handle_missing(struct intel_super *super, struct imsm_dev *dev);
7859
3364781b 7860static int kill_subarray_imsm(struct supertype *st, char *subarray_id)
33414a01 7861{
3364781b 7862 /* remove the subarray currently referenced by subarray_id */
33414a01
DW
7863 __u8 i;
7864 struct intel_dev **dp;
7865 struct intel_super *super = st->sb;
3364781b 7866 __u8 current_vol = strtoul(subarray_id, NULL, 10);
33414a01
DW
7867 struct imsm_super *mpb = super->anchor;
7868
3364781b 7869 if (mpb->num_raid_devs == 0)
33414a01 7870 return 2;
33414a01
DW
7871
7872 /* block deletions that would change the uuid of active subarrays
7873 *
7874 * FIXME when immutable ids are available, but note that we'll
7875 * also need to fixup the invalidated/active subarray indexes in
7876 * mdstat
7877 */
7878 for (i = 0; i < mpb->num_raid_devs; i++) {
7879 char subarray[4];
7880
7881 if (i < current_vol)
7882 continue;
dd5ab402 7883 snprintf(subarray, sizeof(subarray), "%u", i);
4dd2df09 7884 if (is_subarray_active(subarray, st->devnm)) {
e7b84f9d
N
7885 pr_err("deleting subarray-%d would change the UUID of active subarray-%d, aborting\n",
7886 current_vol, i);
33414a01
DW
7887
7888 return 2;
7889 }
7890 }
7891
7892 if (st->update_tail) {
503975b9 7893 struct imsm_update_kill_array *u = xmalloc(sizeof(*u));
33414a01 7894
33414a01
DW
7895 u->type = update_kill_array;
7896 u->dev_idx = current_vol;
7897 append_metadata_update(st, u, sizeof(*u));
7898
7899 return 0;
7900 }
7901
7902 for (dp = &super->devlist; *dp;)
7903 if ((*dp)->index == current_vol) {
7904 *dp = (*dp)->next;
7905 } else {
7906 handle_missing(super, (*dp)->dev);
7907 if ((*dp)->index > current_vol)
7908 (*dp)->index--;
7909 dp = &(*dp)->next;
7910 }
7911
7912 /* no more raid devices, all active components are now spares,
7913 * but of course failed are still failed
7914 */
7915 if (--mpb->num_raid_devs == 0) {
7916 struct dl *d;
7917
7918 for (d = super->disks; d; d = d->next)
a8619d23
AK
7919 if (d->index > -2)
7920 mark_spare(d);
33414a01
DW
7921 }
7922
7923 super->updates_pending++;
7924
7925 return 0;
7926}
aa534678 7927
4345e135
MK
7928/**
7929 * get_rwh_policy_from_update() - Get the rwh policy for update option.
7930 * @update: Update option.
7931 */
7932static int get_rwh_policy_from_update(enum update_opt update)
19ad203e 7933{
4345e135
MK
7934 switch (update) {
7935 case UOPT_PPL:
19ad203e 7936 return RWH_MULTIPLE_DISTRIBUTED;
4345e135 7937 case UOPT_NO_PPL:
19ad203e 7938 return RWH_MULTIPLE_OFF;
4345e135 7939 case UOPT_BITMAP:
19ad203e 7940 return RWH_BITMAP;
4345e135 7941 case UOPT_NO_BITMAP:
19ad203e 7942 return RWH_OFF;
4345e135
MK
7943 default:
7944 break;
7945 }
7946 return UOPT_UNDEFINED;
19ad203e
JR
7947}
7948
a951a4f7 7949static int update_subarray_imsm(struct supertype *st, char *subarray,
03312b52 7950 enum update_opt update, struct mddev_ident *ident)
aa534678
DW
7951{
7952 /* update the subarray currently referenced by ->current_vol */
7953 struct intel_super *super = st->sb;
7954 struct imsm_super *mpb = super->anchor;
7955
03312b52 7956 if (update == UOPT_NAME) {
aa534678 7957 char *name = ident->name;
a951a4f7
N
7958 char *ep;
7959 int vol;
aa534678 7960
e2eb503b 7961 if (imsm_is_name_allowed(super, name, 1) == false)
aa534678
DW
7962 return 2;
7963
a951a4f7
N
7964 vol = strtoul(subarray, &ep, 10);
7965 if (*ep != '\0' || vol >= super->anchor->num_raid_devs)
7966 return 2;
7967
aa534678 7968 if (st->update_tail) {
503975b9 7969 struct imsm_update_rename_array *u = xmalloc(sizeof(*u));
aa534678 7970
aa534678 7971 u->type = update_rename_array;
a951a4f7 7972 u->dev_idx = vol;
618f4e6d
XN
7973 strncpy((char *) u->name, name, MAX_RAID_SERIAL_LEN);
7974 u->name[MAX_RAID_SERIAL_LEN-1] = '\0';
aa534678
DW
7975 append_metadata_update(st, u, sizeof(*u));
7976 } else {
7977 struct imsm_dev *dev;
ebad3af2 7978 int i, namelen;
aa534678 7979
a951a4f7 7980 dev = get_imsm_dev(super, vol);
ebad3af2
JS
7981 memset(dev->volume, '\0', MAX_RAID_SERIAL_LEN);
7982 namelen = min((int)strlen(name), MAX_RAID_SERIAL_LEN);
7983 memcpy(dev->volume, name, namelen);
aa534678
DW
7984 for (i = 0; i < mpb->num_raid_devs; i++) {
7985 dev = get_imsm_dev(super, i);
7986 handle_missing(super, dev);
7987 }
7988 super->updates_pending++;
7989 }
03312b52 7990 } else if (get_rwh_policy_from_update(update) != UOPT_UNDEFINED) {
e6e9dd3f
AP
7991 int new_policy;
7992 char *ep;
7993 int vol = strtoul(subarray, &ep, 10);
7994
7995 if (*ep != '\0' || vol >= super->anchor->num_raid_devs)
7996 return 2;
7997
03312b52 7998 new_policy = get_rwh_policy_from_update(update);
e6e9dd3f
AP
7999
8000 if (st->update_tail) {
8001 struct imsm_update_rwh_policy *u = xmalloc(sizeof(*u));
8002
8003 u->type = update_rwh_policy;
8004 u->dev_idx = vol;
8005 u->new_policy = new_policy;
8006 append_metadata_update(st, u, sizeof(*u));
8007 } else {
8008 struct imsm_dev *dev;
8009
8010 dev = get_imsm_dev(super, vol);
8011 dev->rwh_policy = new_policy;
8012 super->updates_pending++;
8013 }
19ad203e
JR
8014 if (new_policy == RWH_BITMAP)
8015 return write_init_bitmap_imsm_vol(st, vol);
aa534678
DW
8016 } else
8017 return 2;
8018
8019 return 0;
8020}
bf5a934a 8021
195d1d76 8022static bool is_gen_migration(struct imsm_dev *dev)
28bce06f 8023{
195d1d76
PP
8024 if (dev && dev->vol.migr_state &&
8025 migr_type(dev) == MIGR_GEN_MIGR)
8026 return true;
28bce06f 8027
195d1d76 8028 return false;
28bce06f
AK
8029}
8030
1e5c6983
DW
8031static int is_rebuilding(struct imsm_dev *dev)
8032{
8033 struct imsm_map *migr_map;
8034
8035 if (!dev->vol.migr_state)
8036 return 0;
8037
8038 if (migr_type(dev) != MIGR_REBUILD)
8039 return 0;
8040
238c0a71 8041 migr_map = get_imsm_map(dev, MAP_1);
1e5c6983
DW
8042
8043 if (migr_map->map_state == IMSM_T_STATE_DEGRADED)
8044 return 1;
8045 else
8046 return 0;
8047}
8048
6ce1fbf1
AK
8049static int is_initializing(struct imsm_dev *dev)
8050{
8051 struct imsm_map *migr_map;
8052
8053 if (!dev->vol.migr_state)
8054 return 0;
8055
8056 if (migr_type(dev) != MIGR_INIT)
8057 return 0;
8058
238c0a71 8059 migr_map = get_imsm_map(dev, MAP_1);
6ce1fbf1
AK
8060
8061 if (migr_map->map_state == IMSM_T_STATE_UNINITIALIZED)
8062 return 1;
8063
8064 return 0;
6ce1fbf1
AK
8065}
8066
c47b0ff6
AK
8067static void update_recovery_start(struct intel_super *super,
8068 struct imsm_dev *dev,
8069 struct mdinfo *array)
1e5c6983
DW
8070{
8071 struct mdinfo *rebuild = NULL;
8072 struct mdinfo *d;
8073 __u32 units;
8074
8075 if (!is_rebuilding(dev))
8076 return;
8077
8078 /* Find the rebuild target, but punt on the dual rebuild case */
8079 for (d = array->devs; d; d = d->next)
8080 if (d->recovery_start == 0) {
8081 if (rebuild)
8082 return;
8083 rebuild = d;
8084 }
8085
4363fd80
DW
8086 if (!rebuild) {
8087 /* (?) none of the disks are marked with
8088 * IMSM_ORD_REBUILD, so assume they are missing and the
8089 * disk_ord_tbl was not correctly updated
8090 */
1ade5cc1 8091 dprintf("failed to locate out-of-sync disk\n");
4363fd80
DW
8092 return;
8093 }
8094
4036e7ee 8095 units = vol_curr_migr_unit(dev);
c47b0ff6 8096 rebuild->recovery_start = units * blocks_per_migr_unit(super, dev);
1e5c6983
DW
8097}
8098
276d77db 8099static int recover_backup_imsm(struct supertype *st, struct mdinfo *info);
1e5c6983 8100
00bbdbda 8101static struct mdinfo *container_content_imsm(struct supertype *st, char *subarray)
cdddbdbc 8102{
4f5bc454
DW
8103 /* Given a container loaded by load_super_imsm_all,
8104 * extract information about all the arrays into
8105 * an mdinfo tree.
00bbdbda 8106 * If 'subarray' is given, just extract info about that array.
4f5bc454
DW
8107 *
8108 * For each imsm_dev create an mdinfo, fill it in,
8109 * then look for matching devices in super->disks
8110 * and create appropriate device mdinfo.
8111 */
8112 struct intel_super *super = st->sb;
949c47a0 8113 struct imsm_super *mpb = super->anchor;
4f5bc454 8114 struct mdinfo *rest = NULL;
00bbdbda 8115 unsigned int i;
81219e70 8116 int sb_errors = 0;
abef11a3
AK
8117 struct dl *d;
8118 int spare_disks = 0;
b6180160 8119 int current_vol = super->current_vol;
cdddbdbc 8120
19482bcc
AK
8121 /* do not assemble arrays when not all attributes are supported */
8122 if (imsm_check_attributes(mpb->attributes) == 0) {
81219e70 8123 sb_errors = 1;
7a862a02 8124 pr_err("Unsupported attributes in IMSM metadata.Arrays activation is blocked.\n");
19482bcc
AK
8125 }
8126
abef11a3
AK
8127 /* count spare devices, not used in maps
8128 */
8129 for (d = super->disks; d; d = d->next)
8130 if (d->index == -1)
8131 spare_disks++;
8132
4f5bc454 8133 for (i = 0; i < mpb->num_raid_devs; i++) {
00bbdbda
N
8134 struct imsm_dev *dev;
8135 struct imsm_map *map;
86e3692b 8136 struct imsm_map *map2;
4f5bc454 8137 struct mdinfo *this;
a6482415 8138 int slot;
a6482415 8139 int chunk;
00bbdbda 8140 char *ep;
8b9cd157 8141 int level;
00bbdbda
N
8142
8143 if (subarray &&
8144 (i != strtoul(subarray, &ep, 10) || *ep != '\0'))
8145 continue;
8146
8147 dev = get_imsm_dev(super, i);
238c0a71
AK
8148 map = get_imsm_map(dev, MAP_0);
8149 map2 = get_imsm_map(dev, MAP_1);
8b9cd157 8150 level = get_imsm_raid_level(map);
4f5bc454 8151
1ce0101c
DW
8152 /* do not publish arrays that are in the middle of an
8153 * unsupported migration
8154 */
8155 if (dev->vol.migr_state &&
28bce06f 8156 (migr_type(dev) == MIGR_STATE_CHANGE)) {
7a862a02 8157 pr_err("cannot assemble volume '%.16s': unsupported migration in progress\n",
1ce0101c
DW
8158 dev->volume);
8159 continue;
8160 }
2db86302
LM
8161 /* do not publish arrays that are not support by controller's
8162 * OROM/EFI
8163 */
1ce0101c 8164
503975b9 8165 this = xmalloc(sizeof(*this));
4f5bc454 8166
301406c9 8167 super->current_vol = i;
a5d85af7 8168 getinfo_super_imsm_volume(st, this, NULL);
9894ec0d 8169 this->next = rest;
a6482415 8170 chunk = __le16_to_cpu(map->blocks_per_strip) >> 1;
81219e70
LM
8171 /* mdadm does not support all metadata features- set the bit in all arrays state */
8172 if (!validate_geometry_imsm_orom(super,
8b9cd157
MK
8173 level, /* RAID level */
8174 imsm_level_to_layout(level),
81219e70 8175 map->num_members, /* raid disks */
fcc2c9da 8176 &chunk, imsm_dev_size(dev),
81219e70 8177 1 /* verbose */)) {
7a862a02 8178 pr_err("IMSM RAID geometry validation failed. Array %s activation is blocked.\n",
81219e70
LM
8179 dev->volume);
8180 this->array.state |=
8181 (1<<MD_SB_BLOCK_CONTAINER_RESHAPE) |
8182 (1<<MD_SB_BLOCK_VOLUME);
8183 }
81219e70
LM
8184
8185 /* if array has bad blocks, set suitable bit in all arrays state */
8186 if (sb_errors)
8187 this->array.state |=
8188 (1<<MD_SB_BLOCK_CONTAINER_RESHAPE) |
8189 (1<<MD_SB_BLOCK_VOLUME);
8190
4f5bc454 8191 for (slot = 0 ; slot < map->num_members; slot++) {
1e5c6983 8192 unsigned long long recovery_start;
4f5bc454
DW
8193 struct mdinfo *info_d;
8194 struct dl *d;
8195 int idx;
9a1608e5 8196 int skip;
7eef0453 8197 __u32 ord;
8b9cd157 8198 int missing = 0;
4f5bc454 8199
9a1608e5 8200 skip = 0;
238c0a71
AK
8201 idx = get_imsm_disk_idx(dev, slot, MAP_0);
8202 ord = get_imsm_ord_tbl_ent(dev, slot, MAP_X);
4f5bc454
DW
8203 for (d = super->disks; d ; d = d->next)
8204 if (d->index == idx)
0fbd635c 8205 break;
4f5bc454 8206
1e5c6983 8207 recovery_start = MaxSector;
4f5bc454 8208 if (d == NULL)
9a1608e5 8209 skip = 1;
25ed7e59 8210 if (d && is_failed(&d->disk))
9a1608e5 8211 skip = 1;
8b9cd157 8212 if (!skip && (ord & IMSM_ORD_REBUILD))
1e5c6983 8213 recovery_start = 0;
1e93d0d1
BK
8214 if (!(ord & IMSM_ORD_REBUILD))
8215 this->array.working_disks++;
1011e834 8216 /*
9a1608e5 8217 * if we skip some disks the array will be assmebled degraded;
1e5c6983
DW
8218 * reset resync start to avoid a dirty-degraded
8219 * situation when performing the intial sync
9a1608e5 8220 */
8b9cd157
MK
8221 if (skip)
8222 missing++;
8223
8224 if (!(dev->vol.dirty & RAIDVOL_DIRTY)) {
8225 if ((!able_to_resync(level, missing) ||
8226 recovery_start == 0))
8227 this->resync_start = MaxSector;
8b9cd157
MK
8228 }
8229
9a1608e5
DW
8230 if (skip)
8231 continue;
4f5bc454 8232
503975b9 8233 info_d = xcalloc(1, sizeof(*info_d));
4f5bc454
DW
8234 info_d->next = this->devs;
8235 this->devs = info_d;
8236
4f5bc454
DW
8237 info_d->disk.number = d->index;
8238 info_d->disk.major = d->major;
8239 info_d->disk.minor = d->minor;
8240 info_d->disk.raid_disk = slot;
1e5c6983 8241 info_d->recovery_start = recovery_start;
86e3692b
AK
8242 if (map2) {
8243 if (slot < map2->num_members)
8244 info_d->disk.state = (1 << MD_DISK_ACTIVE);
04c3c514
AK
8245 else
8246 this->array.spare_disks++;
86e3692b
AK
8247 } else {
8248 if (slot < map->num_members)
8249 info_d->disk.state = (1 << MD_DISK_ACTIVE);
04c3c514
AK
8250 else
8251 this->array.spare_disks++;
86e3692b 8252 }
4f5bc454
DW
8253
8254 info_d->events = __le32_to_cpu(mpb->generation_num);
5551b113 8255 info_d->data_offset = pba_of_lba0(map);
44490938 8256 info_d->component_size = calc_component_size(map, dev);
06fb291a
PB
8257
8258 if (map->raid_level == 5) {
2432ce9b
AP
8259 info_d->ppl_sector = this->ppl_sector;
8260 info_d->ppl_size = this->ppl_size;
98e96bdb
AP
8261 if (this->consistency_policy == CONSISTENCY_POLICY_PPL &&
8262 recovery_start == 0)
8263 this->resync_start = 0;
06fb291a 8264 }
b12796be 8265
5e46202e 8266 info_d->bb.supported = 1;
b12796be
TM
8267 get_volume_badblocks(super->bbm_log, ord_to_idx(ord),
8268 info_d->data_offset,
8269 info_d->component_size,
8270 &info_d->bb);
4f5bc454 8271 }
1e5c6983 8272 /* now that the disk list is up-to-date fixup recovery_start */
c47b0ff6 8273 update_recovery_start(super, dev, this);
abef11a3 8274 this->array.spare_disks += spare_disks;
276d77db
AK
8275
8276 /* check for reshape */
8277 if (this->reshape_active == 1)
8278 recover_backup_imsm(st, this);
9a1608e5 8279 rest = this;
4f5bc454
DW
8280 }
8281
b6180160 8282 super->current_vol = current_vol;
4f5bc454 8283 return rest;
cdddbdbc
DW
8284}
8285
3b451610
AK
8286static __u8 imsm_check_degraded(struct intel_super *super, struct imsm_dev *dev,
8287 int failed, int look_in_map)
c2a1e7da 8288{
3b451610
AK
8289 struct imsm_map *map;
8290
8291 map = get_imsm_map(dev, look_in_map);
c2a1e7da
DW
8292
8293 if (!failed)
1011e834 8294 return map->map_state == IMSM_T_STATE_UNINITIALIZED ?
3393c6af 8295 IMSM_T_STATE_UNINITIALIZED : IMSM_T_STATE_NORMAL;
c2a1e7da
DW
8296
8297 switch (get_imsm_raid_level(map)) {
8298 case 0:
8299 return IMSM_T_STATE_FAILED;
8300 break;
8301 case 1:
8302 if (failed < map->num_members)
8303 return IMSM_T_STATE_DEGRADED;
8304 else
8305 return IMSM_T_STATE_FAILED;
8306 break;
8307 case 10:
8308 {
8309 /**
c92a2527
DW
8310 * check to see if any mirrors have failed, otherwise we
8311 * are degraded. Even numbered slots are mirrored on
8312 * slot+1
c2a1e7da 8313 */
c2a1e7da 8314 int i;
d9b420a5
N
8315 /* gcc -Os complains that this is unused */
8316 int insync = insync;
c2a1e7da
DW
8317
8318 for (i = 0; i < map->num_members; i++) {
238c0a71 8319 __u32 ord = get_imsm_ord_tbl_ent(dev, i, MAP_X);
c92a2527
DW
8320 int idx = ord_to_idx(ord);
8321 struct imsm_disk *disk;
c2a1e7da 8322
c92a2527 8323 /* reset the potential in-sync count on even-numbered
1011e834 8324 * slots. num_copies is always 2 for imsm raid10
c92a2527
DW
8325 */
8326 if ((i & 1) == 0)
8327 insync = 2;
c2a1e7da 8328
c92a2527 8329 disk = get_imsm_disk(super, idx);
25ed7e59 8330 if (!disk || is_failed(disk) || ord & IMSM_ORD_REBUILD)
c92a2527 8331 insync--;
c2a1e7da 8332
c92a2527
DW
8333 /* no in-sync disks left in this mirror the
8334 * array has failed
8335 */
8336 if (insync == 0)
8337 return IMSM_T_STATE_FAILED;
c2a1e7da
DW
8338 }
8339
8340 return IMSM_T_STATE_DEGRADED;
8341 }
8342 case 5:
8343 if (failed < 2)
8344 return IMSM_T_STATE_DEGRADED;
8345 else
8346 return IMSM_T_STATE_FAILED;
8347 break;
8348 default:
8349 break;
8350 }
8351
8352 return map->map_state;
8353}
8354
3b451610
AK
8355static int imsm_count_failed(struct intel_super *super, struct imsm_dev *dev,
8356 int look_in_map)
c2a1e7da
DW
8357{
8358 int i;
8359 int failed = 0;
8360 struct imsm_disk *disk;
d5985138
AK
8361 struct imsm_map *map = get_imsm_map(dev, MAP_0);
8362 struct imsm_map *prev = get_imsm_map(dev, MAP_1);
68fe4598 8363 struct imsm_map *map_for_loop;
0556e1a2
DW
8364 __u32 ord;
8365 int idx;
d5985138 8366 int idx_1;
c2a1e7da 8367
0556e1a2
DW
8368 /* at the beginning of migration we set IMSM_ORD_REBUILD on
8369 * disks that are being rebuilt. New failures are recorded to
8370 * map[0]. So we look through all the disks we started with and
8371 * see if any failures are still present, or if any new ones
8372 * have arrived
0556e1a2 8373 */
d5985138
AK
8374 map_for_loop = map;
8375 if (prev && (map->num_members < prev->num_members))
8376 map_for_loop = prev;
68fe4598
LD
8377
8378 for (i = 0; i < map_for_loop->num_members; i++) {
d5985138 8379 idx_1 = -255;
238c0a71
AK
8380 /* when MAP_X is passed both maps failures are counted
8381 */
d5985138 8382 if (prev &&
089f9d79
JS
8383 (look_in_map == MAP_1 || look_in_map == MAP_X) &&
8384 i < prev->num_members) {
d5985138
AK
8385 ord = __le32_to_cpu(prev->disk_ord_tbl[i]);
8386 idx_1 = ord_to_idx(ord);
c2a1e7da 8387
d5985138
AK
8388 disk = get_imsm_disk(super, idx_1);
8389 if (!disk || is_failed(disk) || ord & IMSM_ORD_REBUILD)
8390 failed++;
8391 }
089f9d79
JS
8392 if ((look_in_map == MAP_0 || look_in_map == MAP_X) &&
8393 i < map->num_members) {
d5985138
AK
8394 ord = __le32_to_cpu(map->disk_ord_tbl[i]);
8395 idx = ord_to_idx(ord);
8396
8397 if (idx != idx_1) {
8398 disk = get_imsm_disk(super, idx);
8399 if (!disk || is_failed(disk) ||
8400 ord & IMSM_ORD_REBUILD)
8401 failed++;
8402 }
8403 }
c2a1e7da
DW
8404 }
8405
8406 return failed;
845dea95
NB
8407}
8408
97b4d0e9 8409static int imsm_open_new(struct supertype *c, struct active_array *a,
60815698 8410 int inst)
97b4d0e9
DW
8411{
8412 struct intel_super *super = c->sb;
8413 struct imsm_super *mpb = super->anchor;
bbab0940 8414 struct imsm_update_prealloc_bb_mem u;
9587c373 8415
60815698
MG
8416 if (inst >= mpb->num_raid_devs) {
8417 pr_err("subarry index %d, out of range\n", inst);
97b4d0e9
DW
8418 return -ENODEV;
8419 }
8420
60815698
MG
8421 dprintf("imsm: open_new %d\n", inst);
8422 a->info.container_member = inst;
bbab0940
TM
8423
8424 u.type = update_prealloc_badblocks_mem;
8425 imsm_update_metadata_locally(c, &u, sizeof(u));
8426
97b4d0e9
DW
8427 return 0;
8428}
8429
0c046afd
DW
8430static int is_resyncing(struct imsm_dev *dev)
8431{
8432 struct imsm_map *migr_map;
8433
8434 if (!dev->vol.migr_state)
8435 return 0;
8436
1484e727
DW
8437 if (migr_type(dev) == MIGR_INIT ||
8438 migr_type(dev) == MIGR_REPAIR)
0c046afd
DW
8439 return 1;
8440
4c9bc37b
AK
8441 if (migr_type(dev) == MIGR_GEN_MIGR)
8442 return 0;
8443
238c0a71 8444 migr_map = get_imsm_map(dev, MAP_1);
0c046afd 8445
089f9d79
JS
8446 if (migr_map->map_state == IMSM_T_STATE_NORMAL &&
8447 dev->vol.migr_type != MIGR_GEN_MIGR)
0c046afd
DW
8448 return 1;
8449 else
8450 return 0;
8451}
8452
0556e1a2 8453/* return true if we recorded new information */
4c9e8c1e
TM
8454static int mark_failure(struct intel_super *super,
8455 struct imsm_dev *dev, struct imsm_disk *disk, int idx)
47ee5a45 8456{
0556e1a2
DW
8457 __u32 ord;
8458 int slot;
8459 struct imsm_map *map;
86c54047
DW
8460 char buf[MAX_RAID_SERIAL_LEN+3];
8461 unsigned int len, shift = 0;
0556e1a2
DW
8462
8463 /* new failures are always set in map[0] */
238c0a71 8464 map = get_imsm_map(dev, MAP_0);
0556e1a2
DW
8465
8466 slot = get_imsm_disk_slot(map, idx);
8467 if (slot < 0)
8468 return 0;
8469
8470 ord = __le32_to_cpu(map->disk_ord_tbl[slot]);
25ed7e59 8471 if (is_failed(disk) && (ord & IMSM_ORD_REBUILD))
0556e1a2
DW
8472 return 0;
8473
7d0c5e24
LD
8474 memcpy(buf, disk->serial, MAX_RAID_SERIAL_LEN);
8475 buf[MAX_RAID_SERIAL_LEN] = '\000';
8476 strcat(buf, ":0");
86c54047
DW
8477 if ((len = strlen(buf)) >= MAX_RAID_SERIAL_LEN)
8478 shift = len - MAX_RAID_SERIAL_LEN + 1;
167d8bb8 8479 memcpy(disk->serial, &buf[shift], len + 1 - shift);
86c54047 8480
f2f27e63 8481 disk->status |= FAILED_DISK;
0556e1a2 8482 set_imsm_ord_tbl_ent(map, slot, idx | IMSM_ORD_REBUILD);
17788994
AK
8483 /* mark failures in second map if second map exists and this disk
8484 * in this slot.
8485 * This is valid for migration, initialization and rebuild
8486 */
8487 if (dev->vol.migr_state) {
238c0a71 8488 struct imsm_map *map2 = get_imsm_map(dev, MAP_1);
0a108d63
AK
8489 int slot2 = get_imsm_disk_slot(map2, idx);
8490
089f9d79 8491 if (slot2 < map2->num_members && slot2 >= 0)
0a108d63 8492 set_imsm_ord_tbl_ent(map2, slot2,
1ace8403
AK
8493 idx | IMSM_ORD_REBUILD);
8494 }
d7a1fda2
MT
8495 if (map->failed_disk_num == 0xff ||
8496 (!is_rebuilding(dev) && map->failed_disk_num > slot))
0556e1a2 8497 map->failed_disk_num = slot;
4c9e8c1e
TM
8498
8499 clear_disk_badblocks(super->bbm_log, ord_to_idx(ord));
8500
0556e1a2
DW
8501 return 1;
8502}
8503
4c9e8c1e
TM
8504static void mark_missing(struct intel_super *super,
8505 struct imsm_dev *dev, struct imsm_disk *disk, int idx)
0556e1a2 8506{
4c9e8c1e 8507 mark_failure(super, dev, disk, idx);
0556e1a2
DW
8508
8509 if (disk->scsi_id == __cpu_to_le32(~(__u32)0))
8510 return;
8511
47ee5a45
DW
8512 disk->scsi_id = __cpu_to_le32(~(__u32)0);
8513 memmove(&disk->serial[0], &disk->serial[1], MAX_RAID_SERIAL_LEN - 1);
8514}
8515
33414a01
DW
8516static void handle_missing(struct intel_super *super, struct imsm_dev *dev)
8517{
33414a01 8518 struct dl *dl;
33414a01
DW
8519
8520 if (!super->missing)
8521 return;
33414a01 8522
79b68f1b
PC
8523 /* When orom adds replacement for missing disk it does
8524 * not remove entry of missing disk, but just updates map with
8525 * new added disk. So it is not enough just to test if there is
8526 * any missing disk, we have to look if there are any failed disks
8527 * in map to stop migration */
8528
33414a01 8529 dprintf("imsm: mark missing\n");
3d59f0c0
AK
8530 /* end process for initialization and rebuild only
8531 */
195d1d76 8532 if (is_gen_migration(dev) == false) {
fb12a745 8533 int failed = imsm_count_failed(super, dev, MAP_0);
3d59f0c0 8534
fb12a745
TM
8535 if (failed) {
8536 __u8 map_state;
8537 struct imsm_map *map = get_imsm_map(dev, MAP_0);
8538 struct imsm_map *map1;
8539 int i, ord, ord_map1;
8540 int rebuilt = 1;
3d59f0c0 8541
fb12a745
TM
8542 for (i = 0; i < map->num_members; i++) {
8543 ord = get_imsm_ord_tbl_ent(dev, i, MAP_0);
8544 if (!(ord & IMSM_ORD_REBUILD))
8545 continue;
8546
8547 map1 = get_imsm_map(dev, MAP_1);
8548 if (!map1)
8549 continue;
8550
8551 ord_map1 = __le32_to_cpu(map1->disk_ord_tbl[i]);
8552 if (ord_map1 & IMSM_ORD_REBUILD)
8553 rebuilt = 0;
8554 }
8555
8556 if (rebuilt) {
8557 map_state = imsm_check_degraded(super, dev,
8558 failed, MAP_0);
8559 end_migration(dev, super, map_state);
8560 }
8561 }
3d59f0c0 8562 }
33414a01 8563 for (dl = super->missing; dl; dl = dl->next)
4c9e8c1e 8564 mark_missing(super, dev, &dl->disk, dl->index);
33414a01
DW
8565 super->updates_pending++;
8566}
8567
f3871fdc
AK
8568static unsigned long long imsm_set_array_size(struct imsm_dev *dev,
8569 long long new_size)
70bdf0dc 8570{
70bdf0dc 8571 unsigned long long array_blocks;
9529d343
MD
8572 struct imsm_map *map = get_imsm_map(dev, MAP_0);
8573 int used_disks = imsm_num_data_members(map);
70bdf0dc
AK
8574
8575 if (used_disks == 0) {
8576 /* when problems occures
8577 * return current array_blocks value
8578 */
fcc2c9da 8579 array_blocks = imsm_dev_size(dev);
70bdf0dc
AK
8580
8581 return array_blocks;
8582 }
8583
8584 /* set array size in metadata
8585 */
9529d343 8586 if (new_size <= 0)
f3871fdc
AK
8587 /* OLCE size change is caused by added disks
8588 */
44490938 8589 array_blocks = per_dev_array_size(map) * used_disks;
9529d343 8590 else
f3871fdc
AK
8591 /* Online Volume Size Change
8592 * Using available free space
8593 */
8594 array_blocks = new_size;
70bdf0dc 8595
b53bfba6 8596 array_blocks = round_size_to_mb(array_blocks, used_disks);
fcc2c9da 8597 set_imsm_dev_size(dev, array_blocks);
70bdf0dc
AK
8598
8599 return array_blocks;
8600}
8601
28bce06f
AK
8602static void imsm_set_disk(struct active_array *a, int n, int state);
8603
0e2d1a4e
AK
8604static void imsm_progress_container_reshape(struct intel_super *super)
8605{
8606 /* if no device has a migr_state, but some device has a
8607 * different number of members than the previous device, start
8608 * changing the number of devices in this device to match
8609 * previous.
8610 */
8611 struct imsm_super *mpb = super->anchor;
8612 int prev_disks = -1;
8613 int i;
1dfaa380 8614 int copy_map_size;
0e2d1a4e
AK
8615
8616 for (i = 0; i < mpb->num_raid_devs; i++) {
8617 struct imsm_dev *dev = get_imsm_dev(super, i);
238c0a71 8618 struct imsm_map *map = get_imsm_map(dev, MAP_0);
0e2d1a4e
AK
8619 struct imsm_map *map2;
8620 int prev_num_members;
0e2d1a4e
AK
8621
8622 if (dev->vol.migr_state)
8623 return;
8624
8625 if (prev_disks == -1)
8626 prev_disks = map->num_members;
8627 if (prev_disks == map->num_members)
8628 continue;
8629
8630 /* OK, this array needs to enter reshape mode.
8631 * i.e it needs a migr_state
8632 */
8633
1dfaa380 8634 copy_map_size = sizeof_imsm_map(map);
0e2d1a4e
AK
8635 prev_num_members = map->num_members;
8636 map->num_members = prev_disks;
8637 dev->vol.migr_state = 1;
4036e7ee 8638 set_vol_curr_migr_unit(dev, 0);
ea672ee1 8639 set_migr_type(dev, MIGR_GEN_MIGR);
0e2d1a4e
AK
8640 for (i = prev_num_members;
8641 i < map->num_members; i++)
8642 set_imsm_ord_tbl_ent(map, i, i);
238c0a71 8643 map2 = get_imsm_map(dev, MAP_1);
0e2d1a4e 8644 /* Copy the current map */
1dfaa380 8645 memcpy(map2, map, copy_map_size);
0e2d1a4e
AK
8646 map2->num_members = prev_num_members;
8647
f3871fdc 8648 imsm_set_array_size(dev, -1);
51d83f5d 8649 super->clean_migration_record_by_mdmon = 1;
0e2d1a4e
AK
8650 super->updates_pending++;
8651 }
8652}
8653
aad6f216 8654/* Handle dirty -> clean transititions, resync and reshape. Degraded and rebuild
0c046afd
DW
8655 * states are handled in imsm_set_disk() with one exception, when a
8656 * resync is stopped due to a new failure this routine will set the
8657 * 'degraded' state for the array.
8658 */
01f157d7 8659static int imsm_set_array_state(struct active_array *a, int consistent)
a862209d
DW
8660{
8661 int inst = a->info.container_member;
8662 struct intel_super *super = a->container->sb;
949c47a0 8663 struct imsm_dev *dev = get_imsm_dev(super, inst);
238c0a71 8664 struct imsm_map *map = get_imsm_map(dev, MAP_0);
3b451610
AK
8665 int failed = imsm_count_failed(super, dev, MAP_0);
8666 __u8 map_state = imsm_check_degraded(super, dev, failed, MAP_0);
1e5c6983 8667 __u32 blocks_per_unit;
a862209d 8668
1af97990
AK
8669 if (dev->vol.migr_state &&
8670 dev->vol.migr_type == MIGR_GEN_MIGR) {
8671 /* array state change is blocked due to reshape action
aad6f216
N
8672 * We might need to
8673 * - abort the reshape (if last_checkpoint is 0 and action!= reshape)
8674 * - finish the reshape (if last_checkpoint is big and action != reshape)
4036e7ee 8675 * - update vol_curr_migr_unit
1af97990 8676 */
aad6f216 8677 if (a->curr_action == reshape) {
4036e7ee 8678 /* still reshaping, maybe update vol_curr_migr_unit */
633b5610 8679 goto mark_checkpoint;
aad6f216
N
8680 } else {
8681 if (a->last_checkpoint == 0 && a->prev_action == reshape) {
8682 /* for some reason we aborted the reshape.
b66e591b
AK
8683 *
8684 * disable automatic metadata rollback
8685 * user action is required to recover process
aad6f216 8686 */
b66e591b 8687 if (0) {
238c0a71
AK
8688 struct imsm_map *map2 =
8689 get_imsm_map(dev, MAP_1);
8690 dev->vol.migr_state = 0;
8691 set_migr_type(dev, 0);
4036e7ee 8692 set_vol_curr_migr_unit(dev, 0);
238c0a71
AK
8693 memcpy(map, map2,
8694 sizeof_imsm_map(map2));
8695 super->updates_pending++;
b66e591b 8696 }
aad6f216
N
8697 }
8698 if (a->last_checkpoint >= a->info.component_size) {
8699 unsigned long long array_blocks;
8700 int used_disks;
e154ced3 8701 struct mdinfo *mdi;
aad6f216 8702
9529d343 8703 used_disks = imsm_num_data_members(map);
d55adef9
AK
8704 if (used_disks > 0) {
8705 array_blocks =
44490938 8706 per_dev_array_size(map) *
d55adef9 8707 used_disks;
b53bfba6
TM
8708 array_blocks =
8709 round_size_to_mb(array_blocks,
8710 used_disks);
d55adef9
AK
8711 a->info.custom_array_size = array_blocks;
8712 /* encourage manager to update array
8713 * size
8714 */
e154ced3 8715
d55adef9 8716 a->check_reshape = 1;
633b5610 8717 }
e154ced3
AK
8718 /* finalize online capacity expansion/reshape */
8719 for (mdi = a->info.devs; mdi; mdi = mdi->next)
8720 imsm_set_disk(a,
8721 mdi->disk.raid_disk,
8722 mdi->curr_state);
8723
0e2d1a4e 8724 imsm_progress_container_reshape(super);
e154ced3 8725 }
aad6f216 8726 }
1af97990
AK
8727 }
8728
47ee5a45 8729 /* before we activate this array handle any missing disks */
33414a01
DW
8730 if (consistent == 2)
8731 handle_missing(super, dev);
1e5c6983 8732
0c046afd 8733 if (consistent == 2 &&
b7941fd6 8734 (!is_resync_complete(&a->info) ||
0c046afd
DW
8735 map_state != IMSM_T_STATE_NORMAL ||
8736 dev->vol.migr_state))
01f157d7 8737 consistent = 0;
272906ef 8738
b7941fd6 8739 if (is_resync_complete(&a->info)) {
0c046afd 8740 /* complete intialization / resync,
0556e1a2
DW
8741 * recovery and interrupted recovery is completed in
8742 * ->set_disk
0c046afd
DW
8743 */
8744 if (is_resyncing(dev)) {
8745 dprintf("imsm: mark resync done\n");
809da78e 8746 end_migration(dev, super, map_state);
115c3803 8747 super->updates_pending++;
484240d8 8748 a->last_checkpoint = 0;
115c3803 8749 }
b9172665
AK
8750 } else if ((!is_resyncing(dev) && !failed) &&
8751 (imsm_reshape_blocks_arrays_changes(super) == 0)) {
0c046afd 8752 /* mark the start of the init process if nothing is failed */
b7941fd6 8753 dprintf("imsm: mark resync start\n");
1484e727 8754 if (map->map_state == IMSM_T_STATE_UNINITIALIZED)
8e59f3d8 8755 migrate(dev, super, IMSM_T_STATE_NORMAL, MIGR_INIT);
1484e727 8756 else
8e59f3d8 8757 migrate(dev, super, IMSM_T_STATE_NORMAL, MIGR_REPAIR);
3393c6af 8758 super->updates_pending++;
115c3803 8759 }
a862209d 8760
633b5610 8761mark_checkpoint:
5b83bacf
AK
8762 /* skip checkpointing for general migration,
8763 * it is controlled in mdadm
8764 */
8765 if (is_gen_migration(dev))
8766 goto skip_mark_checkpoint;
8767
4036e7ee
MT
8768 /* check if we can update vol_curr_migr_unit from resync_start,
8769 * recovery_start
8770 */
c47b0ff6 8771 blocks_per_unit = blocks_per_migr_unit(super, dev);
4f0a7acc 8772 if (blocks_per_unit) {
4036e7ee
MT
8773 set_vol_curr_migr_unit(dev,
8774 a->last_checkpoint / blocks_per_unit);
8775 dprintf("imsm: mark checkpoint (%llu)\n",
8776 vol_curr_migr_unit(dev));
8777 super->updates_pending++;
1e5c6983 8778 }
f8f603f1 8779
5b83bacf 8780skip_mark_checkpoint:
3393c6af 8781 /* mark dirty / clean */
2432ce9b
AP
8782 if (((dev->vol.dirty & RAIDVOL_DIRTY) && consistent) ||
8783 (!(dev->vol.dirty & RAIDVOL_DIRTY) && !consistent)) {
b7941fd6 8784 dprintf("imsm: mark '%s'\n", consistent ? "clean" : "dirty");
2432ce9b
AP
8785 if (consistent) {
8786 dev->vol.dirty = RAIDVOL_CLEAN;
8787 } else {
8788 dev->vol.dirty = RAIDVOL_DIRTY;
c2462068
PB
8789 if (dev->rwh_policy == RWH_DISTRIBUTED ||
8790 dev->rwh_policy == RWH_MULTIPLE_DISTRIBUTED)
2432ce9b
AP
8791 dev->vol.dirty |= RAIDVOL_DSRECORD_VALID;
8792 }
a862209d
DW
8793 super->updates_pending++;
8794 }
28bce06f 8795
01f157d7 8796 return consistent;
a862209d
DW
8797}
8798
6f50473f
TM
8799static int imsm_disk_slot_to_ord(struct active_array *a, int slot)
8800{
8801 int inst = a->info.container_member;
8802 struct intel_super *super = a->container->sb;
8803 struct imsm_dev *dev = get_imsm_dev(super, inst);
8804 struct imsm_map *map = get_imsm_map(dev, MAP_0);
8805
8806 if (slot > map->num_members) {
8807 pr_err("imsm: imsm_disk_slot_to_ord %d out of range 0..%d\n",
8808 slot, map->num_members - 1);
8809 return -1;
8810 }
8811
8812 if (slot < 0)
8813 return -1;
8814
8815 return get_imsm_ord_tbl_ent(dev, slot, MAP_0);
8816}
8817
8d45d196 8818static void imsm_set_disk(struct active_array *a, int n, int state)
845dea95 8819{
8d45d196
DW
8820 int inst = a->info.container_member;
8821 struct intel_super *super = a->container->sb;
949c47a0 8822 struct imsm_dev *dev = get_imsm_dev(super, inst);
238c0a71 8823 struct imsm_map *map = get_imsm_map(dev, MAP_0);
8d45d196 8824 struct imsm_disk *disk;
7ce05701
LD
8825 struct mdinfo *mdi;
8826 int recovery_not_finished = 0;
0c046afd 8827 int failed;
6f50473f 8828 int ord;
0c046afd 8829 __u8 map_state;
fb12a745
TM
8830 int rebuild_done = 0;
8831 int i;
8d45d196 8832
fb12a745 8833 ord = get_imsm_ord_tbl_ent(dev, n, MAP_X);
6f50473f 8834 if (ord < 0)
8d45d196
DW
8835 return;
8836
4e6e574a 8837 dprintf("imsm: set_disk %d:%x\n", n, state);
b10b37b8 8838 disk = get_imsm_disk(super, ord_to_idx(ord));
8d45d196 8839
5802a811 8840 /* check for new failures */
ae7d61e3 8841 if (disk && (state & DS_FAULTY)) {
4c9e8c1e 8842 if (mark_failure(super, dev, disk, ord_to_idx(ord)))
0556e1a2 8843 super->updates_pending++;
8d45d196 8844 }
47ee5a45 8845
19859edc 8846 /* check if in_sync */
0556e1a2 8847 if (state & DS_INSYNC && ord & IMSM_ORD_REBUILD && is_rebuilding(dev)) {
238c0a71 8848 struct imsm_map *migr_map = get_imsm_map(dev, MAP_1);
b10b37b8
DW
8849
8850 set_imsm_ord_tbl_ent(migr_map, n, ord_to_idx(ord));
fb12a745 8851 rebuild_done = 1;
19859edc
DW
8852 super->updates_pending++;
8853 }
8d45d196 8854
3b451610
AK
8855 failed = imsm_count_failed(super, dev, MAP_0);
8856 map_state = imsm_check_degraded(super, dev, failed, MAP_0);
5802a811 8857
0c046afd 8858 /* check if recovery complete, newly degraded, or failed */
94002678
AK
8859 dprintf("imsm: Detected transition to state ");
8860 switch (map_state) {
8861 case IMSM_T_STATE_NORMAL: /* transition to normal state */
8862 dprintf("normal: ");
8863 if (is_rebuilding(dev)) {
1ade5cc1 8864 dprintf_cont("while rebuilding");
7ce05701
LD
8865 /* check if recovery is really finished */
8866 for (mdi = a->info.devs; mdi ; mdi = mdi->next)
8867 if (mdi->recovery_start != MaxSector) {
8868 recovery_not_finished = 1;
8869 break;
8870 }
8871 if (recovery_not_finished) {
1ade5cc1
N
8872 dprintf_cont("\n");
8873 dprintf("Rebuild has not finished yet, state not changed");
7ce05701
LD
8874 if (a->last_checkpoint < mdi->recovery_start) {
8875 a->last_checkpoint = mdi->recovery_start;
8876 super->updates_pending++;
8877 }
8878 break;
8879 }
94002678 8880 end_migration(dev, super, map_state);
94002678
AK
8881 map->failed_disk_num = ~0;
8882 super->updates_pending++;
8883 a->last_checkpoint = 0;
8884 break;
8885 }
8886 if (is_gen_migration(dev)) {
1ade5cc1 8887 dprintf_cont("while general migration");
bf2f0071 8888 if (a->last_checkpoint >= a->info.component_size)
809da78e 8889 end_migration(dev, super, map_state);
94002678
AK
8890 else
8891 map->map_state = map_state;
28bce06f 8892 map->failed_disk_num = ~0;
94002678 8893 super->updates_pending++;
bf2f0071 8894 break;
94002678
AK
8895 }
8896 break;
8897 case IMSM_T_STATE_DEGRADED: /* transition to degraded state */
1ade5cc1 8898 dprintf_cont("degraded: ");
089f9d79 8899 if (map->map_state != map_state && !dev->vol.migr_state) {
1ade5cc1 8900 dprintf_cont("mark degraded");
94002678
AK
8901 map->map_state = map_state;
8902 super->updates_pending++;
8903 a->last_checkpoint = 0;
8904 break;
8905 }
8906 if (is_rebuilding(dev)) {
d7a1fda2 8907 dprintf_cont("while rebuilding ");
a4e96fd8
MT
8908 if (state & DS_FAULTY) {
8909 dprintf_cont("removing failed drive ");
d7a1fda2
MT
8910 if (n == map->failed_disk_num) {
8911 dprintf_cont("end migration");
8912 end_migration(dev, super, map_state);
a4e96fd8 8913 a->last_checkpoint = 0;
d7a1fda2 8914 } else {
a4e96fd8 8915 dprintf_cont("fail detected during rebuild, changing map state");
d7a1fda2
MT
8916 map->map_state = map_state;
8917 }
94002678 8918 super->updates_pending++;
fb12a745
TM
8919 }
8920
a4e96fd8
MT
8921 if (!rebuild_done)
8922 break;
8923
fb12a745
TM
8924 /* check if recovery is really finished */
8925 for (mdi = a->info.devs; mdi ; mdi = mdi->next)
8926 if (mdi->recovery_start != MaxSector) {
8927 recovery_not_finished = 1;
8928 break;
8929 }
8930 if (recovery_not_finished) {
8931 dprintf_cont("\n");
a4e96fd8 8932 dprintf_cont("Rebuild has not finished yet");
fb12a745
TM
8933 if (a->last_checkpoint < mdi->recovery_start) {
8934 a->last_checkpoint =
8935 mdi->recovery_start;
8936 super->updates_pending++;
8937 }
8938 break;
94002678 8939 }
fb12a745
TM
8940
8941 dprintf_cont(" Rebuild done, still degraded");
a4e96fd8
MT
8942 end_migration(dev, super, map_state);
8943 a->last_checkpoint = 0;
8944 super->updates_pending++;
fb12a745
TM
8945
8946 for (i = 0; i < map->num_members; i++) {
8947 int idx = get_imsm_ord_tbl_ent(dev, i, MAP_0);
8948
8949 if (idx & IMSM_ORD_REBUILD)
8950 map->failed_disk_num = i;
8951 }
8952 super->updates_pending++;
94002678
AK
8953 break;
8954 }
8955 if (is_gen_migration(dev)) {
1ade5cc1 8956 dprintf_cont("while general migration");
bf2f0071 8957 if (a->last_checkpoint >= a->info.component_size)
809da78e 8958 end_migration(dev, super, map_state);
94002678
AK
8959 else {
8960 map->map_state = map_state;
3b451610 8961 manage_second_map(super, dev);
94002678
AK
8962 }
8963 super->updates_pending++;
bf2f0071 8964 break;
28bce06f 8965 }
6ce1fbf1 8966 if (is_initializing(dev)) {
1ade5cc1 8967 dprintf_cont("while initialization.");
6ce1fbf1
AK
8968 map->map_state = map_state;
8969 super->updates_pending++;
8970 break;
8971 }
94002678
AK
8972 break;
8973 case IMSM_T_STATE_FAILED: /* transition to failed state */
1ade5cc1 8974 dprintf_cont("failed: ");
94002678 8975 if (is_gen_migration(dev)) {
1ade5cc1 8976 dprintf_cont("while general migration");
94002678
AK
8977 map->map_state = map_state;
8978 super->updates_pending++;
8979 break;
8980 }
8981 if (map->map_state != map_state) {
1ade5cc1 8982 dprintf_cont("mark failed");
94002678
AK
8983 end_migration(dev, super, map_state);
8984 super->updates_pending++;
8985 a->last_checkpoint = 0;
8986 break;
8987 }
8988 break;
8989 default:
1ade5cc1 8990 dprintf_cont("state %i\n", map_state);
5802a811 8991 }
1ade5cc1 8992 dprintf_cont("\n");
845dea95
NB
8993}
8994
f796af5d 8995static int store_imsm_mpb(int fd, struct imsm_super *mpb)
c2a1e7da 8996{
f796af5d 8997 void *buf = mpb;
c2a1e7da
DW
8998 __u32 mpb_size = __le32_to_cpu(mpb->mpb_size);
8999 unsigned long long dsize;
9000 unsigned long long sectors;
f36a9ecd 9001 unsigned int sector_size;
c2a1e7da 9002
aec01630
JS
9003 if (!get_dev_sector_size(fd, NULL, &sector_size))
9004 return 1;
c2a1e7da
DW
9005 get_dev_size(fd, NULL, &dsize);
9006
f36a9ecd 9007 if (mpb_size > sector_size) {
272f648f 9008 /* -1 to account for anchor */
f36a9ecd 9009 sectors = mpb_sectors(mpb, sector_size) - 1;
c2a1e7da 9010
272f648f 9011 /* write the extended mpb to the sectors preceeding the anchor */
f36a9ecd
PB
9012 if (lseek64(fd, dsize - (sector_size * (2 + sectors)),
9013 SEEK_SET) < 0)
272f648f 9014 return 1;
c2a1e7da 9015
f36a9ecd
PB
9016 if ((unsigned long long)write(fd, buf + sector_size,
9017 sector_size * sectors) != sector_size * sectors)
272f648f
DW
9018 return 1;
9019 }
c2a1e7da 9020
272f648f 9021 /* first block is stored on second to last sector of the disk */
f36a9ecd 9022 if (lseek64(fd, dsize - (sector_size * 2), SEEK_SET) < 0)
c2a1e7da
DW
9023 return 1;
9024
466070ad 9025 if ((unsigned int)write(fd, buf, sector_size) != sector_size)
c2a1e7da
DW
9026 return 1;
9027
c2a1e7da
DW
9028 return 0;
9029}
9030
2e735d19 9031static void imsm_sync_metadata(struct supertype *container)
845dea95 9032{
2e735d19 9033 struct intel_super *super = container->sb;
c2a1e7da 9034
1a64be56 9035 dprintf("sync metadata: %d\n", super->updates_pending);
c2a1e7da
DW
9036 if (!super->updates_pending)
9037 return;
9038
36988a3d 9039 write_super_imsm(container, 0);
c2a1e7da
DW
9040
9041 super->updates_pending = 0;
845dea95
NB
9042}
9043
272906ef
DW
9044static struct dl *imsm_readd(struct intel_super *super, int idx, struct active_array *a)
9045{
9046 struct imsm_dev *dev = get_imsm_dev(super, a->info.container_member);
238c0a71 9047 int i = get_imsm_disk_idx(dev, idx, MAP_X);
272906ef
DW
9048 struct dl *dl;
9049
9050 for (dl = super->disks; dl; dl = dl->next)
9051 if (dl->index == i)
9052 break;
9053
25ed7e59 9054 if (dl && is_failed(&dl->disk))
272906ef
DW
9055 dl = NULL;
9056
9057 if (dl)
1ade5cc1 9058 dprintf("found %x:%x\n", dl->major, dl->minor);
272906ef
DW
9059
9060 return dl;
9061}
9062
a20d2ba5 9063static struct dl *imsm_add_spare(struct intel_super *super, int slot,
8ba77d32
AK
9064 struct active_array *a, int activate_new,
9065 struct mdinfo *additional_test_list)
272906ef
DW
9066{
9067 struct imsm_dev *dev = get_imsm_dev(super, a->info.container_member);
238c0a71 9068 int idx = get_imsm_disk_idx(dev, slot, MAP_X);
a20d2ba5
DW
9069 struct imsm_super *mpb = super->anchor;
9070 struct imsm_map *map;
272906ef
DW
9071 unsigned long long pos;
9072 struct mdinfo *d;
9073 struct extent *ex;
a20d2ba5 9074 int i, j;
272906ef 9075 int found;
569cc43f
DW
9076 __u32 array_start = 0;
9077 __u32 array_end = 0;
272906ef 9078 struct dl *dl;
6c932028 9079 struct mdinfo *test_list;
272906ef
DW
9080
9081 for (dl = super->disks; dl; dl = dl->next) {
9082 /* If in this array, skip */
9083 for (d = a->info.devs ; d ; d = d->next)
4389ce73 9084 if (is_fd_valid(d->state_fd) &&
e553d2a4 9085 d->disk.major == dl->major &&
272906ef 9086 d->disk.minor == dl->minor) {
8ba77d32
AK
9087 dprintf("%x:%x already in array\n",
9088 dl->major, dl->minor);
272906ef
DW
9089 break;
9090 }
9091 if (d)
9092 continue;
6c932028
AK
9093 test_list = additional_test_list;
9094 while (test_list) {
9095 if (test_list->disk.major == dl->major &&
9096 test_list->disk.minor == dl->minor) {
8ba77d32
AK
9097 dprintf("%x:%x already in additional test list\n",
9098 dl->major, dl->minor);
9099 break;
9100 }
6c932028 9101 test_list = test_list->next;
8ba77d32 9102 }
6c932028 9103 if (test_list)
8ba77d32 9104 continue;
272906ef 9105
e553d2a4 9106 /* skip in use or failed drives */
25ed7e59 9107 if (is_failed(&dl->disk) || idx == dl->index ||
df474657
DW
9108 dl->index == -2) {
9109 dprintf("%x:%x status (failed: %d index: %d)\n",
25ed7e59 9110 dl->major, dl->minor, is_failed(&dl->disk), idx);
9a1608e5
DW
9111 continue;
9112 }
9113
a20d2ba5
DW
9114 /* skip pure spares when we are looking for partially
9115 * assimilated drives
9116 */
9117 if (dl->index == -1 && !activate_new)
9118 continue;
9119
f2cc4f7d
AO
9120 if (!drive_validate_sector_size(super, dl))
9121 continue;
9122
272906ef 9123 /* Does this unused device have the requisite free space?
a20d2ba5 9124 * It needs to be able to cover all member volumes
272906ef 9125 */
05501181 9126 ex = get_extents(super, dl, 1);
272906ef
DW
9127 if (!ex) {
9128 dprintf("cannot get extents\n");
9129 continue;
9130 }
a20d2ba5
DW
9131 for (i = 0; i < mpb->num_raid_devs; i++) {
9132 dev = get_imsm_dev(super, i);
238c0a71 9133 map = get_imsm_map(dev, MAP_0);
272906ef 9134
a20d2ba5
DW
9135 /* check if this disk is already a member of
9136 * this array
272906ef 9137 */
620b1713 9138 if (get_imsm_disk_slot(map, dl->index) >= 0)
a20d2ba5
DW
9139 continue;
9140
9141 found = 0;
9142 j = 0;
9143 pos = 0;
5551b113 9144 array_start = pba_of_lba0(map);
329c8278 9145 array_end = array_start +
44490938 9146 per_dev_array_size(map) - 1;
a20d2ba5
DW
9147
9148 do {
9149 /* check that we can start at pba_of_lba0 with
44490938 9150 * num_data_stripes*blocks_per_stripe of space
a20d2ba5 9151 */
329c8278 9152 if (array_start >= pos && array_end < ex[j].start) {
a20d2ba5
DW
9153 found = 1;
9154 break;
9155 }
9156 pos = ex[j].start + ex[j].size;
9157 j++;
9158 } while (ex[j-1].size);
9159
9160 if (!found)
272906ef 9161 break;
a20d2ba5 9162 }
272906ef
DW
9163
9164 free(ex);
a20d2ba5 9165 if (i < mpb->num_raid_devs) {
329c8278
DW
9166 dprintf("%x:%x does not have %u to %u available\n",
9167 dl->major, dl->minor, array_start, array_end);
272906ef
DW
9168 /* No room */
9169 continue;
a20d2ba5
DW
9170 }
9171 return dl;
272906ef
DW
9172 }
9173
9174 return dl;
9175}
9176
95d07a2c
LM
9177static int imsm_rebuild_allowed(struct supertype *cont, int dev_idx, int failed)
9178{
9179 struct imsm_dev *dev2;
9180 struct imsm_map *map;
9181 struct dl *idisk;
9182 int slot;
9183 int idx;
9184 __u8 state;
9185
9186 dev2 = get_imsm_dev(cont->sb, dev_idx);
756a15f3
MG
9187
9188 state = imsm_check_degraded(cont->sb, dev2, failed, MAP_0);
9189 if (state == IMSM_T_STATE_FAILED) {
9190 map = get_imsm_map(dev2, MAP_0);
9191 for (slot = 0; slot < map->num_members; slot++) {
9192 /*
9193 * Check if failed disks are deleted from intel
9194 * disk list or are marked to be deleted
9195 */
9196 idx = get_imsm_disk_idx(dev2, slot, MAP_X);
9197 idisk = get_imsm_dl_disk(cont->sb, idx);
9198 /*
9199 * Do not rebuild the array if failed disks
9200 * from failed sub-array are not removed from
9201 * container.
9202 */
9203 if (idisk &&
9204 is_failed(&idisk->disk) &&
9205 (idisk->action != DISK_REMOVE))
9206 return 0;
95d07a2c
LM
9207 }
9208 }
9209 return 1;
9210}
9211
88758e9d
DW
9212static struct mdinfo *imsm_activate_spare(struct active_array *a,
9213 struct metadata_update **updates)
9214{
9215 /**
d23fe947
DW
9216 * Find a device with unused free space and use it to replace a
9217 * failed/vacant region in an array. We replace failed regions one a
9218 * array at a time. The result is that a new spare disk will be added
9219 * to the first failed array and after the monitor has finished
9220 * propagating failures the remainder will be consumed.
88758e9d 9221 *
d23fe947
DW
9222 * FIXME add a capability for mdmon to request spares from another
9223 * container.
88758e9d
DW
9224 */
9225
9226 struct intel_super *super = a->container->sb;
88758e9d 9227 int inst = a->info.container_member;
949c47a0 9228 struct imsm_dev *dev = get_imsm_dev(super, inst);
238c0a71 9229 struct imsm_map *map = get_imsm_map(dev, MAP_0);
88758e9d
DW
9230 int failed = a->info.array.raid_disks;
9231 struct mdinfo *rv = NULL;
9232 struct mdinfo *d;
9233 struct mdinfo *di;
9234 struct metadata_update *mu;
9235 struct dl *dl;
9236 struct imsm_update_activate_spare *u;
9237 int num_spares = 0;
9238 int i;
95d07a2c 9239 int allowed;
88758e9d 9240
4389ce73
MT
9241 for (d = a->info.devs ; d; d = d->next) {
9242 if (!is_fd_valid(d->state_fd))
9243 continue;
9244
9245 if (d->curr_state & DS_FAULTY)
88758e9d
DW
9246 /* wait for Removal to happen */
9247 return NULL;
4389ce73
MT
9248
9249 failed--;
88758e9d
DW
9250 }
9251
9252 dprintf("imsm: activate spare: inst=%d failed=%d (%d) level=%d\n",
9253 inst, failed, a->info.array.raid_disks, a->info.array.level);
1af97990 9254
e2962bfc
AK
9255 if (imsm_reshape_blocks_arrays_changes(super))
9256 return NULL;
1af97990 9257
fc8ca064
AK
9258 /* Cannot activate another spare if rebuild is in progress already
9259 */
9260 if (is_rebuilding(dev)) {
7a862a02 9261 dprintf("imsm: No spare activation allowed. Rebuild in progress already.\n");
fc8ca064
AK
9262 return NULL;
9263 }
9264
89c67882
AK
9265 if (a->info.array.level == 4)
9266 /* No repair for takeovered array
9267 * imsm doesn't support raid4
9268 */
9269 return NULL;
9270
3b451610
AK
9271 if (imsm_check_degraded(super, dev, failed, MAP_0) !=
9272 IMSM_T_STATE_DEGRADED)
88758e9d
DW
9273 return NULL;
9274
83ca7d45
AP
9275 if (get_imsm_map(dev, MAP_0)->map_state == IMSM_T_STATE_UNINITIALIZED) {
9276 dprintf("imsm: No spare activation allowed. Volume is not initialized.\n");
9277 return NULL;
9278 }
9279
95d07a2c
LM
9280 /*
9281 * If there are any failed disks check state of the other volume.
9282 * Block rebuild if the another one is failed until failed disks
9283 * are removed from container.
9284 */
9285 if (failed) {
7a862a02 9286 dprintf("found failed disks in %.*s, check if there anotherfailed sub-array.\n",
c4acd1e5 9287 MAX_RAID_SERIAL_LEN, dev->volume);
95d07a2c
LM
9288 /* check if states of the other volumes allow for rebuild */
9289 for (i = 0; i < super->anchor->num_raid_devs; i++) {
9290 if (i != inst) {
9291 allowed = imsm_rebuild_allowed(a->container,
9292 i, failed);
9293 if (!allowed)
9294 return NULL;
9295 }
9296 }
9297 }
9298
88758e9d 9299 /* For each slot, if it is not working, find a spare */
88758e9d
DW
9300 for (i = 0; i < a->info.array.raid_disks; i++) {
9301 for (d = a->info.devs ; d ; d = d->next)
9302 if (d->disk.raid_disk == i)
9303 break;
9304 dprintf("found %d: %p %x\n", i, d, d?d->curr_state:0);
4389ce73 9305 if (d && is_fd_valid(d->state_fd))
88758e9d
DW
9306 continue;
9307
272906ef 9308 /*
a20d2ba5
DW
9309 * OK, this device needs recovery. Try to re-add the
9310 * previous occupant of this slot, if this fails see if
9311 * we can continue the assimilation of a spare that was
9312 * partially assimilated, finally try to activate a new
9313 * spare.
272906ef
DW
9314 */
9315 dl = imsm_readd(super, i, a);
9316 if (!dl)
b303fe21 9317 dl = imsm_add_spare(super, i, a, 0, rv);
a20d2ba5 9318 if (!dl)
b303fe21 9319 dl = imsm_add_spare(super, i, a, 1, rv);
272906ef
DW
9320 if (!dl)
9321 continue;
1011e834 9322
272906ef 9323 /* found a usable disk with enough space */
503975b9 9324 di = xcalloc(1, sizeof(*di));
272906ef
DW
9325
9326 /* dl->index will be -1 in the case we are activating a
9327 * pristine spare. imsm_process_update() will create a
9328 * new index in this case. Once a disk is found to be
9329 * failed in all member arrays it is kicked from the
9330 * metadata
9331 */
9332 di->disk.number = dl->index;
d23fe947 9333
272906ef
DW
9334 /* (ab)use di->devs to store a pointer to the device
9335 * we chose
9336 */
9337 di->devs = (struct mdinfo *) dl;
9338
9339 di->disk.raid_disk = i;
9340 di->disk.major = dl->major;
9341 di->disk.minor = dl->minor;
9342 di->disk.state = 0;
d23534e4 9343 di->recovery_start = 0;
5551b113 9344 di->data_offset = pba_of_lba0(map);
272906ef
DW
9345 di->component_size = a->info.component_size;
9346 di->container_member = inst;
5e46202e 9347 di->bb.supported = 1;
2c8890e9 9348 if (a->info.consistency_policy == CONSISTENCY_POLICY_PPL) {
2432ce9b 9349 di->ppl_sector = get_ppl_sector(super, inst);
c2462068 9350 di->ppl_size = MULTIPLE_PPL_AREA_SIZE_IMSM >> 9;
2432ce9b 9351 }
148acb7b 9352 super->random = random32();
272906ef
DW
9353 di->next = rv;
9354 rv = di;
9355 num_spares++;
9356 dprintf("%x:%x to be %d at %llu\n", dl->major, dl->minor,
9357 i, di->data_offset);
88758e9d
DW
9358 }
9359
9360 if (!rv)
9361 /* No spares found */
9362 return rv;
9363 /* Now 'rv' has a list of devices to return.
9364 * Create a metadata_update record to update the
9365 * disk_ord_tbl for the array
9366 */
503975b9 9367 mu = xmalloc(sizeof(*mu));
1011e834 9368 mu->buf = xcalloc(num_spares,
503975b9 9369 sizeof(struct imsm_update_activate_spare));
88758e9d 9370 mu->space = NULL;
cb23f1f4 9371 mu->space_list = NULL;
88758e9d
DW
9372 mu->len = sizeof(struct imsm_update_activate_spare) * num_spares;
9373 mu->next = *updates;
9374 u = (struct imsm_update_activate_spare *) mu->buf;
9375
9376 for (di = rv ; di ; di = di->next) {
9377 u->type = update_activate_spare;
d23fe947
DW
9378 u->dl = (struct dl *) di->devs;
9379 di->devs = NULL;
88758e9d
DW
9380 u->slot = di->disk.raid_disk;
9381 u->array = inst;
9382 u->next = u + 1;
9383 u++;
9384 }
9385 (u-1)->next = NULL;
9386 *updates = mu;
9387
9388 return rv;
9389}
9390
54c2c1ea 9391static int disks_overlap(struct intel_super *super, int idx, struct imsm_update_create_array *u)
8273f55e 9392{
54c2c1ea 9393 struct imsm_dev *dev = get_imsm_dev(super, idx);
238c0a71
AK
9394 struct imsm_map *map = get_imsm_map(dev, MAP_0);
9395 struct imsm_map *new_map = get_imsm_map(&u->dev, MAP_0);
54c2c1ea
DW
9396 struct disk_info *inf = get_disk_info(u);
9397 struct imsm_disk *disk;
8273f55e
DW
9398 int i;
9399 int j;
8273f55e 9400
54c2c1ea 9401 for (i = 0; i < map->num_members; i++) {
238c0a71 9402 disk = get_imsm_disk(super, get_imsm_disk_idx(dev, i, MAP_X));
54c2c1ea
DW
9403 for (j = 0; j < new_map->num_members; j++)
9404 if (serialcmp(disk->serial, inf[j].serial) == 0)
8273f55e
DW
9405 return 1;
9406 }
9407
9408 return 0;
9409}
9410
1a64be56
LM
9411static struct dl *get_disk_super(struct intel_super *super, int major, int minor)
9412{
594dc1b8
JS
9413 struct dl *dl;
9414
1a64be56 9415 for (dl = super->disks; dl; dl = dl->next)
089f9d79 9416 if (dl->major == major && dl->minor == minor)
1a64be56
LM
9417 return dl;
9418 return NULL;
9419}
9420
9421static int remove_disk_super(struct intel_super *super, int major, int minor)
9422{
594dc1b8 9423 struct dl *prev;
1a64be56
LM
9424 struct dl *dl;
9425
9426 prev = NULL;
9427 for (dl = super->disks; dl; dl = dl->next) {
089f9d79 9428 if (dl->major == major && dl->minor == minor) {
1a64be56
LM
9429 /* remove */
9430 if (prev)
9431 prev->next = dl->next;
9432 else
9433 super->disks = dl->next;
9434 dl->next = NULL;
3a85bf0e 9435 __free_imsm_disk(dl, 1);
1ade5cc1 9436 dprintf("removed %x:%x\n", major, minor);
1a64be56
LM
9437 break;
9438 }
9439 prev = dl;
9440 }
9441 return 0;
9442}
9443
f21e18ca 9444static void imsm_delete(struct intel_super *super, struct dl **dlp, unsigned index);
ae6aad82 9445
1a64be56
LM
9446static int add_remove_disk_update(struct intel_super *super)
9447{
9448 int check_degraded = 0;
594dc1b8
JS
9449 struct dl *disk;
9450
1a64be56
LM
9451 /* add/remove some spares to/from the metadata/contrainer */
9452 while (super->disk_mgmt_list) {
9453 struct dl *disk_cfg;
9454
9455 disk_cfg = super->disk_mgmt_list;
9456 super->disk_mgmt_list = disk_cfg->next;
9457 disk_cfg->next = NULL;
9458
9459 if (disk_cfg->action == DISK_ADD) {
9460 disk_cfg->next = super->disks;
9461 super->disks = disk_cfg;
9462 check_degraded = 1;
1ade5cc1
N
9463 dprintf("added %x:%x\n",
9464 disk_cfg->major, disk_cfg->minor);
1a64be56
LM
9465 } else if (disk_cfg->action == DISK_REMOVE) {
9466 dprintf("Disk remove action processed: %x.%x\n",
9467 disk_cfg->major, disk_cfg->minor);
9468 disk = get_disk_super(super,
9469 disk_cfg->major,
9470 disk_cfg->minor);
9471 if (disk) {
9472 /* store action status */
9473 disk->action = DISK_REMOVE;
9474 /* remove spare disks only */
9475 if (disk->index == -1) {
9476 remove_disk_super(super,
9477 disk_cfg->major,
9478 disk_cfg->minor);
91c97c54
MT
9479 } else {
9480 disk_cfg->fd = disk->fd;
9481 disk->fd = -1;
1a64be56
LM
9482 }
9483 }
9484 /* release allocate disk structure */
3a85bf0e 9485 __free_imsm_disk(disk_cfg, 1);
1a64be56
LM
9486 }
9487 }
9488 return check_degraded;
9489}
9490
a29911da
PC
9491static int apply_reshape_migration_update(struct imsm_update_reshape_migration *u,
9492 struct intel_super *super,
9493 void ***space_list)
9494{
9495 struct intel_dev *id;
9496 void **tofree = NULL;
9497 int ret_val = 0;
9498
1ade5cc1 9499 dprintf("(enter)\n");
089f9d79 9500 if (u->subdev < 0 || u->subdev > 1) {
a29911da
PC
9501 dprintf("imsm: Error: Wrong subdev: %i\n", u->subdev);
9502 return ret_val;
9503 }
089f9d79 9504 if (space_list == NULL || *space_list == NULL) {
a29911da
PC
9505 dprintf("imsm: Error: Memory is not allocated\n");
9506 return ret_val;
9507 }
9508
9509 for (id = super->devlist ; id; id = id->next) {
9510 if (id->index == (unsigned)u->subdev) {
9511 struct imsm_dev *dev = get_imsm_dev(super, u->subdev);
9512 struct imsm_map *map;
9513 struct imsm_dev *new_dev =
9514 (struct imsm_dev *)*space_list;
238c0a71 9515 struct imsm_map *migr_map = get_imsm_map(dev, MAP_1);
a29911da
PC
9516 int to_state;
9517 struct dl *new_disk;
9518
9519 if (new_dev == NULL)
9520 return ret_val;
9521 *space_list = **space_list;
9522 memcpy(new_dev, dev, sizeof_imsm_dev(dev, 0));
238c0a71 9523 map = get_imsm_map(new_dev, MAP_0);
a29911da
PC
9524 if (migr_map) {
9525 dprintf("imsm: Error: migration in progress");
9526 return ret_val;
9527 }
9528
9529 to_state = map->map_state;
9530 if ((u->new_level == 5) && (map->raid_level == 0)) {
9531 map->num_members++;
9532 /* this should not happen */
9533 if (u->new_disks[0] < 0) {
9534 map->failed_disk_num =
9535 map->num_members - 1;
9536 to_state = IMSM_T_STATE_DEGRADED;
9537 } else
9538 to_state = IMSM_T_STATE_NORMAL;
9539 }
8e59f3d8 9540 migrate(new_dev, super, to_state, MIGR_GEN_MIGR);
a29911da
PC
9541 if (u->new_level > -1)
9542 map->raid_level = u->new_level;
238c0a71 9543 migr_map = get_imsm_map(new_dev, MAP_1);
a29911da
PC
9544 if ((u->new_level == 5) &&
9545 (migr_map->raid_level == 0)) {
9546 int ord = map->num_members - 1;
9547 migr_map->num_members--;
9548 if (u->new_disks[0] < 0)
9549 ord |= IMSM_ORD_REBUILD;
9550 set_imsm_ord_tbl_ent(map,
9551 map->num_members - 1,
9552 ord);
9553 }
9554 id->dev = new_dev;
9555 tofree = (void **)dev;
9556
4bba0439
PC
9557 /* update chunk size
9558 */
06fb291a 9559 if (u->new_chunksize > 0) {
9529d343
MD
9560 struct imsm_map *dest_map =
9561 get_imsm_map(dev, MAP_0);
06fb291a 9562 int used_disks =
9529d343 9563 imsm_num_data_members(dest_map);
06fb291a
PB
9564
9565 if (used_disks == 0)
9566 return ret_val;
9567
4bba0439
PC
9568 map->blocks_per_strip =
9569 __cpu_to_le16(u->new_chunksize * 2);
1c275381 9570 update_num_data_stripes(map, imsm_dev_size(dev));
06fb291a 9571 }
4bba0439 9572
44490938
MD
9573 /* ensure blocks_per_member has valid value
9574 */
9575 set_blocks_per_member(map,
9576 per_dev_array_size(map) +
9577 NUM_BLOCKS_DIRTY_STRIPE_REGION);
9578
a29911da
PC
9579 /* add disk
9580 */
089f9d79
JS
9581 if (u->new_level != 5 || migr_map->raid_level != 0 ||
9582 migr_map->raid_level == map->raid_level)
a29911da
PC
9583 goto skip_disk_add;
9584
9585 if (u->new_disks[0] >= 0) {
9586 /* use passes spare
9587 */
9588 new_disk = get_disk_super(super,
9589 major(u->new_disks[0]),
9590 minor(u->new_disks[0]));
7a862a02 9591 dprintf("imsm: new disk for reshape is: %i:%i (%p, index = %i)\n",
a29911da
PC
9592 major(u->new_disks[0]),
9593 minor(u->new_disks[0]),
9594 new_disk, new_disk->index);
9595 if (new_disk == NULL)
9596 goto error_disk_add;
9597
9598 new_disk->index = map->num_members - 1;
9599 /* slot to fill in autolayout
9600 */
9601 new_disk->raiddisk = new_disk->index;
9602 new_disk->disk.status |= CONFIGURED_DISK;
9603 new_disk->disk.status &= ~SPARE_DISK;
9604 } else
9605 goto error_disk_add;
9606
9607skip_disk_add:
9608 *tofree = *space_list;
9609 /* calculate new size
9610 */
f3871fdc 9611 imsm_set_array_size(new_dev, -1);
a29911da
PC
9612
9613 ret_val = 1;
9614 }
9615 }
9616
9617 if (tofree)
9618 *space_list = tofree;
9619 return ret_val;
9620
9621error_disk_add:
9622 dprintf("Error: imsm: Cannot find disk.\n");
9623 return ret_val;
9624}
9625
f3871fdc
AK
9626static int apply_size_change_update(struct imsm_update_size_change *u,
9627 struct intel_super *super)
9628{
9629 struct intel_dev *id;
9630 int ret_val = 0;
9631
1ade5cc1 9632 dprintf("(enter)\n");
089f9d79 9633 if (u->subdev < 0 || u->subdev > 1) {
f3871fdc
AK
9634 dprintf("imsm: Error: Wrong subdev: %i\n", u->subdev);
9635 return ret_val;
9636 }
9637
9638 for (id = super->devlist ; id; id = id->next) {
9639 if (id->index == (unsigned)u->subdev) {
9640 struct imsm_dev *dev = get_imsm_dev(super, u->subdev);
9641 struct imsm_map *map = get_imsm_map(dev, MAP_0);
9529d343 9642 int used_disks = imsm_num_data_members(map);
f3871fdc 9643 unsigned long long blocks_per_member;
44490938
MD
9644 unsigned long long new_size_per_disk;
9645
9646 if (used_disks == 0)
9647 return 0;
f3871fdc
AK
9648
9649 /* calculate new size
9650 */
44490938
MD
9651 new_size_per_disk = u->new_size / used_disks;
9652 blocks_per_member = new_size_per_disk +
9653 NUM_BLOCKS_DIRTY_STRIPE_REGION;
f3871fdc 9654
1c275381
MT
9655 imsm_set_array_size(dev, u->new_size);
9656 set_blocks_per_member(map, blocks_per_member);
9657 update_num_data_stripes(map, u->new_size);
f3871fdc
AK
9658 ret_val = 1;
9659 break;
9660 }
9661 }
9662
9663 return ret_val;
9664}
9665
69d40de4
JR
9666static int prepare_spare_to_activate(struct supertype *st,
9667 struct imsm_update_activate_spare *u)
9668{
9669 struct intel_super *super = st->sb;
9670 int prev_current_vol = super->current_vol;
9671 struct active_array *a;
9672 int ret = 1;
9673
9674 for (a = st->arrays; a; a = a->next)
9675 /*
9676 * Additional initialization (adding bitmap header, filling
9677 * the bitmap area with '1's to force initial rebuild for a whole
9678 * data-area) is required when adding the spare to the volume
9679 * with write-intent bitmap.
9680 */
9681 if (a->info.container_member == u->array &&
9682 a->info.consistency_policy == CONSISTENCY_POLICY_BITMAP) {
9683 struct dl *dl;
9684
9685 for (dl = super->disks; dl; dl = dl->next)
9686 if (dl == u->dl)
9687 break;
9688 if (!dl)
9689 break;
9690
9691 super->current_vol = u->array;
9692 if (st->ss->write_bitmap(st, dl->fd, NoUpdate))
9693 ret = 0;
9694 super->current_vol = prev_current_vol;
9695 }
9696 return ret;
9697}
9698
061d7da3 9699static int apply_update_activate_spare(struct imsm_update_activate_spare *u,
ca9de185 9700 struct intel_super *super,
061d7da3
LO
9701 struct active_array *active_array)
9702{
9703 struct imsm_super *mpb = super->anchor;
9704 struct imsm_dev *dev = get_imsm_dev(super, u->array);
238c0a71 9705 struct imsm_map *map = get_imsm_map(dev, MAP_0);
061d7da3
LO
9706 struct imsm_map *migr_map;
9707 struct active_array *a;
9708 struct imsm_disk *disk;
9709 __u8 to_state;
9710 struct dl *dl;
9711 unsigned int found;
9712 int failed;
5961eeec 9713 int victim;
061d7da3 9714 int i;
5961eeec 9715 int second_map_created = 0;
061d7da3 9716
5961eeec 9717 for (; u; u = u->next) {
238c0a71 9718 victim = get_imsm_disk_idx(dev, u->slot, MAP_X);
061d7da3 9719
5961eeec 9720 if (victim < 0)
9721 return 0;
061d7da3 9722
5961eeec 9723 for (dl = super->disks; dl; dl = dl->next)
9724 if (dl == u->dl)
9725 break;
061d7da3 9726
5961eeec 9727 if (!dl) {
7a862a02 9728 pr_err("error: imsm_activate_spare passed an unknown disk (index: %d)\n",
5961eeec 9729 u->dl->index);
9730 return 0;
9731 }
061d7da3 9732
5961eeec 9733 /* count failures (excluding rebuilds and the victim)
9734 * to determine map[0] state
9735 */
9736 failed = 0;
9737 for (i = 0; i < map->num_members; i++) {
9738 if (i == u->slot)
9739 continue;
9740 disk = get_imsm_disk(super,
238c0a71 9741 get_imsm_disk_idx(dev, i, MAP_X));
5961eeec 9742 if (!disk || is_failed(disk))
9743 failed++;
9744 }
061d7da3 9745
5961eeec 9746 /* adding a pristine spare, assign a new index */
9747 if (dl->index < 0) {
9748 dl->index = super->anchor->num_disks;
9749 super->anchor->num_disks++;
9750 }
9751 disk = &dl->disk;
9752 disk->status |= CONFIGURED_DISK;
9753 disk->status &= ~SPARE_DISK;
9754
9755 /* mark rebuild */
238c0a71 9756 to_state = imsm_check_degraded(super, dev, failed, MAP_0);
5961eeec 9757 if (!second_map_created) {
9758 second_map_created = 1;
9759 map->map_state = IMSM_T_STATE_DEGRADED;
9760 migrate(dev, super, to_state, MIGR_REBUILD);
9761 } else
9762 map->map_state = to_state;
238c0a71 9763 migr_map = get_imsm_map(dev, MAP_1);
5961eeec 9764 set_imsm_ord_tbl_ent(map, u->slot, dl->index);
9765 set_imsm_ord_tbl_ent(migr_map, u->slot,
9766 dl->index | IMSM_ORD_REBUILD);
9767
9768 /* update the family_num to mark a new container
9769 * generation, being careful to record the existing
9770 * family_num in orig_family_num to clean up after
9771 * earlier mdadm versions that neglected to set it.
9772 */
9773 if (mpb->orig_family_num == 0)
9774 mpb->orig_family_num = mpb->family_num;
9775 mpb->family_num += super->random;
9776
9777 /* count arrays using the victim in the metadata */
9778 found = 0;
9779 for (a = active_array; a ; a = a->next) {
76c152ca 9780 int dev_idx = a->info.container_member;
061d7da3 9781
76c152ca 9782 if (get_disk_slot_in_dev(super, dev_idx, victim) >= 0)
5961eeec 9783 found++;
9784 }
061d7da3 9785
5961eeec 9786 /* delete the victim if it is no longer being
9787 * utilized anywhere
061d7da3 9788 */
5961eeec 9789 if (!found) {
9790 struct dl **dlp;
061d7da3 9791
5961eeec 9792 /* We know that 'manager' isn't touching anything,
9793 * so it is safe to delete
9794 */
9795 for (dlp = &super->disks; *dlp; dlp = &(*dlp)->next)
061d7da3
LO
9796 if ((*dlp)->index == victim)
9797 break;
5961eeec 9798
9799 /* victim may be on the missing list */
9800 if (!*dlp)
9801 for (dlp = &super->missing; *dlp;
9802 dlp = &(*dlp)->next)
9803 if ((*dlp)->index == victim)
9804 break;
9805 imsm_delete(super, dlp, victim);
9806 }
061d7da3
LO
9807 }
9808
9809 return 1;
9810}
a29911da 9811
2e5dc010
N
9812static int apply_reshape_container_disks_update(struct imsm_update_reshape *u,
9813 struct intel_super *super,
9814 void ***space_list)
9815{
9816 struct dl *new_disk;
9817 struct intel_dev *id;
9818 int i;
9819 int delta_disks = u->new_raid_disks - u->old_raid_disks;
ee4beede 9820 int disk_count = u->old_raid_disks;
2e5dc010
N
9821 void **tofree = NULL;
9822 int devices_to_reshape = 1;
9823 struct imsm_super *mpb = super->anchor;
9824 int ret_val = 0;
d098291a 9825 unsigned int dev_id;
2e5dc010 9826
1ade5cc1 9827 dprintf("(enter)\n");
2e5dc010
N
9828
9829 /* enable spares to use in array */
9830 for (i = 0; i < delta_disks; i++) {
9831 new_disk = get_disk_super(super,
9832 major(u->new_disks[i]),
9833 minor(u->new_disks[i]));
7a862a02 9834 dprintf("imsm: new disk for reshape is: %i:%i (%p, index = %i)\n",
2e5dc010
N
9835 major(u->new_disks[i]), minor(u->new_disks[i]),
9836 new_disk, new_disk->index);
089f9d79
JS
9837 if (new_disk == NULL ||
9838 (new_disk->index >= 0 &&
9839 new_disk->index < u->old_raid_disks))
2e5dc010 9840 goto update_reshape_exit;
ee4beede 9841 new_disk->index = disk_count++;
2e5dc010
N
9842 /* slot to fill in autolayout
9843 */
9844 new_disk->raiddisk = new_disk->index;
9845 new_disk->disk.status |=
9846 CONFIGURED_DISK;
9847 new_disk->disk.status &= ~SPARE_DISK;
9848 }
9849
ed7333bd
AK
9850 dprintf("imsm: volume set mpb->num_raid_devs = %i\n",
9851 mpb->num_raid_devs);
2e5dc010
N
9852 /* manage changes in volume
9853 */
d098291a 9854 for (dev_id = 0; dev_id < mpb->num_raid_devs; dev_id++) {
2e5dc010
N
9855 void **sp = *space_list;
9856 struct imsm_dev *newdev;
9857 struct imsm_map *newmap, *oldmap;
9858
d098291a
AK
9859 for (id = super->devlist ; id; id = id->next) {
9860 if (id->index == dev_id)
9861 break;
9862 }
9863 if (id == NULL)
9864 break;
2e5dc010
N
9865 if (!sp)
9866 continue;
9867 *space_list = *sp;
9868 newdev = (void*)sp;
9869 /* Copy the dev, but not (all of) the map */
9870 memcpy(newdev, id->dev, sizeof(*newdev));
238c0a71
AK
9871 oldmap = get_imsm_map(id->dev, MAP_0);
9872 newmap = get_imsm_map(newdev, MAP_0);
2e5dc010
N
9873 /* Copy the current map */
9874 memcpy(newmap, oldmap, sizeof_imsm_map(oldmap));
9875 /* update one device only
9876 */
9877 if (devices_to_reshape) {
ed7333bd
AK
9878 dprintf("imsm: modifying subdev: %i\n",
9879 id->index);
2e5dc010
N
9880 devices_to_reshape--;
9881 newdev->vol.migr_state = 1;
4036e7ee 9882 set_vol_curr_migr_unit(newdev, 0);
ea672ee1 9883 set_migr_type(newdev, MIGR_GEN_MIGR);
2e5dc010
N
9884 newmap->num_members = u->new_raid_disks;
9885 for (i = 0; i < delta_disks; i++) {
9886 set_imsm_ord_tbl_ent(newmap,
9887 u->old_raid_disks + i,
9888 u->old_raid_disks + i);
9889 }
9890 /* New map is correct, now need to save old map
9891 */
238c0a71 9892 newmap = get_imsm_map(newdev, MAP_1);
2e5dc010
N
9893 memcpy(newmap, oldmap, sizeof_imsm_map(oldmap));
9894
f3871fdc 9895 imsm_set_array_size(newdev, -1);
2e5dc010
N
9896 }
9897
9898 sp = (void **)id->dev;
9899 id->dev = newdev;
9900 *sp = tofree;
9901 tofree = sp;
8e59f3d8
AK
9902
9903 /* Clear migration record */
9904 memset(super->migr_rec, 0, sizeof(struct migr_record));
2e5dc010 9905 }
819bc634
AK
9906 if (tofree)
9907 *space_list = tofree;
2e5dc010
N
9908 ret_val = 1;
9909
9910update_reshape_exit:
9911
9912 return ret_val;
9913}
9914
bb025c2f 9915static int apply_takeover_update(struct imsm_update_takeover *u,
8ca6df95
KW
9916 struct intel_super *super,
9917 void ***space_list)
bb025c2f
KW
9918{
9919 struct imsm_dev *dev = NULL;
8ca6df95
KW
9920 struct intel_dev *dv;
9921 struct imsm_dev *dev_new;
bb025c2f
KW
9922 struct imsm_map *map;
9923 struct dl *dm, *du;
8ca6df95 9924 int i;
bb025c2f
KW
9925
9926 for (dv = super->devlist; dv; dv = dv->next)
9927 if (dv->index == (unsigned int)u->subarray) {
9928 dev = dv->dev;
9929 break;
9930 }
9931
9932 if (dev == NULL)
9933 return 0;
9934
238c0a71 9935 map = get_imsm_map(dev, MAP_0);
bb025c2f
KW
9936
9937 if (u->direction == R10_TO_R0) {
43d5ec18 9938 /* Number of failed disks must be half of initial disk number */
3b451610
AK
9939 if (imsm_count_failed(super, dev, MAP_0) !=
9940 (map->num_members / 2))
43d5ec18
KW
9941 return 0;
9942
bb025c2f
KW
9943 /* iterate through devices to mark removed disks as spare */
9944 for (dm = super->disks; dm; dm = dm->next) {
9945 if (dm->disk.status & FAILED_DISK) {
9946 int idx = dm->index;
9947 /* update indexes on the disk list */
9948/* FIXME this loop-with-the-loop looks wrong, I'm not convinced
9949 the index values will end up being correct.... NB */
9950 for (du = super->disks; du; du = du->next)
9951 if (du->index > idx)
9952 du->index--;
9953 /* mark as spare disk */
a8619d23 9954 mark_spare(dm);
bb025c2f
KW
9955 }
9956 }
bb025c2f 9957 /* update map */
1c275381 9958 map->num_members /= map->num_domains;
bb025c2f 9959 map->map_state = IMSM_T_STATE_NORMAL;
bb025c2f 9960 map->raid_level = 0;
1c275381
MT
9961 set_num_domains(map);
9962 update_num_data_stripes(map, imsm_dev_size(dev));
bb025c2f
KW
9963 map->failed_disk_num = -1;
9964 }
9965
8ca6df95
KW
9966 if (u->direction == R0_TO_R10) {
9967 void **space;
4a353e6e 9968
8ca6df95
KW
9969 /* update slots in current disk list */
9970 for (dm = super->disks; dm; dm = dm->next) {
9971 if (dm->index >= 0)
9972 dm->index *= 2;
9973 }
9974 /* create new *missing* disks */
9975 for (i = 0; i < map->num_members; i++) {
9976 space = *space_list;
9977 if (!space)
9978 continue;
9979 *space_list = *space;
9980 du = (void *)space;
9981 memcpy(du, super->disks, sizeof(*du));
8ca6df95
KW
9982 du->fd = -1;
9983 du->minor = 0;
9984 du->major = 0;
9985 du->index = (i * 2) + 1;
9986 sprintf((char *)du->disk.serial,
9987 " MISSING_%d", du->index);
9988 sprintf((char *)du->serial,
9989 "MISSING_%d", du->index);
9990 du->next = super->missing;
9991 super->missing = du;
9992 }
9993 /* create new dev and map */
9994 space = *space_list;
9995 if (!space)
9996 return 0;
9997 *space_list = *space;
9998 dev_new = (void *)space;
9999 memcpy(dev_new, dev, sizeof(*dev));
10000 /* update new map */
238c0a71 10001 map = get_imsm_map(dev_new, MAP_0);
1c275381 10002
1a2487c2 10003 map->map_state = IMSM_T_STATE_DEGRADED;
8ca6df95 10004 map->raid_level = 1;
1c275381
MT
10005 set_num_domains(map);
10006 map->num_members = map->num_members * map->num_domains;
10007 update_num_data_stripes(map, imsm_dev_size(dev));
4a353e6e 10008
8ca6df95
KW
10009 /* replace dev<->dev_new */
10010 dv->dev = dev_new;
10011 }
bb025c2f
KW
10012 /* update disk order table */
10013 for (du = super->disks; du; du = du->next)
10014 if (du->index >= 0)
10015 set_imsm_ord_tbl_ent(map, du->index, du->index);
8ca6df95 10016 for (du = super->missing; du; du = du->next)
1a2487c2
KW
10017 if (du->index >= 0) {
10018 set_imsm_ord_tbl_ent(map, du->index, du->index);
4c9e8c1e 10019 mark_missing(super, dv->dev, &du->disk, du->index);
1a2487c2 10020 }
bb025c2f
KW
10021
10022 return 1;
10023}
10024
e8319a19
DW
10025static void imsm_process_update(struct supertype *st,
10026 struct metadata_update *update)
10027{
10028 /**
10029 * crack open the metadata_update envelope to find the update record
10030 * update can be one of:
d195167d
AK
10031 * update_reshape_container_disks - all the arrays in the container
10032 * are being reshaped to have more devices. We need to mark
10033 * the arrays for general migration and convert selected spares
10034 * into active devices.
10035 * update_activate_spare - a spare device has replaced a failed
1011e834
N
10036 * device in an array, update the disk_ord_tbl. If this disk is
10037 * present in all member arrays then also clear the SPARE_DISK
10038 * flag
d195167d
AK
10039 * update_create_array
10040 * update_kill_array
10041 * update_rename_array
10042 * update_add_remove_disk
e8319a19
DW
10043 */
10044 struct intel_super *super = st->sb;
4d7b1503 10045 struct imsm_super *mpb;
e8319a19
DW
10046 enum imsm_update_type type = *(enum imsm_update_type *) update->buf;
10047
4d7b1503
DW
10048 /* update requires a larger buf but the allocation failed */
10049 if (super->next_len && !super->next_buf) {
10050 super->next_len = 0;
10051 return;
10052 }
10053
10054 if (super->next_buf) {
10055 memcpy(super->next_buf, super->buf, super->len);
10056 free(super->buf);
10057 super->len = super->next_len;
10058 super->buf = super->next_buf;
10059
10060 super->next_len = 0;
10061 super->next_buf = NULL;
10062 }
10063
10064 mpb = super->anchor;
10065
e8319a19 10066 switch (type) {
0ec5d470
AK
10067 case update_general_migration_checkpoint: {
10068 struct intel_dev *id;
10069 struct imsm_update_general_migration_checkpoint *u =
10070 (void *)update->buf;
10071
1ade5cc1 10072 dprintf("called for update_general_migration_checkpoint\n");
0ec5d470
AK
10073
10074 /* find device under general migration */
10075 for (id = super->devlist ; id; id = id->next) {
10076 if (is_gen_migration(id->dev)) {
4036e7ee
MT
10077 set_vol_curr_migr_unit(id->dev,
10078 u->curr_migr_unit);
0ec5d470
AK
10079 super->updates_pending++;
10080 }
10081 }
10082 break;
10083 }
bb025c2f
KW
10084 case update_takeover: {
10085 struct imsm_update_takeover *u = (void *)update->buf;
1a2487c2
KW
10086 if (apply_takeover_update(u, super, &update->space_list)) {
10087 imsm_update_version_info(super);
bb025c2f 10088 super->updates_pending++;
1a2487c2 10089 }
bb025c2f
KW
10090 break;
10091 }
10092
78b10e66 10093 case update_reshape_container_disks: {
d195167d 10094 struct imsm_update_reshape *u = (void *)update->buf;
2e5dc010
N
10095 if (apply_reshape_container_disks_update(
10096 u, super, &update->space_list))
10097 super->updates_pending++;
78b10e66
N
10098 break;
10099 }
48c5303a 10100 case update_reshape_migration: {
a29911da
PC
10101 struct imsm_update_reshape_migration *u = (void *)update->buf;
10102 if (apply_reshape_migration_update(
10103 u, super, &update->space_list))
10104 super->updates_pending++;
48c5303a
PC
10105 break;
10106 }
f3871fdc
AK
10107 case update_size_change: {
10108 struct imsm_update_size_change *u = (void *)update->buf;
10109 if (apply_size_change_update(u, super))
10110 super->updates_pending++;
10111 break;
10112 }
e8319a19 10113 case update_activate_spare: {
1011e834 10114 struct imsm_update_activate_spare *u = (void *) update->buf;
69d40de4
JR
10115
10116 if (prepare_spare_to_activate(st, u) &&
10117 apply_update_activate_spare(u, super, st->arrays))
061d7da3 10118 super->updates_pending++;
8273f55e
DW
10119 break;
10120 }
10121 case update_create_array: {
10122 /* someone wants to create a new array, we need to be aware of
10123 * a few races/collisions:
10124 * 1/ 'Create' called by two separate instances of mdadm
10125 * 2/ 'Create' versus 'activate_spare': mdadm has chosen
10126 * devices that have since been assimilated via
10127 * activate_spare.
10128 * In the event this update can not be carried out mdadm will
10129 * (FIX ME) notice that its update did not take hold.
10130 */
10131 struct imsm_update_create_array *u = (void *) update->buf;
ba2de7ba 10132 struct intel_dev *dv;
8273f55e
DW
10133 struct imsm_dev *dev;
10134 struct imsm_map *map, *new_map;
10135 unsigned long long start, end;
10136 unsigned long long new_start, new_end;
10137 int i;
54c2c1ea
DW
10138 struct disk_info *inf;
10139 struct dl *dl;
8273f55e
DW
10140
10141 /* handle racing creates: first come first serve */
10142 if (u->dev_idx < mpb->num_raid_devs) {
1ade5cc1 10143 dprintf("subarray %d already defined\n", u->dev_idx);
ba2de7ba 10144 goto create_error;
8273f55e
DW
10145 }
10146
10147 /* check update is next in sequence */
10148 if (u->dev_idx != mpb->num_raid_devs) {
1ade5cc1
N
10149 dprintf("can not create array %d expected index %d\n",
10150 u->dev_idx, mpb->num_raid_devs);
ba2de7ba 10151 goto create_error;
8273f55e
DW
10152 }
10153
238c0a71 10154 new_map = get_imsm_map(&u->dev, MAP_0);
5551b113 10155 new_start = pba_of_lba0(new_map);
44490938 10156 new_end = new_start + per_dev_array_size(new_map);
54c2c1ea 10157 inf = get_disk_info(u);
8273f55e
DW
10158
10159 /* handle activate_spare versus create race:
10160 * check to make sure that overlapping arrays do not include
10161 * overalpping disks
10162 */
10163 for (i = 0; i < mpb->num_raid_devs; i++) {
949c47a0 10164 dev = get_imsm_dev(super, i);
238c0a71 10165 map = get_imsm_map(dev, MAP_0);
5551b113 10166 start = pba_of_lba0(map);
44490938 10167 end = start + per_dev_array_size(map);
8273f55e
DW
10168 if ((new_start >= start && new_start <= end) ||
10169 (start >= new_start && start <= new_end))
54c2c1ea
DW
10170 /* overlap */;
10171 else
10172 continue;
10173
10174 if (disks_overlap(super, i, u)) {
1ade5cc1 10175 dprintf("arrays overlap\n");
ba2de7ba 10176 goto create_error;
8273f55e
DW
10177 }
10178 }
8273f55e 10179
949c47a0
DW
10180 /* check that prepare update was successful */
10181 if (!update->space) {
1ade5cc1 10182 dprintf("prepare update failed\n");
ba2de7ba 10183 goto create_error;
949c47a0
DW
10184 }
10185
54c2c1ea
DW
10186 /* check that all disks are still active before committing
10187 * changes. FIXME: could we instead handle this by creating a
10188 * degraded array? That's probably not what the user expects,
10189 * so better to drop this update on the floor.
10190 */
10191 for (i = 0; i < new_map->num_members; i++) {
10192 dl = serial_to_dl(inf[i].serial, super);
10193 if (!dl) {
1ade5cc1 10194 dprintf("disk disappeared\n");
ba2de7ba 10195 goto create_error;
54c2c1ea 10196 }
949c47a0
DW
10197 }
10198
8273f55e 10199 super->updates_pending++;
54c2c1ea
DW
10200
10201 /* convert spares to members and fixup ord_tbl */
10202 for (i = 0; i < new_map->num_members; i++) {
10203 dl = serial_to_dl(inf[i].serial, super);
10204 if (dl->index == -1) {
10205 dl->index = mpb->num_disks;
10206 mpb->num_disks++;
10207 dl->disk.status |= CONFIGURED_DISK;
10208 dl->disk.status &= ~SPARE_DISK;
10209 }
10210 set_imsm_ord_tbl_ent(new_map, i, dl->index);
10211 }
10212
ba2de7ba
DW
10213 dv = update->space;
10214 dev = dv->dev;
949c47a0
DW
10215 update->space = NULL;
10216 imsm_copy_dev(dev, &u->dev);
ba2de7ba
DW
10217 dv->index = u->dev_idx;
10218 dv->next = super->devlist;
10219 super->devlist = dv;
8273f55e 10220 mpb->num_raid_devs++;
8273f55e 10221
4d1313e9 10222 imsm_update_version_info(super);
8273f55e 10223 break;
ba2de7ba
DW
10224 create_error:
10225 /* mdmon knows how to release update->space, but not
10226 * ((struct intel_dev *) update->space)->dev
10227 */
10228 if (update->space) {
10229 dv = update->space;
10230 free(dv->dev);
10231 }
8273f55e 10232 break;
e8319a19 10233 }
33414a01
DW
10234 case update_kill_array: {
10235 struct imsm_update_kill_array *u = (void *) update->buf;
10236 int victim = u->dev_idx;
10237 struct active_array *a;
10238 struct intel_dev **dp;
33414a01
DW
10239
10240 /* sanity check that we are not affecting the uuid of
10241 * active arrays, or deleting an active array
10242 *
10243 * FIXME when immutable ids are available, but note that
10244 * we'll also need to fixup the invalidated/active
10245 * subarray indexes in mdstat
10246 */
10247 for (a = st->arrays; a; a = a->next)
10248 if (a->info.container_member >= victim)
10249 break;
10250 /* by definition if mdmon is running at least one array
10251 * is active in the container, so checking
10252 * mpb->num_raid_devs is just extra paranoia
10253 */
756a15f3 10254 if (a || mpb->num_raid_devs == 1 || victim >= super->anchor->num_raid_devs) {
33414a01
DW
10255 dprintf("failed to delete subarray-%d\n", victim);
10256 break;
10257 }
10258
10259 for (dp = &super->devlist; *dp;)
f21e18ca 10260 if ((*dp)->index == (unsigned)super->current_vol) {
33414a01
DW
10261 *dp = (*dp)->next;
10262 } else {
f21e18ca 10263 if ((*dp)->index > (unsigned)victim)
33414a01
DW
10264 (*dp)->index--;
10265 dp = &(*dp)->next;
10266 }
10267 mpb->num_raid_devs--;
10268 super->updates_pending++;
10269 break;
10270 }
aa534678
DW
10271 case update_rename_array: {
10272 struct imsm_update_rename_array *u = (void *) update->buf;
10273 char name[MAX_RAID_SERIAL_LEN+1];
10274 int target = u->dev_idx;
10275 struct active_array *a;
10276 struct imsm_dev *dev;
10277
10278 /* sanity check that we are not affecting the uuid of
10279 * an active array
10280 */
40659392 10281 memset(name, 0, sizeof(name));
aa534678
DW
10282 snprintf(name, MAX_RAID_SERIAL_LEN, "%s", (char *) u->name);
10283 name[MAX_RAID_SERIAL_LEN] = '\0';
10284 for (a = st->arrays; a; a = a->next)
10285 if (a->info.container_member == target)
10286 break;
10287 dev = get_imsm_dev(super, u->dev_idx);
e2eb503b
MT
10288
10289 if (a || !dev || imsm_is_name_allowed(super, name, 0) == false) {
aa534678
DW
10290 dprintf("failed to rename subarray-%d\n", target);
10291 break;
10292 }
10293
40659392 10294 memcpy(dev->volume, name, MAX_RAID_SERIAL_LEN);
aa534678
DW
10295 super->updates_pending++;
10296 break;
10297 }
1a64be56 10298 case update_add_remove_disk: {
43dad3d6 10299 /* we may be able to repair some arrays if disks are
095b8088 10300 * being added, check the status of add_remove_disk
1a64be56
LM
10301 * if discs has been added.
10302 */
10303 if (add_remove_disk_update(super)) {
43dad3d6 10304 struct active_array *a;
072b727f
DW
10305
10306 super->updates_pending++;
1a64be56 10307 for (a = st->arrays; a; a = a->next)
43dad3d6
DW
10308 a->check_degraded = 1;
10309 }
43dad3d6 10310 break;
e8319a19 10311 }
bbab0940
TM
10312 case update_prealloc_badblocks_mem:
10313 break;
e6e9dd3f
AP
10314 case update_rwh_policy: {
10315 struct imsm_update_rwh_policy *u = (void *)update->buf;
10316 int target = u->dev_idx;
10317 struct imsm_dev *dev = get_imsm_dev(super, target);
e6e9dd3f
AP
10318
10319 if (dev->rwh_policy != u->new_policy) {
10320 dev->rwh_policy = u->new_policy;
10321 super->updates_pending++;
10322 }
10323 break;
10324 }
1a64be56 10325 default:
ebf3be99 10326 pr_err("error: unsupported process update type:(type: %d)\n", type);
1a64be56 10327 }
e8319a19 10328}
88758e9d 10329
bc0b9d34
PC
10330static struct mdinfo *get_spares_for_grow(struct supertype *st);
10331
5fe6f031
N
10332static int imsm_prepare_update(struct supertype *st,
10333 struct metadata_update *update)
8273f55e 10334{
949c47a0 10335 /**
4d7b1503
DW
10336 * Allocate space to hold new disk entries, raid-device entries or a new
10337 * mpb if necessary. The manager synchronously waits for updates to
10338 * complete in the monitor, so new mpb buffers allocated here can be
10339 * integrated by the monitor thread without worrying about live pointers
10340 * in the manager thread.
8273f55e 10341 */
095b8088 10342 enum imsm_update_type type;
4d7b1503 10343 struct intel_super *super = st->sb;
f36a9ecd 10344 unsigned int sector_size = super->sector_size;
4d7b1503
DW
10345 struct imsm_super *mpb = super->anchor;
10346 size_t buf_len;
10347 size_t len = 0;
949c47a0 10348
095b8088
N
10349 if (update->len < (int)sizeof(type))
10350 return 0;
10351
10352 type = *(enum imsm_update_type *) update->buf;
10353
949c47a0 10354 switch (type) {
0ec5d470 10355 case update_general_migration_checkpoint:
095b8088
N
10356 if (update->len < (int)sizeof(struct imsm_update_general_migration_checkpoint))
10357 return 0;
1ade5cc1 10358 dprintf("called for update_general_migration_checkpoint\n");
0ec5d470 10359 break;
abedf5fc
KW
10360 case update_takeover: {
10361 struct imsm_update_takeover *u = (void *)update->buf;
095b8088
N
10362 if (update->len < (int)sizeof(*u))
10363 return 0;
abedf5fc
KW
10364 if (u->direction == R0_TO_R10) {
10365 void **tail = (void **)&update->space_list;
10366 struct imsm_dev *dev = get_imsm_dev(super, u->subarray);
238c0a71 10367 struct imsm_map *map = get_imsm_map(dev, MAP_0);
abedf5fc
KW
10368 int num_members = map->num_members;
10369 void *space;
10370 int size, i;
abedf5fc
KW
10371 /* allocate memory for added disks */
10372 for (i = 0; i < num_members; i++) {
10373 size = sizeof(struct dl);
503975b9 10374 space = xmalloc(size);
abedf5fc
KW
10375 *tail = space;
10376 tail = space;
10377 *tail = NULL;
10378 }
10379 /* allocate memory for new device */
10380 size = sizeof_imsm_dev(super->devlist->dev, 0) +
10381 (num_members * sizeof(__u32));
503975b9
N
10382 space = xmalloc(size);
10383 *tail = space;
10384 tail = space;
10385 *tail = NULL;
10386 len = disks_to_mpb_size(num_members * 2);
abedf5fc
KW
10387 }
10388
10389 break;
10390 }
78b10e66 10391 case update_reshape_container_disks: {
d195167d
AK
10392 /* Every raid device in the container is about to
10393 * gain some more devices, and we will enter a
10394 * reconfiguration.
10395 * So each 'imsm_map' will be bigger, and the imsm_vol
10396 * will now hold 2 of them.
10397 * Thus we need new 'struct imsm_dev' allocations sized
10398 * as sizeof_imsm_dev but with more devices in both maps.
10399 */
10400 struct imsm_update_reshape *u = (void *)update->buf;
10401 struct intel_dev *dl;
10402 void **space_tail = (void**)&update->space_list;
10403
095b8088
N
10404 if (update->len < (int)sizeof(*u))
10405 return 0;
10406
1ade5cc1 10407 dprintf("for update_reshape\n");
d195167d
AK
10408
10409 for (dl = super->devlist; dl; dl = dl->next) {
10410 int size = sizeof_imsm_dev(dl->dev, 1);
10411 void *s;
d677e0b8
AK
10412 if (u->new_raid_disks > u->old_raid_disks)
10413 size += sizeof(__u32)*2*
10414 (u->new_raid_disks - u->old_raid_disks);
503975b9 10415 s = xmalloc(size);
d195167d
AK
10416 *space_tail = s;
10417 space_tail = s;
10418 *space_tail = NULL;
10419 }
10420
10421 len = disks_to_mpb_size(u->new_raid_disks);
10422 dprintf("New anchor length is %llu\n", (unsigned long long)len);
78b10e66
N
10423 break;
10424 }
48c5303a 10425 case update_reshape_migration: {
bc0b9d34
PC
10426 /* for migration level 0->5 we need to add disks
10427 * so the same as for container operation we will copy
10428 * device to the bigger location.
10429 * in memory prepared device and new disk area are prepared
10430 * for usage in process update
10431 */
10432 struct imsm_update_reshape_migration *u = (void *)update->buf;
10433 struct intel_dev *id;
10434 void **space_tail = (void **)&update->space_list;
10435 int size;
10436 void *s;
10437 int current_level = -1;
10438
095b8088
N
10439 if (update->len < (int)sizeof(*u))
10440 return 0;
10441
1ade5cc1 10442 dprintf("for update_reshape\n");
bc0b9d34
PC
10443
10444 /* add space for bigger array in update
10445 */
10446 for (id = super->devlist; id; id = id->next) {
10447 if (id->index == (unsigned)u->subdev) {
10448 size = sizeof_imsm_dev(id->dev, 1);
10449 if (u->new_raid_disks > u->old_raid_disks)
10450 size += sizeof(__u32)*2*
10451 (u->new_raid_disks - u->old_raid_disks);
503975b9 10452 s = xmalloc(size);
bc0b9d34
PC
10453 *space_tail = s;
10454 space_tail = s;
10455 *space_tail = NULL;
10456 break;
10457 }
10458 }
10459 if (update->space_list == NULL)
10460 break;
10461
10462 /* add space for disk in update
10463 */
10464 size = sizeof(struct dl);
503975b9 10465 s = xmalloc(size);
bc0b9d34
PC
10466 *space_tail = s;
10467 space_tail = s;
10468 *space_tail = NULL;
10469
10470 /* add spare device to update
10471 */
10472 for (id = super->devlist ; id; id = id->next)
10473 if (id->index == (unsigned)u->subdev) {
10474 struct imsm_dev *dev;
10475 struct imsm_map *map;
10476
10477 dev = get_imsm_dev(super, u->subdev);
238c0a71 10478 map = get_imsm_map(dev, MAP_0);
bc0b9d34
PC
10479 current_level = map->raid_level;
10480 break;
10481 }
089f9d79 10482 if (u->new_level == 5 && u->new_level != current_level) {
bc0b9d34
PC
10483 struct mdinfo *spares;
10484
10485 spares = get_spares_for_grow(st);
10486 if (spares) {
10487 struct dl *dl;
10488 struct mdinfo *dev;
10489
10490 dev = spares->devs;
10491 if (dev) {
10492 u->new_disks[0] =
10493 makedev(dev->disk.major,
10494 dev->disk.minor);
10495 dl = get_disk_super(super,
10496 dev->disk.major,
10497 dev->disk.minor);
10498 dl->index = u->old_raid_disks;
10499 dev = dev->next;
10500 }
10501 sysfs_free(spares);
10502 }
10503 }
10504 len = disks_to_mpb_size(u->new_raid_disks);
10505 dprintf("New anchor length is %llu\n", (unsigned long long)len);
48c5303a
PC
10506 break;
10507 }
f3871fdc 10508 case update_size_change: {
095b8088
N
10509 if (update->len < (int)sizeof(struct imsm_update_size_change))
10510 return 0;
10511 break;
10512 }
10513 case update_activate_spare: {
10514 if (update->len < (int)sizeof(struct imsm_update_activate_spare))
10515 return 0;
f3871fdc
AK
10516 break;
10517 }
949c47a0
DW
10518 case update_create_array: {
10519 struct imsm_update_create_array *u = (void *) update->buf;
ba2de7ba 10520 struct intel_dev *dv;
54c2c1ea 10521 struct imsm_dev *dev = &u->dev;
238c0a71 10522 struct imsm_map *map = get_imsm_map(dev, MAP_0);
54c2c1ea
DW
10523 struct dl *dl;
10524 struct disk_info *inf;
10525 int i;
10526 int activate = 0;
949c47a0 10527
095b8088
N
10528 if (update->len < (int)sizeof(*u))
10529 return 0;
10530
54c2c1ea
DW
10531 inf = get_disk_info(u);
10532 len = sizeof_imsm_dev(dev, 1);
ba2de7ba 10533 /* allocate a new super->devlist entry */
503975b9
N
10534 dv = xmalloc(sizeof(*dv));
10535 dv->dev = xmalloc(len);
10536 update->space = dv;
949c47a0 10537
54c2c1ea
DW
10538 /* count how many spares will be converted to members */
10539 for (i = 0; i < map->num_members; i++) {
10540 dl = serial_to_dl(inf[i].serial, super);
10541 if (!dl) {
10542 /* hmm maybe it failed?, nothing we can do about
10543 * it here
10544 */
10545 continue;
10546 }
10547 if (count_memberships(dl, super) == 0)
10548 activate++;
10549 }
10550 len += activate * sizeof(struct imsm_disk);
949c47a0 10551 break;
095b8088
N
10552 }
10553 case update_kill_array: {
10554 if (update->len < (int)sizeof(struct imsm_update_kill_array))
10555 return 0;
949c47a0
DW
10556 break;
10557 }
095b8088
N
10558 case update_rename_array: {
10559 if (update->len < (int)sizeof(struct imsm_update_rename_array))
10560 return 0;
10561 break;
10562 }
10563 case update_add_remove_disk:
10564 /* no update->len needed */
10565 break;
bbab0940
TM
10566 case update_prealloc_badblocks_mem:
10567 super->extra_space += sizeof(struct bbm_log) -
10568 get_imsm_bbm_log_size(super->bbm_log);
10569 break;
e6e9dd3f
AP
10570 case update_rwh_policy: {
10571 if (update->len < (int)sizeof(struct imsm_update_rwh_policy))
10572 return 0;
10573 break;
10574 }
095b8088
N
10575 default:
10576 return 0;
949c47a0 10577 }
8273f55e 10578
4d7b1503
DW
10579 /* check if we need a larger metadata buffer */
10580 if (super->next_buf)
10581 buf_len = super->next_len;
10582 else
10583 buf_len = super->len;
10584
bbab0940 10585 if (__le32_to_cpu(mpb->mpb_size) + super->extra_space + len > buf_len) {
4d7b1503
DW
10586 /* ok we need a larger buf than what is currently allocated
10587 * if this allocation fails process_update will notice that
10588 * ->next_len is set and ->next_buf is NULL
10589 */
bbab0940
TM
10590 buf_len = ROUND_UP(__le32_to_cpu(mpb->mpb_size) +
10591 super->extra_space + len, sector_size);
4d7b1503
DW
10592 if (super->next_buf)
10593 free(super->next_buf);
10594
10595 super->next_len = buf_len;
f36a9ecd 10596 if (posix_memalign(&super->next_buf, sector_size, buf_len) == 0)
1f45a8ad
DW
10597 memset(super->next_buf, 0, buf_len);
10598 else
4d7b1503
DW
10599 super->next_buf = NULL;
10600 }
5fe6f031 10601 return 1;
8273f55e
DW
10602}
10603
ae6aad82 10604/* must be called while manager is quiesced */
f21e18ca 10605static void imsm_delete(struct intel_super *super, struct dl **dlp, unsigned index)
ae6aad82
DW
10606{
10607 struct imsm_super *mpb = super->anchor;
ae6aad82
DW
10608 struct dl *iter;
10609 struct imsm_dev *dev;
10610 struct imsm_map *map;
4c9e8c1e 10611 unsigned int i, j, num_members;
fb12a745 10612 __u32 ord, ord_map0;
4c9e8c1e 10613 struct bbm_log *log = super->bbm_log;
ae6aad82 10614
1ade5cc1 10615 dprintf("deleting device[%d] from imsm_super\n", index);
ae6aad82
DW
10616
10617 /* shift all indexes down one */
10618 for (iter = super->disks; iter; iter = iter->next)
f21e18ca 10619 if (iter->index > (int)index)
ae6aad82 10620 iter->index--;
47ee5a45 10621 for (iter = super->missing; iter; iter = iter->next)
f21e18ca 10622 if (iter->index > (int)index)
47ee5a45 10623 iter->index--;
ae6aad82
DW
10624
10625 for (i = 0; i < mpb->num_raid_devs; i++) {
10626 dev = get_imsm_dev(super, i);
238c0a71 10627 map = get_imsm_map(dev, MAP_0);
24565c9a
DW
10628 num_members = map->num_members;
10629 for (j = 0; j < num_members; j++) {
10630 /* update ord entries being careful not to propagate
10631 * ord-flags to the first map
10632 */
238c0a71 10633 ord = get_imsm_ord_tbl_ent(dev, j, MAP_X);
fb12a745 10634 ord_map0 = get_imsm_ord_tbl_ent(dev, j, MAP_0);
ae6aad82 10635
24565c9a
DW
10636 if (ord_to_idx(ord) <= index)
10637 continue;
ae6aad82 10638
238c0a71 10639 map = get_imsm_map(dev, MAP_0);
fb12a745 10640 set_imsm_ord_tbl_ent(map, j, ord_map0 - 1);
238c0a71 10641 map = get_imsm_map(dev, MAP_1);
24565c9a
DW
10642 if (map)
10643 set_imsm_ord_tbl_ent(map, j, ord - 1);
ae6aad82
DW
10644 }
10645 }
10646
4c9e8c1e
TM
10647 for (i = 0; i < log->entry_count; i++) {
10648 struct bbm_log_entry *entry = &log->marked_block_entries[i];
10649
10650 if (entry->disk_ordinal <= index)
10651 continue;
10652 entry->disk_ordinal--;
10653 }
10654
ae6aad82
DW
10655 mpb->num_disks--;
10656 super->updates_pending++;
24565c9a
DW
10657 if (*dlp) {
10658 struct dl *dl = *dlp;
10659
10660 *dlp = (*dlp)->next;
3a85bf0e 10661 __free_imsm_disk(dl, 1);
24565c9a 10662 }
ae6aad82 10663}
9a717282 10664
9a717282
AK
10665static int imsm_get_allowed_degradation(int level, int raid_disks,
10666 struct intel_super *super,
10667 struct imsm_dev *dev)
10668{
10669 switch (level) {
bf5cf7c7 10670 case 1:
9a717282
AK
10671 case 10:{
10672 int ret_val = 0;
10673 struct imsm_map *map;
10674 int i;
10675
10676 ret_val = raid_disks/2;
10677 /* check map if all disks pairs not failed
10678 * in both maps
10679 */
238c0a71 10680 map = get_imsm_map(dev, MAP_0);
9a717282
AK
10681 for (i = 0; i < ret_val; i++) {
10682 int degradation = 0;
10683 if (get_imsm_disk(super, i) == NULL)
10684 degradation++;
10685 if (get_imsm_disk(super, i + 1) == NULL)
10686 degradation++;
10687 if (degradation == 2)
10688 return 0;
10689 }
238c0a71 10690 map = get_imsm_map(dev, MAP_1);
9a717282
AK
10691 /* if there is no second map
10692 * result can be returned
10693 */
10694 if (map == NULL)
10695 return ret_val;
10696 /* check degradation in second map
10697 */
10698 for (i = 0; i < ret_val; i++) {
10699 int degradation = 0;
10700 if (get_imsm_disk(super, i) == NULL)
10701 degradation++;
10702 if (get_imsm_disk(super, i + 1) == NULL)
10703 degradation++;
10704 if (degradation == 2)
10705 return 0;
10706 }
10707 return ret_val;
10708 }
10709 case 5:
10710 return 1;
10711 case 6:
10712 return 2;
10713 default:
10714 return 0;
10715 }
10716}
10717
d31ad643
PB
10718/*******************************************************************************
10719 * Function: validate_container_imsm
10720 * Description: This routine validates container after assemble,
10721 * eg. if devices in container are under the same controller.
10722 *
10723 * Parameters:
10724 * info : linked list with info about devices used in array
10725 * Returns:
10726 * 1 : HBA mismatch
10727 * 0 : Success
10728 ******************************************************************************/
10729int validate_container_imsm(struct mdinfo *info)
10730{
420dafcd 10731 if (check_no_platform())
6b781d33 10732 return 0;
d31ad643 10733
6b781d33
AP
10734 struct sys_dev *idev;
10735 struct sys_dev *hba = NULL;
10736 struct sys_dev *intel_devices = find_intel_devices();
10737 char *dev_path = devt_to_devpath(makedev(info->disk.major,
7c798f87 10738 info->disk.minor), 1, NULL);
6b781d33
AP
10739
10740 for (idev = intel_devices; idev; idev = idev->next) {
10741 if (dev_path && strstr(dev_path, idev->path)) {
10742 hba = idev;
10743 break;
d31ad643 10744 }
6b781d33
AP
10745 }
10746 if (dev_path)
d31ad643
PB
10747 free(dev_path);
10748
6b781d33
AP
10749 if (!hba) {
10750 pr_err("WARNING - Cannot detect HBA for device %s!\n",
10751 devid2kname(makedev(info->disk.major, info->disk.minor)));
10752 return 1;
10753 }
10754
10755 const struct imsm_orom *orom = get_orom_by_device_id(hba->dev_id);
10756 struct mdinfo *dev;
10757
10758 for (dev = info->next; dev; dev = dev->next) {
7c798f87
MT
10759 dev_path = devt_to_devpath(makedev(dev->disk.major,
10760 dev->disk.minor), 1, NULL);
6b781d33
AP
10761
10762 struct sys_dev *hba2 = NULL;
10763 for (idev = intel_devices; idev; idev = idev->next) {
10764 if (dev_path && strstr(dev_path, idev->path)) {
10765 hba2 = idev;
10766 break;
d31ad643
PB
10767 }
10768 }
6b781d33
AP
10769 if (dev_path)
10770 free(dev_path);
10771
10772 const struct imsm_orom *orom2 = hba2 == NULL ? NULL :
10773 get_orom_by_device_id(hba2->dev_id);
10774
10775 if (hba2 && hba->type != hba2->type) {
10776 pr_err("WARNING - HBAs of devices do not match %s != %s\n",
10777 get_sys_dev_type(hba->type), get_sys_dev_type(hba2->type));
10778 return 1;
10779 }
10780
07cb1e57 10781 if (orom != orom2) {
6b781d33
AP
10782 pr_err("WARNING - IMSM container assembled with disks under different HBAs!\n"
10783 " This operation is not supported and can lead to data loss.\n");
10784 return 1;
10785 }
10786
10787 if (!orom) {
10788 pr_err("WARNING - IMSM container assembled with disks under HBAs without IMSM platform support!\n"
10789 " This operation is not supported and can lead to data loss.\n");
10790 return 1;
10791 }
d31ad643 10792 }
6b781d33 10793
d31ad643
PB
10794 return 0;
10795}
32141c17 10796
6f50473f
TM
10797/*******************************************************************************
10798* Function: imsm_record_badblock
10799* Description: This routine stores new bad block record in BBM log
10800*
10801* Parameters:
10802* a : array containing a bad block
10803* slot : disk number containing a bad block
10804* sector : bad block sector
10805* length : bad block sectors range
10806* Returns:
10807* 1 : Success
10808* 0 : Error
10809******************************************************************************/
10810static int imsm_record_badblock(struct active_array *a, int slot,
10811 unsigned long long sector, int length)
10812{
10813 struct intel_super *super = a->container->sb;
10814 int ord;
10815 int ret;
10816
10817 ord = imsm_disk_slot_to_ord(a, slot);
10818 if (ord < 0)
10819 return 0;
10820
10821 ret = record_new_badblock(super->bbm_log, ord_to_idx(ord), sector,
10822 length);
10823 if (ret)
10824 super->updates_pending++;
10825
10826 return ret;
10827}
c07a5a4f
TM
10828/*******************************************************************************
10829* Function: imsm_clear_badblock
10830* Description: This routine clears bad block record from BBM log
10831*
10832* Parameters:
10833* a : array containing a bad block
10834* slot : disk number containing a bad block
10835* sector : bad block sector
10836* length : bad block sectors range
10837* Returns:
10838* 1 : Success
10839* 0 : Error
10840******************************************************************************/
10841static int imsm_clear_badblock(struct active_array *a, int slot,
10842 unsigned long long sector, int length)
10843{
10844 struct intel_super *super = a->container->sb;
10845 int ord;
10846 int ret;
10847
10848 ord = imsm_disk_slot_to_ord(a, slot);
10849 if (ord < 0)
10850 return 0;
10851
10852 ret = clear_badblock(super->bbm_log, ord_to_idx(ord), sector, length);
10853 if (ret)
10854 super->updates_pending++;
10855
10856 return ret;
10857}
928f1424
TM
10858/*******************************************************************************
10859* Function: imsm_get_badblocks
10860* Description: This routine get list of bad blocks for an array
10861*
10862* Parameters:
10863* a : array
10864* slot : disk number
10865* Returns:
10866* bb : structure containing bad blocks
10867* NULL : error
10868******************************************************************************/
10869static struct md_bb *imsm_get_badblocks(struct active_array *a, int slot)
10870{
10871 int inst = a->info.container_member;
10872 struct intel_super *super = a->container->sb;
10873 struct imsm_dev *dev = get_imsm_dev(super, inst);
10874 struct imsm_map *map = get_imsm_map(dev, MAP_0);
10875 int ord;
10876
10877 ord = imsm_disk_slot_to_ord(a, slot);
10878 if (ord < 0)
10879 return NULL;
10880
10881 get_volume_badblocks(super->bbm_log, ord_to_idx(ord), pba_of_lba0(map),
44490938 10882 per_dev_array_size(map), &super->bb);
928f1424
TM
10883
10884 return &super->bb;
10885}
27156a57
TM
10886/*******************************************************************************
10887* Function: examine_badblocks_imsm
10888* Description: Prints list of bad blocks on a disk to the standard output
10889*
10890* Parameters:
10891* st : metadata handler
10892* fd : open file descriptor for device
10893* devname : device name
10894* Returns:
10895* 0 : Success
10896* 1 : Error
10897******************************************************************************/
10898static int examine_badblocks_imsm(struct supertype *st, int fd, char *devname)
10899{
10900 struct intel_super *super = st->sb;
10901 struct bbm_log *log = super->bbm_log;
10902 struct dl *d = NULL;
10903 int any = 0;
10904
10905 for (d = super->disks; d ; d = d->next) {
10906 if (strcmp(d->devname, devname) == 0)
10907 break;
10908 }
10909
10910 if ((d == NULL) || (d->index < 0)) { /* serial mismatch probably */
10911 pr_err("%s doesn't appear to be part of a raid array\n",
10912 devname);
10913 return 1;
10914 }
10915
10916 if (log != NULL) {
10917 unsigned int i;
10918 struct bbm_log_entry *entry = &log->marked_block_entries[0];
10919
10920 for (i = 0; i < log->entry_count; i++) {
10921 if (entry[i].disk_ordinal == d->index) {
10922 unsigned long long sector = __le48_to_cpu(
10923 &entry[i].defective_block_start);
10924 int cnt = entry[i].marked_count + 1;
10925
10926 if (!any) {
10927 printf("Bad-blocks on %s:\n", devname);
10928 any = 1;
10929 }
10930
10931 printf("%20llu for %d sectors\n", sector, cnt);
10932 }
10933 }
10934 }
10935
10936 if (!any)
10937 printf("No bad-blocks list configured on %s\n", devname);
10938
10939 return 0;
10940}
687629c2
AK
10941/*******************************************************************************
10942 * Function: init_migr_record_imsm
10943 * Description: Function inits imsm migration record
10944 * Parameters:
10945 * super : imsm internal array info
10946 * dev : device under migration
10947 * info : general array info to find the smallest device
10948 * Returns:
10949 * none
10950 ******************************************************************************/
10951void init_migr_record_imsm(struct supertype *st, struct imsm_dev *dev,
10952 struct mdinfo *info)
10953{
10954 struct intel_super *super = st->sb;
10955 struct migr_record *migr_rec = super->migr_rec;
10956 int new_data_disks;
10957 unsigned long long dsize, dev_sectors;
10958 long long unsigned min_dev_sectors = -1LLU;
238c0a71
AK
10959 struct imsm_map *map_dest = get_imsm_map(dev, MAP_0);
10960 struct imsm_map *map_src = get_imsm_map(dev, MAP_1);
687629c2 10961 unsigned long long num_migr_units;
3ef4403c 10962 unsigned long long array_blocks;
2f86fda3 10963 struct dl *dl_disk = NULL;
687629c2
AK
10964
10965 memset(migr_rec, 0, sizeof(struct migr_record));
10966 migr_rec->family_num = __cpu_to_le32(super->anchor->family_num);
10967
10968 /* only ascending reshape supported now */
10969 migr_rec->ascending_migr = __cpu_to_le32(1);
10970
10971 migr_rec->dest_depth_per_unit = GEN_MIGR_AREA_SIZE /
10972 max(map_dest->blocks_per_strip, map_src->blocks_per_strip);
e1742195
AK
10973 migr_rec->dest_depth_per_unit *=
10974 max(map_dest->blocks_per_strip, map_src->blocks_per_strip);
9529d343 10975 new_data_disks = imsm_num_data_members(map_dest);
687629c2
AK
10976 migr_rec->blocks_per_unit =
10977 __cpu_to_le32(migr_rec->dest_depth_per_unit * new_data_disks);
10978 migr_rec->dest_depth_per_unit =
10979 __cpu_to_le32(migr_rec->dest_depth_per_unit);
3ef4403c 10980 array_blocks = info->component_size * new_data_disks;
687629c2
AK
10981 num_migr_units =
10982 array_blocks / __le32_to_cpu(migr_rec->blocks_per_unit);
10983
10984 if (array_blocks % __le32_to_cpu(migr_rec->blocks_per_unit))
10985 num_migr_units++;
9f421827 10986 set_num_migr_units(migr_rec, num_migr_units);
687629c2
AK
10987
10988 migr_rec->post_migr_vol_cap = dev->size_low;
10989 migr_rec->post_migr_vol_cap_hi = dev->size_high;
10990
687629c2 10991 /* Find the smallest dev */
2f86fda3
MT
10992 for (dl_disk = super->disks; dl_disk ; dl_disk = dl_disk->next) {
10993 /* ignore spares in container */
10994 if (dl_disk->index < 0)
687629c2 10995 continue;
2f86fda3 10996 get_dev_size(dl_disk->fd, NULL, &dsize);
687629c2
AK
10997 dev_sectors = dsize / 512;
10998 if (dev_sectors < min_dev_sectors)
10999 min_dev_sectors = dev_sectors;
687629c2 11000 }
9f421827 11001 set_migr_chkp_area_pba(migr_rec, min_dev_sectors -
687629c2
AK
11002 RAID_DISK_RESERVED_BLOCKS_IMSM_HI);
11003
11004 write_imsm_migr_rec(st);
11005
11006 return;
11007}
11008
11009/*******************************************************************************
11010 * Function: save_backup_imsm
11011 * Description: Function saves critical data stripes to Migration Copy Area
11012 * and updates the current migration unit status.
11013 * Use restore_stripes() to form a destination stripe,
11014 * and to write it to the Copy Area.
11015 * Parameters:
11016 * st : supertype information
aea93171 11017 * dev : imsm device that backup is saved for
687629c2
AK
11018 * info : general array info
11019 * buf : input buffer
687629c2
AK
11020 * length : length of data to backup (blocks_per_unit)
11021 * Returns:
11022 * 0 : success
11023 *, -1 : fail
11024 ******************************************************************************/
11025int save_backup_imsm(struct supertype *st,
11026 struct imsm_dev *dev,
11027 struct mdinfo *info,
11028 void *buf,
687629c2
AK
11029 int length)
11030{
11031 int rv = -1;
11032 struct intel_super *super = st->sb;
687629c2 11033 int i;
238c0a71 11034 struct imsm_map *map_dest = get_imsm_map(dev, MAP_0);
687629c2 11035 int new_disks = map_dest->num_members;
ab724b98 11036 int dest_layout = 0;
4389ce73
MT
11037 int dest_chunk, targets[new_disks];
11038 unsigned long long start, target_offsets[new_disks];
9529d343 11039 int data_disks = imsm_num_data_members(map_dest);
687629c2 11040
2f86fda3
MT
11041 for (i = 0; i < new_disks; i++) {
11042 struct dl *dl_disk = get_imsm_dl_disk(super, i);
4389ce73
MT
11043 if (dl_disk && is_fd_valid(dl_disk->fd))
11044 targets[i] = dl_disk->fd;
11045 else
11046 goto abort;
2f86fda3 11047 }
7e45b550 11048
d1877f69 11049 start = info->reshape_progress * 512;
687629c2 11050 for (i = 0; i < new_disks; i++) {
9f421827 11051 target_offsets[i] = migr_chkp_area_pba(super->migr_rec) * 512;
d1877f69
AK
11052 /* move back copy area adderss, it will be moved forward
11053 * in restore_stripes() using start input variable
11054 */
11055 target_offsets[i] -= start/data_disks;
687629c2
AK
11056 }
11057
68eb8bc6 11058 dest_layout = imsm_level_to_layout(map_dest->raid_level);
ab724b98
AK
11059 dest_chunk = __le16_to_cpu(map_dest->blocks_per_strip) * 512;
11060
687629c2
AK
11061 if (restore_stripes(targets, /* list of dest devices */
11062 target_offsets, /* migration record offsets */
11063 new_disks,
ab724b98
AK
11064 dest_chunk,
11065 map_dest->raid_level,
11066 dest_layout,
11067 -1, /* source backup file descriptor */
11068 0, /* input buf offset
11069 * always 0 buf is already offseted */
d1877f69 11070 start,
687629c2
AK
11071 length,
11072 buf) != 0) {
e7b84f9d 11073 pr_err("Error restoring stripes\n");
687629c2
AK
11074 goto abort;
11075 }
11076
11077 rv = 0;
11078
11079abort:
687629c2
AK
11080 return rv;
11081}
11082
11083/*******************************************************************************
11084 * Function: save_checkpoint_imsm
11085 * Description: Function called for current unit status update
11086 * in the migration record. It writes it to disk.
11087 * Parameters:
11088 * super : imsm internal array info
11089 * info : general array info
11090 * Returns:
11091 * 0: success
11092 * 1: failure
0228d92c
AK
11093 * 2: failure, means no valid migration record
11094 * / no general migration in progress /
687629c2
AK
11095 ******************************************************************************/
11096int save_checkpoint_imsm(struct supertype *st, struct mdinfo *info, int state)
11097{
11098 struct intel_super *super = st->sb;
f8b72ef5
AK
11099 unsigned long long blocks_per_unit;
11100 unsigned long long curr_migr_unit;
11101
2f86fda3 11102 if (load_imsm_migr_rec(super) != 0) {
7a862a02 11103 dprintf("imsm: ERROR: Cannot read migration record for checkpoint save.\n");
2e062e82
AK
11104 return 1;
11105 }
11106
f8b72ef5
AK
11107 blocks_per_unit = __le32_to_cpu(super->migr_rec->blocks_per_unit);
11108 if (blocks_per_unit == 0) {
0228d92c
AK
11109 dprintf("imsm: no migration in progress.\n");
11110 return 2;
687629c2 11111 }
f8b72ef5
AK
11112 curr_migr_unit = info->reshape_progress / blocks_per_unit;
11113 /* check if array is alligned to copy area
11114 * if it is not alligned, add one to current migration unit value
11115 * this can happend on array reshape finish only
11116 */
11117 if (info->reshape_progress % blocks_per_unit)
11118 curr_migr_unit++;
687629c2 11119
9f421827 11120 set_current_migr_unit(super->migr_rec, curr_migr_unit);
687629c2 11121 super->migr_rec->rec_status = __cpu_to_le32(state);
9f421827
PB
11122 set_migr_dest_1st_member_lba(super->migr_rec,
11123 super->migr_rec->dest_depth_per_unit * curr_migr_unit);
11124
687629c2 11125 if (write_imsm_migr_rec(st) < 0) {
7a862a02 11126 dprintf("imsm: Cannot write migration record outside backup area\n");
687629c2
AK
11127 return 1;
11128 }
11129
11130 return 0;
11131}
11132
276d77db
AK
11133/*******************************************************************************
11134 * Function: recover_backup_imsm
11135 * Description: Function recovers critical data from the Migration Copy Area
11136 * while assembling an array.
11137 * Parameters:
11138 * super : imsm internal array info
11139 * info : general array info
11140 * Returns:
11141 * 0 : success (or there is no data to recover)
11142 * 1 : fail
11143 ******************************************************************************/
11144int recover_backup_imsm(struct supertype *st, struct mdinfo *info)
11145{
11146 struct intel_super *super = st->sb;
11147 struct migr_record *migr_rec = super->migr_rec;
594dc1b8 11148 struct imsm_map *map_dest;
276d77db
AK
11149 struct intel_dev *id = NULL;
11150 unsigned long long read_offset;
11151 unsigned long long write_offset;
11152 unsigned unit_len;
2f86fda3 11153 int new_disks, err;
276d77db
AK
11154 char *buf = NULL;
11155 int retval = 1;
f36a9ecd 11156 unsigned int sector_size = super->sector_size;
4036e7ee
MT
11157 unsigned long long curr_migr_unit = current_migr_unit(migr_rec);
11158 unsigned long long num_migr_units = get_num_migr_units(migr_rec);
276d77db 11159 char buffer[20];
6c3560c0 11160 int skipped_disks = 0;
2f86fda3 11161 struct dl *dl_disk;
276d77db
AK
11162
11163 err = sysfs_get_str(info, NULL, "array_state", (char *)buffer, 20);
11164 if (err < 1)
11165 return 1;
11166
11167 /* recover data only during assemblation */
11168 if (strncmp(buffer, "inactive", 8) != 0)
11169 return 0;
11170 /* no data to recover */
11171 if (__le32_to_cpu(migr_rec->rec_status) == UNIT_SRC_NORMAL)
11172 return 0;
11173 if (curr_migr_unit >= num_migr_units)
11174 return 1;
11175
11176 /* find device during reshape */
11177 for (id = super->devlist; id; id = id->next)
11178 if (is_gen_migration(id->dev))
11179 break;
11180 if (id == NULL)
11181 return 1;
11182
238c0a71 11183 map_dest = get_imsm_map(id->dev, MAP_0);
276d77db
AK
11184 new_disks = map_dest->num_members;
11185
9f421827 11186 read_offset = migr_chkp_area_pba(migr_rec) * 512;
276d77db 11187
9f421827 11188 write_offset = (migr_dest_1st_member_lba(migr_rec) +
5551b113 11189 pba_of_lba0(map_dest)) * 512;
276d77db
AK
11190
11191 unit_len = __le32_to_cpu(migr_rec->dest_depth_per_unit) * 512;
f36a9ecd 11192 if (posix_memalign((void **)&buf, sector_size, unit_len) != 0)
276d77db 11193 goto abort;
276d77db 11194
2f86fda3
MT
11195 for (dl_disk = super->disks; dl_disk; dl_disk = dl_disk->next) {
11196 if (dl_disk->index < 0)
11197 continue;
276d77db 11198
4389ce73 11199 if (!is_fd_valid(dl_disk->fd)) {
6c3560c0
AK
11200 skipped_disks++;
11201 continue;
11202 }
2f86fda3 11203 if (lseek64(dl_disk->fd, read_offset, SEEK_SET) < 0) {
e7b84f9d
N
11204 pr_err("Cannot seek to block: %s\n",
11205 strerror(errno));
137debce
AK
11206 skipped_disks++;
11207 continue;
276d77db 11208 }
83b3de77 11209 if (read(dl_disk->fd, buf, unit_len) != (ssize_t)unit_len) {
e7b84f9d
N
11210 pr_err("Cannot read copy area block: %s\n",
11211 strerror(errno));
137debce
AK
11212 skipped_disks++;
11213 continue;
276d77db 11214 }
2f86fda3 11215 if (lseek64(dl_disk->fd, write_offset, SEEK_SET) < 0) {
e7b84f9d
N
11216 pr_err("Cannot seek to block: %s\n",
11217 strerror(errno));
137debce
AK
11218 skipped_disks++;
11219 continue;
276d77db 11220 }
83b3de77 11221 if (write(dl_disk->fd, buf, unit_len) != (ssize_t)unit_len) {
e7b84f9d
N
11222 pr_err("Cannot restore block: %s\n",
11223 strerror(errno));
137debce
AK
11224 skipped_disks++;
11225 continue;
276d77db
AK
11226 }
11227 }
11228
137debce
AK
11229 if (skipped_disks > imsm_get_allowed_degradation(info->new_level,
11230 new_disks,
11231 super,
11232 id->dev)) {
7a862a02 11233 pr_err("Cannot restore data from backup. Too many failed disks\n");
6c3560c0
AK
11234 goto abort;
11235 }
11236
befb629b
AK
11237 if (save_checkpoint_imsm(st, info, UNIT_SRC_NORMAL)) {
11238 /* ignore error == 2, this can mean end of reshape here
11239 */
7a862a02 11240 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL) during restart\n");
befb629b 11241 } else
276d77db 11242 retval = 0;
276d77db
AK
11243
11244abort:
276d77db
AK
11245 free(buf);
11246 return retval;
11247}
11248
2cda7640
ML
11249static char disk_by_path[] = "/dev/disk/by-path/";
11250
11251static const char *imsm_get_disk_controller_domain(const char *path)
11252{
2cda7640 11253 char disk_path[PATH_MAX];
96234762
LM
11254 char *drv=NULL;
11255 struct stat st;
2cda7640 11256
dd5ab402 11257 strncpy(disk_path, disk_by_path, PATH_MAX);
96234762
LM
11258 strncat(disk_path, path, PATH_MAX - strlen(disk_path) - 1);
11259 if (stat(disk_path, &st) == 0) {
11260 struct sys_dev* hba;
594dc1b8 11261 char *path;
96234762 11262
7c798f87 11263 path = devt_to_devpath(st.st_rdev, 1, NULL);
96234762
LM
11264 if (path == NULL)
11265 return "unknown";
11266 hba = find_disk_attached_hba(-1, path);
11267 if (hba && hba->type == SYS_DEV_SAS)
11268 drv = "isci";
75350d87 11269 else if (hba && (hba->type == SYS_DEV_SATA || hba->type == SYS_DEV_SATA_VMD))
96234762 11270 drv = "ahci";
c6839718
MT
11271 else if (hba && hba->type == SYS_DEV_VMD)
11272 drv = "vmd";
11273 else if (hba && hba->type == SYS_DEV_NVME)
11274 drv = "nvme";
1011e834 11275 else
96234762
LM
11276 drv = "unknown";
11277 dprintf("path: %s hba: %s attached: %s\n",
11278 path, (hba) ? hba->path : "NULL", drv);
11279 free(path);
2cda7640 11280 }
96234762 11281 return drv;
2cda7640
ML
11282}
11283
4dd2df09 11284static char *imsm_find_array_devnm_by_subdev(int subdev, char *container)
78b10e66 11285{
4dd2df09 11286 static char devnm[32];
78b10e66
N
11287 char subdev_name[20];
11288 struct mdstat_ent *mdstat;
11289
11290 sprintf(subdev_name, "%d", subdev);
11291 mdstat = mdstat_by_subdev(subdev_name, container);
11292 if (!mdstat)
4dd2df09 11293 return NULL;
78b10e66 11294
4dd2df09 11295 strcpy(devnm, mdstat->devnm);
78b10e66 11296 free_mdstat(mdstat);
4dd2df09 11297 return devnm;
78b10e66
N
11298}
11299
11300static int imsm_reshape_is_allowed_on_container(struct supertype *st,
11301 struct geo_params *geo,
fbf3d202
AK
11302 int *old_raid_disks,
11303 int direction)
78b10e66 11304{
694575e7
KW
11305 /* currently we only support increasing the number of devices
11306 * for a container. This increases the number of device for each
11307 * member array. They must all be RAID0 or RAID5.
11308 */
78b10e66
N
11309 int ret_val = 0;
11310 struct mdinfo *info, *member;
11311 int devices_that_can_grow = 0;
11312
7a862a02 11313 dprintf("imsm: imsm_reshape_is_allowed_on_container(ENTER): st->devnm = (%s)\n", st->devnm);
78b10e66 11314
d04f65f4 11315 if (geo->size > 0 ||
78b10e66
N
11316 geo->level != UnSet ||
11317 geo->layout != UnSet ||
11318 geo->chunksize != 0 ||
11319 geo->raid_disks == UnSet) {
7a862a02 11320 dprintf("imsm: Container operation is allowed for raid disks number change only.\n");
78b10e66
N
11321 return ret_val;
11322 }
11323
fbf3d202 11324 if (direction == ROLLBACK_METADATA_CHANGES) {
7a862a02 11325 dprintf("imsm: Metadata changes rollback is not supported for container operation.\n");
fbf3d202
AK
11326 return ret_val;
11327 }
11328
78b10e66
N
11329 info = container_content_imsm(st, NULL);
11330 for (member = info; member; member = member->next) {
4dd2df09 11331 char *result;
78b10e66
N
11332
11333 dprintf("imsm: checking device_num: %i\n",
11334 member->container_member);
11335
d7d205bd 11336 if (geo->raid_disks <= member->array.raid_disks) {
78b10e66
N
11337 /* we work on container for Online Capacity Expansion
11338 * only so raid_disks has to grow
11339 */
7a862a02 11340 dprintf("imsm: for container operation raid disks increase is required\n");
78b10e66
N
11341 break;
11342 }
11343
089f9d79 11344 if (info->array.level != 0 && info->array.level != 5) {
78b10e66
N
11345 /* we cannot use this container with other raid level
11346 */
7a862a02 11347 dprintf("imsm: for container operation wrong raid level (%i) detected\n",
78b10e66
N
11348 info->array.level);
11349 break;
11350 } else {
11351 /* check for platform support
11352 * for this raid level configuration
11353 */
11354 struct intel_super *super = st->sb;
11355 if (!is_raid_level_supported(super->orom,
11356 member->array.level,
11357 geo->raid_disks)) {
7a862a02 11358 dprintf("platform does not support raid%d with %d disk%s\n",
78b10e66
N
11359 info->array.level,
11360 geo->raid_disks,
11361 geo->raid_disks > 1 ? "s" : "");
11362 break;
11363 }
2a4a08e7
AK
11364 /* check if component size is aligned to chunk size
11365 */
11366 if (info->component_size %
11367 (info->array.chunk_size/512)) {
7a862a02 11368 dprintf("Component size is not aligned to chunk size\n");
2a4a08e7
AK
11369 break;
11370 }
78b10e66
N
11371 }
11372
11373 if (*old_raid_disks &&
11374 info->array.raid_disks != *old_raid_disks)
11375 break;
11376 *old_raid_disks = info->array.raid_disks;
11377
11378 /* All raid5 and raid0 volumes in container
11379 * have to be ready for Online Capacity Expansion
11380 * so they need to be assembled. We have already
11381 * checked that no recovery etc is happening.
11382 */
4dd2df09
N
11383 result = imsm_find_array_devnm_by_subdev(member->container_member,
11384 st->container_devnm);
11385 if (result == NULL) {
78b10e66
N
11386 dprintf("imsm: cannot find array\n");
11387 break;
11388 }
11389 devices_that_can_grow++;
11390 }
11391 sysfs_free(info);
11392 if (!member && devices_that_can_grow)
11393 ret_val = 1;
11394
11395 if (ret_val)
1ade5cc1 11396 dprintf("Container operation allowed\n");
78b10e66 11397 else
1ade5cc1 11398 dprintf("Error: %i\n", ret_val);
78b10e66
N
11399
11400 return ret_val;
11401}
11402
11403/* Function: get_spares_for_grow
11404 * Description: Allocates memory and creates list of spare devices
1011e834 11405 * avaliable in container. Checks if spare drive size is acceptable.
78b10e66
N
11406 * Parameters: Pointer to the supertype structure
11407 * Returns: Pointer to the list of spare devices (mdinfo structure) on success,
1011e834 11408 * NULL if fail
78b10e66
N
11409 */
11410static struct mdinfo *get_spares_for_grow(struct supertype *st)
11411{
fbfdcb06
AO
11412 struct spare_criteria sc;
11413
11414 get_spare_criteria_imsm(st, &sc);
11415 return container_choose_spares(st, &sc, NULL, NULL, NULL, 0);
78b10e66
N
11416}
11417
11418/******************************************************************************
11419 * function: imsm_create_metadata_update_for_reshape
11420 * Function creates update for whole IMSM container.
11421 *
11422 ******************************************************************************/
11423static int imsm_create_metadata_update_for_reshape(
11424 struct supertype *st,
11425 struct geo_params *geo,
11426 int old_raid_disks,
11427 struct imsm_update_reshape **updatep)
11428{
11429 struct intel_super *super = st->sb;
11430 struct imsm_super *mpb = super->anchor;
594dc1b8
JS
11431 int update_memory_size;
11432 struct imsm_update_reshape *u;
11433 struct mdinfo *spares;
78b10e66 11434 int i;
594dc1b8 11435 int delta_disks;
bbd24d86 11436 struct mdinfo *dev;
78b10e66 11437
1ade5cc1 11438 dprintf("(enter) raid_disks = %i\n", geo->raid_disks);
78b10e66
N
11439
11440 delta_disks = geo->raid_disks - old_raid_disks;
11441
11442 /* size of all update data without anchor */
11443 update_memory_size = sizeof(struct imsm_update_reshape);
11444
11445 /* now add space for spare disks that we need to add. */
11446 update_memory_size += sizeof(u->new_disks[0]) * (delta_disks - 1);
11447
503975b9 11448 u = xcalloc(1, update_memory_size);
78b10e66
N
11449 u->type = update_reshape_container_disks;
11450 u->old_raid_disks = old_raid_disks;
11451 u->new_raid_disks = geo->raid_disks;
11452
11453 /* now get spare disks list
11454 */
11455 spares = get_spares_for_grow(st);
11456
d7be7d87 11457 if (spares == NULL || delta_disks > spares->array.spare_disks) {
7a862a02 11458 pr_err("imsm: ERROR: Cannot get spare devices for %s.\n", geo->dev_name);
e4c72d1d 11459 i = -1;
78b10e66
N
11460 goto abort;
11461 }
11462
11463 /* we have got spares
11464 * update disk list in imsm_disk list table in anchor
11465 */
11466 dprintf("imsm: %i spares are available.\n\n",
11467 spares->array.spare_disks);
11468
bbd24d86 11469 dev = spares->devs;
78b10e66 11470 for (i = 0; i < delta_disks; i++) {
78b10e66
N
11471 struct dl *dl;
11472
bbd24d86
AK
11473 if (dev == NULL)
11474 break;
78b10e66
N
11475 u->new_disks[i] = makedev(dev->disk.major,
11476 dev->disk.minor);
11477 dl = get_disk_super(super, dev->disk.major, dev->disk.minor);
ee4beede
AK
11478 dl->index = mpb->num_disks;
11479 mpb->num_disks++;
bbd24d86 11480 dev = dev->next;
78b10e66 11481 }
78b10e66
N
11482
11483abort:
11484 /* free spares
11485 */
11486 sysfs_free(spares);
11487
d677e0b8 11488 dprintf("imsm: reshape update preparation :");
78b10e66 11489 if (i == delta_disks) {
1ade5cc1 11490 dprintf_cont(" OK\n");
78b10e66
N
11491 *updatep = u;
11492 return update_memory_size;
11493 }
11494 free(u);
1ade5cc1 11495 dprintf_cont(" Error\n");
78b10e66
N
11496
11497 return 0;
11498}
11499
f3871fdc
AK
11500/******************************************************************************
11501 * function: imsm_create_metadata_update_for_size_change()
11502 * Creates update for IMSM array for array size change.
11503 *
11504 ******************************************************************************/
11505static int imsm_create_metadata_update_for_size_change(
11506 struct supertype *st,
11507 struct geo_params *geo,
11508 struct imsm_update_size_change **updatep)
11509{
11510 struct intel_super *super = st->sb;
594dc1b8
JS
11511 int update_memory_size;
11512 struct imsm_update_size_change *u;
f3871fdc 11513
1ade5cc1 11514 dprintf("(enter) New size = %llu\n", geo->size);
f3871fdc
AK
11515
11516 /* size of all update data without anchor */
11517 update_memory_size = sizeof(struct imsm_update_size_change);
11518
503975b9 11519 u = xcalloc(1, update_memory_size);
f3871fdc
AK
11520 u->type = update_size_change;
11521 u->subdev = super->current_vol;
11522 u->new_size = geo->size;
11523
11524 dprintf("imsm: reshape update preparation : OK\n");
11525 *updatep = u;
11526
11527 return update_memory_size;
11528}
11529
48c5303a
PC
11530/******************************************************************************
11531 * function: imsm_create_metadata_update_for_migration()
11532 * Creates update for IMSM array.
11533 *
11534 ******************************************************************************/
11535static int imsm_create_metadata_update_for_migration(
11536 struct supertype *st,
11537 struct geo_params *geo,
11538 struct imsm_update_reshape_migration **updatep)
11539{
11540 struct intel_super *super = st->sb;
594dc1b8 11541 int update_memory_size;
756a15f3 11542 int current_chunk_size;
594dc1b8 11543 struct imsm_update_reshape_migration *u;
756a15f3
MG
11544 struct imsm_dev *dev = get_imsm_dev(super, super->current_vol);
11545 struct imsm_map *map = get_imsm_map(dev, MAP_0);
48c5303a
PC
11546 int previous_level = -1;
11547
1ade5cc1 11548 dprintf("(enter) New Level = %i\n", geo->level);
48c5303a
PC
11549
11550 /* size of all update data without anchor */
11551 update_memory_size = sizeof(struct imsm_update_reshape_migration);
11552
503975b9 11553 u = xcalloc(1, update_memory_size);
48c5303a
PC
11554 u->type = update_reshape_migration;
11555 u->subdev = super->current_vol;
11556 u->new_level = geo->level;
11557 u->new_layout = geo->layout;
11558 u->new_raid_disks = u->old_raid_disks = geo->raid_disks;
11559 u->new_disks[0] = -1;
4bba0439 11560 u->new_chunksize = -1;
48c5303a 11561
756a15f3 11562 current_chunk_size = __le16_to_cpu(map->blocks_per_strip) / 2;
48c5303a 11563
756a15f3
MG
11564 if (geo->chunksize != current_chunk_size) {
11565 u->new_chunksize = geo->chunksize / 1024;
11566 dprintf("imsm: chunk size change from %i to %i\n",
11567 current_chunk_size, u->new_chunksize);
48c5303a 11568 }
756a15f3
MG
11569 previous_level = map->raid_level;
11570
089f9d79 11571 if (geo->level == 5 && previous_level == 0) {
48c5303a
PC
11572 struct mdinfo *spares = NULL;
11573
11574 u->new_raid_disks++;
11575 spares = get_spares_for_grow(st);
089f9d79 11576 if (spares == NULL || spares->array.spare_disks < 1) {
48c5303a
PC
11577 free(u);
11578 sysfs_free(spares);
11579 update_memory_size = 0;
565cc99e 11580 pr_err("cannot get spare device for requested migration\n");
48c5303a
PC
11581 return 0;
11582 }
11583 sysfs_free(spares);
11584 }
11585 dprintf("imsm: reshape update preparation : OK\n");
11586 *updatep = u;
11587
11588 return update_memory_size;
11589}
11590
8dd70bce
AK
11591static void imsm_update_metadata_locally(struct supertype *st,
11592 void *buf, int len)
11593{
11594 struct metadata_update mu;
11595
11596 mu.buf = buf;
11597 mu.len = len;
11598 mu.space = NULL;
11599 mu.space_list = NULL;
11600 mu.next = NULL;
5fe6f031
N
11601 if (imsm_prepare_update(st, &mu))
11602 imsm_process_update(st, &mu);
8dd70bce
AK
11603
11604 while (mu.space_list) {
11605 void **space = mu.space_list;
11606 mu.space_list = *space;
11607 free(space);
11608 }
11609}
78b10e66 11610
cbaa7904
MT
11611/**
11612 * imsm_analyze_expand() - check expand properties and calculate new size.
11613 * @st: imsm supertype.
11614 * @geo: new geometry params.
11615 * @array: array info.
11616 * @direction: reshape direction.
11617 *
11618 * Obtain free space after the &array and verify if expand to requested size is
11619 * possible. If geo->size is set to %MAX_SIZE, assume that max free size is
11620 * requested.
11621 *
11622 * Return:
11623 * On success %IMSM_STATUS_OK is returned, geo->size and geo->raid_disks are
11624 * updated.
11625 * On error, %IMSM_STATUS_ERROR is returned.
11626 */
11627static imsm_status_t imsm_analyze_expand(struct supertype *st,
11628 struct geo_params *geo,
11629 struct mdinfo *array,
11630 int direction)
11631{
11632 struct intel_super *super = st->sb;
11633 struct imsm_dev *dev = get_imsm_dev(super, super->current_vol);
11634 struct imsm_map *map = get_imsm_map(dev, MAP_0);
11635 int data_disks = imsm_num_data_members(map);
11636
11637 unsigned long long current_size;
11638 unsigned long long free_size;
11639 unsigned long long new_size;
11640 unsigned long long max_size;
11641
11642 const int chunk_kib = geo->chunksize / 1024;
11643 imsm_status_t rv;
11644
11645 if (direction == ROLLBACK_METADATA_CHANGES) {
11646 /**
11647 * Accept size for rollback only.
11648 */
11649 new_size = geo->size * 2;
11650 goto success;
11651 }
11652
cbaa7904
MT
11653 if (data_disks == 0) {
11654 pr_err("imsm: Cannot retrieve data disks.\n");
11655 return IMSM_STATUS_ERROR;
11656 }
11657 current_size = array->custom_array_size / data_disks;
11658
aa19fdd4 11659 rv = imsm_get_free_size(super, dev->vol.map->num_members, 0, chunk_kib, &free_size, true);
cbaa7904
MT
11660 if (rv != IMSM_STATUS_OK) {
11661 pr_err("imsm: Cannot find free space for expand.\n");
11662 return IMSM_STATUS_ERROR;
11663 }
11664 max_size = round_member_size_to_mb(free_size + current_size);
11665
11666 if (geo->size == MAX_SIZE)
11667 new_size = max_size;
11668 else
11669 new_size = round_member_size_to_mb(geo->size * 2);
11670
11671 if (new_size == 0) {
11672 pr_err("imsm: Rounded requested size is 0.\n");
11673 return IMSM_STATUS_ERROR;
11674 }
11675
11676 if (new_size > max_size) {
11677 pr_err("imsm: Rounded requested size (%llu) is larger than free space available (%llu).\n",
11678 new_size, max_size);
11679 return IMSM_STATUS_ERROR;
11680 }
11681
11682 if (new_size == current_size) {
11683 pr_err("imsm: Rounded requested size (%llu) is same as current size (%llu).\n",
11684 new_size, current_size);
11685 return IMSM_STATUS_ERROR;
11686 }
11687
11688 if (new_size < current_size) {
11689 pr_err("imsm: Size reduction is not supported, rounded requested size (%llu) is smaller than current (%llu).\n",
11690 new_size, current_size);
11691 return IMSM_STATUS_ERROR;
11692 }
11693
11694success:
11695 dprintf("imsm: New size per member is %llu.\n", new_size);
11696 geo->size = data_disks * new_size;
11697 geo->raid_disks = dev->vol.map->num_members;
11698 return IMSM_STATUS_OK;
11699}
11700
471bceb6 11701/***************************************************************************
694575e7 11702* Function: imsm_analyze_change
471bceb6 11703* Description: Function analyze change for single volume
1011e834 11704* and validate if transition is supported
fbf3d202
AK
11705* Parameters: Geometry parameters, supertype structure,
11706* metadata change direction (apply/rollback)
694575e7 11707* Returns: Operation type code on success, -1 if fail
471bceb6
KW
11708****************************************************************************/
11709enum imsm_reshape_type imsm_analyze_change(struct supertype *st,
fbf3d202
AK
11710 struct geo_params *geo,
11711 int direction)
694575e7 11712{
471bceb6
KW
11713 struct mdinfo info;
11714 int change = -1;
11715 int check_devs = 0;
c21e737b 11716 int chunk;
67a2db32
AK
11717 /* number of added/removed disks in operation result */
11718 int devNumChange = 0;
11719 /* imsm compatible layout value for array geometry verification */
11720 int imsm_layout = -1;
6d4d9ab2 11721 imsm_status_t rv;
471bceb6
KW
11722
11723 getinfo_super_imsm_volume(st, &info, NULL);
089f9d79
JS
11724 if (geo->level != info.array.level && geo->level >= 0 &&
11725 geo->level != UnSet) {
471bceb6
KW
11726 switch (info.array.level) {
11727 case 0:
11728 if (geo->level == 5) {
b5347799 11729 change = CH_MIGRATION;
e13ce846 11730 if (geo->layout != ALGORITHM_LEFT_ASYMMETRIC) {
7a862a02 11731 pr_err("Error. Requested Layout not supported (left-asymmetric layout is supported only)!\n");
e13ce846
AK
11732 change = -1;
11733 goto analyse_change_exit;
11734 }
67a2db32 11735 imsm_layout = geo->layout;
471bceb6 11736 check_devs = 1;
e91a3bad
LM
11737 devNumChange = 1; /* parity disk added */
11738 } else if (geo->level == 10) {
471bceb6
KW
11739 change = CH_TAKEOVER;
11740 check_devs = 1;
e91a3bad 11741 devNumChange = 2; /* two mirrors added */
67a2db32 11742 imsm_layout = 0x102; /* imsm supported layout */
471bceb6 11743 }
dfe77a9e
KW
11744 break;
11745 case 1:
471bceb6
KW
11746 case 10:
11747 if (geo->level == 0) {
11748 change = CH_TAKEOVER;
11749 check_devs = 1;
e91a3bad 11750 devNumChange = -(geo->raid_disks/2);
67a2db32 11751 imsm_layout = 0; /* imsm raid0 layout */
471bceb6
KW
11752 }
11753 break;
11754 }
11755 if (change == -1) {
7a862a02 11756 pr_err("Error. Level Migration from %d to %d not supported!\n",
e7b84f9d 11757 info.array.level, geo->level);
471bceb6
KW
11758 goto analyse_change_exit;
11759 }
11760 } else
11761 geo->level = info.array.level;
11762
089f9d79
JS
11763 if (geo->layout != info.array.layout &&
11764 (geo->layout != UnSet && geo->layout != -1)) {
b5347799 11765 change = CH_MIGRATION;
089f9d79
JS
11766 if (info.array.layout == 0 && info.array.level == 5 &&
11767 geo->layout == 5) {
471bceb6 11768 /* reshape 5 -> 4 */
089f9d79
JS
11769 } else if (info.array.layout == 5 && info.array.level == 5 &&
11770 geo->layout == 0) {
471bceb6
KW
11771 /* reshape 4 -> 5 */
11772 geo->layout = 0;
11773 geo->level = 5;
11774 } else {
7a862a02 11775 pr_err("Error. Layout Migration from %d to %d not supported!\n",
e7b84f9d 11776 info.array.layout, geo->layout);
471bceb6
KW
11777 change = -1;
11778 goto analyse_change_exit;
11779 }
67a2db32 11780 } else {
471bceb6 11781 geo->layout = info.array.layout;
67a2db32
AK
11782 if (imsm_layout == -1)
11783 imsm_layout = info.array.layout;
11784 }
471bceb6 11785
089f9d79
JS
11786 if (geo->chunksize > 0 && geo->chunksize != UnSet &&
11787 geo->chunksize != info.array.chunk_size) {
2d2b0eb7
MD
11788 if (info.array.level == 10) {
11789 pr_err("Error. Chunk size change for RAID 10 is not supported.\n");
11790 change = -1;
11791 goto analyse_change_exit;
1e9b2c3f
PB
11792 } else if (info.component_size % (geo->chunksize/512)) {
11793 pr_err("New chunk size (%dK) does not evenly divide device size (%lluk). Aborting...\n",
11794 geo->chunksize/1024, info.component_size/2);
11795 change = -1;
11796 goto analyse_change_exit;
2d2b0eb7 11797 }
b5347799 11798 change = CH_MIGRATION;
2d2b0eb7 11799 } else {
471bceb6 11800 geo->chunksize = info.array.chunk_size;
2d2b0eb7 11801 }
471bceb6 11802
cbaa7904 11803 if (geo->size > 0) {
7abc9871 11804 if (change != -1) {
7a862a02 11805 pr_err("Error. Size change should be the only one at a time.\n");
7abc9871
AK
11806 change = -1;
11807 goto analyse_change_exit;
11808 }
6d4d9ab2 11809
cbaa7904 11810 rv = imsm_analyze_expand(st, geo, &info, direction);
6d4d9ab2 11811 if (rv != IMSM_STATUS_OK)
cbaa7904 11812 goto analyse_change_exit;
7abc9871
AK
11813 change = CH_ARRAY_SIZE;
11814 }
cbaa7904
MT
11815
11816 chunk = geo->chunksize / 1024;
471bceb6
KW
11817 if (!validate_geometry_imsm(st,
11818 geo->level,
67a2db32 11819 imsm_layout,
e91a3bad 11820 geo->raid_disks + devNumChange,
c21e737b 11821 &chunk,
af4348dd 11822 geo->size, INVALID_SECTORS,
5308f117 11823 0, 0, info.consistency_policy, 1))
471bceb6
KW
11824 change = -1;
11825
11826 if (check_devs) {
11827 struct intel_super *super = st->sb;
11828 struct imsm_super *mpb = super->anchor;
11829
11830 if (mpb->num_raid_devs > 1) {
f1cc8ab9
LF
11831 pr_err("Error. Cannot perform operation on %s- for this operation "
11832 "it MUST be single array in container\n", geo->dev_name);
471bceb6
KW
11833 change = -1;
11834 }
11835 }
11836
11837analyse_change_exit:
089f9d79
JS
11838 if (direction == ROLLBACK_METADATA_CHANGES &&
11839 (change == CH_MIGRATION || change == CH_TAKEOVER)) {
7a862a02 11840 dprintf("imsm: Metadata changes rollback is not supported for migration and takeover operations.\n");
fbf3d202
AK
11841 change = -1;
11842 }
471bceb6 11843 return change;
694575e7
KW
11844}
11845
bb025c2f
KW
11846int imsm_takeover(struct supertype *st, struct geo_params *geo)
11847{
11848 struct intel_super *super = st->sb;
11849 struct imsm_update_takeover *u;
11850
503975b9 11851 u = xmalloc(sizeof(struct imsm_update_takeover));
bb025c2f
KW
11852
11853 u->type = update_takeover;
11854 u->subarray = super->current_vol;
11855
11856 /* 10->0 transition */
11857 if (geo->level == 0)
11858 u->direction = R10_TO_R0;
11859
0529c688
KW
11860 /* 0->10 transition */
11861 if (geo->level == 10)
11862 u->direction = R0_TO_R10;
11863
bb025c2f
KW
11864 /* update metadata locally */
11865 imsm_update_metadata_locally(st, u,
11866 sizeof(struct imsm_update_takeover));
11867 /* and possibly remotely */
11868 if (st->update_tail)
11869 append_metadata_update(st, u,
11870 sizeof(struct imsm_update_takeover));
11871 else
11872 free(u);
11873
11874 return 0;
11875}
11876
895ffd99
MT
11877/* Flush size update if size calculated by num_data_stripes is higher than
11878 * imsm_dev_size to eliminate differences during reshape.
11879 * Mdmon will recalculate them correctly.
11880 * If subarray index is not set then check whole container.
11881 * Returns:
11882 * 0 - no error occurred
11883 * 1 - error detected
11884 */
11885static int imsm_fix_size_mismatch(struct supertype *st, int subarray_index)
11886{
11887 struct intel_super *super = st->sb;
11888 int tmp = super->current_vol;
11889 int ret_val = 1;
11890 int i;
11891
11892 for (i = 0; i < super->anchor->num_raid_devs; i++) {
11893 if (subarray_index >= 0 && i != subarray_index)
11894 continue;
11895 super->current_vol = i;
11896 struct imsm_dev *dev = get_imsm_dev(super, super->current_vol);
11897 struct imsm_map *map = get_imsm_map(dev, MAP_0);
11898 unsigned int disc_count = imsm_num_data_members(map);
11899 struct geo_params geo;
11900 struct imsm_update_size_change *update;
11901 unsigned long long calc_size = per_dev_array_size(map) * disc_count;
11902 unsigned long long d_size = imsm_dev_size(dev);
11903 int u_size;
11904
42e02e61 11905 if (calc_size == d_size)
895ffd99
MT
11906 continue;
11907
ff904202
MT
11908 /* There is a difference, confirm that imsm_dev_size is
11909 * smaller and push update.
895ffd99 11910 */
ff904202
MT
11911 if (d_size > calc_size) {
11912 pr_err("imsm: dev size of subarray %d is incorrect\n",
11913 i);
895ffd99
MT
11914 goto exit;
11915 }
11916 memset(&geo, 0, sizeof(struct geo_params));
11917 geo.size = d_size;
11918 u_size = imsm_create_metadata_update_for_size_change(st, &geo,
11919 &update);
895ffd99
MT
11920 imsm_update_metadata_locally(st, update, u_size);
11921 if (st->update_tail) {
11922 append_metadata_update(st, update, u_size);
11923 flush_metadata_updates(st);
11924 st->update_tail = &st->updates;
11925 } else {
11926 imsm_sync_metadata(st);
5ce5a15f 11927 free(update);
895ffd99
MT
11928 }
11929 }
11930 ret_val = 0;
11931exit:
11932 super->current_vol = tmp;
11933 return ret_val;
11934}
11935
d04f65f4
N
11936static int imsm_reshape_super(struct supertype *st, unsigned long long size,
11937 int level,
78b10e66 11938 int layout, int chunksize, int raid_disks,
41784c88 11939 int delta_disks, char *backup, char *dev,
016e00f5 11940 int direction, int verbose)
78b10e66 11941{
78b10e66
N
11942 int ret_val = 1;
11943 struct geo_params geo;
11944
1ade5cc1 11945 dprintf("(enter)\n");
78b10e66 11946
71204a50 11947 memset(&geo, 0, sizeof(struct geo_params));
78b10e66
N
11948
11949 geo.dev_name = dev;
4dd2df09 11950 strcpy(geo.devnm, st->devnm);
78b10e66
N
11951 geo.size = size;
11952 geo.level = level;
11953 geo.layout = layout;
11954 geo.chunksize = chunksize;
11955 geo.raid_disks = raid_disks;
41784c88
AK
11956 if (delta_disks != UnSet)
11957 geo.raid_disks += delta_disks;
78b10e66 11958
1ade5cc1
N
11959 dprintf("for level : %i\n", geo.level);
11960 dprintf("for raid_disks : %i\n", geo.raid_disks);
78b10e66 11961
4dd2df09 11962 if (strcmp(st->container_devnm, st->devnm) == 0) {
694575e7
KW
11963 /* On container level we can only increase number of devices. */
11964 dprintf("imsm: info: Container operation\n");
78b10e66 11965 int old_raid_disks = 0;
6dc0be30 11966
78b10e66 11967 if (imsm_reshape_is_allowed_on_container(
fbf3d202 11968 st, &geo, &old_raid_disks, direction)) {
78b10e66
N
11969 struct imsm_update_reshape *u = NULL;
11970 int len;
11971
895ffd99
MT
11972 if (imsm_fix_size_mismatch(st, -1)) {
11973 dprintf("imsm: Cannot fix size mismatch\n");
11974 goto exit_imsm_reshape_super;
11975 }
11976
78b10e66
N
11977 len = imsm_create_metadata_update_for_reshape(
11978 st, &geo, old_raid_disks, &u);
11979
ed08d51c
AK
11980 if (len <= 0) {
11981 dprintf("imsm: Cannot prepare update\n");
11982 goto exit_imsm_reshape_super;
11983 }
11984
8dd70bce
AK
11985 ret_val = 0;
11986 /* update metadata locally */
11987 imsm_update_metadata_locally(st, u, len);
11988 /* and possibly remotely */
11989 if (st->update_tail)
11990 append_metadata_update(st, u, len);
11991 else
ed08d51c 11992 free(u);
8dd70bce 11993
694575e7 11994 } else {
7a862a02 11995 pr_err("(imsm) Operation is not allowed on this container\n");
694575e7
KW
11996 }
11997 } else {
11998 /* On volume level we support following operations
471bceb6
KW
11999 * - takeover: raid10 -> raid0; raid0 -> raid10
12000 * - chunk size migration
12001 * - migration: raid5 -> raid0; raid0 -> raid5
12002 */
12003 struct intel_super *super = st->sb;
12004 struct intel_dev *dev = super->devlist;
4dd2df09 12005 int change;
694575e7 12006 dprintf("imsm: info: Volume operation\n");
471bceb6
KW
12007 /* find requested device */
12008 while (dev) {
1011e834 12009 char *devnm =
4dd2df09
N
12010 imsm_find_array_devnm_by_subdev(
12011 dev->index, st->container_devnm);
12012 if (devnm && strcmp(devnm, geo.devnm) == 0)
471bceb6
KW
12013 break;
12014 dev = dev->next;
12015 }
12016 if (dev == NULL) {
4dd2df09
N
12017 pr_err("Cannot find %s (%s) subarray\n",
12018 geo.dev_name, geo.devnm);
471bceb6
KW
12019 goto exit_imsm_reshape_super;
12020 }
12021 super->current_vol = dev->index;
fbf3d202 12022 change = imsm_analyze_change(st, &geo, direction);
694575e7 12023 switch (change) {
471bceb6 12024 case CH_TAKEOVER:
bb025c2f 12025 ret_val = imsm_takeover(st, &geo);
694575e7 12026 break;
48c5303a
PC
12027 case CH_MIGRATION: {
12028 struct imsm_update_reshape_migration *u = NULL;
12029 int len =
12030 imsm_create_metadata_update_for_migration(
12031 st, &geo, &u);
12032 if (len < 1) {
7a862a02 12033 dprintf("imsm: Cannot prepare update\n");
48c5303a
PC
12034 break;
12035 }
471bceb6 12036 ret_val = 0;
48c5303a
PC
12037 /* update metadata locally */
12038 imsm_update_metadata_locally(st, u, len);
12039 /* and possibly remotely */
12040 if (st->update_tail)
12041 append_metadata_update(st, u, len);
12042 else
12043 free(u);
12044 }
12045 break;
7abc9871 12046 case CH_ARRAY_SIZE: {
f3871fdc
AK
12047 struct imsm_update_size_change *u = NULL;
12048 int len =
12049 imsm_create_metadata_update_for_size_change(
12050 st, &geo, &u);
12051 if (len < 1) {
7a862a02 12052 dprintf("imsm: Cannot prepare update\n");
f3871fdc
AK
12053 break;
12054 }
12055 ret_val = 0;
12056 /* update metadata locally */
12057 imsm_update_metadata_locally(st, u, len);
12058 /* and possibly remotely */
12059 if (st->update_tail)
12060 append_metadata_update(st, u, len);
12061 else
12062 free(u);
7abc9871
AK
12063 }
12064 break;
471bceb6
KW
12065 default:
12066 ret_val = 1;
694575e7 12067 }
694575e7 12068 }
78b10e66 12069
ed08d51c 12070exit_imsm_reshape_super:
78b10e66
N
12071 dprintf("imsm: reshape_super Exit code = %i\n", ret_val);
12072 return ret_val;
12073}
2cda7640 12074
0febb20c
AO
12075#define COMPLETED_OK 0
12076#define COMPLETED_NONE 1
12077#define COMPLETED_DELAYED 2
12078
12079static int read_completed(int fd, unsigned long long *val)
12080{
12081 int ret;
12082 char buf[50];
12083
12084 ret = sysfs_fd_get_str(fd, buf, 50);
12085 if (ret < 0)
12086 return ret;
12087
12088 ret = COMPLETED_OK;
12089 if (strncmp(buf, "none", 4) == 0) {
12090 ret = COMPLETED_NONE;
12091 } else if (strncmp(buf, "delayed", 7) == 0) {
12092 ret = COMPLETED_DELAYED;
12093 } else {
12094 char *ep;
12095 *val = strtoull(buf, &ep, 0);
12096 if (ep == buf || (*ep != 0 && *ep != '\n' && *ep != ' '))
12097 ret = -1;
12098 }
12099 return ret;
12100}
12101
eee67a47
AK
12102/*******************************************************************************
12103 * Function: wait_for_reshape_imsm
12104 * Description: Function writes new sync_max value and waits until
12105 * reshape process reach new position
12106 * Parameters:
12107 * sra : general array info
eee67a47
AK
12108 * ndata : number of disks in new array's layout
12109 * Returns:
12110 * 0 : success,
12111 * 1 : there is no reshape in progress,
12112 * -1 : fail
12113 ******************************************************************************/
ae9f01f8 12114int wait_for_reshape_imsm(struct mdinfo *sra, int ndata)
eee67a47 12115{
85ca499c 12116 int fd = sysfs_get_fd(sra, NULL, "sync_completed");
df2647fa 12117 int retry = 3;
eee67a47 12118 unsigned long long completed;
ae9f01f8
AK
12119 /* to_complete : new sync_max position */
12120 unsigned long long to_complete = sra->reshape_progress;
12121 unsigned long long position_to_set = to_complete / ndata;
eee67a47 12122
4389ce73 12123 if (!is_fd_valid(fd)) {
1ade5cc1 12124 dprintf("cannot open reshape_position\n");
eee67a47 12125 return 1;
ae9f01f8 12126 }
eee67a47 12127
df2647fa
PB
12128 do {
12129 if (sysfs_fd_get_ll(fd, &completed) < 0) {
12130 if (!retry) {
12131 dprintf("cannot read reshape_position (no reshape in progres)\n");
12132 close(fd);
12133 return 1;
12134 }
239b3cc0 12135 sleep_for(0, MSEC_TO_NSEC(30), true);
df2647fa
PB
12136 } else
12137 break;
12138 } while (retry--);
eee67a47 12139
85ca499c 12140 if (completed > position_to_set) {
1ade5cc1 12141 dprintf("wrong next position to set %llu (%llu)\n",
85ca499c 12142 to_complete, position_to_set);
ae9f01f8
AK
12143 close(fd);
12144 return -1;
12145 }
12146 dprintf("Position set: %llu\n", position_to_set);
12147 if (sysfs_set_num(sra, NULL, "sync_max",
12148 position_to_set) != 0) {
1ade5cc1 12149 dprintf("cannot set reshape position to %llu\n",
ae9f01f8
AK
12150 position_to_set);
12151 close(fd);
12152 return -1;
eee67a47
AK
12153 }
12154
eee67a47 12155 do {
0febb20c 12156 int rc;
eee67a47 12157 char action[20];
5ff3a780 12158 int timeout = 3000;
0febb20c 12159
5ff3a780 12160 sysfs_wait(fd, &timeout);
a47e44fb
AK
12161 if (sysfs_get_str(sra, NULL, "sync_action",
12162 action, 20) > 0 &&
d7d3809a 12163 strncmp(action, "reshape", 7) != 0) {
b2be2b62
AO
12164 if (strncmp(action, "idle", 4) == 0)
12165 break;
d7d3809a
AP
12166 close(fd);
12167 return -1;
12168 }
0febb20c
AO
12169
12170 rc = read_completed(fd, &completed);
12171 if (rc < 0) {
1ade5cc1 12172 dprintf("cannot read reshape_position (in loop)\n");
eee67a47
AK
12173 close(fd);
12174 return 1;
0febb20c
AO
12175 } else if (rc == COMPLETED_NONE)
12176 break;
85ca499c 12177 } while (completed < position_to_set);
b2be2b62 12178
eee67a47
AK
12179 close(fd);
12180 return 0;
eee67a47
AK
12181}
12182
b915c95f
AK
12183/*******************************************************************************
12184 * Function: check_degradation_change
12185 * Description: Check that array hasn't become failed.
12186 * Parameters:
12187 * info : for sysfs access
12188 * sources : source disks descriptors
12189 * degraded: previous degradation level
12190 * Returns:
12191 * degradation level
12192 ******************************************************************************/
12193int check_degradation_change(struct mdinfo *info,
12194 int *sources,
12195 int degraded)
12196{
12197 unsigned long long new_degraded;
e1993023
LD
12198 int rv;
12199
12200 rv = sysfs_get_ll(info, NULL, "degraded", &new_degraded);
089f9d79 12201 if (rv == -1 || (new_degraded != (unsigned long long)degraded)) {
b915c95f
AK
12202 /* check each device to ensure it is still working */
12203 struct mdinfo *sd;
12204 new_degraded = 0;
12205 for (sd = info->devs ; sd ; sd = sd->next) {
12206 if (sd->disk.state & (1<<MD_DISK_FAULTY))
12207 continue;
12208 if (sd->disk.state & (1<<MD_DISK_SYNC)) {
cf52eff5 12209 char sbuf[100];
4389ce73 12210 int raid_disk = sd->disk.raid_disk;
cf52eff5 12211
b915c95f 12212 if (sysfs_get_str(info,
cf52eff5 12213 sd, "state", sbuf, sizeof(sbuf)) < 0 ||
b915c95f
AK
12214 strstr(sbuf, "faulty") ||
12215 strstr(sbuf, "in_sync") == NULL) {
12216 /* this device is dead */
12217 sd->disk.state = (1<<MD_DISK_FAULTY);
4389ce73
MT
12218 if (raid_disk >= 0)
12219 close_fd(&sources[raid_disk]);
b915c95f
AK
12220 new_degraded++;
12221 }
12222 }
12223 }
12224 }
12225
12226 return new_degraded;
12227}
12228
10f22854
AK
12229/*******************************************************************************
12230 * Function: imsm_manage_reshape
12231 * Description: Function finds array under reshape and it manages reshape
12232 * process. It creates stripes backups (if required) and sets
942e1cdb 12233 * checkpoints.
10f22854
AK
12234 * Parameters:
12235 * afd : Backup handle (nattive) - not used
12236 * sra : general array info
12237 * reshape : reshape parameters - not used
12238 * st : supertype structure
12239 * blocks : size of critical section [blocks]
12240 * fds : table of source device descriptor
12241 * offsets : start of array (offest per devices)
12242 * dests : not used
12243 * destfd : table of destination device descriptor
12244 * destoffsets : table of destination offsets (per device)
12245 * Returns:
12246 * 1 : success, reshape is done
12247 * 0 : fail
12248 ******************************************************************************/
999b4972
N
12249static int imsm_manage_reshape(
12250 int afd, struct mdinfo *sra, struct reshape *reshape,
10f22854 12251 struct supertype *st, unsigned long backup_blocks,
999b4972
N
12252 int *fds, unsigned long long *offsets,
12253 int dests, int *destfd, unsigned long long *destoffsets)
12254{
10f22854
AK
12255 int ret_val = 0;
12256 struct intel_super *super = st->sb;
594dc1b8 12257 struct intel_dev *dv;
de44e46f 12258 unsigned int sector_size = super->sector_size;
10f22854 12259 struct imsm_dev *dev = NULL;
9529d343 12260 struct imsm_map *map_src, *map_dest;
10f22854
AK
12261 int migr_vol_qan = 0;
12262 int ndata, odata; /* [bytes] */
12263 int chunk; /* [bytes] */
12264 struct migr_record *migr_rec;
12265 char *buf = NULL;
12266 unsigned int buf_size; /* [bytes] */
12267 unsigned long long max_position; /* array size [bytes] */
12268 unsigned long long next_step; /* [blocks]/[bytes] */
12269 unsigned long long old_data_stripe_length;
10f22854
AK
12270 unsigned long long start_src; /* [bytes] */
12271 unsigned long long start; /* [bytes] */
12272 unsigned long long start_buf_shift; /* [bytes] */
b915c95f 12273 int degraded = 0;
ab724b98 12274 int source_layout = 0;
895ffd99 12275 int subarray_index = -1;
10f22854 12276
79a16a9b
JS
12277 if (!sra)
12278 return ret_val;
12279
12280 if (!fds || !offsets)
10f22854
AK
12281 goto abort;
12282
12283 /* Find volume during the reshape */
12284 for (dv = super->devlist; dv; dv = dv->next) {
fc54fe7a
JS
12285 if (dv->dev->vol.migr_type == MIGR_GEN_MIGR &&
12286 dv->dev->vol.migr_state == 1) {
10f22854
AK
12287 dev = dv->dev;
12288 migr_vol_qan++;
895ffd99 12289 subarray_index = dv->index;
10f22854
AK
12290 }
12291 }
12292 /* Only one volume can migrate at the same time */
12293 if (migr_vol_qan != 1) {
676e87a8 12294 pr_err("%s", migr_vol_qan ?
10f22854
AK
12295 "Number of migrating volumes greater than 1\n" :
12296 "There is no volume during migrationg\n");
12297 goto abort;
12298 }
12299
9529d343 12300 map_dest = get_imsm_map(dev, MAP_0);
238c0a71 12301 map_src = get_imsm_map(dev, MAP_1);
10f22854
AK
12302 if (map_src == NULL)
12303 goto abort;
10f22854 12304
9529d343
MD
12305 ndata = imsm_num_data_members(map_dest);
12306 odata = imsm_num_data_members(map_src);
10f22854 12307
7b1ab482 12308 chunk = __le16_to_cpu(map_src->blocks_per_strip) * 512;
10f22854
AK
12309 old_data_stripe_length = odata * chunk;
12310
12311 migr_rec = super->migr_rec;
12312
10f22854
AK
12313 /* initialize migration record for start condition */
12314 if (sra->reshape_progress == 0)
12315 init_migr_record_imsm(st, dev, sra);
b2c59438
AK
12316 else {
12317 if (__le32_to_cpu(migr_rec->rec_status) != UNIT_SRC_NORMAL) {
7a862a02 12318 dprintf("imsm: cannot restart migration when data are present in copy area.\n");
b2c59438
AK
12319 goto abort;
12320 }
6a75c8ca
AK
12321 /* Save checkpoint to update migration record for current
12322 * reshape position (in md). It can be farther than current
12323 * reshape position in metadata.
12324 */
12325 if (save_checkpoint_imsm(st, sra, UNIT_SRC_NORMAL) == 1) {
12326 /* ignore error == 2, this can mean end of reshape here
12327 */
7a862a02 12328 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL, initial save)\n");
6a75c8ca
AK
12329 goto abort;
12330 }
b2c59438 12331 }
10f22854
AK
12332
12333 /* size for data */
12334 buf_size = __le32_to_cpu(migr_rec->blocks_per_unit) * 512;
12335 /* extend buffer size for parity disk */
12336 buf_size += __le32_to_cpu(migr_rec->dest_depth_per_unit) * 512;
3e684231 12337 /* add space for stripe alignment */
10f22854 12338 buf_size += old_data_stripe_length;
de44e46f
PB
12339 if (posix_memalign((void **)&buf, MAX_SECTOR_SIZE, buf_size)) {
12340 dprintf("imsm: Cannot allocate checkpoint buffer\n");
10f22854
AK
12341 goto abort;
12342 }
12343
3ef4403c 12344 max_position = sra->component_size * ndata;
68eb8bc6 12345 source_layout = imsm_level_to_layout(map_src->raid_level);
10f22854 12346
9f421827
PB
12347 while (current_migr_unit(migr_rec) <
12348 get_num_migr_units(migr_rec)) {
10f22854
AK
12349 /* current reshape position [blocks] */
12350 unsigned long long current_position =
12351 __le32_to_cpu(migr_rec->blocks_per_unit)
9f421827 12352 * current_migr_unit(migr_rec);
10f22854
AK
12353 unsigned long long border;
12354
b915c95f
AK
12355 /* Check that array hasn't become failed.
12356 */
12357 degraded = check_degradation_change(sra, fds, degraded);
12358 if (degraded > 1) {
7a862a02 12359 dprintf("imsm: Abort reshape due to degradation level (%i)\n", degraded);
b915c95f
AK
12360 goto abort;
12361 }
12362
10f22854
AK
12363 next_step = __le32_to_cpu(migr_rec->blocks_per_unit);
12364
12365 if ((current_position + next_step) > max_position)
12366 next_step = max_position - current_position;
12367
92144abf 12368 start = current_position * 512;
10f22854 12369
942e1cdb 12370 /* align reading start to old geometry */
10f22854
AK
12371 start_buf_shift = start % old_data_stripe_length;
12372 start_src = start - start_buf_shift;
12373
12374 border = (start_src / odata) - (start / ndata);
12375 border /= 512;
12376 if (border <= __le32_to_cpu(migr_rec->dest_depth_per_unit)) {
12377 /* save critical stripes to buf
12378 * start - start address of current unit
12379 * to backup [bytes]
12380 * start_src - start address of current unit
12381 * to backup alligned to source array
12382 * [bytes]
12383 */
594dc1b8 12384 unsigned long long next_step_filler;
10f22854
AK
12385 unsigned long long copy_length = next_step * 512;
12386
12387 /* allign copy area length to stripe in old geometry */
12388 next_step_filler = ((copy_length + start_buf_shift)
12389 % old_data_stripe_length);
12390 if (next_step_filler)
12391 next_step_filler = (old_data_stripe_length
12392 - next_step_filler);
7a862a02 12393 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
12394 start, start_src, copy_length,
12395 start_buf_shift, next_step_filler);
12396
12397 if (save_stripes(fds, offsets, map_src->num_members,
ab724b98
AK
12398 chunk, map_src->raid_level,
12399 source_layout, 0, NULL, start_src,
10f22854
AK
12400 copy_length +
12401 next_step_filler + start_buf_shift,
12402 buf)) {
7a862a02 12403 dprintf("imsm: Cannot save stripes to buffer\n");
10f22854
AK
12404 goto abort;
12405 }
12406 /* Convert data to destination format and store it
12407 * in backup general migration area
12408 */
12409 if (save_backup_imsm(st, dev, sra,
aea93171 12410 buf + start_buf_shift, copy_length)) {
7a862a02 12411 dprintf("imsm: Cannot save stripes to target devices\n");
10f22854
AK
12412 goto abort;
12413 }
12414 if (save_checkpoint_imsm(st, sra,
12415 UNIT_SRC_IN_CP_AREA)) {
7a862a02 12416 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_IN_CP_AREA)\n");
10f22854
AK
12417 goto abort;
12418 }
8016a6d4
AK
12419 } else {
12420 /* set next step to use whole border area */
12421 border /= next_step;
12422 if (border > 1)
12423 next_step *= border;
10f22854
AK
12424 }
12425 /* When data backed up, checkpoint stored,
12426 * kick the kernel to reshape unit of data
12427 */
12428 next_step = next_step + sra->reshape_progress;
8016a6d4
AK
12429 /* limit next step to array max position */
12430 if (next_step > max_position)
12431 next_step = max_position;
10f22854
AK
12432 sysfs_set_num(sra, NULL, "suspend_lo", sra->reshape_progress);
12433 sysfs_set_num(sra, NULL, "suspend_hi", next_step);
ae9f01f8 12434 sra->reshape_progress = next_step;
10f22854
AK
12435
12436 /* wait until reshape finish */
c85338c6 12437 if (wait_for_reshape_imsm(sra, ndata)) {
c47b0ff6
AK
12438 dprintf("wait_for_reshape_imsm returned error!\n");
12439 goto abort;
12440 }
84d11e6c
N
12441 if (sigterm)
12442 goto abort;
10f22854 12443
0228d92c
AK
12444 if (save_checkpoint_imsm(st, sra, UNIT_SRC_NORMAL) == 1) {
12445 /* ignore error == 2, this can mean end of reshape here
12446 */
7a862a02 12447 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL)\n");
10f22854
AK
12448 goto abort;
12449 }
12450
12451 }
12452
71e5411e
PB
12453 /* clear migr_rec on disks after successful migration */
12454 struct dl *d;
12455
85337573 12456 memset(super->migr_rec_buf, 0, MIGR_REC_BUF_SECTORS*MAX_SECTOR_SIZE);
71e5411e
PB
12457 for (d = super->disks; d; d = d->next) {
12458 if (d->index < 0 || is_failed(&d->disk))
12459 continue;
12460 unsigned long long dsize;
12461
12462 get_dev_size(d->fd, NULL, &dsize);
de44e46f 12463 if (lseek64(d->fd, dsize - MIGR_REC_SECTOR_POSITION*sector_size,
71e5411e 12464 SEEK_SET) >= 0) {
466070ad 12465 if ((unsigned int)write(d->fd, super->migr_rec_buf,
de44e46f
PB
12466 MIGR_REC_BUF_SECTORS*sector_size) !=
12467 MIGR_REC_BUF_SECTORS*sector_size)
71e5411e
PB
12468 perror("Write migr_rec failed");
12469 }
12470 }
12471
10f22854
AK
12472 /* return '1' if done */
12473 ret_val = 1;
895ffd99
MT
12474
12475 /* After the reshape eliminate size mismatch in metadata.
12476 * Don't update md/component_size here, volume hasn't
12477 * to take whole space. It is allowed by kernel.
12478 * md/component_size will be set propoperly after next assembly.
12479 */
12480 imsm_fix_size_mismatch(st, subarray_index);
12481
10f22854
AK
12482abort:
12483 free(buf);
942e1cdb
N
12484 /* See Grow.c: abort_reshape() for further explanation */
12485 sysfs_set_num(sra, NULL, "suspend_lo", 0x7FFFFFFFFFFFFFFFULL);
12486 sysfs_set_num(sra, NULL, "suspend_hi", 0);
12487 sysfs_set_num(sra, NULL, "suspend_lo", 0);
10f22854
AK
12488
12489 return ret_val;
999b4972 12490}
0c21b485 12491
fbc42556
JR
12492/*******************************************************************************
12493 * Function: calculate_bitmap_min_chunksize
12494 * Description: Calculates the minimal valid bitmap chunk size
12495 * Parameters:
12496 * max_bits : indicate how many bits can be used for the bitmap
12497 * data_area_size : the size of the data area covered by the bitmap
12498 *
12499 * Returns:
12500 * The bitmap chunk size
12501 ******************************************************************************/
12502static unsigned long long
12503calculate_bitmap_min_chunksize(unsigned long long max_bits,
12504 unsigned long long data_area_size)
12505{
12506 unsigned long long min_chunk =
12507 4096; /* sub-page chunks don't work yet.. */
12508 unsigned long long bits = data_area_size / min_chunk + 1;
12509
12510 while (bits > max_bits) {
12511 min_chunk *= 2;
12512 bits = (bits + 1) / 2;
12513 }
12514 return min_chunk;
12515}
12516
12517/*******************************************************************************
12518 * Function: calculate_bitmap_chunksize
12519 * Description: Calculates the bitmap chunk size for the given device
12520 * Parameters:
12521 * st : supertype information
12522 * dev : device for the bitmap
12523 *
12524 * Returns:
12525 * The bitmap chunk size
12526 ******************************************************************************/
12527static unsigned long long calculate_bitmap_chunksize(struct supertype *st,
12528 struct imsm_dev *dev)
12529{
12530 struct intel_super *super = st->sb;
12531 unsigned long long min_chunksize;
12532 unsigned long long result = IMSM_DEFAULT_BITMAP_CHUNKSIZE;
12533 size_t dev_size = imsm_dev_size(dev);
12534
12535 min_chunksize = calculate_bitmap_min_chunksize(
12536 IMSM_BITMAP_AREA_SIZE * super->sector_size, dev_size);
12537
12538 if (result < min_chunksize)
12539 result = min_chunksize;
12540
12541 return result;
12542}
12543
12544/*******************************************************************************
12545 * Function: init_bitmap_header
12546 * Description: Initialize the bitmap header structure
12547 * Parameters:
12548 * st : supertype information
12549 * bms : bitmap header struct to initialize
12550 * dev : device for the bitmap
12551 *
12552 * Returns:
12553 * 0 : success
12554 * -1 : fail
12555 ******************************************************************************/
12556static int init_bitmap_header(struct supertype *st, struct bitmap_super_s *bms,
12557 struct imsm_dev *dev)
12558{
12559 int vol_uuid[4];
12560
12561 if (!bms || !dev)
12562 return -1;
12563
12564 bms->magic = __cpu_to_le32(BITMAP_MAGIC);
12565 bms->version = __cpu_to_le32(BITMAP_MAJOR_HI);
12566 bms->daemon_sleep = __cpu_to_le32(IMSM_DEFAULT_BITMAP_DAEMON_SLEEP);
12567 bms->sync_size = __cpu_to_le64(IMSM_BITMAP_AREA_SIZE);
12568 bms->write_behind = __cpu_to_le32(0);
12569
12570 uuid_from_super_imsm(st, vol_uuid);
12571 memcpy(bms->uuid, vol_uuid, 16);
12572
12573 bms->chunksize = calculate_bitmap_chunksize(st, dev);
12574
12575 return 0;
12576}
12577
12578/*******************************************************************************
12579 * Function: validate_internal_bitmap_for_drive
12580 * Description: Verify if the bitmap header for a given drive.
12581 * Parameters:
12582 * st : supertype information
12583 * offset : The offset from the beginning of the drive where to look for
12584 * the bitmap header.
12585 * d : the drive info
12586 *
12587 * Returns:
12588 * 0 : success
12589 * -1 : fail
12590 ******************************************************************************/
12591static int validate_internal_bitmap_for_drive(struct supertype *st,
12592 unsigned long long offset,
12593 struct dl *d)
12594{
12595 struct intel_super *super = st->sb;
12596 int ret = -1;
12597 int vol_uuid[4];
12598 bitmap_super_t *bms;
12599 int fd;
12600
12601 if (!d)
12602 return -1;
12603
12604 void *read_buf;
12605
12606 if (posix_memalign(&read_buf, MAX_SECTOR_SIZE, IMSM_BITMAP_HEADER_SIZE))
12607 return -1;
12608
12609 fd = d->fd;
4389ce73 12610 if (!is_fd_valid(fd)) {
fbc42556 12611 fd = open(d->devname, O_RDONLY, 0);
4389ce73
MT
12612
12613 if (!is_fd_valid(fd)) {
fbc42556
JR
12614 dprintf("cannot open the device %s\n", d->devname);
12615 goto abort;
12616 }
12617 }
12618
12619 if (lseek64(fd, offset * super->sector_size, SEEK_SET) < 0)
12620 goto abort;
12621 if (read(fd, read_buf, IMSM_BITMAP_HEADER_SIZE) !=
12622 IMSM_BITMAP_HEADER_SIZE)
12623 goto abort;
12624
12625 uuid_from_super_imsm(st, vol_uuid);
12626
12627 bms = read_buf;
12628 if ((bms->magic != __cpu_to_le32(BITMAP_MAGIC)) ||
12629 (bms->version != __cpu_to_le32(BITMAP_MAJOR_HI)) ||
12630 (!same_uuid((int *)bms->uuid, vol_uuid, st->ss->swapuuid))) {
12631 dprintf("wrong bitmap header detected\n");
12632 goto abort;
12633 }
12634
12635 ret = 0;
12636abort:
4389ce73
MT
12637 if (!is_fd_valid(d->fd))
12638 close_fd(&fd);
12639
fbc42556
JR
12640 if (read_buf)
12641 free(read_buf);
12642
12643 return ret;
12644}
12645
12646/*******************************************************************************
12647 * Function: validate_internal_bitmap_imsm
12648 * Description: Verify if the bitmap header is in place and with proper data.
12649 * Parameters:
12650 * st : supertype information
12651 *
12652 * Returns:
12653 * 0 : success or device w/o RWH_BITMAP
12654 * -1 : fail
12655 ******************************************************************************/
12656static int validate_internal_bitmap_imsm(struct supertype *st)
12657{
12658 struct intel_super *super = st->sb;
12659 struct imsm_dev *dev = get_imsm_dev(super, super->current_vol);
12660 unsigned long long offset;
12661 struct dl *d;
12662
fbc42556
JR
12663 if (dev->rwh_policy != RWH_BITMAP)
12664 return 0;
12665
12666 offset = get_bitmap_header_sector(super, super->current_vol);
12667 for (d = super->disks; d; d = d->next) {
12668 if (d->index < 0 || is_failed(&d->disk))
12669 continue;
12670
12671 if (validate_internal_bitmap_for_drive(st, offset, d)) {
12672 pr_err("imsm: bitmap validation failed\n");
12673 return -1;
12674 }
12675 }
12676 return 0;
12677}
12678
12679/*******************************************************************************
12680 * Function: add_internal_bitmap_imsm
12681 * Description: Mark the volume to use the bitmap and updates the chunk size value.
12682 * Parameters:
12683 * st : supertype information
12684 * chunkp : bitmap chunk size
12685 * delay : not used for imsm
12686 * write_behind : not used for imsm
12687 * size : not used for imsm
12688 * may_change : not used for imsm
12689 * amajor : not used for imsm
12690 *
12691 * Returns:
12692 * 0 : success
12693 * -1 : fail
12694 ******************************************************************************/
12695static int add_internal_bitmap_imsm(struct supertype *st, int *chunkp,
12696 int delay, int write_behind,
12697 unsigned long long size, int may_change,
12698 int amajor)
12699{
12700 struct intel_super *super = st->sb;
12701 int vol_idx = super->current_vol;
12702 struct imsm_dev *dev;
12703
12704 if (!super->devlist || vol_idx == -1 || !chunkp)
12705 return -1;
12706
12707 dev = get_imsm_dev(super, vol_idx);
fbc42556 12708 dev->rwh_policy = RWH_BITMAP;
fbc42556 12709 *chunkp = calculate_bitmap_chunksize(st, dev);
fbc42556
JR
12710 return 0;
12711}
12712
12713/*******************************************************************************
12714 * Function: locate_bitmap_imsm
12715 * Description: Seek 'fd' to start of write-intent-bitmap.
12716 * Parameters:
12717 * st : supertype information
12718 * fd : file descriptor for the device
12719 * node_num : not used for imsm
12720 *
12721 * Returns:
12722 * 0 : success
12723 * -1 : fail
12724 ******************************************************************************/
12725static int locate_bitmap_imsm(struct supertype *st, int fd, int node_num)
12726{
12727 struct intel_super *super = st->sb;
12728 unsigned long long offset;
12729 int vol_idx = super->current_vol;
12730
12731 if (!super->devlist || vol_idx == -1)
12732 return -1;
12733
12734 offset = get_bitmap_header_sector(super, super->current_vol);
12735 dprintf("bitmap header offset is %llu\n", offset);
12736
12737 lseek64(fd, offset << 9, 0);
12738
12739 return 0;
12740}
12741
12742/*******************************************************************************
12743 * Function: write_init_bitmap_imsm
12744 * Description: Write a bitmap header and prepares the area for the bitmap.
12745 * Parameters:
12746 * st : supertype information
12747 * fd : file descriptor for the device
12748 * update : not used for imsm
12749 *
12750 * Returns:
12751 * 0 : success
12752 * -1 : fail
12753 ******************************************************************************/
12754static int write_init_bitmap_imsm(struct supertype *st, int fd,
12755 enum bitmap_update update)
12756{
12757 struct intel_super *super = st->sb;
12758 int vol_idx = super->current_vol;
12759 int ret = 0;
12760 unsigned long long offset;
12761 bitmap_super_t bms = { 0 };
12762 size_t written = 0;
12763 size_t to_write;
12764 ssize_t rv_num;
12765 void *buf;
12766
12767 if (!super->devlist || !super->sector_size || vol_idx == -1)
12768 return -1;
12769
12770 struct imsm_dev *dev = get_imsm_dev(super, vol_idx);
12771
12772 /* first clear the space for bitmap header */
12773 unsigned long long bitmap_area_start =
12774 get_bitmap_header_sector(super, vol_idx);
12775
12776 dprintf("zeroing area start (%llu) and size (%u)\n", bitmap_area_start,
12777 IMSM_BITMAP_AND_HEADER_SIZE / super->sector_size);
12778 if (zero_disk_range(fd, bitmap_area_start,
12779 IMSM_BITMAP_HEADER_SIZE / super->sector_size)) {
12780 pr_err("imsm: cannot zeroing the space for the bitmap\n");
12781 return -1;
12782 }
12783
12784 /* The bitmap area should be filled with "1"s to perform initial
12785 * synchronization.
12786 */
12787 if (posix_memalign(&buf, MAX_SECTOR_SIZE, MAX_SECTOR_SIZE))
12788 return -1;
12789 memset(buf, 0xFF, MAX_SECTOR_SIZE);
12790 offset = get_bitmap_sector(super, vol_idx);
12791 lseek64(fd, offset << 9, 0);
12792 while (written < IMSM_BITMAP_AREA_SIZE) {
12793 to_write = IMSM_BITMAP_AREA_SIZE - written;
12794 if (to_write > MAX_SECTOR_SIZE)
12795 to_write = MAX_SECTOR_SIZE;
12796 rv_num = write(fd, buf, MAX_SECTOR_SIZE);
12797 if (rv_num != MAX_SECTOR_SIZE) {
12798 ret = -1;
12799 dprintf("cannot initialize bitmap area\n");
12800 goto abort;
12801 }
12802 written += rv_num;
12803 }
12804
12805 /* write a bitmap header */
12806 init_bitmap_header(st, &bms, dev);
12807 memset(buf, 0, MAX_SECTOR_SIZE);
12808 memcpy(buf, &bms, sizeof(bitmap_super_t));
12809 if (locate_bitmap_imsm(st, fd, 0)) {
12810 ret = -1;
12811 dprintf("cannot locate the bitmap\n");
12812 goto abort;
12813 }
12814 if (write(fd, buf, MAX_SECTOR_SIZE) != MAX_SECTOR_SIZE) {
12815 ret = -1;
12816 dprintf("cannot write the bitmap header\n");
12817 goto abort;
12818 }
12819 fsync(fd);
12820
12821abort:
12822 free(buf);
12823
12824 return ret;
12825}
12826
12827/*******************************************************************************
12828 * Function: is_vol_to_setup_bitmap
12829 * Description: Checks if a bitmap should be activated on the dev.
12830 * Parameters:
12831 * info : info about the volume to setup the bitmap
12832 * dev : the device to check against bitmap creation
12833 *
12834 * Returns:
12835 * 0 : bitmap should be set up on the device
12836 * -1 : otherwise
12837 ******************************************************************************/
12838static int is_vol_to_setup_bitmap(struct mdinfo *info, struct imsm_dev *dev)
12839{
12840 if (!dev || !info)
12841 return -1;
12842
12843 if ((strcmp((char *)dev->volume, info->name) == 0) &&
12844 (dev->rwh_policy == RWH_BITMAP))
12845 return -1;
12846
12847 return 0;
12848}
12849
12850/*******************************************************************************
12851 * Function: set_bitmap_sysfs
12852 * Description: Set the sysfs atributes of a given volume to activate the bitmap.
12853 * Parameters:
12854 * info : info about the volume where the bitmap should be setup
12855 * chunksize : bitmap chunk size
12856 * location : location of the bitmap
12857 *
12858 * Returns:
12859 * 0 : success
12860 * -1 : fail
12861 ******************************************************************************/
12862static int set_bitmap_sysfs(struct mdinfo *info, unsigned long long chunksize,
12863 char *location)
12864{
12865 /* The bitmap/metadata is set to external to allow changing of value for
12866 * bitmap/location. When external is used, the kernel will treat an offset
12867 * related to the device's first lba (in opposition to the "internal" case
12868 * when this value is related to the beginning of the superblock).
12869 */
12870 if (sysfs_set_str(info, NULL, "bitmap/metadata", "external")) {
12871 dprintf("failed to set bitmap/metadata\n");
12872 return -1;
12873 }
12874
12875 /* It can only be changed when no bitmap is active.
12876 * Should be bigger than 512 and must be power of 2.
12877 * It is expecting the value in bytes.
12878 */
12879 if (sysfs_set_num(info, NULL, "bitmap/chunksize",
12880 __cpu_to_le32(chunksize))) {
12881 dprintf("failed to set bitmap/chunksize\n");
12882 return -1;
12883 }
12884
12885 /* It is expecting the value in sectors. */
12886 if (sysfs_set_num(info, NULL, "bitmap/space",
12887 __cpu_to_le64(IMSM_BITMAP_AREA_SIZE))) {
12888 dprintf("failed to set bitmap/space\n");
12889 return -1;
12890 }
12891
12892 /* Determines the delay between the bitmap updates.
12893 * It is expecting the value in seconds.
12894 */
12895 if (sysfs_set_num(info, NULL, "bitmap/time_base",
12896 __cpu_to_le64(IMSM_DEFAULT_BITMAP_DAEMON_SLEEP))) {
12897 dprintf("failed to set bitmap/time_base\n");
12898 return -1;
12899 }
12900
12901 /* It is expecting the value in sectors with a sign at the beginning. */
12902 if (sysfs_set_str(info, NULL, "bitmap/location", location)) {
12903 dprintf("failed to set bitmap/location\n");
12904 return -1;
12905 }
12906
12907 return 0;
12908}
12909
12910/*******************************************************************************
12911 * Function: set_bitmap_imsm
12912 * Description: Setup the bitmap for the given volume
12913 * Parameters:
12914 * st : supertype information
12915 * info : info about the volume where the bitmap should be setup
12916 *
12917 * Returns:
12918 * 0 : success
12919 * -1 : fail
12920 ******************************************************************************/
12921static int set_bitmap_imsm(struct supertype *st, struct mdinfo *info)
12922{
12923 struct intel_super *super = st->sb;
12924 int prev_current_vol = super->current_vol;
12925 struct imsm_dev *dev;
12926 int ret = -1;
12927 char location[16] = "";
12928 unsigned long long chunksize;
12929 struct intel_dev *dev_it;
12930
12931 for (dev_it = super->devlist; dev_it; dev_it = dev_it->next) {
12932 super->current_vol = dev_it->index;
12933 dev = get_imsm_dev(super, super->current_vol);
12934
12935 if (is_vol_to_setup_bitmap(info, dev)) {
12936 if (validate_internal_bitmap_imsm(st)) {
12937 dprintf("bitmap header validation failed\n");
12938 goto abort;
12939 }
12940
12941 chunksize = calculate_bitmap_chunksize(st, dev);
12942 dprintf("chunk size is %llu\n", chunksize);
12943
12944 snprintf(location, sizeof(location), "+%llu",
12945 get_bitmap_sector(super, super->current_vol));
12946 dprintf("bitmap offset is %s\n", location);
12947
12948 if (set_bitmap_sysfs(info, chunksize, location)) {
12949 dprintf("cannot setup the bitmap\n");
12950 goto abort;
12951 }
12952 }
12953 }
12954 ret = 0;
12955abort:
12956 super->current_vol = prev_current_vol;
12957 return ret;
12958}
12959
cdddbdbc 12960struct superswitch super_imsm = {
cdddbdbc
DW
12961 .examine_super = examine_super_imsm,
12962 .brief_examine_super = brief_examine_super_imsm,
4737ae25 12963 .brief_examine_subarrays = brief_examine_subarrays_imsm,
9d84c8ea 12964 .export_examine_super = export_examine_super_imsm,
cdddbdbc
DW
12965 .detail_super = detail_super_imsm,
12966 .brief_detail_super = brief_detail_super_imsm,
bf5a934a 12967 .write_init_super = write_init_super_imsm,
0e600426
N
12968 .validate_geometry = validate_geometry_imsm,
12969 .add_to_super = add_to_super_imsm,
1a64be56 12970 .remove_from_super = remove_from_super_imsm,
d665cc31 12971 .detail_platform = detail_platform_imsm,
e50cf220 12972 .export_detail_platform = export_detail_platform_imsm,
33414a01 12973 .kill_subarray = kill_subarray_imsm,
aa534678 12974 .update_subarray = update_subarray_imsm,
2b959fbf 12975 .load_container = load_container_imsm,
71204a50
N
12976 .default_geometry = default_geometry_imsm,
12977 .get_disk_controller_domain = imsm_get_disk_controller_domain,
12978 .reshape_super = imsm_reshape_super,
12979 .manage_reshape = imsm_manage_reshape,
9e2d750d 12980 .recover_backup = recover_backup_imsm,
27156a57 12981 .examine_badblocks = examine_badblocks_imsm,
cdddbdbc
DW
12982 .match_home = match_home_imsm,
12983 .uuid_from_super= uuid_from_super_imsm,
12984 .getinfo_super = getinfo_super_imsm,
5c4cd5da 12985 .getinfo_super_disks = getinfo_super_disks_imsm,
cdddbdbc
DW
12986 .update_super = update_super_imsm,
12987
12988 .avail_size = avail_size_imsm,
fbfdcb06 12989 .get_spare_criteria = get_spare_criteria_imsm,
cdddbdbc
DW
12990
12991 .compare_super = compare_super_imsm,
12992
12993 .load_super = load_super_imsm,
bf5a934a 12994 .init_super = init_super_imsm,
e683ca88 12995 .store_super = store_super_imsm,
cdddbdbc
DW
12996 .free_super = free_super_imsm,
12997 .match_metadata_desc = match_metadata_desc_imsm,
bf5a934a 12998 .container_content = container_content_imsm,
0c21b485 12999 .validate_container = validate_container_imsm,
cdddbdbc 13000
fbc42556
JR
13001 .add_internal_bitmap = add_internal_bitmap_imsm,
13002 .locate_bitmap = locate_bitmap_imsm,
13003 .write_bitmap = write_init_bitmap_imsm,
13004 .set_bitmap = set_bitmap_imsm,
13005
2432ce9b
AP
13006 .write_init_ppl = write_init_ppl_imsm,
13007 .validate_ppl = validate_ppl_imsm,
13008
cdddbdbc 13009 .external = 1,
4cce4069 13010 .name = "imsm",
845dea95
NB
13011
13012/* for mdmon */
13013 .open_new = imsm_open_new,
ed9d66aa 13014 .set_array_state= imsm_set_array_state,
845dea95
NB
13015 .set_disk = imsm_set_disk,
13016 .sync_metadata = imsm_sync_metadata,
88758e9d 13017 .activate_spare = imsm_activate_spare,
e8319a19 13018 .process_update = imsm_process_update,
8273f55e 13019 .prepare_update = imsm_prepare_update,
6f50473f 13020 .record_bad_block = imsm_record_badblock,
c07a5a4f 13021 .clear_bad_block = imsm_clear_badblock,
928f1424 13022 .get_bad_blocks = imsm_get_badblocks,
cdddbdbc 13023};