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