]> git.ipfire.org Git - thirdparty/mdadm.git/blob - super-intel.c
6617dd6db79b979ef6ea51bbbcdbab95fccd064e
[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 char *device_path;
2390 struct dirent *ent;
2391 DIR *dir;
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 fd = open_dev(ent->d_name);
2401 if (fd < 0)
2402 continue;
2403
2404 if (!imsm_is_nvme_supported(fd, 0)) {
2405 if (fd >= 0)
2406 close(fd);
2407 continue;
2408 }
2409
2410 device_path = diskfd_to_devpath(fd);
2411 if (!device_path) {
2412 close(fd);
2413 continue;
2414 }
2415
2416 if (path_attached_to_hba(device_path, hba->path)) {
2417 fd2devname(fd, buf);
2418 if (hba->type == SYS_DEV_VMD)
2419 printf(" NVMe under VMD : %s", buf);
2420 else if (hba->type == SYS_DEV_NVME)
2421 printf(" NVMe Device : %s", buf);
2422 if (!imsm_read_serial(fd, NULL, (__u8 *)buf,
2423 sizeof(buf)))
2424 printf(" (%s)\n", buf);
2425 else
2426 printf("()\n");
2427 }
2428 free(device_path);
2429 close(fd);
2430 }
2431 }
2432
2433 closedir(dir);
2434 return 0;
2435 }
2436
2437 static void print_found_intel_controllers(struct sys_dev *elem)
2438 {
2439 for (; elem; elem = elem->next) {
2440 pr_err("found Intel(R) ");
2441 if (elem->type == SYS_DEV_SATA)
2442 fprintf(stderr, "SATA ");
2443 else if (elem->type == SYS_DEV_SAS)
2444 fprintf(stderr, "SAS ");
2445 else if (elem->type == SYS_DEV_NVME)
2446 fprintf(stderr, "NVMe ");
2447
2448 if (elem->type == SYS_DEV_VMD)
2449 fprintf(stderr, "VMD domain");
2450 else
2451 fprintf(stderr, "RAID controller");
2452
2453 if (elem->pci_id)
2454 fprintf(stderr, " at %s", elem->pci_id);
2455 fprintf(stderr, ".\n");
2456 }
2457 fflush(stderr);
2458 }
2459
2460 static int ahci_get_port_count(const char *hba_path, int *port_count)
2461 {
2462 struct dirent *ent;
2463 DIR *dir;
2464 int host_base = -1;
2465
2466 *port_count = 0;
2467 if ((dir = opendir(hba_path)) == NULL)
2468 return -1;
2469
2470 for (ent = readdir(dir); ent; ent = readdir(dir)) {
2471 int host;
2472
2473 if ((sscanf(ent->d_name, "ata%d", &host) != 1) &&
2474 ((sscanf(ent->d_name, "host%d", &host) != 1)))
2475 continue;
2476 if (*port_count == 0)
2477 host_base = host;
2478 else if (host < host_base)
2479 host_base = host;
2480
2481 if (host + 1 > *port_count + host_base)
2482 *port_count = host + 1 - host_base;
2483 }
2484 closedir(dir);
2485 return host_base;
2486 }
2487
2488 static void print_imsm_capability(const struct imsm_orom *orom)
2489 {
2490 printf(" Platform : Intel(R) ");
2491 if (orom->capabilities == 0 && orom->driver_features == 0)
2492 printf("Matrix Storage Manager\n");
2493 else if (imsm_orom_is_enterprise(orom) && orom->major_ver >= 6)
2494 printf("Virtual RAID on CPU\n");
2495 else
2496 printf("Rapid Storage Technology%s\n",
2497 imsm_orom_is_enterprise(orom) ? " enterprise" : "");
2498 if (orom->major_ver || orom->minor_ver || orom->hotfix_ver || orom->build)
2499 printf(" Version : %d.%d.%d.%d\n", orom->major_ver,
2500 orom->minor_ver, orom->hotfix_ver, orom->build);
2501 printf(" RAID Levels :%s%s%s%s%s\n",
2502 imsm_orom_has_raid0(orom) ? " raid0" : "",
2503 imsm_orom_has_raid1(orom) ? " raid1" : "",
2504 imsm_orom_has_raid1e(orom) ? " raid1e" : "",
2505 imsm_orom_has_raid10(orom) ? " raid10" : "",
2506 imsm_orom_has_raid5(orom) ? " raid5" : "");
2507 printf(" Chunk Sizes :%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
2508 imsm_orom_has_chunk(orom, 2) ? " 2k" : "",
2509 imsm_orom_has_chunk(orom, 4) ? " 4k" : "",
2510 imsm_orom_has_chunk(orom, 8) ? " 8k" : "",
2511 imsm_orom_has_chunk(orom, 16) ? " 16k" : "",
2512 imsm_orom_has_chunk(orom, 32) ? " 32k" : "",
2513 imsm_orom_has_chunk(orom, 64) ? " 64k" : "",
2514 imsm_orom_has_chunk(orom, 128) ? " 128k" : "",
2515 imsm_orom_has_chunk(orom, 256) ? " 256k" : "",
2516 imsm_orom_has_chunk(orom, 512) ? " 512k" : "",
2517 imsm_orom_has_chunk(orom, 1024*1) ? " 1M" : "",
2518 imsm_orom_has_chunk(orom, 1024*2) ? " 2M" : "",
2519 imsm_orom_has_chunk(orom, 1024*4) ? " 4M" : "",
2520 imsm_orom_has_chunk(orom, 1024*8) ? " 8M" : "",
2521 imsm_orom_has_chunk(orom, 1024*16) ? " 16M" : "",
2522 imsm_orom_has_chunk(orom, 1024*32) ? " 32M" : "",
2523 imsm_orom_has_chunk(orom, 1024*64) ? " 64M" : "");
2524 printf(" 2TB volumes :%s supported\n",
2525 (orom->attr & IMSM_OROM_ATTR_2TB)?"":" not");
2526 printf(" 2TB disks :%s supported\n",
2527 (orom->attr & IMSM_OROM_ATTR_2TB_DISK)?"":" not");
2528 printf(" Max Disks : %d\n", orom->tds);
2529 printf(" Max Volumes : %d per array, %d per %s\n",
2530 orom->vpa, orom->vphba,
2531 imsm_orom_is_nvme(orom) ? "platform" : "controller");
2532 return;
2533 }
2534
2535 static void print_imsm_capability_export(const struct imsm_orom *orom)
2536 {
2537 printf("MD_FIRMWARE_TYPE=imsm\n");
2538 if (orom->major_ver || orom->minor_ver || orom->hotfix_ver || orom->build)
2539 printf("IMSM_VERSION=%d.%d.%d.%d\n", orom->major_ver, orom->minor_ver,
2540 orom->hotfix_ver, orom->build);
2541 printf("IMSM_SUPPORTED_RAID_LEVELS=%s%s%s%s%s\n",
2542 imsm_orom_has_raid0(orom) ? "raid0 " : "",
2543 imsm_orom_has_raid1(orom) ? "raid1 " : "",
2544 imsm_orom_has_raid1e(orom) ? "raid1e " : "",
2545 imsm_orom_has_raid5(orom) ? "raid10 " : "",
2546 imsm_orom_has_raid10(orom) ? "raid5 " : "");
2547 printf("IMSM_SUPPORTED_CHUNK_SIZES=%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
2548 imsm_orom_has_chunk(orom, 2) ? "2k " : "",
2549 imsm_orom_has_chunk(orom, 4) ? "4k " : "",
2550 imsm_orom_has_chunk(orom, 8) ? "8k " : "",
2551 imsm_orom_has_chunk(orom, 16) ? "16k " : "",
2552 imsm_orom_has_chunk(orom, 32) ? "32k " : "",
2553 imsm_orom_has_chunk(orom, 64) ? "64k " : "",
2554 imsm_orom_has_chunk(orom, 128) ? "128k " : "",
2555 imsm_orom_has_chunk(orom, 256) ? "256k " : "",
2556 imsm_orom_has_chunk(orom, 512) ? "512k " : "",
2557 imsm_orom_has_chunk(orom, 1024*1) ? "1M " : "",
2558 imsm_orom_has_chunk(orom, 1024*2) ? "2M " : "",
2559 imsm_orom_has_chunk(orom, 1024*4) ? "4M " : "",
2560 imsm_orom_has_chunk(orom, 1024*8) ? "8M " : "",
2561 imsm_orom_has_chunk(orom, 1024*16) ? "16M " : "",
2562 imsm_orom_has_chunk(orom, 1024*32) ? "32M " : "",
2563 imsm_orom_has_chunk(orom, 1024*64) ? "64M " : "");
2564 printf("IMSM_2TB_VOLUMES=%s\n",(orom->attr & IMSM_OROM_ATTR_2TB) ? "yes" : "no");
2565 printf("IMSM_2TB_DISKS=%s\n",(orom->attr & IMSM_OROM_ATTR_2TB_DISK) ? "yes" : "no");
2566 printf("IMSM_MAX_DISKS=%d\n",orom->tds);
2567 printf("IMSM_MAX_VOLUMES_PER_ARRAY=%d\n",orom->vpa);
2568 printf("IMSM_MAX_VOLUMES_PER_CONTROLLER=%d\n",orom->vphba);
2569 }
2570
2571 static int detail_platform_imsm(int verbose, int enumerate_only, char *controller_path)
2572 {
2573 /* There are two components to imsm platform support, the ahci SATA
2574 * controller and the option-rom. To find the SATA controller we
2575 * simply look in /sys/bus/pci/drivers/ahci to see if an ahci
2576 * controller with the Intel vendor id is present. This approach
2577 * allows mdadm to leverage the kernel's ahci detection logic, with the
2578 * caveat that if ahci.ko is not loaded mdadm will not be able to
2579 * detect platform raid capabilities. The option-rom resides in a
2580 * platform "Adapter ROM". We scan for its signature to retrieve the
2581 * platform capabilities. If raid support is disabled in the BIOS the
2582 * option-rom capability structure will not be available.
2583 */
2584 struct sys_dev *list, *hba;
2585 int host_base = 0;
2586 int port_count = 0;
2587 int result=1;
2588
2589 if (enumerate_only) {
2590 if (check_env("IMSM_NO_PLATFORM"))
2591 return 0;
2592 list = find_intel_devices();
2593 if (!list)
2594 return 2;
2595 for (hba = list; hba; hba = hba->next) {
2596 if (find_imsm_capability(hba)) {
2597 result = 0;
2598 break;
2599 }
2600 else
2601 result = 2;
2602 }
2603 return result;
2604 }
2605
2606 list = find_intel_devices();
2607 if (!list) {
2608 if (verbose > 0)
2609 pr_err("no active Intel(R) RAID controller found.\n");
2610 return 2;
2611 } else if (verbose > 0)
2612 print_found_intel_controllers(list);
2613
2614 for (hba = list; hba; hba = hba->next) {
2615 if (controller_path && (compare_paths(hba->path, controller_path) != 0))
2616 continue;
2617 if (!find_imsm_capability(hba)) {
2618 char buf[PATH_MAX];
2619 pr_err("imsm capabilities not found for controller: %s (type %s)\n",
2620 hba->type == SYS_DEV_VMD ? vmd_domain_to_controller(hba, buf) : hba->path,
2621 get_sys_dev_type(hba->type));
2622 continue;
2623 }
2624 result = 0;
2625 }
2626
2627 if (controller_path && result == 1) {
2628 pr_err("no active Intel(R) RAID controller found under %s\n",
2629 controller_path);
2630 return result;
2631 }
2632
2633 const struct orom_entry *entry;
2634
2635 for (entry = orom_entries; entry; entry = entry->next) {
2636 if (entry->type == SYS_DEV_VMD) {
2637 print_imsm_capability(&entry->orom);
2638 printf(" 3rd party NVMe :%s supported\n",
2639 imsm_orom_has_tpv_support(&entry->orom)?"":" not");
2640 for (hba = list; hba; hba = hba->next) {
2641 if (hba->type == SYS_DEV_VMD) {
2642 char buf[PATH_MAX];
2643 printf(" I/O Controller : %s (%s)\n",
2644 vmd_domain_to_controller(hba, buf), get_sys_dev_type(hba->type));
2645 if (print_nvme_info(hba)) {
2646 if (verbose > 0)
2647 pr_err("failed to get devices attached to VMD domain.\n");
2648 result |= 2;
2649 }
2650 }
2651 }
2652 printf("\n");
2653 continue;
2654 }
2655
2656 print_imsm_capability(&entry->orom);
2657 if (entry->type == SYS_DEV_NVME) {
2658 for (hba = list; hba; hba = hba->next) {
2659 if (hba->type == SYS_DEV_NVME)
2660 print_nvme_info(hba);
2661 }
2662 printf("\n");
2663 continue;
2664 }
2665
2666 struct devid_list *devid;
2667 for (devid = entry->devid_list; devid; devid = devid->next) {
2668 hba = device_by_id(devid->devid);
2669 if (!hba)
2670 continue;
2671
2672 printf(" I/O Controller : %s (%s)\n",
2673 hba->path, get_sys_dev_type(hba->type));
2674 if (hba->type == SYS_DEV_SATA) {
2675 host_base = ahci_get_port_count(hba->path, &port_count);
2676 if (ahci_enumerate_ports(hba->path, port_count, host_base, verbose)) {
2677 if (verbose > 0)
2678 pr_err("failed to enumerate ports on SATA controller at %s.\n", hba->pci_id);
2679 result |= 2;
2680 }
2681 }
2682 }
2683 printf("\n");
2684 }
2685
2686 return result;
2687 }
2688
2689 static int export_detail_platform_imsm(int verbose, char *controller_path)
2690 {
2691 struct sys_dev *list, *hba;
2692 int result=1;
2693
2694 list = find_intel_devices();
2695 if (!list) {
2696 if (verbose > 0)
2697 pr_err("IMSM_DETAIL_PLATFORM_ERROR=NO_INTEL_DEVICES\n");
2698 result = 2;
2699 return result;
2700 }
2701
2702 for (hba = list; hba; hba = hba->next) {
2703 if (controller_path && (compare_paths(hba->path,controller_path) != 0))
2704 continue;
2705 if (!find_imsm_capability(hba) && verbose > 0) {
2706 char buf[PATH_MAX];
2707 pr_err("IMSM_DETAIL_PLATFORM_ERROR=NO_IMSM_CAPABLE_DEVICE_UNDER_%s\n",
2708 hba->type == SYS_DEV_VMD ? vmd_domain_to_controller(hba, buf) : hba->path);
2709 }
2710 else
2711 result = 0;
2712 }
2713
2714 const struct orom_entry *entry;
2715
2716 for (entry = orom_entries; entry; entry = entry->next) {
2717 if (entry->type == SYS_DEV_VMD) {
2718 for (hba = list; hba; hba = hba->next)
2719 print_imsm_capability_export(&entry->orom);
2720 continue;
2721 }
2722 print_imsm_capability_export(&entry->orom);
2723 }
2724
2725 return result;
2726 }
2727
2728 static int match_home_imsm(struct supertype *st, char *homehost)
2729 {
2730 /* the imsm metadata format does not specify any host
2731 * identification information. We return -1 since we can never
2732 * confirm nor deny whether a given array is "meant" for this
2733 * host. We rely on compare_super and the 'family_num' fields to
2734 * exclude member disks that do not belong, and we rely on
2735 * mdadm.conf to specify the arrays that should be assembled.
2736 * Auto-assembly may still pick up "foreign" arrays.
2737 */
2738
2739 return -1;
2740 }
2741
2742 static void uuid_from_super_imsm(struct supertype *st, int uuid[4])
2743 {
2744 /* The uuid returned here is used for:
2745 * uuid to put into bitmap file (Create, Grow)
2746 * uuid for backup header when saving critical section (Grow)
2747 * comparing uuids when re-adding a device into an array
2748 * In these cases the uuid required is that of the data-array,
2749 * not the device-set.
2750 * uuid to recognise same set when adding a missing device back
2751 * to an array. This is a uuid for the device-set.
2752 *
2753 * For each of these we can make do with a truncated
2754 * or hashed uuid rather than the original, as long as
2755 * everyone agrees.
2756 * In each case the uuid required is that of the data-array,
2757 * not the device-set.
2758 */
2759 /* imsm does not track uuid's so we synthesis one using sha1 on
2760 * - The signature (Which is constant for all imsm array, but no matter)
2761 * - the orig_family_num of the container
2762 * - the index number of the volume
2763 * - the 'serial' number of the volume.
2764 * Hopefully these are all constant.
2765 */
2766 struct intel_super *super = st->sb;
2767
2768 char buf[20];
2769 struct sha1_ctx ctx;
2770 struct imsm_dev *dev = NULL;
2771 __u32 family_num;
2772
2773 /* some mdadm versions failed to set ->orig_family_num, in which
2774 * case fall back to ->family_num. orig_family_num will be
2775 * fixed up with the first metadata update.
2776 */
2777 family_num = super->anchor->orig_family_num;
2778 if (family_num == 0)
2779 family_num = super->anchor->family_num;
2780 sha1_init_ctx(&ctx);
2781 sha1_process_bytes(super->anchor->sig, MPB_SIG_LEN, &ctx);
2782 sha1_process_bytes(&family_num, sizeof(__u32), &ctx);
2783 if (super->current_vol >= 0)
2784 dev = get_imsm_dev(super, super->current_vol);
2785 if (dev) {
2786 __u32 vol = super->current_vol;
2787 sha1_process_bytes(&vol, sizeof(vol), &ctx);
2788 sha1_process_bytes(dev->volume, MAX_RAID_SERIAL_LEN, &ctx);
2789 }
2790 sha1_finish_ctx(&ctx, buf);
2791 memcpy(uuid, buf, 4*4);
2792 }
2793
2794 #if 0
2795 static void
2796 get_imsm_numerical_version(struct imsm_super *mpb, int *m, int *p)
2797 {
2798 __u8 *v = get_imsm_version(mpb);
2799 __u8 *end = mpb->sig + MAX_SIGNATURE_LENGTH;
2800 char major[] = { 0, 0, 0 };
2801 char minor[] = { 0 ,0, 0 };
2802 char patch[] = { 0, 0, 0 };
2803 char *ver_parse[] = { major, minor, patch };
2804 int i, j;
2805
2806 i = j = 0;
2807 while (*v != '\0' && v < end) {
2808 if (*v != '.' && j < 2)
2809 ver_parse[i][j++] = *v;
2810 else {
2811 i++;
2812 j = 0;
2813 }
2814 v++;
2815 }
2816
2817 *m = strtol(minor, NULL, 0);
2818 *p = strtol(patch, NULL, 0);
2819 }
2820 #endif
2821
2822 static __u32 migr_strip_blocks_resync(struct imsm_dev *dev)
2823 {
2824 /* migr_strip_size when repairing or initializing parity */
2825 struct imsm_map *map = get_imsm_map(dev, MAP_0);
2826 __u32 chunk = __le32_to_cpu(map->blocks_per_strip);
2827
2828 switch (get_imsm_raid_level(map)) {
2829 case 5:
2830 case 10:
2831 return chunk;
2832 default:
2833 return 128*1024 >> 9;
2834 }
2835 }
2836
2837 static __u32 migr_strip_blocks_rebuild(struct imsm_dev *dev)
2838 {
2839 /* migr_strip_size when rebuilding a degraded disk, no idea why
2840 * this is different than migr_strip_size_resync(), but it's good
2841 * to be compatible
2842 */
2843 struct imsm_map *map = get_imsm_map(dev, MAP_1);
2844 __u32 chunk = __le32_to_cpu(map->blocks_per_strip);
2845
2846 switch (get_imsm_raid_level(map)) {
2847 case 1:
2848 case 10:
2849 if (map->num_members % map->num_domains == 0)
2850 return 128*1024 >> 9;
2851 else
2852 return chunk;
2853 case 5:
2854 return max((__u32) 64*1024 >> 9, chunk);
2855 default:
2856 return 128*1024 >> 9;
2857 }
2858 }
2859
2860 static __u32 num_stripes_per_unit_resync(struct imsm_dev *dev)
2861 {
2862 struct imsm_map *lo = get_imsm_map(dev, MAP_0);
2863 struct imsm_map *hi = get_imsm_map(dev, MAP_1);
2864 __u32 lo_chunk = __le32_to_cpu(lo->blocks_per_strip);
2865 __u32 hi_chunk = __le32_to_cpu(hi->blocks_per_strip);
2866
2867 return max((__u32) 1, hi_chunk / lo_chunk);
2868 }
2869
2870 static __u32 num_stripes_per_unit_rebuild(struct imsm_dev *dev)
2871 {
2872 struct imsm_map *lo = get_imsm_map(dev, MAP_0);
2873 int level = get_imsm_raid_level(lo);
2874
2875 if (level == 1 || level == 10) {
2876 struct imsm_map *hi = get_imsm_map(dev, MAP_1);
2877
2878 return hi->num_domains;
2879 } else
2880 return num_stripes_per_unit_resync(dev);
2881 }
2882
2883 static __u8 imsm_num_data_members(struct imsm_map *map)
2884 {
2885 /* named 'imsm_' because raid0, raid1 and raid10
2886 * counter-intuitively have the same number of data disks
2887 */
2888 switch (get_imsm_raid_level(map)) {
2889 case 0:
2890 return map->num_members;
2891 break;
2892 case 1:
2893 case 10:
2894 return map->num_members/2;
2895 case 5:
2896 return map->num_members - 1;
2897 default:
2898 dprintf("unsupported raid level\n");
2899 return 0;
2900 }
2901 }
2902
2903 static unsigned long long calc_component_size(struct imsm_map *map,
2904 struct imsm_dev *dev)
2905 {
2906 unsigned long long component_size;
2907 unsigned long long dev_size = imsm_dev_size(dev);
2908 long long calc_dev_size = 0;
2909 unsigned int member_disks = imsm_num_data_members(map);
2910
2911 if (member_disks == 0)
2912 return 0;
2913
2914 component_size = per_dev_array_size(map);
2915 calc_dev_size = component_size * member_disks;
2916
2917 /* Component size is rounded to 1MB so difference between size from
2918 * metadata and size calculated from num_data_stripes equals up to
2919 * 2048 blocks per each device. If the difference is higher it means
2920 * that array size was expanded and num_data_stripes was not updated.
2921 */
2922 if (llabs(calc_dev_size - (long long)dev_size) >
2923 (1 << SECT_PER_MB_SHIFT) * member_disks) {
2924 component_size = dev_size / member_disks;
2925 dprintf("Invalid num_data_stripes in metadata; expected=%llu, found=%llu\n",
2926 component_size / map->blocks_per_strip,
2927 num_data_stripes(map));
2928 }
2929
2930 return component_size;
2931 }
2932
2933 static __u32 parity_segment_depth(struct imsm_dev *dev)
2934 {
2935 struct imsm_map *map = get_imsm_map(dev, MAP_0);
2936 __u32 chunk = __le32_to_cpu(map->blocks_per_strip);
2937
2938 switch(get_imsm_raid_level(map)) {
2939 case 1:
2940 case 10:
2941 return chunk * map->num_domains;
2942 case 5:
2943 return chunk * map->num_members;
2944 default:
2945 return chunk;
2946 }
2947 }
2948
2949 static __u32 map_migr_block(struct imsm_dev *dev, __u32 block)
2950 {
2951 struct imsm_map *map = get_imsm_map(dev, MAP_1);
2952 __u32 chunk = __le32_to_cpu(map->blocks_per_strip);
2953 __u32 strip = block / chunk;
2954
2955 switch (get_imsm_raid_level(map)) {
2956 case 1:
2957 case 10: {
2958 __u32 vol_strip = (strip * map->num_domains) + 1;
2959 __u32 vol_stripe = vol_strip / map->num_members;
2960
2961 return vol_stripe * chunk + block % chunk;
2962 } case 5: {
2963 __u32 stripe = strip / (map->num_members - 1);
2964
2965 return stripe * chunk + block % chunk;
2966 }
2967 default:
2968 return 0;
2969 }
2970 }
2971
2972 static __u64 blocks_per_migr_unit(struct intel_super *super,
2973 struct imsm_dev *dev)
2974 {
2975 /* calculate the conversion factor between per member 'blocks'
2976 * (md/{resync,rebuild}_start) and imsm migration units, return
2977 * 0 for the 'not migrating' and 'unsupported migration' cases
2978 */
2979 if (!dev->vol.migr_state)
2980 return 0;
2981
2982 switch (migr_type(dev)) {
2983 case MIGR_GEN_MIGR: {
2984 struct migr_record *migr_rec = super->migr_rec;
2985 return __le32_to_cpu(migr_rec->blocks_per_unit);
2986 }
2987 case MIGR_VERIFY:
2988 case MIGR_REPAIR:
2989 case MIGR_INIT: {
2990 struct imsm_map *map = get_imsm_map(dev, MAP_0);
2991 __u32 stripes_per_unit;
2992 __u32 blocks_per_unit;
2993 __u32 parity_depth;
2994 __u32 migr_chunk;
2995 __u32 block_map;
2996 __u32 block_rel;
2997 __u32 segment;
2998 __u32 stripe;
2999 __u8 disks;
3000
3001 /* yes, this is really the translation of migr_units to
3002 * per-member blocks in the 'resync' case
3003 */
3004 stripes_per_unit = num_stripes_per_unit_resync(dev);
3005 migr_chunk = migr_strip_blocks_resync(dev);
3006 disks = imsm_num_data_members(map);
3007 blocks_per_unit = stripes_per_unit * migr_chunk * disks;
3008 stripe = __le16_to_cpu(map->blocks_per_strip) * disks;
3009 segment = blocks_per_unit / stripe;
3010 block_rel = blocks_per_unit - segment * stripe;
3011 parity_depth = parity_segment_depth(dev);
3012 block_map = map_migr_block(dev, block_rel);
3013 return block_map + parity_depth * segment;
3014 }
3015 case MIGR_REBUILD: {
3016 __u32 stripes_per_unit;
3017 __u32 migr_chunk;
3018
3019 stripes_per_unit = num_stripes_per_unit_rebuild(dev);
3020 migr_chunk = migr_strip_blocks_rebuild(dev);
3021 return migr_chunk * stripes_per_unit;
3022 }
3023 case MIGR_STATE_CHANGE:
3024 default:
3025 return 0;
3026 }
3027 }
3028
3029 static int imsm_level_to_layout(int level)
3030 {
3031 switch (level) {
3032 case 0:
3033 case 1:
3034 return 0;
3035 case 5:
3036 case 6:
3037 return ALGORITHM_LEFT_ASYMMETRIC;
3038 case 10:
3039 return 0x102;
3040 }
3041 return UnSet;
3042 }
3043
3044 /*******************************************************************************
3045 * Function: read_imsm_migr_rec
3046 * Description: Function reads imsm migration record from last sector of disk
3047 * Parameters:
3048 * fd : disk descriptor
3049 * super : metadata info
3050 * Returns:
3051 * 0 : success,
3052 * -1 : fail
3053 ******************************************************************************/
3054 static int read_imsm_migr_rec(int fd, struct intel_super *super)
3055 {
3056 int ret_val = -1;
3057 unsigned int sector_size = super->sector_size;
3058 unsigned long long dsize;
3059
3060 get_dev_size(fd, NULL, &dsize);
3061 if (lseek64(fd, dsize - (sector_size*MIGR_REC_SECTOR_POSITION),
3062 SEEK_SET) < 0) {
3063 pr_err("Cannot seek to anchor block: %s\n",
3064 strerror(errno));
3065 goto out;
3066 }
3067 if ((unsigned int)read(fd, super->migr_rec_buf,
3068 MIGR_REC_BUF_SECTORS*sector_size) !=
3069 MIGR_REC_BUF_SECTORS*sector_size) {
3070 pr_err("Cannot read migr record block: %s\n",
3071 strerror(errno));
3072 goto out;
3073 }
3074 ret_val = 0;
3075 if (sector_size == 4096)
3076 convert_from_4k_imsm_migr_rec(super);
3077
3078 out:
3079 return ret_val;
3080 }
3081
3082 static struct imsm_dev *imsm_get_device_during_migration(
3083 struct intel_super *super)
3084 {
3085
3086 struct intel_dev *dv;
3087
3088 for (dv = super->devlist; dv; dv = dv->next) {
3089 if (is_gen_migration(dv->dev))
3090 return dv->dev;
3091 }
3092 return NULL;
3093 }
3094
3095 /*******************************************************************************
3096 * Function: load_imsm_migr_rec
3097 * Description: Function reads imsm migration record (it is stored at the last
3098 * sector of disk)
3099 * Parameters:
3100 * super : imsm internal array info
3101 * Returns:
3102 * 0 : success
3103 * -1 : fail
3104 * -2 : no migration in progress
3105 ******************************************************************************/
3106 static int load_imsm_migr_rec(struct intel_super *super)
3107 {
3108 struct dl *dl;
3109 char nm[30];
3110 int retval = -1;
3111 int fd = -1;
3112 struct imsm_dev *dev;
3113 struct imsm_map *map;
3114 int slot = -1;
3115 int keep_fd = 1;
3116
3117 /* find map under migration */
3118 dev = imsm_get_device_during_migration(super);
3119 /* nothing to load,no migration in progress?
3120 */
3121 if (dev == NULL)
3122 return -2;
3123
3124 map = get_imsm_map(dev, MAP_0);
3125 if (!map)
3126 return -1;
3127
3128 for (dl = super->disks; dl; dl = dl->next) {
3129 /* skip spare and failed disks
3130 */
3131 if (dl->index < 0)
3132 continue;
3133 /* read only from one of the first two slots
3134 */
3135 slot = get_imsm_disk_slot(map, dl->index);
3136 if (slot > 1 || slot < 0)
3137 continue;
3138
3139 if (dl->fd < 0) {
3140 sprintf(nm, "%d:%d", dl->major, dl->minor);
3141 fd = dev_open(nm, O_RDONLY);
3142 if (fd >= 0) {
3143 keep_fd = 0;
3144 break;
3145 }
3146 } else {
3147 fd = dl->fd;
3148 break;
3149 }
3150 }
3151
3152 if (fd < 0)
3153 return retval;
3154 retval = read_imsm_migr_rec(fd, super);
3155 if (!keep_fd)
3156 close(fd);
3157
3158 return retval;
3159 }
3160
3161 /*******************************************************************************
3162 * function: imsm_create_metadata_checkpoint_update
3163 * Description: It creates update for checkpoint change.
3164 * Parameters:
3165 * super : imsm internal array info
3166 * u : pointer to prepared update
3167 * Returns:
3168 * Uptate length.
3169 * If length is equal to 0, input pointer u contains no update
3170 ******************************************************************************/
3171 static int imsm_create_metadata_checkpoint_update(
3172 struct intel_super *super,
3173 struct imsm_update_general_migration_checkpoint **u)
3174 {
3175
3176 int update_memory_size = 0;
3177
3178 dprintf("(enter)\n");
3179
3180 if (u == NULL)
3181 return 0;
3182 *u = NULL;
3183
3184 /* size of all update data without anchor */
3185 update_memory_size =
3186 sizeof(struct imsm_update_general_migration_checkpoint);
3187
3188 *u = xcalloc(1, update_memory_size);
3189 if (*u == NULL) {
3190 dprintf("error: cannot get memory\n");
3191 return 0;
3192 }
3193 (*u)->type = update_general_migration_checkpoint;
3194 (*u)->curr_migr_unit = current_migr_unit(super->migr_rec);
3195 dprintf("prepared for %llu\n", (*u)->curr_migr_unit);
3196
3197 return update_memory_size;
3198 }
3199
3200 static void imsm_update_metadata_locally(struct supertype *st,
3201 void *buf, int len);
3202
3203 /*******************************************************************************
3204 * Function: write_imsm_migr_rec
3205 * Description: Function writes imsm migration record
3206 * (at the last sector of disk)
3207 * Parameters:
3208 * super : imsm internal array info
3209 * Returns:
3210 * 0 : success
3211 * -1 : if fail
3212 ******************************************************************************/
3213 static int write_imsm_migr_rec(struct supertype *st)
3214 {
3215 struct intel_super *super = st->sb;
3216 unsigned int sector_size = super->sector_size;
3217 unsigned long long dsize;
3218 int retval = -1;
3219 struct dl *sd;
3220 int len;
3221 struct imsm_update_general_migration_checkpoint *u;
3222 struct imsm_dev *dev;
3223 struct imsm_map *map;
3224
3225 /* find map under migration */
3226 dev = imsm_get_device_during_migration(super);
3227 /* if no migration, write buffer anyway to clear migr_record
3228 * on disk based on first available device
3229 */
3230 if (dev == NULL)
3231 dev = get_imsm_dev(super, super->current_vol < 0 ? 0 :
3232 super->current_vol);
3233
3234 map = get_imsm_map(dev, MAP_0);
3235
3236 if (sector_size == 4096)
3237 convert_to_4k_imsm_migr_rec(super);
3238 for (sd = super->disks ; sd ; sd = sd->next) {
3239 int slot = -1;
3240
3241 /* skip failed and spare devices */
3242 if (sd->index < 0)
3243 continue;
3244 /* write to 2 first slots only */
3245 if (map)
3246 slot = get_imsm_disk_slot(map, sd->index);
3247 if (map == NULL || slot > 1 || slot < 0)
3248 continue;
3249
3250 get_dev_size(sd->fd, NULL, &dsize);
3251 if (lseek64(sd->fd, dsize - (MIGR_REC_SECTOR_POSITION *
3252 sector_size),
3253 SEEK_SET) < 0) {
3254 pr_err("Cannot seek to anchor block: %s\n",
3255 strerror(errno));
3256 goto out;
3257 }
3258 if ((unsigned int)write(sd->fd, super->migr_rec_buf,
3259 MIGR_REC_BUF_SECTORS*sector_size) !=
3260 MIGR_REC_BUF_SECTORS*sector_size) {
3261 pr_err("Cannot write migr record block: %s\n",
3262 strerror(errno));
3263 goto out;
3264 }
3265 }
3266 if (sector_size == 4096)
3267 convert_from_4k_imsm_migr_rec(super);
3268 /* update checkpoint information in metadata */
3269 len = imsm_create_metadata_checkpoint_update(super, &u);
3270 if (len <= 0) {
3271 dprintf("imsm: Cannot prepare update\n");
3272 goto out;
3273 }
3274 /* update metadata locally */
3275 imsm_update_metadata_locally(st, u, len);
3276 /* and possibly remotely */
3277 if (st->update_tail) {
3278 append_metadata_update(st, u, len);
3279 /* during reshape we do all work inside metadata handler
3280 * manage_reshape(), so metadata update has to be triggered
3281 * insida it
3282 */
3283 flush_metadata_updates(st);
3284 st->update_tail = &st->updates;
3285 } else
3286 free(u);
3287
3288 retval = 0;
3289 out:
3290 return retval;
3291 }
3292
3293 /* spare/missing disks activations are not allowe when
3294 * array/container performs reshape operation, because
3295 * all arrays in container works on the same disks set
3296 */
3297 int imsm_reshape_blocks_arrays_changes(struct intel_super *super)
3298 {
3299 int rv = 0;
3300 struct intel_dev *i_dev;
3301 struct imsm_dev *dev;
3302
3303 /* check whole container
3304 */
3305 for (i_dev = super->devlist; i_dev; i_dev = i_dev->next) {
3306 dev = i_dev->dev;
3307 if (is_gen_migration(dev)) {
3308 /* No repair during any migration in container
3309 */
3310 rv = 1;
3311 break;
3312 }
3313 }
3314 return rv;
3315 }
3316 static unsigned long long imsm_component_size_alignment_check(int level,
3317 int chunk_size,
3318 unsigned int sector_size,
3319 unsigned long long component_size)
3320 {
3321 unsigned int component_size_alignment;
3322
3323 /* check component size alignment
3324 */
3325 component_size_alignment = component_size % (chunk_size/sector_size);
3326
3327 dprintf("(Level: %i, chunk_size = %i, component_size = %llu), component_size_alignment = %u\n",
3328 level, chunk_size, component_size,
3329 component_size_alignment);
3330
3331 if (component_size_alignment && (level != 1) && (level != UnSet)) {
3332 dprintf("imsm: reported component size aligned from %llu ",
3333 component_size);
3334 component_size -= component_size_alignment;
3335 dprintf_cont("to %llu (%i).\n",
3336 component_size, component_size_alignment);
3337 }
3338
3339 return component_size;
3340 }
3341
3342 /*******************************************************************************
3343 * Function: get_bitmap_header_sector
3344 * Description: Returns the sector where the bitmap header is placed.
3345 * Parameters:
3346 * st : supertype information
3347 * dev_idx : index of the device with bitmap
3348 *
3349 * Returns:
3350 * The sector where the bitmap header is placed
3351 ******************************************************************************/
3352 static unsigned long long get_bitmap_header_sector(struct intel_super *super,
3353 int dev_idx)
3354 {
3355 struct imsm_dev *dev = get_imsm_dev(super, dev_idx);
3356 struct imsm_map *map = get_imsm_map(dev, MAP_0);
3357
3358 if (!super->sector_size) {
3359 dprintf("sector size is not set\n");
3360 return 0;
3361 }
3362
3363 return pba_of_lba0(map) + calc_component_size(map, dev) +
3364 (IMSM_BITMAP_HEADER_OFFSET / super->sector_size);
3365 }
3366
3367 /*******************************************************************************
3368 * Function: get_bitmap_sector
3369 * Description: Returns the sector where the bitmap is placed.
3370 * Parameters:
3371 * st : supertype information
3372 * dev_idx : index of the device with bitmap
3373 *
3374 * Returns:
3375 * The sector where the bitmap is placed
3376 ******************************************************************************/
3377 static unsigned long long get_bitmap_sector(struct intel_super *super,
3378 int dev_idx)
3379 {
3380 if (!super->sector_size) {
3381 dprintf("sector size is not set\n");
3382 return 0;
3383 }
3384
3385 return get_bitmap_header_sector(super, dev_idx) +
3386 (IMSM_BITMAP_HEADER_SIZE / super->sector_size);
3387 }
3388
3389 static unsigned long long get_ppl_sector(struct intel_super *super, int dev_idx)
3390 {
3391 struct imsm_dev *dev = get_imsm_dev(super, dev_idx);
3392 struct imsm_map *map = get_imsm_map(dev, MAP_0);
3393
3394 return pba_of_lba0(map) +
3395 (num_data_stripes(map) * map->blocks_per_strip);
3396 }
3397
3398 static void getinfo_super_imsm_volume(struct supertype *st, struct mdinfo *info, char *dmap)
3399 {
3400 struct intel_super *super = st->sb;
3401 struct migr_record *migr_rec = super->migr_rec;
3402 struct imsm_dev *dev = get_imsm_dev(super, super->current_vol);
3403 struct imsm_map *map = get_imsm_map(dev, MAP_0);
3404 struct imsm_map *prev_map = get_imsm_map(dev, MAP_1);
3405 struct imsm_map *map_to_analyse = map;
3406 struct dl *dl;
3407 int map_disks = info->array.raid_disks;
3408
3409 memset(info, 0, sizeof(*info));
3410 if (prev_map)
3411 map_to_analyse = prev_map;
3412
3413 dl = super->current_disk;
3414
3415 info->container_member = super->current_vol;
3416 info->array.raid_disks = map->num_members;
3417 info->array.level = get_imsm_raid_level(map_to_analyse);
3418 info->array.layout = imsm_level_to_layout(info->array.level);
3419 info->array.md_minor = -1;
3420 info->array.ctime = 0;
3421 info->array.utime = 0;
3422 info->array.chunk_size =
3423 __le16_to_cpu(map_to_analyse->blocks_per_strip) << 9;
3424 info->array.state = !(dev->vol.dirty & RAIDVOL_DIRTY);
3425 info->custom_array_size = imsm_dev_size(dev);
3426 info->recovery_blocked = imsm_reshape_blocks_arrays_changes(st->sb);
3427
3428 if (is_gen_migration(dev)) {
3429 info->reshape_active = 1;
3430 info->new_level = get_imsm_raid_level(map);
3431 info->new_layout = imsm_level_to_layout(info->new_level);
3432 info->new_chunk = __le16_to_cpu(map->blocks_per_strip) << 9;
3433 info->delta_disks = map->num_members - prev_map->num_members;
3434 if (info->delta_disks) {
3435 /* this needs to be applied to every array
3436 * in the container.
3437 */
3438 info->reshape_active = CONTAINER_RESHAPE;
3439 }
3440 /* We shape information that we give to md might have to be
3441 * modify to cope with md's requirement for reshaping arrays.
3442 * For example, when reshaping a RAID0, md requires it to be
3443 * presented as a degraded RAID4.
3444 * Also if a RAID0 is migrating to a RAID5 we need to specify
3445 * the array as already being RAID5, but the 'before' layout
3446 * is a RAID4-like layout.
3447 */
3448 switch (info->array.level) {
3449 case 0:
3450 switch(info->new_level) {
3451 case 0:
3452 /* conversion is happening as RAID4 */
3453 info->array.level = 4;
3454 info->array.raid_disks += 1;
3455 break;
3456 case 5:
3457 /* conversion is happening as RAID5 */
3458 info->array.level = 5;
3459 info->array.layout = ALGORITHM_PARITY_N;
3460 info->delta_disks -= 1;
3461 break;
3462 default:
3463 /* FIXME error message */
3464 info->array.level = UnSet;
3465 break;
3466 }
3467 break;
3468 }
3469 } else {
3470 info->new_level = UnSet;
3471 info->new_layout = UnSet;
3472 info->new_chunk = info->array.chunk_size;
3473 info->delta_disks = 0;
3474 }
3475
3476 if (dl) {
3477 info->disk.major = dl->major;
3478 info->disk.minor = dl->minor;
3479 info->disk.number = dl->index;
3480 info->disk.raid_disk = get_imsm_disk_slot(map_to_analyse,
3481 dl->index);
3482 }
3483
3484 info->data_offset = pba_of_lba0(map_to_analyse);
3485 info->component_size = calc_component_size(map, dev);
3486 info->component_size = imsm_component_size_alignment_check(
3487 info->array.level,
3488 info->array.chunk_size,
3489 super->sector_size,
3490 info->component_size);
3491 info->bb.supported = 1;
3492
3493 memset(info->uuid, 0, sizeof(info->uuid));
3494 info->recovery_start = MaxSector;
3495
3496 if (info->array.level == 5 &&
3497 (dev->rwh_policy == RWH_DISTRIBUTED ||
3498 dev->rwh_policy == RWH_MULTIPLE_DISTRIBUTED)) {
3499 info->consistency_policy = CONSISTENCY_POLICY_PPL;
3500 info->ppl_sector = get_ppl_sector(super, super->current_vol);
3501 if (dev->rwh_policy == RWH_MULTIPLE_DISTRIBUTED)
3502 info->ppl_size = MULTIPLE_PPL_AREA_SIZE_IMSM >> 9;
3503 else
3504 info->ppl_size = (PPL_HEADER_SIZE + PPL_ENTRY_SPACE)
3505 >> 9;
3506 } else if (info->array.level <= 0) {
3507 info->consistency_policy = CONSISTENCY_POLICY_NONE;
3508 } else {
3509 if (dev->rwh_policy == RWH_BITMAP) {
3510 info->bitmap_offset = get_bitmap_sector(super, super->current_vol);
3511 info->consistency_policy = CONSISTENCY_POLICY_BITMAP;
3512 } else {
3513 info->consistency_policy = CONSISTENCY_POLICY_RESYNC;
3514 }
3515 }
3516
3517 info->reshape_progress = 0;
3518 info->resync_start = MaxSector;
3519 if ((map_to_analyse->map_state == IMSM_T_STATE_UNINITIALIZED ||
3520 !(info->array.state & 1)) &&
3521 imsm_reshape_blocks_arrays_changes(super) == 0) {
3522 info->resync_start = 0;
3523 }
3524 if (dev->vol.migr_state) {
3525 switch (migr_type(dev)) {
3526 case MIGR_REPAIR:
3527 case MIGR_INIT: {
3528 __u64 blocks_per_unit = blocks_per_migr_unit(super,
3529 dev);
3530 __u64 units = vol_curr_migr_unit(dev);
3531
3532 info->resync_start = blocks_per_unit * units;
3533 break;
3534 }
3535 case MIGR_GEN_MIGR: {
3536 __u64 blocks_per_unit = blocks_per_migr_unit(super,
3537 dev);
3538 __u64 units = current_migr_unit(migr_rec);
3539 int used_disks;
3540
3541 if (__le32_to_cpu(migr_rec->ascending_migr) &&
3542 (units <
3543 (get_num_migr_units(migr_rec)-1)) &&
3544 (super->migr_rec->rec_status ==
3545 __cpu_to_le32(UNIT_SRC_IN_CP_AREA)))
3546 units++;
3547
3548 info->reshape_progress = blocks_per_unit * units;
3549
3550 dprintf("IMSM: General Migration checkpoint : %llu (%llu) -> read reshape progress : %llu\n",
3551 (unsigned long long)units,
3552 (unsigned long long)blocks_per_unit,
3553 info->reshape_progress);
3554
3555 used_disks = imsm_num_data_members(prev_map);
3556 if (used_disks > 0) {
3557 info->custom_array_size = per_dev_array_size(map) *
3558 used_disks;
3559 }
3560 }
3561 case MIGR_VERIFY:
3562 /* we could emulate the checkpointing of
3563 * 'sync_action=check' migrations, but for now
3564 * we just immediately complete them
3565 */
3566 case MIGR_REBUILD:
3567 /* this is handled by container_content_imsm() */
3568 case MIGR_STATE_CHANGE:
3569 /* FIXME handle other migrations */
3570 default:
3571 /* we are not dirty, so... */
3572 info->resync_start = MaxSector;
3573 }
3574 }
3575
3576 strncpy(info->name, (char *) dev->volume, MAX_RAID_SERIAL_LEN);
3577 info->name[MAX_RAID_SERIAL_LEN] = 0;
3578
3579 info->array.major_version = -1;
3580 info->array.minor_version = -2;
3581 sprintf(info->text_version, "/%s/%d", st->container_devnm, info->container_member);
3582 info->safe_mode_delay = 4000; /* 4 secs like the Matrix driver */
3583 uuid_from_super_imsm(st, info->uuid);
3584
3585 if (dmap) {
3586 int i, j;
3587 for (i=0; i<map_disks; i++) {
3588 dmap[i] = 0;
3589 if (i < info->array.raid_disks) {
3590 struct imsm_disk *dsk;
3591 j = get_imsm_disk_idx(dev, i, MAP_X);
3592 dsk = get_imsm_disk(super, j);
3593 if (dsk && (dsk->status & CONFIGURED_DISK))
3594 dmap[i] = 1;
3595 }
3596 }
3597 }
3598 }
3599
3600 static __u8 imsm_check_degraded(struct intel_super *super, struct imsm_dev *dev,
3601 int failed, int look_in_map);
3602
3603 static int imsm_count_failed(struct intel_super *super, struct imsm_dev *dev,
3604 int look_in_map);
3605
3606 static void manage_second_map(struct intel_super *super, struct imsm_dev *dev)
3607 {
3608 if (is_gen_migration(dev)) {
3609 int failed;
3610 __u8 map_state;
3611 struct imsm_map *map2 = get_imsm_map(dev, MAP_1);
3612
3613 failed = imsm_count_failed(super, dev, MAP_1);
3614 map_state = imsm_check_degraded(super, dev, failed, MAP_1);
3615 if (map2->map_state != map_state) {
3616 map2->map_state = map_state;
3617 super->updates_pending++;
3618 }
3619 }
3620 }
3621
3622 static struct imsm_disk *get_imsm_missing(struct intel_super *super, __u8 index)
3623 {
3624 struct dl *d;
3625
3626 for (d = super->missing; d; d = d->next)
3627 if (d->index == index)
3628 return &d->disk;
3629 return NULL;
3630 }
3631
3632 static void getinfo_super_imsm(struct supertype *st, struct mdinfo *info, char *map)
3633 {
3634 struct intel_super *super = st->sb;
3635 struct imsm_disk *disk;
3636 int map_disks = info->array.raid_disks;
3637 int max_enough = -1;
3638 int i;
3639 struct imsm_super *mpb;
3640
3641 if (super->current_vol >= 0) {
3642 getinfo_super_imsm_volume(st, info, map);
3643 return;
3644 }
3645 memset(info, 0, sizeof(*info));
3646
3647 /* Set raid_disks to zero so that Assemble will always pull in valid
3648 * spares
3649 */
3650 info->array.raid_disks = 0;
3651 info->array.level = LEVEL_CONTAINER;
3652 info->array.layout = 0;
3653 info->array.md_minor = -1;
3654 info->array.ctime = 0; /* N/A for imsm */
3655 info->array.utime = 0;
3656 info->array.chunk_size = 0;
3657
3658 info->disk.major = 0;
3659 info->disk.minor = 0;
3660 info->disk.raid_disk = -1;
3661 info->reshape_active = 0;
3662 info->array.major_version = -1;
3663 info->array.minor_version = -2;
3664 strcpy(info->text_version, "imsm");
3665 info->safe_mode_delay = 0;
3666 info->disk.number = -1;
3667 info->disk.state = 0;
3668 info->name[0] = 0;
3669 info->recovery_start = MaxSector;
3670 info->recovery_blocked = imsm_reshape_blocks_arrays_changes(st->sb);
3671 info->bb.supported = 1;
3672
3673 /* do we have the all the insync disks that we expect? */
3674 mpb = super->anchor;
3675 info->events = __le32_to_cpu(mpb->generation_num);
3676
3677 for (i = 0; i < mpb->num_raid_devs; i++) {
3678 struct imsm_dev *dev = get_imsm_dev(super, i);
3679 int failed, enough, j, missing = 0;
3680 struct imsm_map *map;
3681 __u8 state;
3682
3683 failed = imsm_count_failed(super, dev, MAP_0);
3684 state = imsm_check_degraded(super, dev, failed, MAP_0);
3685 map = get_imsm_map(dev, MAP_0);
3686
3687 /* any newly missing disks?
3688 * (catches single-degraded vs double-degraded)
3689 */
3690 for (j = 0; j < map->num_members; j++) {
3691 __u32 ord = get_imsm_ord_tbl_ent(dev, j, MAP_0);
3692 __u32 idx = ord_to_idx(ord);
3693
3694 if (super->disks && super->disks->index == (int)idx)
3695 info->disk.raid_disk = j;
3696
3697 if (!(ord & IMSM_ORD_REBUILD) &&
3698 get_imsm_missing(super, idx)) {
3699 missing = 1;
3700 break;
3701 }
3702 }
3703
3704 if (state == IMSM_T_STATE_FAILED)
3705 enough = -1;
3706 else if (state == IMSM_T_STATE_DEGRADED &&
3707 (state != map->map_state || missing))
3708 enough = 0;
3709 else /* we're normal, or already degraded */
3710 enough = 1;
3711 if (is_gen_migration(dev) && missing) {
3712 /* during general migration we need all disks
3713 * that process is running on.
3714 * No new missing disk is allowed.
3715 */
3716 max_enough = -1;
3717 enough = -1;
3718 /* no more checks necessary
3719 */
3720 break;
3721 }
3722 /* in the missing/failed disk case check to see
3723 * if at least one array is runnable
3724 */
3725 max_enough = max(max_enough, enough);
3726 }
3727 dprintf("enough: %d\n", max_enough);
3728 info->container_enough = max_enough;
3729
3730 if (super->disks) {
3731 __u32 reserved = imsm_reserved_sectors(super, super->disks);
3732
3733 disk = &super->disks->disk;
3734 info->data_offset = total_blocks(&super->disks->disk) - reserved;
3735 info->component_size = reserved;
3736 info->disk.state = is_configured(disk) ? (1 << MD_DISK_ACTIVE) : 0;
3737 /* we don't change info->disk.raid_disk here because
3738 * this state will be finalized in mdmon after we have
3739 * found the 'most fresh' version of the metadata
3740 */
3741 info->disk.state |= is_failed(disk) ? (1 << MD_DISK_FAULTY) : 0;
3742 info->disk.state |= (is_spare(disk) || is_journal(disk)) ?
3743 0 : (1 << MD_DISK_SYNC);
3744 }
3745
3746 /* only call uuid_from_super_imsm when this disk is part of a populated container,
3747 * ->compare_super may have updated the 'num_raid_devs' field for spares
3748 */
3749 if (info->disk.state & (1 << MD_DISK_SYNC) || super->anchor->num_raid_devs)
3750 uuid_from_super_imsm(st, info->uuid);
3751 else
3752 memcpy(info->uuid, uuid_zero, sizeof(uuid_zero));
3753
3754 /* I don't know how to compute 'map' on imsm, so use safe default */
3755 if (map) {
3756 int i;
3757 for (i = 0; i < map_disks; i++)
3758 map[i] = 1;
3759 }
3760
3761 }
3762
3763 /* allocates memory and fills disk in mdinfo structure
3764 * for each disk in array */
3765 struct mdinfo *getinfo_super_disks_imsm(struct supertype *st)
3766 {
3767 struct mdinfo *mddev;
3768 struct intel_super *super = st->sb;
3769 struct imsm_disk *disk;
3770 int count = 0;
3771 struct dl *dl;
3772 if (!super || !super->disks)
3773 return NULL;
3774 dl = super->disks;
3775 mddev = xcalloc(1, sizeof(*mddev));
3776 while (dl) {
3777 struct mdinfo *tmp;
3778 disk = &dl->disk;
3779 tmp = xcalloc(1, sizeof(*tmp));
3780 if (mddev->devs)
3781 tmp->next = mddev->devs;
3782 mddev->devs = tmp;
3783 tmp->disk.number = count++;
3784 tmp->disk.major = dl->major;
3785 tmp->disk.minor = dl->minor;
3786 tmp->disk.state = is_configured(disk) ?
3787 (1 << MD_DISK_ACTIVE) : 0;
3788 tmp->disk.state |= is_failed(disk) ? (1 << MD_DISK_FAULTY) : 0;
3789 tmp->disk.state |= is_spare(disk) ? 0 : (1 << MD_DISK_SYNC);
3790 tmp->disk.raid_disk = -1;
3791 dl = dl->next;
3792 }
3793 return mddev;
3794 }
3795
3796 static int update_super_imsm(struct supertype *st, struct mdinfo *info,
3797 char *update, char *devname, int verbose,
3798 int uuid_set, char *homehost)
3799 {
3800 /* For 'assemble' and 'force' we need to return non-zero if any
3801 * change was made. For others, the return value is ignored.
3802 * Update options are:
3803 * force-one : This device looks a bit old but needs to be included,
3804 * update age info appropriately.
3805 * assemble: clear any 'faulty' flag to allow this device to
3806 * be assembled.
3807 * force-array: Array is degraded but being forced, mark it clean
3808 * if that will be needed to assemble it.
3809 *
3810 * newdev: not used ????
3811 * grow: Array has gained a new device - this is currently for
3812 * linear only
3813 * resync: mark as dirty so a resync will happen.
3814 * name: update the name - preserving the homehost
3815 * uuid: Change the uuid of the array to match watch is given
3816 *
3817 * Following are not relevant for this imsm:
3818 * sparc2.2 : update from old dodgey metadata
3819 * super-minor: change the preferred_minor number
3820 * summaries: update redundant counters.
3821 * homehost: update the recorded homehost
3822 * _reshape_progress: record new reshape_progress position.
3823 */
3824 int rv = 1;
3825 struct intel_super *super = st->sb;
3826 struct imsm_super *mpb;
3827
3828 /* we can only update container info */
3829 if (!super || super->current_vol >= 0 || !super->anchor)
3830 return 1;
3831
3832 mpb = super->anchor;
3833
3834 if (strcmp(update, "uuid") == 0) {
3835 /* We take this to mean that the family_num should be updated.
3836 * However that is much smaller than the uuid so we cannot really
3837 * allow an explicit uuid to be given. And it is hard to reliably
3838 * know if one was.
3839 * So if !uuid_set we know the current uuid is random and just used
3840 * the first 'int' and copy it to the other 3 positions.
3841 * Otherwise we require the 4 'int's to be the same as would be the
3842 * case if we are using a random uuid. So an explicit uuid will be
3843 * accepted as long as all for ints are the same... which shouldn't hurt
3844 */
3845 if (!uuid_set) {
3846 info->uuid[1] = info->uuid[2] = info->uuid[3] = info->uuid[0];
3847 rv = 0;
3848 } else {
3849 if (info->uuid[0] != info->uuid[1] ||
3850 info->uuid[1] != info->uuid[2] ||
3851 info->uuid[2] != info->uuid[3])
3852 rv = -1;
3853 else
3854 rv = 0;
3855 }
3856 if (rv == 0)
3857 mpb->orig_family_num = info->uuid[0];
3858 } else if (strcmp(update, "assemble") == 0)
3859 rv = 0;
3860 else
3861 rv = -1;
3862
3863 /* successful update? recompute checksum */
3864 if (rv == 0)
3865 mpb->check_sum = __le32_to_cpu(__gen_imsm_checksum(mpb));
3866
3867 return rv;
3868 }
3869
3870 static size_t disks_to_mpb_size(int disks)
3871 {
3872 size_t size;
3873
3874 size = sizeof(struct imsm_super);
3875 size += (disks - 1) * sizeof(struct imsm_disk);
3876 size += 2 * sizeof(struct imsm_dev);
3877 /* up to 2 maps per raid device (-2 for imsm_maps in imsm_dev */
3878 size += (4 - 2) * sizeof(struct imsm_map);
3879 /* 4 possible disk_ord_tbl's */
3880 size += 4 * (disks - 1) * sizeof(__u32);
3881 /* maximum bbm log */
3882 size += sizeof(struct bbm_log);
3883
3884 return size;
3885 }
3886
3887 static __u64 avail_size_imsm(struct supertype *st, __u64 devsize,
3888 unsigned long long data_offset)
3889 {
3890 if (devsize < (MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS))
3891 return 0;
3892
3893 return devsize - (MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS);
3894 }
3895
3896 static void free_devlist(struct intel_super *super)
3897 {
3898 struct intel_dev *dv;
3899
3900 while (super->devlist) {
3901 dv = super->devlist->next;
3902 free(super->devlist->dev);
3903 free(super->devlist);
3904 super->devlist = dv;
3905 }
3906 }
3907
3908 static void imsm_copy_dev(struct imsm_dev *dest, struct imsm_dev *src)
3909 {
3910 memcpy(dest, src, sizeof_imsm_dev(src, 0));
3911 }
3912
3913 static int compare_super_imsm(struct supertype *st, struct supertype *tst,
3914 int verbose)
3915 {
3916 /*
3917 * return:
3918 * 0 same, or first was empty, and second was copied
3919 * 1 second had wrong number
3920 * 2 wrong uuid
3921 * 3 wrong other info
3922 */
3923 struct intel_super *first = st->sb;
3924 struct intel_super *sec = tst->sb;
3925
3926 if (!first) {
3927 st->sb = tst->sb;
3928 tst->sb = NULL;
3929 return 0;
3930 }
3931 /* in platform dependent environment test if the disks
3932 * use the same Intel hba
3933 * If not on Intel hba at all, allow anything.
3934 */
3935 if (!check_env("IMSM_NO_PLATFORM") && first->hba && sec->hba) {
3936 if (first->hba->type != sec->hba->type) {
3937 if (verbose)
3938 pr_err("HBAs of devices do not match %s != %s\n",
3939 get_sys_dev_type(first->hba->type),
3940 get_sys_dev_type(sec->hba->type));
3941 return 3;
3942 }
3943
3944 if (first->orom != sec->orom) {
3945 if (verbose)
3946 pr_err("HBAs of devices do not match %s != %s\n",
3947 first->hba->pci_id, sec->hba->pci_id);
3948 return 3;
3949 }
3950
3951 }
3952
3953 /* if an anchor does not have num_raid_devs set then it is a free
3954 * floating spare
3955 */
3956 if (first->anchor->num_raid_devs > 0 &&
3957 sec->anchor->num_raid_devs > 0) {
3958 /* Determine if these disks might ever have been
3959 * related. Further disambiguation can only take place
3960 * in load_super_imsm_all
3961 */
3962 __u32 first_family = first->anchor->orig_family_num;
3963 __u32 sec_family = sec->anchor->orig_family_num;
3964
3965 if (memcmp(first->anchor->sig, sec->anchor->sig,
3966 MAX_SIGNATURE_LENGTH) != 0)
3967 return 3;
3968
3969 if (first_family == 0)
3970 first_family = first->anchor->family_num;
3971 if (sec_family == 0)
3972 sec_family = sec->anchor->family_num;
3973
3974 if (first_family != sec_family)
3975 return 3;
3976
3977 }
3978
3979 /* if 'first' is a spare promote it to a populated mpb with sec's
3980 * family number
3981 */
3982 if (first->anchor->num_raid_devs == 0 &&
3983 sec->anchor->num_raid_devs > 0) {
3984 int i;
3985 struct intel_dev *dv;
3986 struct imsm_dev *dev;
3987
3988 /* we need to copy raid device info from sec if an allocation
3989 * fails here we don't associate the spare
3990 */
3991 for (i = 0; i < sec->anchor->num_raid_devs; i++) {
3992 dv = xmalloc(sizeof(*dv));
3993 dev = xmalloc(sizeof_imsm_dev(get_imsm_dev(sec, i), 1));
3994 dv->dev = dev;
3995 dv->index = i;
3996 dv->next = first->devlist;
3997 first->devlist = dv;
3998 }
3999 if (i < sec->anchor->num_raid_devs) {
4000 /* allocation failure */
4001 free_devlist(first);
4002 pr_err("imsm: failed to associate spare\n");
4003 return 3;
4004 }
4005 first->anchor->num_raid_devs = sec->anchor->num_raid_devs;
4006 first->anchor->orig_family_num = sec->anchor->orig_family_num;
4007 first->anchor->family_num = sec->anchor->family_num;
4008 memcpy(first->anchor->sig, sec->anchor->sig, MAX_SIGNATURE_LENGTH);
4009 for (i = 0; i < sec->anchor->num_raid_devs; i++)
4010 imsm_copy_dev(get_imsm_dev(first, i), get_imsm_dev(sec, i));
4011 }
4012
4013 return 0;
4014 }
4015
4016 static void fd2devname(int fd, char *name)
4017 {
4018 struct stat st;
4019 char path[256];
4020 char dname[PATH_MAX];
4021 char *nm;
4022 int rv;
4023
4024 name[0] = '\0';
4025 if (fstat(fd, &st) != 0)
4026 return;
4027 sprintf(path, "/sys/dev/block/%d:%d",
4028 major(st.st_rdev), minor(st.st_rdev));
4029
4030 rv = readlink(path, dname, sizeof(dname)-1);
4031 if (rv <= 0)
4032 return;
4033
4034 dname[rv] = '\0';
4035 nm = strrchr(dname, '/');
4036 if (nm) {
4037 nm++;
4038 snprintf(name, MAX_RAID_SERIAL_LEN, "/dev/%s", nm);
4039 }
4040 }
4041
4042 static int nvme_get_serial(int fd, void *buf, size_t buf_len)
4043 {
4044 char path[60];
4045 char *name = fd2kname(fd);
4046
4047 if (!name)
4048 return 1;
4049
4050 if (strncmp(name, "nvme", 4) != 0)
4051 return 1;
4052
4053 snprintf(path, sizeof(path) - 1, "/sys/block/%s/device/serial", name);
4054
4055 return load_sys(path, buf, buf_len);
4056 }
4057
4058 extern int scsi_get_serial(int fd, void *buf, size_t buf_len);
4059
4060 static int imsm_read_serial(int fd, char *devname,
4061 __u8 *serial, size_t serial_buf_len)
4062 {
4063 char buf[50];
4064 int rv;
4065 size_t len;
4066 char *dest;
4067 char *src;
4068 unsigned int i;
4069
4070 memset(buf, 0, sizeof(buf));
4071
4072 rv = nvme_get_serial(fd, buf, sizeof(buf));
4073
4074 if (rv)
4075 rv = scsi_get_serial(fd, buf, sizeof(buf));
4076
4077 if (rv && check_env("IMSM_DEVNAME_AS_SERIAL")) {
4078 memset(serial, 0, MAX_RAID_SERIAL_LEN);
4079 fd2devname(fd, (char *) serial);
4080 return 0;
4081 }
4082
4083 if (rv != 0) {
4084 if (devname)
4085 pr_err("Failed to retrieve serial for %s\n",
4086 devname);
4087 return rv;
4088 }
4089
4090 /* trim all whitespace and non-printable characters and convert
4091 * ':' to ';'
4092 */
4093 for (i = 0, dest = buf; i < sizeof(buf) && buf[i]; i++) {
4094 src = &buf[i];
4095 if (*src > 0x20) {
4096 /* ':' is reserved for use in placeholder serial
4097 * numbers for missing disks
4098 */
4099 if (*src == ':')
4100 *dest++ = ';';
4101 else
4102 *dest++ = *src;
4103 }
4104 }
4105 len = dest - buf;
4106 dest = buf;
4107
4108 if (len > serial_buf_len) {
4109 /* truncate leading characters */
4110 dest += len - serial_buf_len;
4111 len = serial_buf_len;
4112 }
4113
4114 memset(serial, 0, serial_buf_len);
4115 memcpy(serial, dest, len);
4116
4117 return 0;
4118 }
4119
4120 static int serialcmp(__u8 *s1, __u8 *s2)
4121 {
4122 return strncmp((char *) s1, (char *) s2, MAX_RAID_SERIAL_LEN);
4123 }
4124
4125 static void serialcpy(__u8 *dest, __u8 *src)
4126 {
4127 strncpy((char *) dest, (char *) src, MAX_RAID_SERIAL_LEN);
4128 }
4129
4130 static struct dl *serial_to_dl(__u8 *serial, struct intel_super *super)
4131 {
4132 struct dl *dl;
4133
4134 for (dl = super->disks; dl; dl = dl->next)
4135 if (serialcmp(dl->serial, serial) == 0)
4136 break;
4137
4138 return dl;
4139 }
4140
4141 static struct imsm_disk *
4142 __serial_to_disk(__u8 *serial, struct imsm_super *mpb, int *idx)
4143 {
4144 int i;
4145
4146 for (i = 0; i < mpb->num_disks; i++) {
4147 struct imsm_disk *disk = __get_imsm_disk(mpb, i);
4148
4149 if (serialcmp(disk->serial, serial) == 0) {
4150 if (idx)
4151 *idx = i;
4152 return disk;
4153 }
4154 }
4155
4156 return NULL;
4157 }
4158
4159 static int
4160 load_imsm_disk(int fd, struct intel_super *super, char *devname, int keep_fd)
4161 {
4162 struct imsm_disk *disk;
4163 struct dl *dl;
4164 struct stat stb;
4165 int rv;
4166 char name[40];
4167 __u8 serial[MAX_RAID_SERIAL_LEN];
4168
4169 rv = imsm_read_serial(fd, devname, serial, MAX_RAID_SERIAL_LEN);
4170
4171 if (rv != 0)
4172 return 2;
4173
4174 dl = xcalloc(1, sizeof(*dl));
4175
4176 fstat(fd, &stb);
4177 dl->major = major(stb.st_rdev);
4178 dl->minor = minor(stb.st_rdev);
4179 dl->next = super->disks;
4180 dl->fd = keep_fd ? fd : -1;
4181 assert(super->disks == NULL);
4182 super->disks = dl;
4183 serialcpy(dl->serial, serial);
4184 dl->index = -2;
4185 dl->e = NULL;
4186 fd2devname(fd, name);
4187 if (devname)
4188 dl->devname = xstrdup(devname);
4189 else
4190 dl->devname = xstrdup(name);
4191
4192 /* look up this disk's index in the current anchor */
4193 disk = __serial_to_disk(dl->serial, super->anchor, &dl->index);
4194 if (disk) {
4195 dl->disk = *disk;
4196 /* only set index on disks that are a member of a
4197 * populated contianer, i.e. one with raid_devs
4198 */
4199 if (is_failed(&dl->disk))
4200 dl->index = -2;
4201 else if (is_spare(&dl->disk) || is_journal(&dl->disk))
4202 dl->index = -1;
4203 }
4204
4205 return 0;
4206 }
4207
4208 /* When migrating map0 contains the 'destination' state while map1
4209 * contains the current state. When not migrating map0 contains the
4210 * current state. This routine assumes that map[0].map_state is set to
4211 * the current array state before being called.
4212 *
4213 * Migration is indicated by one of the following states
4214 * 1/ Idle (migr_state=0 map0state=normal||unitialized||degraded||failed)
4215 * 2/ Initialize (migr_state=1 migr_type=MIGR_INIT map0state=normal
4216 * map1state=unitialized)
4217 * 3/ Repair (Resync) (migr_state=1 migr_type=MIGR_REPAIR map0state=normal
4218 * map1state=normal)
4219 * 4/ Rebuild (migr_state=1 migr_type=MIGR_REBUILD map0state=normal
4220 * map1state=degraded)
4221 * 5/ Migration (mig_state=1 migr_type=MIGR_GEN_MIGR map0state=normal
4222 * map1state=normal)
4223 */
4224 static void migrate(struct imsm_dev *dev, struct intel_super *super,
4225 __u8 to_state, int migr_type)
4226 {
4227 struct imsm_map *dest;
4228 struct imsm_map *src = get_imsm_map(dev, MAP_0);
4229
4230 dev->vol.migr_state = 1;
4231 set_migr_type(dev, migr_type);
4232 set_vol_curr_migr_unit(dev, 0);
4233 dest = get_imsm_map(dev, MAP_1);
4234
4235 /* duplicate and then set the target end state in map[0] */
4236 memcpy(dest, src, sizeof_imsm_map(src));
4237 if (migr_type == MIGR_GEN_MIGR) {
4238 __u32 ord;
4239 int i;
4240
4241 for (i = 0; i < src->num_members; i++) {
4242 ord = __le32_to_cpu(src->disk_ord_tbl[i]);
4243 set_imsm_ord_tbl_ent(src, i, ord_to_idx(ord));
4244 }
4245 }
4246
4247 if (migr_type == MIGR_GEN_MIGR)
4248 /* Clear migration record */
4249 memset(super->migr_rec, 0, sizeof(struct migr_record));
4250
4251 src->map_state = to_state;
4252 }
4253
4254 static void end_migration(struct imsm_dev *dev, struct intel_super *super,
4255 __u8 map_state)
4256 {
4257 struct imsm_map *map = get_imsm_map(dev, MAP_0);
4258 struct imsm_map *prev = get_imsm_map(dev, dev->vol.migr_state == 0 ?
4259 MAP_0 : MAP_1);
4260 int i, j;
4261
4262 /* merge any IMSM_ORD_REBUILD bits that were not successfully
4263 * completed in the last migration.
4264 *
4265 * FIXME add support for raid-level-migration
4266 */
4267 if (map_state != map->map_state && (is_gen_migration(dev) == 0) &&
4268 prev->map_state != IMSM_T_STATE_UNINITIALIZED) {
4269 /* when final map state is other than expected
4270 * merge maps (not for migration)
4271 */
4272 int failed;
4273
4274 for (i = 0; i < prev->num_members; i++)
4275 for (j = 0; j < map->num_members; j++)
4276 /* during online capacity expansion
4277 * disks position can be changed
4278 * if takeover is used
4279 */
4280 if (ord_to_idx(map->disk_ord_tbl[j]) ==
4281 ord_to_idx(prev->disk_ord_tbl[i])) {
4282 map->disk_ord_tbl[j] |=
4283 prev->disk_ord_tbl[i];
4284 break;
4285 }
4286 failed = imsm_count_failed(super, dev, MAP_0);
4287 map_state = imsm_check_degraded(super, dev, failed, MAP_0);
4288 }
4289
4290 dev->vol.migr_state = 0;
4291 set_migr_type(dev, 0);
4292 set_vol_curr_migr_unit(dev, 0);
4293 map->map_state = map_state;
4294 }
4295
4296 static int parse_raid_devices(struct intel_super *super)
4297 {
4298 int i;
4299 struct imsm_dev *dev_new;
4300 size_t len, len_migr;
4301 size_t max_len = 0;
4302 size_t space_needed = 0;
4303 struct imsm_super *mpb = super->anchor;
4304
4305 for (i = 0; i < super->anchor->num_raid_devs; i++) {
4306 struct imsm_dev *dev_iter = __get_imsm_dev(super->anchor, i);
4307 struct intel_dev *dv;
4308
4309 len = sizeof_imsm_dev(dev_iter, 0);
4310 len_migr = sizeof_imsm_dev(dev_iter, 1);
4311 if (len_migr > len)
4312 space_needed += len_migr - len;
4313
4314 dv = xmalloc(sizeof(*dv));
4315 if (max_len < len_migr)
4316 max_len = len_migr;
4317 if (max_len > len_migr)
4318 space_needed += max_len - len_migr;
4319 dev_new = xmalloc(max_len);
4320 imsm_copy_dev(dev_new, dev_iter);
4321 dv->dev = dev_new;
4322 dv->index = i;
4323 dv->next = super->devlist;
4324 super->devlist = dv;
4325 }
4326
4327 /* ensure that super->buf is large enough when all raid devices
4328 * are migrating
4329 */
4330 if (__le32_to_cpu(mpb->mpb_size) + space_needed > super->len) {
4331 void *buf;
4332
4333 len = ROUND_UP(__le32_to_cpu(mpb->mpb_size) + space_needed,
4334 super->sector_size);
4335 if (posix_memalign(&buf, MAX_SECTOR_SIZE, len) != 0)
4336 return 1;
4337
4338 memcpy(buf, super->buf, super->len);
4339 memset(buf + super->len, 0, len - super->len);
4340 free(super->buf);
4341 super->buf = buf;
4342 super->len = len;
4343 }
4344
4345 super->extra_space += space_needed;
4346
4347 return 0;
4348 }
4349
4350 /*******************************************************************************
4351 * Function: check_mpb_migr_compatibility
4352 * Description: Function checks for unsupported migration features:
4353 * - migration optimization area (pba_of_lba0)
4354 * - descending reshape (ascending_migr)
4355 * Parameters:
4356 * super : imsm metadata information
4357 * Returns:
4358 * 0 : migration is compatible
4359 * -1 : migration is not compatible
4360 ******************************************************************************/
4361 int check_mpb_migr_compatibility(struct intel_super *super)
4362 {
4363 struct imsm_map *map0, *map1;
4364 struct migr_record *migr_rec = super->migr_rec;
4365 int i;
4366
4367 for (i = 0; i < super->anchor->num_raid_devs; i++) {
4368 struct imsm_dev *dev_iter = __get_imsm_dev(super->anchor, i);
4369
4370 if (dev_iter &&
4371 dev_iter->vol.migr_state == 1 &&
4372 dev_iter->vol.migr_type == MIGR_GEN_MIGR) {
4373 /* This device is migrating */
4374 map0 = get_imsm_map(dev_iter, MAP_0);
4375 map1 = get_imsm_map(dev_iter, MAP_1);
4376 if (pba_of_lba0(map0) != pba_of_lba0(map1))
4377 /* migration optimization area was used */
4378 return -1;
4379 if (migr_rec->ascending_migr == 0 &&
4380 migr_rec->dest_depth_per_unit > 0)
4381 /* descending reshape not supported yet */
4382 return -1;
4383 }
4384 }
4385 return 0;
4386 }
4387
4388 static void __free_imsm(struct intel_super *super, int free_disks);
4389
4390 /* load_imsm_mpb - read matrix metadata
4391 * allocates super->mpb to be freed by free_imsm
4392 */
4393 static int load_imsm_mpb(int fd, struct intel_super *super, char *devname)
4394 {
4395 unsigned long long dsize;
4396 unsigned long long sectors;
4397 unsigned int sector_size = super->sector_size;
4398 struct stat;
4399 struct imsm_super *anchor;
4400 __u32 check_sum;
4401
4402 get_dev_size(fd, NULL, &dsize);
4403 if (dsize < 2*sector_size) {
4404 if (devname)
4405 pr_err("%s: device to small for imsm\n",
4406 devname);
4407 return 1;
4408 }
4409
4410 if (lseek64(fd, dsize - (sector_size * 2), SEEK_SET) < 0) {
4411 if (devname)
4412 pr_err("Cannot seek to anchor block on %s: %s\n",
4413 devname, strerror(errno));
4414 return 1;
4415 }
4416
4417 if (posix_memalign((void **)&anchor, sector_size, sector_size) != 0) {
4418 if (devname)
4419 pr_err("Failed to allocate imsm anchor buffer on %s\n", devname);
4420 return 1;
4421 }
4422 if ((unsigned int)read(fd, anchor, sector_size) != sector_size) {
4423 if (devname)
4424 pr_err("Cannot read anchor block on %s: %s\n",
4425 devname, strerror(errno));
4426 free(anchor);
4427 return 1;
4428 }
4429
4430 if (strncmp((char *) anchor->sig, MPB_SIGNATURE, MPB_SIG_LEN) != 0) {
4431 if (devname)
4432 pr_err("no IMSM anchor on %s\n", devname);
4433 free(anchor);
4434 return 2;
4435 }
4436
4437 __free_imsm(super, 0);
4438 /* reload capability and hba */
4439
4440 /* capability and hba must be updated with new super allocation */
4441 find_intel_hba_capability(fd, super, devname);
4442 super->len = ROUND_UP(anchor->mpb_size, sector_size);
4443 if (posix_memalign(&super->buf, MAX_SECTOR_SIZE, super->len) != 0) {
4444 if (devname)
4445 pr_err("unable to allocate %zu byte mpb buffer\n",
4446 super->len);
4447 free(anchor);
4448 return 2;
4449 }
4450 memcpy(super->buf, anchor, sector_size);
4451
4452 sectors = mpb_sectors(anchor, sector_size) - 1;
4453 free(anchor);
4454
4455 if (posix_memalign(&super->migr_rec_buf, MAX_SECTOR_SIZE,
4456 MIGR_REC_BUF_SECTORS*MAX_SECTOR_SIZE) != 0) {
4457 pr_err("could not allocate migr_rec buffer\n");
4458 free(super->buf);
4459 return 2;
4460 }
4461 super->clean_migration_record_by_mdmon = 0;
4462
4463 if (!sectors) {
4464 check_sum = __gen_imsm_checksum(super->anchor);
4465 if (check_sum != __le32_to_cpu(super->anchor->check_sum)) {
4466 if (devname)
4467 pr_err("IMSM checksum %x != %x on %s\n",
4468 check_sum,
4469 __le32_to_cpu(super->anchor->check_sum),
4470 devname);
4471 return 2;
4472 }
4473
4474 return 0;
4475 }
4476
4477 /* read the extended mpb */
4478 if (lseek64(fd, dsize - (sector_size * (2 + sectors)), SEEK_SET) < 0) {
4479 if (devname)
4480 pr_err("Cannot seek to extended mpb on %s: %s\n",
4481 devname, strerror(errno));
4482 return 1;
4483 }
4484
4485 if ((unsigned int)read(fd, super->buf + sector_size,
4486 super->len - sector_size) != super->len - sector_size) {
4487 if (devname)
4488 pr_err("Cannot read extended mpb on %s: %s\n",
4489 devname, strerror(errno));
4490 return 2;
4491 }
4492
4493 check_sum = __gen_imsm_checksum(super->anchor);
4494 if (check_sum != __le32_to_cpu(super->anchor->check_sum)) {
4495 if (devname)
4496 pr_err("IMSM checksum %x != %x on %s\n",
4497 check_sum, __le32_to_cpu(super->anchor->check_sum),
4498 devname);
4499 return 3;
4500 }
4501
4502 return 0;
4503 }
4504
4505 static int read_imsm_migr_rec(int fd, struct intel_super *super);
4506
4507 /* clears hi bits in metadata if MPB_ATTRIB_2TB_DISK not set */
4508 static void clear_hi(struct intel_super *super)
4509 {
4510 struct imsm_super *mpb = super->anchor;
4511 int i, n;
4512 if (mpb->attributes & MPB_ATTRIB_2TB_DISK)
4513 return;
4514 for (i = 0; i < mpb->num_disks; ++i) {
4515 struct imsm_disk *disk = &mpb->disk[i];
4516 disk->total_blocks_hi = 0;
4517 }
4518 for (i = 0; i < mpb->num_raid_devs; ++i) {
4519 struct imsm_dev *dev = get_imsm_dev(super, i);
4520 if (!dev)
4521 return;
4522 for (n = 0; n < 2; ++n) {
4523 struct imsm_map *map = get_imsm_map(dev, n);
4524 if (!map)
4525 continue;
4526 map->pba_of_lba0_hi = 0;
4527 map->blocks_per_member_hi = 0;
4528 map->num_data_stripes_hi = 0;
4529 }
4530 }
4531 }
4532
4533 static int
4534 load_and_parse_mpb(int fd, struct intel_super *super, char *devname, int keep_fd)
4535 {
4536 int err;
4537
4538 err = load_imsm_mpb(fd, super, devname);
4539 if (err)
4540 return err;
4541 if (super->sector_size == 4096)
4542 convert_from_4k(super);
4543 err = load_imsm_disk(fd, super, devname, keep_fd);
4544 if (err)
4545 return err;
4546 err = parse_raid_devices(super);
4547 if (err)
4548 return err;
4549 err = load_bbm_log(super);
4550 clear_hi(super);
4551 return err;
4552 }
4553
4554 static void __free_imsm_disk(struct dl *d)
4555 {
4556 if (d->fd >= 0)
4557 close(d->fd);
4558 if (d->devname)
4559 free(d->devname);
4560 if (d->e)
4561 free(d->e);
4562 free(d);
4563
4564 }
4565
4566 static void free_imsm_disks(struct intel_super *super)
4567 {
4568 struct dl *d;
4569
4570 while (super->disks) {
4571 d = super->disks;
4572 super->disks = d->next;
4573 __free_imsm_disk(d);
4574 }
4575 while (super->disk_mgmt_list) {
4576 d = super->disk_mgmt_list;
4577 super->disk_mgmt_list = d->next;
4578 __free_imsm_disk(d);
4579 }
4580 while (super->missing) {
4581 d = super->missing;
4582 super->missing = d->next;
4583 __free_imsm_disk(d);
4584 }
4585
4586 }
4587
4588 /* free all the pieces hanging off of a super pointer */
4589 static void __free_imsm(struct intel_super *super, int free_disks)
4590 {
4591 struct intel_hba *elem, *next;
4592
4593 if (super->buf) {
4594 free(super->buf);
4595 super->buf = NULL;
4596 }
4597 /* unlink capability description */
4598 super->orom = NULL;
4599 if (super->migr_rec_buf) {
4600 free(super->migr_rec_buf);
4601 super->migr_rec_buf = NULL;
4602 }
4603 if (free_disks)
4604 free_imsm_disks(super);
4605 free_devlist(super);
4606 elem = super->hba;
4607 while (elem) {
4608 if (elem->path)
4609 free((void *)elem->path);
4610 next = elem->next;
4611 free(elem);
4612 elem = next;
4613 }
4614 if (super->bbm_log)
4615 free(super->bbm_log);
4616 super->hba = NULL;
4617 }
4618
4619 static void free_imsm(struct intel_super *super)
4620 {
4621 __free_imsm(super, 1);
4622 free(super->bb.entries);
4623 free(super);
4624 }
4625
4626 static void free_super_imsm(struct supertype *st)
4627 {
4628 struct intel_super *super = st->sb;
4629
4630 if (!super)
4631 return;
4632
4633 free_imsm(super);
4634 st->sb = NULL;
4635 }
4636
4637 static struct intel_super *alloc_super(void)
4638 {
4639 struct intel_super *super = xcalloc(1, sizeof(*super));
4640
4641 super->current_vol = -1;
4642 super->create_offset = ~((unsigned long long) 0);
4643
4644 super->bb.entries = xmalloc(BBM_LOG_MAX_ENTRIES *
4645 sizeof(struct md_bb_entry));
4646 if (!super->bb.entries) {
4647 free(super);
4648 return NULL;
4649 }
4650
4651 return super;
4652 }
4653
4654 /*
4655 * find and allocate hba and OROM/EFI based on valid fd of RAID component device
4656 */
4657 static int find_intel_hba_capability(int fd, struct intel_super *super, char *devname)
4658 {
4659 struct sys_dev *hba_name;
4660 int rv = 0;
4661
4662 if (fd >= 0 && test_partition(fd)) {
4663 pr_err("imsm: %s is a partition, cannot be used in IMSM\n",
4664 devname);
4665 return 1;
4666 }
4667 if (fd < 0 || check_env("IMSM_NO_PLATFORM")) {
4668 super->orom = NULL;
4669 super->hba = NULL;
4670 return 0;
4671 }
4672 hba_name = find_disk_attached_hba(fd, NULL);
4673 if (!hba_name) {
4674 if (devname)
4675 pr_err("%s is not attached to Intel(R) RAID controller.\n",
4676 devname);
4677 return 1;
4678 }
4679 rv = attach_hba_to_super(super, hba_name);
4680 if (rv == 2) {
4681 if (devname) {
4682 struct intel_hba *hba = super->hba;
4683
4684 pr_err("%s is attached to Intel(R) %s %s (%s),\n"
4685 " but the container is assigned to Intel(R) %s %s (",
4686 devname,
4687 get_sys_dev_type(hba_name->type),
4688 hba_name->type == SYS_DEV_VMD ? "domain" : "RAID controller",
4689 hba_name->pci_id ? : "Err!",
4690 get_sys_dev_type(super->hba->type),
4691 hba->type == SYS_DEV_VMD ? "domain" : "RAID controller");
4692
4693 while (hba) {
4694 fprintf(stderr, "%s", hba->pci_id ? : "Err!");
4695 if (hba->next)
4696 fprintf(stderr, ", ");
4697 hba = hba->next;
4698 }
4699 fprintf(stderr, ").\n"
4700 " Mixing devices attached to different controllers is not allowed.\n");
4701 }
4702 return 2;
4703 }
4704 super->orom = find_imsm_capability(hba_name);
4705 if (!super->orom)
4706 return 3;
4707
4708 return 0;
4709 }
4710
4711 /* find_missing - helper routine for load_super_imsm_all that identifies
4712 * disks that have disappeared from the system. This routine relies on
4713 * the mpb being uptodate, which it is at load time.
4714 */
4715 static int find_missing(struct intel_super *super)
4716 {
4717 int i;
4718 struct imsm_super *mpb = super->anchor;
4719 struct dl *dl;
4720 struct imsm_disk *disk;
4721
4722 for (i = 0; i < mpb->num_disks; i++) {
4723 disk = __get_imsm_disk(mpb, i);
4724 dl = serial_to_dl(disk->serial, super);
4725 if (dl)
4726 continue;
4727
4728 dl = xmalloc(sizeof(*dl));
4729 dl->major = 0;
4730 dl->minor = 0;
4731 dl->fd = -1;
4732 dl->devname = xstrdup("missing");
4733 dl->index = i;
4734 serialcpy(dl->serial, disk->serial);
4735 dl->disk = *disk;
4736 dl->e = NULL;
4737 dl->next = super->missing;
4738 super->missing = dl;
4739 }
4740
4741 return 0;
4742 }
4743
4744 static struct intel_disk *disk_list_get(__u8 *serial, struct intel_disk *disk_list)
4745 {
4746 struct intel_disk *idisk = disk_list;
4747
4748 while (idisk) {
4749 if (serialcmp(idisk->disk.serial, serial) == 0)
4750 break;
4751 idisk = idisk->next;
4752 }
4753
4754 return idisk;
4755 }
4756
4757 static int __prep_thunderdome(struct intel_super **table, int tbl_size,
4758 struct intel_super *super,
4759 struct intel_disk **disk_list)
4760 {
4761 struct imsm_disk *d = &super->disks->disk;
4762 struct imsm_super *mpb = super->anchor;
4763 int i, j;
4764
4765 for (i = 0; i < tbl_size; i++) {
4766 struct imsm_super *tbl_mpb = table[i]->anchor;
4767 struct imsm_disk *tbl_d = &table[i]->disks->disk;
4768
4769 if (tbl_mpb->family_num == mpb->family_num) {
4770 if (tbl_mpb->check_sum == mpb->check_sum) {
4771 dprintf("mpb from %d:%d matches %d:%d\n",
4772 super->disks->major,
4773 super->disks->minor,
4774 table[i]->disks->major,
4775 table[i]->disks->minor);
4776 break;
4777 }
4778
4779 if (((is_configured(d) && !is_configured(tbl_d)) ||
4780 is_configured(d) == is_configured(tbl_d)) &&
4781 tbl_mpb->generation_num < mpb->generation_num) {
4782 /* current version of the mpb is a
4783 * better candidate than the one in
4784 * super_table, but copy over "cross
4785 * generational" status
4786 */
4787 struct intel_disk *idisk;
4788
4789 dprintf("mpb from %d:%d replaces %d:%d\n",
4790 super->disks->major,
4791 super->disks->minor,
4792 table[i]->disks->major,
4793 table[i]->disks->minor);
4794
4795 idisk = disk_list_get(tbl_d->serial, *disk_list);
4796 if (idisk && is_failed(&idisk->disk))
4797 tbl_d->status |= FAILED_DISK;
4798 break;
4799 } else {
4800 struct intel_disk *idisk;
4801 struct imsm_disk *disk;
4802
4803 /* tbl_mpb is more up to date, but copy
4804 * over cross generational status before
4805 * returning
4806 */
4807 disk = __serial_to_disk(d->serial, mpb, NULL);
4808 if (disk && is_failed(disk))
4809 d->status |= FAILED_DISK;
4810
4811 idisk = disk_list_get(d->serial, *disk_list);
4812 if (idisk) {
4813 idisk->owner = i;
4814 if (disk && is_configured(disk))
4815 idisk->disk.status |= CONFIGURED_DISK;
4816 }
4817
4818 dprintf("mpb from %d:%d prefer %d:%d\n",
4819 super->disks->major,
4820 super->disks->minor,
4821 table[i]->disks->major,
4822 table[i]->disks->minor);
4823
4824 return tbl_size;
4825 }
4826 }
4827 }
4828
4829 if (i >= tbl_size)
4830 table[tbl_size++] = super;
4831 else
4832 table[i] = super;
4833
4834 /* update/extend the merged list of imsm_disk records */
4835 for (j = 0; j < mpb->num_disks; j++) {
4836 struct imsm_disk *disk = __get_imsm_disk(mpb, j);
4837 struct intel_disk *idisk;
4838
4839 idisk = disk_list_get(disk->serial, *disk_list);
4840 if (idisk) {
4841 idisk->disk.status |= disk->status;
4842 if (is_configured(&idisk->disk) ||
4843 is_failed(&idisk->disk))
4844 idisk->disk.status &= ~(SPARE_DISK);
4845 } else {
4846 idisk = xcalloc(1, sizeof(*idisk));
4847 idisk->owner = IMSM_UNKNOWN_OWNER;
4848 idisk->disk = *disk;
4849 idisk->next = *disk_list;
4850 *disk_list = idisk;
4851 }
4852
4853 if (serialcmp(idisk->disk.serial, d->serial) == 0)
4854 idisk->owner = i;
4855 }
4856
4857 return tbl_size;
4858 }
4859
4860 static struct intel_super *
4861 validate_members(struct intel_super *super, struct intel_disk *disk_list,
4862 const int owner)
4863 {
4864 struct imsm_super *mpb = super->anchor;
4865 int ok_count = 0;
4866 int i;
4867
4868 for (i = 0; i < mpb->num_disks; i++) {
4869 struct imsm_disk *disk = __get_imsm_disk(mpb, i);
4870 struct intel_disk *idisk;
4871
4872 idisk = disk_list_get(disk->serial, disk_list);
4873 if (idisk) {
4874 if (idisk->owner == owner ||
4875 idisk->owner == IMSM_UNKNOWN_OWNER)
4876 ok_count++;
4877 else
4878 dprintf("'%.16s' owner %d != %d\n",
4879 disk->serial, idisk->owner,
4880 owner);
4881 } else {
4882 dprintf("unknown disk %x [%d]: %.16s\n",
4883 __le32_to_cpu(mpb->family_num), i,
4884 disk->serial);
4885 break;
4886 }
4887 }
4888
4889 if (ok_count == mpb->num_disks)
4890 return super;
4891 return NULL;
4892 }
4893
4894 static void show_conflicts(__u32 family_num, struct intel_super *super_list)
4895 {
4896 struct intel_super *s;
4897
4898 for (s = super_list; s; s = s->next) {
4899 if (family_num != s->anchor->family_num)
4900 continue;
4901 pr_err("Conflict, offlining family %#x on '%s'\n",
4902 __le32_to_cpu(family_num), s->disks->devname);
4903 }
4904 }
4905
4906 static struct intel_super *
4907 imsm_thunderdome(struct intel_super **super_list, int len)
4908 {
4909 struct intel_super *super_table[len];
4910 struct intel_disk *disk_list = NULL;
4911 struct intel_super *champion, *spare;
4912 struct intel_super *s, **del;
4913 int tbl_size = 0;
4914 int conflict;
4915 int i;
4916
4917 memset(super_table, 0, sizeof(super_table));
4918 for (s = *super_list; s; s = s->next)
4919 tbl_size = __prep_thunderdome(super_table, tbl_size, s, &disk_list);
4920
4921 for (i = 0; i < tbl_size; i++) {
4922 struct imsm_disk *d;
4923 struct intel_disk *idisk;
4924 struct imsm_super *mpb = super_table[i]->anchor;
4925
4926 s = super_table[i];
4927 d = &s->disks->disk;
4928
4929 /* 'd' must appear in merged disk list for its
4930 * configuration to be valid
4931 */
4932 idisk = disk_list_get(d->serial, disk_list);
4933 if (idisk && idisk->owner == i)
4934 s = validate_members(s, disk_list, i);
4935 else
4936 s = NULL;
4937
4938 if (!s)
4939 dprintf("marking family: %#x from %d:%d offline\n",
4940 mpb->family_num,
4941 super_table[i]->disks->major,
4942 super_table[i]->disks->minor);
4943 super_table[i] = s;
4944 }
4945
4946 /* This is where the mdadm implementation differs from the Windows
4947 * driver which has no strict concept of a container. We can only
4948 * assemble one family from a container, so when returning a prodigal
4949 * array member to this system the code will not be able to disambiguate
4950 * the container contents that should be assembled ("foreign" versus
4951 * "local"). It requires user intervention to set the orig_family_num
4952 * to a new value to establish a new container. The Windows driver in
4953 * this situation fixes up the volume name in place and manages the
4954 * foreign array as an independent entity.
4955 */
4956 s = NULL;
4957 spare = NULL;
4958 conflict = 0;
4959 for (i = 0; i < tbl_size; i++) {
4960 struct intel_super *tbl_ent = super_table[i];
4961 int is_spare = 0;
4962
4963 if (!tbl_ent)
4964 continue;
4965
4966 if (tbl_ent->anchor->num_raid_devs == 0) {
4967 spare = tbl_ent;
4968 is_spare = 1;
4969 }
4970
4971 if (s && !is_spare) {
4972 show_conflicts(tbl_ent->anchor->family_num, *super_list);
4973 conflict++;
4974 } else if (!s && !is_spare)
4975 s = tbl_ent;
4976 }
4977
4978 if (!s)
4979 s = spare;
4980 if (!s) {
4981 champion = NULL;
4982 goto out;
4983 }
4984 champion = s;
4985
4986 if (conflict)
4987 pr_err("Chose family %#x on '%s', assemble conflicts to new container with '--update=uuid'\n",
4988 __le32_to_cpu(s->anchor->family_num), s->disks->devname);
4989
4990 /* collect all dl's onto 'champion', and update them to
4991 * champion's version of the status
4992 */
4993 for (s = *super_list; s; s = s->next) {
4994 struct imsm_super *mpb = champion->anchor;
4995 struct dl *dl = s->disks;
4996
4997 if (s == champion)
4998 continue;
4999
5000 mpb->attributes |= s->anchor->attributes & MPB_ATTRIB_2TB_DISK;
5001
5002 for (i = 0; i < mpb->num_disks; i++) {
5003 struct imsm_disk *disk;
5004
5005 disk = __serial_to_disk(dl->serial, mpb, &dl->index);
5006 if (disk) {
5007 dl->disk = *disk;
5008 /* only set index on disks that are a member of
5009 * a populated contianer, i.e. one with
5010 * raid_devs
5011 */
5012 if (is_failed(&dl->disk))
5013 dl->index = -2;
5014 else if (is_spare(&dl->disk))
5015 dl->index = -1;
5016 break;
5017 }
5018 }
5019
5020 if (i >= mpb->num_disks) {
5021 struct intel_disk *idisk;
5022
5023 idisk = disk_list_get(dl->serial, disk_list);
5024 if (idisk && is_spare(&idisk->disk) &&
5025 !is_failed(&idisk->disk) && !is_configured(&idisk->disk))
5026 dl->index = -1;
5027 else {
5028 dl->index = -2;
5029 continue;
5030 }
5031 }
5032
5033 dl->next = champion->disks;
5034 champion->disks = dl;
5035 s->disks = NULL;
5036 }
5037
5038 /* delete 'champion' from super_list */
5039 for (del = super_list; *del; ) {
5040 if (*del == champion) {
5041 *del = (*del)->next;
5042 break;
5043 } else
5044 del = &(*del)->next;
5045 }
5046 champion->next = NULL;
5047
5048 out:
5049 while (disk_list) {
5050 struct intel_disk *idisk = disk_list;
5051
5052 disk_list = disk_list->next;
5053 free(idisk);
5054 }
5055
5056 return champion;
5057 }
5058
5059 static int
5060 get_sra_super_block(int fd, struct intel_super **super_list, char *devname, int *max, int keep_fd);
5061 static int get_super_block(struct intel_super **super_list, char *devnm, char *devname,
5062 int major, int minor, int keep_fd);
5063 static int
5064 get_devlist_super_block(struct md_list *devlist, struct intel_super **super_list,
5065 int *max, int keep_fd);
5066
5067 static int load_super_imsm_all(struct supertype *st, int fd, void **sbp,
5068 char *devname, struct md_list *devlist,
5069 int keep_fd)
5070 {
5071 struct intel_super *super_list = NULL;
5072 struct intel_super *super = NULL;
5073 int err = 0;
5074 int i = 0;
5075
5076 if (fd >= 0)
5077 /* 'fd' is an opened container */
5078 err = get_sra_super_block(fd, &super_list, devname, &i, keep_fd);
5079 else
5080 /* get super block from devlist devices */
5081 err = get_devlist_super_block(devlist, &super_list, &i, keep_fd);
5082 if (err)
5083 goto error;
5084 /* all mpbs enter, maybe one leaves */
5085 super = imsm_thunderdome(&super_list, i);
5086 if (!super) {
5087 err = 1;
5088 goto error;
5089 }
5090
5091 if (find_missing(super) != 0) {
5092 free_imsm(super);
5093 err = 2;
5094 goto error;
5095 }
5096
5097 /* load migration record */
5098 err = load_imsm_migr_rec(super);
5099 if (err == -1) {
5100 /* migration is in progress,
5101 * but migr_rec cannot be loaded,
5102 */
5103 err = 4;
5104 goto error;
5105 }
5106
5107 /* Check migration compatibility */
5108 if (err == 0 && check_mpb_migr_compatibility(super) != 0) {
5109 pr_err("Unsupported migration detected");
5110 if (devname)
5111 fprintf(stderr, " on %s\n", devname);
5112 else
5113 fprintf(stderr, " (IMSM).\n");
5114
5115 err = 5;
5116 goto error;
5117 }
5118
5119 err = 0;
5120
5121 error:
5122 while (super_list) {
5123 struct intel_super *s = super_list;
5124
5125 super_list = super_list->next;
5126 free_imsm(s);
5127 }
5128
5129 if (err)
5130 return err;
5131
5132 *sbp = super;
5133 if (fd >= 0)
5134 strcpy(st->container_devnm, fd2devnm(fd));
5135 else
5136 st->container_devnm[0] = 0;
5137 if (err == 0 && st->ss == NULL) {
5138 st->ss = &super_imsm;
5139 st->minor_version = 0;
5140 st->max_devs = IMSM_MAX_DEVICES;
5141 }
5142 return 0;
5143 }
5144
5145 static int
5146 get_devlist_super_block(struct md_list *devlist, struct intel_super **super_list,
5147 int *max, int keep_fd)
5148 {
5149 struct md_list *tmpdev;
5150 int err = 0;
5151 int i = 0;
5152
5153 for (i = 0, tmpdev = devlist; tmpdev; tmpdev = tmpdev->next) {
5154 if (tmpdev->used != 1)
5155 continue;
5156 if (tmpdev->container == 1) {
5157 int lmax = 0;
5158 int fd = dev_open(tmpdev->devname, O_RDONLY|O_EXCL);
5159 if (fd < 0) {
5160 pr_err("cannot open device %s: %s\n",
5161 tmpdev->devname, strerror(errno));
5162 err = 8;
5163 goto error;
5164 }
5165 err = get_sra_super_block(fd, super_list,
5166 tmpdev->devname, &lmax,
5167 keep_fd);
5168 i += lmax;
5169 close(fd);
5170 if (err) {
5171 err = 7;
5172 goto error;
5173 }
5174 } else {
5175 int major = major(tmpdev->st_rdev);
5176 int minor = minor(tmpdev->st_rdev);
5177 err = get_super_block(super_list,
5178 NULL,
5179 tmpdev->devname,
5180 major, minor,
5181 keep_fd);
5182 i++;
5183 if (err) {
5184 err = 6;
5185 goto error;
5186 }
5187 }
5188 }
5189 error:
5190 *max = i;
5191 return err;
5192 }
5193
5194 static int get_super_block(struct intel_super **super_list, char *devnm, char *devname,
5195 int major, int minor, int keep_fd)
5196 {
5197 struct intel_super *s;
5198 char nm[32];
5199 int dfd = -1;
5200 int err = 0;
5201 int retry;
5202
5203 s = alloc_super();
5204 if (!s) {
5205 err = 1;
5206 goto error;
5207 }
5208
5209 sprintf(nm, "%d:%d", major, minor);
5210 dfd = dev_open(nm, O_RDWR);
5211 if (dfd < 0) {
5212 err = 2;
5213 goto error;
5214 }
5215
5216 get_dev_sector_size(dfd, NULL, &s->sector_size);
5217 find_intel_hba_capability(dfd, s, devname);
5218 err = load_and_parse_mpb(dfd, s, NULL, keep_fd);
5219
5220 /* retry the load if we might have raced against mdmon */
5221 if (err == 3 && devnm && mdmon_running(devnm))
5222 for (retry = 0; retry < 3; retry++) {
5223 usleep(3000);
5224 err = load_and_parse_mpb(dfd, s, NULL, keep_fd);
5225 if (err != 3)
5226 break;
5227 }
5228 error:
5229 if (!err) {
5230 s->next = *super_list;
5231 *super_list = s;
5232 } else {
5233 if (s)
5234 free_imsm(s);
5235 if (dfd >= 0)
5236 close(dfd);
5237 }
5238 if (dfd >= 0 && !keep_fd)
5239 close(dfd);
5240 return err;
5241
5242 }
5243
5244 static int
5245 get_sra_super_block(int fd, struct intel_super **super_list, char *devname, int *max, int keep_fd)
5246 {
5247 struct mdinfo *sra;
5248 char *devnm;
5249 struct mdinfo *sd;
5250 int err = 0;
5251 int i = 0;
5252 sra = sysfs_read(fd, NULL, GET_LEVEL|GET_VERSION|GET_DEVS|GET_STATE);
5253 if (!sra)
5254 return 1;
5255
5256 if (sra->array.major_version != -1 ||
5257 sra->array.minor_version != -2 ||
5258 strcmp(sra->text_version, "imsm") != 0) {
5259 err = 1;
5260 goto error;
5261 }
5262 /* load all mpbs */
5263 devnm = fd2devnm(fd);
5264 for (sd = sra->devs, i = 0; sd; sd = sd->next, i++) {
5265 if (get_super_block(super_list, devnm, devname,
5266 sd->disk.major, sd->disk.minor, keep_fd) != 0) {
5267 err = 7;
5268 goto error;
5269 }
5270 }
5271 error:
5272 sysfs_free(sra);
5273 *max = i;
5274 return err;
5275 }
5276
5277 static int load_container_imsm(struct supertype *st, int fd, char *devname)
5278 {
5279 return load_super_imsm_all(st, fd, &st->sb, devname, NULL, 1);
5280 }
5281
5282 static int load_super_imsm(struct supertype *st, int fd, char *devname)
5283 {
5284 struct intel_super *super;
5285 int rv;
5286 int retry;
5287
5288 if (test_partition(fd))
5289 /* IMSM not allowed on partitions */
5290 return 1;
5291
5292 free_super_imsm(st);
5293
5294 super = alloc_super();
5295 get_dev_sector_size(fd, NULL, &super->sector_size);
5296 if (!super)
5297 return 1;
5298 /* Load hba and capabilities if they exist.
5299 * But do not preclude loading metadata in case capabilities or hba are
5300 * non-compliant and ignore_hw_compat is set.
5301 */
5302 rv = find_intel_hba_capability(fd, super, devname);
5303 /* no orom/efi or non-intel hba of the disk */
5304 if (rv != 0 && st->ignore_hw_compat == 0) {
5305 if (devname)
5306 pr_err("No OROM/EFI properties for %s\n", devname);
5307 free_imsm(super);
5308 return 2;
5309 }
5310 rv = load_and_parse_mpb(fd, super, devname, 0);
5311
5312 /* retry the load if we might have raced against mdmon */
5313 if (rv == 3) {
5314 struct mdstat_ent *mdstat = NULL;
5315 char *name = fd2kname(fd);
5316
5317 if (name)
5318 mdstat = mdstat_by_component(name);
5319
5320 if (mdstat && mdmon_running(mdstat->devnm) && getpid() != mdmon_pid(mdstat->devnm)) {
5321 for (retry = 0; retry < 3; retry++) {
5322 usleep(3000);
5323 rv = load_and_parse_mpb(fd, super, devname, 0);
5324 if (rv != 3)
5325 break;
5326 }
5327 }
5328
5329 free_mdstat(mdstat);
5330 }
5331
5332 if (rv) {
5333 if (devname)
5334 pr_err("Failed to load all information sections on %s\n", devname);
5335 free_imsm(super);
5336 return rv;
5337 }
5338
5339 st->sb = super;
5340 if (st->ss == NULL) {
5341 st->ss = &super_imsm;
5342 st->minor_version = 0;
5343 st->max_devs = IMSM_MAX_DEVICES;
5344 }
5345
5346 /* load migration record */
5347 if (load_imsm_migr_rec(super) == 0) {
5348 /* Check for unsupported migration features */
5349 if (check_mpb_migr_compatibility(super) != 0) {
5350 pr_err("Unsupported migration detected");
5351 if (devname)
5352 fprintf(stderr, " on %s\n", devname);
5353 else
5354 fprintf(stderr, " (IMSM).\n");
5355 return 3;
5356 }
5357 }
5358
5359 return 0;
5360 }
5361
5362 static __u16 info_to_blocks_per_strip(mdu_array_info_t *info)
5363 {
5364 if (info->level == 1)
5365 return 128;
5366 return info->chunk_size >> 9;
5367 }
5368
5369 static unsigned long long info_to_blocks_per_member(mdu_array_info_t *info,
5370 unsigned long long size)
5371 {
5372 if (info->level == 1)
5373 return size * 2;
5374 else
5375 return (size * 2) & ~(info_to_blocks_per_strip(info) - 1);
5376 }
5377
5378 static void imsm_update_version_info(struct intel_super *super)
5379 {
5380 /* update the version and attributes */
5381 struct imsm_super *mpb = super->anchor;
5382 char *version;
5383 struct imsm_dev *dev;
5384 struct imsm_map *map;
5385 int i;
5386
5387 for (i = 0; i < mpb->num_raid_devs; i++) {
5388 dev = get_imsm_dev(super, i);
5389 map = get_imsm_map(dev, MAP_0);
5390 if (__le32_to_cpu(dev->size_high) > 0)
5391 mpb->attributes |= MPB_ATTRIB_2TB;
5392
5393 /* FIXME detect when an array spans a port multiplier */
5394 #if 0
5395 mpb->attributes |= MPB_ATTRIB_PM;
5396 #endif
5397
5398 if (mpb->num_raid_devs > 1 ||
5399 mpb->attributes != MPB_ATTRIB_CHECKSUM_VERIFY) {
5400 version = MPB_VERSION_ATTRIBS;
5401 switch (get_imsm_raid_level(map)) {
5402 case 0: mpb->attributes |= MPB_ATTRIB_RAID0; break;
5403 case 1: mpb->attributes |= MPB_ATTRIB_RAID1; break;
5404 case 10: mpb->attributes |= MPB_ATTRIB_RAID10; break;
5405 case 5: mpb->attributes |= MPB_ATTRIB_RAID5; break;
5406 }
5407 } else {
5408 if (map->num_members >= 5)
5409 version = MPB_VERSION_5OR6_DISK_ARRAY;
5410 else if (dev->status == DEV_CLONE_N_GO)
5411 version = MPB_VERSION_CNG;
5412 else if (get_imsm_raid_level(map) == 5)
5413 version = MPB_VERSION_RAID5;
5414 else if (map->num_members >= 3)
5415 version = MPB_VERSION_3OR4_DISK_ARRAY;
5416 else if (get_imsm_raid_level(map) == 1)
5417 version = MPB_VERSION_RAID1;
5418 else
5419 version = MPB_VERSION_RAID0;
5420 }
5421 strcpy(((char *) mpb->sig) + strlen(MPB_SIGNATURE), version);
5422 }
5423 }
5424
5425 static int check_name(struct intel_super *super, char *name, int quiet)
5426 {
5427 struct imsm_super *mpb = super->anchor;
5428 char *reason = NULL;
5429 char *start = name;
5430 size_t len = strlen(name);
5431 int i;
5432
5433 if (len > 0) {
5434 while (isspace(start[len - 1]))
5435 start[--len] = 0;
5436 while (*start && isspace(*start))
5437 ++start, --len;
5438 memmove(name, start, len + 1);
5439 }
5440
5441 if (len > MAX_RAID_SERIAL_LEN)
5442 reason = "must be 16 characters or less";
5443 else if (len == 0)
5444 reason = "must be a non-empty string";
5445
5446 for (i = 0; i < mpb->num_raid_devs; i++) {
5447 struct imsm_dev *dev = get_imsm_dev(super, i);
5448
5449 if (strncmp((char *) dev->volume, name, MAX_RAID_SERIAL_LEN) == 0) {
5450 reason = "already exists";
5451 break;
5452 }
5453 }
5454
5455 if (reason && !quiet)
5456 pr_err("imsm volume name %s\n", reason);
5457
5458 return !reason;
5459 }
5460
5461 static int init_super_imsm_volume(struct supertype *st, mdu_array_info_t *info,
5462 struct shape *s, char *name,
5463 char *homehost, int *uuid,
5464 long long data_offset)
5465 {
5466 /* We are creating a volume inside a pre-existing container.
5467 * so st->sb is already set.
5468 */
5469 struct intel_super *super = st->sb;
5470 unsigned int sector_size = super->sector_size;
5471 struct imsm_super *mpb = super->anchor;
5472 struct intel_dev *dv;
5473 struct imsm_dev *dev;
5474 struct imsm_vol *vol;
5475 struct imsm_map *map;
5476 int idx = mpb->num_raid_devs;
5477 int i;
5478 int namelen;
5479 unsigned long long array_blocks;
5480 size_t size_old, size_new;
5481 unsigned long long num_data_stripes;
5482 unsigned int data_disks;
5483 unsigned long long size_per_member;
5484
5485 if (super->orom && mpb->num_raid_devs >= super->orom->vpa) {
5486 pr_err("This imsm-container already has the maximum of %d volumes\n", super->orom->vpa);
5487 return 0;
5488 }
5489
5490 /* ensure the mpb is large enough for the new data */
5491 size_old = __le32_to_cpu(mpb->mpb_size);
5492 size_new = disks_to_mpb_size(info->nr_disks);
5493 if (size_new > size_old) {
5494 void *mpb_new;
5495 size_t size_round = ROUND_UP(size_new, sector_size);
5496
5497 if (posix_memalign(&mpb_new, sector_size, size_round) != 0) {
5498 pr_err("could not allocate new mpb\n");
5499 return 0;
5500 }
5501 if (posix_memalign(&super->migr_rec_buf, MAX_SECTOR_SIZE,
5502 MIGR_REC_BUF_SECTORS*
5503 MAX_SECTOR_SIZE) != 0) {
5504 pr_err("could not allocate migr_rec buffer\n");
5505 free(super->buf);
5506 free(super);
5507 free(mpb_new);
5508 return 0;
5509 }
5510 memcpy(mpb_new, mpb, size_old);
5511 free(mpb);
5512 mpb = mpb_new;
5513 super->anchor = mpb_new;
5514 mpb->mpb_size = __cpu_to_le32(size_new);
5515 memset(mpb_new + size_old, 0, size_round - size_old);
5516 super->len = size_round;
5517 }
5518 super->current_vol = idx;
5519
5520 /* handle 'failed_disks' by either:
5521 * a) create dummy disk entries in the table if this the first
5522 * volume in the array. We add them here as this is the only
5523 * opportunity to add them. add_to_super_imsm_volume()
5524 * handles the non-failed disks and continues incrementing
5525 * mpb->num_disks.
5526 * b) validate that 'failed_disks' matches the current number
5527 * of missing disks if the container is populated
5528 */
5529 if (super->current_vol == 0) {
5530 mpb->num_disks = 0;
5531 for (i = 0; i < info->failed_disks; i++) {
5532 struct imsm_disk *disk;
5533
5534 mpb->num_disks++;
5535 disk = __get_imsm_disk(mpb, i);
5536 disk->status = CONFIGURED_DISK | FAILED_DISK;
5537 disk->scsi_id = __cpu_to_le32(~(__u32)0);
5538 snprintf((char *) disk->serial, MAX_RAID_SERIAL_LEN,
5539 "missing:%d", (__u8)i);
5540 }
5541 find_missing(super);
5542 } else {
5543 int missing = 0;
5544 struct dl *d;
5545
5546 for (d = super->missing; d; d = d->next)
5547 missing++;
5548 if (info->failed_disks > missing) {
5549 pr_err("unable to add 'missing' disk to container\n");
5550 return 0;
5551 }
5552 }
5553
5554 if (!check_name(super, name, 0))
5555 return 0;
5556 dv = xmalloc(sizeof(*dv));
5557 dev = xcalloc(1, sizeof(*dev) + sizeof(__u32) * (info->raid_disks - 1));
5558 /*
5559 * Explicitly allow truncating to not confuse gcc's
5560 * -Werror=stringop-truncation
5561 */
5562 namelen = min((int) strlen(name), MAX_RAID_SERIAL_LEN);
5563 memcpy(dev->volume, name, namelen);
5564 array_blocks = calc_array_size(info->level, info->raid_disks,
5565 info->layout, info->chunk_size,
5566 s->size * BLOCKS_PER_KB);
5567 data_disks = get_data_disks(info->level, info->layout,
5568 info->raid_disks);
5569 array_blocks = round_size_to_mb(array_blocks, data_disks);
5570 size_per_member = array_blocks / data_disks;
5571
5572 set_imsm_dev_size(dev, array_blocks);
5573 dev->status = (DEV_READ_COALESCING | DEV_WRITE_COALESCING);
5574 vol = &dev->vol;
5575 vol->migr_state = 0;
5576 set_migr_type(dev, MIGR_INIT);
5577 vol->dirty = !info->state;
5578 set_vol_curr_migr_unit(dev, 0);
5579 map = get_imsm_map(dev, MAP_0);
5580 set_pba_of_lba0(map, super->create_offset);
5581 map->blocks_per_strip = __cpu_to_le16(info_to_blocks_per_strip(info));
5582 map->failed_disk_num = ~0;
5583 if (info->level > 0)
5584 map->map_state = (info->state ? IMSM_T_STATE_NORMAL
5585 : IMSM_T_STATE_UNINITIALIZED);
5586 else
5587 map->map_state = info->failed_disks ? IMSM_T_STATE_FAILED :
5588 IMSM_T_STATE_NORMAL;
5589 map->ddf = 1;
5590
5591 if (info->level == 1 && info->raid_disks > 2) {
5592 free(dev);
5593 free(dv);
5594 pr_err("imsm does not support more than 2 disksin a raid1 volume\n");
5595 return 0;
5596 }
5597
5598 map->raid_level = info->level;
5599 if (info->level == 10) {
5600 map->raid_level = 1;
5601 map->num_domains = info->raid_disks / 2;
5602 } else if (info->level == 1)
5603 map->num_domains = info->raid_disks;
5604 else
5605 map->num_domains = 1;
5606
5607 /* info->size is only int so use the 'size' parameter instead */
5608 num_data_stripes = size_per_member / info_to_blocks_per_strip(info);
5609 num_data_stripes /= map->num_domains;
5610 set_num_data_stripes(map, num_data_stripes);
5611
5612 size_per_member += NUM_BLOCKS_DIRTY_STRIPE_REGION;
5613 set_blocks_per_member(map, info_to_blocks_per_member(info,
5614 size_per_member /
5615 BLOCKS_PER_KB));
5616
5617 map->num_members = info->raid_disks;
5618 for (i = 0; i < map->num_members; i++) {
5619 /* initialized in add_to_super */
5620 set_imsm_ord_tbl_ent(map, i, IMSM_ORD_REBUILD);
5621 }
5622 mpb->num_raid_devs++;
5623 mpb->num_raid_devs_created++;
5624 dev->my_vol_raid_dev_num = mpb->num_raid_devs_created;
5625
5626 if (s->consistency_policy <= CONSISTENCY_POLICY_RESYNC) {
5627 dev->rwh_policy = RWH_MULTIPLE_OFF;
5628 } else if (s->consistency_policy == CONSISTENCY_POLICY_PPL) {
5629 dev->rwh_policy = RWH_MULTIPLE_DISTRIBUTED;
5630 } else {
5631 free(dev);
5632 free(dv);
5633 pr_err("imsm does not support consistency policy %s\n",
5634 map_num(consistency_policies, s->consistency_policy));
5635 return 0;
5636 }
5637
5638 dv->dev = dev;
5639 dv->index = super->current_vol;
5640 dv->next = super->devlist;
5641 super->devlist = dv;
5642
5643 imsm_update_version_info(super);
5644
5645 return 1;
5646 }
5647
5648 static int init_super_imsm(struct supertype *st, mdu_array_info_t *info,
5649 struct shape *s, char *name,
5650 char *homehost, int *uuid,
5651 unsigned long long data_offset)
5652 {
5653 /* This is primarily called by Create when creating a new array.
5654 * We will then get add_to_super called for each component, and then
5655 * write_init_super called to write it out to each device.
5656 * For IMSM, Create can create on fresh devices or on a pre-existing
5657 * array.
5658 * To create on a pre-existing array a different method will be called.
5659 * This one is just for fresh drives.
5660 */
5661 struct intel_super *super;
5662 struct imsm_super *mpb;
5663 size_t mpb_size;
5664 char *version;
5665
5666 if (data_offset != INVALID_SECTORS) {
5667 pr_err("data-offset not supported by imsm\n");
5668 return 0;
5669 }
5670
5671 if (st->sb)
5672 return init_super_imsm_volume(st, info, s, name, homehost, uuid,
5673 data_offset);
5674
5675 if (info)
5676 mpb_size = disks_to_mpb_size(info->nr_disks);
5677 else
5678 mpb_size = MAX_SECTOR_SIZE;
5679
5680 super = alloc_super();
5681 if (super &&
5682 posix_memalign(&super->buf, MAX_SECTOR_SIZE, mpb_size) != 0) {
5683 free_imsm(super);
5684 super = NULL;
5685 }
5686 if (!super) {
5687 pr_err("could not allocate superblock\n");
5688 return 0;
5689 }
5690 if (posix_memalign(&super->migr_rec_buf, MAX_SECTOR_SIZE,
5691 MIGR_REC_BUF_SECTORS*MAX_SECTOR_SIZE) != 0) {
5692 pr_err("could not allocate migr_rec buffer\n");
5693 free(super->buf);
5694 free_imsm(super);
5695 return 0;
5696 }
5697 memset(super->buf, 0, mpb_size);
5698 mpb = super->buf;
5699 mpb->mpb_size = __cpu_to_le32(mpb_size);
5700 st->sb = super;
5701
5702 if (info == NULL) {
5703 /* zeroing superblock */
5704 return 0;
5705 }
5706
5707 mpb->attributes = MPB_ATTRIB_CHECKSUM_VERIFY;
5708
5709 version = (char *) mpb->sig;
5710 strcpy(version, MPB_SIGNATURE);
5711 version += strlen(MPB_SIGNATURE);
5712 strcpy(version, MPB_VERSION_RAID0);
5713
5714 return 1;
5715 }
5716
5717 static int drive_validate_sector_size(struct intel_super *super, struct dl *dl)
5718 {
5719 unsigned int member_sector_size;
5720
5721 if (dl->fd < 0) {
5722 pr_err("Invalid file descriptor for %s\n", dl->devname);
5723 return 0;
5724 }
5725
5726 if (!get_dev_sector_size(dl->fd, dl->devname, &member_sector_size))
5727 return 0;
5728 if (member_sector_size != super->sector_size)
5729 return 0;
5730 return 1;
5731 }
5732
5733 static int add_to_super_imsm_volume(struct supertype *st, mdu_disk_info_t *dk,
5734 int fd, char *devname)
5735 {
5736 struct intel_super *super = st->sb;
5737 struct imsm_super *mpb = super->anchor;
5738 struct imsm_disk *_disk;
5739 struct imsm_dev *dev;
5740 struct imsm_map *map;
5741 struct dl *dl, *df;
5742 int slot;
5743
5744 dev = get_imsm_dev(super, super->current_vol);
5745 map = get_imsm_map(dev, MAP_0);
5746
5747 if (! (dk->state & (1<<MD_DISK_SYNC))) {
5748 pr_err("%s: Cannot add spare devices to IMSM volume\n",
5749 devname);
5750 return 1;
5751 }
5752
5753 if (fd == -1) {
5754 /* we're doing autolayout so grab the pre-marked (in
5755 * validate_geometry) raid_disk
5756 */
5757 for (dl = super->disks; dl; dl = dl->next)
5758 if (dl->raiddisk == dk->raid_disk)
5759 break;
5760 } else {
5761 for (dl = super->disks; dl ; dl = dl->next)
5762 if (dl->major == dk->major &&
5763 dl->minor == dk->minor)
5764 break;
5765 }
5766
5767 if (!dl) {
5768 pr_err("%s is not a member of the same container\n", devname);
5769 return 1;
5770 }
5771
5772 if (mpb->num_disks == 0)
5773 if (!get_dev_sector_size(dl->fd, dl->devname,
5774 &super->sector_size))
5775 return 1;
5776
5777 if (!drive_validate_sector_size(super, dl)) {
5778 pr_err("Combining drives of different sector size in one volume is not allowed\n");
5779 return 1;
5780 }
5781
5782 /* add a pristine spare to the metadata */
5783 if (dl->index < 0) {
5784 dl->index = super->anchor->num_disks;
5785 super->anchor->num_disks++;
5786 }
5787 /* Check the device has not already been added */
5788 slot = get_imsm_disk_slot(map, dl->index);
5789 if (slot >= 0 &&
5790 (get_imsm_ord_tbl_ent(dev, slot, MAP_X) & IMSM_ORD_REBUILD) == 0) {
5791 pr_err("%s has been included in this array twice\n",
5792 devname);
5793 return 1;
5794 }
5795 set_imsm_ord_tbl_ent(map, dk->raid_disk, dl->index);
5796 dl->disk.status = CONFIGURED_DISK;
5797
5798 /* update size of 'missing' disks to be at least as large as the
5799 * largest acitve member (we only have dummy missing disks when
5800 * creating the first volume)
5801 */
5802 if (super->current_vol == 0) {
5803 for (df = super->missing; df; df = df->next) {
5804 if (total_blocks(&dl->disk) > total_blocks(&df->disk))
5805 set_total_blocks(&df->disk, total_blocks(&dl->disk));
5806 _disk = __get_imsm_disk(mpb, df->index);
5807 *_disk = df->disk;
5808 }
5809 }
5810
5811 /* refresh unset/failed slots to point to valid 'missing' entries */
5812 for (df = super->missing; df; df = df->next)
5813 for (slot = 0; slot < mpb->num_disks; slot++) {
5814 __u32 ord = get_imsm_ord_tbl_ent(dev, slot, MAP_X);
5815
5816 if ((ord & IMSM_ORD_REBUILD) == 0)
5817 continue;
5818 set_imsm_ord_tbl_ent(map, slot, df->index | IMSM_ORD_REBUILD);
5819 if (is_gen_migration(dev)) {
5820 struct imsm_map *map2 = get_imsm_map(dev,
5821 MAP_1);
5822 int slot2 = get_imsm_disk_slot(map2, df->index);
5823 if (slot2 < map2->num_members && slot2 >= 0) {
5824 __u32 ord2 = get_imsm_ord_tbl_ent(dev,
5825 slot2,
5826 MAP_1);
5827 if ((unsigned)df->index ==
5828 ord_to_idx(ord2))
5829 set_imsm_ord_tbl_ent(map2,
5830 slot2,
5831 df->index |
5832 IMSM_ORD_REBUILD);
5833 }
5834 }
5835 dprintf("set slot:%d to missing disk:%d\n", slot, df->index);
5836 break;
5837 }
5838
5839 /* if we are creating the first raid device update the family number */
5840 if (super->current_vol == 0) {
5841 __u32 sum;
5842 struct imsm_dev *_dev = __get_imsm_dev(mpb, 0);
5843
5844 _disk = __get_imsm_disk(mpb, dl->index);
5845 if (!_dev || !_disk) {
5846 pr_err("BUG mpb setup error\n");
5847 return 1;
5848 }
5849 *_dev = *dev;
5850 *_disk = dl->disk;
5851 sum = random32();
5852 sum += __gen_imsm_checksum(mpb);
5853 mpb->family_num = __cpu_to_le32(sum);
5854 mpb->orig_family_num = mpb->family_num;
5855 mpb->creation_time = __cpu_to_le64((__u64)time(NULL));
5856 }
5857 super->current_disk = dl;
5858 return 0;
5859 }
5860
5861 /* mark_spare()
5862 * Function marks disk as spare and restores disk serial
5863 * in case it was previously marked as failed by takeover operation
5864 * reruns:
5865 * -1 : critical error
5866 * 0 : disk is marked as spare but serial is not set
5867 * 1 : success
5868 */
5869 int mark_spare(struct dl *disk)
5870 {
5871 __u8 serial[MAX_RAID_SERIAL_LEN];
5872 int ret_val = -1;
5873
5874 if (!disk)
5875 return ret_val;
5876
5877 ret_val = 0;
5878 if (!imsm_read_serial(disk->fd, NULL, serial, MAX_RAID_SERIAL_LEN)) {
5879 /* Restore disk serial number, because takeover marks disk
5880 * as failed and adds to serial ':0' before it becomes
5881 * a spare disk.
5882 */
5883 serialcpy(disk->serial, serial);
5884 serialcpy(disk->disk.serial, serial);
5885 ret_val = 1;
5886 }
5887 disk->disk.status = SPARE_DISK;
5888 disk->index = -1;
5889
5890 return ret_val;
5891 }
5892
5893
5894 static int write_super_imsm_spare(struct intel_super *super, struct dl *d);
5895
5896 static int add_to_super_imsm(struct supertype *st, mdu_disk_info_t *dk,
5897 int fd, char *devname,
5898 unsigned long long data_offset)
5899 {
5900 struct intel_super *super = st->sb;
5901 struct dl *dd;
5902 unsigned long long size;
5903 unsigned int member_sector_size;
5904 __u32 id;
5905 int rv;
5906 struct stat stb;
5907
5908 /* If we are on an RAID enabled platform check that the disk is
5909 * attached to the raid controller.
5910 * We do not need to test disks attachment for container based additions,
5911 * they shall be already tested when container was created/assembled.
5912 */
5913 rv = find_intel_hba_capability(fd, super, devname);
5914 /* no orom/efi or non-intel hba of the disk */
5915 if (rv != 0) {
5916 dprintf("capability: %p fd: %d ret: %d\n",
5917 super->orom, fd, rv);
5918 return 1;
5919 }
5920
5921 if (super->current_vol >= 0)
5922 return add_to_super_imsm_volume(st, dk, fd, devname);
5923
5924 fstat(fd, &stb);
5925 dd = xcalloc(sizeof(*dd), 1);
5926 dd->major = major(stb.st_rdev);
5927 dd->minor = minor(stb.st_rdev);
5928 dd->devname = devname ? xstrdup(devname) : NULL;
5929 dd->fd = fd;
5930 dd->e = NULL;
5931 dd->action = DISK_ADD;
5932 rv = imsm_read_serial(fd, devname, dd->serial, MAX_RAID_SERIAL_LEN);
5933 if (rv) {
5934 pr_err("failed to retrieve scsi serial, aborting\n");
5935 if (dd->devname)
5936 free(dd->devname);
5937 free(dd);
5938 abort();
5939 }
5940 if (super->hba && ((super->hba->type == SYS_DEV_NVME) ||
5941 (super->hba->type == SYS_DEV_VMD))) {
5942 int i;
5943 char *devpath = diskfd_to_devpath(fd);
5944 char controller_path[PATH_MAX];
5945 char *controller_name;
5946
5947 if (!devpath) {
5948 pr_err("failed to get devpath, aborting\n");
5949 if (dd->devname)
5950 free(dd->devname);
5951 free(dd);
5952 return 1;
5953 }
5954
5955 snprintf(controller_path, PATH_MAX-1, "%s/device", devpath);
5956
5957 controller_name = basename(devpath);
5958 if (is_multipath_nvme(fd))
5959 pr_err("%s controller supports Multi-Path I/O, Intel (R) VROC does not support multipathing\n", controller_name);
5960
5961 free(devpath);
5962
5963 if (!imsm_is_nvme_supported(dd->fd, 1)) {
5964 if (dd->devname)
5965 free(dd->devname);
5966 free(dd);
5967 return 1;
5968 }
5969
5970 if (devpath_to_vendor(controller_path) == 0x8086) {
5971 /*
5972 * If Intel's NVMe drive has serial ended with
5973 * "-A","-B","-1" or "-2" it means that this is "x8"
5974 * device (double drive on single PCIe card).
5975 * User should be warned about potential data loss.
5976 */
5977 for (i = MAX_RAID_SERIAL_LEN-1; i > 0; i--) {
5978 /* Skip empty character at the end */
5979 if (dd->serial[i] == 0)
5980 continue;
5981
5982 if (((dd->serial[i] == 'A') ||
5983 (dd->serial[i] == 'B') ||
5984 (dd->serial[i] == '1') ||
5985 (dd->serial[i] == '2')) &&
5986 (dd->serial[i-1] == '-'))
5987 pr_err("\tThe action you are about to take may put your data at risk.\n"
5988 "\tPlease note that x8 devices may consist of two separate x4 devices "
5989 "located on a single PCIe port.\n"
5990 "\tRAID 0 is the only supported configuration for this type of x8 device.\n");
5991 break;
5992 }
5993 } else if (super->hba->type == SYS_DEV_VMD && super->orom &&
5994 !imsm_orom_has_tpv_support(super->orom)) {
5995 pr_err("\tPlatform configuration does not support non-Intel NVMe drives.\n"
5996 "\tPlease refer to Intel(R) RSTe/VROC user guide.\n");
5997 free(dd->devname);
5998 free(dd);
5999 return 1;
6000 }
6001 }
6002
6003 get_dev_size(fd, NULL, &size);
6004 get_dev_sector_size(fd, NULL, &member_sector_size);
6005
6006 if (super->sector_size == 0) {
6007 /* this a first device, so sector_size is not set yet */
6008 super->sector_size = member_sector_size;
6009 }
6010
6011 /* clear migr_rec when adding disk to container */
6012 memset(super->migr_rec_buf, 0, MIGR_REC_BUF_SECTORS*MAX_SECTOR_SIZE);
6013 if (lseek64(fd, size - MIGR_REC_SECTOR_POSITION*member_sector_size,
6014 SEEK_SET) >= 0) {
6015 if ((unsigned int)write(fd, super->migr_rec_buf,
6016 MIGR_REC_BUF_SECTORS*member_sector_size) !=
6017 MIGR_REC_BUF_SECTORS*member_sector_size)
6018 perror("Write migr_rec failed");
6019 }
6020
6021 size /= 512;
6022 serialcpy(dd->disk.serial, dd->serial);
6023 set_total_blocks(&dd->disk, size);
6024 if (__le32_to_cpu(dd->disk.total_blocks_hi) > 0) {
6025 struct imsm_super *mpb = super->anchor;
6026 mpb->attributes |= MPB_ATTRIB_2TB_DISK;
6027 }
6028 mark_spare(dd);
6029 if (sysfs_disk_to_scsi_id(fd, &id) == 0)
6030 dd->disk.scsi_id = __cpu_to_le32(id);
6031 else
6032 dd->disk.scsi_id = __cpu_to_le32(0);
6033
6034 if (st->update_tail) {
6035 dd->next = super->disk_mgmt_list;
6036 super->disk_mgmt_list = dd;
6037 } else {
6038 /* this is called outside of mdmon
6039 * write initial spare metadata
6040 * mdmon will overwrite it.
6041 */
6042 dd->next = super->disks;
6043 super->disks = dd;
6044 write_super_imsm_spare(super, dd);
6045 }
6046
6047 return 0;
6048 }
6049
6050 static int remove_from_super_imsm(struct supertype *st, mdu_disk_info_t *dk)
6051 {
6052 struct intel_super *super = st->sb;
6053 struct dl *dd;
6054
6055 /* remove from super works only in mdmon - for communication
6056 * manager - monitor. Check if communication memory buffer
6057 * is prepared.
6058 */
6059 if (!st->update_tail) {
6060 pr_err("shall be used in mdmon context only\n");
6061 return 1;
6062 }
6063 dd = xcalloc(1, sizeof(*dd));
6064 dd->major = dk->major;
6065 dd->minor = dk->minor;
6066 dd->fd = -1;
6067 mark_spare(dd);
6068 dd->action = DISK_REMOVE;
6069
6070 dd->next = super->disk_mgmt_list;
6071 super->disk_mgmt_list = dd;
6072
6073 return 0;
6074 }
6075
6076 static int store_imsm_mpb(int fd, struct imsm_super *mpb);
6077
6078 static union {
6079 char buf[MAX_SECTOR_SIZE];
6080 struct imsm_super anchor;
6081 } spare_record __attribute__ ((aligned(MAX_SECTOR_SIZE)));
6082
6083
6084 static int write_super_imsm_spare(struct intel_super *super, struct dl *d)
6085 {
6086 struct imsm_super *mpb = super->anchor;
6087 struct imsm_super *spare = &spare_record.anchor;
6088 __u32 sum;
6089
6090 if (d->index != -1)
6091 return 1;
6092
6093 spare->mpb_size = __cpu_to_le32(sizeof(struct imsm_super));
6094 spare->generation_num = __cpu_to_le32(1UL);
6095 spare->attributes = MPB_ATTRIB_CHECKSUM_VERIFY;
6096 spare->num_disks = 1;
6097 spare->num_raid_devs = 0;
6098 spare->cache_size = mpb->cache_size;
6099 spare->pwr_cycle_count = __cpu_to_le32(1);
6100
6101 snprintf((char *) spare->sig, MAX_SIGNATURE_LENGTH,
6102 MPB_SIGNATURE MPB_VERSION_RAID0);
6103
6104 spare->disk[0] = d->disk;
6105 if (__le32_to_cpu(d->disk.total_blocks_hi) > 0)
6106 spare->attributes |= MPB_ATTRIB_2TB_DISK;
6107
6108 if (super->sector_size == 4096)
6109 convert_to_4k_imsm_disk(&spare->disk[0]);
6110
6111 sum = __gen_imsm_checksum(spare);
6112 spare->family_num = __cpu_to_le32(sum);
6113 spare->orig_family_num = 0;
6114 sum = __gen_imsm_checksum(spare);
6115 spare->check_sum = __cpu_to_le32(sum);
6116
6117 if (store_imsm_mpb(d->fd, spare)) {
6118 pr_err("failed for device %d:%d %s\n",
6119 d->major, d->minor, strerror(errno));
6120 return 1;
6121 }
6122
6123 return 0;
6124 }
6125 /* spare records have their own family number and do not have any defined raid
6126 * devices
6127 */
6128 static int write_super_imsm_spares(struct intel_super *super, int doclose)
6129 {
6130 struct dl *d;
6131
6132 for (d = super->disks; d; d = d->next) {
6133 if (d->index != -1)
6134 continue;
6135
6136 if (write_super_imsm_spare(super, d))
6137 return 1;
6138
6139 if (doclose) {
6140 close(d->fd);
6141 d->fd = -1;
6142 }
6143 }
6144
6145 return 0;
6146 }
6147
6148 static int write_super_imsm(struct supertype *st, int doclose)
6149 {
6150 struct intel_super *super = st->sb;
6151 unsigned int sector_size = super->sector_size;
6152 struct imsm_super *mpb = super->anchor;
6153 struct dl *d;
6154 __u32 generation;
6155 __u32 sum;
6156 int spares = 0;
6157 int i;
6158 __u32 mpb_size = sizeof(struct imsm_super) - sizeof(struct imsm_disk);
6159 int num_disks = 0;
6160 int clear_migration_record = 1;
6161 __u32 bbm_log_size;
6162
6163 /* 'generation' is incremented everytime the metadata is written */
6164 generation = __le32_to_cpu(mpb->generation_num);
6165 generation++;
6166 mpb->generation_num = __cpu_to_le32(generation);
6167
6168 /* fix up cases where previous mdadm releases failed to set
6169 * orig_family_num
6170 */
6171 if (mpb->orig_family_num == 0)
6172 mpb->orig_family_num = mpb->family_num;
6173
6174 for (d = super->disks; d; d = d->next) {
6175 if (d->index == -1)
6176 spares++;
6177 else {
6178 mpb->disk[d->index] = d->disk;
6179 num_disks++;
6180 }
6181 }
6182 for (d = super->missing; d; d = d->next) {
6183 mpb->disk[d->index] = d->disk;
6184 num_disks++;
6185 }
6186 mpb->num_disks = num_disks;
6187 mpb_size += sizeof(struct imsm_disk) * mpb->num_disks;
6188
6189 for (i = 0; i < mpb->num_raid_devs; i++) {
6190 struct imsm_dev *dev = __get_imsm_dev(mpb, i);
6191 struct imsm_dev *dev2 = get_imsm_dev(super, i);
6192 if (dev && dev2) {
6193 imsm_copy_dev(dev, dev2);
6194 mpb_size += sizeof_imsm_dev(dev, 0);
6195 }
6196 if (is_gen_migration(dev2))
6197 clear_migration_record = 0;
6198 }
6199
6200 bbm_log_size = get_imsm_bbm_log_size(super->bbm_log);
6201
6202 if (bbm_log_size) {
6203 memcpy((void *)mpb + mpb_size, super->bbm_log, bbm_log_size);
6204 mpb->attributes |= MPB_ATTRIB_BBM;
6205 } else
6206 mpb->attributes &= ~MPB_ATTRIB_BBM;
6207
6208 super->anchor->bbm_log_size = __cpu_to_le32(bbm_log_size);
6209 mpb_size += bbm_log_size;
6210 mpb->mpb_size = __cpu_to_le32(mpb_size);
6211
6212 #ifdef DEBUG
6213 assert(super->len == 0 || mpb_size <= super->len);
6214 #endif
6215
6216 /* recalculate checksum */
6217 sum = __gen_imsm_checksum(mpb);
6218 mpb->check_sum = __cpu_to_le32(sum);
6219
6220 if (super->clean_migration_record_by_mdmon) {
6221 clear_migration_record = 1;
6222 super->clean_migration_record_by_mdmon = 0;
6223 }
6224 if (clear_migration_record)
6225 memset(super->migr_rec_buf, 0,
6226 MIGR_REC_BUF_SECTORS*MAX_SECTOR_SIZE);
6227
6228 if (sector_size == 4096)
6229 convert_to_4k(super);
6230
6231 /* write the mpb for disks that compose raid devices */
6232 for (d = super->disks; d ; d = d->next) {
6233 if (d->index < 0 || is_failed(&d->disk))
6234 continue;
6235
6236 if (clear_migration_record) {
6237 unsigned long long dsize;
6238
6239 get_dev_size(d->fd, NULL, &dsize);
6240 if (lseek64(d->fd, dsize - sector_size,
6241 SEEK_SET) >= 0) {
6242 if ((unsigned int)write(d->fd,
6243 super->migr_rec_buf,
6244 MIGR_REC_BUF_SECTORS*sector_size) !=
6245 MIGR_REC_BUF_SECTORS*sector_size)
6246 perror("Write migr_rec failed");
6247 }
6248 }
6249
6250 if (store_imsm_mpb(d->fd, mpb))
6251 fprintf(stderr,
6252 "failed for device %d:%d (fd: %d)%s\n",
6253 d->major, d->minor,
6254 d->fd, strerror(errno));
6255
6256 if (doclose) {
6257 close(d->fd);
6258 d->fd = -1;
6259 }
6260 }
6261
6262 if (spares)
6263 return write_super_imsm_spares(super, doclose);
6264
6265 return 0;
6266 }
6267
6268 static int create_array(struct supertype *st, int dev_idx)
6269 {
6270 size_t len;
6271 struct imsm_update_create_array *u;
6272 struct intel_super *super = st->sb;
6273 struct imsm_dev *dev = get_imsm_dev(super, dev_idx);
6274 struct imsm_map *map = get_imsm_map(dev, MAP_0);
6275 struct disk_info *inf;
6276 struct imsm_disk *disk;
6277 int i;
6278
6279 len = sizeof(*u) - sizeof(*dev) + sizeof_imsm_dev(dev, 0) +
6280 sizeof(*inf) * map->num_members;
6281 u = xmalloc(len);
6282 u->type = update_create_array;
6283 u->dev_idx = dev_idx;
6284 imsm_copy_dev(&u->dev, dev);
6285 inf = get_disk_info(u);
6286 for (i = 0; i < map->num_members; i++) {
6287 int idx = get_imsm_disk_idx(dev, i, MAP_X);
6288
6289 disk = get_imsm_disk(super, idx);
6290 if (!disk)
6291 disk = get_imsm_missing(super, idx);
6292 serialcpy(inf[i].serial, disk->serial);
6293 }
6294 append_metadata_update(st, u, len);
6295
6296 return 0;
6297 }
6298
6299 static int mgmt_disk(struct supertype *st)
6300 {
6301 struct intel_super *super = st->sb;
6302 size_t len;
6303 struct imsm_update_add_remove_disk *u;
6304
6305 if (!super->disk_mgmt_list)
6306 return 0;
6307
6308 len = sizeof(*u);
6309 u = xmalloc(len);
6310 u->type = update_add_remove_disk;
6311 append_metadata_update(st, u, len);
6312
6313 return 0;
6314 }
6315
6316 __u32 crc32c_le(__u32 crc, unsigned char const *p, size_t len);
6317
6318 static int write_ppl_header(unsigned long long ppl_sector, int fd, void *buf)
6319 {
6320 struct ppl_header *ppl_hdr = buf;
6321 int ret;
6322
6323 ppl_hdr->checksum = __cpu_to_le32(~crc32c_le(~0, buf, PPL_HEADER_SIZE));
6324
6325 if (lseek64(fd, ppl_sector * 512, SEEK_SET) < 0) {
6326 ret = -errno;
6327 perror("Failed to seek to PPL header location");
6328 return ret;
6329 }
6330
6331 if (write(fd, buf, PPL_HEADER_SIZE) != PPL_HEADER_SIZE) {
6332 ret = -errno;
6333 perror("Write PPL header failed");
6334 return ret;
6335 }
6336
6337 fsync(fd);
6338
6339 return 0;
6340 }
6341
6342 static int write_init_ppl_imsm(struct supertype *st, struct mdinfo *info, int fd)
6343 {
6344 struct intel_super *super = st->sb;
6345 void *buf;
6346 struct ppl_header *ppl_hdr;
6347 int ret;
6348
6349 /* first clear entire ppl space */
6350 ret = zero_disk_range(fd, info->ppl_sector, info->ppl_size);
6351 if (ret)
6352 return ret;
6353
6354 ret = posix_memalign(&buf, MAX_SECTOR_SIZE, PPL_HEADER_SIZE);
6355 if (ret) {
6356 pr_err("Failed to allocate PPL header buffer\n");
6357 return -ret;
6358 }
6359
6360 memset(buf, 0, PPL_HEADER_SIZE);
6361 ppl_hdr = buf;
6362 memset(ppl_hdr->reserved, 0xff, PPL_HDR_RESERVED);
6363 ppl_hdr->signature = __cpu_to_le32(super->anchor->orig_family_num);
6364
6365 if (info->mismatch_cnt) {
6366 /*
6367 * We are overwriting an invalid ppl. Make one entry with wrong
6368 * checksum to prevent the kernel from skipping resync.
6369 */
6370 ppl_hdr->entries_count = __cpu_to_le32(1);
6371 ppl_hdr->entries[0].checksum = ~0;
6372 }
6373
6374 ret = write_ppl_header(info->ppl_sector, fd, buf);
6375
6376 free(buf);
6377 return ret;
6378 }
6379
6380 static int is_rebuilding(struct imsm_dev *dev);
6381
6382 static int validate_ppl_imsm(struct supertype *st, struct mdinfo *info,
6383 struct mdinfo *disk)
6384 {
6385 struct intel_super *super = st->sb;
6386 struct dl *d;
6387 void *buf_orig, *buf, *buf_prev = NULL;
6388 int ret = 0;
6389 struct ppl_header *ppl_hdr = NULL;
6390 __u32 crc;
6391 struct imsm_dev *dev;
6392 __u32 idx;
6393 unsigned int i;
6394 unsigned long long ppl_offset = 0;
6395 unsigned long long prev_gen_num = 0;
6396
6397 if (disk->disk.raid_disk < 0)
6398 return 0;
6399
6400 dev = get_imsm_dev(super, info->container_member);
6401 idx = get_imsm_disk_idx(dev, disk->disk.raid_disk, MAP_0);
6402 d = get_imsm_dl_disk(super, idx);
6403
6404 if (!d || d->index < 0 || is_failed(&d->disk))
6405 return 0;
6406
6407 if (posix_memalign(&buf_orig, MAX_SECTOR_SIZE, PPL_HEADER_SIZE * 2)) {
6408 pr_err("Failed to allocate PPL header buffer\n");
6409 return -1;
6410 }
6411 buf = buf_orig;
6412
6413 ret = 1;
6414 while (ppl_offset < MULTIPLE_PPL_AREA_SIZE_IMSM) {
6415 void *tmp;
6416
6417 dprintf("Checking potential PPL at offset: %llu\n", ppl_offset);
6418
6419 if (lseek64(d->fd, info->ppl_sector * 512 + ppl_offset,
6420 SEEK_SET) < 0) {
6421 perror("Failed to seek to PPL header location");
6422 ret = -1;
6423 break;
6424 }
6425
6426 if (read(d->fd, buf, PPL_HEADER_SIZE) != PPL_HEADER_SIZE) {
6427 perror("Read PPL header failed");
6428 ret = -1;
6429 break;
6430 }
6431
6432 ppl_hdr = buf;
6433
6434 crc = __le32_to_cpu(ppl_hdr->checksum);
6435 ppl_hdr->checksum = 0;
6436
6437 if (crc != ~crc32c_le(~0, buf, PPL_HEADER_SIZE)) {
6438 dprintf("Wrong PPL header checksum on %s\n",
6439 d->devname);
6440 break;
6441 }
6442
6443 if (prev_gen_num > __le64_to_cpu(ppl_hdr->generation)) {
6444 /* previous was newest, it was already checked */
6445 break;
6446 }
6447
6448 if ((__le32_to_cpu(ppl_hdr->signature) !=
6449 super->anchor->orig_family_num)) {
6450 dprintf("Wrong PPL header signature on %s\n",
6451 d->devname);
6452 ret = 1;
6453 break;
6454 }
6455
6456 ret = 0;
6457 prev_gen_num = __le64_to_cpu(ppl_hdr->generation);
6458
6459 ppl_offset += PPL_HEADER_SIZE;
6460 for (i = 0; i < __le32_to_cpu(ppl_hdr->entries_count); i++)
6461 ppl_offset +=
6462 __le32_to_cpu(ppl_hdr->entries[i].pp_size);
6463
6464 if (!buf_prev)
6465 buf_prev = buf + PPL_HEADER_SIZE;
6466 tmp = buf_prev;
6467 buf_prev = buf;
6468 buf = tmp;
6469 }
6470
6471 if (buf_prev) {
6472 buf = buf_prev;
6473 ppl_hdr = buf_prev;
6474 }
6475
6476 /*
6477 * Update metadata to use mutliple PPLs area (1MB).
6478 * This is done once for all RAID members
6479 */
6480 if (info->consistency_policy == CONSISTENCY_POLICY_PPL &&
6481 info->ppl_size != (MULTIPLE_PPL_AREA_SIZE_IMSM >> 9)) {
6482 char subarray[20];
6483 struct mdinfo *member_dev;
6484
6485 sprintf(subarray, "%d", info->container_member);
6486
6487 if (mdmon_running(st->container_devnm))
6488 st->update_tail = &st->updates;
6489
6490 if (st->ss->update_subarray(st, subarray, "ppl", NULL)) {
6491 pr_err("Failed to update subarray %s\n",
6492 subarray);
6493 } else {
6494 if (st->update_tail)
6495 flush_metadata_updates(st);
6496 else
6497 st->ss->sync_metadata(st);
6498 info->ppl_size = (MULTIPLE_PPL_AREA_SIZE_IMSM >> 9);
6499 for (member_dev = info->devs; member_dev;
6500 member_dev = member_dev->next)
6501 member_dev->ppl_size =
6502 (MULTIPLE_PPL_AREA_SIZE_IMSM >> 9);
6503 }
6504 }
6505
6506 if (ret == 1) {
6507 struct imsm_map *map = get_imsm_map(dev, MAP_X);
6508
6509 if (map->map_state == IMSM_T_STATE_UNINITIALIZED ||
6510 (map->map_state == IMSM_T_STATE_NORMAL &&
6511 !(dev->vol.dirty & RAIDVOL_DIRTY)) ||
6512 (is_rebuilding(dev) &&
6513 vol_curr_migr_unit(dev) == 0 &&
6514 get_imsm_disk_idx(dev, disk->disk.raid_disk, MAP_1) != idx))
6515 ret = st->ss->write_init_ppl(st, info, d->fd);
6516 else
6517 info->mismatch_cnt++;
6518 } else if (ret == 0 &&
6519 ppl_hdr->entries_count == 0 &&
6520 is_rebuilding(dev) &&
6521 info->resync_start == 0) {
6522 /*
6523 * The header has no entries - add a single empty entry and
6524 * rewrite the header to prevent the kernel from going into
6525 * resync after an interrupted rebuild.
6526 */
6527 ppl_hdr->entries_count = __cpu_to_le32(1);
6528 ret = write_ppl_header(info->ppl_sector, d->fd, buf);
6529 }
6530
6531 free(buf_orig);
6532
6533 return ret;
6534 }
6535
6536 static int write_init_ppl_imsm_all(struct supertype *st, struct mdinfo *info)
6537 {
6538 struct intel_super *super = st->sb;
6539 struct dl *d;
6540 int ret = 0;
6541
6542 if (info->consistency_policy != CONSISTENCY_POLICY_PPL ||
6543 info->array.level != 5)
6544 return 0;
6545
6546 for (d = super->disks; d ; d = d->next) {
6547 if (d->index < 0 || is_failed(&d->disk))
6548 continue;
6549
6550 ret = st->ss->write_init_ppl(st, info, d->fd);
6551 if (ret)
6552 break;
6553 }
6554
6555 return ret;
6556 }
6557
6558 /*******************************************************************************
6559 * Function: write_init_bitmap_imsm_vol
6560 * Description: Write a bitmap header and prepares the area for the bitmap.
6561 * Parameters:
6562 * st : supertype information
6563 * vol_idx : the volume index to use
6564 *
6565 * Returns:
6566 * 0 : success
6567 * -1 : fail
6568 ******************************************************************************/
6569 static int write_init_bitmap_imsm_vol(struct supertype *st, int vol_idx)
6570 {
6571 struct intel_super *super = st->sb;
6572 int prev_current_vol = super->current_vol;
6573 struct dl *d;
6574 int ret = 0;
6575
6576 super->current_vol = vol_idx;
6577 for (d = super->disks; d; d = d->next) {
6578 if (d->index < 0 || is_failed(&d->disk))
6579 continue;
6580 ret = st->ss->write_bitmap(st, d->fd, NoUpdate);
6581 if (ret)
6582 break;
6583 }
6584 super->current_vol = prev_current_vol;
6585 return ret;
6586 }
6587
6588 /*******************************************************************************
6589 * Function: write_init_bitmap_imsm_all
6590 * Description: Write a bitmap header and prepares the area for the bitmap.
6591 * Operation is executed for volumes with CONSISTENCY_POLICY_BITMAP.
6592 * Parameters:
6593 * st : supertype information
6594 * info : info about the volume where the bitmap should be written
6595 * vol_idx : the volume index to use
6596 *
6597 * Returns:
6598 * 0 : success
6599 * -1 : fail
6600 ******************************************************************************/
6601 static int write_init_bitmap_imsm_all(struct supertype *st, struct mdinfo *info,
6602 int vol_idx)
6603 {
6604 int ret = 0;
6605
6606 if (info && (info->consistency_policy == CONSISTENCY_POLICY_BITMAP))
6607 ret = write_init_bitmap_imsm_vol(st, vol_idx);
6608
6609 return ret;
6610 }
6611
6612 static int write_init_super_imsm(struct supertype *st)
6613 {
6614 struct intel_super *super = st->sb;
6615 int current_vol = super->current_vol;
6616 int rv = 0;
6617 struct mdinfo info;
6618
6619 getinfo_super_imsm(st, &info, NULL);
6620
6621 /* we are done with current_vol reset it to point st at the container */
6622 super->current_vol = -1;
6623
6624 if (st->update_tail) {
6625 /* queue the recently created array / added disk
6626 * as a metadata update */
6627
6628 /* determine if we are creating a volume or adding a disk */
6629 if (current_vol < 0) {
6630 /* in the mgmt (add/remove) disk case we are running
6631 * in mdmon context, so don't close fd's
6632 */
6633 rv = mgmt_disk(st);
6634 } else {
6635 /* adding the second volume to the array */
6636 rv = write_init_ppl_imsm_all(st, &info);
6637 if (!rv)
6638 rv = write_init_bitmap_imsm_all(st, &info, current_vol);
6639 if (!rv)
6640 rv = create_array(st, current_vol);
6641 }
6642 } else {
6643 struct dl *d;
6644 for (d = super->disks; d; d = d->next)
6645 Kill(d->devname, NULL, 0, -1, 1);
6646 if (current_vol >= 0) {
6647 rv = write_init_ppl_imsm_all(st, &info);
6648 if (!rv)
6649 rv = write_init_bitmap_imsm_all(st, &info, current_vol);
6650 }
6651
6652 if (!rv)
6653 rv = write_super_imsm(st, 1);
6654 }
6655
6656 return rv;
6657 }
6658
6659 static int store_super_imsm(struct supertype *st, int fd)
6660 {
6661 struct intel_super *super = st->sb;
6662 struct imsm_super *mpb = super ? super->anchor : NULL;
6663
6664 if (!mpb)
6665 return 1;
6666
6667 if (super->sector_size == 4096)
6668 convert_to_4k(super);
6669 return store_imsm_mpb(fd, mpb);
6670 }
6671
6672 static int validate_geometry_imsm_container(struct supertype *st, int level,
6673 int layout, int raiddisks, int chunk,
6674 unsigned long long size,
6675 unsigned long long data_offset,
6676 char *dev,
6677 unsigned long long *freesize,
6678 int verbose)
6679 {
6680 int fd;
6681 unsigned long long ldsize;
6682 struct intel_super *super;
6683 int rv = 0;
6684
6685 if (level != LEVEL_CONTAINER)
6686 return 0;
6687 if (!dev)
6688 return 1;
6689
6690 fd = open(dev, O_RDONLY|O_EXCL, 0);
6691 if (fd < 0) {
6692 if (verbose > 0)
6693 pr_err("imsm: Cannot open %s: %s\n",
6694 dev, strerror(errno));
6695 return 0;
6696 }
6697 if (!get_dev_size(fd, dev, &ldsize)) {
6698 close(fd);
6699 return 0;
6700 }
6701
6702 /* capabilities retrieve could be possible
6703 * note that there is no fd for the disks in array.
6704 */
6705 super = alloc_super();
6706 if (!super) {
6707 close(fd);
6708 return 0;
6709 }
6710 if (!get_dev_sector_size(fd, NULL, &super->sector_size)) {
6711 close(fd);
6712 free_imsm(super);
6713 return 0;
6714 }
6715
6716 rv = find_intel_hba_capability(fd, super, verbose > 0 ? dev : NULL);
6717 if (rv != 0) {
6718 #if DEBUG
6719 char str[256];
6720 fd2devname(fd, str);
6721 dprintf("fd: %d %s orom: %p rv: %d raiddisk: %d\n",
6722 fd, str, super->orom, rv, raiddisks);
6723 #endif
6724 /* no orom/efi or non-intel hba of the disk */
6725 close(fd);
6726 free_imsm(super);
6727 return 0;
6728 }
6729 close(fd);
6730 if (super->orom) {
6731 if (raiddisks > super->orom->tds) {
6732 if (verbose)
6733 pr_err("%d exceeds maximum number of platform supported disks: %d\n",
6734 raiddisks, super->orom->tds);
6735 free_imsm(super);
6736 return 0;
6737 }
6738 if ((super->orom->attr & IMSM_OROM_ATTR_2TB_DISK) == 0 &&
6739 (ldsize >> 9) >> 32 > 0) {
6740 if (verbose)
6741 pr_err("%s exceeds maximum platform supported size\n", dev);
6742 free_imsm(super);
6743 return 0;
6744 }
6745 }
6746
6747 *freesize = avail_size_imsm(st, ldsize >> 9, data_offset);
6748 free_imsm(super);
6749
6750 return 1;
6751 }
6752
6753 static unsigned long long find_size(struct extent *e, int *idx, int num_extents)
6754 {
6755 const unsigned long long base_start = e[*idx].start;
6756 unsigned long long end = base_start + e[*idx].size;
6757 int i;
6758
6759 if (base_start == end)
6760 return 0;
6761
6762 *idx = *idx + 1;
6763 for (i = *idx; i < num_extents; i++) {
6764 /* extend overlapping extents */
6765 if (e[i].start >= base_start &&
6766 e[i].start <= end) {
6767 if (e[i].size == 0)
6768 return 0;
6769 if (e[i].start + e[i].size > end)
6770 end = e[i].start + e[i].size;
6771 } else if (e[i].start > end) {
6772 *idx = i;
6773 break;
6774 }
6775 }
6776
6777 return end - base_start;
6778 }
6779
6780 static unsigned long long merge_extents(struct intel_super *super, int sum_extents)
6781 {
6782 /* build a composite disk with all known extents and generate a new
6783 * 'maxsize' given the "all disks in an array must share a common start
6784 * offset" constraint
6785 */
6786 struct extent *e = xcalloc(sum_extents, sizeof(*e));
6787 struct dl *dl;
6788 int i, j;
6789 int start_extent;
6790 unsigned long long pos;
6791 unsigned long long start = 0;
6792 unsigned long long maxsize;
6793 unsigned long reserve;
6794
6795 /* coalesce and sort all extents. also, check to see if we need to
6796 * reserve space between member arrays
6797 */
6798 j = 0;
6799 for (dl = super->disks; dl; dl = dl->next) {
6800 if (!dl->e)
6801 continue;
6802 for (i = 0; i < dl->extent_cnt; i++)
6803 e[j++] = dl->e[i];
6804 }
6805 qsort(e, sum_extents, sizeof(*e), cmp_extent);
6806
6807 /* merge extents */
6808 i = 0;
6809 j = 0;
6810 while (i < sum_extents) {
6811 e[j].start = e[i].start;
6812 e[j].size = find_size(e, &i, sum_extents);
6813 j++;
6814 if (e[j-1].size == 0)
6815 break;
6816 }
6817
6818 pos = 0;
6819 maxsize = 0;
6820 start_extent = 0;
6821 i = 0;
6822 do {
6823 unsigned long long esize;
6824
6825 esize = e[i].start - pos;
6826 if (esize >= maxsize) {
6827 maxsize = esize;
6828 start = pos;
6829 start_extent = i;
6830 }
6831 pos = e[i].start + e[i].size;
6832 i++;
6833 } while (e[i-1].size);
6834 free(e);
6835
6836 if (maxsize == 0)
6837 return 0;
6838
6839 /* FIXME assumes volume at offset 0 is the first volume in a
6840 * container
6841 */
6842 if (start_extent > 0)
6843 reserve = IMSM_RESERVED_SECTORS; /* gap between raid regions */
6844 else
6845 reserve = 0;
6846
6847 if (maxsize < reserve)
6848 return 0;
6849
6850 super->create_offset = ~((unsigned long long) 0);
6851 if (start + reserve > super->create_offset)
6852 return 0; /* start overflows create_offset */
6853 super->create_offset = start + reserve;
6854
6855 return maxsize - reserve;
6856 }
6857
6858 static int is_raid_level_supported(const struct imsm_orom *orom, int level, int raiddisks)
6859 {
6860 if (level < 0 || level == 6 || level == 4)
6861 return 0;
6862
6863 /* if we have an orom prevent invalid raid levels */
6864 if (orom)
6865 switch (level) {
6866 case 0: return imsm_orom_has_raid0(orom);
6867 case 1:
6868 if (raiddisks > 2)
6869 return imsm_orom_has_raid1e(orom);
6870 return imsm_orom_has_raid1(orom) && raiddisks == 2;
6871 case 10: return imsm_orom_has_raid10(orom) && raiddisks == 4;
6872 case 5: return imsm_orom_has_raid5(orom) && raiddisks > 2;
6873 }
6874 else
6875 return 1; /* not on an Intel RAID platform so anything goes */
6876
6877 return 0;
6878 }
6879
6880 static int
6881 active_arrays_by_format(char *name, char* hba, struct md_list **devlist,
6882 int dpa, int verbose)
6883 {
6884 struct mdstat_ent *mdstat = mdstat_read(0, 0);
6885 struct mdstat_ent *memb;
6886 int count = 0;
6887 int num = 0;
6888 struct md_list *dv;
6889 int found;
6890
6891 for (memb = mdstat ; memb ; memb = memb->next) {
6892 if (memb->metadata_version &&
6893 (strncmp(memb->metadata_version, "external:", 9) == 0) &&
6894 (strcmp(&memb->metadata_version[9], name) == 0) &&
6895 !is_subarray(memb->metadata_version+9) &&
6896 memb->members) {
6897 struct dev_member *dev = memb->members;
6898 int fd = -1;
6899 while(dev && (fd < 0)) {
6900 char *path = xmalloc(strlen(dev->name) + strlen("/dev/") + 1);
6901 num = sprintf(path, "%s%s", "/dev/", dev->name);
6902 if (num > 0)
6903 fd = open(path, O_RDONLY, 0);
6904 if (num <= 0 || fd < 0) {
6905 pr_vrb("Cannot open %s: %s\n",
6906 dev->name, strerror(errno));
6907 }
6908 free(path);
6909 dev = dev->next;
6910 }
6911 found = 0;
6912 if (fd >= 0 && disk_attached_to_hba(fd, hba)) {
6913 struct mdstat_ent *vol;
6914 for (vol = mdstat ; vol ; vol = vol->next) {
6915 if (vol->active > 0 &&
6916 vol->metadata_version &&
6917 is_container_member(vol, memb->devnm)) {
6918 found++;
6919 count++;
6920 }
6921 }
6922 if (*devlist && (found < dpa)) {
6923 dv = xcalloc(1, sizeof(*dv));
6924 dv->devname = xmalloc(strlen(memb->devnm) + strlen("/dev/") + 1);
6925 sprintf(dv->devname, "%s%s", "/dev/", memb->devnm);
6926 dv->found = found;
6927 dv->used = 0;
6928 dv->next = *devlist;
6929 *devlist = dv;
6930 }
6931 }
6932 if (fd >= 0)
6933 close(fd);
6934 }
6935 }
6936 free_mdstat(mdstat);
6937 return count;
6938 }
6939
6940 #ifdef DEBUG_LOOP
6941 static struct md_list*
6942 get_loop_devices(void)
6943 {
6944 int i;
6945 struct md_list *devlist = NULL;
6946 struct md_list *dv;
6947
6948 for(i = 0; i < 12; i++) {
6949 dv = xcalloc(1, sizeof(*dv));
6950 dv->devname = xmalloc(40);
6951 sprintf(dv->devname, "/dev/loop%d", i);
6952 dv->next = devlist;
6953 devlist = dv;
6954 }
6955 return devlist;
6956 }
6957 #endif
6958
6959 static struct md_list*
6960 get_devices(const char *hba_path)
6961 {
6962 struct md_list *devlist = NULL;
6963 struct md_list *dv;
6964 struct dirent *ent;
6965 DIR *dir;
6966 int err = 0;
6967
6968 #if DEBUG_LOOP
6969 devlist = get_loop_devices();
6970 return devlist;
6971 #endif
6972 /* scroll through /sys/dev/block looking for devices attached to
6973 * this hba
6974 */
6975 dir = opendir("/sys/dev/block");
6976 for (ent = dir ? readdir(dir) : NULL; ent; ent = readdir(dir)) {
6977 int fd;
6978 char buf[1024];
6979 int major, minor;
6980 char *path = NULL;
6981 if (sscanf(ent->d_name, "%d:%d", &major, &minor) != 2)
6982 continue;
6983 path = devt_to_devpath(makedev(major, minor));
6984 if (!path)
6985 continue;
6986 if (!path_attached_to_hba(path, hba_path)) {
6987 free(path);
6988 path = NULL;
6989 continue;
6990 }
6991 free(path);
6992 path = NULL;
6993 fd = dev_open(ent->d_name, O_RDONLY);
6994 if (fd >= 0) {
6995 fd2devname(fd, buf);
6996 close(fd);
6997 } else {
6998 pr_err("cannot open device: %s\n",
6999 ent->d_name);
7000 continue;
7001 }
7002
7003 dv = xcalloc(1, sizeof(*dv));
7004 dv->devname = xstrdup(buf);
7005 dv->next = devlist;
7006 devlist = dv;
7007 }
7008 if (err) {
7009 while(devlist) {
7010 dv = devlist;
7011 devlist = devlist->next;
7012 free(dv->devname);
7013 free(dv);
7014 }
7015 }
7016 closedir(dir);
7017 return devlist;
7018 }
7019
7020 static int
7021 count_volumes_list(struct md_list *devlist, char *homehost,
7022 int verbose, int *found)
7023 {
7024 struct md_list *tmpdev;
7025 int count = 0;
7026 struct supertype *st;
7027
7028 /* first walk the list of devices to find a consistent set
7029 * that match the criterea, if that is possible.
7030 * We flag the ones we like with 'used'.
7031 */
7032 *found = 0;
7033 st = match_metadata_desc_imsm("imsm");
7034 if (st == NULL) {
7035 pr_vrb("cannot allocate memory for imsm supertype\n");
7036 return 0;
7037 }
7038
7039 for (tmpdev = devlist; tmpdev; tmpdev = tmpdev->next) {
7040 char *devname = tmpdev->devname;
7041 dev_t rdev;
7042 struct supertype *tst;
7043 int dfd;
7044 if (tmpdev->used > 1)
7045 continue;
7046 tst = dup_super(st);
7047 if (tst == NULL) {
7048 pr_vrb("cannot allocate memory for imsm supertype\n");
7049 goto err_1;
7050 }
7051 tmpdev->container = 0;
7052 dfd = dev_open(devname, O_RDONLY|O_EXCL);
7053 if (dfd < 0) {
7054 dprintf("cannot open device %s: %s\n",
7055 devname, strerror(errno));
7056 tmpdev->used = 2;
7057 } else if (!fstat_is_blkdev(dfd, devname, &rdev)) {
7058 tmpdev->used = 2;
7059 } else if (must_be_container(dfd)) {
7060 struct supertype *cst;
7061 cst = super_by_fd(dfd, NULL);
7062 if (cst == NULL) {
7063 dprintf("cannot recognize container type %s\n",
7064 devname);
7065 tmpdev->used = 2;
7066 } else if (tst->ss != st->ss) {
7067 dprintf("non-imsm container - ignore it: %s\n",
7068 devname);
7069 tmpdev->used = 2;
7070 } else if (!tst->ss->load_container ||
7071 tst->ss->load_container(tst, dfd, NULL))
7072 tmpdev->used = 2;
7073 else {
7074 tmpdev->container = 1;
7075 }
7076 if (cst)
7077 cst->ss->free_super(cst);
7078 } else {
7079 tmpdev->st_rdev = rdev;
7080 if (tst->ss->load_super(tst,dfd, NULL)) {
7081 dprintf("no RAID superblock on %s\n",
7082 devname);
7083 tmpdev->used = 2;
7084 } else if (tst->ss->compare_super == NULL) {
7085 dprintf("Cannot assemble %s metadata on %s\n",
7086 tst->ss->name, devname);
7087 tmpdev->used = 2;
7088 }
7089 }
7090 if (dfd >= 0)
7091 close(dfd);
7092 if (tmpdev->used == 2 || tmpdev->used == 4) {
7093 /* Ignore unrecognised devices during auto-assembly */
7094 goto loop;
7095 }
7096 else {
7097 struct mdinfo info;
7098 tst->ss->getinfo_super(tst, &info, NULL);
7099
7100 if (st->minor_version == -1)
7101 st->minor_version = tst->minor_version;
7102
7103 if (memcmp(info.uuid, uuid_zero,
7104 sizeof(int[4])) == 0) {
7105 /* this is a floating spare. It cannot define
7106 * an array unless there are no more arrays of
7107 * this type to be found. It can be included
7108 * in an array of this type though.
7109 */
7110 tmpdev->used = 3;
7111 goto loop;
7112 }
7113
7114 if (st->ss != tst->ss ||
7115 st->minor_version != tst->minor_version ||
7116 st->ss->compare_super(st, tst, 1) != 0) {
7117 /* Some mismatch. If exactly one array matches this host,
7118 * we can resolve on that one.
7119 * Or, if we are auto assembling, we just ignore the second
7120 * for now.
7121 */
7122 dprintf("superblock on %s doesn't match others - assembly aborted\n",
7123 devname);
7124 goto loop;
7125 }
7126 tmpdev->used = 1;
7127 *found = 1;
7128 dprintf("found: devname: %s\n", devname);
7129 }
7130 loop:
7131 if (tst)
7132 tst->ss->free_super(tst);
7133 }
7134 if (*found != 0) {
7135 int err;
7136 if ((err = load_super_imsm_all(st, -1, &st->sb, NULL, devlist, 0)) == 0) {
7137 struct mdinfo *iter, *head = st->ss->container_content(st, NULL);
7138 for (iter = head; iter; iter = iter->next) {
7139 dprintf("content->text_version: %s vol\n",
7140 iter->text_version);
7141 if (iter->array.state & (1<<MD_SB_BLOCK_VOLUME)) {
7142 /* do not assemble arrays with unsupported
7143 configurations */
7144 dprintf("Cannot activate member %s.\n",
7145 iter->text_version);
7146 } else
7147 count++;
7148 }
7149 sysfs_free(head);
7150
7151 } else {
7152 dprintf("No valid super block on device list: err: %d %p\n",
7153 err, st->sb);
7154 }
7155 } else {
7156 dprintf("no more devices to examine\n");
7157 }
7158
7159 for (tmpdev = devlist; tmpdev; tmpdev = tmpdev->next) {
7160 if (tmpdev->used == 1 && tmpdev->found) {
7161 if (count) {
7162 if (count < tmpdev->found)
7163 count = 0;
7164 else
7165 count -= tmpdev->found;
7166 }
7167 }
7168 if (tmpdev->used == 1)
7169 tmpdev->used = 4;
7170 }
7171 err_1:
7172 if (st)
7173 st->ss->free_super(st);
7174 return count;
7175 }
7176
7177 static int __count_volumes(char *hba_path, int dpa, int verbose,
7178 int cmp_hba_path)
7179 {
7180 struct sys_dev *idev, *intel_devices = find_intel_devices();
7181 int count = 0;
7182 const struct orom_entry *entry;
7183 struct devid_list *dv, *devid_list;
7184
7185 if (!hba_path)
7186 return 0;
7187
7188 for (idev = intel_devices; idev; idev = idev->next) {
7189 if (strstr(idev->path, hba_path))
7190 break;
7191 }
7192
7193 if (!idev || !idev->dev_id)
7194 return 0;
7195
7196 entry = get_orom_entry_by_device_id(idev->dev_id);
7197
7198 if (!entry || !entry->devid_list)
7199 return 0;
7200
7201 devid_list = entry->devid_list;
7202 for (dv = devid_list; dv; dv = dv->next) {
7203 struct md_list *devlist;
7204 struct sys_dev *device = NULL;
7205 char *hpath;
7206 int found = 0;
7207
7208 if (cmp_hba_path)
7209 device = device_by_id_and_path(dv->devid, hba_path);
7210 else
7211 device = device_by_id(dv->devid);
7212
7213 if (device)
7214 hpath = device->path;
7215 else
7216 return 0;
7217
7218 devlist = get_devices(hpath);
7219 /* if no intel devices return zero volumes */
7220 if (devlist == NULL)
7221 return 0;
7222
7223 count += active_arrays_by_format("imsm", hpath, &devlist, dpa,
7224 verbose);
7225 dprintf("path: %s active arrays: %d\n", hpath, count);
7226 if (devlist == NULL)
7227 return 0;
7228 do {
7229 found = 0;
7230 count += count_volumes_list(devlist,
7231 NULL,
7232 verbose,
7233 &found);
7234 dprintf("found %d count: %d\n", found, count);
7235 } while (found);
7236
7237 dprintf("path: %s total number of volumes: %d\n", hpath, count);
7238
7239 while (devlist) {
7240 struct md_list *dv = devlist;
7241 devlist = devlist->next;
7242 free(dv->devname);
7243 free(dv);
7244 }
7245 }
7246 return count;
7247 }
7248
7249 static int count_volumes(struct intel_hba *hba, int dpa, int verbose)
7250 {
7251 if (!hba)
7252 return 0;
7253 if (hba->type == SYS_DEV_VMD) {
7254 struct sys_dev *dev;
7255 int count = 0;
7256
7257 for (dev = find_intel_devices(); dev; dev = dev->next) {
7258 if (dev->type == SYS_DEV_VMD)
7259 count += __count_volumes(dev->path, dpa,
7260 verbose, 1);
7261 }
7262 return count;
7263 }
7264 return __count_volumes(hba->path, dpa, verbose, 0);
7265 }
7266
7267 static int imsm_default_chunk(const struct imsm_orom *orom)
7268 {
7269 /* up to 512 if the plaform supports it, otherwise the platform max.
7270 * 128 if no platform detected
7271 */
7272 int fs = max(7, orom ? fls(orom->sss) : 0);
7273
7274 return min(512, (1 << fs));
7275 }
7276
7277 static int
7278 validate_geometry_imsm_orom(struct intel_super *super, int level, int layout,
7279 int raiddisks, int *chunk, unsigned long long size, int verbose)
7280 {
7281 /* check/set platform and metadata limits/defaults */
7282 if (super->orom && raiddisks > super->orom->dpa) {
7283 pr_vrb("platform supports a maximum of %d disks per array\n",
7284 super->orom->dpa);
7285 return 0;
7286 }
7287
7288 /* capabilities of OROM tested - copied from validate_geometry_imsm_volume */
7289 if (!is_raid_level_supported(super->orom, level, raiddisks)) {
7290 pr_vrb("platform does not support raid%d with %d disk%s\n",
7291 level, raiddisks, raiddisks > 1 ? "s" : "");
7292 return 0;
7293 }
7294
7295 if (*chunk == 0 || *chunk == UnSet)
7296 *chunk = imsm_default_chunk(super->orom);
7297
7298 if (super->orom && !imsm_orom_has_chunk(super->orom, *chunk)) {
7299 pr_vrb("platform does not support a chunk size of: %d\n", *chunk);
7300 return 0;
7301 }
7302
7303 if (layout != imsm_level_to_layout(level)) {
7304 if (level == 5)
7305 pr_vrb("imsm raid 5 only supports the left-asymmetric layout\n");
7306 else if (level == 10)
7307 pr_vrb("imsm raid 10 only supports the n2 layout\n");
7308 else
7309 pr_vrb("imsm unknown layout %#x for this raid level %d\n",
7310 layout, level);
7311 return 0;
7312 }
7313
7314 if (super->orom && (super->orom->attr & IMSM_OROM_ATTR_2TB) == 0 &&
7315 (calc_array_size(level, raiddisks, layout, *chunk, size) >> 32) > 0) {
7316 pr_vrb("platform does not support a volume size over 2TB\n");
7317 return 0;
7318 }
7319
7320 return 1;
7321 }
7322
7323 /* validate_geometry_imsm_volume - lifted from validate_geometry_ddf_bvd
7324 * FIX ME add ahci details
7325 */
7326 static int validate_geometry_imsm_volume(struct supertype *st, int level,
7327 int layout, int raiddisks, int *chunk,
7328 unsigned long long size,
7329 unsigned long long data_offset,
7330 char *dev,
7331 unsigned long long *freesize,
7332 int verbose)
7333 {
7334 dev_t rdev;
7335 struct intel_super *super = st->sb;
7336 struct imsm_super *mpb;
7337 struct dl *dl;
7338 unsigned long long pos = 0;
7339 unsigned long long maxsize;
7340 struct extent *e;
7341 int i;
7342
7343 /* We must have the container info already read in. */
7344 if (!super)
7345 return 0;
7346
7347 mpb = super->anchor;
7348
7349 if (!validate_geometry_imsm_orom(super, level, layout, raiddisks, chunk, size, verbose)) {
7350 pr_err("RAID geometry validation failed. Cannot proceed with the action(s).\n");
7351 return 0;
7352 }
7353 if (!dev) {
7354 /* General test: make sure there is space for
7355 * 'raiddisks' device extents of size 'size' at a given
7356 * offset
7357 */
7358 unsigned long long minsize = size;
7359 unsigned long long start_offset = MaxSector;
7360 int dcnt = 0;
7361 if (minsize == 0)
7362 minsize = MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS;
7363 for (dl = super->disks; dl ; dl = dl->next) {
7364 int found = 0;
7365
7366 pos = 0;
7367 i = 0;
7368 e = get_extents(super, dl, 0);
7369 if (!e) continue;
7370 do {
7371 unsigned long long esize;
7372 esize = e[i].start - pos;
7373 if (esize >= minsize)
7374 found = 1;
7375 if (found && start_offset == MaxSector) {
7376 start_offset = pos;
7377 break;
7378 } else if (found && pos != start_offset) {
7379 found = 0;
7380 break;
7381 }
7382 pos = e[i].start + e[i].size;
7383 i++;
7384 } while (e[i-1].size);
7385 if (found)
7386 dcnt++;
7387 free(e);
7388 }
7389 if (dcnt < raiddisks) {
7390 if (verbose)
7391 pr_err("imsm: Not enough devices with space for this array (%d < %d)\n",
7392 dcnt, raiddisks);
7393 return 0;
7394 }
7395 return 1;
7396 }
7397
7398 /* This device must be a member of the set */
7399 if (!stat_is_blkdev(dev, &rdev))
7400 return 0;
7401 for (dl = super->disks ; dl ; dl = dl->next) {
7402 if (dl->major == (int)major(rdev) &&
7403 dl->minor == (int)minor(rdev))
7404 break;
7405 }
7406 if (!dl) {
7407 if (verbose)
7408 pr_err("%s is not in the same imsm set\n", dev);
7409 return 0;
7410 } else if (super->orom && dl->index < 0 && mpb->num_raid_devs) {
7411 /* If a volume is present then the current creation attempt
7412 * cannot incorporate new spares because the orom may not
7413 * understand this configuration (all member disks must be
7414 * members of each array in the container).
7415 */
7416 pr_err("%s is a spare and a volume is already defined for this container\n", dev);
7417 pr_err("The option-rom requires all member disks to be a member of all volumes\n");
7418 return 0;
7419 } else if (super->orom && mpb->num_raid_devs > 0 &&
7420 mpb->num_disks != raiddisks) {
7421 pr_err("The option-rom requires all member disks to be a member of all volumes\n");
7422 return 0;
7423 }
7424
7425 /* retrieve the largest free space block */
7426 e = get_extents(super, dl, 0);
7427 maxsize = 0;
7428 i = 0;
7429 if (e) {
7430 do {
7431 unsigned long long esize;
7432
7433 esize = e[i].start - pos;
7434 if (esize >= maxsize)
7435 maxsize = esize;
7436 pos = e[i].start + e[i].size;
7437 i++;
7438 } while (e[i-1].size);
7439 dl->e = e;
7440 dl->extent_cnt = i;
7441 } else {
7442 if (verbose)
7443 pr_err("unable to determine free space for: %s\n",
7444 dev);
7445 return 0;
7446 }
7447 if (maxsize < size) {
7448 if (verbose)
7449 pr_err("%s not enough space (%llu < %llu)\n",
7450 dev, maxsize, size);
7451 return 0;
7452 }
7453
7454 /* count total number of extents for merge */
7455 i = 0;
7456 for (dl = super->disks; dl; dl = dl->next)
7457 if (dl->e)
7458 i += dl->extent_cnt;
7459
7460 maxsize = merge_extents(super, i);
7461
7462 if (mpb->num_raid_devs > 0 && size && size != maxsize)
7463 pr_err("attempting to create a second volume with size less then remaining space.\n");
7464
7465 if (maxsize < size || maxsize == 0) {
7466 if (verbose) {
7467 if (maxsize == 0)
7468 pr_err("no free space left on device. Aborting...\n");
7469 else
7470 pr_err("not enough space to create volume of given size (%llu < %llu). Aborting...\n",
7471 maxsize, size);
7472 }
7473 return 0;
7474 }
7475
7476 *freesize = maxsize;
7477
7478 if (super->orom) {
7479 int count = count_volumes(super->hba,
7480 super->orom->dpa, verbose);
7481 if (super->orom->vphba <= count) {
7482 pr_vrb("platform does not support more than %d raid volumes.\n",
7483 super->orom->vphba);
7484 return 0;
7485 }
7486 }
7487 return 1;
7488 }
7489
7490 static int imsm_get_free_size(struct supertype *st, int raiddisks,
7491 unsigned long long size, int chunk,
7492 unsigned long long *freesize)
7493 {
7494 struct intel_super *super = st->sb;
7495 struct imsm_super *mpb = super->anchor;
7496 struct dl *dl;
7497 int i;
7498 int extent_cnt;
7499 struct extent *e;
7500 unsigned long long maxsize;
7501 unsigned long long minsize;
7502 int cnt;
7503 int used;
7504
7505 /* find the largest common start free region of the possible disks */
7506 used = 0;
7507 extent_cnt = 0;
7508 cnt = 0;
7509 for (dl = super->disks; dl; dl = dl->next) {
7510 dl->raiddisk = -1;
7511
7512 if (dl->index >= 0)
7513 used++;
7514
7515 /* don't activate new spares if we are orom constrained
7516 * and there is already a volume active in the container
7517 */
7518 if (super->orom && dl->index < 0 && mpb->num_raid_devs)
7519 continue;
7520
7521 e = get_extents(super, dl, 0);
7522 if (!e)
7523 continue;
7524 for (i = 1; e[i-1].size; i++)
7525 ;
7526 dl->e = e;
7527 dl->extent_cnt = i;
7528 extent_cnt += i;
7529 cnt++;
7530 }
7531
7532 maxsize = merge_extents(super, extent_cnt);
7533 minsize = size;
7534 if (size == 0)
7535 /* chunk is in K */
7536 minsize = chunk * 2;
7537
7538 if (cnt < raiddisks ||
7539 (super->orom && used && used != raiddisks) ||
7540 maxsize < minsize ||
7541 maxsize == 0) {
7542 pr_err("not enough devices with space to create array.\n");
7543 return 0; /* No enough free spaces large enough */
7544 }
7545
7546 if (size == 0) {
7547 size = maxsize;
7548 if (chunk) {
7549 size /= 2 * chunk;
7550 size *= 2 * chunk;
7551 }
7552 maxsize = size;
7553 }
7554 if (mpb->num_raid_devs > 0 && size && size != maxsize)
7555 pr_err("attempting to create a second volume with size less then remaining space.\n");
7556 cnt = 0;
7557 for (dl = super->disks; dl; dl = dl->next)
7558 if (dl->e)
7559 dl->raiddisk = cnt++;
7560
7561 *freesize = size;
7562
7563 dprintf("imsm: imsm_get_free_size() returns : %llu\n", size);
7564
7565 return 1;
7566 }
7567
7568 static int reserve_space(struct supertype *st, int raiddisks,
7569 unsigned long long size, int chunk,
7570 unsigned long long *freesize)
7571 {
7572 struct intel_super *super = st->sb;
7573 struct dl *dl;
7574 int cnt;
7575 int rv = 0;
7576
7577 rv = imsm_get_free_size(st, raiddisks, size, chunk, freesize);
7578 if (rv) {
7579 cnt = 0;
7580 for (dl = super->disks; dl; dl = dl->next)
7581 if (dl->e)
7582 dl->raiddisk = cnt++;
7583 rv = 1;
7584 }
7585
7586 return rv;
7587 }
7588
7589 static int validate_geometry_imsm(struct supertype *st, int level, int layout,
7590 int raiddisks, int *chunk, unsigned long long size,
7591 unsigned long long data_offset,
7592 char *dev, unsigned long long *freesize,
7593 int consistency_policy, int verbose)
7594 {
7595 int fd, cfd;
7596 struct mdinfo *sra;
7597 int is_member = 0;
7598
7599 /* load capability
7600 * if given unused devices create a container
7601 * if given given devices in a container create a member volume
7602 */
7603 if (level == LEVEL_CONTAINER) {
7604 /* Must be a fresh device to add to a container */
7605 return validate_geometry_imsm_container(st, level, layout,
7606 raiddisks,
7607 *chunk,
7608 size, data_offset,
7609 dev, freesize,
7610 verbose);
7611 }
7612
7613 /*
7614 * Size is given in sectors.
7615 */
7616 if (size && (size < 2048)) {
7617 pr_err("Given size must be greater than 1M.\n");
7618 /* Depends on algorithm in Create.c :
7619 * if container was given (dev == NULL) return -1,
7620 * if block device was given ( dev != NULL) return 0.
7621 */
7622 return dev ? -1 : 0;
7623 }
7624
7625 if (!dev) {
7626 if (st->sb) {
7627 struct intel_super *super = st->sb;
7628 if (!validate_geometry_imsm_orom(st->sb, level, layout,
7629 raiddisks, chunk, size,
7630 verbose))
7631 return 0;
7632 /* we are being asked to automatically layout a
7633 * new volume based on the current contents of
7634 * the container. If the the parameters can be
7635 * satisfied reserve_space will record the disks,
7636 * start offset, and size of the volume to be
7637 * created. add_to_super and getinfo_super
7638 * detect when autolayout is in progress.
7639 */
7640 /* assuming that freesize is always given when array is
7641 created */
7642 if (super->orom && freesize) {
7643 int count;
7644 count = count_volumes(super->hba,
7645 super->orom->dpa, verbose);
7646 if (super->orom->vphba <= count) {
7647 pr_vrb("platform does not support more than %d raid volumes.\n",
7648 super->orom->vphba);
7649 return 0;
7650 }
7651 }
7652 if (freesize)
7653 return reserve_space(st, raiddisks, size,
7654 *chunk, freesize);
7655 }
7656 return 1;
7657 }
7658 if (st->sb) {
7659 /* creating in a given container */
7660 return validate_geometry_imsm_volume(st, level, layout,
7661 raiddisks, chunk, size,
7662 data_offset,
7663 dev, freesize, verbose);
7664 }
7665
7666 /* This device needs to be a device in an 'imsm' container */
7667 fd = open(dev, O_RDONLY|O_EXCL, 0);
7668 if (fd >= 0) {
7669 if (verbose)
7670 pr_err("Cannot create this array on device %s\n",
7671 dev);
7672 close(fd);
7673 return 0;
7674 }
7675 if (errno != EBUSY || (fd = open(dev, O_RDONLY, 0)) < 0) {
7676 if (verbose)
7677 pr_err("Cannot open %s: %s\n",
7678 dev, strerror(errno));
7679 return 0;
7680 }
7681 /* Well, it is in use by someone, maybe an 'imsm' container. */
7682 cfd = open_container(fd);
7683 close(fd);
7684 if (cfd < 0) {
7685 if (verbose)
7686 pr_err("Cannot use %s: It is busy\n",
7687 dev);
7688 return 0;
7689 }
7690 sra = sysfs_read(cfd, NULL, GET_VERSION);
7691 if (sra && sra->array.major_version == -1 &&
7692 strcmp(sra->text_version, "imsm") == 0)
7693 is_member = 1;
7694 sysfs_free(sra);
7695 if (is_member) {
7696 /* This is a member of a imsm container. Load the container
7697 * and try to create a volume
7698 */
7699 struct intel_super *super;
7700
7701 if (load_super_imsm_all(st, cfd, (void **) &super, NULL, NULL, 1) == 0) {
7702 st->sb = super;
7703 strcpy(st->container_devnm, fd2devnm(cfd));
7704 close(cfd);
7705 return validate_geometry_imsm_volume(st, level, layout,
7706 raiddisks, chunk,
7707 size, data_offset, dev,
7708 freesize, 1)
7709 ? 1 : -1;
7710 }
7711 }
7712
7713 if (verbose)
7714 pr_err("failed container membership check\n");
7715
7716 close(cfd);
7717 return 0;
7718 }
7719
7720 static void default_geometry_imsm(struct supertype *st, int *level, int *layout, int *chunk)
7721 {
7722 struct intel_super *super = st->sb;
7723
7724 if (level && *level == UnSet)
7725 *level = LEVEL_CONTAINER;
7726
7727 if (level && layout && *layout == UnSet)
7728 *layout = imsm_level_to_layout(*level);
7729
7730 if (chunk && (*chunk == UnSet || *chunk == 0))
7731 *chunk = imsm_default_chunk(super->orom);
7732 }
7733
7734 static void handle_missing(struct intel_super *super, struct imsm_dev *dev);
7735
7736 static int kill_subarray_imsm(struct supertype *st, char *subarray_id)
7737 {
7738 /* remove the subarray currently referenced by subarray_id */
7739 __u8 i;
7740 struct intel_dev **dp;
7741 struct intel_super *super = st->sb;
7742 __u8 current_vol = strtoul(subarray_id, NULL, 10);
7743 struct imsm_super *mpb = super->anchor;
7744
7745 if (mpb->num_raid_devs == 0)
7746 return 2;
7747
7748 /* block deletions that would change the uuid of active subarrays
7749 *
7750 * FIXME when immutable ids are available, but note that we'll
7751 * also need to fixup the invalidated/active subarray indexes in
7752 * mdstat
7753 */
7754 for (i = 0; i < mpb->num_raid_devs; i++) {
7755 char subarray[4];
7756
7757 if (i < current_vol)
7758 continue;
7759 sprintf(subarray, "%u", i);
7760 if (is_subarray_active(subarray, st->devnm)) {
7761 pr_err("deleting subarray-%d would change the UUID of active subarray-%d, aborting\n",
7762 current_vol, i);
7763
7764 return 2;
7765 }
7766 }
7767
7768 if (st->update_tail) {
7769 struct imsm_update_kill_array *u = xmalloc(sizeof(*u));
7770
7771 u->type = update_kill_array;
7772 u->dev_idx = current_vol;
7773 append_metadata_update(st, u, sizeof(*u));
7774
7775 return 0;
7776 }
7777
7778 for (dp = &super->devlist; *dp;)
7779 if ((*dp)->index == current_vol) {
7780 *dp = (*dp)->next;
7781 } else {
7782 handle_missing(super, (*dp)->dev);
7783 if ((*dp)->index > current_vol)
7784 (*dp)->index--;
7785 dp = &(*dp)->next;
7786 }
7787
7788 /* no more raid devices, all active components are now spares,
7789 * but of course failed are still failed
7790 */
7791 if (--mpb->num_raid_devs == 0) {
7792 struct dl *d;
7793
7794 for (d = super->disks; d; d = d->next)
7795 if (d->index > -2)
7796 mark_spare(d);
7797 }
7798
7799 super->updates_pending++;
7800
7801 return 0;
7802 }
7803
7804 static int get_rwh_policy_from_update(char *update)
7805 {
7806 if (strcmp(update, "ppl") == 0)
7807 return RWH_MULTIPLE_DISTRIBUTED;
7808 else if (strcmp(update, "no-ppl") == 0)
7809 return RWH_MULTIPLE_OFF;
7810 else if (strcmp(update, "bitmap") == 0)
7811 return RWH_BITMAP;
7812 else if (strcmp(update, "no-bitmap") == 0)
7813 return RWH_OFF;
7814 return -1;
7815 }
7816
7817 static int update_subarray_imsm(struct supertype *st, char *subarray,
7818 char *update, struct mddev_ident *ident)
7819 {
7820 /* update the subarray currently referenced by ->current_vol */
7821 struct intel_super *super = st->sb;
7822 struct imsm_super *mpb = super->anchor;
7823
7824 if (strcmp(update, "name") == 0) {
7825 char *name = ident->name;
7826 char *ep;
7827 int vol;
7828
7829 if (is_subarray_active(subarray, st->devnm)) {
7830 pr_err("Unable to update name of active subarray\n");
7831 return 2;
7832 }
7833
7834 if (!check_name(super, name, 0))
7835 return 2;
7836
7837 vol = strtoul(subarray, &ep, 10);
7838 if (*ep != '\0' || vol >= super->anchor->num_raid_devs)
7839 return 2;
7840
7841 if (st->update_tail) {
7842 struct imsm_update_rename_array *u = xmalloc(sizeof(*u));
7843
7844 u->type = update_rename_array;
7845 u->dev_idx = vol;
7846 strncpy((char *) u->name, name, MAX_RAID_SERIAL_LEN);
7847 u->name[MAX_RAID_SERIAL_LEN-1] = '\0';
7848 append_metadata_update(st, u, sizeof(*u));
7849 } else {
7850 struct imsm_dev *dev;
7851 int i, namelen;
7852
7853 dev = get_imsm_dev(super, vol);
7854 memset(dev->volume, '\0', MAX_RAID_SERIAL_LEN);
7855 namelen = min((int)strlen(name), MAX_RAID_SERIAL_LEN);
7856 memcpy(dev->volume, name, namelen);
7857 for (i = 0; i < mpb->num_raid_devs; i++) {
7858 dev = get_imsm_dev(super, i);
7859 handle_missing(super, dev);
7860 }
7861 super->updates_pending++;
7862 }
7863 } else if (get_rwh_policy_from_update(update) != -1) {
7864 int new_policy;
7865 char *ep;
7866 int vol = strtoul(subarray, &ep, 10);
7867
7868 if (*ep != '\0' || vol >= super->anchor->num_raid_devs)
7869 return 2;
7870
7871 new_policy = get_rwh_policy_from_update(update);
7872
7873 if (st->update_tail) {
7874 struct imsm_update_rwh_policy *u = xmalloc(sizeof(*u));
7875
7876 u->type = update_rwh_policy;
7877 u->dev_idx = vol;
7878 u->new_policy = new_policy;
7879 append_metadata_update(st, u, sizeof(*u));
7880 } else {
7881 struct imsm_dev *dev;
7882
7883 dev = get_imsm_dev(super, vol);
7884 dev->rwh_policy = new_policy;
7885 super->updates_pending++;
7886 }
7887 if (new_policy == RWH_BITMAP)
7888 return write_init_bitmap_imsm_vol(st, vol);
7889 } else
7890 return 2;
7891
7892 return 0;
7893 }
7894
7895 static int is_gen_migration(struct imsm_dev *dev)
7896 {
7897 if (dev == NULL)
7898 return 0;
7899
7900 if (!dev->vol.migr_state)
7901 return 0;
7902
7903 if (migr_type(dev) == MIGR_GEN_MIGR)
7904 return 1;
7905
7906 return 0;
7907 }
7908
7909 static int is_rebuilding(struct imsm_dev *dev)
7910 {
7911 struct imsm_map *migr_map;
7912
7913 if (!dev->vol.migr_state)
7914 return 0;
7915
7916 if (migr_type(dev) != MIGR_REBUILD)
7917 return 0;
7918
7919 migr_map = get_imsm_map(dev, MAP_1);
7920
7921 if (migr_map->map_state == IMSM_T_STATE_DEGRADED)
7922 return 1;
7923 else
7924 return 0;
7925 }
7926
7927 static int is_initializing(struct imsm_dev *dev)
7928 {
7929 struct imsm_map *migr_map;
7930
7931 if (!dev->vol.migr_state)
7932 return 0;
7933
7934 if (migr_type(dev) != MIGR_INIT)
7935 return 0;
7936
7937 migr_map = get_imsm_map(dev, MAP_1);
7938
7939 if (migr_map->map_state == IMSM_T_STATE_UNINITIALIZED)
7940 return 1;
7941
7942 return 0;
7943 }
7944
7945 static void update_recovery_start(struct intel_super *super,
7946 struct imsm_dev *dev,
7947 struct mdinfo *array)
7948 {
7949 struct mdinfo *rebuild = NULL;
7950 struct mdinfo *d;
7951 __u32 units;
7952
7953 if (!is_rebuilding(dev))
7954 return;
7955
7956 /* Find the rebuild target, but punt on the dual rebuild case */
7957 for (d = array->devs; d; d = d->next)
7958 if (d->recovery_start == 0) {
7959 if (rebuild)
7960 return;
7961 rebuild = d;
7962 }
7963
7964 if (!rebuild) {
7965 /* (?) none of the disks are marked with
7966 * IMSM_ORD_REBUILD, so assume they are missing and the
7967 * disk_ord_tbl was not correctly updated
7968 */
7969 dprintf("failed to locate out-of-sync disk\n");
7970 return;
7971 }
7972
7973 units = vol_curr_migr_unit(dev);
7974 rebuild->recovery_start = units * blocks_per_migr_unit(super, dev);
7975 }
7976
7977 static int recover_backup_imsm(struct supertype *st, struct mdinfo *info);
7978
7979 static struct mdinfo *container_content_imsm(struct supertype *st, char *subarray)
7980 {
7981 /* Given a container loaded by load_super_imsm_all,
7982 * extract information about all the arrays into
7983 * an mdinfo tree.
7984 * If 'subarray' is given, just extract info about that array.
7985 *
7986 * For each imsm_dev create an mdinfo, fill it in,
7987 * then look for matching devices in super->disks
7988 * and create appropriate device mdinfo.
7989 */
7990 struct intel_super *super = st->sb;
7991 struct imsm_super *mpb = super->anchor;
7992 struct mdinfo *rest = NULL;
7993 unsigned int i;
7994 int sb_errors = 0;
7995 struct dl *d;
7996 int spare_disks = 0;
7997 int current_vol = super->current_vol;
7998
7999 /* do not assemble arrays when not all attributes are supported */
8000 if (imsm_check_attributes(mpb->attributes) == 0) {
8001 sb_errors = 1;
8002 pr_err("Unsupported attributes in IMSM metadata.Arrays activation is blocked.\n");
8003 }
8004
8005 /* count spare devices, not used in maps
8006 */
8007 for (d = super->disks; d; d = d->next)
8008 if (d->index == -1)
8009 spare_disks++;
8010
8011 for (i = 0; i < mpb->num_raid_devs; i++) {
8012 struct imsm_dev *dev;
8013 struct imsm_map *map;
8014 struct imsm_map *map2;
8015 struct mdinfo *this;
8016 int slot;
8017 int chunk;
8018 char *ep;
8019 int level;
8020
8021 if (subarray &&
8022 (i != strtoul(subarray, &ep, 10) || *ep != '\0'))
8023 continue;
8024
8025 dev = get_imsm_dev(super, i);
8026 map = get_imsm_map(dev, MAP_0);
8027 map2 = get_imsm_map(dev, MAP_1);
8028 level = get_imsm_raid_level(map);
8029
8030 /* do not publish arrays that are in the middle of an
8031 * unsupported migration
8032 */
8033 if (dev->vol.migr_state &&
8034 (migr_type(dev) == MIGR_STATE_CHANGE)) {
8035 pr_err("cannot assemble volume '%.16s': unsupported migration in progress\n",
8036 dev->volume);
8037 continue;
8038 }
8039 /* do not publish arrays that are not support by controller's
8040 * OROM/EFI
8041 */
8042
8043 this = xmalloc(sizeof(*this));
8044
8045 super->current_vol = i;
8046 getinfo_super_imsm_volume(st, this, NULL);
8047 this->next = rest;
8048 chunk = __le16_to_cpu(map->blocks_per_strip) >> 1;
8049 /* mdadm does not support all metadata features- set the bit in all arrays state */
8050 if (!validate_geometry_imsm_orom(super,
8051 level, /* RAID level */
8052 imsm_level_to_layout(level),
8053 map->num_members, /* raid disks */
8054 &chunk, imsm_dev_size(dev),
8055 1 /* verbose */)) {
8056 pr_err("IMSM RAID geometry validation failed. Array %s activation is blocked.\n",
8057 dev->volume);
8058 this->array.state |=
8059 (1<<MD_SB_BLOCK_CONTAINER_RESHAPE) |
8060 (1<<MD_SB_BLOCK_VOLUME);
8061 }
8062
8063 /* if array has bad blocks, set suitable bit in all arrays state */
8064 if (sb_errors)
8065 this->array.state |=
8066 (1<<MD_SB_BLOCK_CONTAINER_RESHAPE) |
8067 (1<<MD_SB_BLOCK_VOLUME);
8068
8069 for (slot = 0 ; slot < map->num_members; slot++) {
8070 unsigned long long recovery_start;
8071 struct mdinfo *info_d;
8072 struct dl *d;
8073 int idx;
8074 int skip;
8075 __u32 ord;
8076 int missing = 0;
8077
8078 skip = 0;
8079 idx = get_imsm_disk_idx(dev, slot, MAP_0);
8080 ord = get_imsm_ord_tbl_ent(dev, slot, MAP_X);
8081 for (d = super->disks; d ; d = d->next)
8082 if (d->index == idx)
8083 break;
8084
8085 recovery_start = MaxSector;
8086 if (d == NULL)
8087 skip = 1;
8088 if (d && is_failed(&d->disk))
8089 skip = 1;
8090 if (!skip && (ord & IMSM_ORD_REBUILD))
8091 recovery_start = 0;
8092 if (!(ord & IMSM_ORD_REBUILD))
8093 this->array.working_disks++;
8094 /*
8095 * if we skip some disks the array will be assmebled degraded;
8096 * reset resync start to avoid a dirty-degraded
8097 * situation when performing the intial sync
8098 */
8099 if (skip)
8100 missing++;
8101
8102 if (!(dev->vol.dirty & RAIDVOL_DIRTY)) {
8103 if ((!able_to_resync(level, missing) ||
8104 recovery_start == 0))
8105 this->resync_start = MaxSector;
8106 } else {
8107 /*
8108 * FIXME handle dirty degraded
8109 */
8110 }
8111
8112 if (skip)
8113 continue;
8114
8115 info_d = xcalloc(1, sizeof(*info_d));
8116 info_d->next = this->devs;
8117 this->devs = info_d;
8118
8119 info_d->disk.number = d->index;
8120 info_d->disk.major = d->major;
8121 info_d->disk.minor = d->minor;
8122 info_d->disk.raid_disk = slot;
8123 info_d->recovery_start = recovery_start;
8124 if (map2) {
8125 if (slot < map2->num_members)
8126 info_d->disk.state = (1 << MD_DISK_ACTIVE);
8127 else
8128 this->array.spare_disks++;
8129 } else {
8130 if (slot < map->num_members)
8131 info_d->disk.state = (1 << MD_DISK_ACTIVE);
8132 else
8133 this->array.spare_disks++;
8134 }
8135
8136 info_d->events = __le32_to_cpu(mpb->generation_num);
8137 info_d->data_offset = pba_of_lba0(map);
8138 info_d->component_size = calc_component_size(map, dev);
8139
8140 if (map->raid_level == 5) {
8141 info_d->ppl_sector = this->ppl_sector;
8142 info_d->ppl_size = this->ppl_size;
8143 if (this->consistency_policy == CONSISTENCY_POLICY_PPL &&
8144 recovery_start == 0)
8145 this->resync_start = 0;
8146 }
8147
8148 info_d->bb.supported = 1;
8149 get_volume_badblocks(super->bbm_log, ord_to_idx(ord),
8150 info_d->data_offset,
8151 info_d->component_size,
8152 &info_d->bb);
8153 }
8154 /* now that the disk list is up-to-date fixup recovery_start */
8155 update_recovery_start(super, dev, this);
8156 this->array.spare_disks += spare_disks;
8157
8158 /* check for reshape */
8159 if (this->reshape_active == 1)
8160 recover_backup_imsm(st, this);
8161 rest = this;
8162 }
8163
8164 super->current_vol = current_vol;
8165 return rest;
8166 }
8167
8168 static __u8 imsm_check_degraded(struct intel_super *super, struct imsm_dev *dev,
8169 int failed, int look_in_map)
8170 {
8171 struct imsm_map *map;
8172
8173 map = get_imsm_map(dev, look_in_map);
8174
8175 if (!failed)
8176 return map->map_state == IMSM_T_STATE_UNINITIALIZED ?
8177 IMSM_T_STATE_UNINITIALIZED : IMSM_T_STATE_NORMAL;
8178
8179 switch (get_imsm_raid_level(map)) {
8180 case 0:
8181 return IMSM_T_STATE_FAILED;
8182 break;
8183 case 1:
8184 if (failed < map->num_members)
8185 return IMSM_T_STATE_DEGRADED;
8186 else
8187 return IMSM_T_STATE_FAILED;
8188 break;
8189 case 10:
8190 {
8191 /**
8192 * check to see if any mirrors have failed, otherwise we
8193 * are degraded. Even numbered slots are mirrored on
8194 * slot+1
8195 */
8196 int i;
8197 /* gcc -Os complains that this is unused */
8198 int insync = insync;
8199
8200 for (i = 0; i < map->num_members; i++) {
8201 __u32 ord = get_imsm_ord_tbl_ent(dev, i, MAP_X);
8202 int idx = ord_to_idx(ord);
8203 struct imsm_disk *disk;
8204
8205 /* reset the potential in-sync count on even-numbered
8206 * slots. num_copies is always 2 for imsm raid10
8207 */
8208 if ((i & 1) == 0)
8209 insync = 2;
8210
8211 disk = get_imsm_disk(super, idx);
8212 if (!disk || is_failed(disk) || ord & IMSM_ORD_REBUILD)
8213 insync--;
8214
8215 /* no in-sync disks left in this mirror the
8216 * array has failed
8217 */
8218 if (insync == 0)
8219 return IMSM_T_STATE_FAILED;
8220 }
8221
8222 return IMSM_T_STATE_DEGRADED;
8223 }
8224 case 5:
8225 if (failed < 2)
8226 return IMSM_T_STATE_DEGRADED;
8227 else
8228 return IMSM_T_STATE_FAILED;
8229 break;
8230 default:
8231 break;
8232 }
8233
8234 return map->map_state;
8235 }
8236
8237 static int imsm_count_failed(struct intel_super *super, struct imsm_dev *dev,
8238 int look_in_map)
8239 {
8240 int i;
8241 int failed = 0;
8242 struct imsm_disk *disk;
8243 struct imsm_map *map = get_imsm_map(dev, MAP_0);
8244 struct imsm_map *prev = get_imsm_map(dev, MAP_1);
8245 struct imsm_map *map_for_loop;
8246 __u32 ord;
8247 int idx;
8248 int idx_1;
8249
8250 /* at the beginning of migration we set IMSM_ORD_REBUILD on
8251 * disks that are being rebuilt. New failures are recorded to
8252 * map[0]. So we look through all the disks we started with and
8253 * see if any failures are still present, or if any new ones
8254 * have arrived
8255 */
8256 map_for_loop = map;
8257 if (prev && (map->num_members < prev->num_members))
8258 map_for_loop = prev;
8259
8260 for (i = 0; i < map_for_loop->num_members; i++) {
8261 idx_1 = -255;
8262 /* when MAP_X is passed both maps failures are counted
8263 */
8264 if (prev &&
8265 (look_in_map == MAP_1 || look_in_map == MAP_X) &&
8266 i < prev->num_members) {
8267 ord = __le32_to_cpu(prev->disk_ord_tbl[i]);
8268 idx_1 = ord_to_idx(ord);
8269
8270 disk = get_imsm_disk(super, idx_1);
8271 if (!disk || is_failed(disk) || ord & IMSM_ORD_REBUILD)
8272 failed++;
8273 }
8274 if ((look_in_map == MAP_0 || look_in_map == MAP_X) &&
8275 i < map->num_members) {
8276 ord = __le32_to_cpu(map->disk_ord_tbl[i]);
8277 idx = ord_to_idx(ord);
8278
8279 if (idx != idx_1) {
8280 disk = get_imsm_disk(super, idx);
8281 if (!disk || is_failed(disk) ||
8282 ord & IMSM_ORD_REBUILD)
8283 failed++;
8284 }
8285 }
8286 }
8287
8288 return failed;
8289 }
8290
8291 static int imsm_open_new(struct supertype *c, struct active_array *a,
8292 char *inst)
8293 {
8294 struct intel_super *super = c->sb;
8295 struct imsm_super *mpb = super->anchor;
8296 struct imsm_update_prealloc_bb_mem u;
8297
8298 if (atoi(inst) >= mpb->num_raid_devs) {
8299 pr_err("subarry index %d, out of range\n", atoi(inst));
8300 return -ENODEV;
8301 }
8302
8303 dprintf("imsm: open_new %s\n", inst);
8304 a->info.container_member = atoi(inst);
8305
8306 u.type = update_prealloc_badblocks_mem;
8307 imsm_update_metadata_locally(c, &u, sizeof(u));
8308
8309 return 0;
8310 }
8311
8312 static int is_resyncing(struct imsm_dev *dev)
8313 {
8314 struct imsm_map *migr_map;
8315
8316 if (!dev->vol.migr_state)
8317 return 0;
8318
8319 if (migr_type(dev) == MIGR_INIT ||
8320 migr_type(dev) == MIGR_REPAIR)
8321 return 1;
8322
8323 if (migr_type(dev) == MIGR_GEN_MIGR)
8324 return 0;
8325
8326 migr_map = get_imsm_map(dev, MAP_1);
8327
8328 if (migr_map->map_state == IMSM_T_STATE_NORMAL &&
8329 dev->vol.migr_type != MIGR_GEN_MIGR)
8330 return 1;
8331 else
8332 return 0;
8333 }
8334
8335 /* return true if we recorded new information */
8336 static int mark_failure(struct intel_super *super,
8337 struct imsm_dev *dev, struct imsm_disk *disk, int idx)
8338 {
8339 __u32 ord;
8340 int slot;
8341 struct imsm_map *map;
8342 char buf[MAX_RAID_SERIAL_LEN+3];
8343 unsigned int len, shift = 0;
8344
8345 /* new failures are always set in map[0] */
8346 map = get_imsm_map(dev, MAP_0);
8347
8348 slot = get_imsm_disk_slot(map, idx);
8349 if (slot < 0)
8350 return 0;
8351
8352 ord = __le32_to_cpu(map->disk_ord_tbl[slot]);
8353 if (is_failed(disk) && (ord & IMSM_ORD_REBUILD))
8354 return 0;
8355
8356 memcpy(buf, disk->serial, MAX_RAID_SERIAL_LEN);
8357 buf[MAX_RAID_SERIAL_LEN] = '\000';
8358 strcat(buf, ":0");
8359 if ((len = strlen(buf)) >= MAX_RAID_SERIAL_LEN)
8360 shift = len - MAX_RAID_SERIAL_LEN + 1;
8361 memcpy(disk->serial, &buf[shift], len + 1 - shift);
8362
8363 disk->status |= FAILED_DISK;
8364 set_imsm_ord_tbl_ent(map, slot, idx | IMSM_ORD_REBUILD);
8365 /* mark failures in second map if second map exists and this disk
8366 * in this slot.
8367 * This is valid for migration, initialization and rebuild
8368 */
8369 if (dev->vol.migr_state) {
8370 struct imsm_map *map2 = get_imsm_map(dev, MAP_1);
8371 int slot2 = get_imsm_disk_slot(map2, idx);
8372
8373 if (slot2 < map2->num_members && slot2 >= 0)
8374 set_imsm_ord_tbl_ent(map2, slot2,
8375 idx | IMSM_ORD_REBUILD);
8376 }
8377 if (map->failed_disk_num == 0xff ||
8378 (!is_rebuilding(dev) && map->failed_disk_num > slot))
8379 map->failed_disk_num = slot;
8380
8381 clear_disk_badblocks(super->bbm_log, ord_to_idx(ord));
8382
8383 return 1;
8384 }
8385
8386 static void mark_missing(struct intel_super *super,
8387 struct imsm_dev *dev, struct imsm_disk *disk, int idx)
8388 {
8389 mark_failure(super, dev, disk, idx);
8390
8391 if (disk->scsi_id == __cpu_to_le32(~(__u32)0))
8392 return;
8393
8394 disk->scsi_id = __cpu_to_le32(~(__u32)0);
8395 memmove(&disk->serial[0], &disk->serial[1], MAX_RAID_SERIAL_LEN - 1);
8396 }
8397
8398 static void handle_missing(struct intel_super *super, struct imsm_dev *dev)
8399 {
8400 struct dl *dl;
8401
8402 if (!super->missing)
8403 return;
8404
8405 /* When orom adds replacement for missing disk it does
8406 * not remove entry of missing disk, but just updates map with
8407 * new added disk. So it is not enough just to test if there is
8408 * any missing disk, we have to look if there are any failed disks
8409 * in map to stop migration */
8410
8411 dprintf("imsm: mark missing\n");
8412 /* end process for initialization and rebuild only
8413 */
8414 if (is_gen_migration(dev) == 0) {
8415 int failed = imsm_count_failed(super, dev, MAP_0);
8416
8417 if (failed) {
8418 __u8 map_state;
8419 struct imsm_map *map = get_imsm_map(dev, MAP_0);
8420 struct imsm_map *map1;
8421 int i, ord, ord_map1;
8422 int rebuilt = 1;
8423
8424 for (i = 0; i < map->num_members; i++) {
8425 ord = get_imsm_ord_tbl_ent(dev, i, MAP_0);
8426 if (!(ord & IMSM_ORD_REBUILD))
8427 continue;
8428
8429 map1 = get_imsm_map(dev, MAP_1);
8430 if (!map1)
8431 continue;
8432
8433 ord_map1 = __le32_to_cpu(map1->disk_ord_tbl[i]);
8434 if (ord_map1 & IMSM_ORD_REBUILD)
8435 rebuilt = 0;
8436 }
8437
8438 if (rebuilt) {
8439 map_state = imsm_check_degraded(super, dev,
8440 failed, MAP_0);
8441 end_migration(dev, super, map_state);
8442 }
8443 }
8444 }
8445 for (dl = super->missing; dl; dl = dl->next)
8446 mark_missing(super, dev, &dl->disk, dl->index);
8447 super->updates_pending++;
8448 }
8449
8450 static unsigned long long imsm_set_array_size(struct imsm_dev *dev,
8451 long long new_size)
8452 {
8453 unsigned long long array_blocks;
8454 struct imsm_map *map = get_imsm_map(dev, MAP_0);
8455 int used_disks = imsm_num_data_members(map);
8456
8457 if (used_disks == 0) {
8458 /* when problems occures
8459 * return current array_blocks value
8460 */
8461 array_blocks = imsm_dev_size(dev);
8462
8463 return array_blocks;
8464 }
8465
8466 /* set array size in metadata
8467 */
8468 if (new_size <= 0)
8469 /* OLCE size change is caused by added disks
8470 */
8471 array_blocks = per_dev_array_size(map) * used_disks;
8472 else
8473 /* Online Volume Size Change
8474 * Using available free space
8475 */
8476 array_blocks = new_size;
8477
8478 array_blocks = round_size_to_mb(array_blocks, used_disks);
8479 set_imsm_dev_size(dev, array_blocks);
8480
8481 return array_blocks;
8482 }
8483
8484 static void imsm_set_disk(struct active_array *a, int n, int state);
8485
8486 static void imsm_progress_container_reshape(struct intel_super *super)
8487 {
8488 /* if no device has a migr_state, but some device has a
8489 * different number of members than the previous device, start
8490 * changing the number of devices in this device to match
8491 * previous.
8492 */
8493 struct imsm_super *mpb = super->anchor;
8494 int prev_disks = -1;
8495 int i;
8496 int copy_map_size;
8497
8498 for (i = 0; i < mpb->num_raid_devs; i++) {
8499 struct imsm_dev *dev = get_imsm_dev(super, i);
8500 struct imsm_map *map = get_imsm_map(dev, MAP_0);
8501 struct imsm_map *map2;
8502 int prev_num_members;
8503
8504 if (dev->vol.migr_state)
8505 return;
8506
8507 if (prev_disks == -1)
8508 prev_disks = map->num_members;
8509 if (prev_disks == map->num_members)
8510 continue;
8511
8512 /* OK, this array needs to enter reshape mode.
8513 * i.e it needs a migr_state
8514 */
8515
8516 copy_map_size = sizeof_imsm_map(map);
8517 prev_num_members = map->num_members;
8518 map->num_members = prev_disks;
8519 dev->vol.migr_state = 1;
8520 set_vol_curr_migr_unit(dev, 0);
8521 set_migr_type(dev, MIGR_GEN_MIGR);
8522 for (i = prev_num_members;
8523 i < map->num_members; i++)
8524 set_imsm_ord_tbl_ent(map, i, i);
8525 map2 = get_imsm_map(dev, MAP_1);
8526 /* Copy the current map */
8527 memcpy(map2, map, copy_map_size);
8528 map2->num_members = prev_num_members;
8529
8530 imsm_set_array_size(dev, -1);
8531 super->clean_migration_record_by_mdmon = 1;
8532 super->updates_pending++;
8533 }
8534 }
8535
8536 /* Handle dirty -> clean transititions, resync and reshape. Degraded and rebuild
8537 * states are handled in imsm_set_disk() with one exception, when a
8538 * resync is stopped due to a new failure this routine will set the
8539 * 'degraded' state for the array.
8540 */
8541 static int imsm_set_array_state(struct active_array *a, int consistent)
8542 {
8543 int inst = a->info.container_member;
8544 struct intel_super *super = a->container->sb;
8545 struct imsm_dev *dev = get_imsm_dev(super, inst);
8546 struct imsm_map *map = get_imsm_map(dev, MAP_0);
8547 int failed = imsm_count_failed(super, dev, MAP_0);
8548 __u8 map_state = imsm_check_degraded(super, dev, failed, MAP_0);
8549 __u32 blocks_per_unit;
8550
8551 if (dev->vol.migr_state &&
8552 dev->vol.migr_type == MIGR_GEN_MIGR) {
8553 /* array state change is blocked due to reshape action
8554 * We might need to
8555 * - abort the reshape (if last_checkpoint is 0 and action!= reshape)
8556 * - finish the reshape (if last_checkpoint is big and action != reshape)
8557 * - update vol_curr_migr_unit
8558 */
8559 if (a->curr_action == reshape) {
8560 /* still reshaping, maybe update vol_curr_migr_unit */
8561 goto mark_checkpoint;
8562 } else {
8563 if (a->last_checkpoint == 0 && a->prev_action == reshape) {
8564 /* for some reason we aborted the reshape.
8565 *
8566 * disable automatic metadata rollback
8567 * user action is required to recover process
8568 */
8569 if (0) {
8570 struct imsm_map *map2 =
8571 get_imsm_map(dev, MAP_1);
8572 dev->vol.migr_state = 0;
8573 set_migr_type(dev, 0);
8574 set_vol_curr_migr_unit(dev, 0);
8575 memcpy(map, map2,
8576 sizeof_imsm_map(map2));
8577 super->updates_pending++;
8578 }
8579 }
8580 if (a->last_checkpoint >= a->info.component_size) {
8581 unsigned long long array_blocks;
8582 int used_disks;
8583 struct mdinfo *mdi;
8584
8585 used_disks = imsm_num_data_members(map);
8586 if (used_disks > 0) {
8587 array_blocks =
8588 per_dev_array_size(map) *
8589 used_disks;
8590 array_blocks =
8591 round_size_to_mb(array_blocks,
8592 used_disks);
8593 a->info.custom_array_size = array_blocks;
8594 /* encourage manager to update array
8595 * size
8596 */
8597
8598 a->check_reshape = 1;
8599 }
8600 /* finalize online capacity expansion/reshape */
8601 for (mdi = a->info.devs; mdi; mdi = mdi->next)
8602 imsm_set_disk(a,
8603 mdi->disk.raid_disk,
8604 mdi->curr_state);
8605
8606 imsm_progress_container_reshape(super);
8607 }
8608 }
8609 }
8610
8611 /* before we activate this array handle any missing disks */
8612 if (consistent == 2)
8613 handle_missing(super, dev);
8614
8615 if (consistent == 2 &&
8616 (!is_resync_complete(&a->info) ||
8617 map_state != IMSM_T_STATE_NORMAL ||
8618 dev->vol.migr_state))
8619 consistent = 0;
8620
8621 if (is_resync_complete(&a->info)) {
8622 /* complete intialization / resync,
8623 * recovery and interrupted recovery is completed in
8624 * ->set_disk
8625 */
8626 if (is_resyncing(dev)) {
8627 dprintf("imsm: mark resync done\n");
8628 end_migration(dev, super, map_state);
8629 super->updates_pending++;
8630 a->last_checkpoint = 0;
8631 }
8632 } else if ((!is_resyncing(dev) && !failed) &&
8633 (imsm_reshape_blocks_arrays_changes(super) == 0)) {
8634 /* mark the start of the init process if nothing is failed */
8635 dprintf("imsm: mark resync start\n");
8636 if (map->map_state == IMSM_T_STATE_UNINITIALIZED)
8637 migrate(dev, super, IMSM_T_STATE_NORMAL, MIGR_INIT);
8638 else
8639 migrate(dev, super, IMSM_T_STATE_NORMAL, MIGR_REPAIR);
8640 super->updates_pending++;
8641 }
8642
8643 mark_checkpoint:
8644 /* skip checkpointing for general migration,
8645 * it is controlled in mdadm
8646 */
8647 if (is_gen_migration(dev))
8648 goto skip_mark_checkpoint;
8649
8650 /* check if we can update vol_curr_migr_unit from resync_start,
8651 * recovery_start
8652 */
8653 blocks_per_unit = blocks_per_migr_unit(super, dev);
8654 if (blocks_per_unit) {
8655 set_vol_curr_migr_unit(dev,
8656 a->last_checkpoint / blocks_per_unit);
8657 dprintf("imsm: mark checkpoint (%llu)\n",
8658 vol_curr_migr_unit(dev));
8659 super->updates_pending++;
8660 }
8661
8662 skip_mark_checkpoint:
8663 /* mark dirty / clean */
8664 if (((dev->vol.dirty & RAIDVOL_DIRTY) && consistent) ||
8665 (!(dev->vol.dirty & RAIDVOL_DIRTY) && !consistent)) {
8666 dprintf("imsm: mark '%s'\n", consistent ? "clean" : "dirty");
8667 if (consistent) {
8668 dev->vol.dirty = RAIDVOL_CLEAN;
8669 } else {
8670 dev->vol.dirty = RAIDVOL_DIRTY;
8671 if (dev->rwh_policy == RWH_DISTRIBUTED ||
8672 dev->rwh_policy == RWH_MULTIPLE_DISTRIBUTED)
8673 dev->vol.dirty |= RAIDVOL_DSRECORD_VALID;
8674 }
8675 super->updates_pending++;
8676 }
8677
8678 return consistent;
8679 }
8680
8681 static int imsm_disk_slot_to_ord(struct active_array *a, int slot)
8682 {
8683 int inst = a->info.container_member;
8684 struct intel_super *super = a->container->sb;
8685 struct imsm_dev *dev = get_imsm_dev(super, inst);
8686 struct imsm_map *map = get_imsm_map(dev, MAP_0);
8687
8688 if (slot > map->num_members) {
8689 pr_err("imsm: imsm_disk_slot_to_ord %d out of range 0..%d\n",
8690 slot, map->num_members - 1);
8691 return -1;
8692 }
8693
8694 if (slot < 0)
8695 return -1;
8696
8697 return get_imsm_ord_tbl_ent(dev, slot, MAP_0);
8698 }
8699
8700 static void imsm_set_disk(struct active_array *a, int n, int state)
8701 {
8702 int inst = a->info.container_member;
8703 struct intel_super *super = a->container->sb;
8704 struct imsm_dev *dev = get_imsm_dev(super, inst);
8705 struct imsm_map *map = get_imsm_map(dev, MAP_0);
8706 struct imsm_disk *disk;
8707 struct mdinfo *mdi;
8708 int recovery_not_finished = 0;
8709 int failed;
8710 int ord;
8711 __u8 map_state;
8712 int rebuild_done = 0;
8713 int i;
8714
8715 ord = get_imsm_ord_tbl_ent(dev, n, MAP_X);
8716 if (ord < 0)
8717 return;
8718
8719 dprintf("imsm: set_disk %d:%x\n", n, state);
8720 disk = get_imsm_disk(super, ord_to_idx(ord));
8721
8722 /* check for new failures */
8723 if (disk && (state & DS_FAULTY)) {
8724 if (mark_failure(super, dev, disk, ord_to_idx(ord)))
8725 super->updates_pending++;
8726 }
8727
8728 /* check if in_sync */
8729 if (state & DS_INSYNC && ord & IMSM_ORD_REBUILD && is_rebuilding(dev)) {
8730 struct imsm_map *migr_map = get_imsm_map(dev, MAP_1);
8731
8732 set_imsm_ord_tbl_ent(migr_map, n, ord_to_idx(ord));
8733 rebuild_done = 1;
8734 super->updates_pending++;
8735 }
8736
8737 failed = imsm_count_failed(super, dev, MAP_0);
8738 map_state = imsm_check_degraded(super, dev, failed, MAP_0);
8739
8740 /* check if recovery complete, newly degraded, or failed */
8741 dprintf("imsm: Detected transition to state ");
8742 switch (map_state) {
8743 case IMSM_T_STATE_NORMAL: /* transition to normal state */
8744 dprintf("normal: ");
8745 if (is_rebuilding(dev)) {
8746 dprintf_cont("while rebuilding");
8747 /* check if recovery is really finished */
8748 for (mdi = a->info.devs; mdi ; mdi = mdi->next)
8749 if (mdi->recovery_start != MaxSector) {
8750 recovery_not_finished = 1;
8751 break;
8752 }
8753 if (recovery_not_finished) {
8754 dprintf_cont("\n");
8755 dprintf("Rebuild has not finished yet, state not changed");
8756 if (a->last_checkpoint < mdi->recovery_start) {
8757 a->last_checkpoint = mdi->recovery_start;
8758 super->updates_pending++;
8759 }
8760 break;
8761 }
8762 end_migration(dev, super, map_state);
8763 map->failed_disk_num = ~0;
8764 super->updates_pending++;
8765 a->last_checkpoint = 0;
8766 break;
8767 }
8768 if (is_gen_migration(dev)) {
8769 dprintf_cont("while general migration");
8770 if (a->last_checkpoint >= a->info.component_size)
8771 end_migration(dev, super, map_state);
8772 else
8773 map->map_state = map_state;
8774 map->failed_disk_num = ~0;
8775 super->updates_pending++;
8776 break;
8777 }
8778 break;
8779 case IMSM_T_STATE_DEGRADED: /* transition to degraded state */
8780 dprintf_cont("degraded: ");
8781 if (map->map_state != map_state && !dev->vol.migr_state) {
8782 dprintf_cont("mark degraded");
8783 map->map_state = map_state;
8784 super->updates_pending++;
8785 a->last_checkpoint = 0;
8786 break;
8787 }
8788 if (is_rebuilding(dev)) {
8789 dprintf_cont("while rebuilding ");
8790 if (state & DS_FAULTY) {
8791 dprintf_cont("removing failed drive ");
8792 if (n == map->failed_disk_num) {
8793 dprintf_cont("end migration");
8794 end_migration(dev, super, map_state);
8795 a->last_checkpoint = 0;
8796 } else {
8797 dprintf_cont("fail detected during rebuild, changing map state");
8798 map->map_state = map_state;
8799 }
8800 super->updates_pending++;
8801 }
8802
8803 if (!rebuild_done)
8804 break;
8805
8806 /* check if recovery is really finished */
8807 for (mdi = a->info.devs; mdi ; mdi = mdi->next)
8808 if (mdi->recovery_start != MaxSector) {
8809 recovery_not_finished = 1;
8810 break;
8811 }
8812 if (recovery_not_finished) {
8813 dprintf_cont("\n");
8814 dprintf_cont("Rebuild has not finished yet");
8815 if (a->last_checkpoint < mdi->recovery_start) {
8816 a->last_checkpoint =
8817 mdi->recovery_start;
8818 super->updates_pending++;
8819 }
8820 break;
8821 }
8822
8823 dprintf_cont(" Rebuild done, still degraded");
8824 end_migration(dev, super, map_state);
8825 a->last_checkpoint = 0;
8826 super->updates_pending++;
8827
8828 for (i = 0; i < map->num_members; i++) {
8829 int idx = get_imsm_ord_tbl_ent(dev, i, MAP_0);
8830
8831 if (idx & IMSM_ORD_REBUILD)
8832 map->failed_disk_num = i;
8833 }
8834 super->updates_pending++;
8835 break;
8836 }
8837 if (is_gen_migration(dev)) {
8838 dprintf_cont("while general migration");
8839 if (a->last_checkpoint >= a->info.component_size)
8840 end_migration(dev, super, map_state);
8841 else {
8842 map->map_state = map_state;
8843 manage_second_map(super, dev);
8844 }
8845 super->updates_pending++;
8846 break;
8847 }
8848 if (is_initializing(dev)) {
8849 dprintf_cont("while initialization.");
8850 map->map_state = map_state;
8851 super->updates_pending++;
8852 break;
8853 }
8854 break;
8855 case IMSM_T_STATE_FAILED: /* transition to failed state */
8856 dprintf_cont("failed: ");
8857 if (is_gen_migration(dev)) {
8858 dprintf_cont("while general migration");
8859 map->map_state = map_state;
8860 super->updates_pending++;
8861 break;
8862 }
8863 if (map->map_state != map_state) {
8864 dprintf_cont("mark failed");
8865 end_migration(dev, super, map_state);
8866 super->updates_pending++;
8867 a->last_checkpoint = 0;
8868 break;
8869 }
8870 break;
8871 default:
8872 dprintf_cont("state %i\n", map_state);
8873 }
8874 dprintf_cont("\n");
8875 }
8876
8877 static int store_imsm_mpb(int fd, struct imsm_super *mpb)
8878 {
8879 void *buf = mpb;
8880 __u32 mpb_size = __le32_to_cpu(mpb->mpb_size);
8881 unsigned long long dsize;
8882 unsigned long long sectors;
8883 unsigned int sector_size;
8884
8885 get_dev_sector_size(fd, NULL, &sector_size);
8886 get_dev_size(fd, NULL, &dsize);
8887
8888 if (mpb_size > sector_size) {
8889 /* -1 to account for anchor */
8890 sectors = mpb_sectors(mpb, sector_size) - 1;
8891
8892 /* write the extended mpb to the sectors preceeding the anchor */
8893 if (lseek64(fd, dsize - (sector_size * (2 + sectors)),
8894 SEEK_SET) < 0)
8895 return 1;
8896
8897 if ((unsigned long long)write(fd, buf + sector_size,
8898 sector_size * sectors) != sector_size * sectors)
8899 return 1;
8900 }
8901
8902 /* first block is stored on second to last sector of the disk */
8903 if (lseek64(fd, dsize - (sector_size * 2), SEEK_SET) < 0)
8904 return 1;
8905
8906 if ((unsigned int)write(fd, buf, sector_size) != sector_size)
8907 return 1;
8908
8909 return 0;
8910 }
8911
8912 static void imsm_sync_metadata(struct supertype *container)
8913 {
8914 struct intel_super *super = container->sb;
8915
8916 dprintf("sync metadata: %d\n", super->updates_pending);
8917 if (!super->updates_pending)
8918 return;
8919
8920 write_super_imsm(container, 0);
8921
8922 super->updates_pending = 0;
8923 }
8924
8925 static struct dl *imsm_readd(struct intel_super *super, int idx, struct active_array *a)
8926 {
8927 struct imsm_dev *dev = get_imsm_dev(super, a->info.container_member);
8928 int i = get_imsm_disk_idx(dev, idx, MAP_X);
8929 struct dl *dl;
8930
8931 for (dl = super->disks; dl; dl = dl->next)
8932 if (dl->index == i)
8933 break;
8934
8935 if (dl && is_failed(&dl->disk))
8936 dl = NULL;
8937
8938 if (dl)
8939 dprintf("found %x:%x\n", dl->major, dl->minor);
8940
8941 return dl;
8942 }
8943
8944 static struct dl *imsm_add_spare(struct intel_super *super, int slot,
8945 struct active_array *a, int activate_new,
8946 struct mdinfo *additional_test_list)
8947 {
8948 struct imsm_dev *dev = get_imsm_dev(super, a->info.container_member);
8949 int idx = get_imsm_disk_idx(dev, slot, MAP_X);
8950 struct imsm_super *mpb = super->anchor;
8951 struct imsm_map *map;
8952 unsigned long long pos;
8953 struct mdinfo *d;
8954 struct extent *ex;
8955 int i, j;
8956 int found;
8957 __u32 array_start = 0;
8958 __u32 array_end = 0;
8959 struct dl *dl;
8960 struct mdinfo *test_list;
8961
8962 for (dl = super->disks; dl; dl = dl->next) {
8963 /* If in this array, skip */
8964 for (d = a->info.devs ; d ; d = d->next)
8965 if (d->state_fd >= 0 &&
8966 d->disk.major == dl->major &&
8967 d->disk.minor == dl->minor) {
8968 dprintf("%x:%x already in array\n",
8969 dl->major, dl->minor);
8970 break;
8971 }
8972 if (d)
8973 continue;
8974 test_list = additional_test_list;
8975 while (test_list) {
8976 if (test_list->disk.major == dl->major &&
8977 test_list->disk.minor == dl->minor) {
8978 dprintf("%x:%x already in additional test list\n",
8979 dl->major, dl->minor);
8980 break;
8981 }
8982 test_list = test_list->next;
8983 }
8984 if (test_list)
8985 continue;
8986
8987 /* skip in use or failed drives */
8988 if (is_failed(&dl->disk) || idx == dl->index ||
8989 dl->index == -2) {
8990 dprintf("%x:%x status (failed: %d index: %d)\n",
8991 dl->major, dl->minor, is_failed(&dl->disk), idx);
8992 continue;
8993 }
8994
8995 /* skip pure spares when we are looking for partially
8996 * assimilated drives
8997 */
8998 if (dl->index == -1 && !activate_new)
8999 continue;
9000
9001 if (!drive_validate_sector_size(super, dl))
9002 continue;
9003
9004 /* Does this unused device have the requisite free space?
9005 * It needs to be able to cover all member volumes
9006 */
9007 ex = get_extents(super, dl, 1);
9008 if (!ex) {
9009 dprintf("cannot get extents\n");
9010 continue;
9011 }
9012 for (i = 0; i < mpb->num_raid_devs; i++) {
9013 dev = get_imsm_dev(super, i);
9014 map = get_imsm_map(dev, MAP_0);
9015
9016 /* check if this disk is already a member of
9017 * this array
9018 */
9019 if (get_imsm_disk_slot(map, dl->index) >= 0)
9020 continue;
9021
9022 found = 0;
9023 j = 0;
9024 pos = 0;
9025 array_start = pba_of_lba0(map);
9026 array_end = array_start +
9027 per_dev_array_size(map) - 1;
9028
9029 do {
9030 /* check that we can start at pba_of_lba0 with
9031 * num_data_stripes*blocks_per_stripe of space
9032 */
9033 if (array_start >= pos && array_end < ex[j].start) {
9034 found = 1;
9035 break;
9036 }
9037 pos = ex[j].start + ex[j].size;
9038 j++;
9039 } while (ex[j-1].size);
9040
9041 if (!found)
9042 break;
9043 }
9044
9045 free(ex);
9046 if (i < mpb->num_raid_devs) {
9047 dprintf("%x:%x does not have %u to %u available\n",
9048 dl->major, dl->minor, array_start, array_end);
9049 /* No room */
9050 continue;
9051 }
9052 return dl;
9053 }
9054
9055 return dl;
9056 }
9057
9058 static int imsm_rebuild_allowed(struct supertype *cont, int dev_idx, int failed)
9059 {
9060 struct imsm_dev *dev2;
9061 struct imsm_map *map;
9062 struct dl *idisk;
9063 int slot;
9064 int idx;
9065 __u8 state;
9066
9067 dev2 = get_imsm_dev(cont->sb, dev_idx);
9068 if (dev2) {
9069 state = imsm_check_degraded(cont->sb, dev2, failed, MAP_0);
9070 if (state == IMSM_T_STATE_FAILED) {
9071 map = get_imsm_map(dev2, MAP_0);
9072 if (!map)
9073 return 1;
9074 for (slot = 0; slot < map->num_members; slot++) {
9075 /*
9076 * Check if failed disks are deleted from intel
9077 * disk list or are marked to be deleted
9078 */
9079 idx = get_imsm_disk_idx(dev2, slot, MAP_X);
9080 idisk = get_imsm_dl_disk(cont->sb, idx);
9081 /*
9082 * Do not rebuild the array if failed disks
9083 * from failed sub-array are not removed from
9084 * container.
9085 */
9086 if (idisk &&
9087 is_failed(&idisk->disk) &&
9088 (idisk->action != DISK_REMOVE))
9089 return 0;
9090 }
9091 }
9092 }
9093 return 1;
9094 }
9095
9096 static struct mdinfo *imsm_activate_spare(struct active_array *a,
9097 struct metadata_update **updates)
9098 {
9099 /**
9100 * Find a device with unused free space and use it to replace a
9101 * failed/vacant region in an array. We replace failed regions one a
9102 * array at a time. The result is that a new spare disk will be added
9103 * to the first failed array and after the monitor has finished
9104 * propagating failures the remainder will be consumed.
9105 *
9106 * FIXME add a capability for mdmon to request spares from another
9107 * container.
9108 */
9109
9110 struct intel_super *super = a->container->sb;
9111 int inst = a->info.container_member;
9112 struct imsm_dev *dev = get_imsm_dev(super, inst);
9113 struct imsm_map *map = get_imsm_map(dev, MAP_0);
9114 int failed = a->info.array.raid_disks;
9115 struct mdinfo *rv = NULL;
9116 struct mdinfo *d;
9117 struct mdinfo *di;
9118 struct metadata_update *mu;
9119 struct dl *dl;
9120 struct imsm_update_activate_spare *u;
9121 int num_spares = 0;
9122 int i;
9123 int allowed;
9124
9125 for (d = a->info.devs ; d ; d = d->next) {
9126 if ((d->curr_state & DS_FAULTY) &&
9127 d->state_fd >= 0)
9128 /* wait for Removal to happen */
9129 return NULL;
9130 if (d->state_fd >= 0)
9131 failed--;
9132 }
9133
9134 dprintf("imsm: activate spare: inst=%d failed=%d (%d) level=%d\n",
9135 inst, failed, a->info.array.raid_disks, a->info.array.level);
9136
9137 if (imsm_reshape_blocks_arrays_changes(super))
9138 return NULL;
9139
9140 /* Cannot activate another spare if rebuild is in progress already
9141 */
9142 if (is_rebuilding(dev)) {
9143 dprintf("imsm: No spare activation allowed. Rebuild in progress already.\n");
9144 return NULL;
9145 }
9146
9147 if (a->info.array.level == 4)
9148 /* No repair for takeovered array
9149 * imsm doesn't support raid4
9150 */
9151 return NULL;
9152
9153 if (imsm_check_degraded(super, dev, failed, MAP_0) !=
9154 IMSM_T_STATE_DEGRADED)
9155 return NULL;
9156
9157 if (get_imsm_map(dev, MAP_0)->map_state == IMSM_T_STATE_UNINITIALIZED) {
9158 dprintf("imsm: No spare activation allowed. Volume is not initialized.\n");
9159 return NULL;
9160 }
9161
9162 /*
9163 * If there are any failed disks check state of the other volume.
9164 * Block rebuild if the another one is failed until failed disks
9165 * are removed from container.
9166 */
9167 if (failed) {
9168 dprintf("found failed disks in %.*s, check if there anotherfailed sub-array.\n",
9169 MAX_RAID_SERIAL_LEN, dev->volume);
9170 /* check if states of the other volumes allow for rebuild */
9171 for (i = 0; i < super->anchor->num_raid_devs; i++) {
9172 if (i != inst) {
9173 allowed = imsm_rebuild_allowed(a->container,
9174 i, failed);
9175 if (!allowed)
9176 return NULL;
9177 }
9178 }
9179 }
9180
9181 /* For each slot, if it is not working, find a spare */
9182 for (i = 0; i < a->info.array.raid_disks; i++) {
9183 for (d = a->info.devs ; d ; d = d->next)
9184 if (d->disk.raid_disk == i)
9185 break;
9186 dprintf("found %d: %p %x\n", i, d, d?d->curr_state:0);
9187 if (d && (d->state_fd >= 0))
9188 continue;
9189
9190 /*
9191 * OK, this device needs recovery. Try to re-add the
9192 * previous occupant of this slot, if this fails see if
9193 * we can continue the assimilation of a spare that was
9194 * partially assimilated, finally try to activate a new
9195 * spare.
9196 */
9197 dl = imsm_readd(super, i, a);
9198 if (!dl)
9199 dl = imsm_add_spare(super, i, a, 0, rv);
9200 if (!dl)
9201 dl = imsm_add_spare(super, i, a, 1, rv);
9202 if (!dl)
9203 continue;
9204
9205 /* found a usable disk with enough space */
9206 di = xcalloc(1, sizeof(*di));
9207
9208 /* dl->index will be -1 in the case we are activating a
9209 * pristine spare. imsm_process_update() will create a
9210 * new index in this case. Once a disk is found to be
9211 * failed in all member arrays it is kicked from the
9212 * metadata
9213 */
9214 di->disk.number = dl->index;
9215
9216 /* (ab)use di->devs to store a pointer to the device
9217 * we chose
9218 */
9219 di->devs = (struct mdinfo *) dl;
9220
9221 di->disk.raid_disk = i;
9222 di->disk.major = dl->major;
9223 di->disk.minor = dl->minor;
9224 di->disk.state = 0;
9225 di->recovery_start = 0;
9226 di->data_offset = pba_of_lba0(map);
9227 di->component_size = a->info.component_size;
9228 di->container_member = inst;
9229 di->bb.supported = 1;
9230 if (a->info.consistency_policy == CONSISTENCY_POLICY_PPL) {
9231 di->ppl_sector = get_ppl_sector(super, inst);
9232 di->ppl_size = MULTIPLE_PPL_AREA_SIZE_IMSM >> 9;
9233 }
9234 super->random = random32();
9235 di->next = rv;
9236 rv = di;
9237 num_spares++;
9238 dprintf("%x:%x to be %d at %llu\n", dl->major, dl->minor,
9239 i, di->data_offset);
9240 }
9241
9242 if (!rv)
9243 /* No spares found */
9244 return rv;
9245 /* Now 'rv' has a list of devices to return.
9246 * Create a metadata_update record to update the
9247 * disk_ord_tbl for the array
9248 */
9249 mu = xmalloc(sizeof(*mu));
9250 mu->buf = xcalloc(num_spares,
9251 sizeof(struct imsm_update_activate_spare));
9252 mu->space = NULL;
9253 mu->space_list = NULL;
9254 mu->len = sizeof(struct imsm_update_activate_spare) * num_spares;
9255 mu->next = *updates;
9256 u = (struct imsm_update_activate_spare *) mu->buf;
9257
9258 for (di = rv ; di ; di = di->next) {
9259 u->type = update_activate_spare;
9260 u->dl = (struct dl *) di->devs;
9261 di->devs = NULL;
9262 u->slot = di->disk.raid_disk;
9263 u->array = inst;
9264 u->next = u + 1;
9265 u++;
9266 }
9267 (u-1)->next = NULL;
9268 *updates = mu;
9269
9270 return rv;
9271 }
9272
9273 static int disks_overlap(struct intel_super *super, int idx, struct imsm_update_create_array *u)
9274 {
9275 struct imsm_dev *dev = get_imsm_dev(super, idx);
9276 struct imsm_map *map = get_imsm_map(dev, MAP_0);
9277 struct imsm_map *new_map = get_imsm_map(&u->dev, MAP_0);
9278 struct disk_info *inf = get_disk_info(u);
9279 struct imsm_disk *disk;
9280 int i;
9281 int j;
9282
9283 for (i = 0; i < map->num_members; i++) {
9284 disk = get_imsm_disk(super, get_imsm_disk_idx(dev, i, MAP_X));
9285 for (j = 0; j < new_map->num_members; j++)
9286 if (serialcmp(disk->serial, inf[j].serial) == 0)
9287 return 1;
9288 }
9289
9290 return 0;
9291 }
9292
9293 static struct dl *get_disk_super(struct intel_super *super, int major, int minor)
9294 {
9295 struct dl *dl;
9296
9297 for (dl = super->disks; dl; dl = dl->next)
9298 if (dl->major == major && dl->minor == minor)
9299 return dl;
9300 return NULL;
9301 }
9302
9303 static int remove_disk_super(struct intel_super *super, int major, int minor)
9304 {
9305 struct dl *prev;
9306 struct dl *dl;
9307
9308 prev = NULL;
9309 for (dl = super->disks; dl; dl = dl->next) {
9310 if (dl->major == major && dl->minor == minor) {
9311 /* remove */
9312 if (prev)
9313 prev->next = dl->next;
9314 else
9315 super->disks = dl->next;
9316 dl->next = NULL;
9317 __free_imsm_disk(dl);
9318 dprintf("removed %x:%x\n", major, minor);
9319 break;
9320 }
9321 prev = dl;
9322 }
9323 return 0;
9324 }
9325
9326 static void imsm_delete(struct intel_super *super, struct dl **dlp, unsigned index);
9327
9328 static int add_remove_disk_update(struct intel_super *super)
9329 {
9330 int check_degraded = 0;
9331 struct dl *disk;
9332
9333 /* add/remove some spares to/from the metadata/contrainer */
9334 while (super->disk_mgmt_list) {
9335 struct dl *disk_cfg;
9336
9337 disk_cfg = super->disk_mgmt_list;
9338 super->disk_mgmt_list = disk_cfg->next;
9339 disk_cfg->next = NULL;
9340
9341 if (disk_cfg->action == DISK_ADD) {
9342 disk_cfg->next = super->disks;
9343 super->disks = disk_cfg;
9344 check_degraded = 1;
9345 dprintf("added %x:%x\n",
9346 disk_cfg->major, disk_cfg->minor);
9347 } else if (disk_cfg->action == DISK_REMOVE) {
9348 dprintf("Disk remove action processed: %x.%x\n",
9349 disk_cfg->major, disk_cfg->minor);
9350 disk = get_disk_super(super,
9351 disk_cfg->major,
9352 disk_cfg->minor);
9353 if (disk) {
9354 /* store action status */
9355 disk->action = DISK_REMOVE;
9356 /* remove spare disks only */
9357 if (disk->index == -1) {
9358 remove_disk_super(super,
9359 disk_cfg->major,
9360 disk_cfg->minor);
9361 } else {
9362 disk_cfg->fd = disk->fd;
9363 disk->fd = -1;
9364 }
9365 }
9366 /* release allocate disk structure */
9367 __free_imsm_disk(disk_cfg);
9368 }
9369 }
9370 return check_degraded;
9371 }
9372
9373 static int apply_reshape_migration_update(struct imsm_update_reshape_migration *u,
9374 struct intel_super *super,
9375 void ***space_list)
9376 {
9377 struct intel_dev *id;
9378 void **tofree = NULL;
9379 int ret_val = 0;
9380
9381 dprintf("(enter)\n");
9382 if (u->subdev < 0 || u->subdev > 1) {
9383 dprintf("imsm: Error: Wrong subdev: %i\n", u->subdev);
9384 return ret_val;
9385 }
9386 if (space_list == NULL || *space_list == NULL) {
9387 dprintf("imsm: Error: Memory is not allocated\n");
9388 return ret_val;
9389 }
9390
9391 for (id = super->devlist ; id; id = id->next) {
9392 if (id->index == (unsigned)u->subdev) {
9393 struct imsm_dev *dev = get_imsm_dev(super, u->subdev);
9394 struct imsm_map *map;
9395 struct imsm_dev *new_dev =
9396 (struct imsm_dev *)*space_list;
9397 struct imsm_map *migr_map = get_imsm_map(dev, MAP_1);
9398 int to_state;
9399 struct dl *new_disk;
9400
9401 if (new_dev == NULL)
9402 return ret_val;
9403 *space_list = **space_list;
9404 memcpy(new_dev, dev, sizeof_imsm_dev(dev, 0));
9405 map = get_imsm_map(new_dev, MAP_0);
9406 if (migr_map) {
9407 dprintf("imsm: Error: migration in progress");
9408 return ret_val;
9409 }
9410
9411 to_state = map->map_state;
9412 if ((u->new_level == 5) && (map->raid_level == 0)) {
9413 map->num_members++;
9414 /* this should not happen */
9415 if (u->new_disks[0] < 0) {
9416 map->failed_disk_num =
9417 map->num_members - 1;
9418 to_state = IMSM_T_STATE_DEGRADED;
9419 } else
9420 to_state = IMSM_T_STATE_NORMAL;
9421 }
9422 migrate(new_dev, super, to_state, MIGR_GEN_MIGR);
9423 if (u->new_level > -1)
9424 map->raid_level = u->new_level;
9425 migr_map = get_imsm_map(new_dev, MAP_1);
9426 if ((u->new_level == 5) &&
9427 (migr_map->raid_level == 0)) {
9428 int ord = map->num_members - 1;
9429 migr_map->num_members--;
9430 if (u->new_disks[0] < 0)
9431 ord |= IMSM_ORD_REBUILD;
9432 set_imsm_ord_tbl_ent(map,
9433 map->num_members - 1,
9434 ord);
9435 }
9436 id->dev = new_dev;
9437 tofree = (void **)dev;
9438
9439 /* update chunk size
9440 */
9441 if (u->new_chunksize > 0) {
9442 unsigned long long num_data_stripes;
9443 struct imsm_map *dest_map =
9444 get_imsm_map(dev, MAP_0);
9445 int used_disks =
9446 imsm_num_data_members(dest_map);
9447
9448 if (used_disks == 0)
9449 return ret_val;
9450
9451 map->blocks_per_strip =
9452 __cpu_to_le16(u->new_chunksize * 2);
9453 num_data_stripes =
9454 imsm_dev_size(dev) / used_disks;
9455 num_data_stripes /= map->blocks_per_strip;
9456 num_data_stripes /= map->num_domains;
9457 set_num_data_stripes(map, num_data_stripes);
9458 }
9459
9460 /* ensure blocks_per_member has valid value
9461 */
9462 set_blocks_per_member(map,
9463 per_dev_array_size(map) +
9464 NUM_BLOCKS_DIRTY_STRIPE_REGION);
9465
9466 /* add disk
9467 */
9468 if (u->new_level != 5 || migr_map->raid_level != 0 ||
9469 migr_map->raid_level == map->raid_level)
9470 goto skip_disk_add;
9471
9472 if (u->new_disks[0] >= 0) {
9473 /* use passes spare
9474 */
9475 new_disk = get_disk_super(super,
9476 major(u->new_disks[0]),
9477 minor(u->new_disks[0]));
9478 dprintf("imsm: new disk for reshape is: %i:%i (%p, index = %i)\n",
9479 major(u->new_disks[0]),
9480 minor(u->new_disks[0]),
9481 new_disk, new_disk->index);
9482 if (new_disk == NULL)
9483 goto error_disk_add;
9484
9485 new_disk->index = map->num_members - 1;
9486 /* slot to fill in autolayout
9487 */
9488 new_disk->raiddisk = new_disk->index;
9489 new_disk->disk.status |= CONFIGURED_DISK;
9490 new_disk->disk.status &= ~SPARE_DISK;
9491 } else
9492 goto error_disk_add;
9493
9494 skip_disk_add:
9495 *tofree = *space_list;
9496 /* calculate new size
9497 */
9498 imsm_set_array_size(new_dev, -1);
9499
9500 ret_val = 1;
9501 }
9502 }
9503
9504 if (tofree)
9505 *space_list = tofree;
9506 return ret_val;
9507
9508 error_disk_add:
9509 dprintf("Error: imsm: Cannot find disk.\n");
9510 return ret_val;
9511 }
9512
9513 static int apply_size_change_update(struct imsm_update_size_change *u,
9514 struct intel_super *super)
9515 {
9516 struct intel_dev *id;
9517 int ret_val = 0;
9518
9519 dprintf("(enter)\n");
9520 if (u->subdev < 0 || u->subdev > 1) {
9521 dprintf("imsm: Error: Wrong subdev: %i\n", u->subdev);
9522 return ret_val;
9523 }
9524
9525 for (id = super->devlist ; id; id = id->next) {
9526 if (id->index == (unsigned)u->subdev) {
9527 struct imsm_dev *dev = get_imsm_dev(super, u->subdev);
9528 struct imsm_map *map = get_imsm_map(dev, MAP_0);
9529 int used_disks = imsm_num_data_members(map);
9530 unsigned long long blocks_per_member;
9531 unsigned long long num_data_stripes;
9532 unsigned long long new_size_per_disk;
9533
9534 if (used_disks == 0)
9535 return 0;
9536
9537 /* calculate new size
9538 */
9539 new_size_per_disk = u->new_size / used_disks;
9540 blocks_per_member = new_size_per_disk +
9541 NUM_BLOCKS_DIRTY_STRIPE_REGION;
9542 num_data_stripes = new_size_per_disk /
9543 map->blocks_per_strip;
9544 num_data_stripes /= map->num_domains;
9545 dprintf("(size: %llu, blocks per member: %llu, num_data_stipes: %llu)\n",
9546 u->new_size, new_size_per_disk,
9547 num_data_stripes);
9548 set_blocks_per_member(map, blocks_per_member);
9549 set_num_data_stripes(map, num_data_stripes);
9550 imsm_set_array_size(dev, u->new_size);
9551
9552 ret_val = 1;
9553 break;
9554 }
9555 }
9556
9557 return ret_val;
9558 }
9559
9560 static int prepare_spare_to_activate(struct supertype *st,
9561 struct imsm_update_activate_spare *u)
9562 {
9563 struct intel_super *super = st->sb;
9564 int prev_current_vol = super->current_vol;
9565 struct active_array *a;
9566 int ret = 1;
9567
9568 for (a = st->arrays; a; a = a->next)
9569 /*
9570 * Additional initialization (adding bitmap header, filling
9571 * the bitmap area with '1's to force initial rebuild for a whole
9572 * data-area) is required when adding the spare to the volume
9573 * with write-intent bitmap.
9574 */
9575 if (a->info.container_member == u->array &&
9576 a->info.consistency_policy == CONSISTENCY_POLICY_BITMAP) {
9577 struct dl *dl;
9578
9579 for (dl = super->disks; dl; dl = dl->next)
9580 if (dl == u->dl)
9581 break;
9582 if (!dl)
9583 break;
9584
9585 super->current_vol = u->array;
9586 if (st->ss->write_bitmap(st, dl->fd, NoUpdate))
9587 ret = 0;
9588 super->current_vol = prev_current_vol;
9589 }
9590 return ret;
9591 }
9592
9593 static int apply_update_activate_spare(struct imsm_update_activate_spare *u,
9594 struct intel_super *super,
9595 struct active_array *active_array)
9596 {
9597 struct imsm_super *mpb = super->anchor;
9598 struct imsm_dev *dev = get_imsm_dev(super, u->array);
9599 struct imsm_map *map = get_imsm_map(dev, MAP_0);
9600 struct imsm_map *migr_map;
9601 struct active_array *a;
9602 struct imsm_disk *disk;
9603 __u8 to_state;
9604 struct dl *dl;
9605 unsigned int found;
9606 int failed;
9607 int victim;
9608 int i;
9609 int second_map_created = 0;
9610
9611 for (; u; u = u->next) {
9612 victim = get_imsm_disk_idx(dev, u->slot, MAP_X);
9613
9614 if (victim < 0)
9615 return 0;
9616
9617 for (dl = super->disks; dl; dl = dl->next)
9618 if (dl == u->dl)
9619 break;
9620
9621 if (!dl) {
9622 pr_err("error: imsm_activate_spare passed an unknown disk (index: %d)\n",
9623 u->dl->index);
9624 return 0;
9625 }
9626
9627 /* count failures (excluding rebuilds and the victim)
9628 * to determine map[0] state
9629 */
9630 failed = 0;
9631 for (i = 0; i < map->num_members; i++) {
9632 if (i == u->slot)
9633 continue;
9634 disk = get_imsm_disk(super,
9635 get_imsm_disk_idx(dev, i, MAP_X));
9636 if (!disk || is_failed(disk))
9637 failed++;
9638 }
9639
9640 /* adding a pristine spare, assign a new index */
9641 if (dl->index < 0) {
9642 dl->index = super->anchor->num_disks;
9643 super->anchor->num_disks++;
9644 }
9645 disk = &dl->disk;
9646 disk->status |= CONFIGURED_DISK;
9647 disk->status &= ~SPARE_DISK;
9648
9649 /* mark rebuild */
9650 to_state = imsm_check_degraded(super, dev, failed, MAP_0);
9651 if (!second_map_created) {
9652 second_map_created = 1;
9653 map->map_state = IMSM_T_STATE_DEGRADED;
9654 migrate(dev, super, to_state, MIGR_REBUILD);
9655 } else
9656 map->map_state = to_state;
9657 migr_map = get_imsm_map(dev, MAP_1);
9658 set_imsm_ord_tbl_ent(map, u->slot, dl->index);
9659 set_imsm_ord_tbl_ent(migr_map, u->slot,
9660 dl->index | IMSM_ORD_REBUILD);
9661
9662 /* update the family_num to mark a new container
9663 * generation, being careful to record the existing
9664 * family_num in orig_family_num to clean up after
9665 * earlier mdadm versions that neglected to set it.
9666 */
9667 if (mpb->orig_family_num == 0)
9668 mpb->orig_family_num = mpb->family_num;
9669 mpb->family_num += super->random;
9670
9671 /* count arrays using the victim in the metadata */
9672 found = 0;
9673 for (a = active_array; a ; a = a->next) {
9674 dev = get_imsm_dev(super, a->info.container_member);
9675 map = get_imsm_map(dev, MAP_0);
9676
9677 if (get_imsm_disk_slot(map, victim) >= 0)
9678 found++;
9679 }
9680
9681 /* delete the victim if it is no longer being
9682 * utilized anywhere
9683 */
9684 if (!found) {
9685 struct dl **dlp;
9686
9687 /* We know that 'manager' isn't touching anything,
9688 * so it is safe to delete
9689 */
9690 for (dlp = &super->disks; *dlp; dlp = &(*dlp)->next)
9691 if ((*dlp)->index == victim)
9692 break;
9693
9694 /* victim may be on the missing list */
9695 if (!*dlp)
9696 for (dlp = &super->missing; *dlp;
9697 dlp = &(*dlp)->next)
9698 if ((*dlp)->index == victim)
9699 break;
9700 imsm_delete(super, dlp, victim);
9701 }
9702 }
9703
9704 return 1;
9705 }
9706
9707 static int apply_reshape_container_disks_update(struct imsm_update_reshape *u,
9708 struct intel_super *super,
9709 void ***space_list)
9710 {
9711 struct dl *new_disk;
9712 struct intel_dev *id;
9713 int i;
9714 int delta_disks = u->new_raid_disks - u->old_raid_disks;
9715 int disk_count = u->old_raid_disks;
9716 void **tofree = NULL;
9717 int devices_to_reshape = 1;
9718 struct imsm_super *mpb = super->anchor;
9719 int ret_val = 0;
9720 unsigned int dev_id;
9721
9722 dprintf("(enter)\n");
9723
9724 /* enable spares to use in array */
9725 for (i = 0; i < delta_disks; i++) {
9726 new_disk = get_disk_super(super,
9727 major(u->new_disks[i]),
9728 minor(u->new_disks[i]));
9729 dprintf("imsm: new disk for reshape is: %i:%i (%p, index = %i)\n",
9730 major(u->new_disks[i]), minor(u->new_disks[i]),
9731 new_disk, new_disk->index);
9732 if (new_disk == NULL ||
9733 (new_disk->index >= 0 &&
9734 new_disk->index < u->old_raid_disks))
9735 goto update_reshape_exit;
9736 new_disk->index = disk_count++;
9737 /* slot to fill in autolayout
9738 */
9739 new_disk->raiddisk = new_disk->index;
9740 new_disk->disk.status |=
9741 CONFIGURED_DISK;
9742 new_disk->disk.status &= ~SPARE_DISK;
9743 }
9744
9745 dprintf("imsm: volume set mpb->num_raid_devs = %i\n",
9746 mpb->num_raid_devs);
9747 /* manage changes in volume
9748 */
9749 for (dev_id = 0; dev_id < mpb->num_raid_devs; dev_id++) {
9750 void **sp = *space_list;
9751 struct imsm_dev *newdev;
9752 struct imsm_map *newmap, *oldmap;
9753
9754 for (id = super->devlist ; id; id = id->next) {
9755 if (id->index == dev_id)
9756 break;
9757 }
9758 if (id == NULL)
9759 break;
9760 if (!sp)
9761 continue;
9762 *space_list = *sp;
9763 newdev = (void*)sp;
9764 /* Copy the dev, but not (all of) the map */
9765 memcpy(newdev, id->dev, sizeof(*newdev));
9766 oldmap = get_imsm_map(id->dev, MAP_0);
9767 newmap = get_imsm_map(newdev, MAP_0);
9768 /* Copy the current map */
9769 memcpy(newmap, oldmap, sizeof_imsm_map(oldmap));
9770 /* update one device only
9771 */
9772 if (devices_to_reshape) {
9773 dprintf("imsm: modifying subdev: %i\n",
9774 id->index);
9775 devices_to_reshape--;
9776 newdev->vol.migr_state = 1;
9777 set_vol_curr_migr_unit(newdev, 0);
9778 set_migr_type(newdev, MIGR_GEN_MIGR);
9779 newmap->num_members = u->new_raid_disks;
9780 for (i = 0; i < delta_disks; i++) {
9781 set_imsm_ord_tbl_ent(newmap,
9782 u->old_raid_disks + i,
9783 u->old_raid_disks + i);
9784 }
9785 /* New map is correct, now need to save old map
9786 */
9787 newmap = get_imsm_map(newdev, MAP_1);
9788 memcpy(newmap, oldmap, sizeof_imsm_map(oldmap));
9789
9790 imsm_set_array_size(newdev, -1);
9791 }
9792
9793 sp = (void **)id->dev;
9794 id->dev = newdev;
9795 *sp = tofree;
9796 tofree = sp;
9797
9798 /* Clear migration record */
9799 memset(super->migr_rec, 0, sizeof(struct migr_record));
9800 }
9801 if (tofree)
9802 *space_list = tofree;
9803 ret_val = 1;
9804
9805 update_reshape_exit:
9806
9807 return ret_val;
9808 }
9809
9810 static int apply_takeover_update(struct imsm_update_takeover *u,
9811 struct intel_super *super,
9812 void ***space_list)
9813 {
9814 struct imsm_dev *dev = NULL;
9815 struct intel_dev *dv;
9816 struct imsm_dev *dev_new;
9817 struct imsm_map *map;
9818 struct dl *dm, *du;
9819 int i;
9820
9821 for (dv = super->devlist; dv; dv = dv->next)
9822 if (dv->index == (unsigned int)u->subarray) {
9823 dev = dv->dev;
9824 break;
9825 }
9826
9827 if (dev == NULL)
9828 return 0;
9829
9830 map = get_imsm_map(dev, MAP_0);
9831
9832 if (u->direction == R10_TO_R0) {
9833 unsigned long long num_data_stripes;
9834
9835 /* Number of failed disks must be half of initial disk number */
9836 if (imsm_count_failed(super, dev, MAP_0) !=
9837 (map->num_members / 2))
9838 return 0;
9839
9840 /* iterate through devices to mark removed disks as spare */
9841 for (dm = super->disks; dm; dm = dm->next) {
9842 if (dm->disk.status & FAILED_DISK) {
9843 int idx = dm->index;
9844 /* update indexes on the disk list */
9845 /* FIXME this loop-with-the-loop looks wrong, I'm not convinced
9846 the index values will end up being correct.... NB */
9847 for (du = super->disks; du; du = du->next)
9848 if (du->index > idx)
9849 du->index--;
9850 /* mark as spare disk */
9851 mark_spare(dm);
9852 }
9853 }
9854 /* update map */
9855 map->num_members = map->num_members / 2;
9856 map->map_state = IMSM_T_STATE_NORMAL;
9857 map->num_domains = 1;
9858 map->raid_level = 0;
9859 map->failed_disk_num = -1;
9860 num_data_stripes = imsm_dev_size(dev) / 2;
9861 num_data_stripes /= map->blocks_per_strip;
9862 set_num_data_stripes(map, num_data_stripes);
9863 }
9864
9865 if (u->direction == R0_TO_R10) {
9866 void **space;
9867 unsigned long long num_data_stripes;
9868
9869 /* update slots in current disk list */
9870 for (dm = super->disks; dm; dm = dm->next) {
9871 if (dm->index >= 0)
9872 dm->index *= 2;
9873 }
9874 /* create new *missing* disks */
9875 for (i = 0; i < map->num_members; i++) {
9876 space = *space_list;
9877 if (!space)
9878 continue;
9879 *space_list = *space;
9880 du = (void *)space;
9881 memcpy(du, super->disks, sizeof(*du));
9882 du->fd = -1;
9883 du->minor = 0;
9884 du->major = 0;
9885 du->index = (i * 2) + 1;
9886 sprintf((char *)du->disk.serial,
9887 " MISSING_%d", du->index);
9888 sprintf((char *)du->serial,
9889 "MISSING_%d", du->index);
9890 du->next = super->missing;
9891 super->missing = du;
9892 }
9893 /* create new dev and map */
9894 space = *space_list;
9895 if (!space)
9896 return 0;
9897 *space_list = *space;
9898 dev_new = (void *)space;
9899 memcpy(dev_new, dev, sizeof(*dev));
9900 /* update new map */
9901 map = get_imsm_map(dev_new, MAP_0);
9902 map->num_members = map->num_members * 2;
9903 map->map_state = IMSM_T_STATE_DEGRADED;
9904 map->num_domains = 2;
9905 map->raid_level = 1;
9906 num_data_stripes = imsm_dev_size(dev) / 2;
9907 num_data_stripes /= map->blocks_per_strip;
9908 num_data_stripes /= map->num_domains;
9909 set_num_data_stripes(map, num_data_stripes);
9910
9911 /* replace dev<->dev_new */
9912 dv->dev = dev_new;
9913 }
9914 /* update disk order table */
9915 for (du = super->disks; du; du = du->next)
9916 if (du->index >= 0)
9917 set_imsm_ord_tbl_ent(map, du->index, du->index);
9918 for (du = super->missing; du; du = du->next)
9919 if (du->index >= 0) {
9920 set_imsm_ord_tbl_ent(map, du->index, du->index);
9921 mark_missing(super, dv->dev, &du->disk, du->index);
9922 }
9923
9924 return 1;
9925 }
9926
9927 static void imsm_process_update(struct supertype *st,
9928 struct metadata_update *update)
9929 {
9930 /**
9931 * crack open the metadata_update envelope to find the update record
9932 * update can be one of:
9933 * update_reshape_container_disks - all the arrays in the container
9934 * are being reshaped to have more devices. We need to mark
9935 * the arrays for general migration and convert selected spares
9936 * into active devices.
9937 * update_activate_spare - a spare device has replaced a failed
9938 * device in an array, update the disk_ord_tbl. If this disk is
9939 * present in all member arrays then also clear the SPARE_DISK
9940 * flag
9941 * update_create_array
9942 * update_kill_array
9943 * update_rename_array
9944 * update_add_remove_disk
9945 */
9946 struct intel_super *super = st->sb;
9947 struct imsm_super *mpb;
9948 enum imsm_update_type type = *(enum imsm_update_type *) update->buf;
9949
9950 /* update requires a larger buf but the allocation failed */
9951 if (super->next_len && !super->next_buf) {
9952 super->next_len = 0;
9953 return;
9954 }
9955
9956 if (super->next_buf) {
9957 memcpy(super->next_buf, super->buf, super->len);
9958 free(super->buf);
9959 super->len = super->next_len;
9960 super->buf = super->next_buf;
9961
9962 super->next_len = 0;
9963 super->next_buf = NULL;
9964 }
9965
9966 mpb = super->anchor;
9967
9968 switch (type) {
9969 case update_general_migration_checkpoint: {
9970 struct intel_dev *id;
9971 struct imsm_update_general_migration_checkpoint *u =
9972 (void *)update->buf;
9973
9974 dprintf("called for update_general_migration_checkpoint\n");
9975
9976 /* find device under general migration */
9977 for (id = super->devlist ; id; id = id->next) {
9978 if (is_gen_migration(id->dev)) {
9979 set_vol_curr_migr_unit(id->dev,
9980 u->curr_migr_unit);
9981 super->updates_pending++;
9982 }
9983 }
9984 break;
9985 }
9986 case update_takeover: {
9987 struct imsm_update_takeover *u = (void *)update->buf;
9988 if (apply_takeover_update(u, super, &update->space_list)) {
9989 imsm_update_version_info(super);
9990 super->updates_pending++;
9991 }
9992 break;
9993 }
9994
9995 case update_reshape_container_disks: {
9996 struct imsm_update_reshape *u = (void *)update->buf;
9997 if (apply_reshape_container_disks_update(
9998 u, super, &update->space_list))
9999 super->updates_pending++;
10000 break;
10001 }
10002 case update_reshape_migration: {
10003 struct imsm_update_reshape_migration *u = (void *)update->buf;
10004 if (apply_reshape_migration_update(
10005 u, super, &update->space_list))
10006 super->updates_pending++;
10007 break;
10008 }
10009 case update_size_change: {
10010 struct imsm_update_size_change *u = (void *)update->buf;
10011 if (apply_size_change_update(u, super))
10012 super->updates_pending++;
10013 break;
10014 }
10015 case update_activate_spare: {
10016 struct imsm_update_activate_spare *u = (void *) update->buf;
10017
10018 if (prepare_spare_to_activate(st, u) &&
10019 apply_update_activate_spare(u, super, st->arrays))
10020 super->updates_pending++;
10021 break;
10022 }
10023 case update_create_array: {
10024 /* someone wants to create a new array, we need to be aware of
10025 * a few races/collisions:
10026 * 1/ 'Create' called by two separate instances of mdadm
10027 * 2/ 'Create' versus 'activate_spare': mdadm has chosen
10028 * devices that have since been assimilated via
10029 * activate_spare.
10030 * In the event this update can not be carried out mdadm will
10031 * (FIX ME) notice that its update did not take hold.
10032 */
10033 struct imsm_update_create_array *u = (void *) update->buf;
10034 struct intel_dev *dv;
10035 struct imsm_dev *dev;
10036 struct imsm_map *map, *new_map;
10037 unsigned long long start, end;
10038 unsigned long long new_start, new_end;
10039 int i;
10040 struct disk_info *inf;
10041 struct dl *dl;
10042
10043 /* handle racing creates: first come first serve */
10044 if (u->dev_idx < mpb->num_raid_devs) {
10045 dprintf("subarray %d already defined\n", u->dev_idx);
10046 goto create_error;
10047 }
10048
10049 /* check update is next in sequence */
10050 if (u->dev_idx != mpb->num_raid_devs) {
10051 dprintf("can not create array %d expected index %d\n",
10052 u->dev_idx, mpb->num_raid_devs);
10053 goto create_error;
10054 }
10055
10056 new_map = get_imsm_map(&u->dev, MAP_0);
10057 new_start = pba_of_lba0(new_map);
10058 new_end = new_start + per_dev_array_size(new_map);
10059 inf = get_disk_info(u);
10060
10061 /* handle activate_spare versus create race:
10062 * check to make sure that overlapping arrays do not include
10063 * overalpping disks
10064 */
10065 for (i = 0; i < mpb->num_raid_devs; i++) {
10066 dev = get_imsm_dev(super, i);
10067 map = get_imsm_map(dev, MAP_0);
10068 start = pba_of_lba0(map);
10069 end = start + per_dev_array_size(map);
10070 if ((new_start >= start && new_start <= end) ||
10071 (start >= new_start && start <= new_end))
10072 /* overlap */;
10073 else
10074 continue;
10075
10076 if (disks_overlap(super, i, u)) {
10077 dprintf("arrays overlap\n");
10078 goto create_error;
10079 }
10080 }
10081
10082 /* check that prepare update was successful */
10083 if (!update->space) {
10084 dprintf("prepare update failed\n");
10085 goto create_error;
10086 }
10087
10088 /* check that all disks are still active before committing
10089 * changes. FIXME: could we instead handle this by creating a
10090 * degraded array? That's probably not what the user expects,
10091 * so better to drop this update on the floor.
10092 */
10093 for (i = 0; i < new_map->num_members; i++) {
10094 dl = serial_to_dl(inf[i].serial, super);
10095 if (!dl) {
10096 dprintf("disk disappeared\n");
10097 goto create_error;
10098 }
10099 }
10100
10101 super->updates_pending++;
10102
10103 /* convert spares to members and fixup ord_tbl */
10104 for (i = 0; i < new_map->num_members; i++) {
10105 dl = serial_to_dl(inf[i].serial, super);
10106 if (dl->index == -1) {
10107 dl->index = mpb->num_disks;
10108 mpb->num_disks++;
10109 dl->disk.status |= CONFIGURED_DISK;
10110 dl->disk.status &= ~SPARE_DISK;
10111 }
10112 set_imsm_ord_tbl_ent(new_map, i, dl->index);
10113 }
10114
10115 dv = update->space;
10116 dev = dv->dev;
10117 update->space = NULL;
10118 imsm_copy_dev(dev, &u->dev);
10119 dv->index = u->dev_idx;
10120 dv->next = super->devlist;
10121 super->devlist = dv;
10122 mpb->num_raid_devs++;
10123
10124 imsm_update_version_info(super);
10125 break;
10126 create_error:
10127 /* mdmon knows how to release update->space, but not
10128 * ((struct intel_dev *) update->space)->dev
10129 */
10130 if (update->space) {
10131 dv = update->space;
10132 free(dv->dev);
10133 }
10134 break;
10135 }
10136 case update_kill_array: {
10137 struct imsm_update_kill_array *u = (void *) update->buf;
10138 int victim = u->dev_idx;
10139 struct active_array *a;
10140 struct intel_dev **dp;
10141 struct imsm_dev *dev;
10142
10143 /* sanity check that we are not affecting the uuid of
10144 * active arrays, or deleting an active array
10145 *
10146 * FIXME when immutable ids are available, but note that
10147 * we'll also need to fixup the invalidated/active
10148 * subarray indexes in mdstat
10149 */
10150 for (a = st->arrays; a; a = a->next)
10151 if (a->info.container_member >= victim)
10152 break;
10153 /* by definition if mdmon is running at least one array
10154 * is active in the container, so checking
10155 * mpb->num_raid_devs is just extra paranoia
10156 */
10157 dev = get_imsm_dev(super, victim);
10158 if (a || !dev || mpb->num_raid_devs == 1) {
10159 dprintf("failed to delete subarray-%d\n", victim);
10160 break;
10161 }
10162
10163 for (dp = &super->devlist; *dp;)
10164 if ((*dp)->index == (unsigned)super->current_vol) {
10165 *dp = (*dp)->next;
10166 } else {
10167 if ((*dp)->index > (unsigned)victim)
10168 (*dp)->index--;
10169 dp = &(*dp)->next;
10170 }
10171 mpb->num_raid_devs--;
10172 super->updates_pending++;
10173 break;
10174 }
10175 case update_rename_array: {
10176 struct imsm_update_rename_array *u = (void *) update->buf;
10177 char name[MAX_RAID_SERIAL_LEN+1];
10178 int target = u->dev_idx;
10179 struct active_array *a;
10180 struct imsm_dev *dev;
10181
10182 /* sanity check that we are not affecting the uuid of
10183 * an active array
10184 */
10185 memset(name, 0, sizeof(name));
10186 snprintf(name, MAX_RAID_SERIAL_LEN, "%s", (char *) u->name);
10187 name[MAX_RAID_SERIAL_LEN] = '\0';
10188 for (a = st->arrays; a; a = a->next)
10189 if (a->info.container_member == target)
10190 break;
10191 dev = get_imsm_dev(super, u->dev_idx);
10192 if (a || !dev || !check_name(super, name, 1)) {
10193 dprintf("failed to rename subarray-%d\n", target);
10194 break;
10195 }
10196
10197 memcpy(dev->volume, name, MAX_RAID_SERIAL_LEN);
10198 super->updates_pending++;
10199 break;
10200 }
10201 case update_add_remove_disk: {
10202 /* we may be able to repair some arrays if disks are
10203 * being added, check the status of add_remove_disk
10204 * if discs has been added.
10205 */
10206 if (add_remove_disk_update(super)) {
10207 struct active_array *a;
10208
10209 super->updates_pending++;
10210 for (a = st->arrays; a; a = a->next)
10211 a->check_degraded = 1;
10212 }
10213 break;
10214 }
10215 case update_prealloc_badblocks_mem:
10216 break;
10217 case update_rwh_policy: {
10218 struct imsm_update_rwh_policy *u = (void *)update->buf;
10219 int target = u->dev_idx;
10220 struct imsm_dev *dev = get_imsm_dev(super, target);
10221 if (!dev) {
10222 dprintf("could not find subarray-%d\n", target);
10223 break;
10224 }
10225
10226 if (dev->rwh_policy != u->new_policy) {
10227 dev->rwh_policy = u->new_policy;
10228 super->updates_pending++;
10229 }
10230 break;
10231 }
10232 default:
10233 pr_err("error: unsupported process update type:(type: %d)\n", type);
10234 }
10235 }
10236
10237 static struct mdinfo *get_spares_for_grow(struct supertype *st);
10238
10239 static int imsm_prepare_update(struct supertype *st,
10240 struct metadata_update *update)
10241 {
10242 /**
10243 * Allocate space to hold new disk entries, raid-device entries or a new
10244 * mpb if necessary. The manager synchronously waits for updates to
10245 * complete in the monitor, so new mpb buffers allocated here can be
10246 * integrated by the monitor thread without worrying about live pointers
10247 * in the manager thread.
10248 */
10249 enum imsm_update_type type;
10250 struct intel_super *super = st->sb;
10251 unsigned int sector_size = super->sector_size;
10252 struct imsm_super *mpb = super->anchor;
10253 size_t buf_len;
10254 size_t len = 0;
10255
10256 if (update->len < (int)sizeof(type))
10257 return 0;
10258
10259 type = *(enum imsm_update_type *) update->buf;
10260
10261 switch (type) {
10262 case update_general_migration_checkpoint:
10263 if (update->len < (int)sizeof(struct imsm_update_general_migration_checkpoint))
10264 return 0;
10265 dprintf("called for update_general_migration_checkpoint\n");
10266 break;
10267 case update_takeover: {
10268 struct imsm_update_takeover *u = (void *)update->buf;
10269 if (update->len < (int)sizeof(*u))
10270 return 0;
10271 if (u->direction == R0_TO_R10) {
10272 void **tail = (void **)&update->space_list;
10273 struct imsm_dev *dev = get_imsm_dev(super, u->subarray);
10274 struct imsm_map *map = get_imsm_map(dev, MAP_0);
10275 int num_members = map->num_members;
10276 void *space;
10277 int size, i;
10278 /* allocate memory for added disks */
10279 for (i = 0; i < num_members; i++) {
10280 size = sizeof(struct dl);
10281 space = xmalloc(size);
10282 *tail = space;
10283 tail = space;
10284 *tail = NULL;
10285 }
10286 /* allocate memory for new device */
10287 size = sizeof_imsm_dev(super->devlist->dev, 0) +
10288 (num_members * sizeof(__u32));
10289 space = xmalloc(size);
10290 *tail = space;
10291 tail = space;
10292 *tail = NULL;
10293 len = disks_to_mpb_size(num_members * 2);
10294 }
10295
10296 break;
10297 }
10298 case update_reshape_container_disks: {
10299 /* Every raid device in the container is about to
10300 * gain some more devices, and we will enter a
10301 * reconfiguration.
10302 * So each 'imsm_map' will be bigger, and the imsm_vol
10303 * will now hold 2 of them.
10304 * Thus we need new 'struct imsm_dev' allocations sized
10305 * as sizeof_imsm_dev but with more devices in both maps.
10306 */
10307 struct imsm_update_reshape *u = (void *)update->buf;
10308 struct intel_dev *dl;
10309 void **space_tail = (void**)&update->space_list;
10310
10311 if (update->len < (int)sizeof(*u))
10312 return 0;
10313
10314 dprintf("for update_reshape\n");
10315
10316 for (dl = super->devlist; dl; dl = dl->next) {
10317 int size = sizeof_imsm_dev(dl->dev, 1);
10318 void *s;
10319 if (u->new_raid_disks > u->old_raid_disks)
10320 size += sizeof(__u32)*2*
10321 (u->new_raid_disks - u->old_raid_disks);
10322 s = xmalloc(size);
10323 *space_tail = s;
10324 space_tail = s;
10325 *space_tail = NULL;
10326 }
10327
10328 len = disks_to_mpb_size(u->new_raid_disks);
10329 dprintf("New anchor length is %llu\n", (unsigned long long)len);
10330 break;
10331 }
10332 case update_reshape_migration: {
10333 /* for migration level 0->5 we need to add disks
10334 * so the same as for container operation we will copy
10335 * device to the bigger location.
10336 * in memory prepared device and new disk area are prepared
10337 * for usage in process update
10338 */
10339 struct imsm_update_reshape_migration *u = (void *)update->buf;
10340 struct intel_dev *id;
10341 void **space_tail = (void **)&update->space_list;
10342 int size;
10343 void *s;
10344 int current_level = -1;
10345
10346 if (update->len < (int)sizeof(*u))
10347 return 0;
10348
10349 dprintf("for update_reshape\n");
10350
10351 /* add space for bigger array in update
10352 */
10353 for (id = super->devlist; id; id = id->next) {
10354 if (id->index == (unsigned)u->subdev) {
10355 size = sizeof_imsm_dev(id->dev, 1);
10356 if (u->new_raid_disks > u->old_raid_disks)
10357 size += sizeof(__u32)*2*
10358 (u->new_raid_disks - u->old_raid_disks);
10359 s = xmalloc(size);
10360 *space_tail = s;
10361 space_tail = s;
10362 *space_tail = NULL;
10363 break;
10364 }
10365 }
10366 if (update->space_list == NULL)
10367 break;
10368
10369 /* add space for disk in update
10370 */
10371 size = sizeof(struct dl);
10372 s = xmalloc(size);
10373 *space_tail = s;
10374 space_tail = s;
10375 *space_tail = NULL;
10376
10377 /* add spare device to update
10378 */
10379 for (id = super->devlist ; id; id = id->next)
10380 if (id->index == (unsigned)u->subdev) {
10381 struct imsm_dev *dev;
10382 struct imsm_map *map;
10383
10384 dev = get_imsm_dev(super, u->subdev);
10385 map = get_imsm_map(dev, MAP_0);
10386 current_level = map->raid_level;
10387 break;
10388 }
10389 if (u->new_level == 5 && u->new_level != current_level) {
10390 struct mdinfo *spares;
10391
10392 spares = get_spares_for_grow(st);
10393 if (spares) {
10394 struct dl *dl;
10395 struct mdinfo *dev;
10396
10397 dev = spares->devs;
10398 if (dev) {
10399 u->new_disks[0] =
10400 makedev(dev->disk.major,
10401 dev->disk.minor);
10402 dl = get_disk_super(super,
10403 dev->disk.major,
10404 dev->disk.minor);
10405 dl->index = u->old_raid_disks;
10406 dev = dev->next;
10407 }
10408 sysfs_free(spares);
10409 }
10410 }
10411 len = disks_to_mpb_size(u->new_raid_disks);
10412 dprintf("New anchor length is %llu\n", (unsigned long long)len);
10413 break;
10414 }
10415 case update_size_change: {
10416 if (update->len < (int)sizeof(struct imsm_update_size_change))
10417 return 0;
10418 break;
10419 }
10420 case update_activate_spare: {
10421 if (update->len < (int)sizeof(struct imsm_update_activate_spare))
10422 return 0;
10423 break;
10424 }
10425 case update_create_array: {
10426 struct imsm_update_create_array *u = (void *) update->buf;
10427 struct intel_dev *dv;
10428 struct imsm_dev *dev = &u->dev;
10429 struct imsm_map *map = get_imsm_map(dev, MAP_0);
10430 struct dl *dl;
10431 struct disk_info *inf;
10432 int i;
10433 int activate = 0;
10434
10435 if (update->len < (int)sizeof(*u))
10436 return 0;
10437
10438 inf = get_disk_info(u);
10439 len = sizeof_imsm_dev(dev, 1);
10440 /* allocate a new super->devlist entry */
10441 dv = xmalloc(sizeof(*dv));
10442 dv->dev = xmalloc(len);
10443 update->space = dv;
10444
10445 /* count how many spares will be converted to members */
10446 for (i = 0; i < map->num_members; i++) {
10447 dl = serial_to_dl(inf[i].serial, super);
10448 if (!dl) {
10449 /* hmm maybe it failed?, nothing we can do about
10450 * it here
10451 */
10452 continue;
10453 }
10454 if (count_memberships(dl, super) == 0)
10455 activate++;
10456 }
10457 len += activate * sizeof(struct imsm_disk);
10458 break;
10459 }
10460 case update_kill_array: {
10461 if (update->len < (int)sizeof(struct imsm_update_kill_array))
10462 return 0;
10463 break;
10464 }
10465 case update_rename_array: {
10466 if (update->len < (int)sizeof(struct imsm_update_rename_array))
10467 return 0;
10468 break;
10469 }
10470 case update_add_remove_disk:
10471 /* no update->len needed */
10472 break;
10473 case update_prealloc_badblocks_mem:
10474 super->extra_space += sizeof(struct bbm_log) -
10475 get_imsm_bbm_log_size(super->bbm_log);
10476 break;
10477 case update_rwh_policy: {
10478 if (update->len < (int)sizeof(struct imsm_update_rwh_policy))
10479 return 0;
10480 break;
10481 }
10482 default:
10483 return 0;
10484 }
10485
10486 /* check if we need a larger metadata buffer */
10487 if (super->next_buf)
10488 buf_len = super->next_len;
10489 else
10490 buf_len = super->len;
10491
10492 if (__le32_to_cpu(mpb->mpb_size) + super->extra_space + len > buf_len) {
10493 /* ok we need a larger buf than what is currently allocated
10494 * if this allocation fails process_update will notice that
10495 * ->next_len is set and ->next_buf is NULL
10496 */
10497 buf_len = ROUND_UP(__le32_to_cpu(mpb->mpb_size) +
10498 super->extra_space + len, sector_size);
10499 if (super->next_buf)
10500 free(super->next_buf);
10501
10502 super->next_len = buf_len;
10503 if (posix_memalign(&super->next_buf, sector_size, buf_len) == 0)
10504 memset(super->next_buf, 0, buf_len);
10505 else
10506 super->next_buf = NULL;
10507 }
10508 return 1;
10509 }
10510
10511 /* must be called while manager is quiesced */
10512 static void imsm_delete(struct intel_super *super, struct dl **dlp, unsigned index)
10513 {
10514 struct imsm_super *mpb = super->anchor;
10515 struct dl *iter;
10516 struct imsm_dev *dev;
10517 struct imsm_map *map;
10518 unsigned int i, j, num_members;
10519 __u32 ord, ord_map0;
10520 struct bbm_log *log = super->bbm_log;
10521
10522 dprintf("deleting device[%d] from imsm_super\n", index);
10523
10524 /* shift all indexes down one */
10525 for (iter = super->disks; iter; iter = iter->next)
10526 if (iter->index > (int)index)
10527 iter->index--;
10528 for (iter = super->missing; iter; iter = iter->next)
10529 if (iter->index > (int)index)
10530 iter->index--;
10531
10532 for (i = 0; i < mpb->num_raid_devs; i++) {
10533 dev = get_imsm_dev(super, i);
10534 map = get_imsm_map(dev, MAP_0);
10535 num_members = map->num_members;
10536 for (j = 0; j < num_members; j++) {
10537 /* update ord entries being careful not to propagate
10538 * ord-flags to the first map
10539 */
10540 ord = get_imsm_ord_tbl_ent(dev, j, MAP_X);
10541 ord_map0 = get_imsm_ord_tbl_ent(dev, j, MAP_0);
10542
10543 if (ord_to_idx(ord) <= index)
10544 continue;
10545
10546 map = get_imsm_map(dev, MAP_0);
10547 set_imsm_ord_tbl_ent(map, j, ord_map0 - 1);
10548 map = get_imsm_map(dev, MAP_1);
10549 if (map)
10550 set_imsm_ord_tbl_ent(map, j, ord - 1);
10551 }
10552 }
10553
10554 for (i = 0; i < log->entry_count; i++) {
10555 struct bbm_log_entry *entry = &log->marked_block_entries[i];
10556
10557 if (entry->disk_ordinal <= index)
10558 continue;
10559 entry->disk_ordinal--;
10560 }
10561
10562 mpb->num_disks--;
10563 super->updates_pending++;
10564 if (*dlp) {
10565 struct dl *dl = *dlp;
10566
10567 *dlp = (*dlp)->next;
10568 __free_imsm_disk(dl);
10569 }
10570 }
10571
10572 static int imsm_get_allowed_degradation(int level, int raid_disks,
10573 struct intel_super *super,
10574 struct imsm_dev *dev)
10575 {
10576 switch (level) {
10577 case 1:
10578 case 10:{
10579 int ret_val = 0;
10580 struct imsm_map *map;
10581 int i;
10582
10583 ret_val = raid_disks/2;
10584 /* check map if all disks pairs not failed
10585 * in both maps
10586 */
10587 map = get_imsm_map(dev, MAP_0);
10588 for (i = 0; i < ret_val; i++) {
10589 int degradation = 0;
10590 if (get_imsm_disk(super, i) == NULL)
10591 degradation++;
10592 if (get_imsm_disk(super, i + 1) == NULL)
10593 degradation++;
10594 if (degradation == 2)
10595 return 0;
10596 }
10597 map = get_imsm_map(dev, MAP_1);
10598 /* if there is no second map
10599 * result can be returned
10600 */
10601 if (map == NULL)
10602 return ret_val;
10603 /* check degradation in second map
10604 */
10605 for (i = 0; i < ret_val; i++) {
10606 int degradation = 0;
10607 if (get_imsm_disk(super, i) == NULL)
10608 degradation++;
10609 if (get_imsm_disk(super, i + 1) == NULL)
10610 degradation++;
10611 if (degradation == 2)
10612 return 0;
10613 }
10614 return ret_val;
10615 }
10616 case 5:
10617 return 1;
10618 case 6:
10619 return 2;
10620 default:
10621 return 0;
10622 }
10623 }
10624
10625 /*******************************************************************************
10626 * Function: validate_container_imsm
10627 * Description: This routine validates container after assemble,
10628 * eg. if devices in container are under the same controller.
10629 *
10630 * Parameters:
10631 * info : linked list with info about devices used in array
10632 * Returns:
10633 * 1 : HBA mismatch
10634 * 0 : Success
10635 ******************************************************************************/
10636 int validate_container_imsm(struct mdinfo *info)
10637 {
10638 if (check_env("IMSM_NO_PLATFORM"))
10639 return 0;
10640
10641 struct sys_dev *idev;
10642 struct sys_dev *hba = NULL;
10643 struct sys_dev *intel_devices = find_intel_devices();
10644 char *dev_path = devt_to_devpath(makedev(info->disk.major,
10645 info->disk.minor));
10646
10647 for (idev = intel_devices; idev; idev = idev->next) {
10648 if (dev_path && strstr(dev_path, idev->path)) {
10649 hba = idev;
10650 break;
10651 }
10652 }
10653 if (dev_path)
10654 free(dev_path);
10655
10656 if (!hba) {
10657 pr_err("WARNING - Cannot detect HBA for device %s!\n",
10658 devid2kname(makedev(info->disk.major, info->disk.minor)));
10659 return 1;
10660 }
10661
10662 const struct imsm_orom *orom = get_orom_by_device_id(hba->dev_id);
10663 struct mdinfo *dev;
10664
10665 for (dev = info->next; dev; dev = dev->next) {
10666 dev_path = devt_to_devpath(makedev(dev->disk.major, dev->disk.minor));
10667
10668 struct sys_dev *hba2 = NULL;
10669 for (idev = intel_devices; idev; idev = idev->next) {
10670 if (dev_path && strstr(dev_path, idev->path)) {
10671 hba2 = idev;
10672 break;
10673 }
10674 }
10675 if (dev_path)
10676 free(dev_path);
10677
10678 const struct imsm_orom *orom2 = hba2 == NULL ? NULL :
10679 get_orom_by_device_id(hba2->dev_id);
10680
10681 if (hba2 && hba->type != hba2->type) {
10682 pr_err("WARNING - HBAs of devices do not match %s != %s\n",
10683 get_sys_dev_type(hba->type), get_sys_dev_type(hba2->type));
10684 return 1;
10685 }
10686
10687 if (orom != orom2) {
10688 pr_err("WARNING - IMSM container assembled with disks under different HBAs!\n"
10689 " This operation is not supported and can lead to data loss.\n");
10690 return 1;
10691 }
10692
10693 if (!orom) {
10694 pr_err("WARNING - IMSM container assembled with disks under HBAs without IMSM platform support!\n"
10695 " This operation is not supported and can lead to data loss.\n");
10696 return 1;
10697 }
10698 }
10699
10700 return 0;
10701 }
10702
10703 /*******************************************************************************
10704 * Function: imsm_record_badblock
10705 * Description: This routine stores new bad block record in BBM log
10706 *
10707 * Parameters:
10708 * a : array containing a bad block
10709 * slot : disk number containing a bad block
10710 * sector : bad block sector
10711 * length : bad block sectors range
10712 * Returns:
10713 * 1 : Success
10714 * 0 : Error
10715 ******************************************************************************/
10716 static int imsm_record_badblock(struct active_array *a, int slot,
10717 unsigned long long sector, int length)
10718 {
10719 struct intel_super *super = a->container->sb;
10720 int ord;
10721 int ret;
10722
10723 ord = imsm_disk_slot_to_ord(a, slot);
10724 if (ord < 0)
10725 return 0;
10726
10727 ret = record_new_badblock(super->bbm_log, ord_to_idx(ord), sector,
10728 length);
10729 if (ret)
10730 super->updates_pending++;
10731
10732 return ret;
10733 }
10734 /*******************************************************************************
10735 * Function: imsm_clear_badblock
10736 * Description: This routine clears bad block record from BBM log
10737 *
10738 * Parameters:
10739 * a : array containing a bad block
10740 * slot : disk number containing a bad block
10741 * sector : bad block sector
10742 * length : bad block sectors range
10743 * Returns:
10744 * 1 : Success
10745 * 0 : Error
10746 ******************************************************************************/
10747 static int imsm_clear_badblock(struct active_array *a, int slot,
10748 unsigned long long sector, int length)
10749 {
10750 struct intel_super *super = a->container->sb;
10751 int ord;
10752 int ret;
10753
10754 ord = imsm_disk_slot_to_ord(a, slot);
10755 if (ord < 0)
10756 return 0;
10757
10758 ret = clear_badblock(super->bbm_log, ord_to_idx(ord), sector, length);
10759 if (ret)
10760 super->updates_pending++;
10761
10762 return ret;
10763 }
10764 /*******************************************************************************
10765 * Function: imsm_get_badblocks
10766 * Description: This routine get list of bad blocks for an array
10767 *
10768 * Parameters:
10769 * a : array
10770 * slot : disk number
10771 * Returns:
10772 * bb : structure containing bad blocks
10773 * NULL : error
10774 ******************************************************************************/
10775 static struct md_bb *imsm_get_badblocks(struct active_array *a, int slot)
10776 {
10777 int inst = a->info.container_member;
10778 struct intel_super *super = a->container->sb;
10779 struct imsm_dev *dev = get_imsm_dev(super, inst);
10780 struct imsm_map *map = get_imsm_map(dev, MAP_0);
10781 int ord;
10782
10783 ord = imsm_disk_slot_to_ord(a, slot);
10784 if (ord < 0)
10785 return NULL;
10786
10787 get_volume_badblocks(super->bbm_log, ord_to_idx(ord), pba_of_lba0(map),
10788 per_dev_array_size(map), &super->bb);
10789
10790 return &super->bb;
10791 }
10792 /*******************************************************************************
10793 * Function: examine_badblocks_imsm
10794 * Description: Prints list of bad blocks on a disk to the standard output
10795 *
10796 * Parameters:
10797 * st : metadata handler
10798 * fd : open file descriptor for device
10799 * devname : device name
10800 * Returns:
10801 * 0 : Success
10802 * 1 : Error
10803 ******************************************************************************/
10804 static int examine_badblocks_imsm(struct supertype *st, int fd, char *devname)
10805 {
10806 struct intel_super *super = st->sb;
10807 struct bbm_log *log = super->bbm_log;
10808 struct dl *d = NULL;
10809 int any = 0;
10810
10811 for (d = super->disks; d ; d = d->next) {
10812 if (strcmp(d->devname, devname) == 0)
10813 break;
10814 }
10815
10816 if ((d == NULL) || (d->index < 0)) { /* serial mismatch probably */
10817 pr_err("%s doesn't appear to be part of a raid array\n",
10818 devname);
10819 return 1;
10820 }
10821
10822 if (log != NULL) {
10823 unsigned int i;
10824 struct bbm_log_entry *entry = &log->marked_block_entries[0];
10825
10826 for (i = 0; i < log->entry_count; i++) {
10827 if (entry[i].disk_ordinal == d->index) {
10828 unsigned long long sector = __le48_to_cpu(
10829 &entry[i].defective_block_start);
10830 int cnt = entry[i].marked_count + 1;
10831
10832 if (!any) {
10833 printf("Bad-blocks on %s:\n", devname);
10834 any = 1;
10835 }
10836
10837 printf("%20llu for %d sectors\n", sector, cnt);
10838 }
10839 }
10840 }
10841
10842 if (!any)
10843 printf("No bad-blocks list configured on %s\n", devname);
10844
10845 return 0;
10846 }
10847 /*******************************************************************************
10848 * Function: init_migr_record_imsm
10849 * Description: Function inits imsm migration record
10850 * Parameters:
10851 * super : imsm internal array info
10852 * dev : device under migration
10853 * info : general array info to find the smallest device
10854 * Returns:
10855 * none
10856 ******************************************************************************/
10857 void init_migr_record_imsm(struct supertype *st, struct imsm_dev *dev,
10858 struct mdinfo *info)
10859 {
10860 struct intel_super *super = st->sb;
10861 struct migr_record *migr_rec = super->migr_rec;
10862 int new_data_disks;
10863 unsigned long long dsize, dev_sectors;
10864 long long unsigned min_dev_sectors = -1LLU;
10865 struct imsm_map *map_dest = get_imsm_map(dev, MAP_0);
10866 struct imsm_map *map_src = get_imsm_map(dev, MAP_1);
10867 unsigned long long num_migr_units;
10868 unsigned long long array_blocks;
10869 struct dl *dl_disk = NULL;
10870
10871 memset(migr_rec, 0, sizeof(struct migr_record));
10872 migr_rec->family_num = __cpu_to_le32(super->anchor->family_num);
10873
10874 /* only ascending reshape supported now */
10875 migr_rec->ascending_migr = __cpu_to_le32(1);
10876
10877 migr_rec->dest_depth_per_unit = GEN_MIGR_AREA_SIZE /
10878 max(map_dest->blocks_per_strip, map_src->blocks_per_strip);
10879 migr_rec->dest_depth_per_unit *=
10880 max(map_dest->blocks_per_strip, map_src->blocks_per_strip);
10881 new_data_disks = imsm_num_data_members(map_dest);
10882 migr_rec->blocks_per_unit =
10883 __cpu_to_le32(migr_rec->dest_depth_per_unit * new_data_disks);
10884 migr_rec->dest_depth_per_unit =
10885 __cpu_to_le32(migr_rec->dest_depth_per_unit);
10886 array_blocks = info->component_size * new_data_disks;
10887 num_migr_units =
10888 array_blocks / __le32_to_cpu(migr_rec->blocks_per_unit);
10889
10890 if (array_blocks % __le32_to_cpu(migr_rec->blocks_per_unit))
10891 num_migr_units++;
10892 set_num_migr_units(migr_rec, num_migr_units);
10893
10894 migr_rec->post_migr_vol_cap = dev->size_low;
10895 migr_rec->post_migr_vol_cap_hi = dev->size_high;
10896
10897 /* Find the smallest dev */
10898 for (dl_disk = super->disks; dl_disk ; dl_disk = dl_disk->next) {
10899 /* ignore spares in container */
10900 if (dl_disk->index < 0)
10901 continue;
10902 get_dev_size(dl_disk->fd, NULL, &dsize);
10903 dev_sectors = dsize / 512;
10904 if (dev_sectors < min_dev_sectors)
10905 min_dev_sectors = dev_sectors;
10906 }
10907 set_migr_chkp_area_pba(migr_rec, min_dev_sectors -
10908 RAID_DISK_RESERVED_BLOCKS_IMSM_HI);
10909
10910 write_imsm_migr_rec(st);
10911
10912 return;
10913 }
10914
10915 /*******************************************************************************
10916 * Function: save_backup_imsm
10917 * Description: Function saves critical data stripes to Migration Copy Area
10918 * and updates the current migration unit status.
10919 * Use restore_stripes() to form a destination stripe,
10920 * and to write it to the Copy Area.
10921 * Parameters:
10922 * st : supertype information
10923 * dev : imsm device that backup is saved for
10924 * info : general array info
10925 * buf : input buffer
10926 * length : length of data to backup (blocks_per_unit)
10927 * Returns:
10928 * 0 : success
10929 *, -1 : fail
10930 ******************************************************************************/
10931 int save_backup_imsm(struct supertype *st,
10932 struct imsm_dev *dev,
10933 struct mdinfo *info,
10934 void *buf,
10935 int length)
10936 {
10937 int rv = -1;
10938 struct intel_super *super = st->sb;
10939 unsigned long long *target_offsets;
10940 int *targets;
10941 int i;
10942 struct imsm_map *map_dest = get_imsm_map(dev, MAP_0);
10943 int new_disks = map_dest->num_members;
10944 int dest_layout = 0;
10945 int dest_chunk;
10946 unsigned long long start;
10947 int data_disks = imsm_num_data_members(map_dest);
10948
10949 targets = xmalloc(new_disks * sizeof(int));
10950
10951 for (i = 0; i < new_disks; i++) {
10952 struct dl *dl_disk = get_imsm_dl_disk(super, i);
10953
10954 targets[i] = dl_disk->fd;
10955 }
10956
10957 target_offsets = xcalloc(new_disks, sizeof(unsigned long long));
10958
10959 start = info->reshape_progress * 512;
10960 for (i = 0; i < new_disks; i++) {
10961 target_offsets[i] = migr_chkp_area_pba(super->migr_rec) * 512;
10962 /* move back copy area adderss, it will be moved forward
10963 * in restore_stripes() using start input variable
10964 */
10965 target_offsets[i] -= start/data_disks;
10966 }
10967
10968 dest_layout = imsm_level_to_layout(map_dest->raid_level);
10969 dest_chunk = __le16_to_cpu(map_dest->blocks_per_strip) * 512;
10970
10971 if (restore_stripes(targets, /* list of dest devices */
10972 target_offsets, /* migration record offsets */
10973 new_disks,
10974 dest_chunk,
10975 map_dest->raid_level,
10976 dest_layout,
10977 -1, /* source backup file descriptor */
10978 0, /* input buf offset
10979 * always 0 buf is already offseted */
10980 start,
10981 length,
10982 buf) != 0) {
10983 pr_err("Error restoring stripes\n");
10984 goto abort;
10985 }
10986
10987 rv = 0;
10988
10989 abort:
10990 if (targets) {
10991 free(targets);
10992 }
10993 free(target_offsets);
10994
10995 return rv;
10996 }
10997
10998 /*******************************************************************************
10999 * Function: save_checkpoint_imsm
11000 * Description: Function called for current unit status update
11001 * in the migration record. It writes it to disk.
11002 * Parameters:
11003 * super : imsm internal array info
11004 * info : general array info
11005 * Returns:
11006 * 0: success
11007 * 1: failure
11008 * 2: failure, means no valid migration record
11009 * / no general migration in progress /
11010 ******************************************************************************/
11011 int save_checkpoint_imsm(struct supertype *st, struct mdinfo *info, int state)
11012 {
11013 struct intel_super *super = st->sb;
11014 unsigned long long blocks_per_unit;
11015 unsigned long long curr_migr_unit;
11016
11017 if (load_imsm_migr_rec(super) != 0) {
11018 dprintf("imsm: ERROR: Cannot read migration record for checkpoint save.\n");
11019 return 1;
11020 }
11021
11022 blocks_per_unit = __le32_to_cpu(super->migr_rec->blocks_per_unit);
11023 if (blocks_per_unit == 0) {
11024 dprintf("imsm: no migration in progress.\n");
11025 return 2;
11026 }
11027 curr_migr_unit = info->reshape_progress / blocks_per_unit;
11028 /* check if array is alligned to copy area
11029 * if it is not alligned, add one to current migration unit value
11030 * this can happend on array reshape finish only
11031 */
11032 if (info->reshape_progress % blocks_per_unit)
11033 curr_migr_unit++;
11034
11035 set_current_migr_unit(super->migr_rec, curr_migr_unit);
11036 super->migr_rec->rec_status = __cpu_to_le32(state);
11037 set_migr_dest_1st_member_lba(super->migr_rec,
11038 super->migr_rec->dest_depth_per_unit * curr_migr_unit);
11039
11040 if (write_imsm_migr_rec(st) < 0) {
11041 dprintf("imsm: Cannot write migration record outside backup area\n");
11042 return 1;
11043 }
11044
11045 return 0;
11046 }
11047
11048 /*******************************************************************************
11049 * Function: recover_backup_imsm
11050 * Description: Function recovers critical data from the Migration Copy Area
11051 * while assembling an array.
11052 * Parameters:
11053 * super : imsm internal array info
11054 * info : general array info
11055 * Returns:
11056 * 0 : success (or there is no data to recover)
11057 * 1 : fail
11058 ******************************************************************************/
11059 int recover_backup_imsm(struct supertype *st, struct mdinfo *info)
11060 {
11061 struct intel_super *super = st->sb;
11062 struct migr_record *migr_rec = super->migr_rec;
11063 struct imsm_map *map_dest;
11064 struct intel_dev *id = NULL;
11065 unsigned long long read_offset;
11066 unsigned long long write_offset;
11067 unsigned unit_len;
11068 int new_disks, err;
11069 char *buf = NULL;
11070 int retval = 1;
11071 unsigned int sector_size = super->sector_size;
11072 unsigned long long curr_migr_unit = current_migr_unit(migr_rec);
11073 unsigned long long num_migr_units = get_num_migr_units(migr_rec);
11074 char buffer[20];
11075 int skipped_disks = 0;
11076 struct dl *dl_disk;
11077
11078 err = sysfs_get_str(info, NULL, "array_state", (char *)buffer, 20);
11079 if (err < 1)
11080 return 1;
11081
11082 /* recover data only during assemblation */
11083 if (strncmp(buffer, "inactive", 8) != 0)
11084 return 0;
11085 /* no data to recover */
11086 if (__le32_to_cpu(migr_rec->rec_status) == UNIT_SRC_NORMAL)
11087 return 0;
11088 if (curr_migr_unit >= num_migr_units)
11089 return 1;
11090
11091 /* find device during reshape */
11092 for (id = super->devlist; id; id = id->next)
11093 if (is_gen_migration(id->dev))
11094 break;
11095 if (id == NULL)
11096 return 1;
11097
11098 map_dest = get_imsm_map(id->dev, MAP_0);
11099 new_disks = map_dest->num_members;
11100
11101 read_offset = migr_chkp_area_pba(migr_rec) * 512;
11102
11103 write_offset = (migr_dest_1st_member_lba(migr_rec) +
11104 pba_of_lba0(map_dest)) * 512;
11105
11106 unit_len = __le32_to_cpu(migr_rec->dest_depth_per_unit) * 512;
11107 if (posix_memalign((void **)&buf, sector_size, unit_len) != 0)
11108 goto abort;
11109
11110 for (dl_disk = super->disks; dl_disk; dl_disk = dl_disk->next) {
11111 if (dl_disk->index < 0)
11112 continue;
11113
11114 if (dl_disk->fd < 0) {
11115 skipped_disks++;
11116 continue;
11117 }
11118 if (lseek64(dl_disk->fd, read_offset, SEEK_SET) < 0) {
11119 pr_err("Cannot seek to block: %s\n",
11120 strerror(errno));
11121 skipped_disks++;
11122 continue;
11123 }
11124 if (read(dl_disk->fd, buf, unit_len) != unit_len) {
11125 pr_err("Cannot read copy area block: %s\n",
11126 strerror(errno));
11127 skipped_disks++;
11128 continue;
11129 }
11130 if (lseek64(dl_disk->fd, write_offset, SEEK_SET) < 0) {
11131 pr_err("Cannot seek to block: %s\n",
11132 strerror(errno));
11133 skipped_disks++;
11134 continue;
11135 }
11136 if (write(dl_disk->fd, buf, unit_len) != unit_len) {
11137 pr_err("Cannot restore block: %s\n",
11138 strerror(errno));
11139 skipped_disks++;
11140 continue;
11141 }
11142 }
11143
11144 if (skipped_disks > imsm_get_allowed_degradation(info->new_level,
11145 new_disks,
11146 super,
11147 id->dev)) {
11148 pr_err("Cannot restore data from backup. Too many failed disks\n");
11149 goto abort;
11150 }
11151
11152 if (save_checkpoint_imsm(st, info, UNIT_SRC_NORMAL)) {
11153 /* ignore error == 2, this can mean end of reshape here
11154 */
11155 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL) during restart\n");
11156 } else
11157 retval = 0;
11158
11159 abort:
11160 free(buf);
11161 return retval;
11162 }
11163
11164 static char disk_by_path[] = "/dev/disk/by-path/";
11165
11166 static const char *imsm_get_disk_controller_domain(const char *path)
11167 {
11168 char disk_path[PATH_MAX];
11169 char *drv=NULL;
11170 struct stat st;
11171
11172 strcpy(disk_path, disk_by_path);
11173 strncat(disk_path, path, PATH_MAX - strlen(disk_path) - 1);
11174 if (stat(disk_path, &st) == 0) {
11175 struct sys_dev* hba;
11176 char *path;
11177
11178 path = devt_to_devpath(st.st_rdev);
11179 if (path == NULL)
11180 return "unknown";
11181 hba = find_disk_attached_hba(-1, path);
11182 if (hba && hba->type == SYS_DEV_SAS)
11183 drv = "isci";
11184 else if (hba && hba->type == SYS_DEV_SATA)
11185 drv = "ahci";
11186 else if (hba && hba->type == SYS_DEV_VMD)
11187 drv = "vmd";
11188 else if (hba && hba->type == SYS_DEV_NVME)
11189 drv = "nvme";
11190 else
11191 drv = "unknown";
11192 dprintf("path: %s hba: %s attached: %s\n",
11193 path, (hba) ? hba->path : "NULL", drv);
11194 free(path);
11195 }
11196 return drv;
11197 }
11198
11199 static char *imsm_find_array_devnm_by_subdev(int subdev, char *container)
11200 {
11201 static char devnm[32];
11202 char subdev_name[20];
11203 struct mdstat_ent *mdstat;
11204
11205 sprintf(subdev_name, "%d", subdev);
11206 mdstat = mdstat_by_subdev(subdev_name, container);
11207 if (!mdstat)
11208 return NULL;
11209
11210 strcpy(devnm, mdstat->devnm);
11211 free_mdstat(mdstat);
11212 return devnm;
11213 }
11214
11215 static int imsm_reshape_is_allowed_on_container(struct supertype *st,
11216 struct geo_params *geo,
11217 int *old_raid_disks,
11218 int direction)
11219 {
11220 /* currently we only support increasing the number of devices
11221 * for a container. This increases the number of device for each
11222 * member array. They must all be RAID0 or RAID5.
11223 */
11224 int ret_val = 0;
11225 struct mdinfo *info, *member;
11226 int devices_that_can_grow = 0;
11227
11228 dprintf("imsm: imsm_reshape_is_allowed_on_container(ENTER): st->devnm = (%s)\n", st->devnm);
11229
11230 if (geo->size > 0 ||
11231 geo->level != UnSet ||
11232 geo->layout != UnSet ||
11233 geo->chunksize != 0 ||
11234 geo->raid_disks == UnSet) {
11235 dprintf("imsm: Container operation is allowed for raid disks number change only.\n");
11236 return ret_val;
11237 }
11238
11239 if (direction == ROLLBACK_METADATA_CHANGES) {
11240 dprintf("imsm: Metadata changes rollback is not supported for container operation.\n");
11241 return ret_val;
11242 }
11243
11244 info = container_content_imsm(st, NULL);
11245 for (member = info; member; member = member->next) {
11246 char *result;
11247
11248 dprintf("imsm: checking device_num: %i\n",
11249 member->container_member);
11250
11251 if (geo->raid_disks <= member->array.raid_disks) {
11252 /* we work on container for Online Capacity Expansion
11253 * only so raid_disks has to grow
11254 */
11255 dprintf("imsm: for container operation raid disks increase is required\n");
11256 break;
11257 }
11258
11259 if (info->array.level != 0 && info->array.level != 5) {
11260 /* we cannot use this container with other raid level
11261 */
11262 dprintf("imsm: for container operation wrong raid level (%i) detected\n",
11263 info->array.level);
11264 break;
11265 } else {
11266 /* check for platform support
11267 * for this raid level configuration
11268 */
11269 struct intel_super *super = st->sb;
11270 if (!is_raid_level_supported(super->orom,
11271 member->array.level,
11272 geo->raid_disks)) {
11273 dprintf("platform does not support raid%d with %d disk%s\n",
11274 info->array.level,
11275 geo->raid_disks,
11276 geo->raid_disks > 1 ? "s" : "");
11277 break;
11278 }
11279 /* check if component size is aligned to chunk size
11280 */
11281 if (info->component_size %
11282 (info->array.chunk_size/512)) {
11283 dprintf("Component size is not aligned to chunk size\n");
11284 break;
11285 }
11286 }
11287
11288 if (*old_raid_disks &&
11289 info->array.raid_disks != *old_raid_disks)
11290 break;
11291 *old_raid_disks = info->array.raid_disks;
11292
11293 /* All raid5 and raid0 volumes in container
11294 * have to be ready for Online Capacity Expansion
11295 * so they need to be assembled. We have already
11296 * checked that no recovery etc is happening.
11297 */
11298 result = imsm_find_array_devnm_by_subdev(member->container_member,
11299 st->container_devnm);
11300 if (result == NULL) {
11301 dprintf("imsm: cannot find array\n");
11302 break;
11303 }
11304 devices_that_can_grow++;
11305 }
11306 sysfs_free(info);
11307 if (!member && devices_that_can_grow)
11308 ret_val = 1;
11309
11310 if (ret_val)
11311 dprintf("Container operation allowed\n");
11312 else
11313 dprintf("Error: %i\n", ret_val);
11314
11315 return ret_val;
11316 }
11317
11318 /* Function: get_spares_for_grow
11319 * Description: Allocates memory and creates list of spare devices
11320 * avaliable in container. Checks if spare drive size is acceptable.
11321 * Parameters: Pointer to the supertype structure
11322 * Returns: Pointer to the list of spare devices (mdinfo structure) on success,
11323 * NULL if fail
11324 */
11325 static struct mdinfo *get_spares_for_grow(struct supertype *st)
11326 {
11327 struct spare_criteria sc;
11328
11329 get_spare_criteria_imsm(st, &sc);
11330 return container_choose_spares(st, &sc, NULL, NULL, NULL, 0);
11331 }
11332
11333 /******************************************************************************
11334 * function: imsm_create_metadata_update_for_reshape
11335 * Function creates update for whole IMSM container.
11336 *
11337 ******************************************************************************/
11338 static int imsm_create_metadata_update_for_reshape(
11339 struct supertype *st,
11340 struct geo_params *geo,
11341 int old_raid_disks,
11342 struct imsm_update_reshape **updatep)
11343 {
11344 struct intel_super *super = st->sb;
11345 struct imsm_super *mpb = super->anchor;
11346 int update_memory_size;
11347 struct imsm_update_reshape *u;
11348 struct mdinfo *spares;
11349 int i;
11350 int delta_disks;
11351 struct mdinfo *dev;
11352
11353 dprintf("(enter) raid_disks = %i\n", geo->raid_disks);
11354
11355 delta_disks = geo->raid_disks - old_raid_disks;
11356
11357 /* size of all update data without anchor */
11358 update_memory_size = sizeof(struct imsm_update_reshape);
11359
11360 /* now add space for spare disks that we need to add. */
11361 update_memory_size += sizeof(u->new_disks[0]) * (delta_disks - 1);
11362
11363 u = xcalloc(1, update_memory_size);
11364 u->type = update_reshape_container_disks;
11365 u->old_raid_disks = old_raid_disks;
11366 u->new_raid_disks = geo->raid_disks;
11367
11368 /* now get spare disks list
11369 */
11370 spares = get_spares_for_grow(st);
11371
11372 if (spares == NULL || delta_disks > spares->array.spare_disks) {
11373 pr_err("imsm: ERROR: Cannot get spare devices for %s.\n", geo->dev_name);
11374 i = -1;
11375 goto abort;
11376 }
11377
11378 /* we have got spares
11379 * update disk list in imsm_disk list table in anchor
11380 */
11381 dprintf("imsm: %i spares are available.\n\n",
11382 spares->array.spare_disks);
11383
11384 dev = spares->devs;
11385 for (i = 0; i < delta_disks; i++) {
11386 struct dl *dl;
11387
11388 if (dev == NULL)
11389 break;
11390 u->new_disks[i] = makedev(dev->disk.major,
11391 dev->disk.minor);
11392 dl = get_disk_super(super, dev->disk.major, dev->disk.minor);
11393 dl->index = mpb->num_disks;
11394 mpb->num_disks++;
11395 dev = dev->next;
11396 }
11397
11398 abort:
11399 /* free spares
11400 */
11401 sysfs_free(spares);
11402
11403 dprintf("imsm: reshape update preparation :");
11404 if (i == delta_disks) {
11405 dprintf_cont(" OK\n");
11406 *updatep = u;
11407 return update_memory_size;
11408 }
11409 free(u);
11410 dprintf_cont(" Error\n");
11411
11412 return 0;
11413 }
11414
11415 /******************************************************************************
11416 * function: imsm_create_metadata_update_for_size_change()
11417 * Creates update for IMSM array for array size change.
11418 *
11419 ******************************************************************************/
11420 static int imsm_create_metadata_update_for_size_change(
11421 struct supertype *st,
11422 struct geo_params *geo,
11423 struct imsm_update_size_change **updatep)
11424 {
11425 struct intel_super *super = st->sb;
11426 int update_memory_size;
11427 struct imsm_update_size_change *u;
11428
11429 dprintf("(enter) New size = %llu\n", geo->size);
11430
11431 /* size of all update data without anchor */
11432 update_memory_size = sizeof(struct imsm_update_size_change);
11433
11434 u = xcalloc(1, update_memory_size);
11435 u->type = update_size_change;
11436 u->subdev = super->current_vol;
11437 u->new_size = geo->size;
11438
11439 dprintf("imsm: reshape update preparation : OK\n");
11440 *updatep = u;
11441
11442 return update_memory_size;
11443 }
11444
11445 /******************************************************************************
11446 * function: imsm_create_metadata_update_for_migration()
11447 * Creates update for IMSM array.
11448 *
11449 ******************************************************************************/
11450 static int imsm_create_metadata_update_for_migration(
11451 struct supertype *st,
11452 struct geo_params *geo,
11453 struct imsm_update_reshape_migration **updatep)
11454 {
11455 struct intel_super *super = st->sb;
11456 int update_memory_size;
11457 struct imsm_update_reshape_migration *u;
11458 struct imsm_dev *dev;
11459 int previous_level = -1;
11460
11461 dprintf("(enter) New Level = %i\n", geo->level);
11462
11463 /* size of all update data without anchor */
11464 update_memory_size = sizeof(struct imsm_update_reshape_migration);
11465
11466 u = xcalloc(1, update_memory_size);
11467 u->type = update_reshape_migration;
11468 u->subdev = super->current_vol;
11469 u->new_level = geo->level;
11470 u->new_layout = geo->layout;
11471 u->new_raid_disks = u->old_raid_disks = geo->raid_disks;
11472 u->new_disks[0] = -1;
11473 u->new_chunksize = -1;
11474
11475 dev = get_imsm_dev(super, u->subdev);
11476 if (dev) {
11477 struct imsm_map *map;
11478
11479 map = get_imsm_map(dev, MAP_0);
11480 if (map) {
11481 int current_chunk_size =
11482 __le16_to_cpu(map->blocks_per_strip) / 2;
11483
11484 if (geo->chunksize != current_chunk_size) {
11485 u->new_chunksize = geo->chunksize / 1024;
11486 dprintf("imsm: chunk size change from %i to %i\n",
11487 current_chunk_size, u->new_chunksize);
11488 }
11489 previous_level = map->raid_level;
11490 }
11491 }
11492 if (geo->level == 5 && previous_level == 0) {
11493 struct mdinfo *spares = NULL;
11494
11495 u->new_raid_disks++;
11496 spares = get_spares_for_grow(st);
11497 if (spares == NULL || spares->array.spare_disks < 1) {
11498 free(u);
11499 sysfs_free(spares);
11500 update_memory_size = 0;
11501 pr_err("cannot get spare device for requested migration\n");
11502 return 0;
11503 }
11504 sysfs_free(spares);
11505 }
11506 dprintf("imsm: reshape update preparation : OK\n");
11507 *updatep = u;
11508
11509 return update_memory_size;
11510 }
11511
11512 static void imsm_update_metadata_locally(struct supertype *st,
11513 void *buf, int len)
11514 {
11515 struct metadata_update mu;
11516
11517 mu.buf = buf;
11518 mu.len = len;
11519 mu.space = NULL;
11520 mu.space_list = NULL;
11521 mu.next = NULL;
11522 if (imsm_prepare_update(st, &mu))
11523 imsm_process_update(st, &mu);
11524
11525 while (mu.space_list) {
11526 void **space = mu.space_list;
11527 mu.space_list = *space;
11528 free(space);
11529 }
11530 }
11531
11532 /***************************************************************************
11533 * Function: imsm_analyze_change
11534 * Description: Function analyze change for single volume
11535 * and validate if transition is supported
11536 * Parameters: Geometry parameters, supertype structure,
11537 * metadata change direction (apply/rollback)
11538 * Returns: Operation type code on success, -1 if fail
11539 ****************************************************************************/
11540 enum imsm_reshape_type imsm_analyze_change(struct supertype *st,
11541 struct geo_params *geo,
11542 int direction)
11543 {
11544 struct mdinfo info;
11545 int change = -1;
11546 int check_devs = 0;
11547 int chunk;
11548 /* number of added/removed disks in operation result */
11549 int devNumChange = 0;
11550 /* imsm compatible layout value for array geometry verification */
11551 int imsm_layout = -1;
11552 int data_disks;
11553 struct imsm_dev *dev;
11554 struct imsm_map *map;
11555 struct intel_super *super;
11556 unsigned long long current_size;
11557 unsigned long long free_size;
11558 unsigned long long max_size;
11559 int rv;
11560
11561 getinfo_super_imsm_volume(st, &info, NULL);
11562 if (geo->level != info.array.level && geo->level >= 0 &&
11563 geo->level != UnSet) {
11564 switch (info.array.level) {
11565 case 0:
11566 if (geo->level == 5) {
11567 change = CH_MIGRATION;
11568 if (geo->layout != ALGORITHM_LEFT_ASYMMETRIC) {
11569 pr_err("Error. Requested Layout not supported (left-asymmetric layout is supported only)!\n");
11570 change = -1;
11571 goto analyse_change_exit;
11572 }
11573 imsm_layout = geo->layout;
11574 check_devs = 1;
11575 devNumChange = 1; /* parity disk added */
11576 } else if (geo->level == 10) {
11577 change = CH_TAKEOVER;
11578 check_devs = 1;
11579 devNumChange = 2; /* two mirrors added */
11580 imsm_layout = 0x102; /* imsm supported layout */
11581 }
11582 break;
11583 case 1:
11584 case 10:
11585 if (geo->level == 0) {
11586 change = CH_TAKEOVER;
11587 check_devs = 1;
11588 devNumChange = -(geo->raid_disks/2);
11589 imsm_layout = 0; /* imsm raid0 layout */
11590 }
11591 break;
11592 }
11593 if (change == -1) {
11594 pr_err("Error. Level Migration from %d to %d not supported!\n",
11595 info.array.level, geo->level);
11596 goto analyse_change_exit;
11597 }
11598 } else
11599 geo->level = info.array.level;
11600
11601 if (geo->layout != info.array.layout &&
11602 (geo->layout != UnSet && geo->layout != -1)) {
11603 change = CH_MIGRATION;
11604 if (info.array.layout == 0 && info.array.level == 5 &&
11605 geo->layout == 5) {
11606 /* reshape 5 -> 4 */
11607 } else if (info.array.layout == 5 && info.array.level == 5 &&
11608 geo->layout == 0) {
11609 /* reshape 4 -> 5 */
11610 geo->layout = 0;
11611 geo->level = 5;
11612 } else {
11613 pr_err("Error. Layout Migration from %d to %d not supported!\n",
11614 info.array.layout, geo->layout);
11615 change = -1;
11616 goto analyse_change_exit;
11617 }
11618 } else {
11619 geo->layout = info.array.layout;
11620 if (imsm_layout == -1)
11621 imsm_layout = info.array.layout;
11622 }
11623
11624 if (geo->chunksize > 0 && geo->chunksize != UnSet &&
11625 geo->chunksize != info.array.chunk_size) {
11626 if (info.array.level == 10) {
11627 pr_err("Error. Chunk size change for RAID 10 is not supported.\n");
11628 change = -1;
11629 goto analyse_change_exit;
11630 } else if (info.component_size % (geo->chunksize/512)) {
11631 pr_err("New chunk size (%dK) does not evenly divide device size (%lluk). Aborting...\n",
11632 geo->chunksize/1024, info.component_size/2);
11633 change = -1;
11634 goto analyse_change_exit;
11635 }
11636 change = CH_MIGRATION;
11637 } else {
11638 geo->chunksize = info.array.chunk_size;
11639 }
11640
11641 chunk = geo->chunksize / 1024;
11642
11643 super = st->sb;
11644 dev = get_imsm_dev(super, super->current_vol);
11645 map = get_imsm_map(dev, MAP_0);
11646 data_disks = imsm_num_data_members(map);
11647 /* compute current size per disk member
11648 */
11649 current_size = info.custom_array_size / data_disks;
11650
11651 if (geo->size > 0 && geo->size != MAX_SIZE) {
11652 /* align component size
11653 */
11654 geo->size = imsm_component_size_alignment_check(
11655 get_imsm_raid_level(dev->vol.map),
11656 chunk * 1024, super->sector_size,
11657 geo->size * 2);
11658 if (geo->size == 0) {
11659 pr_err("Error. Size expansion is supported only (current size is %llu, requested size /rounded/ is 0).\n",
11660 current_size);
11661 goto analyse_change_exit;
11662 }
11663 }
11664
11665 if (current_size != geo->size && geo->size > 0) {
11666 if (change != -1) {
11667 pr_err("Error. Size change should be the only one at a time.\n");
11668 change = -1;
11669 goto analyse_change_exit;
11670 }
11671 if ((super->current_vol + 1) != super->anchor->num_raid_devs) {
11672 pr_err("Error. The last volume in container can be expanded only (%i/%s).\n",
11673 super->current_vol, st->devnm);
11674 goto analyse_change_exit;
11675 }
11676 /* check the maximum available size
11677 */
11678 rv = imsm_get_free_size(st, dev->vol.map->num_members,
11679 0, chunk, &free_size);
11680 if (rv == 0)
11681 /* Cannot find maximum available space
11682 */
11683 max_size = 0;
11684 else {
11685 max_size = free_size + current_size;
11686 /* align component size
11687 */
11688 max_size = imsm_component_size_alignment_check(
11689 get_imsm_raid_level(dev->vol.map),
11690 chunk * 1024, super->sector_size,
11691 max_size);
11692 }
11693 if (geo->size == MAX_SIZE) {
11694 /* requested size change to the maximum available size
11695 */
11696 if (max_size == 0) {
11697 pr_err("Error. Cannot find maximum available space.\n");
11698 change = -1;
11699 goto analyse_change_exit;
11700 } else
11701 geo->size = max_size;
11702 }
11703
11704 if (direction == ROLLBACK_METADATA_CHANGES) {
11705 /* accept size for rollback only
11706 */
11707 } else {
11708 /* round size due to metadata compatibility
11709 */
11710 geo->size = (geo->size >> SECT_PER_MB_SHIFT)
11711 << SECT_PER_MB_SHIFT;
11712 dprintf("Prepare update for size change to %llu\n",
11713 geo->size );
11714 if (current_size >= geo->size) {
11715 pr_err("Error. Size expansion is supported only (current size is %llu, requested size /rounded/ is %llu).\n",
11716 current_size, geo->size);
11717 goto analyse_change_exit;
11718 }
11719 if (max_size && geo->size > max_size) {
11720 pr_err("Error. Requested size is larger than maximum available size (maximum available size is %llu, requested size /rounded/ is %llu).\n",
11721 max_size, geo->size);
11722 goto analyse_change_exit;
11723 }
11724 }
11725 geo->size *= data_disks;
11726 geo->raid_disks = dev->vol.map->num_members;
11727 change = CH_ARRAY_SIZE;
11728 }
11729 if (!validate_geometry_imsm(st,
11730 geo->level,
11731 imsm_layout,
11732 geo->raid_disks + devNumChange,
11733 &chunk,
11734 geo->size, INVALID_SECTORS,
11735 0, 0, info.consistency_policy, 1))
11736 change = -1;
11737
11738 if (check_devs) {
11739 struct intel_super *super = st->sb;
11740 struct imsm_super *mpb = super->anchor;
11741
11742 if (mpb->num_raid_devs > 1) {
11743 pr_err("Error. Cannot perform operation on %s- for this operation it MUST be single array in container\n",
11744 geo->dev_name);
11745 change = -1;
11746 }
11747 }
11748
11749 analyse_change_exit:
11750 if (direction == ROLLBACK_METADATA_CHANGES &&
11751 (change == CH_MIGRATION || change == CH_TAKEOVER)) {
11752 dprintf("imsm: Metadata changes rollback is not supported for migration and takeover operations.\n");
11753 change = -1;
11754 }
11755 return change;
11756 }
11757
11758 int imsm_takeover(struct supertype *st, struct geo_params *geo)
11759 {
11760 struct intel_super *super = st->sb;
11761 struct imsm_update_takeover *u;
11762
11763 u = xmalloc(sizeof(struct imsm_update_takeover));
11764
11765 u->type = update_takeover;
11766 u->subarray = super->current_vol;
11767
11768 /* 10->0 transition */
11769 if (geo->level == 0)
11770 u->direction = R10_TO_R0;
11771
11772 /* 0->10 transition */
11773 if (geo->level == 10)
11774 u->direction = R0_TO_R10;
11775
11776 /* update metadata locally */
11777 imsm_update_metadata_locally(st, u,
11778 sizeof(struct imsm_update_takeover));
11779 /* and possibly remotely */
11780 if (st->update_tail)
11781 append_metadata_update(st, u,
11782 sizeof(struct imsm_update_takeover));
11783 else
11784 free(u);
11785
11786 return 0;
11787 }
11788
11789 /* Flush size update if size calculated by num_data_stripes is higher than
11790 * imsm_dev_size to eliminate differences during reshape.
11791 * Mdmon will recalculate them correctly.
11792 * If subarray index is not set then check whole container.
11793 * Returns:
11794 * 0 - no error occurred
11795 * 1 - error detected
11796 */
11797 static int imsm_fix_size_mismatch(struct supertype *st, int subarray_index)
11798 {
11799 struct intel_super *super = st->sb;
11800 int tmp = super->current_vol;
11801 int ret_val = 1;
11802 int i;
11803
11804 for (i = 0; i < super->anchor->num_raid_devs; i++) {
11805 if (subarray_index >= 0 && i != subarray_index)
11806 continue;
11807 super->current_vol = i;
11808 struct imsm_dev *dev = get_imsm_dev(super, super->current_vol);
11809 struct imsm_map *map = get_imsm_map(dev, MAP_0);
11810 unsigned int disc_count = imsm_num_data_members(map);
11811 struct geo_params geo;
11812 struct imsm_update_size_change *update;
11813 unsigned long long calc_size = per_dev_array_size(map) * disc_count;
11814 unsigned long long d_size = imsm_dev_size(dev);
11815 int u_size;
11816
11817 if (calc_size == d_size || dev->vol.migr_type == MIGR_GEN_MIGR)
11818 continue;
11819
11820 /* There is a difference, verify that imsm_dev_size is
11821 * rounded correctly and push update.
11822 */
11823 if (d_size != round_size_to_mb(d_size, disc_count)) {
11824 dprintf("imsm: Size of volume %d is not rounded correctly\n",
11825 i);
11826 goto exit;
11827 }
11828 memset(&geo, 0, sizeof(struct geo_params));
11829 geo.size = d_size;
11830 u_size = imsm_create_metadata_update_for_size_change(st, &geo,
11831 &update);
11832 if (u_size < 1) {
11833 dprintf("imsm: Cannot prepare size change update\n");
11834 goto exit;
11835 }
11836 imsm_update_metadata_locally(st, update, u_size);
11837 if (st->update_tail) {
11838 append_metadata_update(st, update, u_size);
11839 flush_metadata_updates(st);
11840 st->update_tail = &st->updates;
11841 } else {
11842 imsm_sync_metadata(st);
11843 }
11844 }
11845 ret_val = 0;
11846 exit:
11847 super->current_vol = tmp;
11848 return ret_val;
11849 }
11850
11851 static int imsm_reshape_super(struct supertype *st, unsigned long long size,
11852 int level,
11853 int layout, int chunksize, int raid_disks,
11854 int delta_disks, char *backup, char *dev,
11855 int direction, int verbose)
11856 {
11857 int ret_val = 1;
11858 struct geo_params geo;
11859
11860 dprintf("(enter)\n");
11861
11862 memset(&geo, 0, sizeof(struct geo_params));
11863
11864 geo.dev_name = dev;
11865 strcpy(geo.devnm, st->devnm);
11866 geo.size = size;
11867 geo.level = level;
11868 geo.layout = layout;
11869 geo.chunksize = chunksize;
11870 geo.raid_disks = raid_disks;
11871 if (delta_disks != UnSet)
11872 geo.raid_disks += delta_disks;
11873
11874 dprintf("for level : %i\n", geo.level);
11875 dprintf("for raid_disks : %i\n", geo.raid_disks);
11876
11877 if (strcmp(st->container_devnm, st->devnm) == 0) {
11878 /* On container level we can only increase number of devices. */
11879 dprintf("imsm: info: Container operation\n");
11880 int old_raid_disks = 0;
11881
11882 if (imsm_reshape_is_allowed_on_container(
11883 st, &geo, &old_raid_disks, direction)) {
11884 struct imsm_update_reshape *u = NULL;
11885 int len;
11886
11887 if (imsm_fix_size_mismatch(st, -1)) {
11888 dprintf("imsm: Cannot fix size mismatch\n");
11889 goto exit_imsm_reshape_super;
11890 }
11891
11892 len = imsm_create_metadata_update_for_reshape(
11893 st, &geo, old_raid_disks, &u);
11894
11895 if (len <= 0) {
11896 dprintf("imsm: Cannot prepare update\n");
11897 goto exit_imsm_reshape_super;
11898 }
11899
11900 ret_val = 0;
11901 /* update metadata locally */
11902 imsm_update_metadata_locally(st, u, len);
11903 /* and possibly remotely */
11904 if (st->update_tail)
11905 append_metadata_update(st, u, len);
11906 else
11907 free(u);
11908
11909 } else {
11910 pr_err("(imsm) Operation is not allowed on this container\n");
11911 }
11912 } else {
11913 /* On volume level we support following operations
11914 * - takeover: raid10 -> raid0; raid0 -> raid10
11915 * - chunk size migration
11916 * - migration: raid5 -> raid0; raid0 -> raid5
11917 */
11918 struct intel_super *super = st->sb;
11919 struct intel_dev *dev = super->devlist;
11920 int change;
11921 dprintf("imsm: info: Volume operation\n");
11922 /* find requested device */
11923 while (dev) {
11924 char *devnm =
11925 imsm_find_array_devnm_by_subdev(
11926 dev->index, st->container_devnm);
11927 if (devnm && strcmp(devnm, geo.devnm) == 0)
11928 break;
11929 dev = dev->next;
11930 }
11931 if (dev == NULL) {
11932 pr_err("Cannot find %s (%s) subarray\n",
11933 geo.dev_name, geo.devnm);
11934 goto exit_imsm_reshape_super;
11935 }
11936 super->current_vol = dev->index;
11937 change = imsm_analyze_change(st, &geo, direction);
11938 switch (change) {
11939 case CH_TAKEOVER:
11940 ret_val = imsm_takeover(st, &geo);
11941 break;
11942 case CH_MIGRATION: {
11943 struct imsm_update_reshape_migration *u = NULL;
11944 int len =
11945 imsm_create_metadata_update_for_migration(
11946 st, &geo, &u);
11947 if (len < 1) {
11948 dprintf("imsm: Cannot prepare update\n");
11949 break;
11950 }
11951 ret_val = 0;
11952 /* update metadata locally */
11953 imsm_update_metadata_locally(st, u, len);
11954 /* and possibly remotely */
11955 if (st->update_tail)
11956 append_metadata_update(st, u, len);
11957 else
11958 free(u);
11959 }
11960 break;
11961 case CH_ARRAY_SIZE: {
11962 struct imsm_update_size_change *u = NULL;
11963 int len =
11964 imsm_create_metadata_update_for_size_change(
11965 st, &geo, &u);
11966 if (len < 1) {
11967 dprintf("imsm: Cannot prepare update\n");
11968 break;
11969 }
11970 ret_val = 0;
11971 /* update metadata locally */
11972 imsm_update_metadata_locally(st, u, len);
11973 /* and possibly remotely */
11974 if (st->update_tail)
11975 append_metadata_update(st, u, len);
11976 else
11977 free(u);
11978 }
11979 break;
11980 default:
11981 ret_val = 1;
11982 }
11983 }
11984
11985 exit_imsm_reshape_super:
11986 dprintf("imsm: reshape_super Exit code = %i\n", ret_val);
11987 return ret_val;
11988 }
11989
11990 #define COMPLETED_OK 0
11991 #define COMPLETED_NONE 1
11992 #define COMPLETED_DELAYED 2
11993
11994 static int read_completed(int fd, unsigned long long *val)
11995 {
11996 int ret;
11997 char buf[50];
11998
11999 ret = sysfs_fd_get_str(fd, buf, 50);
12000 if (ret < 0)
12001 return ret;
12002
12003 ret = COMPLETED_OK;
12004 if (strncmp(buf, "none", 4) == 0) {
12005 ret = COMPLETED_NONE;
12006 } else if (strncmp(buf, "delayed", 7) == 0) {
12007 ret = COMPLETED_DELAYED;
12008 } else {
12009 char *ep;
12010 *val = strtoull(buf, &ep, 0);
12011 if (ep == buf || (*ep != 0 && *ep != '\n' && *ep != ' '))
12012 ret = -1;
12013 }
12014 return ret;
12015 }
12016
12017 /*******************************************************************************
12018 * Function: wait_for_reshape_imsm
12019 * Description: Function writes new sync_max value and waits until
12020 * reshape process reach new position
12021 * Parameters:
12022 * sra : general array info
12023 * ndata : number of disks in new array's layout
12024 * Returns:
12025 * 0 : success,
12026 * 1 : there is no reshape in progress,
12027 * -1 : fail
12028 ******************************************************************************/
12029 int wait_for_reshape_imsm(struct mdinfo *sra, int ndata)
12030 {
12031 int fd = sysfs_get_fd(sra, NULL, "sync_completed");
12032 int retry = 3;
12033 unsigned long long completed;
12034 /* to_complete : new sync_max position */
12035 unsigned long long to_complete = sra->reshape_progress;
12036 unsigned long long position_to_set = to_complete / ndata;
12037
12038 if (fd < 0) {
12039 dprintf("cannot open reshape_position\n");
12040 return 1;
12041 }
12042
12043 do {
12044 if (sysfs_fd_get_ll(fd, &completed) < 0) {
12045 if (!retry) {
12046 dprintf("cannot read reshape_position (no reshape in progres)\n");
12047 close(fd);
12048 return 1;
12049 }
12050 usleep(30000);
12051 } else
12052 break;
12053 } while (retry--);
12054
12055 if (completed > position_to_set) {
12056 dprintf("wrong next position to set %llu (%llu)\n",
12057 to_complete, position_to_set);
12058 close(fd);
12059 return -1;
12060 }
12061 dprintf("Position set: %llu\n", position_to_set);
12062 if (sysfs_set_num(sra, NULL, "sync_max",
12063 position_to_set) != 0) {
12064 dprintf("cannot set reshape position to %llu\n",
12065 position_to_set);
12066 close(fd);
12067 return -1;
12068 }
12069
12070 do {
12071 int rc;
12072 char action[20];
12073 int timeout = 3000;
12074
12075 sysfs_wait(fd, &timeout);
12076 if (sysfs_get_str(sra, NULL, "sync_action",
12077 action, 20) > 0 &&
12078 strncmp(action, "reshape", 7) != 0) {
12079 if (strncmp(action, "idle", 4) == 0)
12080 break;
12081 close(fd);
12082 return -1;
12083 }
12084
12085 rc = read_completed(fd, &completed);
12086 if (rc < 0) {
12087 dprintf("cannot read reshape_position (in loop)\n");
12088 close(fd);
12089 return 1;
12090 } else if (rc == COMPLETED_NONE)
12091 break;
12092 } while (completed < position_to_set);
12093
12094 close(fd);
12095 return 0;
12096 }
12097
12098 /*******************************************************************************
12099 * Function: check_degradation_change
12100 * Description: Check that array hasn't become failed.
12101 * Parameters:
12102 * info : for sysfs access
12103 * sources : source disks descriptors
12104 * degraded: previous degradation level
12105 * Returns:
12106 * degradation level
12107 ******************************************************************************/
12108 int check_degradation_change(struct mdinfo *info,
12109 int *sources,
12110 int degraded)
12111 {
12112 unsigned long long new_degraded;
12113 int rv;
12114
12115 rv = sysfs_get_ll(info, NULL, "degraded", &new_degraded);
12116 if (rv == -1 || (new_degraded != (unsigned long long)degraded)) {
12117 /* check each device to ensure it is still working */
12118 struct mdinfo *sd;
12119 new_degraded = 0;
12120 for (sd = info->devs ; sd ; sd = sd->next) {
12121 if (sd->disk.state & (1<<MD_DISK_FAULTY))
12122 continue;
12123 if (sd->disk.state & (1<<MD_DISK_SYNC)) {
12124 char sbuf[100];
12125
12126 if (sysfs_get_str(info,
12127 sd, "state", sbuf, sizeof(sbuf)) < 0 ||
12128 strstr(sbuf, "faulty") ||
12129 strstr(sbuf, "in_sync") == NULL) {
12130 /* this device is dead */
12131 sd->disk.state = (1<<MD_DISK_FAULTY);
12132 if (sd->disk.raid_disk >= 0 &&
12133 sources[sd->disk.raid_disk] >= 0) {
12134 close(sources[
12135 sd->disk.raid_disk]);
12136 sources[sd->disk.raid_disk] =
12137 -1;
12138 }
12139 new_degraded++;
12140 }
12141 }
12142 }
12143 }
12144
12145 return new_degraded;
12146 }
12147
12148 /*******************************************************************************
12149 * Function: imsm_manage_reshape
12150 * Description: Function finds array under reshape and it manages reshape
12151 * process. It creates stripes backups (if required) and sets
12152 * checkpoints.
12153 * Parameters:
12154 * afd : Backup handle (nattive) - not used
12155 * sra : general array info
12156 * reshape : reshape parameters - not used
12157 * st : supertype structure
12158 * blocks : size of critical section [blocks]
12159 * fds : table of source device descriptor
12160 * offsets : start of array (offest per devices)
12161 * dests : not used
12162 * destfd : table of destination device descriptor
12163 * destoffsets : table of destination offsets (per device)
12164 * Returns:
12165 * 1 : success, reshape is done
12166 * 0 : fail
12167 ******************************************************************************/
12168 static int imsm_manage_reshape(
12169 int afd, struct mdinfo *sra, struct reshape *reshape,
12170 struct supertype *st, unsigned long backup_blocks,
12171 int *fds, unsigned long long *offsets,
12172 int dests, int *destfd, unsigned long long *destoffsets)
12173 {
12174 int ret_val = 0;
12175 struct intel_super *super = st->sb;
12176 struct intel_dev *dv;
12177 unsigned int sector_size = super->sector_size;
12178 struct imsm_dev *dev = NULL;
12179 struct imsm_map *map_src, *map_dest;
12180 int migr_vol_qan = 0;
12181 int ndata, odata; /* [bytes] */
12182 int chunk; /* [bytes] */
12183 struct migr_record *migr_rec;
12184 char *buf = NULL;
12185 unsigned int buf_size; /* [bytes] */
12186 unsigned long long max_position; /* array size [bytes] */
12187 unsigned long long next_step; /* [blocks]/[bytes] */
12188 unsigned long long old_data_stripe_length;
12189 unsigned long long start_src; /* [bytes] */
12190 unsigned long long start; /* [bytes] */
12191 unsigned long long start_buf_shift; /* [bytes] */
12192 int degraded = 0;
12193 int source_layout = 0;
12194 int subarray_index = -1;
12195
12196 if (!sra)
12197 return ret_val;
12198
12199 if (!fds || !offsets)
12200 goto abort;
12201
12202 /* Find volume during the reshape */
12203 for (dv = super->devlist; dv; dv = dv->next) {
12204 if (dv->dev->vol.migr_type == MIGR_GEN_MIGR &&
12205 dv->dev->vol.migr_state == 1) {
12206 dev = dv->dev;
12207 migr_vol_qan++;
12208 subarray_index = dv->index;
12209 }
12210 }
12211 /* Only one volume can migrate at the same time */
12212 if (migr_vol_qan != 1) {
12213 pr_err("%s", migr_vol_qan ?
12214 "Number of migrating volumes greater than 1\n" :
12215 "There is no volume during migrationg\n");
12216 goto abort;
12217 }
12218
12219 map_dest = get_imsm_map(dev, MAP_0);
12220 map_src = get_imsm_map(dev, MAP_1);
12221 if (map_src == NULL)
12222 goto abort;
12223
12224 ndata = imsm_num_data_members(map_dest);
12225 odata = imsm_num_data_members(map_src);
12226
12227 chunk = __le16_to_cpu(map_src->blocks_per_strip) * 512;
12228 old_data_stripe_length = odata * chunk;
12229
12230 migr_rec = super->migr_rec;
12231
12232 /* initialize migration record for start condition */
12233 if (sra->reshape_progress == 0)
12234 init_migr_record_imsm(st, dev, sra);
12235 else {
12236 if (__le32_to_cpu(migr_rec->rec_status) != UNIT_SRC_NORMAL) {
12237 dprintf("imsm: cannot restart migration when data are present in copy area.\n");
12238 goto abort;
12239 }
12240 /* Save checkpoint to update migration record for current
12241 * reshape position (in md). It can be farther than current
12242 * reshape position in metadata.
12243 */
12244 if (save_checkpoint_imsm(st, sra, UNIT_SRC_NORMAL) == 1) {
12245 /* ignore error == 2, this can mean end of reshape here
12246 */
12247 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL, initial save)\n");
12248 goto abort;
12249 }
12250 }
12251
12252 /* size for data */
12253 buf_size = __le32_to_cpu(migr_rec->blocks_per_unit) * 512;
12254 /* extend buffer size for parity disk */
12255 buf_size += __le32_to_cpu(migr_rec->dest_depth_per_unit) * 512;
12256 /* add space for stripe alignment */
12257 buf_size += old_data_stripe_length;
12258 if (posix_memalign((void **)&buf, MAX_SECTOR_SIZE, buf_size)) {
12259 dprintf("imsm: Cannot allocate checkpoint buffer\n");
12260 goto abort;
12261 }
12262
12263 max_position = sra->component_size * ndata;
12264 source_layout = imsm_level_to_layout(map_src->raid_level);
12265
12266 while (current_migr_unit(migr_rec) <
12267 get_num_migr_units(migr_rec)) {
12268 /* current reshape position [blocks] */
12269 unsigned long long current_position =
12270 __le32_to_cpu(migr_rec->blocks_per_unit)
12271 * current_migr_unit(migr_rec);
12272 unsigned long long border;
12273
12274 /* Check that array hasn't become failed.
12275 */
12276 degraded = check_degradation_change(sra, fds, degraded);
12277 if (degraded > 1) {
12278 dprintf("imsm: Abort reshape due to degradation level (%i)\n", degraded);
12279 goto abort;
12280 }
12281
12282 next_step = __le32_to_cpu(migr_rec->blocks_per_unit);
12283
12284 if ((current_position + next_step) > max_position)
12285 next_step = max_position - current_position;
12286
12287 start = current_position * 512;
12288
12289 /* align reading start to old geometry */
12290 start_buf_shift = start % old_data_stripe_length;
12291 start_src = start - start_buf_shift;
12292
12293 border = (start_src / odata) - (start / ndata);
12294 border /= 512;
12295 if (border <= __le32_to_cpu(migr_rec->dest_depth_per_unit)) {
12296 /* save critical stripes to buf
12297 * start - start address of current unit
12298 * to backup [bytes]
12299 * start_src - start address of current unit
12300 * to backup alligned to source array
12301 * [bytes]
12302 */
12303 unsigned long long next_step_filler;
12304 unsigned long long copy_length = next_step * 512;
12305
12306 /* allign copy area length to stripe in old geometry */
12307 next_step_filler = ((copy_length + start_buf_shift)
12308 % old_data_stripe_length);
12309 if (next_step_filler)
12310 next_step_filler = (old_data_stripe_length
12311 - next_step_filler);
12312 dprintf("save_stripes() parameters: start = %llu,\tstart_src = %llu,\tnext_step*512 = %llu,\tstart_in_buf_shift = %llu,\tnext_step_filler = %llu\n",
12313 start, start_src, copy_length,
12314 start_buf_shift, next_step_filler);
12315
12316 if (save_stripes(fds, offsets, map_src->num_members,
12317 chunk, map_src->raid_level,
12318 source_layout, 0, NULL, start_src,
12319 copy_length +
12320 next_step_filler + start_buf_shift,
12321 buf)) {
12322 dprintf("imsm: Cannot save stripes to buffer\n");
12323 goto abort;
12324 }
12325 /* Convert data to destination format and store it
12326 * in backup general migration area
12327 */
12328 if (save_backup_imsm(st, dev, sra,
12329 buf + start_buf_shift, copy_length)) {
12330 dprintf("imsm: Cannot save stripes to target devices\n");
12331 goto abort;
12332 }
12333 if (save_checkpoint_imsm(st, sra,
12334 UNIT_SRC_IN_CP_AREA)) {
12335 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_IN_CP_AREA)\n");
12336 goto abort;
12337 }
12338 } else {
12339 /* set next step to use whole border area */
12340 border /= next_step;
12341 if (border > 1)
12342 next_step *= border;
12343 }
12344 /* When data backed up, checkpoint stored,
12345 * kick the kernel to reshape unit of data
12346 */
12347 next_step = next_step + sra->reshape_progress;
12348 /* limit next step to array max position */
12349 if (next_step > max_position)
12350 next_step = max_position;
12351 sysfs_set_num(sra, NULL, "suspend_lo", sra->reshape_progress);
12352 sysfs_set_num(sra, NULL, "suspend_hi", next_step);
12353 sra->reshape_progress = next_step;
12354
12355 /* wait until reshape finish */
12356 if (wait_for_reshape_imsm(sra, ndata)) {
12357 dprintf("wait_for_reshape_imsm returned error!\n");
12358 goto abort;
12359 }
12360 if (sigterm)
12361 goto abort;
12362
12363 if (save_checkpoint_imsm(st, sra, UNIT_SRC_NORMAL) == 1) {
12364 /* ignore error == 2, this can mean end of reshape here
12365 */
12366 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL)\n");
12367 goto abort;
12368 }
12369
12370 }
12371
12372 /* clear migr_rec on disks after successful migration */
12373 struct dl *d;
12374
12375 memset(super->migr_rec_buf, 0, MIGR_REC_BUF_SECTORS*MAX_SECTOR_SIZE);
12376 for (d = super->disks; d; d = d->next) {
12377 if (d->index < 0 || is_failed(&d->disk))
12378 continue;
12379 unsigned long long dsize;
12380
12381 get_dev_size(d->fd, NULL, &dsize);
12382 if (lseek64(d->fd, dsize - MIGR_REC_SECTOR_POSITION*sector_size,
12383 SEEK_SET) >= 0) {
12384 if ((unsigned int)write(d->fd, super->migr_rec_buf,
12385 MIGR_REC_BUF_SECTORS*sector_size) !=
12386 MIGR_REC_BUF_SECTORS*sector_size)
12387 perror("Write migr_rec failed");
12388 }
12389 }
12390
12391 /* return '1' if done */
12392 ret_val = 1;
12393
12394 /* After the reshape eliminate size mismatch in metadata.
12395 * Don't update md/component_size here, volume hasn't
12396 * to take whole space. It is allowed by kernel.
12397 * md/component_size will be set propoperly after next assembly.
12398 */
12399 imsm_fix_size_mismatch(st, subarray_index);
12400
12401 abort:
12402 free(buf);
12403 /* See Grow.c: abort_reshape() for further explanation */
12404 sysfs_set_num(sra, NULL, "suspend_lo", 0x7FFFFFFFFFFFFFFFULL);
12405 sysfs_set_num(sra, NULL, "suspend_hi", 0);
12406 sysfs_set_num(sra, NULL, "suspend_lo", 0);
12407
12408 return ret_val;
12409 }
12410
12411 /*******************************************************************************
12412 * Function: calculate_bitmap_min_chunksize
12413 * Description: Calculates the minimal valid bitmap chunk size
12414 * Parameters:
12415 * max_bits : indicate how many bits can be used for the bitmap
12416 * data_area_size : the size of the data area covered by the bitmap
12417 *
12418 * Returns:
12419 * The bitmap chunk size
12420 ******************************************************************************/
12421 static unsigned long long
12422 calculate_bitmap_min_chunksize(unsigned long long max_bits,
12423 unsigned long long data_area_size)
12424 {
12425 unsigned long long min_chunk =
12426 4096; /* sub-page chunks don't work yet.. */
12427 unsigned long long bits = data_area_size / min_chunk + 1;
12428
12429 while (bits > max_bits) {
12430 min_chunk *= 2;
12431 bits = (bits + 1) / 2;
12432 }
12433 return min_chunk;
12434 }
12435
12436 /*******************************************************************************
12437 * Function: calculate_bitmap_chunksize
12438 * Description: Calculates the bitmap chunk size for the given device
12439 * Parameters:
12440 * st : supertype information
12441 * dev : device for the bitmap
12442 *
12443 * Returns:
12444 * The bitmap chunk size
12445 ******************************************************************************/
12446 static unsigned long long calculate_bitmap_chunksize(struct supertype *st,
12447 struct imsm_dev *dev)
12448 {
12449 struct intel_super *super = st->sb;
12450 unsigned long long min_chunksize;
12451 unsigned long long result = IMSM_DEFAULT_BITMAP_CHUNKSIZE;
12452 size_t dev_size = imsm_dev_size(dev);
12453
12454 min_chunksize = calculate_bitmap_min_chunksize(
12455 IMSM_BITMAP_AREA_SIZE * super->sector_size, dev_size);
12456
12457 if (result < min_chunksize)
12458 result = min_chunksize;
12459
12460 return result;
12461 }
12462
12463 /*******************************************************************************
12464 * Function: init_bitmap_header
12465 * Description: Initialize the bitmap header structure
12466 * Parameters:
12467 * st : supertype information
12468 * bms : bitmap header struct to initialize
12469 * dev : device for the bitmap
12470 *
12471 * Returns:
12472 * 0 : success
12473 * -1 : fail
12474 ******************************************************************************/
12475 static int init_bitmap_header(struct supertype *st, struct bitmap_super_s *bms,
12476 struct imsm_dev *dev)
12477 {
12478 int vol_uuid[4];
12479
12480 if (!bms || !dev)
12481 return -1;
12482
12483 bms->magic = __cpu_to_le32(BITMAP_MAGIC);
12484 bms->version = __cpu_to_le32(BITMAP_MAJOR_HI);
12485 bms->daemon_sleep = __cpu_to_le32(IMSM_DEFAULT_BITMAP_DAEMON_SLEEP);
12486 bms->sync_size = __cpu_to_le64(IMSM_BITMAP_AREA_SIZE);
12487 bms->write_behind = __cpu_to_le32(0);
12488
12489 uuid_from_super_imsm(st, vol_uuid);
12490 memcpy(bms->uuid, vol_uuid, 16);
12491
12492 bms->chunksize = calculate_bitmap_chunksize(st, dev);
12493
12494 return 0;
12495 }
12496
12497 /*******************************************************************************
12498 * Function: validate_internal_bitmap_for_drive
12499 * Description: Verify if the bitmap header for a given drive.
12500 * Parameters:
12501 * st : supertype information
12502 * offset : The offset from the beginning of the drive where to look for
12503 * the bitmap header.
12504 * d : the drive info
12505 *
12506 * Returns:
12507 * 0 : success
12508 * -1 : fail
12509 ******************************************************************************/
12510 static int validate_internal_bitmap_for_drive(struct supertype *st,
12511 unsigned long long offset,
12512 struct dl *d)
12513 {
12514 struct intel_super *super = st->sb;
12515 int ret = -1;
12516 int vol_uuid[4];
12517 bitmap_super_t *bms;
12518 int fd;
12519
12520 if (!d)
12521 return -1;
12522
12523 void *read_buf;
12524
12525 if (posix_memalign(&read_buf, MAX_SECTOR_SIZE, IMSM_BITMAP_HEADER_SIZE))
12526 return -1;
12527
12528 fd = d->fd;
12529 if (fd < 0) {
12530 fd = open(d->devname, O_RDONLY, 0);
12531 if (fd < 0) {
12532 dprintf("cannot open the device %s\n", d->devname);
12533 goto abort;
12534 }
12535 }
12536
12537 if (lseek64(fd, offset * super->sector_size, SEEK_SET) < 0)
12538 goto abort;
12539 if (read(fd, read_buf, IMSM_BITMAP_HEADER_SIZE) !=
12540 IMSM_BITMAP_HEADER_SIZE)
12541 goto abort;
12542
12543 uuid_from_super_imsm(st, vol_uuid);
12544
12545 bms = read_buf;
12546 if ((bms->magic != __cpu_to_le32(BITMAP_MAGIC)) ||
12547 (bms->version != __cpu_to_le32(BITMAP_MAJOR_HI)) ||
12548 (!same_uuid((int *)bms->uuid, vol_uuid, st->ss->swapuuid))) {
12549 dprintf("wrong bitmap header detected\n");
12550 goto abort;
12551 }
12552
12553 ret = 0;
12554 abort:
12555 if ((d->fd < 0) && (fd >= 0))
12556 close(fd);
12557 if (read_buf)
12558 free(read_buf);
12559
12560 return ret;
12561 }
12562
12563 /*******************************************************************************
12564 * Function: validate_internal_bitmap_imsm
12565 * Description: Verify if the bitmap header is in place and with proper data.
12566 * Parameters:
12567 * st : supertype information
12568 *
12569 * Returns:
12570 * 0 : success or device w/o RWH_BITMAP
12571 * -1 : fail
12572 ******************************************************************************/
12573 static int validate_internal_bitmap_imsm(struct supertype *st)
12574 {
12575 struct intel_super *super = st->sb;
12576 struct imsm_dev *dev = get_imsm_dev(super, super->current_vol);
12577 unsigned long long offset;
12578 struct dl *d;
12579
12580 if (!dev)
12581 return -1;
12582
12583 if (dev->rwh_policy != RWH_BITMAP)
12584 return 0;
12585
12586 offset = get_bitmap_header_sector(super, super->current_vol);
12587 for (d = super->disks; d; d = d->next) {
12588 if (d->index < 0 || is_failed(&d->disk))
12589 continue;
12590
12591 if (validate_internal_bitmap_for_drive(st, offset, d)) {
12592 pr_err("imsm: bitmap validation failed\n");
12593 return -1;
12594 }
12595 }
12596 return 0;
12597 }
12598
12599 /*******************************************************************************
12600 * Function: add_internal_bitmap_imsm
12601 * Description: Mark the volume to use the bitmap and updates the chunk size value.
12602 * Parameters:
12603 * st : supertype information
12604 * chunkp : bitmap chunk size
12605 * delay : not used for imsm
12606 * write_behind : not used for imsm
12607 * size : not used for imsm
12608 * may_change : not used for imsm
12609 * amajor : not used for imsm
12610 *
12611 * Returns:
12612 * 0 : success
12613 * -1 : fail
12614 ******************************************************************************/
12615 static int add_internal_bitmap_imsm(struct supertype *st, int *chunkp,
12616 int delay, int write_behind,
12617 unsigned long long size, int may_change,
12618 int amajor)
12619 {
12620 struct intel_super *super = st->sb;
12621 int vol_idx = super->current_vol;
12622 struct imsm_dev *dev;
12623
12624 if (!super->devlist || vol_idx == -1 || !chunkp)
12625 return -1;
12626
12627 dev = get_imsm_dev(super, vol_idx);
12628
12629 if (!dev) {
12630 dprintf("cannot find the device for volume index %d\n",
12631 vol_idx);
12632 return -1;
12633 }
12634 dev->rwh_policy = RWH_BITMAP;
12635
12636 *chunkp = calculate_bitmap_chunksize(st, dev);
12637
12638 return 0;
12639 }
12640
12641 /*******************************************************************************
12642 * Function: locate_bitmap_imsm
12643 * Description: Seek 'fd' to start of write-intent-bitmap.
12644 * Parameters:
12645 * st : supertype information
12646 * fd : file descriptor for the device
12647 * node_num : not used for imsm
12648 *
12649 * Returns:
12650 * 0 : success
12651 * -1 : fail
12652 ******************************************************************************/
12653 static int locate_bitmap_imsm(struct supertype *st, int fd, int node_num)
12654 {
12655 struct intel_super *super = st->sb;
12656 unsigned long long offset;
12657 int vol_idx = super->current_vol;
12658
12659 if (!super->devlist || vol_idx == -1)
12660 return -1;
12661
12662 offset = get_bitmap_header_sector(super, super->current_vol);
12663 dprintf("bitmap header offset is %llu\n", offset);
12664
12665 lseek64(fd, offset << 9, 0);
12666
12667 return 0;
12668 }
12669
12670 /*******************************************************************************
12671 * Function: write_init_bitmap_imsm
12672 * Description: Write a bitmap header and prepares the area for the bitmap.
12673 * Parameters:
12674 * st : supertype information
12675 * fd : file descriptor for the device
12676 * update : not used for imsm
12677 *
12678 * Returns:
12679 * 0 : success
12680 * -1 : fail
12681 ******************************************************************************/
12682 static int write_init_bitmap_imsm(struct supertype *st, int fd,
12683 enum bitmap_update update)
12684 {
12685 struct intel_super *super = st->sb;
12686 int vol_idx = super->current_vol;
12687 int ret = 0;
12688 unsigned long long offset;
12689 bitmap_super_t bms = { 0 };
12690 size_t written = 0;
12691 size_t to_write;
12692 ssize_t rv_num;
12693 void *buf;
12694
12695 if (!super->devlist || !super->sector_size || vol_idx == -1)
12696 return -1;
12697
12698 struct imsm_dev *dev = get_imsm_dev(super, vol_idx);
12699
12700 /* first clear the space for bitmap header */
12701 unsigned long long bitmap_area_start =
12702 get_bitmap_header_sector(super, vol_idx);
12703
12704 dprintf("zeroing area start (%llu) and size (%u)\n", bitmap_area_start,
12705 IMSM_BITMAP_AND_HEADER_SIZE / super->sector_size);
12706 if (zero_disk_range(fd, bitmap_area_start,
12707 IMSM_BITMAP_HEADER_SIZE / super->sector_size)) {
12708 pr_err("imsm: cannot zeroing the space for the bitmap\n");
12709 return -1;
12710 }
12711
12712 /* The bitmap area should be filled with "1"s to perform initial
12713 * synchronization.
12714 */
12715 if (posix_memalign(&buf, MAX_SECTOR_SIZE, MAX_SECTOR_SIZE))
12716 return -1;
12717 memset(buf, 0xFF, MAX_SECTOR_SIZE);
12718 offset = get_bitmap_sector(super, vol_idx);
12719 lseek64(fd, offset << 9, 0);
12720 while (written < IMSM_BITMAP_AREA_SIZE) {
12721 to_write = IMSM_BITMAP_AREA_SIZE - written;
12722 if (to_write > MAX_SECTOR_SIZE)
12723 to_write = MAX_SECTOR_SIZE;
12724 rv_num = write(fd, buf, MAX_SECTOR_SIZE);
12725 if (rv_num != MAX_SECTOR_SIZE) {
12726 ret = -1;
12727 dprintf("cannot initialize bitmap area\n");
12728 goto abort;
12729 }
12730 written += rv_num;
12731 }
12732
12733 /* write a bitmap header */
12734 init_bitmap_header(st, &bms, dev);
12735 memset(buf, 0, MAX_SECTOR_SIZE);
12736 memcpy(buf, &bms, sizeof(bitmap_super_t));
12737 if (locate_bitmap_imsm(st, fd, 0)) {
12738 ret = -1;
12739 dprintf("cannot locate the bitmap\n");
12740 goto abort;
12741 }
12742 if (write(fd, buf, MAX_SECTOR_SIZE) != MAX_SECTOR_SIZE) {
12743 ret = -1;
12744 dprintf("cannot write the bitmap header\n");
12745 goto abort;
12746 }
12747 fsync(fd);
12748
12749 abort:
12750 free(buf);
12751
12752 return ret;
12753 }
12754
12755 /*******************************************************************************
12756 * Function: is_vol_to_setup_bitmap
12757 * Description: Checks if a bitmap should be activated on the dev.
12758 * Parameters:
12759 * info : info about the volume to setup the bitmap
12760 * dev : the device to check against bitmap creation
12761 *
12762 * Returns:
12763 * 0 : bitmap should be set up on the device
12764 * -1 : otherwise
12765 ******************************************************************************/
12766 static int is_vol_to_setup_bitmap(struct mdinfo *info, struct imsm_dev *dev)
12767 {
12768 if (!dev || !info)
12769 return -1;
12770
12771 if ((strcmp((char *)dev->volume, info->name) == 0) &&
12772 (dev->rwh_policy == RWH_BITMAP))
12773 return -1;
12774
12775 return 0;
12776 }
12777
12778 /*******************************************************************************
12779 * Function: set_bitmap_sysfs
12780 * Description: Set the sysfs atributes of a given volume to activate the bitmap.
12781 * Parameters:
12782 * info : info about the volume where the bitmap should be setup
12783 * chunksize : bitmap chunk size
12784 * location : location of the bitmap
12785 *
12786 * Returns:
12787 * 0 : success
12788 * -1 : fail
12789 ******************************************************************************/
12790 static int set_bitmap_sysfs(struct mdinfo *info, unsigned long long chunksize,
12791 char *location)
12792 {
12793 /* The bitmap/metadata is set to external to allow changing of value for
12794 * bitmap/location. When external is used, the kernel will treat an offset
12795 * related to the device's first lba (in opposition to the "internal" case
12796 * when this value is related to the beginning of the superblock).
12797 */
12798 if (sysfs_set_str(info, NULL, "bitmap/metadata", "external")) {
12799 dprintf("failed to set bitmap/metadata\n");
12800 return -1;
12801 }
12802
12803 /* It can only be changed when no bitmap is active.
12804 * Should be bigger than 512 and must be power of 2.
12805 * It is expecting the value in bytes.
12806 */
12807 if (sysfs_set_num(info, NULL, "bitmap/chunksize",
12808 __cpu_to_le32(chunksize))) {
12809 dprintf("failed to set bitmap/chunksize\n");
12810 return -1;
12811 }
12812
12813 /* It is expecting the value in sectors. */
12814 if (sysfs_set_num(info, NULL, "bitmap/space",
12815 __cpu_to_le64(IMSM_BITMAP_AREA_SIZE))) {
12816 dprintf("failed to set bitmap/space\n");
12817 return -1;
12818 }
12819
12820 /* Determines the delay between the bitmap updates.
12821 * It is expecting the value in seconds.
12822 */
12823 if (sysfs_set_num(info, NULL, "bitmap/time_base",
12824 __cpu_to_le64(IMSM_DEFAULT_BITMAP_DAEMON_SLEEP))) {
12825 dprintf("failed to set bitmap/time_base\n");
12826 return -1;
12827 }
12828
12829 /* It is expecting the value in sectors with a sign at the beginning. */
12830 if (sysfs_set_str(info, NULL, "bitmap/location", location)) {
12831 dprintf("failed to set bitmap/location\n");
12832 return -1;
12833 }
12834
12835 return 0;
12836 }
12837
12838 /*******************************************************************************
12839 * Function: set_bitmap_imsm
12840 * Description: Setup the bitmap for the given volume
12841 * Parameters:
12842 * st : supertype information
12843 * info : info about the volume where the bitmap should be setup
12844 *
12845 * Returns:
12846 * 0 : success
12847 * -1 : fail
12848 ******************************************************************************/
12849 static int set_bitmap_imsm(struct supertype *st, struct mdinfo *info)
12850 {
12851 struct intel_super *super = st->sb;
12852 int prev_current_vol = super->current_vol;
12853 struct imsm_dev *dev;
12854 int ret = -1;
12855 char location[16] = "";
12856 unsigned long long chunksize;
12857 struct intel_dev *dev_it;
12858
12859 for (dev_it = super->devlist; dev_it; dev_it = dev_it->next) {
12860 super->current_vol = dev_it->index;
12861 dev = get_imsm_dev(super, super->current_vol);
12862
12863 if (is_vol_to_setup_bitmap(info, dev)) {
12864 if (validate_internal_bitmap_imsm(st)) {
12865 dprintf("bitmap header validation failed\n");
12866 goto abort;
12867 }
12868
12869 chunksize = calculate_bitmap_chunksize(st, dev);
12870 dprintf("chunk size is %llu\n", chunksize);
12871
12872 snprintf(location, sizeof(location), "+%llu",
12873 get_bitmap_sector(super, super->current_vol));
12874 dprintf("bitmap offset is %s\n", location);
12875
12876 if (set_bitmap_sysfs(info, chunksize, location)) {
12877 dprintf("cannot setup the bitmap\n");
12878 goto abort;
12879 }
12880 }
12881 }
12882 ret = 0;
12883 abort:
12884 super->current_vol = prev_current_vol;
12885 return ret;
12886 }
12887
12888 struct superswitch super_imsm = {
12889 .examine_super = examine_super_imsm,
12890 .brief_examine_super = brief_examine_super_imsm,
12891 .brief_examine_subarrays = brief_examine_subarrays_imsm,
12892 .export_examine_super = export_examine_super_imsm,
12893 .detail_super = detail_super_imsm,
12894 .brief_detail_super = brief_detail_super_imsm,
12895 .write_init_super = write_init_super_imsm,
12896 .validate_geometry = validate_geometry_imsm,
12897 .add_to_super = add_to_super_imsm,
12898 .remove_from_super = remove_from_super_imsm,
12899 .detail_platform = detail_platform_imsm,
12900 .export_detail_platform = export_detail_platform_imsm,
12901 .kill_subarray = kill_subarray_imsm,
12902 .update_subarray = update_subarray_imsm,
12903 .load_container = load_container_imsm,
12904 .default_geometry = default_geometry_imsm,
12905 .get_disk_controller_domain = imsm_get_disk_controller_domain,
12906 .reshape_super = imsm_reshape_super,
12907 .manage_reshape = imsm_manage_reshape,
12908 .recover_backup = recover_backup_imsm,
12909 .examine_badblocks = examine_badblocks_imsm,
12910 .match_home = match_home_imsm,
12911 .uuid_from_super= uuid_from_super_imsm,
12912 .getinfo_super = getinfo_super_imsm,
12913 .getinfo_super_disks = getinfo_super_disks_imsm,
12914 .update_super = update_super_imsm,
12915
12916 .avail_size = avail_size_imsm,
12917 .get_spare_criteria = get_spare_criteria_imsm,
12918
12919 .compare_super = compare_super_imsm,
12920
12921 .load_super = load_super_imsm,
12922 .init_super = init_super_imsm,
12923 .store_super = store_super_imsm,
12924 .free_super = free_super_imsm,
12925 .match_metadata_desc = match_metadata_desc_imsm,
12926 .container_content = container_content_imsm,
12927 .validate_container = validate_container_imsm,
12928
12929 .add_internal_bitmap = add_internal_bitmap_imsm,
12930 .locate_bitmap = locate_bitmap_imsm,
12931 .write_bitmap = write_init_bitmap_imsm,
12932 .set_bitmap = set_bitmap_imsm,
12933
12934 .write_init_ppl = write_init_ppl_imsm,
12935 .validate_ppl = validate_ppl_imsm,
12936
12937 .external = 1,
12938 .name = "imsm",
12939
12940 /* for mdmon */
12941 .open_new = imsm_open_new,
12942 .set_array_state= imsm_set_array_state,
12943 .set_disk = imsm_set_disk,
12944 .sync_metadata = imsm_sync_metadata,
12945 .activate_spare = imsm_activate_spare,
12946 .process_update = imsm_process_update,
12947 .prepare_update = imsm_prepare_update,
12948 .record_bad_block = imsm_record_badblock,
12949 .clear_bad_block = imsm_clear_badblock,
12950 .get_bad_blocks = imsm_get_badblocks,
12951 };