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