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