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