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