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