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