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