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