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