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