]> git.ipfire.org Git - thirdparty/mdadm.git/blob - super-intel.c
6ff336ee6f82dbf955d59bd40c6376748704ee5d
[thirdparty/mdadm.git] / super-intel.c
1 /*
2 * mdadm - Intel(R) Matrix Storage Manager Support
3 *
4 * Copyright (C) 2002-2008 Intel Corporation
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #define HAVE_STDINT_H 1
21 #include "mdadm.h"
22 #include "mdmon.h"
23 #include "sha1.h"
24 #include "platform-intel.h"
25 #include <values.h>
26 #include <scsi/sg.h>
27 #include <ctype.h>
28 #include <dirent.h>
29
30 /* MPB == Metadata Parameter Block */
31 #define MPB_SIGNATURE "Intel Raid ISM Cfg Sig. "
32 #define MPB_SIG_LEN (strlen(MPB_SIGNATURE))
33 #define MPB_VERSION_RAID0 "1.0.00"
34 #define MPB_VERSION_RAID1 "1.1.00"
35 #define MPB_VERSION_MANY_VOLUMES_PER_ARRAY "1.2.00"
36 #define MPB_VERSION_3OR4_DISK_ARRAY "1.2.01"
37 #define MPB_VERSION_RAID5 "1.2.02"
38 #define MPB_VERSION_5OR6_DISK_ARRAY "1.2.04"
39 #define MPB_VERSION_CNG "1.2.06"
40 #define MPB_VERSION_ATTRIBS "1.3.00"
41 #define MAX_SIGNATURE_LENGTH 32
42 #define MAX_RAID_SERIAL_LEN 16
43
44 /* supports RAID0 */
45 #define MPB_ATTRIB_RAID0 __cpu_to_le32(0x00000001)
46 /* supports RAID1 */
47 #define MPB_ATTRIB_RAID1 __cpu_to_le32(0x00000002)
48 /* supports RAID10 */
49 #define MPB_ATTRIB_RAID10 __cpu_to_le32(0x00000004)
50 /* supports RAID1E */
51 #define MPB_ATTRIB_RAID1E __cpu_to_le32(0x00000008)
52 /* supports RAID5 */
53 #define MPB_ATTRIB_RAID5 __cpu_to_le32(0x00000010)
54 /* supports RAID CNG */
55 #define MPB_ATTRIB_RAIDCNG __cpu_to_le32(0x00000020)
56 /* supports expanded stripe sizes of 256K, 512K and 1MB */
57 #define MPB_ATTRIB_EXP_STRIPE_SIZE __cpu_to_le32(0x00000040)
58
59 /* The OROM Support RST Caching of Volumes */
60 #define MPB_ATTRIB_NVM __cpu_to_le32(0x02000000)
61 /* The OROM supports creating disks greater than 2TB */
62 #define MPB_ATTRIB_2TB_DISK __cpu_to_le32(0x04000000)
63 /* The OROM supports Bad Block Management */
64 #define MPB_ATTRIB_BBM __cpu_to_le32(0x08000000)
65
66 /* THe OROM Supports NVM Caching of Volumes */
67 #define MPB_ATTRIB_NEVER_USE2 __cpu_to_le32(0x10000000)
68 /* The OROM supports creating volumes greater than 2TB */
69 #define MPB_ATTRIB_2TB __cpu_to_le32(0x20000000)
70 /* originally for PMP, now it's wasted b/c. Never use this bit! */
71 #define MPB_ATTRIB_NEVER_USE __cpu_to_le32(0x40000000)
72 /* Verify MPB contents against checksum after reading MPB */
73 #define MPB_ATTRIB_CHECKSUM_VERIFY __cpu_to_le32(0x80000000)
74
75 /* Define all supported attributes that have to be accepted by mdadm
76 */
77 #define MPB_ATTRIB_SUPPORTED (MPB_ATTRIB_CHECKSUM_VERIFY | \
78 MPB_ATTRIB_2TB | \
79 MPB_ATTRIB_2TB_DISK | \
80 MPB_ATTRIB_RAID0 | \
81 MPB_ATTRIB_RAID1 | \
82 MPB_ATTRIB_RAID10 | \
83 MPB_ATTRIB_RAID5 | \
84 MPB_ATTRIB_EXP_STRIPE_SIZE | \
85 MPB_ATTRIB_BBM)
86
87 /* Define attributes that are unused but not harmful */
88 #define MPB_ATTRIB_IGNORED (MPB_ATTRIB_NEVER_USE)
89
90 #define MPB_SECTOR_CNT 2210
91 #define IMSM_RESERVED_SECTORS 8192
92 #define NUM_BLOCKS_DIRTY_STRIPE_REGION 2048
93 #define SECT_PER_MB_SHIFT 11
94 #define MAX_SECTOR_SIZE 4096
95 #define MULTIPLE_PPL_AREA_SIZE_IMSM (1024 * 1024) /* Size of the whole
96 * mutliple PPL area
97 */
98
99 /*
100 * Internal Write-intent bitmap is stored in the same area where PPL.
101 * Both features are mutually exclusive, so it is not an issue.
102 * The first 8KiB of the area are reserved and shall not be used.
103 */
104 #define IMSM_BITMAP_AREA_RESERVED_SIZE 8192
105
106 #define IMSM_BITMAP_HEADER_OFFSET (IMSM_BITMAP_AREA_RESERVED_SIZE)
107 #define IMSM_BITMAP_HEADER_SIZE MAX_SECTOR_SIZE
108
109 #define IMSM_BITMAP_START_OFFSET (IMSM_BITMAP_HEADER_OFFSET + IMSM_BITMAP_HEADER_SIZE)
110 #define IMSM_BITMAP_AREA_SIZE (MULTIPLE_PPL_AREA_SIZE_IMSM - IMSM_BITMAP_START_OFFSET)
111 #define IMSM_BITMAP_AND_HEADER_SIZE (IMSM_BITMAP_AREA_SIZE + IMSM_BITMAP_HEADER_SIZE)
112
113 #define IMSM_DEFAULT_BITMAP_CHUNKSIZE (64 * 1024 * 1024)
114 #define IMSM_DEFAULT_BITMAP_DAEMON_SLEEP 5
115
116 /*
117 * This macro let's us ensure that no-one accidentally
118 * changes the size of a struct
119 */
120 #define ASSERT_SIZE(_struct, size) \
121 static inline void __assert_size_##_struct(void) \
122 { \
123 switch (0) { \
124 case 0: break; \
125 case (sizeof(struct _struct) == size): break; \
126 } \
127 }
128
129 /* Disk configuration info. */
130 #define IMSM_MAX_DEVICES 255
131 struct imsm_disk {
132 __u8 serial[MAX_RAID_SERIAL_LEN];/* 0xD8 - 0xE7 ascii serial number */
133 __u32 total_blocks_lo; /* 0xE8 - 0xEB total blocks lo */
134 __u32 scsi_id; /* 0xEC - 0xEF scsi ID */
135 #define SPARE_DISK __cpu_to_le32(0x01) /* Spare */
136 #define CONFIGURED_DISK __cpu_to_le32(0x02) /* Member of some RaidDev */
137 #define FAILED_DISK __cpu_to_le32(0x04) /* Permanent failure */
138 #define JOURNAL_DISK __cpu_to_le32(0x2000000) /* Device marked as Journaling Drive */
139 __u32 status; /* 0xF0 - 0xF3 */
140 __u32 owner_cfg_num; /* which config 0,1,2... owns this disk */
141 __u32 total_blocks_hi; /* 0xF4 - 0xF5 total blocks hi */
142 #define IMSM_DISK_FILLERS 3
143 __u32 filler[IMSM_DISK_FILLERS]; /* 0xF5 - 0x107 MPB_DISK_FILLERS for future expansion */
144 };
145 ASSERT_SIZE(imsm_disk, 48)
146
147 /* map selector for map managment
148 */
149 #define MAP_0 0
150 #define MAP_1 1
151 #define MAP_X -1
152
153 /* RAID map configuration infos. */
154 struct imsm_map {
155 __u32 pba_of_lba0_lo; /* start address of partition */
156 __u32 blocks_per_member_lo;/* blocks per member */
157 __u32 num_data_stripes_lo; /* number of data stripes */
158 __u16 blocks_per_strip;
159 __u8 map_state; /* Normal, Uninitialized, Degraded, Failed */
160 #define IMSM_T_STATE_NORMAL 0
161 #define IMSM_T_STATE_UNINITIALIZED 1
162 #define IMSM_T_STATE_DEGRADED 2
163 #define IMSM_T_STATE_FAILED 3
164 __u8 raid_level;
165 #define IMSM_T_RAID0 0
166 #define IMSM_T_RAID1 1
167 #define IMSM_T_RAID5 5 /* since metadata version 1.2.02 ? */
168 __u8 num_members; /* number of member disks */
169 __u8 num_domains; /* number of parity domains */
170 __u8 failed_disk_num; /* valid only when state is degraded */
171 __u8 ddf;
172 __u32 pba_of_lba0_hi;
173 __u32 blocks_per_member_hi;
174 __u32 num_data_stripes_hi;
175 __u32 filler[4]; /* expansion area */
176 #define IMSM_ORD_REBUILD (1 << 24)
177 __u32 disk_ord_tbl[1]; /* disk_ord_tbl[num_members],
178 * top byte contains some flags
179 */
180 };
181 ASSERT_SIZE(imsm_map, 52)
182
183 struct imsm_vol {
184 __u32 curr_migr_unit_lo;
185 __u32 checkpoint_id; /* id to access curr_migr_unit */
186 __u8 migr_state; /* Normal or Migrating */
187 #define MIGR_INIT 0
188 #define MIGR_REBUILD 1
189 #define MIGR_VERIFY 2 /* analagous to echo check > sync_action */
190 #define MIGR_GEN_MIGR 3
191 #define MIGR_STATE_CHANGE 4
192 #define MIGR_REPAIR 5
193 __u8 migr_type; /* Initializing, Rebuilding, ... */
194 #define RAIDVOL_CLEAN 0
195 #define RAIDVOL_DIRTY 1
196 #define RAIDVOL_DSRECORD_VALID 2
197 __u8 dirty;
198 __u8 fs_state; /* fast-sync state for CnG (0xff == disabled) */
199 __u16 verify_errors; /* number of mismatches */
200 __u16 bad_blocks; /* number of bad blocks during verify */
201 __u32 curr_migr_unit_hi;
202 __u32 filler[3];
203 struct imsm_map map[1];
204 /* here comes another one if migr_state */
205 };
206 ASSERT_SIZE(imsm_vol, 84)
207
208 struct imsm_dev {
209 __u8 volume[MAX_RAID_SERIAL_LEN];
210 __u32 size_low;
211 __u32 size_high;
212 #define DEV_BOOTABLE __cpu_to_le32(0x01)
213 #define DEV_BOOT_DEVICE __cpu_to_le32(0x02)
214 #define DEV_READ_COALESCING __cpu_to_le32(0x04)
215 #define DEV_WRITE_COALESCING __cpu_to_le32(0x08)
216 #define DEV_LAST_SHUTDOWN_DIRTY __cpu_to_le32(0x10)
217 #define DEV_HIDDEN_AT_BOOT __cpu_to_le32(0x20)
218 #define DEV_CURRENTLY_HIDDEN __cpu_to_le32(0x40)
219 #define DEV_VERIFY_AND_FIX __cpu_to_le32(0x80)
220 #define DEV_MAP_STATE_UNINIT __cpu_to_le32(0x100)
221 #define DEV_NO_AUTO_RECOVERY __cpu_to_le32(0x200)
222 #define DEV_CLONE_N_GO __cpu_to_le32(0x400)
223 #define DEV_CLONE_MAN_SYNC __cpu_to_le32(0x800)
224 #define DEV_CNG_MASTER_DISK_NUM __cpu_to_le32(0x1000)
225 __u32 status; /* Persistent RaidDev status */
226 __u32 reserved_blocks; /* Reserved blocks at beginning of volume */
227 __u8 migr_priority;
228 __u8 num_sub_vols;
229 __u8 tid;
230 __u8 cng_master_disk;
231 __u16 cache_policy;
232 __u8 cng_state;
233 __u8 cng_sub_state;
234 __u16 my_vol_raid_dev_num; /* Used in Unique volume Id for this RaidDev */
235
236 /* NVM_EN */
237 __u8 nv_cache_mode;
238 __u8 nv_cache_flags;
239
240 /* Unique Volume Id of the NvCache Volume associated with this volume */
241 __u32 nvc_vol_orig_family_num;
242 __u16 nvc_vol_raid_dev_num;
243
244 #define RWH_OFF 0
245 #define RWH_DISTRIBUTED 1
246 #define RWH_JOURNALING_DRIVE 2
247 #define RWH_MULTIPLE_DISTRIBUTED 3
248 #define RWH_MULTIPLE_PPLS_JOURNALING_DRIVE 4
249 #define RWH_MULTIPLE_OFF 5
250 #define RWH_BITMAP 6
251 __u8 rwh_policy; /* Raid Write Hole Policy */
252 __u8 jd_serial[MAX_RAID_SERIAL_LEN]; /* Journal Drive serial number */
253 __u8 filler1;
254
255 #define IMSM_DEV_FILLERS 3
256 __u32 filler[IMSM_DEV_FILLERS];
257 struct imsm_vol vol;
258 };
259 ASSERT_SIZE(imsm_dev, 164)
260
261 struct imsm_super {
262 __u8 sig[MAX_SIGNATURE_LENGTH]; /* 0x00 - 0x1F */
263 __u32 check_sum; /* 0x20 - 0x23 MPB Checksum */
264 __u32 mpb_size; /* 0x24 - 0x27 Size of MPB */
265 __u32 family_num; /* 0x28 - 0x2B Checksum from first time this config was written */
266 __u32 generation_num; /* 0x2C - 0x2F Incremented each time this array's MPB is written */
267 __u32 error_log_size; /* 0x30 - 0x33 in bytes */
268 __u32 attributes; /* 0x34 - 0x37 */
269 __u8 num_disks; /* 0x38 Number of configured disks */
270 __u8 num_raid_devs; /* 0x39 Number of configured volumes */
271 __u8 error_log_pos; /* 0x3A */
272 __u8 fill[1]; /* 0x3B */
273 __u32 cache_size; /* 0x3c - 0x40 in mb */
274 __u32 orig_family_num; /* 0x40 - 0x43 original family num */
275 __u32 pwr_cycle_count; /* 0x44 - 0x47 simulated power cycle count for array */
276 __u32 bbm_log_size; /* 0x48 - 0x4B - size of bad Block Mgmt Log in bytes */
277 __u16 num_raid_devs_created; /* 0x4C - 0x4D Used for generating unique
278 * volume IDs for raid_dev created in this array
279 * (starts at 1)
280 */
281 __u16 filler1; /* 0x4E - 0x4F */
282 __u64 creation_time; /* 0x50 - 0x57 Array creation time */
283 #define IMSM_FILLERS 32
284 __u32 filler[IMSM_FILLERS]; /* 0x58 - 0xD7 RAID_MPB_FILLERS */
285 struct imsm_disk disk[1]; /* 0xD8 diskTbl[numDisks] */
286 /* here comes imsm_dev[num_raid_devs] */
287 /* here comes BBM logs */
288 };
289 ASSERT_SIZE(imsm_super, 264)
290
291 #define BBM_LOG_MAX_ENTRIES 254
292 #define BBM_LOG_MAX_LBA_ENTRY_VAL 256 /* Represents 256 LBAs */
293 #define BBM_LOG_SIGNATURE 0xabadb10c
294
295 struct bbm_log_block_addr {
296 __u16 w1;
297 __u32 dw1;
298 } __attribute__ ((__packed__));
299
300 struct bbm_log_entry {
301 __u8 marked_count; /* Number of blocks marked - 1 */
302 __u8 disk_ordinal; /* Disk entry within the imsm_super */
303 struct bbm_log_block_addr defective_block_start;
304 } __attribute__ ((__packed__));
305
306 struct bbm_log {
307 __u32 signature; /* 0xABADB10C */
308 __u32 entry_count;
309 struct bbm_log_entry marked_block_entries[BBM_LOG_MAX_ENTRIES];
310 };
311 ASSERT_SIZE(bbm_log, 2040)
312
313 static char *map_state_str[] = { "normal", "uninitialized", "degraded", "failed" };
314
315 #define BLOCKS_PER_KB (1024/512)
316
317 #define RAID_DISK_RESERVED_BLOCKS_IMSM_HI 2209
318
319 #define GEN_MIGR_AREA_SIZE 2048 /* General Migration Copy Area size in blocks */
320
321 #define MIGR_REC_BUF_SECTORS 1 /* size of migr_record i/o buffer in sectors */
322 #define MIGR_REC_SECTOR_POSITION 1 /* migr_record position offset on disk,
323 * MIGR_REC_BUF_SECTORS <= MIGR_REC_SECTOR_POS
324 */
325
326 #define UNIT_SRC_NORMAL 0 /* Source data for curr_migr_unit must
327 * be recovered using srcMap */
328 #define UNIT_SRC_IN_CP_AREA 1 /* Source data for curr_migr_unit has
329 * already been migrated and must
330 * be recovered from checkpoint area */
331
332 #define PPL_ENTRY_SPACE (128 * 1024) /* Size of single PPL, without the header */
333
334 struct migr_record {
335 __u32 rec_status; /* Status used to determine how to restart
336 * migration in case it aborts
337 * in some fashion */
338 __u32 curr_migr_unit_lo; /* 0..numMigrUnits-1 */
339 __u32 family_num; /* Family number of MPB
340 * containing the RaidDev
341 * that is migrating */
342 __u32 ascending_migr; /* True if migrating in increasing
343 * order of lbas */
344 __u32 blocks_per_unit; /* Num disk blocks per unit of operation */
345 __u32 dest_depth_per_unit; /* Num member blocks each destMap
346 * member disk
347 * advances per unit-of-operation */
348 __u32 ckpt_area_pba_lo; /* Pba of first block of ckpt copy area */
349 __u32 dest_1st_member_lba_lo; /* First member lba on first
350 * stripe of destination */
351 __u32 num_migr_units_lo; /* Total num migration units-of-op */
352 __u32 post_migr_vol_cap; /* Size of volume after
353 * migration completes */
354 __u32 post_migr_vol_cap_hi; /* Expansion space for LBA64 */
355 __u32 ckpt_read_disk_num; /* Which member disk in destSubMap[0] the
356 * migration ckpt record was read from
357 * (for recovered migrations) */
358 __u32 curr_migr_unit_hi; /* 0..numMigrUnits-1 high order 32 bits */
359 __u32 ckpt_area_pba_hi; /* Pba of first block of ckpt copy area
360 * high order 32 bits */
361 __u32 dest_1st_member_lba_hi; /* First member lba on first stripe of
362 * destination - high order 32 bits */
363 __u32 num_migr_units_hi; /* Total num migration units-of-op
364 * high order 32 bits */
365 __u32 filler[16];
366 };
367 ASSERT_SIZE(migr_record, 128)
368
369 struct md_list {
370 /* usage marker:
371 * 1: load metadata
372 * 2: metadata does not match
373 * 4: already checked
374 */
375 int used;
376 char *devname;
377 int found;
378 int container;
379 dev_t st_rdev;
380 struct md_list *next;
381 };
382
383 #define pr_vrb(fmt, arg...) (void) (verbose && pr_err(fmt, ##arg))
384
385 static __u8 migr_type(struct imsm_dev *dev)
386 {
387 if (dev->vol.migr_type == MIGR_VERIFY &&
388 dev->status & DEV_VERIFY_AND_FIX)
389 return MIGR_REPAIR;
390 else
391 return dev->vol.migr_type;
392 }
393
394 static void set_migr_type(struct imsm_dev *dev, __u8 migr_type)
395 {
396 /* for compatibility with older oroms convert MIGR_REPAIR, into
397 * MIGR_VERIFY w/ DEV_VERIFY_AND_FIX status
398 */
399 if (migr_type == MIGR_REPAIR) {
400 dev->vol.migr_type = MIGR_VERIFY;
401 dev->status |= DEV_VERIFY_AND_FIX;
402 } else {
403 dev->vol.migr_type = migr_type;
404 dev->status &= ~DEV_VERIFY_AND_FIX;
405 }
406 }
407
408 static unsigned int sector_count(__u32 bytes, unsigned int sector_size)
409 {
410 return ROUND_UP(bytes, sector_size) / sector_size;
411 }
412
413 static unsigned int mpb_sectors(struct imsm_super *mpb,
414 unsigned int sector_size)
415 {
416 return sector_count(__le32_to_cpu(mpb->mpb_size), sector_size);
417 }
418
419 struct intel_dev {
420 struct imsm_dev *dev;
421 struct intel_dev *next;
422 unsigned index;
423 };
424
425 struct intel_hba {
426 enum sys_dev_type type;
427 char *path;
428 char *pci_id;
429 struct intel_hba *next;
430 };
431
432 enum action {
433 DISK_REMOVE = 1,
434 DISK_ADD
435 };
436 /* internal representation of IMSM metadata */
437 struct intel_super {
438 union {
439 void *buf; /* O_DIRECT buffer for reading/writing metadata */
440 struct imsm_super *anchor; /* immovable parameters */
441 };
442 union {
443 void *migr_rec_buf; /* buffer for I/O operations */
444 struct migr_record *migr_rec; /* migration record */
445 };
446 int clean_migration_record_by_mdmon; /* when reshape is switched to next
447 array, it indicates that mdmon is allowed to clean migration
448 record */
449 size_t len; /* size of the 'buf' allocation */
450 size_t extra_space; /* extra space in 'buf' that is not used yet */
451 void *next_buf; /* for realloc'ing buf from the manager */
452 size_t next_len;
453 int updates_pending; /* count of pending updates for mdmon */
454 int current_vol; /* index of raid device undergoing creation */
455 unsigned long long create_offset; /* common start for 'current_vol' */
456 __u32 random; /* random data for seeding new family numbers */
457 struct intel_dev *devlist;
458 unsigned int sector_size; /* sector size of used member drives */
459 struct dl {
460 struct dl *next;
461 int index;
462 __u8 serial[MAX_RAID_SERIAL_LEN];
463 int major, minor;
464 char *devname;
465 struct imsm_disk disk;
466 int fd;
467 int extent_cnt;
468 struct extent *e; /* for determining freespace @ create */
469 int raiddisk; /* slot to fill in autolayout */
470 enum action action;
471 } *disks, *current_disk;
472 struct dl *disk_mgmt_list; /* list of disks to add/remove while mdmon
473 active */
474 struct dl *missing; /* disks removed while we weren't looking */
475 struct bbm_log *bbm_log;
476 struct intel_hba *hba; /* device path of the raid controller for this metadata */
477 const struct imsm_orom *orom; /* platform firmware support */
478 struct intel_super *next; /* (temp) list for disambiguating family_num */
479 struct md_bb bb; /* memory for get_bad_blocks call */
480 };
481
482 struct intel_disk {
483 struct imsm_disk disk;
484 #define IMSM_UNKNOWN_OWNER (-1)
485 int owner;
486 struct intel_disk *next;
487 };
488
489 struct extent {
490 unsigned long long start, size;
491 };
492
493 /* definitions of reshape process types */
494 enum imsm_reshape_type {
495 CH_TAKEOVER,
496 CH_MIGRATION,
497 CH_ARRAY_SIZE,
498 };
499
500 /* definition of messages passed to imsm_process_update */
501 enum imsm_update_type {
502 update_activate_spare,
503 update_create_array,
504 update_kill_array,
505 update_rename_array,
506 update_add_remove_disk,
507 update_reshape_container_disks,
508 update_reshape_migration,
509 update_takeover,
510 update_general_migration_checkpoint,
511 update_size_change,
512 update_prealloc_badblocks_mem,
513 update_rwh_policy,
514 };
515
516 struct imsm_update_activate_spare {
517 enum imsm_update_type type;
518 struct dl *dl;
519 int slot;
520 int array;
521 struct imsm_update_activate_spare *next;
522 };
523
524 struct geo_params {
525 char devnm[32];
526 char *dev_name;
527 unsigned long long size;
528 int level;
529 int layout;
530 int chunksize;
531 int raid_disks;
532 };
533
534 enum takeover_direction {
535 R10_TO_R0,
536 R0_TO_R10
537 };
538 struct imsm_update_takeover {
539 enum imsm_update_type type;
540 int subarray;
541 enum takeover_direction direction;
542 };
543
544 struct imsm_update_reshape {
545 enum imsm_update_type type;
546 int old_raid_disks;
547 int new_raid_disks;
548
549 int new_disks[1]; /* new_raid_disks - old_raid_disks makedev number */
550 };
551
552 struct imsm_update_reshape_migration {
553 enum imsm_update_type type;
554 int old_raid_disks;
555 int new_raid_disks;
556 /* fields for array migration changes
557 */
558 int subdev;
559 int new_level;
560 int new_layout;
561 int new_chunksize;
562
563 int new_disks[1]; /* new_raid_disks - old_raid_disks makedev number */
564 };
565
566 struct imsm_update_size_change {
567 enum imsm_update_type type;
568 int subdev;
569 long long new_size;
570 };
571
572 struct imsm_update_general_migration_checkpoint {
573 enum imsm_update_type type;
574 __u64 curr_migr_unit;
575 };
576
577 struct disk_info {
578 __u8 serial[MAX_RAID_SERIAL_LEN];
579 };
580
581 struct imsm_update_create_array {
582 enum imsm_update_type type;
583 int dev_idx;
584 struct imsm_dev dev;
585 };
586
587 struct imsm_update_kill_array {
588 enum imsm_update_type type;
589 int dev_idx;
590 };
591
592 struct imsm_update_rename_array {
593 enum imsm_update_type type;
594 __u8 name[MAX_RAID_SERIAL_LEN];
595 int dev_idx;
596 };
597
598 struct imsm_update_add_remove_disk {
599 enum imsm_update_type type;
600 };
601
602 struct imsm_update_prealloc_bb_mem {
603 enum imsm_update_type type;
604 };
605
606 struct imsm_update_rwh_policy {
607 enum imsm_update_type type;
608 int new_policy;
609 int dev_idx;
610 };
611
612 static const char *_sys_dev_type[] = {
613 [SYS_DEV_UNKNOWN] = "Unknown",
614 [SYS_DEV_SAS] = "SAS",
615 [SYS_DEV_SATA] = "SATA",
616 [SYS_DEV_NVME] = "NVMe",
617 [SYS_DEV_VMD] = "VMD"
618 };
619
620 const char *get_sys_dev_type(enum sys_dev_type type)
621 {
622 if (type >= SYS_DEV_MAX)
623 type = SYS_DEV_UNKNOWN;
624
625 return _sys_dev_type[type];
626 }
627
628 static struct intel_hba * alloc_intel_hba(struct sys_dev *device)
629 {
630 struct intel_hba *result = xmalloc(sizeof(*result));
631
632 result->type = device->type;
633 result->path = xstrdup(device->path);
634 result->next = NULL;
635 if (result->path && (result->pci_id = strrchr(result->path, '/')) != NULL)
636 result->pci_id++;
637
638 return result;
639 }
640
641 static struct intel_hba * find_intel_hba(struct intel_hba *hba, struct sys_dev *device)
642 {
643 struct intel_hba *result;
644
645 for (result = hba; result; result = result->next) {
646 if (result->type == device->type && strcmp(result->path, device->path) == 0)
647 break;
648 }
649 return result;
650 }
651
652 static int attach_hba_to_super(struct intel_super *super, struct sys_dev *device)
653 {
654 struct intel_hba *hba;
655
656 /* check if disk attached to Intel HBA */
657 hba = find_intel_hba(super->hba, device);
658 if (hba != NULL)
659 return 1;
660 /* Check if HBA is already attached to super */
661 if (super->hba == NULL) {
662 super->hba = alloc_intel_hba(device);
663 return 1;
664 }
665
666 hba = super->hba;
667 /* Intel metadata allows for all disks attached to the same type HBA.
668 * Do not support HBA types mixing
669 */
670 if (device->type != hba->type)
671 return 2;
672
673 /* Multiple same type HBAs can be used if they share the same OROM */
674 const struct imsm_orom *device_orom = get_orom_by_device_id(device->dev_id);
675
676 if (device_orom != super->orom)
677 return 2;
678
679 while (hba->next)
680 hba = hba->next;
681
682 hba->next = alloc_intel_hba(device);
683 return 1;
684 }
685
686 static struct sys_dev* find_disk_attached_hba(int fd, const char *devname)
687 {
688 struct sys_dev *list, *elem;
689 char *disk_path;
690
691 if ((list = find_intel_devices()) == NULL)
692 return 0;
693
694 if (!is_fd_valid(fd))
695 disk_path = (char *) devname;
696 else
697 disk_path = diskfd_to_devpath(fd, 1, NULL);
698
699 if (!disk_path)
700 return 0;
701
702 for (elem = list; elem; elem = elem->next)
703 if (path_attached_to_hba(disk_path, elem->path))
704 return elem;
705
706 if (disk_path != devname)
707 free(disk_path);
708
709 return NULL;
710 }
711
712 static int find_intel_hba_capability(int fd, struct intel_super *super,
713 char *devname);
714
715 static struct supertype *match_metadata_desc_imsm(char *arg)
716 {
717 struct supertype *st;
718
719 if (strcmp(arg, "imsm") != 0 &&
720 strcmp(arg, "default") != 0
721 )
722 return NULL;
723
724 st = xcalloc(1, sizeof(*st));
725 st->ss = &super_imsm;
726 st->max_devs = IMSM_MAX_DEVICES;
727 st->minor_version = 0;
728 st->sb = NULL;
729 return st;
730 }
731
732 static __u8 *get_imsm_version(struct imsm_super *mpb)
733 {
734 return &mpb->sig[MPB_SIG_LEN];
735 }
736
737 /* retrieve a disk directly from the anchor when the anchor is known to be
738 * up-to-date, currently only at load time
739 */
740 static struct imsm_disk *__get_imsm_disk(struct imsm_super *mpb, __u8 index)
741 {
742 if (index >= mpb->num_disks)
743 return NULL;
744 return &mpb->disk[index];
745 }
746
747 /* retrieve the disk description based on a index of the disk
748 * in the sub-array
749 */
750 static struct dl *get_imsm_dl_disk(struct intel_super *super, __u8 index)
751 {
752 struct dl *d;
753
754 for (d = super->disks; d; d = d->next)
755 if (d->index == index)
756 return d;
757
758 return NULL;
759 }
760 /* retrieve a disk from the parsed metadata */
761 static struct imsm_disk *get_imsm_disk(struct intel_super *super, __u8 index)
762 {
763 struct dl *dl;
764
765 dl = get_imsm_dl_disk(super, index);
766 if (dl)
767 return &dl->disk;
768
769 return NULL;
770 }
771
772 /* generate a checksum directly from the anchor when the anchor is known to be
773 * up-to-date, currently only at load or write_super after coalescing
774 */
775 static __u32 __gen_imsm_checksum(struct imsm_super *mpb)
776 {
777 __u32 end = mpb->mpb_size / sizeof(end);
778 __u32 *p = (__u32 *) mpb;
779 __u32 sum = 0;
780
781 while (end--) {
782 sum += __le32_to_cpu(*p);
783 p++;
784 }
785
786 return sum - __le32_to_cpu(mpb->check_sum);
787 }
788
789 static size_t sizeof_imsm_map(struct imsm_map *map)
790 {
791 return sizeof(struct imsm_map) + sizeof(__u32) * (map->num_members - 1);
792 }
793
794 struct imsm_map *get_imsm_map(struct imsm_dev *dev, int second_map)
795 {
796 /* A device can have 2 maps if it is in the middle of a migration.
797 * If second_map is:
798 * MAP_0 - we return the first map
799 * MAP_1 - we return the second map if it exists, else NULL
800 * MAP_X - we return the second map if it exists, else the first
801 */
802 struct imsm_map *map = &dev->vol.map[0];
803 struct imsm_map *map2 = NULL;
804
805 if (dev->vol.migr_state)
806 map2 = (void *)map + sizeof_imsm_map(map);
807
808 switch (second_map) {
809 case MAP_0:
810 break;
811 case MAP_1:
812 map = map2;
813 break;
814 case MAP_X:
815 if (map2)
816 map = map2;
817 break;
818 default:
819 map = NULL;
820 }
821 return map;
822
823 }
824
825 /* return the size of the device.
826 * migr_state increases the returned size if map[0] were to be duplicated
827 */
828 static size_t sizeof_imsm_dev(struct imsm_dev *dev, int migr_state)
829 {
830 size_t size = sizeof(*dev) - sizeof(struct imsm_map) +
831 sizeof_imsm_map(get_imsm_map(dev, MAP_0));
832
833 /* migrating means an additional map */
834 if (dev->vol.migr_state)
835 size += sizeof_imsm_map(get_imsm_map(dev, MAP_1));
836 else if (migr_state)
837 size += sizeof_imsm_map(get_imsm_map(dev, MAP_0));
838
839 return size;
840 }
841
842 /* retrieve disk serial number list from a metadata update */
843 static struct disk_info *get_disk_info(struct imsm_update_create_array *update)
844 {
845 void *u = update;
846 struct disk_info *inf;
847
848 inf = u + sizeof(*update) - sizeof(struct imsm_dev) +
849 sizeof_imsm_dev(&update->dev, 0);
850
851 return inf;
852 }
853
854 static struct imsm_dev *__get_imsm_dev(struct imsm_super *mpb, __u8 index)
855 {
856 int offset;
857 int i;
858 void *_mpb = mpb;
859
860 if (index >= mpb->num_raid_devs)
861 return NULL;
862
863 /* devices start after all disks */
864 offset = ((void *) &mpb->disk[mpb->num_disks]) - _mpb;
865
866 for (i = 0; i <= index; i++)
867 if (i == index)
868 return _mpb + offset;
869 else
870 offset += sizeof_imsm_dev(_mpb + offset, 0);
871
872 return NULL;
873 }
874
875 static struct imsm_dev *get_imsm_dev(struct intel_super *super, __u8 index)
876 {
877 struct intel_dev *dv;
878
879 if (index >= super->anchor->num_raid_devs)
880 return NULL;
881 for (dv = super->devlist; dv; dv = dv->next)
882 if (dv->index == index)
883 return dv->dev;
884 return NULL;
885 }
886
887 static inline unsigned long long __le48_to_cpu(const struct bbm_log_block_addr
888 *addr)
889 {
890 return ((((__u64)__le32_to_cpu(addr->dw1)) << 16) |
891 __le16_to_cpu(addr->w1));
892 }
893
894 static inline struct bbm_log_block_addr __cpu_to_le48(unsigned long long sec)
895 {
896 struct bbm_log_block_addr addr;
897
898 addr.w1 = __cpu_to_le16((__u16)(sec & 0xffff));
899 addr.dw1 = __cpu_to_le32((__u32)(sec >> 16) & 0xffffffff);
900 return addr;
901 }
902
903 /* get size of the bbm log */
904 static __u32 get_imsm_bbm_log_size(struct bbm_log *log)
905 {
906 if (!log || log->entry_count == 0)
907 return 0;
908
909 return sizeof(log->signature) +
910 sizeof(log->entry_count) +
911 log->entry_count * sizeof(struct bbm_log_entry);
912 }
913
914 /* check if bad block is not partially stored in bbm log */
915 static int is_stored_in_bbm(struct bbm_log *log, const __u8 idx, const unsigned
916 long long sector, const int length, __u32 *pos)
917 {
918 __u32 i;
919
920 for (i = *pos; i < log->entry_count; i++) {
921 struct bbm_log_entry *entry = &log->marked_block_entries[i];
922 unsigned long long bb_start;
923 unsigned long long bb_end;
924
925 bb_start = __le48_to_cpu(&entry->defective_block_start);
926 bb_end = bb_start + (entry->marked_count + 1);
927
928 if ((entry->disk_ordinal == idx) && (bb_start >= sector) &&
929 (bb_end <= sector + length)) {
930 *pos = i;
931 return 1;
932 }
933 }
934 return 0;
935 }
936
937 /* record new bad block in bbm log */
938 static int record_new_badblock(struct bbm_log *log, const __u8 idx, unsigned
939 long long sector, int length)
940 {
941 int new_bb = 0;
942 __u32 pos = 0;
943 struct bbm_log_entry *entry = NULL;
944
945 while (is_stored_in_bbm(log, idx, sector, length, &pos)) {
946 struct bbm_log_entry *e = &log->marked_block_entries[pos];
947
948 if ((e->marked_count + 1 == BBM_LOG_MAX_LBA_ENTRY_VAL) &&
949 (__le48_to_cpu(&e->defective_block_start) == sector)) {
950 sector += BBM_LOG_MAX_LBA_ENTRY_VAL;
951 length -= BBM_LOG_MAX_LBA_ENTRY_VAL;
952 pos = pos + 1;
953 continue;
954 }
955 entry = e;
956 break;
957 }
958
959 if (entry) {
960 int cnt = (length <= BBM_LOG_MAX_LBA_ENTRY_VAL) ? length :
961 BBM_LOG_MAX_LBA_ENTRY_VAL;
962 entry->defective_block_start = __cpu_to_le48(sector);
963 entry->marked_count = cnt - 1;
964 if (cnt == length)
965 return 1;
966 sector += cnt;
967 length -= cnt;
968 }
969
970 new_bb = ROUND_UP(length, BBM_LOG_MAX_LBA_ENTRY_VAL) /
971 BBM_LOG_MAX_LBA_ENTRY_VAL;
972 if (log->entry_count + new_bb > BBM_LOG_MAX_ENTRIES)
973 return 0;
974
975 while (length > 0) {
976 int cnt = (length <= BBM_LOG_MAX_LBA_ENTRY_VAL) ? length :
977 BBM_LOG_MAX_LBA_ENTRY_VAL;
978 struct bbm_log_entry *entry =
979 &log->marked_block_entries[log->entry_count];
980
981 entry->defective_block_start = __cpu_to_le48(sector);
982 entry->marked_count = cnt - 1;
983 entry->disk_ordinal = idx;
984
985 sector += cnt;
986 length -= cnt;
987
988 log->entry_count++;
989 }
990
991 return new_bb;
992 }
993
994 /* clear all bad blocks for given disk */
995 static void clear_disk_badblocks(struct bbm_log *log, const __u8 idx)
996 {
997 __u32 i = 0;
998
999 while (i < log->entry_count) {
1000 struct bbm_log_entry *entries = log->marked_block_entries;
1001
1002 if (entries[i].disk_ordinal == idx) {
1003 if (i < log->entry_count - 1)
1004 entries[i] = entries[log->entry_count - 1];
1005 log->entry_count--;
1006 } else {
1007 i++;
1008 }
1009 }
1010 }
1011
1012 /* clear given bad block */
1013 static int clear_badblock(struct bbm_log *log, const __u8 idx, const unsigned
1014 long long sector, const int length) {
1015 __u32 i = 0;
1016
1017 while (i < log->entry_count) {
1018 struct bbm_log_entry *entries = log->marked_block_entries;
1019
1020 if ((entries[i].disk_ordinal == idx) &&
1021 (__le48_to_cpu(&entries[i].defective_block_start) ==
1022 sector) && (entries[i].marked_count + 1 == length)) {
1023 if (i < log->entry_count - 1)
1024 entries[i] = entries[log->entry_count - 1];
1025 log->entry_count--;
1026 break;
1027 }
1028 i++;
1029 }
1030
1031 return 1;
1032 }
1033
1034 /* allocate and load BBM log from metadata */
1035 static int load_bbm_log(struct intel_super *super)
1036 {
1037 struct imsm_super *mpb = super->anchor;
1038 __u32 bbm_log_size = __le32_to_cpu(mpb->bbm_log_size);
1039
1040 super->bbm_log = xcalloc(1, sizeof(struct bbm_log));
1041 if (!super->bbm_log)
1042 return 1;
1043
1044 if (bbm_log_size) {
1045 struct bbm_log *log = (void *)mpb +
1046 __le32_to_cpu(mpb->mpb_size) - bbm_log_size;
1047
1048 __u32 entry_count;
1049
1050 if (bbm_log_size < sizeof(log->signature) +
1051 sizeof(log->entry_count))
1052 return 2;
1053
1054 entry_count = __le32_to_cpu(log->entry_count);
1055 if ((__le32_to_cpu(log->signature) != BBM_LOG_SIGNATURE) ||
1056 (entry_count > BBM_LOG_MAX_ENTRIES))
1057 return 3;
1058
1059 if (bbm_log_size !=
1060 sizeof(log->signature) + sizeof(log->entry_count) +
1061 entry_count * sizeof(struct bbm_log_entry))
1062 return 4;
1063
1064 memcpy(super->bbm_log, log, bbm_log_size);
1065 } else {
1066 super->bbm_log->signature = __cpu_to_le32(BBM_LOG_SIGNATURE);
1067 super->bbm_log->entry_count = 0;
1068 }
1069
1070 return 0;
1071 }
1072
1073 /* checks if bad block is within volume boundaries */
1074 static int is_bad_block_in_volume(const struct bbm_log_entry *entry,
1075 const unsigned long long start_sector,
1076 const unsigned long long size)
1077 {
1078 unsigned long long bb_start;
1079 unsigned long long bb_end;
1080
1081 bb_start = __le48_to_cpu(&entry->defective_block_start);
1082 bb_end = bb_start + (entry->marked_count + 1);
1083
1084 if (((bb_start >= start_sector) && (bb_start < start_sector + size)) ||
1085 ((bb_end >= start_sector) && (bb_end <= start_sector + size)))
1086 return 1;
1087
1088 return 0;
1089 }
1090
1091 /* get list of bad blocks on a drive for a volume */
1092 static void get_volume_badblocks(const struct bbm_log *log, const __u8 idx,
1093 const unsigned long long start_sector,
1094 const unsigned long long size,
1095 struct md_bb *bbs)
1096 {
1097 __u32 count = 0;
1098 __u32 i;
1099
1100 for (i = 0; i < log->entry_count; i++) {
1101 const struct bbm_log_entry *ent =
1102 &log->marked_block_entries[i];
1103 struct md_bb_entry *bb;
1104
1105 if ((ent->disk_ordinal == idx) &&
1106 is_bad_block_in_volume(ent, start_sector, size)) {
1107
1108 if (!bbs->entries) {
1109 bbs->entries = xmalloc(BBM_LOG_MAX_ENTRIES *
1110 sizeof(*bb));
1111 if (!bbs->entries)
1112 break;
1113 }
1114
1115 bb = &bbs->entries[count++];
1116 bb->sector = __le48_to_cpu(&ent->defective_block_start);
1117 bb->length = ent->marked_count + 1;
1118 }
1119 }
1120 bbs->count = count;
1121 }
1122
1123 /*
1124 * for second_map:
1125 * == MAP_0 get first map
1126 * == MAP_1 get second map
1127 * == MAP_X than get map according to the current migr_state
1128 */
1129 static __u32 get_imsm_ord_tbl_ent(struct imsm_dev *dev,
1130 int slot,
1131 int second_map)
1132 {
1133 struct imsm_map *map;
1134
1135 map = get_imsm_map(dev, second_map);
1136
1137 /* top byte identifies disk under rebuild */
1138 return __le32_to_cpu(map->disk_ord_tbl[slot]);
1139 }
1140
1141 #define ord_to_idx(ord) (((ord) << 8) >> 8)
1142 static __u32 get_imsm_disk_idx(struct imsm_dev *dev, int slot, int second_map)
1143 {
1144 __u32 ord = get_imsm_ord_tbl_ent(dev, slot, second_map);
1145
1146 return ord_to_idx(ord);
1147 }
1148
1149 static void set_imsm_ord_tbl_ent(struct imsm_map *map, int slot, __u32 ord)
1150 {
1151 map->disk_ord_tbl[slot] = __cpu_to_le32(ord);
1152 }
1153
1154 static int get_imsm_disk_slot(struct imsm_map *map, unsigned idx)
1155 {
1156 int slot;
1157 __u32 ord;
1158
1159 for (slot = 0; slot < map->num_members; slot++) {
1160 ord = __le32_to_cpu(map->disk_ord_tbl[slot]);
1161 if (ord_to_idx(ord) == idx)
1162 return slot;
1163 }
1164
1165 return -1;
1166 }
1167
1168 static int get_imsm_raid_level(struct imsm_map *map)
1169 {
1170 if (map->raid_level == 1) {
1171 if (map->num_members == 2)
1172 return 1;
1173 else
1174 return 10;
1175 }
1176
1177 return map->raid_level;
1178 }
1179
1180 static int cmp_extent(const void *av, const void *bv)
1181 {
1182 const struct extent *a = av;
1183 const struct extent *b = bv;
1184 if (a->start < b->start)
1185 return -1;
1186 if (a->start > b->start)
1187 return 1;
1188 return 0;
1189 }
1190
1191 static int count_memberships(struct dl *dl, struct intel_super *super)
1192 {
1193 int memberships = 0;
1194 int i;
1195
1196 for (i = 0; i < super->anchor->num_raid_devs; i++) {
1197 struct imsm_dev *dev = get_imsm_dev(super, i);
1198 struct imsm_map *map = get_imsm_map(dev, MAP_0);
1199
1200 if (get_imsm_disk_slot(map, dl->index) >= 0)
1201 memberships++;
1202 }
1203
1204 return memberships;
1205 }
1206
1207 static __u32 imsm_min_reserved_sectors(struct intel_super *super);
1208
1209 static int split_ull(unsigned long long n, void *lo, void *hi)
1210 {
1211 if (lo == 0 || hi == 0)
1212 return 1;
1213 __put_unaligned32(__cpu_to_le32((__u32)n), lo);
1214 __put_unaligned32(__cpu_to_le32((n >> 32)), hi);
1215 return 0;
1216 }
1217
1218 static unsigned long long join_u32(__u32 lo, __u32 hi)
1219 {
1220 return (unsigned long long)__le32_to_cpu(lo) |
1221 (((unsigned long long)__le32_to_cpu(hi)) << 32);
1222 }
1223
1224 static unsigned long long total_blocks(struct imsm_disk *disk)
1225 {
1226 if (disk == NULL)
1227 return 0;
1228 return join_u32(disk->total_blocks_lo, disk->total_blocks_hi);
1229 }
1230
1231 /**
1232 * imsm_num_data_members() - get data drives count for an array.
1233 * @map: Map to analyze.
1234 *
1235 * num_data_members value represents minimal count of drives for level.
1236 * The name of the property could be misleading for RAID5 with asymmetric layout
1237 * because some data required to be calculated from parity.
1238 * The property is extracted from level and num_members value.
1239 *
1240 * Return: num_data_members value on success, zero otherwise.
1241 */
1242 static __u8 imsm_num_data_members(struct imsm_map *map)
1243 {
1244 switch (get_imsm_raid_level(map)) {
1245 case 0:
1246 return map->num_members;
1247 case 1:
1248 case 10:
1249 return map->num_members / 2;
1250 case 5:
1251 return map->num_members - 1;
1252 default:
1253 dprintf("unsupported raid level\n");
1254 return 0;
1255 }
1256 }
1257
1258 static unsigned long long pba_of_lba0(struct imsm_map *map)
1259 {
1260 if (map == NULL)
1261 return 0;
1262 return join_u32(map->pba_of_lba0_lo, map->pba_of_lba0_hi);
1263 }
1264
1265 static unsigned long long blocks_per_member(struct imsm_map *map)
1266 {
1267 if (map == NULL)
1268 return 0;
1269 return join_u32(map->blocks_per_member_lo, map->blocks_per_member_hi);
1270 }
1271
1272 static unsigned long long num_data_stripes(struct imsm_map *map)
1273 {
1274 if (map == NULL)
1275 return 0;
1276 return join_u32(map->num_data_stripes_lo, map->num_data_stripes_hi);
1277 }
1278
1279 static unsigned long long vol_curr_migr_unit(struct imsm_dev *dev)
1280 {
1281 if (dev == NULL)
1282 return 0;
1283
1284 return join_u32(dev->vol.curr_migr_unit_lo, dev->vol.curr_migr_unit_hi);
1285 }
1286
1287 static unsigned long long imsm_dev_size(struct imsm_dev *dev)
1288 {
1289 if (dev == NULL)
1290 return 0;
1291 return join_u32(dev->size_low, dev->size_high);
1292 }
1293
1294 static unsigned long long migr_chkp_area_pba(struct migr_record *migr_rec)
1295 {
1296 if (migr_rec == NULL)
1297 return 0;
1298 return join_u32(migr_rec->ckpt_area_pba_lo,
1299 migr_rec->ckpt_area_pba_hi);
1300 }
1301
1302 static unsigned long long current_migr_unit(struct migr_record *migr_rec)
1303 {
1304 if (migr_rec == NULL)
1305 return 0;
1306 return join_u32(migr_rec->curr_migr_unit_lo,
1307 migr_rec->curr_migr_unit_hi);
1308 }
1309
1310 static unsigned long long migr_dest_1st_member_lba(struct migr_record *migr_rec)
1311 {
1312 if (migr_rec == NULL)
1313 return 0;
1314 return join_u32(migr_rec->dest_1st_member_lba_lo,
1315 migr_rec->dest_1st_member_lba_hi);
1316 }
1317
1318 static unsigned long long get_num_migr_units(struct migr_record *migr_rec)
1319 {
1320 if (migr_rec == NULL)
1321 return 0;
1322 return join_u32(migr_rec->num_migr_units_lo,
1323 migr_rec->num_migr_units_hi);
1324 }
1325
1326 static void set_total_blocks(struct imsm_disk *disk, unsigned long long n)
1327 {
1328 split_ull(n, &disk->total_blocks_lo, &disk->total_blocks_hi);
1329 }
1330
1331 /**
1332 * set_num_domains() - Set number of domains for an array.
1333 * @map: Map to be updated.
1334 *
1335 * num_domains property represents copies count of each data drive, thus make
1336 * it meaningful only for RAID1 and RAID10. IMSM supports two domains for
1337 * raid1 and raid10.
1338 */
1339 static void set_num_domains(struct imsm_map *map)
1340 {
1341 int level = get_imsm_raid_level(map);
1342
1343 if (level == 1 || level == 10)
1344 map->num_domains = 2;
1345 else
1346 map->num_domains = 1;
1347 }
1348
1349 static void set_pba_of_lba0(struct imsm_map *map, unsigned long long n)
1350 {
1351 split_ull(n, &map->pba_of_lba0_lo, &map->pba_of_lba0_hi);
1352 }
1353
1354 static void set_blocks_per_member(struct imsm_map *map, unsigned long long n)
1355 {
1356 split_ull(n, &map->blocks_per_member_lo, &map->blocks_per_member_hi);
1357 }
1358
1359 static void set_num_data_stripes(struct imsm_map *map, unsigned long long n)
1360 {
1361 split_ull(n, &map->num_data_stripes_lo, &map->num_data_stripes_hi);
1362 }
1363
1364 /**
1365 * update_num_data_stripes() - Calculate and update num_data_stripes value.
1366 * @map: map to be updated.
1367 * @dev_size: size of volume.
1368 *
1369 * num_data_stripes value is addictionally divided by num_domains, therefore for
1370 * levels where num_domains is not 1, nds is a part of real value.
1371 */
1372 static void update_num_data_stripes(struct imsm_map *map,
1373 unsigned long long dev_size)
1374 {
1375 unsigned long long nds = dev_size / imsm_num_data_members(map);
1376
1377 nds /= map->num_domains;
1378 nds /= map->blocks_per_strip;
1379 set_num_data_stripes(map, nds);
1380 }
1381
1382 static void set_vol_curr_migr_unit(struct imsm_dev *dev, unsigned long long n)
1383 {
1384 if (dev == NULL)
1385 return;
1386
1387 split_ull(n, &dev->vol.curr_migr_unit_lo, &dev->vol.curr_migr_unit_hi);
1388 }
1389
1390 static void set_imsm_dev_size(struct imsm_dev *dev, unsigned long long n)
1391 {
1392 split_ull(n, &dev->size_low, &dev->size_high);
1393 }
1394
1395 static void set_migr_chkp_area_pba(struct migr_record *migr_rec,
1396 unsigned long long n)
1397 {
1398 split_ull(n, &migr_rec->ckpt_area_pba_lo, &migr_rec->ckpt_area_pba_hi);
1399 }
1400
1401 static void set_current_migr_unit(struct migr_record *migr_rec,
1402 unsigned long long n)
1403 {
1404 split_ull(n, &migr_rec->curr_migr_unit_lo,
1405 &migr_rec->curr_migr_unit_hi);
1406 }
1407
1408 static void set_migr_dest_1st_member_lba(struct migr_record *migr_rec,
1409 unsigned long long n)
1410 {
1411 split_ull(n, &migr_rec->dest_1st_member_lba_lo,
1412 &migr_rec->dest_1st_member_lba_hi);
1413 }
1414
1415 static void set_num_migr_units(struct migr_record *migr_rec,
1416 unsigned long long n)
1417 {
1418 split_ull(n, &migr_rec->num_migr_units_lo,
1419 &migr_rec->num_migr_units_hi);
1420 }
1421
1422 static unsigned long long per_dev_array_size(struct imsm_map *map)
1423 {
1424 unsigned long long array_size = 0;
1425
1426 if (map == NULL)
1427 return array_size;
1428
1429 array_size = num_data_stripes(map) * map->blocks_per_strip;
1430 if (get_imsm_raid_level(map) == 1 || get_imsm_raid_level(map) == 10)
1431 array_size *= 2;
1432
1433 return array_size;
1434 }
1435
1436 static struct extent *get_extents(struct intel_super *super, struct dl *dl,
1437 int get_minimal_reservation)
1438 {
1439 /* find a list of used extents on the given physical device */
1440 struct extent *rv, *e;
1441 int i;
1442 int memberships = count_memberships(dl, super);
1443 __u32 reservation;
1444
1445 /* trim the reserved area for spares, so they can join any array
1446 * regardless of whether the OROM has assigned sectors from the
1447 * IMSM_RESERVED_SECTORS region
1448 */
1449 if (dl->index == -1 || get_minimal_reservation)
1450 reservation = imsm_min_reserved_sectors(super);
1451 else
1452 reservation = MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS;
1453
1454 rv = xcalloc(sizeof(struct extent), (memberships + 1));
1455 e = rv;
1456
1457 for (i = 0; i < super->anchor->num_raid_devs; i++) {
1458 struct imsm_dev *dev = get_imsm_dev(super, i);
1459 struct imsm_map *map = get_imsm_map(dev, MAP_0);
1460
1461 if (get_imsm_disk_slot(map, dl->index) >= 0) {
1462 e->start = pba_of_lba0(map);
1463 e->size = per_dev_array_size(map);
1464 e++;
1465 }
1466 }
1467 qsort(rv, memberships, sizeof(*rv), cmp_extent);
1468
1469 /* determine the start of the metadata
1470 * when no raid devices are defined use the default
1471 * ...otherwise allow the metadata to truncate the value
1472 * as is the case with older versions of imsm
1473 */
1474 if (memberships) {
1475 struct extent *last = &rv[memberships - 1];
1476 unsigned long long remainder;
1477
1478 remainder = total_blocks(&dl->disk) - (last->start + last->size);
1479 /* round down to 1k block to satisfy precision of the kernel
1480 * 'size' interface
1481 */
1482 remainder &= ~1UL;
1483 /* make sure remainder is still sane */
1484 if (remainder < (unsigned)ROUND_UP(super->len, 512) >> 9)
1485 remainder = ROUND_UP(super->len, 512) >> 9;
1486 if (reservation > remainder)
1487 reservation = remainder;
1488 }
1489 e->start = total_blocks(&dl->disk) - reservation;
1490 e->size = 0;
1491 return rv;
1492 }
1493
1494 /* try to determine how much space is reserved for metadata from
1495 * the last get_extents() entry, otherwise fallback to the
1496 * default
1497 */
1498 static __u32 imsm_reserved_sectors(struct intel_super *super, struct dl *dl)
1499 {
1500 struct extent *e;
1501 int i;
1502 __u32 rv;
1503
1504 /* for spares just return a minimal reservation which will grow
1505 * once the spare is picked up by an array
1506 */
1507 if (dl->index == -1)
1508 return MPB_SECTOR_CNT;
1509
1510 e = get_extents(super, dl, 0);
1511 if (!e)
1512 return MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS;
1513
1514 /* scroll to last entry */
1515 for (i = 0; e[i].size; i++)
1516 continue;
1517
1518 rv = total_blocks(&dl->disk) - e[i].start;
1519
1520 free(e);
1521
1522 return rv;
1523 }
1524
1525 static int is_spare(struct imsm_disk *disk)
1526 {
1527 return (disk->status & SPARE_DISK) == SPARE_DISK;
1528 }
1529
1530 static int is_configured(struct imsm_disk *disk)
1531 {
1532 return (disk->status & CONFIGURED_DISK) == CONFIGURED_DISK;
1533 }
1534
1535 static int is_failed(struct imsm_disk *disk)
1536 {
1537 return (disk->status & FAILED_DISK) == FAILED_DISK;
1538 }
1539
1540 static int is_journal(struct imsm_disk *disk)
1541 {
1542 return (disk->status & JOURNAL_DISK) == JOURNAL_DISK;
1543 }
1544
1545 /* round array size down to closest MB and ensure it splits evenly
1546 * between members
1547 */
1548 static unsigned long long round_size_to_mb(unsigned long long size, unsigned int
1549 disk_count)
1550 {
1551 size /= disk_count;
1552 size = (size >> SECT_PER_MB_SHIFT) << SECT_PER_MB_SHIFT;
1553 size *= disk_count;
1554
1555 return size;
1556 }
1557
1558 static int able_to_resync(int raid_level, int missing_disks)
1559 {
1560 int max_missing_disks = 0;
1561
1562 switch (raid_level) {
1563 case 10:
1564 max_missing_disks = 1;
1565 break;
1566 default:
1567 max_missing_disks = 0;
1568 }
1569 return missing_disks <= max_missing_disks;
1570 }
1571
1572 /* try to determine how much space is reserved for metadata from
1573 * the last get_extents() entry on the smallest active disk,
1574 * otherwise fallback to the default
1575 */
1576 static __u32 imsm_min_reserved_sectors(struct intel_super *super)
1577 {
1578 struct extent *e;
1579 int i;
1580 unsigned long long min_active;
1581 __u32 remainder;
1582 __u32 rv = MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS;
1583 struct dl *dl, *dl_min = NULL;
1584
1585 if (!super)
1586 return rv;
1587
1588 min_active = 0;
1589 for (dl = super->disks; dl; dl = dl->next) {
1590 if (dl->index < 0)
1591 continue;
1592 unsigned long long blocks = total_blocks(&dl->disk);
1593 if (blocks < min_active || min_active == 0) {
1594 dl_min = dl;
1595 min_active = blocks;
1596 }
1597 }
1598 if (!dl_min)
1599 return rv;
1600
1601 /* find last lba used by subarrays on the smallest active disk */
1602 e = get_extents(super, dl_min, 0);
1603 if (!e)
1604 return rv;
1605 for (i = 0; e[i].size; i++)
1606 continue;
1607
1608 remainder = min_active - e[i].start;
1609 free(e);
1610
1611 /* to give priority to recovery we should not require full
1612 IMSM_RESERVED_SECTORS from the spare */
1613 rv = MPB_SECTOR_CNT + NUM_BLOCKS_DIRTY_STRIPE_REGION;
1614
1615 /* if real reservation is smaller use that value */
1616 return (remainder < rv) ? remainder : rv;
1617 }
1618
1619 /*
1620 * Return minimum size of a spare and sector size
1621 * that can be used in this array
1622 */
1623 int get_spare_criteria_imsm(struct supertype *st, struct spare_criteria *c)
1624 {
1625 struct intel_super *super = st->sb;
1626 struct dl *dl;
1627 struct extent *e;
1628 int i;
1629 unsigned long long size = 0;
1630
1631 c->min_size = 0;
1632 c->sector_size = 0;
1633
1634 if (!super)
1635 return -EINVAL;
1636 /* find first active disk in array */
1637 dl = super->disks;
1638 while (dl && (is_failed(&dl->disk) || dl->index == -1))
1639 dl = dl->next;
1640 if (!dl)
1641 return -EINVAL;
1642 /* find last lba used by subarrays */
1643 e = get_extents(super, dl, 0);
1644 if (!e)
1645 return -EINVAL;
1646 for (i = 0; e[i].size; i++)
1647 continue;
1648 if (i > 0)
1649 size = e[i-1].start + e[i-1].size;
1650 free(e);
1651
1652 /* add the amount of space needed for metadata */
1653 size += imsm_min_reserved_sectors(super);
1654
1655 c->min_size = size * 512;
1656 c->sector_size = super->sector_size;
1657
1658 return 0;
1659 }
1660
1661 static bool is_gen_migration(struct imsm_dev *dev);
1662
1663 #define IMSM_4K_DIV 8
1664
1665 static __u64 blocks_per_migr_unit(struct intel_super *super,
1666 struct imsm_dev *dev);
1667
1668 static void print_imsm_dev(struct intel_super *super,
1669 struct imsm_dev *dev,
1670 char *uuid,
1671 int disk_idx)
1672 {
1673 __u64 sz;
1674 int slot, i;
1675 struct imsm_map *map = get_imsm_map(dev, MAP_0);
1676 struct imsm_map *map2 = get_imsm_map(dev, MAP_1);
1677 __u32 ord;
1678
1679 printf("\n");
1680 printf("[%.16s]:\n", dev->volume);
1681 printf(" Subarray : %d\n", super->current_vol);
1682 printf(" UUID : %s\n", uuid);
1683 printf(" RAID Level : %d", get_imsm_raid_level(map));
1684 if (map2)
1685 printf(" <-- %d", get_imsm_raid_level(map2));
1686 printf("\n");
1687 printf(" Members : %d", map->num_members);
1688 if (map2)
1689 printf(" <-- %d", map2->num_members);
1690 printf("\n");
1691 printf(" Slots : [");
1692 for (i = 0; i < map->num_members; i++) {
1693 ord = get_imsm_ord_tbl_ent(dev, i, MAP_0);
1694 printf("%s", ord & IMSM_ORD_REBUILD ? "_" : "U");
1695 }
1696 printf("]");
1697 if (map2) {
1698 printf(" <-- [");
1699 for (i = 0; i < map2->num_members; i++) {
1700 ord = get_imsm_ord_tbl_ent(dev, i, MAP_1);
1701 printf("%s", ord & IMSM_ORD_REBUILD ? "_" : "U");
1702 }
1703 printf("]");
1704 }
1705 printf("\n");
1706 printf(" Failed disk : ");
1707 if (map->failed_disk_num == 0xff)
1708 printf("none");
1709 else
1710 printf("%i", map->failed_disk_num);
1711 printf("\n");
1712 slot = get_imsm_disk_slot(map, disk_idx);
1713 if (slot >= 0) {
1714 ord = get_imsm_ord_tbl_ent(dev, slot, MAP_X);
1715 printf(" This Slot : %d%s\n", slot,
1716 ord & IMSM_ORD_REBUILD ? " (out-of-sync)" : "");
1717 } else
1718 printf(" This Slot : ?\n");
1719 printf(" Sector Size : %u\n", super->sector_size);
1720 sz = imsm_dev_size(dev);
1721 printf(" Array Size : %llu%s\n",
1722 (unsigned long long)sz * 512 / super->sector_size,
1723 human_size(sz * 512));
1724 sz = blocks_per_member(map);
1725 printf(" Per Dev Size : %llu%s\n",
1726 (unsigned long long)sz * 512 / super->sector_size,
1727 human_size(sz * 512));
1728 printf(" Sector Offset : %llu\n",
1729 pba_of_lba0(map) * 512 / super->sector_size);
1730 printf(" Num Stripes : %llu\n",
1731 num_data_stripes(map));
1732 printf(" Chunk Size : %u KiB",
1733 __le16_to_cpu(map->blocks_per_strip) / 2);
1734 if (map2)
1735 printf(" <-- %u KiB",
1736 __le16_to_cpu(map2->blocks_per_strip) / 2);
1737 printf("\n");
1738 printf(" Reserved : %d\n", __le32_to_cpu(dev->reserved_blocks));
1739 printf(" Migrate State : ");
1740 if (dev->vol.migr_state) {
1741 if (migr_type(dev) == MIGR_INIT)
1742 printf("initialize\n");
1743 else if (migr_type(dev) == MIGR_REBUILD)
1744 printf("rebuild\n");
1745 else if (migr_type(dev) == MIGR_VERIFY)
1746 printf("check\n");
1747 else if (migr_type(dev) == MIGR_GEN_MIGR)
1748 printf("general migration\n");
1749 else if (migr_type(dev) == MIGR_STATE_CHANGE)
1750 printf("state change\n");
1751 else if (migr_type(dev) == MIGR_REPAIR)
1752 printf("repair\n");
1753 else
1754 printf("<unknown:%d>\n", migr_type(dev));
1755 } else
1756 printf("idle\n");
1757 printf(" Map State : %s", map_state_str[map->map_state]);
1758 if (dev->vol.migr_state) {
1759 struct imsm_map *map = get_imsm_map(dev, MAP_1);
1760
1761 printf(" <-- %s", map_state_str[map->map_state]);
1762 printf("\n Checkpoint : %llu ", vol_curr_migr_unit(dev));
1763 if (is_gen_migration(dev) && (slot > 1 || slot < 0))
1764 printf("(N/A)");
1765 else
1766 printf("(%llu)", (unsigned long long)
1767 blocks_per_migr_unit(super, dev));
1768 }
1769 printf("\n");
1770 printf(" Dirty State : %s\n", (dev->vol.dirty & RAIDVOL_DIRTY) ?
1771 "dirty" : "clean");
1772 printf(" RWH Policy : ");
1773 if (dev->rwh_policy == RWH_OFF || dev->rwh_policy == RWH_MULTIPLE_OFF)
1774 printf("off\n");
1775 else if (dev->rwh_policy == RWH_DISTRIBUTED)
1776 printf("PPL distributed\n");
1777 else if (dev->rwh_policy == RWH_JOURNALING_DRIVE)
1778 printf("PPL journaling drive\n");
1779 else if (dev->rwh_policy == RWH_MULTIPLE_DISTRIBUTED)
1780 printf("Multiple distributed PPLs\n");
1781 else if (dev->rwh_policy == RWH_MULTIPLE_PPLS_JOURNALING_DRIVE)
1782 printf("Multiple PPLs on journaling drive\n");
1783 else if (dev->rwh_policy == RWH_BITMAP)
1784 printf("Write-intent bitmap\n");
1785 else
1786 printf("<unknown:%d>\n", dev->rwh_policy);
1787
1788 printf(" Volume ID : %u\n", dev->my_vol_raid_dev_num);
1789 }
1790
1791 static void print_imsm_disk(struct imsm_disk *disk,
1792 int index,
1793 __u32 reserved,
1794 unsigned int sector_size) {
1795 char str[MAX_RAID_SERIAL_LEN + 1];
1796 __u64 sz;
1797
1798 if (index < -1 || !disk)
1799 return;
1800
1801 printf("\n");
1802 snprintf(str, MAX_RAID_SERIAL_LEN + 1, "%s", disk->serial);
1803 if (index >= 0)
1804 printf(" Disk%02d Serial : %s\n", index, str);
1805 else
1806 printf(" Disk Serial : %s\n", str);
1807 printf(" State :%s%s%s%s\n", is_spare(disk) ? " spare" : "",
1808 is_configured(disk) ? " active" : "",
1809 is_failed(disk) ? " failed" : "",
1810 is_journal(disk) ? " journal" : "");
1811 printf(" Id : %08x\n", __le32_to_cpu(disk->scsi_id));
1812 sz = total_blocks(disk) - reserved;
1813 printf(" Usable Size : %llu%s\n",
1814 (unsigned long long)sz * 512 / sector_size,
1815 human_size(sz * 512));
1816 }
1817
1818 void convert_to_4k_imsm_migr_rec(struct intel_super *super)
1819 {
1820 struct migr_record *migr_rec = super->migr_rec;
1821
1822 migr_rec->blocks_per_unit /= IMSM_4K_DIV;
1823 migr_rec->dest_depth_per_unit /= IMSM_4K_DIV;
1824 split_ull((join_u32(migr_rec->post_migr_vol_cap,
1825 migr_rec->post_migr_vol_cap_hi) / IMSM_4K_DIV),
1826 &migr_rec->post_migr_vol_cap, &migr_rec->post_migr_vol_cap_hi);
1827 set_migr_chkp_area_pba(migr_rec,
1828 migr_chkp_area_pba(migr_rec) / IMSM_4K_DIV);
1829 set_migr_dest_1st_member_lba(migr_rec,
1830 migr_dest_1st_member_lba(migr_rec) / IMSM_4K_DIV);
1831 }
1832
1833 void convert_to_4k_imsm_disk(struct imsm_disk *disk)
1834 {
1835 set_total_blocks(disk, (total_blocks(disk)/IMSM_4K_DIV));
1836 }
1837
1838 void convert_to_4k(struct intel_super *super)
1839 {
1840 struct imsm_super *mpb = super->anchor;
1841 struct imsm_disk *disk;
1842 int i;
1843 __u32 bbm_log_size = __le32_to_cpu(mpb->bbm_log_size);
1844
1845 for (i = 0; i < mpb->num_disks ; i++) {
1846 disk = __get_imsm_disk(mpb, i);
1847 /* disk */
1848 convert_to_4k_imsm_disk(disk);
1849 }
1850 for (i = 0; i < mpb->num_raid_devs; i++) {
1851 struct imsm_dev *dev = __get_imsm_dev(mpb, i);
1852 struct imsm_map *map = get_imsm_map(dev, MAP_0);
1853 /* dev */
1854 set_imsm_dev_size(dev, imsm_dev_size(dev)/IMSM_4K_DIV);
1855 set_vol_curr_migr_unit(dev,
1856 vol_curr_migr_unit(dev) / IMSM_4K_DIV);
1857
1858 /* map0 */
1859 set_blocks_per_member(map, blocks_per_member(map)/IMSM_4K_DIV);
1860 map->blocks_per_strip /= IMSM_4K_DIV;
1861 set_pba_of_lba0(map, pba_of_lba0(map)/IMSM_4K_DIV);
1862
1863 if (dev->vol.migr_state) {
1864 /* map1 */
1865 map = get_imsm_map(dev, MAP_1);
1866 set_blocks_per_member(map,
1867 blocks_per_member(map)/IMSM_4K_DIV);
1868 map->blocks_per_strip /= IMSM_4K_DIV;
1869 set_pba_of_lba0(map, pba_of_lba0(map)/IMSM_4K_DIV);
1870 }
1871 }
1872 if (bbm_log_size) {
1873 struct bbm_log *log = (void *)mpb +
1874 __le32_to_cpu(mpb->mpb_size) - bbm_log_size;
1875 __u32 i;
1876
1877 for (i = 0; i < log->entry_count; i++) {
1878 struct bbm_log_entry *entry =
1879 &log->marked_block_entries[i];
1880
1881 __u8 count = entry->marked_count + 1;
1882 unsigned long long sector =
1883 __le48_to_cpu(&entry->defective_block_start);
1884
1885 entry->defective_block_start =
1886 __cpu_to_le48(sector/IMSM_4K_DIV);
1887 entry->marked_count = max(count/IMSM_4K_DIV, 1) - 1;
1888 }
1889 }
1890
1891 mpb->check_sum = __gen_imsm_checksum(mpb);
1892 }
1893
1894 void examine_migr_rec_imsm(struct intel_super *super)
1895 {
1896 struct migr_record *migr_rec = super->migr_rec;
1897 struct imsm_super *mpb = super->anchor;
1898 int i;
1899
1900 for (i = 0; i < mpb->num_raid_devs; i++) {
1901 struct imsm_dev *dev = __get_imsm_dev(mpb, i);
1902 struct imsm_map *map;
1903 int slot = -1;
1904
1905 if (is_gen_migration(dev) == false)
1906 continue;
1907
1908 printf("\nMigration Record Information:");
1909
1910 /* first map under migration */
1911 map = get_imsm_map(dev, MAP_0);
1912 if (map)
1913 slot = get_imsm_disk_slot(map, super->disks->index);
1914 if (map == NULL || slot > 1 || slot < 0) {
1915 printf(" Empty\n ");
1916 printf("Examine one of first two disks in array\n");
1917 break;
1918 }
1919 printf("\n Status : ");
1920 if (__le32_to_cpu(migr_rec->rec_status) == UNIT_SRC_NORMAL)
1921 printf("Normal\n");
1922 else
1923 printf("Contains Data\n");
1924 printf(" Current Unit : %llu\n",
1925 current_migr_unit(migr_rec));
1926 printf(" Family : %u\n",
1927 __le32_to_cpu(migr_rec->family_num));
1928 printf(" Ascending : %u\n",
1929 __le32_to_cpu(migr_rec->ascending_migr));
1930 printf(" Blocks Per Unit : %u\n",
1931 __le32_to_cpu(migr_rec->blocks_per_unit));
1932 printf(" Dest. Depth Per Unit : %u\n",
1933 __le32_to_cpu(migr_rec->dest_depth_per_unit));
1934 printf(" Checkpoint Area pba : %llu\n",
1935 migr_chkp_area_pba(migr_rec));
1936 printf(" First member lba : %llu\n",
1937 migr_dest_1st_member_lba(migr_rec));
1938 printf(" Total Number of Units : %llu\n",
1939 get_num_migr_units(migr_rec));
1940 printf(" Size of volume : %llu\n",
1941 join_u32(migr_rec->post_migr_vol_cap,
1942 migr_rec->post_migr_vol_cap_hi));
1943 printf(" Record was read from : %u\n",
1944 __le32_to_cpu(migr_rec->ckpt_read_disk_num));
1945
1946 break;
1947 }
1948 }
1949
1950 void convert_from_4k_imsm_migr_rec(struct intel_super *super)
1951 {
1952 struct migr_record *migr_rec = super->migr_rec;
1953
1954 migr_rec->blocks_per_unit *= IMSM_4K_DIV;
1955 migr_rec->dest_depth_per_unit *= IMSM_4K_DIV;
1956 split_ull((join_u32(migr_rec->post_migr_vol_cap,
1957 migr_rec->post_migr_vol_cap_hi) * IMSM_4K_DIV),
1958 &migr_rec->post_migr_vol_cap,
1959 &migr_rec->post_migr_vol_cap_hi);
1960 set_migr_chkp_area_pba(migr_rec,
1961 migr_chkp_area_pba(migr_rec) * IMSM_4K_DIV);
1962 set_migr_dest_1st_member_lba(migr_rec,
1963 migr_dest_1st_member_lba(migr_rec) * IMSM_4K_DIV);
1964 }
1965
1966 void convert_from_4k(struct intel_super *super)
1967 {
1968 struct imsm_super *mpb = super->anchor;
1969 struct imsm_disk *disk;
1970 int i;
1971 __u32 bbm_log_size = __le32_to_cpu(mpb->bbm_log_size);
1972
1973 for (i = 0; i < mpb->num_disks ; i++) {
1974 disk = __get_imsm_disk(mpb, i);
1975 /* disk */
1976 set_total_blocks(disk, (total_blocks(disk)*IMSM_4K_DIV));
1977 }
1978
1979 for (i = 0; i < mpb->num_raid_devs; i++) {
1980 struct imsm_dev *dev = __get_imsm_dev(mpb, i);
1981 struct imsm_map *map = get_imsm_map(dev, MAP_0);
1982 /* dev */
1983 set_imsm_dev_size(dev, imsm_dev_size(dev)*IMSM_4K_DIV);
1984 set_vol_curr_migr_unit(dev,
1985 vol_curr_migr_unit(dev) * IMSM_4K_DIV);
1986
1987 /* map0 */
1988 set_blocks_per_member(map, blocks_per_member(map)*IMSM_4K_DIV);
1989 map->blocks_per_strip *= IMSM_4K_DIV;
1990 set_pba_of_lba0(map, pba_of_lba0(map)*IMSM_4K_DIV);
1991
1992 if (dev->vol.migr_state) {
1993 /* map1 */
1994 map = get_imsm_map(dev, MAP_1);
1995 set_blocks_per_member(map,
1996 blocks_per_member(map)*IMSM_4K_DIV);
1997 map->blocks_per_strip *= IMSM_4K_DIV;
1998 set_pba_of_lba0(map, pba_of_lba0(map)*IMSM_4K_DIV);
1999 }
2000 }
2001 if (bbm_log_size) {
2002 struct bbm_log *log = (void *)mpb +
2003 __le32_to_cpu(mpb->mpb_size) - bbm_log_size;
2004 __u32 i;
2005
2006 for (i = 0; i < log->entry_count; i++) {
2007 struct bbm_log_entry *entry =
2008 &log->marked_block_entries[i];
2009
2010 __u8 count = entry->marked_count + 1;
2011 unsigned long long sector =
2012 __le48_to_cpu(&entry->defective_block_start);
2013
2014 entry->defective_block_start =
2015 __cpu_to_le48(sector*IMSM_4K_DIV);
2016 entry->marked_count = count*IMSM_4K_DIV - 1;
2017 }
2018 }
2019
2020 mpb->check_sum = __gen_imsm_checksum(mpb);
2021 }
2022
2023 /*******************************************************************************
2024 * function: imsm_check_attributes
2025 * Description: Function checks if features represented by attributes flags
2026 * are supported by mdadm.
2027 * Parameters:
2028 * attributes - Attributes read from metadata
2029 * Returns:
2030 * 0 - passed attributes contains unsupported features flags
2031 * 1 - all features are supported
2032 ******************************************************************************/
2033 static int imsm_check_attributes(__u32 attributes)
2034 {
2035 int ret_val = 1;
2036 __u32 not_supported = MPB_ATTRIB_SUPPORTED^0xffffffff;
2037
2038 not_supported &= ~MPB_ATTRIB_IGNORED;
2039
2040 not_supported &= attributes;
2041 if (not_supported) {
2042 pr_err("(IMSM): Unsupported attributes : %x\n",
2043 (unsigned)__le32_to_cpu(not_supported));
2044 if (not_supported & MPB_ATTRIB_CHECKSUM_VERIFY) {
2045 dprintf("\t\tMPB_ATTRIB_CHECKSUM_VERIFY \n");
2046 not_supported ^= MPB_ATTRIB_CHECKSUM_VERIFY;
2047 }
2048 if (not_supported & MPB_ATTRIB_2TB) {
2049 dprintf("\t\tMPB_ATTRIB_2TB\n");
2050 not_supported ^= MPB_ATTRIB_2TB;
2051 }
2052 if (not_supported & MPB_ATTRIB_RAID0) {
2053 dprintf("\t\tMPB_ATTRIB_RAID0\n");
2054 not_supported ^= MPB_ATTRIB_RAID0;
2055 }
2056 if (not_supported & MPB_ATTRIB_RAID1) {
2057 dprintf("\t\tMPB_ATTRIB_RAID1\n");
2058 not_supported ^= MPB_ATTRIB_RAID1;
2059 }
2060 if (not_supported & MPB_ATTRIB_RAID10) {
2061 dprintf("\t\tMPB_ATTRIB_RAID10\n");
2062 not_supported ^= MPB_ATTRIB_RAID10;
2063 }
2064 if (not_supported & MPB_ATTRIB_RAID1E) {
2065 dprintf("\t\tMPB_ATTRIB_RAID1E\n");
2066 not_supported ^= MPB_ATTRIB_RAID1E;
2067 }
2068 if (not_supported & MPB_ATTRIB_RAID5) {
2069 dprintf("\t\tMPB_ATTRIB_RAID5\n");
2070 not_supported ^= MPB_ATTRIB_RAID5;
2071 }
2072 if (not_supported & MPB_ATTRIB_RAIDCNG) {
2073 dprintf("\t\tMPB_ATTRIB_RAIDCNG\n");
2074 not_supported ^= MPB_ATTRIB_RAIDCNG;
2075 }
2076 if (not_supported & MPB_ATTRIB_BBM) {
2077 dprintf("\t\tMPB_ATTRIB_BBM\n");
2078 not_supported ^= MPB_ATTRIB_BBM;
2079 }
2080 if (not_supported & MPB_ATTRIB_CHECKSUM_VERIFY) {
2081 dprintf("\t\tMPB_ATTRIB_CHECKSUM_VERIFY (== MPB_ATTRIB_LEGACY)\n");
2082 not_supported ^= MPB_ATTRIB_CHECKSUM_VERIFY;
2083 }
2084 if (not_supported & MPB_ATTRIB_EXP_STRIPE_SIZE) {
2085 dprintf("\t\tMPB_ATTRIB_EXP_STRIP_SIZE\n");
2086 not_supported ^= MPB_ATTRIB_EXP_STRIPE_SIZE;
2087 }
2088 if (not_supported & MPB_ATTRIB_2TB_DISK) {
2089 dprintf("\t\tMPB_ATTRIB_2TB_DISK\n");
2090 not_supported ^= MPB_ATTRIB_2TB_DISK;
2091 }
2092 if (not_supported & MPB_ATTRIB_NEVER_USE2) {
2093 dprintf("\t\tMPB_ATTRIB_NEVER_USE2\n");
2094 not_supported ^= MPB_ATTRIB_NEVER_USE2;
2095 }
2096 if (not_supported & MPB_ATTRIB_NEVER_USE) {
2097 dprintf("\t\tMPB_ATTRIB_NEVER_USE\n");
2098 not_supported ^= MPB_ATTRIB_NEVER_USE;
2099 }
2100
2101 if (not_supported)
2102 dprintf("(IMSM): Unknown attributes : %x\n", not_supported);
2103
2104 ret_val = 0;
2105 }
2106
2107 return ret_val;
2108 }
2109
2110 static void getinfo_super_imsm(struct supertype *st, struct mdinfo *info, char *map);
2111
2112 static void examine_super_imsm(struct supertype *st, char *homehost)
2113 {
2114 struct intel_super *super = st->sb;
2115 struct imsm_super *mpb = super->anchor;
2116 char str[MAX_SIGNATURE_LENGTH];
2117 int i;
2118 struct mdinfo info;
2119 char nbuf[64];
2120 __u32 sum;
2121 __u32 reserved = imsm_reserved_sectors(super, super->disks);
2122 struct dl *dl;
2123 time_t creation_time;
2124
2125 strncpy(str, (char *)mpb->sig, MPB_SIG_LEN);
2126 str[MPB_SIG_LEN-1] = '\0';
2127 printf(" Magic : %s\n", str);
2128 printf(" Version : %s\n", get_imsm_version(mpb));
2129 printf(" Orig Family : %08x\n", __le32_to_cpu(mpb->orig_family_num));
2130 printf(" Family : %08x\n", __le32_to_cpu(mpb->family_num));
2131 printf(" Generation : %08x\n", __le32_to_cpu(mpb->generation_num));
2132 creation_time = __le64_to_cpu(mpb->creation_time);
2133 printf(" Creation Time : %.24s\n",
2134 creation_time ? ctime(&creation_time) : "Unknown");
2135 printf(" Attributes : ");
2136 if (imsm_check_attributes(mpb->attributes))
2137 printf("All supported\n");
2138 else
2139 printf("not supported\n");
2140 getinfo_super_imsm(st, &info, NULL);
2141 fname_from_uuid(st, &info, nbuf, ':');
2142 printf(" UUID : %s\n", nbuf + 5);
2143 sum = __le32_to_cpu(mpb->check_sum);
2144 printf(" Checksum : %08x %s\n", sum,
2145 __gen_imsm_checksum(mpb) == sum ? "correct" : "incorrect");
2146 printf(" MPB Sectors : %d\n", mpb_sectors(mpb, super->sector_size));
2147 printf(" Disks : %d\n", mpb->num_disks);
2148 printf(" RAID Devices : %d\n", mpb->num_raid_devs);
2149 print_imsm_disk(__get_imsm_disk(mpb, super->disks->index),
2150 super->disks->index, reserved, super->sector_size);
2151 if (get_imsm_bbm_log_size(super->bbm_log)) {
2152 struct bbm_log *log = super->bbm_log;
2153
2154 printf("\n");
2155 printf("Bad Block Management Log:\n");
2156 printf(" Log Size : %d\n", __le32_to_cpu(mpb->bbm_log_size));
2157 printf(" Signature : %x\n", __le32_to_cpu(log->signature));
2158 printf(" Entry Count : %d\n", __le32_to_cpu(log->entry_count));
2159 }
2160 for (i = 0; i < mpb->num_raid_devs; i++) {
2161 struct mdinfo info;
2162 struct imsm_dev *dev = __get_imsm_dev(mpb, i);
2163
2164 super->current_vol = i;
2165 getinfo_super_imsm(st, &info, NULL);
2166 fname_from_uuid(st, &info, nbuf, ':');
2167 print_imsm_dev(super, dev, nbuf + 5, super->disks->index);
2168 }
2169 for (i = 0; i < mpb->num_disks; i++) {
2170 if (i == super->disks->index)
2171 continue;
2172 print_imsm_disk(__get_imsm_disk(mpb, i), i, reserved,
2173 super->sector_size);
2174 }
2175
2176 for (dl = super->disks; dl; dl = dl->next)
2177 if (dl->index == -1)
2178 print_imsm_disk(&dl->disk, -1, reserved,
2179 super->sector_size);
2180
2181 examine_migr_rec_imsm(super);
2182 }
2183
2184 static void brief_examine_super_imsm(struct supertype *st, int verbose)
2185 {
2186 /* We just write a generic IMSM ARRAY entry */
2187 struct mdinfo info;
2188 char nbuf[64];
2189
2190 getinfo_super_imsm(st, &info, NULL);
2191 fname_from_uuid(st, &info, nbuf, ':');
2192 printf("ARRAY metadata=imsm UUID=%s\n", nbuf + 5);
2193 }
2194
2195 static void brief_examine_subarrays_imsm(struct supertype *st, int verbose)
2196 {
2197 /* We just write a generic IMSM ARRAY entry */
2198 struct mdinfo info;
2199 char nbuf[64];
2200 char nbuf1[64];
2201 struct intel_super *super = st->sb;
2202 int i;
2203
2204 if (!super->anchor->num_raid_devs)
2205 return;
2206
2207 getinfo_super_imsm(st, &info, NULL);
2208 fname_from_uuid(st, &info, nbuf, ':');
2209 for (i = 0; i < super->anchor->num_raid_devs; i++) {
2210 struct imsm_dev *dev = get_imsm_dev(super, i);
2211
2212 super->current_vol = i;
2213 getinfo_super_imsm(st, &info, NULL);
2214 fname_from_uuid(st, &info, nbuf1, ':');
2215 printf("ARRAY /dev/md/%.16s container=%s member=%d UUID=%s\n",
2216 dev->volume, nbuf + 5, i, nbuf1 + 5);
2217 }
2218 }
2219
2220 static void export_examine_super_imsm(struct supertype *st)
2221 {
2222 struct intel_super *super = st->sb;
2223 struct imsm_super *mpb = super->anchor;
2224 struct mdinfo info;
2225 char nbuf[64];
2226
2227 getinfo_super_imsm(st, &info, NULL);
2228 fname_from_uuid(st, &info, nbuf, ':');
2229 printf("MD_METADATA=imsm\n");
2230 printf("MD_LEVEL=container\n");
2231 printf("MD_UUID=%s\n", nbuf+5);
2232 printf("MD_DEVICES=%u\n", mpb->num_disks);
2233 printf("MD_CREATION_TIME=%llu\n", __le64_to_cpu(mpb->creation_time));
2234 }
2235
2236 static void detail_super_imsm(struct supertype *st, char *homehost,
2237 char *subarray)
2238 {
2239 struct mdinfo info;
2240 char nbuf[64];
2241 struct intel_super *super = st->sb;
2242 int temp_vol = super->current_vol;
2243
2244 if (subarray)
2245 super->current_vol = strtoul(subarray, NULL, 10);
2246
2247 getinfo_super_imsm(st, &info, NULL);
2248 fname_from_uuid(st, &info, nbuf, ':');
2249 printf("\n UUID : %s\n", nbuf + 5);
2250
2251 super->current_vol = temp_vol;
2252 }
2253
2254 static void brief_detail_super_imsm(struct supertype *st, char *subarray)
2255 {
2256 struct mdinfo info;
2257 char nbuf[64];
2258 struct intel_super *super = st->sb;
2259 int temp_vol = super->current_vol;
2260
2261 if (subarray)
2262 super->current_vol = strtoul(subarray, NULL, 10);
2263
2264 getinfo_super_imsm(st, &info, NULL);
2265 fname_from_uuid(st, &info, nbuf, ':');
2266 printf(" UUID=%s", nbuf + 5);
2267
2268 super->current_vol = temp_vol;
2269 }
2270
2271 static int imsm_read_serial(int fd, char *devname, __u8 *serial,
2272 size_t serial_buf_len);
2273 static void fd2devname(int fd, char *name);
2274
2275 static int ahci_enumerate_ports(const char *hba_path, int port_count, int host_base, int verbose)
2276 {
2277 /* dump an unsorted list of devices attached to AHCI Intel storage
2278 * controller, as well as non-connected ports
2279 */
2280 int hba_len = strlen(hba_path) + 1;
2281 struct dirent *ent;
2282 DIR *dir;
2283 char *path = NULL;
2284 int err = 0;
2285 unsigned long port_mask = (1 << port_count) - 1;
2286
2287 if (port_count > (int)sizeof(port_mask) * 8) {
2288 if (verbose > 0)
2289 pr_err("port_count %d out of range\n", port_count);
2290 return 2;
2291 }
2292
2293 /* scroll through /sys/dev/block looking for devices attached to
2294 * this hba
2295 */
2296 dir = opendir("/sys/dev/block");
2297 if (!dir)
2298 return 1;
2299
2300 for (ent = readdir(dir); ent; ent = readdir(dir)) {
2301 int fd;
2302 char model[64];
2303 char vendor[64];
2304 char buf[1024];
2305 int major, minor;
2306 char device[PATH_MAX];
2307 char *c;
2308 int port;
2309 int type;
2310
2311 if (sscanf(ent->d_name, "%d:%d", &major, &minor) != 2)
2312 continue;
2313 path = devt_to_devpath(makedev(major, minor), 1, NULL);
2314 if (!path)
2315 continue;
2316 if (!path_attached_to_hba(path, hba_path)) {
2317 free(path);
2318 path = NULL;
2319 continue;
2320 }
2321
2322 /* retrieve the scsi device */
2323 if (!devt_to_devpath(makedev(major, minor), 1, device)) {
2324 if (verbose > 0)
2325 pr_err("failed to get device\n");
2326 err = 2;
2327 break;
2328 }
2329 if (devpath_to_char(device, "type", buf, sizeof(buf), 0)) {
2330 err = 2;
2331 break;
2332 }
2333 type = strtoul(buf, NULL, 10);
2334
2335 /* if it's not a disk print the vendor and model */
2336 if (!(type == 0 || type == 7 || type == 14)) {
2337 vendor[0] = '\0';
2338 model[0] = '\0';
2339
2340 if (devpath_to_char(device, "vendor", buf,
2341 sizeof(buf), 0) == 0) {
2342 strncpy(vendor, buf, sizeof(vendor));
2343 vendor[sizeof(vendor) - 1] = '\0';
2344 c = (char *) &vendor[sizeof(vendor) - 1];
2345 while (isspace(*c) || *c == '\0')
2346 *c-- = '\0';
2347
2348 }
2349
2350 if (devpath_to_char(device, "model", buf,
2351 sizeof(buf), 0) == 0) {
2352 strncpy(model, buf, sizeof(model));
2353 model[sizeof(model) - 1] = '\0';
2354 c = (char *) &model[sizeof(model) - 1];
2355 while (isspace(*c) || *c == '\0')
2356 *c-- = '\0';
2357 }
2358
2359 if (vendor[0] && model[0])
2360 sprintf(buf, "%.64s %.64s", vendor, model);
2361 else
2362 switch (type) { /* numbers from hald/linux/device.c */
2363 case 1: sprintf(buf, "tape"); break;
2364 case 2: sprintf(buf, "printer"); break;
2365 case 3: sprintf(buf, "processor"); break;
2366 case 4:
2367 case 5: sprintf(buf, "cdrom"); break;
2368 case 6: sprintf(buf, "scanner"); break;
2369 case 8: sprintf(buf, "media_changer"); break;
2370 case 9: sprintf(buf, "comm"); break;
2371 case 12: sprintf(buf, "raid"); break;
2372 default: sprintf(buf, "unknown");
2373 }
2374 } else
2375 buf[0] = '\0';
2376
2377 /* chop device path to 'host%d' and calculate the port number */
2378 c = strchr(&path[hba_len], '/');
2379 if (!c) {
2380 if (verbose > 0)
2381 pr_err("%s - invalid path name\n", path + hba_len);
2382 err = 2;
2383 break;
2384 }
2385 *c = '\0';
2386 if ((sscanf(&path[hba_len], "ata%d", &port) == 1) ||
2387 ((sscanf(&path[hba_len], "host%d", &port) == 1)))
2388 port -= host_base;
2389 else {
2390 if (verbose > 0) {
2391 *c = '/'; /* repair the full string */
2392 pr_err("failed to determine port number for %s\n",
2393 path);
2394 }
2395 err = 2;
2396 break;
2397 }
2398
2399 /* mark this port as used */
2400 port_mask &= ~(1 << port);
2401
2402 /* print out the device information */
2403 if (buf[0]) {
2404 printf(" Port%d : - non-disk device (%s) -\n", port, buf);
2405 continue;
2406 }
2407
2408 fd = dev_open(ent->d_name, O_RDONLY);
2409 if (!is_fd_valid(fd))
2410 printf(" Port%d : - disk info unavailable -\n", port);
2411 else {
2412 fd2devname(fd, buf);
2413 printf(" Port%d : %s", port, buf);
2414 if (imsm_read_serial(fd, NULL, (__u8 *)buf,
2415 sizeof(buf)) == 0)
2416 printf(" (%s)\n", buf);
2417 else
2418 printf(" ()\n");
2419 close(fd);
2420 }
2421 free(path);
2422 path = NULL;
2423 }
2424 if (path)
2425 free(path);
2426 if (dir)
2427 closedir(dir);
2428 if (err == 0) {
2429 int i;
2430
2431 for (i = 0; i < port_count; i++)
2432 if (port_mask & (1 << i))
2433 printf(" Port%d : - no device attached -\n", i);
2434 }
2435
2436 return err;
2437 }
2438
2439 static int print_nvme_info(struct sys_dev *hba)
2440 {
2441 struct dirent *ent;
2442 DIR *dir;
2443
2444 dir = opendir("/sys/block/");
2445 if (!dir)
2446 return 1;
2447
2448 for (ent = readdir(dir); ent; ent = readdir(dir)) {
2449 char ns_path[PATH_MAX];
2450 char cntrl_path[PATH_MAX];
2451 char buf[PATH_MAX];
2452 int fd = -1;
2453
2454 if (!strstr(ent->d_name, "nvme"))
2455 goto skip;
2456
2457 fd = open_dev(ent->d_name);
2458 if (!is_fd_valid(fd))
2459 goto skip;
2460
2461 if (!diskfd_to_devpath(fd, 0, ns_path) ||
2462 !diskfd_to_devpath(fd, 1, cntrl_path))
2463 goto skip;
2464
2465 if (!path_attached_to_hba(cntrl_path, hba->path))
2466 goto skip;
2467
2468 if (!imsm_is_nvme_namespace_supported(fd, 0))
2469 goto skip;
2470
2471 fd2devname(fd, buf);
2472 if (hba->type == SYS_DEV_VMD)
2473 printf(" NVMe under VMD : %s", buf);
2474 else if (hba->type == SYS_DEV_NVME)
2475 printf(" NVMe Device : %s", buf);
2476
2477 if (!imsm_read_serial(fd, NULL, (__u8 *)buf,
2478 sizeof(buf)))
2479 printf(" (%s)\n", buf);
2480 else
2481 printf("()\n");
2482
2483 skip:
2484 close_fd(&fd);
2485 }
2486
2487 closedir(dir);
2488 return 0;
2489 }
2490
2491 static void print_found_intel_controllers(struct sys_dev *elem)
2492 {
2493 for (; elem; elem = elem->next) {
2494 pr_err("found Intel(R) ");
2495 if (elem->type == SYS_DEV_SATA)
2496 fprintf(stderr, "SATA ");
2497 else if (elem->type == SYS_DEV_SAS)
2498 fprintf(stderr, "SAS ");
2499 else if (elem->type == SYS_DEV_NVME)
2500 fprintf(stderr, "NVMe ");
2501
2502 if (elem->type == SYS_DEV_VMD)
2503 fprintf(stderr, "VMD domain");
2504 else
2505 fprintf(stderr, "RAID controller");
2506
2507 if (elem->pci_id)
2508 fprintf(stderr, " at %s", elem->pci_id);
2509 fprintf(stderr, ".\n");
2510 }
2511 fflush(stderr);
2512 }
2513
2514 static int ahci_get_port_count(const char *hba_path, int *port_count)
2515 {
2516 struct dirent *ent;
2517 DIR *dir;
2518 int host_base = -1;
2519
2520 *port_count = 0;
2521 if ((dir = opendir(hba_path)) == NULL)
2522 return -1;
2523
2524 for (ent = readdir(dir); ent; ent = readdir(dir)) {
2525 int host;
2526
2527 if ((sscanf(ent->d_name, "ata%d", &host) != 1) &&
2528 ((sscanf(ent->d_name, "host%d", &host) != 1)))
2529 continue;
2530 if (*port_count == 0)
2531 host_base = host;
2532 else if (host < host_base)
2533 host_base = host;
2534
2535 if (host + 1 > *port_count + host_base)
2536 *port_count = host + 1 - host_base;
2537 }
2538 closedir(dir);
2539 return host_base;
2540 }
2541
2542 static void print_imsm_capability(const struct imsm_orom *orom)
2543 {
2544 printf(" Platform : Intel(R) ");
2545 if (orom->capabilities == 0 && orom->driver_features == 0)
2546 printf("Matrix Storage Manager\n");
2547 else if (imsm_orom_is_enterprise(orom) && orom->major_ver >= 6)
2548 printf("Virtual RAID on CPU\n");
2549 else
2550 printf("Rapid Storage Technology%s\n",
2551 imsm_orom_is_enterprise(orom) ? " enterprise" : "");
2552 if (orom->major_ver || orom->minor_ver || orom->hotfix_ver || orom->build)
2553 printf(" Version : %d.%d.%d.%d\n", orom->major_ver,
2554 orom->minor_ver, orom->hotfix_ver, orom->build);
2555 printf(" RAID Levels :%s%s%s%s%s\n",
2556 imsm_orom_has_raid0(orom) ? " raid0" : "",
2557 imsm_orom_has_raid1(orom) ? " raid1" : "",
2558 imsm_orom_has_raid1e(orom) ? " raid1e" : "",
2559 imsm_orom_has_raid10(orom) ? " raid10" : "",
2560 imsm_orom_has_raid5(orom) ? " raid5" : "");
2561 printf(" Chunk Sizes :%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
2562 imsm_orom_has_chunk(orom, 2) ? " 2k" : "",
2563 imsm_orom_has_chunk(orom, 4) ? " 4k" : "",
2564 imsm_orom_has_chunk(orom, 8) ? " 8k" : "",
2565 imsm_orom_has_chunk(orom, 16) ? " 16k" : "",
2566 imsm_orom_has_chunk(orom, 32) ? " 32k" : "",
2567 imsm_orom_has_chunk(orom, 64) ? " 64k" : "",
2568 imsm_orom_has_chunk(orom, 128) ? " 128k" : "",
2569 imsm_orom_has_chunk(orom, 256) ? " 256k" : "",
2570 imsm_orom_has_chunk(orom, 512) ? " 512k" : "",
2571 imsm_orom_has_chunk(orom, 1024*1) ? " 1M" : "",
2572 imsm_orom_has_chunk(orom, 1024*2) ? " 2M" : "",
2573 imsm_orom_has_chunk(orom, 1024*4) ? " 4M" : "",
2574 imsm_orom_has_chunk(orom, 1024*8) ? " 8M" : "",
2575 imsm_orom_has_chunk(orom, 1024*16) ? " 16M" : "",
2576 imsm_orom_has_chunk(orom, 1024*32) ? " 32M" : "",
2577 imsm_orom_has_chunk(orom, 1024*64) ? " 64M" : "");
2578 printf(" 2TB volumes :%s supported\n",
2579 (orom->attr & IMSM_OROM_ATTR_2TB)?"":" not");
2580 printf(" 2TB disks :%s supported\n",
2581 (orom->attr & IMSM_OROM_ATTR_2TB_DISK)?"":" not");
2582 printf(" Max Disks : %d\n", orom->tds);
2583 printf(" Max Volumes : %d per array, %d per %s\n",
2584 orom->vpa, orom->vphba,
2585 imsm_orom_is_nvme(orom) ? "platform" : "controller");
2586 return;
2587 }
2588
2589 static void print_imsm_capability_export(const struct imsm_orom *orom)
2590 {
2591 printf("MD_FIRMWARE_TYPE=imsm\n");
2592 if (orom->major_ver || orom->minor_ver || orom->hotfix_ver || orom->build)
2593 printf("IMSM_VERSION=%d.%d.%d.%d\n", orom->major_ver, orom->minor_ver,
2594 orom->hotfix_ver, orom->build);
2595 printf("IMSM_SUPPORTED_RAID_LEVELS=%s%s%s%s%s\n",
2596 imsm_orom_has_raid0(orom) ? "raid0 " : "",
2597 imsm_orom_has_raid1(orom) ? "raid1 " : "",
2598 imsm_orom_has_raid1e(orom) ? "raid1e " : "",
2599 imsm_orom_has_raid5(orom) ? "raid10 " : "",
2600 imsm_orom_has_raid10(orom) ? "raid5 " : "");
2601 printf("IMSM_SUPPORTED_CHUNK_SIZES=%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
2602 imsm_orom_has_chunk(orom, 2) ? "2k " : "",
2603 imsm_orom_has_chunk(orom, 4) ? "4k " : "",
2604 imsm_orom_has_chunk(orom, 8) ? "8k " : "",
2605 imsm_orom_has_chunk(orom, 16) ? "16k " : "",
2606 imsm_orom_has_chunk(orom, 32) ? "32k " : "",
2607 imsm_orom_has_chunk(orom, 64) ? "64k " : "",
2608 imsm_orom_has_chunk(orom, 128) ? "128k " : "",
2609 imsm_orom_has_chunk(orom, 256) ? "256k " : "",
2610 imsm_orom_has_chunk(orom, 512) ? "512k " : "",
2611 imsm_orom_has_chunk(orom, 1024*1) ? "1M " : "",
2612 imsm_orom_has_chunk(orom, 1024*2) ? "2M " : "",
2613 imsm_orom_has_chunk(orom, 1024*4) ? "4M " : "",
2614 imsm_orom_has_chunk(orom, 1024*8) ? "8M " : "",
2615 imsm_orom_has_chunk(orom, 1024*16) ? "16M " : "",
2616 imsm_orom_has_chunk(orom, 1024*32) ? "32M " : "",
2617 imsm_orom_has_chunk(orom, 1024*64) ? "64M " : "");
2618 printf("IMSM_2TB_VOLUMES=%s\n",(orom->attr & IMSM_OROM_ATTR_2TB) ? "yes" : "no");
2619 printf("IMSM_2TB_DISKS=%s\n",(orom->attr & IMSM_OROM_ATTR_2TB_DISK) ? "yes" : "no");
2620 printf("IMSM_MAX_DISKS=%d\n",orom->tds);
2621 printf("IMSM_MAX_VOLUMES_PER_ARRAY=%d\n",orom->vpa);
2622 printf("IMSM_MAX_VOLUMES_PER_CONTROLLER=%d\n",orom->vphba);
2623 }
2624
2625 static int detail_platform_imsm(int verbose, int enumerate_only, char *controller_path)
2626 {
2627 /* There are two components to imsm platform support, the ahci SATA
2628 * controller and the option-rom. To find the SATA controller we
2629 * simply look in /sys/bus/pci/drivers/ahci to see if an ahci
2630 * controller with the Intel vendor id is present. This approach
2631 * allows mdadm to leverage the kernel's ahci detection logic, with the
2632 * caveat that if ahci.ko is not loaded mdadm will not be able to
2633 * detect platform raid capabilities. The option-rom resides in a
2634 * platform "Adapter ROM". We scan for its signature to retrieve the
2635 * platform capabilities. If raid support is disabled in the BIOS the
2636 * option-rom capability structure will not be available.
2637 */
2638 struct sys_dev *list, *hba;
2639 int host_base = 0;
2640 int port_count = 0;
2641 int result=1;
2642
2643 if (enumerate_only) {
2644 if (check_env("IMSM_NO_PLATFORM"))
2645 return 0;
2646 list = find_intel_devices();
2647 if (!list)
2648 return 2;
2649 for (hba = list; hba; hba = hba->next) {
2650 if (find_imsm_capability(hba)) {
2651 result = 0;
2652 break;
2653 }
2654 else
2655 result = 2;
2656 }
2657 return result;
2658 }
2659
2660 list = find_intel_devices();
2661 if (!list) {
2662 if (verbose > 0)
2663 pr_err("no active Intel(R) RAID controller found.\n");
2664 return 2;
2665 } else if (verbose > 0)
2666 print_found_intel_controllers(list);
2667
2668 for (hba = list; hba; hba = hba->next) {
2669 if (controller_path && (compare_paths(hba->path, controller_path) != 0))
2670 continue;
2671 if (!find_imsm_capability(hba)) {
2672 char buf[PATH_MAX];
2673 pr_err("imsm capabilities not found for controller: %s (type %s)\n",
2674 hba->type == SYS_DEV_VMD ? vmd_domain_to_controller(hba, buf) : hba->path,
2675 get_sys_dev_type(hba->type));
2676 continue;
2677 }
2678 result = 0;
2679 }
2680
2681 if (controller_path && result == 1) {
2682 pr_err("no active Intel(R) RAID controller found under %s\n",
2683 controller_path);
2684 return result;
2685 }
2686
2687 const struct orom_entry *entry;
2688
2689 for (entry = orom_entries; entry; entry = entry->next) {
2690 if (entry->type == SYS_DEV_VMD) {
2691 print_imsm_capability(&entry->orom);
2692 printf(" 3rd party NVMe :%s supported\n",
2693 imsm_orom_has_tpv_support(&entry->orom)?"":" not");
2694 for (hba = list; hba; hba = hba->next) {
2695 if (hba->type == SYS_DEV_VMD) {
2696 char buf[PATH_MAX];
2697 printf(" I/O Controller : %s (%s)\n",
2698 vmd_domain_to_controller(hba, buf), get_sys_dev_type(hba->type));
2699 if (print_nvme_info(hba)) {
2700 if (verbose > 0)
2701 pr_err("failed to get devices attached to VMD domain.\n");
2702 result |= 2;
2703 }
2704 }
2705 }
2706 printf("\n");
2707 continue;
2708 }
2709
2710 print_imsm_capability(&entry->orom);
2711 if (entry->type == SYS_DEV_NVME) {
2712 for (hba = list; hba; hba = hba->next) {
2713 if (hba->type == SYS_DEV_NVME)
2714 print_nvme_info(hba);
2715 }
2716 printf("\n");
2717 continue;
2718 }
2719
2720 struct devid_list *devid;
2721 for (devid = entry->devid_list; devid; devid = devid->next) {
2722 hba = device_by_id(devid->devid);
2723 if (!hba)
2724 continue;
2725
2726 printf(" I/O Controller : %s (%s)\n",
2727 hba->path, get_sys_dev_type(hba->type));
2728 if (hba->type == SYS_DEV_SATA) {
2729 host_base = ahci_get_port_count(hba->path, &port_count);
2730 if (ahci_enumerate_ports(hba->path, port_count, host_base, verbose)) {
2731 if (verbose > 0)
2732 pr_err("failed to enumerate ports on SATA controller at %s.\n", hba->pci_id);
2733 result |= 2;
2734 }
2735 }
2736 }
2737 printf("\n");
2738 }
2739
2740 return result;
2741 }
2742
2743 static int export_detail_platform_imsm(int verbose, char *controller_path)
2744 {
2745 struct sys_dev *list, *hba;
2746 int result=1;
2747
2748 list = find_intel_devices();
2749 if (!list) {
2750 if (verbose > 0)
2751 pr_err("IMSM_DETAIL_PLATFORM_ERROR=NO_INTEL_DEVICES\n");
2752 result = 2;
2753 return result;
2754 }
2755
2756 for (hba = list; hba; hba = hba->next) {
2757 if (controller_path && (compare_paths(hba->path,controller_path) != 0))
2758 continue;
2759 if (!find_imsm_capability(hba) && verbose > 0) {
2760 char buf[PATH_MAX];
2761 pr_err("IMSM_DETAIL_PLATFORM_ERROR=NO_IMSM_CAPABLE_DEVICE_UNDER_%s\n",
2762 hba->type == SYS_DEV_VMD ? vmd_domain_to_controller(hba, buf) : hba->path);
2763 }
2764 else
2765 result = 0;
2766 }
2767
2768 const struct orom_entry *entry;
2769
2770 for (entry = orom_entries; entry; entry = entry->next) {
2771 if (entry->type == SYS_DEV_VMD) {
2772 for (hba = list; hba; hba = hba->next)
2773 print_imsm_capability_export(&entry->orom);
2774 continue;
2775 }
2776 print_imsm_capability_export(&entry->orom);
2777 }
2778
2779 return result;
2780 }
2781
2782 static int match_home_imsm(struct supertype *st, char *homehost)
2783 {
2784 /* the imsm metadata format does not specify any host
2785 * identification information. We return -1 since we can never
2786 * confirm nor deny whether a given array is "meant" for this
2787 * host. We rely on compare_super and the 'family_num' fields to
2788 * exclude member disks that do not belong, and we rely on
2789 * mdadm.conf to specify the arrays that should be assembled.
2790 * Auto-assembly may still pick up "foreign" arrays.
2791 */
2792
2793 return -1;
2794 }
2795
2796 static void uuid_from_super_imsm(struct supertype *st, int uuid[4])
2797 {
2798 /* The uuid returned here is used for:
2799 * uuid to put into bitmap file (Create, Grow)
2800 * uuid for backup header when saving critical section (Grow)
2801 * comparing uuids when re-adding a device into an array
2802 * In these cases the uuid required is that of the data-array,
2803 * not the device-set.
2804 * uuid to recognise same set when adding a missing device back
2805 * to an array. This is a uuid for the device-set.
2806 *
2807 * For each of these we can make do with a truncated
2808 * or hashed uuid rather than the original, as long as
2809 * everyone agrees.
2810 * In each case the uuid required is that of the data-array,
2811 * not the device-set.
2812 */
2813 /* imsm does not track uuid's so we synthesis one using sha1 on
2814 * - The signature (Which is constant for all imsm array, but no matter)
2815 * - the orig_family_num of the container
2816 * - the index number of the volume
2817 * - the 'serial' number of the volume.
2818 * Hopefully these are all constant.
2819 */
2820 struct intel_super *super = st->sb;
2821
2822 char buf[20];
2823 struct sha1_ctx ctx;
2824 struct imsm_dev *dev = NULL;
2825 __u32 family_num;
2826
2827 /* some mdadm versions failed to set ->orig_family_num, in which
2828 * case fall back to ->family_num. orig_family_num will be
2829 * fixed up with the first metadata update.
2830 */
2831 family_num = super->anchor->orig_family_num;
2832 if (family_num == 0)
2833 family_num = super->anchor->family_num;
2834 sha1_init_ctx(&ctx);
2835 sha1_process_bytes(super->anchor->sig, MPB_SIG_LEN, &ctx);
2836 sha1_process_bytes(&family_num, sizeof(__u32), &ctx);
2837 if (super->current_vol >= 0)
2838 dev = get_imsm_dev(super, super->current_vol);
2839 if (dev) {
2840 __u32 vol = super->current_vol;
2841 sha1_process_bytes(&vol, sizeof(vol), &ctx);
2842 sha1_process_bytes(dev->volume, MAX_RAID_SERIAL_LEN, &ctx);
2843 }
2844 sha1_finish_ctx(&ctx, buf);
2845 memcpy(uuid, buf, 4*4);
2846 }
2847
2848 #if 0
2849 static void
2850 get_imsm_numerical_version(struct imsm_super *mpb, int *m, int *p)
2851 {
2852 __u8 *v = get_imsm_version(mpb);
2853 __u8 *end = mpb->sig + MAX_SIGNATURE_LENGTH;
2854 char major[] = { 0, 0, 0 };
2855 char minor[] = { 0 ,0, 0 };
2856 char patch[] = { 0, 0, 0 };
2857 char *ver_parse[] = { major, minor, patch };
2858 int i, j;
2859
2860 i = j = 0;
2861 while (*v != '\0' && v < end) {
2862 if (*v != '.' && j < 2)
2863 ver_parse[i][j++] = *v;
2864 else {
2865 i++;
2866 j = 0;
2867 }
2868 v++;
2869 }
2870
2871 *m = strtol(minor, NULL, 0);
2872 *p = strtol(patch, NULL, 0);
2873 }
2874 #endif
2875
2876 static __u32 migr_strip_blocks_resync(struct imsm_dev *dev)
2877 {
2878 /* migr_strip_size when repairing or initializing parity */
2879 struct imsm_map *map = get_imsm_map(dev, MAP_0);
2880 __u32 chunk = __le32_to_cpu(map->blocks_per_strip);
2881
2882 switch (get_imsm_raid_level(map)) {
2883 case 5:
2884 case 10:
2885 return chunk;
2886 default:
2887 return 128*1024 >> 9;
2888 }
2889 }
2890
2891 static __u32 migr_strip_blocks_rebuild(struct imsm_dev *dev)
2892 {
2893 /* migr_strip_size when rebuilding a degraded disk, no idea why
2894 * this is different than migr_strip_size_resync(), but it's good
2895 * to be compatible
2896 */
2897 struct imsm_map *map = get_imsm_map(dev, MAP_1);
2898 __u32 chunk = __le32_to_cpu(map->blocks_per_strip);
2899
2900 switch (get_imsm_raid_level(map)) {
2901 case 1:
2902 case 10:
2903 if (map->num_members % map->num_domains == 0)
2904 return 128*1024 >> 9;
2905 else
2906 return chunk;
2907 case 5:
2908 return max((__u32) 64*1024 >> 9, chunk);
2909 default:
2910 return 128*1024 >> 9;
2911 }
2912 }
2913
2914 static __u32 num_stripes_per_unit_resync(struct imsm_dev *dev)
2915 {
2916 struct imsm_map *lo = get_imsm_map(dev, MAP_0);
2917 struct imsm_map *hi = get_imsm_map(dev, MAP_1);
2918 __u32 lo_chunk = __le32_to_cpu(lo->blocks_per_strip);
2919 __u32 hi_chunk = __le32_to_cpu(hi->blocks_per_strip);
2920
2921 return max((__u32) 1, hi_chunk / lo_chunk);
2922 }
2923
2924 static __u32 num_stripes_per_unit_rebuild(struct imsm_dev *dev)
2925 {
2926 struct imsm_map *lo = get_imsm_map(dev, MAP_0);
2927 int level = get_imsm_raid_level(lo);
2928
2929 if (level == 1 || level == 10) {
2930 struct imsm_map *hi = get_imsm_map(dev, MAP_1);
2931
2932 return hi->num_domains;
2933 } else
2934 return num_stripes_per_unit_resync(dev);
2935 }
2936
2937 static unsigned long long calc_component_size(struct imsm_map *map,
2938 struct imsm_dev *dev)
2939 {
2940 unsigned long long component_size;
2941 unsigned long long dev_size = imsm_dev_size(dev);
2942 long long calc_dev_size = 0;
2943 unsigned int member_disks = imsm_num_data_members(map);
2944
2945 if (member_disks == 0)
2946 return 0;
2947
2948 component_size = per_dev_array_size(map);
2949 calc_dev_size = component_size * member_disks;
2950
2951 /* Component size is rounded to 1MB so difference between size from
2952 * metadata and size calculated from num_data_stripes equals up to
2953 * 2048 blocks per each device. If the difference is higher it means
2954 * that array size was expanded and num_data_stripes was not updated.
2955 */
2956 if (llabs(calc_dev_size - (long long)dev_size) >
2957 (1 << SECT_PER_MB_SHIFT) * member_disks) {
2958 component_size = dev_size / member_disks;
2959 dprintf("Invalid num_data_stripes in metadata; expected=%llu, found=%llu\n",
2960 component_size / map->blocks_per_strip,
2961 num_data_stripes(map));
2962 }
2963
2964 return component_size;
2965 }
2966
2967 static __u32 parity_segment_depth(struct imsm_dev *dev)
2968 {
2969 struct imsm_map *map = get_imsm_map(dev, MAP_0);
2970 __u32 chunk = __le32_to_cpu(map->blocks_per_strip);
2971
2972 switch(get_imsm_raid_level(map)) {
2973 case 1:
2974 case 10:
2975 return chunk * map->num_domains;
2976 case 5:
2977 return chunk * map->num_members;
2978 default:
2979 return chunk;
2980 }
2981 }
2982
2983 static __u32 map_migr_block(struct imsm_dev *dev, __u32 block)
2984 {
2985 struct imsm_map *map = get_imsm_map(dev, MAP_1);
2986 __u32 chunk = __le32_to_cpu(map->blocks_per_strip);
2987 __u32 strip = block / chunk;
2988
2989 switch (get_imsm_raid_level(map)) {
2990 case 1:
2991 case 10: {
2992 __u32 vol_strip = (strip * map->num_domains) + 1;
2993 __u32 vol_stripe = vol_strip / map->num_members;
2994
2995 return vol_stripe * chunk + block % chunk;
2996 } case 5: {
2997 __u32 stripe = strip / (map->num_members - 1);
2998
2999 return stripe * chunk + block % chunk;
3000 }
3001 default:
3002 return 0;
3003 }
3004 }
3005
3006 static __u64 blocks_per_migr_unit(struct intel_super *super,
3007 struct imsm_dev *dev)
3008 {
3009 /* calculate the conversion factor between per member 'blocks'
3010 * (md/{resync,rebuild}_start) and imsm migration units, return
3011 * 0 for the 'not migrating' and 'unsupported migration' cases
3012 */
3013 if (!dev->vol.migr_state)
3014 return 0;
3015
3016 switch (migr_type(dev)) {
3017 case MIGR_GEN_MIGR: {
3018 struct migr_record *migr_rec = super->migr_rec;
3019 return __le32_to_cpu(migr_rec->blocks_per_unit);
3020 }
3021 case MIGR_VERIFY:
3022 case MIGR_REPAIR:
3023 case MIGR_INIT: {
3024 struct imsm_map *map = get_imsm_map(dev, MAP_0);
3025 __u32 stripes_per_unit;
3026 __u32 blocks_per_unit;
3027 __u32 parity_depth;
3028 __u32 migr_chunk;
3029 __u32 block_map;
3030 __u32 block_rel;
3031 __u32 segment;
3032 __u32 stripe;
3033 __u8 disks;
3034
3035 /* yes, this is really the translation of migr_units to
3036 * per-member blocks in the 'resync' case
3037 */
3038 stripes_per_unit = num_stripes_per_unit_resync(dev);
3039 migr_chunk = migr_strip_blocks_resync(dev);
3040 disks = imsm_num_data_members(map);
3041 blocks_per_unit = stripes_per_unit * migr_chunk * disks;
3042 stripe = __le16_to_cpu(map->blocks_per_strip) * disks;
3043 segment = blocks_per_unit / stripe;
3044 block_rel = blocks_per_unit - segment * stripe;
3045 parity_depth = parity_segment_depth(dev);
3046 block_map = map_migr_block(dev, block_rel);
3047 return block_map + parity_depth * segment;
3048 }
3049 case MIGR_REBUILD: {
3050 __u32 stripes_per_unit;
3051 __u32 migr_chunk;
3052
3053 stripes_per_unit = num_stripes_per_unit_rebuild(dev);
3054 migr_chunk = migr_strip_blocks_rebuild(dev);
3055 return migr_chunk * stripes_per_unit;
3056 }
3057 case MIGR_STATE_CHANGE:
3058 default:
3059 return 0;
3060 }
3061 }
3062
3063 static int imsm_level_to_layout(int level)
3064 {
3065 switch (level) {
3066 case 0:
3067 case 1:
3068 return 0;
3069 case 5:
3070 case 6:
3071 return ALGORITHM_LEFT_ASYMMETRIC;
3072 case 10:
3073 return 0x102;
3074 }
3075 return UnSet;
3076 }
3077
3078 /*******************************************************************************
3079 * Function: read_imsm_migr_rec
3080 * Description: Function reads imsm migration record from last sector of disk
3081 * Parameters:
3082 * fd : disk descriptor
3083 * super : metadata info
3084 * Returns:
3085 * 0 : success,
3086 * -1 : fail
3087 ******************************************************************************/
3088 static int read_imsm_migr_rec(int fd, struct intel_super *super)
3089 {
3090 int ret_val = -1;
3091 unsigned int sector_size = super->sector_size;
3092 unsigned long long dsize;
3093
3094 get_dev_size(fd, NULL, &dsize);
3095 if (lseek64(fd, dsize - (sector_size*MIGR_REC_SECTOR_POSITION),
3096 SEEK_SET) < 0) {
3097 pr_err("Cannot seek to anchor block: %s\n",
3098 strerror(errno));
3099 goto out;
3100 }
3101 if ((unsigned int)read(fd, super->migr_rec_buf,
3102 MIGR_REC_BUF_SECTORS*sector_size) !=
3103 MIGR_REC_BUF_SECTORS*sector_size) {
3104 pr_err("Cannot read migr record block: %s\n",
3105 strerror(errno));
3106 goto out;
3107 }
3108 ret_val = 0;
3109 if (sector_size == 4096)
3110 convert_from_4k_imsm_migr_rec(super);
3111
3112 out:
3113 return ret_val;
3114 }
3115
3116 static struct imsm_dev *imsm_get_device_during_migration(
3117 struct intel_super *super)
3118 {
3119
3120 struct intel_dev *dv;
3121
3122 for (dv = super->devlist; dv; dv = dv->next) {
3123 if (is_gen_migration(dv->dev))
3124 return dv->dev;
3125 }
3126 return NULL;
3127 }
3128
3129 /*******************************************************************************
3130 * Function: load_imsm_migr_rec
3131 * Description: Function reads imsm migration record (it is stored at the last
3132 * sector of disk)
3133 * Parameters:
3134 * super : imsm internal array info
3135 * Returns:
3136 * 0 : success
3137 * -1 : fail
3138 * -2 : no migration in progress
3139 ******************************************************************************/
3140 static int load_imsm_migr_rec(struct intel_super *super)
3141 {
3142 struct dl *dl;
3143 char nm[30];
3144 int retval = -1;
3145 int fd = -1;
3146 struct imsm_dev *dev;
3147 struct imsm_map *map;
3148 int slot = -1;
3149 int keep_fd = 1;
3150
3151 /* find map under migration */
3152 dev = imsm_get_device_during_migration(super);
3153 /* nothing to load,no migration in progress?
3154 */
3155 if (dev == NULL)
3156 return -2;
3157
3158 map = get_imsm_map(dev, MAP_0);
3159 if (!map)
3160 return -1;
3161
3162 for (dl = super->disks; dl; dl = dl->next) {
3163 /* skip spare and failed disks
3164 */
3165 if (dl->index < 0)
3166 continue;
3167 /* read only from one of the first two slots
3168 */
3169 slot = get_imsm_disk_slot(map, dl->index);
3170 if (slot > 1 || slot < 0)
3171 continue;
3172
3173 if (!is_fd_valid(dl->fd)) {
3174 sprintf(nm, "%d:%d", dl->major, dl->minor);
3175 fd = dev_open(nm, O_RDONLY);
3176
3177 if (is_fd_valid(fd)) {
3178 keep_fd = 0;
3179 break;
3180 }
3181 } else {
3182 fd = dl->fd;
3183 break;
3184 }
3185 }
3186
3187 if (!is_fd_valid(fd))
3188 return retval;
3189 retval = read_imsm_migr_rec(fd, super);
3190 if (!keep_fd)
3191 close(fd);
3192
3193 return retval;
3194 }
3195
3196 /*******************************************************************************
3197 * function: imsm_create_metadata_checkpoint_update
3198 * Description: It creates update for checkpoint change.
3199 * Parameters:
3200 * super : imsm internal array info
3201 * u : pointer to prepared update
3202 * Returns:
3203 * Uptate length.
3204 * If length is equal to 0, input pointer u contains no update
3205 ******************************************************************************/
3206 static int imsm_create_metadata_checkpoint_update(
3207 struct intel_super *super,
3208 struct imsm_update_general_migration_checkpoint **u)
3209 {
3210
3211 int update_memory_size = 0;
3212
3213 dprintf("(enter)\n");
3214
3215 if (u == NULL)
3216 return 0;
3217 *u = NULL;
3218
3219 /* size of all update data without anchor */
3220 update_memory_size =
3221 sizeof(struct imsm_update_general_migration_checkpoint);
3222
3223 *u = xcalloc(1, update_memory_size);
3224 if (*u == NULL) {
3225 dprintf("error: cannot get memory\n");
3226 return 0;
3227 }
3228 (*u)->type = update_general_migration_checkpoint;
3229 (*u)->curr_migr_unit = current_migr_unit(super->migr_rec);
3230 dprintf("prepared for %llu\n", (unsigned long long)(*u)->curr_migr_unit);
3231
3232 return update_memory_size;
3233 }
3234
3235 static void imsm_update_metadata_locally(struct supertype *st,
3236 void *buf, int len);
3237
3238 /*******************************************************************************
3239 * Function: write_imsm_migr_rec
3240 * Description: Function writes imsm migration record
3241 * (at the last sector of disk)
3242 * Parameters:
3243 * super : imsm internal array info
3244 * Returns:
3245 * 0 : success
3246 * -1 : if fail
3247 ******************************************************************************/
3248 static int write_imsm_migr_rec(struct supertype *st)
3249 {
3250 struct intel_super *super = st->sb;
3251 unsigned int sector_size = super->sector_size;
3252 unsigned long long dsize;
3253 int retval = -1;
3254 struct dl *sd;
3255 int len;
3256 struct imsm_update_general_migration_checkpoint *u;
3257 struct imsm_dev *dev;
3258 struct imsm_map *map;
3259
3260 /* find map under migration */
3261 dev = imsm_get_device_during_migration(super);
3262 /* if no migration, write buffer anyway to clear migr_record
3263 * on disk based on first available device
3264 */
3265 if (dev == NULL)
3266 dev = get_imsm_dev(super, super->current_vol < 0 ? 0 :
3267 super->current_vol);
3268
3269 map = get_imsm_map(dev, MAP_0);
3270
3271 if (sector_size == 4096)
3272 convert_to_4k_imsm_migr_rec(super);
3273 for (sd = super->disks ; sd ; sd = sd->next) {
3274 int slot = -1;
3275
3276 /* skip failed and spare devices */
3277 if (sd->index < 0)
3278 continue;
3279 /* write to 2 first slots only */
3280 if (map)
3281 slot = get_imsm_disk_slot(map, sd->index);
3282 if (map == NULL || slot > 1 || slot < 0)
3283 continue;
3284
3285 get_dev_size(sd->fd, NULL, &dsize);
3286 if (lseek64(sd->fd, dsize - (MIGR_REC_SECTOR_POSITION *
3287 sector_size),
3288 SEEK_SET) < 0) {
3289 pr_err("Cannot seek to anchor block: %s\n",
3290 strerror(errno));
3291 goto out;
3292 }
3293 if ((unsigned int)write(sd->fd, super->migr_rec_buf,
3294 MIGR_REC_BUF_SECTORS*sector_size) !=
3295 MIGR_REC_BUF_SECTORS*sector_size) {
3296 pr_err("Cannot write migr record block: %s\n",
3297 strerror(errno));
3298 goto out;
3299 }
3300 }
3301 if (sector_size == 4096)
3302 convert_from_4k_imsm_migr_rec(super);
3303 /* update checkpoint information in metadata */
3304 len = imsm_create_metadata_checkpoint_update(super, &u);
3305 if (len <= 0) {
3306 dprintf("imsm: Cannot prepare update\n");
3307 goto out;
3308 }
3309 /* update metadata locally */
3310 imsm_update_metadata_locally(st, u, len);
3311 /* and possibly remotely */
3312 if (st->update_tail) {
3313 append_metadata_update(st, u, len);
3314 /* during reshape we do all work inside metadata handler
3315 * manage_reshape(), so metadata update has to be triggered
3316 * insida it
3317 */
3318 flush_metadata_updates(st);
3319 st->update_tail = &st->updates;
3320 } else
3321 free(u);
3322
3323 retval = 0;
3324 out:
3325 return retval;
3326 }
3327
3328 /* spare/missing disks activations are not allowe when
3329 * array/container performs reshape operation, because
3330 * all arrays in container works on the same disks set
3331 */
3332 int imsm_reshape_blocks_arrays_changes(struct intel_super *super)
3333 {
3334 int rv = 0;
3335 struct intel_dev *i_dev;
3336 struct imsm_dev *dev;
3337
3338 /* check whole container
3339 */
3340 for (i_dev = super->devlist; i_dev; i_dev = i_dev->next) {
3341 dev = i_dev->dev;
3342 if (is_gen_migration(dev)) {
3343 /* No repair during any migration in container
3344 */
3345 rv = 1;
3346 break;
3347 }
3348 }
3349 return rv;
3350 }
3351 static unsigned long long imsm_component_size_alignment_check(int level,
3352 int chunk_size,
3353 unsigned int sector_size,
3354 unsigned long long component_size)
3355 {
3356 unsigned int component_size_alignment;
3357
3358 /* check component size alignment
3359 */
3360 component_size_alignment = component_size % (chunk_size/sector_size);
3361
3362 dprintf("(Level: %i, chunk_size = %i, component_size = %llu), component_size_alignment = %u\n",
3363 level, chunk_size, component_size,
3364 component_size_alignment);
3365
3366 if (component_size_alignment && (level != 1) && (level != UnSet)) {
3367 dprintf("imsm: reported component size aligned from %llu ",
3368 component_size);
3369 component_size -= component_size_alignment;
3370 dprintf_cont("to %llu (%i).\n",
3371 component_size, component_size_alignment);
3372 }
3373
3374 return component_size;
3375 }
3376
3377 /*******************************************************************************
3378 * Function: get_bitmap_header_sector
3379 * Description: Returns the sector where the bitmap header is placed.
3380 * Parameters:
3381 * st : supertype information
3382 * dev_idx : index of the device with bitmap
3383 *
3384 * Returns:
3385 * The sector where the bitmap header is placed
3386 ******************************************************************************/
3387 static unsigned long long get_bitmap_header_sector(struct intel_super *super,
3388 int dev_idx)
3389 {
3390 struct imsm_dev *dev = get_imsm_dev(super, dev_idx);
3391 struct imsm_map *map = get_imsm_map(dev, MAP_0);
3392
3393 if (!super->sector_size) {
3394 dprintf("sector size is not set\n");
3395 return 0;
3396 }
3397
3398 return pba_of_lba0(map) + calc_component_size(map, dev) +
3399 (IMSM_BITMAP_HEADER_OFFSET / super->sector_size);
3400 }
3401
3402 /*******************************************************************************
3403 * Function: get_bitmap_sector
3404 * Description: Returns the sector where the bitmap is placed.
3405 * Parameters:
3406 * st : supertype information
3407 * dev_idx : index of the device with bitmap
3408 *
3409 * Returns:
3410 * The sector where the bitmap is placed
3411 ******************************************************************************/
3412 static unsigned long long get_bitmap_sector(struct intel_super *super,
3413 int dev_idx)
3414 {
3415 if (!super->sector_size) {
3416 dprintf("sector size is not set\n");
3417 return 0;
3418 }
3419
3420 return get_bitmap_header_sector(super, dev_idx) +
3421 (IMSM_BITMAP_HEADER_SIZE / super->sector_size);
3422 }
3423
3424 static unsigned long long get_ppl_sector(struct intel_super *super, int dev_idx)
3425 {
3426 struct imsm_dev *dev = get_imsm_dev(super, dev_idx);
3427 struct imsm_map *map = get_imsm_map(dev, MAP_0);
3428
3429 return pba_of_lba0(map) +
3430 (num_data_stripes(map) * map->blocks_per_strip);
3431 }
3432
3433 static void getinfo_super_imsm_volume(struct supertype *st, struct mdinfo *info, char *dmap)
3434 {
3435 struct intel_super *super = st->sb;
3436 struct migr_record *migr_rec = super->migr_rec;
3437 struct imsm_dev *dev = get_imsm_dev(super, super->current_vol);
3438 struct imsm_map *map = get_imsm_map(dev, MAP_0);
3439 struct imsm_map *prev_map = get_imsm_map(dev, MAP_1);
3440 struct imsm_map *map_to_analyse = map;
3441 struct dl *dl;
3442 int map_disks = info->array.raid_disks;
3443
3444 memset(info, 0, sizeof(*info));
3445 if (prev_map)
3446 map_to_analyse = prev_map;
3447
3448 dl = super->current_disk;
3449
3450 info->container_member = super->current_vol;
3451 info->array.raid_disks = map->num_members;
3452 info->array.level = get_imsm_raid_level(map_to_analyse);
3453 info->array.layout = imsm_level_to_layout(info->array.level);
3454 info->array.md_minor = -1;
3455 info->array.ctime = 0;
3456 info->array.utime = 0;
3457 info->array.chunk_size =
3458 __le16_to_cpu(map_to_analyse->blocks_per_strip) << 9;
3459 info->array.state = !(dev->vol.dirty & RAIDVOL_DIRTY);
3460 info->custom_array_size = imsm_dev_size(dev);
3461 info->recovery_blocked = imsm_reshape_blocks_arrays_changes(st->sb);
3462
3463 if (is_gen_migration(dev)) {
3464 /*
3465 * device prev_map should be added if it is in the middle
3466 * of migration
3467 */
3468 assert(prev_map);
3469
3470 info->reshape_active = 1;
3471 info->new_level = get_imsm_raid_level(map);
3472 info->new_layout = imsm_level_to_layout(info->new_level);
3473 info->new_chunk = __le16_to_cpu(map->blocks_per_strip) << 9;
3474 info->delta_disks = map->num_members - prev_map->num_members;
3475 if (info->delta_disks) {
3476 /* this needs to be applied to every array
3477 * in the container.
3478 */
3479 info->reshape_active = CONTAINER_RESHAPE;
3480 }
3481 /* We shape information that we give to md might have to be
3482 * modify to cope with md's requirement for reshaping arrays.
3483 * For example, when reshaping a RAID0, md requires it to be
3484 * presented as a degraded RAID4.
3485 * Also if a RAID0 is migrating to a RAID5 we need to specify
3486 * the array as already being RAID5, but the 'before' layout
3487 * is a RAID4-like layout.
3488 */
3489 switch (info->array.level) {
3490 case 0:
3491 switch(info->new_level) {
3492 case 0:
3493 /* conversion is happening as RAID4 */
3494 info->array.level = 4;
3495 info->array.raid_disks += 1;
3496 break;
3497 case 5:
3498 /* conversion is happening as RAID5 */
3499 info->array.level = 5;
3500 info->array.layout = ALGORITHM_PARITY_N;
3501 info->delta_disks -= 1;
3502 break;
3503 default:
3504 /* FIXME error message */
3505 info->array.level = UnSet;
3506 break;
3507 }
3508 break;
3509 }
3510 } else {
3511 info->new_level = UnSet;
3512 info->new_layout = UnSet;
3513 info->new_chunk = info->array.chunk_size;
3514 info->delta_disks = 0;
3515 }
3516
3517 if (dl) {
3518 info->disk.major = dl->major;
3519 info->disk.minor = dl->minor;
3520 info->disk.number = dl->index;
3521 info->disk.raid_disk = get_imsm_disk_slot(map_to_analyse,
3522 dl->index);
3523 }
3524
3525 info->data_offset = pba_of_lba0(map_to_analyse);
3526 info->component_size = calc_component_size(map, dev);
3527 info->component_size = imsm_component_size_alignment_check(
3528 info->array.level,
3529 info->array.chunk_size,
3530 super->sector_size,
3531 info->component_size);
3532 info->bb.supported = 1;
3533
3534 memset(info->uuid, 0, sizeof(info->uuid));
3535 info->recovery_start = MaxSector;
3536
3537 if (info->array.level == 5 &&
3538 (dev->rwh_policy == RWH_DISTRIBUTED ||
3539 dev->rwh_policy == RWH_MULTIPLE_DISTRIBUTED)) {
3540 info->consistency_policy = CONSISTENCY_POLICY_PPL;
3541 info->ppl_sector = get_ppl_sector(super, super->current_vol);
3542 if (dev->rwh_policy == RWH_MULTIPLE_DISTRIBUTED)
3543 info->ppl_size = MULTIPLE_PPL_AREA_SIZE_IMSM >> 9;
3544 else
3545 info->ppl_size = (PPL_HEADER_SIZE + PPL_ENTRY_SPACE)
3546 >> 9;
3547 } else if (info->array.level <= 0) {
3548 info->consistency_policy = CONSISTENCY_POLICY_NONE;
3549 } else {
3550 if (dev->rwh_policy == RWH_BITMAP) {
3551 info->bitmap_offset = get_bitmap_sector(super, super->current_vol);
3552 info->consistency_policy = CONSISTENCY_POLICY_BITMAP;
3553 } else {
3554 info->consistency_policy = CONSISTENCY_POLICY_RESYNC;
3555 }
3556 }
3557
3558 info->reshape_progress = 0;
3559 info->resync_start = MaxSector;
3560 if ((map_to_analyse->map_state == IMSM_T_STATE_UNINITIALIZED ||
3561 !(info->array.state & 1)) &&
3562 imsm_reshape_blocks_arrays_changes(super) == 0) {
3563 info->resync_start = 0;
3564 }
3565 if (dev->vol.migr_state) {
3566 switch (migr_type(dev)) {
3567 case MIGR_REPAIR:
3568 case MIGR_INIT: {
3569 __u64 blocks_per_unit = blocks_per_migr_unit(super,
3570 dev);
3571 __u64 units = vol_curr_migr_unit(dev);
3572
3573 info->resync_start = blocks_per_unit * units;
3574 break;
3575 }
3576 case MIGR_GEN_MIGR: {
3577 __u64 blocks_per_unit = blocks_per_migr_unit(super,
3578 dev);
3579 __u64 units = current_migr_unit(migr_rec);
3580 int used_disks;
3581
3582 if (__le32_to_cpu(migr_rec->ascending_migr) &&
3583 (units <
3584 (get_num_migr_units(migr_rec)-1)) &&
3585 (super->migr_rec->rec_status ==
3586 __cpu_to_le32(UNIT_SRC_IN_CP_AREA)))
3587 units++;
3588
3589 info->reshape_progress = blocks_per_unit * units;
3590
3591 dprintf("IMSM: General Migration checkpoint : %llu (%llu) -> read reshape progress : %llu\n",
3592 (unsigned long long)units,
3593 (unsigned long long)blocks_per_unit,
3594 info->reshape_progress);
3595
3596 used_disks = imsm_num_data_members(prev_map);
3597 if (used_disks > 0) {
3598 info->custom_array_size = per_dev_array_size(map) *
3599 used_disks;
3600 }
3601 }
3602 case MIGR_VERIFY:
3603 /* we could emulate the checkpointing of
3604 * 'sync_action=check' migrations, but for now
3605 * we just immediately complete them
3606 */
3607 case MIGR_REBUILD:
3608 /* this is handled by container_content_imsm() */
3609 case MIGR_STATE_CHANGE:
3610 /* FIXME handle other migrations */
3611 default:
3612 /* we are not dirty, so... */
3613 info->resync_start = MaxSector;
3614 }
3615 }
3616
3617 strncpy(info->name, (char *) dev->volume, MAX_RAID_SERIAL_LEN);
3618 info->name[MAX_RAID_SERIAL_LEN] = 0;
3619
3620 info->array.major_version = -1;
3621 info->array.minor_version = -2;
3622 sprintf(info->text_version, "/%s/%d", st->container_devnm, info->container_member);
3623 info->safe_mode_delay = 4000; /* 4 secs like the Matrix driver */
3624 uuid_from_super_imsm(st, info->uuid);
3625
3626 if (dmap) {
3627 int i, j;
3628 for (i=0; i<map_disks; i++) {
3629 dmap[i] = 0;
3630 if (i < info->array.raid_disks) {
3631 struct imsm_disk *dsk;
3632 j = get_imsm_disk_idx(dev, i, MAP_X);
3633 dsk = get_imsm_disk(super, j);
3634 if (dsk && (dsk->status & CONFIGURED_DISK))
3635 dmap[i] = 1;
3636 }
3637 }
3638 }
3639 }
3640
3641 static __u8 imsm_check_degraded(struct intel_super *super, struct imsm_dev *dev,
3642 int failed, int look_in_map);
3643
3644 static int imsm_count_failed(struct intel_super *super, struct imsm_dev *dev,
3645 int look_in_map);
3646
3647 static void manage_second_map(struct intel_super *super, struct imsm_dev *dev)
3648 {
3649 if (is_gen_migration(dev)) {
3650 int failed;
3651 __u8 map_state;
3652 struct imsm_map *map2 = get_imsm_map(dev, MAP_1);
3653
3654 failed = imsm_count_failed(super, dev, MAP_1);
3655 map_state = imsm_check_degraded(super, dev, failed, MAP_1);
3656 if (map2->map_state != map_state) {
3657 map2->map_state = map_state;
3658 super->updates_pending++;
3659 }
3660 }
3661 }
3662
3663 static struct imsm_disk *get_imsm_missing(struct intel_super *super, __u8 index)
3664 {
3665 struct dl *d;
3666
3667 for (d = super->missing; d; d = d->next)
3668 if (d->index == index)
3669 return &d->disk;
3670 return NULL;
3671 }
3672
3673 static void getinfo_super_imsm(struct supertype *st, struct mdinfo *info, char *map)
3674 {
3675 struct intel_super *super = st->sb;
3676 struct imsm_disk *disk;
3677 int map_disks = info->array.raid_disks;
3678 int max_enough = -1;
3679 int i;
3680 struct imsm_super *mpb;
3681
3682 if (super->current_vol >= 0) {
3683 getinfo_super_imsm_volume(st, info, map);
3684 return;
3685 }
3686 memset(info, 0, sizeof(*info));
3687
3688 /* Set raid_disks to zero so that Assemble will always pull in valid
3689 * spares
3690 */
3691 info->array.raid_disks = 0;
3692 info->array.level = LEVEL_CONTAINER;
3693 info->array.layout = 0;
3694 info->array.md_minor = -1;
3695 info->array.ctime = 0; /* N/A for imsm */
3696 info->array.utime = 0;
3697 info->array.chunk_size = 0;
3698
3699 info->disk.major = 0;
3700 info->disk.minor = 0;
3701 info->disk.raid_disk = -1;
3702 info->reshape_active = 0;
3703 info->array.major_version = -1;
3704 info->array.minor_version = -2;
3705 strcpy(info->text_version, "imsm");
3706 info->safe_mode_delay = 0;
3707 info->disk.number = -1;
3708 info->disk.state = 0;
3709 info->name[0] = 0;
3710 info->recovery_start = MaxSector;
3711 info->recovery_blocked = imsm_reshape_blocks_arrays_changes(st->sb);
3712 info->bb.supported = 1;
3713
3714 /* do we have the all the insync disks that we expect? */
3715 mpb = super->anchor;
3716 info->events = __le32_to_cpu(mpb->generation_num);
3717
3718 for (i = 0; i < mpb->num_raid_devs; i++) {
3719 struct imsm_dev *dev = get_imsm_dev(super, i);
3720 int failed, enough, j, missing = 0;
3721 struct imsm_map *map;
3722 __u8 state;
3723
3724 failed = imsm_count_failed(super, dev, MAP_0);
3725 state = imsm_check_degraded(super, dev, failed, MAP_0);
3726 map = get_imsm_map(dev, MAP_0);
3727
3728 /* any newly missing disks?
3729 * (catches single-degraded vs double-degraded)
3730 */
3731 for (j = 0; j < map->num_members; j++) {
3732 __u32 ord = get_imsm_ord_tbl_ent(dev, j, MAP_0);
3733 __u32 idx = ord_to_idx(ord);
3734
3735 if (super->disks && super->disks->index == (int)idx)
3736 info->disk.raid_disk = j;
3737
3738 if (!(ord & IMSM_ORD_REBUILD) &&
3739 get_imsm_missing(super, idx)) {
3740 missing = 1;
3741 break;
3742 }
3743 }
3744
3745 if (state == IMSM_T_STATE_FAILED)
3746 enough = -1;
3747 else if (state == IMSM_T_STATE_DEGRADED &&
3748 (state != map->map_state || missing))
3749 enough = 0;
3750 else /* we're normal, or already degraded */
3751 enough = 1;
3752 if (is_gen_migration(dev) && missing) {
3753 /* during general migration we need all disks
3754 * that process is running on.
3755 * No new missing disk is allowed.
3756 */
3757 max_enough = -1;
3758 enough = -1;
3759 /* no more checks necessary
3760 */
3761 break;
3762 }
3763 /* in the missing/failed disk case check to see
3764 * if at least one array is runnable
3765 */
3766 max_enough = max(max_enough, enough);
3767 }
3768 dprintf("enough: %d\n", max_enough);
3769 info->container_enough = max_enough;
3770
3771 if (super->disks) {
3772 __u32 reserved = imsm_reserved_sectors(super, super->disks);
3773
3774 disk = &super->disks->disk;
3775 info->data_offset = total_blocks(&super->disks->disk) - reserved;
3776 info->component_size = reserved;
3777 info->disk.state = is_configured(disk) ? (1 << MD_DISK_ACTIVE) : 0;
3778 /* we don't change info->disk.raid_disk here because
3779 * this state will be finalized in mdmon after we have
3780 * found the 'most fresh' version of the metadata
3781 */
3782 info->disk.state |= is_failed(disk) ? (1 << MD_DISK_FAULTY) : 0;
3783 info->disk.state |= (is_spare(disk) || is_journal(disk)) ?
3784 0 : (1 << MD_DISK_SYNC);
3785 }
3786
3787 /* only call uuid_from_super_imsm when this disk is part of a populated container,
3788 * ->compare_super may have updated the 'num_raid_devs' field for spares
3789 */
3790 if (info->disk.state & (1 << MD_DISK_SYNC) || super->anchor->num_raid_devs)
3791 uuid_from_super_imsm(st, info->uuid);
3792 else
3793 memcpy(info->uuid, uuid_zero, sizeof(uuid_zero));
3794
3795 /* I don't know how to compute 'map' on imsm, so use safe default */
3796 if (map) {
3797 int i;
3798 for (i = 0; i < map_disks; i++)
3799 map[i] = 1;
3800 }
3801
3802 }
3803
3804 /* allocates memory and fills disk in mdinfo structure
3805 * for each disk in array */
3806 struct mdinfo *getinfo_super_disks_imsm(struct supertype *st)
3807 {
3808 struct mdinfo *mddev;
3809 struct intel_super *super = st->sb;
3810 struct imsm_disk *disk;
3811 int count = 0;
3812 struct dl *dl;
3813 if (!super || !super->disks)
3814 return NULL;
3815 dl = super->disks;
3816 mddev = xcalloc(1, sizeof(*mddev));
3817 while (dl) {
3818 struct mdinfo *tmp;
3819 disk = &dl->disk;
3820 tmp = xcalloc(1, sizeof(*tmp));
3821 if (mddev->devs)
3822 tmp->next = mddev->devs;
3823 mddev->devs = tmp;
3824 tmp->disk.number = count++;
3825 tmp->disk.major = dl->major;
3826 tmp->disk.minor = dl->minor;
3827 tmp->disk.state = is_configured(disk) ?
3828 (1 << MD_DISK_ACTIVE) : 0;
3829 tmp->disk.state |= is_failed(disk) ? (1 << MD_DISK_FAULTY) : 0;
3830 tmp->disk.state |= is_spare(disk) ? 0 : (1 << MD_DISK_SYNC);
3831 tmp->disk.raid_disk = -1;
3832 dl = dl->next;
3833 }
3834 return mddev;
3835 }
3836
3837 static int update_super_imsm(struct supertype *st, struct mdinfo *info,
3838 char *update, char *devname, int verbose,
3839 int uuid_set, char *homehost)
3840 {
3841 /* For 'assemble' and 'force' we need to return non-zero if any
3842 * change was made. For others, the return value is ignored.
3843 * Update options are:
3844 * force-one : This device looks a bit old but needs to be included,
3845 * update age info appropriately.
3846 * assemble: clear any 'faulty' flag to allow this device to
3847 * be assembled.
3848 * force-array: Array is degraded but being forced, mark it clean
3849 * if that will be needed to assemble it.
3850 *
3851 * newdev: not used ????
3852 * grow: Array has gained a new device - this is currently for
3853 * linear only
3854 * resync: mark as dirty so a resync will happen.
3855 * name: update the name - preserving the homehost
3856 * uuid: Change the uuid of the array to match watch is given
3857 *
3858 * Following are not relevant for this imsm:
3859 * sparc2.2 : update from old dodgey metadata
3860 * super-minor: change the preferred_minor number
3861 * summaries: update redundant counters.
3862 * homehost: update the recorded homehost
3863 * _reshape_progress: record new reshape_progress position.
3864 */
3865 int rv = 1;
3866 struct intel_super *super = st->sb;
3867 struct imsm_super *mpb;
3868
3869 /* we can only update container info */
3870 if (!super || super->current_vol >= 0 || !super->anchor)
3871 return 1;
3872
3873 mpb = super->anchor;
3874
3875 if (strcmp(update, "uuid") == 0) {
3876 /* We take this to mean that the family_num should be updated.
3877 * However that is much smaller than the uuid so we cannot really
3878 * allow an explicit uuid to be given. And it is hard to reliably
3879 * know if one was.
3880 * So if !uuid_set we know the current uuid is random and just used
3881 * the first 'int' and copy it to the other 3 positions.
3882 * Otherwise we require the 4 'int's to be the same as would be the
3883 * case if we are using a random uuid. So an explicit uuid will be
3884 * accepted as long as all for ints are the same... which shouldn't hurt
3885 */
3886 if (!uuid_set) {
3887 info->uuid[1] = info->uuid[2] = info->uuid[3] = info->uuid[0];
3888 rv = 0;
3889 } else {
3890 if (info->uuid[0] != info->uuid[1] ||
3891 info->uuid[1] != info->uuid[2] ||
3892 info->uuid[2] != info->uuid[3])
3893 rv = -1;
3894 else
3895 rv = 0;
3896 }
3897 if (rv == 0)
3898 mpb->orig_family_num = info->uuid[0];
3899 } else if (strcmp(update, "assemble") == 0)
3900 rv = 0;
3901 else
3902 rv = -1;
3903
3904 /* successful update? recompute checksum */
3905 if (rv == 0)
3906 mpb->check_sum = __le32_to_cpu(__gen_imsm_checksum(mpb));
3907
3908 return rv;
3909 }
3910
3911 static size_t disks_to_mpb_size(int disks)
3912 {
3913 size_t size;
3914
3915 size = sizeof(struct imsm_super);
3916 size += (disks - 1) * sizeof(struct imsm_disk);
3917 size += 2 * sizeof(struct imsm_dev);
3918 /* up to 2 maps per raid device (-2 for imsm_maps in imsm_dev */
3919 size += (4 - 2) * sizeof(struct imsm_map);
3920 /* 4 possible disk_ord_tbl's */
3921 size += 4 * (disks - 1) * sizeof(__u32);
3922 /* maximum bbm log */
3923 size += sizeof(struct bbm_log);
3924
3925 return size;
3926 }
3927
3928 static __u64 avail_size_imsm(struct supertype *st, __u64 devsize,
3929 unsigned long long data_offset)
3930 {
3931 if (devsize < (MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS))
3932 return 0;
3933
3934 return devsize - (MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS);
3935 }
3936
3937 static void free_devlist(struct intel_super *super)
3938 {
3939 struct intel_dev *dv;
3940
3941 while (super->devlist) {
3942 dv = super->devlist->next;
3943 free(super->devlist->dev);
3944 free(super->devlist);
3945 super->devlist = dv;
3946 }
3947 }
3948
3949 static void imsm_copy_dev(struct imsm_dev *dest, struct imsm_dev *src)
3950 {
3951 memcpy(dest, src, sizeof_imsm_dev(src, 0));
3952 }
3953
3954 static int compare_super_imsm(struct supertype *st, struct supertype *tst,
3955 int verbose)
3956 {
3957 /* return:
3958 * 0 same, or first was empty, and second was copied
3959 * 1 sb are different
3960 */
3961 struct intel_super *first = st->sb;
3962 struct intel_super *sec = tst->sb;
3963
3964 if (!first) {
3965 st->sb = tst->sb;
3966 tst->sb = NULL;
3967 return 0;
3968 }
3969
3970 /* in platform dependent environment test if the disks
3971 * use the same Intel hba
3972 * if not on Intel hba at all, allow anything.
3973 * doesn't check HBAs if num_raid_devs is not set, as it means
3974 * it is a free floating spare, and all spares regardless of HBA type
3975 * will fall into separate container during the assembly
3976 */
3977 if (first->hba && sec->hba && first->anchor->num_raid_devs != 0) {
3978 if (first->hba->type != sec->hba->type) {
3979 if (verbose)
3980 pr_err("HBAs of devices do not match %s != %s\n",
3981 get_sys_dev_type(first->hba->type),
3982 get_sys_dev_type(sec->hba->type));
3983 return 1;
3984 }
3985 if (first->orom != sec->orom) {
3986 if (verbose)
3987 pr_err("HBAs of devices do not match %s != %s\n",
3988 first->hba->pci_id, sec->hba->pci_id);
3989 return 1;
3990 }
3991 }
3992
3993 if (first->anchor->num_raid_devs > 0 &&
3994 sec->anchor->num_raid_devs > 0) {
3995 /* Determine if these disks might ever have been
3996 * related. Further disambiguation can only take place
3997 * in load_super_imsm_all
3998 */
3999 __u32 first_family = first->anchor->orig_family_num;
4000 __u32 sec_family = sec->anchor->orig_family_num;
4001
4002 if (memcmp(first->anchor->sig, sec->anchor->sig,
4003 MAX_SIGNATURE_LENGTH) != 0)
4004 return 1;
4005
4006 if (first_family == 0)
4007 first_family = first->anchor->family_num;
4008 if (sec_family == 0)
4009 sec_family = sec->anchor->family_num;
4010
4011 if (first_family != sec_family)
4012 return 1;
4013
4014 }
4015
4016 /* if an anchor does not have num_raid_devs set then it is a free
4017 * floating spare. don't assosiate spare with any array, as during assembly
4018 * spares shall fall into separate container, from which they can be moved
4019 * when necessary
4020 */
4021 if (first->anchor->num_raid_devs ^ sec->anchor->num_raid_devs)
4022 return 1;
4023
4024 return 0;
4025 }
4026
4027 static void fd2devname(int fd, char *name)
4028 {
4029 char *nm;
4030
4031 nm = fd2kname(fd);
4032 if (!nm)
4033 return;
4034
4035 snprintf(name, MAX_RAID_SERIAL_LEN, "/dev/%s", nm);
4036 }
4037
4038 static int nvme_get_serial(int fd, void *buf, size_t buf_len)
4039 {
4040 char path[PATH_MAX];
4041 char *name = fd2kname(fd);
4042
4043 if (!name)
4044 return 1;
4045
4046 if (strncmp(name, "nvme", 4) != 0)
4047 return 1;
4048
4049 if (!diskfd_to_devpath(fd, 1, path))
4050 return 1;
4051
4052 return devpath_to_char(path, "serial", buf, buf_len, 0);
4053 }
4054
4055 extern int scsi_get_serial(int fd, void *buf, size_t buf_len);
4056
4057 static int imsm_read_serial(int fd, char *devname,
4058 __u8 *serial, size_t serial_buf_len)
4059 {
4060 char buf[50];
4061 int rv;
4062 size_t len;
4063 char *dest;
4064 char *src;
4065 unsigned int i;
4066
4067 memset(buf, 0, sizeof(buf));
4068
4069 rv = nvme_get_serial(fd, buf, sizeof(buf));
4070
4071 if (rv)
4072 rv = scsi_get_serial(fd, buf, sizeof(buf));
4073
4074 if (rv && check_env("IMSM_DEVNAME_AS_SERIAL")) {
4075 memset(serial, 0, MAX_RAID_SERIAL_LEN);
4076 fd2devname(fd, (char *) serial);
4077 return 0;
4078 }
4079
4080 if (rv != 0) {
4081 if (devname)
4082 pr_err("Failed to retrieve serial for %s\n",
4083 devname);
4084 return rv;
4085 }
4086
4087 /* trim all whitespace and non-printable characters and convert
4088 * ':' to ';'
4089 */
4090 for (i = 0, dest = buf; i < sizeof(buf) && buf[i]; i++) {
4091 src = &buf[i];
4092 if (*src > 0x20) {
4093 /* ':' is reserved for use in placeholder serial
4094 * numbers for missing disks
4095 */
4096 if (*src == ':')
4097 *dest++ = ';';
4098 else
4099 *dest++ = *src;
4100 }
4101 }
4102 len = dest - buf;
4103 dest = buf;
4104
4105 if (len > serial_buf_len) {
4106 /* truncate leading characters */
4107 dest += len - serial_buf_len;
4108 len = serial_buf_len;
4109 }
4110
4111 memset(serial, 0, serial_buf_len);
4112 memcpy(serial, dest, len);
4113
4114 return 0;
4115 }
4116
4117 static int serialcmp(__u8 *s1, __u8 *s2)
4118 {
4119 return strncmp((char *) s1, (char *) s2, MAX_RAID_SERIAL_LEN);
4120 }
4121
4122 static void serialcpy(__u8 *dest, __u8 *src)
4123 {
4124 strncpy((char *) dest, (char *) src, MAX_RAID_SERIAL_LEN);
4125 }
4126
4127 static struct dl *serial_to_dl(__u8 *serial, struct intel_super *super)
4128 {
4129 struct dl *dl;
4130
4131 for (dl = super->disks; dl; dl = dl->next)
4132 if (serialcmp(dl->serial, serial) == 0)
4133 break;
4134
4135 return dl;
4136 }
4137
4138 static struct imsm_disk *
4139 __serial_to_disk(__u8 *serial, struct imsm_super *mpb, int *idx)
4140 {
4141 int i;
4142
4143 for (i = 0; i < mpb->num_disks; i++) {
4144 struct imsm_disk *disk = __get_imsm_disk(mpb, i);
4145
4146 if (serialcmp(disk->serial, serial) == 0) {
4147 if (idx)
4148 *idx = i;
4149 return disk;
4150 }
4151 }
4152
4153 return NULL;
4154 }
4155
4156 static int
4157 load_imsm_disk(int fd, struct intel_super *super, char *devname, int keep_fd)
4158 {
4159 struct imsm_disk *disk;
4160 struct dl *dl;
4161 struct stat stb;
4162 int rv;
4163 char name[40];
4164 __u8 serial[MAX_RAID_SERIAL_LEN];
4165
4166 rv = imsm_read_serial(fd, devname, serial, MAX_RAID_SERIAL_LEN);
4167
4168 if (rv != 0)
4169 return 2;
4170
4171 dl = xcalloc(1, sizeof(*dl));
4172
4173 fstat(fd, &stb);
4174 dl->major = major(stb.st_rdev);
4175 dl->minor = minor(stb.st_rdev);
4176 dl->next = super->disks;
4177 dl->fd = keep_fd ? fd : -1;
4178 assert(super->disks == NULL);
4179 super->disks = dl;
4180 serialcpy(dl->serial, serial);
4181 dl->index = -2;
4182 dl->e = NULL;
4183 fd2devname(fd, name);
4184 if (devname)
4185 dl->devname = xstrdup(devname);
4186 else
4187 dl->devname = xstrdup(name);
4188
4189 /* look up this disk's index in the current anchor */
4190 disk = __serial_to_disk(dl->serial, super->anchor, &dl->index);
4191 if (disk) {
4192 dl->disk = *disk;
4193 /* only set index on disks that are a member of a
4194 * populated contianer, i.e. one with raid_devs
4195 */
4196 if (is_failed(&dl->disk))
4197 dl->index = -2;
4198 else if (is_spare(&dl->disk) || is_journal(&dl->disk))
4199 dl->index = -1;
4200 }
4201
4202 return 0;
4203 }
4204
4205 /* When migrating map0 contains the 'destination' state while map1
4206 * contains the current state. When not migrating map0 contains the
4207 * current state. This routine assumes that map[0].map_state is set to
4208 * the current array state before being called.
4209 *
4210 * Migration is indicated by one of the following states
4211 * 1/ Idle (migr_state=0 map0state=normal||unitialized||degraded||failed)
4212 * 2/ Initialize (migr_state=1 migr_type=MIGR_INIT map0state=normal
4213 * map1state=unitialized)
4214 * 3/ Repair (Resync) (migr_state=1 migr_type=MIGR_REPAIR map0state=normal
4215 * map1state=normal)
4216 * 4/ Rebuild (migr_state=1 migr_type=MIGR_REBUILD map0state=normal
4217 * map1state=degraded)
4218 * 5/ Migration (mig_state=1 migr_type=MIGR_GEN_MIGR map0state=normal
4219 * map1state=normal)
4220 */
4221 static void migrate(struct imsm_dev *dev, struct intel_super *super,
4222 __u8 to_state, int migr_type)
4223 {
4224 struct imsm_map *dest;
4225 struct imsm_map *src = get_imsm_map(dev, MAP_0);
4226
4227 dev->vol.migr_state = 1;
4228 set_migr_type(dev, migr_type);
4229 set_vol_curr_migr_unit(dev, 0);
4230 dest = get_imsm_map(dev, MAP_1);
4231
4232 /* duplicate and then set the target end state in map[0] */
4233 memcpy(dest, src, sizeof_imsm_map(src));
4234 if (migr_type == MIGR_GEN_MIGR) {
4235 __u32 ord;
4236 int i;
4237
4238 for (i = 0; i < src->num_members; i++) {
4239 ord = __le32_to_cpu(src->disk_ord_tbl[i]);
4240 set_imsm_ord_tbl_ent(src, i, ord_to_idx(ord));
4241 }
4242 }
4243
4244 if (migr_type == MIGR_GEN_MIGR)
4245 /* Clear migration record */
4246 memset(super->migr_rec, 0, sizeof(struct migr_record));
4247
4248 src->map_state = to_state;
4249 }
4250
4251 static void end_migration(struct imsm_dev *dev, struct intel_super *super,
4252 __u8 map_state)
4253 {
4254 struct imsm_map *map = get_imsm_map(dev, MAP_0);
4255 struct imsm_map *prev = get_imsm_map(dev, dev->vol.migr_state == 0 ?
4256 MAP_0 : MAP_1);
4257 int i, j;
4258
4259 /* merge any IMSM_ORD_REBUILD bits that were not successfully
4260 * completed in the last migration.
4261 *
4262 * FIXME add support for raid-level-migration
4263 */
4264 if (map_state != map->map_state && (is_gen_migration(dev) == false) &&
4265 prev->map_state != IMSM_T_STATE_UNINITIALIZED) {
4266 /* when final map state is other than expected
4267 * merge maps (not for migration)
4268 */
4269 int failed;
4270
4271 for (i = 0; i < prev->num_members; i++)
4272 for (j = 0; j < map->num_members; j++)
4273 /* during online capacity expansion
4274 * disks position can be changed
4275 * if takeover is used
4276 */
4277 if (ord_to_idx(map->disk_ord_tbl[j]) ==
4278 ord_to_idx(prev->disk_ord_tbl[i])) {
4279 map->disk_ord_tbl[j] |=
4280 prev->disk_ord_tbl[i];
4281 break;
4282 }
4283 failed = imsm_count_failed(super, dev, MAP_0);
4284 map_state = imsm_check_degraded(super, dev, failed, MAP_0);
4285 }
4286
4287 dev->vol.migr_state = 0;
4288 set_migr_type(dev, 0);
4289 set_vol_curr_migr_unit(dev, 0);
4290 map->map_state = map_state;
4291 }
4292
4293 static int parse_raid_devices(struct intel_super *super)
4294 {
4295 int i;
4296 struct imsm_dev *dev_new;
4297 size_t len, len_migr;
4298 size_t max_len = 0;
4299 size_t space_needed = 0;
4300 struct imsm_super *mpb = super->anchor;
4301
4302 for (i = 0; i < super->anchor->num_raid_devs; i++) {
4303 struct imsm_dev *dev_iter = __get_imsm_dev(super->anchor, i);
4304 struct intel_dev *dv;
4305
4306 len = sizeof_imsm_dev(dev_iter, 0);
4307 len_migr = sizeof_imsm_dev(dev_iter, 1);
4308 if (len_migr > len)
4309 space_needed += len_migr - len;
4310
4311 dv = xmalloc(sizeof(*dv));
4312 if (max_len < len_migr)
4313 max_len = len_migr;
4314 if (max_len > len_migr)
4315 space_needed += max_len - len_migr;
4316 dev_new = xmalloc(max_len);
4317 imsm_copy_dev(dev_new, dev_iter);
4318 dv->dev = dev_new;
4319 dv->index = i;
4320 dv->next = super->devlist;
4321 super->devlist = dv;
4322 }
4323
4324 /* ensure that super->buf is large enough when all raid devices
4325 * are migrating
4326 */
4327 if (__le32_to_cpu(mpb->mpb_size) + space_needed > super->len) {
4328 void *buf;
4329
4330 len = ROUND_UP(__le32_to_cpu(mpb->mpb_size) + space_needed,
4331 super->sector_size);
4332 if (posix_memalign(&buf, MAX_SECTOR_SIZE, len) != 0)
4333 return 1;
4334
4335 memcpy(buf, super->buf, super->len);
4336 memset(buf + super->len, 0, len - super->len);
4337 free(super->buf);
4338 super->buf = buf;
4339 super->len = len;
4340 }
4341
4342 super->extra_space += space_needed;
4343
4344 return 0;
4345 }
4346
4347 /*******************************************************************************
4348 * Function: check_mpb_migr_compatibility
4349 * Description: Function checks for unsupported migration features:
4350 * - migration optimization area (pba_of_lba0)
4351 * - descending reshape (ascending_migr)
4352 * Parameters:
4353 * super : imsm metadata information
4354 * Returns:
4355 * 0 : migration is compatible
4356 * -1 : migration is not compatible
4357 ******************************************************************************/
4358 int check_mpb_migr_compatibility(struct intel_super *super)
4359 {
4360 struct imsm_map *map0, *map1;
4361 struct migr_record *migr_rec = super->migr_rec;
4362 int i;
4363
4364 for (i = 0; i < super->anchor->num_raid_devs; i++) {
4365 struct imsm_dev *dev_iter = __get_imsm_dev(super->anchor, i);
4366
4367 if (dev_iter &&
4368 dev_iter->vol.migr_state == 1 &&
4369 dev_iter->vol.migr_type == MIGR_GEN_MIGR) {
4370 /* This device is migrating */
4371 map0 = get_imsm_map(dev_iter, MAP_0);
4372 map1 = get_imsm_map(dev_iter, MAP_1);
4373 if (pba_of_lba0(map0) != pba_of_lba0(map1))
4374 /* migration optimization area was used */
4375 return -1;
4376 if (migr_rec->ascending_migr == 0 &&
4377 migr_rec->dest_depth_per_unit > 0)
4378 /* descending reshape not supported yet */
4379 return -1;
4380 }
4381 }
4382 return 0;
4383 }
4384
4385 static void __free_imsm(struct intel_super *super, int free_disks);
4386
4387 /* load_imsm_mpb - read matrix metadata
4388 * allocates super->mpb to be freed by free_imsm
4389 */
4390 static int load_imsm_mpb(int fd, struct intel_super *super, char *devname)
4391 {
4392 unsigned long long dsize;
4393 unsigned long long sectors;
4394 unsigned int sector_size = super->sector_size;
4395 struct stat;
4396 struct imsm_super *anchor;
4397 __u32 check_sum;
4398
4399 get_dev_size(fd, NULL, &dsize);
4400 if (dsize < 2*sector_size) {
4401 if (devname)
4402 pr_err("%s: device to small for imsm\n",
4403 devname);
4404 return 1;
4405 }
4406
4407 if (lseek64(fd, dsize - (sector_size * 2), SEEK_SET) < 0) {
4408 if (devname)
4409 pr_err("Cannot seek to anchor block on %s: %s\n",
4410 devname, strerror(errno));
4411 return 1;
4412 }
4413
4414 if (posix_memalign((void **)&anchor, sector_size, sector_size) != 0) {
4415 if (devname)
4416 pr_err("Failed to allocate imsm anchor buffer on %s\n", devname);
4417 return 1;
4418 }
4419 if ((unsigned int)read(fd, anchor, sector_size) != sector_size) {
4420 if (devname)
4421 pr_err("Cannot read anchor block on %s: %s\n",
4422 devname, strerror(errno));
4423 free(anchor);
4424 return 1;
4425 }
4426
4427 if (strncmp((char *) anchor->sig, MPB_SIGNATURE, MPB_SIG_LEN) != 0) {
4428 if (devname)
4429 pr_err("no IMSM anchor on %s\n", devname);
4430 free(anchor);
4431 return 2;
4432 }
4433
4434 __free_imsm(super, 0);
4435 /* reload capability and hba */
4436
4437 /* capability and hba must be updated with new super allocation */
4438 find_intel_hba_capability(fd, super, devname);
4439 super->len = ROUND_UP(anchor->mpb_size, sector_size);
4440 if (posix_memalign(&super->buf, MAX_SECTOR_SIZE, super->len) != 0) {
4441 if (devname)
4442 pr_err("unable to allocate %zu byte mpb buffer\n",
4443 super->len);
4444 free(anchor);
4445 return 2;
4446 }
4447 memcpy(super->buf, anchor, sector_size);
4448
4449 sectors = mpb_sectors(anchor, sector_size) - 1;
4450 free(anchor);
4451
4452 if (posix_memalign(&super->migr_rec_buf, MAX_SECTOR_SIZE,
4453 MIGR_REC_BUF_SECTORS*MAX_SECTOR_SIZE) != 0) {
4454 pr_err("could not allocate migr_rec buffer\n");
4455 free(super->buf);
4456 return 2;
4457 }
4458 super->clean_migration_record_by_mdmon = 0;
4459
4460 if (!sectors) {
4461 check_sum = __gen_imsm_checksum(super->anchor);
4462 if (check_sum != __le32_to_cpu(super->anchor->check_sum)) {
4463 if (devname)
4464 pr_err("IMSM checksum %x != %x on %s\n",
4465 check_sum,
4466 __le32_to_cpu(super->anchor->check_sum),
4467 devname);
4468 return 2;
4469 }
4470
4471 return 0;
4472 }
4473
4474 /* read the extended mpb */
4475 if (lseek64(fd, dsize - (sector_size * (2 + sectors)), SEEK_SET) < 0) {
4476 if (devname)
4477 pr_err("Cannot seek to extended mpb on %s: %s\n",
4478 devname, strerror(errno));
4479 return 1;
4480 }
4481
4482 if ((unsigned int)read(fd, super->buf + sector_size,
4483 super->len - sector_size) != super->len - sector_size) {
4484 if (devname)
4485 pr_err("Cannot read extended mpb on %s: %s\n",
4486 devname, strerror(errno));
4487 return 2;
4488 }
4489
4490 check_sum = __gen_imsm_checksum(super->anchor);
4491 if (check_sum != __le32_to_cpu(super->anchor->check_sum)) {
4492 if (devname)
4493 pr_err("IMSM checksum %x != %x on %s\n",
4494 check_sum, __le32_to_cpu(super->anchor->check_sum),
4495 devname);
4496 return 3;
4497 }
4498
4499 return 0;
4500 }
4501
4502 static int read_imsm_migr_rec(int fd, struct intel_super *super);
4503
4504 /* clears hi bits in metadata if MPB_ATTRIB_2TB_DISK not set */
4505 static void clear_hi(struct intel_super *super)
4506 {
4507 struct imsm_super *mpb = super->anchor;
4508 int i, n;
4509 if (mpb->attributes & MPB_ATTRIB_2TB_DISK)
4510 return;
4511 for (i = 0; i < mpb->num_disks; ++i) {
4512 struct imsm_disk *disk = &mpb->disk[i];
4513 disk->total_blocks_hi = 0;
4514 }
4515 for (i = 0; i < mpb->num_raid_devs; ++i) {
4516 struct imsm_dev *dev = get_imsm_dev(super, i);
4517 if (!dev)
4518 return;
4519 for (n = 0; n < 2; ++n) {
4520 struct imsm_map *map = get_imsm_map(dev, n);
4521 if (!map)
4522 continue;
4523 map->pba_of_lba0_hi = 0;
4524 map->blocks_per_member_hi = 0;
4525 map->num_data_stripes_hi = 0;
4526 }
4527 }
4528 }
4529
4530 static int
4531 load_and_parse_mpb(int fd, struct intel_super *super, char *devname, int keep_fd)
4532 {
4533 int err;
4534
4535 err = load_imsm_mpb(fd, super, devname);
4536 if (err)
4537 return err;
4538 if (super->sector_size == 4096)
4539 convert_from_4k(super);
4540 err = load_imsm_disk(fd, super, devname, keep_fd);
4541 if (err)
4542 return err;
4543 err = parse_raid_devices(super);
4544 if (err)
4545 return err;
4546 err = load_bbm_log(super);
4547 clear_hi(super);
4548 return err;
4549 }
4550
4551 static void __free_imsm_disk(struct dl *d, int do_close)
4552 {
4553 if (do_close)
4554 close_fd(&d->fd);
4555 if (d->devname)
4556 free(d->devname);
4557 if (d->e)
4558 free(d->e);
4559 free(d);
4560
4561 }
4562
4563 static void free_imsm_disks(struct intel_super *super)
4564 {
4565 struct dl *d;
4566
4567 while (super->disks) {
4568 d = super->disks;
4569 super->disks = d->next;
4570 __free_imsm_disk(d, 1);
4571 }
4572 while (super->disk_mgmt_list) {
4573 d = super->disk_mgmt_list;
4574 super->disk_mgmt_list = d->next;
4575 __free_imsm_disk(d, 1);
4576 }
4577 while (super->missing) {
4578 d = super->missing;
4579 super->missing = d->next;
4580 __free_imsm_disk(d, 1);
4581 }
4582
4583 }
4584
4585 /* free all the pieces hanging off of a super pointer */
4586 static void __free_imsm(struct intel_super *super, int free_disks)
4587 {
4588 struct intel_hba *elem, *next;
4589
4590 if (super->buf) {
4591 free(super->buf);
4592 super->buf = NULL;
4593 }
4594 /* unlink capability description */
4595 super->orom = NULL;
4596 if (super->migr_rec_buf) {
4597 free(super->migr_rec_buf);
4598 super->migr_rec_buf = NULL;
4599 }
4600 if (free_disks)
4601 free_imsm_disks(super);
4602 free_devlist(super);
4603 elem = super->hba;
4604 while (elem) {
4605 if (elem->path)
4606 free((void *)elem->path);
4607 next = elem->next;
4608 free(elem);
4609 elem = next;
4610 }
4611 if (super->bbm_log)
4612 free(super->bbm_log);
4613 super->hba = NULL;
4614 }
4615
4616 static void free_imsm(struct intel_super *super)
4617 {
4618 __free_imsm(super, 1);
4619 free(super->bb.entries);
4620 free(super);
4621 }
4622
4623 static void free_super_imsm(struct supertype *st)
4624 {
4625 struct intel_super *super = st->sb;
4626
4627 if (!super)
4628 return;
4629
4630 free_imsm(super);
4631 st->sb = NULL;
4632 }
4633
4634 static struct intel_super *alloc_super(void)
4635 {
4636 struct intel_super *super = xcalloc(1, sizeof(*super));
4637
4638 super->current_vol = -1;
4639 super->create_offset = ~((unsigned long long) 0);
4640
4641 super->bb.entries = xmalloc(BBM_LOG_MAX_ENTRIES *
4642 sizeof(struct md_bb_entry));
4643 if (!super->bb.entries) {
4644 free(super);
4645 return NULL;
4646 }
4647
4648 return super;
4649 }
4650
4651 /*
4652 * find and allocate hba and OROM/EFI based on valid fd of RAID component device
4653 */
4654 static int find_intel_hba_capability(int fd, struct intel_super *super, char *devname)
4655 {
4656 struct sys_dev *hba_name;
4657 int rv = 0;
4658
4659 if (is_fd_valid(fd) && test_partition(fd)) {
4660 pr_err("imsm: %s is a partition, cannot be used in IMSM\n",
4661 devname);
4662 return 1;
4663 }
4664 if (!is_fd_valid(fd) || check_env("IMSM_NO_PLATFORM")) {
4665 super->orom = NULL;
4666 super->hba = NULL;
4667 return 0;
4668 }
4669 hba_name = find_disk_attached_hba(fd, NULL);
4670 if (!hba_name) {
4671 if (devname)
4672 pr_err("%s is not attached to Intel(R) RAID controller.\n",
4673 devname);
4674 return 1;
4675 }
4676 rv = attach_hba_to_super(super, hba_name);
4677 if (rv == 2) {
4678 if (devname) {
4679 struct intel_hba *hba = super->hba;
4680
4681 pr_err("%s is attached to Intel(R) %s %s (%s),\n"
4682 " but the container is assigned to Intel(R) %s %s (",
4683 devname,
4684 get_sys_dev_type(hba_name->type),
4685 hba_name->type == SYS_DEV_VMD ? "domain" : "RAID controller",
4686 hba_name->pci_id ? : "Err!",
4687 get_sys_dev_type(super->hba->type),
4688 hba->type == SYS_DEV_VMD ? "domain" : "RAID controller");
4689
4690 while (hba) {
4691 fprintf(stderr, "%s", hba->pci_id ? : "Err!");
4692 if (hba->next)
4693 fprintf(stderr, ", ");
4694 hba = hba->next;
4695 }
4696 fprintf(stderr, ").\n"
4697 " Mixing devices attached to different controllers is not allowed.\n");
4698 }
4699 return 2;
4700 }
4701 super->orom = find_imsm_capability(hba_name);
4702 if (!super->orom)
4703 return 3;
4704
4705 return 0;
4706 }
4707
4708 /* find_missing - helper routine for load_super_imsm_all that identifies
4709 * disks that have disappeared from the system. This routine relies on
4710 * the mpb being uptodate, which it is at load time.
4711 */
4712 static int find_missing(struct intel_super *super)
4713 {
4714 int i;
4715 struct imsm_super *mpb = super->anchor;
4716 struct dl *dl;
4717 struct imsm_disk *disk;
4718
4719 for (i = 0; i < mpb->num_disks; i++) {
4720 disk = __get_imsm_disk(mpb, i);
4721 dl = serial_to_dl(disk->serial, super);
4722 if (dl)
4723 continue;
4724
4725 dl = xmalloc(sizeof(*dl));
4726 dl->major = 0;
4727 dl->minor = 0;
4728 dl->fd = -1;
4729 dl->devname = xstrdup("missing");
4730 dl->index = i;
4731 serialcpy(dl->serial, disk->serial);
4732 dl->disk = *disk;
4733 dl->e = NULL;
4734 dl->next = super->missing;
4735 super->missing = dl;
4736 }
4737
4738 return 0;
4739 }
4740
4741 static struct intel_disk *disk_list_get(__u8 *serial, struct intel_disk *disk_list)
4742 {
4743 struct intel_disk *idisk = disk_list;
4744
4745 while (idisk) {
4746 if (serialcmp(idisk->disk.serial, serial) == 0)
4747 break;
4748 idisk = idisk->next;
4749 }
4750
4751 return idisk;
4752 }
4753
4754 static int __prep_thunderdome(struct intel_super **table, int tbl_size,
4755 struct intel_super *super,
4756 struct intel_disk **disk_list)
4757 {
4758 struct imsm_disk *d = &super->disks->disk;
4759 struct imsm_super *mpb = super->anchor;
4760 int i, j;
4761
4762 for (i = 0; i < tbl_size; i++) {
4763 struct imsm_super *tbl_mpb = table[i]->anchor;
4764 struct imsm_disk *tbl_d = &table[i]->disks->disk;
4765
4766 if (tbl_mpb->family_num == mpb->family_num) {
4767 if (tbl_mpb->check_sum == mpb->check_sum) {
4768 dprintf("mpb from %d:%d matches %d:%d\n",
4769 super->disks->major,
4770 super->disks->minor,
4771 table[i]->disks->major,
4772 table[i]->disks->minor);
4773 break;
4774 }
4775
4776 if (((is_configured(d) && !is_configured(tbl_d)) ||
4777 is_configured(d) == is_configured(tbl_d)) &&
4778 tbl_mpb->generation_num < mpb->generation_num) {
4779 /* current version of the mpb is a
4780 * better candidate than the one in
4781 * super_table, but copy over "cross
4782 * generational" status
4783 */
4784 struct intel_disk *idisk;
4785
4786 dprintf("mpb from %d:%d replaces %d:%d\n",
4787 super->disks->major,
4788 super->disks->minor,
4789 table[i]->disks->major,
4790 table[i]->disks->minor);
4791
4792 idisk = disk_list_get(tbl_d->serial, *disk_list);
4793 if (idisk && is_failed(&idisk->disk))
4794 tbl_d->status |= FAILED_DISK;
4795 break;
4796 } else {
4797 struct intel_disk *idisk;
4798 struct imsm_disk *disk;
4799
4800 /* tbl_mpb is more up to date, but copy
4801 * over cross generational status before
4802 * returning
4803 */
4804 disk = __serial_to_disk(d->serial, mpb, NULL);
4805 if (disk && is_failed(disk))
4806 d->status |= FAILED_DISK;
4807
4808 idisk = disk_list_get(d->serial, *disk_list);
4809 if (idisk) {
4810 idisk->owner = i;
4811 if (disk && is_configured(disk))
4812 idisk->disk.status |= CONFIGURED_DISK;
4813 }
4814
4815 dprintf("mpb from %d:%d prefer %d:%d\n",
4816 super->disks->major,
4817 super->disks->minor,
4818 table[i]->disks->major,
4819 table[i]->disks->minor);
4820
4821 return tbl_size;
4822 }
4823 }
4824 }
4825
4826 if (i >= tbl_size)
4827 table[tbl_size++] = super;
4828 else
4829 table[i] = super;
4830
4831 /* update/extend the merged list of imsm_disk records */
4832 for (j = 0; j < mpb->num_disks; j++) {
4833 struct imsm_disk *disk = __get_imsm_disk(mpb, j);
4834 struct intel_disk *idisk;
4835
4836 idisk = disk_list_get(disk->serial, *disk_list);
4837 if (idisk) {
4838 idisk->disk.status |= disk->status;
4839 if (is_configured(&idisk->disk) ||
4840 is_failed(&idisk->disk))
4841 idisk->disk.status &= ~(SPARE_DISK);
4842 } else {
4843 idisk = xcalloc(1, sizeof(*idisk));
4844 idisk->owner = IMSM_UNKNOWN_OWNER;
4845 idisk->disk = *disk;
4846 idisk->next = *disk_list;
4847 *disk_list = idisk;
4848 }
4849
4850 if (serialcmp(idisk->disk.serial, d->serial) == 0)
4851 idisk->owner = i;
4852 }
4853
4854 return tbl_size;
4855 }
4856
4857 static struct intel_super *
4858 validate_members(struct intel_super *super, struct intel_disk *disk_list,
4859 const int owner)
4860 {
4861 struct imsm_super *mpb = super->anchor;
4862 int ok_count = 0;
4863 int i;
4864
4865 for (i = 0; i < mpb->num_disks; i++) {
4866 struct imsm_disk *disk = __get_imsm_disk(mpb, i);
4867 struct intel_disk *idisk;
4868
4869 idisk = disk_list_get(disk->serial, disk_list);
4870 if (idisk) {
4871 if (idisk->owner == owner ||
4872 idisk->owner == IMSM_UNKNOWN_OWNER)
4873 ok_count++;
4874 else
4875 dprintf("'%.16s' owner %d != %d\n",
4876 disk->serial, idisk->owner,
4877 owner);
4878 } else {
4879 dprintf("unknown disk %x [%d]: %.16s\n",
4880 __le32_to_cpu(mpb->family_num), i,
4881 disk->serial);
4882 break;
4883 }
4884 }
4885
4886 if (ok_count == mpb->num_disks)
4887 return super;
4888 return NULL;
4889 }
4890
4891 static void show_conflicts(__u32 family_num, struct intel_super *super_list)
4892 {
4893 struct intel_super *s;
4894
4895 for (s = super_list; s; s = s->next) {
4896 if (family_num != s->anchor->family_num)
4897 continue;
4898 pr_err("Conflict, offlining family %#x on '%s'\n",
4899 __le32_to_cpu(family_num), s->disks->devname);
4900 }
4901 }
4902
4903 static struct intel_super *
4904 imsm_thunderdome(struct intel_super **super_list, int len)
4905 {
4906 struct intel_super *super_table[len];
4907 struct intel_disk *disk_list = NULL;
4908 struct intel_super *champion, *spare;
4909 struct intel_super *s, **del;
4910 int tbl_size = 0;
4911 int conflict;
4912 int i;
4913
4914 memset(super_table, 0, sizeof(super_table));
4915 for (s = *super_list; s; s = s->next)
4916 tbl_size = __prep_thunderdome(super_table, tbl_size, s, &disk_list);
4917
4918 for (i = 0; i < tbl_size; i++) {
4919 struct imsm_disk *d;
4920 struct intel_disk *idisk;
4921 struct imsm_super *mpb = super_table[i]->anchor;
4922
4923 s = super_table[i];
4924 d = &s->disks->disk;
4925
4926 /* 'd' must appear in merged disk list for its
4927 * configuration to be valid
4928 */
4929 idisk = disk_list_get(d->serial, disk_list);
4930 if (idisk && idisk->owner == i)
4931 s = validate_members(s, disk_list, i);
4932 else
4933 s = NULL;
4934
4935 if (!s)
4936 dprintf("marking family: %#x from %d:%d offline\n",
4937 mpb->family_num,
4938 super_table[i]->disks->major,
4939 super_table[i]->disks->minor);
4940 super_table[i] = s;
4941 }
4942
4943 /* This is where the mdadm implementation differs from the Windows
4944 * driver which has no strict concept of a container. We can only
4945 * assemble one family from a container, so when returning a prodigal
4946 * array member to this system the code will not be able to disambiguate
4947 * the container contents that should be assembled ("foreign" versus
4948 * "local"). It requires user intervention to set the orig_family_num
4949 * to a new value to establish a new container. The Windows driver in
4950 * this situation fixes up the volume name in place and manages the
4951 * foreign array as an independent entity.
4952 */
4953 s = NULL;
4954 spare = NULL;
4955 conflict = 0;
4956 for (i = 0; i < tbl_size; i++) {
4957 struct intel_super *tbl_ent = super_table[i];
4958 int is_spare = 0;
4959
4960 if (!tbl_ent)
4961 continue;
4962
4963 if (tbl_ent->anchor->num_raid_devs == 0) {
4964 spare = tbl_ent;
4965 is_spare = 1;
4966 }
4967
4968 if (s && !is_spare) {
4969 show_conflicts(tbl_ent->anchor->family_num, *super_list);
4970 conflict++;
4971 } else if (!s && !is_spare)
4972 s = tbl_ent;
4973 }
4974
4975 if (!s)
4976 s = spare;
4977 if (!s) {
4978 champion = NULL;
4979 goto out;
4980 }
4981 champion = s;
4982
4983 if (conflict)
4984 pr_err("Chose family %#x on '%s', assemble conflicts to new container with '--update=uuid'\n",
4985 __le32_to_cpu(s->anchor->family_num), s->disks->devname);
4986
4987 /* collect all dl's onto 'champion', and update them to
4988 * champion's version of the status
4989 */
4990 for (s = *super_list; s; s = s->next) {
4991 struct imsm_super *mpb = champion->anchor;
4992 struct dl *dl = s->disks;
4993
4994 if (s == champion)
4995 continue;
4996
4997 mpb->attributes |= s->anchor->attributes & MPB_ATTRIB_2TB_DISK;
4998
4999 for (i = 0; i < mpb->num_disks; i++) {
5000 struct imsm_disk *disk;
5001
5002 disk = __serial_to_disk(dl->serial, mpb, &dl->index);
5003 if (disk) {
5004 dl->disk = *disk;
5005 /* only set index on disks that are a member of
5006 * a populated contianer, i.e. one with
5007 * raid_devs
5008 */
5009 if (is_failed(&dl->disk))
5010 dl->index = -2;
5011 else if (is_spare(&dl->disk))
5012 dl->index = -1;
5013 break;
5014 }
5015 }
5016
5017 if (i >= mpb->num_disks) {
5018 struct intel_disk *idisk;
5019
5020 idisk = disk_list_get(dl->serial, disk_list);
5021 if (idisk && is_spare(&idisk->disk) &&
5022 !is_failed(&idisk->disk) && !is_configured(&idisk->disk))
5023 dl->index = -1;
5024 else {
5025 dl->index = -2;
5026 continue;
5027 }
5028 }
5029
5030 dl->next = champion->disks;
5031 champion->disks = dl;
5032 s->disks = NULL;
5033 }
5034
5035 /* delete 'champion' from super_list */
5036 for (del = super_list; *del; ) {
5037 if (*del == champion) {
5038 *del = (*del)->next;
5039 break;
5040 } else
5041 del = &(*del)->next;
5042 }
5043 champion->next = NULL;
5044
5045 out:
5046 while (disk_list) {
5047 struct intel_disk *idisk = disk_list;
5048
5049 disk_list = disk_list->next;
5050 free(idisk);
5051 }
5052
5053 return champion;
5054 }
5055
5056 static int
5057 get_sra_super_block(int fd, struct intel_super **super_list, char *devname, int *max, int keep_fd);
5058 static int get_super_block(struct intel_super **super_list, char *devnm, char *devname,
5059 int major, int minor, int keep_fd);
5060 static int
5061 get_devlist_super_block(struct md_list *devlist, struct intel_super **super_list,
5062 int *max, int keep_fd);
5063
5064 static int load_super_imsm_all(struct supertype *st, int fd, void **sbp,
5065 char *devname, struct md_list *devlist,
5066 int keep_fd)
5067 {
5068 struct intel_super *super_list = NULL;
5069 struct intel_super *super = NULL;
5070 int err = 0;
5071 int i = 0;
5072
5073 if (is_fd_valid(fd))
5074 /* 'fd' is an opened container */
5075 err = get_sra_super_block(fd, &super_list, devname, &i, keep_fd);
5076 else
5077 /* get super block from devlist devices */
5078 err = get_devlist_super_block(devlist, &super_list, &i, keep_fd);
5079 if (err)
5080 goto error;
5081 /* all mpbs enter, maybe one leaves */
5082 super = imsm_thunderdome(&super_list, i);
5083 if (!super) {
5084 err = 1;
5085 goto error;
5086 }
5087
5088 if (find_missing(super) != 0) {
5089 free_imsm(super);
5090 err = 2;
5091 goto error;
5092 }
5093
5094 /* load migration record */
5095 err = load_imsm_migr_rec(super);
5096 if (err == -1) {
5097 /* migration is in progress,
5098 * but migr_rec cannot be loaded,
5099 */
5100 err = 4;
5101 goto error;
5102 }
5103
5104 /* Check migration compatibility */
5105 if (err == 0 && check_mpb_migr_compatibility(super) != 0) {
5106 pr_err("Unsupported migration detected");
5107 if (devname)
5108 fprintf(stderr, " on %s\n", devname);
5109 else
5110 fprintf(stderr, " (IMSM).\n");
5111
5112 err = 5;
5113 goto error;
5114 }
5115
5116 err = 0;
5117
5118 error:
5119 while (super_list) {
5120 struct intel_super *s = super_list;
5121
5122 super_list = super_list->next;
5123 free_imsm(s);
5124 }
5125
5126 if (err)
5127 return err;
5128
5129 *sbp = super;
5130 if (is_fd_valid(fd))
5131 strcpy(st->container_devnm, fd2devnm(fd));
5132 else
5133 st->container_devnm[0] = 0;
5134 if (err == 0 && st->ss == NULL) {
5135 st->ss = &super_imsm;
5136 st->minor_version = 0;
5137 st->max_devs = IMSM_MAX_DEVICES;
5138 }
5139 return 0;
5140 }
5141
5142 static int
5143 get_devlist_super_block(struct md_list *devlist, struct intel_super **super_list,
5144 int *max, int keep_fd)
5145 {
5146 struct md_list *tmpdev;
5147 int err = 0;
5148 int i = 0;
5149
5150 for (i = 0, tmpdev = devlist; tmpdev; tmpdev = tmpdev->next) {
5151 if (tmpdev->used != 1)
5152 continue;
5153 if (tmpdev->container == 1) {
5154 int lmax = 0;
5155 int fd = dev_open(tmpdev->devname, O_RDONLY|O_EXCL);
5156 if (!is_fd_valid(fd)) {
5157 pr_err("cannot open device %s: %s\n",
5158 tmpdev->devname, strerror(errno));
5159 err = 8;
5160 goto error;
5161 }
5162 err = get_sra_super_block(fd, super_list,
5163 tmpdev->devname, &lmax,
5164 keep_fd);
5165 i += lmax;
5166 close(fd);
5167 if (err) {
5168 err = 7;
5169 goto error;
5170 }
5171 } else {
5172 int major = major(tmpdev->st_rdev);
5173 int minor = minor(tmpdev->st_rdev);
5174 err = get_super_block(super_list,
5175 NULL,
5176 tmpdev->devname,
5177 major, minor,
5178 keep_fd);
5179 i++;
5180 if (err) {
5181 err = 6;
5182 goto error;
5183 }
5184 }
5185 }
5186 error:
5187 *max = i;
5188 return err;
5189 }
5190
5191 static int get_super_block(struct intel_super **super_list, char *devnm, char *devname,
5192 int major, int minor, int keep_fd)
5193 {
5194 struct intel_super *s;
5195 char nm[32];
5196 int dfd = -1;
5197 int err = 0;
5198 int retry;
5199
5200 s = alloc_super();
5201 if (!s) {
5202 err = 1;
5203 goto error;
5204 }
5205
5206 sprintf(nm, "%d:%d", major, minor);
5207 dfd = dev_open(nm, O_RDWR);
5208 if (!is_fd_valid(dfd)) {
5209 err = 2;
5210 goto error;
5211 }
5212
5213 if (!get_dev_sector_size(dfd, NULL, &s->sector_size)) {
5214 err = 2;
5215 goto error;
5216 }
5217 find_intel_hba_capability(dfd, s, devname);
5218 err = load_and_parse_mpb(dfd, s, NULL, keep_fd);
5219
5220 /* retry the load if we might have raced against mdmon */
5221 if (err == 3 && devnm && mdmon_running(devnm))
5222 for (retry = 0; retry < 3; retry++) {
5223 usleep(3000);
5224 err = load_and_parse_mpb(dfd, s, NULL, keep_fd);
5225 if (err != 3)
5226 break;
5227 }
5228 error:
5229 if (!err) {
5230 s->next = *super_list;
5231 *super_list = s;
5232 } else {
5233 if (s)
5234 free_imsm(s);
5235 close_fd(&dfd);
5236 }
5237 if (!keep_fd)
5238 close_fd(&dfd);
5239 return err;
5240
5241 }
5242
5243 static int
5244 get_sra_super_block(int fd, struct intel_super **super_list, char *devname, int *max, int keep_fd)
5245 {
5246 struct mdinfo *sra;
5247 char *devnm;
5248 struct mdinfo *sd;
5249 int err = 0;
5250 int i = 0;
5251 sra = sysfs_read(fd, NULL, GET_LEVEL|GET_VERSION|GET_DEVS|GET_STATE);
5252 if (!sra)
5253 return 1;
5254
5255 if (sra->array.major_version != -1 ||
5256 sra->array.minor_version != -2 ||
5257 strcmp(sra->text_version, "imsm") != 0) {
5258 err = 1;
5259 goto error;
5260 }
5261 /* load all mpbs */
5262 devnm = fd2devnm(fd);
5263 for (sd = sra->devs, i = 0; sd; sd = sd->next, i++) {
5264 if (get_super_block(super_list, devnm, devname,
5265 sd->disk.major, sd->disk.minor, keep_fd) != 0) {
5266 err = 7;
5267 goto error;
5268 }
5269 }
5270 error:
5271 sysfs_free(sra);
5272 *max = i;
5273 return err;
5274 }
5275
5276 static int load_container_imsm(struct supertype *st, int fd, char *devname)
5277 {
5278 return load_super_imsm_all(st, fd, &st->sb, devname, NULL, 1);
5279 }
5280
5281 static int load_super_imsm(struct supertype *st, int fd, char *devname)
5282 {
5283 struct intel_super *super;
5284 int rv;
5285 int retry;
5286
5287 if (test_partition(fd))
5288 /* IMSM not allowed on partitions */
5289 return 1;
5290
5291 free_super_imsm(st);
5292
5293 super = alloc_super();
5294 if (!super)
5295 return 1;
5296
5297 if (!get_dev_sector_size(fd, NULL, &super->sector_size)) {
5298 free_imsm(super);
5299 return 1;
5300 }
5301 /* Load hba and capabilities if they exist.
5302 * But do not preclude loading metadata in case capabilities or hba are
5303 * non-compliant and ignore_hw_compat is set.
5304 */
5305 rv = find_intel_hba_capability(fd, super, devname);
5306 /* no orom/efi or non-intel hba of the disk */
5307 if (rv != 0 && st->ignore_hw_compat == 0) {
5308 if (devname)
5309 pr_err("No OROM/EFI properties for %s\n", devname);
5310 free_imsm(super);
5311 return 2;
5312 }
5313 rv = load_and_parse_mpb(fd, super, devname, 0);
5314
5315 /* retry the load if we might have raced against mdmon */
5316 if (rv == 3) {
5317 struct mdstat_ent *mdstat = NULL;
5318 char *name = fd2kname(fd);
5319
5320 if (name)
5321 mdstat = mdstat_by_component(name);
5322
5323 if (mdstat && mdmon_running(mdstat->devnm) && getpid() != mdmon_pid(mdstat->devnm)) {
5324 for (retry = 0; retry < 3; retry++) {
5325 usleep(3000);
5326 rv = load_and_parse_mpb(fd, super, devname, 0);
5327 if (rv != 3)
5328 break;
5329 }
5330 }
5331
5332 free_mdstat(mdstat);
5333 }
5334
5335 if (rv) {
5336 if (devname)
5337 pr_err("Failed to load all information sections on %s\n", devname);
5338 free_imsm(super);
5339 return rv;
5340 }
5341
5342 st->sb = super;
5343 if (st->ss == NULL) {
5344 st->ss = &super_imsm;
5345 st->minor_version = 0;
5346 st->max_devs = IMSM_MAX_DEVICES;
5347 }
5348
5349 /* load migration record */
5350 if (load_imsm_migr_rec(super) == 0) {
5351 /* Check for unsupported migration features */
5352 if (check_mpb_migr_compatibility(super) != 0) {
5353 pr_err("Unsupported migration detected");
5354 if (devname)
5355 fprintf(stderr, " on %s\n", devname);
5356 else
5357 fprintf(stderr, " (IMSM).\n");
5358 return 3;
5359 }
5360 }
5361
5362 return 0;
5363 }
5364
5365 static __u16 info_to_blocks_per_strip(mdu_array_info_t *info)
5366 {
5367 if (info->level == 1)
5368 return 128;
5369 return info->chunk_size >> 9;
5370 }
5371
5372 static unsigned long long info_to_blocks_per_member(mdu_array_info_t *info,
5373 unsigned long long size)
5374 {
5375 if (info->level == 1)
5376 return size * 2;
5377 else
5378 return (size * 2) & ~(info_to_blocks_per_strip(info) - 1);
5379 }
5380
5381 static void imsm_update_version_info(struct intel_super *super)
5382 {
5383 /* update the version and attributes */
5384 struct imsm_super *mpb = super->anchor;
5385 char *version;
5386 struct imsm_dev *dev;
5387 struct imsm_map *map;
5388 int i;
5389
5390 for (i = 0; i < mpb->num_raid_devs; i++) {
5391 dev = get_imsm_dev(super, i);
5392 map = get_imsm_map(dev, MAP_0);
5393 if (__le32_to_cpu(dev->size_high) > 0)
5394 mpb->attributes |= MPB_ATTRIB_2TB;
5395
5396 /* FIXME detect when an array spans a port multiplier */
5397 #if 0
5398 mpb->attributes |= MPB_ATTRIB_PM;
5399 #endif
5400
5401 if (mpb->num_raid_devs > 1 ||
5402 mpb->attributes != MPB_ATTRIB_CHECKSUM_VERIFY) {
5403 version = MPB_VERSION_ATTRIBS;
5404 switch (get_imsm_raid_level(map)) {
5405 case 0: mpb->attributes |= MPB_ATTRIB_RAID0; break;
5406 case 1: mpb->attributes |= MPB_ATTRIB_RAID1; break;
5407 case 10: mpb->attributes |= MPB_ATTRIB_RAID10; break;
5408 case 5: mpb->attributes |= MPB_ATTRIB_RAID5; break;
5409 }
5410 } else {
5411 if (map->num_members >= 5)
5412 version = MPB_VERSION_5OR6_DISK_ARRAY;
5413 else if (dev->status == DEV_CLONE_N_GO)
5414 version = MPB_VERSION_CNG;
5415 else if (get_imsm_raid_level(map) == 5)
5416 version = MPB_VERSION_RAID5;
5417 else if (map->num_members >= 3)
5418 version = MPB_VERSION_3OR4_DISK_ARRAY;
5419 else if (get_imsm_raid_level(map) == 1)
5420 version = MPB_VERSION_RAID1;
5421 else
5422 version = MPB_VERSION_RAID0;
5423 }
5424 strcpy(((char *) mpb->sig) + strlen(MPB_SIGNATURE), version);
5425 }
5426 }
5427
5428 static int check_name(struct intel_super *super, char *name, int quiet)
5429 {
5430 struct imsm_super *mpb = super->anchor;
5431 char *reason = NULL;
5432 char *start = name;
5433 size_t len = strlen(name);
5434 int i;
5435
5436 if (len > 0) {
5437 while (isspace(start[len - 1]))
5438 start[--len] = 0;
5439 while (*start && isspace(*start))
5440 ++start, --len;
5441 memmove(name, start, len + 1);
5442 }
5443
5444 if (len > MAX_RAID_SERIAL_LEN)
5445 reason = "must be 16 characters or less";
5446 else if (len == 0)
5447 reason = "must be a non-empty string";
5448
5449 for (i = 0; i < mpb->num_raid_devs; i++) {
5450 struct imsm_dev *dev = get_imsm_dev(super, i);
5451
5452 if (strncmp((char *) dev->volume, name, MAX_RAID_SERIAL_LEN) == 0) {
5453 reason = "already exists";
5454 break;
5455 }
5456 }
5457
5458 if (reason && !quiet)
5459 pr_err("imsm volume name %s\n", reason);
5460
5461 return !reason;
5462 }
5463
5464 static int init_super_imsm_volume(struct supertype *st, mdu_array_info_t *info,
5465 struct shape *s, char *name,
5466 char *homehost, int *uuid,
5467 long long data_offset)
5468 {
5469 /* We are creating a volume inside a pre-existing container.
5470 * so st->sb is already set.
5471 */
5472 struct intel_super *super = st->sb;
5473 unsigned int sector_size = super->sector_size;
5474 struct imsm_super *mpb = super->anchor;
5475 struct intel_dev *dv;
5476 struct imsm_dev *dev;
5477 struct imsm_vol *vol;
5478 struct imsm_map *map;
5479 int idx = mpb->num_raid_devs;
5480 int i;
5481 int namelen;
5482 unsigned long long array_blocks;
5483 size_t size_old, size_new;
5484 unsigned int data_disks;
5485 unsigned long long size_per_member;
5486
5487 if (super->orom && mpb->num_raid_devs >= super->orom->vpa) {
5488 pr_err("This imsm-container already has the maximum of %d volumes\n", super->orom->vpa);
5489 return 0;
5490 }
5491
5492 /* ensure the mpb is large enough for the new data */
5493 size_old = __le32_to_cpu(mpb->mpb_size);
5494 size_new = disks_to_mpb_size(info->nr_disks);
5495 if (size_new > size_old) {
5496 void *mpb_new;
5497 size_t size_round = ROUND_UP(size_new, sector_size);
5498
5499 if (posix_memalign(&mpb_new, sector_size, size_round) != 0) {
5500 pr_err("could not allocate new mpb\n");
5501 return 0;
5502 }
5503 if (posix_memalign(&super->migr_rec_buf, MAX_SECTOR_SIZE,
5504 MIGR_REC_BUF_SECTORS*
5505 MAX_SECTOR_SIZE) != 0) {
5506 pr_err("could not allocate migr_rec buffer\n");
5507 free(super->buf);
5508 free(super);
5509 free(mpb_new);
5510 return 0;
5511 }
5512 memcpy(mpb_new, mpb, size_old);
5513 free(mpb);
5514 mpb = mpb_new;
5515 super->anchor = mpb_new;
5516 mpb->mpb_size = __cpu_to_le32(size_new);
5517 memset(mpb_new + size_old, 0, size_round - size_old);
5518 super->len = size_round;
5519 }
5520 super->current_vol = idx;
5521
5522 /* handle 'failed_disks' by either:
5523 * a) create dummy disk entries in the table if this the first
5524 * volume in the array. We add them here as this is the only
5525 * opportunity to add them. add_to_super_imsm_volume()
5526 * handles the non-failed disks and continues incrementing
5527 * mpb->num_disks.
5528 * b) validate that 'failed_disks' matches the current number
5529 * of missing disks if the container is populated
5530 */
5531 if (super->current_vol == 0) {
5532 mpb->num_disks = 0;
5533 for (i = 0; i < info->failed_disks; i++) {
5534 struct imsm_disk *disk;
5535
5536 mpb->num_disks++;
5537 disk = __get_imsm_disk(mpb, i);
5538 disk->status = CONFIGURED_DISK | FAILED_DISK;
5539 disk->scsi_id = __cpu_to_le32(~(__u32)0);
5540 snprintf((char *) disk->serial, MAX_RAID_SERIAL_LEN,
5541 "missing:%d", (__u8)i);
5542 }
5543 find_missing(super);
5544 } else {
5545 int missing = 0;
5546 struct dl *d;
5547
5548 for (d = super->missing; d; d = d->next)
5549 missing++;
5550 if (info->failed_disks > missing) {
5551 pr_err("unable to add 'missing' disk to container\n");
5552 return 0;
5553 }
5554 }
5555
5556 if (!check_name(super, name, 0))
5557 return 0;
5558 dv = xmalloc(sizeof(*dv));
5559 dev = xcalloc(1, sizeof(*dev) + sizeof(__u32) * (info->raid_disks - 1));
5560 /*
5561 * Explicitly allow truncating to not confuse gcc's
5562 * -Werror=stringop-truncation
5563 */
5564 namelen = min((int) strlen(name), MAX_RAID_SERIAL_LEN);
5565 memcpy(dev->volume, name, namelen);
5566 array_blocks = calc_array_size(info->level, info->raid_disks,
5567 info->layout, info->chunk_size,
5568 s->size * BLOCKS_PER_KB);
5569 data_disks = get_data_disks(info->level, info->layout,
5570 info->raid_disks);
5571 array_blocks = round_size_to_mb(array_blocks, data_disks);
5572 size_per_member = array_blocks / data_disks;
5573
5574 set_imsm_dev_size(dev, array_blocks);
5575 dev->status = (DEV_READ_COALESCING | DEV_WRITE_COALESCING);
5576 vol = &dev->vol;
5577 vol->migr_state = 0;
5578 set_migr_type(dev, MIGR_INIT);
5579 vol->dirty = !info->state;
5580 set_vol_curr_migr_unit(dev, 0);
5581 map = get_imsm_map(dev, MAP_0);
5582 set_pba_of_lba0(map, super->create_offset);
5583 map->blocks_per_strip = __cpu_to_le16(info_to_blocks_per_strip(info));
5584 map->failed_disk_num = ~0;
5585 if (info->level > 0)
5586 map->map_state = (info->state ? IMSM_T_STATE_NORMAL
5587 : IMSM_T_STATE_UNINITIALIZED);
5588 else
5589 map->map_state = info->failed_disks ? IMSM_T_STATE_FAILED :
5590 IMSM_T_STATE_NORMAL;
5591 map->ddf = 1;
5592
5593 if (info->level == 1 && info->raid_disks > 2) {
5594 free(dev);
5595 free(dv);
5596 pr_err("imsm does not support more than 2 disksin a raid1 volume\n");
5597 return 0;
5598 }
5599
5600 map->raid_level = info->level;
5601 if (info->level == 10)
5602 map->raid_level = 1;
5603 set_num_domains(map);
5604
5605 size_per_member += NUM_BLOCKS_DIRTY_STRIPE_REGION;
5606 set_blocks_per_member(map, info_to_blocks_per_member(info,
5607 size_per_member /
5608 BLOCKS_PER_KB));
5609
5610 map->num_members = info->raid_disks;
5611 update_num_data_stripes(map, array_blocks);
5612 for (i = 0; i < map->num_members; i++) {
5613 /* initialized in add_to_super */
5614 set_imsm_ord_tbl_ent(map, i, IMSM_ORD_REBUILD);
5615 }
5616 mpb->num_raid_devs++;
5617 mpb->num_raid_devs_created++;
5618 dev->my_vol_raid_dev_num = mpb->num_raid_devs_created;
5619
5620 if (s->consistency_policy <= CONSISTENCY_POLICY_RESYNC) {
5621 dev->rwh_policy = RWH_MULTIPLE_OFF;
5622 } else if (s->consistency_policy == CONSISTENCY_POLICY_PPL) {
5623 dev->rwh_policy = RWH_MULTIPLE_DISTRIBUTED;
5624 } else {
5625 free(dev);
5626 free(dv);
5627 pr_err("imsm does not support consistency policy %s\n",
5628 map_num(consistency_policies, s->consistency_policy));
5629 return 0;
5630 }
5631
5632 dv->dev = dev;
5633 dv->index = super->current_vol;
5634 dv->next = super->devlist;
5635 super->devlist = dv;
5636
5637 imsm_update_version_info(super);
5638
5639 return 1;
5640 }
5641
5642 static int init_super_imsm(struct supertype *st, mdu_array_info_t *info,
5643 struct shape *s, char *name,
5644 char *homehost, int *uuid,
5645 unsigned long long data_offset)
5646 {
5647 /* This is primarily called by Create when creating a new array.
5648 * We will then get add_to_super called for each component, and then
5649 * write_init_super called to write it out to each device.
5650 * For IMSM, Create can create on fresh devices or on a pre-existing
5651 * array.
5652 * To create on a pre-existing array a different method will be called.
5653 * This one is just for fresh drives.
5654 */
5655 struct intel_super *super;
5656 struct imsm_super *mpb;
5657 size_t mpb_size;
5658 char *version;
5659
5660 if (data_offset != INVALID_SECTORS) {
5661 pr_err("data-offset not supported by imsm\n");
5662 return 0;
5663 }
5664
5665 if (st->sb)
5666 return init_super_imsm_volume(st, info, s, name, homehost, uuid,
5667 data_offset);
5668
5669 if (info)
5670 mpb_size = disks_to_mpb_size(info->nr_disks);
5671 else
5672 mpb_size = MAX_SECTOR_SIZE;
5673
5674 super = alloc_super();
5675 if (super &&
5676 posix_memalign(&super->buf, MAX_SECTOR_SIZE, mpb_size) != 0) {
5677 free_imsm(super);
5678 super = NULL;
5679 }
5680 if (!super) {
5681 pr_err("could not allocate superblock\n");
5682 return 0;
5683 }
5684 if (posix_memalign(&super->migr_rec_buf, MAX_SECTOR_SIZE,
5685 MIGR_REC_BUF_SECTORS*MAX_SECTOR_SIZE) != 0) {
5686 pr_err("could not allocate migr_rec buffer\n");
5687 free(super->buf);
5688 free_imsm(super);
5689 return 0;
5690 }
5691 memset(super->buf, 0, mpb_size);
5692 mpb = super->buf;
5693 mpb->mpb_size = __cpu_to_le32(mpb_size);
5694 st->sb = super;
5695
5696 if (info == NULL) {
5697 /* zeroing superblock */
5698 return 0;
5699 }
5700
5701 mpb->attributes = MPB_ATTRIB_CHECKSUM_VERIFY;
5702
5703 version = (char *) mpb->sig;
5704 strcpy(version, MPB_SIGNATURE);
5705 version += strlen(MPB_SIGNATURE);
5706 strcpy(version, MPB_VERSION_RAID0);
5707
5708 return 1;
5709 }
5710
5711 static int drive_validate_sector_size(struct intel_super *super, struct dl *dl)
5712 {
5713 unsigned int member_sector_size;
5714
5715 if (!is_fd_valid(dl->fd)) {
5716 pr_err("Invalid file descriptor for %s\n", dl->devname);
5717 return 0;
5718 }
5719
5720 if (!get_dev_sector_size(dl->fd, dl->devname, &member_sector_size))
5721 return 0;
5722 if (member_sector_size != super->sector_size)
5723 return 0;
5724 return 1;
5725 }
5726
5727 static int add_to_super_imsm_volume(struct supertype *st, mdu_disk_info_t *dk,
5728 int fd, char *devname)
5729 {
5730 struct intel_super *super = st->sb;
5731 struct imsm_super *mpb = super->anchor;
5732 struct imsm_disk *_disk;
5733 struct imsm_dev *dev;
5734 struct imsm_map *map;
5735 struct dl *dl, *df;
5736 int slot;
5737
5738 dev = get_imsm_dev(super, super->current_vol);
5739 map = get_imsm_map(dev, MAP_0);
5740
5741 if (! (dk->state & (1<<MD_DISK_SYNC))) {
5742 pr_err("%s: Cannot add spare devices to IMSM volume\n",
5743 devname);
5744 return 1;
5745 }
5746
5747 if (!is_fd_valid(fd)) {
5748 /* we're doing autolayout so grab the pre-marked (in
5749 * validate_geometry) raid_disk
5750 */
5751 for (dl = super->disks; dl; dl = dl->next)
5752 if (dl->raiddisk == dk->raid_disk)
5753 break;
5754 } else {
5755 for (dl = super->disks; dl ; dl = dl->next)
5756 if (dl->major == dk->major &&
5757 dl->minor == dk->minor)
5758 break;
5759 }
5760
5761 if (!dl) {
5762 pr_err("%s is not a member of the same container\n", devname);
5763 return 1;
5764 }
5765
5766 if (mpb->num_disks == 0)
5767 if (!get_dev_sector_size(dl->fd, dl->devname,
5768 &super->sector_size))
5769 return 1;
5770
5771 if (!drive_validate_sector_size(super, dl)) {
5772 pr_err("Combining drives of different sector size in one volume is not allowed\n");
5773 return 1;
5774 }
5775
5776 /* add a pristine spare to the metadata */
5777 if (dl->index < 0) {
5778 dl->index = super->anchor->num_disks;
5779 super->anchor->num_disks++;
5780 }
5781 /* Check the device has not already been added */
5782 slot = get_imsm_disk_slot(map, dl->index);
5783 if (slot >= 0 &&
5784 (get_imsm_ord_tbl_ent(dev, slot, MAP_X) & IMSM_ORD_REBUILD) == 0) {
5785 pr_err("%s has been included in this array twice\n",
5786 devname);
5787 return 1;
5788 }
5789 set_imsm_ord_tbl_ent(map, dk->raid_disk, dl->index);
5790 dl->disk.status = CONFIGURED_DISK;
5791
5792 /* update size of 'missing' disks to be at least as large as the
5793 * largest acitve member (we only have dummy missing disks when
5794 * creating the first volume)
5795 */
5796 if (super->current_vol == 0) {
5797 for (df = super->missing; df; df = df->next) {
5798 if (total_blocks(&dl->disk) > total_blocks(&df->disk))
5799 set_total_blocks(&df->disk, total_blocks(&dl->disk));
5800 _disk = __get_imsm_disk(mpb, df->index);
5801 *_disk = df->disk;
5802 }
5803 }
5804
5805 /* refresh unset/failed slots to point to valid 'missing' entries */
5806 for (df = super->missing; df; df = df->next)
5807 for (slot = 0; slot < mpb->num_disks; slot++) {
5808 __u32 ord = get_imsm_ord_tbl_ent(dev, slot, MAP_X);
5809
5810 if ((ord & IMSM_ORD_REBUILD) == 0)
5811 continue;
5812 set_imsm_ord_tbl_ent(map, slot, df->index | IMSM_ORD_REBUILD);
5813 if (is_gen_migration(dev)) {
5814 struct imsm_map *map2 = get_imsm_map(dev,
5815 MAP_1);
5816 int slot2 = get_imsm_disk_slot(map2, df->index);
5817 if (slot2 < map2->num_members && slot2 >= 0) {
5818 __u32 ord2 = get_imsm_ord_tbl_ent(dev,
5819 slot2,
5820 MAP_1);
5821 if ((unsigned)df->index ==
5822 ord_to_idx(ord2))
5823 set_imsm_ord_tbl_ent(map2,
5824 slot2,
5825 df->index |
5826 IMSM_ORD_REBUILD);
5827 }
5828 }
5829 dprintf("set slot:%d to missing disk:%d\n", slot, df->index);
5830 break;
5831 }
5832
5833 /* if we are creating the first raid device update the family number */
5834 if (super->current_vol == 0) {
5835 __u32 sum;
5836 struct imsm_dev *_dev = __get_imsm_dev(mpb, 0);
5837
5838 _disk = __get_imsm_disk(mpb, dl->index);
5839 if (!_dev || !_disk) {
5840 pr_err("BUG mpb setup error\n");
5841 return 1;
5842 }
5843 *_dev = *dev;
5844 *_disk = dl->disk;
5845 sum = random32();
5846 sum += __gen_imsm_checksum(mpb);
5847 mpb->family_num = __cpu_to_le32(sum);
5848 mpb->orig_family_num = mpb->family_num;
5849 mpb->creation_time = __cpu_to_le64((__u64)time(NULL));
5850 }
5851 super->current_disk = dl;
5852 return 0;
5853 }
5854
5855 /* mark_spare()
5856 * Function marks disk as spare and restores disk serial
5857 * in case it was previously marked as failed by takeover operation
5858 * reruns:
5859 * -1 : critical error
5860 * 0 : disk is marked as spare but serial is not set
5861 * 1 : success
5862 */
5863 int mark_spare(struct dl *disk)
5864 {
5865 __u8 serial[MAX_RAID_SERIAL_LEN];
5866 int ret_val = -1;
5867
5868 if (!disk)
5869 return ret_val;
5870
5871 ret_val = 0;
5872 if (!imsm_read_serial(disk->fd, NULL, serial, MAX_RAID_SERIAL_LEN)) {
5873 /* Restore disk serial number, because takeover marks disk
5874 * as failed and adds to serial ':0' before it becomes
5875 * a spare disk.
5876 */
5877 serialcpy(disk->serial, serial);
5878 serialcpy(disk->disk.serial, serial);
5879 ret_val = 1;
5880 }
5881 disk->disk.status = SPARE_DISK;
5882 disk->index = -1;
5883
5884 return ret_val;
5885 }
5886
5887
5888 static int write_super_imsm_spare(struct intel_super *super, struct dl *d);
5889
5890 static int add_to_super_imsm(struct supertype *st, mdu_disk_info_t *dk,
5891 int fd, char *devname,
5892 unsigned long long data_offset)
5893 {
5894 struct intel_super *super = st->sb;
5895 struct dl *dd;
5896 unsigned long long size;
5897 unsigned int member_sector_size;
5898 __u32 id;
5899 int rv;
5900 struct stat stb;
5901
5902 /* If we are on an RAID enabled platform check that the disk is
5903 * attached to the raid controller.
5904 * We do not need to test disks attachment for container based additions,
5905 * they shall be already tested when container was created/assembled.
5906 */
5907 rv = find_intel_hba_capability(fd, super, devname);
5908 /* no orom/efi or non-intel hba of the disk */
5909 if (rv != 0) {
5910 dprintf("capability: %p fd: %d ret: %d\n",
5911 super->orom, fd, rv);
5912 return 1;
5913 }
5914
5915 if (super->current_vol >= 0)
5916 return add_to_super_imsm_volume(st, dk, fd, devname);
5917
5918 fstat(fd, &stb);
5919 dd = xcalloc(sizeof(*dd), 1);
5920 dd->major = major(stb.st_rdev);
5921 dd->minor = minor(stb.st_rdev);
5922 dd->devname = devname ? xstrdup(devname) : NULL;
5923 dd->fd = fd;
5924 dd->e = NULL;
5925 dd->action = DISK_ADD;
5926 rv = imsm_read_serial(fd, devname, dd->serial, MAX_RAID_SERIAL_LEN);
5927 if (rv) {
5928 pr_err("failed to retrieve scsi serial, aborting\n");
5929 __free_imsm_disk(dd, 0);
5930 abort();
5931 }
5932
5933 if (super->hba && ((super->hba->type == SYS_DEV_NVME) ||
5934 (super->hba->type == SYS_DEV_VMD))) {
5935 int i;
5936 char cntrl_path[PATH_MAX];
5937 char *cntrl_name;
5938 char pci_dev_path[PATH_MAX];
5939
5940 if (!diskfd_to_devpath(fd, 2, pci_dev_path) ||
5941 !diskfd_to_devpath(fd, 1, cntrl_path)) {
5942 pr_err("failed to get dev paths, aborting\n");
5943 __free_imsm_disk(dd, 0);
5944 return 1;
5945 }
5946
5947 cntrl_name = basename(cntrl_path);
5948 if (is_multipath_nvme(fd))
5949 pr_err("%s controller supports Multi-Path I/O, Intel (R) VROC does not support multipathing\n",
5950 cntrl_name);
5951
5952 if (devpath_to_vendor(pci_dev_path) == 0x8086) {
5953 /*
5954 * If Intel's NVMe drive has serial ended with
5955 * "-A","-B","-1" or "-2" it means that this is "x8"
5956 * device (double drive on single PCIe card).
5957 * User should be warned about potential data loss.
5958 */
5959 for (i = MAX_RAID_SERIAL_LEN-1; i > 0; i--) {
5960 /* Skip empty character at the end */
5961 if (dd->serial[i] == 0)
5962 continue;
5963
5964 if (((dd->serial[i] == 'A') ||
5965 (dd->serial[i] == 'B') ||
5966 (dd->serial[i] == '1') ||
5967 (dd->serial[i] == '2')) &&
5968 (dd->serial[i-1] == '-'))
5969 pr_err("\tThe action you are about to take may put your data at risk.\n"
5970 "\tPlease note that x8 devices may consist of two separate x4 devices "
5971 "located on a single PCIe port.\n"
5972 "\tRAID 0 is the only supported configuration for this type of x8 device.\n");
5973 break;
5974 }
5975 } else if (super->hba->type == SYS_DEV_VMD && super->orom &&
5976 !imsm_orom_has_tpv_support(super->orom)) {
5977 pr_err("\tPlatform configuration does not support non-Intel NVMe drives.\n"
5978 "\tPlease refer to Intel(R) RSTe/VROC user guide.\n");
5979 __free_imsm_disk(dd, 0);
5980 return 1;
5981 }
5982 }
5983
5984 get_dev_size(fd, NULL, &size);
5985 if (!get_dev_sector_size(fd, NULL, &member_sector_size)) {
5986 __free_imsm_disk(dd, 0);
5987 return 1;
5988 }
5989
5990 if (super->sector_size == 0) {
5991 /* this a first device, so sector_size is not set yet */
5992 super->sector_size = member_sector_size;
5993 }
5994
5995 /* clear migr_rec when adding disk to container */
5996 memset(super->migr_rec_buf, 0, MIGR_REC_BUF_SECTORS*MAX_SECTOR_SIZE);
5997 if (lseek64(fd, size - MIGR_REC_SECTOR_POSITION*member_sector_size,
5998 SEEK_SET) >= 0) {
5999 if ((unsigned int)write(fd, super->migr_rec_buf,
6000 MIGR_REC_BUF_SECTORS*member_sector_size) !=
6001 MIGR_REC_BUF_SECTORS*member_sector_size)
6002 perror("Write migr_rec failed");
6003 }
6004
6005 size /= 512;
6006 serialcpy(dd->disk.serial, dd->serial);
6007 set_total_blocks(&dd->disk, size);
6008 if (__le32_to_cpu(dd->disk.total_blocks_hi) > 0) {
6009 struct imsm_super *mpb = super->anchor;
6010 mpb->attributes |= MPB_ATTRIB_2TB_DISK;
6011 }
6012 mark_spare(dd);
6013 if (sysfs_disk_to_scsi_id(fd, &id) == 0)
6014 dd->disk.scsi_id = __cpu_to_le32(id);
6015 else
6016 dd->disk.scsi_id = __cpu_to_le32(0);
6017
6018 if (st->update_tail) {
6019 dd->next = super->disk_mgmt_list;
6020 super->disk_mgmt_list = dd;
6021 } else {
6022 /* this is called outside of mdmon
6023 * write initial spare metadata
6024 * mdmon will overwrite it.
6025 */
6026 dd->next = super->disks;
6027 super->disks = dd;
6028 write_super_imsm_spare(super, dd);
6029 }
6030
6031 return 0;
6032 }
6033
6034 static int remove_from_super_imsm(struct supertype *st, mdu_disk_info_t *dk)
6035 {
6036 struct intel_super *super = st->sb;
6037 struct dl *dd;
6038
6039 /* remove from super works only in mdmon - for communication
6040 * manager - monitor. Check if communication memory buffer
6041 * is prepared.
6042 */
6043 if (!st->update_tail) {
6044 pr_err("shall be used in mdmon context only\n");
6045 return 1;
6046 }
6047 dd = xcalloc(1, sizeof(*dd));
6048 dd->major = dk->major;
6049 dd->minor = dk->minor;
6050 dd->fd = -1;
6051 mark_spare(dd);
6052 dd->action = DISK_REMOVE;
6053
6054 dd->next = super->disk_mgmt_list;
6055 super->disk_mgmt_list = dd;
6056
6057 return 0;
6058 }
6059
6060 static int store_imsm_mpb(int fd, struct imsm_super *mpb);
6061
6062 static union {
6063 char buf[MAX_SECTOR_SIZE];
6064 struct imsm_super anchor;
6065 } spare_record __attribute__ ((aligned(MAX_SECTOR_SIZE)));
6066
6067
6068 static int write_super_imsm_spare(struct intel_super *super, struct dl *d)
6069 {
6070 struct imsm_super *mpb = super->anchor;
6071 struct imsm_super *spare = &spare_record.anchor;
6072 __u32 sum;
6073
6074 if (d->index != -1)
6075 return 1;
6076
6077 spare->mpb_size = __cpu_to_le32(sizeof(struct imsm_super));
6078 spare->generation_num = __cpu_to_le32(1UL);
6079 spare->attributes = MPB_ATTRIB_CHECKSUM_VERIFY;
6080 spare->num_disks = 1;
6081 spare->num_raid_devs = 0;
6082 spare->cache_size = mpb->cache_size;
6083 spare->pwr_cycle_count = __cpu_to_le32(1);
6084
6085 snprintf((char *) spare->sig, MAX_SIGNATURE_LENGTH,
6086 MPB_SIGNATURE MPB_VERSION_RAID0);
6087
6088 spare->disk[0] = d->disk;
6089 if (__le32_to_cpu(d->disk.total_blocks_hi) > 0)
6090 spare->attributes |= MPB_ATTRIB_2TB_DISK;
6091
6092 if (super->sector_size == 4096)
6093 convert_to_4k_imsm_disk(&spare->disk[0]);
6094
6095 sum = __gen_imsm_checksum(spare);
6096 spare->family_num = __cpu_to_le32(sum);
6097 spare->orig_family_num = 0;
6098 sum = __gen_imsm_checksum(spare);
6099 spare->check_sum = __cpu_to_le32(sum);
6100
6101 if (store_imsm_mpb(d->fd, spare)) {
6102 pr_err("failed for device %d:%d %s\n",
6103 d->major, d->minor, strerror(errno));
6104 return 1;
6105 }
6106
6107 return 0;
6108 }
6109 /* spare records have their own family number and do not have any defined raid
6110 * devices
6111 */
6112 static int write_super_imsm_spares(struct intel_super *super, int doclose)
6113 {
6114 struct dl *d;
6115
6116 for (d = super->disks; d; d = d->next) {
6117 if (d->index != -1)
6118 continue;
6119
6120 if (write_super_imsm_spare(super, d))
6121 return 1;
6122
6123 if (doclose)
6124 close_fd(&d->fd);
6125 }
6126
6127 return 0;
6128 }
6129
6130 static int write_super_imsm(struct supertype *st, int doclose)
6131 {
6132 struct intel_super *super = st->sb;
6133 unsigned int sector_size = super->sector_size;
6134 struct imsm_super *mpb = super->anchor;
6135 struct dl *d;
6136 __u32 generation;
6137 __u32 sum;
6138 int spares = 0;
6139 int i;
6140 __u32 mpb_size = sizeof(struct imsm_super) - sizeof(struct imsm_disk);
6141 int num_disks = 0;
6142 int clear_migration_record = 1;
6143 __u32 bbm_log_size;
6144
6145 /* 'generation' is incremented everytime the metadata is written */
6146 generation = __le32_to_cpu(mpb->generation_num);
6147 generation++;
6148 mpb->generation_num = __cpu_to_le32(generation);
6149
6150 /* fix up cases where previous mdadm releases failed to set
6151 * orig_family_num
6152 */
6153 if (mpb->orig_family_num == 0)
6154 mpb->orig_family_num = mpb->family_num;
6155
6156 for (d = super->disks; d; d = d->next) {
6157 if (d->index == -1)
6158 spares++;
6159 else {
6160 mpb->disk[d->index] = d->disk;
6161 num_disks++;
6162 }
6163 }
6164 for (d = super->missing; d; d = d->next) {
6165 mpb->disk[d->index] = d->disk;
6166 num_disks++;
6167 }
6168 mpb->num_disks = num_disks;
6169 mpb_size += sizeof(struct imsm_disk) * mpb->num_disks;
6170
6171 for (i = 0; i < mpb->num_raid_devs; i++) {
6172 struct imsm_dev *dev = __get_imsm_dev(mpb, i);
6173 struct imsm_dev *dev2 = get_imsm_dev(super, i);
6174 if (dev && dev2) {
6175 imsm_copy_dev(dev, dev2);
6176 mpb_size += sizeof_imsm_dev(dev, 0);
6177 }
6178 if (is_gen_migration(dev2))
6179 clear_migration_record = 0;
6180 }
6181
6182 bbm_log_size = get_imsm_bbm_log_size(super->bbm_log);
6183
6184 if (bbm_log_size) {
6185 memcpy((void *)mpb + mpb_size, super->bbm_log, bbm_log_size);
6186 mpb->attributes |= MPB_ATTRIB_BBM;
6187 } else
6188 mpb->attributes &= ~MPB_ATTRIB_BBM;
6189
6190 super->anchor->bbm_log_size = __cpu_to_le32(bbm_log_size);
6191 mpb_size += bbm_log_size;
6192 mpb->mpb_size = __cpu_to_le32(mpb_size);
6193
6194 #ifdef DEBUG
6195 assert(super->len == 0 || mpb_size <= super->len);
6196 #endif
6197
6198 /* recalculate checksum */
6199 sum = __gen_imsm_checksum(mpb);
6200 mpb->check_sum = __cpu_to_le32(sum);
6201
6202 if (super->clean_migration_record_by_mdmon) {
6203 clear_migration_record = 1;
6204 super->clean_migration_record_by_mdmon = 0;
6205 }
6206 if (clear_migration_record)
6207 memset(super->migr_rec_buf, 0,
6208 MIGR_REC_BUF_SECTORS*MAX_SECTOR_SIZE);
6209
6210 if (sector_size == 4096)
6211 convert_to_4k(super);
6212
6213 /* write the mpb for disks that compose raid devices */
6214 for (d = super->disks; d ; d = d->next) {
6215 if (d->index < 0 || is_failed(&d->disk))
6216 continue;
6217
6218 if (clear_migration_record) {
6219 unsigned long long dsize;
6220
6221 get_dev_size(d->fd, NULL, &dsize);
6222 if (lseek64(d->fd, dsize - sector_size,
6223 SEEK_SET) >= 0) {
6224 if ((unsigned int)write(d->fd,
6225 super->migr_rec_buf,
6226 MIGR_REC_BUF_SECTORS*sector_size) !=
6227 MIGR_REC_BUF_SECTORS*sector_size)
6228 perror("Write migr_rec failed");
6229 }
6230 }
6231
6232 if (store_imsm_mpb(d->fd, mpb))
6233 fprintf(stderr,
6234 "failed for device %d:%d (fd: %d)%s\n",
6235 d->major, d->minor,
6236 d->fd, strerror(errno));
6237
6238 if (doclose)
6239 close_fd(&d->fd);
6240 }
6241
6242 if (spares)
6243 return write_super_imsm_spares(super, doclose);
6244
6245 return 0;
6246 }
6247
6248 static int create_array(struct supertype *st, int dev_idx)
6249 {
6250 size_t len;
6251 struct imsm_update_create_array *u;
6252 struct intel_super *super = st->sb;
6253 struct imsm_dev *dev = get_imsm_dev(super, dev_idx);
6254 struct imsm_map *map = get_imsm_map(dev, MAP_0);
6255 struct disk_info *inf;
6256 struct imsm_disk *disk;
6257 int i;
6258
6259 len = sizeof(*u) - sizeof(*dev) + sizeof_imsm_dev(dev, 0) +
6260 sizeof(*inf) * map->num_members;
6261 u = xmalloc(len);
6262 u->type = update_create_array;
6263 u->dev_idx = dev_idx;
6264 imsm_copy_dev(&u->dev, dev);
6265 inf = get_disk_info(u);
6266 for (i = 0; i < map->num_members; i++) {
6267 int idx = get_imsm_disk_idx(dev, i, MAP_X);
6268
6269 disk = get_imsm_disk(super, idx);
6270 if (!disk)
6271 disk = get_imsm_missing(super, idx);
6272 serialcpy(inf[i].serial, disk->serial);
6273 }
6274 append_metadata_update(st, u, len);
6275
6276 return 0;
6277 }
6278
6279 static int mgmt_disk(struct supertype *st)
6280 {
6281 struct intel_super *super = st->sb;
6282 size_t len;
6283 struct imsm_update_add_remove_disk *u;
6284
6285 if (!super->disk_mgmt_list)
6286 return 0;
6287
6288 len = sizeof(*u);
6289 u = xmalloc(len);
6290 u->type = update_add_remove_disk;
6291 append_metadata_update(st, u, len);
6292
6293 return 0;
6294 }
6295
6296 __u32 crc32c_le(__u32 crc, unsigned char const *p, size_t len);
6297
6298 static int write_ppl_header(unsigned long long ppl_sector, int fd, void *buf)
6299 {
6300 struct ppl_header *ppl_hdr = buf;
6301 int ret;
6302
6303 ppl_hdr->checksum = __cpu_to_le32(~crc32c_le(~0, buf, PPL_HEADER_SIZE));
6304
6305 if (lseek64(fd, ppl_sector * 512, SEEK_SET) < 0) {
6306 ret = -errno;
6307 perror("Failed to seek to PPL header location");
6308 return ret;
6309 }
6310
6311 if (write(fd, buf, PPL_HEADER_SIZE) != PPL_HEADER_SIZE) {
6312 ret = -errno;
6313 perror("Write PPL header failed");
6314 return ret;
6315 }
6316
6317 fsync(fd);
6318
6319 return 0;
6320 }
6321
6322 static int write_init_ppl_imsm(struct supertype *st, struct mdinfo *info, int fd)
6323 {
6324 struct intel_super *super = st->sb;
6325 void *buf;
6326 struct ppl_header *ppl_hdr;
6327 int ret;
6328
6329 /* first clear entire ppl space */
6330 ret = zero_disk_range(fd, info->ppl_sector, info->ppl_size);
6331 if (ret)
6332 return ret;
6333
6334 ret = posix_memalign(&buf, MAX_SECTOR_SIZE, PPL_HEADER_SIZE);
6335 if (ret) {
6336 pr_err("Failed to allocate PPL header buffer\n");
6337 return -ret;
6338 }
6339
6340 memset(buf, 0, PPL_HEADER_SIZE);
6341 ppl_hdr = buf;
6342 memset(ppl_hdr->reserved, 0xff, PPL_HDR_RESERVED);
6343 ppl_hdr->signature = __cpu_to_le32(super->anchor->orig_family_num);
6344
6345 if (info->mismatch_cnt) {
6346 /*
6347 * We are overwriting an invalid ppl. Make one entry with wrong
6348 * checksum to prevent the kernel from skipping resync.
6349 */
6350 ppl_hdr->entries_count = __cpu_to_le32(1);
6351 ppl_hdr->entries[0].checksum = ~0;
6352 }
6353
6354 ret = write_ppl_header(info->ppl_sector, fd, buf);
6355
6356 free(buf);
6357 return ret;
6358 }
6359
6360 static int is_rebuilding(struct imsm_dev *dev);
6361
6362 static int validate_ppl_imsm(struct supertype *st, struct mdinfo *info,
6363 struct mdinfo *disk)
6364 {
6365 struct intel_super *super = st->sb;
6366 struct dl *d;
6367 void *buf_orig, *buf, *buf_prev = NULL;
6368 int ret = 0;
6369 struct ppl_header *ppl_hdr = NULL;
6370 __u32 crc;
6371 struct imsm_dev *dev;
6372 __u32 idx;
6373 unsigned int i;
6374 unsigned long long ppl_offset = 0;
6375 unsigned long long prev_gen_num = 0;
6376
6377 if (disk->disk.raid_disk < 0)
6378 return 0;
6379
6380 dev = get_imsm_dev(super, info->container_member);
6381 idx = get_imsm_disk_idx(dev, disk->disk.raid_disk, MAP_0);
6382 d = get_imsm_dl_disk(super, idx);
6383
6384 if (!d || d->index < 0 || is_failed(&d->disk))
6385 return 0;
6386
6387 if (posix_memalign(&buf_orig, MAX_SECTOR_SIZE, PPL_HEADER_SIZE * 2)) {
6388 pr_err("Failed to allocate PPL header buffer\n");
6389 return -1;
6390 }
6391 buf = buf_orig;
6392
6393 ret = 1;
6394 while (ppl_offset < MULTIPLE_PPL_AREA_SIZE_IMSM) {
6395 void *tmp;
6396
6397 dprintf("Checking potential PPL at offset: %llu\n", ppl_offset);
6398
6399 if (lseek64(d->fd, info->ppl_sector * 512 + ppl_offset,
6400 SEEK_SET) < 0) {
6401 perror("Failed to seek to PPL header location");
6402 ret = -1;
6403 break;
6404 }
6405
6406 if (read(d->fd, buf, PPL_HEADER_SIZE) != PPL_HEADER_SIZE) {
6407 perror("Read PPL header failed");
6408 ret = -1;
6409 break;
6410 }
6411
6412 ppl_hdr = buf;
6413
6414 crc = __le32_to_cpu(ppl_hdr->checksum);
6415 ppl_hdr->checksum = 0;
6416
6417 if (crc != ~crc32c_le(~0, buf, PPL_HEADER_SIZE)) {
6418 dprintf("Wrong PPL header checksum on %s\n",
6419 d->devname);
6420 break;
6421 }
6422
6423 if (prev_gen_num > __le64_to_cpu(ppl_hdr->generation)) {
6424 /* previous was newest, it was already checked */
6425 break;
6426 }
6427
6428 if ((__le32_to_cpu(ppl_hdr->signature) !=
6429 super->anchor->orig_family_num)) {
6430 dprintf("Wrong PPL header signature on %s\n",
6431 d->devname);
6432 ret = 1;
6433 break;
6434 }
6435
6436 ret = 0;
6437 prev_gen_num = __le64_to_cpu(ppl_hdr->generation);
6438
6439 ppl_offset += PPL_HEADER_SIZE;
6440 for (i = 0; i < __le32_to_cpu(ppl_hdr->entries_count); i++)
6441 ppl_offset +=
6442 __le32_to_cpu(ppl_hdr->entries[i].pp_size);
6443
6444 if (!buf_prev)
6445 buf_prev = buf + PPL_HEADER_SIZE;
6446 tmp = buf_prev;
6447 buf_prev = buf;
6448 buf = tmp;
6449 }
6450
6451 if (buf_prev) {
6452 buf = buf_prev;
6453 ppl_hdr = buf_prev;
6454 }
6455
6456 /*
6457 * Update metadata to use mutliple PPLs area (1MB).
6458 * This is done once for all RAID members
6459 */
6460 if (info->consistency_policy == CONSISTENCY_POLICY_PPL &&
6461 info->ppl_size != (MULTIPLE_PPL_AREA_SIZE_IMSM >> 9)) {
6462 char subarray[20];
6463 struct mdinfo *member_dev;
6464
6465 sprintf(subarray, "%d", info->container_member);
6466
6467 if (mdmon_running(st->container_devnm))
6468 st->update_tail = &st->updates;
6469
6470 if (st->ss->update_subarray(st, subarray, "ppl", NULL)) {
6471 pr_err("Failed to update subarray %s\n",
6472 subarray);
6473 } else {
6474 if (st->update_tail)
6475 flush_metadata_updates(st);
6476 else
6477 st->ss->sync_metadata(st);
6478 info->ppl_size = (MULTIPLE_PPL_AREA_SIZE_IMSM >> 9);
6479 for (member_dev = info->devs; member_dev;
6480 member_dev = member_dev->next)
6481 member_dev->ppl_size =
6482 (MULTIPLE_PPL_AREA_SIZE_IMSM >> 9);
6483 }
6484 }
6485
6486 if (ret == 1) {
6487 struct imsm_map *map = get_imsm_map(dev, MAP_X);
6488
6489 if (map->map_state == IMSM_T_STATE_UNINITIALIZED ||
6490 (map->map_state == IMSM_T_STATE_NORMAL &&
6491 !(dev->vol.dirty & RAIDVOL_DIRTY)) ||
6492 (is_rebuilding(dev) &&
6493 vol_curr_migr_unit(dev) == 0 &&
6494 get_imsm_disk_idx(dev, disk->disk.raid_disk, MAP_1) != idx))
6495 ret = st->ss->write_init_ppl(st, info, d->fd);
6496 else
6497 info->mismatch_cnt++;
6498 } else if (ret == 0 &&
6499 ppl_hdr->entries_count == 0 &&
6500 is_rebuilding(dev) &&
6501 info->resync_start == 0) {
6502 /*
6503 * The header has no entries - add a single empty entry and
6504 * rewrite the header to prevent the kernel from going into
6505 * resync after an interrupted rebuild.
6506 */
6507 ppl_hdr->entries_count = __cpu_to_le32(1);
6508 ret = write_ppl_header(info->ppl_sector, d->fd, buf);
6509 }
6510
6511 free(buf_orig);
6512
6513 return ret;
6514 }
6515
6516 static int write_init_ppl_imsm_all(struct supertype *st, struct mdinfo *info)
6517 {
6518 struct intel_super *super = st->sb;
6519 struct dl *d;
6520 int ret = 0;
6521
6522 if (info->consistency_policy != CONSISTENCY_POLICY_PPL ||
6523 info->array.level != 5)
6524 return 0;
6525
6526 for (d = super->disks; d ; d = d->next) {
6527 if (d->index < 0 || is_failed(&d->disk))
6528 continue;
6529
6530 ret = st->ss->write_init_ppl(st, info, d->fd);
6531 if (ret)
6532 break;
6533 }
6534
6535 return ret;
6536 }
6537
6538 /*******************************************************************************
6539 * Function: write_init_bitmap_imsm_vol
6540 * Description: Write a bitmap header and prepares the area for the bitmap.
6541 * Parameters:
6542 * st : supertype information
6543 * vol_idx : the volume index to use
6544 *
6545 * Returns:
6546 * 0 : success
6547 * -1 : fail
6548 ******************************************************************************/
6549 static int write_init_bitmap_imsm_vol(struct supertype *st, int vol_idx)
6550 {
6551 struct intel_super *super = st->sb;
6552 int prev_current_vol = super->current_vol;
6553 struct dl *d;
6554 int ret = 0;
6555
6556 super->current_vol = vol_idx;
6557 for (d = super->disks; d; d = d->next) {
6558 if (d->index < 0 || is_failed(&d->disk))
6559 continue;
6560 ret = st->ss->write_bitmap(st, d->fd, NoUpdate);
6561 if (ret)
6562 break;
6563 }
6564 super->current_vol = prev_current_vol;
6565 return ret;
6566 }
6567
6568 /*******************************************************************************
6569 * Function: write_init_bitmap_imsm_all
6570 * Description: Write a bitmap header and prepares the area for the bitmap.
6571 * Operation is executed for volumes with CONSISTENCY_POLICY_BITMAP.
6572 * Parameters:
6573 * st : supertype information
6574 * info : info about the volume where the bitmap should be written
6575 * vol_idx : the volume index to use
6576 *
6577 * Returns:
6578 * 0 : success
6579 * -1 : fail
6580 ******************************************************************************/
6581 static int write_init_bitmap_imsm_all(struct supertype *st, struct mdinfo *info,
6582 int vol_idx)
6583 {
6584 int ret = 0;
6585
6586 if (info && (info->consistency_policy == CONSISTENCY_POLICY_BITMAP))
6587 ret = write_init_bitmap_imsm_vol(st, vol_idx);
6588
6589 return ret;
6590 }
6591
6592 static int write_init_super_imsm(struct supertype *st)
6593 {
6594 struct intel_super *super = st->sb;
6595 int current_vol = super->current_vol;
6596 int rv = 0;
6597 struct mdinfo info;
6598
6599 getinfo_super_imsm(st, &info, NULL);
6600
6601 /* we are done with current_vol reset it to point st at the container */
6602 super->current_vol = -1;
6603
6604 if (st->update_tail) {
6605 /* queue the recently created array / added disk
6606 * as a metadata update */
6607
6608 /* determine if we are creating a volume or adding a disk */
6609 if (current_vol < 0) {
6610 /* in the mgmt (add/remove) disk case we are running
6611 * in mdmon context, so don't close fd's
6612 */
6613 rv = mgmt_disk(st);
6614 } else {
6615 /* adding the second volume to the array */
6616 rv = write_init_ppl_imsm_all(st, &info);
6617 if (!rv)
6618 rv = write_init_bitmap_imsm_all(st, &info, current_vol);
6619 if (!rv)
6620 rv = create_array(st, current_vol);
6621 }
6622 } else {
6623 struct dl *d;
6624 for (d = super->disks; d; d = d->next)
6625 Kill(d->devname, NULL, 0, -1, 1);
6626 if (current_vol >= 0) {
6627 rv = write_init_ppl_imsm_all(st, &info);
6628 if (!rv)
6629 rv = write_init_bitmap_imsm_all(st, &info, current_vol);
6630 }
6631
6632 if (!rv)
6633 rv = write_super_imsm(st, 1);
6634 }
6635
6636 return rv;
6637 }
6638
6639 static int store_super_imsm(struct supertype *st, int fd)
6640 {
6641 struct intel_super *super = st->sb;
6642 struct imsm_super *mpb = super ? super->anchor : NULL;
6643
6644 if (!mpb)
6645 return 1;
6646
6647 if (super->sector_size == 4096)
6648 convert_to_4k(super);
6649 return store_imsm_mpb(fd, mpb);
6650 }
6651
6652 static int validate_geometry_imsm_container(struct supertype *st, int level,
6653 int raiddisks,
6654 unsigned long long data_offset,
6655 char *dev,
6656 unsigned long long *freesize,
6657 int verbose)
6658 {
6659 int fd;
6660 unsigned long long ldsize;
6661 struct intel_super *super = NULL;
6662 int rv = 0;
6663
6664 if (level != LEVEL_CONTAINER)
6665 return 0;
6666 if (!dev)
6667 return 1;
6668
6669 fd = dev_open(dev, O_RDONLY|O_EXCL);
6670 if (!is_fd_valid(fd)) {
6671 pr_vrb("imsm: Cannot open %s: %s\n", dev, strerror(errno));
6672 return 0;
6673 }
6674 if (!get_dev_size(fd, dev, &ldsize))
6675 goto exit;
6676
6677 /* capabilities retrieve could be possible
6678 * note that there is no fd for the disks in array.
6679 */
6680 super = alloc_super();
6681 if (!super)
6682 goto exit;
6683
6684 if (!get_dev_sector_size(fd, NULL, &super->sector_size))
6685 goto exit;
6686
6687 rv = find_intel_hba_capability(fd, super, verbose > 0 ? dev : NULL);
6688 if (rv != 0) {
6689 #if DEBUG
6690 char str[256];
6691 fd2devname(fd, str);
6692 dprintf("fd: %d %s orom: %p rv: %d raiddisk: %d\n",
6693 fd, str, super->orom, rv, raiddisks);
6694 #endif
6695 /* no orom/efi or non-intel hba of the disk */
6696 rv = 0;
6697 goto exit;
6698 }
6699 if (super->orom) {
6700 if (raiddisks > super->orom->tds) {
6701 if (verbose)
6702 pr_err("%d exceeds maximum number of platform supported disks: %d\n",
6703 raiddisks, super->orom->tds);
6704 goto exit;
6705 }
6706 if ((super->orom->attr & IMSM_OROM_ATTR_2TB_DISK) == 0 &&
6707 (ldsize >> 9) >> 32 > 0) {
6708 if (verbose)
6709 pr_err("%s exceeds maximum platform supported size\n", dev);
6710 goto exit;
6711 }
6712
6713 if (super->hba->type == SYS_DEV_VMD ||
6714 super->hba->type == SYS_DEV_NVME) {
6715 if (!imsm_is_nvme_namespace_supported(fd, 1)) {
6716 if (verbose)
6717 pr_err("NVMe namespace %s is not supported by IMSM\n",
6718 basename(dev));
6719 goto exit;
6720 }
6721 }
6722 }
6723 if (freesize)
6724 *freesize = avail_size_imsm(st, ldsize >> 9, data_offset);
6725 rv = 1;
6726 exit:
6727 if (super)
6728 free_imsm(super);
6729 close(fd);
6730
6731 return rv;
6732 }
6733
6734 static unsigned long long find_size(struct extent *e, int *idx, int num_extents)
6735 {
6736 const unsigned long long base_start = e[*idx].start;
6737 unsigned long long end = base_start + e[*idx].size;
6738 int i;
6739
6740 if (base_start == end)
6741 return 0;
6742
6743 *idx = *idx + 1;
6744 for (i = *idx; i < num_extents; i++) {
6745 /* extend overlapping extents */
6746 if (e[i].start >= base_start &&
6747 e[i].start <= end) {
6748 if (e[i].size == 0)
6749 return 0;
6750 if (e[i].start + e[i].size > end)
6751 end = e[i].start + e[i].size;
6752 } else if (e[i].start > end) {
6753 *idx = i;
6754 break;
6755 }
6756 }
6757
6758 return end - base_start;
6759 }
6760
6761 static unsigned long long merge_extents(struct intel_super *super, int sum_extents)
6762 {
6763 /* build a composite disk with all known extents and generate a new
6764 * 'maxsize' given the "all disks in an array must share a common start
6765 * offset" constraint
6766 */
6767 struct extent *e = xcalloc(sum_extents, sizeof(*e));
6768 struct dl *dl;
6769 int i, j;
6770 int start_extent;
6771 unsigned long long pos;
6772 unsigned long long start = 0;
6773 unsigned long long maxsize;
6774 unsigned long reserve;
6775
6776 /* coalesce and sort all extents. also, check to see if we need to
6777 * reserve space between member arrays
6778 */
6779 j = 0;
6780 for (dl = super->disks; dl; dl = dl->next) {
6781 if (!dl->e)
6782 continue;
6783 for (i = 0; i < dl->extent_cnt; i++)
6784 e[j++] = dl->e[i];
6785 }
6786 qsort(e, sum_extents, sizeof(*e), cmp_extent);
6787
6788 /* merge extents */
6789 i = 0;
6790 j = 0;
6791 while (i < sum_extents) {
6792 e[j].start = e[i].start;
6793 e[j].size = find_size(e, &i, sum_extents);
6794 j++;
6795 if (e[j-1].size == 0)
6796 break;
6797 }
6798
6799 pos = 0;
6800 maxsize = 0;
6801 start_extent = 0;
6802 i = 0;
6803 do {
6804 unsigned long long esize;
6805
6806 esize = e[i].start - pos;
6807 if (esize >= maxsize) {
6808 maxsize = esize;
6809 start = pos;
6810 start_extent = i;
6811 }
6812 pos = e[i].start + e[i].size;
6813 i++;
6814 } while (e[i-1].size);
6815 free(e);
6816
6817 if (maxsize == 0)
6818 return 0;
6819
6820 /* FIXME assumes volume at offset 0 is the first volume in a
6821 * container
6822 */
6823 if (start_extent > 0)
6824 reserve = IMSM_RESERVED_SECTORS; /* gap between raid regions */
6825 else
6826 reserve = 0;
6827
6828 if (maxsize < reserve)
6829 return 0;
6830
6831 super->create_offset = ~((unsigned long long) 0);
6832 if (start + reserve > super->create_offset)
6833 return 0; /* start overflows create_offset */
6834 super->create_offset = start + reserve;
6835
6836 return maxsize - reserve;
6837 }
6838
6839 static int is_raid_level_supported(const struct imsm_orom *orom, int level, int raiddisks)
6840 {
6841 if (level < 0 || level == 6 || level == 4)
6842 return 0;
6843
6844 /* if we have an orom prevent invalid raid levels */
6845 if (orom)
6846 switch (level) {
6847 case 0: return imsm_orom_has_raid0(orom);
6848 case 1:
6849 if (raiddisks > 2)
6850 return imsm_orom_has_raid1e(orom);
6851 return imsm_orom_has_raid1(orom) && raiddisks == 2;
6852 case 10: return imsm_orom_has_raid10(orom) && raiddisks == 4;
6853 case 5: return imsm_orom_has_raid5(orom) && raiddisks > 2;
6854 }
6855 else
6856 return 1; /* not on an Intel RAID platform so anything goes */
6857
6858 return 0;
6859 }
6860
6861 static int
6862 active_arrays_by_format(char *name, char* hba, struct md_list **devlist,
6863 int dpa, int verbose)
6864 {
6865 struct mdstat_ent *mdstat = mdstat_read(0, 0);
6866 struct mdstat_ent *memb;
6867 int count = 0;
6868 int num = 0;
6869 struct md_list *dv;
6870 int found;
6871
6872 for (memb = mdstat ; memb ; memb = memb->next) {
6873 if (memb->metadata_version &&
6874 (strncmp(memb->metadata_version, "external:", 9) == 0) &&
6875 (strcmp(&memb->metadata_version[9], name) == 0) &&
6876 !is_subarray(memb->metadata_version+9) &&
6877 memb->members) {
6878 struct dev_member *dev = memb->members;
6879 int fd = -1;
6880 while (dev && !is_fd_valid(fd)) {
6881 char *path = xmalloc(strlen(dev->name) + strlen("/dev/") + 1);
6882 num = sprintf(path, "%s%s", "/dev/", dev->name);
6883 if (num > 0)
6884 fd = open(path, O_RDONLY, 0);
6885 if (num <= 0 || !is_fd_valid(fd)) {
6886 pr_vrb("Cannot open %s: %s\n",
6887 dev->name, strerror(errno));
6888 }
6889 free(path);
6890 dev = dev->next;
6891 }
6892 found = 0;
6893 if (is_fd_valid(fd) && disk_attached_to_hba(fd, hba)) {
6894 struct mdstat_ent *vol;
6895 for (vol = mdstat ; vol ; vol = vol->next) {
6896 if (vol->active > 0 &&
6897 vol->metadata_version &&
6898 is_container_member(vol, memb->devnm)) {
6899 found++;
6900 count++;
6901 }
6902 }
6903 if (*devlist && (found < dpa)) {
6904 dv = xcalloc(1, sizeof(*dv));
6905 dv->devname = xmalloc(strlen(memb->devnm) + strlen("/dev/") + 1);
6906 sprintf(dv->devname, "%s%s", "/dev/", memb->devnm);
6907 dv->found = found;
6908 dv->used = 0;
6909 dv->next = *devlist;
6910 *devlist = dv;
6911 }
6912 }
6913 close_fd(&fd);
6914 }
6915 }
6916 free_mdstat(mdstat);
6917 return count;
6918 }
6919
6920 #ifdef DEBUG_LOOP
6921 static struct md_list*
6922 get_loop_devices(void)
6923 {
6924 int i;
6925 struct md_list *devlist = NULL;
6926 struct md_list *dv;
6927
6928 for(i = 0; i < 12; i++) {
6929 dv = xcalloc(1, sizeof(*dv));
6930 dv->devname = xmalloc(40);
6931 sprintf(dv->devname, "/dev/loop%d", i);
6932 dv->next = devlist;
6933 devlist = dv;
6934 }
6935 return devlist;
6936 }
6937 #endif
6938
6939 static struct md_list*
6940 get_devices(const char *hba_path)
6941 {
6942 struct md_list *devlist = NULL;
6943 struct md_list *dv;
6944 struct dirent *ent;
6945 DIR *dir;
6946 int err = 0;
6947
6948 #if DEBUG_LOOP
6949 devlist = get_loop_devices();
6950 return devlist;
6951 #endif
6952 /* scroll through /sys/dev/block looking for devices attached to
6953 * this hba
6954 */
6955 dir = opendir("/sys/dev/block");
6956 for (ent = dir ? readdir(dir) : NULL; ent; ent = readdir(dir)) {
6957 int fd;
6958 char buf[1024];
6959 int major, minor;
6960 char *path = NULL;
6961 if (sscanf(ent->d_name, "%d:%d", &major, &minor) != 2)
6962 continue;
6963 path = devt_to_devpath(makedev(major, minor), 1, NULL);
6964 if (!path)
6965 continue;
6966 if (!path_attached_to_hba(path, hba_path)) {
6967 free(path);
6968 path = NULL;
6969 continue;
6970 }
6971 free(path);
6972 path = NULL;
6973 fd = dev_open(ent->d_name, O_RDONLY);
6974 if (is_fd_valid(fd)) {
6975 fd2devname(fd, buf);
6976 close(fd);
6977 } else {
6978 pr_err("cannot open device: %s\n",
6979 ent->d_name);
6980 continue;
6981 }
6982
6983 dv = xcalloc(1, sizeof(*dv));
6984 dv->devname = xstrdup(buf);
6985 dv->next = devlist;
6986 devlist = dv;
6987 }
6988 if (err) {
6989 while(devlist) {
6990 dv = devlist;
6991 devlist = devlist->next;
6992 free(dv->devname);
6993 free(dv);
6994 }
6995 }
6996 closedir(dir);
6997 return devlist;
6998 }
6999
7000 static int
7001 count_volumes_list(struct md_list *devlist, char *homehost,
7002 int verbose, int *found)
7003 {
7004 struct md_list *tmpdev;
7005 int count = 0;
7006 struct supertype *st;
7007
7008 /* first walk the list of devices to find a consistent set
7009 * that match the criterea, if that is possible.
7010 * We flag the ones we like with 'used'.
7011 */
7012 *found = 0;
7013 st = match_metadata_desc_imsm("imsm");
7014 if (st == NULL) {
7015 pr_vrb("cannot allocate memory for imsm supertype\n");
7016 return 0;
7017 }
7018
7019 for (tmpdev = devlist; tmpdev; tmpdev = tmpdev->next) {
7020 char *devname = tmpdev->devname;
7021 dev_t rdev;
7022 struct supertype *tst;
7023 int dfd;
7024 if (tmpdev->used > 1)
7025 continue;
7026 tst = dup_super(st);
7027 if (tst == NULL) {
7028 pr_vrb("cannot allocate memory for imsm supertype\n");
7029 goto err_1;
7030 }
7031 tmpdev->container = 0;
7032 dfd = dev_open(devname, O_RDONLY|O_EXCL);
7033 if (!is_fd_valid(dfd)) {
7034 dprintf("cannot open device %s: %s\n",
7035 devname, strerror(errno));
7036 tmpdev->used = 2;
7037 } else if (!fstat_is_blkdev(dfd, devname, &rdev)) {
7038 tmpdev->used = 2;
7039 } else if (must_be_container(dfd)) {
7040 struct supertype *cst;
7041 cst = super_by_fd(dfd, NULL);
7042 if (cst == NULL) {
7043 dprintf("cannot recognize container type %s\n",
7044 devname);
7045 tmpdev->used = 2;
7046 } else if (tst->ss != st->ss) {
7047 dprintf("non-imsm container - ignore it: %s\n",
7048 devname);
7049 tmpdev->used = 2;
7050 } else if (!tst->ss->load_container ||
7051 tst->ss->load_container(tst, dfd, NULL))
7052 tmpdev->used = 2;
7053 else {
7054 tmpdev->container = 1;
7055 }
7056 if (cst)
7057 cst->ss->free_super(cst);
7058 } else {
7059 tmpdev->st_rdev = rdev;
7060 if (tst->ss->load_super(tst,dfd, NULL)) {
7061 dprintf("no RAID superblock on %s\n",
7062 devname);
7063 tmpdev->used = 2;
7064 } else if (tst->ss->compare_super == NULL) {
7065 dprintf("Cannot assemble %s metadata on %s\n",
7066 tst->ss->name, devname);
7067 tmpdev->used = 2;
7068 }
7069 }
7070 close_fd(&dfd);
7071
7072 if (tmpdev->used == 2 || tmpdev->used == 4) {
7073 /* Ignore unrecognised devices during auto-assembly */
7074 goto loop;
7075 }
7076 else {
7077 struct mdinfo info;
7078 tst->ss->getinfo_super(tst, &info, NULL);
7079
7080 if (st->minor_version == -1)
7081 st->minor_version = tst->minor_version;
7082
7083 if (memcmp(info.uuid, uuid_zero,
7084 sizeof(int[4])) == 0) {
7085 /* this is a floating spare. It cannot define
7086 * an array unless there are no more arrays of
7087 * this type to be found. It can be included
7088 * in an array of this type though.
7089 */
7090 tmpdev->used = 3;
7091 goto loop;
7092 }
7093
7094 if (st->ss != tst->ss ||
7095 st->minor_version != tst->minor_version ||
7096 st->ss->compare_super(st, tst, 1) != 0) {
7097 /* Some mismatch. If exactly one array matches this host,
7098 * we can resolve on that one.
7099 * Or, if we are auto assembling, we just ignore the second
7100 * for now.
7101 */
7102 dprintf("superblock on %s doesn't match others - assembly aborted\n",
7103 devname);
7104 goto loop;
7105 }
7106 tmpdev->used = 1;
7107 *found = 1;
7108 dprintf("found: devname: %s\n", devname);
7109 }
7110 loop:
7111 if (tst)
7112 tst->ss->free_super(tst);
7113 }
7114 if (*found != 0) {
7115 int err;
7116 if ((err = load_super_imsm_all(st, -1, &st->sb, NULL, devlist, 0)) == 0) {
7117 struct mdinfo *iter, *head = st->ss->container_content(st, NULL);
7118 for (iter = head; iter; iter = iter->next) {
7119 dprintf("content->text_version: %s vol\n",
7120 iter->text_version);
7121 if (iter->array.state & (1<<MD_SB_BLOCK_VOLUME)) {
7122 /* do not assemble arrays with unsupported
7123 configurations */
7124 dprintf("Cannot activate member %s.\n",
7125 iter->text_version);
7126 } else
7127 count++;
7128 }
7129 sysfs_free(head);
7130
7131 } else {
7132 dprintf("No valid super block on device list: err: %d %p\n",
7133 err, st->sb);
7134 }
7135 } else {
7136 dprintf("no more devices to examine\n");
7137 }
7138
7139 for (tmpdev = devlist; tmpdev; tmpdev = tmpdev->next) {
7140 if (tmpdev->used == 1 && tmpdev->found) {
7141 if (count) {
7142 if (count < tmpdev->found)
7143 count = 0;
7144 else
7145 count -= tmpdev->found;
7146 }
7147 }
7148 if (tmpdev->used == 1)
7149 tmpdev->used = 4;
7150 }
7151 err_1:
7152 if (st)
7153 st->ss->free_super(st);
7154 return count;
7155 }
7156
7157 static int __count_volumes(char *hba_path, int dpa, int verbose,
7158 int cmp_hba_path)
7159 {
7160 struct sys_dev *idev, *intel_devices = find_intel_devices();
7161 int count = 0;
7162 const struct orom_entry *entry;
7163 struct devid_list *dv, *devid_list;
7164
7165 if (!hba_path)
7166 return 0;
7167
7168 for (idev = intel_devices; idev; idev = idev->next) {
7169 if (strstr(idev->path, hba_path))
7170 break;
7171 }
7172
7173 if (!idev || !idev->dev_id)
7174 return 0;
7175
7176 entry = get_orom_entry_by_device_id(idev->dev_id);
7177
7178 if (!entry || !entry->devid_list)
7179 return 0;
7180
7181 devid_list = entry->devid_list;
7182 for (dv = devid_list; dv; dv = dv->next) {
7183 struct md_list *devlist;
7184 struct sys_dev *device = NULL;
7185 char *hpath;
7186 int found = 0;
7187
7188 if (cmp_hba_path)
7189 device = device_by_id_and_path(dv->devid, hba_path);
7190 else
7191 device = device_by_id(dv->devid);
7192
7193 if (device)
7194 hpath = device->path;
7195 else
7196 return 0;
7197
7198 devlist = get_devices(hpath);
7199 /* if no intel devices return zero volumes */
7200 if (devlist == NULL)
7201 return 0;
7202
7203 count += active_arrays_by_format("imsm", hpath, &devlist, dpa,
7204 verbose);
7205 dprintf("path: %s active arrays: %d\n", hpath, count);
7206 if (devlist == NULL)
7207 return 0;
7208 do {
7209 found = 0;
7210 count += count_volumes_list(devlist,
7211 NULL,
7212 verbose,
7213 &found);
7214 dprintf("found %d count: %d\n", found, count);
7215 } while (found);
7216
7217 dprintf("path: %s total number of volumes: %d\n", hpath, count);
7218
7219 while (devlist) {
7220 struct md_list *dv = devlist;
7221 devlist = devlist->next;
7222 free(dv->devname);
7223 free(dv);
7224 }
7225 }
7226 return count;
7227 }
7228
7229 static int count_volumes(struct intel_hba *hba, int dpa, int verbose)
7230 {
7231 if (!hba)
7232 return 0;
7233 if (hba->type == SYS_DEV_VMD) {
7234 struct sys_dev *dev;
7235 int count = 0;
7236
7237 for (dev = find_intel_devices(); dev; dev = dev->next) {
7238 if (dev->type == SYS_DEV_VMD)
7239 count += __count_volumes(dev->path, dpa,
7240 verbose, 1);
7241 }
7242 return count;
7243 }
7244 return __count_volumes(hba->path, dpa, verbose, 0);
7245 }
7246
7247 static int imsm_default_chunk(const struct imsm_orom *orom)
7248 {
7249 /* up to 512 if the plaform supports it, otherwise the platform max.
7250 * 128 if no platform detected
7251 */
7252 int fs = max(7, orom ? fls(orom->sss) : 0);
7253
7254 return min(512, (1 << fs));
7255 }
7256
7257 static int
7258 validate_geometry_imsm_orom(struct intel_super *super, int level, int layout,
7259 int raiddisks, int *chunk, unsigned long long size, int verbose)
7260 {
7261 /* check/set platform and metadata limits/defaults */
7262 if (super->orom && raiddisks > super->orom->dpa) {
7263 pr_vrb("platform supports a maximum of %d disks per array\n",
7264 super->orom->dpa);
7265 return 0;
7266 }
7267
7268 /* capabilities of OROM tested - copied from validate_geometry_imsm_volume */
7269 if (!is_raid_level_supported(super->orom, level, raiddisks)) {
7270 pr_vrb("platform does not support raid%d with %d disk%s\n",
7271 level, raiddisks, raiddisks > 1 ? "s" : "");
7272 return 0;
7273 }
7274
7275 if (*chunk == 0 || *chunk == UnSet)
7276 *chunk = imsm_default_chunk(super->orom);
7277
7278 if (super->orom && !imsm_orom_has_chunk(super->orom, *chunk)) {
7279 pr_vrb("platform does not support a chunk size of: %d\n", *chunk);
7280 return 0;
7281 }
7282
7283 if (layout != imsm_level_to_layout(level)) {
7284 if (level == 5)
7285 pr_vrb("imsm raid 5 only supports the left-asymmetric layout\n");
7286 else if (level == 10)
7287 pr_vrb("imsm raid 10 only supports the n2 layout\n");
7288 else
7289 pr_vrb("imsm unknown layout %#x for this raid level %d\n",
7290 layout, level);
7291 return 0;
7292 }
7293
7294 if (super->orom && (super->orom->attr & IMSM_OROM_ATTR_2TB) == 0 &&
7295 (calc_array_size(level, raiddisks, layout, *chunk, size) >> 32) > 0) {
7296 pr_vrb("platform does not support a volume size over 2TB\n");
7297 return 0;
7298 }
7299
7300 return 1;
7301 }
7302
7303 /* validate_geometry_imsm_volume - lifted from validate_geometry_ddf_bvd
7304 * FIX ME add ahci details
7305 */
7306 static int validate_geometry_imsm_volume(struct supertype *st, int level,
7307 int layout, int raiddisks, int *chunk,
7308 unsigned long long size,
7309 unsigned long long data_offset,
7310 char *dev,
7311 unsigned long long *freesize,
7312 int verbose)
7313 {
7314 dev_t rdev;
7315 struct intel_super *super = st->sb;
7316 struct imsm_super *mpb;
7317 struct dl *dl;
7318 unsigned long long pos = 0;
7319 unsigned long long maxsize;
7320 struct extent *e;
7321 int i;
7322
7323 /* We must have the container info already read in. */
7324 if (!super)
7325 return 0;
7326
7327 mpb = super->anchor;
7328
7329 if (!validate_geometry_imsm_orom(super, level, layout, raiddisks, chunk, size, verbose)) {
7330 pr_err("RAID geometry validation failed. Cannot proceed with the action(s).\n");
7331 return 0;
7332 }
7333 if (!dev) {
7334 /* General test: make sure there is space for
7335 * 'raiddisks' device extents of size 'size' at a given
7336 * offset
7337 */
7338 unsigned long long minsize = size;
7339 unsigned long long start_offset = MaxSector;
7340 int dcnt = 0;
7341 if (minsize == 0)
7342 minsize = MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS;
7343 for (dl = super->disks; dl ; dl = dl->next) {
7344 int found = 0;
7345
7346 pos = 0;
7347 i = 0;
7348 e = get_extents(super, dl, 0);
7349 if (!e) continue;
7350 do {
7351 unsigned long long esize;
7352 esize = e[i].start - pos;
7353 if (esize >= minsize)
7354 found = 1;
7355 if (found && start_offset == MaxSector) {
7356 start_offset = pos;
7357 break;
7358 } else if (found && pos != start_offset) {
7359 found = 0;
7360 break;
7361 }
7362 pos = e[i].start + e[i].size;
7363 i++;
7364 } while (e[i-1].size);
7365 if (found)
7366 dcnt++;
7367 free(e);
7368 }
7369 if (dcnt < raiddisks) {
7370 if (verbose)
7371 pr_err("imsm: Not enough devices with space for this array (%d < %d)\n",
7372 dcnt, raiddisks);
7373 return 0;
7374 }
7375 return 1;
7376 }
7377
7378 /* This device must be a member of the set */
7379 if (!stat_is_blkdev(dev, &rdev))
7380 return 0;
7381 for (dl = super->disks ; dl ; dl = dl->next) {
7382 if (dl->major == (int)major(rdev) &&
7383 dl->minor == (int)minor(rdev))
7384 break;
7385 }
7386 if (!dl) {
7387 if (verbose)
7388 pr_err("%s is not in the same imsm set\n", dev);
7389 return 0;
7390 } else if (super->orom && dl->index < 0 && mpb->num_raid_devs) {
7391 /* If a volume is present then the current creation attempt
7392 * cannot incorporate new spares because the orom may not
7393 * understand this configuration (all member disks must be
7394 * members of each array in the container).
7395 */
7396 pr_err("%s is a spare and a volume is already defined for this container\n", dev);
7397 pr_err("The option-rom requires all member disks to be a member of all volumes\n");
7398 return 0;
7399 } else if (super->orom && mpb->num_raid_devs > 0 &&
7400 mpb->num_disks != raiddisks) {
7401 pr_err("The option-rom requires all member disks to be a member of all volumes\n");
7402 return 0;
7403 }
7404
7405 /* retrieve the largest free space block */
7406 e = get_extents(super, dl, 0);
7407 maxsize = 0;
7408 i = 0;
7409 if (e) {
7410 do {
7411 unsigned long long esize;
7412
7413 esize = e[i].start - pos;
7414 if (esize >= maxsize)
7415 maxsize = esize;
7416 pos = e[i].start + e[i].size;
7417 i++;
7418 } while (e[i-1].size);
7419 dl->e = e;
7420 dl->extent_cnt = i;
7421 } else {
7422 if (verbose)
7423 pr_err("unable to determine free space for: %s\n",
7424 dev);
7425 return 0;
7426 }
7427 if (maxsize < size) {
7428 if (verbose)
7429 pr_err("%s not enough space (%llu < %llu)\n",
7430 dev, maxsize, size);
7431 return 0;
7432 }
7433
7434 /* count total number of extents for merge */
7435 i = 0;
7436 for (dl = super->disks; dl; dl = dl->next)
7437 if (dl->e)
7438 i += dl->extent_cnt;
7439
7440 maxsize = merge_extents(super, i);
7441
7442 if (mpb->num_raid_devs > 0 && size && size != maxsize)
7443 pr_err("attempting to create a second volume with size less then remaining space.\n");
7444
7445 if (maxsize < size || maxsize == 0) {
7446 if (verbose) {
7447 if (maxsize == 0)
7448 pr_err("no free space left on device. Aborting...\n");
7449 else
7450 pr_err("not enough space to create volume of given size (%llu < %llu). Aborting...\n",
7451 maxsize, size);
7452 }
7453 return 0;
7454 }
7455
7456 *freesize = maxsize;
7457
7458 if (super->orom) {
7459 int count = count_volumes(super->hba,
7460 super->orom->dpa, verbose);
7461 if (super->orom->vphba <= count) {
7462 pr_vrb("platform does not support more than %d raid volumes.\n",
7463 super->orom->vphba);
7464 return 0;
7465 }
7466 }
7467 return 1;
7468 }
7469
7470 static int imsm_get_free_size(struct supertype *st, int raiddisks,
7471 unsigned long long size, int chunk,
7472 unsigned long long *freesize)
7473 {
7474 struct intel_super *super = st->sb;
7475 struct imsm_super *mpb = super->anchor;
7476 struct dl *dl;
7477 int i;
7478 int extent_cnt;
7479 struct extent *e;
7480 unsigned long long maxsize;
7481 unsigned long long minsize;
7482 int cnt;
7483 int used;
7484
7485 /* find the largest common start free region of the possible disks */
7486 used = 0;
7487 extent_cnt = 0;
7488 cnt = 0;
7489 for (dl = super->disks; dl; dl = dl->next) {
7490 dl->raiddisk = -1;
7491
7492 if (dl->index >= 0)
7493 used++;
7494
7495 /* don't activate new spares if we are orom constrained
7496 * and there is already a volume active in the container
7497 */
7498 if (super->orom && dl->index < 0 && mpb->num_raid_devs)
7499 continue;
7500
7501 e = get_extents(super, dl, 0);
7502 if (!e)
7503 continue;
7504 for (i = 1; e[i-1].size; i++)
7505 ;
7506 dl->e = e;
7507 dl->extent_cnt = i;
7508 extent_cnt += i;
7509 cnt++;
7510 }
7511
7512 maxsize = merge_extents(super, extent_cnt);
7513 minsize = size;
7514 if (size == 0)
7515 /* chunk is in K */
7516 minsize = chunk * 2;
7517
7518 if (cnt < raiddisks ||
7519 (super->orom && used && used != raiddisks) ||
7520 maxsize < minsize ||
7521 maxsize == 0) {
7522 pr_err("not enough devices with space to create array.\n");
7523 return 0; /* No enough free spaces large enough */
7524 }
7525
7526 if (size == 0) {
7527 size = maxsize;
7528 if (chunk) {
7529 size /= 2 * chunk;
7530 size *= 2 * chunk;
7531 }
7532 maxsize = size;
7533 }
7534 if (mpb->num_raid_devs > 0 && size && size != maxsize)
7535 pr_err("attempting to create a second volume with size less then remaining space.\n");
7536 cnt = 0;
7537 for (dl = super->disks; dl; dl = dl->next)
7538 if (dl->e)
7539 dl->raiddisk = cnt++;
7540
7541 *freesize = size;
7542
7543 dprintf("imsm: imsm_get_free_size() returns : %llu\n", size);
7544
7545 return 1;
7546 }
7547
7548 static int reserve_space(struct supertype *st, int raiddisks,
7549 unsigned long long size, int chunk,
7550 unsigned long long *freesize)
7551 {
7552 struct intel_super *super = st->sb;
7553 struct dl *dl;
7554 int cnt;
7555 int rv = 0;
7556
7557 rv = imsm_get_free_size(st, raiddisks, size, chunk, freesize);
7558 if (rv) {
7559 cnt = 0;
7560 for (dl = super->disks; dl; dl = dl->next)
7561 if (dl->e)
7562 dl->raiddisk = cnt++;
7563 rv = 1;
7564 }
7565
7566 return rv;
7567 }
7568
7569 static int validate_geometry_imsm(struct supertype *st, int level, int layout,
7570 int raiddisks, int *chunk, unsigned long long size,
7571 unsigned long long data_offset,
7572 char *dev, unsigned long long *freesize,
7573 int consistency_policy, int verbose)
7574 {
7575 int fd, cfd;
7576 struct mdinfo *sra;
7577 int is_member = 0;
7578
7579 /* load capability
7580 * if given unused devices create a container
7581 * if given given devices in a container create a member volume
7582 */
7583 if (level == LEVEL_CONTAINER)
7584 /* Must be a fresh device to add to a container */
7585 return validate_geometry_imsm_container(st, level, raiddisks,
7586 data_offset, dev,
7587 freesize, verbose);
7588
7589 /*
7590 * Size is given in sectors.
7591 */
7592 if (size && (size < 2048)) {
7593 pr_err("Given size must be greater than 1M.\n");
7594 /* Depends on algorithm in Create.c :
7595 * if container was given (dev == NULL) return -1,
7596 * if block device was given ( dev != NULL) return 0.
7597 */
7598 return dev ? -1 : 0;
7599 }
7600
7601 if (!dev) {
7602 if (st->sb) {
7603 struct intel_super *super = st->sb;
7604 if (!validate_geometry_imsm_orom(st->sb, level, layout,
7605 raiddisks, chunk, size,
7606 verbose))
7607 return 0;
7608 /* we are being asked to automatically layout a
7609 * new volume based on the current contents of
7610 * the container. If the the parameters can be
7611 * satisfied reserve_space will record the disks,
7612 * start offset, and size of the volume to be
7613 * created. add_to_super and getinfo_super
7614 * detect when autolayout is in progress.
7615 */
7616 /* assuming that freesize is always given when array is
7617 created */
7618 if (super->orom && freesize) {
7619 int count;
7620 count = count_volumes(super->hba,
7621 super->orom->dpa, verbose);
7622 if (super->orom->vphba <= count) {
7623 pr_vrb("platform does not support more than %d raid volumes.\n",
7624 super->orom->vphba);
7625 return 0;
7626 }
7627 }
7628 if (freesize)
7629 return reserve_space(st, raiddisks, size,
7630 *chunk, freesize);
7631 }
7632 return 1;
7633 }
7634 if (st->sb) {
7635 /* creating in a given container */
7636 return validate_geometry_imsm_volume(st, level, layout,
7637 raiddisks, chunk, size,
7638 data_offset,
7639 dev, freesize, verbose);
7640 }
7641
7642 /* This device needs to be a device in an 'imsm' container */
7643 fd = open(dev, O_RDONLY|O_EXCL, 0);
7644
7645 if (is_fd_valid(fd)) {
7646 pr_vrb("Cannot create this array on device %s\n", dev);
7647 close(fd);
7648 return 0;
7649 }
7650 if (errno == EBUSY)
7651 fd = open(dev, O_RDONLY, 0);
7652
7653 if (!is_fd_valid(fd)) {
7654 pr_vrb("Cannot open %s: %s\n", dev, strerror(errno));
7655 return 0;
7656 }
7657
7658 /* Well, it is in use by someone, maybe an 'imsm' container. */
7659 cfd = open_container(fd);
7660 close_fd(&fd);
7661
7662 if (!is_fd_valid(cfd)) {
7663 pr_vrb("Cannot use %s: It is busy\n", dev);
7664 return 0;
7665 }
7666 sra = sysfs_read(cfd, NULL, GET_VERSION);
7667 if (sra && sra->array.major_version == -1 &&
7668 strcmp(sra->text_version, "imsm") == 0)
7669 is_member = 1;
7670 sysfs_free(sra);
7671 if (is_member) {
7672 /* This is a member of a imsm container. Load the container
7673 * and try to create a volume
7674 */
7675 struct intel_super *super;
7676
7677 if (load_super_imsm_all(st, cfd, (void **) &super, NULL, NULL, 1) == 0) {
7678 st->sb = super;
7679 strcpy(st->container_devnm, fd2devnm(cfd));
7680 close(cfd);
7681 return validate_geometry_imsm_volume(st, level, layout,
7682 raiddisks, chunk,
7683 size, data_offset, dev,
7684 freesize, 1)
7685 ? 1 : -1;
7686 }
7687 }
7688
7689 if (verbose)
7690 pr_err("failed container membership check\n");
7691
7692 close(cfd);
7693 return 0;
7694 }
7695
7696 static void default_geometry_imsm(struct supertype *st, int *level, int *layout, int *chunk)
7697 {
7698 struct intel_super *super = st->sb;
7699
7700 if (level && *level == UnSet)
7701 *level = LEVEL_CONTAINER;
7702
7703 if (level && layout && *layout == UnSet)
7704 *layout = imsm_level_to_layout(*level);
7705
7706 if (chunk && (*chunk == UnSet || *chunk == 0))
7707 *chunk = imsm_default_chunk(super->orom);
7708 }
7709
7710 static void handle_missing(struct intel_super *super, struct imsm_dev *dev);
7711
7712 static int kill_subarray_imsm(struct supertype *st, char *subarray_id)
7713 {
7714 /* remove the subarray currently referenced by subarray_id */
7715 __u8 i;
7716 struct intel_dev **dp;
7717 struct intel_super *super = st->sb;
7718 __u8 current_vol = strtoul(subarray_id, NULL, 10);
7719 struct imsm_super *mpb = super->anchor;
7720
7721 if (mpb->num_raid_devs == 0)
7722 return 2;
7723
7724 /* block deletions that would change the uuid of active subarrays
7725 *
7726 * FIXME when immutable ids are available, but note that we'll
7727 * also need to fixup the invalidated/active subarray indexes in
7728 * mdstat
7729 */
7730 for (i = 0; i < mpb->num_raid_devs; i++) {
7731 char subarray[4];
7732
7733 if (i < current_vol)
7734 continue;
7735 sprintf(subarray, "%u", i);
7736 if (is_subarray_active(subarray, st->devnm)) {
7737 pr_err("deleting subarray-%d would change the UUID of active subarray-%d, aborting\n",
7738 current_vol, i);
7739
7740 return 2;
7741 }
7742 }
7743
7744 if (st->update_tail) {
7745 struct imsm_update_kill_array *u = xmalloc(sizeof(*u));
7746
7747 u->type = update_kill_array;
7748 u->dev_idx = current_vol;
7749 append_metadata_update(st, u, sizeof(*u));
7750
7751 return 0;
7752 }
7753
7754 for (dp = &super->devlist; *dp;)
7755 if ((*dp)->index == current_vol) {
7756 *dp = (*dp)->next;
7757 } else {
7758 handle_missing(super, (*dp)->dev);
7759 if ((*dp)->index > current_vol)
7760 (*dp)->index--;
7761 dp = &(*dp)->next;
7762 }
7763
7764 /* no more raid devices, all active components are now spares,
7765 * but of course failed are still failed
7766 */
7767 if (--mpb->num_raid_devs == 0) {
7768 struct dl *d;
7769
7770 for (d = super->disks; d; d = d->next)
7771 if (d->index > -2)
7772 mark_spare(d);
7773 }
7774
7775 super->updates_pending++;
7776
7777 return 0;
7778 }
7779
7780 static int get_rwh_policy_from_update(char *update)
7781 {
7782 if (strcmp(update, "ppl") == 0)
7783 return RWH_MULTIPLE_DISTRIBUTED;
7784 else if (strcmp(update, "no-ppl") == 0)
7785 return RWH_MULTIPLE_OFF;
7786 else if (strcmp(update, "bitmap") == 0)
7787 return RWH_BITMAP;
7788 else if (strcmp(update, "no-bitmap") == 0)
7789 return RWH_OFF;
7790 return -1;
7791 }
7792
7793 static int update_subarray_imsm(struct supertype *st, char *subarray,
7794 char *update, struct mddev_ident *ident)
7795 {
7796 /* update the subarray currently referenced by ->current_vol */
7797 struct intel_super *super = st->sb;
7798 struct imsm_super *mpb = super->anchor;
7799
7800 if (strcmp(update, "name") == 0) {
7801 char *name = ident->name;
7802 char *ep;
7803 int vol;
7804
7805 if (is_subarray_active(subarray, st->devnm)) {
7806 pr_err("Unable to update name of active subarray\n");
7807 return 2;
7808 }
7809
7810 if (!check_name(super, name, 0))
7811 return 2;
7812
7813 vol = strtoul(subarray, &ep, 10);
7814 if (*ep != '\0' || vol >= super->anchor->num_raid_devs)
7815 return 2;
7816
7817 if (st->update_tail) {
7818 struct imsm_update_rename_array *u = xmalloc(sizeof(*u));
7819
7820 u->type = update_rename_array;
7821 u->dev_idx = vol;
7822 strncpy((char *) u->name, name, MAX_RAID_SERIAL_LEN);
7823 u->name[MAX_RAID_SERIAL_LEN-1] = '\0';
7824 append_metadata_update(st, u, sizeof(*u));
7825 } else {
7826 struct imsm_dev *dev;
7827 int i, namelen;
7828
7829 dev = get_imsm_dev(super, vol);
7830 memset(dev->volume, '\0', MAX_RAID_SERIAL_LEN);
7831 namelen = min((int)strlen(name), MAX_RAID_SERIAL_LEN);
7832 memcpy(dev->volume, name, namelen);
7833 for (i = 0; i < mpb->num_raid_devs; i++) {
7834 dev = get_imsm_dev(super, i);
7835 handle_missing(super, dev);
7836 }
7837 super->updates_pending++;
7838 }
7839 } else if (get_rwh_policy_from_update(update) != -1) {
7840 int new_policy;
7841 char *ep;
7842 int vol = strtoul(subarray, &ep, 10);
7843
7844 if (*ep != '\0' || vol >= super->anchor->num_raid_devs)
7845 return 2;
7846
7847 new_policy = get_rwh_policy_from_update(update);
7848
7849 if (st->update_tail) {
7850 struct imsm_update_rwh_policy *u = xmalloc(sizeof(*u));
7851
7852 u->type = update_rwh_policy;
7853 u->dev_idx = vol;
7854 u->new_policy = new_policy;
7855 append_metadata_update(st, u, sizeof(*u));
7856 } else {
7857 struct imsm_dev *dev;
7858
7859 dev = get_imsm_dev(super, vol);
7860 dev->rwh_policy = new_policy;
7861 super->updates_pending++;
7862 }
7863 if (new_policy == RWH_BITMAP)
7864 return write_init_bitmap_imsm_vol(st, vol);
7865 } else
7866 return 2;
7867
7868 return 0;
7869 }
7870
7871 static bool is_gen_migration(struct imsm_dev *dev)
7872 {
7873 if (dev && dev->vol.migr_state &&
7874 migr_type(dev) == MIGR_GEN_MIGR)
7875 return true;
7876
7877 return false;
7878 }
7879
7880 static int is_rebuilding(struct imsm_dev *dev)
7881 {
7882 struct imsm_map *migr_map;
7883
7884 if (!dev->vol.migr_state)
7885 return 0;
7886
7887 if (migr_type(dev) != MIGR_REBUILD)
7888 return 0;
7889
7890 migr_map = get_imsm_map(dev, MAP_1);
7891
7892 if (migr_map->map_state == IMSM_T_STATE_DEGRADED)
7893 return 1;
7894 else
7895 return 0;
7896 }
7897
7898 static int is_initializing(struct imsm_dev *dev)
7899 {
7900 struct imsm_map *migr_map;
7901
7902 if (!dev->vol.migr_state)
7903 return 0;
7904
7905 if (migr_type(dev) != MIGR_INIT)
7906 return 0;
7907
7908 migr_map = get_imsm_map(dev, MAP_1);
7909
7910 if (migr_map->map_state == IMSM_T_STATE_UNINITIALIZED)
7911 return 1;
7912
7913 return 0;
7914 }
7915
7916 static void update_recovery_start(struct intel_super *super,
7917 struct imsm_dev *dev,
7918 struct mdinfo *array)
7919 {
7920 struct mdinfo *rebuild = NULL;
7921 struct mdinfo *d;
7922 __u32 units;
7923
7924 if (!is_rebuilding(dev))
7925 return;
7926
7927 /* Find the rebuild target, but punt on the dual rebuild case */
7928 for (d = array->devs; d; d = d->next)
7929 if (d->recovery_start == 0) {
7930 if (rebuild)
7931 return;
7932 rebuild = d;
7933 }
7934
7935 if (!rebuild) {
7936 /* (?) none of the disks are marked with
7937 * IMSM_ORD_REBUILD, so assume they are missing and the
7938 * disk_ord_tbl was not correctly updated
7939 */
7940 dprintf("failed to locate out-of-sync disk\n");
7941 return;
7942 }
7943
7944 units = vol_curr_migr_unit(dev);
7945 rebuild->recovery_start = units * blocks_per_migr_unit(super, dev);
7946 }
7947
7948 static int recover_backup_imsm(struct supertype *st, struct mdinfo *info);
7949
7950 static struct mdinfo *container_content_imsm(struct supertype *st, char *subarray)
7951 {
7952 /* Given a container loaded by load_super_imsm_all,
7953 * extract information about all the arrays into
7954 * an mdinfo tree.
7955 * If 'subarray' is given, just extract info about that array.
7956 *
7957 * For each imsm_dev create an mdinfo, fill it in,
7958 * then look for matching devices in super->disks
7959 * and create appropriate device mdinfo.
7960 */
7961 struct intel_super *super = st->sb;
7962 struct imsm_super *mpb = super->anchor;
7963 struct mdinfo *rest = NULL;
7964 unsigned int i;
7965 int sb_errors = 0;
7966 struct dl *d;
7967 int spare_disks = 0;
7968 int current_vol = super->current_vol;
7969
7970 /* do not assemble arrays when not all attributes are supported */
7971 if (imsm_check_attributes(mpb->attributes) == 0) {
7972 sb_errors = 1;
7973 pr_err("Unsupported attributes in IMSM metadata.Arrays activation is blocked.\n");
7974 }
7975
7976 /* count spare devices, not used in maps
7977 */
7978 for (d = super->disks; d; d = d->next)
7979 if (d->index == -1)
7980 spare_disks++;
7981
7982 for (i = 0; i < mpb->num_raid_devs; i++) {
7983 struct imsm_dev *dev;
7984 struct imsm_map *map;
7985 struct imsm_map *map2;
7986 struct mdinfo *this;
7987 int slot;
7988 int chunk;
7989 char *ep;
7990 int level;
7991
7992 if (subarray &&
7993 (i != strtoul(subarray, &ep, 10) || *ep != '\0'))
7994 continue;
7995
7996 dev = get_imsm_dev(super, i);
7997 map = get_imsm_map(dev, MAP_0);
7998 map2 = get_imsm_map(dev, MAP_1);
7999 level = get_imsm_raid_level(map);
8000
8001 /* do not publish arrays that are in the middle of an
8002 * unsupported migration
8003 */
8004 if (dev->vol.migr_state &&
8005 (migr_type(dev) == MIGR_STATE_CHANGE)) {
8006 pr_err("cannot assemble volume '%.16s': unsupported migration in progress\n",
8007 dev->volume);
8008 continue;
8009 }
8010 /* do not publish arrays that are not support by controller's
8011 * OROM/EFI
8012 */
8013
8014 this = xmalloc(sizeof(*this));
8015
8016 super->current_vol = i;
8017 getinfo_super_imsm_volume(st, this, NULL);
8018 this->next = rest;
8019 chunk = __le16_to_cpu(map->blocks_per_strip) >> 1;
8020 /* mdadm does not support all metadata features- set the bit in all arrays state */
8021 if (!validate_geometry_imsm_orom(super,
8022 level, /* RAID level */
8023 imsm_level_to_layout(level),
8024 map->num_members, /* raid disks */
8025 &chunk, imsm_dev_size(dev),
8026 1 /* verbose */)) {
8027 pr_err("IMSM RAID geometry validation failed. Array %s activation is blocked.\n",
8028 dev->volume);
8029 this->array.state |=
8030 (1<<MD_SB_BLOCK_CONTAINER_RESHAPE) |
8031 (1<<MD_SB_BLOCK_VOLUME);
8032 }
8033
8034 /* if array has bad blocks, set suitable bit in all arrays state */
8035 if (sb_errors)
8036 this->array.state |=
8037 (1<<MD_SB_BLOCK_CONTAINER_RESHAPE) |
8038 (1<<MD_SB_BLOCK_VOLUME);
8039
8040 for (slot = 0 ; slot < map->num_members; slot++) {
8041 unsigned long long recovery_start;
8042 struct mdinfo *info_d;
8043 struct dl *d;
8044 int idx;
8045 int skip;
8046 __u32 ord;
8047 int missing = 0;
8048
8049 skip = 0;
8050 idx = get_imsm_disk_idx(dev, slot, MAP_0);
8051 ord = get_imsm_ord_tbl_ent(dev, slot, MAP_X);
8052 for (d = super->disks; d ; d = d->next)
8053 if (d->index == idx)
8054 break;
8055
8056 recovery_start = MaxSector;
8057 if (d == NULL)
8058 skip = 1;
8059 if (d && is_failed(&d->disk))
8060 skip = 1;
8061 if (!skip && (ord & IMSM_ORD_REBUILD))
8062 recovery_start = 0;
8063 if (!(ord & IMSM_ORD_REBUILD))
8064 this->array.working_disks++;
8065 /*
8066 * if we skip some disks the array will be assmebled degraded;
8067 * reset resync start to avoid a dirty-degraded
8068 * situation when performing the intial sync
8069 */
8070 if (skip)
8071 missing++;
8072
8073 if (!(dev->vol.dirty & RAIDVOL_DIRTY)) {
8074 if ((!able_to_resync(level, missing) ||
8075 recovery_start == 0))
8076 this->resync_start = MaxSector;
8077 }
8078
8079 if (skip)
8080 continue;
8081
8082 info_d = xcalloc(1, sizeof(*info_d));
8083 info_d->next = this->devs;
8084 this->devs = info_d;
8085
8086 info_d->disk.number = d->index;
8087 info_d->disk.major = d->major;
8088 info_d->disk.minor = d->minor;
8089 info_d->disk.raid_disk = slot;
8090 info_d->recovery_start = recovery_start;
8091 if (map2) {
8092 if (slot < map2->num_members)
8093 info_d->disk.state = (1 << MD_DISK_ACTIVE);
8094 else
8095 this->array.spare_disks++;
8096 } else {
8097 if (slot < map->num_members)
8098 info_d->disk.state = (1 << MD_DISK_ACTIVE);
8099 else
8100 this->array.spare_disks++;
8101 }
8102
8103 info_d->events = __le32_to_cpu(mpb->generation_num);
8104 info_d->data_offset = pba_of_lba0(map);
8105 info_d->component_size = calc_component_size(map, dev);
8106
8107 if (map->raid_level == 5) {
8108 info_d->ppl_sector = this->ppl_sector;
8109 info_d->ppl_size = this->ppl_size;
8110 if (this->consistency_policy == CONSISTENCY_POLICY_PPL &&
8111 recovery_start == 0)
8112 this->resync_start = 0;
8113 }
8114
8115 info_d->bb.supported = 1;
8116 get_volume_badblocks(super->bbm_log, ord_to_idx(ord),
8117 info_d->data_offset,
8118 info_d->component_size,
8119 &info_d->bb);
8120 }
8121 /* now that the disk list is up-to-date fixup recovery_start */
8122 update_recovery_start(super, dev, this);
8123 this->array.spare_disks += spare_disks;
8124
8125 /* check for reshape */
8126 if (this->reshape_active == 1)
8127 recover_backup_imsm(st, this);
8128 rest = this;
8129 }
8130
8131 super->current_vol = current_vol;
8132 return rest;
8133 }
8134
8135 static __u8 imsm_check_degraded(struct intel_super *super, struct imsm_dev *dev,
8136 int failed, int look_in_map)
8137 {
8138 struct imsm_map *map;
8139
8140 map = get_imsm_map(dev, look_in_map);
8141
8142 if (!failed)
8143 return map->map_state == IMSM_T_STATE_UNINITIALIZED ?
8144 IMSM_T_STATE_UNINITIALIZED : IMSM_T_STATE_NORMAL;
8145
8146 switch (get_imsm_raid_level(map)) {
8147 case 0:
8148 return IMSM_T_STATE_FAILED;
8149 break;
8150 case 1:
8151 if (failed < map->num_members)
8152 return IMSM_T_STATE_DEGRADED;
8153 else
8154 return IMSM_T_STATE_FAILED;
8155 break;
8156 case 10:
8157 {
8158 /**
8159 * check to see if any mirrors have failed, otherwise we
8160 * are degraded. Even numbered slots are mirrored on
8161 * slot+1
8162 */
8163 int i;
8164 /* gcc -Os complains that this is unused */
8165 int insync = insync;
8166
8167 for (i = 0; i < map->num_members; i++) {
8168 __u32 ord = get_imsm_ord_tbl_ent(dev, i, MAP_X);
8169 int idx = ord_to_idx(ord);
8170 struct imsm_disk *disk;
8171
8172 /* reset the potential in-sync count on even-numbered
8173 * slots. num_copies is always 2 for imsm raid10
8174 */
8175 if ((i & 1) == 0)
8176 insync = 2;
8177
8178 disk = get_imsm_disk(super, idx);
8179 if (!disk || is_failed(disk) || ord & IMSM_ORD_REBUILD)
8180 insync--;
8181
8182 /* no in-sync disks left in this mirror the
8183 * array has failed
8184 */
8185 if (insync == 0)
8186 return IMSM_T_STATE_FAILED;
8187 }
8188
8189 return IMSM_T_STATE_DEGRADED;
8190 }
8191 case 5:
8192 if (failed < 2)
8193 return IMSM_T_STATE_DEGRADED;
8194 else
8195 return IMSM_T_STATE_FAILED;
8196 break;
8197 default:
8198 break;
8199 }
8200
8201 return map->map_state;
8202 }
8203
8204 static int imsm_count_failed(struct intel_super *super, struct imsm_dev *dev,
8205 int look_in_map)
8206 {
8207 int i;
8208 int failed = 0;
8209 struct imsm_disk *disk;
8210 struct imsm_map *map = get_imsm_map(dev, MAP_0);
8211 struct imsm_map *prev = get_imsm_map(dev, MAP_1);
8212 struct imsm_map *map_for_loop;
8213 __u32 ord;
8214 int idx;
8215 int idx_1;
8216
8217 /* at the beginning of migration we set IMSM_ORD_REBUILD on
8218 * disks that are being rebuilt. New failures are recorded to
8219 * map[0]. So we look through all the disks we started with and
8220 * see if any failures are still present, or if any new ones
8221 * have arrived
8222 */
8223 map_for_loop = map;
8224 if (prev && (map->num_members < prev->num_members))
8225 map_for_loop = prev;
8226
8227 for (i = 0; i < map_for_loop->num_members; i++) {
8228 idx_1 = -255;
8229 /* when MAP_X is passed both maps failures are counted
8230 */
8231 if (prev &&
8232 (look_in_map == MAP_1 || look_in_map == MAP_X) &&
8233 i < prev->num_members) {
8234 ord = __le32_to_cpu(prev->disk_ord_tbl[i]);
8235 idx_1 = ord_to_idx(ord);
8236
8237 disk = get_imsm_disk(super, idx_1);
8238 if (!disk || is_failed(disk) || ord & IMSM_ORD_REBUILD)
8239 failed++;
8240 }
8241 if ((look_in_map == MAP_0 || look_in_map == MAP_X) &&
8242 i < map->num_members) {
8243 ord = __le32_to_cpu(map->disk_ord_tbl[i]);
8244 idx = ord_to_idx(ord);
8245
8246 if (idx != idx_1) {
8247 disk = get_imsm_disk(super, idx);
8248 if (!disk || is_failed(disk) ||
8249 ord & IMSM_ORD_REBUILD)
8250 failed++;
8251 }
8252 }
8253 }
8254
8255 return failed;
8256 }
8257
8258 static int imsm_open_new(struct supertype *c, struct active_array *a,
8259 int inst)
8260 {
8261 struct intel_super *super = c->sb;
8262 struct imsm_super *mpb = super->anchor;
8263 struct imsm_update_prealloc_bb_mem u;
8264
8265 if (inst >= mpb->num_raid_devs) {
8266 pr_err("subarry index %d, out of range\n", inst);
8267 return -ENODEV;
8268 }
8269
8270 dprintf("imsm: open_new %d\n", inst);
8271 a->info.container_member = inst;
8272
8273 u.type = update_prealloc_badblocks_mem;
8274 imsm_update_metadata_locally(c, &u, sizeof(u));
8275
8276 return 0;
8277 }
8278
8279 static int is_resyncing(struct imsm_dev *dev)
8280 {
8281 struct imsm_map *migr_map;
8282
8283 if (!dev->vol.migr_state)
8284 return 0;
8285
8286 if (migr_type(dev) == MIGR_INIT ||
8287 migr_type(dev) == MIGR_REPAIR)
8288 return 1;
8289
8290 if (migr_type(dev) == MIGR_GEN_MIGR)
8291 return 0;
8292
8293 migr_map = get_imsm_map(dev, MAP_1);
8294
8295 if (migr_map->map_state == IMSM_T_STATE_NORMAL &&
8296 dev->vol.migr_type != MIGR_GEN_MIGR)
8297 return 1;
8298 else
8299 return 0;
8300 }
8301
8302 /* return true if we recorded new information */
8303 static int mark_failure(struct intel_super *super,
8304 struct imsm_dev *dev, struct imsm_disk *disk, int idx)
8305 {
8306 __u32 ord;
8307 int slot;
8308 struct imsm_map *map;
8309 char buf[MAX_RAID_SERIAL_LEN+3];
8310 unsigned int len, shift = 0;
8311
8312 /* new failures are always set in map[0] */
8313 map = get_imsm_map(dev, MAP_0);
8314
8315 slot = get_imsm_disk_slot(map, idx);
8316 if (slot < 0)
8317 return 0;
8318
8319 ord = __le32_to_cpu(map->disk_ord_tbl[slot]);
8320 if (is_failed(disk) && (ord & IMSM_ORD_REBUILD))
8321 return 0;
8322
8323 memcpy(buf, disk->serial, MAX_RAID_SERIAL_LEN);
8324 buf[MAX_RAID_SERIAL_LEN] = '\000';
8325 strcat(buf, ":0");
8326 if ((len = strlen(buf)) >= MAX_RAID_SERIAL_LEN)
8327 shift = len - MAX_RAID_SERIAL_LEN + 1;
8328 memcpy(disk->serial, &buf[shift], len + 1 - shift);
8329
8330 disk->status |= FAILED_DISK;
8331 set_imsm_ord_tbl_ent(map, slot, idx | IMSM_ORD_REBUILD);
8332 /* mark failures in second map if second map exists and this disk
8333 * in this slot.
8334 * This is valid for migration, initialization and rebuild
8335 */
8336 if (dev->vol.migr_state) {
8337 struct imsm_map *map2 = get_imsm_map(dev, MAP_1);
8338 int slot2 = get_imsm_disk_slot(map2, idx);
8339
8340 if (slot2 < map2->num_members && slot2 >= 0)
8341 set_imsm_ord_tbl_ent(map2, slot2,
8342 idx | IMSM_ORD_REBUILD);
8343 }
8344 if (map->failed_disk_num == 0xff ||
8345 (!is_rebuilding(dev) && map->failed_disk_num > slot))
8346 map->failed_disk_num = slot;
8347
8348 clear_disk_badblocks(super->bbm_log, ord_to_idx(ord));
8349
8350 return 1;
8351 }
8352
8353 static void mark_missing(struct intel_super *super,
8354 struct imsm_dev *dev, struct imsm_disk *disk, int idx)
8355 {
8356 mark_failure(super, dev, disk, idx);
8357
8358 if (disk->scsi_id == __cpu_to_le32(~(__u32)0))
8359 return;
8360
8361 disk->scsi_id = __cpu_to_le32(~(__u32)0);
8362 memmove(&disk->serial[0], &disk->serial[1], MAX_RAID_SERIAL_LEN - 1);
8363 }
8364
8365 static void handle_missing(struct intel_super *super, struct imsm_dev *dev)
8366 {
8367 struct dl *dl;
8368
8369 if (!super->missing)
8370 return;
8371
8372 /* When orom adds replacement for missing disk it does
8373 * not remove entry of missing disk, but just updates map with
8374 * new added disk. So it is not enough just to test if there is
8375 * any missing disk, we have to look if there are any failed disks
8376 * in map to stop migration */
8377
8378 dprintf("imsm: mark missing\n");
8379 /* end process for initialization and rebuild only
8380 */
8381 if (is_gen_migration(dev) == false) {
8382 int failed = imsm_count_failed(super, dev, MAP_0);
8383
8384 if (failed) {
8385 __u8 map_state;
8386 struct imsm_map *map = get_imsm_map(dev, MAP_0);
8387 struct imsm_map *map1;
8388 int i, ord, ord_map1;
8389 int rebuilt = 1;
8390
8391 for (i = 0; i < map->num_members; i++) {
8392 ord = get_imsm_ord_tbl_ent(dev, i, MAP_0);
8393 if (!(ord & IMSM_ORD_REBUILD))
8394 continue;
8395
8396 map1 = get_imsm_map(dev, MAP_1);
8397 if (!map1)
8398 continue;
8399
8400 ord_map1 = __le32_to_cpu(map1->disk_ord_tbl[i]);
8401 if (ord_map1 & IMSM_ORD_REBUILD)
8402 rebuilt = 0;
8403 }
8404
8405 if (rebuilt) {
8406 map_state = imsm_check_degraded(super, dev,
8407 failed, MAP_0);
8408 end_migration(dev, super, map_state);
8409 }
8410 }
8411 }
8412 for (dl = super->missing; dl; dl = dl->next)
8413 mark_missing(super, dev, &dl->disk, dl->index);
8414 super->updates_pending++;
8415 }
8416
8417 static unsigned long long imsm_set_array_size(struct imsm_dev *dev,
8418 long long new_size)
8419 {
8420 unsigned long long array_blocks;
8421 struct imsm_map *map = get_imsm_map(dev, MAP_0);
8422 int used_disks = imsm_num_data_members(map);
8423
8424 if (used_disks == 0) {
8425 /* when problems occures
8426 * return current array_blocks value
8427 */
8428 array_blocks = imsm_dev_size(dev);
8429
8430 return array_blocks;
8431 }
8432
8433 /* set array size in metadata
8434 */
8435 if (new_size <= 0)
8436 /* OLCE size change is caused by added disks
8437 */
8438 array_blocks = per_dev_array_size(map) * used_disks;
8439 else
8440 /* Online Volume Size Change
8441 * Using available free space
8442 */
8443 array_blocks = new_size;
8444
8445 array_blocks = round_size_to_mb(array_blocks, used_disks);
8446 set_imsm_dev_size(dev, array_blocks);
8447
8448 return array_blocks;
8449 }
8450
8451 static void imsm_set_disk(struct active_array *a, int n, int state);
8452
8453 static void imsm_progress_container_reshape(struct intel_super *super)
8454 {
8455 /* if no device has a migr_state, but some device has a
8456 * different number of members than the previous device, start
8457 * changing the number of devices in this device to match
8458 * previous.
8459 */
8460 struct imsm_super *mpb = super->anchor;
8461 int prev_disks = -1;
8462 int i;
8463 int copy_map_size;
8464
8465 for (i = 0; i < mpb->num_raid_devs; i++) {
8466 struct imsm_dev *dev = get_imsm_dev(super, i);
8467 struct imsm_map *map = get_imsm_map(dev, MAP_0);
8468 struct imsm_map *map2;
8469 int prev_num_members;
8470
8471 if (dev->vol.migr_state)
8472 return;
8473
8474 if (prev_disks == -1)
8475 prev_disks = map->num_members;
8476 if (prev_disks == map->num_members)
8477 continue;
8478
8479 /* OK, this array needs to enter reshape mode.
8480 * i.e it needs a migr_state
8481 */
8482
8483 copy_map_size = sizeof_imsm_map(map);
8484 prev_num_members = map->num_members;
8485 map->num_members = prev_disks;
8486 dev->vol.migr_state = 1;
8487 set_vol_curr_migr_unit(dev, 0);
8488 set_migr_type(dev, MIGR_GEN_MIGR);
8489 for (i = prev_num_members;
8490 i < map->num_members; i++)
8491 set_imsm_ord_tbl_ent(map, i, i);
8492 map2 = get_imsm_map(dev, MAP_1);
8493 /* Copy the current map */
8494 memcpy(map2, map, copy_map_size);
8495 map2->num_members = prev_num_members;
8496
8497 imsm_set_array_size(dev, -1);
8498 super->clean_migration_record_by_mdmon = 1;
8499 super->updates_pending++;
8500 }
8501 }
8502
8503 /* Handle dirty -> clean transititions, resync and reshape. Degraded and rebuild
8504 * states are handled in imsm_set_disk() with one exception, when a
8505 * resync is stopped due to a new failure this routine will set the
8506 * 'degraded' state for the array.
8507 */
8508 static int imsm_set_array_state(struct active_array *a, int consistent)
8509 {
8510 int inst = a->info.container_member;
8511 struct intel_super *super = a->container->sb;
8512 struct imsm_dev *dev = get_imsm_dev(super, inst);
8513 struct imsm_map *map = get_imsm_map(dev, MAP_0);
8514 int failed = imsm_count_failed(super, dev, MAP_0);
8515 __u8 map_state = imsm_check_degraded(super, dev, failed, MAP_0);
8516 __u32 blocks_per_unit;
8517
8518 if (dev->vol.migr_state &&
8519 dev->vol.migr_type == MIGR_GEN_MIGR) {
8520 /* array state change is blocked due to reshape action
8521 * We might need to
8522 * - abort the reshape (if last_checkpoint is 0 and action!= reshape)
8523 * - finish the reshape (if last_checkpoint is big and action != reshape)
8524 * - update vol_curr_migr_unit
8525 */
8526 if (a->curr_action == reshape) {
8527 /* still reshaping, maybe update vol_curr_migr_unit */
8528 goto mark_checkpoint;
8529 } else {
8530 if (a->last_checkpoint == 0 && a->prev_action == reshape) {
8531 /* for some reason we aborted the reshape.
8532 *
8533 * disable automatic metadata rollback
8534 * user action is required to recover process
8535 */
8536 if (0) {
8537 struct imsm_map *map2 =
8538 get_imsm_map(dev, MAP_1);
8539 dev->vol.migr_state = 0;
8540 set_migr_type(dev, 0);
8541 set_vol_curr_migr_unit(dev, 0);
8542 memcpy(map, map2,
8543 sizeof_imsm_map(map2));
8544 super->updates_pending++;
8545 }
8546 }
8547 if (a->last_checkpoint >= a->info.component_size) {
8548 unsigned long long array_blocks;
8549 int used_disks;
8550 struct mdinfo *mdi;
8551
8552 used_disks = imsm_num_data_members(map);
8553 if (used_disks > 0) {
8554 array_blocks =
8555 per_dev_array_size(map) *
8556 used_disks;
8557 array_blocks =
8558 round_size_to_mb(array_blocks,
8559 used_disks);
8560 a->info.custom_array_size = array_blocks;
8561 /* encourage manager to update array
8562 * size
8563 */
8564
8565 a->check_reshape = 1;
8566 }
8567 /* finalize online capacity expansion/reshape */
8568 for (mdi = a->info.devs; mdi; mdi = mdi->next)
8569 imsm_set_disk(a,
8570 mdi->disk.raid_disk,
8571 mdi->curr_state);
8572
8573 imsm_progress_container_reshape(super);
8574 }
8575 }
8576 }
8577
8578 /* before we activate this array handle any missing disks */
8579 if (consistent == 2)
8580 handle_missing(super, dev);
8581
8582 if (consistent == 2 &&
8583 (!is_resync_complete(&a->info) ||
8584 map_state != IMSM_T_STATE_NORMAL ||
8585 dev->vol.migr_state))
8586 consistent = 0;
8587
8588 if (is_resync_complete(&a->info)) {
8589 /* complete intialization / resync,
8590 * recovery and interrupted recovery is completed in
8591 * ->set_disk
8592 */
8593 if (is_resyncing(dev)) {
8594 dprintf("imsm: mark resync done\n");
8595 end_migration(dev, super, map_state);
8596 super->updates_pending++;
8597 a->last_checkpoint = 0;
8598 }
8599 } else if ((!is_resyncing(dev) && !failed) &&
8600 (imsm_reshape_blocks_arrays_changes(super) == 0)) {
8601 /* mark the start of the init process if nothing is failed */
8602 dprintf("imsm: mark resync start\n");
8603 if (map->map_state == IMSM_T_STATE_UNINITIALIZED)
8604 migrate(dev, super, IMSM_T_STATE_NORMAL, MIGR_INIT);
8605 else
8606 migrate(dev, super, IMSM_T_STATE_NORMAL, MIGR_REPAIR);
8607 super->updates_pending++;
8608 }
8609
8610 mark_checkpoint:
8611 /* skip checkpointing for general migration,
8612 * it is controlled in mdadm
8613 */
8614 if (is_gen_migration(dev))
8615 goto skip_mark_checkpoint;
8616
8617 /* check if we can update vol_curr_migr_unit from resync_start,
8618 * recovery_start
8619 */
8620 blocks_per_unit = blocks_per_migr_unit(super, dev);
8621 if (blocks_per_unit) {
8622 set_vol_curr_migr_unit(dev,
8623 a->last_checkpoint / blocks_per_unit);
8624 dprintf("imsm: mark checkpoint (%llu)\n",
8625 vol_curr_migr_unit(dev));
8626 super->updates_pending++;
8627 }
8628
8629 skip_mark_checkpoint:
8630 /* mark dirty / clean */
8631 if (((dev->vol.dirty & RAIDVOL_DIRTY) && consistent) ||
8632 (!(dev->vol.dirty & RAIDVOL_DIRTY) && !consistent)) {
8633 dprintf("imsm: mark '%s'\n", consistent ? "clean" : "dirty");
8634 if (consistent) {
8635 dev->vol.dirty = RAIDVOL_CLEAN;
8636 } else {
8637 dev->vol.dirty = RAIDVOL_DIRTY;
8638 if (dev->rwh_policy == RWH_DISTRIBUTED ||
8639 dev->rwh_policy == RWH_MULTIPLE_DISTRIBUTED)
8640 dev->vol.dirty |= RAIDVOL_DSRECORD_VALID;
8641 }
8642 super->updates_pending++;
8643 }
8644
8645 return consistent;
8646 }
8647
8648 static int imsm_disk_slot_to_ord(struct active_array *a, int slot)
8649 {
8650 int inst = a->info.container_member;
8651 struct intel_super *super = a->container->sb;
8652 struct imsm_dev *dev = get_imsm_dev(super, inst);
8653 struct imsm_map *map = get_imsm_map(dev, MAP_0);
8654
8655 if (slot > map->num_members) {
8656 pr_err("imsm: imsm_disk_slot_to_ord %d out of range 0..%d\n",
8657 slot, map->num_members - 1);
8658 return -1;
8659 }
8660
8661 if (slot < 0)
8662 return -1;
8663
8664 return get_imsm_ord_tbl_ent(dev, slot, MAP_0);
8665 }
8666
8667 static void imsm_set_disk(struct active_array *a, int n, int state)
8668 {
8669 int inst = a->info.container_member;
8670 struct intel_super *super = a->container->sb;
8671 struct imsm_dev *dev = get_imsm_dev(super, inst);
8672 struct imsm_map *map = get_imsm_map(dev, MAP_0);
8673 struct imsm_disk *disk;
8674 struct mdinfo *mdi;
8675 int recovery_not_finished = 0;
8676 int failed;
8677 int ord;
8678 __u8 map_state;
8679 int rebuild_done = 0;
8680 int i;
8681
8682 ord = get_imsm_ord_tbl_ent(dev, n, MAP_X);
8683 if (ord < 0)
8684 return;
8685
8686 dprintf("imsm: set_disk %d:%x\n", n, state);
8687 disk = get_imsm_disk(super, ord_to_idx(ord));
8688
8689 /* check for new failures */
8690 if (disk && (state & DS_FAULTY)) {
8691 if (mark_failure(super, dev, disk, ord_to_idx(ord)))
8692 super->updates_pending++;
8693 }
8694
8695 /* check if in_sync */
8696 if (state & DS_INSYNC && ord & IMSM_ORD_REBUILD && is_rebuilding(dev)) {
8697 struct imsm_map *migr_map = get_imsm_map(dev, MAP_1);
8698
8699 set_imsm_ord_tbl_ent(migr_map, n, ord_to_idx(ord));
8700 rebuild_done = 1;
8701 super->updates_pending++;
8702 }
8703
8704 failed = imsm_count_failed(super, dev, MAP_0);
8705 map_state = imsm_check_degraded(super, dev, failed, MAP_0);
8706
8707 /* check if recovery complete, newly degraded, or failed */
8708 dprintf("imsm: Detected transition to state ");
8709 switch (map_state) {
8710 case IMSM_T_STATE_NORMAL: /* transition to normal state */
8711 dprintf("normal: ");
8712 if (is_rebuilding(dev)) {
8713 dprintf_cont("while rebuilding");
8714 /* check if recovery is really finished */
8715 for (mdi = a->info.devs; mdi ; mdi = mdi->next)
8716 if (mdi->recovery_start != MaxSector) {
8717 recovery_not_finished = 1;
8718 break;
8719 }
8720 if (recovery_not_finished) {
8721 dprintf_cont("\n");
8722 dprintf("Rebuild has not finished yet, state not changed");
8723 if (a->last_checkpoint < mdi->recovery_start) {
8724 a->last_checkpoint = mdi->recovery_start;
8725 super->updates_pending++;
8726 }
8727 break;
8728 }
8729 end_migration(dev, super, map_state);
8730 map->failed_disk_num = ~0;
8731 super->updates_pending++;
8732 a->last_checkpoint = 0;
8733 break;
8734 }
8735 if (is_gen_migration(dev)) {
8736 dprintf_cont("while general migration");
8737 if (a->last_checkpoint >= a->info.component_size)
8738 end_migration(dev, super, map_state);
8739 else
8740 map->map_state = map_state;
8741 map->failed_disk_num = ~0;
8742 super->updates_pending++;
8743 break;
8744 }
8745 break;
8746 case IMSM_T_STATE_DEGRADED: /* transition to degraded state */
8747 dprintf_cont("degraded: ");
8748 if (map->map_state != map_state && !dev->vol.migr_state) {
8749 dprintf_cont("mark degraded");
8750 map->map_state = map_state;
8751 super->updates_pending++;
8752 a->last_checkpoint = 0;
8753 break;
8754 }
8755 if (is_rebuilding(dev)) {
8756 dprintf_cont("while rebuilding ");
8757 if (state & DS_FAULTY) {
8758 dprintf_cont("removing failed drive ");
8759 if (n == map->failed_disk_num) {
8760 dprintf_cont("end migration");
8761 end_migration(dev, super, map_state);
8762 a->last_checkpoint = 0;
8763 } else {
8764 dprintf_cont("fail detected during rebuild, changing map state");
8765 map->map_state = map_state;
8766 }
8767 super->updates_pending++;
8768 }
8769
8770 if (!rebuild_done)
8771 break;
8772
8773 /* check if recovery is really finished */
8774 for (mdi = a->info.devs; mdi ; mdi = mdi->next)
8775 if (mdi->recovery_start != MaxSector) {
8776 recovery_not_finished = 1;
8777 break;
8778 }
8779 if (recovery_not_finished) {
8780 dprintf_cont("\n");
8781 dprintf_cont("Rebuild has not finished yet");
8782 if (a->last_checkpoint < mdi->recovery_start) {
8783 a->last_checkpoint =
8784 mdi->recovery_start;
8785 super->updates_pending++;
8786 }
8787 break;
8788 }
8789
8790 dprintf_cont(" Rebuild done, still degraded");
8791 end_migration(dev, super, map_state);
8792 a->last_checkpoint = 0;
8793 super->updates_pending++;
8794
8795 for (i = 0; i < map->num_members; i++) {
8796 int idx = get_imsm_ord_tbl_ent(dev, i, MAP_0);
8797
8798 if (idx & IMSM_ORD_REBUILD)
8799 map->failed_disk_num = i;
8800 }
8801 super->updates_pending++;
8802 break;
8803 }
8804 if (is_gen_migration(dev)) {
8805 dprintf_cont("while general migration");
8806 if (a->last_checkpoint >= a->info.component_size)
8807 end_migration(dev, super, map_state);
8808 else {
8809 map->map_state = map_state;
8810 manage_second_map(super, dev);
8811 }
8812 super->updates_pending++;
8813 break;
8814 }
8815 if (is_initializing(dev)) {
8816 dprintf_cont("while initialization.");
8817 map->map_state = map_state;
8818 super->updates_pending++;
8819 break;
8820 }
8821 break;
8822 case IMSM_T_STATE_FAILED: /* transition to failed state */
8823 dprintf_cont("failed: ");
8824 if (is_gen_migration(dev)) {
8825 dprintf_cont("while general migration");
8826 map->map_state = map_state;
8827 super->updates_pending++;
8828 break;
8829 }
8830 if (map->map_state != map_state) {
8831 dprintf_cont("mark failed");
8832 end_migration(dev, super, map_state);
8833 super->updates_pending++;
8834 a->last_checkpoint = 0;
8835 break;
8836 }
8837 break;
8838 default:
8839 dprintf_cont("state %i\n", map_state);
8840 }
8841 dprintf_cont("\n");
8842 }
8843
8844 static int store_imsm_mpb(int fd, struct imsm_super *mpb)
8845 {
8846 void *buf = mpb;
8847 __u32 mpb_size = __le32_to_cpu(mpb->mpb_size);
8848 unsigned long long dsize;
8849 unsigned long long sectors;
8850 unsigned int sector_size;
8851
8852 if (!get_dev_sector_size(fd, NULL, &sector_size))
8853 return 1;
8854 get_dev_size(fd, NULL, &dsize);
8855
8856 if (mpb_size > sector_size) {
8857 /* -1 to account for anchor */
8858 sectors = mpb_sectors(mpb, sector_size) - 1;
8859
8860 /* write the extended mpb to the sectors preceeding the anchor */
8861 if (lseek64(fd, dsize - (sector_size * (2 + sectors)),
8862 SEEK_SET) < 0)
8863 return 1;
8864
8865 if ((unsigned long long)write(fd, buf + sector_size,
8866 sector_size * sectors) != sector_size * sectors)
8867 return 1;
8868 }
8869
8870 /* first block is stored on second to last sector of the disk */
8871 if (lseek64(fd, dsize - (sector_size * 2), SEEK_SET) < 0)
8872 return 1;
8873
8874 if ((unsigned int)write(fd, buf, sector_size) != sector_size)
8875 return 1;
8876
8877 return 0;
8878 }
8879
8880 static void imsm_sync_metadata(struct supertype *container)
8881 {
8882 struct intel_super *super = container->sb;
8883
8884 dprintf("sync metadata: %d\n", super->updates_pending);
8885 if (!super->updates_pending)
8886 return;
8887
8888 write_super_imsm(container, 0);
8889
8890 super->updates_pending = 0;
8891 }
8892
8893 static struct dl *imsm_readd(struct intel_super *super, int idx, struct active_array *a)
8894 {
8895 struct imsm_dev *dev = get_imsm_dev(super, a->info.container_member);
8896 int i = get_imsm_disk_idx(dev, idx, MAP_X);
8897 struct dl *dl;
8898
8899 for (dl = super->disks; dl; dl = dl->next)
8900 if (dl->index == i)
8901 break;
8902
8903 if (dl && is_failed(&dl->disk))
8904 dl = NULL;
8905
8906 if (dl)
8907 dprintf("found %x:%x\n", dl->major, dl->minor);
8908
8909 return dl;
8910 }
8911
8912 static struct dl *imsm_add_spare(struct intel_super *super, int slot,
8913 struct active_array *a, int activate_new,
8914 struct mdinfo *additional_test_list)
8915 {
8916 struct imsm_dev *dev = get_imsm_dev(super, a->info.container_member);
8917 int idx = get_imsm_disk_idx(dev, slot, MAP_X);
8918 struct imsm_super *mpb = super->anchor;
8919 struct imsm_map *map;
8920 unsigned long long pos;
8921 struct mdinfo *d;
8922 struct extent *ex;
8923 int i, j;
8924 int found;
8925 __u32 array_start = 0;
8926 __u32 array_end = 0;
8927 struct dl *dl;
8928 struct mdinfo *test_list;
8929
8930 for (dl = super->disks; dl; dl = dl->next) {
8931 /* If in this array, skip */
8932 for (d = a->info.devs ; d ; d = d->next)
8933 if (is_fd_valid(d->state_fd) &&
8934 d->disk.major == dl->major &&
8935 d->disk.minor == dl->minor) {
8936 dprintf("%x:%x already in array\n",
8937 dl->major, dl->minor);
8938 break;
8939 }
8940 if (d)
8941 continue;
8942 test_list = additional_test_list;
8943 while (test_list) {
8944 if (test_list->disk.major == dl->major &&
8945 test_list->disk.minor == dl->minor) {
8946 dprintf("%x:%x already in additional test list\n",
8947 dl->major, dl->minor);
8948 break;
8949 }
8950 test_list = test_list->next;
8951 }
8952 if (test_list)
8953 continue;
8954
8955 /* skip in use or failed drives */
8956 if (is_failed(&dl->disk) || idx == dl->index ||
8957 dl->index == -2) {
8958 dprintf("%x:%x status (failed: %d index: %d)\n",
8959 dl->major, dl->minor, is_failed(&dl->disk), idx);
8960 continue;
8961 }
8962
8963 /* skip pure spares when we are looking for partially
8964 * assimilated drives
8965 */
8966 if (dl->index == -1 && !activate_new)
8967 continue;
8968
8969 if (!drive_validate_sector_size(super, dl))
8970 continue;
8971
8972 /* Does this unused device have the requisite free space?
8973 * It needs to be able to cover all member volumes
8974 */
8975 ex = get_extents(super, dl, 1);
8976 if (!ex) {
8977 dprintf("cannot get extents\n");
8978 continue;
8979 }
8980 for (i = 0; i < mpb->num_raid_devs; i++) {
8981 dev = get_imsm_dev(super, i);
8982 map = get_imsm_map(dev, MAP_0);
8983
8984 /* check if this disk is already a member of
8985 * this array
8986 */
8987 if (get_imsm_disk_slot(map, dl->index) >= 0)
8988 continue;
8989
8990 found = 0;
8991 j = 0;
8992 pos = 0;
8993 array_start = pba_of_lba0(map);
8994 array_end = array_start +
8995 per_dev_array_size(map) - 1;
8996
8997 do {
8998 /* check that we can start at pba_of_lba0 with
8999 * num_data_stripes*blocks_per_stripe of space
9000 */
9001 if (array_start >= pos && array_end < ex[j].start) {
9002 found = 1;
9003 break;
9004 }
9005 pos = ex[j].start + ex[j].size;
9006 j++;
9007 } while (ex[j-1].size);
9008
9009 if (!found)
9010 break;
9011 }
9012
9013 free(ex);
9014 if (i < mpb->num_raid_devs) {
9015 dprintf("%x:%x does not have %u to %u available\n",
9016 dl->major, dl->minor, array_start, array_end);
9017 /* No room */
9018 continue;
9019 }
9020 return dl;
9021 }
9022
9023 return dl;
9024 }
9025
9026 static int imsm_rebuild_allowed(struct supertype *cont, int dev_idx, int failed)
9027 {
9028 struct imsm_dev *dev2;
9029 struct imsm_map *map;
9030 struct dl *idisk;
9031 int slot;
9032 int idx;
9033 __u8 state;
9034
9035 dev2 = get_imsm_dev(cont->sb, dev_idx);
9036 if (dev2) {
9037 state = imsm_check_degraded(cont->sb, dev2, failed, MAP_0);
9038 if (state == IMSM_T_STATE_FAILED) {
9039 map = get_imsm_map(dev2, MAP_0);
9040 if (!map)
9041 return 1;
9042 for (slot = 0; slot < map->num_members; slot++) {
9043 /*
9044 * Check if failed disks are deleted from intel
9045 * disk list or are marked to be deleted
9046 */
9047 idx = get_imsm_disk_idx(dev2, slot, MAP_X);
9048 idisk = get_imsm_dl_disk(cont->sb, idx);
9049 /*
9050 * Do not rebuild the array if failed disks
9051 * from failed sub-array are not removed from
9052 * container.
9053 */
9054 if (idisk &&
9055 is_failed(&idisk->disk) &&
9056 (idisk->action != DISK_REMOVE))
9057 return 0;
9058 }
9059 }
9060 }
9061 return 1;
9062 }
9063
9064 static struct mdinfo *imsm_activate_spare(struct active_array *a,
9065 struct metadata_update **updates)
9066 {
9067 /**
9068 * Find a device with unused free space and use it to replace a
9069 * failed/vacant region in an array. We replace failed regions one a
9070 * array at a time. The result is that a new spare disk will be added
9071 * to the first failed array and after the monitor has finished
9072 * propagating failures the remainder will be consumed.
9073 *
9074 * FIXME add a capability for mdmon to request spares from another
9075 * container.
9076 */
9077
9078 struct intel_super *super = a->container->sb;
9079 int inst = a->info.container_member;
9080 struct imsm_dev *dev = get_imsm_dev(super, inst);
9081 struct imsm_map *map = get_imsm_map(dev, MAP_0);
9082 int failed = a->info.array.raid_disks;
9083 struct mdinfo *rv = NULL;
9084 struct mdinfo *d;
9085 struct mdinfo *di;
9086 struct metadata_update *mu;
9087 struct dl *dl;
9088 struct imsm_update_activate_spare *u;
9089 int num_spares = 0;
9090 int i;
9091 int allowed;
9092
9093 for (d = a->info.devs ; d; d = d->next) {
9094 if (!is_fd_valid(d->state_fd))
9095 continue;
9096
9097 if (d->curr_state & DS_FAULTY)
9098 /* wait for Removal to happen */
9099 return NULL;
9100
9101 failed--;
9102 }
9103
9104 dprintf("imsm: activate spare: inst=%d failed=%d (%d) level=%d\n",
9105 inst, failed, a->info.array.raid_disks, a->info.array.level);
9106
9107 if (imsm_reshape_blocks_arrays_changes(super))
9108 return NULL;
9109
9110 /* Cannot activate another spare if rebuild is in progress already
9111 */
9112 if (is_rebuilding(dev)) {
9113 dprintf("imsm: No spare activation allowed. Rebuild in progress already.\n");
9114 return NULL;
9115 }
9116
9117 if (a->info.array.level == 4)
9118 /* No repair for takeovered array
9119 * imsm doesn't support raid4
9120 */
9121 return NULL;
9122
9123 if (imsm_check_degraded(super, dev, failed, MAP_0) !=
9124 IMSM_T_STATE_DEGRADED)
9125 return NULL;
9126
9127 if (get_imsm_map(dev, MAP_0)->map_state == IMSM_T_STATE_UNINITIALIZED) {
9128 dprintf("imsm: No spare activation allowed. Volume is not initialized.\n");
9129 return NULL;
9130 }
9131
9132 /*
9133 * If there are any failed disks check state of the other volume.
9134 * Block rebuild if the another one is failed until failed disks
9135 * are removed from container.
9136 */
9137 if (failed) {
9138 dprintf("found failed disks in %.*s, check if there anotherfailed sub-array.\n",
9139 MAX_RAID_SERIAL_LEN, dev->volume);
9140 /* check if states of the other volumes allow for rebuild */
9141 for (i = 0; i < super->anchor->num_raid_devs; i++) {
9142 if (i != inst) {
9143 allowed = imsm_rebuild_allowed(a->container,
9144 i, failed);
9145 if (!allowed)
9146 return NULL;
9147 }
9148 }
9149 }
9150
9151 /* For each slot, if it is not working, find a spare */
9152 for (i = 0; i < a->info.array.raid_disks; i++) {
9153 for (d = a->info.devs ; d ; d = d->next)
9154 if (d->disk.raid_disk == i)
9155 break;
9156 dprintf("found %d: %p %x\n", i, d, d?d->curr_state:0);
9157 if (d && is_fd_valid(d->state_fd))
9158 continue;
9159
9160 /*
9161 * OK, this device needs recovery. Try to re-add the
9162 * previous occupant of this slot, if this fails see if
9163 * we can continue the assimilation of a spare that was
9164 * partially assimilated, finally try to activate a new
9165 * spare.
9166 */
9167 dl = imsm_readd(super, i, a);
9168 if (!dl)
9169 dl = imsm_add_spare(super, i, a, 0, rv);
9170 if (!dl)
9171 dl = imsm_add_spare(super, i, a, 1, rv);
9172 if (!dl)
9173 continue;
9174
9175 /* found a usable disk with enough space */
9176 di = xcalloc(1, sizeof(*di));
9177
9178 /* dl->index will be -1 in the case we are activating a
9179 * pristine spare. imsm_process_update() will create a
9180 * new index in this case. Once a disk is found to be
9181 * failed in all member arrays it is kicked from the
9182 * metadata
9183 */
9184 di->disk.number = dl->index;
9185
9186 /* (ab)use di->devs to store a pointer to the device
9187 * we chose
9188 */
9189 di->devs = (struct mdinfo *) dl;
9190
9191 di->disk.raid_disk = i;
9192 di->disk.major = dl->major;
9193 di->disk.minor = dl->minor;
9194 di->disk.state = 0;
9195 di->recovery_start = 0;
9196 di->data_offset = pba_of_lba0(map);
9197 di->component_size = a->info.component_size;
9198 di->container_member = inst;
9199 di->bb.supported = 1;
9200 if (a->info.consistency_policy == CONSISTENCY_POLICY_PPL) {
9201 di->ppl_sector = get_ppl_sector(super, inst);
9202 di->ppl_size = MULTIPLE_PPL_AREA_SIZE_IMSM >> 9;
9203 }
9204 super->random = random32();
9205 di->next = rv;
9206 rv = di;
9207 num_spares++;
9208 dprintf("%x:%x to be %d at %llu\n", dl->major, dl->minor,
9209 i, di->data_offset);
9210 }
9211
9212 if (!rv)
9213 /* No spares found */
9214 return rv;
9215 /* Now 'rv' has a list of devices to return.
9216 * Create a metadata_update record to update the
9217 * disk_ord_tbl for the array
9218 */
9219 mu = xmalloc(sizeof(*mu));
9220 mu->buf = xcalloc(num_spares,
9221 sizeof(struct imsm_update_activate_spare));
9222 mu->space = NULL;
9223 mu->space_list = NULL;
9224 mu->len = sizeof(struct imsm_update_activate_spare) * num_spares;
9225 mu->next = *updates;
9226 u = (struct imsm_update_activate_spare *) mu->buf;
9227
9228 for (di = rv ; di ; di = di->next) {
9229 u->type = update_activate_spare;
9230 u->dl = (struct dl *) di->devs;
9231 di->devs = NULL;
9232 u->slot = di->disk.raid_disk;
9233 u->array = inst;
9234 u->next = u + 1;
9235 u++;
9236 }
9237 (u-1)->next = NULL;
9238 *updates = mu;
9239
9240 return rv;
9241 }
9242
9243 static int disks_overlap(struct intel_super *super, int idx, struct imsm_update_create_array *u)
9244 {
9245 struct imsm_dev *dev = get_imsm_dev(super, idx);
9246 struct imsm_map *map = get_imsm_map(dev, MAP_0);
9247 struct imsm_map *new_map = get_imsm_map(&u->dev, MAP_0);
9248 struct disk_info *inf = get_disk_info(u);
9249 struct imsm_disk *disk;
9250 int i;
9251 int j;
9252
9253 for (i = 0; i < map->num_members; i++) {
9254 disk = get_imsm_disk(super, get_imsm_disk_idx(dev, i, MAP_X));
9255 for (j = 0; j < new_map->num_members; j++)
9256 if (serialcmp(disk->serial, inf[j].serial) == 0)
9257 return 1;
9258 }
9259
9260 return 0;
9261 }
9262
9263 static struct dl *get_disk_super(struct intel_super *super, int major, int minor)
9264 {
9265 struct dl *dl;
9266
9267 for (dl = super->disks; dl; dl = dl->next)
9268 if (dl->major == major && dl->minor == minor)
9269 return dl;
9270 return NULL;
9271 }
9272
9273 static int remove_disk_super(struct intel_super *super, int major, int minor)
9274 {
9275 struct dl *prev;
9276 struct dl *dl;
9277
9278 prev = NULL;
9279 for (dl = super->disks; dl; dl = dl->next) {
9280 if (dl->major == major && dl->minor == minor) {
9281 /* remove */
9282 if (prev)
9283 prev->next = dl->next;
9284 else
9285 super->disks = dl->next;
9286 dl->next = NULL;
9287 __free_imsm_disk(dl, 1);
9288 dprintf("removed %x:%x\n", major, minor);
9289 break;
9290 }
9291 prev = dl;
9292 }
9293 return 0;
9294 }
9295
9296 static void imsm_delete(struct intel_super *super, struct dl **dlp, unsigned index);
9297
9298 static int add_remove_disk_update(struct intel_super *super)
9299 {
9300 int check_degraded = 0;
9301 struct dl *disk;
9302
9303 /* add/remove some spares to/from the metadata/contrainer */
9304 while (super->disk_mgmt_list) {
9305 struct dl *disk_cfg;
9306
9307 disk_cfg = super->disk_mgmt_list;
9308 super->disk_mgmt_list = disk_cfg->next;
9309 disk_cfg->next = NULL;
9310
9311 if (disk_cfg->action == DISK_ADD) {
9312 disk_cfg->next = super->disks;
9313 super->disks = disk_cfg;
9314 check_degraded = 1;
9315 dprintf("added %x:%x\n",
9316 disk_cfg->major, disk_cfg->minor);
9317 } else if (disk_cfg->action == DISK_REMOVE) {
9318 dprintf("Disk remove action processed: %x.%x\n",
9319 disk_cfg->major, disk_cfg->minor);
9320 disk = get_disk_super(super,
9321 disk_cfg->major,
9322 disk_cfg->minor);
9323 if (disk) {
9324 /* store action status */
9325 disk->action = DISK_REMOVE;
9326 /* remove spare disks only */
9327 if (disk->index == -1) {
9328 remove_disk_super(super,
9329 disk_cfg->major,
9330 disk_cfg->minor);
9331 } else {
9332 disk_cfg->fd = disk->fd;
9333 disk->fd = -1;
9334 }
9335 }
9336 /* release allocate disk structure */
9337 __free_imsm_disk(disk_cfg, 1);
9338 }
9339 }
9340 return check_degraded;
9341 }
9342
9343 static int apply_reshape_migration_update(struct imsm_update_reshape_migration *u,
9344 struct intel_super *super,
9345 void ***space_list)
9346 {
9347 struct intel_dev *id;
9348 void **tofree = NULL;
9349 int ret_val = 0;
9350
9351 dprintf("(enter)\n");
9352 if (u->subdev < 0 || u->subdev > 1) {
9353 dprintf("imsm: Error: Wrong subdev: %i\n", u->subdev);
9354 return ret_val;
9355 }
9356 if (space_list == NULL || *space_list == NULL) {
9357 dprintf("imsm: Error: Memory is not allocated\n");
9358 return ret_val;
9359 }
9360
9361 for (id = super->devlist ; id; id = id->next) {
9362 if (id->index == (unsigned)u->subdev) {
9363 struct imsm_dev *dev = get_imsm_dev(super, u->subdev);
9364 struct imsm_map *map;
9365 struct imsm_dev *new_dev =
9366 (struct imsm_dev *)*space_list;
9367 struct imsm_map *migr_map = get_imsm_map(dev, MAP_1);
9368 int to_state;
9369 struct dl *new_disk;
9370
9371 if (new_dev == NULL)
9372 return ret_val;
9373 *space_list = **space_list;
9374 memcpy(new_dev, dev, sizeof_imsm_dev(dev, 0));
9375 map = get_imsm_map(new_dev, MAP_0);
9376 if (migr_map) {
9377 dprintf("imsm: Error: migration in progress");
9378 return ret_val;
9379 }
9380
9381 to_state = map->map_state;
9382 if ((u->new_level == 5) && (map->raid_level == 0)) {
9383 map->num_members++;
9384 /* this should not happen */
9385 if (u->new_disks[0] < 0) {
9386 map->failed_disk_num =
9387 map->num_members - 1;
9388 to_state = IMSM_T_STATE_DEGRADED;
9389 } else
9390 to_state = IMSM_T_STATE_NORMAL;
9391 }
9392 migrate(new_dev, super, to_state, MIGR_GEN_MIGR);
9393 if (u->new_level > -1)
9394 map->raid_level = u->new_level;
9395 migr_map = get_imsm_map(new_dev, MAP_1);
9396 if ((u->new_level == 5) &&
9397 (migr_map->raid_level == 0)) {
9398 int ord = map->num_members - 1;
9399 migr_map->num_members--;
9400 if (u->new_disks[0] < 0)
9401 ord |= IMSM_ORD_REBUILD;
9402 set_imsm_ord_tbl_ent(map,
9403 map->num_members - 1,
9404 ord);
9405 }
9406 id->dev = new_dev;
9407 tofree = (void **)dev;
9408
9409 /* update chunk size
9410 */
9411 if (u->new_chunksize > 0) {
9412 struct imsm_map *dest_map =
9413 get_imsm_map(dev, MAP_0);
9414 int used_disks =
9415 imsm_num_data_members(dest_map);
9416
9417 if (used_disks == 0)
9418 return ret_val;
9419
9420 map->blocks_per_strip =
9421 __cpu_to_le16(u->new_chunksize * 2);
9422 update_num_data_stripes(map, imsm_dev_size(dev));
9423 }
9424
9425 /* ensure blocks_per_member has valid value
9426 */
9427 set_blocks_per_member(map,
9428 per_dev_array_size(map) +
9429 NUM_BLOCKS_DIRTY_STRIPE_REGION);
9430
9431 /* add disk
9432 */
9433 if (u->new_level != 5 || migr_map->raid_level != 0 ||
9434 migr_map->raid_level == map->raid_level)
9435 goto skip_disk_add;
9436
9437 if (u->new_disks[0] >= 0) {
9438 /* use passes spare
9439 */
9440 new_disk = get_disk_super(super,
9441 major(u->new_disks[0]),
9442 minor(u->new_disks[0]));
9443 dprintf("imsm: new disk for reshape is: %i:%i (%p, index = %i)\n",
9444 major(u->new_disks[0]),
9445 minor(u->new_disks[0]),
9446 new_disk, new_disk->index);
9447 if (new_disk == NULL)
9448 goto error_disk_add;
9449
9450 new_disk->index = map->num_members - 1;
9451 /* slot to fill in autolayout
9452 */
9453 new_disk->raiddisk = new_disk->index;
9454 new_disk->disk.status |= CONFIGURED_DISK;
9455 new_disk->disk.status &= ~SPARE_DISK;
9456 } else
9457 goto error_disk_add;
9458
9459 skip_disk_add:
9460 *tofree = *space_list;
9461 /* calculate new size
9462 */
9463 imsm_set_array_size(new_dev, -1);
9464
9465 ret_val = 1;
9466 }
9467 }
9468
9469 if (tofree)
9470 *space_list = tofree;
9471 return ret_val;
9472
9473 error_disk_add:
9474 dprintf("Error: imsm: Cannot find disk.\n");
9475 return ret_val;
9476 }
9477
9478 static int apply_size_change_update(struct imsm_update_size_change *u,
9479 struct intel_super *super)
9480 {
9481 struct intel_dev *id;
9482 int ret_val = 0;
9483
9484 dprintf("(enter)\n");
9485 if (u->subdev < 0 || u->subdev > 1) {
9486 dprintf("imsm: Error: Wrong subdev: %i\n", u->subdev);
9487 return ret_val;
9488 }
9489
9490 for (id = super->devlist ; id; id = id->next) {
9491 if (id->index == (unsigned)u->subdev) {
9492 struct imsm_dev *dev = get_imsm_dev(super, u->subdev);
9493 struct imsm_map *map = get_imsm_map(dev, MAP_0);
9494 int used_disks = imsm_num_data_members(map);
9495 unsigned long long blocks_per_member;
9496 unsigned long long new_size_per_disk;
9497
9498 if (used_disks == 0)
9499 return 0;
9500
9501 /* calculate new size
9502 */
9503 new_size_per_disk = u->new_size / used_disks;
9504 blocks_per_member = new_size_per_disk +
9505 NUM_BLOCKS_DIRTY_STRIPE_REGION;
9506
9507 imsm_set_array_size(dev, u->new_size);
9508 set_blocks_per_member(map, blocks_per_member);
9509 update_num_data_stripes(map, u->new_size);
9510 ret_val = 1;
9511 break;
9512 }
9513 }
9514
9515 return ret_val;
9516 }
9517
9518 static int prepare_spare_to_activate(struct supertype *st,
9519 struct imsm_update_activate_spare *u)
9520 {
9521 struct intel_super *super = st->sb;
9522 int prev_current_vol = super->current_vol;
9523 struct active_array *a;
9524 int ret = 1;
9525
9526 for (a = st->arrays; a; a = a->next)
9527 /*
9528 * Additional initialization (adding bitmap header, filling
9529 * the bitmap area with '1's to force initial rebuild for a whole
9530 * data-area) is required when adding the spare to the volume
9531 * with write-intent bitmap.
9532 */
9533 if (a->info.container_member == u->array &&
9534 a->info.consistency_policy == CONSISTENCY_POLICY_BITMAP) {
9535 struct dl *dl;
9536
9537 for (dl = super->disks; dl; dl = dl->next)
9538 if (dl == u->dl)
9539 break;
9540 if (!dl)
9541 break;
9542
9543 super->current_vol = u->array;
9544 if (st->ss->write_bitmap(st, dl->fd, NoUpdate))
9545 ret = 0;
9546 super->current_vol = prev_current_vol;
9547 }
9548 return ret;
9549 }
9550
9551 static int apply_update_activate_spare(struct imsm_update_activate_spare *u,
9552 struct intel_super *super,
9553 struct active_array *active_array)
9554 {
9555 struct imsm_super *mpb = super->anchor;
9556 struct imsm_dev *dev = get_imsm_dev(super, u->array);
9557 struct imsm_map *map = get_imsm_map(dev, MAP_0);
9558 struct imsm_map *migr_map;
9559 struct active_array *a;
9560 struct imsm_disk *disk;
9561 __u8 to_state;
9562 struct dl *dl;
9563 unsigned int found;
9564 int failed;
9565 int victim;
9566 int i;
9567 int second_map_created = 0;
9568
9569 for (; u; u = u->next) {
9570 victim = get_imsm_disk_idx(dev, u->slot, MAP_X);
9571
9572 if (victim < 0)
9573 return 0;
9574
9575 for (dl = super->disks; dl; dl = dl->next)
9576 if (dl == u->dl)
9577 break;
9578
9579 if (!dl) {
9580 pr_err("error: imsm_activate_spare passed an unknown disk (index: %d)\n",
9581 u->dl->index);
9582 return 0;
9583 }
9584
9585 /* count failures (excluding rebuilds and the victim)
9586 * to determine map[0] state
9587 */
9588 failed = 0;
9589 for (i = 0; i < map->num_members; i++) {
9590 if (i == u->slot)
9591 continue;
9592 disk = get_imsm_disk(super,
9593 get_imsm_disk_idx(dev, i, MAP_X));
9594 if (!disk || is_failed(disk))
9595 failed++;
9596 }
9597
9598 /* adding a pristine spare, assign a new index */
9599 if (dl->index < 0) {
9600 dl->index = super->anchor->num_disks;
9601 super->anchor->num_disks++;
9602 }
9603 disk = &dl->disk;
9604 disk->status |= CONFIGURED_DISK;
9605 disk->status &= ~SPARE_DISK;
9606
9607 /* mark rebuild */
9608 to_state = imsm_check_degraded(super, dev, failed, MAP_0);
9609 if (!second_map_created) {
9610 second_map_created = 1;
9611 map->map_state = IMSM_T_STATE_DEGRADED;
9612 migrate(dev, super, to_state, MIGR_REBUILD);
9613 } else
9614 map->map_state = to_state;
9615 migr_map = get_imsm_map(dev, MAP_1);
9616 set_imsm_ord_tbl_ent(map, u->slot, dl->index);
9617 set_imsm_ord_tbl_ent(migr_map, u->slot,
9618 dl->index | IMSM_ORD_REBUILD);
9619
9620 /* update the family_num to mark a new container
9621 * generation, being careful to record the existing
9622 * family_num in orig_family_num to clean up after
9623 * earlier mdadm versions that neglected to set it.
9624 */
9625 if (mpb->orig_family_num == 0)
9626 mpb->orig_family_num = mpb->family_num;
9627 mpb->family_num += super->random;
9628
9629 /* count arrays using the victim in the metadata */
9630 found = 0;
9631 for (a = active_array; a ; a = a->next) {
9632 dev = get_imsm_dev(super, a->info.container_member);
9633 map = get_imsm_map(dev, MAP_0);
9634
9635 if (get_imsm_disk_slot(map, victim) >= 0)
9636 found++;
9637 }
9638
9639 /* delete the victim if it is no longer being
9640 * utilized anywhere
9641 */
9642 if (!found) {
9643 struct dl **dlp;
9644
9645 /* We know that 'manager' isn't touching anything,
9646 * so it is safe to delete
9647 */
9648 for (dlp = &super->disks; *dlp; dlp = &(*dlp)->next)
9649 if ((*dlp)->index == victim)
9650 break;
9651
9652 /* victim may be on the missing list */
9653 if (!*dlp)
9654 for (dlp = &super->missing; *dlp;
9655 dlp = &(*dlp)->next)
9656 if ((*dlp)->index == victim)
9657 break;
9658 imsm_delete(super, dlp, victim);
9659 }
9660 }
9661
9662 return 1;
9663 }
9664
9665 static int apply_reshape_container_disks_update(struct imsm_update_reshape *u,
9666 struct intel_super *super,
9667 void ***space_list)
9668 {
9669 struct dl *new_disk;
9670 struct intel_dev *id;
9671 int i;
9672 int delta_disks = u->new_raid_disks - u->old_raid_disks;
9673 int disk_count = u->old_raid_disks;
9674 void **tofree = NULL;
9675 int devices_to_reshape = 1;
9676 struct imsm_super *mpb = super->anchor;
9677 int ret_val = 0;
9678 unsigned int dev_id;
9679
9680 dprintf("(enter)\n");
9681
9682 /* enable spares to use in array */
9683 for (i = 0; i < delta_disks; i++) {
9684 new_disk = get_disk_super(super,
9685 major(u->new_disks[i]),
9686 minor(u->new_disks[i]));
9687 dprintf("imsm: new disk for reshape is: %i:%i (%p, index = %i)\n",
9688 major(u->new_disks[i]), minor(u->new_disks[i]),
9689 new_disk, new_disk->index);
9690 if (new_disk == NULL ||
9691 (new_disk->index >= 0 &&
9692 new_disk->index < u->old_raid_disks))
9693 goto update_reshape_exit;
9694 new_disk->index = disk_count++;
9695 /* slot to fill in autolayout
9696 */
9697 new_disk->raiddisk = new_disk->index;
9698 new_disk->disk.status |=
9699 CONFIGURED_DISK;
9700 new_disk->disk.status &= ~SPARE_DISK;
9701 }
9702
9703 dprintf("imsm: volume set mpb->num_raid_devs = %i\n",
9704 mpb->num_raid_devs);
9705 /* manage changes in volume
9706 */
9707 for (dev_id = 0; dev_id < mpb->num_raid_devs; dev_id++) {
9708 void **sp = *space_list;
9709 struct imsm_dev *newdev;
9710 struct imsm_map *newmap, *oldmap;
9711
9712 for (id = super->devlist ; id; id = id->next) {
9713 if (id->index == dev_id)
9714 break;
9715 }
9716 if (id == NULL)
9717 break;
9718 if (!sp)
9719 continue;
9720 *space_list = *sp;
9721 newdev = (void*)sp;
9722 /* Copy the dev, but not (all of) the map */
9723 memcpy(newdev, id->dev, sizeof(*newdev));
9724 oldmap = get_imsm_map(id->dev, MAP_0);
9725 newmap = get_imsm_map(newdev, MAP_0);
9726 /* Copy the current map */
9727 memcpy(newmap, oldmap, sizeof_imsm_map(oldmap));
9728 /* update one device only
9729 */
9730 if (devices_to_reshape) {
9731 dprintf("imsm: modifying subdev: %i\n",
9732 id->index);
9733 devices_to_reshape--;
9734 newdev->vol.migr_state = 1;
9735 set_vol_curr_migr_unit(newdev, 0);
9736 set_migr_type(newdev, MIGR_GEN_MIGR);
9737 newmap->num_members = u->new_raid_disks;
9738 for (i = 0; i < delta_disks; i++) {
9739 set_imsm_ord_tbl_ent(newmap,
9740 u->old_raid_disks + i,
9741 u->old_raid_disks + i);
9742 }
9743 /* New map is correct, now need to save old map
9744 */
9745 newmap = get_imsm_map(newdev, MAP_1);
9746 memcpy(newmap, oldmap, sizeof_imsm_map(oldmap));
9747
9748 imsm_set_array_size(newdev, -1);
9749 }
9750
9751 sp = (void **)id->dev;
9752 id->dev = newdev;
9753 *sp = tofree;
9754 tofree = sp;
9755
9756 /* Clear migration record */
9757 memset(super->migr_rec, 0, sizeof(struct migr_record));
9758 }
9759 if (tofree)
9760 *space_list = tofree;
9761 ret_val = 1;
9762
9763 update_reshape_exit:
9764
9765 return ret_val;
9766 }
9767
9768 static int apply_takeover_update(struct imsm_update_takeover *u,
9769 struct intel_super *super,
9770 void ***space_list)
9771 {
9772 struct imsm_dev *dev = NULL;
9773 struct intel_dev *dv;
9774 struct imsm_dev *dev_new;
9775 struct imsm_map *map;
9776 struct dl *dm, *du;
9777 int i;
9778
9779 for (dv = super->devlist; dv; dv = dv->next)
9780 if (dv->index == (unsigned int)u->subarray) {
9781 dev = dv->dev;
9782 break;
9783 }
9784
9785 if (dev == NULL)
9786 return 0;
9787
9788 map = get_imsm_map(dev, MAP_0);
9789
9790 if (u->direction == R10_TO_R0) {
9791 /* Number of failed disks must be half of initial disk number */
9792 if (imsm_count_failed(super, dev, MAP_0) !=
9793 (map->num_members / 2))
9794 return 0;
9795
9796 /* iterate through devices to mark removed disks as spare */
9797 for (dm = super->disks; dm; dm = dm->next) {
9798 if (dm->disk.status & FAILED_DISK) {
9799 int idx = dm->index;
9800 /* update indexes on the disk list */
9801 /* FIXME this loop-with-the-loop looks wrong, I'm not convinced
9802 the index values will end up being correct.... NB */
9803 for (du = super->disks; du; du = du->next)
9804 if (du->index > idx)
9805 du->index--;
9806 /* mark as spare disk */
9807 mark_spare(dm);
9808 }
9809 }
9810 /* update map */
9811 map->num_members /= map->num_domains;
9812 map->map_state = IMSM_T_STATE_NORMAL;
9813 map->raid_level = 0;
9814 set_num_domains(map);
9815 update_num_data_stripes(map, imsm_dev_size(dev));
9816 map->failed_disk_num = -1;
9817 }
9818
9819 if (u->direction == R0_TO_R10) {
9820 void **space;
9821
9822 /* update slots in current disk list */
9823 for (dm = super->disks; dm; dm = dm->next) {
9824 if (dm->index >= 0)
9825 dm->index *= 2;
9826 }
9827 /* create new *missing* disks */
9828 for (i = 0; i < map->num_members; i++) {
9829 space = *space_list;
9830 if (!space)
9831 continue;
9832 *space_list = *space;
9833 du = (void *)space;
9834 memcpy(du, super->disks, sizeof(*du));
9835 du->fd = -1;
9836 du->minor = 0;
9837 du->major = 0;
9838 du->index = (i * 2) + 1;
9839 sprintf((char *)du->disk.serial,
9840 " MISSING_%d", du->index);
9841 sprintf((char *)du->serial,
9842 "MISSING_%d", du->index);
9843 du->next = super->missing;
9844 super->missing = du;
9845 }
9846 /* create new dev and map */
9847 space = *space_list;
9848 if (!space)
9849 return 0;
9850 *space_list = *space;
9851 dev_new = (void *)space;
9852 memcpy(dev_new, dev, sizeof(*dev));
9853 /* update new map */
9854 map = get_imsm_map(dev_new, MAP_0);
9855
9856 map->map_state = IMSM_T_STATE_DEGRADED;
9857 map->raid_level = 1;
9858 set_num_domains(map);
9859 map->num_members = map->num_members * map->num_domains;
9860 update_num_data_stripes(map, imsm_dev_size(dev));
9861
9862 /* replace dev<->dev_new */
9863 dv->dev = dev_new;
9864 }
9865 /* update disk order table */
9866 for (du = super->disks; du; du = du->next)
9867 if (du->index >= 0)
9868 set_imsm_ord_tbl_ent(map, du->index, du->index);
9869 for (du = super->missing; du; du = du->next)
9870 if (du->index >= 0) {
9871 set_imsm_ord_tbl_ent(map, du->index, du->index);
9872 mark_missing(super, dv->dev, &du->disk, du->index);
9873 }
9874
9875 return 1;
9876 }
9877
9878 static void imsm_process_update(struct supertype *st,
9879 struct metadata_update *update)
9880 {
9881 /**
9882 * crack open the metadata_update envelope to find the update record
9883 * update can be one of:
9884 * update_reshape_container_disks - all the arrays in the container
9885 * are being reshaped to have more devices. We need to mark
9886 * the arrays for general migration and convert selected spares
9887 * into active devices.
9888 * update_activate_spare - a spare device has replaced a failed
9889 * device in an array, update the disk_ord_tbl. If this disk is
9890 * present in all member arrays then also clear the SPARE_DISK
9891 * flag
9892 * update_create_array
9893 * update_kill_array
9894 * update_rename_array
9895 * update_add_remove_disk
9896 */
9897 struct intel_super *super = st->sb;
9898 struct imsm_super *mpb;
9899 enum imsm_update_type type = *(enum imsm_update_type *) update->buf;
9900
9901 /* update requires a larger buf but the allocation failed */
9902 if (super->next_len && !super->next_buf) {
9903 super->next_len = 0;
9904 return;
9905 }
9906
9907 if (super->next_buf) {
9908 memcpy(super->next_buf, super->buf, super->len);
9909 free(super->buf);
9910 super->len = super->next_len;
9911 super->buf = super->next_buf;
9912
9913 super->next_len = 0;
9914 super->next_buf = NULL;
9915 }
9916
9917 mpb = super->anchor;
9918
9919 switch (type) {
9920 case update_general_migration_checkpoint: {
9921 struct intel_dev *id;
9922 struct imsm_update_general_migration_checkpoint *u =
9923 (void *)update->buf;
9924
9925 dprintf("called for update_general_migration_checkpoint\n");
9926
9927 /* find device under general migration */
9928 for (id = super->devlist ; id; id = id->next) {
9929 if (is_gen_migration(id->dev)) {
9930 set_vol_curr_migr_unit(id->dev,
9931 u->curr_migr_unit);
9932 super->updates_pending++;
9933 }
9934 }
9935 break;
9936 }
9937 case update_takeover: {
9938 struct imsm_update_takeover *u = (void *)update->buf;
9939 if (apply_takeover_update(u, super, &update->space_list)) {
9940 imsm_update_version_info(super);
9941 super->updates_pending++;
9942 }
9943 break;
9944 }
9945
9946 case update_reshape_container_disks: {
9947 struct imsm_update_reshape *u = (void *)update->buf;
9948 if (apply_reshape_container_disks_update(
9949 u, super, &update->space_list))
9950 super->updates_pending++;
9951 break;
9952 }
9953 case update_reshape_migration: {
9954 struct imsm_update_reshape_migration *u = (void *)update->buf;
9955 if (apply_reshape_migration_update(
9956 u, super, &update->space_list))
9957 super->updates_pending++;
9958 break;
9959 }
9960 case update_size_change: {
9961 struct imsm_update_size_change *u = (void *)update->buf;
9962 if (apply_size_change_update(u, super))
9963 super->updates_pending++;
9964 break;
9965 }
9966 case update_activate_spare: {
9967 struct imsm_update_activate_spare *u = (void *) update->buf;
9968
9969 if (prepare_spare_to_activate(st, u) &&
9970 apply_update_activate_spare(u, super, st->arrays))
9971 super->updates_pending++;
9972 break;
9973 }
9974 case update_create_array: {
9975 /* someone wants to create a new array, we need to be aware of
9976 * a few races/collisions:
9977 * 1/ 'Create' called by two separate instances of mdadm
9978 * 2/ 'Create' versus 'activate_spare': mdadm has chosen
9979 * devices that have since been assimilated via
9980 * activate_spare.
9981 * In the event this update can not be carried out mdadm will
9982 * (FIX ME) notice that its update did not take hold.
9983 */
9984 struct imsm_update_create_array *u = (void *) update->buf;
9985 struct intel_dev *dv;
9986 struct imsm_dev *dev;
9987 struct imsm_map *map, *new_map;
9988 unsigned long long start, end;
9989 unsigned long long new_start, new_end;
9990 int i;
9991 struct disk_info *inf;
9992 struct dl *dl;
9993
9994 /* handle racing creates: first come first serve */
9995 if (u->dev_idx < mpb->num_raid_devs) {
9996 dprintf("subarray %d already defined\n", u->dev_idx);
9997 goto create_error;
9998 }
9999
10000 /* check update is next in sequence */
10001 if (u->dev_idx != mpb->num_raid_devs) {
10002 dprintf("can not create array %d expected index %d\n",
10003 u->dev_idx, mpb->num_raid_devs);
10004 goto create_error;
10005 }
10006
10007 new_map = get_imsm_map(&u->dev, MAP_0);
10008 new_start = pba_of_lba0(new_map);
10009 new_end = new_start + per_dev_array_size(new_map);
10010 inf = get_disk_info(u);
10011
10012 /* handle activate_spare versus create race:
10013 * check to make sure that overlapping arrays do not include
10014 * overalpping disks
10015 */
10016 for (i = 0; i < mpb->num_raid_devs; i++) {
10017 dev = get_imsm_dev(super, i);
10018 map = get_imsm_map(dev, MAP_0);
10019 start = pba_of_lba0(map);
10020 end = start + per_dev_array_size(map);
10021 if ((new_start >= start && new_start <= end) ||
10022 (start >= new_start && start <= new_end))
10023 /* overlap */;
10024 else
10025 continue;
10026
10027 if (disks_overlap(super, i, u)) {
10028 dprintf("arrays overlap\n");
10029 goto create_error;
10030 }
10031 }
10032
10033 /* check that prepare update was successful */
10034 if (!update->space) {
10035 dprintf("prepare update failed\n");
10036 goto create_error;
10037 }
10038
10039 /* check that all disks are still active before committing
10040 * changes. FIXME: could we instead handle this by creating a
10041 * degraded array? That's probably not what the user expects,
10042 * so better to drop this update on the floor.
10043 */
10044 for (i = 0; i < new_map->num_members; i++) {
10045 dl = serial_to_dl(inf[i].serial, super);
10046 if (!dl) {
10047 dprintf("disk disappeared\n");
10048 goto create_error;
10049 }
10050 }
10051
10052 super->updates_pending++;
10053
10054 /* convert spares to members and fixup ord_tbl */
10055 for (i = 0; i < new_map->num_members; i++) {
10056 dl = serial_to_dl(inf[i].serial, super);
10057 if (dl->index == -1) {
10058 dl->index = mpb->num_disks;
10059 mpb->num_disks++;
10060 dl->disk.status |= CONFIGURED_DISK;
10061 dl->disk.status &= ~SPARE_DISK;
10062 }
10063 set_imsm_ord_tbl_ent(new_map, i, dl->index);
10064 }
10065
10066 dv = update->space;
10067 dev = dv->dev;
10068 update->space = NULL;
10069 imsm_copy_dev(dev, &u->dev);
10070 dv->index = u->dev_idx;
10071 dv->next = super->devlist;
10072 super->devlist = dv;
10073 mpb->num_raid_devs++;
10074
10075 imsm_update_version_info(super);
10076 break;
10077 create_error:
10078 /* mdmon knows how to release update->space, but not
10079 * ((struct intel_dev *) update->space)->dev
10080 */
10081 if (update->space) {
10082 dv = update->space;
10083 free(dv->dev);
10084 }
10085 break;
10086 }
10087 case update_kill_array: {
10088 struct imsm_update_kill_array *u = (void *) update->buf;
10089 int victim = u->dev_idx;
10090 struct active_array *a;
10091 struct intel_dev **dp;
10092 struct imsm_dev *dev;
10093
10094 /* sanity check that we are not affecting the uuid of
10095 * active arrays, or deleting an active array
10096 *
10097 * FIXME when immutable ids are available, but note that
10098 * we'll also need to fixup the invalidated/active
10099 * subarray indexes in mdstat
10100 */
10101 for (a = st->arrays; a; a = a->next)
10102 if (a->info.container_member >= victim)
10103 break;
10104 /* by definition if mdmon is running at least one array
10105 * is active in the container, so checking
10106 * mpb->num_raid_devs is just extra paranoia
10107 */
10108 dev = get_imsm_dev(super, victim);
10109 if (a || !dev || mpb->num_raid_devs == 1) {
10110 dprintf("failed to delete subarray-%d\n", victim);
10111 break;
10112 }
10113
10114 for (dp = &super->devlist; *dp;)
10115 if ((*dp)->index == (unsigned)super->current_vol) {
10116 *dp = (*dp)->next;
10117 } else {
10118 if ((*dp)->index > (unsigned)victim)
10119 (*dp)->index--;
10120 dp = &(*dp)->next;
10121 }
10122 mpb->num_raid_devs--;
10123 super->updates_pending++;
10124 break;
10125 }
10126 case update_rename_array: {
10127 struct imsm_update_rename_array *u = (void *) update->buf;
10128 char name[MAX_RAID_SERIAL_LEN+1];
10129 int target = u->dev_idx;
10130 struct active_array *a;
10131 struct imsm_dev *dev;
10132
10133 /* sanity check that we are not affecting the uuid of
10134 * an active array
10135 */
10136 memset(name, 0, sizeof(name));
10137 snprintf(name, MAX_RAID_SERIAL_LEN, "%s", (char *) u->name);
10138 name[MAX_RAID_SERIAL_LEN] = '\0';
10139 for (a = st->arrays; a; a = a->next)
10140 if (a->info.container_member == target)
10141 break;
10142 dev = get_imsm_dev(super, u->dev_idx);
10143 if (a || !dev || !check_name(super, name, 1)) {
10144 dprintf("failed to rename subarray-%d\n", target);
10145 break;
10146 }
10147
10148 memcpy(dev->volume, name, MAX_RAID_SERIAL_LEN);
10149 super->updates_pending++;
10150 break;
10151 }
10152 case update_add_remove_disk: {
10153 /* we may be able to repair some arrays if disks are
10154 * being added, check the status of add_remove_disk
10155 * if discs has been added.
10156 */
10157 if (add_remove_disk_update(super)) {
10158 struct active_array *a;
10159
10160 super->updates_pending++;
10161 for (a = st->arrays; a; a = a->next)
10162 a->check_degraded = 1;
10163 }
10164 break;
10165 }
10166 case update_prealloc_badblocks_mem:
10167 break;
10168 case update_rwh_policy: {
10169 struct imsm_update_rwh_policy *u = (void *)update->buf;
10170 int target = u->dev_idx;
10171 struct imsm_dev *dev = get_imsm_dev(super, target);
10172 if (!dev) {
10173 dprintf("could not find subarray-%d\n", target);
10174 break;
10175 }
10176
10177 if (dev->rwh_policy != u->new_policy) {
10178 dev->rwh_policy = u->new_policy;
10179 super->updates_pending++;
10180 }
10181 break;
10182 }
10183 default:
10184 pr_err("error: unsupported process update type:(type: %d)\n", type);
10185 }
10186 }
10187
10188 static struct mdinfo *get_spares_for_grow(struct supertype *st);
10189
10190 static int imsm_prepare_update(struct supertype *st,
10191 struct metadata_update *update)
10192 {
10193 /**
10194 * Allocate space to hold new disk entries, raid-device entries or a new
10195 * mpb if necessary. The manager synchronously waits for updates to
10196 * complete in the monitor, so new mpb buffers allocated here can be
10197 * integrated by the monitor thread without worrying about live pointers
10198 * in the manager thread.
10199 */
10200 enum imsm_update_type type;
10201 struct intel_super *super = st->sb;
10202 unsigned int sector_size = super->sector_size;
10203 struct imsm_super *mpb = super->anchor;
10204 size_t buf_len;
10205 size_t len = 0;
10206
10207 if (update->len < (int)sizeof(type))
10208 return 0;
10209
10210 type = *(enum imsm_update_type *) update->buf;
10211
10212 switch (type) {
10213 case update_general_migration_checkpoint:
10214 if (update->len < (int)sizeof(struct imsm_update_general_migration_checkpoint))
10215 return 0;
10216 dprintf("called for update_general_migration_checkpoint\n");
10217 break;
10218 case update_takeover: {
10219 struct imsm_update_takeover *u = (void *)update->buf;
10220 if (update->len < (int)sizeof(*u))
10221 return 0;
10222 if (u->direction == R0_TO_R10) {
10223 void **tail = (void **)&update->space_list;
10224 struct imsm_dev *dev = get_imsm_dev(super, u->subarray);
10225 struct imsm_map *map = get_imsm_map(dev, MAP_0);
10226 int num_members = map->num_members;
10227 void *space;
10228 int size, i;
10229 /* allocate memory for added disks */
10230 for (i = 0; i < num_members; i++) {
10231 size = sizeof(struct dl);
10232 space = xmalloc(size);
10233 *tail = space;
10234 tail = space;
10235 *tail = NULL;
10236 }
10237 /* allocate memory for new device */
10238 size = sizeof_imsm_dev(super->devlist->dev, 0) +
10239 (num_members * sizeof(__u32));
10240 space = xmalloc(size);
10241 *tail = space;
10242 tail = space;
10243 *tail = NULL;
10244 len = disks_to_mpb_size(num_members * 2);
10245 }
10246
10247 break;
10248 }
10249 case update_reshape_container_disks: {
10250 /* Every raid device in the container is about to
10251 * gain some more devices, and we will enter a
10252 * reconfiguration.
10253 * So each 'imsm_map' will be bigger, and the imsm_vol
10254 * will now hold 2 of them.
10255 * Thus we need new 'struct imsm_dev' allocations sized
10256 * as sizeof_imsm_dev but with more devices in both maps.
10257 */
10258 struct imsm_update_reshape *u = (void *)update->buf;
10259 struct intel_dev *dl;
10260 void **space_tail = (void**)&update->space_list;
10261
10262 if (update->len < (int)sizeof(*u))
10263 return 0;
10264
10265 dprintf("for update_reshape\n");
10266
10267 for (dl = super->devlist; dl; dl = dl->next) {
10268 int size = sizeof_imsm_dev(dl->dev, 1);
10269 void *s;
10270 if (u->new_raid_disks > u->old_raid_disks)
10271 size += sizeof(__u32)*2*
10272 (u->new_raid_disks - u->old_raid_disks);
10273 s = xmalloc(size);
10274 *space_tail = s;
10275 space_tail = s;
10276 *space_tail = NULL;
10277 }
10278
10279 len = disks_to_mpb_size(u->new_raid_disks);
10280 dprintf("New anchor length is %llu\n", (unsigned long long)len);
10281 break;
10282 }
10283 case update_reshape_migration: {
10284 /* for migration level 0->5 we need to add disks
10285 * so the same as for container operation we will copy
10286 * device to the bigger location.
10287 * in memory prepared device and new disk area are prepared
10288 * for usage in process update
10289 */
10290 struct imsm_update_reshape_migration *u = (void *)update->buf;
10291 struct intel_dev *id;
10292 void **space_tail = (void **)&update->space_list;
10293 int size;
10294 void *s;
10295 int current_level = -1;
10296
10297 if (update->len < (int)sizeof(*u))
10298 return 0;
10299
10300 dprintf("for update_reshape\n");
10301
10302 /* add space for bigger array in update
10303 */
10304 for (id = super->devlist; id; id = id->next) {
10305 if (id->index == (unsigned)u->subdev) {
10306 size = sizeof_imsm_dev(id->dev, 1);
10307 if (u->new_raid_disks > u->old_raid_disks)
10308 size += sizeof(__u32)*2*
10309 (u->new_raid_disks - u->old_raid_disks);
10310 s = xmalloc(size);
10311 *space_tail = s;
10312 space_tail = s;
10313 *space_tail = NULL;
10314 break;
10315 }
10316 }
10317 if (update->space_list == NULL)
10318 break;
10319
10320 /* add space for disk in update
10321 */
10322 size = sizeof(struct dl);
10323 s = xmalloc(size);
10324 *space_tail = s;
10325 space_tail = s;
10326 *space_tail = NULL;
10327
10328 /* add spare device to update
10329 */
10330 for (id = super->devlist ; id; id = id->next)
10331 if (id->index == (unsigned)u->subdev) {
10332 struct imsm_dev *dev;
10333 struct imsm_map *map;
10334
10335 dev = get_imsm_dev(super, u->subdev);
10336 map = get_imsm_map(dev, MAP_0);
10337 current_level = map->raid_level;
10338 break;
10339 }
10340 if (u->new_level == 5 && u->new_level != current_level) {
10341 struct mdinfo *spares;
10342
10343 spares = get_spares_for_grow(st);
10344 if (spares) {
10345 struct dl *dl;
10346 struct mdinfo *dev;
10347
10348 dev = spares->devs;
10349 if (dev) {
10350 u->new_disks[0] =
10351 makedev(dev->disk.major,
10352 dev->disk.minor);
10353 dl = get_disk_super(super,
10354 dev->disk.major,
10355 dev->disk.minor);
10356 dl->index = u->old_raid_disks;
10357 dev = dev->next;
10358 }
10359 sysfs_free(spares);
10360 }
10361 }
10362 len = disks_to_mpb_size(u->new_raid_disks);
10363 dprintf("New anchor length is %llu\n", (unsigned long long)len);
10364 break;
10365 }
10366 case update_size_change: {
10367 if (update->len < (int)sizeof(struct imsm_update_size_change))
10368 return 0;
10369 break;
10370 }
10371 case update_activate_spare: {
10372 if (update->len < (int)sizeof(struct imsm_update_activate_spare))
10373 return 0;
10374 break;
10375 }
10376 case update_create_array: {
10377 struct imsm_update_create_array *u = (void *) update->buf;
10378 struct intel_dev *dv;
10379 struct imsm_dev *dev = &u->dev;
10380 struct imsm_map *map = get_imsm_map(dev, MAP_0);
10381 struct dl *dl;
10382 struct disk_info *inf;
10383 int i;
10384 int activate = 0;
10385
10386 if (update->len < (int)sizeof(*u))
10387 return 0;
10388
10389 inf = get_disk_info(u);
10390 len = sizeof_imsm_dev(dev, 1);
10391 /* allocate a new super->devlist entry */
10392 dv = xmalloc(sizeof(*dv));
10393 dv->dev = xmalloc(len);
10394 update->space = dv;
10395
10396 /* count how many spares will be converted to members */
10397 for (i = 0; i < map->num_members; i++) {
10398 dl = serial_to_dl(inf[i].serial, super);
10399 if (!dl) {
10400 /* hmm maybe it failed?, nothing we can do about
10401 * it here
10402 */
10403 continue;
10404 }
10405 if (count_memberships(dl, super) == 0)
10406 activate++;
10407 }
10408 len += activate * sizeof(struct imsm_disk);
10409 break;
10410 }
10411 case update_kill_array: {
10412 if (update->len < (int)sizeof(struct imsm_update_kill_array))
10413 return 0;
10414 break;
10415 }
10416 case update_rename_array: {
10417 if (update->len < (int)sizeof(struct imsm_update_rename_array))
10418 return 0;
10419 break;
10420 }
10421 case update_add_remove_disk:
10422 /* no update->len needed */
10423 break;
10424 case update_prealloc_badblocks_mem:
10425 super->extra_space += sizeof(struct bbm_log) -
10426 get_imsm_bbm_log_size(super->bbm_log);
10427 break;
10428 case update_rwh_policy: {
10429 if (update->len < (int)sizeof(struct imsm_update_rwh_policy))
10430 return 0;
10431 break;
10432 }
10433 default:
10434 return 0;
10435 }
10436
10437 /* check if we need a larger metadata buffer */
10438 if (super->next_buf)
10439 buf_len = super->next_len;
10440 else
10441 buf_len = super->len;
10442
10443 if (__le32_to_cpu(mpb->mpb_size) + super->extra_space + len > buf_len) {
10444 /* ok we need a larger buf than what is currently allocated
10445 * if this allocation fails process_update will notice that
10446 * ->next_len is set and ->next_buf is NULL
10447 */
10448 buf_len = ROUND_UP(__le32_to_cpu(mpb->mpb_size) +
10449 super->extra_space + len, sector_size);
10450 if (super->next_buf)
10451 free(super->next_buf);
10452
10453 super->next_len = buf_len;
10454 if (posix_memalign(&super->next_buf, sector_size, buf_len) == 0)
10455 memset(super->next_buf, 0, buf_len);
10456 else
10457 super->next_buf = NULL;
10458 }
10459 return 1;
10460 }
10461
10462 /* must be called while manager is quiesced */
10463 static void imsm_delete(struct intel_super *super, struct dl **dlp, unsigned index)
10464 {
10465 struct imsm_super *mpb = super->anchor;
10466 struct dl *iter;
10467 struct imsm_dev *dev;
10468 struct imsm_map *map;
10469 unsigned int i, j, num_members;
10470 __u32 ord, ord_map0;
10471 struct bbm_log *log = super->bbm_log;
10472
10473 dprintf("deleting device[%d] from imsm_super\n", index);
10474
10475 /* shift all indexes down one */
10476 for (iter = super->disks; iter; iter = iter->next)
10477 if (iter->index > (int)index)
10478 iter->index--;
10479 for (iter = super->missing; iter; iter = iter->next)
10480 if (iter->index > (int)index)
10481 iter->index--;
10482
10483 for (i = 0; i < mpb->num_raid_devs; i++) {
10484 dev = get_imsm_dev(super, i);
10485 map = get_imsm_map(dev, MAP_0);
10486 num_members = map->num_members;
10487 for (j = 0; j < num_members; j++) {
10488 /* update ord entries being careful not to propagate
10489 * ord-flags to the first map
10490 */
10491 ord = get_imsm_ord_tbl_ent(dev, j, MAP_X);
10492 ord_map0 = get_imsm_ord_tbl_ent(dev, j, MAP_0);
10493
10494 if (ord_to_idx(ord) <= index)
10495 continue;
10496
10497 map = get_imsm_map(dev, MAP_0);
10498 set_imsm_ord_tbl_ent(map, j, ord_map0 - 1);
10499 map = get_imsm_map(dev, MAP_1);
10500 if (map)
10501 set_imsm_ord_tbl_ent(map, j, ord - 1);
10502 }
10503 }
10504
10505 for (i = 0; i < log->entry_count; i++) {
10506 struct bbm_log_entry *entry = &log->marked_block_entries[i];
10507
10508 if (entry->disk_ordinal <= index)
10509 continue;
10510 entry->disk_ordinal--;
10511 }
10512
10513 mpb->num_disks--;
10514 super->updates_pending++;
10515 if (*dlp) {
10516 struct dl *dl = *dlp;
10517
10518 *dlp = (*dlp)->next;
10519 __free_imsm_disk(dl, 1);
10520 }
10521 }
10522
10523 static int imsm_get_allowed_degradation(int level, int raid_disks,
10524 struct intel_super *super,
10525 struct imsm_dev *dev)
10526 {
10527 switch (level) {
10528 case 1:
10529 case 10:{
10530 int ret_val = 0;
10531 struct imsm_map *map;
10532 int i;
10533
10534 ret_val = raid_disks/2;
10535 /* check map if all disks pairs not failed
10536 * in both maps
10537 */
10538 map = get_imsm_map(dev, MAP_0);
10539 for (i = 0; i < ret_val; i++) {
10540 int degradation = 0;
10541 if (get_imsm_disk(super, i) == NULL)
10542 degradation++;
10543 if (get_imsm_disk(super, i + 1) == NULL)
10544 degradation++;
10545 if (degradation == 2)
10546 return 0;
10547 }
10548 map = get_imsm_map(dev, MAP_1);
10549 /* if there is no second map
10550 * result can be returned
10551 */
10552 if (map == NULL)
10553 return ret_val;
10554 /* check degradation in second map
10555 */
10556 for (i = 0; i < ret_val; i++) {
10557 int degradation = 0;
10558 if (get_imsm_disk(super, i) == NULL)
10559 degradation++;
10560 if (get_imsm_disk(super, i + 1) == NULL)
10561 degradation++;
10562 if (degradation == 2)
10563 return 0;
10564 }
10565 return ret_val;
10566 }
10567 case 5:
10568 return 1;
10569 case 6:
10570 return 2;
10571 default:
10572 return 0;
10573 }
10574 }
10575
10576 /*******************************************************************************
10577 * Function: validate_container_imsm
10578 * Description: This routine validates container after assemble,
10579 * eg. if devices in container are under the same controller.
10580 *
10581 * Parameters:
10582 * info : linked list with info about devices used in array
10583 * Returns:
10584 * 1 : HBA mismatch
10585 * 0 : Success
10586 ******************************************************************************/
10587 int validate_container_imsm(struct mdinfo *info)
10588 {
10589 if (check_env("IMSM_NO_PLATFORM"))
10590 return 0;
10591
10592 struct sys_dev *idev;
10593 struct sys_dev *hba = NULL;
10594 struct sys_dev *intel_devices = find_intel_devices();
10595 char *dev_path = devt_to_devpath(makedev(info->disk.major,
10596 info->disk.minor), 1, NULL);
10597
10598 for (idev = intel_devices; idev; idev = idev->next) {
10599 if (dev_path && strstr(dev_path, idev->path)) {
10600 hba = idev;
10601 break;
10602 }
10603 }
10604 if (dev_path)
10605 free(dev_path);
10606
10607 if (!hba) {
10608 pr_err("WARNING - Cannot detect HBA for device %s!\n",
10609 devid2kname(makedev(info->disk.major, info->disk.minor)));
10610 return 1;
10611 }
10612
10613 const struct imsm_orom *orom = get_orom_by_device_id(hba->dev_id);
10614 struct mdinfo *dev;
10615
10616 for (dev = info->next; dev; dev = dev->next) {
10617 dev_path = devt_to_devpath(makedev(dev->disk.major,
10618 dev->disk.minor), 1, NULL);
10619
10620 struct sys_dev *hba2 = NULL;
10621 for (idev = intel_devices; idev; idev = idev->next) {
10622 if (dev_path && strstr(dev_path, idev->path)) {
10623 hba2 = idev;
10624 break;
10625 }
10626 }
10627 if (dev_path)
10628 free(dev_path);
10629
10630 const struct imsm_orom *orom2 = hba2 == NULL ? NULL :
10631 get_orom_by_device_id(hba2->dev_id);
10632
10633 if (hba2 && hba->type != hba2->type) {
10634 pr_err("WARNING - HBAs of devices do not match %s != %s\n",
10635 get_sys_dev_type(hba->type), get_sys_dev_type(hba2->type));
10636 return 1;
10637 }
10638
10639 if (orom != orom2) {
10640 pr_err("WARNING - IMSM container assembled with disks under different HBAs!\n"
10641 " This operation is not supported and can lead to data loss.\n");
10642 return 1;
10643 }
10644
10645 if (!orom) {
10646 pr_err("WARNING - IMSM container assembled with disks under HBAs without IMSM platform support!\n"
10647 " This operation is not supported and can lead to data loss.\n");
10648 return 1;
10649 }
10650 }
10651
10652 return 0;
10653 }
10654
10655 /*******************************************************************************
10656 * Function: imsm_record_badblock
10657 * Description: This routine stores new bad block record in BBM log
10658 *
10659 * Parameters:
10660 * a : array containing a bad block
10661 * slot : disk number containing a bad block
10662 * sector : bad block sector
10663 * length : bad block sectors range
10664 * Returns:
10665 * 1 : Success
10666 * 0 : Error
10667 ******************************************************************************/
10668 static int imsm_record_badblock(struct active_array *a, int slot,
10669 unsigned long long sector, int length)
10670 {
10671 struct intel_super *super = a->container->sb;
10672 int ord;
10673 int ret;
10674
10675 ord = imsm_disk_slot_to_ord(a, slot);
10676 if (ord < 0)
10677 return 0;
10678
10679 ret = record_new_badblock(super->bbm_log, ord_to_idx(ord), sector,
10680 length);
10681 if (ret)
10682 super->updates_pending++;
10683
10684 return ret;
10685 }
10686 /*******************************************************************************
10687 * Function: imsm_clear_badblock
10688 * Description: This routine clears bad block record from BBM log
10689 *
10690 * Parameters:
10691 * a : array containing a bad block
10692 * slot : disk number containing a bad block
10693 * sector : bad block sector
10694 * length : bad block sectors range
10695 * Returns:
10696 * 1 : Success
10697 * 0 : Error
10698 ******************************************************************************/
10699 static int imsm_clear_badblock(struct active_array *a, int slot,
10700 unsigned long long sector, int length)
10701 {
10702 struct intel_super *super = a->container->sb;
10703 int ord;
10704 int ret;
10705
10706 ord = imsm_disk_slot_to_ord(a, slot);
10707 if (ord < 0)
10708 return 0;
10709
10710 ret = clear_badblock(super->bbm_log, ord_to_idx(ord), sector, length);
10711 if (ret)
10712 super->updates_pending++;
10713
10714 return ret;
10715 }
10716 /*******************************************************************************
10717 * Function: imsm_get_badblocks
10718 * Description: This routine get list of bad blocks for an array
10719 *
10720 * Parameters:
10721 * a : array
10722 * slot : disk number
10723 * Returns:
10724 * bb : structure containing bad blocks
10725 * NULL : error
10726 ******************************************************************************/
10727 static struct md_bb *imsm_get_badblocks(struct active_array *a, int slot)
10728 {
10729 int inst = a->info.container_member;
10730 struct intel_super *super = a->container->sb;
10731 struct imsm_dev *dev = get_imsm_dev(super, inst);
10732 struct imsm_map *map = get_imsm_map(dev, MAP_0);
10733 int ord;
10734
10735 ord = imsm_disk_slot_to_ord(a, slot);
10736 if (ord < 0)
10737 return NULL;
10738
10739 get_volume_badblocks(super->bbm_log, ord_to_idx(ord), pba_of_lba0(map),
10740 per_dev_array_size(map), &super->bb);
10741
10742 return &super->bb;
10743 }
10744 /*******************************************************************************
10745 * Function: examine_badblocks_imsm
10746 * Description: Prints list of bad blocks on a disk to the standard output
10747 *
10748 * Parameters:
10749 * st : metadata handler
10750 * fd : open file descriptor for device
10751 * devname : device name
10752 * Returns:
10753 * 0 : Success
10754 * 1 : Error
10755 ******************************************************************************/
10756 static int examine_badblocks_imsm(struct supertype *st, int fd, char *devname)
10757 {
10758 struct intel_super *super = st->sb;
10759 struct bbm_log *log = super->bbm_log;
10760 struct dl *d = NULL;
10761 int any = 0;
10762
10763 for (d = super->disks; d ; d = d->next) {
10764 if (strcmp(d->devname, devname) == 0)
10765 break;
10766 }
10767
10768 if ((d == NULL) || (d->index < 0)) { /* serial mismatch probably */
10769 pr_err("%s doesn't appear to be part of a raid array\n",
10770 devname);
10771 return 1;
10772 }
10773
10774 if (log != NULL) {
10775 unsigned int i;
10776 struct bbm_log_entry *entry = &log->marked_block_entries[0];
10777
10778 for (i = 0; i < log->entry_count; i++) {
10779 if (entry[i].disk_ordinal == d->index) {
10780 unsigned long long sector = __le48_to_cpu(
10781 &entry[i].defective_block_start);
10782 int cnt = entry[i].marked_count + 1;
10783
10784 if (!any) {
10785 printf("Bad-blocks on %s:\n", devname);
10786 any = 1;
10787 }
10788
10789 printf("%20llu for %d sectors\n", sector, cnt);
10790 }
10791 }
10792 }
10793
10794 if (!any)
10795 printf("No bad-blocks list configured on %s\n", devname);
10796
10797 return 0;
10798 }
10799 /*******************************************************************************
10800 * Function: init_migr_record_imsm
10801 * Description: Function inits imsm migration record
10802 * Parameters:
10803 * super : imsm internal array info
10804 * dev : device under migration
10805 * info : general array info to find the smallest device
10806 * Returns:
10807 * none
10808 ******************************************************************************/
10809 void init_migr_record_imsm(struct supertype *st, struct imsm_dev *dev,
10810 struct mdinfo *info)
10811 {
10812 struct intel_super *super = st->sb;
10813 struct migr_record *migr_rec = super->migr_rec;
10814 int new_data_disks;
10815 unsigned long long dsize, dev_sectors;
10816 long long unsigned min_dev_sectors = -1LLU;
10817 struct imsm_map *map_dest = get_imsm_map(dev, MAP_0);
10818 struct imsm_map *map_src = get_imsm_map(dev, MAP_1);
10819 unsigned long long num_migr_units;
10820 unsigned long long array_blocks;
10821 struct dl *dl_disk = NULL;
10822
10823 memset(migr_rec, 0, sizeof(struct migr_record));
10824 migr_rec->family_num = __cpu_to_le32(super->anchor->family_num);
10825
10826 /* only ascending reshape supported now */
10827 migr_rec->ascending_migr = __cpu_to_le32(1);
10828
10829 migr_rec->dest_depth_per_unit = GEN_MIGR_AREA_SIZE /
10830 max(map_dest->blocks_per_strip, map_src->blocks_per_strip);
10831 migr_rec->dest_depth_per_unit *=
10832 max(map_dest->blocks_per_strip, map_src->blocks_per_strip);
10833 new_data_disks = imsm_num_data_members(map_dest);
10834 migr_rec->blocks_per_unit =
10835 __cpu_to_le32(migr_rec->dest_depth_per_unit * new_data_disks);
10836 migr_rec->dest_depth_per_unit =
10837 __cpu_to_le32(migr_rec->dest_depth_per_unit);
10838 array_blocks = info->component_size * new_data_disks;
10839 num_migr_units =
10840 array_blocks / __le32_to_cpu(migr_rec->blocks_per_unit);
10841
10842 if (array_blocks % __le32_to_cpu(migr_rec->blocks_per_unit))
10843 num_migr_units++;
10844 set_num_migr_units(migr_rec, num_migr_units);
10845
10846 migr_rec->post_migr_vol_cap = dev->size_low;
10847 migr_rec->post_migr_vol_cap_hi = dev->size_high;
10848
10849 /* Find the smallest dev */
10850 for (dl_disk = super->disks; dl_disk ; dl_disk = dl_disk->next) {
10851 /* ignore spares in container */
10852 if (dl_disk->index < 0)
10853 continue;
10854 get_dev_size(dl_disk->fd, NULL, &dsize);
10855 dev_sectors = dsize / 512;
10856 if (dev_sectors < min_dev_sectors)
10857 min_dev_sectors = dev_sectors;
10858 }
10859 set_migr_chkp_area_pba(migr_rec, min_dev_sectors -
10860 RAID_DISK_RESERVED_BLOCKS_IMSM_HI);
10861
10862 write_imsm_migr_rec(st);
10863
10864 return;
10865 }
10866
10867 /*******************************************************************************
10868 * Function: save_backup_imsm
10869 * Description: Function saves critical data stripes to Migration Copy Area
10870 * and updates the current migration unit status.
10871 * Use restore_stripes() to form a destination stripe,
10872 * and to write it to the Copy Area.
10873 * Parameters:
10874 * st : supertype information
10875 * dev : imsm device that backup is saved for
10876 * info : general array info
10877 * buf : input buffer
10878 * length : length of data to backup (blocks_per_unit)
10879 * Returns:
10880 * 0 : success
10881 *, -1 : fail
10882 ******************************************************************************/
10883 int save_backup_imsm(struct supertype *st,
10884 struct imsm_dev *dev,
10885 struct mdinfo *info,
10886 void *buf,
10887 int length)
10888 {
10889 int rv = -1;
10890 struct intel_super *super = st->sb;
10891 int i;
10892 struct imsm_map *map_dest = get_imsm_map(dev, MAP_0);
10893 int new_disks = map_dest->num_members;
10894 int dest_layout = 0;
10895 int dest_chunk, targets[new_disks];
10896 unsigned long long start, target_offsets[new_disks];
10897 int data_disks = imsm_num_data_members(map_dest);
10898
10899 for (i = 0; i < new_disks; i++) {
10900 struct dl *dl_disk = get_imsm_dl_disk(super, i);
10901 if (dl_disk && is_fd_valid(dl_disk->fd))
10902 targets[i] = dl_disk->fd;
10903 else
10904 goto abort;
10905 }
10906
10907 start = info->reshape_progress * 512;
10908 for (i = 0; i < new_disks; i++) {
10909 target_offsets[i] = migr_chkp_area_pba(super->migr_rec) * 512;
10910 /* move back copy area adderss, it will be moved forward
10911 * in restore_stripes() using start input variable
10912 */
10913 target_offsets[i] -= start/data_disks;
10914 }
10915
10916 dest_layout = imsm_level_to_layout(map_dest->raid_level);
10917 dest_chunk = __le16_to_cpu(map_dest->blocks_per_strip) * 512;
10918
10919 if (restore_stripes(targets, /* list of dest devices */
10920 target_offsets, /* migration record offsets */
10921 new_disks,
10922 dest_chunk,
10923 map_dest->raid_level,
10924 dest_layout,
10925 -1, /* source backup file descriptor */
10926 0, /* input buf offset
10927 * always 0 buf is already offseted */
10928 start,
10929 length,
10930 buf) != 0) {
10931 pr_err("Error restoring stripes\n");
10932 goto abort;
10933 }
10934
10935 rv = 0;
10936
10937 abort:
10938 return rv;
10939 }
10940
10941 /*******************************************************************************
10942 * Function: save_checkpoint_imsm
10943 * Description: Function called for current unit status update
10944 * in the migration record. It writes it to disk.
10945 * Parameters:
10946 * super : imsm internal array info
10947 * info : general array info
10948 * Returns:
10949 * 0: success
10950 * 1: failure
10951 * 2: failure, means no valid migration record
10952 * / no general migration in progress /
10953 ******************************************************************************/
10954 int save_checkpoint_imsm(struct supertype *st, struct mdinfo *info, int state)
10955 {
10956 struct intel_super *super = st->sb;
10957 unsigned long long blocks_per_unit;
10958 unsigned long long curr_migr_unit;
10959
10960 if (load_imsm_migr_rec(super) != 0) {
10961 dprintf("imsm: ERROR: Cannot read migration record for checkpoint save.\n");
10962 return 1;
10963 }
10964
10965 blocks_per_unit = __le32_to_cpu(super->migr_rec->blocks_per_unit);
10966 if (blocks_per_unit == 0) {
10967 dprintf("imsm: no migration in progress.\n");
10968 return 2;
10969 }
10970 curr_migr_unit = info->reshape_progress / blocks_per_unit;
10971 /* check if array is alligned to copy area
10972 * if it is not alligned, add one to current migration unit value
10973 * this can happend on array reshape finish only
10974 */
10975 if (info->reshape_progress % blocks_per_unit)
10976 curr_migr_unit++;
10977
10978 set_current_migr_unit(super->migr_rec, curr_migr_unit);
10979 super->migr_rec->rec_status = __cpu_to_le32(state);
10980 set_migr_dest_1st_member_lba(super->migr_rec,
10981 super->migr_rec->dest_depth_per_unit * curr_migr_unit);
10982
10983 if (write_imsm_migr_rec(st) < 0) {
10984 dprintf("imsm: Cannot write migration record outside backup area\n");
10985 return 1;
10986 }
10987
10988 return 0;
10989 }
10990
10991 /*******************************************************************************
10992 * Function: recover_backup_imsm
10993 * Description: Function recovers critical data from the Migration Copy Area
10994 * while assembling an array.
10995 * Parameters:
10996 * super : imsm internal array info
10997 * info : general array info
10998 * Returns:
10999 * 0 : success (or there is no data to recover)
11000 * 1 : fail
11001 ******************************************************************************/
11002 int recover_backup_imsm(struct supertype *st, struct mdinfo *info)
11003 {
11004 struct intel_super *super = st->sb;
11005 struct migr_record *migr_rec = super->migr_rec;
11006 struct imsm_map *map_dest;
11007 struct intel_dev *id = NULL;
11008 unsigned long long read_offset;
11009 unsigned long long write_offset;
11010 unsigned unit_len;
11011 int new_disks, err;
11012 char *buf = NULL;
11013 int retval = 1;
11014 unsigned int sector_size = super->sector_size;
11015 unsigned long long curr_migr_unit = current_migr_unit(migr_rec);
11016 unsigned long long num_migr_units = get_num_migr_units(migr_rec);
11017 char buffer[20];
11018 int skipped_disks = 0;
11019 struct dl *dl_disk;
11020
11021 err = sysfs_get_str(info, NULL, "array_state", (char *)buffer, 20);
11022 if (err < 1)
11023 return 1;
11024
11025 /* recover data only during assemblation */
11026 if (strncmp(buffer, "inactive", 8) != 0)
11027 return 0;
11028 /* no data to recover */
11029 if (__le32_to_cpu(migr_rec->rec_status) == UNIT_SRC_NORMAL)
11030 return 0;
11031 if (curr_migr_unit >= num_migr_units)
11032 return 1;
11033
11034 /* find device during reshape */
11035 for (id = super->devlist; id; id = id->next)
11036 if (is_gen_migration(id->dev))
11037 break;
11038 if (id == NULL)
11039 return 1;
11040
11041 map_dest = get_imsm_map(id->dev, MAP_0);
11042 new_disks = map_dest->num_members;
11043
11044 read_offset = migr_chkp_area_pba(migr_rec) * 512;
11045
11046 write_offset = (migr_dest_1st_member_lba(migr_rec) +
11047 pba_of_lba0(map_dest)) * 512;
11048
11049 unit_len = __le32_to_cpu(migr_rec->dest_depth_per_unit) * 512;
11050 if (posix_memalign((void **)&buf, sector_size, unit_len) != 0)
11051 goto abort;
11052
11053 for (dl_disk = super->disks; dl_disk; dl_disk = dl_disk->next) {
11054 if (dl_disk->index < 0)
11055 continue;
11056
11057 if (!is_fd_valid(dl_disk->fd)) {
11058 skipped_disks++;
11059 continue;
11060 }
11061 if (lseek64(dl_disk->fd, read_offset, SEEK_SET) < 0) {
11062 pr_err("Cannot seek to block: %s\n",
11063 strerror(errno));
11064 skipped_disks++;
11065 continue;
11066 }
11067 if (read(dl_disk->fd, buf, unit_len) != (ssize_t)unit_len) {
11068 pr_err("Cannot read copy area block: %s\n",
11069 strerror(errno));
11070 skipped_disks++;
11071 continue;
11072 }
11073 if (lseek64(dl_disk->fd, write_offset, SEEK_SET) < 0) {
11074 pr_err("Cannot seek to block: %s\n",
11075 strerror(errno));
11076 skipped_disks++;
11077 continue;
11078 }
11079 if (write(dl_disk->fd, buf, unit_len) != (ssize_t)unit_len) {
11080 pr_err("Cannot restore block: %s\n",
11081 strerror(errno));
11082 skipped_disks++;
11083 continue;
11084 }
11085 }
11086
11087 if (skipped_disks > imsm_get_allowed_degradation(info->new_level,
11088 new_disks,
11089 super,
11090 id->dev)) {
11091 pr_err("Cannot restore data from backup. Too many failed disks\n");
11092 goto abort;
11093 }
11094
11095 if (save_checkpoint_imsm(st, info, UNIT_SRC_NORMAL)) {
11096 /* ignore error == 2, this can mean end of reshape here
11097 */
11098 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL) during restart\n");
11099 } else
11100 retval = 0;
11101
11102 abort:
11103 free(buf);
11104 return retval;
11105 }
11106
11107 static char disk_by_path[] = "/dev/disk/by-path/";
11108
11109 static const char *imsm_get_disk_controller_domain(const char *path)
11110 {
11111 char disk_path[PATH_MAX];
11112 char *drv=NULL;
11113 struct stat st;
11114
11115 strcpy(disk_path, disk_by_path);
11116 strncat(disk_path, path, PATH_MAX - strlen(disk_path) - 1);
11117 if (stat(disk_path, &st) == 0) {
11118 struct sys_dev* hba;
11119 char *path;
11120
11121 path = devt_to_devpath(st.st_rdev, 1, NULL);
11122 if (path == NULL)
11123 return "unknown";
11124 hba = find_disk_attached_hba(-1, path);
11125 if (hba && hba->type == SYS_DEV_SAS)
11126 drv = "isci";
11127 else if (hba && hba->type == SYS_DEV_SATA)
11128 drv = "ahci";
11129 else if (hba && hba->type == SYS_DEV_VMD)
11130 drv = "vmd";
11131 else if (hba && hba->type == SYS_DEV_NVME)
11132 drv = "nvme";
11133 else
11134 drv = "unknown";
11135 dprintf("path: %s hba: %s attached: %s\n",
11136 path, (hba) ? hba->path : "NULL", drv);
11137 free(path);
11138 }
11139 return drv;
11140 }
11141
11142 static char *imsm_find_array_devnm_by_subdev(int subdev, char *container)
11143 {
11144 static char devnm[32];
11145 char subdev_name[20];
11146 struct mdstat_ent *mdstat;
11147
11148 sprintf(subdev_name, "%d", subdev);
11149 mdstat = mdstat_by_subdev(subdev_name, container);
11150 if (!mdstat)
11151 return NULL;
11152
11153 strcpy(devnm, mdstat->devnm);
11154 free_mdstat(mdstat);
11155 return devnm;
11156 }
11157
11158 static int imsm_reshape_is_allowed_on_container(struct supertype *st,
11159 struct geo_params *geo,
11160 int *old_raid_disks,
11161 int direction)
11162 {
11163 /* currently we only support increasing the number of devices
11164 * for a container. This increases the number of device for each
11165 * member array. They must all be RAID0 or RAID5.
11166 */
11167 int ret_val = 0;
11168 struct mdinfo *info, *member;
11169 int devices_that_can_grow = 0;
11170
11171 dprintf("imsm: imsm_reshape_is_allowed_on_container(ENTER): st->devnm = (%s)\n", st->devnm);
11172
11173 if (geo->size > 0 ||
11174 geo->level != UnSet ||
11175 geo->layout != UnSet ||
11176 geo->chunksize != 0 ||
11177 geo->raid_disks == UnSet) {
11178 dprintf("imsm: Container operation is allowed for raid disks number change only.\n");
11179 return ret_val;
11180 }
11181
11182 if (direction == ROLLBACK_METADATA_CHANGES) {
11183 dprintf("imsm: Metadata changes rollback is not supported for container operation.\n");
11184 return ret_val;
11185 }
11186
11187 info = container_content_imsm(st, NULL);
11188 for (member = info; member; member = member->next) {
11189 char *result;
11190
11191 dprintf("imsm: checking device_num: %i\n",
11192 member->container_member);
11193
11194 if (geo->raid_disks <= member->array.raid_disks) {
11195 /* we work on container for Online Capacity Expansion
11196 * only so raid_disks has to grow
11197 */
11198 dprintf("imsm: for container operation raid disks increase is required\n");
11199 break;
11200 }
11201
11202 if (info->array.level != 0 && info->array.level != 5) {
11203 /* we cannot use this container with other raid level
11204 */
11205 dprintf("imsm: for container operation wrong raid level (%i) detected\n",
11206 info->array.level);
11207 break;
11208 } else {
11209 /* check for platform support
11210 * for this raid level configuration
11211 */
11212 struct intel_super *super = st->sb;
11213 if (!is_raid_level_supported(super->orom,
11214 member->array.level,
11215 geo->raid_disks)) {
11216 dprintf("platform does not support raid%d with %d disk%s\n",
11217 info->array.level,
11218 geo->raid_disks,
11219 geo->raid_disks > 1 ? "s" : "");
11220 break;
11221 }
11222 /* check if component size is aligned to chunk size
11223 */
11224 if (info->component_size %
11225 (info->array.chunk_size/512)) {
11226 dprintf("Component size is not aligned to chunk size\n");
11227 break;
11228 }
11229 }
11230
11231 if (*old_raid_disks &&
11232 info->array.raid_disks != *old_raid_disks)
11233 break;
11234 *old_raid_disks = info->array.raid_disks;
11235
11236 /* All raid5 and raid0 volumes in container
11237 * have to be ready for Online Capacity Expansion
11238 * so they need to be assembled. We have already
11239 * checked that no recovery etc is happening.
11240 */
11241 result = imsm_find_array_devnm_by_subdev(member->container_member,
11242 st->container_devnm);
11243 if (result == NULL) {
11244 dprintf("imsm: cannot find array\n");
11245 break;
11246 }
11247 devices_that_can_grow++;
11248 }
11249 sysfs_free(info);
11250 if (!member && devices_that_can_grow)
11251 ret_val = 1;
11252
11253 if (ret_val)
11254 dprintf("Container operation allowed\n");
11255 else
11256 dprintf("Error: %i\n", ret_val);
11257
11258 return ret_val;
11259 }
11260
11261 /* Function: get_spares_for_grow
11262 * Description: Allocates memory and creates list of spare devices
11263 * avaliable in container. Checks if spare drive size is acceptable.
11264 * Parameters: Pointer to the supertype structure
11265 * Returns: Pointer to the list of spare devices (mdinfo structure) on success,
11266 * NULL if fail
11267 */
11268 static struct mdinfo *get_spares_for_grow(struct supertype *st)
11269 {
11270 struct spare_criteria sc;
11271
11272 get_spare_criteria_imsm(st, &sc);
11273 return container_choose_spares(st, &sc, NULL, NULL, NULL, 0);
11274 }
11275
11276 /******************************************************************************
11277 * function: imsm_create_metadata_update_for_reshape
11278 * Function creates update for whole IMSM container.
11279 *
11280 ******************************************************************************/
11281 static int imsm_create_metadata_update_for_reshape(
11282 struct supertype *st,
11283 struct geo_params *geo,
11284 int old_raid_disks,
11285 struct imsm_update_reshape **updatep)
11286 {
11287 struct intel_super *super = st->sb;
11288 struct imsm_super *mpb = super->anchor;
11289 int update_memory_size;
11290 struct imsm_update_reshape *u;
11291 struct mdinfo *spares;
11292 int i;
11293 int delta_disks;
11294 struct mdinfo *dev;
11295
11296 dprintf("(enter) raid_disks = %i\n", geo->raid_disks);
11297
11298 delta_disks = geo->raid_disks - old_raid_disks;
11299
11300 /* size of all update data without anchor */
11301 update_memory_size = sizeof(struct imsm_update_reshape);
11302
11303 /* now add space for spare disks that we need to add. */
11304 update_memory_size += sizeof(u->new_disks[0]) * (delta_disks - 1);
11305
11306 u = xcalloc(1, update_memory_size);
11307 u->type = update_reshape_container_disks;
11308 u->old_raid_disks = old_raid_disks;
11309 u->new_raid_disks = geo->raid_disks;
11310
11311 /* now get spare disks list
11312 */
11313 spares = get_spares_for_grow(st);
11314
11315 if (spares == NULL || delta_disks > spares->array.spare_disks) {
11316 pr_err("imsm: ERROR: Cannot get spare devices for %s.\n", geo->dev_name);
11317 i = -1;
11318 goto abort;
11319 }
11320
11321 /* we have got spares
11322 * update disk list in imsm_disk list table in anchor
11323 */
11324 dprintf("imsm: %i spares are available.\n\n",
11325 spares->array.spare_disks);
11326
11327 dev = spares->devs;
11328 for (i = 0; i < delta_disks; i++) {
11329 struct dl *dl;
11330
11331 if (dev == NULL)
11332 break;
11333 u->new_disks[i] = makedev(dev->disk.major,
11334 dev->disk.minor);
11335 dl = get_disk_super(super, dev->disk.major, dev->disk.minor);
11336 dl->index = mpb->num_disks;
11337 mpb->num_disks++;
11338 dev = dev->next;
11339 }
11340
11341 abort:
11342 /* free spares
11343 */
11344 sysfs_free(spares);
11345
11346 dprintf("imsm: reshape update preparation :");
11347 if (i == delta_disks) {
11348 dprintf_cont(" OK\n");
11349 *updatep = u;
11350 return update_memory_size;
11351 }
11352 free(u);
11353 dprintf_cont(" Error\n");
11354
11355 return 0;
11356 }
11357
11358 /******************************************************************************
11359 * function: imsm_create_metadata_update_for_size_change()
11360 * Creates update for IMSM array for array size change.
11361 *
11362 ******************************************************************************/
11363 static int imsm_create_metadata_update_for_size_change(
11364 struct supertype *st,
11365 struct geo_params *geo,
11366 struct imsm_update_size_change **updatep)
11367 {
11368 struct intel_super *super = st->sb;
11369 int update_memory_size;
11370 struct imsm_update_size_change *u;
11371
11372 dprintf("(enter) New size = %llu\n", geo->size);
11373
11374 /* size of all update data without anchor */
11375 update_memory_size = sizeof(struct imsm_update_size_change);
11376
11377 u = xcalloc(1, update_memory_size);
11378 u->type = update_size_change;
11379 u->subdev = super->current_vol;
11380 u->new_size = geo->size;
11381
11382 dprintf("imsm: reshape update preparation : OK\n");
11383 *updatep = u;
11384
11385 return update_memory_size;
11386 }
11387
11388 /******************************************************************************
11389 * function: imsm_create_metadata_update_for_migration()
11390 * Creates update for IMSM array.
11391 *
11392 ******************************************************************************/
11393 static int imsm_create_metadata_update_for_migration(
11394 struct supertype *st,
11395 struct geo_params *geo,
11396 struct imsm_update_reshape_migration **updatep)
11397 {
11398 struct intel_super *super = st->sb;
11399 int update_memory_size;
11400 struct imsm_update_reshape_migration *u;
11401 struct imsm_dev *dev;
11402 int previous_level = -1;
11403
11404 dprintf("(enter) New Level = %i\n", geo->level);
11405
11406 /* size of all update data without anchor */
11407 update_memory_size = sizeof(struct imsm_update_reshape_migration);
11408
11409 u = xcalloc(1, update_memory_size);
11410 u->type = update_reshape_migration;
11411 u->subdev = super->current_vol;
11412 u->new_level = geo->level;
11413 u->new_layout = geo->layout;
11414 u->new_raid_disks = u->old_raid_disks = geo->raid_disks;
11415 u->new_disks[0] = -1;
11416 u->new_chunksize = -1;
11417
11418 dev = get_imsm_dev(super, u->subdev);
11419 if (dev) {
11420 struct imsm_map *map;
11421
11422 map = get_imsm_map(dev, MAP_0);
11423 if (map) {
11424 int current_chunk_size =
11425 __le16_to_cpu(map->blocks_per_strip) / 2;
11426
11427 if (geo->chunksize != current_chunk_size) {
11428 u->new_chunksize = geo->chunksize / 1024;
11429 dprintf("imsm: chunk size change from %i to %i\n",
11430 current_chunk_size, u->new_chunksize);
11431 }
11432 previous_level = map->raid_level;
11433 }
11434 }
11435 if (geo->level == 5 && previous_level == 0) {
11436 struct mdinfo *spares = NULL;
11437
11438 u->new_raid_disks++;
11439 spares = get_spares_for_grow(st);
11440 if (spares == NULL || spares->array.spare_disks < 1) {
11441 free(u);
11442 sysfs_free(spares);
11443 update_memory_size = 0;
11444 pr_err("cannot get spare device for requested migration\n");
11445 return 0;
11446 }
11447 sysfs_free(spares);
11448 }
11449 dprintf("imsm: reshape update preparation : OK\n");
11450 *updatep = u;
11451
11452 return update_memory_size;
11453 }
11454
11455 static void imsm_update_metadata_locally(struct supertype *st,
11456 void *buf, int len)
11457 {
11458 struct metadata_update mu;
11459
11460 mu.buf = buf;
11461 mu.len = len;
11462 mu.space = NULL;
11463 mu.space_list = NULL;
11464 mu.next = NULL;
11465 if (imsm_prepare_update(st, &mu))
11466 imsm_process_update(st, &mu);
11467
11468 while (mu.space_list) {
11469 void **space = mu.space_list;
11470 mu.space_list = *space;
11471 free(space);
11472 }
11473 }
11474
11475 /***************************************************************************
11476 * Function: imsm_analyze_change
11477 * Description: Function analyze change for single volume
11478 * and validate if transition is supported
11479 * Parameters: Geometry parameters, supertype structure,
11480 * metadata change direction (apply/rollback)
11481 * Returns: Operation type code on success, -1 if fail
11482 ****************************************************************************/
11483 enum imsm_reshape_type imsm_analyze_change(struct supertype *st,
11484 struct geo_params *geo,
11485 int direction)
11486 {
11487 struct mdinfo info;
11488 int change = -1;
11489 int check_devs = 0;
11490 int chunk;
11491 /* number of added/removed disks in operation result */
11492 int devNumChange = 0;
11493 /* imsm compatible layout value for array geometry verification */
11494 int imsm_layout = -1;
11495 int data_disks;
11496 struct imsm_dev *dev;
11497 struct imsm_map *map;
11498 struct intel_super *super;
11499 unsigned long long current_size;
11500 unsigned long long free_size;
11501 unsigned long long max_size;
11502 int rv;
11503
11504 getinfo_super_imsm_volume(st, &info, NULL);
11505 if (geo->level != info.array.level && geo->level >= 0 &&
11506 geo->level != UnSet) {
11507 switch (info.array.level) {
11508 case 0:
11509 if (geo->level == 5) {
11510 change = CH_MIGRATION;
11511 if (geo->layout != ALGORITHM_LEFT_ASYMMETRIC) {
11512 pr_err("Error. Requested Layout not supported (left-asymmetric layout is supported only)!\n");
11513 change = -1;
11514 goto analyse_change_exit;
11515 }
11516 imsm_layout = geo->layout;
11517 check_devs = 1;
11518 devNumChange = 1; /* parity disk added */
11519 } else if (geo->level == 10) {
11520 change = CH_TAKEOVER;
11521 check_devs = 1;
11522 devNumChange = 2; /* two mirrors added */
11523 imsm_layout = 0x102; /* imsm supported layout */
11524 }
11525 break;
11526 case 1:
11527 case 10:
11528 if (geo->level == 0) {
11529 change = CH_TAKEOVER;
11530 check_devs = 1;
11531 devNumChange = -(geo->raid_disks/2);
11532 imsm_layout = 0; /* imsm raid0 layout */
11533 }
11534 break;
11535 }
11536 if (change == -1) {
11537 pr_err("Error. Level Migration from %d to %d not supported!\n",
11538 info.array.level, geo->level);
11539 goto analyse_change_exit;
11540 }
11541 } else
11542 geo->level = info.array.level;
11543
11544 if (geo->layout != info.array.layout &&
11545 (geo->layout != UnSet && geo->layout != -1)) {
11546 change = CH_MIGRATION;
11547 if (info.array.layout == 0 && info.array.level == 5 &&
11548 geo->layout == 5) {
11549 /* reshape 5 -> 4 */
11550 } else if (info.array.layout == 5 && info.array.level == 5 &&
11551 geo->layout == 0) {
11552 /* reshape 4 -> 5 */
11553 geo->layout = 0;
11554 geo->level = 5;
11555 } else {
11556 pr_err("Error. Layout Migration from %d to %d not supported!\n",
11557 info.array.layout, geo->layout);
11558 change = -1;
11559 goto analyse_change_exit;
11560 }
11561 } else {
11562 geo->layout = info.array.layout;
11563 if (imsm_layout == -1)
11564 imsm_layout = info.array.layout;
11565 }
11566
11567 if (geo->chunksize > 0 && geo->chunksize != UnSet &&
11568 geo->chunksize != info.array.chunk_size) {
11569 if (info.array.level == 10) {
11570 pr_err("Error. Chunk size change for RAID 10 is not supported.\n");
11571 change = -1;
11572 goto analyse_change_exit;
11573 } else if (info.component_size % (geo->chunksize/512)) {
11574 pr_err("New chunk size (%dK) does not evenly divide device size (%lluk). Aborting...\n",
11575 geo->chunksize/1024, info.component_size/2);
11576 change = -1;
11577 goto analyse_change_exit;
11578 }
11579 change = CH_MIGRATION;
11580 } else {
11581 geo->chunksize = info.array.chunk_size;
11582 }
11583
11584 chunk = geo->chunksize / 1024;
11585
11586 super = st->sb;
11587 dev = get_imsm_dev(super, super->current_vol);
11588 map = get_imsm_map(dev, MAP_0);
11589 data_disks = imsm_num_data_members(map);
11590 /* compute current size per disk member
11591 */
11592 current_size = info.custom_array_size / data_disks;
11593
11594 if (geo->size > 0 && geo->size != MAX_SIZE) {
11595 /* align component size
11596 */
11597 geo->size = imsm_component_size_alignment_check(
11598 get_imsm_raid_level(dev->vol.map),
11599 chunk * 1024, super->sector_size,
11600 geo->size * 2);
11601 if (geo->size == 0) {
11602 pr_err("Error. Size expansion is supported only (current size is %llu, requested size /rounded/ is 0).\n",
11603 current_size);
11604 goto analyse_change_exit;
11605 }
11606 }
11607
11608 if (current_size != geo->size && geo->size > 0) {
11609 if (change != -1) {
11610 pr_err("Error. Size change should be the only one at a time.\n");
11611 change = -1;
11612 goto analyse_change_exit;
11613 }
11614 if ((super->current_vol + 1) != super->anchor->num_raid_devs) {
11615 pr_err("Error. The last volume in container can be expanded only (%i/%s).\n",
11616 super->current_vol, st->devnm);
11617 goto analyse_change_exit;
11618 }
11619 /* check the maximum available size
11620 */
11621 rv = imsm_get_free_size(st, dev->vol.map->num_members,
11622 0, chunk, &free_size);
11623 if (rv == 0)
11624 /* Cannot find maximum available space
11625 */
11626 max_size = 0;
11627 else {
11628 max_size = free_size + current_size;
11629 /* align component size
11630 */
11631 max_size = imsm_component_size_alignment_check(
11632 get_imsm_raid_level(dev->vol.map),
11633 chunk * 1024, super->sector_size,
11634 max_size);
11635 }
11636 if (geo->size == MAX_SIZE) {
11637 /* requested size change to the maximum available size
11638 */
11639 if (max_size == 0) {
11640 pr_err("Error. Cannot find maximum available space.\n");
11641 change = -1;
11642 goto analyse_change_exit;
11643 } else
11644 geo->size = max_size;
11645 }
11646
11647 if (direction == ROLLBACK_METADATA_CHANGES) {
11648 /* accept size for rollback only
11649 */
11650 } else {
11651 /* round size due to metadata compatibility
11652 */
11653 geo->size = (geo->size >> SECT_PER_MB_SHIFT)
11654 << SECT_PER_MB_SHIFT;
11655 dprintf("Prepare update for size change to %llu\n",
11656 geo->size );
11657 if (current_size >= geo->size) {
11658 pr_err("Error. Size expansion is supported only (current size is %llu, requested size /rounded/ is %llu).\n",
11659 current_size, geo->size);
11660 goto analyse_change_exit;
11661 }
11662 if (max_size && geo->size > max_size) {
11663 pr_err("Error. Requested size is larger than maximum available size (maximum available size is %llu, requested size /rounded/ is %llu).\n",
11664 max_size, geo->size);
11665 goto analyse_change_exit;
11666 }
11667 }
11668 geo->size *= data_disks;
11669 geo->raid_disks = dev->vol.map->num_members;
11670 change = CH_ARRAY_SIZE;
11671 }
11672 if (!validate_geometry_imsm(st,
11673 geo->level,
11674 imsm_layout,
11675 geo->raid_disks + devNumChange,
11676 &chunk,
11677 geo->size, INVALID_SECTORS,
11678 0, 0, info.consistency_policy, 1))
11679 change = -1;
11680
11681 if (check_devs) {
11682 struct intel_super *super = st->sb;
11683 struct imsm_super *mpb = super->anchor;
11684
11685 if (mpb->num_raid_devs > 1) {
11686 pr_err("Error. Cannot perform operation on %s- for this operation "
11687 "it MUST be single array in container\n", geo->dev_name);
11688 change = -1;
11689 }
11690 }
11691
11692 analyse_change_exit:
11693 if (direction == ROLLBACK_METADATA_CHANGES &&
11694 (change == CH_MIGRATION || change == CH_TAKEOVER)) {
11695 dprintf("imsm: Metadata changes rollback is not supported for migration and takeover operations.\n");
11696 change = -1;
11697 }
11698 return change;
11699 }
11700
11701 int imsm_takeover(struct supertype *st, struct geo_params *geo)
11702 {
11703 struct intel_super *super = st->sb;
11704 struct imsm_update_takeover *u;
11705
11706 u = xmalloc(sizeof(struct imsm_update_takeover));
11707
11708 u->type = update_takeover;
11709 u->subarray = super->current_vol;
11710
11711 /* 10->0 transition */
11712 if (geo->level == 0)
11713 u->direction = R10_TO_R0;
11714
11715 /* 0->10 transition */
11716 if (geo->level == 10)
11717 u->direction = R0_TO_R10;
11718
11719 /* update metadata locally */
11720 imsm_update_metadata_locally(st, u,
11721 sizeof(struct imsm_update_takeover));
11722 /* and possibly remotely */
11723 if (st->update_tail)
11724 append_metadata_update(st, u,
11725 sizeof(struct imsm_update_takeover));
11726 else
11727 free(u);
11728
11729 return 0;
11730 }
11731
11732 /* Flush size update if size calculated by num_data_stripes is higher than
11733 * imsm_dev_size to eliminate differences during reshape.
11734 * Mdmon will recalculate them correctly.
11735 * If subarray index is not set then check whole container.
11736 * Returns:
11737 * 0 - no error occurred
11738 * 1 - error detected
11739 */
11740 static int imsm_fix_size_mismatch(struct supertype *st, int subarray_index)
11741 {
11742 struct intel_super *super = st->sb;
11743 int tmp = super->current_vol;
11744 int ret_val = 1;
11745 int i;
11746
11747 for (i = 0; i < super->anchor->num_raid_devs; i++) {
11748 if (subarray_index >= 0 && i != subarray_index)
11749 continue;
11750 super->current_vol = i;
11751 struct imsm_dev *dev = get_imsm_dev(super, super->current_vol);
11752 struct imsm_map *map = get_imsm_map(dev, MAP_0);
11753 unsigned int disc_count = imsm_num_data_members(map);
11754 struct geo_params geo;
11755 struct imsm_update_size_change *update;
11756 unsigned long long calc_size = per_dev_array_size(map) * disc_count;
11757 unsigned long long d_size = imsm_dev_size(dev);
11758 int u_size;
11759
11760 if (calc_size == d_size || dev->vol.migr_type == MIGR_GEN_MIGR)
11761 continue;
11762
11763 /* There is a difference, confirm that imsm_dev_size is
11764 * smaller and push update.
11765 */
11766 if (d_size > calc_size) {
11767 pr_err("imsm: dev size of subarray %d is incorrect\n",
11768 i);
11769 goto exit;
11770 }
11771 memset(&geo, 0, sizeof(struct geo_params));
11772 geo.size = d_size;
11773 u_size = imsm_create_metadata_update_for_size_change(st, &geo,
11774 &update);
11775 if (u_size < 1) {
11776 dprintf("imsm: Cannot prepare size change update\n");
11777 goto exit;
11778 }
11779 imsm_update_metadata_locally(st, update, u_size);
11780 if (st->update_tail) {
11781 append_metadata_update(st, update, u_size);
11782 flush_metadata_updates(st);
11783 st->update_tail = &st->updates;
11784 } else {
11785 imsm_sync_metadata(st);
11786 free(update);
11787 }
11788 }
11789 ret_val = 0;
11790 exit:
11791 super->current_vol = tmp;
11792 return ret_val;
11793 }
11794
11795 static int imsm_reshape_super(struct supertype *st, unsigned long long size,
11796 int level,
11797 int layout, int chunksize, int raid_disks,
11798 int delta_disks, char *backup, char *dev,
11799 int direction, int verbose)
11800 {
11801 int ret_val = 1;
11802 struct geo_params geo;
11803
11804 dprintf("(enter)\n");
11805
11806 memset(&geo, 0, sizeof(struct geo_params));
11807
11808 geo.dev_name = dev;
11809 strcpy(geo.devnm, st->devnm);
11810 geo.size = size;
11811 geo.level = level;
11812 geo.layout = layout;
11813 geo.chunksize = chunksize;
11814 geo.raid_disks = raid_disks;
11815 if (delta_disks != UnSet)
11816 geo.raid_disks += delta_disks;
11817
11818 dprintf("for level : %i\n", geo.level);
11819 dprintf("for raid_disks : %i\n", geo.raid_disks);
11820
11821 if (strcmp(st->container_devnm, st->devnm) == 0) {
11822 /* On container level we can only increase number of devices. */
11823 dprintf("imsm: info: Container operation\n");
11824 int old_raid_disks = 0;
11825
11826 if (imsm_reshape_is_allowed_on_container(
11827 st, &geo, &old_raid_disks, direction)) {
11828 struct imsm_update_reshape *u = NULL;
11829 int len;
11830
11831 if (imsm_fix_size_mismatch(st, -1)) {
11832 dprintf("imsm: Cannot fix size mismatch\n");
11833 goto exit_imsm_reshape_super;
11834 }
11835
11836 len = imsm_create_metadata_update_for_reshape(
11837 st, &geo, old_raid_disks, &u);
11838
11839 if (len <= 0) {
11840 dprintf("imsm: Cannot prepare update\n");
11841 goto exit_imsm_reshape_super;
11842 }
11843
11844 ret_val = 0;
11845 /* update metadata locally */
11846 imsm_update_metadata_locally(st, u, len);
11847 /* and possibly remotely */
11848 if (st->update_tail)
11849 append_metadata_update(st, u, len);
11850 else
11851 free(u);
11852
11853 } else {
11854 pr_err("(imsm) Operation is not allowed on this container\n");
11855 }
11856 } else {
11857 /* On volume level we support following operations
11858 * - takeover: raid10 -> raid0; raid0 -> raid10
11859 * - chunk size migration
11860 * - migration: raid5 -> raid0; raid0 -> raid5
11861 */
11862 struct intel_super *super = st->sb;
11863 struct intel_dev *dev = super->devlist;
11864 int change;
11865 dprintf("imsm: info: Volume operation\n");
11866 /* find requested device */
11867 while (dev) {
11868 char *devnm =
11869 imsm_find_array_devnm_by_subdev(
11870 dev->index, st->container_devnm);
11871 if (devnm && strcmp(devnm, geo.devnm) == 0)
11872 break;
11873 dev = dev->next;
11874 }
11875 if (dev == NULL) {
11876 pr_err("Cannot find %s (%s) subarray\n",
11877 geo.dev_name, geo.devnm);
11878 goto exit_imsm_reshape_super;
11879 }
11880 super->current_vol = dev->index;
11881 change = imsm_analyze_change(st, &geo, direction);
11882 switch (change) {
11883 case CH_TAKEOVER:
11884 ret_val = imsm_takeover(st, &geo);
11885 break;
11886 case CH_MIGRATION: {
11887 struct imsm_update_reshape_migration *u = NULL;
11888 int len =
11889 imsm_create_metadata_update_for_migration(
11890 st, &geo, &u);
11891 if (len < 1) {
11892 dprintf("imsm: Cannot prepare update\n");
11893 break;
11894 }
11895 ret_val = 0;
11896 /* update metadata locally */
11897 imsm_update_metadata_locally(st, u, len);
11898 /* and possibly remotely */
11899 if (st->update_tail)
11900 append_metadata_update(st, u, len);
11901 else
11902 free(u);
11903 }
11904 break;
11905 case CH_ARRAY_SIZE: {
11906 struct imsm_update_size_change *u = NULL;
11907 int len =
11908 imsm_create_metadata_update_for_size_change(
11909 st, &geo, &u);
11910 if (len < 1) {
11911 dprintf("imsm: Cannot prepare update\n");
11912 break;
11913 }
11914 ret_val = 0;
11915 /* update metadata locally */
11916 imsm_update_metadata_locally(st, u, len);
11917 /* and possibly remotely */
11918 if (st->update_tail)
11919 append_metadata_update(st, u, len);
11920 else
11921 free(u);
11922 }
11923 break;
11924 default:
11925 ret_val = 1;
11926 }
11927 }
11928
11929 exit_imsm_reshape_super:
11930 dprintf("imsm: reshape_super Exit code = %i\n", ret_val);
11931 return ret_val;
11932 }
11933
11934 #define COMPLETED_OK 0
11935 #define COMPLETED_NONE 1
11936 #define COMPLETED_DELAYED 2
11937
11938 static int read_completed(int fd, unsigned long long *val)
11939 {
11940 int ret;
11941 char buf[50];
11942
11943 ret = sysfs_fd_get_str(fd, buf, 50);
11944 if (ret < 0)
11945 return ret;
11946
11947 ret = COMPLETED_OK;
11948 if (strncmp(buf, "none", 4) == 0) {
11949 ret = COMPLETED_NONE;
11950 } else if (strncmp(buf, "delayed", 7) == 0) {
11951 ret = COMPLETED_DELAYED;
11952 } else {
11953 char *ep;
11954 *val = strtoull(buf, &ep, 0);
11955 if (ep == buf || (*ep != 0 && *ep != '\n' && *ep != ' '))
11956 ret = -1;
11957 }
11958 return ret;
11959 }
11960
11961 /*******************************************************************************
11962 * Function: wait_for_reshape_imsm
11963 * Description: Function writes new sync_max value and waits until
11964 * reshape process reach new position
11965 * Parameters:
11966 * sra : general array info
11967 * ndata : number of disks in new array's layout
11968 * Returns:
11969 * 0 : success,
11970 * 1 : there is no reshape in progress,
11971 * -1 : fail
11972 ******************************************************************************/
11973 int wait_for_reshape_imsm(struct mdinfo *sra, int ndata)
11974 {
11975 int fd = sysfs_get_fd(sra, NULL, "sync_completed");
11976 int retry = 3;
11977 unsigned long long completed;
11978 /* to_complete : new sync_max position */
11979 unsigned long long to_complete = sra->reshape_progress;
11980 unsigned long long position_to_set = to_complete / ndata;
11981
11982 if (!is_fd_valid(fd)) {
11983 dprintf("cannot open reshape_position\n");
11984 return 1;
11985 }
11986
11987 do {
11988 if (sysfs_fd_get_ll(fd, &completed) < 0) {
11989 if (!retry) {
11990 dprintf("cannot read reshape_position (no reshape in progres)\n");
11991 close(fd);
11992 return 1;
11993 }
11994 usleep(30000);
11995 } else
11996 break;
11997 } while (retry--);
11998
11999 if (completed > position_to_set) {
12000 dprintf("wrong next position to set %llu (%llu)\n",
12001 to_complete, position_to_set);
12002 close(fd);
12003 return -1;
12004 }
12005 dprintf("Position set: %llu\n", position_to_set);
12006 if (sysfs_set_num(sra, NULL, "sync_max",
12007 position_to_set) != 0) {
12008 dprintf("cannot set reshape position to %llu\n",
12009 position_to_set);
12010 close(fd);
12011 return -1;
12012 }
12013
12014 do {
12015 int rc;
12016 char action[20];
12017 int timeout = 3000;
12018
12019 sysfs_wait(fd, &timeout);
12020 if (sysfs_get_str(sra, NULL, "sync_action",
12021 action, 20) > 0 &&
12022 strncmp(action, "reshape", 7) != 0) {
12023 if (strncmp(action, "idle", 4) == 0)
12024 break;
12025 close(fd);
12026 return -1;
12027 }
12028
12029 rc = read_completed(fd, &completed);
12030 if (rc < 0) {
12031 dprintf("cannot read reshape_position (in loop)\n");
12032 close(fd);
12033 return 1;
12034 } else if (rc == COMPLETED_NONE)
12035 break;
12036 } while (completed < position_to_set);
12037
12038 close(fd);
12039 return 0;
12040 }
12041
12042 /*******************************************************************************
12043 * Function: check_degradation_change
12044 * Description: Check that array hasn't become failed.
12045 * Parameters:
12046 * info : for sysfs access
12047 * sources : source disks descriptors
12048 * degraded: previous degradation level
12049 * Returns:
12050 * degradation level
12051 ******************************************************************************/
12052 int check_degradation_change(struct mdinfo *info,
12053 int *sources,
12054 int degraded)
12055 {
12056 unsigned long long new_degraded;
12057 int rv;
12058
12059 rv = sysfs_get_ll(info, NULL, "degraded", &new_degraded);
12060 if (rv == -1 || (new_degraded != (unsigned long long)degraded)) {
12061 /* check each device to ensure it is still working */
12062 struct mdinfo *sd;
12063 new_degraded = 0;
12064 for (sd = info->devs ; sd ; sd = sd->next) {
12065 if (sd->disk.state & (1<<MD_DISK_FAULTY))
12066 continue;
12067 if (sd->disk.state & (1<<MD_DISK_SYNC)) {
12068 char sbuf[100];
12069 int raid_disk = sd->disk.raid_disk;
12070
12071 if (sysfs_get_str(info,
12072 sd, "state", sbuf, sizeof(sbuf)) < 0 ||
12073 strstr(sbuf, "faulty") ||
12074 strstr(sbuf, "in_sync") == NULL) {
12075 /* this device is dead */
12076 sd->disk.state = (1<<MD_DISK_FAULTY);
12077 if (raid_disk >= 0)
12078 close_fd(&sources[raid_disk]);
12079 new_degraded++;
12080 }
12081 }
12082 }
12083 }
12084
12085 return new_degraded;
12086 }
12087
12088 /*******************************************************************************
12089 * Function: imsm_manage_reshape
12090 * Description: Function finds array under reshape and it manages reshape
12091 * process. It creates stripes backups (if required) and sets
12092 * checkpoints.
12093 * Parameters:
12094 * afd : Backup handle (nattive) - not used
12095 * sra : general array info
12096 * reshape : reshape parameters - not used
12097 * st : supertype structure
12098 * blocks : size of critical section [blocks]
12099 * fds : table of source device descriptor
12100 * offsets : start of array (offest per devices)
12101 * dests : not used
12102 * destfd : table of destination device descriptor
12103 * destoffsets : table of destination offsets (per device)
12104 * Returns:
12105 * 1 : success, reshape is done
12106 * 0 : fail
12107 ******************************************************************************/
12108 static int imsm_manage_reshape(
12109 int afd, struct mdinfo *sra, struct reshape *reshape,
12110 struct supertype *st, unsigned long backup_blocks,
12111 int *fds, unsigned long long *offsets,
12112 int dests, int *destfd, unsigned long long *destoffsets)
12113 {
12114 int ret_val = 0;
12115 struct intel_super *super = st->sb;
12116 struct intel_dev *dv;
12117 unsigned int sector_size = super->sector_size;
12118 struct imsm_dev *dev = NULL;
12119 struct imsm_map *map_src, *map_dest;
12120 int migr_vol_qan = 0;
12121 int ndata, odata; /* [bytes] */
12122 int chunk; /* [bytes] */
12123 struct migr_record *migr_rec;
12124 char *buf = NULL;
12125 unsigned int buf_size; /* [bytes] */
12126 unsigned long long max_position; /* array size [bytes] */
12127 unsigned long long next_step; /* [blocks]/[bytes] */
12128 unsigned long long old_data_stripe_length;
12129 unsigned long long start_src; /* [bytes] */
12130 unsigned long long start; /* [bytes] */
12131 unsigned long long start_buf_shift; /* [bytes] */
12132 int degraded = 0;
12133 int source_layout = 0;
12134 int subarray_index = -1;
12135
12136 if (!sra)
12137 return ret_val;
12138
12139 if (!fds || !offsets)
12140 goto abort;
12141
12142 /* Find volume during the reshape */
12143 for (dv = super->devlist; dv; dv = dv->next) {
12144 if (dv->dev->vol.migr_type == MIGR_GEN_MIGR &&
12145 dv->dev->vol.migr_state == 1) {
12146 dev = dv->dev;
12147 migr_vol_qan++;
12148 subarray_index = dv->index;
12149 }
12150 }
12151 /* Only one volume can migrate at the same time */
12152 if (migr_vol_qan != 1) {
12153 pr_err("%s", migr_vol_qan ?
12154 "Number of migrating volumes greater than 1\n" :
12155 "There is no volume during migrationg\n");
12156 goto abort;
12157 }
12158
12159 map_dest = get_imsm_map(dev, MAP_0);
12160 map_src = get_imsm_map(dev, MAP_1);
12161 if (map_src == NULL)
12162 goto abort;
12163
12164 ndata = imsm_num_data_members(map_dest);
12165 odata = imsm_num_data_members(map_src);
12166
12167 chunk = __le16_to_cpu(map_src->blocks_per_strip) * 512;
12168 old_data_stripe_length = odata * chunk;
12169
12170 migr_rec = super->migr_rec;
12171
12172 /* initialize migration record for start condition */
12173 if (sra->reshape_progress == 0)
12174 init_migr_record_imsm(st, dev, sra);
12175 else {
12176 if (__le32_to_cpu(migr_rec->rec_status) != UNIT_SRC_NORMAL) {
12177 dprintf("imsm: cannot restart migration when data are present in copy area.\n");
12178 goto abort;
12179 }
12180 /* Save checkpoint to update migration record for current
12181 * reshape position (in md). It can be farther than current
12182 * reshape position in metadata.
12183 */
12184 if (save_checkpoint_imsm(st, sra, UNIT_SRC_NORMAL) == 1) {
12185 /* ignore error == 2, this can mean end of reshape here
12186 */
12187 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL, initial save)\n");
12188 goto abort;
12189 }
12190 }
12191
12192 /* size for data */
12193 buf_size = __le32_to_cpu(migr_rec->blocks_per_unit) * 512;
12194 /* extend buffer size for parity disk */
12195 buf_size += __le32_to_cpu(migr_rec->dest_depth_per_unit) * 512;
12196 /* add space for stripe alignment */
12197 buf_size += old_data_stripe_length;
12198 if (posix_memalign((void **)&buf, MAX_SECTOR_SIZE, buf_size)) {
12199 dprintf("imsm: Cannot allocate checkpoint buffer\n");
12200 goto abort;
12201 }
12202
12203 max_position = sra->component_size * ndata;
12204 source_layout = imsm_level_to_layout(map_src->raid_level);
12205
12206 while (current_migr_unit(migr_rec) <
12207 get_num_migr_units(migr_rec)) {
12208 /* current reshape position [blocks] */
12209 unsigned long long current_position =
12210 __le32_to_cpu(migr_rec->blocks_per_unit)
12211 * current_migr_unit(migr_rec);
12212 unsigned long long border;
12213
12214 /* Check that array hasn't become failed.
12215 */
12216 degraded = check_degradation_change(sra, fds, degraded);
12217 if (degraded > 1) {
12218 dprintf("imsm: Abort reshape due to degradation level (%i)\n", degraded);
12219 goto abort;
12220 }
12221
12222 next_step = __le32_to_cpu(migr_rec->blocks_per_unit);
12223
12224 if ((current_position + next_step) > max_position)
12225 next_step = max_position - current_position;
12226
12227 start = current_position * 512;
12228
12229 /* align reading start to old geometry */
12230 start_buf_shift = start % old_data_stripe_length;
12231 start_src = start - start_buf_shift;
12232
12233 border = (start_src / odata) - (start / ndata);
12234 border /= 512;
12235 if (border <= __le32_to_cpu(migr_rec->dest_depth_per_unit)) {
12236 /* save critical stripes to buf
12237 * start - start address of current unit
12238 * to backup [bytes]
12239 * start_src - start address of current unit
12240 * to backup alligned to source array
12241 * [bytes]
12242 */
12243 unsigned long long next_step_filler;
12244 unsigned long long copy_length = next_step * 512;
12245
12246 /* allign copy area length to stripe in old geometry */
12247 next_step_filler = ((copy_length + start_buf_shift)
12248 % old_data_stripe_length);
12249 if (next_step_filler)
12250 next_step_filler = (old_data_stripe_length
12251 - next_step_filler);
12252 dprintf("save_stripes() parameters: start = %llu,\tstart_src = %llu,\tnext_step*512 = %llu,\tstart_in_buf_shift = %llu,\tnext_step_filler = %llu\n",
12253 start, start_src, copy_length,
12254 start_buf_shift, next_step_filler);
12255
12256 if (save_stripes(fds, offsets, map_src->num_members,
12257 chunk, map_src->raid_level,
12258 source_layout, 0, NULL, start_src,
12259 copy_length +
12260 next_step_filler + start_buf_shift,
12261 buf)) {
12262 dprintf("imsm: Cannot save stripes to buffer\n");
12263 goto abort;
12264 }
12265 /* Convert data to destination format and store it
12266 * in backup general migration area
12267 */
12268 if (save_backup_imsm(st, dev, sra,
12269 buf + start_buf_shift, copy_length)) {
12270 dprintf("imsm: Cannot save stripes to target devices\n");
12271 goto abort;
12272 }
12273 if (save_checkpoint_imsm(st, sra,
12274 UNIT_SRC_IN_CP_AREA)) {
12275 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_IN_CP_AREA)\n");
12276 goto abort;
12277 }
12278 } else {
12279 /* set next step to use whole border area */
12280 border /= next_step;
12281 if (border > 1)
12282 next_step *= border;
12283 }
12284 /* When data backed up, checkpoint stored,
12285 * kick the kernel to reshape unit of data
12286 */
12287 next_step = next_step + sra->reshape_progress;
12288 /* limit next step to array max position */
12289 if (next_step > max_position)
12290 next_step = max_position;
12291 sysfs_set_num(sra, NULL, "suspend_lo", sra->reshape_progress);
12292 sysfs_set_num(sra, NULL, "suspend_hi", next_step);
12293 sra->reshape_progress = next_step;
12294
12295 /* wait until reshape finish */
12296 if (wait_for_reshape_imsm(sra, ndata)) {
12297 dprintf("wait_for_reshape_imsm returned error!\n");
12298 goto abort;
12299 }
12300 if (sigterm)
12301 goto abort;
12302
12303 if (save_checkpoint_imsm(st, sra, UNIT_SRC_NORMAL) == 1) {
12304 /* ignore error == 2, this can mean end of reshape here
12305 */
12306 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL)\n");
12307 goto abort;
12308 }
12309
12310 }
12311
12312 /* clear migr_rec on disks after successful migration */
12313 struct dl *d;
12314
12315 memset(super->migr_rec_buf, 0, MIGR_REC_BUF_SECTORS*MAX_SECTOR_SIZE);
12316 for (d = super->disks; d; d = d->next) {
12317 if (d->index < 0 || is_failed(&d->disk))
12318 continue;
12319 unsigned long long dsize;
12320
12321 get_dev_size(d->fd, NULL, &dsize);
12322 if (lseek64(d->fd, dsize - MIGR_REC_SECTOR_POSITION*sector_size,
12323 SEEK_SET) >= 0) {
12324 if ((unsigned int)write(d->fd, super->migr_rec_buf,
12325 MIGR_REC_BUF_SECTORS*sector_size) !=
12326 MIGR_REC_BUF_SECTORS*sector_size)
12327 perror("Write migr_rec failed");
12328 }
12329 }
12330
12331 /* return '1' if done */
12332 ret_val = 1;
12333
12334 /* After the reshape eliminate size mismatch in metadata.
12335 * Don't update md/component_size here, volume hasn't
12336 * to take whole space. It is allowed by kernel.
12337 * md/component_size will be set propoperly after next assembly.
12338 */
12339 imsm_fix_size_mismatch(st, subarray_index);
12340
12341 abort:
12342 free(buf);
12343 /* See Grow.c: abort_reshape() for further explanation */
12344 sysfs_set_num(sra, NULL, "suspend_lo", 0x7FFFFFFFFFFFFFFFULL);
12345 sysfs_set_num(sra, NULL, "suspend_hi", 0);
12346 sysfs_set_num(sra, NULL, "suspend_lo", 0);
12347
12348 return ret_val;
12349 }
12350
12351 /*******************************************************************************
12352 * Function: calculate_bitmap_min_chunksize
12353 * Description: Calculates the minimal valid bitmap chunk size
12354 * Parameters:
12355 * max_bits : indicate how many bits can be used for the bitmap
12356 * data_area_size : the size of the data area covered by the bitmap
12357 *
12358 * Returns:
12359 * The bitmap chunk size
12360 ******************************************************************************/
12361 static unsigned long long
12362 calculate_bitmap_min_chunksize(unsigned long long max_bits,
12363 unsigned long long data_area_size)
12364 {
12365 unsigned long long min_chunk =
12366 4096; /* sub-page chunks don't work yet.. */
12367 unsigned long long bits = data_area_size / min_chunk + 1;
12368
12369 while (bits > max_bits) {
12370 min_chunk *= 2;
12371 bits = (bits + 1) / 2;
12372 }
12373 return min_chunk;
12374 }
12375
12376 /*******************************************************************************
12377 * Function: calculate_bitmap_chunksize
12378 * Description: Calculates the bitmap chunk size for the given device
12379 * Parameters:
12380 * st : supertype information
12381 * dev : device for the bitmap
12382 *
12383 * Returns:
12384 * The bitmap chunk size
12385 ******************************************************************************/
12386 static unsigned long long calculate_bitmap_chunksize(struct supertype *st,
12387 struct imsm_dev *dev)
12388 {
12389 struct intel_super *super = st->sb;
12390 unsigned long long min_chunksize;
12391 unsigned long long result = IMSM_DEFAULT_BITMAP_CHUNKSIZE;
12392 size_t dev_size = imsm_dev_size(dev);
12393
12394 min_chunksize = calculate_bitmap_min_chunksize(
12395 IMSM_BITMAP_AREA_SIZE * super->sector_size, dev_size);
12396
12397 if (result < min_chunksize)
12398 result = min_chunksize;
12399
12400 return result;
12401 }
12402
12403 /*******************************************************************************
12404 * Function: init_bitmap_header
12405 * Description: Initialize the bitmap header structure
12406 * Parameters:
12407 * st : supertype information
12408 * bms : bitmap header struct to initialize
12409 * dev : device for the bitmap
12410 *
12411 * Returns:
12412 * 0 : success
12413 * -1 : fail
12414 ******************************************************************************/
12415 static int init_bitmap_header(struct supertype *st, struct bitmap_super_s *bms,
12416 struct imsm_dev *dev)
12417 {
12418 int vol_uuid[4];
12419
12420 if (!bms || !dev)
12421 return -1;
12422
12423 bms->magic = __cpu_to_le32(BITMAP_MAGIC);
12424 bms->version = __cpu_to_le32(BITMAP_MAJOR_HI);
12425 bms->daemon_sleep = __cpu_to_le32(IMSM_DEFAULT_BITMAP_DAEMON_SLEEP);
12426 bms->sync_size = __cpu_to_le64(IMSM_BITMAP_AREA_SIZE);
12427 bms->write_behind = __cpu_to_le32(0);
12428
12429 uuid_from_super_imsm(st, vol_uuid);
12430 memcpy(bms->uuid, vol_uuid, 16);
12431
12432 bms->chunksize = calculate_bitmap_chunksize(st, dev);
12433
12434 return 0;
12435 }
12436
12437 /*******************************************************************************
12438 * Function: validate_internal_bitmap_for_drive
12439 * Description: Verify if the bitmap header for a given drive.
12440 * Parameters:
12441 * st : supertype information
12442 * offset : The offset from the beginning of the drive where to look for
12443 * the bitmap header.
12444 * d : the drive info
12445 *
12446 * Returns:
12447 * 0 : success
12448 * -1 : fail
12449 ******************************************************************************/
12450 static int validate_internal_bitmap_for_drive(struct supertype *st,
12451 unsigned long long offset,
12452 struct dl *d)
12453 {
12454 struct intel_super *super = st->sb;
12455 int ret = -1;
12456 int vol_uuid[4];
12457 bitmap_super_t *bms;
12458 int fd;
12459
12460 if (!d)
12461 return -1;
12462
12463 void *read_buf;
12464
12465 if (posix_memalign(&read_buf, MAX_SECTOR_SIZE, IMSM_BITMAP_HEADER_SIZE))
12466 return -1;
12467
12468 fd = d->fd;
12469 if (!is_fd_valid(fd)) {
12470 fd = open(d->devname, O_RDONLY, 0);
12471
12472 if (!is_fd_valid(fd)) {
12473 dprintf("cannot open the device %s\n", d->devname);
12474 goto abort;
12475 }
12476 }
12477
12478 if (lseek64(fd, offset * super->sector_size, SEEK_SET) < 0)
12479 goto abort;
12480 if (read(fd, read_buf, IMSM_BITMAP_HEADER_SIZE) !=
12481 IMSM_BITMAP_HEADER_SIZE)
12482 goto abort;
12483
12484 uuid_from_super_imsm(st, vol_uuid);
12485
12486 bms = read_buf;
12487 if ((bms->magic != __cpu_to_le32(BITMAP_MAGIC)) ||
12488 (bms->version != __cpu_to_le32(BITMAP_MAJOR_HI)) ||
12489 (!same_uuid((int *)bms->uuid, vol_uuid, st->ss->swapuuid))) {
12490 dprintf("wrong bitmap header detected\n");
12491 goto abort;
12492 }
12493
12494 ret = 0;
12495 abort:
12496 if (!is_fd_valid(d->fd))
12497 close_fd(&fd);
12498
12499 if (read_buf)
12500 free(read_buf);
12501
12502 return ret;
12503 }
12504
12505 /*******************************************************************************
12506 * Function: validate_internal_bitmap_imsm
12507 * Description: Verify if the bitmap header is in place and with proper data.
12508 * Parameters:
12509 * st : supertype information
12510 *
12511 * Returns:
12512 * 0 : success or device w/o RWH_BITMAP
12513 * -1 : fail
12514 ******************************************************************************/
12515 static int validate_internal_bitmap_imsm(struct supertype *st)
12516 {
12517 struct intel_super *super = st->sb;
12518 struct imsm_dev *dev = get_imsm_dev(super, super->current_vol);
12519 unsigned long long offset;
12520 struct dl *d;
12521
12522 if (!dev)
12523 return -1;
12524
12525 if (dev->rwh_policy != RWH_BITMAP)
12526 return 0;
12527
12528 offset = get_bitmap_header_sector(super, super->current_vol);
12529 for (d = super->disks; d; d = d->next) {
12530 if (d->index < 0 || is_failed(&d->disk))
12531 continue;
12532
12533 if (validate_internal_bitmap_for_drive(st, offset, d)) {
12534 pr_err("imsm: bitmap validation failed\n");
12535 return -1;
12536 }
12537 }
12538 return 0;
12539 }
12540
12541 /*******************************************************************************
12542 * Function: add_internal_bitmap_imsm
12543 * Description: Mark the volume to use the bitmap and updates the chunk size value.
12544 * Parameters:
12545 * st : supertype information
12546 * chunkp : bitmap chunk size
12547 * delay : not used for imsm
12548 * write_behind : not used for imsm
12549 * size : not used for imsm
12550 * may_change : not used for imsm
12551 * amajor : not used for imsm
12552 *
12553 * Returns:
12554 * 0 : success
12555 * -1 : fail
12556 ******************************************************************************/
12557 static int add_internal_bitmap_imsm(struct supertype *st, int *chunkp,
12558 int delay, int write_behind,
12559 unsigned long long size, int may_change,
12560 int amajor)
12561 {
12562 struct intel_super *super = st->sb;
12563 int vol_idx = super->current_vol;
12564 struct imsm_dev *dev;
12565
12566 if (!super->devlist || vol_idx == -1 || !chunkp)
12567 return -1;
12568
12569 dev = get_imsm_dev(super, vol_idx);
12570
12571 if (!dev) {
12572 dprintf("cannot find the device for volume index %d\n",
12573 vol_idx);
12574 return -1;
12575 }
12576 dev->rwh_policy = RWH_BITMAP;
12577
12578 *chunkp = calculate_bitmap_chunksize(st, dev);
12579
12580 return 0;
12581 }
12582
12583 /*******************************************************************************
12584 * Function: locate_bitmap_imsm
12585 * Description: Seek 'fd' to start of write-intent-bitmap.
12586 * Parameters:
12587 * st : supertype information
12588 * fd : file descriptor for the device
12589 * node_num : not used for imsm
12590 *
12591 * Returns:
12592 * 0 : success
12593 * -1 : fail
12594 ******************************************************************************/
12595 static int locate_bitmap_imsm(struct supertype *st, int fd, int node_num)
12596 {
12597 struct intel_super *super = st->sb;
12598 unsigned long long offset;
12599 int vol_idx = super->current_vol;
12600
12601 if (!super->devlist || vol_idx == -1)
12602 return -1;
12603
12604 offset = get_bitmap_header_sector(super, super->current_vol);
12605 dprintf("bitmap header offset is %llu\n", offset);
12606
12607 lseek64(fd, offset << 9, 0);
12608
12609 return 0;
12610 }
12611
12612 /*******************************************************************************
12613 * Function: write_init_bitmap_imsm
12614 * Description: Write a bitmap header and prepares the area for the bitmap.
12615 * Parameters:
12616 * st : supertype information
12617 * fd : file descriptor for the device
12618 * update : not used for imsm
12619 *
12620 * Returns:
12621 * 0 : success
12622 * -1 : fail
12623 ******************************************************************************/
12624 static int write_init_bitmap_imsm(struct supertype *st, int fd,
12625 enum bitmap_update update)
12626 {
12627 struct intel_super *super = st->sb;
12628 int vol_idx = super->current_vol;
12629 int ret = 0;
12630 unsigned long long offset;
12631 bitmap_super_t bms = { 0 };
12632 size_t written = 0;
12633 size_t to_write;
12634 ssize_t rv_num;
12635 void *buf;
12636
12637 if (!super->devlist || !super->sector_size || vol_idx == -1)
12638 return -1;
12639
12640 struct imsm_dev *dev = get_imsm_dev(super, vol_idx);
12641
12642 /* first clear the space for bitmap header */
12643 unsigned long long bitmap_area_start =
12644 get_bitmap_header_sector(super, vol_idx);
12645
12646 dprintf("zeroing area start (%llu) and size (%u)\n", bitmap_area_start,
12647 IMSM_BITMAP_AND_HEADER_SIZE / super->sector_size);
12648 if (zero_disk_range(fd, bitmap_area_start,
12649 IMSM_BITMAP_HEADER_SIZE / super->sector_size)) {
12650 pr_err("imsm: cannot zeroing the space for the bitmap\n");
12651 return -1;
12652 }
12653
12654 /* The bitmap area should be filled with "1"s to perform initial
12655 * synchronization.
12656 */
12657 if (posix_memalign(&buf, MAX_SECTOR_SIZE, MAX_SECTOR_SIZE))
12658 return -1;
12659 memset(buf, 0xFF, MAX_SECTOR_SIZE);
12660 offset = get_bitmap_sector(super, vol_idx);
12661 lseek64(fd, offset << 9, 0);
12662 while (written < IMSM_BITMAP_AREA_SIZE) {
12663 to_write = IMSM_BITMAP_AREA_SIZE - written;
12664 if (to_write > MAX_SECTOR_SIZE)
12665 to_write = MAX_SECTOR_SIZE;
12666 rv_num = write(fd, buf, MAX_SECTOR_SIZE);
12667 if (rv_num != MAX_SECTOR_SIZE) {
12668 ret = -1;
12669 dprintf("cannot initialize bitmap area\n");
12670 goto abort;
12671 }
12672 written += rv_num;
12673 }
12674
12675 /* write a bitmap header */
12676 init_bitmap_header(st, &bms, dev);
12677 memset(buf, 0, MAX_SECTOR_SIZE);
12678 memcpy(buf, &bms, sizeof(bitmap_super_t));
12679 if (locate_bitmap_imsm(st, fd, 0)) {
12680 ret = -1;
12681 dprintf("cannot locate the bitmap\n");
12682 goto abort;
12683 }
12684 if (write(fd, buf, MAX_SECTOR_SIZE) != MAX_SECTOR_SIZE) {
12685 ret = -1;
12686 dprintf("cannot write the bitmap header\n");
12687 goto abort;
12688 }
12689 fsync(fd);
12690
12691 abort:
12692 free(buf);
12693
12694 return ret;
12695 }
12696
12697 /*******************************************************************************
12698 * Function: is_vol_to_setup_bitmap
12699 * Description: Checks if a bitmap should be activated on the dev.
12700 * Parameters:
12701 * info : info about the volume to setup the bitmap
12702 * dev : the device to check against bitmap creation
12703 *
12704 * Returns:
12705 * 0 : bitmap should be set up on the device
12706 * -1 : otherwise
12707 ******************************************************************************/
12708 static int is_vol_to_setup_bitmap(struct mdinfo *info, struct imsm_dev *dev)
12709 {
12710 if (!dev || !info)
12711 return -1;
12712
12713 if ((strcmp((char *)dev->volume, info->name) == 0) &&
12714 (dev->rwh_policy == RWH_BITMAP))
12715 return -1;
12716
12717 return 0;
12718 }
12719
12720 /*******************************************************************************
12721 * Function: set_bitmap_sysfs
12722 * Description: Set the sysfs atributes of a given volume to activate the bitmap.
12723 * Parameters:
12724 * info : info about the volume where the bitmap should be setup
12725 * chunksize : bitmap chunk size
12726 * location : location of the bitmap
12727 *
12728 * Returns:
12729 * 0 : success
12730 * -1 : fail
12731 ******************************************************************************/
12732 static int set_bitmap_sysfs(struct mdinfo *info, unsigned long long chunksize,
12733 char *location)
12734 {
12735 /* The bitmap/metadata is set to external to allow changing of value for
12736 * bitmap/location. When external is used, the kernel will treat an offset
12737 * related to the device's first lba (in opposition to the "internal" case
12738 * when this value is related to the beginning of the superblock).
12739 */
12740 if (sysfs_set_str(info, NULL, "bitmap/metadata", "external")) {
12741 dprintf("failed to set bitmap/metadata\n");
12742 return -1;
12743 }
12744
12745 /* It can only be changed when no bitmap is active.
12746 * Should be bigger than 512 and must be power of 2.
12747 * It is expecting the value in bytes.
12748 */
12749 if (sysfs_set_num(info, NULL, "bitmap/chunksize",
12750 __cpu_to_le32(chunksize))) {
12751 dprintf("failed to set bitmap/chunksize\n");
12752 return -1;
12753 }
12754
12755 /* It is expecting the value in sectors. */
12756 if (sysfs_set_num(info, NULL, "bitmap/space",
12757 __cpu_to_le64(IMSM_BITMAP_AREA_SIZE))) {
12758 dprintf("failed to set bitmap/space\n");
12759 return -1;
12760 }
12761
12762 /* Determines the delay between the bitmap updates.
12763 * It is expecting the value in seconds.
12764 */
12765 if (sysfs_set_num(info, NULL, "bitmap/time_base",
12766 __cpu_to_le64(IMSM_DEFAULT_BITMAP_DAEMON_SLEEP))) {
12767 dprintf("failed to set bitmap/time_base\n");
12768 return -1;
12769 }
12770
12771 /* It is expecting the value in sectors with a sign at the beginning. */
12772 if (sysfs_set_str(info, NULL, "bitmap/location", location)) {
12773 dprintf("failed to set bitmap/location\n");
12774 return -1;
12775 }
12776
12777 return 0;
12778 }
12779
12780 /*******************************************************************************
12781 * Function: set_bitmap_imsm
12782 * Description: Setup the bitmap for the given volume
12783 * Parameters:
12784 * st : supertype information
12785 * info : info about the volume where the bitmap should be setup
12786 *
12787 * Returns:
12788 * 0 : success
12789 * -1 : fail
12790 ******************************************************************************/
12791 static int set_bitmap_imsm(struct supertype *st, struct mdinfo *info)
12792 {
12793 struct intel_super *super = st->sb;
12794 int prev_current_vol = super->current_vol;
12795 struct imsm_dev *dev;
12796 int ret = -1;
12797 char location[16] = "";
12798 unsigned long long chunksize;
12799 struct intel_dev *dev_it;
12800
12801 for (dev_it = super->devlist; dev_it; dev_it = dev_it->next) {
12802 super->current_vol = dev_it->index;
12803 dev = get_imsm_dev(super, super->current_vol);
12804
12805 if (is_vol_to_setup_bitmap(info, dev)) {
12806 if (validate_internal_bitmap_imsm(st)) {
12807 dprintf("bitmap header validation failed\n");
12808 goto abort;
12809 }
12810
12811 chunksize = calculate_bitmap_chunksize(st, dev);
12812 dprintf("chunk size is %llu\n", chunksize);
12813
12814 snprintf(location, sizeof(location), "+%llu",
12815 get_bitmap_sector(super, super->current_vol));
12816 dprintf("bitmap offset is %s\n", location);
12817
12818 if (set_bitmap_sysfs(info, chunksize, location)) {
12819 dprintf("cannot setup the bitmap\n");
12820 goto abort;
12821 }
12822 }
12823 }
12824 ret = 0;
12825 abort:
12826 super->current_vol = prev_current_vol;
12827 return ret;
12828 }
12829
12830 struct superswitch super_imsm = {
12831 .examine_super = examine_super_imsm,
12832 .brief_examine_super = brief_examine_super_imsm,
12833 .brief_examine_subarrays = brief_examine_subarrays_imsm,
12834 .export_examine_super = export_examine_super_imsm,
12835 .detail_super = detail_super_imsm,
12836 .brief_detail_super = brief_detail_super_imsm,
12837 .write_init_super = write_init_super_imsm,
12838 .validate_geometry = validate_geometry_imsm,
12839 .add_to_super = add_to_super_imsm,
12840 .remove_from_super = remove_from_super_imsm,
12841 .detail_platform = detail_platform_imsm,
12842 .export_detail_platform = export_detail_platform_imsm,
12843 .kill_subarray = kill_subarray_imsm,
12844 .update_subarray = update_subarray_imsm,
12845 .load_container = load_container_imsm,
12846 .default_geometry = default_geometry_imsm,
12847 .get_disk_controller_domain = imsm_get_disk_controller_domain,
12848 .reshape_super = imsm_reshape_super,
12849 .manage_reshape = imsm_manage_reshape,
12850 .recover_backup = recover_backup_imsm,
12851 .examine_badblocks = examine_badblocks_imsm,
12852 .match_home = match_home_imsm,
12853 .uuid_from_super= uuid_from_super_imsm,
12854 .getinfo_super = getinfo_super_imsm,
12855 .getinfo_super_disks = getinfo_super_disks_imsm,
12856 .update_super = update_super_imsm,
12857
12858 .avail_size = avail_size_imsm,
12859 .get_spare_criteria = get_spare_criteria_imsm,
12860
12861 .compare_super = compare_super_imsm,
12862
12863 .load_super = load_super_imsm,
12864 .init_super = init_super_imsm,
12865 .store_super = store_super_imsm,
12866 .free_super = free_super_imsm,
12867 .match_metadata_desc = match_metadata_desc_imsm,
12868 .container_content = container_content_imsm,
12869 .validate_container = validate_container_imsm,
12870
12871 .add_internal_bitmap = add_internal_bitmap_imsm,
12872 .locate_bitmap = locate_bitmap_imsm,
12873 .write_bitmap = write_init_bitmap_imsm,
12874 .set_bitmap = set_bitmap_imsm,
12875
12876 .write_init_ppl = write_init_ppl_imsm,
12877 .validate_ppl = validate_ppl_imsm,
12878
12879 .external = 1,
12880 .name = "imsm",
12881
12882 /* for mdmon */
12883 .open_new = imsm_open_new,
12884 .set_array_state= imsm_set_array_state,
12885 .set_disk = imsm_set_disk,
12886 .sync_metadata = imsm_sync_metadata,
12887 .activate_spare = imsm_activate_spare,
12888 .process_update = imsm_process_update,
12889 .prepare_update = imsm_prepare_update,
12890 .record_bad_block = imsm_record_badblock,
12891 .clear_bad_block = imsm_clear_badblock,
12892 .get_bad_blocks = imsm_get_badblocks,
12893 };