]> git.ipfire.org Git - thirdparty/mdadm.git/blob - super-intel.c
2bfcad15cc5d0993cbc79c8080e488518a3c7c8d
[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 * Internal Write-intent bitmap is stored in the same area where PPL.
101 * Both features are mutually exclusive, so it is not an issue.
102 * The first 8KiB of the area are reserved and shall not be used.
103 */
104 #define IMSM_BITMAP_AREA_RESERVED_SIZE 8192
105
106 #define IMSM_BITMAP_HEADER_OFFSET (IMSM_BITMAP_AREA_RESERVED_SIZE)
107 #define IMSM_BITMAP_HEADER_SIZE MAX_SECTOR_SIZE
108
109 #define IMSM_BITMAP_START_OFFSET (IMSM_BITMAP_HEADER_OFFSET + IMSM_BITMAP_HEADER_SIZE)
110 #define IMSM_BITMAP_AREA_SIZE (MULTIPLE_PPL_AREA_SIZE_IMSM - IMSM_BITMAP_START_OFFSET)
111 #define IMSM_BITMAP_AND_HEADER_SIZE (IMSM_BITMAP_AREA_SIZE + IMSM_BITMAP_HEADER_SIZE)
112
113 #define IMSM_DEFAULT_BITMAP_CHUNKSIZE (64 * 1024 * 1024)
114 #define IMSM_DEFAULT_BITMAP_DAEMON_SLEEP 5
115
116 /*
117 * This macro let's us ensure that no-one accidentally
118 * changes the size of a struct
119 */
120 #define ASSERT_SIZE(_struct, size) \
121 static inline void __assert_size_##_struct(void) \
122 { \
123 switch (0) { \
124 case 0: break; \
125 case (sizeof(struct _struct) == size): break; \
126 } \
127 }
128
129 /* Disk configuration info. */
130 #define IMSM_MAX_DEVICES 255
131 struct imsm_disk {
132 __u8 serial[MAX_RAID_SERIAL_LEN];/* 0xD8 - 0xE7 ascii serial number */
133 __u32 total_blocks_lo; /* 0xE8 - 0xEB total blocks lo */
134 __u32 scsi_id; /* 0xEC - 0xEF scsi ID */
135 #define SPARE_DISK __cpu_to_le32(0x01) /* Spare */
136 #define CONFIGURED_DISK __cpu_to_le32(0x02) /* Member of some RaidDev */
137 #define FAILED_DISK __cpu_to_le32(0x04) /* Permanent failure */
138 #define JOURNAL_DISK __cpu_to_le32(0x2000000) /* Device marked as Journaling Drive */
139 __u32 status; /* 0xF0 - 0xF3 */
140 __u32 owner_cfg_num; /* which config 0,1,2... owns this disk */
141 __u32 total_blocks_hi; /* 0xF4 - 0xF5 total blocks hi */
142 #define IMSM_DISK_FILLERS 3
143 __u32 filler[IMSM_DISK_FILLERS]; /* 0xF5 - 0x107 MPB_DISK_FILLERS for future expansion */
144 };
145 ASSERT_SIZE(imsm_disk, 48)
146
147 /* map selector for map managment
148 */
149 #define MAP_0 0
150 #define MAP_1 1
151 #define MAP_X -1
152
153 /* RAID map configuration infos. */
154 struct imsm_map {
155 __u32 pba_of_lba0_lo; /* start address of partition */
156 __u32 blocks_per_member_lo;/* blocks per member */
157 __u32 num_data_stripes_lo; /* number of data stripes */
158 __u16 blocks_per_strip;
159 __u8 map_state; /* Normal, Uninitialized, Degraded, Failed */
160 #define IMSM_T_STATE_NORMAL 0
161 #define IMSM_T_STATE_UNINITIALIZED 1
162 #define IMSM_T_STATE_DEGRADED 2
163 #define IMSM_T_STATE_FAILED 3
164 __u8 raid_level;
165 #define IMSM_T_RAID0 0
166 #define IMSM_T_RAID1 1
167 #define IMSM_T_RAID5 5 /* since metadata version 1.2.02 ? */
168 __u8 num_members; /* number of member disks */
169 __u8 num_domains; /* number of parity domains */
170 __u8 failed_disk_num; /* valid only when state is degraded */
171 __u8 ddf;
172 __u32 pba_of_lba0_hi;
173 __u32 blocks_per_member_hi;
174 __u32 num_data_stripes_hi;
175 __u32 filler[4]; /* expansion area */
176 #define IMSM_ORD_REBUILD (1 << 24)
177 __u32 disk_ord_tbl[1]; /* disk_ord_tbl[num_members],
178 * top byte contains some flags
179 */
180 };
181 ASSERT_SIZE(imsm_map, 52)
182
183 struct imsm_vol {
184 __u32 curr_migr_unit_lo;
185 __u32 checkpoint_id; /* id to access curr_migr_unit */
186 __u8 migr_state; /* Normal or Migrating */
187 #define MIGR_INIT 0
188 #define MIGR_REBUILD 1
189 #define MIGR_VERIFY 2 /* analagous to echo check > sync_action */
190 #define MIGR_GEN_MIGR 3
191 #define MIGR_STATE_CHANGE 4
192 #define MIGR_REPAIR 5
193 __u8 migr_type; /* Initializing, Rebuilding, ... */
194 #define RAIDVOL_CLEAN 0
195 #define RAIDVOL_DIRTY 1
196 #define RAIDVOL_DSRECORD_VALID 2
197 __u8 dirty;
198 __u8 fs_state; /* fast-sync state for CnG (0xff == disabled) */
199 __u16 verify_errors; /* number of mismatches */
200 __u16 bad_blocks; /* number of bad blocks during verify */
201 __u32 curr_migr_unit_hi;
202 __u32 filler[3];
203 struct imsm_map map[1];
204 /* here comes another one if migr_state */
205 };
206 ASSERT_SIZE(imsm_vol, 84)
207
208 struct imsm_dev {
209 __u8 volume[MAX_RAID_SERIAL_LEN];
210 __u32 size_low;
211 __u32 size_high;
212 #define DEV_BOOTABLE __cpu_to_le32(0x01)
213 #define DEV_BOOT_DEVICE __cpu_to_le32(0x02)
214 #define DEV_READ_COALESCING __cpu_to_le32(0x04)
215 #define DEV_WRITE_COALESCING __cpu_to_le32(0x08)
216 #define DEV_LAST_SHUTDOWN_DIRTY __cpu_to_le32(0x10)
217 #define DEV_HIDDEN_AT_BOOT __cpu_to_le32(0x20)
218 #define DEV_CURRENTLY_HIDDEN __cpu_to_le32(0x40)
219 #define DEV_VERIFY_AND_FIX __cpu_to_le32(0x80)
220 #define DEV_MAP_STATE_UNINIT __cpu_to_le32(0x100)
221 #define DEV_NO_AUTO_RECOVERY __cpu_to_le32(0x200)
222 #define DEV_CLONE_N_GO __cpu_to_le32(0x400)
223 #define DEV_CLONE_MAN_SYNC __cpu_to_le32(0x800)
224 #define DEV_CNG_MASTER_DISK_NUM __cpu_to_le32(0x1000)
225 __u32 status; /* Persistent RaidDev status */
226 __u32 reserved_blocks; /* Reserved blocks at beginning of volume */
227 __u8 migr_priority;
228 __u8 num_sub_vols;
229 __u8 tid;
230 __u8 cng_master_disk;
231 __u16 cache_policy;
232 __u8 cng_state;
233 __u8 cng_sub_state;
234 __u16 my_vol_raid_dev_num; /* Used in Unique volume Id for this RaidDev */
235
236 /* NVM_EN */
237 __u8 nv_cache_mode;
238 __u8 nv_cache_flags;
239
240 /* Unique Volume Id of the NvCache Volume associated with this volume */
241 __u32 nvc_vol_orig_family_num;
242 __u16 nvc_vol_raid_dev_num;
243
244 #define RWH_OFF 0
245 #define RWH_DISTRIBUTED 1
246 #define RWH_JOURNALING_DRIVE 2
247 #define RWH_MULTIPLE_DISTRIBUTED 3
248 #define RWH_MULTIPLE_PPLS_JOURNALING_DRIVE 4
249 #define RWH_MULTIPLE_OFF 5
250 #define RWH_BITMAP 6
251 __u8 rwh_policy; /* Raid Write Hole Policy */
252 __u8 jd_serial[MAX_RAID_SERIAL_LEN]; /* Journal Drive serial number */
253 __u8 filler1;
254
255 #define IMSM_DEV_FILLERS 3
256 __u32 filler[IMSM_DEV_FILLERS];
257 struct imsm_vol vol;
258 };
259 ASSERT_SIZE(imsm_dev, 164)
260
261 struct imsm_super {
262 __u8 sig[MAX_SIGNATURE_LENGTH]; /* 0x00 - 0x1F */
263 __u32 check_sum; /* 0x20 - 0x23 MPB Checksum */
264 __u32 mpb_size; /* 0x24 - 0x27 Size of MPB */
265 __u32 family_num; /* 0x28 - 0x2B Checksum from first time this config was written */
266 __u32 generation_num; /* 0x2C - 0x2F Incremented each time this array's MPB is written */
267 __u32 error_log_size; /* 0x30 - 0x33 in bytes */
268 __u32 attributes; /* 0x34 - 0x37 */
269 __u8 num_disks; /* 0x38 Number of configured disks */
270 __u8 num_raid_devs; /* 0x39 Number of configured volumes */
271 __u8 error_log_pos; /* 0x3A */
272 __u8 fill[1]; /* 0x3B */
273 __u32 cache_size; /* 0x3c - 0x40 in mb */
274 __u32 orig_family_num; /* 0x40 - 0x43 original family num */
275 __u32 pwr_cycle_count; /* 0x44 - 0x47 simulated power cycle count for array */
276 __u32 bbm_log_size; /* 0x48 - 0x4B - size of bad Block Mgmt Log in bytes */
277 __u16 num_raid_devs_created; /* 0x4C - 0x4D Used for generating unique
278 * volume IDs for raid_dev created in this array
279 * (starts at 1)
280 */
281 __u16 filler1; /* 0x4E - 0x4F */
282 __u64 creation_time; /* 0x50 - 0x57 Array creation time */
283 #define IMSM_FILLERS 32
284 __u32 filler[IMSM_FILLERS]; /* 0x58 - 0xD7 RAID_MPB_FILLERS */
285 struct imsm_disk disk[1]; /* 0xD8 diskTbl[numDisks] */
286 /* here comes imsm_dev[num_raid_devs] */
287 /* here comes BBM logs */
288 };
289 ASSERT_SIZE(imsm_super, 264)
290
291 #define BBM_LOG_MAX_ENTRIES 254
292 #define BBM_LOG_MAX_LBA_ENTRY_VAL 256 /* Represents 256 LBAs */
293 #define BBM_LOG_SIGNATURE 0xabadb10c
294
295 struct bbm_log_block_addr {
296 __u16 w1;
297 __u32 dw1;
298 } __attribute__ ((__packed__));
299
300 struct bbm_log_entry {
301 __u8 marked_count; /* Number of blocks marked - 1 */
302 __u8 disk_ordinal; /* Disk entry within the imsm_super */
303 struct bbm_log_block_addr defective_block_start;
304 } __attribute__ ((__packed__));
305
306 struct bbm_log {
307 __u32 signature; /* 0xABADB10C */
308 __u32 entry_count;
309 struct bbm_log_entry marked_block_entries[BBM_LOG_MAX_ENTRIES];
310 };
311 ASSERT_SIZE(bbm_log, 2040)
312
313 static char *map_state_str[] = { "normal", "uninitialized", "degraded", "failed" };
314
315 #define BLOCKS_PER_KB (1024/512)
316
317 #define RAID_DISK_RESERVED_BLOCKS_IMSM_HI 2209
318
319 #define GEN_MIGR_AREA_SIZE 2048 /* General Migration Copy Area size in blocks */
320
321 #define MIGR_REC_BUF_SECTORS 1 /* size of migr_record i/o buffer in sectors */
322 #define MIGR_REC_SECTOR_POSITION 1 /* migr_record position offset on disk,
323 * MIGR_REC_BUF_SECTORS <= MIGR_REC_SECTOR_POS
324 */
325
326 #define UNIT_SRC_NORMAL 0 /* Source data for curr_migr_unit must
327 * be recovered using srcMap */
328 #define UNIT_SRC_IN_CP_AREA 1 /* Source data for curr_migr_unit has
329 * already been migrated and must
330 * be recovered from checkpoint area */
331
332 #define PPL_ENTRY_SPACE (128 * 1024) /* Size of single PPL, without the header */
333
334 struct migr_record {
335 __u32 rec_status; /* Status used to determine how to restart
336 * migration in case it aborts
337 * in some fashion */
338 __u32 curr_migr_unit_lo; /* 0..numMigrUnits-1 */
339 __u32 family_num; /* Family number of MPB
340 * containing the RaidDev
341 * that is migrating */
342 __u32 ascending_migr; /* True if migrating in increasing
343 * order of lbas */
344 __u32 blocks_per_unit; /* Num disk blocks per unit of operation */
345 __u32 dest_depth_per_unit; /* Num member blocks each destMap
346 * member disk
347 * advances per unit-of-operation */
348 __u32 ckpt_area_pba_lo; /* Pba of first block of ckpt copy area */
349 __u32 dest_1st_member_lba_lo; /* First member lba on first
350 * stripe of destination */
351 __u32 num_migr_units_lo; /* Total num migration units-of-op */
352 __u32 post_migr_vol_cap; /* Size of volume after
353 * migration completes */
354 __u32 post_migr_vol_cap_hi; /* Expansion space for LBA64 */
355 __u32 ckpt_read_disk_num; /* Which member disk in destSubMap[0] the
356 * migration ckpt record was read from
357 * (for recovered migrations) */
358 __u32 curr_migr_unit_hi; /* 0..numMigrUnits-1 high order 32 bits */
359 __u32 ckpt_area_pba_hi; /* Pba of first block of ckpt copy area
360 * high order 32 bits */
361 __u32 dest_1st_member_lba_hi; /* First member lba on first stripe of
362 * destination - high order 32 bits */
363 __u32 num_migr_units_hi; /* Total num migration units-of-op
364 * high order 32 bits */
365 __u32 filler[16];
366 };
367 ASSERT_SIZE(migr_record, 128)
368
369 struct md_list {
370 /* usage marker:
371 * 1: load metadata
372 * 2: metadata does not match
373 * 4: already checked
374 */
375 int used;
376 char *devname;
377 int found;
378 int container;
379 dev_t st_rdev;
380 struct md_list *next;
381 };
382
383 #define pr_vrb(fmt, arg...) (void) (verbose && pr_err(fmt, ##arg))
384
385 static __u8 migr_type(struct imsm_dev *dev)
386 {
387 if (dev->vol.migr_type == MIGR_VERIFY &&
388 dev->status & DEV_VERIFY_AND_FIX)
389 return MIGR_REPAIR;
390 else
391 return dev->vol.migr_type;
392 }
393
394 static void set_migr_type(struct imsm_dev *dev, __u8 migr_type)
395 {
396 /* for compatibility with older oroms convert MIGR_REPAIR, into
397 * MIGR_VERIFY w/ DEV_VERIFY_AND_FIX status
398 */
399 if (migr_type == MIGR_REPAIR) {
400 dev->vol.migr_type = MIGR_VERIFY;
401 dev->status |= DEV_VERIFY_AND_FIX;
402 } else {
403 dev->vol.migr_type = migr_type;
404 dev->status &= ~DEV_VERIFY_AND_FIX;
405 }
406 }
407
408 static unsigned int sector_count(__u32 bytes, unsigned int sector_size)
409 {
410 return ROUND_UP(bytes, sector_size) / sector_size;
411 }
412
413 static unsigned int mpb_sectors(struct imsm_super *mpb,
414 unsigned int sector_size)
415 {
416 return sector_count(__le32_to_cpu(mpb->mpb_size), sector_size);
417 }
418
419 struct intel_dev {
420 struct imsm_dev *dev;
421 struct intel_dev *next;
422 unsigned index;
423 };
424
425 struct intel_hba {
426 enum sys_dev_type type;
427 char *path;
428 char *pci_id;
429 struct intel_hba *next;
430 };
431
432 enum action {
433 DISK_REMOVE = 1,
434 DISK_ADD
435 };
436 /* internal representation of IMSM metadata */
437 struct intel_super {
438 union {
439 void *buf; /* O_DIRECT buffer for reading/writing metadata */
440 struct imsm_super *anchor; /* immovable parameters */
441 };
442 union {
443 void *migr_rec_buf; /* buffer for I/O operations */
444 struct migr_record *migr_rec; /* migration record */
445 };
446 int clean_migration_record_by_mdmon; /* when reshape is switched to next
447 array, it indicates that mdmon is allowed to clean migration
448 record */
449 size_t len; /* size of the 'buf' allocation */
450 size_t extra_space; /* extra space in 'buf' that is not used yet */
451 void *next_buf; /* for realloc'ing buf from the manager */
452 size_t next_len;
453 int updates_pending; /* count of pending updates for mdmon */
454 int current_vol; /* index of raid device undergoing creation */
455 unsigned long long create_offset; /* common start for 'current_vol' */
456 __u32 random; /* random data for seeding new family numbers */
457 struct intel_dev *devlist;
458 unsigned int sector_size; /* sector size of used member drives */
459 struct dl {
460 struct dl *next;
461 int index;
462 __u8 serial[MAX_RAID_SERIAL_LEN];
463 int major, minor;
464 char *devname;
465 struct imsm_disk disk;
466 int fd;
467 int extent_cnt;
468 struct extent *e; /* for determining freespace @ create */
469 int raiddisk; /* slot to fill in autolayout */
470 enum action action;
471 } *disks, *current_disk;
472 struct dl *disk_mgmt_list; /* list of disks to add/remove while mdmon
473 active */
474 struct dl *missing; /* disks removed while we weren't looking */
475 struct bbm_log *bbm_log;
476 struct intel_hba *hba; /* device path of the raid controller for this metadata */
477 const struct imsm_orom *orom; /* platform firmware support */
478 struct intel_super *next; /* (temp) list for disambiguating family_num */
479 struct md_bb bb; /* memory for get_bad_blocks call */
480 };
481
482 struct intel_disk {
483 struct imsm_disk disk;
484 #define IMSM_UNKNOWN_OWNER (-1)
485 int owner;
486 struct intel_disk *next;
487 };
488
489 struct extent {
490 unsigned long long start, size;
491 };
492
493 /* definitions of reshape process types */
494 enum imsm_reshape_type {
495 CH_TAKEOVER,
496 CH_MIGRATION,
497 CH_ARRAY_SIZE,
498 };
499
500 /* definition of messages passed to imsm_process_update */
501 enum imsm_update_type {
502 update_activate_spare,
503 update_create_array,
504 update_kill_array,
505 update_rename_array,
506 update_add_remove_disk,
507 update_reshape_container_disks,
508 update_reshape_migration,
509 update_takeover,
510 update_general_migration_checkpoint,
511 update_size_change,
512 update_prealloc_badblocks_mem,
513 update_rwh_policy,
514 };
515
516 struct imsm_update_activate_spare {
517 enum imsm_update_type type;
518 struct dl *dl;
519 int slot;
520 int array;
521 struct imsm_update_activate_spare *next;
522 };
523
524 struct geo_params {
525 char devnm[32];
526 char *dev_name;
527 unsigned long long size;
528 int level;
529 int layout;
530 int chunksize;
531 int raid_disks;
532 };
533
534 enum takeover_direction {
535 R10_TO_R0,
536 R0_TO_R10
537 };
538 struct imsm_update_takeover {
539 enum imsm_update_type type;
540 int subarray;
541 enum takeover_direction direction;
542 };
543
544 struct imsm_update_reshape {
545 enum imsm_update_type type;
546 int old_raid_disks;
547 int new_raid_disks;
548
549 int new_disks[1]; /* new_raid_disks - old_raid_disks makedev number */
550 };
551
552 struct imsm_update_reshape_migration {
553 enum imsm_update_type type;
554 int old_raid_disks;
555 int new_raid_disks;
556 /* fields for array migration changes
557 */
558 int subdev;
559 int new_level;
560 int new_layout;
561 int new_chunksize;
562
563 int new_disks[1]; /* new_raid_disks - old_raid_disks makedev number */
564 };
565
566 struct imsm_update_size_change {
567 enum imsm_update_type type;
568 int subdev;
569 long long new_size;
570 };
571
572 struct imsm_update_general_migration_checkpoint {
573 enum imsm_update_type type;
574 __u64 curr_migr_unit;
575 };
576
577 struct disk_info {
578 __u8 serial[MAX_RAID_SERIAL_LEN];
579 };
580
581 struct imsm_update_create_array {
582 enum imsm_update_type type;
583 int dev_idx;
584 struct imsm_dev dev;
585 };
586
587 struct imsm_update_kill_array {
588 enum imsm_update_type type;
589 int dev_idx;
590 };
591
592 struct imsm_update_rename_array {
593 enum imsm_update_type type;
594 __u8 name[MAX_RAID_SERIAL_LEN];
595 int dev_idx;
596 };
597
598 struct imsm_update_add_remove_disk {
599 enum imsm_update_type type;
600 };
601
602 struct imsm_update_prealloc_bb_mem {
603 enum imsm_update_type type;
604 };
605
606 struct imsm_update_rwh_policy {
607 enum imsm_update_type type;
608 int new_policy;
609 int dev_idx;
610 };
611
612 static const char *_sys_dev_type[] = {
613 [SYS_DEV_UNKNOWN] = "Unknown",
614 [SYS_DEV_SAS] = "SAS",
615 [SYS_DEV_SATA] = "SATA",
616 [SYS_DEV_NVME] = "NVMe",
617 [SYS_DEV_VMD] = "VMD"
618 };
619
620 const char *get_sys_dev_type(enum sys_dev_type type)
621 {
622 if (type >= SYS_DEV_MAX)
623 type = SYS_DEV_UNKNOWN;
624
625 return _sys_dev_type[type];
626 }
627
628 static struct intel_hba * alloc_intel_hba(struct sys_dev *device)
629 {
630 struct intel_hba *result = xmalloc(sizeof(*result));
631
632 result->type = device->type;
633 result->path = xstrdup(device->path);
634 result->next = NULL;
635 if (result->path && (result->pci_id = strrchr(result->path, '/')) != NULL)
636 result->pci_id++;
637
638 return result;
639 }
640
641 static struct intel_hba * find_intel_hba(struct intel_hba *hba, struct sys_dev *device)
642 {
643 struct intel_hba *result;
644
645 for (result = hba; result; result = result->next) {
646 if (result->type == device->type && strcmp(result->path, device->path) == 0)
647 break;
648 }
649 return result;
650 }
651
652 static int attach_hba_to_super(struct intel_super *super, struct sys_dev *device)
653 {
654 struct intel_hba *hba;
655
656 /* check if disk attached to Intel HBA */
657 hba = find_intel_hba(super->hba, device);
658 if (hba != NULL)
659 return 1;
660 /* Check if HBA is already attached to super */
661 if (super->hba == NULL) {
662 super->hba = alloc_intel_hba(device);
663 return 1;
664 }
665
666 hba = super->hba;
667 /* Intel metadata allows for all disks attached to the same type HBA.
668 * Do not support HBA types mixing
669 */
670 if (device->type != hba->type)
671 return 2;
672
673 /* Multiple same type HBAs can be used if they share the same OROM */
674 const struct imsm_orom *device_orom = get_orom_by_device_id(device->dev_id);
675
676 if (device_orom != super->orom)
677 return 2;
678
679 while (hba->next)
680 hba = hba->next;
681
682 hba->next = alloc_intel_hba(device);
683 return 1;
684 }
685
686 static struct sys_dev* find_disk_attached_hba(int fd, const char *devname)
687 {
688 struct sys_dev *list, *elem;
689 char *disk_path;
690
691 if ((list = find_intel_devices()) == NULL)
692 return 0;
693
694 if (fd < 0)
695 disk_path = (char *) devname;
696 else
697 disk_path = diskfd_to_devpath(fd);
698
699 if (!disk_path)
700 return 0;
701
702 for (elem = list; elem; elem = elem->next)
703 if (path_attached_to_hba(disk_path, elem->path))
704 return elem;
705
706 if (disk_path != devname)
707 free(disk_path);
708
709 return NULL;
710 }
711
712 static int find_intel_hba_capability(int fd, struct intel_super *super,
713 char *devname);
714
715 static struct supertype *match_metadata_desc_imsm(char *arg)
716 {
717 struct supertype *st;
718
719 if (strcmp(arg, "imsm") != 0 &&
720 strcmp(arg, "default") != 0
721 )
722 return NULL;
723
724 st = xcalloc(1, sizeof(*st));
725 st->ss = &super_imsm;
726 st->max_devs = IMSM_MAX_DEVICES;
727 st->minor_version = 0;
728 st->sb = NULL;
729 return st;
730 }
731
732 static __u8 *get_imsm_version(struct imsm_super *mpb)
733 {
734 return &mpb->sig[MPB_SIG_LEN];
735 }
736
737 /* retrieve a disk directly from the anchor when the anchor is known to be
738 * up-to-date, currently only at load time
739 */
740 static struct imsm_disk *__get_imsm_disk(struct imsm_super *mpb, __u8 index)
741 {
742 if (index >= mpb->num_disks)
743 return NULL;
744 return &mpb->disk[index];
745 }
746
747 /* retrieve the disk description based on a index of the disk
748 * in the sub-array
749 */
750 static struct dl *get_imsm_dl_disk(struct intel_super *super, __u8 index)
751 {
752 struct dl *d;
753
754 for (d = super->disks; d; d = d->next)
755 if (d->index == index)
756 return d;
757
758 return NULL;
759 }
760 /* retrieve a disk from the parsed metadata */
761 static struct imsm_disk *get_imsm_disk(struct intel_super *super, __u8 index)
762 {
763 struct dl *dl;
764
765 dl = get_imsm_dl_disk(super, index);
766 if (dl)
767 return &dl->disk;
768
769 return NULL;
770 }
771
772 /* generate a checksum directly from the anchor when the anchor is known to be
773 * up-to-date, currently only at load or write_super after coalescing
774 */
775 static __u32 __gen_imsm_checksum(struct imsm_super *mpb)
776 {
777 __u32 end = mpb->mpb_size / sizeof(end);
778 __u32 *p = (__u32 *) mpb;
779 __u32 sum = 0;
780
781 while (end--) {
782 sum += __le32_to_cpu(*p);
783 p++;
784 }
785
786 return sum - __le32_to_cpu(mpb->check_sum);
787 }
788
789 static size_t sizeof_imsm_map(struct imsm_map *map)
790 {
791 return sizeof(struct imsm_map) + sizeof(__u32) * (map->num_members - 1);
792 }
793
794 struct imsm_map *get_imsm_map(struct imsm_dev *dev, int second_map)
795 {
796 /* A device can have 2 maps if it is in the middle of a migration.
797 * If second_map is:
798 * MAP_0 - we return the first map
799 * MAP_1 - we return the second map if it exists, else NULL
800 * MAP_X - we return the second map if it exists, else the first
801 */
802 struct imsm_map *map = &dev->vol.map[0];
803 struct imsm_map *map2 = NULL;
804
805 if (dev->vol.migr_state)
806 map2 = (void *)map + sizeof_imsm_map(map);
807
808 switch (second_map) {
809 case MAP_0:
810 break;
811 case MAP_1:
812 map = map2;
813 break;
814 case MAP_X:
815 if (map2)
816 map = map2;
817 break;
818 default:
819 map = NULL;
820 }
821 return map;
822
823 }
824
825 /* return the size of the device.
826 * migr_state increases the returned size if map[0] were to be duplicated
827 */
828 static size_t sizeof_imsm_dev(struct imsm_dev *dev, int migr_state)
829 {
830 size_t size = sizeof(*dev) - sizeof(struct imsm_map) +
831 sizeof_imsm_map(get_imsm_map(dev, MAP_0));
832
833 /* migrating means an additional map */
834 if (dev->vol.migr_state)
835 size += sizeof_imsm_map(get_imsm_map(dev, MAP_1));
836 else if (migr_state)
837 size += sizeof_imsm_map(get_imsm_map(dev, MAP_0));
838
839 return size;
840 }
841
842 /* retrieve disk serial number list from a metadata update */
843 static struct disk_info *get_disk_info(struct imsm_update_create_array *update)
844 {
845 void *u = update;
846 struct disk_info *inf;
847
848 inf = u + sizeof(*update) - sizeof(struct imsm_dev) +
849 sizeof_imsm_dev(&update->dev, 0);
850
851 return inf;
852 }
853
854 static struct imsm_dev *__get_imsm_dev(struct imsm_super *mpb, __u8 index)
855 {
856 int offset;
857 int i;
858 void *_mpb = mpb;
859
860 if (index >= mpb->num_raid_devs)
861 return NULL;
862
863 /* devices start after all disks */
864 offset = ((void *) &mpb->disk[mpb->num_disks]) - _mpb;
865
866 for (i = 0; i <= index; i++)
867 if (i == index)
868 return _mpb + offset;
869 else
870 offset += sizeof_imsm_dev(_mpb + offset, 0);
871
872 return NULL;
873 }
874
875 static struct imsm_dev *get_imsm_dev(struct intel_super *super, __u8 index)
876 {
877 struct intel_dev *dv;
878
879 if (index >= super->anchor->num_raid_devs)
880 return NULL;
881 for (dv = super->devlist; dv; dv = dv->next)
882 if (dv->index == index)
883 return dv->dev;
884 return NULL;
885 }
886
887 static inline unsigned long long __le48_to_cpu(const struct bbm_log_block_addr
888 *addr)
889 {
890 return ((((__u64)__le32_to_cpu(addr->dw1)) << 16) |
891 __le16_to_cpu(addr->w1));
892 }
893
894 static inline struct bbm_log_block_addr __cpu_to_le48(unsigned long long sec)
895 {
896 struct bbm_log_block_addr addr;
897
898 addr.w1 = __cpu_to_le16((__u16)(sec & 0xffff));
899 addr.dw1 = __cpu_to_le32((__u32)(sec >> 16) & 0xffffffff);
900 return addr;
901 }
902
903 /* get size of the bbm log */
904 static __u32 get_imsm_bbm_log_size(struct bbm_log *log)
905 {
906 if (!log || log->entry_count == 0)
907 return 0;
908
909 return sizeof(log->signature) +
910 sizeof(log->entry_count) +
911 log->entry_count * sizeof(struct bbm_log_entry);
912 }
913
914 /* check if bad block is not partially stored in bbm log */
915 static int is_stored_in_bbm(struct bbm_log *log, const __u8 idx, const unsigned
916 long long sector, const int length, __u32 *pos)
917 {
918 __u32 i;
919
920 for (i = *pos; i < log->entry_count; i++) {
921 struct bbm_log_entry *entry = &log->marked_block_entries[i];
922 unsigned long long bb_start;
923 unsigned long long bb_end;
924
925 bb_start = __le48_to_cpu(&entry->defective_block_start);
926 bb_end = bb_start + (entry->marked_count + 1);
927
928 if ((entry->disk_ordinal == idx) && (bb_start >= sector) &&
929 (bb_end <= sector + length)) {
930 *pos = i;
931 return 1;
932 }
933 }
934 return 0;
935 }
936
937 /* record new bad block in bbm log */
938 static int record_new_badblock(struct bbm_log *log, const __u8 idx, unsigned
939 long long sector, int length)
940 {
941 int new_bb = 0;
942 __u32 pos = 0;
943 struct bbm_log_entry *entry = NULL;
944
945 while (is_stored_in_bbm(log, idx, sector, length, &pos)) {
946 struct bbm_log_entry *e = &log->marked_block_entries[pos];
947
948 if ((e->marked_count + 1 == BBM_LOG_MAX_LBA_ENTRY_VAL) &&
949 (__le48_to_cpu(&e->defective_block_start) == sector)) {
950 sector += BBM_LOG_MAX_LBA_ENTRY_VAL;
951 length -= BBM_LOG_MAX_LBA_ENTRY_VAL;
952 pos = pos + 1;
953 continue;
954 }
955 entry = e;
956 break;
957 }
958
959 if (entry) {
960 int cnt = (length <= BBM_LOG_MAX_LBA_ENTRY_VAL) ? length :
961 BBM_LOG_MAX_LBA_ENTRY_VAL;
962 entry->defective_block_start = __cpu_to_le48(sector);
963 entry->marked_count = cnt - 1;
964 if (cnt == length)
965 return 1;
966 sector += cnt;
967 length -= cnt;
968 }
969
970 new_bb = ROUND_UP(length, BBM_LOG_MAX_LBA_ENTRY_VAL) /
971 BBM_LOG_MAX_LBA_ENTRY_VAL;
972 if (log->entry_count + new_bb > BBM_LOG_MAX_ENTRIES)
973 return 0;
974
975 while (length > 0) {
976 int cnt = (length <= BBM_LOG_MAX_LBA_ENTRY_VAL) ? length :
977 BBM_LOG_MAX_LBA_ENTRY_VAL;
978 struct bbm_log_entry *entry =
979 &log->marked_block_entries[log->entry_count];
980
981 entry->defective_block_start = __cpu_to_le48(sector);
982 entry->marked_count = cnt - 1;
983 entry->disk_ordinal = idx;
984
985 sector += cnt;
986 length -= cnt;
987
988 log->entry_count++;
989 }
990
991 return new_bb;
992 }
993
994 /* clear all bad blocks for given disk */
995 static void clear_disk_badblocks(struct bbm_log *log, const __u8 idx)
996 {
997 __u32 i = 0;
998
999 while (i < log->entry_count) {
1000 struct bbm_log_entry *entries = log->marked_block_entries;
1001
1002 if (entries[i].disk_ordinal == idx) {
1003 if (i < log->entry_count - 1)
1004 entries[i] = entries[log->entry_count - 1];
1005 log->entry_count--;
1006 } else {
1007 i++;
1008 }
1009 }
1010 }
1011
1012 /* clear given bad block */
1013 static int clear_badblock(struct bbm_log *log, const __u8 idx, const unsigned
1014 long long sector, const int length) {
1015 __u32 i = 0;
1016
1017 while (i < log->entry_count) {
1018 struct bbm_log_entry *entries = log->marked_block_entries;
1019
1020 if ((entries[i].disk_ordinal == idx) &&
1021 (__le48_to_cpu(&entries[i].defective_block_start) ==
1022 sector) && (entries[i].marked_count + 1 == length)) {
1023 if (i < log->entry_count - 1)
1024 entries[i] = entries[log->entry_count - 1];
1025 log->entry_count--;
1026 break;
1027 }
1028 i++;
1029 }
1030
1031 return 1;
1032 }
1033
1034 /* allocate and load BBM log from metadata */
1035 static int load_bbm_log(struct intel_super *super)
1036 {
1037 struct imsm_super *mpb = super->anchor;
1038 __u32 bbm_log_size = __le32_to_cpu(mpb->bbm_log_size);
1039
1040 super->bbm_log = xcalloc(1, sizeof(struct bbm_log));
1041 if (!super->bbm_log)
1042 return 1;
1043
1044 if (bbm_log_size) {
1045 struct bbm_log *log = (void *)mpb +
1046 __le32_to_cpu(mpb->mpb_size) - bbm_log_size;
1047
1048 __u32 entry_count;
1049
1050 if (bbm_log_size < sizeof(log->signature) +
1051 sizeof(log->entry_count))
1052 return 2;
1053
1054 entry_count = __le32_to_cpu(log->entry_count);
1055 if ((__le32_to_cpu(log->signature) != BBM_LOG_SIGNATURE) ||
1056 (entry_count > BBM_LOG_MAX_ENTRIES))
1057 return 3;
1058
1059 if (bbm_log_size !=
1060 sizeof(log->signature) + sizeof(log->entry_count) +
1061 entry_count * sizeof(struct bbm_log_entry))
1062 return 4;
1063
1064 memcpy(super->bbm_log, log, bbm_log_size);
1065 } else {
1066 super->bbm_log->signature = __cpu_to_le32(BBM_LOG_SIGNATURE);
1067 super->bbm_log->entry_count = 0;
1068 }
1069
1070 return 0;
1071 }
1072
1073 /* checks if bad block is within volume boundaries */
1074 static int is_bad_block_in_volume(const struct bbm_log_entry *entry,
1075 const unsigned long long start_sector,
1076 const unsigned long long size)
1077 {
1078 unsigned long long bb_start;
1079 unsigned long long bb_end;
1080
1081 bb_start = __le48_to_cpu(&entry->defective_block_start);
1082 bb_end = bb_start + (entry->marked_count + 1);
1083
1084 if (((bb_start >= start_sector) && (bb_start < start_sector + size)) ||
1085 ((bb_end >= start_sector) && (bb_end <= start_sector + size)))
1086 return 1;
1087
1088 return 0;
1089 }
1090
1091 /* get list of bad blocks on a drive for a volume */
1092 static void get_volume_badblocks(const struct bbm_log *log, const __u8 idx,
1093 const unsigned long long start_sector,
1094 const unsigned long long size,
1095 struct md_bb *bbs)
1096 {
1097 __u32 count = 0;
1098 __u32 i;
1099
1100 for (i = 0; i < log->entry_count; i++) {
1101 const struct bbm_log_entry *ent =
1102 &log->marked_block_entries[i];
1103 struct md_bb_entry *bb;
1104
1105 if ((ent->disk_ordinal == idx) &&
1106 is_bad_block_in_volume(ent, start_sector, size)) {
1107
1108 if (!bbs->entries) {
1109 bbs->entries = xmalloc(BBM_LOG_MAX_ENTRIES *
1110 sizeof(*bb));
1111 if (!bbs->entries)
1112 break;
1113 }
1114
1115 bb = &bbs->entries[count++];
1116 bb->sector = __le48_to_cpu(&ent->defective_block_start);
1117 bb->length = ent->marked_count + 1;
1118 }
1119 }
1120 bbs->count = count;
1121 }
1122
1123 /*
1124 * for second_map:
1125 * == MAP_0 get first map
1126 * == MAP_1 get second map
1127 * == MAP_X than get map according to the current migr_state
1128 */
1129 static __u32 get_imsm_ord_tbl_ent(struct imsm_dev *dev,
1130 int slot,
1131 int second_map)
1132 {
1133 struct imsm_map *map;
1134
1135 map = get_imsm_map(dev, second_map);
1136
1137 /* top byte identifies disk under rebuild */
1138 return __le32_to_cpu(map->disk_ord_tbl[slot]);
1139 }
1140
1141 #define ord_to_idx(ord) (((ord) << 8) >> 8)
1142 static __u32 get_imsm_disk_idx(struct imsm_dev *dev, int slot, int second_map)
1143 {
1144 __u32 ord = get_imsm_ord_tbl_ent(dev, slot, second_map);
1145
1146 return ord_to_idx(ord);
1147 }
1148
1149 static void set_imsm_ord_tbl_ent(struct imsm_map *map, int slot, __u32 ord)
1150 {
1151 map->disk_ord_tbl[slot] = __cpu_to_le32(ord);
1152 }
1153
1154 static int get_imsm_disk_slot(struct imsm_map *map, unsigned idx)
1155 {
1156 int slot;
1157 __u32 ord;
1158
1159 for (slot = 0; slot < map->num_members; slot++) {
1160 ord = __le32_to_cpu(map->disk_ord_tbl[slot]);
1161 if (ord_to_idx(ord) == idx)
1162 return slot;
1163 }
1164
1165 return -1;
1166 }
1167
1168 static int get_imsm_raid_level(struct imsm_map *map)
1169 {
1170 if (map->raid_level == 1) {
1171 if (map->num_members == 2)
1172 return 1;
1173 else
1174 return 10;
1175 }
1176
1177 return map->raid_level;
1178 }
1179
1180 static int cmp_extent(const void *av, const void *bv)
1181 {
1182 const struct extent *a = av;
1183 const struct extent *b = bv;
1184 if (a->start < b->start)
1185 return -1;
1186 if (a->start > b->start)
1187 return 1;
1188 return 0;
1189 }
1190
1191 static int count_memberships(struct dl *dl, struct intel_super *super)
1192 {
1193 int memberships = 0;
1194 int i;
1195
1196 for (i = 0; i < super->anchor->num_raid_devs; i++) {
1197 struct imsm_dev *dev = get_imsm_dev(super, i);
1198 struct imsm_map *map = get_imsm_map(dev, MAP_0);
1199
1200 if (get_imsm_disk_slot(map, dl->index) >= 0)
1201 memberships++;
1202 }
1203
1204 return memberships;
1205 }
1206
1207 static __u32 imsm_min_reserved_sectors(struct intel_super *super);
1208
1209 static int split_ull(unsigned long long n, void *lo, void *hi)
1210 {
1211 if (lo == 0 || hi == 0)
1212 return 1;
1213 __put_unaligned32(__cpu_to_le32((__u32)n), lo);
1214 __put_unaligned32(__cpu_to_le32((n >> 32)), hi);
1215 return 0;
1216 }
1217
1218 static unsigned long long join_u32(__u32 lo, __u32 hi)
1219 {
1220 return (unsigned long long)__le32_to_cpu(lo) |
1221 (((unsigned long long)__le32_to_cpu(hi)) << 32);
1222 }
1223
1224 static unsigned long long total_blocks(struct imsm_disk *disk)
1225 {
1226 if (disk == NULL)
1227 return 0;
1228 return join_u32(disk->total_blocks_lo, disk->total_blocks_hi);
1229 }
1230
1231 static unsigned long long pba_of_lba0(struct imsm_map *map)
1232 {
1233 if (map == NULL)
1234 return 0;
1235 return join_u32(map->pba_of_lba0_lo, map->pba_of_lba0_hi);
1236 }
1237
1238 static unsigned long long blocks_per_member(struct imsm_map *map)
1239 {
1240 if (map == NULL)
1241 return 0;
1242 return join_u32(map->blocks_per_member_lo, map->blocks_per_member_hi);
1243 }
1244
1245 static unsigned long long num_data_stripes(struct imsm_map *map)
1246 {
1247 if (map == NULL)
1248 return 0;
1249 return join_u32(map->num_data_stripes_lo, map->num_data_stripes_hi);
1250 }
1251
1252 static unsigned long long vol_curr_migr_unit(struct imsm_dev *dev)
1253 {
1254 if (dev == NULL)
1255 return 0;
1256
1257 return join_u32(dev->vol.curr_migr_unit_lo, dev->vol.curr_migr_unit_hi);
1258 }
1259
1260 static unsigned long long imsm_dev_size(struct imsm_dev *dev)
1261 {
1262 if (dev == NULL)
1263 return 0;
1264 return join_u32(dev->size_low, dev->size_high);
1265 }
1266
1267 static unsigned long long migr_chkp_area_pba(struct migr_record *migr_rec)
1268 {
1269 if (migr_rec == NULL)
1270 return 0;
1271 return join_u32(migr_rec->ckpt_area_pba_lo,
1272 migr_rec->ckpt_area_pba_hi);
1273 }
1274
1275 static unsigned long long current_migr_unit(struct migr_record *migr_rec)
1276 {
1277 if (migr_rec == NULL)
1278 return 0;
1279 return join_u32(migr_rec->curr_migr_unit_lo,
1280 migr_rec->curr_migr_unit_hi);
1281 }
1282
1283 static unsigned long long migr_dest_1st_member_lba(struct migr_record *migr_rec)
1284 {
1285 if (migr_rec == NULL)
1286 return 0;
1287 return join_u32(migr_rec->dest_1st_member_lba_lo,
1288 migr_rec->dest_1st_member_lba_hi);
1289 }
1290
1291 static unsigned long long get_num_migr_units(struct migr_record *migr_rec)
1292 {
1293 if (migr_rec == NULL)
1294 return 0;
1295 return join_u32(migr_rec->num_migr_units_lo,
1296 migr_rec->num_migr_units_hi);
1297 }
1298
1299 static void set_total_blocks(struct imsm_disk *disk, unsigned long long n)
1300 {
1301 split_ull(n, &disk->total_blocks_lo, &disk->total_blocks_hi);
1302 }
1303
1304 static void set_pba_of_lba0(struct imsm_map *map, unsigned long long n)
1305 {
1306 split_ull(n, &map->pba_of_lba0_lo, &map->pba_of_lba0_hi);
1307 }
1308
1309 static void set_blocks_per_member(struct imsm_map *map, unsigned long long n)
1310 {
1311 split_ull(n, &map->blocks_per_member_lo, &map->blocks_per_member_hi);
1312 }
1313
1314 static void set_num_data_stripes(struct imsm_map *map, unsigned long long n)
1315 {
1316 split_ull(n, &map->num_data_stripes_lo, &map->num_data_stripes_hi);
1317 }
1318
1319 static void set_vol_curr_migr_unit(struct imsm_dev *dev, unsigned long long n)
1320 {
1321 if (dev == NULL)
1322 return;
1323
1324 split_ull(n, &dev->vol.curr_migr_unit_lo, &dev->vol.curr_migr_unit_hi);
1325 }
1326
1327 static void set_imsm_dev_size(struct imsm_dev *dev, unsigned long long n)
1328 {
1329 split_ull(n, &dev->size_low, &dev->size_high);
1330 }
1331
1332 static void set_migr_chkp_area_pba(struct migr_record *migr_rec,
1333 unsigned long long n)
1334 {
1335 split_ull(n, &migr_rec->ckpt_area_pba_lo, &migr_rec->ckpt_area_pba_hi);
1336 }
1337
1338 static void set_current_migr_unit(struct migr_record *migr_rec,
1339 unsigned long long n)
1340 {
1341 split_ull(n, &migr_rec->curr_migr_unit_lo,
1342 &migr_rec->curr_migr_unit_hi);
1343 }
1344
1345 static void set_migr_dest_1st_member_lba(struct migr_record *migr_rec,
1346 unsigned long long n)
1347 {
1348 split_ull(n, &migr_rec->dest_1st_member_lba_lo,
1349 &migr_rec->dest_1st_member_lba_hi);
1350 }
1351
1352 static void set_num_migr_units(struct migr_record *migr_rec,
1353 unsigned long long n)
1354 {
1355 split_ull(n, &migr_rec->num_migr_units_lo,
1356 &migr_rec->num_migr_units_hi);
1357 }
1358
1359 static unsigned long long per_dev_array_size(struct imsm_map *map)
1360 {
1361 unsigned long long array_size = 0;
1362
1363 if (map == NULL)
1364 return array_size;
1365
1366 array_size = num_data_stripes(map) * map->blocks_per_strip;
1367 if (get_imsm_raid_level(map) == 1 || get_imsm_raid_level(map) == 10)
1368 array_size *= 2;
1369
1370 return array_size;
1371 }
1372
1373 static struct extent *get_extents(struct intel_super *super, struct dl *dl,
1374 int get_minimal_reservation)
1375 {
1376 /* find a list of used extents on the given physical device */
1377 struct extent *rv, *e;
1378 int i;
1379 int memberships = count_memberships(dl, super);
1380 __u32 reservation;
1381
1382 /* trim the reserved area for spares, so they can join any array
1383 * regardless of whether the OROM has assigned sectors from the
1384 * IMSM_RESERVED_SECTORS region
1385 */
1386 if (dl->index == -1 || get_minimal_reservation)
1387 reservation = imsm_min_reserved_sectors(super);
1388 else
1389 reservation = MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS;
1390
1391 rv = xcalloc(sizeof(struct extent), (memberships + 1));
1392 e = rv;
1393
1394 for (i = 0; i < super->anchor->num_raid_devs; i++) {
1395 struct imsm_dev *dev = get_imsm_dev(super, i);
1396 struct imsm_map *map = get_imsm_map(dev, MAP_0);
1397
1398 if (get_imsm_disk_slot(map, dl->index) >= 0) {
1399 e->start = pba_of_lba0(map);
1400 e->size = per_dev_array_size(map);
1401 e++;
1402 }
1403 }
1404 qsort(rv, memberships, sizeof(*rv), cmp_extent);
1405
1406 /* determine the start of the metadata
1407 * when no raid devices are defined use the default
1408 * ...otherwise allow the metadata to truncate the value
1409 * as is the case with older versions of imsm
1410 */
1411 if (memberships) {
1412 struct extent *last = &rv[memberships - 1];
1413 unsigned long long remainder;
1414
1415 remainder = total_blocks(&dl->disk) - (last->start + last->size);
1416 /* round down to 1k block to satisfy precision of the kernel
1417 * 'size' interface
1418 */
1419 remainder &= ~1UL;
1420 /* make sure remainder is still sane */
1421 if (remainder < (unsigned)ROUND_UP(super->len, 512) >> 9)
1422 remainder = ROUND_UP(super->len, 512) >> 9;
1423 if (reservation > remainder)
1424 reservation = remainder;
1425 }
1426 e->start = total_blocks(&dl->disk) - reservation;
1427 e->size = 0;
1428 return rv;
1429 }
1430
1431 /* try to determine how much space is reserved for metadata from
1432 * the last get_extents() entry, otherwise fallback to the
1433 * default
1434 */
1435 static __u32 imsm_reserved_sectors(struct intel_super *super, struct dl *dl)
1436 {
1437 struct extent *e;
1438 int i;
1439 __u32 rv;
1440
1441 /* for spares just return a minimal reservation which will grow
1442 * once the spare is picked up by an array
1443 */
1444 if (dl->index == -1)
1445 return MPB_SECTOR_CNT;
1446
1447 e = get_extents(super, dl, 0);
1448 if (!e)
1449 return MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS;
1450
1451 /* scroll to last entry */
1452 for (i = 0; e[i].size; i++)
1453 continue;
1454
1455 rv = total_blocks(&dl->disk) - e[i].start;
1456
1457 free(e);
1458
1459 return rv;
1460 }
1461
1462 static int is_spare(struct imsm_disk *disk)
1463 {
1464 return (disk->status & SPARE_DISK) == SPARE_DISK;
1465 }
1466
1467 static int is_configured(struct imsm_disk *disk)
1468 {
1469 return (disk->status & CONFIGURED_DISK) == CONFIGURED_DISK;
1470 }
1471
1472 static int is_failed(struct imsm_disk *disk)
1473 {
1474 return (disk->status & FAILED_DISK) == FAILED_DISK;
1475 }
1476
1477 static int is_journal(struct imsm_disk *disk)
1478 {
1479 return (disk->status & JOURNAL_DISK) == JOURNAL_DISK;
1480 }
1481
1482 /* round array size down to closest MB and ensure it splits evenly
1483 * between members
1484 */
1485 static unsigned long long round_size_to_mb(unsigned long long size, unsigned int
1486 disk_count)
1487 {
1488 size /= disk_count;
1489 size = (size >> SECT_PER_MB_SHIFT) << SECT_PER_MB_SHIFT;
1490 size *= disk_count;
1491
1492 return size;
1493 }
1494
1495 static int able_to_resync(int raid_level, int missing_disks)
1496 {
1497 int max_missing_disks = 0;
1498
1499 switch (raid_level) {
1500 case 10:
1501 max_missing_disks = 1;
1502 break;
1503 default:
1504 max_missing_disks = 0;
1505 }
1506 return missing_disks <= max_missing_disks;
1507 }
1508
1509 /* try to determine how much space is reserved for metadata from
1510 * the last get_extents() entry on the smallest active disk,
1511 * otherwise fallback to the default
1512 */
1513 static __u32 imsm_min_reserved_sectors(struct intel_super *super)
1514 {
1515 struct extent *e;
1516 int i;
1517 unsigned long long min_active;
1518 __u32 remainder;
1519 __u32 rv = MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS;
1520 struct dl *dl, *dl_min = NULL;
1521
1522 if (!super)
1523 return rv;
1524
1525 min_active = 0;
1526 for (dl = super->disks; dl; dl = dl->next) {
1527 if (dl->index < 0)
1528 continue;
1529 unsigned long long blocks = total_blocks(&dl->disk);
1530 if (blocks < min_active || min_active == 0) {
1531 dl_min = dl;
1532 min_active = blocks;
1533 }
1534 }
1535 if (!dl_min)
1536 return rv;
1537
1538 /* find last lba used by subarrays on the smallest active disk */
1539 e = get_extents(super, dl_min, 0);
1540 if (!e)
1541 return rv;
1542 for (i = 0; e[i].size; i++)
1543 continue;
1544
1545 remainder = min_active - e[i].start;
1546 free(e);
1547
1548 /* to give priority to recovery we should not require full
1549 IMSM_RESERVED_SECTORS from the spare */
1550 rv = MPB_SECTOR_CNT + NUM_BLOCKS_DIRTY_STRIPE_REGION;
1551
1552 /* if real reservation is smaller use that value */
1553 return (remainder < rv) ? remainder : rv;
1554 }
1555
1556 /*
1557 * Return minimum size of a spare and sector size
1558 * that can be used in this array
1559 */
1560 int get_spare_criteria_imsm(struct supertype *st, struct spare_criteria *c)
1561 {
1562 struct intel_super *super = st->sb;
1563 struct dl *dl;
1564 struct extent *e;
1565 int i;
1566 unsigned long long size = 0;
1567
1568 c->min_size = 0;
1569 c->sector_size = 0;
1570
1571 if (!super)
1572 return -EINVAL;
1573 /* find first active disk in array */
1574 dl = super->disks;
1575 while (dl && (is_failed(&dl->disk) || dl->index == -1))
1576 dl = dl->next;
1577 if (!dl)
1578 return -EINVAL;
1579 /* find last lba used by subarrays */
1580 e = get_extents(super, dl, 0);
1581 if (!e)
1582 return -EINVAL;
1583 for (i = 0; e[i].size; i++)
1584 continue;
1585 if (i > 0)
1586 size = e[i-1].start + e[i-1].size;
1587 free(e);
1588
1589 /* add the amount of space needed for metadata */
1590 size += imsm_min_reserved_sectors(super);
1591
1592 c->min_size = size * 512;
1593 c->sector_size = super->sector_size;
1594
1595 return 0;
1596 }
1597
1598 static int is_gen_migration(struct imsm_dev *dev);
1599
1600 #define IMSM_4K_DIV 8
1601
1602 static __u64 blocks_per_migr_unit(struct intel_super *super,
1603 struct imsm_dev *dev);
1604
1605 static void print_imsm_dev(struct intel_super *super,
1606 struct imsm_dev *dev,
1607 char *uuid,
1608 int disk_idx)
1609 {
1610 __u64 sz;
1611 int slot, i;
1612 struct imsm_map *map = get_imsm_map(dev, MAP_0);
1613 struct imsm_map *map2 = get_imsm_map(dev, MAP_1);
1614 __u32 ord;
1615
1616 printf("\n");
1617 printf("[%.16s]:\n", dev->volume);
1618 printf(" Subarray : %d\n", super->current_vol);
1619 printf(" UUID : %s\n", uuid);
1620 printf(" RAID Level : %d", get_imsm_raid_level(map));
1621 if (map2)
1622 printf(" <-- %d", get_imsm_raid_level(map2));
1623 printf("\n");
1624 printf(" Members : %d", map->num_members);
1625 if (map2)
1626 printf(" <-- %d", map2->num_members);
1627 printf("\n");
1628 printf(" Slots : [");
1629 for (i = 0; i < map->num_members; i++) {
1630 ord = get_imsm_ord_tbl_ent(dev, i, MAP_0);
1631 printf("%s", ord & IMSM_ORD_REBUILD ? "_" : "U");
1632 }
1633 printf("]");
1634 if (map2) {
1635 printf(" <-- [");
1636 for (i = 0; i < map2->num_members; i++) {
1637 ord = get_imsm_ord_tbl_ent(dev, i, MAP_1);
1638 printf("%s", ord & IMSM_ORD_REBUILD ? "_" : "U");
1639 }
1640 printf("]");
1641 }
1642 printf("\n");
1643 printf(" Failed disk : ");
1644 if (map->failed_disk_num == 0xff)
1645 printf("none");
1646 else
1647 printf("%i", map->failed_disk_num);
1648 printf("\n");
1649 slot = get_imsm_disk_slot(map, disk_idx);
1650 if (slot >= 0) {
1651 ord = get_imsm_ord_tbl_ent(dev, slot, MAP_X);
1652 printf(" This Slot : %d%s\n", slot,
1653 ord & IMSM_ORD_REBUILD ? " (out-of-sync)" : "");
1654 } else
1655 printf(" This Slot : ?\n");
1656 printf(" Sector Size : %u\n", super->sector_size);
1657 sz = imsm_dev_size(dev);
1658 printf(" Array Size : %llu%s\n",
1659 (unsigned long long)sz * 512 / super->sector_size,
1660 human_size(sz * 512));
1661 sz = blocks_per_member(map);
1662 printf(" Per Dev Size : %llu%s\n",
1663 (unsigned long long)sz * 512 / super->sector_size,
1664 human_size(sz * 512));
1665 printf(" Sector Offset : %llu\n",
1666 pba_of_lba0(map));
1667 printf(" Num Stripes : %llu\n",
1668 num_data_stripes(map));
1669 printf(" Chunk Size : %u KiB",
1670 __le16_to_cpu(map->blocks_per_strip) / 2);
1671 if (map2)
1672 printf(" <-- %u KiB",
1673 __le16_to_cpu(map2->blocks_per_strip) / 2);
1674 printf("\n");
1675 printf(" Reserved : %d\n", __le32_to_cpu(dev->reserved_blocks));
1676 printf(" Migrate State : ");
1677 if (dev->vol.migr_state) {
1678 if (migr_type(dev) == MIGR_INIT)
1679 printf("initialize\n");
1680 else if (migr_type(dev) == MIGR_REBUILD)
1681 printf("rebuild\n");
1682 else if (migr_type(dev) == MIGR_VERIFY)
1683 printf("check\n");
1684 else if (migr_type(dev) == MIGR_GEN_MIGR)
1685 printf("general migration\n");
1686 else if (migr_type(dev) == MIGR_STATE_CHANGE)
1687 printf("state change\n");
1688 else if (migr_type(dev) == MIGR_REPAIR)
1689 printf("repair\n");
1690 else
1691 printf("<unknown:%d>\n", migr_type(dev));
1692 } else
1693 printf("idle\n");
1694 printf(" Map State : %s", map_state_str[map->map_state]);
1695 if (dev->vol.migr_state) {
1696 struct imsm_map *map = get_imsm_map(dev, MAP_1);
1697
1698 printf(" <-- %s", map_state_str[map->map_state]);
1699 printf("\n Checkpoint : %llu ", vol_curr_migr_unit(dev));
1700 if (is_gen_migration(dev) && (slot > 1 || slot < 0))
1701 printf("(N/A)");
1702 else
1703 printf("(%llu)", (unsigned long long)
1704 blocks_per_migr_unit(super, dev));
1705 }
1706 printf("\n");
1707 printf(" Dirty State : %s\n", (dev->vol.dirty & RAIDVOL_DIRTY) ?
1708 "dirty" : "clean");
1709 printf(" RWH Policy : ");
1710 if (dev->rwh_policy == RWH_OFF || dev->rwh_policy == RWH_MULTIPLE_OFF)
1711 printf("off\n");
1712 else if (dev->rwh_policy == RWH_DISTRIBUTED)
1713 printf("PPL distributed\n");
1714 else if (dev->rwh_policy == RWH_JOURNALING_DRIVE)
1715 printf("PPL journaling drive\n");
1716 else if (dev->rwh_policy == RWH_MULTIPLE_DISTRIBUTED)
1717 printf("Multiple distributed PPLs\n");
1718 else if (dev->rwh_policy == RWH_MULTIPLE_PPLS_JOURNALING_DRIVE)
1719 printf("Multiple PPLs on journaling drive\n");
1720 else if (dev->rwh_policy == RWH_BITMAP)
1721 printf("Write-intent bitmap\n");
1722 else
1723 printf("<unknown:%d>\n", dev->rwh_policy);
1724
1725 printf(" Volume ID : %u\n", dev->my_vol_raid_dev_num);
1726 }
1727
1728 static void print_imsm_disk(struct imsm_disk *disk,
1729 int index,
1730 __u32 reserved,
1731 unsigned int sector_size) {
1732 char str[MAX_RAID_SERIAL_LEN + 1];
1733 __u64 sz;
1734
1735 if (index < -1 || !disk)
1736 return;
1737
1738 printf("\n");
1739 snprintf(str, MAX_RAID_SERIAL_LEN + 1, "%s", disk->serial);
1740 if (index >= 0)
1741 printf(" Disk%02d Serial : %s\n", index, str);
1742 else
1743 printf(" Disk Serial : %s\n", str);
1744 printf(" State :%s%s%s%s\n", is_spare(disk) ? " spare" : "",
1745 is_configured(disk) ? " active" : "",
1746 is_failed(disk) ? " failed" : "",
1747 is_journal(disk) ? " journal" : "");
1748 printf(" Id : %08x\n", __le32_to_cpu(disk->scsi_id));
1749 sz = total_blocks(disk) - reserved;
1750 printf(" Usable Size : %llu%s\n",
1751 (unsigned long long)sz * 512 / sector_size,
1752 human_size(sz * 512));
1753 }
1754
1755 void convert_to_4k_imsm_migr_rec(struct intel_super *super)
1756 {
1757 struct migr_record *migr_rec = super->migr_rec;
1758
1759 migr_rec->blocks_per_unit /= IMSM_4K_DIV;
1760 migr_rec->dest_depth_per_unit /= IMSM_4K_DIV;
1761 split_ull((join_u32(migr_rec->post_migr_vol_cap,
1762 migr_rec->post_migr_vol_cap_hi) / IMSM_4K_DIV),
1763 &migr_rec->post_migr_vol_cap, &migr_rec->post_migr_vol_cap_hi);
1764 set_migr_chkp_area_pba(migr_rec,
1765 migr_chkp_area_pba(migr_rec) / IMSM_4K_DIV);
1766 set_migr_dest_1st_member_lba(migr_rec,
1767 migr_dest_1st_member_lba(migr_rec) / IMSM_4K_DIV);
1768 }
1769
1770 void convert_to_4k_imsm_disk(struct imsm_disk *disk)
1771 {
1772 set_total_blocks(disk, (total_blocks(disk)/IMSM_4K_DIV));
1773 }
1774
1775 void convert_to_4k(struct intel_super *super)
1776 {
1777 struct imsm_super *mpb = super->anchor;
1778 struct imsm_disk *disk;
1779 int i;
1780 __u32 bbm_log_size = __le32_to_cpu(mpb->bbm_log_size);
1781
1782 for (i = 0; i < mpb->num_disks ; i++) {
1783 disk = __get_imsm_disk(mpb, i);
1784 /* disk */
1785 convert_to_4k_imsm_disk(disk);
1786 }
1787 for (i = 0; i < mpb->num_raid_devs; i++) {
1788 struct imsm_dev *dev = __get_imsm_dev(mpb, i);
1789 struct imsm_map *map = get_imsm_map(dev, MAP_0);
1790 /* dev */
1791 set_imsm_dev_size(dev, imsm_dev_size(dev)/IMSM_4K_DIV);
1792 set_vol_curr_migr_unit(dev,
1793 vol_curr_migr_unit(dev) / IMSM_4K_DIV);
1794
1795 /* map0 */
1796 set_blocks_per_member(map, blocks_per_member(map)/IMSM_4K_DIV);
1797 map->blocks_per_strip /= IMSM_4K_DIV;
1798 set_pba_of_lba0(map, pba_of_lba0(map)/IMSM_4K_DIV);
1799
1800 if (dev->vol.migr_state) {
1801 /* map1 */
1802 map = get_imsm_map(dev, MAP_1);
1803 set_blocks_per_member(map,
1804 blocks_per_member(map)/IMSM_4K_DIV);
1805 map->blocks_per_strip /= IMSM_4K_DIV;
1806 set_pba_of_lba0(map, pba_of_lba0(map)/IMSM_4K_DIV);
1807 }
1808 }
1809 if (bbm_log_size) {
1810 struct bbm_log *log = (void *)mpb +
1811 __le32_to_cpu(mpb->mpb_size) - bbm_log_size;
1812 __u32 i;
1813
1814 for (i = 0; i < log->entry_count; i++) {
1815 struct bbm_log_entry *entry =
1816 &log->marked_block_entries[i];
1817
1818 __u8 count = entry->marked_count + 1;
1819 unsigned long long sector =
1820 __le48_to_cpu(&entry->defective_block_start);
1821
1822 entry->defective_block_start =
1823 __cpu_to_le48(sector/IMSM_4K_DIV);
1824 entry->marked_count = max(count/IMSM_4K_DIV, 1) - 1;
1825 }
1826 }
1827
1828 mpb->check_sum = __gen_imsm_checksum(mpb);
1829 }
1830
1831 void examine_migr_rec_imsm(struct intel_super *super)
1832 {
1833 struct migr_record *migr_rec = super->migr_rec;
1834 struct imsm_super *mpb = super->anchor;
1835 int i;
1836
1837 for (i = 0; i < mpb->num_raid_devs; i++) {
1838 struct imsm_dev *dev = __get_imsm_dev(mpb, i);
1839 struct imsm_map *map;
1840 int slot = -1;
1841
1842 if (is_gen_migration(dev) == 0)
1843 continue;
1844
1845 printf("\nMigration Record Information:");
1846
1847 /* first map under migration */
1848 map = get_imsm_map(dev, MAP_0);
1849 if (map)
1850 slot = get_imsm_disk_slot(map, super->disks->index);
1851 if (map == NULL || slot > 1 || slot < 0) {
1852 printf(" Empty\n ");
1853 printf("Examine one of first two disks in array\n");
1854 break;
1855 }
1856 printf("\n Status : ");
1857 if (__le32_to_cpu(migr_rec->rec_status) == UNIT_SRC_NORMAL)
1858 printf("Normal\n");
1859 else
1860 printf("Contains Data\n");
1861 printf(" Current Unit : %llu\n",
1862 current_migr_unit(migr_rec));
1863 printf(" Family : %u\n",
1864 __le32_to_cpu(migr_rec->family_num));
1865 printf(" Ascending : %u\n",
1866 __le32_to_cpu(migr_rec->ascending_migr));
1867 printf(" Blocks Per Unit : %u\n",
1868 __le32_to_cpu(migr_rec->blocks_per_unit));
1869 printf(" Dest. Depth Per Unit : %u\n",
1870 __le32_to_cpu(migr_rec->dest_depth_per_unit));
1871 printf(" Checkpoint Area pba : %llu\n",
1872 migr_chkp_area_pba(migr_rec));
1873 printf(" First member lba : %llu\n",
1874 migr_dest_1st_member_lba(migr_rec));
1875 printf(" Total Number of Units : %llu\n",
1876 get_num_migr_units(migr_rec));
1877 printf(" Size of volume : %llu\n",
1878 join_u32(migr_rec->post_migr_vol_cap,
1879 migr_rec->post_migr_vol_cap_hi));
1880 printf(" Record was read from : %u\n",
1881 __le32_to_cpu(migr_rec->ckpt_read_disk_num));
1882
1883 break;
1884 }
1885 }
1886
1887 void convert_from_4k_imsm_migr_rec(struct intel_super *super)
1888 {
1889 struct migr_record *migr_rec = super->migr_rec;
1890
1891 migr_rec->blocks_per_unit *= IMSM_4K_DIV;
1892 migr_rec->dest_depth_per_unit *= IMSM_4K_DIV;
1893 split_ull((join_u32(migr_rec->post_migr_vol_cap,
1894 migr_rec->post_migr_vol_cap_hi) * IMSM_4K_DIV),
1895 &migr_rec->post_migr_vol_cap,
1896 &migr_rec->post_migr_vol_cap_hi);
1897 set_migr_chkp_area_pba(migr_rec,
1898 migr_chkp_area_pba(migr_rec) * IMSM_4K_DIV);
1899 set_migr_dest_1st_member_lba(migr_rec,
1900 migr_dest_1st_member_lba(migr_rec) * IMSM_4K_DIV);
1901 }
1902
1903 void convert_from_4k(struct intel_super *super)
1904 {
1905 struct imsm_super *mpb = super->anchor;
1906 struct imsm_disk *disk;
1907 int i;
1908 __u32 bbm_log_size = __le32_to_cpu(mpb->bbm_log_size);
1909
1910 for (i = 0; i < mpb->num_disks ; i++) {
1911 disk = __get_imsm_disk(mpb, i);
1912 /* disk */
1913 set_total_blocks(disk, (total_blocks(disk)*IMSM_4K_DIV));
1914 }
1915
1916 for (i = 0; i < mpb->num_raid_devs; i++) {
1917 struct imsm_dev *dev = __get_imsm_dev(mpb, i);
1918 struct imsm_map *map = get_imsm_map(dev, MAP_0);
1919 /* dev */
1920 set_imsm_dev_size(dev, imsm_dev_size(dev)*IMSM_4K_DIV);
1921 set_vol_curr_migr_unit(dev,
1922 vol_curr_migr_unit(dev) * IMSM_4K_DIV);
1923
1924 /* map0 */
1925 set_blocks_per_member(map, blocks_per_member(map)*IMSM_4K_DIV);
1926 map->blocks_per_strip *= IMSM_4K_DIV;
1927 set_pba_of_lba0(map, pba_of_lba0(map)*IMSM_4K_DIV);
1928
1929 if (dev->vol.migr_state) {
1930 /* map1 */
1931 map = get_imsm_map(dev, MAP_1);
1932 set_blocks_per_member(map,
1933 blocks_per_member(map)*IMSM_4K_DIV);
1934 map->blocks_per_strip *= IMSM_4K_DIV;
1935 set_pba_of_lba0(map, pba_of_lba0(map)*IMSM_4K_DIV);
1936 }
1937 }
1938 if (bbm_log_size) {
1939 struct bbm_log *log = (void *)mpb +
1940 __le32_to_cpu(mpb->mpb_size) - bbm_log_size;
1941 __u32 i;
1942
1943 for (i = 0; i < log->entry_count; i++) {
1944 struct bbm_log_entry *entry =
1945 &log->marked_block_entries[i];
1946
1947 __u8 count = entry->marked_count + 1;
1948 unsigned long long sector =
1949 __le48_to_cpu(&entry->defective_block_start);
1950
1951 entry->defective_block_start =
1952 __cpu_to_le48(sector*IMSM_4K_DIV);
1953 entry->marked_count = count*IMSM_4K_DIV - 1;
1954 }
1955 }
1956
1957 mpb->check_sum = __gen_imsm_checksum(mpb);
1958 }
1959
1960 /*******************************************************************************
1961 * function: imsm_check_attributes
1962 * Description: Function checks if features represented by attributes flags
1963 * are supported by mdadm.
1964 * Parameters:
1965 * attributes - Attributes read from metadata
1966 * Returns:
1967 * 0 - passed attributes contains unsupported features flags
1968 * 1 - all features are supported
1969 ******************************************************************************/
1970 static int imsm_check_attributes(__u32 attributes)
1971 {
1972 int ret_val = 1;
1973 __u32 not_supported = MPB_ATTRIB_SUPPORTED^0xffffffff;
1974
1975 not_supported &= ~MPB_ATTRIB_IGNORED;
1976
1977 not_supported &= attributes;
1978 if (not_supported) {
1979 pr_err("(IMSM): Unsupported attributes : %x\n",
1980 (unsigned)__le32_to_cpu(not_supported));
1981 if (not_supported & MPB_ATTRIB_CHECKSUM_VERIFY) {
1982 dprintf("\t\tMPB_ATTRIB_CHECKSUM_VERIFY \n");
1983 not_supported ^= MPB_ATTRIB_CHECKSUM_VERIFY;
1984 }
1985 if (not_supported & MPB_ATTRIB_2TB) {
1986 dprintf("\t\tMPB_ATTRIB_2TB\n");
1987 not_supported ^= MPB_ATTRIB_2TB;
1988 }
1989 if (not_supported & MPB_ATTRIB_RAID0) {
1990 dprintf("\t\tMPB_ATTRIB_RAID0\n");
1991 not_supported ^= MPB_ATTRIB_RAID0;
1992 }
1993 if (not_supported & MPB_ATTRIB_RAID1) {
1994 dprintf("\t\tMPB_ATTRIB_RAID1\n");
1995 not_supported ^= MPB_ATTRIB_RAID1;
1996 }
1997 if (not_supported & MPB_ATTRIB_RAID10) {
1998 dprintf("\t\tMPB_ATTRIB_RAID10\n");
1999 not_supported ^= MPB_ATTRIB_RAID10;
2000 }
2001 if (not_supported & MPB_ATTRIB_RAID1E) {
2002 dprintf("\t\tMPB_ATTRIB_RAID1E\n");
2003 not_supported ^= MPB_ATTRIB_RAID1E;
2004 }
2005 if (not_supported & MPB_ATTRIB_RAID5) {
2006 dprintf("\t\tMPB_ATTRIB_RAID5\n");
2007 not_supported ^= MPB_ATTRIB_RAID5;
2008 }
2009 if (not_supported & MPB_ATTRIB_RAIDCNG) {
2010 dprintf("\t\tMPB_ATTRIB_RAIDCNG\n");
2011 not_supported ^= MPB_ATTRIB_RAIDCNG;
2012 }
2013 if (not_supported & MPB_ATTRIB_BBM) {
2014 dprintf("\t\tMPB_ATTRIB_BBM\n");
2015 not_supported ^= MPB_ATTRIB_BBM;
2016 }
2017 if (not_supported & MPB_ATTRIB_CHECKSUM_VERIFY) {
2018 dprintf("\t\tMPB_ATTRIB_CHECKSUM_VERIFY (== MPB_ATTRIB_LEGACY)\n");
2019 not_supported ^= MPB_ATTRIB_CHECKSUM_VERIFY;
2020 }
2021 if (not_supported & MPB_ATTRIB_EXP_STRIPE_SIZE) {
2022 dprintf("\t\tMPB_ATTRIB_EXP_STRIP_SIZE\n");
2023 not_supported ^= MPB_ATTRIB_EXP_STRIPE_SIZE;
2024 }
2025 if (not_supported & MPB_ATTRIB_2TB_DISK) {
2026 dprintf("\t\tMPB_ATTRIB_2TB_DISK\n");
2027 not_supported ^= MPB_ATTRIB_2TB_DISK;
2028 }
2029 if (not_supported & MPB_ATTRIB_NEVER_USE2) {
2030 dprintf("\t\tMPB_ATTRIB_NEVER_USE2\n");
2031 not_supported ^= MPB_ATTRIB_NEVER_USE2;
2032 }
2033 if (not_supported & MPB_ATTRIB_NEVER_USE) {
2034 dprintf("\t\tMPB_ATTRIB_NEVER_USE\n");
2035 not_supported ^= MPB_ATTRIB_NEVER_USE;
2036 }
2037
2038 if (not_supported)
2039 dprintf("(IMSM): Unknown attributes : %x\n", not_supported);
2040
2041 ret_val = 0;
2042 }
2043
2044 return ret_val;
2045 }
2046
2047 static void getinfo_super_imsm(struct supertype *st, struct mdinfo *info, char *map);
2048
2049 static void examine_super_imsm(struct supertype *st, char *homehost)
2050 {
2051 struct intel_super *super = st->sb;
2052 struct imsm_super *mpb = super->anchor;
2053 char str[MAX_SIGNATURE_LENGTH];
2054 int i;
2055 struct mdinfo info;
2056 char nbuf[64];
2057 __u32 sum;
2058 __u32 reserved = imsm_reserved_sectors(super, super->disks);
2059 struct dl *dl;
2060 time_t creation_time;
2061
2062 strncpy(str, (char *)mpb->sig, MPB_SIG_LEN);
2063 str[MPB_SIG_LEN-1] = '\0';
2064 printf(" Magic : %s\n", str);
2065 printf(" Version : %s\n", get_imsm_version(mpb));
2066 printf(" Orig Family : %08x\n", __le32_to_cpu(mpb->orig_family_num));
2067 printf(" Family : %08x\n", __le32_to_cpu(mpb->family_num));
2068 printf(" Generation : %08x\n", __le32_to_cpu(mpb->generation_num));
2069 creation_time = __le64_to_cpu(mpb->creation_time);
2070 printf(" Creation Time : %.24s\n",
2071 creation_time ? ctime(&creation_time) : "Unknown");
2072 printf(" Attributes : ");
2073 if (imsm_check_attributes(mpb->attributes))
2074 printf("All supported\n");
2075 else
2076 printf("not supported\n");
2077 getinfo_super_imsm(st, &info, NULL);
2078 fname_from_uuid(st, &info, nbuf, ':');
2079 printf(" UUID : %s\n", nbuf + 5);
2080 sum = __le32_to_cpu(mpb->check_sum);
2081 printf(" Checksum : %08x %s\n", sum,
2082 __gen_imsm_checksum(mpb) == sum ? "correct" : "incorrect");
2083 printf(" MPB Sectors : %d\n", mpb_sectors(mpb, super->sector_size));
2084 printf(" Disks : %d\n", mpb->num_disks);
2085 printf(" RAID Devices : %d\n", mpb->num_raid_devs);
2086 print_imsm_disk(__get_imsm_disk(mpb, super->disks->index),
2087 super->disks->index, reserved, super->sector_size);
2088 if (get_imsm_bbm_log_size(super->bbm_log)) {
2089 struct bbm_log *log = super->bbm_log;
2090
2091 printf("\n");
2092 printf("Bad Block Management Log:\n");
2093 printf(" Log Size : %d\n", __le32_to_cpu(mpb->bbm_log_size));
2094 printf(" Signature : %x\n", __le32_to_cpu(log->signature));
2095 printf(" Entry Count : %d\n", __le32_to_cpu(log->entry_count));
2096 }
2097 for (i = 0; i < mpb->num_raid_devs; i++) {
2098 struct mdinfo info;
2099 struct imsm_dev *dev = __get_imsm_dev(mpb, i);
2100
2101 super->current_vol = i;
2102 getinfo_super_imsm(st, &info, NULL);
2103 fname_from_uuid(st, &info, nbuf, ':');
2104 print_imsm_dev(super, dev, nbuf + 5, super->disks->index);
2105 }
2106 for (i = 0; i < mpb->num_disks; i++) {
2107 if (i == super->disks->index)
2108 continue;
2109 print_imsm_disk(__get_imsm_disk(mpb, i), i, reserved,
2110 super->sector_size);
2111 }
2112
2113 for (dl = super->disks; dl; dl = dl->next)
2114 if (dl->index == -1)
2115 print_imsm_disk(&dl->disk, -1, reserved,
2116 super->sector_size);
2117
2118 examine_migr_rec_imsm(super);
2119 }
2120
2121 static void brief_examine_super_imsm(struct supertype *st, int verbose)
2122 {
2123 /* We just write a generic IMSM ARRAY entry */
2124 struct mdinfo info;
2125 char nbuf[64];
2126 struct intel_super *super = st->sb;
2127
2128 if (!super->anchor->num_raid_devs) {
2129 printf("ARRAY metadata=imsm\n");
2130 return;
2131 }
2132
2133 getinfo_super_imsm(st, &info, NULL);
2134 fname_from_uuid(st, &info, nbuf, ':');
2135 printf("ARRAY metadata=imsm UUID=%s\n", nbuf + 5);
2136 }
2137
2138 static void brief_examine_subarrays_imsm(struct supertype *st, int verbose)
2139 {
2140 /* We just write a generic IMSM ARRAY entry */
2141 struct mdinfo info;
2142 char nbuf[64];
2143 char nbuf1[64];
2144 struct intel_super *super = st->sb;
2145 int i;
2146
2147 if (!super->anchor->num_raid_devs)
2148 return;
2149
2150 getinfo_super_imsm(st, &info, NULL);
2151 fname_from_uuid(st, &info, nbuf, ':');
2152 for (i = 0; i < super->anchor->num_raid_devs; i++) {
2153 struct imsm_dev *dev = get_imsm_dev(super, i);
2154
2155 super->current_vol = i;
2156 getinfo_super_imsm(st, &info, NULL);
2157 fname_from_uuid(st, &info, nbuf1, ':');
2158 printf("ARRAY /dev/md/%.16s container=%s member=%d UUID=%s\n",
2159 dev->volume, nbuf + 5, i, nbuf1 + 5);
2160 }
2161 }
2162
2163 static void export_examine_super_imsm(struct supertype *st)
2164 {
2165 struct intel_super *super = st->sb;
2166 struct imsm_super *mpb = super->anchor;
2167 struct mdinfo info;
2168 char nbuf[64];
2169
2170 getinfo_super_imsm(st, &info, NULL);
2171 fname_from_uuid(st, &info, nbuf, ':');
2172 printf("MD_METADATA=imsm\n");
2173 printf("MD_LEVEL=container\n");
2174 printf("MD_UUID=%s\n", nbuf+5);
2175 printf("MD_DEVICES=%u\n", mpb->num_disks);
2176 printf("MD_CREATION_TIME=%llu\n", __le64_to_cpu(mpb->creation_time));
2177 }
2178
2179 static void detail_super_imsm(struct supertype *st, char *homehost,
2180 char *subarray)
2181 {
2182 struct mdinfo info;
2183 char nbuf[64];
2184 struct intel_super *super = st->sb;
2185 int temp_vol = super->current_vol;
2186
2187 if (subarray)
2188 super->current_vol = strtoul(subarray, NULL, 10);
2189
2190 getinfo_super_imsm(st, &info, NULL);
2191 fname_from_uuid(st, &info, nbuf, ':');
2192 printf("\n UUID : %s\n", nbuf + 5);
2193
2194 super->current_vol = temp_vol;
2195 }
2196
2197 static void brief_detail_super_imsm(struct supertype *st, char *subarray)
2198 {
2199 struct mdinfo info;
2200 char nbuf[64];
2201 struct intel_super *super = st->sb;
2202 int temp_vol = super->current_vol;
2203
2204 if (subarray)
2205 super->current_vol = strtoul(subarray, NULL, 10);
2206
2207 getinfo_super_imsm(st, &info, NULL);
2208 fname_from_uuid(st, &info, nbuf, ':');
2209 printf(" UUID=%s", nbuf + 5);
2210
2211 super->current_vol = temp_vol;
2212 }
2213
2214 static int imsm_read_serial(int fd, char *devname, __u8 *serial,
2215 size_t serial_buf_len);
2216 static void fd2devname(int fd, char *name);
2217
2218 static int ahci_enumerate_ports(const char *hba_path, int port_count, int host_base, int verbose)
2219 {
2220 /* dump an unsorted list of devices attached to AHCI Intel storage
2221 * controller, as well as non-connected ports
2222 */
2223 int hba_len = strlen(hba_path) + 1;
2224 struct dirent *ent;
2225 DIR *dir;
2226 char *path = NULL;
2227 int err = 0;
2228 unsigned long port_mask = (1 << port_count) - 1;
2229
2230 if (port_count > (int)sizeof(port_mask) * 8) {
2231 if (verbose > 0)
2232 pr_err("port_count %d out of range\n", port_count);
2233 return 2;
2234 }
2235
2236 /* scroll through /sys/dev/block looking for devices attached to
2237 * this hba
2238 */
2239 dir = opendir("/sys/dev/block");
2240 if (!dir)
2241 return 1;
2242
2243 for (ent = readdir(dir); ent; ent = readdir(dir)) {
2244 int fd;
2245 char model[64];
2246 char vendor[64];
2247 char buf[1024];
2248 int major, minor;
2249 char *device;
2250 char *c;
2251 int port;
2252 int type;
2253
2254 if (sscanf(ent->d_name, "%d:%d", &major, &minor) != 2)
2255 continue;
2256 path = devt_to_devpath(makedev(major, minor));
2257 if (!path)
2258 continue;
2259 if (!path_attached_to_hba(path, hba_path)) {
2260 free(path);
2261 path = NULL;
2262 continue;
2263 }
2264
2265 /* retrieve the scsi device type */
2266 if (asprintf(&device, "/sys/dev/block/%d:%d/device/xxxxxxx", major, minor) < 0) {
2267 if (verbose > 0)
2268 pr_err("failed to allocate 'device'\n");
2269 err = 2;
2270 break;
2271 }
2272 sprintf(device, "/sys/dev/block/%d:%d/device/type", major, minor);
2273 if (load_sys(device, buf, sizeof(buf)) != 0) {
2274 if (verbose > 0)
2275 pr_err("failed to read device type for %s\n",
2276 path);
2277 err = 2;
2278 free(device);
2279 break;
2280 }
2281 type = strtoul(buf, NULL, 10);
2282
2283 /* if it's not a disk print the vendor and model */
2284 if (!(type == 0 || type == 7 || type == 14)) {
2285 vendor[0] = '\0';
2286 model[0] = '\0';
2287 sprintf(device, "/sys/dev/block/%d:%d/device/vendor", major, minor);
2288 if (load_sys(device, buf, sizeof(buf)) == 0) {
2289 strncpy(vendor, buf, sizeof(vendor));
2290 vendor[sizeof(vendor) - 1] = '\0';
2291 c = (char *) &vendor[sizeof(vendor) - 1];
2292 while (isspace(*c) || *c == '\0')
2293 *c-- = '\0';
2294
2295 }
2296 sprintf(device, "/sys/dev/block/%d:%d/device/model", major, minor);
2297 if (load_sys(device, buf, sizeof(buf)) == 0) {
2298 strncpy(model, buf, sizeof(model));
2299 model[sizeof(model) - 1] = '\0';
2300 c = (char *) &model[sizeof(model) - 1];
2301 while (isspace(*c) || *c == '\0')
2302 *c-- = '\0';
2303 }
2304
2305 if (vendor[0] && model[0])
2306 sprintf(buf, "%.64s %.64s", vendor, model);
2307 else
2308 switch (type) { /* numbers from hald/linux/device.c */
2309 case 1: sprintf(buf, "tape"); break;
2310 case 2: sprintf(buf, "printer"); break;
2311 case 3: sprintf(buf, "processor"); break;
2312 case 4:
2313 case 5: sprintf(buf, "cdrom"); break;
2314 case 6: sprintf(buf, "scanner"); break;
2315 case 8: sprintf(buf, "media_changer"); break;
2316 case 9: sprintf(buf, "comm"); break;
2317 case 12: sprintf(buf, "raid"); break;
2318 default: sprintf(buf, "unknown");
2319 }
2320 } else
2321 buf[0] = '\0';
2322 free(device);
2323
2324 /* chop device path to 'host%d' and calculate the port number */
2325 c = strchr(&path[hba_len], '/');
2326 if (!c) {
2327 if (verbose > 0)
2328 pr_err("%s - invalid path name\n", path + hba_len);
2329 err = 2;
2330 break;
2331 }
2332 *c = '\0';
2333 if ((sscanf(&path[hba_len], "ata%d", &port) == 1) ||
2334 ((sscanf(&path[hba_len], "host%d", &port) == 1)))
2335 port -= host_base;
2336 else {
2337 if (verbose > 0) {
2338 *c = '/'; /* repair the full string */
2339 pr_err("failed to determine port number for %s\n",
2340 path);
2341 }
2342 err = 2;
2343 break;
2344 }
2345
2346 /* mark this port as used */
2347 port_mask &= ~(1 << port);
2348
2349 /* print out the device information */
2350 if (buf[0]) {
2351 printf(" Port%d : - non-disk device (%s) -\n", port, buf);
2352 continue;
2353 }
2354
2355 fd = dev_open(ent->d_name, O_RDONLY);
2356 if (fd < 0)
2357 printf(" Port%d : - disk info unavailable -\n", port);
2358 else {
2359 fd2devname(fd, buf);
2360 printf(" Port%d : %s", port, buf);
2361 if (imsm_read_serial(fd, NULL, (__u8 *)buf,
2362 sizeof(buf)) == 0)
2363 printf(" (%s)\n", buf);
2364 else
2365 printf(" ()\n");
2366 close(fd);
2367 }
2368 free(path);
2369 path = NULL;
2370 }
2371 if (path)
2372 free(path);
2373 if (dir)
2374 closedir(dir);
2375 if (err == 0) {
2376 int i;
2377
2378 for (i = 0; i < port_count; i++)
2379 if (port_mask & (1 << i))
2380 printf(" Port%d : - no device attached -\n", i);
2381 }
2382
2383 return err;
2384 }
2385
2386 static int print_nvme_info(struct sys_dev *hba)
2387 {
2388 char buf[1024];
2389 struct dirent *ent;
2390 DIR *dir;
2391 char *rp;
2392 int fd;
2393
2394 dir = opendir("/sys/block/");
2395 if (!dir)
2396 return 1;
2397
2398 for (ent = readdir(dir); ent; ent = readdir(dir)) {
2399 if (strstr(ent->d_name, "nvme")) {
2400 sprintf(buf, "/sys/block/%s", ent->d_name);
2401 rp = realpath(buf, NULL);
2402 if (!rp)
2403 continue;
2404 if (path_attached_to_hba(rp, hba->path)) {
2405 fd = open_dev(ent->d_name);
2406 if (!imsm_is_nvme_supported(fd, 0)) {
2407 if (fd >= 0)
2408 close(fd);
2409 free(rp);
2410 continue;
2411 }
2412
2413 fd2devname(fd, buf);
2414 if (hba->type == SYS_DEV_VMD)
2415 printf(" NVMe under VMD : %s", buf);
2416 else if (hba->type == SYS_DEV_NVME)
2417 printf(" NVMe Device : %s", buf);
2418 if (!imsm_read_serial(fd, NULL, (__u8 *)buf,
2419 sizeof(buf)))
2420 printf(" (%s)\n", buf);
2421 else
2422 printf("()\n");
2423 close(fd);
2424 }
2425 free(rp);
2426 }
2427 }
2428
2429 closedir(dir);
2430 return 0;
2431 }
2432
2433 static void print_found_intel_controllers(struct sys_dev *elem)
2434 {
2435 for (; elem; elem = elem->next) {
2436 pr_err("found Intel(R) ");
2437 if (elem->type == SYS_DEV_SATA)
2438 fprintf(stderr, "SATA ");
2439 else if (elem->type == SYS_DEV_SAS)
2440 fprintf(stderr, "SAS ");
2441 else if (elem->type == SYS_DEV_NVME)
2442 fprintf(stderr, "NVMe ");
2443
2444 if (elem->type == SYS_DEV_VMD)
2445 fprintf(stderr, "VMD domain");
2446 else
2447 fprintf(stderr, "RAID controller");
2448
2449 if (elem->pci_id)
2450 fprintf(stderr, " at %s", elem->pci_id);
2451 fprintf(stderr, ".\n");
2452 }
2453 fflush(stderr);
2454 }
2455
2456 static int ahci_get_port_count(const char *hba_path, int *port_count)
2457 {
2458 struct dirent *ent;
2459 DIR *dir;
2460 int host_base = -1;
2461
2462 *port_count = 0;
2463 if ((dir = opendir(hba_path)) == NULL)
2464 return -1;
2465
2466 for (ent = readdir(dir); ent; ent = readdir(dir)) {
2467 int host;
2468
2469 if ((sscanf(ent->d_name, "ata%d", &host) != 1) &&
2470 ((sscanf(ent->d_name, "host%d", &host) != 1)))
2471 continue;
2472 if (*port_count == 0)
2473 host_base = host;
2474 else if (host < host_base)
2475 host_base = host;
2476
2477 if (host + 1 > *port_count + host_base)
2478 *port_count = host + 1 - host_base;
2479 }
2480 closedir(dir);
2481 return host_base;
2482 }
2483
2484 static void print_imsm_capability(const struct imsm_orom *orom)
2485 {
2486 printf(" Platform : Intel(R) ");
2487 if (orom->capabilities == 0 && orom->driver_features == 0)
2488 printf("Matrix Storage Manager\n");
2489 else if (imsm_orom_is_enterprise(orom) && orom->major_ver >= 6)
2490 printf("Virtual RAID on CPU\n");
2491 else
2492 printf("Rapid Storage Technology%s\n",
2493 imsm_orom_is_enterprise(orom) ? " enterprise" : "");
2494 if (orom->major_ver || orom->minor_ver || orom->hotfix_ver || orom->build)
2495 printf(" Version : %d.%d.%d.%d\n", orom->major_ver,
2496 orom->minor_ver, orom->hotfix_ver, orom->build);
2497 printf(" RAID Levels :%s%s%s%s%s\n",
2498 imsm_orom_has_raid0(orom) ? " raid0" : "",
2499 imsm_orom_has_raid1(orom) ? " raid1" : "",
2500 imsm_orom_has_raid1e(orom) ? " raid1e" : "",
2501 imsm_orom_has_raid10(orom) ? " raid10" : "",
2502 imsm_orom_has_raid5(orom) ? " raid5" : "");
2503 printf(" Chunk Sizes :%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
2504 imsm_orom_has_chunk(orom, 2) ? " 2k" : "",
2505 imsm_orom_has_chunk(orom, 4) ? " 4k" : "",
2506 imsm_orom_has_chunk(orom, 8) ? " 8k" : "",
2507 imsm_orom_has_chunk(orom, 16) ? " 16k" : "",
2508 imsm_orom_has_chunk(orom, 32) ? " 32k" : "",
2509 imsm_orom_has_chunk(orom, 64) ? " 64k" : "",
2510 imsm_orom_has_chunk(orom, 128) ? " 128k" : "",
2511 imsm_orom_has_chunk(orom, 256) ? " 256k" : "",
2512 imsm_orom_has_chunk(orom, 512) ? " 512k" : "",
2513 imsm_orom_has_chunk(orom, 1024*1) ? " 1M" : "",
2514 imsm_orom_has_chunk(orom, 1024*2) ? " 2M" : "",
2515 imsm_orom_has_chunk(orom, 1024*4) ? " 4M" : "",
2516 imsm_orom_has_chunk(orom, 1024*8) ? " 8M" : "",
2517 imsm_orom_has_chunk(orom, 1024*16) ? " 16M" : "",
2518 imsm_orom_has_chunk(orom, 1024*32) ? " 32M" : "",
2519 imsm_orom_has_chunk(orom, 1024*64) ? " 64M" : "");
2520 printf(" 2TB volumes :%s supported\n",
2521 (orom->attr & IMSM_OROM_ATTR_2TB)?"":" not");
2522 printf(" 2TB disks :%s supported\n",
2523 (orom->attr & IMSM_OROM_ATTR_2TB_DISK)?"":" not");
2524 printf(" Max Disks : %d\n", orom->tds);
2525 printf(" Max Volumes : %d per array, %d per %s\n",
2526 orom->vpa, orom->vphba,
2527 imsm_orom_is_nvme(orom) ? "platform" : "controller");
2528 return;
2529 }
2530
2531 static void print_imsm_capability_export(const struct imsm_orom *orom)
2532 {
2533 printf("MD_FIRMWARE_TYPE=imsm\n");
2534 if (orom->major_ver || orom->minor_ver || orom->hotfix_ver || orom->build)
2535 printf("IMSM_VERSION=%d.%d.%d.%d\n", orom->major_ver, orom->minor_ver,
2536 orom->hotfix_ver, orom->build);
2537 printf("IMSM_SUPPORTED_RAID_LEVELS=%s%s%s%s%s\n",
2538 imsm_orom_has_raid0(orom) ? "raid0 " : "",
2539 imsm_orom_has_raid1(orom) ? "raid1 " : "",
2540 imsm_orom_has_raid1e(orom) ? "raid1e " : "",
2541 imsm_orom_has_raid5(orom) ? "raid10 " : "",
2542 imsm_orom_has_raid10(orom) ? "raid5 " : "");
2543 printf("IMSM_SUPPORTED_CHUNK_SIZES=%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
2544 imsm_orom_has_chunk(orom, 2) ? "2k " : "",
2545 imsm_orom_has_chunk(orom, 4) ? "4k " : "",
2546 imsm_orom_has_chunk(orom, 8) ? "8k " : "",
2547 imsm_orom_has_chunk(orom, 16) ? "16k " : "",
2548 imsm_orom_has_chunk(orom, 32) ? "32k " : "",
2549 imsm_orom_has_chunk(orom, 64) ? "64k " : "",
2550 imsm_orom_has_chunk(orom, 128) ? "128k " : "",
2551 imsm_orom_has_chunk(orom, 256) ? "256k " : "",
2552 imsm_orom_has_chunk(orom, 512) ? "512k " : "",
2553 imsm_orom_has_chunk(orom, 1024*1) ? "1M " : "",
2554 imsm_orom_has_chunk(orom, 1024*2) ? "2M " : "",
2555 imsm_orom_has_chunk(orom, 1024*4) ? "4M " : "",
2556 imsm_orom_has_chunk(orom, 1024*8) ? "8M " : "",
2557 imsm_orom_has_chunk(orom, 1024*16) ? "16M " : "",
2558 imsm_orom_has_chunk(orom, 1024*32) ? "32M " : "",
2559 imsm_orom_has_chunk(orom, 1024*64) ? "64M " : "");
2560 printf("IMSM_2TB_VOLUMES=%s\n",(orom->attr & IMSM_OROM_ATTR_2TB) ? "yes" : "no");
2561 printf("IMSM_2TB_DISKS=%s\n",(orom->attr & IMSM_OROM_ATTR_2TB_DISK) ? "yes" : "no");
2562 printf("IMSM_MAX_DISKS=%d\n",orom->tds);
2563 printf("IMSM_MAX_VOLUMES_PER_ARRAY=%d\n",orom->vpa);
2564 printf("IMSM_MAX_VOLUMES_PER_CONTROLLER=%d\n",orom->vphba);
2565 }
2566
2567 static int detail_platform_imsm(int verbose, int enumerate_only, char *controller_path)
2568 {
2569 /* There are two components to imsm platform support, the ahci SATA
2570 * controller and the option-rom. To find the SATA controller we
2571 * simply look in /sys/bus/pci/drivers/ahci to see if an ahci
2572 * controller with the Intel vendor id is present. This approach
2573 * allows mdadm to leverage the kernel's ahci detection logic, with the
2574 * caveat that if ahci.ko is not loaded mdadm will not be able to
2575 * detect platform raid capabilities. The option-rom resides in a
2576 * platform "Adapter ROM". We scan for its signature to retrieve the
2577 * platform capabilities. If raid support is disabled in the BIOS the
2578 * option-rom capability structure will not be available.
2579 */
2580 struct sys_dev *list, *hba;
2581 int host_base = 0;
2582 int port_count = 0;
2583 int result=1;
2584
2585 if (enumerate_only) {
2586 if (check_env("IMSM_NO_PLATFORM"))
2587 return 0;
2588 list = find_intel_devices();
2589 if (!list)
2590 return 2;
2591 for (hba = list; hba; hba = hba->next) {
2592 if (find_imsm_capability(hba)) {
2593 result = 0;
2594 break;
2595 }
2596 else
2597 result = 2;
2598 }
2599 return result;
2600 }
2601
2602 list = find_intel_devices();
2603 if (!list) {
2604 if (verbose > 0)
2605 pr_err("no active Intel(R) RAID controller found.\n");
2606 return 2;
2607 } else if (verbose > 0)
2608 print_found_intel_controllers(list);
2609
2610 for (hba = list; hba; hba = hba->next) {
2611 if (controller_path && (compare_paths(hba->path, controller_path) != 0))
2612 continue;
2613 if (!find_imsm_capability(hba)) {
2614 char buf[PATH_MAX];
2615 pr_err("imsm capabilities not found for controller: %s (type %s)\n",
2616 hba->type == SYS_DEV_VMD ? vmd_domain_to_controller(hba, buf) : hba->path,
2617 get_sys_dev_type(hba->type));
2618 continue;
2619 }
2620 result = 0;
2621 }
2622
2623 if (controller_path && result == 1) {
2624 pr_err("no active Intel(R) RAID controller found under %s\n",
2625 controller_path);
2626 return result;
2627 }
2628
2629 const struct orom_entry *entry;
2630
2631 for (entry = orom_entries; entry; entry = entry->next) {
2632 if (entry->type == SYS_DEV_VMD) {
2633 print_imsm_capability(&entry->orom);
2634 printf(" 3rd party NVMe :%s supported\n",
2635 imsm_orom_has_tpv_support(&entry->orom)?"":" not");
2636 for (hba = list; hba; hba = hba->next) {
2637 if (hba->type == SYS_DEV_VMD) {
2638 char buf[PATH_MAX];
2639 printf(" I/O Controller : %s (%s)\n",
2640 vmd_domain_to_controller(hba, buf), get_sys_dev_type(hba->type));
2641 if (print_nvme_info(hba)) {
2642 if (verbose > 0)
2643 pr_err("failed to get devices attached to VMD domain.\n");
2644 result |= 2;
2645 }
2646 }
2647 }
2648 printf("\n");
2649 continue;
2650 }
2651
2652 print_imsm_capability(&entry->orom);
2653 if (entry->type == SYS_DEV_NVME) {
2654 for (hba = list; hba; hba = hba->next) {
2655 if (hba->type == SYS_DEV_NVME)
2656 print_nvme_info(hba);
2657 }
2658 printf("\n");
2659 continue;
2660 }
2661
2662 struct devid_list *devid;
2663 for (devid = entry->devid_list; devid; devid = devid->next) {
2664 hba = device_by_id(devid->devid);
2665 if (!hba)
2666 continue;
2667
2668 printf(" I/O Controller : %s (%s)\n",
2669 hba->path, get_sys_dev_type(hba->type));
2670 if (hba->type == SYS_DEV_SATA) {
2671 host_base = ahci_get_port_count(hba->path, &port_count);
2672 if (ahci_enumerate_ports(hba->path, port_count, host_base, verbose)) {
2673 if (verbose > 0)
2674 pr_err("failed to enumerate ports on SATA controller at %s.\n", hba->pci_id);
2675 result |= 2;
2676 }
2677 }
2678 }
2679 printf("\n");
2680 }
2681
2682 return result;
2683 }
2684
2685 static int export_detail_platform_imsm(int verbose, char *controller_path)
2686 {
2687 struct sys_dev *list, *hba;
2688 int result=1;
2689
2690 list = find_intel_devices();
2691 if (!list) {
2692 if (verbose > 0)
2693 pr_err("IMSM_DETAIL_PLATFORM_ERROR=NO_INTEL_DEVICES\n");
2694 result = 2;
2695 return result;
2696 }
2697
2698 for (hba = list; hba; hba = hba->next) {
2699 if (controller_path && (compare_paths(hba->path,controller_path) != 0))
2700 continue;
2701 if (!find_imsm_capability(hba) && verbose > 0) {
2702 char buf[PATH_MAX];
2703 pr_err("IMSM_DETAIL_PLATFORM_ERROR=NO_IMSM_CAPABLE_DEVICE_UNDER_%s\n",
2704 hba->type == SYS_DEV_VMD ? vmd_domain_to_controller(hba, buf) : hba->path);
2705 }
2706 else
2707 result = 0;
2708 }
2709
2710 const struct orom_entry *entry;
2711
2712 for (entry = orom_entries; entry; entry = entry->next) {
2713 if (entry->type == SYS_DEV_VMD) {
2714 for (hba = list; hba; hba = hba->next)
2715 print_imsm_capability_export(&entry->orom);
2716 continue;
2717 }
2718 print_imsm_capability_export(&entry->orom);
2719 }
2720
2721 return result;
2722 }
2723
2724 static int match_home_imsm(struct supertype *st, char *homehost)
2725 {
2726 /* the imsm metadata format does not specify any host
2727 * identification information. We return -1 since we can never
2728 * confirm nor deny whether a given array is "meant" for this
2729 * host. We rely on compare_super and the 'family_num' fields to
2730 * exclude member disks that do not belong, and we rely on
2731 * mdadm.conf to specify the arrays that should be assembled.
2732 * Auto-assembly may still pick up "foreign" arrays.
2733 */
2734
2735 return -1;
2736 }
2737
2738 static void uuid_from_super_imsm(struct supertype *st, int uuid[4])
2739 {
2740 /* The uuid returned here is used for:
2741 * uuid to put into bitmap file (Create, Grow)
2742 * uuid for backup header when saving critical section (Grow)
2743 * comparing uuids when re-adding a device into an array
2744 * In these cases the uuid required is that of the data-array,
2745 * not the device-set.
2746 * uuid to recognise same set when adding a missing device back
2747 * to an array. This is a uuid for the device-set.
2748 *
2749 * For each of these we can make do with a truncated
2750 * or hashed uuid rather than the original, as long as
2751 * everyone agrees.
2752 * In each case the uuid required is that of the data-array,
2753 * not the device-set.
2754 */
2755 /* imsm does not track uuid's so we synthesis one using sha1 on
2756 * - The signature (Which is constant for all imsm array, but no matter)
2757 * - the orig_family_num of the container
2758 * - the index number of the volume
2759 * - the 'serial' number of the volume.
2760 * Hopefully these are all constant.
2761 */
2762 struct intel_super *super = st->sb;
2763
2764 char buf[20];
2765 struct sha1_ctx ctx;
2766 struct imsm_dev *dev = NULL;
2767 __u32 family_num;
2768
2769 /* some mdadm versions failed to set ->orig_family_num, in which
2770 * case fall back to ->family_num. orig_family_num will be
2771 * fixed up with the first metadata update.
2772 */
2773 family_num = super->anchor->orig_family_num;
2774 if (family_num == 0)
2775 family_num = super->anchor->family_num;
2776 sha1_init_ctx(&ctx);
2777 sha1_process_bytes(super->anchor->sig, MPB_SIG_LEN, &ctx);
2778 sha1_process_bytes(&family_num, sizeof(__u32), &ctx);
2779 if (super->current_vol >= 0)
2780 dev = get_imsm_dev(super, super->current_vol);
2781 if (dev) {
2782 __u32 vol = super->current_vol;
2783 sha1_process_bytes(&vol, sizeof(vol), &ctx);
2784 sha1_process_bytes(dev->volume, MAX_RAID_SERIAL_LEN, &ctx);
2785 }
2786 sha1_finish_ctx(&ctx, buf);
2787 memcpy(uuid, buf, 4*4);
2788 }
2789
2790 #if 0
2791 static void
2792 get_imsm_numerical_version(struct imsm_super *mpb, int *m, int *p)
2793 {
2794 __u8 *v = get_imsm_version(mpb);
2795 __u8 *end = mpb->sig + MAX_SIGNATURE_LENGTH;
2796 char major[] = { 0, 0, 0 };
2797 char minor[] = { 0 ,0, 0 };
2798 char patch[] = { 0, 0, 0 };
2799 char *ver_parse[] = { major, minor, patch };
2800 int i, j;
2801
2802 i = j = 0;
2803 while (*v != '\0' && v < end) {
2804 if (*v != '.' && j < 2)
2805 ver_parse[i][j++] = *v;
2806 else {
2807 i++;
2808 j = 0;
2809 }
2810 v++;
2811 }
2812
2813 *m = strtol(minor, NULL, 0);
2814 *p = strtol(patch, NULL, 0);
2815 }
2816 #endif
2817
2818 static __u32 migr_strip_blocks_resync(struct imsm_dev *dev)
2819 {
2820 /* migr_strip_size when repairing or initializing parity */
2821 struct imsm_map *map = get_imsm_map(dev, MAP_0);
2822 __u32 chunk = __le32_to_cpu(map->blocks_per_strip);
2823
2824 switch (get_imsm_raid_level(map)) {
2825 case 5:
2826 case 10:
2827 return chunk;
2828 default:
2829 return 128*1024 >> 9;
2830 }
2831 }
2832
2833 static __u32 migr_strip_blocks_rebuild(struct imsm_dev *dev)
2834 {
2835 /* migr_strip_size when rebuilding a degraded disk, no idea why
2836 * this is different than migr_strip_size_resync(), but it's good
2837 * to be compatible
2838 */
2839 struct imsm_map *map = get_imsm_map(dev, MAP_1);
2840 __u32 chunk = __le32_to_cpu(map->blocks_per_strip);
2841
2842 switch (get_imsm_raid_level(map)) {
2843 case 1:
2844 case 10:
2845 if (map->num_members % map->num_domains == 0)
2846 return 128*1024 >> 9;
2847 else
2848 return chunk;
2849 case 5:
2850 return max((__u32) 64*1024 >> 9, chunk);
2851 default:
2852 return 128*1024 >> 9;
2853 }
2854 }
2855
2856 static __u32 num_stripes_per_unit_resync(struct imsm_dev *dev)
2857 {
2858 struct imsm_map *lo = get_imsm_map(dev, MAP_0);
2859 struct imsm_map *hi = get_imsm_map(dev, MAP_1);
2860 __u32 lo_chunk = __le32_to_cpu(lo->blocks_per_strip);
2861 __u32 hi_chunk = __le32_to_cpu(hi->blocks_per_strip);
2862
2863 return max((__u32) 1, hi_chunk / lo_chunk);
2864 }
2865
2866 static __u32 num_stripes_per_unit_rebuild(struct imsm_dev *dev)
2867 {
2868 struct imsm_map *lo = get_imsm_map(dev, MAP_0);
2869 int level = get_imsm_raid_level(lo);
2870
2871 if (level == 1 || level == 10) {
2872 struct imsm_map *hi = get_imsm_map(dev, MAP_1);
2873
2874 return hi->num_domains;
2875 } else
2876 return num_stripes_per_unit_resync(dev);
2877 }
2878
2879 static __u8 imsm_num_data_members(struct imsm_map *map)
2880 {
2881 /* named 'imsm_' because raid0, raid1 and raid10
2882 * counter-intuitively have the same number of data disks
2883 */
2884 switch (get_imsm_raid_level(map)) {
2885 case 0:
2886 return map->num_members;
2887 break;
2888 case 1:
2889 case 10:
2890 return map->num_members/2;
2891 case 5:
2892 return map->num_members - 1;
2893 default:
2894 dprintf("unsupported raid level\n");
2895 return 0;
2896 }
2897 }
2898
2899 static unsigned long long calc_component_size(struct imsm_map *map,
2900 struct imsm_dev *dev)
2901 {
2902 unsigned long long component_size;
2903 unsigned long long dev_size = imsm_dev_size(dev);
2904 long long calc_dev_size = 0;
2905 unsigned int member_disks = imsm_num_data_members(map);
2906
2907 if (member_disks == 0)
2908 return 0;
2909
2910 component_size = per_dev_array_size(map);
2911 calc_dev_size = component_size * member_disks;
2912
2913 /* Component size is rounded to 1MB so difference between size from
2914 * metadata and size calculated from num_data_stripes equals up to
2915 * 2048 blocks per each device. If the difference is higher it means
2916 * that array size was expanded and num_data_stripes was not updated.
2917 */
2918 if (llabs(calc_dev_size - (long long)dev_size) >
2919 (1 << SECT_PER_MB_SHIFT) * member_disks) {
2920 component_size = dev_size / member_disks;
2921 dprintf("Invalid num_data_stripes in metadata; expected=%llu, found=%llu\n",
2922 component_size / map->blocks_per_strip,
2923 num_data_stripes(map));
2924 }
2925
2926 return component_size;
2927 }
2928
2929 static __u32 parity_segment_depth(struct imsm_dev *dev)
2930 {
2931 struct imsm_map *map = get_imsm_map(dev, MAP_0);
2932 __u32 chunk = __le32_to_cpu(map->blocks_per_strip);
2933
2934 switch(get_imsm_raid_level(map)) {
2935 case 1:
2936 case 10:
2937 return chunk * map->num_domains;
2938 case 5:
2939 return chunk * map->num_members;
2940 default:
2941 return chunk;
2942 }
2943 }
2944
2945 static __u32 map_migr_block(struct imsm_dev *dev, __u32 block)
2946 {
2947 struct imsm_map *map = get_imsm_map(dev, MAP_1);
2948 __u32 chunk = __le32_to_cpu(map->blocks_per_strip);
2949 __u32 strip = block / chunk;
2950
2951 switch (get_imsm_raid_level(map)) {
2952 case 1:
2953 case 10: {
2954 __u32 vol_strip = (strip * map->num_domains) + 1;
2955 __u32 vol_stripe = vol_strip / map->num_members;
2956
2957 return vol_stripe * chunk + block % chunk;
2958 } case 5: {
2959 __u32 stripe = strip / (map->num_members - 1);
2960
2961 return stripe * chunk + block % chunk;
2962 }
2963 default:
2964 return 0;
2965 }
2966 }
2967
2968 static __u64 blocks_per_migr_unit(struct intel_super *super,
2969 struct imsm_dev *dev)
2970 {
2971 /* calculate the conversion factor between per member 'blocks'
2972 * (md/{resync,rebuild}_start) and imsm migration units, return
2973 * 0 for the 'not migrating' and 'unsupported migration' cases
2974 */
2975 if (!dev->vol.migr_state)
2976 return 0;
2977
2978 switch (migr_type(dev)) {
2979 case MIGR_GEN_MIGR: {
2980 struct migr_record *migr_rec = super->migr_rec;
2981 return __le32_to_cpu(migr_rec->blocks_per_unit);
2982 }
2983 case MIGR_VERIFY:
2984 case MIGR_REPAIR:
2985 case MIGR_INIT: {
2986 struct imsm_map *map = get_imsm_map(dev, MAP_0);
2987 __u32 stripes_per_unit;
2988 __u32 blocks_per_unit;
2989 __u32 parity_depth;
2990 __u32 migr_chunk;
2991 __u32 block_map;
2992 __u32 block_rel;
2993 __u32 segment;
2994 __u32 stripe;
2995 __u8 disks;
2996
2997 /* yes, this is really the translation of migr_units to
2998 * per-member blocks in the 'resync' case
2999 */
3000 stripes_per_unit = num_stripes_per_unit_resync(dev);
3001 migr_chunk = migr_strip_blocks_resync(dev);
3002 disks = imsm_num_data_members(map);
3003 blocks_per_unit = stripes_per_unit * migr_chunk * disks;
3004 stripe = __le16_to_cpu(map->blocks_per_strip) * disks;
3005 segment = blocks_per_unit / stripe;
3006 block_rel = blocks_per_unit - segment * stripe;
3007 parity_depth = parity_segment_depth(dev);
3008 block_map = map_migr_block(dev, block_rel);
3009 return block_map + parity_depth * segment;
3010 }
3011 case MIGR_REBUILD: {
3012 __u32 stripes_per_unit;
3013 __u32 migr_chunk;
3014
3015 stripes_per_unit = num_stripes_per_unit_rebuild(dev);
3016 migr_chunk = migr_strip_blocks_rebuild(dev);
3017 return migr_chunk * stripes_per_unit;
3018 }
3019 case MIGR_STATE_CHANGE:
3020 default:
3021 return 0;
3022 }
3023 }
3024
3025 static int imsm_level_to_layout(int level)
3026 {
3027 switch (level) {
3028 case 0:
3029 case 1:
3030 return 0;
3031 case 5:
3032 case 6:
3033 return ALGORITHM_LEFT_ASYMMETRIC;
3034 case 10:
3035 return 0x102;
3036 }
3037 return UnSet;
3038 }
3039
3040 /*******************************************************************************
3041 * Function: read_imsm_migr_rec
3042 * Description: Function reads imsm migration record from last sector of disk
3043 * Parameters:
3044 * fd : disk descriptor
3045 * super : metadata info
3046 * Returns:
3047 * 0 : success,
3048 * -1 : fail
3049 ******************************************************************************/
3050 static int read_imsm_migr_rec(int fd, struct intel_super *super)
3051 {
3052 int ret_val = -1;
3053 unsigned int sector_size = super->sector_size;
3054 unsigned long long dsize;
3055
3056 get_dev_size(fd, NULL, &dsize);
3057 if (lseek64(fd, dsize - (sector_size*MIGR_REC_SECTOR_POSITION),
3058 SEEK_SET) < 0) {
3059 pr_err("Cannot seek to anchor block: %s\n",
3060 strerror(errno));
3061 goto out;
3062 }
3063 if ((unsigned int)read(fd, super->migr_rec_buf,
3064 MIGR_REC_BUF_SECTORS*sector_size) !=
3065 MIGR_REC_BUF_SECTORS*sector_size) {
3066 pr_err("Cannot read migr record block: %s\n",
3067 strerror(errno));
3068 goto out;
3069 }
3070 ret_val = 0;
3071 if (sector_size == 4096)
3072 convert_from_4k_imsm_migr_rec(super);
3073
3074 out:
3075 return ret_val;
3076 }
3077
3078 static struct imsm_dev *imsm_get_device_during_migration(
3079 struct intel_super *super)
3080 {
3081
3082 struct intel_dev *dv;
3083
3084 for (dv = super->devlist; dv; dv = dv->next) {
3085 if (is_gen_migration(dv->dev))
3086 return dv->dev;
3087 }
3088 return NULL;
3089 }
3090
3091 /*******************************************************************************
3092 * Function: load_imsm_migr_rec
3093 * Description: Function reads imsm migration record (it is stored at the last
3094 * sector of disk)
3095 * Parameters:
3096 * super : imsm internal array info
3097 * Returns:
3098 * 0 : success
3099 * -1 : fail
3100 * -2 : no migration in progress
3101 ******************************************************************************/
3102 static int load_imsm_migr_rec(struct intel_super *super)
3103 {
3104 struct dl *dl;
3105 char nm[30];
3106 int retval = -1;
3107 int fd = -1;
3108 struct imsm_dev *dev;
3109 struct imsm_map *map;
3110 int slot = -1;
3111 int keep_fd = 1;
3112
3113 /* find map under migration */
3114 dev = imsm_get_device_during_migration(super);
3115 /* nothing to load,no migration in progress?
3116 */
3117 if (dev == NULL)
3118 return -2;
3119
3120 map = get_imsm_map(dev, MAP_0);
3121 if (!map)
3122 return -1;
3123
3124 for (dl = super->disks; dl; dl = dl->next) {
3125 /* skip spare and failed disks
3126 */
3127 if (dl->index < 0)
3128 continue;
3129 /* read only from one of the first two slots
3130 */
3131 slot = get_imsm_disk_slot(map, dl->index);
3132 if (slot > 1 || slot < 0)
3133 continue;
3134
3135 if (dl->fd < 0) {
3136 sprintf(nm, "%d:%d", dl->major, dl->minor);
3137 fd = dev_open(nm, O_RDONLY);
3138 if (fd >= 0) {
3139 keep_fd = 0;
3140 break;
3141 }
3142 } else {
3143 fd = dl->fd;
3144 break;
3145 }
3146 }
3147
3148 if (fd < 0)
3149 return retval;
3150 retval = read_imsm_migr_rec(fd, super);
3151 if (!keep_fd)
3152 close(fd);
3153
3154 return retval;
3155 }
3156
3157 /*******************************************************************************
3158 * function: imsm_create_metadata_checkpoint_update
3159 * Description: It creates update for checkpoint change.
3160 * Parameters:
3161 * super : imsm internal array info
3162 * u : pointer to prepared update
3163 * Returns:
3164 * Uptate length.
3165 * If length is equal to 0, input pointer u contains no update
3166 ******************************************************************************/
3167 static int imsm_create_metadata_checkpoint_update(
3168 struct intel_super *super,
3169 struct imsm_update_general_migration_checkpoint **u)
3170 {
3171
3172 int update_memory_size = 0;
3173
3174 dprintf("(enter)\n");
3175
3176 if (u == NULL)
3177 return 0;
3178 *u = NULL;
3179
3180 /* size of all update data without anchor */
3181 update_memory_size =
3182 sizeof(struct imsm_update_general_migration_checkpoint);
3183
3184 *u = xcalloc(1, update_memory_size);
3185 if (*u == NULL) {
3186 dprintf("error: cannot get memory\n");
3187 return 0;
3188 }
3189 (*u)->type = update_general_migration_checkpoint;
3190 (*u)->curr_migr_unit = current_migr_unit(super->migr_rec);
3191 dprintf("prepared for %llu\n", (*u)->curr_migr_unit);
3192
3193 return update_memory_size;
3194 }
3195
3196 static void imsm_update_metadata_locally(struct supertype *st,
3197 void *buf, int len);
3198
3199 /*******************************************************************************
3200 * Function: write_imsm_migr_rec
3201 * Description: Function writes imsm migration record
3202 * (at the last sector of disk)
3203 * Parameters:
3204 * super : imsm internal array info
3205 * Returns:
3206 * 0 : success
3207 * -1 : if fail
3208 ******************************************************************************/
3209 static int write_imsm_migr_rec(struct supertype *st)
3210 {
3211 struct intel_super *super = st->sb;
3212 unsigned int sector_size = super->sector_size;
3213 unsigned long long dsize;
3214 int retval = -1;
3215 struct dl *sd;
3216 int len;
3217 struct imsm_update_general_migration_checkpoint *u;
3218 struct imsm_dev *dev;
3219 struct imsm_map *map;
3220
3221 /* find map under migration */
3222 dev = imsm_get_device_during_migration(super);
3223 /* if no migration, write buffer anyway to clear migr_record
3224 * on disk based on first available device
3225 */
3226 if (dev == NULL)
3227 dev = get_imsm_dev(super, super->current_vol < 0 ? 0 :
3228 super->current_vol);
3229
3230 map = get_imsm_map(dev, MAP_0);
3231
3232 if (sector_size == 4096)
3233 convert_to_4k_imsm_migr_rec(super);
3234 for (sd = super->disks ; sd ; sd = sd->next) {
3235 int slot = -1;
3236
3237 /* skip failed and spare devices */
3238 if (sd->index < 0)
3239 continue;
3240 /* write to 2 first slots only */
3241 if (map)
3242 slot = get_imsm_disk_slot(map, sd->index);
3243 if (map == NULL || slot > 1 || slot < 0)
3244 continue;
3245
3246 get_dev_size(sd->fd, NULL, &dsize);
3247 if (lseek64(sd->fd, dsize - (MIGR_REC_SECTOR_POSITION *
3248 sector_size),
3249 SEEK_SET) < 0) {
3250 pr_err("Cannot seek to anchor block: %s\n",
3251 strerror(errno));
3252 goto out;
3253 }
3254 if ((unsigned int)write(sd->fd, super->migr_rec_buf,
3255 MIGR_REC_BUF_SECTORS*sector_size) !=
3256 MIGR_REC_BUF_SECTORS*sector_size) {
3257 pr_err("Cannot write migr record block: %s\n",
3258 strerror(errno));
3259 goto out;
3260 }
3261 }
3262 if (sector_size == 4096)
3263 convert_from_4k_imsm_migr_rec(super);
3264 /* update checkpoint information in metadata */
3265 len = imsm_create_metadata_checkpoint_update(super, &u);
3266 if (len <= 0) {
3267 dprintf("imsm: Cannot prepare update\n");
3268 goto out;
3269 }
3270 /* update metadata locally */
3271 imsm_update_metadata_locally(st, u, len);
3272 /* and possibly remotely */
3273 if (st->update_tail) {
3274 append_metadata_update(st, u, len);
3275 /* during reshape we do all work inside metadata handler
3276 * manage_reshape(), so metadata update has to be triggered
3277 * insida it
3278 */
3279 flush_metadata_updates(st);
3280 st->update_tail = &st->updates;
3281 } else
3282 free(u);
3283
3284 retval = 0;
3285 out:
3286 return retval;
3287 }
3288
3289 /* spare/missing disks activations are not allowe when
3290 * array/container performs reshape operation, because
3291 * all arrays in container works on the same disks set
3292 */
3293 int imsm_reshape_blocks_arrays_changes(struct intel_super *super)
3294 {
3295 int rv = 0;
3296 struct intel_dev *i_dev;
3297 struct imsm_dev *dev;
3298
3299 /* check whole container
3300 */
3301 for (i_dev = super->devlist; i_dev; i_dev = i_dev->next) {
3302 dev = i_dev->dev;
3303 if (is_gen_migration(dev)) {
3304 /* No repair during any migration in container
3305 */
3306 rv = 1;
3307 break;
3308 }
3309 }
3310 return rv;
3311 }
3312 static unsigned long long imsm_component_size_alignment_check(int level,
3313 int chunk_size,
3314 unsigned int sector_size,
3315 unsigned long long component_size)
3316 {
3317 unsigned int component_size_alignment;
3318
3319 /* check component size alignment
3320 */
3321 component_size_alignment = component_size % (chunk_size/sector_size);
3322
3323 dprintf("(Level: %i, chunk_size = %i, component_size = %llu), component_size_alignment = %u\n",
3324 level, chunk_size, component_size,
3325 component_size_alignment);
3326
3327 if (component_size_alignment && (level != 1) && (level != UnSet)) {
3328 dprintf("imsm: reported component size aligned from %llu ",
3329 component_size);
3330 component_size -= component_size_alignment;
3331 dprintf_cont("to %llu (%i).\n",
3332 component_size, component_size_alignment);
3333 }
3334
3335 return component_size;
3336 }
3337
3338 /*******************************************************************************
3339 * Function: get_bitmap_header_sector
3340 * Description: Returns the sector where the bitmap header is placed.
3341 * Parameters:
3342 * st : supertype information
3343 * dev_idx : index of the device with bitmap
3344 *
3345 * Returns:
3346 * The sector where the bitmap header is placed
3347 ******************************************************************************/
3348 static unsigned long long get_bitmap_header_sector(struct intel_super *super,
3349 int dev_idx)
3350 {
3351 struct imsm_dev *dev = get_imsm_dev(super, dev_idx);
3352 struct imsm_map *map = get_imsm_map(dev, MAP_0);
3353
3354 if (!super->sector_size) {
3355 dprintf("sector size is not set\n");
3356 return 0;
3357 }
3358
3359 return pba_of_lba0(map) + calc_component_size(map, dev) +
3360 (IMSM_BITMAP_HEADER_OFFSET / super->sector_size);
3361 }
3362
3363 /*******************************************************************************
3364 * Function: get_bitmap_sector
3365 * Description: Returns the sector where the bitmap is placed.
3366 * Parameters:
3367 * st : supertype information
3368 * dev_idx : index of the device with bitmap
3369 *
3370 * Returns:
3371 * The sector where the bitmap is placed
3372 ******************************************************************************/
3373 static unsigned long long get_bitmap_sector(struct intel_super *super,
3374 int dev_idx)
3375 {
3376 if (!super->sector_size) {
3377 dprintf("sector size is not set\n");
3378 return 0;
3379 }
3380
3381 return get_bitmap_header_sector(super, dev_idx) +
3382 (IMSM_BITMAP_HEADER_SIZE / super->sector_size);
3383 }
3384
3385 static unsigned long long get_ppl_sector(struct intel_super *super, int dev_idx)
3386 {
3387 struct imsm_dev *dev = get_imsm_dev(super, dev_idx);
3388 struct imsm_map *map = get_imsm_map(dev, MAP_0);
3389
3390 return pba_of_lba0(map) +
3391 (num_data_stripes(map) * map->blocks_per_strip);
3392 }
3393
3394 static void getinfo_super_imsm_volume(struct supertype *st, struct mdinfo *info, char *dmap)
3395 {
3396 struct intel_super *super = st->sb;
3397 struct migr_record *migr_rec = super->migr_rec;
3398 struct imsm_dev *dev = get_imsm_dev(super, super->current_vol);
3399 struct imsm_map *map = get_imsm_map(dev, MAP_0);
3400 struct imsm_map *prev_map = get_imsm_map(dev, MAP_1);
3401 struct imsm_map *map_to_analyse = map;
3402 struct dl *dl;
3403 int map_disks = info->array.raid_disks;
3404
3405 memset(info, 0, sizeof(*info));
3406 if (prev_map)
3407 map_to_analyse = prev_map;
3408
3409 dl = super->current_disk;
3410
3411 info->container_member = super->current_vol;
3412 info->array.raid_disks = map->num_members;
3413 info->array.level = get_imsm_raid_level(map_to_analyse);
3414 info->array.layout = imsm_level_to_layout(info->array.level);
3415 info->array.md_minor = -1;
3416 info->array.ctime = 0;
3417 info->array.utime = 0;
3418 info->array.chunk_size =
3419 __le16_to_cpu(map_to_analyse->blocks_per_strip) << 9;
3420 info->array.state = !(dev->vol.dirty & RAIDVOL_DIRTY);
3421 info->custom_array_size = imsm_dev_size(dev);
3422 info->recovery_blocked = imsm_reshape_blocks_arrays_changes(st->sb);
3423
3424 if (is_gen_migration(dev)) {
3425 info->reshape_active = 1;
3426 info->new_level = get_imsm_raid_level(map);
3427 info->new_layout = imsm_level_to_layout(info->new_level);
3428 info->new_chunk = __le16_to_cpu(map->blocks_per_strip) << 9;
3429 info->delta_disks = map->num_members - prev_map->num_members;
3430 if (info->delta_disks) {
3431 /* this needs to be applied to every array
3432 * in the container.
3433 */
3434 info->reshape_active = CONTAINER_RESHAPE;
3435 }
3436 /* We shape information that we give to md might have to be
3437 * modify to cope with md's requirement for reshaping arrays.
3438 * For example, when reshaping a RAID0, md requires it to be
3439 * presented as a degraded RAID4.
3440 * Also if a RAID0 is migrating to a RAID5 we need to specify
3441 * the array as already being RAID5, but the 'before' layout
3442 * is a RAID4-like layout.
3443 */
3444 switch (info->array.level) {
3445 case 0:
3446 switch(info->new_level) {
3447 case 0:
3448 /* conversion is happening as RAID4 */
3449 info->array.level = 4;
3450 info->array.raid_disks += 1;
3451 break;
3452 case 5:
3453 /* conversion is happening as RAID5 */
3454 info->array.level = 5;
3455 info->array.layout = ALGORITHM_PARITY_N;
3456 info->delta_disks -= 1;
3457 break;
3458 default:
3459 /* FIXME error message */
3460 info->array.level = UnSet;
3461 break;
3462 }
3463 break;
3464 }
3465 } else {
3466 info->new_level = UnSet;
3467 info->new_layout = UnSet;
3468 info->new_chunk = info->array.chunk_size;
3469 info->delta_disks = 0;
3470 }
3471
3472 if (dl) {
3473 info->disk.major = dl->major;
3474 info->disk.minor = dl->minor;
3475 info->disk.number = dl->index;
3476 info->disk.raid_disk = get_imsm_disk_slot(map_to_analyse,
3477 dl->index);
3478 }
3479
3480 info->data_offset = pba_of_lba0(map_to_analyse);
3481 info->component_size = calc_component_size(map, dev);
3482 info->component_size = imsm_component_size_alignment_check(
3483 info->array.level,
3484 info->array.chunk_size,
3485 super->sector_size,
3486 info->component_size);
3487 info->bb.supported = 1;
3488
3489 memset(info->uuid, 0, sizeof(info->uuid));
3490 info->recovery_start = MaxSector;
3491
3492 if (info->array.level == 5 &&
3493 (dev->rwh_policy == RWH_DISTRIBUTED ||
3494 dev->rwh_policy == RWH_MULTIPLE_DISTRIBUTED)) {
3495 info->consistency_policy = CONSISTENCY_POLICY_PPL;
3496 info->ppl_sector = get_ppl_sector(super, super->current_vol);
3497 if (dev->rwh_policy == RWH_MULTIPLE_DISTRIBUTED)
3498 info->ppl_size = MULTIPLE_PPL_AREA_SIZE_IMSM >> 9;
3499 else
3500 info->ppl_size = (PPL_HEADER_SIZE + PPL_ENTRY_SPACE)
3501 >> 9;
3502 } else if (info->array.level <= 0) {
3503 info->consistency_policy = CONSISTENCY_POLICY_NONE;
3504 } else {
3505 if (dev->rwh_policy == RWH_BITMAP) {
3506 info->bitmap_offset = get_bitmap_sector(super, super->current_vol);
3507 info->consistency_policy = CONSISTENCY_POLICY_BITMAP;
3508 } else {
3509 info->consistency_policy = CONSISTENCY_POLICY_RESYNC;
3510 }
3511 }
3512
3513 info->reshape_progress = 0;
3514 info->resync_start = MaxSector;
3515 if ((map_to_analyse->map_state == IMSM_T_STATE_UNINITIALIZED ||
3516 !(info->array.state & 1)) &&
3517 imsm_reshape_blocks_arrays_changes(super) == 0) {
3518 info->resync_start = 0;
3519 }
3520 if (dev->vol.migr_state) {
3521 switch (migr_type(dev)) {
3522 case MIGR_REPAIR:
3523 case MIGR_INIT: {
3524 __u64 blocks_per_unit = blocks_per_migr_unit(super,
3525 dev);
3526 __u64 units = vol_curr_migr_unit(dev);
3527
3528 info->resync_start = blocks_per_unit * units;
3529 break;
3530 }
3531 case MIGR_GEN_MIGR: {
3532 __u64 blocks_per_unit = blocks_per_migr_unit(super,
3533 dev);
3534 __u64 units = current_migr_unit(migr_rec);
3535 int used_disks;
3536
3537 if (__le32_to_cpu(migr_rec->ascending_migr) &&
3538 (units <
3539 (get_num_migr_units(migr_rec)-1)) &&
3540 (super->migr_rec->rec_status ==
3541 __cpu_to_le32(UNIT_SRC_IN_CP_AREA)))
3542 units++;
3543
3544 info->reshape_progress = blocks_per_unit * units;
3545
3546 dprintf("IMSM: General Migration checkpoint : %llu (%llu) -> read reshape progress : %llu\n",
3547 (unsigned long long)units,
3548 (unsigned long long)blocks_per_unit,
3549 info->reshape_progress);
3550
3551 used_disks = imsm_num_data_members(prev_map);
3552 if (used_disks > 0) {
3553 info->custom_array_size = per_dev_array_size(map) *
3554 used_disks;
3555 }
3556 }
3557 case MIGR_VERIFY:
3558 /* we could emulate the checkpointing of
3559 * 'sync_action=check' migrations, but for now
3560 * we just immediately complete them
3561 */
3562 case MIGR_REBUILD:
3563 /* this is handled by container_content_imsm() */
3564 case MIGR_STATE_CHANGE:
3565 /* FIXME handle other migrations */
3566 default:
3567 /* we are not dirty, so... */
3568 info->resync_start = MaxSector;
3569 }
3570 }
3571
3572 strncpy(info->name, (char *) dev->volume, MAX_RAID_SERIAL_LEN);
3573 info->name[MAX_RAID_SERIAL_LEN] = 0;
3574
3575 info->array.major_version = -1;
3576 info->array.minor_version = -2;
3577 sprintf(info->text_version, "/%s/%d", st->container_devnm, info->container_member);
3578 info->safe_mode_delay = 4000; /* 4 secs like the Matrix driver */
3579 uuid_from_super_imsm(st, info->uuid);
3580
3581 if (dmap) {
3582 int i, j;
3583 for (i=0; i<map_disks; i++) {
3584 dmap[i] = 0;
3585 if (i < info->array.raid_disks) {
3586 struct imsm_disk *dsk;
3587 j = get_imsm_disk_idx(dev, i, MAP_X);
3588 dsk = get_imsm_disk(super, j);
3589 if (dsk && (dsk->status & CONFIGURED_DISK))
3590 dmap[i] = 1;
3591 }
3592 }
3593 }
3594 }
3595
3596 static __u8 imsm_check_degraded(struct intel_super *super, struct imsm_dev *dev,
3597 int failed, int look_in_map);
3598
3599 static int imsm_count_failed(struct intel_super *super, struct imsm_dev *dev,
3600 int look_in_map);
3601
3602 static void manage_second_map(struct intel_super *super, struct imsm_dev *dev)
3603 {
3604 if (is_gen_migration(dev)) {
3605 int failed;
3606 __u8 map_state;
3607 struct imsm_map *map2 = get_imsm_map(dev, MAP_1);
3608
3609 failed = imsm_count_failed(super, dev, MAP_1);
3610 map_state = imsm_check_degraded(super, dev, failed, MAP_1);
3611 if (map2->map_state != map_state) {
3612 map2->map_state = map_state;
3613 super->updates_pending++;
3614 }
3615 }
3616 }
3617
3618 static struct imsm_disk *get_imsm_missing(struct intel_super *super, __u8 index)
3619 {
3620 struct dl *d;
3621
3622 for (d = super->missing; d; d = d->next)
3623 if (d->index == index)
3624 return &d->disk;
3625 return NULL;
3626 }
3627
3628 static void getinfo_super_imsm(struct supertype *st, struct mdinfo *info, char *map)
3629 {
3630 struct intel_super *super = st->sb;
3631 struct imsm_disk *disk;
3632 int map_disks = info->array.raid_disks;
3633 int max_enough = -1;
3634 int i;
3635 struct imsm_super *mpb;
3636
3637 if (super->current_vol >= 0) {
3638 getinfo_super_imsm_volume(st, info, map);
3639 return;
3640 }
3641 memset(info, 0, sizeof(*info));
3642
3643 /* Set raid_disks to zero so that Assemble will always pull in valid
3644 * spares
3645 */
3646 info->array.raid_disks = 0;
3647 info->array.level = LEVEL_CONTAINER;
3648 info->array.layout = 0;
3649 info->array.md_minor = -1;
3650 info->array.ctime = 0; /* N/A for imsm */
3651 info->array.utime = 0;
3652 info->array.chunk_size = 0;
3653
3654 info->disk.major = 0;
3655 info->disk.minor = 0;
3656 info->disk.raid_disk = -1;
3657 info->reshape_active = 0;
3658 info->array.major_version = -1;
3659 info->array.minor_version = -2;
3660 strcpy(info->text_version, "imsm");
3661 info->safe_mode_delay = 0;
3662 info->disk.number = -1;
3663 info->disk.state = 0;
3664 info->name[0] = 0;
3665 info->recovery_start = MaxSector;
3666 info->recovery_blocked = imsm_reshape_blocks_arrays_changes(st->sb);
3667 info->bb.supported = 1;
3668
3669 /* do we have the all the insync disks that we expect? */
3670 mpb = super->anchor;
3671 info->events = __le32_to_cpu(mpb->generation_num);
3672
3673 for (i = 0; i < mpb->num_raid_devs; i++) {
3674 struct imsm_dev *dev = get_imsm_dev(super, i);
3675 int failed, enough, j, missing = 0;
3676 struct imsm_map *map;
3677 __u8 state;
3678
3679 failed = imsm_count_failed(super, dev, MAP_0);
3680 state = imsm_check_degraded(super, dev, failed, MAP_0);
3681 map = get_imsm_map(dev, MAP_0);
3682
3683 /* any newly missing disks?
3684 * (catches single-degraded vs double-degraded)
3685 */
3686 for (j = 0; j < map->num_members; j++) {
3687 __u32 ord = get_imsm_ord_tbl_ent(dev, j, MAP_0);
3688 __u32 idx = ord_to_idx(ord);
3689
3690 if (super->disks && super->disks->index == (int)idx)
3691 info->disk.raid_disk = j;
3692
3693 if (!(ord & IMSM_ORD_REBUILD) &&
3694 get_imsm_missing(super, idx)) {
3695 missing = 1;
3696 break;
3697 }
3698 }
3699
3700 if (state == IMSM_T_STATE_FAILED)
3701 enough = -1;
3702 else if (state == IMSM_T_STATE_DEGRADED &&
3703 (state != map->map_state || missing))
3704 enough = 0;
3705 else /* we're normal, or already degraded */
3706 enough = 1;
3707 if (is_gen_migration(dev) && missing) {
3708 /* during general migration we need all disks
3709 * that process is running on.
3710 * No new missing disk is allowed.
3711 */
3712 max_enough = -1;
3713 enough = -1;
3714 /* no more checks necessary
3715 */
3716 break;
3717 }
3718 /* in the missing/failed disk case check to see
3719 * if at least one array is runnable
3720 */
3721 max_enough = max(max_enough, enough);
3722 }
3723 dprintf("enough: %d\n", max_enough);
3724 info->container_enough = max_enough;
3725
3726 if (super->disks) {
3727 __u32 reserved = imsm_reserved_sectors(super, super->disks);
3728
3729 disk = &super->disks->disk;
3730 info->data_offset = total_blocks(&super->disks->disk) - reserved;
3731 info->component_size = reserved;
3732 info->disk.state = is_configured(disk) ? (1 << MD_DISK_ACTIVE) : 0;
3733 /* we don't change info->disk.raid_disk here because
3734 * this state will be finalized in mdmon after we have
3735 * found the 'most fresh' version of the metadata
3736 */
3737 info->disk.state |= is_failed(disk) ? (1 << MD_DISK_FAULTY) : 0;
3738 info->disk.state |= (is_spare(disk) || is_journal(disk)) ?
3739 0 : (1 << MD_DISK_SYNC);
3740 }
3741
3742 /* only call uuid_from_super_imsm when this disk is part of a populated container,
3743 * ->compare_super may have updated the 'num_raid_devs' field for spares
3744 */
3745 if (info->disk.state & (1 << MD_DISK_SYNC) || super->anchor->num_raid_devs)
3746 uuid_from_super_imsm(st, info->uuid);
3747 else
3748 memcpy(info->uuid, uuid_zero, sizeof(uuid_zero));
3749
3750 /* I don't know how to compute 'map' on imsm, so use safe default */
3751 if (map) {
3752 int i;
3753 for (i = 0; i < map_disks; i++)
3754 map[i] = 1;
3755 }
3756
3757 }
3758
3759 /* allocates memory and fills disk in mdinfo structure
3760 * for each disk in array */
3761 struct mdinfo *getinfo_super_disks_imsm(struct supertype *st)
3762 {
3763 struct mdinfo *mddev;
3764 struct intel_super *super = st->sb;
3765 struct imsm_disk *disk;
3766 int count = 0;
3767 struct dl *dl;
3768 if (!super || !super->disks)
3769 return NULL;
3770 dl = super->disks;
3771 mddev = xcalloc(1, sizeof(*mddev));
3772 while (dl) {
3773 struct mdinfo *tmp;
3774 disk = &dl->disk;
3775 tmp = xcalloc(1, sizeof(*tmp));
3776 if (mddev->devs)
3777 tmp->next = mddev->devs;
3778 mddev->devs = tmp;
3779 tmp->disk.number = count++;
3780 tmp->disk.major = dl->major;
3781 tmp->disk.minor = dl->minor;
3782 tmp->disk.state = is_configured(disk) ?
3783 (1 << MD_DISK_ACTIVE) : 0;
3784 tmp->disk.state |= is_failed(disk) ? (1 << MD_DISK_FAULTY) : 0;
3785 tmp->disk.state |= is_spare(disk) ? 0 : (1 << MD_DISK_SYNC);
3786 tmp->disk.raid_disk = -1;
3787 dl = dl->next;
3788 }
3789 return mddev;
3790 }
3791
3792 static int update_super_imsm(struct supertype *st, struct mdinfo *info,
3793 char *update, char *devname, int verbose,
3794 int uuid_set, char *homehost)
3795 {
3796 /* For 'assemble' and 'force' we need to return non-zero if any
3797 * change was made. For others, the return value is ignored.
3798 * Update options are:
3799 * force-one : This device looks a bit old but needs to be included,
3800 * update age info appropriately.
3801 * assemble: clear any 'faulty' flag to allow this device to
3802 * be assembled.
3803 * force-array: Array is degraded but being forced, mark it clean
3804 * if that will be needed to assemble it.
3805 *
3806 * newdev: not used ????
3807 * grow: Array has gained a new device - this is currently for
3808 * linear only
3809 * resync: mark as dirty so a resync will happen.
3810 * name: update the name - preserving the homehost
3811 * uuid: Change the uuid of the array to match watch is given
3812 *
3813 * Following are not relevant for this imsm:
3814 * sparc2.2 : update from old dodgey metadata
3815 * super-minor: change the preferred_minor number
3816 * summaries: update redundant counters.
3817 * homehost: update the recorded homehost
3818 * _reshape_progress: record new reshape_progress position.
3819 */
3820 int rv = 1;
3821 struct intel_super *super = st->sb;
3822 struct imsm_super *mpb;
3823
3824 /* we can only update container info */
3825 if (!super || super->current_vol >= 0 || !super->anchor)
3826 return 1;
3827
3828 mpb = super->anchor;
3829
3830 if (strcmp(update, "uuid") == 0) {
3831 /* We take this to mean that the family_num should be updated.
3832 * However that is much smaller than the uuid so we cannot really
3833 * allow an explicit uuid to be given. And it is hard to reliably
3834 * know if one was.
3835 * So if !uuid_set we know the current uuid is random and just used
3836 * the first 'int' and copy it to the other 3 positions.
3837 * Otherwise we require the 4 'int's to be the same as would be the
3838 * case if we are using a random uuid. So an explicit uuid will be
3839 * accepted as long as all for ints are the same... which shouldn't hurt
3840 */
3841 if (!uuid_set) {
3842 info->uuid[1] = info->uuid[2] = info->uuid[3] = info->uuid[0];
3843 rv = 0;
3844 } else {
3845 if (info->uuid[0] != info->uuid[1] ||
3846 info->uuid[1] != info->uuid[2] ||
3847 info->uuid[2] != info->uuid[3])
3848 rv = -1;
3849 else
3850 rv = 0;
3851 }
3852 if (rv == 0)
3853 mpb->orig_family_num = info->uuid[0];
3854 } else if (strcmp(update, "assemble") == 0)
3855 rv = 0;
3856 else
3857 rv = -1;
3858
3859 /* successful update? recompute checksum */
3860 if (rv == 0)
3861 mpb->check_sum = __le32_to_cpu(__gen_imsm_checksum(mpb));
3862
3863 return rv;
3864 }
3865
3866 static size_t disks_to_mpb_size(int disks)
3867 {
3868 size_t size;
3869
3870 size = sizeof(struct imsm_super);
3871 size += (disks - 1) * sizeof(struct imsm_disk);
3872 size += 2 * sizeof(struct imsm_dev);
3873 /* up to 2 maps per raid device (-2 for imsm_maps in imsm_dev */
3874 size += (4 - 2) * sizeof(struct imsm_map);
3875 /* 4 possible disk_ord_tbl's */
3876 size += 4 * (disks - 1) * sizeof(__u32);
3877 /* maximum bbm log */
3878 size += sizeof(struct bbm_log);
3879
3880 return size;
3881 }
3882
3883 static __u64 avail_size_imsm(struct supertype *st, __u64 devsize,
3884 unsigned long long data_offset)
3885 {
3886 if (devsize < (MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS))
3887 return 0;
3888
3889 return devsize - (MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS);
3890 }
3891
3892 static void free_devlist(struct intel_super *super)
3893 {
3894 struct intel_dev *dv;
3895
3896 while (super->devlist) {
3897 dv = super->devlist->next;
3898 free(super->devlist->dev);
3899 free(super->devlist);
3900 super->devlist = dv;
3901 }
3902 }
3903
3904 static void imsm_copy_dev(struct imsm_dev *dest, struct imsm_dev *src)
3905 {
3906 memcpy(dest, src, sizeof_imsm_dev(src, 0));
3907 }
3908
3909 static int compare_super_imsm(struct supertype *st, struct supertype *tst,
3910 int verbose)
3911 {
3912 /*
3913 * return:
3914 * 0 same, or first was empty, and second was copied
3915 * 1 second had wrong number
3916 * 2 wrong uuid
3917 * 3 wrong other info
3918 */
3919 struct intel_super *first = st->sb;
3920 struct intel_super *sec = tst->sb;
3921
3922 if (!first) {
3923 st->sb = tst->sb;
3924 tst->sb = NULL;
3925 return 0;
3926 }
3927 /* in platform dependent environment test if the disks
3928 * use the same Intel hba
3929 * If not on Intel hba at all, allow anything.
3930 */
3931 if (!check_env("IMSM_NO_PLATFORM") && first->hba && sec->hba) {
3932 if (first->hba->type != sec->hba->type) {
3933 if (verbose)
3934 pr_err("HBAs of devices do not match %s != %s\n",
3935 get_sys_dev_type(first->hba->type),
3936 get_sys_dev_type(sec->hba->type));
3937 return 3;
3938 }
3939
3940 if (first->orom != sec->orom) {
3941 if (verbose)
3942 pr_err("HBAs of devices do not match %s != %s\n",
3943 first->hba->pci_id, sec->hba->pci_id);
3944 return 3;
3945 }
3946
3947 }
3948
3949 /* if an anchor does not have num_raid_devs set then it is a free
3950 * floating spare
3951 */
3952 if (first->anchor->num_raid_devs > 0 &&
3953 sec->anchor->num_raid_devs > 0) {
3954 /* Determine if these disks might ever have been
3955 * related. Further disambiguation can only take place
3956 * in load_super_imsm_all
3957 */
3958 __u32 first_family = first->anchor->orig_family_num;
3959 __u32 sec_family = sec->anchor->orig_family_num;
3960
3961 if (memcmp(first->anchor->sig, sec->anchor->sig,
3962 MAX_SIGNATURE_LENGTH) != 0)
3963 return 3;
3964
3965 if (first_family == 0)
3966 first_family = first->anchor->family_num;
3967 if (sec_family == 0)
3968 sec_family = sec->anchor->family_num;
3969
3970 if (first_family != sec_family)
3971 return 3;
3972
3973 }
3974
3975 /* if 'first' is a spare promote it to a populated mpb with sec's
3976 * family number
3977 */
3978 if (first->anchor->num_raid_devs == 0 &&
3979 sec->anchor->num_raid_devs > 0) {
3980 int i;
3981 struct intel_dev *dv;
3982 struct imsm_dev *dev;
3983
3984 /* we need to copy raid device info from sec if an allocation
3985 * fails here we don't associate the spare
3986 */
3987 for (i = 0; i < sec->anchor->num_raid_devs; i++) {
3988 dv = xmalloc(sizeof(*dv));
3989 dev = xmalloc(sizeof_imsm_dev(get_imsm_dev(sec, i), 1));
3990 dv->dev = dev;
3991 dv->index = i;
3992 dv->next = first->devlist;
3993 first->devlist = dv;
3994 }
3995 if (i < sec->anchor->num_raid_devs) {
3996 /* allocation failure */
3997 free_devlist(first);
3998 pr_err("imsm: failed to associate spare\n");
3999 return 3;
4000 }
4001 first->anchor->num_raid_devs = sec->anchor->num_raid_devs;
4002 first->anchor->orig_family_num = sec->anchor->orig_family_num;
4003 first->anchor->family_num = sec->anchor->family_num;
4004 memcpy(first->anchor->sig, sec->anchor->sig, MAX_SIGNATURE_LENGTH);
4005 for (i = 0; i < sec->anchor->num_raid_devs; i++)
4006 imsm_copy_dev(get_imsm_dev(first, i), get_imsm_dev(sec, i));
4007 }
4008
4009 return 0;
4010 }
4011
4012 static void fd2devname(int fd, char *name)
4013 {
4014 struct stat st;
4015 char path[256];
4016 char dname[PATH_MAX];
4017 char *nm;
4018 int rv;
4019
4020 name[0] = '\0';
4021 if (fstat(fd, &st) != 0)
4022 return;
4023 sprintf(path, "/sys/dev/block/%d:%d",
4024 major(st.st_rdev), minor(st.st_rdev));
4025
4026 rv = readlink(path, dname, sizeof(dname)-1);
4027 if (rv <= 0)
4028 return;
4029
4030 dname[rv] = '\0';
4031 nm = strrchr(dname, '/');
4032 if (nm) {
4033 nm++;
4034 snprintf(name, MAX_RAID_SERIAL_LEN, "/dev/%s", nm);
4035 }
4036 }
4037
4038 static int nvme_get_serial(int fd, void *buf, size_t buf_len)
4039 {
4040 char path[60];
4041 char *name = fd2kname(fd);
4042
4043 if (!name)
4044 return 1;
4045
4046 if (strncmp(name, "nvme", 4) != 0)
4047 return 1;
4048
4049 snprintf(path, sizeof(path) - 1, "/sys/block/%s/device/serial", name);
4050
4051 return load_sys(path, buf, buf_len);
4052 }
4053
4054 extern int scsi_get_serial(int fd, void *buf, size_t buf_len);
4055
4056 static int imsm_read_serial(int fd, char *devname,
4057 __u8 *serial, size_t serial_buf_len)
4058 {
4059 char buf[50];
4060 int rv;
4061 size_t len;
4062 char *dest;
4063 char *src;
4064 unsigned int i;
4065
4066 memset(buf, 0, sizeof(buf));
4067
4068 rv = nvme_get_serial(fd, buf, sizeof(buf));
4069
4070 if (rv)
4071 rv = scsi_get_serial(fd, buf, sizeof(buf));
4072
4073 if (rv && check_env("IMSM_DEVNAME_AS_SERIAL")) {
4074 memset(serial, 0, MAX_RAID_SERIAL_LEN);
4075 fd2devname(fd, (char *) serial);
4076 return 0;
4077 }
4078
4079 if (rv != 0) {
4080 if (devname)
4081 pr_err("Failed to retrieve serial for %s\n",
4082 devname);
4083 return rv;
4084 }
4085
4086 /* trim all whitespace and non-printable characters and convert
4087 * ':' to ';'
4088 */
4089 for (i = 0, dest = buf; i < sizeof(buf) && buf[i]; i++) {
4090 src = &buf[i];
4091 if (*src > 0x20) {
4092 /* ':' is reserved for use in placeholder serial
4093 * numbers for missing disks
4094 */
4095 if (*src == ':')
4096 *dest++ = ';';
4097 else
4098 *dest++ = *src;
4099 }
4100 }
4101 len = dest - buf;
4102 dest = buf;
4103
4104 if (len > serial_buf_len) {
4105 /* truncate leading characters */
4106 dest += len - serial_buf_len;
4107 len = serial_buf_len;
4108 }
4109
4110 memset(serial, 0, serial_buf_len);
4111 memcpy(serial, dest, len);
4112
4113 return 0;
4114 }
4115
4116 static int serialcmp(__u8 *s1, __u8 *s2)
4117 {
4118 return strncmp((char *) s1, (char *) s2, MAX_RAID_SERIAL_LEN);
4119 }
4120
4121 static void serialcpy(__u8 *dest, __u8 *src)
4122 {
4123 strncpy((char *) dest, (char *) src, MAX_RAID_SERIAL_LEN);
4124 }
4125
4126 static struct dl *serial_to_dl(__u8 *serial, struct intel_super *super)
4127 {
4128 struct dl *dl;
4129
4130 for (dl = super->disks; dl; dl = dl->next)
4131 if (serialcmp(dl->serial, serial) == 0)
4132 break;
4133
4134 return dl;
4135 }
4136
4137 static struct imsm_disk *
4138 __serial_to_disk(__u8 *serial, struct imsm_super *mpb, int *idx)
4139 {
4140 int i;
4141
4142 for (i = 0; i < mpb->num_disks; i++) {
4143 struct imsm_disk *disk = __get_imsm_disk(mpb, i);
4144
4145 if (serialcmp(disk->serial, serial) == 0) {
4146 if (idx)
4147 *idx = i;
4148 return disk;
4149 }
4150 }
4151
4152 return NULL;
4153 }
4154
4155 static int
4156 load_imsm_disk(int fd, struct intel_super *super, char *devname, int keep_fd)
4157 {
4158 struct imsm_disk *disk;
4159 struct dl *dl;
4160 struct stat stb;
4161 int rv;
4162 char name[40];
4163 __u8 serial[MAX_RAID_SERIAL_LEN];
4164
4165 rv = imsm_read_serial(fd, devname, serial, MAX_RAID_SERIAL_LEN);
4166
4167 if (rv != 0)
4168 return 2;
4169
4170 dl = xcalloc(1, sizeof(*dl));
4171
4172 fstat(fd, &stb);
4173 dl->major = major(stb.st_rdev);
4174 dl->minor = minor(stb.st_rdev);
4175 dl->next = super->disks;
4176 dl->fd = keep_fd ? fd : -1;
4177 assert(super->disks == NULL);
4178 super->disks = dl;
4179 serialcpy(dl->serial, serial);
4180 dl->index = -2;
4181 dl->e = NULL;
4182 fd2devname(fd, name);
4183 if (devname)
4184 dl->devname = xstrdup(devname);
4185 else
4186 dl->devname = xstrdup(name);
4187
4188 /* look up this disk's index in the current anchor */
4189 disk = __serial_to_disk(dl->serial, super->anchor, &dl->index);
4190 if (disk) {
4191 dl->disk = *disk;
4192 /* only set index on disks that are a member of a
4193 * populated contianer, i.e. one with raid_devs
4194 */
4195 if (is_failed(&dl->disk))
4196 dl->index = -2;
4197 else if (is_spare(&dl->disk) || is_journal(&dl->disk))
4198 dl->index = -1;
4199 }
4200
4201 return 0;
4202 }
4203
4204 /* When migrating map0 contains the 'destination' state while map1
4205 * contains the current state. When not migrating map0 contains the
4206 * current state. This routine assumes that map[0].map_state is set to
4207 * the current array state before being called.
4208 *
4209 * Migration is indicated by one of the following states
4210 * 1/ Idle (migr_state=0 map0state=normal||unitialized||degraded||failed)
4211 * 2/ Initialize (migr_state=1 migr_type=MIGR_INIT map0state=normal
4212 * map1state=unitialized)
4213 * 3/ Repair (Resync) (migr_state=1 migr_type=MIGR_REPAIR map0state=normal
4214 * map1state=normal)
4215 * 4/ Rebuild (migr_state=1 migr_type=MIGR_REBUILD map0state=normal
4216 * map1state=degraded)
4217 * 5/ Migration (mig_state=1 migr_type=MIGR_GEN_MIGR map0state=normal
4218 * map1state=normal)
4219 */
4220 static void migrate(struct imsm_dev *dev, struct intel_super *super,
4221 __u8 to_state, int migr_type)
4222 {
4223 struct imsm_map *dest;
4224 struct imsm_map *src = get_imsm_map(dev, MAP_0);
4225
4226 dev->vol.migr_state = 1;
4227 set_migr_type(dev, migr_type);
4228 set_vol_curr_migr_unit(dev, 0);
4229 dest = get_imsm_map(dev, MAP_1);
4230
4231 /* duplicate and then set the target end state in map[0] */
4232 memcpy(dest, src, sizeof_imsm_map(src));
4233 if (migr_type == MIGR_GEN_MIGR) {
4234 __u32 ord;
4235 int i;
4236
4237 for (i = 0; i < src->num_members; i++) {
4238 ord = __le32_to_cpu(src->disk_ord_tbl[i]);
4239 set_imsm_ord_tbl_ent(src, i, ord_to_idx(ord));
4240 }
4241 }
4242
4243 if (migr_type == MIGR_GEN_MIGR)
4244 /* Clear migration record */
4245 memset(super->migr_rec, 0, sizeof(struct migr_record));
4246
4247 src->map_state = to_state;
4248 }
4249
4250 static void end_migration(struct imsm_dev *dev, struct intel_super *super,
4251 __u8 map_state)
4252 {
4253 struct imsm_map *map = get_imsm_map(dev, MAP_0);
4254 struct imsm_map *prev = get_imsm_map(dev, dev->vol.migr_state == 0 ?
4255 MAP_0 : MAP_1);
4256 int i, j;
4257
4258 /* merge any IMSM_ORD_REBUILD bits that were not successfully
4259 * completed in the last migration.
4260 *
4261 * FIXME add support for raid-level-migration
4262 */
4263 if (map_state != map->map_state && (is_gen_migration(dev) == 0) &&
4264 prev->map_state != IMSM_T_STATE_UNINITIALIZED) {
4265 /* when final map state is other than expected
4266 * merge maps (not for migration)
4267 */
4268 int failed;
4269
4270 for (i = 0; i < prev->num_members; i++)
4271 for (j = 0; j < map->num_members; j++)
4272 /* during online capacity expansion
4273 * disks position can be changed
4274 * if takeover is used
4275 */
4276 if (ord_to_idx(map->disk_ord_tbl[j]) ==
4277 ord_to_idx(prev->disk_ord_tbl[i])) {
4278 map->disk_ord_tbl[j] |=
4279 prev->disk_ord_tbl[i];
4280 break;
4281 }
4282 failed = imsm_count_failed(super, dev, MAP_0);
4283 map_state = imsm_check_degraded(super, dev, failed, MAP_0);
4284 }
4285
4286 dev->vol.migr_state = 0;
4287 set_migr_type(dev, 0);
4288 set_vol_curr_migr_unit(dev, 0);
4289 map->map_state = map_state;
4290 }
4291
4292 static int parse_raid_devices(struct intel_super *super)
4293 {
4294 int i;
4295 struct imsm_dev *dev_new;
4296 size_t len, len_migr;
4297 size_t max_len = 0;
4298 size_t space_needed = 0;
4299 struct imsm_super *mpb = super->anchor;
4300
4301 for (i = 0; i < super->anchor->num_raid_devs; i++) {
4302 struct imsm_dev *dev_iter = __get_imsm_dev(super->anchor, i);
4303 struct intel_dev *dv;
4304
4305 len = sizeof_imsm_dev(dev_iter, 0);
4306 len_migr = sizeof_imsm_dev(dev_iter, 1);
4307 if (len_migr > len)
4308 space_needed += len_migr - len;
4309
4310 dv = xmalloc(sizeof(*dv));
4311 if (max_len < len_migr)
4312 max_len = len_migr;
4313 if (max_len > len_migr)
4314 space_needed += max_len - len_migr;
4315 dev_new = xmalloc(max_len);
4316 imsm_copy_dev(dev_new, dev_iter);
4317 dv->dev = dev_new;
4318 dv->index = i;
4319 dv->next = super->devlist;
4320 super->devlist = dv;
4321 }
4322
4323 /* ensure that super->buf is large enough when all raid devices
4324 * are migrating
4325 */
4326 if (__le32_to_cpu(mpb->mpb_size) + space_needed > super->len) {
4327 void *buf;
4328
4329 len = ROUND_UP(__le32_to_cpu(mpb->mpb_size) + space_needed,
4330 super->sector_size);
4331 if (posix_memalign(&buf, MAX_SECTOR_SIZE, len) != 0)
4332 return 1;
4333
4334 memcpy(buf, super->buf, super->len);
4335 memset(buf + super->len, 0, len - super->len);
4336 free(super->buf);
4337 super->buf = buf;
4338 super->len = len;
4339 }
4340
4341 super->extra_space += space_needed;
4342
4343 return 0;
4344 }
4345
4346 /*******************************************************************************
4347 * Function: check_mpb_migr_compatibility
4348 * Description: Function checks for unsupported migration features:
4349 * - migration optimization area (pba_of_lba0)
4350 * - descending reshape (ascending_migr)
4351 * Parameters:
4352 * super : imsm metadata information
4353 * Returns:
4354 * 0 : migration is compatible
4355 * -1 : migration is not compatible
4356 ******************************************************************************/
4357 int check_mpb_migr_compatibility(struct intel_super *super)
4358 {
4359 struct imsm_map *map0, *map1;
4360 struct migr_record *migr_rec = super->migr_rec;
4361 int i;
4362
4363 for (i = 0; i < super->anchor->num_raid_devs; i++) {
4364 struct imsm_dev *dev_iter = __get_imsm_dev(super->anchor, i);
4365
4366 if (dev_iter &&
4367 dev_iter->vol.migr_state == 1 &&
4368 dev_iter->vol.migr_type == MIGR_GEN_MIGR) {
4369 /* This device is migrating */
4370 map0 = get_imsm_map(dev_iter, MAP_0);
4371 map1 = get_imsm_map(dev_iter, MAP_1);
4372 if (pba_of_lba0(map0) != pba_of_lba0(map1))
4373 /* migration optimization area was used */
4374 return -1;
4375 if (migr_rec->ascending_migr == 0 &&
4376 migr_rec->dest_depth_per_unit > 0)
4377 /* descending reshape not supported yet */
4378 return -1;
4379 }
4380 }
4381 return 0;
4382 }
4383
4384 static void __free_imsm(struct intel_super *super, int free_disks);
4385
4386 /* load_imsm_mpb - read matrix metadata
4387 * allocates super->mpb to be freed by free_imsm
4388 */
4389 static int load_imsm_mpb(int fd, struct intel_super *super, char *devname)
4390 {
4391 unsigned long long dsize;
4392 unsigned long long sectors;
4393 unsigned int sector_size = super->sector_size;
4394 struct stat;
4395 struct imsm_super *anchor;
4396 __u32 check_sum;
4397
4398 get_dev_size(fd, NULL, &dsize);
4399 if (dsize < 2*sector_size) {
4400 if (devname)
4401 pr_err("%s: device to small for imsm\n",
4402 devname);
4403 return 1;
4404 }
4405
4406 if (lseek64(fd, dsize - (sector_size * 2), SEEK_SET) < 0) {
4407 if (devname)
4408 pr_err("Cannot seek to anchor block on %s: %s\n",
4409 devname, strerror(errno));
4410 return 1;
4411 }
4412
4413 if (posix_memalign((void **)&anchor, sector_size, sector_size) != 0) {
4414 if (devname)
4415 pr_err("Failed to allocate imsm anchor buffer on %s\n", devname);
4416 return 1;
4417 }
4418 if ((unsigned int)read(fd, anchor, sector_size) != sector_size) {
4419 if (devname)
4420 pr_err("Cannot read anchor block on %s: %s\n",
4421 devname, strerror(errno));
4422 free(anchor);
4423 return 1;
4424 }
4425
4426 if (strncmp((char *) anchor->sig, MPB_SIGNATURE, MPB_SIG_LEN) != 0) {
4427 if (devname)
4428 pr_err("no IMSM anchor on %s\n", devname);
4429 free(anchor);
4430 return 2;
4431 }
4432
4433 __free_imsm(super, 0);
4434 /* reload capability and hba */
4435
4436 /* capability and hba must be updated with new super allocation */
4437 find_intel_hba_capability(fd, super, devname);
4438 super->len = ROUND_UP(anchor->mpb_size, sector_size);
4439 if (posix_memalign(&super->buf, MAX_SECTOR_SIZE, super->len) != 0) {
4440 if (devname)
4441 pr_err("unable to allocate %zu byte mpb buffer\n",
4442 super->len);
4443 free(anchor);
4444 return 2;
4445 }
4446 memcpy(super->buf, anchor, sector_size);
4447
4448 sectors = mpb_sectors(anchor, sector_size) - 1;
4449 free(anchor);
4450
4451 if (posix_memalign(&super->migr_rec_buf, MAX_SECTOR_SIZE,
4452 MIGR_REC_BUF_SECTORS*MAX_SECTOR_SIZE) != 0) {
4453 pr_err("could not allocate migr_rec buffer\n");
4454 free(super->buf);
4455 return 2;
4456 }
4457 super->clean_migration_record_by_mdmon = 0;
4458
4459 if (!sectors) {
4460 check_sum = __gen_imsm_checksum(super->anchor);
4461 if (check_sum != __le32_to_cpu(super->anchor->check_sum)) {
4462 if (devname)
4463 pr_err("IMSM checksum %x != %x on %s\n",
4464 check_sum,
4465 __le32_to_cpu(super->anchor->check_sum),
4466 devname);
4467 return 2;
4468 }
4469
4470 return 0;
4471 }
4472
4473 /* read the extended mpb */
4474 if (lseek64(fd, dsize - (sector_size * (2 + sectors)), SEEK_SET) < 0) {
4475 if (devname)
4476 pr_err("Cannot seek to extended mpb on %s: %s\n",
4477 devname, strerror(errno));
4478 return 1;
4479 }
4480
4481 if ((unsigned int)read(fd, super->buf + sector_size,
4482 super->len - sector_size) != super->len - sector_size) {
4483 if (devname)
4484 pr_err("Cannot read extended mpb on %s: %s\n",
4485 devname, strerror(errno));
4486 return 2;
4487 }
4488
4489 check_sum = __gen_imsm_checksum(super->anchor);
4490 if (check_sum != __le32_to_cpu(super->anchor->check_sum)) {
4491 if (devname)
4492 pr_err("IMSM checksum %x != %x on %s\n",
4493 check_sum, __le32_to_cpu(super->anchor->check_sum),
4494 devname);
4495 return 3;
4496 }
4497
4498 return 0;
4499 }
4500
4501 static int read_imsm_migr_rec(int fd, struct intel_super *super);
4502
4503 /* clears hi bits in metadata if MPB_ATTRIB_2TB_DISK not set */
4504 static void clear_hi(struct intel_super *super)
4505 {
4506 struct imsm_super *mpb = super->anchor;
4507 int i, n;
4508 if (mpb->attributes & MPB_ATTRIB_2TB_DISK)
4509 return;
4510 for (i = 0; i < mpb->num_disks; ++i) {
4511 struct imsm_disk *disk = &mpb->disk[i];
4512 disk->total_blocks_hi = 0;
4513 }
4514 for (i = 0; i < mpb->num_raid_devs; ++i) {
4515 struct imsm_dev *dev = get_imsm_dev(super, i);
4516 if (!dev)
4517 return;
4518 for (n = 0; n < 2; ++n) {
4519 struct imsm_map *map = get_imsm_map(dev, n);
4520 if (!map)
4521 continue;
4522 map->pba_of_lba0_hi = 0;
4523 map->blocks_per_member_hi = 0;
4524 map->num_data_stripes_hi = 0;
4525 }
4526 }
4527 }
4528
4529 static int
4530 load_and_parse_mpb(int fd, struct intel_super *super, char *devname, int keep_fd)
4531 {
4532 int err;
4533
4534 err = load_imsm_mpb(fd, super, devname);
4535 if (err)
4536 return err;
4537 if (super->sector_size == 4096)
4538 convert_from_4k(super);
4539 err = load_imsm_disk(fd, super, devname, keep_fd);
4540 if (err)
4541 return err;
4542 err = parse_raid_devices(super);
4543 if (err)
4544 return err;
4545 err = load_bbm_log(super);
4546 clear_hi(super);
4547 return err;
4548 }
4549
4550 static void __free_imsm_disk(struct dl *d)
4551 {
4552 if (d->fd >= 0)
4553 close(d->fd);
4554 if (d->devname)
4555 free(d->devname);
4556 if (d->e)
4557 free(d->e);
4558 free(d);
4559
4560 }
4561
4562 static void free_imsm_disks(struct intel_super *super)
4563 {
4564 struct dl *d;
4565
4566 while (super->disks) {
4567 d = super->disks;
4568 super->disks = d->next;
4569 __free_imsm_disk(d);
4570 }
4571 while (super->disk_mgmt_list) {
4572 d = super->disk_mgmt_list;
4573 super->disk_mgmt_list = d->next;
4574 __free_imsm_disk(d);
4575 }
4576 while (super->missing) {
4577 d = super->missing;
4578 super->missing = d->next;
4579 __free_imsm_disk(d);
4580 }
4581
4582 }
4583
4584 /* free all the pieces hanging off of a super pointer */
4585 static void __free_imsm(struct intel_super *super, int free_disks)
4586 {
4587 struct intel_hba *elem, *next;
4588
4589 if (super->buf) {
4590 free(super->buf);
4591 super->buf = NULL;
4592 }
4593 /* unlink capability description */
4594 super->orom = NULL;
4595 if (super->migr_rec_buf) {
4596 free(super->migr_rec_buf);
4597 super->migr_rec_buf = NULL;
4598 }
4599 if (free_disks)
4600 free_imsm_disks(super);
4601 free_devlist(super);
4602 elem = super->hba;
4603 while (elem) {
4604 if (elem->path)
4605 free((void *)elem->path);
4606 next = elem->next;
4607 free(elem);
4608 elem = next;
4609 }
4610 if (super->bbm_log)
4611 free(super->bbm_log);
4612 super->hba = NULL;
4613 }
4614
4615 static void free_imsm(struct intel_super *super)
4616 {
4617 __free_imsm(super, 1);
4618 free(super->bb.entries);
4619 free(super);
4620 }
4621
4622 static void free_super_imsm(struct supertype *st)
4623 {
4624 struct intel_super *super = st->sb;
4625
4626 if (!super)
4627 return;
4628
4629 free_imsm(super);
4630 st->sb = NULL;
4631 }
4632
4633 static struct intel_super *alloc_super(void)
4634 {
4635 struct intel_super *super = xcalloc(1, sizeof(*super));
4636
4637 super->current_vol = -1;
4638 super->create_offset = ~((unsigned long long) 0);
4639
4640 super->bb.entries = xmalloc(BBM_LOG_MAX_ENTRIES *
4641 sizeof(struct md_bb_entry));
4642 if (!super->bb.entries) {
4643 free(super);
4644 return NULL;
4645 }
4646
4647 return super;
4648 }
4649
4650 /*
4651 * find and allocate hba and OROM/EFI based on valid fd of RAID component device
4652 */
4653 static int find_intel_hba_capability(int fd, struct intel_super *super, char *devname)
4654 {
4655 struct sys_dev *hba_name;
4656 int rv = 0;
4657
4658 if (fd >= 0 && test_partition(fd)) {
4659 pr_err("imsm: %s is a partition, cannot be used in IMSM\n",
4660 devname);
4661 return 1;
4662 }
4663 if (fd < 0 || check_env("IMSM_NO_PLATFORM")) {
4664 super->orom = NULL;
4665 super->hba = NULL;
4666 return 0;
4667 }
4668 hba_name = find_disk_attached_hba(fd, NULL);
4669 if (!hba_name) {
4670 if (devname)
4671 pr_err("%s is not attached to Intel(R) RAID controller.\n",
4672 devname);
4673 return 1;
4674 }
4675 rv = attach_hba_to_super(super, hba_name);
4676 if (rv == 2) {
4677 if (devname) {
4678 struct intel_hba *hba = super->hba;
4679
4680 pr_err("%s is attached to Intel(R) %s %s (%s),\n"
4681 " but the container is assigned to Intel(R) %s %s (",
4682 devname,
4683 get_sys_dev_type(hba_name->type),
4684 hba_name->type == SYS_DEV_VMD ? "domain" : "RAID controller",
4685 hba_name->pci_id ? : "Err!",
4686 get_sys_dev_type(super->hba->type),
4687 hba->type == SYS_DEV_VMD ? "domain" : "RAID controller");
4688
4689 while (hba) {
4690 fprintf(stderr, "%s", hba->pci_id ? : "Err!");
4691 if (hba->next)
4692 fprintf(stderr, ", ");
4693 hba = hba->next;
4694 }
4695 fprintf(stderr, ").\n"
4696 " Mixing devices attached to different controllers is not allowed.\n");
4697 }
4698 return 2;
4699 }
4700 super->orom = find_imsm_capability(hba_name);
4701 if (!super->orom)
4702 return 3;
4703
4704 return 0;
4705 }
4706
4707 /* find_missing - helper routine for load_super_imsm_all that identifies
4708 * disks that have disappeared from the system. This routine relies on
4709 * the mpb being uptodate, which it is at load time.
4710 */
4711 static int find_missing(struct intel_super *super)
4712 {
4713 int i;
4714 struct imsm_super *mpb = super->anchor;
4715 struct dl *dl;
4716 struct imsm_disk *disk;
4717
4718 for (i = 0; i < mpb->num_disks; i++) {
4719 disk = __get_imsm_disk(mpb, i);
4720 dl = serial_to_dl(disk->serial, super);
4721 if (dl)
4722 continue;
4723
4724 dl = xmalloc(sizeof(*dl));
4725 dl->major = 0;
4726 dl->minor = 0;
4727 dl->fd = -1;
4728 dl->devname = xstrdup("missing");
4729 dl->index = i;
4730 serialcpy(dl->serial, disk->serial);
4731 dl->disk = *disk;
4732 dl->e = NULL;
4733 dl->next = super->missing;
4734 super->missing = dl;
4735 }
4736
4737 return 0;
4738 }
4739
4740 static struct intel_disk *disk_list_get(__u8 *serial, struct intel_disk *disk_list)
4741 {
4742 struct intel_disk *idisk = disk_list;
4743
4744 while (idisk) {
4745 if (serialcmp(idisk->disk.serial, serial) == 0)
4746 break;
4747 idisk = idisk->next;
4748 }
4749
4750 return idisk;
4751 }
4752
4753 static int __prep_thunderdome(struct intel_super **table, int tbl_size,
4754 struct intel_super *super,
4755 struct intel_disk **disk_list)
4756 {
4757 struct imsm_disk *d = &super->disks->disk;
4758 struct imsm_super *mpb = super->anchor;
4759 int i, j;
4760
4761 for (i = 0; i < tbl_size; i++) {
4762 struct imsm_super *tbl_mpb = table[i]->anchor;
4763 struct imsm_disk *tbl_d = &table[i]->disks->disk;
4764
4765 if (tbl_mpb->family_num == mpb->family_num) {
4766 if (tbl_mpb->check_sum == mpb->check_sum) {
4767 dprintf("mpb from %d:%d matches %d:%d\n",
4768 super->disks->major,
4769 super->disks->minor,
4770 table[i]->disks->major,
4771 table[i]->disks->minor);
4772 break;
4773 }
4774
4775 if (((is_configured(d) && !is_configured(tbl_d)) ||
4776 is_configured(d) == is_configured(tbl_d)) &&
4777 tbl_mpb->generation_num < mpb->generation_num) {
4778 /* current version of the mpb is a
4779 * better candidate than the one in
4780 * super_table, but copy over "cross
4781 * generational" status
4782 */
4783 struct intel_disk *idisk;
4784
4785 dprintf("mpb from %d:%d replaces %d:%d\n",
4786 super->disks->major,
4787 super->disks->minor,
4788 table[i]->disks->major,
4789 table[i]->disks->minor);
4790
4791 idisk = disk_list_get(tbl_d->serial, *disk_list);
4792 if (idisk && is_failed(&idisk->disk))
4793 tbl_d->status |= FAILED_DISK;
4794 break;
4795 } else {
4796 struct intel_disk *idisk;
4797 struct imsm_disk *disk;
4798
4799 /* tbl_mpb is more up to date, but copy
4800 * over cross generational status before
4801 * returning
4802 */
4803 disk = __serial_to_disk(d->serial, mpb, NULL);
4804 if (disk && is_failed(disk))
4805 d->status |= FAILED_DISK;
4806
4807 idisk = disk_list_get(d->serial, *disk_list);
4808 if (idisk) {
4809 idisk->owner = i;
4810 if (disk && is_configured(disk))
4811 idisk->disk.status |= CONFIGURED_DISK;
4812 }
4813
4814 dprintf("mpb from %d:%d prefer %d:%d\n",
4815 super->disks->major,
4816 super->disks->minor,
4817 table[i]->disks->major,
4818 table[i]->disks->minor);
4819
4820 return tbl_size;
4821 }
4822 }
4823 }
4824
4825 if (i >= tbl_size)
4826 table[tbl_size++] = super;
4827 else
4828 table[i] = super;
4829
4830 /* update/extend the merged list of imsm_disk records */
4831 for (j = 0; j < mpb->num_disks; j++) {
4832 struct imsm_disk *disk = __get_imsm_disk(mpb, j);
4833 struct intel_disk *idisk;
4834
4835 idisk = disk_list_get(disk->serial, *disk_list);
4836 if (idisk) {
4837 idisk->disk.status |= disk->status;
4838 if (is_configured(&idisk->disk) ||
4839 is_failed(&idisk->disk))
4840 idisk->disk.status &= ~(SPARE_DISK);
4841 } else {
4842 idisk = xcalloc(1, sizeof(*idisk));
4843 idisk->owner = IMSM_UNKNOWN_OWNER;
4844 idisk->disk = *disk;
4845 idisk->next = *disk_list;
4846 *disk_list = idisk;
4847 }
4848
4849 if (serialcmp(idisk->disk.serial, d->serial) == 0)
4850 idisk->owner = i;
4851 }
4852
4853 return tbl_size;
4854 }
4855
4856 static struct intel_super *
4857 validate_members(struct intel_super *super, struct intel_disk *disk_list,
4858 const int owner)
4859 {
4860 struct imsm_super *mpb = super->anchor;
4861 int ok_count = 0;
4862 int i;
4863
4864 for (i = 0; i < mpb->num_disks; i++) {
4865 struct imsm_disk *disk = __get_imsm_disk(mpb, i);
4866 struct intel_disk *idisk;
4867
4868 idisk = disk_list_get(disk->serial, disk_list);
4869 if (idisk) {
4870 if (idisk->owner == owner ||
4871 idisk->owner == IMSM_UNKNOWN_OWNER)
4872 ok_count++;
4873 else
4874 dprintf("'%.16s' owner %d != %d\n",
4875 disk->serial, idisk->owner,
4876 owner);
4877 } else {
4878 dprintf("unknown disk %x [%d]: %.16s\n",
4879 __le32_to_cpu(mpb->family_num), i,
4880 disk->serial);
4881 break;
4882 }
4883 }
4884
4885 if (ok_count == mpb->num_disks)
4886 return super;
4887 return NULL;
4888 }
4889
4890 static void show_conflicts(__u32 family_num, struct intel_super *super_list)
4891 {
4892 struct intel_super *s;
4893
4894 for (s = super_list; s; s = s->next) {
4895 if (family_num != s->anchor->family_num)
4896 continue;
4897 pr_err("Conflict, offlining family %#x on '%s'\n",
4898 __le32_to_cpu(family_num), s->disks->devname);
4899 }
4900 }
4901
4902 static struct intel_super *
4903 imsm_thunderdome(struct intel_super **super_list, int len)
4904 {
4905 struct intel_super *super_table[len];
4906 struct intel_disk *disk_list = NULL;
4907 struct intel_super *champion, *spare;
4908 struct intel_super *s, **del;
4909 int tbl_size = 0;
4910 int conflict;
4911 int i;
4912
4913 memset(super_table, 0, sizeof(super_table));
4914 for (s = *super_list; s; s = s->next)
4915 tbl_size = __prep_thunderdome(super_table, tbl_size, s, &disk_list);
4916
4917 for (i = 0; i < tbl_size; i++) {
4918 struct imsm_disk *d;
4919 struct intel_disk *idisk;
4920 struct imsm_super *mpb = super_table[i]->anchor;
4921
4922 s = super_table[i];
4923 d = &s->disks->disk;
4924
4925 /* 'd' must appear in merged disk list for its
4926 * configuration to be valid
4927 */
4928 idisk = disk_list_get(d->serial, disk_list);
4929 if (idisk && idisk->owner == i)
4930 s = validate_members(s, disk_list, i);
4931 else
4932 s = NULL;
4933
4934 if (!s)
4935 dprintf("marking family: %#x from %d:%d offline\n",
4936 mpb->family_num,
4937 super_table[i]->disks->major,
4938 super_table[i]->disks->minor);
4939 super_table[i] = s;
4940 }
4941
4942 /* This is where the mdadm implementation differs from the Windows
4943 * driver which has no strict concept of a container. We can only
4944 * assemble one family from a container, so when returning a prodigal
4945 * array member to this system the code will not be able to disambiguate
4946 * the container contents that should be assembled ("foreign" versus
4947 * "local"). It requires user intervention to set the orig_family_num
4948 * to a new value to establish a new container. The Windows driver in
4949 * this situation fixes up the volume name in place and manages the
4950 * foreign array as an independent entity.
4951 */
4952 s = NULL;
4953 spare = NULL;
4954 conflict = 0;
4955 for (i = 0; i < tbl_size; i++) {
4956 struct intel_super *tbl_ent = super_table[i];
4957 int is_spare = 0;
4958
4959 if (!tbl_ent)
4960 continue;
4961
4962 if (tbl_ent->anchor->num_raid_devs == 0) {
4963 spare = tbl_ent;
4964 is_spare = 1;
4965 }
4966
4967 if (s && !is_spare) {
4968 show_conflicts(tbl_ent->anchor->family_num, *super_list);
4969 conflict++;
4970 } else if (!s && !is_spare)
4971 s = tbl_ent;
4972 }
4973
4974 if (!s)
4975 s = spare;
4976 if (!s) {
4977 champion = NULL;
4978 goto out;
4979 }
4980 champion = s;
4981
4982 if (conflict)
4983 pr_err("Chose family %#x on '%s', assemble conflicts to new container with '--update=uuid'\n",
4984 __le32_to_cpu(s->anchor->family_num), s->disks->devname);
4985
4986 /* collect all dl's onto 'champion', and update them to
4987 * champion's version of the status
4988 */
4989 for (s = *super_list; s; s = s->next) {
4990 struct imsm_super *mpb = champion->anchor;
4991 struct dl *dl = s->disks;
4992
4993 if (s == champion)
4994 continue;
4995
4996 mpb->attributes |= s->anchor->attributes & MPB_ATTRIB_2TB_DISK;
4997
4998 for (i = 0; i < mpb->num_disks; i++) {
4999 struct imsm_disk *disk;
5000
5001 disk = __serial_to_disk(dl->serial, mpb, &dl->index);
5002 if (disk) {
5003 dl->disk = *disk;
5004 /* only set index on disks that are a member of
5005 * a populated contianer, i.e. one with
5006 * raid_devs
5007 */
5008 if (is_failed(&dl->disk))
5009 dl->index = -2;
5010 else if (is_spare(&dl->disk))
5011 dl->index = -1;
5012 break;
5013 }
5014 }
5015
5016 if (i >= mpb->num_disks) {
5017 struct intel_disk *idisk;
5018
5019 idisk = disk_list_get(dl->serial, disk_list);
5020 if (idisk && is_spare(&idisk->disk) &&
5021 !is_failed(&idisk->disk) && !is_configured(&idisk->disk))
5022 dl->index = -1;
5023 else {
5024 dl->index = -2;
5025 continue;
5026 }
5027 }
5028
5029 dl->next = champion->disks;
5030 champion->disks = dl;
5031 s->disks = NULL;
5032 }
5033
5034 /* delete 'champion' from super_list */
5035 for (del = super_list; *del; ) {
5036 if (*del == champion) {
5037 *del = (*del)->next;
5038 break;
5039 } else
5040 del = &(*del)->next;
5041 }
5042 champion->next = NULL;
5043
5044 out:
5045 while (disk_list) {
5046 struct intel_disk *idisk = disk_list;
5047
5048 disk_list = disk_list->next;
5049 free(idisk);
5050 }
5051
5052 return champion;
5053 }
5054
5055 static int
5056 get_sra_super_block(int fd, struct intel_super **super_list, char *devname, int *max, int keep_fd);
5057 static int get_super_block(struct intel_super **super_list, char *devnm, char *devname,
5058 int major, int minor, int keep_fd);
5059 static int
5060 get_devlist_super_block(struct md_list *devlist, struct intel_super **super_list,
5061 int *max, int keep_fd);
5062
5063 static int load_super_imsm_all(struct supertype *st, int fd, void **sbp,
5064 char *devname, struct md_list *devlist,
5065 int keep_fd)
5066 {
5067 struct intel_super *super_list = NULL;
5068 struct intel_super *super = NULL;
5069 int err = 0;
5070 int i = 0;
5071
5072 if (fd >= 0)
5073 /* 'fd' is an opened container */
5074 err = get_sra_super_block(fd, &super_list, devname, &i, keep_fd);
5075 else
5076 /* get super block from devlist devices */
5077 err = get_devlist_super_block(devlist, &super_list, &i, keep_fd);
5078 if (err)
5079 goto error;
5080 /* all mpbs enter, maybe one leaves */
5081 super = imsm_thunderdome(&super_list, i);
5082 if (!super) {
5083 err = 1;
5084 goto error;
5085 }
5086
5087 if (find_missing(super) != 0) {
5088 free_imsm(super);
5089 err = 2;
5090 goto error;
5091 }
5092
5093 /* load migration record */
5094 err = load_imsm_migr_rec(super);
5095 if (err == -1) {
5096 /* migration is in progress,
5097 * but migr_rec cannot be loaded,
5098 */
5099 err = 4;
5100 goto error;
5101 }
5102
5103 /* Check migration compatibility */
5104 if (err == 0 && check_mpb_migr_compatibility(super) != 0) {
5105 pr_err("Unsupported migration detected");
5106 if (devname)
5107 fprintf(stderr, " on %s\n", devname);
5108 else
5109 fprintf(stderr, " (IMSM).\n");
5110
5111 err = 5;
5112 goto error;
5113 }
5114
5115 err = 0;
5116
5117 error:
5118 while (super_list) {
5119 struct intel_super *s = super_list;
5120
5121 super_list = super_list->next;
5122 free_imsm(s);
5123 }
5124
5125 if (err)
5126 return err;
5127
5128 *sbp = super;
5129 if (fd >= 0)
5130 strcpy(st->container_devnm, fd2devnm(fd));
5131 else
5132 st->container_devnm[0] = 0;
5133 if (err == 0 && st->ss == NULL) {
5134 st->ss = &super_imsm;
5135 st->minor_version = 0;
5136 st->max_devs = IMSM_MAX_DEVICES;
5137 }
5138 return 0;
5139 }
5140
5141 static int
5142 get_devlist_super_block(struct md_list *devlist, struct intel_super **super_list,
5143 int *max, int keep_fd)
5144 {
5145 struct md_list *tmpdev;
5146 int err = 0;
5147 int i = 0;
5148
5149 for (i = 0, tmpdev = devlist; tmpdev; tmpdev = tmpdev->next) {
5150 if (tmpdev->used != 1)
5151 continue;
5152 if (tmpdev->container == 1) {
5153 int lmax = 0;
5154 int fd = dev_open(tmpdev->devname, O_RDONLY|O_EXCL);
5155 if (fd < 0) {
5156 pr_err("cannot open device %s: %s\n",
5157 tmpdev->devname, strerror(errno));
5158 err = 8;
5159 goto error;
5160 }
5161 err = get_sra_super_block(fd, super_list,
5162 tmpdev->devname, &lmax,
5163 keep_fd);
5164 i += lmax;
5165 close(fd);
5166 if (err) {
5167 err = 7;
5168 goto error;
5169 }
5170 } else {
5171 int major = major(tmpdev->st_rdev);
5172 int minor = minor(tmpdev->st_rdev);
5173 err = get_super_block(super_list,
5174 NULL,
5175 tmpdev->devname,
5176 major, minor,
5177 keep_fd);
5178 i++;
5179 if (err) {
5180 err = 6;
5181 goto error;
5182 }
5183 }
5184 }
5185 error:
5186 *max = i;
5187 return err;
5188 }
5189
5190 static int get_super_block(struct intel_super **super_list, char *devnm, char *devname,
5191 int major, int minor, int keep_fd)
5192 {
5193 struct intel_super *s;
5194 char nm[32];
5195 int dfd = -1;
5196 int err = 0;
5197 int retry;
5198
5199 s = alloc_super();
5200 if (!s) {
5201 err = 1;
5202 goto error;
5203 }
5204
5205 sprintf(nm, "%d:%d", major, minor);
5206 dfd = dev_open(nm, O_RDWR);
5207 if (dfd < 0) {
5208 err = 2;
5209 goto error;
5210 }
5211
5212 get_dev_sector_size(dfd, NULL, &s->sector_size);
5213 find_intel_hba_capability(dfd, s, devname);
5214 err = load_and_parse_mpb(dfd, s, NULL, keep_fd);
5215
5216 /* retry the load if we might have raced against mdmon */
5217 if (err == 3 && devnm && mdmon_running(devnm))
5218 for (retry = 0; retry < 3; retry++) {
5219 usleep(3000);
5220 err = load_and_parse_mpb(dfd, s, NULL, keep_fd);
5221 if (err != 3)
5222 break;
5223 }
5224 error:
5225 if (!err) {
5226 s->next = *super_list;
5227 *super_list = s;
5228 } else {
5229 if (s)
5230 free_imsm(s);
5231 if (dfd >= 0)
5232 close(dfd);
5233 }
5234 if (dfd >= 0 && !keep_fd)
5235 close(dfd);
5236 return err;
5237
5238 }
5239
5240 static int
5241 get_sra_super_block(int fd, struct intel_super **super_list, char *devname, int *max, int keep_fd)
5242 {
5243 struct mdinfo *sra;
5244 char *devnm;
5245 struct mdinfo *sd;
5246 int err = 0;
5247 int i = 0;
5248 sra = sysfs_read(fd, NULL, GET_LEVEL|GET_VERSION|GET_DEVS|GET_STATE);
5249 if (!sra)
5250 return 1;
5251
5252 if (sra->array.major_version != -1 ||
5253 sra->array.minor_version != -2 ||
5254 strcmp(sra->text_version, "imsm") != 0) {
5255 err = 1;
5256 goto error;
5257 }
5258 /* load all mpbs */
5259 devnm = fd2devnm(fd);
5260 for (sd = sra->devs, i = 0; sd; sd = sd->next, i++) {
5261 if (get_super_block(super_list, devnm, devname,
5262 sd->disk.major, sd->disk.minor, keep_fd) != 0) {
5263 err = 7;
5264 goto error;
5265 }
5266 }
5267 error:
5268 sysfs_free(sra);
5269 *max = i;
5270 return err;
5271 }
5272
5273 static int load_container_imsm(struct supertype *st, int fd, char *devname)
5274 {
5275 return load_super_imsm_all(st, fd, &st->sb, devname, NULL, 1);
5276 }
5277
5278 static int load_super_imsm(struct supertype *st, int fd, char *devname)
5279 {
5280 struct intel_super *super;
5281 int rv;
5282 int retry;
5283
5284 if (test_partition(fd))
5285 /* IMSM not allowed on partitions */
5286 return 1;
5287
5288 free_super_imsm(st);
5289
5290 super = alloc_super();
5291 get_dev_sector_size(fd, NULL, &super->sector_size);
5292 if (!super)
5293 return 1;
5294 /* Load hba and capabilities if they exist.
5295 * But do not preclude loading metadata in case capabilities or hba are
5296 * non-compliant and ignore_hw_compat is set.
5297 */
5298 rv = find_intel_hba_capability(fd, super, devname);
5299 /* no orom/efi or non-intel hba of the disk */
5300 if (rv != 0 && st->ignore_hw_compat == 0) {
5301 if (devname)
5302 pr_err("No OROM/EFI properties for %s\n", devname);
5303 free_imsm(super);
5304 return 2;
5305 }
5306 rv = load_and_parse_mpb(fd, super, devname, 0);
5307
5308 /* retry the load if we might have raced against mdmon */
5309 if (rv == 3) {
5310 struct mdstat_ent *mdstat = NULL;
5311 char *name = fd2kname(fd);
5312
5313 if (name)
5314 mdstat = mdstat_by_component(name);
5315
5316 if (mdstat && mdmon_running(mdstat->devnm) && getpid() != mdmon_pid(mdstat->devnm)) {
5317 for (retry = 0; retry < 3; retry++) {
5318 usleep(3000);
5319 rv = load_and_parse_mpb(fd, super, devname, 0);
5320 if (rv != 3)
5321 break;
5322 }
5323 }
5324
5325 free_mdstat(mdstat);
5326 }
5327
5328 if (rv) {
5329 if (devname)
5330 pr_err("Failed to load all information sections on %s\n", devname);
5331 free_imsm(super);
5332 return rv;
5333 }
5334
5335 st->sb = super;
5336 if (st->ss == NULL) {
5337 st->ss = &super_imsm;
5338 st->minor_version = 0;
5339 st->max_devs = IMSM_MAX_DEVICES;
5340 }
5341
5342 /* load migration record */
5343 if (load_imsm_migr_rec(super) == 0) {
5344 /* Check for unsupported migration features */
5345 if (check_mpb_migr_compatibility(super) != 0) {
5346 pr_err("Unsupported migration detected");
5347 if (devname)
5348 fprintf(stderr, " on %s\n", devname);
5349 else
5350 fprintf(stderr, " (IMSM).\n");
5351 return 3;
5352 }
5353 }
5354
5355 return 0;
5356 }
5357
5358 static __u16 info_to_blocks_per_strip(mdu_array_info_t *info)
5359 {
5360 if (info->level == 1)
5361 return 128;
5362 return info->chunk_size >> 9;
5363 }
5364
5365 static unsigned long long info_to_blocks_per_member(mdu_array_info_t *info,
5366 unsigned long long size)
5367 {
5368 if (info->level == 1)
5369 return size * 2;
5370 else
5371 return (size * 2) & ~(info_to_blocks_per_strip(info) - 1);
5372 }
5373
5374 static void imsm_update_version_info(struct intel_super *super)
5375 {
5376 /* update the version and attributes */
5377 struct imsm_super *mpb = super->anchor;
5378 char *version;
5379 struct imsm_dev *dev;
5380 struct imsm_map *map;
5381 int i;
5382
5383 for (i = 0; i < mpb->num_raid_devs; i++) {
5384 dev = get_imsm_dev(super, i);
5385 map = get_imsm_map(dev, MAP_0);
5386 if (__le32_to_cpu(dev->size_high) > 0)
5387 mpb->attributes |= MPB_ATTRIB_2TB;
5388
5389 /* FIXME detect when an array spans a port multiplier */
5390 #if 0
5391 mpb->attributes |= MPB_ATTRIB_PM;
5392 #endif
5393
5394 if (mpb->num_raid_devs > 1 ||
5395 mpb->attributes != MPB_ATTRIB_CHECKSUM_VERIFY) {
5396 version = MPB_VERSION_ATTRIBS;
5397 switch (get_imsm_raid_level(map)) {
5398 case 0: mpb->attributes |= MPB_ATTRIB_RAID0; break;
5399 case 1: mpb->attributes |= MPB_ATTRIB_RAID1; break;
5400 case 10: mpb->attributes |= MPB_ATTRIB_RAID10; break;
5401 case 5: mpb->attributes |= MPB_ATTRIB_RAID5; break;
5402 }
5403 } else {
5404 if (map->num_members >= 5)
5405 version = MPB_VERSION_5OR6_DISK_ARRAY;
5406 else if (dev->status == DEV_CLONE_N_GO)
5407 version = MPB_VERSION_CNG;
5408 else if (get_imsm_raid_level(map) == 5)
5409 version = MPB_VERSION_RAID5;
5410 else if (map->num_members >= 3)
5411 version = MPB_VERSION_3OR4_DISK_ARRAY;
5412 else if (get_imsm_raid_level(map) == 1)
5413 version = MPB_VERSION_RAID1;
5414 else
5415 version = MPB_VERSION_RAID0;
5416 }
5417 strcpy(((char *) mpb->sig) + strlen(MPB_SIGNATURE), version);
5418 }
5419 }
5420
5421 static int check_name(struct intel_super *super, char *name, int quiet)
5422 {
5423 struct imsm_super *mpb = super->anchor;
5424 char *reason = NULL;
5425 char *start = name;
5426 size_t len = strlen(name);
5427 int i;
5428
5429 if (len > 0) {
5430 while (isspace(start[len - 1]))
5431 start[--len] = 0;
5432 while (*start && isspace(*start))
5433 ++start, --len;
5434 memmove(name, start, len + 1);
5435 }
5436
5437 if (len > MAX_RAID_SERIAL_LEN)
5438 reason = "must be 16 characters or less";
5439 else if (len == 0)
5440 reason = "must be a non-empty string";
5441
5442 for (i = 0; i < mpb->num_raid_devs; i++) {
5443 struct imsm_dev *dev = get_imsm_dev(super, i);
5444
5445 if (strncmp((char *) dev->volume, name, MAX_RAID_SERIAL_LEN) == 0) {
5446 reason = "already exists";
5447 break;
5448 }
5449 }
5450
5451 if (reason && !quiet)
5452 pr_err("imsm volume name %s\n", reason);
5453
5454 return !reason;
5455 }
5456
5457 static int init_super_imsm_volume(struct supertype *st, mdu_array_info_t *info,
5458 struct shape *s, char *name,
5459 char *homehost, int *uuid,
5460 long long data_offset)
5461 {
5462 /* We are creating a volume inside a pre-existing container.
5463 * so st->sb is already set.
5464 */
5465 struct intel_super *super = st->sb;
5466 unsigned int sector_size = super->sector_size;
5467 struct imsm_super *mpb = super->anchor;
5468 struct intel_dev *dv;
5469 struct imsm_dev *dev;
5470 struct imsm_vol *vol;
5471 struct imsm_map *map;
5472 int idx = mpb->num_raid_devs;
5473 int i;
5474 int namelen;
5475 unsigned long long array_blocks;
5476 size_t size_old, size_new;
5477 unsigned long long num_data_stripes;
5478 unsigned int data_disks;
5479 unsigned long long size_per_member;
5480
5481 if (super->orom && mpb->num_raid_devs >= super->orom->vpa) {
5482 pr_err("This imsm-container already has the maximum of %d volumes\n", super->orom->vpa);
5483 return 0;
5484 }
5485
5486 /* ensure the mpb is large enough for the new data */
5487 size_old = __le32_to_cpu(mpb->mpb_size);
5488 size_new = disks_to_mpb_size(info->nr_disks);
5489 if (size_new > size_old) {
5490 void *mpb_new;
5491 size_t size_round = ROUND_UP(size_new, sector_size);
5492
5493 if (posix_memalign(&mpb_new, sector_size, size_round) != 0) {
5494 pr_err("could not allocate new mpb\n");
5495 return 0;
5496 }
5497 if (posix_memalign(&super->migr_rec_buf, MAX_SECTOR_SIZE,
5498 MIGR_REC_BUF_SECTORS*
5499 MAX_SECTOR_SIZE) != 0) {
5500 pr_err("could not allocate migr_rec buffer\n");
5501 free(super->buf);
5502 free(super);
5503 free(mpb_new);
5504 return 0;
5505 }
5506 memcpy(mpb_new, mpb, size_old);
5507 free(mpb);
5508 mpb = mpb_new;
5509 super->anchor = mpb_new;
5510 mpb->mpb_size = __cpu_to_le32(size_new);
5511 memset(mpb_new + size_old, 0, size_round - size_old);
5512 super->len = size_round;
5513 }
5514 super->current_vol = idx;
5515
5516 /* handle 'failed_disks' by either:
5517 * a) create dummy disk entries in the table if this the first
5518 * volume in the array. We add them here as this is the only
5519 * opportunity to add them. add_to_super_imsm_volume()
5520 * handles the non-failed disks and continues incrementing
5521 * mpb->num_disks.
5522 * b) validate that 'failed_disks' matches the current number
5523 * of missing disks if the container is populated
5524 */
5525 if (super->current_vol == 0) {
5526 mpb->num_disks = 0;
5527 for (i = 0; i < info->failed_disks; i++) {
5528 struct imsm_disk *disk;
5529
5530 mpb->num_disks++;
5531 disk = __get_imsm_disk(mpb, i);
5532 disk->status = CONFIGURED_DISK | FAILED_DISK;
5533 disk->scsi_id = __cpu_to_le32(~(__u32)0);
5534 snprintf((char *) disk->serial, MAX_RAID_SERIAL_LEN,
5535 "missing:%d", (__u8)i);
5536 }
5537 find_missing(super);
5538 } else {
5539 int missing = 0;
5540 struct dl *d;
5541
5542 for (d = super->missing; d; d = d->next)
5543 missing++;
5544 if (info->failed_disks > missing) {
5545 pr_err("unable to add 'missing' disk to container\n");
5546 return 0;
5547 }
5548 }
5549
5550 if (!check_name(super, name, 0))
5551 return 0;
5552 dv = xmalloc(sizeof(*dv));
5553 dev = xcalloc(1, sizeof(*dev) + sizeof(__u32) * (info->raid_disks - 1));
5554 /*
5555 * Explicitly allow truncating to not confuse gcc's
5556 * -Werror=stringop-truncation
5557 */
5558 namelen = min((int) strlen(name), MAX_RAID_SERIAL_LEN);
5559 memcpy(dev->volume, name, namelen);
5560 array_blocks = calc_array_size(info->level, info->raid_disks,
5561 info->layout, info->chunk_size,
5562 s->size * BLOCKS_PER_KB);
5563 data_disks = get_data_disks(info->level, info->layout,
5564 info->raid_disks);
5565 array_blocks = round_size_to_mb(array_blocks, data_disks);
5566 size_per_member = array_blocks / data_disks;
5567
5568 set_imsm_dev_size(dev, array_blocks);
5569 dev->status = (DEV_READ_COALESCING | DEV_WRITE_COALESCING);
5570 vol = &dev->vol;
5571 vol->migr_state = 0;
5572 set_migr_type(dev, MIGR_INIT);
5573 vol->dirty = !info->state;
5574 set_vol_curr_migr_unit(dev, 0);
5575 map = get_imsm_map(dev, MAP_0);
5576 set_pba_of_lba0(map, super->create_offset);
5577 map->blocks_per_strip = __cpu_to_le16(info_to_blocks_per_strip(info));
5578 map->failed_disk_num = ~0;
5579 if (info->level > 0)
5580 map->map_state = (info->state ? IMSM_T_STATE_NORMAL
5581 : IMSM_T_STATE_UNINITIALIZED);
5582 else
5583 map->map_state = info->failed_disks ? IMSM_T_STATE_FAILED :
5584 IMSM_T_STATE_NORMAL;
5585 map->ddf = 1;
5586
5587 if (info->level == 1 && info->raid_disks > 2) {
5588 free(dev);
5589 free(dv);
5590 pr_err("imsm does not support more than 2 disksin a raid1 volume\n");
5591 return 0;
5592 }
5593
5594 map->raid_level = info->level;
5595 if (info->level == 10) {
5596 map->raid_level = 1;
5597 map->num_domains = info->raid_disks / 2;
5598 } else if (info->level == 1)
5599 map->num_domains = info->raid_disks;
5600 else
5601 map->num_domains = 1;
5602
5603 /* info->size is only int so use the 'size' parameter instead */
5604 num_data_stripes = size_per_member / info_to_blocks_per_strip(info);
5605 num_data_stripes /= map->num_domains;
5606 set_num_data_stripes(map, num_data_stripes);
5607
5608 size_per_member += NUM_BLOCKS_DIRTY_STRIPE_REGION;
5609 set_blocks_per_member(map, info_to_blocks_per_member(info,
5610 size_per_member /
5611 BLOCKS_PER_KB));
5612
5613 map->num_members = info->raid_disks;
5614 for (i = 0; i < map->num_members; i++) {
5615 /* initialized in add_to_super */
5616 set_imsm_ord_tbl_ent(map, i, IMSM_ORD_REBUILD);
5617 }
5618 mpb->num_raid_devs++;
5619 mpb->num_raid_devs_created++;
5620 dev->my_vol_raid_dev_num = mpb->num_raid_devs_created;
5621
5622 if (s->consistency_policy <= CONSISTENCY_POLICY_RESYNC) {
5623 dev->rwh_policy = RWH_MULTIPLE_OFF;
5624 } else if (s->consistency_policy == CONSISTENCY_POLICY_PPL) {
5625 dev->rwh_policy = RWH_MULTIPLE_DISTRIBUTED;
5626 } else {
5627 free(dev);
5628 free(dv);
5629 pr_err("imsm does not support consistency policy %s\n",
5630 map_num(consistency_policies, s->consistency_policy));
5631 return 0;
5632 }
5633
5634 dv->dev = dev;
5635 dv->index = super->current_vol;
5636 dv->next = super->devlist;
5637 super->devlist = dv;
5638
5639 imsm_update_version_info(super);
5640
5641 return 1;
5642 }
5643
5644 static int init_super_imsm(struct supertype *st, mdu_array_info_t *info,
5645 struct shape *s, char *name,
5646 char *homehost, int *uuid,
5647 unsigned long long data_offset)
5648 {
5649 /* This is primarily called by Create when creating a new array.
5650 * We will then get add_to_super called for each component, and then
5651 * write_init_super called to write it out to each device.
5652 * For IMSM, Create can create on fresh devices or on a pre-existing
5653 * array.
5654 * To create on a pre-existing array a different method will be called.
5655 * This one is just for fresh drives.
5656 */
5657 struct intel_super *super;
5658 struct imsm_super *mpb;
5659 size_t mpb_size;
5660 char *version;
5661
5662 if (data_offset != INVALID_SECTORS) {
5663 pr_err("data-offset not supported by imsm\n");
5664 return 0;
5665 }
5666
5667 if (st->sb)
5668 return init_super_imsm_volume(st, info, s, name, homehost, uuid,
5669 data_offset);
5670
5671 if (info)
5672 mpb_size = disks_to_mpb_size(info->nr_disks);
5673 else
5674 mpb_size = MAX_SECTOR_SIZE;
5675
5676 super = alloc_super();
5677 if (super &&
5678 posix_memalign(&super->buf, MAX_SECTOR_SIZE, mpb_size) != 0) {
5679 free_imsm(super);
5680 super = NULL;
5681 }
5682 if (!super) {
5683 pr_err("could not allocate superblock\n");
5684 return 0;
5685 }
5686 if (posix_memalign(&super->migr_rec_buf, MAX_SECTOR_SIZE,
5687 MIGR_REC_BUF_SECTORS*MAX_SECTOR_SIZE) != 0) {
5688 pr_err("could not allocate migr_rec buffer\n");
5689 free(super->buf);
5690 free_imsm(super);
5691 return 0;
5692 }
5693 memset(super->buf, 0, mpb_size);
5694 mpb = super->buf;
5695 mpb->mpb_size = __cpu_to_le32(mpb_size);
5696 st->sb = super;
5697
5698 if (info == NULL) {
5699 /* zeroing superblock */
5700 return 0;
5701 }
5702
5703 mpb->attributes = MPB_ATTRIB_CHECKSUM_VERIFY;
5704
5705 version = (char *) mpb->sig;
5706 strcpy(version, MPB_SIGNATURE);
5707 version += strlen(MPB_SIGNATURE);
5708 strcpy(version, MPB_VERSION_RAID0);
5709
5710 return 1;
5711 }
5712
5713 static int drive_validate_sector_size(struct intel_super *super, struct dl *dl)
5714 {
5715 unsigned int member_sector_size;
5716
5717 if (dl->fd < 0) {
5718 pr_err("Invalid file descriptor for %s\n", dl->devname);
5719 return 0;
5720 }
5721
5722 if (!get_dev_sector_size(dl->fd, dl->devname, &member_sector_size))
5723 return 0;
5724 if (member_sector_size != super->sector_size)
5725 return 0;
5726 return 1;
5727 }
5728
5729 static int add_to_super_imsm_volume(struct supertype *st, mdu_disk_info_t *dk,
5730 int fd, char *devname)
5731 {
5732 struct intel_super *super = st->sb;
5733 struct imsm_super *mpb = super->anchor;
5734 struct imsm_disk *_disk;
5735 struct imsm_dev *dev;
5736 struct imsm_map *map;
5737 struct dl *dl, *df;
5738 int slot;
5739
5740 dev = get_imsm_dev(super, super->current_vol);
5741 map = get_imsm_map(dev, MAP_0);
5742
5743 if (! (dk->state & (1<<MD_DISK_SYNC))) {
5744 pr_err("%s: Cannot add spare devices to IMSM volume\n",
5745 devname);
5746 return 1;
5747 }
5748
5749 if (fd == -1) {
5750 /* we're doing autolayout so grab the pre-marked (in
5751 * validate_geometry) raid_disk
5752 */
5753 for (dl = super->disks; dl; dl = dl->next)
5754 if (dl->raiddisk == dk->raid_disk)
5755 break;
5756 } else {
5757 for (dl = super->disks; dl ; dl = dl->next)
5758 if (dl->major == dk->major &&
5759 dl->minor == dk->minor)
5760 break;
5761 }
5762
5763 if (!dl) {
5764 pr_err("%s is not a member of the same container\n", devname);
5765 return 1;
5766 }
5767
5768 if (mpb->num_disks == 0)
5769 if (!get_dev_sector_size(dl->fd, dl->devname,
5770 &super->sector_size))
5771 return 1;
5772
5773 if (!drive_validate_sector_size(super, dl)) {
5774 pr_err("Combining drives of different sector size in one volume is not allowed\n");
5775 return 1;
5776 }
5777
5778 /* add a pristine spare to the metadata */
5779 if (dl->index < 0) {
5780 dl->index = super->anchor->num_disks;
5781 super->anchor->num_disks++;
5782 }
5783 /* Check the device has not already been added */
5784 slot = get_imsm_disk_slot(map, dl->index);
5785 if (slot >= 0 &&
5786 (get_imsm_ord_tbl_ent(dev, slot, MAP_X) & IMSM_ORD_REBUILD) == 0) {
5787 pr_err("%s has been included in this array twice\n",
5788 devname);
5789 return 1;
5790 }
5791 set_imsm_ord_tbl_ent(map, dk->raid_disk, dl->index);
5792 dl->disk.status = CONFIGURED_DISK;
5793
5794 /* update size of 'missing' disks to be at least as large as the
5795 * largest acitve member (we only have dummy missing disks when
5796 * creating the first volume)
5797 */
5798 if (super->current_vol == 0) {
5799 for (df = super->missing; df; df = df->next) {
5800 if (total_blocks(&dl->disk) > total_blocks(&df->disk))
5801 set_total_blocks(&df->disk, total_blocks(&dl->disk));
5802 _disk = __get_imsm_disk(mpb, df->index);
5803 *_disk = df->disk;
5804 }
5805 }
5806
5807 /* refresh unset/failed slots to point to valid 'missing' entries */
5808 for (df = super->missing; df; df = df->next)
5809 for (slot = 0; slot < mpb->num_disks; slot++) {
5810 __u32 ord = get_imsm_ord_tbl_ent(dev, slot, MAP_X);
5811
5812 if ((ord & IMSM_ORD_REBUILD) == 0)
5813 continue;
5814 set_imsm_ord_tbl_ent(map, slot, df->index | IMSM_ORD_REBUILD);
5815 if (is_gen_migration(dev)) {
5816 struct imsm_map *map2 = get_imsm_map(dev,
5817 MAP_1);
5818 int slot2 = get_imsm_disk_slot(map2, df->index);
5819 if (slot2 < map2->num_members && slot2 >= 0) {
5820 __u32 ord2 = get_imsm_ord_tbl_ent(dev,
5821 slot2,
5822 MAP_1);
5823 if ((unsigned)df->index ==
5824 ord_to_idx(ord2))
5825 set_imsm_ord_tbl_ent(map2,
5826 slot2,
5827 df->index |
5828 IMSM_ORD_REBUILD);
5829 }
5830 }
5831 dprintf("set slot:%d to missing disk:%d\n", slot, df->index);
5832 break;
5833 }
5834
5835 /* if we are creating the first raid device update the family number */
5836 if (super->current_vol == 0) {
5837 __u32 sum;
5838 struct imsm_dev *_dev = __get_imsm_dev(mpb, 0);
5839
5840 _disk = __get_imsm_disk(mpb, dl->index);
5841 if (!_dev || !_disk) {
5842 pr_err("BUG mpb setup error\n");
5843 return 1;
5844 }
5845 *_dev = *dev;
5846 *_disk = dl->disk;
5847 sum = random32();
5848 sum += __gen_imsm_checksum(mpb);
5849 mpb->family_num = __cpu_to_le32(sum);
5850 mpb->orig_family_num = mpb->family_num;
5851 mpb->creation_time = __cpu_to_le64((__u64)time(NULL));
5852 }
5853 super->current_disk = dl;
5854 return 0;
5855 }
5856
5857 /* mark_spare()
5858 * Function marks disk as spare and restores disk serial
5859 * in case it was previously marked as failed by takeover operation
5860 * reruns:
5861 * -1 : critical error
5862 * 0 : disk is marked as spare but serial is not set
5863 * 1 : success
5864 */
5865 int mark_spare(struct dl *disk)
5866 {
5867 __u8 serial[MAX_RAID_SERIAL_LEN];
5868 int ret_val = -1;
5869
5870 if (!disk)
5871 return ret_val;
5872
5873 ret_val = 0;
5874 if (!imsm_read_serial(disk->fd, NULL, serial, MAX_RAID_SERIAL_LEN)) {
5875 /* Restore disk serial number, because takeover marks disk
5876 * as failed and adds to serial ':0' before it becomes
5877 * a spare disk.
5878 */
5879 serialcpy(disk->serial, serial);
5880 serialcpy(disk->disk.serial, serial);
5881 ret_val = 1;
5882 }
5883 disk->disk.status = SPARE_DISK;
5884 disk->index = -1;
5885
5886 return ret_val;
5887 }
5888
5889
5890 static int write_super_imsm_spare(struct intel_super *super, struct dl *d);
5891
5892 static int add_to_super_imsm(struct supertype *st, mdu_disk_info_t *dk,
5893 int fd, char *devname,
5894 unsigned long long data_offset)
5895 {
5896 struct intel_super *super = st->sb;
5897 struct dl *dd;
5898 unsigned long long size;
5899 unsigned int member_sector_size;
5900 __u32 id;
5901 int rv;
5902 struct stat stb;
5903
5904 /* If we are on an RAID enabled platform check that the disk is
5905 * attached to the raid controller.
5906 * We do not need to test disks attachment for container based additions,
5907 * they shall be already tested when container was created/assembled.
5908 */
5909 rv = find_intel_hba_capability(fd, super, devname);
5910 /* no orom/efi or non-intel hba of the disk */
5911 if (rv != 0) {
5912 dprintf("capability: %p fd: %d ret: %d\n",
5913 super->orom, fd, rv);
5914 return 1;
5915 }
5916
5917 if (super->current_vol >= 0)
5918 return add_to_super_imsm_volume(st, dk, fd, devname);
5919
5920 fstat(fd, &stb);
5921 dd = xcalloc(sizeof(*dd), 1);
5922 dd->major = major(stb.st_rdev);
5923 dd->minor = minor(stb.st_rdev);
5924 dd->devname = devname ? xstrdup(devname) : NULL;
5925 dd->fd = fd;
5926 dd->e = NULL;
5927 dd->action = DISK_ADD;
5928 rv = imsm_read_serial(fd, devname, dd->serial, MAX_RAID_SERIAL_LEN);
5929 if (rv) {
5930 pr_err("failed to retrieve scsi serial, aborting\n");
5931 if (dd->devname)
5932 free(dd->devname);
5933 free(dd);
5934 abort();
5935 }
5936 if (super->hba && ((super->hba->type == SYS_DEV_NVME) ||
5937 (super->hba->type == SYS_DEV_VMD))) {
5938 int i;
5939 char *devpath = diskfd_to_devpath(fd);
5940 char controller_path[PATH_MAX];
5941
5942 if (!devpath) {
5943 pr_err("failed to get devpath, aborting\n");
5944 if (dd->devname)
5945 free(dd->devname);
5946 free(dd);
5947 return 1;
5948 }
5949
5950 snprintf(controller_path, PATH_MAX-1, "%s/device", devpath);
5951 free(devpath);
5952
5953 if (!imsm_is_nvme_supported(dd->fd, 1)) {
5954 if (dd->devname)
5955 free(dd->devname);
5956 free(dd);
5957 return 1;
5958 }
5959
5960 if (devpath_to_vendor(controller_path) == 0x8086) {
5961 /*
5962 * If Intel's NVMe drive has serial ended with
5963 * "-A","-B","-1" or "-2" it means that this is "x8"
5964 * device (double drive on single PCIe card).
5965 * User should be warned about potential data loss.
5966 */
5967 for (i = MAX_RAID_SERIAL_LEN-1; i > 0; i--) {
5968 /* Skip empty character at the end */
5969 if (dd->serial[i] == 0)
5970 continue;
5971
5972 if (((dd->serial[i] == 'A') ||
5973 (dd->serial[i] == 'B') ||
5974 (dd->serial[i] == '1') ||
5975 (dd->serial[i] == '2')) &&
5976 (dd->serial[i-1] == '-'))
5977 pr_err("\tThe action you are about to take may put your data at risk.\n"
5978 "\tPlease note that x8 devices may consist of two separate x4 devices "
5979 "located on a single PCIe port.\n"
5980 "\tRAID 0 is the only supported configuration for this type of x8 device.\n");
5981 break;
5982 }
5983 } else if (super->hba->type == SYS_DEV_VMD && super->orom &&
5984 !imsm_orom_has_tpv_support(super->orom)) {
5985 pr_err("\tPlatform configuration does not support non-Intel NVMe drives.\n"
5986 "\tPlease refer to Intel(R) RSTe/VROC user guide.\n");
5987 free(dd->devname);
5988 free(dd);
5989 return 1;
5990 }
5991 }
5992
5993 get_dev_size(fd, NULL, &size);
5994 get_dev_sector_size(fd, NULL, &member_sector_size);
5995
5996 if (super->sector_size == 0) {
5997 /* this a first device, so sector_size is not set yet */
5998 super->sector_size = member_sector_size;
5999 }
6000
6001 /* clear migr_rec when adding disk to container */
6002 memset(super->migr_rec_buf, 0, MIGR_REC_BUF_SECTORS*MAX_SECTOR_SIZE);
6003 if (lseek64(fd, size - MIGR_REC_SECTOR_POSITION*member_sector_size,
6004 SEEK_SET) >= 0) {
6005 if ((unsigned int)write(fd, super->migr_rec_buf,
6006 MIGR_REC_BUF_SECTORS*member_sector_size) !=
6007 MIGR_REC_BUF_SECTORS*member_sector_size)
6008 perror("Write migr_rec failed");
6009 }
6010
6011 size /= 512;
6012 serialcpy(dd->disk.serial, dd->serial);
6013 set_total_blocks(&dd->disk, size);
6014 if (__le32_to_cpu(dd->disk.total_blocks_hi) > 0) {
6015 struct imsm_super *mpb = super->anchor;
6016 mpb->attributes |= MPB_ATTRIB_2TB_DISK;
6017 }
6018 mark_spare(dd);
6019 if (sysfs_disk_to_scsi_id(fd, &id) == 0)
6020 dd->disk.scsi_id = __cpu_to_le32(id);
6021 else
6022 dd->disk.scsi_id = __cpu_to_le32(0);
6023
6024 if (st->update_tail) {
6025 dd->next = super->disk_mgmt_list;
6026 super->disk_mgmt_list = dd;
6027 } else {
6028 /* this is called outside of mdmon
6029 * write initial spare metadata
6030 * mdmon will overwrite it.
6031 */
6032 dd->next = super->disks;
6033 super->disks = dd;
6034 write_super_imsm_spare(super, dd);
6035 }
6036
6037 return 0;
6038 }
6039
6040 static int remove_from_super_imsm(struct supertype *st, mdu_disk_info_t *dk)
6041 {
6042 struct intel_super *super = st->sb;
6043 struct dl *dd;
6044
6045 /* remove from super works only in mdmon - for communication
6046 * manager - monitor. Check if communication memory buffer
6047 * is prepared.
6048 */
6049 if (!st->update_tail) {
6050 pr_err("shall be used in mdmon context only\n");
6051 return 1;
6052 }
6053 dd = xcalloc(1, sizeof(*dd));
6054 dd->major = dk->major;
6055 dd->minor = dk->minor;
6056 dd->fd = -1;
6057 mark_spare(dd);
6058 dd->action = DISK_REMOVE;
6059
6060 dd->next = super->disk_mgmt_list;
6061 super->disk_mgmt_list = dd;
6062
6063 return 0;
6064 }
6065
6066 static int store_imsm_mpb(int fd, struct imsm_super *mpb);
6067
6068 static union {
6069 char buf[MAX_SECTOR_SIZE];
6070 struct imsm_super anchor;
6071 } spare_record __attribute__ ((aligned(MAX_SECTOR_SIZE)));
6072
6073
6074 static int write_super_imsm_spare(struct intel_super *super, struct dl *d)
6075 {
6076 struct imsm_super *mpb = super->anchor;
6077 struct imsm_super *spare = &spare_record.anchor;
6078 __u32 sum;
6079
6080 if (d->index != -1)
6081 return 1;
6082
6083 spare->mpb_size = __cpu_to_le32(sizeof(struct imsm_super));
6084 spare->generation_num = __cpu_to_le32(1UL);
6085 spare->attributes = MPB_ATTRIB_CHECKSUM_VERIFY;
6086 spare->num_disks = 1;
6087 spare->num_raid_devs = 0;
6088 spare->cache_size = mpb->cache_size;
6089 spare->pwr_cycle_count = __cpu_to_le32(1);
6090
6091 snprintf((char *) spare->sig, MAX_SIGNATURE_LENGTH,
6092 MPB_SIGNATURE MPB_VERSION_RAID0);
6093
6094 spare->disk[0] = d->disk;
6095 if (__le32_to_cpu(d->disk.total_blocks_hi) > 0)
6096 spare->attributes |= MPB_ATTRIB_2TB_DISK;
6097
6098 if (super->sector_size == 4096)
6099 convert_to_4k_imsm_disk(&spare->disk[0]);
6100
6101 sum = __gen_imsm_checksum(spare);
6102 spare->family_num = __cpu_to_le32(sum);
6103 spare->orig_family_num = 0;
6104 sum = __gen_imsm_checksum(spare);
6105 spare->check_sum = __cpu_to_le32(sum);
6106
6107 if (store_imsm_mpb(d->fd, spare)) {
6108 pr_err("failed for device %d:%d %s\n",
6109 d->major, d->minor, strerror(errno));
6110 return 1;
6111 }
6112
6113 return 0;
6114 }
6115 /* spare records have their own family number and do not have any defined raid
6116 * devices
6117 */
6118 static int write_super_imsm_spares(struct intel_super *super, int doclose)
6119 {
6120 struct dl *d;
6121
6122 for (d = super->disks; d; d = d->next) {
6123 if (d->index != -1)
6124 continue;
6125
6126 if (write_super_imsm_spare(super, d))
6127 return 1;
6128
6129 if (doclose) {
6130 close(d->fd);
6131 d->fd = -1;
6132 }
6133 }
6134
6135 return 0;
6136 }
6137
6138 static int write_super_imsm(struct supertype *st, int doclose)
6139 {
6140 struct intel_super *super = st->sb;
6141 unsigned int sector_size = super->sector_size;
6142 struct imsm_super *mpb = super->anchor;
6143 struct dl *d;
6144 __u32 generation;
6145 __u32 sum;
6146 int spares = 0;
6147 int i;
6148 __u32 mpb_size = sizeof(struct imsm_super) - sizeof(struct imsm_disk);
6149 int num_disks = 0;
6150 int clear_migration_record = 1;
6151 __u32 bbm_log_size;
6152
6153 /* 'generation' is incremented everytime the metadata is written */
6154 generation = __le32_to_cpu(mpb->generation_num);
6155 generation++;
6156 mpb->generation_num = __cpu_to_le32(generation);
6157
6158 /* fix up cases where previous mdadm releases failed to set
6159 * orig_family_num
6160 */
6161 if (mpb->orig_family_num == 0)
6162 mpb->orig_family_num = mpb->family_num;
6163
6164 for (d = super->disks; d; d = d->next) {
6165 if (d->index == -1)
6166 spares++;
6167 else {
6168 mpb->disk[d->index] = d->disk;
6169 num_disks++;
6170 }
6171 }
6172 for (d = super->missing; d; d = d->next) {
6173 mpb->disk[d->index] = d->disk;
6174 num_disks++;
6175 }
6176 mpb->num_disks = num_disks;
6177 mpb_size += sizeof(struct imsm_disk) * mpb->num_disks;
6178
6179 for (i = 0; i < mpb->num_raid_devs; i++) {
6180 struct imsm_dev *dev = __get_imsm_dev(mpb, i);
6181 struct imsm_dev *dev2 = get_imsm_dev(super, i);
6182 if (dev && dev2) {
6183 imsm_copy_dev(dev, dev2);
6184 mpb_size += sizeof_imsm_dev(dev, 0);
6185 }
6186 if (is_gen_migration(dev2))
6187 clear_migration_record = 0;
6188 }
6189
6190 bbm_log_size = get_imsm_bbm_log_size(super->bbm_log);
6191
6192 if (bbm_log_size) {
6193 memcpy((void *)mpb + mpb_size, super->bbm_log, bbm_log_size);
6194 mpb->attributes |= MPB_ATTRIB_BBM;
6195 } else
6196 mpb->attributes &= ~MPB_ATTRIB_BBM;
6197
6198 super->anchor->bbm_log_size = __cpu_to_le32(bbm_log_size);
6199 mpb_size += bbm_log_size;
6200 mpb->mpb_size = __cpu_to_le32(mpb_size);
6201
6202 #ifdef DEBUG
6203 assert(super->len == 0 || mpb_size <= super->len);
6204 #endif
6205
6206 /* recalculate checksum */
6207 sum = __gen_imsm_checksum(mpb);
6208 mpb->check_sum = __cpu_to_le32(sum);
6209
6210 if (super->clean_migration_record_by_mdmon) {
6211 clear_migration_record = 1;
6212 super->clean_migration_record_by_mdmon = 0;
6213 }
6214 if (clear_migration_record)
6215 memset(super->migr_rec_buf, 0,
6216 MIGR_REC_BUF_SECTORS*MAX_SECTOR_SIZE);
6217
6218 if (sector_size == 4096)
6219 convert_to_4k(super);
6220
6221 /* write the mpb for disks that compose raid devices */
6222 for (d = super->disks; d ; d = d->next) {
6223 if (d->index < 0 || is_failed(&d->disk))
6224 continue;
6225
6226 if (clear_migration_record) {
6227 unsigned long long dsize;
6228
6229 get_dev_size(d->fd, NULL, &dsize);
6230 if (lseek64(d->fd, dsize - sector_size,
6231 SEEK_SET) >= 0) {
6232 if ((unsigned int)write(d->fd,
6233 super->migr_rec_buf,
6234 MIGR_REC_BUF_SECTORS*sector_size) !=
6235 MIGR_REC_BUF_SECTORS*sector_size)
6236 perror("Write migr_rec failed");
6237 }
6238 }
6239
6240 if (store_imsm_mpb(d->fd, mpb))
6241 fprintf(stderr,
6242 "failed for device %d:%d (fd: %d)%s\n",
6243 d->major, d->minor,
6244 d->fd, strerror(errno));
6245
6246 if (doclose) {
6247 close(d->fd);
6248 d->fd = -1;
6249 }
6250 }
6251
6252 if (spares)
6253 return write_super_imsm_spares(super, doclose);
6254
6255 return 0;
6256 }
6257
6258 static int create_array(struct supertype *st, int dev_idx)
6259 {
6260 size_t len;
6261 struct imsm_update_create_array *u;
6262 struct intel_super *super = st->sb;
6263 struct imsm_dev *dev = get_imsm_dev(super, dev_idx);
6264 struct imsm_map *map = get_imsm_map(dev, MAP_0);
6265 struct disk_info *inf;
6266 struct imsm_disk *disk;
6267 int i;
6268
6269 len = sizeof(*u) - sizeof(*dev) + sizeof_imsm_dev(dev, 0) +
6270 sizeof(*inf) * map->num_members;
6271 u = xmalloc(len);
6272 u->type = update_create_array;
6273 u->dev_idx = dev_idx;
6274 imsm_copy_dev(&u->dev, dev);
6275 inf = get_disk_info(u);
6276 for (i = 0; i < map->num_members; i++) {
6277 int idx = get_imsm_disk_idx(dev, i, MAP_X);
6278
6279 disk = get_imsm_disk(super, idx);
6280 if (!disk)
6281 disk = get_imsm_missing(super, idx);
6282 serialcpy(inf[i].serial, disk->serial);
6283 }
6284 append_metadata_update(st, u, len);
6285
6286 return 0;
6287 }
6288
6289 static int mgmt_disk(struct supertype *st)
6290 {
6291 struct intel_super *super = st->sb;
6292 size_t len;
6293 struct imsm_update_add_remove_disk *u;
6294
6295 if (!super->disk_mgmt_list)
6296 return 0;
6297
6298 len = sizeof(*u);
6299 u = xmalloc(len);
6300 u->type = update_add_remove_disk;
6301 append_metadata_update(st, u, len);
6302
6303 return 0;
6304 }
6305
6306 __u32 crc32c_le(__u32 crc, unsigned char const *p, size_t len);
6307
6308 static int write_ppl_header(unsigned long long ppl_sector, int fd, void *buf)
6309 {
6310 struct ppl_header *ppl_hdr = buf;
6311 int ret;
6312
6313 ppl_hdr->checksum = __cpu_to_le32(~crc32c_le(~0, buf, PPL_HEADER_SIZE));
6314
6315 if (lseek64(fd, ppl_sector * 512, SEEK_SET) < 0) {
6316 ret = -errno;
6317 perror("Failed to seek to PPL header location");
6318 return ret;
6319 }
6320
6321 if (write(fd, buf, PPL_HEADER_SIZE) != PPL_HEADER_SIZE) {
6322 ret = -errno;
6323 perror("Write PPL header failed");
6324 return ret;
6325 }
6326
6327 fsync(fd);
6328
6329 return 0;
6330 }
6331
6332 static int write_init_ppl_imsm(struct supertype *st, struct mdinfo *info, int fd)
6333 {
6334 struct intel_super *super = st->sb;
6335 void *buf;
6336 struct ppl_header *ppl_hdr;
6337 int ret;
6338
6339 /* first clear entire ppl space */
6340 ret = zero_disk_range(fd, info->ppl_sector, info->ppl_size);
6341 if (ret)
6342 return ret;
6343
6344 ret = posix_memalign(&buf, MAX_SECTOR_SIZE, PPL_HEADER_SIZE);
6345 if (ret) {
6346 pr_err("Failed to allocate PPL header buffer\n");
6347 return -ret;
6348 }
6349
6350 memset(buf, 0, PPL_HEADER_SIZE);
6351 ppl_hdr = buf;
6352 memset(ppl_hdr->reserved, 0xff, PPL_HDR_RESERVED);
6353 ppl_hdr->signature = __cpu_to_le32(super->anchor->orig_family_num);
6354
6355 if (info->mismatch_cnt) {
6356 /*
6357 * We are overwriting an invalid ppl. Make one entry with wrong
6358 * checksum to prevent the kernel from skipping resync.
6359 */
6360 ppl_hdr->entries_count = __cpu_to_le32(1);
6361 ppl_hdr->entries[0].checksum = ~0;
6362 }
6363
6364 ret = write_ppl_header(info->ppl_sector, fd, buf);
6365
6366 free(buf);
6367 return ret;
6368 }
6369
6370 static int is_rebuilding(struct imsm_dev *dev);
6371
6372 static int validate_ppl_imsm(struct supertype *st, struct mdinfo *info,
6373 struct mdinfo *disk)
6374 {
6375 struct intel_super *super = st->sb;
6376 struct dl *d;
6377 void *buf_orig, *buf, *buf_prev = NULL;
6378 int ret = 0;
6379 struct ppl_header *ppl_hdr = NULL;
6380 __u32 crc;
6381 struct imsm_dev *dev;
6382 __u32 idx;
6383 unsigned int i;
6384 unsigned long long ppl_offset = 0;
6385 unsigned long long prev_gen_num = 0;
6386
6387 if (disk->disk.raid_disk < 0)
6388 return 0;
6389
6390 dev = get_imsm_dev(super, info->container_member);
6391 idx = get_imsm_disk_idx(dev, disk->disk.raid_disk, MAP_0);
6392 d = get_imsm_dl_disk(super, idx);
6393
6394 if (!d || d->index < 0 || is_failed(&d->disk))
6395 return 0;
6396
6397 if (posix_memalign(&buf_orig, MAX_SECTOR_SIZE, PPL_HEADER_SIZE * 2)) {
6398 pr_err("Failed to allocate PPL header buffer\n");
6399 return -1;
6400 }
6401 buf = buf_orig;
6402
6403 ret = 1;
6404 while (ppl_offset < MULTIPLE_PPL_AREA_SIZE_IMSM) {
6405 void *tmp;
6406
6407 dprintf("Checking potential PPL at offset: %llu\n", ppl_offset);
6408
6409 if (lseek64(d->fd, info->ppl_sector * 512 + ppl_offset,
6410 SEEK_SET) < 0) {
6411 perror("Failed to seek to PPL header location");
6412 ret = -1;
6413 break;
6414 }
6415
6416 if (read(d->fd, buf, PPL_HEADER_SIZE) != PPL_HEADER_SIZE) {
6417 perror("Read PPL header failed");
6418 ret = -1;
6419 break;
6420 }
6421
6422 ppl_hdr = buf;
6423
6424 crc = __le32_to_cpu(ppl_hdr->checksum);
6425 ppl_hdr->checksum = 0;
6426
6427 if (crc != ~crc32c_le(~0, buf, PPL_HEADER_SIZE)) {
6428 dprintf("Wrong PPL header checksum on %s\n",
6429 d->devname);
6430 break;
6431 }
6432
6433 if (prev_gen_num > __le64_to_cpu(ppl_hdr->generation)) {
6434 /* previous was newest, it was already checked */
6435 break;
6436 }
6437
6438 if ((__le32_to_cpu(ppl_hdr->signature) !=
6439 super->anchor->orig_family_num)) {
6440 dprintf("Wrong PPL header signature on %s\n",
6441 d->devname);
6442 ret = 1;
6443 break;
6444 }
6445
6446 ret = 0;
6447 prev_gen_num = __le64_to_cpu(ppl_hdr->generation);
6448
6449 ppl_offset += PPL_HEADER_SIZE;
6450 for (i = 0; i < __le32_to_cpu(ppl_hdr->entries_count); i++)
6451 ppl_offset +=
6452 __le32_to_cpu(ppl_hdr->entries[i].pp_size);
6453
6454 if (!buf_prev)
6455 buf_prev = buf + PPL_HEADER_SIZE;
6456 tmp = buf_prev;
6457 buf_prev = buf;
6458 buf = tmp;
6459 }
6460
6461 if (buf_prev) {
6462 buf = buf_prev;
6463 ppl_hdr = buf_prev;
6464 }
6465
6466 /*
6467 * Update metadata to use mutliple PPLs area (1MB).
6468 * This is done once for all RAID members
6469 */
6470 if (info->consistency_policy == CONSISTENCY_POLICY_PPL &&
6471 info->ppl_size != (MULTIPLE_PPL_AREA_SIZE_IMSM >> 9)) {
6472 char subarray[20];
6473 struct mdinfo *member_dev;
6474
6475 sprintf(subarray, "%d", info->container_member);
6476
6477 if (mdmon_running(st->container_devnm))
6478 st->update_tail = &st->updates;
6479
6480 if (st->ss->update_subarray(st, subarray, "ppl", NULL)) {
6481 pr_err("Failed to update subarray %s\n",
6482 subarray);
6483 } else {
6484 if (st->update_tail)
6485 flush_metadata_updates(st);
6486 else
6487 st->ss->sync_metadata(st);
6488 info->ppl_size = (MULTIPLE_PPL_AREA_SIZE_IMSM >> 9);
6489 for (member_dev = info->devs; member_dev;
6490 member_dev = member_dev->next)
6491 member_dev->ppl_size =
6492 (MULTIPLE_PPL_AREA_SIZE_IMSM >> 9);
6493 }
6494 }
6495
6496 if (ret == 1) {
6497 struct imsm_map *map = get_imsm_map(dev, MAP_X);
6498
6499 if (map->map_state == IMSM_T_STATE_UNINITIALIZED ||
6500 (map->map_state == IMSM_T_STATE_NORMAL &&
6501 !(dev->vol.dirty & RAIDVOL_DIRTY)) ||
6502 (is_rebuilding(dev) &&
6503 vol_curr_migr_unit(dev) == 0 &&
6504 get_imsm_disk_idx(dev, disk->disk.raid_disk, MAP_1) != idx))
6505 ret = st->ss->write_init_ppl(st, info, d->fd);
6506 else
6507 info->mismatch_cnt++;
6508 } else if (ret == 0 &&
6509 ppl_hdr->entries_count == 0 &&
6510 is_rebuilding(dev) &&
6511 info->resync_start == 0) {
6512 /*
6513 * The header has no entries - add a single empty entry and
6514 * rewrite the header to prevent the kernel from going into
6515 * resync after an interrupted rebuild.
6516 */
6517 ppl_hdr->entries_count = __cpu_to_le32(1);
6518 ret = write_ppl_header(info->ppl_sector, d->fd, buf);
6519 }
6520
6521 free(buf_orig);
6522
6523 return ret;
6524 }
6525
6526 static int write_init_ppl_imsm_all(struct supertype *st, struct mdinfo *info)
6527 {
6528 struct intel_super *super = st->sb;
6529 struct dl *d;
6530 int ret = 0;
6531
6532 if (info->consistency_policy != CONSISTENCY_POLICY_PPL ||
6533 info->array.level != 5)
6534 return 0;
6535
6536 for (d = super->disks; d ; d = d->next) {
6537 if (d->index < 0 || is_failed(&d->disk))
6538 continue;
6539
6540 ret = st->ss->write_init_ppl(st, info, d->fd);
6541 if (ret)
6542 break;
6543 }
6544
6545 return ret;
6546 }
6547
6548 /*******************************************************************************
6549 * Function: write_init_bitmap_imsm_vol
6550 * Description: Write a bitmap header and prepares the area for the bitmap.
6551 * Parameters:
6552 * st : supertype information
6553 * vol_idx : the volume index to use
6554 *
6555 * Returns:
6556 * 0 : success
6557 * -1 : fail
6558 ******************************************************************************/
6559 static int write_init_bitmap_imsm_vol(struct supertype *st, int vol_idx)
6560 {
6561 struct intel_super *super = st->sb;
6562 int prev_current_vol = super->current_vol;
6563 struct dl *d;
6564 int ret = 0;
6565
6566 super->current_vol = vol_idx;
6567 for (d = super->disks; d; d = d->next) {
6568 if (d->index < 0 || is_failed(&d->disk))
6569 continue;
6570 ret = st->ss->write_bitmap(st, d->fd, NoUpdate);
6571 if (ret)
6572 break;
6573 }
6574 super->current_vol = prev_current_vol;
6575 return ret;
6576 }
6577
6578 /*******************************************************************************
6579 * Function: write_init_bitmap_imsm_all
6580 * Description: Write a bitmap header and prepares the area for the bitmap.
6581 * Operation is executed for volumes with CONSISTENCY_POLICY_BITMAP.
6582 * Parameters:
6583 * st : supertype information
6584 * info : info about the volume where the bitmap should be written
6585 * vol_idx : the volume index to use
6586 *
6587 * Returns:
6588 * 0 : success
6589 * -1 : fail
6590 ******************************************************************************/
6591 static int write_init_bitmap_imsm_all(struct supertype *st, struct mdinfo *info,
6592 int vol_idx)
6593 {
6594 int ret = 0;
6595
6596 if (info && (info->consistency_policy == CONSISTENCY_POLICY_BITMAP))
6597 ret = write_init_bitmap_imsm_vol(st, vol_idx);
6598
6599 return ret;
6600 }
6601
6602 static int write_init_super_imsm(struct supertype *st)
6603 {
6604 struct intel_super *super = st->sb;
6605 int current_vol = super->current_vol;
6606 int rv = 0;
6607 struct mdinfo info;
6608
6609 getinfo_super_imsm(st, &info, NULL);
6610
6611 /* we are done with current_vol reset it to point st at the container */
6612 super->current_vol = -1;
6613
6614 if (st->update_tail) {
6615 /* queue the recently created array / added disk
6616 * as a metadata update */
6617
6618 /* determine if we are creating a volume or adding a disk */
6619 if (current_vol < 0) {
6620 /* in the mgmt (add/remove) disk case we are running
6621 * in mdmon context, so don't close fd's
6622 */
6623 rv = mgmt_disk(st);
6624 } else {
6625 /* adding the second volume to the array */
6626 rv = write_init_ppl_imsm_all(st, &info);
6627 if (!rv)
6628 rv = write_init_bitmap_imsm_all(st, &info, current_vol);
6629 if (!rv)
6630 rv = create_array(st, current_vol);
6631 }
6632 } else {
6633 struct dl *d;
6634 for (d = super->disks; d; d = d->next)
6635 Kill(d->devname, NULL, 0, -1, 1);
6636 if (current_vol >= 0) {
6637 rv = write_init_ppl_imsm_all(st, &info);
6638 if (!rv)
6639 rv = write_init_bitmap_imsm_all(st, &info, current_vol);
6640 }
6641
6642 if (!rv)
6643 rv = write_super_imsm(st, 1);
6644 }
6645
6646 return rv;
6647 }
6648
6649 static int store_super_imsm(struct supertype *st, int fd)
6650 {
6651 struct intel_super *super = st->sb;
6652 struct imsm_super *mpb = super ? super->anchor : NULL;
6653
6654 if (!mpb)
6655 return 1;
6656
6657 if (super->sector_size == 4096)
6658 convert_to_4k(super);
6659 return store_imsm_mpb(fd, mpb);
6660 }
6661
6662 static int validate_geometry_imsm_container(struct supertype *st, int level,
6663 int layout, int raiddisks, int chunk,
6664 unsigned long long size,
6665 unsigned long long data_offset,
6666 char *dev,
6667 unsigned long long *freesize,
6668 int verbose)
6669 {
6670 int fd;
6671 unsigned long long ldsize;
6672 struct intel_super *super;
6673 int rv = 0;
6674
6675 if (level != LEVEL_CONTAINER)
6676 return 0;
6677 if (!dev)
6678 return 1;
6679
6680 fd = open(dev, O_RDONLY|O_EXCL, 0);
6681 if (fd < 0) {
6682 if (verbose > 0)
6683 pr_err("imsm: Cannot open %s: %s\n",
6684 dev, strerror(errno));
6685 return 0;
6686 }
6687 if (!get_dev_size(fd, dev, &ldsize)) {
6688 close(fd);
6689 return 0;
6690 }
6691
6692 /* capabilities retrieve could be possible
6693 * note that there is no fd for the disks in array.
6694 */
6695 super = alloc_super();
6696 if (!super) {
6697 close(fd);
6698 return 0;
6699 }
6700 if (!get_dev_sector_size(fd, NULL, &super->sector_size)) {
6701 close(fd);
6702 free_imsm(super);
6703 return 0;
6704 }
6705
6706 rv = find_intel_hba_capability(fd, super, verbose > 0 ? dev : NULL);
6707 if (rv != 0) {
6708 #if DEBUG
6709 char str[256];
6710 fd2devname(fd, str);
6711 dprintf("fd: %d %s orom: %p rv: %d raiddisk: %d\n",
6712 fd, str, super->orom, rv, raiddisks);
6713 #endif
6714 /* no orom/efi or non-intel hba of the disk */
6715 close(fd);
6716 free_imsm(super);
6717 return 0;
6718 }
6719 close(fd);
6720 if (super->orom) {
6721 if (raiddisks > super->orom->tds) {
6722 if (verbose)
6723 pr_err("%d exceeds maximum number of platform supported disks: %d\n",
6724 raiddisks, super->orom->tds);
6725 free_imsm(super);
6726 return 0;
6727 }
6728 if ((super->orom->attr & IMSM_OROM_ATTR_2TB_DISK) == 0 &&
6729 (ldsize >> 9) >> 32 > 0) {
6730 if (verbose)
6731 pr_err("%s exceeds maximum platform supported size\n", dev);
6732 free_imsm(super);
6733 return 0;
6734 }
6735 }
6736
6737 *freesize = avail_size_imsm(st, ldsize >> 9, data_offset);
6738 free_imsm(super);
6739
6740 return 1;
6741 }
6742
6743 static unsigned long long find_size(struct extent *e, int *idx, int num_extents)
6744 {
6745 const unsigned long long base_start = e[*idx].start;
6746 unsigned long long end = base_start + e[*idx].size;
6747 int i;
6748
6749 if (base_start == end)
6750 return 0;
6751
6752 *idx = *idx + 1;
6753 for (i = *idx; i < num_extents; i++) {
6754 /* extend overlapping extents */
6755 if (e[i].start >= base_start &&
6756 e[i].start <= end) {
6757 if (e[i].size == 0)
6758 return 0;
6759 if (e[i].start + e[i].size > end)
6760 end = e[i].start + e[i].size;
6761 } else if (e[i].start > end) {
6762 *idx = i;
6763 break;
6764 }
6765 }
6766
6767 return end - base_start;
6768 }
6769
6770 static unsigned long long merge_extents(struct intel_super *super, int sum_extents)
6771 {
6772 /* build a composite disk with all known extents and generate a new
6773 * 'maxsize' given the "all disks in an array must share a common start
6774 * offset" constraint
6775 */
6776 struct extent *e = xcalloc(sum_extents, sizeof(*e));
6777 struct dl *dl;
6778 int i, j;
6779 int start_extent;
6780 unsigned long long pos;
6781 unsigned long long start = 0;
6782 unsigned long long maxsize;
6783 unsigned long reserve;
6784
6785 /* coalesce and sort all extents. also, check to see if we need to
6786 * reserve space between member arrays
6787 */
6788 j = 0;
6789 for (dl = super->disks; dl; dl = dl->next) {
6790 if (!dl->e)
6791 continue;
6792 for (i = 0; i < dl->extent_cnt; i++)
6793 e[j++] = dl->e[i];
6794 }
6795 qsort(e, sum_extents, sizeof(*e), cmp_extent);
6796
6797 /* merge extents */
6798 i = 0;
6799 j = 0;
6800 while (i < sum_extents) {
6801 e[j].start = e[i].start;
6802 e[j].size = find_size(e, &i, sum_extents);
6803 j++;
6804 if (e[j-1].size == 0)
6805 break;
6806 }
6807
6808 pos = 0;
6809 maxsize = 0;
6810 start_extent = 0;
6811 i = 0;
6812 do {
6813 unsigned long long esize;
6814
6815 esize = e[i].start - pos;
6816 if (esize >= maxsize) {
6817 maxsize = esize;
6818 start = pos;
6819 start_extent = i;
6820 }
6821 pos = e[i].start + e[i].size;
6822 i++;
6823 } while (e[i-1].size);
6824 free(e);
6825
6826 if (maxsize == 0)
6827 return 0;
6828
6829 /* FIXME assumes volume at offset 0 is the first volume in a
6830 * container
6831 */
6832 if (start_extent > 0)
6833 reserve = IMSM_RESERVED_SECTORS; /* gap between raid regions */
6834 else
6835 reserve = 0;
6836
6837 if (maxsize < reserve)
6838 return 0;
6839
6840 super->create_offset = ~((unsigned long long) 0);
6841 if (start + reserve > super->create_offset)
6842 return 0; /* start overflows create_offset */
6843 super->create_offset = start + reserve;
6844
6845 return maxsize - reserve;
6846 }
6847
6848 static int is_raid_level_supported(const struct imsm_orom *orom, int level, int raiddisks)
6849 {
6850 if (level < 0 || level == 6 || level == 4)
6851 return 0;
6852
6853 /* if we have an orom prevent invalid raid levels */
6854 if (orom)
6855 switch (level) {
6856 case 0: return imsm_orom_has_raid0(orom);
6857 case 1:
6858 if (raiddisks > 2)
6859 return imsm_orom_has_raid1e(orom);
6860 return imsm_orom_has_raid1(orom) && raiddisks == 2;
6861 case 10: return imsm_orom_has_raid10(orom) && raiddisks == 4;
6862 case 5: return imsm_orom_has_raid5(orom) && raiddisks > 2;
6863 }
6864 else
6865 return 1; /* not on an Intel RAID platform so anything goes */
6866
6867 return 0;
6868 }
6869
6870 static int
6871 active_arrays_by_format(char *name, char* hba, struct md_list **devlist,
6872 int dpa, int verbose)
6873 {
6874 struct mdstat_ent *mdstat = mdstat_read(0, 0);
6875 struct mdstat_ent *memb;
6876 int count = 0;
6877 int num = 0;
6878 struct md_list *dv;
6879 int found;
6880
6881 for (memb = mdstat ; memb ; memb = memb->next) {
6882 if (memb->metadata_version &&
6883 (strncmp(memb->metadata_version, "external:", 9) == 0) &&
6884 (strcmp(&memb->metadata_version[9], name) == 0) &&
6885 !is_subarray(memb->metadata_version+9) &&
6886 memb->members) {
6887 struct dev_member *dev = memb->members;
6888 int fd = -1;
6889 while(dev && (fd < 0)) {
6890 char *path = xmalloc(strlen(dev->name) + strlen("/dev/") + 1);
6891 num = sprintf(path, "%s%s", "/dev/", dev->name);
6892 if (num > 0)
6893 fd = open(path, O_RDONLY, 0);
6894 if (num <= 0 || fd < 0) {
6895 pr_vrb("Cannot open %s: %s\n",
6896 dev->name, strerror(errno));
6897 }
6898 free(path);
6899 dev = dev->next;
6900 }
6901 found = 0;
6902 if (fd >= 0 && disk_attached_to_hba(fd, hba)) {
6903 struct mdstat_ent *vol;
6904 for (vol = mdstat ; vol ; vol = vol->next) {
6905 if (vol->active > 0 &&
6906 vol->metadata_version &&
6907 is_container_member(vol, memb->devnm)) {
6908 found++;
6909 count++;
6910 }
6911 }
6912 if (*devlist && (found < dpa)) {
6913 dv = xcalloc(1, sizeof(*dv));
6914 dv->devname = xmalloc(strlen(memb->devnm) + strlen("/dev/") + 1);
6915 sprintf(dv->devname, "%s%s", "/dev/", memb->devnm);
6916 dv->found = found;
6917 dv->used = 0;
6918 dv->next = *devlist;
6919 *devlist = dv;
6920 }
6921 }
6922 if (fd >= 0)
6923 close(fd);
6924 }
6925 }
6926 free_mdstat(mdstat);
6927 return count;
6928 }
6929
6930 #ifdef DEBUG_LOOP
6931 static struct md_list*
6932 get_loop_devices(void)
6933 {
6934 int i;
6935 struct md_list *devlist = NULL;
6936 struct md_list *dv;
6937
6938 for(i = 0; i < 12; i++) {
6939 dv = xcalloc(1, sizeof(*dv));
6940 dv->devname = xmalloc(40);
6941 sprintf(dv->devname, "/dev/loop%d", i);
6942 dv->next = devlist;
6943 devlist = dv;
6944 }
6945 return devlist;
6946 }
6947 #endif
6948
6949 static struct md_list*
6950 get_devices(const char *hba_path)
6951 {
6952 struct md_list *devlist = NULL;
6953 struct md_list *dv;
6954 struct dirent *ent;
6955 DIR *dir;
6956 int err = 0;
6957
6958 #if DEBUG_LOOP
6959 devlist = get_loop_devices();
6960 return devlist;
6961 #endif
6962 /* scroll through /sys/dev/block looking for devices attached to
6963 * this hba
6964 */
6965 dir = opendir("/sys/dev/block");
6966 for (ent = dir ? readdir(dir) : NULL; ent; ent = readdir(dir)) {
6967 int fd;
6968 char buf[1024];
6969 int major, minor;
6970 char *path = NULL;
6971 if (sscanf(ent->d_name, "%d:%d", &major, &minor) != 2)
6972 continue;
6973 path = devt_to_devpath(makedev(major, minor));
6974 if (!path)
6975 continue;
6976 if (!path_attached_to_hba(path, hba_path)) {
6977 free(path);
6978 path = NULL;
6979 continue;
6980 }
6981 free(path);
6982 path = NULL;
6983 fd = dev_open(ent->d_name, O_RDONLY);
6984 if (fd >= 0) {
6985 fd2devname(fd, buf);
6986 close(fd);
6987 } else {
6988 pr_err("cannot open device: %s\n",
6989 ent->d_name);
6990 continue;
6991 }
6992
6993 dv = xcalloc(1, sizeof(*dv));
6994 dv->devname = xstrdup(buf);
6995 dv->next = devlist;
6996 devlist = dv;
6997 }
6998 if (err) {
6999 while(devlist) {
7000 dv = devlist;
7001 devlist = devlist->next;
7002 free(dv->devname);
7003 free(dv);
7004 }
7005 }
7006 closedir(dir);
7007 return devlist;
7008 }
7009
7010 static int
7011 count_volumes_list(struct md_list *devlist, char *homehost,
7012 int verbose, int *found)
7013 {
7014 struct md_list *tmpdev;
7015 int count = 0;
7016 struct supertype *st;
7017
7018 /* first walk the list of devices to find a consistent set
7019 * that match the criterea, if that is possible.
7020 * We flag the ones we like with 'used'.
7021 */
7022 *found = 0;
7023 st = match_metadata_desc_imsm("imsm");
7024 if (st == NULL) {
7025 pr_vrb("cannot allocate memory for imsm supertype\n");
7026 return 0;
7027 }
7028
7029 for (tmpdev = devlist; tmpdev; tmpdev = tmpdev->next) {
7030 char *devname = tmpdev->devname;
7031 dev_t rdev;
7032 struct supertype *tst;
7033 int dfd;
7034 if (tmpdev->used > 1)
7035 continue;
7036 tst = dup_super(st);
7037 if (tst == NULL) {
7038 pr_vrb("cannot allocate memory for imsm supertype\n");
7039 goto err_1;
7040 }
7041 tmpdev->container = 0;
7042 dfd = dev_open(devname, O_RDONLY|O_EXCL);
7043 if (dfd < 0) {
7044 dprintf("cannot open device %s: %s\n",
7045 devname, strerror(errno));
7046 tmpdev->used = 2;
7047 } else if (!fstat_is_blkdev(dfd, devname, &rdev)) {
7048 tmpdev->used = 2;
7049 } else if (must_be_container(dfd)) {
7050 struct supertype *cst;
7051 cst = super_by_fd(dfd, NULL);
7052 if (cst == NULL) {
7053 dprintf("cannot recognize container type %s\n",
7054 devname);
7055 tmpdev->used = 2;
7056 } else if (tst->ss != st->ss) {
7057 dprintf("non-imsm container - ignore it: %s\n",
7058 devname);
7059 tmpdev->used = 2;
7060 } else if (!tst->ss->load_container ||
7061 tst->ss->load_container(tst, dfd, NULL))
7062 tmpdev->used = 2;
7063 else {
7064 tmpdev->container = 1;
7065 }
7066 if (cst)
7067 cst->ss->free_super(cst);
7068 } else {
7069 tmpdev->st_rdev = rdev;
7070 if (tst->ss->load_super(tst,dfd, NULL)) {
7071 dprintf("no RAID superblock on %s\n",
7072 devname);
7073 tmpdev->used = 2;
7074 } else if (tst->ss->compare_super == NULL) {
7075 dprintf("Cannot assemble %s metadata on %s\n",
7076 tst->ss->name, devname);
7077 tmpdev->used = 2;
7078 }
7079 }
7080 if (dfd >= 0)
7081 close(dfd);
7082 if (tmpdev->used == 2 || tmpdev->used == 4) {
7083 /* Ignore unrecognised devices during auto-assembly */
7084 goto loop;
7085 }
7086 else {
7087 struct mdinfo info;
7088 tst->ss->getinfo_super(tst, &info, NULL);
7089
7090 if (st->minor_version == -1)
7091 st->minor_version = tst->minor_version;
7092
7093 if (memcmp(info.uuid, uuid_zero,
7094 sizeof(int[4])) == 0) {
7095 /* this is a floating spare. It cannot define
7096 * an array unless there are no more arrays of
7097 * this type to be found. It can be included
7098 * in an array of this type though.
7099 */
7100 tmpdev->used = 3;
7101 goto loop;
7102 }
7103
7104 if (st->ss != tst->ss ||
7105 st->minor_version != tst->minor_version ||
7106 st->ss->compare_super(st, tst, 1) != 0) {
7107 /* Some mismatch. If exactly one array matches this host,
7108 * we can resolve on that one.
7109 * Or, if we are auto assembling, we just ignore the second
7110 * for now.
7111 */
7112 dprintf("superblock on %s doesn't match others - assembly aborted\n",
7113 devname);
7114 goto loop;
7115 }
7116 tmpdev->used = 1;
7117 *found = 1;
7118 dprintf("found: devname: %s\n", devname);
7119 }
7120 loop:
7121 if (tst)
7122 tst->ss->free_super(tst);
7123 }
7124 if (*found != 0) {
7125 int err;
7126 if ((err = load_super_imsm_all(st, -1, &st->sb, NULL, devlist, 0)) == 0) {
7127 struct mdinfo *iter, *head = st->ss->container_content(st, NULL);
7128 for (iter = head; iter; iter = iter->next) {
7129 dprintf("content->text_version: %s vol\n",
7130 iter->text_version);
7131 if (iter->array.state & (1<<MD_SB_BLOCK_VOLUME)) {
7132 /* do not assemble arrays with unsupported
7133 configurations */
7134 dprintf("Cannot activate member %s.\n",
7135 iter->text_version);
7136 } else
7137 count++;
7138 }
7139 sysfs_free(head);
7140
7141 } else {
7142 dprintf("No valid super block on device list: err: %d %p\n",
7143 err, st->sb);
7144 }
7145 } else {
7146 dprintf("no more devices to examine\n");
7147 }
7148
7149 for (tmpdev = devlist; tmpdev; tmpdev = tmpdev->next) {
7150 if (tmpdev->used == 1 && tmpdev->found) {
7151 if (count) {
7152 if (count < tmpdev->found)
7153 count = 0;
7154 else
7155 count -= tmpdev->found;
7156 }
7157 }
7158 if (tmpdev->used == 1)
7159 tmpdev->used = 4;
7160 }
7161 err_1:
7162 if (st)
7163 st->ss->free_super(st);
7164 return count;
7165 }
7166
7167 static int __count_volumes(char *hba_path, int dpa, int verbose,
7168 int cmp_hba_path)
7169 {
7170 struct sys_dev *idev, *intel_devices = find_intel_devices();
7171 int count = 0;
7172 const struct orom_entry *entry;
7173 struct devid_list *dv, *devid_list;
7174
7175 if (!hba_path)
7176 return 0;
7177
7178 for (idev = intel_devices; idev; idev = idev->next) {
7179 if (strstr(idev->path, hba_path))
7180 break;
7181 }
7182
7183 if (!idev || !idev->dev_id)
7184 return 0;
7185
7186 entry = get_orom_entry_by_device_id(idev->dev_id);
7187
7188 if (!entry || !entry->devid_list)
7189 return 0;
7190
7191 devid_list = entry->devid_list;
7192 for (dv = devid_list; dv; dv = dv->next) {
7193 struct md_list *devlist;
7194 struct sys_dev *device = NULL;
7195 char *hpath;
7196 int found = 0;
7197
7198 if (cmp_hba_path)
7199 device = device_by_id_and_path(dv->devid, hba_path);
7200 else
7201 device = device_by_id(dv->devid);
7202
7203 if (device)
7204 hpath = device->path;
7205 else
7206 return 0;
7207
7208 devlist = get_devices(hpath);
7209 /* if no intel devices return zero volumes */
7210 if (devlist == NULL)
7211 return 0;
7212
7213 count += active_arrays_by_format("imsm", hpath, &devlist, dpa,
7214 verbose);
7215 dprintf("path: %s active arrays: %d\n", hpath, count);
7216 if (devlist == NULL)
7217 return 0;
7218 do {
7219 found = 0;
7220 count += count_volumes_list(devlist,
7221 NULL,
7222 verbose,
7223 &found);
7224 dprintf("found %d count: %d\n", found, count);
7225 } while (found);
7226
7227 dprintf("path: %s total number of volumes: %d\n", hpath, count);
7228
7229 while (devlist) {
7230 struct md_list *dv = devlist;
7231 devlist = devlist->next;
7232 free(dv->devname);
7233 free(dv);
7234 }
7235 }
7236 return count;
7237 }
7238
7239 static int count_volumes(struct intel_hba *hba, int dpa, int verbose)
7240 {
7241 if (!hba)
7242 return 0;
7243 if (hba->type == SYS_DEV_VMD) {
7244 struct sys_dev *dev;
7245 int count = 0;
7246
7247 for (dev = find_intel_devices(); dev; dev = dev->next) {
7248 if (dev->type == SYS_DEV_VMD)
7249 count += __count_volumes(dev->path, dpa,
7250 verbose, 1);
7251 }
7252 return count;
7253 }
7254 return __count_volumes(hba->path, dpa, verbose, 0);
7255 }
7256
7257 static int imsm_default_chunk(const struct imsm_orom *orom)
7258 {
7259 /* up to 512 if the plaform supports it, otherwise the platform max.
7260 * 128 if no platform detected
7261 */
7262 int fs = max(7, orom ? fls(orom->sss) : 0);
7263
7264 return min(512, (1 << fs));
7265 }
7266
7267 static int
7268 validate_geometry_imsm_orom(struct intel_super *super, int level, int layout,
7269 int raiddisks, int *chunk, unsigned long long size, int verbose)
7270 {
7271 /* check/set platform and metadata limits/defaults */
7272 if (super->orom && raiddisks > super->orom->dpa) {
7273 pr_vrb("platform supports a maximum of %d disks per array\n",
7274 super->orom->dpa);
7275 return 0;
7276 }
7277
7278 /* capabilities of OROM tested - copied from validate_geometry_imsm_volume */
7279 if (!is_raid_level_supported(super->orom, level, raiddisks)) {
7280 pr_vrb("platform does not support raid%d with %d disk%s\n",
7281 level, raiddisks, raiddisks > 1 ? "s" : "");
7282 return 0;
7283 }
7284
7285 if (*chunk == 0 || *chunk == UnSet)
7286 *chunk = imsm_default_chunk(super->orom);
7287
7288 if (super->orom && !imsm_orom_has_chunk(super->orom, *chunk)) {
7289 pr_vrb("platform does not support a chunk size of: %d\n", *chunk);
7290 return 0;
7291 }
7292
7293 if (layout != imsm_level_to_layout(level)) {
7294 if (level == 5)
7295 pr_vrb("imsm raid 5 only supports the left-asymmetric layout\n");
7296 else if (level == 10)
7297 pr_vrb("imsm raid 10 only supports the n2 layout\n");
7298 else
7299 pr_vrb("imsm unknown layout %#x for this raid level %d\n",
7300 layout, level);
7301 return 0;
7302 }
7303
7304 if (super->orom && (super->orom->attr & IMSM_OROM_ATTR_2TB) == 0 &&
7305 (calc_array_size(level, raiddisks, layout, *chunk, size) >> 32) > 0) {
7306 pr_vrb("platform does not support a volume size over 2TB\n");
7307 return 0;
7308 }
7309
7310 return 1;
7311 }
7312
7313 /* validate_geometry_imsm_volume - lifted from validate_geometry_ddf_bvd
7314 * FIX ME add ahci details
7315 */
7316 static int validate_geometry_imsm_volume(struct supertype *st, int level,
7317 int layout, int raiddisks, int *chunk,
7318 unsigned long long size,
7319 unsigned long long data_offset,
7320 char *dev,
7321 unsigned long long *freesize,
7322 int verbose)
7323 {
7324 dev_t rdev;
7325 struct intel_super *super = st->sb;
7326 struct imsm_super *mpb;
7327 struct dl *dl;
7328 unsigned long long pos = 0;
7329 unsigned long long maxsize;
7330 struct extent *e;
7331 int i;
7332
7333 /* We must have the container info already read in. */
7334 if (!super)
7335 return 0;
7336
7337 mpb = super->anchor;
7338
7339 if (!validate_geometry_imsm_orom(super, level, layout, raiddisks, chunk, size, verbose)) {
7340 pr_err("RAID geometry validation failed. Cannot proceed with the action(s).\n");
7341 return 0;
7342 }
7343 if (!dev) {
7344 /* General test: make sure there is space for
7345 * 'raiddisks' device extents of size 'size' at a given
7346 * offset
7347 */
7348 unsigned long long minsize = size;
7349 unsigned long long start_offset = MaxSector;
7350 int dcnt = 0;
7351 if (minsize == 0)
7352 minsize = MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS;
7353 for (dl = super->disks; dl ; dl = dl->next) {
7354 int found = 0;
7355
7356 pos = 0;
7357 i = 0;
7358 e = get_extents(super, dl, 0);
7359 if (!e) continue;
7360 do {
7361 unsigned long long esize;
7362 esize = e[i].start - pos;
7363 if (esize >= minsize)
7364 found = 1;
7365 if (found && start_offset == MaxSector) {
7366 start_offset = pos;
7367 break;
7368 } else if (found && pos != start_offset) {
7369 found = 0;
7370 break;
7371 }
7372 pos = e[i].start + e[i].size;
7373 i++;
7374 } while (e[i-1].size);
7375 if (found)
7376 dcnt++;
7377 free(e);
7378 }
7379 if (dcnt < raiddisks) {
7380 if (verbose)
7381 pr_err("imsm: Not enough devices with space for this array (%d < %d)\n",
7382 dcnt, raiddisks);
7383 return 0;
7384 }
7385 return 1;
7386 }
7387
7388 /* This device must be a member of the set */
7389 if (!stat_is_blkdev(dev, &rdev))
7390 return 0;
7391 for (dl = super->disks ; dl ; dl = dl->next) {
7392 if (dl->major == (int)major(rdev) &&
7393 dl->minor == (int)minor(rdev))
7394 break;
7395 }
7396 if (!dl) {
7397 if (verbose)
7398 pr_err("%s is not in the same imsm set\n", dev);
7399 return 0;
7400 } else if (super->orom && dl->index < 0 && mpb->num_raid_devs) {
7401 /* If a volume is present then the current creation attempt
7402 * cannot incorporate new spares because the orom may not
7403 * understand this configuration (all member disks must be
7404 * members of each array in the container).
7405 */
7406 pr_err("%s is a spare and a volume is already defined for this container\n", dev);
7407 pr_err("The option-rom requires all member disks to be a member of all volumes\n");
7408 return 0;
7409 } else if (super->orom && mpb->num_raid_devs > 0 &&
7410 mpb->num_disks != raiddisks) {
7411 pr_err("The option-rom requires all member disks to be a member of all volumes\n");
7412 return 0;
7413 }
7414
7415 /* retrieve the largest free space block */
7416 e = get_extents(super, dl, 0);
7417 maxsize = 0;
7418 i = 0;
7419 if (e) {
7420 do {
7421 unsigned long long esize;
7422
7423 esize = e[i].start - pos;
7424 if (esize >= maxsize)
7425 maxsize = esize;
7426 pos = e[i].start + e[i].size;
7427 i++;
7428 } while (e[i-1].size);
7429 dl->e = e;
7430 dl->extent_cnt = i;
7431 } else {
7432 if (verbose)
7433 pr_err("unable to determine free space for: %s\n",
7434 dev);
7435 return 0;
7436 }
7437 if (maxsize < size) {
7438 if (verbose)
7439 pr_err("%s not enough space (%llu < %llu)\n",
7440 dev, maxsize, size);
7441 return 0;
7442 }
7443
7444 /* count total number of extents for merge */
7445 i = 0;
7446 for (dl = super->disks; dl; dl = dl->next)
7447 if (dl->e)
7448 i += dl->extent_cnt;
7449
7450 maxsize = merge_extents(super, i);
7451
7452 if (mpb->num_raid_devs > 0 && size && size != maxsize)
7453 pr_err("attempting to create a second volume with size less then remaining space.\n");
7454
7455 if (maxsize < size || maxsize == 0) {
7456 if (verbose) {
7457 if (maxsize == 0)
7458 pr_err("no free space left on device. Aborting...\n");
7459 else
7460 pr_err("not enough space to create volume of given size (%llu < %llu). Aborting...\n",
7461 maxsize, size);
7462 }
7463 return 0;
7464 }
7465
7466 *freesize = maxsize;
7467
7468 if (super->orom) {
7469 int count = count_volumes(super->hba,
7470 super->orom->dpa, verbose);
7471 if (super->orom->vphba <= count) {
7472 pr_vrb("platform does not support more than %d raid volumes.\n",
7473 super->orom->vphba);
7474 return 0;
7475 }
7476 }
7477 return 1;
7478 }
7479
7480 static int imsm_get_free_size(struct supertype *st, int raiddisks,
7481 unsigned long long size, int chunk,
7482 unsigned long long *freesize)
7483 {
7484 struct intel_super *super = st->sb;
7485 struct imsm_super *mpb = super->anchor;
7486 struct dl *dl;
7487 int i;
7488 int extent_cnt;
7489 struct extent *e;
7490 unsigned long long maxsize;
7491 unsigned long long minsize;
7492 int cnt;
7493 int used;
7494
7495 /* find the largest common start free region of the possible disks */
7496 used = 0;
7497 extent_cnt = 0;
7498 cnt = 0;
7499 for (dl = super->disks; dl; dl = dl->next) {
7500 dl->raiddisk = -1;
7501
7502 if (dl->index >= 0)
7503 used++;
7504
7505 /* don't activate new spares if we are orom constrained
7506 * and there is already a volume active in the container
7507 */
7508 if (super->orom && dl->index < 0 && mpb->num_raid_devs)
7509 continue;
7510
7511 e = get_extents(super, dl, 0);
7512 if (!e)
7513 continue;
7514 for (i = 1; e[i-1].size; i++)
7515 ;
7516 dl->e = e;
7517 dl->extent_cnt = i;
7518 extent_cnt += i;
7519 cnt++;
7520 }
7521
7522 maxsize = merge_extents(super, extent_cnt);
7523 minsize = size;
7524 if (size == 0)
7525 /* chunk is in K */
7526 minsize = chunk * 2;
7527
7528 if (cnt < raiddisks ||
7529 (super->orom && used && used != raiddisks) ||
7530 maxsize < minsize ||
7531 maxsize == 0) {
7532 pr_err("not enough devices with space to create array.\n");
7533 return 0; /* No enough free spaces large enough */
7534 }
7535
7536 if (size == 0) {
7537 size = maxsize;
7538 if (chunk) {
7539 size /= 2 * chunk;
7540 size *= 2 * chunk;
7541 }
7542 maxsize = size;
7543 }
7544 if (mpb->num_raid_devs > 0 && size && size != maxsize)
7545 pr_err("attempting to create a second volume with size less then remaining space.\n");
7546 cnt = 0;
7547 for (dl = super->disks; dl; dl = dl->next)
7548 if (dl->e)
7549 dl->raiddisk = cnt++;
7550
7551 *freesize = size;
7552
7553 dprintf("imsm: imsm_get_free_size() returns : %llu\n", size);
7554
7555 return 1;
7556 }
7557
7558 static int reserve_space(struct supertype *st, int raiddisks,
7559 unsigned long long size, int chunk,
7560 unsigned long long *freesize)
7561 {
7562 struct intel_super *super = st->sb;
7563 struct dl *dl;
7564 int cnt;
7565 int rv = 0;
7566
7567 rv = imsm_get_free_size(st, raiddisks, size, chunk, freesize);
7568 if (rv) {
7569 cnt = 0;
7570 for (dl = super->disks; dl; dl = dl->next)
7571 if (dl->e)
7572 dl->raiddisk = cnt++;
7573 rv = 1;
7574 }
7575
7576 return rv;
7577 }
7578
7579 static int validate_geometry_imsm(struct supertype *st, int level, int layout,
7580 int raiddisks, int *chunk, unsigned long long size,
7581 unsigned long long data_offset,
7582 char *dev, unsigned long long *freesize,
7583 int consistency_policy, int verbose)
7584 {
7585 int fd, cfd;
7586 struct mdinfo *sra;
7587 int is_member = 0;
7588
7589 /* load capability
7590 * if given unused devices create a container
7591 * if given given devices in a container create a member volume
7592 */
7593 if (level == LEVEL_CONTAINER) {
7594 /* Must be a fresh device to add to a container */
7595 return validate_geometry_imsm_container(st, level, layout,
7596 raiddisks,
7597 *chunk,
7598 size, data_offset,
7599 dev, freesize,
7600 verbose);
7601 }
7602
7603 /*
7604 * Size is given in sectors.
7605 */
7606 if (size && (size < 2048)) {
7607 pr_err("Given size must be greater than 1M.\n");
7608 /* Depends on algorithm in Create.c :
7609 * if container was given (dev == NULL) return -1,
7610 * if block device was given ( dev != NULL) return 0.
7611 */
7612 return dev ? -1 : 0;
7613 }
7614
7615 if (!dev) {
7616 if (st->sb) {
7617 struct intel_super *super = st->sb;
7618 if (!validate_geometry_imsm_orom(st->sb, level, layout,
7619 raiddisks, chunk, size,
7620 verbose))
7621 return 0;
7622 /* we are being asked to automatically layout a
7623 * new volume based on the current contents of
7624 * the container. If the the parameters can be
7625 * satisfied reserve_space will record the disks,
7626 * start offset, and size of the volume to be
7627 * created. add_to_super and getinfo_super
7628 * detect when autolayout is in progress.
7629 */
7630 /* assuming that freesize is always given when array is
7631 created */
7632 if (super->orom && freesize) {
7633 int count;
7634 count = count_volumes(super->hba,
7635 super->orom->dpa, verbose);
7636 if (super->orom->vphba <= count) {
7637 pr_vrb("platform does not support more than %d raid volumes.\n",
7638 super->orom->vphba);
7639 return 0;
7640 }
7641 }
7642 if (freesize)
7643 return reserve_space(st, raiddisks, size,
7644 *chunk, freesize);
7645 }
7646 return 1;
7647 }
7648 if (st->sb) {
7649 /* creating in a given container */
7650 return validate_geometry_imsm_volume(st, level, layout,
7651 raiddisks, chunk, size,
7652 data_offset,
7653 dev, freesize, verbose);
7654 }
7655
7656 /* This device needs to be a device in an 'imsm' container */
7657 fd = open(dev, O_RDONLY|O_EXCL, 0);
7658 if (fd >= 0) {
7659 if (verbose)
7660 pr_err("Cannot create this array on device %s\n",
7661 dev);
7662 close(fd);
7663 return 0;
7664 }
7665 if (errno != EBUSY || (fd = open(dev, O_RDONLY, 0)) < 0) {
7666 if (verbose)
7667 pr_err("Cannot open %s: %s\n",
7668 dev, strerror(errno));
7669 return 0;
7670 }
7671 /* Well, it is in use by someone, maybe an 'imsm' container. */
7672 cfd = open_container(fd);
7673 close(fd);
7674 if (cfd < 0) {
7675 if (verbose)
7676 pr_err("Cannot use %s: It is busy\n",
7677 dev);
7678 return 0;
7679 }
7680 sra = sysfs_read(cfd, NULL, GET_VERSION);
7681 if (sra && sra->array.major_version == -1 &&
7682 strcmp(sra->text_version, "imsm") == 0)
7683 is_member = 1;
7684 sysfs_free(sra);
7685 if (is_member) {
7686 /* This is a member of a imsm container. Load the container
7687 * and try to create a volume
7688 */
7689 struct intel_super *super;
7690
7691 if (load_super_imsm_all(st, cfd, (void **) &super, NULL, NULL, 1) == 0) {
7692 st->sb = super;
7693 strcpy(st->container_devnm, fd2devnm(cfd));
7694 close(cfd);
7695 return validate_geometry_imsm_volume(st, level, layout,
7696 raiddisks, chunk,
7697 size, data_offset, dev,
7698 freesize, 1)
7699 ? 1 : -1;
7700 }
7701 }
7702
7703 if (verbose)
7704 pr_err("failed container membership check\n");
7705
7706 close(cfd);
7707 return 0;
7708 }
7709
7710 static void default_geometry_imsm(struct supertype *st, int *level, int *layout, int *chunk)
7711 {
7712 struct intel_super *super = st->sb;
7713
7714 if (level && *level == UnSet)
7715 *level = LEVEL_CONTAINER;
7716
7717 if (level && layout && *layout == UnSet)
7718 *layout = imsm_level_to_layout(*level);
7719
7720 if (chunk && (*chunk == UnSet || *chunk == 0))
7721 *chunk = imsm_default_chunk(super->orom);
7722 }
7723
7724 static void handle_missing(struct intel_super *super, struct imsm_dev *dev);
7725
7726 static int kill_subarray_imsm(struct supertype *st, char *subarray_id)
7727 {
7728 /* remove the subarray currently referenced by subarray_id */
7729 __u8 i;
7730 struct intel_dev **dp;
7731 struct intel_super *super = st->sb;
7732 __u8 current_vol = strtoul(subarray_id, NULL, 10);
7733 struct imsm_super *mpb = super->anchor;
7734
7735 if (mpb->num_raid_devs == 0)
7736 return 2;
7737
7738 /* block deletions that would change the uuid of active subarrays
7739 *
7740 * FIXME when immutable ids are available, but note that we'll
7741 * also need to fixup the invalidated/active subarray indexes in
7742 * mdstat
7743 */
7744 for (i = 0; i < mpb->num_raid_devs; i++) {
7745 char subarray[4];
7746
7747 if (i < current_vol)
7748 continue;
7749 sprintf(subarray, "%u", i);
7750 if (is_subarray_active(subarray, st->devnm)) {
7751 pr_err("deleting subarray-%d would change the UUID of active subarray-%d, aborting\n",
7752 current_vol, i);
7753
7754 return 2;
7755 }
7756 }
7757
7758 if (st->update_tail) {
7759 struct imsm_update_kill_array *u = xmalloc(sizeof(*u));
7760
7761 u->type = update_kill_array;
7762 u->dev_idx = current_vol;
7763 append_metadata_update(st, u, sizeof(*u));
7764
7765 return 0;
7766 }
7767
7768 for (dp = &super->devlist; *dp;)
7769 if ((*dp)->index == current_vol) {
7770 *dp = (*dp)->next;
7771 } else {
7772 handle_missing(super, (*dp)->dev);
7773 if ((*dp)->index > current_vol)
7774 (*dp)->index--;
7775 dp = &(*dp)->next;
7776 }
7777
7778 /* no more raid devices, all active components are now spares,
7779 * but of course failed are still failed
7780 */
7781 if (--mpb->num_raid_devs == 0) {
7782 struct dl *d;
7783
7784 for (d = super->disks; d; d = d->next)
7785 if (d->index > -2)
7786 mark_spare(d);
7787 }
7788
7789 super->updates_pending++;
7790
7791 return 0;
7792 }
7793
7794 static int get_rwh_policy_from_update(char *update)
7795 {
7796 if (strcmp(update, "ppl") == 0)
7797 return RWH_MULTIPLE_DISTRIBUTED;
7798 else if (strcmp(update, "no-ppl") == 0)
7799 return RWH_MULTIPLE_OFF;
7800 else if (strcmp(update, "bitmap") == 0)
7801 return RWH_BITMAP;
7802 else if (strcmp(update, "no-bitmap") == 0)
7803 return RWH_OFF;
7804 return -1;
7805 }
7806
7807 static int update_subarray_imsm(struct supertype *st, char *subarray,
7808 char *update, struct mddev_ident *ident)
7809 {
7810 /* update the subarray currently referenced by ->current_vol */
7811 struct intel_super *super = st->sb;
7812 struct imsm_super *mpb = super->anchor;
7813
7814 if (strcmp(update, "name") == 0) {
7815 char *name = ident->name;
7816 char *ep;
7817 int vol;
7818
7819 if (is_subarray_active(subarray, st->devnm)) {
7820 pr_err("Unable to update name of active subarray\n");
7821 return 2;
7822 }
7823
7824 if (!check_name(super, name, 0))
7825 return 2;
7826
7827 vol = strtoul(subarray, &ep, 10);
7828 if (*ep != '\0' || vol >= super->anchor->num_raid_devs)
7829 return 2;
7830
7831 if (st->update_tail) {
7832 struct imsm_update_rename_array *u = xmalloc(sizeof(*u));
7833
7834 u->type = update_rename_array;
7835 u->dev_idx = vol;
7836 strncpy((char *) u->name, name, MAX_RAID_SERIAL_LEN);
7837 u->name[MAX_RAID_SERIAL_LEN-1] = '\0';
7838 append_metadata_update(st, u, sizeof(*u));
7839 } else {
7840 struct imsm_dev *dev;
7841 int i, namelen;
7842
7843 dev = get_imsm_dev(super, vol);
7844 memset(dev->volume, '\0', MAX_RAID_SERIAL_LEN);
7845 namelen = min((int)strlen(name), MAX_RAID_SERIAL_LEN);
7846 memcpy(dev->volume, name, namelen);
7847 for (i = 0; i < mpb->num_raid_devs; i++) {
7848 dev = get_imsm_dev(super, i);
7849 handle_missing(super, dev);
7850 }
7851 super->updates_pending++;
7852 }
7853 } else if (get_rwh_policy_from_update(update) != -1) {
7854 int new_policy;
7855 char *ep;
7856 int vol = strtoul(subarray, &ep, 10);
7857
7858 if (*ep != '\0' || vol >= super->anchor->num_raid_devs)
7859 return 2;
7860
7861 new_policy = get_rwh_policy_from_update(update);
7862
7863 if (st->update_tail) {
7864 struct imsm_update_rwh_policy *u = xmalloc(sizeof(*u));
7865
7866 u->type = update_rwh_policy;
7867 u->dev_idx = vol;
7868 u->new_policy = new_policy;
7869 append_metadata_update(st, u, sizeof(*u));
7870 } else {
7871 struct imsm_dev *dev;
7872
7873 dev = get_imsm_dev(super, vol);
7874 dev->rwh_policy = new_policy;
7875 super->updates_pending++;
7876 }
7877 if (new_policy == RWH_BITMAP)
7878 return write_init_bitmap_imsm_vol(st, vol);
7879 } else
7880 return 2;
7881
7882 return 0;
7883 }
7884
7885 static int is_gen_migration(struct imsm_dev *dev)
7886 {
7887 if (dev == NULL)
7888 return 0;
7889
7890 if (!dev->vol.migr_state)
7891 return 0;
7892
7893 if (migr_type(dev) == MIGR_GEN_MIGR)
7894 return 1;
7895
7896 return 0;
7897 }
7898
7899 static int is_rebuilding(struct imsm_dev *dev)
7900 {
7901 struct imsm_map *migr_map;
7902
7903 if (!dev->vol.migr_state)
7904 return 0;
7905
7906 if (migr_type(dev) != MIGR_REBUILD)
7907 return 0;
7908
7909 migr_map = get_imsm_map(dev, MAP_1);
7910
7911 if (migr_map->map_state == IMSM_T_STATE_DEGRADED)
7912 return 1;
7913 else
7914 return 0;
7915 }
7916
7917 static int is_initializing(struct imsm_dev *dev)
7918 {
7919 struct imsm_map *migr_map;
7920
7921 if (!dev->vol.migr_state)
7922 return 0;
7923
7924 if (migr_type(dev) != MIGR_INIT)
7925 return 0;
7926
7927 migr_map = get_imsm_map(dev, MAP_1);
7928
7929 if (migr_map->map_state == IMSM_T_STATE_UNINITIALIZED)
7930 return 1;
7931
7932 return 0;
7933 }
7934
7935 static void update_recovery_start(struct intel_super *super,
7936 struct imsm_dev *dev,
7937 struct mdinfo *array)
7938 {
7939 struct mdinfo *rebuild = NULL;
7940 struct mdinfo *d;
7941 __u32 units;
7942
7943 if (!is_rebuilding(dev))
7944 return;
7945
7946 /* Find the rebuild target, but punt on the dual rebuild case */
7947 for (d = array->devs; d; d = d->next)
7948 if (d->recovery_start == 0) {
7949 if (rebuild)
7950 return;
7951 rebuild = d;
7952 }
7953
7954 if (!rebuild) {
7955 /* (?) none of the disks are marked with
7956 * IMSM_ORD_REBUILD, so assume they are missing and the
7957 * disk_ord_tbl was not correctly updated
7958 */
7959 dprintf("failed to locate out-of-sync disk\n");
7960 return;
7961 }
7962
7963 units = vol_curr_migr_unit(dev);
7964 rebuild->recovery_start = units * blocks_per_migr_unit(super, dev);
7965 }
7966
7967 static int recover_backup_imsm(struct supertype *st, struct mdinfo *info);
7968
7969 static struct mdinfo *container_content_imsm(struct supertype *st, char *subarray)
7970 {
7971 /* Given a container loaded by load_super_imsm_all,
7972 * extract information about all the arrays into
7973 * an mdinfo tree.
7974 * If 'subarray' is given, just extract info about that array.
7975 *
7976 * For each imsm_dev create an mdinfo, fill it in,
7977 * then look for matching devices in super->disks
7978 * and create appropriate device mdinfo.
7979 */
7980 struct intel_super *super = st->sb;
7981 struct imsm_super *mpb = super->anchor;
7982 struct mdinfo *rest = NULL;
7983 unsigned int i;
7984 int sb_errors = 0;
7985 struct dl *d;
7986 int spare_disks = 0;
7987 int current_vol = super->current_vol;
7988
7989 /* do not assemble arrays when not all attributes are supported */
7990 if (imsm_check_attributes(mpb->attributes) == 0) {
7991 sb_errors = 1;
7992 pr_err("Unsupported attributes in IMSM metadata.Arrays activation is blocked.\n");
7993 }
7994
7995 /* count spare devices, not used in maps
7996 */
7997 for (d = super->disks; d; d = d->next)
7998 if (d->index == -1)
7999 spare_disks++;
8000
8001 for (i = 0; i < mpb->num_raid_devs; i++) {
8002 struct imsm_dev *dev;
8003 struct imsm_map *map;
8004 struct imsm_map *map2;
8005 struct mdinfo *this;
8006 int slot;
8007 int chunk;
8008 char *ep;
8009 int level;
8010
8011 if (subarray &&
8012 (i != strtoul(subarray, &ep, 10) || *ep != '\0'))
8013 continue;
8014
8015 dev = get_imsm_dev(super, i);
8016 map = get_imsm_map(dev, MAP_0);
8017 map2 = get_imsm_map(dev, MAP_1);
8018 level = get_imsm_raid_level(map);
8019
8020 /* do not publish arrays that are in the middle of an
8021 * unsupported migration
8022 */
8023 if (dev->vol.migr_state &&
8024 (migr_type(dev) == MIGR_STATE_CHANGE)) {
8025 pr_err("cannot assemble volume '%.16s': unsupported migration in progress\n",
8026 dev->volume);
8027 continue;
8028 }
8029 /* do not publish arrays that are not support by controller's
8030 * OROM/EFI
8031 */
8032
8033 this = xmalloc(sizeof(*this));
8034
8035 super->current_vol = i;
8036 getinfo_super_imsm_volume(st, this, NULL);
8037 this->next = rest;
8038 chunk = __le16_to_cpu(map->blocks_per_strip) >> 1;
8039 /* mdadm does not support all metadata features- set the bit in all arrays state */
8040 if (!validate_geometry_imsm_orom(super,
8041 level, /* RAID level */
8042 imsm_level_to_layout(level),
8043 map->num_members, /* raid disks */
8044 &chunk, imsm_dev_size(dev),
8045 1 /* verbose */)) {
8046 pr_err("IMSM RAID geometry validation failed. Array %s activation is blocked.\n",
8047 dev->volume);
8048 this->array.state |=
8049 (1<<MD_SB_BLOCK_CONTAINER_RESHAPE) |
8050 (1<<MD_SB_BLOCK_VOLUME);
8051 }
8052
8053 /* if array has bad blocks, set suitable bit in all arrays state */
8054 if (sb_errors)
8055 this->array.state |=
8056 (1<<MD_SB_BLOCK_CONTAINER_RESHAPE) |
8057 (1<<MD_SB_BLOCK_VOLUME);
8058
8059 for (slot = 0 ; slot < map->num_members; slot++) {
8060 unsigned long long recovery_start;
8061 struct mdinfo *info_d;
8062 struct dl *d;
8063 int idx;
8064 int skip;
8065 __u32 ord;
8066 int missing = 0;
8067
8068 skip = 0;
8069 idx = get_imsm_disk_idx(dev, slot, MAP_0);
8070 ord = get_imsm_ord_tbl_ent(dev, slot, MAP_X);
8071 for (d = super->disks; d ; d = d->next)
8072 if (d->index == idx)
8073 break;
8074
8075 recovery_start = MaxSector;
8076 if (d == NULL)
8077 skip = 1;
8078 if (d && is_failed(&d->disk))
8079 skip = 1;
8080 if (!skip && (ord & IMSM_ORD_REBUILD))
8081 recovery_start = 0;
8082 if (!(ord & IMSM_ORD_REBUILD))
8083 this->array.working_disks++;
8084 /*
8085 * if we skip some disks the array will be assmebled degraded;
8086 * reset resync start to avoid a dirty-degraded
8087 * situation when performing the intial sync
8088 */
8089 if (skip)
8090 missing++;
8091
8092 if (!(dev->vol.dirty & RAIDVOL_DIRTY)) {
8093 if ((!able_to_resync(level, missing) ||
8094 recovery_start == 0))
8095 this->resync_start = MaxSector;
8096 } else {
8097 /*
8098 * FIXME handle dirty degraded
8099 */
8100 }
8101
8102 if (skip)
8103 continue;
8104
8105 info_d = xcalloc(1, sizeof(*info_d));
8106 info_d->next = this->devs;
8107 this->devs = info_d;
8108
8109 info_d->disk.number = d->index;
8110 info_d->disk.major = d->major;
8111 info_d->disk.minor = d->minor;
8112 info_d->disk.raid_disk = slot;
8113 info_d->recovery_start = recovery_start;
8114 if (map2) {
8115 if (slot < map2->num_members)
8116 info_d->disk.state = (1 << MD_DISK_ACTIVE);
8117 else
8118 this->array.spare_disks++;
8119 } else {
8120 if (slot < map->num_members)
8121 info_d->disk.state = (1 << MD_DISK_ACTIVE);
8122 else
8123 this->array.spare_disks++;
8124 }
8125
8126 info_d->events = __le32_to_cpu(mpb->generation_num);
8127 info_d->data_offset = pba_of_lba0(map);
8128 info_d->component_size = calc_component_size(map, dev);
8129
8130 if (map->raid_level == 5) {
8131 info_d->ppl_sector = this->ppl_sector;
8132 info_d->ppl_size = this->ppl_size;
8133 if (this->consistency_policy == CONSISTENCY_POLICY_PPL &&
8134 recovery_start == 0)
8135 this->resync_start = 0;
8136 }
8137
8138 info_d->bb.supported = 1;
8139 get_volume_badblocks(super->bbm_log, ord_to_idx(ord),
8140 info_d->data_offset,
8141 info_d->component_size,
8142 &info_d->bb);
8143 }
8144 /* now that the disk list is up-to-date fixup recovery_start */
8145 update_recovery_start(super, dev, this);
8146 this->array.spare_disks += spare_disks;
8147
8148 /* check for reshape */
8149 if (this->reshape_active == 1)
8150 recover_backup_imsm(st, this);
8151 rest = this;
8152 }
8153
8154 super->current_vol = current_vol;
8155 return rest;
8156 }
8157
8158 static __u8 imsm_check_degraded(struct intel_super *super, struct imsm_dev *dev,
8159 int failed, int look_in_map)
8160 {
8161 struct imsm_map *map;
8162
8163 map = get_imsm_map(dev, look_in_map);
8164
8165 if (!failed)
8166 return map->map_state == IMSM_T_STATE_UNINITIALIZED ?
8167 IMSM_T_STATE_UNINITIALIZED : IMSM_T_STATE_NORMAL;
8168
8169 switch (get_imsm_raid_level(map)) {
8170 case 0:
8171 return IMSM_T_STATE_FAILED;
8172 break;
8173 case 1:
8174 if (failed < map->num_members)
8175 return IMSM_T_STATE_DEGRADED;
8176 else
8177 return IMSM_T_STATE_FAILED;
8178 break;
8179 case 10:
8180 {
8181 /**
8182 * check to see if any mirrors have failed, otherwise we
8183 * are degraded. Even numbered slots are mirrored on
8184 * slot+1
8185 */
8186 int i;
8187 /* gcc -Os complains that this is unused */
8188 int insync = insync;
8189
8190 for (i = 0; i < map->num_members; i++) {
8191 __u32 ord = get_imsm_ord_tbl_ent(dev, i, MAP_X);
8192 int idx = ord_to_idx(ord);
8193 struct imsm_disk *disk;
8194
8195 /* reset the potential in-sync count on even-numbered
8196 * slots. num_copies is always 2 for imsm raid10
8197 */
8198 if ((i & 1) == 0)
8199 insync = 2;
8200
8201 disk = get_imsm_disk(super, idx);
8202 if (!disk || is_failed(disk) || ord & IMSM_ORD_REBUILD)
8203 insync--;
8204
8205 /* no in-sync disks left in this mirror the
8206 * array has failed
8207 */
8208 if (insync == 0)
8209 return IMSM_T_STATE_FAILED;
8210 }
8211
8212 return IMSM_T_STATE_DEGRADED;
8213 }
8214 case 5:
8215 if (failed < 2)
8216 return IMSM_T_STATE_DEGRADED;
8217 else
8218 return IMSM_T_STATE_FAILED;
8219 break;
8220 default:
8221 break;
8222 }
8223
8224 return map->map_state;
8225 }
8226
8227 static int imsm_count_failed(struct intel_super *super, struct imsm_dev *dev,
8228 int look_in_map)
8229 {
8230 int i;
8231 int failed = 0;
8232 struct imsm_disk *disk;
8233 struct imsm_map *map = get_imsm_map(dev, MAP_0);
8234 struct imsm_map *prev = get_imsm_map(dev, MAP_1);
8235 struct imsm_map *map_for_loop;
8236 __u32 ord;
8237 int idx;
8238 int idx_1;
8239
8240 /* at the beginning of migration we set IMSM_ORD_REBUILD on
8241 * disks that are being rebuilt. New failures are recorded to
8242 * map[0]. So we look through all the disks we started with and
8243 * see if any failures are still present, or if any new ones
8244 * have arrived
8245 */
8246 map_for_loop = map;
8247 if (prev && (map->num_members < prev->num_members))
8248 map_for_loop = prev;
8249
8250 for (i = 0; i < map_for_loop->num_members; i++) {
8251 idx_1 = -255;
8252 /* when MAP_X is passed both maps failures are counted
8253 */
8254 if (prev &&
8255 (look_in_map == MAP_1 || look_in_map == MAP_X) &&
8256 i < prev->num_members) {
8257 ord = __le32_to_cpu(prev->disk_ord_tbl[i]);
8258 idx_1 = ord_to_idx(ord);
8259
8260 disk = get_imsm_disk(super, idx_1);
8261 if (!disk || is_failed(disk) || ord & IMSM_ORD_REBUILD)
8262 failed++;
8263 }
8264 if ((look_in_map == MAP_0 || look_in_map == MAP_X) &&
8265 i < map->num_members) {
8266 ord = __le32_to_cpu(map->disk_ord_tbl[i]);
8267 idx = ord_to_idx(ord);
8268
8269 if (idx != idx_1) {
8270 disk = get_imsm_disk(super, idx);
8271 if (!disk || is_failed(disk) ||
8272 ord & IMSM_ORD_REBUILD)
8273 failed++;
8274 }
8275 }
8276 }
8277
8278 return failed;
8279 }
8280
8281 static int imsm_open_new(struct supertype *c, struct active_array *a,
8282 char *inst)
8283 {
8284 struct intel_super *super = c->sb;
8285 struct imsm_super *mpb = super->anchor;
8286 struct imsm_update_prealloc_bb_mem u;
8287
8288 if (atoi(inst) >= mpb->num_raid_devs) {
8289 pr_err("subarry index %d, out of range\n", atoi(inst));
8290 return -ENODEV;
8291 }
8292
8293 dprintf("imsm: open_new %s\n", inst);
8294 a->info.container_member = atoi(inst);
8295
8296 u.type = update_prealloc_badblocks_mem;
8297 imsm_update_metadata_locally(c, &u, sizeof(u));
8298
8299 return 0;
8300 }
8301
8302 static int is_resyncing(struct imsm_dev *dev)
8303 {
8304 struct imsm_map *migr_map;
8305
8306 if (!dev->vol.migr_state)
8307 return 0;
8308
8309 if (migr_type(dev) == MIGR_INIT ||
8310 migr_type(dev) == MIGR_REPAIR)
8311 return 1;
8312
8313 if (migr_type(dev) == MIGR_GEN_MIGR)
8314 return 0;
8315
8316 migr_map = get_imsm_map(dev, MAP_1);
8317
8318 if (migr_map->map_state == IMSM_T_STATE_NORMAL &&
8319 dev->vol.migr_type != MIGR_GEN_MIGR)
8320 return 1;
8321 else
8322 return 0;
8323 }
8324
8325 /* return true if we recorded new information */
8326 static int mark_failure(struct intel_super *super,
8327 struct imsm_dev *dev, struct imsm_disk *disk, int idx)
8328 {
8329 __u32 ord;
8330 int slot;
8331 struct imsm_map *map;
8332 char buf[MAX_RAID_SERIAL_LEN+3];
8333 unsigned int len, shift = 0;
8334
8335 /* new failures are always set in map[0] */
8336 map = get_imsm_map(dev, MAP_0);
8337
8338 slot = get_imsm_disk_slot(map, idx);
8339 if (slot < 0)
8340 return 0;
8341
8342 ord = __le32_to_cpu(map->disk_ord_tbl[slot]);
8343 if (is_failed(disk) && (ord & IMSM_ORD_REBUILD))
8344 return 0;
8345
8346 memcpy(buf, disk->serial, MAX_RAID_SERIAL_LEN);
8347 buf[MAX_RAID_SERIAL_LEN] = '\000';
8348 strcat(buf, ":0");
8349 if ((len = strlen(buf)) >= MAX_RAID_SERIAL_LEN)
8350 shift = len - MAX_RAID_SERIAL_LEN + 1;
8351 memcpy(disk->serial, &buf[shift], len + 1 - shift);
8352
8353 disk->status |= FAILED_DISK;
8354 set_imsm_ord_tbl_ent(map, slot, idx | IMSM_ORD_REBUILD);
8355 /* mark failures in second map if second map exists and this disk
8356 * in this slot.
8357 * This is valid for migration, initialization and rebuild
8358 */
8359 if (dev->vol.migr_state) {
8360 struct imsm_map *map2 = get_imsm_map(dev, MAP_1);
8361 int slot2 = get_imsm_disk_slot(map2, idx);
8362
8363 if (slot2 < map2->num_members && slot2 >= 0)
8364 set_imsm_ord_tbl_ent(map2, slot2,
8365 idx | IMSM_ORD_REBUILD);
8366 }
8367 if (map->failed_disk_num == 0xff ||
8368 (!is_rebuilding(dev) && map->failed_disk_num > slot))
8369 map->failed_disk_num = slot;
8370
8371 clear_disk_badblocks(super->bbm_log, ord_to_idx(ord));
8372
8373 return 1;
8374 }
8375
8376 static void mark_missing(struct intel_super *super,
8377 struct imsm_dev *dev, struct imsm_disk *disk, int idx)
8378 {
8379 mark_failure(super, dev, disk, idx);
8380
8381 if (disk->scsi_id == __cpu_to_le32(~(__u32)0))
8382 return;
8383
8384 disk->scsi_id = __cpu_to_le32(~(__u32)0);
8385 memmove(&disk->serial[0], &disk->serial[1], MAX_RAID_SERIAL_LEN - 1);
8386 }
8387
8388 static void handle_missing(struct intel_super *super, struct imsm_dev *dev)
8389 {
8390 struct dl *dl;
8391
8392 if (!super->missing)
8393 return;
8394
8395 /* When orom adds replacement for missing disk it does
8396 * not remove entry of missing disk, but just updates map with
8397 * new added disk. So it is not enough just to test if there is
8398 * any missing disk, we have to look if there are any failed disks
8399 * in map to stop migration */
8400
8401 dprintf("imsm: mark missing\n");
8402 /* end process for initialization and rebuild only
8403 */
8404 if (is_gen_migration(dev) == 0) {
8405 int failed = imsm_count_failed(super, dev, MAP_0);
8406
8407 if (failed) {
8408 __u8 map_state;
8409 struct imsm_map *map = get_imsm_map(dev, MAP_0);
8410 struct imsm_map *map1;
8411 int i, ord, ord_map1;
8412 int rebuilt = 1;
8413
8414 for (i = 0; i < map->num_members; i++) {
8415 ord = get_imsm_ord_tbl_ent(dev, i, MAP_0);
8416 if (!(ord & IMSM_ORD_REBUILD))
8417 continue;
8418
8419 map1 = get_imsm_map(dev, MAP_1);
8420 if (!map1)
8421 continue;
8422
8423 ord_map1 = __le32_to_cpu(map1->disk_ord_tbl[i]);
8424 if (ord_map1 & IMSM_ORD_REBUILD)
8425 rebuilt = 0;
8426 }
8427
8428 if (rebuilt) {
8429 map_state = imsm_check_degraded(super, dev,
8430 failed, MAP_0);
8431 end_migration(dev, super, map_state);
8432 }
8433 }
8434 }
8435 for (dl = super->missing; dl; dl = dl->next)
8436 mark_missing(super, dev, &dl->disk, dl->index);
8437 super->updates_pending++;
8438 }
8439
8440 static unsigned long long imsm_set_array_size(struct imsm_dev *dev,
8441 long long new_size)
8442 {
8443 unsigned long long array_blocks;
8444 struct imsm_map *map = get_imsm_map(dev, MAP_0);
8445 int used_disks = imsm_num_data_members(map);
8446
8447 if (used_disks == 0) {
8448 /* when problems occures
8449 * return current array_blocks value
8450 */
8451 array_blocks = imsm_dev_size(dev);
8452
8453 return array_blocks;
8454 }
8455
8456 /* set array size in metadata
8457 */
8458 if (new_size <= 0)
8459 /* OLCE size change is caused by added disks
8460 */
8461 array_blocks = per_dev_array_size(map) * used_disks;
8462 else
8463 /* Online Volume Size Change
8464 * Using available free space
8465 */
8466 array_blocks = new_size;
8467
8468 array_blocks = round_size_to_mb(array_blocks, used_disks);
8469 set_imsm_dev_size(dev, array_blocks);
8470
8471 return array_blocks;
8472 }
8473
8474 static void imsm_set_disk(struct active_array *a, int n, int state);
8475
8476 static void imsm_progress_container_reshape(struct intel_super *super)
8477 {
8478 /* if no device has a migr_state, but some device has a
8479 * different number of members than the previous device, start
8480 * changing the number of devices in this device to match
8481 * previous.
8482 */
8483 struct imsm_super *mpb = super->anchor;
8484 int prev_disks = -1;
8485 int i;
8486 int copy_map_size;
8487
8488 for (i = 0; i < mpb->num_raid_devs; i++) {
8489 struct imsm_dev *dev = get_imsm_dev(super, i);
8490 struct imsm_map *map = get_imsm_map(dev, MAP_0);
8491 struct imsm_map *map2;
8492 int prev_num_members;
8493
8494 if (dev->vol.migr_state)
8495 return;
8496
8497 if (prev_disks == -1)
8498 prev_disks = map->num_members;
8499 if (prev_disks == map->num_members)
8500 continue;
8501
8502 /* OK, this array needs to enter reshape mode.
8503 * i.e it needs a migr_state
8504 */
8505
8506 copy_map_size = sizeof_imsm_map(map);
8507 prev_num_members = map->num_members;
8508 map->num_members = prev_disks;
8509 dev->vol.migr_state = 1;
8510 set_vol_curr_migr_unit(dev, 0);
8511 set_migr_type(dev, MIGR_GEN_MIGR);
8512 for (i = prev_num_members;
8513 i < map->num_members; i++)
8514 set_imsm_ord_tbl_ent(map, i, i);
8515 map2 = get_imsm_map(dev, MAP_1);
8516 /* Copy the current map */
8517 memcpy(map2, map, copy_map_size);
8518 map2->num_members = prev_num_members;
8519
8520 imsm_set_array_size(dev, -1);
8521 super->clean_migration_record_by_mdmon = 1;
8522 super->updates_pending++;
8523 }
8524 }
8525
8526 /* Handle dirty -> clean transititions, resync and reshape. Degraded and rebuild
8527 * states are handled in imsm_set_disk() with one exception, when a
8528 * resync is stopped due to a new failure this routine will set the
8529 * 'degraded' state for the array.
8530 */
8531 static int imsm_set_array_state(struct active_array *a, int consistent)
8532 {
8533 int inst = a->info.container_member;
8534 struct intel_super *super = a->container->sb;
8535 struct imsm_dev *dev = get_imsm_dev(super, inst);
8536 struct imsm_map *map = get_imsm_map(dev, MAP_0);
8537 int failed = imsm_count_failed(super, dev, MAP_0);
8538 __u8 map_state = imsm_check_degraded(super, dev, failed, MAP_0);
8539 __u32 blocks_per_unit;
8540
8541 if (dev->vol.migr_state &&
8542 dev->vol.migr_type == MIGR_GEN_MIGR) {
8543 /* array state change is blocked due to reshape action
8544 * We might need to
8545 * - abort the reshape (if last_checkpoint is 0 and action!= reshape)
8546 * - finish the reshape (if last_checkpoint is big and action != reshape)
8547 * - update vol_curr_migr_unit
8548 */
8549 if (a->curr_action == reshape) {
8550 /* still reshaping, maybe update vol_curr_migr_unit */
8551 goto mark_checkpoint;
8552 } else {
8553 if (a->last_checkpoint == 0 && a->prev_action == reshape) {
8554 /* for some reason we aborted the reshape.
8555 *
8556 * disable automatic metadata rollback
8557 * user action is required to recover process
8558 */
8559 if (0) {
8560 struct imsm_map *map2 =
8561 get_imsm_map(dev, MAP_1);
8562 dev->vol.migr_state = 0;
8563 set_migr_type(dev, 0);
8564 set_vol_curr_migr_unit(dev, 0);
8565 memcpy(map, map2,
8566 sizeof_imsm_map(map2));
8567 super->updates_pending++;
8568 }
8569 }
8570 if (a->last_checkpoint >= a->info.component_size) {
8571 unsigned long long array_blocks;
8572 int used_disks;
8573 struct mdinfo *mdi;
8574
8575 used_disks = imsm_num_data_members(map);
8576 if (used_disks > 0) {
8577 array_blocks =
8578 per_dev_array_size(map) *
8579 used_disks;
8580 array_blocks =
8581 round_size_to_mb(array_blocks,
8582 used_disks);
8583 a->info.custom_array_size = array_blocks;
8584 /* encourage manager to update array
8585 * size
8586 */
8587
8588 a->check_reshape = 1;
8589 }
8590 /* finalize online capacity expansion/reshape */
8591 for (mdi = a->info.devs; mdi; mdi = mdi->next)
8592 imsm_set_disk(a,
8593 mdi->disk.raid_disk,
8594 mdi->curr_state);
8595
8596 imsm_progress_container_reshape(super);
8597 }
8598 }
8599 }
8600
8601 /* before we activate this array handle any missing disks */
8602 if (consistent == 2)
8603 handle_missing(super, dev);
8604
8605 if (consistent == 2 &&
8606 (!is_resync_complete(&a->info) ||
8607 map_state != IMSM_T_STATE_NORMAL ||
8608 dev->vol.migr_state))
8609 consistent = 0;
8610
8611 if (is_resync_complete(&a->info)) {
8612 /* complete intialization / resync,
8613 * recovery and interrupted recovery is completed in
8614 * ->set_disk
8615 */
8616 if (is_resyncing(dev)) {
8617 dprintf("imsm: mark resync done\n");
8618 end_migration(dev, super, map_state);
8619 super->updates_pending++;
8620 a->last_checkpoint = 0;
8621 }
8622 } else if ((!is_resyncing(dev) && !failed) &&
8623 (imsm_reshape_blocks_arrays_changes(super) == 0)) {
8624 /* mark the start of the init process if nothing is failed */
8625 dprintf("imsm: mark resync start\n");
8626 if (map->map_state == IMSM_T_STATE_UNINITIALIZED)
8627 migrate(dev, super, IMSM_T_STATE_NORMAL, MIGR_INIT);
8628 else
8629 migrate(dev, super, IMSM_T_STATE_NORMAL, MIGR_REPAIR);
8630 super->updates_pending++;
8631 }
8632
8633 mark_checkpoint:
8634 /* skip checkpointing for general migration,
8635 * it is controlled in mdadm
8636 */
8637 if (is_gen_migration(dev))
8638 goto skip_mark_checkpoint;
8639
8640 /* check if we can update vol_curr_migr_unit from resync_start,
8641 * recovery_start
8642 */
8643 blocks_per_unit = blocks_per_migr_unit(super, dev);
8644 if (blocks_per_unit) {
8645 set_vol_curr_migr_unit(dev,
8646 a->last_checkpoint / blocks_per_unit);
8647 dprintf("imsm: mark checkpoint (%llu)\n",
8648 vol_curr_migr_unit(dev));
8649 super->updates_pending++;
8650 }
8651
8652 skip_mark_checkpoint:
8653 /* mark dirty / clean */
8654 if (((dev->vol.dirty & RAIDVOL_DIRTY) && consistent) ||
8655 (!(dev->vol.dirty & RAIDVOL_DIRTY) && !consistent)) {
8656 dprintf("imsm: mark '%s'\n", consistent ? "clean" : "dirty");
8657 if (consistent) {
8658 dev->vol.dirty = RAIDVOL_CLEAN;
8659 } else {
8660 dev->vol.dirty = RAIDVOL_DIRTY;
8661 if (dev->rwh_policy == RWH_DISTRIBUTED ||
8662 dev->rwh_policy == RWH_MULTIPLE_DISTRIBUTED)
8663 dev->vol.dirty |= RAIDVOL_DSRECORD_VALID;
8664 }
8665 super->updates_pending++;
8666 }
8667
8668 return consistent;
8669 }
8670
8671 static int imsm_disk_slot_to_ord(struct active_array *a, int slot)
8672 {
8673 int inst = a->info.container_member;
8674 struct intel_super *super = a->container->sb;
8675 struct imsm_dev *dev = get_imsm_dev(super, inst);
8676 struct imsm_map *map = get_imsm_map(dev, MAP_0);
8677
8678 if (slot > map->num_members) {
8679 pr_err("imsm: imsm_disk_slot_to_ord %d out of range 0..%d\n",
8680 slot, map->num_members - 1);
8681 return -1;
8682 }
8683
8684 if (slot < 0)
8685 return -1;
8686
8687 return get_imsm_ord_tbl_ent(dev, slot, MAP_0);
8688 }
8689
8690 static void imsm_set_disk(struct active_array *a, int n, int state)
8691 {
8692 int inst = a->info.container_member;
8693 struct intel_super *super = a->container->sb;
8694 struct imsm_dev *dev = get_imsm_dev(super, inst);
8695 struct imsm_map *map = get_imsm_map(dev, MAP_0);
8696 struct imsm_disk *disk;
8697 struct mdinfo *mdi;
8698 int recovery_not_finished = 0;
8699 int failed;
8700 int ord;
8701 __u8 map_state;
8702 int rebuild_done = 0;
8703 int i;
8704
8705 ord = get_imsm_ord_tbl_ent(dev, n, MAP_X);
8706 if (ord < 0)
8707 return;
8708
8709 dprintf("imsm: set_disk %d:%x\n", n, state);
8710 disk = get_imsm_disk(super, ord_to_idx(ord));
8711
8712 /* check for new failures */
8713 if (disk && (state & DS_FAULTY)) {
8714 if (mark_failure(super, dev, disk, ord_to_idx(ord)))
8715 super->updates_pending++;
8716 }
8717
8718 /* check if in_sync */
8719 if (state & DS_INSYNC && ord & IMSM_ORD_REBUILD && is_rebuilding(dev)) {
8720 struct imsm_map *migr_map = get_imsm_map(dev, MAP_1);
8721
8722 set_imsm_ord_tbl_ent(migr_map, n, ord_to_idx(ord));
8723 rebuild_done = 1;
8724 super->updates_pending++;
8725 }
8726
8727 failed = imsm_count_failed(super, dev, MAP_0);
8728 map_state = imsm_check_degraded(super, dev, failed, MAP_0);
8729
8730 /* check if recovery complete, newly degraded, or failed */
8731 dprintf("imsm: Detected transition to state ");
8732 switch (map_state) {
8733 case IMSM_T_STATE_NORMAL: /* transition to normal state */
8734 dprintf("normal: ");
8735 if (is_rebuilding(dev)) {
8736 dprintf_cont("while rebuilding");
8737 /* check if recovery is really finished */
8738 for (mdi = a->info.devs; mdi ; mdi = mdi->next)
8739 if (mdi->recovery_start != MaxSector) {
8740 recovery_not_finished = 1;
8741 break;
8742 }
8743 if (recovery_not_finished) {
8744 dprintf_cont("\n");
8745 dprintf("Rebuild has not finished yet, state not changed");
8746 if (a->last_checkpoint < mdi->recovery_start) {
8747 a->last_checkpoint = mdi->recovery_start;
8748 super->updates_pending++;
8749 }
8750 break;
8751 }
8752 end_migration(dev, super, map_state);
8753 map->failed_disk_num = ~0;
8754 super->updates_pending++;
8755 a->last_checkpoint = 0;
8756 break;
8757 }
8758 if (is_gen_migration(dev)) {
8759 dprintf_cont("while general migration");
8760 if (a->last_checkpoint >= a->info.component_size)
8761 end_migration(dev, super, map_state);
8762 else
8763 map->map_state = map_state;
8764 map->failed_disk_num = ~0;
8765 super->updates_pending++;
8766 break;
8767 }
8768 break;
8769 case IMSM_T_STATE_DEGRADED: /* transition to degraded state */
8770 dprintf_cont("degraded: ");
8771 if (map->map_state != map_state && !dev->vol.migr_state) {
8772 dprintf_cont("mark degraded");
8773 map->map_state = map_state;
8774 super->updates_pending++;
8775 a->last_checkpoint = 0;
8776 break;
8777 }
8778 if (is_rebuilding(dev)) {
8779 dprintf_cont("while rebuilding ");
8780 if (state & DS_FAULTY) {
8781 dprintf_cont("removing failed drive ");
8782 if (n == map->failed_disk_num) {
8783 dprintf_cont("end migration");
8784 end_migration(dev, super, map_state);
8785 a->last_checkpoint = 0;
8786 } else {
8787 dprintf_cont("fail detected during rebuild, changing map state");
8788 map->map_state = map_state;
8789 }
8790 super->updates_pending++;
8791 }
8792
8793 if (!rebuild_done)
8794 break;
8795
8796 /* check if recovery is really finished */
8797 for (mdi = a->info.devs; mdi ; mdi = mdi->next)
8798 if (mdi->recovery_start != MaxSector) {
8799 recovery_not_finished = 1;
8800 break;
8801 }
8802 if (recovery_not_finished) {
8803 dprintf_cont("\n");
8804 dprintf_cont("Rebuild has not finished yet");
8805 if (a->last_checkpoint < mdi->recovery_start) {
8806 a->last_checkpoint =
8807 mdi->recovery_start;
8808 super->updates_pending++;
8809 }
8810 break;
8811 }
8812
8813 dprintf_cont(" Rebuild done, still degraded");
8814 end_migration(dev, super, map_state);
8815 a->last_checkpoint = 0;
8816 super->updates_pending++;
8817
8818 for (i = 0; i < map->num_members; i++) {
8819 int idx = get_imsm_ord_tbl_ent(dev, i, MAP_0);
8820
8821 if (idx & IMSM_ORD_REBUILD)
8822 map->failed_disk_num = i;
8823 }
8824 super->updates_pending++;
8825 break;
8826 }
8827 if (is_gen_migration(dev)) {
8828 dprintf_cont("while general migration");
8829 if (a->last_checkpoint >= a->info.component_size)
8830 end_migration(dev, super, map_state);
8831 else {
8832 map->map_state = map_state;
8833 manage_second_map(super, dev);
8834 }
8835 super->updates_pending++;
8836 break;
8837 }
8838 if (is_initializing(dev)) {
8839 dprintf_cont("while initialization.");
8840 map->map_state = map_state;
8841 super->updates_pending++;
8842 break;
8843 }
8844 break;
8845 case IMSM_T_STATE_FAILED: /* transition to failed state */
8846 dprintf_cont("failed: ");
8847 if (is_gen_migration(dev)) {
8848 dprintf_cont("while general migration");
8849 map->map_state = map_state;
8850 super->updates_pending++;
8851 break;
8852 }
8853 if (map->map_state != map_state) {
8854 dprintf_cont("mark failed");
8855 end_migration(dev, super, map_state);
8856 super->updates_pending++;
8857 a->last_checkpoint = 0;
8858 break;
8859 }
8860 break;
8861 default:
8862 dprintf_cont("state %i\n", map_state);
8863 }
8864 dprintf_cont("\n");
8865 }
8866
8867 static int store_imsm_mpb(int fd, struct imsm_super *mpb)
8868 {
8869 void *buf = mpb;
8870 __u32 mpb_size = __le32_to_cpu(mpb->mpb_size);
8871 unsigned long long dsize;
8872 unsigned long long sectors;
8873 unsigned int sector_size;
8874
8875 get_dev_sector_size(fd, NULL, &sector_size);
8876 get_dev_size(fd, NULL, &dsize);
8877
8878 if (mpb_size > sector_size) {
8879 /* -1 to account for anchor */
8880 sectors = mpb_sectors(mpb, sector_size) - 1;
8881
8882 /* write the extended mpb to the sectors preceeding the anchor */
8883 if (lseek64(fd, dsize - (sector_size * (2 + sectors)),
8884 SEEK_SET) < 0)
8885 return 1;
8886
8887 if ((unsigned long long)write(fd, buf + sector_size,
8888 sector_size * sectors) != sector_size * sectors)
8889 return 1;
8890 }
8891
8892 /* first block is stored on second to last sector of the disk */
8893 if (lseek64(fd, dsize - (sector_size * 2), SEEK_SET) < 0)
8894 return 1;
8895
8896 if ((unsigned int)write(fd, buf, sector_size) != sector_size)
8897 return 1;
8898
8899 return 0;
8900 }
8901
8902 static void imsm_sync_metadata(struct supertype *container)
8903 {
8904 struct intel_super *super = container->sb;
8905
8906 dprintf("sync metadata: %d\n", super->updates_pending);
8907 if (!super->updates_pending)
8908 return;
8909
8910 write_super_imsm(container, 0);
8911
8912 super->updates_pending = 0;
8913 }
8914
8915 static struct dl *imsm_readd(struct intel_super *super, int idx, struct active_array *a)
8916 {
8917 struct imsm_dev *dev = get_imsm_dev(super, a->info.container_member);
8918 int i = get_imsm_disk_idx(dev, idx, MAP_X);
8919 struct dl *dl;
8920
8921 for (dl = super->disks; dl; dl = dl->next)
8922 if (dl->index == i)
8923 break;
8924
8925 if (dl && is_failed(&dl->disk))
8926 dl = NULL;
8927
8928 if (dl)
8929 dprintf("found %x:%x\n", dl->major, dl->minor);
8930
8931 return dl;
8932 }
8933
8934 static struct dl *imsm_add_spare(struct intel_super *super, int slot,
8935 struct active_array *a, int activate_new,
8936 struct mdinfo *additional_test_list)
8937 {
8938 struct imsm_dev *dev = get_imsm_dev(super, a->info.container_member);
8939 int idx = get_imsm_disk_idx(dev, slot, MAP_X);
8940 struct imsm_super *mpb = super->anchor;
8941 struct imsm_map *map;
8942 unsigned long long pos;
8943 struct mdinfo *d;
8944 struct extent *ex;
8945 int i, j;
8946 int found;
8947 __u32 array_start = 0;
8948 __u32 array_end = 0;
8949 struct dl *dl;
8950 struct mdinfo *test_list;
8951
8952 for (dl = super->disks; dl; dl = dl->next) {
8953 /* If in this array, skip */
8954 for (d = a->info.devs ; d ; d = d->next)
8955 if (d->state_fd >= 0 &&
8956 d->disk.major == dl->major &&
8957 d->disk.minor == dl->minor) {
8958 dprintf("%x:%x already in array\n",
8959 dl->major, dl->minor);
8960 break;
8961 }
8962 if (d)
8963 continue;
8964 test_list = additional_test_list;
8965 while (test_list) {
8966 if (test_list->disk.major == dl->major &&
8967 test_list->disk.minor == dl->minor) {
8968 dprintf("%x:%x already in additional test list\n",
8969 dl->major, dl->minor);
8970 break;
8971 }
8972 test_list = test_list->next;
8973 }
8974 if (test_list)
8975 continue;
8976
8977 /* skip in use or failed drives */
8978 if (is_failed(&dl->disk) || idx == dl->index ||
8979 dl->index == -2) {
8980 dprintf("%x:%x status (failed: %d index: %d)\n",
8981 dl->major, dl->minor, is_failed(&dl->disk), idx);
8982 continue;
8983 }
8984
8985 /* skip pure spares when we are looking for partially
8986 * assimilated drives
8987 */
8988 if (dl->index == -1 && !activate_new)
8989 continue;
8990
8991 if (!drive_validate_sector_size(super, dl))
8992 continue;
8993
8994 /* Does this unused device have the requisite free space?
8995 * It needs to be able to cover all member volumes
8996 */
8997 ex = get_extents(super, dl, 1);
8998 if (!ex) {
8999 dprintf("cannot get extents\n");
9000 continue;
9001 }
9002 for (i = 0; i < mpb->num_raid_devs; i++) {
9003 dev = get_imsm_dev(super, i);
9004 map = get_imsm_map(dev, MAP_0);
9005
9006 /* check if this disk is already a member of
9007 * this array
9008 */
9009 if (get_imsm_disk_slot(map, dl->index) >= 0)
9010 continue;
9011
9012 found = 0;
9013 j = 0;
9014 pos = 0;
9015 array_start = pba_of_lba0(map);
9016 array_end = array_start +
9017 per_dev_array_size(map) - 1;
9018
9019 do {
9020 /* check that we can start at pba_of_lba0 with
9021 * num_data_stripes*blocks_per_stripe of space
9022 */
9023 if (array_start >= pos && array_end < ex[j].start) {
9024 found = 1;
9025 break;
9026 }
9027 pos = ex[j].start + ex[j].size;
9028 j++;
9029 } while (ex[j-1].size);
9030
9031 if (!found)
9032 break;
9033 }
9034
9035 free(ex);
9036 if (i < mpb->num_raid_devs) {
9037 dprintf("%x:%x does not have %u to %u available\n",
9038 dl->major, dl->minor, array_start, array_end);
9039 /* No room */
9040 continue;
9041 }
9042 return dl;
9043 }
9044
9045 return dl;
9046 }
9047
9048 static int imsm_rebuild_allowed(struct supertype *cont, int dev_idx, int failed)
9049 {
9050 struct imsm_dev *dev2;
9051 struct imsm_map *map;
9052 struct dl *idisk;
9053 int slot;
9054 int idx;
9055 __u8 state;
9056
9057 dev2 = get_imsm_dev(cont->sb, dev_idx);
9058 if (dev2) {
9059 state = imsm_check_degraded(cont->sb, dev2, failed, MAP_0);
9060 if (state == IMSM_T_STATE_FAILED) {
9061 map = get_imsm_map(dev2, MAP_0);
9062 if (!map)
9063 return 1;
9064 for (slot = 0; slot < map->num_members; slot++) {
9065 /*
9066 * Check if failed disks are deleted from intel
9067 * disk list or are marked to be deleted
9068 */
9069 idx = get_imsm_disk_idx(dev2, slot, MAP_X);
9070 idisk = get_imsm_dl_disk(cont->sb, idx);
9071 /*
9072 * Do not rebuild the array if failed disks
9073 * from failed sub-array are not removed from
9074 * container.
9075 */
9076 if (idisk &&
9077 is_failed(&idisk->disk) &&
9078 (idisk->action != DISK_REMOVE))
9079 return 0;
9080 }
9081 }
9082 }
9083 return 1;
9084 }
9085
9086 static struct mdinfo *imsm_activate_spare(struct active_array *a,
9087 struct metadata_update **updates)
9088 {
9089 /**
9090 * Find a device with unused free space and use it to replace a
9091 * failed/vacant region in an array. We replace failed regions one a
9092 * array at a time. The result is that a new spare disk will be added
9093 * to the first failed array and after the monitor has finished
9094 * propagating failures the remainder will be consumed.
9095 *
9096 * FIXME add a capability for mdmon to request spares from another
9097 * container.
9098 */
9099
9100 struct intel_super *super = a->container->sb;
9101 int inst = a->info.container_member;
9102 struct imsm_dev *dev = get_imsm_dev(super, inst);
9103 struct imsm_map *map = get_imsm_map(dev, MAP_0);
9104 int failed = a->info.array.raid_disks;
9105 struct mdinfo *rv = NULL;
9106 struct mdinfo *d;
9107 struct mdinfo *di;
9108 struct metadata_update *mu;
9109 struct dl *dl;
9110 struct imsm_update_activate_spare *u;
9111 int num_spares = 0;
9112 int i;
9113 int allowed;
9114
9115 for (d = a->info.devs ; d ; d = d->next) {
9116 if ((d->curr_state & DS_FAULTY) &&
9117 d->state_fd >= 0)
9118 /* wait for Removal to happen */
9119 return NULL;
9120 if (d->state_fd >= 0)
9121 failed--;
9122 }
9123
9124 dprintf("imsm: activate spare: inst=%d failed=%d (%d) level=%d\n",
9125 inst, failed, a->info.array.raid_disks, a->info.array.level);
9126
9127 if (imsm_reshape_blocks_arrays_changes(super))
9128 return NULL;
9129
9130 /* Cannot activate another spare if rebuild is in progress already
9131 */
9132 if (is_rebuilding(dev)) {
9133 dprintf("imsm: No spare activation allowed. Rebuild in progress already.\n");
9134 return NULL;
9135 }
9136
9137 if (a->info.array.level == 4)
9138 /* No repair for takeovered array
9139 * imsm doesn't support raid4
9140 */
9141 return NULL;
9142
9143 if (imsm_check_degraded(super, dev, failed, MAP_0) !=
9144 IMSM_T_STATE_DEGRADED)
9145 return NULL;
9146
9147 if (get_imsm_map(dev, MAP_0)->map_state == IMSM_T_STATE_UNINITIALIZED) {
9148 dprintf("imsm: No spare activation allowed. Volume is not initialized.\n");
9149 return NULL;
9150 }
9151
9152 /*
9153 * If there are any failed disks check state of the other volume.
9154 * Block rebuild if the another one is failed until failed disks
9155 * are removed from container.
9156 */
9157 if (failed) {
9158 dprintf("found failed disks in %.*s, check if there anotherfailed sub-array.\n",
9159 MAX_RAID_SERIAL_LEN, dev->volume);
9160 /* check if states of the other volumes allow for rebuild */
9161 for (i = 0; i < super->anchor->num_raid_devs; i++) {
9162 if (i != inst) {
9163 allowed = imsm_rebuild_allowed(a->container,
9164 i, failed);
9165 if (!allowed)
9166 return NULL;
9167 }
9168 }
9169 }
9170
9171 /* For each slot, if it is not working, find a spare */
9172 for (i = 0; i < a->info.array.raid_disks; i++) {
9173 for (d = a->info.devs ; d ; d = d->next)
9174 if (d->disk.raid_disk == i)
9175 break;
9176 dprintf("found %d: %p %x\n", i, d, d?d->curr_state:0);
9177 if (d && (d->state_fd >= 0))
9178 continue;
9179
9180 /*
9181 * OK, this device needs recovery. Try to re-add the
9182 * previous occupant of this slot, if this fails see if
9183 * we can continue the assimilation of a spare that was
9184 * partially assimilated, finally try to activate a new
9185 * spare.
9186 */
9187 dl = imsm_readd(super, i, a);
9188 if (!dl)
9189 dl = imsm_add_spare(super, i, a, 0, rv);
9190 if (!dl)
9191 dl = imsm_add_spare(super, i, a, 1, rv);
9192 if (!dl)
9193 continue;
9194
9195 /* found a usable disk with enough space */
9196 di = xcalloc(1, sizeof(*di));
9197
9198 /* dl->index will be -1 in the case we are activating a
9199 * pristine spare. imsm_process_update() will create a
9200 * new index in this case. Once a disk is found to be
9201 * failed in all member arrays it is kicked from the
9202 * metadata
9203 */
9204 di->disk.number = dl->index;
9205
9206 /* (ab)use di->devs to store a pointer to the device
9207 * we chose
9208 */
9209 di->devs = (struct mdinfo *) dl;
9210
9211 di->disk.raid_disk = i;
9212 di->disk.major = dl->major;
9213 di->disk.minor = dl->minor;
9214 di->disk.state = 0;
9215 di->recovery_start = 0;
9216 di->data_offset = pba_of_lba0(map);
9217 di->component_size = a->info.component_size;
9218 di->container_member = inst;
9219 di->bb.supported = 1;
9220 if (a->info.consistency_policy == CONSISTENCY_POLICY_PPL) {
9221 di->ppl_sector = get_ppl_sector(super, inst);
9222 di->ppl_size = MULTIPLE_PPL_AREA_SIZE_IMSM >> 9;
9223 }
9224 super->random = random32();
9225 di->next = rv;
9226 rv = di;
9227 num_spares++;
9228 dprintf("%x:%x to be %d at %llu\n", dl->major, dl->minor,
9229 i, di->data_offset);
9230 }
9231
9232 if (!rv)
9233 /* No spares found */
9234 return rv;
9235 /* Now 'rv' has a list of devices to return.
9236 * Create a metadata_update record to update the
9237 * disk_ord_tbl for the array
9238 */
9239 mu = xmalloc(sizeof(*mu));
9240 mu->buf = xcalloc(num_spares,
9241 sizeof(struct imsm_update_activate_spare));
9242 mu->space = NULL;
9243 mu->space_list = NULL;
9244 mu->len = sizeof(struct imsm_update_activate_spare) * num_spares;
9245 mu->next = *updates;
9246 u = (struct imsm_update_activate_spare *) mu->buf;
9247
9248 for (di = rv ; di ; di = di->next) {
9249 u->type = update_activate_spare;
9250 u->dl = (struct dl *) di->devs;
9251 di->devs = NULL;
9252 u->slot = di->disk.raid_disk;
9253 u->array = inst;
9254 u->next = u + 1;
9255 u++;
9256 }
9257 (u-1)->next = NULL;
9258 *updates = mu;
9259
9260 return rv;
9261 }
9262
9263 static int disks_overlap(struct intel_super *super, int idx, struct imsm_update_create_array *u)
9264 {
9265 struct imsm_dev *dev = get_imsm_dev(super, idx);
9266 struct imsm_map *map = get_imsm_map(dev, MAP_0);
9267 struct imsm_map *new_map = get_imsm_map(&u->dev, MAP_0);
9268 struct disk_info *inf = get_disk_info(u);
9269 struct imsm_disk *disk;
9270 int i;
9271 int j;
9272
9273 for (i = 0; i < map->num_members; i++) {
9274 disk = get_imsm_disk(super, get_imsm_disk_idx(dev, i, MAP_X));
9275 for (j = 0; j < new_map->num_members; j++)
9276 if (serialcmp(disk->serial, inf[j].serial) == 0)
9277 return 1;
9278 }
9279
9280 return 0;
9281 }
9282
9283 static struct dl *get_disk_super(struct intel_super *super, int major, int minor)
9284 {
9285 struct dl *dl;
9286
9287 for (dl = super->disks; dl; dl = dl->next)
9288 if (dl->major == major && dl->minor == minor)
9289 return dl;
9290 return NULL;
9291 }
9292
9293 static int remove_disk_super(struct intel_super *super, int major, int minor)
9294 {
9295 struct dl *prev;
9296 struct dl *dl;
9297
9298 prev = NULL;
9299 for (dl = super->disks; dl; dl = dl->next) {
9300 if (dl->major == major && dl->minor == minor) {
9301 /* remove */
9302 if (prev)
9303 prev->next = dl->next;
9304 else
9305 super->disks = dl->next;
9306 dl->next = NULL;
9307 __free_imsm_disk(dl);
9308 dprintf("removed %x:%x\n", major, minor);
9309 break;
9310 }
9311 prev = dl;
9312 }
9313 return 0;
9314 }
9315
9316 static void imsm_delete(struct intel_super *super, struct dl **dlp, unsigned index);
9317
9318 static int add_remove_disk_update(struct intel_super *super)
9319 {
9320 int check_degraded = 0;
9321 struct dl *disk;
9322
9323 /* add/remove some spares to/from the metadata/contrainer */
9324 while (super->disk_mgmt_list) {
9325 struct dl *disk_cfg;
9326
9327 disk_cfg = super->disk_mgmt_list;
9328 super->disk_mgmt_list = disk_cfg->next;
9329 disk_cfg->next = NULL;
9330
9331 if (disk_cfg->action == DISK_ADD) {
9332 disk_cfg->next = super->disks;
9333 super->disks = disk_cfg;
9334 check_degraded = 1;
9335 dprintf("added %x:%x\n",
9336 disk_cfg->major, disk_cfg->minor);
9337 } else if (disk_cfg->action == DISK_REMOVE) {
9338 dprintf("Disk remove action processed: %x.%x\n",
9339 disk_cfg->major, disk_cfg->minor);
9340 disk = get_disk_super(super,
9341 disk_cfg->major,
9342 disk_cfg->minor);
9343 if (disk) {
9344 /* store action status */
9345 disk->action = DISK_REMOVE;
9346 /* remove spare disks only */
9347 if (disk->index == -1) {
9348 remove_disk_super(super,
9349 disk_cfg->major,
9350 disk_cfg->minor);
9351 } else {
9352 disk_cfg->fd = disk->fd;
9353 disk->fd = -1;
9354 }
9355 }
9356 /* release allocate disk structure */
9357 __free_imsm_disk(disk_cfg);
9358 }
9359 }
9360 return check_degraded;
9361 }
9362
9363 static int apply_reshape_migration_update(struct imsm_update_reshape_migration *u,
9364 struct intel_super *super,
9365 void ***space_list)
9366 {
9367 struct intel_dev *id;
9368 void **tofree = NULL;
9369 int ret_val = 0;
9370
9371 dprintf("(enter)\n");
9372 if (u->subdev < 0 || u->subdev > 1) {
9373 dprintf("imsm: Error: Wrong subdev: %i\n", u->subdev);
9374 return ret_val;
9375 }
9376 if (space_list == NULL || *space_list == NULL) {
9377 dprintf("imsm: Error: Memory is not allocated\n");
9378 return ret_val;
9379 }
9380
9381 for (id = super->devlist ; id; id = id->next) {
9382 if (id->index == (unsigned)u->subdev) {
9383 struct imsm_dev *dev = get_imsm_dev(super, u->subdev);
9384 struct imsm_map *map;
9385 struct imsm_dev *new_dev =
9386 (struct imsm_dev *)*space_list;
9387 struct imsm_map *migr_map = get_imsm_map(dev, MAP_1);
9388 int to_state;
9389 struct dl *new_disk;
9390
9391 if (new_dev == NULL)
9392 return ret_val;
9393 *space_list = **space_list;
9394 memcpy(new_dev, dev, sizeof_imsm_dev(dev, 0));
9395 map = get_imsm_map(new_dev, MAP_0);
9396 if (migr_map) {
9397 dprintf("imsm: Error: migration in progress");
9398 return ret_val;
9399 }
9400
9401 to_state = map->map_state;
9402 if ((u->new_level == 5) && (map->raid_level == 0)) {
9403 map->num_members++;
9404 /* this should not happen */
9405 if (u->new_disks[0] < 0) {
9406 map->failed_disk_num =
9407 map->num_members - 1;
9408 to_state = IMSM_T_STATE_DEGRADED;
9409 } else
9410 to_state = IMSM_T_STATE_NORMAL;
9411 }
9412 migrate(new_dev, super, to_state, MIGR_GEN_MIGR);
9413 if (u->new_level > -1)
9414 map->raid_level = u->new_level;
9415 migr_map = get_imsm_map(new_dev, MAP_1);
9416 if ((u->new_level == 5) &&
9417 (migr_map->raid_level == 0)) {
9418 int ord = map->num_members - 1;
9419 migr_map->num_members--;
9420 if (u->new_disks[0] < 0)
9421 ord |= IMSM_ORD_REBUILD;
9422 set_imsm_ord_tbl_ent(map,
9423 map->num_members - 1,
9424 ord);
9425 }
9426 id->dev = new_dev;
9427 tofree = (void **)dev;
9428
9429 /* update chunk size
9430 */
9431 if (u->new_chunksize > 0) {
9432 unsigned long long num_data_stripes;
9433 struct imsm_map *dest_map =
9434 get_imsm_map(dev, MAP_0);
9435 int used_disks =
9436 imsm_num_data_members(dest_map);
9437
9438 if (used_disks == 0)
9439 return ret_val;
9440
9441 map->blocks_per_strip =
9442 __cpu_to_le16(u->new_chunksize * 2);
9443 num_data_stripes =
9444 imsm_dev_size(dev) / used_disks;
9445 num_data_stripes /= map->blocks_per_strip;
9446 num_data_stripes /= map->num_domains;
9447 set_num_data_stripes(map, num_data_stripes);
9448 }
9449
9450 /* ensure blocks_per_member has valid value
9451 */
9452 set_blocks_per_member(map,
9453 per_dev_array_size(map) +
9454 NUM_BLOCKS_DIRTY_STRIPE_REGION);
9455
9456 /* add disk
9457 */
9458 if (u->new_level != 5 || migr_map->raid_level != 0 ||
9459 migr_map->raid_level == map->raid_level)
9460 goto skip_disk_add;
9461
9462 if (u->new_disks[0] >= 0) {
9463 /* use passes spare
9464 */
9465 new_disk = get_disk_super(super,
9466 major(u->new_disks[0]),
9467 minor(u->new_disks[0]));
9468 dprintf("imsm: new disk for reshape is: %i:%i (%p, index = %i)\n",
9469 major(u->new_disks[0]),
9470 minor(u->new_disks[0]),
9471 new_disk, new_disk->index);
9472 if (new_disk == NULL)
9473 goto error_disk_add;
9474
9475 new_disk->index = map->num_members - 1;
9476 /* slot to fill in autolayout
9477 */
9478 new_disk->raiddisk = new_disk->index;
9479 new_disk->disk.status |= CONFIGURED_DISK;
9480 new_disk->disk.status &= ~SPARE_DISK;
9481 } else
9482 goto error_disk_add;
9483
9484 skip_disk_add:
9485 *tofree = *space_list;
9486 /* calculate new size
9487 */
9488 imsm_set_array_size(new_dev, -1);
9489
9490 ret_val = 1;
9491 }
9492 }
9493
9494 if (tofree)
9495 *space_list = tofree;
9496 return ret_val;
9497
9498 error_disk_add:
9499 dprintf("Error: imsm: Cannot find disk.\n");
9500 return ret_val;
9501 }
9502
9503 static int apply_size_change_update(struct imsm_update_size_change *u,
9504 struct intel_super *super)
9505 {
9506 struct intel_dev *id;
9507 int ret_val = 0;
9508
9509 dprintf("(enter)\n");
9510 if (u->subdev < 0 || u->subdev > 1) {
9511 dprintf("imsm: Error: Wrong subdev: %i\n", u->subdev);
9512 return ret_val;
9513 }
9514
9515 for (id = super->devlist ; id; id = id->next) {
9516 if (id->index == (unsigned)u->subdev) {
9517 struct imsm_dev *dev = get_imsm_dev(super, u->subdev);
9518 struct imsm_map *map = get_imsm_map(dev, MAP_0);
9519 int used_disks = imsm_num_data_members(map);
9520 unsigned long long blocks_per_member;
9521 unsigned long long num_data_stripes;
9522 unsigned long long new_size_per_disk;
9523
9524 if (used_disks == 0)
9525 return 0;
9526
9527 /* calculate new size
9528 */
9529 new_size_per_disk = u->new_size / used_disks;
9530 blocks_per_member = new_size_per_disk +
9531 NUM_BLOCKS_DIRTY_STRIPE_REGION;
9532 num_data_stripes = new_size_per_disk /
9533 map->blocks_per_strip;
9534 num_data_stripes /= map->num_domains;
9535 dprintf("(size: %llu, blocks per member: %llu, num_data_stipes: %llu)\n",
9536 u->new_size, new_size_per_disk,
9537 num_data_stripes);
9538 set_blocks_per_member(map, blocks_per_member);
9539 set_num_data_stripes(map, num_data_stripes);
9540 imsm_set_array_size(dev, u->new_size);
9541
9542 ret_val = 1;
9543 break;
9544 }
9545 }
9546
9547 return ret_val;
9548 }
9549
9550 static int prepare_spare_to_activate(struct supertype *st,
9551 struct imsm_update_activate_spare *u)
9552 {
9553 struct intel_super *super = st->sb;
9554 int prev_current_vol = super->current_vol;
9555 struct active_array *a;
9556 int ret = 1;
9557
9558 for (a = st->arrays; a; a = a->next)
9559 /*
9560 * Additional initialization (adding bitmap header, filling
9561 * the bitmap area with '1's to force initial rebuild for a whole
9562 * data-area) is required when adding the spare to the volume
9563 * with write-intent bitmap.
9564 */
9565 if (a->info.container_member == u->array &&
9566 a->info.consistency_policy == CONSISTENCY_POLICY_BITMAP) {
9567 struct dl *dl;
9568
9569 for (dl = super->disks; dl; dl = dl->next)
9570 if (dl == u->dl)
9571 break;
9572 if (!dl)
9573 break;
9574
9575 super->current_vol = u->array;
9576 if (st->ss->write_bitmap(st, dl->fd, NoUpdate))
9577 ret = 0;
9578 super->current_vol = prev_current_vol;
9579 }
9580 return ret;
9581 }
9582
9583 static int apply_update_activate_spare(struct imsm_update_activate_spare *u,
9584 struct intel_super *super,
9585 struct active_array *active_array)
9586 {
9587 struct imsm_super *mpb = super->anchor;
9588 struct imsm_dev *dev = get_imsm_dev(super, u->array);
9589 struct imsm_map *map = get_imsm_map(dev, MAP_0);
9590 struct imsm_map *migr_map;
9591 struct active_array *a;
9592 struct imsm_disk *disk;
9593 __u8 to_state;
9594 struct dl *dl;
9595 unsigned int found;
9596 int failed;
9597 int victim;
9598 int i;
9599 int second_map_created = 0;
9600
9601 for (; u; u = u->next) {
9602 victim = get_imsm_disk_idx(dev, u->slot, MAP_X);
9603
9604 if (victim < 0)
9605 return 0;
9606
9607 for (dl = super->disks; dl; dl = dl->next)
9608 if (dl == u->dl)
9609 break;
9610
9611 if (!dl) {
9612 pr_err("error: imsm_activate_spare passed an unknown disk (index: %d)\n",
9613 u->dl->index);
9614 return 0;
9615 }
9616
9617 /* count failures (excluding rebuilds and the victim)
9618 * to determine map[0] state
9619 */
9620 failed = 0;
9621 for (i = 0; i < map->num_members; i++) {
9622 if (i == u->slot)
9623 continue;
9624 disk = get_imsm_disk(super,
9625 get_imsm_disk_idx(dev, i, MAP_X));
9626 if (!disk || is_failed(disk))
9627 failed++;
9628 }
9629
9630 /* adding a pristine spare, assign a new index */
9631 if (dl->index < 0) {
9632 dl->index = super->anchor->num_disks;
9633 super->anchor->num_disks++;
9634 }
9635 disk = &dl->disk;
9636 disk->status |= CONFIGURED_DISK;
9637 disk->status &= ~SPARE_DISK;
9638
9639 /* mark rebuild */
9640 to_state = imsm_check_degraded(super, dev, failed, MAP_0);
9641 if (!second_map_created) {
9642 second_map_created = 1;
9643 map->map_state = IMSM_T_STATE_DEGRADED;
9644 migrate(dev, super, to_state, MIGR_REBUILD);
9645 } else
9646 map->map_state = to_state;
9647 migr_map = get_imsm_map(dev, MAP_1);
9648 set_imsm_ord_tbl_ent(map, u->slot, dl->index);
9649 set_imsm_ord_tbl_ent(migr_map, u->slot,
9650 dl->index | IMSM_ORD_REBUILD);
9651
9652 /* update the family_num to mark a new container
9653 * generation, being careful to record the existing
9654 * family_num in orig_family_num to clean up after
9655 * earlier mdadm versions that neglected to set it.
9656 */
9657 if (mpb->orig_family_num == 0)
9658 mpb->orig_family_num = mpb->family_num;
9659 mpb->family_num += super->random;
9660
9661 /* count arrays using the victim in the metadata */
9662 found = 0;
9663 for (a = active_array; a ; a = a->next) {
9664 dev = get_imsm_dev(super, a->info.container_member);
9665 map = get_imsm_map(dev, MAP_0);
9666
9667 if (get_imsm_disk_slot(map, victim) >= 0)
9668 found++;
9669 }
9670
9671 /* delete the victim if it is no longer being
9672 * utilized anywhere
9673 */
9674 if (!found) {
9675 struct dl **dlp;
9676
9677 /* We know that 'manager' isn't touching anything,
9678 * so it is safe to delete
9679 */
9680 for (dlp = &super->disks; *dlp; dlp = &(*dlp)->next)
9681 if ((*dlp)->index == victim)
9682 break;
9683
9684 /* victim may be on the missing list */
9685 if (!*dlp)
9686 for (dlp = &super->missing; *dlp;
9687 dlp = &(*dlp)->next)
9688 if ((*dlp)->index == victim)
9689 break;
9690 imsm_delete(super, dlp, victim);
9691 }
9692 }
9693
9694 return 1;
9695 }
9696
9697 static int apply_reshape_container_disks_update(struct imsm_update_reshape *u,
9698 struct intel_super *super,
9699 void ***space_list)
9700 {
9701 struct dl *new_disk;
9702 struct intel_dev *id;
9703 int i;
9704 int delta_disks = u->new_raid_disks - u->old_raid_disks;
9705 int disk_count = u->old_raid_disks;
9706 void **tofree = NULL;
9707 int devices_to_reshape = 1;
9708 struct imsm_super *mpb = super->anchor;
9709 int ret_val = 0;
9710 unsigned int dev_id;
9711
9712 dprintf("(enter)\n");
9713
9714 /* enable spares to use in array */
9715 for (i = 0; i < delta_disks; i++) {
9716 new_disk = get_disk_super(super,
9717 major(u->new_disks[i]),
9718 minor(u->new_disks[i]));
9719 dprintf("imsm: new disk for reshape is: %i:%i (%p, index = %i)\n",
9720 major(u->new_disks[i]), minor(u->new_disks[i]),
9721 new_disk, new_disk->index);
9722 if (new_disk == NULL ||
9723 (new_disk->index >= 0 &&
9724 new_disk->index < u->old_raid_disks))
9725 goto update_reshape_exit;
9726 new_disk->index = disk_count++;
9727 /* slot to fill in autolayout
9728 */
9729 new_disk->raiddisk = new_disk->index;
9730 new_disk->disk.status |=
9731 CONFIGURED_DISK;
9732 new_disk->disk.status &= ~SPARE_DISK;
9733 }
9734
9735 dprintf("imsm: volume set mpb->num_raid_devs = %i\n",
9736 mpb->num_raid_devs);
9737 /* manage changes in volume
9738 */
9739 for (dev_id = 0; dev_id < mpb->num_raid_devs; dev_id++) {
9740 void **sp = *space_list;
9741 struct imsm_dev *newdev;
9742 struct imsm_map *newmap, *oldmap;
9743
9744 for (id = super->devlist ; id; id = id->next) {
9745 if (id->index == dev_id)
9746 break;
9747 }
9748 if (id == NULL)
9749 break;
9750 if (!sp)
9751 continue;
9752 *space_list = *sp;
9753 newdev = (void*)sp;
9754 /* Copy the dev, but not (all of) the map */
9755 memcpy(newdev, id->dev, sizeof(*newdev));
9756 oldmap = get_imsm_map(id->dev, MAP_0);
9757 newmap = get_imsm_map(newdev, MAP_0);
9758 /* Copy the current map */
9759 memcpy(newmap, oldmap, sizeof_imsm_map(oldmap));
9760 /* update one device only
9761 */
9762 if (devices_to_reshape) {
9763 dprintf("imsm: modifying subdev: %i\n",
9764 id->index);
9765 devices_to_reshape--;
9766 newdev->vol.migr_state = 1;
9767 set_vol_curr_migr_unit(newdev, 0);
9768 set_migr_type(newdev, MIGR_GEN_MIGR);
9769 newmap->num_members = u->new_raid_disks;
9770 for (i = 0; i < delta_disks; i++) {
9771 set_imsm_ord_tbl_ent(newmap,
9772 u->old_raid_disks + i,
9773 u->old_raid_disks + i);
9774 }
9775 /* New map is correct, now need to save old map
9776 */
9777 newmap = get_imsm_map(newdev, MAP_1);
9778 memcpy(newmap, oldmap, sizeof_imsm_map(oldmap));
9779
9780 imsm_set_array_size(newdev, -1);
9781 }
9782
9783 sp = (void **)id->dev;
9784 id->dev = newdev;
9785 *sp = tofree;
9786 tofree = sp;
9787
9788 /* Clear migration record */
9789 memset(super->migr_rec, 0, sizeof(struct migr_record));
9790 }
9791 if (tofree)
9792 *space_list = tofree;
9793 ret_val = 1;
9794
9795 update_reshape_exit:
9796
9797 return ret_val;
9798 }
9799
9800 static int apply_takeover_update(struct imsm_update_takeover *u,
9801 struct intel_super *super,
9802 void ***space_list)
9803 {
9804 struct imsm_dev *dev = NULL;
9805 struct intel_dev *dv;
9806 struct imsm_dev *dev_new;
9807 struct imsm_map *map;
9808 struct dl *dm, *du;
9809 int i;
9810
9811 for (dv = super->devlist; dv; dv = dv->next)
9812 if (dv->index == (unsigned int)u->subarray) {
9813 dev = dv->dev;
9814 break;
9815 }
9816
9817 if (dev == NULL)
9818 return 0;
9819
9820 map = get_imsm_map(dev, MAP_0);
9821
9822 if (u->direction == R10_TO_R0) {
9823 unsigned long long num_data_stripes;
9824
9825 /* Number of failed disks must be half of initial disk number */
9826 if (imsm_count_failed(super, dev, MAP_0) !=
9827 (map->num_members / 2))
9828 return 0;
9829
9830 /* iterate through devices to mark removed disks as spare */
9831 for (dm = super->disks; dm; dm = dm->next) {
9832 if (dm->disk.status & FAILED_DISK) {
9833 int idx = dm->index;
9834 /* update indexes on the disk list */
9835 /* FIXME this loop-with-the-loop looks wrong, I'm not convinced
9836 the index values will end up being correct.... NB */
9837 for (du = super->disks; du; du = du->next)
9838 if (du->index > idx)
9839 du->index--;
9840 /* mark as spare disk */
9841 mark_spare(dm);
9842 }
9843 }
9844 /* update map */
9845 map->num_members = map->num_members / 2;
9846 map->map_state = IMSM_T_STATE_NORMAL;
9847 map->num_domains = 1;
9848 map->raid_level = 0;
9849 map->failed_disk_num = -1;
9850 num_data_stripes = imsm_dev_size(dev) / 2;
9851 num_data_stripes /= map->blocks_per_strip;
9852 set_num_data_stripes(map, num_data_stripes);
9853 }
9854
9855 if (u->direction == R0_TO_R10) {
9856 void **space;
9857 unsigned long long num_data_stripes;
9858
9859 /* update slots in current disk list */
9860 for (dm = super->disks; dm; dm = dm->next) {
9861 if (dm->index >= 0)
9862 dm->index *= 2;
9863 }
9864 /* create new *missing* disks */
9865 for (i = 0; i < map->num_members; i++) {
9866 space = *space_list;
9867 if (!space)
9868 continue;
9869 *space_list = *space;
9870 du = (void *)space;
9871 memcpy(du, super->disks, sizeof(*du));
9872 du->fd = -1;
9873 du->minor = 0;
9874 du->major = 0;
9875 du->index = (i * 2) + 1;
9876 sprintf((char *)du->disk.serial,
9877 " MISSING_%d", du->index);
9878 sprintf((char *)du->serial,
9879 "MISSING_%d", du->index);
9880 du->next = super->missing;
9881 super->missing = du;
9882 }
9883 /* create new dev and map */
9884 space = *space_list;
9885 if (!space)
9886 return 0;
9887 *space_list = *space;
9888 dev_new = (void *)space;
9889 memcpy(dev_new, dev, sizeof(*dev));
9890 /* update new map */
9891 map = get_imsm_map(dev_new, MAP_0);
9892 map->num_members = map->num_members * 2;
9893 map->map_state = IMSM_T_STATE_DEGRADED;
9894 map->num_domains = 2;
9895 map->raid_level = 1;
9896 num_data_stripes = imsm_dev_size(dev) / 2;
9897 num_data_stripes /= map->blocks_per_strip;
9898 num_data_stripes /= map->num_domains;
9899 set_num_data_stripes(map, num_data_stripes);
9900
9901 /* replace dev<->dev_new */
9902 dv->dev = dev_new;
9903 }
9904 /* update disk order table */
9905 for (du = super->disks; du; du = du->next)
9906 if (du->index >= 0)
9907 set_imsm_ord_tbl_ent(map, du->index, du->index);
9908 for (du = super->missing; du; du = du->next)
9909 if (du->index >= 0) {
9910 set_imsm_ord_tbl_ent(map, du->index, du->index);
9911 mark_missing(super, dv->dev, &du->disk, du->index);
9912 }
9913
9914 return 1;
9915 }
9916
9917 static void imsm_process_update(struct supertype *st,
9918 struct metadata_update *update)
9919 {
9920 /**
9921 * crack open the metadata_update envelope to find the update record
9922 * update can be one of:
9923 * update_reshape_container_disks - all the arrays in the container
9924 * are being reshaped to have more devices. We need to mark
9925 * the arrays for general migration and convert selected spares
9926 * into active devices.
9927 * update_activate_spare - a spare device has replaced a failed
9928 * device in an array, update the disk_ord_tbl. If this disk is
9929 * present in all member arrays then also clear the SPARE_DISK
9930 * flag
9931 * update_create_array
9932 * update_kill_array
9933 * update_rename_array
9934 * update_add_remove_disk
9935 */
9936 struct intel_super *super = st->sb;
9937 struct imsm_super *mpb;
9938 enum imsm_update_type type = *(enum imsm_update_type *) update->buf;
9939
9940 /* update requires a larger buf but the allocation failed */
9941 if (super->next_len && !super->next_buf) {
9942 super->next_len = 0;
9943 return;
9944 }
9945
9946 if (super->next_buf) {
9947 memcpy(super->next_buf, super->buf, super->len);
9948 free(super->buf);
9949 super->len = super->next_len;
9950 super->buf = super->next_buf;
9951
9952 super->next_len = 0;
9953 super->next_buf = NULL;
9954 }
9955
9956 mpb = super->anchor;
9957
9958 switch (type) {
9959 case update_general_migration_checkpoint: {
9960 struct intel_dev *id;
9961 struct imsm_update_general_migration_checkpoint *u =
9962 (void *)update->buf;
9963
9964 dprintf("called for update_general_migration_checkpoint\n");
9965
9966 /* find device under general migration */
9967 for (id = super->devlist ; id; id = id->next) {
9968 if (is_gen_migration(id->dev)) {
9969 set_vol_curr_migr_unit(id->dev,
9970 u->curr_migr_unit);
9971 super->updates_pending++;
9972 }
9973 }
9974 break;
9975 }
9976 case update_takeover: {
9977 struct imsm_update_takeover *u = (void *)update->buf;
9978 if (apply_takeover_update(u, super, &update->space_list)) {
9979 imsm_update_version_info(super);
9980 super->updates_pending++;
9981 }
9982 break;
9983 }
9984
9985 case update_reshape_container_disks: {
9986 struct imsm_update_reshape *u = (void *)update->buf;
9987 if (apply_reshape_container_disks_update(
9988 u, super, &update->space_list))
9989 super->updates_pending++;
9990 break;
9991 }
9992 case update_reshape_migration: {
9993 struct imsm_update_reshape_migration *u = (void *)update->buf;
9994 if (apply_reshape_migration_update(
9995 u, super, &update->space_list))
9996 super->updates_pending++;
9997 break;
9998 }
9999 case update_size_change: {
10000 struct imsm_update_size_change *u = (void *)update->buf;
10001 if (apply_size_change_update(u, super))
10002 super->updates_pending++;
10003 break;
10004 }
10005 case update_activate_spare: {
10006 struct imsm_update_activate_spare *u = (void *) update->buf;
10007
10008 if (prepare_spare_to_activate(st, u) &&
10009 apply_update_activate_spare(u, super, st->arrays))
10010 super->updates_pending++;
10011 break;
10012 }
10013 case update_create_array: {
10014 /* someone wants to create a new array, we need to be aware of
10015 * a few races/collisions:
10016 * 1/ 'Create' called by two separate instances of mdadm
10017 * 2/ 'Create' versus 'activate_spare': mdadm has chosen
10018 * devices that have since been assimilated via
10019 * activate_spare.
10020 * In the event this update can not be carried out mdadm will
10021 * (FIX ME) notice that its update did not take hold.
10022 */
10023 struct imsm_update_create_array *u = (void *) update->buf;
10024 struct intel_dev *dv;
10025 struct imsm_dev *dev;
10026 struct imsm_map *map, *new_map;
10027 unsigned long long start, end;
10028 unsigned long long new_start, new_end;
10029 int i;
10030 struct disk_info *inf;
10031 struct dl *dl;
10032
10033 /* handle racing creates: first come first serve */
10034 if (u->dev_idx < mpb->num_raid_devs) {
10035 dprintf("subarray %d already defined\n", u->dev_idx);
10036 goto create_error;
10037 }
10038
10039 /* check update is next in sequence */
10040 if (u->dev_idx != mpb->num_raid_devs) {
10041 dprintf("can not create array %d expected index %d\n",
10042 u->dev_idx, mpb->num_raid_devs);
10043 goto create_error;
10044 }
10045
10046 new_map = get_imsm_map(&u->dev, MAP_0);
10047 new_start = pba_of_lba0(new_map);
10048 new_end = new_start + per_dev_array_size(new_map);
10049 inf = get_disk_info(u);
10050
10051 /* handle activate_spare versus create race:
10052 * check to make sure that overlapping arrays do not include
10053 * overalpping disks
10054 */
10055 for (i = 0; i < mpb->num_raid_devs; i++) {
10056 dev = get_imsm_dev(super, i);
10057 map = get_imsm_map(dev, MAP_0);
10058 start = pba_of_lba0(map);
10059 end = start + per_dev_array_size(map);
10060 if ((new_start >= start && new_start <= end) ||
10061 (start >= new_start && start <= new_end))
10062 /* overlap */;
10063 else
10064 continue;
10065
10066 if (disks_overlap(super, i, u)) {
10067 dprintf("arrays overlap\n");
10068 goto create_error;
10069 }
10070 }
10071
10072 /* check that prepare update was successful */
10073 if (!update->space) {
10074 dprintf("prepare update failed\n");
10075 goto create_error;
10076 }
10077
10078 /* check that all disks are still active before committing
10079 * changes. FIXME: could we instead handle this by creating a
10080 * degraded array? That's probably not what the user expects,
10081 * so better to drop this update on the floor.
10082 */
10083 for (i = 0; i < new_map->num_members; i++) {
10084 dl = serial_to_dl(inf[i].serial, super);
10085 if (!dl) {
10086 dprintf("disk disappeared\n");
10087 goto create_error;
10088 }
10089 }
10090
10091 super->updates_pending++;
10092
10093 /* convert spares to members and fixup ord_tbl */
10094 for (i = 0; i < new_map->num_members; i++) {
10095 dl = serial_to_dl(inf[i].serial, super);
10096 if (dl->index == -1) {
10097 dl->index = mpb->num_disks;
10098 mpb->num_disks++;
10099 dl->disk.status |= CONFIGURED_DISK;
10100 dl->disk.status &= ~SPARE_DISK;
10101 }
10102 set_imsm_ord_tbl_ent(new_map, i, dl->index);
10103 }
10104
10105 dv = update->space;
10106 dev = dv->dev;
10107 update->space = NULL;
10108 imsm_copy_dev(dev, &u->dev);
10109 dv->index = u->dev_idx;
10110 dv->next = super->devlist;
10111 super->devlist = dv;
10112 mpb->num_raid_devs++;
10113
10114 imsm_update_version_info(super);
10115 break;
10116 create_error:
10117 /* mdmon knows how to release update->space, but not
10118 * ((struct intel_dev *) update->space)->dev
10119 */
10120 if (update->space) {
10121 dv = update->space;
10122 free(dv->dev);
10123 }
10124 break;
10125 }
10126 case update_kill_array: {
10127 struct imsm_update_kill_array *u = (void *) update->buf;
10128 int victim = u->dev_idx;
10129 struct active_array *a;
10130 struct intel_dev **dp;
10131 struct imsm_dev *dev;
10132
10133 /* sanity check that we are not affecting the uuid of
10134 * active arrays, or deleting an active array
10135 *
10136 * FIXME when immutable ids are available, but note that
10137 * we'll also need to fixup the invalidated/active
10138 * subarray indexes in mdstat
10139 */
10140 for (a = st->arrays; a; a = a->next)
10141 if (a->info.container_member >= victim)
10142 break;
10143 /* by definition if mdmon is running at least one array
10144 * is active in the container, so checking
10145 * mpb->num_raid_devs is just extra paranoia
10146 */
10147 dev = get_imsm_dev(super, victim);
10148 if (a || !dev || mpb->num_raid_devs == 1) {
10149 dprintf("failed to delete subarray-%d\n", victim);
10150 break;
10151 }
10152
10153 for (dp = &super->devlist; *dp;)
10154 if ((*dp)->index == (unsigned)super->current_vol) {
10155 *dp = (*dp)->next;
10156 } else {
10157 if ((*dp)->index > (unsigned)victim)
10158 (*dp)->index--;
10159 dp = &(*dp)->next;
10160 }
10161 mpb->num_raid_devs--;
10162 super->updates_pending++;
10163 break;
10164 }
10165 case update_rename_array: {
10166 struct imsm_update_rename_array *u = (void *) update->buf;
10167 char name[MAX_RAID_SERIAL_LEN+1];
10168 int target = u->dev_idx;
10169 struct active_array *a;
10170 struct imsm_dev *dev;
10171
10172 /* sanity check that we are not affecting the uuid of
10173 * an active array
10174 */
10175 memset(name, 0, sizeof(name));
10176 snprintf(name, MAX_RAID_SERIAL_LEN, "%s", (char *) u->name);
10177 name[MAX_RAID_SERIAL_LEN] = '\0';
10178 for (a = st->arrays; a; a = a->next)
10179 if (a->info.container_member == target)
10180 break;
10181 dev = get_imsm_dev(super, u->dev_idx);
10182 if (a || !dev || !check_name(super, name, 1)) {
10183 dprintf("failed to rename subarray-%d\n", target);
10184 break;
10185 }
10186
10187 memcpy(dev->volume, name, MAX_RAID_SERIAL_LEN);
10188 super->updates_pending++;
10189 break;
10190 }
10191 case update_add_remove_disk: {
10192 /* we may be able to repair some arrays if disks are
10193 * being added, check the status of add_remove_disk
10194 * if discs has been added.
10195 */
10196 if (add_remove_disk_update(super)) {
10197 struct active_array *a;
10198
10199 super->updates_pending++;
10200 for (a = st->arrays; a; a = a->next)
10201 a->check_degraded = 1;
10202 }
10203 break;
10204 }
10205 case update_prealloc_badblocks_mem:
10206 break;
10207 case update_rwh_policy: {
10208 struct imsm_update_rwh_policy *u = (void *)update->buf;
10209 int target = u->dev_idx;
10210 struct imsm_dev *dev = get_imsm_dev(super, target);
10211 if (!dev) {
10212 dprintf("could not find subarray-%d\n", target);
10213 break;
10214 }
10215
10216 if (dev->rwh_policy != u->new_policy) {
10217 dev->rwh_policy = u->new_policy;
10218 super->updates_pending++;
10219 }
10220 break;
10221 }
10222 default:
10223 pr_err("error: unsupported process update type:(type: %d)\n", type);
10224 }
10225 }
10226
10227 static struct mdinfo *get_spares_for_grow(struct supertype *st);
10228
10229 static int imsm_prepare_update(struct supertype *st,
10230 struct metadata_update *update)
10231 {
10232 /**
10233 * Allocate space to hold new disk entries, raid-device entries or a new
10234 * mpb if necessary. The manager synchronously waits for updates to
10235 * complete in the monitor, so new mpb buffers allocated here can be
10236 * integrated by the monitor thread without worrying about live pointers
10237 * in the manager thread.
10238 */
10239 enum imsm_update_type type;
10240 struct intel_super *super = st->sb;
10241 unsigned int sector_size = super->sector_size;
10242 struct imsm_super *mpb = super->anchor;
10243 size_t buf_len;
10244 size_t len = 0;
10245
10246 if (update->len < (int)sizeof(type))
10247 return 0;
10248
10249 type = *(enum imsm_update_type *) update->buf;
10250
10251 switch (type) {
10252 case update_general_migration_checkpoint:
10253 if (update->len < (int)sizeof(struct imsm_update_general_migration_checkpoint))
10254 return 0;
10255 dprintf("called for update_general_migration_checkpoint\n");
10256 break;
10257 case update_takeover: {
10258 struct imsm_update_takeover *u = (void *)update->buf;
10259 if (update->len < (int)sizeof(*u))
10260 return 0;
10261 if (u->direction == R0_TO_R10) {
10262 void **tail = (void **)&update->space_list;
10263 struct imsm_dev *dev = get_imsm_dev(super, u->subarray);
10264 struct imsm_map *map = get_imsm_map(dev, MAP_0);
10265 int num_members = map->num_members;
10266 void *space;
10267 int size, i;
10268 /* allocate memory for added disks */
10269 for (i = 0; i < num_members; i++) {
10270 size = sizeof(struct dl);
10271 space = xmalloc(size);
10272 *tail = space;
10273 tail = space;
10274 *tail = NULL;
10275 }
10276 /* allocate memory for new device */
10277 size = sizeof_imsm_dev(super->devlist->dev, 0) +
10278 (num_members * sizeof(__u32));
10279 space = xmalloc(size);
10280 *tail = space;
10281 tail = space;
10282 *tail = NULL;
10283 len = disks_to_mpb_size(num_members * 2);
10284 }
10285
10286 break;
10287 }
10288 case update_reshape_container_disks: {
10289 /* Every raid device in the container is about to
10290 * gain some more devices, and we will enter a
10291 * reconfiguration.
10292 * So each 'imsm_map' will be bigger, and the imsm_vol
10293 * will now hold 2 of them.
10294 * Thus we need new 'struct imsm_dev' allocations sized
10295 * as sizeof_imsm_dev but with more devices in both maps.
10296 */
10297 struct imsm_update_reshape *u = (void *)update->buf;
10298 struct intel_dev *dl;
10299 void **space_tail = (void**)&update->space_list;
10300
10301 if (update->len < (int)sizeof(*u))
10302 return 0;
10303
10304 dprintf("for update_reshape\n");
10305
10306 for (dl = super->devlist; dl; dl = dl->next) {
10307 int size = sizeof_imsm_dev(dl->dev, 1);
10308 void *s;
10309 if (u->new_raid_disks > u->old_raid_disks)
10310 size += sizeof(__u32)*2*
10311 (u->new_raid_disks - u->old_raid_disks);
10312 s = xmalloc(size);
10313 *space_tail = s;
10314 space_tail = s;
10315 *space_tail = NULL;
10316 }
10317
10318 len = disks_to_mpb_size(u->new_raid_disks);
10319 dprintf("New anchor length is %llu\n", (unsigned long long)len);
10320 break;
10321 }
10322 case update_reshape_migration: {
10323 /* for migration level 0->5 we need to add disks
10324 * so the same as for container operation we will copy
10325 * device to the bigger location.
10326 * in memory prepared device and new disk area are prepared
10327 * for usage in process update
10328 */
10329 struct imsm_update_reshape_migration *u = (void *)update->buf;
10330 struct intel_dev *id;
10331 void **space_tail = (void **)&update->space_list;
10332 int size;
10333 void *s;
10334 int current_level = -1;
10335
10336 if (update->len < (int)sizeof(*u))
10337 return 0;
10338
10339 dprintf("for update_reshape\n");
10340
10341 /* add space for bigger array in update
10342 */
10343 for (id = super->devlist; id; id = id->next) {
10344 if (id->index == (unsigned)u->subdev) {
10345 size = sizeof_imsm_dev(id->dev, 1);
10346 if (u->new_raid_disks > u->old_raid_disks)
10347 size += sizeof(__u32)*2*
10348 (u->new_raid_disks - u->old_raid_disks);
10349 s = xmalloc(size);
10350 *space_tail = s;
10351 space_tail = s;
10352 *space_tail = NULL;
10353 break;
10354 }
10355 }
10356 if (update->space_list == NULL)
10357 break;
10358
10359 /* add space for disk in update
10360 */
10361 size = sizeof(struct dl);
10362 s = xmalloc(size);
10363 *space_tail = s;
10364 space_tail = s;
10365 *space_tail = NULL;
10366
10367 /* add spare device to update
10368 */
10369 for (id = super->devlist ; id; id = id->next)
10370 if (id->index == (unsigned)u->subdev) {
10371 struct imsm_dev *dev;
10372 struct imsm_map *map;
10373
10374 dev = get_imsm_dev(super, u->subdev);
10375 map = get_imsm_map(dev, MAP_0);
10376 current_level = map->raid_level;
10377 break;
10378 }
10379 if (u->new_level == 5 && u->new_level != current_level) {
10380 struct mdinfo *spares;
10381
10382 spares = get_spares_for_grow(st);
10383 if (spares) {
10384 struct dl *dl;
10385 struct mdinfo *dev;
10386
10387 dev = spares->devs;
10388 if (dev) {
10389 u->new_disks[0] =
10390 makedev(dev->disk.major,
10391 dev->disk.minor);
10392 dl = get_disk_super(super,
10393 dev->disk.major,
10394 dev->disk.minor);
10395 dl->index = u->old_raid_disks;
10396 dev = dev->next;
10397 }
10398 sysfs_free(spares);
10399 }
10400 }
10401 len = disks_to_mpb_size(u->new_raid_disks);
10402 dprintf("New anchor length is %llu\n", (unsigned long long)len);
10403 break;
10404 }
10405 case update_size_change: {
10406 if (update->len < (int)sizeof(struct imsm_update_size_change))
10407 return 0;
10408 break;
10409 }
10410 case update_activate_spare: {
10411 if (update->len < (int)sizeof(struct imsm_update_activate_spare))
10412 return 0;
10413 break;
10414 }
10415 case update_create_array: {
10416 struct imsm_update_create_array *u = (void *) update->buf;
10417 struct intel_dev *dv;
10418 struct imsm_dev *dev = &u->dev;
10419 struct imsm_map *map = get_imsm_map(dev, MAP_0);
10420 struct dl *dl;
10421 struct disk_info *inf;
10422 int i;
10423 int activate = 0;
10424
10425 if (update->len < (int)sizeof(*u))
10426 return 0;
10427
10428 inf = get_disk_info(u);
10429 len = sizeof_imsm_dev(dev, 1);
10430 /* allocate a new super->devlist entry */
10431 dv = xmalloc(sizeof(*dv));
10432 dv->dev = xmalloc(len);
10433 update->space = dv;
10434
10435 /* count how many spares will be converted to members */
10436 for (i = 0; i < map->num_members; i++) {
10437 dl = serial_to_dl(inf[i].serial, super);
10438 if (!dl) {
10439 /* hmm maybe it failed?, nothing we can do about
10440 * it here
10441 */
10442 continue;
10443 }
10444 if (count_memberships(dl, super) == 0)
10445 activate++;
10446 }
10447 len += activate * sizeof(struct imsm_disk);
10448 break;
10449 }
10450 case update_kill_array: {
10451 if (update->len < (int)sizeof(struct imsm_update_kill_array))
10452 return 0;
10453 break;
10454 }
10455 case update_rename_array: {
10456 if (update->len < (int)sizeof(struct imsm_update_rename_array))
10457 return 0;
10458 break;
10459 }
10460 case update_add_remove_disk:
10461 /* no update->len needed */
10462 break;
10463 case update_prealloc_badblocks_mem:
10464 super->extra_space += sizeof(struct bbm_log) -
10465 get_imsm_bbm_log_size(super->bbm_log);
10466 break;
10467 case update_rwh_policy: {
10468 if (update->len < (int)sizeof(struct imsm_update_rwh_policy))
10469 return 0;
10470 break;
10471 }
10472 default:
10473 return 0;
10474 }
10475
10476 /* check if we need a larger metadata buffer */
10477 if (super->next_buf)
10478 buf_len = super->next_len;
10479 else
10480 buf_len = super->len;
10481
10482 if (__le32_to_cpu(mpb->mpb_size) + super->extra_space + len > buf_len) {
10483 /* ok we need a larger buf than what is currently allocated
10484 * if this allocation fails process_update will notice that
10485 * ->next_len is set and ->next_buf is NULL
10486 */
10487 buf_len = ROUND_UP(__le32_to_cpu(mpb->mpb_size) +
10488 super->extra_space + len, sector_size);
10489 if (super->next_buf)
10490 free(super->next_buf);
10491
10492 super->next_len = buf_len;
10493 if (posix_memalign(&super->next_buf, sector_size, buf_len) == 0)
10494 memset(super->next_buf, 0, buf_len);
10495 else
10496 super->next_buf = NULL;
10497 }
10498 return 1;
10499 }
10500
10501 /* must be called while manager is quiesced */
10502 static void imsm_delete(struct intel_super *super, struct dl **dlp, unsigned index)
10503 {
10504 struct imsm_super *mpb = super->anchor;
10505 struct dl *iter;
10506 struct imsm_dev *dev;
10507 struct imsm_map *map;
10508 unsigned int i, j, num_members;
10509 __u32 ord, ord_map0;
10510 struct bbm_log *log = super->bbm_log;
10511
10512 dprintf("deleting device[%d] from imsm_super\n", index);
10513
10514 /* shift all indexes down one */
10515 for (iter = super->disks; iter; iter = iter->next)
10516 if (iter->index > (int)index)
10517 iter->index--;
10518 for (iter = super->missing; iter; iter = iter->next)
10519 if (iter->index > (int)index)
10520 iter->index--;
10521
10522 for (i = 0; i < mpb->num_raid_devs; i++) {
10523 dev = get_imsm_dev(super, i);
10524 map = get_imsm_map(dev, MAP_0);
10525 num_members = map->num_members;
10526 for (j = 0; j < num_members; j++) {
10527 /* update ord entries being careful not to propagate
10528 * ord-flags to the first map
10529 */
10530 ord = get_imsm_ord_tbl_ent(dev, j, MAP_X);
10531 ord_map0 = get_imsm_ord_tbl_ent(dev, j, MAP_0);
10532
10533 if (ord_to_idx(ord) <= index)
10534 continue;
10535
10536 map = get_imsm_map(dev, MAP_0);
10537 set_imsm_ord_tbl_ent(map, j, ord_map0 - 1);
10538 map = get_imsm_map(dev, MAP_1);
10539 if (map)
10540 set_imsm_ord_tbl_ent(map, j, ord - 1);
10541 }
10542 }
10543
10544 for (i = 0; i < log->entry_count; i++) {
10545 struct bbm_log_entry *entry = &log->marked_block_entries[i];
10546
10547 if (entry->disk_ordinal <= index)
10548 continue;
10549 entry->disk_ordinal--;
10550 }
10551
10552 mpb->num_disks--;
10553 super->updates_pending++;
10554 if (*dlp) {
10555 struct dl *dl = *dlp;
10556
10557 *dlp = (*dlp)->next;
10558 __free_imsm_disk(dl);
10559 }
10560 }
10561
10562 static int imsm_get_allowed_degradation(int level, int raid_disks,
10563 struct intel_super *super,
10564 struct imsm_dev *dev)
10565 {
10566 switch (level) {
10567 case 1:
10568 case 10:{
10569 int ret_val = 0;
10570 struct imsm_map *map;
10571 int i;
10572
10573 ret_val = raid_disks/2;
10574 /* check map if all disks pairs not failed
10575 * in both maps
10576 */
10577 map = get_imsm_map(dev, MAP_0);
10578 for (i = 0; i < ret_val; i++) {
10579 int degradation = 0;
10580 if (get_imsm_disk(super, i) == NULL)
10581 degradation++;
10582 if (get_imsm_disk(super, i + 1) == NULL)
10583 degradation++;
10584 if (degradation == 2)
10585 return 0;
10586 }
10587 map = get_imsm_map(dev, MAP_1);
10588 /* if there is no second map
10589 * result can be returned
10590 */
10591 if (map == NULL)
10592 return ret_val;
10593 /* check degradation in second map
10594 */
10595 for (i = 0; i < ret_val; i++) {
10596 int degradation = 0;
10597 if (get_imsm_disk(super, i) == NULL)
10598 degradation++;
10599 if (get_imsm_disk(super, i + 1) == NULL)
10600 degradation++;
10601 if (degradation == 2)
10602 return 0;
10603 }
10604 return ret_val;
10605 }
10606 case 5:
10607 return 1;
10608 case 6:
10609 return 2;
10610 default:
10611 return 0;
10612 }
10613 }
10614
10615 /*******************************************************************************
10616 * Function: validate_container_imsm
10617 * Description: This routine validates container after assemble,
10618 * eg. if devices in container are under the same controller.
10619 *
10620 * Parameters:
10621 * info : linked list with info about devices used in array
10622 * Returns:
10623 * 1 : HBA mismatch
10624 * 0 : Success
10625 ******************************************************************************/
10626 int validate_container_imsm(struct mdinfo *info)
10627 {
10628 if (check_env("IMSM_NO_PLATFORM"))
10629 return 0;
10630
10631 struct sys_dev *idev;
10632 struct sys_dev *hba = NULL;
10633 struct sys_dev *intel_devices = find_intel_devices();
10634 char *dev_path = devt_to_devpath(makedev(info->disk.major,
10635 info->disk.minor));
10636
10637 for (idev = intel_devices; idev; idev = idev->next) {
10638 if (dev_path && strstr(dev_path, idev->path)) {
10639 hba = idev;
10640 break;
10641 }
10642 }
10643 if (dev_path)
10644 free(dev_path);
10645
10646 if (!hba) {
10647 pr_err("WARNING - Cannot detect HBA for device %s!\n",
10648 devid2kname(makedev(info->disk.major, info->disk.minor)));
10649 return 1;
10650 }
10651
10652 const struct imsm_orom *orom = get_orom_by_device_id(hba->dev_id);
10653 struct mdinfo *dev;
10654
10655 for (dev = info->next; dev; dev = dev->next) {
10656 dev_path = devt_to_devpath(makedev(dev->disk.major, dev->disk.minor));
10657
10658 struct sys_dev *hba2 = NULL;
10659 for (idev = intel_devices; idev; idev = idev->next) {
10660 if (dev_path && strstr(dev_path, idev->path)) {
10661 hba2 = idev;
10662 break;
10663 }
10664 }
10665 if (dev_path)
10666 free(dev_path);
10667
10668 const struct imsm_orom *orom2 = hba2 == NULL ? NULL :
10669 get_orom_by_device_id(hba2->dev_id);
10670
10671 if (hba2 && hba->type != hba2->type) {
10672 pr_err("WARNING - HBAs of devices do not match %s != %s\n",
10673 get_sys_dev_type(hba->type), get_sys_dev_type(hba2->type));
10674 return 1;
10675 }
10676
10677 if (orom != orom2) {
10678 pr_err("WARNING - IMSM container assembled with disks under different HBAs!\n"
10679 " This operation is not supported and can lead to data loss.\n");
10680 return 1;
10681 }
10682
10683 if (!orom) {
10684 pr_err("WARNING - IMSM container assembled with disks under HBAs without IMSM platform support!\n"
10685 " This operation is not supported and can lead to data loss.\n");
10686 return 1;
10687 }
10688 }
10689
10690 return 0;
10691 }
10692
10693 /*******************************************************************************
10694 * Function: imsm_record_badblock
10695 * Description: This routine stores new bad block record in BBM log
10696 *
10697 * Parameters:
10698 * a : array containing a bad block
10699 * slot : disk number containing a bad block
10700 * sector : bad block sector
10701 * length : bad block sectors range
10702 * Returns:
10703 * 1 : Success
10704 * 0 : Error
10705 ******************************************************************************/
10706 static int imsm_record_badblock(struct active_array *a, int slot,
10707 unsigned long long sector, int length)
10708 {
10709 struct intel_super *super = a->container->sb;
10710 int ord;
10711 int ret;
10712
10713 ord = imsm_disk_slot_to_ord(a, slot);
10714 if (ord < 0)
10715 return 0;
10716
10717 ret = record_new_badblock(super->bbm_log, ord_to_idx(ord), sector,
10718 length);
10719 if (ret)
10720 super->updates_pending++;
10721
10722 return ret;
10723 }
10724 /*******************************************************************************
10725 * Function: imsm_clear_badblock
10726 * Description: This routine clears bad block record from BBM log
10727 *
10728 * Parameters:
10729 * a : array containing a bad block
10730 * slot : disk number containing a bad block
10731 * sector : bad block sector
10732 * length : bad block sectors range
10733 * Returns:
10734 * 1 : Success
10735 * 0 : Error
10736 ******************************************************************************/
10737 static int imsm_clear_badblock(struct active_array *a, int slot,
10738 unsigned long long sector, int length)
10739 {
10740 struct intel_super *super = a->container->sb;
10741 int ord;
10742 int ret;
10743
10744 ord = imsm_disk_slot_to_ord(a, slot);
10745 if (ord < 0)
10746 return 0;
10747
10748 ret = clear_badblock(super->bbm_log, ord_to_idx(ord), sector, length);
10749 if (ret)
10750 super->updates_pending++;
10751
10752 return ret;
10753 }
10754 /*******************************************************************************
10755 * Function: imsm_get_badblocks
10756 * Description: This routine get list of bad blocks for an array
10757 *
10758 * Parameters:
10759 * a : array
10760 * slot : disk number
10761 * Returns:
10762 * bb : structure containing bad blocks
10763 * NULL : error
10764 ******************************************************************************/
10765 static struct md_bb *imsm_get_badblocks(struct active_array *a, int slot)
10766 {
10767 int inst = a->info.container_member;
10768 struct intel_super *super = a->container->sb;
10769 struct imsm_dev *dev = get_imsm_dev(super, inst);
10770 struct imsm_map *map = get_imsm_map(dev, MAP_0);
10771 int ord;
10772
10773 ord = imsm_disk_slot_to_ord(a, slot);
10774 if (ord < 0)
10775 return NULL;
10776
10777 get_volume_badblocks(super->bbm_log, ord_to_idx(ord), pba_of_lba0(map),
10778 per_dev_array_size(map), &super->bb);
10779
10780 return &super->bb;
10781 }
10782 /*******************************************************************************
10783 * Function: examine_badblocks_imsm
10784 * Description: Prints list of bad blocks on a disk to the standard output
10785 *
10786 * Parameters:
10787 * st : metadata handler
10788 * fd : open file descriptor for device
10789 * devname : device name
10790 * Returns:
10791 * 0 : Success
10792 * 1 : Error
10793 ******************************************************************************/
10794 static int examine_badblocks_imsm(struct supertype *st, int fd, char *devname)
10795 {
10796 struct intel_super *super = st->sb;
10797 struct bbm_log *log = super->bbm_log;
10798 struct dl *d = NULL;
10799 int any = 0;
10800
10801 for (d = super->disks; d ; d = d->next) {
10802 if (strcmp(d->devname, devname) == 0)
10803 break;
10804 }
10805
10806 if ((d == NULL) || (d->index < 0)) { /* serial mismatch probably */
10807 pr_err("%s doesn't appear to be part of a raid array\n",
10808 devname);
10809 return 1;
10810 }
10811
10812 if (log != NULL) {
10813 unsigned int i;
10814 struct bbm_log_entry *entry = &log->marked_block_entries[0];
10815
10816 for (i = 0; i < log->entry_count; i++) {
10817 if (entry[i].disk_ordinal == d->index) {
10818 unsigned long long sector = __le48_to_cpu(
10819 &entry[i].defective_block_start);
10820 int cnt = entry[i].marked_count + 1;
10821
10822 if (!any) {
10823 printf("Bad-blocks on %s:\n", devname);
10824 any = 1;
10825 }
10826
10827 printf("%20llu for %d sectors\n", sector, cnt);
10828 }
10829 }
10830 }
10831
10832 if (!any)
10833 printf("No bad-blocks list configured on %s\n", devname);
10834
10835 return 0;
10836 }
10837 /*******************************************************************************
10838 * Function: init_migr_record_imsm
10839 * Description: Function inits imsm migration record
10840 * Parameters:
10841 * super : imsm internal array info
10842 * dev : device under migration
10843 * info : general array info to find the smallest device
10844 * Returns:
10845 * none
10846 ******************************************************************************/
10847 void init_migr_record_imsm(struct supertype *st, struct imsm_dev *dev,
10848 struct mdinfo *info)
10849 {
10850 struct intel_super *super = st->sb;
10851 struct migr_record *migr_rec = super->migr_rec;
10852 int new_data_disks;
10853 unsigned long long dsize, dev_sectors;
10854 long long unsigned min_dev_sectors = -1LLU;
10855 struct imsm_map *map_dest = get_imsm_map(dev, MAP_0);
10856 struct imsm_map *map_src = get_imsm_map(dev, MAP_1);
10857 unsigned long long num_migr_units;
10858 unsigned long long array_blocks;
10859 struct dl *dl_disk = NULL;
10860
10861 memset(migr_rec, 0, sizeof(struct migr_record));
10862 migr_rec->family_num = __cpu_to_le32(super->anchor->family_num);
10863
10864 /* only ascending reshape supported now */
10865 migr_rec->ascending_migr = __cpu_to_le32(1);
10866
10867 migr_rec->dest_depth_per_unit = GEN_MIGR_AREA_SIZE /
10868 max(map_dest->blocks_per_strip, map_src->blocks_per_strip);
10869 migr_rec->dest_depth_per_unit *=
10870 max(map_dest->blocks_per_strip, map_src->blocks_per_strip);
10871 new_data_disks = imsm_num_data_members(map_dest);
10872 migr_rec->blocks_per_unit =
10873 __cpu_to_le32(migr_rec->dest_depth_per_unit * new_data_disks);
10874 migr_rec->dest_depth_per_unit =
10875 __cpu_to_le32(migr_rec->dest_depth_per_unit);
10876 array_blocks = info->component_size * new_data_disks;
10877 num_migr_units =
10878 array_blocks / __le32_to_cpu(migr_rec->blocks_per_unit);
10879
10880 if (array_blocks % __le32_to_cpu(migr_rec->blocks_per_unit))
10881 num_migr_units++;
10882 set_num_migr_units(migr_rec, num_migr_units);
10883
10884 migr_rec->post_migr_vol_cap = dev->size_low;
10885 migr_rec->post_migr_vol_cap_hi = dev->size_high;
10886
10887 /* Find the smallest dev */
10888 for (dl_disk = super->disks; dl_disk ; dl_disk = dl_disk->next) {
10889 /* ignore spares in container */
10890 if (dl_disk->index < 0)
10891 continue;
10892 get_dev_size(dl_disk->fd, NULL, &dsize);
10893 dev_sectors = dsize / 512;
10894 if (dev_sectors < min_dev_sectors)
10895 min_dev_sectors = dev_sectors;
10896 }
10897 set_migr_chkp_area_pba(migr_rec, min_dev_sectors -
10898 RAID_DISK_RESERVED_BLOCKS_IMSM_HI);
10899
10900 write_imsm_migr_rec(st);
10901
10902 return;
10903 }
10904
10905 /*******************************************************************************
10906 * Function: save_backup_imsm
10907 * Description: Function saves critical data stripes to Migration Copy Area
10908 * and updates the current migration unit status.
10909 * Use restore_stripes() to form a destination stripe,
10910 * and to write it to the Copy Area.
10911 * Parameters:
10912 * st : supertype information
10913 * dev : imsm device that backup is saved for
10914 * info : general array info
10915 * buf : input buffer
10916 * length : length of data to backup (blocks_per_unit)
10917 * Returns:
10918 * 0 : success
10919 *, -1 : fail
10920 ******************************************************************************/
10921 int save_backup_imsm(struct supertype *st,
10922 struct imsm_dev *dev,
10923 struct mdinfo *info,
10924 void *buf,
10925 int length)
10926 {
10927 int rv = -1;
10928 struct intel_super *super = st->sb;
10929 unsigned long long *target_offsets;
10930 int *targets;
10931 int i;
10932 struct imsm_map *map_dest = get_imsm_map(dev, MAP_0);
10933 int new_disks = map_dest->num_members;
10934 int dest_layout = 0;
10935 int dest_chunk;
10936 unsigned long long start;
10937 int data_disks = imsm_num_data_members(map_dest);
10938
10939 targets = xmalloc(new_disks * sizeof(int));
10940
10941 for (i = 0; i < new_disks; i++) {
10942 struct dl *dl_disk = get_imsm_dl_disk(super, i);
10943
10944 targets[i] = dl_disk->fd;
10945 }
10946
10947 target_offsets = xcalloc(new_disks, sizeof(unsigned long long));
10948
10949 start = info->reshape_progress * 512;
10950 for (i = 0; i < new_disks; i++) {
10951 target_offsets[i] = migr_chkp_area_pba(super->migr_rec) * 512;
10952 /* move back copy area adderss, it will be moved forward
10953 * in restore_stripes() using start input variable
10954 */
10955 target_offsets[i] -= start/data_disks;
10956 }
10957
10958 dest_layout = imsm_level_to_layout(map_dest->raid_level);
10959 dest_chunk = __le16_to_cpu(map_dest->blocks_per_strip) * 512;
10960
10961 if (restore_stripes(targets, /* list of dest devices */
10962 target_offsets, /* migration record offsets */
10963 new_disks,
10964 dest_chunk,
10965 map_dest->raid_level,
10966 dest_layout,
10967 -1, /* source backup file descriptor */
10968 0, /* input buf offset
10969 * always 0 buf is already offseted */
10970 start,
10971 length,
10972 buf) != 0) {
10973 pr_err("Error restoring stripes\n");
10974 goto abort;
10975 }
10976
10977 rv = 0;
10978
10979 abort:
10980 if (targets) {
10981 free(targets);
10982 }
10983 free(target_offsets);
10984
10985 return rv;
10986 }
10987
10988 /*******************************************************************************
10989 * Function: save_checkpoint_imsm
10990 * Description: Function called for current unit status update
10991 * in the migration record. It writes it to disk.
10992 * Parameters:
10993 * super : imsm internal array info
10994 * info : general array info
10995 * Returns:
10996 * 0: success
10997 * 1: failure
10998 * 2: failure, means no valid migration record
10999 * / no general migration in progress /
11000 ******************************************************************************/
11001 int save_checkpoint_imsm(struct supertype *st, struct mdinfo *info, int state)
11002 {
11003 struct intel_super *super = st->sb;
11004 unsigned long long blocks_per_unit;
11005 unsigned long long curr_migr_unit;
11006
11007 if (load_imsm_migr_rec(super) != 0) {
11008 dprintf("imsm: ERROR: Cannot read migration record for checkpoint save.\n");
11009 return 1;
11010 }
11011
11012 blocks_per_unit = __le32_to_cpu(super->migr_rec->blocks_per_unit);
11013 if (blocks_per_unit == 0) {
11014 dprintf("imsm: no migration in progress.\n");
11015 return 2;
11016 }
11017 curr_migr_unit = info->reshape_progress / blocks_per_unit;
11018 /* check if array is alligned to copy area
11019 * if it is not alligned, add one to current migration unit value
11020 * this can happend on array reshape finish only
11021 */
11022 if (info->reshape_progress % blocks_per_unit)
11023 curr_migr_unit++;
11024
11025 set_current_migr_unit(super->migr_rec, curr_migr_unit);
11026 super->migr_rec->rec_status = __cpu_to_le32(state);
11027 set_migr_dest_1st_member_lba(super->migr_rec,
11028 super->migr_rec->dest_depth_per_unit * curr_migr_unit);
11029
11030 if (write_imsm_migr_rec(st) < 0) {
11031 dprintf("imsm: Cannot write migration record outside backup area\n");
11032 return 1;
11033 }
11034
11035 return 0;
11036 }
11037
11038 /*******************************************************************************
11039 * Function: recover_backup_imsm
11040 * Description: Function recovers critical data from the Migration Copy Area
11041 * while assembling an array.
11042 * Parameters:
11043 * super : imsm internal array info
11044 * info : general array info
11045 * Returns:
11046 * 0 : success (or there is no data to recover)
11047 * 1 : fail
11048 ******************************************************************************/
11049 int recover_backup_imsm(struct supertype *st, struct mdinfo *info)
11050 {
11051 struct intel_super *super = st->sb;
11052 struct migr_record *migr_rec = super->migr_rec;
11053 struct imsm_map *map_dest;
11054 struct intel_dev *id = NULL;
11055 unsigned long long read_offset;
11056 unsigned long long write_offset;
11057 unsigned unit_len;
11058 int new_disks, err;
11059 char *buf = NULL;
11060 int retval = 1;
11061 unsigned int sector_size = super->sector_size;
11062 unsigned long long curr_migr_unit = current_migr_unit(migr_rec);
11063 unsigned long long num_migr_units = get_num_migr_units(migr_rec);
11064 char buffer[20];
11065 int skipped_disks = 0;
11066 struct dl *dl_disk;
11067
11068 err = sysfs_get_str(info, NULL, "array_state", (char *)buffer, 20);
11069 if (err < 1)
11070 return 1;
11071
11072 /* recover data only during assemblation */
11073 if (strncmp(buffer, "inactive", 8) != 0)
11074 return 0;
11075 /* no data to recover */
11076 if (__le32_to_cpu(migr_rec->rec_status) == UNIT_SRC_NORMAL)
11077 return 0;
11078 if (curr_migr_unit >= num_migr_units)
11079 return 1;
11080
11081 /* find device during reshape */
11082 for (id = super->devlist; id; id = id->next)
11083 if (is_gen_migration(id->dev))
11084 break;
11085 if (id == NULL)
11086 return 1;
11087
11088 map_dest = get_imsm_map(id->dev, MAP_0);
11089 new_disks = map_dest->num_members;
11090
11091 read_offset = migr_chkp_area_pba(migr_rec) * 512;
11092
11093 write_offset = (migr_dest_1st_member_lba(migr_rec) +
11094 pba_of_lba0(map_dest)) * 512;
11095
11096 unit_len = __le32_to_cpu(migr_rec->dest_depth_per_unit) * 512;
11097 if (posix_memalign((void **)&buf, sector_size, unit_len) != 0)
11098 goto abort;
11099
11100 for (dl_disk = super->disks; dl_disk; dl_disk = dl_disk->next) {
11101 if (dl_disk->index < 0)
11102 continue;
11103
11104 if (dl_disk->fd < 0) {
11105 skipped_disks++;
11106 continue;
11107 }
11108 if (lseek64(dl_disk->fd, read_offset, SEEK_SET) < 0) {
11109 pr_err("Cannot seek to block: %s\n",
11110 strerror(errno));
11111 skipped_disks++;
11112 continue;
11113 }
11114 if (read(dl_disk->fd, buf, unit_len) != unit_len) {
11115 pr_err("Cannot read copy area block: %s\n",
11116 strerror(errno));
11117 skipped_disks++;
11118 continue;
11119 }
11120 if (lseek64(dl_disk->fd, write_offset, SEEK_SET) < 0) {
11121 pr_err("Cannot seek to block: %s\n",
11122 strerror(errno));
11123 skipped_disks++;
11124 continue;
11125 }
11126 if (write(dl_disk->fd, buf, unit_len) != unit_len) {
11127 pr_err("Cannot restore block: %s\n",
11128 strerror(errno));
11129 skipped_disks++;
11130 continue;
11131 }
11132 }
11133
11134 if (skipped_disks > imsm_get_allowed_degradation(info->new_level,
11135 new_disks,
11136 super,
11137 id->dev)) {
11138 pr_err("Cannot restore data from backup. Too many failed disks\n");
11139 goto abort;
11140 }
11141
11142 if (save_checkpoint_imsm(st, info, UNIT_SRC_NORMAL)) {
11143 /* ignore error == 2, this can mean end of reshape here
11144 */
11145 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL) during restart\n");
11146 } else
11147 retval = 0;
11148
11149 abort:
11150 free(buf);
11151 return retval;
11152 }
11153
11154 static char disk_by_path[] = "/dev/disk/by-path/";
11155
11156 static const char *imsm_get_disk_controller_domain(const char *path)
11157 {
11158 char disk_path[PATH_MAX];
11159 char *drv=NULL;
11160 struct stat st;
11161
11162 strcpy(disk_path, disk_by_path);
11163 strncat(disk_path, path, PATH_MAX - strlen(disk_path) - 1);
11164 if (stat(disk_path, &st) == 0) {
11165 struct sys_dev* hba;
11166 char *path;
11167
11168 path = devt_to_devpath(st.st_rdev);
11169 if (path == NULL)
11170 return "unknown";
11171 hba = find_disk_attached_hba(-1, path);
11172 if (hba && hba->type == SYS_DEV_SAS)
11173 drv = "isci";
11174 else if (hba && hba->type == SYS_DEV_SATA)
11175 drv = "ahci";
11176 else if (hba && hba->type == SYS_DEV_VMD)
11177 drv = "vmd";
11178 else if (hba && hba->type == SYS_DEV_NVME)
11179 drv = "nvme";
11180 else
11181 drv = "unknown";
11182 dprintf("path: %s hba: %s attached: %s\n",
11183 path, (hba) ? hba->path : "NULL", drv);
11184 free(path);
11185 }
11186 return drv;
11187 }
11188
11189 static char *imsm_find_array_devnm_by_subdev(int subdev, char *container)
11190 {
11191 static char devnm[32];
11192 char subdev_name[20];
11193 struct mdstat_ent *mdstat;
11194
11195 sprintf(subdev_name, "%d", subdev);
11196 mdstat = mdstat_by_subdev(subdev_name, container);
11197 if (!mdstat)
11198 return NULL;
11199
11200 strcpy(devnm, mdstat->devnm);
11201 free_mdstat(mdstat);
11202 return devnm;
11203 }
11204
11205 static int imsm_reshape_is_allowed_on_container(struct supertype *st,
11206 struct geo_params *geo,
11207 int *old_raid_disks,
11208 int direction)
11209 {
11210 /* currently we only support increasing the number of devices
11211 * for a container. This increases the number of device for each
11212 * member array. They must all be RAID0 or RAID5.
11213 */
11214 int ret_val = 0;
11215 struct mdinfo *info, *member;
11216 int devices_that_can_grow = 0;
11217
11218 dprintf("imsm: imsm_reshape_is_allowed_on_container(ENTER): st->devnm = (%s)\n", st->devnm);
11219
11220 if (geo->size > 0 ||
11221 geo->level != UnSet ||
11222 geo->layout != UnSet ||
11223 geo->chunksize != 0 ||
11224 geo->raid_disks == UnSet) {
11225 dprintf("imsm: Container operation is allowed for raid disks number change only.\n");
11226 return ret_val;
11227 }
11228
11229 if (direction == ROLLBACK_METADATA_CHANGES) {
11230 dprintf("imsm: Metadata changes rollback is not supported for container operation.\n");
11231 return ret_val;
11232 }
11233
11234 info = container_content_imsm(st, NULL);
11235 for (member = info; member; member = member->next) {
11236 char *result;
11237
11238 dprintf("imsm: checking device_num: %i\n",
11239 member->container_member);
11240
11241 if (geo->raid_disks <= member->array.raid_disks) {
11242 /* we work on container for Online Capacity Expansion
11243 * only so raid_disks has to grow
11244 */
11245 dprintf("imsm: for container operation raid disks increase is required\n");
11246 break;
11247 }
11248
11249 if (info->array.level != 0 && info->array.level != 5) {
11250 /* we cannot use this container with other raid level
11251 */
11252 dprintf("imsm: for container operation wrong raid level (%i) detected\n",
11253 info->array.level);
11254 break;
11255 } else {
11256 /* check for platform support
11257 * for this raid level configuration
11258 */
11259 struct intel_super *super = st->sb;
11260 if (!is_raid_level_supported(super->orom,
11261 member->array.level,
11262 geo->raid_disks)) {
11263 dprintf("platform does not support raid%d with %d disk%s\n",
11264 info->array.level,
11265 geo->raid_disks,
11266 geo->raid_disks > 1 ? "s" : "");
11267 break;
11268 }
11269 /* check if component size is aligned to chunk size
11270 */
11271 if (info->component_size %
11272 (info->array.chunk_size/512)) {
11273 dprintf("Component size is not aligned to chunk size\n");
11274 break;
11275 }
11276 }
11277
11278 if (*old_raid_disks &&
11279 info->array.raid_disks != *old_raid_disks)
11280 break;
11281 *old_raid_disks = info->array.raid_disks;
11282
11283 /* All raid5 and raid0 volumes in container
11284 * have to be ready for Online Capacity Expansion
11285 * so they need to be assembled. We have already
11286 * checked that no recovery etc is happening.
11287 */
11288 result = imsm_find_array_devnm_by_subdev(member->container_member,
11289 st->container_devnm);
11290 if (result == NULL) {
11291 dprintf("imsm: cannot find array\n");
11292 break;
11293 }
11294 devices_that_can_grow++;
11295 }
11296 sysfs_free(info);
11297 if (!member && devices_that_can_grow)
11298 ret_val = 1;
11299
11300 if (ret_val)
11301 dprintf("Container operation allowed\n");
11302 else
11303 dprintf("Error: %i\n", ret_val);
11304
11305 return ret_val;
11306 }
11307
11308 /* Function: get_spares_for_grow
11309 * Description: Allocates memory and creates list of spare devices
11310 * avaliable in container. Checks if spare drive size is acceptable.
11311 * Parameters: Pointer to the supertype structure
11312 * Returns: Pointer to the list of spare devices (mdinfo structure) on success,
11313 * NULL if fail
11314 */
11315 static struct mdinfo *get_spares_for_grow(struct supertype *st)
11316 {
11317 struct spare_criteria sc;
11318
11319 get_spare_criteria_imsm(st, &sc);
11320 return container_choose_spares(st, &sc, NULL, NULL, NULL, 0);
11321 }
11322
11323 /******************************************************************************
11324 * function: imsm_create_metadata_update_for_reshape
11325 * Function creates update for whole IMSM container.
11326 *
11327 ******************************************************************************/
11328 static int imsm_create_metadata_update_for_reshape(
11329 struct supertype *st,
11330 struct geo_params *geo,
11331 int old_raid_disks,
11332 struct imsm_update_reshape **updatep)
11333 {
11334 struct intel_super *super = st->sb;
11335 struct imsm_super *mpb = super->anchor;
11336 int update_memory_size;
11337 struct imsm_update_reshape *u;
11338 struct mdinfo *spares;
11339 int i;
11340 int delta_disks;
11341 struct mdinfo *dev;
11342
11343 dprintf("(enter) raid_disks = %i\n", geo->raid_disks);
11344
11345 delta_disks = geo->raid_disks - old_raid_disks;
11346
11347 /* size of all update data without anchor */
11348 update_memory_size = sizeof(struct imsm_update_reshape);
11349
11350 /* now add space for spare disks that we need to add. */
11351 update_memory_size += sizeof(u->new_disks[0]) * (delta_disks - 1);
11352
11353 u = xcalloc(1, update_memory_size);
11354 u->type = update_reshape_container_disks;
11355 u->old_raid_disks = old_raid_disks;
11356 u->new_raid_disks = geo->raid_disks;
11357
11358 /* now get spare disks list
11359 */
11360 spares = get_spares_for_grow(st);
11361
11362 if (spares == NULL || delta_disks > spares->array.spare_disks) {
11363 pr_err("imsm: ERROR: Cannot get spare devices for %s.\n", geo->dev_name);
11364 i = -1;
11365 goto abort;
11366 }
11367
11368 /* we have got spares
11369 * update disk list in imsm_disk list table in anchor
11370 */
11371 dprintf("imsm: %i spares are available.\n\n",
11372 spares->array.spare_disks);
11373
11374 dev = spares->devs;
11375 for (i = 0; i < delta_disks; i++) {
11376 struct dl *dl;
11377
11378 if (dev == NULL)
11379 break;
11380 u->new_disks[i] = makedev(dev->disk.major,
11381 dev->disk.minor);
11382 dl = get_disk_super(super, dev->disk.major, dev->disk.minor);
11383 dl->index = mpb->num_disks;
11384 mpb->num_disks++;
11385 dev = dev->next;
11386 }
11387
11388 abort:
11389 /* free spares
11390 */
11391 sysfs_free(spares);
11392
11393 dprintf("imsm: reshape update preparation :");
11394 if (i == delta_disks) {
11395 dprintf_cont(" OK\n");
11396 *updatep = u;
11397 return update_memory_size;
11398 }
11399 free(u);
11400 dprintf_cont(" Error\n");
11401
11402 return 0;
11403 }
11404
11405 /******************************************************************************
11406 * function: imsm_create_metadata_update_for_size_change()
11407 * Creates update for IMSM array for array size change.
11408 *
11409 ******************************************************************************/
11410 static int imsm_create_metadata_update_for_size_change(
11411 struct supertype *st,
11412 struct geo_params *geo,
11413 struct imsm_update_size_change **updatep)
11414 {
11415 struct intel_super *super = st->sb;
11416 int update_memory_size;
11417 struct imsm_update_size_change *u;
11418
11419 dprintf("(enter) New size = %llu\n", geo->size);
11420
11421 /* size of all update data without anchor */
11422 update_memory_size = sizeof(struct imsm_update_size_change);
11423
11424 u = xcalloc(1, update_memory_size);
11425 u->type = update_size_change;
11426 u->subdev = super->current_vol;
11427 u->new_size = geo->size;
11428
11429 dprintf("imsm: reshape update preparation : OK\n");
11430 *updatep = u;
11431
11432 return update_memory_size;
11433 }
11434
11435 /******************************************************************************
11436 * function: imsm_create_metadata_update_for_migration()
11437 * Creates update for IMSM array.
11438 *
11439 ******************************************************************************/
11440 static int imsm_create_metadata_update_for_migration(
11441 struct supertype *st,
11442 struct geo_params *geo,
11443 struct imsm_update_reshape_migration **updatep)
11444 {
11445 struct intel_super *super = st->sb;
11446 int update_memory_size;
11447 struct imsm_update_reshape_migration *u;
11448 struct imsm_dev *dev;
11449 int previous_level = -1;
11450
11451 dprintf("(enter) New Level = %i\n", geo->level);
11452
11453 /* size of all update data without anchor */
11454 update_memory_size = sizeof(struct imsm_update_reshape_migration);
11455
11456 u = xcalloc(1, update_memory_size);
11457 u->type = update_reshape_migration;
11458 u->subdev = super->current_vol;
11459 u->new_level = geo->level;
11460 u->new_layout = geo->layout;
11461 u->new_raid_disks = u->old_raid_disks = geo->raid_disks;
11462 u->new_disks[0] = -1;
11463 u->new_chunksize = -1;
11464
11465 dev = get_imsm_dev(super, u->subdev);
11466 if (dev) {
11467 struct imsm_map *map;
11468
11469 map = get_imsm_map(dev, MAP_0);
11470 if (map) {
11471 int current_chunk_size =
11472 __le16_to_cpu(map->blocks_per_strip) / 2;
11473
11474 if (geo->chunksize != current_chunk_size) {
11475 u->new_chunksize = geo->chunksize / 1024;
11476 dprintf("imsm: chunk size change from %i to %i\n",
11477 current_chunk_size, u->new_chunksize);
11478 }
11479 previous_level = map->raid_level;
11480 }
11481 }
11482 if (geo->level == 5 && previous_level == 0) {
11483 struct mdinfo *spares = NULL;
11484
11485 u->new_raid_disks++;
11486 spares = get_spares_for_grow(st);
11487 if (spares == NULL || spares->array.spare_disks < 1) {
11488 free(u);
11489 sysfs_free(spares);
11490 update_memory_size = 0;
11491 pr_err("cannot get spare device for requested migration\n");
11492 return 0;
11493 }
11494 sysfs_free(spares);
11495 }
11496 dprintf("imsm: reshape update preparation : OK\n");
11497 *updatep = u;
11498
11499 return update_memory_size;
11500 }
11501
11502 static void imsm_update_metadata_locally(struct supertype *st,
11503 void *buf, int len)
11504 {
11505 struct metadata_update mu;
11506
11507 mu.buf = buf;
11508 mu.len = len;
11509 mu.space = NULL;
11510 mu.space_list = NULL;
11511 mu.next = NULL;
11512 if (imsm_prepare_update(st, &mu))
11513 imsm_process_update(st, &mu);
11514
11515 while (mu.space_list) {
11516 void **space = mu.space_list;
11517 mu.space_list = *space;
11518 free(space);
11519 }
11520 }
11521
11522 /***************************************************************************
11523 * Function: imsm_analyze_change
11524 * Description: Function analyze change for single volume
11525 * and validate if transition is supported
11526 * Parameters: Geometry parameters, supertype structure,
11527 * metadata change direction (apply/rollback)
11528 * Returns: Operation type code on success, -1 if fail
11529 ****************************************************************************/
11530 enum imsm_reshape_type imsm_analyze_change(struct supertype *st,
11531 struct geo_params *geo,
11532 int direction)
11533 {
11534 struct mdinfo info;
11535 int change = -1;
11536 int check_devs = 0;
11537 int chunk;
11538 /* number of added/removed disks in operation result */
11539 int devNumChange = 0;
11540 /* imsm compatible layout value for array geometry verification */
11541 int imsm_layout = -1;
11542 int data_disks;
11543 struct imsm_dev *dev;
11544 struct imsm_map *map;
11545 struct intel_super *super;
11546 unsigned long long current_size;
11547 unsigned long long free_size;
11548 unsigned long long max_size;
11549 int rv;
11550
11551 getinfo_super_imsm_volume(st, &info, NULL);
11552 if (geo->level != info.array.level && geo->level >= 0 &&
11553 geo->level != UnSet) {
11554 switch (info.array.level) {
11555 case 0:
11556 if (geo->level == 5) {
11557 change = CH_MIGRATION;
11558 if (geo->layout != ALGORITHM_LEFT_ASYMMETRIC) {
11559 pr_err("Error. Requested Layout not supported (left-asymmetric layout is supported only)!\n");
11560 change = -1;
11561 goto analyse_change_exit;
11562 }
11563 imsm_layout = geo->layout;
11564 check_devs = 1;
11565 devNumChange = 1; /* parity disk added */
11566 } else if (geo->level == 10) {
11567 change = CH_TAKEOVER;
11568 check_devs = 1;
11569 devNumChange = 2; /* two mirrors added */
11570 imsm_layout = 0x102; /* imsm supported layout */
11571 }
11572 break;
11573 case 1:
11574 case 10:
11575 if (geo->level == 0) {
11576 change = CH_TAKEOVER;
11577 check_devs = 1;
11578 devNumChange = -(geo->raid_disks/2);
11579 imsm_layout = 0; /* imsm raid0 layout */
11580 }
11581 break;
11582 }
11583 if (change == -1) {
11584 pr_err("Error. Level Migration from %d to %d not supported!\n",
11585 info.array.level, geo->level);
11586 goto analyse_change_exit;
11587 }
11588 } else
11589 geo->level = info.array.level;
11590
11591 if (geo->layout != info.array.layout &&
11592 (geo->layout != UnSet && geo->layout != -1)) {
11593 change = CH_MIGRATION;
11594 if (info.array.layout == 0 && info.array.level == 5 &&
11595 geo->layout == 5) {
11596 /* reshape 5 -> 4 */
11597 } else if (info.array.layout == 5 && info.array.level == 5 &&
11598 geo->layout == 0) {
11599 /* reshape 4 -> 5 */
11600 geo->layout = 0;
11601 geo->level = 5;
11602 } else {
11603 pr_err("Error. Layout Migration from %d to %d not supported!\n",
11604 info.array.layout, geo->layout);
11605 change = -1;
11606 goto analyse_change_exit;
11607 }
11608 } else {
11609 geo->layout = info.array.layout;
11610 if (imsm_layout == -1)
11611 imsm_layout = info.array.layout;
11612 }
11613
11614 if (geo->chunksize > 0 && geo->chunksize != UnSet &&
11615 geo->chunksize != info.array.chunk_size) {
11616 if (info.array.level == 10) {
11617 pr_err("Error. Chunk size change for RAID 10 is not supported.\n");
11618 change = -1;
11619 goto analyse_change_exit;
11620 } else if (info.component_size % (geo->chunksize/512)) {
11621 pr_err("New chunk size (%dK) does not evenly divide device size (%lluk). Aborting...\n",
11622 geo->chunksize/1024, info.component_size/2);
11623 change = -1;
11624 goto analyse_change_exit;
11625 }
11626 change = CH_MIGRATION;
11627 } else {
11628 geo->chunksize = info.array.chunk_size;
11629 }
11630
11631 chunk = geo->chunksize / 1024;
11632
11633 super = st->sb;
11634 dev = get_imsm_dev(super, super->current_vol);
11635 map = get_imsm_map(dev, MAP_0);
11636 data_disks = imsm_num_data_members(map);
11637 /* compute current size per disk member
11638 */
11639 current_size = info.custom_array_size / data_disks;
11640
11641 if (geo->size > 0 && geo->size != MAX_SIZE) {
11642 /* align component size
11643 */
11644 geo->size = imsm_component_size_alignment_check(
11645 get_imsm_raid_level(dev->vol.map),
11646 chunk * 1024, super->sector_size,
11647 geo->size * 2);
11648 if (geo->size == 0) {
11649 pr_err("Error. Size expansion is supported only (current size is %llu, requested size /rounded/ is 0).\n",
11650 current_size);
11651 goto analyse_change_exit;
11652 }
11653 }
11654
11655 if (current_size != geo->size && geo->size > 0) {
11656 if (change != -1) {
11657 pr_err("Error. Size change should be the only one at a time.\n");
11658 change = -1;
11659 goto analyse_change_exit;
11660 }
11661 if ((super->current_vol + 1) != super->anchor->num_raid_devs) {
11662 pr_err("Error. The last volume in container can be expanded only (%i/%s).\n",
11663 super->current_vol, st->devnm);
11664 goto analyse_change_exit;
11665 }
11666 /* check the maximum available size
11667 */
11668 rv = imsm_get_free_size(st, dev->vol.map->num_members,
11669 0, chunk, &free_size);
11670 if (rv == 0)
11671 /* Cannot find maximum available space
11672 */
11673 max_size = 0;
11674 else {
11675 max_size = free_size + current_size;
11676 /* align component size
11677 */
11678 max_size = imsm_component_size_alignment_check(
11679 get_imsm_raid_level(dev->vol.map),
11680 chunk * 1024, super->sector_size,
11681 max_size);
11682 }
11683 if (geo->size == MAX_SIZE) {
11684 /* requested size change to the maximum available size
11685 */
11686 if (max_size == 0) {
11687 pr_err("Error. Cannot find maximum available space.\n");
11688 change = -1;
11689 goto analyse_change_exit;
11690 } else
11691 geo->size = max_size;
11692 }
11693
11694 if (direction == ROLLBACK_METADATA_CHANGES) {
11695 /* accept size for rollback only
11696 */
11697 } else {
11698 /* round size due to metadata compatibility
11699 */
11700 geo->size = (geo->size >> SECT_PER_MB_SHIFT)
11701 << SECT_PER_MB_SHIFT;
11702 dprintf("Prepare update for size change to %llu\n",
11703 geo->size );
11704 if (current_size >= geo->size) {
11705 pr_err("Error. Size expansion is supported only (current size is %llu, requested size /rounded/ is %llu).\n",
11706 current_size, geo->size);
11707 goto analyse_change_exit;
11708 }
11709 if (max_size && geo->size > max_size) {
11710 pr_err("Error. Requested size is larger than maximum available size (maximum available size is %llu, requested size /rounded/ is %llu).\n",
11711 max_size, geo->size);
11712 goto analyse_change_exit;
11713 }
11714 }
11715 geo->size *= data_disks;
11716 geo->raid_disks = dev->vol.map->num_members;
11717 change = CH_ARRAY_SIZE;
11718 }
11719 if (!validate_geometry_imsm(st,
11720 geo->level,
11721 imsm_layout,
11722 geo->raid_disks + devNumChange,
11723 &chunk,
11724 geo->size, INVALID_SECTORS,
11725 0, 0, info.consistency_policy, 1))
11726 change = -1;
11727
11728 if (check_devs) {
11729 struct intel_super *super = st->sb;
11730 struct imsm_super *mpb = super->anchor;
11731
11732 if (mpb->num_raid_devs > 1) {
11733 pr_err("Error. Cannot perform operation on %s- for this operation it MUST be single array in container\n",
11734 geo->dev_name);
11735 change = -1;
11736 }
11737 }
11738
11739 analyse_change_exit:
11740 if (direction == ROLLBACK_METADATA_CHANGES &&
11741 (change == CH_MIGRATION || change == CH_TAKEOVER)) {
11742 dprintf("imsm: Metadata changes rollback is not supported for migration and takeover operations.\n");
11743 change = -1;
11744 }
11745 return change;
11746 }
11747
11748 int imsm_takeover(struct supertype *st, struct geo_params *geo)
11749 {
11750 struct intel_super *super = st->sb;
11751 struct imsm_update_takeover *u;
11752
11753 u = xmalloc(sizeof(struct imsm_update_takeover));
11754
11755 u->type = update_takeover;
11756 u->subarray = super->current_vol;
11757
11758 /* 10->0 transition */
11759 if (geo->level == 0)
11760 u->direction = R10_TO_R0;
11761
11762 /* 0->10 transition */
11763 if (geo->level == 10)
11764 u->direction = R0_TO_R10;
11765
11766 /* update metadata locally */
11767 imsm_update_metadata_locally(st, u,
11768 sizeof(struct imsm_update_takeover));
11769 /* and possibly remotely */
11770 if (st->update_tail)
11771 append_metadata_update(st, u,
11772 sizeof(struct imsm_update_takeover));
11773 else
11774 free(u);
11775
11776 return 0;
11777 }
11778
11779 /* Flush size update if size calculated by num_data_stripes is higher than
11780 * imsm_dev_size to eliminate differences during reshape.
11781 * Mdmon will recalculate them correctly.
11782 * If subarray index is not set then check whole container.
11783 * Returns:
11784 * 0 - no error occurred
11785 * 1 - error detected
11786 */
11787 static int imsm_fix_size_mismatch(struct supertype *st, int subarray_index)
11788 {
11789 struct intel_super *super = st->sb;
11790 int tmp = super->current_vol;
11791 int ret_val = 1;
11792 int i;
11793
11794 for (i = 0; i < super->anchor->num_raid_devs; i++) {
11795 if (subarray_index >= 0 && i != subarray_index)
11796 continue;
11797 super->current_vol = i;
11798 struct imsm_dev *dev = get_imsm_dev(super, super->current_vol);
11799 struct imsm_map *map = get_imsm_map(dev, MAP_0);
11800 unsigned int disc_count = imsm_num_data_members(map);
11801 struct geo_params geo;
11802 struct imsm_update_size_change *update;
11803 unsigned long long calc_size = per_dev_array_size(map) * disc_count;
11804 unsigned long long d_size = imsm_dev_size(dev);
11805 int u_size;
11806
11807 if (calc_size == d_size || dev->vol.migr_type == MIGR_GEN_MIGR)
11808 continue;
11809
11810 /* There is a difference, verify that imsm_dev_size is
11811 * rounded correctly and push update.
11812 */
11813 if (d_size != round_size_to_mb(d_size, disc_count)) {
11814 dprintf("imsm: Size of volume %d is not rounded correctly\n",
11815 i);
11816 goto exit;
11817 }
11818 memset(&geo, 0, sizeof(struct geo_params));
11819 geo.size = d_size;
11820 u_size = imsm_create_metadata_update_for_size_change(st, &geo,
11821 &update);
11822 if (u_size < 1) {
11823 dprintf("imsm: Cannot prepare size change update\n");
11824 goto exit;
11825 }
11826 imsm_update_metadata_locally(st, update, u_size);
11827 if (st->update_tail) {
11828 append_metadata_update(st, update, u_size);
11829 flush_metadata_updates(st);
11830 st->update_tail = &st->updates;
11831 } else {
11832 imsm_sync_metadata(st);
11833 }
11834 }
11835 ret_val = 0;
11836 exit:
11837 super->current_vol = tmp;
11838 return ret_val;
11839 }
11840
11841 static int imsm_reshape_super(struct supertype *st, unsigned long long size,
11842 int level,
11843 int layout, int chunksize, int raid_disks,
11844 int delta_disks, char *backup, char *dev,
11845 int direction, int verbose)
11846 {
11847 int ret_val = 1;
11848 struct geo_params geo;
11849
11850 dprintf("(enter)\n");
11851
11852 memset(&geo, 0, sizeof(struct geo_params));
11853
11854 geo.dev_name = dev;
11855 strcpy(geo.devnm, st->devnm);
11856 geo.size = size;
11857 geo.level = level;
11858 geo.layout = layout;
11859 geo.chunksize = chunksize;
11860 geo.raid_disks = raid_disks;
11861 if (delta_disks != UnSet)
11862 geo.raid_disks += delta_disks;
11863
11864 dprintf("for level : %i\n", geo.level);
11865 dprintf("for raid_disks : %i\n", geo.raid_disks);
11866
11867 if (strcmp(st->container_devnm, st->devnm) == 0) {
11868 /* On container level we can only increase number of devices. */
11869 dprintf("imsm: info: Container operation\n");
11870 int old_raid_disks = 0;
11871
11872 if (imsm_reshape_is_allowed_on_container(
11873 st, &geo, &old_raid_disks, direction)) {
11874 struct imsm_update_reshape *u = NULL;
11875 int len;
11876
11877 if (imsm_fix_size_mismatch(st, -1)) {
11878 dprintf("imsm: Cannot fix size mismatch\n");
11879 goto exit_imsm_reshape_super;
11880 }
11881
11882 len = imsm_create_metadata_update_for_reshape(
11883 st, &geo, old_raid_disks, &u);
11884
11885 if (len <= 0) {
11886 dprintf("imsm: Cannot prepare update\n");
11887 goto exit_imsm_reshape_super;
11888 }
11889
11890 ret_val = 0;
11891 /* update metadata locally */
11892 imsm_update_metadata_locally(st, u, len);
11893 /* and possibly remotely */
11894 if (st->update_tail)
11895 append_metadata_update(st, u, len);
11896 else
11897 free(u);
11898
11899 } else {
11900 pr_err("(imsm) Operation is not allowed on this container\n");
11901 }
11902 } else {
11903 /* On volume level we support following operations
11904 * - takeover: raid10 -> raid0; raid0 -> raid10
11905 * - chunk size migration
11906 * - migration: raid5 -> raid0; raid0 -> raid5
11907 */
11908 struct intel_super *super = st->sb;
11909 struct intel_dev *dev = super->devlist;
11910 int change;
11911 dprintf("imsm: info: Volume operation\n");
11912 /* find requested device */
11913 while (dev) {
11914 char *devnm =
11915 imsm_find_array_devnm_by_subdev(
11916 dev->index, st->container_devnm);
11917 if (devnm && strcmp(devnm, geo.devnm) == 0)
11918 break;
11919 dev = dev->next;
11920 }
11921 if (dev == NULL) {
11922 pr_err("Cannot find %s (%s) subarray\n",
11923 geo.dev_name, geo.devnm);
11924 goto exit_imsm_reshape_super;
11925 }
11926 super->current_vol = dev->index;
11927 change = imsm_analyze_change(st, &geo, direction);
11928 switch (change) {
11929 case CH_TAKEOVER:
11930 ret_val = imsm_takeover(st, &geo);
11931 break;
11932 case CH_MIGRATION: {
11933 struct imsm_update_reshape_migration *u = NULL;
11934 int len =
11935 imsm_create_metadata_update_for_migration(
11936 st, &geo, &u);
11937 if (len < 1) {
11938 dprintf("imsm: Cannot prepare update\n");
11939 break;
11940 }
11941 ret_val = 0;
11942 /* update metadata locally */
11943 imsm_update_metadata_locally(st, u, len);
11944 /* and possibly remotely */
11945 if (st->update_tail)
11946 append_metadata_update(st, u, len);
11947 else
11948 free(u);
11949 }
11950 break;
11951 case CH_ARRAY_SIZE: {
11952 struct imsm_update_size_change *u = NULL;
11953 int len =
11954 imsm_create_metadata_update_for_size_change(
11955 st, &geo, &u);
11956 if (len < 1) {
11957 dprintf("imsm: Cannot prepare update\n");
11958 break;
11959 }
11960 ret_val = 0;
11961 /* update metadata locally */
11962 imsm_update_metadata_locally(st, u, len);
11963 /* and possibly remotely */
11964 if (st->update_tail)
11965 append_metadata_update(st, u, len);
11966 else
11967 free(u);
11968 }
11969 break;
11970 default:
11971 ret_val = 1;
11972 }
11973 }
11974
11975 exit_imsm_reshape_super:
11976 dprintf("imsm: reshape_super Exit code = %i\n", ret_val);
11977 return ret_val;
11978 }
11979
11980 #define COMPLETED_OK 0
11981 #define COMPLETED_NONE 1
11982 #define COMPLETED_DELAYED 2
11983
11984 static int read_completed(int fd, unsigned long long *val)
11985 {
11986 int ret;
11987 char buf[50];
11988
11989 ret = sysfs_fd_get_str(fd, buf, 50);
11990 if (ret < 0)
11991 return ret;
11992
11993 ret = COMPLETED_OK;
11994 if (strncmp(buf, "none", 4) == 0) {
11995 ret = COMPLETED_NONE;
11996 } else if (strncmp(buf, "delayed", 7) == 0) {
11997 ret = COMPLETED_DELAYED;
11998 } else {
11999 char *ep;
12000 *val = strtoull(buf, &ep, 0);
12001 if (ep == buf || (*ep != 0 && *ep != '\n' && *ep != ' '))
12002 ret = -1;
12003 }
12004 return ret;
12005 }
12006
12007 /*******************************************************************************
12008 * Function: wait_for_reshape_imsm
12009 * Description: Function writes new sync_max value and waits until
12010 * reshape process reach new position
12011 * Parameters:
12012 * sra : general array info
12013 * ndata : number of disks in new array's layout
12014 * Returns:
12015 * 0 : success,
12016 * 1 : there is no reshape in progress,
12017 * -1 : fail
12018 ******************************************************************************/
12019 int wait_for_reshape_imsm(struct mdinfo *sra, int ndata)
12020 {
12021 int fd = sysfs_get_fd(sra, NULL, "sync_completed");
12022 int retry = 3;
12023 unsigned long long completed;
12024 /* to_complete : new sync_max position */
12025 unsigned long long to_complete = sra->reshape_progress;
12026 unsigned long long position_to_set = to_complete / ndata;
12027
12028 if (fd < 0) {
12029 dprintf("cannot open reshape_position\n");
12030 return 1;
12031 }
12032
12033 do {
12034 if (sysfs_fd_get_ll(fd, &completed) < 0) {
12035 if (!retry) {
12036 dprintf("cannot read reshape_position (no reshape in progres)\n");
12037 close(fd);
12038 return 1;
12039 }
12040 usleep(30000);
12041 } else
12042 break;
12043 } while (retry--);
12044
12045 if (completed > position_to_set) {
12046 dprintf("wrong next position to set %llu (%llu)\n",
12047 to_complete, position_to_set);
12048 close(fd);
12049 return -1;
12050 }
12051 dprintf("Position set: %llu\n", position_to_set);
12052 if (sysfs_set_num(sra, NULL, "sync_max",
12053 position_to_set) != 0) {
12054 dprintf("cannot set reshape position to %llu\n",
12055 position_to_set);
12056 close(fd);
12057 return -1;
12058 }
12059
12060 do {
12061 int rc;
12062 char action[20];
12063 int timeout = 3000;
12064
12065 sysfs_wait(fd, &timeout);
12066 if (sysfs_get_str(sra, NULL, "sync_action",
12067 action, 20) > 0 &&
12068 strncmp(action, "reshape", 7) != 0) {
12069 if (strncmp(action, "idle", 4) == 0)
12070 break;
12071 close(fd);
12072 return -1;
12073 }
12074
12075 rc = read_completed(fd, &completed);
12076 if (rc < 0) {
12077 dprintf("cannot read reshape_position (in loop)\n");
12078 close(fd);
12079 return 1;
12080 } else if (rc == COMPLETED_NONE)
12081 break;
12082 } while (completed < position_to_set);
12083
12084 close(fd);
12085 return 0;
12086 }
12087
12088 /*******************************************************************************
12089 * Function: check_degradation_change
12090 * Description: Check that array hasn't become failed.
12091 * Parameters:
12092 * info : for sysfs access
12093 * sources : source disks descriptors
12094 * degraded: previous degradation level
12095 * Returns:
12096 * degradation level
12097 ******************************************************************************/
12098 int check_degradation_change(struct mdinfo *info,
12099 int *sources,
12100 int degraded)
12101 {
12102 unsigned long long new_degraded;
12103 int rv;
12104
12105 rv = sysfs_get_ll(info, NULL, "degraded", &new_degraded);
12106 if (rv == -1 || (new_degraded != (unsigned long long)degraded)) {
12107 /* check each device to ensure it is still working */
12108 struct mdinfo *sd;
12109 new_degraded = 0;
12110 for (sd = info->devs ; sd ; sd = sd->next) {
12111 if (sd->disk.state & (1<<MD_DISK_FAULTY))
12112 continue;
12113 if (sd->disk.state & (1<<MD_DISK_SYNC)) {
12114 char sbuf[100];
12115
12116 if (sysfs_get_str(info,
12117 sd, "state", sbuf, sizeof(sbuf)) < 0 ||
12118 strstr(sbuf, "faulty") ||
12119 strstr(sbuf, "in_sync") == NULL) {
12120 /* this device is dead */
12121 sd->disk.state = (1<<MD_DISK_FAULTY);
12122 if (sd->disk.raid_disk >= 0 &&
12123 sources[sd->disk.raid_disk] >= 0) {
12124 close(sources[
12125 sd->disk.raid_disk]);
12126 sources[sd->disk.raid_disk] =
12127 -1;
12128 }
12129 new_degraded++;
12130 }
12131 }
12132 }
12133 }
12134
12135 return new_degraded;
12136 }
12137
12138 /*******************************************************************************
12139 * Function: imsm_manage_reshape
12140 * Description: Function finds array under reshape and it manages reshape
12141 * process. It creates stripes backups (if required) and sets
12142 * checkpoints.
12143 * Parameters:
12144 * afd : Backup handle (nattive) - not used
12145 * sra : general array info
12146 * reshape : reshape parameters - not used
12147 * st : supertype structure
12148 * blocks : size of critical section [blocks]
12149 * fds : table of source device descriptor
12150 * offsets : start of array (offest per devices)
12151 * dests : not used
12152 * destfd : table of destination device descriptor
12153 * destoffsets : table of destination offsets (per device)
12154 * Returns:
12155 * 1 : success, reshape is done
12156 * 0 : fail
12157 ******************************************************************************/
12158 static int imsm_manage_reshape(
12159 int afd, struct mdinfo *sra, struct reshape *reshape,
12160 struct supertype *st, unsigned long backup_blocks,
12161 int *fds, unsigned long long *offsets,
12162 int dests, int *destfd, unsigned long long *destoffsets)
12163 {
12164 int ret_val = 0;
12165 struct intel_super *super = st->sb;
12166 struct intel_dev *dv;
12167 unsigned int sector_size = super->sector_size;
12168 struct imsm_dev *dev = NULL;
12169 struct imsm_map *map_src, *map_dest;
12170 int migr_vol_qan = 0;
12171 int ndata, odata; /* [bytes] */
12172 int chunk; /* [bytes] */
12173 struct migr_record *migr_rec;
12174 char *buf = NULL;
12175 unsigned int buf_size; /* [bytes] */
12176 unsigned long long max_position; /* array size [bytes] */
12177 unsigned long long next_step; /* [blocks]/[bytes] */
12178 unsigned long long old_data_stripe_length;
12179 unsigned long long start_src; /* [bytes] */
12180 unsigned long long start; /* [bytes] */
12181 unsigned long long start_buf_shift; /* [bytes] */
12182 int degraded = 0;
12183 int source_layout = 0;
12184 int subarray_index = -1;
12185
12186 if (!sra)
12187 return ret_val;
12188
12189 if (!fds || !offsets)
12190 goto abort;
12191
12192 /* Find volume during the reshape */
12193 for (dv = super->devlist; dv; dv = dv->next) {
12194 if (dv->dev->vol.migr_type == MIGR_GEN_MIGR &&
12195 dv->dev->vol.migr_state == 1) {
12196 dev = dv->dev;
12197 migr_vol_qan++;
12198 subarray_index = dv->index;
12199 }
12200 }
12201 /* Only one volume can migrate at the same time */
12202 if (migr_vol_qan != 1) {
12203 pr_err("%s", migr_vol_qan ?
12204 "Number of migrating volumes greater than 1\n" :
12205 "There is no volume during migrationg\n");
12206 goto abort;
12207 }
12208
12209 map_dest = get_imsm_map(dev, MAP_0);
12210 map_src = get_imsm_map(dev, MAP_1);
12211 if (map_src == NULL)
12212 goto abort;
12213
12214 ndata = imsm_num_data_members(map_dest);
12215 odata = imsm_num_data_members(map_src);
12216
12217 chunk = __le16_to_cpu(map_src->blocks_per_strip) * 512;
12218 old_data_stripe_length = odata * chunk;
12219
12220 migr_rec = super->migr_rec;
12221
12222 /* initialize migration record for start condition */
12223 if (sra->reshape_progress == 0)
12224 init_migr_record_imsm(st, dev, sra);
12225 else {
12226 if (__le32_to_cpu(migr_rec->rec_status) != UNIT_SRC_NORMAL) {
12227 dprintf("imsm: cannot restart migration when data are present in copy area.\n");
12228 goto abort;
12229 }
12230 /* Save checkpoint to update migration record for current
12231 * reshape position (in md). It can be farther than current
12232 * reshape position in metadata.
12233 */
12234 if (save_checkpoint_imsm(st, sra, UNIT_SRC_NORMAL) == 1) {
12235 /* ignore error == 2, this can mean end of reshape here
12236 */
12237 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL, initial save)\n");
12238 goto abort;
12239 }
12240 }
12241
12242 /* size for data */
12243 buf_size = __le32_to_cpu(migr_rec->blocks_per_unit) * 512;
12244 /* extend buffer size for parity disk */
12245 buf_size += __le32_to_cpu(migr_rec->dest_depth_per_unit) * 512;
12246 /* add space for stripe alignment */
12247 buf_size += old_data_stripe_length;
12248 if (posix_memalign((void **)&buf, MAX_SECTOR_SIZE, buf_size)) {
12249 dprintf("imsm: Cannot allocate checkpoint buffer\n");
12250 goto abort;
12251 }
12252
12253 max_position = sra->component_size * ndata;
12254 source_layout = imsm_level_to_layout(map_src->raid_level);
12255
12256 while (current_migr_unit(migr_rec) <
12257 get_num_migr_units(migr_rec)) {
12258 /* current reshape position [blocks] */
12259 unsigned long long current_position =
12260 __le32_to_cpu(migr_rec->blocks_per_unit)
12261 * current_migr_unit(migr_rec);
12262 unsigned long long border;
12263
12264 /* Check that array hasn't become failed.
12265 */
12266 degraded = check_degradation_change(sra, fds, degraded);
12267 if (degraded > 1) {
12268 dprintf("imsm: Abort reshape due to degradation level (%i)\n", degraded);
12269 goto abort;
12270 }
12271
12272 next_step = __le32_to_cpu(migr_rec->blocks_per_unit);
12273
12274 if ((current_position + next_step) > max_position)
12275 next_step = max_position - current_position;
12276
12277 start = current_position * 512;
12278
12279 /* align reading start to old geometry */
12280 start_buf_shift = start % old_data_stripe_length;
12281 start_src = start - start_buf_shift;
12282
12283 border = (start_src / odata) - (start / ndata);
12284 border /= 512;
12285 if (border <= __le32_to_cpu(migr_rec->dest_depth_per_unit)) {
12286 /* save critical stripes to buf
12287 * start - start address of current unit
12288 * to backup [bytes]
12289 * start_src - start address of current unit
12290 * to backup alligned to source array
12291 * [bytes]
12292 */
12293 unsigned long long next_step_filler;
12294 unsigned long long copy_length = next_step * 512;
12295
12296 /* allign copy area length to stripe in old geometry */
12297 next_step_filler = ((copy_length + start_buf_shift)
12298 % old_data_stripe_length);
12299 if (next_step_filler)
12300 next_step_filler = (old_data_stripe_length
12301 - next_step_filler);
12302 dprintf("save_stripes() parameters: start = %llu,\tstart_src = %llu,\tnext_step*512 = %llu,\tstart_in_buf_shift = %llu,\tnext_step_filler = %llu\n",
12303 start, start_src, copy_length,
12304 start_buf_shift, next_step_filler);
12305
12306 if (save_stripes(fds, offsets, map_src->num_members,
12307 chunk, map_src->raid_level,
12308 source_layout, 0, NULL, start_src,
12309 copy_length +
12310 next_step_filler + start_buf_shift,
12311 buf)) {
12312 dprintf("imsm: Cannot save stripes to buffer\n");
12313 goto abort;
12314 }
12315 /* Convert data to destination format and store it
12316 * in backup general migration area
12317 */
12318 if (save_backup_imsm(st, dev, sra,
12319 buf + start_buf_shift, copy_length)) {
12320 dprintf("imsm: Cannot save stripes to target devices\n");
12321 goto abort;
12322 }
12323 if (save_checkpoint_imsm(st, sra,
12324 UNIT_SRC_IN_CP_AREA)) {
12325 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_IN_CP_AREA)\n");
12326 goto abort;
12327 }
12328 } else {
12329 /* set next step to use whole border area */
12330 border /= next_step;
12331 if (border > 1)
12332 next_step *= border;
12333 }
12334 /* When data backed up, checkpoint stored,
12335 * kick the kernel to reshape unit of data
12336 */
12337 next_step = next_step + sra->reshape_progress;
12338 /* limit next step to array max position */
12339 if (next_step > max_position)
12340 next_step = max_position;
12341 sysfs_set_num(sra, NULL, "suspend_lo", sra->reshape_progress);
12342 sysfs_set_num(sra, NULL, "suspend_hi", next_step);
12343 sra->reshape_progress = next_step;
12344
12345 /* wait until reshape finish */
12346 if (wait_for_reshape_imsm(sra, ndata)) {
12347 dprintf("wait_for_reshape_imsm returned error!\n");
12348 goto abort;
12349 }
12350 if (sigterm)
12351 goto abort;
12352
12353 if (save_checkpoint_imsm(st, sra, UNIT_SRC_NORMAL) == 1) {
12354 /* ignore error == 2, this can mean end of reshape here
12355 */
12356 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL)\n");
12357 goto abort;
12358 }
12359
12360 }
12361
12362 /* clear migr_rec on disks after successful migration */
12363 struct dl *d;
12364
12365 memset(super->migr_rec_buf, 0, MIGR_REC_BUF_SECTORS*MAX_SECTOR_SIZE);
12366 for (d = super->disks; d; d = d->next) {
12367 if (d->index < 0 || is_failed(&d->disk))
12368 continue;
12369 unsigned long long dsize;
12370
12371 get_dev_size(d->fd, NULL, &dsize);
12372 if (lseek64(d->fd, dsize - MIGR_REC_SECTOR_POSITION*sector_size,
12373 SEEK_SET) >= 0) {
12374 if ((unsigned int)write(d->fd, super->migr_rec_buf,
12375 MIGR_REC_BUF_SECTORS*sector_size) !=
12376 MIGR_REC_BUF_SECTORS*sector_size)
12377 perror("Write migr_rec failed");
12378 }
12379 }
12380
12381 /* return '1' if done */
12382 ret_val = 1;
12383
12384 /* After the reshape eliminate size mismatch in metadata.
12385 * Don't update md/component_size here, volume hasn't
12386 * to take whole space. It is allowed by kernel.
12387 * md/component_size will be set propoperly after next assembly.
12388 */
12389 imsm_fix_size_mismatch(st, subarray_index);
12390
12391 abort:
12392 free(buf);
12393 /* See Grow.c: abort_reshape() for further explanation */
12394 sysfs_set_num(sra, NULL, "suspend_lo", 0x7FFFFFFFFFFFFFFFULL);
12395 sysfs_set_num(sra, NULL, "suspend_hi", 0);
12396 sysfs_set_num(sra, NULL, "suspend_lo", 0);
12397
12398 return ret_val;
12399 }
12400
12401 /*******************************************************************************
12402 * Function: calculate_bitmap_min_chunksize
12403 * Description: Calculates the minimal valid bitmap chunk size
12404 * Parameters:
12405 * max_bits : indicate how many bits can be used for the bitmap
12406 * data_area_size : the size of the data area covered by the bitmap
12407 *
12408 * Returns:
12409 * The bitmap chunk size
12410 ******************************************************************************/
12411 static unsigned long long
12412 calculate_bitmap_min_chunksize(unsigned long long max_bits,
12413 unsigned long long data_area_size)
12414 {
12415 unsigned long long min_chunk =
12416 4096; /* sub-page chunks don't work yet.. */
12417 unsigned long long bits = data_area_size / min_chunk + 1;
12418
12419 while (bits > max_bits) {
12420 min_chunk *= 2;
12421 bits = (bits + 1) / 2;
12422 }
12423 return min_chunk;
12424 }
12425
12426 /*******************************************************************************
12427 * Function: calculate_bitmap_chunksize
12428 * Description: Calculates the bitmap chunk size for the given device
12429 * Parameters:
12430 * st : supertype information
12431 * dev : device for the bitmap
12432 *
12433 * Returns:
12434 * The bitmap chunk size
12435 ******************************************************************************/
12436 static unsigned long long calculate_bitmap_chunksize(struct supertype *st,
12437 struct imsm_dev *dev)
12438 {
12439 struct intel_super *super = st->sb;
12440 unsigned long long min_chunksize;
12441 unsigned long long result = IMSM_DEFAULT_BITMAP_CHUNKSIZE;
12442 size_t dev_size = imsm_dev_size(dev);
12443
12444 min_chunksize = calculate_bitmap_min_chunksize(
12445 IMSM_BITMAP_AREA_SIZE * super->sector_size, dev_size);
12446
12447 if (result < min_chunksize)
12448 result = min_chunksize;
12449
12450 return result;
12451 }
12452
12453 /*******************************************************************************
12454 * Function: init_bitmap_header
12455 * Description: Initialize the bitmap header structure
12456 * Parameters:
12457 * st : supertype information
12458 * bms : bitmap header struct to initialize
12459 * dev : device for the bitmap
12460 *
12461 * Returns:
12462 * 0 : success
12463 * -1 : fail
12464 ******************************************************************************/
12465 static int init_bitmap_header(struct supertype *st, struct bitmap_super_s *bms,
12466 struct imsm_dev *dev)
12467 {
12468 int vol_uuid[4];
12469
12470 if (!bms || !dev)
12471 return -1;
12472
12473 bms->magic = __cpu_to_le32(BITMAP_MAGIC);
12474 bms->version = __cpu_to_le32(BITMAP_MAJOR_HI);
12475 bms->daemon_sleep = __cpu_to_le32(IMSM_DEFAULT_BITMAP_DAEMON_SLEEP);
12476 bms->sync_size = __cpu_to_le64(IMSM_BITMAP_AREA_SIZE);
12477 bms->write_behind = __cpu_to_le32(0);
12478
12479 uuid_from_super_imsm(st, vol_uuid);
12480 memcpy(bms->uuid, vol_uuid, 16);
12481
12482 bms->chunksize = calculate_bitmap_chunksize(st, dev);
12483
12484 return 0;
12485 }
12486
12487 /*******************************************************************************
12488 * Function: validate_internal_bitmap_for_drive
12489 * Description: Verify if the bitmap header for a given drive.
12490 * Parameters:
12491 * st : supertype information
12492 * offset : The offset from the beginning of the drive where to look for
12493 * the bitmap header.
12494 * d : the drive info
12495 *
12496 * Returns:
12497 * 0 : success
12498 * -1 : fail
12499 ******************************************************************************/
12500 static int validate_internal_bitmap_for_drive(struct supertype *st,
12501 unsigned long long offset,
12502 struct dl *d)
12503 {
12504 struct intel_super *super = st->sb;
12505 int ret = -1;
12506 int vol_uuid[4];
12507 bitmap_super_t *bms;
12508 int fd;
12509
12510 if (!d)
12511 return -1;
12512
12513 void *read_buf;
12514
12515 if (posix_memalign(&read_buf, MAX_SECTOR_SIZE, IMSM_BITMAP_HEADER_SIZE))
12516 return -1;
12517
12518 fd = d->fd;
12519 if (fd < 0) {
12520 fd = open(d->devname, O_RDONLY, 0);
12521 if (fd < 0) {
12522 dprintf("cannot open the device %s\n", d->devname);
12523 goto abort;
12524 }
12525 }
12526
12527 if (lseek64(fd, offset * super->sector_size, SEEK_SET) < 0)
12528 goto abort;
12529 if (read(fd, read_buf, IMSM_BITMAP_HEADER_SIZE) !=
12530 IMSM_BITMAP_HEADER_SIZE)
12531 goto abort;
12532
12533 uuid_from_super_imsm(st, vol_uuid);
12534
12535 bms = read_buf;
12536 if ((bms->magic != __cpu_to_le32(BITMAP_MAGIC)) ||
12537 (bms->version != __cpu_to_le32(BITMAP_MAJOR_HI)) ||
12538 (!same_uuid((int *)bms->uuid, vol_uuid, st->ss->swapuuid))) {
12539 dprintf("wrong bitmap header detected\n");
12540 goto abort;
12541 }
12542
12543 ret = 0;
12544 abort:
12545 if ((d->fd < 0) && (fd >= 0))
12546 close(fd);
12547 if (read_buf)
12548 free(read_buf);
12549
12550 return ret;
12551 }
12552
12553 /*******************************************************************************
12554 * Function: validate_internal_bitmap_imsm
12555 * Description: Verify if the bitmap header is in place and with proper data.
12556 * Parameters:
12557 * st : supertype information
12558 *
12559 * Returns:
12560 * 0 : success or device w/o RWH_BITMAP
12561 * -1 : fail
12562 ******************************************************************************/
12563 static int validate_internal_bitmap_imsm(struct supertype *st)
12564 {
12565 struct intel_super *super = st->sb;
12566 struct imsm_dev *dev = get_imsm_dev(super, super->current_vol);
12567 unsigned long long offset;
12568 struct dl *d;
12569
12570 if (!dev)
12571 return -1;
12572
12573 if (dev->rwh_policy != RWH_BITMAP)
12574 return 0;
12575
12576 offset = get_bitmap_header_sector(super, super->current_vol);
12577 for (d = super->disks; d; d = d->next) {
12578 if (d->index < 0 || is_failed(&d->disk))
12579 continue;
12580
12581 if (validate_internal_bitmap_for_drive(st, offset, d)) {
12582 pr_err("imsm: bitmap validation failed\n");
12583 return -1;
12584 }
12585 }
12586 return 0;
12587 }
12588
12589 /*******************************************************************************
12590 * Function: add_internal_bitmap_imsm
12591 * Description: Mark the volume to use the bitmap and updates the chunk size value.
12592 * Parameters:
12593 * st : supertype information
12594 * chunkp : bitmap chunk size
12595 * delay : not used for imsm
12596 * write_behind : not used for imsm
12597 * size : not used for imsm
12598 * may_change : not used for imsm
12599 * amajor : not used for imsm
12600 *
12601 * Returns:
12602 * 0 : success
12603 * -1 : fail
12604 ******************************************************************************/
12605 static int add_internal_bitmap_imsm(struct supertype *st, int *chunkp,
12606 int delay, int write_behind,
12607 unsigned long long size, int may_change,
12608 int amajor)
12609 {
12610 struct intel_super *super = st->sb;
12611 int vol_idx = super->current_vol;
12612 struct imsm_dev *dev;
12613
12614 if (!super->devlist || vol_idx == -1 || !chunkp)
12615 return -1;
12616
12617 dev = get_imsm_dev(super, vol_idx);
12618
12619 if (!dev) {
12620 dprintf("cannot find the device for volume index %d\n",
12621 vol_idx);
12622 return -1;
12623 }
12624 dev->rwh_policy = RWH_BITMAP;
12625
12626 *chunkp = calculate_bitmap_chunksize(st, dev);
12627
12628 return 0;
12629 }
12630
12631 /*******************************************************************************
12632 * Function: locate_bitmap_imsm
12633 * Description: Seek 'fd' to start of write-intent-bitmap.
12634 * Parameters:
12635 * st : supertype information
12636 * fd : file descriptor for the device
12637 * node_num : not used for imsm
12638 *
12639 * Returns:
12640 * 0 : success
12641 * -1 : fail
12642 ******************************************************************************/
12643 static int locate_bitmap_imsm(struct supertype *st, int fd, int node_num)
12644 {
12645 struct intel_super *super = st->sb;
12646 unsigned long long offset;
12647 int vol_idx = super->current_vol;
12648
12649 if (!super->devlist || vol_idx == -1)
12650 return -1;
12651
12652 offset = get_bitmap_header_sector(super, super->current_vol);
12653 dprintf("bitmap header offset is %llu\n", offset);
12654
12655 lseek64(fd, offset << 9, 0);
12656
12657 return 0;
12658 }
12659
12660 /*******************************************************************************
12661 * Function: write_init_bitmap_imsm
12662 * Description: Write a bitmap header and prepares the area for the bitmap.
12663 * Parameters:
12664 * st : supertype information
12665 * fd : file descriptor for the device
12666 * update : not used for imsm
12667 *
12668 * Returns:
12669 * 0 : success
12670 * -1 : fail
12671 ******************************************************************************/
12672 static int write_init_bitmap_imsm(struct supertype *st, int fd,
12673 enum bitmap_update update)
12674 {
12675 struct intel_super *super = st->sb;
12676 int vol_idx = super->current_vol;
12677 int ret = 0;
12678 unsigned long long offset;
12679 bitmap_super_t bms = { 0 };
12680 size_t written = 0;
12681 size_t to_write;
12682 ssize_t rv_num;
12683 void *buf;
12684
12685 if (!super->devlist || !super->sector_size || vol_idx == -1)
12686 return -1;
12687
12688 struct imsm_dev *dev = get_imsm_dev(super, vol_idx);
12689
12690 /* first clear the space for bitmap header */
12691 unsigned long long bitmap_area_start =
12692 get_bitmap_header_sector(super, vol_idx);
12693
12694 dprintf("zeroing area start (%llu) and size (%u)\n", bitmap_area_start,
12695 IMSM_BITMAP_AND_HEADER_SIZE / super->sector_size);
12696 if (zero_disk_range(fd, bitmap_area_start,
12697 IMSM_BITMAP_HEADER_SIZE / super->sector_size)) {
12698 pr_err("imsm: cannot zeroing the space for the bitmap\n");
12699 return -1;
12700 }
12701
12702 /* The bitmap area should be filled with "1"s to perform initial
12703 * synchronization.
12704 */
12705 if (posix_memalign(&buf, MAX_SECTOR_SIZE, MAX_SECTOR_SIZE))
12706 return -1;
12707 memset(buf, 0xFF, MAX_SECTOR_SIZE);
12708 offset = get_bitmap_sector(super, vol_idx);
12709 lseek64(fd, offset << 9, 0);
12710 while (written < IMSM_BITMAP_AREA_SIZE) {
12711 to_write = IMSM_BITMAP_AREA_SIZE - written;
12712 if (to_write > MAX_SECTOR_SIZE)
12713 to_write = MAX_SECTOR_SIZE;
12714 rv_num = write(fd, buf, MAX_SECTOR_SIZE);
12715 if (rv_num != MAX_SECTOR_SIZE) {
12716 ret = -1;
12717 dprintf("cannot initialize bitmap area\n");
12718 goto abort;
12719 }
12720 written += rv_num;
12721 }
12722
12723 /* write a bitmap header */
12724 init_bitmap_header(st, &bms, dev);
12725 memset(buf, 0, MAX_SECTOR_SIZE);
12726 memcpy(buf, &bms, sizeof(bitmap_super_t));
12727 if (locate_bitmap_imsm(st, fd, 0)) {
12728 ret = -1;
12729 dprintf("cannot locate the bitmap\n");
12730 goto abort;
12731 }
12732 if (write(fd, buf, MAX_SECTOR_SIZE) != MAX_SECTOR_SIZE) {
12733 ret = -1;
12734 dprintf("cannot write the bitmap header\n");
12735 goto abort;
12736 }
12737 fsync(fd);
12738
12739 abort:
12740 free(buf);
12741
12742 return ret;
12743 }
12744
12745 /*******************************************************************************
12746 * Function: is_vol_to_setup_bitmap
12747 * Description: Checks if a bitmap should be activated on the dev.
12748 * Parameters:
12749 * info : info about the volume to setup the bitmap
12750 * dev : the device to check against bitmap creation
12751 *
12752 * Returns:
12753 * 0 : bitmap should be set up on the device
12754 * -1 : otherwise
12755 ******************************************************************************/
12756 static int is_vol_to_setup_bitmap(struct mdinfo *info, struct imsm_dev *dev)
12757 {
12758 if (!dev || !info)
12759 return -1;
12760
12761 if ((strcmp((char *)dev->volume, info->name) == 0) &&
12762 (dev->rwh_policy == RWH_BITMAP))
12763 return -1;
12764
12765 return 0;
12766 }
12767
12768 /*******************************************************************************
12769 * Function: set_bitmap_sysfs
12770 * Description: Set the sysfs atributes of a given volume to activate the bitmap.
12771 * Parameters:
12772 * info : info about the volume where the bitmap should be setup
12773 * chunksize : bitmap chunk size
12774 * location : location of the bitmap
12775 *
12776 * Returns:
12777 * 0 : success
12778 * -1 : fail
12779 ******************************************************************************/
12780 static int set_bitmap_sysfs(struct mdinfo *info, unsigned long long chunksize,
12781 char *location)
12782 {
12783 /* The bitmap/metadata is set to external to allow changing of value for
12784 * bitmap/location. When external is used, the kernel will treat an offset
12785 * related to the device's first lba (in opposition to the "internal" case
12786 * when this value is related to the beginning of the superblock).
12787 */
12788 if (sysfs_set_str(info, NULL, "bitmap/metadata", "external")) {
12789 dprintf("failed to set bitmap/metadata\n");
12790 return -1;
12791 }
12792
12793 /* It can only be changed when no bitmap is active.
12794 * Should be bigger than 512 and must be power of 2.
12795 * It is expecting the value in bytes.
12796 */
12797 if (sysfs_set_num(info, NULL, "bitmap/chunksize",
12798 __cpu_to_le32(chunksize))) {
12799 dprintf("failed to set bitmap/chunksize\n");
12800 return -1;
12801 }
12802
12803 /* It is expecting the value in sectors. */
12804 if (sysfs_set_num(info, NULL, "bitmap/space",
12805 __cpu_to_le64(IMSM_BITMAP_AREA_SIZE))) {
12806 dprintf("failed to set bitmap/space\n");
12807 return -1;
12808 }
12809
12810 /* Determines the delay between the bitmap updates.
12811 * It is expecting the value in seconds.
12812 */
12813 if (sysfs_set_num(info, NULL, "bitmap/time_base",
12814 __cpu_to_le64(IMSM_DEFAULT_BITMAP_DAEMON_SLEEP))) {
12815 dprintf("failed to set bitmap/time_base\n");
12816 return -1;
12817 }
12818
12819 /* It is expecting the value in sectors with a sign at the beginning. */
12820 if (sysfs_set_str(info, NULL, "bitmap/location", location)) {
12821 dprintf("failed to set bitmap/location\n");
12822 return -1;
12823 }
12824
12825 return 0;
12826 }
12827
12828 /*******************************************************************************
12829 * Function: set_bitmap_imsm
12830 * Description: Setup the bitmap for the given volume
12831 * Parameters:
12832 * st : supertype information
12833 * info : info about the volume where the bitmap should be setup
12834 *
12835 * Returns:
12836 * 0 : success
12837 * -1 : fail
12838 ******************************************************************************/
12839 static int set_bitmap_imsm(struct supertype *st, struct mdinfo *info)
12840 {
12841 struct intel_super *super = st->sb;
12842 int prev_current_vol = super->current_vol;
12843 struct imsm_dev *dev;
12844 int ret = -1;
12845 char location[16] = "";
12846 unsigned long long chunksize;
12847 struct intel_dev *dev_it;
12848
12849 for (dev_it = super->devlist; dev_it; dev_it = dev_it->next) {
12850 super->current_vol = dev_it->index;
12851 dev = get_imsm_dev(super, super->current_vol);
12852
12853 if (is_vol_to_setup_bitmap(info, dev)) {
12854 if (validate_internal_bitmap_imsm(st)) {
12855 dprintf("bitmap header validation failed\n");
12856 goto abort;
12857 }
12858
12859 chunksize = calculate_bitmap_chunksize(st, dev);
12860 dprintf("chunk size is %llu\n", chunksize);
12861
12862 snprintf(location, sizeof(location), "+%llu",
12863 get_bitmap_sector(super, super->current_vol));
12864 dprintf("bitmap offset is %s\n", location);
12865
12866 if (set_bitmap_sysfs(info, chunksize, location)) {
12867 dprintf("cannot setup the bitmap\n");
12868 goto abort;
12869 }
12870 }
12871 }
12872 ret = 0;
12873 abort:
12874 super->current_vol = prev_current_vol;
12875 return ret;
12876 }
12877
12878 struct superswitch super_imsm = {
12879 .examine_super = examine_super_imsm,
12880 .brief_examine_super = brief_examine_super_imsm,
12881 .brief_examine_subarrays = brief_examine_subarrays_imsm,
12882 .export_examine_super = export_examine_super_imsm,
12883 .detail_super = detail_super_imsm,
12884 .brief_detail_super = brief_detail_super_imsm,
12885 .write_init_super = write_init_super_imsm,
12886 .validate_geometry = validate_geometry_imsm,
12887 .add_to_super = add_to_super_imsm,
12888 .remove_from_super = remove_from_super_imsm,
12889 .detail_platform = detail_platform_imsm,
12890 .export_detail_platform = export_detail_platform_imsm,
12891 .kill_subarray = kill_subarray_imsm,
12892 .update_subarray = update_subarray_imsm,
12893 .load_container = load_container_imsm,
12894 .default_geometry = default_geometry_imsm,
12895 .get_disk_controller_domain = imsm_get_disk_controller_domain,
12896 .reshape_super = imsm_reshape_super,
12897 .manage_reshape = imsm_manage_reshape,
12898 .recover_backup = recover_backup_imsm,
12899 .examine_badblocks = examine_badblocks_imsm,
12900 .match_home = match_home_imsm,
12901 .uuid_from_super= uuid_from_super_imsm,
12902 .getinfo_super = getinfo_super_imsm,
12903 .getinfo_super_disks = getinfo_super_disks_imsm,
12904 .update_super = update_super_imsm,
12905
12906 .avail_size = avail_size_imsm,
12907 .get_spare_criteria = get_spare_criteria_imsm,
12908
12909 .compare_super = compare_super_imsm,
12910
12911 .load_super = load_super_imsm,
12912 .init_super = init_super_imsm,
12913 .store_super = store_super_imsm,
12914 .free_super = free_super_imsm,
12915 .match_metadata_desc = match_metadata_desc_imsm,
12916 .container_content = container_content_imsm,
12917 .validate_container = validate_container_imsm,
12918
12919 .add_internal_bitmap = add_internal_bitmap_imsm,
12920 .locate_bitmap = locate_bitmap_imsm,
12921 .write_bitmap = write_init_bitmap_imsm,
12922 .set_bitmap = set_bitmap_imsm,
12923
12924 .write_init_ppl = write_init_ppl_imsm,
12925 .validate_ppl = validate_ppl_imsm,
12926
12927 .external = 1,
12928 .name = "imsm",
12929
12930 /* for mdmon */
12931 .open_new = imsm_open_new,
12932 .set_array_state= imsm_set_array_state,
12933 .set_disk = imsm_set_disk,
12934 .sync_metadata = imsm_sync_metadata,
12935 .activate_spare = imsm_activate_spare,
12936 .process_update = imsm_process_update,
12937 .prepare_update = imsm_prepare_update,
12938 .record_bad_block = imsm_record_badblock,
12939 .clear_bad_block = imsm_clear_badblock,
12940 .get_bad_blocks = imsm_get_badblocks,
12941 };