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