]> git.ipfire.org Git - thirdparty/mdadm.git/blob - super-intel.c
e155a8ae99cb420724900d146a8e430786dfb223
[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 /**
370 * enum imsm_status - internal IMSM return values representation.
371 * @STATUS_OK: function succeeded.
372 * @STATUS_ERROR: General error ocurred (not specified).
373 *
374 * Typedefed to imsm_status_t.
375 */
376 typedef enum imsm_status {
377 IMSM_STATUS_ERROR = -1,
378 IMSM_STATUS_OK = 0,
379 } imsm_status_t;
380
381 struct md_list {
382 /* usage marker:
383 * 1: load metadata
384 * 2: metadata does not match
385 * 4: already checked
386 */
387 int used;
388 char *devname;
389 int found;
390 int container;
391 dev_t st_rdev;
392 struct md_list *next;
393 };
394
395 #define pr_vrb(fmt, arg...) (void) (verbose && pr_err(fmt, ##arg))
396
397 static __u8 migr_type(struct imsm_dev *dev)
398 {
399 if (dev->vol.migr_type == MIGR_VERIFY &&
400 dev->status & DEV_VERIFY_AND_FIX)
401 return MIGR_REPAIR;
402 else
403 return dev->vol.migr_type;
404 }
405
406 static void set_migr_type(struct imsm_dev *dev, __u8 migr_type)
407 {
408 /* for compatibility with older oroms convert MIGR_REPAIR, into
409 * MIGR_VERIFY w/ DEV_VERIFY_AND_FIX status
410 */
411 if (migr_type == MIGR_REPAIR) {
412 dev->vol.migr_type = MIGR_VERIFY;
413 dev->status |= DEV_VERIFY_AND_FIX;
414 } else {
415 dev->vol.migr_type = migr_type;
416 dev->status &= ~DEV_VERIFY_AND_FIX;
417 }
418 }
419
420 static unsigned int sector_count(__u32 bytes, unsigned int sector_size)
421 {
422 return ROUND_UP(bytes, sector_size) / sector_size;
423 }
424
425 static unsigned int mpb_sectors(struct imsm_super *mpb,
426 unsigned int sector_size)
427 {
428 return sector_count(__le32_to_cpu(mpb->mpb_size), sector_size);
429 }
430
431 struct intel_dev {
432 struct imsm_dev *dev;
433 struct intel_dev *next;
434 unsigned index;
435 };
436
437 struct intel_hba {
438 enum sys_dev_type type;
439 char *path;
440 char *pci_id;
441 struct intel_hba *next;
442 };
443
444 enum action {
445 DISK_REMOVE = 1,
446 DISK_ADD
447 };
448 /* internal representation of IMSM metadata */
449 struct intel_super {
450 union {
451 void *buf; /* O_DIRECT buffer for reading/writing metadata */
452 struct imsm_super *anchor; /* immovable parameters */
453 };
454 union {
455 void *migr_rec_buf; /* buffer for I/O operations */
456 struct migr_record *migr_rec; /* migration record */
457 };
458 int clean_migration_record_by_mdmon; /* when reshape is switched to next
459 array, it indicates that mdmon is allowed to clean migration
460 record */
461 size_t len; /* size of the 'buf' allocation */
462 size_t extra_space; /* extra space in 'buf' that is not used yet */
463 void *next_buf; /* for realloc'ing buf from the manager */
464 size_t next_len;
465 int updates_pending; /* count of pending updates for mdmon */
466 int current_vol; /* index of raid device undergoing creation */
467 unsigned long long create_offset; /* common start for 'current_vol' */
468 __u32 random; /* random data for seeding new family numbers */
469 struct intel_dev *devlist;
470 unsigned int sector_size; /* sector size of used member drives */
471 struct dl {
472 struct dl *next;
473 int index;
474 __u8 serial[MAX_RAID_SERIAL_LEN];
475 int major, minor;
476 char *devname;
477 struct imsm_disk disk;
478 int fd;
479 int extent_cnt;
480 struct extent *e; /* for determining freespace @ create */
481 int raiddisk; /* slot to fill in autolayout */
482 enum action action;
483 } *disks, *current_disk;
484 struct dl *disk_mgmt_list; /* list of disks to add/remove while mdmon
485 active */
486 struct dl *missing; /* disks removed while we weren't looking */
487 struct bbm_log *bbm_log;
488 struct intel_hba *hba; /* device path of the raid controller for this metadata */
489 const struct imsm_orom *orom; /* platform firmware support */
490 struct intel_super *next; /* (temp) list for disambiguating family_num */
491 struct md_bb bb; /* memory for get_bad_blocks call */
492 };
493
494 struct intel_disk {
495 struct imsm_disk disk;
496 #define IMSM_UNKNOWN_OWNER (-1)
497 int owner;
498 struct intel_disk *next;
499 };
500
501 struct extent {
502 unsigned long long start, size;
503 };
504
505 /* definitions of reshape process types */
506 enum imsm_reshape_type {
507 CH_TAKEOVER,
508 CH_MIGRATION,
509 CH_ARRAY_SIZE,
510 };
511
512 /* definition of messages passed to imsm_process_update */
513 enum imsm_update_type {
514 update_activate_spare,
515 update_create_array,
516 update_kill_array,
517 update_rename_array,
518 update_add_remove_disk,
519 update_reshape_container_disks,
520 update_reshape_migration,
521 update_takeover,
522 update_general_migration_checkpoint,
523 update_size_change,
524 update_prealloc_badblocks_mem,
525 update_rwh_policy,
526 };
527
528 struct imsm_update_activate_spare {
529 enum imsm_update_type type;
530 struct dl *dl;
531 int slot;
532 int array;
533 struct imsm_update_activate_spare *next;
534 };
535
536 struct geo_params {
537 char devnm[32];
538 char *dev_name;
539 unsigned long long size;
540 int level;
541 int layout;
542 int chunksize;
543 int raid_disks;
544 };
545
546 enum takeover_direction {
547 R10_TO_R0,
548 R0_TO_R10
549 };
550 struct imsm_update_takeover {
551 enum imsm_update_type type;
552 int subarray;
553 enum takeover_direction direction;
554 };
555
556 struct imsm_update_reshape {
557 enum imsm_update_type type;
558 int old_raid_disks;
559 int new_raid_disks;
560
561 int new_disks[1]; /* new_raid_disks - old_raid_disks makedev number */
562 };
563
564 struct imsm_update_reshape_migration {
565 enum imsm_update_type type;
566 int old_raid_disks;
567 int new_raid_disks;
568 /* fields for array migration changes
569 */
570 int subdev;
571 int new_level;
572 int new_layout;
573 int new_chunksize;
574
575 int new_disks[1]; /* new_raid_disks - old_raid_disks makedev number */
576 };
577
578 struct imsm_update_size_change {
579 enum imsm_update_type type;
580 int subdev;
581 long long new_size;
582 };
583
584 struct imsm_update_general_migration_checkpoint {
585 enum imsm_update_type type;
586 __u64 curr_migr_unit;
587 };
588
589 struct disk_info {
590 __u8 serial[MAX_RAID_SERIAL_LEN];
591 };
592
593 struct imsm_update_create_array {
594 enum imsm_update_type type;
595 int dev_idx;
596 struct imsm_dev dev;
597 };
598
599 struct imsm_update_kill_array {
600 enum imsm_update_type type;
601 int dev_idx;
602 };
603
604 struct imsm_update_rename_array {
605 enum imsm_update_type type;
606 __u8 name[MAX_RAID_SERIAL_LEN];
607 int dev_idx;
608 };
609
610 struct imsm_update_add_remove_disk {
611 enum imsm_update_type type;
612 };
613
614 struct imsm_update_prealloc_bb_mem {
615 enum imsm_update_type type;
616 };
617
618 struct imsm_update_rwh_policy {
619 enum imsm_update_type type;
620 int new_policy;
621 int dev_idx;
622 };
623
624 static const char *_sys_dev_type[] = {
625 [SYS_DEV_UNKNOWN] = "Unknown",
626 [SYS_DEV_SAS] = "SAS",
627 [SYS_DEV_SATA] = "SATA",
628 [SYS_DEV_NVME] = "NVMe",
629 [SYS_DEV_VMD] = "VMD"
630 };
631
632 const char *get_sys_dev_type(enum sys_dev_type type)
633 {
634 if (type >= SYS_DEV_MAX)
635 type = SYS_DEV_UNKNOWN;
636
637 return _sys_dev_type[type];
638 }
639
640 static struct intel_hba * alloc_intel_hba(struct sys_dev *device)
641 {
642 struct intel_hba *result = xmalloc(sizeof(*result));
643
644 result->type = device->type;
645 result->path = xstrdup(device->path);
646 result->next = NULL;
647 if (result->path && (result->pci_id = strrchr(result->path, '/')) != NULL)
648 result->pci_id++;
649
650 return result;
651 }
652
653 static struct intel_hba * find_intel_hba(struct intel_hba *hba, struct sys_dev *device)
654 {
655 struct intel_hba *result;
656
657 for (result = hba; result; result = result->next) {
658 if (result->type == device->type && strcmp(result->path, device->path) == 0)
659 break;
660 }
661 return result;
662 }
663
664 static int attach_hba_to_super(struct intel_super *super, struct sys_dev *device)
665 {
666 struct intel_hba *hba;
667
668 /* check if disk attached to Intel HBA */
669 hba = find_intel_hba(super->hba, device);
670 if (hba != NULL)
671 return 1;
672 /* Check if HBA is already attached to super */
673 if (super->hba == NULL) {
674 super->hba = alloc_intel_hba(device);
675 return 1;
676 }
677
678 hba = super->hba;
679 /* Intel metadata allows for all disks attached to the same type HBA.
680 * Do not support HBA types mixing
681 */
682 if (device->type != hba->type)
683 return 2;
684
685 /* Multiple same type HBAs can be used if they share the same OROM */
686 const struct imsm_orom *device_orom = get_orom_by_device_id(device->dev_id);
687
688 if (device_orom != super->orom)
689 return 2;
690
691 while (hba->next)
692 hba = hba->next;
693
694 hba->next = alloc_intel_hba(device);
695 return 1;
696 }
697
698 static struct sys_dev* find_disk_attached_hba(int fd, const char *devname)
699 {
700 struct sys_dev *list, *elem;
701 char *disk_path;
702
703 if ((list = find_intel_devices()) == NULL)
704 return 0;
705
706 if (!is_fd_valid(fd))
707 disk_path = (char *) devname;
708 else
709 disk_path = diskfd_to_devpath(fd, 1, NULL);
710
711 if (!disk_path)
712 return 0;
713
714 for (elem = list; elem; elem = elem->next)
715 if (path_attached_to_hba(disk_path, elem->path))
716 break;
717
718 if (disk_path != devname)
719 free(disk_path);
720
721 return elem;
722 }
723
724 static int find_intel_hba_capability(int fd, struct intel_super *super,
725 char *devname);
726
727 static struct supertype *match_metadata_desc_imsm(char *arg)
728 {
729 struct supertype *st;
730
731 if (strcmp(arg, "imsm") != 0 &&
732 strcmp(arg, "default") != 0
733 )
734 return NULL;
735
736 st = xcalloc(1, sizeof(*st));
737 st->ss = &super_imsm;
738 st->max_devs = IMSM_MAX_DEVICES;
739 st->minor_version = 0;
740 st->sb = NULL;
741 return st;
742 }
743
744 static __u8 *get_imsm_version(struct imsm_super *mpb)
745 {
746 return &mpb->sig[MPB_SIG_LEN];
747 }
748
749 /* retrieve a disk directly from the anchor when the anchor is known to be
750 * up-to-date, currently only at load time
751 */
752 static struct imsm_disk *__get_imsm_disk(struct imsm_super *mpb, __u8 index)
753 {
754 if (index >= mpb->num_disks)
755 return NULL;
756 return &mpb->disk[index];
757 }
758
759 /* retrieve the disk description based on a index of the disk
760 * in the sub-array
761 */
762 static struct dl *get_imsm_dl_disk(struct intel_super *super, __u8 index)
763 {
764 struct dl *d;
765
766 for (d = super->disks; d; d = d->next)
767 if (d->index == index)
768 return d;
769
770 return NULL;
771 }
772 /* retrieve a disk from the parsed metadata */
773 static struct imsm_disk *get_imsm_disk(struct intel_super *super, __u8 index)
774 {
775 struct dl *dl;
776
777 dl = get_imsm_dl_disk(super, index);
778 if (dl)
779 return &dl->disk;
780
781 return NULL;
782 }
783
784 /* generate a checksum directly from the anchor when the anchor is known to be
785 * up-to-date, currently only at load or write_super after coalescing
786 */
787 static __u32 __gen_imsm_checksum(struct imsm_super *mpb)
788 {
789 __u32 end = mpb->mpb_size / sizeof(end);
790 __u32 *p = (__u32 *) mpb;
791 __u32 sum = 0;
792
793 while (end--) {
794 sum += __le32_to_cpu(*p);
795 p++;
796 }
797
798 return sum - __le32_to_cpu(mpb->check_sum);
799 }
800
801 static size_t sizeof_imsm_map(struct imsm_map *map)
802 {
803 return sizeof(struct imsm_map) + sizeof(__u32) * (map->num_members - 1);
804 }
805
806 struct imsm_map *get_imsm_map(struct imsm_dev *dev, int second_map)
807 {
808 /* A device can have 2 maps if it is in the middle of a migration.
809 * If second_map is:
810 * MAP_0 - we return the first map
811 * MAP_1 - we return the second map if it exists, else NULL
812 * MAP_X - we return the second map if it exists, else the first
813 */
814 struct imsm_map *map = &dev->vol.map[0];
815 struct imsm_map *map2 = NULL;
816
817 if (dev->vol.migr_state)
818 map2 = (void *)map + sizeof_imsm_map(map);
819
820 switch (second_map) {
821 case MAP_0:
822 break;
823 case MAP_1:
824 map = map2;
825 break;
826 case MAP_X:
827 if (map2)
828 map = map2;
829 break;
830 default:
831 map = NULL;
832 }
833 return map;
834
835 }
836
837 /* return the size of the device.
838 * migr_state increases the returned size if map[0] were to be duplicated
839 */
840 static size_t sizeof_imsm_dev(struct imsm_dev *dev, int migr_state)
841 {
842 size_t size = sizeof(*dev) - sizeof(struct imsm_map) +
843 sizeof_imsm_map(get_imsm_map(dev, MAP_0));
844
845 /* migrating means an additional map */
846 if (dev->vol.migr_state)
847 size += sizeof_imsm_map(get_imsm_map(dev, MAP_1));
848 else if (migr_state)
849 size += sizeof_imsm_map(get_imsm_map(dev, MAP_0));
850
851 return size;
852 }
853
854 /* retrieve disk serial number list from a metadata update */
855 static struct disk_info *get_disk_info(struct imsm_update_create_array *update)
856 {
857 void *u = update;
858 struct disk_info *inf;
859
860 inf = u + sizeof(*update) - sizeof(struct imsm_dev) +
861 sizeof_imsm_dev(&update->dev, 0);
862
863 return inf;
864 }
865
866 /**
867 * __get_imsm_dev() - Get device with index from imsm_super.
868 * @mpb: &imsm_super pointer, not NULL.
869 * @index: Device index.
870 *
871 * Function works as non-NULL, aborting in such a case,
872 * when NULL would be returned.
873 *
874 * Device index should be in range 0 up to num_raid_devs.
875 * Function assumes the index was already verified.
876 * Index must be valid, otherwise abort() is called.
877 *
878 * Return: Pointer to corresponding imsm_dev.
879 *
880 */
881 static struct imsm_dev *__get_imsm_dev(struct imsm_super *mpb, __u8 index)
882 {
883 int offset;
884 int i;
885 void *_mpb = mpb;
886
887 if (index >= mpb->num_raid_devs)
888 goto error;
889
890 /* devices start after all disks */
891 offset = ((void *) &mpb->disk[mpb->num_disks]) - _mpb;
892
893 for (i = 0; i <= index; i++, offset += sizeof_imsm_dev(_mpb + offset, 0))
894 if (i == index)
895 return _mpb + offset;
896 error:
897 pr_err("cannot find imsm_dev with index %u in imsm_super\n", index);
898 abort();
899 }
900
901 /**
902 * get_imsm_dev() - Get device with index from intel_super.
903 * @super: &intel_super pointer, not NULL.
904 * @index: Device index.
905 *
906 * Function works as non-NULL, aborting in such a case,
907 * when NULL would be returned.
908 *
909 * Device index should be in range 0 up to num_raid_devs.
910 * Function assumes the index was already verified.
911 * Index must be valid, otherwise abort() is called.
912 *
913 * Return: Pointer to corresponding imsm_dev.
914 *
915 */
916 static struct imsm_dev *get_imsm_dev(struct intel_super *super, __u8 index)
917 {
918 struct intel_dev *dv;
919
920 if (index >= super->anchor->num_raid_devs)
921 goto error;
922
923 for (dv = super->devlist; dv; dv = dv->next)
924 if (dv->index == index)
925 return dv->dev;
926 error:
927 pr_err("cannot find imsm_dev with index %u in intel_super\n", index);
928 abort();
929 }
930
931 static inline unsigned long long __le48_to_cpu(const struct bbm_log_block_addr
932 *addr)
933 {
934 return ((((__u64)__le32_to_cpu(addr->dw1)) << 16) |
935 __le16_to_cpu(addr->w1));
936 }
937
938 static inline struct bbm_log_block_addr __cpu_to_le48(unsigned long long sec)
939 {
940 struct bbm_log_block_addr addr;
941
942 addr.w1 = __cpu_to_le16((__u16)(sec & 0xffff));
943 addr.dw1 = __cpu_to_le32((__u32)(sec >> 16) & 0xffffffff);
944 return addr;
945 }
946
947 /* get size of the bbm log */
948 static __u32 get_imsm_bbm_log_size(struct bbm_log *log)
949 {
950 if (!log || log->entry_count == 0)
951 return 0;
952
953 return sizeof(log->signature) +
954 sizeof(log->entry_count) +
955 log->entry_count * sizeof(struct bbm_log_entry);
956 }
957
958 /* check if bad block is not partially stored in bbm log */
959 static int is_stored_in_bbm(struct bbm_log *log, const __u8 idx, const unsigned
960 long long sector, const int length, __u32 *pos)
961 {
962 __u32 i;
963
964 for (i = *pos; i < log->entry_count; i++) {
965 struct bbm_log_entry *entry = &log->marked_block_entries[i];
966 unsigned long long bb_start;
967 unsigned long long bb_end;
968
969 bb_start = __le48_to_cpu(&entry->defective_block_start);
970 bb_end = bb_start + (entry->marked_count + 1);
971
972 if ((entry->disk_ordinal == idx) && (bb_start >= sector) &&
973 (bb_end <= sector + length)) {
974 *pos = i;
975 return 1;
976 }
977 }
978 return 0;
979 }
980
981 /* record new bad block in bbm log */
982 static int record_new_badblock(struct bbm_log *log, const __u8 idx, unsigned
983 long long sector, int length)
984 {
985 int new_bb = 0;
986 __u32 pos = 0;
987 struct bbm_log_entry *entry = NULL;
988
989 while (is_stored_in_bbm(log, idx, sector, length, &pos)) {
990 struct bbm_log_entry *e = &log->marked_block_entries[pos];
991
992 if ((e->marked_count + 1 == BBM_LOG_MAX_LBA_ENTRY_VAL) &&
993 (__le48_to_cpu(&e->defective_block_start) == sector)) {
994 sector += BBM_LOG_MAX_LBA_ENTRY_VAL;
995 length -= BBM_LOG_MAX_LBA_ENTRY_VAL;
996 pos = pos + 1;
997 continue;
998 }
999 entry = e;
1000 break;
1001 }
1002
1003 if (entry) {
1004 int cnt = (length <= BBM_LOG_MAX_LBA_ENTRY_VAL) ? length :
1005 BBM_LOG_MAX_LBA_ENTRY_VAL;
1006 entry->defective_block_start = __cpu_to_le48(sector);
1007 entry->marked_count = cnt - 1;
1008 if (cnt == length)
1009 return 1;
1010 sector += cnt;
1011 length -= cnt;
1012 }
1013
1014 new_bb = ROUND_UP(length, BBM_LOG_MAX_LBA_ENTRY_VAL) /
1015 BBM_LOG_MAX_LBA_ENTRY_VAL;
1016 if (log->entry_count + new_bb > BBM_LOG_MAX_ENTRIES)
1017 return 0;
1018
1019 while (length > 0) {
1020 int cnt = (length <= BBM_LOG_MAX_LBA_ENTRY_VAL) ? length :
1021 BBM_LOG_MAX_LBA_ENTRY_VAL;
1022 struct bbm_log_entry *entry =
1023 &log->marked_block_entries[log->entry_count];
1024
1025 entry->defective_block_start = __cpu_to_le48(sector);
1026 entry->marked_count = cnt - 1;
1027 entry->disk_ordinal = idx;
1028
1029 sector += cnt;
1030 length -= cnt;
1031
1032 log->entry_count++;
1033 }
1034
1035 return new_bb;
1036 }
1037
1038 /* clear all bad blocks for given disk */
1039 static void clear_disk_badblocks(struct bbm_log *log, const __u8 idx)
1040 {
1041 __u32 i = 0;
1042
1043 while (i < log->entry_count) {
1044 struct bbm_log_entry *entries = log->marked_block_entries;
1045
1046 if (entries[i].disk_ordinal == idx) {
1047 if (i < log->entry_count - 1)
1048 entries[i] = entries[log->entry_count - 1];
1049 log->entry_count--;
1050 } else {
1051 i++;
1052 }
1053 }
1054 }
1055
1056 /* clear given bad block */
1057 static int clear_badblock(struct bbm_log *log, const __u8 idx, const unsigned
1058 long long sector, const int length) {
1059 __u32 i = 0;
1060
1061 while (i < log->entry_count) {
1062 struct bbm_log_entry *entries = log->marked_block_entries;
1063
1064 if ((entries[i].disk_ordinal == idx) &&
1065 (__le48_to_cpu(&entries[i].defective_block_start) ==
1066 sector) && (entries[i].marked_count + 1 == length)) {
1067 if (i < log->entry_count - 1)
1068 entries[i] = entries[log->entry_count - 1];
1069 log->entry_count--;
1070 break;
1071 }
1072 i++;
1073 }
1074
1075 return 1;
1076 }
1077
1078 /* allocate and load BBM log from metadata */
1079 static int load_bbm_log(struct intel_super *super)
1080 {
1081 struct imsm_super *mpb = super->anchor;
1082 __u32 bbm_log_size = __le32_to_cpu(mpb->bbm_log_size);
1083
1084 super->bbm_log = xcalloc(1, sizeof(struct bbm_log));
1085 if (!super->bbm_log)
1086 return 1;
1087
1088 if (bbm_log_size) {
1089 struct bbm_log *log = (void *)mpb +
1090 __le32_to_cpu(mpb->mpb_size) - bbm_log_size;
1091
1092 __u32 entry_count;
1093
1094 if (bbm_log_size < sizeof(log->signature) +
1095 sizeof(log->entry_count))
1096 return 2;
1097
1098 entry_count = __le32_to_cpu(log->entry_count);
1099 if ((__le32_to_cpu(log->signature) != BBM_LOG_SIGNATURE) ||
1100 (entry_count > BBM_LOG_MAX_ENTRIES))
1101 return 3;
1102
1103 if (bbm_log_size !=
1104 sizeof(log->signature) + sizeof(log->entry_count) +
1105 entry_count * sizeof(struct bbm_log_entry))
1106 return 4;
1107
1108 memcpy(super->bbm_log, log, bbm_log_size);
1109 } else {
1110 super->bbm_log->signature = __cpu_to_le32(BBM_LOG_SIGNATURE);
1111 super->bbm_log->entry_count = 0;
1112 }
1113
1114 return 0;
1115 }
1116
1117 /* checks if bad block is within volume boundaries */
1118 static int is_bad_block_in_volume(const struct bbm_log_entry *entry,
1119 const unsigned long long start_sector,
1120 const unsigned long long size)
1121 {
1122 unsigned long long bb_start;
1123 unsigned long long bb_end;
1124
1125 bb_start = __le48_to_cpu(&entry->defective_block_start);
1126 bb_end = bb_start + (entry->marked_count + 1);
1127
1128 if (((bb_start >= start_sector) && (bb_start < start_sector + size)) ||
1129 ((bb_end >= start_sector) && (bb_end <= start_sector + size)))
1130 return 1;
1131
1132 return 0;
1133 }
1134
1135 /* get list of bad blocks on a drive for a volume */
1136 static void get_volume_badblocks(const struct bbm_log *log, const __u8 idx,
1137 const unsigned long long start_sector,
1138 const unsigned long long size,
1139 struct md_bb *bbs)
1140 {
1141 __u32 count = 0;
1142 __u32 i;
1143
1144 for (i = 0; i < log->entry_count; i++) {
1145 const struct bbm_log_entry *ent =
1146 &log->marked_block_entries[i];
1147 struct md_bb_entry *bb;
1148
1149 if ((ent->disk_ordinal == idx) &&
1150 is_bad_block_in_volume(ent, start_sector, size)) {
1151
1152 if (!bbs->entries) {
1153 bbs->entries = xmalloc(BBM_LOG_MAX_ENTRIES *
1154 sizeof(*bb));
1155 if (!bbs->entries)
1156 break;
1157 }
1158
1159 bb = &bbs->entries[count++];
1160 bb->sector = __le48_to_cpu(&ent->defective_block_start);
1161 bb->length = ent->marked_count + 1;
1162 }
1163 }
1164 bbs->count = count;
1165 }
1166
1167 /*
1168 * for second_map:
1169 * == MAP_0 get first map
1170 * == MAP_1 get second map
1171 * == MAP_X than get map according to the current migr_state
1172 */
1173 static __u32 get_imsm_ord_tbl_ent(struct imsm_dev *dev,
1174 int slot,
1175 int second_map)
1176 {
1177 struct imsm_map *map;
1178
1179 map = get_imsm_map(dev, second_map);
1180
1181 /* top byte identifies disk under rebuild */
1182 return __le32_to_cpu(map->disk_ord_tbl[slot]);
1183 }
1184
1185 #define ord_to_idx(ord) (((ord) << 8) >> 8)
1186 static __u32 get_imsm_disk_idx(struct imsm_dev *dev, int slot, int second_map)
1187 {
1188 __u32 ord = get_imsm_ord_tbl_ent(dev, slot, second_map);
1189
1190 return ord_to_idx(ord);
1191 }
1192
1193 static void set_imsm_ord_tbl_ent(struct imsm_map *map, int slot, __u32 ord)
1194 {
1195 map->disk_ord_tbl[slot] = __cpu_to_le32(ord);
1196 }
1197
1198 static int get_imsm_disk_slot(struct imsm_map *map, const unsigned int idx)
1199 {
1200 int slot;
1201 __u32 ord;
1202
1203 for (slot = 0; slot < map->num_members; slot++) {
1204 ord = __le32_to_cpu(map->disk_ord_tbl[slot]);
1205 if (ord_to_idx(ord) == idx)
1206 return slot;
1207 }
1208
1209 return IMSM_STATUS_ERROR;
1210 }
1211
1212 static int get_imsm_raid_level(struct imsm_map *map)
1213 {
1214 if (map->raid_level == 1) {
1215 if (map->num_members == 2)
1216 return 1;
1217 else
1218 return 10;
1219 }
1220
1221 return map->raid_level;
1222 }
1223
1224 /**
1225 * get_disk_slot_in_dev() - retrieve disk slot from &imsm_dev.
1226 * @super: &intel_super pointer, not NULL.
1227 * @dev_idx: imsm device index.
1228 * @idx: disk index.
1229 *
1230 * Return: Slot on success, IMSM_STATUS_ERROR otherwise.
1231 */
1232 static int get_disk_slot_in_dev(struct intel_super *super, const __u8 dev_idx,
1233 const unsigned int idx)
1234 {
1235 struct imsm_dev *dev = get_imsm_dev(super, dev_idx);
1236 struct imsm_map *map = get_imsm_map(dev, MAP_0);
1237
1238 return get_imsm_disk_slot(map, idx);
1239 }
1240
1241 static int cmp_extent(const void *av, const void *bv)
1242 {
1243 const struct extent *a = av;
1244 const struct extent *b = bv;
1245 if (a->start < b->start)
1246 return -1;
1247 if (a->start > b->start)
1248 return 1;
1249 return 0;
1250 }
1251
1252 static int count_memberships(struct dl *dl, struct intel_super *super)
1253 {
1254 int memberships = 0;
1255 int i;
1256
1257 for (i = 0; i < super->anchor->num_raid_devs; i++)
1258 if (get_disk_slot_in_dev(super, i, dl->index) >= 0)
1259 memberships++;
1260
1261 return memberships;
1262 }
1263
1264 static __u32 imsm_min_reserved_sectors(struct intel_super *super);
1265
1266 static int split_ull(unsigned long long n, void *lo, void *hi)
1267 {
1268 if (lo == 0 || hi == 0)
1269 return 1;
1270 __put_unaligned32(__cpu_to_le32((__u32)n), lo);
1271 __put_unaligned32(__cpu_to_le32((n >> 32)), hi);
1272 return 0;
1273 }
1274
1275 static unsigned long long join_u32(__u32 lo, __u32 hi)
1276 {
1277 return (unsigned long long)__le32_to_cpu(lo) |
1278 (((unsigned long long)__le32_to_cpu(hi)) << 32);
1279 }
1280
1281 static unsigned long long total_blocks(struct imsm_disk *disk)
1282 {
1283 if (disk == NULL)
1284 return 0;
1285 return join_u32(disk->total_blocks_lo, disk->total_blocks_hi);
1286 }
1287
1288 /**
1289 * imsm_num_data_members() - get data drives count for an array.
1290 * @map: Map to analyze.
1291 *
1292 * num_data_members value represents minimal count of drives for level.
1293 * The name of the property could be misleading for RAID5 with asymmetric layout
1294 * because some data required to be calculated from parity.
1295 * The property is extracted from level and num_members value.
1296 *
1297 * Return: num_data_members value on success, zero otherwise.
1298 */
1299 static __u8 imsm_num_data_members(struct imsm_map *map)
1300 {
1301 switch (get_imsm_raid_level(map)) {
1302 case 0:
1303 return map->num_members;
1304 case 1:
1305 case 10:
1306 return map->num_members / 2;
1307 case 5:
1308 return map->num_members - 1;
1309 default:
1310 dprintf("unsupported raid level\n");
1311 return 0;
1312 }
1313 }
1314
1315 static unsigned long long pba_of_lba0(struct imsm_map *map)
1316 {
1317 if (map == NULL)
1318 return 0;
1319 return join_u32(map->pba_of_lba0_lo, map->pba_of_lba0_hi);
1320 }
1321
1322 static unsigned long long blocks_per_member(struct imsm_map *map)
1323 {
1324 if (map == NULL)
1325 return 0;
1326 return join_u32(map->blocks_per_member_lo, map->blocks_per_member_hi);
1327 }
1328
1329 static unsigned long long num_data_stripes(struct imsm_map *map)
1330 {
1331 if (map == NULL)
1332 return 0;
1333 return join_u32(map->num_data_stripes_lo, map->num_data_stripes_hi);
1334 }
1335
1336 static unsigned long long vol_curr_migr_unit(struct imsm_dev *dev)
1337 {
1338 if (dev == NULL)
1339 return 0;
1340
1341 return join_u32(dev->vol.curr_migr_unit_lo, dev->vol.curr_migr_unit_hi);
1342 }
1343
1344 static unsigned long long imsm_dev_size(struct imsm_dev *dev)
1345 {
1346 if (dev == NULL)
1347 return 0;
1348 return join_u32(dev->size_low, dev->size_high);
1349 }
1350
1351 static unsigned long long migr_chkp_area_pba(struct migr_record *migr_rec)
1352 {
1353 if (migr_rec == NULL)
1354 return 0;
1355 return join_u32(migr_rec->ckpt_area_pba_lo,
1356 migr_rec->ckpt_area_pba_hi);
1357 }
1358
1359 static unsigned long long current_migr_unit(struct migr_record *migr_rec)
1360 {
1361 if (migr_rec == NULL)
1362 return 0;
1363 return join_u32(migr_rec->curr_migr_unit_lo,
1364 migr_rec->curr_migr_unit_hi);
1365 }
1366
1367 static unsigned long long migr_dest_1st_member_lba(struct migr_record *migr_rec)
1368 {
1369 if (migr_rec == NULL)
1370 return 0;
1371 return join_u32(migr_rec->dest_1st_member_lba_lo,
1372 migr_rec->dest_1st_member_lba_hi);
1373 }
1374
1375 static unsigned long long get_num_migr_units(struct migr_record *migr_rec)
1376 {
1377 if (migr_rec == NULL)
1378 return 0;
1379 return join_u32(migr_rec->num_migr_units_lo,
1380 migr_rec->num_migr_units_hi);
1381 }
1382
1383 static void set_total_blocks(struct imsm_disk *disk, unsigned long long n)
1384 {
1385 split_ull(n, &disk->total_blocks_lo, &disk->total_blocks_hi);
1386 }
1387
1388 /**
1389 * set_num_domains() - Set number of domains for an array.
1390 * @map: Map to be updated.
1391 *
1392 * num_domains property represents copies count of each data drive, thus make
1393 * it meaningful only for RAID1 and RAID10. IMSM supports two domains for
1394 * raid1 and raid10.
1395 */
1396 static void set_num_domains(struct imsm_map *map)
1397 {
1398 int level = get_imsm_raid_level(map);
1399
1400 if (level == 1 || level == 10)
1401 map->num_domains = 2;
1402 else
1403 map->num_domains = 1;
1404 }
1405
1406 static void set_pba_of_lba0(struct imsm_map *map, unsigned long long n)
1407 {
1408 split_ull(n, &map->pba_of_lba0_lo, &map->pba_of_lba0_hi);
1409 }
1410
1411 static void set_blocks_per_member(struct imsm_map *map, unsigned long long n)
1412 {
1413 split_ull(n, &map->blocks_per_member_lo, &map->blocks_per_member_hi);
1414 }
1415
1416 static void set_num_data_stripes(struct imsm_map *map, unsigned long long n)
1417 {
1418 split_ull(n, &map->num_data_stripes_lo, &map->num_data_stripes_hi);
1419 }
1420
1421 /**
1422 * update_num_data_stripes() - Calculate and update num_data_stripes value.
1423 * @map: map to be updated.
1424 * @dev_size: size of volume.
1425 *
1426 * num_data_stripes value is addictionally divided by num_domains, therefore for
1427 * levels where num_domains is not 1, nds is a part of real value.
1428 */
1429 static void update_num_data_stripes(struct imsm_map *map,
1430 unsigned long long dev_size)
1431 {
1432 unsigned long long nds = dev_size / imsm_num_data_members(map);
1433
1434 nds /= map->num_domains;
1435 nds /= map->blocks_per_strip;
1436 set_num_data_stripes(map, nds);
1437 }
1438
1439 static void set_vol_curr_migr_unit(struct imsm_dev *dev, unsigned long long n)
1440 {
1441 if (dev == NULL)
1442 return;
1443
1444 split_ull(n, &dev->vol.curr_migr_unit_lo, &dev->vol.curr_migr_unit_hi);
1445 }
1446
1447 static void set_imsm_dev_size(struct imsm_dev *dev, unsigned long long n)
1448 {
1449 split_ull(n, &dev->size_low, &dev->size_high);
1450 }
1451
1452 static void set_migr_chkp_area_pba(struct migr_record *migr_rec,
1453 unsigned long long n)
1454 {
1455 split_ull(n, &migr_rec->ckpt_area_pba_lo, &migr_rec->ckpt_area_pba_hi);
1456 }
1457
1458 static void set_current_migr_unit(struct migr_record *migr_rec,
1459 unsigned long long n)
1460 {
1461 split_ull(n, &migr_rec->curr_migr_unit_lo,
1462 &migr_rec->curr_migr_unit_hi);
1463 }
1464
1465 static void set_migr_dest_1st_member_lba(struct migr_record *migr_rec,
1466 unsigned long long n)
1467 {
1468 split_ull(n, &migr_rec->dest_1st_member_lba_lo,
1469 &migr_rec->dest_1st_member_lba_hi);
1470 }
1471
1472 static void set_num_migr_units(struct migr_record *migr_rec,
1473 unsigned long long n)
1474 {
1475 split_ull(n, &migr_rec->num_migr_units_lo,
1476 &migr_rec->num_migr_units_hi);
1477 }
1478
1479 static unsigned long long per_dev_array_size(struct imsm_map *map)
1480 {
1481 unsigned long long array_size = 0;
1482
1483 if (map == NULL)
1484 return array_size;
1485
1486 array_size = num_data_stripes(map) * map->blocks_per_strip;
1487 if (get_imsm_raid_level(map) == 1 || get_imsm_raid_level(map) == 10)
1488 array_size *= 2;
1489
1490 return array_size;
1491 }
1492
1493 static struct extent *get_extents(struct intel_super *super, struct dl *dl,
1494 int get_minimal_reservation)
1495 {
1496 /* find a list of used extents on the given physical device */
1497 struct extent *rv, *e;
1498 int i;
1499 int memberships = count_memberships(dl, super);
1500 __u32 reservation;
1501
1502 /* trim the reserved area for spares, so they can join any array
1503 * regardless of whether the OROM has assigned sectors from the
1504 * IMSM_RESERVED_SECTORS region
1505 */
1506 if (dl->index == -1 || get_minimal_reservation)
1507 reservation = imsm_min_reserved_sectors(super);
1508 else
1509 reservation = MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS;
1510
1511 rv = xcalloc(sizeof(struct extent), (memberships + 1));
1512 e = rv;
1513
1514 for (i = 0; i < super->anchor->num_raid_devs; i++) {
1515 struct imsm_dev *dev = get_imsm_dev(super, i);
1516 struct imsm_map *map = get_imsm_map(dev, MAP_0);
1517
1518 if (get_imsm_disk_slot(map, dl->index) >= 0) {
1519 e->start = pba_of_lba0(map);
1520 e->size = per_dev_array_size(map);
1521 e++;
1522 }
1523 }
1524 qsort(rv, memberships, sizeof(*rv), cmp_extent);
1525
1526 /* determine the start of the metadata
1527 * when no raid devices are defined use the default
1528 * ...otherwise allow the metadata to truncate the value
1529 * as is the case with older versions of imsm
1530 */
1531 if (memberships) {
1532 struct extent *last = &rv[memberships - 1];
1533 unsigned long long remainder;
1534
1535 remainder = total_blocks(&dl->disk) - (last->start + last->size);
1536 /* round down to 1k block to satisfy precision of the kernel
1537 * 'size' interface
1538 */
1539 remainder &= ~1UL;
1540 /* make sure remainder is still sane */
1541 if (remainder < (unsigned)ROUND_UP(super->len, 512) >> 9)
1542 remainder = ROUND_UP(super->len, 512) >> 9;
1543 if (reservation > remainder)
1544 reservation = remainder;
1545 }
1546 e->start = total_blocks(&dl->disk) - reservation;
1547 e->size = 0;
1548 return rv;
1549 }
1550
1551 /* try to determine how much space is reserved for metadata from
1552 * the last get_extents() entry, otherwise fallback to the
1553 * default
1554 */
1555 static __u32 imsm_reserved_sectors(struct intel_super *super, struct dl *dl)
1556 {
1557 struct extent *e;
1558 int i;
1559 __u32 rv;
1560
1561 /* for spares just return a minimal reservation which will grow
1562 * once the spare is picked up by an array
1563 */
1564 if (dl->index == -1)
1565 return MPB_SECTOR_CNT;
1566
1567 e = get_extents(super, dl, 0);
1568 if (!e)
1569 return MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS;
1570
1571 /* scroll to last entry */
1572 for (i = 0; e[i].size; i++)
1573 continue;
1574
1575 rv = total_blocks(&dl->disk) - e[i].start;
1576
1577 free(e);
1578
1579 return rv;
1580 }
1581
1582 static int is_spare(struct imsm_disk *disk)
1583 {
1584 return (disk->status & SPARE_DISK) == SPARE_DISK;
1585 }
1586
1587 static int is_configured(struct imsm_disk *disk)
1588 {
1589 return (disk->status & CONFIGURED_DISK) == CONFIGURED_DISK;
1590 }
1591
1592 static int is_failed(struct imsm_disk *disk)
1593 {
1594 return (disk->status & FAILED_DISK) == FAILED_DISK;
1595 }
1596
1597 static int is_journal(struct imsm_disk *disk)
1598 {
1599 return (disk->status & JOURNAL_DISK) == JOURNAL_DISK;
1600 }
1601
1602 /* round array size down to closest MB and ensure it splits evenly
1603 * between members
1604 */
1605 static unsigned long long round_size_to_mb(unsigned long long size, unsigned int
1606 disk_count)
1607 {
1608 size /= disk_count;
1609 size = (size >> SECT_PER_MB_SHIFT) << SECT_PER_MB_SHIFT;
1610 size *= disk_count;
1611
1612 return size;
1613 }
1614
1615 static int able_to_resync(int raid_level, int missing_disks)
1616 {
1617 int max_missing_disks = 0;
1618
1619 switch (raid_level) {
1620 case 10:
1621 max_missing_disks = 1;
1622 break;
1623 default:
1624 max_missing_disks = 0;
1625 }
1626 return missing_disks <= max_missing_disks;
1627 }
1628
1629 /* try to determine how much space is reserved for metadata from
1630 * the last get_extents() entry on the smallest active disk,
1631 * otherwise fallback to the default
1632 */
1633 static __u32 imsm_min_reserved_sectors(struct intel_super *super)
1634 {
1635 struct extent *e;
1636 int i;
1637 unsigned long long min_active;
1638 __u32 remainder;
1639 __u32 rv = MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS;
1640 struct dl *dl, *dl_min = NULL;
1641
1642 if (!super)
1643 return rv;
1644
1645 min_active = 0;
1646 for (dl = super->disks; dl; dl = dl->next) {
1647 if (dl->index < 0)
1648 continue;
1649 unsigned long long blocks = total_blocks(&dl->disk);
1650 if (blocks < min_active || min_active == 0) {
1651 dl_min = dl;
1652 min_active = blocks;
1653 }
1654 }
1655 if (!dl_min)
1656 return rv;
1657
1658 /* find last lba used by subarrays on the smallest active disk */
1659 e = get_extents(super, dl_min, 0);
1660 if (!e)
1661 return rv;
1662 for (i = 0; e[i].size; i++)
1663 continue;
1664
1665 remainder = min_active - e[i].start;
1666 free(e);
1667
1668 /* to give priority to recovery we should not require full
1669 IMSM_RESERVED_SECTORS from the spare */
1670 rv = MPB_SECTOR_CNT + NUM_BLOCKS_DIRTY_STRIPE_REGION;
1671
1672 /* if real reservation is smaller use that value */
1673 return (remainder < rv) ? remainder : rv;
1674 }
1675
1676 /*
1677 * Return minimum size of a spare and sector size
1678 * that can be used in this array
1679 */
1680 int get_spare_criteria_imsm(struct supertype *st, struct spare_criteria *c)
1681 {
1682 struct intel_super *super = st->sb;
1683 struct dl *dl;
1684 struct extent *e;
1685 int i;
1686 unsigned long long size = 0;
1687
1688 c->min_size = 0;
1689 c->sector_size = 0;
1690
1691 if (!super)
1692 return -EINVAL;
1693 /* find first active disk in array */
1694 dl = super->disks;
1695 while (dl && (is_failed(&dl->disk) || dl->index == -1))
1696 dl = dl->next;
1697 if (!dl)
1698 return -EINVAL;
1699 /* find last lba used by subarrays */
1700 e = get_extents(super, dl, 0);
1701 if (!e)
1702 return -EINVAL;
1703 for (i = 0; e[i].size; i++)
1704 continue;
1705 if (i > 0)
1706 size = e[i-1].start + e[i-1].size;
1707 free(e);
1708
1709 /* add the amount of space needed for metadata */
1710 size += imsm_min_reserved_sectors(super);
1711
1712 c->min_size = size * 512;
1713 c->sector_size = super->sector_size;
1714
1715 return 0;
1716 }
1717
1718 static bool is_gen_migration(struct imsm_dev *dev);
1719
1720 #define IMSM_4K_DIV 8
1721
1722 static __u64 blocks_per_migr_unit(struct intel_super *super,
1723 struct imsm_dev *dev);
1724
1725 static void print_imsm_dev(struct intel_super *super,
1726 struct imsm_dev *dev,
1727 char *uuid,
1728 int disk_idx)
1729 {
1730 __u64 sz;
1731 int slot, i;
1732 struct imsm_map *map = get_imsm_map(dev, MAP_0);
1733 struct imsm_map *map2 = get_imsm_map(dev, MAP_1);
1734 __u32 ord;
1735
1736 printf("\n");
1737 printf("[%.16s]:\n", dev->volume);
1738 printf(" Subarray : %d\n", super->current_vol);
1739 printf(" UUID : %s\n", uuid);
1740 printf(" RAID Level : %d", get_imsm_raid_level(map));
1741 if (map2)
1742 printf(" <-- %d", get_imsm_raid_level(map2));
1743 printf("\n");
1744 printf(" Members : %d", map->num_members);
1745 if (map2)
1746 printf(" <-- %d", map2->num_members);
1747 printf("\n");
1748 printf(" Slots : [");
1749 for (i = 0; i < map->num_members; i++) {
1750 ord = get_imsm_ord_tbl_ent(dev, i, MAP_0);
1751 printf("%s", ord & IMSM_ORD_REBUILD ? "_" : "U");
1752 }
1753 printf("]");
1754 if (map2) {
1755 printf(" <-- [");
1756 for (i = 0; i < map2->num_members; i++) {
1757 ord = get_imsm_ord_tbl_ent(dev, i, MAP_1);
1758 printf("%s", ord & IMSM_ORD_REBUILD ? "_" : "U");
1759 }
1760 printf("]");
1761 }
1762 printf("\n");
1763 printf(" Failed disk : ");
1764 if (map->failed_disk_num == 0xff)
1765 printf("none");
1766 else
1767 printf("%i", map->failed_disk_num);
1768 printf("\n");
1769 slot = get_imsm_disk_slot(map, disk_idx);
1770 if (slot >= 0) {
1771 ord = get_imsm_ord_tbl_ent(dev, slot, MAP_X);
1772 printf(" This Slot : %d%s\n", slot,
1773 ord & IMSM_ORD_REBUILD ? " (out-of-sync)" : "");
1774 } else
1775 printf(" This Slot : ?\n");
1776 printf(" Sector Size : %u\n", super->sector_size);
1777 sz = imsm_dev_size(dev);
1778 printf(" Array Size : %llu%s\n",
1779 (unsigned long long)sz * 512 / super->sector_size,
1780 human_size(sz * 512));
1781 sz = blocks_per_member(map);
1782 printf(" Per Dev Size : %llu%s\n",
1783 (unsigned long long)sz * 512 / super->sector_size,
1784 human_size(sz * 512));
1785 printf(" Sector Offset : %llu\n",
1786 pba_of_lba0(map) * 512 / super->sector_size);
1787 printf(" Num Stripes : %llu\n",
1788 num_data_stripes(map));
1789 printf(" Chunk Size : %u KiB",
1790 __le16_to_cpu(map->blocks_per_strip) / 2);
1791 if (map2)
1792 printf(" <-- %u KiB",
1793 __le16_to_cpu(map2->blocks_per_strip) / 2);
1794 printf("\n");
1795 printf(" Reserved : %d\n", __le32_to_cpu(dev->reserved_blocks));
1796 printf(" Migrate State : ");
1797 if (dev->vol.migr_state) {
1798 if (migr_type(dev) == MIGR_INIT)
1799 printf("initialize\n");
1800 else if (migr_type(dev) == MIGR_REBUILD)
1801 printf("rebuild\n");
1802 else if (migr_type(dev) == MIGR_VERIFY)
1803 printf("check\n");
1804 else if (migr_type(dev) == MIGR_GEN_MIGR)
1805 printf("general migration\n");
1806 else if (migr_type(dev) == MIGR_STATE_CHANGE)
1807 printf("state change\n");
1808 else if (migr_type(dev) == MIGR_REPAIR)
1809 printf("repair\n");
1810 else
1811 printf("<unknown:%d>\n", migr_type(dev));
1812 } else
1813 printf("idle\n");
1814 printf(" Map State : %s", map_state_str[map->map_state]);
1815 if (dev->vol.migr_state) {
1816 struct imsm_map *map = get_imsm_map(dev, MAP_1);
1817
1818 printf(" <-- %s", map_state_str[map->map_state]);
1819 printf("\n Checkpoint : %llu ", vol_curr_migr_unit(dev));
1820 if (is_gen_migration(dev) && (slot > 1 || slot < 0))
1821 printf("(N/A)");
1822 else
1823 printf("(%llu)", (unsigned long long)
1824 blocks_per_migr_unit(super, dev));
1825 }
1826 printf("\n");
1827 printf(" Dirty State : %s\n", (dev->vol.dirty & RAIDVOL_DIRTY) ?
1828 "dirty" : "clean");
1829 printf(" RWH Policy : ");
1830 if (dev->rwh_policy == RWH_OFF || dev->rwh_policy == RWH_MULTIPLE_OFF)
1831 printf("off\n");
1832 else if (dev->rwh_policy == RWH_DISTRIBUTED)
1833 printf("PPL distributed\n");
1834 else if (dev->rwh_policy == RWH_JOURNALING_DRIVE)
1835 printf("PPL journaling drive\n");
1836 else if (dev->rwh_policy == RWH_MULTIPLE_DISTRIBUTED)
1837 printf("Multiple distributed PPLs\n");
1838 else if (dev->rwh_policy == RWH_MULTIPLE_PPLS_JOURNALING_DRIVE)
1839 printf("Multiple PPLs on journaling drive\n");
1840 else if (dev->rwh_policy == RWH_BITMAP)
1841 printf("Write-intent bitmap\n");
1842 else
1843 printf("<unknown:%d>\n", dev->rwh_policy);
1844
1845 printf(" Volume ID : %u\n", dev->my_vol_raid_dev_num);
1846 }
1847
1848 static void print_imsm_disk(struct imsm_disk *disk,
1849 int index,
1850 __u32 reserved,
1851 unsigned int sector_size) {
1852 char str[MAX_RAID_SERIAL_LEN + 1];
1853 __u64 sz;
1854
1855 if (index < -1 || !disk)
1856 return;
1857
1858 printf("\n");
1859 snprintf(str, MAX_RAID_SERIAL_LEN + 1, "%s", disk->serial);
1860 if (index >= 0)
1861 printf(" Disk%02d Serial : %s\n", index, str);
1862 else
1863 printf(" Disk Serial : %s\n", str);
1864 printf(" State :%s%s%s%s\n", is_spare(disk) ? " spare" : "",
1865 is_configured(disk) ? " active" : "",
1866 is_failed(disk) ? " failed" : "",
1867 is_journal(disk) ? " journal" : "");
1868 printf(" Id : %08x\n", __le32_to_cpu(disk->scsi_id));
1869 sz = total_blocks(disk) - reserved;
1870 printf(" Usable Size : %llu%s\n",
1871 (unsigned long long)sz * 512 / sector_size,
1872 human_size(sz * 512));
1873 }
1874
1875 void convert_to_4k_imsm_migr_rec(struct intel_super *super)
1876 {
1877 struct migr_record *migr_rec = super->migr_rec;
1878
1879 migr_rec->blocks_per_unit /= IMSM_4K_DIV;
1880 migr_rec->dest_depth_per_unit /= IMSM_4K_DIV;
1881 split_ull((join_u32(migr_rec->post_migr_vol_cap,
1882 migr_rec->post_migr_vol_cap_hi) / IMSM_4K_DIV),
1883 &migr_rec->post_migr_vol_cap, &migr_rec->post_migr_vol_cap_hi);
1884 set_migr_chkp_area_pba(migr_rec,
1885 migr_chkp_area_pba(migr_rec) / IMSM_4K_DIV);
1886 set_migr_dest_1st_member_lba(migr_rec,
1887 migr_dest_1st_member_lba(migr_rec) / IMSM_4K_DIV);
1888 }
1889
1890 void convert_to_4k_imsm_disk(struct imsm_disk *disk)
1891 {
1892 set_total_blocks(disk, (total_blocks(disk)/IMSM_4K_DIV));
1893 }
1894
1895 void convert_to_4k(struct intel_super *super)
1896 {
1897 struct imsm_super *mpb = super->anchor;
1898 struct imsm_disk *disk;
1899 int i;
1900 __u32 bbm_log_size = __le32_to_cpu(mpb->bbm_log_size);
1901
1902 for (i = 0; i < mpb->num_disks ; i++) {
1903 disk = __get_imsm_disk(mpb, i);
1904 /* disk */
1905 convert_to_4k_imsm_disk(disk);
1906 }
1907 for (i = 0; i < mpb->num_raid_devs; i++) {
1908 struct imsm_dev *dev = __get_imsm_dev(mpb, i);
1909 struct imsm_map *map = get_imsm_map(dev, MAP_0);
1910 /* dev */
1911 set_imsm_dev_size(dev, imsm_dev_size(dev)/IMSM_4K_DIV);
1912 set_vol_curr_migr_unit(dev,
1913 vol_curr_migr_unit(dev) / IMSM_4K_DIV);
1914
1915 /* map0 */
1916 set_blocks_per_member(map, blocks_per_member(map)/IMSM_4K_DIV);
1917 map->blocks_per_strip /= IMSM_4K_DIV;
1918 set_pba_of_lba0(map, pba_of_lba0(map)/IMSM_4K_DIV);
1919
1920 if (dev->vol.migr_state) {
1921 /* map1 */
1922 map = get_imsm_map(dev, MAP_1);
1923 set_blocks_per_member(map,
1924 blocks_per_member(map)/IMSM_4K_DIV);
1925 map->blocks_per_strip /= IMSM_4K_DIV;
1926 set_pba_of_lba0(map, pba_of_lba0(map)/IMSM_4K_DIV);
1927 }
1928 }
1929 if (bbm_log_size) {
1930 struct bbm_log *log = (void *)mpb +
1931 __le32_to_cpu(mpb->mpb_size) - bbm_log_size;
1932 __u32 i;
1933
1934 for (i = 0; i < log->entry_count; i++) {
1935 struct bbm_log_entry *entry =
1936 &log->marked_block_entries[i];
1937
1938 __u8 count = entry->marked_count + 1;
1939 unsigned long long sector =
1940 __le48_to_cpu(&entry->defective_block_start);
1941
1942 entry->defective_block_start =
1943 __cpu_to_le48(sector/IMSM_4K_DIV);
1944 entry->marked_count = max(count/IMSM_4K_DIV, 1) - 1;
1945 }
1946 }
1947
1948 mpb->check_sum = __gen_imsm_checksum(mpb);
1949 }
1950
1951 void examine_migr_rec_imsm(struct intel_super *super)
1952 {
1953 struct migr_record *migr_rec = super->migr_rec;
1954 struct imsm_super *mpb = super->anchor;
1955 int i;
1956
1957 for (i = 0; i < mpb->num_raid_devs; i++) {
1958 struct imsm_dev *dev = __get_imsm_dev(mpb, i);
1959 struct imsm_map *map;
1960 int slot = -1;
1961
1962 if (is_gen_migration(dev) == false)
1963 continue;
1964
1965 printf("\nMigration Record Information:");
1966
1967 /* first map under migration */
1968 map = get_imsm_map(dev, MAP_0);
1969
1970 if (map)
1971 slot = get_imsm_disk_slot(map, super->disks->index);
1972 if (map == NULL || slot > 1 || slot < 0) {
1973 printf(" Empty\n ");
1974 printf("Examine one of first two disks in array\n");
1975 break;
1976 }
1977 printf("\n Status : ");
1978 if (__le32_to_cpu(migr_rec->rec_status) == UNIT_SRC_NORMAL)
1979 printf("Normal\n");
1980 else
1981 printf("Contains Data\n");
1982 printf(" Current Unit : %llu\n",
1983 current_migr_unit(migr_rec));
1984 printf(" Family : %u\n",
1985 __le32_to_cpu(migr_rec->family_num));
1986 printf(" Ascending : %u\n",
1987 __le32_to_cpu(migr_rec->ascending_migr));
1988 printf(" Blocks Per Unit : %u\n",
1989 __le32_to_cpu(migr_rec->blocks_per_unit));
1990 printf(" Dest. Depth Per Unit : %u\n",
1991 __le32_to_cpu(migr_rec->dest_depth_per_unit));
1992 printf(" Checkpoint Area pba : %llu\n",
1993 migr_chkp_area_pba(migr_rec));
1994 printf(" First member lba : %llu\n",
1995 migr_dest_1st_member_lba(migr_rec));
1996 printf(" Total Number of Units : %llu\n",
1997 get_num_migr_units(migr_rec));
1998 printf(" Size of volume : %llu\n",
1999 join_u32(migr_rec->post_migr_vol_cap,
2000 migr_rec->post_migr_vol_cap_hi));
2001 printf(" Record was read from : %u\n",
2002 __le32_to_cpu(migr_rec->ckpt_read_disk_num));
2003
2004 break;
2005 }
2006 }
2007
2008 void convert_from_4k_imsm_migr_rec(struct intel_super *super)
2009 {
2010 struct migr_record *migr_rec = super->migr_rec;
2011
2012 migr_rec->blocks_per_unit *= IMSM_4K_DIV;
2013 migr_rec->dest_depth_per_unit *= IMSM_4K_DIV;
2014 split_ull((join_u32(migr_rec->post_migr_vol_cap,
2015 migr_rec->post_migr_vol_cap_hi) * IMSM_4K_DIV),
2016 &migr_rec->post_migr_vol_cap,
2017 &migr_rec->post_migr_vol_cap_hi);
2018 set_migr_chkp_area_pba(migr_rec,
2019 migr_chkp_area_pba(migr_rec) * IMSM_4K_DIV);
2020 set_migr_dest_1st_member_lba(migr_rec,
2021 migr_dest_1st_member_lba(migr_rec) * IMSM_4K_DIV);
2022 }
2023
2024 void convert_from_4k(struct intel_super *super)
2025 {
2026 struct imsm_super *mpb = super->anchor;
2027 struct imsm_disk *disk;
2028 int i;
2029 __u32 bbm_log_size = __le32_to_cpu(mpb->bbm_log_size);
2030
2031 for (i = 0; i < mpb->num_disks ; i++) {
2032 disk = __get_imsm_disk(mpb, i);
2033 /* disk */
2034 set_total_blocks(disk, (total_blocks(disk)*IMSM_4K_DIV));
2035 }
2036
2037 for (i = 0; i < mpb->num_raid_devs; i++) {
2038 struct imsm_dev *dev = __get_imsm_dev(mpb, i);
2039 struct imsm_map *map = get_imsm_map(dev, MAP_0);
2040 /* dev */
2041 set_imsm_dev_size(dev, imsm_dev_size(dev)*IMSM_4K_DIV);
2042 set_vol_curr_migr_unit(dev,
2043 vol_curr_migr_unit(dev) * IMSM_4K_DIV);
2044
2045 /* map0 */
2046 set_blocks_per_member(map, blocks_per_member(map)*IMSM_4K_DIV);
2047 map->blocks_per_strip *= IMSM_4K_DIV;
2048 set_pba_of_lba0(map, pba_of_lba0(map)*IMSM_4K_DIV);
2049
2050 if (dev->vol.migr_state) {
2051 /* map1 */
2052 map = get_imsm_map(dev, MAP_1);
2053 set_blocks_per_member(map,
2054 blocks_per_member(map)*IMSM_4K_DIV);
2055 map->blocks_per_strip *= IMSM_4K_DIV;
2056 set_pba_of_lba0(map, pba_of_lba0(map)*IMSM_4K_DIV);
2057 }
2058 }
2059 if (bbm_log_size) {
2060 struct bbm_log *log = (void *)mpb +
2061 __le32_to_cpu(mpb->mpb_size) - bbm_log_size;
2062 __u32 i;
2063
2064 for (i = 0; i < log->entry_count; i++) {
2065 struct bbm_log_entry *entry =
2066 &log->marked_block_entries[i];
2067
2068 __u8 count = entry->marked_count + 1;
2069 unsigned long long sector =
2070 __le48_to_cpu(&entry->defective_block_start);
2071
2072 entry->defective_block_start =
2073 __cpu_to_le48(sector*IMSM_4K_DIV);
2074 entry->marked_count = count*IMSM_4K_DIV - 1;
2075 }
2076 }
2077
2078 mpb->check_sum = __gen_imsm_checksum(mpb);
2079 }
2080
2081 /*******************************************************************************
2082 * function: imsm_check_attributes
2083 * Description: Function checks if features represented by attributes flags
2084 * are supported by mdadm.
2085 * Parameters:
2086 * attributes - Attributes read from metadata
2087 * Returns:
2088 * 0 - passed attributes contains unsupported features flags
2089 * 1 - all features are supported
2090 ******************************************************************************/
2091 static int imsm_check_attributes(__u32 attributes)
2092 {
2093 int ret_val = 1;
2094 __u32 not_supported = MPB_ATTRIB_SUPPORTED^0xffffffff;
2095
2096 not_supported &= ~MPB_ATTRIB_IGNORED;
2097
2098 not_supported &= attributes;
2099 if (not_supported) {
2100 pr_err("(IMSM): Unsupported attributes : %x\n",
2101 (unsigned)__le32_to_cpu(not_supported));
2102 if (not_supported & MPB_ATTRIB_CHECKSUM_VERIFY) {
2103 dprintf("\t\tMPB_ATTRIB_CHECKSUM_VERIFY \n");
2104 not_supported ^= MPB_ATTRIB_CHECKSUM_VERIFY;
2105 }
2106 if (not_supported & MPB_ATTRIB_2TB) {
2107 dprintf("\t\tMPB_ATTRIB_2TB\n");
2108 not_supported ^= MPB_ATTRIB_2TB;
2109 }
2110 if (not_supported & MPB_ATTRIB_RAID0) {
2111 dprintf("\t\tMPB_ATTRIB_RAID0\n");
2112 not_supported ^= MPB_ATTRIB_RAID0;
2113 }
2114 if (not_supported & MPB_ATTRIB_RAID1) {
2115 dprintf("\t\tMPB_ATTRIB_RAID1\n");
2116 not_supported ^= MPB_ATTRIB_RAID1;
2117 }
2118 if (not_supported & MPB_ATTRIB_RAID10) {
2119 dprintf("\t\tMPB_ATTRIB_RAID10\n");
2120 not_supported ^= MPB_ATTRIB_RAID10;
2121 }
2122 if (not_supported & MPB_ATTRIB_RAID1E) {
2123 dprintf("\t\tMPB_ATTRIB_RAID1E\n");
2124 not_supported ^= MPB_ATTRIB_RAID1E;
2125 }
2126 if (not_supported & MPB_ATTRIB_RAID5) {
2127 dprintf("\t\tMPB_ATTRIB_RAID5\n");
2128 not_supported ^= MPB_ATTRIB_RAID5;
2129 }
2130 if (not_supported & MPB_ATTRIB_RAIDCNG) {
2131 dprintf("\t\tMPB_ATTRIB_RAIDCNG\n");
2132 not_supported ^= MPB_ATTRIB_RAIDCNG;
2133 }
2134 if (not_supported & MPB_ATTRIB_BBM) {
2135 dprintf("\t\tMPB_ATTRIB_BBM\n");
2136 not_supported ^= MPB_ATTRIB_BBM;
2137 }
2138 if (not_supported & MPB_ATTRIB_CHECKSUM_VERIFY) {
2139 dprintf("\t\tMPB_ATTRIB_CHECKSUM_VERIFY (== MPB_ATTRIB_LEGACY)\n");
2140 not_supported ^= MPB_ATTRIB_CHECKSUM_VERIFY;
2141 }
2142 if (not_supported & MPB_ATTRIB_EXP_STRIPE_SIZE) {
2143 dprintf("\t\tMPB_ATTRIB_EXP_STRIP_SIZE\n");
2144 not_supported ^= MPB_ATTRIB_EXP_STRIPE_SIZE;
2145 }
2146 if (not_supported & MPB_ATTRIB_2TB_DISK) {
2147 dprintf("\t\tMPB_ATTRIB_2TB_DISK\n");
2148 not_supported ^= MPB_ATTRIB_2TB_DISK;
2149 }
2150 if (not_supported & MPB_ATTRIB_NEVER_USE2) {
2151 dprintf("\t\tMPB_ATTRIB_NEVER_USE2\n");
2152 not_supported ^= MPB_ATTRIB_NEVER_USE2;
2153 }
2154 if (not_supported & MPB_ATTRIB_NEVER_USE) {
2155 dprintf("\t\tMPB_ATTRIB_NEVER_USE\n");
2156 not_supported ^= MPB_ATTRIB_NEVER_USE;
2157 }
2158
2159 if (not_supported)
2160 dprintf("(IMSM): Unknown attributes : %x\n", not_supported);
2161
2162 ret_val = 0;
2163 }
2164
2165 return ret_val;
2166 }
2167
2168 static void getinfo_super_imsm(struct supertype *st, struct mdinfo *info, char *map);
2169
2170 static void examine_super_imsm(struct supertype *st, char *homehost)
2171 {
2172 struct intel_super *super = st->sb;
2173 struct imsm_super *mpb = super->anchor;
2174 char str[MAX_SIGNATURE_LENGTH];
2175 int i;
2176 struct mdinfo info;
2177 char nbuf[64];
2178 __u32 sum;
2179 __u32 reserved = imsm_reserved_sectors(super, super->disks);
2180 struct dl *dl;
2181 time_t creation_time;
2182
2183 strncpy(str, (char *)mpb->sig, MPB_SIG_LEN);
2184 str[MPB_SIG_LEN-1] = '\0';
2185 printf(" Magic : %s\n", str);
2186 printf(" Version : %s\n", get_imsm_version(mpb));
2187 printf(" Orig Family : %08x\n", __le32_to_cpu(mpb->orig_family_num));
2188 printf(" Family : %08x\n", __le32_to_cpu(mpb->family_num));
2189 printf(" Generation : %08x\n", __le32_to_cpu(mpb->generation_num));
2190 creation_time = __le64_to_cpu(mpb->creation_time);
2191 printf(" Creation Time : %.24s\n",
2192 creation_time ? ctime(&creation_time) : "Unknown");
2193 printf(" Attributes : ");
2194 if (imsm_check_attributes(mpb->attributes))
2195 printf("All supported\n");
2196 else
2197 printf("not supported\n");
2198 getinfo_super_imsm(st, &info, NULL);
2199 fname_from_uuid(st, &info, nbuf, ':');
2200 printf(" UUID : %s\n", nbuf + 5);
2201 sum = __le32_to_cpu(mpb->check_sum);
2202 printf(" Checksum : %08x %s\n", sum,
2203 __gen_imsm_checksum(mpb) == sum ? "correct" : "incorrect");
2204 printf(" MPB Sectors : %d\n", mpb_sectors(mpb, super->sector_size));
2205 printf(" Disks : %d\n", mpb->num_disks);
2206 printf(" RAID Devices : %d\n", mpb->num_raid_devs);
2207 print_imsm_disk(__get_imsm_disk(mpb, super->disks->index),
2208 super->disks->index, reserved, super->sector_size);
2209 if (get_imsm_bbm_log_size(super->bbm_log)) {
2210 struct bbm_log *log = super->bbm_log;
2211
2212 printf("\n");
2213 printf("Bad Block Management Log:\n");
2214 printf(" Log Size : %d\n", __le32_to_cpu(mpb->bbm_log_size));
2215 printf(" Signature : %x\n", __le32_to_cpu(log->signature));
2216 printf(" Entry Count : %d\n", __le32_to_cpu(log->entry_count));
2217 }
2218 for (i = 0; i < mpb->num_raid_devs; i++) {
2219 struct mdinfo info;
2220 struct imsm_dev *dev = __get_imsm_dev(mpb, i);
2221
2222 super->current_vol = i;
2223 getinfo_super_imsm(st, &info, NULL);
2224 fname_from_uuid(st, &info, nbuf, ':');
2225 print_imsm_dev(super, dev, nbuf + 5, super->disks->index);
2226 }
2227 for (i = 0; i < mpb->num_disks; i++) {
2228 if (i == super->disks->index)
2229 continue;
2230 print_imsm_disk(__get_imsm_disk(mpb, i), i, reserved,
2231 super->sector_size);
2232 }
2233
2234 for (dl = super->disks; dl; dl = dl->next)
2235 if (dl->index == -1)
2236 print_imsm_disk(&dl->disk, -1, reserved,
2237 super->sector_size);
2238
2239 examine_migr_rec_imsm(super);
2240 }
2241
2242 static void brief_examine_super_imsm(struct supertype *st, int verbose)
2243 {
2244 /* We just write a generic IMSM ARRAY entry */
2245 struct mdinfo info;
2246 char nbuf[64];
2247
2248 getinfo_super_imsm(st, &info, NULL);
2249 fname_from_uuid(st, &info, nbuf, ':');
2250 printf("ARRAY metadata=imsm UUID=%s\n", nbuf + 5);
2251 }
2252
2253 static void brief_examine_subarrays_imsm(struct supertype *st, int verbose)
2254 {
2255 /* We just write a generic IMSM ARRAY entry */
2256 struct mdinfo info;
2257 char nbuf[64];
2258 char nbuf1[64];
2259 struct intel_super *super = st->sb;
2260 int i;
2261
2262 if (!super->anchor->num_raid_devs)
2263 return;
2264
2265 getinfo_super_imsm(st, &info, NULL);
2266 fname_from_uuid(st, &info, nbuf, ':');
2267 for (i = 0; i < super->anchor->num_raid_devs; i++) {
2268 struct imsm_dev *dev = get_imsm_dev(super, i);
2269
2270 super->current_vol = i;
2271 getinfo_super_imsm(st, &info, NULL);
2272 fname_from_uuid(st, &info, nbuf1, ':');
2273 printf("ARRAY /dev/md/%.16s container=%s member=%d UUID=%s\n",
2274 dev->volume, nbuf + 5, i, nbuf1 + 5);
2275 }
2276 }
2277
2278 static void export_examine_super_imsm(struct supertype *st)
2279 {
2280 struct intel_super *super = st->sb;
2281 struct imsm_super *mpb = super->anchor;
2282 struct mdinfo info;
2283 char nbuf[64];
2284
2285 getinfo_super_imsm(st, &info, NULL);
2286 fname_from_uuid(st, &info, nbuf, ':');
2287 printf("MD_METADATA=imsm\n");
2288 printf("MD_LEVEL=container\n");
2289 printf("MD_UUID=%s\n", nbuf+5);
2290 printf("MD_DEVICES=%u\n", mpb->num_disks);
2291 printf("MD_CREATION_TIME=%llu\n", __le64_to_cpu(mpb->creation_time));
2292 }
2293
2294 static void detail_super_imsm(struct supertype *st, char *homehost,
2295 char *subarray)
2296 {
2297 struct mdinfo info;
2298 char nbuf[64];
2299 struct intel_super *super = st->sb;
2300 int temp_vol = super->current_vol;
2301
2302 if (subarray)
2303 super->current_vol = strtoul(subarray, NULL, 10);
2304
2305 getinfo_super_imsm(st, &info, NULL);
2306 fname_from_uuid(st, &info, nbuf, ':');
2307 printf("\n UUID : %s\n", nbuf + 5);
2308
2309 super->current_vol = temp_vol;
2310 }
2311
2312 static void brief_detail_super_imsm(struct supertype *st, char *subarray)
2313 {
2314 struct mdinfo info;
2315 char nbuf[64];
2316 struct intel_super *super = st->sb;
2317 int temp_vol = super->current_vol;
2318
2319 if (subarray)
2320 super->current_vol = strtoul(subarray, NULL, 10);
2321
2322 getinfo_super_imsm(st, &info, NULL);
2323 fname_from_uuid(st, &info, nbuf, ':');
2324 printf(" UUID=%s", nbuf + 5);
2325
2326 super->current_vol = temp_vol;
2327 }
2328
2329 static int imsm_read_serial(int fd, char *devname, __u8 *serial,
2330 size_t serial_buf_len);
2331 static void fd2devname(int fd, char *name);
2332
2333 static int ahci_enumerate_ports(const char *hba_path, int port_count, int host_base, int verbose)
2334 {
2335 /* dump an unsorted list of devices attached to AHCI Intel storage
2336 * controller, as well as non-connected ports
2337 */
2338 int hba_len = strlen(hba_path) + 1;
2339 struct dirent *ent;
2340 DIR *dir;
2341 char *path = NULL;
2342 int err = 0;
2343 unsigned long port_mask = (1 << port_count) - 1;
2344
2345 if (port_count > (int)sizeof(port_mask) * 8) {
2346 if (verbose > 0)
2347 pr_err("port_count %d out of range\n", port_count);
2348 return 2;
2349 }
2350
2351 /* scroll through /sys/dev/block looking for devices attached to
2352 * this hba
2353 */
2354 dir = opendir("/sys/dev/block");
2355 if (!dir)
2356 return 1;
2357
2358 for (ent = readdir(dir); ent; ent = readdir(dir)) {
2359 int fd;
2360 char model[64];
2361 char vendor[64];
2362 char buf[1024];
2363 int major, minor;
2364 char device[PATH_MAX];
2365 char *c;
2366 int port;
2367 int type;
2368
2369 if (sscanf(ent->d_name, "%d:%d", &major, &minor) != 2)
2370 continue;
2371 path = devt_to_devpath(makedev(major, minor), 1, NULL);
2372 if (!path)
2373 continue;
2374 if (!path_attached_to_hba(path, hba_path)) {
2375 free(path);
2376 path = NULL;
2377 continue;
2378 }
2379
2380 /* retrieve the scsi device */
2381 if (!devt_to_devpath(makedev(major, minor), 1, device)) {
2382 if (verbose > 0)
2383 pr_err("failed to get device\n");
2384 err = 2;
2385 break;
2386 }
2387 if (devpath_to_char(device, "type", buf, sizeof(buf), 0)) {
2388 err = 2;
2389 break;
2390 }
2391 type = strtoul(buf, NULL, 10);
2392
2393 /* if it's not a disk print the vendor and model */
2394 if (!(type == 0 || type == 7 || type == 14)) {
2395 vendor[0] = '\0';
2396 model[0] = '\0';
2397
2398 if (devpath_to_char(device, "vendor", buf,
2399 sizeof(buf), 0) == 0) {
2400 strncpy(vendor, buf, sizeof(vendor));
2401 vendor[sizeof(vendor) - 1] = '\0';
2402 c = (char *) &vendor[sizeof(vendor) - 1];
2403 while (isspace(*c) || *c == '\0')
2404 *c-- = '\0';
2405
2406 }
2407
2408 if (devpath_to_char(device, "model", buf,
2409 sizeof(buf), 0) == 0) {
2410 strncpy(model, buf, sizeof(model));
2411 model[sizeof(model) - 1] = '\0';
2412 c = (char *) &model[sizeof(model) - 1];
2413 while (isspace(*c) || *c == '\0')
2414 *c-- = '\0';
2415 }
2416
2417 if (vendor[0] && model[0])
2418 sprintf(buf, "%.64s %.64s", vendor, model);
2419 else
2420 switch (type) { /* numbers from hald/linux/device.c */
2421 case 1: sprintf(buf, "tape"); break;
2422 case 2: sprintf(buf, "printer"); break;
2423 case 3: sprintf(buf, "processor"); break;
2424 case 4:
2425 case 5: sprintf(buf, "cdrom"); break;
2426 case 6: sprintf(buf, "scanner"); break;
2427 case 8: sprintf(buf, "media_changer"); break;
2428 case 9: sprintf(buf, "comm"); break;
2429 case 12: sprintf(buf, "raid"); break;
2430 default: sprintf(buf, "unknown");
2431 }
2432 } else
2433 buf[0] = '\0';
2434
2435 /* chop device path to 'host%d' and calculate the port number */
2436 c = strchr(&path[hba_len], '/');
2437 if (!c) {
2438 if (verbose > 0)
2439 pr_err("%s - invalid path name\n", path + hba_len);
2440 err = 2;
2441 break;
2442 }
2443 *c = '\0';
2444 if ((sscanf(&path[hba_len], "ata%d", &port) == 1) ||
2445 ((sscanf(&path[hba_len], "host%d", &port) == 1)))
2446 port -= host_base;
2447 else {
2448 if (verbose > 0) {
2449 *c = '/'; /* repair the full string */
2450 pr_err("failed to determine port number for %s\n",
2451 path);
2452 }
2453 err = 2;
2454 break;
2455 }
2456
2457 /* mark this port as used */
2458 port_mask &= ~(1 << port);
2459
2460 /* print out the device information */
2461 if (buf[0]) {
2462 printf(" Port%d : - non-disk device (%s) -\n", port, buf);
2463 continue;
2464 }
2465
2466 fd = dev_open(ent->d_name, O_RDONLY);
2467 if (!is_fd_valid(fd))
2468 printf(" Port%d : - disk info unavailable -\n", port);
2469 else {
2470 fd2devname(fd, buf);
2471 printf(" Port%d : %s", port, buf);
2472 if (imsm_read_serial(fd, NULL, (__u8 *)buf,
2473 sizeof(buf)) == 0)
2474 printf(" (%s)\n", buf);
2475 else
2476 printf(" ()\n");
2477 close(fd);
2478 }
2479 free(path);
2480 path = NULL;
2481 }
2482 if (path)
2483 free(path);
2484 if (dir)
2485 closedir(dir);
2486 if (err == 0) {
2487 int i;
2488
2489 for (i = 0; i < port_count; i++)
2490 if (port_mask & (1 << i))
2491 printf(" Port%d : - no device attached -\n", i);
2492 }
2493
2494 return err;
2495 }
2496
2497 static int print_nvme_info(struct sys_dev *hba)
2498 {
2499 struct dirent *ent;
2500 DIR *dir;
2501
2502 dir = opendir("/sys/block/");
2503 if (!dir)
2504 return 1;
2505
2506 for (ent = readdir(dir); ent; ent = readdir(dir)) {
2507 char ns_path[PATH_MAX];
2508 char cntrl_path[PATH_MAX];
2509 char buf[PATH_MAX];
2510 int fd = -1;
2511
2512 if (!strstr(ent->d_name, "nvme"))
2513 goto skip;
2514
2515 fd = open_dev(ent->d_name);
2516 if (!is_fd_valid(fd))
2517 goto skip;
2518
2519 if (!diskfd_to_devpath(fd, 0, ns_path) ||
2520 !diskfd_to_devpath(fd, 1, cntrl_path))
2521 goto skip;
2522
2523 if (!path_attached_to_hba(cntrl_path, hba->path))
2524 goto skip;
2525
2526 if (!imsm_is_nvme_namespace_supported(fd, 0))
2527 goto skip;
2528
2529 fd2devname(fd, buf);
2530 if (hba->type == SYS_DEV_VMD)
2531 printf(" NVMe under VMD : %s", buf);
2532 else if (hba->type == SYS_DEV_NVME)
2533 printf(" NVMe Device : %s", buf);
2534
2535 if (!imsm_read_serial(fd, NULL, (__u8 *)buf,
2536 sizeof(buf)))
2537 printf(" (%s)\n", buf);
2538 else
2539 printf("()\n");
2540
2541 skip:
2542 close_fd(&fd);
2543 }
2544
2545 closedir(dir);
2546 return 0;
2547 }
2548
2549 static void print_found_intel_controllers(struct sys_dev *elem)
2550 {
2551 for (; elem; elem = elem->next) {
2552 pr_err("found Intel(R) ");
2553 if (elem->type == SYS_DEV_SATA)
2554 fprintf(stderr, "SATA ");
2555 else if (elem->type == SYS_DEV_SAS)
2556 fprintf(stderr, "SAS ");
2557 else if (elem->type == SYS_DEV_NVME)
2558 fprintf(stderr, "NVMe ");
2559
2560 if (elem->type == SYS_DEV_VMD)
2561 fprintf(stderr, "VMD domain");
2562 else
2563 fprintf(stderr, "RAID controller");
2564
2565 if (elem->pci_id)
2566 fprintf(stderr, " at %s", elem->pci_id);
2567 fprintf(stderr, ".\n");
2568 }
2569 fflush(stderr);
2570 }
2571
2572 static int ahci_get_port_count(const char *hba_path, int *port_count)
2573 {
2574 struct dirent *ent;
2575 DIR *dir;
2576 int host_base = -1;
2577
2578 *port_count = 0;
2579 if ((dir = opendir(hba_path)) == NULL)
2580 return -1;
2581
2582 for (ent = readdir(dir); ent; ent = readdir(dir)) {
2583 int host;
2584
2585 if ((sscanf(ent->d_name, "ata%d", &host) != 1) &&
2586 ((sscanf(ent->d_name, "host%d", &host) != 1)))
2587 continue;
2588 if (*port_count == 0)
2589 host_base = host;
2590 else if (host < host_base)
2591 host_base = host;
2592
2593 if (host + 1 > *port_count + host_base)
2594 *port_count = host + 1 - host_base;
2595 }
2596 closedir(dir);
2597 return host_base;
2598 }
2599
2600 static void print_imsm_capability(const struct imsm_orom *orom)
2601 {
2602 printf(" Platform : Intel(R) ");
2603 if (orom->capabilities == 0 && orom->driver_features == 0)
2604 printf("Matrix Storage Manager\n");
2605 else if (imsm_orom_is_enterprise(orom) && orom->major_ver >= 6)
2606 printf("Virtual RAID on CPU\n");
2607 else
2608 printf("Rapid Storage Technology%s\n",
2609 imsm_orom_is_enterprise(orom) ? " enterprise" : "");
2610 if (orom->major_ver || orom->minor_ver || orom->hotfix_ver || orom->build)
2611 printf(" Version : %d.%d.%d.%d\n", orom->major_ver,
2612 orom->minor_ver, orom->hotfix_ver, orom->build);
2613 printf(" RAID Levels :%s%s%s%s%s\n",
2614 imsm_orom_has_raid0(orom) ? " raid0" : "",
2615 imsm_orom_has_raid1(orom) ? " raid1" : "",
2616 imsm_orom_has_raid1e(orom) ? " raid1e" : "",
2617 imsm_orom_has_raid10(orom) ? " raid10" : "",
2618 imsm_orom_has_raid5(orom) ? " raid5" : "");
2619 printf(" Chunk Sizes :%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
2620 imsm_orom_has_chunk(orom, 2) ? " 2k" : "",
2621 imsm_orom_has_chunk(orom, 4) ? " 4k" : "",
2622 imsm_orom_has_chunk(orom, 8) ? " 8k" : "",
2623 imsm_orom_has_chunk(orom, 16) ? " 16k" : "",
2624 imsm_orom_has_chunk(orom, 32) ? " 32k" : "",
2625 imsm_orom_has_chunk(orom, 64) ? " 64k" : "",
2626 imsm_orom_has_chunk(orom, 128) ? " 128k" : "",
2627 imsm_orom_has_chunk(orom, 256) ? " 256k" : "",
2628 imsm_orom_has_chunk(orom, 512) ? " 512k" : "",
2629 imsm_orom_has_chunk(orom, 1024*1) ? " 1M" : "",
2630 imsm_orom_has_chunk(orom, 1024*2) ? " 2M" : "",
2631 imsm_orom_has_chunk(orom, 1024*4) ? " 4M" : "",
2632 imsm_orom_has_chunk(orom, 1024*8) ? " 8M" : "",
2633 imsm_orom_has_chunk(orom, 1024*16) ? " 16M" : "",
2634 imsm_orom_has_chunk(orom, 1024*32) ? " 32M" : "",
2635 imsm_orom_has_chunk(orom, 1024*64) ? " 64M" : "");
2636 printf(" 2TB volumes :%s supported\n",
2637 (orom->attr & IMSM_OROM_ATTR_2TB)?"":" not");
2638 printf(" 2TB disks :%s supported\n",
2639 (orom->attr & IMSM_OROM_ATTR_2TB_DISK)?"":" not");
2640 printf(" Max Disks : %d\n", orom->tds);
2641 printf(" Max Volumes : %d per array, %d per %s\n",
2642 orom->vpa, orom->vphba,
2643 imsm_orom_is_nvme(orom) ? "platform" : "controller");
2644 return;
2645 }
2646
2647 static void print_imsm_capability_export(const struct imsm_orom *orom)
2648 {
2649 printf("MD_FIRMWARE_TYPE=imsm\n");
2650 if (orom->major_ver || orom->minor_ver || orom->hotfix_ver || orom->build)
2651 printf("IMSM_VERSION=%d.%d.%d.%d\n", orom->major_ver, orom->minor_ver,
2652 orom->hotfix_ver, orom->build);
2653 printf("IMSM_SUPPORTED_RAID_LEVELS=%s%s%s%s%s\n",
2654 imsm_orom_has_raid0(orom) ? "raid0 " : "",
2655 imsm_orom_has_raid1(orom) ? "raid1 " : "",
2656 imsm_orom_has_raid1e(orom) ? "raid1e " : "",
2657 imsm_orom_has_raid5(orom) ? "raid10 " : "",
2658 imsm_orom_has_raid10(orom) ? "raid5 " : "");
2659 printf("IMSM_SUPPORTED_CHUNK_SIZES=%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
2660 imsm_orom_has_chunk(orom, 2) ? "2k " : "",
2661 imsm_orom_has_chunk(orom, 4) ? "4k " : "",
2662 imsm_orom_has_chunk(orom, 8) ? "8k " : "",
2663 imsm_orom_has_chunk(orom, 16) ? "16k " : "",
2664 imsm_orom_has_chunk(orom, 32) ? "32k " : "",
2665 imsm_orom_has_chunk(orom, 64) ? "64k " : "",
2666 imsm_orom_has_chunk(orom, 128) ? "128k " : "",
2667 imsm_orom_has_chunk(orom, 256) ? "256k " : "",
2668 imsm_orom_has_chunk(orom, 512) ? "512k " : "",
2669 imsm_orom_has_chunk(orom, 1024*1) ? "1M " : "",
2670 imsm_orom_has_chunk(orom, 1024*2) ? "2M " : "",
2671 imsm_orom_has_chunk(orom, 1024*4) ? "4M " : "",
2672 imsm_orom_has_chunk(orom, 1024*8) ? "8M " : "",
2673 imsm_orom_has_chunk(orom, 1024*16) ? "16M " : "",
2674 imsm_orom_has_chunk(orom, 1024*32) ? "32M " : "",
2675 imsm_orom_has_chunk(orom, 1024*64) ? "64M " : "");
2676 printf("IMSM_2TB_VOLUMES=%s\n",(orom->attr & IMSM_OROM_ATTR_2TB) ? "yes" : "no");
2677 printf("IMSM_2TB_DISKS=%s\n",(orom->attr & IMSM_OROM_ATTR_2TB_DISK) ? "yes" : "no");
2678 printf("IMSM_MAX_DISKS=%d\n",orom->tds);
2679 printf("IMSM_MAX_VOLUMES_PER_ARRAY=%d\n",orom->vpa);
2680 printf("IMSM_MAX_VOLUMES_PER_CONTROLLER=%d\n",orom->vphba);
2681 }
2682
2683 static int detail_platform_imsm(int verbose, int enumerate_only, char *controller_path)
2684 {
2685 /* There are two components to imsm platform support, the ahci SATA
2686 * controller and the option-rom. To find the SATA controller we
2687 * simply look in /sys/bus/pci/drivers/ahci to see if an ahci
2688 * controller with the Intel vendor id is present. This approach
2689 * allows mdadm to leverage the kernel's ahci detection logic, with the
2690 * caveat that if ahci.ko is not loaded mdadm will not be able to
2691 * detect platform raid capabilities. The option-rom resides in a
2692 * platform "Adapter ROM". We scan for its signature to retrieve the
2693 * platform capabilities. If raid support is disabled in the BIOS the
2694 * option-rom capability structure will not be available.
2695 */
2696 struct sys_dev *list, *hba;
2697 int host_base = 0;
2698 int port_count = 0;
2699 int result=1;
2700
2701 if (enumerate_only) {
2702 if (check_env("IMSM_NO_PLATFORM"))
2703 return 0;
2704 list = find_intel_devices();
2705 if (!list)
2706 return 2;
2707 for (hba = list; hba; hba = hba->next) {
2708 if (find_imsm_capability(hba)) {
2709 result = 0;
2710 break;
2711 }
2712 else
2713 result = 2;
2714 }
2715 return result;
2716 }
2717
2718 list = find_intel_devices();
2719 if (!list) {
2720 if (verbose > 0)
2721 pr_err("no active Intel(R) RAID controller found.\n");
2722 return 2;
2723 } else if (verbose > 0)
2724 print_found_intel_controllers(list);
2725
2726 for (hba = list; hba; hba = hba->next) {
2727 if (controller_path && (compare_paths(hba->path, controller_path) != 0))
2728 continue;
2729 if (!find_imsm_capability(hba)) {
2730 char buf[PATH_MAX];
2731 pr_err("imsm capabilities not found for controller: %s (type %s)\n",
2732 hba->type == SYS_DEV_VMD ? vmd_domain_to_controller(hba, buf) : hba->path,
2733 get_sys_dev_type(hba->type));
2734 continue;
2735 }
2736 result = 0;
2737 }
2738
2739 if (controller_path && result == 1) {
2740 pr_err("no active Intel(R) RAID controller found under %s\n",
2741 controller_path);
2742 return result;
2743 }
2744
2745 const struct orom_entry *entry;
2746
2747 for (entry = orom_entries; entry; entry = entry->next) {
2748 if (entry->type == SYS_DEV_VMD) {
2749 print_imsm_capability(&entry->orom);
2750 printf(" 3rd party NVMe :%s supported\n",
2751 imsm_orom_has_tpv_support(&entry->orom)?"":" not");
2752 for (hba = list; hba; hba = hba->next) {
2753 if (hba->type == SYS_DEV_VMD) {
2754 char buf[PATH_MAX];
2755 printf(" I/O Controller : %s (%s)\n",
2756 vmd_domain_to_controller(hba, buf), get_sys_dev_type(hba->type));
2757 if (print_nvme_info(hba)) {
2758 if (verbose > 0)
2759 pr_err("failed to get devices attached to VMD domain.\n");
2760 result |= 2;
2761 }
2762 }
2763 }
2764 printf("\n");
2765 continue;
2766 }
2767
2768 print_imsm_capability(&entry->orom);
2769 if (entry->type == SYS_DEV_NVME) {
2770 for (hba = list; hba; hba = hba->next) {
2771 if (hba->type == SYS_DEV_NVME)
2772 print_nvme_info(hba);
2773 }
2774 printf("\n");
2775 continue;
2776 }
2777
2778 struct devid_list *devid;
2779 for (devid = entry->devid_list; devid; devid = devid->next) {
2780 hba = device_by_id(devid->devid);
2781 if (!hba)
2782 continue;
2783
2784 printf(" I/O Controller : %s (%s)\n",
2785 hba->path, get_sys_dev_type(hba->type));
2786 if (hba->type == SYS_DEV_SATA) {
2787 host_base = ahci_get_port_count(hba->path, &port_count);
2788 if (ahci_enumerate_ports(hba->path, port_count, host_base, verbose)) {
2789 if (verbose > 0)
2790 pr_err("failed to enumerate ports on SATA controller at %s.\n", hba->pci_id);
2791 result |= 2;
2792 }
2793 }
2794 }
2795 printf("\n");
2796 }
2797
2798 return result;
2799 }
2800
2801 static int export_detail_platform_imsm(int verbose, char *controller_path)
2802 {
2803 struct sys_dev *list, *hba;
2804 int result=1;
2805
2806 list = find_intel_devices();
2807 if (!list) {
2808 if (verbose > 0)
2809 pr_err("IMSM_DETAIL_PLATFORM_ERROR=NO_INTEL_DEVICES\n");
2810 result = 2;
2811 return result;
2812 }
2813
2814 for (hba = list; hba; hba = hba->next) {
2815 if (controller_path && (compare_paths(hba->path,controller_path) != 0))
2816 continue;
2817 if (!find_imsm_capability(hba) && verbose > 0) {
2818 char buf[PATH_MAX];
2819 pr_err("IMSM_DETAIL_PLATFORM_ERROR=NO_IMSM_CAPABLE_DEVICE_UNDER_%s\n",
2820 hba->type == SYS_DEV_VMD ? vmd_domain_to_controller(hba, buf) : hba->path);
2821 }
2822 else
2823 result = 0;
2824 }
2825
2826 const struct orom_entry *entry;
2827
2828 for (entry = orom_entries; entry; entry = entry->next) {
2829 if (entry->type == SYS_DEV_VMD) {
2830 for (hba = list; hba; hba = hba->next)
2831 print_imsm_capability_export(&entry->orom);
2832 continue;
2833 }
2834 print_imsm_capability_export(&entry->orom);
2835 }
2836
2837 return result;
2838 }
2839
2840 static int match_home_imsm(struct supertype *st, char *homehost)
2841 {
2842 /* the imsm metadata format does not specify any host
2843 * identification information. We return -1 since we can never
2844 * confirm nor deny whether a given array is "meant" for this
2845 * host. We rely on compare_super and the 'family_num' fields to
2846 * exclude member disks that do not belong, and we rely on
2847 * mdadm.conf to specify the arrays that should be assembled.
2848 * Auto-assembly may still pick up "foreign" arrays.
2849 */
2850
2851 return -1;
2852 }
2853
2854 static void uuid_from_super_imsm(struct supertype *st, int uuid[4])
2855 {
2856 /* The uuid returned here is used for:
2857 * uuid to put into bitmap file (Create, Grow)
2858 * uuid for backup header when saving critical section (Grow)
2859 * comparing uuids when re-adding a device into an array
2860 * In these cases the uuid required is that of the data-array,
2861 * not the device-set.
2862 * uuid to recognise same set when adding a missing device back
2863 * to an array. This is a uuid for the device-set.
2864 *
2865 * For each of these we can make do with a truncated
2866 * or hashed uuid rather than the original, as long as
2867 * everyone agrees.
2868 * In each case the uuid required is that of the data-array,
2869 * not the device-set.
2870 */
2871 /* imsm does not track uuid's so we synthesis one using sha1 on
2872 * - The signature (Which is constant for all imsm array, but no matter)
2873 * - the orig_family_num of the container
2874 * - the index number of the volume
2875 * - the 'serial' number of the volume.
2876 * Hopefully these are all constant.
2877 */
2878 struct intel_super *super = st->sb;
2879
2880 char buf[20];
2881 struct sha1_ctx ctx;
2882 struct imsm_dev *dev = NULL;
2883 __u32 family_num;
2884
2885 /* some mdadm versions failed to set ->orig_family_num, in which
2886 * case fall back to ->family_num. orig_family_num will be
2887 * fixed up with the first metadata update.
2888 */
2889 family_num = super->anchor->orig_family_num;
2890 if (family_num == 0)
2891 family_num = super->anchor->family_num;
2892 sha1_init_ctx(&ctx);
2893 sha1_process_bytes(super->anchor->sig, MPB_SIG_LEN, &ctx);
2894 sha1_process_bytes(&family_num, sizeof(__u32), &ctx);
2895 if (super->current_vol >= 0)
2896 dev = get_imsm_dev(super, super->current_vol);
2897 if (dev) {
2898 __u32 vol = super->current_vol;
2899 sha1_process_bytes(&vol, sizeof(vol), &ctx);
2900 sha1_process_bytes(dev->volume, MAX_RAID_SERIAL_LEN, &ctx);
2901 }
2902 sha1_finish_ctx(&ctx, buf);
2903 memcpy(uuid, buf, 4*4);
2904 }
2905
2906 #if 0
2907 static void
2908 get_imsm_numerical_version(struct imsm_super *mpb, int *m, int *p)
2909 {
2910 __u8 *v = get_imsm_version(mpb);
2911 __u8 *end = mpb->sig + MAX_SIGNATURE_LENGTH;
2912 char major[] = { 0, 0, 0 };
2913 char minor[] = { 0 ,0, 0 };
2914 char patch[] = { 0, 0, 0 };
2915 char *ver_parse[] = { major, minor, patch };
2916 int i, j;
2917
2918 i = j = 0;
2919 while (*v != '\0' && v < end) {
2920 if (*v != '.' && j < 2)
2921 ver_parse[i][j++] = *v;
2922 else {
2923 i++;
2924 j = 0;
2925 }
2926 v++;
2927 }
2928
2929 *m = strtol(minor, NULL, 0);
2930 *p = strtol(patch, NULL, 0);
2931 }
2932 #endif
2933
2934 static __u32 migr_strip_blocks_resync(struct imsm_dev *dev)
2935 {
2936 /* migr_strip_size when repairing or initializing parity */
2937 struct imsm_map *map = get_imsm_map(dev, MAP_0);
2938 __u32 chunk = __le32_to_cpu(map->blocks_per_strip);
2939
2940 switch (get_imsm_raid_level(map)) {
2941 case 5:
2942 case 10:
2943 return chunk;
2944 default:
2945 return 128*1024 >> 9;
2946 }
2947 }
2948
2949 static __u32 migr_strip_blocks_rebuild(struct imsm_dev *dev)
2950 {
2951 /* migr_strip_size when rebuilding a degraded disk, no idea why
2952 * this is different than migr_strip_size_resync(), but it's good
2953 * to be compatible
2954 */
2955 struct imsm_map *map = get_imsm_map(dev, MAP_1);
2956 __u32 chunk = __le32_to_cpu(map->blocks_per_strip);
2957
2958 switch (get_imsm_raid_level(map)) {
2959 case 1:
2960 case 10:
2961 if (map->num_members % map->num_domains == 0)
2962 return 128*1024 >> 9;
2963 else
2964 return chunk;
2965 case 5:
2966 return max((__u32) 64*1024 >> 9, chunk);
2967 default:
2968 return 128*1024 >> 9;
2969 }
2970 }
2971
2972 static __u32 num_stripes_per_unit_resync(struct imsm_dev *dev)
2973 {
2974 struct imsm_map *lo = get_imsm_map(dev, MAP_0);
2975 struct imsm_map *hi = get_imsm_map(dev, MAP_1);
2976 __u32 lo_chunk = __le32_to_cpu(lo->blocks_per_strip);
2977 __u32 hi_chunk = __le32_to_cpu(hi->blocks_per_strip);
2978
2979 return max((__u32) 1, hi_chunk / lo_chunk);
2980 }
2981
2982 static __u32 num_stripes_per_unit_rebuild(struct imsm_dev *dev)
2983 {
2984 struct imsm_map *lo = get_imsm_map(dev, MAP_0);
2985 int level = get_imsm_raid_level(lo);
2986
2987 if (level == 1 || level == 10) {
2988 struct imsm_map *hi = get_imsm_map(dev, MAP_1);
2989
2990 return hi->num_domains;
2991 } else
2992 return num_stripes_per_unit_resync(dev);
2993 }
2994
2995 static unsigned long long calc_component_size(struct imsm_map *map,
2996 struct imsm_dev *dev)
2997 {
2998 unsigned long long component_size;
2999 unsigned long long dev_size = imsm_dev_size(dev);
3000 long long calc_dev_size = 0;
3001 unsigned int member_disks = imsm_num_data_members(map);
3002
3003 if (member_disks == 0)
3004 return 0;
3005
3006 component_size = per_dev_array_size(map);
3007 calc_dev_size = component_size * member_disks;
3008
3009 /* Component size is rounded to 1MB so difference between size from
3010 * metadata and size calculated from num_data_stripes equals up to
3011 * 2048 blocks per each device. If the difference is higher it means
3012 * that array size was expanded and num_data_stripes was not updated.
3013 */
3014 if (llabs(calc_dev_size - (long long)dev_size) >
3015 (1 << SECT_PER_MB_SHIFT) * member_disks) {
3016 component_size = dev_size / member_disks;
3017 dprintf("Invalid num_data_stripes in metadata; expected=%llu, found=%llu\n",
3018 component_size / map->blocks_per_strip,
3019 num_data_stripes(map));
3020 }
3021
3022 return component_size;
3023 }
3024
3025 static __u32 parity_segment_depth(struct imsm_dev *dev)
3026 {
3027 struct imsm_map *map = get_imsm_map(dev, MAP_0);
3028 __u32 chunk = __le32_to_cpu(map->blocks_per_strip);
3029
3030 switch(get_imsm_raid_level(map)) {
3031 case 1:
3032 case 10:
3033 return chunk * map->num_domains;
3034 case 5:
3035 return chunk * map->num_members;
3036 default:
3037 return chunk;
3038 }
3039 }
3040
3041 static __u32 map_migr_block(struct imsm_dev *dev, __u32 block)
3042 {
3043 struct imsm_map *map = get_imsm_map(dev, MAP_1);
3044 __u32 chunk = __le32_to_cpu(map->blocks_per_strip);
3045 __u32 strip = block / chunk;
3046
3047 switch (get_imsm_raid_level(map)) {
3048 case 1:
3049 case 10: {
3050 __u32 vol_strip = (strip * map->num_domains) + 1;
3051 __u32 vol_stripe = vol_strip / map->num_members;
3052
3053 return vol_stripe * chunk + block % chunk;
3054 } case 5: {
3055 __u32 stripe = strip / (map->num_members - 1);
3056
3057 return stripe * chunk + block % chunk;
3058 }
3059 default:
3060 return 0;
3061 }
3062 }
3063
3064 static __u64 blocks_per_migr_unit(struct intel_super *super,
3065 struct imsm_dev *dev)
3066 {
3067 /* calculate the conversion factor between per member 'blocks'
3068 * (md/{resync,rebuild}_start) and imsm migration units, return
3069 * 0 for the 'not migrating' and 'unsupported migration' cases
3070 */
3071 if (!dev->vol.migr_state)
3072 return 0;
3073
3074 switch (migr_type(dev)) {
3075 case MIGR_GEN_MIGR: {
3076 struct migr_record *migr_rec = super->migr_rec;
3077 return __le32_to_cpu(migr_rec->blocks_per_unit);
3078 }
3079 case MIGR_VERIFY:
3080 case MIGR_REPAIR:
3081 case MIGR_INIT: {
3082 struct imsm_map *map = get_imsm_map(dev, MAP_0);
3083 __u32 stripes_per_unit;
3084 __u32 blocks_per_unit;
3085 __u32 parity_depth;
3086 __u32 migr_chunk;
3087 __u32 block_map;
3088 __u32 block_rel;
3089 __u32 segment;
3090 __u32 stripe;
3091 __u8 disks;
3092
3093 /* yes, this is really the translation of migr_units to
3094 * per-member blocks in the 'resync' case
3095 */
3096 stripes_per_unit = num_stripes_per_unit_resync(dev);
3097 migr_chunk = migr_strip_blocks_resync(dev);
3098 disks = imsm_num_data_members(map);
3099 blocks_per_unit = stripes_per_unit * migr_chunk * disks;
3100 stripe = __le16_to_cpu(map->blocks_per_strip) * disks;
3101 segment = blocks_per_unit / stripe;
3102 block_rel = blocks_per_unit - segment * stripe;
3103 parity_depth = parity_segment_depth(dev);
3104 block_map = map_migr_block(dev, block_rel);
3105 return block_map + parity_depth * segment;
3106 }
3107 case MIGR_REBUILD: {
3108 __u32 stripes_per_unit;
3109 __u32 migr_chunk;
3110
3111 stripes_per_unit = num_stripes_per_unit_rebuild(dev);
3112 migr_chunk = migr_strip_blocks_rebuild(dev);
3113 return migr_chunk * stripes_per_unit;
3114 }
3115 case MIGR_STATE_CHANGE:
3116 default:
3117 return 0;
3118 }
3119 }
3120
3121 static int imsm_level_to_layout(int level)
3122 {
3123 switch (level) {
3124 case 0:
3125 case 1:
3126 return 0;
3127 case 5:
3128 case 6:
3129 return ALGORITHM_LEFT_ASYMMETRIC;
3130 case 10:
3131 return 0x102;
3132 }
3133 return UnSet;
3134 }
3135
3136 /*******************************************************************************
3137 * Function: read_imsm_migr_rec
3138 * Description: Function reads imsm migration record from last sector of disk
3139 * Parameters:
3140 * fd : disk descriptor
3141 * super : metadata info
3142 * Returns:
3143 * 0 : success,
3144 * -1 : fail
3145 ******************************************************************************/
3146 static int read_imsm_migr_rec(int fd, struct intel_super *super)
3147 {
3148 int ret_val = -1;
3149 unsigned int sector_size = super->sector_size;
3150 unsigned long long dsize;
3151
3152 get_dev_size(fd, NULL, &dsize);
3153 if (lseek64(fd, dsize - (sector_size*MIGR_REC_SECTOR_POSITION),
3154 SEEK_SET) < 0) {
3155 pr_err("Cannot seek to anchor block: %s\n",
3156 strerror(errno));
3157 goto out;
3158 }
3159 if ((unsigned int)read(fd, super->migr_rec_buf,
3160 MIGR_REC_BUF_SECTORS*sector_size) !=
3161 MIGR_REC_BUF_SECTORS*sector_size) {
3162 pr_err("Cannot read migr record block: %s\n",
3163 strerror(errno));
3164 goto out;
3165 }
3166 ret_val = 0;
3167 if (sector_size == 4096)
3168 convert_from_4k_imsm_migr_rec(super);
3169
3170 out:
3171 return ret_val;
3172 }
3173
3174 static struct imsm_dev *imsm_get_device_during_migration(
3175 struct intel_super *super)
3176 {
3177
3178 struct intel_dev *dv;
3179
3180 for (dv = super->devlist; dv; dv = dv->next) {
3181 if (is_gen_migration(dv->dev))
3182 return dv->dev;
3183 }
3184 return NULL;
3185 }
3186
3187 /*******************************************************************************
3188 * Function: load_imsm_migr_rec
3189 * Description: Function reads imsm migration record (it is stored at the last
3190 * sector of disk)
3191 * Parameters:
3192 * super : imsm internal array info
3193 * Returns:
3194 * 0 : success
3195 * -1 : fail
3196 * -2 : no migration in progress
3197 ******************************************************************************/
3198 static int load_imsm_migr_rec(struct intel_super *super)
3199 {
3200 struct dl *dl;
3201 char nm[30];
3202 int retval = -1;
3203 int fd = -1;
3204 struct imsm_dev *dev;
3205 struct imsm_map *map;
3206 int slot = -1;
3207 int keep_fd = 1;
3208
3209 /* find map under migration */
3210 dev = imsm_get_device_during_migration(super);
3211 /* nothing to load,no migration in progress?
3212 */
3213 if (dev == NULL)
3214 return -2;
3215
3216 map = get_imsm_map(dev, MAP_0);
3217 if (!map)
3218 return -1;
3219
3220 for (dl = super->disks; dl; dl = dl->next) {
3221 /* skip spare and failed disks
3222 */
3223 if (dl->index < 0)
3224 continue;
3225 /* read only from one of the first two slots
3226 */
3227 slot = get_imsm_disk_slot(map, dl->index);
3228 if (slot > 1 || slot < 0)
3229 continue;
3230
3231 if (!is_fd_valid(dl->fd)) {
3232 sprintf(nm, "%d:%d", dl->major, dl->minor);
3233 fd = dev_open(nm, O_RDONLY);
3234
3235 if (is_fd_valid(fd)) {
3236 keep_fd = 0;
3237 break;
3238 }
3239 } else {
3240 fd = dl->fd;
3241 break;
3242 }
3243 }
3244
3245 if (!is_fd_valid(fd))
3246 return retval;
3247 retval = read_imsm_migr_rec(fd, super);
3248 if (!keep_fd)
3249 close(fd);
3250
3251 return retval;
3252 }
3253
3254 /*******************************************************************************
3255 * function: imsm_create_metadata_checkpoint_update
3256 * Description: It creates update for checkpoint change.
3257 * Parameters:
3258 * super : imsm internal array info
3259 * u : pointer to prepared update
3260 * Returns:
3261 * Uptate length.
3262 * If length is equal to 0, input pointer u contains no update
3263 ******************************************************************************/
3264 static int imsm_create_metadata_checkpoint_update(
3265 struct intel_super *super,
3266 struct imsm_update_general_migration_checkpoint **u)
3267 {
3268
3269 int update_memory_size = 0;
3270
3271 dprintf("(enter)\n");
3272
3273 if (u == NULL)
3274 return 0;
3275 *u = NULL;
3276
3277 /* size of all update data without anchor */
3278 update_memory_size =
3279 sizeof(struct imsm_update_general_migration_checkpoint);
3280
3281 *u = xcalloc(1, update_memory_size);
3282 if (*u == NULL) {
3283 dprintf("error: cannot get memory\n");
3284 return 0;
3285 }
3286 (*u)->type = update_general_migration_checkpoint;
3287 (*u)->curr_migr_unit = current_migr_unit(super->migr_rec);
3288 dprintf("prepared for %llu\n", (unsigned long long)(*u)->curr_migr_unit);
3289
3290 return update_memory_size;
3291 }
3292
3293 static void imsm_update_metadata_locally(struct supertype *st,
3294 void *buf, int len);
3295
3296 /*******************************************************************************
3297 * Function: write_imsm_migr_rec
3298 * Description: Function writes imsm migration record
3299 * (at the last sector of disk)
3300 * Parameters:
3301 * super : imsm internal array info
3302 * Returns:
3303 * 0 : success
3304 * -1 : if fail
3305 ******************************************************************************/
3306 static int write_imsm_migr_rec(struct supertype *st)
3307 {
3308 struct intel_super *super = st->sb;
3309 unsigned int sector_size = super->sector_size;
3310 unsigned long long dsize;
3311 int retval = -1;
3312 struct dl *sd;
3313 int len;
3314 struct imsm_update_general_migration_checkpoint *u;
3315 struct imsm_dev *dev;
3316 struct imsm_map *map;
3317
3318 /* find map under migration */
3319 dev = imsm_get_device_during_migration(super);
3320 /* if no migration, write buffer anyway to clear migr_record
3321 * on disk based on first available device
3322 */
3323 if (dev == NULL)
3324 dev = get_imsm_dev(super, super->current_vol < 0 ? 0 :
3325 super->current_vol);
3326
3327 map = get_imsm_map(dev, MAP_0);
3328
3329 if (sector_size == 4096)
3330 convert_to_4k_imsm_migr_rec(super);
3331 for (sd = super->disks ; sd ; sd = sd->next) {
3332 int slot = -1;
3333
3334 /* skip failed and spare devices */
3335 if (sd->index < 0)
3336 continue;
3337 /* write to 2 first slots only */
3338 if (map)
3339 slot = get_imsm_disk_slot(map, sd->index);
3340 if (map == NULL || slot > 1 || slot < 0)
3341 continue;
3342
3343 get_dev_size(sd->fd, NULL, &dsize);
3344 if (lseek64(sd->fd, dsize - (MIGR_REC_SECTOR_POSITION *
3345 sector_size),
3346 SEEK_SET) < 0) {
3347 pr_err("Cannot seek to anchor block: %s\n",
3348 strerror(errno));
3349 goto out;
3350 }
3351 if ((unsigned int)write(sd->fd, super->migr_rec_buf,
3352 MIGR_REC_BUF_SECTORS*sector_size) !=
3353 MIGR_REC_BUF_SECTORS*sector_size) {
3354 pr_err("Cannot write migr record block: %s\n",
3355 strerror(errno));
3356 goto out;
3357 }
3358 }
3359 if (sector_size == 4096)
3360 convert_from_4k_imsm_migr_rec(super);
3361 /* update checkpoint information in metadata */
3362 len = imsm_create_metadata_checkpoint_update(super, &u);
3363 if (len <= 0) {
3364 dprintf("imsm: Cannot prepare update\n");
3365 goto out;
3366 }
3367 /* update metadata locally */
3368 imsm_update_metadata_locally(st, u, len);
3369 /* and possibly remotely */
3370 if (st->update_tail) {
3371 append_metadata_update(st, u, len);
3372 /* during reshape we do all work inside metadata handler
3373 * manage_reshape(), so metadata update has to be triggered
3374 * insida it
3375 */
3376 flush_metadata_updates(st);
3377 st->update_tail = &st->updates;
3378 } else
3379 free(u);
3380
3381 retval = 0;
3382 out:
3383 return retval;
3384 }
3385
3386 /* spare/missing disks activations are not allowe when
3387 * array/container performs reshape operation, because
3388 * all arrays in container works on the same disks set
3389 */
3390 int imsm_reshape_blocks_arrays_changes(struct intel_super *super)
3391 {
3392 int rv = 0;
3393 struct intel_dev *i_dev;
3394 struct imsm_dev *dev;
3395
3396 /* check whole container
3397 */
3398 for (i_dev = super->devlist; i_dev; i_dev = i_dev->next) {
3399 dev = i_dev->dev;
3400 if (is_gen_migration(dev)) {
3401 /* No repair during any migration in container
3402 */
3403 rv = 1;
3404 break;
3405 }
3406 }
3407 return rv;
3408 }
3409 static unsigned long long imsm_component_size_alignment_check(int level,
3410 int chunk_size,
3411 unsigned int sector_size,
3412 unsigned long long component_size)
3413 {
3414 unsigned int component_size_alignment;
3415
3416 /* check component size alignment
3417 */
3418 component_size_alignment = component_size % (chunk_size/sector_size);
3419
3420 dprintf("(Level: %i, chunk_size = %i, component_size = %llu), component_size_alignment = %u\n",
3421 level, chunk_size, component_size,
3422 component_size_alignment);
3423
3424 if (component_size_alignment && (level != 1) && (level != UnSet)) {
3425 dprintf("imsm: reported component size aligned from %llu ",
3426 component_size);
3427 component_size -= component_size_alignment;
3428 dprintf_cont("to %llu (%i).\n",
3429 component_size, component_size_alignment);
3430 }
3431
3432 return component_size;
3433 }
3434
3435 /*******************************************************************************
3436 * Function: get_bitmap_header_sector
3437 * Description: Returns the sector where the bitmap header is placed.
3438 * Parameters:
3439 * st : supertype information
3440 * dev_idx : index of the device with bitmap
3441 *
3442 * Returns:
3443 * The sector where the bitmap header is placed
3444 ******************************************************************************/
3445 static unsigned long long get_bitmap_header_sector(struct intel_super *super,
3446 int dev_idx)
3447 {
3448 struct imsm_dev *dev = get_imsm_dev(super, dev_idx);
3449 struct imsm_map *map = get_imsm_map(dev, MAP_0);
3450
3451 if (!super->sector_size) {
3452 dprintf("sector size is not set\n");
3453 return 0;
3454 }
3455
3456 return pba_of_lba0(map) + calc_component_size(map, dev) +
3457 (IMSM_BITMAP_HEADER_OFFSET / super->sector_size);
3458 }
3459
3460 /*******************************************************************************
3461 * Function: get_bitmap_sector
3462 * Description: Returns the sector where the bitmap is placed.
3463 * Parameters:
3464 * st : supertype information
3465 * dev_idx : index of the device with bitmap
3466 *
3467 * Returns:
3468 * The sector where the bitmap is placed
3469 ******************************************************************************/
3470 static unsigned long long get_bitmap_sector(struct intel_super *super,
3471 int dev_idx)
3472 {
3473 if (!super->sector_size) {
3474 dprintf("sector size is not set\n");
3475 return 0;
3476 }
3477
3478 return get_bitmap_header_sector(super, dev_idx) +
3479 (IMSM_BITMAP_HEADER_SIZE / super->sector_size);
3480 }
3481
3482 static unsigned long long get_ppl_sector(struct intel_super *super, int dev_idx)
3483 {
3484 struct imsm_dev *dev = get_imsm_dev(super, dev_idx);
3485 struct imsm_map *map = get_imsm_map(dev, MAP_0);
3486
3487 return pba_of_lba0(map) +
3488 (num_data_stripes(map) * map->blocks_per_strip);
3489 }
3490
3491 static void getinfo_super_imsm_volume(struct supertype *st, struct mdinfo *info, char *dmap)
3492 {
3493 struct intel_super *super = st->sb;
3494 struct migr_record *migr_rec = super->migr_rec;
3495 struct imsm_dev *dev = get_imsm_dev(super, super->current_vol);
3496 struct imsm_map *map = get_imsm_map(dev, MAP_0);
3497 struct imsm_map *prev_map = get_imsm_map(dev, MAP_1);
3498 struct imsm_map *map_to_analyse = map;
3499 struct dl *dl;
3500 int map_disks = info->array.raid_disks;
3501
3502 memset(info, 0, sizeof(*info));
3503 if (prev_map)
3504 map_to_analyse = prev_map;
3505
3506 dl = super->current_disk;
3507
3508 info->container_member = super->current_vol;
3509 info->array.raid_disks = map->num_members;
3510 info->array.level = get_imsm_raid_level(map_to_analyse);
3511 info->array.layout = imsm_level_to_layout(info->array.level);
3512 info->array.md_minor = -1;
3513 info->array.ctime = 0;
3514 info->array.utime = 0;
3515 info->array.chunk_size =
3516 __le16_to_cpu(map_to_analyse->blocks_per_strip) << 9;
3517 info->array.state = !(dev->vol.dirty & RAIDVOL_DIRTY);
3518 info->custom_array_size = imsm_dev_size(dev);
3519 info->recovery_blocked = imsm_reshape_blocks_arrays_changes(st->sb);
3520
3521 if (is_gen_migration(dev)) {
3522 /*
3523 * device prev_map should be added if it is in the middle
3524 * of migration
3525 */
3526 assert(prev_map);
3527
3528 info->reshape_active = 1;
3529 info->new_level = get_imsm_raid_level(map);
3530 info->new_layout = imsm_level_to_layout(info->new_level);
3531 info->new_chunk = __le16_to_cpu(map->blocks_per_strip) << 9;
3532 info->delta_disks = map->num_members - prev_map->num_members;
3533 if (info->delta_disks) {
3534 /* this needs to be applied to every array
3535 * in the container.
3536 */
3537 info->reshape_active = CONTAINER_RESHAPE;
3538 }
3539 /* We shape information that we give to md might have to be
3540 * modify to cope with md's requirement for reshaping arrays.
3541 * For example, when reshaping a RAID0, md requires it to be
3542 * presented as a degraded RAID4.
3543 * Also if a RAID0 is migrating to a RAID5 we need to specify
3544 * the array as already being RAID5, but the 'before' layout
3545 * is a RAID4-like layout.
3546 */
3547 switch (info->array.level) {
3548 case 0:
3549 switch(info->new_level) {
3550 case 0:
3551 /* conversion is happening as RAID4 */
3552 info->array.level = 4;
3553 info->array.raid_disks += 1;
3554 break;
3555 case 5:
3556 /* conversion is happening as RAID5 */
3557 info->array.level = 5;
3558 info->array.layout = ALGORITHM_PARITY_N;
3559 info->delta_disks -= 1;
3560 break;
3561 default:
3562 /* FIXME error message */
3563 info->array.level = UnSet;
3564 break;
3565 }
3566 break;
3567 }
3568 } else {
3569 info->new_level = UnSet;
3570 info->new_layout = UnSet;
3571 info->new_chunk = info->array.chunk_size;
3572 info->delta_disks = 0;
3573 }
3574
3575 if (dl) {
3576 info->disk.major = dl->major;
3577 info->disk.minor = dl->minor;
3578 info->disk.number = dl->index;
3579 info->disk.raid_disk = get_imsm_disk_slot(map_to_analyse,
3580 dl->index);
3581 }
3582
3583 info->data_offset = pba_of_lba0(map_to_analyse);
3584 info->component_size = calc_component_size(map, dev);
3585 info->component_size = imsm_component_size_alignment_check(
3586 info->array.level,
3587 info->array.chunk_size,
3588 super->sector_size,
3589 info->component_size);
3590 info->bb.supported = 1;
3591
3592 memset(info->uuid, 0, sizeof(info->uuid));
3593 info->recovery_start = MaxSector;
3594
3595 if (info->array.level == 5 &&
3596 (dev->rwh_policy == RWH_DISTRIBUTED ||
3597 dev->rwh_policy == RWH_MULTIPLE_DISTRIBUTED)) {
3598 info->consistency_policy = CONSISTENCY_POLICY_PPL;
3599 info->ppl_sector = get_ppl_sector(super, super->current_vol);
3600 if (dev->rwh_policy == RWH_MULTIPLE_DISTRIBUTED)
3601 info->ppl_size = MULTIPLE_PPL_AREA_SIZE_IMSM >> 9;
3602 else
3603 info->ppl_size = (PPL_HEADER_SIZE + PPL_ENTRY_SPACE)
3604 >> 9;
3605 } else if (info->array.level <= 0) {
3606 info->consistency_policy = CONSISTENCY_POLICY_NONE;
3607 } else {
3608 if (dev->rwh_policy == RWH_BITMAP) {
3609 info->bitmap_offset = get_bitmap_sector(super, super->current_vol);
3610 info->consistency_policy = CONSISTENCY_POLICY_BITMAP;
3611 } else {
3612 info->consistency_policy = CONSISTENCY_POLICY_RESYNC;
3613 }
3614 }
3615
3616 info->reshape_progress = 0;
3617 info->resync_start = MaxSector;
3618 if ((map_to_analyse->map_state == IMSM_T_STATE_UNINITIALIZED ||
3619 !(info->array.state & 1)) &&
3620 imsm_reshape_blocks_arrays_changes(super) == 0) {
3621 info->resync_start = 0;
3622 }
3623 if (dev->vol.migr_state) {
3624 switch (migr_type(dev)) {
3625 case MIGR_REPAIR:
3626 case MIGR_INIT: {
3627 __u64 blocks_per_unit = blocks_per_migr_unit(super,
3628 dev);
3629 __u64 units = vol_curr_migr_unit(dev);
3630
3631 info->resync_start = blocks_per_unit * units;
3632 break;
3633 }
3634 case MIGR_GEN_MIGR: {
3635 __u64 blocks_per_unit = blocks_per_migr_unit(super,
3636 dev);
3637 __u64 units = current_migr_unit(migr_rec);
3638 int used_disks;
3639
3640 if (__le32_to_cpu(migr_rec->ascending_migr) &&
3641 (units <
3642 (get_num_migr_units(migr_rec)-1)) &&
3643 (super->migr_rec->rec_status ==
3644 __cpu_to_le32(UNIT_SRC_IN_CP_AREA)))
3645 units++;
3646
3647 info->reshape_progress = blocks_per_unit * units;
3648
3649 dprintf("IMSM: General Migration checkpoint : %llu (%llu) -> read reshape progress : %llu\n",
3650 (unsigned long long)units,
3651 (unsigned long long)blocks_per_unit,
3652 info->reshape_progress);
3653
3654 used_disks = imsm_num_data_members(prev_map);
3655 if (used_disks > 0) {
3656 info->custom_array_size = per_dev_array_size(map) *
3657 used_disks;
3658 }
3659 }
3660 case MIGR_VERIFY:
3661 /* we could emulate the checkpointing of
3662 * 'sync_action=check' migrations, but for now
3663 * we just immediately complete them
3664 */
3665 case MIGR_REBUILD:
3666 /* this is handled by container_content_imsm() */
3667 case MIGR_STATE_CHANGE:
3668 /* FIXME handle other migrations */
3669 default:
3670 /* we are not dirty, so... */
3671 info->resync_start = MaxSector;
3672 }
3673 }
3674
3675 strncpy(info->name, (char *) dev->volume, MAX_RAID_SERIAL_LEN);
3676 info->name[MAX_RAID_SERIAL_LEN] = 0;
3677
3678 info->array.major_version = -1;
3679 info->array.minor_version = -2;
3680 sprintf(info->text_version, "/%s/%d", st->container_devnm, info->container_member);
3681 info->safe_mode_delay = 4000; /* 4 secs like the Matrix driver */
3682 uuid_from_super_imsm(st, info->uuid);
3683
3684 if (dmap) {
3685 int i, j;
3686 for (i=0; i<map_disks; i++) {
3687 dmap[i] = 0;
3688 if (i < info->array.raid_disks) {
3689 struct imsm_disk *dsk;
3690 j = get_imsm_disk_idx(dev, i, MAP_X);
3691 dsk = get_imsm_disk(super, j);
3692 if (dsk && (dsk->status & CONFIGURED_DISK))
3693 dmap[i] = 1;
3694 }
3695 }
3696 }
3697 }
3698
3699 static __u8 imsm_check_degraded(struct intel_super *super, struct imsm_dev *dev,
3700 int failed, int look_in_map);
3701
3702 static int imsm_count_failed(struct intel_super *super, struct imsm_dev *dev,
3703 int look_in_map);
3704
3705 static void manage_second_map(struct intel_super *super, struct imsm_dev *dev)
3706 {
3707 if (is_gen_migration(dev)) {
3708 int failed;
3709 __u8 map_state;
3710 struct imsm_map *map2 = get_imsm_map(dev, MAP_1);
3711
3712 failed = imsm_count_failed(super, dev, MAP_1);
3713 map_state = imsm_check_degraded(super, dev, failed, MAP_1);
3714 if (map2->map_state != map_state) {
3715 map2->map_state = map_state;
3716 super->updates_pending++;
3717 }
3718 }
3719 }
3720
3721 static struct imsm_disk *get_imsm_missing(struct intel_super *super, __u8 index)
3722 {
3723 struct dl *d;
3724
3725 for (d = super->missing; d; d = d->next)
3726 if (d->index == index)
3727 return &d->disk;
3728 return NULL;
3729 }
3730
3731 static void getinfo_super_imsm(struct supertype *st, struct mdinfo *info, char *map)
3732 {
3733 struct intel_super *super = st->sb;
3734 struct imsm_disk *disk;
3735 int map_disks = info->array.raid_disks;
3736 int max_enough = -1;
3737 int i;
3738 struct imsm_super *mpb;
3739
3740 if (super->current_vol >= 0) {
3741 getinfo_super_imsm_volume(st, info, map);
3742 return;
3743 }
3744 memset(info, 0, sizeof(*info));
3745
3746 /* Set raid_disks to zero so that Assemble will always pull in valid
3747 * spares
3748 */
3749 info->array.raid_disks = 0;
3750 info->array.level = LEVEL_CONTAINER;
3751 info->array.layout = 0;
3752 info->array.md_minor = -1;
3753 info->array.ctime = 0; /* N/A for imsm */
3754 info->array.utime = 0;
3755 info->array.chunk_size = 0;
3756
3757 info->disk.major = 0;
3758 info->disk.minor = 0;
3759 info->disk.raid_disk = -1;
3760 info->reshape_active = 0;
3761 info->array.major_version = -1;
3762 info->array.minor_version = -2;
3763 strcpy(info->text_version, "imsm");
3764 info->safe_mode_delay = 0;
3765 info->disk.number = -1;
3766 info->disk.state = 0;
3767 info->name[0] = 0;
3768 info->recovery_start = MaxSector;
3769 info->recovery_blocked = imsm_reshape_blocks_arrays_changes(st->sb);
3770 info->bb.supported = 1;
3771
3772 /* do we have the all the insync disks that we expect? */
3773 mpb = super->anchor;
3774 info->events = __le32_to_cpu(mpb->generation_num);
3775
3776 for (i = 0; i < mpb->num_raid_devs; i++) {
3777 struct imsm_dev *dev = get_imsm_dev(super, i);
3778 int failed, enough, j, missing = 0;
3779 struct imsm_map *map;
3780 __u8 state;
3781
3782 failed = imsm_count_failed(super, dev, MAP_0);
3783 state = imsm_check_degraded(super, dev, failed, MAP_0);
3784 map = get_imsm_map(dev, MAP_0);
3785
3786 /* any newly missing disks?
3787 * (catches single-degraded vs double-degraded)
3788 */
3789 for (j = 0; j < map->num_members; j++) {
3790 __u32 ord = get_imsm_ord_tbl_ent(dev, j, MAP_0);
3791 __u32 idx = ord_to_idx(ord);
3792
3793 if (super->disks && super->disks->index == (int)idx)
3794 info->disk.raid_disk = j;
3795
3796 if (!(ord & IMSM_ORD_REBUILD) &&
3797 get_imsm_missing(super, idx)) {
3798 missing = 1;
3799 break;
3800 }
3801 }
3802
3803 if (state == IMSM_T_STATE_FAILED)
3804 enough = -1;
3805 else if (state == IMSM_T_STATE_DEGRADED &&
3806 (state != map->map_state || missing))
3807 enough = 0;
3808 else /* we're normal, or already degraded */
3809 enough = 1;
3810 if (is_gen_migration(dev) && missing) {
3811 /* during general migration we need all disks
3812 * that process is running on.
3813 * No new missing disk is allowed.
3814 */
3815 max_enough = -1;
3816 enough = -1;
3817 /* no more checks necessary
3818 */
3819 break;
3820 }
3821 /* in the missing/failed disk case check to see
3822 * if at least one array is runnable
3823 */
3824 max_enough = max(max_enough, enough);
3825 }
3826 dprintf("enough: %d\n", max_enough);
3827 info->container_enough = max_enough;
3828
3829 if (super->disks) {
3830 __u32 reserved = imsm_reserved_sectors(super, super->disks);
3831
3832 disk = &super->disks->disk;
3833 info->data_offset = total_blocks(&super->disks->disk) - reserved;
3834 info->component_size = reserved;
3835 info->disk.state = is_configured(disk) ? (1 << MD_DISK_ACTIVE) : 0;
3836 /* we don't change info->disk.raid_disk here because
3837 * this state will be finalized in mdmon after we have
3838 * found the 'most fresh' version of the metadata
3839 */
3840 info->disk.state |= is_failed(disk) ? (1 << MD_DISK_FAULTY) : 0;
3841 info->disk.state |= (is_spare(disk) || is_journal(disk)) ?
3842 0 : (1 << MD_DISK_SYNC);
3843 }
3844
3845 /* only call uuid_from_super_imsm when this disk is part of a populated container,
3846 * ->compare_super may have updated the 'num_raid_devs' field for spares
3847 */
3848 if (info->disk.state & (1 << MD_DISK_SYNC) || super->anchor->num_raid_devs)
3849 uuid_from_super_imsm(st, info->uuid);
3850 else
3851 memcpy(info->uuid, uuid_zero, sizeof(uuid_zero));
3852
3853 /* I don't know how to compute 'map' on imsm, so use safe default */
3854 if (map) {
3855 int i;
3856 for (i = 0; i < map_disks; i++)
3857 map[i] = 1;
3858 }
3859
3860 }
3861
3862 /* allocates memory and fills disk in mdinfo structure
3863 * for each disk in array */
3864 struct mdinfo *getinfo_super_disks_imsm(struct supertype *st)
3865 {
3866 struct mdinfo *mddev;
3867 struct intel_super *super = st->sb;
3868 struct imsm_disk *disk;
3869 int count = 0;
3870 struct dl *dl;
3871 if (!super || !super->disks)
3872 return NULL;
3873 dl = super->disks;
3874 mddev = xcalloc(1, sizeof(*mddev));
3875 while (dl) {
3876 struct mdinfo *tmp;
3877 disk = &dl->disk;
3878 tmp = xcalloc(1, sizeof(*tmp));
3879 if (mddev->devs)
3880 tmp->next = mddev->devs;
3881 mddev->devs = tmp;
3882 tmp->disk.number = count++;
3883 tmp->disk.major = dl->major;
3884 tmp->disk.minor = dl->minor;
3885 tmp->disk.state = is_configured(disk) ?
3886 (1 << MD_DISK_ACTIVE) : 0;
3887 tmp->disk.state |= is_failed(disk) ? (1 << MD_DISK_FAULTY) : 0;
3888 tmp->disk.state |= is_spare(disk) ? 0 : (1 << MD_DISK_SYNC);
3889 tmp->disk.raid_disk = -1;
3890 dl = dl->next;
3891 }
3892 return mddev;
3893 }
3894
3895 static int update_super_imsm(struct supertype *st, struct mdinfo *info,
3896 enum update_opt update, char *devname,
3897 int verbose, int uuid_set, char *homehost)
3898 {
3899 /* For 'assemble' and 'force' we need to return non-zero if any
3900 * change was made. For others, the return value is ignored.
3901 * Update options are:
3902 * force-one : This device looks a bit old but needs to be included,
3903 * update age info appropriately.
3904 * assemble: clear any 'faulty' flag to allow this device to
3905 * be assembled.
3906 * force-array: Array is degraded but being forced, mark it clean
3907 * if that will be needed to assemble it.
3908 *
3909 * newdev: not used ????
3910 * grow: Array has gained a new device - this is currently for
3911 * linear only
3912 * resync: mark as dirty so a resync will happen.
3913 * name: update the name - preserving the homehost
3914 * uuid: Change the uuid of the array to match watch is given
3915 *
3916 * Following are not relevant for this imsm:
3917 * sparc2.2 : update from old dodgey metadata
3918 * super-minor: change the preferred_minor number
3919 * summaries: update redundant counters.
3920 * homehost: update the recorded homehost
3921 * _reshape_progress: record new reshape_progress position.
3922 */
3923 int rv = 1;
3924 struct intel_super *super = st->sb;
3925 struct imsm_super *mpb;
3926
3927 /* we can only update container info */
3928 if (!super || super->current_vol >= 0 || !super->anchor)
3929 return 1;
3930
3931 mpb = super->anchor;
3932
3933 switch (update) {
3934 case UOPT_UUID:
3935 /* We take this to mean that the family_num should be updated.
3936 * However that is much smaller than the uuid so we cannot really
3937 * allow an explicit uuid to be given. And it is hard to reliably
3938 * know if one was.
3939 * So if !uuid_set we know the current uuid is random and just used
3940 * the first 'int' and copy it to the other 3 positions.
3941 * Otherwise we require the 4 'int's to be the same as would be the
3942 * case if we are using a random uuid. So an explicit uuid will be
3943 * accepted as long as all for ints are the same... which shouldn't hurt
3944 */
3945 if (!uuid_set) {
3946 info->uuid[1] = info->uuid[2] = info->uuid[3] = info->uuid[0];
3947 rv = 0;
3948 } else {
3949 if (info->uuid[0] != info->uuid[1] ||
3950 info->uuid[1] != info->uuid[2] ||
3951 info->uuid[2] != info->uuid[3])
3952 rv = -1;
3953 else
3954 rv = 0;
3955 }
3956 if (rv == 0)
3957 mpb->orig_family_num = info->uuid[0];
3958 break;
3959 case UOPT_SPEC_ASSEMBLE:
3960 rv = 0;
3961 break;
3962 default:
3963 rv = -1;
3964 break;
3965 }
3966
3967 /* successful update? recompute checksum */
3968 if (rv == 0)
3969 mpb->check_sum = __le32_to_cpu(__gen_imsm_checksum(mpb));
3970
3971 return rv;
3972 }
3973
3974 static size_t disks_to_mpb_size(int disks)
3975 {
3976 size_t size;
3977
3978 size = sizeof(struct imsm_super);
3979 size += (disks - 1) * sizeof(struct imsm_disk);
3980 size += 2 * sizeof(struct imsm_dev);
3981 /* up to 2 maps per raid device (-2 for imsm_maps in imsm_dev */
3982 size += (4 - 2) * sizeof(struct imsm_map);
3983 /* 4 possible disk_ord_tbl's */
3984 size += 4 * (disks - 1) * sizeof(__u32);
3985 /* maximum bbm log */
3986 size += sizeof(struct bbm_log);
3987
3988 return size;
3989 }
3990
3991 static __u64 avail_size_imsm(struct supertype *st, __u64 devsize,
3992 unsigned long long data_offset)
3993 {
3994 if (devsize < (MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS))
3995 return 0;
3996
3997 return devsize - (MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS);
3998 }
3999
4000 static void free_devlist(struct intel_super *super)
4001 {
4002 struct intel_dev *dv;
4003
4004 while (super->devlist) {
4005 dv = super->devlist->next;
4006 free(super->devlist->dev);
4007 free(super->devlist);
4008 super->devlist = dv;
4009 }
4010 }
4011
4012 static void imsm_copy_dev(struct imsm_dev *dest, struct imsm_dev *src)
4013 {
4014 memcpy(dest, src, sizeof_imsm_dev(src, 0));
4015 }
4016
4017 static int compare_super_imsm(struct supertype *st, struct supertype *tst,
4018 int verbose)
4019 {
4020 /* return:
4021 * 0 same, or first was empty, and second was copied
4022 * 1 sb are different
4023 */
4024 struct intel_super *first = st->sb;
4025 struct intel_super *sec = tst->sb;
4026
4027 if (!first) {
4028 st->sb = tst->sb;
4029 tst->sb = NULL;
4030 return 0;
4031 }
4032
4033 /* in platform dependent environment test if the disks
4034 * use the same Intel hba
4035 * if not on Intel hba at all, allow anything.
4036 * doesn't check HBAs if num_raid_devs is not set, as it means
4037 * it is a free floating spare, and all spares regardless of HBA type
4038 * will fall into separate container during the assembly
4039 */
4040 if (first->hba && sec->hba && first->anchor->num_raid_devs != 0) {
4041 if (first->hba->type != sec->hba->type) {
4042 if (verbose)
4043 pr_err("HBAs of devices do not match %s != %s\n",
4044 get_sys_dev_type(first->hba->type),
4045 get_sys_dev_type(sec->hba->type));
4046 return 1;
4047 }
4048 if (first->orom != sec->orom) {
4049 if (verbose)
4050 pr_err("HBAs of devices do not match %s != %s\n",
4051 first->hba->pci_id, sec->hba->pci_id);
4052 return 1;
4053 }
4054 }
4055
4056 if (first->anchor->num_raid_devs > 0 &&
4057 sec->anchor->num_raid_devs > 0) {
4058 /* Determine if these disks might ever have been
4059 * related. Further disambiguation can only take place
4060 * in load_super_imsm_all
4061 */
4062 __u32 first_family = first->anchor->orig_family_num;
4063 __u32 sec_family = sec->anchor->orig_family_num;
4064
4065 if (memcmp(first->anchor->sig, sec->anchor->sig,
4066 MAX_SIGNATURE_LENGTH) != 0)
4067 return 1;
4068
4069 if (first_family == 0)
4070 first_family = first->anchor->family_num;
4071 if (sec_family == 0)
4072 sec_family = sec->anchor->family_num;
4073
4074 if (first_family != sec_family)
4075 return 1;
4076
4077 }
4078
4079 /* if an anchor does not have num_raid_devs set then it is a free
4080 * floating spare. don't assosiate spare with any array, as during assembly
4081 * spares shall fall into separate container, from which they can be moved
4082 * when necessary
4083 */
4084 if (first->anchor->num_raid_devs ^ sec->anchor->num_raid_devs)
4085 return 1;
4086
4087 return 0;
4088 }
4089
4090 static void fd2devname(int fd, char *name)
4091 {
4092 char *nm;
4093
4094 nm = fd2kname(fd);
4095 if (!nm)
4096 return;
4097
4098 snprintf(name, MAX_RAID_SERIAL_LEN, "/dev/%s", nm);
4099 }
4100
4101 static int nvme_get_serial(int fd, void *buf, size_t buf_len)
4102 {
4103 char path[PATH_MAX];
4104 char *name = fd2kname(fd);
4105
4106 if (!name)
4107 return 1;
4108
4109 if (strncmp(name, "nvme", 4) != 0)
4110 return 1;
4111
4112 if (!diskfd_to_devpath(fd, 1, path))
4113 return 1;
4114
4115 return devpath_to_char(path, "serial", buf, buf_len, 0);
4116 }
4117
4118 extern int scsi_get_serial(int fd, void *buf, size_t buf_len);
4119
4120 static int imsm_read_serial(int fd, char *devname,
4121 __u8 *serial, size_t serial_buf_len)
4122 {
4123 char buf[50];
4124 int rv;
4125 size_t len;
4126 char *dest;
4127 char *src;
4128 unsigned int i;
4129
4130 memset(buf, 0, sizeof(buf));
4131
4132 rv = nvme_get_serial(fd, buf, sizeof(buf));
4133
4134 if (rv)
4135 rv = scsi_get_serial(fd, buf, sizeof(buf));
4136
4137 if (rv && check_env("IMSM_DEVNAME_AS_SERIAL")) {
4138 memset(serial, 0, MAX_RAID_SERIAL_LEN);
4139 fd2devname(fd, (char *) serial);
4140 return 0;
4141 }
4142
4143 if (rv != 0) {
4144 if (devname)
4145 pr_err("Failed to retrieve serial for %s\n",
4146 devname);
4147 return rv;
4148 }
4149
4150 /* trim all whitespace and non-printable characters and convert
4151 * ':' to ';'
4152 */
4153 for (i = 0, dest = buf; i < sizeof(buf) && buf[i]; i++) {
4154 src = &buf[i];
4155 if (*src > 0x20) {
4156 /* ':' is reserved for use in placeholder serial
4157 * numbers for missing disks
4158 */
4159 if (*src == ':')
4160 *dest++ = ';';
4161 else
4162 *dest++ = *src;
4163 }
4164 }
4165 len = dest - buf;
4166 dest = buf;
4167
4168 if (len > serial_buf_len) {
4169 /* truncate leading characters */
4170 dest += len - serial_buf_len;
4171 len = serial_buf_len;
4172 }
4173
4174 memset(serial, 0, serial_buf_len);
4175 memcpy(serial, dest, len);
4176
4177 return 0;
4178 }
4179
4180 static int serialcmp(__u8 *s1, __u8 *s2)
4181 {
4182 return strncmp((char *) s1, (char *) s2, MAX_RAID_SERIAL_LEN);
4183 }
4184
4185 static void serialcpy(__u8 *dest, __u8 *src)
4186 {
4187 strncpy((char *) dest, (char *) src, MAX_RAID_SERIAL_LEN);
4188 }
4189
4190 static struct dl *serial_to_dl(__u8 *serial, struct intel_super *super)
4191 {
4192 struct dl *dl;
4193
4194 for (dl = super->disks; dl; dl = dl->next)
4195 if (serialcmp(dl->serial, serial) == 0)
4196 break;
4197
4198 return dl;
4199 }
4200
4201 static struct imsm_disk *
4202 __serial_to_disk(__u8 *serial, struct imsm_super *mpb, int *idx)
4203 {
4204 int i;
4205
4206 for (i = 0; i < mpb->num_disks; i++) {
4207 struct imsm_disk *disk = __get_imsm_disk(mpb, i);
4208
4209 if (serialcmp(disk->serial, serial) == 0) {
4210 if (idx)
4211 *idx = i;
4212 return disk;
4213 }
4214 }
4215
4216 return NULL;
4217 }
4218
4219 static int
4220 load_imsm_disk(int fd, struct intel_super *super, char *devname, int keep_fd)
4221 {
4222 struct imsm_disk *disk;
4223 struct dl *dl;
4224 struct stat stb;
4225 int rv;
4226 char name[40];
4227 __u8 serial[MAX_RAID_SERIAL_LEN];
4228
4229 rv = imsm_read_serial(fd, devname, serial, MAX_RAID_SERIAL_LEN);
4230
4231 if (rv != 0)
4232 return 2;
4233
4234 dl = xcalloc(1, sizeof(*dl));
4235
4236 fstat(fd, &stb);
4237 dl->major = major(stb.st_rdev);
4238 dl->minor = minor(stb.st_rdev);
4239 dl->next = super->disks;
4240 dl->fd = keep_fd ? fd : -1;
4241 assert(super->disks == NULL);
4242 super->disks = dl;
4243 serialcpy(dl->serial, serial);
4244 dl->index = -2;
4245 dl->e = NULL;
4246 fd2devname(fd, name);
4247 if (devname)
4248 dl->devname = xstrdup(devname);
4249 else
4250 dl->devname = xstrdup(name);
4251
4252 /* look up this disk's index in the current anchor */
4253 disk = __serial_to_disk(dl->serial, super->anchor, &dl->index);
4254 if (disk) {
4255 dl->disk = *disk;
4256 /* only set index on disks that are a member of a
4257 * populated contianer, i.e. one with raid_devs
4258 */
4259 if (is_failed(&dl->disk))
4260 dl->index = -2;
4261 else if (is_spare(&dl->disk) || is_journal(&dl->disk))
4262 dl->index = -1;
4263 }
4264
4265 return 0;
4266 }
4267
4268 /* When migrating map0 contains the 'destination' state while map1
4269 * contains the current state. When not migrating map0 contains the
4270 * current state. This routine assumes that map[0].map_state is set to
4271 * the current array state before being called.
4272 *
4273 * Migration is indicated by one of the following states
4274 * 1/ Idle (migr_state=0 map0state=normal||unitialized||degraded||failed)
4275 * 2/ Initialize (migr_state=1 migr_type=MIGR_INIT map0state=normal
4276 * map1state=unitialized)
4277 * 3/ Repair (Resync) (migr_state=1 migr_type=MIGR_REPAIR map0state=normal
4278 * map1state=normal)
4279 * 4/ Rebuild (migr_state=1 migr_type=MIGR_REBUILD map0state=normal
4280 * map1state=degraded)
4281 * 5/ Migration (mig_state=1 migr_type=MIGR_GEN_MIGR map0state=normal
4282 * map1state=normal)
4283 */
4284 static void migrate(struct imsm_dev *dev, struct intel_super *super,
4285 __u8 to_state, int migr_type)
4286 {
4287 struct imsm_map *dest;
4288 struct imsm_map *src = get_imsm_map(dev, MAP_0);
4289
4290 dev->vol.migr_state = 1;
4291 set_migr_type(dev, migr_type);
4292 set_vol_curr_migr_unit(dev, 0);
4293 dest = get_imsm_map(dev, MAP_1);
4294
4295 /* duplicate and then set the target end state in map[0] */
4296 memcpy(dest, src, sizeof_imsm_map(src));
4297 if (migr_type == MIGR_GEN_MIGR) {
4298 __u32 ord;
4299 int i;
4300
4301 for (i = 0; i < src->num_members; i++) {
4302 ord = __le32_to_cpu(src->disk_ord_tbl[i]);
4303 set_imsm_ord_tbl_ent(src, i, ord_to_idx(ord));
4304 }
4305 }
4306
4307 if (migr_type == MIGR_GEN_MIGR)
4308 /* Clear migration record */
4309 memset(super->migr_rec, 0, sizeof(struct migr_record));
4310
4311 src->map_state = to_state;
4312 }
4313
4314 static void end_migration(struct imsm_dev *dev, struct intel_super *super,
4315 __u8 map_state)
4316 {
4317 struct imsm_map *map = get_imsm_map(dev, MAP_0);
4318 struct imsm_map *prev = get_imsm_map(dev, dev->vol.migr_state == 0 ?
4319 MAP_0 : MAP_1);
4320 int i, j;
4321
4322 /* merge any IMSM_ORD_REBUILD bits that were not successfully
4323 * completed in the last migration.
4324 *
4325 * FIXME add support for raid-level-migration
4326 */
4327 if (map_state != map->map_state && (is_gen_migration(dev) == false) &&
4328 prev->map_state != IMSM_T_STATE_UNINITIALIZED) {
4329 /* when final map state is other than expected
4330 * merge maps (not for migration)
4331 */
4332 int failed;
4333
4334 for (i = 0; i < prev->num_members; i++)
4335 for (j = 0; j < map->num_members; j++)
4336 /* during online capacity expansion
4337 * disks position can be changed
4338 * if takeover is used
4339 */
4340 if (ord_to_idx(map->disk_ord_tbl[j]) ==
4341 ord_to_idx(prev->disk_ord_tbl[i])) {
4342 map->disk_ord_tbl[j] |=
4343 prev->disk_ord_tbl[i];
4344 break;
4345 }
4346 failed = imsm_count_failed(super, dev, MAP_0);
4347 map_state = imsm_check_degraded(super, dev, failed, MAP_0);
4348 }
4349
4350 dev->vol.migr_state = 0;
4351 set_migr_type(dev, 0);
4352 set_vol_curr_migr_unit(dev, 0);
4353 map->map_state = map_state;
4354 }
4355
4356 static int parse_raid_devices(struct intel_super *super)
4357 {
4358 int i;
4359 struct imsm_dev *dev_new;
4360 size_t len, len_migr;
4361 size_t max_len = 0;
4362 size_t space_needed = 0;
4363 struct imsm_super *mpb = super->anchor;
4364
4365 for (i = 0; i < super->anchor->num_raid_devs; i++) {
4366 struct imsm_dev *dev_iter = __get_imsm_dev(super->anchor, i);
4367 struct intel_dev *dv;
4368
4369 len = sizeof_imsm_dev(dev_iter, 0);
4370 len_migr = sizeof_imsm_dev(dev_iter, 1);
4371 if (len_migr > len)
4372 space_needed += len_migr - len;
4373
4374 dv = xmalloc(sizeof(*dv));
4375 if (max_len < len_migr)
4376 max_len = len_migr;
4377 if (max_len > len_migr)
4378 space_needed += max_len - len_migr;
4379 dev_new = xmalloc(max_len);
4380 imsm_copy_dev(dev_new, dev_iter);
4381 dv->dev = dev_new;
4382 dv->index = i;
4383 dv->next = super->devlist;
4384 super->devlist = dv;
4385 }
4386
4387 /* ensure that super->buf is large enough when all raid devices
4388 * are migrating
4389 */
4390 if (__le32_to_cpu(mpb->mpb_size) + space_needed > super->len) {
4391 void *buf;
4392
4393 len = ROUND_UP(__le32_to_cpu(mpb->mpb_size) + space_needed,
4394 super->sector_size);
4395 if (posix_memalign(&buf, MAX_SECTOR_SIZE, len) != 0)
4396 return 1;
4397
4398 memcpy(buf, super->buf, super->len);
4399 memset(buf + super->len, 0, len - super->len);
4400 free(super->buf);
4401 super->buf = buf;
4402 super->len = len;
4403 }
4404
4405 super->extra_space += space_needed;
4406
4407 return 0;
4408 }
4409
4410 /*******************************************************************************
4411 * Function: check_mpb_migr_compatibility
4412 * Description: Function checks for unsupported migration features:
4413 * - migration optimization area (pba_of_lba0)
4414 * - descending reshape (ascending_migr)
4415 * Parameters:
4416 * super : imsm metadata information
4417 * Returns:
4418 * 0 : migration is compatible
4419 * -1 : migration is not compatible
4420 ******************************************************************************/
4421 int check_mpb_migr_compatibility(struct intel_super *super)
4422 {
4423 struct imsm_map *map0, *map1;
4424 struct migr_record *migr_rec = super->migr_rec;
4425 int i;
4426
4427 for (i = 0; i < super->anchor->num_raid_devs; i++) {
4428 struct imsm_dev *dev_iter = __get_imsm_dev(super->anchor, i);
4429
4430 if (dev_iter->vol.migr_state == 1 &&
4431 dev_iter->vol.migr_type == MIGR_GEN_MIGR) {
4432 /* This device is migrating */
4433 map0 = get_imsm_map(dev_iter, MAP_0);
4434 map1 = get_imsm_map(dev_iter, MAP_1);
4435 if (pba_of_lba0(map0) != pba_of_lba0(map1))
4436 /* migration optimization area was used */
4437 return -1;
4438 if (migr_rec->ascending_migr == 0 &&
4439 migr_rec->dest_depth_per_unit > 0)
4440 /* descending reshape not supported yet */
4441 return -1;
4442 }
4443 }
4444 return 0;
4445 }
4446
4447 static void __free_imsm(struct intel_super *super, int free_disks);
4448
4449 /* load_imsm_mpb - read matrix metadata
4450 * allocates super->mpb to be freed by free_imsm
4451 */
4452 static int load_imsm_mpb(int fd, struct intel_super *super, char *devname)
4453 {
4454 unsigned long long dsize;
4455 unsigned long long sectors;
4456 unsigned int sector_size = super->sector_size;
4457 struct stat;
4458 struct imsm_super *anchor;
4459 __u32 check_sum;
4460
4461 get_dev_size(fd, NULL, &dsize);
4462 if (dsize < 2*sector_size) {
4463 if (devname)
4464 pr_err("%s: device to small for imsm\n",
4465 devname);
4466 return 1;
4467 }
4468
4469 if (lseek64(fd, dsize - (sector_size * 2), SEEK_SET) < 0) {
4470 if (devname)
4471 pr_err("Cannot seek to anchor block on %s: %s\n",
4472 devname, strerror(errno));
4473 return 1;
4474 }
4475
4476 if (posix_memalign((void **)&anchor, sector_size, sector_size) != 0) {
4477 if (devname)
4478 pr_err("Failed to allocate imsm anchor buffer on %s\n", devname);
4479 return 1;
4480 }
4481 if ((unsigned int)read(fd, anchor, sector_size) != sector_size) {
4482 if (devname)
4483 pr_err("Cannot read anchor block on %s: %s\n",
4484 devname, strerror(errno));
4485 free(anchor);
4486 return 1;
4487 }
4488
4489 if (strncmp((char *) anchor->sig, MPB_SIGNATURE, MPB_SIG_LEN) != 0) {
4490 if (devname)
4491 pr_err("no IMSM anchor on %s\n", devname);
4492 free(anchor);
4493 return 2;
4494 }
4495
4496 __free_imsm(super, 0);
4497 /* reload capability and hba */
4498
4499 /* capability and hba must be updated with new super allocation */
4500 find_intel_hba_capability(fd, super, devname);
4501 super->len = ROUND_UP(anchor->mpb_size, sector_size);
4502 if (posix_memalign(&super->buf, MAX_SECTOR_SIZE, super->len) != 0) {
4503 if (devname)
4504 pr_err("unable to allocate %zu byte mpb buffer\n",
4505 super->len);
4506 free(anchor);
4507 return 2;
4508 }
4509 memcpy(super->buf, anchor, sector_size);
4510
4511 sectors = mpb_sectors(anchor, sector_size) - 1;
4512 free(anchor);
4513
4514 if (posix_memalign(&super->migr_rec_buf, MAX_SECTOR_SIZE,
4515 MIGR_REC_BUF_SECTORS*MAX_SECTOR_SIZE) != 0) {
4516 pr_err("could not allocate migr_rec buffer\n");
4517 free(super->buf);
4518 super->buf = NULL;
4519 return 2;
4520 }
4521 super->clean_migration_record_by_mdmon = 0;
4522
4523 if (!sectors) {
4524 check_sum = __gen_imsm_checksum(super->anchor);
4525 if (check_sum != __le32_to_cpu(super->anchor->check_sum)) {
4526 if (devname)
4527 pr_err("IMSM checksum %x != %x on %s\n",
4528 check_sum,
4529 __le32_to_cpu(super->anchor->check_sum),
4530 devname);
4531 return 2;
4532 }
4533
4534 return 0;
4535 }
4536
4537 /* read the extended mpb */
4538 if (lseek64(fd, dsize - (sector_size * (2 + sectors)), SEEK_SET) < 0) {
4539 if (devname)
4540 pr_err("Cannot seek to extended mpb on %s: %s\n",
4541 devname, strerror(errno));
4542 return 1;
4543 }
4544
4545 if ((unsigned int)read(fd, super->buf + sector_size,
4546 super->len - sector_size) != super->len - sector_size) {
4547 if (devname)
4548 pr_err("Cannot read extended mpb on %s: %s\n",
4549 devname, strerror(errno));
4550 return 2;
4551 }
4552
4553 check_sum = __gen_imsm_checksum(super->anchor);
4554 if (check_sum != __le32_to_cpu(super->anchor->check_sum)) {
4555 if (devname)
4556 pr_err("IMSM checksum %x != %x on %s\n",
4557 check_sum, __le32_to_cpu(super->anchor->check_sum),
4558 devname);
4559 return 3;
4560 }
4561
4562 return 0;
4563 }
4564
4565 static int read_imsm_migr_rec(int fd, struct intel_super *super);
4566
4567 /* clears hi bits in metadata if MPB_ATTRIB_2TB_DISK not set */
4568 static void clear_hi(struct intel_super *super)
4569 {
4570 struct imsm_super *mpb = super->anchor;
4571 int i, n;
4572 if (mpb->attributes & MPB_ATTRIB_2TB_DISK)
4573 return;
4574 for (i = 0; i < mpb->num_disks; ++i) {
4575 struct imsm_disk *disk = &mpb->disk[i];
4576 disk->total_blocks_hi = 0;
4577 }
4578 for (i = 0; i < mpb->num_raid_devs; ++i) {
4579 struct imsm_dev *dev = get_imsm_dev(super, i);
4580 for (n = 0; n < 2; ++n) {
4581 struct imsm_map *map = get_imsm_map(dev, n);
4582 if (!map)
4583 continue;
4584 map->pba_of_lba0_hi = 0;
4585 map->blocks_per_member_hi = 0;
4586 map->num_data_stripes_hi = 0;
4587 }
4588 }
4589 }
4590
4591 static int
4592 load_and_parse_mpb(int fd, struct intel_super *super, char *devname, int keep_fd)
4593 {
4594 int err;
4595
4596 err = load_imsm_mpb(fd, super, devname);
4597 if (err)
4598 return err;
4599 if (super->sector_size == 4096)
4600 convert_from_4k(super);
4601 err = load_imsm_disk(fd, super, devname, keep_fd);
4602 if (err)
4603 return err;
4604 err = parse_raid_devices(super);
4605 if (err)
4606 return err;
4607 err = load_bbm_log(super);
4608 clear_hi(super);
4609 return err;
4610 }
4611
4612 static void __free_imsm_disk(struct dl *d, int do_close)
4613 {
4614 if (do_close)
4615 close_fd(&d->fd);
4616 if (d->devname)
4617 free(d->devname);
4618 if (d->e)
4619 free(d->e);
4620 free(d);
4621
4622 }
4623
4624 static void free_imsm_disks(struct intel_super *super)
4625 {
4626 struct dl *d;
4627
4628 while (super->disks) {
4629 d = super->disks;
4630 super->disks = d->next;
4631 __free_imsm_disk(d, 1);
4632 }
4633 while (super->disk_mgmt_list) {
4634 d = super->disk_mgmt_list;
4635 super->disk_mgmt_list = d->next;
4636 __free_imsm_disk(d, 1);
4637 }
4638 while (super->missing) {
4639 d = super->missing;
4640 super->missing = d->next;
4641 __free_imsm_disk(d, 1);
4642 }
4643
4644 }
4645
4646 /* free all the pieces hanging off of a super pointer */
4647 static void __free_imsm(struct intel_super *super, int free_disks)
4648 {
4649 struct intel_hba *elem, *next;
4650
4651 if (super->buf) {
4652 free(super->buf);
4653 super->buf = NULL;
4654 }
4655 /* unlink capability description */
4656 super->orom = NULL;
4657 if (super->migr_rec_buf) {
4658 free(super->migr_rec_buf);
4659 super->migr_rec_buf = NULL;
4660 }
4661 if (free_disks)
4662 free_imsm_disks(super);
4663 free_devlist(super);
4664 elem = super->hba;
4665 while (elem) {
4666 if (elem->path)
4667 free((void *)elem->path);
4668 next = elem->next;
4669 free(elem);
4670 elem = next;
4671 }
4672 if (super->bbm_log)
4673 free(super->bbm_log);
4674 super->hba = NULL;
4675 }
4676
4677 static void free_imsm(struct intel_super *super)
4678 {
4679 __free_imsm(super, 1);
4680 free(super->bb.entries);
4681 free(super);
4682 }
4683
4684 static void free_super_imsm(struct supertype *st)
4685 {
4686 struct intel_super *super = st->sb;
4687
4688 if (!super)
4689 return;
4690
4691 free_imsm(super);
4692 st->sb = NULL;
4693 }
4694
4695 static struct intel_super *alloc_super(void)
4696 {
4697 struct intel_super *super = xcalloc(1, sizeof(*super));
4698
4699 super->current_vol = -1;
4700 super->create_offset = ~((unsigned long long) 0);
4701
4702 super->bb.entries = xmalloc(BBM_LOG_MAX_ENTRIES *
4703 sizeof(struct md_bb_entry));
4704 if (!super->bb.entries) {
4705 free(super);
4706 return NULL;
4707 }
4708
4709 return super;
4710 }
4711
4712 /*
4713 * find and allocate hba and OROM/EFI based on valid fd of RAID component device
4714 */
4715 static int find_intel_hba_capability(int fd, struct intel_super *super, char *devname)
4716 {
4717 struct sys_dev *hba_name;
4718 int rv = 0;
4719
4720 if (is_fd_valid(fd) && test_partition(fd)) {
4721 pr_err("imsm: %s is a partition, cannot be used in IMSM\n",
4722 devname);
4723 return 1;
4724 }
4725 if (!is_fd_valid(fd) || check_env("IMSM_NO_PLATFORM")) {
4726 super->orom = NULL;
4727 super->hba = NULL;
4728 return 0;
4729 }
4730 hba_name = find_disk_attached_hba(fd, NULL);
4731 if (!hba_name) {
4732 if (devname)
4733 pr_err("%s is not attached to Intel(R) RAID controller.\n",
4734 devname);
4735 return 1;
4736 }
4737 rv = attach_hba_to_super(super, hba_name);
4738 if (rv == 2) {
4739 if (devname) {
4740 struct intel_hba *hba = super->hba;
4741
4742 pr_err("%s is attached to Intel(R) %s %s (%s),\n"
4743 " but the container is assigned to Intel(R) %s %s (",
4744 devname,
4745 get_sys_dev_type(hba_name->type),
4746 hba_name->type == SYS_DEV_VMD ? "domain" : "RAID controller",
4747 hba_name->pci_id ? : "Err!",
4748 get_sys_dev_type(super->hba->type),
4749 hba->type == SYS_DEV_VMD ? "domain" : "RAID controller");
4750
4751 while (hba) {
4752 fprintf(stderr, "%s", hba->pci_id ? : "Err!");
4753 if (hba->next)
4754 fprintf(stderr, ", ");
4755 hba = hba->next;
4756 }
4757 fprintf(stderr, ").\n"
4758 " Mixing devices attached to different controllers is not allowed.\n");
4759 }
4760 return 2;
4761 }
4762 super->orom = find_imsm_capability(hba_name);
4763 if (!super->orom)
4764 return 3;
4765
4766 return 0;
4767 }
4768
4769 /* find_missing - helper routine for load_super_imsm_all that identifies
4770 * disks that have disappeared from the system. This routine relies on
4771 * the mpb being uptodate, which it is at load time.
4772 */
4773 static int find_missing(struct intel_super *super)
4774 {
4775 int i;
4776 struct imsm_super *mpb = super->anchor;
4777 struct dl *dl;
4778 struct imsm_disk *disk;
4779
4780 for (i = 0; i < mpb->num_disks; i++) {
4781 disk = __get_imsm_disk(mpb, i);
4782 dl = serial_to_dl(disk->serial, super);
4783 if (dl)
4784 continue;
4785
4786 dl = xmalloc(sizeof(*dl));
4787 dl->major = 0;
4788 dl->minor = 0;
4789 dl->fd = -1;
4790 dl->devname = xstrdup("missing");
4791 dl->index = i;
4792 serialcpy(dl->serial, disk->serial);
4793 dl->disk = *disk;
4794 dl->e = NULL;
4795 dl->next = super->missing;
4796 super->missing = dl;
4797 }
4798
4799 return 0;
4800 }
4801
4802 static struct intel_disk *disk_list_get(__u8 *serial, struct intel_disk *disk_list)
4803 {
4804 struct intel_disk *idisk = disk_list;
4805
4806 while (idisk) {
4807 if (serialcmp(idisk->disk.serial, serial) == 0)
4808 break;
4809 idisk = idisk->next;
4810 }
4811
4812 return idisk;
4813 }
4814
4815 static int __prep_thunderdome(struct intel_super **table, int tbl_size,
4816 struct intel_super *super,
4817 struct intel_disk **disk_list)
4818 {
4819 struct imsm_disk *d = &super->disks->disk;
4820 struct imsm_super *mpb = super->anchor;
4821 int i, j;
4822
4823 for (i = 0; i < tbl_size; i++) {
4824 struct imsm_super *tbl_mpb = table[i]->anchor;
4825 struct imsm_disk *tbl_d = &table[i]->disks->disk;
4826
4827 if (tbl_mpb->family_num == mpb->family_num) {
4828 if (tbl_mpb->check_sum == mpb->check_sum) {
4829 dprintf("mpb from %d:%d matches %d:%d\n",
4830 super->disks->major,
4831 super->disks->minor,
4832 table[i]->disks->major,
4833 table[i]->disks->minor);
4834 break;
4835 }
4836
4837 if (((is_configured(d) && !is_configured(tbl_d)) ||
4838 is_configured(d) == is_configured(tbl_d)) &&
4839 tbl_mpb->generation_num < mpb->generation_num) {
4840 /* current version of the mpb is a
4841 * better candidate than the one in
4842 * super_table, but copy over "cross
4843 * generational" status
4844 */
4845 struct intel_disk *idisk;
4846
4847 dprintf("mpb from %d:%d replaces %d:%d\n",
4848 super->disks->major,
4849 super->disks->minor,
4850 table[i]->disks->major,
4851 table[i]->disks->minor);
4852
4853 idisk = disk_list_get(tbl_d->serial, *disk_list);
4854 if (idisk && is_failed(&idisk->disk))
4855 tbl_d->status |= FAILED_DISK;
4856 break;
4857 } else {
4858 struct intel_disk *idisk;
4859 struct imsm_disk *disk;
4860
4861 /* tbl_mpb is more up to date, but copy
4862 * over cross generational status before
4863 * returning
4864 */
4865 disk = __serial_to_disk(d->serial, mpb, NULL);
4866 if (disk && is_failed(disk))
4867 d->status |= FAILED_DISK;
4868
4869 idisk = disk_list_get(d->serial, *disk_list);
4870 if (idisk) {
4871 idisk->owner = i;
4872 if (disk && is_configured(disk))
4873 idisk->disk.status |= CONFIGURED_DISK;
4874 }
4875
4876 dprintf("mpb from %d:%d prefer %d:%d\n",
4877 super->disks->major,
4878 super->disks->minor,
4879 table[i]->disks->major,
4880 table[i]->disks->minor);
4881
4882 return tbl_size;
4883 }
4884 }
4885 }
4886
4887 if (i >= tbl_size)
4888 table[tbl_size++] = super;
4889 else
4890 table[i] = super;
4891
4892 /* update/extend the merged list of imsm_disk records */
4893 for (j = 0; j < mpb->num_disks; j++) {
4894 struct imsm_disk *disk = __get_imsm_disk(mpb, j);
4895 struct intel_disk *idisk;
4896
4897 idisk = disk_list_get(disk->serial, *disk_list);
4898 if (idisk) {
4899 idisk->disk.status |= disk->status;
4900 if (is_configured(&idisk->disk) ||
4901 is_failed(&idisk->disk))
4902 idisk->disk.status &= ~(SPARE_DISK);
4903 } else {
4904 idisk = xcalloc(1, sizeof(*idisk));
4905 idisk->owner = IMSM_UNKNOWN_OWNER;
4906 idisk->disk = *disk;
4907 idisk->next = *disk_list;
4908 *disk_list = idisk;
4909 }
4910
4911 if (serialcmp(idisk->disk.serial, d->serial) == 0)
4912 idisk->owner = i;
4913 }
4914
4915 return tbl_size;
4916 }
4917
4918 static struct intel_super *
4919 validate_members(struct intel_super *super, struct intel_disk *disk_list,
4920 const int owner)
4921 {
4922 struct imsm_super *mpb = super->anchor;
4923 int ok_count = 0;
4924 int i;
4925
4926 for (i = 0; i < mpb->num_disks; i++) {
4927 struct imsm_disk *disk = __get_imsm_disk(mpb, i);
4928 struct intel_disk *idisk;
4929
4930 idisk = disk_list_get(disk->serial, disk_list);
4931 if (idisk) {
4932 if (idisk->owner == owner ||
4933 idisk->owner == IMSM_UNKNOWN_OWNER)
4934 ok_count++;
4935 else
4936 dprintf("'%.16s' owner %d != %d\n",
4937 disk->serial, idisk->owner,
4938 owner);
4939 } else {
4940 dprintf("unknown disk %x [%d]: %.16s\n",
4941 __le32_to_cpu(mpb->family_num), i,
4942 disk->serial);
4943 break;
4944 }
4945 }
4946
4947 if (ok_count == mpb->num_disks)
4948 return super;
4949 return NULL;
4950 }
4951
4952 static void show_conflicts(__u32 family_num, struct intel_super *super_list)
4953 {
4954 struct intel_super *s;
4955
4956 for (s = super_list; s; s = s->next) {
4957 if (family_num != s->anchor->family_num)
4958 continue;
4959 pr_err("Conflict, offlining family %#x on '%s'\n",
4960 __le32_to_cpu(family_num), s->disks->devname);
4961 }
4962 }
4963
4964 static struct intel_super *
4965 imsm_thunderdome(struct intel_super **super_list, int len)
4966 {
4967 struct intel_super *super_table[len];
4968 struct intel_disk *disk_list = NULL;
4969 struct intel_super *champion, *spare;
4970 struct intel_super *s, **del;
4971 int tbl_size = 0;
4972 int conflict;
4973 int i;
4974
4975 memset(super_table, 0, sizeof(super_table));
4976 for (s = *super_list; s; s = s->next)
4977 tbl_size = __prep_thunderdome(super_table, tbl_size, s, &disk_list);
4978
4979 for (i = 0; i < tbl_size; i++) {
4980 struct imsm_disk *d;
4981 struct intel_disk *idisk;
4982 struct imsm_super *mpb = super_table[i]->anchor;
4983
4984 s = super_table[i];
4985 d = &s->disks->disk;
4986
4987 /* 'd' must appear in merged disk list for its
4988 * configuration to be valid
4989 */
4990 idisk = disk_list_get(d->serial, disk_list);
4991 if (idisk && idisk->owner == i)
4992 s = validate_members(s, disk_list, i);
4993 else
4994 s = NULL;
4995
4996 if (!s)
4997 dprintf("marking family: %#x from %d:%d offline\n",
4998 mpb->family_num,
4999 super_table[i]->disks->major,
5000 super_table[i]->disks->minor);
5001 super_table[i] = s;
5002 }
5003
5004 /* This is where the mdadm implementation differs from the Windows
5005 * driver which has no strict concept of a container. We can only
5006 * assemble one family from a container, so when returning a prodigal
5007 * array member to this system the code will not be able to disambiguate
5008 * the container contents that should be assembled ("foreign" versus
5009 * "local"). It requires user intervention to set the orig_family_num
5010 * to a new value to establish a new container. The Windows driver in
5011 * this situation fixes up the volume name in place and manages the
5012 * foreign array as an independent entity.
5013 */
5014 s = NULL;
5015 spare = NULL;
5016 conflict = 0;
5017 for (i = 0; i < tbl_size; i++) {
5018 struct intel_super *tbl_ent = super_table[i];
5019 int is_spare = 0;
5020
5021 if (!tbl_ent)
5022 continue;
5023
5024 if (tbl_ent->anchor->num_raid_devs == 0) {
5025 spare = tbl_ent;
5026 is_spare = 1;
5027 }
5028
5029 if (s && !is_spare) {
5030 show_conflicts(tbl_ent->anchor->family_num, *super_list);
5031 conflict++;
5032 } else if (!s && !is_spare)
5033 s = tbl_ent;
5034 }
5035
5036 if (!s)
5037 s = spare;
5038 if (!s) {
5039 champion = NULL;
5040 goto out;
5041 }
5042 champion = s;
5043
5044 if (conflict)
5045 pr_err("Chose family %#x on '%s', assemble conflicts to new container with '--update=uuid'\n",
5046 __le32_to_cpu(s->anchor->family_num), s->disks->devname);
5047
5048 /* collect all dl's onto 'champion', and update them to
5049 * champion's version of the status
5050 */
5051 for (s = *super_list; s; s = s->next) {
5052 struct imsm_super *mpb = champion->anchor;
5053 struct dl *dl = s->disks;
5054
5055 if (s == champion)
5056 continue;
5057
5058 mpb->attributes |= s->anchor->attributes & MPB_ATTRIB_2TB_DISK;
5059
5060 for (i = 0; i < mpb->num_disks; i++) {
5061 struct imsm_disk *disk;
5062
5063 disk = __serial_to_disk(dl->serial, mpb, &dl->index);
5064 if (disk) {
5065 dl->disk = *disk;
5066 /* only set index on disks that are a member of
5067 * a populated contianer, i.e. one with
5068 * raid_devs
5069 */
5070 if (is_failed(&dl->disk))
5071 dl->index = -2;
5072 else if (is_spare(&dl->disk))
5073 dl->index = -1;
5074 break;
5075 }
5076 }
5077
5078 if (i >= mpb->num_disks) {
5079 struct intel_disk *idisk;
5080
5081 idisk = disk_list_get(dl->serial, disk_list);
5082 if (idisk && is_spare(&idisk->disk) &&
5083 !is_failed(&idisk->disk) && !is_configured(&idisk->disk))
5084 dl->index = -1;
5085 else {
5086 dl->index = -2;
5087 continue;
5088 }
5089 }
5090
5091 dl->next = champion->disks;
5092 champion->disks = dl;
5093 s->disks = NULL;
5094 }
5095
5096 /* delete 'champion' from super_list */
5097 for (del = super_list; *del; ) {
5098 if (*del == champion) {
5099 *del = (*del)->next;
5100 break;
5101 } else
5102 del = &(*del)->next;
5103 }
5104 champion->next = NULL;
5105
5106 out:
5107 while (disk_list) {
5108 struct intel_disk *idisk = disk_list;
5109
5110 disk_list = disk_list->next;
5111 free(idisk);
5112 }
5113
5114 return champion;
5115 }
5116
5117 static int
5118 get_sra_super_block(int fd, struct intel_super **super_list, char *devname, int *max, int keep_fd);
5119 static int get_super_block(struct intel_super **super_list, char *devnm, char *devname,
5120 int major, int minor, int keep_fd);
5121 static int
5122 get_devlist_super_block(struct md_list *devlist, struct intel_super **super_list,
5123 int *max, int keep_fd);
5124
5125 static int load_super_imsm_all(struct supertype *st, int fd, void **sbp,
5126 char *devname, struct md_list *devlist,
5127 int keep_fd)
5128 {
5129 struct intel_super *super_list = NULL;
5130 struct intel_super *super = NULL;
5131 int err = 0;
5132 int i = 0;
5133
5134 if (is_fd_valid(fd))
5135 /* 'fd' is an opened container */
5136 err = get_sra_super_block(fd, &super_list, devname, &i, keep_fd);
5137 else
5138 /* get super block from devlist devices */
5139 err = get_devlist_super_block(devlist, &super_list, &i, keep_fd);
5140 if (err)
5141 goto error;
5142 /* all mpbs enter, maybe one leaves */
5143 super = imsm_thunderdome(&super_list, i);
5144 if (!super) {
5145 err = 1;
5146 goto error;
5147 }
5148
5149 if (find_missing(super) != 0) {
5150 free_imsm(super);
5151 err = 2;
5152 goto error;
5153 }
5154
5155 /* load migration record */
5156 err = load_imsm_migr_rec(super);
5157 if (err == -1) {
5158 /* migration is in progress,
5159 * but migr_rec cannot be loaded,
5160 */
5161 err = 4;
5162 goto error;
5163 }
5164
5165 /* Check migration compatibility */
5166 if (err == 0 && check_mpb_migr_compatibility(super) != 0) {
5167 pr_err("Unsupported migration detected");
5168 if (devname)
5169 fprintf(stderr, " on %s\n", devname);
5170 else
5171 fprintf(stderr, " (IMSM).\n");
5172
5173 err = 5;
5174 goto error;
5175 }
5176
5177 err = 0;
5178
5179 error:
5180 while (super_list) {
5181 struct intel_super *s = super_list;
5182
5183 super_list = super_list->next;
5184 free_imsm(s);
5185 }
5186
5187 if (err)
5188 return err;
5189
5190 *sbp = super;
5191 if (is_fd_valid(fd))
5192 strcpy(st->container_devnm, fd2devnm(fd));
5193 else
5194 st->container_devnm[0] = 0;
5195 if (err == 0 && st->ss == NULL) {
5196 st->ss = &super_imsm;
5197 st->minor_version = 0;
5198 st->max_devs = IMSM_MAX_DEVICES;
5199 }
5200 return 0;
5201 }
5202
5203 static int
5204 get_devlist_super_block(struct md_list *devlist, struct intel_super **super_list,
5205 int *max, int keep_fd)
5206 {
5207 struct md_list *tmpdev;
5208 int err = 0;
5209 int i = 0;
5210
5211 for (i = 0, tmpdev = devlist; tmpdev; tmpdev = tmpdev->next) {
5212 if (tmpdev->used != 1)
5213 continue;
5214 if (tmpdev->container == 1) {
5215 int lmax = 0;
5216 int fd = dev_open(tmpdev->devname, O_RDONLY|O_EXCL);
5217 if (!is_fd_valid(fd)) {
5218 pr_err("cannot open device %s: %s\n",
5219 tmpdev->devname, strerror(errno));
5220 err = 8;
5221 goto error;
5222 }
5223 err = get_sra_super_block(fd, super_list,
5224 tmpdev->devname, &lmax,
5225 keep_fd);
5226 i += lmax;
5227 close(fd);
5228 if (err) {
5229 err = 7;
5230 goto error;
5231 }
5232 } else {
5233 int major = major(tmpdev->st_rdev);
5234 int minor = minor(tmpdev->st_rdev);
5235 err = get_super_block(super_list,
5236 NULL,
5237 tmpdev->devname,
5238 major, minor,
5239 keep_fd);
5240 i++;
5241 if (err) {
5242 err = 6;
5243 goto error;
5244 }
5245 }
5246 }
5247 error:
5248 *max = i;
5249 return err;
5250 }
5251
5252 static int get_super_block(struct intel_super **super_list, char *devnm, char *devname,
5253 int major, int minor, int keep_fd)
5254 {
5255 struct intel_super *s;
5256 char nm[32];
5257 int dfd = -1;
5258 int err = 0;
5259 int retry;
5260
5261 s = alloc_super();
5262 if (!s) {
5263 err = 1;
5264 goto error;
5265 }
5266
5267 sprintf(nm, "%d:%d", major, minor);
5268 dfd = dev_open(nm, O_RDWR);
5269 if (!is_fd_valid(dfd)) {
5270 err = 2;
5271 goto error;
5272 }
5273
5274 if (!get_dev_sector_size(dfd, NULL, &s->sector_size)) {
5275 err = 2;
5276 goto error;
5277 }
5278 find_intel_hba_capability(dfd, s, devname);
5279 err = load_and_parse_mpb(dfd, s, NULL, keep_fd);
5280
5281 /* retry the load if we might have raced against mdmon */
5282 if (err == 3 && devnm && mdmon_running(devnm))
5283 for (retry = 0; retry < 3; retry++) {
5284 sleep_for(0, MSEC_TO_NSEC(3), true);
5285 err = load_and_parse_mpb(dfd, s, NULL, keep_fd);
5286 if (err != 3)
5287 break;
5288 }
5289 error:
5290 if (!err) {
5291 s->next = *super_list;
5292 *super_list = s;
5293 } else {
5294 if (s)
5295 free_imsm(s);
5296 close_fd(&dfd);
5297 }
5298 if (!keep_fd)
5299 close_fd(&dfd);
5300 return err;
5301
5302 }
5303
5304 static int
5305 get_sra_super_block(int fd, struct intel_super **super_list, char *devname, int *max, int keep_fd)
5306 {
5307 struct mdinfo *sra;
5308 char *devnm;
5309 struct mdinfo *sd;
5310 int err = 0;
5311 int i = 0;
5312 sra = sysfs_read(fd, NULL, GET_LEVEL|GET_VERSION|GET_DEVS|GET_STATE);
5313 if (!sra)
5314 return 1;
5315
5316 if (sra->array.major_version != -1 ||
5317 sra->array.minor_version != -2 ||
5318 strcmp(sra->text_version, "imsm") != 0) {
5319 err = 1;
5320 goto error;
5321 }
5322 /* load all mpbs */
5323 devnm = fd2devnm(fd);
5324 for (sd = sra->devs, i = 0; sd; sd = sd->next, i++) {
5325 if (get_super_block(super_list, devnm, devname,
5326 sd->disk.major, sd->disk.minor, keep_fd) != 0) {
5327 err = 7;
5328 goto error;
5329 }
5330 }
5331 error:
5332 sysfs_free(sra);
5333 *max = i;
5334 return err;
5335 }
5336
5337 static int load_container_imsm(struct supertype *st, int fd, char *devname)
5338 {
5339 return load_super_imsm_all(st, fd, &st->sb, devname, NULL, 1);
5340 }
5341
5342 static int load_super_imsm(struct supertype *st, int fd, char *devname)
5343 {
5344 struct intel_super *super;
5345 int rv;
5346 int retry;
5347
5348 if (test_partition(fd))
5349 /* IMSM not allowed on partitions */
5350 return 1;
5351
5352 free_super_imsm(st);
5353
5354 super = alloc_super();
5355 if (!super)
5356 return 1;
5357
5358 if (!get_dev_sector_size(fd, NULL, &super->sector_size)) {
5359 free_imsm(super);
5360 return 1;
5361 }
5362 /* Load hba and capabilities if they exist.
5363 * But do not preclude loading metadata in case capabilities or hba are
5364 * non-compliant and ignore_hw_compat is set.
5365 */
5366 rv = find_intel_hba_capability(fd, super, devname);
5367 /* no orom/efi or non-intel hba of the disk */
5368 if (rv != 0 && st->ignore_hw_compat == 0) {
5369 if (devname)
5370 pr_err("No OROM/EFI properties for %s\n", devname);
5371 free_imsm(super);
5372 return 2;
5373 }
5374 rv = load_and_parse_mpb(fd, super, devname, 0);
5375
5376 /* retry the load if we might have raced against mdmon */
5377 if (rv == 3) {
5378 struct mdstat_ent *mdstat = NULL;
5379 char *name = fd2kname(fd);
5380
5381 if (name)
5382 mdstat = mdstat_by_component(name);
5383
5384 if (mdstat && mdmon_running(mdstat->devnm) && getpid() != mdmon_pid(mdstat->devnm)) {
5385 for (retry = 0; retry < 3; retry++) {
5386 sleep_for(0, MSEC_TO_NSEC(3), true);
5387 rv = load_and_parse_mpb(fd, super, devname, 0);
5388 if (rv != 3)
5389 break;
5390 }
5391 }
5392
5393 free_mdstat(mdstat);
5394 }
5395
5396 if (rv) {
5397 if (devname)
5398 pr_err("Failed to load all information sections on %s\n", devname);
5399 free_imsm(super);
5400 return rv;
5401 }
5402
5403 st->sb = super;
5404 if (st->ss == NULL) {
5405 st->ss = &super_imsm;
5406 st->minor_version = 0;
5407 st->max_devs = IMSM_MAX_DEVICES;
5408 }
5409
5410 /* load migration record */
5411 if (load_imsm_migr_rec(super) == 0) {
5412 /* Check for unsupported migration features */
5413 if (check_mpb_migr_compatibility(super) != 0) {
5414 pr_err("Unsupported migration detected");
5415 if (devname)
5416 fprintf(stderr, " on %s\n", devname);
5417 else
5418 fprintf(stderr, " (IMSM).\n");
5419 return 3;
5420 }
5421 }
5422
5423 return 0;
5424 }
5425
5426 static __u16 info_to_blocks_per_strip(mdu_array_info_t *info)
5427 {
5428 if (info->level == 1)
5429 return 128;
5430 return info->chunk_size >> 9;
5431 }
5432
5433 static unsigned long long info_to_blocks_per_member(mdu_array_info_t *info,
5434 unsigned long long size)
5435 {
5436 if (info->level == 1)
5437 return size * 2;
5438 else
5439 return (size * 2) & ~(info_to_blocks_per_strip(info) - 1);
5440 }
5441
5442 static void imsm_update_version_info(struct intel_super *super)
5443 {
5444 /* update the version and attributes */
5445 struct imsm_super *mpb = super->anchor;
5446 char *version;
5447 struct imsm_dev *dev;
5448 struct imsm_map *map;
5449 int i;
5450
5451 for (i = 0; i < mpb->num_raid_devs; i++) {
5452 dev = get_imsm_dev(super, i);
5453 map = get_imsm_map(dev, MAP_0);
5454 if (__le32_to_cpu(dev->size_high) > 0)
5455 mpb->attributes |= MPB_ATTRIB_2TB;
5456
5457 /* FIXME detect when an array spans a port multiplier */
5458 #if 0
5459 mpb->attributes |= MPB_ATTRIB_PM;
5460 #endif
5461
5462 if (mpb->num_raid_devs > 1 ||
5463 mpb->attributes != MPB_ATTRIB_CHECKSUM_VERIFY) {
5464 version = MPB_VERSION_ATTRIBS;
5465 switch (get_imsm_raid_level(map)) {
5466 case 0: mpb->attributes |= MPB_ATTRIB_RAID0; break;
5467 case 1: mpb->attributes |= MPB_ATTRIB_RAID1; break;
5468 case 10: mpb->attributes |= MPB_ATTRIB_RAID10; break;
5469 case 5: mpb->attributes |= MPB_ATTRIB_RAID5; break;
5470 }
5471 } else {
5472 if (map->num_members >= 5)
5473 version = MPB_VERSION_5OR6_DISK_ARRAY;
5474 else if (dev->status == DEV_CLONE_N_GO)
5475 version = MPB_VERSION_CNG;
5476 else if (get_imsm_raid_level(map) == 5)
5477 version = MPB_VERSION_RAID5;
5478 else if (map->num_members >= 3)
5479 version = MPB_VERSION_3OR4_DISK_ARRAY;
5480 else if (get_imsm_raid_level(map) == 1)
5481 version = MPB_VERSION_RAID1;
5482 else
5483 version = MPB_VERSION_RAID0;
5484 }
5485 strcpy(((char *) mpb->sig) + strlen(MPB_SIGNATURE), version);
5486 }
5487 }
5488
5489 static int check_name(struct intel_super *super, char *name, int quiet)
5490 {
5491 struct imsm_super *mpb = super->anchor;
5492 char *reason = NULL;
5493 char *start = name;
5494 size_t len = strlen(name);
5495 int i;
5496
5497 if (len > 0) {
5498 while (isspace(start[len - 1]))
5499 start[--len] = 0;
5500 while (*start && isspace(*start))
5501 ++start, --len;
5502 memmove(name, start, len + 1);
5503 }
5504
5505 if (len > MAX_RAID_SERIAL_LEN)
5506 reason = "must be 16 characters or less";
5507 else if (len == 0)
5508 reason = "must be a non-empty string";
5509
5510 for (i = 0; i < mpb->num_raid_devs; i++) {
5511 struct imsm_dev *dev = get_imsm_dev(super, i);
5512
5513 if (strncmp((char *) dev->volume, name, MAX_RAID_SERIAL_LEN) == 0) {
5514 reason = "already exists";
5515 break;
5516 }
5517 }
5518
5519 if (reason && !quiet)
5520 pr_err("imsm volume name %s\n", reason);
5521
5522 return !reason;
5523 }
5524
5525 static int init_super_imsm_volume(struct supertype *st, mdu_array_info_t *info,
5526 struct shape *s, char *name,
5527 char *homehost, int *uuid,
5528 long long data_offset)
5529 {
5530 /* We are creating a volume inside a pre-existing container.
5531 * so st->sb is already set.
5532 */
5533 struct intel_super *super = st->sb;
5534 unsigned int sector_size = super->sector_size;
5535 struct imsm_super *mpb = super->anchor;
5536 struct intel_dev *dv;
5537 struct imsm_dev *dev;
5538 struct imsm_vol *vol;
5539 struct imsm_map *map;
5540 int idx = mpb->num_raid_devs;
5541 int i;
5542 int namelen;
5543 unsigned long long array_blocks;
5544 size_t size_old, size_new;
5545 unsigned int data_disks;
5546 unsigned long long size_per_member;
5547
5548 if (super->orom && mpb->num_raid_devs >= super->orom->vpa) {
5549 pr_err("This imsm-container already has the maximum of %d volumes\n", super->orom->vpa);
5550 return 0;
5551 }
5552
5553 /* ensure the mpb is large enough for the new data */
5554 size_old = __le32_to_cpu(mpb->mpb_size);
5555 size_new = disks_to_mpb_size(info->nr_disks);
5556 if (size_new > size_old) {
5557 void *mpb_new;
5558 size_t size_round = ROUND_UP(size_new, sector_size);
5559
5560 if (posix_memalign(&mpb_new, sector_size, size_round) != 0) {
5561 pr_err("could not allocate new mpb\n");
5562 return 0;
5563 }
5564 if (posix_memalign(&super->migr_rec_buf, MAX_SECTOR_SIZE,
5565 MIGR_REC_BUF_SECTORS*
5566 MAX_SECTOR_SIZE) != 0) {
5567 pr_err("could not allocate migr_rec buffer\n");
5568 free(super->buf);
5569 free(super);
5570 free(mpb_new);
5571 return 0;
5572 }
5573 memcpy(mpb_new, mpb, size_old);
5574 free(mpb);
5575 mpb = mpb_new;
5576 super->anchor = mpb_new;
5577 mpb->mpb_size = __cpu_to_le32(size_new);
5578 memset(mpb_new + size_old, 0, size_round - size_old);
5579 super->len = size_round;
5580 }
5581 super->current_vol = idx;
5582
5583 /* handle 'failed_disks' by either:
5584 * a) create dummy disk entries in the table if this the first
5585 * volume in the array. We add them here as this is the only
5586 * opportunity to add them. add_to_super_imsm_volume()
5587 * handles the non-failed disks and continues incrementing
5588 * mpb->num_disks.
5589 * b) validate that 'failed_disks' matches the current number
5590 * of missing disks if the container is populated
5591 */
5592 if (super->current_vol == 0) {
5593 mpb->num_disks = 0;
5594 for (i = 0; i < info->failed_disks; i++) {
5595 struct imsm_disk *disk;
5596
5597 mpb->num_disks++;
5598 disk = __get_imsm_disk(mpb, i);
5599 disk->status = CONFIGURED_DISK | FAILED_DISK;
5600 disk->scsi_id = __cpu_to_le32(~(__u32)0);
5601 snprintf((char *) disk->serial, MAX_RAID_SERIAL_LEN,
5602 "missing:%d", (__u8)i);
5603 }
5604 find_missing(super);
5605 } else {
5606 int missing = 0;
5607 struct dl *d;
5608
5609 for (d = super->missing; d; d = d->next)
5610 missing++;
5611 if (info->failed_disks > missing) {
5612 pr_err("unable to add 'missing' disk to container\n");
5613 return 0;
5614 }
5615 }
5616
5617 if (!check_name(super, name, 0))
5618 return 0;
5619 dv = xmalloc(sizeof(*dv));
5620 dev = xcalloc(1, sizeof(*dev) + sizeof(__u32) * (info->raid_disks - 1));
5621 /*
5622 * Explicitly allow truncating to not confuse gcc's
5623 * -Werror=stringop-truncation
5624 */
5625 namelen = min((int) strlen(name), MAX_RAID_SERIAL_LEN);
5626 memcpy(dev->volume, name, namelen);
5627 array_blocks = calc_array_size(info->level, info->raid_disks,
5628 info->layout, info->chunk_size,
5629 s->size * BLOCKS_PER_KB);
5630 data_disks = get_data_disks(info->level, info->layout,
5631 info->raid_disks);
5632 array_blocks = round_size_to_mb(array_blocks, data_disks);
5633 size_per_member = array_blocks / data_disks;
5634
5635 set_imsm_dev_size(dev, array_blocks);
5636 dev->status = (DEV_READ_COALESCING | DEV_WRITE_COALESCING);
5637 vol = &dev->vol;
5638 vol->migr_state = 0;
5639 set_migr_type(dev, MIGR_INIT);
5640 vol->dirty = !info->state;
5641 set_vol_curr_migr_unit(dev, 0);
5642 map = get_imsm_map(dev, MAP_0);
5643 set_pba_of_lba0(map, super->create_offset);
5644 map->blocks_per_strip = __cpu_to_le16(info_to_blocks_per_strip(info));
5645 map->failed_disk_num = ~0;
5646 if (info->level > 0)
5647 map->map_state = (info->state ? IMSM_T_STATE_NORMAL
5648 : IMSM_T_STATE_UNINITIALIZED);
5649 else
5650 map->map_state = info->failed_disks ? IMSM_T_STATE_FAILED :
5651 IMSM_T_STATE_NORMAL;
5652 map->ddf = 1;
5653
5654 if (info->level == 1 && info->raid_disks > 2) {
5655 free(dev);
5656 free(dv);
5657 pr_err("imsm does not support more than 2 disksin a raid1 volume\n");
5658 return 0;
5659 }
5660
5661 map->raid_level = info->level;
5662 if (info->level == 10)
5663 map->raid_level = 1;
5664 set_num_domains(map);
5665
5666 size_per_member += NUM_BLOCKS_DIRTY_STRIPE_REGION;
5667 set_blocks_per_member(map, info_to_blocks_per_member(info,
5668 size_per_member /
5669 BLOCKS_PER_KB));
5670
5671 map->num_members = info->raid_disks;
5672 update_num_data_stripes(map, array_blocks);
5673 for (i = 0; i < map->num_members; i++) {
5674 /* initialized in add_to_super */
5675 set_imsm_ord_tbl_ent(map, i, IMSM_ORD_REBUILD);
5676 }
5677 mpb->num_raid_devs++;
5678 mpb->num_raid_devs_created++;
5679 dev->my_vol_raid_dev_num = mpb->num_raid_devs_created;
5680
5681 if (s->consistency_policy <= CONSISTENCY_POLICY_RESYNC) {
5682 dev->rwh_policy = RWH_MULTIPLE_OFF;
5683 } else if (s->consistency_policy == CONSISTENCY_POLICY_PPL) {
5684 dev->rwh_policy = RWH_MULTIPLE_DISTRIBUTED;
5685 } else {
5686 free(dev);
5687 free(dv);
5688 pr_err("imsm does not support consistency policy %s\n",
5689 map_num_s(consistency_policies, s->consistency_policy));
5690 return 0;
5691 }
5692
5693 dv->dev = dev;
5694 dv->index = super->current_vol;
5695 dv->next = super->devlist;
5696 super->devlist = dv;
5697
5698 imsm_update_version_info(super);
5699
5700 return 1;
5701 }
5702
5703 static int init_super_imsm(struct supertype *st, mdu_array_info_t *info,
5704 struct shape *s, char *name,
5705 char *homehost, int *uuid,
5706 unsigned long long data_offset)
5707 {
5708 /* This is primarily called by Create when creating a new array.
5709 * We will then get add_to_super called for each component, and then
5710 * write_init_super called to write it out to each device.
5711 * For IMSM, Create can create on fresh devices or on a pre-existing
5712 * array.
5713 * To create on a pre-existing array a different method will be called.
5714 * This one is just for fresh drives.
5715 */
5716 struct intel_super *super;
5717 struct imsm_super *mpb;
5718 size_t mpb_size;
5719 char *version;
5720
5721 if (data_offset != INVALID_SECTORS) {
5722 pr_err("data-offset not supported by imsm\n");
5723 return 0;
5724 }
5725
5726 if (st->sb)
5727 return init_super_imsm_volume(st, info, s, name, homehost, uuid,
5728 data_offset);
5729
5730 if (info)
5731 mpb_size = disks_to_mpb_size(info->nr_disks);
5732 else
5733 mpb_size = MAX_SECTOR_SIZE;
5734
5735 super = alloc_super();
5736 if (super &&
5737 posix_memalign(&super->buf, MAX_SECTOR_SIZE, mpb_size) != 0) {
5738 free_imsm(super);
5739 super = NULL;
5740 }
5741 if (!super) {
5742 pr_err("could not allocate superblock\n");
5743 return 0;
5744 }
5745 if (posix_memalign(&super->migr_rec_buf, MAX_SECTOR_SIZE,
5746 MIGR_REC_BUF_SECTORS*MAX_SECTOR_SIZE) != 0) {
5747 pr_err("could not allocate migr_rec buffer\n");
5748 free(super->buf);
5749 free_imsm(super);
5750 return 0;
5751 }
5752 memset(super->buf, 0, mpb_size);
5753 mpb = super->buf;
5754 mpb->mpb_size = __cpu_to_le32(mpb_size);
5755 st->sb = super;
5756
5757 if (info == NULL) {
5758 /* zeroing superblock */
5759 return 0;
5760 }
5761
5762 mpb->attributes = MPB_ATTRIB_CHECKSUM_VERIFY;
5763
5764 version = (char *) mpb->sig;
5765 strcpy(version, MPB_SIGNATURE);
5766 version += strlen(MPB_SIGNATURE);
5767 strcpy(version, MPB_VERSION_RAID0);
5768
5769 return 1;
5770 }
5771
5772 static int drive_validate_sector_size(struct intel_super *super, struct dl *dl)
5773 {
5774 unsigned int member_sector_size;
5775
5776 if (!is_fd_valid(dl->fd)) {
5777 pr_err("Invalid file descriptor for %s\n", dl->devname);
5778 return 0;
5779 }
5780
5781 if (!get_dev_sector_size(dl->fd, dl->devname, &member_sector_size))
5782 return 0;
5783 if (member_sector_size != super->sector_size)
5784 return 0;
5785 return 1;
5786 }
5787
5788 static int add_to_super_imsm_volume(struct supertype *st, mdu_disk_info_t *dk,
5789 int fd, char *devname)
5790 {
5791 struct intel_super *super = st->sb;
5792 struct imsm_super *mpb = super->anchor;
5793 struct imsm_disk *_disk;
5794 struct imsm_dev *dev;
5795 struct imsm_map *map;
5796 struct dl *dl, *df;
5797 int slot;
5798 int autolayout = 0;
5799
5800 if (!is_fd_valid(fd))
5801 autolayout = 1;
5802
5803 dev = get_imsm_dev(super, super->current_vol);
5804 map = get_imsm_map(dev, MAP_0);
5805
5806 if (! (dk->state & (1<<MD_DISK_SYNC))) {
5807 pr_err("%s: Cannot add spare devices to IMSM volume\n",
5808 devname);
5809 return 1;
5810 }
5811
5812 for (dl = super->disks; dl ; dl = dl->next) {
5813 if (autolayout) {
5814 if (dl->raiddisk == dk->raid_disk)
5815 break;
5816 } else if (dl->major == dk->major && dl->minor == dk->minor)
5817 break;
5818 }
5819
5820 if (!dl) {
5821 if (!autolayout)
5822 pr_err("%s is not a member of the same container.\n",
5823 devname);
5824 return 1;
5825 }
5826
5827 if (!autolayout && super->current_vol > 0) {
5828 int _slot = get_disk_slot_in_dev(super, 0, dl->index);
5829
5830 if (_slot != dk->raid_disk) {
5831 pr_err("Member %s is in %d slot for the first volume, but is in %d slot for a new volume.\n",
5832 dl->devname, _slot, dk->raid_disk);
5833 pr_err("Raid members are in different order than for the first volume, aborting.\n");
5834 return 1;
5835 }
5836 }
5837
5838 if (mpb->num_disks == 0)
5839 if (!get_dev_sector_size(dl->fd, dl->devname,
5840 &super->sector_size))
5841 return 1;
5842
5843 if (!drive_validate_sector_size(super, dl)) {
5844 pr_err("Combining drives of different sector size in one volume is not allowed\n");
5845 return 1;
5846 }
5847
5848 /* add a pristine spare to the metadata */
5849 if (dl->index < 0) {
5850 dl->index = super->anchor->num_disks;
5851 super->anchor->num_disks++;
5852 }
5853 /* Check the device has not already been added */
5854 slot = get_imsm_disk_slot(map, dl->index);
5855 if (slot >= 0 &&
5856 (get_imsm_ord_tbl_ent(dev, slot, MAP_X) & IMSM_ORD_REBUILD) == 0) {
5857 pr_err("%s has been included in this array twice\n",
5858 devname);
5859 return 1;
5860 }
5861 set_imsm_ord_tbl_ent(map, dk->raid_disk, dl->index);
5862 dl->disk.status = CONFIGURED_DISK;
5863
5864 /* update size of 'missing' disks to be at least as large as the
5865 * largest acitve member (we only have dummy missing disks when
5866 * creating the first volume)
5867 */
5868 if (super->current_vol == 0) {
5869 for (df = super->missing; df; df = df->next) {
5870 if (total_blocks(&dl->disk) > total_blocks(&df->disk))
5871 set_total_blocks(&df->disk, total_blocks(&dl->disk));
5872 _disk = __get_imsm_disk(mpb, df->index);
5873 *_disk = df->disk;
5874 }
5875 }
5876
5877 /* refresh unset/failed slots to point to valid 'missing' entries */
5878 for (df = super->missing; df; df = df->next)
5879 for (slot = 0; slot < mpb->num_disks; slot++) {
5880 __u32 ord = get_imsm_ord_tbl_ent(dev, slot, MAP_X);
5881
5882 if ((ord & IMSM_ORD_REBUILD) == 0)
5883 continue;
5884 set_imsm_ord_tbl_ent(map, slot, df->index | IMSM_ORD_REBUILD);
5885 if (is_gen_migration(dev)) {
5886 struct imsm_map *map2 = get_imsm_map(dev,
5887 MAP_1);
5888 int slot2 = get_imsm_disk_slot(map2, df->index);
5889 if (slot2 < map2->num_members && slot2 >= 0) {
5890 __u32 ord2 = get_imsm_ord_tbl_ent(dev,
5891 slot2,
5892 MAP_1);
5893 if ((unsigned)df->index ==
5894 ord_to_idx(ord2))
5895 set_imsm_ord_tbl_ent(map2,
5896 slot2,
5897 df->index |
5898 IMSM_ORD_REBUILD);
5899 }
5900 }
5901 dprintf("set slot:%d to missing disk:%d\n", slot, df->index);
5902 break;
5903 }
5904
5905 /* if we are creating the first raid device update the family number */
5906 if (super->current_vol == 0) {
5907 __u32 sum;
5908 struct imsm_dev *_dev = __get_imsm_dev(mpb, 0);
5909
5910 _disk = __get_imsm_disk(mpb, dl->index);
5911 if (!_disk) {
5912 pr_err("BUG mpb setup error\n");
5913 return 1;
5914 }
5915 *_dev = *dev;
5916 *_disk = dl->disk;
5917 sum = random32();
5918 sum += __gen_imsm_checksum(mpb);
5919 mpb->family_num = __cpu_to_le32(sum);
5920 mpb->orig_family_num = mpb->family_num;
5921 mpb->creation_time = __cpu_to_le64((__u64)time(NULL));
5922 }
5923 super->current_disk = dl;
5924 return 0;
5925 }
5926
5927 /* mark_spare()
5928 * Function marks disk as spare and restores disk serial
5929 * in case it was previously marked as failed by takeover operation
5930 * reruns:
5931 * -1 : critical error
5932 * 0 : disk is marked as spare but serial is not set
5933 * 1 : success
5934 */
5935 int mark_spare(struct dl *disk)
5936 {
5937 __u8 serial[MAX_RAID_SERIAL_LEN];
5938 int ret_val = -1;
5939
5940 if (!disk)
5941 return ret_val;
5942
5943 ret_val = 0;
5944 if (!imsm_read_serial(disk->fd, NULL, serial, MAX_RAID_SERIAL_LEN)) {
5945 /* Restore disk serial number, because takeover marks disk
5946 * as failed and adds to serial ':0' before it becomes
5947 * a spare disk.
5948 */
5949 serialcpy(disk->serial, serial);
5950 serialcpy(disk->disk.serial, serial);
5951 ret_val = 1;
5952 }
5953 disk->disk.status = SPARE_DISK;
5954 disk->index = -1;
5955
5956 return ret_val;
5957 }
5958
5959
5960 static int write_super_imsm_spare(struct intel_super *super, struct dl *d);
5961
5962 static int add_to_super_imsm(struct supertype *st, mdu_disk_info_t *dk,
5963 int fd, char *devname,
5964 unsigned long long data_offset)
5965 {
5966 struct intel_super *super = st->sb;
5967 struct dl *dd;
5968 unsigned long long size;
5969 unsigned int member_sector_size;
5970 __u32 id;
5971 int rv;
5972 struct stat stb;
5973
5974 /* If we are on an RAID enabled platform check that the disk is
5975 * attached to the raid controller.
5976 * We do not need to test disks attachment for container based additions,
5977 * they shall be already tested when container was created/assembled.
5978 */
5979 rv = find_intel_hba_capability(fd, super, devname);
5980 /* no orom/efi or non-intel hba of the disk */
5981 if (rv != 0) {
5982 dprintf("capability: %p fd: %d ret: %d\n",
5983 super->orom, fd, rv);
5984 return 1;
5985 }
5986
5987 if (super->current_vol >= 0)
5988 return add_to_super_imsm_volume(st, dk, fd, devname);
5989
5990 fstat(fd, &stb);
5991 dd = xcalloc(sizeof(*dd), 1);
5992 dd->major = major(stb.st_rdev);
5993 dd->minor = minor(stb.st_rdev);
5994 dd->devname = devname ? xstrdup(devname) : NULL;
5995 dd->fd = fd;
5996 dd->e = NULL;
5997 dd->action = DISK_ADD;
5998 rv = imsm_read_serial(fd, devname, dd->serial, MAX_RAID_SERIAL_LEN);
5999 if (rv) {
6000 pr_err("failed to retrieve scsi serial, aborting\n");
6001 __free_imsm_disk(dd, 0);
6002 abort();
6003 }
6004
6005 if (super->hba && ((super->hba->type == SYS_DEV_NVME) ||
6006 (super->hba->type == SYS_DEV_VMD))) {
6007 int i;
6008 char cntrl_path[PATH_MAX];
6009 char *cntrl_name;
6010 char pci_dev_path[PATH_MAX];
6011
6012 if (!diskfd_to_devpath(fd, 2, pci_dev_path) ||
6013 !diskfd_to_devpath(fd, 1, cntrl_path)) {
6014 pr_err("failed to get dev paths, aborting\n");
6015 __free_imsm_disk(dd, 0);
6016 return 1;
6017 }
6018
6019 cntrl_name = basename(cntrl_path);
6020 if (is_multipath_nvme(fd))
6021 pr_err("%s controller supports Multi-Path I/O, Intel (R) VROC does not support multipathing\n",
6022 cntrl_name);
6023
6024 if (devpath_to_vendor(pci_dev_path) == 0x8086) {
6025 /*
6026 * If Intel's NVMe drive has serial ended with
6027 * "-A","-B","-1" or "-2" it means that this is "x8"
6028 * device (double drive on single PCIe card).
6029 * User should be warned about potential data loss.
6030 */
6031 for (i = MAX_RAID_SERIAL_LEN-1; i > 0; i--) {
6032 /* Skip empty character at the end */
6033 if (dd->serial[i] == 0)
6034 continue;
6035
6036 if (((dd->serial[i] == 'A') ||
6037 (dd->serial[i] == 'B') ||
6038 (dd->serial[i] == '1') ||
6039 (dd->serial[i] == '2')) &&
6040 (dd->serial[i-1] == '-'))
6041 pr_err("\tThe action you are about to take may put your data at risk.\n"
6042 "\tPlease note that x8 devices may consist of two separate x4 devices "
6043 "located on a single PCIe port.\n"
6044 "\tRAID 0 is the only supported configuration for this type of x8 device.\n");
6045 break;
6046 }
6047 } else if (super->hba->type == SYS_DEV_VMD && super->orom &&
6048 !imsm_orom_has_tpv_support(super->orom)) {
6049 pr_err("\tPlatform configuration does not support non-Intel NVMe drives.\n"
6050 "\tPlease refer to Intel(R) RSTe/VROC user guide.\n");
6051 __free_imsm_disk(dd, 0);
6052 return 1;
6053 }
6054 }
6055
6056 get_dev_size(fd, NULL, &size);
6057 if (!get_dev_sector_size(fd, NULL, &member_sector_size)) {
6058 __free_imsm_disk(dd, 0);
6059 return 1;
6060 }
6061
6062 if (super->sector_size == 0) {
6063 /* this a first device, so sector_size is not set yet */
6064 super->sector_size = member_sector_size;
6065 }
6066
6067 /* clear migr_rec when adding disk to container */
6068 memset(super->migr_rec_buf, 0, MIGR_REC_BUF_SECTORS*MAX_SECTOR_SIZE);
6069 if (lseek64(fd, size - MIGR_REC_SECTOR_POSITION*member_sector_size,
6070 SEEK_SET) >= 0) {
6071 if ((unsigned int)write(fd, super->migr_rec_buf,
6072 MIGR_REC_BUF_SECTORS*member_sector_size) !=
6073 MIGR_REC_BUF_SECTORS*member_sector_size)
6074 perror("Write migr_rec failed");
6075 }
6076
6077 size /= 512;
6078 serialcpy(dd->disk.serial, dd->serial);
6079 set_total_blocks(&dd->disk, size);
6080 if (__le32_to_cpu(dd->disk.total_blocks_hi) > 0) {
6081 struct imsm_super *mpb = super->anchor;
6082 mpb->attributes |= MPB_ATTRIB_2TB_DISK;
6083 }
6084 mark_spare(dd);
6085 if (sysfs_disk_to_scsi_id(fd, &id) == 0)
6086 dd->disk.scsi_id = __cpu_to_le32(id);
6087 else
6088 dd->disk.scsi_id = __cpu_to_le32(0);
6089
6090 if (st->update_tail) {
6091 dd->next = super->disk_mgmt_list;
6092 super->disk_mgmt_list = dd;
6093 } else {
6094 /* this is called outside of mdmon
6095 * write initial spare metadata
6096 * mdmon will overwrite it.
6097 */
6098 dd->next = super->disks;
6099 super->disks = dd;
6100 write_super_imsm_spare(super, dd);
6101 }
6102
6103 return 0;
6104 }
6105
6106 static int remove_from_super_imsm(struct supertype *st, mdu_disk_info_t *dk)
6107 {
6108 struct intel_super *super = st->sb;
6109 struct dl *dd;
6110
6111 /* remove from super works only in mdmon - for communication
6112 * manager - monitor. Check if communication memory buffer
6113 * is prepared.
6114 */
6115 if (!st->update_tail) {
6116 pr_err("shall be used in mdmon context only\n");
6117 return 1;
6118 }
6119 dd = xcalloc(1, sizeof(*dd));
6120 dd->major = dk->major;
6121 dd->minor = dk->minor;
6122 dd->fd = -1;
6123 mark_spare(dd);
6124 dd->action = DISK_REMOVE;
6125
6126 dd->next = super->disk_mgmt_list;
6127 super->disk_mgmt_list = dd;
6128
6129 return 0;
6130 }
6131
6132 static int store_imsm_mpb(int fd, struct imsm_super *mpb);
6133
6134 static union {
6135 char buf[MAX_SECTOR_SIZE];
6136 struct imsm_super anchor;
6137 } spare_record __attribute__ ((aligned(MAX_SECTOR_SIZE)));
6138
6139
6140 static int write_super_imsm_spare(struct intel_super *super, struct dl *d)
6141 {
6142 struct imsm_super *mpb = super->anchor;
6143 struct imsm_super *spare = &spare_record.anchor;
6144 __u32 sum;
6145
6146 if (d->index != -1)
6147 return 1;
6148
6149 spare->mpb_size = __cpu_to_le32(sizeof(struct imsm_super));
6150 spare->generation_num = __cpu_to_le32(1UL);
6151 spare->attributes = MPB_ATTRIB_CHECKSUM_VERIFY;
6152 spare->num_disks = 1;
6153 spare->num_raid_devs = 0;
6154 spare->cache_size = mpb->cache_size;
6155 spare->pwr_cycle_count = __cpu_to_le32(1);
6156
6157 snprintf((char *) spare->sig, MAX_SIGNATURE_LENGTH,
6158 MPB_SIGNATURE MPB_VERSION_RAID0);
6159
6160 spare->disk[0] = d->disk;
6161 if (__le32_to_cpu(d->disk.total_blocks_hi) > 0)
6162 spare->attributes |= MPB_ATTRIB_2TB_DISK;
6163
6164 if (super->sector_size == 4096)
6165 convert_to_4k_imsm_disk(&spare->disk[0]);
6166
6167 sum = __gen_imsm_checksum(spare);
6168 spare->family_num = __cpu_to_le32(sum);
6169 spare->orig_family_num = 0;
6170 sum = __gen_imsm_checksum(spare);
6171 spare->check_sum = __cpu_to_le32(sum);
6172
6173 if (store_imsm_mpb(d->fd, spare)) {
6174 pr_err("failed for device %d:%d %s\n",
6175 d->major, d->minor, strerror(errno));
6176 return 1;
6177 }
6178
6179 return 0;
6180 }
6181 /* spare records have their own family number and do not have any defined raid
6182 * devices
6183 */
6184 static int write_super_imsm_spares(struct intel_super *super, int doclose)
6185 {
6186 struct dl *d;
6187
6188 for (d = super->disks; d; d = d->next) {
6189 if (d->index != -1)
6190 continue;
6191
6192 if (write_super_imsm_spare(super, d))
6193 return 1;
6194
6195 if (doclose)
6196 close_fd(&d->fd);
6197 }
6198
6199 return 0;
6200 }
6201
6202 static int write_super_imsm(struct supertype *st, int doclose)
6203 {
6204 struct intel_super *super = st->sb;
6205 unsigned int sector_size = super->sector_size;
6206 struct imsm_super *mpb = super->anchor;
6207 struct dl *d;
6208 __u32 generation;
6209 __u32 sum;
6210 int spares = 0;
6211 int i;
6212 __u32 mpb_size = sizeof(struct imsm_super) - sizeof(struct imsm_disk);
6213 int num_disks = 0;
6214 int clear_migration_record = 1;
6215 __u32 bbm_log_size;
6216
6217 /* 'generation' is incremented everytime the metadata is written */
6218 generation = __le32_to_cpu(mpb->generation_num);
6219 generation++;
6220 mpb->generation_num = __cpu_to_le32(generation);
6221
6222 /* fix up cases where previous mdadm releases failed to set
6223 * orig_family_num
6224 */
6225 if (mpb->orig_family_num == 0)
6226 mpb->orig_family_num = mpb->family_num;
6227
6228 for (d = super->disks; d; d = d->next) {
6229 if (d->index == -1)
6230 spares++;
6231 else {
6232 mpb->disk[d->index] = d->disk;
6233 num_disks++;
6234 }
6235 }
6236 for (d = super->missing; d; d = d->next) {
6237 mpb->disk[d->index] = d->disk;
6238 num_disks++;
6239 }
6240 mpb->num_disks = num_disks;
6241 mpb_size += sizeof(struct imsm_disk) * mpb->num_disks;
6242
6243 for (i = 0; i < mpb->num_raid_devs; i++) {
6244 struct imsm_dev *dev = __get_imsm_dev(mpb, i);
6245 struct imsm_dev *dev2 = get_imsm_dev(super, i);
6246
6247 imsm_copy_dev(dev, dev2);
6248 mpb_size += sizeof_imsm_dev(dev, 0);
6249
6250 if (is_gen_migration(dev2))
6251 clear_migration_record = 0;
6252 }
6253
6254 bbm_log_size = get_imsm_bbm_log_size(super->bbm_log);
6255
6256 if (bbm_log_size) {
6257 memcpy((void *)mpb + mpb_size, super->bbm_log, bbm_log_size);
6258 mpb->attributes |= MPB_ATTRIB_BBM;
6259 } else
6260 mpb->attributes &= ~MPB_ATTRIB_BBM;
6261
6262 super->anchor->bbm_log_size = __cpu_to_le32(bbm_log_size);
6263 mpb_size += bbm_log_size;
6264 mpb->mpb_size = __cpu_to_le32(mpb_size);
6265
6266 #ifdef DEBUG
6267 assert(super->len == 0 || mpb_size <= super->len);
6268 #endif
6269
6270 /* recalculate checksum */
6271 sum = __gen_imsm_checksum(mpb);
6272 mpb->check_sum = __cpu_to_le32(sum);
6273
6274 if (super->clean_migration_record_by_mdmon) {
6275 clear_migration_record = 1;
6276 super->clean_migration_record_by_mdmon = 0;
6277 }
6278 if (clear_migration_record)
6279 memset(super->migr_rec_buf, 0,
6280 MIGR_REC_BUF_SECTORS*MAX_SECTOR_SIZE);
6281
6282 if (sector_size == 4096)
6283 convert_to_4k(super);
6284
6285 /* write the mpb for disks that compose raid devices */
6286 for (d = super->disks; d ; d = d->next) {
6287 if (d->index < 0 || is_failed(&d->disk))
6288 continue;
6289
6290 if (clear_migration_record) {
6291 unsigned long long dsize;
6292
6293 get_dev_size(d->fd, NULL, &dsize);
6294 if (lseek64(d->fd, dsize - sector_size,
6295 SEEK_SET) >= 0) {
6296 if ((unsigned int)write(d->fd,
6297 super->migr_rec_buf,
6298 MIGR_REC_BUF_SECTORS*sector_size) !=
6299 MIGR_REC_BUF_SECTORS*sector_size)
6300 perror("Write migr_rec failed");
6301 }
6302 }
6303
6304 if (store_imsm_mpb(d->fd, mpb))
6305 fprintf(stderr,
6306 "failed for device %d:%d (fd: %d)%s\n",
6307 d->major, d->minor,
6308 d->fd, strerror(errno));
6309
6310 if (doclose)
6311 close_fd(&d->fd);
6312 }
6313
6314 if (spares)
6315 return write_super_imsm_spares(super, doclose);
6316
6317 return 0;
6318 }
6319
6320 static int create_array(struct supertype *st, int dev_idx)
6321 {
6322 size_t len;
6323 struct imsm_update_create_array *u;
6324 struct intel_super *super = st->sb;
6325 struct imsm_dev *dev = get_imsm_dev(super, dev_idx);
6326 struct imsm_map *map = get_imsm_map(dev, MAP_0);
6327 struct disk_info *inf;
6328 struct imsm_disk *disk;
6329 int i;
6330
6331 len = sizeof(*u) - sizeof(*dev) + sizeof_imsm_dev(dev, 0) +
6332 sizeof(*inf) * map->num_members;
6333 u = xmalloc(len);
6334 u->type = update_create_array;
6335 u->dev_idx = dev_idx;
6336 imsm_copy_dev(&u->dev, dev);
6337 inf = get_disk_info(u);
6338 for (i = 0; i < map->num_members; i++) {
6339 int idx = get_imsm_disk_idx(dev, i, MAP_X);
6340
6341 disk = get_imsm_disk(super, idx);
6342 if (!disk)
6343 disk = get_imsm_missing(super, idx);
6344 serialcpy(inf[i].serial, disk->serial);
6345 }
6346 append_metadata_update(st, u, len);
6347
6348 return 0;
6349 }
6350
6351 static int mgmt_disk(struct supertype *st)
6352 {
6353 struct intel_super *super = st->sb;
6354 size_t len;
6355 struct imsm_update_add_remove_disk *u;
6356
6357 if (!super->disk_mgmt_list)
6358 return 0;
6359
6360 len = sizeof(*u);
6361 u = xmalloc(len);
6362 u->type = update_add_remove_disk;
6363 append_metadata_update(st, u, len);
6364
6365 return 0;
6366 }
6367
6368 __u32 crc32c_le(__u32 crc, unsigned char const *p, size_t len);
6369
6370 static int write_ppl_header(unsigned long long ppl_sector, int fd, void *buf)
6371 {
6372 struct ppl_header *ppl_hdr = buf;
6373 int ret;
6374
6375 ppl_hdr->checksum = __cpu_to_le32(~crc32c_le(~0, buf, PPL_HEADER_SIZE));
6376
6377 if (lseek64(fd, ppl_sector * 512, SEEK_SET) < 0) {
6378 ret = -errno;
6379 perror("Failed to seek to PPL header location");
6380 return ret;
6381 }
6382
6383 if (write(fd, buf, PPL_HEADER_SIZE) != PPL_HEADER_SIZE) {
6384 ret = -errno;
6385 perror("Write PPL header failed");
6386 return ret;
6387 }
6388
6389 fsync(fd);
6390
6391 return 0;
6392 }
6393
6394 static int write_init_ppl_imsm(struct supertype *st, struct mdinfo *info, int fd)
6395 {
6396 struct intel_super *super = st->sb;
6397 void *buf;
6398 struct ppl_header *ppl_hdr;
6399 int ret;
6400
6401 /* first clear entire ppl space */
6402 ret = zero_disk_range(fd, info->ppl_sector, info->ppl_size);
6403 if (ret)
6404 return ret;
6405
6406 ret = posix_memalign(&buf, MAX_SECTOR_SIZE, PPL_HEADER_SIZE);
6407 if (ret) {
6408 pr_err("Failed to allocate PPL header buffer\n");
6409 return -ret;
6410 }
6411
6412 memset(buf, 0, PPL_HEADER_SIZE);
6413 ppl_hdr = buf;
6414 memset(ppl_hdr->reserved, 0xff, PPL_HDR_RESERVED);
6415 ppl_hdr->signature = __cpu_to_le32(super->anchor->orig_family_num);
6416
6417 if (info->mismatch_cnt) {
6418 /*
6419 * We are overwriting an invalid ppl. Make one entry with wrong
6420 * checksum to prevent the kernel from skipping resync.
6421 */
6422 ppl_hdr->entries_count = __cpu_to_le32(1);
6423 ppl_hdr->entries[0].checksum = ~0;
6424 }
6425
6426 ret = write_ppl_header(info->ppl_sector, fd, buf);
6427
6428 free(buf);
6429 return ret;
6430 }
6431
6432 static int is_rebuilding(struct imsm_dev *dev);
6433
6434 static int validate_ppl_imsm(struct supertype *st, struct mdinfo *info,
6435 struct mdinfo *disk)
6436 {
6437 struct intel_super *super = st->sb;
6438 struct dl *d;
6439 void *buf_orig, *buf, *buf_prev = NULL;
6440 int ret = 0;
6441 struct ppl_header *ppl_hdr = NULL;
6442 __u32 crc;
6443 struct imsm_dev *dev;
6444 __u32 idx;
6445 unsigned int i;
6446 unsigned long long ppl_offset = 0;
6447 unsigned long long prev_gen_num = 0;
6448
6449 if (disk->disk.raid_disk < 0)
6450 return 0;
6451
6452 dev = get_imsm_dev(super, info->container_member);
6453 idx = get_imsm_disk_idx(dev, disk->disk.raid_disk, MAP_0);
6454 d = get_imsm_dl_disk(super, idx);
6455
6456 if (!d || d->index < 0 || is_failed(&d->disk))
6457 return 0;
6458
6459 if (posix_memalign(&buf_orig, MAX_SECTOR_SIZE, PPL_HEADER_SIZE * 2)) {
6460 pr_err("Failed to allocate PPL header buffer\n");
6461 return -1;
6462 }
6463 buf = buf_orig;
6464
6465 ret = 1;
6466 while (ppl_offset < MULTIPLE_PPL_AREA_SIZE_IMSM) {
6467 void *tmp;
6468
6469 dprintf("Checking potential PPL at offset: %llu\n", ppl_offset);
6470
6471 if (lseek64(d->fd, info->ppl_sector * 512 + ppl_offset,
6472 SEEK_SET) < 0) {
6473 perror("Failed to seek to PPL header location");
6474 ret = -1;
6475 break;
6476 }
6477
6478 if (read(d->fd, buf, PPL_HEADER_SIZE) != PPL_HEADER_SIZE) {
6479 perror("Read PPL header failed");
6480 ret = -1;
6481 break;
6482 }
6483
6484 ppl_hdr = buf;
6485
6486 crc = __le32_to_cpu(ppl_hdr->checksum);
6487 ppl_hdr->checksum = 0;
6488
6489 if (crc != ~crc32c_le(~0, buf, PPL_HEADER_SIZE)) {
6490 dprintf("Wrong PPL header checksum on %s\n",
6491 d->devname);
6492 break;
6493 }
6494
6495 if (prev_gen_num > __le64_to_cpu(ppl_hdr->generation)) {
6496 /* previous was newest, it was already checked */
6497 break;
6498 }
6499
6500 if ((__le32_to_cpu(ppl_hdr->signature) !=
6501 super->anchor->orig_family_num)) {
6502 dprintf("Wrong PPL header signature on %s\n",
6503 d->devname);
6504 ret = 1;
6505 break;
6506 }
6507
6508 ret = 0;
6509 prev_gen_num = __le64_to_cpu(ppl_hdr->generation);
6510
6511 ppl_offset += PPL_HEADER_SIZE;
6512 for (i = 0; i < __le32_to_cpu(ppl_hdr->entries_count); i++)
6513 ppl_offset +=
6514 __le32_to_cpu(ppl_hdr->entries[i].pp_size);
6515
6516 if (!buf_prev)
6517 buf_prev = buf + PPL_HEADER_SIZE;
6518 tmp = buf_prev;
6519 buf_prev = buf;
6520 buf = tmp;
6521 }
6522
6523 if (buf_prev) {
6524 buf = buf_prev;
6525 ppl_hdr = buf_prev;
6526 }
6527
6528 /*
6529 * Update metadata to use mutliple PPLs area (1MB).
6530 * This is done once for all RAID members
6531 */
6532 if (info->consistency_policy == CONSISTENCY_POLICY_PPL &&
6533 info->ppl_size != (MULTIPLE_PPL_AREA_SIZE_IMSM >> 9)) {
6534 char subarray[20];
6535 struct mdinfo *member_dev;
6536
6537 sprintf(subarray, "%d", info->container_member);
6538
6539 if (mdmon_running(st->container_devnm))
6540 st->update_tail = &st->updates;
6541
6542 if (st->ss->update_subarray(st, subarray, UOPT_PPL, NULL)) {
6543 pr_err("Failed to update subarray %s\n",
6544 subarray);
6545 } else {
6546 if (st->update_tail)
6547 flush_metadata_updates(st);
6548 else
6549 st->ss->sync_metadata(st);
6550 info->ppl_size = (MULTIPLE_PPL_AREA_SIZE_IMSM >> 9);
6551 for (member_dev = info->devs; member_dev;
6552 member_dev = member_dev->next)
6553 member_dev->ppl_size =
6554 (MULTIPLE_PPL_AREA_SIZE_IMSM >> 9);
6555 }
6556 }
6557
6558 if (ret == 1) {
6559 struct imsm_map *map = get_imsm_map(dev, MAP_X);
6560
6561 if (map->map_state == IMSM_T_STATE_UNINITIALIZED ||
6562 (map->map_state == IMSM_T_STATE_NORMAL &&
6563 !(dev->vol.dirty & RAIDVOL_DIRTY)) ||
6564 (is_rebuilding(dev) &&
6565 vol_curr_migr_unit(dev) == 0 &&
6566 get_imsm_disk_idx(dev, disk->disk.raid_disk, MAP_1) != idx))
6567 ret = st->ss->write_init_ppl(st, info, d->fd);
6568 else
6569 info->mismatch_cnt++;
6570 } else if (ret == 0 &&
6571 ppl_hdr->entries_count == 0 &&
6572 is_rebuilding(dev) &&
6573 info->resync_start == 0) {
6574 /*
6575 * The header has no entries - add a single empty entry and
6576 * rewrite the header to prevent the kernel from going into
6577 * resync after an interrupted rebuild.
6578 */
6579 ppl_hdr->entries_count = __cpu_to_le32(1);
6580 ret = write_ppl_header(info->ppl_sector, d->fd, buf);
6581 }
6582
6583 free(buf_orig);
6584
6585 return ret;
6586 }
6587
6588 static int write_init_ppl_imsm_all(struct supertype *st, struct mdinfo *info)
6589 {
6590 struct intel_super *super = st->sb;
6591 struct dl *d;
6592 int ret = 0;
6593
6594 if (info->consistency_policy != CONSISTENCY_POLICY_PPL ||
6595 info->array.level != 5)
6596 return 0;
6597
6598 for (d = super->disks; d ; d = d->next) {
6599 if (d->index < 0 || is_failed(&d->disk))
6600 continue;
6601
6602 ret = st->ss->write_init_ppl(st, info, d->fd);
6603 if (ret)
6604 break;
6605 }
6606
6607 return ret;
6608 }
6609
6610 /*******************************************************************************
6611 * Function: write_init_bitmap_imsm_vol
6612 * Description: Write a bitmap header and prepares the area for the bitmap.
6613 * Parameters:
6614 * st : supertype information
6615 * vol_idx : the volume index to use
6616 *
6617 * Returns:
6618 * 0 : success
6619 * -1 : fail
6620 ******************************************************************************/
6621 static int write_init_bitmap_imsm_vol(struct supertype *st, int vol_idx)
6622 {
6623 struct intel_super *super = st->sb;
6624 int prev_current_vol = super->current_vol;
6625 struct dl *d;
6626 int ret = 0;
6627
6628 super->current_vol = vol_idx;
6629 for (d = super->disks; d; d = d->next) {
6630 if (d->index < 0 || is_failed(&d->disk))
6631 continue;
6632 ret = st->ss->write_bitmap(st, d->fd, NoUpdate);
6633 if (ret)
6634 break;
6635 }
6636 super->current_vol = prev_current_vol;
6637 return ret;
6638 }
6639
6640 /*******************************************************************************
6641 * Function: write_init_bitmap_imsm_all
6642 * Description: Write a bitmap header and prepares the area for the bitmap.
6643 * Operation is executed for volumes with CONSISTENCY_POLICY_BITMAP.
6644 * Parameters:
6645 * st : supertype information
6646 * info : info about the volume where the bitmap should be written
6647 * vol_idx : the volume index to use
6648 *
6649 * Returns:
6650 * 0 : success
6651 * -1 : fail
6652 ******************************************************************************/
6653 static int write_init_bitmap_imsm_all(struct supertype *st, struct mdinfo *info,
6654 int vol_idx)
6655 {
6656 int ret = 0;
6657
6658 if (info && (info->consistency_policy == CONSISTENCY_POLICY_BITMAP))
6659 ret = write_init_bitmap_imsm_vol(st, vol_idx);
6660
6661 return ret;
6662 }
6663
6664 static int write_init_super_imsm(struct supertype *st)
6665 {
6666 struct intel_super *super = st->sb;
6667 int current_vol = super->current_vol;
6668 int rv = 0;
6669 struct mdinfo info;
6670
6671 getinfo_super_imsm(st, &info, NULL);
6672
6673 /* we are done with current_vol reset it to point st at the container */
6674 super->current_vol = -1;
6675
6676 if (st->update_tail) {
6677 /* queue the recently created array / added disk
6678 * as a metadata update */
6679
6680 /* determine if we are creating a volume or adding a disk */
6681 if (current_vol < 0) {
6682 /* in the mgmt (add/remove) disk case we are running
6683 * in mdmon context, so don't close fd's
6684 */
6685 rv = mgmt_disk(st);
6686 } else {
6687 /* adding the second volume to the array */
6688 rv = write_init_ppl_imsm_all(st, &info);
6689 if (!rv)
6690 rv = write_init_bitmap_imsm_all(st, &info, current_vol);
6691 if (!rv)
6692 rv = create_array(st, current_vol);
6693 }
6694 } else {
6695 struct dl *d;
6696 for (d = super->disks; d; d = d->next)
6697 Kill(d->devname, NULL, 0, -1, 1);
6698 if (current_vol >= 0) {
6699 rv = write_init_ppl_imsm_all(st, &info);
6700 if (!rv)
6701 rv = write_init_bitmap_imsm_all(st, &info, current_vol);
6702 }
6703
6704 if (!rv)
6705 rv = write_super_imsm(st, 1);
6706 }
6707
6708 return rv;
6709 }
6710
6711 static int store_super_imsm(struct supertype *st, int fd)
6712 {
6713 struct intel_super *super = st->sb;
6714 struct imsm_super *mpb = super ? super->anchor : NULL;
6715
6716 if (!mpb)
6717 return 1;
6718
6719 if (super->sector_size == 4096)
6720 convert_to_4k(super);
6721 return store_imsm_mpb(fd, mpb);
6722 }
6723
6724 static int validate_geometry_imsm_container(struct supertype *st, int level,
6725 int raiddisks,
6726 unsigned long long data_offset,
6727 char *dev,
6728 unsigned long long *freesize,
6729 int verbose)
6730 {
6731 int fd;
6732 unsigned long long ldsize;
6733 struct intel_super *super = NULL;
6734 int rv = 0;
6735
6736 if (!is_container(level))
6737 return 0;
6738 if (!dev)
6739 return 1;
6740
6741 fd = dev_open(dev, O_RDONLY|O_EXCL);
6742 if (!is_fd_valid(fd)) {
6743 pr_vrb("imsm: Cannot open %s: %s\n", dev, strerror(errno));
6744 return 0;
6745 }
6746 if (!get_dev_size(fd, dev, &ldsize))
6747 goto exit;
6748
6749 /* capabilities retrieve could be possible
6750 * note that there is no fd for the disks in array.
6751 */
6752 super = alloc_super();
6753 if (!super)
6754 goto exit;
6755
6756 if (!get_dev_sector_size(fd, NULL, &super->sector_size))
6757 goto exit;
6758
6759 rv = find_intel_hba_capability(fd, super, verbose > 0 ? dev : NULL);
6760 if (rv != 0) {
6761 #if DEBUG
6762 char str[256];
6763 fd2devname(fd, str);
6764 dprintf("fd: %d %s orom: %p rv: %d raiddisk: %d\n",
6765 fd, str, super->orom, rv, raiddisks);
6766 #endif
6767 /* no orom/efi or non-intel hba of the disk */
6768 rv = 0;
6769 goto exit;
6770 }
6771 if (super->orom) {
6772 if (raiddisks > super->orom->tds) {
6773 if (verbose)
6774 pr_err("%d exceeds maximum number of platform supported disks: %d\n",
6775 raiddisks, super->orom->tds);
6776 goto exit;
6777 }
6778 if ((super->orom->attr & IMSM_OROM_ATTR_2TB_DISK) == 0 &&
6779 (ldsize >> 9) >> 32 > 0) {
6780 if (verbose)
6781 pr_err("%s exceeds maximum platform supported size\n", dev);
6782 goto exit;
6783 }
6784
6785 if (super->hba->type == SYS_DEV_VMD ||
6786 super->hba->type == SYS_DEV_NVME) {
6787 if (!imsm_is_nvme_namespace_supported(fd, 1)) {
6788 if (verbose)
6789 pr_err("NVMe namespace %s is not supported by IMSM\n",
6790 basename(dev));
6791 goto exit;
6792 }
6793 }
6794 }
6795 if (freesize)
6796 *freesize = avail_size_imsm(st, ldsize >> 9, data_offset);
6797 rv = 1;
6798 exit:
6799 if (super)
6800 free_imsm(super);
6801 close(fd);
6802
6803 return rv;
6804 }
6805
6806 static unsigned long long find_size(struct extent *e, int *idx, int num_extents)
6807 {
6808 const unsigned long long base_start = e[*idx].start;
6809 unsigned long long end = base_start + e[*idx].size;
6810 int i;
6811
6812 if (base_start == end)
6813 return 0;
6814
6815 *idx = *idx + 1;
6816 for (i = *idx; i < num_extents; i++) {
6817 /* extend overlapping extents */
6818 if (e[i].start >= base_start &&
6819 e[i].start <= end) {
6820 if (e[i].size == 0)
6821 return 0;
6822 if (e[i].start + e[i].size > end)
6823 end = e[i].start + e[i].size;
6824 } else if (e[i].start > end) {
6825 *idx = i;
6826 break;
6827 }
6828 }
6829
6830 return end - base_start;
6831 }
6832
6833 static unsigned long long merge_extents(struct intel_super *super, int sum_extents)
6834 {
6835 /* build a composite disk with all known extents and generate a new
6836 * 'maxsize' given the "all disks in an array must share a common start
6837 * offset" constraint
6838 */
6839 struct extent *e = xcalloc(sum_extents, sizeof(*e));
6840 struct dl *dl;
6841 int i, j;
6842 int start_extent;
6843 unsigned long long pos;
6844 unsigned long long start = 0;
6845 unsigned long long maxsize;
6846 unsigned long reserve;
6847
6848 /* coalesce and sort all extents. also, check to see if we need to
6849 * reserve space between member arrays
6850 */
6851 j = 0;
6852 for (dl = super->disks; dl; dl = dl->next) {
6853 if (!dl->e)
6854 continue;
6855 for (i = 0; i < dl->extent_cnt; i++)
6856 e[j++] = dl->e[i];
6857 }
6858 qsort(e, sum_extents, sizeof(*e), cmp_extent);
6859
6860 /* merge extents */
6861 i = 0;
6862 j = 0;
6863 while (i < sum_extents) {
6864 e[j].start = e[i].start;
6865 e[j].size = find_size(e, &i, sum_extents);
6866 j++;
6867 if (e[j-1].size == 0)
6868 break;
6869 }
6870
6871 pos = 0;
6872 maxsize = 0;
6873 start_extent = 0;
6874 i = 0;
6875 do {
6876 unsigned long long esize;
6877
6878 esize = e[i].start - pos;
6879 if (esize >= maxsize) {
6880 maxsize = esize;
6881 start = pos;
6882 start_extent = i;
6883 }
6884 pos = e[i].start + e[i].size;
6885 i++;
6886 } while (e[i-1].size);
6887 free(e);
6888
6889 if (maxsize == 0)
6890 return 0;
6891
6892 /* FIXME assumes volume at offset 0 is the first volume in a
6893 * container
6894 */
6895 if (start_extent > 0)
6896 reserve = IMSM_RESERVED_SECTORS; /* gap between raid regions */
6897 else
6898 reserve = 0;
6899
6900 if (maxsize < reserve)
6901 return 0;
6902
6903 super->create_offset = ~((unsigned long long) 0);
6904 if (start + reserve > super->create_offset)
6905 return 0; /* start overflows create_offset */
6906 super->create_offset = start + reserve;
6907
6908 return maxsize - reserve;
6909 }
6910
6911 static int is_raid_level_supported(const struct imsm_orom *orom, int level, int raiddisks)
6912 {
6913 if (level < 0 || level == 6 || level == 4)
6914 return 0;
6915
6916 /* if we have an orom prevent invalid raid levels */
6917 if (orom)
6918 switch (level) {
6919 case 0: return imsm_orom_has_raid0(orom);
6920 case 1:
6921 if (raiddisks > 2)
6922 return imsm_orom_has_raid1e(orom);
6923 return imsm_orom_has_raid1(orom) && raiddisks == 2;
6924 case 10: return imsm_orom_has_raid10(orom) && raiddisks == 4;
6925 case 5: return imsm_orom_has_raid5(orom) && raiddisks > 2;
6926 }
6927 else
6928 return 1; /* not on an Intel RAID platform so anything goes */
6929
6930 return 0;
6931 }
6932
6933 static int
6934 active_arrays_by_format(char *name, char* hba, struct md_list **devlist,
6935 int dpa, int verbose)
6936 {
6937 struct mdstat_ent *mdstat = mdstat_read(0, 0);
6938 struct mdstat_ent *memb;
6939 int count = 0;
6940 int num = 0;
6941 struct md_list *dv;
6942 int found;
6943
6944 for (memb = mdstat ; memb ; memb = memb->next) {
6945 if (memb->metadata_version &&
6946 (strncmp(memb->metadata_version, "external:", 9) == 0) &&
6947 (strcmp(&memb->metadata_version[9], name) == 0) &&
6948 !is_subarray(memb->metadata_version+9) &&
6949 memb->members) {
6950 struct dev_member *dev = memb->members;
6951 int fd = -1;
6952 while (dev && !is_fd_valid(fd)) {
6953 char *path = xmalloc(strlen(dev->name) + strlen("/dev/") + 1);
6954 num = sprintf(path, "%s%s", "/dev/", dev->name);
6955 if (num > 0)
6956 fd = open(path, O_RDONLY, 0);
6957 if (num <= 0 || !is_fd_valid(fd)) {
6958 pr_vrb("Cannot open %s: %s\n",
6959 dev->name, strerror(errno));
6960 }
6961 free(path);
6962 dev = dev->next;
6963 }
6964 found = 0;
6965 if (is_fd_valid(fd) && disk_attached_to_hba(fd, hba)) {
6966 struct mdstat_ent *vol;
6967 for (vol = mdstat ; vol ; vol = vol->next) {
6968 if (vol->active > 0 &&
6969 vol->metadata_version &&
6970 is_container_member(vol, memb->devnm)) {
6971 found++;
6972 count++;
6973 }
6974 }
6975 if (*devlist && (found < dpa)) {
6976 dv = xcalloc(1, sizeof(*dv));
6977 dv->devname = xmalloc(strlen(memb->devnm) + strlen("/dev/") + 1);
6978 sprintf(dv->devname, "%s%s", "/dev/", memb->devnm);
6979 dv->found = found;
6980 dv->used = 0;
6981 dv->next = *devlist;
6982 *devlist = dv;
6983 }
6984 }
6985 close_fd(&fd);
6986 }
6987 }
6988 free_mdstat(mdstat);
6989 return count;
6990 }
6991
6992 #ifdef DEBUG_LOOP
6993 static struct md_list*
6994 get_loop_devices(void)
6995 {
6996 int i;
6997 struct md_list *devlist = NULL;
6998 struct md_list *dv;
6999
7000 for(i = 0; i < 12; i++) {
7001 dv = xcalloc(1, sizeof(*dv));
7002 dv->devname = xmalloc(40);
7003 sprintf(dv->devname, "/dev/loop%d", i);
7004 dv->next = devlist;
7005 devlist = dv;
7006 }
7007 return devlist;
7008 }
7009 #endif
7010
7011 static struct md_list*
7012 get_devices(const char *hba_path)
7013 {
7014 struct md_list *devlist = NULL;
7015 struct md_list *dv;
7016 struct dirent *ent;
7017 DIR *dir;
7018 int err = 0;
7019
7020 #if DEBUG_LOOP
7021 devlist = get_loop_devices();
7022 return devlist;
7023 #endif
7024 /* scroll through /sys/dev/block looking for devices attached to
7025 * this hba
7026 */
7027 dir = opendir("/sys/dev/block");
7028 for (ent = dir ? readdir(dir) : NULL; ent; ent = readdir(dir)) {
7029 int fd;
7030 char buf[1024];
7031 int major, minor;
7032 char *path = NULL;
7033 if (sscanf(ent->d_name, "%d:%d", &major, &minor) != 2)
7034 continue;
7035 path = devt_to_devpath(makedev(major, minor), 1, NULL);
7036 if (!path)
7037 continue;
7038 if (!path_attached_to_hba(path, hba_path)) {
7039 free(path);
7040 path = NULL;
7041 continue;
7042 }
7043 free(path);
7044 path = NULL;
7045 fd = dev_open(ent->d_name, O_RDONLY);
7046 if (is_fd_valid(fd)) {
7047 fd2devname(fd, buf);
7048 close(fd);
7049 } else {
7050 pr_err("cannot open device: %s\n",
7051 ent->d_name);
7052 continue;
7053 }
7054
7055 dv = xcalloc(1, sizeof(*dv));
7056 dv->devname = xstrdup(buf);
7057 dv->next = devlist;
7058 devlist = dv;
7059 }
7060 if (err) {
7061 while(devlist) {
7062 dv = devlist;
7063 devlist = devlist->next;
7064 free(dv->devname);
7065 free(dv);
7066 }
7067 }
7068 closedir(dir);
7069 return devlist;
7070 }
7071
7072 static int
7073 count_volumes_list(struct md_list *devlist, char *homehost,
7074 int verbose, int *found)
7075 {
7076 struct md_list *tmpdev;
7077 int count = 0;
7078 struct supertype *st;
7079
7080 /* first walk the list of devices to find a consistent set
7081 * that match the criterea, if that is possible.
7082 * We flag the ones we like with 'used'.
7083 */
7084 *found = 0;
7085 st = match_metadata_desc_imsm("imsm");
7086 if (st == NULL) {
7087 pr_vrb("cannot allocate memory for imsm supertype\n");
7088 return 0;
7089 }
7090
7091 for (tmpdev = devlist; tmpdev; tmpdev = tmpdev->next) {
7092 char *devname = tmpdev->devname;
7093 dev_t rdev;
7094 struct supertype *tst;
7095 int dfd;
7096 if (tmpdev->used > 1)
7097 continue;
7098 tst = dup_super(st);
7099 if (tst == NULL) {
7100 pr_vrb("cannot allocate memory for imsm supertype\n");
7101 goto err_1;
7102 }
7103 tmpdev->container = 0;
7104 dfd = dev_open(devname, O_RDONLY|O_EXCL);
7105 if (!is_fd_valid(dfd)) {
7106 dprintf("cannot open device %s: %s\n",
7107 devname, strerror(errno));
7108 tmpdev->used = 2;
7109 } else if (!fstat_is_blkdev(dfd, devname, &rdev)) {
7110 tmpdev->used = 2;
7111 } else if (must_be_container(dfd)) {
7112 struct supertype *cst;
7113 cst = super_by_fd(dfd, NULL);
7114 if (cst == NULL) {
7115 dprintf("cannot recognize container type %s\n",
7116 devname);
7117 tmpdev->used = 2;
7118 } else if (tst->ss != st->ss) {
7119 dprintf("non-imsm container - ignore it: %s\n",
7120 devname);
7121 tmpdev->used = 2;
7122 } else if (!tst->ss->load_container ||
7123 tst->ss->load_container(tst, dfd, NULL))
7124 tmpdev->used = 2;
7125 else {
7126 tmpdev->container = 1;
7127 }
7128 if (cst)
7129 cst->ss->free_super(cst);
7130 } else {
7131 tmpdev->st_rdev = rdev;
7132 if (tst->ss->load_super(tst,dfd, NULL)) {
7133 dprintf("no RAID superblock on %s\n",
7134 devname);
7135 tmpdev->used = 2;
7136 } else if (tst->ss->compare_super == NULL) {
7137 dprintf("Cannot assemble %s metadata on %s\n",
7138 tst->ss->name, devname);
7139 tmpdev->used = 2;
7140 }
7141 }
7142 close_fd(&dfd);
7143
7144 if (tmpdev->used == 2 || tmpdev->used == 4) {
7145 /* Ignore unrecognised devices during auto-assembly */
7146 goto loop;
7147 }
7148 else {
7149 struct mdinfo info;
7150 tst->ss->getinfo_super(tst, &info, NULL);
7151
7152 if (st->minor_version == -1)
7153 st->minor_version = tst->minor_version;
7154
7155 if (memcmp(info.uuid, uuid_zero,
7156 sizeof(int[4])) == 0) {
7157 /* this is a floating spare. It cannot define
7158 * an array unless there are no more arrays of
7159 * this type to be found. It can be included
7160 * in an array of this type though.
7161 */
7162 tmpdev->used = 3;
7163 goto loop;
7164 }
7165
7166 if (st->ss != tst->ss ||
7167 st->minor_version != tst->minor_version ||
7168 st->ss->compare_super(st, tst, 1) != 0) {
7169 /* Some mismatch. If exactly one array matches this host,
7170 * we can resolve on that one.
7171 * Or, if we are auto assembling, we just ignore the second
7172 * for now.
7173 */
7174 dprintf("superblock on %s doesn't match others - assembly aborted\n",
7175 devname);
7176 goto loop;
7177 }
7178 tmpdev->used = 1;
7179 *found = 1;
7180 dprintf("found: devname: %s\n", devname);
7181 }
7182 loop:
7183 if (tst)
7184 tst->ss->free_super(tst);
7185 }
7186 if (*found != 0) {
7187 int err;
7188 if ((err = load_super_imsm_all(st, -1, &st->sb, NULL, devlist, 0)) == 0) {
7189 struct mdinfo *iter, *head = st->ss->container_content(st, NULL);
7190 for (iter = head; iter; iter = iter->next) {
7191 dprintf("content->text_version: %s vol\n",
7192 iter->text_version);
7193 if (iter->array.state & (1<<MD_SB_BLOCK_VOLUME)) {
7194 /* do not assemble arrays with unsupported
7195 configurations */
7196 dprintf("Cannot activate member %s.\n",
7197 iter->text_version);
7198 } else
7199 count++;
7200 }
7201 sysfs_free(head);
7202
7203 } else {
7204 dprintf("No valid super block on device list: err: %d %p\n",
7205 err, st->sb);
7206 }
7207 } else {
7208 dprintf("no more devices to examine\n");
7209 }
7210
7211 for (tmpdev = devlist; tmpdev; tmpdev = tmpdev->next) {
7212 if (tmpdev->used == 1 && tmpdev->found) {
7213 if (count) {
7214 if (count < tmpdev->found)
7215 count = 0;
7216 else
7217 count -= tmpdev->found;
7218 }
7219 }
7220 if (tmpdev->used == 1)
7221 tmpdev->used = 4;
7222 }
7223 err_1:
7224 if (st)
7225 st->ss->free_super(st);
7226 return count;
7227 }
7228
7229 static int __count_volumes(char *hba_path, int dpa, int verbose,
7230 int cmp_hba_path)
7231 {
7232 struct sys_dev *idev, *intel_devices = find_intel_devices();
7233 int count = 0;
7234 const struct orom_entry *entry;
7235 struct devid_list *dv, *devid_list;
7236
7237 if (!hba_path)
7238 return 0;
7239
7240 for (idev = intel_devices; idev; idev = idev->next) {
7241 if (strstr(idev->path, hba_path))
7242 break;
7243 }
7244
7245 if (!idev || !idev->dev_id)
7246 return 0;
7247
7248 entry = get_orom_entry_by_device_id(idev->dev_id);
7249
7250 if (!entry || !entry->devid_list)
7251 return 0;
7252
7253 devid_list = entry->devid_list;
7254 for (dv = devid_list; dv; dv = dv->next) {
7255 struct md_list *devlist;
7256 struct sys_dev *device = NULL;
7257 char *hpath;
7258 int found = 0;
7259
7260 if (cmp_hba_path)
7261 device = device_by_id_and_path(dv->devid, hba_path);
7262 else
7263 device = device_by_id(dv->devid);
7264
7265 if (device)
7266 hpath = device->path;
7267 else
7268 return 0;
7269
7270 devlist = get_devices(hpath);
7271 /* if no intel devices return zero volumes */
7272 if (devlist == NULL)
7273 return 0;
7274
7275 count += active_arrays_by_format("imsm", hpath, &devlist, dpa,
7276 verbose);
7277 dprintf("path: %s active arrays: %d\n", hpath, count);
7278 if (devlist == NULL)
7279 return 0;
7280 do {
7281 found = 0;
7282 count += count_volumes_list(devlist,
7283 NULL,
7284 verbose,
7285 &found);
7286 dprintf("found %d count: %d\n", found, count);
7287 } while (found);
7288
7289 dprintf("path: %s total number of volumes: %d\n", hpath, count);
7290
7291 while (devlist) {
7292 struct md_list *dv = devlist;
7293 devlist = devlist->next;
7294 free(dv->devname);
7295 free(dv);
7296 }
7297 }
7298 return count;
7299 }
7300
7301 static int count_volumes(struct intel_hba *hba, int dpa, int verbose)
7302 {
7303 if (!hba)
7304 return 0;
7305 if (hba->type == SYS_DEV_VMD) {
7306 struct sys_dev *dev;
7307 int count = 0;
7308
7309 for (dev = find_intel_devices(); dev; dev = dev->next) {
7310 if (dev->type == SYS_DEV_VMD)
7311 count += __count_volumes(dev->path, dpa,
7312 verbose, 1);
7313 }
7314 return count;
7315 }
7316 return __count_volumes(hba->path, dpa, verbose, 0);
7317 }
7318
7319 static int imsm_default_chunk(const struct imsm_orom *orom)
7320 {
7321 /* up to 512 if the plaform supports it, otherwise the platform max.
7322 * 128 if no platform detected
7323 */
7324 int fs = max(7, orom ? fls(orom->sss) : 0);
7325
7326 return min(512, (1 << fs));
7327 }
7328
7329 static int
7330 validate_geometry_imsm_orom(struct intel_super *super, int level, int layout,
7331 int raiddisks, int *chunk, unsigned long long size, int verbose)
7332 {
7333 /* check/set platform and metadata limits/defaults */
7334 if (super->orom && raiddisks > super->orom->dpa) {
7335 pr_vrb("platform supports a maximum of %d disks per array\n",
7336 super->orom->dpa);
7337 return 0;
7338 }
7339
7340 /* capabilities of OROM tested - copied from validate_geometry_imsm_volume */
7341 if (!is_raid_level_supported(super->orom, level, raiddisks)) {
7342 pr_vrb("platform does not support raid%d with %d disk%s\n",
7343 level, raiddisks, raiddisks > 1 ? "s" : "");
7344 return 0;
7345 }
7346
7347 if (*chunk == 0 || *chunk == UnSet)
7348 *chunk = imsm_default_chunk(super->orom);
7349
7350 if (super->orom && !imsm_orom_has_chunk(super->orom, *chunk)) {
7351 pr_vrb("platform does not support a chunk size of: %d\n", *chunk);
7352 return 0;
7353 }
7354
7355 if (layout != imsm_level_to_layout(level)) {
7356 if (level == 5)
7357 pr_vrb("imsm raid 5 only supports the left-asymmetric layout\n");
7358 else if (level == 10)
7359 pr_vrb("imsm raid 10 only supports the n2 layout\n");
7360 else
7361 pr_vrb("imsm unknown layout %#x for this raid level %d\n",
7362 layout, level);
7363 return 0;
7364 }
7365
7366 if (super->orom && (super->orom->attr & IMSM_OROM_ATTR_2TB) == 0 &&
7367 (calc_array_size(level, raiddisks, layout, *chunk, size) >> 32) > 0) {
7368 pr_vrb("platform does not support a volume size over 2TB\n");
7369 return 0;
7370 }
7371
7372 return 1;
7373 }
7374
7375 /* validate_geometry_imsm_volume - lifted from validate_geometry_ddf_bvd
7376 * FIX ME add ahci details
7377 */
7378 static int validate_geometry_imsm_volume(struct supertype *st, int level,
7379 int layout, int raiddisks, int *chunk,
7380 unsigned long long size,
7381 unsigned long long data_offset,
7382 char *dev,
7383 unsigned long long *freesize,
7384 int verbose)
7385 {
7386 dev_t rdev;
7387 struct intel_super *super = st->sb;
7388 struct imsm_super *mpb;
7389 struct dl *dl;
7390 unsigned long long pos = 0;
7391 unsigned long long maxsize;
7392 struct extent *e;
7393 int i;
7394
7395 /* We must have the container info already read in. */
7396 if (!super)
7397 return 0;
7398
7399 mpb = super->anchor;
7400
7401 if (!validate_geometry_imsm_orom(super, level, layout, raiddisks, chunk, size, verbose)) {
7402 pr_err("RAID geometry validation failed. Cannot proceed with the action(s).\n");
7403 return 0;
7404 }
7405 if (!dev) {
7406 /* General test: make sure there is space for
7407 * 'raiddisks' device extents of size 'size' at a given
7408 * offset
7409 */
7410 unsigned long long minsize = size;
7411 unsigned long long start_offset = MaxSector;
7412 int dcnt = 0;
7413 if (minsize == 0)
7414 minsize = MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS;
7415 for (dl = super->disks; dl ; dl = dl->next) {
7416 int found = 0;
7417
7418 pos = 0;
7419 i = 0;
7420 e = get_extents(super, dl, 0);
7421 if (!e) continue;
7422 do {
7423 unsigned long long esize;
7424 esize = e[i].start - pos;
7425 if (esize >= minsize)
7426 found = 1;
7427 if (found && start_offset == MaxSector) {
7428 start_offset = pos;
7429 break;
7430 } else if (found && pos != start_offset) {
7431 found = 0;
7432 break;
7433 }
7434 pos = e[i].start + e[i].size;
7435 i++;
7436 } while (e[i-1].size);
7437 if (found)
7438 dcnt++;
7439 free(e);
7440 }
7441 if (dcnt < raiddisks) {
7442 if (verbose)
7443 pr_err("imsm: Not enough devices with space for this array (%d < %d)\n",
7444 dcnt, raiddisks);
7445 return 0;
7446 }
7447 return 1;
7448 }
7449
7450 /* This device must be a member of the set */
7451 if (!stat_is_blkdev(dev, &rdev))
7452 return 0;
7453 for (dl = super->disks ; dl ; dl = dl->next) {
7454 if (dl->major == (int)major(rdev) &&
7455 dl->minor == (int)minor(rdev))
7456 break;
7457 }
7458 if (!dl) {
7459 if (verbose)
7460 pr_err("%s is not in the same imsm set\n", dev);
7461 return 0;
7462 } else if (super->orom && dl->index < 0 && mpb->num_raid_devs) {
7463 /* If a volume is present then the current creation attempt
7464 * cannot incorporate new spares because the orom may not
7465 * understand this configuration (all member disks must be
7466 * members of each array in the container).
7467 */
7468 pr_err("%s is a spare and a volume is already defined for this container\n", dev);
7469 pr_err("The option-rom requires all member disks to be a member of all volumes\n");
7470 return 0;
7471 } else if (super->orom && mpb->num_raid_devs > 0 &&
7472 mpb->num_disks != raiddisks) {
7473 pr_err("The option-rom requires all member disks to be a member of all volumes\n");
7474 return 0;
7475 }
7476
7477 /* retrieve the largest free space block */
7478 e = get_extents(super, dl, 0);
7479 maxsize = 0;
7480 i = 0;
7481 if (e) {
7482 do {
7483 unsigned long long esize;
7484
7485 esize = e[i].start - pos;
7486 if (esize >= maxsize)
7487 maxsize = esize;
7488 pos = e[i].start + e[i].size;
7489 i++;
7490 } while (e[i-1].size);
7491 dl->e = e;
7492 dl->extent_cnt = i;
7493 } else {
7494 if (verbose)
7495 pr_err("unable to determine free space for: %s\n",
7496 dev);
7497 return 0;
7498 }
7499 if (maxsize < size) {
7500 if (verbose)
7501 pr_err("%s not enough space (%llu < %llu)\n",
7502 dev, maxsize, size);
7503 return 0;
7504 }
7505
7506 /* count total number of extents for merge */
7507 i = 0;
7508 for (dl = super->disks; dl; dl = dl->next)
7509 if (dl->e)
7510 i += dl->extent_cnt;
7511
7512 maxsize = merge_extents(super, i);
7513
7514 if (mpb->num_raid_devs > 0 && size && size != maxsize)
7515 pr_err("attempting to create a second volume with size less then remaining space.\n");
7516
7517 if (maxsize < size || maxsize == 0) {
7518 if (verbose) {
7519 if (maxsize == 0)
7520 pr_err("no free space left on device. Aborting...\n");
7521 else
7522 pr_err("not enough space to create volume of given size (%llu < %llu). Aborting...\n",
7523 maxsize, size);
7524 }
7525 return 0;
7526 }
7527
7528 *freesize = maxsize;
7529
7530 if (super->orom) {
7531 int count = count_volumes(super->hba,
7532 super->orom->dpa, verbose);
7533 if (super->orom->vphba <= count) {
7534 pr_vrb("platform does not support more than %d raid volumes.\n",
7535 super->orom->vphba);
7536 return 0;
7537 }
7538 }
7539 return 1;
7540 }
7541
7542 /**
7543 * imsm_get_free_size() - get the biggest, common free space from members.
7544 * @super: &intel_super pointer, not NULL.
7545 * @raiddisks: number of raid disks.
7546 * @size: requested size, could be 0 (means max size).
7547 * @chunk: requested chunk.
7548 * @freesize: pointer for returned size value.
7549 *
7550 * Return: &IMSM_STATUS_OK or &IMSM_STATUS_ERROR.
7551 *
7552 * @freesize is set to meaningful value, this can be @size, or calculated
7553 * max free size.
7554 * super->create_offset value is modified and set appropriately in
7555 * merge_extends() for further creation.
7556 */
7557 static imsm_status_t imsm_get_free_size(struct intel_super *super,
7558 const int raiddisks,
7559 unsigned long long size,
7560 const int chunk,
7561 unsigned long long *freesize)
7562 {
7563 struct imsm_super *mpb = super->anchor;
7564 struct dl *dl;
7565 int i;
7566 int extent_cnt;
7567 struct extent *e;
7568 unsigned long long maxsize;
7569 unsigned long long minsize;
7570 int cnt;
7571 int used;
7572
7573 /* find the largest common start free region of the possible disks */
7574 used = 0;
7575 extent_cnt = 0;
7576 cnt = 0;
7577 for (dl = super->disks; dl; dl = dl->next) {
7578 dl->raiddisk = -1;
7579
7580 if (dl->index >= 0)
7581 used++;
7582
7583 /* don't activate new spares if we are orom constrained
7584 * and there is already a volume active in the container
7585 */
7586 if (super->orom && dl->index < 0 && mpb->num_raid_devs)
7587 continue;
7588
7589 e = get_extents(super, dl, 0);
7590 if (!e)
7591 continue;
7592 for (i = 1; e[i-1].size; i++)
7593 ;
7594 dl->e = e;
7595 dl->extent_cnt = i;
7596 extent_cnt += i;
7597 cnt++;
7598 }
7599
7600 maxsize = merge_extents(super, extent_cnt);
7601 minsize = size;
7602 if (size == 0)
7603 /* chunk is in K */
7604 minsize = chunk * 2;
7605
7606 if (cnt < raiddisks || (super->orom && used && used != raiddisks) ||
7607 maxsize < minsize || maxsize == 0) {
7608 pr_err("not enough devices with space to create array.\n");
7609 return IMSM_STATUS_ERROR;
7610 }
7611
7612 if (size == 0) {
7613 size = maxsize;
7614 if (chunk) {
7615 size /= 2 * chunk;
7616 size *= 2 * chunk;
7617 }
7618 maxsize = size;
7619 }
7620 if (mpb->num_raid_devs > 0 && size && size != maxsize)
7621 pr_err("attempting to create a second volume with size less then remaining space.\n");
7622 *freesize = size;
7623
7624 dprintf("imsm: imsm_get_free_size() returns : %llu\n", size);
7625
7626 return IMSM_STATUS_OK;
7627 }
7628
7629 /**
7630 * autolayout_imsm() - automatically layout a new volume.
7631 * @super: &intel_super pointer, not NULL.
7632 * @raiddisks: number of raid disks.
7633 * @size: requested size, could be 0 (means max size).
7634 * @chunk: requested chunk.
7635 * @freesize: pointer for returned size value.
7636 *
7637 * We are being asked to automatically layout a new volume based on the current
7638 * contents of the container. If the parameters can be satisfied autolayout_imsm
7639 * will record the disks, start offset, and will return size of the volume to
7640 * be created. See imsm_get_free_size() for details.
7641 * add_to_super() and getinfo_super() detect when autolayout is in progress.
7642 * If first volume exists, slots are set consistently to it.
7643 *
7644 * Return: &IMSM_STATUS_OK on success, &IMSM_STATUS_ERROR otherwise.
7645 *
7646 * Disks are marked for creation via dl->raiddisk.
7647 */
7648 static imsm_status_t autolayout_imsm(struct intel_super *super,
7649 const int raiddisks,
7650 unsigned long long size, const int chunk,
7651 unsigned long long *freesize)
7652 {
7653 int curr_slot = 0;
7654 struct dl *disk;
7655 int vol_cnt = super->anchor->num_raid_devs;
7656 imsm_status_t rv;
7657
7658 rv = imsm_get_free_size(super, raiddisks, size, chunk, freesize);
7659 if (rv != IMSM_STATUS_OK)
7660 return IMSM_STATUS_ERROR;
7661
7662 for (disk = super->disks; disk; disk = disk->next) {
7663 if (!disk->e)
7664 continue;
7665
7666 if (curr_slot == raiddisks)
7667 break;
7668
7669 if (vol_cnt == 0) {
7670 disk->raiddisk = curr_slot;
7671 } else {
7672 int _slot = get_disk_slot_in_dev(super, 0, disk->index);
7673
7674 if (_slot == -1) {
7675 pr_err("Disk %s is not used in first volume, aborting\n",
7676 disk->devname);
7677 return IMSM_STATUS_ERROR;
7678 }
7679 disk->raiddisk = _slot;
7680 }
7681 curr_slot++;
7682 }
7683
7684 return IMSM_STATUS_OK;
7685 }
7686
7687 static int validate_geometry_imsm(struct supertype *st, int level, int layout,
7688 int raiddisks, int *chunk, unsigned long long size,
7689 unsigned long long data_offset,
7690 char *dev, unsigned long long *freesize,
7691 int consistency_policy, int verbose)
7692 {
7693 int fd, cfd;
7694 struct mdinfo *sra;
7695 int is_member = 0;
7696
7697 /* load capability
7698 * if given unused devices create a container
7699 * if given given devices in a container create a member volume
7700 */
7701 if (is_container(level))
7702 /* Must be a fresh device to add to a container */
7703 return validate_geometry_imsm_container(st, level, raiddisks,
7704 data_offset, dev,
7705 freesize, verbose);
7706
7707 /*
7708 * Size is given in sectors.
7709 */
7710 if (size && (size < 2048)) {
7711 pr_err("Given size must be greater than 1M.\n");
7712 /* Depends on algorithm in Create.c :
7713 * if container was given (dev == NULL) return -1,
7714 * if block device was given ( dev != NULL) return 0.
7715 */
7716 return dev ? -1 : 0;
7717 }
7718
7719 if (!dev) {
7720 struct intel_super *super = st->sb;
7721
7722 /*
7723 * Autolayout mode, st->sb must be set.
7724 */
7725 if (!super) {
7726 pr_vrb("superblock must be set for autolayout, aborting\n");
7727 return 0;
7728 }
7729
7730 if (!validate_geometry_imsm_orom(st->sb, level, layout,
7731 raiddisks, chunk, size,
7732 verbose))
7733 return 0;
7734
7735 if (super->orom && freesize) {
7736 imsm_status_t rv;
7737 int count = count_volumes(super->hba, super->orom->dpa,
7738 verbose);
7739 if (super->orom->vphba <= count) {
7740 pr_vrb("platform does not support more than %d raid volumes.\n",
7741 super->orom->vphba);
7742 return 0;
7743 }
7744
7745 rv = autolayout_imsm(super, raiddisks, size, *chunk,
7746 freesize);
7747 if (rv != IMSM_STATUS_OK)
7748 return 0;
7749 }
7750 return 1;
7751 }
7752 if (st->sb) {
7753 /* creating in a given container */
7754 return validate_geometry_imsm_volume(st, level, layout,
7755 raiddisks, chunk, size,
7756 data_offset,
7757 dev, freesize, verbose);
7758 }
7759
7760 /* This device needs to be a device in an 'imsm' container */
7761 fd = open(dev, O_RDONLY|O_EXCL, 0);
7762
7763 if (is_fd_valid(fd)) {
7764 pr_vrb("Cannot create this array on device %s\n", dev);
7765 close(fd);
7766 return 0;
7767 }
7768 if (errno == EBUSY)
7769 fd = open(dev, O_RDONLY, 0);
7770
7771 if (!is_fd_valid(fd)) {
7772 pr_vrb("Cannot open %s: %s\n", dev, strerror(errno));
7773 return 0;
7774 }
7775
7776 /* Well, it is in use by someone, maybe an 'imsm' container. */
7777 cfd = open_container(fd);
7778 close_fd(&fd);
7779
7780 if (!is_fd_valid(cfd)) {
7781 pr_vrb("Cannot use %s: It is busy\n", dev);
7782 return 0;
7783 }
7784 sra = sysfs_read(cfd, NULL, GET_VERSION);
7785 if (sra && sra->array.major_version == -1 &&
7786 strcmp(sra->text_version, "imsm") == 0)
7787 is_member = 1;
7788 sysfs_free(sra);
7789 if (is_member) {
7790 /* This is a member of a imsm container. Load the container
7791 * and try to create a volume
7792 */
7793 struct intel_super *super;
7794
7795 if (load_super_imsm_all(st, cfd, (void **) &super, NULL, NULL, 1) == 0) {
7796 st->sb = super;
7797 strcpy(st->container_devnm, fd2devnm(cfd));
7798 close(cfd);
7799 return validate_geometry_imsm_volume(st, level, layout,
7800 raiddisks, chunk,
7801 size, data_offset, dev,
7802 freesize, 1)
7803 ? 1 : -1;
7804 }
7805 }
7806
7807 if (verbose)
7808 pr_err("failed container membership check\n");
7809
7810 close(cfd);
7811 return 0;
7812 }
7813
7814 static void default_geometry_imsm(struct supertype *st, int *level, int *layout, int *chunk)
7815 {
7816 struct intel_super *super = st->sb;
7817
7818 if (level && *level == UnSet)
7819 *level = LEVEL_CONTAINER;
7820
7821 if (level && layout && *layout == UnSet)
7822 *layout = imsm_level_to_layout(*level);
7823
7824 if (chunk && (*chunk == UnSet || *chunk == 0))
7825 *chunk = imsm_default_chunk(super->orom);
7826 }
7827
7828 static void handle_missing(struct intel_super *super, struct imsm_dev *dev);
7829
7830 static int kill_subarray_imsm(struct supertype *st, char *subarray_id)
7831 {
7832 /* remove the subarray currently referenced by subarray_id */
7833 __u8 i;
7834 struct intel_dev **dp;
7835 struct intel_super *super = st->sb;
7836 __u8 current_vol = strtoul(subarray_id, NULL, 10);
7837 struct imsm_super *mpb = super->anchor;
7838
7839 if (mpb->num_raid_devs == 0)
7840 return 2;
7841
7842 /* block deletions that would change the uuid of active subarrays
7843 *
7844 * FIXME when immutable ids are available, but note that we'll
7845 * also need to fixup the invalidated/active subarray indexes in
7846 * mdstat
7847 */
7848 for (i = 0; i < mpb->num_raid_devs; i++) {
7849 char subarray[4];
7850
7851 if (i < current_vol)
7852 continue;
7853 sprintf(subarray, "%u", i);
7854 if (is_subarray_active(subarray, st->devnm)) {
7855 pr_err("deleting subarray-%d would change the UUID of active subarray-%d, aborting\n",
7856 current_vol, i);
7857
7858 return 2;
7859 }
7860 }
7861
7862 if (st->update_tail) {
7863 struct imsm_update_kill_array *u = xmalloc(sizeof(*u));
7864
7865 u->type = update_kill_array;
7866 u->dev_idx = current_vol;
7867 append_metadata_update(st, u, sizeof(*u));
7868
7869 return 0;
7870 }
7871
7872 for (dp = &super->devlist; *dp;)
7873 if ((*dp)->index == current_vol) {
7874 *dp = (*dp)->next;
7875 } else {
7876 handle_missing(super, (*dp)->dev);
7877 if ((*dp)->index > current_vol)
7878 (*dp)->index--;
7879 dp = &(*dp)->next;
7880 }
7881
7882 /* no more raid devices, all active components are now spares,
7883 * but of course failed are still failed
7884 */
7885 if (--mpb->num_raid_devs == 0) {
7886 struct dl *d;
7887
7888 for (d = super->disks; d; d = d->next)
7889 if (d->index > -2)
7890 mark_spare(d);
7891 }
7892
7893 super->updates_pending++;
7894
7895 return 0;
7896 }
7897
7898 /**
7899 * get_rwh_policy_from_update() - Get the rwh policy for update option.
7900 * @update: Update option.
7901 */
7902 static int get_rwh_policy_from_update(enum update_opt update)
7903 {
7904 switch (update) {
7905 case UOPT_PPL:
7906 return RWH_MULTIPLE_DISTRIBUTED;
7907 case UOPT_NO_PPL:
7908 return RWH_MULTIPLE_OFF;
7909 case UOPT_BITMAP:
7910 return RWH_BITMAP;
7911 case UOPT_NO_BITMAP:
7912 return RWH_OFF;
7913 default:
7914 break;
7915 }
7916 return UOPT_UNDEFINED;
7917 }
7918
7919 static int update_subarray_imsm(struct supertype *st, char *subarray,
7920 enum update_opt update, struct mddev_ident *ident)
7921 {
7922 /* update the subarray currently referenced by ->current_vol */
7923 struct intel_super *super = st->sb;
7924 struct imsm_super *mpb = super->anchor;
7925
7926 if (update == UOPT_NAME) {
7927 char *name = ident->name;
7928 char *ep;
7929 int vol;
7930
7931 if (!check_name(super, name, 0))
7932 return 2;
7933
7934 vol = strtoul(subarray, &ep, 10);
7935 if (*ep != '\0' || vol >= super->anchor->num_raid_devs)
7936 return 2;
7937
7938 if (st->update_tail) {
7939 struct imsm_update_rename_array *u = xmalloc(sizeof(*u));
7940
7941 u->type = update_rename_array;
7942 u->dev_idx = vol;
7943 strncpy((char *) u->name, name, MAX_RAID_SERIAL_LEN);
7944 u->name[MAX_RAID_SERIAL_LEN-1] = '\0';
7945 append_metadata_update(st, u, sizeof(*u));
7946 } else {
7947 struct imsm_dev *dev;
7948 int i, namelen;
7949
7950 dev = get_imsm_dev(super, vol);
7951 memset(dev->volume, '\0', MAX_RAID_SERIAL_LEN);
7952 namelen = min((int)strlen(name), MAX_RAID_SERIAL_LEN);
7953 memcpy(dev->volume, name, namelen);
7954 for (i = 0; i < mpb->num_raid_devs; i++) {
7955 dev = get_imsm_dev(super, i);
7956 handle_missing(super, dev);
7957 }
7958 super->updates_pending++;
7959 }
7960 } else if (get_rwh_policy_from_update(update) != UOPT_UNDEFINED) {
7961 int new_policy;
7962 char *ep;
7963 int vol = strtoul(subarray, &ep, 10);
7964
7965 if (*ep != '\0' || vol >= super->anchor->num_raid_devs)
7966 return 2;
7967
7968 new_policy = get_rwh_policy_from_update(update);
7969
7970 if (st->update_tail) {
7971 struct imsm_update_rwh_policy *u = xmalloc(sizeof(*u));
7972
7973 u->type = update_rwh_policy;
7974 u->dev_idx = vol;
7975 u->new_policy = new_policy;
7976 append_metadata_update(st, u, sizeof(*u));
7977 } else {
7978 struct imsm_dev *dev;
7979
7980 dev = get_imsm_dev(super, vol);
7981 dev->rwh_policy = new_policy;
7982 super->updates_pending++;
7983 }
7984 if (new_policy == RWH_BITMAP)
7985 return write_init_bitmap_imsm_vol(st, vol);
7986 } else
7987 return 2;
7988
7989 return 0;
7990 }
7991
7992 static bool is_gen_migration(struct imsm_dev *dev)
7993 {
7994 if (dev && dev->vol.migr_state &&
7995 migr_type(dev) == MIGR_GEN_MIGR)
7996 return true;
7997
7998 return false;
7999 }
8000
8001 static int is_rebuilding(struct imsm_dev *dev)
8002 {
8003 struct imsm_map *migr_map;
8004
8005 if (!dev->vol.migr_state)
8006 return 0;
8007
8008 if (migr_type(dev) != MIGR_REBUILD)
8009 return 0;
8010
8011 migr_map = get_imsm_map(dev, MAP_1);
8012
8013 if (migr_map->map_state == IMSM_T_STATE_DEGRADED)
8014 return 1;
8015 else
8016 return 0;
8017 }
8018
8019 static int is_initializing(struct imsm_dev *dev)
8020 {
8021 struct imsm_map *migr_map;
8022
8023 if (!dev->vol.migr_state)
8024 return 0;
8025
8026 if (migr_type(dev) != MIGR_INIT)
8027 return 0;
8028
8029 migr_map = get_imsm_map(dev, MAP_1);
8030
8031 if (migr_map->map_state == IMSM_T_STATE_UNINITIALIZED)
8032 return 1;
8033
8034 return 0;
8035 }
8036
8037 static void update_recovery_start(struct intel_super *super,
8038 struct imsm_dev *dev,
8039 struct mdinfo *array)
8040 {
8041 struct mdinfo *rebuild = NULL;
8042 struct mdinfo *d;
8043 __u32 units;
8044
8045 if (!is_rebuilding(dev))
8046 return;
8047
8048 /* Find the rebuild target, but punt on the dual rebuild case */
8049 for (d = array->devs; d; d = d->next)
8050 if (d->recovery_start == 0) {
8051 if (rebuild)
8052 return;
8053 rebuild = d;
8054 }
8055
8056 if (!rebuild) {
8057 /* (?) none of the disks are marked with
8058 * IMSM_ORD_REBUILD, so assume they are missing and the
8059 * disk_ord_tbl was not correctly updated
8060 */
8061 dprintf("failed to locate out-of-sync disk\n");
8062 return;
8063 }
8064
8065 units = vol_curr_migr_unit(dev);
8066 rebuild->recovery_start = units * blocks_per_migr_unit(super, dev);
8067 }
8068
8069 static int recover_backup_imsm(struct supertype *st, struct mdinfo *info);
8070
8071 static struct mdinfo *container_content_imsm(struct supertype *st, char *subarray)
8072 {
8073 /* Given a container loaded by load_super_imsm_all,
8074 * extract information about all the arrays into
8075 * an mdinfo tree.
8076 * If 'subarray' is given, just extract info about that array.
8077 *
8078 * For each imsm_dev create an mdinfo, fill it in,
8079 * then look for matching devices in super->disks
8080 * and create appropriate device mdinfo.
8081 */
8082 struct intel_super *super = st->sb;
8083 struct imsm_super *mpb = super->anchor;
8084 struct mdinfo *rest = NULL;
8085 unsigned int i;
8086 int sb_errors = 0;
8087 struct dl *d;
8088 int spare_disks = 0;
8089 int current_vol = super->current_vol;
8090
8091 /* do not assemble arrays when not all attributes are supported */
8092 if (imsm_check_attributes(mpb->attributes) == 0) {
8093 sb_errors = 1;
8094 pr_err("Unsupported attributes in IMSM metadata.Arrays activation is blocked.\n");
8095 }
8096
8097 /* count spare devices, not used in maps
8098 */
8099 for (d = super->disks; d; d = d->next)
8100 if (d->index == -1)
8101 spare_disks++;
8102
8103 for (i = 0; i < mpb->num_raid_devs; i++) {
8104 struct imsm_dev *dev;
8105 struct imsm_map *map;
8106 struct imsm_map *map2;
8107 struct mdinfo *this;
8108 int slot;
8109 int chunk;
8110 char *ep;
8111 int level;
8112
8113 if (subarray &&
8114 (i != strtoul(subarray, &ep, 10) || *ep != '\0'))
8115 continue;
8116
8117 dev = get_imsm_dev(super, i);
8118 map = get_imsm_map(dev, MAP_0);
8119 map2 = get_imsm_map(dev, MAP_1);
8120 level = get_imsm_raid_level(map);
8121
8122 /* do not publish arrays that are in the middle of an
8123 * unsupported migration
8124 */
8125 if (dev->vol.migr_state &&
8126 (migr_type(dev) == MIGR_STATE_CHANGE)) {
8127 pr_err("cannot assemble volume '%.16s': unsupported migration in progress\n",
8128 dev->volume);
8129 continue;
8130 }
8131 /* do not publish arrays that are not support by controller's
8132 * OROM/EFI
8133 */
8134
8135 this = xmalloc(sizeof(*this));
8136
8137 super->current_vol = i;
8138 getinfo_super_imsm_volume(st, this, NULL);
8139 this->next = rest;
8140 chunk = __le16_to_cpu(map->blocks_per_strip) >> 1;
8141 /* mdadm does not support all metadata features- set the bit in all arrays state */
8142 if (!validate_geometry_imsm_orom(super,
8143 level, /* RAID level */
8144 imsm_level_to_layout(level),
8145 map->num_members, /* raid disks */
8146 &chunk, imsm_dev_size(dev),
8147 1 /* verbose */)) {
8148 pr_err("IMSM RAID geometry validation failed. Array %s activation is blocked.\n",
8149 dev->volume);
8150 this->array.state |=
8151 (1<<MD_SB_BLOCK_CONTAINER_RESHAPE) |
8152 (1<<MD_SB_BLOCK_VOLUME);
8153 }
8154
8155 /* if array has bad blocks, set suitable bit in all arrays state */
8156 if (sb_errors)
8157 this->array.state |=
8158 (1<<MD_SB_BLOCK_CONTAINER_RESHAPE) |
8159 (1<<MD_SB_BLOCK_VOLUME);
8160
8161 for (slot = 0 ; slot < map->num_members; slot++) {
8162 unsigned long long recovery_start;
8163 struct mdinfo *info_d;
8164 struct dl *d;
8165 int idx;
8166 int skip;
8167 __u32 ord;
8168 int missing = 0;
8169
8170 skip = 0;
8171 idx = get_imsm_disk_idx(dev, slot, MAP_0);
8172 ord = get_imsm_ord_tbl_ent(dev, slot, MAP_X);
8173 for (d = super->disks; d ; d = d->next)
8174 if (d->index == idx)
8175 break;
8176
8177 recovery_start = MaxSector;
8178 if (d == NULL)
8179 skip = 1;
8180 if (d && is_failed(&d->disk))
8181 skip = 1;
8182 if (!skip && (ord & IMSM_ORD_REBUILD))
8183 recovery_start = 0;
8184 if (!(ord & IMSM_ORD_REBUILD))
8185 this->array.working_disks++;
8186 /*
8187 * if we skip some disks the array will be assmebled degraded;
8188 * reset resync start to avoid a dirty-degraded
8189 * situation when performing the intial sync
8190 */
8191 if (skip)
8192 missing++;
8193
8194 if (!(dev->vol.dirty & RAIDVOL_DIRTY)) {
8195 if ((!able_to_resync(level, missing) ||
8196 recovery_start == 0))
8197 this->resync_start = MaxSector;
8198 }
8199
8200 if (skip)
8201 continue;
8202
8203 info_d = xcalloc(1, sizeof(*info_d));
8204 info_d->next = this->devs;
8205 this->devs = info_d;
8206
8207 info_d->disk.number = d->index;
8208 info_d->disk.major = d->major;
8209 info_d->disk.minor = d->minor;
8210 info_d->disk.raid_disk = slot;
8211 info_d->recovery_start = recovery_start;
8212 if (map2) {
8213 if (slot < map2->num_members)
8214 info_d->disk.state = (1 << MD_DISK_ACTIVE);
8215 else
8216 this->array.spare_disks++;
8217 } else {
8218 if (slot < map->num_members)
8219 info_d->disk.state = (1 << MD_DISK_ACTIVE);
8220 else
8221 this->array.spare_disks++;
8222 }
8223
8224 info_d->events = __le32_to_cpu(mpb->generation_num);
8225 info_d->data_offset = pba_of_lba0(map);
8226 info_d->component_size = calc_component_size(map, dev);
8227
8228 if (map->raid_level == 5) {
8229 info_d->ppl_sector = this->ppl_sector;
8230 info_d->ppl_size = this->ppl_size;
8231 if (this->consistency_policy == CONSISTENCY_POLICY_PPL &&
8232 recovery_start == 0)
8233 this->resync_start = 0;
8234 }
8235
8236 info_d->bb.supported = 1;
8237 get_volume_badblocks(super->bbm_log, ord_to_idx(ord),
8238 info_d->data_offset,
8239 info_d->component_size,
8240 &info_d->bb);
8241 }
8242 /* now that the disk list is up-to-date fixup recovery_start */
8243 update_recovery_start(super, dev, this);
8244 this->array.spare_disks += spare_disks;
8245
8246 /* check for reshape */
8247 if (this->reshape_active == 1)
8248 recover_backup_imsm(st, this);
8249 rest = this;
8250 }
8251
8252 super->current_vol = current_vol;
8253 return rest;
8254 }
8255
8256 static __u8 imsm_check_degraded(struct intel_super *super, struct imsm_dev *dev,
8257 int failed, int look_in_map)
8258 {
8259 struct imsm_map *map;
8260
8261 map = get_imsm_map(dev, look_in_map);
8262
8263 if (!failed)
8264 return map->map_state == IMSM_T_STATE_UNINITIALIZED ?
8265 IMSM_T_STATE_UNINITIALIZED : IMSM_T_STATE_NORMAL;
8266
8267 switch (get_imsm_raid_level(map)) {
8268 case 0:
8269 return IMSM_T_STATE_FAILED;
8270 break;
8271 case 1:
8272 if (failed < map->num_members)
8273 return IMSM_T_STATE_DEGRADED;
8274 else
8275 return IMSM_T_STATE_FAILED;
8276 break;
8277 case 10:
8278 {
8279 /**
8280 * check to see if any mirrors have failed, otherwise we
8281 * are degraded. Even numbered slots are mirrored on
8282 * slot+1
8283 */
8284 int i;
8285 /* gcc -Os complains that this is unused */
8286 int insync = insync;
8287
8288 for (i = 0; i < map->num_members; i++) {
8289 __u32 ord = get_imsm_ord_tbl_ent(dev, i, MAP_X);
8290 int idx = ord_to_idx(ord);
8291 struct imsm_disk *disk;
8292
8293 /* reset the potential in-sync count on even-numbered
8294 * slots. num_copies is always 2 for imsm raid10
8295 */
8296 if ((i & 1) == 0)
8297 insync = 2;
8298
8299 disk = get_imsm_disk(super, idx);
8300 if (!disk || is_failed(disk) || ord & IMSM_ORD_REBUILD)
8301 insync--;
8302
8303 /* no in-sync disks left in this mirror the
8304 * array has failed
8305 */
8306 if (insync == 0)
8307 return IMSM_T_STATE_FAILED;
8308 }
8309
8310 return IMSM_T_STATE_DEGRADED;
8311 }
8312 case 5:
8313 if (failed < 2)
8314 return IMSM_T_STATE_DEGRADED;
8315 else
8316 return IMSM_T_STATE_FAILED;
8317 break;
8318 default:
8319 break;
8320 }
8321
8322 return map->map_state;
8323 }
8324
8325 static int imsm_count_failed(struct intel_super *super, struct imsm_dev *dev,
8326 int look_in_map)
8327 {
8328 int i;
8329 int failed = 0;
8330 struct imsm_disk *disk;
8331 struct imsm_map *map = get_imsm_map(dev, MAP_0);
8332 struct imsm_map *prev = get_imsm_map(dev, MAP_1);
8333 struct imsm_map *map_for_loop;
8334 __u32 ord;
8335 int idx;
8336 int idx_1;
8337
8338 /* at the beginning of migration we set IMSM_ORD_REBUILD on
8339 * disks that are being rebuilt. New failures are recorded to
8340 * map[0]. So we look through all the disks we started with and
8341 * see if any failures are still present, or if any new ones
8342 * have arrived
8343 */
8344 map_for_loop = map;
8345 if (prev && (map->num_members < prev->num_members))
8346 map_for_loop = prev;
8347
8348 for (i = 0; i < map_for_loop->num_members; i++) {
8349 idx_1 = -255;
8350 /* when MAP_X is passed both maps failures are counted
8351 */
8352 if (prev &&
8353 (look_in_map == MAP_1 || look_in_map == MAP_X) &&
8354 i < prev->num_members) {
8355 ord = __le32_to_cpu(prev->disk_ord_tbl[i]);
8356 idx_1 = ord_to_idx(ord);
8357
8358 disk = get_imsm_disk(super, idx_1);
8359 if (!disk || is_failed(disk) || ord & IMSM_ORD_REBUILD)
8360 failed++;
8361 }
8362 if ((look_in_map == MAP_0 || look_in_map == MAP_X) &&
8363 i < map->num_members) {
8364 ord = __le32_to_cpu(map->disk_ord_tbl[i]);
8365 idx = ord_to_idx(ord);
8366
8367 if (idx != idx_1) {
8368 disk = get_imsm_disk(super, idx);
8369 if (!disk || is_failed(disk) ||
8370 ord & IMSM_ORD_REBUILD)
8371 failed++;
8372 }
8373 }
8374 }
8375
8376 return failed;
8377 }
8378
8379 static int imsm_open_new(struct supertype *c, struct active_array *a,
8380 int inst)
8381 {
8382 struct intel_super *super = c->sb;
8383 struct imsm_super *mpb = super->anchor;
8384 struct imsm_update_prealloc_bb_mem u;
8385
8386 if (inst >= mpb->num_raid_devs) {
8387 pr_err("subarry index %d, out of range\n", inst);
8388 return -ENODEV;
8389 }
8390
8391 dprintf("imsm: open_new %d\n", inst);
8392 a->info.container_member = inst;
8393
8394 u.type = update_prealloc_badblocks_mem;
8395 imsm_update_metadata_locally(c, &u, sizeof(u));
8396
8397 return 0;
8398 }
8399
8400 static int is_resyncing(struct imsm_dev *dev)
8401 {
8402 struct imsm_map *migr_map;
8403
8404 if (!dev->vol.migr_state)
8405 return 0;
8406
8407 if (migr_type(dev) == MIGR_INIT ||
8408 migr_type(dev) == MIGR_REPAIR)
8409 return 1;
8410
8411 if (migr_type(dev) == MIGR_GEN_MIGR)
8412 return 0;
8413
8414 migr_map = get_imsm_map(dev, MAP_1);
8415
8416 if (migr_map->map_state == IMSM_T_STATE_NORMAL &&
8417 dev->vol.migr_type != MIGR_GEN_MIGR)
8418 return 1;
8419 else
8420 return 0;
8421 }
8422
8423 /* return true if we recorded new information */
8424 static int mark_failure(struct intel_super *super,
8425 struct imsm_dev *dev, struct imsm_disk *disk, int idx)
8426 {
8427 __u32 ord;
8428 int slot;
8429 struct imsm_map *map;
8430 char buf[MAX_RAID_SERIAL_LEN+3];
8431 unsigned int len, shift = 0;
8432
8433 /* new failures are always set in map[0] */
8434 map = get_imsm_map(dev, MAP_0);
8435
8436 slot = get_imsm_disk_slot(map, idx);
8437 if (slot < 0)
8438 return 0;
8439
8440 ord = __le32_to_cpu(map->disk_ord_tbl[slot]);
8441 if (is_failed(disk) && (ord & IMSM_ORD_REBUILD))
8442 return 0;
8443
8444 memcpy(buf, disk->serial, MAX_RAID_SERIAL_LEN);
8445 buf[MAX_RAID_SERIAL_LEN] = '\000';
8446 strcat(buf, ":0");
8447 if ((len = strlen(buf)) >= MAX_RAID_SERIAL_LEN)
8448 shift = len - MAX_RAID_SERIAL_LEN + 1;
8449 memcpy(disk->serial, &buf[shift], len + 1 - shift);
8450
8451 disk->status |= FAILED_DISK;
8452 set_imsm_ord_tbl_ent(map, slot, idx | IMSM_ORD_REBUILD);
8453 /* mark failures in second map if second map exists and this disk
8454 * in this slot.
8455 * This is valid for migration, initialization and rebuild
8456 */
8457 if (dev->vol.migr_state) {
8458 struct imsm_map *map2 = get_imsm_map(dev, MAP_1);
8459 int slot2 = get_imsm_disk_slot(map2, idx);
8460
8461 if (slot2 < map2->num_members && slot2 >= 0)
8462 set_imsm_ord_tbl_ent(map2, slot2,
8463 idx | IMSM_ORD_REBUILD);
8464 }
8465 if (map->failed_disk_num == 0xff ||
8466 (!is_rebuilding(dev) && map->failed_disk_num > slot))
8467 map->failed_disk_num = slot;
8468
8469 clear_disk_badblocks(super->bbm_log, ord_to_idx(ord));
8470
8471 return 1;
8472 }
8473
8474 static void mark_missing(struct intel_super *super,
8475 struct imsm_dev *dev, struct imsm_disk *disk, int idx)
8476 {
8477 mark_failure(super, dev, disk, idx);
8478
8479 if (disk->scsi_id == __cpu_to_le32(~(__u32)0))
8480 return;
8481
8482 disk->scsi_id = __cpu_to_le32(~(__u32)0);
8483 memmove(&disk->serial[0], &disk->serial[1], MAX_RAID_SERIAL_LEN - 1);
8484 }
8485
8486 static void handle_missing(struct intel_super *super, struct imsm_dev *dev)
8487 {
8488 struct dl *dl;
8489
8490 if (!super->missing)
8491 return;
8492
8493 /* When orom adds replacement for missing disk it does
8494 * not remove entry of missing disk, but just updates map with
8495 * new added disk. So it is not enough just to test if there is
8496 * any missing disk, we have to look if there are any failed disks
8497 * in map to stop migration */
8498
8499 dprintf("imsm: mark missing\n");
8500 /* end process for initialization and rebuild only
8501 */
8502 if (is_gen_migration(dev) == false) {
8503 int failed = imsm_count_failed(super, dev, MAP_0);
8504
8505 if (failed) {
8506 __u8 map_state;
8507 struct imsm_map *map = get_imsm_map(dev, MAP_0);
8508 struct imsm_map *map1;
8509 int i, ord, ord_map1;
8510 int rebuilt = 1;
8511
8512 for (i = 0; i < map->num_members; i++) {
8513 ord = get_imsm_ord_tbl_ent(dev, i, MAP_0);
8514 if (!(ord & IMSM_ORD_REBUILD))
8515 continue;
8516
8517 map1 = get_imsm_map(dev, MAP_1);
8518 if (!map1)
8519 continue;
8520
8521 ord_map1 = __le32_to_cpu(map1->disk_ord_tbl[i]);
8522 if (ord_map1 & IMSM_ORD_REBUILD)
8523 rebuilt = 0;
8524 }
8525
8526 if (rebuilt) {
8527 map_state = imsm_check_degraded(super, dev,
8528 failed, MAP_0);
8529 end_migration(dev, super, map_state);
8530 }
8531 }
8532 }
8533 for (dl = super->missing; dl; dl = dl->next)
8534 mark_missing(super, dev, &dl->disk, dl->index);
8535 super->updates_pending++;
8536 }
8537
8538 static unsigned long long imsm_set_array_size(struct imsm_dev *dev,
8539 long long new_size)
8540 {
8541 unsigned long long array_blocks;
8542 struct imsm_map *map = get_imsm_map(dev, MAP_0);
8543 int used_disks = imsm_num_data_members(map);
8544
8545 if (used_disks == 0) {
8546 /* when problems occures
8547 * return current array_blocks value
8548 */
8549 array_blocks = imsm_dev_size(dev);
8550
8551 return array_blocks;
8552 }
8553
8554 /* set array size in metadata
8555 */
8556 if (new_size <= 0)
8557 /* OLCE size change is caused by added disks
8558 */
8559 array_blocks = per_dev_array_size(map) * used_disks;
8560 else
8561 /* Online Volume Size Change
8562 * Using available free space
8563 */
8564 array_blocks = new_size;
8565
8566 array_blocks = round_size_to_mb(array_blocks, used_disks);
8567 set_imsm_dev_size(dev, array_blocks);
8568
8569 return array_blocks;
8570 }
8571
8572 static void imsm_set_disk(struct active_array *a, int n, int state);
8573
8574 static void imsm_progress_container_reshape(struct intel_super *super)
8575 {
8576 /* if no device has a migr_state, but some device has a
8577 * different number of members than the previous device, start
8578 * changing the number of devices in this device to match
8579 * previous.
8580 */
8581 struct imsm_super *mpb = super->anchor;
8582 int prev_disks = -1;
8583 int i;
8584 int copy_map_size;
8585
8586 for (i = 0; i < mpb->num_raid_devs; i++) {
8587 struct imsm_dev *dev = get_imsm_dev(super, i);
8588 struct imsm_map *map = get_imsm_map(dev, MAP_0);
8589 struct imsm_map *map2;
8590 int prev_num_members;
8591
8592 if (dev->vol.migr_state)
8593 return;
8594
8595 if (prev_disks == -1)
8596 prev_disks = map->num_members;
8597 if (prev_disks == map->num_members)
8598 continue;
8599
8600 /* OK, this array needs to enter reshape mode.
8601 * i.e it needs a migr_state
8602 */
8603
8604 copy_map_size = sizeof_imsm_map(map);
8605 prev_num_members = map->num_members;
8606 map->num_members = prev_disks;
8607 dev->vol.migr_state = 1;
8608 set_vol_curr_migr_unit(dev, 0);
8609 set_migr_type(dev, MIGR_GEN_MIGR);
8610 for (i = prev_num_members;
8611 i < map->num_members; i++)
8612 set_imsm_ord_tbl_ent(map, i, i);
8613 map2 = get_imsm_map(dev, MAP_1);
8614 /* Copy the current map */
8615 memcpy(map2, map, copy_map_size);
8616 map2->num_members = prev_num_members;
8617
8618 imsm_set_array_size(dev, -1);
8619 super->clean_migration_record_by_mdmon = 1;
8620 super->updates_pending++;
8621 }
8622 }
8623
8624 /* Handle dirty -> clean transititions, resync and reshape. Degraded and rebuild
8625 * states are handled in imsm_set_disk() with one exception, when a
8626 * resync is stopped due to a new failure this routine will set the
8627 * 'degraded' state for the array.
8628 */
8629 static int imsm_set_array_state(struct active_array *a, int consistent)
8630 {
8631 int inst = a->info.container_member;
8632 struct intel_super *super = a->container->sb;
8633 struct imsm_dev *dev = get_imsm_dev(super, inst);
8634 struct imsm_map *map = get_imsm_map(dev, MAP_0);
8635 int failed = imsm_count_failed(super, dev, MAP_0);
8636 __u8 map_state = imsm_check_degraded(super, dev, failed, MAP_0);
8637 __u32 blocks_per_unit;
8638
8639 if (dev->vol.migr_state &&
8640 dev->vol.migr_type == MIGR_GEN_MIGR) {
8641 /* array state change is blocked due to reshape action
8642 * We might need to
8643 * - abort the reshape (if last_checkpoint is 0 and action!= reshape)
8644 * - finish the reshape (if last_checkpoint is big and action != reshape)
8645 * - update vol_curr_migr_unit
8646 */
8647 if (a->curr_action == reshape) {
8648 /* still reshaping, maybe update vol_curr_migr_unit */
8649 goto mark_checkpoint;
8650 } else {
8651 if (a->last_checkpoint == 0 && a->prev_action == reshape) {
8652 /* for some reason we aborted the reshape.
8653 *
8654 * disable automatic metadata rollback
8655 * user action is required to recover process
8656 */
8657 if (0) {
8658 struct imsm_map *map2 =
8659 get_imsm_map(dev, MAP_1);
8660 dev->vol.migr_state = 0;
8661 set_migr_type(dev, 0);
8662 set_vol_curr_migr_unit(dev, 0);
8663 memcpy(map, map2,
8664 sizeof_imsm_map(map2));
8665 super->updates_pending++;
8666 }
8667 }
8668 if (a->last_checkpoint >= a->info.component_size) {
8669 unsigned long long array_blocks;
8670 int used_disks;
8671 struct mdinfo *mdi;
8672
8673 used_disks = imsm_num_data_members(map);
8674 if (used_disks > 0) {
8675 array_blocks =
8676 per_dev_array_size(map) *
8677 used_disks;
8678 array_blocks =
8679 round_size_to_mb(array_blocks,
8680 used_disks);
8681 a->info.custom_array_size = array_blocks;
8682 /* encourage manager to update array
8683 * size
8684 */
8685
8686 a->check_reshape = 1;
8687 }
8688 /* finalize online capacity expansion/reshape */
8689 for (mdi = a->info.devs; mdi; mdi = mdi->next)
8690 imsm_set_disk(a,
8691 mdi->disk.raid_disk,
8692 mdi->curr_state);
8693
8694 imsm_progress_container_reshape(super);
8695 }
8696 }
8697 }
8698
8699 /* before we activate this array handle any missing disks */
8700 if (consistent == 2)
8701 handle_missing(super, dev);
8702
8703 if (consistent == 2 &&
8704 (!is_resync_complete(&a->info) ||
8705 map_state != IMSM_T_STATE_NORMAL ||
8706 dev->vol.migr_state))
8707 consistent = 0;
8708
8709 if (is_resync_complete(&a->info)) {
8710 /* complete intialization / resync,
8711 * recovery and interrupted recovery is completed in
8712 * ->set_disk
8713 */
8714 if (is_resyncing(dev)) {
8715 dprintf("imsm: mark resync done\n");
8716 end_migration(dev, super, map_state);
8717 super->updates_pending++;
8718 a->last_checkpoint = 0;
8719 }
8720 } else if ((!is_resyncing(dev) && !failed) &&
8721 (imsm_reshape_blocks_arrays_changes(super) == 0)) {
8722 /* mark the start of the init process if nothing is failed */
8723 dprintf("imsm: mark resync start\n");
8724 if (map->map_state == IMSM_T_STATE_UNINITIALIZED)
8725 migrate(dev, super, IMSM_T_STATE_NORMAL, MIGR_INIT);
8726 else
8727 migrate(dev, super, IMSM_T_STATE_NORMAL, MIGR_REPAIR);
8728 super->updates_pending++;
8729 }
8730
8731 mark_checkpoint:
8732 /* skip checkpointing for general migration,
8733 * it is controlled in mdadm
8734 */
8735 if (is_gen_migration(dev))
8736 goto skip_mark_checkpoint;
8737
8738 /* check if we can update vol_curr_migr_unit from resync_start,
8739 * recovery_start
8740 */
8741 blocks_per_unit = blocks_per_migr_unit(super, dev);
8742 if (blocks_per_unit) {
8743 set_vol_curr_migr_unit(dev,
8744 a->last_checkpoint / blocks_per_unit);
8745 dprintf("imsm: mark checkpoint (%llu)\n",
8746 vol_curr_migr_unit(dev));
8747 super->updates_pending++;
8748 }
8749
8750 skip_mark_checkpoint:
8751 /* mark dirty / clean */
8752 if (((dev->vol.dirty & RAIDVOL_DIRTY) && consistent) ||
8753 (!(dev->vol.dirty & RAIDVOL_DIRTY) && !consistent)) {
8754 dprintf("imsm: mark '%s'\n", consistent ? "clean" : "dirty");
8755 if (consistent) {
8756 dev->vol.dirty = RAIDVOL_CLEAN;
8757 } else {
8758 dev->vol.dirty = RAIDVOL_DIRTY;
8759 if (dev->rwh_policy == RWH_DISTRIBUTED ||
8760 dev->rwh_policy == RWH_MULTIPLE_DISTRIBUTED)
8761 dev->vol.dirty |= RAIDVOL_DSRECORD_VALID;
8762 }
8763 super->updates_pending++;
8764 }
8765
8766 return consistent;
8767 }
8768
8769 static int imsm_disk_slot_to_ord(struct active_array *a, int slot)
8770 {
8771 int inst = a->info.container_member;
8772 struct intel_super *super = a->container->sb;
8773 struct imsm_dev *dev = get_imsm_dev(super, inst);
8774 struct imsm_map *map = get_imsm_map(dev, MAP_0);
8775
8776 if (slot > map->num_members) {
8777 pr_err("imsm: imsm_disk_slot_to_ord %d out of range 0..%d\n",
8778 slot, map->num_members - 1);
8779 return -1;
8780 }
8781
8782 if (slot < 0)
8783 return -1;
8784
8785 return get_imsm_ord_tbl_ent(dev, slot, MAP_0);
8786 }
8787
8788 static void imsm_set_disk(struct active_array *a, int n, int state)
8789 {
8790 int inst = a->info.container_member;
8791 struct intel_super *super = a->container->sb;
8792 struct imsm_dev *dev = get_imsm_dev(super, inst);
8793 struct imsm_map *map = get_imsm_map(dev, MAP_0);
8794 struct imsm_disk *disk;
8795 struct mdinfo *mdi;
8796 int recovery_not_finished = 0;
8797 int failed;
8798 int ord;
8799 __u8 map_state;
8800 int rebuild_done = 0;
8801 int i;
8802
8803 ord = get_imsm_ord_tbl_ent(dev, n, MAP_X);
8804 if (ord < 0)
8805 return;
8806
8807 dprintf("imsm: set_disk %d:%x\n", n, state);
8808 disk = get_imsm_disk(super, ord_to_idx(ord));
8809
8810 /* check for new failures */
8811 if (disk && (state & DS_FAULTY)) {
8812 if (mark_failure(super, dev, disk, ord_to_idx(ord)))
8813 super->updates_pending++;
8814 }
8815
8816 /* check if in_sync */
8817 if (state & DS_INSYNC && ord & IMSM_ORD_REBUILD && is_rebuilding(dev)) {
8818 struct imsm_map *migr_map = get_imsm_map(dev, MAP_1);
8819
8820 set_imsm_ord_tbl_ent(migr_map, n, ord_to_idx(ord));
8821 rebuild_done = 1;
8822 super->updates_pending++;
8823 }
8824
8825 failed = imsm_count_failed(super, dev, MAP_0);
8826 map_state = imsm_check_degraded(super, dev, failed, MAP_0);
8827
8828 /* check if recovery complete, newly degraded, or failed */
8829 dprintf("imsm: Detected transition to state ");
8830 switch (map_state) {
8831 case IMSM_T_STATE_NORMAL: /* transition to normal state */
8832 dprintf("normal: ");
8833 if (is_rebuilding(dev)) {
8834 dprintf_cont("while rebuilding");
8835 /* check if recovery is really finished */
8836 for (mdi = a->info.devs; mdi ; mdi = mdi->next)
8837 if (mdi->recovery_start != MaxSector) {
8838 recovery_not_finished = 1;
8839 break;
8840 }
8841 if (recovery_not_finished) {
8842 dprintf_cont("\n");
8843 dprintf("Rebuild has not finished yet, state not changed");
8844 if (a->last_checkpoint < mdi->recovery_start) {
8845 a->last_checkpoint = mdi->recovery_start;
8846 super->updates_pending++;
8847 }
8848 break;
8849 }
8850 end_migration(dev, super, map_state);
8851 map->failed_disk_num = ~0;
8852 super->updates_pending++;
8853 a->last_checkpoint = 0;
8854 break;
8855 }
8856 if (is_gen_migration(dev)) {
8857 dprintf_cont("while general migration");
8858 if (a->last_checkpoint >= a->info.component_size)
8859 end_migration(dev, super, map_state);
8860 else
8861 map->map_state = map_state;
8862 map->failed_disk_num = ~0;
8863 super->updates_pending++;
8864 break;
8865 }
8866 break;
8867 case IMSM_T_STATE_DEGRADED: /* transition to degraded state */
8868 dprintf_cont("degraded: ");
8869 if (map->map_state != map_state && !dev->vol.migr_state) {
8870 dprintf_cont("mark degraded");
8871 map->map_state = map_state;
8872 super->updates_pending++;
8873 a->last_checkpoint = 0;
8874 break;
8875 }
8876 if (is_rebuilding(dev)) {
8877 dprintf_cont("while rebuilding ");
8878 if (state & DS_FAULTY) {
8879 dprintf_cont("removing failed drive ");
8880 if (n == map->failed_disk_num) {
8881 dprintf_cont("end migration");
8882 end_migration(dev, super, map_state);
8883 a->last_checkpoint = 0;
8884 } else {
8885 dprintf_cont("fail detected during rebuild, changing map state");
8886 map->map_state = map_state;
8887 }
8888 super->updates_pending++;
8889 }
8890
8891 if (!rebuild_done)
8892 break;
8893
8894 /* check if recovery is really finished */
8895 for (mdi = a->info.devs; mdi ; mdi = mdi->next)
8896 if (mdi->recovery_start != MaxSector) {
8897 recovery_not_finished = 1;
8898 break;
8899 }
8900 if (recovery_not_finished) {
8901 dprintf_cont("\n");
8902 dprintf_cont("Rebuild has not finished yet");
8903 if (a->last_checkpoint < mdi->recovery_start) {
8904 a->last_checkpoint =
8905 mdi->recovery_start;
8906 super->updates_pending++;
8907 }
8908 break;
8909 }
8910
8911 dprintf_cont(" Rebuild done, still degraded");
8912 end_migration(dev, super, map_state);
8913 a->last_checkpoint = 0;
8914 super->updates_pending++;
8915
8916 for (i = 0; i < map->num_members; i++) {
8917 int idx = get_imsm_ord_tbl_ent(dev, i, MAP_0);
8918
8919 if (idx & IMSM_ORD_REBUILD)
8920 map->failed_disk_num = i;
8921 }
8922 super->updates_pending++;
8923 break;
8924 }
8925 if (is_gen_migration(dev)) {
8926 dprintf_cont("while general migration");
8927 if (a->last_checkpoint >= a->info.component_size)
8928 end_migration(dev, super, map_state);
8929 else {
8930 map->map_state = map_state;
8931 manage_second_map(super, dev);
8932 }
8933 super->updates_pending++;
8934 break;
8935 }
8936 if (is_initializing(dev)) {
8937 dprintf_cont("while initialization.");
8938 map->map_state = map_state;
8939 super->updates_pending++;
8940 break;
8941 }
8942 break;
8943 case IMSM_T_STATE_FAILED: /* transition to failed state */
8944 dprintf_cont("failed: ");
8945 if (is_gen_migration(dev)) {
8946 dprintf_cont("while general migration");
8947 map->map_state = map_state;
8948 super->updates_pending++;
8949 break;
8950 }
8951 if (map->map_state != map_state) {
8952 dprintf_cont("mark failed");
8953 end_migration(dev, super, map_state);
8954 super->updates_pending++;
8955 a->last_checkpoint = 0;
8956 break;
8957 }
8958 break;
8959 default:
8960 dprintf_cont("state %i\n", map_state);
8961 }
8962 dprintf_cont("\n");
8963 }
8964
8965 static int store_imsm_mpb(int fd, struct imsm_super *mpb)
8966 {
8967 void *buf = mpb;
8968 __u32 mpb_size = __le32_to_cpu(mpb->mpb_size);
8969 unsigned long long dsize;
8970 unsigned long long sectors;
8971 unsigned int sector_size;
8972
8973 if (!get_dev_sector_size(fd, NULL, &sector_size))
8974 return 1;
8975 get_dev_size(fd, NULL, &dsize);
8976
8977 if (mpb_size > sector_size) {
8978 /* -1 to account for anchor */
8979 sectors = mpb_sectors(mpb, sector_size) - 1;
8980
8981 /* write the extended mpb to the sectors preceeding the anchor */
8982 if (lseek64(fd, dsize - (sector_size * (2 + sectors)),
8983 SEEK_SET) < 0)
8984 return 1;
8985
8986 if ((unsigned long long)write(fd, buf + sector_size,
8987 sector_size * sectors) != sector_size * sectors)
8988 return 1;
8989 }
8990
8991 /* first block is stored on second to last sector of the disk */
8992 if (lseek64(fd, dsize - (sector_size * 2), SEEK_SET) < 0)
8993 return 1;
8994
8995 if ((unsigned int)write(fd, buf, sector_size) != sector_size)
8996 return 1;
8997
8998 return 0;
8999 }
9000
9001 static void imsm_sync_metadata(struct supertype *container)
9002 {
9003 struct intel_super *super = container->sb;
9004
9005 dprintf("sync metadata: %d\n", super->updates_pending);
9006 if (!super->updates_pending)
9007 return;
9008
9009 write_super_imsm(container, 0);
9010
9011 super->updates_pending = 0;
9012 }
9013
9014 static struct dl *imsm_readd(struct intel_super *super, int idx, struct active_array *a)
9015 {
9016 struct imsm_dev *dev = get_imsm_dev(super, a->info.container_member);
9017 int i = get_imsm_disk_idx(dev, idx, MAP_X);
9018 struct dl *dl;
9019
9020 for (dl = super->disks; dl; dl = dl->next)
9021 if (dl->index == i)
9022 break;
9023
9024 if (dl && is_failed(&dl->disk))
9025 dl = NULL;
9026
9027 if (dl)
9028 dprintf("found %x:%x\n", dl->major, dl->minor);
9029
9030 return dl;
9031 }
9032
9033 static struct dl *imsm_add_spare(struct intel_super *super, int slot,
9034 struct active_array *a, int activate_new,
9035 struct mdinfo *additional_test_list)
9036 {
9037 struct imsm_dev *dev = get_imsm_dev(super, a->info.container_member);
9038 int idx = get_imsm_disk_idx(dev, slot, MAP_X);
9039 struct imsm_super *mpb = super->anchor;
9040 struct imsm_map *map;
9041 unsigned long long pos;
9042 struct mdinfo *d;
9043 struct extent *ex;
9044 int i, j;
9045 int found;
9046 __u32 array_start = 0;
9047 __u32 array_end = 0;
9048 struct dl *dl;
9049 struct mdinfo *test_list;
9050
9051 for (dl = super->disks; dl; dl = dl->next) {
9052 /* If in this array, skip */
9053 for (d = a->info.devs ; d ; d = d->next)
9054 if (is_fd_valid(d->state_fd) &&
9055 d->disk.major == dl->major &&
9056 d->disk.minor == dl->minor) {
9057 dprintf("%x:%x already in array\n",
9058 dl->major, dl->minor);
9059 break;
9060 }
9061 if (d)
9062 continue;
9063 test_list = additional_test_list;
9064 while (test_list) {
9065 if (test_list->disk.major == dl->major &&
9066 test_list->disk.minor == dl->minor) {
9067 dprintf("%x:%x already in additional test list\n",
9068 dl->major, dl->minor);
9069 break;
9070 }
9071 test_list = test_list->next;
9072 }
9073 if (test_list)
9074 continue;
9075
9076 /* skip in use or failed drives */
9077 if (is_failed(&dl->disk) || idx == dl->index ||
9078 dl->index == -2) {
9079 dprintf("%x:%x status (failed: %d index: %d)\n",
9080 dl->major, dl->minor, is_failed(&dl->disk), idx);
9081 continue;
9082 }
9083
9084 /* skip pure spares when we are looking for partially
9085 * assimilated drives
9086 */
9087 if (dl->index == -1 && !activate_new)
9088 continue;
9089
9090 if (!drive_validate_sector_size(super, dl))
9091 continue;
9092
9093 /* Does this unused device have the requisite free space?
9094 * It needs to be able to cover all member volumes
9095 */
9096 ex = get_extents(super, dl, 1);
9097 if (!ex) {
9098 dprintf("cannot get extents\n");
9099 continue;
9100 }
9101 for (i = 0; i < mpb->num_raid_devs; i++) {
9102 dev = get_imsm_dev(super, i);
9103 map = get_imsm_map(dev, MAP_0);
9104
9105 /* check if this disk is already a member of
9106 * this array
9107 */
9108 if (get_imsm_disk_slot(map, dl->index) >= 0)
9109 continue;
9110
9111 found = 0;
9112 j = 0;
9113 pos = 0;
9114 array_start = pba_of_lba0(map);
9115 array_end = array_start +
9116 per_dev_array_size(map) - 1;
9117
9118 do {
9119 /* check that we can start at pba_of_lba0 with
9120 * num_data_stripes*blocks_per_stripe of space
9121 */
9122 if (array_start >= pos && array_end < ex[j].start) {
9123 found = 1;
9124 break;
9125 }
9126 pos = ex[j].start + ex[j].size;
9127 j++;
9128 } while (ex[j-1].size);
9129
9130 if (!found)
9131 break;
9132 }
9133
9134 free(ex);
9135 if (i < mpb->num_raid_devs) {
9136 dprintf("%x:%x does not have %u to %u available\n",
9137 dl->major, dl->minor, array_start, array_end);
9138 /* No room */
9139 continue;
9140 }
9141 return dl;
9142 }
9143
9144 return dl;
9145 }
9146
9147 static int imsm_rebuild_allowed(struct supertype *cont, int dev_idx, int failed)
9148 {
9149 struct imsm_dev *dev2;
9150 struct imsm_map *map;
9151 struct dl *idisk;
9152 int slot;
9153 int idx;
9154 __u8 state;
9155
9156 dev2 = get_imsm_dev(cont->sb, dev_idx);
9157
9158 state = imsm_check_degraded(cont->sb, dev2, failed, MAP_0);
9159 if (state == IMSM_T_STATE_FAILED) {
9160 map = get_imsm_map(dev2, MAP_0);
9161 for (slot = 0; slot < map->num_members; slot++) {
9162 /*
9163 * Check if failed disks are deleted from intel
9164 * disk list or are marked to be deleted
9165 */
9166 idx = get_imsm_disk_idx(dev2, slot, MAP_X);
9167 idisk = get_imsm_dl_disk(cont->sb, idx);
9168 /*
9169 * Do not rebuild the array if failed disks
9170 * from failed sub-array are not removed from
9171 * container.
9172 */
9173 if (idisk &&
9174 is_failed(&idisk->disk) &&
9175 (idisk->action != DISK_REMOVE))
9176 return 0;
9177 }
9178 }
9179 return 1;
9180 }
9181
9182 static struct mdinfo *imsm_activate_spare(struct active_array *a,
9183 struct metadata_update **updates)
9184 {
9185 /**
9186 * Find a device with unused free space and use it to replace a
9187 * failed/vacant region in an array. We replace failed regions one a
9188 * array at a time. The result is that a new spare disk will be added
9189 * to the first failed array and after the monitor has finished
9190 * propagating failures the remainder will be consumed.
9191 *
9192 * FIXME add a capability for mdmon to request spares from another
9193 * container.
9194 */
9195
9196 struct intel_super *super = a->container->sb;
9197 int inst = a->info.container_member;
9198 struct imsm_dev *dev = get_imsm_dev(super, inst);
9199 struct imsm_map *map = get_imsm_map(dev, MAP_0);
9200 int failed = a->info.array.raid_disks;
9201 struct mdinfo *rv = NULL;
9202 struct mdinfo *d;
9203 struct mdinfo *di;
9204 struct metadata_update *mu;
9205 struct dl *dl;
9206 struct imsm_update_activate_spare *u;
9207 int num_spares = 0;
9208 int i;
9209 int allowed;
9210
9211 for (d = a->info.devs ; d; d = d->next) {
9212 if (!is_fd_valid(d->state_fd))
9213 continue;
9214
9215 if (d->curr_state & DS_FAULTY)
9216 /* wait for Removal to happen */
9217 return NULL;
9218
9219 failed--;
9220 }
9221
9222 dprintf("imsm: activate spare: inst=%d failed=%d (%d) level=%d\n",
9223 inst, failed, a->info.array.raid_disks, a->info.array.level);
9224
9225 if (imsm_reshape_blocks_arrays_changes(super))
9226 return NULL;
9227
9228 /* Cannot activate another spare if rebuild is in progress already
9229 */
9230 if (is_rebuilding(dev)) {
9231 dprintf("imsm: No spare activation allowed. Rebuild in progress already.\n");
9232 return NULL;
9233 }
9234
9235 if (a->info.array.level == 4)
9236 /* No repair for takeovered array
9237 * imsm doesn't support raid4
9238 */
9239 return NULL;
9240
9241 if (imsm_check_degraded(super, dev, failed, MAP_0) !=
9242 IMSM_T_STATE_DEGRADED)
9243 return NULL;
9244
9245 if (get_imsm_map(dev, MAP_0)->map_state == IMSM_T_STATE_UNINITIALIZED) {
9246 dprintf("imsm: No spare activation allowed. Volume is not initialized.\n");
9247 return NULL;
9248 }
9249
9250 /*
9251 * If there are any failed disks check state of the other volume.
9252 * Block rebuild if the another one is failed until failed disks
9253 * are removed from container.
9254 */
9255 if (failed) {
9256 dprintf("found failed disks in %.*s, check if there anotherfailed sub-array.\n",
9257 MAX_RAID_SERIAL_LEN, dev->volume);
9258 /* check if states of the other volumes allow for rebuild */
9259 for (i = 0; i < super->anchor->num_raid_devs; i++) {
9260 if (i != inst) {
9261 allowed = imsm_rebuild_allowed(a->container,
9262 i, failed);
9263 if (!allowed)
9264 return NULL;
9265 }
9266 }
9267 }
9268
9269 /* For each slot, if it is not working, find a spare */
9270 for (i = 0; i < a->info.array.raid_disks; i++) {
9271 for (d = a->info.devs ; d ; d = d->next)
9272 if (d->disk.raid_disk == i)
9273 break;
9274 dprintf("found %d: %p %x\n", i, d, d?d->curr_state:0);
9275 if (d && is_fd_valid(d->state_fd))
9276 continue;
9277
9278 /*
9279 * OK, this device needs recovery. Try to re-add the
9280 * previous occupant of this slot, if this fails see if
9281 * we can continue the assimilation of a spare that was
9282 * partially assimilated, finally try to activate a new
9283 * spare.
9284 */
9285 dl = imsm_readd(super, i, a);
9286 if (!dl)
9287 dl = imsm_add_spare(super, i, a, 0, rv);
9288 if (!dl)
9289 dl = imsm_add_spare(super, i, a, 1, rv);
9290 if (!dl)
9291 continue;
9292
9293 /* found a usable disk with enough space */
9294 di = xcalloc(1, sizeof(*di));
9295
9296 /* dl->index will be -1 in the case we are activating a
9297 * pristine spare. imsm_process_update() will create a
9298 * new index in this case. Once a disk is found to be
9299 * failed in all member arrays it is kicked from the
9300 * metadata
9301 */
9302 di->disk.number = dl->index;
9303
9304 /* (ab)use di->devs to store a pointer to the device
9305 * we chose
9306 */
9307 di->devs = (struct mdinfo *) dl;
9308
9309 di->disk.raid_disk = i;
9310 di->disk.major = dl->major;
9311 di->disk.minor = dl->minor;
9312 di->disk.state = 0;
9313 di->recovery_start = 0;
9314 di->data_offset = pba_of_lba0(map);
9315 di->component_size = a->info.component_size;
9316 di->container_member = inst;
9317 di->bb.supported = 1;
9318 if (a->info.consistency_policy == CONSISTENCY_POLICY_PPL) {
9319 di->ppl_sector = get_ppl_sector(super, inst);
9320 di->ppl_size = MULTIPLE_PPL_AREA_SIZE_IMSM >> 9;
9321 }
9322 super->random = random32();
9323 di->next = rv;
9324 rv = di;
9325 num_spares++;
9326 dprintf("%x:%x to be %d at %llu\n", dl->major, dl->minor,
9327 i, di->data_offset);
9328 }
9329
9330 if (!rv)
9331 /* No spares found */
9332 return rv;
9333 /* Now 'rv' has a list of devices to return.
9334 * Create a metadata_update record to update the
9335 * disk_ord_tbl for the array
9336 */
9337 mu = xmalloc(sizeof(*mu));
9338 mu->buf = xcalloc(num_spares,
9339 sizeof(struct imsm_update_activate_spare));
9340 mu->space = NULL;
9341 mu->space_list = NULL;
9342 mu->len = sizeof(struct imsm_update_activate_spare) * num_spares;
9343 mu->next = *updates;
9344 u = (struct imsm_update_activate_spare *) mu->buf;
9345
9346 for (di = rv ; di ; di = di->next) {
9347 u->type = update_activate_spare;
9348 u->dl = (struct dl *) di->devs;
9349 di->devs = NULL;
9350 u->slot = di->disk.raid_disk;
9351 u->array = inst;
9352 u->next = u + 1;
9353 u++;
9354 }
9355 (u-1)->next = NULL;
9356 *updates = mu;
9357
9358 return rv;
9359 }
9360
9361 static int disks_overlap(struct intel_super *super, int idx, struct imsm_update_create_array *u)
9362 {
9363 struct imsm_dev *dev = get_imsm_dev(super, idx);
9364 struct imsm_map *map = get_imsm_map(dev, MAP_0);
9365 struct imsm_map *new_map = get_imsm_map(&u->dev, MAP_0);
9366 struct disk_info *inf = get_disk_info(u);
9367 struct imsm_disk *disk;
9368 int i;
9369 int j;
9370
9371 for (i = 0; i < map->num_members; i++) {
9372 disk = get_imsm_disk(super, get_imsm_disk_idx(dev, i, MAP_X));
9373 for (j = 0; j < new_map->num_members; j++)
9374 if (serialcmp(disk->serial, inf[j].serial) == 0)
9375 return 1;
9376 }
9377
9378 return 0;
9379 }
9380
9381 static struct dl *get_disk_super(struct intel_super *super, int major, int minor)
9382 {
9383 struct dl *dl;
9384
9385 for (dl = super->disks; dl; dl = dl->next)
9386 if (dl->major == major && dl->minor == minor)
9387 return dl;
9388 return NULL;
9389 }
9390
9391 static int remove_disk_super(struct intel_super *super, int major, int minor)
9392 {
9393 struct dl *prev;
9394 struct dl *dl;
9395
9396 prev = NULL;
9397 for (dl = super->disks; dl; dl = dl->next) {
9398 if (dl->major == major && dl->minor == minor) {
9399 /* remove */
9400 if (prev)
9401 prev->next = dl->next;
9402 else
9403 super->disks = dl->next;
9404 dl->next = NULL;
9405 __free_imsm_disk(dl, 1);
9406 dprintf("removed %x:%x\n", major, minor);
9407 break;
9408 }
9409 prev = dl;
9410 }
9411 return 0;
9412 }
9413
9414 static void imsm_delete(struct intel_super *super, struct dl **dlp, unsigned index);
9415
9416 static int add_remove_disk_update(struct intel_super *super)
9417 {
9418 int check_degraded = 0;
9419 struct dl *disk;
9420
9421 /* add/remove some spares to/from the metadata/contrainer */
9422 while (super->disk_mgmt_list) {
9423 struct dl *disk_cfg;
9424
9425 disk_cfg = super->disk_mgmt_list;
9426 super->disk_mgmt_list = disk_cfg->next;
9427 disk_cfg->next = NULL;
9428
9429 if (disk_cfg->action == DISK_ADD) {
9430 disk_cfg->next = super->disks;
9431 super->disks = disk_cfg;
9432 check_degraded = 1;
9433 dprintf("added %x:%x\n",
9434 disk_cfg->major, disk_cfg->minor);
9435 } else if (disk_cfg->action == DISK_REMOVE) {
9436 dprintf("Disk remove action processed: %x.%x\n",
9437 disk_cfg->major, disk_cfg->minor);
9438 disk = get_disk_super(super,
9439 disk_cfg->major,
9440 disk_cfg->minor);
9441 if (disk) {
9442 /* store action status */
9443 disk->action = DISK_REMOVE;
9444 /* remove spare disks only */
9445 if (disk->index == -1) {
9446 remove_disk_super(super,
9447 disk_cfg->major,
9448 disk_cfg->minor);
9449 } else {
9450 disk_cfg->fd = disk->fd;
9451 disk->fd = -1;
9452 }
9453 }
9454 /* release allocate disk structure */
9455 __free_imsm_disk(disk_cfg, 1);
9456 }
9457 }
9458 return check_degraded;
9459 }
9460
9461 static int apply_reshape_migration_update(struct imsm_update_reshape_migration *u,
9462 struct intel_super *super,
9463 void ***space_list)
9464 {
9465 struct intel_dev *id;
9466 void **tofree = NULL;
9467 int ret_val = 0;
9468
9469 dprintf("(enter)\n");
9470 if (u->subdev < 0 || u->subdev > 1) {
9471 dprintf("imsm: Error: Wrong subdev: %i\n", u->subdev);
9472 return ret_val;
9473 }
9474 if (space_list == NULL || *space_list == NULL) {
9475 dprintf("imsm: Error: Memory is not allocated\n");
9476 return ret_val;
9477 }
9478
9479 for (id = super->devlist ; id; id = id->next) {
9480 if (id->index == (unsigned)u->subdev) {
9481 struct imsm_dev *dev = get_imsm_dev(super, u->subdev);
9482 struct imsm_map *map;
9483 struct imsm_dev *new_dev =
9484 (struct imsm_dev *)*space_list;
9485 struct imsm_map *migr_map = get_imsm_map(dev, MAP_1);
9486 int to_state;
9487 struct dl *new_disk;
9488
9489 if (new_dev == NULL)
9490 return ret_val;
9491 *space_list = **space_list;
9492 memcpy(new_dev, dev, sizeof_imsm_dev(dev, 0));
9493 map = get_imsm_map(new_dev, MAP_0);
9494 if (migr_map) {
9495 dprintf("imsm: Error: migration in progress");
9496 return ret_val;
9497 }
9498
9499 to_state = map->map_state;
9500 if ((u->new_level == 5) && (map->raid_level == 0)) {
9501 map->num_members++;
9502 /* this should not happen */
9503 if (u->new_disks[0] < 0) {
9504 map->failed_disk_num =
9505 map->num_members - 1;
9506 to_state = IMSM_T_STATE_DEGRADED;
9507 } else
9508 to_state = IMSM_T_STATE_NORMAL;
9509 }
9510 migrate(new_dev, super, to_state, MIGR_GEN_MIGR);
9511 if (u->new_level > -1)
9512 map->raid_level = u->new_level;
9513 migr_map = get_imsm_map(new_dev, MAP_1);
9514 if ((u->new_level == 5) &&
9515 (migr_map->raid_level == 0)) {
9516 int ord = map->num_members - 1;
9517 migr_map->num_members--;
9518 if (u->new_disks[0] < 0)
9519 ord |= IMSM_ORD_REBUILD;
9520 set_imsm_ord_tbl_ent(map,
9521 map->num_members - 1,
9522 ord);
9523 }
9524 id->dev = new_dev;
9525 tofree = (void **)dev;
9526
9527 /* update chunk size
9528 */
9529 if (u->new_chunksize > 0) {
9530 struct imsm_map *dest_map =
9531 get_imsm_map(dev, MAP_0);
9532 int used_disks =
9533 imsm_num_data_members(dest_map);
9534
9535 if (used_disks == 0)
9536 return ret_val;
9537
9538 map->blocks_per_strip =
9539 __cpu_to_le16(u->new_chunksize * 2);
9540 update_num_data_stripes(map, imsm_dev_size(dev));
9541 }
9542
9543 /* ensure blocks_per_member has valid value
9544 */
9545 set_blocks_per_member(map,
9546 per_dev_array_size(map) +
9547 NUM_BLOCKS_DIRTY_STRIPE_REGION);
9548
9549 /* add disk
9550 */
9551 if (u->new_level != 5 || migr_map->raid_level != 0 ||
9552 migr_map->raid_level == map->raid_level)
9553 goto skip_disk_add;
9554
9555 if (u->new_disks[0] >= 0) {
9556 /* use passes spare
9557 */
9558 new_disk = get_disk_super(super,
9559 major(u->new_disks[0]),
9560 minor(u->new_disks[0]));
9561 dprintf("imsm: new disk for reshape is: %i:%i (%p, index = %i)\n",
9562 major(u->new_disks[0]),
9563 minor(u->new_disks[0]),
9564 new_disk, new_disk->index);
9565 if (new_disk == NULL)
9566 goto error_disk_add;
9567
9568 new_disk->index = map->num_members - 1;
9569 /* slot to fill in autolayout
9570 */
9571 new_disk->raiddisk = new_disk->index;
9572 new_disk->disk.status |= CONFIGURED_DISK;
9573 new_disk->disk.status &= ~SPARE_DISK;
9574 } else
9575 goto error_disk_add;
9576
9577 skip_disk_add:
9578 *tofree = *space_list;
9579 /* calculate new size
9580 */
9581 imsm_set_array_size(new_dev, -1);
9582
9583 ret_val = 1;
9584 }
9585 }
9586
9587 if (tofree)
9588 *space_list = tofree;
9589 return ret_val;
9590
9591 error_disk_add:
9592 dprintf("Error: imsm: Cannot find disk.\n");
9593 return ret_val;
9594 }
9595
9596 static int apply_size_change_update(struct imsm_update_size_change *u,
9597 struct intel_super *super)
9598 {
9599 struct intel_dev *id;
9600 int ret_val = 0;
9601
9602 dprintf("(enter)\n");
9603 if (u->subdev < 0 || u->subdev > 1) {
9604 dprintf("imsm: Error: Wrong subdev: %i\n", u->subdev);
9605 return ret_val;
9606 }
9607
9608 for (id = super->devlist ; id; id = id->next) {
9609 if (id->index == (unsigned)u->subdev) {
9610 struct imsm_dev *dev = get_imsm_dev(super, u->subdev);
9611 struct imsm_map *map = get_imsm_map(dev, MAP_0);
9612 int used_disks = imsm_num_data_members(map);
9613 unsigned long long blocks_per_member;
9614 unsigned long long new_size_per_disk;
9615
9616 if (used_disks == 0)
9617 return 0;
9618
9619 /* calculate new size
9620 */
9621 new_size_per_disk = u->new_size / used_disks;
9622 blocks_per_member = new_size_per_disk +
9623 NUM_BLOCKS_DIRTY_STRIPE_REGION;
9624
9625 imsm_set_array_size(dev, u->new_size);
9626 set_blocks_per_member(map, blocks_per_member);
9627 update_num_data_stripes(map, u->new_size);
9628 ret_val = 1;
9629 break;
9630 }
9631 }
9632
9633 return ret_val;
9634 }
9635
9636 static int prepare_spare_to_activate(struct supertype *st,
9637 struct imsm_update_activate_spare *u)
9638 {
9639 struct intel_super *super = st->sb;
9640 int prev_current_vol = super->current_vol;
9641 struct active_array *a;
9642 int ret = 1;
9643
9644 for (a = st->arrays; a; a = a->next)
9645 /*
9646 * Additional initialization (adding bitmap header, filling
9647 * the bitmap area with '1's to force initial rebuild for a whole
9648 * data-area) is required when adding the spare to the volume
9649 * with write-intent bitmap.
9650 */
9651 if (a->info.container_member == u->array &&
9652 a->info.consistency_policy == CONSISTENCY_POLICY_BITMAP) {
9653 struct dl *dl;
9654
9655 for (dl = super->disks; dl; dl = dl->next)
9656 if (dl == u->dl)
9657 break;
9658 if (!dl)
9659 break;
9660
9661 super->current_vol = u->array;
9662 if (st->ss->write_bitmap(st, dl->fd, NoUpdate))
9663 ret = 0;
9664 super->current_vol = prev_current_vol;
9665 }
9666 return ret;
9667 }
9668
9669 static int apply_update_activate_spare(struct imsm_update_activate_spare *u,
9670 struct intel_super *super,
9671 struct active_array *active_array)
9672 {
9673 struct imsm_super *mpb = super->anchor;
9674 struct imsm_dev *dev = get_imsm_dev(super, u->array);
9675 struct imsm_map *map = get_imsm_map(dev, MAP_0);
9676 struct imsm_map *migr_map;
9677 struct active_array *a;
9678 struct imsm_disk *disk;
9679 __u8 to_state;
9680 struct dl *dl;
9681 unsigned int found;
9682 int failed;
9683 int victim;
9684 int i;
9685 int second_map_created = 0;
9686
9687 for (; u; u = u->next) {
9688 victim = get_imsm_disk_idx(dev, u->slot, MAP_X);
9689
9690 if (victim < 0)
9691 return 0;
9692
9693 for (dl = super->disks; dl; dl = dl->next)
9694 if (dl == u->dl)
9695 break;
9696
9697 if (!dl) {
9698 pr_err("error: imsm_activate_spare passed an unknown disk (index: %d)\n",
9699 u->dl->index);
9700 return 0;
9701 }
9702
9703 /* count failures (excluding rebuilds and the victim)
9704 * to determine map[0] state
9705 */
9706 failed = 0;
9707 for (i = 0; i < map->num_members; i++) {
9708 if (i == u->slot)
9709 continue;
9710 disk = get_imsm_disk(super,
9711 get_imsm_disk_idx(dev, i, MAP_X));
9712 if (!disk || is_failed(disk))
9713 failed++;
9714 }
9715
9716 /* adding a pristine spare, assign a new index */
9717 if (dl->index < 0) {
9718 dl->index = super->anchor->num_disks;
9719 super->anchor->num_disks++;
9720 }
9721 disk = &dl->disk;
9722 disk->status |= CONFIGURED_DISK;
9723 disk->status &= ~SPARE_DISK;
9724
9725 /* mark rebuild */
9726 to_state = imsm_check_degraded(super, dev, failed, MAP_0);
9727 if (!second_map_created) {
9728 second_map_created = 1;
9729 map->map_state = IMSM_T_STATE_DEGRADED;
9730 migrate(dev, super, to_state, MIGR_REBUILD);
9731 } else
9732 map->map_state = to_state;
9733 migr_map = get_imsm_map(dev, MAP_1);
9734 set_imsm_ord_tbl_ent(map, u->slot, dl->index);
9735 set_imsm_ord_tbl_ent(migr_map, u->slot,
9736 dl->index | IMSM_ORD_REBUILD);
9737
9738 /* update the family_num to mark a new container
9739 * generation, being careful to record the existing
9740 * family_num in orig_family_num to clean up after
9741 * earlier mdadm versions that neglected to set it.
9742 */
9743 if (mpb->orig_family_num == 0)
9744 mpb->orig_family_num = mpb->family_num;
9745 mpb->family_num += super->random;
9746
9747 /* count arrays using the victim in the metadata */
9748 found = 0;
9749 for (a = active_array; a ; a = a->next) {
9750 int dev_idx = a->info.container_member;
9751
9752 if (get_disk_slot_in_dev(super, dev_idx, victim) >= 0)
9753 found++;
9754 }
9755
9756 /* delete the victim if it is no longer being
9757 * utilized anywhere
9758 */
9759 if (!found) {
9760 struct dl **dlp;
9761
9762 /* We know that 'manager' isn't touching anything,
9763 * so it is safe to delete
9764 */
9765 for (dlp = &super->disks; *dlp; dlp = &(*dlp)->next)
9766 if ((*dlp)->index == victim)
9767 break;
9768
9769 /* victim may be on the missing list */
9770 if (!*dlp)
9771 for (dlp = &super->missing; *dlp;
9772 dlp = &(*dlp)->next)
9773 if ((*dlp)->index == victim)
9774 break;
9775 imsm_delete(super, dlp, victim);
9776 }
9777 }
9778
9779 return 1;
9780 }
9781
9782 static int apply_reshape_container_disks_update(struct imsm_update_reshape *u,
9783 struct intel_super *super,
9784 void ***space_list)
9785 {
9786 struct dl *new_disk;
9787 struct intel_dev *id;
9788 int i;
9789 int delta_disks = u->new_raid_disks - u->old_raid_disks;
9790 int disk_count = u->old_raid_disks;
9791 void **tofree = NULL;
9792 int devices_to_reshape = 1;
9793 struct imsm_super *mpb = super->anchor;
9794 int ret_val = 0;
9795 unsigned int dev_id;
9796
9797 dprintf("(enter)\n");
9798
9799 /* enable spares to use in array */
9800 for (i = 0; i < delta_disks; i++) {
9801 new_disk = get_disk_super(super,
9802 major(u->new_disks[i]),
9803 minor(u->new_disks[i]));
9804 dprintf("imsm: new disk for reshape is: %i:%i (%p, index = %i)\n",
9805 major(u->new_disks[i]), minor(u->new_disks[i]),
9806 new_disk, new_disk->index);
9807 if (new_disk == NULL ||
9808 (new_disk->index >= 0 &&
9809 new_disk->index < u->old_raid_disks))
9810 goto update_reshape_exit;
9811 new_disk->index = disk_count++;
9812 /* slot to fill in autolayout
9813 */
9814 new_disk->raiddisk = new_disk->index;
9815 new_disk->disk.status |=
9816 CONFIGURED_DISK;
9817 new_disk->disk.status &= ~SPARE_DISK;
9818 }
9819
9820 dprintf("imsm: volume set mpb->num_raid_devs = %i\n",
9821 mpb->num_raid_devs);
9822 /* manage changes in volume
9823 */
9824 for (dev_id = 0; dev_id < mpb->num_raid_devs; dev_id++) {
9825 void **sp = *space_list;
9826 struct imsm_dev *newdev;
9827 struct imsm_map *newmap, *oldmap;
9828
9829 for (id = super->devlist ; id; id = id->next) {
9830 if (id->index == dev_id)
9831 break;
9832 }
9833 if (id == NULL)
9834 break;
9835 if (!sp)
9836 continue;
9837 *space_list = *sp;
9838 newdev = (void*)sp;
9839 /* Copy the dev, but not (all of) the map */
9840 memcpy(newdev, id->dev, sizeof(*newdev));
9841 oldmap = get_imsm_map(id->dev, MAP_0);
9842 newmap = get_imsm_map(newdev, MAP_0);
9843 /* Copy the current map */
9844 memcpy(newmap, oldmap, sizeof_imsm_map(oldmap));
9845 /* update one device only
9846 */
9847 if (devices_to_reshape) {
9848 dprintf("imsm: modifying subdev: %i\n",
9849 id->index);
9850 devices_to_reshape--;
9851 newdev->vol.migr_state = 1;
9852 set_vol_curr_migr_unit(newdev, 0);
9853 set_migr_type(newdev, MIGR_GEN_MIGR);
9854 newmap->num_members = u->new_raid_disks;
9855 for (i = 0; i < delta_disks; i++) {
9856 set_imsm_ord_tbl_ent(newmap,
9857 u->old_raid_disks + i,
9858 u->old_raid_disks + i);
9859 }
9860 /* New map is correct, now need to save old map
9861 */
9862 newmap = get_imsm_map(newdev, MAP_1);
9863 memcpy(newmap, oldmap, sizeof_imsm_map(oldmap));
9864
9865 imsm_set_array_size(newdev, -1);
9866 }
9867
9868 sp = (void **)id->dev;
9869 id->dev = newdev;
9870 *sp = tofree;
9871 tofree = sp;
9872
9873 /* Clear migration record */
9874 memset(super->migr_rec, 0, sizeof(struct migr_record));
9875 }
9876 if (tofree)
9877 *space_list = tofree;
9878 ret_val = 1;
9879
9880 update_reshape_exit:
9881
9882 return ret_val;
9883 }
9884
9885 static int apply_takeover_update(struct imsm_update_takeover *u,
9886 struct intel_super *super,
9887 void ***space_list)
9888 {
9889 struct imsm_dev *dev = NULL;
9890 struct intel_dev *dv;
9891 struct imsm_dev *dev_new;
9892 struct imsm_map *map;
9893 struct dl *dm, *du;
9894 int i;
9895
9896 for (dv = super->devlist; dv; dv = dv->next)
9897 if (dv->index == (unsigned int)u->subarray) {
9898 dev = dv->dev;
9899 break;
9900 }
9901
9902 if (dev == NULL)
9903 return 0;
9904
9905 map = get_imsm_map(dev, MAP_0);
9906
9907 if (u->direction == R10_TO_R0) {
9908 /* Number of failed disks must be half of initial disk number */
9909 if (imsm_count_failed(super, dev, MAP_0) !=
9910 (map->num_members / 2))
9911 return 0;
9912
9913 /* iterate through devices to mark removed disks as spare */
9914 for (dm = super->disks; dm; dm = dm->next) {
9915 if (dm->disk.status & FAILED_DISK) {
9916 int idx = dm->index;
9917 /* update indexes on the disk list */
9918 /* FIXME this loop-with-the-loop looks wrong, I'm not convinced
9919 the index values will end up being correct.... NB */
9920 for (du = super->disks; du; du = du->next)
9921 if (du->index > idx)
9922 du->index--;
9923 /* mark as spare disk */
9924 mark_spare(dm);
9925 }
9926 }
9927 /* update map */
9928 map->num_members /= map->num_domains;
9929 map->map_state = IMSM_T_STATE_NORMAL;
9930 map->raid_level = 0;
9931 set_num_domains(map);
9932 update_num_data_stripes(map, imsm_dev_size(dev));
9933 map->failed_disk_num = -1;
9934 }
9935
9936 if (u->direction == R0_TO_R10) {
9937 void **space;
9938
9939 /* update slots in current disk list */
9940 for (dm = super->disks; dm; dm = dm->next) {
9941 if (dm->index >= 0)
9942 dm->index *= 2;
9943 }
9944 /* create new *missing* disks */
9945 for (i = 0; i < map->num_members; i++) {
9946 space = *space_list;
9947 if (!space)
9948 continue;
9949 *space_list = *space;
9950 du = (void *)space;
9951 memcpy(du, super->disks, sizeof(*du));
9952 du->fd = -1;
9953 du->minor = 0;
9954 du->major = 0;
9955 du->index = (i * 2) + 1;
9956 sprintf((char *)du->disk.serial,
9957 " MISSING_%d", du->index);
9958 sprintf((char *)du->serial,
9959 "MISSING_%d", du->index);
9960 du->next = super->missing;
9961 super->missing = du;
9962 }
9963 /* create new dev and map */
9964 space = *space_list;
9965 if (!space)
9966 return 0;
9967 *space_list = *space;
9968 dev_new = (void *)space;
9969 memcpy(dev_new, dev, sizeof(*dev));
9970 /* update new map */
9971 map = get_imsm_map(dev_new, MAP_0);
9972
9973 map->map_state = IMSM_T_STATE_DEGRADED;
9974 map->raid_level = 1;
9975 set_num_domains(map);
9976 map->num_members = map->num_members * map->num_domains;
9977 update_num_data_stripes(map, imsm_dev_size(dev));
9978
9979 /* replace dev<->dev_new */
9980 dv->dev = dev_new;
9981 }
9982 /* update disk order table */
9983 for (du = super->disks; du; du = du->next)
9984 if (du->index >= 0)
9985 set_imsm_ord_tbl_ent(map, du->index, du->index);
9986 for (du = super->missing; du; du = du->next)
9987 if (du->index >= 0) {
9988 set_imsm_ord_tbl_ent(map, du->index, du->index);
9989 mark_missing(super, dv->dev, &du->disk, du->index);
9990 }
9991
9992 return 1;
9993 }
9994
9995 static void imsm_process_update(struct supertype *st,
9996 struct metadata_update *update)
9997 {
9998 /**
9999 * crack open the metadata_update envelope to find the update record
10000 * update can be one of:
10001 * update_reshape_container_disks - all the arrays in the container
10002 * are being reshaped to have more devices. We need to mark
10003 * the arrays for general migration and convert selected spares
10004 * into active devices.
10005 * update_activate_spare - a spare device has replaced a failed
10006 * device in an array, update the disk_ord_tbl. If this disk is
10007 * present in all member arrays then also clear the SPARE_DISK
10008 * flag
10009 * update_create_array
10010 * update_kill_array
10011 * update_rename_array
10012 * update_add_remove_disk
10013 */
10014 struct intel_super *super = st->sb;
10015 struct imsm_super *mpb;
10016 enum imsm_update_type type = *(enum imsm_update_type *) update->buf;
10017
10018 /* update requires a larger buf but the allocation failed */
10019 if (super->next_len && !super->next_buf) {
10020 super->next_len = 0;
10021 return;
10022 }
10023
10024 if (super->next_buf) {
10025 memcpy(super->next_buf, super->buf, super->len);
10026 free(super->buf);
10027 super->len = super->next_len;
10028 super->buf = super->next_buf;
10029
10030 super->next_len = 0;
10031 super->next_buf = NULL;
10032 }
10033
10034 mpb = super->anchor;
10035
10036 switch (type) {
10037 case update_general_migration_checkpoint: {
10038 struct intel_dev *id;
10039 struct imsm_update_general_migration_checkpoint *u =
10040 (void *)update->buf;
10041
10042 dprintf("called for update_general_migration_checkpoint\n");
10043
10044 /* find device under general migration */
10045 for (id = super->devlist ; id; id = id->next) {
10046 if (is_gen_migration(id->dev)) {
10047 set_vol_curr_migr_unit(id->dev,
10048 u->curr_migr_unit);
10049 super->updates_pending++;
10050 }
10051 }
10052 break;
10053 }
10054 case update_takeover: {
10055 struct imsm_update_takeover *u = (void *)update->buf;
10056 if (apply_takeover_update(u, super, &update->space_list)) {
10057 imsm_update_version_info(super);
10058 super->updates_pending++;
10059 }
10060 break;
10061 }
10062
10063 case update_reshape_container_disks: {
10064 struct imsm_update_reshape *u = (void *)update->buf;
10065 if (apply_reshape_container_disks_update(
10066 u, super, &update->space_list))
10067 super->updates_pending++;
10068 break;
10069 }
10070 case update_reshape_migration: {
10071 struct imsm_update_reshape_migration *u = (void *)update->buf;
10072 if (apply_reshape_migration_update(
10073 u, super, &update->space_list))
10074 super->updates_pending++;
10075 break;
10076 }
10077 case update_size_change: {
10078 struct imsm_update_size_change *u = (void *)update->buf;
10079 if (apply_size_change_update(u, super))
10080 super->updates_pending++;
10081 break;
10082 }
10083 case update_activate_spare: {
10084 struct imsm_update_activate_spare *u = (void *) update->buf;
10085
10086 if (prepare_spare_to_activate(st, u) &&
10087 apply_update_activate_spare(u, super, st->arrays))
10088 super->updates_pending++;
10089 break;
10090 }
10091 case update_create_array: {
10092 /* someone wants to create a new array, we need to be aware of
10093 * a few races/collisions:
10094 * 1/ 'Create' called by two separate instances of mdadm
10095 * 2/ 'Create' versus 'activate_spare': mdadm has chosen
10096 * devices that have since been assimilated via
10097 * activate_spare.
10098 * In the event this update can not be carried out mdadm will
10099 * (FIX ME) notice that its update did not take hold.
10100 */
10101 struct imsm_update_create_array *u = (void *) update->buf;
10102 struct intel_dev *dv;
10103 struct imsm_dev *dev;
10104 struct imsm_map *map, *new_map;
10105 unsigned long long start, end;
10106 unsigned long long new_start, new_end;
10107 int i;
10108 struct disk_info *inf;
10109 struct dl *dl;
10110
10111 /* handle racing creates: first come first serve */
10112 if (u->dev_idx < mpb->num_raid_devs) {
10113 dprintf("subarray %d already defined\n", u->dev_idx);
10114 goto create_error;
10115 }
10116
10117 /* check update is next in sequence */
10118 if (u->dev_idx != mpb->num_raid_devs) {
10119 dprintf("can not create array %d expected index %d\n",
10120 u->dev_idx, mpb->num_raid_devs);
10121 goto create_error;
10122 }
10123
10124 new_map = get_imsm_map(&u->dev, MAP_0);
10125 new_start = pba_of_lba0(new_map);
10126 new_end = new_start + per_dev_array_size(new_map);
10127 inf = get_disk_info(u);
10128
10129 /* handle activate_spare versus create race:
10130 * check to make sure that overlapping arrays do not include
10131 * overalpping disks
10132 */
10133 for (i = 0; i < mpb->num_raid_devs; i++) {
10134 dev = get_imsm_dev(super, i);
10135 map = get_imsm_map(dev, MAP_0);
10136 start = pba_of_lba0(map);
10137 end = start + per_dev_array_size(map);
10138 if ((new_start >= start && new_start <= end) ||
10139 (start >= new_start && start <= new_end))
10140 /* overlap */;
10141 else
10142 continue;
10143
10144 if (disks_overlap(super, i, u)) {
10145 dprintf("arrays overlap\n");
10146 goto create_error;
10147 }
10148 }
10149
10150 /* check that prepare update was successful */
10151 if (!update->space) {
10152 dprintf("prepare update failed\n");
10153 goto create_error;
10154 }
10155
10156 /* check that all disks are still active before committing
10157 * changes. FIXME: could we instead handle this by creating a
10158 * degraded array? That's probably not what the user expects,
10159 * so better to drop this update on the floor.
10160 */
10161 for (i = 0; i < new_map->num_members; i++) {
10162 dl = serial_to_dl(inf[i].serial, super);
10163 if (!dl) {
10164 dprintf("disk disappeared\n");
10165 goto create_error;
10166 }
10167 }
10168
10169 super->updates_pending++;
10170
10171 /* convert spares to members and fixup ord_tbl */
10172 for (i = 0; i < new_map->num_members; i++) {
10173 dl = serial_to_dl(inf[i].serial, super);
10174 if (dl->index == -1) {
10175 dl->index = mpb->num_disks;
10176 mpb->num_disks++;
10177 dl->disk.status |= CONFIGURED_DISK;
10178 dl->disk.status &= ~SPARE_DISK;
10179 }
10180 set_imsm_ord_tbl_ent(new_map, i, dl->index);
10181 }
10182
10183 dv = update->space;
10184 dev = dv->dev;
10185 update->space = NULL;
10186 imsm_copy_dev(dev, &u->dev);
10187 dv->index = u->dev_idx;
10188 dv->next = super->devlist;
10189 super->devlist = dv;
10190 mpb->num_raid_devs++;
10191
10192 imsm_update_version_info(super);
10193 break;
10194 create_error:
10195 /* mdmon knows how to release update->space, but not
10196 * ((struct intel_dev *) update->space)->dev
10197 */
10198 if (update->space) {
10199 dv = update->space;
10200 free(dv->dev);
10201 }
10202 break;
10203 }
10204 case update_kill_array: {
10205 struct imsm_update_kill_array *u = (void *) update->buf;
10206 int victim = u->dev_idx;
10207 struct active_array *a;
10208 struct intel_dev **dp;
10209
10210 /* sanity check that we are not affecting the uuid of
10211 * active arrays, or deleting an active array
10212 *
10213 * FIXME when immutable ids are available, but note that
10214 * we'll also need to fixup the invalidated/active
10215 * subarray indexes in mdstat
10216 */
10217 for (a = st->arrays; a; a = a->next)
10218 if (a->info.container_member >= victim)
10219 break;
10220 /* by definition if mdmon is running at least one array
10221 * is active in the container, so checking
10222 * mpb->num_raid_devs is just extra paranoia
10223 */
10224 if (a || mpb->num_raid_devs == 1 || victim >= super->anchor->num_raid_devs) {
10225 dprintf("failed to delete subarray-%d\n", victim);
10226 break;
10227 }
10228
10229 for (dp = &super->devlist; *dp;)
10230 if ((*dp)->index == (unsigned)super->current_vol) {
10231 *dp = (*dp)->next;
10232 } else {
10233 if ((*dp)->index > (unsigned)victim)
10234 (*dp)->index--;
10235 dp = &(*dp)->next;
10236 }
10237 mpb->num_raid_devs--;
10238 super->updates_pending++;
10239 break;
10240 }
10241 case update_rename_array: {
10242 struct imsm_update_rename_array *u = (void *) update->buf;
10243 char name[MAX_RAID_SERIAL_LEN+1];
10244 int target = u->dev_idx;
10245 struct active_array *a;
10246 struct imsm_dev *dev;
10247
10248 /* sanity check that we are not affecting the uuid of
10249 * an active array
10250 */
10251 memset(name, 0, sizeof(name));
10252 snprintf(name, MAX_RAID_SERIAL_LEN, "%s", (char *) u->name);
10253 name[MAX_RAID_SERIAL_LEN] = '\0';
10254 for (a = st->arrays; a; a = a->next)
10255 if (a->info.container_member == target)
10256 break;
10257 dev = get_imsm_dev(super, u->dev_idx);
10258 if (a || !check_name(super, name, 1)) {
10259 dprintf("failed to rename subarray-%d\n", target);
10260 break;
10261 }
10262
10263 memcpy(dev->volume, name, MAX_RAID_SERIAL_LEN);
10264 super->updates_pending++;
10265 break;
10266 }
10267 case update_add_remove_disk: {
10268 /* we may be able to repair some arrays if disks are
10269 * being added, check the status of add_remove_disk
10270 * if discs has been added.
10271 */
10272 if (add_remove_disk_update(super)) {
10273 struct active_array *a;
10274
10275 super->updates_pending++;
10276 for (a = st->arrays; a; a = a->next)
10277 a->check_degraded = 1;
10278 }
10279 break;
10280 }
10281 case update_prealloc_badblocks_mem:
10282 break;
10283 case update_rwh_policy: {
10284 struct imsm_update_rwh_policy *u = (void *)update->buf;
10285 int target = u->dev_idx;
10286 struct imsm_dev *dev = get_imsm_dev(super, target);
10287
10288 if (dev->rwh_policy != u->new_policy) {
10289 dev->rwh_policy = u->new_policy;
10290 super->updates_pending++;
10291 }
10292 break;
10293 }
10294 default:
10295 pr_err("error: unsupported process update type:(type: %d)\n", type);
10296 }
10297 }
10298
10299 static struct mdinfo *get_spares_for_grow(struct supertype *st);
10300
10301 static int imsm_prepare_update(struct supertype *st,
10302 struct metadata_update *update)
10303 {
10304 /**
10305 * Allocate space to hold new disk entries, raid-device entries or a new
10306 * mpb if necessary. The manager synchronously waits for updates to
10307 * complete in the monitor, so new mpb buffers allocated here can be
10308 * integrated by the monitor thread without worrying about live pointers
10309 * in the manager thread.
10310 */
10311 enum imsm_update_type type;
10312 struct intel_super *super = st->sb;
10313 unsigned int sector_size = super->sector_size;
10314 struct imsm_super *mpb = super->anchor;
10315 size_t buf_len;
10316 size_t len = 0;
10317
10318 if (update->len < (int)sizeof(type))
10319 return 0;
10320
10321 type = *(enum imsm_update_type *) update->buf;
10322
10323 switch (type) {
10324 case update_general_migration_checkpoint:
10325 if (update->len < (int)sizeof(struct imsm_update_general_migration_checkpoint))
10326 return 0;
10327 dprintf("called for update_general_migration_checkpoint\n");
10328 break;
10329 case update_takeover: {
10330 struct imsm_update_takeover *u = (void *)update->buf;
10331 if (update->len < (int)sizeof(*u))
10332 return 0;
10333 if (u->direction == R0_TO_R10) {
10334 void **tail = (void **)&update->space_list;
10335 struct imsm_dev *dev = get_imsm_dev(super, u->subarray);
10336 struct imsm_map *map = get_imsm_map(dev, MAP_0);
10337 int num_members = map->num_members;
10338 void *space;
10339 int size, i;
10340 /* allocate memory for added disks */
10341 for (i = 0; i < num_members; i++) {
10342 size = sizeof(struct dl);
10343 space = xmalloc(size);
10344 *tail = space;
10345 tail = space;
10346 *tail = NULL;
10347 }
10348 /* allocate memory for new device */
10349 size = sizeof_imsm_dev(super->devlist->dev, 0) +
10350 (num_members * sizeof(__u32));
10351 space = xmalloc(size);
10352 *tail = space;
10353 tail = space;
10354 *tail = NULL;
10355 len = disks_to_mpb_size(num_members * 2);
10356 }
10357
10358 break;
10359 }
10360 case update_reshape_container_disks: {
10361 /* Every raid device in the container is about to
10362 * gain some more devices, and we will enter a
10363 * reconfiguration.
10364 * So each 'imsm_map' will be bigger, and the imsm_vol
10365 * will now hold 2 of them.
10366 * Thus we need new 'struct imsm_dev' allocations sized
10367 * as sizeof_imsm_dev but with more devices in both maps.
10368 */
10369 struct imsm_update_reshape *u = (void *)update->buf;
10370 struct intel_dev *dl;
10371 void **space_tail = (void**)&update->space_list;
10372
10373 if (update->len < (int)sizeof(*u))
10374 return 0;
10375
10376 dprintf("for update_reshape\n");
10377
10378 for (dl = super->devlist; dl; dl = dl->next) {
10379 int size = sizeof_imsm_dev(dl->dev, 1);
10380 void *s;
10381 if (u->new_raid_disks > u->old_raid_disks)
10382 size += sizeof(__u32)*2*
10383 (u->new_raid_disks - u->old_raid_disks);
10384 s = xmalloc(size);
10385 *space_tail = s;
10386 space_tail = s;
10387 *space_tail = NULL;
10388 }
10389
10390 len = disks_to_mpb_size(u->new_raid_disks);
10391 dprintf("New anchor length is %llu\n", (unsigned long long)len);
10392 break;
10393 }
10394 case update_reshape_migration: {
10395 /* for migration level 0->5 we need to add disks
10396 * so the same as for container operation we will copy
10397 * device to the bigger location.
10398 * in memory prepared device and new disk area are prepared
10399 * for usage in process update
10400 */
10401 struct imsm_update_reshape_migration *u = (void *)update->buf;
10402 struct intel_dev *id;
10403 void **space_tail = (void **)&update->space_list;
10404 int size;
10405 void *s;
10406 int current_level = -1;
10407
10408 if (update->len < (int)sizeof(*u))
10409 return 0;
10410
10411 dprintf("for update_reshape\n");
10412
10413 /* add space for bigger array in update
10414 */
10415 for (id = super->devlist; id; id = id->next) {
10416 if (id->index == (unsigned)u->subdev) {
10417 size = sizeof_imsm_dev(id->dev, 1);
10418 if (u->new_raid_disks > u->old_raid_disks)
10419 size += sizeof(__u32)*2*
10420 (u->new_raid_disks - u->old_raid_disks);
10421 s = xmalloc(size);
10422 *space_tail = s;
10423 space_tail = s;
10424 *space_tail = NULL;
10425 break;
10426 }
10427 }
10428 if (update->space_list == NULL)
10429 break;
10430
10431 /* add space for disk in update
10432 */
10433 size = sizeof(struct dl);
10434 s = xmalloc(size);
10435 *space_tail = s;
10436 space_tail = s;
10437 *space_tail = NULL;
10438
10439 /* add spare device to update
10440 */
10441 for (id = super->devlist ; id; id = id->next)
10442 if (id->index == (unsigned)u->subdev) {
10443 struct imsm_dev *dev;
10444 struct imsm_map *map;
10445
10446 dev = get_imsm_dev(super, u->subdev);
10447 map = get_imsm_map(dev, MAP_0);
10448 current_level = map->raid_level;
10449 break;
10450 }
10451 if (u->new_level == 5 && u->new_level != current_level) {
10452 struct mdinfo *spares;
10453
10454 spares = get_spares_for_grow(st);
10455 if (spares) {
10456 struct dl *dl;
10457 struct mdinfo *dev;
10458
10459 dev = spares->devs;
10460 if (dev) {
10461 u->new_disks[0] =
10462 makedev(dev->disk.major,
10463 dev->disk.minor);
10464 dl = get_disk_super(super,
10465 dev->disk.major,
10466 dev->disk.minor);
10467 dl->index = u->old_raid_disks;
10468 dev = dev->next;
10469 }
10470 sysfs_free(spares);
10471 }
10472 }
10473 len = disks_to_mpb_size(u->new_raid_disks);
10474 dprintf("New anchor length is %llu\n", (unsigned long long)len);
10475 break;
10476 }
10477 case update_size_change: {
10478 if (update->len < (int)sizeof(struct imsm_update_size_change))
10479 return 0;
10480 break;
10481 }
10482 case update_activate_spare: {
10483 if (update->len < (int)sizeof(struct imsm_update_activate_spare))
10484 return 0;
10485 break;
10486 }
10487 case update_create_array: {
10488 struct imsm_update_create_array *u = (void *) update->buf;
10489 struct intel_dev *dv;
10490 struct imsm_dev *dev = &u->dev;
10491 struct imsm_map *map = get_imsm_map(dev, MAP_0);
10492 struct dl *dl;
10493 struct disk_info *inf;
10494 int i;
10495 int activate = 0;
10496
10497 if (update->len < (int)sizeof(*u))
10498 return 0;
10499
10500 inf = get_disk_info(u);
10501 len = sizeof_imsm_dev(dev, 1);
10502 /* allocate a new super->devlist entry */
10503 dv = xmalloc(sizeof(*dv));
10504 dv->dev = xmalloc(len);
10505 update->space = dv;
10506
10507 /* count how many spares will be converted to members */
10508 for (i = 0; i < map->num_members; i++) {
10509 dl = serial_to_dl(inf[i].serial, super);
10510 if (!dl) {
10511 /* hmm maybe it failed?, nothing we can do about
10512 * it here
10513 */
10514 continue;
10515 }
10516 if (count_memberships(dl, super) == 0)
10517 activate++;
10518 }
10519 len += activate * sizeof(struct imsm_disk);
10520 break;
10521 }
10522 case update_kill_array: {
10523 if (update->len < (int)sizeof(struct imsm_update_kill_array))
10524 return 0;
10525 break;
10526 }
10527 case update_rename_array: {
10528 if (update->len < (int)sizeof(struct imsm_update_rename_array))
10529 return 0;
10530 break;
10531 }
10532 case update_add_remove_disk:
10533 /* no update->len needed */
10534 break;
10535 case update_prealloc_badblocks_mem:
10536 super->extra_space += sizeof(struct bbm_log) -
10537 get_imsm_bbm_log_size(super->bbm_log);
10538 break;
10539 case update_rwh_policy: {
10540 if (update->len < (int)sizeof(struct imsm_update_rwh_policy))
10541 return 0;
10542 break;
10543 }
10544 default:
10545 return 0;
10546 }
10547
10548 /* check if we need a larger metadata buffer */
10549 if (super->next_buf)
10550 buf_len = super->next_len;
10551 else
10552 buf_len = super->len;
10553
10554 if (__le32_to_cpu(mpb->mpb_size) + super->extra_space + len > buf_len) {
10555 /* ok we need a larger buf than what is currently allocated
10556 * if this allocation fails process_update will notice that
10557 * ->next_len is set and ->next_buf is NULL
10558 */
10559 buf_len = ROUND_UP(__le32_to_cpu(mpb->mpb_size) +
10560 super->extra_space + len, sector_size);
10561 if (super->next_buf)
10562 free(super->next_buf);
10563
10564 super->next_len = buf_len;
10565 if (posix_memalign(&super->next_buf, sector_size, buf_len) == 0)
10566 memset(super->next_buf, 0, buf_len);
10567 else
10568 super->next_buf = NULL;
10569 }
10570 return 1;
10571 }
10572
10573 /* must be called while manager is quiesced */
10574 static void imsm_delete(struct intel_super *super, struct dl **dlp, unsigned index)
10575 {
10576 struct imsm_super *mpb = super->anchor;
10577 struct dl *iter;
10578 struct imsm_dev *dev;
10579 struct imsm_map *map;
10580 unsigned int i, j, num_members;
10581 __u32 ord, ord_map0;
10582 struct bbm_log *log = super->bbm_log;
10583
10584 dprintf("deleting device[%d] from imsm_super\n", index);
10585
10586 /* shift all indexes down one */
10587 for (iter = super->disks; iter; iter = iter->next)
10588 if (iter->index > (int)index)
10589 iter->index--;
10590 for (iter = super->missing; iter; iter = iter->next)
10591 if (iter->index > (int)index)
10592 iter->index--;
10593
10594 for (i = 0; i < mpb->num_raid_devs; i++) {
10595 dev = get_imsm_dev(super, i);
10596 map = get_imsm_map(dev, MAP_0);
10597 num_members = map->num_members;
10598 for (j = 0; j < num_members; j++) {
10599 /* update ord entries being careful not to propagate
10600 * ord-flags to the first map
10601 */
10602 ord = get_imsm_ord_tbl_ent(dev, j, MAP_X);
10603 ord_map0 = get_imsm_ord_tbl_ent(dev, j, MAP_0);
10604
10605 if (ord_to_idx(ord) <= index)
10606 continue;
10607
10608 map = get_imsm_map(dev, MAP_0);
10609 set_imsm_ord_tbl_ent(map, j, ord_map0 - 1);
10610 map = get_imsm_map(dev, MAP_1);
10611 if (map)
10612 set_imsm_ord_tbl_ent(map, j, ord - 1);
10613 }
10614 }
10615
10616 for (i = 0; i < log->entry_count; i++) {
10617 struct bbm_log_entry *entry = &log->marked_block_entries[i];
10618
10619 if (entry->disk_ordinal <= index)
10620 continue;
10621 entry->disk_ordinal--;
10622 }
10623
10624 mpb->num_disks--;
10625 super->updates_pending++;
10626 if (*dlp) {
10627 struct dl *dl = *dlp;
10628
10629 *dlp = (*dlp)->next;
10630 __free_imsm_disk(dl, 1);
10631 }
10632 }
10633
10634 static int imsm_get_allowed_degradation(int level, int raid_disks,
10635 struct intel_super *super,
10636 struct imsm_dev *dev)
10637 {
10638 switch (level) {
10639 case 1:
10640 case 10:{
10641 int ret_val = 0;
10642 struct imsm_map *map;
10643 int i;
10644
10645 ret_val = raid_disks/2;
10646 /* check map if all disks pairs not failed
10647 * in both maps
10648 */
10649 map = get_imsm_map(dev, MAP_0);
10650 for (i = 0; i < ret_val; i++) {
10651 int degradation = 0;
10652 if (get_imsm_disk(super, i) == NULL)
10653 degradation++;
10654 if (get_imsm_disk(super, i + 1) == NULL)
10655 degradation++;
10656 if (degradation == 2)
10657 return 0;
10658 }
10659 map = get_imsm_map(dev, MAP_1);
10660 /* if there is no second map
10661 * result can be returned
10662 */
10663 if (map == NULL)
10664 return ret_val;
10665 /* check degradation in second map
10666 */
10667 for (i = 0; i < ret_val; i++) {
10668 int degradation = 0;
10669 if (get_imsm_disk(super, i) == NULL)
10670 degradation++;
10671 if (get_imsm_disk(super, i + 1) == NULL)
10672 degradation++;
10673 if (degradation == 2)
10674 return 0;
10675 }
10676 return ret_val;
10677 }
10678 case 5:
10679 return 1;
10680 case 6:
10681 return 2;
10682 default:
10683 return 0;
10684 }
10685 }
10686
10687 /*******************************************************************************
10688 * Function: validate_container_imsm
10689 * Description: This routine validates container after assemble,
10690 * eg. if devices in container are under the same controller.
10691 *
10692 * Parameters:
10693 * info : linked list with info about devices used in array
10694 * Returns:
10695 * 1 : HBA mismatch
10696 * 0 : Success
10697 ******************************************************************************/
10698 int validate_container_imsm(struct mdinfo *info)
10699 {
10700 if (check_env("IMSM_NO_PLATFORM"))
10701 return 0;
10702
10703 struct sys_dev *idev;
10704 struct sys_dev *hba = NULL;
10705 struct sys_dev *intel_devices = find_intel_devices();
10706 char *dev_path = devt_to_devpath(makedev(info->disk.major,
10707 info->disk.minor), 1, NULL);
10708
10709 for (idev = intel_devices; idev; idev = idev->next) {
10710 if (dev_path && strstr(dev_path, idev->path)) {
10711 hba = idev;
10712 break;
10713 }
10714 }
10715 if (dev_path)
10716 free(dev_path);
10717
10718 if (!hba) {
10719 pr_err("WARNING - Cannot detect HBA for device %s!\n",
10720 devid2kname(makedev(info->disk.major, info->disk.minor)));
10721 return 1;
10722 }
10723
10724 const struct imsm_orom *orom = get_orom_by_device_id(hba->dev_id);
10725 struct mdinfo *dev;
10726
10727 for (dev = info->next; dev; dev = dev->next) {
10728 dev_path = devt_to_devpath(makedev(dev->disk.major,
10729 dev->disk.minor), 1, NULL);
10730
10731 struct sys_dev *hba2 = NULL;
10732 for (idev = intel_devices; idev; idev = idev->next) {
10733 if (dev_path && strstr(dev_path, idev->path)) {
10734 hba2 = idev;
10735 break;
10736 }
10737 }
10738 if (dev_path)
10739 free(dev_path);
10740
10741 const struct imsm_orom *orom2 = hba2 == NULL ? NULL :
10742 get_orom_by_device_id(hba2->dev_id);
10743
10744 if (hba2 && hba->type != hba2->type) {
10745 pr_err("WARNING - HBAs of devices do not match %s != %s\n",
10746 get_sys_dev_type(hba->type), get_sys_dev_type(hba2->type));
10747 return 1;
10748 }
10749
10750 if (orom != orom2) {
10751 pr_err("WARNING - IMSM container assembled with disks under different HBAs!\n"
10752 " This operation is not supported and can lead to data loss.\n");
10753 return 1;
10754 }
10755
10756 if (!orom) {
10757 pr_err("WARNING - IMSM container assembled with disks under HBAs without IMSM platform support!\n"
10758 " This operation is not supported and can lead to data loss.\n");
10759 return 1;
10760 }
10761 }
10762
10763 return 0;
10764 }
10765
10766 /*******************************************************************************
10767 * Function: imsm_record_badblock
10768 * Description: This routine stores new bad block record in BBM log
10769 *
10770 * Parameters:
10771 * a : array containing a bad block
10772 * slot : disk number containing a bad block
10773 * sector : bad block sector
10774 * length : bad block sectors range
10775 * Returns:
10776 * 1 : Success
10777 * 0 : Error
10778 ******************************************************************************/
10779 static int imsm_record_badblock(struct active_array *a, int slot,
10780 unsigned long long sector, int length)
10781 {
10782 struct intel_super *super = a->container->sb;
10783 int ord;
10784 int ret;
10785
10786 ord = imsm_disk_slot_to_ord(a, slot);
10787 if (ord < 0)
10788 return 0;
10789
10790 ret = record_new_badblock(super->bbm_log, ord_to_idx(ord), sector,
10791 length);
10792 if (ret)
10793 super->updates_pending++;
10794
10795 return ret;
10796 }
10797 /*******************************************************************************
10798 * Function: imsm_clear_badblock
10799 * Description: This routine clears bad block record from BBM log
10800 *
10801 * Parameters:
10802 * a : array containing a bad block
10803 * slot : disk number containing a bad block
10804 * sector : bad block sector
10805 * length : bad block sectors range
10806 * Returns:
10807 * 1 : Success
10808 * 0 : Error
10809 ******************************************************************************/
10810 static int imsm_clear_badblock(struct active_array *a, int slot,
10811 unsigned long long sector, int length)
10812 {
10813 struct intel_super *super = a->container->sb;
10814 int ord;
10815 int ret;
10816
10817 ord = imsm_disk_slot_to_ord(a, slot);
10818 if (ord < 0)
10819 return 0;
10820
10821 ret = clear_badblock(super->bbm_log, ord_to_idx(ord), sector, length);
10822 if (ret)
10823 super->updates_pending++;
10824
10825 return ret;
10826 }
10827 /*******************************************************************************
10828 * Function: imsm_get_badblocks
10829 * Description: This routine get list of bad blocks for an array
10830 *
10831 * Parameters:
10832 * a : array
10833 * slot : disk number
10834 * Returns:
10835 * bb : structure containing bad blocks
10836 * NULL : error
10837 ******************************************************************************/
10838 static struct md_bb *imsm_get_badblocks(struct active_array *a, int slot)
10839 {
10840 int inst = a->info.container_member;
10841 struct intel_super *super = a->container->sb;
10842 struct imsm_dev *dev = get_imsm_dev(super, inst);
10843 struct imsm_map *map = get_imsm_map(dev, MAP_0);
10844 int ord;
10845
10846 ord = imsm_disk_slot_to_ord(a, slot);
10847 if (ord < 0)
10848 return NULL;
10849
10850 get_volume_badblocks(super->bbm_log, ord_to_idx(ord), pba_of_lba0(map),
10851 per_dev_array_size(map), &super->bb);
10852
10853 return &super->bb;
10854 }
10855 /*******************************************************************************
10856 * Function: examine_badblocks_imsm
10857 * Description: Prints list of bad blocks on a disk to the standard output
10858 *
10859 * Parameters:
10860 * st : metadata handler
10861 * fd : open file descriptor for device
10862 * devname : device name
10863 * Returns:
10864 * 0 : Success
10865 * 1 : Error
10866 ******************************************************************************/
10867 static int examine_badblocks_imsm(struct supertype *st, int fd, char *devname)
10868 {
10869 struct intel_super *super = st->sb;
10870 struct bbm_log *log = super->bbm_log;
10871 struct dl *d = NULL;
10872 int any = 0;
10873
10874 for (d = super->disks; d ; d = d->next) {
10875 if (strcmp(d->devname, devname) == 0)
10876 break;
10877 }
10878
10879 if ((d == NULL) || (d->index < 0)) { /* serial mismatch probably */
10880 pr_err("%s doesn't appear to be part of a raid array\n",
10881 devname);
10882 return 1;
10883 }
10884
10885 if (log != NULL) {
10886 unsigned int i;
10887 struct bbm_log_entry *entry = &log->marked_block_entries[0];
10888
10889 for (i = 0; i < log->entry_count; i++) {
10890 if (entry[i].disk_ordinal == d->index) {
10891 unsigned long long sector = __le48_to_cpu(
10892 &entry[i].defective_block_start);
10893 int cnt = entry[i].marked_count + 1;
10894
10895 if (!any) {
10896 printf("Bad-blocks on %s:\n", devname);
10897 any = 1;
10898 }
10899
10900 printf("%20llu for %d sectors\n", sector, cnt);
10901 }
10902 }
10903 }
10904
10905 if (!any)
10906 printf("No bad-blocks list configured on %s\n", devname);
10907
10908 return 0;
10909 }
10910 /*******************************************************************************
10911 * Function: init_migr_record_imsm
10912 * Description: Function inits imsm migration record
10913 * Parameters:
10914 * super : imsm internal array info
10915 * dev : device under migration
10916 * info : general array info to find the smallest device
10917 * Returns:
10918 * none
10919 ******************************************************************************/
10920 void init_migr_record_imsm(struct supertype *st, struct imsm_dev *dev,
10921 struct mdinfo *info)
10922 {
10923 struct intel_super *super = st->sb;
10924 struct migr_record *migr_rec = super->migr_rec;
10925 int new_data_disks;
10926 unsigned long long dsize, dev_sectors;
10927 long long unsigned min_dev_sectors = -1LLU;
10928 struct imsm_map *map_dest = get_imsm_map(dev, MAP_0);
10929 struct imsm_map *map_src = get_imsm_map(dev, MAP_1);
10930 unsigned long long num_migr_units;
10931 unsigned long long array_blocks;
10932 struct dl *dl_disk = NULL;
10933
10934 memset(migr_rec, 0, sizeof(struct migr_record));
10935 migr_rec->family_num = __cpu_to_le32(super->anchor->family_num);
10936
10937 /* only ascending reshape supported now */
10938 migr_rec->ascending_migr = __cpu_to_le32(1);
10939
10940 migr_rec->dest_depth_per_unit = GEN_MIGR_AREA_SIZE /
10941 max(map_dest->blocks_per_strip, map_src->blocks_per_strip);
10942 migr_rec->dest_depth_per_unit *=
10943 max(map_dest->blocks_per_strip, map_src->blocks_per_strip);
10944 new_data_disks = imsm_num_data_members(map_dest);
10945 migr_rec->blocks_per_unit =
10946 __cpu_to_le32(migr_rec->dest_depth_per_unit * new_data_disks);
10947 migr_rec->dest_depth_per_unit =
10948 __cpu_to_le32(migr_rec->dest_depth_per_unit);
10949 array_blocks = info->component_size * new_data_disks;
10950 num_migr_units =
10951 array_blocks / __le32_to_cpu(migr_rec->blocks_per_unit);
10952
10953 if (array_blocks % __le32_to_cpu(migr_rec->blocks_per_unit))
10954 num_migr_units++;
10955 set_num_migr_units(migr_rec, num_migr_units);
10956
10957 migr_rec->post_migr_vol_cap = dev->size_low;
10958 migr_rec->post_migr_vol_cap_hi = dev->size_high;
10959
10960 /* Find the smallest dev */
10961 for (dl_disk = super->disks; dl_disk ; dl_disk = dl_disk->next) {
10962 /* ignore spares in container */
10963 if (dl_disk->index < 0)
10964 continue;
10965 get_dev_size(dl_disk->fd, NULL, &dsize);
10966 dev_sectors = dsize / 512;
10967 if (dev_sectors < min_dev_sectors)
10968 min_dev_sectors = dev_sectors;
10969 }
10970 set_migr_chkp_area_pba(migr_rec, min_dev_sectors -
10971 RAID_DISK_RESERVED_BLOCKS_IMSM_HI);
10972
10973 write_imsm_migr_rec(st);
10974
10975 return;
10976 }
10977
10978 /*******************************************************************************
10979 * Function: save_backup_imsm
10980 * Description: Function saves critical data stripes to Migration Copy Area
10981 * and updates the current migration unit status.
10982 * Use restore_stripes() to form a destination stripe,
10983 * and to write it to the Copy Area.
10984 * Parameters:
10985 * st : supertype information
10986 * dev : imsm device that backup is saved for
10987 * info : general array info
10988 * buf : input buffer
10989 * length : length of data to backup (blocks_per_unit)
10990 * Returns:
10991 * 0 : success
10992 *, -1 : fail
10993 ******************************************************************************/
10994 int save_backup_imsm(struct supertype *st,
10995 struct imsm_dev *dev,
10996 struct mdinfo *info,
10997 void *buf,
10998 int length)
10999 {
11000 int rv = -1;
11001 struct intel_super *super = st->sb;
11002 int i;
11003 struct imsm_map *map_dest = get_imsm_map(dev, MAP_0);
11004 int new_disks = map_dest->num_members;
11005 int dest_layout = 0;
11006 int dest_chunk, targets[new_disks];
11007 unsigned long long start, target_offsets[new_disks];
11008 int data_disks = imsm_num_data_members(map_dest);
11009
11010 for (i = 0; i < new_disks; i++) {
11011 struct dl *dl_disk = get_imsm_dl_disk(super, i);
11012 if (dl_disk && is_fd_valid(dl_disk->fd))
11013 targets[i] = dl_disk->fd;
11014 else
11015 goto abort;
11016 }
11017
11018 start = info->reshape_progress * 512;
11019 for (i = 0; i < new_disks; i++) {
11020 target_offsets[i] = migr_chkp_area_pba(super->migr_rec) * 512;
11021 /* move back copy area adderss, it will be moved forward
11022 * in restore_stripes() using start input variable
11023 */
11024 target_offsets[i] -= start/data_disks;
11025 }
11026
11027 dest_layout = imsm_level_to_layout(map_dest->raid_level);
11028 dest_chunk = __le16_to_cpu(map_dest->blocks_per_strip) * 512;
11029
11030 if (restore_stripes(targets, /* list of dest devices */
11031 target_offsets, /* migration record offsets */
11032 new_disks,
11033 dest_chunk,
11034 map_dest->raid_level,
11035 dest_layout,
11036 -1, /* source backup file descriptor */
11037 0, /* input buf offset
11038 * always 0 buf is already offseted */
11039 start,
11040 length,
11041 buf) != 0) {
11042 pr_err("Error restoring stripes\n");
11043 goto abort;
11044 }
11045
11046 rv = 0;
11047
11048 abort:
11049 return rv;
11050 }
11051
11052 /*******************************************************************************
11053 * Function: save_checkpoint_imsm
11054 * Description: Function called for current unit status update
11055 * in the migration record. It writes it to disk.
11056 * Parameters:
11057 * super : imsm internal array info
11058 * info : general array info
11059 * Returns:
11060 * 0: success
11061 * 1: failure
11062 * 2: failure, means no valid migration record
11063 * / no general migration in progress /
11064 ******************************************************************************/
11065 int save_checkpoint_imsm(struct supertype *st, struct mdinfo *info, int state)
11066 {
11067 struct intel_super *super = st->sb;
11068 unsigned long long blocks_per_unit;
11069 unsigned long long curr_migr_unit;
11070
11071 if (load_imsm_migr_rec(super) != 0) {
11072 dprintf("imsm: ERROR: Cannot read migration record for checkpoint save.\n");
11073 return 1;
11074 }
11075
11076 blocks_per_unit = __le32_to_cpu(super->migr_rec->blocks_per_unit);
11077 if (blocks_per_unit == 0) {
11078 dprintf("imsm: no migration in progress.\n");
11079 return 2;
11080 }
11081 curr_migr_unit = info->reshape_progress / blocks_per_unit;
11082 /* check if array is alligned to copy area
11083 * if it is not alligned, add one to current migration unit value
11084 * this can happend on array reshape finish only
11085 */
11086 if (info->reshape_progress % blocks_per_unit)
11087 curr_migr_unit++;
11088
11089 set_current_migr_unit(super->migr_rec, curr_migr_unit);
11090 super->migr_rec->rec_status = __cpu_to_le32(state);
11091 set_migr_dest_1st_member_lba(super->migr_rec,
11092 super->migr_rec->dest_depth_per_unit * curr_migr_unit);
11093
11094 if (write_imsm_migr_rec(st) < 0) {
11095 dprintf("imsm: Cannot write migration record outside backup area\n");
11096 return 1;
11097 }
11098
11099 return 0;
11100 }
11101
11102 /*******************************************************************************
11103 * Function: recover_backup_imsm
11104 * Description: Function recovers critical data from the Migration Copy Area
11105 * while assembling an array.
11106 * Parameters:
11107 * super : imsm internal array info
11108 * info : general array info
11109 * Returns:
11110 * 0 : success (or there is no data to recover)
11111 * 1 : fail
11112 ******************************************************************************/
11113 int recover_backup_imsm(struct supertype *st, struct mdinfo *info)
11114 {
11115 struct intel_super *super = st->sb;
11116 struct migr_record *migr_rec = super->migr_rec;
11117 struct imsm_map *map_dest;
11118 struct intel_dev *id = NULL;
11119 unsigned long long read_offset;
11120 unsigned long long write_offset;
11121 unsigned unit_len;
11122 int new_disks, err;
11123 char *buf = NULL;
11124 int retval = 1;
11125 unsigned int sector_size = super->sector_size;
11126 unsigned long long curr_migr_unit = current_migr_unit(migr_rec);
11127 unsigned long long num_migr_units = get_num_migr_units(migr_rec);
11128 char buffer[20];
11129 int skipped_disks = 0;
11130 struct dl *dl_disk;
11131
11132 err = sysfs_get_str(info, NULL, "array_state", (char *)buffer, 20);
11133 if (err < 1)
11134 return 1;
11135
11136 /* recover data only during assemblation */
11137 if (strncmp(buffer, "inactive", 8) != 0)
11138 return 0;
11139 /* no data to recover */
11140 if (__le32_to_cpu(migr_rec->rec_status) == UNIT_SRC_NORMAL)
11141 return 0;
11142 if (curr_migr_unit >= num_migr_units)
11143 return 1;
11144
11145 /* find device during reshape */
11146 for (id = super->devlist; id; id = id->next)
11147 if (is_gen_migration(id->dev))
11148 break;
11149 if (id == NULL)
11150 return 1;
11151
11152 map_dest = get_imsm_map(id->dev, MAP_0);
11153 new_disks = map_dest->num_members;
11154
11155 read_offset = migr_chkp_area_pba(migr_rec) * 512;
11156
11157 write_offset = (migr_dest_1st_member_lba(migr_rec) +
11158 pba_of_lba0(map_dest)) * 512;
11159
11160 unit_len = __le32_to_cpu(migr_rec->dest_depth_per_unit) * 512;
11161 if (posix_memalign((void **)&buf, sector_size, unit_len) != 0)
11162 goto abort;
11163
11164 for (dl_disk = super->disks; dl_disk; dl_disk = dl_disk->next) {
11165 if (dl_disk->index < 0)
11166 continue;
11167
11168 if (!is_fd_valid(dl_disk->fd)) {
11169 skipped_disks++;
11170 continue;
11171 }
11172 if (lseek64(dl_disk->fd, read_offset, SEEK_SET) < 0) {
11173 pr_err("Cannot seek to block: %s\n",
11174 strerror(errno));
11175 skipped_disks++;
11176 continue;
11177 }
11178 if (read(dl_disk->fd, buf, unit_len) != (ssize_t)unit_len) {
11179 pr_err("Cannot read copy area block: %s\n",
11180 strerror(errno));
11181 skipped_disks++;
11182 continue;
11183 }
11184 if (lseek64(dl_disk->fd, write_offset, SEEK_SET) < 0) {
11185 pr_err("Cannot seek to block: %s\n",
11186 strerror(errno));
11187 skipped_disks++;
11188 continue;
11189 }
11190 if (write(dl_disk->fd, buf, unit_len) != (ssize_t)unit_len) {
11191 pr_err("Cannot restore block: %s\n",
11192 strerror(errno));
11193 skipped_disks++;
11194 continue;
11195 }
11196 }
11197
11198 if (skipped_disks > imsm_get_allowed_degradation(info->new_level,
11199 new_disks,
11200 super,
11201 id->dev)) {
11202 pr_err("Cannot restore data from backup. Too many failed disks\n");
11203 goto abort;
11204 }
11205
11206 if (save_checkpoint_imsm(st, info, UNIT_SRC_NORMAL)) {
11207 /* ignore error == 2, this can mean end of reshape here
11208 */
11209 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL) during restart\n");
11210 } else
11211 retval = 0;
11212
11213 abort:
11214 free(buf);
11215 return retval;
11216 }
11217
11218 static char disk_by_path[] = "/dev/disk/by-path/";
11219
11220 static const char *imsm_get_disk_controller_domain(const char *path)
11221 {
11222 char disk_path[PATH_MAX];
11223 char *drv=NULL;
11224 struct stat st;
11225
11226 strcpy(disk_path, disk_by_path);
11227 strncat(disk_path, path, PATH_MAX - strlen(disk_path) - 1);
11228 if (stat(disk_path, &st) == 0) {
11229 struct sys_dev* hba;
11230 char *path;
11231
11232 path = devt_to_devpath(st.st_rdev, 1, NULL);
11233 if (path == NULL)
11234 return "unknown";
11235 hba = find_disk_attached_hba(-1, path);
11236 if (hba && hba->type == SYS_DEV_SAS)
11237 drv = "isci";
11238 else if (hba && hba->type == SYS_DEV_SATA)
11239 drv = "ahci";
11240 else if (hba && hba->type == SYS_DEV_VMD)
11241 drv = "vmd";
11242 else if (hba && hba->type == SYS_DEV_NVME)
11243 drv = "nvme";
11244 else
11245 drv = "unknown";
11246 dprintf("path: %s hba: %s attached: %s\n",
11247 path, (hba) ? hba->path : "NULL", drv);
11248 free(path);
11249 }
11250 return drv;
11251 }
11252
11253 static char *imsm_find_array_devnm_by_subdev(int subdev, char *container)
11254 {
11255 static char devnm[32];
11256 char subdev_name[20];
11257 struct mdstat_ent *mdstat;
11258
11259 sprintf(subdev_name, "%d", subdev);
11260 mdstat = mdstat_by_subdev(subdev_name, container);
11261 if (!mdstat)
11262 return NULL;
11263
11264 strcpy(devnm, mdstat->devnm);
11265 free_mdstat(mdstat);
11266 return devnm;
11267 }
11268
11269 static int imsm_reshape_is_allowed_on_container(struct supertype *st,
11270 struct geo_params *geo,
11271 int *old_raid_disks,
11272 int direction)
11273 {
11274 /* currently we only support increasing the number of devices
11275 * for a container. This increases the number of device for each
11276 * member array. They must all be RAID0 or RAID5.
11277 */
11278 int ret_val = 0;
11279 struct mdinfo *info, *member;
11280 int devices_that_can_grow = 0;
11281
11282 dprintf("imsm: imsm_reshape_is_allowed_on_container(ENTER): st->devnm = (%s)\n", st->devnm);
11283
11284 if (geo->size > 0 ||
11285 geo->level != UnSet ||
11286 geo->layout != UnSet ||
11287 geo->chunksize != 0 ||
11288 geo->raid_disks == UnSet) {
11289 dprintf("imsm: Container operation is allowed for raid disks number change only.\n");
11290 return ret_val;
11291 }
11292
11293 if (direction == ROLLBACK_METADATA_CHANGES) {
11294 dprintf("imsm: Metadata changes rollback is not supported for container operation.\n");
11295 return ret_val;
11296 }
11297
11298 info = container_content_imsm(st, NULL);
11299 for (member = info; member; member = member->next) {
11300 char *result;
11301
11302 dprintf("imsm: checking device_num: %i\n",
11303 member->container_member);
11304
11305 if (geo->raid_disks <= member->array.raid_disks) {
11306 /* we work on container for Online Capacity Expansion
11307 * only so raid_disks has to grow
11308 */
11309 dprintf("imsm: for container operation raid disks increase is required\n");
11310 break;
11311 }
11312
11313 if (info->array.level != 0 && info->array.level != 5) {
11314 /* we cannot use this container with other raid level
11315 */
11316 dprintf("imsm: for container operation wrong raid level (%i) detected\n",
11317 info->array.level);
11318 break;
11319 } else {
11320 /* check for platform support
11321 * for this raid level configuration
11322 */
11323 struct intel_super *super = st->sb;
11324 if (!is_raid_level_supported(super->orom,
11325 member->array.level,
11326 geo->raid_disks)) {
11327 dprintf("platform does not support raid%d with %d disk%s\n",
11328 info->array.level,
11329 geo->raid_disks,
11330 geo->raid_disks > 1 ? "s" : "");
11331 break;
11332 }
11333 /* check if component size is aligned to chunk size
11334 */
11335 if (info->component_size %
11336 (info->array.chunk_size/512)) {
11337 dprintf("Component size is not aligned to chunk size\n");
11338 break;
11339 }
11340 }
11341
11342 if (*old_raid_disks &&
11343 info->array.raid_disks != *old_raid_disks)
11344 break;
11345 *old_raid_disks = info->array.raid_disks;
11346
11347 /* All raid5 and raid0 volumes in container
11348 * have to be ready for Online Capacity Expansion
11349 * so they need to be assembled. We have already
11350 * checked that no recovery etc is happening.
11351 */
11352 result = imsm_find_array_devnm_by_subdev(member->container_member,
11353 st->container_devnm);
11354 if (result == NULL) {
11355 dprintf("imsm: cannot find array\n");
11356 break;
11357 }
11358 devices_that_can_grow++;
11359 }
11360 sysfs_free(info);
11361 if (!member && devices_that_can_grow)
11362 ret_val = 1;
11363
11364 if (ret_val)
11365 dprintf("Container operation allowed\n");
11366 else
11367 dprintf("Error: %i\n", ret_val);
11368
11369 return ret_val;
11370 }
11371
11372 /* Function: get_spares_for_grow
11373 * Description: Allocates memory and creates list of spare devices
11374 * avaliable in container. Checks if spare drive size is acceptable.
11375 * Parameters: Pointer to the supertype structure
11376 * Returns: Pointer to the list of spare devices (mdinfo structure) on success,
11377 * NULL if fail
11378 */
11379 static struct mdinfo *get_spares_for_grow(struct supertype *st)
11380 {
11381 struct spare_criteria sc;
11382
11383 get_spare_criteria_imsm(st, &sc);
11384 return container_choose_spares(st, &sc, NULL, NULL, NULL, 0);
11385 }
11386
11387 /******************************************************************************
11388 * function: imsm_create_metadata_update_for_reshape
11389 * Function creates update for whole IMSM container.
11390 *
11391 ******************************************************************************/
11392 static int imsm_create_metadata_update_for_reshape(
11393 struct supertype *st,
11394 struct geo_params *geo,
11395 int old_raid_disks,
11396 struct imsm_update_reshape **updatep)
11397 {
11398 struct intel_super *super = st->sb;
11399 struct imsm_super *mpb = super->anchor;
11400 int update_memory_size;
11401 struct imsm_update_reshape *u;
11402 struct mdinfo *spares;
11403 int i;
11404 int delta_disks;
11405 struct mdinfo *dev;
11406
11407 dprintf("(enter) raid_disks = %i\n", geo->raid_disks);
11408
11409 delta_disks = geo->raid_disks - old_raid_disks;
11410
11411 /* size of all update data without anchor */
11412 update_memory_size = sizeof(struct imsm_update_reshape);
11413
11414 /* now add space for spare disks that we need to add. */
11415 update_memory_size += sizeof(u->new_disks[0]) * (delta_disks - 1);
11416
11417 u = xcalloc(1, update_memory_size);
11418 u->type = update_reshape_container_disks;
11419 u->old_raid_disks = old_raid_disks;
11420 u->new_raid_disks = geo->raid_disks;
11421
11422 /* now get spare disks list
11423 */
11424 spares = get_spares_for_grow(st);
11425
11426 if (spares == NULL || delta_disks > spares->array.spare_disks) {
11427 pr_err("imsm: ERROR: Cannot get spare devices for %s.\n", geo->dev_name);
11428 i = -1;
11429 goto abort;
11430 }
11431
11432 /* we have got spares
11433 * update disk list in imsm_disk list table in anchor
11434 */
11435 dprintf("imsm: %i spares are available.\n\n",
11436 spares->array.spare_disks);
11437
11438 dev = spares->devs;
11439 for (i = 0; i < delta_disks; i++) {
11440 struct dl *dl;
11441
11442 if (dev == NULL)
11443 break;
11444 u->new_disks[i] = makedev(dev->disk.major,
11445 dev->disk.minor);
11446 dl = get_disk_super(super, dev->disk.major, dev->disk.minor);
11447 dl->index = mpb->num_disks;
11448 mpb->num_disks++;
11449 dev = dev->next;
11450 }
11451
11452 abort:
11453 /* free spares
11454 */
11455 sysfs_free(spares);
11456
11457 dprintf("imsm: reshape update preparation :");
11458 if (i == delta_disks) {
11459 dprintf_cont(" OK\n");
11460 *updatep = u;
11461 return update_memory_size;
11462 }
11463 free(u);
11464 dprintf_cont(" Error\n");
11465
11466 return 0;
11467 }
11468
11469 /******************************************************************************
11470 * function: imsm_create_metadata_update_for_size_change()
11471 * Creates update for IMSM array for array size change.
11472 *
11473 ******************************************************************************/
11474 static int imsm_create_metadata_update_for_size_change(
11475 struct supertype *st,
11476 struct geo_params *geo,
11477 struct imsm_update_size_change **updatep)
11478 {
11479 struct intel_super *super = st->sb;
11480 int update_memory_size;
11481 struct imsm_update_size_change *u;
11482
11483 dprintf("(enter) New size = %llu\n", geo->size);
11484
11485 /* size of all update data without anchor */
11486 update_memory_size = sizeof(struct imsm_update_size_change);
11487
11488 u = xcalloc(1, update_memory_size);
11489 u->type = update_size_change;
11490 u->subdev = super->current_vol;
11491 u->new_size = geo->size;
11492
11493 dprintf("imsm: reshape update preparation : OK\n");
11494 *updatep = u;
11495
11496 return update_memory_size;
11497 }
11498
11499 /******************************************************************************
11500 * function: imsm_create_metadata_update_for_migration()
11501 * Creates update for IMSM array.
11502 *
11503 ******************************************************************************/
11504 static int imsm_create_metadata_update_for_migration(
11505 struct supertype *st,
11506 struct geo_params *geo,
11507 struct imsm_update_reshape_migration **updatep)
11508 {
11509 struct intel_super *super = st->sb;
11510 int update_memory_size;
11511 int current_chunk_size;
11512 struct imsm_update_reshape_migration *u;
11513 struct imsm_dev *dev = get_imsm_dev(super, super->current_vol);
11514 struct imsm_map *map = get_imsm_map(dev, MAP_0);
11515 int previous_level = -1;
11516
11517 dprintf("(enter) New Level = %i\n", geo->level);
11518
11519 /* size of all update data without anchor */
11520 update_memory_size = sizeof(struct imsm_update_reshape_migration);
11521
11522 u = xcalloc(1, update_memory_size);
11523 u->type = update_reshape_migration;
11524 u->subdev = super->current_vol;
11525 u->new_level = geo->level;
11526 u->new_layout = geo->layout;
11527 u->new_raid_disks = u->old_raid_disks = geo->raid_disks;
11528 u->new_disks[0] = -1;
11529 u->new_chunksize = -1;
11530
11531 current_chunk_size = __le16_to_cpu(map->blocks_per_strip) / 2;
11532
11533 if (geo->chunksize != current_chunk_size) {
11534 u->new_chunksize = geo->chunksize / 1024;
11535 dprintf("imsm: chunk size change from %i to %i\n",
11536 current_chunk_size, u->new_chunksize);
11537 }
11538 previous_level = map->raid_level;
11539
11540 if (geo->level == 5 && previous_level == 0) {
11541 struct mdinfo *spares = NULL;
11542
11543 u->new_raid_disks++;
11544 spares = get_spares_for_grow(st);
11545 if (spares == NULL || spares->array.spare_disks < 1) {
11546 free(u);
11547 sysfs_free(spares);
11548 update_memory_size = 0;
11549 pr_err("cannot get spare device for requested migration\n");
11550 return 0;
11551 }
11552 sysfs_free(spares);
11553 }
11554 dprintf("imsm: reshape update preparation : OK\n");
11555 *updatep = u;
11556
11557 return update_memory_size;
11558 }
11559
11560 static void imsm_update_metadata_locally(struct supertype *st,
11561 void *buf, int len)
11562 {
11563 struct metadata_update mu;
11564
11565 mu.buf = buf;
11566 mu.len = len;
11567 mu.space = NULL;
11568 mu.space_list = NULL;
11569 mu.next = NULL;
11570 if (imsm_prepare_update(st, &mu))
11571 imsm_process_update(st, &mu);
11572
11573 while (mu.space_list) {
11574 void **space = mu.space_list;
11575 mu.space_list = *space;
11576 free(space);
11577 }
11578 }
11579
11580 /***************************************************************************
11581 * Function: imsm_analyze_change
11582 * Description: Function analyze change for single volume
11583 * and validate if transition is supported
11584 * Parameters: Geometry parameters, supertype structure,
11585 * metadata change direction (apply/rollback)
11586 * Returns: Operation type code on success, -1 if fail
11587 ****************************************************************************/
11588 enum imsm_reshape_type imsm_analyze_change(struct supertype *st,
11589 struct geo_params *geo,
11590 int direction)
11591 {
11592 struct mdinfo info;
11593 int change = -1;
11594 int check_devs = 0;
11595 int chunk;
11596 /* number of added/removed disks in operation result */
11597 int devNumChange = 0;
11598 /* imsm compatible layout value for array geometry verification */
11599 int imsm_layout = -1;
11600 int data_disks;
11601 struct imsm_dev *dev;
11602 struct imsm_map *map;
11603 struct intel_super *super;
11604 unsigned long long current_size;
11605 unsigned long long free_size;
11606 unsigned long long max_size;
11607 imsm_status_t rv;
11608
11609 getinfo_super_imsm_volume(st, &info, NULL);
11610 if (geo->level != info.array.level && geo->level >= 0 &&
11611 geo->level != UnSet) {
11612 switch (info.array.level) {
11613 case 0:
11614 if (geo->level == 5) {
11615 change = CH_MIGRATION;
11616 if (geo->layout != ALGORITHM_LEFT_ASYMMETRIC) {
11617 pr_err("Error. Requested Layout not supported (left-asymmetric layout is supported only)!\n");
11618 change = -1;
11619 goto analyse_change_exit;
11620 }
11621 imsm_layout = geo->layout;
11622 check_devs = 1;
11623 devNumChange = 1; /* parity disk added */
11624 } else if (geo->level == 10) {
11625 change = CH_TAKEOVER;
11626 check_devs = 1;
11627 devNumChange = 2; /* two mirrors added */
11628 imsm_layout = 0x102; /* imsm supported layout */
11629 }
11630 break;
11631 case 1:
11632 case 10:
11633 if (geo->level == 0) {
11634 change = CH_TAKEOVER;
11635 check_devs = 1;
11636 devNumChange = -(geo->raid_disks/2);
11637 imsm_layout = 0; /* imsm raid0 layout */
11638 }
11639 break;
11640 }
11641 if (change == -1) {
11642 pr_err("Error. Level Migration from %d to %d not supported!\n",
11643 info.array.level, geo->level);
11644 goto analyse_change_exit;
11645 }
11646 } else
11647 geo->level = info.array.level;
11648
11649 if (geo->layout != info.array.layout &&
11650 (geo->layout != UnSet && geo->layout != -1)) {
11651 change = CH_MIGRATION;
11652 if (info.array.layout == 0 && info.array.level == 5 &&
11653 geo->layout == 5) {
11654 /* reshape 5 -> 4 */
11655 } else if (info.array.layout == 5 && info.array.level == 5 &&
11656 geo->layout == 0) {
11657 /* reshape 4 -> 5 */
11658 geo->layout = 0;
11659 geo->level = 5;
11660 } else {
11661 pr_err("Error. Layout Migration from %d to %d not supported!\n",
11662 info.array.layout, geo->layout);
11663 change = -1;
11664 goto analyse_change_exit;
11665 }
11666 } else {
11667 geo->layout = info.array.layout;
11668 if (imsm_layout == -1)
11669 imsm_layout = info.array.layout;
11670 }
11671
11672 if (geo->chunksize > 0 && geo->chunksize != UnSet &&
11673 geo->chunksize != info.array.chunk_size) {
11674 if (info.array.level == 10) {
11675 pr_err("Error. Chunk size change for RAID 10 is not supported.\n");
11676 change = -1;
11677 goto analyse_change_exit;
11678 } else if (info.component_size % (geo->chunksize/512)) {
11679 pr_err("New chunk size (%dK) does not evenly divide device size (%lluk). Aborting...\n",
11680 geo->chunksize/1024, info.component_size/2);
11681 change = -1;
11682 goto analyse_change_exit;
11683 }
11684 change = CH_MIGRATION;
11685 } else {
11686 geo->chunksize = info.array.chunk_size;
11687 }
11688
11689 chunk = geo->chunksize / 1024;
11690
11691 super = st->sb;
11692 dev = get_imsm_dev(super, super->current_vol);
11693 map = get_imsm_map(dev, MAP_0);
11694 data_disks = imsm_num_data_members(map);
11695 /* compute current size per disk member
11696 */
11697 current_size = info.custom_array_size / data_disks;
11698
11699 if (geo->size > 0 && geo->size != MAX_SIZE) {
11700 /* align component size
11701 */
11702 geo->size = imsm_component_size_alignment_check(
11703 get_imsm_raid_level(dev->vol.map),
11704 chunk * 1024, super->sector_size,
11705 geo->size * 2);
11706 if (geo->size == 0) {
11707 pr_err("Error. Size expansion is supported only (current size is %llu, requested size /rounded/ is 0).\n",
11708 current_size);
11709 goto analyse_change_exit;
11710 }
11711 }
11712
11713 if (current_size != geo->size && geo->size > 0) {
11714 if (change != -1) {
11715 pr_err("Error. Size change should be the only one at a time.\n");
11716 change = -1;
11717 goto analyse_change_exit;
11718 }
11719 if ((super->current_vol + 1) != super->anchor->num_raid_devs) {
11720 pr_err("Error. The last volume in container can be expanded only (%i/%s).\n",
11721 super->current_vol, st->devnm);
11722 goto analyse_change_exit;
11723 }
11724 /* check the maximum available size
11725 */
11726 rv = imsm_get_free_size(super, dev->vol.map->num_members,
11727 0, chunk, &free_size);
11728
11729 if (rv != IMSM_STATUS_OK)
11730 /* Cannot find maximum available space
11731 */
11732 max_size = 0;
11733 else {
11734 max_size = free_size + current_size;
11735 /* align component size
11736 */
11737 max_size = imsm_component_size_alignment_check(
11738 get_imsm_raid_level(dev->vol.map),
11739 chunk * 1024, super->sector_size,
11740 max_size);
11741 }
11742 if (geo->size == MAX_SIZE) {
11743 /* requested size change to the maximum available size
11744 */
11745 if (max_size == 0) {
11746 pr_err("Error. Cannot find maximum available space.\n");
11747 change = -1;
11748 goto analyse_change_exit;
11749 } else
11750 geo->size = max_size;
11751 }
11752
11753 if (direction == ROLLBACK_METADATA_CHANGES) {
11754 /* accept size for rollback only
11755 */
11756 } else {
11757 /* round size due to metadata compatibility
11758 */
11759 geo->size = (geo->size >> SECT_PER_MB_SHIFT)
11760 << SECT_PER_MB_SHIFT;
11761 dprintf("Prepare update for size change to %llu\n",
11762 geo->size );
11763 if (current_size >= geo->size) {
11764 pr_err("Error. Size expansion is supported only (current size is %llu, requested size /rounded/ is %llu).\n",
11765 current_size, geo->size);
11766 goto analyse_change_exit;
11767 }
11768 if (max_size && geo->size > max_size) {
11769 pr_err("Error. Requested size is larger than maximum available size (maximum available size is %llu, requested size /rounded/ is %llu).\n",
11770 max_size, geo->size);
11771 goto analyse_change_exit;
11772 }
11773 }
11774 geo->size *= data_disks;
11775 geo->raid_disks = dev->vol.map->num_members;
11776 change = CH_ARRAY_SIZE;
11777 }
11778 if (!validate_geometry_imsm(st,
11779 geo->level,
11780 imsm_layout,
11781 geo->raid_disks + devNumChange,
11782 &chunk,
11783 geo->size, INVALID_SECTORS,
11784 0, 0, info.consistency_policy, 1))
11785 change = -1;
11786
11787 if (check_devs) {
11788 struct intel_super *super = st->sb;
11789 struct imsm_super *mpb = super->anchor;
11790
11791 if (mpb->num_raid_devs > 1) {
11792 pr_err("Error. Cannot perform operation on %s- for this operation "
11793 "it MUST be single array in container\n", geo->dev_name);
11794 change = -1;
11795 }
11796 }
11797
11798 analyse_change_exit:
11799 if (direction == ROLLBACK_METADATA_CHANGES &&
11800 (change == CH_MIGRATION || change == CH_TAKEOVER)) {
11801 dprintf("imsm: Metadata changes rollback is not supported for migration and takeover operations.\n");
11802 change = -1;
11803 }
11804 return change;
11805 }
11806
11807 int imsm_takeover(struct supertype *st, struct geo_params *geo)
11808 {
11809 struct intel_super *super = st->sb;
11810 struct imsm_update_takeover *u;
11811
11812 u = xmalloc(sizeof(struct imsm_update_takeover));
11813
11814 u->type = update_takeover;
11815 u->subarray = super->current_vol;
11816
11817 /* 10->0 transition */
11818 if (geo->level == 0)
11819 u->direction = R10_TO_R0;
11820
11821 /* 0->10 transition */
11822 if (geo->level == 10)
11823 u->direction = R0_TO_R10;
11824
11825 /* update metadata locally */
11826 imsm_update_metadata_locally(st, u,
11827 sizeof(struct imsm_update_takeover));
11828 /* and possibly remotely */
11829 if (st->update_tail)
11830 append_metadata_update(st, u,
11831 sizeof(struct imsm_update_takeover));
11832 else
11833 free(u);
11834
11835 return 0;
11836 }
11837
11838 /* Flush size update if size calculated by num_data_stripes is higher than
11839 * imsm_dev_size to eliminate differences during reshape.
11840 * Mdmon will recalculate them correctly.
11841 * If subarray index is not set then check whole container.
11842 * Returns:
11843 * 0 - no error occurred
11844 * 1 - error detected
11845 */
11846 static int imsm_fix_size_mismatch(struct supertype *st, int subarray_index)
11847 {
11848 struct intel_super *super = st->sb;
11849 int tmp = super->current_vol;
11850 int ret_val = 1;
11851 int i;
11852
11853 for (i = 0; i < super->anchor->num_raid_devs; i++) {
11854 if (subarray_index >= 0 && i != subarray_index)
11855 continue;
11856 super->current_vol = i;
11857 struct imsm_dev *dev = get_imsm_dev(super, super->current_vol);
11858 struct imsm_map *map = get_imsm_map(dev, MAP_0);
11859 unsigned int disc_count = imsm_num_data_members(map);
11860 struct geo_params geo;
11861 struct imsm_update_size_change *update;
11862 unsigned long long calc_size = per_dev_array_size(map) * disc_count;
11863 unsigned long long d_size = imsm_dev_size(dev);
11864 int u_size;
11865
11866 if (calc_size == d_size)
11867 continue;
11868
11869 /* There is a difference, confirm that imsm_dev_size is
11870 * smaller and push update.
11871 */
11872 if (d_size > calc_size) {
11873 pr_err("imsm: dev size of subarray %d is incorrect\n",
11874 i);
11875 goto exit;
11876 }
11877 memset(&geo, 0, sizeof(struct geo_params));
11878 geo.size = d_size;
11879 u_size = imsm_create_metadata_update_for_size_change(st, &geo,
11880 &update);
11881 imsm_update_metadata_locally(st, update, u_size);
11882 if (st->update_tail) {
11883 append_metadata_update(st, update, u_size);
11884 flush_metadata_updates(st);
11885 st->update_tail = &st->updates;
11886 } else {
11887 imsm_sync_metadata(st);
11888 free(update);
11889 }
11890 }
11891 ret_val = 0;
11892 exit:
11893 super->current_vol = tmp;
11894 return ret_val;
11895 }
11896
11897 static int imsm_reshape_super(struct supertype *st, unsigned long long size,
11898 int level,
11899 int layout, int chunksize, int raid_disks,
11900 int delta_disks, char *backup, char *dev,
11901 int direction, int verbose)
11902 {
11903 int ret_val = 1;
11904 struct geo_params geo;
11905
11906 dprintf("(enter)\n");
11907
11908 memset(&geo, 0, sizeof(struct geo_params));
11909
11910 geo.dev_name = dev;
11911 strcpy(geo.devnm, st->devnm);
11912 geo.size = size;
11913 geo.level = level;
11914 geo.layout = layout;
11915 geo.chunksize = chunksize;
11916 geo.raid_disks = raid_disks;
11917 if (delta_disks != UnSet)
11918 geo.raid_disks += delta_disks;
11919
11920 dprintf("for level : %i\n", geo.level);
11921 dprintf("for raid_disks : %i\n", geo.raid_disks);
11922
11923 if (strcmp(st->container_devnm, st->devnm) == 0) {
11924 /* On container level we can only increase number of devices. */
11925 dprintf("imsm: info: Container operation\n");
11926 int old_raid_disks = 0;
11927
11928 if (imsm_reshape_is_allowed_on_container(
11929 st, &geo, &old_raid_disks, direction)) {
11930 struct imsm_update_reshape *u = NULL;
11931 int len;
11932
11933 if (imsm_fix_size_mismatch(st, -1)) {
11934 dprintf("imsm: Cannot fix size mismatch\n");
11935 goto exit_imsm_reshape_super;
11936 }
11937
11938 len = imsm_create_metadata_update_for_reshape(
11939 st, &geo, old_raid_disks, &u);
11940
11941 if (len <= 0) {
11942 dprintf("imsm: Cannot prepare update\n");
11943 goto exit_imsm_reshape_super;
11944 }
11945
11946 ret_val = 0;
11947 /* update metadata locally */
11948 imsm_update_metadata_locally(st, u, len);
11949 /* and possibly remotely */
11950 if (st->update_tail)
11951 append_metadata_update(st, u, len);
11952 else
11953 free(u);
11954
11955 } else {
11956 pr_err("(imsm) Operation is not allowed on this container\n");
11957 }
11958 } else {
11959 /* On volume level we support following operations
11960 * - takeover: raid10 -> raid0; raid0 -> raid10
11961 * - chunk size migration
11962 * - migration: raid5 -> raid0; raid0 -> raid5
11963 */
11964 struct intel_super *super = st->sb;
11965 struct intel_dev *dev = super->devlist;
11966 int change;
11967 dprintf("imsm: info: Volume operation\n");
11968 /* find requested device */
11969 while (dev) {
11970 char *devnm =
11971 imsm_find_array_devnm_by_subdev(
11972 dev->index, st->container_devnm);
11973 if (devnm && strcmp(devnm, geo.devnm) == 0)
11974 break;
11975 dev = dev->next;
11976 }
11977 if (dev == NULL) {
11978 pr_err("Cannot find %s (%s) subarray\n",
11979 geo.dev_name, geo.devnm);
11980 goto exit_imsm_reshape_super;
11981 }
11982 super->current_vol = dev->index;
11983 change = imsm_analyze_change(st, &geo, direction);
11984 switch (change) {
11985 case CH_TAKEOVER:
11986 ret_val = imsm_takeover(st, &geo);
11987 break;
11988 case CH_MIGRATION: {
11989 struct imsm_update_reshape_migration *u = NULL;
11990 int len =
11991 imsm_create_metadata_update_for_migration(
11992 st, &geo, &u);
11993 if (len < 1) {
11994 dprintf("imsm: Cannot prepare update\n");
11995 break;
11996 }
11997 ret_val = 0;
11998 /* update metadata locally */
11999 imsm_update_metadata_locally(st, u, len);
12000 /* and possibly remotely */
12001 if (st->update_tail)
12002 append_metadata_update(st, u, len);
12003 else
12004 free(u);
12005 }
12006 break;
12007 case CH_ARRAY_SIZE: {
12008 struct imsm_update_size_change *u = NULL;
12009 int len =
12010 imsm_create_metadata_update_for_size_change(
12011 st, &geo, &u);
12012 if (len < 1) {
12013 dprintf("imsm: Cannot prepare update\n");
12014 break;
12015 }
12016 ret_val = 0;
12017 /* update metadata locally */
12018 imsm_update_metadata_locally(st, u, len);
12019 /* and possibly remotely */
12020 if (st->update_tail)
12021 append_metadata_update(st, u, len);
12022 else
12023 free(u);
12024 }
12025 break;
12026 default:
12027 ret_val = 1;
12028 }
12029 }
12030
12031 exit_imsm_reshape_super:
12032 dprintf("imsm: reshape_super Exit code = %i\n", ret_val);
12033 return ret_val;
12034 }
12035
12036 #define COMPLETED_OK 0
12037 #define COMPLETED_NONE 1
12038 #define COMPLETED_DELAYED 2
12039
12040 static int read_completed(int fd, unsigned long long *val)
12041 {
12042 int ret;
12043 char buf[50];
12044
12045 ret = sysfs_fd_get_str(fd, buf, 50);
12046 if (ret < 0)
12047 return ret;
12048
12049 ret = COMPLETED_OK;
12050 if (strncmp(buf, "none", 4) == 0) {
12051 ret = COMPLETED_NONE;
12052 } else if (strncmp(buf, "delayed", 7) == 0) {
12053 ret = COMPLETED_DELAYED;
12054 } else {
12055 char *ep;
12056 *val = strtoull(buf, &ep, 0);
12057 if (ep == buf || (*ep != 0 && *ep != '\n' && *ep != ' '))
12058 ret = -1;
12059 }
12060 return ret;
12061 }
12062
12063 /*******************************************************************************
12064 * Function: wait_for_reshape_imsm
12065 * Description: Function writes new sync_max value and waits until
12066 * reshape process reach new position
12067 * Parameters:
12068 * sra : general array info
12069 * ndata : number of disks in new array's layout
12070 * Returns:
12071 * 0 : success,
12072 * 1 : there is no reshape in progress,
12073 * -1 : fail
12074 ******************************************************************************/
12075 int wait_for_reshape_imsm(struct mdinfo *sra, int ndata)
12076 {
12077 int fd = sysfs_get_fd(sra, NULL, "sync_completed");
12078 int retry = 3;
12079 unsigned long long completed;
12080 /* to_complete : new sync_max position */
12081 unsigned long long to_complete = sra->reshape_progress;
12082 unsigned long long position_to_set = to_complete / ndata;
12083
12084 if (!is_fd_valid(fd)) {
12085 dprintf("cannot open reshape_position\n");
12086 return 1;
12087 }
12088
12089 do {
12090 if (sysfs_fd_get_ll(fd, &completed) < 0) {
12091 if (!retry) {
12092 dprintf("cannot read reshape_position (no reshape in progres)\n");
12093 close(fd);
12094 return 1;
12095 }
12096 sleep_for(0, MSEC_TO_NSEC(30), true);
12097 } else
12098 break;
12099 } while (retry--);
12100
12101 if (completed > position_to_set) {
12102 dprintf("wrong next position to set %llu (%llu)\n",
12103 to_complete, position_to_set);
12104 close(fd);
12105 return -1;
12106 }
12107 dprintf("Position set: %llu\n", position_to_set);
12108 if (sysfs_set_num(sra, NULL, "sync_max",
12109 position_to_set) != 0) {
12110 dprintf("cannot set reshape position to %llu\n",
12111 position_to_set);
12112 close(fd);
12113 return -1;
12114 }
12115
12116 do {
12117 int rc;
12118 char action[20];
12119 int timeout = 3000;
12120
12121 sysfs_wait(fd, &timeout);
12122 if (sysfs_get_str(sra, NULL, "sync_action",
12123 action, 20) > 0 &&
12124 strncmp(action, "reshape", 7) != 0) {
12125 if (strncmp(action, "idle", 4) == 0)
12126 break;
12127 close(fd);
12128 return -1;
12129 }
12130
12131 rc = read_completed(fd, &completed);
12132 if (rc < 0) {
12133 dprintf("cannot read reshape_position (in loop)\n");
12134 close(fd);
12135 return 1;
12136 } else if (rc == COMPLETED_NONE)
12137 break;
12138 } while (completed < position_to_set);
12139
12140 close(fd);
12141 return 0;
12142 }
12143
12144 /*******************************************************************************
12145 * Function: check_degradation_change
12146 * Description: Check that array hasn't become failed.
12147 * Parameters:
12148 * info : for sysfs access
12149 * sources : source disks descriptors
12150 * degraded: previous degradation level
12151 * Returns:
12152 * degradation level
12153 ******************************************************************************/
12154 int check_degradation_change(struct mdinfo *info,
12155 int *sources,
12156 int degraded)
12157 {
12158 unsigned long long new_degraded;
12159 int rv;
12160
12161 rv = sysfs_get_ll(info, NULL, "degraded", &new_degraded);
12162 if (rv == -1 || (new_degraded != (unsigned long long)degraded)) {
12163 /* check each device to ensure it is still working */
12164 struct mdinfo *sd;
12165 new_degraded = 0;
12166 for (sd = info->devs ; sd ; sd = sd->next) {
12167 if (sd->disk.state & (1<<MD_DISK_FAULTY))
12168 continue;
12169 if (sd->disk.state & (1<<MD_DISK_SYNC)) {
12170 char sbuf[100];
12171 int raid_disk = sd->disk.raid_disk;
12172
12173 if (sysfs_get_str(info,
12174 sd, "state", sbuf, sizeof(sbuf)) < 0 ||
12175 strstr(sbuf, "faulty") ||
12176 strstr(sbuf, "in_sync") == NULL) {
12177 /* this device is dead */
12178 sd->disk.state = (1<<MD_DISK_FAULTY);
12179 if (raid_disk >= 0)
12180 close_fd(&sources[raid_disk]);
12181 new_degraded++;
12182 }
12183 }
12184 }
12185 }
12186
12187 return new_degraded;
12188 }
12189
12190 /*******************************************************************************
12191 * Function: imsm_manage_reshape
12192 * Description: Function finds array under reshape and it manages reshape
12193 * process. It creates stripes backups (if required) and sets
12194 * checkpoints.
12195 * Parameters:
12196 * afd : Backup handle (nattive) - not used
12197 * sra : general array info
12198 * reshape : reshape parameters - not used
12199 * st : supertype structure
12200 * blocks : size of critical section [blocks]
12201 * fds : table of source device descriptor
12202 * offsets : start of array (offest per devices)
12203 * dests : not used
12204 * destfd : table of destination device descriptor
12205 * destoffsets : table of destination offsets (per device)
12206 * Returns:
12207 * 1 : success, reshape is done
12208 * 0 : fail
12209 ******************************************************************************/
12210 static int imsm_manage_reshape(
12211 int afd, struct mdinfo *sra, struct reshape *reshape,
12212 struct supertype *st, unsigned long backup_blocks,
12213 int *fds, unsigned long long *offsets,
12214 int dests, int *destfd, unsigned long long *destoffsets)
12215 {
12216 int ret_val = 0;
12217 struct intel_super *super = st->sb;
12218 struct intel_dev *dv;
12219 unsigned int sector_size = super->sector_size;
12220 struct imsm_dev *dev = NULL;
12221 struct imsm_map *map_src, *map_dest;
12222 int migr_vol_qan = 0;
12223 int ndata, odata; /* [bytes] */
12224 int chunk; /* [bytes] */
12225 struct migr_record *migr_rec;
12226 char *buf = NULL;
12227 unsigned int buf_size; /* [bytes] */
12228 unsigned long long max_position; /* array size [bytes] */
12229 unsigned long long next_step; /* [blocks]/[bytes] */
12230 unsigned long long old_data_stripe_length;
12231 unsigned long long start_src; /* [bytes] */
12232 unsigned long long start; /* [bytes] */
12233 unsigned long long start_buf_shift; /* [bytes] */
12234 int degraded = 0;
12235 int source_layout = 0;
12236 int subarray_index = -1;
12237
12238 if (!sra)
12239 return ret_val;
12240
12241 if (!fds || !offsets)
12242 goto abort;
12243
12244 /* Find volume during the reshape */
12245 for (dv = super->devlist; dv; dv = dv->next) {
12246 if (dv->dev->vol.migr_type == MIGR_GEN_MIGR &&
12247 dv->dev->vol.migr_state == 1) {
12248 dev = dv->dev;
12249 migr_vol_qan++;
12250 subarray_index = dv->index;
12251 }
12252 }
12253 /* Only one volume can migrate at the same time */
12254 if (migr_vol_qan != 1) {
12255 pr_err("%s", migr_vol_qan ?
12256 "Number of migrating volumes greater than 1\n" :
12257 "There is no volume during migrationg\n");
12258 goto abort;
12259 }
12260
12261 map_dest = get_imsm_map(dev, MAP_0);
12262 map_src = get_imsm_map(dev, MAP_1);
12263 if (map_src == NULL)
12264 goto abort;
12265
12266 ndata = imsm_num_data_members(map_dest);
12267 odata = imsm_num_data_members(map_src);
12268
12269 chunk = __le16_to_cpu(map_src->blocks_per_strip) * 512;
12270 old_data_stripe_length = odata * chunk;
12271
12272 migr_rec = super->migr_rec;
12273
12274 /* initialize migration record for start condition */
12275 if (sra->reshape_progress == 0)
12276 init_migr_record_imsm(st, dev, sra);
12277 else {
12278 if (__le32_to_cpu(migr_rec->rec_status) != UNIT_SRC_NORMAL) {
12279 dprintf("imsm: cannot restart migration when data are present in copy area.\n");
12280 goto abort;
12281 }
12282 /* Save checkpoint to update migration record for current
12283 * reshape position (in md). It can be farther than current
12284 * reshape position in metadata.
12285 */
12286 if (save_checkpoint_imsm(st, sra, UNIT_SRC_NORMAL) == 1) {
12287 /* ignore error == 2, this can mean end of reshape here
12288 */
12289 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL, initial save)\n");
12290 goto abort;
12291 }
12292 }
12293
12294 /* size for data */
12295 buf_size = __le32_to_cpu(migr_rec->blocks_per_unit) * 512;
12296 /* extend buffer size for parity disk */
12297 buf_size += __le32_to_cpu(migr_rec->dest_depth_per_unit) * 512;
12298 /* add space for stripe alignment */
12299 buf_size += old_data_stripe_length;
12300 if (posix_memalign((void **)&buf, MAX_SECTOR_SIZE, buf_size)) {
12301 dprintf("imsm: Cannot allocate checkpoint buffer\n");
12302 goto abort;
12303 }
12304
12305 max_position = sra->component_size * ndata;
12306 source_layout = imsm_level_to_layout(map_src->raid_level);
12307
12308 while (current_migr_unit(migr_rec) <
12309 get_num_migr_units(migr_rec)) {
12310 /* current reshape position [blocks] */
12311 unsigned long long current_position =
12312 __le32_to_cpu(migr_rec->blocks_per_unit)
12313 * current_migr_unit(migr_rec);
12314 unsigned long long border;
12315
12316 /* Check that array hasn't become failed.
12317 */
12318 degraded = check_degradation_change(sra, fds, degraded);
12319 if (degraded > 1) {
12320 dprintf("imsm: Abort reshape due to degradation level (%i)\n", degraded);
12321 goto abort;
12322 }
12323
12324 next_step = __le32_to_cpu(migr_rec->blocks_per_unit);
12325
12326 if ((current_position + next_step) > max_position)
12327 next_step = max_position - current_position;
12328
12329 start = current_position * 512;
12330
12331 /* align reading start to old geometry */
12332 start_buf_shift = start % old_data_stripe_length;
12333 start_src = start - start_buf_shift;
12334
12335 border = (start_src / odata) - (start / ndata);
12336 border /= 512;
12337 if (border <= __le32_to_cpu(migr_rec->dest_depth_per_unit)) {
12338 /* save critical stripes to buf
12339 * start - start address of current unit
12340 * to backup [bytes]
12341 * start_src - start address of current unit
12342 * to backup alligned to source array
12343 * [bytes]
12344 */
12345 unsigned long long next_step_filler;
12346 unsigned long long copy_length = next_step * 512;
12347
12348 /* allign copy area length to stripe in old geometry */
12349 next_step_filler = ((copy_length + start_buf_shift)
12350 % old_data_stripe_length);
12351 if (next_step_filler)
12352 next_step_filler = (old_data_stripe_length
12353 - next_step_filler);
12354 dprintf("save_stripes() parameters: start = %llu,\tstart_src = %llu,\tnext_step*512 = %llu,\tstart_in_buf_shift = %llu,\tnext_step_filler = %llu\n",
12355 start, start_src, copy_length,
12356 start_buf_shift, next_step_filler);
12357
12358 if (save_stripes(fds, offsets, map_src->num_members,
12359 chunk, map_src->raid_level,
12360 source_layout, 0, NULL, start_src,
12361 copy_length +
12362 next_step_filler + start_buf_shift,
12363 buf)) {
12364 dprintf("imsm: Cannot save stripes to buffer\n");
12365 goto abort;
12366 }
12367 /* Convert data to destination format and store it
12368 * in backup general migration area
12369 */
12370 if (save_backup_imsm(st, dev, sra,
12371 buf + start_buf_shift, copy_length)) {
12372 dprintf("imsm: Cannot save stripes to target devices\n");
12373 goto abort;
12374 }
12375 if (save_checkpoint_imsm(st, sra,
12376 UNIT_SRC_IN_CP_AREA)) {
12377 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_IN_CP_AREA)\n");
12378 goto abort;
12379 }
12380 } else {
12381 /* set next step to use whole border area */
12382 border /= next_step;
12383 if (border > 1)
12384 next_step *= border;
12385 }
12386 /* When data backed up, checkpoint stored,
12387 * kick the kernel to reshape unit of data
12388 */
12389 next_step = next_step + sra->reshape_progress;
12390 /* limit next step to array max position */
12391 if (next_step > max_position)
12392 next_step = max_position;
12393 sysfs_set_num(sra, NULL, "suspend_lo", sra->reshape_progress);
12394 sysfs_set_num(sra, NULL, "suspend_hi", next_step);
12395 sra->reshape_progress = next_step;
12396
12397 /* wait until reshape finish */
12398 if (wait_for_reshape_imsm(sra, ndata)) {
12399 dprintf("wait_for_reshape_imsm returned error!\n");
12400 goto abort;
12401 }
12402 if (sigterm)
12403 goto abort;
12404
12405 if (save_checkpoint_imsm(st, sra, UNIT_SRC_NORMAL) == 1) {
12406 /* ignore error == 2, this can mean end of reshape here
12407 */
12408 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL)\n");
12409 goto abort;
12410 }
12411
12412 }
12413
12414 /* clear migr_rec on disks after successful migration */
12415 struct dl *d;
12416
12417 memset(super->migr_rec_buf, 0, MIGR_REC_BUF_SECTORS*MAX_SECTOR_SIZE);
12418 for (d = super->disks; d; d = d->next) {
12419 if (d->index < 0 || is_failed(&d->disk))
12420 continue;
12421 unsigned long long dsize;
12422
12423 get_dev_size(d->fd, NULL, &dsize);
12424 if (lseek64(d->fd, dsize - MIGR_REC_SECTOR_POSITION*sector_size,
12425 SEEK_SET) >= 0) {
12426 if ((unsigned int)write(d->fd, super->migr_rec_buf,
12427 MIGR_REC_BUF_SECTORS*sector_size) !=
12428 MIGR_REC_BUF_SECTORS*sector_size)
12429 perror("Write migr_rec failed");
12430 }
12431 }
12432
12433 /* return '1' if done */
12434 ret_val = 1;
12435
12436 /* After the reshape eliminate size mismatch in metadata.
12437 * Don't update md/component_size here, volume hasn't
12438 * to take whole space. It is allowed by kernel.
12439 * md/component_size will be set propoperly after next assembly.
12440 */
12441 imsm_fix_size_mismatch(st, subarray_index);
12442
12443 abort:
12444 free(buf);
12445 /* See Grow.c: abort_reshape() for further explanation */
12446 sysfs_set_num(sra, NULL, "suspend_lo", 0x7FFFFFFFFFFFFFFFULL);
12447 sysfs_set_num(sra, NULL, "suspend_hi", 0);
12448 sysfs_set_num(sra, NULL, "suspend_lo", 0);
12449
12450 return ret_val;
12451 }
12452
12453 /*******************************************************************************
12454 * Function: calculate_bitmap_min_chunksize
12455 * Description: Calculates the minimal valid bitmap chunk size
12456 * Parameters:
12457 * max_bits : indicate how many bits can be used for the bitmap
12458 * data_area_size : the size of the data area covered by the bitmap
12459 *
12460 * Returns:
12461 * The bitmap chunk size
12462 ******************************************************************************/
12463 static unsigned long long
12464 calculate_bitmap_min_chunksize(unsigned long long max_bits,
12465 unsigned long long data_area_size)
12466 {
12467 unsigned long long min_chunk =
12468 4096; /* sub-page chunks don't work yet.. */
12469 unsigned long long bits = data_area_size / min_chunk + 1;
12470
12471 while (bits > max_bits) {
12472 min_chunk *= 2;
12473 bits = (bits + 1) / 2;
12474 }
12475 return min_chunk;
12476 }
12477
12478 /*******************************************************************************
12479 * Function: calculate_bitmap_chunksize
12480 * Description: Calculates the bitmap chunk size for the given device
12481 * Parameters:
12482 * st : supertype information
12483 * dev : device for the bitmap
12484 *
12485 * Returns:
12486 * The bitmap chunk size
12487 ******************************************************************************/
12488 static unsigned long long calculate_bitmap_chunksize(struct supertype *st,
12489 struct imsm_dev *dev)
12490 {
12491 struct intel_super *super = st->sb;
12492 unsigned long long min_chunksize;
12493 unsigned long long result = IMSM_DEFAULT_BITMAP_CHUNKSIZE;
12494 size_t dev_size = imsm_dev_size(dev);
12495
12496 min_chunksize = calculate_bitmap_min_chunksize(
12497 IMSM_BITMAP_AREA_SIZE * super->sector_size, dev_size);
12498
12499 if (result < min_chunksize)
12500 result = min_chunksize;
12501
12502 return result;
12503 }
12504
12505 /*******************************************************************************
12506 * Function: init_bitmap_header
12507 * Description: Initialize the bitmap header structure
12508 * Parameters:
12509 * st : supertype information
12510 * bms : bitmap header struct to initialize
12511 * dev : device for the bitmap
12512 *
12513 * Returns:
12514 * 0 : success
12515 * -1 : fail
12516 ******************************************************************************/
12517 static int init_bitmap_header(struct supertype *st, struct bitmap_super_s *bms,
12518 struct imsm_dev *dev)
12519 {
12520 int vol_uuid[4];
12521
12522 if (!bms || !dev)
12523 return -1;
12524
12525 bms->magic = __cpu_to_le32(BITMAP_MAGIC);
12526 bms->version = __cpu_to_le32(BITMAP_MAJOR_HI);
12527 bms->daemon_sleep = __cpu_to_le32(IMSM_DEFAULT_BITMAP_DAEMON_SLEEP);
12528 bms->sync_size = __cpu_to_le64(IMSM_BITMAP_AREA_SIZE);
12529 bms->write_behind = __cpu_to_le32(0);
12530
12531 uuid_from_super_imsm(st, vol_uuid);
12532 memcpy(bms->uuid, vol_uuid, 16);
12533
12534 bms->chunksize = calculate_bitmap_chunksize(st, dev);
12535
12536 return 0;
12537 }
12538
12539 /*******************************************************************************
12540 * Function: validate_internal_bitmap_for_drive
12541 * Description: Verify if the bitmap header for a given drive.
12542 * Parameters:
12543 * st : supertype information
12544 * offset : The offset from the beginning of the drive where to look for
12545 * the bitmap header.
12546 * d : the drive info
12547 *
12548 * Returns:
12549 * 0 : success
12550 * -1 : fail
12551 ******************************************************************************/
12552 static int validate_internal_bitmap_for_drive(struct supertype *st,
12553 unsigned long long offset,
12554 struct dl *d)
12555 {
12556 struct intel_super *super = st->sb;
12557 int ret = -1;
12558 int vol_uuid[4];
12559 bitmap_super_t *bms;
12560 int fd;
12561
12562 if (!d)
12563 return -1;
12564
12565 void *read_buf;
12566
12567 if (posix_memalign(&read_buf, MAX_SECTOR_SIZE, IMSM_BITMAP_HEADER_SIZE))
12568 return -1;
12569
12570 fd = d->fd;
12571 if (!is_fd_valid(fd)) {
12572 fd = open(d->devname, O_RDONLY, 0);
12573
12574 if (!is_fd_valid(fd)) {
12575 dprintf("cannot open the device %s\n", d->devname);
12576 goto abort;
12577 }
12578 }
12579
12580 if (lseek64(fd, offset * super->sector_size, SEEK_SET) < 0)
12581 goto abort;
12582 if (read(fd, read_buf, IMSM_BITMAP_HEADER_SIZE) !=
12583 IMSM_BITMAP_HEADER_SIZE)
12584 goto abort;
12585
12586 uuid_from_super_imsm(st, vol_uuid);
12587
12588 bms = read_buf;
12589 if ((bms->magic != __cpu_to_le32(BITMAP_MAGIC)) ||
12590 (bms->version != __cpu_to_le32(BITMAP_MAJOR_HI)) ||
12591 (!same_uuid((int *)bms->uuid, vol_uuid, st->ss->swapuuid))) {
12592 dprintf("wrong bitmap header detected\n");
12593 goto abort;
12594 }
12595
12596 ret = 0;
12597 abort:
12598 if (!is_fd_valid(d->fd))
12599 close_fd(&fd);
12600
12601 if (read_buf)
12602 free(read_buf);
12603
12604 return ret;
12605 }
12606
12607 /*******************************************************************************
12608 * Function: validate_internal_bitmap_imsm
12609 * Description: Verify if the bitmap header is in place and with proper data.
12610 * Parameters:
12611 * st : supertype information
12612 *
12613 * Returns:
12614 * 0 : success or device w/o RWH_BITMAP
12615 * -1 : fail
12616 ******************************************************************************/
12617 static int validate_internal_bitmap_imsm(struct supertype *st)
12618 {
12619 struct intel_super *super = st->sb;
12620 struct imsm_dev *dev = get_imsm_dev(super, super->current_vol);
12621 unsigned long long offset;
12622 struct dl *d;
12623
12624 if (dev->rwh_policy != RWH_BITMAP)
12625 return 0;
12626
12627 offset = get_bitmap_header_sector(super, super->current_vol);
12628 for (d = super->disks; d; d = d->next) {
12629 if (d->index < 0 || is_failed(&d->disk))
12630 continue;
12631
12632 if (validate_internal_bitmap_for_drive(st, offset, d)) {
12633 pr_err("imsm: bitmap validation failed\n");
12634 return -1;
12635 }
12636 }
12637 return 0;
12638 }
12639
12640 /*******************************************************************************
12641 * Function: add_internal_bitmap_imsm
12642 * Description: Mark the volume to use the bitmap and updates the chunk size value.
12643 * Parameters:
12644 * st : supertype information
12645 * chunkp : bitmap chunk size
12646 * delay : not used for imsm
12647 * write_behind : not used for imsm
12648 * size : not used for imsm
12649 * may_change : not used for imsm
12650 * amajor : not used for imsm
12651 *
12652 * Returns:
12653 * 0 : success
12654 * -1 : fail
12655 ******************************************************************************/
12656 static int add_internal_bitmap_imsm(struct supertype *st, int *chunkp,
12657 int delay, int write_behind,
12658 unsigned long long size, int may_change,
12659 int amajor)
12660 {
12661 struct intel_super *super = st->sb;
12662 int vol_idx = super->current_vol;
12663 struct imsm_dev *dev;
12664
12665 if (!super->devlist || vol_idx == -1 || !chunkp)
12666 return -1;
12667
12668 dev = get_imsm_dev(super, vol_idx);
12669 dev->rwh_policy = RWH_BITMAP;
12670 *chunkp = calculate_bitmap_chunksize(st, dev);
12671 return 0;
12672 }
12673
12674 /*******************************************************************************
12675 * Function: locate_bitmap_imsm
12676 * Description: Seek 'fd' to start of write-intent-bitmap.
12677 * Parameters:
12678 * st : supertype information
12679 * fd : file descriptor for the device
12680 * node_num : not used for imsm
12681 *
12682 * Returns:
12683 * 0 : success
12684 * -1 : fail
12685 ******************************************************************************/
12686 static int locate_bitmap_imsm(struct supertype *st, int fd, int node_num)
12687 {
12688 struct intel_super *super = st->sb;
12689 unsigned long long offset;
12690 int vol_idx = super->current_vol;
12691
12692 if (!super->devlist || vol_idx == -1)
12693 return -1;
12694
12695 offset = get_bitmap_header_sector(super, super->current_vol);
12696 dprintf("bitmap header offset is %llu\n", offset);
12697
12698 lseek64(fd, offset << 9, 0);
12699
12700 return 0;
12701 }
12702
12703 /*******************************************************************************
12704 * Function: write_init_bitmap_imsm
12705 * Description: Write a bitmap header and prepares the area for the bitmap.
12706 * Parameters:
12707 * st : supertype information
12708 * fd : file descriptor for the device
12709 * update : not used for imsm
12710 *
12711 * Returns:
12712 * 0 : success
12713 * -1 : fail
12714 ******************************************************************************/
12715 static int write_init_bitmap_imsm(struct supertype *st, int fd,
12716 enum bitmap_update update)
12717 {
12718 struct intel_super *super = st->sb;
12719 int vol_idx = super->current_vol;
12720 int ret = 0;
12721 unsigned long long offset;
12722 bitmap_super_t bms = { 0 };
12723 size_t written = 0;
12724 size_t to_write;
12725 ssize_t rv_num;
12726 void *buf;
12727
12728 if (!super->devlist || !super->sector_size || vol_idx == -1)
12729 return -1;
12730
12731 struct imsm_dev *dev = get_imsm_dev(super, vol_idx);
12732
12733 /* first clear the space for bitmap header */
12734 unsigned long long bitmap_area_start =
12735 get_bitmap_header_sector(super, vol_idx);
12736
12737 dprintf("zeroing area start (%llu) and size (%u)\n", bitmap_area_start,
12738 IMSM_BITMAP_AND_HEADER_SIZE / super->sector_size);
12739 if (zero_disk_range(fd, bitmap_area_start,
12740 IMSM_BITMAP_HEADER_SIZE / super->sector_size)) {
12741 pr_err("imsm: cannot zeroing the space for the bitmap\n");
12742 return -1;
12743 }
12744
12745 /* The bitmap area should be filled with "1"s to perform initial
12746 * synchronization.
12747 */
12748 if (posix_memalign(&buf, MAX_SECTOR_SIZE, MAX_SECTOR_SIZE))
12749 return -1;
12750 memset(buf, 0xFF, MAX_SECTOR_SIZE);
12751 offset = get_bitmap_sector(super, vol_idx);
12752 lseek64(fd, offset << 9, 0);
12753 while (written < IMSM_BITMAP_AREA_SIZE) {
12754 to_write = IMSM_BITMAP_AREA_SIZE - written;
12755 if (to_write > MAX_SECTOR_SIZE)
12756 to_write = MAX_SECTOR_SIZE;
12757 rv_num = write(fd, buf, MAX_SECTOR_SIZE);
12758 if (rv_num != MAX_SECTOR_SIZE) {
12759 ret = -1;
12760 dprintf("cannot initialize bitmap area\n");
12761 goto abort;
12762 }
12763 written += rv_num;
12764 }
12765
12766 /* write a bitmap header */
12767 init_bitmap_header(st, &bms, dev);
12768 memset(buf, 0, MAX_SECTOR_SIZE);
12769 memcpy(buf, &bms, sizeof(bitmap_super_t));
12770 if (locate_bitmap_imsm(st, fd, 0)) {
12771 ret = -1;
12772 dprintf("cannot locate the bitmap\n");
12773 goto abort;
12774 }
12775 if (write(fd, buf, MAX_SECTOR_SIZE) != MAX_SECTOR_SIZE) {
12776 ret = -1;
12777 dprintf("cannot write the bitmap header\n");
12778 goto abort;
12779 }
12780 fsync(fd);
12781
12782 abort:
12783 free(buf);
12784
12785 return ret;
12786 }
12787
12788 /*******************************************************************************
12789 * Function: is_vol_to_setup_bitmap
12790 * Description: Checks if a bitmap should be activated on the dev.
12791 * Parameters:
12792 * info : info about the volume to setup the bitmap
12793 * dev : the device to check against bitmap creation
12794 *
12795 * Returns:
12796 * 0 : bitmap should be set up on the device
12797 * -1 : otherwise
12798 ******************************************************************************/
12799 static int is_vol_to_setup_bitmap(struct mdinfo *info, struct imsm_dev *dev)
12800 {
12801 if (!dev || !info)
12802 return -1;
12803
12804 if ((strcmp((char *)dev->volume, info->name) == 0) &&
12805 (dev->rwh_policy == RWH_BITMAP))
12806 return -1;
12807
12808 return 0;
12809 }
12810
12811 /*******************************************************************************
12812 * Function: set_bitmap_sysfs
12813 * Description: Set the sysfs atributes of a given volume to activate the bitmap.
12814 * Parameters:
12815 * info : info about the volume where the bitmap should be setup
12816 * chunksize : bitmap chunk size
12817 * location : location of the bitmap
12818 *
12819 * Returns:
12820 * 0 : success
12821 * -1 : fail
12822 ******************************************************************************/
12823 static int set_bitmap_sysfs(struct mdinfo *info, unsigned long long chunksize,
12824 char *location)
12825 {
12826 /* The bitmap/metadata is set to external to allow changing of value for
12827 * bitmap/location. When external is used, the kernel will treat an offset
12828 * related to the device's first lba (in opposition to the "internal" case
12829 * when this value is related to the beginning of the superblock).
12830 */
12831 if (sysfs_set_str(info, NULL, "bitmap/metadata", "external")) {
12832 dprintf("failed to set bitmap/metadata\n");
12833 return -1;
12834 }
12835
12836 /* It can only be changed when no bitmap is active.
12837 * Should be bigger than 512 and must be power of 2.
12838 * It is expecting the value in bytes.
12839 */
12840 if (sysfs_set_num(info, NULL, "bitmap/chunksize",
12841 __cpu_to_le32(chunksize))) {
12842 dprintf("failed to set bitmap/chunksize\n");
12843 return -1;
12844 }
12845
12846 /* It is expecting the value in sectors. */
12847 if (sysfs_set_num(info, NULL, "bitmap/space",
12848 __cpu_to_le64(IMSM_BITMAP_AREA_SIZE))) {
12849 dprintf("failed to set bitmap/space\n");
12850 return -1;
12851 }
12852
12853 /* Determines the delay between the bitmap updates.
12854 * It is expecting the value in seconds.
12855 */
12856 if (sysfs_set_num(info, NULL, "bitmap/time_base",
12857 __cpu_to_le64(IMSM_DEFAULT_BITMAP_DAEMON_SLEEP))) {
12858 dprintf("failed to set bitmap/time_base\n");
12859 return -1;
12860 }
12861
12862 /* It is expecting the value in sectors with a sign at the beginning. */
12863 if (sysfs_set_str(info, NULL, "bitmap/location", location)) {
12864 dprintf("failed to set bitmap/location\n");
12865 return -1;
12866 }
12867
12868 return 0;
12869 }
12870
12871 /*******************************************************************************
12872 * Function: set_bitmap_imsm
12873 * Description: Setup the bitmap for the given volume
12874 * Parameters:
12875 * st : supertype information
12876 * info : info about the volume where the bitmap should be setup
12877 *
12878 * Returns:
12879 * 0 : success
12880 * -1 : fail
12881 ******************************************************************************/
12882 static int set_bitmap_imsm(struct supertype *st, struct mdinfo *info)
12883 {
12884 struct intel_super *super = st->sb;
12885 int prev_current_vol = super->current_vol;
12886 struct imsm_dev *dev;
12887 int ret = -1;
12888 char location[16] = "";
12889 unsigned long long chunksize;
12890 struct intel_dev *dev_it;
12891
12892 for (dev_it = super->devlist; dev_it; dev_it = dev_it->next) {
12893 super->current_vol = dev_it->index;
12894 dev = get_imsm_dev(super, super->current_vol);
12895
12896 if (is_vol_to_setup_bitmap(info, dev)) {
12897 if (validate_internal_bitmap_imsm(st)) {
12898 dprintf("bitmap header validation failed\n");
12899 goto abort;
12900 }
12901
12902 chunksize = calculate_bitmap_chunksize(st, dev);
12903 dprintf("chunk size is %llu\n", chunksize);
12904
12905 snprintf(location, sizeof(location), "+%llu",
12906 get_bitmap_sector(super, super->current_vol));
12907 dprintf("bitmap offset is %s\n", location);
12908
12909 if (set_bitmap_sysfs(info, chunksize, location)) {
12910 dprintf("cannot setup the bitmap\n");
12911 goto abort;
12912 }
12913 }
12914 }
12915 ret = 0;
12916 abort:
12917 super->current_vol = prev_current_vol;
12918 return ret;
12919 }
12920
12921 struct superswitch super_imsm = {
12922 .examine_super = examine_super_imsm,
12923 .brief_examine_super = brief_examine_super_imsm,
12924 .brief_examine_subarrays = brief_examine_subarrays_imsm,
12925 .export_examine_super = export_examine_super_imsm,
12926 .detail_super = detail_super_imsm,
12927 .brief_detail_super = brief_detail_super_imsm,
12928 .write_init_super = write_init_super_imsm,
12929 .validate_geometry = validate_geometry_imsm,
12930 .add_to_super = add_to_super_imsm,
12931 .remove_from_super = remove_from_super_imsm,
12932 .detail_platform = detail_platform_imsm,
12933 .export_detail_platform = export_detail_platform_imsm,
12934 .kill_subarray = kill_subarray_imsm,
12935 .update_subarray = update_subarray_imsm,
12936 .load_container = load_container_imsm,
12937 .default_geometry = default_geometry_imsm,
12938 .get_disk_controller_domain = imsm_get_disk_controller_domain,
12939 .reshape_super = imsm_reshape_super,
12940 .manage_reshape = imsm_manage_reshape,
12941 .recover_backup = recover_backup_imsm,
12942 .examine_badblocks = examine_badblocks_imsm,
12943 .match_home = match_home_imsm,
12944 .uuid_from_super= uuid_from_super_imsm,
12945 .getinfo_super = getinfo_super_imsm,
12946 .getinfo_super_disks = getinfo_super_disks_imsm,
12947 .update_super = update_super_imsm,
12948
12949 .avail_size = avail_size_imsm,
12950 .get_spare_criteria = get_spare_criteria_imsm,
12951
12952 .compare_super = compare_super_imsm,
12953
12954 .load_super = load_super_imsm,
12955 .init_super = init_super_imsm,
12956 .store_super = store_super_imsm,
12957 .free_super = free_super_imsm,
12958 .match_metadata_desc = match_metadata_desc_imsm,
12959 .container_content = container_content_imsm,
12960 .validate_container = validate_container_imsm,
12961
12962 .add_internal_bitmap = add_internal_bitmap_imsm,
12963 .locate_bitmap = locate_bitmap_imsm,
12964 .write_bitmap = write_init_bitmap_imsm,
12965 .set_bitmap = set_bitmap_imsm,
12966
12967 .write_init_ppl = write_init_ppl_imsm,
12968 .validate_ppl = validate_ppl_imsm,
12969
12970 .external = 1,
12971 .name = "imsm",
12972
12973 /* for mdmon */
12974 .open_new = imsm_open_new,
12975 .set_array_state= imsm_set_array_state,
12976 .set_disk = imsm_set_disk,
12977 .sync_metadata = imsm_sync_metadata,
12978 .activate_spare = imsm_activate_spare,
12979 .process_update = imsm_process_update,
12980 .prepare_update = imsm_prepare_update,
12981 .record_bad_block = imsm_record_badblock,
12982 .clear_bad_block = imsm_clear_badblock,
12983 .get_bad_blocks = imsm_get_badblocks,
12984 };