]> git.ipfire.org Git - thirdparty/mdadm.git/blame - super-intel.c
mdadm: define ident_set_devname()
[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
0d481d37 2979#if 0
4f5bc454
DW
2980static void
2981get_imsm_numerical_version(struct imsm_super *mpb, int *m, int *p)
cdddbdbc 2982{
cdddbdbc
DW
2983 __u8 *v = get_imsm_version(mpb);
2984 __u8 *end = mpb->sig + MAX_SIGNATURE_LENGTH;
2985 char major[] = { 0, 0, 0 };
2986 char minor[] = { 0 ,0, 0 };
2987 char patch[] = { 0, 0, 0 };
2988 char *ver_parse[] = { major, minor, patch };
2989 int i, j;
2990
2991 i = j = 0;
2992 while (*v != '\0' && v < end) {
2993 if (*v != '.' && j < 2)
2994 ver_parse[i][j++] = *v;
2995 else {
2996 i++;
2997 j = 0;
2998 }
2999 v++;
3000 }
3001
4f5bc454
DW
3002 *m = strtol(minor, NULL, 0);
3003 *p = strtol(patch, NULL, 0);
3004}
0d481d37 3005#endif
4f5bc454 3006
1e5c6983
DW
3007static __u32 migr_strip_blocks_resync(struct imsm_dev *dev)
3008{
3009 /* migr_strip_size when repairing or initializing parity */
238c0a71 3010 struct imsm_map *map = get_imsm_map(dev, MAP_0);
1e5c6983
DW
3011 __u32 chunk = __le32_to_cpu(map->blocks_per_strip);
3012
3013 switch (get_imsm_raid_level(map)) {
3014 case 5:
3015 case 10:
3016 return chunk;
3017 default:
3018 return 128*1024 >> 9;
3019 }
3020}
3021
3022static __u32 migr_strip_blocks_rebuild(struct imsm_dev *dev)
3023{
3024 /* migr_strip_size when rebuilding a degraded disk, no idea why
3025 * this is different than migr_strip_size_resync(), but it's good
3026 * to be compatible
3027 */
238c0a71 3028 struct imsm_map *map = get_imsm_map(dev, MAP_1);
1e5c6983
DW
3029 __u32 chunk = __le32_to_cpu(map->blocks_per_strip);
3030
3031 switch (get_imsm_raid_level(map)) {
3032 case 1:
3033 case 10:
3034 if (map->num_members % map->num_domains == 0)
3035 return 128*1024 >> 9;
3036 else
3037 return chunk;
3038 case 5:
3039 return max((__u32) 64*1024 >> 9, chunk);
3040 default:
3041 return 128*1024 >> 9;
3042 }
3043}
3044
3045static __u32 num_stripes_per_unit_resync(struct imsm_dev *dev)
3046{
238c0a71
AK
3047 struct imsm_map *lo = get_imsm_map(dev, MAP_0);
3048 struct imsm_map *hi = get_imsm_map(dev, MAP_1);
1e5c6983
DW
3049 __u32 lo_chunk = __le32_to_cpu(lo->blocks_per_strip);
3050 __u32 hi_chunk = __le32_to_cpu(hi->blocks_per_strip);
3051
3052 return max((__u32) 1, hi_chunk / lo_chunk);
3053}
3054
3055static __u32 num_stripes_per_unit_rebuild(struct imsm_dev *dev)
3056{
238c0a71 3057 struct imsm_map *lo = get_imsm_map(dev, MAP_0);
1e5c6983
DW
3058 int level = get_imsm_raid_level(lo);
3059
3060 if (level == 1 || level == 10) {
238c0a71 3061 struct imsm_map *hi = get_imsm_map(dev, MAP_1);
1e5c6983
DW
3062
3063 return hi->num_domains;
3064 } else
3065 return num_stripes_per_unit_resync(dev);
3066}
3067
44490938
MD
3068static unsigned long long calc_component_size(struct imsm_map *map,
3069 struct imsm_dev *dev)
3070{
3071 unsigned long long component_size;
3072 unsigned long long dev_size = imsm_dev_size(dev);
a4f7290c 3073 long long calc_dev_size = 0;
44490938
MD
3074 unsigned int member_disks = imsm_num_data_members(map);
3075
3076 if (member_disks == 0)
3077 return 0;
3078
3079 component_size = per_dev_array_size(map);
3080 calc_dev_size = component_size * member_disks;
3081
3082 /* Component size is rounded to 1MB so difference between size from
3083 * metadata and size calculated from num_data_stripes equals up to
3084 * 2048 blocks per each device. If the difference is higher it means
3085 * that array size was expanded and num_data_stripes was not updated.
3086 */
a4f7290c 3087 if (llabs(calc_dev_size - (long long)dev_size) >
44490938
MD
3088 (1 << SECT_PER_MB_SHIFT) * member_disks) {
3089 component_size = dev_size / member_disks;
3090 dprintf("Invalid num_data_stripes in metadata; expected=%llu, found=%llu\n",
3091 component_size / map->blocks_per_strip,
3092 num_data_stripes(map));
3093 }
3094
3095 return component_size;
3096}
3097
1e5c6983
DW
3098static __u32 parity_segment_depth(struct imsm_dev *dev)
3099{
238c0a71 3100 struct imsm_map *map = get_imsm_map(dev, MAP_0);
1e5c6983
DW
3101 __u32 chunk = __le32_to_cpu(map->blocks_per_strip);
3102
3103 switch(get_imsm_raid_level(map)) {
3104 case 1:
3105 case 10:
3106 return chunk * map->num_domains;
3107 case 5:
3108 return chunk * map->num_members;
3109 default:
3110 return chunk;
3111 }
3112}
3113
3114static __u32 map_migr_block(struct imsm_dev *dev, __u32 block)
3115{
238c0a71 3116 struct imsm_map *map = get_imsm_map(dev, MAP_1);
1e5c6983
DW
3117 __u32 chunk = __le32_to_cpu(map->blocks_per_strip);
3118 __u32 strip = block / chunk;
3119
3120 switch (get_imsm_raid_level(map)) {
3121 case 1:
3122 case 10: {
3123 __u32 vol_strip = (strip * map->num_domains) + 1;
3124 __u32 vol_stripe = vol_strip / map->num_members;
3125
3126 return vol_stripe * chunk + block % chunk;
3127 } case 5: {
3128 __u32 stripe = strip / (map->num_members - 1);
3129
3130 return stripe * chunk + block % chunk;
3131 }
3132 default:
3133 return 0;
3134 }
3135}
3136
c47b0ff6
AK
3137static __u64 blocks_per_migr_unit(struct intel_super *super,
3138 struct imsm_dev *dev)
1e5c6983
DW
3139{
3140 /* calculate the conversion factor between per member 'blocks'
3141 * (md/{resync,rebuild}_start) and imsm migration units, return
3142 * 0 for the 'not migrating' and 'unsupported migration' cases
3143 */
3144 if (!dev->vol.migr_state)
3145 return 0;
3146
3147 switch (migr_type(dev)) {
c47b0ff6
AK
3148 case MIGR_GEN_MIGR: {
3149 struct migr_record *migr_rec = super->migr_rec;
3150 return __le32_to_cpu(migr_rec->blocks_per_unit);
3151 }
1e5c6983
DW
3152 case MIGR_VERIFY:
3153 case MIGR_REPAIR:
3154 case MIGR_INIT: {
238c0a71 3155 struct imsm_map *map = get_imsm_map(dev, MAP_0);
1e5c6983
DW
3156 __u32 stripes_per_unit;
3157 __u32 blocks_per_unit;
3158 __u32 parity_depth;
3159 __u32 migr_chunk;
3160 __u32 block_map;
3161 __u32 block_rel;
3162 __u32 segment;
3163 __u32 stripe;
3164 __u8 disks;
3165
3166 /* yes, this is really the translation of migr_units to
3167 * per-member blocks in the 'resync' case
3168 */
3169 stripes_per_unit = num_stripes_per_unit_resync(dev);
3170 migr_chunk = migr_strip_blocks_resync(dev);
9529d343 3171 disks = imsm_num_data_members(map);
1e5c6983 3172 blocks_per_unit = stripes_per_unit * migr_chunk * disks;
7b1ab482 3173 stripe = __le16_to_cpu(map->blocks_per_strip) * disks;
1e5c6983
DW
3174 segment = blocks_per_unit / stripe;
3175 block_rel = blocks_per_unit - segment * stripe;
3176 parity_depth = parity_segment_depth(dev);
3177 block_map = map_migr_block(dev, block_rel);
3178 return block_map + parity_depth * segment;
3179 }
3180 case MIGR_REBUILD: {
3181 __u32 stripes_per_unit;
3182 __u32 migr_chunk;
3183
3184 stripes_per_unit = num_stripes_per_unit_rebuild(dev);
3185 migr_chunk = migr_strip_blocks_rebuild(dev);
3186 return migr_chunk * stripes_per_unit;
3187 }
1e5c6983
DW
3188 case MIGR_STATE_CHANGE:
3189 default:
3190 return 0;
3191 }
3192}
3193
c2c087e6
DW
3194static int imsm_level_to_layout(int level)
3195{
3196 switch (level) {
3197 case 0:
3198 case 1:
3199 return 0;
3200 case 5:
3201 case 6:
a380c027 3202 return ALGORITHM_LEFT_ASYMMETRIC;
c2c087e6 3203 case 10:
c92a2527 3204 return 0x102;
c2c087e6 3205 }
a18a888e 3206 return UnSet;
c2c087e6
DW
3207}
3208
8e59f3d8
AK
3209/*******************************************************************************
3210 * Function: read_imsm_migr_rec
3211 * Description: Function reads imsm migration record from last sector of disk
3212 * Parameters:
3213 * fd : disk descriptor
3214 * super : metadata info
3215 * Returns:
3216 * 0 : success,
3217 * -1 : fail
3218 ******************************************************************************/
3219static int read_imsm_migr_rec(int fd, struct intel_super *super)
3220{
3221 int ret_val = -1;
de44e46f 3222 unsigned int sector_size = super->sector_size;
8e59f3d8
AK
3223 unsigned long long dsize;
3224
3225 get_dev_size(fd, NULL, &dsize);
de44e46f
PB
3226 if (lseek64(fd, dsize - (sector_size*MIGR_REC_SECTOR_POSITION),
3227 SEEK_SET) < 0) {
e7b84f9d
N
3228 pr_err("Cannot seek to anchor block: %s\n",
3229 strerror(errno));
8e59f3d8
AK
3230 goto out;
3231 }
466070ad 3232 if ((unsigned int)read(fd, super->migr_rec_buf,
de44e46f
PB
3233 MIGR_REC_BUF_SECTORS*sector_size) !=
3234 MIGR_REC_BUF_SECTORS*sector_size) {
e7b84f9d
N
3235 pr_err("Cannot read migr record block: %s\n",
3236 strerror(errno));
8e59f3d8
AK
3237 goto out;
3238 }
3239 ret_val = 0;
de44e46f
PB
3240 if (sector_size == 4096)
3241 convert_from_4k_imsm_migr_rec(super);
8e59f3d8
AK
3242
3243out:
3244 return ret_val;
3245}
3246
3136abe5
AK
3247static struct imsm_dev *imsm_get_device_during_migration(
3248 struct intel_super *super)
3249{
3250
3251 struct intel_dev *dv;
3252
3253 for (dv = super->devlist; dv; dv = dv->next) {
3254 if (is_gen_migration(dv->dev))
3255 return dv->dev;
3256 }
3257 return NULL;
3258}
3259
8e59f3d8
AK
3260/*******************************************************************************
3261 * Function: load_imsm_migr_rec
3262 * Description: Function reads imsm migration record (it is stored at the last
3263 * sector of disk)
3264 * Parameters:
3265 * super : imsm internal array info
8e59f3d8
AK
3266 * Returns:
3267 * 0 : success
3268 * -1 : fail
4c965cc9 3269 * -2 : no migration in progress
8e59f3d8 3270 ******************************************************************************/
2f86fda3 3271static int load_imsm_migr_rec(struct intel_super *super)
8e59f3d8 3272{
594dc1b8 3273 struct dl *dl;
8e59f3d8
AK
3274 char nm[30];
3275 int retval = -1;
3276 int fd = -1;
3136abe5 3277 struct imsm_dev *dev;
594dc1b8 3278 struct imsm_map *map;
b4ab44d8 3279 int slot = -1;
2f86fda3 3280 int keep_fd = 1;
3136abe5
AK
3281
3282 /* find map under migration */
3283 dev = imsm_get_device_during_migration(super);
3284 /* nothing to load,no migration in progress?
3285 */
3286 if (dev == NULL)
4c965cc9 3287 return -2;
8e59f3d8 3288
2f86fda3
MT
3289 map = get_imsm_map(dev, MAP_0);
3290 if (!map)
3291 return -1;
3136abe5 3292
2f86fda3
MT
3293 for (dl = super->disks; dl; dl = dl->next) {
3294 /* skip spare and failed disks
3295 */
3296 if (dl->index < 0)
3297 continue;
3298 /* read only from one of the first two slots
3299 */
3300 slot = get_imsm_disk_slot(map, dl->index);
3301 if (slot > 1 || slot < 0)
3302 continue;
3303
4389ce73 3304 if (!is_fd_valid(dl->fd)) {
8e59f3d8
AK
3305 sprintf(nm, "%d:%d", dl->major, dl->minor);
3306 fd = dev_open(nm, O_RDONLY);
4389ce73
MT
3307
3308 if (is_fd_valid(fd)) {
2f86fda3 3309 keep_fd = 0;
8e59f3d8 3310 break;
2f86fda3
MT
3311 }
3312 } else {
3313 fd = dl->fd;
3314 break;
8e59f3d8
AK
3315 }
3316 }
2f86fda3 3317
4389ce73 3318 if (!is_fd_valid(fd))
2f86fda3 3319 return retval;
8e59f3d8 3320 retval = read_imsm_migr_rec(fd, super);
2f86fda3 3321 if (!keep_fd)
8e59f3d8 3322 close(fd);
2f86fda3 3323
8e59f3d8
AK
3324 return retval;
3325}
3326
c17608ea
AK
3327/*******************************************************************************
3328 * function: imsm_create_metadata_checkpoint_update
3329 * Description: It creates update for checkpoint change.
3330 * Parameters:
3331 * super : imsm internal array info
3332 * u : pointer to prepared update
3333 * Returns:
3334 * Uptate length.
3335 * If length is equal to 0, input pointer u contains no update
3336 ******************************************************************************/
3337static int imsm_create_metadata_checkpoint_update(
3338 struct intel_super *super,
3339 struct imsm_update_general_migration_checkpoint **u)
3340{
3341
3342 int update_memory_size = 0;
3343
1ade5cc1 3344 dprintf("(enter)\n");
c17608ea
AK
3345
3346 if (u == NULL)
3347 return 0;
3348 *u = NULL;
3349
3350 /* size of all update data without anchor */
3351 update_memory_size =
3352 sizeof(struct imsm_update_general_migration_checkpoint);
3353
503975b9 3354 *u = xcalloc(1, update_memory_size);
c17608ea 3355 if (*u == NULL) {
1ade5cc1 3356 dprintf("error: cannot get memory\n");
c17608ea
AK
3357 return 0;
3358 }
3359 (*u)->type = update_general_migration_checkpoint;
9f421827 3360 (*u)->curr_migr_unit = current_migr_unit(super->migr_rec);
83b3de77 3361 dprintf("prepared for %llu\n", (unsigned long long)(*u)->curr_migr_unit);
c17608ea
AK
3362
3363 return update_memory_size;
3364}
3365
c17608ea
AK
3366static void imsm_update_metadata_locally(struct supertype *st,
3367 void *buf, int len);
3368
687629c2
AK
3369/*******************************************************************************
3370 * Function: write_imsm_migr_rec
3371 * Description: Function writes imsm migration record
3372 * (at the last sector of disk)
3373 * Parameters:
3374 * super : imsm internal array info
3375 * Returns:
3376 * 0 : success
3377 * -1 : if fail
3378 ******************************************************************************/
3379static int write_imsm_migr_rec(struct supertype *st)
3380{
3381 struct intel_super *super = st->sb;
de44e46f 3382 unsigned int sector_size = super->sector_size;
687629c2 3383 unsigned long long dsize;
687629c2
AK
3384 int retval = -1;
3385 struct dl *sd;
c17608ea
AK
3386 int len;
3387 struct imsm_update_general_migration_checkpoint *u;
3136abe5 3388 struct imsm_dev *dev;
594dc1b8 3389 struct imsm_map *map;
3136abe5
AK
3390
3391 /* find map under migration */
3392 dev = imsm_get_device_during_migration(super);
3393 /* if no migration, write buffer anyway to clear migr_record
3394 * on disk based on first available device
3395 */
3396 if (dev == NULL)
3397 dev = get_imsm_dev(super, super->current_vol < 0 ? 0 :
3398 super->current_vol);
3399
44bfe6df 3400 map = get_imsm_map(dev, MAP_0);
687629c2 3401
de44e46f
PB
3402 if (sector_size == 4096)
3403 convert_to_4k_imsm_migr_rec(super);
687629c2 3404 for (sd = super->disks ; sd ; sd = sd->next) {
b4ab44d8 3405 int slot = -1;
3136abe5
AK
3406
3407 /* skip failed and spare devices */
3408 if (sd->index < 0)
3409 continue;
687629c2 3410 /* write to 2 first slots only */
3136abe5
AK
3411 if (map)
3412 slot = get_imsm_disk_slot(map, sd->index);
089f9d79 3413 if (map == NULL || slot > 1 || slot < 0)
687629c2 3414 continue;
3136abe5 3415
2f86fda3
MT
3416 get_dev_size(sd->fd, NULL, &dsize);
3417 if (lseek64(sd->fd, dsize - (MIGR_REC_SECTOR_POSITION *
3418 sector_size),
de44e46f 3419 SEEK_SET) < 0) {
e7b84f9d
N
3420 pr_err("Cannot seek to anchor block: %s\n",
3421 strerror(errno));
687629c2
AK
3422 goto out;
3423 }
2f86fda3 3424 if ((unsigned int)write(sd->fd, super->migr_rec_buf,
de44e46f
PB
3425 MIGR_REC_BUF_SECTORS*sector_size) !=
3426 MIGR_REC_BUF_SECTORS*sector_size) {
e7b84f9d
N
3427 pr_err("Cannot write migr record block: %s\n",
3428 strerror(errno));
687629c2
AK
3429 goto out;
3430 }
687629c2 3431 }
de44e46f
PB
3432 if (sector_size == 4096)
3433 convert_from_4k_imsm_migr_rec(super);
c17608ea
AK
3434 /* update checkpoint information in metadata */
3435 len = imsm_create_metadata_checkpoint_update(super, &u);
c17608ea
AK
3436 if (len <= 0) {
3437 dprintf("imsm: Cannot prepare update\n");
3438 goto out;
3439 }
3440 /* update metadata locally */
3441 imsm_update_metadata_locally(st, u, len);
3442 /* and possibly remotely */
3443 if (st->update_tail) {
3444 append_metadata_update(st, u, len);
3445 /* during reshape we do all work inside metadata handler
3446 * manage_reshape(), so metadata update has to be triggered
3447 * insida it
3448 */
3449 flush_metadata_updates(st);
3450 st->update_tail = &st->updates;
3451 } else
3452 free(u);
687629c2
AK
3453
3454 retval = 0;
3455 out:
687629c2
AK
3456 return retval;
3457}
3458
e2962bfc
AK
3459/* spare/missing disks activations are not allowe when
3460 * array/container performs reshape operation, because
3461 * all arrays in container works on the same disks set
3462 */
3463int imsm_reshape_blocks_arrays_changes(struct intel_super *super)
3464{
3465 int rv = 0;
3466 struct intel_dev *i_dev;
3467 struct imsm_dev *dev;
3468
3469 /* check whole container
3470 */
3471 for (i_dev = super->devlist; i_dev; i_dev = i_dev->next) {
3472 dev = i_dev->dev;
3ad25638 3473 if (is_gen_migration(dev)) {
e2962bfc
AK
3474 /* No repair during any migration in container
3475 */
3476 rv = 1;
3477 break;
3478 }
3479 }
3480 return rv;
3481}
3e684231 3482static unsigned long long imsm_component_size_alignment_check(int level,
c41e00b2 3483 int chunk_size,
f36a9ecd 3484 unsigned int sector_size,
c41e00b2
AK
3485 unsigned long long component_size)
3486{
3e684231 3487 unsigned int component_size_alignment;
c41e00b2 3488
3e684231 3489 /* check component size alignment
c41e00b2 3490 */
3e684231 3491 component_size_alignment = component_size % (chunk_size/sector_size);
c41e00b2 3492
3e684231 3493 dprintf("(Level: %i, chunk_size = %i, component_size = %llu), component_size_alignment = %u\n",
c41e00b2 3494 level, chunk_size, component_size,
3e684231 3495 component_size_alignment);
c41e00b2 3496
3e684231
MZ
3497 if (component_size_alignment && (level != 1) && (level != UnSet)) {
3498 dprintf("imsm: reported component size aligned from %llu ",
c41e00b2 3499 component_size);
3e684231 3500 component_size -= component_size_alignment;
1ade5cc1 3501 dprintf_cont("to %llu (%i).\n",
3e684231 3502 component_size, component_size_alignment);
c41e00b2
AK
3503 }
3504
3505 return component_size;
3506}
e2962bfc 3507
fbc42556
JR
3508/*******************************************************************************
3509 * Function: get_bitmap_header_sector
3510 * Description: Returns the sector where the bitmap header is placed.
3511 * Parameters:
3512 * st : supertype information
3513 * dev_idx : index of the device with bitmap
3514 *
3515 * Returns:
3516 * The sector where the bitmap header is placed
3517 ******************************************************************************/
3518static unsigned long long get_bitmap_header_sector(struct intel_super *super,
3519 int dev_idx)
3520{
3521 struct imsm_dev *dev = get_imsm_dev(super, dev_idx);
3522 struct imsm_map *map = get_imsm_map(dev, MAP_0);
3523
3524 if (!super->sector_size) {
3525 dprintf("sector size is not set\n");
3526 return 0;
3527 }
3528
3529 return pba_of_lba0(map) + calc_component_size(map, dev) +
3530 (IMSM_BITMAP_HEADER_OFFSET / super->sector_size);
3531}
3532
3533/*******************************************************************************
3534 * Function: get_bitmap_sector
3535 * Description: Returns the sector where the bitmap is placed.
3536 * Parameters:
3537 * st : supertype information
3538 * dev_idx : index of the device with bitmap
3539 *
3540 * Returns:
3541 * The sector where the bitmap is placed
3542 ******************************************************************************/
3543static unsigned long long get_bitmap_sector(struct intel_super *super,
3544 int dev_idx)
3545{
3546 if (!super->sector_size) {
3547 dprintf("sector size is not set\n");
3548 return 0;
3549 }
3550
3551 return get_bitmap_header_sector(super, dev_idx) +
3552 (IMSM_BITMAP_HEADER_SIZE / super->sector_size);
3553}
3554
2432ce9b
AP
3555static unsigned long long get_ppl_sector(struct intel_super *super, int dev_idx)
3556{
3557 struct imsm_dev *dev = get_imsm_dev(super, dev_idx);
3558 struct imsm_map *map = get_imsm_map(dev, MAP_0);
3559
3560 return pba_of_lba0(map) +
3561 (num_data_stripes(map) * map->blocks_per_strip);
3562}
3563
a5d85af7 3564static void getinfo_super_imsm_volume(struct supertype *st, struct mdinfo *info, char *dmap)
bf5a934a
DW
3565{
3566 struct intel_super *super = st->sb;
c47b0ff6 3567 struct migr_record *migr_rec = super->migr_rec;
949c47a0 3568 struct imsm_dev *dev = get_imsm_dev(super, super->current_vol);
238c0a71
AK
3569 struct imsm_map *map = get_imsm_map(dev, MAP_0);
3570 struct imsm_map *prev_map = get_imsm_map(dev, MAP_1);
b335e593 3571 struct imsm_map *map_to_analyse = map;
efb30e7f 3572 struct dl *dl;
a5d85af7 3573 int map_disks = info->array.raid_disks;
bf5a934a 3574
95eeceeb 3575 memset(info, 0, sizeof(*info));
b335e593
AK
3576 if (prev_map)
3577 map_to_analyse = prev_map;
3578
ca0748fa 3579 dl = super->current_disk;
9894ec0d 3580
bf5a934a 3581 info->container_member = super->current_vol;
cd0430a1 3582 info->array.raid_disks = map->num_members;
b335e593 3583 info->array.level = get_imsm_raid_level(map_to_analyse);
bf5a934a
DW
3584 info->array.layout = imsm_level_to_layout(info->array.level);
3585 info->array.md_minor = -1;
3586 info->array.ctime = 0;
3587 info->array.utime = 0;
b335e593
AK
3588 info->array.chunk_size =
3589 __le16_to_cpu(map_to_analyse->blocks_per_strip) << 9;
2432ce9b 3590 info->array.state = !(dev->vol.dirty & RAIDVOL_DIRTY);
fcc2c9da 3591 info->custom_array_size = imsm_dev_size(dev);
3ad25638
AK
3592 info->recovery_blocked = imsm_reshape_blocks_arrays_changes(st->sb);
3593
3f510843 3594 if (is_gen_migration(dev)) {
195d1d76
PP
3595 /*
3596 * device prev_map should be added if it is in the middle
3597 * of migration
3598 */
3599 assert(prev_map);
3600
3f83228a 3601 info->reshape_active = 1;
b335e593
AK
3602 info->new_level = get_imsm_raid_level(map);
3603 info->new_layout = imsm_level_to_layout(info->new_level);
3604 info->new_chunk = __le16_to_cpu(map->blocks_per_strip) << 9;
3f83228a 3605 info->delta_disks = map->num_members - prev_map->num_members;
493f5dd6
N
3606 if (info->delta_disks) {
3607 /* this needs to be applied to every array
3608 * in the container.
3609 */
81219e70 3610 info->reshape_active = CONTAINER_RESHAPE;
493f5dd6 3611 }
3f83228a
N
3612 /* We shape information that we give to md might have to be
3613 * modify to cope with md's requirement for reshaping arrays.
3614 * For example, when reshaping a RAID0, md requires it to be
3615 * presented as a degraded RAID4.
3616 * Also if a RAID0 is migrating to a RAID5 we need to specify
3617 * the array as already being RAID5, but the 'before' layout
3618 * is a RAID4-like layout.
3619 */
3620 switch (info->array.level) {
3621 case 0:
3622 switch(info->new_level) {
3623 case 0:
3624 /* conversion is happening as RAID4 */
3625 info->array.level = 4;
3626 info->array.raid_disks += 1;
3627 break;
3628 case 5:
3629 /* conversion is happening as RAID5 */
3630 info->array.level = 5;
3631 info->array.layout = ALGORITHM_PARITY_N;
3f83228a
N
3632 info->delta_disks -= 1;
3633 break;
3634 default:
3635 /* FIXME error message */
3636 info->array.level = UnSet;
3637 break;
3638 }
3639 break;
3640 }
b335e593
AK
3641 } else {
3642 info->new_level = UnSet;
3643 info->new_layout = UnSet;
3644 info->new_chunk = info->array.chunk_size;
3f83228a 3645 info->delta_disks = 0;
b335e593 3646 }
ca0748fa 3647
efb30e7f
DW
3648 if (dl) {
3649 info->disk.major = dl->major;
3650 info->disk.minor = dl->minor;
ca0748fa 3651 info->disk.number = dl->index;
656b6b5a
N
3652 info->disk.raid_disk = get_imsm_disk_slot(map_to_analyse,
3653 dl->index);
efb30e7f 3654 }
bf5a934a 3655
5551b113 3656 info->data_offset = pba_of_lba0(map_to_analyse);
44490938 3657 info->component_size = calc_component_size(map, dev);
3e684231 3658 info->component_size = imsm_component_size_alignment_check(
c41e00b2
AK
3659 info->array.level,
3660 info->array.chunk_size,
f36a9ecd 3661 super->sector_size,
c41e00b2 3662 info->component_size);
5e46202e 3663 info->bb.supported = 1;
139dae11 3664
301406c9 3665 memset(info->uuid, 0, sizeof(info->uuid));
921d9e16 3666 info->recovery_start = MaxSector;
bf5a934a 3667
c2462068
PB
3668 if (info->array.level == 5 &&
3669 (dev->rwh_policy == RWH_DISTRIBUTED ||
3670 dev->rwh_policy == RWH_MULTIPLE_DISTRIBUTED)) {
2432ce9b
AP
3671 info->consistency_policy = CONSISTENCY_POLICY_PPL;
3672 info->ppl_sector = get_ppl_sector(super, super->current_vol);
c2462068
PB
3673 if (dev->rwh_policy == RWH_MULTIPLE_DISTRIBUTED)
3674 info->ppl_size = MULTIPLE_PPL_AREA_SIZE_IMSM >> 9;
3675 else
3676 info->ppl_size = (PPL_HEADER_SIZE + PPL_ENTRY_SPACE)
3677 >> 9;
2432ce9b
AP
3678 } else if (info->array.level <= 0) {
3679 info->consistency_policy = CONSISTENCY_POLICY_NONE;
3680 } else {
fbc42556
JR
3681 if (dev->rwh_policy == RWH_BITMAP) {
3682 info->bitmap_offset = get_bitmap_sector(super, super->current_vol);
3683 info->consistency_policy = CONSISTENCY_POLICY_BITMAP;
3684 } else {
3685 info->consistency_policy = CONSISTENCY_POLICY_RESYNC;
3686 }
2432ce9b
AP
3687 }
3688
d2e6d5d6 3689 info->reshape_progress = 0;
b6796ce1 3690 info->resync_start = MaxSector;
b9172665 3691 if ((map_to_analyse->map_state == IMSM_T_STATE_UNINITIALIZED ||
2432ce9b 3692 !(info->array.state & 1)) &&
b9172665 3693 imsm_reshape_blocks_arrays_changes(super) == 0) {
301406c9 3694 info->resync_start = 0;
b6796ce1
AK
3695 }
3696 if (dev->vol.migr_state) {
1e5c6983
DW
3697 switch (migr_type(dev)) {
3698 case MIGR_REPAIR:
3699 case MIGR_INIT: {
c47b0ff6
AK
3700 __u64 blocks_per_unit = blocks_per_migr_unit(super,
3701 dev);
4036e7ee 3702 __u64 units = vol_curr_migr_unit(dev);
1e5c6983
DW
3703
3704 info->resync_start = blocks_per_unit * units;
3705 break;
3706 }
d2e6d5d6 3707 case MIGR_GEN_MIGR: {
c47b0ff6
AK
3708 __u64 blocks_per_unit = blocks_per_migr_unit(super,
3709 dev);
9f421827 3710 __u64 units = current_migr_unit(migr_rec);
04fa9523 3711 int used_disks;
d2e6d5d6 3712
befb629b
AK
3713 if (__le32_to_cpu(migr_rec->ascending_migr) &&
3714 (units <
9f421827 3715 (get_num_migr_units(migr_rec)-1)) &&
befb629b
AK
3716 (super->migr_rec->rec_status ==
3717 __cpu_to_le32(UNIT_SRC_IN_CP_AREA)))
3718 units++;
3719
d2e6d5d6 3720 info->reshape_progress = blocks_per_unit * units;
6289d1e0 3721
7a862a02 3722 dprintf("IMSM: General Migration checkpoint : %llu (%llu) -> read reshape progress : %llu\n",
19986c72
MB
3723 (unsigned long long)units,
3724 (unsigned long long)blocks_per_unit,
3725 info->reshape_progress);
75156c46 3726
9529d343 3727 used_disks = imsm_num_data_members(prev_map);
75156c46 3728 if (used_disks > 0) {
895ffd99 3729 info->custom_array_size = per_dev_array_size(map) *
75156c46 3730 used_disks;
75156c46 3731 }
d2e6d5d6 3732 }
1e5c6983
DW
3733 case MIGR_VERIFY:
3734 /* we could emulate the checkpointing of
3735 * 'sync_action=check' migrations, but for now
3736 * we just immediately complete them
3737 */
3738 case MIGR_REBUILD:
3739 /* this is handled by container_content_imsm() */
1e5c6983
DW
3740 case MIGR_STATE_CHANGE:
3741 /* FIXME handle other migrations */
3742 default:
3743 /* we are not dirty, so... */
3744 info->resync_start = MaxSector;
3745 }
b6796ce1 3746 }
301406c9
DW
3747
3748 strncpy(info->name, (char *) dev->volume, MAX_RAID_SERIAL_LEN);
3749 info->name[MAX_RAID_SERIAL_LEN] = 0;
bf5a934a 3750
f35f2525
N
3751 info->array.major_version = -1;
3752 info->array.minor_version = -2;
4dd2df09 3753 sprintf(info->text_version, "/%s/%d", st->container_devnm, info->container_member);
a67dd8cc 3754 info->safe_mode_delay = 4000; /* 4 secs like the Matrix driver */
51006d85 3755 uuid_from_super_imsm(st, info->uuid);
a5d85af7
N
3756
3757 if (dmap) {
3758 int i, j;
3759 for (i=0; i<map_disks; i++) {
3760 dmap[i] = 0;
3761 if (i < info->array.raid_disks) {
3762 struct imsm_disk *dsk;
238c0a71 3763 j = get_imsm_disk_idx(dev, i, MAP_X);
a5d85af7
N
3764 dsk = get_imsm_disk(super, j);
3765 if (dsk && (dsk->status & CONFIGURED_DISK))
3766 dmap[i] = 1;
3767 }
3768 }
3769 }
81ac8b4d 3770}
bf5a934a 3771
3b451610
AK
3772static __u8 imsm_check_degraded(struct intel_super *super, struct imsm_dev *dev,
3773 int failed, int look_in_map);
3774
3775static int imsm_count_failed(struct intel_super *super, struct imsm_dev *dev,
3776 int look_in_map);
3777
3778static void manage_second_map(struct intel_super *super, struct imsm_dev *dev)
3779{
3780 if (is_gen_migration(dev)) {
3781 int failed;
3782 __u8 map_state;
3783 struct imsm_map *map2 = get_imsm_map(dev, MAP_1);
3784
3785 failed = imsm_count_failed(super, dev, MAP_1);
238c0a71 3786 map_state = imsm_check_degraded(super, dev, failed, MAP_1);
3b451610
AK
3787 if (map2->map_state != map_state) {
3788 map2->map_state = map_state;
3789 super->updates_pending++;
3790 }
3791 }
3792}
97b4d0e9
DW
3793
3794static struct imsm_disk *get_imsm_missing(struct intel_super *super, __u8 index)
3795{
3796 struct dl *d;
3797
3798 for (d = super->missing; d; d = d->next)
3799 if (d->index == index)
3800 return &d->disk;
3801 return NULL;
3802}
3803
a5d85af7 3804static void getinfo_super_imsm(struct supertype *st, struct mdinfo *info, char *map)
4f5bc454
DW
3805{
3806 struct intel_super *super = st->sb;
4f5bc454 3807 struct imsm_disk *disk;
a5d85af7 3808 int map_disks = info->array.raid_disks;
ab3cb6b3
N
3809 int max_enough = -1;
3810 int i;
3811 struct imsm_super *mpb;
4f5bc454 3812
bf5a934a 3813 if (super->current_vol >= 0) {
a5d85af7 3814 getinfo_super_imsm_volume(st, info, map);
bf5a934a
DW
3815 return;
3816 }
95eeceeb 3817 memset(info, 0, sizeof(*info));
d23fe947
DW
3818
3819 /* Set raid_disks to zero so that Assemble will always pull in valid
3820 * spares
3821 */
3822 info->array.raid_disks = 0;
cdddbdbc
DW
3823 info->array.level = LEVEL_CONTAINER;
3824 info->array.layout = 0;
3825 info->array.md_minor = -1;
1011e834 3826 info->array.ctime = 0; /* N/A for imsm */
cdddbdbc
DW
3827 info->array.utime = 0;
3828 info->array.chunk_size = 0;
3829
3830 info->disk.major = 0;
3831 info->disk.minor = 0;
cdddbdbc 3832 info->disk.raid_disk = -1;
c2c087e6 3833 info->reshape_active = 0;
f35f2525
N
3834 info->array.major_version = -1;
3835 info->array.minor_version = -2;
c2c087e6 3836 strcpy(info->text_version, "imsm");
a67dd8cc 3837 info->safe_mode_delay = 0;
c2c087e6
DW
3838 info->disk.number = -1;
3839 info->disk.state = 0;
c5afc314 3840 info->name[0] = 0;
921d9e16 3841 info->recovery_start = MaxSector;
3ad25638 3842 info->recovery_blocked = imsm_reshape_blocks_arrays_changes(st->sb);
5e46202e 3843 info->bb.supported = 1;
c2c087e6 3844
97b4d0e9 3845 /* do we have the all the insync disks that we expect? */
ab3cb6b3 3846 mpb = super->anchor;
b7d81a38 3847 info->events = __le32_to_cpu(mpb->generation_num);
97b4d0e9 3848
ab3cb6b3
N
3849 for (i = 0; i < mpb->num_raid_devs; i++) {
3850 struct imsm_dev *dev = get_imsm_dev(super, i);
3851 int failed, enough, j, missing = 0;
3852 struct imsm_map *map;
3853 __u8 state;
97b4d0e9 3854
3b451610
AK
3855 failed = imsm_count_failed(super, dev, MAP_0);
3856 state = imsm_check_degraded(super, dev, failed, MAP_0);
238c0a71 3857 map = get_imsm_map(dev, MAP_0);
ab3cb6b3
N
3858
3859 /* any newly missing disks?
3860 * (catches single-degraded vs double-degraded)
3861 */
3862 for (j = 0; j < map->num_members; j++) {
238c0a71 3863 __u32 ord = get_imsm_ord_tbl_ent(dev, j, MAP_0);
ab3cb6b3
N
3864 __u32 idx = ord_to_idx(ord);
3865
20dc76d1
MT
3866 if (super->disks && super->disks->index == (int)idx)
3867 info->disk.raid_disk = j;
3868
ab3cb6b3
N
3869 if (!(ord & IMSM_ORD_REBUILD) &&
3870 get_imsm_missing(super, idx)) {
3871 missing = 1;
3872 break;
3873 }
97b4d0e9 3874 }
ab3cb6b3
N
3875
3876 if (state == IMSM_T_STATE_FAILED)
3877 enough = -1;
3878 else if (state == IMSM_T_STATE_DEGRADED &&
3879 (state != map->map_state || missing))
3880 enough = 0;
3881 else /* we're normal, or already degraded */
3882 enough = 1;
d2bde6d3
AK
3883 if (is_gen_migration(dev) && missing) {
3884 /* during general migration we need all disks
3885 * that process is running on.
3886 * No new missing disk is allowed.
3887 */
3888 max_enough = -1;
3889 enough = -1;
3890 /* no more checks necessary
3891 */
3892 break;
3893 }
ab3cb6b3
N
3894 /* in the missing/failed disk case check to see
3895 * if at least one array is runnable
3896 */
3897 max_enough = max(max_enough, enough);
3898 }
1ade5cc1 3899 dprintf("enough: %d\n", max_enough);
ab3cb6b3 3900 info->container_enough = max_enough;
97b4d0e9 3901
4a04ec6c 3902 if (super->disks) {
14e8215b
DW
3903 __u32 reserved = imsm_reserved_sectors(super, super->disks);
3904
b9f594fe 3905 disk = &super->disks->disk;
5551b113 3906 info->data_offset = total_blocks(&super->disks->disk) - reserved;
14e8215b 3907 info->component_size = reserved;
25ed7e59 3908 info->disk.state = is_configured(disk) ? (1 << MD_DISK_ACTIVE) : 0;
df474657
DW
3909 /* we don't change info->disk.raid_disk here because
3910 * this state will be finalized in mdmon after we have
3911 * found the 'most fresh' version of the metadata
3912 */
25ed7e59 3913 info->disk.state |= is_failed(disk) ? (1 << MD_DISK_FAULTY) : 0;
2432ce9b
AP
3914 info->disk.state |= (is_spare(disk) || is_journal(disk)) ?
3915 0 : (1 << MD_DISK_SYNC);
cdddbdbc 3916 }
a575e2a7
DW
3917
3918 /* only call uuid_from_super_imsm when this disk is part of a populated container,
3919 * ->compare_super may have updated the 'num_raid_devs' field for spares
3920 */
3921 if (info->disk.state & (1 << MD_DISK_SYNC) || super->anchor->num_raid_devs)
36ba7d48 3922 uuid_from_super_imsm(st, info->uuid);
22e263f6
AC
3923 else
3924 memcpy(info->uuid, uuid_zero, sizeof(uuid_zero));
a5d85af7
N
3925
3926 /* I don't know how to compute 'map' on imsm, so use safe default */
3927 if (map) {
3928 int i;
3929 for (i = 0; i < map_disks; i++)
3930 map[i] = 1;
3931 }
3932
cdddbdbc
DW
3933}
3934
5c4cd5da
AC
3935/* allocates memory and fills disk in mdinfo structure
3936 * for each disk in array */
3937struct mdinfo *getinfo_super_disks_imsm(struct supertype *st)
3938{
594dc1b8 3939 struct mdinfo *mddev;
5c4cd5da
AC
3940 struct intel_super *super = st->sb;
3941 struct imsm_disk *disk;
3942 int count = 0;
3943 struct dl *dl;
3944 if (!super || !super->disks)
3945 return NULL;
3946 dl = super->disks;
503975b9 3947 mddev = xcalloc(1, sizeof(*mddev));
5c4cd5da
AC
3948 while (dl) {
3949 struct mdinfo *tmp;
3950 disk = &dl->disk;
503975b9 3951 tmp = xcalloc(1, sizeof(*tmp));
5c4cd5da
AC
3952 if (mddev->devs)
3953 tmp->next = mddev->devs;
3954 mddev->devs = tmp;
3955 tmp->disk.number = count++;
3956 tmp->disk.major = dl->major;
3957 tmp->disk.minor = dl->minor;
3958 tmp->disk.state = is_configured(disk) ?
3959 (1 << MD_DISK_ACTIVE) : 0;
3960 tmp->disk.state |= is_failed(disk) ? (1 << MD_DISK_FAULTY) : 0;
3961 tmp->disk.state |= is_spare(disk) ? 0 : (1 << MD_DISK_SYNC);
3962 tmp->disk.raid_disk = -1;
3963 dl = dl->next;
3964 }
3965 return mddev;
3966}
3967
cdddbdbc 3968static int update_super_imsm(struct supertype *st, struct mdinfo *info,
03312b52
MK
3969 enum update_opt update, char *devname,
3970 int verbose, int uuid_set, char *homehost)
cdddbdbc 3971{
f352c545
DW
3972 /* For 'assemble' and 'force' we need to return non-zero if any
3973 * change was made. For others, the return value is ignored.
3974 * Update options are:
3975 * force-one : This device looks a bit old but needs to be included,
3976 * update age info appropriately.
3977 * assemble: clear any 'faulty' flag to allow this device to
3978 * be assembled.
3979 * force-array: Array is degraded but being forced, mark it clean
3980 * if that will be needed to assemble it.
3981 *
3982 * newdev: not used ????
3983 * grow: Array has gained a new device - this is currently for
3984 * linear only
3985 * resync: mark as dirty so a resync will happen.
3986 * name: update the name - preserving the homehost
6e46bf34 3987 * uuid: Change the uuid of the array to match watch is given
f352c545
DW
3988 *
3989 * Following are not relevant for this imsm:
3990 * sparc2.2 : update from old dodgey metadata
3991 * super-minor: change the preferred_minor number
3992 * summaries: update redundant counters.
f352c545
DW
3993 * homehost: update the recorded homehost
3994 * _reshape_progress: record new reshape_progress position.
3995 */
6e46bf34
DW
3996 int rv = 1;
3997 struct intel_super *super = st->sb;
3998 struct imsm_super *mpb;
f352c545 3999
6e46bf34
DW
4000 /* we can only update container info */
4001 if (!super || super->current_vol >= 0 || !super->anchor)
4002 return 1;
4003
4004 mpb = super->anchor;
4005
03312b52 4006 switch (update) {
4345e135 4007 case UOPT_UUID:
81a5b4f5
N
4008 /* We take this to mean that the family_num should be updated.
4009 * However that is much smaller than the uuid so we cannot really
4010 * allow an explicit uuid to be given. And it is hard to reliably
4011 * know if one was.
4012 * So if !uuid_set we know the current uuid is random and just used
4013 * the first 'int' and copy it to the other 3 positions.
4014 * Otherwise we require the 4 'int's to be the same as would be the
4015 * case if we are using a random uuid. So an explicit uuid will be
4016 * accepted as long as all for ints are the same... which shouldn't hurt
6e46bf34 4017 */
81a5b4f5
N
4018 if (!uuid_set) {
4019 info->uuid[1] = info->uuid[2] = info->uuid[3] = info->uuid[0];
6e46bf34 4020 rv = 0;
81a5b4f5
N
4021 } else {
4022 if (info->uuid[0] != info->uuid[1] ||
4023 info->uuid[1] != info->uuid[2] ||
4024 info->uuid[2] != info->uuid[3])
4025 rv = -1;
4026 else
4027 rv = 0;
6e46bf34 4028 }
81a5b4f5
N
4029 if (rv == 0)
4030 mpb->orig_family_num = info->uuid[0];
4345e135
MK
4031 break;
4032 case UOPT_SPEC_ASSEMBLE:
6e46bf34 4033 rv = 0;
4345e135
MK
4034 break;
4035 default:
1e2b2765 4036 rv = -1;
4345e135
MK
4037 break;
4038 }
f352c545 4039
6e46bf34
DW
4040 /* successful update? recompute checksum */
4041 if (rv == 0)
4042 mpb->check_sum = __le32_to_cpu(__gen_imsm_checksum(mpb));
f352c545
DW
4043
4044 return rv;
cdddbdbc
DW
4045}
4046
c2c087e6 4047static size_t disks_to_mpb_size(int disks)
cdddbdbc 4048{
c2c087e6 4049 size_t size;
cdddbdbc 4050
c2c087e6
DW
4051 size = sizeof(struct imsm_super);
4052 size += (disks - 1) * sizeof(struct imsm_disk);
4053 size += 2 * sizeof(struct imsm_dev);
4054 /* up to 2 maps per raid device (-2 for imsm_maps in imsm_dev */
4055 size += (4 - 2) * sizeof(struct imsm_map);
4056 /* 4 possible disk_ord_tbl's */
4057 size += 4 * (disks - 1) * sizeof(__u32);
bbab0940
TM
4058 /* maximum bbm log */
4059 size += sizeof(struct bbm_log);
c2c087e6
DW
4060
4061 return size;
4062}
4063
387fcd59
N
4064static __u64 avail_size_imsm(struct supertype *st, __u64 devsize,
4065 unsigned long long data_offset)
c2c087e6
DW
4066{
4067 if (devsize < (MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS))
4068 return 0;
4069
4070 return devsize - (MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS);
cdddbdbc
DW
4071}
4072
ba2de7ba
DW
4073static void free_devlist(struct intel_super *super)
4074{
4075 struct intel_dev *dv;
4076
4077 while (super->devlist) {
4078 dv = super->devlist->next;
4079 free(super->devlist->dev);
4080 free(super->devlist);
4081 super->devlist = dv;
4082 }
4083}
4084
4085static void imsm_copy_dev(struct imsm_dev *dest, struct imsm_dev *src)
4086{
4087 memcpy(dest, src, sizeof_imsm_dev(src, 0));
4088}
4089
c7b8547c
MT
4090static int compare_super_imsm(struct supertype *st, struct supertype *tst,
4091 int verbose)
cdddbdbc 4092{
601ffa78 4093 /* return:
cdddbdbc 4094 * 0 same, or first was empty, and second was copied
601ffa78 4095 * 1 sb are different
cdddbdbc
DW
4096 */
4097 struct intel_super *first = st->sb;
4098 struct intel_super *sec = tst->sb;
4099
5d500228
N
4100 if (!first) {
4101 st->sb = tst->sb;
4102 tst->sb = NULL;
4103 return 0;
4104 }
601ffa78 4105
8603ea6f
LM
4106 /* in platform dependent environment test if the disks
4107 * use the same Intel hba
601ffa78
OS
4108 * if not on Intel hba at all, allow anything.
4109 * doesn't check HBAs if num_raid_devs is not set, as it means
4110 * it is a free floating spare, and all spares regardless of HBA type
4111 * will fall into separate container during the assembly
8603ea6f 4112 */
601ffa78 4113 if (first->hba && sec->hba && first->anchor->num_raid_devs != 0) {
6b781d33 4114 if (first->hba->type != sec->hba->type) {
c7b8547c
MT
4115 if (verbose)
4116 pr_err("HBAs of devices do not match %s != %s\n",
4117 get_sys_dev_type(first->hba->type),
4118 get_sys_dev_type(sec->hba->type));
601ffa78 4119 return 1;
6b781d33
AP
4120 }
4121 if (first->orom != sec->orom) {
c7b8547c
MT
4122 if (verbose)
4123 pr_err("HBAs of devices do not match %s != %s\n",
4124 first->hba->pci_id, sec->hba->pci_id);
601ffa78 4125 return 1;
8603ea6f
LM
4126 }
4127 }
cdddbdbc 4128
d23fe947
DW
4129 if (first->anchor->num_raid_devs > 0 &&
4130 sec->anchor->num_raid_devs > 0) {
a2b97981
DW
4131 /* Determine if these disks might ever have been
4132 * related. Further disambiguation can only take place
4133 * in load_super_imsm_all
4134 */
4135 __u32 first_family = first->anchor->orig_family_num;
4136 __u32 sec_family = sec->anchor->orig_family_num;
4137
f796af5d
DW
4138 if (memcmp(first->anchor->sig, sec->anchor->sig,
4139 MAX_SIGNATURE_LENGTH) != 0)
601ffa78 4140 return 1;
f796af5d 4141
a2b97981
DW
4142 if (first_family == 0)
4143 first_family = first->anchor->family_num;
4144 if (sec_family == 0)
4145 sec_family = sec->anchor->family_num;
4146
4147 if (first_family != sec_family)
601ffa78 4148 return 1;
f796af5d 4149
d23fe947 4150 }
cdddbdbc 4151
601ffa78
OS
4152 /* if an anchor does not have num_raid_devs set then it is a free
4153 * floating spare. don't assosiate spare with any array, as during assembly
4154 * spares shall fall into separate container, from which they can be moved
4155 * when necessary
4156 */
4157 if (first->anchor->num_raid_devs ^ sec->anchor->num_raid_devs)
4158 return 1;
3e372e5a 4159
cdddbdbc
DW
4160 return 0;
4161}
4162
0030e8d6
DW
4163static void fd2devname(int fd, char *name)
4164{
0030e8d6 4165 char *nm;
0030e8d6 4166
7c798f87
MT
4167 nm = fd2kname(fd);
4168 if (!nm)
0030e8d6 4169 return;
9587c373 4170
7c798f87 4171 snprintf(name, MAX_RAID_SERIAL_LEN, "/dev/%s", nm);
0030e8d6
DW
4172}
4173
21e9380b
AP
4174static int nvme_get_serial(int fd, void *buf, size_t buf_len)
4175{
fcebeb77 4176 char path[PATH_MAX];
21e9380b
AP
4177 char *name = fd2kname(fd);
4178
4179 if (!name)
4180 return 1;
4181
4182 if (strncmp(name, "nvme", 4) != 0)
4183 return 1;
4184
fcebeb77
MT
4185 if (!diskfd_to_devpath(fd, 1, path))
4186 return 1;
21e9380b 4187
fcebeb77 4188 return devpath_to_char(path, "serial", buf, buf_len, 0);
21e9380b
AP
4189}
4190
cdddbdbc
DW
4191extern int scsi_get_serial(int fd, void *buf, size_t buf_len);
4192
4193static int imsm_read_serial(int fd, char *devname,
6da53c0e 4194 __u8 *serial, size_t serial_buf_len)
cdddbdbc 4195{
21e9380b 4196 char buf[50];
cdddbdbc 4197 int rv;
6da53c0e 4198 size_t len;
316e2bf4
DW
4199 char *dest;
4200 char *src;
21e9380b
AP
4201 unsigned int i;
4202
4203 memset(buf, 0, sizeof(buf));
cdddbdbc 4204
21e9380b 4205 rv = nvme_get_serial(fd, buf, sizeof(buf));
cdddbdbc 4206
21e9380b
AP
4207 if (rv)
4208 rv = scsi_get_serial(fd, buf, sizeof(buf));
f9ba0ff1 4209
40ebbb9c 4210 if (rv && check_env("IMSM_DEVNAME_AS_SERIAL")) {
f9ba0ff1
DW
4211 memset(serial, 0, MAX_RAID_SERIAL_LEN);
4212 fd2devname(fd, (char *) serial);
0030e8d6
DW
4213 return 0;
4214 }
4215
cdddbdbc
DW
4216 if (rv != 0) {
4217 if (devname)
e7b84f9d
N
4218 pr_err("Failed to retrieve serial for %s\n",
4219 devname);
cdddbdbc
DW
4220 return rv;
4221 }
4222
316e2bf4
DW
4223 /* trim all whitespace and non-printable characters and convert
4224 * ':' to ';'
4225 */
21e9380b
AP
4226 for (i = 0, dest = buf; i < sizeof(buf) && buf[i]; i++) {
4227 src = &buf[i];
316e2bf4
DW
4228 if (*src > 0x20) {
4229 /* ':' is reserved for use in placeholder serial
4230 * numbers for missing disks
4231 */
4232 if (*src == ':')
4233 *dest++ = ';';
4234 else
4235 *dest++ = *src;
4236 }
4237 }
21e9380b
AP
4238 len = dest - buf;
4239 dest = buf;
316e2bf4 4240
6da53c0e
BK
4241 if (len > serial_buf_len) {
4242 /* truncate leading characters */
4243 dest += len - serial_buf_len;
4244 len = serial_buf_len;
316e2bf4 4245 }
5c3db629 4246
6da53c0e 4247 memset(serial, 0, serial_buf_len);
316e2bf4 4248 memcpy(serial, dest, len);
cdddbdbc
DW
4249
4250 return 0;
4251}
4252
1f24f035
DW
4253static int serialcmp(__u8 *s1, __u8 *s2)
4254{
4255 return strncmp((char *) s1, (char *) s2, MAX_RAID_SERIAL_LEN);
4256}
4257
4258static void serialcpy(__u8 *dest, __u8 *src)
4259{
4260 strncpy((char *) dest, (char *) src, MAX_RAID_SERIAL_LEN);
4261}
4262
54c2c1ea
DW
4263static struct dl *serial_to_dl(__u8 *serial, struct intel_super *super)
4264{
4265 struct dl *dl;
4266
4267 for (dl = super->disks; dl; dl = dl->next)
4268 if (serialcmp(dl->serial, serial) == 0)
4269 break;
4270
4271 return dl;
4272}
4273
a2b97981
DW
4274static struct imsm_disk *
4275__serial_to_disk(__u8 *serial, struct imsm_super *mpb, int *idx)
4276{
4277 int i;
4278
4279 for (i = 0; i < mpb->num_disks; i++) {
4280 struct imsm_disk *disk = __get_imsm_disk(mpb, i);
4281
4282 if (serialcmp(disk->serial, serial) == 0) {
4283 if (idx)
4284 *idx = i;
4285 return disk;
4286 }
4287 }
4288
4289 return NULL;
4290}
4291
cdddbdbc
DW
4292static int
4293load_imsm_disk(int fd, struct intel_super *super, char *devname, int keep_fd)
4294{
a2b97981 4295 struct imsm_disk *disk;
cdddbdbc
DW
4296 struct dl *dl;
4297 struct stat stb;
cdddbdbc 4298 int rv;
a2b97981 4299 char name[40];
d23fe947
DW
4300 __u8 serial[MAX_RAID_SERIAL_LEN];
4301
6da53c0e 4302 rv = imsm_read_serial(fd, devname, serial, MAX_RAID_SERIAL_LEN);
d23fe947
DW
4303
4304 if (rv != 0)
4305 return 2;
4306
503975b9 4307 dl = xcalloc(1, sizeof(*dl));
cdddbdbc 4308
a2b97981
DW
4309 fstat(fd, &stb);
4310 dl->major = major(stb.st_rdev);
4311 dl->minor = minor(stb.st_rdev);
4312 dl->next = super->disks;
4313 dl->fd = keep_fd ? fd : -1;
4314 assert(super->disks == NULL);
4315 super->disks = dl;
4316 serialcpy(dl->serial, serial);
4317 dl->index = -2;
4318 dl->e = NULL;
4319 fd2devname(fd, name);
4320 if (devname)
503975b9 4321 dl->devname = xstrdup(devname);
a2b97981 4322 else
503975b9 4323 dl->devname = xstrdup(name);
cdddbdbc 4324
d23fe947 4325 /* look up this disk's index in the current anchor */
a2b97981
DW
4326 disk = __serial_to_disk(dl->serial, super->anchor, &dl->index);
4327 if (disk) {
4328 dl->disk = *disk;
4329 /* only set index on disks that are a member of a
4330 * populated contianer, i.e. one with raid_devs
4331 */
4332 if (is_failed(&dl->disk))
3f6efecc 4333 dl->index = -2;
2432ce9b 4334 else if (is_spare(&dl->disk) || is_journal(&dl->disk))
a2b97981 4335 dl->index = -1;
3f6efecc
DW
4336 }
4337
949c47a0
DW
4338 return 0;
4339}
4340
0c046afd
DW
4341/* When migrating map0 contains the 'destination' state while map1
4342 * contains the current state. When not migrating map0 contains the
4343 * current state. This routine assumes that map[0].map_state is set to
4344 * the current array state before being called.
4345 *
4346 * Migration is indicated by one of the following states
4347 * 1/ Idle (migr_state=0 map0state=normal||unitialized||degraded||failed)
e3bba0e0 4348 * 2/ Initialize (migr_state=1 migr_type=MIGR_INIT map0state=normal
0c046afd 4349 * map1state=unitialized)
1484e727 4350 * 3/ Repair (Resync) (migr_state=1 migr_type=MIGR_REPAIR map0state=normal
0c046afd 4351 * map1state=normal)
e3bba0e0 4352 * 4/ Rebuild (migr_state=1 migr_type=MIGR_REBUILD map0state=normal
0c046afd 4353 * map1state=degraded)
8e59f3d8
AK
4354 * 5/ Migration (mig_state=1 migr_type=MIGR_GEN_MIGR map0state=normal
4355 * map1state=normal)
0c046afd 4356 */
8e59f3d8
AK
4357static void migrate(struct imsm_dev *dev, struct intel_super *super,
4358 __u8 to_state, int migr_type)
3393c6af 4359{
0c046afd 4360 struct imsm_map *dest;
238c0a71 4361 struct imsm_map *src = get_imsm_map(dev, MAP_0);
3393c6af 4362
0c046afd 4363 dev->vol.migr_state = 1;
1484e727 4364 set_migr_type(dev, migr_type);
4036e7ee 4365 set_vol_curr_migr_unit(dev, 0);
238c0a71 4366 dest = get_imsm_map(dev, MAP_1);
0c046afd 4367
0556e1a2 4368 /* duplicate and then set the target end state in map[0] */
3393c6af 4369 memcpy(dest, src, sizeof_imsm_map(src));
fb12a745 4370 if (migr_type == MIGR_GEN_MIGR) {
0556e1a2
DW
4371 __u32 ord;
4372 int i;
4373
4374 for (i = 0; i < src->num_members; i++) {
4375 ord = __le32_to_cpu(src->disk_ord_tbl[i]);
4376 set_imsm_ord_tbl_ent(src, i, ord_to_idx(ord));
4377 }
4378 }
4379
8e59f3d8
AK
4380 if (migr_type == MIGR_GEN_MIGR)
4381 /* Clear migration record */
4382 memset(super->migr_rec, 0, sizeof(struct migr_record));
4383
0c046afd 4384 src->map_state = to_state;
949c47a0 4385}
f8f603f1 4386
809da78e
AK
4387static void end_migration(struct imsm_dev *dev, struct intel_super *super,
4388 __u8 map_state)
f8f603f1 4389{
238c0a71
AK
4390 struct imsm_map *map = get_imsm_map(dev, MAP_0);
4391 struct imsm_map *prev = get_imsm_map(dev, dev->vol.migr_state == 0 ?
4392 MAP_0 : MAP_1);
28bce06f 4393 int i, j;
0556e1a2
DW
4394
4395 /* merge any IMSM_ORD_REBUILD bits that were not successfully
4396 * completed in the last migration.
4397 *
28bce06f 4398 * FIXME add support for raid-level-migration
0556e1a2 4399 */
195d1d76 4400 if (map_state != map->map_state && (is_gen_migration(dev) == false) &&
089f9d79 4401 prev->map_state != IMSM_T_STATE_UNINITIALIZED) {
809da78e
AK
4402 /* when final map state is other than expected
4403 * merge maps (not for migration)
4404 */
4405 int failed;
4406
4407 for (i = 0; i < prev->num_members; i++)
4408 for (j = 0; j < map->num_members; j++)
4409 /* during online capacity expansion
4410 * disks position can be changed
4411 * if takeover is used
4412 */
4413 if (ord_to_idx(map->disk_ord_tbl[j]) ==
4414 ord_to_idx(prev->disk_ord_tbl[i])) {
4415 map->disk_ord_tbl[j] |=
4416 prev->disk_ord_tbl[i];
4417 break;
4418 }
4419 failed = imsm_count_failed(super, dev, MAP_0);
4420 map_state = imsm_check_degraded(super, dev, failed, MAP_0);
4421 }
f8f603f1
DW
4422
4423 dev->vol.migr_state = 0;
ea672ee1 4424 set_migr_type(dev, 0);
4036e7ee 4425 set_vol_curr_migr_unit(dev, 0);
f8f603f1
DW
4426 map->map_state = map_state;
4427}
949c47a0
DW
4428
4429static int parse_raid_devices(struct intel_super *super)
4430{
4431 int i;
4432 struct imsm_dev *dev_new;
4d7b1503 4433 size_t len, len_migr;
401d313b 4434 size_t max_len = 0;
4d7b1503
DW
4435 size_t space_needed = 0;
4436 struct imsm_super *mpb = super->anchor;
949c47a0
DW
4437
4438 for (i = 0; i < super->anchor->num_raid_devs; i++) {
4439 struct imsm_dev *dev_iter = __get_imsm_dev(super->anchor, i);
ba2de7ba 4440 struct intel_dev *dv;
949c47a0 4441
4d7b1503
DW
4442 len = sizeof_imsm_dev(dev_iter, 0);
4443 len_migr = sizeof_imsm_dev(dev_iter, 1);
4444 if (len_migr > len)
4445 space_needed += len_migr - len;
ca9de185 4446
503975b9 4447 dv = xmalloc(sizeof(*dv));
401d313b
AK
4448 if (max_len < len_migr)
4449 max_len = len_migr;
4450 if (max_len > len_migr)
4451 space_needed += max_len - len_migr;
503975b9 4452 dev_new = xmalloc(max_len);
949c47a0 4453 imsm_copy_dev(dev_new, dev_iter);
ba2de7ba
DW
4454 dv->dev = dev_new;
4455 dv->index = i;
4456 dv->next = super->devlist;
4457 super->devlist = dv;
949c47a0 4458 }
cdddbdbc 4459
4d7b1503
DW
4460 /* ensure that super->buf is large enough when all raid devices
4461 * are migrating
4462 */
4463 if (__le32_to_cpu(mpb->mpb_size) + space_needed > super->len) {
4464 void *buf;
4465
f36a9ecd
PB
4466 len = ROUND_UP(__le32_to_cpu(mpb->mpb_size) + space_needed,
4467 super->sector_size);
4468 if (posix_memalign(&buf, MAX_SECTOR_SIZE, len) != 0)
4d7b1503
DW
4469 return 1;
4470
1f45a8ad
DW
4471 memcpy(buf, super->buf, super->len);
4472 memset(buf + super->len, 0, len - super->len);
4d7b1503
DW
4473 free(super->buf);
4474 super->buf = buf;
4475 super->len = len;
4476 }
ca9de185 4477
bbab0940
TM
4478 super->extra_space += space_needed;
4479
cdddbdbc
DW
4480 return 0;
4481}
4482
e2f41b2c
AK
4483/*******************************************************************************
4484 * Function: check_mpb_migr_compatibility
4485 * Description: Function checks for unsupported migration features:
4486 * - migration optimization area (pba_of_lba0)
4487 * - descending reshape (ascending_migr)
4488 * Parameters:
4489 * super : imsm metadata information
4490 * Returns:
4491 * 0 : migration is compatible
4492 * -1 : migration is not compatible
4493 ******************************************************************************/
4494int check_mpb_migr_compatibility(struct intel_super *super)
4495{
4496 struct imsm_map *map0, *map1;
4497 struct migr_record *migr_rec = super->migr_rec;
4498 int i;
4499
4500 for (i = 0; i < super->anchor->num_raid_devs; i++) {
4501 struct imsm_dev *dev_iter = __get_imsm_dev(super->anchor, i);
4502
756a15f3 4503 if (dev_iter->vol.migr_state == 1 &&
e2f41b2c
AK
4504 dev_iter->vol.migr_type == MIGR_GEN_MIGR) {
4505 /* This device is migrating */
238c0a71
AK
4506 map0 = get_imsm_map(dev_iter, MAP_0);
4507 map1 = get_imsm_map(dev_iter, MAP_1);
5551b113 4508 if (pba_of_lba0(map0) != pba_of_lba0(map1))
e2f41b2c
AK
4509 /* migration optimization area was used */
4510 return -1;
fc54fe7a
JS
4511 if (migr_rec->ascending_migr == 0 &&
4512 migr_rec->dest_depth_per_unit > 0)
e2f41b2c
AK
4513 /* descending reshape not supported yet */
4514 return -1;
4515 }
4516 }
4517 return 0;
4518}
4519
d23fe947 4520static void __free_imsm(struct intel_super *super, int free_disks);
9ca2c81c 4521
cdddbdbc 4522/* load_imsm_mpb - read matrix metadata
f2f5c343 4523 * allocates super->mpb to be freed by free_imsm
cdddbdbc
DW
4524 */
4525static int load_imsm_mpb(int fd, struct intel_super *super, char *devname)
4526{
4527 unsigned long long dsize;
cdddbdbc 4528 unsigned long long sectors;
f36a9ecd 4529 unsigned int sector_size = super->sector_size;
cdddbdbc 4530 struct stat;
6416d527 4531 struct imsm_super *anchor;
cdddbdbc
DW
4532 __u32 check_sum;
4533
cdddbdbc 4534 get_dev_size(fd, NULL, &dsize);
f36a9ecd 4535 if (dsize < 2*sector_size) {
64436f06 4536 if (devname)
e7b84f9d
N
4537 pr_err("%s: device to small for imsm\n",
4538 devname);
64436f06
N
4539 return 1;
4540 }
cdddbdbc 4541
f36a9ecd 4542 if (lseek64(fd, dsize - (sector_size * 2), SEEK_SET) < 0) {
cdddbdbc 4543 if (devname)
e7b84f9d
N
4544 pr_err("Cannot seek to anchor block on %s: %s\n",
4545 devname, strerror(errno));
cdddbdbc
DW
4546 return 1;
4547 }
4548
f36a9ecd 4549 if (posix_memalign((void **)&anchor, sector_size, sector_size) != 0) {
ad97895e 4550 if (devname)
7a862a02 4551 pr_err("Failed to allocate imsm anchor buffer on %s\n", devname);
ad97895e
DW
4552 return 1;
4553 }
466070ad 4554 if ((unsigned int)read(fd, anchor, sector_size) != sector_size) {
cdddbdbc 4555 if (devname)
e7b84f9d
N
4556 pr_err("Cannot read anchor block on %s: %s\n",
4557 devname, strerror(errno));
6416d527 4558 free(anchor);
cdddbdbc
DW
4559 return 1;
4560 }
4561
6416d527 4562 if (strncmp((char *) anchor->sig, MPB_SIGNATURE, MPB_SIG_LEN) != 0) {
cdddbdbc 4563 if (devname)
e7b84f9d 4564 pr_err("no IMSM anchor on %s\n", devname);
6416d527 4565 free(anchor);
cdddbdbc
DW
4566 return 2;
4567 }
4568
d23fe947 4569 __free_imsm(super, 0);
f2f5c343
LM
4570 /* reload capability and hba */
4571
4572 /* capability and hba must be updated with new super allocation */
d424212e 4573 find_intel_hba_capability(fd, super, devname);
f36a9ecd
PB
4574 super->len = ROUND_UP(anchor->mpb_size, sector_size);
4575 if (posix_memalign(&super->buf, MAX_SECTOR_SIZE, super->len) != 0) {
cdddbdbc 4576 if (devname)
e7b84f9d
N
4577 pr_err("unable to allocate %zu byte mpb buffer\n",
4578 super->len);
6416d527 4579 free(anchor);
cdddbdbc
DW
4580 return 2;
4581 }
f36a9ecd 4582 memcpy(super->buf, anchor, sector_size);
cdddbdbc 4583
f36a9ecd 4584 sectors = mpb_sectors(anchor, sector_size) - 1;
6416d527 4585 free(anchor);
8e59f3d8 4586
85337573
AO
4587 if (posix_memalign(&super->migr_rec_buf, MAX_SECTOR_SIZE,
4588 MIGR_REC_BUF_SECTORS*MAX_SECTOR_SIZE) != 0) {
1ade5cc1 4589 pr_err("could not allocate migr_rec buffer\n");
8e59f3d8 4590 free(super->buf);
50cd06b4 4591 super->buf = NULL;
8e59f3d8
AK
4592 return 2;
4593 }
51d83f5d 4594 super->clean_migration_record_by_mdmon = 0;
8e59f3d8 4595
949c47a0 4596 if (!sectors) {
ecf45690
DW
4597 check_sum = __gen_imsm_checksum(super->anchor);
4598 if (check_sum != __le32_to_cpu(super->anchor->check_sum)) {
4599 if (devname)
e7b84f9d
N
4600 pr_err("IMSM checksum %x != %x on %s\n",
4601 check_sum,
4602 __le32_to_cpu(super->anchor->check_sum),
4603 devname);
ecf45690
DW
4604 return 2;
4605 }
4606
a2b97981 4607 return 0;
949c47a0 4608 }
cdddbdbc
DW
4609
4610 /* read the extended mpb */
f36a9ecd 4611 if (lseek64(fd, dsize - (sector_size * (2 + sectors)), SEEK_SET) < 0) {
cdddbdbc 4612 if (devname)
e7b84f9d
N
4613 pr_err("Cannot seek to extended mpb on %s: %s\n",
4614 devname, strerror(errno));
cdddbdbc
DW
4615 return 1;
4616 }
4617
f36a9ecd
PB
4618 if ((unsigned int)read(fd, super->buf + sector_size,
4619 super->len - sector_size) != super->len - sector_size) {
cdddbdbc 4620 if (devname)
e7b84f9d
N
4621 pr_err("Cannot read extended mpb on %s: %s\n",
4622 devname, strerror(errno));
cdddbdbc
DW
4623 return 2;
4624 }
4625
949c47a0
DW
4626 check_sum = __gen_imsm_checksum(super->anchor);
4627 if (check_sum != __le32_to_cpu(super->anchor->check_sum)) {
cdddbdbc 4628 if (devname)
e7b84f9d
N
4629 pr_err("IMSM checksum %x != %x on %s\n",
4630 check_sum, __le32_to_cpu(super->anchor->check_sum),
4631 devname);
db575f3b 4632 return 3;
cdddbdbc
DW
4633 }
4634
a2b97981
DW
4635 return 0;
4636}
4637
8e59f3d8
AK
4638static int read_imsm_migr_rec(int fd, struct intel_super *super);
4639
97f81ee2
CA
4640/* clears hi bits in metadata if MPB_ATTRIB_2TB_DISK not set */
4641static void clear_hi(struct intel_super *super)
4642{
4643 struct imsm_super *mpb = super->anchor;
4644 int i, n;
4645 if (mpb->attributes & MPB_ATTRIB_2TB_DISK)
4646 return;
4647 for (i = 0; i < mpb->num_disks; ++i) {
4648 struct imsm_disk *disk = &mpb->disk[i];
4649 disk->total_blocks_hi = 0;
4650 }
4651 for (i = 0; i < mpb->num_raid_devs; ++i) {
4652 struct imsm_dev *dev = get_imsm_dev(super, i);
97f81ee2
CA
4653 for (n = 0; n < 2; ++n) {
4654 struct imsm_map *map = get_imsm_map(dev, n);
4655 if (!map)
4656 continue;
4657 map->pba_of_lba0_hi = 0;
4658 map->blocks_per_member_hi = 0;
4659 map->num_data_stripes_hi = 0;
4660 }
4661 }
4662}
4663
a2b97981
DW
4664static int
4665load_and_parse_mpb(int fd, struct intel_super *super, char *devname, int keep_fd)
4666{
4667 int err;
4668
4669 err = load_imsm_mpb(fd, super, devname);
4670 if (err)
4671 return err;
f36a9ecd
PB
4672 if (super->sector_size == 4096)
4673 convert_from_4k(super);
a2b97981
DW
4674 err = load_imsm_disk(fd, super, devname, keep_fd);
4675 if (err)
4676 return err;
4677 err = parse_raid_devices(super);
8d67477f
TM
4678 if (err)
4679 return err;
4680 err = load_bbm_log(super);
97f81ee2 4681 clear_hi(super);
a2b97981 4682 return err;
cdddbdbc
DW
4683}
4684
4389ce73 4685static void __free_imsm_disk(struct dl *d, int do_close)
ae6aad82 4686{
4389ce73
MT
4687 if (do_close)
4688 close_fd(&d->fd);
ae6aad82
DW
4689 if (d->devname)
4690 free(d->devname);
0dcecb2e
DW
4691 if (d->e)
4692 free(d->e);
ae6aad82
DW
4693 free(d);
4694
4695}
1a64be56 4696
cdddbdbc
DW
4697static void free_imsm_disks(struct intel_super *super)
4698{
47ee5a45 4699 struct dl *d;
cdddbdbc 4700
47ee5a45
DW
4701 while (super->disks) {
4702 d = super->disks;
cdddbdbc 4703 super->disks = d->next;
3a85bf0e 4704 __free_imsm_disk(d, 1);
cdddbdbc 4705 }
cb82edca
AK
4706 while (super->disk_mgmt_list) {
4707 d = super->disk_mgmt_list;
4708 super->disk_mgmt_list = d->next;
3a85bf0e 4709 __free_imsm_disk(d, 1);
cb82edca 4710 }
47ee5a45
DW
4711 while (super->missing) {
4712 d = super->missing;
4713 super->missing = d->next;
3a85bf0e 4714 __free_imsm_disk(d, 1);
47ee5a45
DW
4715 }
4716
cdddbdbc
DW
4717}
4718
9ca2c81c 4719/* free all the pieces hanging off of a super pointer */
d23fe947 4720static void __free_imsm(struct intel_super *super, int free_disks)
cdddbdbc 4721{
88654014
LM
4722 struct intel_hba *elem, *next;
4723
9ca2c81c 4724 if (super->buf) {
949c47a0 4725 free(super->buf);
9ca2c81c
DW
4726 super->buf = NULL;
4727 }
f2f5c343
LM
4728 /* unlink capability description */
4729 super->orom = NULL;
8e59f3d8
AK
4730 if (super->migr_rec_buf) {
4731 free(super->migr_rec_buf);
4732 super->migr_rec_buf = NULL;
4733 }
d23fe947
DW
4734 if (free_disks)
4735 free_imsm_disks(super);
ba2de7ba 4736 free_devlist(super);
88654014
LM
4737 elem = super->hba;
4738 while (elem) {
4739 if (elem->path)
4740 free((void *)elem->path);
4741 next = elem->next;
4742 free(elem);
4743 elem = next;
88c32bb1 4744 }
8d67477f
TM
4745 if (super->bbm_log)
4746 free(super->bbm_log);
88654014 4747 super->hba = NULL;
cdddbdbc
DW
4748}
4749
9ca2c81c
DW
4750static void free_imsm(struct intel_super *super)
4751{
d23fe947 4752 __free_imsm(super, 1);
928f1424 4753 free(super->bb.entries);
9ca2c81c
DW
4754 free(super);
4755}
cdddbdbc
DW
4756
4757static void free_super_imsm(struct supertype *st)
4758{
4759 struct intel_super *super = st->sb;
4760
4761 if (!super)
4762 return;
4763
4764 free_imsm(super);
4765 st->sb = NULL;
4766}
4767
49133e57 4768static struct intel_super *alloc_super(void)
c2c087e6 4769{
503975b9 4770 struct intel_super *super = xcalloc(1, sizeof(*super));
c2c087e6 4771
503975b9
N
4772 super->current_vol = -1;
4773 super->create_offset = ~((unsigned long long) 0);
928f1424
TM
4774
4775 super->bb.entries = xmalloc(BBM_LOG_MAX_ENTRIES *
4776 sizeof(struct md_bb_entry));
4777 if (!super->bb.entries) {
4778 free(super);
4779 return NULL;
4780 }
4781
c2c087e6
DW
4782 return super;
4783}
4784
f0f5a016
LM
4785/*
4786 * find and allocate hba and OROM/EFI based on valid fd of RAID component device
4787 */
d424212e 4788static int find_intel_hba_capability(int fd, struct intel_super *super, char *devname)
f0f5a016
LM
4789{
4790 struct sys_dev *hba_name;
4791 int rv = 0;
4792
4389ce73 4793 if (is_fd_valid(fd) && test_partition(fd)) {
3a30e28e
MT
4794 pr_err("imsm: %s is a partition, cannot be used in IMSM\n",
4795 devname);
4796 return 1;
4797 }
420dafcd 4798 if (!is_fd_valid(fd) || check_no_platform()) {
f2f5c343 4799 super->orom = NULL;
f0f5a016
LM
4800 super->hba = NULL;
4801 return 0;
4802 }
4803 hba_name = find_disk_attached_hba(fd, NULL);
4804 if (!hba_name) {
d424212e 4805 if (devname)
e7b84f9d
N
4806 pr_err("%s is not attached to Intel(R) RAID controller.\n",
4807 devname);
f0f5a016
LM
4808 return 1;
4809 }
4810 rv = attach_hba_to_super(super, hba_name);
4811 if (rv == 2) {
d424212e
N
4812 if (devname) {
4813 struct intel_hba *hba = super->hba;
f0f5a016 4814
60f0f54d
PB
4815 pr_err("%s is attached to Intel(R) %s %s (%s),\n"
4816 " but the container is assigned to Intel(R) %s %s (",
d424212e 4817 devname,
614902f6 4818 get_sys_dev_type(hba_name->type),
75350d87
KF
4819 hba_name->type == SYS_DEV_VMD || hba_name->type == SYS_DEV_SATA_VMD ?
4820 "domain" : "RAID controller",
f0f5a016 4821 hba_name->pci_id ? : "Err!",
60f0f54d 4822 get_sys_dev_type(super->hba->type),
75350d87
KF
4823 hba->type == SYS_DEV_VMD || hba_name->type == SYS_DEV_SATA_VMD ?
4824 "domain" : "RAID controller");
f0f5a016 4825
f0f5a016
LM
4826 while (hba) {
4827 fprintf(stderr, "%s", hba->pci_id ? : "Err!");
4828 if (hba->next)
4829 fprintf(stderr, ", ");
4830 hba = hba->next;
4831 }
6b781d33 4832 fprintf(stderr, ").\n"
cca67208 4833 " Mixing devices attached to different controllers is not allowed.\n");
f0f5a016 4834 }
f0f5a016
LM
4835 return 2;
4836 }
6b781d33 4837 super->orom = find_imsm_capability(hba_name);
f2f5c343
LM
4838 if (!super->orom)
4839 return 3;
614902f6 4840
f0f5a016
LM
4841 return 0;
4842}
4843
47ee5a45
DW
4844/* find_missing - helper routine for load_super_imsm_all that identifies
4845 * disks that have disappeared from the system. This routine relies on
4846 * the mpb being uptodate, which it is at load time.
4847 */
4848static int find_missing(struct intel_super *super)
4849{
4850 int i;
4851 struct imsm_super *mpb = super->anchor;
4852 struct dl *dl;
4853 struct imsm_disk *disk;
47ee5a45
DW
4854
4855 for (i = 0; i < mpb->num_disks; i++) {
4856 disk = __get_imsm_disk(mpb, i);
54c2c1ea 4857 dl = serial_to_dl(disk->serial, super);
47ee5a45
DW
4858 if (dl)
4859 continue;
47ee5a45 4860
503975b9 4861 dl = xmalloc(sizeof(*dl));
47ee5a45
DW
4862 dl->major = 0;
4863 dl->minor = 0;
4864 dl->fd = -1;
503975b9 4865 dl->devname = xstrdup("missing");
47ee5a45
DW
4866 dl->index = i;
4867 serialcpy(dl->serial, disk->serial);
4868 dl->disk = *disk;
689c9bf3 4869 dl->e = NULL;
47ee5a45
DW
4870 dl->next = super->missing;
4871 super->missing = dl;
4872 }
4873
4874 return 0;
4875}
4876
a2b97981
DW
4877static struct intel_disk *disk_list_get(__u8 *serial, struct intel_disk *disk_list)
4878{
4879 struct intel_disk *idisk = disk_list;
4880
4881 while (idisk) {
4882 if (serialcmp(idisk->disk.serial, serial) == 0)
4883 break;
4884 idisk = idisk->next;
4885 }
4886
4887 return idisk;
4888}
4889
4890static int __prep_thunderdome(struct intel_super **table, int tbl_size,
4891 struct intel_super *super,
4892 struct intel_disk **disk_list)
4893{
4894 struct imsm_disk *d = &super->disks->disk;
4895 struct imsm_super *mpb = super->anchor;
4896 int i, j;
4897
4898 for (i = 0; i < tbl_size; i++) {
4899 struct imsm_super *tbl_mpb = table[i]->anchor;
4900 struct imsm_disk *tbl_d = &table[i]->disks->disk;
4901
4902 if (tbl_mpb->family_num == mpb->family_num) {
4903 if (tbl_mpb->check_sum == mpb->check_sum) {
1ade5cc1
N
4904 dprintf("mpb from %d:%d matches %d:%d\n",
4905 super->disks->major,
a2b97981
DW
4906 super->disks->minor,
4907 table[i]->disks->major,
4908 table[i]->disks->minor);
4909 break;
4910 }
4911
4912 if (((is_configured(d) && !is_configured(tbl_d)) ||
4913 is_configured(d) == is_configured(tbl_d)) &&
4914 tbl_mpb->generation_num < mpb->generation_num) {
4915 /* current version of the mpb is a
4916 * better candidate than the one in
4917 * super_table, but copy over "cross
4918 * generational" status
4919 */
4920 struct intel_disk *idisk;
4921
1ade5cc1
N
4922 dprintf("mpb from %d:%d replaces %d:%d\n",
4923 super->disks->major,
a2b97981
DW
4924 super->disks->minor,
4925 table[i]->disks->major,
4926 table[i]->disks->minor);
4927
4928 idisk = disk_list_get(tbl_d->serial, *disk_list);
4929 if (idisk && is_failed(&idisk->disk))
4930 tbl_d->status |= FAILED_DISK;
4931 break;
4932 } else {
4933 struct intel_disk *idisk;
4934 struct imsm_disk *disk;
4935
4936 /* tbl_mpb is more up to date, but copy
4937 * over cross generational status before
4938 * returning
4939 */
4940 disk = __serial_to_disk(d->serial, mpb, NULL);
4941 if (disk && is_failed(disk))
4942 d->status |= FAILED_DISK;
4943
4944 idisk = disk_list_get(d->serial, *disk_list);
4945 if (idisk) {
4946 idisk->owner = i;
4947 if (disk && is_configured(disk))
4948 idisk->disk.status |= CONFIGURED_DISK;
4949 }
4950
1ade5cc1
N
4951 dprintf("mpb from %d:%d prefer %d:%d\n",
4952 super->disks->major,
a2b97981
DW
4953 super->disks->minor,
4954 table[i]->disks->major,
4955 table[i]->disks->minor);
4956
4957 return tbl_size;
4958 }
4959 }
4960 }
4961
4962 if (i >= tbl_size)
4963 table[tbl_size++] = super;
4964 else
4965 table[i] = super;
4966
4967 /* update/extend the merged list of imsm_disk records */
4968 for (j = 0; j < mpb->num_disks; j++) {
4969 struct imsm_disk *disk = __get_imsm_disk(mpb, j);
4970 struct intel_disk *idisk;
4971
4972 idisk = disk_list_get(disk->serial, *disk_list);
4973 if (idisk) {
4974 idisk->disk.status |= disk->status;
4975 if (is_configured(&idisk->disk) ||
4976 is_failed(&idisk->disk))
4977 idisk->disk.status &= ~(SPARE_DISK);
4978 } else {
503975b9 4979 idisk = xcalloc(1, sizeof(*idisk));
a2b97981
DW
4980 idisk->owner = IMSM_UNKNOWN_OWNER;
4981 idisk->disk = *disk;
4982 idisk->next = *disk_list;
4983 *disk_list = idisk;
4984 }
4985
4986 if (serialcmp(idisk->disk.serial, d->serial) == 0)
4987 idisk->owner = i;
4988 }
4989
4990 return tbl_size;
4991}
4992
4993static struct intel_super *
4994validate_members(struct intel_super *super, struct intel_disk *disk_list,
4995 const int owner)
4996{
4997 struct imsm_super *mpb = super->anchor;
4998 int ok_count = 0;
4999 int i;
5000
5001 for (i = 0; i < mpb->num_disks; i++) {
5002 struct imsm_disk *disk = __get_imsm_disk(mpb, i);
5003 struct intel_disk *idisk;
5004
5005 idisk = disk_list_get(disk->serial, disk_list);
5006 if (idisk) {
5007 if (idisk->owner == owner ||
5008 idisk->owner == IMSM_UNKNOWN_OWNER)
5009 ok_count++;
5010 else
1ade5cc1
N
5011 dprintf("'%.16s' owner %d != %d\n",
5012 disk->serial, idisk->owner,
a2b97981
DW
5013 owner);
5014 } else {
1ade5cc1
N
5015 dprintf("unknown disk %x [%d]: %.16s\n",
5016 __le32_to_cpu(mpb->family_num), i,
a2b97981
DW
5017 disk->serial);
5018 break;
5019 }
5020 }
5021
5022 if (ok_count == mpb->num_disks)
5023 return super;
5024 return NULL;
5025}
5026
5027static void show_conflicts(__u32 family_num, struct intel_super *super_list)
5028{
5029 struct intel_super *s;
5030
5031 for (s = super_list; s; s = s->next) {
5032 if (family_num != s->anchor->family_num)
5033 continue;
e12b3daa 5034 pr_err("Conflict, offlining family %#x on '%s'\n",
a2b97981
DW
5035 __le32_to_cpu(family_num), s->disks->devname);
5036 }
5037}
5038
5039static struct intel_super *
5040imsm_thunderdome(struct intel_super **super_list, int len)
5041{
5042 struct intel_super *super_table[len];
5043 struct intel_disk *disk_list = NULL;
5044 struct intel_super *champion, *spare;
5045 struct intel_super *s, **del;
5046 int tbl_size = 0;
5047 int conflict;
5048 int i;
5049
5050 memset(super_table, 0, sizeof(super_table));
5051 for (s = *super_list; s; s = s->next)
5052 tbl_size = __prep_thunderdome(super_table, tbl_size, s, &disk_list);
5053
5054 for (i = 0; i < tbl_size; i++) {
5055 struct imsm_disk *d;
5056 struct intel_disk *idisk;
5057 struct imsm_super *mpb = super_table[i]->anchor;
5058
5059 s = super_table[i];
5060 d = &s->disks->disk;
5061
5062 /* 'd' must appear in merged disk list for its
5063 * configuration to be valid
5064 */
5065 idisk = disk_list_get(d->serial, disk_list);
5066 if (idisk && idisk->owner == i)
5067 s = validate_members(s, disk_list, i);
5068 else
5069 s = NULL;
5070
5071 if (!s)
1ade5cc1
N
5072 dprintf("marking family: %#x from %d:%d offline\n",
5073 mpb->family_num,
a2b97981
DW
5074 super_table[i]->disks->major,
5075 super_table[i]->disks->minor);
5076 super_table[i] = s;
5077 }
5078
5079 /* This is where the mdadm implementation differs from the Windows
5080 * driver which has no strict concept of a container. We can only
5081 * assemble one family from a container, so when returning a prodigal
5082 * array member to this system the code will not be able to disambiguate
5083 * the container contents that should be assembled ("foreign" versus
5084 * "local"). It requires user intervention to set the orig_family_num
5085 * to a new value to establish a new container. The Windows driver in
5086 * this situation fixes up the volume name in place and manages the
5087 * foreign array as an independent entity.
5088 */
5089 s = NULL;
5090 spare = NULL;
5091 conflict = 0;
5092 for (i = 0; i < tbl_size; i++) {
5093 struct intel_super *tbl_ent = super_table[i];
5094 int is_spare = 0;
5095
5096 if (!tbl_ent)
5097 continue;
5098
5099 if (tbl_ent->anchor->num_raid_devs == 0) {
5100 spare = tbl_ent;
5101 is_spare = 1;
5102 }
5103
5104 if (s && !is_spare) {
5105 show_conflicts(tbl_ent->anchor->family_num, *super_list);
5106 conflict++;
5107 } else if (!s && !is_spare)
5108 s = tbl_ent;
5109 }
5110
5111 if (!s)
5112 s = spare;
5113 if (!s) {
5114 champion = NULL;
5115 goto out;
5116 }
5117 champion = s;
5118
5119 if (conflict)
7a862a02 5120 pr_err("Chose family %#x on '%s', assemble conflicts to new container with '--update=uuid'\n",
a2b97981
DW
5121 __le32_to_cpu(s->anchor->family_num), s->disks->devname);
5122
5123 /* collect all dl's onto 'champion', and update them to
5124 * champion's version of the status
5125 */
5126 for (s = *super_list; s; s = s->next) {
5127 struct imsm_super *mpb = champion->anchor;
5128 struct dl *dl = s->disks;
5129
5130 if (s == champion)
5131 continue;
5132
5d7b407a
CA
5133 mpb->attributes |= s->anchor->attributes & MPB_ATTRIB_2TB_DISK;
5134
a2b97981
DW
5135 for (i = 0; i < mpb->num_disks; i++) {
5136 struct imsm_disk *disk;
5137
5138 disk = __serial_to_disk(dl->serial, mpb, &dl->index);
5139 if (disk) {
5140 dl->disk = *disk;
5141 /* only set index on disks that are a member of
5142 * a populated contianer, i.e. one with
5143 * raid_devs
5144 */
5145 if (is_failed(&dl->disk))
5146 dl->index = -2;
5147 else if (is_spare(&dl->disk))
5148 dl->index = -1;
5149 break;
5150 }
5151 }
5152
5153 if (i >= mpb->num_disks) {
5154 struct intel_disk *idisk;
5155
5156 idisk = disk_list_get(dl->serial, disk_list);
ecf408e9 5157 if (idisk && is_spare(&idisk->disk) &&
a2b97981
DW
5158 !is_failed(&idisk->disk) && !is_configured(&idisk->disk))
5159 dl->index = -1;
5160 else {
5161 dl->index = -2;
5162 continue;
5163 }
5164 }
5165
5166 dl->next = champion->disks;
5167 champion->disks = dl;
5168 s->disks = NULL;
5169 }
5170
5171 /* delete 'champion' from super_list */
5172 for (del = super_list; *del; ) {
5173 if (*del == champion) {
5174 *del = (*del)->next;
5175 break;
5176 } else
5177 del = &(*del)->next;
5178 }
5179 champion->next = NULL;
5180
5181 out:
5182 while (disk_list) {
5183 struct intel_disk *idisk = disk_list;
5184
5185 disk_list = disk_list->next;
5186 free(idisk);
5187 }
5188
5189 return champion;
5190}
5191
9587c373
LM
5192static int
5193get_sra_super_block(int fd, struct intel_super **super_list, char *devname, int *max, int keep_fd);
4dd2df09 5194static int get_super_block(struct intel_super **super_list, char *devnm, char *devname,
9587c373 5195 int major, int minor, int keep_fd);
ec50f7b6
LM
5196static int
5197get_devlist_super_block(struct md_list *devlist, struct intel_super **super_list,
5198 int *max, int keep_fd);
5199
cdddbdbc 5200static int load_super_imsm_all(struct supertype *st, int fd, void **sbp,
ec50f7b6
LM
5201 char *devname, struct md_list *devlist,
5202 int keep_fd)
cdddbdbc 5203{
a2b97981
DW
5204 struct intel_super *super_list = NULL;
5205 struct intel_super *super = NULL;
a2b97981 5206 int err = 0;
9587c373 5207 int i = 0;
dab4a513 5208
4389ce73 5209 if (is_fd_valid(fd))
9587c373
LM
5210 /* 'fd' is an opened container */
5211 err = get_sra_super_block(fd, &super_list, devname, &i, keep_fd);
5212 else
ec50f7b6
LM
5213 /* get super block from devlist devices */
5214 err = get_devlist_super_block(devlist, &super_list, &i, keep_fd);
9587c373 5215 if (err)
1602d52c 5216 goto error;
a2b97981
DW
5217 /* all mpbs enter, maybe one leaves */
5218 super = imsm_thunderdome(&super_list, i);
5219 if (!super) {
5220 err = 1;
5221 goto error;
cdddbdbc
DW
5222 }
5223
47ee5a45
DW
5224 if (find_missing(super) != 0) {
5225 free_imsm(super);
a2b97981
DW
5226 err = 2;
5227 goto error;
47ee5a45 5228 }
8e59f3d8
AK
5229
5230 /* load migration record */
2f86fda3 5231 err = load_imsm_migr_rec(super);
4c965cc9
AK
5232 if (err == -1) {
5233 /* migration is in progress,
5234 * but migr_rec cannot be loaded,
5235 */
8e59f3d8
AK
5236 err = 4;
5237 goto error;
5238 }
e2f41b2c
AK
5239
5240 /* Check migration compatibility */
089f9d79 5241 if (err == 0 && check_mpb_migr_compatibility(super) != 0) {
e7b84f9d 5242 pr_err("Unsupported migration detected");
e2f41b2c
AK
5243 if (devname)
5244 fprintf(stderr, " on %s\n", devname);
5245 else
5246 fprintf(stderr, " (IMSM).\n");
5247
5248 err = 5;
5249 goto error;
5250 }
5251
a2b97981
DW
5252 err = 0;
5253
5254 error:
5255 while (super_list) {
5256 struct intel_super *s = super_list;
5257
5258 super_list = super_list->next;
5259 free_imsm(s);
5260 }
9587c373 5261
a2b97981
DW
5262 if (err)
5263 return err;
f7e7067b 5264
cdddbdbc 5265 *sbp = super;
4389ce73 5266 if (is_fd_valid(fd))
4dd2df09 5267 strcpy(st->container_devnm, fd2devnm(fd));
9587c373 5268 else
4dd2df09 5269 st->container_devnm[0] = 0;
a2b97981 5270 if (err == 0 && st->ss == NULL) {
bf5a934a 5271 st->ss = &super_imsm;
cdddbdbc
DW
5272 st->minor_version = 0;
5273 st->max_devs = IMSM_MAX_DEVICES;
5274 }
cdddbdbc
DW
5275 return 0;
5276}
2b959fbf 5277
ec50f7b6
LM
5278static int
5279get_devlist_super_block(struct md_list *devlist, struct intel_super **super_list,
5280 int *max, int keep_fd)
5281{
5282 struct md_list *tmpdev;
5283 int err = 0;
5284 int i = 0;
9587c373 5285
ec50f7b6
LM
5286 for (i = 0, tmpdev = devlist; tmpdev; tmpdev = tmpdev->next) {
5287 if (tmpdev->used != 1)
5288 continue;
5289 if (tmpdev->container == 1) {
ca9de185 5290 int lmax = 0;
ec50f7b6 5291 int fd = dev_open(tmpdev->devname, O_RDONLY|O_EXCL);
4389ce73 5292 if (!is_fd_valid(fd)) {
e7b84f9d 5293 pr_err("cannot open device %s: %s\n",
ec50f7b6
LM
5294 tmpdev->devname, strerror(errno));
5295 err = 8;
5296 goto error;
5297 }
5298 err = get_sra_super_block(fd, super_list,
5299 tmpdev->devname, &lmax,
5300 keep_fd);
5301 i += lmax;
5302 close(fd);
5303 if (err) {
5304 err = 7;
5305 goto error;
5306 }
5307 } else {
5308 int major = major(tmpdev->st_rdev);
5309 int minor = minor(tmpdev->st_rdev);
5310 err = get_super_block(super_list,
4dd2df09 5311 NULL,
ec50f7b6
LM
5312 tmpdev->devname,
5313 major, minor,
5314 keep_fd);
5315 i++;
5316 if (err) {
5317 err = 6;
5318 goto error;
5319 }
5320 }
5321 }
5322 error:
5323 *max = i;
5324 return err;
5325}
9587c373 5326
4dd2df09 5327static int get_super_block(struct intel_super **super_list, char *devnm, char *devname,
9587c373
LM
5328 int major, int minor, int keep_fd)
5329{
594dc1b8 5330 struct intel_super *s;
9587c373
LM
5331 char nm[32];
5332 int dfd = -1;
9587c373
LM
5333 int err = 0;
5334 int retry;
5335
5336 s = alloc_super();
5337 if (!s) {
5338 err = 1;
5339 goto error;
5340 }
5341
5342 sprintf(nm, "%d:%d", major, minor);
5343 dfd = dev_open(nm, O_RDWR);
4389ce73 5344 if (!is_fd_valid(dfd)) {
9587c373
LM
5345 err = 2;
5346 goto error;
5347 }
5348
aec01630
JS
5349 if (!get_dev_sector_size(dfd, NULL, &s->sector_size)) {
5350 err = 2;
5351 goto error;
5352 }
cb8f6859 5353 find_intel_hba_capability(dfd, s, devname);
9587c373
LM
5354 err = load_and_parse_mpb(dfd, s, NULL, keep_fd);
5355
5356 /* retry the load if we might have raced against mdmon */
4dd2df09 5357 if (err == 3 && devnm && mdmon_running(devnm))
9587c373 5358 for (retry = 0; retry < 3; retry++) {
239b3cc0 5359 sleep_for(0, MSEC_TO_NSEC(3), true);
9587c373
LM
5360 err = load_and_parse_mpb(dfd, s, NULL, keep_fd);
5361 if (err != 3)
5362 break;
5363 }
5364 error:
5365 if (!err) {
5366 s->next = *super_list;
5367 *super_list = s;
5368 } else {
5369 if (s)
8d67477f 5370 free_imsm(s);
4389ce73 5371 close_fd(&dfd);
9587c373 5372 }
4389ce73
MT
5373 if (!keep_fd)
5374 close_fd(&dfd);
9587c373
LM
5375 return err;
5376
5377}
5378
5379static int
5380get_sra_super_block(int fd, struct intel_super **super_list, char *devname, int *max, int keep_fd)
5381{
5382 struct mdinfo *sra;
4dd2df09 5383 char *devnm;
9587c373
LM
5384 struct mdinfo *sd;
5385 int err = 0;
5386 int i = 0;
4dd2df09 5387 sra = sysfs_read(fd, NULL, GET_LEVEL|GET_VERSION|GET_DEVS|GET_STATE);
9587c373
LM
5388 if (!sra)
5389 return 1;
5390
5391 if (sra->array.major_version != -1 ||
5392 sra->array.minor_version != -2 ||
5393 strcmp(sra->text_version, "imsm") != 0) {
5394 err = 1;
5395 goto error;
5396 }
5397 /* load all mpbs */
4dd2df09 5398 devnm = fd2devnm(fd);
9587c373 5399 for (sd = sra->devs, i = 0; sd; sd = sd->next, i++) {
4dd2df09 5400 if (get_super_block(super_list, devnm, devname,
9587c373
LM
5401 sd->disk.major, sd->disk.minor, keep_fd) != 0) {
5402 err = 7;
5403 goto error;
5404 }
5405 }
5406 error:
5407 sysfs_free(sra);
5408 *max = i;
5409 return err;
5410}
5411
2b959fbf
N
5412static int load_container_imsm(struct supertype *st, int fd, char *devname)
5413{
ec50f7b6 5414 return load_super_imsm_all(st, fd, &st->sb, devname, NULL, 1);
2b959fbf 5415}
cdddbdbc
DW
5416
5417static int load_super_imsm(struct supertype *st, int fd, char *devname)
5418{
5419 struct intel_super *super;
5420 int rv;
8a3544f8 5421 int retry;
cdddbdbc 5422
357ac106 5423 if (test_partition(fd))
691c6ee1
N
5424 /* IMSM not allowed on partitions */
5425 return 1;
5426
37424f13
DW
5427 free_super_imsm(st);
5428
49133e57 5429 super = alloc_super();
8d67477f
TM
5430 if (!super)
5431 return 1;
3a85bf0e
MG
5432
5433 if (!get_dev_sector_size(fd, NULL, &super->sector_size)) {
5434 free_imsm(super);
5435 return 1;
5436 }
ea2bc72b
LM
5437 /* Load hba and capabilities if they exist.
5438 * But do not preclude loading metadata in case capabilities or hba are
5439 * non-compliant and ignore_hw_compat is set.
5440 */
d424212e 5441 rv = find_intel_hba_capability(fd, super, devname);
f2f5c343 5442 /* no orom/efi or non-intel hba of the disk */
089f9d79 5443 if (rv != 0 && st->ignore_hw_compat == 0) {
f2f5c343 5444 if (devname)
e7b84f9d 5445 pr_err("No OROM/EFI properties for %s\n", devname);
f2f5c343
LM
5446 free_imsm(super);
5447 return 2;
5448 }
a2b97981 5449 rv = load_and_parse_mpb(fd, super, devname, 0);
cdddbdbc 5450
8a3544f8
AP
5451 /* retry the load if we might have raced against mdmon */
5452 if (rv == 3) {
f96b1302
AP
5453 struct mdstat_ent *mdstat = NULL;
5454 char *name = fd2kname(fd);
5455
5456 if (name)
5457 mdstat = mdstat_by_component(name);
8a3544f8
AP
5458
5459 if (mdstat && mdmon_running(mdstat->devnm) && getpid() != mdmon_pid(mdstat->devnm)) {
5460 for (retry = 0; retry < 3; retry++) {
239b3cc0 5461 sleep_for(0, MSEC_TO_NSEC(3), true);
8a3544f8
AP
5462 rv = load_and_parse_mpb(fd, super, devname, 0);
5463 if (rv != 3)
5464 break;
5465 }
5466 }
5467
5468 free_mdstat(mdstat);
5469 }
5470
cdddbdbc
DW
5471 if (rv) {
5472 if (devname)
7a862a02 5473 pr_err("Failed to load all information sections on %s\n", devname);
cdddbdbc
DW
5474 free_imsm(super);
5475 return rv;
5476 }
5477
5478 st->sb = super;
5479 if (st->ss == NULL) {
5480 st->ss = &super_imsm;
5481 st->minor_version = 0;
5482 st->max_devs = IMSM_MAX_DEVICES;
5483 }
8e59f3d8
AK
5484
5485 /* load migration record */
2f86fda3 5486 if (load_imsm_migr_rec(super) == 0) {
2e062e82
AK
5487 /* Check for unsupported migration features */
5488 if (check_mpb_migr_compatibility(super) != 0) {
e7b84f9d 5489 pr_err("Unsupported migration detected");
2e062e82
AK
5490 if (devname)
5491 fprintf(stderr, " on %s\n", devname);
5492 else
5493 fprintf(stderr, " (IMSM).\n");
5494 return 3;
5495 }
e2f41b2c
AK
5496 }
5497
cdddbdbc
DW
5498 return 0;
5499}
5500
ef6ffade
DW
5501static __u16 info_to_blocks_per_strip(mdu_array_info_t *info)
5502{
5503 if (info->level == 1)
5504 return 128;
5505 return info->chunk_size >> 9;
5506}
5507
5551b113
CA
5508static unsigned long long info_to_blocks_per_member(mdu_array_info_t *info,
5509 unsigned long long size)
fcfd9599 5510{
4025c288 5511 if (info->level == 1)
5551b113 5512 return size * 2;
4025c288 5513 else
5551b113 5514 return (size * 2) & ~(info_to_blocks_per_strip(info) - 1);
fcfd9599
DW
5515}
5516
4d1313e9
DW
5517static void imsm_update_version_info(struct intel_super *super)
5518{
5519 /* update the version and attributes */
5520 struct imsm_super *mpb = super->anchor;
5521 char *version;
5522 struct imsm_dev *dev;
5523 struct imsm_map *map;
5524 int i;
5525
5526 for (i = 0; i < mpb->num_raid_devs; i++) {
5527 dev = get_imsm_dev(super, i);
238c0a71 5528 map = get_imsm_map(dev, MAP_0);
4d1313e9
DW
5529 if (__le32_to_cpu(dev->size_high) > 0)
5530 mpb->attributes |= MPB_ATTRIB_2TB;
5531
5532 /* FIXME detect when an array spans a port multiplier */
5533 #if 0
5534 mpb->attributes |= MPB_ATTRIB_PM;
5535 #endif
5536
5537 if (mpb->num_raid_devs > 1 ||
5538 mpb->attributes != MPB_ATTRIB_CHECKSUM_VERIFY) {
5539 version = MPB_VERSION_ATTRIBS;
5540 switch (get_imsm_raid_level(map)) {
5541 case 0: mpb->attributes |= MPB_ATTRIB_RAID0; break;
5542 case 1: mpb->attributes |= MPB_ATTRIB_RAID1; break;
5543 case 10: mpb->attributes |= MPB_ATTRIB_RAID10; break;
5544 case 5: mpb->attributes |= MPB_ATTRIB_RAID5; break;
5545 }
5546 } else {
5547 if (map->num_members >= 5)
5548 version = MPB_VERSION_5OR6_DISK_ARRAY;
5549 else if (dev->status == DEV_CLONE_N_GO)
5550 version = MPB_VERSION_CNG;
5551 else if (get_imsm_raid_level(map) == 5)
5552 version = MPB_VERSION_RAID5;
5553 else if (map->num_members >= 3)
5554 version = MPB_VERSION_3OR4_DISK_ARRAY;
5555 else if (get_imsm_raid_level(map) == 1)
5556 version = MPB_VERSION_RAID1;
5557 else
5558 version = MPB_VERSION_RAID0;
5559 }
5560 strcpy(((char *) mpb->sig) + strlen(MPB_SIGNATURE), version);
5561 }
5562}
5563
aa534678
DW
5564static int check_name(struct intel_super *super, char *name, int quiet)
5565{
5566 struct imsm_super *mpb = super->anchor;
5567 char *reason = NULL;
9bd99a90
RS
5568 char *start = name;
5569 size_t len = strlen(name);
aa534678
DW
5570 int i;
5571
9bd99a90
RS
5572 if (len > 0) {
5573 while (isspace(start[len - 1]))
5574 start[--len] = 0;
5575 while (*start && isspace(*start))
5576 ++start, --len;
5577 memmove(name, start, len + 1);
5578 }
5579
5580 if (len > MAX_RAID_SERIAL_LEN)
aa534678 5581 reason = "must be 16 characters or less";
9bd99a90
RS
5582 else if (len == 0)
5583 reason = "must be a non-empty string";
aa534678
DW
5584
5585 for (i = 0; i < mpb->num_raid_devs; i++) {
5586 struct imsm_dev *dev = get_imsm_dev(super, i);
5587
5588 if (strncmp((char *) dev->volume, name, MAX_RAID_SERIAL_LEN) == 0) {
5589 reason = "already exists";
5590 break;
5591 }
5592 }
5593
5594 if (reason && !quiet)
e7b84f9d 5595 pr_err("imsm volume name %s\n", reason);
aa534678
DW
5596
5597 return !reason;
5598}
5599
8b353278 5600static int init_super_imsm_volume(struct supertype *st, mdu_array_info_t *info,
5308f117 5601 struct shape *s, char *name,
83cd1e97
N
5602 char *homehost, int *uuid,
5603 long long data_offset)
cdddbdbc 5604{
c2c087e6
DW
5605 /* We are creating a volume inside a pre-existing container.
5606 * so st->sb is already set.
5607 */
5608 struct intel_super *super = st->sb;
f36a9ecd 5609 unsigned int sector_size = super->sector_size;
949c47a0 5610 struct imsm_super *mpb = super->anchor;
ba2de7ba 5611 struct intel_dev *dv;
c2c087e6
DW
5612 struct imsm_dev *dev;
5613 struct imsm_vol *vol;
5614 struct imsm_map *map;
5615 int idx = mpb->num_raid_devs;
5616 int i;
760365f9 5617 int namelen;
c2c087e6 5618 unsigned long long array_blocks;
2c092cad 5619 size_t size_old, size_new;
b53bfba6
TM
5620 unsigned int data_disks;
5621 unsigned long long size_per_member;
cdddbdbc 5622
88c32bb1 5623 if (super->orom && mpb->num_raid_devs >= super->orom->vpa) {
7a862a02 5624 pr_err("This imsm-container already has the maximum of %d volumes\n", super->orom->vpa);
c2c087e6
DW
5625 return 0;
5626 }
5627
2c092cad
DW
5628 /* ensure the mpb is large enough for the new data */
5629 size_old = __le32_to_cpu(mpb->mpb_size);
5630 size_new = disks_to_mpb_size(info->nr_disks);
5631 if (size_new > size_old) {
5632 void *mpb_new;
f36a9ecd 5633 size_t size_round = ROUND_UP(size_new, sector_size);
2c092cad 5634
f36a9ecd 5635 if (posix_memalign(&mpb_new, sector_size, size_round) != 0) {
e7b84f9d 5636 pr_err("could not allocate new mpb\n");
2c092cad
DW
5637 return 0;
5638 }
85337573
AO
5639 if (posix_memalign(&super->migr_rec_buf, MAX_SECTOR_SIZE,
5640 MIGR_REC_BUF_SECTORS*
5641 MAX_SECTOR_SIZE) != 0) {
1ade5cc1 5642 pr_err("could not allocate migr_rec buffer\n");
8e59f3d8
AK
5643 free(super->buf);
5644 free(super);
ea944c8f 5645 free(mpb_new);
8e59f3d8
AK
5646 return 0;
5647 }
2c092cad
DW
5648 memcpy(mpb_new, mpb, size_old);
5649 free(mpb);
5650 mpb = mpb_new;
949c47a0 5651 super->anchor = mpb_new;
2c092cad
DW
5652 mpb->mpb_size = __cpu_to_le32(size_new);
5653 memset(mpb_new + size_old, 0, size_round - size_old);
bbab0940 5654 super->len = size_round;
2c092cad 5655 }
bf5a934a 5656 super->current_vol = idx;
3960e579
DW
5657
5658 /* handle 'failed_disks' by either:
5659 * a) create dummy disk entries in the table if this the first
5660 * volume in the array. We add them here as this is the only
5661 * opportunity to add them. add_to_super_imsm_volume()
5662 * handles the non-failed disks and continues incrementing
5663 * mpb->num_disks.
5664 * b) validate that 'failed_disks' matches the current number
5665 * of missing disks if the container is populated
d23fe947 5666 */
3960e579 5667 if (super->current_vol == 0) {
d23fe947 5668 mpb->num_disks = 0;
3960e579
DW
5669 for (i = 0; i < info->failed_disks; i++) {
5670 struct imsm_disk *disk;
5671
5672 mpb->num_disks++;
5673 disk = __get_imsm_disk(mpb, i);
5674 disk->status = CONFIGURED_DISK | FAILED_DISK;
5675 disk->scsi_id = __cpu_to_le32(~(__u32)0);
5676 snprintf((char *) disk->serial, MAX_RAID_SERIAL_LEN,
5b975129 5677 "missing:%d", (__u8)i);
3960e579
DW
5678 }
5679 find_missing(super);
5680 } else {
5681 int missing = 0;
5682 struct dl *d;
5683
5684 for (d = super->missing; d; d = d->next)
5685 missing++;
5686 if (info->failed_disks > missing) {
e7b84f9d 5687 pr_err("unable to add 'missing' disk to container\n");
3960e579
DW
5688 return 0;
5689 }
5690 }
5a038140 5691
aa534678
DW
5692 if (!check_name(super, name, 0))
5693 return 0;
503975b9
N
5694 dv = xmalloc(sizeof(*dv));
5695 dev = xcalloc(1, sizeof(*dev) + sizeof(__u32) * (info->raid_disks - 1));
760365f9
JS
5696 /*
5697 * Explicitly allow truncating to not confuse gcc's
5698 * -Werror=stringop-truncation
5699 */
5700 namelen = min((int) strlen(name), MAX_RAID_SERIAL_LEN);
5701 memcpy(dev->volume, name, namelen);
e03640bd 5702 array_blocks = calc_array_size(info->level, info->raid_disks,
03bcbc65 5703 info->layout, info->chunk_size,
b53bfba6
TM
5704 s->size * BLOCKS_PER_KB);
5705 data_disks = get_data_disks(info->level, info->layout,
5706 info->raid_disks);
5707 array_blocks = round_size_to_mb(array_blocks, data_disks);
5708 size_per_member = array_blocks / data_disks;
979d38be 5709
fcc2c9da 5710 set_imsm_dev_size(dev, array_blocks);
1a2487c2 5711 dev->status = (DEV_READ_COALESCING | DEV_WRITE_COALESCING);
c2c087e6
DW
5712 vol = &dev->vol;
5713 vol->migr_state = 0;
1484e727 5714 set_migr_type(dev, MIGR_INIT);
3960e579 5715 vol->dirty = !info->state;
4036e7ee 5716 set_vol_curr_migr_unit(dev, 0);
238c0a71 5717 map = get_imsm_map(dev, MAP_0);
5551b113 5718 set_pba_of_lba0(map, super->create_offset);
ef6ffade 5719 map->blocks_per_strip = __cpu_to_le16(info_to_blocks_per_strip(info));
0556e1a2 5720 map->failed_disk_num = ~0;
bf4442ab 5721 if (info->level > 0)
fffaf1ff
N
5722 map->map_state = (info->state ? IMSM_T_STATE_NORMAL
5723 : IMSM_T_STATE_UNINITIALIZED);
bf4442ab
AK
5724 else
5725 map->map_state = info->failed_disks ? IMSM_T_STATE_FAILED :
5726 IMSM_T_STATE_NORMAL;
252d23c0 5727 map->ddf = 1;
ef6ffade
DW
5728
5729 if (info->level == 1 && info->raid_disks > 2) {
38950822
AW
5730 free(dev);
5731 free(dv);
7a862a02 5732 pr_err("imsm does not support more than 2 disksin a raid1 volume\n");
ef6ffade
DW
5733 return 0;
5734 }
81062a36
DW
5735
5736 map->raid_level = info->level;
1c275381 5737 if (info->level == 10)
c2c087e6 5738 map->raid_level = 1;
1c275381 5739 set_num_domains(map);
ef6ffade 5740
44490938
MD
5741 size_per_member += NUM_BLOCKS_DIRTY_STRIPE_REGION;
5742 set_blocks_per_member(map, info_to_blocks_per_member(info,
5743 size_per_member /
5744 BLOCKS_PER_KB));
5745
c2c087e6 5746 map->num_members = info->raid_disks;
1c275381 5747 update_num_data_stripes(map, array_blocks);
c2c087e6
DW
5748 for (i = 0; i < map->num_members; i++) {
5749 /* initialized in add_to_super */
4eb26970 5750 set_imsm_ord_tbl_ent(map, i, IMSM_ORD_REBUILD);
c2c087e6 5751 }
949c47a0 5752 mpb->num_raid_devs++;
2a24dc1b
PB
5753 mpb->num_raid_devs_created++;
5754 dev->my_vol_raid_dev_num = mpb->num_raid_devs_created;
ba2de7ba 5755
b7580566 5756 if (s->consistency_policy <= CONSISTENCY_POLICY_RESYNC) {
c2462068 5757 dev->rwh_policy = RWH_MULTIPLE_OFF;
2432ce9b 5758 } else if (s->consistency_policy == CONSISTENCY_POLICY_PPL) {
c2462068 5759 dev->rwh_policy = RWH_MULTIPLE_DISTRIBUTED;
2432ce9b
AP
5760 } else {
5761 free(dev);
5762 free(dv);
5763 pr_err("imsm does not support consistency policy %s\n",
5f21d674 5764 map_num_s(consistency_policies, s->consistency_policy));
2432ce9b
AP
5765 return 0;
5766 }
5767
ba2de7ba
DW
5768 dv->dev = dev;
5769 dv->index = super->current_vol;
5770 dv->next = super->devlist;
5771 super->devlist = dv;
c2c087e6 5772
4d1313e9
DW
5773 imsm_update_version_info(super);
5774
c2c087e6 5775 return 1;
cdddbdbc
DW
5776}
5777
bf5a934a 5778static int init_super_imsm(struct supertype *st, mdu_array_info_t *info,
5308f117 5779 struct shape *s, char *name,
83cd1e97
N
5780 char *homehost, int *uuid,
5781 unsigned long long data_offset)
bf5a934a
DW
5782{
5783 /* This is primarily called by Create when creating a new array.
5784 * We will then get add_to_super called for each component, and then
5785 * write_init_super called to write it out to each device.
5786 * For IMSM, Create can create on fresh devices or on a pre-existing
5787 * array.
5788 * To create on a pre-existing array a different method will be called.
5789 * This one is just for fresh drives.
5790 */
5791 struct intel_super *super;
5792 struct imsm_super *mpb;
5793 size_t mpb_size;
4d1313e9 5794 char *version;
bf5a934a 5795
83cd1e97 5796 if (data_offset != INVALID_SECTORS) {
ed503f89 5797 pr_err("data-offset not supported by imsm\n");
83cd1e97
N
5798 return 0;
5799 }
5800
bf5a934a 5801 if (st->sb)
5308f117 5802 return init_super_imsm_volume(st, info, s, name, homehost, uuid,
83cd1e97 5803 data_offset);
e683ca88
DW
5804
5805 if (info)
5806 mpb_size = disks_to_mpb_size(info->nr_disks);
5807 else
f36a9ecd 5808 mpb_size = MAX_SECTOR_SIZE;
bf5a934a 5809
49133e57 5810 super = alloc_super();
f36a9ecd
PB
5811 if (super &&
5812 posix_memalign(&super->buf, MAX_SECTOR_SIZE, mpb_size) != 0) {
8d67477f 5813 free_imsm(super);
e683ca88
DW
5814 super = NULL;
5815 }
5816 if (!super) {
1ade5cc1 5817 pr_err("could not allocate superblock\n");
bf5a934a
DW
5818 return 0;
5819 }
de44e46f
PB
5820 if (posix_memalign(&super->migr_rec_buf, MAX_SECTOR_SIZE,
5821 MIGR_REC_BUF_SECTORS*MAX_SECTOR_SIZE) != 0) {
1ade5cc1 5822 pr_err("could not allocate migr_rec buffer\n");
8e59f3d8 5823 free(super->buf);
8d67477f 5824 free_imsm(super);
8e59f3d8
AK
5825 return 0;
5826 }
e683ca88 5827 memset(super->buf, 0, mpb_size);
ef649044 5828 mpb = super->buf;
e683ca88
DW
5829 mpb->mpb_size = __cpu_to_le32(mpb_size);
5830 st->sb = super;
5831
5832 if (info == NULL) {
5833 /* zeroing superblock */
5834 return 0;
5835 }
bf5a934a 5836
4d1313e9
DW
5837 mpb->attributes = MPB_ATTRIB_CHECKSUM_VERIFY;
5838
5839 version = (char *) mpb->sig;
5840 strcpy(version, MPB_SIGNATURE);
5841 version += strlen(MPB_SIGNATURE);
5842 strcpy(version, MPB_VERSION_RAID0);
bf5a934a 5843
bf5a934a
DW
5844 return 1;
5845}
5846
f2cc4f7d
AO
5847static int drive_validate_sector_size(struct intel_super *super, struct dl *dl)
5848{
5849 unsigned int member_sector_size;
5850
4389ce73 5851 if (!is_fd_valid(dl->fd)) {
f2cc4f7d
AO
5852 pr_err("Invalid file descriptor for %s\n", dl->devname);
5853 return 0;
5854 }
5855
5856 if (!get_dev_sector_size(dl->fd, dl->devname, &member_sector_size))
5857 return 0;
5858 if (member_sector_size != super->sector_size)
5859 return 0;
5860 return 1;
5861}
5862
f20c3968 5863static int add_to_super_imsm_volume(struct supertype *st, mdu_disk_info_t *dk,
bf5a934a
DW
5864 int fd, char *devname)
5865{
5866 struct intel_super *super = st->sb;
d23fe947 5867 struct imsm_super *mpb = super->anchor;
3960e579 5868 struct imsm_disk *_disk;
bf5a934a
DW
5869 struct imsm_dev *dev;
5870 struct imsm_map *map;
3960e579 5871 struct dl *dl, *df;
4eb26970 5872 int slot;
9a7df595
MT
5873 int autolayout = 0;
5874
5875 if (!is_fd_valid(fd))
5876 autolayout = 1;
bf5a934a 5877
949c47a0 5878 dev = get_imsm_dev(super, super->current_vol);
238c0a71 5879 map = get_imsm_map(dev, MAP_0);
bf5a934a 5880
208933a7 5881 if (! (dk->state & (1<<MD_DISK_SYNC))) {
e7b84f9d 5882 pr_err("%s: Cannot add spare devices to IMSM volume\n",
208933a7
N
5883 devname);
5884 return 1;
5885 }
5886
9a7df595
MT
5887 for (dl = super->disks; dl ; dl = dl->next) {
5888 if (autolayout) {
efb30e7f
DW
5889 if (dl->raiddisk == dk->raid_disk)
5890 break;
9a7df595
MT
5891 } else if (dl->major == dk->major && dl->minor == dk->minor)
5892 break;
efb30e7f 5893 }
d23fe947 5894
208933a7 5895 if (!dl) {
9a7df595
MT
5896 if (!autolayout)
5897 pr_err("%s is not a member of the same container.\n",
5898 devname);
f20c3968 5899 return 1;
208933a7 5900 }
bf5a934a 5901
9a7df595
MT
5902 if (!autolayout && super->current_vol > 0) {
5903 int _slot = get_disk_slot_in_dev(super, 0, dl->index);
5904
5905 if (_slot != dk->raid_disk) {
5906 pr_err("Member %s is in %d slot for the first volume, but is in %d slot for a new volume.\n",
5907 dl->devname, _slot, dk->raid_disk);
5908 pr_err("Raid members are in different order than for the first volume, aborting.\n");
5909 return 1;
5910 }
5911 }
5912
59632db9
MZ
5913 if (mpb->num_disks == 0)
5914 if (!get_dev_sector_size(dl->fd, dl->devname,
5915 &super->sector_size))
5916 return 1;
5917
f2cc4f7d
AO
5918 if (!drive_validate_sector_size(super, dl)) {
5919 pr_err("Combining drives of different sector size in one volume is not allowed\n");
5920 return 1;
5921 }
5922
d23fe947
DW
5923 /* add a pristine spare to the metadata */
5924 if (dl->index < 0) {
5925 dl->index = super->anchor->num_disks;
5926 super->anchor->num_disks++;
5927 }
4eb26970
DW
5928 /* Check the device has not already been added */
5929 slot = get_imsm_disk_slot(map, dl->index);
5930 if (slot >= 0 &&
238c0a71 5931 (get_imsm_ord_tbl_ent(dev, slot, MAP_X) & IMSM_ORD_REBUILD) == 0) {
e7b84f9d 5932 pr_err("%s has been included in this array twice\n",
4eb26970
DW
5933 devname);
5934 return 1;
5935 }
656b6b5a 5936 set_imsm_ord_tbl_ent(map, dk->raid_disk, dl->index);
ee5aad5a 5937 dl->disk.status = CONFIGURED_DISK;
d23fe947 5938
3960e579
DW
5939 /* update size of 'missing' disks to be at least as large as the
5940 * largest acitve member (we only have dummy missing disks when
5941 * creating the first volume)
5942 */
5943 if (super->current_vol == 0) {
5944 for (df = super->missing; df; df = df->next) {
5551b113
CA
5945 if (total_blocks(&dl->disk) > total_blocks(&df->disk))
5946 set_total_blocks(&df->disk, total_blocks(&dl->disk));
3960e579
DW
5947 _disk = __get_imsm_disk(mpb, df->index);
5948 *_disk = df->disk;
5949 }
5950 }
5951
5952 /* refresh unset/failed slots to point to valid 'missing' entries */
5953 for (df = super->missing; df; df = df->next)
5954 for (slot = 0; slot < mpb->num_disks; slot++) {
238c0a71 5955 __u32 ord = get_imsm_ord_tbl_ent(dev, slot, MAP_X);
3960e579
DW
5956
5957 if ((ord & IMSM_ORD_REBUILD) == 0)
5958 continue;
5959 set_imsm_ord_tbl_ent(map, slot, df->index | IMSM_ORD_REBUILD);
1ace8403 5960 if (is_gen_migration(dev)) {
238c0a71
AK
5961 struct imsm_map *map2 = get_imsm_map(dev,
5962 MAP_1);
0a108d63 5963 int slot2 = get_imsm_disk_slot(map2, df->index);
089f9d79 5964 if (slot2 < map2->num_members && slot2 >= 0) {
1ace8403 5965 __u32 ord2 = get_imsm_ord_tbl_ent(dev,
238c0a71
AK
5966 slot2,
5967 MAP_1);
1ace8403
AK
5968 if ((unsigned)df->index ==
5969 ord_to_idx(ord2))
5970 set_imsm_ord_tbl_ent(map2,
0a108d63 5971 slot2,
1ace8403
AK
5972 df->index |
5973 IMSM_ORD_REBUILD);
5974 }
5975 }
3960e579
DW
5976 dprintf("set slot:%d to missing disk:%d\n", slot, df->index);
5977 break;
5978 }
5979
d23fe947
DW
5980 /* if we are creating the first raid device update the family number */
5981 if (super->current_vol == 0) {
5982 __u32 sum;
5983 struct imsm_dev *_dev = __get_imsm_dev(mpb, 0);
d23fe947 5984
3960e579 5985 _disk = __get_imsm_disk(mpb, dl->index);
756a15f3 5986 if (!_disk) {
e7b84f9d 5987 pr_err("BUG mpb setup error\n");
791b666a
AW
5988 return 1;
5989 }
d23fe947
DW
5990 *_dev = *dev;
5991 *_disk = dl->disk;
148acb7b
DW
5992 sum = random32();
5993 sum += __gen_imsm_checksum(mpb);
d23fe947 5994 mpb->family_num = __cpu_to_le32(sum);
148acb7b 5995 mpb->orig_family_num = mpb->family_num;
e48aed3c 5996 mpb->creation_time = __cpu_to_le64((__u64)time(NULL));
d23fe947 5997 }
ca0748fa 5998 super->current_disk = dl;
f20c3968 5999 return 0;
bf5a934a
DW
6000}
6001
a8619d23
AK
6002/* mark_spare()
6003 * Function marks disk as spare and restores disk serial
6004 * in case it was previously marked as failed by takeover operation
6005 * reruns:
6006 * -1 : critical error
6007 * 0 : disk is marked as spare but serial is not set
6008 * 1 : success
6009 */
6010int mark_spare(struct dl *disk)
6011{
6012 __u8 serial[MAX_RAID_SERIAL_LEN];
6013 int ret_val = -1;
6014
6015 if (!disk)
6016 return ret_val;
6017
6018 ret_val = 0;
6da53c0e 6019 if (!imsm_read_serial(disk->fd, NULL, serial, MAX_RAID_SERIAL_LEN)) {
a8619d23
AK
6020 /* Restore disk serial number, because takeover marks disk
6021 * as failed and adds to serial ':0' before it becomes
6022 * a spare disk.
6023 */
6024 serialcpy(disk->serial, serial);
6025 serialcpy(disk->disk.serial, serial);
6026 ret_val = 1;
6027 }
6028 disk->disk.status = SPARE_DISK;
6029 disk->index = -1;
6030
6031 return ret_val;
6032}
88654014 6033
12724c01
TM
6034
6035static int write_super_imsm_spare(struct intel_super *super, struct dl *d);
6036
f20c3968 6037static int add_to_super_imsm(struct supertype *st, mdu_disk_info_t *dk,
72ca9bcf
N
6038 int fd, char *devname,
6039 unsigned long long data_offset)
cdddbdbc 6040{
c2c087e6 6041 struct intel_super *super = st->sb;
c2c087e6
DW
6042 struct dl *dd;
6043 unsigned long long size;
fa7bb6f8 6044 unsigned int member_sector_size;
f2f27e63 6045 __u32 id;
c2c087e6
DW
6046 int rv;
6047 struct stat stb;
6048
88654014
LM
6049 /* If we are on an RAID enabled platform check that the disk is
6050 * attached to the raid controller.
6051 * We do not need to test disks attachment for container based additions,
6052 * they shall be already tested when container was created/assembled.
88c32bb1 6053 */
d424212e 6054 rv = find_intel_hba_capability(fd, super, devname);
f2f5c343 6055 /* no orom/efi or non-intel hba of the disk */
f0f5a016
LM
6056 if (rv != 0) {
6057 dprintf("capability: %p fd: %d ret: %d\n",
6058 super->orom, fd, rv);
6059 return 1;
88c32bb1
DW
6060 }
6061
f20c3968
DW
6062 if (super->current_vol >= 0)
6063 return add_to_super_imsm_volume(st, dk, fd, devname);
bf5a934a 6064
c2c087e6 6065 fstat(fd, &stb);
503975b9 6066 dd = xcalloc(sizeof(*dd), 1);
c2c087e6
DW
6067 dd->major = major(stb.st_rdev);
6068 dd->minor = minor(stb.st_rdev);
503975b9 6069 dd->devname = devname ? xstrdup(devname) : NULL;
c2c087e6 6070 dd->fd = fd;
689c9bf3 6071 dd->e = NULL;
1a64be56 6072 dd->action = DISK_ADD;
6da53c0e 6073 rv = imsm_read_serial(fd, devname, dd->serial, MAX_RAID_SERIAL_LEN);
32ba9157 6074 if (rv) {
e7b84f9d 6075 pr_err("failed to retrieve scsi serial, aborting\n");
3a85bf0e 6076 __free_imsm_disk(dd, 0);
0030e8d6 6077 abort();
c2c087e6 6078 }
7c798f87 6079
20bee0f8
PB
6080 if (super->hba && ((super->hba->type == SYS_DEV_NVME) ||
6081 (super->hba->type == SYS_DEV_VMD))) {
6082 int i;
7c798f87
MT
6083 char cntrl_path[PATH_MAX];
6084 char *cntrl_name;
6085 char pci_dev_path[PATH_MAX];
20bee0f8 6086
7c798f87
MT
6087 if (!diskfd_to_devpath(fd, 2, pci_dev_path) ||
6088 !diskfd_to_devpath(fd, 1, cntrl_path)) {
8662f92d 6089 pr_err("failed to get dev paths, aborting\n");
3a85bf0e 6090 __free_imsm_disk(dd, 0);
a8f3cfd5
MT
6091 return 1;
6092 }
6093
7c798f87
MT
6094 cntrl_name = basename(cntrl_path);
6095 if (is_multipath_nvme(fd))
6096 pr_err("%s controller supports Multi-Path I/O, Intel (R) VROC does not support multipathing\n",
6097 cntrl_name);
6098
6099 if (devpath_to_vendor(pci_dev_path) == 0x8086) {
20bee0f8
PB
6100 /*
6101 * If Intel's NVMe drive has serial ended with
6102 * "-A","-B","-1" or "-2" it means that this is "x8"
6103 * device (double drive on single PCIe card).
6104 * User should be warned about potential data loss.
6105 */
6106 for (i = MAX_RAID_SERIAL_LEN-1; i > 0; i--) {
6107 /* Skip empty character at the end */
6108 if (dd->serial[i] == 0)
6109 continue;
6110
6111 if (((dd->serial[i] == 'A') ||
6112 (dd->serial[i] == 'B') ||
6113 (dd->serial[i] == '1') ||
6114 (dd->serial[i] == '2')) &&
6115 (dd->serial[i-1] == '-'))
6116 pr_err("\tThe action you are about to take may put your data at risk.\n"
6117 "\tPlease note that x8 devices may consist of two separate x4 devices "
6118 "located on a single PCIe port.\n"
6119 "\tRAID 0 is the only supported configuration for this type of x8 device.\n");
6120 break;
6121 }
32716c51
PB
6122 } else if (super->hba->type == SYS_DEV_VMD && super->orom &&
6123 !imsm_orom_has_tpv_support(super->orom)) {
6124 pr_err("\tPlatform configuration does not support non-Intel NVMe drives.\n"
8b751247 6125 "\tPlease refer to Intel(R) RSTe/VROC user guide.\n");
3a85bf0e 6126 __free_imsm_disk(dd, 0);
32716c51 6127 return 1;
20bee0f8
PB
6128 }
6129 }
c2c087e6 6130
c2c087e6 6131 get_dev_size(fd, NULL, &size);
3a85bf0e
MG
6132 if (!get_dev_sector_size(fd, NULL, &member_sector_size)) {
6133 __free_imsm_disk(dd, 0);
aec01630 6134 return 1;
3a85bf0e 6135 }
fa7bb6f8
PB
6136
6137 if (super->sector_size == 0) {
6138 /* this a first device, so sector_size is not set yet */
6139 super->sector_size = member_sector_size;
fa7bb6f8
PB
6140 }
6141
71e5411e 6142 /* clear migr_rec when adding disk to container */
85337573
AO
6143 memset(super->migr_rec_buf, 0, MIGR_REC_BUF_SECTORS*MAX_SECTOR_SIZE);
6144 if (lseek64(fd, size - MIGR_REC_SECTOR_POSITION*member_sector_size,
de44e46f 6145 SEEK_SET) >= 0) {
466070ad 6146 if ((unsigned int)write(fd, super->migr_rec_buf,
85337573
AO
6147 MIGR_REC_BUF_SECTORS*member_sector_size) !=
6148 MIGR_REC_BUF_SECTORS*member_sector_size)
71e5411e
PB
6149 perror("Write migr_rec failed");
6150 }
6151
c2c087e6 6152 size /= 512;
1f24f035 6153 serialcpy(dd->disk.serial, dd->serial);
5551b113
CA
6154 set_total_blocks(&dd->disk, size);
6155 if (__le32_to_cpu(dd->disk.total_blocks_hi) > 0) {
6156 struct imsm_super *mpb = super->anchor;
6157 mpb->attributes |= MPB_ATTRIB_2TB_DISK;
6158 }
a8619d23 6159 mark_spare(dd);
c2c087e6 6160 if (sysfs_disk_to_scsi_id(fd, &id) == 0)
b9f594fe 6161 dd->disk.scsi_id = __cpu_to_le32(id);
c2c087e6 6162 else
b9f594fe 6163 dd->disk.scsi_id = __cpu_to_le32(0);
43dad3d6
DW
6164
6165 if (st->update_tail) {
1a64be56
LM
6166 dd->next = super->disk_mgmt_list;
6167 super->disk_mgmt_list = dd;
43dad3d6 6168 } else {
12724c01
TM
6169 /* this is called outside of mdmon
6170 * write initial spare metadata
6171 * mdmon will overwrite it.
6172 */
43dad3d6
DW
6173 dd->next = super->disks;
6174 super->disks = dd;
12724c01 6175 write_super_imsm_spare(super, dd);
43dad3d6 6176 }
f20c3968
DW
6177
6178 return 0;
cdddbdbc
DW
6179}
6180
1a64be56
LM
6181static int remove_from_super_imsm(struct supertype *st, mdu_disk_info_t *dk)
6182{
6183 struct intel_super *super = st->sb;
6184 struct dl *dd;
6185
6186 /* remove from super works only in mdmon - for communication
6187 * manager - monitor. Check if communication memory buffer
6188 * is prepared.
6189 */
6190 if (!st->update_tail) {
1ade5cc1 6191 pr_err("shall be used in mdmon context only\n");
1a64be56
LM
6192 return 1;
6193 }
503975b9 6194 dd = xcalloc(1, sizeof(*dd));
1a64be56
LM
6195 dd->major = dk->major;
6196 dd->minor = dk->minor;
1a64be56 6197 dd->fd = -1;
a8619d23 6198 mark_spare(dd);
1a64be56
LM
6199 dd->action = DISK_REMOVE;
6200
6201 dd->next = super->disk_mgmt_list;
6202 super->disk_mgmt_list = dd;
6203
1a64be56
LM
6204 return 0;
6205}
6206
f796af5d
DW
6207static int store_imsm_mpb(int fd, struct imsm_super *mpb);
6208
6209static union {
f36a9ecd 6210 char buf[MAX_SECTOR_SIZE];
f796af5d 6211 struct imsm_super anchor;
f36a9ecd 6212} spare_record __attribute__ ((aligned(MAX_SECTOR_SIZE)));
c2c087e6 6213
12724c01
TM
6214
6215static int write_super_imsm_spare(struct intel_super *super, struct dl *d)
d23fe947 6216{
d23fe947 6217 struct imsm_super *mpb = super->anchor;
f796af5d 6218 struct imsm_super *spare = &spare_record.anchor;
d23fe947 6219 __u32 sum;
12724c01
TM
6220
6221 if (d->index != -1)
6222 return 1;
d23fe947 6223
68641cdb
JS
6224 spare->mpb_size = __cpu_to_le32(sizeof(struct imsm_super));
6225 spare->generation_num = __cpu_to_le32(1UL);
f796af5d 6226 spare->attributes = MPB_ATTRIB_CHECKSUM_VERIFY;
68641cdb
JS
6227 spare->num_disks = 1;
6228 spare->num_raid_devs = 0;
6229 spare->cache_size = mpb->cache_size;
6230 spare->pwr_cycle_count = __cpu_to_le32(1);
f796af5d
DW
6231
6232 snprintf((char *) spare->sig, MAX_SIGNATURE_LENGTH,
6233 MPB_SIGNATURE MPB_VERSION_RAID0);
d23fe947 6234
12724c01
TM
6235 spare->disk[0] = d->disk;
6236 if (__le32_to_cpu(d->disk.total_blocks_hi) > 0)
6237 spare->attributes |= MPB_ATTRIB_2TB_DISK;
6238
6239 if (super->sector_size == 4096)
6240 convert_to_4k_imsm_disk(&spare->disk[0]);
d23fe947 6241
12724c01
TM
6242 sum = __gen_imsm_checksum(spare);
6243 spare->family_num = __cpu_to_le32(sum);
6244 spare->orig_family_num = 0;
6245 sum = __gen_imsm_checksum(spare);
6246 spare->check_sum = __cpu_to_le32(sum);
027c374f 6247
12724c01
TM
6248 if (store_imsm_mpb(d->fd, spare)) {
6249 pr_err("failed for device %d:%d %s\n",
6250 d->major, d->minor, strerror(errno));
6251 return 1;
6252 }
6253
6254 return 0;
6255}
6256/* spare records have their own family number and do not have any defined raid
6257 * devices
6258 */
6259static int write_super_imsm_spares(struct intel_super *super, int doclose)
6260{
6261 struct dl *d;
f36a9ecd 6262
12724c01
TM
6263 for (d = super->disks; d; d = d->next) {
6264 if (d->index != -1)
6265 continue;
d23fe947 6266
12724c01 6267 if (write_super_imsm_spare(super, d))
e74255d9 6268 return 1;
12724c01 6269
4389ce73
MT
6270 if (doclose)
6271 close_fd(&d->fd);
d23fe947
DW
6272 }
6273
e74255d9 6274 return 0;
d23fe947
DW
6275}
6276
36988a3d 6277static int write_super_imsm(struct supertype *st, int doclose)
cdddbdbc 6278{
36988a3d 6279 struct intel_super *super = st->sb;
f36a9ecd 6280 unsigned int sector_size = super->sector_size;
949c47a0 6281 struct imsm_super *mpb = super->anchor;
c2c087e6
DW
6282 struct dl *d;
6283 __u32 generation;
6284 __u32 sum;
d23fe947 6285 int spares = 0;
949c47a0 6286 int i;
a48ac0a8 6287 __u32 mpb_size = sizeof(struct imsm_super) - sizeof(struct imsm_disk);
36988a3d 6288 int num_disks = 0;
146c6260 6289 int clear_migration_record = 1;
bbab0940 6290 __u32 bbm_log_size;
cdddbdbc 6291
c2c087e6
DW
6292 /* 'generation' is incremented everytime the metadata is written */
6293 generation = __le32_to_cpu(mpb->generation_num);
6294 generation++;
6295 mpb->generation_num = __cpu_to_le32(generation);
6296
148acb7b
DW
6297 /* fix up cases where previous mdadm releases failed to set
6298 * orig_family_num
6299 */
6300 if (mpb->orig_family_num == 0)
6301 mpb->orig_family_num = mpb->family_num;
6302
d23fe947 6303 for (d = super->disks; d; d = d->next) {
8796fdc4 6304 if (d->index == -1)
d23fe947 6305 spares++;
36988a3d 6306 else {
d23fe947 6307 mpb->disk[d->index] = d->disk;
36988a3d
AK
6308 num_disks++;
6309 }
d23fe947 6310 }
36988a3d 6311 for (d = super->missing; d; d = d->next) {
47ee5a45 6312 mpb->disk[d->index] = d->disk;
36988a3d
AK
6313 num_disks++;
6314 }
6315 mpb->num_disks = num_disks;
6316 mpb_size += sizeof(struct imsm_disk) * mpb->num_disks;
b9f594fe 6317
949c47a0
DW
6318 for (i = 0; i < mpb->num_raid_devs; i++) {
6319 struct imsm_dev *dev = __get_imsm_dev(mpb, i);
36988a3d 6320 struct imsm_dev *dev2 = get_imsm_dev(super, i);
756a15f3
MG
6321
6322 imsm_copy_dev(dev, dev2);
6323 mpb_size += sizeof_imsm_dev(dev, 0);
6324
146c6260
AK
6325 if (is_gen_migration(dev2))
6326 clear_migration_record = 0;
949c47a0 6327 }
bbab0940
TM
6328
6329 bbm_log_size = get_imsm_bbm_log_size(super->bbm_log);
6330
6331 if (bbm_log_size) {
6332 memcpy((void *)mpb + mpb_size, super->bbm_log, bbm_log_size);
6333 mpb->attributes |= MPB_ATTRIB_BBM;
6334 } else
6335 mpb->attributes &= ~MPB_ATTRIB_BBM;
6336
6337 super->anchor->bbm_log_size = __cpu_to_le32(bbm_log_size);
6338 mpb_size += bbm_log_size;
a48ac0a8 6339 mpb->mpb_size = __cpu_to_le32(mpb_size);
949c47a0 6340
bbab0940
TM
6341#ifdef DEBUG
6342 assert(super->len == 0 || mpb_size <= super->len);
6343#endif
6344
c2c087e6 6345 /* recalculate checksum */
949c47a0 6346 sum = __gen_imsm_checksum(mpb);
c2c087e6
DW
6347 mpb->check_sum = __cpu_to_le32(sum);
6348
51d83f5d
AK
6349 if (super->clean_migration_record_by_mdmon) {
6350 clear_migration_record = 1;
6351 super->clean_migration_record_by_mdmon = 0;
6352 }
146c6260 6353 if (clear_migration_record)
de44e46f 6354 memset(super->migr_rec_buf, 0,
85337573 6355 MIGR_REC_BUF_SECTORS*MAX_SECTOR_SIZE);
146c6260 6356
f36a9ecd
PB
6357 if (sector_size == 4096)
6358 convert_to_4k(super);
6359
d23fe947 6360 /* write the mpb for disks that compose raid devices */
c2c087e6 6361 for (d = super->disks; d ; d = d->next) {
86c54047 6362 if (d->index < 0 || is_failed(&d->disk))
d23fe947 6363 continue;
30602f53 6364
146c6260
AK
6365 if (clear_migration_record) {
6366 unsigned long long dsize;
6367
6368 get_dev_size(d->fd, NULL, &dsize);
de44e46f
PB
6369 if (lseek64(d->fd, dsize - sector_size,
6370 SEEK_SET) >= 0) {
466070ad
PB
6371 if ((unsigned int)write(d->fd,
6372 super->migr_rec_buf,
de44e46f
PB
6373 MIGR_REC_BUF_SECTORS*sector_size) !=
6374 MIGR_REC_BUF_SECTORS*sector_size)
9e2d750d 6375 perror("Write migr_rec failed");
146c6260
AK
6376 }
6377 }
51d83f5d
AK
6378
6379 if (store_imsm_mpb(d->fd, mpb))
6380 fprintf(stderr,
1ade5cc1
N
6381 "failed for device %d:%d (fd: %d)%s\n",
6382 d->major, d->minor,
51d83f5d
AK
6383 d->fd, strerror(errno));
6384
4389ce73
MT
6385 if (doclose)
6386 close_fd(&d->fd);
c2c087e6
DW
6387 }
6388
d23fe947
DW
6389 if (spares)
6390 return write_super_imsm_spares(super, doclose);
6391
e74255d9 6392 return 0;
c2c087e6
DW
6393}
6394
9b1fb677 6395static int create_array(struct supertype *st, int dev_idx)
43dad3d6
DW
6396{
6397 size_t len;
6398 struct imsm_update_create_array *u;
6399 struct intel_super *super = st->sb;
9b1fb677 6400 struct imsm_dev *dev = get_imsm_dev(super, dev_idx);
238c0a71 6401 struct imsm_map *map = get_imsm_map(dev, MAP_0);
54c2c1ea
DW
6402 struct disk_info *inf;
6403 struct imsm_disk *disk;
6404 int i;
43dad3d6 6405
54c2c1ea
DW
6406 len = sizeof(*u) - sizeof(*dev) + sizeof_imsm_dev(dev, 0) +
6407 sizeof(*inf) * map->num_members;
503975b9 6408 u = xmalloc(len);
43dad3d6 6409 u->type = update_create_array;
9b1fb677 6410 u->dev_idx = dev_idx;
43dad3d6 6411 imsm_copy_dev(&u->dev, dev);
54c2c1ea
DW
6412 inf = get_disk_info(u);
6413 for (i = 0; i < map->num_members; i++) {
238c0a71 6414 int idx = get_imsm_disk_idx(dev, i, MAP_X);
9b1fb677 6415
54c2c1ea 6416 disk = get_imsm_disk(super, idx);
1ca5c8e0
N
6417 if (!disk)
6418 disk = get_imsm_missing(super, idx);
54c2c1ea
DW
6419 serialcpy(inf[i].serial, disk->serial);
6420 }
43dad3d6
DW
6421 append_metadata_update(st, u, len);
6422
6423 return 0;
6424}
6425
1a64be56 6426static int mgmt_disk(struct supertype *st)
43dad3d6
DW
6427{
6428 struct intel_super *super = st->sb;
6429 size_t len;
1a64be56 6430 struct imsm_update_add_remove_disk *u;
43dad3d6 6431
1a64be56 6432 if (!super->disk_mgmt_list)
43dad3d6
DW
6433 return 0;
6434
6435 len = sizeof(*u);
503975b9 6436 u = xmalloc(len);
1a64be56 6437 u->type = update_add_remove_disk;
43dad3d6
DW
6438 append_metadata_update(st, u, len);
6439
6440 return 0;
6441}
2432ce9b
AP
6442
6443__u32 crc32c_le(__u32 crc, unsigned char const *p, size_t len);
6444
e397cefe
AP
6445static int write_ppl_header(unsigned long long ppl_sector, int fd, void *buf)
6446{
6447 struct ppl_header *ppl_hdr = buf;
6448 int ret;
6449
6450 ppl_hdr->checksum = __cpu_to_le32(~crc32c_le(~0, buf, PPL_HEADER_SIZE));
6451
6452 if (lseek64(fd, ppl_sector * 512, SEEK_SET) < 0) {
6453 ret = -errno;
6454 perror("Failed to seek to PPL header location");
6455 return ret;
6456 }
6457
6458 if (write(fd, buf, PPL_HEADER_SIZE) != PPL_HEADER_SIZE) {
6459 ret = -errno;
6460 perror("Write PPL header failed");
6461 return ret;
6462 }
6463
6464 fsync(fd);
6465
6466 return 0;
6467}
6468
2432ce9b
AP
6469static int write_init_ppl_imsm(struct supertype *st, struct mdinfo *info, int fd)
6470{
6471 struct intel_super *super = st->sb;
6472 void *buf;
6473 struct ppl_header *ppl_hdr;
6474 int ret;
6475
b2514242
PB
6476 /* first clear entire ppl space */
6477 ret = zero_disk_range(fd, info->ppl_sector, info->ppl_size);
6478 if (ret)
6479 return ret;
6480
6481 ret = posix_memalign(&buf, MAX_SECTOR_SIZE, PPL_HEADER_SIZE);
2432ce9b
AP
6482 if (ret) {
6483 pr_err("Failed to allocate PPL header buffer\n");
e397cefe 6484 return -ret;
2432ce9b
AP
6485 }
6486
6487 memset(buf, 0, PPL_HEADER_SIZE);
6488 ppl_hdr = buf;
6489 memset(ppl_hdr->reserved, 0xff, PPL_HDR_RESERVED);
6490 ppl_hdr->signature = __cpu_to_le32(super->anchor->orig_family_num);
b23d0750
AP
6491
6492 if (info->mismatch_cnt) {
6493 /*
6494 * We are overwriting an invalid ppl. Make one entry with wrong
6495 * checksum to prevent the kernel from skipping resync.
6496 */
6497 ppl_hdr->entries_count = __cpu_to_le32(1);
6498 ppl_hdr->entries[0].checksum = ~0;
6499 }
6500
e397cefe 6501 ret = write_ppl_header(info->ppl_sector, fd, buf);
2432ce9b
AP
6502
6503 free(buf);
6504 return ret;
6505}
6506
e397cefe
AP
6507static int is_rebuilding(struct imsm_dev *dev);
6508
2432ce9b
AP
6509static int validate_ppl_imsm(struct supertype *st, struct mdinfo *info,
6510 struct mdinfo *disk)
6511{
6512 struct intel_super *super = st->sb;
6513 struct dl *d;
e397cefe 6514 void *buf_orig, *buf, *buf_prev = NULL;
2432ce9b 6515 int ret = 0;
e397cefe 6516 struct ppl_header *ppl_hdr = NULL;
2432ce9b
AP
6517 __u32 crc;
6518 struct imsm_dev *dev;
2432ce9b 6519 __u32 idx;
44b6b876
PB
6520 unsigned int i;
6521 unsigned long long ppl_offset = 0;
6522 unsigned long long prev_gen_num = 0;
2432ce9b
AP
6523
6524 if (disk->disk.raid_disk < 0)
6525 return 0;
6526
2432ce9b 6527 dev = get_imsm_dev(super, info->container_member);
2fc0fc63 6528 idx = get_imsm_disk_idx(dev, disk->disk.raid_disk, MAP_0);
2432ce9b
AP
6529 d = get_imsm_dl_disk(super, idx);
6530
6531 if (!d || d->index < 0 || is_failed(&d->disk))
e397cefe
AP
6532 return 0;
6533
6534 if (posix_memalign(&buf_orig, MAX_SECTOR_SIZE, PPL_HEADER_SIZE * 2)) {
6535 pr_err("Failed to allocate PPL header buffer\n");
6536 return -1;
6537 }
6538 buf = buf_orig;
2432ce9b 6539
44b6b876
PB
6540 ret = 1;
6541 while (ppl_offset < MULTIPLE_PPL_AREA_SIZE_IMSM) {
e397cefe
AP
6542 void *tmp;
6543
44b6b876 6544 dprintf("Checking potential PPL at offset: %llu\n", ppl_offset);
2432ce9b 6545
44b6b876
PB
6546 if (lseek64(d->fd, info->ppl_sector * 512 + ppl_offset,
6547 SEEK_SET) < 0) {
6548 perror("Failed to seek to PPL header location");
6549 ret = -1;
e397cefe 6550 break;
44b6b876 6551 }
2432ce9b 6552
44b6b876
PB
6553 if (read(d->fd, buf, PPL_HEADER_SIZE) != PPL_HEADER_SIZE) {
6554 perror("Read PPL header failed");
6555 ret = -1;
e397cefe 6556 break;
44b6b876 6557 }
2432ce9b 6558
44b6b876 6559 ppl_hdr = buf;
2432ce9b 6560
44b6b876
PB
6561 crc = __le32_to_cpu(ppl_hdr->checksum);
6562 ppl_hdr->checksum = 0;
6563
6564 if (crc != ~crc32c_le(~0, buf, PPL_HEADER_SIZE)) {
6565 dprintf("Wrong PPL header checksum on %s\n",
6566 d->devname);
e397cefe 6567 break;
44b6b876
PB
6568 }
6569
6570 if (prev_gen_num > __le64_to_cpu(ppl_hdr->generation)) {
6571 /* previous was newest, it was already checked */
e397cefe 6572 break;
44b6b876
PB
6573 }
6574
6575 if ((__le32_to_cpu(ppl_hdr->signature) !=
6576 super->anchor->orig_family_num)) {
6577 dprintf("Wrong PPL header signature on %s\n",
6578 d->devname);
6579 ret = 1;
e397cefe 6580 break;
44b6b876
PB
6581 }
6582
6583 ret = 0;
6584 prev_gen_num = __le64_to_cpu(ppl_hdr->generation);
2432ce9b 6585
44b6b876
PB
6586 ppl_offset += PPL_HEADER_SIZE;
6587 for (i = 0; i < __le32_to_cpu(ppl_hdr->entries_count); i++)
6588 ppl_offset +=
6589 __le32_to_cpu(ppl_hdr->entries[i].pp_size);
e397cefe
AP
6590
6591 if (!buf_prev)
6592 buf_prev = buf + PPL_HEADER_SIZE;
6593 tmp = buf_prev;
6594 buf_prev = buf;
6595 buf = tmp;
2432ce9b
AP
6596 }
6597
e397cefe
AP
6598 if (buf_prev) {
6599 buf = buf_prev;
6600 ppl_hdr = buf_prev;
6601 }
2432ce9b 6602
54148aba
PB
6603 /*
6604 * Update metadata to use mutliple PPLs area (1MB).
6605 * This is done once for all RAID members
6606 */
6607 if (info->consistency_policy == CONSISTENCY_POLICY_PPL &&
6608 info->ppl_size != (MULTIPLE_PPL_AREA_SIZE_IMSM >> 9)) {
6609 char subarray[20];
6610 struct mdinfo *member_dev;
6611
6612 sprintf(subarray, "%d", info->container_member);
6613
6614 if (mdmon_running(st->container_devnm))
6615 st->update_tail = &st->updates;
6616
03312b52 6617 if (st->ss->update_subarray(st, subarray, UOPT_PPL, NULL)) {
54148aba
PB
6618 pr_err("Failed to update subarray %s\n",
6619 subarray);
6620 } else {
6621 if (st->update_tail)
6622 flush_metadata_updates(st);
6623 else
6624 st->ss->sync_metadata(st);
6625 info->ppl_size = (MULTIPLE_PPL_AREA_SIZE_IMSM >> 9);
6626 for (member_dev = info->devs; member_dev;
6627 member_dev = member_dev->next)
6628 member_dev->ppl_size =
6629 (MULTIPLE_PPL_AREA_SIZE_IMSM >> 9);
6630 }
6631 }
6632
b23d0750 6633 if (ret == 1) {
2fc0fc63
AP
6634 struct imsm_map *map = get_imsm_map(dev, MAP_X);
6635
50b9c10d
PB
6636 if (map->map_state == IMSM_T_STATE_UNINITIALIZED ||
6637 (map->map_state == IMSM_T_STATE_NORMAL &&
2ec9d182 6638 !(dev->vol.dirty & RAIDVOL_DIRTY)) ||
e397cefe 6639 (is_rebuilding(dev) &&
4036e7ee 6640 vol_curr_migr_unit(dev) == 0 &&
2ec9d182 6641 get_imsm_disk_idx(dev, disk->disk.raid_disk, MAP_1) != idx))
b23d0750
AP
6642 ret = st->ss->write_init_ppl(st, info, d->fd);
6643 else
6644 info->mismatch_cnt++;
e397cefe
AP
6645 } else if (ret == 0 &&
6646 ppl_hdr->entries_count == 0 &&
6647 is_rebuilding(dev) &&
6648 info->resync_start == 0) {
6649 /*
6650 * The header has no entries - add a single empty entry and
6651 * rewrite the header to prevent the kernel from going into
6652 * resync after an interrupted rebuild.
6653 */
6654 ppl_hdr->entries_count = __cpu_to_le32(1);
6655 ret = write_ppl_header(info->ppl_sector, d->fd, buf);
b23d0750 6656 }
2432ce9b 6657
e397cefe
AP
6658 free(buf_orig);
6659
2432ce9b
AP
6660 return ret;
6661}
6662
2432ce9b
AP
6663static int write_init_ppl_imsm_all(struct supertype *st, struct mdinfo *info)
6664{
6665 struct intel_super *super = st->sb;
6666 struct dl *d;
6667 int ret = 0;
6668
6669 if (info->consistency_policy != CONSISTENCY_POLICY_PPL ||
6670 info->array.level != 5)
6671 return 0;
6672
6673 for (d = super->disks; d ; d = d->next) {
6674 if (d->index < 0 || is_failed(&d->disk))
6675 continue;
6676
6677 ret = st->ss->write_init_ppl(st, info, d->fd);
6678 if (ret)
6679 break;
6680 }
6681
6682 return ret;
6683}
43dad3d6 6684
fbc42556
JR
6685/*******************************************************************************
6686 * Function: write_init_bitmap_imsm_vol
6687 * Description: Write a bitmap header and prepares the area for the bitmap.
6688 * Parameters:
6689 * st : supertype information
6690 * vol_idx : the volume index to use
6691 *
6692 * Returns:
6693 * 0 : success
6694 * -1 : fail
6695 ******************************************************************************/
6696static int write_init_bitmap_imsm_vol(struct supertype *st, int vol_idx)
6697{
6698 struct intel_super *super = st->sb;
6699 int prev_current_vol = super->current_vol;
6700 struct dl *d;
6701 int ret = 0;
6702
6703 super->current_vol = vol_idx;
6704 for (d = super->disks; d; d = d->next) {
6705 if (d->index < 0 || is_failed(&d->disk))
6706 continue;
6707 ret = st->ss->write_bitmap(st, d->fd, NoUpdate);
6708 if (ret)
6709 break;
6710 }
6711 super->current_vol = prev_current_vol;
6712 return ret;
6713}
6714
6715/*******************************************************************************
6716 * Function: write_init_bitmap_imsm_all
6717 * Description: Write a bitmap header and prepares the area for the bitmap.
6718 * Operation is executed for volumes with CONSISTENCY_POLICY_BITMAP.
6719 * Parameters:
6720 * st : supertype information
6721 * info : info about the volume where the bitmap should be written
6722 * vol_idx : the volume index to use
6723 *
6724 * Returns:
6725 * 0 : success
6726 * -1 : fail
6727 ******************************************************************************/
6728static int write_init_bitmap_imsm_all(struct supertype *st, struct mdinfo *info,
6729 int vol_idx)
6730{
6731 int ret = 0;
6732
6733 if (info && (info->consistency_policy == CONSISTENCY_POLICY_BITMAP))
6734 ret = write_init_bitmap_imsm_vol(st, vol_idx);
6735
6736 return ret;
6737}
6738
c2c087e6
DW
6739static int write_init_super_imsm(struct supertype *st)
6740{
9b1fb677
DW
6741 struct intel_super *super = st->sb;
6742 int current_vol = super->current_vol;
2432ce9b
AP
6743 int rv = 0;
6744 struct mdinfo info;
6745
6746 getinfo_super_imsm(st, &info, NULL);
9b1fb677
DW
6747
6748 /* we are done with current_vol reset it to point st at the container */
6749 super->current_vol = -1;
6750
8273f55e 6751 if (st->update_tail) {
43dad3d6
DW
6752 /* queue the recently created array / added disk
6753 * as a metadata update */
8273f55e 6754
43dad3d6 6755 /* determine if we are creating a volume or adding a disk */
9b1fb677 6756 if (current_vol < 0) {
1a64be56
LM
6757 /* in the mgmt (add/remove) disk case we are running
6758 * in mdmon context, so don't close fd's
43dad3d6 6759 */
2432ce9b
AP
6760 rv = mgmt_disk(st);
6761 } else {
fbc42556 6762 /* adding the second volume to the array */
2432ce9b 6763 rv = write_init_ppl_imsm_all(st, &info);
fbc42556
JR
6764 if (!rv)
6765 rv = write_init_bitmap_imsm_all(st, &info, current_vol);
2432ce9b
AP
6766 if (!rv)
6767 rv = create_array(st, current_vol);
6768 }
d682f344
N
6769 } else {
6770 struct dl *d;
6771 for (d = super->disks; d; d = d->next)
ba728be7 6772 Kill(d->devname, NULL, 0, -1, 1);
fbc42556 6773 if (current_vol >= 0) {
2432ce9b 6774 rv = write_init_ppl_imsm_all(st, &info);
fbc42556
JR
6775 if (!rv)
6776 rv = write_init_bitmap_imsm_all(st, &info, current_vol);
6777 }
6778
2432ce9b
AP
6779 if (!rv)
6780 rv = write_super_imsm(st, 1);
d682f344 6781 }
2432ce9b
AP
6782
6783 return rv;
cdddbdbc
DW
6784}
6785
e683ca88 6786static int store_super_imsm(struct supertype *st, int fd)
cdddbdbc 6787{
e683ca88
DW
6788 struct intel_super *super = st->sb;
6789 struct imsm_super *mpb = super ? super->anchor : NULL;
551c80c1 6790
e683ca88 6791 if (!mpb)
ad97895e
DW
6792 return 1;
6793
f36a9ecd
PB
6794 if (super->sector_size == 4096)
6795 convert_to_4k(super);
e683ca88 6796 return store_imsm_mpb(fd, mpb);
cdddbdbc
DW
6797}
6798
cdddbdbc 6799static int validate_geometry_imsm_container(struct supertype *st, int level,
1f5d54a0 6800 int raiddisks,
af4348dd
N
6801 unsigned long long data_offset,
6802 char *dev,
2c514b71
NB
6803 unsigned long long *freesize,
6804 int verbose)
cdddbdbc 6805{
c2c087e6
DW
6806 int fd;
6807 unsigned long long ldsize;
8662f92d 6808 struct intel_super *super = NULL;
f2f5c343 6809 int rv = 0;
cdddbdbc 6810
6f2af6a4 6811 if (!is_container(level))
c2c087e6
DW
6812 return 0;
6813 if (!dev)
6814 return 1;
6815
dca80fcd 6816 fd = dev_open(dev, O_RDONLY|O_EXCL);
4389ce73
MT
6817 if (!is_fd_valid(fd)) {
6818 pr_vrb("imsm: Cannot open %s: %s\n", dev, strerror(errno));
c2c087e6
DW
6819 return 0;
6820 }
8662f92d
MT
6821 if (!get_dev_size(fd, dev, &ldsize))
6822 goto exit;
f2f5c343
LM
6823
6824 /* capabilities retrieve could be possible
6825 * note that there is no fd for the disks in array.
6826 */
6827 super = alloc_super();
8662f92d
MT
6828 if (!super)
6829 goto exit;
6830
6831 if (!get_dev_sector_size(fd, NULL, &super->sector_size))
6832 goto exit;
fa7bb6f8 6833
ba728be7 6834 rv = find_intel_hba_capability(fd, super, verbose > 0 ? dev : NULL);
f2f5c343
LM
6835 if (rv != 0) {
6836#if DEBUG
6837 char str[256];
6838 fd2devname(fd, str);
1ade5cc1 6839 dprintf("fd: %d %s orom: %p rv: %d raiddisk: %d\n",
f2f5c343
LM
6840 fd, str, super->orom, rv, raiddisks);
6841#endif
6842 /* no orom/efi or non-intel hba of the disk */
8662f92d
MT
6843 rv = 0;
6844 goto exit;
f2f5c343 6845 }
9126b9a8
CA
6846 if (super->orom) {
6847 if (raiddisks > super->orom->tds) {
6848 if (verbose)
7a862a02 6849 pr_err("%d exceeds maximum number of platform supported disks: %d\n",
9126b9a8 6850 raiddisks, super->orom->tds);
8662f92d 6851 goto exit;
9126b9a8
CA
6852 }
6853 if ((super->orom->attr & IMSM_OROM_ATTR_2TB_DISK) == 0 &&
6854 (ldsize >> 9) >> 32 > 0) {
6855 if (verbose)
e7b84f9d 6856 pr_err("%s exceeds maximum platform supported size\n", dev);
8662f92d
MT
6857 goto exit;
6858 }
6859
6860 if (super->hba->type == SYS_DEV_VMD ||
6861 super->hba->type == SYS_DEV_NVME) {
6862 if (!imsm_is_nvme_namespace_supported(fd, 1)) {
6863 if (verbose)
6864 pr_err("NVMe namespace %s is not supported by IMSM\n",
6865 basename(dev));
6866 goto exit;
6867 }
9126b9a8 6868 }
f2f5c343 6869 }
1f5d54a0
MT
6870 if (freesize)
6871 *freesize = avail_size_imsm(st, ldsize >> 9, data_offset);
8662f92d
MT
6872 rv = 1;
6873exit:
6874 if (super)
6875 free_imsm(super);
6876 close(fd);
c2c087e6 6877
8662f92d 6878 return rv;
cdddbdbc
DW
6879}
6880
0dcecb2e
DW
6881static unsigned long long find_size(struct extent *e, int *idx, int num_extents)
6882{
6883 const unsigned long long base_start = e[*idx].start;
6884 unsigned long long end = base_start + e[*idx].size;
6885 int i;
6886
6887 if (base_start == end)
6888 return 0;
6889
6890 *idx = *idx + 1;
6891 for (i = *idx; i < num_extents; i++) {
6892 /* extend overlapping extents */
6893 if (e[i].start >= base_start &&
6894 e[i].start <= end) {
6895 if (e[i].size == 0)
6896 return 0;
6897 if (e[i].start + e[i].size > end)
6898 end = e[i].start + e[i].size;
6899 } else if (e[i].start > end) {
6900 *idx = i;
6901 break;
6902 }
6903 }
6904
6905 return end - base_start;
6906}
6907
aa19fdd4 6908/** merge_extents() - analyze extents and get free size.
9bc426fa 6909 * @super: Intel metadata, not NULL.
aa19fdd4 6910 * @expanding: if set, we are expanding &super->current_vol.
9bc426fa 6911 *
aa19fdd4
MT
6912 * Build a composite disk with all known extents and generate a size given the
6913 * "all disks in an array must share a common start offset" constraint.
6914 * If a volume is expanded, then return free space after the volume.
9bc426fa 6915 *
aa19fdd4 6916 * Return: Free space or 0 on failure.
9bc426fa 6917 */
aa19fdd4 6918static unsigned long long merge_extents(struct intel_super *super, const bool expanding)
0dcecb2e 6919{
9bc426fa 6920 struct extent *e;
0dcecb2e 6921 struct dl *dl;
aa19fdd4
MT
6922 int i, j, pos_vol_idx = -1;
6923 int extent_idx = 0;
6924 int sum_extents = 0;
6925 unsigned long long pos = 0;
b9d77223 6926 unsigned long long start = 0;
1dea84ae
MT
6927 unsigned long long free_size = 0;
6928
6929 unsigned long pre_reservation = 0;
6930 unsigned long post_reservation = IMSM_RESERVED_SECTORS;
6931 unsigned long reservation_size;
0dcecb2e 6932
9bc426fa
MT
6933 for (dl = super->disks; dl; dl = dl->next)
6934 if (dl->e)
6935 sum_extents += dl->extent_cnt;
6936 e = xcalloc(sum_extents, sizeof(struct extent));
6937
0dcecb2e
DW
6938 /* coalesce and sort all extents. also, check to see if we need to
6939 * reserve space between member arrays
6940 */
6941 j = 0;
6942 for (dl = super->disks; dl; dl = dl->next) {
6943 if (!dl->e)
6944 continue;
6945 for (i = 0; i < dl->extent_cnt; i++)
6946 e[j++] = dl->e[i];
6947 }
6948 qsort(e, sum_extents, sizeof(*e), cmp_extent);
6949
6950 /* merge extents */
6951 i = 0;
6952 j = 0;
6953 while (i < sum_extents) {
6954 e[j].start = e[i].start;
aa19fdd4 6955 e[j].vol = e[i].vol;
0dcecb2e
DW
6956 e[j].size = find_size(e, &i, sum_extents);
6957 j++;
6958 if (e[j-1].size == 0)
6959 break;
6960 }
6961
0dcecb2e
DW
6962 i = 0;
6963 do {
aa19fdd4 6964 unsigned long long esize = e[i].start - pos;
0dcecb2e 6965
1dea84ae
MT
6966 if (expanding ? pos_vol_idx == super->current_vol : esize >= free_size) {
6967 free_size = esize;
0dcecb2e 6968 start = pos;
aa19fdd4 6969 extent_idx = i;
0dcecb2e 6970 }
aa19fdd4 6971
0dcecb2e 6972 pos = e[i].start + e[i].size;
aa19fdd4
MT
6973 pos_vol_idx = e[i].vol;
6974
0dcecb2e
DW
6975 i++;
6976 } while (e[i-1].size);
0dcecb2e 6977
1dea84ae
MT
6978 if (free_size == 0) {
6979 dprintf("imsm: Cannot find free size.\n");
6980 free(e);
a7dd165b 6981 return 0;
1dea84ae 6982 }
a7dd165b 6983
1dea84ae
MT
6984 if (!expanding && extent_idx != 0)
6985 /*
6986 * Not a real first volume in a container is created, pre_reservation is needed.
6987 */
6988 pre_reservation = IMSM_RESERVED_SECTORS;
0dcecb2e 6989
1dea84ae
MT
6990 if (e[extent_idx].size == 0)
6991 /*
6992 * extent_idx points to the metadata, post_reservation is allready done.
6993 */
6994 post_reservation = 0;
6995 free(e);
0dcecb2e 6996
1dea84ae
MT
6997 reservation_size = pre_reservation + post_reservation;
6998
6999 if (free_size < reservation_size) {
7000 dprintf("imsm: Reservation size is greater than free space.\n");
7001 return 0;
7002 }
0dcecb2e 7003
1dea84ae
MT
7004 super->create_offset = start + pre_reservation;
7005 return free_size - reservation_size;
0dcecb2e
DW
7006}
7007
88c32bb1
DW
7008static int is_raid_level_supported(const struct imsm_orom *orom, int level, int raiddisks)
7009{
7010 if (level < 0 || level == 6 || level == 4)
7011 return 0;
7012
7013 /* if we have an orom prevent invalid raid levels */
7014 if (orom)
7015 switch (level) {
7016 case 0: return imsm_orom_has_raid0(orom);
7017 case 1:
7018 if (raiddisks > 2)
7019 return imsm_orom_has_raid1e(orom);
1c556e92
DW
7020 return imsm_orom_has_raid1(orom) && raiddisks == 2;
7021 case 10: return imsm_orom_has_raid10(orom) && raiddisks == 4;
7022 case 5: return imsm_orom_has_raid5(orom) && raiddisks > 2;
88c32bb1
DW
7023 }
7024 else
7025 return 1; /* not on an Intel RAID platform so anything goes */
7026
7027 return 0;
7028}
7029
ca9de185
LM
7030static int
7031active_arrays_by_format(char *name, char* hba, struct md_list **devlist,
7032 int dpa, int verbose)
7033{
7034 struct mdstat_ent *mdstat = mdstat_read(0, 0);
594dc1b8 7035 struct mdstat_ent *memb;
ca9de185
LM
7036 int count = 0;
7037 int num = 0;
594dc1b8 7038 struct md_list *dv;
ca9de185
LM
7039 int found;
7040
7041 for (memb = mdstat ; memb ; memb = memb->next) {
7042 if (memb->metadata_version &&
fc54fe7a 7043 (strncmp(memb->metadata_version, "external:", 9) == 0) &&
ca9de185
LM
7044 (strcmp(&memb->metadata_version[9], name) == 0) &&
7045 !is_subarray(memb->metadata_version+9) &&
7046 memb->members) {
7047 struct dev_member *dev = memb->members;
7048 int fd = -1;
4389ce73 7049 while (dev && !is_fd_valid(fd)) {
503975b9 7050 char *path = xmalloc(strlen(dev->name) + strlen("/dev/") + 1);
dd5ab402 7051 num = snprintf(path, PATH_MAX, "%s%s", "/dev/", dev->name);
503975b9
N
7052 if (num > 0)
7053 fd = open(path, O_RDONLY, 0);
4389ce73 7054 if (num <= 0 || !is_fd_valid(fd)) {
676e87a8 7055 pr_vrb("Cannot open %s: %s\n",
503975b9 7056 dev->name, strerror(errno));
ca9de185 7057 }
503975b9 7058 free(path);
ca9de185
LM
7059 dev = dev->next;
7060 }
7061 found = 0;
4389ce73 7062 if (is_fd_valid(fd) && disk_attached_to_hba(fd, hba)) {
ca9de185
LM
7063 struct mdstat_ent *vol;
7064 for (vol = mdstat ; vol ; vol = vol->next) {
089f9d79 7065 if (vol->active > 0 &&
ca9de185 7066 vol->metadata_version &&
9581efb1 7067 is_container_member(vol, memb->devnm)) {
ca9de185
LM
7068 found++;
7069 count++;
7070 }
7071 }
7072 if (*devlist && (found < dpa)) {
503975b9 7073 dv = xcalloc(1, sizeof(*dv));
9581efb1
N
7074 dv->devname = xmalloc(strlen(memb->devnm) + strlen("/dev/") + 1);
7075 sprintf(dv->devname, "%s%s", "/dev/", memb->devnm);
503975b9
N
7076 dv->found = found;
7077 dv->used = 0;
7078 dv->next = *devlist;
7079 *devlist = dv;
ca9de185
LM
7080 }
7081 }
4389ce73 7082 close_fd(&fd);
ca9de185
LM
7083 }
7084 }
7085 free_mdstat(mdstat);
7086 return count;
7087}
7088
7089#ifdef DEBUG_LOOP
7090static struct md_list*
7091get_loop_devices(void)
7092{
7093 int i;
7094 struct md_list *devlist = NULL;
594dc1b8 7095 struct md_list *dv;
ca9de185
LM
7096
7097 for(i = 0; i < 12; i++) {
503975b9
N
7098 dv = xcalloc(1, sizeof(*dv));
7099 dv->devname = xmalloc(40);
ca9de185
LM
7100 sprintf(dv->devname, "/dev/loop%d", i);
7101 dv->next = devlist;
7102 devlist = dv;
7103 }
7104 return devlist;
7105}
7106#endif
7107
7108static struct md_list*
7109get_devices(const char *hba_path)
7110{
7111 struct md_list *devlist = NULL;
594dc1b8 7112 struct md_list *dv;
ca9de185
LM
7113 struct dirent *ent;
7114 DIR *dir;
7115 int err = 0;
7116
7117#if DEBUG_LOOP
7118 devlist = get_loop_devices();
7119 return devlist;
7120#endif
7121 /* scroll through /sys/dev/block looking for devices attached to
7122 * this hba
7123 */
7124 dir = opendir("/sys/dev/block");
7125 for (ent = dir ? readdir(dir) : NULL; ent; ent = readdir(dir)) {
7126 int fd;
7127 char buf[1024];
7128 int major, minor;
7129 char *path = NULL;
7130 if (sscanf(ent->d_name, "%d:%d", &major, &minor) != 2)
7131 continue;
7c798f87 7132 path = devt_to_devpath(makedev(major, minor), 1, NULL);
ca9de185
LM
7133 if (!path)
7134 continue;
7135 if (!path_attached_to_hba(path, hba_path)) {
7136 free(path);
7137 path = NULL;
7138 continue;
7139 }
7140 free(path);
7141 path = NULL;
7142 fd = dev_open(ent->d_name, O_RDONLY);
4389ce73 7143 if (is_fd_valid(fd)) {
ca9de185
LM
7144 fd2devname(fd, buf);
7145 close(fd);
7146 } else {
e7b84f9d 7147 pr_err("cannot open device: %s\n",
ca9de185
LM
7148 ent->d_name);
7149 continue;
7150 }
7151
503975b9
N
7152 dv = xcalloc(1, sizeof(*dv));
7153 dv->devname = xstrdup(buf);
ca9de185
LM
7154 dv->next = devlist;
7155 devlist = dv;
7156 }
7157 if (err) {
7158 while(devlist) {
7159 dv = devlist;
7160 devlist = devlist->next;
7161 free(dv->devname);
7162 free(dv);
7163 }
7164 }
562aa102 7165 closedir(dir);
ca9de185
LM
7166 return devlist;
7167}
7168
7169static int
7170count_volumes_list(struct md_list *devlist, char *homehost,
7171 int verbose, int *found)
7172{
7173 struct md_list *tmpdev;
7174 int count = 0;
594dc1b8 7175 struct supertype *st;
ca9de185
LM
7176
7177 /* first walk the list of devices to find a consistent set
7178 * that match the criterea, if that is possible.
7179 * We flag the ones we like with 'used'.
7180 */
7181 *found = 0;
7182 st = match_metadata_desc_imsm("imsm");
7183 if (st == NULL) {
676e87a8 7184 pr_vrb("cannot allocate memory for imsm supertype\n");
ca9de185
LM
7185 return 0;
7186 }
7187
7188 for (tmpdev = devlist; tmpdev; tmpdev = tmpdev->next) {
7189 char *devname = tmpdev->devname;
0a6bff09 7190 dev_t rdev;
ca9de185
LM
7191 struct supertype *tst;
7192 int dfd;
7193 if (tmpdev->used > 1)
7194 continue;
7195 tst = dup_super(st);
7196 if (tst == NULL) {
676e87a8 7197 pr_vrb("cannot allocate memory for imsm supertype\n");
ca9de185
LM
7198 goto err_1;
7199 }
7200 tmpdev->container = 0;
7201 dfd = dev_open(devname, O_RDONLY|O_EXCL);
4389ce73 7202 if (!is_fd_valid(dfd)) {
1ade5cc1 7203 dprintf("cannot open device %s: %s\n",
ca9de185
LM
7204 devname, strerror(errno));
7205 tmpdev->used = 2;
0a6bff09 7206 } else if (!fstat_is_blkdev(dfd, devname, &rdev)) {
ca9de185
LM
7207 tmpdev->used = 2;
7208 } else if (must_be_container(dfd)) {
7209 struct supertype *cst;
7210 cst = super_by_fd(dfd, NULL);
7211 if (cst == NULL) {
1ade5cc1 7212 dprintf("cannot recognize container type %s\n",
ca9de185
LM
7213 devname);
7214 tmpdev->used = 2;
7215 } else if (tst->ss != st->ss) {
1ade5cc1 7216 dprintf("non-imsm container - ignore it: %s\n",
ca9de185
LM
7217 devname);
7218 tmpdev->used = 2;
7219 } else if (!tst->ss->load_container ||
7220 tst->ss->load_container(tst, dfd, NULL))
7221 tmpdev->used = 2;
7222 else {
7223 tmpdev->container = 1;
7224 }
7225 if (cst)
7226 cst->ss->free_super(cst);
7227 } else {
0a6bff09 7228 tmpdev->st_rdev = rdev;
ca9de185 7229 if (tst->ss->load_super(tst,dfd, NULL)) {
1ade5cc1 7230 dprintf("no RAID superblock on %s\n",
ca9de185
LM
7231 devname);
7232 tmpdev->used = 2;
7233 } else if (tst->ss->compare_super == NULL) {
1ade5cc1 7234 dprintf("Cannot assemble %s metadata on %s\n",
ca9de185
LM
7235 tst->ss->name, devname);
7236 tmpdev->used = 2;
7237 }
7238 }
4389ce73
MT
7239 close_fd(&dfd);
7240
ca9de185
LM
7241 if (tmpdev->used == 2 || tmpdev->used == 4) {
7242 /* Ignore unrecognised devices during auto-assembly */
7243 goto loop;
7244 }
7245 else {
7246 struct mdinfo info;
7247 tst->ss->getinfo_super(tst, &info, NULL);
7248
7249 if (st->minor_version == -1)
7250 st->minor_version = tst->minor_version;
7251
7252 if (memcmp(info.uuid, uuid_zero,
7253 sizeof(int[4])) == 0) {
7254 /* this is a floating spare. It cannot define
7255 * an array unless there are no more arrays of
7256 * this type to be found. It can be included
7257 * in an array of this type though.
7258 */
7259 tmpdev->used = 3;
7260 goto loop;
7261 }
7262
7263 if (st->ss != tst->ss ||
7264 st->minor_version != tst->minor_version ||
c7b8547c 7265 st->ss->compare_super(st, tst, 1) != 0) {
ca9de185
LM
7266 /* Some mismatch. If exactly one array matches this host,
7267 * we can resolve on that one.
7268 * Or, if we are auto assembling, we just ignore the second
7269 * for now.
7270 */
1ade5cc1 7271 dprintf("superblock on %s doesn't match others - assembly aborted\n",
ca9de185
LM
7272 devname);
7273 goto loop;
7274 }
7275 tmpdev->used = 1;
7276 *found = 1;
7277 dprintf("found: devname: %s\n", devname);
7278 }
7279 loop:
7280 if (tst)
7281 tst->ss->free_super(tst);
7282 }
7283 if (*found != 0) {
7284 int err;
7285 if ((err = load_super_imsm_all(st, -1, &st->sb, NULL, devlist, 0)) == 0) {
7286 struct mdinfo *iter, *head = st->ss->container_content(st, NULL);
7287 for (iter = head; iter; iter = iter->next) {
7288 dprintf("content->text_version: %s vol\n",
7289 iter->text_version);
7290 if (iter->array.state & (1<<MD_SB_BLOCK_VOLUME)) {
7291 /* do not assemble arrays with unsupported
7292 configurations */
1ade5cc1 7293 dprintf("Cannot activate member %s.\n",
ca9de185
LM
7294 iter->text_version);
7295 } else
7296 count++;
7297 }
7298 sysfs_free(head);
7299
7300 } else {
1ade5cc1 7301 dprintf("No valid super block on device list: err: %d %p\n",
ca9de185
LM
7302 err, st->sb);
7303 }
7304 } else {
1ade5cc1 7305 dprintf("no more devices to examine\n");
ca9de185
LM
7306 }
7307
7308 for (tmpdev = devlist; tmpdev; tmpdev = tmpdev->next) {
089f9d79 7309 if (tmpdev->used == 1 && tmpdev->found) {
ca9de185
LM
7310 if (count) {
7311 if (count < tmpdev->found)
7312 count = 0;
7313 else
7314 count -= tmpdev->found;
7315 }
7316 }
7317 if (tmpdev->used == 1)
7318 tmpdev->used = 4;
7319 }
7320 err_1:
7321 if (st)
7322 st->ss->free_super(st);
7323 return count;
7324}
7325
d3c11416
AO
7326static int __count_volumes(char *hba_path, int dpa, int verbose,
7327 int cmp_hba_path)
ca9de185 7328{
72a45777 7329 struct sys_dev *idev, *intel_devices = find_intel_devices();
ca9de185 7330 int count = 0;
72a45777
PB
7331 const struct orom_entry *entry;
7332 struct devid_list *dv, *devid_list;
ca9de185 7333
d3c11416 7334 if (!hba_path)
ca9de185
LM
7335 return 0;
7336
72a45777 7337 for (idev = intel_devices; idev; idev = idev->next) {
d3c11416
AO
7338 if (strstr(idev->path, hba_path))
7339 break;
72a45777
PB
7340 }
7341
7342 if (!idev || !idev->dev_id)
ca9de185 7343 return 0;
72a45777
PB
7344
7345 entry = get_orom_entry_by_device_id(idev->dev_id);
7346
7347 if (!entry || !entry->devid_list)
7348 return 0;
7349
7350 devid_list = entry->devid_list;
7351 for (dv = devid_list; dv; dv = dv->next) {
594dc1b8 7352 struct md_list *devlist;
d3c11416
AO
7353 struct sys_dev *device = NULL;
7354 char *hpath;
72a45777
PB
7355 int found = 0;
7356
d3c11416
AO
7357 if (cmp_hba_path)
7358 device = device_by_id_and_path(dv->devid, hba_path);
7359 else
7360 device = device_by_id(dv->devid);
7361
72a45777 7362 if (device)
d3c11416 7363 hpath = device->path;
72a45777
PB
7364 else
7365 return 0;
7366
d3c11416 7367 devlist = get_devices(hpath);
72a45777
PB
7368 /* if no intel devices return zero volumes */
7369 if (devlist == NULL)
7370 return 0;
7371
d3c11416
AO
7372 count += active_arrays_by_format("imsm", hpath, &devlist, dpa,
7373 verbose);
7374 dprintf("path: %s active arrays: %d\n", hpath, count);
72a45777
PB
7375 if (devlist == NULL)
7376 return 0;
7377 do {
7378 found = 0;
7379 count += count_volumes_list(devlist,
7380 NULL,
7381 verbose,
7382 &found);
7383 dprintf("found %d count: %d\n", found, count);
7384 } while (found);
7385
d3c11416 7386 dprintf("path: %s total number of volumes: %d\n", hpath, count);
72a45777
PB
7387
7388 while (devlist) {
7389 struct md_list *dv = devlist;
7390 devlist = devlist->next;
7391 free(dv->devname);
7392 free(dv);
7393 }
ca9de185
LM
7394 }
7395 return count;
7396}
7397
d3c11416
AO
7398static int count_volumes(struct intel_hba *hba, int dpa, int verbose)
7399{
7400 if (!hba)
7401 return 0;
7402 if (hba->type == SYS_DEV_VMD) {
7403 struct sys_dev *dev;
7404 int count = 0;
7405
7406 for (dev = find_intel_devices(); dev; dev = dev->next) {
7407 if (dev->type == SYS_DEV_VMD)
7408 count += __count_volumes(dev->path, dpa,
7409 verbose, 1);
7410 }
7411 return count;
7412 }
7413 return __count_volumes(hba->path, dpa, verbose, 0);
7414}
7415
cd9d1ac7
DW
7416static int imsm_default_chunk(const struct imsm_orom *orom)
7417{
7418 /* up to 512 if the plaform supports it, otherwise the platform max.
7419 * 128 if no platform detected
7420 */
7421 int fs = max(7, orom ? fls(orom->sss) : 0);
7422
7423 return min(512, (1 << fs));
7424}
73408129 7425
6592ce37
DW
7426static int
7427validate_geometry_imsm_orom(struct intel_super *super, int level, int layout,
2cc699af 7428 int raiddisks, int *chunk, unsigned long long size, int verbose)
6592ce37 7429{
660260d0
DW
7430 /* check/set platform and metadata limits/defaults */
7431 if (super->orom && raiddisks > super->orom->dpa) {
676e87a8 7432 pr_vrb("platform supports a maximum of %d disks per array\n",
660260d0 7433 super->orom->dpa);
73408129
LM
7434 return 0;
7435 }
7436
5d500228 7437 /* capabilities of OROM tested - copied from validate_geometry_imsm_volume */
660260d0 7438 if (!is_raid_level_supported(super->orom, level, raiddisks)) {
676e87a8 7439 pr_vrb("platform does not support raid%d with %d disk%s\n",
6592ce37
DW
7440 level, raiddisks, raiddisks > 1 ? "s" : "");
7441 return 0;
7442 }
cd9d1ac7 7443
7ccc4cc4 7444 if (*chunk == 0 || *chunk == UnSet)
cd9d1ac7
DW
7445 *chunk = imsm_default_chunk(super->orom);
7446
7ccc4cc4 7447 if (super->orom && !imsm_orom_has_chunk(super->orom, *chunk)) {
676e87a8 7448 pr_vrb("platform does not support a chunk size of: %d\n", *chunk);
cd9d1ac7 7449 return 0;
6592ce37 7450 }
cd9d1ac7 7451
6592ce37
DW
7452 if (layout != imsm_level_to_layout(level)) {
7453 if (level == 5)
676e87a8 7454 pr_vrb("imsm raid 5 only supports the left-asymmetric layout\n");
6592ce37 7455 else if (level == 10)
676e87a8 7456 pr_vrb("imsm raid 10 only supports the n2 layout\n");
6592ce37 7457 else
676e87a8 7458 pr_vrb("imsm unknown layout %#x for this raid level %d\n",
6592ce37
DW
7459 layout, level);
7460 return 0;
7461 }
2cc699af 7462
7ccc4cc4 7463 if (super->orom && (super->orom->attr & IMSM_OROM_ATTR_2TB) == 0 &&
2cc699af 7464 (calc_array_size(level, raiddisks, layout, *chunk, size) >> 32) > 0) {
676e87a8 7465 pr_vrb("platform does not support a volume size over 2TB\n");
2cc699af
CA
7466 return 0;
7467 }
614902f6 7468
6592ce37
DW
7469 return 1;
7470}
7471
1011e834 7472/* validate_geometry_imsm_volume - lifted from validate_geometry_ddf_bvd
c2c087e6
DW
7473 * FIX ME add ahci details
7474 */
8b353278 7475static int validate_geometry_imsm_volume(struct supertype *st, int level,
c21e737b 7476 int layout, int raiddisks, int *chunk,
af4348dd
N
7477 unsigned long long size,
7478 unsigned long long data_offset,
7479 char *dev,
2c514b71
NB
7480 unsigned long long *freesize,
7481 int verbose)
cdddbdbc 7482{
9e04ac1c 7483 dev_t rdev;
c2c087e6 7484 struct intel_super *super = st->sb;
b2916f25 7485 struct imsm_super *mpb;
c2c087e6
DW
7486 struct dl *dl;
7487 unsigned long long pos = 0;
7488 unsigned long long maxsize;
7489 struct extent *e;
7490 int i;
cdddbdbc 7491
88c32bb1
DW
7492 /* We must have the container info already read in. */
7493 if (!super)
c2c087e6
DW
7494 return 0;
7495
b2916f25
JS
7496 mpb = super->anchor;
7497
2cc699af 7498 if (!validate_geometry_imsm_orom(super, level, layout, raiddisks, chunk, size, verbose)) {
3e684231 7499 pr_err("RAID geometry validation failed. Cannot proceed with the action(s).\n");
c2c087e6 7500 return 0;
d54559f0 7501 }
c2c087e6
DW
7502 if (!dev) {
7503 /* General test: make sure there is space for
2da8544a
DW
7504 * 'raiddisks' device extents of size 'size' at a given
7505 * offset
c2c087e6 7506 */
e46273eb 7507 unsigned long long minsize = size;
b7528a20 7508 unsigned long long start_offset = MaxSector;
c2c087e6
DW
7509 int dcnt = 0;
7510 if (minsize == 0)
7511 minsize = MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS;
7512 for (dl = super->disks; dl ; dl = dl->next) {
7513 int found = 0;
7514
bf5a934a 7515 pos = 0;
c2c087e6 7516 i = 0;
05501181 7517 e = get_extents(super, dl, 0);
c2c087e6
DW
7518 if (!e) continue;
7519 do {
7520 unsigned long long esize;
7521 esize = e[i].start - pos;
7522 if (esize >= minsize)
7523 found = 1;
b7528a20 7524 if (found && start_offset == MaxSector) {
2da8544a
DW
7525 start_offset = pos;
7526 break;
7527 } else if (found && pos != start_offset) {
7528 found = 0;
7529 break;
7530 }
c2c087e6
DW
7531 pos = e[i].start + e[i].size;
7532 i++;
7533 } while (e[i-1].size);
7534 if (found)
7535 dcnt++;
7536 free(e);
7537 }
7538 if (dcnt < raiddisks) {
2c514b71 7539 if (verbose)
7a862a02 7540 pr_err("imsm: Not enough devices with space for this array (%d < %d)\n",
2c514b71 7541 dcnt, raiddisks);
c2c087e6
DW
7542 return 0;
7543 }
7544 return 1;
7545 }
0dcecb2e 7546
c2c087e6 7547 /* This device must be a member of the set */
9e04ac1c 7548 if (!stat_is_blkdev(dev, &rdev))
c2c087e6
DW
7549 return 0;
7550 for (dl = super->disks ; dl ; dl = dl->next) {
9e04ac1c
ZL
7551 if (dl->major == (int)major(rdev) &&
7552 dl->minor == (int)minor(rdev))
c2c087e6
DW
7553 break;
7554 }
7555 if (!dl) {
2c514b71 7556 if (verbose)
7a862a02 7557 pr_err("%s is not in the same imsm set\n", dev);
c2c087e6 7558 return 0;
a20d2ba5
DW
7559 } else if (super->orom && dl->index < 0 && mpb->num_raid_devs) {
7560 /* If a volume is present then the current creation attempt
7561 * cannot incorporate new spares because the orom may not
7562 * understand this configuration (all member disks must be
7563 * members of each array in the container).
7564 */
7a862a02
N
7565 pr_err("%s is a spare and a volume is already defined for this container\n", dev);
7566 pr_err("The option-rom requires all member disks to be a member of all volumes\n");
a20d2ba5 7567 return 0;
5fe62b94
WD
7568 } else if (super->orom && mpb->num_raid_devs > 0 &&
7569 mpb->num_disks != raiddisks) {
7a862a02 7570 pr_err("The option-rom requires all member disks to be a member of all volumes\n");
5fe62b94 7571 return 0;
c2c087e6 7572 }
0dcecb2e
DW
7573
7574 /* retrieve the largest free space block */
05501181 7575 e = get_extents(super, dl, 0);
c2c087e6
DW
7576 maxsize = 0;
7577 i = 0;
0dcecb2e
DW
7578 if (e) {
7579 do {
7580 unsigned long long esize;
7581
7582 esize = e[i].start - pos;
7583 if (esize >= maxsize)
7584 maxsize = esize;
7585 pos = e[i].start + e[i].size;
7586 i++;
7587 } while (e[i-1].size);
7588 dl->e = e;
7589 dl->extent_cnt = i;
7590 } else {
7591 if (verbose)
e7b84f9d 7592 pr_err("unable to determine free space for: %s\n",
0dcecb2e
DW
7593 dev);
7594 return 0;
7595 }
7596 if (maxsize < size) {
7597 if (verbose)
e7b84f9d 7598 pr_err("%s not enough space (%llu < %llu)\n",
0dcecb2e
DW
7599 dev, maxsize, size);
7600 return 0;
7601 }
7602
aa19fdd4 7603 maxsize = merge_extents(super, false);
3baa56ab 7604
1a1ced1e
KS
7605 if (mpb->num_raid_devs > 0 && size && size != maxsize)
7606 pr_err("attempting to create a second volume with size less then remaining space.\n");
3baa56ab 7607
a7dd165b 7608 if (maxsize < size || maxsize == 0) {
b3071342
LD
7609 if (verbose) {
7610 if (maxsize == 0)
7a862a02 7611 pr_err("no free space left on device. Aborting...\n");
b3071342 7612 else
7a862a02 7613 pr_err("not enough space to create volume of given size (%llu < %llu). Aborting...\n",
b3071342
LD
7614 maxsize, size);
7615 }
0dcecb2e 7616 return 0;
0dcecb2e
DW
7617 }
7618
c2c087e6
DW
7619 *freesize = maxsize;
7620
ca9de185 7621 if (super->orom) {
72a45777 7622 int count = count_volumes(super->hba,
ca9de185
LM
7623 super->orom->dpa, verbose);
7624 if (super->orom->vphba <= count) {
676e87a8 7625 pr_vrb("platform does not support more than %d raid volumes.\n",
ca9de185
LM
7626 super->orom->vphba);
7627 return 0;
7628 }
7629 }
c2c087e6 7630 return 1;
cdddbdbc
DW
7631}
7632
6d4d9ab2
MT
7633/**
7634 * imsm_get_free_size() - get the biggest, common free space from members.
7635 * @super: &intel_super pointer, not NULL.
7636 * @raiddisks: number of raid disks.
7637 * @size: requested size, could be 0 (means max size).
5f027b93 7638 * @chunk: requested chunk size in KiB.
6d4d9ab2
MT
7639 * @freesize: pointer for returned size value.
7640 *
7641 * Return: &IMSM_STATUS_OK or &IMSM_STATUS_ERROR.
7642 *
7643 * @freesize is set to meaningful value, this can be @size, or calculated
7644 * max free size.
7645 * super->create_offset value is modified and set appropriately in
7646 * merge_extends() for further creation.
7647 */
7648static imsm_status_t imsm_get_free_size(struct intel_super *super,
7649 const int raiddisks,
7650 unsigned long long size,
7651 const int chunk,
aa19fdd4
MT
7652 unsigned long long *freesize,
7653 bool expanding)
efb30e7f 7654{
efb30e7f
DW
7655 struct imsm_super *mpb = super->anchor;
7656 struct dl *dl;
7657 int i;
efb30e7f 7658 struct extent *e;
5f027b93
MT
7659 int cnt = 0;
7660 int used = 0;
efb30e7f 7661 unsigned long long maxsize;
5f027b93
MT
7662 unsigned long long minsize = size;
7663
7664 if (minsize == 0)
7665 minsize = chunk * 2;
efb30e7f
DW
7666
7667 /* find the largest common start free region of the possible disks */
efb30e7f
DW
7668 for (dl = super->disks; dl; dl = dl->next) {
7669 dl->raiddisk = -1;
7670
7671 if (dl->index >= 0)
7672 used++;
7673
7674 /* don't activate new spares if we are orom constrained
7675 * and there is already a volume active in the container
7676 */
7677 if (super->orom && dl->index < 0 && mpb->num_raid_devs)
7678 continue;
7679
05501181 7680 e = get_extents(super, dl, 0);
efb30e7f
DW
7681 if (!e)
7682 continue;
7683 for (i = 1; e[i-1].size; i++)
7684 ;
7685 dl->e = e;
7686 dl->extent_cnt = i;
efb30e7f
DW
7687 cnt++;
7688 }
7689
aa19fdd4 7690 maxsize = merge_extents(super, expanding);
5f027b93
MT
7691 if (maxsize < minsize) {
7692 pr_err("imsm: Free space is %llu but must be equal or larger than %llu.\n",
7693 maxsize, minsize);
7694 return IMSM_STATUS_ERROR;
7695 }
efb30e7f 7696
5f027b93
MT
7697 if (cnt < raiddisks || (super->orom && used && used != raiddisks)) {
7698 pr_err("imsm: Not enough devices with space to create array.\n");
6d4d9ab2 7699 return IMSM_STATUS_ERROR;
efb30e7f
DW
7700 }
7701
7702 if (size == 0) {
7703 size = maxsize;
7704 if (chunk) {
612e59d8
CA
7705 size /= 2 * chunk;
7706 size *= 2 * chunk;
efb30e7f 7707 }
f878b242
LM
7708 maxsize = size;
7709 }
1a1ced1e
KS
7710 if (mpb->num_raid_devs > 0 && size && size != maxsize)
7711 pr_err("attempting to create a second volume with size less then remaining space.\n");
efb30e7f
DW
7712 *freesize = size;
7713
13bcac90
AK
7714 dprintf("imsm: imsm_get_free_size() returns : %llu\n", size);
7715
6d4d9ab2 7716 return IMSM_STATUS_OK;
efb30e7f
DW
7717}
7718
6d4d9ab2
MT
7719/**
7720 * autolayout_imsm() - automatically layout a new volume.
7721 * @super: &intel_super pointer, not NULL.
7722 * @raiddisks: number of raid disks.
7723 * @size: requested size, could be 0 (means max size).
7724 * @chunk: requested chunk.
7725 * @freesize: pointer for returned size value.
7726 *
7727 * We are being asked to automatically layout a new volume based on the current
7728 * contents of the container. If the parameters can be satisfied autolayout_imsm
7729 * will record the disks, start offset, and will return size of the volume to
7730 * be created. See imsm_get_free_size() for details.
7731 * add_to_super() and getinfo_super() detect when autolayout is in progress.
7732 * If first volume exists, slots are set consistently to it.
7733 *
7734 * Return: &IMSM_STATUS_OK on success, &IMSM_STATUS_ERROR otherwise.
7735 *
7736 * Disks are marked for creation via dl->raiddisk.
7737 */
7738static imsm_status_t autolayout_imsm(struct intel_super *super,
7739 const int raiddisks,
7740 unsigned long long size, const int chunk,
7741 unsigned long long *freesize)
13bcac90 7742{
6d4d9ab2
MT
7743 int curr_slot = 0;
7744 struct dl *disk;
7745 int vol_cnt = super->anchor->num_raid_devs;
7746 imsm_status_t rv;
13bcac90 7747
aa19fdd4 7748 rv = imsm_get_free_size(super, raiddisks, size, chunk, freesize, false);
6d4d9ab2
MT
7749 if (rv != IMSM_STATUS_OK)
7750 return IMSM_STATUS_ERROR;
7751
7752 for (disk = super->disks; disk; disk = disk->next) {
7753 if (!disk->e)
7754 continue;
7755
7756 if (curr_slot == raiddisks)
7757 break;
7758
7759 if (vol_cnt == 0) {
7760 disk->raiddisk = curr_slot;
7761 } else {
7762 int _slot = get_disk_slot_in_dev(super, 0, disk->index);
7763
7764 if (_slot == -1) {
7765 pr_err("Disk %s is not used in first volume, aborting\n",
7766 disk->devname);
7767 return IMSM_STATUS_ERROR;
7768 }
7769 disk->raiddisk = _slot;
7770 }
7771 curr_slot++;
13bcac90
AK
7772 }
7773
6d4d9ab2 7774 return IMSM_STATUS_OK;
13bcac90
AK
7775}
7776
bf5a934a 7777static int validate_geometry_imsm(struct supertype *st, int level, int layout,
c21e737b 7778 int raiddisks, int *chunk, unsigned long long size,
af4348dd 7779 unsigned long long data_offset,
bf5a934a 7780 char *dev, unsigned long long *freesize,
5308f117 7781 int consistency_policy, int verbose)
bf5a934a
DW
7782{
7783 int fd, cfd;
7784 struct mdinfo *sra;
20cbe8d2 7785 int is_member = 0;
bf5a934a 7786
d54559f0
LM
7787 /* load capability
7788 * if given unused devices create a container
bf5a934a
DW
7789 * if given given devices in a container create a member volume
7790 */
6f2af6a4 7791 if (is_container(level))
bf5a934a 7792 /* Must be a fresh device to add to a container */
1f5d54a0
MT
7793 return validate_geometry_imsm_container(st, level, raiddisks,
7794 data_offset, dev,
7795 freesize, verbose);
9587c373 7796
06a6101c
BK
7797 /*
7798 * Size is given in sectors.
7799 */
7800 if (size && (size < 2048)) {
22dc741f 7801 pr_err("Given size must be greater than 1M.\n");
54865c30
RS
7802 /* Depends on algorithm in Create.c :
7803 * if container was given (dev == NULL) return -1,
7804 * if block device was given ( dev != NULL) return 0.
7805 */
7806 return dev ? -1 : 0;
7807 }
7808
8592f29d 7809 if (!dev) {
6d4d9ab2
MT
7810 struct intel_super *super = st->sb;
7811
7812 /*
071f839e 7813 * Autolayout mode, st->sb must be set.
6d4d9ab2 7814 */
071f839e
KT
7815 if (!super) {
7816 pr_vrb("superblock must be set for autolayout, aborting\n");
7817 return 0;
6d4d9ab2
MT
7818 }
7819
7820 if (!validate_geometry_imsm_orom(st->sb, level, layout,
7821 raiddisks, chunk, size,
7822 verbose))
7823 return 0;
7824
071f839e 7825 if (super->orom && freesize) {
6d4d9ab2
MT
7826 imsm_status_t rv;
7827 int count = count_volumes(super->hba, super->orom->dpa,
7828 verbose);
7829 if (super->orom->vphba <= count) {
7830 pr_vrb("platform does not support more than %d raid volumes.\n",
7831 super->orom->vphba);
e91a3bad 7832 return 0;
ca9de185 7833 }
6d4d9ab2
MT
7834
7835 rv = autolayout_imsm(super, raiddisks, size, *chunk,
7836 freesize);
7837 if (rv != IMSM_STATUS_OK)
7838 return 0;
8592f29d
N
7839 }
7840 return 1;
7841 }
bf5a934a
DW
7842 if (st->sb) {
7843 /* creating in a given container */
7844 return validate_geometry_imsm_volume(st, level, layout,
7845 raiddisks, chunk, size,
af4348dd 7846 data_offset,
bf5a934a
DW
7847 dev, freesize, verbose);
7848 }
7849
bf5a934a
DW
7850 /* This device needs to be a device in an 'imsm' container */
7851 fd = open(dev, O_RDONLY|O_EXCL, 0);
4389ce73
MT
7852
7853 if (is_fd_valid(fd)) {
7854 pr_vrb("Cannot create this array on device %s\n", dev);
bf5a934a
DW
7855 close(fd);
7856 return 0;
7857 }
4389ce73
MT
7858 if (errno == EBUSY)
7859 fd = open(dev, O_RDONLY, 0);
7860
7861 if (!is_fd_valid(fd)) {
7862 pr_vrb("Cannot open %s: %s\n", dev, strerror(errno));
bf5a934a
DW
7863 return 0;
7864 }
4389ce73 7865
bf5a934a
DW
7866 /* Well, it is in use by someone, maybe an 'imsm' container. */
7867 cfd = open_container(fd);
4389ce73
MT
7868 close_fd(&fd);
7869
7870 if (!is_fd_valid(cfd)) {
7871 pr_vrb("Cannot use %s: It is busy\n", dev);
bf5a934a
DW
7872 return 0;
7873 }
4dd2df09 7874 sra = sysfs_read(cfd, NULL, GET_VERSION);
bf5a934a 7875 if (sra && sra->array.major_version == -1 &&
20cbe8d2
AW
7876 strcmp(sra->text_version, "imsm") == 0)
7877 is_member = 1;
7878 sysfs_free(sra);
7879 if (is_member) {
bf5a934a
DW
7880 /* This is a member of a imsm container. Load the container
7881 * and try to create a volume
7882 */
7883 struct intel_super *super;
7884
ec50f7b6 7885 if (load_super_imsm_all(st, cfd, (void **) &super, NULL, NULL, 1) == 0) {
bf5a934a 7886 st->sb = super;
4dd2df09 7887 strcpy(st->container_devnm, fd2devnm(cfd));
bf5a934a
DW
7888 close(cfd);
7889 return validate_geometry_imsm_volume(st, level, layout,
7890 raiddisks, chunk,
af4348dd 7891 size, data_offset, dev,
ecbd9e81
N
7892 freesize, 1)
7893 ? 1 : -1;
bf5a934a 7894 }
20cbe8d2 7895 }
bf5a934a 7896
20cbe8d2 7897 if (verbose)
e7b84f9d 7898 pr_err("failed container membership check\n");
20cbe8d2
AW
7899
7900 close(cfd);
7901 return 0;
bf5a934a 7902}
0bd16cf2 7903
30f58b22 7904static void default_geometry_imsm(struct supertype *st, int *level, int *layout, int *chunk)
0bd16cf2
DJ
7905{
7906 struct intel_super *super = st->sb;
7907
30f58b22
DW
7908 if (level && *level == UnSet)
7909 *level = LEVEL_CONTAINER;
7910
7911 if (level && layout && *layout == UnSet)
7912 *layout = imsm_level_to_layout(*level);
0bd16cf2 7913
cd9d1ac7
DW
7914 if (chunk && (*chunk == UnSet || *chunk == 0))
7915 *chunk = imsm_default_chunk(super->orom);
0bd16cf2
DJ
7916}
7917
33414a01
DW
7918static void handle_missing(struct intel_super *super, struct imsm_dev *dev);
7919
3364781b 7920static int kill_subarray_imsm(struct supertype *st, char *subarray_id)
33414a01 7921{
3364781b 7922 /* remove the subarray currently referenced by subarray_id */
33414a01
DW
7923 __u8 i;
7924 struct intel_dev **dp;
7925 struct intel_super *super = st->sb;
3364781b 7926 __u8 current_vol = strtoul(subarray_id, NULL, 10);
33414a01
DW
7927 struct imsm_super *mpb = super->anchor;
7928
3364781b 7929 if (mpb->num_raid_devs == 0)
33414a01 7930 return 2;
33414a01
DW
7931
7932 /* block deletions that would change the uuid of active subarrays
7933 *
7934 * FIXME when immutable ids are available, but note that we'll
7935 * also need to fixup the invalidated/active subarray indexes in
7936 * mdstat
7937 */
7938 for (i = 0; i < mpb->num_raid_devs; i++) {
7939 char subarray[4];
7940
7941 if (i < current_vol)
7942 continue;
dd5ab402 7943 snprintf(subarray, sizeof(subarray), "%u", i);
4dd2df09 7944 if (is_subarray_active(subarray, st->devnm)) {
e7b84f9d
N
7945 pr_err("deleting subarray-%d would change the UUID of active subarray-%d, aborting\n",
7946 current_vol, i);
33414a01
DW
7947
7948 return 2;
7949 }
7950 }
7951
7952 if (st->update_tail) {
503975b9 7953 struct imsm_update_kill_array *u = xmalloc(sizeof(*u));
33414a01 7954
33414a01
DW
7955 u->type = update_kill_array;
7956 u->dev_idx = current_vol;
7957 append_metadata_update(st, u, sizeof(*u));
7958
7959 return 0;
7960 }
7961
7962 for (dp = &super->devlist; *dp;)
7963 if ((*dp)->index == current_vol) {
7964 *dp = (*dp)->next;
7965 } else {
7966 handle_missing(super, (*dp)->dev);
7967 if ((*dp)->index > current_vol)
7968 (*dp)->index--;
7969 dp = &(*dp)->next;
7970 }
7971
7972 /* no more raid devices, all active components are now spares,
7973 * but of course failed are still failed
7974 */
7975 if (--mpb->num_raid_devs == 0) {
7976 struct dl *d;
7977
7978 for (d = super->disks; d; d = d->next)
a8619d23
AK
7979 if (d->index > -2)
7980 mark_spare(d);
33414a01
DW
7981 }
7982
7983 super->updates_pending++;
7984
7985 return 0;
7986}
aa534678 7987
4345e135
MK
7988/**
7989 * get_rwh_policy_from_update() - Get the rwh policy for update option.
7990 * @update: Update option.
7991 */
7992static int get_rwh_policy_from_update(enum update_opt update)
19ad203e 7993{
4345e135
MK
7994 switch (update) {
7995 case UOPT_PPL:
19ad203e 7996 return RWH_MULTIPLE_DISTRIBUTED;
4345e135 7997 case UOPT_NO_PPL:
19ad203e 7998 return RWH_MULTIPLE_OFF;
4345e135 7999 case UOPT_BITMAP:
19ad203e 8000 return RWH_BITMAP;
4345e135 8001 case UOPT_NO_BITMAP:
19ad203e 8002 return RWH_OFF;
4345e135
MK
8003 default:
8004 break;
8005 }
8006 return UOPT_UNDEFINED;
19ad203e
JR
8007}
8008
a951a4f7 8009static int update_subarray_imsm(struct supertype *st, char *subarray,
03312b52 8010 enum update_opt update, struct mddev_ident *ident)
aa534678
DW
8011{
8012 /* update the subarray currently referenced by ->current_vol */
8013 struct intel_super *super = st->sb;
8014 struct imsm_super *mpb = super->anchor;
8015
03312b52 8016 if (update == UOPT_NAME) {
aa534678 8017 char *name = ident->name;
a951a4f7
N
8018 char *ep;
8019 int vol;
aa534678 8020
aa534678
DW
8021 if (!check_name(super, name, 0))
8022 return 2;
8023
a951a4f7
N
8024 vol = strtoul(subarray, &ep, 10);
8025 if (*ep != '\0' || vol >= super->anchor->num_raid_devs)
8026 return 2;
8027
aa534678 8028 if (st->update_tail) {
503975b9 8029 struct imsm_update_rename_array *u = xmalloc(sizeof(*u));
aa534678 8030
aa534678 8031 u->type = update_rename_array;
a951a4f7 8032 u->dev_idx = vol;
618f4e6d
XN
8033 strncpy((char *) u->name, name, MAX_RAID_SERIAL_LEN);
8034 u->name[MAX_RAID_SERIAL_LEN-1] = '\0';
aa534678
DW
8035 append_metadata_update(st, u, sizeof(*u));
8036 } else {
8037 struct imsm_dev *dev;
ebad3af2 8038 int i, namelen;
aa534678 8039
a951a4f7 8040 dev = get_imsm_dev(super, vol);
ebad3af2
JS
8041 memset(dev->volume, '\0', MAX_RAID_SERIAL_LEN);
8042 namelen = min((int)strlen(name), MAX_RAID_SERIAL_LEN);
8043 memcpy(dev->volume, name, namelen);
aa534678
DW
8044 for (i = 0; i < mpb->num_raid_devs; i++) {
8045 dev = get_imsm_dev(super, i);
8046 handle_missing(super, dev);
8047 }
8048 super->updates_pending++;
8049 }
03312b52 8050 } else if (get_rwh_policy_from_update(update) != UOPT_UNDEFINED) {
e6e9dd3f
AP
8051 int new_policy;
8052 char *ep;
8053 int vol = strtoul(subarray, &ep, 10);
8054
8055 if (*ep != '\0' || vol >= super->anchor->num_raid_devs)
8056 return 2;
8057
03312b52 8058 new_policy = get_rwh_policy_from_update(update);
e6e9dd3f
AP
8059
8060 if (st->update_tail) {
8061 struct imsm_update_rwh_policy *u = xmalloc(sizeof(*u));
8062
8063 u->type = update_rwh_policy;
8064 u->dev_idx = vol;
8065 u->new_policy = new_policy;
8066 append_metadata_update(st, u, sizeof(*u));
8067 } else {
8068 struct imsm_dev *dev;
8069
8070 dev = get_imsm_dev(super, vol);
8071 dev->rwh_policy = new_policy;
8072 super->updates_pending++;
8073 }
19ad203e
JR
8074 if (new_policy == RWH_BITMAP)
8075 return write_init_bitmap_imsm_vol(st, vol);
aa534678
DW
8076 } else
8077 return 2;
8078
8079 return 0;
8080}
bf5a934a 8081
195d1d76 8082static bool is_gen_migration(struct imsm_dev *dev)
28bce06f 8083{
195d1d76
PP
8084 if (dev && dev->vol.migr_state &&
8085 migr_type(dev) == MIGR_GEN_MIGR)
8086 return true;
28bce06f 8087
195d1d76 8088 return false;
28bce06f
AK
8089}
8090
1e5c6983
DW
8091static int is_rebuilding(struct imsm_dev *dev)
8092{
8093 struct imsm_map *migr_map;
8094
8095 if (!dev->vol.migr_state)
8096 return 0;
8097
8098 if (migr_type(dev) != MIGR_REBUILD)
8099 return 0;
8100
238c0a71 8101 migr_map = get_imsm_map(dev, MAP_1);
1e5c6983
DW
8102
8103 if (migr_map->map_state == IMSM_T_STATE_DEGRADED)
8104 return 1;
8105 else
8106 return 0;
8107}
8108
6ce1fbf1
AK
8109static int is_initializing(struct imsm_dev *dev)
8110{
8111 struct imsm_map *migr_map;
8112
8113 if (!dev->vol.migr_state)
8114 return 0;
8115
8116 if (migr_type(dev) != MIGR_INIT)
8117 return 0;
8118
238c0a71 8119 migr_map = get_imsm_map(dev, MAP_1);
6ce1fbf1
AK
8120
8121 if (migr_map->map_state == IMSM_T_STATE_UNINITIALIZED)
8122 return 1;
8123
8124 return 0;
6ce1fbf1
AK
8125}
8126
c47b0ff6
AK
8127static void update_recovery_start(struct intel_super *super,
8128 struct imsm_dev *dev,
8129 struct mdinfo *array)
1e5c6983
DW
8130{
8131 struct mdinfo *rebuild = NULL;
8132 struct mdinfo *d;
8133 __u32 units;
8134
8135 if (!is_rebuilding(dev))
8136 return;
8137
8138 /* Find the rebuild target, but punt on the dual rebuild case */
8139 for (d = array->devs; d; d = d->next)
8140 if (d->recovery_start == 0) {
8141 if (rebuild)
8142 return;
8143 rebuild = d;
8144 }
8145
4363fd80
DW
8146 if (!rebuild) {
8147 /* (?) none of the disks are marked with
8148 * IMSM_ORD_REBUILD, so assume they are missing and the
8149 * disk_ord_tbl was not correctly updated
8150 */
1ade5cc1 8151 dprintf("failed to locate out-of-sync disk\n");
4363fd80
DW
8152 return;
8153 }
8154
4036e7ee 8155 units = vol_curr_migr_unit(dev);
c47b0ff6 8156 rebuild->recovery_start = units * blocks_per_migr_unit(super, dev);
1e5c6983
DW
8157}
8158
276d77db 8159static int recover_backup_imsm(struct supertype *st, struct mdinfo *info);
1e5c6983 8160
00bbdbda 8161static struct mdinfo *container_content_imsm(struct supertype *st, char *subarray)
cdddbdbc 8162{
4f5bc454
DW
8163 /* Given a container loaded by load_super_imsm_all,
8164 * extract information about all the arrays into
8165 * an mdinfo tree.
00bbdbda 8166 * If 'subarray' is given, just extract info about that array.
4f5bc454
DW
8167 *
8168 * For each imsm_dev create an mdinfo, fill it in,
8169 * then look for matching devices in super->disks
8170 * and create appropriate device mdinfo.
8171 */
8172 struct intel_super *super = st->sb;
949c47a0 8173 struct imsm_super *mpb = super->anchor;
4f5bc454 8174 struct mdinfo *rest = NULL;
00bbdbda 8175 unsigned int i;
81219e70 8176 int sb_errors = 0;
abef11a3
AK
8177 struct dl *d;
8178 int spare_disks = 0;
b6180160 8179 int current_vol = super->current_vol;
cdddbdbc 8180
19482bcc
AK
8181 /* do not assemble arrays when not all attributes are supported */
8182 if (imsm_check_attributes(mpb->attributes) == 0) {
81219e70 8183 sb_errors = 1;
7a862a02 8184 pr_err("Unsupported attributes in IMSM metadata.Arrays activation is blocked.\n");
19482bcc
AK
8185 }
8186
abef11a3
AK
8187 /* count spare devices, not used in maps
8188 */
8189 for (d = super->disks; d; d = d->next)
8190 if (d->index == -1)
8191 spare_disks++;
8192
4f5bc454 8193 for (i = 0; i < mpb->num_raid_devs; i++) {
00bbdbda
N
8194 struct imsm_dev *dev;
8195 struct imsm_map *map;
86e3692b 8196 struct imsm_map *map2;
4f5bc454 8197 struct mdinfo *this;
a6482415 8198 int slot;
a6482415 8199 int chunk;
00bbdbda 8200 char *ep;
8b9cd157 8201 int level;
00bbdbda
N
8202
8203 if (subarray &&
8204 (i != strtoul(subarray, &ep, 10) || *ep != '\0'))
8205 continue;
8206
8207 dev = get_imsm_dev(super, i);
238c0a71
AK
8208 map = get_imsm_map(dev, MAP_0);
8209 map2 = get_imsm_map(dev, MAP_1);
8b9cd157 8210 level = get_imsm_raid_level(map);
4f5bc454 8211
1ce0101c
DW
8212 /* do not publish arrays that are in the middle of an
8213 * unsupported migration
8214 */
8215 if (dev->vol.migr_state &&
28bce06f 8216 (migr_type(dev) == MIGR_STATE_CHANGE)) {
7a862a02 8217 pr_err("cannot assemble volume '%.16s': unsupported migration in progress\n",
1ce0101c
DW
8218 dev->volume);
8219 continue;
8220 }
2db86302
LM
8221 /* do not publish arrays that are not support by controller's
8222 * OROM/EFI
8223 */
1ce0101c 8224
503975b9 8225 this = xmalloc(sizeof(*this));
4f5bc454 8226
301406c9 8227 super->current_vol = i;
a5d85af7 8228 getinfo_super_imsm_volume(st, this, NULL);
9894ec0d 8229 this->next = rest;
a6482415 8230 chunk = __le16_to_cpu(map->blocks_per_strip) >> 1;
81219e70
LM
8231 /* mdadm does not support all metadata features- set the bit in all arrays state */
8232 if (!validate_geometry_imsm_orom(super,
8b9cd157
MK
8233 level, /* RAID level */
8234 imsm_level_to_layout(level),
81219e70 8235 map->num_members, /* raid disks */
fcc2c9da 8236 &chunk, imsm_dev_size(dev),
81219e70 8237 1 /* verbose */)) {
7a862a02 8238 pr_err("IMSM RAID geometry validation failed. Array %s activation is blocked.\n",
81219e70
LM
8239 dev->volume);
8240 this->array.state |=
8241 (1<<MD_SB_BLOCK_CONTAINER_RESHAPE) |
8242 (1<<MD_SB_BLOCK_VOLUME);
8243 }
81219e70
LM
8244
8245 /* if array has bad blocks, set suitable bit in all arrays state */
8246 if (sb_errors)
8247 this->array.state |=
8248 (1<<MD_SB_BLOCK_CONTAINER_RESHAPE) |
8249 (1<<MD_SB_BLOCK_VOLUME);
8250
4f5bc454 8251 for (slot = 0 ; slot < map->num_members; slot++) {
1e5c6983 8252 unsigned long long recovery_start;
4f5bc454
DW
8253 struct mdinfo *info_d;
8254 struct dl *d;
8255 int idx;
9a1608e5 8256 int skip;
7eef0453 8257 __u32 ord;
8b9cd157 8258 int missing = 0;
4f5bc454 8259
9a1608e5 8260 skip = 0;
238c0a71
AK
8261 idx = get_imsm_disk_idx(dev, slot, MAP_0);
8262 ord = get_imsm_ord_tbl_ent(dev, slot, MAP_X);
4f5bc454
DW
8263 for (d = super->disks; d ; d = d->next)
8264 if (d->index == idx)
0fbd635c 8265 break;
4f5bc454 8266
1e5c6983 8267 recovery_start = MaxSector;
4f5bc454 8268 if (d == NULL)
9a1608e5 8269 skip = 1;
25ed7e59 8270 if (d && is_failed(&d->disk))
9a1608e5 8271 skip = 1;
8b9cd157 8272 if (!skip && (ord & IMSM_ORD_REBUILD))
1e5c6983 8273 recovery_start = 0;
1e93d0d1
BK
8274 if (!(ord & IMSM_ORD_REBUILD))
8275 this->array.working_disks++;
1011e834 8276 /*
9a1608e5 8277 * if we skip some disks the array will be assmebled degraded;
1e5c6983
DW
8278 * reset resync start to avoid a dirty-degraded
8279 * situation when performing the intial sync
9a1608e5 8280 */
8b9cd157
MK
8281 if (skip)
8282 missing++;
8283
8284 if (!(dev->vol.dirty & RAIDVOL_DIRTY)) {
8285 if ((!able_to_resync(level, missing) ||
8286 recovery_start == 0))
8287 this->resync_start = MaxSector;
8b9cd157
MK
8288 }
8289
9a1608e5
DW
8290 if (skip)
8291 continue;
4f5bc454 8292
503975b9 8293 info_d = xcalloc(1, sizeof(*info_d));
4f5bc454
DW
8294 info_d->next = this->devs;
8295 this->devs = info_d;
8296
4f5bc454
DW
8297 info_d->disk.number = d->index;
8298 info_d->disk.major = d->major;
8299 info_d->disk.minor = d->minor;
8300 info_d->disk.raid_disk = slot;
1e5c6983 8301 info_d->recovery_start = recovery_start;
86e3692b
AK
8302 if (map2) {
8303 if (slot < map2->num_members)
8304 info_d->disk.state = (1 << MD_DISK_ACTIVE);
04c3c514
AK
8305 else
8306 this->array.spare_disks++;
86e3692b
AK
8307 } else {
8308 if (slot < map->num_members)
8309 info_d->disk.state = (1 << MD_DISK_ACTIVE);
04c3c514
AK
8310 else
8311 this->array.spare_disks++;
86e3692b 8312 }
4f5bc454
DW
8313
8314 info_d->events = __le32_to_cpu(mpb->generation_num);
5551b113 8315 info_d->data_offset = pba_of_lba0(map);
44490938 8316 info_d->component_size = calc_component_size(map, dev);
06fb291a
PB
8317
8318 if (map->raid_level == 5) {
2432ce9b
AP
8319 info_d->ppl_sector = this->ppl_sector;
8320 info_d->ppl_size = this->ppl_size;
98e96bdb
AP
8321 if (this->consistency_policy == CONSISTENCY_POLICY_PPL &&
8322 recovery_start == 0)
8323 this->resync_start = 0;
06fb291a 8324 }
b12796be 8325
5e46202e 8326 info_d->bb.supported = 1;
b12796be
TM
8327 get_volume_badblocks(super->bbm_log, ord_to_idx(ord),
8328 info_d->data_offset,
8329 info_d->component_size,
8330 &info_d->bb);
4f5bc454 8331 }
1e5c6983 8332 /* now that the disk list is up-to-date fixup recovery_start */
c47b0ff6 8333 update_recovery_start(super, dev, this);
abef11a3 8334 this->array.spare_disks += spare_disks;
276d77db
AK
8335
8336 /* check for reshape */
8337 if (this->reshape_active == 1)
8338 recover_backup_imsm(st, this);
9a1608e5 8339 rest = this;
4f5bc454
DW
8340 }
8341
b6180160 8342 super->current_vol = current_vol;
4f5bc454 8343 return rest;
cdddbdbc
DW
8344}
8345
3b451610
AK
8346static __u8 imsm_check_degraded(struct intel_super *super, struct imsm_dev *dev,
8347 int failed, int look_in_map)
c2a1e7da 8348{
3b451610
AK
8349 struct imsm_map *map;
8350
8351 map = get_imsm_map(dev, look_in_map);
c2a1e7da
DW
8352
8353 if (!failed)
1011e834 8354 return map->map_state == IMSM_T_STATE_UNINITIALIZED ?
3393c6af 8355 IMSM_T_STATE_UNINITIALIZED : IMSM_T_STATE_NORMAL;
c2a1e7da
DW
8356
8357 switch (get_imsm_raid_level(map)) {
8358 case 0:
8359 return IMSM_T_STATE_FAILED;
8360 break;
8361 case 1:
8362 if (failed < map->num_members)
8363 return IMSM_T_STATE_DEGRADED;
8364 else
8365 return IMSM_T_STATE_FAILED;
8366 break;
8367 case 10:
8368 {
8369 /**
c92a2527
DW
8370 * check to see if any mirrors have failed, otherwise we
8371 * are degraded. Even numbered slots are mirrored on
8372 * slot+1
c2a1e7da 8373 */
c2a1e7da 8374 int i;
d9b420a5
N
8375 /* gcc -Os complains that this is unused */
8376 int insync = insync;
c2a1e7da
DW
8377
8378 for (i = 0; i < map->num_members; i++) {
238c0a71 8379 __u32 ord = get_imsm_ord_tbl_ent(dev, i, MAP_X);
c92a2527
DW
8380 int idx = ord_to_idx(ord);
8381 struct imsm_disk *disk;
c2a1e7da 8382
c92a2527 8383 /* reset the potential in-sync count on even-numbered
1011e834 8384 * slots. num_copies is always 2 for imsm raid10
c92a2527
DW
8385 */
8386 if ((i & 1) == 0)
8387 insync = 2;
c2a1e7da 8388
c92a2527 8389 disk = get_imsm_disk(super, idx);
25ed7e59 8390 if (!disk || is_failed(disk) || ord & IMSM_ORD_REBUILD)
c92a2527 8391 insync--;
c2a1e7da 8392
c92a2527
DW
8393 /* no in-sync disks left in this mirror the
8394 * array has failed
8395 */
8396 if (insync == 0)
8397 return IMSM_T_STATE_FAILED;
c2a1e7da
DW
8398 }
8399
8400 return IMSM_T_STATE_DEGRADED;
8401 }
8402 case 5:
8403 if (failed < 2)
8404 return IMSM_T_STATE_DEGRADED;
8405 else
8406 return IMSM_T_STATE_FAILED;
8407 break;
8408 default:
8409 break;
8410 }
8411
8412 return map->map_state;
8413}
8414
3b451610
AK
8415static int imsm_count_failed(struct intel_super *super, struct imsm_dev *dev,
8416 int look_in_map)
c2a1e7da
DW
8417{
8418 int i;
8419 int failed = 0;
8420 struct imsm_disk *disk;
d5985138
AK
8421 struct imsm_map *map = get_imsm_map(dev, MAP_0);
8422 struct imsm_map *prev = get_imsm_map(dev, MAP_1);
68fe4598 8423 struct imsm_map *map_for_loop;
0556e1a2
DW
8424 __u32 ord;
8425 int idx;
d5985138 8426 int idx_1;
c2a1e7da 8427
0556e1a2
DW
8428 /* at the beginning of migration we set IMSM_ORD_REBUILD on
8429 * disks that are being rebuilt. New failures are recorded to
8430 * map[0]. So we look through all the disks we started with and
8431 * see if any failures are still present, or if any new ones
8432 * have arrived
0556e1a2 8433 */
d5985138
AK
8434 map_for_loop = map;
8435 if (prev && (map->num_members < prev->num_members))
8436 map_for_loop = prev;
68fe4598
LD
8437
8438 for (i = 0; i < map_for_loop->num_members; i++) {
d5985138 8439 idx_1 = -255;
238c0a71
AK
8440 /* when MAP_X is passed both maps failures are counted
8441 */
d5985138 8442 if (prev &&
089f9d79
JS
8443 (look_in_map == MAP_1 || look_in_map == MAP_X) &&
8444 i < prev->num_members) {
d5985138
AK
8445 ord = __le32_to_cpu(prev->disk_ord_tbl[i]);
8446 idx_1 = ord_to_idx(ord);
c2a1e7da 8447
d5985138
AK
8448 disk = get_imsm_disk(super, idx_1);
8449 if (!disk || is_failed(disk) || ord & IMSM_ORD_REBUILD)
8450 failed++;
8451 }
089f9d79
JS
8452 if ((look_in_map == MAP_0 || look_in_map == MAP_X) &&
8453 i < map->num_members) {
d5985138
AK
8454 ord = __le32_to_cpu(map->disk_ord_tbl[i]);
8455 idx = ord_to_idx(ord);
8456
8457 if (idx != idx_1) {
8458 disk = get_imsm_disk(super, idx);
8459 if (!disk || is_failed(disk) ||
8460 ord & IMSM_ORD_REBUILD)
8461 failed++;
8462 }
8463 }
c2a1e7da
DW
8464 }
8465
8466 return failed;
845dea95
NB
8467}
8468
97b4d0e9 8469static int imsm_open_new(struct supertype *c, struct active_array *a,
60815698 8470 int inst)
97b4d0e9
DW
8471{
8472 struct intel_super *super = c->sb;
8473 struct imsm_super *mpb = super->anchor;
bbab0940 8474 struct imsm_update_prealloc_bb_mem u;
9587c373 8475
60815698
MG
8476 if (inst >= mpb->num_raid_devs) {
8477 pr_err("subarry index %d, out of range\n", inst);
97b4d0e9
DW
8478 return -ENODEV;
8479 }
8480
60815698
MG
8481 dprintf("imsm: open_new %d\n", inst);
8482 a->info.container_member = inst;
bbab0940
TM
8483
8484 u.type = update_prealloc_badblocks_mem;
8485 imsm_update_metadata_locally(c, &u, sizeof(u));
8486
97b4d0e9
DW
8487 return 0;
8488}
8489
0c046afd
DW
8490static int is_resyncing(struct imsm_dev *dev)
8491{
8492 struct imsm_map *migr_map;
8493
8494 if (!dev->vol.migr_state)
8495 return 0;
8496
1484e727
DW
8497 if (migr_type(dev) == MIGR_INIT ||
8498 migr_type(dev) == MIGR_REPAIR)
0c046afd
DW
8499 return 1;
8500
4c9bc37b
AK
8501 if (migr_type(dev) == MIGR_GEN_MIGR)
8502 return 0;
8503
238c0a71 8504 migr_map = get_imsm_map(dev, MAP_1);
0c046afd 8505
089f9d79
JS
8506 if (migr_map->map_state == IMSM_T_STATE_NORMAL &&
8507 dev->vol.migr_type != MIGR_GEN_MIGR)
0c046afd
DW
8508 return 1;
8509 else
8510 return 0;
8511}
8512
0556e1a2 8513/* return true if we recorded new information */
4c9e8c1e
TM
8514static int mark_failure(struct intel_super *super,
8515 struct imsm_dev *dev, struct imsm_disk *disk, int idx)
47ee5a45 8516{
0556e1a2
DW
8517 __u32 ord;
8518 int slot;
8519 struct imsm_map *map;
86c54047
DW
8520 char buf[MAX_RAID_SERIAL_LEN+3];
8521 unsigned int len, shift = 0;
0556e1a2
DW
8522
8523 /* new failures are always set in map[0] */
238c0a71 8524 map = get_imsm_map(dev, MAP_0);
0556e1a2
DW
8525
8526 slot = get_imsm_disk_slot(map, idx);
8527 if (slot < 0)
8528 return 0;
8529
8530 ord = __le32_to_cpu(map->disk_ord_tbl[slot]);
25ed7e59 8531 if (is_failed(disk) && (ord & IMSM_ORD_REBUILD))
0556e1a2
DW
8532 return 0;
8533
7d0c5e24
LD
8534 memcpy(buf, disk->serial, MAX_RAID_SERIAL_LEN);
8535 buf[MAX_RAID_SERIAL_LEN] = '\000';
8536 strcat(buf, ":0");
86c54047
DW
8537 if ((len = strlen(buf)) >= MAX_RAID_SERIAL_LEN)
8538 shift = len - MAX_RAID_SERIAL_LEN + 1;
167d8bb8 8539 memcpy(disk->serial, &buf[shift], len + 1 - shift);
86c54047 8540
f2f27e63 8541 disk->status |= FAILED_DISK;
0556e1a2 8542 set_imsm_ord_tbl_ent(map, slot, idx | IMSM_ORD_REBUILD);
17788994
AK
8543 /* mark failures in second map if second map exists and this disk
8544 * in this slot.
8545 * This is valid for migration, initialization and rebuild
8546 */
8547 if (dev->vol.migr_state) {
238c0a71 8548 struct imsm_map *map2 = get_imsm_map(dev, MAP_1);
0a108d63
AK
8549 int slot2 = get_imsm_disk_slot(map2, idx);
8550
089f9d79 8551 if (slot2 < map2->num_members && slot2 >= 0)
0a108d63 8552 set_imsm_ord_tbl_ent(map2, slot2,
1ace8403
AK
8553 idx | IMSM_ORD_REBUILD);
8554 }
d7a1fda2
MT
8555 if (map->failed_disk_num == 0xff ||
8556 (!is_rebuilding(dev) && map->failed_disk_num > slot))
0556e1a2 8557 map->failed_disk_num = slot;
4c9e8c1e
TM
8558
8559 clear_disk_badblocks(super->bbm_log, ord_to_idx(ord));
8560
0556e1a2
DW
8561 return 1;
8562}
8563
4c9e8c1e
TM
8564static void mark_missing(struct intel_super *super,
8565 struct imsm_dev *dev, struct imsm_disk *disk, int idx)
0556e1a2 8566{
4c9e8c1e 8567 mark_failure(super, dev, disk, idx);
0556e1a2
DW
8568
8569 if (disk->scsi_id == __cpu_to_le32(~(__u32)0))
8570 return;
8571
47ee5a45
DW
8572 disk->scsi_id = __cpu_to_le32(~(__u32)0);
8573 memmove(&disk->serial[0], &disk->serial[1], MAX_RAID_SERIAL_LEN - 1);
8574}
8575
33414a01
DW
8576static void handle_missing(struct intel_super *super, struct imsm_dev *dev)
8577{
33414a01 8578 struct dl *dl;
33414a01
DW
8579
8580 if (!super->missing)
8581 return;
33414a01 8582
79b68f1b
PC
8583 /* When orom adds replacement for missing disk it does
8584 * not remove entry of missing disk, but just updates map with
8585 * new added disk. So it is not enough just to test if there is
8586 * any missing disk, we have to look if there are any failed disks
8587 * in map to stop migration */
8588
33414a01 8589 dprintf("imsm: mark missing\n");
3d59f0c0
AK
8590 /* end process for initialization and rebuild only
8591 */
195d1d76 8592 if (is_gen_migration(dev) == false) {
fb12a745 8593 int failed = imsm_count_failed(super, dev, MAP_0);
3d59f0c0 8594
fb12a745
TM
8595 if (failed) {
8596 __u8 map_state;
8597 struct imsm_map *map = get_imsm_map(dev, MAP_0);
8598 struct imsm_map *map1;
8599 int i, ord, ord_map1;
8600 int rebuilt = 1;
3d59f0c0 8601
fb12a745
TM
8602 for (i = 0; i < map->num_members; i++) {
8603 ord = get_imsm_ord_tbl_ent(dev, i, MAP_0);
8604 if (!(ord & IMSM_ORD_REBUILD))
8605 continue;
8606
8607 map1 = get_imsm_map(dev, MAP_1);
8608 if (!map1)
8609 continue;
8610
8611 ord_map1 = __le32_to_cpu(map1->disk_ord_tbl[i]);
8612 if (ord_map1 & IMSM_ORD_REBUILD)
8613 rebuilt = 0;
8614 }
8615
8616 if (rebuilt) {
8617 map_state = imsm_check_degraded(super, dev,
8618 failed, MAP_0);
8619 end_migration(dev, super, map_state);
8620 }
8621 }
3d59f0c0 8622 }
33414a01 8623 for (dl = super->missing; dl; dl = dl->next)
4c9e8c1e 8624 mark_missing(super, dev, &dl->disk, dl->index);
33414a01
DW
8625 super->updates_pending++;
8626}
8627
f3871fdc
AK
8628static unsigned long long imsm_set_array_size(struct imsm_dev *dev,
8629 long long new_size)
70bdf0dc 8630{
70bdf0dc 8631 unsigned long long array_blocks;
9529d343
MD
8632 struct imsm_map *map = get_imsm_map(dev, MAP_0);
8633 int used_disks = imsm_num_data_members(map);
70bdf0dc
AK
8634
8635 if (used_disks == 0) {
8636 /* when problems occures
8637 * return current array_blocks value
8638 */
fcc2c9da 8639 array_blocks = imsm_dev_size(dev);
70bdf0dc
AK
8640
8641 return array_blocks;
8642 }
8643
8644 /* set array size in metadata
8645 */
9529d343 8646 if (new_size <= 0)
f3871fdc
AK
8647 /* OLCE size change is caused by added disks
8648 */
44490938 8649 array_blocks = per_dev_array_size(map) * used_disks;
9529d343 8650 else
f3871fdc
AK
8651 /* Online Volume Size Change
8652 * Using available free space
8653 */
8654 array_blocks = new_size;
70bdf0dc 8655
b53bfba6 8656 array_blocks = round_size_to_mb(array_blocks, used_disks);
fcc2c9da 8657 set_imsm_dev_size(dev, array_blocks);
70bdf0dc
AK
8658
8659 return array_blocks;
8660}
8661
28bce06f
AK
8662static void imsm_set_disk(struct active_array *a, int n, int state);
8663
0e2d1a4e
AK
8664static void imsm_progress_container_reshape(struct intel_super *super)
8665{
8666 /* if no device has a migr_state, but some device has a
8667 * different number of members than the previous device, start
8668 * changing the number of devices in this device to match
8669 * previous.
8670 */
8671 struct imsm_super *mpb = super->anchor;
8672 int prev_disks = -1;
8673 int i;
1dfaa380 8674 int copy_map_size;
0e2d1a4e
AK
8675
8676 for (i = 0; i < mpb->num_raid_devs; i++) {
8677 struct imsm_dev *dev = get_imsm_dev(super, i);
238c0a71 8678 struct imsm_map *map = get_imsm_map(dev, MAP_0);
0e2d1a4e
AK
8679 struct imsm_map *map2;
8680 int prev_num_members;
0e2d1a4e
AK
8681
8682 if (dev->vol.migr_state)
8683 return;
8684
8685 if (prev_disks == -1)
8686 prev_disks = map->num_members;
8687 if (prev_disks == map->num_members)
8688 continue;
8689
8690 /* OK, this array needs to enter reshape mode.
8691 * i.e it needs a migr_state
8692 */
8693
1dfaa380 8694 copy_map_size = sizeof_imsm_map(map);
0e2d1a4e
AK
8695 prev_num_members = map->num_members;
8696 map->num_members = prev_disks;
8697 dev->vol.migr_state = 1;
4036e7ee 8698 set_vol_curr_migr_unit(dev, 0);
ea672ee1 8699 set_migr_type(dev, MIGR_GEN_MIGR);
0e2d1a4e
AK
8700 for (i = prev_num_members;
8701 i < map->num_members; i++)
8702 set_imsm_ord_tbl_ent(map, i, i);
238c0a71 8703 map2 = get_imsm_map(dev, MAP_1);
0e2d1a4e 8704 /* Copy the current map */
1dfaa380 8705 memcpy(map2, map, copy_map_size);
0e2d1a4e
AK
8706 map2->num_members = prev_num_members;
8707
f3871fdc 8708 imsm_set_array_size(dev, -1);
51d83f5d 8709 super->clean_migration_record_by_mdmon = 1;
0e2d1a4e
AK
8710 super->updates_pending++;
8711 }
8712}
8713
aad6f216 8714/* Handle dirty -> clean transititions, resync and reshape. Degraded and rebuild
0c046afd
DW
8715 * states are handled in imsm_set_disk() with one exception, when a
8716 * resync is stopped due to a new failure this routine will set the
8717 * 'degraded' state for the array.
8718 */
01f157d7 8719static int imsm_set_array_state(struct active_array *a, int consistent)
a862209d
DW
8720{
8721 int inst = a->info.container_member;
8722 struct intel_super *super = a->container->sb;
949c47a0 8723 struct imsm_dev *dev = get_imsm_dev(super, inst);
238c0a71 8724 struct imsm_map *map = get_imsm_map(dev, MAP_0);
3b451610
AK
8725 int failed = imsm_count_failed(super, dev, MAP_0);
8726 __u8 map_state = imsm_check_degraded(super, dev, failed, MAP_0);
1e5c6983 8727 __u32 blocks_per_unit;
a862209d 8728
1af97990
AK
8729 if (dev->vol.migr_state &&
8730 dev->vol.migr_type == MIGR_GEN_MIGR) {
8731 /* array state change is blocked due to reshape action
aad6f216
N
8732 * We might need to
8733 * - abort the reshape (if last_checkpoint is 0 and action!= reshape)
8734 * - finish the reshape (if last_checkpoint is big and action != reshape)
4036e7ee 8735 * - update vol_curr_migr_unit
1af97990 8736 */
aad6f216 8737 if (a->curr_action == reshape) {
4036e7ee 8738 /* still reshaping, maybe update vol_curr_migr_unit */
633b5610 8739 goto mark_checkpoint;
aad6f216
N
8740 } else {
8741 if (a->last_checkpoint == 0 && a->prev_action == reshape) {
8742 /* for some reason we aborted the reshape.
b66e591b
AK
8743 *
8744 * disable automatic metadata rollback
8745 * user action is required to recover process
aad6f216 8746 */
b66e591b 8747 if (0) {
238c0a71
AK
8748 struct imsm_map *map2 =
8749 get_imsm_map(dev, MAP_1);
8750 dev->vol.migr_state = 0;
8751 set_migr_type(dev, 0);
4036e7ee 8752 set_vol_curr_migr_unit(dev, 0);
238c0a71
AK
8753 memcpy(map, map2,
8754 sizeof_imsm_map(map2));
8755 super->updates_pending++;
b66e591b 8756 }
aad6f216
N
8757 }
8758 if (a->last_checkpoint >= a->info.component_size) {
8759 unsigned long long array_blocks;
8760 int used_disks;
e154ced3 8761 struct mdinfo *mdi;
aad6f216 8762
9529d343 8763 used_disks = imsm_num_data_members(map);
d55adef9
AK
8764 if (used_disks > 0) {
8765 array_blocks =
44490938 8766 per_dev_array_size(map) *
d55adef9 8767 used_disks;
b53bfba6
TM
8768 array_blocks =
8769 round_size_to_mb(array_blocks,
8770 used_disks);
d55adef9
AK
8771 a->info.custom_array_size = array_blocks;
8772 /* encourage manager to update array
8773 * size
8774 */
e154ced3 8775
d55adef9 8776 a->check_reshape = 1;
633b5610 8777 }
e154ced3
AK
8778 /* finalize online capacity expansion/reshape */
8779 for (mdi = a->info.devs; mdi; mdi = mdi->next)
8780 imsm_set_disk(a,
8781 mdi->disk.raid_disk,
8782 mdi->curr_state);
8783
0e2d1a4e 8784 imsm_progress_container_reshape(super);
e154ced3 8785 }
aad6f216 8786 }
1af97990
AK
8787 }
8788
47ee5a45 8789 /* before we activate this array handle any missing disks */
33414a01
DW
8790 if (consistent == 2)
8791 handle_missing(super, dev);
1e5c6983 8792
0c046afd 8793 if (consistent == 2 &&
b7941fd6 8794 (!is_resync_complete(&a->info) ||
0c046afd
DW
8795 map_state != IMSM_T_STATE_NORMAL ||
8796 dev->vol.migr_state))
01f157d7 8797 consistent = 0;
272906ef 8798
b7941fd6 8799 if (is_resync_complete(&a->info)) {
0c046afd 8800 /* complete intialization / resync,
0556e1a2
DW
8801 * recovery and interrupted recovery is completed in
8802 * ->set_disk
0c046afd
DW
8803 */
8804 if (is_resyncing(dev)) {
8805 dprintf("imsm: mark resync done\n");
809da78e 8806 end_migration(dev, super, map_state);
115c3803 8807 super->updates_pending++;
484240d8 8808 a->last_checkpoint = 0;
115c3803 8809 }
b9172665
AK
8810 } else if ((!is_resyncing(dev) && !failed) &&
8811 (imsm_reshape_blocks_arrays_changes(super) == 0)) {
0c046afd 8812 /* mark the start of the init process if nothing is failed */
b7941fd6 8813 dprintf("imsm: mark resync start\n");
1484e727 8814 if (map->map_state == IMSM_T_STATE_UNINITIALIZED)
8e59f3d8 8815 migrate(dev, super, IMSM_T_STATE_NORMAL, MIGR_INIT);
1484e727 8816 else
8e59f3d8 8817 migrate(dev, super, IMSM_T_STATE_NORMAL, MIGR_REPAIR);
3393c6af 8818 super->updates_pending++;
115c3803 8819 }
a862209d 8820
633b5610 8821mark_checkpoint:
5b83bacf
AK
8822 /* skip checkpointing for general migration,
8823 * it is controlled in mdadm
8824 */
8825 if (is_gen_migration(dev))
8826 goto skip_mark_checkpoint;
8827
4036e7ee
MT
8828 /* check if we can update vol_curr_migr_unit from resync_start,
8829 * recovery_start
8830 */
c47b0ff6 8831 blocks_per_unit = blocks_per_migr_unit(super, dev);
4f0a7acc 8832 if (blocks_per_unit) {
4036e7ee
MT
8833 set_vol_curr_migr_unit(dev,
8834 a->last_checkpoint / blocks_per_unit);
8835 dprintf("imsm: mark checkpoint (%llu)\n",
8836 vol_curr_migr_unit(dev));
8837 super->updates_pending++;
1e5c6983 8838 }
f8f603f1 8839
5b83bacf 8840skip_mark_checkpoint:
3393c6af 8841 /* mark dirty / clean */
2432ce9b
AP
8842 if (((dev->vol.dirty & RAIDVOL_DIRTY) && consistent) ||
8843 (!(dev->vol.dirty & RAIDVOL_DIRTY) && !consistent)) {
b7941fd6 8844 dprintf("imsm: mark '%s'\n", consistent ? "clean" : "dirty");
2432ce9b
AP
8845 if (consistent) {
8846 dev->vol.dirty = RAIDVOL_CLEAN;
8847 } else {
8848 dev->vol.dirty = RAIDVOL_DIRTY;
c2462068
PB
8849 if (dev->rwh_policy == RWH_DISTRIBUTED ||
8850 dev->rwh_policy == RWH_MULTIPLE_DISTRIBUTED)
2432ce9b
AP
8851 dev->vol.dirty |= RAIDVOL_DSRECORD_VALID;
8852 }
a862209d
DW
8853 super->updates_pending++;
8854 }
28bce06f 8855
01f157d7 8856 return consistent;
a862209d
DW
8857}
8858
6f50473f
TM
8859static int imsm_disk_slot_to_ord(struct active_array *a, int slot)
8860{
8861 int inst = a->info.container_member;
8862 struct intel_super *super = a->container->sb;
8863 struct imsm_dev *dev = get_imsm_dev(super, inst);
8864 struct imsm_map *map = get_imsm_map(dev, MAP_0);
8865
8866 if (slot > map->num_members) {
8867 pr_err("imsm: imsm_disk_slot_to_ord %d out of range 0..%d\n",
8868 slot, map->num_members - 1);
8869 return -1;
8870 }
8871
8872 if (slot < 0)
8873 return -1;
8874
8875 return get_imsm_ord_tbl_ent(dev, slot, MAP_0);
8876}
8877
8d45d196 8878static void imsm_set_disk(struct active_array *a, int n, int state)
845dea95 8879{
8d45d196
DW
8880 int inst = a->info.container_member;
8881 struct intel_super *super = a->container->sb;
949c47a0 8882 struct imsm_dev *dev = get_imsm_dev(super, inst);
238c0a71 8883 struct imsm_map *map = get_imsm_map(dev, MAP_0);
8d45d196 8884 struct imsm_disk *disk;
7ce05701
LD
8885 struct mdinfo *mdi;
8886 int recovery_not_finished = 0;
0c046afd 8887 int failed;
6f50473f 8888 int ord;
0c046afd 8889 __u8 map_state;
fb12a745
TM
8890 int rebuild_done = 0;
8891 int i;
8d45d196 8892
fb12a745 8893 ord = get_imsm_ord_tbl_ent(dev, n, MAP_X);
6f50473f 8894 if (ord < 0)
8d45d196
DW
8895 return;
8896
4e6e574a 8897 dprintf("imsm: set_disk %d:%x\n", n, state);
b10b37b8 8898 disk = get_imsm_disk(super, ord_to_idx(ord));
8d45d196 8899
5802a811 8900 /* check for new failures */
ae7d61e3 8901 if (disk && (state & DS_FAULTY)) {
4c9e8c1e 8902 if (mark_failure(super, dev, disk, ord_to_idx(ord)))
0556e1a2 8903 super->updates_pending++;
8d45d196 8904 }
47ee5a45 8905
19859edc 8906 /* check if in_sync */
0556e1a2 8907 if (state & DS_INSYNC && ord & IMSM_ORD_REBUILD && is_rebuilding(dev)) {
238c0a71 8908 struct imsm_map *migr_map = get_imsm_map(dev, MAP_1);
b10b37b8
DW
8909
8910 set_imsm_ord_tbl_ent(migr_map, n, ord_to_idx(ord));
fb12a745 8911 rebuild_done = 1;
19859edc
DW
8912 super->updates_pending++;
8913 }
8d45d196 8914
3b451610
AK
8915 failed = imsm_count_failed(super, dev, MAP_0);
8916 map_state = imsm_check_degraded(super, dev, failed, MAP_0);
5802a811 8917
0c046afd 8918 /* check if recovery complete, newly degraded, or failed */
94002678
AK
8919 dprintf("imsm: Detected transition to state ");
8920 switch (map_state) {
8921 case IMSM_T_STATE_NORMAL: /* transition to normal state */
8922 dprintf("normal: ");
8923 if (is_rebuilding(dev)) {
1ade5cc1 8924 dprintf_cont("while rebuilding");
7ce05701
LD
8925 /* check if recovery is really finished */
8926 for (mdi = a->info.devs; mdi ; mdi = mdi->next)
8927 if (mdi->recovery_start != MaxSector) {
8928 recovery_not_finished = 1;
8929 break;
8930 }
8931 if (recovery_not_finished) {
1ade5cc1
N
8932 dprintf_cont("\n");
8933 dprintf("Rebuild has not finished yet, state not changed");
7ce05701
LD
8934 if (a->last_checkpoint < mdi->recovery_start) {
8935 a->last_checkpoint = mdi->recovery_start;
8936 super->updates_pending++;
8937 }
8938 break;
8939 }
94002678 8940 end_migration(dev, super, map_state);
94002678
AK
8941 map->failed_disk_num = ~0;
8942 super->updates_pending++;
8943 a->last_checkpoint = 0;
8944 break;
8945 }
8946 if (is_gen_migration(dev)) {
1ade5cc1 8947 dprintf_cont("while general migration");
bf2f0071 8948 if (a->last_checkpoint >= a->info.component_size)
809da78e 8949 end_migration(dev, super, map_state);
94002678
AK
8950 else
8951 map->map_state = map_state;
28bce06f 8952 map->failed_disk_num = ~0;
94002678 8953 super->updates_pending++;
bf2f0071 8954 break;
94002678
AK
8955 }
8956 break;
8957 case IMSM_T_STATE_DEGRADED: /* transition to degraded state */
1ade5cc1 8958 dprintf_cont("degraded: ");
089f9d79 8959 if (map->map_state != map_state && !dev->vol.migr_state) {
1ade5cc1 8960 dprintf_cont("mark degraded");
94002678
AK
8961 map->map_state = map_state;
8962 super->updates_pending++;
8963 a->last_checkpoint = 0;
8964 break;
8965 }
8966 if (is_rebuilding(dev)) {
d7a1fda2 8967 dprintf_cont("while rebuilding ");
a4e96fd8
MT
8968 if (state & DS_FAULTY) {
8969 dprintf_cont("removing failed drive ");
d7a1fda2
MT
8970 if (n == map->failed_disk_num) {
8971 dprintf_cont("end migration");
8972 end_migration(dev, super, map_state);
a4e96fd8 8973 a->last_checkpoint = 0;
d7a1fda2 8974 } else {
a4e96fd8 8975 dprintf_cont("fail detected during rebuild, changing map state");
d7a1fda2
MT
8976 map->map_state = map_state;
8977 }
94002678 8978 super->updates_pending++;
fb12a745
TM
8979 }
8980
a4e96fd8
MT
8981 if (!rebuild_done)
8982 break;
8983
fb12a745
TM
8984 /* check if recovery is really finished */
8985 for (mdi = a->info.devs; mdi ; mdi = mdi->next)
8986 if (mdi->recovery_start != MaxSector) {
8987 recovery_not_finished = 1;
8988 break;
8989 }
8990 if (recovery_not_finished) {
8991 dprintf_cont("\n");
a4e96fd8 8992 dprintf_cont("Rebuild has not finished yet");
fb12a745
TM
8993 if (a->last_checkpoint < mdi->recovery_start) {
8994 a->last_checkpoint =
8995 mdi->recovery_start;
8996 super->updates_pending++;
8997 }
8998 break;
94002678 8999 }
fb12a745
TM
9000
9001 dprintf_cont(" Rebuild done, still degraded");
a4e96fd8
MT
9002 end_migration(dev, super, map_state);
9003 a->last_checkpoint = 0;
9004 super->updates_pending++;
fb12a745
TM
9005
9006 for (i = 0; i < map->num_members; i++) {
9007 int idx = get_imsm_ord_tbl_ent(dev, i, MAP_0);
9008
9009 if (idx & IMSM_ORD_REBUILD)
9010 map->failed_disk_num = i;
9011 }
9012 super->updates_pending++;
94002678
AK
9013 break;
9014 }
9015 if (is_gen_migration(dev)) {
1ade5cc1 9016 dprintf_cont("while general migration");
bf2f0071 9017 if (a->last_checkpoint >= a->info.component_size)
809da78e 9018 end_migration(dev, super, map_state);
94002678
AK
9019 else {
9020 map->map_state = map_state;
3b451610 9021 manage_second_map(super, dev);
94002678
AK
9022 }
9023 super->updates_pending++;
bf2f0071 9024 break;
28bce06f 9025 }
6ce1fbf1 9026 if (is_initializing(dev)) {
1ade5cc1 9027 dprintf_cont("while initialization.");
6ce1fbf1
AK
9028 map->map_state = map_state;
9029 super->updates_pending++;
9030 break;
9031 }
94002678
AK
9032 break;
9033 case IMSM_T_STATE_FAILED: /* transition to failed state */
1ade5cc1 9034 dprintf_cont("failed: ");
94002678 9035 if (is_gen_migration(dev)) {
1ade5cc1 9036 dprintf_cont("while general migration");
94002678
AK
9037 map->map_state = map_state;
9038 super->updates_pending++;
9039 break;
9040 }
9041 if (map->map_state != map_state) {
1ade5cc1 9042 dprintf_cont("mark failed");
94002678
AK
9043 end_migration(dev, super, map_state);
9044 super->updates_pending++;
9045 a->last_checkpoint = 0;
9046 break;
9047 }
9048 break;
9049 default:
1ade5cc1 9050 dprintf_cont("state %i\n", map_state);
5802a811 9051 }
1ade5cc1 9052 dprintf_cont("\n");
845dea95
NB
9053}
9054
f796af5d 9055static int store_imsm_mpb(int fd, struct imsm_super *mpb)
c2a1e7da 9056{
f796af5d 9057 void *buf = mpb;
c2a1e7da
DW
9058 __u32 mpb_size = __le32_to_cpu(mpb->mpb_size);
9059 unsigned long long dsize;
9060 unsigned long long sectors;
f36a9ecd 9061 unsigned int sector_size;
c2a1e7da 9062
aec01630
JS
9063 if (!get_dev_sector_size(fd, NULL, &sector_size))
9064 return 1;
c2a1e7da
DW
9065 get_dev_size(fd, NULL, &dsize);
9066
f36a9ecd 9067 if (mpb_size > sector_size) {
272f648f 9068 /* -1 to account for anchor */
f36a9ecd 9069 sectors = mpb_sectors(mpb, sector_size) - 1;
c2a1e7da 9070
272f648f 9071 /* write the extended mpb to the sectors preceeding the anchor */
f36a9ecd
PB
9072 if (lseek64(fd, dsize - (sector_size * (2 + sectors)),
9073 SEEK_SET) < 0)
272f648f 9074 return 1;
c2a1e7da 9075
f36a9ecd
PB
9076 if ((unsigned long long)write(fd, buf + sector_size,
9077 sector_size * sectors) != sector_size * sectors)
272f648f
DW
9078 return 1;
9079 }
c2a1e7da 9080
272f648f 9081 /* first block is stored on second to last sector of the disk */
f36a9ecd 9082 if (lseek64(fd, dsize - (sector_size * 2), SEEK_SET) < 0)
c2a1e7da
DW
9083 return 1;
9084
466070ad 9085 if ((unsigned int)write(fd, buf, sector_size) != sector_size)
c2a1e7da
DW
9086 return 1;
9087
c2a1e7da
DW
9088 return 0;
9089}
9090
2e735d19 9091static void imsm_sync_metadata(struct supertype *container)
845dea95 9092{
2e735d19 9093 struct intel_super *super = container->sb;
c2a1e7da 9094
1a64be56 9095 dprintf("sync metadata: %d\n", super->updates_pending);
c2a1e7da
DW
9096 if (!super->updates_pending)
9097 return;
9098
36988a3d 9099 write_super_imsm(container, 0);
c2a1e7da
DW
9100
9101 super->updates_pending = 0;
845dea95
NB
9102}
9103
272906ef
DW
9104static struct dl *imsm_readd(struct intel_super *super, int idx, struct active_array *a)
9105{
9106 struct imsm_dev *dev = get_imsm_dev(super, a->info.container_member);
238c0a71 9107 int i = get_imsm_disk_idx(dev, idx, MAP_X);
272906ef
DW
9108 struct dl *dl;
9109
9110 for (dl = super->disks; dl; dl = dl->next)
9111 if (dl->index == i)
9112 break;
9113
25ed7e59 9114 if (dl && is_failed(&dl->disk))
272906ef
DW
9115 dl = NULL;
9116
9117 if (dl)
1ade5cc1 9118 dprintf("found %x:%x\n", dl->major, dl->minor);
272906ef
DW
9119
9120 return dl;
9121}
9122
a20d2ba5 9123static struct dl *imsm_add_spare(struct intel_super *super, int slot,
8ba77d32
AK
9124 struct active_array *a, int activate_new,
9125 struct mdinfo *additional_test_list)
272906ef
DW
9126{
9127 struct imsm_dev *dev = get_imsm_dev(super, a->info.container_member);
238c0a71 9128 int idx = get_imsm_disk_idx(dev, slot, MAP_X);
a20d2ba5
DW
9129 struct imsm_super *mpb = super->anchor;
9130 struct imsm_map *map;
272906ef
DW
9131 unsigned long long pos;
9132 struct mdinfo *d;
9133 struct extent *ex;
a20d2ba5 9134 int i, j;
272906ef 9135 int found;
569cc43f
DW
9136 __u32 array_start = 0;
9137 __u32 array_end = 0;
272906ef 9138 struct dl *dl;
6c932028 9139 struct mdinfo *test_list;
272906ef
DW
9140
9141 for (dl = super->disks; dl; dl = dl->next) {
9142 /* If in this array, skip */
9143 for (d = a->info.devs ; d ; d = d->next)
4389ce73 9144 if (is_fd_valid(d->state_fd) &&
e553d2a4 9145 d->disk.major == dl->major &&
272906ef 9146 d->disk.minor == dl->minor) {
8ba77d32
AK
9147 dprintf("%x:%x already in array\n",
9148 dl->major, dl->minor);
272906ef
DW
9149 break;
9150 }
9151 if (d)
9152 continue;
6c932028
AK
9153 test_list = additional_test_list;
9154 while (test_list) {
9155 if (test_list->disk.major == dl->major &&
9156 test_list->disk.minor == dl->minor) {
8ba77d32
AK
9157 dprintf("%x:%x already in additional test list\n",
9158 dl->major, dl->minor);
9159 break;
9160 }
6c932028 9161 test_list = test_list->next;
8ba77d32 9162 }
6c932028 9163 if (test_list)
8ba77d32 9164 continue;
272906ef 9165
e553d2a4 9166 /* skip in use or failed drives */
25ed7e59 9167 if (is_failed(&dl->disk) || idx == dl->index ||
df474657
DW
9168 dl->index == -2) {
9169 dprintf("%x:%x status (failed: %d index: %d)\n",
25ed7e59 9170 dl->major, dl->minor, is_failed(&dl->disk), idx);
9a1608e5
DW
9171 continue;
9172 }
9173
a20d2ba5
DW
9174 /* skip pure spares when we are looking for partially
9175 * assimilated drives
9176 */
9177 if (dl->index == -1 && !activate_new)
9178 continue;
9179
f2cc4f7d
AO
9180 if (!drive_validate_sector_size(super, dl))
9181 continue;
9182
272906ef 9183 /* Does this unused device have the requisite free space?
a20d2ba5 9184 * It needs to be able to cover all member volumes
272906ef 9185 */
05501181 9186 ex = get_extents(super, dl, 1);
272906ef
DW
9187 if (!ex) {
9188 dprintf("cannot get extents\n");
9189 continue;
9190 }
a20d2ba5
DW
9191 for (i = 0; i < mpb->num_raid_devs; i++) {
9192 dev = get_imsm_dev(super, i);
238c0a71 9193 map = get_imsm_map(dev, MAP_0);
272906ef 9194
a20d2ba5
DW
9195 /* check if this disk is already a member of
9196 * this array
272906ef 9197 */
620b1713 9198 if (get_imsm_disk_slot(map, dl->index) >= 0)
a20d2ba5
DW
9199 continue;
9200
9201 found = 0;
9202 j = 0;
9203 pos = 0;
5551b113 9204 array_start = pba_of_lba0(map);
329c8278 9205 array_end = array_start +
44490938 9206 per_dev_array_size(map) - 1;
a20d2ba5
DW
9207
9208 do {
9209 /* check that we can start at pba_of_lba0 with
44490938 9210 * num_data_stripes*blocks_per_stripe of space
a20d2ba5 9211 */
329c8278 9212 if (array_start >= pos && array_end < ex[j].start) {
a20d2ba5
DW
9213 found = 1;
9214 break;
9215 }
9216 pos = ex[j].start + ex[j].size;
9217 j++;
9218 } while (ex[j-1].size);
9219
9220 if (!found)
272906ef 9221 break;
a20d2ba5 9222 }
272906ef
DW
9223
9224 free(ex);
a20d2ba5 9225 if (i < mpb->num_raid_devs) {
329c8278
DW
9226 dprintf("%x:%x does not have %u to %u available\n",
9227 dl->major, dl->minor, array_start, array_end);
272906ef
DW
9228 /* No room */
9229 continue;
a20d2ba5
DW
9230 }
9231 return dl;
272906ef
DW
9232 }
9233
9234 return dl;
9235}
9236
95d07a2c
LM
9237static int imsm_rebuild_allowed(struct supertype *cont, int dev_idx, int failed)
9238{
9239 struct imsm_dev *dev2;
9240 struct imsm_map *map;
9241 struct dl *idisk;
9242 int slot;
9243 int idx;
9244 __u8 state;
9245
9246 dev2 = get_imsm_dev(cont->sb, dev_idx);
756a15f3
MG
9247
9248 state = imsm_check_degraded(cont->sb, dev2, failed, MAP_0);
9249 if (state == IMSM_T_STATE_FAILED) {
9250 map = get_imsm_map(dev2, MAP_0);
9251 for (slot = 0; slot < map->num_members; slot++) {
9252 /*
9253 * Check if failed disks are deleted from intel
9254 * disk list or are marked to be deleted
9255 */
9256 idx = get_imsm_disk_idx(dev2, slot, MAP_X);
9257 idisk = get_imsm_dl_disk(cont->sb, idx);
9258 /*
9259 * Do not rebuild the array if failed disks
9260 * from failed sub-array are not removed from
9261 * container.
9262 */
9263 if (idisk &&
9264 is_failed(&idisk->disk) &&
9265 (idisk->action != DISK_REMOVE))
9266 return 0;
95d07a2c
LM
9267 }
9268 }
9269 return 1;
9270}
9271
88758e9d
DW
9272static struct mdinfo *imsm_activate_spare(struct active_array *a,
9273 struct metadata_update **updates)
9274{
9275 /**
d23fe947
DW
9276 * Find a device with unused free space and use it to replace a
9277 * failed/vacant region in an array. We replace failed regions one a
9278 * array at a time. The result is that a new spare disk will be added
9279 * to the first failed array and after the monitor has finished
9280 * propagating failures the remainder will be consumed.
88758e9d 9281 *
d23fe947
DW
9282 * FIXME add a capability for mdmon to request spares from another
9283 * container.
88758e9d
DW
9284 */
9285
9286 struct intel_super *super = a->container->sb;
88758e9d 9287 int inst = a->info.container_member;
949c47a0 9288 struct imsm_dev *dev = get_imsm_dev(super, inst);
238c0a71 9289 struct imsm_map *map = get_imsm_map(dev, MAP_0);
88758e9d
DW
9290 int failed = a->info.array.raid_disks;
9291 struct mdinfo *rv = NULL;
9292 struct mdinfo *d;
9293 struct mdinfo *di;
9294 struct metadata_update *mu;
9295 struct dl *dl;
9296 struct imsm_update_activate_spare *u;
9297 int num_spares = 0;
9298 int i;
95d07a2c 9299 int allowed;
88758e9d 9300
4389ce73
MT
9301 for (d = a->info.devs ; d; d = d->next) {
9302 if (!is_fd_valid(d->state_fd))
9303 continue;
9304
9305 if (d->curr_state & DS_FAULTY)
88758e9d
DW
9306 /* wait for Removal to happen */
9307 return NULL;
4389ce73
MT
9308
9309 failed--;
88758e9d
DW
9310 }
9311
9312 dprintf("imsm: activate spare: inst=%d failed=%d (%d) level=%d\n",
9313 inst, failed, a->info.array.raid_disks, a->info.array.level);
1af97990 9314
e2962bfc
AK
9315 if (imsm_reshape_blocks_arrays_changes(super))
9316 return NULL;
1af97990 9317
fc8ca064
AK
9318 /* Cannot activate another spare if rebuild is in progress already
9319 */
9320 if (is_rebuilding(dev)) {
7a862a02 9321 dprintf("imsm: No spare activation allowed. Rebuild in progress already.\n");
fc8ca064
AK
9322 return NULL;
9323 }
9324
89c67882
AK
9325 if (a->info.array.level == 4)
9326 /* No repair for takeovered array
9327 * imsm doesn't support raid4
9328 */
9329 return NULL;
9330
3b451610
AK
9331 if (imsm_check_degraded(super, dev, failed, MAP_0) !=
9332 IMSM_T_STATE_DEGRADED)
88758e9d
DW
9333 return NULL;
9334
83ca7d45
AP
9335 if (get_imsm_map(dev, MAP_0)->map_state == IMSM_T_STATE_UNINITIALIZED) {
9336 dprintf("imsm: No spare activation allowed. Volume is not initialized.\n");
9337 return NULL;
9338 }
9339
95d07a2c
LM
9340 /*
9341 * If there are any failed disks check state of the other volume.
9342 * Block rebuild if the another one is failed until failed disks
9343 * are removed from container.
9344 */
9345 if (failed) {
7a862a02 9346 dprintf("found failed disks in %.*s, check if there anotherfailed sub-array.\n",
c4acd1e5 9347 MAX_RAID_SERIAL_LEN, dev->volume);
95d07a2c
LM
9348 /* check if states of the other volumes allow for rebuild */
9349 for (i = 0; i < super->anchor->num_raid_devs; i++) {
9350 if (i != inst) {
9351 allowed = imsm_rebuild_allowed(a->container,
9352 i, failed);
9353 if (!allowed)
9354 return NULL;
9355 }
9356 }
9357 }
9358
88758e9d 9359 /* For each slot, if it is not working, find a spare */
88758e9d
DW
9360 for (i = 0; i < a->info.array.raid_disks; i++) {
9361 for (d = a->info.devs ; d ; d = d->next)
9362 if (d->disk.raid_disk == i)
9363 break;
9364 dprintf("found %d: %p %x\n", i, d, d?d->curr_state:0);
4389ce73 9365 if (d && is_fd_valid(d->state_fd))
88758e9d
DW
9366 continue;
9367
272906ef 9368 /*
a20d2ba5
DW
9369 * OK, this device needs recovery. Try to re-add the
9370 * previous occupant of this slot, if this fails see if
9371 * we can continue the assimilation of a spare that was
9372 * partially assimilated, finally try to activate a new
9373 * spare.
272906ef
DW
9374 */
9375 dl = imsm_readd(super, i, a);
9376 if (!dl)
b303fe21 9377 dl = imsm_add_spare(super, i, a, 0, rv);
a20d2ba5 9378 if (!dl)
b303fe21 9379 dl = imsm_add_spare(super, i, a, 1, rv);
272906ef
DW
9380 if (!dl)
9381 continue;
1011e834 9382
272906ef 9383 /* found a usable disk with enough space */
503975b9 9384 di = xcalloc(1, sizeof(*di));
272906ef
DW
9385
9386 /* dl->index will be -1 in the case we are activating a
9387 * pristine spare. imsm_process_update() will create a
9388 * new index in this case. Once a disk is found to be
9389 * failed in all member arrays it is kicked from the
9390 * metadata
9391 */
9392 di->disk.number = dl->index;
d23fe947 9393
272906ef
DW
9394 /* (ab)use di->devs to store a pointer to the device
9395 * we chose
9396 */
9397 di->devs = (struct mdinfo *) dl;
9398
9399 di->disk.raid_disk = i;
9400 di->disk.major = dl->major;
9401 di->disk.minor = dl->minor;
9402 di->disk.state = 0;
d23534e4 9403 di->recovery_start = 0;
5551b113 9404 di->data_offset = pba_of_lba0(map);
272906ef
DW
9405 di->component_size = a->info.component_size;
9406 di->container_member = inst;
5e46202e 9407 di->bb.supported = 1;
2c8890e9 9408 if (a->info.consistency_policy == CONSISTENCY_POLICY_PPL) {
2432ce9b 9409 di->ppl_sector = get_ppl_sector(super, inst);
c2462068 9410 di->ppl_size = MULTIPLE_PPL_AREA_SIZE_IMSM >> 9;
2432ce9b 9411 }
148acb7b 9412 super->random = random32();
272906ef
DW
9413 di->next = rv;
9414 rv = di;
9415 num_spares++;
9416 dprintf("%x:%x to be %d at %llu\n", dl->major, dl->minor,
9417 i, di->data_offset);
88758e9d
DW
9418 }
9419
9420 if (!rv)
9421 /* No spares found */
9422 return rv;
9423 /* Now 'rv' has a list of devices to return.
9424 * Create a metadata_update record to update the
9425 * disk_ord_tbl for the array
9426 */
503975b9 9427 mu = xmalloc(sizeof(*mu));
1011e834 9428 mu->buf = xcalloc(num_spares,
503975b9 9429 sizeof(struct imsm_update_activate_spare));
88758e9d 9430 mu->space = NULL;
cb23f1f4 9431 mu->space_list = NULL;
88758e9d
DW
9432 mu->len = sizeof(struct imsm_update_activate_spare) * num_spares;
9433 mu->next = *updates;
9434 u = (struct imsm_update_activate_spare *) mu->buf;
9435
9436 for (di = rv ; di ; di = di->next) {
9437 u->type = update_activate_spare;
d23fe947
DW
9438 u->dl = (struct dl *) di->devs;
9439 di->devs = NULL;
88758e9d
DW
9440 u->slot = di->disk.raid_disk;
9441 u->array = inst;
9442 u->next = u + 1;
9443 u++;
9444 }
9445 (u-1)->next = NULL;
9446 *updates = mu;
9447
9448 return rv;
9449}
9450
54c2c1ea 9451static int disks_overlap(struct intel_super *super, int idx, struct imsm_update_create_array *u)
8273f55e 9452{
54c2c1ea 9453 struct imsm_dev *dev = get_imsm_dev(super, idx);
238c0a71
AK
9454 struct imsm_map *map = get_imsm_map(dev, MAP_0);
9455 struct imsm_map *new_map = get_imsm_map(&u->dev, MAP_0);
54c2c1ea
DW
9456 struct disk_info *inf = get_disk_info(u);
9457 struct imsm_disk *disk;
8273f55e
DW
9458 int i;
9459 int j;
8273f55e 9460
54c2c1ea 9461 for (i = 0; i < map->num_members; i++) {
238c0a71 9462 disk = get_imsm_disk(super, get_imsm_disk_idx(dev, i, MAP_X));
54c2c1ea
DW
9463 for (j = 0; j < new_map->num_members; j++)
9464 if (serialcmp(disk->serial, inf[j].serial) == 0)
8273f55e
DW
9465 return 1;
9466 }
9467
9468 return 0;
9469}
9470
1a64be56
LM
9471static struct dl *get_disk_super(struct intel_super *super, int major, int minor)
9472{
594dc1b8
JS
9473 struct dl *dl;
9474
1a64be56 9475 for (dl = super->disks; dl; dl = dl->next)
089f9d79 9476 if (dl->major == major && dl->minor == minor)
1a64be56
LM
9477 return dl;
9478 return NULL;
9479}
9480
9481static int remove_disk_super(struct intel_super *super, int major, int minor)
9482{
594dc1b8 9483 struct dl *prev;
1a64be56
LM
9484 struct dl *dl;
9485
9486 prev = NULL;
9487 for (dl = super->disks; dl; dl = dl->next) {
089f9d79 9488 if (dl->major == major && dl->minor == minor) {
1a64be56
LM
9489 /* remove */
9490 if (prev)
9491 prev->next = dl->next;
9492 else
9493 super->disks = dl->next;
9494 dl->next = NULL;
3a85bf0e 9495 __free_imsm_disk(dl, 1);
1ade5cc1 9496 dprintf("removed %x:%x\n", major, minor);
1a64be56
LM
9497 break;
9498 }
9499 prev = dl;
9500 }
9501 return 0;
9502}
9503
f21e18ca 9504static void imsm_delete(struct intel_super *super, struct dl **dlp, unsigned index);
ae6aad82 9505
1a64be56
LM
9506static int add_remove_disk_update(struct intel_super *super)
9507{
9508 int check_degraded = 0;
594dc1b8
JS
9509 struct dl *disk;
9510
1a64be56
LM
9511 /* add/remove some spares to/from the metadata/contrainer */
9512 while (super->disk_mgmt_list) {
9513 struct dl *disk_cfg;
9514
9515 disk_cfg = super->disk_mgmt_list;
9516 super->disk_mgmt_list = disk_cfg->next;
9517 disk_cfg->next = NULL;
9518
9519 if (disk_cfg->action == DISK_ADD) {
9520 disk_cfg->next = super->disks;
9521 super->disks = disk_cfg;
9522 check_degraded = 1;
1ade5cc1
N
9523 dprintf("added %x:%x\n",
9524 disk_cfg->major, disk_cfg->minor);
1a64be56
LM
9525 } else if (disk_cfg->action == DISK_REMOVE) {
9526 dprintf("Disk remove action processed: %x.%x\n",
9527 disk_cfg->major, disk_cfg->minor);
9528 disk = get_disk_super(super,
9529 disk_cfg->major,
9530 disk_cfg->minor);
9531 if (disk) {
9532 /* store action status */
9533 disk->action = DISK_REMOVE;
9534 /* remove spare disks only */
9535 if (disk->index == -1) {
9536 remove_disk_super(super,
9537 disk_cfg->major,
9538 disk_cfg->minor);
91c97c54
MT
9539 } else {
9540 disk_cfg->fd = disk->fd;
9541 disk->fd = -1;
1a64be56
LM
9542 }
9543 }
9544 /* release allocate disk structure */
3a85bf0e 9545 __free_imsm_disk(disk_cfg, 1);
1a64be56
LM
9546 }
9547 }
9548 return check_degraded;
9549}
9550
a29911da
PC
9551static int apply_reshape_migration_update(struct imsm_update_reshape_migration *u,
9552 struct intel_super *super,
9553 void ***space_list)
9554{
9555 struct intel_dev *id;
9556 void **tofree = NULL;
9557 int ret_val = 0;
9558
1ade5cc1 9559 dprintf("(enter)\n");
089f9d79 9560 if (u->subdev < 0 || u->subdev > 1) {
a29911da
PC
9561 dprintf("imsm: Error: Wrong subdev: %i\n", u->subdev);
9562 return ret_val;
9563 }
089f9d79 9564 if (space_list == NULL || *space_list == NULL) {
a29911da
PC
9565 dprintf("imsm: Error: Memory is not allocated\n");
9566 return ret_val;
9567 }
9568
9569 for (id = super->devlist ; id; id = id->next) {
9570 if (id->index == (unsigned)u->subdev) {
9571 struct imsm_dev *dev = get_imsm_dev(super, u->subdev);
9572 struct imsm_map *map;
9573 struct imsm_dev *new_dev =
9574 (struct imsm_dev *)*space_list;
238c0a71 9575 struct imsm_map *migr_map = get_imsm_map(dev, MAP_1);
a29911da
PC
9576 int to_state;
9577 struct dl *new_disk;
9578
9579 if (new_dev == NULL)
9580 return ret_val;
9581 *space_list = **space_list;
9582 memcpy(new_dev, dev, sizeof_imsm_dev(dev, 0));
238c0a71 9583 map = get_imsm_map(new_dev, MAP_0);
a29911da
PC
9584 if (migr_map) {
9585 dprintf("imsm: Error: migration in progress");
9586 return ret_val;
9587 }
9588
9589 to_state = map->map_state;
9590 if ((u->new_level == 5) && (map->raid_level == 0)) {
9591 map->num_members++;
9592 /* this should not happen */
9593 if (u->new_disks[0] < 0) {
9594 map->failed_disk_num =
9595 map->num_members - 1;
9596 to_state = IMSM_T_STATE_DEGRADED;
9597 } else
9598 to_state = IMSM_T_STATE_NORMAL;
9599 }
8e59f3d8 9600 migrate(new_dev, super, to_state, MIGR_GEN_MIGR);
a29911da
PC
9601 if (u->new_level > -1)
9602 map->raid_level = u->new_level;
238c0a71 9603 migr_map = get_imsm_map(new_dev, MAP_1);
a29911da
PC
9604 if ((u->new_level == 5) &&
9605 (migr_map->raid_level == 0)) {
9606 int ord = map->num_members - 1;
9607 migr_map->num_members--;
9608 if (u->new_disks[0] < 0)
9609 ord |= IMSM_ORD_REBUILD;
9610 set_imsm_ord_tbl_ent(map,
9611 map->num_members - 1,
9612 ord);
9613 }
9614 id->dev = new_dev;
9615 tofree = (void **)dev;
9616
4bba0439
PC
9617 /* update chunk size
9618 */
06fb291a 9619 if (u->new_chunksize > 0) {
9529d343
MD
9620 struct imsm_map *dest_map =
9621 get_imsm_map(dev, MAP_0);
06fb291a 9622 int used_disks =
9529d343 9623 imsm_num_data_members(dest_map);
06fb291a
PB
9624
9625 if (used_disks == 0)
9626 return ret_val;
9627
4bba0439
PC
9628 map->blocks_per_strip =
9629 __cpu_to_le16(u->new_chunksize * 2);
1c275381 9630 update_num_data_stripes(map, imsm_dev_size(dev));
06fb291a 9631 }
4bba0439 9632
44490938
MD
9633 /* ensure blocks_per_member has valid value
9634 */
9635 set_blocks_per_member(map,
9636 per_dev_array_size(map) +
9637 NUM_BLOCKS_DIRTY_STRIPE_REGION);
9638
a29911da
PC
9639 /* add disk
9640 */
089f9d79
JS
9641 if (u->new_level != 5 || migr_map->raid_level != 0 ||
9642 migr_map->raid_level == map->raid_level)
a29911da
PC
9643 goto skip_disk_add;
9644
9645 if (u->new_disks[0] >= 0) {
9646 /* use passes spare
9647 */
9648 new_disk = get_disk_super(super,
9649 major(u->new_disks[0]),
9650 minor(u->new_disks[0]));
7a862a02 9651 dprintf("imsm: new disk for reshape is: %i:%i (%p, index = %i)\n",
a29911da
PC
9652 major(u->new_disks[0]),
9653 minor(u->new_disks[0]),
9654 new_disk, new_disk->index);
9655 if (new_disk == NULL)
9656 goto error_disk_add;
9657
9658 new_disk->index = map->num_members - 1;
9659 /* slot to fill in autolayout
9660 */
9661 new_disk->raiddisk = new_disk->index;
9662 new_disk->disk.status |= CONFIGURED_DISK;
9663 new_disk->disk.status &= ~SPARE_DISK;
9664 } else
9665 goto error_disk_add;
9666
9667skip_disk_add:
9668 *tofree = *space_list;
9669 /* calculate new size
9670 */
f3871fdc 9671 imsm_set_array_size(new_dev, -1);
a29911da
PC
9672
9673 ret_val = 1;
9674 }
9675 }
9676
9677 if (tofree)
9678 *space_list = tofree;
9679 return ret_val;
9680
9681error_disk_add:
9682 dprintf("Error: imsm: Cannot find disk.\n");
9683 return ret_val;
9684}
9685
f3871fdc
AK
9686static int apply_size_change_update(struct imsm_update_size_change *u,
9687 struct intel_super *super)
9688{
9689 struct intel_dev *id;
9690 int ret_val = 0;
9691
1ade5cc1 9692 dprintf("(enter)\n");
089f9d79 9693 if (u->subdev < 0 || u->subdev > 1) {
f3871fdc
AK
9694 dprintf("imsm: Error: Wrong subdev: %i\n", u->subdev);
9695 return ret_val;
9696 }
9697
9698 for (id = super->devlist ; id; id = id->next) {
9699 if (id->index == (unsigned)u->subdev) {
9700 struct imsm_dev *dev = get_imsm_dev(super, u->subdev);
9701 struct imsm_map *map = get_imsm_map(dev, MAP_0);
9529d343 9702 int used_disks = imsm_num_data_members(map);
f3871fdc 9703 unsigned long long blocks_per_member;
44490938
MD
9704 unsigned long long new_size_per_disk;
9705
9706 if (used_disks == 0)
9707 return 0;
f3871fdc
AK
9708
9709 /* calculate new size
9710 */
44490938
MD
9711 new_size_per_disk = u->new_size / used_disks;
9712 blocks_per_member = new_size_per_disk +
9713 NUM_BLOCKS_DIRTY_STRIPE_REGION;
f3871fdc 9714
1c275381
MT
9715 imsm_set_array_size(dev, u->new_size);
9716 set_blocks_per_member(map, blocks_per_member);
9717 update_num_data_stripes(map, u->new_size);
f3871fdc
AK
9718 ret_val = 1;
9719 break;
9720 }
9721 }
9722
9723 return ret_val;
9724}
9725
69d40de4
JR
9726static int prepare_spare_to_activate(struct supertype *st,
9727 struct imsm_update_activate_spare *u)
9728{
9729 struct intel_super *super = st->sb;
9730 int prev_current_vol = super->current_vol;
9731 struct active_array *a;
9732 int ret = 1;
9733
9734 for (a = st->arrays; a; a = a->next)
9735 /*
9736 * Additional initialization (adding bitmap header, filling
9737 * the bitmap area with '1's to force initial rebuild for a whole
9738 * data-area) is required when adding the spare to the volume
9739 * with write-intent bitmap.
9740 */
9741 if (a->info.container_member == u->array &&
9742 a->info.consistency_policy == CONSISTENCY_POLICY_BITMAP) {
9743 struct dl *dl;
9744
9745 for (dl = super->disks; dl; dl = dl->next)
9746 if (dl == u->dl)
9747 break;
9748 if (!dl)
9749 break;
9750
9751 super->current_vol = u->array;
9752 if (st->ss->write_bitmap(st, dl->fd, NoUpdate))
9753 ret = 0;
9754 super->current_vol = prev_current_vol;
9755 }
9756 return ret;
9757}
9758
061d7da3 9759static int apply_update_activate_spare(struct imsm_update_activate_spare *u,
ca9de185 9760 struct intel_super *super,
061d7da3
LO
9761 struct active_array *active_array)
9762{
9763 struct imsm_super *mpb = super->anchor;
9764 struct imsm_dev *dev = get_imsm_dev(super, u->array);
238c0a71 9765 struct imsm_map *map = get_imsm_map(dev, MAP_0);
061d7da3
LO
9766 struct imsm_map *migr_map;
9767 struct active_array *a;
9768 struct imsm_disk *disk;
9769 __u8 to_state;
9770 struct dl *dl;
9771 unsigned int found;
9772 int failed;
5961eeec 9773 int victim;
061d7da3 9774 int i;
5961eeec 9775 int second_map_created = 0;
061d7da3 9776
5961eeec 9777 for (; u; u = u->next) {
238c0a71 9778 victim = get_imsm_disk_idx(dev, u->slot, MAP_X);
061d7da3 9779
5961eeec 9780 if (victim < 0)
9781 return 0;
061d7da3 9782
5961eeec 9783 for (dl = super->disks; dl; dl = dl->next)
9784 if (dl == u->dl)
9785 break;
061d7da3 9786
5961eeec 9787 if (!dl) {
7a862a02 9788 pr_err("error: imsm_activate_spare passed an unknown disk (index: %d)\n",
5961eeec 9789 u->dl->index);
9790 return 0;
9791 }
061d7da3 9792
5961eeec 9793 /* count failures (excluding rebuilds and the victim)
9794 * to determine map[0] state
9795 */
9796 failed = 0;
9797 for (i = 0; i < map->num_members; i++) {
9798 if (i == u->slot)
9799 continue;
9800 disk = get_imsm_disk(super,
238c0a71 9801 get_imsm_disk_idx(dev, i, MAP_X));
5961eeec 9802 if (!disk || is_failed(disk))
9803 failed++;
9804 }
061d7da3 9805
5961eeec 9806 /* adding a pristine spare, assign a new index */
9807 if (dl->index < 0) {
9808 dl->index = super->anchor->num_disks;
9809 super->anchor->num_disks++;
9810 }
9811 disk = &dl->disk;
9812 disk->status |= CONFIGURED_DISK;
9813 disk->status &= ~SPARE_DISK;
9814
9815 /* mark rebuild */
238c0a71 9816 to_state = imsm_check_degraded(super, dev, failed, MAP_0);
5961eeec 9817 if (!second_map_created) {
9818 second_map_created = 1;
9819 map->map_state = IMSM_T_STATE_DEGRADED;
9820 migrate(dev, super, to_state, MIGR_REBUILD);
9821 } else
9822 map->map_state = to_state;
238c0a71 9823 migr_map = get_imsm_map(dev, MAP_1);
5961eeec 9824 set_imsm_ord_tbl_ent(map, u->slot, dl->index);
9825 set_imsm_ord_tbl_ent(migr_map, u->slot,
9826 dl->index | IMSM_ORD_REBUILD);
9827
9828 /* update the family_num to mark a new container
9829 * generation, being careful to record the existing
9830 * family_num in orig_family_num to clean up after
9831 * earlier mdadm versions that neglected to set it.
9832 */
9833 if (mpb->orig_family_num == 0)
9834 mpb->orig_family_num = mpb->family_num;
9835 mpb->family_num += super->random;
9836
9837 /* count arrays using the victim in the metadata */
9838 found = 0;
9839 for (a = active_array; a ; a = a->next) {
76c152ca 9840 int dev_idx = a->info.container_member;
061d7da3 9841
76c152ca 9842 if (get_disk_slot_in_dev(super, dev_idx, victim) >= 0)
5961eeec 9843 found++;
9844 }
061d7da3 9845
5961eeec 9846 /* delete the victim if it is no longer being
9847 * utilized anywhere
061d7da3 9848 */
5961eeec 9849 if (!found) {
9850 struct dl **dlp;
061d7da3 9851
5961eeec 9852 /* We know that 'manager' isn't touching anything,
9853 * so it is safe to delete
9854 */
9855 for (dlp = &super->disks; *dlp; dlp = &(*dlp)->next)
061d7da3
LO
9856 if ((*dlp)->index == victim)
9857 break;
5961eeec 9858
9859 /* victim may be on the missing list */
9860 if (!*dlp)
9861 for (dlp = &super->missing; *dlp;
9862 dlp = &(*dlp)->next)
9863 if ((*dlp)->index == victim)
9864 break;
9865 imsm_delete(super, dlp, victim);
9866 }
061d7da3
LO
9867 }
9868
9869 return 1;
9870}
a29911da 9871
2e5dc010
N
9872static int apply_reshape_container_disks_update(struct imsm_update_reshape *u,
9873 struct intel_super *super,
9874 void ***space_list)
9875{
9876 struct dl *new_disk;
9877 struct intel_dev *id;
9878 int i;
9879 int delta_disks = u->new_raid_disks - u->old_raid_disks;
ee4beede 9880 int disk_count = u->old_raid_disks;
2e5dc010
N
9881 void **tofree = NULL;
9882 int devices_to_reshape = 1;
9883 struct imsm_super *mpb = super->anchor;
9884 int ret_val = 0;
d098291a 9885 unsigned int dev_id;
2e5dc010 9886
1ade5cc1 9887 dprintf("(enter)\n");
2e5dc010
N
9888
9889 /* enable spares to use in array */
9890 for (i = 0; i < delta_disks; i++) {
9891 new_disk = get_disk_super(super,
9892 major(u->new_disks[i]),
9893 minor(u->new_disks[i]));
7a862a02 9894 dprintf("imsm: new disk for reshape is: %i:%i (%p, index = %i)\n",
2e5dc010
N
9895 major(u->new_disks[i]), minor(u->new_disks[i]),
9896 new_disk, new_disk->index);
089f9d79
JS
9897 if (new_disk == NULL ||
9898 (new_disk->index >= 0 &&
9899 new_disk->index < u->old_raid_disks))
2e5dc010 9900 goto update_reshape_exit;
ee4beede 9901 new_disk->index = disk_count++;
2e5dc010
N
9902 /* slot to fill in autolayout
9903 */
9904 new_disk->raiddisk = new_disk->index;
9905 new_disk->disk.status |=
9906 CONFIGURED_DISK;
9907 new_disk->disk.status &= ~SPARE_DISK;
9908 }
9909
ed7333bd
AK
9910 dprintf("imsm: volume set mpb->num_raid_devs = %i\n",
9911 mpb->num_raid_devs);
2e5dc010
N
9912 /* manage changes in volume
9913 */
d098291a 9914 for (dev_id = 0; dev_id < mpb->num_raid_devs; dev_id++) {
2e5dc010
N
9915 void **sp = *space_list;
9916 struct imsm_dev *newdev;
9917 struct imsm_map *newmap, *oldmap;
9918
d098291a
AK
9919 for (id = super->devlist ; id; id = id->next) {
9920 if (id->index == dev_id)
9921 break;
9922 }
9923 if (id == NULL)
9924 break;
2e5dc010
N
9925 if (!sp)
9926 continue;
9927 *space_list = *sp;
9928 newdev = (void*)sp;
9929 /* Copy the dev, but not (all of) the map */
9930 memcpy(newdev, id->dev, sizeof(*newdev));
238c0a71
AK
9931 oldmap = get_imsm_map(id->dev, MAP_0);
9932 newmap = get_imsm_map(newdev, MAP_0);
2e5dc010
N
9933 /* Copy the current map */
9934 memcpy(newmap, oldmap, sizeof_imsm_map(oldmap));
9935 /* update one device only
9936 */
9937 if (devices_to_reshape) {
ed7333bd
AK
9938 dprintf("imsm: modifying subdev: %i\n",
9939 id->index);
2e5dc010
N
9940 devices_to_reshape--;
9941 newdev->vol.migr_state = 1;
4036e7ee 9942 set_vol_curr_migr_unit(newdev, 0);
ea672ee1 9943 set_migr_type(newdev, MIGR_GEN_MIGR);
2e5dc010
N
9944 newmap->num_members = u->new_raid_disks;
9945 for (i = 0; i < delta_disks; i++) {
9946 set_imsm_ord_tbl_ent(newmap,
9947 u->old_raid_disks + i,
9948 u->old_raid_disks + i);
9949 }
9950 /* New map is correct, now need to save old map
9951 */
238c0a71 9952 newmap = get_imsm_map(newdev, MAP_1);
2e5dc010
N
9953 memcpy(newmap, oldmap, sizeof_imsm_map(oldmap));
9954
f3871fdc 9955 imsm_set_array_size(newdev, -1);
2e5dc010
N
9956 }
9957
9958 sp = (void **)id->dev;
9959 id->dev = newdev;
9960 *sp = tofree;
9961 tofree = sp;
8e59f3d8
AK
9962
9963 /* Clear migration record */
9964 memset(super->migr_rec, 0, sizeof(struct migr_record));
2e5dc010 9965 }
819bc634
AK
9966 if (tofree)
9967 *space_list = tofree;
2e5dc010
N
9968 ret_val = 1;
9969
9970update_reshape_exit:
9971
9972 return ret_val;
9973}
9974
bb025c2f 9975static int apply_takeover_update(struct imsm_update_takeover *u,
8ca6df95
KW
9976 struct intel_super *super,
9977 void ***space_list)
bb025c2f
KW
9978{
9979 struct imsm_dev *dev = NULL;
8ca6df95
KW
9980 struct intel_dev *dv;
9981 struct imsm_dev *dev_new;
bb025c2f
KW
9982 struct imsm_map *map;
9983 struct dl *dm, *du;
8ca6df95 9984 int i;
bb025c2f
KW
9985
9986 for (dv = super->devlist; dv; dv = dv->next)
9987 if (dv->index == (unsigned int)u->subarray) {
9988 dev = dv->dev;
9989 break;
9990 }
9991
9992 if (dev == NULL)
9993 return 0;
9994
238c0a71 9995 map = get_imsm_map(dev, MAP_0);
bb025c2f
KW
9996
9997 if (u->direction == R10_TO_R0) {
43d5ec18 9998 /* Number of failed disks must be half of initial disk number */
3b451610
AK
9999 if (imsm_count_failed(super, dev, MAP_0) !=
10000 (map->num_members / 2))
43d5ec18
KW
10001 return 0;
10002
bb025c2f
KW
10003 /* iterate through devices to mark removed disks as spare */
10004 for (dm = super->disks; dm; dm = dm->next) {
10005 if (dm->disk.status & FAILED_DISK) {
10006 int idx = dm->index;
10007 /* update indexes on the disk list */
10008/* FIXME this loop-with-the-loop looks wrong, I'm not convinced
10009 the index values will end up being correct.... NB */
10010 for (du = super->disks; du; du = du->next)
10011 if (du->index > idx)
10012 du->index--;
10013 /* mark as spare disk */
a8619d23 10014 mark_spare(dm);
bb025c2f
KW
10015 }
10016 }
bb025c2f 10017 /* update map */
1c275381 10018 map->num_members /= map->num_domains;
bb025c2f 10019 map->map_state = IMSM_T_STATE_NORMAL;
bb025c2f 10020 map->raid_level = 0;
1c275381
MT
10021 set_num_domains(map);
10022 update_num_data_stripes(map, imsm_dev_size(dev));
bb025c2f
KW
10023 map->failed_disk_num = -1;
10024 }
10025
8ca6df95
KW
10026 if (u->direction == R0_TO_R10) {
10027 void **space;
4a353e6e 10028
8ca6df95
KW
10029 /* update slots in current disk list */
10030 for (dm = super->disks; dm; dm = dm->next) {
10031 if (dm->index >= 0)
10032 dm->index *= 2;
10033 }
10034 /* create new *missing* disks */
10035 for (i = 0; i < map->num_members; i++) {
10036 space = *space_list;
10037 if (!space)
10038 continue;
10039 *space_list = *space;
10040 du = (void *)space;
10041 memcpy(du, super->disks, sizeof(*du));
8ca6df95
KW
10042 du->fd = -1;
10043 du->minor = 0;
10044 du->major = 0;
10045 du->index = (i * 2) + 1;
10046 sprintf((char *)du->disk.serial,
10047 " MISSING_%d", du->index);
10048 sprintf((char *)du->serial,
10049 "MISSING_%d", du->index);
10050 du->next = super->missing;
10051 super->missing = du;
10052 }
10053 /* create new dev and map */
10054 space = *space_list;
10055 if (!space)
10056 return 0;
10057 *space_list = *space;
10058 dev_new = (void *)space;
10059 memcpy(dev_new, dev, sizeof(*dev));
10060 /* update new map */
238c0a71 10061 map = get_imsm_map(dev_new, MAP_0);
1c275381 10062
1a2487c2 10063 map->map_state = IMSM_T_STATE_DEGRADED;
8ca6df95 10064 map->raid_level = 1;
1c275381
MT
10065 set_num_domains(map);
10066 map->num_members = map->num_members * map->num_domains;
10067 update_num_data_stripes(map, imsm_dev_size(dev));
4a353e6e 10068
8ca6df95
KW
10069 /* replace dev<->dev_new */
10070 dv->dev = dev_new;
10071 }
bb025c2f
KW
10072 /* update disk order table */
10073 for (du = super->disks; du; du = du->next)
10074 if (du->index >= 0)
10075 set_imsm_ord_tbl_ent(map, du->index, du->index);
8ca6df95 10076 for (du = super->missing; du; du = du->next)
1a2487c2
KW
10077 if (du->index >= 0) {
10078 set_imsm_ord_tbl_ent(map, du->index, du->index);
4c9e8c1e 10079 mark_missing(super, dv->dev, &du->disk, du->index);
1a2487c2 10080 }
bb025c2f
KW
10081
10082 return 1;
10083}
10084
e8319a19
DW
10085static void imsm_process_update(struct supertype *st,
10086 struct metadata_update *update)
10087{
10088 /**
10089 * crack open the metadata_update envelope to find the update record
10090 * update can be one of:
d195167d
AK
10091 * update_reshape_container_disks - all the arrays in the container
10092 * are being reshaped to have more devices. We need to mark
10093 * the arrays for general migration and convert selected spares
10094 * into active devices.
10095 * update_activate_spare - a spare device has replaced a failed
1011e834
N
10096 * device in an array, update the disk_ord_tbl. If this disk is
10097 * present in all member arrays then also clear the SPARE_DISK
10098 * flag
d195167d
AK
10099 * update_create_array
10100 * update_kill_array
10101 * update_rename_array
10102 * update_add_remove_disk
e8319a19
DW
10103 */
10104 struct intel_super *super = st->sb;
4d7b1503 10105 struct imsm_super *mpb;
e8319a19
DW
10106 enum imsm_update_type type = *(enum imsm_update_type *) update->buf;
10107
4d7b1503
DW
10108 /* update requires a larger buf but the allocation failed */
10109 if (super->next_len && !super->next_buf) {
10110 super->next_len = 0;
10111 return;
10112 }
10113
10114 if (super->next_buf) {
10115 memcpy(super->next_buf, super->buf, super->len);
10116 free(super->buf);
10117 super->len = super->next_len;
10118 super->buf = super->next_buf;
10119
10120 super->next_len = 0;
10121 super->next_buf = NULL;
10122 }
10123
10124 mpb = super->anchor;
10125
e8319a19 10126 switch (type) {
0ec5d470
AK
10127 case update_general_migration_checkpoint: {
10128 struct intel_dev *id;
10129 struct imsm_update_general_migration_checkpoint *u =
10130 (void *)update->buf;
10131
1ade5cc1 10132 dprintf("called for update_general_migration_checkpoint\n");
0ec5d470
AK
10133
10134 /* find device under general migration */
10135 for (id = super->devlist ; id; id = id->next) {
10136 if (is_gen_migration(id->dev)) {
4036e7ee
MT
10137 set_vol_curr_migr_unit(id->dev,
10138 u->curr_migr_unit);
0ec5d470
AK
10139 super->updates_pending++;
10140 }
10141 }
10142 break;
10143 }
bb025c2f
KW
10144 case update_takeover: {
10145 struct imsm_update_takeover *u = (void *)update->buf;
1a2487c2
KW
10146 if (apply_takeover_update(u, super, &update->space_list)) {
10147 imsm_update_version_info(super);
bb025c2f 10148 super->updates_pending++;
1a2487c2 10149 }
bb025c2f
KW
10150 break;
10151 }
10152
78b10e66 10153 case update_reshape_container_disks: {
d195167d 10154 struct imsm_update_reshape *u = (void *)update->buf;
2e5dc010
N
10155 if (apply_reshape_container_disks_update(
10156 u, super, &update->space_list))
10157 super->updates_pending++;
78b10e66
N
10158 break;
10159 }
48c5303a 10160 case update_reshape_migration: {
a29911da
PC
10161 struct imsm_update_reshape_migration *u = (void *)update->buf;
10162 if (apply_reshape_migration_update(
10163 u, super, &update->space_list))
10164 super->updates_pending++;
48c5303a
PC
10165 break;
10166 }
f3871fdc
AK
10167 case update_size_change: {
10168 struct imsm_update_size_change *u = (void *)update->buf;
10169 if (apply_size_change_update(u, super))
10170 super->updates_pending++;
10171 break;
10172 }
e8319a19 10173 case update_activate_spare: {
1011e834 10174 struct imsm_update_activate_spare *u = (void *) update->buf;
69d40de4
JR
10175
10176 if (prepare_spare_to_activate(st, u) &&
10177 apply_update_activate_spare(u, super, st->arrays))
061d7da3 10178 super->updates_pending++;
8273f55e
DW
10179 break;
10180 }
10181 case update_create_array: {
10182 /* someone wants to create a new array, we need to be aware of
10183 * a few races/collisions:
10184 * 1/ 'Create' called by two separate instances of mdadm
10185 * 2/ 'Create' versus 'activate_spare': mdadm has chosen
10186 * devices that have since been assimilated via
10187 * activate_spare.
10188 * In the event this update can not be carried out mdadm will
10189 * (FIX ME) notice that its update did not take hold.
10190 */
10191 struct imsm_update_create_array *u = (void *) update->buf;
ba2de7ba 10192 struct intel_dev *dv;
8273f55e
DW
10193 struct imsm_dev *dev;
10194 struct imsm_map *map, *new_map;
10195 unsigned long long start, end;
10196 unsigned long long new_start, new_end;
10197 int i;
54c2c1ea
DW
10198 struct disk_info *inf;
10199 struct dl *dl;
8273f55e
DW
10200
10201 /* handle racing creates: first come first serve */
10202 if (u->dev_idx < mpb->num_raid_devs) {
1ade5cc1 10203 dprintf("subarray %d already defined\n", u->dev_idx);
ba2de7ba 10204 goto create_error;
8273f55e
DW
10205 }
10206
10207 /* check update is next in sequence */
10208 if (u->dev_idx != mpb->num_raid_devs) {
1ade5cc1
N
10209 dprintf("can not create array %d expected index %d\n",
10210 u->dev_idx, mpb->num_raid_devs);
ba2de7ba 10211 goto create_error;
8273f55e
DW
10212 }
10213
238c0a71 10214 new_map = get_imsm_map(&u->dev, MAP_0);
5551b113 10215 new_start = pba_of_lba0(new_map);
44490938 10216 new_end = new_start + per_dev_array_size(new_map);
54c2c1ea 10217 inf = get_disk_info(u);
8273f55e
DW
10218
10219 /* handle activate_spare versus create race:
10220 * check to make sure that overlapping arrays do not include
10221 * overalpping disks
10222 */
10223 for (i = 0; i < mpb->num_raid_devs; i++) {
949c47a0 10224 dev = get_imsm_dev(super, i);
238c0a71 10225 map = get_imsm_map(dev, MAP_0);
5551b113 10226 start = pba_of_lba0(map);
44490938 10227 end = start + per_dev_array_size(map);
8273f55e
DW
10228 if ((new_start >= start && new_start <= end) ||
10229 (start >= new_start && start <= new_end))
54c2c1ea
DW
10230 /* overlap */;
10231 else
10232 continue;
10233
10234 if (disks_overlap(super, i, u)) {
1ade5cc1 10235 dprintf("arrays overlap\n");
ba2de7ba 10236 goto create_error;
8273f55e
DW
10237 }
10238 }
8273f55e 10239
949c47a0
DW
10240 /* check that prepare update was successful */
10241 if (!update->space) {
1ade5cc1 10242 dprintf("prepare update failed\n");
ba2de7ba 10243 goto create_error;
949c47a0
DW
10244 }
10245
54c2c1ea
DW
10246 /* check that all disks are still active before committing
10247 * changes. FIXME: could we instead handle this by creating a
10248 * degraded array? That's probably not what the user expects,
10249 * so better to drop this update on the floor.
10250 */
10251 for (i = 0; i < new_map->num_members; i++) {
10252 dl = serial_to_dl(inf[i].serial, super);
10253 if (!dl) {
1ade5cc1 10254 dprintf("disk disappeared\n");
ba2de7ba 10255 goto create_error;
54c2c1ea 10256 }
949c47a0
DW
10257 }
10258
8273f55e 10259 super->updates_pending++;
54c2c1ea
DW
10260
10261 /* convert spares to members and fixup ord_tbl */
10262 for (i = 0; i < new_map->num_members; i++) {
10263 dl = serial_to_dl(inf[i].serial, super);
10264 if (dl->index == -1) {
10265 dl->index = mpb->num_disks;
10266 mpb->num_disks++;
10267 dl->disk.status |= CONFIGURED_DISK;
10268 dl->disk.status &= ~SPARE_DISK;
10269 }
10270 set_imsm_ord_tbl_ent(new_map, i, dl->index);
10271 }
10272
ba2de7ba
DW
10273 dv = update->space;
10274 dev = dv->dev;
949c47a0
DW
10275 update->space = NULL;
10276 imsm_copy_dev(dev, &u->dev);
ba2de7ba
DW
10277 dv->index = u->dev_idx;
10278 dv->next = super->devlist;
10279 super->devlist = dv;
8273f55e 10280 mpb->num_raid_devs++;
8273f55e 10281
4d1313e9 10282 imsm_update_version_info(super);
8273f55e 10283 break;
ba2de7ba
DW
10284 create_error:
10285 /* mdmon knows how to release update->space, but not
10286 * ((struct intel_dev *) update->space)->dev
10287 */
10288 if (update->space) {
10289 dv = update->space;
10290 free(dv->dev);
10291 }
8273f55e 10292 break;
e8319a19 10293 }
33414a01
DW
10294 case update_kill_array: {
10295 struct imsm_update_kill_array *u = (void *) update->buf;
10296 int victim = u->dev_idx;
10297 struct active_array *a;
10298 struct intel_dev **dp;
33414a01
DW
10299
10300 /* sanity check that we are not affecting the uuid of
10301 * active arrays, or deleting an active array
10302 *
10303 * FIXME when immutable ids are available, but note that
10304 * we'll also need to fixup the invalidated/active
10305 * subarray indexes in mdstat
10306 */
10307 for (a = st->arrays; a; a = a->next)
10308 if (a->info.container_member >= victim)
10309 break;
10310 /* by definition if mdmon is running at least one array
10311 * is active in the container, so checking
10312 * mpb->num_raid_devs is just extra paranoia
10313 */
756a15f3 10314 if (a || mpb->num_raid_devs == 1 || victim >= super->anchor->num_raid_devs) {
33414a01
DW
10315 dprintf("failed to delete subarray-%d\n", victim);
10316 break;
10317 }
10318
10319 for (dp = &super->devlist; *dp;)
f21e18ca 10320 if ((*dp)->index == (unsigned)super->current_vol) {
33414a01
DW
10321 *dp = (*dp)->next;
10322 } else {
f21e18ca 10323 if ((*dp)->index > (unsigned)victim)
33414a01
DW
10324 (*dp)->index--;
10325 dp = &(*dp)->next;
10326 }
10327 mpb->num_raid_devs--;
10328 super->updates_pending++;
10329 break;
10330 }
aa534678
DW
10331 case update_rename_array: {
10332 struct imsm_update_rename_array *u = (void *) update->buf;
10333 char name[MAX_RAID_SERIAL_LEN+1];
10334 int target = u->dev_idx;
10335 struct active_array *a;
10336 struct imsm_dev *dev;
10337
10338 /* sanity check that we are not affecting the uuid of
10339 * an active array
10340 */
40659392 10341 memset(name, 0, sizeof(name));
aa534678
DW
10342 snprintf(name, MAX_RAID_SERIAL_LEN, "%s", (char *) u->name);
10343 name[MAX_RAID_SERIAL_LEN] = '\0';
10344 for (a = st->arrays; a; a = a->next)
10345 if (a->info.container_member == target)
10346 break;
10347 dev = get_imsm_dev(super, u->dev_idx);
756a15f3 10348 if (a || !check_name(super, name, 1)) {
aa534678
DW
10349 dprintf("failed to rename subarray-%d\n", target);
10350 break;
10351 }
10352
40659392 10353 memcpy(dev->volume, name, MAX_RAID_SERIAL_LEN);
aa534678
DW
10354 super->updates_pending++;
10355 break;
10356 }
1a64be56 10357 case update_add_remove_disk: {
43dad3d6 10358 /* we may be able to repair some arrays if disks are
095b8088 10359 * being added, check the status of add_remove_disk
1a64be56
LM
10360 * if discs has been added.
10361 */
10362 if (add_remove_disk_update(super)) {
43dad3d6 10363 struct active_array *a;
072b727f
DW
10364
10365 super->updates_pending++;
1a64be56 10366 for (a = st->arrays; a; a = a->next)
43dad3d6
DW
10367 a->check_degraded = 1;
10368 }
43dad3d6 10369 break;
e8319a19 10370 }
bbab0940
TM
10371 case update_prealloc_badblocks_mem:
10372 break;
e6e9dd3f
AP
10373 case update_rwh_policy: {
10374 struct imsm_update_rwh_policy *u = (void *)update->buf;
10375 int target = u->dev_idx;
10376 struct imsm_dev *dev = get_imsm_dev(super, target);
e6e9dd3f
AP
10377
10378 if (dev->rwh_policy != u->new_policy) {
10379 dev->rwh_policy = u->new_policy;
10380 super->updates_pending++;
10381 }
10382 break;
10383 }
1a64be56 10384 default:
ebf3be99 10385 pr_err("error: unsupported process update type:(type: %d)\n", type);
1a64be56 10386 }
e8319a19 10387}
88758e9d 10388
bc0b9d34
PC
10389static struct mdinfo *get_spares_for_grow(struct supertype *st);
10390
5fe6f031
N
10391static int imsm_prepare_update(struct supertype *st,
10392 struct metadata_update *update)
8273f55e 10393{
949c47a0 10394 /**
4d7b1503
DW
10395 * Allocate space to hold new disk entries, raid-device entries or a new
10396 * mpb if necessary. The manager synchronously waits for updates to
10397 * complete in the monitor, so new mpb buffers allocated here can be
10398 * integrated by the monitor thread without worrying about live pointers
10399 * in the manager thread.
8273f55e 10400 */
095b8088 10401 enum imsm_update_type type;
4d7b1503 10402 struct intel_super *super = st->sb;
f36a9ecd 10403 unsigned int sector_size = super->sector_size;
4d7b1503
DW
10404 struct imsm_super *mpb = super->anchor;
10405 size_t buf_len;
10406 size_t len = 0;
949c47a0 10407
095b8088
N
10408 if (update->len < (int)sizeof(type))
10409 return 0;
10410
10411 type = *(enum imsm_update_type *) update->buf;
10412
949c47a0 10413 switch (type) {
0ec5d470 10414 case update_general_migration_checkpoint:
095b8088
N
10415 if (update->len < (int)sizeof(struct imsm_update_general_migration_checkpoint))
10416 return 0;
1ade5cc1 10417 dprintf("called for update_general_migration_checkpoint\n");
0ec5d470 10418 break;
abedf5fc
KW
10419 case update_takeover: {
10420 struct imsm_update_takeover *u = (void *)update->buf;
095b8088
N
10421 if (update->len < (int)sizeof(*u))
10422 return 0;
abedf5fc
KW
10423 if (u->direction == R0_TO_R10) {
10424 void **tail = (void **)&update->space_list;
10425 struct imsm_dev *dev = get_imsm_dev(super, u->subarray);
238c0a71 10426 struct imsm_map *map = get_imsm_map(dev, MAP_0);
abedf5fc
KW
10427 int num_members = map->num_members;
10428 void *space;
10429 int size, i;
abedf5fc
KW
10430 /* allocate memory for added disks */
10431 for (i = 0; i < num_members; i++) {
10432 size = sizeof(struct dl);
503975b9 10433 space = xmalloc(size);
abedf5fc
KW
10434 *tail = space;
10435 tail = space;
10436 *tail = NULL;
10437 }
10438 /* allocate memory for new device */
10439 size = sizeof_imsm_dev(super->devlist->dev, 0) +
10440 (num_members * sizeof(__u32));
503975b9
N
10441 space = xmalloc(size);
10442 *tail = space;
10443 tail = space;
10444 *tail = NULL;
10445 len = disks_to_mpb_size(num_members * 2);
abedf5fc
KW
10446 }
10447
10448 break;
10449 }
78b10e66 10450 case update_reshape_container_disks: {
d195167d
AK
10451 /* Every raid device in the container is about to
10452 * gain some more devices, and we will enter a
10453 * reconfiguration.
10454 * So each 'imsm_map' will be bigger, and the imsm_vol
10455 * will now hold 2 of them.
10456 * Thus we need new 'struct imsm_dev' allocations sized
10457 * as sizeof_imsm_dev but with more devices in both maps.
10458 */
10459 struct imsm_update_reshape *u = (void *)update->buf;
10460 struct intel_dev *dl;
10461 void **space_tail = (void**)&update->space_list;
10462
095b8088
N
10463 if (update->len < (int)sizeof(*u))
10464 return 0;
10465
1ade5cc1 10466 dprintf("for update_reshape\n");
d195167d
AK
10467
10468 for (dl = super->devlist; dl; dl = dl->next) {
10469 int size = sizeof_imsm_dev(dl->dev, 1);
10470 void *s;
d677e0b8
AK
10471 if (u->new_raid_disks > u->old_raid_disks)
10472 size += sizeof(__u32)*2*
10473 (u->new_raid_disks - u->old_raid_disks);
503975b9 10474 s = xmalloc(size);
d195167d
AK
10475 *space_tail = s;
10476 space_tail = s;
10477 *space_tail = NULL;
10478 }
10479
10480 len = disks_to_mpb_size(u->new_raid_disks);
10481 dprintf("New anchor length is %llu\n", (unsigned long long)len);
78b10e66
N
10482 break;
10483 }
48c5303a 10484 case update_reshape_migration: {
bc0b9d34
PC
10485 /* for migration level 0->5 we need to add disks
10486 * so the same as for container operation we will copy
10487 * device to the bigger location.
10488 * in memory prepared device and new disk area are prepared
10489 * for usage in process update
10490 */
10491 struct imsm_update_reshape_migration *u = (void *)update->buf;
10492 struct intel_dev *id;
10493 void **space_tail = (void **)&update->space_list;
10494 int size;
10495 void *s;
10496 int current_level = -1;
10497
095b8088
N
10498 if (update->len < (int)sizeof(*u))
10499 return 0;
10500
1ade5cc1 10501 dprintf("for update_reshape\n");
bc0b9d34
PC
10502
10503 /* add space for bigger array in update
10504 */
10505 for (id = super->devlist; id; id = id->next) {
10506 if (id->index == (unsigned)u->subdev) {
10507 size = sizeof_imsm_dev(id->dev, 1);
10508 if (u->new_raid_disks > u->old_raid_disks)
10509 size += sizeof(__u32)*2*
10510 (u->new_raid_disks - u->old_raid_disks);
503975b9 10511 s = xmalloc(size);
bc0b9d34
PC
10512 *space_tail = s;
10513 space_tail = s;
10514 *space_tail = NULL;
10515 break;
10516 }
10517 }
10518 if (update->space_list == NULL)
10519 break;
10520
10521 /* add space for disk in update
10522 */
10523 size = sizeof(struct dl);
503975b9 10524 s = xmalloc(size);
bc0b9d34
PC
10525 *space_tail = s;
10526 space_tail = s;
10527 *space_tail = NULL;
10528
10529 /* add spare device to update
10530 */
10531 for (id = super->devlist ; id; id = id->next)
10532 if (id->index == (unsigned)u->subdev) {
10533 struct imsm_dev *dev;
10534 struct imsm_map *map;
10535
10536 dev = get_imsm_dev(super, u->subdev);
238c0a71 10537 map = get_imsm_map(dev, MAP_0);
bc0b9d34
PC
10538 current_level = map->raid_level;
10539 break;
10540 }
089f9d79 10541 if (u->new_level == 5 && u->new_level != current_level) {
bc0b9d34
PC
10542 struct mdinfo *spares;
10543
10544 spares = get_spares_for_grow(st);
10545 if (spares) {
10546 struct dl *dl;
10547 struct mdinfo *dev;
10548
10549 dev = spares->devs;
10550 if (dev) {
10551 u->new_disks[0] =
10552 makedev(dev->disk.major,
10553 dev->disk.minor);
10554 dl = get_disk_super(super,
10555 dev->disk.major,
10556 dev->disk.minor);
10557 dl->index = u->old_raid_disks;
10558 dev = dev->next;
10559 }
10560 sysfs_free(spares);
10561 }
10562 }
10563 len = disks_to_mpb_size(u->new_raid_disks);
10564 dprintf("New anchor length is %llu\n", (unsigned long long)len);
48c5303a
PC
10565 break;
10566 }
f3871fdc 10567 case update_size_change: {
095b8088
N
10568 if (update->len < (int)sizeof(struct imsm_update_size_change))
10569 return 0;
10570 break;
10571 }
10572 case update_activate_spare: {
10573 if (update->len < (int)sizeof(struct imsm_update_activate_spare))
10574 return 0;
f3871fdc
AK
10575 break;
10576 }
949c47a0
DW
10577 case update_create_array: {
10578 struct imsm_update_create_array *u = (void *) update->buf;
ba2de7ba 10579 struct intel_dev *dv;
54c2c1ea 10580 struct imsm_dev *dev = &u->dev;
238c0a71 10581 struct imsm_map *map = get_imsm_map(dev, MAP_0);
54c2c1ea
DW
10582 struct dl *dl;
10583 struct disk_info *inf;
10584 int i;
10585 int activate = 0;
949c47a0 10586
095b8088
N
10587 if (update->len < (int)sizeof(*u))
10588 return 0;
10589
54c2c1ea
DW
10590 inf = get_disk_info(u);
10591 len = sizeof_imsm_dev(dev, 1);
ba2de7ba 10592 /* allocate a new super->devlist entry */
503975b9
N
10593 dv = xmalloc(sizeof(*dv));
10594 dv->dev = xmalloc(len);
10595 update->space = dv;
949c47a0 10596
54c2c1ea
DW
10597 /* count how many spares will be converted to members */
10598 for (i = 0; i < map->num_members; i++) {
10599 dl = serial_to_dl(inf[i].serial, super);
10600 if (!dl) {
10601 /* hmm maybe it failed?, nothing we can do about
10602 * it here
10603 */
10604 continue;
10605 }
10606 if (count_memberships(dl, super) == 0)
10607 activate++;
10608 }
10609 len += activate * sizeof(struct imsm_disk);
949c47a0 10610 break;
095b8088
N
10611 }
10612 case update_kill_array: {
10613 if (update->len < (int)sizeof(struct imsm_update_kill_array))
10614 return 0;
949c47a0
DW
10615 break;
10616 }
095b8088
N
10617 case update_rename_array: {
10618 if (update->len < (int)sizeof(struct imsm_update_rename_array))
10619 return 0;
10620 break;
10621 }
10622 case update_add_remove_disk:
10623 /* no update->len needed */
10624 break;
bbab0940
TM
10625 case update_prealloc_badblocks_mem:
10626 super->extra_space += sizeof(struct bbm_log) -
10627 get_imsm_bbm_log_size(super->bbm_log);
10628 break;
e6e9dd3f
AP
10629 case update_rwh_policy: {
10630 if (update->len < (int)sizeof(struct imsm_update_rwh_policy))
10631 return 0;
10632 break;
10633 }
095b8088
N
10634 default:
10635 return 0;
949c47a0 10636 }
8273f55e 10637
4d7b1503
DW
10638 /* check if we need a larger metadata buffer */
10639 if (super->next_buf)
10640 buf_len = super->next_len;
10641 else
10642 buf_len = super->len;
10643
bbab0940 10644 if (__le32_to_cpu(mpb->mpb_size) + super->extra_space + len > buf_len) {
4d7b1503
DW
10645 /* ok we need a larger buf than what is currently allocated
10646 * if this allocation fails process_update will notice that
10647 * ->next_len is set and ->next_buf is NULL
10648 */
bbab0940
TM
10649 buf_len = ROUND_UP(__le32_to_cpu(mpb->mpb_size) +
10650 super->extra_space + len, sector_size);
4d7b1503
DW
10651 if (super->next_buf)
10652 free(super->next_buf);
10653
10654 super->next_len = buf_len;
f36a9ecd 10655 if (posix_memalign(&super->next_buf, sector_size, buf_len) == 0)
1f45a8ad
DW
10656 memset(super->next_buf, 0, buf_len);
10657 else
4d7b1503
DW
10658 super->next_buf = NULL;
10659 }
5fe6f031 10660 return 1;
8273f55e
DW
10661}
10662
ae6aad82 10663/* must be called while manager is quiesced */
f21e18ca 10664static void imsm_delete(struct intel_super *super, struct dl **dlp, unsigned index)
ae6aad82
DW
10665{
10666 struct imsm_super *mpb = super->anchor;
ae6aad82
DW
10667 struct dl *iter;
10668 struct imsm_dev *dev;
10669 struct imsm_map *map;
4c9e8c1e 10670 unsigned int i, j, num_members;
fb12a745 10671 __u32 ord, ord_map0;
4c9e8c1e 10672 struct bbm_log *log = super->bbm_log;
ae6aad82 10673
1ade5cc1 10674 dprintf("deleting device[%d] from imsm_super\n", index);
ae6aad82
DW
10675
10676 /* shift all indexes down one */
10677 for (iter = super->disks; iter; iter = iter->next)
f21e18ca 10678 if (iter->index > (int)index)
ae6aad82 10679 iter->index--;
47ee5a45 10680 for (iter = super->missing; iter; iter = iter->next)
f21e18ca 10681 if (iter->index > (int)index)
47ee5a45 10682 iter->index--;
ae6aad82
DW
10683
10684 for (i = 0; i < mpb->num_raid_devs; i++) {
10685 dev = get_imsm_dev(super, i);
238c0a71 10686 map = get_imsm_map(dev, MAP_0);
24565c9a
DW
10687 num_members = map->num_members;
10688 for (j = 0; j < num_members; j++) {
10689 /* update ord entries being careful not to propagate
10690 * ord-flags to the first map
10691 */
238c0a71 10692 ord = get_imsm_ord_tbl_ent(dev, j, MAP_X);
fb12a745 10693 ord_map0 = get_imsm_ord_tbl_ent(dev, j, MAP_0);
ae6aad82 10694
24565c9a
DW
10695 if (ord_to_idx(ord) <= index)
10696 continue;
ae6aad82 10697
238c0a71 10698 map = get_imsm_map(dev, MAP_0);
fb12a745 10699 set_imsm_ord_tbl_ent(map, j, ord_map0 - 1);
238c0a71 10700 map = get_imsm_map(dev, MAP_1);
24565c9a
DW
10701 if (map)
10702 set_imsm_ord_tbl_ent(map, j, ord - 1);
ae6aad82
DW
10703 }
10704 }
10705
4c9e8c1e
TM
10706 for (i = 0; i < log->entry_count; i++) {
10707 struct bbm_log_entry *entry = &log->marked_block_entries[i];
10708
10709 if (entry->disk_ordinal <= index)
10710 continue;
10711 entry->disk_ordinal--;
10712 }
10713
ae6aad82
DW
10714 mpb->num_disks--;
10715 super->updates_pending++;
24565c9a
DW
10716 if (*dlp) {
10717 struct dl *dl = *dlp;
10718
10719 *dlp = (*dlp)->next;
3a85bf0e 10720 __free_imsm_disk(dl, 1);
24565c9a 10721 }
ae6aad82 10722}
9a717282 10723
9a717282
AK
10724static int imsm_get_allowed_degradation(int level, int raid_disks,
10725 struct intel_super *super,
10726 struct imsm_dev *dev)
10727{
10728 switch (level) {
bf5cf7c7 10729 case 1:
9a717282
AK
10730 case 10:{
10731 int ret_val = 0;
10732 struct imsm_map *map;
10733 int i;
10734
10735 ret_val = raid_disks/2;
10736 /* check map if all disks pairs not failed
10737 * in both maps
10738 */
238c0a71 10739 map = get_imsm_map(dev, MAP_0);
9a717282
AK
10740 for (i = 0; i < ret_val; i++) {
10741 int degradation = 0;
10742 if (get_imsm_disk(super, i) == NULL)
10743 degradation++;
10744 if (get_imsm_disk(super, i + 1) == NULL)
10745 degradation++;
10746 if (degradation == 2)
10747 return 0;
10748 }
238c0a71 10749 map = get_imsm_map(dev, MAP_1);
9a717282
AK
10750 /* if there is no second map
10751 * result can be returned
10752 */
10753 if (map == NULL)
10754 return ret_val;
10755 /* check degradation in second map
10756 */
10757 for (i = 0; i < ret_val; i++) {
10758 int degradation = 0;
10759 if (get_imsm_disk(super, i) == NULL)
10760 degradation++;
10761 if (get_imsm_disk(super, i + 1) == NULL)
10762 degradation++;
10763 if (degradation == 2)
10764 return 0;
10765 }
10766 return ret_val;
10767 }
10768 case 5:
10769 return 1;
10770 case 6:
10771 return 2;
10772 default:
10773 return 0;
10774 }
10775}
10776
d31ad643
PB
10777/*******************************************************************************
10778 * Function: validate_container_imsm
10779 * Description: This routine validates container after assemble,
10780 * eg. if devices in container are under the same controller.
10781 *
10782 * Parameters:
10783 * info : linked list with info about devices used in array
10784 * Returns:
10785 * 1 : HBA mismatch
10786 * 0 : Success
10787 ******************************************************************************/
10788int validate_container_imsm(struct mdinfo *info)
10789{
420dafcd 10790 if (check_no_platform())
6b781d33 10791 return 0;
d31ad643 10792
6b781d33
AP
10793 struct sys_dev *idev;
10794 struct sys_dev *hba = NULL;
10795 struct sys_dev *intel_devices = find_intel_devices();
10796 char *dev_path = devt_to_devpath(makedev(info->disk.major,
7c798f87 10797 info->disk.minor), 1, NULL);
6b781d33
AP
10798
10799 for (idev = intel_devices; idev; idev = idev->next) {
10800 if (dev_path && strstr(dev_path, idev->path)) {
10801 hba = idev;
10802 break;
d31ad643 10803 }
6b781d33
AP
10804 }
10805 if (dev_path)
d31ad643
PB
10806 free(dev_path);
10807
6b781d33
AP
10808 if (!hba) {
10809 pr_err("WARNING - Cannot detect HBA for device %s!\n",
10810 devid2kname(makedev(info->disk.major, info->disk.minor)));
10811 return 1;
10812 }
10813
10814 const struct imsm_orom *orom = get_orom_by_device_id(hba->dev_id);
10815 struct mdinfo *dev;
10816
10817 for (dev = info->next; dev; dev = dev->next) {
7c798f87
MT
10818 dev_path = devt_to_devpath(makedev(dev->disk.major,
10819 dev->disk.minor), 1, NULL);
6b781d33
AP
10820
10821 struct sys_dev *hba2 = NULL;
10822 for (idev = intel_devices; idev; idev = idev->next) {
10823 if (dev_path && strstr(dev_path, idev->path)) {
10824 hba2 = idev;
10825 break;
d31ad643
PB
10826 }
10827 }
6b781d33
AP
10828 if (dev_path)
10829 free(dev_path);
10830
10831 const struct imsm_orom *orom2 = hba2 == NULL ? NULL :
10832 get_orom_by_device_id(hba2->dev_id);
10833
10834 if (hba2 && hba->type != hba2->type) {
10835 pr_err("WARNING - HBAs of devices do not match %s != %s\n",
10836 get_sys_dev_type(hba->type), get_sys_dev_type(hba2->type));
10837 return 1;
10838 }
10839
07cb1e57 10840 if (orom != orom2) {
6b781d33
AP
10841 pr_err("WARNING - IMSM container assembled with disks under different HBAs!\n"
10842 " This operation is not supported and can lead to data loss.\n");
10843 return 1;
10844 }
10845
10846 if (!orom) {
10847 pr_err("WARNING - IMSM container assembled with disks under HBAs without IMSM platform support!\n"
10848 " This operation is not supported and can lead to data loss.\n");
10849 return 1;
10850 }
d31ad643 10851 }
6b781d33 10852
d31ad643
PB
10853 return 0;
10854}
32141c17 10855
6f50473f
TM
10856/*******************************************************************************
10857* Function: imsm_record_badblock
10858* Description: This routine stores new bad block record in BBM log
10859*
10860* Parameters:
10861* a : array containing a bad block
10862* slot : disk number containing a bad block
10863* sector : bad block sector
10864* length : bad block sectors range
10865* Returns:
10866* 1 : Success
10867* 0 : Error
10868******************************************************************************/
10869static int imsm_record_badblock(struct active_array *a, int slot,
10870 unsigned long long sector, int length)
10871{
10872 struct intel_super *super = a->container->sb;
10873 int ord;
10874 int ret;
10875
10876 ord = imsm_disk_slot_to_ord(a, slot);
10877 if (ord < 0)
10878 return 0;
10879
10880 ret = record_new_badblock(super->bbm_log, ord_to_idx(ord), sector,
10881 length);
10882 if (ret)
10883 super->updates_pending++;
10884
10885 return ret;
10886}
c07a5a4f
TM
10887/*******************************************************************************
10888* Function: imsm_clear_badblock
10889* Description: This routine clears bad block record from BBM log
10890*
10891* Parameters:
10892* a : array containing a bad block
10893* slot : disk number containing a bad block
10894* sector : bad block sector
10895* length : bad block sectors range
10896* Returns:
10897* 1 : Success
10898* 0 : Error
10899******************************************************************************/
10900static int imsm_clear_badblock(struct active_array *a, int slot,
10901 unsigned long long sector, int length)
10902{
10903 struct intel_super *super = a->container->sb;
10904 int ord;
10905 int ret;
10906
10907 ord = imsm_disk_slot_to_ord(a, slot);
10908 if (ord < 0)
10909 return 0;
10910
10911 ret = clear_badblock(super->bbm_log, ord_to_idx(ord), sector, length);
10912 if (ret)
10913 super->updates_pending++;
10914
10915 return ret;
10916}
928f1424
TM
10917/*******************************************************************************
10918* Function: imsm_get_badblocks
10919* Description: This routine get list of bad blocks for an array
10920*
10921* Parameters:
10922* a : array
10923* slot : disk number
10924* Returns:
10925* bb : structure containing bad blocks
10926* NULL : error
10927******************************************************************************/
10928static struct md_bb *imsm_get_badblocks(struct active_array *a, int slot)
10929{
10930 int inst = a->info.container_member;
10931 struct intel_super *super = a->container->sb;
10932 struct imsm_dev *dev = get_imsm_dev(super, inst);
10933 struct imsm_map *map = get_imsm_map(dev, MAP_0);
10934 int ord;
10935
10936 ord = imsm_disk_slot_to_ord(a, slot);
10937 if (ord < 0)
10938 return NULL;
10939
10940 get_volume_badblocks(super->bbm_log, ord_to_idx(ord), pba_of_lba0(map),
44490938 10941 per_dev_array_size(map), &super->bb);
928f1424
TM
10942
10943 return &super->bb;
10944}
27156a57
TM
10945/*******************************************************************************
10946* Function: examine_badblocks_imsm
10947* Description: Prints list of bad blocks on a disk to the standard output
10948*
10949* Parameters:
10950* st : metadata handler
10951* fd : open file descriptor for device
10952* devname : device name
10953* Returns:
10954* 0 : Success
10955* 1 : Error
10956******************************************************************************/
10957static int examine_badblocks_imsm(struct supertype *st, int fd, char *devname)
10958{
10959 struct intel_super *super = st->sb;
10960 struct bbm_log *log = super->bbm_log;
10961 struct dl *d = NULL;
10962 int any = 0;
10963
10964 for (d = super->disks; d ; d = d->next) {
10965 if (strcmp(d->devname, devname) == 0)
10966 break;
10967 }
10968
10969 if ((d == NULL) || (d->index < 0)) { /* serial mismatch probably */
10970 pr_err("%s doesn't appear to be part of a raid array\n",
10971 devname);
10972 return 1;
10973 }
10974
10975 if (log != NULL) {
10976 unsigned int i;
10977 struct bbm_log_entry *entry = &log->marked_block_entries[0];
10978
10979 for (i = 0; i < log->entry_count; i++) {
10980 if (entry[i].disk_ordinal == d->index) {
10981 unsigned long long sector = __le48_to_cpu(
10982 &entry[i].defective_block_start);
10983 int cnt = entry[i].marked_count + 1;
10984
10985 if (!any) {
10986 printf("Bad-blocks on %s:\n", devname);
10987 any = 1;
10988 }
10989
10990 printf("%20llu for %d sectors\n", sector, cnt);
10991 }
10992 }
10993 }
10994
10995 if (!any)
10996 printf("No bad-blocks list configured on %s\n", devname);
10997
10998 return 0;
10999}
687629c2
AK
11000/*******************************************************************************
11001 * Function: init_migr_record_imsm
11002 * Description: Function inits imsm migration record
11003 * Parameters:
11004 * super : imsm internal array info
11005 * dev : device under migration
11006 * info : general array info to find the smallest device
11007 * Returns:
11008 * none
11009 ******************************************************************************/
11010void init_migr_record_imsm(struct supertype *st, struct imsm_dev *dev,
11011 struct mdinfo *info)
11012{
11013 struct intel_super *super = st->sb;
11014 struct migr_record *migr_rec = super->migr_rec;
11015 int new_data_disks;
11016 unsigned long long dsize, dev_sectors;
11017 long long unsigned min_dev_sectors = -1LLU;
238c0a71
AK
11018 struct imsm_map *map_dest = get_imsm_map(dev, MAP_0);
11019 struct imsm_map *map_src = get_imsm_map(dev, MAP_1);
687629c2 11020 unsigned long long num_migr_units;
3ef4403c 11021 unsigned long long array_blocks;
2f86fda3 11022 struct dl *dl_disk = NULL;
687629c2
AK
11023
11024 memset(migr_rec, 0, sizeof(struct migr_record));
11025 migr_rec->family_num = __cpu_to_le32(super->anchor->family_num);
11026
11027 /* only ascending reshape supported now */
11028 migr_rec->ascending_migr = __cpu_to_le32(1);
11029
11030 migr_rec->dest_depth_per_unit = GEN_MIGR_AREA_SIZE /
11031 max(map_dest->blocks_per_strip, map_src->blocks_per_strip);
e1742195
AK
11032 migr_rec->dest_depth_per_unit *=
11033 max(map_dest->blocks_per_strip, map_src->blocks_per_strip);
9529d343 11034 new_data_disks = imsm_num_data_members(map_dest);
687629c2
AK
11035 migr_rec->blocks_per_unit =
11036 __cpu_to_le32(migr_rec->dest_depth_per_unit * new_data_disks);
11037 migr_rec->dest_depth_per_unit =
11038 __cpu_to_le32(migr_rec->dest_depth_per_unit);
3ef4403c 11039 array_blocks = info->component_size * new_data_disks;
687629c2
AK
11040 num_migr_units =
11041 array_blocks / __le32_to_cpu(migr_rec->blocks_per_unit);
11042
11043 if (array_blocks % __le32_to_cpu(migr_rec->blocks_per_unit))
11044 num_migr_units++;
9f421827 11045 set_num_migr_units(migr_rec, num_migr_units);
687629c2
AK
11046
11047 migr_rec->post_migr_vol_cap = dev->size_low;
11048 migr_rec->post_migr_vol_cap_hi = dev->size_high;
11049
687629c2 11050 /* Find the smallest dev */
2f86fda3
MT
11051 for (dl_disk = super->disks; dl_disk ; dl_disk = dl_disk->next) {
11052 /* ignore spares in container */
11053 if (dl_disk->index < 0)
687629c2 11054 continue;
2f86fda3 11055 get_dev_size(dl_disk->fd, NULL, &dsize);
687629c2
AK
11056 dev_sectors = dsize / 512;
11057 if (dev_sectors < min_dev_sectors)
11058 min_dev_sectors = dev_sectors;
687629c2 11059 }
9f421827 11060 set_migr_chkp_area_pba(migr_rec, min_dev_sectors -
687629c2
AK
11061 RAID_DISK_RESERVED_BLOCKS_IMSM_HI);
11062
11063 write_imsm_migr_rec(st);
11064
11065 return;
11066}
11067
11068/*******************************************************************************
11069 * Function: save_backup_imsm
11070 * Description: Function saves critical data stripes to Migration Copy Area
11071 * and updates the current migration unit status.
11072 * Use restore_stripes() to form a destination stripe,
11073 * and to write it to the Copy Area.
11074 * Parameters:
11075 * st : supertype information
aea93171 11076 * dev : imsm device that backup is saved for
687629c2
AK
11077 * info : general array info
11078 * buf : input buffer
687629c2
AK
11079 * length : length of data to backup (blocks_per_unit)
11080 * Returns:
11081 * 0 : success
11082 *, -1 : fail
11083 ******************************************************************************/
11084int save_backup_imsm(struct supertype *st,
11085 struct imsm_dev *dev,
11086 struct mdinfo *info,
11087 void *buf,
687629c2
AK
11088 int length)
11089{
11090 int rv = -1;
11091 struct intel_super *super = st->sb;
687629c2 11092 int i;
238c0a71 11093 struct imsm_map *map_dest = get_imsm_map(dev, MAP_0);
687629c2 11094 int new_disks = map_dest->num_members;
ab724b98 11095 int dest_layout = 0;
4389ce73
MT
11096 int dest_chunk, targets[new_disks];
11097 unsigned long long start, target_offsets[new_disks];
9529d343 11098 int data_disks = imsm_num_data_members(map_dest);
687629c2 11099
2f86fda3
MT
11100 for (i = 0; i < new_disks; i++) {
11101 struct dl *dl_disk = get_imsm_dl_disk(super, i);
4389ce73
MT
11102 if (dl_disk && is_fd_valid(dl_disk->fd))
11103 targets[i] = dl_disk->fd;
11104 else
11105 goto abort;
2f86fda3 11106 }
7e45b550 11107
d1877f69 11108 start = info->reshape_progress * 512;
687629c2 11109 for (i = 0; i < new_disks; i++) {
9f421827 11110 target_offsets[i] = migr_chkp_area_pba(super->migr_rec) * 512;
d1877f69
AK
11111 /* move back copy area adderss, it will be moved forward
11112 * in restore_stripes() using start input variable
11113 */
11114 target_offsets[i] -= start/data_disks;
687629c2
AK
11115 }
11116
68eb8bc6 11117 dest_layout = imsm_level_to_layout(map_dest->raid_level);
ab724b98
AK
11118 dest_chunk = __le16_to_cpu(map_dest->blocks_per_strip) * 512;
11119
687629c2
AK
11120 if (restore_stripes(targets, /* list of dest devices */
11121 target_offsets, /* migration record offsets */
11122 new_disks,
ab724b98
AK
11123 dest_chunk,
11124 map_dest->raid_level,
11125 dest_layout,
11126 -1, /* source backup file descriptor */
11127 0, /* input buf offset
11128 * always 0 buf is already offseted */
d1877f69 11129 start,
687629c2
AK
11130 length,
11131 buf) != 0) {
e7b84f9d 11132 pr_err("Error restoring stripes\n");
687629c2
AK
11133 goto abort;
11134 }
11135
11136 rv = 0;
11137
11138abort:
687629c2
AK
11139 return rv;
11140}
11141
11142/*******************************************************************************
11143 * Function: save_checkpoint_imsm
11144 * Description: Function called for current unit status update
11145 * in the migration record. It writes it to disk.
11146 * Parameters:
11147 * super : imsm internal array info
11148 * info : general array info
11149 * Returns:
11150 * 0: success
11151 * 1: failure
0228d92c
AK
11152 * 2: failure, means no valid migration record
11153 * / no general migration in progress /
687629c2
AK
11154 ******************************************************************************/
11155int save_checkpoint_imsm(struct supertype *st, struct mdinfo *info, int state)
11156{
11157 struct intel_super *super = st->sb;
f8b72ef5
AK
11158 unsigned long long blocks_per_unit;
11159 unsigned long long curr_migr_unit;
11160
2f86fda3 11161 if (load_imsm_migr_rec(super) != 0) {
7a862a02 11162 dprintf("imsm: ERROR: Cannot read migration record for checkpoint save.\n");
2e062e82
AK
11163 return 1;
11164 }
11165
f8b72ef5
AK
11166 blocks_per_unit = __le32_to_cpu(super->migr_rec->blocks_per_unit);
11167 if (blocks_per_unit == 0) {
0228d92c
AK
11168 dprintf("imsm: no migration in progress.\n");
11169 return 2;
687629c2 11170 }
f8b72ef5
AK
11171 curr_migr_unit = info->reshape_progress / blocks_per_unit;
11172 /* check if array is alligned to copy area
11173 * if it is not alligned, add one to current migration unit value
11174 * this can happend on array reshape finish only
11175 */
11176 if (info->reshape_progress % blocks_per_unit)
11177 curr_migr_unit++;
687629c2 11178
9f421827 11179 set_current_migr_unit(super->migr_rec, curr_migr_unit);
687629c2 11180 super->migr_rec->rec_status = __cpu_to_le32(state);
9f421827
PB
11181 set_migr_dest_1st_member_lba(super->migr_rec,
11182 super->migr_rec->dest_depth_per_unit * curr_migr_unit);
11183
687629c2 11184 if (write_imsm_migr_rec(st) < 0) {
7a862a02 11185 dprintf("imsm: Cannot write migration record outside backup area\n");
687629c2
AK
11186 return 1;
11187 }
11188
11189 return 0;
11190}
11191
276d77db
AK
11192/*******************************************************************************
11193 * Function: recover_backup_imsm
11194 * Description: Function recovers critical data from the Migration Copy Area
11195 * while assembling an array.
11196 * Parameters:
11197 * super : imsm internal array info
11198 * info : general array info
11199 * Returns:
11200 * 0 : success (or there is no data to recover)
11201 * 1 : fail
11202 ******************************************************************************/
11203int recover_backup_imsm(struct supertype *st, struct mdinfo *info)
11204{
11205 struct intel_super *super = st->sb;
11206 struct migr_record *migr_rec = super->migr_rec;
594dc1b8 11207 struct imsm_map *map_dest;
276d77db
AK
11208 struct intel_dev *id = NULL;
11209 unsigned long long read_offset;
11210 unsigned long long write_offset;
11211 unsigned unit_len;
2f86fda3 11212 int new_disks, err;
276d77db
AK
11213 char *buf = NULL;
11214 int retval = 1;
f36a9ecd 11215 unsigned int sector_size = super->sector_size;
4036e7ee
MT
11216 unsigned long long curr_migr_unit = current_migr_unit(migr_rec);
11217 unsigned long long num_migr_units = get_num_migr_units(migr_rec);
276d77db 11218 char buffer[20];
6c3560c0 11219 int skipped_disks = 0;
2f86fda3 11220 struct dl *dl_disk;
276d77db
AK
11221
11222 err = sysfs_get_str(info, NULL, "array_state", (char *)buffer, 20);
11223 if (err < 1)
11224 return 1;
11225
11226 /* recover data only during assemblation */
11227 if (strncmp(buffer, "inactive", 8) != 0)
11228 return 0;
11229 /* no data to recover */
11230 if (__le32_to_cpu(migr_rec->rec_status) == UNIT_SRC_NORMAL)
11231 return 0;
11232 if (curr_migr_unit >= num_migr_units)
11233 return 1;
11234
11235 /* find device during reshape */
11236 for (id = super->devlist; id; id = id->next)
11237 if (is_gen_migration(id->dev))
11238 break;
11239 if (id == NULL)
11240 return 1;
11241
238c0a71 11242 map_dest = get_imsm_map(id->dev, MAP_0);
276d77db
AK
11243 new_disks = map_dest->num_members;
11244
9f421827 11245 read_offset = migr_chkp_area_pba(migr_rec) * 512;
276d77db 11246
9f421827 11247 write_offset = (migr_dest_1st_member_lba(migr_rec) +
5551b113 11248 pba_of_lba0(map_dest)) * 512;
276d77db
AK
11249
11250 unit_len = __le32_to_cpu(migr_rec->dest_depth_per_unit) * 512;
f36a9ecd 11251 if (posix_memalign((void **)&buf, sector_size, unit_len) != 0)
276d77db 11252 goto abort;
276d77db 11253
2f86fda3
MT
11254 for (dl_disk = super->disks; dl_disk; dl_disk = dl_disk->next) {
11255 if (dl_disk->index < 0)
11256 continue;
276d77db 11257
4389ce73 11258 if (!is_fd_valid(dl_disk->fd)) {
6c3560c0
AK
11259 skipped_disks++;
11260 continue;
11261 }
2f86fda3 11262 if (lseek64(dl_disk->fd, read_offset, SEEK_SET) < 0) {
e7b84f9d
N
11263 pr_err("Cannot seek to block: %s\n",
11264 strerror(errno));
137debce
AK
11265 skipped_disks++;
11266 continue;
276d77db 11267 }
83b3de77 11268 if (read(dl_disk->fd, buf, unit_len) != (ssize_t)unit_len) {
e7b84f9d
N
11269 pr_err("Cannot read copy area block: %s\n",
11270 strerror(errno));
137debce
AK
11271 skipped_disks++;
11272 continue;
276d77db 11273 }
2f86fda3 11274 if (lseek64(dl_disk->fd, write_offset, SEEK_SET) < 0) {
e7b84f9d
N
11275 pr_err("Cannot seek to block: %s\n",
11276 strerror(errno));
137debce
AK
11277 skipped_disks++;
11278 continue;
276d77db 11279 }
83b3de77 11280 if (write(dl_disk->fd, buf, unit_len) != (ssize_t)unit_len) {
e7b84f9d
N
11281 pr_err("Cannot restore block: %s\n",
11282 strerror(errno));
137debce
AK
11283 skipped_disks++;
11284 continue;
276d77db
AK
11285 }
11286 }
11287
137debce
AK
11288 if (skipped_disks > imsm_get_allowed_degradation(info->new_level,
11289 new_disks,
11290 super,
11291 id->dev)) {
7a862a02 11292 pr_err("Cannot restore data from backup. Too many failed disks\n");
6c3560c0
AK
11293 goto abort;
11294 }
11295
befb629b
AK
11296 if (save_checkpoint_imsm(st, info, UNIT_SRC_NORMAL)) {
11297 /* ignore error == 2, this can mean end of reshape here
11298 */
7a862a02 11299 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL) during restart\n");
befb629b 11300 } else
276d77db 11301 retval = 0;
276d77db
AK
11302
11303abort:
276d77db
AK
11304 free(buf);
11305 return retval;
11306}
11307
2cda7640
ML
11308static char disk_by_path[] = "/dev/disk/by-path/";
11309
11310static const char *imsm_get_disk_controller_domain(const char *path)
11311{
2cda7640 11312 char disk_path[PATH_MAX];
96234762
LM
11313 char *drv=NULL;
11314 struct stat st;
2cda7640 11315
dd5ab402 11316 strncpy(disk_path, disk_by_path, PATH_MAX);
96234762
LM
11317 strncat(disk_path, path, PATH_MAX - strlen(disk_path) - 1);
11318 if (stat(disk_path, &st) == 0) {
11319 struct sys_dev* hba;
594dc1b8 11320 char *path;
96234762 11321
7c798f87 11322 path = devt_to_devpath(st.st_rdev, 1, NULL);
96234762
LM
11323 if (path == NULL)
11324 return "unknown";
11325 hba = find_disk_attached_hba(-1, path);
11326 if (hba && hba->type == SYS_DEV_SAS)
11327 drv = "isci";
75350d87 11328 else if (hba && (hba->type == SYS_DEV_SATA || hba->type == SYS_DEV_SATA_VMD))
96234762 11329 drv = "ahci";
c6839718
MT
11330 else if (hba && hba->type == SYS_DEV_VMD)
11331 drv = "vmd";
11332 else if (hba && hba->type == SYS_DEV_NVME)
11333 drv = "nvme";
1011e834 11334 else
96234762
LM
11335 drv = "unknown";
11336 dprintf("path: %s hba: %s attached: %s\n",
11337 path, (hba) ? hba->path : "NULL", drv);
11338 free(path);
2cda7640 11339 }
96234762 11340 return drv;
2cda7640
ML
11341}
11342
4dd2df09 11343static char *imsm_find_array_devnm_by_subdev(int subdev, char *container)
78b10e66 11344{
4dd2df09 11345 static char devnm[32];
78b10e66
N
11346 char subdev_name[20];
11347 struct mdstat_ent *mdstat;
11348
11349 sprintf(subdev_name, "%d", subdev);
11350 mdstat = mdstat_by_subdev(subdev_name, container);
11351 if (!mdstat)
4dd2df09 11352 return NULL;
78b10e66 11353
4dd2df09 11354 strcpy(devnm, mdstat->devnm);
78b10e66 11355 free_mdstat(mdstat);
4dd2df09 11356 return devnm;
78b10e66
N
11357}
11358
11359static int imsm_reshape_is_allowed_on_container(struct supertype *st,
11360 struct geo_params *geo,
fbf3d202
AK
11361 int *old_raid_disks,
11362 int direction)
78b10e66 11363{
694575e7
KW
11364 /* currently we only support increasing the number of devices
11365 * for a container. This increases the number of device for each
11366 * member array. They must all be RAID0 or RAID5.
11367 */
78b10e66
N
11368 int ret_val = 0;
11369 struct mdinfo *info, *member;
11370 int devices_that_can_grow = 0;
11371
7a862a02 11372 dprintf("imsm: imsm_reshape_is_allowed_on_container(ENTER): st->devnm = (%s)\n", st->devnm);
78b10e66 11373
d04f65f4 11374 if (geo->size > 0 ||
78b10e66
N
11375 geo->level != UnSet ||
11376 geo->layout != UnSet ||
11377 geo->chunksize != 0 ||
11378 geo->raid_disks == UnSet) {
7a862a02 11379 dprintf("imsm: Container operation is allowed for raid disks number change only.\n");
78b10e66
N
11380 return ret_val;
11381 }
11382
fbf3d202 11383 if (direction == ROLLBACK_METADATA_CHANGES) {
7a862a02 11384 dprintf("imsm: Metadata changes rollback is not supported for container operation.\n");
fbf3d202
AK
11385 return ret_val;
11386 }
11387
78b10e66
N
11388 info = container_content_imsm(st, NULL);
11389 for (member = info; member; member = member->next) {
4dd2df09 11390 char *result;
78b10e66
N
11391
11392 dprintf("imsm: checking device_num: %i\n",
11393 member->container_member);
11394
d7d205bd 11395 if (geo->raid_disks <= member->array.raid_disks) {
78b10e66
N
11396 /* we work on container for Online Capacity Expansion
11397 * only so raid_disks has to grow
11398 */
7a862a02 11399 dprintf("imsm: for container operation raid disks increase is required\n");
78b10e66
N
11400 break;
11401 }
11402
089f9d79 11403 if (info->array.level != 0 && info->array.level != 5) {
78b10e66
N
11404 /* we cannot use this container with other raid level
11405 */
7a862a02 11406 dprintf("imsm: for container operation wrong raid level (%i) detected\n",
78b10e66
N
11407 info->array.level);
11408 break;
11409 } else {
11410 /* check for platform support
11411 * for this raid level configuration
11412 */
11413 struct intel_super *super = st->sb;
11414 if (!is_raid_level_supported(super->orom,
11415 member->array.level,
11416 geo->raid_disks)) {
7a862a02 11417 dprintf("platform does not support raid%d with %d disk%s\n",
78b10e66
N
11418 info->array.level,
11419 geo->raid_disks,
11420 geo->raid_disks > 1 ? "s" : "");
11421 break;
11422 }
2a4a08e7
AK
11423 /* check if component size is aligned to chunk size
11424 */
11425 if (info->component_size %
11426 (info->array.chunk_size/512)) {
7a862a02 11427 dprintf("Component size is not aligned to chunk size\n");
2a4a08e7
AK
11428 break;
11429 }
78b10e66
N
11430 }
11431
11432 if (*old_raid_disks &&
11433 info->array.raid_disks != *old_raid_disks)
11434 break;
11435 *old_raid_disks = info->array.raid_disks;
11436
11437 /* All raid5 and raid0 volumes in container
11438 * have to be ready for Online Capacity Expansion
11439 * so they need to be assembled. We have already
11440 * checked that no recovery etc is happening.
11441 */
4dd2df09
N
11442 result = imsm_find_array_devnm_by_subdev(member->container_member,
11443 st->container_devnm);
11444 if (result == NULL) {
78b10e66
N
11445 dprintf("imsm: cannot find array\n");
11446 break;
11447 }
11448 devices_that_can_grow++;
11449 }
11450 sysfs_free(info);
11451 if (!member && devices_that_can_grow)
11452 ret_val = 1;
11453
11454 if (ret_val)
1ade5cc1 11455 dprintf("Container operation allowed\n");
78b10e66 11456 else
1ade5cc1 11457 dprintf("Error: %i\n", ret_val);
78b10e66
N
11458
11459 return ret_val;
11460}
11461
11462/* Function: get_spares_for_grow
11463 * Description: Allocates memory and creates list of spare devices
1011e834 11464 * avaliable in container. Checks if spare drive size is acceptable.
78b10e66
N
11465 * Parameters: Pointer to the supertype structure
11466 * Returns: Pointer to the list of spare devices (mdinfo structure) on success,
1011e834 11467 * NULL if fail
78b10e66
N
11468 */
11469static struct mdinfo *get_spares_for_grow(struct supertype *st)
11470{
fbfdcb06
AO
11471 struct spare_criteria sc;
11472
11473 get_spare_criteria_imsm(st, &sc);
11474 return container_choose_spares(st, &sc, NULL, NULL, NULL, 0);
78b10e66
N
11475}
11476
11477/******************************************************************************
11478 * function: imsm_create_metadata_update_for_reshape
11479 * Function creates update for whole IMSM container.
11480 *
11481 ******************************************************************************/
11482static int imsm_create_metadata_update_for_reshape(
11483 struct supertype *st,
11484 struct geo_params *geo,
11485 int old_raid_disks,
11486 struct imsm_update_reshape **updatep)
11487{
11488 struct intel_super *super = st->sb;
11489 struct imsm_super *mpb = super->anchor;
594dc1b8
JS
11490 int update_memory_size;
11491 struct imsm_update_reshape *u;
11492 struct mdinfo *spares;
78b10e66 11493 int i;
594dc1b8 11494 int delta_disks;
bbd24d86 11495 struct mdinfo *dev;
78b10e66 11496
1ade5cc1 11497 dprintf("(enter) raid_disks = %i\n", geo->raid_disks);
78b10e66
N
11498
11499 delta_disks = geo->raid_disks - old_raid_disks;
11500
11501 /* size of all update data without anchor */
11502 update_memory_size = sizeof(struct imsm_update_reshape);
11503
11504 /* now add space for spare disks that we need to add. */
11505 update_memory_size += sizeof(u->new_disks[0]) * (delta_disks - 1);
11506
503975b9 11507 u = xcalloc(1, update_memory_size);
78b10e66
N
11508 u->type = update_reshape_container_disks;
11509 u->old_raid_disks = old_raid_disks;
11510 u->new_raid_disks = geo->raid_disks;
11511
11512 /* now get spare disks list
11513 */
11514 spares = get_spares_for_grow(st);
11515
d7be7d87 11516 if (spares == NULL || delta_disks > spares->array.spare_disks) {
7a862a02 11517 pr_err("imsm: ERROR: Cannot get spare devices for %s.\n", geo->dev_name);
e4c72d1d 11518 i = -1;
78b10e66
N
11519 goto abort;
11520 }
11521
11522 /* we have got spares
11523 * update disk list in imsm_disk list table in anchor
11524 */
11525 dprintf("imsm: %i spares are available.\n\n",
11526 spares->array.spare_disks);
11527
bbd24d86 11528 dev = spares->devs;
78b10e66 11529 for (i = 0; i < delta_disks; i++) {
78b10e66
N
11530 struct dl *dl;
11531
bbd24d86
AK
11532 if (dev == NULL)
11533 break;
78b10e66
N
11534 u->new_disks[i] = makedev(dev->disk.major,
11535 dev->disk.minor);
11536 dl = get_disk_super(super, dev->disk.major, dev->disk.minor);
ee4beede
AK
11537 dl->index = mpb->num_disks;
11538 mpb->num_disks++;
bbd24d86 11539 dev = dev->next;
78b10e66 11540 }
78b10e66
N
11541
11542abort:
11543 /* free spares
11544 */
11545 sysfs_free(spares);
11546
d677e0b8 11547 dprintf("imsm: reshape update preparation :");
78b10e66 11548 if (i == delta_disks) {
1ade5cc1 11549 dprintf_cont(" OK\n");
78b10e66
N
11550 *updatep = u;
11551 return update_memory_size;
11552 }
11553 free(u);
1ade5cc1 11554 dprintf_cont(" Error\n");
78b10e66
N
11555
11556 return 0;
11557}
11558
f3871fdc
AK
11559/******************************************************************************
11560 * function: imsm_create_metadata_update_for_size_change()
11561 * Creates update for IMSM array for array size change.
11562 *
11563 ******************************************************************************/
11564static int imsm_create_metadata_update_for_size_change(
11565 struct supertype *st,
11566 struct geo_params *geo,
11567 struct imsm_update_size_change **updatep)
11568{
11569 struct intel_super *super = st->sb;
594dc1b8
JS
11570 int update_memory_size;
11571 struct imsm_update_size_change *u;
f3871fdc 11572
1ade5cc1 11573 dprintf("(enter) New size = %llu\n", geo->size);
f3871fdc
AK
11574
11575 /* size of all update data without anchor */
11576 update_memory_size = sizeof(struct imsm_update_size_change);
11577
503975b9 11578 u = xcalloc(1, update_memory_size);
f3871fdc
AK
11579 u->type = update_size_change;
11580 u->subdev = super->current_vol;
11581 u->new_size = geo->size;
11582
11583 dprintf("imsm: reshape update preparation : OK\n");
11584 *updatep = u;
11585
11586 return update_memory_size;
11587}
11588
48c5303a
PC
11589/******************************************************************************
11590 * function: imsm_create_metadata_update_for_migration()
11591 * Creates update for IMSM array.
11592 *
11593 ******************************************************************************/
11594static int imsm_create_metadata_update_for_migration(
11595 struct supertype *st,
11596 struct geo_params *geo,
11597 struct imsm_update_reshape_migration **updatep)
11598{
11599 struct intel_super *super = st->sb;
594dc1b8 11600 int update_memory_size;
756a15f3 11601 int current_chunk_size;
594dc1b8 11602 struct imsm_update_reshape_migration *u;
756a15f3
MG
11603 struct imsm_dev *dev = get_imsm_dev(super, super->current_vol);
11604 struct imsm_map *map = get_imsm_map(dev, MAP_0);
48c5303a
PC
11605 int previous_level = -1;
11606
1ade5cc1 11607 dprintf("(enter) New Level = %i\n", geo->level);
48c5303a
PC
11608
11609 /* size of all update data without anchor */
11610 update_memory_size = sizeof(struct imsm_update_reshape_migration);
11611
503975b9 11612 u = xcalloc(1, update_memory_size);
48c5303a
PC
11613 u->type = update_reshape_migration;
11614 u->subdev = super->current_vol;
11615 u->new_level = geo->level;
11616 u->new_layout = geo->layout;
11617 u->new_raid_disks = u->old_raid_disks = geo->raid_disks;
11618 u->new_disks[0] = -1;
4bba0439 11619 u->new_chunksize = -1;
48c5303a 11620
756a15f3 11621 current_chunk_size = __le16_to_cpu(map->blocks_per_strip) / 2;
48c5303a 11622
756a15f3
MG
11623 if (geo->chunksize != current_chunk_size) {
11624 u->new_chunksize = geo->chunksize / 1024;
11625 dprintf("imsm: chunk size change from %i to %i\n",
11626 current_chunk_size, u->new_chunksize);
48c5303a 11627 }
756a15f3
MG
11628 previous_level = map->raid_level;
11629
089f9d79 11630 if (geo->level == 5 && previous_level == 0) {
48c5303a
PC
11631 struct mdinfo *spares = NULL;
11632
11633 u->new_raid_disks++;
11634 spares = get_spares_for_grow(st);
089f9d79 11635 if (spares == NULL || spares->array.spare_disks < 1) {
48c5303a
PC
11636 free(u);
11637 sysfs_free(spares);
11638 update_memory_size = 0;
565cc99e 11639 pr_err("cannot get spare device for requested migration\n");
48c5303a
PC
11640 return 0;
11641 }
11642 sysfs_free(spares);
11643 }
11644 dprintf("imsm: reshape update preparation : OK\n");
11645 *updatep = u;
11646
11647 return update_memory_size;
11648}
11649
8dd70bce
AK
11650static void imsm_update_metadata_locally(struct supertype *st,
11651 void *buf, int len)
11652{
11653 struct metadata_update mu;
11654
11655 mu.buf = buf;
11656 mu.len = len;
11657 mu.space = NULL;
11658 mu.space_list = NULL;
11659 mu.next = NULL;
5fe6f031
N
11660 if (imsm_prepare_update(st, &mu))
11661 imsm_process_update(st, &mu);
8dd70bce
AK
11662
11663 while (mu.space_list) {
11664 void **space = mu.space_list;
11665 mu.space_list = *space;
11666 free(space);
11667 }
11668}
78b10e66 11669
cbaa7904
MT
11670/**
11671 * imsm_analyze_expand() - check expand properties and calculate new size.
11672 * @st: imsm supertype.
11673 * @geo: new geometry params.
11674 * @array: array info.
11675 * @direction: reshape direction.
11676 *
11677 * Obtain free space after the &array and verify if expand to requested size is
11678 * possible. If geo->size is set to %MAX_SIZE, assume that max free size is
11679 * requested.
11680 *
11681 * Return:
11682 * On success %IMSM_STATUS_OK is returned, geo->size and geo->raid_disks are
11683 * updated.
11684 * On error, %IMSM_STATUS_ERROR is returned.
11685 */
11686static imsm_status_t imsm_analyze_expand(struct supertype *st,
11687 struct geo_params *geo,
11688 struct mdinfo *array,
11689 int direction)
11690{
11691 struct intel_super *super = st->sb;
11692 struct imsm_dev *dev = get_imsm_dev(super, super->current_vol);
11693 struct imsm_map *map = get_imsm_map(dev, MAP_0);
11694 int data_disks = imsm_num_data_members(map);
11695
11696 unsigned long long current_size;
11697 unsigned long long free_size;
11698 unsigned long long new_size;
11699 unsigned long long max_size;
11700
11701 const int chunk_kib = geo->chunksize / 1024;
11702 imsm_status_t rv;
11703
11704 if (direction == ROLLBACK_METADATA_CHANGES) {
11705 /**
11706 * Accept size for rollback only.
11707 */
11708 new_size = geo->size * 2;
11709 goto success;
11710 }
11711
cbaa7904
MT
11712 if (data_disks == 0) {
11713 pr_err("imsm: Cannot retrieve data disks.\n");
11714 return IMSM_STATUS_ERROR;
11715 }
11716 current_size = array->custom_array_size / data_disks;
11717
aa19fdd4 11718 rv = imsm_get_free_size(super, dev->vol.map->num_members, 0, chunk_kib, &free_size, true);
cbaa7904
MT
11719 if (rv != IMSM_STATUS_OK) {
11720 pr_err("imsm: Cannot find free space for expand.\n");
11721 return IMSM_STATUS_ERROR;
11722 }
11723 max_size = round_member_size_to_mb(free_size + current_size);
11724
11725 if (geo->size == MAX_SIZE)
11726 new_size = max_size;
11727 else
11728 new_size = round_member_size_to_mb(geo->size * 2);
11729
11730 if (new_size == 0) {
11731 pr_err("imsm: Rounded requested size is 0.\n");
11732 return IMSM_STATUS_ERROR;
11733 }
11734
11735 if (new_size > max_size) {
11736 pr_err("imsm: Rounded requested size (%llu) is larger than free space available (%llu).\n",
11737 new_size, max_size);
11738 return IMSM_STATUS_ERROR;
11739 }
11740
11741 if (new_size == current_size) {
11742 pr_err("imsm: Rounded requested size (%llu) is same as current size (%llu).\n",
11743 new_size, current_size);
11744 return IMSM_STATUS_ERROR;
11745 }
11746
11747 if (new_size < current_size) {
11748 pr_err("imsm: Size reduction is not supported, rounded requested size (%llu) is smaller than current (%llu).\n",
11749 new_size, current_size);
11750 return IMSM_STATUS_ERROR;
11751 }
11752
11753success:
11754 dprintf("imsm: New size per member is %llu.\n", new_size);
11755 geo->size = data_disks * new_size;
11756 geo->raid_disks = dev->vol.map->num_members;
11757 return IMSM_STATUS_OK;
11758}
11759
471bceb6 11760/***************************************************************************
694575e7 11761* Function: imsm_analyze_change
471bceb6 11762* Description: Function analyze change for single volume
1011e834 11763* and validate if transition is supported
fbf3d202
AK
11764* Parameters: Geometry parameters, supertype structure,
11765* metadata change direction (apply/rollback)
694575e7 11766* Returns: Operation type code on success, -1 if fail
471bceb6
KW
11767****************************************************************************/
11768enum imsm_reshape_type imsm_analyze_change(struct supertype *st,
fbf3d202
AK
11769 struct geo_params *geo,
11770 int direction)
694575e7 11771{
471bceb6
KW
11772 struct mdinfo info;
11773 int change = -1;
11774 int check_devs = 0;
c21e737b 11775 int chunk;
67a2db32
AK
11776 /* number of added/removed disks in operation result */
11777 int devNumChange = 0;
11778 /* imsm compatible layout value for array geometry verification */
11779 int imsm_layout = -1;
6d4d9ab2 11780 imsm_status_t rv;
471bceb6
KW
11781
11782 getinfo_super_imsm_volume(st, &info, NULL);
089f9d79
JS
11783 if (geo->level != info.array.level && geo->level >= 0 &&
11784 geo->level != UnSet) {
471bceb6
KW
11785 switch (info.array.level) {
11786 case 0:
11787 if (geo->level == 5) {
b5347799 11788 change = CH_MIGRATION;
e13ce846 11789 if (geo->layout != ALGORITHM_LEFT_ASYMMETRIC) {
7a862a02 11790 pr_err("Error. Requested Layout not supported (left-asymmetric layout is supported only)!\n");
e13ce846
AK
11791 change = -1;
11792 goto analyse_change_exit;
11793 }
67a2db32 11794 imsm_layout = geo->layout;
471bceb6 11795 check_devs = 1;
e91a3bad
LM
11796 devNumChange = 1; /* parity disk added */
11797 } else if (geo->level == 10) {
471bceb6
KW
11798 change = CH_TAKEOVER;
11799 check_devs = 1;
e91a3bad 11800 devNumChange = 2; /* two mirrors added */
67a2db32 11801 imsm_layout = 0x102; /* imsm supported layout */
471bceb6 11802 }
dfe77a9e
KW
11803 break;
11804 case 1:
471bceb6
KW
11805 case 10:
11806 if (geo->level == 0) {
11807 change = CH_TAKEOVER;
11808 check_devs = 1;
e91a3bad 11809 devNumChange = -(geo->raid_disks/2);
67a2db32 11810 imsm_layout = 0; /* imsm raid0 layout */
471bceb6
KW
11811 }
11812 break;
11813 }
11814 if (change == -1) {
7a862a02 11815 pr_err("Error. Level Migration from %d to %d not supported!\n",
e7b84f9d 11816 info.array.level, geo->level);
471bceb6
KW
11817 goto analyse_change_exit;
11818 }
11819 } else
11820 geo->level = info.array.level;
11821
089f9d79
JS
11822 if (geo->layout != info.array.layout &&
11823 (geo->layout != UnSet && geo->layout != -1)) {
b5347799 11824 change = CH_MIGRATION;
089f9d79
JS
11825 if (info.array.layout == 0 && info.array.level == 5 &&
11826 geo->layout == 5) {
471bceb6 11827 /* reshape 5 -> 4 */
089f9d79
JS
11828 } else if (info.array.layout == 5 && info.array.level == 5 &&
11829 geo->layout == 0) {
471bceb6
KW
11830 /* reshape 4 -> 5 */
11831 geo->layout = 0;
11832 geo->level = 5;
11833 } else {
7a862a02 11834 pr_err("Error. Layout Migration from %d to %d not supported!\n",
e7b84f9d 11835 info.array.layout, geo->layout);
471bceb6
KW
11836 change = -1;
11837 goto analyse_change_exit;
11838 }
67a2db32 11839 } else {
471bceb6 11840 geo->layout = info.array.layout;
67a2db32
AK
11841 if (imsm_layout == -1)
11842 imsm_layout = info.array.layout;
11843 }
471bceb6 11844
089f9d79
JS
11845 if (geo->chunksize > 0 && geo->chunksize != UnSet &&
11846 geo->chunksize != info.array.chunk_size) {
2d2b0eb7
MD
11847 if (info.array.level == 10) {
11848 pr_err("Error. Chunk size change for RAID 10 is not supported.\n");
11849 change = -1;
11850 goto analyse_change_exit;
1e9b2c3f
PB
11851 } else if (info.component_size % (geo->chunksize/512)) {
11852 pr_err("New chunk size (%dK) does not evenly divide device size (%lluk). Aborting...\n",
11853 geo->chunksize/1024, info.component_size/2);
11854 change = -1;
11855 goto analyse_change_exit;
2d2b0eb7 11856 }
b5347799 11857 change = CH_MIGRATION;
2d2b0eb7 11858 } else {
471bceb6 11859 geo->chunksize = info.array.chunk_size;
2d2b0eb7 11860 }
471bceb6 11861
cbaa7904 11862 if (geo->size > 0) {
7abc9871 11863 if (change != -1) {
7a862a02 11864 pr_err("Error. Size change should be the only one at a time.\n");
7abc9871
AK
11865 change = -1;
11866 goto analyse_change_exit;
11867 }
6d4d9ab2 11868
cbaa7904 11869 rv = imsm_analyze_expand(st, geo, &info, direction);
6d4d9ab2 11870 if (rv != IMSM_STATUS_OK)
cbaa7904 11871 goto analyse_change_exit;
7abc9871
AK
11872 change = CH_ARRAY_SIZE;
11873 }
cbaa7904
MT
11874
11875 chunk = geo->chunksize / 1024;
471bceb6
KW
11876 if (!validate_geometry_imsm(st,
11877 geo->level,
67a2db32 11878 imsm_layout,
e91a3bad 11879 geo->raid_disks + devNumChange,
c21e737b 11880 &chunk,
af4348dd 11881 geo->size, INVALID_SECTORS,
5308f117 11882 0, 0, info.consistency_policy, 1))
471bceb6
KW
11883 change = -1;
11884
11885 if (check_devs) {
11886 struct intel_super *super = st->sb;
11887 struct imsm_super *mpb = super->anchor;
11888
11889 if (mpb->num_raid_devs > 1) {
f1cc8ab9
LF
11890 pr_err("Error. Cannot perform operation on %s- for this operation "
11891 "it MUST be single array in container\n", geo->dev_name);
471bceb6
KW
11892 change = -1;
11893 }
11894 }
11895
11896analyse_change_exit:
089f9d79
JS
11897 if (direction == ROLLBACK_METADATA_CHANGES &&
11898 (change == CH_MIGRATION || change == CH_TAKEOVER)) {
7a862a02 11899 dprintf("imsm: Metadata changes rollback is not supported for migration and takeover operations.\n");
fbf3d202
AK
11900 change = -1;
11901 }
471bceb6 11902 return change;
694575e7
KW
11903}
11904
bb025c2f
KW
11905int imsm_takeover(struct supertype *st, struct geo_params *geo)
11906{
11907 struct intel_super *super = st->sb;
11908 struct imsm_update_takeover *u;
11909
503975b9 11910 u = xmalloc(sizeof(struct imsm_update_takeover));
bb025c2f
KW
11911
11912 u->type = update_takeover;
11913 u->subarray = super->current_vol;
11914
11915 /* 10->0 transition */
11916 if (geo->level == 0)
11917 u->direction = R10_TO_R0;
11918
0529c688
KW
11919 /* 0->10 transition */
11920 if (geo->level == 10)
11921 u->direction = R0_TO_R10;
11922
bb025c2f
KW
11923 /* update metadata locally */
11924 imsm_update_metadata_locally(st, u,
11925 sizeof(struct imsm_update_takeover));
11926 /* and possibly remotely */
11927 if (st->update_tail)
11928 append_metadata_update(st, u,
11929 sizeof(struct imsm_update_takeover));
11930 else
11931 free(u);
11932
11933 return 0;
11934}
11935
895ffd99
MT
11936/* Flush size update if size calculated by num_data_stripes is higher than
11937 * imsm_dev_size to eliminate differences during reshape.
11938 * Mdmon will recalculate them correctly.
11939 * If subarray index is not set then check whole container.
11940 * Returns:
11941 * 0 - no error occurred
11942 * 1 - error detected
11943 */
11944static int imsm_fix_size_mismatch(struct supertype *st, int subarray_index)
11945{
11946 struct intel_super *super = st->sb;
11947 int tmp = super->current_vol;
11948 int ret_val = 1;
11949 int i;
11950
11951 for (i = 0; i < super->anchor->num_raid_devs; i++) {
11952 if (subarray_index >= 0 && i != subarray_index)
11953 continue;
11954 super->current_vol = i;
11955 struct imsm_dev *dev = get_imsm_dev(super, super->current_vol);
11956 struct imsm_map *map = get_imsm_map(dev, MAP_0);
11957 unsigned int disc_count = imsm_num_data_members(map);
11958 struct geo_params geo;
11959 struct imsm_update_size_change *update;
11960 unsigned long long calc_size = per_dev_array_size(map) * disc_count;
11961 unsigned long long d_size = imsm_dev_size(dev);
11962 int u_size;
11963
42e02e61 11964 if (calc_size == d_size)
895ffd99
MT
11965 continue;
11966
ff904202
MT
11967 /* There is a difference, confirm that imsm_dev_size is
11968 * smaller and push update.
895ffd99 11969 */
ff904202
MT
11970 if (d_size > calc_size) {
11971 pr_err("imsm: dev size of subarray %d is incorrect\n",
11972 i);
895ffd99
MT
11973 goto exit;
11974 }
11975 memset(&geo, 0, sizeof(struct geo_params));
11976 geo.size = d_size;
11977 u_size = imsm_create_metadata_update_for_size_change(st, &geo,
11978 &update);
895ffd99
MT
11979 imsm_update_metadata_locally(st, update, u_size);
11980 if (st->update_tail) {
11981 append_metadata_update(st, update, u_size);
11982 flush_metadata_updates(st);
11983 st->update_tail = &st->updates;
11984 } else {
11985 imsm_sync_metadata(st);
5ce5a15f 11986 free(update);
895ffd99
MT
11987 }
11988 }
11989 ret_val = 0;
11990exit:
11991 super->current_vol = tmp;
11992 return ret_val;
11993}
11994
d04f65f4
N
11995static int imsm_reshape_super(struct supertype *st, unsigned long long size,
11996 int level,
78b10e66 11997 int layout, int chunksize, int raid_disks,
41784c88 11998 int delta_disks, char *backup, char *dev,
016e00f5 11999 int direction, int verbose)
78b10e66 12000{
78b10e66
N
12001 int ret_val = 1;
12002 struct geo_params geo;
12003
1ade5cc1 12004 dprintf("(enter)\n");
78b10e66 12005
71204a50 12006 memset(&geo, 0, sizeof(struct geo_params));
78b10e66
N
12007
12008 geo.dev_name = dev;
4dd2df09 12009 strcpy(geo.devnm, st->devnm);
78b10e66
N
12010 geo.size = size;
12011 geo.level = level;
12012 geo.layout = layout;
12013 geo.chunksize = chunksize;
12014 geo.raid_disks = raid_disks;
41784c88
AK
12015 if (delta_disks != UnSet)
12016 geo.raid_disks += delta_disks;
78b10e66 12017
1ade5cc1
N
12018 dprintf("for level : %i\n", geo.level);
12019 dprintf("for raid_disks : %i\n", geo.raid_disks);
78b10e66 12020
4dd2df09 12021 if (strcmp(st->container_devnm, st->devnm) == 0) {
694575e7
KW
12022 /* On container level we can only increase number of devices. */
12023 dprintf("imsm: info: Container operation\n");
78b10e66 12024 int old_raid_disks = 0;
6dc0be30 12025
78b10e66 12026 if (imsm_reshape_is_allowed_on_container(
fbf3d202 12027 st, &geo, &old_raid_disks, direction)) {
78b10e66
N
12028 struct imsm_update_reshape *u = NULL;
12029 int len;
12030
895ffd99
MT
12031 if (imsm_fix_size_mismatch(st, -1)) {
12032 dprintf("imsm: Cannot fix size mismatch\n");
12033 goto exit_imsm_reshape_super;
12034 }
12035
78b10e66
N
12036 len = imsm_create_metadata_update_for_reshape(
12037 st, &geo, old_raid_disks, &u);
12038
ed08d51c
AK
12039 if (len <= 0) {
12040 dprintf("imsm: Cannot prepare update\n");
12041 goto exit_imsm_reshape_super;
12042 }
12043
8dd70bce
AK
12044 ret_val = 0;
12045 /* update metadata locally */
12046 imsm_update_metadata_locally(st, u, len);
12047 /* and possibly remotely */
12048 if (st->update_tail)
12049 append_metadata_update(st, u, len);
12050 else
ed08d51c 12051 free(u);
8dd70bce 12052
694575e7 12053 } else {
7a862a02 12054 pr_err("(imsm) Operation is not allowed on this container\n");
694575e7
KW
12055 }
12056 } else {
12057 /* On volume level we support following operations
471bceb6
KW
12058 * - takeover: raid10 -> raid0; raid0 -> raid10
12059 * - chunk size migration
12060 * - migration: raid5 -> raid0; raid0 -> raid5
12061 */
12062 struct intel_super *super = st->sb;
12063 struct intel_dev *dev = super->devlist;
4dd2df09 12064 int change;
694575e7 12065 dprintf("imsm: info: Volume operation\n");
471bceb6
KW
12066 /* find requested device */
12067 while (dev) {
1011e834 12068 char *devnm =
4dd2df09
N
12069 imsm_find_array_devnm_by_subdev(
12070 dev->index, st->container_devnm);
12071 if (devnm && strcmp(devnm, geo.devnm) == 0)
471bceb6
KW
12072 break;
12073 dev = dev->next;
12074 }
12075 if (dev == NULL) {
4dd2df09
N
12076 pr_err("Cannot find %s (%s) subarray\n",
12077 geo.dev_name, geo.devnm);
471bceb6
KW
12078 goto exit_imsm_reshape_super;
12079 }
12080 super->current_vol = dev->index;
fbf3d202 12081 change = imsm_analyze_change(st, &geo, direction);
694575e7 12082 switch (change) {
471bceb6 12083 case CH_TAKEOVER:
bb025c2f 12084 ret_val = imsm_takeover(st, &geo);
694575e7 12085 break;
48c5303a
PC
12086 case CH_MIGRATION: {
12087 struct imsm_update_reshape_migration *u = NULL;
12088 int len =
12089 imsm_create_metadata_update_for_migration(
12090 st, &geo, &u);
12091 if (len < 1) {
7a862a02 12092 dprintf("imsm: Cannot prepare update\n");
48c5303a
PC
12093 break;
12094 }
471bceb6 12095 ret_val = 0;
48c5303a
PC
12096 /* update metadata locally */
12097 imsm_update_metadata_locally(st, u, len);
12098 /* and possibly remotely */
12099 if (st->update_tail)
12100 append_metadata_update(st, u, len);
12101 else
12102 free(u);
12103 }
12104 break;
7abc9871 12105 case CH_ARRAY_SIZE: {
f3871fdc
AK
12106 struct imsm_update_size_change *u = NULL;
12107 int len =
12108 imsm_create_metadata_update_for_size_change(
12109 st, &geo, &u);
12110 if (len < 1) {
7a862a02 12111 dprintf("imsm: Cannot prepare update\n");
f3871fdc
AK
12112 break;
12113 }
12114 ret_val = 0;
12115 /* update metadata locally */
12116 imsm_update_metadata_locally(st, u, len);
12117 /* and possibly remotely */
12118 if (st->update_tail)
12119 append_metadata_update(st, u, len);
12120 else
12121 free(u);
7abc9871
AK
12122 }
12123 break;
471bceb6
KW
12124 default:
12125 ret_val = 1;
694575e7 12126 }
694575e7 12127 }
78b10e66 12128
ed08d51c 12129exit_imsm_reshape_super:
78b10e66
N
12130 dprintf("imsm: reshape_super Exit code = %i\n", ret_val);
12131 return ret_val;
12132}
2cda7640 12133
0febb20c
AO
12134#define COMPLETED_OK 0
12135#define COMPLETED_NONE 1
12136#define COMPLETED_DELAYED 2
12137
12138static int read_completed(int fd, unsigned long long *val)
12139{
12140 int ret;
12141 char buf[50];
12142
12143 ret = sysfs_fd_get_str(fd, buf, 50);
12144 if (ret < 0)
12145 return ret;
12146
12147 ret = COMPLETED_OK;
12148 if (strncmp(buf, "none", 4) == 0) {
12149 ret = COMPLETED_NONE;
12150 } else if (strncmp(buf, "delayed", 7) == 0) {
12151 ret = COMPLETED_DELAYED;
12152 } else {
12153 char *ep;
12154 *val = strtoull(buf, &ep, 0);
12155 if (ep == buf || (*ep != 0 && *ep != '\n' && *ep != ' '))
12156 ret = -1;
12157 }
12158 return ret;
12159}
12160
eee67a47
AK
12161/*******************************************************************************
12162 * Function: wait_for_reshape_imsm
12163 * Description: Function writes new sync_max value and waits until
12164 * reshape process reach new position
12165 * Parameters:
12166 * sra : general array info
eee67a47
AK
12167 * ndata : number of disks in new array's layout
12168 * Returns:
12169 * 0 : success,
12170 * 1 : there is no reshape in progress,
12171 * -1 : fail
12172 ******************************************************************************/
ae9f01f8 12173int wait_for_reshape_imsm(struct mdinfo *sra, int ndata)
eee67a47 12174{
85ca499c 12175 int fd = sysfs_get_fd(sra, NULL, "sync_completed");
df2647fa 12176 int retry = 3;
eee67a47 12177 unsigned long long completed;
ae9f01f8
AK
12178 /* to_complete : new sync_max position */
12179 unsigned long long to_complete = sra->reshape_progress;
12180 unsigned long long position_to_set = to_complete / ndata;
eee67a47 12181
4389ce73 12182 if (!is_fd_valid(fd)) {
1ade5cc1 12183 dprintf("cannot open reshape_position\n");
eee67a47 12184 return 1;
ae9f01f8 12185 }
eee67a47 12186
df2647fa
PB
12187 do {
12188 if (sysfs_fd_get_ll(fd, &completed) < 0) {
12189 if (!retry) {
12190 dprintf("cannot read reshape_position (no reshape in progres)\n");
12191 close(fd);
12192 return 1;
12193 }
239b3cc0 12194 sleep_for(0, MSEC_TO_NSEC(30), true);
df2647fa
PB
12195 } else
12196 break;
12197 } while (retry--);
eee67a47 12198
85ca499c 12199 if (completed > position_to_set) {
1ade5cc1 12200 dprintf("wrong next position to set %llu (%llu)\n",
85ca499c 12201 to_complete, position_to_set);
ae9f01f8
AK
12202 close(fd);
12203 return -1;
12204 }
12205 dprintf("Position set: %llu\n", position_to_set);
12206 if (sysfs_set_num(sra, NULL, "sync_max",
12207 position_to_set) != 0) {
1ade5cc1 12208 dprintf("cannot set reshape position to %llu\n",
ae9f01f8
AK
12209 position_to_set);
12210 close(fd);
12211 return -1;
eee67a47
AK
12212 }
12213
eee67a47 12214 do {
0febb20c 12215 int rc;
eee67a47 12216 char action[20];
5ff3a780 12217 int timeout = 3000;
0febb20c 12218
5ff3a780 12219 sysfs_wait(fd, &timeout);
a47e44fb
AK
12220 if (sysfs_get_str(sra, NULL, "sync_action",
12221 action, 20) > 0 &&
d7d3809a 12222 strncmp(action, "reshape", 7) != 0) {
b2be2b62
AO
12223 if (strncmp(action, "idle", 4) == 0)
12224 break;
d7d3809a
AP
12225 close(fd);
12226 return -1;
12227 }
0febb20c
AO
12228
12229 rc = read_completed(fd, &completed);
12230 if (rc < 0) {
1ade5cc1 12231 dprintf("cannot read reshape_position (in loop)\n");
eee67a47
AK
12232 close(fd);
12233 return 1;
0febb20c
AO
12234 } else if (rc == COMPLETED_NONE)
12235 break;
85ca499c 12236 } while (completed < position_to_set);
b2be2b62 12237
eee67a47
AK
12238 close(fd);
12239 return 0;
eee67a47
AK
12240}
12241
b915c95f
AK
12242/*******************************************************************************
12243 * Function: check_degradation_change
12244 * Description: Check that array hasn't become failed.
12245 * Parameters:
12246 * info : for sysfs access
12247 * sources : source disks descriptors
12248 * degraded: previous degradation level
12249 * Returns:
12250 * degradation level
12251 ******************************************************************************/
12252int check_degradation_change(struct mdinfo *info,
12253 int *sources,
12254 int degraded)
12255{
12256 unsigned long long new_degraded;
e1993023
LD
12257 int rv;
12258
12259 rv = sysfs_get_ll(info, NULL, "degraded", &new_degraded);
089f9d79 12260 if (rv == -1 || (new_degraded != (unsigned long long)degraded)) {
b915c95f
AK
12261 /* check each device to ensure it is still working */
12262 struct mdinfo *sd;
12263 new_degraded = 0;
12264 for (sd = info->devs ; sd ; sd = sd->next) {
12265 if (sd->disk.state & (1<<MD_DISK_FAULTY))
12266 continue;
12267 if (sd->disk.state & (1<<MD_DISK_SYNC)) {
cf52eff5 12268 char sbuf[100];
4389ce73 12269 int raid_disk = sd->disk.raid_disk;
cf52eff5 12270
b915c95f 12271 if (sysfs_get_str(info,
cf52eff5 12272 sd, "state", sbuf, sizeof(sbuf)) < 0 ||
b915c95f
AK
12273 strstr(sbuf, "faulty") ||
12274 strstr(sbuf, "in_sync") == NULL) {
12275 /* this device is dead */
12276 sd->disk.state = (1<<MD_DISK_FAULTY);
4389ce73
MT
12277 if (raid_disk >= 0)
12278 close_fd(&sources[raid_disk]);
b915c95f
AK
12279 new_degraded++;
12280 }
12281 }
12282 }
12283 }
12284
12285 return new_degraded;
12286}
12287
10f22854
AK
12288/*******************************************************************************
12289 * Function: imsm_manage_reshape
12290 * Description: Function finds array under reshape and it manages reshape
12291 * process. It creates stripes backups (if required) and sets
942e1cdb 12292 * checkpoints.
10f22854
AK
12293 * Parameters:
12294 * afd : Backup handle (nattive) - not used
12295 * sra : general array info
12296 * reshape : reshape parameters - not used
12297 * st : supertype structure
12298 * blocks : size of critical section [blocks]
12299 * fds : table of source device descriptor
12300 * offsets : start of array (offest per devices)
12301 * dests : not used
12302 * destfd : table of destination device descriptor
12303 * destoffsets : table of destination offsets (per device)
12304 * Returns:
12305 * 1 : success, reshape is done
12306 * 0 : fail
12307 ******************************************************************************/
999b4972
N
12308static int imsm_manage_reshape(
12309 int afd, struct mdinfo *sra, struct reshape *reshape,
10f22854 12310 struct supertype *st, unsigned long backup_blocks,
999b4972
N
12311 int *fds, unsigned long long *offsets,
12312 int dests, int *destfd, unsigned long long *destoffsets)
12313{
10f22854
AK
12314 int ret_val = 0;
12315 struct intel_super *super = st->sb;
594dc1b8 12316 struct intel_dev *dv;
de44e46f 12317 unsigned int sector_size = super->sector_size;
10f22854 12318 struct imsm_dev *dev = NULL;
9529d343 12319 struct imsm_map *map_src, *map_dest;
10f22854
AK
12320 int migr_vol_qan = 0;
12321 int ndata, odata; /* [bytes] */
12322 int chunk; /* [bytes] */
12323 struct migr_record *migr_rec;
12324 char *buf = NULL;
12325 unsigned int buf_size; /* [bytes] */
12326 unsigned long long max_position; /* array size [bytes] */
12327 unsigned long long next_step; /* [blocks]/[bytes] */
12328 unsigned long long old_data_stripe_length;
10f22854
AK
12329 unsigned long long start_src; /* [bytes] */
12330 unsigned long long start; /* [bytes] */
12331 unsigned long long start_buf_shift; /* [bytes] */
b915c95f 12332 int degraded = 0;
ab724b98 12333 int source_layout = 0;
895ffd99 12334 int subarray_index = -1;
10f22854 12335
79a16a9b
JS
12336 if (!sra)
12337 return ret_val;
12338
12339 if (!fds || !offsets)
10f22854
AK
12340 goto abort;
12341
12342 /* Find volume during the reshape */
12343 for (dv = super->devlist; dv; dv = dv->next) {
fc54fe7a
JS
12344 if (dv->dev->vol.migr_type == MIGR_GEN_MIGR &&
12345 dv->dev->vol.migr_state == 1) {
10f22854
AK
12346 dev = dv->dev;
12347 migr_vol_qan++;
895ffd99 12348 subarray_index = dv->index;
10f22854
AK
12349 }
12350 }
12351 /* Only one volume can migrate at the same time */
12352 if (migr_vol_qan != 1) {
676e87a8 12353 pr_err("%s", migr_vol_qan ?
10f22854
AK
12354 "Number of migrating volumes greater than 1\n" :
12355 "There is no volume during migrationg\n");
12356 goto abort;
12357 }
12358
9529d343 12359 map_dest = get_imsm_map(dev, MAP_0);
238c0a71 12360 map_src = get_imsm_map(dev, MAP_1);
10f22854
AK
12361 if (map_src == NULL)
12362 goto abort;
10f22854 12363
9529d343
MD
12364 ndata = imsm_num_data_members(map_dest);
12365 odata = imsm_num_data_members(map_src);
10f22854 12366
7b1ab482 12367 chunk = __le16_to_cpu(map_src->blocks_per_strip) * 512;
10f22854
AK
12368 old_data_stripe_length = odata * chunk;
12369
12370 migr_rec = super->migr_rec;
12371
10f22854
AK
12372 /* initialize migration record for start condition */
12373 if (sra->reshape_progress == 0)
12374 init_migr_record_imsm(st, dev, sra);
b2c59438
AK
12375 else {
12376 if (__le32_to_cpu(migr_rec->rec_status) != UNIT_SRC_NORMAL) {
7a862a02 12377 dprintf("imsm: cannot restart migration when data are present in copy area.\n");
b2c59438
AK
12378 goto abort;
12379 }
6a75c8ca
AK
12380 /* Save checkpoint to update migration record for current
12381 * reshape position (in md). It can be farther than current
12382 * reshape position in metadata.
12383 */
12384 if (save_checkpoint_imsm(st, sra, UNIT_SRC_NORMAL) == 1) {
12385 /* ignore error == 2, this can mean end of reshape here
12386 */
7a862a02 12387 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL, initial save)\n");
6a75c8ca
AK
12388 goto abort;
12389 }
b2c59438 12390 }
10f22854
AK
12391
12392 /* size for data */
12393 buf_size = __le32_to_cpu(migr_rec->blocks_per_unit) * 512;
12394 /* extend buffer size for parity disk */
12395 buf_size += __le32_to_cpu(migr_rec->dest_depth_per_unit) * 512;
3e684231 12396 /* add space for stripe alignment */
10f22854 12397 buf_size += old_data_stripe_length;
de44e46f
PB
12398 if (posix_memalign((void **)&buf, MAX_SECTOR_SIZE, buf_size)) {
12399 dprintf("imsm: Cannot allocate checkpoint buffer\n");
10f22854
AK
12400 goto abort;
12401 }
12402
3ef4403c 12403 max_position = sra->component_size * ndata;
68eb8bc6 12404 source_layout = imsm_level_to_layout(map_src->raid_level);
10f22854 12405
9f421827
PB
12406 while (current_migr_unit(migr_rec) <
12407 get_num_migr_units(migr_rec)) {
10f22854
AK
12408 /* current reshape position [blocks] */
12409 unsigned long long current_position =
12410 __le32_to_cpu(migr_rec->blocks_per_unit)
9f421827 12411 * current_migr_unit(migr_rec);
10f22854
AK
12412 unsigned long long border;
12413
b915c95f
AK
12414 /* Check that array hasn't become failed.
12415 */
12416 degraded = check_degradation_change(sra, fds, degraded);
12417 if (degraded > 1) {
7a862a02 12418 dprintf("imsm: Abort reshape due to degradation level (%i)\n", degraded);
b915c95f
AK
12419 goto abort;
12420 }
12421
10f22854
AK
12422 next_step = __le32_to_cpu(migr_rec->blocks_per_unit);
12423
12424 if ((current_position + next_step) > max_position)
12425 next_step = max_position - current_position;
12426
92144abf 12427 start = current_position * 512;
10f22854 12428
942e1cdb 12429 /* align reading start to old geometry */
10f22854
AK
12430 start_buf_shift = start % old_data_stripe_length;
12431 start_src = start - start_buf_shift;
12432
12433 border = (start_src / odata) - (start / ndata);
12434 border /= 512;
12435 if (border <= __le32_to_cpu(migr_rec->dest_depth_per_unit)) {
12436 /* save critical stripes to buf
12437 * start - start address of current unit
12438 * to backup [bytes]
12439 * start_src - start address of current unit
12440 * to backup alligned to source array
12441 * [bytes]
12442 */
594dc1b8 12443 unsigned long long next_step_filler;
10f22854
AK
12444 unsigned long long copy_length = next_step * 512;
12445
12446 /* allign copy area length to stripe in old geometry */
12447 next_step_filler = ((copy_length + start_buf_shift)
12448 % old_data_stripe_length);
12449 if (next_step_filler)
12450 next_step_filler = (old_data_stripe_length
12451 - next_step_filler);
7a862a02 12452 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
12453 start, start_src, copy_length,
12454 start_buf_shift, next_step_filler);
12455
12456 if (save_stripes(fds, offsets, map_src->num_members,
ab724b98
AK
12457 chunk, map_src->raid_level,
12458 source_layout, 0, NULL, start_src,
10f22854
AK
12459 copy_length +
12460 next_step_filler + start_buf_shift,
12461 buf)) {
7a862a02 12462 dprintf("imsm: Cannot save stripes to buffer\n");
10f22854
AK
12463 goto abort;
12464 }
12465 /* Convert data to destination format and store it
12466 * in backup general migration area
12467 */
12468 if (save_backup_imsm(st, dev, sra,
aea93171 12469 buf + start_buf_shift, copy_length)) {
7a862a02 12470 dprintf("imsm: Cannot save stripes to target devices\n");
10f22854
AK
12471 goto abort;
12472 }
12473 if (save_checkpoint_imsm(st, sra,
12474 UNIT_SRC_IN_CP_AREA)) {
7a862a02 12475 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_IN_CP_AREA)\n");
10f22854
AK
12476 goto abort;
12477 }
8016a6d4
AK
12478 } else {
12479 /* set next step to use whole border area */
12480 border /= next_step;
12481 if (border > 1)
12482 next_step *= border;
10f22854
AK
12483 }
12484 /* When data backed up, checkpoint stored,
12485 * kick the kernel to reshape unit of data
12486 */
12487 next_step = next_step + sra->reshape_progress;
8016a6d4
AK
12488 /* limit next step to array max position */
12489 if (next_step > max_position)
12490 next_step = max_position;
10f22854
AK
12491 sysfs_set_num(sra, NULL, "suspend_lo", sra->reshape_progress);
12492 sysfs_set_num(sra, NULL, "suspend_hi", next_step);
ae9f01f8 12493 sra->reshape_progress = next_step;
10f22854
AK
12494
12495 /* wait until reshape finish */
c85338c6 12496 if (wait_for_reshape_imsm(sra, ndata)) {
c47b0ff6
AK
12497 dprintf("wait_for_reshape_imsm returned error!\n");
12498 goto abort;
12499 }
84d11e6c
N
12500 if (sigterm)
12501 goto abort;
10f22854 12502
0228d92c
AK
12503 if (save_checkpoint_imsm(st, sra, UNIT_SRC_NORMAL) == 1) {
12504 /* ignore error == 2, this can mean end of reshape here
12505 */
7a862a02 12506 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL)\n");
10f22854
AK
12507 goto abort;
12508 }
12509
12510 }
12511
71e5411e
PB
12512 /* clear migr_rec on disks after successful migration */
12513 struct dl *d;
12514
85337573 12515 memset(super->migr_rec_buf, 0, MIGR_REC_BUF_SECTORS*MAX_SECTOR_SIZE);
71e5411e
PB
12516 for (d = super->disks; d; d = d->next) {
12517 if (d->index < 0 || is_failed(&d->disk))
12518 continue;
12519 unsigned long long dsize;
12520
12521 get_dev_size(d->fd, NULL, &dsize);
de44e46f 12522 if (lseek64(d->fd, dsize - MIGR_REC_SECTOR_POSITION*sector_size,
71e5411e 12523 SEEK_SET) >= 0) {
466070ad 12524 if ((unsigned int)write(d->fd, super->migr_rec_buf,
de44e46f
PB
12525 MIGR_REC_BUF_SECTORS*sector_size) !=
12526 MIGR_REC_BUF_SECTORS*sector_size)
71e5411e
PB
12527 perror("Write migr_rec failed");
12528 }
12529 }
12530
10f22854
AK
12531 /* return '1' if done */
12532 ret_val = 1;
895ffd99
MT
12533
12534 /* After the reshape eliminate size mismatch in metadata.
12535 * Don't update md/component_size here, volume hasn't
12536 * to take whole space. It is allowed by kernel.
12537 * md/component_size will be set propoperly after next assembly.
12538 */
12539 imsm_fix_size_mismatch(st, subarray_index);
12540
10f22854
AK
12541abort:
12542 free(buf);
942e1cdb
N
12543 /* See Grow.c: abort_reshape() for further explanation */
12544 sysfs_set_num(sra, NULL, "suspend_lo", 0x7FFFFFFFFFFFFFFFULL);
12545 sysfs_set_num(sra, NULL, "suspend_hi", 0);
12546 sysfs_set_num(sra, NULL, "suspend_lo", 0);
10f22854
AK
12547
12548 return ret_val;
999b4972 12549}
0c21b485 12550
fbc42556
JR
12551/*******************************************************************************
12552 * Function: calculate_bitmap_min_chunksize
12553 * Description: Calculates the minimal valid bitmap chunk size
12554 * Parameters:
12555 * max_bits : indicate how many bits can be used for the bitmap
12556 * data_area_size : the size of the data area covered by the bitmap
12557 *
12558 * Returns:
12559 * The bitmap chunk size
12560 ******************************************************************************/
12561static unsigned long long
12562calculate_bitmap_min_chunksize(unsigned long long max_bits,
12563 unsigned long long data_area_size)
12564{
12565 unsigned long long min_chunk =
12566 4096; /* sub-page chunks don't work yet.. */
12567 unsigned long long bits = data_area_size / min_chunk + 1;
12568
12569 while (bits > max_bits) {
12570 min_chunk *= 2;
12571 bits = (bits + 1) / 2;
12572 }
12573 return min_chunk;
12574}
12575
12576/*******************************************************************************
12577 * Function: calculate_bitmap_chunksize
12578 * Description: Calculates the bitmap chunk size for the given device
12579 * Parameters:
12580 * st : supertype information
12581 * dev : device for the bitmap
12582 *
12583 * Returns:
12584 * The bitmap chunk size
12585 ******************************************************************************/
12586static unsigned long long calculate_bitmap_chunksize(struct supertype *st,
12587 struct imsm_dev *dev)
12588{
12589 struct intel_super *super = st->sb;
12590 unsigned long long min_chunksize;
12591 unsigned long long result = IMSM_DEFAULT_BITMAP_CHUNKSIZE;
12592 size_t dev_size = imsm_dev_size(dev);
12593
12594 min_chunksize = calculate_bitmap_min_chunksize(
12595 IMSM_BITMAP_AREA_SIZE * super->sector_size, dev_size);
12596
12597 if (result < min_chunksize)
12598 result = min_chunksize;
12599
12600 return result;
12601}
12602
12603/*******************************************************************************
12604 * Function: init_bitmap_header
12605 * Description: Initialize the bitmap header structure
12606 * Parameters:
12607 * st : supertype information
12608 * bms : bitmap header struct to initialize
12609 * dev : device for the bitmap
12610 *
12611 * Returns:
12612 * 0 : success
12613 * -1 : fail
12614 ******************************************************************************/
12615static int init_bitmap_header(struct supertype *st, struct bitmap_super_s *bms,
12616 struct imsm_dev *dev)
12617{
12618 int vol_uuid[4];
12619
12620 if (!bms || !dev)
12621 return -1;
12622
12623 bms->magic = __cpu_to_le32(BITMAP_MAGIC);
12624 bms->version = __cpu_to_le32(BITMAP_MAJOR_HI);
12625 bms->daemon_sleep = __cpu_to_le32(IMSM_DEFAULT_BITMAP_DAEMON_SLEEP);
12626 bms->sync_size = __cpu_to_le64(IMSM_BITMAP_AREA_SIZE);
12627 bms->write_behind = __cpu_to_le32(0);
12628
12629 uuid_from_super_imsm(st, vol_uuid);
12630 memcpy(bms->uuid, vol_uuid, 16);
12631
12632 bms->chunksize = calculate_bitmap_chunksize(st, dev);
12633
12634 return 0;
12635}
12636
12637/*******************************************************************************
12638 * Function: validate_internal_bitmap_for_drive
12639 * Description: Verify if the bitmap header for a given drive.
12640 * Parameters:
12641 * st : supertype information
12642 * offset : The offset from the beginning of the drive where to look for
12643 * the bitmap header.
12644 * d : the drive info
12645 *
12646 * Returns:
12647 * 0 : success
12648 * -1 : fail
12649 ******************************************************************************/
12650static int validate_internal_bitmap_for_drive(struct supertype *st,
12651 unsigned long long offset,
12652 struct dl *d)
12653{
12654 struct intel_super *super = st->sb;
12655 int ret = -1;
12656 int vol_uuid[4];
12657 bitmap_super_t *bms;
12658 int fd;
12659
12660 if (!d)
12661 return -1;
12662
12663 void *read_buf;
12664
12665 if (posix_memalign(&read_buf, MAX_SECTOR_SIZE, IMSM_BITMAP_HEADER_SIZE))
12666 return -1;
12667
12668 fd = d->fd;
4389ce73 12669 if (!is_fd_valid(fd)) {
fbc42556 12670 fd = open(d->devname, O_RDONLY, 0);
4389ce73
MT
12671
12672 if (!is_fd_valid(fd)) {
fbc42556
JR
12673 dprintf("cannot open the device %s\n", d->devname);
12674 goto abort;
12675 }
12676 }
12677
12678 if (lseek64(fd, offset * super->sector_size, SEEK_SET) < 0)
12679 goto abort;
12680 if (read(fd, read_buf, IMSM_BITMAP_HEADER_SIZE) !=
12681 IMSM_BITMAP_HEADER_SIZE)
12682 goto abort;
12683
12684 uuid_from_super_imsm(st, vol_uuid);
12685
12686 bms = read_buf;
12687 if ((bms->magic != __cpu_to_le32(BITMAP_MAGIC)) ||
12688 (bms->version != __cpu_to_le32(BITMAP_MAJOR_HI)) ||
12689 (!same_uuid((int *)bms->uuid, vol_uuid, st->ss->swapuuid))) {
12690 dprintf("wrong bitmap header detected\n");
12691 goto abort;
12692 }
12693
12694 ret = 0;
12695abort:
4389ce73
MT
12696 if (!is_fd_valid(d->fd))
12697 close_fd(&fd);
12698
fbc42556
JR
12699 if (read_buf)
12700 free(read_buf);
12701
12702 return ret;
12703}
12704
12705/*******************************************************************************
12706 * Function: validate_internal_bitmap_imsm
12707 * Description: Verify if the bitmap header is in place and with proper data.
12708 * Parameters:
12709 * st : supertype information
12710 *
12711 * Returns:
12712 * 0 : success or device w/o RWH_BITMAP
12713 * -1 : fail
12714 ******************************************************************************/
12715static int validate_internal_bitmap_imsm(struct supertype *st)
12716{
12717 struct intel_super *super = st->sb;
12718 struct imsm_dev *dev = get_imsm_dev(super, super->current_vol);
12719 unsigned long long offset;
12720 struct dl *d;
12721
fbc42556
JR
12722 if (dev->rwh_policy != RWH_BITMAP)
12723 return 0;
12724
12725 offset = get_bitmap_header_sector(super, super->current_vol);
12726 for (d = super->disks; d; d = d->next) {
12727 if (d->index < 0 || is_failed(&d->disk))
12728 continue;
12729
12730 if (validate_internal_bitmap_for_drive(st, offset, d)) {
12731 pr_err("imsm: bitmap validation failed\n");
12732 return -1;
12733 }
12734 }
12735 return 0;
12736}
12737
12738/*******************************************************************************
12739 * Function: add_internal_bitmap_imsm
12740 * Description: Mark the volume to use the bitmap and updates the chunk size value.
12741 * Parameters:
12742 * st : supertype information
12743 * chunkp : bitmap chunk size
12744 * delay : not used for imsm
12745 * write_behind : not used for imsm
12746 * size : not used for imsm
12747 * may_change : not used for imsm
12748 * amajor : not used for imsm
12749 *
12750 * Returns:
12751 * 0 : success
12752 * -1 : fail
12753 ******************************************************************************/
12754static int add_internal_bitmap_imsm(struct supertype *st, int *chunkp,
12755 int delay, int write_behind,
12756 unsigned long long size, int may_change,
12757 int amajor)
12758{
12759 struct intel_super *super = st->sb;
12760 int vol_idx = super->current_vol;
12761 struct imsm_dev *dev;
12762
12763 if (!super->devlist || vol_idx == -1 || !chunkp)
12764 return -1;
12765
12766 dev = get_imsm_dev(super, vol_idx);
fbc42556 12767 dev->rwh_policy = RWH_BITMAP;
fbc42556 12768 *chunkp = calculate_bitmap_chunksize(st, dev);
fbc42556
JR
12769 return 0;
12770}
12771
12772/*******************************************************************************
12773 * Function: locate_bitmap_imsm
12774 * Description: Seek 'fd' to start of write-intent-bitmap.
12775 * Parameters:
12776 * st : supertype information
12777 * fd : file descriptor for the device
12778 * node_num : not used for imsm
12779 *
12780 * Returns:
12781 * 0 : success
12782 * -1 : fail
12783 ******************************************************************************/
12784static int locate_bitmap_imsm(struct supertype *st, int fd, int node_num)
12785{
12786 struct intel_super *super = st->sb;
12787 unsigned long long offset;
12788 int vol_idx = super->current_vol;
12789
12790 if (!super->devlist || vol_idx == -1)
12791 return -1;
12792
12793 offset = get_bitmap_header_sector(super, super->current_vol);
12794 dprintf("bitmap header offset is %llu\n", offset);
12795
12796 lseek64(fd, offset << 9, 0);
12797
12798 return 0;
12799}
12800
12801/*******************************************************************************
12802 * Function: write_init_bitmap_imsm
12803 * Description: Write a bitmap header and prepares the area for the bitmap.
12804 * Parameters:
12805 * st : supertype information
12806 * fd : file descriptor for the device
12807 * update : not used for imsm
12808 *
12809 * Returns:
12810 * 0 : success
12811 * -1 : fail
12812 ******************************************************************************/
12813static int write_init_bitmap_imsm(struct supertype *st, int fd,
12814 enum bitmap_update update)
12815{
12816 struct intel_super *super = st->sb;
12817 int vol_idx = super->current_vol;
12818 int ret = 0;
12819 unsigned long long offset;
12820 bitmap_super_t bms = { 0 };
12821 size_t written = 0;
12822 size_t to_write;
12823 ssize_t rv_num;
12824 void *buf;
12825
12826 if (!super->devlist || !super->sector_size || vol_idx == -1)
12827 return -1;
12828
12829 struct imsm_dev *dev = get_imsm_dev(super, vol_idx);
12830
12831 /* first clear the space for bitmap header */
12832 unsigned long long bitmap_area_start =
12833 get_bitmap_header_sector(super, vol_idx);
12834
12835 dprintf("zeroing area start (%llu) and size (%u)\n", bitmap_area_start,
12836 IMSM_BITMAP_AND_HEADER_SIZE / super->sector_size);
12837 if (zero_disk_range(fd, bitmap_area_start,
12838 IMSM_BITMAP_HEADER_SIZE / super->sector_size)) {
12839 pr_err("imsm: cannot zeroing the space for the bitmap\n");
12840 return -1;
12841 }
12842
12843 /* The bitmap area should be filled with "1"s to perform initial
12844 * synchronization.
12845 */
12846 if (posix_memalign(&buf, MAX_SECTOR_SIZE, MAX_SECTOR_SIZE))
12847 return -1;
12848 memset(buf, 0xFF, MAX_SECTOR_SIZE);
12849 offset = get_bitmap_sector(super, vol_idx);
12850 lseek64(fd, offset << 9, 0);
12851 while (written < IMSM_BITMAP_AREA_SIZE) {
12852 to_write = IMSM_BITMAP_AREA_SIZE - written;
12853 if (to_write > MAX_SECTOR_SIZE)
12854 to_write = MAX_SECTOR_SIZE;
12855 rv_num = write(fd, buf, MAX_SECTOR_SIZE);
12856 if (rv_num != MAX_SECTOR_SIZE) {
12857 ret = -1;
12858 dprintf("cannot initialize bitmap area\n");
12859 goto abort;
12860 }
12861 written += rv_num;
12862 }
12863
12864 /* write a bitmap header */
12865 init_bitmap_header(st, &bms, dev);
12866 memset(buf, 0, MAX_SECTOR_SIZE);
12867 memcpy(buf, &bms, sizeof(bitmap_super_t));
12868 if (locate_bitmap_imsm(st, fd, 0)) {
12869 ret = -1;
12870 dprintf("cannot locate the bitmap\n");
12871 goto abort;
12872 }
12873 if (write(fd, buf, MAX_SECTOR_SIZE) != MAX_SECTOR_SIZE) {
12874 ret = -1;
12875 dprintf("cannot write the bitmap header\n");
12876 goto abort;
12877 }
12878 fsync(fd);
12879
12880abort:
12881 free(buf);
12882
12883 return ret;
12884}
12885
12886/*******************************************************************************
12887 * Function: is_vol_to_setup_bitmap
12888 * Description: Checks if a bitmap should be activated on the dev.
12889 * Parameters:
12890 * info : info about the volume to setup the bitmap
12891 * dev : the device to check against bitmap creation
12892 *
12893 * Returns:
12894 * 0 : bitmap should be set up on the device
12895 * -1 : otherwise
12896 ******************************************************************************/
12897static int is_vol_to_setup_bitmap(struct mdinfo *info, struct imsm_dev *dev)
12898{
12899 if (!dev || !info)
12900 return -1;
12901
12902 if ((strcmp((char *)dev->volume, info->name) == 0) &&
12903 (dev->rwh_policy == RWH_BITMAP))
12904 return -1;
12905
12906 return 0;
12907}
12908
12909/*******************************************************************************
12910 * Function: set_bitmap_sysfs
12911 * Description: Set the sysfs atributes of a given volume to activate the bitmap.
12912 * Parameters:
12913 * info : info about the volume where the bitmap should be setup
12914 * chunksize : bitmap chunk size
12915 * location : location of the bitmap
12916 *
12917 * Returns:
12918 * 0 : success
12919 * -1 : fail
12920 ******************************************************************************/
12921static int set_bitmap_sysfs(struct mdinfo *info, unsigned long long chunksize,
12922 char *location)
12923{
12924 /* The bitmap/metadata is set to external to allow changing of value for
12925 * bitmap/location. When external is used, the kernel will treat an offset
12926 * related to the device's first lba (in opposition to the "internal" case
12927 * when this value is related to the beginning of the superblock).
12928 */
12929 if (sysfs_set_str(info, NULL, "bitmap/metadata", "external")) {
12930 dprintf("failed to set bitmap/metadata\n");
12931 return -1;
12932 }
12933
12934 /* It can only be changed when no bitmap is active.
12935 * Should be bigger than 512 and must be power of 2.
12936 * It is expecting the value in bytes.
12937 */
12938 if (sysfs_set_num(info, NULL, "bitmap/chunksize",
12939 __cpu_to_le32(chunksize))) {
12940 dprintf("failed to set bitmap/chunksize\n");
12941 return -1;
12942 }
12943
12944 /* It is expecting the value in sectors. */
12945 if (sysfs_set_num(info, NULL, "bitmap/space",
12946 __cpu_to_le64(IMSM_BITMAP_AREA_SIZE))) {
12947 dprintf("failed to set bitmap/space\n");
12948 return -1;
12949 }
12950
12951 /* Determines the delay between the bitmap updates.
12952 * It is expecting the value in seconds.
12953 */
12954 if (sysfs_set_num(info, NULL, "bitmap/time_base",
12955 __cpu_to_le64(IMSM_DEFAULT_BITMAP_DAEMON_SLEEP))) {
12956 dprintf("failed to set bitmap/time_base\n");
12957 return -1;
12958 }
12959
12960 /* It is expecting the value in sectors with a sign at the beginning. */
12961 if (sysfs_set_str(info, NULL, "bitmap/location", location)) {
12962 dprintf("failed to set bitmap/location\n");
12963 return -1;
12964 }
12965
12966 return 0;
12967}
12968
12969/*******************************************************************************
12970 * Function: set_bitmap_imsm
12971 * Description: Setup the bitmap for the given volume
12972 * Parameters:
12973 * st : supertype information
12974 * info : info about the volume where the bitmap should be setup
12975 *
12976 * Returns:
12977 * 0 : success
12978 * -1 : fail
12979 ******************************************************************************/
12980static int set_bitmap_imsm(struct supertype *st, struct mdinfo *info)
12981{
12982 struct intel_super *super = st->sb;
12983 int prev_current_vol = super->current_vol;
12984 struct imsm_dev *dev;
12985 int ret = -1;
12986 char location[16] = "";
12987 unsigned long long chunksize;
12988 struct intel_dev *dev_it;
12989
12990 for (dev_it = super->devlist; dev_it; dev_it = dev_it->next) {
12991 super->current_vol = dev_it->index;
12992 dev = get_imsm_dev(super, super->current_vol);
12993
12994 if (is_vol_to_setup_bitmap(info, dev)) {
12995 if (validate_internal_bitmap_imsm(st)) {
12996 dprintf("bitmap header validation failed\n");
12997 goto abort;
12998 }
12999
13000 chunksize = calculate_bitmap_chunksize(st, dev);
13001 dprintf("chunk size is %llu\n", chunksize);
13002
13003 snprintf(location, sizeof(location), "+%llu",
13004 get_bitmap_sector(super, super->current_vol));
13005 dprintf("bitmap offset is %s\n", location);
13006
13007 if (set_bitmap_sysfs(info, chunksize, location)) {
13008 dprintf("cannot setup the bitmap\n");
13009 goto abort;
13010 }
13011 }
13012 }
13013 ret = 0;
13014abort:
13015 super->current_vol = prev_current_vol;
13016 return ret;
13017}
13018
cdddbdbc 13019struct superswitch super_imsm = {
cdddbdbc
DW
13020 .examine_super = examine_super_imsm,
13021 .brief_examine_super = brief_examine_super_imsm,
4737ae25 13022 .brief_examine_subarrays = brief_examine_subarrays_imsm,
9d84c8ea 13023 .export_examine_super = export_examine_super_imsm,
cdddbdbc
DW
13024 .detail_super = detail_super_imsm,
13025 .brief_detail_super = brief_detail_super_imsm,
bf5a934a 13026 .write_init_super = write_init_super_imsm,
0e600426
N
13027 .validate_geometry = validate_geometry_imsm,
13028 .add_to_super = add_to_super_imsm,
1a64be56 13029 .remove_from_super = remove_from_super_imsm,
d665cc31 13030 .detail_platform = detail_platform_imsm,
e50cf220 13031 .export_detail_platform = export_detail_platform_imsm,
33414a01 13032 .kill_subarray = kill_subarray_imsm,
aa534678 13033 .update_subarray = update_subarray_imsm,
2b959fbf 13034 .load_container = load_container_imsm,
71204a50
N
13035 .default_geometry = default_geometry_imsm,
13036 .get_disk_controller_domain = imsm_get_disk_controller_domain,
13037 .reshape_super = imsm_reshape_super,
13038 .manage_reshape = imsm_manage_reshape,
9e2d750d 13039 .recover_backup = recover_backup_imsm,
27156a57 13040 .examine_badblocks = examine_badblocks_imsm,
cdddbdbc
DW
13041 .match_home = match_home_imsm,
13042 .uuid_from_super= uuid_from_super_imsm,
13043 .getinfo_super = getinfo_super_imsm,
5c4cd5da 13044 .getinfo_super_disks = getinfo_super_disks_imsm,
cdddbdbc
DW
13045 .update_super = update_super_imsm,
13046
13047 .avail_size = avail_size_imsm,
fbfdcb06 13048 .get_spare_criteria = get_spare_criteria_imsm,
cdddbdbc
DW
13049
13050 .compare_super = compare_super_imsm,
13051
13052 .load_super = load_super_imsm,
bf5a934a 13053 .init_super = init_super_imsm,
e683ca88 13054 .store_super = store_super_imsm,
cdddbdbc
DW
13055 .free_super = free_super_imsm,
13056 .match_metadata_desc = match_metadata_desc_imsm,
bf5a934a 13057 .container_content = container_content_imsm,
0c21b485 13058 .validate_container = validate_container_imsm,
cdddbdbc 13059
fbc42556
JR
13060 .add_internal_bitmap = add_internal_bitmap_imsm,
13061 .locate_bitmap = locate_bitmap_imsm,
13062 .write_bitmap = write_init_bitmap_imsm,
13063 .set_bitmap = set_bitmap_imsm,
13064
2432ce9b
AP
13065 .write_init_ppl = write_init_ppl_imsm,
13066 .validate_ppl = validate_ppl_imsm,
13067
cdddbdbc 13068 .external = 1,
4cce4069 13069 .name = "imsm",
845dea95
NB
13070
13071/* for mdmon */
13072 .open_new = imsm_open_new,
ed9d66aa 13073 .set_array_state= imsm_set_array_state,
845dea95
NB
13074 .set_disk = imsm_set_disk,
13075 .sync_metadata = imsm_sync_metadata,
88758e9d 13076 .activate_spare = imsm_activate_spare,
e8319a19 13077 .process_update = imsm_process_update,
8273f55e 13078 .prepare_update = imsm_prepare_update,
6f50473f 13079 .record_bad_block = imsm_record_badblock,
c07a5a4f 13080 .clear_bad_block = imsm_clear_badblock,
928f1424 13081 .get_bad_blocks = imsm_get_badblocks,
cdddbdbc 13082};