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