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