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