]> git.ipfire.org Git - thirdparty/mdadm.git/blob - super-intel.c
tests/23rdev-lifetime: fix a typo
[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 "dlink.h"
24 #include "sha1.h"
25 #include "platform-intel.h"
26 #include <values.h>
27 #include <scsi/sg.h>
28 #include <ctype.h>
29 #include <dirent.h>
30 #include "drive_encryption.h"
31
32 /* MPB == Metadata Parameter Block */
33 #define MPB_SIGNATURE "Intel Raid ISM Cfg Sig. "
34 #define MPB_SIG_LEN (strlen(MPB_SIGNATURE))
35
36 /* Legacy IMSM versions:
37 * MPB_VERSION_RAID0 1.0.00
38 * MPB_VERSION_RAID1 1.1.00
39 * MPB_VERSION_MANY_VOLUMES_PER_ARRAY 1.2.00
40 * MPB_VERSION_3OR4_DISK_ARRAY 1.2.01
41 * MPB_VERSION_RAID5 1.2.02
42 * MPB_VERSION_5OR6_DISK_ARRAY 1.2.04
43 * MPB_VERSION_CNG 1.2.06
44 */
45
46 #define MPB_VERSION_ATTRIBS "1.3.00"
47 #define MPB_VERSION_ATTRIBS_JD "2.0.00"
48 #define MAX_SIGNATURE_LENGTH 32
49 #define MAX_RAID_SERIAL_LEN 16
50
51 /* supports RAID0 */
52 #define MPB_ATTRIB_RAID0 __cpu_to_le32(0x00000001)
53 /* supports RAID1 */
54 #define MPB_ATTRIB_RAID1 __cpu_to_le32(0x00000002)
55 /* supports RAID10 */
56 #define MPB_ATTRIB_RAID10 __cpu_to_le32(0x00000004)
57 /* supports RAID1E */
58 #define MPB_ATTRIB_RAID1E __cpu_to_le32(0x00000008)
59 /* supports RAID5 */
60 #define MPB_ATTRIB_RAID5 __cpu_to_le32(0x00000010)
61 /* supports RAID CNG */
62 #define MPB_ATTRIB_RAIDCNG __cpu_to_le32(0x00000020)
63 /* supports expanded stripe sizes of 256K, 512K and 1MB */
64 #define MPB_ATTRIB_EXP_STRIPE_SIZE __cpu_to_le32(0x00000040)
65 /* supports RAID10 with more than 4 drives */
66 #define MPB_ATTRIB_RAID10_EXT __cpu_to_le32(0x00000080)
67
68 /* The OROM Support RST Caching of Volumes */
69 #define MPB_ATTRIB_NVM __cpu_to_le32(0x02000000)
70 /* The OROM supports creating disks greater than 2TB */
71 #define MPB_ATTRIB_2TB_DISK __cpu_to_le32(0x04000000)
72 /* The OROM supports Bad Block Management */
73 #define MPB_ATTRIB_BBM __cpu_to_le32(0x08000000)
74
75 /* THe OROM Supports NVM Caching of Volumes */
76 #define MPB_ATTRIB_NEVER_USE2 __cpu_to_le32(0x10000000)
77 /* The OROM supports creating volumes greater than 2TB */
78 #define MPB_ATTRIB_2TB __cpu_to_le32(0x20000000)
79 /* originally for PMP, now it's wasted b/c. Never use this bit! */
80 #define MPB_ATTRIB_NEVER_USE __cpu_to_le32(0x40000000)
81 /* Verify MPB contents against checksum after reading MPB */
82 #define MPB_ATTRIB_CHECKSUM_VERIFY __cpu_to_le32(0x80000000)
83
84 /* Define all supported attributes that have to be accepted by mdadm
85 */
86 #define MPB_ATTRIB_SUPPORTED (MPB_ATTRIB_CHECKSUM_VERIFY | \
87 MPB_ATTRIB_2TB | \
88 MPB_ATTRIB_2TB_DISK | \
89 MPB_ATTRIB_RAID0 | \
90 MPB_ATTRIB_RAID1 | \
91 MPB_ATTRIB_RAID10 | \
92 MPB_ATTRIB_RAID5 | \
93 MPB_ATTRIB_EXP_STRIPE_SIZE | \
94 MPB_ATTRIB_RAID10_EXT | \
95 MPB_ATTRIB_BBM)
96
97 /* Define attributes that are unused but not harmful */
98 #define MPB_ATTRIB_IGNORED (MPB_ATTRIB_NEVER_USE)
99
100 #define MPB_SECTOR_CNT 2210
101 #define IMSM_RESERVED_SECTORS 8192
102 #define NUM_BLOCKS_DIRTY_STRIPE_REGION 2048
103 #define SECT_PER_MB_SHIFT 11
104 #define MAX_SECTOR_SIZE 4096
105 #define MULTIPLE_PPL_AREA_SIZE_IMSM (1024 * 1024) /* Size of the whole
106 * mutliple PPL area
107 */
108
109 /*
110 * Internal Write-intent bitmap is stored in the same area where PPL.
111 * Both features are mutually exclusive, so it is not an issue.
112 * The first 8KiB of the area are reserved and shall not be used.
113 */
114 #define IMSM_BITMAP_AREA_RESERVED_SIZE 8192
115
116 #define IMSM_BITMAP_HEADER_OFFSET (IMSM_BITMAP_AREA_RESERVED_SIZE)
117 #define IMSM_BITMAP_HEADER_SIZE MAX_SECTOR_SIZE
118
119 #define IMSM_BITMAP_START_OFFSET (IMSM_BITMAP_HEADER_OFFSET + IMSM_BITMAP_HEADER_SIZE)
120 #define IMSM_BITMAP_AREA_SIZE (MULTIPLE_PPL_AREA_SIZE_IMSM - IMSM_BITMAP_START_OFFSET)
121 #define IMSM_BITMAP_AND_HEADER_SIZE (IMSM_BITMAP_AREA_SIZE + IMSM_BITMAP_HEADER_SIZE)
122
123 #define IMSM_DEFAULT_BITMAP_CHUNKSIZE (64 * 1024 * 1024)
124 #define IMSM_DEFAULT_BITMAP_DAEMON_SLEEP 5
125
126 /*
127 * This macro let's us ensure that no-one accidentally
128 * changes the size of a struct
129 */
130 #define ASSERT_SIZE(_struct, size) \
131 static inline void __assert_size_##_struct(void) \
132 { \
133 switch (0) { \
134 case 0: break; \
135 case (sizeof(struct _struct) == size): break; \
136 } \
137 }
138
139 /* Disk configuration info. */
140 #define IMSM_MAX_DEVICES 255
141 struct imsm_disk {
142 __u8 serial[MAX_RAID_SERIAL_LEN];/* 0xD8 - 0xE7 ascii serial number */
143 __u32 total_blocks_lo; /* 0xE8 - 0xEB total blocks lo */
144 __u32 scsi_id; /* 0xEC - 0xEF scsi ID */
145 #define SPARE_DISK __cpu_to_le32(0x01) /* Spare */
146 #define CONFIGURED_DISK __cpu_to_le32(0x02) /* Member of some RaidDev */
147 #define FAILED_DISK __cpu_to_le32(0x04) /* Permanent failure */
148 #define JOURNAL_DISK __cpu_to_le32(0x2000000) /* Device marked as Journaling Drive */
149 __u32 status; /* 0xF0 - 0xF3 */
150 __u32 owner_cfg_num; /* which config 0,1,2... owns this disk */
151 __u32 total_blocks_hi; /* 0xF4 - 0xF5 total blocks hi */
152 #define IMSM_DISK_FILLERS 3
153 __u32 filler[IMSM_DISK_FILLERS]; /* 0xF5 - 0x107 MPB_DISK_FILLERS for future expansion */
154 };
155 ASSERT_SIZE(imsm_disk, 48)
156
157 /* map selector for map managment
158 */
159 #define MAP_0 0
160 #define MAP_1 1
161 #define MAP_X -1
162
163 /* RAID map configuration infos. */
164 struct imsm_map {
165 __u32 pba_of_lba0_lo; /* start address of partition */
166 __u32 blocks_per_member_lo;/* blocks per member */
167 __u32 num_data_stripes_lo; /* number of data stripes */
168 __u16 blocks_per_strip;
169 __u8 map_state; /* Normal, Uninitialized, Degraded, Failed */
170 #define IMSM_T_STATE_NORMAL 0
171 #define IMSM_T_STATE_UNINITIALIZED 1
172 #define IMSM_T_STATE_DEGRADED 2
173 #define IMSM_T_STATE_FAILED 3
174 __u8 raid_level;
175 #define IMSM_T_RAID0 0
176 #define IMSM_T_RAID1 1
177 #define IMSM_T_RAID5 5
178 #define IMSM_T_RAID10 10
179 __u8 num_members; /* number of member disks */
180 __u8 num_domains; /* number of parity domains */
181 __u8 failed_disk_num; /* valid only when state is degraded */
182 __u8 ddf;
183 __u32 pba_of_lba0_hi;
184 __u32 blocks_per_member_hi;
185 __u32 num_data_stripes_hi;
186 __u32 filler[4]; /* expansion area */
187 #define IMSM_ORD_REBUILD (1 << 24)
188 __u32 disk_ord_tbl[1]; /* disk_ord_tbl[num_members],
189 * top byte contains some flags
190 */
191 };
192 ASSERT_SIZE(imsm_map, 52)
193
194 struct imsm_vol {
195 __u32 curr_migr_unit_lo;
196 __u32 checkpoint_id; /* id to access curr_migr_unit */
197 __u8 migr_state; /* Normal or Migrating */
198 #define MIGR_INIT 0
199 #define MIGR_REBUILD 1
200 #define MIGR_VERIFY 2 /* analagous to echo check > sync_action */
201 #define MIGR_GEN_MIGR 3
202 #define MIGR_STATE_CHANGE 4
203 #define MIGR_REPAIR 5
204 __u8 migr_type; /* Initializing, Rebuilding, ... */
205 #define RAIDVOL_CLEAN 0
206 #define RAIDVOL_DIRTY 1
207 #define RAIDVOL_DSRECORD_VALID 2
208 __u8 dirty;
209 __u8 fs_state; /* fast-sync state for CnG (0xff == disabled) */
210 __u16 verify_errors; /* number of mismatches */
211 __u16 bad_blocks; /* number of bad blocks during verify */
212 __u32 curr_migr_unit_hi;
213 __u32 filler[3];
214 struct imsm_map map[1];
215 /* here comes another one if migr_state */
216 };
217 ASSERT_SIZE(imsm_vol, 84)
218
219 struct imsm_dev {
220 __u8 volume[MAX_RAID_SERIAL_LEN];
221 __u32 size_low;
222 __u32 size_high;
223 #define DEV_BOOTABLE __cpu_to_le32(0x01)
224 #define DEV_BOOT_DEVICE __cpu_to_le32(0x02)
225 #define DEV_READ_COALESCING __cpu_to_le32(0x04)
226 #define DEV_WRITE_COALESCING __cpu_to_le32(0x08)
227 #define DEV_LAST_SHUTDOWN_DIRTY __cpu_to_le32(0x10)
228 #define DEV_HIDDEN_AT_BOOT __cpu_to_le32(0x20)
229 #define DEV_CURRENTLY_HIDDEN __cpu_to_le32(0x40)
230 #define DEV_VERIFY_AND_FIX __cpu_to_le32(0x80)
231 #define DEV_MAP_STATE_UNINIT __cpu_to_le32(0x100)
232 #define DEV_NO_AUTO_RECOVERY __cpu_to_le32(0x200)
233 #define DEV_CLONE_N_GO __cpu_to_le32(0x400)
234 #define DEV_CLONE_MAN_SYNC __cpu_to_le32(0x800)
235 #define DEV_CNG_MASTER_DISK_NUM __cpu_to_le32(0x1000)
236 __u32 status; /* Persistent RaidDev status */
237 __u32 reserved_blocks; /* Reserved blocks at beginning of volume */
238 __u8 migr_priority;
239 __u8 num_sub_vols;
240 __u8 tid;
241 __u8 cng_master_disk;
242 __u16 cache_policy;
243 __u8 cng_state;
244 __u8 cng_sub_state;
245 __u16 my_vol_raid_dev_num; /* Used in Unique volume Id for this RaidDev */
246
247 /* NVM_EN */
248 __u8 nv_cache_mode;
249 __u8 nv_cache_flags;
250
251 /* Unique Volume Id of the NvCache Volume associated with this volume */
252 __u32 nvc_vol_orig_family_num;
253 __u16 nvc_vol_raid_dev_num;
254
255 #define RWH_OFF 0
256 #define RWH_DISTRIBUTED 1
257 #define RWH_JOURNALING_DRIVE 2
258 #define RWH_MULTIPLE_DISTRIBUTED 3
259 #define RWH_MULTIPLE_PPLS_JOURNALING_DRIVE 4
260 #define RWH_MULTIPLE_OFF 5
261 #define RWH_BITMAP 6
262 __u8 rwh_policy; /* Raid Write Hole Policy */
263 __u8 jd_serial[MAX_RAID_SERIAL_LEN]; /* Journal Drive serial number */
264 __u8 filler1;
265
266 #define IMSM_DEV_FILLERS 3
267 __u32 filler[IMSM_DEV_FILLERS];
268 struct imsm_vol vol;
269 };
270 ASSERT_SIZE(imsm_dev, 164)
271
272 struct imsm_super {
273 __u8 sig[MAX_SIGNATURE_LENGTH]; /* 0x00 - 0x1F */
274 __u32 check_sum; /* 0x20 - 0x23 MPB Checksum */
275 __u32 mpb_size; /* 0x24 - 0x27 Size of MPB */
276 __u32 family_num; /* 0x28 - 0x2B Checksum from first time this config was written */
277 __u32 generation_num; /* 0x2C - 0x2F Incremented each time this array's MPB is written */
278 __u32 error_log_size; /* 0x30 - 0x33 in bytes */
279 __u32 attributes; /* 0x34 - 0x37 */
280 __u8 num_disks; /* 0x38 Number of configured disks */
281 __u8 num_raid_devs; /* 0x39 Number of configured volumes */
282 __u8 error_log_pos; /* 0x3A */
283 __u8 fill[1]; /* 0x3B */
284 __u32 cache_size; /* 0x3c - 0x40 in mb */
285 __u32 orig_family_num; /* 0x40 - 0x43 original family num */
286 __u32 pwr_cycle_count; /* 0x44 - 0x47 simulated power cycle count for array */
287 __u32 bbm_log_size; /* 0x48 - 0x4B - size of bad Block Mgmt Log in bytes */
288 __u16 num_raid_devs_created; /* 0x4C - 0x4D Used for generating unique
289 * volume IDs for raid_dev created in this array
290 * (starts at 1)
291 */
292 __u16 filler1; /* 0x4E - 0x4F */
293 __u64 creation_time; /* 0x50 - 0x57 Array creation time */
294 #define IMSM_FILLERS 32
295 __u32 filler[IMSM_FILLERS]; /* 0x58 - 0xD7 RAID_MPB_FILLERS */
296 struct imsm_disk disk[1]; /* 0xD8 diskTbl[numDisks] */
297 /* here comes imsm_dev[num_raid_devs] */
298 /* here comes BBM logs */
299 };
300 ASSERT_SIZE(imsm_super, 264)
301
302 #define BBM_LOG_MAX_ENTRIES 254
303 #define BBM_LOG_MAX_LBA_ENTRY_VAL 256 /* Represents 256 LBAs */
304 #define BBM_LOG_SIGNATURE 0xabadb10c
305
306 struct bbm_log_block_addr {
307 __u16 w1;
308 __u32 dw1;
309 } __attribute__ ((__packed__));
310
311 struct bbm_log_entry {
312 __u8 marked_count; /* Number of blocks marked - 1 */
313 __u8 disk_ordinal; /* Disk entry within the imsm_super */
314 struct bbm_log_block_addr defective_block_start;
315 } __attribute__ ((__packed__));
316
317 struct bbm_log {
318 __u32 signature; /* 0xABADB10C */
319 __u32 entry_count;
320 struct bbm_log_entry marked_block_entries[BBM_LOG_MAX_ENTRIES];
321 };
322 ASSERT_SIZE(bbm_log, 2040)
323
324 static char *map_state_str[] = { "normal", "uninitialized", "degraded", "failed" };
325
326 #define BLOCKS_PER_KB (1024/512)
327
328 #define RAID_DISK_RESERVED_BLOCKS_IMSM_HI 2209
329
330 #define GEN_MIGR_AREA_SIZE 2048 /* General Migration Copy Area size in blocks */
331
332 #define MIGR_REC_BUF_SECTORS 1 /* size of migr_record i/o buffer in sectors */
333 #define MIGR_REC_SECTOR_POSITION 1 /* migr_record position offset on disk,
334 * MIGR_REC_BUF_SECTORS <= MIGR_REC_SECTOR_POS
335 */
336
337 #define UNIT_SRC_NORMAL 0 /* Source data for curr_migr_unit must
338 * be recovered using srcMap */
339 #define UNIT_SRC_IN_CP_AREA 1 /* Source data for curr_migr_unit has
340 * already been migrated and must
341 * be recovered from checkpoint area */
342
343 #define PPL_ENTRY_SPACE (128 * 1024) /* Size of single PPL, without the header */
344
345 struct migr_record {
346 __u32 rec_status; /* Status used to determine how to restart
347 * migration in case it aborts
348 * in some fashion */
349 __u32 curr_migr_unit_lo; /* 0..numMigrUnits-1 */
350 __u32 family_num; /* Family number of MPB
351 * containing the RaidDev
352 * that is migrating */
353 __u32 ascending_migr; /* True if migrating in increasing
354 * order of lbas */
355 __u32 blocks_per_unit; /* Num disk blocks per unit of operation */
356 __u32 dest_depth_per_unit; /* Num member blocks each destMap
357 * member disk
358 * advances per unit-of-operation */
359 __u32 ckpt_area_pba_lo; /* Pba of first block of ckpt copy area */
360 __u32 dest_1st_member_lba_lo; /* First member lba on first
361 * stripe of destination */
362 __u32 num_migr_units_lo; /* Total num migration units-of-op */
363 __u32 post_migr_vol_cap; /* Size of volume after
364 * migration completes */
365 __u32 post_migr_vol_cap_hi; /* Expansion space for LBA64 */
366 __u32 ckpt_read_disk_num; /* Which member disk in destSubMap[0] the
367 * migration ckpt record was read from
368 * (for recovered migrations) */
369 __u32 curr_migr_unit_hi; /* 0..numMigrUnits-1 high order 32 bits */
370 __u32 ckpt_area_pba_hi; /* Pba of first block of ckpt copy area
371 * high order 32 bits */
372 __u32 dest_1st_member_lba_hi; /* First member lba on first stripe of
373 * destination - high order 32 bits */
374 __u32 num_migr_units_hi; /* Total num migration units-of-op
375 * high order 32 bits */
376 __u32 filler[16];
377 };
378 ASSERT_SIZE(migr_record, 128)
379
380 /**
381 * enum imsm_status - internal IMSM return values representation.
382 * @STATUS_OK: function succeeded.
383 * @STATUS_ERROR: General error ocurred (not specified).
384 *
385 * Typedefed to imsm_status_t.
386 */
387 typedef enum imsm_status {
388 IMSM_STATUS_ERROR = -1,
389 IMSM_STATUS_OK = 0,
390 } imsm_status_t;
391
392 struct md_list {
393 /* usage marker:
394 * 1: load metadata
395 * 2: metadata does not match
396 * 4: already checked
397 */
398 int used;
399 char *devname;
400 int found;
401 int container;
402 dev_t st_rdev;
403 struct md_list *next;
404 };
405
406 static __u8 migr_type(struct imsm_dev *dev)
407 {
408 if (dev->vol.migr_type == MIGR_VERIFY &&
409 dev->status & DEV_VERIFY_AND_FIX)
410 return MIGR_REPAIR;
411 else
412 return dev->vol.migr_type;
413 }
414
415 static void set_migr_type(struct imsm_dev *dev, __u8 migr_type)
416 {
417 /* for compatibility with older oroms convert MIGR_REPAIR, into
418 * MIGR_VERIFY w/ DEV_VERIFY_AND_FIX status
419 */
420 if (migr_type == MIGR_REPAIR) {
421 dev->vol.migr_type = MIGR_VERIFY;
422 dev->status |= DEV_VERIFY_AND_FIX;
423 } else {
424 dev->vol.migr_type = migr_type;
425 dev->status &= ~DEV_VERIFY_AND_FIX;
426 }
427 }
428
429 static unsigned int sector_count(__u32 bytes, unsigned int sector_size)
430 {
431 return ROUND_UP(bytes, sector_size) / sector_size;
432 }
433
434 static unsigned int mpb_sectors(struct imsm_super *mpb,
435 unsigned int sector_size)
436 {
437 return sector_count(__le32_to_cpu(mpb->mpb_size), sector_size);
438 }
439
440 struct intel_dev {
441 struct imsm_dev *dev;
442 struct intel_dev *next;
443 unsigned index;
444 };
445
446 struct intel_hba {
447 enum sys_dev_type type;
448 char *path;
449 char *pci_id;
450 struct intel_hba *next;
451 };
452
453 enum action {
454 DISK_REMOVE = 1,
455 DISK_ADD
456 };
457 /* internal representation of IMSM metadata */
458 struct intel_super {
459 union {
460 void *buf; /* O_DIRECT buffer for reading/writing metadata */
461 struct imsm_super *anchor; /* immovable parameters */
462 };
463 union {
464 void *migr_rec_buf; /* buffer for I/O operations */
465 struct migr_record *migr_rec; /* migration record */
466 };
467 int clean_migration_record_by_mdmon; /* when reshape is switched to next
468 array, it indicates that mdmon is allowed to clean migration
469 record */
470 size_t len; /* size of the 'buf' allocation */
471 size_t extra_space; /* extra space in 'buf' that is not used yet */
472 void *next_buf; /* for realloc'ing buf from the manager */
473 size_t next_len;
474 int updates_pending; /* count of pending updates for mdmon */
475 int current_vol; /* index of raid device undergoing creation */
476 unsigned long long create_offset; /* common start for 'current_vol' */
477 __u32 random; /* random data for seeding new family numbers */
478 struct intel_dev *devlist;
479 unsigned int sector_size; /* sector size of used member drives */
480 struct dl {
481 struct dl *next;
482 int index;
483 __u8 serial[MAX_RAID_SERIAL_LEN];
484 int major, minor;
485 char *devname;
486 struct imsm_disk disk;
487 int fd;
488 int extent_cnt;
489 struct extent *e; /* for determining freespace @ create */
490 int raiddisk; /* slot to fill in autolayout */
491 enum action action;
492 } *disks, *current_disk;
493 struct dl *disk_mgmt_list; /* list of disks to add/remove while mdmon
494 active */
495 struct dl *missing; /* disks removed while we weren't looking */
496 struct bbm_log *bbm_log;
497 struct intel_hba *hba; /* device path of the raid controller for this metadata */
498 const struct imsm_orom *orom; /* platform firmware support */
499 struct intel_super *next; /* (temp) list for disambiguating family_num */
500 struct md_bb bb; /* memory for get_bad_blocks call */
501 };
502
503 struct intel_disk {
504 struct imsm_disk disk;
505 #define IMSM_UNKNOWN_OWNER (-1)
506 int owner;
507 struct intel_disk *next;
508 };
509
510 /**
511 * struct extent - reserved space details.
512 * @start: start offset.
513 * @size: size of reservation, set to 0 for metadata reservation.
514 * @vol: index of the volume, meaningful if &size is set.
515 */
516 struct extent {
517 unsigned long long start, size;
518 int vol;
519 };
520
521 /* definitions of reshape process types */
522 enum imsm_reshape_type {
523 CH_TAKEOVER,
524 CH_MIGRATION,
525 CH_ARRAY_SIZE,
526 CH_ABORT
527 };
528
529 /* definition of messages passed to imsm_process_update */
530 enum imsm_update_type {
531 update_activate_spare,
532 update_create_array,
533 update_kill_array,
534 update_rename_array,
535 update_add_remove_disk,
536 update_reshape_container_disks,
537 update_reshape_migration,
538 update_takeover,
539 update_general_migration_checkpoint,
540 update_size_change,
541 update_prealloc_badblocks_mem,
542 update_rwh_policy,
543 };
544
545 struct imsm_update_activate_spare {
546 enum imsm_update_type type;
547 struct dl *dl;
548 int slot;
549 int array;
550 struct imsm_update_activate_spare *next;
551 };
552
553 struct geo_params {
554 char devnm[32];
555 char *dev_name;
556 unsigned long long size;
557 int level;
558 int layout;
559 int chunksize;
560 int raid_disks;
561 };
562
563 enum takeover_direction {
564 R10_TO_R0,
565 R0_TO_R10
566 };
567 struct imsm_update_takeover {
568 enum imsm_update_type type;
569 int subarray;
570 enum takeover_direction direction;
571 };
572
573 struct imsm_update_reshape {
574 enum imsm_update_type type;
575 int old_raid_disks;
576 int new_raid_disks;
577
578 int new_disks[1]; /* new_raid_disks - old_raid_disks makedev number */
579 };
580
581 struct imsm_update_reshape_migration {
582 enum imsm_update_type type;
583 int old_raid_disks;
584 int new_raid_disks;
585 /* fields for array migration changes
586 */
587 int subdev;
588 int new_level;
589 int new_layout;
590 int new_chunksize;
591
592 int new_disks[1]; /* new_raid_disks - old_raid_disks makedev number */
593 };
594
595 struct imsm_update_size_change {
596 enum imsm_update_type type;
597 int subdev;
598 long long new_size;
599 };
600
601 struct imsm_update_general_migration_checkpoint {
602 enum imsm_update_type type;
603 __u64 curr_migr_unit;
604 };
605
606 struct disk_info {
607 __u8 serial[MAX_RAID_SERIAL_LEN];
608 };
609
610 struct imsm_update_create_array {
611 enum imsm_update_type type;
612 int dev_idx;
613 struct imsm_dev dev;
614 };
615
616 struct imsm_update_kill_array {
617 enum imsm_update_type type;
618 int dev_idx;
619 };
620
621 struct imsm_update_rename_array {
622 enum imsm_update_type type;
623 __u8 name[MAX_RAID_SERIAL_LEN];
624 int dev_idx;
625 };
626
627 struct imsm_update_add_remove_disk {
628 enum imsm_update_type type;
629 };
630
631 struct imsm_update_prealloc_bb_mem {
632 enum imsm_update_type type;
633 };
634
635 struct imsm_update_rwh_policy {
636 enum imsm_update_type type;
637 int new_policy;
638 int dev_idx;
639 };
640
641 static const char *_sys_dev_type[] = {
642 [SYS_DEV_UNKNOWN] = "Unknown",
643 [SYS_DEV_SAS] = "SAS",
644 [SYS_DEV_SATA] = "SATA",
645 [SYS_DEV_NVME] = "NVMe",
646 [SYS_DEV_VMD] = "VMD",
647 [SYS_DEV_SATA_VMD] = "SATA VMD"
648 };
649
650 static int no_platform = -1;
651
652 static int check_no_platform(void)
653 {
654 static const char search[] = "mdadm.imsm.test=1";
655 FILE *fp;
656
657 if (no_platform >= 0)
658 return no_platform;
659
660 if (check_env("IMSM_NO_PLATFORM")) {
661 no_platform = 1;
662 return 1;
663 }
664 fp = fopen("/proc/cmdline", "r");
665 if (fp) {
666 char *l = conf_line(fp);
667 char *w = l;
668
669 if (l == NULL) {
670 fclose(fp);
671 return 0;
672 }
673
674 do {
675 if (strcmp(w, search) == 0)
676 no_platform = 1;
677 w = dl_next(w);
678 } while (w != l);
679 free_line(l);
680 fclose(fp);
681 if (no_platform >= 0)
682 return no_platform;
683 }
684 no_platform = 0;
685 return 0;
686 }
687
688 void imsm_set_no_platform(int v)
689 {
690 no_platform = v;
691 }
692
693 const char *get_sys_dev_type(enum sys_dev_type type)
694 {
695 if (type >= SYS_DEV_MAX)
696 type = SYS_DEV_UNKNOWN;
697
698 return _sys_dev_type[type];
699 }
700
701 static struct intel_hba * alloc_intel_hba(struct sys_dev *device)
702 {
703 struct intel_hba *result = xmalloc(sizeof(*result));
704
705 result->type = device->type;
706 result->path = xstrdup(device->path);
707 result->next = NULL;
708 if (result->path && (result->pci_id = strrchr(result->path, '/')) != NULL)
709 result->pci_id++;
710
711 return result;
712 }
713
714 static struct intel_hba * find_intel_hba(struct intel_hba *hba, struct sys_dev *device)
715 {
716 struct intel_hba *result;
717
718 for (result = hba; result; result = result->next) {
719 if (result->type == device->type && strcmp(result->path, device->path) == 0)
720 break;
721 }
722 return result;
723 }
724
725 static int attach_hba_to_super(struct intel_super *super, struct sys_dev *device)
726 {
727 struct intel_hba *hba;
728
729 /* check if disk attached to Intel HBA */
730 hba = find_intel_hba(super->hba, device);
731 if (hba != NULL)
732 return 1;
733 /* Check if HBA is already attached to super */
734 if (super->hba == NULL) {
735 super->hba = alloc_intel_hba(device);
736 return 1;
737 }
738
739 hba = super->hba;
740 /* Intel metadata allows for all disks attached to the same type HBA.
741 * Do not support HBA types mixing
742 */
743 if (device->type != hba->type)
744 return 2;
745
746 /* Multiple same type HBAs can be used if they share the same OROM */
747 const struct imsm_orom *device_orom = get_orom_by_device_id(device->dev_id);
748
749 if (device_orom != super->orom)
750 return 2;
751
752 while (hba->next)
753 hba = hba->next;
754
755 hba->next = alloc_intel_hba(device);
756 return 1;
757 }
758
759 static struct sys_dev* find_disk_attached_hba(int fd, const char *devname)
760 {
761 struct sys_dev *list, *elem;
762 char *disk_path;
763
764 if ((list = find_intel_devices()) == NULL)
765 return 0;
766
767 if (!is_fd_valid(fd))
768 disk_path = (char *) devname;
769 else
770 disk_path = diskfd_to_devpath(fd, 1, NULL);
771
772 if (!disk_path)
773 return 0;
774
775 for (elem = list; elem; elem = elem->next)
776 if (path_attached_to_hba(disk_path, elem->path))
777 break;
778
779 if (disk_path != devname)
780 free(disk_path);
781
782 return elem;
783 }
784
785 static int find_intel_hba_capability(int fd, struct intel_super *super,
786 char *devname);
787
788 static struct supertype *match_metadata_desc_imsm(char *arg)
789 {
790 struct supertype *st;
791
792 if (strcmp(arg, "imsm") != 0 &&
793 strcmp(arg, "default") != 0
794 )
795 return NULL;
796
797 st = xcalloc(1, sizeof(*st));
798 st->ss = &super_imsm;
799 st->max_devs = IMSM_MAX_DEVICES;
800 st->minor_version = 0;
801 st->sb = NULL;
802 return st;
803 }
804
805 static __u8 *get_imsm_version(struct imsm_super *mpb)
806 {
807 return &mpb->sig[MPB_SIG_LEN];
808 }
809
810 /* retrieve a disk directly from the anchor when the anchor is known to be
811 * up-to-date, currently only at load time
812 */
813 static struct imsm_disk *__get_imsm_disk(struct imsm_super *mpb, __u8 index)
814 {
815 if (index >= mpb->num_disks)
816 return NULL;
817 return &mpb->disk[index];
818 }
819
820 /* retrieve the disk description based on a index of the disk
821 * in the sub-array
822 */
823 static struct dl *get_imsm_dl_disk(struct intel_super *super, __u8 index)
824 {
825 struct dl *d;
826
827 for (d = super->disks; d; d = d->next)
828 if (d->index == index)
829 return d;
830
831 return NULL;
832 }
833 /* retrieve a disk from the parsed metadata */
834 static struct imsm_disk *get_imsm_disk(struct intel_super *super, __u8 index)
835 {
836 struct dl *dl;
837
838 dl = get_imsm_dl_disk(super, index);
839 if (dl)
840 return &dl->disk;
841
842 return NULL;
843 }
844
845 /* generate a checksum directly from the anchor when the anchor is known to be
846 * up-to-date, currently only at load or write_super after coalescing
847 */
848 static __u32 __gen_imsm_checksum(struct imsm_super *mpb)
849 {
850 __u32 end = mpb->mpb_size / sizeof(end);
851 __u32 *p = (__u32 *) mpb;
852 __u32 sum = 0;
853
854 while (end--) {
855 sum += __le32_to_cpu(*p);
856 p++;
857 }
858
859 return sum - __le32_to_cpu(mpb->check_sum);
860 }
861
862 static size_t sizeof_imsm_map(struct imsm_map *map)
863 {
864 return sizeof(struct imsm_map) + sizeof(__u32) * (map->num_members - 1);
865 }
866
867 struct imsm_map *get_imsm_map(struct imsm_dev *dev, int second_map)
868 {
869 /* A device can have 2 maps if it is in the middle of a migration.
870 * If second_map is:
871 * MAP_0 - we return the first map
872 * MAP_1 - we return the second map if it exists, else NULL
873 * MAP_X - we return the second map if it exists, else the first
874 */
875 struct imsm_map *map = &dev->vol.map[0];
876 struct imsm_map *map2 = NULL;
877
878 if (dev->vol.migr_state)
879 map2 = (void *)map + sizeof_imsm_map(map);
880
881 switch (second_map) {
882 case MAP_0:
883 break;
884 case MAP_1:
885 map = map2;
886 break;
887 case MAP_X:
888 if (map2)
889 map = map2;
890 break;
891 default:
892 map = NULL;
893 }
894 return map;
895
896 }
897
898 /* return the size of the device.
899 * migr_state increases the returned size if map[0] were to be duplicated
900 */
901 static size_t sizeof_imsm_dev(struct imsm_dev *dev, int migr_state)
902 {
903 size_t size = sizeof(*dev) - sizeof(struct imsm_map) +
904 sizeof_imsm_map(get_imsm_map(dev, MAP_0));
905
906 /* migrating means an additional map */
907 if (dev->vol.migr_state)
908 size += sizeof_imsm_map(get_imsm_map(dev, MAP_1));
909 else if (migr_state)
910 size += sizeof_imsm_map(get_imsm_map(dev, MAP_0));
911
912 return size;
913 }
914
915 /* retrieve disk serial number list from a metadata update */
916 static struct disk_info *get_disk_info(struct imsm_update_create_array *update)
917 {
918 void *u = update;
919 struct disk_info *inf;
920
921 inf = u + sizeof(*update) - sizeof(struct imsm_dev) +
922 sizeof_imsm_dev(&update->dev, 0);
923
924 return inf;
925 }
926
927 /**
928 * __get_imsm_dev() - Get device with index from imsm_super.
929 * @mpb: &imsm_super pointer, not NULL.
930 * @index: Device index.
931 *
932 * Function works as non-NULL, aborting in such a case,
933 * when NULL would be returned.
934 *
935 * Device index should be in range 0 up to num_raid_devs.
936 * Function assumes the index was already verified.
937 * Index must be valid, otherwise abort() is called.
938 *
939 * Return: Pointer to corresponding imsm_dev.
940 *
941 */
942 static struct imsm_dev *__get_imsm_dev(struct imsm_super *mpb, __u8 index)
943 {
944 int offset;
945 int i;
946 void *_mpb = mpb;
947
948 if (index >= mpb->num_raid_devs)
949 goto error;
950
951 /* devices start after all disks */
952 offset = ((void *) &mpb->disk[mpb->num_disks]) - _mpb;
953
954 for (i = 0; i <= index; i++, offset += sizeof_imsm_dev(_mpb + offset, 0))
955 if (i == index)
956 return _mpb + offset;
957 error:
958 pr_err("cannot find imsm_dev with index %u in imsm_super\n", index);
959 abort();
960 }
961
962 /**
963 * get_imsm_dev() - Get device with index from intel_super.
964 * @super: &intel_super pointer, not NULL.
965 * @index: Device index.
966 *
967 * Function works as non-NULL, aborting in such a case,
968 * when NULL would be returned.
969 *
970 * Device index should be in range 0 up to num_raid_devs.
971 * Function assumes the index was already verified.
972 * Index must be valid, otherwise abort() is called.
973 *
974 * Return: Pointer to corresponding imsm_dev.
975 *
976 */
977 static struct imsm_dev *get_imsm_dev(struct intel_super *super, __u8 index)
978 {
979 struct intel_dev *dv;
980
981 if (index >= super->anchor->num_raid_devs)
982 goto error;
983
984 for (dv = super->devlist; dv; dv = dv->next)
985 if (dv->index == index)
986 return dv->dev;
987 error:
988 pr_err("cannot find imsm_dev with index %u in intel_super\n", index);
989 abort();
990 }
991
992 static inline unsigned long long __le48_to_cpu(const struct bbm_log_block_addr
993 *addr)
994 {
995 return ((((__u64)__le32_to_cpu(addr->dw1)) << 16) |
996 __le16_to_cpu(addr->w1));
997 }
998
999 static inline struct bbm_log_block_addr __cpu_to_le48(unsigned long long sec)
1000 {
1001 struct bbm_log_block_addr addr;
1002
1003 addr.w1 = __cpu_to_le16((__u16)(sec & 0xffff));
1004 addr.dw1 = __cpu_to_le32((__u32)(sec >> 16) & 0xffffffff);
1005 return addr;
1006 }
1007
1008 /* get size of the bbm log */
1009 static __u32 get_imsm_bbm_log_size(struct bbm_log *log)
1010 {
1011 if (!log || log->entry_count == 0)
1012 return 0;
1013
1014 return sizeof(log->signature) +
1015 sizeof(log->entry_count) +
1016 log->entry_count * sizeof(struct bbm_log_entry);
1017 }
1018
1019 /* check if bad block is not partially stored in bbm log */
1020 static int is_stored_in_bbm(struct bbm_log *log, const __u8 idx, const unsigned
1021 long long sector, const int length, __u32 *pos)
1022 {
1023 __u32 i;
1024
1025 for (i = *pos; i < log->entry_count; i++) {
1026 struct bbm_log_entry *entry = &log->marked_block_entries[i];
1027 unsigned long long bb_start;
1028 unsigned long long bb_end;
1029
1030 bb_start = __le48_to_cpu(&entry->defective_block_start);
1031 bb_end = bb_start + (entry->marked_count + 1);
1032
1033 if ((entry->disk_ordinal == idx) && (bb_start >= sector) &&
1034 (bb_end <= sector + length)) {
1035 *pos = i;
1036 return 1;
1037 }
1038 }
1039 return 0;
1040 }
1041
1042 /* record new bad block in bbm log */
1043 static int record_new_badblock(struct bbm_log *log, const __u8 idx, unsigned
1044 long long sector, int length)
1045 {
1046 int new_bb = 0;
1047 __u32 pos = 0;
1048 struct bbm_log_entry *entry = NULL;
1049
1050 while (is_stored_in_bbm(log, idx, sector, length, &pos)) {
1051 struct bbm_log_entry *e = &log->marked_block_entries[pos];
1052
1053 if ((e->marked_count + 1 == BBM_LOG_MAX_LBA_ENTRY_VAL) &&
1054 (__le48_to_cpu(&e->defective_block_start) == sector)) {
1055 sector += BBM_LOG_MAX_LBA_ENTRY_VAL;
1056 length -= BBM_LOG_MAX_LBA_ENTRY_VAL;
1057 pos = pos + 1;
1058 continue;
1059 }
1060 entry = e;
1061 break;
1062 }
1063
1064 if (entry) {
1065 int cnt = (length <= BBM_LOG_MAX_LBA_ENTRY_VAL) ? length :
1066 BBM_LOG_MAX_LBA_ENTRY_VAL;
1067 entry->defective_block_start = __cpu_to_le48(sector);
1068 entry->marked_count = cnt - 1;
1069 if (cnt == length)
1070 return 1;
1071 sector += cnt;
1072 length -= cnt;
1073 }
1074
1075 new_bb = ROUND_UP(length, BBM_LOG_MAX_LBA_ENTRY_VAL) /
1076 BBM_LOG_MAX_LBA_ENTRY_VAL;
1077 if (log->entry_count + new_bb > BBM_LOG_MAX_ENTRIES)
1078 return 0;
1079
1080 while (length > 0) {
1081 int cnt = (length <= BBM_LOG_MAX_LBA_ENTRY_VAL) ? length :
1082 BBM_LOG_MAX_LBA_ENTRY_VAL;
1083 struct bbm_log_entry *entry =
1084 &log->marked_block_entries[log->entry_count];
1085
1086 entry->defective_block_start = __cpu_to_le48(sector);
1087 entry->marked_count = cnt - 1;
1088 entry->disk_ordinal = idx;
1089
1090 sector += cnt;
1091 length -= cnt;
1092
1093 log->entry_count++;
1094 }
1095
1096 return new_bb;
1097 }
1098
1099 /* clear all bad blocks for given disk */
1100 static void clear_disk_badblocks(struct bbm_log *log, const __u8 idx)
1101 {
1102 __u32 i = 0;
1103
1104 while (i < log->entry_count) {
1105 struct bbm_log_entry *entries = log->marked_block_entries;
1106
1107 if (entries[i].disk_ordinal == idx) {
1108 if (i < log->entry_count - 1)
1109 entries[i] = entries[log->entry_count - 1];
1110 log->entry_count--;
1111 } else {
1112 i++;
1113 }
1114 }
1115 }
1116
1117 /* clear given bad block */
1118 static int clear_badblock(struct bbm_log *log, const __u8 idx, const unsigned
1119 long long sector, const int length) {
1120 __u32 i = 0;
1121
1122 while (i < log->entry_count) {
1123 struct bbm_log_entry *entries = log->marked_block_entries;
1124
1125 if ((entries[i].disk_ordinal == idx) &&
1126 (__le48_to_cpu(&entries[i].defective_block_start) ==
1127 sector) && (entries[i].marked_count + 1 == length)) {
1128 if (i < log->entry_count - 1)
1129 entries[i] = entries[log->entry_count - 1];
1130 log->entry_count--;
1131 break;
1132 }
1133 i++;
1134 }
1135
1136 return 1;
1137 }
1138
1139 /* allocate and load BBM log from metadata */
1140 static int load_bbm_log(struct intel_super *super)
1141 {
1142 struct imsm_super *mpb = super->anchor;
1143 __u32 bbm_log_size = __le32_to_cpu(mpb->bbm_log_size);
1144
1145 super->bbm_log = xcalloc(1, sizeof(struct bbm_log));
1146 if (!super->bbm_log)
1147 return 1;
1148
1149 if (bbm_log_size) {
1150 struct bbm_log *log = (void *)mpb +
1151 __le32_to_cpu(mpb->mpb_size) - bbm_log_size;
1152
1153 __u32 entry_count;
1154
1155 if (bbm_log_size < sizeof(log->signature) +
1156 sizeof(log->entry_count))
1157 return 2;
1158
1159 entry_count = __le32_to_cpu(log->entry_count);
1160 if ((__le32_to_cpu(log->signature) != BBM_LOG_SIGNATURE) ||
1161 (entry_count > BBM_LOG_MAX_ENTRIES))
1162 return 3;
1163
1164 if (bbm_log_size !=
1165 sizeof(log->signature) + sizeof(log->entry_count) +
1166 entry_count * sizeof(struct bbm_log_entry))
1167 return 4;
1168
1169 memcpy(super->bbm_log, log, bbm_log_size);
1170 } else {
1171 super->bbm_log->signature = __cpu_to_le32(BBM_LOG_SIGNATURE);
1172 super->bbm_log->entry_count = 0;
1173 }
1174
1175 return 0;
1176 }
1177
1178 /* checks if bad block is within volume boundaries */
1179 static int is_bad_block_in_volume(const struct bbm_log_entry *entry,
1180 const unsigned long long start_sector,
1181 const unsigned long long size)
1182 {
1183 unsigned long long bb_start;
1184 unsigned long long bb_end;
1185
1186 bb_start = __le48_to_cpu(&entry->defective_block_start);
1187 bb_end = bb_start + (entry->marked_count + 1);
1188
1189 if (((bb_start >= start_sector) && (bb_start < start_sector + size)) ||
1190 ((bb_end >= start_sector) && (bb_end <= start_sector + size)))
1191 return 1;
1192
1193 return 0;
1194 }
1195
1196 /* get list of bad blocks on a drive for a volume */
1197 static void get_volume_badblocks(const struct bbm_log *log, const __u8 idx,
1198 const unsigned long long start_sector,
1199 const unsigned long long size,
1200 struct md_bb *bbs)
1201 {
1202 __u32 count = 0;
1203 __u32 i;
1204
1205 for (i = 0; i < log->entry_count; i++) {
1206 const struct bbm_log_entry *ent =
1207 &log->marked_block_entries[i];
1208 struct md_bb_entry *bb;
1209
1210 if ((ent->disk_ordinal == idx) &&
1211 is_bad_block_in_volume(ent, start_sector, size)) {
1212
1213 if (!bbs->entries) {
1214 bbs->entries = xmalloc(BBM_LOG_MAX_ENTRIES *
1215 sizeof(*bb));
1216 if (!bbs->entries)
1217 break;
1218 }
1219
1220 bb = &bbs->entries[count++];
1221 bb->sector = __le48_to_cpu(&ent->defective_block_start);
1222 bb->length = ent->marked_count + 1;
1223 }
1224 }
1225 bbs->count = count;
1226 }
1227
1228 /*
1229 * for second_map:
1230 * == MAP_0 get first map
1231 * == MAP_1 get second map
1232 * == MAP_X than get map according to the current migr_state
1233 */
1234 static __u32 get_imsm_ord_tbl_ent(struct imsm_dev *dev,
1235 int slot,
1236 int second_map)
1237 {
1238 struct imsm_map *map;
1239
1240 map = get_imsm_map(dev, second_map);
1241
1242 /* top byte identifies disk under rebuild */
1243 return __le32_to_cpu(map->disk_ord_tbl[slot]);
1244 }
1245
1246 #define ord_to_idx(ord) (((ord) << 8) >> 8)
1247 static __u32 get_imsm_disk_idx(struct imsm_dev *dev, int slot, int second_map)
1248 {
1249 __u32 ord = get_imsm_ord_tbl_ent(dev, slot, second_map);
1250
1251 return ord_to_idx(ord);
1252 }
1253
1254 static void set_imsm_ord_tbl_ent(struct imsm_map *map, int slot, __u32 ord)
1255 {
1256 map->disk_ord_tbl[slot] = __cpu_to_le32(ord);
1257 }
1258
1259 static int get_imsm_disk_slot(struct imsm_map *map, const unsigned int idx)
1260 {
1261 int slot;
1262 __u32 ord;
1263
1264 for (slot = 0; slot < map->num_members; slot++) {
1265 ord = __le32_to_cpu(map->disk_ord_tbl[slot]);
1266 if (ord_to_idx(ord) == idx)
1267 return slot;
1268 }
1269
1270 return IMSM_STATUS_ERROR;
1271 }
1272 /**
1273 * update_imsm_raid_level() - update raid level appropriately in &imsm_map.
1274 * @map: &imsm_map pointer.
1275 * @new_level: MD style level.
1276 *
1277 * For backward compatibility reasons we need to differentiate RAID10.
1278 * In the past IMSM RAID10 was presented as RAID1.
1279 * Keep compatibility unless it is not explicitly updated by UEFI driver.
1280 *
1281 * Routine needs num_members to be set and (optionally) raid_level.
1282 */
1283 static void update_imsm_raid_level(struct imsm_map *map, int new_level)
1284 {
1285 if (new_level != IMSM_T_RAID10) {
1286 map->raid_level = new_level;
1287 return;
1288 }
1289
1290 if (map->num_members == 4) {
1291 if (map->raid_level == IMSM_T_RAID10 || map->raid_level == IMSM_T_RAID1)
1292 return;
1293
1294 map->raid_level = IMSM_T_RAID1;
1295 return;
1296 }
1297
1298 map->raid_level = IMSM_T_RAID10;
1299 }
1300
1301 static int get_imsm_raid_level(struct imsm_map *map)
1302 {
1303 if (map->raid_level == IMSM_T_RAID1) {
1304 if (map->num_members == 2)
1305 return IMSM_T_RAID1;
1306 else
1307 return IMSM_T_RAID10;
1308 }
1309
1310 return map->raid_level;
1311 }
1312
1313 /**
1314 * get_disk_slot_in_dev() - retrieve disk slot from &imsm_dev.
1315 * @super: &intel_super pointer, not NULL.
1316 * @dev_idx: imsm device index.
1317 * @idx: disk index.
1318 *
1319 * Return: Slot on success, IMSM_STATUS_ERROR otherwise.
1320 */
1321 static int get_disk_slot_in_dev(struct intel_super *super, const __u8 dev_idx,
1322 const unsigned int idx)
1323 {
1324 struct imsm_dev *dev = get_imsm_dev(super, dev_idx);
1325 struct imsm_map *map = get_imsm_map(dev, MAP_0);
1326
1327 return get_imsm_disk_slot(map, idx);
1328 }
1329
1330 static int cmp_extent(const void *av, const void *bv)
1331 {
1332 const struct extent *a = av;
1333 const struct extent *b = bv;
1334 if (a->start < b->start)
1335 return -1;
1336 if (a->start > b->start)
1337 return 1;
1338 return 0;
1339 }
1340
1341 static int count_memberships(struct dl *dl, struct intel_super *super)
1342 {
1343 int memberships = 0;
1344 int i;
1345
1346 for (i = 0; i < super->anchor->num_raid_devs; i++)
1347 if (get_disk_slot_in_dev(super, i, dl->index) >= 0)
1348 memberships++;
1349
1350 return memberships;
1351 }
1352
1353 static __u32 imsm_min_reserved_sectors(struct intel_super *super);
1354
1355 static int split_ull(unsigned long long n, void *lo, void *hi)
1356 {
1357 if (lo == 0 || hi == 0)
1358 return 1;
1359 __put_unaligned32(__cpu_to_le32((__u32)n), lo);
1360 __put_unaligned32(__cpu_to_le32((n >> 32)), hi);
1361 return 0;
1362 }
1363
1364 static unsigned long long join_u32(__u32 lo, __u32 hi)
1365 {
1366 return (unsigned long long)__le32_to_cpu(lo) |
1367 (((unsigned long long)__le32_to_cpu(hi)) << 32);
1368 }
1369
1370 static unsigned long long total_blocks(struct imsm_disk *disk)
1371 {
1372 if (disk == NULL)
1373 return 0;
1374 return join_u32(disk->total_blocks_lo, disk->total_blocks_hi);
1375 }
1376
1377 /**
1378 * imsm_num_data_members() - get data drives count for an array.
1379 * @map: Map to analyze.
1380 *
1381 * num_data_members value represents minimal count of drives for level.
1382 * The name of the property could be misleading for RAID5 with asymmetric layout
1383 * because some data required to be calculated from parity.
1384 * The property is extracted from level and num_members value.
1385 *
1386 * Return: num_data_members value on success, zero otherwise.
1387 */
1388 static __u8 imsm_num_data_members(struct imsm_map *map)
1389 {
1390 switch (get_imsm_raid_level(map)) {
1391 case 0:
1392 return map->num_members;
1393 case 1:
1394 case 10:
1395 return map->num_members / 2;
1396 case 5:
1397 return map->num_members - 1;
1398 default:
1399 dprintf("unsupported raid level\n");
1400 return 0;
1401 }
1402 }
1403
1404 static unsigned long long pba_of_lba0(struct imsm_map *map)
1405 {
1406 if (map == NULL)
1407 return 0;
1408 return join_u32(map->pba_of_lba0_lo, map->pba_of_lba0_hi);
1409 }
1410
1411 static unsigned long long blocks_per_member(struct imsm_map *map)
1412 {
1413 if (map == NULL)
1414 return 0;
1415 return join_u32(map->blocks_per_member_lo, map->blocks_per_member_hi);
1416 }
1417
1418 static unsigned long long num_data_stripes(struct imsm_map *map)
1419 {
1420 if (map == NULL)
1421 return 0;
1422 return join_u32(map->num_data_stripes_lo, map->num_data_stripes_hi);
1423 }
1424
1425 static unsigned long long vol_curr_migr_unit(struct imsm_dev *dev)
1426 {
1427 if (dev == NULL)
1428 return 0;
1429
1430 return join_u32(dev->vol.curr_migr_unit_lo, dev->vol.curr_migr_unit_hi);
1431 }
1432
1433 static unsigned long long imsm_dev_size(struct imsm_dev *dev)
1434 {
1435 if (dev == NULL)
1436 return 0;
1437 return join_u32(dev->size_low, dev->size_high);
1438 }
1439
1440 static unsigned long long migr_chkp_area_pba(struct migr_record *migr_rec)
1441 {
1442 if (migr_rec == NULL)
1443 return 0;
1444 return join_u32(migr_rec->ckpt_area_pba_lo,
1445 migr_rec->ckpt_area_pba_hi);
1446 }
1447
1448 static unsigned long long current_migr_unit(struct migr_record *migr_rec)
1449 {
1450 if (migr_rec == NULL)
1451 return 0;
1452 return join_u32(migr_rec->curr_migr_unit_lo,
1453 migr_rec->curr_migr_unit_hi);
1454 }
1455
1456 static unsigned long long migr_dest_1st_member_lba(struct migr_record *migr_rec)
1457 {
1458 if (migr_rec == NULL)
1459 return 0;
1460 return join_u32(migr_rec->dest_1st_member_lba_lo,
1461 migr_rec->dest_1st_member_lba_hi);
1462 }
1463
1464 static unsigned long long get_num_migr_units(struct migr_record *migr_rec)
1465 {
1466 if (migr_rec == NULL)
1467 return 0;
1468 return join_u32(migr_rec->num_migr_units_lo,
1469 migr_rec->num_migr_units_hi);
1470 }
1471
1472 static void set_total_blocks(struct imsm_disk *disk, unsigned long long n)
1473 {
1474 split_ull(n, &disk->total_blocks_lo, &disk->total_blocks_hi);
1475 }
1476
1477 /**
1478 * set_num_domains() - Set number of domains for an array.
1479 * @map: Map to be updated.
1480 *
1481 * num_domains property represents copies count of each data drive, thus make
1482 * it meaningful only for RAID1 and RAID10. IMSM supports two domains for
1483 * raid1 and raid10.
1484 */
1485 static void set_num_domains(struct imsm_map *map)
1486 {
1487 int level = get_imsm_raid_level(map);
1488
1489 if (level == 1 || level == 10)
1490 map->num_domains = 2;
1491 else
1492 map->num_domains = 1;
1493 }
1494
1495 static void set_pba_of_lba0(struct imsm_map *map, unsigned long long n)
1496 {
1497 split_ull(n, &map->pba_of_lba0_lo, &map->pba_of_lba0_hi);
1498 }
1499
1500 static void set_blocks_per_member(struct imsm_map *map, unsigned long long n)
1501 {
1502 split_ull(n, &map->blocks_per_member_lo, &map->blocks_per_member_hi);
1503 }
1504
1505 static void set_num_data_stripes(struct imsm_map *map, unsigned long long n)
1506 {
1507 split_ull(n, &map->num_data_stripes_lo, &map->num_data_stripes_hi);
1508 }
1509
1510 /**
1511 * update_num_data_stripes() - Calculate and update num_data_stripes value.
1512 * @map: map to be updated.
1513 * @dev_size: size of volume.
1514 *
1515 * num_data_stripes value is addictionally divided by num_domains, therefore for
1516 * levels where num_domains is not 1, nds is a part of real value.
1517 */
1518 static void update_num_data_stripes(struct imsm_map *map,
1519 unsigned long long dev_size)
1520 {
1521 unsigned long long nds = dev_size / imsm_num_data_members(map);
1522
1523 nds /= map->num_domains;
1524 nds /= map->blocks_per_strip;
1525 set_num_data_stripes(map, nds);
1526 }
1527
1528 static void set_vol_curr_migr_unit(struct imsm_dev *dev, unsigned long long n)
1529 {
1530 if (dev == NULL)
1531 return;
1532
1533 split_ull(n, &dev->vol.curr_migr_unit_lo, &dev->vol.curr_migr_unit_hi);
1534 }
1535
1536 static void set_imsm_dev_size(struct imsm_dev *dev, unsigned long long n)
1537 {
1538 split_ull(n, &dev->size_low, &dev->size_high);
1539 }
1540
1541 static void set_migr_chkp_area_pba(struct migr_record *migr_rec,
1542 unsigned long long n)
1543 {
1544 split_ull(n, &migr_rec->ckpt_area_pba_lo, &migr_rec->ckpt_area_pba_hi);
1545 }
1546
1547 static void set_current_migr_unit(struct migr_record *migr_rec,
1548 unsigned long long n)
1549 {
1550 split_ull(n, &migr_rec->curr_migr_unit_lo,
1551 &migr_rec->curr_migr_unit_hi);
1552 }
1553
1554 static void set_migr_dest_1st_member_lba(struct migr_record *migr_rec,
1555 unsigned long long n)
1556 {
1557 split_ull(n, &migr_rec->dest_1st_member_lba_lo,
1558 &migr_rec->dest_1st_member_lba_hi);
1559 }
1560
1561 static void set_num_migr_units(struct migr_record *migr_rec,
1562 unsigned long long n)
1563 {
1564 split_ull(n, &migr_rec->num_migr_units_lo,
1565 &migr_rec->num_migr_units_hi);
1566 }
1567
1568 static unsigned long long per_dev_array_size(struct imsm_map *map)
1569 {
1570 unsigned long long array_size = 0;
1571
1572 if (map == NULL)
1573 return array_size;
1574
1575 array_size = num_data_stripes(map) * map->blocks_per_strip;
1576 if (get_imsm_raid_level(map) == 1 || get_imsm_raid_level(map) == 10)
1577 array_size *= 2;
1578
1579 return array_size;
1580 }
1581
1582 static struct extent *get_extents(struct intel_super *super, struct dl *dl,
1583 int get_minimal_reservation)
1584 {
1585 /* find a list of used extents on the given physical device */
1586 int memberships = count_memberships(dl, super);
1587 struct extent *rv = xcalloc(memberships + 1, sizeof(struct extent));
1588 struct extent *e = rv;
1589 int i;
1590 __u32 reservation;
1591
1592 /* trim the reserved area for spares, so they can join any array
1593 * regardless of whether the OROM has assigned sectors from the
1594 * IMSM_RESERVED_SECTORS region
1595 */
1596 if (dl->index == -1 || get_minimal_reservation)
1597 reservation = imsm_min_reserved_sectors(super);
1598 else
1599 reservation = MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS;
1600
1601 for (i = 0; i < super->anchor->num_raid_devs; i++) {
1602 struct imsm_dev *dev = get_imsm_dev(super, i);
1603 struct imsm_map *map = get_imsm_map(dev, MAP_0);
1604
1605 if (get_imsm_disk_slot(map, dl->index) >= 0) {
1606 e->start = pba_of_lba0(map);
1607 e->size = per_dev_array_size(map);
1608 e->vol = i;
1609 e++;
1610 }
1611 }
1612 qsort(rv, memberships, sizeof(*rv), cmp_extent);
1613
1614 /* determine the start of the metadata
1615 * when no raid devices are defined use the default
1616 * ...otherwise allow the metadata to truncate the value
1617 * as is the case with older versions of imsm
1618 */
1619 if (memberships) {
1620 struct extent *last = &rv[memberships - 1];
1621 unsigned long long remainder;
1622
1623 remainder = total_blocks(&dl->disk) - (last->start + last->size);
1624 /* round down to 1k block to satisfy precision of the kernel
1625 * 'size' interface
1626 */
1627 remainder &= ~1UL;
1628 /* make sure remainder is still sane */
1629 if (remainder < (unsigned)ROUND_UP(super->len, 512) >> 9)
1630 remainder = ROUND_UP(super->len, 512) >> 9;
1631 if (reservation > remainder)
1632 reservation = remainder;
1633 }
1634 e->start = total_blocks(&dl->disk) - reservation;
1635 e->size = 0;
1636 return rv;
1637 }
1638
1639 /* try to determine how much space is reserved for metadata from
1640 * the last get_extents() entry, otherwise fallback to the
1641 * default
1642 */
1643 static __u32 imsm_reserved_sectors(struct intel_super *super, struct dl *dl)
1644 {
1645 struct extent *e;
1646 int i;
1647 __u32 rv;
1648
1649 /* for spares just return a minimal reservation which will grow
1650 * once the spare is picked up by an array
1651 */
1652 if (dl->index == -1)
1653 return MPB_SECTOR_CNT;
1654
1655 e = get_extents(super, dl, 0);
1656 if (!e)
1657 return MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS;
1658
1659 /* scroll to last entry */
1660 for (i = 0; e[i].size; i++)
1661 continue;
1662
1663 rv = total_blocks(&dl->disk) - e[i].start;
1664
1665 free(e);
1666
1667 return rv;
1668 }
1669
1670 static int is_spare(struct imsm_disk *disk)
1671 {
1672 return (disk->status & SPARE_DISK) == SPARE_DISK;
1673 }
1674
1675 static int is_configured(struct imsm_disk *disk)
1676 {
1677 return (disk->status & CONFIGURED_DISK) == CONFIGURED_DISK;
1678 }
1679
1680 static int is_failed(struct imsm_disk *disk)
1681 {
1682 return (disk->status & FAILED_DISK) == FAILED_DISK;
1683 }
1684
1685 static int is_journal(struct imsm_disk *disk)
1686 {
1687 return (disk->status & JOURNAL_DISK) == JOURNAL_DISK;
1688 }
1689
1690 /**
1691 * round_member_size_to_mb()- Round given size to closest MiB.
1692 * @size: size to round in sectors.
1693 */
1694 static inline unsigned long long round_member_size_to_mb(unsigned long long size)
1695 {
1696 return (size >> SECT_PER_MB_SHIFT) << SECT_PER_MB_SHIFT;
1697 }
1698
1699 /**
1700 * round_size_to_mb()- Round given size.
1701 * @array_size: size to round in sectors.
1702 * @disk_count: count of data members.
1703 *
1704 * Get size per each data member and round it to closest MiB to ensure that data
1705 * splits evenly between members.
1706 *
1707 * Return: Array size, rounded down.
1708 */
1709 static inline unsigned long long round_size_to_mb(unsigned long long array_size,
1710 unsigned int disk_count)
1711 {
1712 return round_member_size_to_mb(array_size / disk_count) * disk_count;
1713 }
1714
1715 static int able_to_resync(int raid_level, int missing_disks)
1716 {
1717 int max_missing_disks = 0;
1718
1719 switch (raid_level) {
1720 case 10:
1721 max_missing_disks = 1;
1722 break;
1723 default:
1724 max_missing_disks = 0;
1725 }
1726 return missing_disks <= max_missing_disks;
1727 }
1728
1729 /* try to determine how much space is reserved for metadata from
1730 * the last get_extents() entry on the smallest active disk,
1731 * otherwise fallback to the default
1732 */
1733 static __u32 imsm_min_reserved_sectors(struct intel_super *super)
1734 {
1735 struct extent *e;
1736 int i;
1737 unsigned long long min_active;
1738 __u32 remainder;
1739 __u32 rv = MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS;
1740 struct dl *dl, *dl_min = NULL;
1741
1742 if (!super)
1743 return rv;
1744
1745 min_active = 0;
1746 for (dl = super->disks; dl; dl = dl->next) {
1747 if (dl->index < 0)
1748 continue;
1749 unsigned long long blocks = total_blocks(&dl->disk);
1750 if (blocks < min_active || min_active == 0) {
1751 dl_min = dl;
1752 min_active = blocks;
1753 }
1754 }
1755 if (!dl_min)
1756 return rv;
1757
1758 /* find last lba used by subarrays on the smallest active disk */
1759 e = get_extents(super, dl_min, 0);
1760 if (!e)
1761 return rv;
1762 for (i = 0; e[i].size; i++)
1763 continue;
1764
1765 remainder = min_active - e[i].start;
1766 free(e);
1767
1768 /* to give priority to recovery we should not require full
1769 IMSM_RESERVED_SECTORS from the spare */
1770 rv = MPB_SECTOR_CNT + NUM_BLOCKS_DIRTY_STRIPE_REGION;
1771
1772 /* if real reservation is smaller use that value */
1773 return (remainder < rv) ? remainder : rv;
1774 }
1775
1776 static bool is_gen_migration(struct imsm_dev *dev);
1777
1778 #define IMSM_4K_DIV 8
1779
1780 static __u64 blocks_per_migr_unit(struct intel_super *super,
1781 struct imsm_dev *dev);
1782
1783 static void print_imsm_dev(struct intel_super *super,
1784 struct imsm_dev *dev,
1785 char *uuid,
1786 int disk_idx)
1787 {
1788 __u64 sz;
1789 int slot, i;
1790 struct imsm_map *map = get_imsm_map(dev, MAP_0);
1791 struct imsm_map *map2 = get_imsm_map(dev, MAP_1);
1792 __u32 ord;
1793
1794 printf("\n");
1795 printf("[%.16s]:\n", dev->volume);
1796 printf(" Subarray : %d\n", super->current_vol);
1797 printf(" UUID : %s\n", uuid);
1798 printf(" RAID Level : %d", get_imsm_raid_level(map));
1799 if (map2)
1800 printf(" <-- %d", get_imsm_raid_level(map2));
1801 printf("\n");
1802 printf(" Members : %d", map->num_members);
1803 if (map2)
1804 printf(" <-- %d", map2->num_members);
1805 printf("\n");
1806 printf(" Slots : [");
1807 for (i = 0; i < map->num_members; i++) {
1808 ord = get_imsm_ord_tbl_ent(dev, i, MAP_0);
1809 printf("%s", ord & IMSM_ORD_REBUILD ? "_" : "U");
1810 }
1811 printf("]");
1812 if (map2) {
1813 printf(" <-- [");
1814 for (i = 0; i < map2->num_members; i++) {
1815 ord = get_imsm_ord_tbl_ent(dev, i, MAP_1);
1816 printf("%s", ord & IMSM_ORD_REBUILD ? "_" : "U");
1817 }
1818 printf("]");
1819 }
1820 printf("\n");
1821 printf(" Failed disk : ");
1822 if (map->failed_disk_num == 0xff)
1823 printf(STR_COMMON_NONE);
1824 else
1825 printf("%i", map->failed_disk_num);
1826 printf("\n");
1827 slot = get_imsm_disk_slot(map, disk_idx);
1828 if (slot >= 0) {
1829 ord = get_imsm_ord_tbl_ent(dev, slot, MAP_X);
1830 printf(" This Slot : %d%s\n", slot,
1831 ord & IMSM_ORD_REBUILD ? " (out-of-sync)" : "");
1832 } else
1833 printf(" This Slot : ?\n");
1834 printf(" Sector Size : %u\n", super->sector_size);
1835 sz = imsm_dev_size(dev);
1836 printf(" Array Size : %llu%s\n",
1837 (unsigned long long)sz * 512 / super->sector_size,
1838 human_size(sz * 512));
1839 sz = blocks_per_member(map);
1840 printf(" Per Dev Size : %llu%s\n",
1841 (unsigned long long)sz * 512 / super->sector_size,
1842 human_size(sz * 512));
1843 printf(" Sector Offset : %llu\n",
1844 pba_of_lba0(map) * 512 / super->sector_size);
1845 printf(" Num Stripes : %llu\n",
1846 num_data_stripes(map));
1847 printf(" Chunk Size : %u KiB",
1848 __le16_to_cpu(map->blocks_per_strip) / 2);
1849 if (map2)
1850 printf(" <-- %u KiB",
1851 __le16_to_cpu(map2->blocks_per_strip) / 2);
1852 printf("\n");
1853 printf(" Reserved : %d\n", __le32_to_cpu(dev->reserved_blocks));
1854 printf(" Migrate State : ");
1855 if (dev->vol.migr_state) {
1856 if (migr_type(dev) == MIGR_INIT)
1857 printf("initialize\n");
1858 else if (migr_type(dev) == MIGR_REBUILD)
1859 printf("rebuild\n");
1860 else if (migr_type(dev) == MIGR_VERIFY)
1861 printf("check\n");
1862 else if (migr_type(dev) == MIGR_GEN_MIGR)
1863 printf("general migration\n");
1864 else if (migr_type(dev) == MIGR_STATE_CHANGE)
1865 printf("state change\n");
1866 else if (migr_type(dev) == MIGR_REPAIR)
1867 printf("repair\n");
1868 else
1869 printf("<unknown:%d>\n", migr_type(dev));
1870 } else
1871 printf("idle\n");
1872 printf(" Map State : %s", map_state_str[map->map_state]);
1873 if (dev->vol.migr_state) {
1874 struct imsm_map *map = get_imsm_map(dev, MAP_1);
1875
1876 printf(" <-- %s", map_state_str[map->map_state]);
1877 printf("\n Checkpoint : %llu ", vol_curr_migr_unit(dev));
1878 if (is_gen_migration(dev) && (slot > 1 || slot < 0))
1879 printf("(N/A)");
1880 else
1881 printf("(%llu)", (unsigned long long)
1882 blocks_per_migr_unit(super, dev));
1883 }
1884 printf("\n");
1885 printf(" Dirty State : %s\n", (dev->vol.dirty & RAIDVOL_DIRTY) ?
1886 "dirty" : "clean");
1887 printf(" RWH Policy : ");
1888 if (dev->rwh_policy == RWH_OFF || dev->rwh_policy == RWH_MULTIPLE_OFF)
1889 printf("off\n");
1890 else if (dev->rwh_policy == RWH_DISTRIBUTED)
1891 printf("PPL distributed\n");
1892 else if (dev->rwh_policy == RWH_JOURNALING_DRIVE)
1893 printf("PPL journaling drive\n");
1894 else if (dev->rwh_policy == RWH_MULTIPLE_DISTRIBUTED)
1895 printf("Multiple distributed PPLs\n");
1896 else if (dev->rwh_policy == RWH_MULTIPLE_PPLS_JOURNALING_DRIVE)
1897 printf("Multiple PPLs on journaling drive\n");
1898 else if (dev->rwh_policy == RWH_BITMAP)
1899 printf("Write-intent bitmap\n");
1900 else
1901 printf("<unknown:%d>\n", dev->rwh_policy);
1902
1903 printf(" Volume ID : %u\n", dev->my_vol_raid_dev_num);
1904 }
1905
1906 static void print_imsm_disk(struct imsm_disk *disk,
1907 int index,
1908 __u32 reserved,
1909 unsigned int sector_size) {
1910 char str[MAX_RAID_SERIAL_LEN + 1];
1911 __u64 sz;
1912
1913 if (index < -1 || !disk)
1914 return;
1915
1916 printf("\n");
1917 snprintf(str, MAX_RAID_SERIAL_LEN + 1, "%s", disk->serial);
1918 if (index >= 0)
1919 printf(" Disk%02d Serial : %s\n", index, str);
1920 else
1921 printf(" Disk Serial : %s\n", str);
1922 printf(" State :%s%s%s%s\n", is_spare(disk) ? " spare" : "",
1923 is_configured(disk) ? " active" : "",
1924 is_failed(disk) ? " failed" : "",
1925 is_journal(disk) ? " journal" : "");
1926 printf(" Id : %08x\n", __le32_to_cpu(disk->scsi_id));
1927 sz = total_blocks(disk) - reserved;
1928 printf(" Usable Size : %llu%s\n",
1929 (unsigned long long)sz * 512 / sector_size,
1930 human_size(sz * 512));
1931 }
1932
1933 void convert_to_4k_imsm_migr_rec(struct intel_super *super)
1934 {
1935 struct migr_record *migr_rec = super->migr_rec;
1936
1937 migr_rec->blocks_per_unit /= IMSM_4K_DIV;
1938 migr_rec->dest_depth_per_unit /= IMSM_4K_DIV;
1939 split_ull((join_u32(migr_rec->post_migr_vol_cap,
1940 migr_rec->post_migr_vol_cap_hi) / IMSM_4K_DIV),
1941 &migr_rec->post_migr_vol_cap, &migr_rec->post_migr_vol_cap_hi);
1942 set_migr_chkp_area_pba(migr_rec,
1943 migr_chkp_area_pba(migr_rec) / IMSM_4K_DIV);
1944 set_migr_dest_1st_member_lba(migr_rec,
1945 migr_dest_1st_member_lba(migr_rec) / IMSM_4K_DIV);
1946 }
1947
1948 void convert_to_4k_imsm_disk(struct imsm_disk *disk)
1949 {
1950 set_total_blocks(disk, (total_blocks(disk)/IMSM_4K_DIV));
1951 }
1952
1953 void convert_to_4k(struct intel_super *super)
1954 {
1955 struct imsm_super *mpb = super->anchor;
1956 struct imsm_disk *disk;
1957 int i;
1958 __u32 bbm_log_size = __le32_to_cpu(mpb->bbm_log_size);
1959
1960 for (i = 0; i < mpb->num_disks ; i++) {
1961 disk = __get_imsm_disk(mpb, i);
1962 /* disk */
1963 convert_to_4k_imsm_disk(disk);
1964 }
1965 for (i = 0; i < mpb->num_raid_devs; i++) {
1966 struct imsm_dev *dev = __get_imsm_dev(mpb, i);
1967 struct imsm_map *map = get_imsm_map(dev, MAP_0);
1968 /* dev */
1969 set_imsm_dev_size(dev, imsm_dev_size(dev)/IMSM_4K_DIV);
1970 set_vol_curr_migr_unit(dev,
1971 vol_curr_migr_unit(dev) / IMSM_4K_DIV);
1972
1973 /* map0 */
1974 set_blocks_per_member(map, blocks_per_member(map)/IMSM_4K_DIV);
1975 map->blocks_per_strip /= IMSM_4K_DIV;
1976 set_pba_of_lba0(map, pba_of_lba0(map)/IMSM_4K_DIV);
1977
1978 if (dev->vol.migr_state) {
1979 /* map1 */
1980 map = get_imsm_map(dev, MAP_1);
1981 set_blocks_per_member(map,
1982 blocks_per_member(map)/IMSM_4K_DIV);
1983 map->blocks_per_strip /= IMSM_4K_DIV;
1984 set_pba_of_lba0(map, pba_of_lba0(map)/IMSM_4K_DIV);
1985 }
1986 }
1987 if (bbm_log_size) {
1988 struct bbm_log *log = (void *)mpb +
1989 __le32_to_cpu(mpb->mpb_size) - bbm_log_size;
1990 __u32 i;
1991
1992 for (i = 0; i < log->entry_count; i++) {
1993 struct bbm_log_entry *entry =
1994 &log->marked_block_entries[i];
1995
1996 __u8 count = entry->marked_count + 1;
1997 unsigned long long sector =
1998 __le48_to_cpu(&entry->defective_block_start);
1999
2000 entry->defective_block_start =
2001 __cpu_to_le48(sector/IMSM_4K_DIV);
2002 entry->marked_count = max(count/IMSM_4K_DIV, 1) - 1;
2003 }
2004 }
2005
2006 mpb->check_sum = __gen_imsm_checksum(mpb);
2007 }
2008
2009 void examine_migr_rec_imsm(struct intel_super *super)
2010 {
2011 struct migr_record *migr_rec = super->migr_rec;
2012 struct imsm_super *mpb = super->anchor;
2013 int i;
2014
2015 for (i = 0; i < mpb->num_raid_devs; i++) {
2016 struct imsm_dev *dev = __get_imsm_dev(mpb, i);
2017 struct imsm_map *map;
2018 int slot = -1;
2019
2020 if (is_gen_migration(dev) == false)
2021 continue;
2022
2023 printf("\nMigration Record Information:");
2024
2025 /* first map under migration */
2026 map = get_imsm_map(dev, MAP_0);
2027
2028 if (map)
2029 slot = get_imsm_disk_slot(map, super->disks->index);
2030 if (map == NULL || slot > 1 || slot < 0) {
2031 printf(" Empty\n ");
2032 printf("Examine one of first two disks in array\n");
2033 break;
2034 }
2035 printf("\n Status : ");
2036 if (__le32_to_cpu(migr_rec->rec_status) == UNIT_SRC_NORMAL)
2037 printf("Normal\n");
2038 else
2039 printf("Contains Data\n");
2040 printf(" Current Unit : %llu\n",
2041 current_migr_unit(migr_rec));
2042 printf(" Family : %u\n",
2043 __le32_to_cpu(migr_rec->family_num));
2044 printf(" Ascending : %u\n",
2045 __le32_to_cpu(migr_rec->ascending_migr));
2046 printf(" Blocks Per Unit : %u\n",
2047 __le32_to_cpu(migr_rec->blocks_per_unit));
2048 printf(" Dest. Depth Per Unit : %u\n",
2049 __le32_to_cpu(migr_rec->dest_depth_per_unit));
2050 printf(" Checkpoint Area pba : %llu\n",
2051 migr_chkp_area_pba(migr_rec));
2052 printf(" First member lba : %llu\n",
2053 migr_dest_1st_member_lba(migr_rec));
2054 printf(" Total Number of Units : %llu\n",
2055 get_num_migr_units(migr_rec));
2056 printf(" Size of volume : %llu\n",
2057 join_u32(migr_rec->post_migr_vol_cap,
2058 migr_rec->post_migr_vol_cap_hi));
2059 printf(" Record was read from : %u\n",
2060 __le32_to_cpu(migr_rec->ckpt_read_disk_num));
2061
2062 break;
2063 }
2064 }
2065
2066 void convert_from_4k_imsm_migr_rec(struct intel_super *super)
2067 {
2068 struct migr_record *migr_rec = super->migr_rec;
2069
2070 migr_rec->blocks_per_unit *= IMSM_4K_DIV;
2071 migr_rec->dest_depth_per_unit *= IMSM_4K_DIV;
2072 split_ull((join_u32(migr_rec->post_migr_vol_cap,
2073 migr_rec->post_migr_vol_cap_hi) * IMSM_4K_DIV),
2074 &migr_rec->post_migr_vol_cap,
2075 &migr_rec->post_migr_vol_cap_hi);
2076 set_migr_chkp_area_pba(migr_rec,
2077 migr_chkp_area_pba(migr_rec) * IMSM_4K_DIV);
2078 set_migr_dest_1st_member_lba(migr_rec,
2079 migr_dest_1st_member_lba(migr_rec) * IMSM_4K_DIV);
2080 }
2081
2082 void convert_from_4k(struct intel_super *super)
2083 {
2084 struct imsm_super *mpb = super->anchor;
2085 struct imsm_disk *disk;
2086 int i;
2087 __u32 bbm_log_size = __le32_to_cpu(mpb->bbm_log_size);
2088
2089 for (i = 0; i < mpb->num_disks ; i++) {
2090 disk = __get_imsm_disk(mpb, i);
2091 /* disk */
2092 set_total_blocks(disk, (total_blocks(disk)*IMSM_4K_DIV));
2093 }
2094
2095 for (i = 0; i < mpb->num_raid_devs; i++) {
2096 struct imsm_dev *dev = __get_imsm_dev(mpb, i);
2097 struct imsm_map *map = get_imsm_map(dev, MAP_0);
2098 /* dev */
2099 set_imsm_dev_size(dev, imsm_dev_size(dev)*IMSM_4K_DIV);
2100 set_vol_curr_migr_unit(dev,
2101 vol_curr_migr_unit(dev) * IMSM_4K_DIV);
2102
2103 /* map0 */
2104 set_blocks_per_member(map, blocks_per_member(map)*IMSM_4K_DIV);
2105 map->blocks_per_strip *= IMSM_4K_DIV;
2106 set_pba_of_lba0(map, pba_of_lba0(map)*IMSM_4K_DIV);
2107
2108 if (dev->vol.migr_state) {
2109 /* map1 */
2110 map = get_imsm_map(dev, MAP_1);
2111 set_blocks_per_member(map,
2112 blocks_per_member(map)*IMSM_4K_DIV);
2113 map->blocks_per_strip *= IMSM_4K_DIV;
2114 set_pba_of_lba0(map, pba_of_lba0(map)*IMSM_4K_DIV);
2115 }
2116 }
2117 if (bbm_log_size) {
2118 struct bbm_log *log = (void *)mpb +
2119 __le32_to_cpu(mpb->mpb_size) - bbm_log_size;
2120 __u32 i;
2121
2122 for (i = 0; i < log->entry_count; i++) {
2123 struct bbm_log_entry *entry =
2124 &log->marked_block_entries[i];
2125
2126 __u8 count = entry->marked_count + 1;
2127 unsigned long long sector =
2128 __le48_to_cpu(&entry->defective_block_start);
2129
2130 entry->defective_block_start =
2131 __cpu_to_le48(sector*IMSM_4K_DIV);
2132 entry->marked_count = count*IMSM_4K_DIV - 1;
2133 }
2134 }
2135
2136 mpb->check_sum = __gen_imsm_checksum(mpb);
2137 }
2138
2139 /**
2140 * imsm_check_attributes() - Check if features represented by attributes flags are supported.
2141 *
2142 * @attributes: attributes read from metadata.
2143 * Returns: true if all features are supported, false otherwise.
2144 */
2145 static bool imsm_check_attributes(__u32 attributes)
2146 {
2147 if ((attributes & (MPB_ATTRIB_SUPPORTED | MPB_ATTRIB_IGNORED)) == attributes)
2148 return true;
2149
2150 return false;
2151 }
2152
2153 static void getinfo_super_imsm(struct supertype *st, struct mdinfo *info, char *map);
2154
2155 static void examine_super_imsm(struct supertype *st, char *homehost)
2156 {
2157 struct intel_super *super = st->sb;
2158 struct imsm_super *mpb = super->anchor;
2159 char str[MAX_SIGNATURE_LENGTH];
2160 int i;
2161 struct mdinfo info;
2162 char nbuf[64];
2163 __u32 sum;
2164 __u32 reserved = imsm_reserved_sectors(super, super->disks);
2165 struct dl *dl;
2166 time_t creation_time;
2167
2168 strncpy(str, (char *)mpb->sig, MPB_SIG_LEN);
2169 str[MPB_SIG_LEN-1] = '\0';
2170 printf(" Magic : %s\n", str);
2171 printf(" Version : %s\n", get_imsm_version(mpb));
2172 printf(" Orig Family : %08x\n", __le32_to_cpu(mpb->orig_family_num));
2173 printf(" Family : %08x\n", __le32_to_cpu(mpb->family_num));
2174 printf(" Generation : %08x\n", __le32_to_cpu(mpb->generation_num));
2175 creation_time = __le64_to_cpu(mpb->creation_time);
2176 printf(" Creation Time : %.24s\n",
2177 creation_time ? ctime(&creation_time) : "Unknown");
2178
2179 printf(" Attributes : %08x (%s)\n", mpb->attributes,
2180 imsm_check_attributes(mpb->attributes) ? "supported" : "not supported");
2181
2182 getinfo_super_imsm(st, &info, NULL);
2183 fname_from_uuid(&info, nbuf);
2184 printf(" UUID : %s\n", nbuf + 5);
2185 sum = __le32_to_cpu(mpb->check_sum);
2186 printf(" Checksum : %08x %s\n", sum,
2187 __gen_imsm_checksum(mpb) == sum ? "correct" : "incorrect");
2188 printf(" MPB Sectors : %d\n", mpb_sectors(mpb, super->sector_size));
2189 printf(" Disks : %d\n", mpb->num_disks);
2190 printf(" RAID Devices : %d\n", mpb->num_raid_devs);
2191 print_imsm_disk(__get_imsm_disk(mpb, super->disks->index),
2192 super->disks->index, reserved, super->sector_size);
2193 if (get_imsm_bbm_log_size(super->bbm_log)) {
2194 struct bbm_log *log = super->bbm_log;
2195
2196 printf("\n");
2197 printf("Bad Block Management Log:\n");
2198 printf(" Log Size : %d\n", __le32_to_cpu(mpb->bbm_log_size));
2199 printf(" Signature : %x\n", __le32_to_cpu(log->signature));
2200 printf(" Entry Count : %d\n", __le32_to_cpu(log->entry_count));
2201 }
2202 for (i = 0; i < mpb->num_raid_devs; i++) {
2203 struct mdinfo info;
2204 struct imsm_dev *dev = __get_imsm_dev(mpb, i);
2205
2206 super->current_vol = i;
2207 getinfo_super_imsm(st, &info, NULL);
2208 fname_from_uuid(&info, nbuf);
2209 print_imsm_dev(super, dev, nbuf + 5, super->disks->index);
2210 }
2211 for (i = 0; i < mpb->num_disks; i++) {
2212 if (i == super->disks->index)
2213 continue;
2214 print_imsm_disk(__get_imsm_disk(mpb, i), i, reserved,
2215 super->sector_size);
2216 }
2217
2218 for (dl = super->disks; dl; dl = dl->next)
2219 if (dl->index == -1)
2220 print_imsm_disk(&dl->disk, -1, reserved,
2221 super->sector_size);
2222
2223 examine_migr_rec_imsm(super);
2224 }
2225
2226 static void brief_examine_super_imsm(struct supertype *st, int verbose)
2227 {
2228 /* We just write a generic IMSM ARRAY entry */
2229 struct mdinfo info;
2230 char nbuf[64];
2231
2232 getinfo_super_imsm(st, &info, NULL);
2233 fname_from_uuid(&info, nbuf);
2234 printf("ARRAY metadata=imsm UUID=%s\n", nbuf + 5);
2235 }
2236
2237 static void brief_examine_subarrays_imsm(struct supertype *st, int verbose)
2238 {
2239 /* We just write a generic IMSM ARRAY entry */
2240 struct mdinfo info;
2241 char nbuf[64];
2242 char nbuf1[64];
2243 struct intel_super *super = st->sb;
2244 int i;
2245
2246 if (!super->anchor->num_raid_devs)
2247 return;
2248
2249 getinfo_super_imsm(st, &info, NULL);
2250 fname_from_uuid(&info, nbuf);
2251 for (i = 0; i < super->anchor->num_raid_devs; i++) {
2252 struct imsm_dev *dev = get_imsm_dev(super, i);
2253
2254 super->current_vol = i;
2255 getinfo_super_imsm(st, &info, NULL);
2256 fname_from_uuid(&info, nbuf1);
2257 printf("ARRAY " DEV_MD_DIR "%.16s container=%s member=%d UUID=%s\n",
2258 dev->volume, nbuf + 5, i, nbuf1 + 5);
2259 }
2260 }
2261
2262 static void export_examine_super_imsm(struct supertype *st)
2263 {
2264 struct intel_super *super = st->sb;
2265 struct imsm_super *mpb = super->anchor;
2266 struct mdinfo info;
2267 char nbuf[64];
2268
2269 getinfo_super_imsm(st, &info, NULL);
2270 fname_from_uuid(&info, nbuf);
2271 printf("MD_METADATA=imsm\n");
2272 printf("MD_LEVEL=container\n");
2273 printf("MD_UUID=%s\n", nbuf+5);
2274 printf("MD_DEVICES=%u\n", mpb->num_disks);
2275 printf("MD_CREATION_TIME=%llu\n", __le64_to_cpu(mpb->creation_time));
2276 }
2277
2278 static void detail_super_imsm(struct supertype *st, char *homehost,
2279 char *subarray)
2280 {
2281 struct mdinfo info;
2282 char nbuf[64];
2283 struct intel_super *super = st->sb;
2284 int temp_vol = super->current_vol;
2285
2286 if (subarray)
2287 super->current_vol = strtoul(subarray, NULL, 10);
2288
2289 getinfo_super_imsm(st, &info, NULL);
2290 fname_from_uuid(&info, nbuf);
2291 printf("\n UUID : %s\n", nbuf + 5);
2292
2293 super->current_vol = temp_vol;
2294 }
2295
2296 static void brief_detail_super_imsm(struct supertype *st, char *subarray)
2297 {
2298 struct mdinfo info;
2299 char nbuf[64];
2300 struct intel_super *super = st->sb;
2301 int temp_vol = super->current_vol;
2302
2303 if (subarray)
2304 super->current_vol = strtoul(subarray, NULL, 10);
2305
2306 getinfo_super_imsm(st, &info, NULL);
2307 fname_from_uuid(&info, nbuf);
2308 printf(" UUID=%s", nbuf + 5);
2309
2310 super->current_vol = temp_vol;
2311 }
2312
2313 static int imsm_read_serial(int fd, char *devname, __u8 *serial,
2314 size_t serial_buf_len);
2315 static void fd2devname(int fd, char *name);
2316
2317 void print_encryption_information(int disk_fd, enum sys_dev_type hba_type)
2318 {
2319 struct encryption_information information = {0};
2320 mdadm_status_t status = MDADM_STATUS_SUCCESS;
2321 const char *indent = " ";
2322
2323 switch (hba_type) {
2324 case SYS_DEV_VMD:
2325 case SYS_DEV_NVME:
2326 status = get_nvme_opal_encryption_information(disk_fd, &information, 1);
2327 break;
2328 case SYS_DEV_SATA:
2329 case SYS_DEV_SATA_VMD:
2330 status = get_ata_encryption_information(disk_fd, &information, 1);
2331 break;
2332 default:
2333 return;
2334 }
2335
2336 if (status) {
2337 pr_err("Failed to get drive encryption information.\n");
2338 return;
2339 }
2340
2341 printf("%sEncryption(Ability|Status): %s|%s\n", indent,
2342 get_encryption_ability_string(information.ability),
2343 get_encryption_status_string(information.status));
2344 }
2345
2346 static int ahci_enumerate_ports(struct sys_dev *hba, int port_count, int host_base, int verbose)
2347 {
2348 /* dump an unsorted list of devices attached to AHCI Intel storage
2349 * controller, as well as non-connected ports
2350 */
2351 int hba_len = strlen(hba->path) + 1;
2352 struct dirent *ent;
2353 DIR *dir;
2354 char *path = NULL;
2355 int err = 0;
2356 unsigned long port_mask = (1 << port_count) - 1;
2357
2358 if (port_count > (int)sizeof(port_mask) * 8) {
2359 if (verbose > 0)
2360 pr_err("port_count %d out of range\n", port_count);
2361 return 2;
2362 }
2363
2364 /* scroll through /sys/dev/block looking for devices attached to
2365 * this hba
2366 */
2367 dir = opendir("/sys/dev/block");
2368 if (!dir)
2369 return 1;
2370
2371 for (ent = readdir(dir); ent; ent = readdir(dir)) {
2372 int fd;
2373 char model[64];
2374 char vendor[64];
2375 char buf[1024];
2376 int major, minor;
2377 char device[PATH_MAX];
2378 char *c;
2379 int port;
2380 int type;
2381
2382 if (sscanf(ent->d_name, "%d:%d", &major, &minor) != 2)
2383 continue;
2384 path = devt_to_devpath(makedev(major, minor), 1, NULL);
2385 if (!path)
2386 continue;
2387 if (!path_attached_to_hba(path, hba->path)) {
2388 free(path);
2389 path = NULL;
2390 continue;
2391 }
2392
2393 /* retrieve the scsi device */
2394 if (!devt_to_devpath(makedev(major, minor), 1, device)) {
2395 if (verbose > 0)
2396 pr_err("failed to get device\n");
2397 err = 2;
2398 break;
2399 }
2400 if (devpath_to_char(device, "type", buf, sizeof(buf), 0)) {
2401 err = 2;
2402 break;
2403 }
2404 type = strtoul(buf, NULL, 10);
2405
2406 /* if it's not a disk print the vendor and model */
2407 if (!(type == 0 || type == 7 || type == 14)) {
2408 vendor[0] = '\0';
2409 model[0] = '\0';
2410
2411 if (devpath_to_char(device, "vendor", buf,
2412 sizeof(buf), 0) == 0) {
2413 strncpy(vendor, buf, sizeof(vendor));
2414 vendor[sizeof(vendor) - 1] = '\0';
2415 c = (char *) &vendor[sizeof(vendor) - 1];
2416 while (isspace(*c) || *c == '\0')
2417 *c-- = '\0';
2418
2419 }
2420
2421 if (devpath_to_char(device, "model", buf,
2422 sizeof(buf), 0) == 0) {
2423 strncpy(model, buf, sizeof(model));
2424 model[sizeof(model) - 1] = '\0';
2425 c = (char *) &model[sizeof(model) - 1];
2426 while (isspace(*c) || *c == '\0')
2427 *c-- = '\0';
2428 }
2429
2430 if (vendor[0] && model[0])
2431 sprintf(buf, "%.64s %.64s", vendor, model);
2432 else
2433 switch (type) { /* numbers from hald/linux/device.c */
2434 case 1: sprintf(buf, "tape"); break;
2435 case 2: sprintf(buf, "printer"); break;
2436 case 3: sprintf(buf, "processor"); break;
2437 case 4:
2438 case 5: sprintf(buf, "cdrom"); break;
2439 case 6: sprintf(buf, "scanner"); break;
2440 case 8: sprintf(buf, "media_changer"); break;
2441 case 9: sprintf(buf, "comm"); break;
2442 case 12: sprintf(buf, "raid"); break;
2443 default: sprintf(buf, "unknown");
2444 }
2445 } else
2446 buf[0] = '\0';
2447
2448 /* chop device path to 'host%d' and calculate the port number */
2449 c = strchr(&path[hba_len], '/');
2450 if (!c) {
2451 if (verbose > 0)
2452 pr_err("%s - invalid path name\n", path + hba_len);
2453 err = 2;
2454 break;
2455 }
2456 *c = '\0';
2457 if ((sscanf(&path[hba_len], "ata%d", &port) == 1) ||
2458 ((sscanf(&path[hba_len], "host%d", &port) == 1)))
2459 port -= host_base;
2460 else {
2461 if (verbose > 0) {
2462 *c = '/'; /* repair the full string */
2463 pr_err("failed to determine port number for %s\n",
2464 path);
2465 }
2466 err = 2;
2467 break;
2468 }
2469
2470 /* mark this port as used */
2471 port_mask &= ~(1 << port);
2472
2473 /* print out the device information */
2474 if (buf[0]) {
2475 printf(" Port%d : - non-disk device (%s) -\n", port, buf);
2476 continue;
2477 }
2478
2479 fd = dev_open(ent->d_name, O_RDONLY);
2480 if (!is_fd_valid(fd))
2481 printf(" Port%d : - disk info unavailable -\n", port);
2482 else {
2483 fd2devname(fd, buf);
2484 printf(" Port%d : %s", port, buf);
2485 if (imsm_read_serial(fd, NULL, (__u8 *)buf,
2486 sizeof(buf)) == 0)
2487 printf(" (%s)\n", buf);
2488 else
2489 printf(" ()\n");
2490
2491 print_encryption_information(fd, hba->type);
2492 close(fd);
2493 }
2494 free(path);
2495 path = NULL;
2496 }
2497 if (path)
2498 free(path);
2499 if (dir)
2500 closedir(dir);
2501 if (err == 0) {
2502 int i;
2503
2504 for (i = 0; i < port_count; i++)
2505 if (port_mask & (1 << i))
2506 printf(" Port%d : - no device attached -\n", i);
2507 }
2508
2509 return err;
2510 }
2511
2512 static int print_nvme_info(struct sys_dev *hba)
2513 {
2514 struct dirent *ent;
2515 DIR *dir;
2516
2517 dir = opendir("/sys/block/");
2518 if (!dir)
2519 return 1;
2520
2521 for (ent = readdir(dir); ent; ent = readdir(dir)) {
2522 char ns_path[PATH_MAX];
2523 char cntrl_path[PATH_MAX];
2524 char buf[PATH_MAX];
2525 int fd = -1;
2526
2527 if (!strstr(ent->d_name, "nvme"))
2528 goto skip;
2529
2530 fd = open_dev(ent->d_name);
2531 if (!is_fd_valid(fd))
2532 goto skip;
2533
2534 if (!diskfd_to_devpath(fd, 0, ns_path) ||
2535 !diskfd_to_devpath(fd, 1, cntrl_path))
2536 goto skip;
2537
2538 if (!path_attached_to_hba(cntrl_path, hba->path))
2539 goto skip;
2540
2541 if (!imsm_is_nvme_namespace_supported(fd, 0))
2542 goto skip;
2543
2544 fd2devname(fd, buf);
2545 if (hba->type == SYS_DEV_VMD)
2546 printf(" NVMe under VMD : %s", buf);
2547 else if (hba->type == SYS_DEV_NVME)
2548 printf(" NVMe Device : %s", buf);
2549
2550 if (!imsm_read_serial(fd, NULL, (__u8 *)buf,
2551 sizeof(buf)))
2552 printf(" (%s)\n", buf);
2553 else
2554 printf("()\n");
2555
2556 print_encryption_information(fd, hba->type);
2557
2558 skip:
2559 close_fd(&fd);
2560 }
2561
2562 closedir(dir);
2563 return 0;
2564 }
2565
2566 static void print_found_intel_controllers(struct sys_dev *elem)
2567 {
2568 for (; elem; elem = elem->next) {
2569 pr_err("found Intel(R) ");
2570 if (elem->type == SYS_DEV_SATA)
2571 fprintf(stderr, "SATA ");
2572 else if (elem->type == SYS_DEV_SAS)
2573 fprintf(stderr, "SAS ");
2574 else if (elem->type == SYS_DEV_NVME)
2575 fprintf(stderr, "NVMe ");
2576
2577 if (elem->type == SYS_DEV_VMD)
2578 fprintf(stderr, "VMD domain");
2579 else if (elem->type == SYS_DEV_SATA_VMD)
2580 fprintf(stderr, "SATA VMD domain");
2581 else
2582 fprintf(stderr, "RAID controller");
2583
2584 if (elem->pci_id)
2585 fprintf(stderr, " at %s", elem->pci_id);
2586 fprintf(stderr, ".\n");
2587 }
2588 fflush(stderr);
2589 }
2590
2591 static int ahci_get_port_count(const char *hba_path, int *port_count)
2592 {
2593 struct dirent *ent;
2594 DIR *dir;
2595 int host_base = -1;
2596
2597 *port_count = 0;
2598 if ((dir = opendir(hba_path)) == NULL)
2599 return -1;
2600
2601 for (ent = readdir(dir); ent; ent = readdir(dir)) {
2602 int host;
2603
2604 if ((sscanf(ent->d_name, "ata%d", &host) != 1) &&
2605 ((sscanf(ent->d_name, "host%d", &host) != 1)))
2606 continue;
2607 if (*port_count == 0)
2608 host_base = host;
2609 else if (host < host_base)
2610 host_base = host;
2611
2612 if (host + 1 > *port_count + host_base)
2613 *port_count = host + 1 - host_base;
2614 }
2615 closedir(dir);
2616 return host_base;
2617 }
2618
2619 static void print_imsm_level_capability(const struct imsm_orom *orom)
2620 {
2621 int idx;
2622
2623 for (idx = 0; imsm_level_ops[idx].name; idx++)
2624 if (imsm_level_ops[idx].is_level_supported(orom))
2625 printf("%s ", imsm_level_ops[idx].name);
2626 }
2627
2628 static void print_imsm_capability(const struct imsm_orom *orom)
2629 {
2630 printf(" Platform : Intel(R) ");
2631 if (orom->capabilities == 0 && orom->driver_features == 0)
2632 printf("Matrix Storage Manager\n");
2633 else if (imsm_orom_is_enterprise(orom) && orom->major_ver >= 6)
2634 printf("Virtual RAID on CPU\n");
2635 else
2636 printf("Rapid Storage Technology%s\n",
2637 imsm_orom_is_enterprise(orom) ? " enterprise" : "");
2638 if (orom->major_ver || orom->minor_ver || orom->hotfix_ver || orom->build) {
2639 if (imsm_orom_is_vmd_without_efi(orom))
2640 printf(" Version : %d.%d\n", orom->major_ver,
2641 orom->minor_ver);
2642 else
2643 printf(" Version : %d.%d.%d.%d\n", orom->major_ver,
2644 orom->minor_ver, orom->hotfix_ver, orom->build);
2645 }
2646
2647 printf(" RAID Levels : ");
2648 print_imsm_level_capability(orom);
2649 printf("\n");
2650
2651 printf(" Chunk Sizes :%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
2652 imsm_orom_has_chunk(orom, 2) ? " 2k" : "",
2653 imsm_orom_has_chunk(orom, 4) ? " 4k" : "",
2654 imsm_orom_has_chunk(orom, 8) ? " 8k" : "",
2655 imsm_orom_has_chunk(orom, 16) ? " 16k" : "",
2656 imsm_orom_has_chunk(orom, 32) ? " 32k" : "",
2657 imsm_orom_has_chunk(orom, 64) ? " 64k" : "",
2658 imsm_orom_has_chunk(orom, 128) ? " 128k" : "",
2659 imsm_orom_has_chunk(orom, 256) ? " 256k" : "",
2660 imsm_orom_has_chunk(orom, 512) ? " 512k" : "",
2661 imsm_orom_has_chunk(orom, 1024*1) ? " 1M" : "",
2662 imsm_orom_has_chunk(orom, 1024*2) ? " 2M" : "",
2663 imsm_orom_has_chunk(orom, 1024*4) ? " 4M" : "",
2664 imsm_orom_has_chunk(orom, 1024*8) ? " 8M" : "",
2665 imsm_orom_has_chunk(orom, 1024*16) ? " 16M" : "",
2666 imsm_orom_has_chunk(orom, 1024*32) ? " 32M" : "",
2667 imsm_orom_has_chunk(orom, 1024*64) ? " 64M" : "");
2668 printf(" 2TB volumes :%s supported\n",
2669 (orom->attr & IMSM_OROM_ATTR_2TB)?"":" not");
2670 printf(" 2TB disks :%s supported\n",
2671 (orom->attr & IMSM_OROM_ATTR_2TB_DISK)?"":" not");
2672 printf(" Max Disks : %d\n", orom->tds);
2673 printf(" Max Volumes : %d per array, %d per %s\n",
2674 orom->vpa, orom->vphba,
2675 imsm_orom_is_nvme(orom) ? "platform" : "controller");
2676 return;
2677 }
2678
2679 static void print_imsm_capability_export(const struct imsm_orom *orom)
2680 {
2681 printf("MD_FIRMWARE_TYPE=imsm\n");
2682 if (orom->major_ver || orom->minor_ver || orom->hotfix_ver || orom->build)
2683 printf("IMSM_VERSION=%d.%d.%d.%d\n", orom->major_ver, orom->minor_ver,
2684 orom->hotfix_ver, orom->build);
2685
2686 printf("IMSM_SUPPORTED_RAID_LEVELS=");
2687 print_imsm_level_capability(orom);
2688 printf("\n");
2689
2690 printf("IMSM_SUPPORTED_CHUNK_SIZES=%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
2691 imsm_orom_has_chunk(orom, 2) ? "2k " : "",
2692 imsm_orom_has_chunk(orom, 4) ? "4k " : "",
2693 imsm_orom_has_chunk(orom, 8) ? "8k " : "",
2694 imsm_orom_has_chunk(orom, 16) ? "16k " : "",
2695 imsm_orom_has_chunk(orom, 32) ? "32k " : "",
2696 imsm_orom_has_chunk(orom, 64) ? "64k " : "",
2697 imsm_orom_has_chunk(orom, 128) ? "128k " : "",
2698 imsm_orom_has_chunk(orom, 256) ? "256k " : "",
2699 imsm_orom_has_chunk(orom, 512) ? "512k " : "",
2700 imsm_orom_has_chunk(orom, 1024*1) ? "1M " : "",
2701 imsm_orom_has_chunk(orom, 1024*2) ? "2M " : "",
2702 imsm_orom_has_chunk(orom, 1024*4) ? "4M " : "",
2703 imsm_orom_has_chunk(orom, 1024*8) ? "8M " : "",
2704 imsm_orom_has_chunk(orom, 1024*16) ? "16M " : "",
2705 imsm_orom_has_chunk(orom, 1024*32) ? "32M " : "",
2706 imsm_orom_has_chunk(orom, 1024*64) ? "64M " : "");
2707 printf("IMSM_2TB_VOLUMES=%s\n",(orom->attr & IMSM_OROM_ATTR_2TB) ? "yes" : "no");
2708 printf("IMSM_2TB_DISKS=%s\n",(orom->attr & IMSM_OROM_ATTR_2TB_DISK) ? "yes" : "no");
2709 printf("IMSM_MAX_DISKS=%d\n",orom->tds);
2710 printf("IMSM_MAX_VOLUMES_PER_ARRAY=%d\n",orom->vpa);
2711 printf("IMSM_MAX_VOLUMES_PER_CONTROLLER=%d\n",orom->vphba);
2712 }
2713
2714 static int detail_platform_imsm(int verbose, int enumerate_only, char *controller_path)
2715 {
2716 /* There are two components to imsm platform support, the ahci SATA
2717 * controller and the option-rom. To find the SATA controller we
2718 * simply look in /sys/bus/pci/drivers/ahci to see if an ahci
2719 * controller with the Intel vendor id is present. This approach
2720 * allows mdadm to leverage the kernel's ahci detection logic, with the
2721 * caveat that if ahci.ko is not loaded mdadm will not be able to
2722 * detect platform raid capabilities. The option-rom resides in a
2723 * platform "Adapter ROM". We scan for its signature to retrieve the
2724 * platform capabilities. If raid support is disabled in the BIOS the
2725 * option-rom capability structure will not be available.
2726 */
2727 struct sys_dev *list, *hba;
2728 int host_base = 0;
2729 int port_count = 0;
2730 int result=1;
2731
2732 if (enumerate_only) {
2733 if (check_no_platform())
2734 return 0;
2735 list = find_intel_devices();
2736 if (!list)
2737 return 2;
2738 for (hba = list; hba; hba = hba->next) {
2739 if (find_imsm_capability(hba)) {
2740 result = 0;
2741 break;
2742 }
2743 else
2744 result = 2;
2745 }
2746 return result;
2747 }
2748
2749 list = find_intel_devices();
2750 if (!list) {
2751 if (verbose > 0)
2752 pr_err("no active Intel(R) RAID controller found.\n");
2753 return 2;
2754 } else if (verbose > 0)
2755 print_found_intel_controllers(list);
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)) {
2761 char buf[PATH_MAX];
2762 pr_err("imsm capabilities not found for controller: %s (type %s)\n",
2763 hba->type == SYS_DEV_VMD || hba->type == SYS_DEV_SATA_VMD ?
2764 vmd_domain_to_controller(hba, buf) :
2765 hba->path, get_sys_dev_type(hba->type));
2766 continue;
2767 }
2768 result = 0;
2769 }
2770
2771 if (controller_path && result == 1) {
2772 pr_err("no active Intel(R) RAID controller found under %s\n",
2773 controller_path);
2774 return result;
2775 }
2776
2777 const struct orom_entry *entry;
2778
2779 for (entry = orom_entries; entry; entry = entry->next) {
2780 if (entry->type == SYS_DEV_VMD) {
2781 print_imsm_capability(&entry->orom);
2782 printf(" 3rd party NVMe :%s supported\n",
2783 imsm_orom_has_tpv_support(&entry->orom)?"":" not");
2784 for (hba = list; hba; hba = hba->next) {
2785 if (hba->type == SYS_DEV_VMD) {
2786 char buf[PATH_MAX];
2787 printf(" I/O Controller : %s (%s)\n",
2788 vmd_domain_to_controller(hba, buf), get_sys_dev_type(hba->type));
2789 if (print_nvme_info(hba)) {
2790 if (verbose > 0)
2791 pr_err("failed to get devices attached to VMD domain.\n");
2792 result |= 2;
2793 }
2794 }
2795 }
2796 printf("\n");
2797 continue;
2798 }
2799
2800 print_imsm_capability(&entry->orom);
2801 if (entry->type == SYS_DEV_NVME) {
2802 for (hba = list; hba; hba = hba->next) {
2803 if (hba->type == SYS_DEV_NVME)
2804 print_nvme_info(hba);
2805 }
2806 printf("\n");
2807 continue;
2808 }
2809
2810 struct devid_list *devid;
2811 for (devid = entry->devid_list; devid; devid = devid->next) {
2812 hba = device_by_id(devid->devid);
2813 if (!hba)
2814 continue;
2815
2816 printf(" I/O Controller : %s (%s)\n",
2817 hba->path, get_sys_dev_type(hba->type));
2818 if (hba->type == SYS_DEV_SATA || hba->type == SYS_DEV_SATA_VMD) {
2819 host_base = ahci_get_port_count(hba->path, &port_count);
2820 if (ahci_enumerate_ports(hba, port_count, host_base, verbose)) {
2821 if (verbose > 0)
2822 pr_err("failed to enumerate ports on %s controller at %s.\n",
2823 get_sys_dev_type(hba->type), hba->pci_id);
2824 result |= 2;
2825 }
2826 }
2827 }
2828 printf("\n");
2829 }
2830
2831 return result;
2832 }
2833
2834 static int export_detail_platform_imsm(int verbose, char *controller_path)
2835 {
2836 struct sys_dev *list, *hba;
2837 int result=1;
2838
2839 list = find_intel_devices();
2840 if (!list) {
2841 if (verbose > 0)
2842 pr_err("IMSM_DETAIL_PLATFORM_ERROR=NO_INTEL_DEVICES\n");
2843 result = 2;
2844 return result;
2845 }
2846
2847 for (hba = list; hba; hba = hba->next) {
2848 if (controller_path && (compare_paths(hba->path,controller_path) != 0))
2849 continue;
2850 if (!find_imsm_capability(hba) && verbose > 0) {
2851 char buf[PATH_MAX];
2852 pr_err("IMSM_DETAIL_PLATFORM_ERROR=NO_IMSM_CAPABLE_DEVICE_UNDER_%s\n",
2853 hba->type == SYS_DEV_VMD || hba->type == SYS_DEV_SATA_VMD ?
2854 vmd_domain_to_controller(hba, buf) : hba->path);
2855 }
2856 else
2857 result = 0;
2858 }
2859
2860 const struct orom_entry *entry;
2861
2862 for (entry = orom_entries; entry; entry = entry->next) {
2863 if (entry->type == SYS_DEV_VMD || entry->type == SYS_DEV_SATA_VMD) {
2864 for (hba = list; hba; hba = hba->next)
2865 print_imsm_capability_export(&entry->orom);
2866 continue;
2867 }
2868 print_imsm_capability_export(&entry->orom);
2869 }
2870
2871 return result;
2872 }
2873
2874 static int match_home_imsm(struct supertype *st, char *homehost)
2875 {
2876 /* the imsm metadata format does not specify any host
2877 * identification information. We return -1 since we can never
2878 * confirm nor deny whether a given array is "meant" for this
2879 * host. We rely on compare_super and the 'family_num' fields to
2880 * exclude member disks that do not belong, and we rely on
2881 * mdadm.conf to specify the arrays that should be assembled.
2882 * Auto-assembly may still pick up "foreign" arrays.
2883 */
2884
2885 return -1;
2886 }
2887
2888 static void uuid_from_super_imsm(struct supertype *st, int uuid[4])
2889 {
2890 /* The uuid returned here is used for:
2891 * uuid to put into bitmap file (Create, Grow)
2892 * uuid for backup header when saving critical section (Grow)
2893 * comparing uuids when re-adding a device into an array
2894 * In these cases the uuid required is that of the data-array,
2895 * not the device-set.
2896 * uuid to recognise same set when adding a missing device back
2897 * to an array. This is a uuid for the device-set.
2898 *
2899 * For each of these we can make do with a truncated
2900 * or hashed uuid rather than the original, as long as
2901 * everyone agrees.
2902 * In each case the uuid required is that of the data-array,
2903 * not the device-set.
2904 */
2905 /* imsm does not track uuid's so we synthesis one using sha1 on
2906 * - The signature (Which is constant for all imsm array, but no matter)
2907 * - the orig_family_num of the container
2908 * - the index number of the volume
2909 * - the 'serial' number of the volume.
2910 * Hopefully these are all constant.
2911 */
2912 struct intel_super *super = st->sb;
2913
2914 char buf[20];
2915 struct sha1_ctx ctx;
2916 struct imsm_dev *dev = NULL;
2917 __u32 family_num;
2918
2919 /* some mdadm versions failed to set ->orig_family_num, in which
2920 * case fall back to ->family_num. orig_family_num will be
2921 * fixed up with the first metadata update.
2922 */
2923 family_num = super->anchor->orig_family_num;
2924 if (family_num == 0)
2925 family_num = super->anchor->family_num;
2926 sha1_init_ctx(&ctx);
2927 sha1_process_bytes(super->anchor->sig, MPB_SIG_LEN, &ctx);
2928 sha1_process_bytes(&family_num, sizeof(__u32), &ctx);
2929 if (super->current_vol >= 0)
2930 dev = get_imsm_dev(super, super->current_vol);
2931 if (dev) {
2932 __u32 vol = super->current_vol;
2933 sha1_process_bytes(&vol, sizeof(vol), &ctx);
2934 sha1_process_bytes(dev->volume, MAX_RAID_SERIAL_LEN, &ctx);
2935 }
2936 sha1_finish_ctx(&ctx, buf);
2937 memcpy(uuid, buf, 4*4);
2938 }
2939
2940 static __u32 migr_strip_blocks_resync(struct imsm_dev *dev)
2941 {
2942 /* migr_strip_size when repairing or initializing parity */
2943 struct imsm_map *map = get_imsm_map(dev, MAP_0);
2944 __u32 chunk = __le32_to_cpu(map->blocks_per_strip);
2945
2946 switch (get_imsm_raid_level(map)) {
2947 case 5:
2948 case 10:
2949 return chunk;
2950 default:
2951 return 128*1024 >> 9;
2952 }
2953 }
2954
2955 static __u32 migr_strip_blocks_rebuild(struct imsm_dev *dev)
2956 {
2957 /* migr_strip_size when rebuilding a degraded disk, no idea why
2958 * this is different than migr_strip_size_resync(), but it's good
2959 * to be compatible
2960 */
2961 struct imsm_map *map = get_imsm_map(dev, MAP_1);
2962 __u32 chunk = __le32_to_cpu(map->blocks_per_strip);
2963
2964 switch (get_imsm_raid_level(map)) {
2965 case 1:
2966 case 10:
2967 if (map->num_members % map->num_domains == 0)
2968 return 128*1024 >> 9;
2969 else
2970 return chunk;
2971 case 5:
2972 return max((__u32) 64*1024 >> 9, chunk);
2973 default:
2974 return 128*1024 >> 9;
2975 }
2976 }
2977
2978 static __u32 num_stripes_per_unit_resync(struct imsm_dev *dev)
2979 {
2980 struct imsm_map *lo = get_imsm_map(dev, MAP_0);
2981 struct imsm_map *hi = get_imsm_map(dev, MAP_1);
2982 __u32 lo_chunk = __le32_to_cpu(lo->blocks_per_strip);
2983 __u32 hi_chunk = __le32_to_cpu(hi->blocks_per_strip);
2984
2985 return max((__u32) 1, hi_chunk / lo_chunk);
2986 }
2987
2988 static __u32 num_stripes_per_unit_rebuild(struct imsm_dev *dev)
2989 {
2990 struct imsm_map *lo = get_imsm_map(dev, MAP_0);
2991 int level = get_imsm_raid_level(lo);
2992
2993 if (level == 1 || level == 10) {
2994 struct imsm_map *hi = get_imsm_map(dev, MAP_1);
2995
2996 return hi->num_domains;
2997 } else
2998 return num_stripes_per_unit_resync(dev);
2999 }
3000
3001 static unsigned long long calc_component_size(struct imsm_map *map,
3002 struct imsm_dev *dev)
3003 {
3004 unsigned long long component_size;
3005 unsigned long long dev_size = imsm_dev_size(dev);
3006 long long calc_dev_size = 0;
3007 unsigned int member_disks = imsm_num_data_members(map);
3008
3009 if (member_disks == 0)
3010 return 0;
3011
3012 component_size = per_dev_array_size(map);
3013 calc_dev_size = component_size * member_disks;
3014
3015 /* Component size is rounded to 1MB so difference between size from
3016 * metadata and size calculated from num_data_stripes equals up to
3017 * 2048 blocks per each device. If the difference is higher it means
3018 * that array size was expanded and num_data_stripes was not updated.
3019 */
3020 if (llabs(calc_dev_size - (long long)dev_size) >
3021 (1 << SECT_PER_MB_SHIFT) * member_disks) {
3022 component_size = dev_size / member_disks;
3023 dprintf("Invalid num_data_stripes in metadata; expected=%llu, found=%llu\n",
3024 component_size / map->blocks_per_strip,
3025 num_data_stripes(map));
3026 }
3027
3028 return component_size;
3029 }
3030
3031 static __u32 parity_segment_depth(struct imsm_dev *dev)
3032 {
3033 struct imsm_map *map = get_imsm_map(dev, MAP_0);
3034 __u32 chunk = __le32_to_cpu(map->blocks_per_strip);
3035
3036 switch(get_imsm_raid_level(map)) {
3037 case 1:
3038 case 10:
3039 return chunk * map->num_domains;
3040 case 5:
3041 return chunk * map->num_members;
3042 default:
3043 return chunk;
3044 }
3045 }
3046
3047 static __u32 map_migr_block(struct imsm_dev *dev, __u32 block)
3048 {
3049 struct imsm_map *map = get_imsm_map(dev, MAP_1);
3050 __u32 chunk = __le32_to_cpu(map->blocks_per_strip);
3051 __u32 strip = block / chunk;
3052
3053 switch (get_imsm_raid_level(map)) {
3054 case 1:
3055 case 10: {
3056 __u32 vol_strip = (strip * map->num_domains) + 1;
3057 __u32 vol_stripe = vol_strip / map->num_members;
3058
3059 return vol_stripe * chunk + block % chunk;
3060 } case 5: {
3061 __u32 stripe = strip / (map->num_members - 1);
3062
3063 return stripe * chunk + block % chunk;
3064 }
3065 default:
3066 return 0;
3067 }
3068 }
3069
3070 static __u64 blocks_per_migr_unit(struct intel_super *super,
3071 struct imsm_dev *dev)
3072 {
3073 /* calculate the conversion factor between per member 'blocks'
3074 * (md/{resync,rebuild}_start) and imsm migration units, return
3075 * 0 for the 'not migrating' and 'unsupported migration' cases
3076 */
3077 if (!dev->vol.migr_state)
3078 return 0;
3079
3080 switch (migr_type(dev)) {
3081 case MIGR_GEN_MIGR: {
3082 struct migr_record *migr_rec = super->migr_rec;
3083 return __le32_to_cpu(migr_rec->blocks_per_unit);
3084 }
3085 case MIGR_VERIFY:
3086 case MIGR_REPAIR:
3087 case MIGR_INIT: {
3088 struct imsm_map *map = get_imsm_map(dev, MAP_0);
3089 __u32 stripes_per_unit;
3090 __u32 blocks_per_unit;
3091 __u32 parity_depth;
3092 __u32 migr_chunk;
3093 __u32 block_map;
3094 __u32 block_rel;
3095 __u32 segment;
3096 __u32 stripe;
3097 __u8 disks;
3098
3099 /* yes, this is really the translation of migr_units to
3100 * per-member blocks in the 'resync' case
3101 */
3102 stripes_per_unit = num_stripes_per_unit_resync(dev);
3103 migr_chunk = migr_strip_blocks_resync(dev);
3104 disks = imsm_num_data_members(map);
3105 blocks_per_unit = stripes_per_unit * migr_chunk * disks;
3106 stripe = __le16_to_cpu(map->blocks_per_strip) * disks;
3107 segment = blocks_per_unit / stripe;
3108 block_rel = blocks_per_unit - segment * stripe;
3109 parity_depth = parity_segment_depth(dev);
3110 block_map = map_migr_block(dev, block_rel);
3111 return block_map + parity_depth * segment;
3112 }
3113 case MIGR_REBUILD: {
3114 __u32 stripes_per_unit;
3115 __u32 migr_chunk;
3116
3117 stripes_per_unit = num_stripes_per_unit_rebuild(dev);
3118 migr_chunk = migr_strip_blocks_rebuild(dev);
3119 return migr_chunk * stripes_per_unit;
3120 }
3121 case MIGR_STATE_CHANGE:
3122 default:
3123 return 0;
3124 }
3125 }
3126
3127 static int imsm_level_to_layout(int level)
3128 {
3129 switch (level) {
3130 case 0:
3131 case 1:
3132 return 0;
3133 case 5:
3134 case 6:
3135 return ALGORITHM_LEFT_ASYMMETRIC;
3136 case 10:
3137 return 0x102;
3138 }
3139 return UnSet;
3140 }
3141
3142 /*******************************************************************************
3143 * Function: read_imsm_migr_rec
3144 * Description: Function reads imsm migration record from last sector of disk
3145 * Parameters:
3146 * fd : disk descriptor
3147 * super : metadata info
3148 * Returns:
3149 * 0 : success,
3150 * -1 : fail
3151 ******************************************************************************/
3152 static int read_imsm_migr_rec(int fd, struct intel_super *super)
3153 {
3154 int ret_val = -1;
3155 unsigned int sector_size = super->sector_size;
3156 unsigned long long dsize;
3157
3158 get_dev_size(fd, NULL, &dsize);
3159 if (lseek64(fd, dsize - (sector_size*MIGR_REC_SECTOR_POSITION),
3160 SEEK_SET) < 0) {
3161 pr_err("Cannot seek to anchor block: %s\n",
3162 strerror(errno));
3163 goto out;
3164 }
3165 if ((unsigned int)read(fd, super->migr_rec_buf,
3166 MIGR_REC_BUF_SECTORS*sector_size) !=
3167 MIGR_REC_BUF_SECTORS*sector_size) {
3168 pr_err("Cannot read migr record block: %s\n",
3169 strerror(errno));
3170 goto out;
3171 }
3172 ret_val = 0;
3173 if (sector_size == 4096)
3174 convert_from_4k_imsm_migr_rec(super);
3175
3176 out:
3177 return ret_val;
3178 }
3179
3180 static struct imsm_dev *imsm_get_device_during_migration(
3181 struct intel_super *super)
3182 {
3183
3184 struct intel_dev *dv;
3185
3186 for (dv = super->devlist; dv; dv = dv->next) {
3187 if (is_gen_migration(dv->dev))
3188 return dv->dev;
3189 }
3190 return NULL;
3191 }
3192
3193 /*******************************************************************************
3194 * Function: load_imsm_migr_rec
3195 * Description: Function reads imsm migration record (it is stored at the last
3196 * sector of disk)
3197 * Parameters:
3198 * super : imsm internal array info
3199 * Returns:
3200 * 0 : success
3201 * -1 : fail
3202 * -2 : no migration in progress
3203 ******************************************************************************/
3204 static int load_imsm_migr_rec(struct intel_super *super)
3205 {
3206 struct dl *dl;
3207 char nm[30];
3208 int retval = -1;
3209 int fd = -1;
3210 struct imsm_dev *dev;
3211 struct imsm_map *map;
3212 int slot = -1;
3213 int keep_fd = 1;
3214
3215 /* find map under migration */
3216 dev = imsm_get_device_during_migration(super);
3217 /* nothing to load,no migration in progress?
3218 */
3219 if (dev == NULL)
3220 return -2;
3221
3222 map = get_imsm_map(dev, MAP_0);
3223 if (!map)
3224 return -1;
3225
3226 for (dl = super->disks; dl; dl = dl->next) {
3227 /* skip spare and failed disks
3228 */
3229 if (dl->index < 0)
3230 continue;
3231 /* read only from one of the first two slots
3232 */
3233 slot = get_imsm_disk_slot(map, dl->index);
3234 if (slot > 1 || slot < 0)
3235 continue;
3236
3237 if (!is_fd_valid(dl->fd)) {
3238 sprintf(nm, "%d:%d", dl->major, dl->minor);
3239 fd = dev_open(nm, O_RDONLY);
3240
3241 if (is_fd_valid(fd)) {
3242 keep_fd = 0;
3243 break;
3244 }
3245 } else {
3246 fd = dl->fd;
3247 break;
3248 }
3249 }
3250
3251 if (!is_fd_valid(fd))
3252 return retval;
3253 retval = read_imsm_migr_rec(fd, super);
3254 if (!keep_fd)
3255 close(fd);
3256
3257 return retval;
3258 }
3259
3260 /*******************************************************************************
3261 * function: imsm_create_metadata_checkpoint_update
3262 * Description: It creates update for checkpoint change.
3263 * Parameters:
3264 * super : imsm internal array info
3265 * u : pointer to prepared update
3266 * Returns:
3267 * Uptate length.
3268 * If length is equal to 0, input pointer u contains no update
3269 ******************************************************************************/
3270 static int imsm_create_metadata_checkpoint_update(
3271 struct intel_super *super,
3272 struct imsm_update_general_migration_checkpoint **u)
3273 {
3274
3275 int update_memory_size = 0;
3276
3277 dprintf("(enter)\n");
3278
3279 if (u == NULL)
3280 return 0;
3281 *u = NULL;
3282
3283 /* size of all update data without anchor */
3284 update_memory_size =
3285 sizeof(struct imsm_update_general_migration_checkpoint);
3286
3287 *u = xcalloc(1, update_memory_size);
3288 if (*u == NULL) {
3289 dprintf("error: cannot get memory\n");
3290 return 0;
3291 }
3292 (*u)->type = update_general_migration_checkpoint;
3293 (*u)->curr_migr_unit = current_migr_unit(super->migr_rec);
3294 dprintf("prepared for %llu\n", (unsigned long long)(*u)->curr_migr_unit);
3295
3296 return update_memory_size;
3297 }
3298
3299 static void imsm_update_metadata_locally(struct supertype *st,
3300 void *buf, int len);
3301
3302 /*******************************************************************************
3303 * Function: write_imsm_migr_rec
3304 * Description: Function writes imsm migration record
3305 * (at the last sector of disk)
3306 * Parameters:
3307 * super : imsm internal array info
3308 * Returns:
3309 * 0 : success
3310 * -1 : if fail
3311 ******************************************************************************/
3312 static int write_imsm_migr_rec(struct supertype *st)
3313 {
3314 struct intel_super *super = st->sb;
3315 unsigned int sector_size = super->sector_size;
3316 unsigned long long dsize;
3317 int retval = -1;
3318 struct dl *sd;
3319 int len;
3320 struct imsm_update_general_migration_checkpoint *u;
3321 struct imsm_dev *dev;
3322 struct imsm_map *map;
3323
3324 /* find map under migration */
3325 dev = imsm_get_device_during_migration(super);
3326 /* if no migration, write buffer anyway to clear migr_record
3327 * on disk based on first available device
3328 */
3329 if (dev == NULL)
3330 dev = get_imsm_dev(super, super->current_vol < 0 ? 0 :
3331 super->current_vol);
3332
3333 map = get_imsm_map(dev, MAP_0);
3334
3335 if (sector_size == 4096)
3336 convert_to_4k_imsm_migr_rec(super);
3337 for (sd = super->disks ; sd ; sd = sd->next) {
3338 int slot = -1;
3339
3340 /* skip failed and spare devices */
3341 if (sd->index < 0)
3342 continue;
3343 /* write to 2 first slots only */
3344 if (map)
3345 slot = get_imsm_disk_slot(map, sd->index);
3346 if (map == NULL || slot > 1 || slot < 0)
3347 continue;
3348
3349 get_dev_size(sd->fd, NULL, &dsize);
3350 if (lseek64(sd->fd, dsize - (MIGR_REC_SECTOR_POSITION *
3351 sector_size),
3352 SEEK_SET) < 0) {
3353 pr_err("Cannot seek to anchor block: %s\n",
3354 strerror(errno));
3355 goto out;
3356 }
3357 if ((unsigned int)write(sd->fd, super->migr_rec_buf,
3358 MIGR_REC_BUF_SECTORS*sector_size) !=
3359 MIGR_REC_BUF_SECTORS*sector_size) {
3360 pr_err("Cannot write migr record block: %s\n",
3361 strerror(errno));
3362 goto out;
3363 }
3364 }
3365 if (sector_size == 4096)
3366 convert_from_4k_imsm_migr_rec(super);
3367 /* update checkpoint information in metadata */
3368 len = imsm_create_metadata_checkpoint_update(super, &u);
3369 if (len <= 0) {
3370 dprintf("imsm: Cannot prepare update\n");
3371 goto out;
3372 }
3373 /* update metadata locally */
3374 imsm_update_metadata_locally(st, u, len);
3375 /* and possibly remotely */
3376 if (st->update_tail) {
3377 append_metadata_update(st, u, len);
3378 /* during reshape we do all work inside metadata handler
3379 * manage_reshape(), so metadata update has to be triggered
3380 * insida it
3381 */
3382 flush_metadata_updates(st);
3383 st->update_tail = &st->updates;
3384 } else
3385 free(u);
3386
3387 retval = 0;
3388 out:
3389 return retval;
3390 }
3391
3392 /* spare/missing disks activations are not allowe when
3393 * array/container performs reshape operation, because
3394 * all arrays in container works on the same disks set
3395 */
3396 int imsm_reshape_blocks_arrays_changes(struct intel_super *super)
3397 {
3398 int rv = 0;
3399 struct intel_dev *i_dev;
3400 struct imsm_dev *dev;
3401
3402 /* check whole container
3403 */
3404 for (i_dev = super->devlist; i_dev; i_dev = i_dev->next) {
3405 dev = i_dev->dev;
3406 if (is_gen_migration(dev)) {
3407 /* No repair during any migration in container
3408 */
3409 rv = 1;
3410 break;
3411 }
3412 }
3413 return rv;
3414 }
3415 static unsigned long long imsm_component_size_alignment_check(int level,
3416 int chunk_size,
3417 unsigned int sector_size,
3418 unsigned long long component_size)
3419 {
3420 unsigned int component_size_alignment;
3421
3422 /* check component size alignment
3423 */
3424 component_size_alignment = component_size % (chunk_size/sector_size);
3425
3426 dprintf("(Level: %i, chunk_size = %i, component_size = %llu), component_size_alignment = %u\n",
3427 level, chunk_size, component_size,
3428 component_size_alignment);
3429
3430 if (component_size_alignment && (level != 1) && (level != UnSet)) {
3431 dprintf("imsm: reported component size aligned from %llu ",
3432 component_size);
3433 component_size -= component_size_alignment;
3434 dprintf_cont("to %llu (%i).\n",
3435 component_size, component_size_alignment);
3436 }
3437
3438 return component_size;
3439 }
3440
3441 /*******************************************************************************
3442 * Function: get_bitmap_header_sector
3443 * Description: Returns the sector where the bitmap header is placed.
3444 * Parameters:
3445 * st : supertype information
3446 * dev_idx : index of the device with bitmap
3447 *
3448 * Returns:
3449 * The sector where the bitmap header is placed
3450 ******************************************************************************/
3451 static unsigned long long get_bitmap_header_sector(struct intel_super *super,
3452 int dev_idx)
3453 {
3454 struct imsm_dev *dev = get_imsm_dev(super, dev_idx);
3455 struct imsm_map *map = get_imsm_map(dev, MAP_0);
3456
3457 if (!super->sector_size) {
3458 dprintf("sector size is not set\n");
3459 return 0;
3460 }
3461
3462 return pba_of_lba0(map) + calc_component_size(map, dev) +
3463 (IMSM_BITMAP_HEADER_OFFSET / super->sector_size);
3464 }
3465
3466 /*******************************************************************************
3467 * Function: get_bitmap_sector
3468 * Description: Returns the sector where the bitmap is placed.
3469 * Parameters:
3470 * st : supertype information
3471 * dev_idx : index of the device with bitmap
3472 *
3473 * Returns:
3474 * The sector where the bitmap is placed
3475 ******************************************************************************/
3476 static unsigned long long get_bitmap_sector(struct intel_super *super,
3477 int dev_idx)
3478 {
3479 if (!super->sector_size) {
3480 dprintf("sector size is not set\n");
3481 return 0;
3482 }
3483
3484 return get_bitmap_header_sector(super, dev_idx) +
3485 (IMSM_BITMAP_HEADER_SIZE / super->sector_size);
3486 }
3487
3488 static unsigned long long get_ppl_sector(struct intel_super *super, int dev_idx)
3489 {
3490 struct imsm_dev *dev = get_imsm_dev(super, dev_idx);
3491 struct imsm_map *map = get_imsm_map(dev, MAP_0);
3492
3493 return pba_of_lba0(map) +
3494 (num_data_stripes(map) * map->blocks_per_strip);
3495 }
3496
3497 static void getinfo_super_imsm_volume(struct supertype *st, struct mdinfo *info, char *dmap)
3498 {
3499 struct intel_super *super = st->sb;
3500 struct migr_record *migr_rec = super->migr_rec;
3501 struct imsm_dev *dev = get_imsm_dev(super, super->current_vol);
3502 struct imsm_map *map = get_imsm_map(dev, MAP_0);
3503 struct imsm_map *prev_map = get_imsm_map(dev, MAP_1);
3504 struct imsm_map *map_to_analyse = map;
3505 struct dl *dl;
3506 int map_disks = info->array.raid_disks;
3507
3508 memset(info, 0, sizeof(*info));
3509 if (prev_map)
3510 map_to_analyse = prev_map;
3511
3512 dl = super->current_disk;
3513
3514 info->container_member = super->current_vol;
3515 info->array.raid_disks = map->num_members;
3516 info->array.level = get_imsm_raid_level(map_to_analyse);
3517 info->array.layout = imsm_level_to_layout(info->array.level);
3518 info->array.md_minor = -1;
3519 info->array.ctime = 0;
3520 info->array.utime = 0;
3521 info->array.chunk_size =
3522 __le16_to_cpu(map_to_analyse->blocks_per_strip) << 9;
3523 info->array.state = !(dev->vol.dirty & RAIDVOL_DIRTY);
3524 info->custom_array_size = imsm_dev_size(dev);
3525 info->recovery_blocked = imsm_reshape_blocks_arrays_changes(st->sb);
3526
3527 if (is_gen_migration(dev)) {
3528 /*
3529 * device prev_map should be added if it is in the middle
3530 * of migration
3531 */
3532 assert(prev_map);
3533
3534 info->reshape_active = 1;
3535 info->new_level = get_imsm_raid_level(map);
3536 info->new_layout = imsm_level_to_layout(info->new_level);
3537 info->new_chunk = __le16_to_cpu(map->blocks_per_strip) << 9;
3538 info->delta_disks = map->num_members - prev_map->num_members;
3539 if (info->delta_disks) {
3540 /* this needs to be applied to every array
3541 * in the container.
3542 */
3543 info->reshape_active = CONTAINER_RESHAPE;
3544 }
3545 /* We shape information that we give to md might have to be
3546 * modify to cope with md's requirement for reshaping arrays.
3547 * For example, when reshaping a RAID0, md requires it to be
3548 * presented as a degraded RAID4.
3549 * Also if a RAID0 is migrating to a RAID5 we need to specify
3550 * the array as already being RAID5, but the 'before' layout
3551 * is a RAID4-like layout.
3552 */
3553 switch (info->array.level) {
3554 case 0:
3555 switch(info->new_level) {
3556 case 0:
3557 /* conversion is happening as RAID4 */
3558 info->array.level = 4;
3559 info->array.raid_disks += 1;
3560 break;
3561 case 5:
3562 /* conversion is happening as RAID5 */
3563 info->array.level = 5;
3564 info->array.layout = ALGORITHM_PARITY_N;
3565 info->delta_disks -= 1;
3566 break;
3567 default:
3568 /* FIXME error message */
3569 info->array.level = UnSet;
3570 break;
3571 }
3572 break;
3573 }
3574 } else {
3575 info->new_level = UnSet;
3576 info->new_layout = UnSet;
3577 info->new_chunk = info->array.chunk_size;
3578 info->delta_disks = 0;
3579 }
3580
3581 if (dl) {
3582 info->disk.major = dl->major;
3583 info->disk.minor = dl->minor;
3584 info->disk.number = dl->index;
3585 info->disk.raid_disk = get_imsm_disk_slot(map_to_analyse,
3586 dl->index);
3587 }
3588
3589 info->data_offset = pba_of_lba0(map_to_analyse);
3590 info->component_size = calc_component_size(map, dev);
3591 info->component_size = imsm_component_size_alignment_check(
3592 info->array.level,
3593 info->array.chunk_size,
3594 super->sector_size,
3595 info->component_size);
3596 info->bb.supported = 1;
3597
3598 memset(info->uuid, 0, sizeof(info->uuid));
3599 info->recovery_start = MaxSector;
3600
3601 if (info->array.level == 5 &&
3602 (dev->rwh_policy == RWH_DISTRIBUTED ||
3603 dev->rwh_policy == RWH_MULTIPLE_DISTRIBUTED)) {
3604 info->consistency_policy = CONSISTENCY_POLICY_PPL;
3605 info->ppl_sector = get_ppl_sector(super, super->current_vol);
3606 if (dev->rwh_policy == RWH_MULTIPLE_DISTRIBUTED)
3607 info->ppl_size = MULTIPLE_PPL_AREA_SIZE_IMSM >> 9;
3608 else
3609 info->ppl_size = (PPL_HEADER_SIZE + PPL_ENTRY_SPACE)
3610 >> 9;
3611 } else if (info->array.level <= 0) {
3612 info->consistency_policy = CONSISTENCY_POLICY_NONE;
3613 } else {
3614 if (dev->rwh_policy == RWH_BITMAP) {
3615 info->bitmap_offset = get_bitmap_sector(super, super->current_vol);
3616 info->consistency_policy = CONSISTENCY_POLICY_BITMAP;
3617 } else {
3618 info->consistency_policy = CONSISTENCY_POLICY_RESYNC;
3619 }
3620 }
3621
3622 info->reshape_progress = 0;
3623 info->resync_start = MaxSector;
3624 if ((map_to_analyse->map_state == IMSM_T_STATE_UNINITIALIZED ||
3625 !(info->array.state & 1)) &&
3626 imsm_reshape_blocks_arrays_changes(super) == 0) {
3627 info->resync_start = 0;
3628 }
3629 if (dev->vol.migr_state) {
3630 switch (migr_type(dev)) {
3631 case MIGR_REPAIR:
3632 case MIGR_INIT: {
3633 __u64 blocks_per_unit = blocks_per_migr_unit(super,
3634 dev);
3635 __u64 units = vol_curr_migr_unit(dev);
3636
3637 info->resync_start = blocks_per_unit * units;
3638 break;
3639 }
3640 case MIGR_GEN_MIGR: {
3641 __u64 blocks_per_unit = blocks_per_migr_unit(super,
3642 dev);
3643 __u64 units = current_migr_unit(migr_rec);
3644 int used_disks;
3645
3646 if (__le32_to_cpu(migr_rec->ascending_migr) &&
3647 (units <
3648 (get_num_migr_units(migr_rec)-1)) &&
3649 (super->migr_rec->rec_status ==
3650 __cpu_to_le32(UNIT_SRC_IN_CP_AREA)))
3651 units++;
3652
3653 info->reshape_progress = blocks_per_unit * units;
3654
3655 dprintf("IMSM: General Migration checkpoint : %llu (%llu) -> read reshape progress : %llu\n",
3656 (unsigned long long)units,
3657 (unsigned long long)blocks_per_unit,
3658 info->reshape_progress);
3659
3660 used_disks = imsm_num_data_members(prev_map);
3661 if (used_disks > 0) {
3662 info->custom_array_size = per_dev_array_size(map) *
3663 used_disks;
3664 }
3665 }
3666 case MIGR_VERIFY:
3667 /* we could emulate the checkpointing of
3668 * 'sync_action=check' migrations, but for now
3669 * we just immediately complete them
3670 */
3671 case MIGR_REBUILD:
3672 /* this is handled by container_content_imsm() */
3673 case MIGR_STATE_CHANGE:
3674 /* FIXME handle other migrations */
3675 default:
3676 /* we are not dirty, so... */
3677 info->resync_start = MaxSector;
3678 }
3679 }
3680
3681 strncpy(info->name, (char *) dev->volume, MAX_RAID_SERIAL_LEN);
3682 info->name[MAX_RAID_SERIAL_LEN] = 0;
3683
3684 info->array.major_version = -1;
3685 info->array.minor_version = -2;
3686 sprintf(info->text_version, "/%s/%d", st->container_devnm, info->container_member);
3687 info->safe_mode_delay = 4000; /* 4 secs like the Matrix driver */
3688 uuid_from_super_imsm(st, info->uuid);
3689
3690 if (dmap) {
3691 int i, j;
3692 for (i=0; i<map_disks; i++) {
3693 dmap[i] = 0;
3694 if (i < info->array.raid_disks) {
3695 struct imsm_disk *dsk;
3696 j = get_imsm_disk_idx(dev, i, MAP_X);
3697 dsk = get_imsm_disk(super, j);
3698 if (dsk && (dsk->status & CONFIGURED_DISK))
3699 dmap[i] = 1;
3700 }
3701 }
3702 }
3703 }
3704
3705 static __u8 imsm_check_degraded(struct intel_super *super, struct imsm_dev *dev,
3706 int failed, int look_in_map);
3707
3708 static int imsm_count_failed(struct intel_super *super, struct imsm_dev *dev,
3709 int look_in_map);
3710
3711 static void manage_second_map(struct intel_super *super, struct imsm_dev *dev)
3712 {
3713 if (is_gen_migration(dev)) {
3714 int failed;
3715 __u8 map_state;
3716 struct imsm_map *map2 = get_imsm_map(dev, MAP_1);
3717
3718 failed = imsm_count_failed(super, dev, MAP_1);
3719 map_state = imsm_check_degraded(super, dev, failed, MAP_1);
3720 if (map2->map_state != map_state) {
3721 map2->map_state = map_state;
3722 super->updates_pending++;
3723 }
3724 }
3725 }
3726
3727 static struct imsm_disk *get_imsm_missing(struct intel_super *super, __u8 index)
3728 {
3729 struct dl *d;
3730
3731 for (d = super->missing; d; d = d->next)
3732 if (d->index == index)
3733 return &d->disk;
3734 return NULL;
3735 }
3736
3737 static void getinfo_super_imsm(struct supertype *st, struct mdinfo *info, char *map)
3738 {
3739 struct intel_super *super = st->sb;
3740 struct imsm_disk *disk;
3741 int map_disks = info->array.raid_disks;
3742 int max_enough = -1;
3743 int i;
3744 struct imsm_super *mpb;
3745
3746 if (super->current_vol >= 0) {
3747 getinfo_super_imsm_volume(st, info, map);
3748 return;
3749 }
3750 memset(info, 0, sizeof(*info));
3751
3752 /* Set raid_disks to zero so that Assemble will always pull in valid
3753 * spares
3754 */
3755 info->array.raid_disks = 0;
3756 info->array.level = LEVEL_CONTAINER;
3757 info->array.layout = 0;
3758 info->array.md_minor = -1;
3759 info->array.ctime = 0; /* N/A for imsm */
3760 info->array.utime = 0;
3761 info->array.chunk_size = 0;
3762
3763 info->disk.major = 0;
3764 info->disk.minor = 0;
3765 info->disk.raid_disk = -1;
3766 info->reshape_active = 0;
3767 info->array.major_version = -1;
3768 info->array.minor_version = -2;
3769 strcpy(info->text_version, "imsm");
3770 info->safe_mode_delay = 0;
3771 info->disk.number = -1;
3772 info->disk.state = 0;
3773 info->name[0] = 0;
3774 info->recovery_start = MaxSector;
3775 info->recovery_blocked = imsm_reshape_blocks_arrays_changes(st->sb);
3776 info->bb.supported = 1;
3777
3778 /* do we have the all the insync disks that we expect? */
3779 mpb = super->anchor;
3780 info->events = __le32_to_cpu(mpb->generation_num);
3781
3782 for (i = 0; i < mpb->num_raid_devs; i++) {
3783 struct imsm_dev *dev = get_imsm_dev(super, i);
3784 int failed, enough, j, missing = 0;
3785 struct imsm_map *map;
3786 __u8 state;
3787
3788 failed = imsm_count_failed(super, dev, MAP_0);
3789 state = imsm_check_degraded(super, dev, failed, MAP_0);
3790 map = get_imsm_map(dev, MAP_0);
3791
3792 /* any newly missing disks?
3793 * (catches single-degraded vs double-degraded)
3794 */
3795 for (j = 0; j < map->num_members; j++) {
3796 __u32 ord = get_imsm_ord_tbl_ent(dev, j, MAP_0);
3797 __u32 idx = ord_to_idx(ord);
3798
3799 if (super->disks && super->disks->index == (int)idx)
3800 info->disk.raid_disk = j;
3801
3802 if (!(ord & IMSM_ORD_REBUILD) &&
3803 get_imsm_missing(super, idx)) {
3804 missing = 1;
3805 break;
3806 }
3807 }
3808
3809 if (state == IMSM_T_STATE_FAILED)
3810 enough = -1;
3811 else if (state == IMSM_T_STATE_DEGRADED &&
3812 (state != map->map_state || missing))
3813 enough = 0;
3814 else /* we're normal, or already degraded */
3815 enough = 1;
3816 if (is_gen_migration(dev) && missing) {
3817 /* during general migration we need all disks
3818 * that process is running on.
3819 * No new missing disk is allowed.
3820 */
3821 max_enough = -1;
3822 enough = -1;
3823 /* no more checks necessary
3824 */
3825 break;
3826 }
3827 /* in the missing/failed disk case check to see
3828 * if at least one array is runnable
3829 */
3830 max_enough = max(max_enough, enough);
3831 }
3832
3833 info->container_enough = max_enough;
3834
3835 if (super->disks) {
3836 __u32 reserved = imsm_reserved_sectors(super, super->disks);
3837
3838 disk = &super->disks->disk;
3839 info->data_offset = total_blocks(&super->disks->disk) - reserved;
3840 info->component_size = reserved;
3841 info->disk.state = is_configured(disk) ? (1 << MD_DISK_ACTIVE) : 0;
3842 /* we don't change info->disk.raid_disk here because
3843 * this state will be finalized in mdmon after we have
3844 * found the 'most fresh' version of the metadata
3845 */
3846 info->disk.state |= is_failed(disk) ? (1 << MD_DISK_FAULTY) : 0;
3847 info->disk.state |= (is_spare(disk) || is_journal(disk)) ?
3848 0 : (1 << MD_DISK_SYNC);
3849 }
3850
3851 /* only call uuid_from_super_imsm when this disk is part of a populated container,
3852 * ->compare_super may have updated the 'num_raid_devs' field for spares
3853 */
3854 if (info->disk.state & (1 << MD_DISK_SYNC) || super->anchor->num_raid_devs)
3855 uuid_from_super_imsm(st, info->uuid);
3856 else
3857 memcpy(info->uuid, uuid_zero, sizeof(uuid_zero));
3858
3859 /* I don't know how to compute 'map' on imsm, so use safe default */
3860 if (map) {
3861 int i;
3862 for (i = 0; i < map_disks; i++)
3863 map[i] = 1;
3864 }
3865
3866 }
3867
3868 /* allocates memory and fills disk in mdinfo structure
3869 * for each disk in array */
3870 struct mdinfo *getinfo_super_disks_imsm(struct supertype *st)
3871 {
3872 struct mdinfo *mddev;
3873 struct intel_super *super = st->sb;
3874 struct imsm_disk *disk;
3875 int count = 0;
3876 struct dl *dl;
3877 if (!super || !super->disks)
3878 return NULL;
3879 dl = super->disks;
3880 mddev = xcalloc(1, sizeof(*mddev));
3881 while (dl) {
3882 struct mdinfo *tmp;
3883 disk = &dl->disk;
3884 tmp = xcalloc(1, sizeof(*tmp));
3885 if (mddev->devs)
3886 tmp->next = mddev->devs;
3887 mddev->devs = tmp;
3888 tmp->disk.number = count++;
3889 tmp->disk.major = dl->major;
3890 tmp->disk.minor = dl->minor;
3891 tmp->disk.state = is_configured(disk) ?
3892 (1 << MD_DISK_ACTIVE) : 0;
3893 tmp->disk.state |= is_failed(disk) ? (1 << MD_DISK_FAULTY) : 0;
3894 tmp->disk.state |= is_spare(disk) ? 0 : (1 << MD_DISK_SYNC);
3895 tmp->disk.raid_disk = -1;
3896 dl = dl->next;
3897 }
3898 return mddev;
3899 }
3900
3901 static int update_super_imsm(struct supertype *st, struct mdinfo *info,
3902 enum update_opt update, char *devname,
3903 int verbose, int uuid_set, char *homehost)
3904 {
3905 /* For 'assemble' and 'force' we need to return non-zero if any
3906 * change was made. For others, the return value is ignored.
3907 * Update options are:
3908 * force-one : This device looks a bit old but needs to be included,
3909 * update age info appropriately.
3910 * assemble: clear any 'faulty' flag to allow this device to
3911 * be assembled.
3912 * force-array: Array is degraded but being forced, mark it clean
3913 * if that will be needed to assemble it.
3914 *
3915 * newdev: not used ????
3916 * grow: Array has gained a new device - this is currently for
3917 * linear only
3918 * resync: mark as dirty so a resync will happen.
3919 * name: update the name - preserving the homehost
3920 * uuid: Change the uuid of the array to match watch is given
3921 *
3922 * Following are not relevant for this imsm:
3923 * sparc2.2 : update from old dodgey metadata
3924 * super-minor: change the preferred_minor number
3925 * summaries: update redundant counters.
3926 * homehost: update the recorded homehost
3927 * _reshape_progress: record new reshape_progress position.
3928 */
3929 int rv = 1;
3930 struct intel_super *super = st->sb;
3931 struct imsm_super *mpb;
3932
3933 /* we can only update container info */
3934 if (!super || super->current_vol >= 0 || !super->anchor)
3935 return 1;
3936
3937 mpb = super->anchor;
3938
3939 switch (update) {
3940 case UOPT_UUID:
3941 /* We take this to mean that the family_num should be updated.
3942 * However that is much smaller than the uuid so we cannot really
3943 * allow an explicit uuid to be given. And it is hard to reliably
3944 * know if one was.
3945 * So if !uuid_set we know the current uuid is random and just used
3946 * the first 'int' and copy it to the other 3 positions.
3947 * Otherwise we require the 4 'int's to be the same as would be the
3948 * case if we are using a random uuid. So an explicit uuid will be
3949 * accepted as long as all for ints are the same... which shouldn't hurt
3950 */
3951 if (!uuid_set) {
3952 info->uuid[1] = info->uuid[2] = info->uuid[3] = info->uuid[0];
3953 rv = 0;
3954 } else {
3955 if (info->uuid[0] != info->uuid[1] ||
3956 info->uuid[1] != info->uuid[2] ||
3957 info->uuid[2] != info->uuid[3])
3958 rv = -1;
3959 else
3960 rv = 0;
3961 }
3962 if (rv == 0)
3963 mpb->orig_family_num = info->uuid[0];
3964 break;
3965 case UOPT_SPEC_ASSEMBLE:
3966 rv = 0;
3967 break;
3968 default:
3969 rv = -1;
3970 break;
3971 }
3972
3973 /* successful update? recompute checksum */
3974 if (rv == 0)
3975 mpb->check_sum = __le32_to_cpu(__gen_imsm_checksum(mpb));
3976
3977 return rv;
3978 }
3979
3980 static size_t disks_to_mpb_size(int disks)
3981 {
3982 size_t size;
3983
3984 size = sizeof(struct imsm_super);
3985 size += (disks - 1) * sizeof(struct imsm_disk);
3986 size += 2 * sizeof(struct imsm_dev);
3987 /* up to 2 maps per raid device (-2 for imsm_maps in imsm_dev */
3988 size += (4 - 2) * sizeof(struct imsm_map);
3989 /* 4 possible disk_ord_tbl's */
3990 size += 4 * (disks - 1) * sizeof(__u32);
3991 /* maximum bbm log */
3992 size += sizeof(struct bbm_log);
3993
3994 return size;
3995 }
3996
3997 static __u64 avail_size_imsm(struct supertype *st, __u64 devsize,
3998 unsigned long long data_offset)
3999 {
4000 if (devsize < (MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS))
4001 return 0;
4002
4003 return devsize - (MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS);
4004 }
4005
4006 static void free_devlist(struct intel_super *super)
4007 {
4008 struct intel_dev *dv;
4009
4010 while (super->devlist) {
4011 dv = super->devlist->next;
4012 free(super->devlist->dev);
4013 free(super->devlist);
4014 super->devlist = dv;
4015 }
4016 }
4017
4018 static void imsm_copy_dev(struct imsm_dev *dest, struct imsm_dev *src)
4019 {
4020 memcpy(dest, src, sizeof_imsm_dev(src, 0));
4021 }
4022
4023 static int compare_super_imsm(struct supertype *st, struct supertype *tst,
4024 int verbose)
4025 {
4026 /* return:
4027 * 0 same, or first was empty, and second was copied
4028 * 1 sb are different
4029 */
4030 struct intel_super *first = st->sb;
4031 struct intel_super *sec = tst->sb;
4032
4033 if (!first) {
4034 st->sb = tst->sb;
4035 tst->sb = NULL;
4036 return 0;
4037 }
4038
4039 /* in platform dependent environment test if the disks
4040 * use the same Intel hba
4041 * if not on Intel hba at all, allow anything.
4042 * doesn't check HBAs if num_raid_devs is not set, as it means
4043 * it is a free floating spare, and all spares regardless of HBA type
4044 * will fall into separate container during the assembly
4045 */
4046 if (first->hba && sec->hba && first->anchor->num_raid_devs != 0) {
4047 if (first->hba->type != sec->hba->type) {
4048 if (verbose)
4049 pr_err("HBAs of devices do not match %s != %s\n",
4050 get_sys_dev_type(first->hba->type),
4051 get_sys_dev_type(sec->hba->type));
4052 return 1;
4053 }
4054 if (first->orom != sec->orom) {
4055 if (verbose)
4056 pr_err("HBAs of devices do not match %s != %s\n",
4057 first->hba->pci_id, sec->hba->pci_id);
4058 return 1;
4059 }
4060 }
4061
4062 if (first->anchor->num_raid_devs > 0 &&
4063 sec->anchor->num_raid_devs > 0) {
4064 /* Determine if these disks might ever have been
4065 * related. Further disambiguation can only take place
4066 * in load_super_imsm_all
4067 */
4068 __u32 first_family = first->anchor->orig_family_num;
4069 __u32 sec_family = sec->anchor->orig_family_num;
4070
4071 if (memcmp(first->anchor->sig, sec->anchor->sig,
4072 MAX_SIGNATURE_LENGTH) != 0)
4073 return 1;
4074
4075 if (first_family == 0)
4076 first_family = first->anchor->family_num;
4077 if (sec_family == 0)
4078 sec_family = sec->anchor->family_num;
4079
4080 if (first_family != sec_family)
4081 return 1;
4082
4083 }
4084
4085 /* if an anchor does not have num_raid_devs set then it is a free
4086 * floating spare. don't assosiate spare with any array, as during assembly
4087 * spares shall fall into separate container, from which they can be moved
4088 * when necessary
4089 */
4090 if (first->anchor->num_raid_devs ^ sec->anchor->num_raid_devs)
4091 return 1;
4092
4093 return 0;
4094 }
4095
4096 static void fd2devname(int fd, char *name)
4097 {
4098 char *nm;
4099
4100 nm = fd2kname(fd);
4101 if (!nm)
4102 return;
4103
4104 snprintf(name, MAX_RAID_SERIAL_LEN, "/dev/%s", nm);
4105 }
4106
4107 static int nvme_get_serial(int fd, void *buf, size_t buf_len)
4108 {
4109 char path[PATH_MAX];
4110 char *name = fd2kname(fd);
4111
4112 if (!name)
4113 return 1;
4114
4115 if (strncmp(name, "nvme", 4) != 0)
4116 return 1;
4117
4118 if (!diskfd_to_devpath(fd, 1, path))
4119 return 1;
4120
4121 return devpath_to_char(path, "serial", buf, buf_len, 0);
4122 }
4123
4124 extern int scsi_get_serial(int fd, void *buf, size_t buf_len);
4125
4126 static int imsm_read_serial(int fd, char *devname,
4127 __u8 *serial, size_t serial_buf_len)
4128 {
4129 char buf[50];
4130 int rv;
4131 size_t len;
4132 char *dest;
4133 char *src;
4134 unsigned int i;
4135
4136 memset(buf, 0, sizeof(buf));
4137
4138 if (check_env("IMSM_DEVNAME_AS_SERIAL")) {
4139 memset(serial, 0, serial_buf_len);
4140 fd2devname(fd, (char *) serial);
4141 return 0;
4142 }
4143
4144 rv = nvme_get_serial(fd, buf, sizeof(buf));
4145
4146 if (rv)
4147 rv = scsi_get_serial(fd, buf, sizeof(buf));
4148
4149 if (rv != 0) {
4150 if (devname)
4151 pr_err("Failed to retrieve serial for %s\n",
4152 devname);
4153 return rv;
4154 }
4155
4156 /* trim all whitespace and non-printable characters and convert
4157 * ':' to ';'
4158 */
4159 for (i = 0, dest = buf; i < sizeof(buf) && buf[i]; i++) {
4160 src = &buf[i];
4161 if (*src > 0x20) {
4162 /* ':' is reserved for use in placeholder serial
4163 * numbers for missing disks
4164 */
4165 if (*src == ':')
4166 *dest++ = ';';
4167 else
4168 *dest++ = *src;
4169 }
4170 }
4171 len = dest - buf;
4172 dest = buf;
4173
4174 if (len > serial_buf_len) {
4175 /* truncate leading characters */
4176 dest += len - serial_buf_len;
4177 len = serial_buf_len;
4178 }
4179
4180 memset(serial, 0, serial_buf_len);
4181 memcpy(serial, dest, len);
4182
4183 return 0;
4184 }
4185
4186 static int serialcmp(__u8 *s1, __u8 *s2)
4187 {
4188 return strncmp((char *) s1, (char *) s2, MAX_RAID_SERIAL_LEN);
4189 }
4190
4191 static void serialcpy(__u8 *dest, __u8 *src)
4192 {
4193 strncpy((char *) dest, (char *) src, MAX_RAID_SERIAL_LEN);
4194 }
4195
4196 static struct dl *serial_to_dl(__u8 *serial, struct intel_super *super)
4197 {
4198 struct dl *dl;
4199
4200 for (dl = super->disks; dl; dl = dl->next)
4201 if (serialcmp(dl->serial, serial) == 0)
4202 break;
4203
4204 return dl;
4205 }
4206
4207 static struct imsm_disk *
4208 __serial_to_disk(__u8 *serial, struct imsm_super *mpb, int *idx)
4209 {
4210 int i;
4211
4212 for (i = 0; i < mpb->num_disks; i++) {
4213 struct imsm_disk *disk = __get_imsm_disk(mpb, i);
4214
4215 if (serialcmp(disk->serial, serial) == 0) {
4216 if (idx)
4217 *idx = i;
4218 return disk;
4219 }
4220 }
4221
4222 return NULL;
4223 }
4224
4225 static int
4226 load_imsm_disk(int fd, struct intel_super *super, char *devname, int keep_fd)
4227 {
4228 struct imsm_disk *disk;
4229 struct dl *dl;
4230 struct stat stb;
4231 int rv;
4232 char name[40];
4233 __u8 serial[MAX_RAID_SERIAL_LEN];
4234
4235 rv = imsm_read_serial(fd, devname, serial, MAX_RAID_SERIAL_LEN);
4236
4237 if (rv != 0)
4238 return 2;
4239
4240 dl = xcalloc(1, sizeof(*dl));
4241
4242 fstat(fd, &stb);
4243 dl->major = major(stb.st_rdev);
4244 dl->minor = minor(stb.st_rdev);
4245 dl->next = super->disks;
4246 dl->fd = keep_fd ? fd : -1;
4247 assert(super->disks == NULL);
4248 super->disks = dl;
4249 serialcpy(dl->serial, serial);
4250 dl->index = -2;
4251 dl->e = NULL;
4252 fd2devname(fd, name);
4253 if (devname)
4254 dl->devname = xstrdup(devname);
4255 else
4256 dl->devname = xstrdup(name);
4257
4258 /* look up this disk's index in the current anchor */
4259 disk = __serial_to_disk(dl->serial, super->anchor, &dl->index);
4260 if (disk) {
4261 dl->disk = *disk;
4262 /* only set index on disks that are a member of a
4263 * populated contianer, i.e. one with raid_devs
4264 */
4265 if (is_failed(&dl->disk))
4266 dl->index = -2;
4267 else if (is_spare(&dl->disk) || is_journal(&dl->disk))
4268 dl->index = -1;
4269 }
4270
4271 return 0;
4272 }
4273
4274 /* When migrating map0 contains the 'destination' state while map1
4275 * contains the current state. When not migrating map0 contains the
4276 * current state. This routine assumes that map[0].map_state is set to
4277 * the current array state before being called.
4278 *
4279 * Migration is indicated by one of the following states
4280 * 1/ Idle (migr_state=0 map0state=normal||unitialized||degraded||failed)
4281 * 2/ Initialize (migr_state=1 migr_type=MIGR_INIT map0state=normal
4282 * map1state=unitialized)
4283 * 3/ Repair (Resync) (migr_state=1 migr_type=MIGR_REPAIR map0state=normal
4284 * map1state=normal)
4285 * 4/ Rebuild (migr_state=1 migr_type=MIGR_REBUILD map0state=normal
4286 * map1state=degraded)
4287 * 5/ Migration (mig_state=1 migr_type=MIGR_GEN_MIGR map0state=normal
4288 * map1state=normal)
4289 */
4290 static void migrate(struct imsm_dev *dev, struct intel_super *super,
4291 __u8 to_state, int migr_type)
4292 {
4293 struct imsm_map *dest;
4294 struct imsm_map *src = get_imsm_map(dev, MAP_0);
4295
4296 dev->vol.migr_state = 1;
4297 set_migr_type(dev, migr_type);
4298 set_vol_curr_migr_unit(dev, 0);
4299 dest = get_imsm_map(dev, MAP_1);
4300
4301 /* duplicate and then set the target end state in map[0] */
4302 memcpy(dest, src, sizeof_imsm_map(src));
4303 if (migr_type == MIGR_GEN_MIGR) {
4304 __u32 ord;
4305 int i;
4306
4307 for (i = 0; i < src->num_members; i++) {
4308 ord = __le32_to_cpu(src->disk_ord_tbl[i]);
4309 set_imsm_ord_tbl_ent(src, i, ord_to_idx(ord));
4310 }
4311 }
4312
4313 if (migr_type == MIGR_GEN_MIGR)
4314 /* Clear migration record */
4315 memset(super->migr_rec, 0, sizeof(struct migr_record));
4316
4317 src->map_state = to_state;
4318 }
4319
4320 static void end_migration(struct imsm_dev *dev, struct intel_super *super,
4321 __u8 map_state)
4322 {
4323 struct imsm_map *map = get_imsm_map(dev, MAP_0);
4324 struct imsm_map *prev = get_imsm_map(dev, dev->vol.migr_state == 0 ?
4325 MAP_0 : MAP_1);
4326 int i, j;
4327
4328 /* merge any IMSM_ORD_REBUILD bits that were not successfully
4329 * completed in the last migration.
4330 *
4331 * FIXME add support for raid-level-migration
4332 */
4333 if (map_state != map->map_state && (is_gen_migration(dev) == false) &&
4334 prev->map_state != IMSM_T_STATE_UNINITIALIZED) {
4335 /* when final map state is other than expected
4336 * merge maps (not for migration)
4337 */
4338 int failed;
4339
4340 for (i = 0; i < prev->num_members; i++)
4341 for (j = 0; j < map->num_members; j++)
4342 /* during online capacity expansion
4343 * disks position can be changed
4344 * if takeover is used
4345 */
4346 if (ord_to_idx(map->disk_ord_tbl[j]) ==
4347 ord_to_idx(prev->disk_ord_tbl[i])) {
4348 map->disk_ord_tbl[j] |=
4349 prev->disk_ord_tbl[i];
4350 break;
4351 }
4352 failed = imsm_count_failed(super, dev, MAP_0);
4353 map_state = imsm_check_degraded(super, dev, failed, MAP_0);
4354 }
4355
4356 dev->vol.migr_state = 0;
4357 set_migr_type(dev, 0);
4358 set_vol_curr_migr_unit(dev, 0);
4359 map->map_state = map_state;
4360 }
4361
4362 static int parse_raid_devices(struct intel_super *super)
4363 {
4364 int i;
4365 struct imsm_dev *dev_new;
4366 size_t len, len_migr;
4367 size_t max_len = 0;
4368 size_t space_needed = 0;
4369 struct imsm_super *mpb = super->anchor;
4370
4371 for (i = 0; i < super->anchor->num_raid_devs; i++) {
4372 struct imsm_dev *dev_iter = __get_imsm_dev(super->anchor, i);
4373 struct intel_dev *dv;
4374
4375 len = sizeof_imsm_dev(dev_iter, 0);
4376 len_migr = sizeof_imsm_dev(dev_iter, 1);
4377 if (len_migr > len)
4378 space_needed += len_migr - len;
4379
4380 dv = xmalloc(sizeof(*dv));
4381 if (max_len < len_migr)
4382 max_len = len_migr;
4383 if (max_len > len_migr)
4384 space_needed += max_len - len_migr;
4385 dev_new = xmalloc(max_len);
4386 imsm_copy_dev(dev_new, dev_iter);
4387 dv->dev = dev_new;
4388 dv->index = i;
4389 dv->next = super->devlist;
4390 super->devlist = dv;
4391 }
4392
4393 /* ensure that super->buf is large enough when all raid devices
4394 * are migrating
4395 */
4396 if (__le32_to_cpu(mpb->mpb_size) + space_needed > super->len) {
4397 void *buf;
4398
4399 len = ROUND_UP(__le32_to_cpu(mpb->mpb_size) + space_needed,
4400 super->sector_size);
4401 if (posix_memalign(&buf, MAX_SECTOR_SIZE, len) != 0)
4402 return 1;
4403
4404 memcpy(buf, super->buf, super->len);
4405 memset(buf + super->len, 0, len - super->len);
4406 free(super->buf);
4407 super->buf = buf;
4408 super->len = len;
4409 }
4410
4411 super->extra_space += space_needed;
4412
4413 return 0;
4414 }
4415
4416 /*******************************************************************************
4417 * Function: check_mpb_migr_compatibility
4418 * Description: Function checks for unsupported migration features:
4419 * - migration optimization area (pba_of_lba0)
4420 * - descending reshape (ascending_migr)
4421 * Parameters:
4422 * super : imsm metadata information
4423 * Returns:
4424 * 0 : migration is compatible
4425 * -1 : migration is not compatible
4426 ******************************************************************************/
4427 int check_mpb_migr_compatibility(struct intel_super *super)
4428 {
4429 struct imsm_map *map0, *map1;
4430 struct migr_record *migr_rec = super->migr_rec;
4431 int i;
4432
4433 for (i = 0; i < super->anchor->num_raid_devs; i++) {
4434 struct imsm_dev *dev_iter = __get_imsm_dev(super->anchor, i);
4435
4436 if (dev_iter->vol.migr_state == 1 &&
4437 dev_iter->vol.migr_type == MIGR_GEN_MIGR) {
4438 /* This device is migrating */
4439 map0 = get_imsm_map(dev_iter, MAP_0);
4440 map1 = get_imsm_map(dev_iter, MAP_1);
4441 if (pba_of_lba0(map0) != pba_of_lba0(map1))
4442 /* migration optimization area was used */
4443 return -1;
4444 if (migr_rec->ascending_migr == 0 &&
4445 migr_rec->dest_depth_per_unit > 0)
4446 /* descending reshape not supported yet */
4447 return -1;
4448 }
4449 }
4450 return 0;
4451 }
4452
4453 static void __free_imsm(struct intel_super *super, int free_disks);
4454
4455 /* load_imsm_mpb - read matrix metadata
4456 * allocates super->mpb to be freed by free_imsm
4457 */
4458 static int load_imsm_mpb(int fd, struct intel_super *super, char *devname)
4459 {
4460 unsigned long long dsize;
4461 unsigned long long sectors;
4462 unsigned int sector_size = super->sector_size;
4463 struct stat;
4464 struct imsm_super *anchor;
4465 __u32 check_sum;
4466
4467 get_dev_size(fd, NULL, &dsize);
4468 if (dsize < 2*sector_size) {
4469 if (devname)
4470 pr_err("%s: device to small for imsm\n",
4471 devname);
4472 return 1;
4473 }
4474
4475 if (lseek64(fd, dsize - (sector_size * 2), SEEK_SET) < 0) {
4476 if (devname)
4477 pr_err("Cannot seek to anchor block on %s: %s\n",
4478 devname, strerror(errno));
4479 return 1;
4480 }
4481
4482 if (posix_memalign((void **)&anchor, sector_size, sector_size) != 0) {
4483 if (devname)
4484 pr_err("Failed to allocate imsm anchor buffer on %s\n", devname);
4485 return 1;
4486 }
4487 if ((unsigned int)read(fd, anchor, sector_size) != sector_size) {
4488 if (devname)
4489 pr_err("Cannot read anchor block on %s: %s\n",
4490 devname, strerror(errno));
4491 free(anchor);
4492 return 1;
4493 }
4494
4495 if (strncmp((char *) anchor->sig, MPB_SIGNATURE, MPB_SIG_LEN) != 0) {
4496 if (devname)
4497 pr_err("no IMSM anchor on %s\n", devname);
4498 free(anchor);
4499 return 2;
4500 }
4501
4502 __free_imsm(super, 0);
4503 /* reload capability and hba */
4504
4505 /* capability and hba must be updated with new super allocation */
4506 find_intel_hba_capability(fd, super, devname);
4507 super->len = ROUND_UP(anchor->mpb_size, sector_size);
4508 if (posix_memalign(&super->buf, MAX_SECTOR_SIZE, super->len) != 0) {
4509 if (devname)
4510 pr_err("unable to allocate %zu byte mpb buffer\n",
4511 super->len);
4512 free(anchor);
4513 return 2;
4514 }
4515 memcpy(super->buf, anchor, sector_size);
4516
4517 sectors = mpb_sectors(anchor, sector_size) - 1;
4518 free(anchor);
4519
4520 if (posix_memalign(&super->migr_rec_buf, MAX_SECTOR_SIZE,
4521 MIGR_REC_BUF_SECTORS*MAX_SECTOR_SIZE) != 0) {
4522 pr_err("could not allocate migr_rec buffer\n");
4523 free(super->buf);
4524 super->buf = NULL;
4525 return 2;
4526 }
4527 super->clean_migration_record_by_mdmon = 0;
4528
4529 if (!sectors) {
4530 check_sum = __gen_imsm_checksum(super->anchor);
4531 if (check_sum != __le32_to_cpu(super->anchor->check_sum)) {
4532 if (devname)
4533 pr_err("IMSM checksum %x != %x on %s\n",
4534 check_sum,
4535 __le32_to_cpu(super->anchor->check_sum),
4536 devname);
4537 return 2;
4538 }
4539
4540 return 0;
4541 }
4542
4543 /* read the extended mpb */
4544 if (lseek64(fd, dsize - (sector_size * (2 + sectors)), SEEK_SET) < 0) {
4545 if (devname)
4546 pr_err("Cannot seek to extended mpb on %s: %s\n",
4547 devname, strerror(errno));
4548 return 1;
4549 }
4550
4551 if ((unsigned int)read(fd, super->buf + sector_size,
4552 super->len - sector_size) != super->len - sector_size) {
4553 if (devname)
4554 pr_err("Cannot read extended mpb on %s: %s\n",
4555 devname, strerror(errno));
4556 return 2;
4557 }
4558
4559 check_sum = __gen_imsm_checksum(super->anchor);
4560 if (check_sum != __le32_to_cpu(super->anchor->check_sum)) {
4561 if (devname)
4562 pr_err("IMSM checksum %x != %x on %s\n",
4563 check_sum, __le32_to_cpu(super->anchor->check_sum),
4564 devname);
4565 return 3;
4566 }
4567
4568 return 0;
4569 }
4570
4571 static int read_imsm_migr_rec(int fd, struct intel_super *super);
4572
4573 /* clears hi bits in metadata if MPB_ATTRIB_2TB_DISK not set */
4574 static void clear_hi(struct intel_super *super)
4575 {
4576 struct imsm_super *mpb = super->anchor;
4577 int i, n;
4578 if (mpb->attributes & MPB_ATTRIB_2TB_DISK)
4579 return;
4580 for (i = 0; i < mpb->num_disks; ++i) {
4581 struct imsm_disk *disk = &mpb->disk[i];
4582 disk->total_blocks_hi = 0;
4583 }
4584 for (i = 0; i < mpb->num_raid_devs; ++i) {
4585 struct imsm_dev *dev = get_imsm_dev(super, i);
4586 for (n = 0; n < 2; ++n) {
4587 struct imsm_map *map = get_imsm_map(dev, n);
4588 if (!map)
4589 continue;
4590 map->pba_of_lba0_hi = 0;
4591 map->blocks_per_member_hi = 0;
4592 map->num_data_stripes_hi = 0;
4593 }
4594 }
4595 }
4596
4597 static int
4598 load_and_parse_mpb(int fd, struct intel_super *super, char *devname, int keep_fd)
4599 {
4600 int err;
4601
4602 err = load_imsm_mpb(fd, super, devname);
4603 if (err)
4604 return err;
4605 if (super->sector_size == 4096)
4606 convert_from_4k(super);
4607 err = load_imsm_disk(fd, super, devname, keep_fd);
4608 if (err)
4609 return err;
4610 err = parse_raid_devices(super);
4611 if (err)
4612 return err;
4613 err = load_bbm_log(super);
4614 clear_hi(super);
4615 return err;
4616 }
4617
4618 static void __free_imsm_disk(struct dl *d, int do_close)
4619 {
4620 if (do_close)
4621 close_fd(&d->fd);
4622 if (d->devname)
4623 free(d->devname);
4624 if (d->e)
4625 free(d->e);
4626 free(d);
4627
4628 }
4629
4630 static void free_imsm_disks(struct intel_super *super)
4631 {
4632 struct dl *d;
4633
4634 while (super->disks) {
4635 d = super->disks;
4636 super->disks = d->next;
4637 __free_imsm_disk(d, 1);
4638 }
4639 while (super->disk_mgmt_list) {
4640 d = super->disk_mgmt_list;
4641 super->disk_mgmt_list = d->next;
4642 __free_imsm_disk(d, 1);
4643 }
4644 while (super->missing) {
4645 d = super->missing;
4646 super->missing = d->next;
4647 __free_imsm_disk(d, 1);
4648 }
4649
4650 }
4651
4652 /* free all the pieces hanging off of a super pointer */
4653 static void __free_imsm(struct intel_super *super, int free_disks)
4654 {
4655 struct intel_hba *elem, *next;
4656
4657 if (super->buf) {
4658 free(super->buf);
4659 super->buf = NULL;
4660 }
4661 /* unlink capability description */
4662 super->orom = NULL;
4663 if (super->migr_rec_buf) {
4664 free(super->migr_rec_buf);
4665 super->migr_rec_buf = NULL;
4666 }
4667 if (free_disks)
4668 free_imsm_disks(super);
4669 free_devlist(super);
4670 elem = super->hba;
4671 while (elem) {
4672 if (elem->path)
4673 free((void *)elem->path);
4674 next = elem->next;
4675 free(elem);
4676 elem = next;
4677 }
4678 if (super->bbm_log)
4679 free(super->bbm_log);
4680 super->hba = NULL;
4681 }
4682
4683 static void free_imsm(struct intel_super *super)
4684 {
4685 __free_imsm(super, 1);
4686 free(super->bb.entries);
4687 free(super);
4688 }
4689
4690 static void free_super_imsm(struct supertype *st)
4691 {
4692 struct intel_super *super = st->sb;
4693
4694 if (!super)
4695 return;
4696
4697 free_imsm(super);
4698 st->sb = NULL;
4699 }
4700
4701 static struct intel_super *alloc_super(void)
4702 {
4703 struct intel_super *super = xcalloc(1, sizeof(*super));
4704
4705 super->current_vol = -1;
4706 super->create_offset = ~((unsigned long long) 0);
4707
4708 super->bb.entries = xmalloc(BBM_LOG_MAX_ENTRIES *
4709 sizeof(struct md_bb_entry));
4710 if (!super->bb.entries) {
4711 free(super);
4712 return NULL;
4713 }
4714
4715 return super;
4716 }
4717
4718 /*
4719 * find and allocate hba and OROM/EFI based on valid fd of RAID component device
4720 */
4721 static int find_intel_hba_capability(int fd, struct intel_super *super, char *devname)
4722 {
4723 struct sys_dev *hba_name;
4724 int rv = 0;
4725
4726 if (is_fd_valid(fd) && test_partition(fd)) {
4727 pr_err("imsm: %s is a partition, cannot be used in IMSM\n",
4728 devname);
4729 return 1;
4730 }
4731 if (!is_fd_valid(fd) || check_no_platform()) {
4732 super->orom = NULL;
4733 super->hba = NULL;
4734 return 0;
4735 }
4736 hba_name = find_disk_attached_hba(fd, NULL);
4737 if (!hba_name) {
4738 if (devname)
4739 pr_err("%s is not attached to Intel(R) RAID controller.\n",
4740 devname);
4741 return 1;
4742 }
4743 rv = attach_hba_to_super(super, hba_name);
4744 if (rv == 2) {
4745 if (devname) {
4746 struct intel_hba *hba = super->hba;
4747
4748 pr_err("%s is attached to Intel(R) %s %s (%s),\n"
4749 " but the container is assigned to Intel(R) %s %s (",
4750 devname,
4751 get_sys_dev_type(hba_name->type),
4752 hba_name->type == SYS_DEV_VMD || hba_name->type == SYS_DEV_SATA_VMD ?
4753 "domain" : "RAID controller",
4754 hba_name->pci_id ? : "Err!",
4755 get_sys_dev_type(super->hba->type),
4756 hba->type == SYS_DEV_VMD || hba_name->type == SYS_DEV_SATA_VMD ?
4757 "domain" : "RAID controller");
4758
4759 while (hba) {
4760 fprintf(stderr, "%s", hba->pci_id ? : "Err!");
4761 if (hba->next)
4762 fprintf(stderr, ", ");
4763 hba = hba->next;
4764 }
4765 fprintf(stderr, ").\n"
4766 " Mixing devices attached to different controllers is not allowed.\n");
4767 }
4768 return 2;
4769 }
4770 super->orom = find_imsm_capability(hba_name);
4771 if (!super->orom)
4772 return 3;
4773
4774 return 0;
4775 }
4776
4777 /* find_missing - helper routine for load_super_imsm_all that identifies
4778 * disks that have disappeared from the system. This routine relies on
4779 * the mpb being uptodate, which it is at load time.
4780 */
4781 static int find_missing(struct intel_super *super)
4782 {
4783 int i;
4784 struct imsm_super *mpb = super->anchor;
4785 struct dl *dl;
4786 struct imsm_disk *disk;
4787
4788 for (i = 0; i < mpb->num_disks; i++) {
4789 disk = __get_imsm_disk(mpb, i);
4790 dl = serial_to_dl(disk->serial, super);
4791 if (dl)
4792 continue;
4793
4794 dl = xmalloc(sizeof(*dl));
4795 dl->major = 0;
4796 dl->minor = 0;
4797 dl->fd = -1;
4798 dl->devname = xstrdup("missing");
4799 dl->index = i;
4800 serialcpy(dl->serial, disk->serial);
4801 dl->disk = *disk;
4802 dl->e = NULL;
4803 dl->next = super->missing;
4804 super->missing = dl;
4805 }
4806
4807 return 0;
4808 }
4809
4810 static struct intel_disk *disk_list_get(__u8 *serial, struct intel_disk *disk_list)
4811 {
4812 struct intel_disk *idisk = disk_list;
4813
4814 while (idisk) {
4815 if (serialcmp(idisk->disk.serial, serial) == 0)
4816 break;
4817 idisk = idisk->next;
4818 }
4819
4820 return idisk;
4821 }
4822
4823 static int __prep_thunderdome(struct intel_super **table, int tbl_size,
4824 struct intel_super *super,
4825 struct intel_disk **disk_list)
4826 {
4827 struct imsm_disk *d = &super->disks->disk;
4828 struct imsm_super *mpb = super->anchor;
4829 int i, j;
4830
4831 for (i = 0; i < tbl_size; i++) {
4832 struct imsm_super *tbl_mpb = table[i]->anchor;
4833 struct imsm_disk *tbl_d = &table[i]->disks->disk;
4834
4835 if (tbl_mpb->family_num == mpb->family_num) {
4836 if (tbl_mpb->check_sum == mpb->check_sum) {
4837 dprintf("mpb from %d:%d matches %d:%d\n",
4838 super->disks->major,
4839 super->disks->minor,
4840 table[i]->disks->major,
4841 table[i]->disks->minor);
4842 break;
4843 }
4844
4845 if (((is_configured(d) && !is_configured(tbl_d)) ||
4846 is_configured(d) == is_configured(tbl_d)) &&
4847 tbl_mpb->generation_num < mpb->generation_num) {
4848 /* current version of the mpb is a
4849 * better candidate than the one in
4850 * super_table, but copy over "cross
4851 * generational" status
4852 */
4853 struct intel_disk *idisk;
4854
4855 dprintf("mpb from %d:%d replaces %d:%d\n",
4856 super->disks->major,
4857 super->disks->minor,
4858 table[i]->disks->major,
4859 table[i]->disks->minor);
4860
4861 idisk = disk_list_get(tbl_d->serial, *disk_list);
4862 if (idisk && is_failed(&idisk->disk))
4863 tbl_d->status |= FAILED_DISK;
4864 break;
4865 } else {
4866 struct intel_disk *idisk;
4867 struct imsm_disk *disk;
4868
4869 /* tbl_mpb is more up to date, but copy
4870 * over cross generational status before
4871 * returning
4872 */
4873 disk = __serial_to_disk(d->serial, mpb, NULL);
4874 if (disk && is_failed(disk))
4875 d->status |= FAILED_DISK;
4876
4877 idisk = disk_list_get(d->serial, *disk_list);
4878 if (idisk) {
4879 idisk->owner = i;
4880 if (disk && is_configured(disk))
4881 idisk->disk.status |= CONFIGURED_DISK;
4882 }
4883
4884 dprintf("mpb from %d:%d prefer %d:%d\n",
4885 super->disks->major,
4886 super->disks->minor,
4887 table[i]->disks->major,
4888 table[i]->disks->minor);
4889
4890 return tbl_size;
4891 }
4892 }
4893 }
4894
4895 if (i >= tbl_size)
4896 table[tbl_size++] = super;
4897 else
4898 table[i] = super;
4899
4900 /* update/extend the merged list of imsm_disk records */
4901 for (j = 0; j < mpb->num_disks; j++) {
4902 struct imsm_disk *disk = __get_imsm_disk(mpb, j);
4903 struct intel_disk *idisk;
4904
4905 idisk = disk_list_get(disk->serial, *disk_list);
4906 if (idisk) {
4907 idisk->disk.status |= disk->status;
4908 if (is_configured(&idisk->disk) ||
4909 is_failed(&idisk->disk))
4910 idisk->disk.status &= ~(SPARE_DISK);
4911 } else {
4912 idisk = xcalloc(1, sizeof(*idisk));
4913 idisk->owner = IMSM_UNKNOWN_OWNER;
4914 idisk->disk = *disk;
4915 idisk->next = *disk_list;
4916 *disk_list = idisk;
4917 }
4918
4919 if (serialcmp(idisk->disk.serial, d->serial) == 0)
4920 idisk->owner = i;
4921 }
4922
4923 return tbl_size;
4924 }
4925
4926 static struct intel_super *
4927 validate_members(struct intel_super *super, struct intel_disk *disk_list,
4928 const int owner)
4929 {
4930 struct imsm_super *mpb = super->anchor;
4931 int ok_count = 0;
4932 int i;
4933
4934 for (i = 0; i < mpb->num_disks; i++) {
4935 struct imsm_disk *disk = __get_imsm_disk(mpb, i);
4936 struct intel_disk *idisk;
4937
4938 idisk = disk_list_get(disk->serial, disk_list);
4939 if (idisk) {
4940 if (idisk->owner == owner ||
4941 idisk->owner == IMSM_UNKNOWN_OWNER)
4942 ok_count++;
4943 else
4944 dprintf("'%.16s' owner %d != %d\n",
4945 disk->serial, idisk->owner,
4946 owner);
4947 } else {
4948 dprintf("unknown disk %x [%d]: %.16s\n",
4949 __le32_to_cpu(mpb->family_num), i,
4950 disk->serial);
4951 break;
4952 }
4953 }
4954
4955 if (ok_count == mpb->num_disks)
4956 return super;
4957 return NULL;
4958 }
4959
4960 static void show_conflicts(__u32 family_num, struct intel_super *super_list)
4961 {
4962 struct intel_super *s;
4963
4964 for (s = super_list; s; s = s->next) {
4965 if (family_num != s->anchor->family_num)
4966 continue;
4967 pr_err("Conflict, offlining family %#x on '%s'\n",
4968 __le32_to_cpu(family_num), s->disks->devname);
4969 }
4970 }
4971
4972 static struct intel_super *
4973 imsm_thunderdome(struct intel_super **super_list, int len)
4974 {
4975 struct intel_super *super_table[len];
4976 struct intel_disk *disk_list = NULL;
4977 struct intel_super *champion, *spare;
4978 struct intel_super *s, **del;
4979 int tbl_size = 0;
4980 int conflict;
4981 int i;
4982
4983 memset(super_table, 0, sizeof(super_table));
4984 for (s = *super_list; s; s = s->next)
4985 tbl_size = __prep_thunderdome(super_table, tbl_size, s, &disk_list);
4986
4987 for (i = 0; i < tbl_size; i++) {
4988 struct imsm_disk *d;
4989 struct intel_disk *idisk;
4990 struct imsm_super *mpb = super_table[i]->anchor;
4991
4992 s = super_table[i];
4993 d = &s->disks->disk;
4994
4995 /* 'd' must appear in merged disk list for its
4996 * configuration to be valid
4997 */
4998 idisk = disk_list_get(d->serial, disk_list);
4999 if (idisk && idisk->owner == i)
5000 s = validate_members(s, disk_list, i);
5001 else
5002 s = NULL;
5003
5004 if (!s)
5005 dprintf("marking family: %#x from %d:%d offline\n",
5006 mpb->family_num,
5007 super_table[i]->disks->major,
5008 super_table[i]->disks->minor);
5009 super_table[i] = s;
5010 }
5011
5012 /* This is where the mdadm implementation differs from the Windows
5013 * driver which has no strict concept of a container. We can only
5014 * assemble one family from a container, so when returning a prodigal
5015 * array member to this system the code will not be able to disambiguate
5016 * the container contents that should be assembled ("foreign" versus
5017 * "local"). It requires user intervention to set the orig_family_num
5018 * to a new value to establish a new container. The Windows driver in
5019 * this situation fixes up the volume name in place and manages the
5020 * foreign array as an independent entity.
5021 */
5022 s = NULL;
5023 spare = NULL;
5024 conflict = 0;
5025 for (i = 0; i < tbl_size; i++) {
5026 struct intel_super *tbl_ent = super_table[i];
5027 int is_spare = 0;
5028
5029 if (!tbl_ent)
5030 continue;
5031
5032 if (tbl_ent->anchor->num_raid_devs == 0) {
5033 spare = tbl_ent;
5034 is_spare = 1;
5035 }
5036
5037 if (s && !is_spare) {
5038 show_conflicts(tbl_ent->anchor->family_num, *super_list);
5039 conflict++;
5040 } else if (!s && !is_spare)
5041 s = tbl_ent;
5042 }
5043
5044 if (!s)
5045 s = spare;
5046 if (!s) {
5047 champion = NULL;
5048 goto out;
5049 }
5050 champion = s;
5051
5052 if (conflict)
5053 pr_err("Chose family %#x on '%s', assemble conflicts to new container with '--update=uuid'\n",
5054 __le32_to_cpu(s->anchor->family_num), s->disks->devname);
5055
5056 /* collect all dl's onto 'champion', and update them to
5057 * champion's version of the status
5058 */
5059 for (s = *super_list; s; s = s->next) {
5060 struct imsm_super *mpb = champion->anchor;
5061 struct dl *dl = s->disks;
5062
5063 if (s == champion)
5064 continue;
5065
5066 mpb->attributes |= s->anchor->attributes & MPB_ATTRIB_2TB_DISK;
5067
5068 for (i = 0; i < mpb->num_disks; i++) {
5069 struct imsm_disk *disk;
5070
5071 disk = __serial_to_disk(dl->serial, mpb, &dl->index);
5072 if (disk) {
5073 dl->disk = *disk;
5074 /* only set index on disks that are a member of
5075 * a populated contianer, i.e. one with
5076 * raid_devs
5077 */
5078 if (is_failed(&dl->disk))
5079 dl->index = -2;
5080 else if (is_spare(&dl->disk))
5081 dl->index = -1;
5082 break;
5083 }
5084 }
5085
5086 if (i >= mpb->num_disks) {
5087 struct intel_disk *idisk;
5088
5089 idisk = disk_list_get(dl->serial, disk_list);
5090 if (idisk && is_spare(&idisk->disk) &&
5091 !is_failed(&idisk->disk) && !is_configured(&idisk->disk))
5092 dl->index = -1;
5093 else {
5094 dl->index = -2;
5095 continue;
5096 }
5097 }
5098
5099 dl->next = champion->disks;
5100 champion->disks = dl;
5101 s->disks = NULL;
5102 }
5103
5104 /* delete 'champion' from super_list */
5105 for (del = super_list; *del; ) {
5106 if (*del == champion) {
5107 *del = (*del)->next;
5108 break;
5109 } else
5110 del = &(*del)->next;
5111 }
5112 champion->next = NULL;
5113
5114 out:
5115 while (disk_list) {
5116 struct intel_disk *idisk = disk_list;
5117
5118 disk_list = disk_list->next;
5119 free(idisk);
5120 }
5121
5122 return champion;
5123 }
5124
5125 static int
5126 get_sra_super_block(int fd, struct intel_super **super_list, char *devname, int *max, int keep_fd);
5127 static int get_super_block(struct intel_super **super_list, char *devnm, char *devname,
5128 int major, int minor, int keep_fd);
5129 static int
5130 get_devlist_super_block(struct md_list *devlist, struct intel_super **super_list,
5131 int *max, int keep_fd);
5132
5133 static int load_super_imsm_all(struct supertype *st, int fd, void **sbp,
5134 char *devname, struct md_list *devlist,
5135 int keep_fd)
5136 {
5137 struct intel_super *super_list = NULL;
5138 struct intel_super *super = NULL;
5139 int err = 0;
5140 int i = 0;
5141
5142 if (is_fd_valid(fd))
5143 /* 'fd' is an opened container */
5144 err = get_sra_super_block(fd, &super_list, devname, &i, keep_fd);
5145 else
5146 /* get super block from devlist devices */
5147 err = get_devlist_super_block(devlist, &super_list, &i, keep_fd);
5148 if (err)
5149 goto error;
5150 /* all mpbs enter, maybe one leaves */
5151 super = imsm_thunderdome(&super_list, i);
5152 if (!super) {
5153 err = 1;
5154 goto error;
5155 }
5156
5157 if (find_missing(super) != 0) {
5158 free_imsm(super);
5159 err = 2;
5160 goto error;
5161 }
5162
5163 /* load migration record */
5164 err = load_imsm_migr_rec(super);
5165 if (err == -1) {
5166 /* migration is in progress,
5167 * but migr_rec cannot be loaded,
5168 */
5169 err = 4;
5170 goto error;
5171 }
5172
5173 /* Check migration compatibility */
5174 if (err == 0 && check_mpb_migr_compatibility(super) != 0) {
5175 pr_err("Unsupported migration detected");
5176 if (devname)
5177 fprintf(stderr, " on %s\n", devname);
5178 else
5179 fprintf(stderr, " (IMSM).\n");
5180
5181 err = 5;
5182 goto error;
5183 }
5184
5185 err = 0;
5186
5187 error:
5188 while (super_list) {
5189 struct intel_super *s = super_list;
5190
5191 super_list = super_list->next;
5192 free_imsm(s);
5193 }
5194
5195 if (err)
5196 return err;
5197
5198 *sbp = super;
5199 if (is_fd_valid(fd))
5200 strcpy(st->container_devnm, fd2devnm(fd));
5201 else
5202 st->container_devnm[0] = 0;
5203 if (err == 0 && st->ss == NULL) {
5204 st->ss = &super_imsm;
5205 st->minor_version = 0;
5206 st->max_devs = IMSM_MAX_DEVICES;
5207 }
5208 return 0;
5209 }
5210
5211 static int
5212 get_devlist_super_block(struct md_list *devlist, struct intel_super **super_list,
5213 int *max, int keep_fd)
5214 {
5215 struct md_list *tmpdev;
5216 int err = 0;
5217 int i = 0;
5218
5219 for (i = 0, tmpdev = devlist; tmpdev; tmpdev = tmpdev->next) {
5220 if (tmpdev->used != 1)
5221 continue;
5222 if (tmpdev->container == 1) {
5223 int lmax = 0;
5224 int fd = dev_open(tmpdev->devname, O_RDONLY|O_EXCL);
5225 if (!is_fd_valid(fd)) {
5226 pr_err("cannot open device %s: %s\n",
5227 tmpdev->devname, strerror(errno));
5228 err = 8;
5229 goto error;
5230 }
5231 err = get_sra_super_block(fd, super_list,
5232 tmpdev->devname, &lmax,
5233 keep_fd);
5234 i += lmax;
5235 close(fd);
5236 if (err) {
5237 err = 7;
5238 goto error;
5239 }
5240 } else {
5241 int major = major(tmpdev->st_rdev);
5242 int minor = minor(tmpdev->st_rdev);
5243 err = get_super_block(super_list,
5244 NULL,
5245 tmpdev->devname,
5246 major, minor,
5247 keep_fd);
5248 i++;
5249 if (err) {
5250 err = 6;
5251 goto error;
5252 }
5253 }
5254 }
5255 error:
5256 *max = i;
5257 return err;
5258 }
5259
5260 static int get_super_block(struct intel_super **super_list, char *devnm, char *devname,
5261 int major, int minor, int keep_fd)
5262 {
5263 struct intel_super *s;
5264 char nm[32];
5265 int dfd = -1;
5266 int err = 0;
5267 int retry;
5268
5269 s = alloc_super();
5270 if (!s) {
5271 err = 1;
5272 goto error;
5273 }
5274
5275 sprintf(nm, "%d:%d", major, minor);
5276 dfd = dev_open(nm, O_RDWR);
5277 if (!is_fd_valid(dfd)) {
5278 err = 2;
5279 goto error;
5280 }
5281
5282 if (!get_dev_sector_size(dfd, NULL, &s->sector_size)) {
5283 err = 2;
5284 goto error;
5285 }
5286 find_intel_hba_capability(dfd, s, devname);
5287 err = load_and_parse_mpb(dfd, s, NULL, keep_fd);
5288
5289 /* retry the load if we might have raced against mdmon */
5290 if (err == 3 && devnm && mdmon_running(devnm))
5291 for (retry = 0; retry < 3; retry++) {
5292 sleep_for(0, MSEC_TO_NSEC(3), true);
5293 err = load_and_parse_mpb(dfd, s, NULL, keep_fd);
5294 if (err != 3)
5295 break;
5296 }
5297 error:
5298 if (!err) {
5299 s->next = *super_list;
5300 *super_list = s;
5301 } else {
5302 if (s)
5303 free_imsm(s);
5304 close_fd(&dfd);
5305 }
5306 if (!keep_fd)
5307 close_fd(&dfd);
5308 return err;
5309
5310 }
5311
5312 static int
5313 get_sra_super_block(int fd, struct intel_super **super_list, char *devname, int *max, int keep_fd)
5314 {
5315 struct mdinfo *sra;
5316 char *devnm;
5317 struct mdinfo *sd;
5318 int err = 0;
5319 int i = 0;
5320 sra = sysfs_read(fd, NULL, GET_LEVEL|GET_VERSION|GET_DEVS|GET_STATE);
5321 if (!sra)
5322 return 1;
5323
5324 if (sra->array.major_version != -1 ||
5325 sra->array.minor_version != -2 ||
5326 strcmp(sra->text_version, "imsm") != 0) {
5327 err = 1;
5328 goto error;
5329 }
5330 /* load all mpbs */
5331 devnm = fd2devnm(fd);
5332 for (sd = sra->devs, i = 0; sd; sd = sd->next, i++) {
5333 if (get_super_block(super_list, devnm, devname,
5334 sd->disk.major, sd->disk.minor, keep_fd) != 0) {
5335 err = 7;
5336 goto error;
5337 }
5338 }
5339 error:
5340 sysfs_free(sra);
5341 *max = i;
5342 return err;
5343 }
5344
5345 static int load_container_imsm(struct supertype *st, int fd, char *devname)
5346 {
5347 return load_super_imsm_all(st, fd, &st->sb, devname, NULL, 1);
5348 }
5349
5350 static int load_super_imsm(struct supertype *st, int fd, char *devname)
5351 {
5352 struct intel_super *super;
5353 int rv;
5354 int retry;
5355
5356 if (test_partition(fd))
5357 /* IMSM not allowed on partitions */
5358 return 1;
5359
5360 free_super_imsm(st);
5361
5362 super = alloc_super();
5363 if (!super)
5364 return 1;
5365
5366 if (!get_dev_sector_size(fd, NULL, &super->sector_size)) {
5367 free_imsm(super);
5368 return 1;
5369 }
5370 /* Load hba and capabilities if they exist.
5371 * But do not preclude loading metadata in case capabilities or hba are
5372 * non-compliant and ignore_hw_compat is set.
5373 */
5374 rv = find_intel_hba_capability(fd, super, devname);
5375 /* no orom/efi or non-intel hba of the disk */
5376 if (rv != 0 && st->ignore_hw_compat == 0) {
5377 if (devname)
5378 pr_err("No OROM/EFI properties for %s\n", devname);
5379 free_imsm(super);
5380 return 2;
5381 }
5382 rv = load_and_parse_mpb(fd, super, devname, 0);
5383
5384 /* retry the load if we might have raced against mdmon */
5385 if (rv == 3) {
5386 struct mdstat_ent *mdstat = NULL;
5387 char *name = fd2kname(fd);
5388
5389 if (name)
5390 mdstat = mdstat_by_component(name);
5391
5392 if (mdstat && mdmon_running(mdstat->devnm) && getpid() != mdmon_pid(mdstat->devnm)) {
5393 for (retry = 0; retry < 3; retry++) {
5394 sleep_for(0, MSEC_TO_NSEC(3), true);
5395 rv = load_and_parse_mpb(fd, super, devname, 0);
5396 if (rv != 3)
5397 break;
5398 }
5399 }
5400
5401 free_mdstat(mdstat);
5402 }
5403
5404 if (rv) {
5405 if (devname)
5406 pr_err("Failed to load all information sections on %s\n", devname);
5407 free_imsm(super);
5408 return rv;
5409 }
5410
5411 st->sb = super;
5412 if (st->ss == NULL) {
5413 st->ss = &super_imsm;
5414 st->minor_version = 0;
5415 st->max_devs = IMSM_MAX_DEVICES;
5416 }
5417
5418 /* load migration record */
5419 if (load_imsm_migr_rec(super) == 0) {
5420 /* Check for unsupported migration features */
5421 if (check_mpb_migr_compatibility(super) != 0) {
5422 pr_err("Unsupported migration detected");
5423 if (devname)
5424 fprintf(stderr, " on %s\n", devname);
5425 else
5426 fprintf(stderr, " (IMSM).\n");
5427 return 3;
5428 }
5429 }
5430
5431 return 0;
5432 }
5433
5434 static __u16 info_to_blocks_per_strip(mdu_array_info_t *info)
5435 {
5436 if (info->level == 1)
5437 return 128;
5438 return info->chunk_size >> 9;
5439 }
5440
5441 static unsigned long long info_to_blocks_per_member(mdu_array_info_t *info,
5442 unsigned long long size)
5443 {
5444 if (info->level == 1)
5445 return size * 2;
5446 else
5447 return (size * 2) & ~(info_to_blocks_per_strip(info) - 1);
5448 }
5449
5450 static void imsm_write_signature(struct imsm_super *mpb)
5451 {
5452 /* It is safer to eventually truncate version rather than left it not NULL ended */
5453 snprintf((char *) mpb->sig, MAX_SIGNATURE_LENGTH, MPB_SIGNATURE MPB_VERSION_ATTRIBS);
5454 }
5455
5456 static void imsm_update_version_info(struct intel_super *super)
5457 {
5458 /* update the version and attributes */
5459 struct imsm_super *mpb = super->anchor;
5460 struct imsm_dev *dev;
5461 struct imsm_map *map;
5462 int i;
5463
5464 mpb->attributes |= MPB_ATTRIB_CHECKSUM_VERIFY;
5465
5466 for (i = 0; i < mpb->num_raid_devs; i++) {
5467 dev = get_imsm_dev(super, i);
5468 map = get_imsm_map(dev, MAP_0);
5469
5470 if (__le32_to_cpu(dev->size_high) > 0)
5471 mpb->attributes |= MPB_ATTRIB_2TB;
5472
5473 switch (get_imsm_raid_level(map)) {
5474 case IMSM_T_RAID0:
5475 mpb->attributes |= MPB_ATTRIB_RAID0;
5476 break;
5477 case IMSM_T_RAID1:
5478 mpb->attributes |= MPB_ATTRIB_RAID1;
5479 break;
5480 case IMSM_T_RAID5:
5481 mpb->attributes |= MPB_ATTRIB_RAID5;
5482 break;
5483 case IMSM_T_RAID10:
5484 mpb->attributes |= MPB_ATTRIB_RAID10;
5485 if (map->num_members > 4)
5486 mpb->attributes |= MPB_ATTRIB_RAID10_EXT;
5487 break;
5488 }
5489 }
5490
5491 imsm_write_signature(mpb);
5492 }
5493
5494 /**
5495 * imsm_check_name() - check imsm naming criteria.
5496 * @super: &intel_super pointer, not NULL.
5497 * @name: name to check.
5498 * @verbose: verbose level.
5499 *
5500 * Name must be no longer than &MAX_RAID_SERIAL_LEN and must be unique across volumes.
5501 *
5502 * Returns: &true if @name matches, &false otherwise.
5503 */
5504 static bool imsm_is_name_allowed(struct intel_super *super, const char * const name,
5505 const int verbose)
5506 {
5507 struct imsm_super *mpb = super->anchor;
5508 int i;
5509
5510 if (is_string_lq(name, MAX_RAID_SERIAL_LEN + 1) == false) {
5511 pr_vrb("imsm: Name \"%s\" is too long\n", name);
5512 return false;
5513 }
5514
5515 for (i = 0; i < mpb->num_raid_devs; i++) {
5516 struct imsm_dev *dev = get_imsm_dev(super, i);
5517
5518 if (strncmp((char *) dev->volume, name, MAX_RAID_SERIAL_LEN) == 0) {
5519 pr_vrb("imsm: Name \"%s\" already exists\n", name);
5520 return false;
5521 }
5522 }
5523
5524 return true;
5525 }
5526
5527 static int init_super_imsm_volume(struct supertype *st, mdu_array_info_t *info,
5528 struct shape *s, char *name,
5529 char *homehost, int *uuid,
5530 long long data_offset)
5531 {
5532 /* We are creating a volume inside a pre-existing container.
5533 * so st->sb is already set.
5534 */
5535 struct intel_super *super = st->sb;
5536 unsigned int sector_size = super->sector_size;
5537 struct imsm_super *mpb = super->anchor;
5538 struct intel_dev *dv;
5539 struct imsm_dev *dev;
5540 struct imsm_vol *vol;
5541 struct imsm_map *map;
5542 int idx = mpb->num_raid_devs;
5543 int i;
5544 int namelen;
5545 unsigned long long array_blocks;
5546 size_t size_old, size_new;
5547 unsigned int data_disks;
5548 unsigned long long size_per_member;
5549
5550 if (super->orom && mpb->num_raid_devs >= super->orom->vpa) {
5551 pr_err("This imsm-container already has the maximum of %d volumes\n", super->orom->vpa);
5552 return 0;
5553 }
5554
5555 /* ensure the mpb is large enough for the new data */
5556 size_old = __le32_to_cpu(mpb->mpb_size);
5557 size_new = disks_to_mpb_size(info->nr_disks);
5558 if (size_new > size_old) {
5559 void *mpb_new;
5560 size_t size_round = ROUND_UP(size_new, sector_size);
5561
5562 if (posix_memalign(&mpb_new, sector_size, size_round) != 0) {
5563 pr_err("could not allocate new mpb\n");
5564 return 0;
5565 }
5566 if (posix_memalign(&super->migr_rec_buf, MAX_SECTOR_SIZE,
5567 MIGR_REC_BUF_SECTORS*
5568 MAX_SECTOR_SIZE) != 0) {
5569 pr_err("could not allocate migr_rec buffer\n");
5570 free(super->buf);
5571 free(super);
5572 free(mpb_new);
5573 return 0;
5574 }
5575 memcpy(mpb_new, mpb, size_old);
5576 free(mpb);
5577 mpb = mpb_new;
5578 super->anchor = mpb_new;
5579 mpb->mpb_size = __cpu_to_le32(size_new);
5580 memset(mpb_new + size_old, 0, size_round - size_old);
5581 super->len = size_round;
5582 }
5583 super->current_vol = idx;
5584
5585 /* handle 'failed_disks' by either:
5586 * a) create dummy disk entries in the table if this the first
5587 * volume in the array. We add them here as this is the only
5588 * opportunity to add them. add_to_super_imsm_volume()
5589 * handles the non-failed disks and continues incrementing
5590 * mpb->num_disks.
5591 * b) validate that 'failed_disks' matches the current number
5592 * of missing disks if the container is populated
5593 */
5594 if (super->current_vol == 0) {
5595 mpb->num_disks = 0;
5596 for (i = 0; i < info->failed_disks; i++) {
5597 struct imsm_disk *disk;
5598
5599 mpb->num_disks++;
5600 disk = __get_imsm_disk(mpb, i);
5601 disk->status = CONFIGURED_DISK | FAILED_DISK;
5602 disk->scsi_id = __cpu_to_le32(~(__u32)0);
5603 snprintf((char *) disk->serial, MAX_RAID_SERIAL_LEN,
5604 "missing:%d", (__u8)i);
5605 }
5606 find_missing(super);
5607 } else {
5608 int missing = 0;
5609 struct dl *d;
5610
5611 for (d = super->missing; d; d = d->next)
5612 missing++;
5613 if (info->failed_disks > missing) {
5614 pr_err("unable to add 'missing' disk to container\n");
5615 return 0;
5616 }
5617 }
5618
5619 if (imsm_is_name_allowed(super, name, 1) == false)
5620 return 0;
5621
5622 dv = xmalloc(sizeof(*dv));
5623 dev = xcalloc(1, sizeof(*dev) + sizeof(__u32) * (info->raid_disks - 1));
5624 /*
5625 * Explicitly allow truncating to not confuse gcc's
5626 * -Werror=stringop-truncation
5627 */
5628 namelen = min((int) strlen(name), MAX_RAID_SERIAL_LEN);
5629 memcpy(dev->volume, name, namelen);
5630 array_blocks = calc_array_size(info->level, info->raid_disks,
5631 info->layout, info->chunk_size,
5632 s->size * BLOCKS_PER_KB);
5633 data_disks = get_data_disks(info->level, info->layout,
5634 info->raid_disks);
5635 array_blocks = round_size_to_mb(array_blocks, data_disks);
5636 size_per_member = array_blocks / data_disks;
5637
5638 set_imsm_dev_size(dev, array_blocks);
5639 dev->status = (DEV_READ_COALESCING | DEV_WRITE_COALESCING);
5640 vol = &dev->vol;
5641 vol->migr_state = 0;
5642 set_migr_type(dev, MIGR_INIT);
5643 vol->dirty = !info->state;
5644 set_vol_curr_migr_unit(dev, 0);
5645 map = get_imsm_map(dev, MAP_0);
5646 set_pba_of_lba0(map, super->create_offset);
5647 map->blocks_per_strip = __cpu_to_le16(info_to_blocks_per_strip(info));
5648 map->failed_disk_num = ~0;
5649 if (info->level > IMSM_T_RAID0)
5650 map->map_state = (info->state ? IMSM_T_STATE_NORMAL
5651 : IMSM_T_STATE_UNINITIALIZED);
5652 else
5653 map->map_state = info->failed_disks ? IMSM_T_STATE_FAILED :
5654 IMSM_T_STATE_NORMAL;
5655 map->ddf = 1;
5656
5657 if (info->level == IMSM_T_RAID1 && info->raid_disks > 2) {
5658 free(dev);
5659 free(dv);
5660 pr_err("imsm does not support more than 2 disks in a raid1 volume\n");
5661 return 0;
5662 }
5663 map->num_members = info->raid_disks;
5664
5665 update_imsm_raid_level(map, info->level);
5666 set_num_domains(map);
5667
5668 size_per_member += NUM_BLOCKS_DIRTY_STRIPE_REGION;
5669 set_blocks_per_member(map, info_to_blocks_per_member(info,
5670 size_per_member /
5671 BLOCKS_PER_KB));
5672
5673 update_num_data_stripes(map, array_blocks);
5674 for (i = 0; i < map->num_members; i++) {
5675 /* initialized in add_to_super */
5676 set_imsm_ord_tbl_ent(map, i, IMSM_ORD_REBUILD);
5677 }
5678 mpb->num_raid_devs++;
5679 mpb->num_raid_devs_created++;
5680 dev->my_vol_raid_dev_num = mpb->num_raid_devs_created;
5681
5682 if (s->consistency_policy <= CONSISTENCY_POLICY_RESYNC) {
5683 dev->rwh_policy = RWH_MULTIPLE_OFF;
5684 } else if (s->consistency_policy == CONSISTENCY_POLICY_PPL) {
5685 dev->rwh_policy = RWH_MULTIPLE_DISTRIBUTED;
5686 } else {
5687 free(dev);
5688 free(dv);
5689 pr_err("imsm does not support consistency policy %s\n",
5690 map_num_s(consistency_policies, s->consistency_policy));
5691 return 0;
5692 }
5693
5694 dv->dev = dev;
5695 dv->index = super->current_vol;
5696 dv->next = super->devlist;
5697 super->devlist = dv;
5698
5699 imsm_update_version_info(super);
5700
5701 return 1;
5702 }
5703
5704 static int init_super_imsm(struct supertype *st, mdu_array_info_t *info,
5705 struct shape *s, char *name,
5706 char *homehost, int *uuid,
5707 unsigned long long data_offset)
5708 {
5709 /* This is primarily called by Create when creating a new array.
5710 * We will then get add_to_super called for each component, and then
5711 * write_init_super called to write it out to each device.
5712 * For IMSM, Create can create on fresh devices or on a pre-existing
5713 * array.
5714 * To create on a pre-existing array a different method will be called.
5715 * This one is just for fresh drives.
5716 */
5717 struct intel_super *super;
5718 struct imsm_super *mpb;
5719 size_t mpb_size;
5720
5721 if (data_offset != INVALID_SECTORS) {
5722 pr_err("data-offset not supported by imsm\n");
5723 return 0;
5724 }
5725
5726 if (st->sb)
5727 return init_super_imsm_volume(st, info, s, name, homehost, uuid,
5728 data_offset);
5729
5730 if (info)
5731 mpb_size = disks_to_mpb_size(info->nr_disks);
5732 else
5733 mpb_size = MAX_SECTOR_SIZE;
5734
5735 super = alloc_super();
5736 if (super &&
5737 posix_memalign(&super->buf, MAX_SECTOR_SIZE, mpb_size) != 0) {
5738 free_imsm(super);
5739 super = NULL;
5740 }
5741 if (!super) {
5742 pr_err("could not allocate superblock\n");
5743 return 0;
5744 }
5745 if (posix_memalign(&super->migr_rec_buf, MAX_SECTOR_SIZE,
5746 MIGR_REC_BUF_SECTORS*MAX_SECTOR_SIZE) != 0) {
5747 pr_err("could not allocate migr_rec buffer\n");
5748 free(super->buf);
5749 free_imsm(super);
5750 return 0;
5751 }
5752 memset(super->buf, 0, mpb_size);
5753 mpb = super->buf;
5754 mpb->mpb_size = __cpu_to_le32(mpb_size);
5755 st->sb = super;
5756
5757 if (info == NULL) {
5758 /* zeroing superblock */
5759 return 0;
5760 }
5761
5762 imsm_update_version_info(super);
5763 return 1;
5764 }
5765
5766 static int drive_validate_sector_size(struct intel_super *super, struct dl *dl)
5767 {
5768 unsigned int member_sector_size;
5769
5770 if (!is_fd_valid(dl->fd)) {
5771 pr_err("Invalid file descriptor for %s\n", dl->devname);
5772 return 0;
5773 }
5774
5775 if (!get_dev_sector_size(dl->fd, dl->devname, &member_sector_size))
5776 return 0;
5777 if (member_sector_size != super->sector_size)
5778 return 0;
5779 return 1;
5780 }
5781
5782 static int add_to_super_imsm_volume(struct supertype *st, mdu_disk_info_t *dk,
5783 int fd, char *devname)
5784 {
5785 struct intel_super *super = st->sb;
5786 struct imsm_super *mpb = super->anchor;
5787 struct imsm_disk *_disk;
5788 struct imsm_dev *dev;
5789 struct imsm_map *map;
5790 struct dl *dl, *df;
5791 int slot;
5792 int autolayout = 0;
5793
5794 if (!is_fd_valid(fd))
5795 autolayout = 1;
5796
5797 dev = get_imsm_dev(super, super->current_vol);
5798 map = get_imsm_map(dev, MAP_0);
5799
5800 if (! (dk->state & (1<<MD_DISK_SYNC))) {
5801 pr_err("%s: Cannot add spare devices to IMSM volume\n",
5802 devname);
5803 return 1;
5804 }
5805
5806 for (dl = super->disks; dl ; dl = dl->next) {
5807 if (autolayout) {
5808 if (dl->raiddisk == dk->raid_disk)
5809 break;
5810 } else if (dl->major == dk->major && dl->minor == dk->minor)
5811 break;
5812 }
5813
5814 if (!dl) {
5815 if (!autolayout)
5816 pr_err("%s is not a member of the same container.\n",
5817 devname);
5818 return 1;
5819 }
5820
5821 if (!autolayout && super->current_vol > 0) {
5822 int _slot = get_disk_slot_in_dev(super, 0, dl->index);
5823
5824 if (_slot != dk->raid_disk) {
5825 pr_err("Member %s is in %d slot for the first volume, but is in %d slot for a new volume.\n",
5826 dl->devname, _slot, dk->raid_disk);
5827 pr_err("Raid members are in different order than for the first volume, aborting.\n");
5828 return 1;
5829 }
5830 }
5831
5832 if (mpb->num_disks == 0)
5833 if (!get_dev_sector_size(dl->fd, dl->devname,
5834 &super->sector_size))
5835 return 1;
5836
5837 if (!drive_validate_sector_size(super, dl)) {
5838 pr_err("Combining drives of different sector size in one volume is not allowed\n");
5839 return 1;
5840 }
5841
5842 /* add a pristine spare to the metadata */
5843 if (dl->index < 0) {
5844 dl->index = super->anchor->num_disks;
5845 super->anchor->num_disks++;
5846 }
5847 /* Check the device has not already been added */
5848 slot = get_imsm_disk_slot(map, dl->index);
5849 if (slot >= 0 &&
5850 (get_imsm_ord_tbl_ent(dev, slot, MAP_X) & IMSM_ORD_REBUILD) == 0) {
5851 pr_err("%s has been included in this array twice\n",
5852 devname);
5853 return 1;
5854 }
5855 set_imsm_ord_tbl_ent(map, dk->raid_disk, dl->index);
5856 dl->disk.status = CONFIGURED_DISK;
5857
5858 /* update size of 'missing' disks to be at least as large as the
5859 * largest acitve member (we only have dummy missing disks when
5860 * creating the first volume)
5861 */
5862 if (super->current_vol == 0) {
5863 for (df = super->missing; df; df = df->next) {
5864 if (total_blocks(&dl->disk) > total_blocks(&df->disk))
5865 set_total_blocks(&df->disk, total_blocks(&dl->disk));
5866 _disk = __get_imsm_disk(mpb, df->index);
5867 *_disk = df->disk;
5868 }
5869 }
5870
5871 /* refresh unset/failed slots to point to valid 'missing' entries */
5872 for (df = super->missing; df; df = df->next)
5873 for (slot = 0; slot < mpb->num_disks; slot++) {
5874 __u32 ord = get_imsm_ord_tbl_ent(dev, slot, MAP_X);
5875
5876 if ((ord & IMSM_ORD_REBUILD) == 0)
5877 continue;
5878 set_imsm_ord_tbl_ent(map, slot, df->index | IMSM_ORD_REBUILD);
5879 if (is_gen_migration(dev)) {
5880 struct imsm_map *map2 = get_imsm_map(dev,
5881 MAP_1);
5882 int slot2 = get_imsm_disk_slot(map2, df->index);
5883 if (slot2 < map2->num_members && slot2 >= 0) {
5884 __u32 ord2 = get_imsm_ord_tbl_ent(dev,
5885 slot2,
5886 MAP_1);
5887 if ((unsigned)df->index ==
5888 ord_to_idx(ord2))
5889 set_imsm_ord_tbl_ent(map2,
5890 slot2,
5891 df->index |
5892 IMSM_ORD_REBUILD);
5893 }
5894 }
5895 dprintf("set slot:%d to missing disk:%d\n", slot, df->index);
5896 break;
5897 }
5898
5899 /* if we are creating the first raid device update the family number */
5900 if (super->current_vol == 0) {
5901 __u32 sum;
5902 struct imsm_dev *_dev = __get_imsm_dev(mpb, 0);
5903
5904 _disk = __get_imsm_disk(mpb, dl->index);
5905 if (!_disk) {
5906 pr_err("BUG mpb setup error\n");
5907 return 1;
5908 }
5909 *_dev = *dev;
5910 *_disk = dl->disk;
5911 sum = random32();
5912 sum += __gen_imsm_checksum(mpb);
5913 mpb->family_num = __cpu_to_le32(sum);
5914 mpb->orig_family_num = mpb->family_num;
5915 mpb->creation_time = __cpu_to_le64((__u64)time(NULL));
5916 }
5917 super->current_disk = dl;
5918 return 0;
5919 }
5920
5921 /* mark_spare()
5922 * Function marks disk as spare and restores disk serial
5923 * in case it was previously marked as failed by takeover operation
5924 * reruns:
5925 * -1 : critical error
5926 * 0 : disk is marked as spare but serial is not set
5927 * 1 : success
5928 */
5929 int mark_spare(struct dl *disk)
5930 {
5931 __u8 serial[MAX_RAID_SERIAL_LEN];
5932 int ret_val = -1;
5933
5934 if (!disk)
5935 return ret_val;
5936
5937 ret_val = 0;
5938 if (!imsm_read_serial(disk->fd, NULL, serial, MAX_RAID_SERIAL_LEN)) {
5939 /* Restore disk serial number, because takeover marks disk
5940 * as failed and adds to serial ':0' before it becomes
5941 * a spare disk.
5942 */
5943 serialcpy(disk->serial, serial);
5944 serialcpy(disk->disk.serial, serial);
5945 ret_val = 1;
5946 }
5947 disk->disk.status = SPARE_DISK;
5948 disk->index = -1;
5949
5950 return ret_val;
5951 }
5952
5953
5954 static int write_super_imsm_spare(struct intel_super *super, struct dl *d);
5955
5956 static int add_to_super_imsm(struct supertype *st, mdu_disk_info_t *dk,
5957 int fd, char *devname,
5958 unsigned long long data_offset)
5959 {
5960 struct intel_super *super = st->sb;
5961 struct dl *dd;
5962 unsigned long long size;
5963 unsigned int member_sector_size;
5964 __u32 id;
5965 int rv;
5966 struct stat stb;
5967
5968 /* If we are on an RAID enabled platform check that the disk is
5969 * attached to the raid controller.
5970 * We do not need to test disks attachment for container based additions,
5971 * they shall be already tested when container was created/assembled.
5972 */
5973 rv = find_intel_hba_capability(fd, super, devname);
5974 /* no orom/efi or non-intel hba of the disk */
5975 if (rv != 0) {
5976 dprintf("capability: %p fd: %d ret: %d\n",
5977 super->orom, fd, rv);
5978 return 1;
5979 }
5980
5981 if (super->current_vol >= 0)
5982 return add_to_super_imsm_volume(st, dk, fd, devname);
5983
5984 fstat(fd, &stb);
5985 dd = xcalloc(sizeof(*dd), 1);
5986 dd->major = major(stb.st_rdev);
5987 dd->minor = minor(stb.st_rdev);
5988 dd->devname = devname ? xstrdup(devname) : NULL;
5989 dd->fd = fd;
5990 dd->e = NULL;
5991 dd->action = DISK_ADD;
5992 rv = imsm_read_serial(fd, devname, dd->serial, MAX_RAID_SERIAL_LEN);
5993 if (rv) {
5994 pr_err("failed to retrieve scsi serial, aborting\n");
5995 __free_imsm_disk(dd, 0);
5996 abort();
5997 }
5998
5999 if (super->hba && ((super->hba->type == SYS_DEV_NVME) ||
6000 (super->hba->type == SYS_DEV_VMD))) {
6001 int i;
6002 char cntrl_path[PATH_MAX];
6003 char *cntrl_name;
6004 char pci_dev_path[PATH_MAX];
6005
6006 if (!diskfd_to_devpath(fd, 2, pci_dev_path) ||
6007 !diskfd_to_devpath(fd, 1, cntrl_path)) {
6008 pr_err("failed to get dev paths, aborting\n");
6009 __free_imsm_disk(dd, 0);
6010 return 1;
6011 }
6012
6013 cntrl_name = basename(cntrl_path);
6014 if (is_multipath_nvme(fd))
6015 pr_err("%s controller supports Multi-Path I/O, Intel (R) VROC does not support multipathing\n",
6016 cntrl_name);
6017
6018 if (devpath_to_vendor(pci_dev_path) == 0x8086) {
6019 /*
6020 * If Intel's NVMe drive has serial ended with
6021 * "-A","-B","-1" or "-2" it means that this is "x8"
6022 * device (double drive on single PCIe card).
6023 * User should be warned about potential data loss.
6024 */
6025 for (i = MAX_RAID_SERIAL_LEN-1; i > 0; i--) {
6026 /* Skip empty character at the end */
6027 if (dd->serial[i] == 0)
6028 continue;
6029
6030 if (((dd->serial[i] == 'A') ||
6031 (dd->serial[i] == 'B') ||
6032 (dd->serial[i] == '1') ||
6033 (dd->serial[i] == '2')) &&
6034 (dd->serial[i-1] == '-'))
6035 pr_err("\tThe action you are about to take may put your data at risk.\n"
6036 "\tPlease note that x8 devices may consist of two separate x4 devices "
6037 "located on a single PCIe port.\n"
6038 "\tRAID 0 is the only supported configuration for this type of x8 device.\n");
6039 break;
6040 }
6041 } else if (super->hba->type == SYS_DEV_VMD && super->orom &&
6042 !imsm_orom_has_tpv_support(super->orom)) {
6043 pr_err("\tPlatform configuration does not support non-Intel NVMe drives.\n"
6044 "\tPlease refer to Intel(R) RSTe/VROC user guide.\n");
6045 __free_imsm_disk(dd, 0);
6046 return 1;
6047 }
6048 }
6049
6050 get_dev_size(fd, NULL, &size);
6051 if (!get_dev_sector_size(fd, NULL, &member_sector_size)) {
6052 __free_imsm_disk(dd, 0);
6053 return 1;
6054 }
6055
6056 if (super->sector_size == 0) {
6057 /* this a first device, so sector_size is not set yet */
6058 super->sector_size = member_sector_size;
6059 }
6060
6061 /* clear migr_rec when adding disk to container */
6062 memset(super->migr_rec_buf, 0, MIGR_REC_BUF_SECTORS*MAX_SECTOR_SIZE);
6063 if (lseek64(fd, size - MIGR_REC_SECTOR_POSITION*member_sector_size,
6064 SEEK_SET) >= 0) {
6065 if ((unsigned int)write(fd, super->migr_rec_buf,
6066 MIGR_REC_BUF_SECTORS*member_sector_size) !=
6067 MIGR_REC_BUF_SECTORS*member_sector_size)
6068 perror("Write migr_rec failed");
6069 }
6070
6071 size /= 512;
6072 serialcpy(dd->disk.serial, dd->serial);
6073 set_total_blocks(&dd->disk, size);
6074 if (__le32_to_cpu(dd->disk.total_blocks_hi) > 0) {
6075 struct imsm_super *mpb = super->anchor;
6076 mpb->attributes |= MPB_ATTRIB_2TB_DISK;
6077 }
6078 mark_spare(dd);
6079 if (sysfs_disk_to_scsi_id(fd, &id) == 0)
6080 dd->disk.scsi_id = __cpu_to_le32(id);
6081 else
6082 dd->disk.scsi_id = __cpu_to_le32(0);
6083
6084 if (st->update_tail) {
6085 dd->next = super->disk_mgmt_list;
6086 super->disk_mgmt_list = dd;
6087 } else {
6088 /* this is called outside of mdmon
6089 * write initial spare metadata
6090 * mdmon will overwrite it.
6091 */
6092 dd->next = super->disks;
6093 super->disks = dd;
6094 write_super_imsm_spare(super, dd);
6095 }
6096
6097 return 0;
6098 }
6099
6100 static int remove_from_super_imsm(struct supertype *st, mdu_disk_info_t *dk)
6101 {
6102 struct intel_super *super = st->sb;
6103 struct dl *dd;
6104
6105 /* remove from super works only in mdmon - for communication
6106 * manager - monitor. Check if communication memory buffer
6107 * is prepared.
6108 */
6109 if (!st->update_tail) {
6110 pr_err("shall be used in mdmon context only\n");
6111 return 1;
6112 }
6113 dd = xcalloc(1, sizeof(*dd));
6114 dd->major = dk->major;
6115 dd->minor = dk->minor;
6116 dd->fd = -1;
6117 mark_spare(dd);
6118 dd->action = DISK_REMOVE;
6119
6120 dd->next = super->disk_mgmt_list;
6121 super->disk_mgmt_list = dd;
6122
6123 return 0;
6124 }
6125
6126 static int store_imsm_mpb(int fd, struct imsm_super *mpb);
6127
6128 static union {
6129 char buf[MAX_SECTOR_SIZE];
6130 struct imsm_super anchor;
6131 } spare_record __attribute__ ((aligned(MAX_SECTOR_SIZE)));
6132
6133
6134 static int write_super_imsm_spare(struct intel_super *super, struct dl *d)
6135 {
6136 struct imsm_super *spare = &spare_record.anchor;
6137 __u32 sum;
6138
6139 if (d->index != -1)
6140 return 1;
6141
6142 spare->mpb_size = __cpu_to_le32(sizeof(struct imsm_super));
6143 spare->generation_num = __cpu_to_le32(1UL);
6144 spare->num_disks = 1;
6145 spare->num_raid_devs = 0;
6146 spare->pwr_cycle_count = __cpu_to_le32(1);
6147
6148 imsm_write_signature(spare);
6149
6150 spare->disk[0] = d->disk;
6151 if (__le32_to_cpu(d->disk.total_blocks_hi) > 0)
6152 spare->attributes |= MPB_ATTRIB_2TB_DISK;
6153
6154 if (super->sector_size == 4096)
6155 convert_to_4k_imsm_disk(&spare->disk[0]);
6156
6157 sum = __gen_imsm_checksum(spare);
6158 spare->family_num = __cpu_to_le32(sum);
6159 spare->orig_family_num = 0;
6160 sum = __gen_imsm_checksum(spare);
6161 spare->check_sum = __cpu_to_le32(sum);
6162
6163 if (store_imsm_mpb(d->fd, spare)) {
6164 pr_err("failed for device %d:%d %s\n",
6165 d->major, d->minor, strerror(errno));
6166 return 1;
6167 }
6168
6169 return 0;
6170 }
6171 /* spare records have their own family number and do not have any defined raid
6172 * devices
6173 */
6174 static int write_super_imsm_spares(struct intel_super *super, int doclose)
6175 {
6176 struct dl *d;
6177
6178 for (d = super->disks; d; d = d->next) {
6179 if (d->index != -1)
6180 continue;
6181
6182 if (write_super_imsm_spare(super, d))
6183 return 1;
6184
6185 if (doclose)
6186 close_fd(&d->fd);
6187 }
6188
6189 return 0;
6190 }
6191
6192 static int write_super_imsm(struct supertype *st, int doclose)
6193 {
6194 struct intel_super *super = st->sb;
6195 unsigned int sector_size = super->sector_size;
6196 struct imsm_super *mpb = super->anchor;
6197 struct dl *d;
6198 __u32 generation;
6199 __u32 sum;
6200 int spares = 0;
6201 int i;
6202 __u32 mpb_size = sizeof(struct imsm_super) - sizeof(struct imsm_disk);
6203 int num_disks = 0;
6204 int clear_migration_record = 1;
6205 __u32 bbm_log_size;
6206
6207 /* 'generation' is incremented everytime the metadata is written */
6208 generation = __le32_to_cpu(mpb->generation_num);
6209 generation++;
6210 mpb->generation_num = __cpu_to_le32(generation);
6211
6212 /* fix up cases where previous mdadm releases failed to set
6213 * orig_family_num
6214 */
6215 if (mpb->orig_family_num == 0)
6216 mpb->orig_family_num = mpb->family_num;
6217
6218 for (d = super->disks; d; d = d->next) {
6219 if (d->index == -1)
6220 spares++;
6221 else {
6222 mpb->disk[d->index] = d->disk;
6223 num_disks++;
6224 }
6225 }
6226 for (d = super->missing; d; d = d->next) {
6227 mpb->disk[d->index] = d->disk;
6228 num_disks++;
6229 }
6230 mpb->num_disks = num_disks;
6231 mpb_size += sizeof(struct imsm_disk) * mpb->num_disks;
6232
6233 for (i = 0; i < mpb->num_raid_devs; i++) {
6234 struct imsm_dev *dev = __get_imsm_dev(mpb, i);
6235 struct imsm_dev *dev2 = get_imsm_dev(super, i);
6236
6237 imsm_copy_dev(dev, dev2);
6238 mpb_size += sizeof_imsm_dev(dev, 0);
6239
6240 if (is_gen_migration(dev2))
6241 clear_migration_record = 0;
6242 }
6243
6244 bbm_log_size = get_imsm_bbm_log_size(super->bbm_log);
6245
6246 if (bbm_log_size) {
6247 memcpy((void *)mpb + mpb_size, super->bbm_log, bbm_log_size);
6248 mpb->attributes |= MPB_ATTRIB_BBM;
6249 } else
6250 mpb->attributes &= ~MPB_ATTRIB_BBM;
6251
6252 super->anchor->bbm_log_size = __cpu_to_le32(bbm_log_size);
6253 mpb_size += bbm_log_size;
6254 mpb->mpb_size = __cpu_to_le32(mpb_size);
6255
6256 #ifdef DEBUG
6257 assert(super->len == 0 || mpb_size <= super->len);
6258 #endif
6259
6260 /* recalculate checksum */
6261 sum = __gen_imsm_checksum(mpb);
6262 mpb->check_sum = __cpu_to_le32(sum);
6263
6264 if (super->clean_migration_record_by_mdmon) {
6265 clear_migration_record = 1;
6266 super->clean_migration_record_by_mdmon = 0;
6267 }
6268 if (clear_migration_record)
6269 memset(super->migr_rec_buf, 0,
6270 MIGR_REC_BUF_SECTORS*MAX_SECTOR_SIZE);
6271
6272 if (sector_size == 4096)
6273 convert_to_4k(super);
6274
6275 /* write the mpb for disks that compose raid devices */
6276 for (d = super->disks; d ; d = d->next) {
6277 if (d->index < 0 || is_failed(&d->disk))
6278 continue;
6279
6280 if (clear_migration_record) {
6281 unsigned long long dsize;
6282
6283 get_dev_size(d->fd, NULL, &dsize);
6284 if (lseek64(d->fd, dsize - sector_size,
6285 SEEK_SET) >= 0) {
6286 if ((unsigned int)write(d->fd,
6287 super->migr_rec_buf,
6288 MIGR_REC_BUF_SECTORS*sector_size) !=
6289 MIGR_REC_BUF_SECTORS*sector_size)
6290 perror("Write migr_rec failed");
6291 }
6292 }
6293
6294 if (store_imsm_mpb(d->fd, mpb))
6295 fprintf(stderr,
6296 "failed for device %d:%d (fd: %d)%s\n",
6297 d->major, d->minor,
6298 d->fd, strerror(errno));
6299
6300 if (doclose)
6301 close_fd(&d->fd);
6302 }
6303
6304 if (spares)
6305 return write_super_imsm_spares(super, doclose);
6306
6307 return 0;
6308 }
6309
6310 static int create_array(struct supertype *st, int dev_idx)
6311 {
6312 size_t len;
6313 struct imsm_update_create_array *u;
6314 struct intel_super *super = st->sb;
6315 struct imsm_dev *dev = get_imsm_dev(super, dev_idx);
6316 struct imsm_map *map = get_imsm_map(dev, MAP_0);
6317 struct disk_info *inf;
6318 struct imsm_disk *disk;
6319 int i;
6320
6321 len = sizeof(*u) - sizeof(*dev) + sizeof_imsm_dev(dev, 0) +
6322 sizeof(*inf) * map->num_members;
6323 u = xmalloc(len);
6324 u->type = update_create_array;
6325 u->dev_idx = dev_idx;
6326 imsm_copy_dev(&u->dev, dev);
6327 inf = get_disk_info(u);
6328 for (i = 0; i < map->num_members; i++) {
6329 int idx = get_imsm_disk_idx(dev, i, MAP_X);
6330
6331 disk = get_imsm_disk(super, idx);
6332 if (!disk)
6333 disk = get_imsm_missing(super, idx);
6334 serialcpy(inf[i].serial, disk->serial);
6335 }
6336 append_metadata_update(st, u, len);
6337
6338 return 0;
6339 }
6340
6341 static int mgmt_disk(struct supertype *st)
6342 {
6343 struct intel_super *super = st->sb;
6344 size_t len;
6345 struct imsm_update_add_remove_disk *u;
6346
6347 if (!super->disk_mgmt_list)
6348 return 0;
6349
6350 len = sizeof(*u);
6351 u = xmalloc(len);
6352 u->type = update_add_remove_disk;
6353 append_metadata_update(st, u, len);
6354
6355 return 0;
6356 }
6357
6358 __u32 crc32c_le(__u32 crc, unsigned char const *p, size_t len);
6359
6360 static int write_ppl_header(unsigned long long ppl_sector, int fd, void *buf)
6361 {
6362 struct ppl_header *ppl_hdr = buf;
6363 int ret;
6364
6365 ppl_hdr->checksum = __cpu_to_le32(~crc32c_le(~0, buf, PPL_HEADER_SIZE));
6366
6367 if (lseek64(fd, ppl_sector * 512, SEEK_SET) < 0) {
6368 ret = -errno;
6369 perror("Failed to seek to PPL header location");
6370 return ret;
6371 }
6372
6373 if (write(fd, buf, PPL_HEADER_SIZE) != PPL_HEADER_SIZE) {
6374 ret = -errno;
6375 perror("Write PPL header failed");
6376 return ret;
6377 }
6378
6379 fsync(fd);
6380
6381 return 0;
6382 }
6383
6384 static int write_init_ppl_imsm(struct supertype *st, struct mdinfo *info, int fd)
6385 {
6386 struct intel_super *super = st->sb;
6387 void *buf;
6388 struct ppl_header *ppl_hdr;
6389 int ret;
6390
6391 /* first clear entire ppl space */
6392 ret = zero_disk_range(fd, info->ppl_sector, info->ppl_size);
6393 if (ret)
6394 return ret;
6395
6396 ret = posix_memalign(&buf, MAX_SECTOR_SIZE, PPL_HEADER_SIZE);
6397 if (ret) {
6398 pr_err("Failed to allocate PPL header buffer\n");
6399 return -ret;
6400 }
6401
6402 memset(buf, 0, PPL_HEADER_SIZE);
6403 ppl_hdr = buf;
6404 memset(ppl_hdr->reserved, 0xff, PPL_HDR_RESERVED);
6405 ppl_hdr->signature = __cpu_to_le32(super->anchor->orig_family_num);
6406
6407 if (info->mismatch_cnt) {
6408 /*
6409 * We are overwriting an invalid ppl. Make one entry with wrong
6410 * checksum to prevent the kernel from skipping resync.
6411 */
6412 ppl_hdr->entries_count = __cpu_to_le32(1);
6413 ppl_hdr->entries[0].checksum = ~0;
6414 }
6415
6416 ret = write_ppl_header(info->ppl_sector, fd, buf);
6417
6418 free(buf);
6419 return ret;
6420 }
6421
6422 static int is_rebuilding(struct imsm_dev *dev);
6423
6424 static int validate_ppl_imsm(struct supertype *st, struct mdinfo *info,
6425 struct mdinfo *disk)
6426 {
6427 struct intel_super *super = st->sb;
6428 struct dl *d;
6429 void *buf_orig, *buf, *buf_prev = NULL;
6430 int ret = 0;
6431 struct ppl_header *ppl_hdr = NULL;
6432 __u32 crc;
6433 struct imsm_dev *dev;
6434 __u32 idx;
6435 unsigned int i;
6436 unsigned long long ppl_offset = 0;
6437 unsigned long long prev_gen_num = 0;
6438
6439 if (disk->disk.raid_disk < 0)
6440 return 0;
6441
6442 dev = get_imsm_dev(super, info->container_member);
6443 idx = get_imsm_disk_idx(dev, disk->disk.raid_disk, MAP_0);
6444 d = get_imsm_dl_disk(super, idx);
6445
6446 if (!d || d->index < 0 || is_failed(&d->disk))
6447 return 0;
6448
6449 if (posix_memalign(&buf_orig, MAX_SECTOR_SIZE, PPL_HEADER_SIZE * 2)) {
6450 pr_err("Failed to allocate PPL header buffer\n");
6451 return -1;
6452 }
6453 buf = buf_orig;
6454
6455 ret = 1;
6456 while (ppl_offset < MULTIPLE_PPL_AREA_SIZE_IMSM) {
6457 void *tmp;
6458
6459 dprintf("Checking potential PPL at offset: %llu\n", ppl_offset);
6460
6461 if (lseek64(d->fd, info->ppl_sector * 512 + ppl_offset,
6462 SEEK_SET) < 0) {
6463 perror("Failed to seek to PPL header location");
6464 ret = -1;
6465 break;
6466 }
6467
6468 if (read(d->fd, buf, PPL_HEADER_SIZE) != PPL_HEADER_SIZE) {
6469 perror("Read PPL header failed");
6470 ret = -1;
6471 break;
6472 }
6473
6474 ppl_hdr = buf;
6475
6476 crc = __le32_to_cpu(ppl_hdr->checksum);
6477 ppl_hdr->checksum = 0;
6478
6479 if (crc != ~crc32c_le(~0, buf, PPL_HEADER_SIZE)) {
6480 dprintf("Wrong PPL header checksum on %s\n",
6481 d->devname);
6482 break;
6483 }
6484
6485 if (prev_gen_num > __le64_to_cpu(ppl_hdr->generation)) {
6486 /* previous was newest, it was already checked */
6487 break;
6488 }
6489
6490 if ((__le32_to_cpu(ppl_hdr->signature) !=
6491 super->anchor->orig_family_num)) {
6492 dprintf("Wrong PPL header signature on %s\n",
6493 d->devname);
6494 ret = 1;
6495 break;
6496 }
6497
6498 ret = 0;
6499 prev_gen_num = __le64_to_cpu(ppl_hdr->generation);
6500
6501 ppl_offset += PPL_HEADER_SIZE;
6502 for (i = 0; i < __le32_to_cpu(ppl_hdr->entries_count); i++)
6503 ppl_offset +=
6504 __le32_to_cpu(ppl_hdr->entries[i].pp_size);
6505
6506 if (!buf_prev)
6507 buf_prev = buf + PPL_HEADER_SIZE;
6508 tmp = buf_prev;
6509 buf_prev = buf;
6510 buf = tmp;
6511 }
6512
6513 if (buf_prev) {
6514 buf = buf_prev;
6515 ppl_hdr = buf_prev;
6516 }
6517
6518 /*
6519 * Update metadata to use mutliple PPLs area (1MB).
6520 * This is done once for all RAID members
6521 */
6522 if (info->consistency_policy == CONSISTENCY_POLICY_PPL &&
6523 info->ppl_size != (MULTIPLE_PPL_AREA_SIZE_IMSM >> 9)) {
6524 char subarray[20];
6525 struct mdinfo *member_dev;
6526
6527 sprintf(subarray, "%d", info->container_member);
6528
6529 if (mdmon_running(st->container_devnm))
6530 st->update_tail = &st->updates;
6531
6532 if (st->ss->update_subarray(st, subarray, UOPT_PPL, NULL)) {
6533 pr_err("Failed to update subarray %s\n",
6534 subarray);
6535 } else {
6536 if (st->update_tail)
6537 flush_metadata_updates(st);
6538 else
6539 st->ss->sync_metadata(st);
6540 info->ppl_size = (MULTIPLE_PPL_AREA_SIZE_IMSM >> 9);
6541 for (member_dev = info->devs; member_dev;
6542 member_dev = member_dev->next)
6543 member_dev->ppl_size =
6544 (MULTIPLE_PPL_AREA_SIZE_IMSM >> 9);
6545 }
6546 }
6547
6548 if (ret == 1) {
6549 struct imsm_map *map = get_imsm_map(dev, MAP_X);
6550
6551 if (map->map_state == IMSM_T_STATE_UNINITIALIZED ||
6552 (map->map_state == IMSM_T_STATE_NORMAL &&
6553 !(dev->vol.dirty & RAIDVOL_DIRTY)) ||
6554 (is_rebuilding(dev) &&
6555 vol_curr_migr_unit(dev) == 0 &&
6556 get_imsm_disk_idx(dev, disk->disk.raid_disk, MAP_1) != idx))
6557 ret = st->ss->write_init_ppl(st, info, d->fd);
6558 else
6559 info->mismatch_cnt++;
6560 } else if (ret == 0 &&
6561 ppl_hdr->entries_count == 0 &&
6562 is_rebuilding(dev) &&
6563 info->resync_start == 0) {
6564 /*
6565 * The header has no entries - add a single empty entry and
6566 * rewrite the header to prevent the kernel from going into
6567 * resync after an interrupted rebuild.
6568 */
6569 ppl_hdr->entries_count = __cpu_to_le32(1);
6570 ret = write_ppl_header(info->ppl_sector, d->fd, buf);
6571 }
6572
6573 free(buf_orig);
6574
6575 return ret;
6576 }
6577
6578 static int write_init_ppl_imsm_all(struct supertype *st, struct mdinfo *info)
6579 {
6580 struct intel_super *super = st->sb;
6581 struct dl *d;
6582 int ret = 0;
6583
6584 if (info->consistency_policy != CONSISTENCY_POLICY_PPL ||
6585 info->array.level != 5)
6586 return 0;
6587
6588 for (d = super->disks; d ; d = d->next) {
6589 if (d->index < 0 || is_failed(&d->disk))
6590 continue;
6591
6592 ret = st->ss->write_init_ppl(st, info, d->fd);
6593 if (ret)
6594 break;
6595 }
6596
6597 return ret;
6598 }
6599
6600 /*******************************************************************************
6601 * Function: write_init_bitmap_imsm_vol
6602 * Description: Write a bitmap header and prepares the area for the bitmap.
6603 * Parameters:
6604 * st : supertype information
6605 * vol_idx : the volume index to use
6606 *
6607 * Returns:
6608 * 0 : success
6609 * -1 : fail
6610 ******************************************************************************/
6611 static int write_init_bitmap_imsm_vol(struct supertype *st, int vol_idx)
6612 {
6613 struct intel_super *super = st->sb;
6614 int prev_current_vol = super->current_vol;
6615 struct dl *d;
6616 int ret = 0;
6617
6618 super->current_vol = vol_idx;
6619 for (d = super->disks; d; d = d->next) {
6620 if (d->index < 0 || is_failed(&d->disk))
6621 continue;
6622 ret = st->ss->write_bitmap(st, d->fd, NoUpdate);
6623 if (ret)
6624 break;
6625 }
6626 super->current_vol = prev_current_vol;
6627 return ret;
6628 }
6629
6630 /*******************************************************************************
6631 * Function: write_init_bitmap_imsm_all
6632 * Description: Write a bitmap header and prepares the area for the bitmap.
6633 * Operation is executed for volumes with CONSISTENCY_POLICY_BITMAP.
6634 * Parameters:
6635 * st : supertype information
6636 * info : info about the volume where the bitmap should be written
6637 * vol_idx : the volume index to use
6638 *
6639 * Returns:
6640 * 0 : success
6641 * -1 : fail
6642 ******************************************************************************/
6643 static int write_init_bitmap_imsm_all(struct supertype *st, struct mdinfo *info,
6644 int vol_idx)
6645 {
6646 int ret = 0;
6647
6648 if (info && (info->consistency_policy == CONSISTENCY_POLICY_BITMAP))
6649 ret = write_init_bitmap_imsm_vol(st, vol_idx);
6650
6651 return ret;
6652 }
6653
6654 static int write_init_super_imsm(struct supertype *st)
6655 {
6656 struct intel_super *super = st->sb;
6657 int current_vol = super->current_vol;
6658 int rv = 0;
6659 struct mdinfo info;
6660
6661 getinfo_super_imsm(st, &info, NULL);
6662
6663 /* we are done with current_vol reset it to point st at the container */
6664 super->current_vol = -1;
6665
6666 if (st->update_tail) {
6667 /* queue the recently created array / added disk
6668 * as a metadata update */
6669
6670 /* determine if we are creating a volume or adding a disk */
6671 if (current_vol < 0) {
6672 /* in the mgmt (add/remove) disk case we are running
6673 * in mdmon context, so don't close fd's
6674 */
6675 rv = mgmt_disk(st);
6676 } else {
6677 /* adding the second volume to the array */
6678 rv = write_init_ppl_imsm_all(st, &info);
6679 if (!rv)
6680 rv = write_init_bitmap_imsm_all(st, &info, current_vol);
6681 if (!rv)
6682 rv = create_array(st, current_vol);
6683 }
6684 } else {
6685 struct dl *d;
6686 for (d = super->disks; d; d = d->next)
6687 Kill(d->devname, NULL, 0, -1, 1);
6688 if (current_vol >= 0) {
6689 rv = write_init_ppl_imsm_all(st, &info);
6690 if (!rv)
6691 rv = write_init_bitmap_imsm_all(st, &info, current_vol);
6692 }
6693
6694 if (!rv)
6695 rv = write_super_imsm(st, 1);
6696 }
6697
6698 return rv;
6699 }
6700
6701 static int store_super_imsm(struct supertype *st, int fd)
6702 {
6703 struct intel_super *super = st->sb;
6704 struct imsm_super *mpb = super ? super->anchor : NULL;
6705
6706 if (!mpb)
6707 return 1;
6708
6709 if (super->sector_size == 4096)
6710 convert_to_4k(super);
6711 return store_imsm_mpb(fd, mpb);
6712 }
6713
6714 static int validate_geometry_imsm_container(struct supertype *st, int level,
6715 int raiddisks,
6716 unsigned long long data_offset,
6717 char *dev,
6718 unsigned long long *freesize,
6719 int verbose)
6720 {
6721 int fd;
6722 unsigned long long ldsize;
6723 struct intel_super *super = NULL;
6724 int rv = 0;
6725
6726 if (!is_container(level))
6727 return 0;
6728 if (!dev)
6729 return 1;
6730
6731 fd = dev_open(dev, O_RDONLY|O_EXCL);
6732 if (!is_fd_valid(fd)) {
6733 pr_vrb("imsm: Cannot open %s: %s\n", dev, strerror(errno));
6734 return 0;
6735 }
6736 if (!get_dev_size(fd, dev, &ldsize))
6737 goto exit;
6738
6739 /* capabilities retrieve could be possible
6740 * note that there is no fd for the disks in array.
6741 */
6742 super = alloc_super();
6743 if (!super)
6744 goto exit;
6745
6746 if (!get_dev_sector_size(fd, NULL, &super->sector_size))
6747 goto exit;
6748
6749 rv = find_intel_hba_capability(fd, super, verbose > 0 ? dev : NULL);
6750 if (rv != 0) {
6751 #if DEBUG
6752 char str[256];
6753 fd2devname(fd, str);
6754 dprintf("fd: %d %s orom: %p rv: %d raiddisk: %d\n",
6755 fd, str, super->orom, rv, raiddisks);
6756 #endif
6757 /* no orom/efi or non-intel hba of the disk */
6758 rv = 0;
6759 goto exit;
6760 }
6761 if (super->orom) {
6762 if (raiddisks > super->orom->tds) {
6763 if (verbose)
6764 pr_err("%d exceeds maximum number of platform supported disks: %d\n",
6765 raiddisks, super->orom->tds);
6766 goto exit;
6767 }
6768 if ((super->orom->attr & IMSM_OROM_ATTR_2TB_DISK) == 0 &&
6769 (ldsize >> 9) >> 32 > 0) {
6770 if (verbose)
6771 pr_err("%s exceeds maximum platform supported size\n", dev);
6772 goto exit;
6773 }
6774
6775 if (super->hba->type == SYS_DEV_VMD ||
6776 super->hba->type == SYS_DEV_NVME) {
6777 if (!imsm_is_nvme_namespace_supported(fd, 1)) {
6778 if (verbose)
6779 pr_err("NVMe namespace %s is not supported by IMSM\n",
6780 basename(dev));
6781 goto exit;
6782 }
6783 }
6784 }
6785 if (freesize)
6786 *freesize = avail_size_imsm(st, ldsize >> 9, data_offset);
6787 rv = 1;
6788 exit:
6789 if (super)
6790 free_imsm(super);
6791 close(fd);
6792
6793 return rv;
6794 }
6795
6796 static unsigned long long find_size(struct extent *e, int *idx, int num_extents)
6797 {
6798 const unsigned long long base_start = e[*idx].start;
6799 unsigned long long end = base_start + e[*idx].size;
6800 int i;
6801
6802 if (base_start == end)
6803 return 0;
6804
6805 *idx = *idx + 1;
6806 for (i = *idx; i < num_extents; i++) {
6807 /* extend overlapping extents */
6808 if (e[i].start >= base_start &&
6809 e[i].start <= end) {
6810 if (e[i].size == 0)
6811 return 0;
6812 if (e[i].start + e[i].size > end)
6813 end = e[i].start + e[i].size;
6814 } else if (e[i].start > end) {
6815 *idx = i;
6816 break;
6817 }
6818 }
6819
6820 return end - base_start;
6821 }
6822
6823 /** merge_extents() - analyze extents and get free size.
6824 * @super: Intel metadata, not NULL.
6825 * @expanding: if set, we are expanding &super->current_vol.
6826 *
6827 * Build a composite disk with all known extents and generate a size given the
6828 * "all disks in an array must share a common start offset" constraint.
6829 * If a volume is expanded, then return free space after the volume.
6830 *
6831 * Return: Free space or 0 on failure.
6832 */
6833 static unsigned long long merge_extents(struct intel_super *super, const bool expanding)
6834 {
6835 struct extent *e;
6836 struct dl *dl;
6837 int i, j, pos_vol_idx = -1;
6838 int extent_idx = 0;
6839 int sum_extents = 0;
6840 unsigned long long pos = 0;
6841 unsigned long long start = 0;
6842 unsigned long long free_size = 0;
6843
6844 unsigned long pre_reservation = 0;
6845 unsigned long post_reservation = IMSM_RESERVED_SECTORS;
6846 unsigned long reservation_size;
6847
6848 for (dl = super->disks; dl; dl = dl->next)
6849 if (dl->e)
6850 sum_extents += dl->extent_cnt;
6851 e = xcalloc(sum_extents, sizeof(struct extent));
6852
6853 /* coalesce and sort all extents. also, check to see if we need to
6854 * reserve space between member arrays
6855 */
6856 j = 0;
6857 for (dl = super->disks; dl; dl = dl->next) {
6858 if (!dl->e)
6859 continue;
6860 for (i = 0; i < dl->extent_cnt; i++)
6861 e[j++] = dl->e[i];
6862 }
6863 qsort(e, sum_extents, sizeof(*e), cmp_extent);
6864
6865 /* merge extents */
6866 i = 0;
6867 j = 0;
6868 while (i < sum_extents) {
6869 e[j].start = e[i].start;
6870 e[j].vol = e[i].vol;
6871 e[j].size = find_size(e, &i, sum_extents);
6872 j++;
6873 if (e[j-1].size == 0)
6874 break;
6875 }
6876
6877 i = 0;
6878 do {
6879 unsigned long long esize = e[i].start - pos;
6880
6881 if (expanding ? pos_vol_idx == super->current_vol : esize >= free_size) {
6882 free_size = esize;
6883 start = pos;
6884 extent_idx = i;
6885 }
6886
6887 pos = e[i].start + e[i].size;
6888 pos_vol_idx = e[i].vol;
6889
6890 i++;
6891 } while (e[i-1].size);
6892
6893 if (free_size == 0) {
6894 dprintf("imsm: Cannot find free size.\n");
6895 free(e);
6896 return 0;
6897 }
6898
6899 if (!expanding && extent_idx != 0)
6900 /*
6901 * Not a real first volume in a container is created, pre_reservation is needed.
6902 */
6903 pre_reservation = IMSM_RESERVED_SECTORS;
6904
6905 if (e[extent_idx].size == 0)
6906 /*
6907 * extent_idx points to the metadata, post_reservation is allready done.
6908 */
6909 post_reservation = 0;
6910 free(e);
6911
6912 reservation_size = pre_reservation + post_reservation;
6913
6914 if (free_size < reservation_size) {
6915 dprintf("imsm: Reservation size is greater than free space.\n");
6916 return 0;
6917 }
6918
6919 super->create_offset = start + pre_reservation;
6920 return free_size - reservation_size;
6921 }
6922
6923 /**
6924 * is_raid_level_supported() - check if this count of drives and level is supported by platform.
6925 * @orom: hardware properties, could be NULL.
6926 * @level: requested raid level.
6927 * @raiddisks: requested disk count.
6928 *
6929 * IMSM UEFI/OROM does not provide information about supported count of raid disks
6930 * for particular level. That is why it is hardcoded.
6931 * It is recommended to not allow of usage other levels than supported,
6932 * IMSM code is not tested against different level implementations.
6933 *
6934 * Return: true if supported, false otherwise.
6935 */
6936 static bool is_raid_level_supported(const struct imsm_orom *orom, int level, int raiddisks)
6937 {
6938 int idx;
6939
6940 for (idx = 0; imsm_level_ops[idx].name; idx++) {
6941 if (imsm_level_ops[idx].level == level)
6942 break;
6943 }
6944
6945 if (!imsm_level_ops[idx].name)
6946 return false;
6947
6948 if (!imsm_level_ops[idx].is_raiddisks_count_supported(raiddisks))
6949 return false;
6950
6951 if (!orom)
6952 return true;
6953
6954 if (imsm_level_ops[idx].is_level_supported(orom))
6955 return true;
6956
6957 return false;
6958 }
6959
6960 static int
6961 active_arrays_by_format(char *name, char* hba, struct md_list **devlist,
6962 int dpa, int verbose)
6963 {
6964 struct mdstat_ent *mdstat = mdstat_read(0, 0);
6965 struct mdstat_ent *memb;
6966 int count = 0;
6967 int num = 0;
6968 struct md_list *dv;
6969 int found;
6970
6971 for (memb = mdstat ; memb ; memb = memb->next) {
6972 if (memb->metadata_version &&
6973 (strncmp(memb->metadata_version, "external:", 9) == 0) &&
6974 (strcmp(&memb->metadata_version[9], name) == 0) &&
6975 !is_subarray(memb->metadata_version+9) &&
6976 memb->members) {
6977 struct dev_member *dev = memb->members;
6978 int fd = -1;
6979 while (dev && !is_fd_valid(fd)) {
6980 char *path = xmalloc(strlen(dev->name) + strlen("/dev/") + 1);
6981 num = snprintf(path, PATH_MAX, "%s%s", "/dev/", dev->name);
6982 if (num > 0)
6983 fd = open(path, O_RDONLY, 0);
6984 if (num <= 0 || !is_fd_valid(fd)) {
6985 pr_vrb("Cannot open %s: %s\n",
6986 dev->name, strerror(errno));
6987 }
6988 free(path);
6989 dev = dev->next;
6990 }
6991 found = 0;
6992 if (is_fd_valid(fd) && disk_attached_to_hba(fd, hba)) {
6993 struct mdstat_ent *vol;
6994 for (vol = mdstat ; vol ; vol = vol->next) {
6995 if (vol->active > 0 &&
6996 vol->metadata_version &&
6997 is_container_member(vol, memb->devnm)) {
6998 found++;
6999 count++;
7000 }
7001 }
7002 if (*devlist && (found < dpa)) {
7003 dv = xcalloc(1, sizeof(*dv));
7004 dv->devname = xmalloc(strlen(memb->devnm) + strlen("/dev/") + 1);
7005 sprintf(dv->devname, "%s%s", "/dev/", memb->devnm);
7006 dv->found = found;
7007 dv->used = 0;
7008 dv->next = *devlist;
7009 *devlist = dv;
7010 }
7011 }
7012 close_fd(&fd);
7013 }
7014 }
7015 free_mdstat(mdstat);
7016 return count;
7017 }
7018
7019 #ifdef DEBUG_LOOP
7020 static struct md_list*
7021 get_loop_devices(void)
7022 {
7023 int i;
7024 struct md_list *devlist = NULL;
7025 struct md_list *dv;
7026
7027 for(i = 0; i < 12; i++) {
7028 dv = xcalloc(1, sizeof(*dv));
7029 dv->devname = xmalloc(40);
7030 sprintf(dv->devname, "/dev/loop%d", i);
7031 dv->next = devlist;
7032 devlist = dv;
7033 }
7034 return devlist;
7035 }
7036 #endif
7037
7038 static struct md_list*
7039 get_devices(const char *hba_path)
7040 {
7041 struct md_list *devlist = NULL;
7042 struct md_list *dv;
7043 struct dirent *ent;
7044 DIR *dir;
7045 int err = 0;
7046
7047 #if DEBUG_LOOP
7048 devlist = get_loop_devices();
7049 return devlist;
7050 #endif
7051 /* scroll through /sys/dev/block looking for devices attached to
7052 * this hba
7053 */
7054 dir = opendir("/sys/dev/block");
7055 for (ent = dir ? readdir(dir) : NULL; ent; ent = readdir(dir)) {
7056 int fd;
7057 char buf[1024];
7058 int major, minor;
7059 char *path = NULL;
7060 if (sscanf(ent->d_name, "%d:%d", &major, &minor) != 2)
7061 continue;
7062 path = devt_to_devpath(makedev(major, minor), 1, NULL);
7063 if (!path)
7064 continue;
7065 if (!path_attached_to_hba(path, hba_path)) {
7066 free(path);
7067 path = NULL;
7068 continue;
7069 }
7070 free(path);
7071 path = NULL;
7072 fd = dev_open(ent->d_name, O_RDONLY);
7073 if (is_fd_valid(fd)) {
7074 fd2devname(fd, buf);
7075 close(fd);
7076 } else {
7077 pr_err("cannot open device: %s\n",
7078 ent->d_name);
7079 continue;
7080 }
7081
7082 dv = xcalloc(1, sizeof(*dv));
7083 dv->devname = xstrdup(buf);
7084 dv->next = devlist;
7085 devlist = dv;
7086 }
7087 if (err) {
7088 while(devlist) {
7089 dv = devlist;
7090 devlist = devlist->next;
7091 free(dv->devname);
7092 free(dv);
7093 }
7094 }
7095 closedir(dir);
7096 return devlist;
7097 }
7098
7099 static int
7100 count_volumes_list(struct md_list *devlist, char *homehost,
7101 int verbose, int *found)
7102 {
7103 struct md_list *tmpdev;
7104 int count = 0;
7105 struct supertype *st;
7106
7107 /* first walk the list of devices to find a consistent set
7108 * that match the criterea, if that is possible.
7109 * We flag the ones we like with 'used'.
7110 */
7111 *found = 0;
7112 st = match_metadata_desc_imsm("imsm");
7113 if (st == NULL) {
7114 pr_vrb("cannot allocate memory for imsm supertype\n");
7115 return 0;
7116 }
7117
7118 for (tmpdev = devlist; tmpdev; tmpdev = tmpdev->next) {
7119 char *devname = tmpdev->devname;
7120 dev_t rdev;
7121 struct supertype *tst;
7122 int dfd;
7123 if (tmpdev->used > 1)
7124 continue;
7125 tst = dup_super(st);
7126 if (tst == NULL) {
7127 pr_vrb("cannot allocate memory for imsm supertype\n");
7128 goto err_1;
7129 }
7130 tmpdev->container = 0;
7131 dfd = dev_open(devname, O_RDONLY|O_EXCL);
7132 if (!is_fd_valid(dfd)) {
7133 dprintf("cannot open device %s: %s\n",
7134 devname, strerror(errno));
7135 tmpdev->used = 2;
7136 } else if (!fstat_is_blkdev(dfd, devname, &rdev)) {
7137 tmpdev->used = 2;
7138 } else if (must_be_container(dfd)) {
7139 struct supertype *cst;
7140 cst = super_by_fd(dfd, NULL);
7141 if (cst == NULL) {
7142 dprintf("cannot recognize container type %s\n",
7143 devname);
7144 tmpdev->used = 2;
7145 } else if (tst->ss != st->ss) {
7146 dprintf("non-imsm container - ignore it: %s\n",
7147 devname);
7148 tmpdev->used = 2;
7149 } else if (!tst->ss->load_container ||
7150 tst->ss->load_container(tst, dfd, NULL))
7151 tmpdev->used = 2;
7152 else {
7153 tmpdev->container = 1;
7154 }
7155 if (cst)
7156 cst->ss->free_super(cst);
7157 } else {
7158 tmpdev->st_rdev = rdev;
7159 if (tst->ss->load_super(tst,dfd, NULL)) {
7160 dprintf("no RAID superblock on %s\n",
7161 devname);
7162 tmpdev->used = 2;
7163 } else if (tst->ss->compare_super == NULL) {
7164 dprintf("Cannot assemble %s metadata on %s\n",
7165 tst->ss->name, devname);
7166 tmpdev->used = 2;
7167 }
7168 }
7169 close_fd(&dfd);
7170
7171 if (tmpdev->used == 2 || tmpdev->used == 4) {
7172 /* Ignore unrecognised devices during auto-assembly */
7173 goto loop;
7174 }
7175 else {
7176 struct mdinfo info;
7177 tst->ss->getinfo_super(tst, &info, NULL);
7178
7179 if (st->minor_version == -1)
7180 st->minor_version = tst->minor_version;
7181
7182 if (memcmp(info.uuid, uuid_zero,
7183 sizeof(int[4])) == 0) {
7184 /* this is a floating spare. It cannot define
7185 * an array unless there are no more arrays of
7186 * this type to be found. It can be included
7187 * in an array of this type though.
7188 */
7189 tmpdev->used = 3;
7190 goto loop;
7191 }
7192
7193 if (st->ss != tst->ss ||
7194 st->minor_version != tst->minor_version ||
7195 st->ss->compare_super(st, tst, 1) != 0) {
7196 /* Some mismatch. If exactly one array matches this host,
7197 * we can resolve on that one.
7198 * Or, if we are auto assembling, we just ignore the second
7199 * for now.
7200 */
7201 dprintf("superblock on %s doesn't match others - assembly aborted\n",
7202 devname);
7203 goto loop;
7204 }
7205 tmpdev->used = 1;
7206 *found = 1;
7207 dprintf("found: devname: %s\n", devname);
7208 }
7209 loop:
7210 if (tst)
7211 tst->ss->free_super(tst);
7212 }
7213 if (*found != 0) {
7214 int err;
7215 if ((err = load_super_imsm_all(st, -1, &st->sb, NULL, devlist, 0)) == 0) {
7216 struct mdinfo *iter, *head = st->ss->container_content(st, NULL);
7217 for (iter = head; iter; iter = iter->next) {
7218 dprintf("content->text_version: %s vol\n",
7219 iter->text_version);
7220 if (iter->array.state & (1<<MD_SB_BLOCK_VOLUME)) {
7221 /* do not assemble arrays with unsupported
7222 configurations */
7223 dprintf("Cannot activate member %s.\n",
7224 iter->text_version);
7225 } else
7226 count++;
7227 }
7228 sysfs_free(head);
7229
7230 } else {
7231 dprintf("No valid super block on device list: err: %d %p\n",
7232 err, st->sb);
7233 }
7234 } else {
7235 dprintf("no more devices to examine\n");
7236 }
7237
7238 for (tmpdev = devlist; tmpdev; tmpdev = tmpdev->next) {
7239 if (tmpdev->used == 1 && tmpdev->found) {
7240 if (count) {
7241 if (count < tmpdev->found)
7242 count = 0;
7243 else
7244 count -= tmpdev->found;
7245 }
7246 }
7247 if (tmpdev->used == 1)
7248 tmpdev->used = 4;
7249 }
7250 err_1:
7251 if (st)
7252 st->ss->free_super(st);
7253 return count;
7254 }
7255
7256 static int __count_volumes(char *hba_path, int dpa, int verbose,
7257 int cmp_hba_path)
7258 {
7259 struct sys_dev *idev, *intel_devices = find_intel_devices();
7260 int count = 0;
7261 const struct orom_entry *entry;
7262 struct devid_list *dv, *devid_list;
7263
7264 if (!hba_path)
7265 return 0;
7266
7267 for (idev = intel_devices; idev; idev = idev->next) {
7268 if (strstr(idev->path, hba_path))
7269 break;
7270 }
7271
7272 if (!idev || !idev->dev_id)
7273 return 0;
7274
7275 entry = get_orom_entry_by_device_id(idev->dev_id);
7276
7277 if (!entry || !entry->devid_list)
7278 return 0;
7279
7280 devid_list = entry->devid_list;
7281 for (dv = devid_list; dv; dv = dv->next) {
7282 struct md_list *devlist;
7283 struct sys_dev *device = NULL;
7284 char *hpath;
7285 int found = 0;
7286
7287 if (cmp_hba_path)
7288 device = device_by_id_and_path(dv->devid, hba_path);
7289 else
7290 device = device_by_id(dv->devid);
7291
7292 if (device)
7293 hpath = device->path;
7294 else
7295 return 0;
7296
7297 devlist = get_devices(hpath);
7298 /* if no intel devices return zero volumes */
7299 if (devlist == NULL)
7300 return 0;
7301
7302 count += active_arrays_by_format("imsm", hpath, &devlist, dpa,
7303 verbose);
7304 dprintf("path: %s active arrays: %d\n", hpath, count);
7305 if (devlist == NULL)
7306 return 0;
7307 do {
7308 found = 0;
7309 count += count_volumes_list(devlist,
7310 NULL,
7311 verbose,
7312 &found);
7313 dprintf("found %d count: %d\n", found, count);
7314 } while (found);
7315
7316 dprintf("path: %s total number of volumes: %d\n", hpath, count);
7317
7318 while (devlist) {
7319 struct md_list *dv = devlist;
7320 devlist = devlist->next;
7321 free(dv->devname);
7322 free(dv);
7323 }
7324 }
7325 return count;
7326 }
7327
7328 static int count_volumes(struct intel_hba *hba, int dpa, int verbose)
7329 {
7330 if (!hba)
7331 return 0;
7332 if (hba->type == SYS_DEV_VMD) {
7333 struct sys_dev *dev;
7334 int count = 0;
7335
7336 for (dev = find_intel_devices(); dev; dev = dev->next) {
7337 if (dev->type == SYS_DEV_VMD)
7338 count += __count_volumes(dev->path, dpa,
7339 verbose, 1);
7340 }
7341 return count;
7342 }
7343 return __count_volumes(hba->path, dpa, verbose, 0);
7344 }
7345
7346 static int imsm_default_chunk(const struct imsm_orom *orom)
7347 {
7348 /* up to 512 if the plaform supports it, otherwise the platform max.
7349 * 128 if no platform detected
7350 */
7351 int fs = max(7, orom ? fls(orom->sss) : 0);
7352
7353 return min(512, (1 << fs));
7354 }
7355
7356 static int
7357 validate_geometry_imsm_orom(struct intel_super *super, int level, int layout,
7358 int raiddisks, int *chunk, unsigned long long size, int verbose)
7359 {
7360 /* check/set platform and metadata limits/defaults */
7361 if (super->orom && raiddisks > super->orom->dpa) {
7362 pr_vrb("platform supports a maximum of %d disks per array\n",
7363 super->orom->dpa);
7364 return 0;
7365 }
7366
7367 /* capabilities of OROM tested - copied from validate_geometry_imsm_volume */
7368 if (!is_raid_level_supported(super->orom, level, raiddisks)) {
7369 pr_vrb("platform does not support raid%d with %d disk%s\n",
7370 level, raiddisks, raiddisks > 1 ? "s" : "");
7371 return 0;
7372 }
7373
7374 if (*chunk == 0 || *chunk == UnSet)
7375 *chunk = imsm_default_chunk(super->orom);
7376
7377 if (super->orom && !imsm_orom_has_chunk(super->orom, *chunk)) {
7378 pr_vrb("platform does not support a chunk size of: %d\n", *chunk);
7379 return 0;
7380 }
7381
7382 if (layout != imsm_level_to_layout(level)) {
7383 if (level == 5)
7384 pr_vrb("imsm raid 5 only supports the left-asymmetric layout\n");
7385 else if (level == 10)
7386 pr_vrb("imsm raid 10 only supports the n2 layout\n");
7387 else
7388 pr_vrb("imsm unknown layout %#x for this raid level %d\n",
7389 layout, level);
7390 return 0;
7391 }
7392
7393 if (super->orom && (super->orom->attr & IMSM_OROM_ATTR_2TB) == 0 &&
7394 (calc_array_size(level, raiddisks, layout, *chunk, size) >> 32) > 0) {
7395 pr_vrb("platform does not support a volume size over 2TB\n");
7396 return 0;
7397 }
7398
7399 return 1;
7400 }
7401
7402 /* validate_geometry_imsm_volume - lifted from validate_geometry_ddf_bvd
7403 * FIX ME add ahci details
7404 */
7405 static int validate_geometry_imsm_volume(struct supertype *st, int level,
7406 int layout, int raiddisks, int *chunk,
7407 unsigned long long size,
7408 unsigned long long data_offset,
7409 char *dev,
7410 unsigned long long *freesize,
7411 int verbose)
7412 {
7413 dev_t rdev;
7414 struct intel_super *super = st->sb;
7415 struct imsm_super *mpb;
7416 struct dl *dl;
7417 unsigned long long pos = 0;
7418 unsigned long long maxsize;
7419 struct extent *e;
7420 int i;
7421
7422 /* We must have the container info already read in. */
7423 if (!super)
7424 return 0;
7425
7426 mpb = super->anchor;
7427
7428 if (!validate_geometry_imsm_orom(super, level, layout, raiddisks, chunk, size, verbose)) {
7429 pr_err("RAID geometry validation failed. Cannot proceed with the action(s).\n");
7430 return 0;
7431 }
7432 if (!dev) {
7433 /* General test: make sure there is space for
7434 * 'raiddisks' device extents of size 'size' at a given
7435 * offset
7436 */
7437 unsigned long long minsize = size;
7438 unsigned long long start_offset = MaxSector;
7439 int dcnt = 0;
7440 if (minsize == 0)
7441 minsize = MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS;
7442 for (dl = super->disks; dl ; dl = dl->next) {
7443 int found = 0;
7444
7445 pos = 0;
7446 i = 0;
7447 e = get_extents(super, dl, 0);
7448 if (!e) continue;
7449 do {
7450 unsigned long long esize;
7451 esize = e[i].start - pos;
7452 if (esize >= minsize)
7453 found = 1;
7454 if (found && start_offset == MaxSector) {
7455 start_offset = pos;
7456 break;
7457 } else if (found && pos != start_offset) {
7458 found = 0;
7459 break;
7460 }
7461 pos = e[i].start + e[i].size;
7462 i++;
7463 } while (e[i-1].size);
7464 if (found)
7465 dcnt++;
7466 free(e);
7467 }
7468 if (dcnt < raiddisks) {
7469 if (verbose)
7470 pr_err("imsm: Not enough devices with space for this array (%d < %d)\n",
7471 dcnt, raiddisks);
7472 return 0;
7473 }
7474 return 1;
7475 }
7476
7477 /* This device must be a member of the set */
7478 if (!stat_is_blkdev(dev, &rdev))
7479 return 0;
7480 for (dl = super->disks ; dl ; dl = dl->next) {
7481 if (dl->major == (int)major(rdev) &&
7482 dl->minor == (int)minor(rdev))
7483 break;
7484 }
7485 if (!dl) {
7486 if (verbose)
7487 pr_err("%s is not in the same imsm set\n", dev);
7488 return 0;
7489 } else if (super->orom && dl->index < 0 && mpb->num_raid_devs) {
7490 /* If a volume is present then the current creation attempt
7491 * cannot incorporate new spares because the orom may not
7492 * understand this configuration (all member disks must be
7493 * members of each array in the container).
7494 */
7495 pr_err("%s is a spare and a volume is already defined for this container\n", dev);
7496 pr_err("The option-rom requires all member disks to be a member of all volumes\n");
7497 return 0;
7498 } else if (super->orom && mpb->num_raid_devs > 0 &&
7499 mpb->num_disks != raiddisks) {
7500 pr_err("The option-rom requires all member disks to be a member of all volumes\n");
7501 return 0;
7502 }
7503
7504 /* retrieve the largest free space block */
7505 e = get_extents(super, dl, 0);
7506 maxsize = 0;
7507 i = 0;
7508 if (e) {
7509 do {
7510 unsigned long long esize;
7511
7512 esize = e[i].start - pos;
7513 if (esize >= maxsize)
7514 maxsize = esize;
7515 pos = e[i].start + e[i].size;
7516 i++;
7517 } while (e[i-1].size);
7518 dl->e = e;
7519 dl->extent_cnt = i;
7520 } else {
7521 if (verbose)
7522 pr_err("unable to determine free space for: %s\n",
7523 dev);
7524 return 0;
7525 }
7526 if (maxsize < size) {
7527 if (verbose)
7528 pr_err("%s not enough space (%llu < %llu)\n",
7529 dev, maxsize, size);
7530 return 0;
7531 }
7532
7533 maxsize = merge_extents(super, false);
7534
7535 if (mpb->num_raid_devs > 0 && size && size != maxsize)
7536 pr_err("attempting to create a second volume with size less then remaining space.\n");
7537
7538 if (maxsize < size || maxsize == 0) {
7539 if (verbose) {
7540 if (maxsize == 0)
7541 pr_err("no free space left on device. Aborting...\n");
7542 else
7543 pr_err("not enough space to create volume of given size (%llu < %llu). Aborting...\n",
7544 maxsize, size);
7545 }
7546 return 0;
7547 }
7548
7549 *freesize = maxsize;
7550
7551 if (super->orom) {
7552 int count = count_volumes(super->hba,
7553 super->orom->dpa, verbose);
7554 if (super->orom->vphba <= count) {
7555 pr_vrb("platform does not support more than %d raid volumes.\n",
7556 super->orom->vphba);
7557 return 0;
7558 }
7559 }
7560 return 1;
7561 }
7562
7563 /**
7564 * imsm_get_free_size() - get the biggest, common free space from members.
7565 * @super: &intel_super pointer, not NULL.
7566 * @raiddisks: number of raid disks.
7567 * @size: requested size, could be 0 (means max size).
7568 * @chunk: requested chunk size in KiB.
7569 * @freesize: pointer for returned size value.
7570 *
7571 * Return: &IMSM_STATUS_OK or &IMSM_STATUS_ERROR.
7572 *
7573 * @freesize is set to meaningful value, this can be @size, or calculated
7574 * max free size.
7575 * super->create_offset value is modified and set appropriately in
7576 * merge_extends() for further creation.
7577 */
7578 static imsm_status_t imsm_get_free_size(struct intel_super *super,
7579 const int raiddisks,
7580 unsigned long long size,
7581 const int chunk,
7582 unsigned long long *freesize,
7583 bool expanding)
7584 {
7585 struct imsm_super *mpb = super->anchor;
7586 struct dl *dl;
7587 int i;
7588 struct extent *e;
7589 int cnt = 0;
7590 int used = 0;
7591 unsigned long long maxsize;
7592 unsigned long long minsize = size;
7593
7594 if (minsize == 0)
7595 minsize = chunk * 2;
7596
7597 /* find the largest common start free region of the possible disks */
7598 for (dl = super->disks; dl; dl = dl->next) {
7599 dl->raiddisk = -1;
7600
7601 if (dl->index >= 0)
7602 used++;
7603
7604 /* don't activate new spares if we are orom constrained
7605 * and there is already a volume active in the container
7606 */
7607 if (super->orom && dl->index < 0 && mpb->num_raid_devs)
7608 continue;
7609
7610 e = get_extents(super, dl, 0);
7611 if (!e)
7612 continue;
7613 for (i = 1; e[i-1].size; i++)
7614 ;
7615 dl->e = e;
7616 dl->extent_cnt = i;
7617 cnt++;
7618 }
7619
7620 maxsize = merge_extents(super, expanding);
7621 if (maxsize < minsize) {
7622 pr_err("imsm: Free space is %llu but must be equal or larger than %llu.\n",
7623 maxsize, minsize);
7624 return IMSM_STATUS_ERROR;
7625 }
7626
7627 if (cnt < raiddisks || (super->orom && used && used != raiddisks)) {
7628 pr_err("imsm: Not enough devices with space to create array.\n");
7629 return IMSM_STATUS_ERROR;
7630 }
7631
7632 if (size == 0) {
7633 size = maxsize;
7634 if (chunk) {
7635 size /= 2 * chunk;
7636 size *= 2 * chunk;
7637 }
7638 maxsize = size;
7639 }
7640 if (mpb->num_raid_devs > 0 && size && size != maxsize)
7641 pr_err("attempting to create a second volume with size less then remaining space.\n");
7642 *freesize = size;
7643
7644 dprintf("imsm: imsm_get_free_size() returns : %llu\n", size);
7645
7646 return IMSM_STATUS_OK;
7647 }
7648
7649 /**
7650 * autolayout_imsm() - automatically layout a new volume.
7651 * @super: &intel_super pointer, not NULL.
7652 * @raiddisks: number of raid disks.
7653 * @size: requested size, could be 0 (means max size).
7654 * @chunk: requested chunk.
7655 * @freesize: pointer for returned size value.
7656 *
7657 * We are being asked to automatically layout a new volume based on the current
7658 * contents of the container. If the parameters can be satisfied autolayout_imsm
7659 * will record the disks, start offset, and will return size of the volume to
7660 * be created. See imsm_get_free_size() for details.
7661 * add_to_super() and getinfo_super() detect when autolayout is in progress.
7662 * If first volume exists, slots are set consistently to it.
7663 *
7664 * Return: &IMSM_STATUS_OK on success, &IMSM_STATUS_ERROR otherwise.
7665 *
7666 * Disks are marked for creation via dl->raiddisk.
7667 */
7668 static imsm_status_t autolayout_imsm(struct intel_super *super,
7669 const int raiddisks,
7670 unsigned long long size, const int chunk,
7671 unsigned long long *freesize)
7672 {
7673 int curr_slot = 0;
7674 struct dl *disk;
7675 int vol_cnt = super->anchor->num_raid_devs;
7676 imsm_status_t rv;
7677
7678 rv = imsm_get_free_size(super, raiddisks, size, chunk, freesize, false);
7679 if (rv != IMSM_STATUS_OK)
7680 return IMSM_STATUS_ERROR;
7681
7682 for (disk = super->disks; disk; disk = disk->next) {
7683 if (!disk->e)
7684 continue;
7685
7686 if (curr_slot == raiddisks)
7687 break;
7688
7689 if (vol_cnt == 0) {
7690 disk->raiddisk = curr_slot;
7691 } else {
7692 int _slot = get_disk_slot_in_dev(super, 0, disk->index);
7693
7694 if (_slot == -1) {
7695 pr_err("Disk %s is not used in first volume, aborting\n",
7696 disk->devname);
7697 return IMSM_STATUS_ERROR;
7698 }
7699 disk->raiddisk = _slot;
7700 }
7701 curr_slot++;
7702 }
7703
7704 return IMSM_STATUS_OK;
7705 }
7706
7707 static int validate_geometry_imsm(struct supertype *st, int level, int layout,
7708 int raiddisks, int *chunk, unsigned long long size,
7709 unsigned long long data_offset,
7710 char *dev, unsigned long long *freesize,
7711 int consistency_policy, int verbose)
7712 {
7713 int fd, cfd;
7714 struct mdinfo *sra;
7715 int is_member = 0;
7716
7717 /* load capability
7718 * if given unused devices create a container
7719 * if given given devices in a container create a member volume
7720 */
7721 if (is_container(level))
7722 /* Must be a fresh device to add to a container */
7723 return validate_geometry_imsm_container(st, level, raiddisks,
7724 data_offset, dev,
7725 freesize, verbose);
7726
7727 /*
7728 * Size is given in sectors.
7729 */
7730 if (size && (size < 2048)) {
7731 pr_err("Given size must be greater than 1M.\n");
7732 /* Depends on algorithm in Create.c :
7733 * if container was given (dev == NULL) return -1,
7734 * if block device was given ( dev != NULL) return 0.
7735 */
7736 return dev ? -1 : 0;
7737 }
7738
7739 if (!dev) {
7740 struct intel_super *super = st->sb;
7741
7742 /*
7743 * Autolayout mode, st->sb must be set.
7744 */
7745 if (!super) {
7746 pr_vrb("superblock must be set for autolayout, aborting\n");
7747 return 0;
7748 }
7749
7750 if (!validate_geometry_imsm_orom(st->sb, level, layout,
7751 raiddisks, chunk, size,
7752 verbose))
7753 return 0;
7754
7755 if (super->orom && freesize) {
7756 imsm_status_t rv;
7757 int count = count_volumes(super->hba, super->orom->dpa,
7758 verbose);
7759 if (super->orom->vphba <= count) {
7760 pr_vrb("platform does not support more than %d raid volumes.\n",
7761 super->orom->vphba);
7762 return 0;
7763 }
7764
7765 rv = autolayout_imsm(super, raiddisks, size, *chunk,
7766 freesize);
7767 if (rv != IMSM_STATUS_OK)
7768 return 0;
7769 }
7770 return 1;
7771 }
7772 if (st->sb) {
7773 /* creating in a given container */
7774 return validate_geometry_imsm_volume(st, level, layout,
7775 raiddisks, chunk, size,
7776 data_offset,
7777 dev, freesize, verbose);
7778 }
7779
7780 /* This device needs to be a device in an 'imsm' container */
7781 fd = open(dev, O_RDONLY|O_EXCL, 0);
7782
7783 if (is_fd_valid(fd)) {
7784 pr_vrb("Cannot create this array on device %s\n", dev);
7785 close(fd);
7786 return 0;
7787 }
7788 if (errno == EBUSY)
7789 fd = open(dev, O_RDONLY, 0);
7790
7791 if (!is_fd_valid(fd)) {
7792 pr_vrb("Cannot open %s: %s\n", dev, strerror(errno));
7793 return 0;
7794 }
7795
7796 /* Well, it is in use by someone, maybe an 'imsm' container. */
7797 cfd = open_container(fd);
7798 close_fd(&fd);
7799
7800 if (!is_fd_valid(cfd)) {
7801 pr_vrb("Cannot use %s: It is busy\n", dev);
7802 return 0;
7803 }
7804 sra = sysfs_read(cfd, NULL, GET_VERSION);
7805 if (sra && sra->array.major_version == -1 &&
7806 strcmp(sra->text_version, "imsm") == 0)
7807 is_member = 1;
7808 sysfs_free(sra);
7809 if (is_member) {
7810 /* This is a member of a imsm container. Load the container
7811 * and try to create a volume
7812 */
7813 struct intel_super *super;
7814
7815 if (load_super_imsm_all(st, cfd, (void **) &super, NULL, NULL, 1) == 0) {
7816 st->sb = super;
7817 strcpy(st->container_devnm, fd2devnm(cfd));
7818 close(cfd);
7819 return validate_geometry_imsm_volume(st, level, layout,
7820 raiddisks, chunk,
7821 size, data_offset, dev,
7822 freesize, 1)
7823 ? 1 : -1;
7824 }
7825 }
7826
7827 if (verbose)
7828 pr_err("failed container membership check\n");
7829
7830 close(cfd);
7831 return 0;
7832 }
7833
7834 static void default_geometry_imsm(struct supertype *st, int *level, int *layout, int *chunk)
7835 {
7836 struct intel_super *super = st->sb;
7837
7838 if (level && *level == UnSet)
7839 *level = LEVEL_CONTAINER;
7840
7841 if (level && layout && *layout == UnSet)
7842 *layout = imsm_level_to_layout(*level);
7843
7844 if (chunk && (*chunk == UnSet || *chunk == 0))
7845 *chunk = imsm_default_chunk(super->orom);
7846 }
7847
7848 static void handle_missing(struct intel_super *super, struct imsm_dev *dev);
7849
7850 static int kill_subarray_imsm(struct supertype *st, char *subarray_id)
7851 {
7852 /* remove the subarray currently referenced by subarray_id */
7853 __u8 i;
7854 struct intel_dev **dp;
7855 struct intel_super *super = st->sb;
7856 __u8 current_vol = strtoul(subarray_id, NULL, 10);
7857 struct imsm_super *mpb = super->anchor;
7858
7859 if (mpb->num_raid_devs == 0)
7860 return 2;
7861
7862 /* block deletions that would change the uuid of active subarrays
7863 *
7864 * FIXME when immutable ids are available, but note that we'll
7865 * also need to fixup the invalidated/active subarray indexes in
7866 * mdstat
7867 */
7868 for (i = 0; i < mpb->num_raid_devs; i++) {
7869 char subarray[4];
7870
7871 if (i < current_vol)
7872 continue;
7873 snprintf(subarray, sizeof(subarray), "%u", i);
7874 if (is_subarray_active(subarray, st->devnm)) {
7875 pr_err("deleting subarray-%d would change the UUID of active subarray-%d, aborting\n",
7876 current_vol, i);
7877
7878 return 2;
7879 }
7880 }
7881
7882 if (st->update_tail) {
7883 struct imsm_update_kill_array *u = xmalloc(sizeof(*u));
7884
7885 u->type = update_kill_array;
7886 u->dev_idx = current_vol;
7887 append_metadata_update(st, u, sizeof(*u));
7888
7889 return 0;
7890 }
7891
7892 for (dp = &super->devlist; *dp;)
7893 if ((*dp)->index == current_vol) {
7894 *dp = (*dp)->next;
7895 } else {
7896 handle_missing(super, (*dp)->dev);
7897 if ((*dp)->index > current_vol)
7898 (*dp)->index--;
7899 dp = &(*dp)->next;
7900 }
7901
7902 /* no more raid devices, all active components are now spares,
7903 * but of course failed are still failed
7904 */
7905 if (--mpb->num_raid_devs == 0) {
7906 struct dl *d;
7907
7908 for (d = super->disks; d; d = d->next)
7909 if (d->index > -2)
7910 mark_spare(d);
7911 }
7912
7913 super->updates_pending++;
7914
7915 return 0;
7916 }
7917
7918 /**
7919 * get_rwh_policy_from_update() - Get the rwh policy for update option.
7920 * @update: Update option.
7921 */
7922 static int get_rwh_policy_from_update(enum update_opt update)
7923 {
7924 switch (update) {
7925 case UOPT_PPL:
7926 return RWH_MULTIPLE_DISTRIBUTED;
7927 case UOPT_NO_PPL:
7928 return RWH_MULTIPLE_OFF;
7929 case UOPT_BITMAP:
7930 return RWH_BITMAP;
7931 case UOPT_NO_BITMAP:
7932 return RWH_OFF;
7933 default:
7934 break;
7935 }
7936 return UOPT_UNDEFINED;
7937 }
7938
7939 static int update_subarray_imsm(struct supertype *st, char *subarray,
7940 enum update_opt update, struct mddev_ident *ident)
7941 {
7942 /* update the subarray currently referenced by ->current_vol */
7943 struct intel_super *super = st->sb;
7944 struct imsm_super *mpb = super->anchor;
7945
7946 if (update == UOPT_NAME) {
7947 char *name = ident->name;
7948 char *ep;
7949 int vol;
7950
7951 if (imsm_is_name_allowed(super, name, 1) == false)
7952 return 2;
7953
7954 vol = strtoul(subarray, &ep, 10);
7955 if (*ep != '\0' || vol >= super->anchor->num_raid_devs)
7956 return 2;
7957
7958 if (st->update_tail) {
7959 struct imsm_update_rename_array *u = xmalloc(sizeof(*u));
7960
7961 u->type = update_rename_array;
7962 u->dev_idx = vol;
7963 strncpy((char *) u->name, name, MAX_RAID_SERIAL_LEN);
7964 u->name[MAX_RAID_SERIAL_LEN-1] = '\0';
7965 append_metadata_update(st, u, sizeof(*u));
7966 } else {
7967 struct imsm_dev *dev;
7968 int i, namelen;
7969
7970 dev = get_imsm_dev(super, vol);
7971 memset(dev->volume, '\0', MAX_RAID_SERIAL_LEN);
7972 namelen = min((int)strlen(name), MAX_RAID_SERIAL_LEN);
7973 memcpy(dev->volume, name, namelen);
7974 for (i = 0; i < mpb->num_raid_devs; i++) {
7975 dev = get_imsm_dev(super, i);
7976 handle_missing(super, dev);
7977 }
7978 super->updates_pending++;
7979 }
7980 } else if (get_rwh_policy_from_update(update) != UOPT_UNDEFINED) {
7981 int new_policy;
7982 char *ep;
7983 int vol = strtoul(subarray, &ep, 10);
7984
7985 if (*ep != '\0' || vol >= super->anchor->num_raid_devs)
7986 return 2;
7987
7988 new_policy = get_rwh_policy_from_update(update);
7989
7990 if (st->update_tail) {
7991 struct imsm_update_rwh_policy *u = xmalloc(sizeof(*u));
7992
7993 u->type = update_rwh_policy;
7994 u->dev_idx = vol;
7995 u->new_policy = new_policy;
7996 append_metadata_update(st, u, sizeof(*u));
7997 } else {
7998 struct imsm_dev *dev;
7999
8000 dev = get_imsm_dev(super, vol);
8001 dev->rwh_policy = new_policy;
8002 super->updates_pending++;
8003 }
8004 if (new_policy == RWH_BITMAP)
8005 return write_init_bitmap_imsm_vol(st, vol);
8006 } else
8007 return 2;
8008
8009 return 0;
8010 }
8011
8012 static bool is_gen_migration(struct imsm_dev *dev)
8013 {
8014 if (dev && dev->vol.migr_state &&
8015 migr_type(dev) == MIGR_GEN_MIGR)
8016 return true;
8017
8018 return false;
8019 }
8020
8021 static int is_rebuilding(struct imsm_dev *dev)
8022 {
8023 struct imsm_map *migr_map;
8024
8025 if (!dev->vol.migr_state)
8026 return 0;
8027
8028 if (migr_type(dev) != MIGR_REBUILD)
8029 return 0;
8030
8031 migr_map = get_imsm_map(dev, MAP_1);
8032
8033 if (migr_map->map_state == IMSM_T_STATE_DEGRADED)
8034 return 1;
8035 else
8036 return 0;
8037 }
8038
8039 static int is_initializing(struct imsm_dev *dev)
8040 {
8041 struct imsm_map *migr_map;
8042
8043 if (!dev->vol.migr_state)
8044 return 0;
8045
8046 if (migr_type(dev) != MIGR_INIT)
8047 return 0;
8048
8049 migr_map = get_imsm_map(dev, MAP_1);
8050
8051 if (migr_map->map_state == IMSM_T_STATE_UNINITIALIZED)
8052 return 1;
8053
8054 return 0;
8055 }
8056
8057 static void update_recovery_start(struct intel_super *super,
8058 struct imsm_dev *dev,
8059 struct mdinfo *array)
8060 {
8061 struct mdinfo *rebuild = NULL;
8062 struct mdinfo *d;
8063 __u32 units;
8064
8065 if (!is_rebuilding(dev))
8066 return;
8067
8068 /* Find the rebuild target, but punt on the dual rebuild case */
8069 for (d = array->devs; d; d = d->next)
8070 if (d->recovery_start == 0) {
8071 if (rebuild)
8072 return;
8073 rebuild = d;
8074 }
8075
8076 if (!rebuild) {
8077 /* (?) none of the disks are marked with
8078 * IMSM_ORD_REBUILD, so assume they are missing and the
8079 * disk_ord_tbl was not correctly updated
8080 */
8081 dprintf("failed to locate out-of-sync disk\n");
8082 return;
8083 }
8084
8085 units = vol_curr_migr_unit(dev);
8086 rebuild->recovery_start = units * blocks_per_migr_unit(super, dev);
8087 }
8088
8089 static int recover_backup_imsm(struct supertype *st, struct mdinfo *info);
8090
8091 static struct mdinfo *container_content_imsm(struct supertype *st, char *subarray)
8092 {
8093 /* Given a container loaded by load_super_imsm_all,
8094 * extract information about all the arrays into
8095 * an mdinfo tree.
8096 * If 'subarray' is given, just extract info about that array.
8097 *
8098 * For each imsm_dev create an mdinfo, fill it in,
8099 * then look for matching devices in super->disks
8100 * and create appropriate device mdinfo.
8101 */
8102 struct intel_super *super = st->sb;
8103 struct imsm_super *mpb = super->anchor;
8104 struct mdinfo *rest = NULL;
8105 unsigned int i;
8106 int sb_errors = 0;
8107 struct dl *d;
8108 int spare_disks = 0;
8109 int current_vol = super->current_vol;
8110
8111 /* do not assemble arrays when not all attributes are supported */
8112 if (imsm_check_attributes(mpb->attributes) == false) {
8113 sb_errors = 1;
8114 pr_err("Unsupported attributes in IMSM metadata. Arrays activation is blocked.\n");
8115 }
8116
8117 /* count spare devices, not used in maps
8118 */
8119 for (d = super->disks; d; d = d->next)
8120 if (d->index == -1)
8121 spare_disks++;
8122
8123 for (i = 0; i < mpb->num_raid_devs; i++) {
8124 struct imsm_dev *dev;
8125 struct imsm_map *map;
8126 struct imsm_map *map2;
8127 struct mdinfo *this;
8128 int slot;
8129 int chunk;
8130 char *ep;
8131 int level;
8132
8133 if (subarray &&
8134 (i != strtoul(subarray, &ep, 10) || *ep != '\0'))
8135 continue;
8136
8137 dev = get_imsm_dev(super, i);
8138 map = get_imsm_map(dev, MAP_0);
8139 map2 = get_imsm_map(dev, MAP_1);
8140 level = get_imsm_raid_level(map);
8141
8142 /* do not publish arrays that are in the middle of an
8143 * unsupported migration
8144 */
8145 if (dev->vol.migr_state &&
8146 (migr_type(dev) == MIGR_STATE_CHANGE)) {
8147 pr_err("cannot assemble volume '%.16s': unsupported migration in progress\n",
8148 dev->volume);
8149 continue;
8150 }
8151 /* do not publish arrays that are not support by controller's
8152 * OROM/EFI
8153 */
8154
8155 this = xmalloc(sizeof(*this));
8156
8157 super->current_vol = i;
8158 getinfo_super_imsm_volume(st, this, NULL);
8159 this->next = rest;
8160 chunk = __le16_to_cpu(map->blocks_per_strip) >> 1;
8161 /* mdadm does not support all metadata features- set the bit in all arrays state */
8162 if (!validate_geometry_imsm_orom(super,
8163 level, /* RAID level */
8164 imsm_level_to_layout(level),
8165 map->num_members, /* raid disks */
8166 &chunk, imsm_dev_size(dev),
8167 1 /* verbose */)) {
8168 pr_err("IMSM RAID geometry validation failed. Array %s activation is blocked.\n",
8169 dev->volume);
8170 this->array.state |=
8171 (1<<MD_SB_BLOCK_CONTAINER_RESHAPE) |
8172 (1<<MD_SB_BLOCK_VOLUME);
8173 }
8174
8175 /* if array has bad blocks, set suitable bit in all arrays state */
8176 if (sb_errors)
8177 this->array.state |=
8178 (1<<MD_SB_BLOCK_CONTAINER_RESHAPE) |
8179 (1<<MD_SB_BLOCK_VOLUME);
8180
8181 for (slot = 0 ; slot < map->num_members; slot++) {
8182 unsigned long long recovery_start;
8183 struct mdinfo *info_d;
8184 struct dl *d;
8185 int idx;
8186 int skip;
8187 __u32 ord;
8188 int missing = 0;
8189
8190 skip = 0;
8191 idx = get_imsm_disk_idx(dev, slot, MAP_0);
8192 ord = get_imsm_ord_tbl_ent(dev, slot, MAP_X);
8193 for (d = super->disks; d ; d = d->next)
8194 if (d->index == idx)
8195 break;
8196
8197 recovery_start = MaxSector;
8198 if (d == NULL)
8199 skip = 1;
8200 if (d && is_failed(&d->disk))
8201 skip = 1;
8202 if (!skip && (ord & IMSM_ORD_REBUILD))
8203 recovery_start = 0;
8204 if (!(ord & IMSM_ORD_REBUILD))
8205 this->array.working_disks++;
8206 /*
8207 * if we skip some disks the array will be assmebled degraded;
8208 * reset resync start to avoid a dirty-degraded
8209 * situation when performing the intial sync
8210 */
8211 if (skip)
8212 missing++;
8213
8214 if (!(dev->vol.dirty & RAIDVOL_DIRTY)) {
8215 if ((!able_to_resync(level, missing) ||
8216 recovery_start == 0))
8217 this->resync_start = MaxSector;
8218 }
8219
8220 if (skip)
8221 continue;
8222
8223 info_d = xcalloc(1, sizeof(*info_d));
8224 info_d->next = this->devs;
8225 this->devs = info_d;
8226
8227 info_d->disk.number = d->index;
8228 info_d->disk.major = d->major;
8229 info_d->disk.minor = d->minor;
8230 info_d->disk.raid_disk = slot;
8231 info_d->recovery_start = recovery_start;
8232 if (map2) {
8233 if (slot < map2->num_members)
8234 info_d->disk.state = (1 << MD_DISK_ACTIVE);
8235 else
8236 this->array.spare_disks++;
8237 } else {
8238 if (slot < map->num_members)
8239 info_d->disk.state = (1 << MD_DISK_ACTIVE);
8240 else
8241 this->array.spare_disks++;
8242 }
8243
8244 info_d->events = __le32_to_cpu(mpb->generation_num);
8245 info_d->data_offset = pba_of_lba0(map);
8246 info_d->component_size = calc_component_size(map, dev);
8247
8248 if (map->raid_level == IMSM_T_RAID5) {
8249 info_d->ppl_sector = this->ppl_sector;
8250 info_d->ppl_size = this->ppl_size;
8251 if (this->consistency_policy == CONSISTENCY_POLICY_PPL &&
8252 recovery_start == 0)
8253 this->resync_start = 0;
8254 }
8255
8256 info_d->bb.supported = 1;
8257 get_volume_badblocks(super->bbm_log, ord_to_idx(ord),
8258 info_d->data_offset,
8259 info_d->component_size,
8260 &info_d->bb);
8261 }
8262 /* now that the disk list is up-to-date fixup recovery_start */
8263 update_recovery_start(super, dev, this);
8264 this->array.spare_disks += spare_disks;
8265
8266 /* check for reshape */
8267 if (this->reshape_active == 1)
8268 recover_backup_imsm(st, this);
8269 rest = this;
8270 }
8271
8272 super->current_vol = current_vol;
8273 return rest;
8274 }
8275
8276 static __u8 imsm_check_degraded(struct intel_super *super, struct imsm_dev *dev,
8277 int failed, int look_in_map)
8278 {
8279 struct imsm_map *map;
8280
8281 map = get_imsm_map(dev, look_in_map);
8282
8283 if (!failed)
8284 return map->map_state == IMSM_T_STATE_UNINITIALIZED ?
8285 IMSM_T_STATE_UNINITIALIZED : IMSM_T_STATE_NORMAL;
8286
8287 switch (get_imsm_raid_level(map)) {
8288 case 0:
8289 return IMSM_T_STATE_FAILED;
8290 break;
8291 case 1:
8292 if (failed < map->num_members)
8293 return IMSM_T_STATE_DEGRADED;
8294 else
8295 return IMSM_T_STATE_FAILED;
8296 break;
8297 case 10:
8298 {
8299 /**
8300 * check to see if any mirrors have failed, otherwise we
8301 * are degraded. Even numbered slots are mirrored on
8302 * slot+1
8303 */
8304 int i;
8305 /* gcc -Os complains that this is unused */
8306 int insync = insync;
8307
8308 for (i = 0; i < map->num_members; i++) {
8309 __u32 ord = get_imsm_ord_tbl_ent(dev, i, MAP_X);
8310 int idx = ord_to_idx(ord);
8311 struct imsm_disk *disk;
8312
8313 /* reset the potential in-sync count on even-numbered
8314 * slots. num_copies is always 2 for imsm raid10
8315 */
8316 if ((i & 1) == 0)
8317 insync = 2;
8318
8319 disk = get_imsm_disk(super, idx);
8320 if (!disk || is_failed(disk) || ord & IMSM_ORD_REBUILD)
8321 insync--;
8322
8323 /* no in-sync disks left in this mirror the
8324 * array has failed
8325 */
8326 if (insync == 0)
8327 return IMSM_T_STATE_FAILED;
8328 }
8329
8330 return IMSM_T_STATE_DEGRADED;
8331 }
8332 case 5:
8333 if (failed < 2)
8334 return IMSM_T_STATE_DEGRADED;
8335 else
8336 return IMSM_T_STATE_FAILED;
8337 break;
8338 default:
8339 break;
8340 }
8341
8342 return map->map_state;
8343 }
8344
8345 static int imsm_count_failed(struct intel_super *super, struct imsm_dev *dev,
8346 int look_in_map)
8347 {
8348 int i;
8349 int failed = 0;
8350 struct imsm_disk *disk;
8351 struct imsm_map *map = get_imsm_map(dev, MAP_0);
8352 struct imsm_map *prev = get_imsm_map(dev, MAP_1);
8353 struct imsm_map *map_for_loop;
8354 __u32 ord;
8355 int idx;
8356 int idx_1;
8357
8358 /* at the beginning of migration we set IMSM_ORD_REBUILD on
8359 * disks that are being rebuilt. New failures are recorded to
8360 * map[0]. So we look through all the disks we started with and
8361 * see if any failures are still present, or if any new ones
8362 * have arrived
8363 */
8364 map_for_loop = map;
8365 if (prev && (map->num_members < prev->num_members))
8366 map_for_loop = prev;
8367
8368 for (i = 0; i < map_for_loop->num_members; i++) {
8369 idx_1 = -255;
8370 /* when MAP_X is passed both maps failures are counted
8371 */
8372 if (prev &&
8373 (look_in_map == MAP_1 || look_in_map == MAP_X) &&
8374 i < prev->num_members) {
8375 ord = __le32_to_cpu(prev->disk_ord_tbl[i]);
8376 idx_1 = ord_to_idx(ord);
8377
8378 disk = get_imsm_disk(super, idx_1);
8379 if (!disk || is_failed(disk) || ord & IMSM_ORD_REBUILD)
8380 failed++;
8381 }
8382 if ((look_in_map == MAP_0 || look_in_map == MAP_X) &&
8383 i < map->num_members) {
8384 ord = __le32_to_cpu(map->disk_ord_tbl[i]);
8385 idx = ord_to_idx(ord);
8386
8387 if (idx != idx_1) {
8388 disk = get_imsm_disk(super, idx);
8389 if (!disk || is_failed(disk) ||
8390 ord & IMSM_ORD_REBUILD)
8391 failed++;
8392 }
8393 }
8394 }
8395
8396 return failed;
8397 }
8398
8399 static int imsm_open_new(struct supertype *c, struct active_array *a,
8400 int inst)
8401 {
8402 struct intel_super *super = c->sb;
8403 struct imsm_super *mpb = super->anchor;
8404 struct imsm_update_prealloc_bb_mem u;
8405
8406 if (inst >= mpb->num_raid_devs) {
8407 pr_err("subarry index %d, out of range\n", inst);
8408 return -ENODEV;
8409 }
8410
8411 dprintf("imsm: open_new %d\n", inst);
8412 a->info.container_member = inst;
8413
8414 u.type = update_prealloc_badblocks_mem;
8415 imsm_update_metadata_locally(c, &u, sizeof(u));
8416
8417 return 0;
8418 }
8419
8420 static int is_resyncing(struct imsm_dev *dev)
8421 {
8422 struct imsm_map *migr_map;
8423
8424 if (!dev->vol.migr_state)
8425 return 0;
8426
8427 if (migr_type(dev) == MIGR_INIT ||
8428 migr_type(dev) == MIGR_REPAIR)
8429 return 1;
8430
8431 if (migr_type(dev) == MIGR_GEN_MIGR)
8432 return 0;
8433
8434 migr_map = get_imsm_map(dev, MAP_1);
8435
8436 if (migr_map->map_state == IMSM_T_STATE_NORMAL &&
8437 dev->vol.migr_type != MIGR_GEN_MIGR)
8438 return 1;
8439 else
8440 return 0;
8441 }
8442
8443 /* return true if we recorded new information */
8444 static int mark_failure(struct intel_super *super,
8445 struct imsm_dev *dev, struct imsm_disk *disk, int idx)
8446 {
8447 __u32 ord;
8448 int slot;
8449 struct imsm_map *map;
8450 char buf[MAX_RAID_SERIAL_LEN+3];
8451 unsigned int len, shift = 0;
8452
8453 /* new failures are always set in map[0] */
8454 map = get_imsm_map(dev, MAP_0);
8455
8456 slot = get_imsm_disk_slot(map, idx);
8457 if (slot < 0)
8458 return 0;
8459
8460 ord = __le32_to_cpu(map->disk_ord_tbl[slot]);
8461 if (is_failed(disk) && (ord & IMSM_ORD_REBUILD))
8462 return 0;
8463
8464 memcpy(buf, disk->serial, MAX_RAID_SERIAL_LEN);
8465 buf[MAX_RAID_SERIAL_LEN] = '\000';
8466 strcat(buf, ":0");
8467 if ((len = strlen(buf)) >= MAX_RAID_SERIAL_LEN)
8468 shift = len - MAX_RAID_SERIAL_LEN + 1;
8469 memcpy(disk->serial, &buf[shift], len + 1 - shift);
8470
8471 disk->status |= FAILED_DISK;
8472 set_imsm_ord_tbl_ent(map, slot, idx | IMSM_ORD_REBUILD);
8473 /* mark failures in second map if second map exists and this disk
8474 * in this slot.
8475 * This is valid for migration, initialization and rebuild
8476 */
8477 if (dev->vol.migr_state) {
8478 struct imsm_map *map2 = get_imsm_map(dev, MAP_1);
8479 int slot2 = get_imsm_disk_slot(map2, idx);
8480
8481 if (slot2 < map2->num_members && slot2 >= 0)
8482 set_imsm_ord_tbl_ent(map2, slot2,
8483 idx | IMSM_ORD_REBUILD);
8484 }
8485 if (map->failed_disk_num == 0xff ||
8486 (!is_rebuilding(dev) && map->failed_disk_num > slot))
8487 map->failed_disk_num = slot;
8488
8489 clear_disk_badblocks(super->bbm_log, ord_to_idx(ord));
8490
8491 return 1;
8492 }
8493
8494 static void mark_missing(struct intel_super *super,
8495 struct imsm_dev *dev, struct imsm_disk *disk, int idx)
8496 {
8497 mark_failure(super, dev, disk, idx);
8498
8499 if (disk->scsi_id == __cpu_to_le32(~(__u32)0))
8500 return;
8501
8502 disk->scsi_id = __cpu_to_le32(~(__u32)0);
8503 memmove(&disk->serial[0], &disk->serial[1], MAX_RAID_SERIAL_LEN - 1);
8504 }
8505
8506 static void handle_missing(struct intel_super *super, struct imsm_dev *dev)
8507 {
8508 struct dl *dl;
8509
8510 if (!super->missing)
8511 return;
8512
8513 /* When orom adds replacement for missing disk it does
8514 * not remove entry of missing disk, but just updates map with
8515 * new added disk. So it is not enough just to test if there is
8516 * any missing disk, we have to look if there are any failed disks
8517 * in map to stop migration */
8518
8519 dprintf("imsm: mark missing\n");
8520 /* end process for initialization and rebuild only
8521 */
8522 if (is_gen_migration(dev) == false) {
8523 int failed = imsm_count_failed(super, dev, MAP_0);
8524
8525 if (failed) {
8526 __u8 map_state;
8527 struct imsm_map *map = get_imsm_map(dev, MAP_0);
8528 struct imsm_map *map1;
8529 int i, ord, ord_map1;
8530 int rebuilt = 1;
8531
8532 for (i = 0; i < map->num_members; i++) {
8533 ord = get_imsm_ord_tbl_ent(dev, i, MAP_0);
8534 if (!(ord & IMSM_ORD_REBUILD))
8535 continue;
8536
8537 map1 = get_imsm_map(dev, MAP_1);
8538 if (!map1)
8539 continue;
8540
8541 ord_map1 = __le32_to_cpu(map1->disk_ord_tbl[i]);
8542 if (ord_map1 & IMSM_ORD_REBUILD)
8543 rebuilt = 0;
8544 }
8545
8546 if (rebuilt) {
8547 map_state = imsm_check_degraded(super, dev,
8548 failed, MAP_0);
8549 end_migration(dev, super, map_state);
8550 }
8551 }
8552 }
8553 for (dl = super->missing; dl; dl = dl->next)
8554 mark_missing(super, dev, &dl->disk, dl->index);
8555 super->updates_pending++;
8556 }
8557
8558 static unsigned long long imsm_set_array_size(struct imsm_dev *dev,
8559 long long new_size)
8560 {
8561 unsigned long long array_blocks;
8562 struct imsm_map *map = get_imsm_map(dev, MAP_0);
8563 int used_disks = imsm_num_data_members(map);
8564
8565 if (used_disks == 0) {
8566 /* when problems occures
8567 * return current array_blocks value
8568 */
8569 array_blocks = imsm_dev_size(dev);
8570
8571 return array_blocks;
8572 }
8573
8574 /* set array size in metadata
8575 */
8576 if (new_size <= 0)
8577 /* OLCE size change is caused by added disks
8578 */
8579 array_blocks = per_dev_array_size(map) * used_disks;
8580 else
8581 /* Online Volume Size Change
8582 * Using available free space
8583 */
8584 array_blocks = new_size;
8585
8586 array_blocks = round_size_to_mb(array_blocks, used_disks);
8587 set_imsm_dev_size(dev, array_blocks);
8588
8589 return array_blocks;
8590 }
8591
8592 static void imsm_set_disk(struct active_array *a, int n, int state);
8593
8594 static void imsm_progress_container_reshape(struct intel_super *super)
8595 {
8596 /* if no device has a migr_state, but some device has a
8597 * different number of members than the previous device, start
8598 * changing the number of devices in this device to match
8599 * previous.
8600 */
8601 struct imsm_super *mpb = super->anchor;
8602 int prev_disks = -1;
8603 int i;
8604 int copy_map_size;
8605
8606 for (i = 0; i < mpb->num_raid_devs; i++) {
8607 struct imsm_dev *dev = get_imsm_dev(super, i);
8608 struct imsm_map *map = get_imsm_map(dev, MAP_0);
8609 struct imsm_map *map2;
8610 int prev_num_members;
8611
8612 if (dev->vol.migr_state)
8613 return;
8614
8615 if (prev_disks == -1)
8616 prev_disks = map->num_members;
8617 if (prev_disks == map->num_members)
8618 continue;
8619
8620 /* OK, this array needs to enter reshape mode.
8621 * i.e it needs a migr_state
8622 */
8623
8624 copy_map_size = sizeof_imsm_map(map);
8625 prev_num_members = map->num_members;
8626 map->num_members = prev_disks;
8627 dev->vol.migr_state = 1;
8628 set_vol_curr_migr_unit(dev, 0);
8629 set_migr_type(dev, MIGR_GEN_MIGR);
8630 for (i = prev_num_members;
8631 i < map->num_members; i++)
8632 set_imsm_ord_tbl_ent(map, i, i);
8633 map2 = get_imsm_map(dev, MAP_1);
8634 /* Copy the current map */
8635 memcpy(map2, map, copy_map_size);
8636 map2->num_members = prev_num_members;
8637
8638 imsm_set_array_size(dev, -1);
8639 super->clean_migration_record_by_mdmon = 1;
8640 super->updates_pending++;
8641 }
8642 }
8643
8644 /* Handle dirty -> clean transititions, resync and reshape. Degraded and rebuild
8645 * states are handled in imsm_set_disk() with one exception, when a
8646 * resync is stopped due to a new failure this routine will set the
8647 * 'degraded' state for the array.
8648 */
8649 static int imsm_set_array_state(struct active_array *a, int consistent)
8650 {
8651 int inst = a->info.container_member;
8652 struct intel_super *super = a->container->sb;
8653 struct imsm_dev *dev = get_imsm_dev(super, inst);
8654 struct imsm_map *map = get_imsm_map(dev, MAP_0);
8655 int failed = imsm_count_failed(super, dev, MAP_0);
8656 __u8 map_state = imsm_check_degraded(super, dev, failed, MAP_0);
8657 __u32 blocks_per_unit;
8658
8659 if (dev->vol.migr_state &&
8660 dev->vol.migr_type == MIGR_GEN_MIGR) {
8661 /* array state change is blocked due to reshape action
8662 * We might need to
8663 * - abort the reshape (if last_checkpoint is 0 and action!= reshape)
8664 * - finish the reshape (if last_checkpoint is big and action != reshape)
8665 * - update vol_curr_migr_unit
8666 */
8667 if (a->curr_action == reshape) {
8668 /* still reshaping, maybe update vol_curr_migr_unit */
8669 goto mark_checkpoint;
8670 } else {
8671 if (a->last_checkpoint >= a->info.component_size) {
8672 unsigned long long array_blocks;
8673 int used_disks;
8674 struct mdinfo *mdi;
8675
8676 used_disks = imsm_num_data_members(map);
8677 if (used_disks > 0) {
8678 array_blocks =
8679 per_dev_array_size(map) *
8680 used_disks;
8681 array_blocks =
8682 round_size_to_mb(array_blocks,
8683 used_disks);
8684 a->info.custom_array_size = array_blocks;
8685 /* encourage manager to update array
8686 * size
8687 */
8688
8689 a->check_reshape = 1;
8690 }
8691 /* finalize online capacity expansion/reshape */
8692 for (mdi = a->info.devs; mdi; mdi = mdi->next)
8693 imsm_set_disk(a,
8694 mdi->disk.raid_disk,
8695 mdi->curr_state);
8696
8697 imsm_progress_container_reshape(super);
8698 }
8699 }
8700 }
8701
8702 /* before we activate this array handle any missing disks */
8703 if (consistent == 2)
8704 handle_missing(super, dev);
8705
8706 if (consistent == 2 &&
8707 (!is_resync_complete(&a->info) ||
8708 map_state != IMSM_T_STATE_NORMAL ||
8709 dev->vol.migr_state))
8710 consistent = 0;
8711
8712 if (is_resync_complete(&a->info)) {
8713 /* complete intialization / resync,
8714 * recovery and interrupted recovery is completed in
8715 * ->set_disk
8716 */
8717 if (is_resyncing(dev)) {
8718 dprintf("imsm: mark resync done\n");
8719 end_migration(dev, super, map_state);
8720 super->updates_pending++;
8721 a->last_checkpoint = 0;
8722 }
8723 } else if ((!is_resyncing(dev) && !failed) &&
8724 (imsm_reshape_blocks_arrays_changes(super) == 0)) {
8725 /* mark the start of the init process if nothing is failed */
8726 dprintf("imsm: mark resync start\n");
8727 if (map->map_state == IMSM_T_STATE_UNINITIALIZED)
8728 migrate(dev, super, IMSM_T_STATE_NORMAL, MIGR_INIT);
8729 else
8730 migrate(dev, super, IMSM_T_STATE_NORMAL, MIGR_REPAIR);
8731 super->updates_pending++;
8732 }
8733
8734 if (a->prev_action == idle)
8735 goto skip_mark_checkpoint;
8736
8737 mark_checkpoint:
8738 /* skip checkpointing for general migration,
8739 * it is controlled in mdadm
8740 */
8741 if (is_gen_migration(dev))
8742 goto skip_mark_checkpoint;
8743
8744 /* check if we can update vol_curr_migr_unit from resync_start,
8745 * recovery_start
8746 */
8747 blocks_per_unit = blocks_per_migr_unit(super, dev);
8748 if (blocks_per_unit) {
8749 set_vol_curr_migr_unit(dev,
8750 a->last_checkpoint / blocks_per_unit);
8751 dprintf("imsm: mark checkpoint (%llu)\n",
8752 vol_curr_migr_unit(dev));
8753 super->updates_pending++;
8754 }
8755
8756 skip_mark_checkpoint:
8757 /* mark dirty / clean */
8758 if (((dev->vol.dirty & RAIDVOL_DIRTY) && consistent) ||
8759 (!(dev->vol.dirty & RAIDVOL_DIRTY) && !consistent)) {
8760 dprintf("imsm: mark '%s'\n", consistent ? "clean" : "dirty");
8761 if (consistent) {
8762 dev->vol.dirty = RAIDVOL_CLEAN;
8763 } else {
8764 dev->vol.dirty = RAIDVOL_DIRTY;
8765 if (dev->rwh_policy == RWH_DISTRIBUTED ||
8766 dev->rwh_policy == RWH_MULTIPLE_DISTRIBUTED)
8767 dev->vol.dirty |= RAIDVOL_DSRECORD_VALID;
8768 }
8769 super->updates_pending++;
8770 }
8771
8772 return consistent;
8773 }
8774
8775 static int imsm_disk_slot_to_ord(struct active_array *a, int slot)
8776 {
8777 int inst = a->info.container_member;
8778 struct intel_super *super = a->container->sb;
8779 struct imsm_dev *dev = get_imsm_dev(super, inst);
8780 struct imsm_map *map = get_imsm_map(dev, MAP_0);
8781
8782 if (slot > map->num_members) {
8783 pr_err("imsm: imsm_disk_slot_to_ord %d out of range 0..%d\n",
8784 slot, map->num_members - 1);
8785 return -1;
8786 }
8787
8788 if (slot < 0)
8789 return -1;
8790
8791 return get_imsm_ord_tbl_ent(dev, slot, MAP_0);
8792 }
8793
8794 static void imsm_set_disk(struct active_array *a, int n, int state)
8795 {
8796 int inst = a->info.container_member;
8797 struct intel_super *super = a->container->sb;
8798 struct imsm_dev *dev = get_imsm_dev(super, inst);
8799 struct imsm_map *map = get_imsm_map(dev, MAP_0);
8800 struct imsm_disk *disk;
8801 struct mdinfo *mdi;
8802 int recovery_not_finished = 0;
8803 int failed;
8804 int ord;
8805 __u8 map_state;
8806 int rebuild_done = 0;
8807 int i;
8808
8809 ord = get_imsm_ord_tbl_ent(dev, n, MAP_X);
8810 if (ord < 0)
8811 return;
8812
8813 dprintf("imsm: set_disk %d:%x\n", n, state);
8814 disk = get_imsm_disk(super, ord_to_idx(ord));
8815
8816 /* check for new failures */
8817 if (disk && (state & DS_FAULTY)) {
8818 if (mark_failure(super, dev, disk, ord_to_idx(ord)))
8819 super->updates_pending++;
8820 }
8821
8822 /* check if in_sync */
8823 if (state & DS_INSYNC && ord & IMSM_ORD_REBUILD && is_rebuilding(dev)) {
8824 struct imsm_map *migr_map = get_imsm_map(dev, MAP_1);
8825
8826 set_imsm_ord_tbl_ent(migr_map, n, ord_to_idx(ord));
8827 rebuild_done = 1;
8828 super->updates_pending++;
8829 }
8830
8831 failed = imsm_count_failed(super, dev, MAP_0);
8832 map_state = imsm_check_degraded(super, dev, failed, MAP_0);
8833
8834 /* check if recovery complete, newly degraded, or failed */
8835 dprintf("imsm: Detected transition to state ");
8836 switch (map_state) {
8837 case IMSM_T_STATE_NORMAL: /* transition to normal state */
8838 dprintf("normal: ");
8839 if (is_rebuilding(dev)) {
8840 dprintf_cont("while rebuilding");
8841 /* check if recovery is really finished */
8842 for (mdi = a->info.devs; mdi ; mdi = mdi->next)
8843 if (mdi->recovery_start != MaxSector) {
8844 recovery_not_finished = 1;
8845 break;
8846 }
8847 if (recovery_not_finished) {
8848 dprintf_cont("\n");
8849 dprintf("Rebuild has not finished yet, state not changed");
8850 if (a->last_checkpoint < mdi->recovery_start) {
8851 a->last_checkpoint = mdi->recovery_start;
8852 super->updates_pending++;
8853 }
8854 break;
8855 }
8856 end_migration(dev, super, map_state);
8857 map->failed_disk_num = ~0;
8858 super->updates_pending++;
8859 a->last_checkpoint = 0;
8860 break;
8861 }
8862 if (is_gen_migration(dev)) {
8863 dprintf_cont("while general migration");
8864 if (a->last_checkpoint >= a->info.component_size)
8865 end_migration(dev, super, map_state);
8866 else
8867 map->map_state = map_state;
8868 map->failed_disk_num = ~0;
8869 super->updates_pending++;
8870 break;
8871 }
8872 break;
8873 case IMSM_T_STATE_DEGRADED: /* transition to degraded state */
8874 dprintf_cont("degraded: ");
8875 if (map->map_state != map_state && !dev->vol.migr_state) {
8876 dprintf_cont("mark degraded");
8877 map->map_state = map_state;
8878 super->updates_pending++;
8879 a->last_checkpoint = 0;
8880 break;
8881 }
8882 if (is_rebuilding(dev)) {
8883 dprintf_cont("while rebuilding ");
8884 if (state & DS_FAULTY) {
8885 dprintf_cont("removing failed drive ");
8886 if (n == map->failed_disk_num) {
8887 dprintf_cont("end migration");
8888 end_migration(dev, super, map_state);
8889 a->last_checkpoint = 0;
8890 } else {
8891 dprintf_cont("fail detected during rebuild, changing map state");
8892 map->map_state = map_state;
8893 }
8894 super->updates_pending++;
8895 }
8896
8897 if (!rebuild_done)
8898 break;
8899
8900 /* check if recovery is really finished */
8901 for (mdi = a->info.devs; mdi ; mdi = mdi->next)
8902 if (mdi->recovery_start != MaxSector) {
8903 recovery_not_finished = 1;
8904 break;
8905 }
8906 if (recovery_not_finished) {
8907 dprintf_cont("\n");
8908 dprintf_cont("Rebuild has not finished yet");
8909 if (a->last_checkpoint < mdi->recovery_start) {
8910 a->last_checkpoint =
8911 mdi->recovery_start;
8912 super->updates_pending++;
8913 }
8914 break;
8915 }
8916
8917 dprintf_cont(" Rebuild done, still degraded");
8918 end_migration(dev, super, map_state);
8919 a->last_checkpoint = 0;
8920 super->updates_pending++;
8921
8922 for (i = 0; i < map->num_members; i++) {
8923 int idx = get_imsm_ord_tbl_ent(dev, i, MAP_0);
8924
8925 if (idx & IMSM_ORD_REBUILD)
8926 map->failed_disk_num = i;
8927 }
8928 super->updates_pending++;
8929 break;
8930 }
8931 if (is_gen_migration(dev)) {
8932 dprintf_cont("while general migration");
8933 if (a->last_checkpoint >= a->info.component_size)
8934 end_migration(dev, super, map_state);
8935 else {
8936 map->map_state = map_state;
8937 manage_second_map(super, dev);
8938 }
8939 super->updates_pending++;
8940 break;
8941 }
8942 if (is_initializing(dev)) {
8943 dprintf_cont("while initialization.");
8944 map->map_state = map_state;
8945 super->updates_pending++;
8946 break;
8947 }
8948 break;
8949 case IMSM_T_STATE_FAILED: /* transition to failed state */
8950 dprintf_cont("failed: ");
8951 if (is_gen_migration(dev)) {
8952 dprintf_cont("while general migration");
8953 map->map_state = map_state;
8954 super->updates_pending++;
8955 break;
8956 }
8957 if (map->map_state != map_state) {
8958 dprintf_cont("mark failed");
8959 end_migration(dev, super, map_state);
8960 super->updates_pending++;
8961 a->last_checkpoint = 0;
8962 break;
8963 }
8964 break;
8965 default:
8966 dprintf_cont("state %i\n", map_state);
8967 }
8968 dprintf_cont("\n");
8969 }
8970
8971 static int store_imsm_mpb(int fd, struct imsm_super *mpb)
8972 {
8973 void *buf = mpb;
8974 __u32 mpb_size = __le32_to_cpu(mpb->mpb_size);
8975 unsigned long long dsize;
8976 unsigned long long sectors;
8977 unsigned int sector_size;
8978
8979 if (!get_dev_sector_size(fd, NULL, &sector_size))
8980 return 1;
8981 get_dev_size(fd, NULL, &dsize);
8982
8983 if (mpb_size > sector_size) {
8984 /* -1 to account for anchor */
8985 sectors = mpb_sectors(mpb, sector_size) - 1;
8986
8987 /* write the extended mpb to the sectors preceeding the anchor */
8988 if (lseek64(fd, dsize - (sector_size * (2 + sectors)),
8989 SEEK_SET) < 0)
8990 return 1;
8991
8992 if ((unsigned long long)write(fd, buf + sector_size,
8993 sector_size * sectors) != sector_size * sectors)
8994 return 1;
8995 }
8996
8997 /* first block is stored on second to last sector of the disk */
8998 if (lseek64(fd, dsize - (sector_size * 2), SEEK_SET) < 0)
8999 return 1;
9000
9001 if ((unsigned int)write(fd, buf, sector_size) != sector_size)
9002 return 1;
9003
9004 return 0;
9005 }
9006
9007 static void imsm_sync_metadata(struct supertype *container)
9008 {
9009 struct intel_super *super = container->sb;
9010
9011 dprintf("sync metadata: %d\n", super->updates_pending);
9012 if (!super->updates_pending)
9013 return;
9014
9015 write_super_imsm(container, 0);
9016
9017 super->updates_pending = 0;
9018 }
9019
9020 static struct dl *imsm_readd(struct intel_super *super, int idx, struct active_array *a)
9021 {
9022 struct imsm_dev *dev = get_imsm_dev(super, a->info.container_member);
9023 int i = get_imsm_disk_idx(dev, idx, MAP_X);
9024 struct dl *dl;
9025
9026 for (dl = super->disks; dl; dl = dl->next)
9027 if (dl->index == i)
9028 break;
9029
9030 if (dl && is_failed(&dl->disk))
9031 dl = NULL;
9032
9033 if (dl)
9034 dprintf("found %x:%x\n", dl->major, dl->minor);
9035
9036 return dl;
9037 }
9038
9039 static struct dl *imsm_add_spare(struct intel_super *super, int slot,
9040 struct active_array *a, int activate_new,
9041 struct mdinfo *additional_test_list)
9042 {
9043 struct imsm_dev *dev = get_imsm_dev(super, a->info.container_member);
9044 int idx = get_imsm_disk_idx(dev, slot, MAP_X);
9045 struct imsm_super *mpb = super->anchor;
9046 struct imsm_map *map;
9047 unsigned long long pos;
9048 struct mdinfo *d;
9049 struct extent *ex;
9050 int i, j;
9051 int found;
9052 __u32 array_start = 0;
9053 __u32 array_end = 0;
9054 struct dl *dl;
9055 struct mdinfo *test_list;
9056
9057 for (dl = super->disks; dl; dl = dl->next) {
9058 /* If in this array, skip */
9059 for (d = a->info.devs ; d ; d = d->next)
9060 if (is_fd_valid(d->state_fd) &&
9061 d->disk.major == dl->major &&
9062 d->disk.minor == dl->minor) {
9063 dprintf("%x:%x already in array\n",
9064 dl->major, dl->minor);
9065 break;
9066 }
9067 if (d)
9068 continue;
9069 test_list = additional_test_list;
9070 while (test_list) {
9071 if (test_list->disk.major == dl->major &&
9072 test_list->disk.minor == dl->minor) {
9073 dprintf("%x:%x already in additional test list\n",
9074 dl->major, dl->minor);
9075 break;
9076 }
9077 test_list = test_list->next;
9078 }
9079 if (test_list)
9080 continue;
9081
9082 /* skip in use or failed drives */
9083 if (is_failed(&dl->disk) || idx == dl->index ||
9084 dl->index == -2) {
9085 dprintf("%x:%x status (failed: %d index: %d)\n",
9086 dl->major, dl->minor, is_failed(&dl->disk), idx);
9087 continue;
9088 }
9089
9090 /* skip pure spares when we are looking for partially
9091 * assimilated drives
9092 */
9093 if (dl->index == -1 && !activate_new)
9094 continue;
9095
9096 if (!drive_validate_sector_size(super, dl))
9097 continue;
9098
9099 /* Does this unused device have the requisite free space?
9100 * It needs to be able to cover all member volumes
9101 */
9102 ex = get_extents(super, dl, 1);
9103 if (!ex) {
9104 dprintf("cannot get extents\n");
9105 continue;
9106 }
9107 for (i = 0; i < mpb->num_raid_devs; i++) {
9108 dev = get_imsm_dev(super, i);
9109 map = get_imsm_map(dev, MAP_0);
9110
9111 /* check if this disk is already a member of
9112 * this array
9113 */
9114 if (get_imsm_disk_slot(map, dl->index) >= 0)
9115 continue;
9116
9117 found = 0;
9118 j = 0;
9119 pos = 0;
9120 array_start = pba_of_lba0(map);
9121 array_end = array_start +
9122 per_dev_array_size(map) - 1;
9123
9124 do {
9125 /* check that we can start at pba_of_lba0 with
9126 * num_data_stripes*blocks_per_stripe of space
9127 */
9128 if (array_start >= pos && array_end < ex[j].start) {
9129 found = 1;
9130 break;
9131 }
9132 pos = ex[j].start + ex[j].size;
9133 j++;
9134 } while (ex[j-1].size);
9135
9136 if (!found)
9137 break;
9138 }
9139
9140 free(ex);
9141 if (i < mpb->num_raid_devs) {
9142 dprintf("%x:%x does not have %u to %u available\n",
9143 dl->major, dl->minor, array_start, array_end);
9144 /* No room */
9145 continue;
9146 }
9147 return dl;
9148 }
9149
9150 return dl;
9151 }
9152
9153 static int imsm_rebuild_allowed(struct supertype *cont, int dev_idx, int failed)
9154 {
9155 struct imsm_dev *dev2;
9156 struct imsm_map *map;
9157 struct dl *idisk;
9158 int slot;
9159 int idx;
9160 __u8 state;
9161
9162 dev2 = get_imsm_dev(cont->sb, dev_idx);
9163
9164 state = imsm_check_degraded(cont->sb, dev2, failed, MAP_0);
9165 if (state == IMSM_T_STATE_FAILED) {
9166 map = get_imsm_map(dev2, MAP_0);
9167 for (slot = 0; slot < map->num_members; slot++) {
9168 /*
9169 * Check if failed disks are deleted from intel
9170 * disk list or are marked to be deleted
9171 */
9172 idx = get_imsm_disk_idx(dev2, slot, MAP_X);
9173 idisk = get_imsm_dl_disk(cont->sb, idx);
9174 /*
9175 * Do not rebuild the array if failed disks
9176 * from failed sub-array are not removed from
9177 * container.
9178 */
9179 if (idisk &&
9180 is_failed(&idisk->disk) &&
9181 (idisk->action != DISK_REMOVE))
9182 return 0;
9183 }
9184 }
9185 return 1;
9186 }
9187
9188 static struct mdinfo *imsm_activate_spare(struct active_array *a,
9189 struct metadata_update **updates)
9190 {
9191 /**
9192 * Find a device with unused free space and use it to replace a
9193 * failed/vacant region in an array. We replace failed regions one a
9194 * array at a time. The result is that a new spare disk will be added
9195 * to the first failed array and after the monitor has finished
9196 * propagating failures the remainder will be consumed.
9197 *
9198 * FIXME add a capability for mdmon to request spares from another
9199 * container.
9200 */
9201
9202 struct intel_super *super = a->container->sb;
9203 int inst = a->info.container_member;
9204 struct imsm_dev *dev = get_imsm_dev(super, inst);
9205 struct imsm_map *map = get_imsm_map(dev, MAP_0);
9206 int failed = a->info.array.raid_disks;
9207 struct mdinfo *rv = NULL;
9208 struct mdinfo *d;
9209 struct mdinfo *di;
9210 struct metadata_update *mu;
9211 struct dl *dl;
9212 struct imsm_update_activate_spare *u;
9213 int num_spares = 0;
9214 int i;
9215 int allowed;
9216
9217 for (d = a->info.devs ; d; d = d->next) {
9218 if (!is_fd_valid(d->state_fd))
9219 continue;
9220
9221 if (d->curr_state & DS_FAULTY)
9222 /* wait for Removal to happen */
9223 return NULL;
9224
9225 failed--;
9226 }
9227
9228 dprintf("imsm: activate spare: inst=%d failed=%d (%d) level=%d\n",
9229 inst, failed, a->info.array.raid_disks, a->info.array.level);
9230
9231 if (imsm_reshape_blocks_arrays_changes(super))
9232 return NULL;
9233
9234 /* Cannot activate another spare if rebuild is in progress already
9235 */
9236 if (is_rebuilding(dev)) {
9237 dprintf("imsm: No spare activation allowed. Rebuild in progress already.\n");
9238 return NULL;
9239 }
9240
9241 if (a->info.array.level == 4)
9242 /* No repair for takeovered array
9243 * imsm doesn't support raid4
9244 */
9245 return NULL;
9246
9247 if (imsm_check_degraded(super, dev, failed, MAP_0) !=
9248 IMSM_T_STATE_DEGRADED)
9249 return NULL;
9250
9251 if (get_imsm_map(dev, MAP_0)->map_state == IMSM_T_STATE_UNINITIALIZED) {
9252 dprintf("imsm: No spare activation allowed. Volume is not initialized.\n");
9253 return NULL;
9254 }
9255
9256 /*
9257 * If there are any failed disks check state of the other volume.
9258 * Block rebuild if the another one is failed until failed disks
9259 * are removed from container.
9260 */
9261 if (failed) {
9262 dprintf("found failed disks in %.*s, check if there anotherfailed sub-array.\n",
9263 MAX_RAID_SERIAL_LEN, dev->volume);
9264 /* check if states of the other volumes allow for rebuild */
9265 for (i = 0; i < super->anchor->num_raid_devs; i++) {
9266 if (i != inst) {
9267 allowed = imsm_rebuild_allowed(a->container,
9268 i, failed);
9269 if (!allowed)
9270 return NULL;
9271 }
9272 }
9273 }
9274
9275 /* For each slot, if it is not working, find a spare */
9276 for (i = 0; i < a->info.array.raid_disks; i++) {
9277 for (d = a->info.devs ; d ; d = d->next)
9278 if (d->disk.raid_disk == i)
9279 break;
9280 dprintf("found %d: %p %x\n", i, d, d?d->curr_state:0);
9281 if (d && is_fd_valid(d->state_fd))
9282 continue;
9283
9284 /*
9285 * OK, this device needs recovery. Try to re-add the
9286 * previous occupant of this slot, if this fails see if
9287 * we can continue the assimilation of a spare that was
9288 * partially assimilated, finally try to activate a new
9289 * spare.
9290 */
9291 dl = imsm_readd(super, i, a);
9292 if (!dl)
9293 dl = imsm_add_spare(super, i, a, 0, rv);
9294 if (!dl)
9295 dl = imsm_add_spare(super, i, a, 1, rv);
9296 if (!dl)
9297 continue;
9298
9299 /* found a usable disk with enough space */
9300 di = xcalloc(1, sizeof(*di));
9301
9302 /* dl->index will be -1 in the case we are activating a
9303 * pristine spare. imsm_process_update() will create a
9304 * new index in this case. Once a disk is found to be
9305 * failed in all member arrays it is kicked from the
9306 * metadata
9307 */
9308 di->disk.number = dl->index;
9309
9310 /* (ab)use di->devs to store a pointer to the device
9311 * we chose
9312 */
9313 di->devs = (struct mdinfo *) dl;
9314
9315 di->disk.raid_disk = i;
9316 di->disk.major = dl->major;
9317 di->disk.minor = dl->minor;
9318 di->disk.state = 0;
9319 di->recovery_start = 0;
9320 di->data_offset = pba_of_lba0(map);
9321 di->component_size = a->info.component_size;
9322 di->container_member = inst;
9323 di->bb.supported = 1;
9324 if (a->info.consistency_policy == CONSISTENCY_POLICY_PPL) {
9325 di->ppl_sector = get_ppl_sector(super, inst);
9326 di->ppl_size = MULTIPLE_PPL_AREA_SIZE_IMSM >> 9;
9327 }
9328 super->random = random32();
9329 di->next = rv;
9330 rv = di;
9331 num_spares++;
9332 dprintf("%x:%x to be %d at %llu\n", dl->major, dl->minor,
9333 i, di->data_offset);
9334 }
9335
9336 if (!rv)
9337 /* No spares found */
9338 return rv;
9339 /* Now 'rv' has a list of devices to return.
9340 * Create a metadata_update record to update the
9341 * disk_ord_tbl for the array
9342 */
9343 mu = xmalloc(sizeof(*mu));
9344 mu->buf = xcalloc(num_spares,
9345 sizeof(struct imsm_update_activate_spare));
9346 mu->space = NULL;
9347 mu->space_list = NULL;
9348 mu->len = sizeof(struct imsm_update_activate_spare) * num_spares;
9349 mu->next = *updates;
9350 u = (struct imsm_update_activate_spare *) mu->buf;
9351
9352 for (di = rv ; di ; di = di->next) {
9353 u->type = update_activate_spare;
9354 u->dl = (struct dl *) di->devs;
9355 di->devs = NULL;
9356 u->slot = di->disk.raid_disk;
9357 u->array = inst;
9358 u->next = u + 1;
9359 u++;
9360 }
9361 (u-1)->next = NULL;
9362 *updates = mu;
9363
9364 return rv;
9365 }
9366
9367 static int disks_overlap(struct intel_super *super, int idx, struct imsm_update_create_array *u)
9368 {
9369 struct imsm_dev *dev = get_imsm_dev(super, idx);
9370 struct imsm_map *map = get_imsm_map(dev, MAP_0);
9371 struct imsm_map *new_map = get_imsm_map(&u->dev, MAP_0);
9372 struct disk_info *inf = get_disk_info(u);
9373 struct imsm_disk *disk;
9374 int i;
9375 int j;
9376
9377 for (i = 0; i < map->num_members; i++) {
9378 disk = get_imsm_disk(super, get_imsm_disk_idx(dev, i, MAP_X));
9379 for (j = 0; j < new_map->num_members; j++)
9380 if (serialcmp(disk->serial, inf[j].serial) == 0)
9381 return 1;
9382 }
9383
9384 return 0;
9385 }
9386
9387 static struct dl *get_disk_super(struct intel_super *super, int major, int minor)
9388 {
9389 struct dl *dl;
9390
9391 for (dl = super->disks; dl; dl = dl->next)
9392 if (dl->major == major && dl->minor == minor)
9393 return dl;
9394 return NULL;
9395 }
9396
9397 static int remove_disk_super(struct intel_super *super, int major, int minor)
9398 {
9399 struct dl *prev;
9400 struct dl *dl;
9401
9402 prev = NULL;
9403 for (dl = super->disks; dl; dl = dl->next) {
9404 if (dl->major == major && dl->minor == minor) {
9405 /* remove */
9406 if (prev)
9407 prev->next = dl->next;
9408 else
9409 super->disks = dl->next;
9410 dl->next = NULL;
9411 __free_imsm_disk(dl, 1);
9412 dprintf("removed %x:%x\n", major, minor);
9413 break;
9414 }
9415 prev = dl;
9416 }
9417 return 0;
9418 }
9419
9420 static void imsm_delete(struct intel_super *super, struct dl **dlp, unsigned index);
9421
9422 static int add_remove_disk_update(struct intel_super *super)
9423 {
9424 int check_degraded = 0;
9425 struct dl *disk;
9426
9427 /* add/remove some spares to/from the metadata/contrainer */
9428 while (super->disk_mgmt_list) {
9429 struct dl *disk_cfg;
9430
9431 disk_cfg = super->disk_mgmt_list;
9432 super->disk_mgmt_list = disk_cfg->next;
9433 disk_cfg->next = NULL;
9434
9435 if (disk_cfg->action == DISK_ADD) {
9436 disk_cfg->next = super->disks;
9437 super->disks = disk_cfg;
9438 check_degraded = 1;
9439 dprintf("added %x:%x\n",
9440 disk_cfg->major, disk_cfg->minor);
9441 } else if (disk_cfg->action == DISK_REMOVE) {
9442 dprintf("Disk remove action processed: %x.%x\n",
9443 disk_cfg->major, disk_cfg->minor);
9444 disk = get_disk_super(super,
9445 disk_cfg->major,
9446 disk_cfg->minor);
9447 if (disk) {
9448 /* store action status */
9449 disk->action = DISK_REMOVE;
9450 /* remove spare disks only */
9451 if (disk->index == -1) {
9452 remove_disk_super(super,
9453 disk_cfg->major,
9454 disk_cfg->minor);
9455 } else {
9456 disk_cfg->fd = disk->fd;
9457 disk->fd = -1;
9458 }
9459 }
9460 /* release allocate disk structure */
9461 __free_imsm_disk(disk_cfg, 1);
9462 }
9463 }
9464 return check_degraded;
9465 }
9466
9467 static int apply_reshape_migration_update(struct imsm_update_reshape_migration *u,
9468 struct intel_super *super,
9469 void ***space_list)
9470 {
9471 struct intel_dev *id;
9472 void **tofree = NULL;
9473 int ret_val = 0;
9474
9475 dprintf("(enter)\n");
9476 if (u->subdev < 0 || u->subdev > 1) {
9477 dprintf("imsm: Error: Wrong subdev: %i\n", u->subdev);
9478 return ret_val;
9479 }
9480 if (space_list == NULL || *space_list == NULL) {
9481 dprintf("imsm: Error: Memory is not allocated\n");
9482 return ret_val;
9483 }
9484
9485 for (id = super->devlist ; id; id = id->next) {
9486 if (id->index == (unsigned)u->subdev) {
9487 struct imsm_dev *dev = get_imsm_dev(super, u->subdev);
9488 struct imsm_map *map;
9489 struct imsm_dev *new_dev =
9490 (struct imsm_dev *)*space_list;
9491 struct imsm_map *migr_map = get_imsm_map(dev, MAP_1);
9492 int to_state;
9493 struct dl *new_disk;
9494
9495 if (new_dev == NULL)
9496 return ret_val;
9497 *space_list = **space_list;
9498 memcpy(new_dev, dev, sizeof_imsm_dev(dev, 0));
9499 map = get_imsm_map(new_dev, MAP_0);
9500 if (migr_map) {
9501 dprintf("imsm: Error: migration in progress");
9502 return ret_val;
9503 }
9504
9505 to_state = map->map_state;
9506 if ((u->new_level == IMSM_T_RAID5) && (map->raid_level == IMSM_T_RAID0)) {
9507 map->num_members++;
9508 /* this should not happen */
9509 if (u->new_disks[0] < 0) {
9510 map->failed_disk_num =
9511 map->num_members - 1;
9512 to_state = IMSM_T_STATE_DEGRADED;
9513 } else
9514 to_state = IMSM_T_STATE_NORMAL;
9515 }
9516 migrate(new_dev, super, to_state, MIGR_GEN_MIGR);
9517
9518 if (u->new_level > -1)
9519 update_imsm_raid_level(map, u->new_level);
9520
9521 migr_map = get_imsm_map(new_dev, MAP_1);
9522 if ((u->new_level == IMSM_T_RAID5) &&
9523 (migr_map->raid_level == IMSM_T_RAID0)) {
9524 int ord = map->num_members - 1;
9525 migr_map->num_members--;
9526 if (u->new_disks[0] < 0)
9527 ord |= IMSM_ORD_REBUILD;
9528 set_imsm_ord_tbl_ent(map,
9529 map->num_members - 1,
9530 ord);
9531 }
9532 id->dev = new_dev;
9533 tofree = (void **)dev;
9534
9535 /* update chunk size
9536 */
9537 if (u->new_chunksize > 0) {
9538 struct imsm_map *dest_map =
9539 get_imsm_map(dev, MAP_0);
9540 int used_disks =
9541 imsm_num_data_members(dest_map);
9542
9543 if (used_disks == 0)
9544 return ret_val;
9545
9546 map->blocks_per_strip =
9547 __cpu_to_le16(u->new_chunksize * 2);
9548 update_num_data_stripes(map, imsm_dev_size(dev));
9549 }
9550
9551 /* ensure blocks_per_member has valid value
9552 */
9553 set_blocks_per_member(map,
9554 per_dev_array_size(map) +
9555 NUM_BLOCKS_DIRTY_STRIPE_REGION);
9556
9557 /* add disk
9558 */
9559 if (u->new_level != IMSM_T_RAID5 || migr_map->raid_level != IMSM_T_RAID0 ||
9560 migr_map->raid_level == map->raid_level)
9561 goto skip_disk_add;
9562
9563 if (u->new_disks[0] >= 0) {
9564 /* use passes spare
9565 */
9566 new_disk = get_disk_super(super,
9567 major(u->new_disks[0]),
9568 minor(u->new_disks[0]));
9569 dprintf("imsm: new disk for reshape is: %i:%i (%p, index = %i)\n",
9570 major(u->new_disks[0]),
9571 minor(u->new_disks[0]),
9572 new_disk, new_disk->index);
9573 if (new_disk == NULL)
9574 goto error_disk_add;
9575
9576 new_disk->index = map->num_members - 1;
9577 /* slot to fill in autolayout
9578 */
9579 new_disk->raiddisk = new_disk->index;
9580 new_disk->disk.status |= CONFIGURED_DISK;
9581 new_disk->disk.status &= ~SPARE_DISK;
9582 } else
9583 goto error_disk_add;
9584
9585 skip_disk_add:
9586 *tofree = *space_list;
9587 /* calculate new size
9588 */
9589 imsm_set_array_size(new_dev, -1);
9590
9591 ret_val = 1;
9592 }
9593 }
9594
9595 if (tofree)
9596 *space_list = tofree;
9597 return ret_val;
9598
9599 error_disk_add:
9600 dprintf("Error: imsm: Cannot find disk.\n");
9601 return ret_val;
9602 }
9603
9604 static int apply_size_change_update(struct imsm_update_size_change *u,
9605 struct intel_super *super)
9606 {
9607 struct intel_dev *id;
9608 int ret_val = 0;
9609
9610 dprintf("(enter)\n");
9611 if (u->subdev < 0 || u->subdev > 1) {
9612 dprintf("imsm: Error: Wrong subdev: %i\n", u->subdev);
9613 return ret_val;
9614 }
9615
9616 for (id = super->devlist ; id; id = id->next) {
9617 if (id->index == (unsigned)u->subdev) {
9618 struct imsm_dev *dev = get_imsm_dev(super, u->subdev);
9619 struct imsm_map *map = get_imsm_map(dev, MAP_0);
9620 int used_disks = imsm_num_data_members(map);
9621 unsigned long long blocks_per_member;
9622 unsigned long long new_size_per_disk;
9623
9624 if (used_disks == 0)
9625 return 0;
9626
9627 /* calculate new size
9628 */
9629 new_size_per_disk = u->new_size / used_disks;
9630 blocks_per_member = new_size_per_disk +
9631 NUM_BLOCKS_DIRTY_STRIPE_REGION;
9632
9633 imsm_set_array_size(dev, u->new_size);
9634 set_blocks_per_member(map, blocks_per_member);
9635 update_num_data_stripes(map, u->new_size);
9636 ret_val = 1;
9637 break;
9638 }
9639 }
9640
9641 return ret_val;
9642 }
9643
9644 static int prepare_spare_to_activate(struct supertype *st,
9645 struct imsm_update_activate_spare *u)
9646 {
9647 struct intel_super *super = st->sb;
9648 int prev_current_vol = super->current_vol;
9649 struct active_array *a;
9650 int ret = 1;
9651
9652 for (a = st->arrays; a; a = a->next)
9653 /*
9654 * Additional initialization (adding bitmap header, filling
9655 * the bitmap area with '1's to force initial rebuild for a whole
9656 * data-area) is required when adding the spare to the volume
9657 * with write-intent bitmap.
9658 */
9659 if (a->info.container_member == u->array &&
9660 a->info.consistency_policy == CONSISTENCY_POLICY_BITMAP) {
9661 struct dl *dl;
9662
9663 for (dl = super->disks; dl; dl = dl->next)
9664 if (dl == u->dl)
9665 break;
9666 if (!dl)
9667 break;
9668
9669 super->current_vol = u->array;
9670 if (st->ss->write_bitmap(st, dl->fd, NoUpdate))
9671 ret = 0;
9672 super->current_vol = prev_current_vol;
9673 }
9674 return ret;
9675 }
9676
9677 static int apply_update_activate_spare(struct imsm_update_activate_spare *u,
9678 struct intel_super *super,
9679 struct active_array *active_array)
9680 {
9681 struct imsm_super *mpb = super->anchor;
9682 struct imsm_dev *dev = get_imsm_dev(super, u->array);
9683 struct imsm_map *map = get_imsm_map(dev, MAP_0);
9684 struct imsm_map *migr_map;
9685 struct active_array *a;
9686 struct imsm_disk *disk;
9687 __u8 to_state;
9688 struct dl *dl;
9689 unsigned int found;
9690 int failed;
9691 int victim;
9692 int i;
9693 int second_map_created = 0;
9694
9695 for (; u; u = u->next) {
9696 victim = get_imsm_disk_idx(dev, u->slot, MAP_X);
9697
9698 if (victim < 0)
9699 return 0;
9700
9701 for (dl = super->disks; dl; dl = dl->next)
9702 if (dl == u->dl)
9703 break;
9704
9705 if (!dl) {
9706 pr_err("error: imsm_activate_spare passed an unknown disk (index: %d)\n",
9707 u->dl->index);
9708 return 0;
9709 }
9710
9711 /* count failures (excluding rebuilds and the victim)
9712 * to determine map[0] state
9713 */
9714 failed = 0;
9715 for (i = 0; i < map->num_members; i++) {
9716 if (i == u->slot)
9717 continue;
9718 disk = get_imsm_disk(super,
9719 get_imsm_disk_idx(dev, i, MAP_X));
9720 if (!disk || is_failed(disk))
9721 failed++;
9722 }
9723
9724 /* adding a pristine spare, assign a new index */
9725 if (dl->index < 0) {
9726 dl->index = super->anchor->num_disks;
9727 super->anchor->num_disks++;
9728 }
9729 disk = &dl->disk;
9730 disk->status |= CONFIGURED_DISK;
9731 disk->status &= ~SPARE_DISK;
9732
9733 /* mark rebuild */
9734 to_state = imsm_check_degraded(super, dev, failed, MAP_0);
9735 if (!second_map_created) {
9736 second_map_created = 1;
9737 map->map_state = IMSM_T_STATE_DEGRADED;
9738 migrate(dev, super, to_state, MIGR_REBUILD);
9739 } else
9740 map->map_state = to_state;
9741 migr_map = get_imsm_map(dev, MAP_1);
9742 set_imsm_ord_tbl_ent(map, u->slot, dl->index);
9743 set_imsm_ord_tbl_ent(migr_map, u->slot,
9744 dl->index | IMSM_ORD_REBUILD);
9745
9746 /* update the family_num to mark a new container
9747 * generation, being careful to record the existing
9748 * family_num in orig_family_num to clean up after
9749 * earlier mdadm versions that neglected to set it.
9750 */
9751 if (mpb->orig_family_num == 0)
9752 mpb->orig_family_num = mpb->family_num;
9753 mpb->family_num += super->random;
9754
9755 /* count arrays using the victim in the metadata */
9756 found = 0;
9757 for (a = active_array; a ; a = a->next) {
9758 int dev_idx = a->info.container_member;
9759
9760 if (get_disk_slot_in_dev(super, dev_idx, victim) >= 0)
9761 found++;
9762 }
9763
9764 /* delete the victim if it is no longer being
9765 * utilized anywhere
9766 */
9767 if (!found) {
9768 struct dl **dlp;
9769
9770 /* We know that 'manager' isn't touching anything,
9771 * so it is safe to delete
9772 */
9773 for (dlp = &super->disks; *dlp; dlp = &(*dlp)->next)
9774 if ((*dlp)->index == victim)
9775 break;
9776
9777 /* victim may be on the missing list */
9778 if (!*dlp)
9779 for (dlp = &super->missing; *dlp;
9780 dlp = &(*dlp)->next)
9781 if ((*dlp)->index == victim)
9782 break;
9783 imsm_delete(super, dlp, victim);
9784 }
9785 }
9786
9787 return 1;
9788 }
9789
9790 static int apply_reshape_container_disks_update(struct imsm_update_reshape *u,
9791 struct intel_super *super,
9792 void ***space_list)
9793 {
9794 struct dl *new_disk;
9795 struct intel_dev *id;
9796 int i;
9797 int delta_disks = u->new_raid_disks - u->old_raid_disks;
9798 int disk_count = u->old_raid_disks;
9799 void **tofree = NULL;
9800 int devices_to_reshape = 1;
9801 struct imsm_super *mpb = super->anchor;
9802 int ret_val = 0;
9803 unsigned int dev_id;
9804
9805 dprintf("(enter)\n");
9806
9807 /* enable spares to use in array */
9808 for (i = 0; i < delta_disks; i++) {
9809 new_disk = get_disk_super(super,
9810 major(u->new_disks[i]),
9811 minor(u->new_disks[i]));
9812 dprintf("imsm: new disk for reshape is: %i:%i (%p, index = %i)\n",
9813 major(u->new_disks[i]), minor(u->new_disks[i]),
9814 new_disk, new_disk->index);
9815 if (new_disk == NULL ||
9816 (new_disk->index >= 0 &&
9817 new_disk->index < u->old_raid_disks))
9818 goto update_reshape_exit;
9819 new_disk->index = disk_count++;
9820 /* slot to fill in autolayout
9821 */
9822 new_disk->raiddisk = new_disk->index;
9823 new_disk->disk.status |=
9824 CONFIGURED_DISK;
9825 new_disk->disk.status &= ~SPARE_DISK;
9826 }
9827
9828 dprintf("imsm: volume set mpb->num_raid_devs = %i\n",
9829 mpb->num_raid_devs);
9830 /* manage changes in volume
9831 */
9832 for (dev_id = 0; dev_id < mpb->num_raid_devs; dev_id++) {
9833 void **sp = *space_list;
9834 struct imsm_dev *newdev;
9835 struct imsm_map *newmap, *oldmap;
9836
9837 for (id = super->devlist ; id; id = id->next) {
9838 if (id->index == dev_id)
9839 break;
9840 }
9841 if (id == NULL)
9842 break;
9843 if (!sp)
9844 continue;
9845 *space_list = *sp;
9846 newdev = (void*)sp;
9847 /* Copy the dev, but not (all of) the map */
9848 memcpy(newdev, id->dev, sizeof(*newdev));
9849 oldmap = get_imsm_map(id->dev, MAP_0);
9850 newmap = get_imsm_map(newdev, MAP_0);
9851 /* Copy the current map */
9852 memcpy(newmap, oldmap, sizeof_imsm_map(oldmap));
9853 /* update one device only
9854 */
9855 if (devices_to_reshape) {
9856 dprintf("imsm: modifying subdev: %i\n",
9857 id->index);
9858 devices_to_reshape--;
9859 newdev->vol.migr_state = 1;
9860 set_vol_curr_migr_unit(newdev, 0);
9861 set_migr_type(newdev, MIGR_GEN_MIGR);
9862 newmap->num_members = u->new_raid_disks;
9863 for (i = 0; i < delta_disks; i++) {
9864 set_imsm_ord_tbl_ent(newmap,
9865 u->old_raid_disks + i,
9866 u->old_raid_disks + i);
9867 }
9868 /* New map is correct, now need to save old map
9869 */
9870 newmap = get_imsm_map(newdev, MAP_1);
9871 memcpy(newmap, oldmap, sizeof_imsm_map(oldmap));
9872
9873 imsm_set_array_size(newdev, -1);
9874 }
9875
9876 sp = (void **)id->dev;
9877 id->dev = newdev;
9878 *sp = tofree;
9879 tofree = sp;
9880
9881 /* Clear migration record */
9882 memset(super->migr_rec, 0, sizeof(struct migr_record));
9883 }
9884 if (tofree)
9885 *space_list = tofree;
9886 ret_val = 1;
9887
9888 update_reshape_exit:
9889
9890 return ret_val;
9891 }
9892
9893 static int apply_takeover_update(struct imsm_update_takeover *u,
9894 struct intel_super *super,
9895 void ***space_list)
9896 {
9897 struct imsm_dev *dev = NULL;
9898 struct intel_dev *dv;
9899 struct imsm_dev *dev_new;
9900 struct imsm_map *map;
9901 struct dl *dm, *du;
9902 int i;
9903
9904 for (dv = super->devlist; dv; dv = dv->next)
9905 if (dv->index == (unsigned int)u->subarray) {
9906 dev = dv->dev;
9907 break;
9908 }
9909
9910 if (dev == NULL)
9911 return 0;
9912
9913 map = get_imsm_map(dev, MAP_0);
9914
9915 if (u->direction == R10_TO_R0) {
9916 /* Number of failed disks must be half of initial disk number */
9917 if (imsm_count_failed(super, dev, MAP_0) !=
9918 (map->num_members / 2))
9919 return 0;
9920
9921 /* iterate through devices to mark removed disks as spare */
9922 for (dm = super->disks; dm; dm = dm->next) {
9923 if (dm->disk.status & FAILED_DISK) {
9924 int idx = dm->index;
9925 /* update indexes on the disk list */
9926 /* FIXME this loop-with-the-loop looks wrong, I'm not convinced
9927 the index values will end up being correct.... NB */
9928 for (du = super->disks; du; du = du->next)
9929 if (du->index > idx)
9930 du->index--;
9931 /* mark as spare disk */
9932 mark_spare(dm);
9933 }
9934 }
9935 /* update map */
9936 map->num_members /= map->num_domains;
9937 map->map_state = IMSM_T_STATE_NORMAL;
9938 update_imsm_raid_level(map, IMSM_T_RAID0);
9939 set_num_domains(map);
9940 update_num_data_stripes(map, imsm_dev_size(dev));
9941 map->failed_disk_num = -1;
9942 }
9943
9944 if (u->direction == R0_TO_R10) {
9945 void **space;
9946
9947 /* update slots in current disk list */
9948 for (dm = super->disks; dm; dm = dm->next) {
9949 if (dm->index >= 0)
9950 dm->index *= 2;
9951 }
9952 /* create new *missing* disks */
9953 for (i = 0; i < map->num_members; i++) {
9954 space = *space_list;
9955 if (!space)
9956 continue;
9957 *space_list = *space;
9958 du = (void *)space;
9959 memcpy(du, super->disks, sizeof(*du));
9960 du->fd = -1;
9961 du->minor = 0;
9962 du->major = 0;
9963 du->index = (i * 2) + 1;
9964 sprintf((char *)du->disk.serial,
9965 " MISSING_%d", du->index);
9966 sprintf((char *)du->serial,
9967 "MISSING_%d", du->index);
9968 du->next = super->missing;
9969 super->missing = du;
9970 }
9971 /* create new dev and map */
9972 space = *space_list;
9973 if (!space)
9974 return 0;
9975 *space_list = *space;
9976 dev_new = (void *)space;
9977 memcpy(dev_new, dev, sizeof(*dev));
9978 /* update new map */
9979 map = get_imsm_map(dev_new, MAP_0);
9980
9981 map->map_state = IMSM_T_STATE_DEGRADED;
9982 update_imsm_raid_level(map, IMSM_T_RAID10);
9983 set_num_domains(map);
9984 map->num_members = map->num_members * map->num_domains;
9985 update_num_data_stripes(map, imsm_dev_size(dev));
9986
9987 /* replace dev<->dev_new */
9988 dv->dev = dev_new;
9989 }
9990 /* update disk order table */
9991 for (du = super->disks; du; du = du->next)
9992 if (du->index >= 0)
9993 set_imsm_ord_tbl_ent(map, du->index, du->index);
9994 for (du = super->missing; du; du = du->next)
9995 if (du->index >= 0) {
9996 set_imsm_ord_tbl_ent(map, du->index, du->index);
9997 mark_missing(super, dv->dev, &du->disk, du->index);
9998 }
9999
10000 return 1;
10001 }
10002
10003 static void imsm_process_update(struct supertype *st,
10004 struct metadata_update *update)
10005 {
10006 /**
10007 * crack open the metadata_update envelope to find the update record
10008 * update can be one of:
10009 * update_reshape_container_disks - all the arrays in the container
10010 * are being reshaped to have more devices. We need to mark
10011 * the arrays for general migration and convert selected spares
10012 * into active devices.
10013 * update_activate_spare - a spare device has replaced a failed
10014 * device in an array, update the disk_ord_tbl. If this disk is
10015 * present in all member arrays then also clear the SPARE_DISK
10016 * flag
10017 * update_create_array
10018 * update_kill_array
10019 * update_rename_array
10020 * update_add_remove_disk
10021 */
10022 struct intel_super *super = st->sb;
10023 struct imsm_super *mpb;
10024 enum imsm_update_type type = *(enum imsm_update_type *) update->buf;
10025
10026 /* update requires a larger buf but the allocation failed */
10027 if (super->next_len && !super->next_buf) {
10028 super->next_len = 0;
10029 return;
10030 }
10031
10032 if (super->next_buf) {
10033 memcpy(super->next_buf, super->buf, super->len);
10034 free(super->buf);
10035 super->len = super->next_len;
10036 super->buf = super->next_buf;
10037
10038 super->next_len = 0;
10039 super->next_buf = NULL;
10040 }
10041
10042 mpb = super->anchor;
10043
10044 switch (type) {
10045 case update_general_migration_checkpoint: {
10046 struct intel_dev *id;
10047 struct imsm_update_general_migration_checkpoint *u =
10048 (void *)update->buf;
10049
10050 dprintf("called for update_general_migration_checkpoint\n");
10051
10052 /* find device under general migration */
10053 for (id = super->devlist ; id; id = id->next) {
10054 if (is_gen_migration(id->dev)) {
10055 set_vol_curr_migr_unit(id->dev,
10056 u->curr_migr_unit);
10057 super->updates_pending++;
10058 }
10059 }
10060 break;
10061 }
10062 case update_takeover: {
10063 struct imsm_update_takeover *u = (void *)update->buf;
10064 if (apply_takeover_update(u, super, &update->space_list)) {
10065 imsm_update_version_info(super);
10066 super->updates_pending++;
10067 }
10068 break;
10069 }
10070
10071 case update_reshape_container_disks: {
10072 struct imsm_update_reshape *u = (void *)update->buf;
10073 if (apply_reshape_container_disks_update(
10074 u, super, &update->space_list))
10075 super->updates_pending++;
10076 break;
10077 }
10078 case update_reshape_migration: {
10079 struct imsm_update_reshape_migration *u = (void *)update->buf;
10080 if (apply_reshape_migration_update(
10081 u, super, &update->space_list))
10082 super->updates_pending++;
10083 break;
10084 }
10085 case update_size_change: {
10086 struct imsm_update_size_change *u = (void *)update->buf;
10087 if (apply_size_change_update(u, super))
10088 super->updates_pending++;
10089 break;
10090 }
10091 case update_activate_spare: {
10092 struct imsm_update_activate_spare *u = (void *) update->buf;
10093
10094 if (prepare_spare_to_activate(st, u) &&
10095 apply_update_activate_spare(u, super, st->arrays))
10096 super->updates_pending++;
10097 break;
10098 }
10099 case update_create_array: {
10100 /* someone wants to create a new array, we need to be aware of
10101 * a few races/collisions:
10102 * 1/ 'Create' called by two separate instances of mdadm
10103 * 2/ 'Create' versus 'activate_spare': mdadm has chosen
10104 * devices that have since been assimilated via
10105 * activate_spare.
10106 * In the event this update can not be carried out mdadm will
10107 * (FIX ME) notice that its update did not take hold.
10108 */
10109 struct imsm_update_create_array *u = (void *) update->buf;
10110 struct intel_dev *dv;
10111 struct imsm_dev *dev;
10112 struct imsm_map *map, *new_map;
10113 unsigned long long start, end;
10114 unsigned long long new_start, new_end;
10115 int i;
10116 struct disk_info *inf;
10117 struct dl *dl;
10118
10119 /* handle racing creates: first come first serve */
10120 if (u->dev_idx < mpb->num_raid_devs) {
10121 dprintf("subarray %d already defined\n", u->dev_idx);
10122 goto create_error;
10123 }
10124
10125 /* check update is next in sequence */
10126 if (u->dev_idx != mpb->num_raid_devs) {
10127 dprintf("can not create array %d expected index %d\n",
10128 u->dev_idx, mpb->num_raid_devs);
10129 goto create_error;
10130 }
10131
10132 new_map = get_imsm_map(&u->dev, MAP_0);
10133 new_start = pba_of_lba0(new_map);
10134 new_end = new_start + per_dev_array_size(new_map);
10135 inf = get_disk_info(u);
10136
10137 /* handle activate_spare versus create race:
10138 * check to make sure that overlapping arrays do not include
10139 * overalpping disks
10140 */
10141 for (i = 0; i < mpb->num_raid_devs; i++) {
10142 dev = get_imsm_dev(super, i);
10143 map = get_imsm_map(dev, MAP_0);
10144 start = pba_of_lba0(map);
10145 end = start + per_dev_array_size(map);
10146 if ((new_start >= start && new_start <= end) ||
10147 (start >= new_start && start <= new_end))
10148 /* overlap */;
10149 else
10150 continue;
10151
10152 if (disks_overlap(super, i, u)) {
10153 dprintf("arrays overlap\n");
10154 goto create_error;
10155 }
10156 }
10157
10158 /* check that prepare update was successful */
10159 if (!update->space) {
10160 dprintf("prepare update failed\n");
10161 goto create_error;
10162 }
10163
10164 /* check that all disks are still active before committing
10165 * changes. FIXME: could we instead handle this by creating a
10166 * degraded array? That's probably not what the user expects,
10167 * so better to drop this update on the floor.
10168 */
10169 for (i = 0; i < new_map->num_members; i++) {
10170 dl = serial_to_dl(inf[i].serial, super);
10171 if (!dl) {
10172 dprintf("disk disappeared\n");
10173 goto create_error;
10174 }
10175 }
10176
10177 super->updates_pending++;
10178
10179 /* convert spares to members and fixup ord_tbl */
10180 for (i = 0; i < new_map->num_members; i++) {
10181 dl = serial_to_dl(inf[i].serial, super);
10182 if (dl->index == -1) {
10183 dl->index = mpb->num_disks;
10184 mpb->num_disks++;
10185 dl->disk.status |= CONFIGURED_DISK;
10186 dl->disk.status &= ~SPARE_DISK;
10187 }
10188 set_imsm_ord_tbl_ent(new_map, i, dl->index);
10189 }
10190
10191 dv = update->space;
10192 dev = dv->dev;
10193 update->space = NULL;
10194 imsm_copy_dev(dev, &u->dev);
10195 dv->index = u->dev_idx;
10196 dv->next = super->devlist;
10197 super->devlist = dv;
10198 mpb->num_raid_devs++;
10199
10200 imsm_update_version_info(super);
10201 break;
10202 create_error:
10203 /* mdmon knows how to release update->space, but not
10204 * ((struct intel_dev *) update->space)->dev
10205 */
10206 if (update->space) {
10207 dv = update->space;
10208 free(dv->dev);
10209 }
10210 break;
10211 }
10212 case update_kill_array: {
10213 struct imsm_update_kill_array *u = (void *) update->buf;
10214 int victim = u->dev_idx;
10215 struct active_array *a;
10216 struct intel_dev **dp;
10217
10218 /* sanity check that we are not affecting the uuid of
10219 * active arrays, or deleting an active array
10220 *
10221 * FIXME when immutable ids are available, but note that
10222 * we'll also need to fixup the invalidated/active
10223 * subarray indexes in mdstat
10224 */
10225 for (a = st->arrays; a; a = a->next)
10226 if (a->info.container_member >= victim)
10227 break;
10228 /* by definition if mdmon is running at least one array
10229 * is active in the container, so checking
10230 * mpb->num_raid_devs is just extra paranoia
10231 */
10232 if (a || mpb->num_raid_devs == 1 || victim >= super->anchor->num_raid_devs) {
10233 dprintf("failed to delete subarray-%d\n", victim);
10234 break;
10235 }
10236
10237 for (dp = &super->devlist; *dp;)
10238 if ((*dp)->index == (unsigned)super->current_vol) {
10239 *dp = (*dp)->next;
10240 } else {
10241 if ((*dp)->index > (unsigned)victim)
10242 (*dp)->index--;
10243 dp = &(*dp)->next;
10244 }
10245 mpb->num_raid_devs--;
10246 super->updates_pending++;
10247 break;
10248 }
10249 case update_rename_array: {
10250 struct imsm_update_rename_array *u = (void *) update->buf;
10251 char name[MAX_RAID_SERIAL_LEN+1];
10252 int target = u->dev_idx;
10253 struct active_array *a;
10254 struct imsm_dev *dev;
10255
10256 /* sanity check that we are not affecting the uuid of
10257 * an active array
10258 */
10259 memset(name, 0, sizeof(name));
10260 snprintf(name, MAX_RAID_SERIAL_LEN, "%s", (char *) u->name);
10261 name[MAX_RAID_SERIAL_LEN] = '\0';
10262 for (a = st->arrays; a; a = a->next)
10263 if (a->info.container_member == target)
10264 break;
10265 dev = get_imsm_dev(super, u->dev_idx);
10266
10267 if (a || !dev || imsm_is_name_allowed(super, name, 0) == false) {
10268 dprintf("failed to rename subarray-%d\n", target);
10269 break;
10270 }
10271
10272 memcpy(dev->volume, name, MAX_RAID_SERIAL_LEN);
10273 super->updates_pending++;
10274 break;
10275 }
10276 case update_add_remove_disk: {
10277 /* we may be able to repair some arrays if disks are
10278 * being added, check the status of add_remove_disk
10279 * if discs has been added.
10280 */
10281 if (add_remove_disk_update(super)) {
10282 struct active_array *a;
10283
10284 super->updates_pending++;
10285 for (a = st->arrays; a; a = a->next)
10286 a->check_degraded = 1;
10287 }
10288 break;
10289 }
10290 case update_prealloc_badblocks_mem:
10291 break;
10292 case update_rwh_policy: {
10293 struct imsm_update_rwh_policy *u = (void *)update->buf;
10294 int target = u->dev_idx;
10295 struct imsm_dev *dev = get_imsm_dev(super, target);
10296
10297 if (dev->rwh_policy != u->new_policy) {
10298 dev->rwh_policy = u->new_policy;
10299 super->updates_pending++;
10300 }
10301 break;
10302 }
10303 default:
10304 pr_err("error: unsupported process update type:(type: %d)\n", type);
10305 }
10306 }
10307
10308 static struct mdinfo *get_spares_for_grow(struct supertype *st);
10309
10310 static int imsm_prepare_update(struct supertype *st,
10311 struct metadata_update *update)
10312 {
10313 /**
10314 * Allocate space to hold new disk entries, raid-device entries or a new
10315 * mpb if necessary. The manager synchronously waits for updates to
10316 * complete in the monitor, so new mpb buffers allocated here can be
10317 * integrated by the monitor thread without worrying about live pointers
10318 * in the manager thread.
10319 */
10320 enum imsm_update_type type;
10321 struct intel_super *super = st->sb;
10322 unsigned int sector_size = super->sector_size;
10323 struct imsm_super *mpb = super->anchor;
10324 size_t buf_len;
10325 size_t len = 0;
10326
10327 if (update->len < (int)sizeof(type))
10328 return 0;
10329
10330 type = *(enum imsm_update_type *) update->buf;
10331
10332 switch (type) {
10333 case update_general_migration_checkpoint:
10334 if (update->len < (int)sizeof(struct imsm_update_general_migration_checkpoint))
10335 return 0;
10336 dprintf("called for update_general_migration_checkpoint\n");
10337 break;
10338 case update_takeover: {
10339 struct imsm_update_takeover *u = (void *)update->buf;
10340 if (update->len < (int)sizeof(*u))
10341 return 0;
10342 if (u->direction == R0_TO_R10) {
10343 void **tail = (void **)&update->space_list;
10344 struct imsm_dev *dev = get_imsm_dev(super, u->subarray);
10345 struct imsm_map *map = get_imsm_map(dev, MAP_0);
10346 int num_members = map->num_members;
10347 void *space;
10348 int size, i;
10349 /* allocate memory for added disks */
10350 for (i = 0; i < num_members; i++) {
10351 size = sizeof(struct dl);
10352 space = xmalloc(size);
10353 *tail = space;
10354 tail = space;
10355 *tail = NULL;
10356 }
10357 /* allocate memory for new device */
10358 size = sizeof_imsm_dev(super->devlist->dev, 0) +
10359 (num_members * sizeof(__u32));
10360 space = xmalloc(size);
10361 *tail = space;
10362 tail = space;
10363 *tail = NULL;
10364 len = disks_to_mpb_size(num_members * 2);
10365 }
10366
10367 break;
10368 }
10369 case update_reshape_container_disks: {
10370 /* Every raid device in the container is about to
10371 * gain some more devices, and we will enter a
10372 * reconfiguration.
10373 * So each 'imsm_map' will be bigger, and the imsm_vol
10374 * will now hold 2 of them.
10375 * Thus we need new 'struct imsm_dev' allocations sized
10376 * as sizeof_imsm_dev but with more devices in both maps.
10377 */
10378 struct imsm_update_reshape *u = (void *)update->buf;
10379 struct intel_dev *dl;
10380 void **space_tail = (void**)&update->space_list;
10381
10382 if (update->len < (int)sizeof(*u))
10383 return 0;
10384
10385 dprintf("for update_reshape\n");
10386
10387 for (dl = super->devlist; dl; dl = dl->next) {
10388 int size = sizeof_imsm_dev(dl->dev, 1);
10389 void *s;
10390 if (u->new_raid_disks > u->old_raid_disks)
10391 size += sizeof(__u32)*2*
10392 (u->new_raid_disks - u->old_raid_disks);
10393 s = xmalloc(size);
10394 *space_tail = s;
10395 space_tail = s;
10396 *space_tail = NULL;
10397 }
10398
10399 len = disks_to_mpb_size(u->new_raid_disks);
10400 dprintf("New anchor length is %llu\n", (unsigned long long)len);
10401 break;
10402 }
10403 case update_reshape_migration: {
10404 /* for migration level 0->5 we need to add disks
10405 * so the same as for container operation we will copy
10406 * device to the bigger location.
10407 * in memory prepared device and new disk area are prepared
10408 * for usage in process update
10409 */
10410 struct imsm_update_reshape_migration *u = (void *)update->buf;
10411 struct intel_dev *id;
10412 void **space_tail = (void **)&update->space_list;
10413 int size;
10414 void *s;
10415 int current_level = -1;
10416
10417 if (update->len < (int)sizeof(*u))
10418 return 0;
10419
10420 dprintf("for update_reshape\n");
10421
10422 /* add space for bigger array in update
10423 */
10424 for (id = super->devlist; id; id = id->next) {
10425 if (id->index == (unsigned)u->subdev) {
10426 size = sizeof_imsm_dev(id->dev, 1);
10427 if (u->new_raid_disks > u->old_raid_disks)
10428 size += sizeof(__u32)*2*
10429 (u->new_raid_disks - u->old_raid_disks);
10430 s = xmalloc(size);
10431 *space_tail = s;
10432 space_tail = s;
10433 *space_tail = NULL;
10434 break;
10435 }
10436 }
10437 if (update->space_list == NULL)
10438 break;
10439
10440 /* add space for disk in update
10441 */
10442 size = sizeof(struct dl);
10443 s = xmalloc(size);
10444 *space_tail = s;
10445 space_tail = s;
10446 *space_tail = NULL;
10447
10448 /* add spare device to update
10449 */
10450 for (id = super->devlist ; id; id = id->next)
10451 if (id->index == (unsigned)u->subdev) {
10452 struct imsm_dev *dev;
10453 struct imsm_map *map;
10454
10455 dev = get_imsm_dev(super, u->subdev);
10456 map = get_imsm_map(dev, MAP_0);
10457 current_level = map->raid_level;
10458 break;
10459 }
10460 if (u->new_level == 5 && u->new_level != current_level) {
10461 struct mdinfo *spares;
10462
10463 spares = get_spares_for_grow(st);
10464 if (spares) {
10465 struct dl *dl;
10466 struct mdinfo *dev;
10467
10468 dev = spares->devs;
10469 if (dev) {
10470 u->new_disks[0] =
10471 makedev(dev->disk.major,
10472 dev->disk.minor);
10473 dl = get_disk_super(super,
10474 dev->disk.major,
10475 dev->disk.minor);
10476 dl->index = u->old_raid_disks;
10477 dev = dev->next;
10478 }
10479 sysfs_free(spares);
10480 }
10481 }
10482 len = disks_to_mpb_size(u->new_raid_disks);
10483 dprintf("New anchor length is %llu\n", (unsigned long long)len);
10484 break;
10485 }
10486 case update_size_change: {
10487 if (update->len < (int)sizeof(struct imsm_update_size_change))
10488 return 0;
10489 break;
10490 }
10491 case update_activate_spare: {
10492 if (update->len < (int)sizeof(struct imsm_update_activate_spare))
10493 return 0;
10494 break;
10495 }
10496 case update_create_array: {
10497 struct imsm_update_create_array *u = (void *) update->buf;
10498 struct intel_dev *dv;
10499 struct imsm_dev *dev = &u->dev;
10500 struct imsm_map *map = get_imsm_map(dev, MAP_0);
10501 struct dl *dl;
10502 struct disk_info *inf;
10503 int i;
10504 int activate = 0;
10505
10506 if (update->len < (int)sizeof(*u))
10507 return 0;
10508
10509 inf = get_disk_info(u);
10510 len = sizeof_imsm_dev(dev, 1);
10511 /* allocate a new super->devlist entry */
10512 dv = xmalloc(sizeof(*dv));
10513 dv->dev = xmalloc(len);
10514 update->space = dv;
10515
10516 /* count how many spares will be converted to members */
10517 for (i = 0; i < map->num_members; i++) {
10518 dl = serial_to_dl(inf[i].serial, super);
10519 if (!dl) {
10520 /* hmm maybe it failed?, nothing we can do about
10521 * it here
10522 */
10523 continue;
10524 }
10525 if (count_memberships(dl, super) == 0)
10526 activate++;
10527 }
10528 len += activate * sizeof(struct imsm_disk);
10529 break;
10530 }
10531 case update_kill_array: {
10532 if (update->len < (int)sizeof(struct imsm_update_kill_array))
10533 return 0;
10534 break;
10535 }
10536 case update_rename_array: {
10537 if (update->len < (int)sizeof(struct imsm_update_rename_array))
10538 return 0;
10539 break;
10540 }
10541 case update_add_remove_disk:
10542 /* no update->len needed */
10543 break;
10544 case update_prealloc_badblocks_mem:
10545 super->extra_space += sizeof(struct bbm_log) -
10546 get_imsm_bbm_log_size(super->bbm_log);
10547 break;
10548 case update_rwh_policy: {
10549 if (update->len < (int)sizeof(struct imsm_update_rwh_policy))
10550 return 0;
10551 break;
10552 }
10553 default:
10554 return 0;
10555 }
10556
10557 /* check if we need a larger metadata buffer */
10558 if (super->next_buf)
10559 buf_len = super->next_len;
10560 else
10561 buf_len = super->len;
10562
10563 if (__le32_to_cpu(mpb->mpb_size) + super->extra_space + len > buf_len) {
10564 /* ok we need a larger buf than what is currently allocated
10565 * if this allocation fails process_update will notice that
10566 * ->next_len is set and ->next_buf is NULL
10567 */
10568 buf_len = ROUND_UP(__le32_to_cpu(mpb->mpb_size) +
10569 super->extra_space + len, sector_size);
10570 if (super->next_buf)
10571 free(super->next_buf);
10572
10573 super->next_len = buf_len;
10574 if (posix_memalign(&super->next_buf, sector_size, buf_len) == 0)
10575 memset(super->next_buf, 0, buf_len);
10576 else
10577 super->next_buf = NULL;
10578 }
10579 return 1;
10580 }
10581
10582 /* must be called while manager is quiesced */
10583 static void imsm_delete(struct intel_super *super, struct dl **dlp, unsigned index)
10584 {
10585 struct imsm_super *mpb = super->anchor;
10586 struct dl *iter;
10587 struct imsm_dev *dev;
10588 struct imsm_map *map;
10589 unsigned int i, j, num_members;
10590 __u32 ord, ord_map0;
10591 struct bbm_log *log = super->bbm_log;
10592
10593 dprintf("deleting device[%d] from imsm_super\n", index);
10594
10595 /* shift all indexes down one */
10596 for (iter = super->disks; iter; iter = iter->next)
10597 if (iter->index > (int)index)
10598 iter->index--;
10599 for (iter = super->missing; iter; iter = iter->next)
10600 if (iter->index > (int)index)
10601 iter->index--;
10602
10603 for (i = 0; i < mpb->num_raid_devs; i++) {
10604 dev = get_imsm_dev(super, i);
10605 map = get_imsm_map(dev, MAP_0);
10606 num_members = map->num_members;
10607 for (j = 0; j < num_members; j++) {
10608 /* update ord entries being careful not to propagate
10609 * ord-flags to the first map
10610 */
10611 ord = get_imsm_ord_tbl_ent(dev, j, MAP_X);
10612 ord_map0 = get_imsm_ord_tbl_ent(dev, j, MAP_0);
10613
10614 if (ord_to_idx(ord) <= index)
10615 continue;
10616
10617 map = get_imsm_map(dev, MAP_0);
10618 set_imsm_ord_tbl_ent(map, j, ord_map0 - 1);
10619 map = get_imsm_map(dev, MAP_1);
10620 if (map)
10621 set_imsm_ord_tbl_ent(map, j, ord - 1);
10622 }
10623 }
10624
10625 for (i = 0; i < log->entry_count; i++) {
10626 struct bbm_log_entry *entry = &log->marked_block_entries[i];
10627
10628 if (entry->disk_ordinal <= index)
10629 continue;
10630 entry->disk_ordinal--;
10631 }
10632
10633 mpb->num_disks--;
10634 super->updates_pending++;
10635 if (*dlp) {
10636 struct dl *dl = *dlp;
10637
10638 *dlp = (*dlp)->next;
10639 __free_imsm_disk(dl, 1);
10640 }
10641 }
10642
10643 static int imsm_get_allowed_degradation(int level, int raid_disks,
10644 struct intel_super *super,
10645 struct imsm_dev *dev)
10646 {
10647 switch (level) {
10648 case 1:
10649 case 10:{
10650 int ret_val = 0;
10651 struct imsm_map *map;
10652 int i;
10653
10654 ret_val = raid_disks/2;
10655 /* check map if all disks pairs not failed
10656 * in both maps
10657 */
10658 map = get_imsm_map(dev, MAP_0);
10659 for (i = 0; i < ret_val; i++) {
10660 int degradation = 0;
10661 if (get_imsm_disk(super, i) == NULL)
10662 degradation++;
10663 if (get_imsm_disk(super, i + 1) == NULL)
10664 degradation++;
10665 if (degradation == 2)
10666 return 0;
10667 }
10668 map = get_imsm_map(dev, MAP_1);
10669 /* if there is no second map
10670 * result can be returned
10671 */
10672 if (map == NULL)
10673 return ret_val;
10674 /* check degradation in second map
10675 */
10676 for (i = 0; i < ret_val; i++) {
10677 int degradation = 0;
10678 if (get_imsm_disk(super, i) == NULL)
10679 degradation++;
10680 if (get_imsm_disk(super, i + 1) == NULL)
10681 degradation++;
10682 if (degradation == 2)
10683 return 0;
10684 }
10685 return ret_val;
10686 }
10687 case 5:
10688 return 1;
10689 case 6:
10690 return 2;
10691 default:
10692 return 0;
10693 }
10694 }
10695
10696 /*******************************************************************************
10697 * Function: validate_container_imsm
10698 * Description: This routine validates container after assemble,
10699 * eg. if devices in container are under the same controller.
10700 *
10701 * Parameters:
10702 * info : linked list with info about devices used in array
10703 * Returns:
10704 * 1 : HBA mismatch
10705 * 0 : Success
10706 ******************************************************************************/
10707 int validate_container_imsm(struct mdinfo *info)
10708 {
10709 if (check_no_platform())
10710 return 0;
10711
10712 struct sys_dev *idev;
10713 struct sys_dev *hba = NULL;
10714 struct sys_dev *intel_devices = find_intel_devices();
10715 char *dev_path = devt_to_devpath(makedev(info->disk.major,
10716 info->disk.minor), 1, NULL);
10717
10718 for (idev = intel_devices; idev; idev = idev->next) {
10719 if (dev_path && strstr(dev_path, idev->path)) {
10720 hba = idev;
10721 break;
10722 }
10723 }
10724 if (dev_path)
10725 free(dev_path);
10726
10727 if (!hba) {
10728 pr_err("WARNING - Cannot detect HBA for device %s!\n",
10729 devid2kname(makedev(info->disk.major, info->disk.minor)));
10730 return 1;
10731 }
10732
10733 const struct imsm_orom *orom = get_orom_by_device_id(hba->dev_id);
10734 struct mdinfo *dev;
10735
10736 for (dev = info->next; dev; dev = dev->next) {
10737 dev_path = devt_to_devpath(makedev(dev->disk.major,
10738 dev->disk.minor), 1, NULL);
10739
10740 struct sys_dev *hba2 = NULL;
10741 for (idev = intel_devices; idev; idev = idev->next) {
10742 if (dev_path && strstr(dev_path, idev->path)) {
10743 hba2 = idev;
10744 break;
10745 }
10746 }
10747 if (dev_path)
10748 free(dev_path);
10749
10750 const struct imsm_orom *orom2 = hba2 == NULL ? NULL :
10751 get_orom_by_device_id(hba2->dev_id);
10752
10753 if (hba2 && hba->type != hba2->type) {
10754 pr_err("WARNING - HBAs of devices do not match %s != %s\n",
10755 get_sys_dev_type(hba->type), get_sys_dev_type(hba2->type));
10756 return 1;
10757 }
10758
10759 if (orom != orom2) {
10760 pr_err("WARNING - IMSM container assembled with disks under different HBAs!\n"
10761 " This operation is not supported and can lead to data loss.\n");
10762 return 1;
10763 }
10764
10765 if (!orom) {
10766 pr_err("WARNING - IMSM container assembled with disks under HBAs without IMSM platform support!\n"
10767 " This operation is not supported and can lead to data loss.\n");
10768 return 1;
10769 }
10770 }
10771
10772 return 0;
10773 }
10774
10775 /*******************************************************************************
10776 * Function: imsm_record_badblock
10777 * Description: This routine stores new bad block record in BBM log
10778 *
10779 * Parameters:
10780 * a : array containing a bad block
10781 * slot : disk number containing a bad block
10782 * sector : bad block sector
10783 * length : bad block sectors range
10784 * Returns:
10785 * 1 : Success
10786 * 0 : Error
10787 ******************************************************************************/
10788 static int imsm_record_badblock(struct active_array *a, int slot,
10789 unsigned long long sector, int length)
10790 {
10791 struct intel_super *super = a->container->sb;
10792 int ord;
10793 int ret;
10794
10795 ord = imsm_disk_slot_to_ord(a, slot);
10796 if (ord < 0)
10797 return 0;
10798
10799 ret = record_new_badblock(super->bbm_log, ord_to_idx(ord), sector,
10800 length);
10801 if (ret)
10802 super->updates_pending++;
10803
10804 return ret;
10805 }
10806 /*******************************************************************************
10807 * Function: imsm_clear_badblock
10808 * Description: This routine clears bad block record from BBM log
10809 *
10810 * Parameters:
10811 * a : array containing a bad block
10812 * slot : disk number containing a bad block
10813 * sector : bad block sector
10814 * length : bad block sectors range
10815 * Returns:
10816 * 1 : Success
10817 * 0 : Error
10818 ******************************************************************************/
10819 static int imsm_clear_badblock(struct active_array *a, int slot,
10820 unsigned long long sector, int length)
10821 {
10822 struct intel_super *super = a->container->sb;
10823 int ord;
10824 int ret;
10825
10826 ord = imsm_disk_slot_to_ord(a, slot);
10827 if (ord < 0)
10828 return 0;
10829
10830 ret = clear_badblock(super->bbm_log, ord_to_idx(ord), sector, length);
10831 if (ret)
10832 super->updates_pending++;
10833
10834 return ret;
10835 }
10836 /*******************************************************************************
10837 * Function: imsm_get_badblocks
10838 * Description: This routine get list of bad blocks for an array
10839 *
10840 * Parameters:
10841 * a : array
10842 * slot : disk number
10843 * Returns:
10844 * bb : structure containing bad blocks
10845 * NULL : error
10846 ******************************************************************************/
10847 static struct md_bb *imsm_get_badblocks(struct active_array *a, int slot)
10848 {
10849 int inst = a->info.container_member;
10850 struct intel_super *super = a->container->sb;
10851 struct imsm_dev *dev = get_imsm_dev(super, inst);
10852 struct imsm_map *map = get_imsm_map(dev, MAP_0);
10853 int ord;
10854
10855 ord = imsm_disk_slot_to_ord(a, slot);
10856 if (ord < 0)
10857 return NULL;
10858
10859 get_volume_badblocks(super->bbm_log, ord_to_idx(ord), pba_of_lba0(map),
10860 per_dev_array_size(map), &super->bb);
10861
10862 return &super->bb;
10863 }
10864 /*******************************************************************************
10865 * Function: examine_badblocks_imsm
10866 * Description: Prints list of bad blocks on a disk to the standard output
10867 *
10868 * Parameters:
10869 * st : metadata handler
10870 * fd : open file descriptor for device
10871 * devname : device name
10872 * Returns:
10873 * 0 : Success
10874 * 1 : Error
10875 ******************************************************************************/
10876 static int examine_badblocks_imsm(struct supertype *st, int fd, char *devname)
10877 {
10878 struct intel_super *super = st->sb;
10879 struct bbm_log *log = super->bbm_log;
10880 struct dl *d = NULL;
10881 int any = 0;
10882
10883 for (d = super->disks; d ; d = d->next) {
10884 if (strcmp(d->devname, devname) == 0)
10885 break;
10886 }
10887
10888 if ((d == NULL) || (d->index < 0)) { /* serial mismatch probably */
10889 pr_err("%s doesn't appear to be part of a raid array\n",
10890 devname);
10891 return 1;
10892 }
10893
10894 if (log != NULL) {
10895 unsigned int i;
10896 struct bbm_log_entry *entry = &log->marked_block_entries[0];
10897
10898 for (i = 0; i < log->entry_count; i++) {
10899 if (entry[i].disk_ordinal == d->index) {
10900 unsigned long long sector = __le48_to_cpu(
10901 &entry[i].defective_block_start);
10902 int cnt = entry[i].marked_count + 1;
10903
10904 if (!any) {
10905 printf("Bad-blocks on %s:\n", devname);
10906 any = 1;
10907 }
10908
10909 printf("%20llu for %d sectors\n", sector, cnt);
10910 }
10911 }
10912 }
10913
10914 if (!any)
10915 printf("No bad-blocks list configured on %s\n", devname);
10916
10917 return 0;
10918 }
10919 /*******************************************************************************
10920 * Function: init_migr_record_imsm
10921 * Description: Function inits imsm migration record
10922 * Parameters:
10923 * super : imsm internal array info
10924 * dev : device under migration
10925 * info : general array info to find the smallest device
10926 * Returns:
10927 * none
10928 ******************************************************************************/
10929 void init_migr_record_imsm(struct supertype *st, struct imsm_dev *dev,
10930 struct mdinfo *info)
10931 {
10932 struct intel_super *super = st->sb;
10933 struct migr_record *migr_rec = super->migr_rec;
10934 int new_data_disks;
10935 unsigned long long dsize, dev_sectors;
10936 long long unsigned min_dev_sectors = -1LLU;
10937 struct imsm_map *map_dest = get_imsm_map(dev, MAP_0);
10938 struct imsm_map *map_src = get_imsm_map(dev, MAP_1);
10939 unsigned long long num_migr_units;
10940 unsigned long long array_blocks;
10941 struct dl *dl_disk = NULL;
10942
10943 memset(migr_rec, 0, sizeof(struct migr_record));
10944 migr_rec->family_num = __cpu_to_le32(super->anchor->family_num);
10945
10946 /* only ascending reshape supported now */
10947 migr_rec->ascending_migr = __cpu_to_le32(1);
10948
10949 migr_rec->dest_depth_per_unit = GEN_MIGR_AREA_SIZE /
10950 max(map_dest->blocks_per_strip, map_src->blocks_per_strip);
10951 migr_rec->dest_depth_per_unit *=
10952 max(map_dest->blocks_per_strip, map_src->blocks_per_strip);
10953 new_data_disks = imsm_num_data_members(map_dest);
10954 migr_rec->blocks_per_unit =
10955 __cpu_to_le32(migr_rec->dest_depth_per_unit * new_data_disks);
10956 migr_rec->dest_depth_per_unit =
10957 __cpu_to_le32(migr_rec->dest_depth_per_unit);
10958 array_blocks = info->component_size * new_data_disks;
10959 num_migr_units =
10960 array_blocks / __le32_to_cpu(migr_rec->blocks_per_unit);
10961
10962 if (array_blocks % __le32_to_cpu(migr_rec->blocks_per_unit))
10963 num_migr_units++;
10964 set_num_migr_units(migr_rec, num_migr_units);
10965
10966 migr_rec->post_migr_vol_cap = dev->size_low;
10967 migr_rec->post_migr_vol_cap_hi = dev->size_high;
10968
10969 /* Find the smallest dev */
10970 for (dl_disk = super->disks; dl_disk ; dl_disk = dl_disk->next) {
10971 /* ignore spares in container */
10972 if (dl_disk->index < 0)
10973 continue;
10974 get_dev_size(dl_disk->fd, NULL, &dsize);
10975 dev_sectors = dsize / 512;
10976 if (dev_sectors < min_dev_sectors)
10977 min_dev_sectors = dev_sectors;
10978 }
10979 set_migr_chkp_area_pba(migr_rec, min_dev_sectors -
10980 RAID_DISK_RESERVED_BLOCKS_IMSM_HI);
10981
10982 write_imsm_migr_rec(st);
10983
10984 return;
10985 }
10986
10987 /*******************************************************************************
10988 * Function: save_backup_imsm
10989 * Description: Function saves critical data stripes to Migration Copy Area
10990 * and updates the current migration unit status.
10991 * Use restore_stripes() to form a destination stripe,
10992 * and to write it to the Copy Area.
10993 * Parameters:
10994 * st : supertype information
10995 * dev : imsm device that backup is saved for
10996 * info : general array info
10997 * buf : input buffer
10998 * length : length of data to backup (blocks_per_unit)
10999 * Returns:
11000 * 0 : success
11001 *, -1 : fail
11002 ******************************************************************************/
11003 int save_backup_imsm(struct supertype *st,
11004 struct imsm_dev *dev,
11005 struct mdinfo *info,
11006 void *buf,
11007 int length)
11008 {
11009 int rv = -1;
11010 struct intel_super *super = st->sb;
11011 int i;
11012 struct imsm_map *map_dest = get_imsm_map(dev, MAP_0);
11013 int new_disks = map_dest->num_members;
11014 int dest_layout = 0;
11015 int dest_chunk, targets[new_disks];
11016 unsigned long long start, target_offsets[new_disks];
11017 int data_disks = imsm_num_data_members(map_dest);
11018
11019 for (i = 0; i < new_disks; i++) {
11020 struct dl *dl_disk = get_imsm_dl_disk(super, i);
11021 if (dl_disk && is_fd_valid(dl_disk->fd))
11022 targets[i] = dl_disk->fd;
11023 else
11024 goto abort;
11025 }
11026
11027 start = info->reshape_progress * 512;
11028 for (i = 0; i < new_disks; i++) {
11029 target_offsets[i] = migr_chkp_area_pba(super->migr_rec) * 512;
11030 /* move back copy area adderss, it will be moved forward
11031 * in restore_stripes() using start input variable
11032 */
11033 target_offsets[i] -= start/data_disks;
11034 }
11035
11036 dest_layout = imsm_level_to_layout(map_dest->raid_level);
11037 dest_chunk = __le16_to_cpu(map_dest->blocks_per_strip) * 512;
11038
11039 if (restore_stripes(targets, /* list of dest devices */
11040 target_offsets, /* migration record offsets */
11041 new_disks,
11042 dest_chunk,
11043 map_dest->raid_level,
11044 dest_layout,
11045 -1, /* source backup file descriptor */
11046 0, /* input buf offset
11047 * always 0 buf is already offseted */
11048 start,
11049 length,
11050 buf) != 0) {
11051 pr_err("Error restoring stripes\n");
11052 goto abort;
11053 }
11054
11055 rv = 0;
11056
11057 abort:
11058 return rv;
11059 }
11060
11061 /*******************************************************************************
11062 * Function: save_checkpoint_imsm
11063 * Description: Function called for current unit status update
11064 * in the migration record. It writes it to disk.
11065 * Parameters:
11066 * super : imsm internal array info
11067 * info : general array info
11068 * Returns:
11069 * 0: success
11070 * 1: failure
11071 * 2: failure, means no valid migration record
11072 * / no general migration in progress /
11073 ******************************************************************************/
11074 int save_checkpoint_imsm(struct supertype *st, struct mdinfo *info, int state)
11075 {
11076 struct intel_super *super = st->sb;
11077 unsigned long long blocks_per_unit;
11078 unsigned long long curr_migr_unit;
11079
11080 if (load_imsm_migr_rec(super) != 0) {
11081 dprintf("imsm: ERROR: Cannot read migration record for checkpoint save.\n");
11082 return 1;
11083 }
11084
11085 blocks_per_unit = __le32_to_cpu(super->migr_rec->blocks_per_unit);
11086 if (blocks_per_unit == 0) {
11087 dprintf("imsm: no migration in progress.\n");
11088 return 2;
11089 }
11090 curr_migr_unit = info->reshape_progress / blocks_per_unit;
11091 /* check if array is alligned to copy area
11092 * if it is not alligned, add one to current migration unit value
11093 * this can happend on array reshape finish only
11094 */
11095 if (info->reshape_progress % blocks_per_unit)
11096 curr_migr_unit++;
11097
11098 set_current_migr_unit(super->migr_rec, curr_migr_unit);
11099 super->migr_rec->rec_status = __cpu_to_le32(state);
11100 set_migr_dest_1st_member_lba(super->migr_rec,
11101 super->migr_rec->dest_depth_per_unit * curr_migr_unit);
11102
11103 if (write_imsm_migr_rec(st) < 0) {
11104 dprintf("imsm: Cannot write migration record outside backup area\n");
11105 return 1;
11106 }
11107
11108 return 0;
11109 }
11110
11111 /*******************************************************************************
11112 * Function: recover_backup_imsm
11113 * Description: Function recovers critical data from the Migration Copy Area
11114 * while assembling an array.
11115 * Parameters:
11116 * super : imsm internal array info
11117 * info : general array info
11118 * Returns:
11119 * 0 : success (or there is no data to recover)
11120 * 1 : fail
11121 ******************************************************************************/
11122 int recover_backup_imsm(struct supertype *st, struct mdinfo *info)
11123 {
11124 struct intel_super *super = st->sb;
11125 struct migr_record *migr_rec = super->migr_rec;
11126 struct imsm_map *map_dest;
11127 struct intel_dev *id = NULL;
11128 unsigned long long read_offset;
11129 unsigned long long write_offset;
11130 unsigned unit_len;
11131 int new_disks, err;
11132 char *buf = NULL;
11133 int retval = 1;
11134 unsigned int sector_size = super->sector_size;
11135 unsigned long long curr_migr_unit = current_migr_unit(migr_rec);
11136 unsigned long long num_migr_units = get_num_migr_units(migr_rec);
11137 char buffer[SYSFS_MAX_BUF_SIZE];
11138 int skipped_disks = 0;
11139 struct dl *dl_disk;
11140
11141 err = sysfs_get_str(info, NULL, "array_state", (char *)buffer, sizeof(buffer));
11142 if (err < 1)
11143 return 1;
11144
11145 /* recover data only during assemblation */
11146 if (strncmp(buffer, "inactive", 8) != 0)
11147 return 0;
11148 /* no data to recover */
11149 if (__le32_to_cpu(migr_rec->rec_status) == UNIT_SRC_NORMAL)
11150 return 0;
11151 if (curr_migr_unit >= num_migr_units)
11152 return 1;
11153
11154 /* find device during reshape */
11155 for (id = super->devlist; id; id = id->next)
11156 if (is_gen_migration(id->dev))
11157 break;
11158 if (id == NULL)
11159 return 1;
11160
11161 map_dest = get_imsm_map(id->dev, MAP_0);
11162 new_disks = map_dest->num_members;
11163
11164 read_offset = migr_chkp_area_pba(migr_rec) * 512;
11165
11166 write_offset = (migr_dest_1st_member_lba(migr_rec) +
11167 pba_of_lba0(map_dest)) * 512;
11168
11169 unit_len = __le32_to_cpu(migr_rec->dest_depth_per_unit) * 512;
11170 if (posix_memalign((void **)&buf, sector_size, unit_len) != 0)
11171 goto abort;
11172
11173 for (dl_disk = super->disks; dl_disk; dl_disk = dl_disk->next) {
11174 if (dl_disk->index < 0)
11175 continue;
11176
11177 if (!is_fd_valid(dl_disk->fd)) {
11178 skipped_disks++;
11179 continue;
11180 }
11181 if (lseek64(dl_disk->fd, read_offset, SEEK_SET) < 0) {
11182 pr_err("Cannot seek to block: %s\n",
11183 strerror(errno));
11184 skipped_disks++;
11185 continue;
11186 }
11187 if (read(dl_disk->fd, buf, unit_len) != (ssize_t)unit_len) {
11188 pr_err("Cannot read copy area block: %s\n",
11189 strerror(errno));
11190 skipped_disks++;
11191 continue;
11192 }
11193 if (lseek64(dl_disk->fd, write_offset, SEEK_SET) < 0) {
11194 pr_err("Cannot seek to block: %s\n",
11195 strerror(errno));
11196 skipped_disks++;
11197 continue;
11198 }
11199 if (write(dl_disk->fd, buf, unit_len) != (ssize_t)unit_len) {
11200 pr_err("Cannot restore block: %s\n",
11201 strerror(errno));
11202 skipped_disks++;
11203 continue;
11204 }
11205 }
11206
11207 if (skipped_disks > imsm_get_allowed_degradation(info->new_level,
11208 new_disks,
11209 super,
11210 id->dev)) {
11211 pr_err("Cannot restore data from backup. Too many failed disks\n");
11212 goto abort;
11213 }
11214
11215 if (save_checkpoint_imsm(st, info, UNIT_SRC_NORMAL)) {
11216 /* ignore error == 2, this can mean end of reshape here
11217 */
11218 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL) during restart\n");
11219 } else
11220 retval = 0;
11221
11222 abort:
11223 free(buf);
11224 return retval;
11225 }
11226
11227 /**
11228 * test_and_add_drive_controller_policy_imsm() - add disk controller to policies list.
11229 * @type: Policy type to search on list.
11230 * @pols: List of currently recorded policies.
11231 * @disk_fd: File descriptor of the device to check.
11232 * @hba: The hba disk is attached, could be NULL if verification is disabled.
11233 * @verbose: verbose flag.
11234 *
11235 * IMSM cares about drive physical placement. If @hba is not set, it adds unknown policy.
11236 * If there is no controller policy on pols we are free to add first one. If there is a policy then,
11237 * new must be the same - no controller mixing allowed.
11238 */
11239 static mdadm_status_t
11240 test_and_add_drive_controller_policy_imsm(const char * const type, dev_policy_t **pols, int disk_fd,
11241 struct sys_dev *hba, const int verbose)
11242 {
11243 const char *controller_policy = get_sys_dev_type(SYS_DEV_UNKNOWN);
11244 struct dev_policy *pol = pol_find(*pols, (char *)type);
11245 char devname[MAX_RAID_SERIAL_LEN];
11246
11247 if (hba)
11248 controller_policy = get_sys_dev_type(hba->type);
11249
11250 if (!pol) {
11251 pol_add(pols, (char *)type, (char *)controller_policy, "imsm");
11252 return MDADM_STATUS_SUCCESS;
11253 }
11254
11255 if (strcmp(pol->value, controller_policy) == 0)
11256 return MDADM_STATUS_SUCCESS;
11257
11258 fd2devname(disk_fd, devname);
11259 pr_vrb("Intel(R) raid controller \"%s\" found for %s, but \"%s\" was detected earlier\n",
11260 controller_policy, devname, pol->value);
11261 pr_vrb("Disks under different controllers cannot be used, aborting\n");
11262
11263 return MDADM_STATUS_ERROR;
11264 }
11265
11266 /**
11267 * test_and_add_drive_encryption_policy_imsm() - add disk encryption to policies list.
11268 * @type: policy type to search in the list.
11269 * @pols: list of currently recorded policies.
11270 * @disk_fd: file descriptor of the device to check.
11271 * @hba: The hba to which the drive is attached, could be NULL if verification is disabled.
11272 * @verbose: verbose flag.
11273 *
11274 * IMSM cares about drive encryption state. It is not allowed to mix disks with different
11275 * encryption state within one md device.
11276 * If there is no encryption policy on pols we are free to add first one.
11277 * If there is a policy then, new must be the same.
11278 */
11279 static mdadm_status_t
11280 test_and_add_drive_encryption_policy_imsm(const char * const type, dev_policy_t **pols, int disk_fd,
11281 struct sys_dev *hba, const int verbose)
11282 {
11283 struct dev_policy *expected_policy = pol_find(*pols, (char *)type);
11284 struct encryption_information information = {0};
11285 char *encryption_state = "Unknown";
11286 int status = MDADM_STATUS_SUCCESS;
11287 bool encryption_checked = true;
11288 char devname[PATH_MAX];
11289
11290 if (!hba)
11291 goto check_policy;
11292
11293 switch (hba->type) {
11294 case SYS_DEV_NVME:
11295 case SYS_DEV_VMD:
11296 status = get_nvme_opal_encryption_information(disk_fd, &information, verbose);
11297 break;
11298 case SYS_DEV_SATA:
11299 case SYS_DEV_SATA_VMD:
11300 status = get_ata_encryption_information(disk_fd, &information, verbose);
11301 break;
11302 default:
11303 encryption_checked = false;
11304 }
11305
11306 if (status) {
11307 fd2devname(disk_fd, devname);
11308 pr_vrb("Failed to read encryption information of device %s\n", devname);
11309 return MDADM_STATUS_ERROR;
11310 }
11311
11312 if (encryption_checked) {
11313 if (information.status == ENC_STATUS_LOCKED) {
11314 fd2devname(disk_fd, devname);
11315 pr_vrb("Device %s is in Locked state, cannot use. Aborting.\n", devname);
11316 return MDADM_STATUS_ERROR;
11317 }
11318 encryption_state = (char *)get_encryption_status_string(information.status);
11319 }
11320
11321 check_policy:
11322 if (expected_policy) {
11323 if (strcmp(expected_policy->value, encryption_state) == 0)
11324 return MDADM_STATUS_SUCCESS;
11325
11326 fd2devname(disk_fd, devname);
11327 pr_vrb("Encryption status \"%s\" detected for disk %s, but \"%s\" status was detected eariler.\n",
11328 encryption_state, devname, expected_policy->value);
11329 pr_vrb("Disks with different encryption status cannot be used.\n");
11330 return MDADM_STATUS_ERROR;
11331 }
11332
11333 pol_add(pols, (char *)type, encryption_state, "imsm");
11334
11335 return MDADM_STATUS_SUCCESS;
11336 }
11337
11338 struct imsm_drive_policy {
11339 char *type;
11340 mdadm_status_t (*test_and_add_drive_policy)(const char * const type,
11341 struct dev_policy **pols, int disk_fd,
11342 struct sys_dev *hba, const int verbose);
11343 };
11344
11345 struct imsm_drive_policy imsm_policies[] = {
11346 {"controller", test_and_add_drive_controller_policy_imsm},
11347 {"encryption", test_and_add_drive_encryption_policy_imsm}
11348 };
11349
11350 mdadm_status_t test_and_add_drive_policies_imsm(struct dev_policy **pols, int disk_fd,
11351 const int verbose)
11352 {
11353 struct imsm_drive_policy *imsm_pol;
11354 struct sys_dev *hba = NULL;
11355 char path[PATH_MAX];
11356 mdadm_status_t ret;
11357 unsigned int i;
11358
11359 /* If imsm platform verification is disabled, do not search for hba. */
11360 if (check_no_platform() != 1) {
11361 if (!diskfd_to_devpath(disk_fd, 1, path)) {
11362 pr_vrb("IMSM: Failed to retrieve device path by file descriptor.\n");
11363 return MDADM_STATUS_ERROR;
11364 }
11365
11366 hba = find_disk_attached_hba(disk_fd, path);
11367 if (!hba) {
11368 pr_vrb("IMSM: Failed to find hba for %s\n", path);
11369 return MDADM_STATUS_ERROR;
11370 }
11371 }
11372
11373 for (i = 0; i < ARRAY_SIZE(imsm_policies); i++) {
11374 imsm_pol = &imsm_policies[i];
11375
11376 ret = imsm_pol->test_and_add_drive_policy(imsm_pol->type, pols, disk_fd, hba,
11377 verbose);
11378 if (ret != MDADM_STATUS_SUCCESS)
11379 /* Inherit error code */
11380 return ret;
11381 }
11382
11383 return MDADM_STATUS_SUCCESS;
11384 }
11385
11386 /**
11387 * get_spare_criteria_imsm() - set spare criteria.
11388 * @st: supertype.
11389 * @mddev_path: path to md device devnode, it must be container.
11390 * @c: spare_criteria struct to fill, not NULL.
11391 *
11392 * If superblock is not loaded, use mddev_path to load_container. It must be given in this case.
11393 * Filles size and sector size accordingly to superblock.
11394 */
11395 mdadm_status_t get_spare_criteria_imsm(struct supertype *st, char *mddev_path,
11396 struct spare_criteria *c)
11397 {
11398 mdadm_status_t ret = MDADM_STATUS_ERROR;
11399 bool free_superblock = false;
11400 unsigned long long size = 0;
11401 struct intel_super *super;
11402 struct extent *e;
11403 struct dl *dl;
11404 int i;
11405
11406 /* If no superblock and no mddev_path, we cannot load superblock. */
11407 assert(st->sb || mddev_path);
11408
11409 if (mddev_path) {
11410 int fd = open(mddev_path, O_RDONLY);
11411 mdadm_status_t rv;
11412
11413 if (!is_fd_valid(fd))
11414 return MDADM_STATUS_ERROR;
11415
11416 if (!st->sb) {
11417 if (load_container_imsm(st, fd, st->devnm)) {
11418 close(fd);
11419 return MDADM_STATUS_ERROR;
11420 }
11421 free_superblock = true;
11422 }
11423
11424 rv = mddev_test_and_add_drive_policies(st, &c->pols, fd, 0);
11425 close(fd);
11426
11427 if (rv != MDADM_STATUS_SUCCESS)
11428 goto out;
11429 }
11430
11431 super = st->sb;
11432
11433 /* find first active disk in array */
11434 dl = super->disks;
11435 while (dl && (is_failed(&dl->disk) || dl->index == -1))
11436 dl = dl->next;
11437
11438 if (!dl)
11439 goto out;
11440
11441 /* find last lba used by subarrays */
11442 e = get_extents(super, dl, 0);
11443 if (!e)
11444 goto out;
11445
11446 for (i = 0; e[i].size; i++)
11447 continue;
11448 if (i > 0)
11449 size = e[i - 1].start + e[i - 1].size;
11450 free(e);
11451
11452 /* add the amount of space needed for metadata */
11453 size += imsm_min_reserved_sectors(super);
11454
11455 c->min_size = size * 512;
11456 c->sector_size = super->sector_size;
11457 c->criteria_set = true;
11458 ret = MDADM_STATUS_SUCCESS;
11459
11460 out:
11461 if (free_superblock)
11462 free_super_imsm(st);
11463
11464 if (ret != MDADM_STATUS_SUCCESS)
11465 c->criteria_set = false;
11466
11467 return ret;
11468 }
11469
11470 static char *imsm_find_array_devnm_by_subdev(int subdev, char *container)
11471 {
11472 static char devnm[32];
11473 char subdev_name[20];
11474 struct mdstat_ent *mdstat;
11475
11476 sprintf(subdev_name, "%d", subdev);
11477 mdstat = mdstat_by_subdev(subdev_name, container);
11478 if (!mdstat)
11479 return NULL;
11480
11481 strcpy(devnm, mdstat->devnm);
11482 free_mdstat(mdstat);
11483 return devnm;
11484 }
11485
11486 static int imsm_reshape_is_allowed_on_container(struct supertype *st,
11487 struct geo_params *geo,
11488 int *old_raid_disks,
11489 int direction)
11490 {
11491 /* currently we only support increasing the number of devices
11492 * for a container. This increases the number of device for each
11493 * member array. They must all be RAID0 or RAID5.
11494 */
11495 int ret_val = 0;
11496 struct mdinfo *info, *member;
11497 int devices_that_can_grow = 0;
11498
11499 dprintf("imsm: imsm_reshape_is_allowed_on_container(ENTER): st->devnm = (%s)\n", st->devnm);
11500
11501 if (geo->size > 0 ||
11502 geo->level != UnSet ||
11503 geo->layout != UnSet ||
11504 geo->chunksize != 0 ||
11505 geo->raid_disks == UnSet) {
11506 dprintf("imsm: Container operation is allowed for raid disks number change only.\n");
11507 return ret_val;
11508 }
11509
11510 if (direction == ROLLBACK_METADATA_CHANGES) {
11511 dprintf("imsm: Metadata changes rollback is not supported for container operation.\n");
11512 return ret_val;
11513 }
11514
11515 info = container_content_imsm(st, NULL);
11516 for (member = info; member; member = member->next) {
11517 char *result;
11518
11519 dprintf("imsm: checking device_num: %i\n",
11520 member->container_member);
11521
11522 if (geo->raid_disks <= member->array.raid_disks) {
11523 /* we work on container for Online Capacity Expansion
11524 * only so raid_disks has to grow
11525 */
11526 dprintf("imsm: for container operation raid disks increase is required\n");
11527 break;
11528 }
11529
11530 if (info->array.level != 0 && info->array.level != 5) {
11531 /* we cannot use this container with other raid level
11532 */
11533 dprintf("imsm: for container operation wrong raid level (%i) detected\n",
11534 info->array.level);
11535 break;
11536 } else {
11537 /* check for platform support
11538 * for this raid level configuration
11539 */
11540 struct intel_super *super = st->sb;
11541 if (!is_raid_level_supported(super->orom,
11542 member->array.level,
11543 geo->raid_disks)) {
11544 dprintf("platform does not support raid%d with %d disk%s\n",
11545 info->array.level,
11546 geo->raid_disks,
11547 geo->raid_disks > 1 ? "s" : "");
11548 break;
11549 }
11550 /* check if component size is aligned to chunk size
11551 */
11552 if (info->component_size %
11553 (info->array.chunk_size/512)) {
11554 dprintf("Component size is not aligned to chunk size\n");
11555 break;
11556 }
11557 }
11558
11559 if (*old_raid_disks &&
11560 info->array.raid_disks != *old_raid_disks)
11561 break;
11562 *old_raid_disks = info->array.raid_disks;
11563
11564 /* All raid5 and raid0 volumes in container
11565 * have to be ready for Online Capacity Expansion
11566 * so they need to be assembled. We have already
11567 * checked that no recovery etc is happening.
11568 */
11569 result = imsm_find_array_devnm_by_subdev(member->container_member,
11570 st->container_devnm);
11571 if (result == NULL) {
11572 dprintf("imsm: cannot find array\n");
11573 break;
11574 }
11575 devices_that_can_grow++;
11576 }
11577 sysfs_free(info);
11578 if (!member && devices_that_can_grow)
11579 ret_val = 1;
11580
11581 if (ret_val)
11582 dprintf("Container operation allowed\n");
11583 else
11584 dprintf("Error: %i\n", ret_val);
11585
11586 return ret_val;
11587 }
11588
11589 /* Function: get_spares_for_grow
11590 * Description: Allocates memory and creates list of spare devices
11591 * avaliable in container. Checks if spare drive size is acceptable.
11592 * Parameters: Pointer to the supertype structure
11593 * Returns: Pointer to the list of spare devices (mdinfo structure) on success,
11594 * NULL if fail
11595 */
11596 static struct mdinfo *get_spares_for_grow(struct supertype *st)
11597 {
11598 struct spare_criteria sc = {0};
11599 struct mdinfo *spares;
11600
11601 get_spare_criteria_imsm(st, NULL, &sc);
11602 spares = container_choose_spares(st, &sc, NULL, NULL, NULL, 0);
11603
11604 dev_policy_free(sc.pols);
11605
11606 return spares;
11607 }
11608
11609 /******************************************************************************
11610 * function: imsm_create_metadata_update_for_reshape
11611 * Function creates update for whole IMSM container.
11612 *
11613 ******************************************************************************/
11614 static int imsm_create_metadata_update_for_reshape(
11615 struct supertype *st,
11616 struct geo_params *geo,
11617 int old_raid_disks,
11618 struct imsm_update_reshape **updatep)
11619 {
11620 struct intel_super *super = st->sb;
11621 struct imsm_super *mpb = super->anchor;
11622 int update_memory_size;
11623 struct imsm_update_reshape *u;
11624 struct mdinfo *spares;
11625 int i;
11626 int delta_disks;
11627 struct mdinfo *dev;
11628
11629 dprintf("(enter) raid_disks = %i\n", geo->raid_disks);
11630
11631 delta_disks = geo->raid_disks - old_raid_disks;
11632
11633 /* size of all update data without anchor */
11634 update_memory_size = sizeof(struct imsm_update_reshape);
11635
11636 /* now add space for spare disks that we need to add. */
11637 update_memory_size += sizeof(u->new_disks[0]) * (delta_disks - 1);
11638
11639 u = xcalloc(1, update_memory_size);
11640 u->type = update_reshape_container_disks;
11641 u->old_raid_disks = old_raid_disks;
11642 u->new_raid_disks = geo->raid_disks;
11643
11644 /* now get spare disks list
11645 */
11646 spares = get_spares_for_grow(st);
11647
11648 if (spares == NULL || delta_disks > spares->array.spare_disks) {
11649 pr_err("imsm: ERROR: Cannot get spare devices for %s.\n", geo->dev_name);
11650 i = -1;
11651 goto abort;
11652 }
11653
11654 /* we have got spares
11655 * update disk list in imsm_disk list table in anchor
11656 */
11657 dprintf("imsm: %i spares are available.\n\n",
11658 spares->array.spare_disks);
11659
11660 dev = spares->devs;
11661 for (i = 0; i < delta_disks; i++) {
11662 struct dl *dl;
11663
11664 if (dev == NULL)
11665 break;
11666 u->new_disks[i] = makedev(dev->disk.major,
11667 dev->disk.minor);
11668 dl = get_disk_super(super, dev->disk.major, dev->disk.minor);
11669 dl->index = mpb->num_disks;
11670 mpb->num_disks++;
11671 dev = dev->next;
11672 }
11673
11674 abort:
11675 /* free spares
11676 */
11677 sysfs_free(spares);
11678
11679 dprintf("imsm: reshape update preparation :");
11680 if (i == delta_disks) {
11681 dprintf_cont(" OK\n");
11682 *updatep = u;
11683 return update_memory_size;
11684 }
11685 free(u);
11686 dprintf_cont(" Error\n");
11687
11688 return 0;
11689 }
11690
11691 /******************************************************************************
11692 * function: imsm_create_metadata_update_for_size_change()
11693 * Creates update for IMSM array for array size change.
11694 *
11695 ******************************************************************************/
11696 static int imsm_create_metadata_update_for_size_change(
11697 struct supertype *st,
11698 struct geo_params *geo,
11699 struct imsm_update_size_change **updatep)
11700 {
11701 struct intel_super *super = st->sb;
11702 int update_memory_size;
11703 struct imsm_update_size_change *u;
11704
11705 dprintf("(enter) New size = %llu\n", geo->size);
11706
11707 /* size of all update data without anchor */
11708 update_memory_size = sizeof(struct imsm_update_size_change);
11709
11710 u = xcalloc(1, update_memory_size);
11711 u->type = update_size_change;
11712 u->subdev = super->current_vol;
11713 u->new_size = geo->size;
11714
11715 dprintf("imsm: reshape update preparation : OK\n");
11716 *updatep = u;
11717
11718 return update_memory_size;
11719 }
11720
11721 /******************************************************************************
11722 * function: imsm_create_metadata_update_for_migration()
11723 * Creates update for IMSM array.
11724 *
11725 ******************************************************************************/
11726 static int imsm_create_metadata_update_for_migration(
11727 struct supertype *st,
11728 struct geo_params *geo,
11729 struct imsm_update_reshape_migration **updatep)
11730 {
11731 struct intel_super *super = st->sb;
11732 int update_memory_size;
11733 int current_chunk_size;
11734 struct imsm_update_reshape_migration *u;
11735 struct imsm_dev *dev = get_imsm_dev(super, super->current_vol);
11736 struct imsm_map *map = get_imsm_map(dev, MAP_0);
11737 int previous_level = -1;
11738
11739 dprintf("(enter) New Level = %i\n", geo->level);
11740
11741 /* size of all update data without anchor */
11742 update_memory_size = sizeof(struct imsm_update_reshape_migration);
11743
11744 u = xcalloc(1, update_memory_size);
11745 u->type = update_reshape_migration;
11746 u->subdev = super->current_vol;
11747 u->new_level = geo->level;
11748 u->new_layout = geo->layout;
11749 u->new_raid_disks = u->old_raid_disks = geo->raid_disks;
11750 u->new_disks[0] = -1;
11751 u->new_chunksize = -1;
11752
11753 current_chunk_size = __le16_to_cpu(map->blocks_per_strip) / 2;
11754
11755 if (geo->chunksize != current_chunk_size) {
11756 u->new_chunksize = geo->chunksize / 1024;
11757 dprintf("imsm: chunk size change from %i to %i\n",
11758 current_chunk_size, u->new_chunksize);
11759 }
11760 previous_level = map->raid_level;
11761
11762 if (geo->level == 5 && previous_level == 0) {
11763 struct mdinfo *spares = NULL;
11764
11765 u->new_raid_disks++;
11766 spares = get_spares_for_grow(st);
11767 if (spares == NULL || spares->array.spare_disks < 1) {
11768 free(u);
11769 sysfs_free(spares);
11770 update_memory_size = 0;
11771 pr_err("cannot get spare device for requested migration\n");
11772 return 0;
11773 }
11774 sysfs_free(spares);
11775 }
11776 dprintf("imsm: reshape update preparation : OK\n");
11777 *updatep = u;
11778
11779 return update_memory_size;
11780 }
11781
11782 static void imsm_update_metadata_locally(struct supertype *st,
11783 void *buf, int len)
11784 {
11785 struct metadata_update mu;
11786
11787 mu.buf = buf;
11788 mu.len = len;
11789 mu.space = NULL;
11790 mu.space_list = NULL;
11791 mu.next = NULL;
11792 if (imsm_prepare_update(st, &mu))
11793 imsm_process_update(st, &mu);
11794
11795 while (mu.space_list) {
11796 void **space = mu.space_list;
11797 mu.space_list = *space;
11798 free(space);
11799 }
11800 }
11801
11802 /**
11803 * imsm_analyze_expand() - check expand properties and calculate new size.
11804 * @st: imsm supertype.
11805 * @geo: new geometry params.
11806 * @array: array info.
11807 * @direction: reshape direction.
11808 *
11809 * Obtain free space after the &array and verify if expand to requested size is
11810 * possible. If geo->size is set to %MAX_SIZE, assume that max free size is
11811 * requested.
11812 *
11813 * Return:
11814 * On success %IMSM_STATUS_OK is returned, geo->size and geo->raid_disks are
11815 * updated.
11816 * On error, %IMSM_STATUS_ERROR is returned.
11817 */
11818 static imsm_status_t imsm_analyze_expand(struct supertype *st,
11819 struct geo_params *geo,
11820 struct mdinfo *array,
11821 int direction)
11822 {
11823 struct intel_super *super = st->sb;
11824 struct imsm_dev *dev = get_imsm_dev(super, super->current_vol);
11825 struct imsm_map *map = get_imsm_map(dev, MAP_0);
11826 int data_disks = imsm_num_data_members(map);
11827
11828 unsigned long long current_size;
11829 unsigned long long free_size;
11830 unsigned long long new_size;
11831 unsigned long long max_size;
11832
11833 const int chunk_kib = geo->chunksize / 1024;
11834 imsm_status_t rv;
11835
11836 if (direction == ROLLBACK_METADATA_CHANGES) {
11837 /**
11838 * Accept size for rollback only.
11839 */
11840 new_size = geo->size * 2;
11841 goto success;
11842 }
11843
11844 if (data_disks == 0) {
11845 pr_err("imsm: Cannot retrieve data disks.\n");
11846 return IMSM_STATUS_ERROR;
11847 }
11848 current_size = array->custom_array_size / data_disks;
11849
11850 rv = imsm_get_free_size(super, dev->vol.map->num_members, 0, chunk_kib, &free_size, true);
11851 if (rv != IMSM_STATUS_OK) {
11852 pr_err("imsm: Cannot find free space for expand.\n");
11853 return IMSM_STATUS_ERROR;
11854 }
11855 max_size = round_member_size_to_mb(free_size + current_size);
11856
11857 if (geo->size == MAX_SIZE)
11858 new_size = max_size;
11859 else
11860 new_size = round_member_size_to_mb(geo->size * 2);
11861
11862 if (new_size == 0) {
11863 pr_err("imsm: Rounded requested size is 0.\n");
11864 return IMSM_STATUS_ERROR;
11865 }
11866
11867 if (new_size > max_size) {
11868 pr_err("imsm: Rounded requested size (%llu) is larger than free space available (%llu).\n",
11869 new_size, max_size);
11870 return IMSM_STATUS_ERROR;
11871 }
11872
11873 if (new_size == current_size) {
11874 pr_err("imsm: Rounded requested size (%llu) is same as current size (%llu).\n",
11875 new_size, current_size);
11876 return IMSM_STATUS_ERROR;
11877 }
11878
11879 if (new_size < current_size) {
11880 pr_err("imsm: Size reduction is not supported, rounded requested size (%llu) is smaller than current (%llu).\n",
11881 new_size, current_size);
11882 return IMSM_STATUS_ERROR;
11883 }
11884
11885 success:
11886 dprintf("imsm: New size per member is %llu.\n", new_size);
11887 geo->size = data_disks * new_size;
11888 geo->raid_disks = dev->vol.map->num_members;
11889 return IMSM_STATUS_OK;
11890 }
11891
11892 /***************************************************************************
11893 * Function: imsm_analyze_change
11894 * Description: Function analyze change for single volume
11895 * and validate if transition is supported
11896 * Parameters: Geometry parameters, supertype structure,
11897 * metadata change direction (apply/rollback)
11898 * Returns: Operation type code on success, -1 if fail
11899 ****************************************************************************/
11900 enum imsm_reshape_type imsm_analyze_change(struct supertype *st,
11901 struct geo_params *geo,
11902 int direction, struct context *c)
11903 {
11904 struct mdinfo info;
11905 int change = -1;
11906 int check_devs = 0;
11907 int chunk;
11908 /* imsm compatible layout value for array geometry verification */
11909 int imsm_layout = -1;
11910 int raid_disks = geo->raid_disks;
11911 imsm_status_t rv;
11912
11913 getinfo_super_imsm_volume(st, &info, NULL);
11914 if (geo->level != info.array.level && geo->level >= IMSM_T_RAID0 &&
11915 geo->level != UnSet) {
11916 switch (info.array.level) {
11917 case IMSM_T_RAID0:
11918 if (geo->level == IMSM_T_RAID5) {
11919 change = CH_MIGRATION;
11920 if (geo->layout != ALGORITHM_LEFT_ASYMMETRIC) {
11921 pr_err("Error. Requested Layout not supported (left-asymmetric layout is supported only)!\n");
11922 change = -1;
11923 goto analyse_change_exit;
11924 }
11925 imsm_layout = geo->layout;
11926 check_devs = 1;
11927 raid_disks += 1; /* parity disk added */
11928 } else if (geo->level == IMSM_T_RAID10) {
11929 if (geo->level == IMSM_T_RAID10 && geo->raid_disks > 2 &&
11930 !c->force) {
11931 pr_err("Warning! VROC UEFI driver does not support RAID10 in requested layout.\n");
11932 pr_err("Array won't be suitable as boot device.\n");
11933 pr_err("Note: You can omit this check with \"--force\"\n");
11934 if (ask("Do you want to continue") < 1)
11935 return CH_ABORT;
11936 }
11937 change = CH_TAKEOVER;
11938 check_devs = 1;
11939 raid_disks *= 2; /* mirrors added */
11940 imsm_layout = 0x102; /* imsm supported layout */
11941 }
11942 break;
11943 case IMSM_T_RAID1:
11944 case IMSM_T_RAID10:
11945 if (geo->level == 0) {
11946 change = CH_TAKEOVER;
11947 check_devs = 1;
11948 raid_disks /= 2;
11949 imsm_layout = 0; /* imsm raid0 layout */
11950 }
11951 break;
11952 }
11953 if (change == -1) {
11954 pr_err("Error. Level Migration from %d to %d not supported!\n",
11955 info.array.level, geo->level);
11956 goto analyse_change_exit;
11957 }
11958 } else
11959 geo->level = info.array.level;
11960
11961 if (geo->layout != info.array.layout &&
11962 (geo->layout != UnSet && geo->layout != -1)) {
11963 change = CH_MIGRATION;
11964 if (info.array.layout == 0 && info.array.level == IMSM_T_RAID5 &&
11965 geo->layout == 5) {
11966 /* reshape 5 -> 4 */
11967 } else if (info.array.layout == 5 && info.array.level == IMSM_T_RAID5 &&
11968 geo->layout == 0) {
11969 /* reshape 4 -> 5 */
11970 geo->layout = 0;
11971 geo->level = 5;
11972 } else {
11973 pr_err("Error. Layout Migration from %d to %d not supported!\n",
11974 info.array.layout, geo->layout);
11975 change = -1;
11976 goto analyse_change_exit;
11977 }
11978 } else {
11979 geo->layout = info.array.layout;
11980 if (imsm_layout == -1)
11981 imsm_layout = info.array.layout;
11982 }
11983
11984 if (geo->chunksize > 0 && geo->chunksize != UnSet &&
11985 geo->chunksize != info.array.chunk_size) {
11986 if (info.array.level == IMSM_T_RAID10) {
11987 pr_err("Error. Chunk size change for RAID 10 is not supported.\n");
11988 change = -1;
11989 goto analyse_change_exit;
11990 } else if (info.component_size % (geo->chunksize/512)) {
11991 pr_err("New chunk size (%dK) does not evenly divide device size (%lluk). Aborting...\n",
11992 geo->chunksize/1024, info.component_size/2);
11993 change = -1;
11994 goto analyse_change_exit;
11995 }
11996 change = CH_MIGRATION;
11997 } else {
11998 geo->chunksize = info.array.chunk_size;
11999 }
12000
12001 if (geo->size > 0) {
12002 if (change != -1) {
12003 pr_err("Error. Size change should be the only one at a time.\n");
12004 change = -1;
12005 goto analyse_change_exit;
12006 }
12007
12008 rv = imsm_analyze_expand(st, geo, &info, direction);
12009 if (rv != IMSM_STATUS_OK)
12010 goto analyse_change_exit;
12011 raid_disks = geo->raid_disks;
12012 change = CH_ARRAY_SIZE;
12013 }
12014
12015 chunk = geo->chunksize / 1024;
12016
12017 if (!validate_geometry_imsm(st,
12018 geo->level,
12019 imsm_layout,
12020 raid_disks,
12021 &chunk,
12022 geo->size, INVALID_SECTORS,
12023 0, 0, info.consistency_policy, 1))
12024 change = -1;
12025
12026 if (check_devs) {
12027 struct intel_super *super = st->sb;
12028 struct imsm_super *mpb = super->anchor;
12029
12030 if (mpb->num_raid_devs > 1) {
12031 pr_err("Error. Cannot perform operation on %s- for this operation "
12032 "it MUST be single array in container\n", geo->dev_name);
12033 change = -1;
12034 }
12035 }
12036
12037 analyse_change_exit:
12038 if (direction == ROLLBACK_METADATA_CHANGES &&
12039 (change == CH_MIGRATION || change == CH_TAKEOVER)) {
12040 dprintf("imsm: Metadata changes rollback is not supported for migration and takeover operations.\n");
12041 change = -1;
12042 }
12043 return change;
12044 }
12045
12046 int imsm_takeover(struct supertype *st, struct geo_params *geo)
12047 {
12048 struct intel_super *super = st->sb;
12049 struct imsm_update_takeover *u;
12050
12051 u = xmalloc(sizeof(struct imsm_update_takeover));
12052
12053 u->type = update_takeover;
12054 u->subarray = super->current_vol;
12055
12056 /* 10->0 transition */
12057 if (geo->level == 0)
12058 u->direction = R10_TO_R0;
12059
12060 /* 0->10 transition */
12061 if (geo->level == 10)
12062 u->direction = R0_TO_R10;
12063
12064 /* update metadata locally */
12065 imsm_update_metadata_locally(st, u,
12066 sizeof(struct imsm_update_takeover));
12067 /* and possibly remotely */
12068 if (st->update_tail)
12069 append_metadata_update(st, u,
12070 sizeof(struct imsm_update_takeover));
12071 else
12072 free(u);
12073
12074 return 0;
12075 }
12076
12077 /* Flush size update if size calculated by num_data_stripes is higher than
12078 * imsm_dev_size to eliminate differences during reshape.
12079 * Mdmon will recalculate them correctly.
12080 * If subarray index is not set then check whole container.
12081 * Returns:
12082 * 0 - no error occurred
12083 * 1 - error detected
12084 */
12085 static int imsm_fix_size_mismatch(struct supertype *st, int subarray_index)
12086 {
12087 struct intel_super *super = st->sb;
12088 int tmp = super->current_vol;
12089 int ret_val = 1;
12090 int i;
12091
12092 for (i = 0; i < super->anchor->num_raid_devs; i++) {
12093 if (subarray_index >= 0 && i != subarray_index)
12094 continue;
12095 super->current_vol = i;
12096 struct imsm_dev *dev = get_imsm_dev(super, super->current_vol);
12097 struct imsm_map *map = get_imsm_map(dev, MAP_0);
12098 unsigned int disc_count = imsm_num_data_members(map);
12099 struct geo_params geo;
12100 struct imsm_update_size_change *update;
12101 unsigned long long calc_size = per_dev_array_size(map) * disc_count;
12102 unsigned long long d_size = imsm_dev_size(dev);
12103 int u_size;
12104
12105 if (calc_size == d_size)
12106 continue;
12107
12108 /* There is a difference, confirm that imsm_dev_size is
12109 * smaller and push update.
12110 */
12111 if (d_size > calc_size) {
12112 pr_err("imsm: dev size of subarray %d is incorrect\n",
12113 i);
12114 goto exit;
12115 }
12116 memset(&geo, 0, sizeof(struct geo_params));
12117 geo.size = d_size;
12118 u_size = imsm_create_metadata_update_for_size_change(st, &geo,
12119 &update);
12120 imsm_update_metadata_locally(st, update, u_size);
12121 if (st->update_tail) {
12122 append_metadata_update(st, update, u_size);
12123 flush_metadata_updates(st);
12124 st->update_tail = &st->updates;
12125 } else {
12126 imsm_sync_metadata(st);
12127 free(update);
12128 }
12129 }
12130 ret_val = 0;
12131 exit:
12132 super->current_vol = tmp;
12133 return ret_val;
12134 }
12135
12136 /**
12137 * shape_to_geo() - fill geo_params from shape.
12138 *
12139 * @shape: array details.
12140 * @geo: new geometry params.
12141 * Returns: 0 on success, 1 otherwise.
12142 */
12143 static void shape_to_geo(struct shape *shape, struct geo_params *geo)
12144 {
12145 assert(shape);
12146 assert(geo);
12147
12148 geo->dev_name = shape->dev;
12149 geo->size = shape->size;
12150 geo->level = shape->level;
12151 geo->layout = shape->layout;
12152 geo->chunksize = shape->chunk;
12153 geo->raid_disks = shape->raiddisks;
12154 }
12155
12156 static int imsm_reshape_super(struct supertype *st, struct shape *shape, struct context *c)
12157 {
12158 int ret_val = 1;
12159 struct geo_params geo = {0};
12160
12161 dprintf("(enter)\n");
12162
12163 shape_to_geo(shape, &geo);
12164 strcpy(geo.devnm, st->devnm);
12165 if (shape->delta_disks != UnSet)
12166 geo.raid_disks += shape->delta_disks;
12167
12168 dprintf("for level : %i\n", geo.level);
12169 dprintf("for raid_disks : %i\n", geo.raid_disks);
12170
12171 if (strcmp(st->container_devnm, st->devnm) == 0) {
12172 /* On container level we can only increase number of devices. */
12173 dprintf("imsm: info: Container operation\n");
12174 int old_raid_disks = 0;
12175
12176 if (imsm_reshape_is_allowed_on_container(
12177 st, &geo, &old_raid_disks, shape->direction)) {
12178 struct imsm_update_reshape *u = NULL;
12179 int len;
12180
12181 if (imsm_fix_size_mismatch(st, -1)) {
12182 dprintf("imsm: Cannot fix size mismatch\n");
12183 goto exit_imsm_reshape_super;
12184 }
12185
12186 len = imsm_create_metadata_update_for_reshape(
12187 st, &geo, old_raid_disks, &u);
12188
12189 if (len <= 0) {
12190 dprintf("imsm: Cannot prepare update\n");
12191 goto exit_imsm_reshape_super;
12192 }
12193
12194 ret_val = 0;
12195 /* update metadata locally */
12196 imsm_update_metadata_locally(st, u, len);
12197 /* and possibly remotely */
12198 if (st->update_tail)
12199 append_metadata_update(st, u, len);
12200 else
12201 free(u);
12202
12203 } else {
12204 pr_err("(imsm) Operation is not allowed on this container\n");
12205 }
12206 } else {
12207 /* On volume level we support following operations
12208 * - takeover: raid10 -> raid0; raid0 -> raid10
12209 * - chunk size migration
12210 * - migration: raid5 -> raid0; raid0 -> raid5
12211 */
12212 struct intel_super *super = st->sb;
12213 struct intel_dev *dev = super->devlist;
12214 int change;
12215 dprintf("imsm: info: Volume operation\n");
12216 /* find requested device */
12217 while (dev) {
12218 char *devnm =
12219 imsm_find_array_devnm_by_subdev(
12220 dev->index, st->container_devnm);
12221 if (devnm && strcmp(devnm, geo.devnm) == 0)
12222 break;
12223 dev = dev->next;
12224 }
12225 if (dev == NULL) {
12226 pr_err("Cannot find %s (%s) subarray\n",
12227 geo.dev_name, geo.devnm);
12228 goto exit_imsm_reshape_super;
12229 }
12230 super->current_vol = dev->index;
12231 change = imsm_analyze_change(st, &geo, shape->direction, c);
12232 switch (change) {
12233 case CH_TAKEOVER:
12234 ret_val = imsm_takeover(st, &geo);
12235 break;
12236 case CH_MIGRATION: {
12237 struct imsm_update_reshape_migration *u = NULL;
12238 int len =
12239 imsm_create_metadata_update_for_migration(
12240 st, &geo, &u);
12241 if (len < 1) {
12242 dprintf("imsm: Cannot prepare update\n");
12243 break;
12244 }
12245 ret_val = 0;
12246 /* update metadata locally */
12247 imsm_update_metadata_locally(st, u, len);
12248 /* and possibly remotely */
12249 if (st->update_tail)
12250 append_metadata_update(st, u, len);
12251 else
12252 free(u);
12253 }
12254 break;
12255 case CH_ARRAY_SIZE: {
12256 struct imsm_update_size_change *u = NULL;
12257 int len =
12258 imsm_create_metadata_update_for_size_change(
12259 st, &geo, &u);
12260 if (len < 1) {
12261 dprintf("imsm: Cannot prepare update\n");
12262 break;
12263 }
12264 ret_val = 0;
12265 /* update metadata locally */
12266 imsm_update_metadata_locally(st, u, len);
12267 /* and possibly remotely */
12268 if (st->update_tail)
12269 append_metadata_update(st, u, len);
12270 else
12271 free(u);
12272 }
12273 break;
12274 case CH_ABORT:
12275 default:
12276 ret_val = 1;
12277 }
12278 }
12279
12280 exit_imsm_reshape_super:
12281 dprintf("imsm: reshape_super Exit code = %i\n", ret_val);
12282 return ret_val;
12283 }
12284
12285 #define COMPLETED_OK 0
12286 #define COMPLETED_NONE 1
12287 #define COMPLETED_DELAYED 2
12288
12289 static int read_completed(int fd, unsigned long long *val)
12290 {
12291 int ret;
12292 char buf[SYSFS_MAX_BUF_SIZE];
12293
12294 ret = sysfs_fd_get_str(fd, buf, sizeof(buf));
12295 if (ret < 0)
12296 return ret;
12297
12298 ret = COMPLETED_OK;
12299 if (str_is_none(buf) == true) {
12300 ret = COMPLETED_NONE;
12301 } else if (strncmp(buf, "delayed", 7) == 0) {
12302 ret = COMPLETED_DELAYED;
12303 } else {
12304 char *ep;
12305 *val = strtoull(buf, &ep, 0);
12306 if (ep == buf || (*ep != 0 && *ep != '\n' && *ep != ' '))
12307 ret = -1;
12308 }
12309 return ret;
12310 }
12311
12312 /*******************************************************************************
12313 * Function: wait_for_reshape_imsm
12314 * Description: Function writes new sync_max value and waits until
12315 * reshape process reach new position
12316 * Parameters:
12317 * sra : general array info
12318 * ndata : number of disks in new array's layout
12319 * Returns:
12320 * 0 : success,
12321 * 1 : there is no reshape in progress,
12322 * -1 : fail
12323 ******************************************************************************/
12324 int wait_for_reshape_imsm(struct mdinfo *sra, int ndata)
12325 {
12326 int fd = sysfs_get_fd(sra, NULL, "sync_completed");
12327 int retry = 3;
12328 unsigned long long completed;
12329 /* to_complete : new sync_max position */
12330 unsigned long long to_complete = sra->reshape_progress;
12331 unsigned long long position_to_set = to_complete / ndata;
12332
12333 if (!is_fd_valid(fd)) {
12334 dprintf("cannot open reshape_position\n");
12335 return 1;
12336 }
12337
12338 do {
12339 if (sysfs_fd_get_ll(fd, &completed) < 0) {
12340 if (!retry) {
12341 dprintf("cannot read reshape_position (no reshape in progres)\n");
12342 close(fd);
12343 return 1;
12344 }
12345 sleep_for(0, MSEC_TO_NSEC(30), true);
12346 } else
12347 break;
12348 } while (retry--);
12349
12350 if (completed > position_to_set) {
12351 dprintf("wrong next position to set %llu (%llu)\n",
12352 to_complete, position_to_set);
12353 close(fd);
12354 return -1;
12355 }
12356 dprintf("Position set: %llu\n", position_to_set);
12357 if (sysfs_set_num(sra, NULL, "sync_max",
12358 position_to_set) != 0) {
12359 dprintf("cannot set reshape position to %llu\n",
12360 position_to_set);
12361 close(fd);
12362 return -1;
12363 }
12364
12365 do {
12366 int rc;
12367 char action[SYSFS_MAX_BUF_SIZE];
12368 int timeout = 3000;
12369
12370 sysfs_wait(fd, &timeout);
12371 if (sysfs_get_str(sra, NULL, "sync_action",
12372 action, sizeof(action)) > 0 &&
12373 strncmp(action, "reshape", 7) != 0) {
12374 if (strncmp(action, "idle", 4) == 0)
12375 break;
12376 close(fd);
12377 return -1;
12378 }
12379
12380 rc = read_completed(fd, &completed);
12381 if (rc < 0) {
12382 dprintf("cannot read reshape_position (in loop)\n");
12383 close(fd);
12384 return 1;
12385 } else if (rc == COMPLETED_NONE)
12386 break;
12387 } while (completed < position_to_set);
12388
12389 close(fd);
12390 return 0;
12391 }
12392
12393 /*******************************************************************************
12394 * Function: check_degradation_change
12395 * Description: Check that array hasn't become failed.
12396 * Parameters:
12397 * info : for sysfs access
12398 * sources : source disks descriptors
12399 * degraded: previous degradation level
12400 * Returns:
12401 * degradation level
12402 ******************************************************************************/
12403 int check_degradation_change(struct mdinfo *info,
12404 int *sources,
12405 int degraded)
12406 {
12407 unsigned long long new_degraded;
12408 int rv;
12409
12410 rv = sysfs_get_ll(info, NULL, "degraded", &new_degraded);
12411 if (rv == -1 || (new_degraded != (unsigned long long)degraded)) {
12412 /* check each device to ensure it is still working */
12413 struct mdinfo *sd;
12414 new_degraded = 0;
12415 for (sd = info->devs ; sd ; sd = sd->next) {
12416 if (sd->disk.state & (1<<MD_DISK_FAULTY))
12417 continue;
12418 if (sd->disk.state & (1<<MD_DISK_SYNC)) {
12419 char sbuf[SYSFS_MAX_BUF_SIZE];
12420 int raid_disk = sd->disk.raid_disk;
12421
12422 if (sysfs_get_str(info,
12423 sd, "state", sbuf, sizeof(sbuf)) < 0 ||
12424 strstr(sbuf, "faulty") ||
12425 strstr(sbuf, "in_sync") == NULL) {
12426 /* this device is dead */
12427 sd->disk.state = (1<<MD_DISK_FAULTY);
12428 if (raid_disk >= 0)
12429 close_fd(&sources[raid_disk]);
12430 new_degraded++;
12431 }
12432 }
12433 }
12434 }
12435
12436 return new_degraded;
12437 }
12438
12439 /*******************************************************************************
12440 * Function: imsm_manage_reshape
12441 * Description: Function finds array under reshape and it manages reshape
12442 * process. It creates stripes backups (if required) and sets
12443 * checkpoints.
12444 * Parameters:
12445 * afd : Backup handle (nattive) - not used
12446 * sra : general array info
12447 * reshape : reshape parameters - not used
12448 * st : supertype structure
12449 * blocks : size of critical section [blocks]
12450 * fds : table of source device descriptor
12451 * offsets : start of array (offest per devices)
12452 * dests : not used
12453 * destfd : table of destination device descriptor
12454 * destoffsets : table of destination offsets (per device)
12455 * Returns:
12456 * 1 : success, reshape is done
12457 * 0 : fail
12458 ******************************************************************************/
12459 static int imsm_manage_reshape(
12460 int afd, struct mdinfo *sra, struct reshape *reshape,
12461 struct supertype *st, unsigned long backup_blocks,
12462 int *fds, unsigned long long *offsets,
12463 int dests, int *destfd, unsigned long long *destoffsets)
12464 {
12465 int ret_val = 0;
12466 struct intel_super *super = st->sb;
12467 struct intel_dev *dv;
12468 unsigned int sector_size = super->sector_size;
12469 struct imsm_dev *dev = NULL;
12470 struct imsm_map *map_src, *map_dest;
12471 int migr_vol_qan = 0;
12472 int ndata, odata; /* [bytes] */
12473 int chunk; /* [bytes] */
12474 struct migr_record *migr_rec;
12475 char *buf = NULL;
12476 unsigned int buf_size; /* [bytes] */
12477 unsigned long long max_position; /* array size [bytes] */
12478 unsigned long long next_step; /* [blocks]/[bytes] */
12479 unsigned long long old_data_stripe_length;
12480 unsigned long long start_src; /* [bytes] */
12481 unsigned long long start; /* [bytes] */
12482 unsigned long long start_buf_shift; /* [bytes] */
12483 int degraded = 0;
12484 int source_layout = 0;
12485 int subarray_index = -1;
12486
12487 if (!sra)
12488 return ret_val;
12489
12490 if (!fds || !offsets)
12491 goto abort;
12492
12493 /* Find volume during the reshape */
12494 for (dv = super->devlist; dv; dv = dv->next) {
12495 if (dv->dev->vol.migr_type == MIGR_GEN_MIGR &&
12496 dv->dev->vol.migr_state == 1) {
12497 dev = dv->dev;
12498 migr_vol_qan++;
12499 subarray_index = dv->index;
12500 }
12501 }
12502 /* Only one volume can migrate at the same time */
12503 if (migr_vol_qan != 1) {
12504 pr_err("%s", migr_vol_qan ?
12505 "Number of migrating volumes greater than 1\n" :
12506 "There is no volume during migrationg\n");
12507 goto abort;
12508 }
12509
12510 map_dest = get_imsm_map(dev, MAP_0);
12511 map_src = get_imsm_map(dev, MAP_1);
12512 if (map_src == NULL)
12513 goto abort;
12514
12515 ndata = imsm_num_data_members(map_dest);
12516 odata = imsm_num_data_members(map_src);
12517
12518 chunk = __le16_to_cpu(map_src->blocks_per_strip) * 512;
12519 old_data_stripe_length = odata * chunk;
12520
12521 migr_rec = super->migr_rec;
12522
12523 /* initialize migration record for start condition */
12524 if (sra->reshape_progress == 0)
12525 init_migr_record_imsm(st, dev, sra);
12526 else {
12527 if (__le32_to_cpu(migr_rec->rec_status) != UNIT_SRC_NORMAL) {
12528 dprintf("imsm: cannot restart migration when data are present in copy area.\n");
12529 goto abort;
12530 }
12531 /* Save checkpoint to update migration record for current
12532 * reshape position (in md). It can be farther than current
12533 * reshape position in metadata.
12534 */
12535 if (save_checkpoint_imsm(st, sra, UNIT_SRC_NORMAL) == 1) {
12536 /* ignore error == 2, this can mean end of reshape here
12537 */
12538 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL, initial save)\n");
12539 goto abort;
12540 }
12541 }
12542
12543 /* size for data */
12544 buf_size = __le32_to_cpu(migr_rec->blocks_per_unit) * 512;
12545 /* extend buffer size for parity disk */
12546 buf_size += __le32_to_cpu(migr_rec->dest_depth_per_unit) * 512;
12547 /* add space for stripe alignment */
12548 buf_size += old_data_stripe_length;
12549 if (posix_memalign((void **)&buf, MAX_SECTOR_SIZE, buf_size)) {
12550 dprintf("imsm: Cannot allocate checkpoint buffer\n");
12551 goto abort;
12552 }
12553
12554 max_position = sra->component_size * ndata;
12555 source_layout = imsm_level_to_layout(map_src->raid_level);
12556
12557 while (current_migr_unit(migr_rec) <
12558 get_num_migr_units(migr_rec)) {
12559 /* current reshape position [blocks] */
12560 unsigned long long current_position =
12561 __le32_to_cpu(migr_rec->blocks_per_unit)
12562 * current_migr_unit(migr_rec);
12563 unsigned long long border;
12564
12565 /* Check that array hasn't become failed.
12566 */
12567 degraded = check_degradation_change(sra, fds, degraded);
12568 if (degraded > 1) {
12569 dprintf("imsm: Abort reshape due to degradation level (%i)\n", degraded);
12570 goto abort;
12571 }
12572
12573 next_step = __le32_to_cpu(migr_rec->blocks_per_unit);
12574
12575 if ((current_position + next_step) > max_position)
12576 next_step = max_position - current_position;
12577
12578 start = current_position * 512;
12579
12580 /* align reading start to old geometry */
12581 start_buf_shift = start % old_data_stripe_length;
12582 start_src = start - start_buf_shift;
12583
12584 border = (start_src / odata) - (start / ndata);
12585 border /= 512;
12586 if (border <= __le32_to_cpu(migr_rec->dest_depth_per_unit)) {
12587 /* save critical stripes to buf
12588 * start - start address of current unit
12589 * to backup [bytes]
12590 * start_src - start address of current unit
12591 * to backup alligned to source array
12592 * [bytes]
12593 */
12594 unsigned long long next_step_filler;
12595 unsigned long long copy_length = next_step * 512;
12596
12597 /* allign copy area length to stripe in old geometry */
12598 next_step_filler = ((copy_length + start_buf_shift)
12599 % old_data_stripe_length);
12600 if (next_step_filler)
12601 next_step_filler = (old_data_stripe_length
12602 - next_step_filler);
12603 dprintf("save_stripes() parameters: start = %llu,\tstart_src = %llu,\tnext_step*512 = %llu,\tstart_in_buf_shift = %llu,\tnext_step_filler = %llu\n",
12604 start, start_src, copy_length,
12605 start_buf_shift, next_step_filler);
12606
12607 if (save_stripes(fds, offsets, map_src->num_members,
12608 chunk, map_src->raid_level,
12609 source_layout, 0, NULL, start_src,
12610 copy_length +
12611 next_step_filler + start_buf_shift,
12612 buf)) {
12613 dprintf("imsm: Cannot save stripes to buffer\n");
12614 goto abort;
12615 }
12616 /* Convert data to destination format and store it
12617 * in backup general migration area
12618 */
12619 if (save_backup_imsm(st, dev, sra,
12620 buf + start_buf_shift, copy_length)) {
12621 dprintf("imsm: Cannot save stripes to target devices\n");
12622 goto abort;
12623 }
12624 if (save_checkpoint_imsm(st, sra,
12625 UNIT_SRC_IN_CP_AREA)) {
12626 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_IN_CP_AREA)\n");
12627 goto abort;
12628 }
12629 } else {
12630 /* set next step to use whole border area */
12631 border /= next_step;
12632 if (border > 1)
12633 next_step *= border;
12634 }
12635 /* When data backed up, checkpoint stored,
12636 * kick the kernel to reshape unit of data
12637 */
12638 next_step = next_step + sra->reshape_progress;
12639 /* limit next step to array max position */
12640 if (next_step > max_position)
12641 next_step = max_position;
12642 sysfs_set_num(sra, NULL, "suspend_lo", sra->reshape_progress);
12643 sysfs_set_num(sra, NULL, "suspend_hi", next_step);
12644 sra->reshape_progress = next_step;
12645
12646 /* wait until reshape finish */
12647 if (wait_for_reshape_imsm(sra, ndata)) {
12648 dprintf("wait_for_reshape_imsm returned error!\n");
12649 goto abort;
12650 }
12651 if (sigterm)
12652 goto abort;
12653
12654 if (save_checkpoint_imsm(st, sra, UNIT_SRC_NORMAL) == 1) {
12655 /* ignore error == 2, this can mean end of reshape here
12656 */
12657 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL)\n");
12658 goto abort;
12659 }
12660
12661 }
12662
12663 /* clear migr_rec on disks after successful migration */
12664 struct dl *d;
12665
12666 memset(super->migr_rec_buf, 0, MIGR_REC_BUF_SECTORS*MAX_SECTOR_SIZE);
12667 for (d = super->disks; d; d = d->next) {
12668 if (d->index < 0 || is_failed(&d->disk))
12669 continue;
12670 unsigned long long dsize;
12671
12672 get_dev_size(d->fd, NULL, &dsize);
12673 if (lseek64(d->fd, dsize - MIGR_REC_SECTOR_POSITION*sector_size,
12674 SEEK_SET) >= 0) {
12675 if ((unsigned int)write(d->fd, super->migr_rec_buf,
12676 MIGR_REC_BUF_SECTORS*sector_size) !=
12677 MIGR_REC_BUF_SECTORS*sector_size)
12678 perror("Write migr_rec failed");
12679 }
12680 }
12681
12682 /* return '1' if done */
12683 ret_val = 1;
12684
12685 /* After the reshape eliminate size mismatch in metadata.
12686 * Don't update md/component_size here, volume hasn't
12687 * to take whole space. It is allowed by kernel.
12688 * md/component_size will be set propoperly after next assembly.
12689 */
12690 imsm_fix_size_mismatch(st, subarray_index);
12691
12692 abort:
12693 free(buf);
12694 /* See Grow.c: abort_reshape() for further explanation */
12695 sysfs_set_num(sra, NULL, "suspend_lo", 0x7FFFFFFFFFFFFFFFULL);
12696 sysfs_set_num(sra, NULL, "suspend_hi", 0);
12697 sysfs_set_num(sra, NULL, "suspend_lo", 0);
12698
12699 return ret_val;
12700 }
12701
12702 /*******************************************************************************
12703 * Function: calculate_bitmap_min_chunksize
12704 * Description: Calculates the minimal valid bitmap chunk size
12705 * Parameters:
12706 * max_bits : indicate how many bits can be used for the bitmap
12707 * data_area_size : the size of the data area covered by the bitmap
12708 *
12709 * Returns:
12710 * The bitmap chunk size
12711 ******************************************************************************/
12712 static unsigned long long
12713 calculate_bitmap_min_chunksize(unsigned long long max_bits,
12714 unsigned long long data_area_size)
12715 {
12716 unsigned long long min_chunk =
12717 4096; /* sub-page chunks don't work yet.. */
12718 unsigned long long bits = data_area_size / min_chunk + 1;
12719
12720 while (bits > max_bits) {
12721 min_chunk *= 2;
12722 bits = (bits + 1) / 2;
12723 }
12724 return min_chunk;
12725 }
12726
12727 /*******************************************************************************
12728 * Function: calculate_bitmap_chunksize
12729 * Description: Calculates the bitmap chunk size for the given device
12730 * Parameters:
12731 * st : supertype information
12732 * dev : device for the bitmap
12733 *
12734 * Returns:
12735 * The bitmap chunk size
12736 ******************************************************************************/
12737 static unsigned long long calculate_bitmap_chunksize(struct supertype *st,
12738 struct imsm_dev *dev)
12739 {
12740 struct intel_super *super = st->sb;
12741 unsigned long long min_chunksize;
12742 unsigned long long result = IMSM_DEFAULT_BITMAP_CHUNKSIZE;
12743 size_t dev_size = imsm_dev_size(dev);
12744
12745 min_chunksize = calculate_bitmap_min_chunksize(
12746 IMSM_BITMAP_AREA_SIZE * super->sector_size, dev_size);
12747
12748 if (result < min_chunksize)
12749 result = min_chunksize;
12750
12751 return result;
12752 }
12753
12754 /*******************************************************************************
12755 * Function: init_bitmap_header
12756 * Description: Initialize the bitmap header structure
12757 * Parameters:
12758 * st : supertype information
12759 * bms : bitmap header struct to initialize
12760 * dev : device for the bitmap
12761 *
12762 * Returns:
12763 * 0 : success
12764 * -1 : fail
12765 ******************************************************************************/
12766 static int init_bitmap_header(struct supertype *st, struct bitmap_super_s *bms,
12767 struct imsm_dev *dev)
12768 {
12769 int vol_uuid[4];
12770
12771 if (!bms || !dev)
12772 return -1;
12773
12774 bms->magic = __cpu_to_le32(BITMAP_MAGIC);
12775 bms->version = __cpu_to_le32(BITMAP_MAJOR_HI);
12776 bms->daemon_sleep = __cpu_to_le32(IMSM_DEFAULT_BITMAP_DAEMON_SLEEP);
12777 bms->sync_size = __cpu_to_le64(IMSM_BITMAP_AREA_SIZE);
12778 bms->write_behind = __cpu_to_le32(0);
12779
12780 uuid_from_super_imsm(st, vol_uuid);
12781 memcpy(bms->uuid, vol_uuid, 16);
12782
12783 bms->chunksize = calculate_bitmap_chunksize(st, dev);
12784
12785 return 0;
12786 }
12787
12788 /*******************************************************************************
12789 * Function: validate_internal_bitmap_for_drive
12790 * Description: Verify if the bitmap header for a given drive.
12791 * Parameters:
12792 * st : supertype information
12793 * offset : The offset from the beginning of the drive where to look for
12794 * the bitmap header.
12795 * d : the drive info
12796 *
12797 * Returns:
12798 * 0 : success
12799 * -1 : fail
12800 ******************************************************************************/
12801 static int validate_internal_bitmap_for_drive(struct supertype *st,
12802 unsigned long long offset,
12803 struct dl *d)
12804 {
12805 struct intel_super *super = st->sb;
12806 int ret = -1;
12807 int vol_uuid[4];
12808 bitmap_super_t *bms;
12809 int fd;
12810
12811 if (!d)
12812 return -1;
12813
12814 void *read_buf;
12815
12816 if (posix_memalign(&read_buf, MAX_SECTOR_SIZE, IMSM_BITMAP_HEADER_SIZE))
12817 return -1;
12818
12819 fd = d->fd;
12820 if (!is_fd_valid(fd)) {
12821 fd = open(d->devname, O_RDONLY, 0);
12822
12823 if (!is_fd_valid(fd)) {
12824 dprintf("cannot open the device %s\n", d->devname);
12825 goto abort;
12826 }
12827 }
12828
12829 if (lseek64(fd, offset * super->sector_size, SEEK_SET) < 0)
12830 goto abort;
12831 if (read(fd, read_buf, IMSM_BITMAP_HEADER_SIZE) !=
12832 IMSM_BITMAP_HEADER_SIZE)
12833 goto abort;
12834
12835 uuid_from_super_imsm(st, vol_uuid);
12836
12837 bms = read_buf;
12838 if ((bms->magic != __cpu_to_le32(BITMAP_MAGIC)) ||
12839 (bms->version != __cpu_to_le32(BITMAP_MAJOR_HI)) ||
12840 (!same_uuid((int *)bms->uuid, vol_uuid, st->ss->swapuuid))) {
12841 dprintf("wrong bitmap header detected\n");
12842 goto abort;
12843 }
12844
12845 ret = 0;
12846 abort:
12847 if (!is_fd_valid(d->fd))
12848 close_fd(&fd);
12849
12850 if (read_buf)
12851 free(read_buf);
12852
12853 return ret;
12854 }
12855
12856 /*******************************************************************************
12857 * Function: validate_internal_bitmap_imsm
12858 * Description: Verify if the bitmap header is in place and with proper data.
12859 * Parameters:
12860 * st : supertype information
12861 *
12862 * Returns:
12863 * 0 : success or device w/o RWH_BITMAP
12864 * -1 : fail
12865 ******************************************************************************/
12866 static int validate_internal_bitmap_imsm(struct supertype *st)
12867 {
12868 struct intel_super *super = st->sb;
12869 struct imsm_dev *dev = get_imsm_dev(super, super->current_vol);
12870 unsigned long long offset;
12871 struct dl *d;
12872
12873 if (dev->rwh_policy != RWH_BITMAP)
12874 return 0;
12875
12876 offset = get_bitmap_header_sector(super, super->current_vol);
12877 for (d = super->disks; d; d = d->next) {
12878 if (d->index < 0 || is_failed(&d->disk))
12879 continue;
12880
12881 if (validate_internal_bitmap_for_drive(st, offset, d)) {
12882 pr_err("imsm: bitmap validation failed\n");
12883 return -1;
12884 }
12885 }
12886 return 0;
12887 }
12888
12889 /*******************************************************************************
12890 * Function: add_internal_bitmap_imsm
12891 * Description: Mark the volume to use the bitmap and updates the chunk size value.
12892 * Parameters:
12893 * st : supertype information
12894 * chunkp : bitmap chunk size
12895 * delay : not used for imsm
12896 * write_behind : not used for imsm
12897 * size : not used for imsm
12898 * may_change : not used for imsm
12899 * amajor : not used for imsm
12900 *
12901 * Returns:
12902 * 0 : success
12903 * -1 : fail
12904 ******************************************************************************/
12905 static int add_internal_bitmap_imsm(struct supertype *st, int *chunkp,
12906 int delay, int write_behind,
12907 unsigned long long size, int may_change,
12908 int amajor)
12909 {
12910 struct intel_super *super = st->sb;
12911 int vol_idx = super->current_vol;
12912 struct imsm_dev *dev;
12913
12914 if (!super->devlist || vol_idx == -1 || !chunkp)
12915 return -1;
12916
12917 dev = get_imsm_dev(super, vol_idx);
12918 dev->rwh_policy = RWH_BITMAP;
12919 *chunkp = calculate_bitmap_chunksize(st, dev);
12920 return 0;
12921 }
12922
12923 /*******************************************************************************
12924 * Function: locate_bitmap_imsm
12925 * Description: Seek 'fd' to start of write-intent-bitmap.
12926 * Parameters:
12927 * st : supertype information
12928 * fd : file descriptor for the device
12929 * node_num : not used for imsm
12930 *
12931 * Returns:
12932 * 0 : success
12933 * -1 : fail
12934 ******************************************************************************/
12935 static int locate_bitmap_imsm(struct supertype *st, int fd, int node_num)
12936 {
12937 struct intel_super *super = st->sb;
12938 unsigned long long offset;
12939 int vol_idx = super->current_vol;
12940
12941 if (!super->devlist || vol_idx == -1)
12942 return -1;
12943
12944 offset = get_bitmap_header_sector(super, super->current_vol);
12945 dprintf("bitmap header offset is %llu\n", offset);
12946
12947 lseek64(fd, offset << 9, 0);
12948
12949 return 0;
12950 }
12951
12952 /*******************************************************************************
12953 * Function: write_init_bitmap_imsm
12954 * Description: Write a bitmap header and prepares the area for the bitmap.
12955 * Parameters:
12956 * st : supertype information
12957 * fd : file descriptor for the device
12958 * update : not used for imsm
12959 *
12960 * Returns:
12961 * 0 : success
12962 * -1 : fail
12963 ******************************************************************************/
12964 static int write_init_bitmap_imsm(struct supertype *st, int fd,
12965 enum bitmap_update update)
12966 {
12967 struct intel_super *super = st->sb;
12968 int vol_idx = super->current_vol;
12969 int ret = 0;
12970 unsigned long long offset;
12971 bitmap_super_t bms = { 0 };
12972 size_t written = 0;
12973 size_t to_write;
12974 ssize_t rv_num;
12975 void *buf;
12976
12977 if (!super->devlist || !super->sector_size || vol_idx == -1)
12978 return -1;
12979
12980 struct imsm_dev *dev = get_imsm_dev(super, vol_idx);
12981
12982 /* first clear the space for bitmap header */
12983 unsigned long long bitmap_area_start =
12984 get_bitmap_header_sector(super, vol_idx);
12985
12986 dprintf("zeroing area start (%llu) and size (%u)\n", bitmap_area_start,
12987 IMSM_BITMAP_AND_HEADER_SIZE / super->sector_size);
12988 if (zero_disk_range(fd, bitmap_area_start,
12989 IMSM_BITMAP_HEADER_SIZE / super->sector_size)) {
12990 pr_err("imsm: cannot zeroing the space for the bitmap\n");
12991 return -1;
12992 }
12993
12994 /* The bitmap area should be filled with "1"s to perform initial
12995 * synchronization.
12996 */
12997 if (posix_memalign(&buf, MAX_SECTOR_SIZE, MAX_SECTOR_SIZE))
12998 return -1;
12999 memset(buf, 0xFF, MAX_SECTOR_SIZE);
13000 offset = get_bitmap_sector(super, vol_idx);
13001 lseek64(fd, offset << 9, 0);
13002 while (written < IMSM_BITMAP_AREA_SIZE) {
13003 to_write = IMSM_BITMAP_AREA_SIZE - written;
13004 if (to_write > MAX_SECTOR_SIZE)
13005 to_write = MAX_SECTOR_SIZE;
13006 rv_num = write(fd, buf, MAX_SECTOR_SIZE);
13007 if (rv_num != MAX_SECTOR_SIZE) {
13008 ret = -1;
13009 dprintf("cannot initialize bitmap area\n");
13010 goto abort;
13011 }
13012 written += rv_num;
13013 }
13014
13015 /* write a bitmap header */
13016 init_bitmap_header(st, &bms, dev);
13017 memset(buf, 0, MAX_SECTOR_SIZE);
13018 memcpy(buf, &bms, sizeof(bitmap_super_t));
13019 if (locate_bitmap_imsm(st, fd, 0)) {
13020 ret = -1;
13021 dprintf("cannot locate the bitmap\n");
13022 goto abort;
13023 }
13024 if (write(fd, buf, MAX_SECTOR_SIZE) != MAX_SECTOR_SIZE) {
13025 ret = -1;
13026 dprintf("cannot write the bitmap header\n");
13027 goto abort;
13028 }
13029 fsync(fd);
13030
13031 abort:
13032 free(buf);
13033
13034 return ret;
13035 }
13036
13037 /*******************************************************************************
13038 * Function: is_vol_to_setup_bitmap
13039 * Description: Checks if a bitmap should be activated on the dev.
13040 * Parameters:
13041 * info : info about the volume to setup the bitmap
13042 * dev : the device to check against bitmap creation
13043 *
13044 * Returns:
13045 * 0 : bitmap should be set up on the device
13046 * -1 : otherwise
13047 ******************************************************************************/
13048 static int is_vol_to_setup_bitmap(struct mdinfo *info, struct imsm_dev *dev)
13049 {
13050 if (!dev || !info)
13051 return -1;
13052
13053 if ((strcmp((char *)dev->volume, info->name) == 0) &&
13054 (dev->rwh_policy == RWH_BITMAP))
13055 return -1;
13056
13057 return 0;
13058 }
13059
13060 /*******************************************************************************
13061 * Function: set_bitmap_sysfs
13062 * Description: Set the sysfs atributes of a given volume to activate the bitmap.
13063 * Parameters:
13064 * info : info about the volume where the bitmap should be setup
13065 * chunksize : bitmap chunk size
13066 * location : location of the bitmap
13067 *
13068 * Returns:
13069 * 0 : success
13070 * -1 : fail
13071 ******************************************************************************/
13072 static int set_bitmap_sysfs(struct mdinfo *info, unsigned long long chunksize,
13073 char *location)
13074 {
13075 /* The bitmap/metadata is set to external to allow changing of value for
13076 * bitmap/location. When external is used, the kernel will treat an offset
13077 * related to the device's first lba (in opposition to the "internal" case
13078 * when this value is related to the beginning of the superblock).
13079 */
13080 if (sysfs_set_str(info, NULL, "bitmap/metadata", "external")) {
13081 dprintf("failed to set bitmap/metadata\n");
13082 return -1;
13083 }
13084
13085 /* It can only be changed when no bitmap is active.
13086 * Should be bigger than 512 and must be power of 2.
13087 * It is expecting the value in bytes.
13088 */
13089 if (sysfs_set_num(info, NULL, "bitmap/chunksize",
13090 __cpu_to_le32(chunksize))) {
13091 dprintf("failed to set bitmap/chunksize\n");
13092 return -1;
13093 }
13094
13095 /* It is expecting the value in sectors. */
13096 if (sysfs_set_num(info, NULL, "bitmap/space",
13097 __cpu_to_le64(IMSM_BITMAP_AREA_SIZE))) {
13098 dprintf("failed to set bitmap/space\n");
13099 return -1;
13100 }
13101
13102 /* Determines the delay between the bitmap updates.
13103 * It is expecting the value in seconds.
13104 */
13105 if (sysfs_set_num(info, NULL, "bitmap/time_base",
13106 __cpu_to_le64(IMSM_DEFAULT_BITMAP_DAEMON_SLEEP))) {
13107 dprintf("failed to set bitmap/time_base\n");
13108 return -1;
13109 }
13110
13111 /* It is expecting the value in sectors with a sign at the beginning. */
13112 if (sysfs_set_str(info, NULL, "bitmap/location", location)) {
13113 dprintf("failed to set bitmap/location\n");
13114 return -1;
13115 }
13116
13117 return 0;
13118 }
13119
13120 /*******************************************************************************
13121 * Function: set_bitmap_imsm
13122 * Description: Setup the bitmap for the given volume
13123 * Parameters:
13124 * st : supertype information
13125 * info : info about the volume where the bitmap should be setup
13126 *
13127 * Returns:
13128 * 0 : success
13129 * -1 : fail
13130 ******************************************************************************/
13131 static int set_bitmap_imsm(struct supertype *st, struct mdinfo *info)
13132 {
13133 struct intel_super *super = st->sb;
13134 int prev_current_vol = super->current_vol;
13135 struct imsm_dev *dev;
13136 int ret = -1;
13137 char location[16] = "";
13138 unsigned long long chunksize;
13139 struct intel_dev *dev_it;
13140
13141 for (dev_it = super->devlist; dev_it; dev_it = dev_it->next) {
13142 super->current_vol = dev_it->index;
13143 dev = get_imsm_dev(super, super->current_vol);
13144
13145 if (is_vol_to_setup_bitmap(info, dev)) {
13146 if (validate_internal_bitmap_imsm(st)) {
13147 dprintf("bitmap header validation failed\n");
13148 goto abort;
13149 }
13150
13151 chunksize = calculate_bitmap_chunksize(st, dev);
13152 dprintf("chunk size is %llu\n", chunksize);
13153
13154 snprintf(location, sizeof(location), "+%llu",
13155 get_bitmap_sector(super, super->current_vol));
13156 dprintf("bitmap offset is %s\n", location);
13157
13158 if (set_bitmap_sysfs(info, chunksize, location)) {
13159 dprintf("cannot setup the bitmap\n");
13160 goto abort;
13161 }
13162 }
13163 }
13164 ret = 0;
13165 abort:
13166 super->current_vol = prev_current_vol;
13167 return ret;
13168 }
13169
13170 struct superswitch super_imsm = {
13171 .examine_super = examine_super_imsm,
13172 .brief_examine_super = brief_examine_super_imsm,
13173 .brief_examine_subarrays = brief_examine_subarrays_imsm,
13174 .export_examine_super = export_examine_super_imsm,
13175 .detail_super = detail_super_imsm,
13176 .brief_detail_super = brief_detail_super_imsm,
13177 .write_init_super = write_init_super_imsm,
13178 .validate_geometry = validate_geometry_imsm,
13179 .add_to_super = add_to_super_imsm,
13180 .remove_from_super = remove_from_super_imsm,
13181 .detail_platform = detail_platform_imsm,
13182 .export_detail_platform = export_detail_platform_imsm,
13183 .kill_subarray = kill_subarray_imsm,
13184 .update_subarray = update_subarray_imsm,
13185 .load_container = load_container_imsm,
13186 .default_geometry = default_geometry_imsm,
13187 .test_and_add_drive_policies = test_and_add_drive_policies_imsm,
13188 .reshape_super = imsm_reshape_super,
13189 .manage_reshape = imsm_manage_reshape,
13190 .recover_backup = recover_backup_imsm,
13191 .examine_badblocks = examine_badblocks_imsm,
13192 .match_home = match_home_imsm,
13193 .uuid_from_super= uuid_from_super_imsm,
13194 .getinfo_super = getinfo_super_imsm,
13195 .getinfo_super_disks = getinfo_super_disks_imsm,
13196 .update_super = update_super_imsm,
13197
13198 .avail_size = avail_size_imsm,
13199 .get_spare_criteria = get_spare_criteria_imsm,
13200
13201 .compare_super = compare_super_imsm,
13202
13203 .load_super = load_super_imsm,
13204 .init_super = init_super_imsm,
13205 .store_super = store_super_imsm,
13206 .free_super = free_super_imsm,
13207 .match_metadata_desc = match_metadata_desc_imsm,
13208 .container_content = container_content_imsm,
13209 .validate_container = validate_container_imsm,
13210
13211 .add_internal_bitmap = add_internal_bitmap_imsm,
13212 .locate_bitmap = locate_bitmap_imsm,
13213 .write_bitmap = write_init_bitmap_imsm,
13214 .set_bitmap = set_bitmap_imsm,
13215
13216 .write_init_ppl = write_init_ppl_imsm,
13217 .validate_ppl = validate_ppl_imsm,
13218
13219 .external = 1,
13220 .swapuuid = 0,
13221 .name = "imsm",
13222
13223 /* for mdmon */
13224 .open_new = imsm_open_new,
13225 .set_array_state= imsm_set_array_state,
13226 .set_disk = imsm_set_disk,
13227 .sync_metadata = imsm_sync_metadata,
13228 .activate_spare = imsm_activate_spare,
13229 .process_update = imsm_process_update,
13230 .prepare_update = imsm_prepare_update,
13231 .record_bad_block = imsm_record_badblock,
13232 .clear_bad_block = imsm_clear_badblock,
13233 .get_bad_blocks = imsm_get_badblocks,
13234 };