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