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