mdmon: fix wrong array state when disk fails during mdmon startup
[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 MPB_ATTRIB_BBM)
86
87 /* Define attributes that are unused but not harmful */
88 #define MPB_ATTRIB_IGNORED (MPB_ATTRIB_NEVER_USE)
89
90 #define MPB_SECTOR_CNT 2210
91 #define IMSM_RESERVED_SECTORS 8192
92 #define NUM_BLOCKS_DIRTY_STRIPE_REGION 2048
93 #define SECT_PER_MB_SHIFT 11
94 #define MAX_SECTOR_SIZE 4096
95 #define MULTIPLE_PPL_AREA_SIZE_IMSM (1024 * 1024) /* Size of the whole
96 * mutliple PPL area
97 */
98
99 /* Disk configuration info. */
100 #define IMSM_MAX_DEVICES 255
101 struct imsm_disk {
102 __u8 serial[MAX_RAID_SERIAL_LEN];/* 0xD8 - 0xE7 ascii serial number */
103 __u32 total_blocks_lo; /* 0xE8 - 0xEB total blocks lo */
104 __u32 scsi_id; /* 0xEC - 0xEF scsi ID */
105 #define SPARE_DISK __cpu_to_le32(0x01) /* Spare */
106 #define CONFIGURED_DISK __cpu_to_le32(0x02) /* Member of some RaidDev */
107 #define FAILED_DISK __cpu_to_le32(0x04) /* Permanent failure */
108 #define JOURNAL_DISK __cpu_to_le32(0x2000000) /* Device marked as Journaling Drive */
109 __u32 status; /* 0xF0 - 0xF3 */
110 __u32 owner_cfg_num; /* which config 0,1,2... owns this disk */
111 __u32 total_blocks_hi; /* 0xF4 - 0xF5 total blocks hi */
112 #define IMSM_DISK_FILLERS 3
113 __u32 filler[IMSM_DISK_FILLERS]; /* 0xF5 - 0x107 MPB_DISK_FILLERS for future expansion */
114 };
115
116 /* map selector for map managment
117 */
118 #define MAP_0 0
119 #define MAP_1 1
120 #define MAP_X -1
121
122 /* RAID map configuration infos. */
123 struct imsm_map {
124 __u32 pba_of_lba0_lo; /* start address of partition */
125 __u32 blocks_per_member_lo;/* blocks per member */
126 __u32 num_data_stripes_lo; /* number of data stripes */
127 __u16 blocks_per_strip;
128 __u8 map_state; /* Normal, Uninitialized, Degraded, Failed */
129 #define IMSM_T_STATE_NORMAL 0
130 #define IMSM_T_STATE_UNINITIALIZED 1
131 #define IMSM_T_STATE_DEGRADED 2
132 #define IMSM_T_STATE_FAILED 3
133 __u8 raid_level;
134 #define IMSM_T_RAID0 0
135 #define IMSM_T_RAID1 1
136 #define IMSM_T_RAID5 5 /* since metadata version 1.2.02 ? */
137 __u8 num_members; /* number of member disks */
138 __u8 num_domains; /* number of parity domains */
139 __u8 failed_disk_num; /* valid only when state is degraded */
140 __u8 ddf;
141 __u32 pba_of_lba0_hi;
142 __u32 blocks_per_member_hi;
143 __u32 num_data_stripes_hi;
144 __u32 filler[4]; /* expansion area */
145 #define IMSM_ORD_REBUILD (1 << 24)
146 __u32 disk_ord_tbl[1]; /* disk_ord_tbl[num_members],
147 * top byte contains some flags
148 */
149 } __attribute__ ((packed));
150
151 struct imsm_vol {
152 __u32 curr_migr_unit;
153 __u32 checkpoint_id; /* id to access curr_migr_unit */
154 __u8 migr_state; /* Normal or Migrating */
155 #define MIGR_INIT 0
156 #define MIGR_REBUILD 1
157 #define MIGR_VERIFY 2 /* analagous to echo check > sync_action */
158 #define MIGR_GEN_MIGR 3
159 #define MIGR_STATE_CHANGE 4
160 #define MIGR_REPAIR 5
161 __u8 migr_type; /* Initializing, Rebuilding, ... */
162 #define RAIDVOL_CLEAN 0
163 #define RAIDVOL_DIRTY 1
164 #define RAIDVOL_DSRECORD_VALID 2
165 __u8 dirty;
166 __u8 fs_state; /* fast-sync state for CnG (0xff == disabled) */
167 __u16 verify_errors; /* number of mismatches */
168 __u16 bad_blocks; /* number of bad blocks during verify */
169 __u32 filler[4];
170 struct imsm_map map[1];
171 /* here comes another one if migr_state */
172 } __attribute__ ((packed));
173
174 struct imsm_dev {
175 __u8 volume[MAX_RAID_SERIAL_LEN];
176 __u32 size_low;
177 __u32 size_high;
178 #define DEV_BOOTABLE __cpu_to_le32(0x01)
179 #define DEV_BOOT_DEVICE __cpu_to_le32(0x02)
180 #define DEV_READ_COALESCING __cpu_to_le32(0x04)
181 #define DEV_WRITE_COALESCING __cpu_to_le32(0x08)
182 #define DEV_LAST_SHUTDOWN_DIRTY __cpu_to_le32(0x10)
183 #define DEV_HIDDEN_AT_BOOT __cpu_to_le32(0x20)
184 #define DEV_CURRENTLY_HIDDEN __cpu_to_le32(0x40)
185 #define DEV_VERIFY_AND_FIX __cpu_to_le32(0x80)
186 #define DEV_MAP_STATE_UNINIT __cpu_to_le32(0x100)
187 #define DEV_NO_AUTO_RECOVERY __cpu_to_le32(0x200)
188 #define DEV_CLONE_N_GO __cpu_to_le32(0x400)
189 #define DEV_CLONE_MAN_SYNC __cpu_to_le32(0x800)
190 #define DEV_CNG_MASTER_DISK_NUM __cpu_to_le32(0x1000)
191 __u32 status; /* Persistent RaidDev status */
192 __u32 reserved_blocks; /* Reserved blocks at beginning of volume */
193 __u8 migr_priority;
194 __u8 num_sub_vols;
195 __u8 tid;
196 __u8 cng_master_disk;
197 __u16 cache_policy;
198 __u8 cng_state;
199 __u8 cng_sub_state;
200 __u16 my_vol_raid_dev_num; /* Used in Unique volume Id for this RaidDev */
201
202 /* NVM_EN */
203 __u8 nv_cache_mode;
204 __u8 nv_cache_flags;
205
206 /* Unique Volume Id of the NvCache Volume associated with this volume */
207 __u32 nvc_vol_orig_family_num;
208 __u16 nvc_vol_raid_dev_num;
209
210 #define RWH_OFF 0
211 #define RWH_DISTRIBUTED 1
212 #define RWH_JOURNALING_DRIVE 2
213 #define RWH_MULTIPLE_DISTRIBUTED 3
214 #define RWH_MULTIPLE_PPLS_JOURNALING_DRIVE 4
215 #define RWH_MULTIPLE_OFF 5
216 __u8 rwh_policy; /* Raid Write Hole Policy */
217 __u8 jd_serial[MAX_RAID_SERIAL_LEN]; /* Journal Drive serial number */
218 __u8 filler1;
219
220 #define IMSM_DEV_FILLERS 3
221 __u32 filler[IMSM_DEV_FILLERS];
222 struct imsm_vol vol;
223 } __attribute__ ((packed));
224
225 struct imsm_super {
226 __u8 sig[MAX_SIGNATURE_LENGTH]; /* 0x00 - 0x1F */
227 __u32 check_sum; /* 0x20 - 0x23 MPB Checksum */
228 __u32 mpb_size; /* 0x24 - 0x27 Size of MPB */
229 __u32 family_num; /* 0x28 - 0x2B Checksum from first time this config was written */
230 __u32 generation_num; /* 0x2C - 0x2F Incremented each time this array's MPB is written */
231 __u32 error_log_size; /* 0x30 - 0x33 in bytes */
232 __u32 attributes; /* 0x34 - 0x37 */
233 __u8 num_disks; /* 0x38 Number of configured disks */
234 __u8 num_raid_devs; /* 0x39 Number of configured volumes */
235 __u8 error_log_pos; /* 0x3A */
236 __u8 fill[1]; /* 0x3B */
237 __u32 cache_size; /* 0x3c - 0x40 in mb */
238 __u32 orig_family_num; /* 0x40 - 0x43 original family num */
239 __u32 pwr_cycle_count; /* 0x44 - 0x47 simulated power cycle count for array */
240 __u32 bbm_log_size; /* 0x48 - 0x4B - size of bad Block Mgmt Log in bytes */
241 __u16 num_raid_devs_created; /* 0x4C - 0x4D Used for generating unique
242 * volume IDs for raid_dev created in this array
243 * (starts at 1)
244 */
245 __u16 filler1; /* 0x4E - 0x4F */
246 #define IMSM_FILLERS 34
247 __u32 filler[IMSM_FILLERS]; /* 0x50 - 0xD7 RAID_MPB_FILLERS */
248 struct imsm_disk disk[1]; /* 0xD8 diskTbl[numDisks] */
249 /* here comes imsm_dev[num_raid_devs] */
250 /* here comes BBM logs */
251 } __attribute__ ((packed));
252
253 #define BBM_LOG_MAX_ENTRIES 254
254 #define BBM_LOG_MAX_LBA_ENTRY_VAL 256 /* Represents 256 LBAs */
255 #define BBM_LOG_SIGNATURE 0xabadb10c
256
257 struct bbm_log_block_addr {
258 __u16 w1;
259 __u32 dw1;
260 } __attribute__ ((__packed__));
261
262 struct bbm_log_entry {
263 __u8 marked_count; /* Number of blocks marked - 1 */
264 __u8 disk_ordinal; /* Disk entry within the imsm_super */
265 struct bbm_log_block_addr defective_block_start;
266 } __attribute__ ((__packed__));
267
268 struct bbm_log {
269 __u32 signature; /* 0xABADB10C */
270 __u32 entry_count;
271 struct bbm_log_entry marked_block_entries[BBM_LOG_MAX_ENTRIES];
272 } __attribute__ ((__packed__));
273
274 static char *map_state_str[] = { "normal", "uninitialized", "degraded", "failed" };
275
276 #define BLOCKS_PER_KB (1024/512)
277
278 #define RAID_DISK_RESERVED_BLOCKS_IMSM_HI 2209
279
280 #define GEN_MIGR_AREA_SIZE 2048 /* General Migration Copy Area size in blocks */
281
282 #define MIGR_REC_BUF_SECTORS 1 /* size of migr_record i/o buffer in sectors */
283 #define MIGR_REC_SECTOR_POSITION 1 /* migr_record position offset on disk,
284 * MIGR_REC_BUF_SECTORS <= MIGR_REC_SECTOR_POS
285 */
286
287 #define UNIT_SRC_NORMAL 0 /* Source data for curr_migr_unit must
288 * be recovered using srcMap */
289 #define UNIT_SRC_IN_CP_AREA 1 /* Source data for curr_migr_unit has
290 * already been migrated and must
291 * be recovered from checkpoint area */
292
293 #define PPL_ENTRY_SPACE (128 * 1024) /* Size of single PPL, without the header */
294
295 struct migr_record {
296 __u32 rec_status; /* Status used to determine how to restart
297 * migration in case it aborts
298 * in some fashion */
299 __u32 curr_migr_unit_lo; /* 0..numMigrUnits-1 */
300 __u32 family_num; /* Family number of MPB
301 * containing the RaidDev
302 * that is migrating */
303 __u32 ascending_migr; /* True if migrating in increasing
304 * order of lbas */
305 __u32 blocks_per_unit; /* Num disk blocks per unit of operation */
306 __u32 dest_depth_per_unit; /* Num member blocks each destMap
307 * member disk
308 * advances per unit-of-operation */
309 __u32 ckpt_area_pba_lo; /* Pba of first block of ckpt copy area */
310 __u32 dest_1st_member_lba_lo; /* First member lba on first
311 * stripe of destination */
312 __u32 num_migr_units_lo; /* Total num migration units-of-op */
313 __u32 post_migr_vol_cap; /* Size of volume after
314 * migration completes */
315 __u32 post_migr_vol_cap_hi; /* Expansion space for LBA64 */
316 __u32 ckpt_read_disk_num; /* Which member disk in destSubMap[0] the
317 * migration ckpt record was read from
318 * (for recovered migrations) */
319 __u32 curr_migr_unit_hi; /* 0..numMigrUnits-1 high order 32 bits */
320 __u32 ckpt_area_pba_hi; /* Pba of first block of ckpt copy area
321 * high order 32 bits */
322 __u32 dest_1st_member_lba_hi; /* First member lba on first stripe of
323 * destination - high order 32 bits */
324 __u32 num_migr_units_hi; /* Total num migration units-of-op
325 * high order 32 bits */
326 } __attribute__ ((__packed__));
327
328 struct md_list {
329 /* usage marker:
330 * 1: load metadata
331 * 2: metadata does not match
332 * 4: already checked
333 */
334 int used;
335 char *devname;
336 int found;
337 int container;
338 dev_t st_rdev;
339 struct md_list *next;
340 };
341
342 #define pr_vrb(fmt, arg...) (void) (verbose && pr_err(fmt, ##arg))
343
344 static __u8 migr_type(struct imsm_dev *dev)
345 {
346 if (dev->vol.migr_type == MIGR_VERIFY &&
347 dev->status & DEV_VERIFY_AND_FIX)
348 return MIGR_REPAIR;
349 else
350 return dev->vol.migr_type;
351 }
352
353 static void set_migr_type(struct imsm_dev *dev, __u8 migr_type)
354 {
355 /* for compatibility with older oroms convert MIGR_REPAIR, into
356 * MIGR_VERIFY w/ DEV_VERIFY_AND_FIX status
357 */
358 if (migr_type == MIGR_REPAIR) {
359 dev->vol.migr_type = MIGR_VERIFY;
360 dev->status |= DEV_VERIFY_AND_FIX;
361 } else {
362 dev->vol.migr_type = migr_type;
363 dev->status &= ~DEV_VERIFY_AND_FIX;
364 }
365 }
366
367 static unsigned int sector_count(__u32 bytes, unsigned int sector_size)
368 {
369 return ROUND_UP(bytes, sector_size) / sector_size;
370 }
371
372 static unsigned int mpb_sectors(struct imsm_super *mpb,
373 unsigned int sector_size)
374 {
375 return sector_count(__le32_to_cpu(mpb->mpb_size), sector_size);
376 }
377
378 struct intel_dev {
379 struct imsm_dev *dev;
380 struct intel_dev *next;
381 unsigned index;
382 };
383
384 struct intel_hba {
385 enum sys_dev_type type;
386 char *path;
387 char *pci_id;
388 struct intel_hba *next;
389 };
390
391 enum action {
392 DISK_REMOVE = 1,
393 DISK_ADD
394 };
395 /* internal representation of IMSM metadata */
396 struct intel_super {
397 union {
398 void *buf; /* O_DIRECT buffer for reading/writing metadata */
399 struct imsm_super *anchor; /* immovable parameters */
400 };
401 union {
402 void *migr_rec_buf; /* buffer for I/O operations */
403 struct migr_record *migr_rec; /* migration record */
404 };
405 int clean_migration_record_by_mdmon; /* when reshape is switched to next
406 array, it indicates that mdmon is allowed to clean migration
407 record */
408 size_t len; /* size of the 'buf' allocation */
409 size_t extra_space; /* extra space in 'buf' that is not used yet */
410 void *next_buf; /* for realloc'ing buf from the manager */
411 size_t next_len;
412 int updates_pending; /* count of pending updates for mdmon */
413 int current_vol; /* index of raid device undergoing creation */
414 unsigned long long create_offset; /* common start for 'current_vol' */
415 __u32 random; /* random data for seeding new family numbers */
416 struct intel_dev *devlist;
417 unsigned int sector_size; /* sector size of used member drives */
418 struct dl {
419 struct dl *next;
420 int index;
421 __u8 serial[MAX_RAID_SERIAL_LEN];
422 int major, minor;
423 char *devname;
424 struct imsm_disk disk;
425 int fd;
426 int extent_cnt;
427 struct extent *e; /* for determining freespace @ create */
428 int raiddisk; /* slot to fill in autolayout */
429 enum action action;
430 } *disks, *current_disk;
431 struct dl *disk_mgmt_list; /* list of disks to add/remove while mdmon
432 active */
433 struct dl *missing; /* disks removed while we weren't looking */
434 struct bbm_log *bbm_log;
435 struct intel_hba *hba; /* device path of the raid controller for this metadata */
436 const struct imsm_orom *orom; /* platform firmware support */
437 struct intel_super *next; /* (temp) list for disambiguating family_num */
438 struct md_bb bb; /* memory for get_bad_blocks call */
439 };
440
441 struct intel_disk {
442 struct imsm_disk disk;
443 #define IMSM_UNKNOWN_OWNER (-1)
444 int owner;
445 struct intel_disk *next;
446 };
447
448 struct extent {
449 unsigned long long start, size;
450 };
451
452 /* definitions of reshape process types */
453 enum imsm_reshape_type {
454 CH_TAKEOVER,
455 CH_MIGRATION,
456 CH_ARRAY_SIZE,
457 };
458
459 /* definition of messages passed to imsm_process_update */
460 enum imsm_update_type {
461 update_activate_spare,
462 update_create_array,
463 update_kill_array,
464 update_rename_array,
465 update_add_remove_disk,
466 update_reshape_container_disks,
467 update_reshape_migration,
468 update_takeover,
469 update_general_migration_checkpoint,
470 update_size_change,
471 update_prealloc_badblocks_mem,
472 update_rwh_policy,
473 };
474
475 struct imsm_update_activate_spare {
476 enum imsm_update_type type;
477 struct dl *dl;
478 int slot;
479 int array;
480 struct imsm_update_activate_spare *next;
481 };
482
483 struct geo_params {
484 char devnm[32];
485 char *dev_name;
486 unsigned long long size;
487 int level;
488 int layout;
489 int chunksize;
490 int raid_disks;
491 };
492
493 enum takeover_direction {
494 R10_TO_R0,
495 R0_TO_R10
496 };
497 struct imsm_update_takeover {
498 enum imsm_update_type type;
499 int subarray;
500 enum takeover_direction direction;
501 };
502
503 struct imsm_update_reshape {
504 enum imsm_update_type type;
505 int old_raid_disks;
506 int new_raid_disks;
507
508 int new_disks[1]; /* new_raid_disks - old_raid_disks makedev number */
509 };
510
511 struct imsm_update_reshape_migration {
512 enum imsm_update_type type;
513 int old_raid_disks;
514 int new_raid_disks;
515 /* fields for array migration changes
516 */
517 int subdev;
518 int new_level;
519 int new_layout;
520 int new_chunksize;
521
522 int new_disks[1]; /* new_raid_disks - old_raid_disks makedev number */
523 };
524
525 struct imsm_update_size_change {
526 enum imsm_update_type type;
527 int subdev;
528 long long new_size;
529 };
530
531 struct imsm_update_general_migration_checkpoint {
532 enum imsm_update_type type;
533 __u32 curr_migr_unit;
534 };
535
536 struct disk_info {
537 __u8 serial[MAX_RAID_SERIAL_LEN];
538 };
539
540 struct imsm_update_create_array {
541 enum imsm_update_type type;
542 int dev_idx;
543 struct imsm_dev dev;
544 };
545
546 struct imsm_update_kill_array {
547 enum imsm_update_type type;
548 int dev_idx;
549 };
550
551 struct imsm_update_rename_array {
552 enum imsm_update_type type;
553 __u8 name[MAX_RAID_SERIAL_LEN];
554 int dev_idx;
555 };
556
557 struct imsm_update_add_remove_disk {
558 enum imsm_update_type type;
559 };
560
561 struct imsm_update_prealloc_bb_mem {
562 enum imsm_update_type type;
563 };
564
565 struct imsm_update_rwh_policy {
566 enum imsm_update_type type;
567 int new_policy;
568 int dev_idx;
569 };
570
571 static const char *_sys_dev_type[] = {
572 [SYS_DEV_UNKNOWN] = "Unknown",
573 [SYS_DEV_SAS] = "SAS",
574 [SYS_DEV_SATA] = "SATA",
575 [SYS_DEV_NVME] = "NVMe",
576 [SYS_DEV_VMD] = "VMD"
577 };
578
579 const char *get_sys_dev_type(enum sys_dev_type type)
580 {
581 if (type >= SYS_DEV_MAX)
582 type = SYS_DEV_UNKNOWN;
583
584 return _sys_dev_type[type];
585 }
586
587 static struct intel_hba * alloc_intel_hba(struct sys_dev *device)
588 {
589 struct intel_hba *result = xmalloc(sizeof(*result));
590
591 result->type = device->type;
592 result->path = xstrdup(device->path);
593 result->next = NULL;
594 if (result->path && (result->pci_id = strrchr(result->path, '/')) != NULL)
595 result->pci_id++;
596
597 return result;
598 }
599
600 static struct intel_hba * find_intel_hba(struct intel_hba *hba, struct sys_dev *device)
601 {
602 struct intel_hba *result;
603
604 for (result = hba; result; result = result->next) {
605 if (result->type == device->type && strcmp(result->path, device->path) == 0)
606 break;
607 }
608 return result;
609 }
610
611 static int attach_hba_to_super(struct intel_super *super, struct sys_dev *device)
612 {
613 struct intel_hba *hba;
614
615 /* check if disk attached to Intel HBA */
616 hba = find_intel_hba(super->hba, device);
617 if (hba != NULL)
618 return 1;
619 /* Check if HBA is already attached to super */
620 if (super->hba == NULL) {
621 super->hba = alloc_intel_hba(device);
622 return 1;
623 }
624
625 hba = super->hba;
626 /* Intel metadata allows for all disks attached to the same type HBA.
627 * Do not support HBA types mixing
628 */
629 if (device->type != hba->type)
630 return 2;
631
632 /* Multiple same type HBAs can be used if they share the same OROM */
633 const struct imsm_orom *device_orom = get_orom_by_device_id(device->dev_id);
634
635 if (device_orom != super->orom)
636 return 2;
637
638 while (hba->next)
639 hba = hba->next;
640
641 hba->next = alloc_intel_hba(device);
642 return 1;
643 }
644
645 static struct sys_dev* find_disk_attached_hba(int fd, const char *devname)
646 {
647 struct sys_dev *list, *elem;
648 char *disk_path;
649
650 if ((list = find_intel_devices()) == NULL)
651 return 0;
652
653 if (fd < 0)
654 disk_path = (char *) devname;
655 else
656 disk_path = diskfd_to_devpath(fd);
657
658 if (!disk_path)
659 return 0;
660
661 for (elem = list; elem; elem = elem->next)
662 if (path_attached_to_hba(disk_path, elem->path))
663 return elem;
664
665 if (disk_path != devname)
666 free(disk_path);
667
668 return NULL;
669 }
670
671 static int find_intel_hba_capability(int fd, struct intel_super *super,
672 char *devname);
673
674 static struct supertype *match_metadata_desc_imsm(char *arg)
675 {
676 struct supertype *st;
677
678 if (strcmp(arg, "imsm") != 0 &&
679 strcmp(arg, "default") != 0
680 )
681 return NULL;
682
683 st = xcalloc(1, sizeof(*st));
684 st->ss = &super_imsm;
685 st->max_devs = IMSM_MAX_DEVICES;
686 st->minor_version = 0;
687 st->sb = NULL;
688 return st;
689 }
690
691 static __u8 *get_imsm_version(struct imsm_super *mpb)
692 {
693 return &mpb->sig[MPB_SIG_LEN];
694 }
695
696 /* retrieve a disk directly from the anchor when the anchor is known to be
697 * up-to-date, currently only at load time
698 */
699 static struct imsm_disk *__get_imsm_disk(struct imsm_super *mpb, __u8 index)
700 {
701 if (index >= mpb->num_disks)
702 return NULL;
703 return &mpb->disk[index];
704 }
705
706 /* retrieve the disk description based on a index of the disk
707 * in the sub-array
708 */
709 static struct dl *get_imsm_dl_disk(struct intel_super *super, __u8 index)
710 {
711 struct dl *d;
712
713 for (d = super->disks; d; d = d->next)
714 if (d->index == index)
715 return d;
716
717 return NULL;
718 }
719 /* retrieve a disk from the parsed metadata */
720 static struct imsm_disk *get_imsm_disk(struct intel_super *super, __u8 index)
721 {
722 struct dl *dl;
723
724 dl = get_imsm_dl_disk(super, index);
725 if (dl)
726 return &dl->disk;
727
728 return NULL;
729 }
730
731 /* generate a checksum directly from the anchor when the anchor is known to be
732 * up-to-date, currently only at load or write_super after coalescing
733 */
734 static __u32 __gen_imsm_checksum(struct imsm_super *mpb)
735 {
736 __u32 end = mpb->mpb_size / sizeof(end);
737 __u32 *p = (__u32 *) mpb;
738 __u32 sum = 0;
739
740 while (end--) {
741 sum += __le32_to_cpu(*p);
742 p++;
743 }
744
745 return sum - __le32_to_cpu(mpb->check_sum);
746 }
747
748 static size_t sizeof_imsm_map(struct imsm_map *map)
749 {
750 return sizeof(struct imsm_map) + sizeof(__u32) * (map->num_members - 1);
751 }
752
753 struct imsm_map *get_imsm_map(struct imsm_dev *dev, int second_map)
754 {
755 /* A device can have 2 maps if it is in the middle of a migration.
756 * If second_map is:
757 * MAP_0 - we return the first map
758 * MAP_1 - we return the second map if it exists, else NULL
759 * MAP_X - we return the second map if it exists, else the first
760 */
761 struct imsm_map *map = &dev->vol.map[0];
762 struct imsm_map *map2 = NULL;
763
764 if (dev->vol.migr_state)
765 map2 = (void *)map + sizeof_imsm_map(map);
766
767 switch (second_map) {
768 case MAP_0:
769 break;
770 case MAP_1:
771 map = map2;
772 break;
773 case MAP_X:
774 if (map2)
775 map = map2;
776 break;
777 default:
778 map = NULL;
779 }
780 return map;
781
782 }
783
784 /* return the size of the device.
785 * migr_state increases the returned size if map[0] were to be duplicated
786 */
787 static size_t sizeof_imsm_dev(struct imsm_dev *dev, int migr_state)
788 {
789 size_t size = sizeof(*dev) - sizeof(struct imsm_map) +
790 sizeof_imsm_map(get_imsm_map(dev, MAP_0));
791
792 /* migrating means an additional map */
793 if (dev->vol.migr_state)
794 size += sizeof_imsm_map(get_imsm_map(dev, MAP_1));
795 else if (migr_state)
796 size += sizeof_imsm_map(get_imsm_map(dev, MAP_0));
797
798 return size;
799 }
800
801 /* retrieve disk serial number list from a metadata update */
802 static struct disk_info *get_disk_info(struct imsm_update_create_array *update)
803 {
804 void *u = update;
805 struct disk_info *inf;
806
807 inf = u + sizeof(*update) - sizeof(struct imsm_dev) +
808 sizeof_imsm_dev(&update->dev, 0);
809
810 return inf;
811 }
812
813 static struct imsm_dev *__get_imsm_dev(struct imsm_super *mpb, __u8 index)
814 {
815 int offset;
816 int i;
817 void *_mpb = mpb;
818
819 if (index >= mpb->num_raid_devs)
820 return NULL;
821
822 /* devices start after all disks */
823 offset = ((void *) &mpb->disk[mpb->num_disks]) - _mpb;
824
825 for (i = 0; i <= index; i++)
826 if (i == index)
827 return _mpb + offset;
828 else
829 offset += sizeof_imsm_dev(_mpb + offset, 0);
830
831 return NULL;
832 }
833
834 static struct imsm_dev *get_imsm_dev(struct intel_super *super, __u8 index)
835 {
836 struct intel_dev *dv;
837
838 if (index >= super->anchor->num_raid_devs)
839 return NULL;
840 for (dv = super->devlist; dv; dv = dv->next)
841 if (dv->index == index)
842 return dv->dev;
843 return NULL;
844 }
845
846 static inline unsigned long long __le48_to_cpu(const struct bbm_log_block_addr
847 *addr)
848 {
849 return ((((__u64)__le32_to_cpu(addr->dw1)) << 16) |
850 __le16_to_cpu(addr->w1));
851 }
852
853 static inline struct bbm_log_block_addr __cpu_to_le48(unsigned long long sec)
854 {
855 struct bbm_log_block_addr addr;
856
857 addr.w1 = __cpu_to_le16((__u16)(sec & 0xffff));
858 addr.dw1 = __cpu_to_le32((__u32)(sec >> 16) & 0xffffffff);
859 return addr;
860 }
861
862 /* get size of the bbm log */
863 static __u32 get_imsm_bbm_log_size(struct bbm_log *log)
864 {
865 if (!log || log->entry_count == 0)
866 return 0;
867
868 return sizeof(log->signature) +
869 sizeof(log->entry_count) +
870 log->entry_count * sizeof(struct bbm_log_entry);
871 }
872
873 /* check if bad block is not partially stored in bbm log */
874 static int is_stored_in_bbm(struct bbm_log *log, const __u8 idx, const unsigned
875 long long sector, const int length, __u32 *pos)
876 {
877 __u32 i;
878
879 for (i = *pos; i < log->entry_count; i++) {
880 struct bbm_log_entry *entry = &log->marked_block_entries[i];
881 unsigned long long bb_start;
882 unsigned long long bb_end;
883
884 bb_start = __le48_to_cpu(&entry->defective_block_start);
885 bb_end = bb_start + (entry->marked_count + 1);
886
887 if ((entry->disk_ordinal == idx) && (bb_start >= sector) &&
888 (bb_end <= sector + length)) {
889 *pos = i;
890 return 1;
891 }
892 }
893 return 0;
894 }
895
896 /* record new bad block in bbm log */
897 static int record_new_badblock(struct bbm_log *log, const __u8 idx, unsigned
898 long long sector, int length)
899 {
900 int new_bb = 0;
901 __u32 pos = 0;
902 struct bbm_log_entry *entry = NULL;
903
904 while (is_stored_in_bbm(log, idx, sector, length, &pos)) {
905 struct bbm_log_entry *e = &log->marked_block_entries[pos];
906
907 if ((e->marked_count + 1 == BBM_LOG_MAX_LBA_ENTRY_VAL) &&
908 (__le48_to_cpu(&e->defective_block_start) == sector)) {
909 sector += BBM_LOG_MAX_LBA_ENTRY_VAL;
910 length -= BBM_LOG_MAX_LBA_ENTRY_VAL;
911 pos = pos + 1;
912 continue;
913 }
914 entry = e;
915 break;
916 }
917
918 if (entry) {
919 int cnt = (length <= BBM_LOG_MAX_LBA_ENTRY_VAL) ? length :
920 BBM_LOG_MAX_LBA_ENTRY_VAL;
921 entry->defective_block_start = __cpu_to_le48(sector);
922 entry->marked_count = cnt - 1;
923 if (cnt == length)
924 return 1;
925 sector += cnt;
926 length -= cnt;
927 }
928
929 new_bb = ROUND_UP(length, BBM_LOG_MAX_LBA_ENTRY_VAL) /
930 BBM_LOG_MAX_LBA_ENTRY_VAL;
931 if (log->entry_count + new_bb > BBM_LOG_MAX_ENTRIES)
932 return 0;
933
934 while (length > 0) {
935 int cnt = (length <= BBM_LOG_MAX_LBA_ENTRY_VAL) ? length :
936 BBM_LOG_MAX_LBA_ENTRY_VAL;
937 struct bbm_log_entry *entry =
938 &log->marked_block_entries[log->entry_count];
939
940 entry->defective_block_start = __cpu_to_le48(sector);
941 entry->marked_count = cnt - 1;
942 entry->disk_ordinal = idx;
943
944 sector += cnt;
945 length -= cnt;
946
947 log->entry_count++;
948 }
949
950 return new_bb;
951 }
952
953 /* clear all bad blocks for given disk */
954 static void clear_disk_badblocks(struct bbm_log *log, const __u8 idx)
955 {
956 __u32 i = 0;
957
958 while (i < log->entry_count) {
959 struct bbm_log_entry *entries = log->marked_block_entries;
960
961 if (entries[i].disk_ordinal == idx) {
962 if (i < log->entry_count - 1)
963 entries[i] = entries[log->entry_count - 1];
964 log->entry_count--;
965 } else {
966 i++;
967 }
968 }
969 }
970
971 /* clear given bad block */
972 static int clear_badblock(struct bbm_log *log, const __u8 idx, const unsigned
973 long long sector, const int length) {
974 __u32 i = 0;
975
976 while (i < log->entry_count) {
977 struct bbm_log_entry *entries = log->marked_block_entries;
978
979 if ((entries[i].disk_ordinal == idx) &&
980 (__le48_to_cpu(&entries[i].defective_block_start) ==
981 sector) && (entries[i].marked_count + 1 == length)) {
982 if (i < log->entry_count - 1)
983 entries[i] = entries[log->entry_count - 1];
984 log->entry_count--;
985 break;
986 }
987 i++;
988 }
989
990 return 1;
991 }
992
993 /* allocate and load BBM log from metadata */
994 static int load_bbm_log(struct intel_super *super)
995 {
996 struct imsm_super *mpb = super->anchor;
997 __u32 bbm_log_size = __le32_to_cpu(mpb->bbm_log_size);
998
999 super->bbm_log = xcalloc(1, sizeof(struct bbm_log));
1000 if (!super->bbm_log)
1001 return 1;
1002
1003 if (bbm_log_size) {
1004 struct bbm_log *log = (void *)mpb +
1005 __le32_to_cpu(mpb->mpb_size) - bbm_log_size;
1006
1007 __u32 entry_count;
1008
1009 if (bbm_log_size < sizeof(log->signature) +
1010 sizeof(log->entry_count))
1011 return 2;
1012
1013 entry_count = __le32_to_cpu(log->entry_count);
1014 if ((__le32_to_cpu(log->signature) != BBM_LOG_SIGNATURE) ||
1015 (entry_count > BBM_LOG_MAX_ENTRIES))
1016 return 3;
1017
1018 if (bbm_log_size !=
1019 sizeof(log->signature) + sizeof(log->entry_count) +
1020 entry_count * sizeof(struct bbm_log_entry))
1021 return 4;
1022
1023 memcpy(super->bbm_log, log, bbm_log_size);
1024 } else {
1025 super->bbm_log->signature = __cpu_to_le32(BBM_LOG_SIGNATURE);
1026 super->bbm_log->entry_count = 0;
1027 }
1028
1029 return 0;
1030 }
1031
1032 /* checks if bad block is within volume boundaries */
1033 static int is_bad_block_in_volume(const struct bbm_log_entry *entry,
1034 const unsigned long long start_sector,
1035 const unsigned long long size)
1036 {
1037 unsigned long long bb_start;
1038 unsigned long long bb_end;
1039
1040 bb_start = __le48_to_cpu(&entry->defective_block_start);
1041 bb_end = bb_start + (entry->marked_count + 1);
1042
1043 if (((bb_start >= start_sector) && (bb_start < start_sector + size)) ||
1044 ((bb_end >= start_sector) && (bb_end <= start_sector + size)))
1045 return 1;
1046
1047 return 0;
1048 }
1049
1050 /* get list of bad blocks on a drive for a volume */
1051 static void get_volume_badblocks(const struct bbm_log *log, const __u8 idx,
1052 const unsigned long long start_sector,
1053 const unsigned long long size,
1054 struct md_bb *bbs)
1055 {
1056 __u32 count = 0;
1057 __u32 i;
1058
1059 for (i = 0; i < log->entry_count; i++) {
1060 const struct bbm_log_entry *ent =
1061 &log->marked_block_entries[i];
1062 struct md_bb_entry *bb;
1063
1064 if ((ent->disk_ordinal == idx) &&
1065 is_bad_block_in_volume(ent, start_sector, size)) {
1066
1067 if (!bbs->entries) {
1068 bbs->entries = xmalloc(BBM_LOG_MAX_ENTRIES *
1069 sizeof(*bb));
1070 if (!bbs->entries)
1071 break;
1072 }
1073
1074 bb = &bbs->entries[count++];
1075 bb->sector = __le48_to_cpu(&ent->defective_block_start);
1076 bb->length = ent->marked_count + 1;
1077 }
1078 }
1079 bbs->count = count;
1080 }
1081
1082 /*
1083 * for second_map:
1084 * == MAP_0 get first map
1085 * == MAP_1 get second map
1086 * == MAP_X than get map according to the current migr_state
1087 */
1088 static __u32 get_imsm_ord_tbl_ent(struct imsm_dev *dev,
1089 int slot,
1090 int second_map)
1091 {
1092 struct imsm_map *map;
1093
1094 map = get_imsm_map(dev, second_map);
1095
1096 /* top byte identifies disk under rebuild */
1097 return __le32_to_cpu(map->disk_ord_tbl[slot]);
1098 }
1099
1100 #define ord_to_idx(ord) (((ord) << 8) >> 8)
1101 static __u32 get_imsm_disk_idx(struct imsm_dev *dev, int slot, int second_map)
1102 {
1103 __u32 ord = get_imsm_ord_tbl_ent(dev, slot, second_map);
1104
1105 return ord_to_idx(ord);
1106 }
1107
1108 static void set_imsm_ord_tbl_ent(struct imsm_map *map, int slot, __u32 ord)
1109 {
1110 map->disk_ord_tbl[slot] = __cpu_to_le32(ord);
1111 }
1112
1113 static int get_imsm_disk_slot(struct imsm_map *map, unsigned idx)
1114 {
1115 int slot;
1116 __u32 ord;
1117
1118 for (slot = 0; slot < map->num_members; slot++) {
1119 ord = __le32_to_cpu(map->disk_ord_tbl[slot]);
1120 if (ord_to_idx(ord) == idx)
1121 return slot;
1122 }
1123
1124 return -1;
1125 }
1126
1127 static int get_imsm_raid_level(struct imsm_map *map)
1128 {
1129 if (map->raid_level == 1) {
1130 if (map->num_members == 2)
1131 return 1;
1132 else
1133 return 10;
1134 }
1135
1136 return map->raid_level;
1137 }
1138
1139 static int cmp_extent(const void *av, const void *bv)
1140 {
1141 const struct extent *a = av;
1142 const struct extent *b = bv;
1143 if (a->start < b->start)
1144 return -1;
1145 if (a->start > b->start)
1146 return 1;
1147 return 0;
1148 }
1149
1150 static int count_memberships(struct dl *dl, struct intel_super *super)
1151 {
1152 int memberships = 0;
1153 int i;
1154
1155 for (i = 0; i < super->anchor->num_raid_devs; i++) {
1156 struct imsm_dev *dev = get_imsm_dev(super, i);
1157 struct imsm_map *map = get_imsm_map(dev, MAP_0);
1158
1159 if (get_imsm_disk_slot(map, dl->index) >= 0)
1160 memberships++;
1161 }
1162
1163 return memberships;
1164 }
1165
1166 static __u32 imsm_min_reserved_sectors(struct intel_super *super);
1167
1168 static int split_ull(unsigned long long n, __u32 *lo, __u32 *hi)
1169 {
1170 if (lo == 0 || hi == 0)
1171 return 1;
1172 *lo = __le32_to_cpu((unsigned)n);
1173 *hi = __le32_to_cpu((unsigned)(n >> 32));
1174 return 0;
1175 }
1176
1177 static unsigned long long join_u32(__u32 lo, __u32 hi)
1178 {
1179 return (unsigned long long)__le32_to_cpu(lo) |
1180 (((unsigned long long)__le32_to_cpu(hi)) << 32);
1181 }
1182
1183 static unsigned long long total_blocks(struct imsm_disk *disk)
1184 {
1185 if (disk == NULL)
1186 return 0;
1187 return join_u32(disk->total_blocks_lo, disk->total_blocks_hi);
1188 }
1189
1190 static unsigned long long pba_of_lba0(struct imsm_map *map)
1191 {
1192 if (map == NULL)
1193 return 0;
1194 return join_u32(map->pba_of_lba0_lo, map->pba_of_lba0_hi);
1195 }
1196
1197 static unsigned long long blocks_per_member(struct imsm_map *map)
1198 {
1199 if (map == NULL)
1200 return 0;
1201 return join_u32(map->blocks_per_member_lo, map->blocks_per_member_hi);
1202 }
1203
1204 static unsigned long long num_data_stripes(struct imsm_map *map)
1205 {
1206 if (map == NULL)
1207 return 0;
1208 return join_u32(map->num_data_stripes_lo, map->num_data_stripes_hi);
1209 }
1210
1211 static unsigned long long imsm_dev_size(struct imsm_dev *dev)
1212 {
1213 if (dev == NULL)
1214 return 0;
1215 return join_u32(dev->size_low, dev->size_high);
1216 }
1217
1218 static unsigned long long migr_chkp_area_pba(struct migr_record *migr_rec)
1219 {
1220 if (migr_rec == NULL)
1221 return 0;
1222 return join_u32(migr_rec->ckpt_area_pba_lo,
1223 migr_rec->ckpt_area_pba_hi);
1224 }
1225
1226 static unsigned long long current_migr_unit(struct migr_record *migr_rec)
1227 {
1228 if (migr_rec == NULL)
1229 return 0;
1230 return join_u32(migr_rec->curr_migr_unit_lo,
1231 migr_rec->curr_migr_unit_hi);
1232 }
1233
1234 static unsigned long long migr_dest_1st_member_lba(struct migr_record *migr_rec)
1235 {
1236 if (migr_rec == NULL)
1237 return 0;
1238 return join_u32(migr_rec->dest_1st_member_lba_lo,
1239 migr_rec->dest_1st_member_lba_hi);
1240 }
1241
1242 static unsigned long long get_num_migr_units(struct migr_record *migr_rec)
1243 {
1244 if (migr_rec == NULL)
1245 return 0;
1246 return join_u32(migr_rec->num_migr_units_lo,
1247 migr_rec->num_migr_units_hi);
1248 }
1249
1250 static void set_total_blocks(struct imsm_disk *disk, unsigned long long n)
1251 {
1252 split_ull(n, &disk->total_blocks_lo, &disk->total_blocks_hi);
1253 }
1254
1255 static void set_pba_of_lba0(struct imsm_map *map, unsigned long long n)
1256 {
1257 split_ull(n, &map->pba_of_lba0_lo, &map->pba_of_lba0_hi);
1258 }
1259
1260 static void set_blocks_per_member(struct imsm_map *map, unsigned long long n)
1261 {
1262 split_ull(n, &map->blocks_per_member_lo, &map->blocks_per_member_hi);
1263 }
1264
1265 static void set_num_data_stripes(struct imsm_map *map, unsigned long long n)
1266 {
1267 split_ull(n, &map->num_data_stripes_lo, &map->num_data_stripes_hi);
1268 }
1269
1270 static void set_imsm_dev_size(struct imsm_dev *dev, unsigned long long n)
1271 {
1272 split_ull(n, &dev->size_low, &dev->size_high);
1273 }
1274
1275 static void set_migr_chkp_area_pba(struct migr_record *migr_rec,
1276 unsigned long long n)
1277 {
1278 split_ull(n, &migr_rec->ckpt_area_pba_lo, &migr_rec->ckpt_area_pba_hi);
1279 }
1280
1281 static void set_current_migr_unit(struct migr_record *migr_rec,
1282 unsigned long long n)
1283 {
1284 split_ull(n, &migr_rec->curr_migr_unit_lo,
1285 &migr_rec->curr_migr_unit_hi);
1286 }
1287
1288 static void set_migr_dest_1st_member_lba(struct migr_record *migr_rec,
1289 unsigned long long n)
1290 {
1291 split_ull(n, &migr_rec->dest_1st_member_lba_lo,
1292 &migr_rec->dest_1st_member_lba_hi);
1293 }
1294
1295 static void set_num_migr_units(struct migr_record *migr_rec,
1296 unsigned long long n)
1297 {
1298 split_ull(n, &migr_rec->num_migr_units_lo,
1299 &migr_rec->num_migr_units_hi);
1300 }
1301
1302 static unsigned long long per_dev_array_size(struct imsm_map *map)
1303 {
1304 unsigned long long array_size = 0;
1305
1306 if (map == NULL)
1307 return array_size;
1308
1309 array_size = num_data_stripes(map) * map->blocks_per_strip;
1310 if (get_imsm_raid_level(map) == 1 || get_imsm_raid_level(map) == 10)
1311 array_size *= 2;
1312
1313 return array_size;
1314 }
1315
1316 static struct extent *get_extents(struct intel_super *super, struct dl *dl,
1317 int get_minimal_reservation)
1318 {
1319 /* find a list of used extents on the given physical device */
1320 struct extent *rv, *e;
1321 int i;
1322 int memberships = count_memberships(dl, super);
1323 __u32 reservation;
1324
1325 /* trim the reserved area for spares, so they can join any array
1326 * regardless of whether the OROM has assigned sectors from the
1327 * IMSM_RESERVED_SECTORS region
1328 */
1329 if (dl->index == -1 || get_minimal_reservation)
1330 reservation = imsm_min_reserved_sectors(super);
1331 else
1332 reservation = MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS;
1333
1334 rv = xcalloc(sizeof(struct extent), (memberships + 1));
1335 e = rv;
1336
1337 for (i = 0; i < super->anchor->num_raid_devs; i++) {
1338 struct imsm_dev *dev = get_imsm_dev(super, i);
1339 struct imsm_map *map = get_imsm_map(dev, MAP_0);
1340
1341 if (get_imsm_disk_slot(map, dl->index) >= 0) {
1342 e->start = pba_of_lba0(map);
1343 e->size = per_dev_array_size(map);
1344 e++;
1345 }
1346 }
1347 qsort(rv, memberships, sizeof(*rv), cmp_extent);
1348
1349 /* determine the start of the metadata
1350 * when no raid devices are defined use the default
1351 * ...otherwise allow the metadata to truncate the value
1352 * as is the case with older versions of imsm
1353 */
1354 if (memberships) {
1355 struct extent *last = &rv[memberships - 1];
1356 unsigned long long remainder;
1357
1358 remainder = total_blocks(&dl->disk) - (last->start + last->size);
1359 /* round down to 1k block to satisfy precision of the kernel
1360 * 'size' interface
1361 */
1362 remainder &= ~1UL;
1363 /* make sure remainder is still sane */
1364 if (remainder < (unsigned)ROUND_UP(super->len, 512) >> 9)
1365 remainder = ROUND_UP(super->len, 512) >> 9;
1366 if (reservation > remainder)
1367 reservation = remainder;
1368 }
1369 e->start = total_blocks(&dl->disk) - reservation;
1370 e->size = 0;
1371 return rv;
1372 }
1373
1374 /* try to determine how much space is reserved for metadata from
1375 * the last get_extents() entry, otherwise fallback to the
1376 * default
1377 */
1378 static __u32 imsm_reserved_sectors(struct intel_super *super, struct dl *dl)
1379 {
1380 struct extent *e;
1381 int i;
1382 __u32 rv;
1383
1384 /* for spares just return a minimal reservation which will grow
1385 * once the spare is picked up by an array
1386 */
1387 if (dl->index == -1)
1388 return MPB_SECTOR_CNT;
1389
1390 e = get_extents(super, dl, 0);
1391 if (!e)
1392 return MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS;
1393
1394 /* scroll to last entry */
1395 for (i = 0; e[i].size; i++)
1396 continue;
1397
1398 rv = total_blocks(&dl->disk) - e[i].start;
1399
1400 free(e);
1401
1402 return rv;
1403 }
1404
1405 static int is_spare(struct imsm_disk *disk)
1406 {
1407 return (disk->status & SPARE_DISK) == SPARE_DISK;
1408 }
1409
1410 static int is_configured(struct imsm_disk *disk)
1411 {
1412 return (disk->status & CONFIGURED_DISK) == CONFIGURED_DISK;
1413 }
1414
1415 static int is_failed(struct imsm_disk *disk)
1416 {
1417 return (disk->status & FAILED_DISK) == FAILED_DISK;
1418 }
1419
1420 static int is_journal(struct imsm_disk *disk)
1421 {
1422 return (disk->status & JOURNAL_DISK) == JOURNAL_DISK;
1423 }
1424
1425 /* round array size down to closest MB and ensure it splits evenly
1426 * between members
1427 */
1428 static unsigned long long round_size_to_mb(unsigned long long size, unsigned int
1429 disk_count)
1430 {
1431 size /= disk_count;
1432 size = (size >> SECT_PER_MB_SHIFT) << SECT_PER_MB_SHIFT;
1433 size *= disk_count;
1434
1435 return size;
1436 }
1437
1438 static int able_to_resync(int raid_level, int missing_disks)
1439 {
1440 int max_missing_disks = 0;
1441
1442 switch (raid_level) {
1443 case 10:
1444 max_missing_disks = 1;
1445 break;
1446 default:
1447 max_missing_disks = 0;
1448 }
1449 return missing_disks <= max_missing_disks;
1450 }
1451
1452 /* try to determine how much space is reserved for metadata from
1453 * the last get_extents() entry on the smallest active disk,
1454 * otherwise fallback to the default
1455 */
1456 static __u32 imsm_min_reserved_sectors(struct intel_super *super)
1457 {
1458 struct extent *e;
1459 int i;
1460 unsigned long long min_active;
1461 __u32 remainder;
1462 __u32 rv = MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS;
1463 struct dl *dl, *dl_min = NULL;
1464
1465 if (!super)
1466 return rv;
1467
1468 min_active = 0;
1469 for (dl = super->disks; dl; dl = dl->next) {
1470 if (dl->index < 0)
1471 continue;
1472 unsigned long long blocks = total_blocks(&dl->disk);
1473 if (blocks < min_active || min_active == 0) {
1474 dl_min = dl;
1475 min_active = blocks;
1476 }
1477 }
1478 if (!dl_min)
1479 return rv;
1480
1481 /* find last lba used by subarrays on the smallest active disk */
1482 e = get_extents(super, dl_min, 0);
1483 if (!e)
1484 return rv;
1485 for (i = 0; e[i].size; i++)
1486 continue;
1487
1488 remainder = min_active - e[i].start;
1489 free(e);
1490
1491 /* to give priority to recovery we should not require full
1492 IMSM_RESERVED_SECTORS from the spare */
1493 rv = MPB_SECTOR_CNT + NUM_BLOCKS_DIRTY_STRIPE_REGION;
1494
1495 /* if real reservation is smaller use that value */
1496 return (remainder < rv) ? remainder : rv;
1497 }
1498
1499 /*
1500 * Return minimum size of a spare and sector size
1501 * that can be used in this array
1502 */
1503 int get_spare_criteria_imsm(struct supertype *st, struct spare_criteria *c)
1504 {
1505 struct intel_super *super = st->sb;
1506 struct dl *dl;
1507 struct extent *e;
1508 int i;
1509 unsigned long long size = 0;
1510
1511 c->min_size = 0;
1512 c->sector_size = 0;
1513
1514 if (!super)
1515 return -EINVAL;
1516 /* find first active disk in array */
1517 dl = super->disks;
1518 while (dl && (is_failed(&dl->disk) || dl->index == -1))
1519 dl = dl->next;
1520 if (!dl)
1521 return -EINVAL;
1522 /* find last lba used by subarrays */
1523 e = get_extents(super, dl, 0);
1524 if (!e)
1525 return -EINVAL;
1526 for (i = 0; e[i].size; i++)
1527 continue;
1528 if (i > 0)
1529 size = e[i-1].start + e[i-1].size;
1530 free(e);
1531
1532 /* add the amount of space needed for metadata */
1533 size += imsm_min_reserved_sectors(super);
1534
1535 c->min_size = size * 512;
1536 c->sector_size = super->sector_size;
1537
1538 return 0;
1539 }
1540
1541 static int is_gen_migration(struct imsm_dev *dev);
1542
1543 #define IMSM_4K_DIV 8
1544
1545 static __u64 blocks_per_migr_unit(struct intel_super *super,
1546 struct imsm_dev *dev);
1547
1548 static void print_imsm_dev(struct intel_super *super,
1549 struct imsm_dev *dev,
1550 char *uuid,
1551 int disk_idx)
1552 {
1553 __u64 sz;
1554 int slot, i;
1555 struct imsm_map *map = get_imsm_map(dev, MAP_0);
1556 struct imsm_map *map2 = get_imsm_map(dev, MAP_1);
1557 __u32 ord;
1558
1559 printf("\n");
1560 printf("[%.16s]:\n", dev->volume);
1561 printf(" UUID : %s\n", uuid);
1562 printf(" RAID Level : %d", get_imsm_raid_level(map));
1563 if (map2)
1564 printf(" <-- %d", get_imsm_raid_level(map2));
1565 printf("\n");
1566 printf(" Members : %d", map->num_members);
1567 if (map2)
1568 printf(" <-- %d", map2->num_members);
1569 printf("\n");
1570 printf(" Slots : [");
1571 for (i = 0; i < map->num_members; i++) {
1572 ord = get_imsm_ord_tbl_ent(dev, i, MAP_0);
1573 printf("%s", ord & IMSM_ORD_REBUILD ? "_" : "U");
1574 }
1575 printf("]");
1576 if (map2) {
1577 printf(" <-- [");
1578 for (i = 0; i < map2->num_members; i++) {
1579 ord = get_imsm_ord_tbl_ent(dev, i, MAP_1);
1580 printf("%s", ord & IMSM_ORD_REBUILD ? "_" : "U");
1581 }
1582 printf("]");
1583 }
1584 printf("\n");
1585 printf(" Failed disk : ");
1586 if (map->failed_disk_num == 0xff)
1587 printf("none");
1588 else
1589 printf("%i", map->failed_disk_num);
1590 printf("\n");
1591 slot = get_imsm_disk_slot(map, disk_idx);
1592 if (slot >= 0) {
1593 ord = get_imsm_ord_tbl_ent(dev, slot, MAP_X);
1594 printf(" This Slot : %d%s\n", slot,
1595 ord & IMSM_ORD_REBUILD ? " (out-of-sync)" : "");
1596 } else
1597 printf(" This Slot : ?\n");
1598 printf(" Sector Size : %u\n", super->sector_size);
1599 sz = imsm_dev_size(dev);
1600 printf(" Array Size : %llu%s\n",
1601 (unsigned long long)sz * 512 / super->sector_size,
1602 human_size(sz * 512));
1603 sz = blocks_per_member(map);
1604 printf(" Per Dev Size : %llu%s\n",
1605 (unsigned long long)sz * 512 / super->sector_size,
1606 human_size(sz * 512));
1607 printf(" Sector Offset : %llu\n",
1608 pba_of_lba0(map));
1609 printf(" Num Stripes : %llu\n",
1610 num_data_stripes(map));
1611 printf(" Chunk Size : %u KiB",
1612 __le16_to_cpu(map->blocks_per_strip) / 2);
1613 if (map2)
1614 printf(" <-- %u KiB",
1615 __le16_to_cpu(map2->blocks_per_strip) / 2);
1616 printf("\n");
1617 printf(" Reserved : %d\n", __le32_to_cpu(dev->reserved_blocks));
1618 printf(" Migrate State : ");
1619 if (dev->vol.migr_state) {
1620 if (migr_type(dev) == MIGR_INIT)
1621 printf("initialize\n");
1622 else if (migr_type(dev) == MIGR_REBUILD)
1623 printf("rebuild\n");
1624 else if (migr_type(dev) == MIGR_VERIFY)
1625 printf("check\n");
1626 else if (migr_type(dev) == MIGR_GEN_MIGR)
1627 printf("general migration\n");
1628 else if (migr_type(dev) == MIGR_STATE_CHANGE)
1629 printf("state change\n");
1630 else if (migr_type(dev) == MIGR_REPAIR)
1631 printf("repair\n");
1632 else
1633 printf("<unknown:%d>\n", migr_type(dev));
1634 } else
1635 printf("idle\n");
1636 printf(" Map State : %s", map_state_str[map->map_state]);
1637 if (dev->vol.migr_state) {
1638 struct imsm_map *map = get_imsm_map(dev, MAP_1);
1639
1640 printf(" <-- %s", map_state_str[map->map_state]);
1641 printf("\n Checkpoint : %u ",
1642 __le32_to_cpu(dev->vol.curr_migr_unit));
1643 if (is_gen_migration(dev) && (slot > 1 || slot < 0))
1644 printf("(N/A)");
1645 else
1646 printf("(%llu)", (unsigned long long)
1647 blocks_per_migr_unit(super, dev));
1648 }
1649 printf("\n");
1650 printf(" Dirty State : %s\n", (dev->vol.dirty & RAIDVOL_DIRTY) ?
1651 "dirty" : "clean");
1652 printf(" RWH Policy : ");
1653 if (dev->rwh_policy == RWH_OFF || dev->rwh_policy == RWH_MULTIPLE_OFF)
1654 printf("off\n");
1655 else if (dev->rwh_policy == RWH_DISTRIBUTED)
1656 printf("PPL distributed\n");
1657 else if (dev->rwh_policy == RWH_JOURNALING_DRIVE)
1658 printf("PPL journaling drive\n");
1659 else if (dev->rwh_policy == RWH_MULTIPLE_DISTRIBUTED)
1660 printf("Multiple distributed PPLs\n");
1661 else if (dev->rwh_policy == RWH_MULTIPLE_PPLS_JOURNALING_DRIVE)
1662 printf("Multiple PPLs on journaling drive\n");
1663 else
1664 printf("<unknown:%d>\n", dev->rwh_policy);
1665 }
1666
1667 static void print_imsm_disk(struct imsm_disk *disk,
1668 int index,
1669 __u32 reserved,
1670 unsigned int sector_size) {
1671 char str[MAX_RAID_SERIAL_LEN + 1];
1672 __u64 sz;
1673
1674 if (index < -1 || !disk)
1675 return;
1676
1677 printf("\n");
1678 snprintf(str, MAX_RAID_SERIAL_LEN + 1, "%s", disk->serial);
1679 if (index >= 0)
1680 printf(" Disk%02d Serial : %s\n", index, str);
1681 else
1682 printf(" Disk Serial : %s\n", str);
1683 printf(" State :%s%s%s%s\n", is_spare(disk) ? " spare" : "",
1684 is_configured(disk) ? " active" : "",
1685 is_failed(disk) ? " failed" : "",
1686 is_journal(disk) ? " journal" : "");
1687 printf(" Id : %08x\n", __le32_to_cpu(disk->scsi_id));
1688 sz = total_blocks(disk) - reserved;
1689 printf(" Usable Size : %llu%s\n",
1690 (unsigned long long)sz * 512 / sector_size,
1691 human_size(sz * 512));
1692 }
1693
1694 void convert_to_4k_imsm_migr_rec(struct intel_super *super)
1695 {
1696 struct migr_record *migr_rec = super->migr_rec;
1697
1698 migr_rec->blocks_per_unit /= IMSM_4K_DIV;
1699 migr_rec->dest_depth_per_unit /= IMSM_4K_DIV;
1700 split_ull((join_u32(migr_rec->post_migr_vol_cap,
1701 migr_rec->post_migr_vol_cap_hi) / IMSM_4K_DIV),
1702 &migr_rec->post_migr_vol_cap, &migr_rec->post_migr_vol_cap_hi);
1703 set_migr_chkp_area_pba(migr_rec,
1704 migr_chkp_area_pba(migr_rec) / IMSM_4K_DIV);
1705 set_migr_dest_1st_member_lba(migr_rec,
1706 migr_dest_1st_member_lba(migr_rec) / IMSM_4K_DIV);
1707 }
1708
1709 void convert_to_4k_imsm_disk(struct imsm_disk *disk)
1710 {
1711 set_total_blocks(disk, (total_blocks(disk)/IMSM_4K_DIV));
1712 }
1713
1714 void convert_to_4k(struct intel_super *super)
1715 {
1716 struct imsm_super *mpb = super->anchor;
1717 struct imsm_disk *disk;
1718 int i;
1719 __u32 bbm_log_size = __le32_to_cpu(mpb->bbm_log_size);
1720
1721 for (i = 0; i < mpb->num_disks ; i++) {
1722 disk = __get_imsm_disk(mpb, i);
1723 /* disk */
1724 convert_to_4k_imsm_disk(disk);
1725 }
1726 for (i = 0; i < mpb->num_raid_devs; i++) {
1727 struct imsm_dev *dev = __get_imsm_dev(mpb, i);
1728 struct imsm_map *map = get_imsm_map(dev, MAP_0);
1729 /* dev */
1730 set_imsm_dev_size(dev, imsm_dev_size(dev)/IMSM_4K_DIV);
1731 dev->vol.curr_migr_unit /= IMSM_4K_DIV;
1732
1733 /* map0 */
1734 set_blocks_per_member(map, blocks_per_member(map)/IMSM_4K_DIV);
1735 map->blocks_per_strip /= IMSM_4K_DIV;
1736 set_pba_of_lba0(map, pba_of_lba0(map)/IMSM_4K_DIV);
1737
1738 if (dev->vol.migr_state) {
1739 /* map1 */
1740 map = get_imsm_map(dev, MAP_1);
1741 set_blocks_per_member(map,
1742 blocks_per_member(map)/IMSM_4K_DIV);
1743 map->blocks_per_strip /= IMSM_4K_DIV;
1744 set_pba_of_lba0(map, pba_of_lba0(map)/IMSM_4K_DIV);
1745 }
1746 }
1747 if (bbm_log_size) {
1748 struct bbm_log *log = (void *)mpb +
1749 __le32_to_cpu(mpb->mpb_size) - bbm_log_size;
1750 __u32 i;
1751
1752 for (i = 0; i < log->entry_count; i++) {
1753 struct bbm_log_entry *entry =
1754 &log->marked_block_entries[i];
1755
1756 __u8 count = entry->marked_count + 1;
1757 unsigned long long sector =
1758 __le48_to_cpu(&entry->defective_block_start);
1759
1760 entry->defective_block_start =
1761 __cpu_to_le48(sector/IMSM_4K_DIV);
1762 entry->marked_count = max(count/IMSM_4K_DIV, 1) - 1;
1763 }
1764 }
1765
1766 mpb->check_sum = __gen_imsm_checksum(mpb);
1767 }
1768
1769 void examine_migr_rec_imsm(struct intel_super *super)
1770 {
1771 struct migr_record *migr_rec = super->migr_rec;
1772 struct imsm_super *mpb = super->anchor;
1773 int i;
1774
1775 for (i = 0; i < mpb->num_raid_devs; i++) {
1776 struct imsm_dev *dev = __get_imsm_dev(mpb, i);
1777 struct imsm_map *map;
1778 int slot = -1;
1779
1780 if (is_gen_migration(dev) == 0)
1781 continue;
1782
1783 printf("\nMigration Record Information:");
1784
1785 /* first map under migration */
1786 map = get_imsm_map(dev, MAP_0);
1787 if (map)
1788 slot = get_imsm_disk_slot(map, super->disks->index);
1789 if (map == NULL || slot > 1 || slot < 0) {
1790 printf(" Empty\n ");
1791 printf("Examine one of first two disks in array\n");
1792 break;
1793 }
1794 printf("\n Status : ");
1795 if (__le32_to_cpu(migr_rec->rec_status) == UNIT_SRC_NORMAL)
1796 printf("Normal\n");
1797 else
1798 printf("Contains Data\n");
1799 printf(" Current Unit : %llu\n",
1800 current_migr_unit(migr_rec));
1801 printf(" Family : %u\n",
1802 __le32_to_cpu(migr_rec->family_num));
1803 printf(" Ascending : %u\n",
1804 __le32_to_cpu(migr_rec->ascending_migr));
1805 printf(" Blocks Per Unit : %u\n",
1806 __le32_to_cpu(migr_rec->blocks_per_unit));
1807 printf(" Dest. Depth Per Unit : %u\n",
1808 __le32_to_cpu(migr_rec->dest_depth_per_unit));
1809 printf(" Checkpoint Area pba : %llu\n",
1810 migr_chkp_area_pba(migr_rec));
1811 printf(" First member lba : %llu\n",
1812 migr_dest_1st_member_lba(migr_rec));
1813 printf(" Total Number of Units : %llu\n",
1814 get_num_migr_units(migr_rec));
1815 printf(" Size of volume : %llu\n",
1816 join_u32(migr_rec->post_migr_vol_cap,
1817 migr_rec->post_migr_vol_cap_hi));
1818 printf(" Record was read from : %u\n",
1819 __le32_to_cpu(migr_rec->ckpt_read_disk_num));
1820
1821 break;
1822 }
1823 }
1824
1825 void convert_from_4k_imsm_migr_rec(struct intel_super *super)
1826 {
1827 struct migr_record *migr_rec = super->migr_rec;
1828
1829 migr_rec->blocks_per_unit *= IMSM_4K_DIV;
1830 migr_rec->dest_depth_per_unit *= IMSM_4K_DIV;
1831 split_ull((join_u32(migr_rec->post_migr_vol_cap,
1832 migr_rec->post_migr_vol_cap_hi) * IMSM_4K_DIV),
1833 &migr_rec->post_migr_vol_cap,
1834 &migr_rec->post_migr_vol_cap_hi);
1835 set_migr_chkp_area_pba(migr_rec,
1836 migr_chkp_area_pba(migr_rec) * IMSM_4K_DIV);
1837 set_migr_dest_1st_member_lba(migr_rec,
1838 migr_dest_1st_member_lba(migr_rec) * IMSM_4K_DIV);
1839 }
1840
1841 void convert_from_4k(struct intel_super *super)
1842 {
1843 struct imsm_super *mpb = super->anchor;
1844 struct imsm_disk *disk;
1845 int i;
1846 __u32 bbm_log_size = __le32_to_cpu(mpb->bbm_log_size);
1847
1848 for (i = 0; i < mpb->num_disks ; i++) {
1849 disk = __get_imsm_disk(mpb, i);
1850 /* disk */
1851 set_total_blocks(disk, (total_blocks(disk)*IMSM_4K_DIV));
1852 }
1853
1854 for (i = 0; i < mpb->num_raid_devs; i++) {
1855 struct imsm_dev *dev = __get_imsm_dev(mpb, i);
1856 struct imsm_map *map = get_imsm_map(dev, MAP_0);
1857 /* dev */
1858 set_imsm_dev_size(dev, imsm_dev_size(dev)*IMSM_4K_DIV);
1859 dev->vol.curr_migr_unit *= IMSM_4K_DIV;
1860
1861 /* map0 */
1862 set_blocks_per_member(map, blocks_per_member(map)*IMSM_4K_DIV);
1863 map->blocks_per_strip *= IMSM_4K_DIV;
1864 set_pba_of_lba0(map, pba_of_lba0(map)*IMSM_4K_DIV);
1865
1866 if (dev->vol.migr_state) {
1867 /* map1 */
1868 map = get_imsm_map(dev, MAP_1);
1869 set_blocks_per_member(map,
1870 blocks_per_member(map)*IMSM_4K_DIV);
1871 map->blocks_per_strip *= IMSM_4K_DIV;
1872 set_pba_of_lba0(map, pba_of_lba0(map)*IMSM_4K_DIV);
1873 }
1874 }
1875 if (bbm_log_size) {
1876 struct bbm_log *log = (void *)mpb +
1877 __le32_to_cpu(mpb->mpb_size) - bbm_log_size;
1878 __u32 i;
1879
1880 for (i = 0; i < log->entry_count; i++) {
1881 struct bbm_log_entry *entry =
1882 &log->marked_block_entries[i];
1883
1884 __u8 count = entry->marked_count + 1;
1885 unsigned long long sector =
1886 __le48_to_cpu(&entry->defective_block_start);
1887
1888 entry->defective_block_start =
1889 __cpu_to_le48(sector*IMSM_4K_DIV);
1890 entry->marked_count = count*IMSM_4K_DIV - 1;
1891 }
1892 }
1893
1894 mpb->check_sum = __gen_imsm_checksum(mpb);
1895 }
1896
1897 /*******************************************************************************
1898 * function: imsm_check_attributes
1899 * Description: Function checks if features represented by attributes flags
1900 * are supported by mdadm.
1901 * Parameters:
1902 * attributes - Attributes read from metadata
1903 * Returns:
1904 * 0 - passed attributes contains unsupported features flags
1905 * 1 - all features are supported
1906 ******************************************************************************/
1907 static int imsm_check_attributes(__u32 attributes)
1908 {
1909 int ret_val = 1;
1910 __u32 not_supported = MPB_ATTRIB_SUPPORTED^0xffffffff;
1911
1912 not_supported &= ~MPB_ATTRIB_IGNORED;
1913
1914 not_supported &= attributes;
1915 if (not_supported) {
1916 pr_err("(IMSM): Unsupported attributes : %x\n",
1917 (unsigned)__le32_to_cpu(not_supported));
1918 if (not_supported & MPB_ATTRIB_CHECKSUM_VERIFY) {
1919 dprintf("\t\tMPB_ATTRIB_CHECKSUM_VERIFY \n");
1920 not_supported ^= MPB_ATTRIB_CHECKSUM_VERIFY;
1921 }
1922 if (not_supported & MPB_ATTRIB_2TB) {
1923 dprintf("\t\tMPB_ATTRIB_2TB\n");
1924 not_supported ^= MPB_ATTRIB_2TB;
1925 }
1926 if (not_supported & MPB_ATTRIB_RAID0) {
1927 dprintf("\t\tMPB_ATTRIB_RAID0\n");
1928 not_supported ^= MPB_ATTRIB_RAID0;
1929 }
1930 if (not_supported & MPB_ATTRIB_RAID1) {
1931 dprintf("\t\tMPB_ATTRIB_RAID1\n");
1932 not_supported ^= MPB_ATTRIB_RAID1;
1933 }
1934 if (not_supported & MPB_ATTRIB_RAID10) {
1935 dprintf("\t\tMPB_ATTRIB_RAID10\n");
1936 not_supported ^= MPB_ATTRIB_RAID10;
1937 }
1938 if (not_supported & MPB_ATTRIB_RAID1E) {
1939 dprintf("\t\tMPB_ATTRIB_RAID1E\n");
1940 not_supported ^= MPB_ATTRIB_RAID1E;
1941 }
1942 if (not_supported & MPB_ATTRIB_RAID5) {
1943 dprintf("\t\tMPB_ATTRIB_RAID5\n");
1944 not_supported ^= MPB_ATTRIB_RAID5;
1945 }
1946 if (not_supported & MPB_ATTRIB_RAIDCNG) {
1947 dprintf("\t\tMPB_ATTRIB_RAIDCNG\n");
1948 not_supported ^= MPB_ATTRIB_RAIDCNG;
1949 }
1950 if (not_supported & MPB_ATTRIB_BBM) {
1951 dprintf("\t\tMPB_ATTRIB_BBM\n");
1952 not_supported ^= MPB_ATTRIB_BBM;
1953 }
1954 if (not_supported & MPB_ATTRIB_CHECKSUM_VERIFY) {
1955 dprintf("\t\tMPB_ATTRIB_CHECKSUM_VERIFY (== MPB_ATTRIB_LEGACY)\n");
1956 not_supported ^= MPB_ATTRIB_CHECKSUM_VERIFY;
1957 }
1958 if (not_supported & MPB_ATTRIB_EXP_STRIPE_SIZE) {
1959 dprintf("\t\tMPB_ATTRIB_EXP_STRIP_SIZE\n");
1960 not_supported ^= MPB_ATTRIB_EXP_STRIPE_SIZE;
1961 }
1962 if (not_supported & MPB_ATTRIB_2TB_DISK) {
1963 dprintf("\t\tMPB_ATTRIB_2TB_DISK\n");
1964 not_supported ^= MPB_ATTRIB_2TB_DISK;
1965 }
1966 if (not_supported & MPB_ATTRIB_NEVER_USE2) {
1967 dprintf("\t\tMPB_ATTRIB_NEVER_USE2\n");
1968 not_supported ^= MPB_ATTRIB_NEVER_USE2;
1969 }
1970 if (not_supported & MPB_ATTRIB_NEVER_USE) {
1971 dprintf("\t\tMPB_ATTRIB_NEVER_USE\n");
1972 not_supported ^= MPB_ATTRIB_NEVER_USE;
1973 }
1974
1975 if (not_supported)
1976 dprintf("(IMSM): Unknown attributes : %x\n", not_supported);
1977
1978 ret_val = 0;
1979 }
1980
1981 return ret_val;
1982 }
1983
1984 static void getinfo_super_imsm(struct supertype *st, struct mdinfo *info, char *map);
1985
1986 static void examine_super_imsm(struct supertype *st, char *homehost)
1987 {
1988 struct intel_super *super = st->sb;
1989 struct imsm_super *mpb = super->anchor;
1990 char str[MAX_SIGNATURE_LENGTH];
1991 int i;
1992 struct mdinfo info;
1993 char nbuf[64];
1994 __u32 sum;
1995 __u32 reserved = imsm_reserved_sectors(super, super->disks);
1996 struct dl *dl;
1997
1998 strncpy(str, (char *)mpb->sig, MPB_SIG_LEN);
1999 str[MPB_SIG_LEN-1] = '\0';
2000 printf(" Magic : %s\n", str);
2001 printf(" Version : %s\n", get_imsm_version(mpb));
2002 printf(" Orig Family : %08x\n", __le32_to_cpu(mpb->orig_family_num));
2003 printf(" Family : %08x\n", __le32_to_cpu(mpb->family_num));
2004 printf(" Generation : %08x\n", __le32_to_cpu(mpb->generation_num));
2005 printf(" Attributes : ");
2006 if (imsm_check_attributes(mpb->attributes))
2007 printf("All supported\n");
2008 else
2009 printf("not supported\n");
2010 getinfo_super_imsm(st, &info, NULL);
2011 fname_from_uuid(st, &info, nbuf, ':');
2012 printf(" UUID : %s\n", nbuf + 5);
2013 sum = __le32_to_cpu(mpb->check_sum);
2014 printf(" Checksum : %08x %s\n", sum,
2015 __gen_imsm_checksum(mpb) == sum ? "correct" : "incorrect");
2016 printf(" MPB Sectors : %d\n", mpb_sectors(mpb, super->sector_size));
2017 printf(" Disks : %d\n", mpb->num_disks);
2018 printf(" RAID Devices : %d\n", mpb->num_raid_devs);
2019 print_imsm_disk(__get_imsm_disk(mpb, super->disks->index),
2020 super->disks->index, reserved, super->sector_size);
2021 if (get_imsm_bbm_log_size(super->bbm_log)) {
2022 struct bbm_log *log = super->bbm_log;
2023
2024 printf("\n");
2025 printf("Bad Block Management Log:\n");
2026 printf(" Log Size : %d\n", __le32_to_cpu(mpb->bbm_log_size));
2027 printf(" Signature : %x\n", __le32_to_cpu(log->signature));
2028 printf(" Entry Count : %d\n", __le32_to_cpu(log->entry_count));
2029 }
2030 for (i = 0; i < mpb->num_raid_devs; i++) {
2031 struct mdinfo info;
2032 struct imsm_dev *dev = __get_imsm_dev(mpb, i);
2033
2034 super->current_vol = i;
2035 getinfo_super_imsm(st, &info, NULL);
2036 fname_from_uuid(st, &info, nbuf, ':');
2037 print_imsm_dev(super, dev, nbuf + 5, super->disks->index);
2038 }
2039 for (i = 0; i < mpb->num_disks; i++) {
2040 if (i == super->disks->index)
2041 continue;
2042 print_imsm_disk(__get_imsm_disk(mpb, i), i, reserved,
2043 super->sector_size);
2044 }
2045
2046 for (dl = super->disks; dl; dl = dl->next)
2047 if (dl->index == -1)
2048 print_imsm_disk(&dl->disk, -1, reserved,
2049 super->sector_size);
2050
2051 examine_migr_rec_imsm(super);
2052 }
2053
2054 static void brief_examine_super_imsm(struct supertype *st, int verbose)
2055 {
2056 /* We just write a generic IMSM ARRAY entry */
2057 struct mdinfo info;
2058 char nbuf[64];
2059 struct intel_super *super = st->sb;
2060
2061 if (!super->anchor->num_raid_devs) {
2062 printf("ARRAY metadata=imsm\n");
2063 return;
2064 }
2065
2066 getinfo_super_imsm(st, &info, NULL);
2067 fname_from_uuid(st, &info, nbuf, ':');
2068 printf("ARRAY metadata=imsm UUID=%s\n", nbuf + 5);
2069 }
2070
2071 static void brief_examine_subarrays_imsm(struct supertype *st, int verbose)
2072 {
2073 /* We just write a generic IMSM ARRAY entry */
2074 struct mdinfo info;
2075 char nbuf[64];
2076 char nbuf1[64];
2077 struct intel_super *super = st->sb;
2078 int i;
2079
2080 if (!super->anchor->num_raid_devs)
2081 return;
2082
2083 getinfo_super_imsm(st, &info, NULL);
2084 fname_from_uuid(st, &info, nbuf, ':');
2085 for (i = 0; i < super->anchor->num_raid_devs; i++) {
2086 struct imsm_dev *dev = get_imsm_dev(super, i);
2087
2088 super->current_vol = i;
2089 getinfo_super_imsm(st, &info, NULL);
2090 fname_from_uuid(st, &info, nbuf1, ':');
2091 printf("ARRAY /dev/md/%.16s container=%s member=%d UUID=%s\n",
2092 dev->volume, nbuf + 5, i, nbuf1 + 5);
2093 }
2094 }
2095
2096 static void export_examine_super_imsm(struct supertype *st)
2097 {
2098 struct intel_super *super = st->sb;
2099 struct imsm_super *mpb = super->anchor;
2100 struct mdinfo info;
2101 char nbuf[64];
2102
2103 getinfo_super_imsm(st, &info, NULL);
2104 fname_from_uuid(st, &info, nbuf, ':');
2105 printf("MD_METADATA=imsm\n");
2106 printf("MD_LEVEL=container\n");
2107 printf("MD_UUID=%s\n", nbuf+5);
2108 printf("MD_DEVICES=%u\n", mpb->num_disks);
2109 }
2110
2111 static int copy_metadata_imsm(struct supertype *st, int from, int to)
2112 {
2113 /* The second last sector of the device contains
2114 * the "struct imsm_super" metadata.
2115 * This contains mpb_size which is the size in bytes of the
2116 * extended metadata. This is located immediately before
2117 * the imsm_super.
2118 * We want to read all that, plus the last sector which
2119 * may contain a migration record, and write it all
2120 * to the target.
2121 */
2122 void *buf;
2123 unsigned long long dsize, offset;
2124 int sectors;
2125 struct imsm_super *sb;
2126 struct intel_super *super = st->sb;
2127 unsigned int sector_size = super->sector_size;
2128 unsigned int written = 0;
2129
2130 if (posix_memalign(&buf, MAX_SECTOR_SIZE, MAX_SECTOR_SIZE) != 0)
2131 return 1;
2132
2133 if (!get_dev_size(from, NULL, &dsize))
2134 goto err;
2135
2136 if (lseek64(from, dsize-(2*sector_size), 0) < 0)
2137 goto err;
2138 if ((unsigned int)read(from, buf, sector_size) != sector_size)
2139 goto err;
2140 sb = buf;
2141 if (strncmp((char*)sb->sig, MPB_SIGNATURE, MPB_SIG_LEN) != 0)
2142 goto err;
2143
2144 sectors = mpb_sectors(sb, sector_size) + 2;
2145 offset = dsize - sectors * sector_size;
2146 if (lseek64(from, offset, 0) < 0 ||
2147 lseek64(to, offset, 0) < 0)
2148 goto err;
2149 while (written < sectors * sector_size) {
2150 int n = sectors*sector_size - written;
2151 if (n > 4096)
2152 n = 4096;
2153 if (read(from, buf, n) != n)
2154 goto err;
2155 if (write(to, buf, n) != n)
2156 goto err;
2157 written += n;
2158 }
2159 free(buf);
2160 return 0;
2161 err:
2162 free(buf);
2163 return 1;
2164 }
2165
2166 static void detail_super_imsm(struct supertype *st, char *homehost)
2167 {
2168 struct mdinfo info;
2169 char nbuf[64];
2170
2171 getinfo_super_imsm(st, &info, NULL);
2172 fname_from_uuid(st, &info, nbuf, ':');
2173 printf("\n UUID : %s\n", nbuf + 5);
2174 }
2175
2176 static void brief_detail_super_imsm(struct supertype *st)
2177 {
2178 struct mdinfo info;
2179 char nbuf[64];
2180 getinfo_super_imsm(st, &info, NULL);
2181 fname_from_uuid(st, &info, nbuf, ':');
2182 printf(" UUID=%s", nbuf + 5);
2183 }
2184
2185 static int imsm_read_serial(int fd, char *devname, __u8 *serial);
2186 static void fd2devname(int fd, char *name);
2187
2188 static int ahci_enumerate_ports(const char *hba_path, int port_count, int host_base, int verbose)
2189 {
2190 /* dump an unsorted list of devices attached to AHCI Intel storage
2191 * controller, as well as non-connected ports
2192 */
2193 int hba_len = strlen(hba_path) + 1;
2194 struct dirent *ent;
2195 DIR *dir;
2196 char *path = NULL;
2197 int err = 0;
2198 unsigned long port_mask = (1 << port_count) - 1;
2199
2200 if (port_count > (int)sizeof(port_mask) * 8) {
2201 if (verbose > 0)
2202 pr_err("port_count %d out of range\n", port_count);
2203 return 2;
2204 }
2205
2206 /* scroll through /sys/dev/block looking for devices attached to
2207 * this hba
2208 */
2209 dir = opendir("/sys/dev/block");
2210 if (!dir)
2211 return 1;
2212
2213 for (ent = readdir(dir); ent; ent = readdir(dir)) {
2214 int fd;
2215 char model[64];
2216 char vendor[64];
2217 char buf[1024];
2218 int major, minor;
2219 char *device;
2220 char *c;
2221 int port;
2222 int type;
2223
2224 if (sscanf(ent->d_name, "%d:%d", &major, &minor) != 2)
2225 continue;
2226 path = devt_to_devpath(makedev(major, minor));
2227 if (!path)
2228 continue;
2229 if (!path_attached_to_hba(path, hba_path)) {
2230 free(path);
2231 path = NULL;
2232 continue;
2233 }
2234
2235 /* retrieve the scsi device type */
2236 if (asprintf(&device, "/sys/dev/block/%d:%d/device/xxxxxxx", major, minor) < 0) {
2237 if (verbose > 0)
2238 pr_err("failed to allocate 'device'\n");
2239 err = 2;
2240 break;
2241 }
2242 sprintf(device, "/sys/dev/block/%d:%d/device/type", major, minor);
2243 if (load_sys(device, buf, sizeof(buf)) != 0) {
2244 if (verbose > 0)
2245 pr_err("failed to read device type for %s\n",
2246 path);
2247 err = 2;
2248 free(device);
2249 break;
2250 }
2251 type = strtoul(buf, NULL, 10);
2252
2253 /* if it's not a disk print the vendor and model */
2254 if (!(type == 0 || type == 7 || type == 14)) {
2255 vendor[0] = '\0';
2256 model[0] = '\0';
2257 sprintf(device, "/sys/dev/block/%d:%d/device/vendor", major, minor);
2258 if (load_sys(device, buf, sizeof(buf)) == 0) {
2259 strncpy(vendor, buf, sizeof(vendor));
2260 vendor[sizeof(vendor) - 1] = '\0';
2261 c = (char *) &vendor[sizeof(vendor) - 1];
2262 while (isspace(*c) || *c == '\0')
2263 *c-- = '\0';
2264
2265 }
2266 sprintf(device, "/sys/dev/block/%d:%d/device/model", major, minor);
2267 if (load_sys(device, buf, sizeof(buf)) == 0) {
2268 strncpy(model, buf, sizeof(model));
2269 model[sizeof(model) - 1] = '\0';
2270 c = (char *) &model[sizeof(model) - 1];
2271 while (isspace(*c) || *c == '\0')
2272 *c-- = '\0';
2273 }
2274
2275 if (vendor[0] && model[0])
2276 sprintf(buf, "%.64s %.64s", vendor, model);
2277 else
2278 switch (type) { /* numbers from hald/linux/device.c */
2279 case 1: sprintf(buf, "tape"); break;
2280 case 2: sprintf(buf, "printer"); break;
2281 case 3: sprintf(buf, "processor"); break;
2282 case 4:
2283 case 5: sprintf(buf, "cdrom"); break;
2284 case 6: sprintf(buf, "scanner"); break;
2285 case 8: sprintf(buf, "media_changer"); break;
2286 case 9: sprintf(buf, "comm"); break;
2287 case 12: sprintf(buf, "raid"); break;
2288 default: sprintf(buf, "unknown");
2289 }
2290 } else
2291 buf[0] = '\0';
2292 free(device);
2293
2294 /* chop device path to 'host%d' and calculate the port number */
2295 c = strchr(&path[hba_len], '/');
2296 if (!c) {
2297 if (verbose > 0)
2298 pr_err("%s - invalid path name\n", path + hba_len);
2299 err = 2;
2300 break;
2301 }
2302 *c = '\0';
2303 if ((sscanf(&path[hba_len], "ata%d", &port) == 1) ||
2304 ((sscanf(&path[hba_len], "host%d", &port) == 1)))
2305 port -= host_base;
2306 else {
2307 if (verbose > 0) {
2308 *c = '/'; /* repair the full string */
2309 pr_err("failed to determine port number for %s\n",
2310 path);
2311 }
2312 err = 2;
2313 break;
2314 }
2315
2316 /* mark this port as used */
2317 port_mask &= ~(1 << port);
2318
2319 /* print out the device information */
2320 if (buf[0]) {
2321 printf(" Port%d : - non-disk device (%s) -\n", port, buf);
2322 continue;
2323 }
2324
2325 fd = dev_open(ent->d_name, O_RDONLY);
2326 if (fd < 0)
2327 printf(" Port%d : - disk info unavailable -\n", port);
2328 else {
2329 fd2devname(fd, buf);
2330 printf(" Port%d : %s", port, buf);
2331 if (imsm_read_serial(fd, NULL, (__u8 *) buf) == 0)
2332 printf(" (%.*s)\n", MAX_RAID_SERIAL_LEN, buf);
2333 else
2334 printf(" ()\n");
2335 close(fd);
2336 }
2337 free(path);
2338 path = NULL;
2339 }
2340 if (path)
2341 free(path);
2342 if (dir)
2343 closedir(dir);
2344 if (err == 0) {
2345 int i;
2346
2347 for (i = 0; i < port_count; i++)
2348 if (port_mask & (1 << i))
2349 printf(" Port%d : - no device attached -\n", i);
2350 }
2351
2352 return err;
2353 }
2354
2355 static int print_vmd_attached_devs(struct sys_dev *hba)
2356 {
2357 struct dirent *ent;
2358 DIR *dir;
2359 char path[292];
2360 char link[256];
2361 char *c, *rp;
2362
2363 if (hba->type != SYS_DEV_VMD)
2364 return 1;
2365
2366 /* scroll through /sys/dev/block looking for devices attached to
2367 * this hba
2368 */
2369 dir = opendir("/sys/bus/pci/drivers/nvme");
2370 if (!dir)
2371 return 1;
2372
2373 for (ent = readdir(dir); ent; ent = readdir(dir)) {
2374 int n;
2375
2376 /* is 'ent' a device? check that the 'subsystem' link exists and
2377 * that its target matches 'bus'
2378 */
2379 sprintf(path, "/sys/bus/pci/drivers/nvme/%s/subsystem",
2380 ent->d_name);
2381 n = readlink(path, link, sizeof(link));
2382 if (n < 0 || n >= (int)sizeof(link))
2383 continue;
2384 link[n] = '\0';
2385 c = strrchr(link, '/');
2386 if (!c)
2387 continue;
2388 if (strncmp("pci", c+1, strlen("pci")) != 0)
2389 continue;
2390
2391 sprintf(path, "/sys/bus/pci/drivers/nvme/%s", ent->d_name);
2392
2393 rp = realpath(path, NULL);
2394 if (!rp)
2395 continue;
2396
2397 if (path_attached_to_hba(rp, hba->path)) {
2398 printf(" NVMe under VMD : %s\n", rp);
2399 }
2400 free(rp);
2401 }
2402
2403 closedir(dir);
2404 return 0;
2405 }
2406
2407 static void print_found_intel_controllers(struct sys_dev *elem)
2408 {
2409 for (; elem; elem = elem->next) {
2410 pr_err("found Intel(R) ");
2411 if (elem->type == SYS_DEV_SATA)
2412 fprintf(stderr, "SATA ");
2413 else if (elem->type == SYS_DEV_SAS)
2414 fprintf(stderr, "SAS ");
2415 else if (elem->type == SYS_DEV_NVME)
2416 fprintf(stderr, "NVMe ");
2417
2418 if (elem->type == SYS_DEV_VMD)
2419 fprintf(stderr, "VMD domain");
2420 else
2421 fprintf(stderr, "RAID controller");
2422
2423 if (elem->pci_id)
2424 fprintf(stderr, " at %s", elem->pci_id);
2425 fprintf(stderr, ".\n");
2426 }
2427 fflush(stderr);
2428 }
2429
2430 static int ahci_get_port_count(const char *hba_path, int *port_count)
2431 {
2432 struct dirent *ent;
2433 DIR *dir;
2434 int host_base = -1;
2435
2436 *port_count = 0;
2437 if ((dir = opendir(hba_path)) == NULL)
2438 return -1;
2439
2440 for (ent = readdir(dir); ent; ent = readdir(dir)) {
2441 int host;
2442
2443 if ((sscanf(ent->d_name, "ata%d", &host) != 1) &&
2444 ((sscanf(ent->d_name, "host%d", &host) != 1)))
2445 continue;
2446 if (*port_count == 0)
2447 host_base = host;
2448 else if (host < host_base)
2449 host_base = host;
2450
2451 if (host + 1 > *port_count + host_base)
2452 *port_count = host + 1 - host_base;
2453 }
2454 closedir(dir);
2455 return host_base;
2456 }
2457
2458 static void print_imsm_capability(const struct imsm_orom *orom)
2459 {
2460 printf(" Platform : Intel(R) ");
2461 if (orom->capabilities == 0 && orom->driver_features == 0)
2462 printf("Matrix Storage Manager\n");
2463 else if (imsm_orom_is_enterprise(orom) && orom->major_ver >= 6)
2464 printf("Virtual RAID on CPU\n");
2465 else
2466 printf("Rapid Storage Technology%s\n",
2467 imsm_orom_is_enterprise(orom) ? " enterprise" : "");
2468 if (orom->major_ver || orom->minor_ver || orom->hotfix_ver || orom->build)
2469 printf(" Version : %d.%d.%d.%d\n", orom->major_ver,
2470 orom->minor_ver, orom->hotfix_ver, orom->build);
2471 printf(" RAID Levels :%s%s%s%s%s\n",
2472 imsm_orom_has_raid0(orom) ? " raid0" : "",
2473 imsm_orom_has_raid1(orom) ? " raid1" : "",
2474 imsm_orom_has_raid1e(orom) ? " raid1e" : "",
2475 imsm_orom_has_raid10(orom) ? " raid10" : "",
2476 imsm_orom_has_raid5(orom) ? " raid5" : "");
2477 printf(" Chunk Sizes :%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
2478 imsm_orom_has_chunk(orom, 2) ? " 2k" : "",
2479 imsm_orom_has_chunk(orom, 4) ? " 4k" : "",
2480 imsm_orom_has_chunk(orom, 8) ? " 8k" : "",
2481 imsm_orom_has_chunk(orom, 16) ? " 16k" : "",
2482 imsm_orom_has_chunk(orom, 32) ? " 32k" : "",
2483 imsm_orom_has_chunk(orom, 64) ? " 64k" : "",
2484 imsm_orom_has_chunk(orom, 128) ? " 128k" : "",
2485 imsm_orom_has_chunk(orom, 256) ? " 256k" : "",
2486 imsm_orom_has_chunk(orom, 512) ? " 512k" : "",
2487 imsm_orom_has_chunk(orom, 1024*1) ? " 1M" : "",
2488 imsm_orom_has_chunk(orom, 1024*2) ? " 2M" : "",
2489 imsm_orom_has_chunk(orom, 1024*4) ? " 4M" : "",
2490 imsm_orom_has_chunk(orom, 1024*8) ? " 8M" : "",
2491 imsm_orom_has_chunk(orom, 1024*16) ? " 16M" : "",
2492 imsm_orom_has_chunk(orom, 1024*32) ? " 32M" : "",
2493 imsm_orom_has_chunk(orom, 1024*64) ? " 64M" : "");
2494 printf(" 2TB volumes :%s supported\n",
2495 (orom->attr & IMSM_OROM_ATTR_2TB)?"":" not");
2496 printf(" 2TB disks :%s supported\n",
2497 (orom->attr & IMSM_OROM_ATTR_2TB_DISK)?"":" not");
2498 printf(" Max Disks : %d\n", orom->tds);
2499 printf(" Max Volumes : %d per array, %d per %s\n",
2500 orom->vpa, orom->vphba,
2501 imsm_orom_is_nvme(orom) ? "platform" : "controller");
2502 return;
2503 }
2504
2505 static void print_imsm_capability_export(const struct imsm_orom *orom)
2506 {
2507 printf("MD_FIRMWARE_TYPE=imsm\n");
2508 if (orom->major_ver || orom->minor_ver || orom->hotfix_ver || orom->build)
2509 printf("IMSM_VERSION=%d.%d.%d.%d\n", orom->major_ver, orom->minor_ver,
2510 orom->hotfix_ver, orom->build);
2511 printf("IMSM_SUPPORTED_RAID_LEVELS=%s%s%s%s%s\n",
2512 imsm_orom_has_raid0(orom) ? "raid0 " : "",
2513 imsm_orom_has_raid1(orom) ? "raid1 " : "",
2514 imsm_orom_has_raid1e(orom) ? "raid1e " : "",
2515 imsm_orom_has_raid5(orom) ? "raid10 " : "",
2516 imsm_orom_has_raid10(orom) ? "raid5 " : "");
2517 printf("IMSM_SUPPORTED_CHUNK_SIZES=%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
2518 imsm_orom_has_chunk(orom, 2) ? "2k " : "",
2519 imsm_orom_has_chunk(orom, 4) ? "4k " : "",
2520 imsm_orom_has_chunk(orom, 8) ? "8k " : "",
2521 imsm_orom_has_chunk(orom, 16) ? "16k " : "",
2522 imsm_orom_has_chunk(orom, 32) ? "32k " : "",
2523 imsm_orom_has_chunk(orom, 64) ? "64k " : "",
2524 imsm_orom_has_chunk(orom, 128) ? "128k " : "",
2525 imsm_orom_has_chunk(orom, 256) ? "256k " : "",
2526 imsm_orom_has_chunk(orom, 512) ? "512k " : "",
2527 imsm_orom_has_chunk(orom, 1024*1) ? "1M " : "",
2528 imsm_orom_has_chunk(orom, 1024*2) ? "2M " : "",
2529 imsm_orom_has_chunk(orom, 1024*4) ? "4M " : "",
2530 imsm_orom_has_chunk(orom, 1024*8) ? "8M " : "",
2531 imsm_orom_has_chunk(orom, 1024*16) ? "16M " : "",
2532 imsm_orom_has_chunk(orom, 1024*32) ? "32M " : "",
2533 imsm_orom_has_chunk(orom, 1024*64) ? "64M " : "");
2534 printf("IMSM_2TB_VOLUMES=%s\n",(orom->attr & IMSM_OROM_ATTR_2TB) ? "yes" : "no");
2535 printf("IMSM_2TB_DISKS=%s\n",(orom->attr & IMSM_OROM_ATTR_2TB_DISK) ? "yes" : "no");
2536 printf("IMSM_MAX_DISKS=%d\n",orom->tds);
2537 printf("IMSM_MAX_VOLUMES_PER_ARRAY=%d\n",orom->vpa);
2538 printf("IMSM_MAX_VOLUMES_PER_CONTROLLER=%d\n",orom->vphba);
2539 }
2540
2541 static int detail_platform_imsm(int verbose, int enumerate_only, char *controller_path)
2542 {
2543 /* There are two components to imsm platform support, the ahci SATA
2544 * controller and the option-rom. To find the SATA controller we
2545 * simply look in /sys/bus/pci/drivers/ahci to see if an ahci
2546 * controller with the Intel vendor id is present. This approach
2547 * allows mdadm to leverage the kernel's ahci detection logic, with the
2548 * caveat that if ahci.ko is not loaded mdadm will not be able to
2549 * detect platform raid capabilities. The option-rom resides in a
2550 * platform "Adapter ROM". We scan for its signature to retrieve the
2551 * platform capabilities. If raid support is disabled in the BIOS the
2552 * option-rom capability structure will not be available.
2553 */
2554 struct sys_dev *list, *hba;
2555 int host_base = 0;
2556 int port_count = 0;
2557 int result=1;
2558
2559 if (enumerate_only) {
2560 if (check_env("IMSM_NO_PLATFORM"))
2561 return 0;
2562 list = find_intel_devices();
2563 if (!list)
2564 return 2;
2565 for (hba = list; hba; hba = hba->next) {
2566 if (find_imsm_capability(hba)) {
2567 result = 0;
2568 break;
2569 }
2570 else
2571 result = 2;
2572 }
2573 return result;
2574 }
2575
2576 list = find_intel_devices();
2577 if (!list) {
2578 if (verbose > 0)
2579 pr_err("no active Intel(R) RAID controller found.\n");
2580 return 2;
2581 } else if (verbose > 0)
2582 print_found_intel_controllers(list);
2583
2584 for (hba = list; hba; hba = hba->next) {
2585 if (controller_path && (compare_paths(hba->path, controller_path) != 0))
2586 continue;
2587 if (!find_imsm_capability(hba)) {
2588 char buf[PATH_MAX];
2589 pr_err("imsm capabilities not found for controller: %s (type %s)\n",
2590 hba->type == SYS_DEV_VMD ? vmd_domain_to_controller(hba, buf) : hba->path,
2591 get_sys_dev_type(hba->type));
2592 continue;
2593 }
2594 result = 0;
2595 }
2596
2597 if (controller_path && result == 1) {
2598 pr_err("no active Intel(R) RAID controller found under %s\n",
2599 controller_path);
2600 return result;
2601 }
2602
2603 const struct orom_entry *entry;
2604
2605 for (entry = orom_entries; entry; entry = entry->next) {
2606 if (entry->type == SYS_DEV_VMD) {
2607 print_imsm_capability(&entry->orom);
2608 printf(" 3rd party NVMe :%s supported\n",
2609 imsm_orom_has_tpv_support(&entry->orom)?"":" not");
2610 for (hba = list; hba; hba = hba->next) {
2611 if (hba->type == SYS_DEV_VMD) {
2612 char buf[PATH_MAX];
2613 printf(" I/O Controller : %s (%s)\n",
2614 vmd_domain_to_controller(hba, buf), get_sys_dev_type(hba->type));
2615 if (print_vmd_attached_devs(hba)) {
2616 if (verbose > 0)
2617 pr_err("failed to get devices attached to VMD domain.\n");
2618 result |= 2;
2619 }
2620 }
2621 }
2622 printf("\n");
2623 continue;
2624 }
2625
2626 print_imsm_capability(&entry->orom);
2627 if (entry->type == SYS_DEV_NVME) {
2628 for (hba = list; hba; hba = hba->next) {
2629 if (hba->type == SYS_DEV_NVME)
2630 printf(" NVMe Device : %s\n", hba->path);
2631 }
2632 printf("\n");
2633 continue;
2634 }
2635
2636 struct devid_list *devid;
2637 for (devid = entry->devid_list; devid; devid = devid->next) {
2638 hba = device_by_id(devid->devid);
2639 if (!hba)
2640 continue;
2641
2642 printf(" I/O Controller : %s (%s)\n",
2643 hba->path, get_sys_dev_type(hba->type));
2644 if (hba->type == SYS_DEV_SATA) {
2645 host_base = ahci_get_port_count(hba->path, &port_count);
2646 if (ahci_enumerate_ports(hba->path, port_count, host_base, verbose)) {
2647 if (verbose > 0)
2648 pr_err("failed to enumerate ports on SATA controller at %s.\n", hba->pci_id);
2649 result |= 2;
2650 }
2651 }
2652 }
2653 printf("\n");
2654 }
2655
2656 return result;
2657 }
2658
2659 static int export_detail_platform_imsm(int verbose, char *controller_path)
2660 {
2661 struct sys_dev *list, *hba;
2662 int result=1;
2663
2664 list = find_intel_devices();
2665 if (!list) {
2666 if (verbose > 0)
2667 pr_err("IMSM_DETAIL_PLATFORM_ERROR=NO_INTEL_DEVICES\n");
2668 result = 2;
2669 return result;
2670 }
2671
2672 for (hba = list; hba; hba = hba->next) {
2673 if (controller_path && (compare_paths(hba->path,controller_path) != 0))
2674 continue;
2675 if (!find_imsm_capability(hba) && verbose > 0) {
2676 char buf[PATH_MAX];
2677 pr_err("IMSM_DETAIL_PLATFORM_ERROR=NO_IMSM_CAPABLE_DEVICE_UNDER_%s\n",
2678 hba->type == SYS_DEV_VMD ? vmd_domain_to_controller(hba, buf) : hba->path);
2679 }
2680 else
2681 result = 0;
2682 }
2683
2684 const struct orom_entry *entry;
2685
2686 for (entry = orom_entries; entry; entry = entry->next) {
2687 if (entry->type == SYS_DEV_VMD) {
2688 for (hba = list; hba; hba = hba->next)
2689 print_imsm_capability_export(&entry->orom);
2690 continue;
2691 }
2692 print_imsm_capability_export(&entry->orom);
2693 }
2694
2695 return result;
2696 }
2697
2698 static int match_home_imsm(struct supertype *st, char *homehost)
2699 {
2700 /* the imsm metadata format does not specify any host
2701 * identification information. We return -1 since we can never
2702 * confirm nor deny whether a given array is "meant" for this
2703 * host. We rely on compare_super and the 'family_num' fields to
2704 * exclude member disks that do not belong, and we rely on
2705 * mdadm.conf to specify the arrays that should be assembled.
2706 * Auto-assembly may still pick up "foreign" arrays.
2707 */
2708
2709 return -1;
2710 }
2711
2712 static void uuid_from_super_imsm(struct supertype *st, int uuid[4])
2713 {
2714 /* The uuid returned here is used for:
2715 * uuid to put into bitmap file (Create, Grow)
2716 * uuid for backup header when saving critical section (Grow)
2717 * comparing uuids when re-adding a device into an array
2718 * In these cases the uuid required is that of the data-array,
2719 * not the device-set.
2720 * uuid to recognise same set when adding a missing device back
2721 * to an array. This is a uuid for the device-set.
2722 *
2723 * For each of these we can make do with a truncated
2724 * or hashed uuid rather than the original, as long as
2725 * everyone agrees.
2726 * In each case the uuid required is that of the data-array,
2727 * not the device-set.
2728 */
2729 /* imsm does not track uuid's so we synthesis one using sha1 on
2730 * - The signature (Which is constant for all imsm array, but no matter)
2731 * - the orig_family_num of the container
2732 * - the index number of the volume
2733 * - the 'serial' number of the volume.
2734 * Hopefully these are all constant.
2735 */
2736 struct intel_super *super = st->sb;
2737
2738 char buf[20];
2739 struct sha1_ctx ctx;
2740 struct imsm_dev *dev = NULL;
2741 __u32 family_num;
2742
2743 /* some mdadm versions failed to set ->orig_family_num, in which
2744 * case fall back to ->family_num. orig_family_num will be
2745 * fixed up with the first metadata update.
2746 */
2747 family_num = super->anchor->orig_family_num;
2748 if (family_num == 0)
2749 family_num = super->anchor->family_num;
2750 sha1_init_ctx(&ctx);
2751 sha1_process_bytes(super->anchor->sig, MPB_SIG_LEN, &ctx);
2752 sha1_process_bytes(&family_num, sizeof(__u32), &ctx);
2753 if (super->current_vol >= 0)
2754 dev = get_imsm_dev(super, super->current_vol);
2755 if (dev) {
2756 __u32 vol = super->current_vol;
2757 sha1_process_bytes(&vol, sizeof(vol), &ctx);
2758 sha1_process_bytes(dev->volume, MAX_RAID_SERIAL_LEN, &ctx);
2759 }
2760 sha1_finish_ctx(&ctx, buf);
2761 memcpy(uuid, buf, 4*4);
2762 }
2763
2764 #if 0
2765 static void
2766 get_imsm_numerical_version(struct imsm_super *mpb, int *m, int *p)
2767 {
2768 __u8 *v = get_imsm_version(mpb);
2769 __u8 *end = mpb->sig + MAX_SIGNATURE_LENGTH;
2770 char major[] = { 0, 0, 0 };
2771 char minor[] = { 0 ,0, 0 };
2772 char patch[] = { 0, 0, 0 };
2773 char *ver_parse[] = { major, minor, patch };
2774 int i, j;
2775
2776 i = j = 0;
2777 while (*v != '\0' && v < end) {
2778 if (*v != '.' && j < 2)
2779 ver_parse[i][j++] = *v;
2780 else {
2781 i++;
2782 j = 0;
2783 }
2784 v++;
2785 }
2786
2787 *m = strtol(minor, NULL, 0);
2788 *p = strtol(patch, NULL, 0);
2789 }
2790 #endif
2791
2792 static __u32 migr_strip_blocks_resync(struct imsm_dev *dev)
2793 {
2794 /* migr_strip_size when repairing or initializing parity */
2795 struct imsm_map *map = get_imsm_map(dev, MAP_0);
2796 __u32 chunk = __le32_to_cpu(map->blocks_per_strip);
2797
2798 switch (get_imsm_raid_level(map)) {
2799 case 5:
2800 case 10:
2801 return chunk;
2802 default:
2803 return 128*1024 >> 9;
2804 }
2805 }
2806
2807 static __u32 migr_strip_blocks_rebuild(struct imsm_dev *dev)
2808 {
2809 /* migr_strip_size when rebuilding a degraded disk, no idea why
2810 * this is different than migr_strip_size_resync(), but it's good
2811 * to be compatible
2812 */
2813 struct imsm_map *map = get_imsm_map(dev, MAP_1);
2814 __u32 chunk = __le32_to_cpu(map->blocks_per_strip);
2815
2816 switch (get_imsm_raid_level(map)) {
2817 case 1:
2818 case 10:
2819 if (map->num_members % map->num_domains == 0)
2820 return 128*1024 >> 9;
2821 else
2822 return chunk;
2823 case 5:
2824 return max((__u32) 64*1024 >> 9, chunk);
2825 default:
2826 return 128*1024 >> 9;
2827 }
2828 }
2829
2830 static __u32 num_stripes_per_unit_resync(struct imsm_dev *dev)
2831 {
2832 struct imsm_map *lo = get_imsm_map(dev, MAP_0);
2833 struct imsm_map *hi = get_imsm_map(dev, MAP_1);
2834 __u32 lo_chunk = __le32_to_cpu(lo->blocks_per_strip);
2835 __u32 hi_chunk = __le32_to_cpu(hi->blocks_per_strip);
2836
2837 return max((__u32) 1, hi_chunk / lo_chunk);
2838 }
2839
2840 static __u32 num_stripes_per_unit_rebuild(struct imsm_dev *dev)
2841 {
2842 struct imsm_map *lo = get_imsm_map(dev, MAP_0);
2843 int level = get_imsm_raid_level(lo);
2844
2845 if (level == 1 || level == 10) {
2846 struct imsm_map *hi = get_imsm_map(dev, MAP_1);
2847
2848 return hi->num_domains;
2849 } else
2850 return num_stripes_per_unit_resync(dev);
2851 }
2852
2853 static __u8 imsm_num_data_members(struct imsm_map *map)
2854 {
2855 /* named 'imsm_' because raid0, raid1 and raid10
2856 * counter-intuitively have the same number of data disks
2857 */
2858 switch (get_imsm_raid_level(map)) {
2859 case 0:
2860 return map->num_members;
2861 break;
2862 case 1:
2863 case 10:
2864 return map->num_members/2;
2865 case 5:
2866 return map->num_members - 1;
2867 default:
2868 dprintf("unsupported raid level\n");
2869 return 0;
2870 }
2871 }
2872
2873 static unsigned long long calc_component_size(struct imsm_map *map,
2874 struct imsm_dev *dev)
2875 {
2876 unsigned long long component_size;
2877 unsigned long long dev_size = imsm_dev_size(dev);
2878 unsigned long long calc_dev_size = 0;
2879 unsigned int member_disks = imsm_num_data_members(map);
2880
2881 if (member_disks == 0)
2882 return 0;
2883
2884 component_size = per_dev_array_size(map);
2885 calc_dev_size = component_size * member_disks;
2886
2887 /* Component size is rounded to 1MB so difference between size from
2888 * metadata and size calculated from num_data_stripes equals up to
2889 * 2048 blocks per each device. If the difference is higher it means
2890 * that array size was expanded and num_data_stripes was not updated.
2891 */
2892 if ((unsigned int)abs(calc_dev_size - dev_size) >
2893 (1 << SECT_PER_MB_SHIFT) * member_disks) {
2894 component_size = dev_size / member_disks;
2895 dprintf("Invalid num_data_stripes in metadata; expected=%llu, found=%llu\n",
2896 component_size / map->blocks_per_strip,
2897 num_data_stripes(map));
2898 }
2899
2900 return component_size;
2901 }
2902
2903 static __u32 parity_segment_depth(struct imsm_dev *dev)
2904 {
2905 struct imsm_map *map = get_imsm_map(dev, MAP_0);
2906 __u32 chunk = __le32_to_cpu(map->blocks_per_strip);
2907
2908 switch(get_imsm_raid_level(map)) {
2909 case 1:
2910 case 10:
2911 return chunk * map->num_domains;
2912 case 5:
2913 return chunk * map->num_members;
2914 default:
2915 return chunk;
2916 }
2917 }
2918
2919 static __u32 map_migr_block(struct imsm_dev *dev, __u32 block)
2920 {
2921 struct imsm_map *map = get_imsm_map(dev, MAP_1);
2922 __u32 chunk = __le32_to_cpu(map->blocks_per_strip);
2923 __u32 strip = block / chunk;
2924
2925 switch (get_imsm_raid_level(map)) {
2926 case 1:
2927 case 10: {
2928 __u32 vol_strip = (strip * map->num_domains) + 1;
2929 __u32 vol_stripe = vol_strip / map->num_members;
2930
2931 return vol_stripe * chunk + block % chunk;
2932 } case 5: {
2933 __u32 stripe = strip / (map->num_members - 1);
2934
2935 return stripe * chunk + block % chunk;
2936 }
2937 default:
2938 return 0;
2939 }
2940 }
2941
2942 static __u64 blocks_per_migr_unit(struct intel_super *super,
2943 struct imsm_dev *dev)
2944 {
2945 /* calculate the conversion factor between per member 'blocks'
2946 * (md/{resync,rebuild}_start) and imsm migration units, return
2947 * 0 for the 'not migrating' and 'unsupported migration' cases
2948 */
2949 if (!dev->vol.migr_state)
2950 return 0;
2951
2952 switch (migr_type(dev)) {
2953 case MIGR_GEN_MIGR: {
2954 struct migr_record *migr_rec = super->migr_rec;
2955 return __le32_to_cpu(migr_rec->blocks_per_unit);
2956 }
2957 case MIGR_VERIFY:
2958 case MIGR_REPAIR:
2959 case MIGR_INIT: {
2960 struct imsm_map *map = get_imsm_map(dev, MAP_0);
2961 __u32 stripes_per_unit;
2962 __u32 blocks_per_unit;
2963 __u32 parity_depth;
2964 __u32 migr_chunk;
2965 __u32 block_map;
2966 __u32 block_rel;
2967 __u32 segment;
2968 __u32 stripe;
2969 __u8 disks;
2970
2971 /* yes, this is really the translation of migr_units to
2972 * per-member blocks in the 'resync' case
2973 */
2974 stripes_per_unit = num_stripes_per_unit_resync(dev);
2975 migr_chunk = migr_strip_blocks_resync(dev);
2976 disks = imsm_num_data_members(map);
2977 blocks_per_unit = stripes_per_unit * migr_chunk * disks;
2978 stripe = __le16_to_cpu(map->blocks_per_strip) * disks;
2979 segment = blocks_per_unit / stripe;
2980 block_rel = blocks_per_unit - segment * stripe;
2981 parity_depth = parity_segment_depth(dev);
2982 block_map = map_migr_block(dev, block_rel);
2983 return block_map + parity_depth * segment;
2984 }
2985 case MIGR_REBUILD: {
2986 __u32 stripes_per_unit;
2987 __u32 migr_chunk;
2988
2989 stripes_per_unit = num_stripes_per_unit_rebuild(dev);
2990 migr_chunk = migr_strip_blocks_rebuild(dev);
2991 return migr_chunk * stripes_per_unit;
2992 }
2993 case MIGR_STATE_CHANGE:
2994 default:
2995 return 0;
2996 }
2997 }
2998
2999 static int imsm_level_to_layout(int level)
3000 {
3001 switch (level) {
3002 case 0:
3003 case 1:
3004 return 0;
3005 case 5:
3006 case 6:
3007 return ALGORITHM_LEFT_ASYMMETRIC;
3008 case 10:
3009 return 0x102;
3010 }
3011 return UnSet;
3012 }
3013
3014 /*******************************************************************************
3015 * Function: read_imsm_migr_rec
3016 * Description: Function reads imsm migration record from last sector of disk
3017 * Parameters:
3018 * fd : disk descriptor
3019 * super : metadata info
3020 * Returns:
3021 * 0 : success,
3022 * -1 : fail
3023 ******************************************************************************/
3024 static int read_imsm_migr_rec(int fd, struct intel_super *super)
3025 {
3026 int ret_val = -1;
3027 unsigned int sector_size = super->sector_size;
3028 unsigned long long dsize;
3029
3030 get_dev_size(fd, NULL, &dsize);
3031 if (lseek64(fd, dsize - (sector_size*MIGR_REC_SECTOR_POSITION),
3032 SEEK_SET) < 0) {
3033 pr_err("Cannot seek to anchor block: %s\n",
3034 strerror(errno));
3035 goto out;
3036 }
3037 if ((unsigned int)read(fd, super->migr_rec_buf,
3038 MIGR_REC_BUF_SECTORS*sector_size) !=
3039 MIGR_REC_BUF_SECTORS*sector_size) {
3040 pr_err("Cannot read migr record block: %s\n",
3041 strerror(errno));
3042 goto out;
3043 }
3044 ret_val = 0;
3045 if (sector_size == 4096)
3046 convert_from_4k_imsm_migr_rec(super);
3047
3048 out:
3049 return ret_val;
3050 }
3051
3052 static struct imsm_dev *imsm_get_device_during_migration(
3053 struct intel_super *super)
3054 {
3055
3056 struct intel_dev *dv;
3057
3058 for (dv = super->devlist; dv; dv = dv->next) {
3059 if (is_gen_migration(dv->dev))
3060 return dv->dev;
3061 }
3062 return NULL;
3063 }
3064
3065 /*******************************************************************************
3066 * Function: load_imsm_migr_rec
3067 * Description: Function reads imsm migration record (it is stored at the last
3068 * sector of disk)
3069 * Parameters:
3070 * super : imsm internal array info
3071 * info : general array info
3072 * Returns:
3073 * 0 : success
3074 * -1 : fail
3075 * -2 : no migration in progress
3076 ******************************************************************************/
3077 static int load_imsm_migr_rec(struct intel_super *super, struct mdinfo *info)
3078 {
3079 struct mdinfo *sd;
3080 struct dl *dl;
3081 char nm[30];
3082 int retval = -1;
3083 int fd = -1;
3084 struct imsm_dev *dev;
3085 struct imsm_map *map;
3086 int slot = -1;
3087
3088 /* find map under migration */
3089 dev = imsm_get_device_during_migration(super);
3090 /* nothing to load,no migration in progress?
3091 */
3092 if (dev == NULL)
3093 return -2;
3094
3095 if (info) {
3096 for (sd = info->devs ; sd ; sd = sd->next) {
3097 /* read only from one of the first two slots */
3098 if ((sd->disk.raid_disk < 0) ||
3099 (sd->disk.raid_disk > 1))
3100 continue;
3101
3102 sprintf(nm, "%d:%d", sd->disk.major, sd->disk.minor);
3103 fd = dev_open(nm, O_RDONLY);
3104 if (fd >= 0)
3105 break;
3106 }
3107 }
3108 if (fd < 0) {
3109 map = get_imsm_map(dev, MAP_0);
3110 for (dl = super->disks; dl; dl = dl->next) {
3111 /* skip spare and failed disks
3112 */
3113 if (dl->index < 0)
3114 continue;
3115 /* read only from one of the first two slots */
3116 if (map)
3117 slot = get_imsm_disk_slot(map, dl->index);
3118 if (map == NULL || slot > 1 || slot < 0)
3119 continue;
3120 sprintf(nm, "%d:%d", dl->major, dl->minor);
3121 fd = dev_open(nm, O_RDONLY);
3122 if