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