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