]> git.ipfire.org Git - thirdparty/mdadm.git/blob - super-intel.c
40519f8fce2a77bd735a949690261afa1e44d142
[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 "drive_encryption.h"
25 #include "sha1.h"
26 #include "platform-intel.h"
27 #include "xmalloc.h"
28
29 #include <ctype.h>
30 #include <dirent.h>
31 #include <scsi/scsi.h>
32 #include <scsi/sg.h>
33 #include <string.h>
34 #include <sys/ioctl.h>
35 #include <values.h>
36
37 /* MPB == Metadata Parameter Block */
38 #define MPB_SIGNATURE "Intel Raid ISM Cfg Sig. "
39 #define MPB_SIG_LEN (strlen(MPB_SIGNATURE))
40
41 /* Legacy IMSM versions:
42 * MPB_VERSION_RAID0 1.0.00
43 * MPB_VERSION_RAID1 1.1.00
44 * MPB_VERSION_MANY_VOLUMES_PER_ARRAY 1.2.00
45 * MPB_VERSION_3OR4_DISK_ARRAY 1.2.01
46 * MPB_VERSION_RAID5 1.2.02
47 * MPB_VERSION_5OR6_DISK_ARRAY 1.2.04
48 * MPB_VERSION_CNG 1.2.06
49 */
50
51 #define MPB_VERSION_ATTRIBS "1.3.00"
52 #define MPB_VERSION_ATTRIBS_JD "2.0.00"
53 #define MAX_SIGNATURE_LENGTH 32
54 #define MAX_RAID_SERIAL_LEN 16
55
56 /* supports RAID0 */
57 #define MPB_ATTRIB_RAID0 __cpu_to_le32(0x00000001)
58 /* supports RAID1 */
59 #define MPB_ATTRIB_RAID1 __cpu_to_le32(0x00000002)
60 /* supports RAID10 */
61 #define MPB_ATTRIB_RAID10 __cpu_to_le32(0x00000004)
62 /* supports RAID1E */
63 #define MPB_ATTRIB_RAID1E __cpu_to_le32(0x00000008)
64 /* supports RAID5 */
65 #define MPB_ATTRIB_RAID5 __cpu_to_le32(0x00000010)
66 /* supports RAID CNG */
67 #define MPB_ATTRIB_RAIDCNG __cpu_to_le32(0x00000020)
68 /* supports expanded stripe sizes of 256K, 512K and 1MB */
69 #define MPB_ATTRIB_EXP_STRIPE_SIZE __cpu_to_le32(0x00000040)
70 /* supports RAID10 with more than 4 drives */
71 #define MPB_ATTRIB_RAID10_EXT __cpu_to_le32(0x00000080)
72
73 /* The OROM Support RST Caching of Volumes */
74 #define MPB_ATTRIB_NVM __cpu_to_le32(0x02000000)
75 /* The OROM supports creating disks greater than 2TB */
76 #define MPB_ATTRIB_2TB_DISK __cpu_to_le32(0x04000000)
77 /* The OROM supports Bad Block Management */
78 #define MPB_ATTRIB_BBM __cpu_to_le32(0x08000000)
79
80 /* THe OROM Supports NVM Caching of Volumes */
81 #define MPB_ATTRIB_NEVER_USE2 __cpu_to_le32(0x10000000)
82 /* The OROM supports creating volumes greater than 2TB */
83 #define MPB_ATTRIB_2TB __cpu_to_le32(0x20000000)
84 /* originally for PMP, now it's wasted b/c. Never use this bit! */
85 #define MPB_ATTRIB_NEVER_USE __cpu_to_le32(0x40000000)
86 /* Verify MPB contents against checksum after reading MPB */
87 #define MPB_ATTRIB_CHECKSUM_VERIFY __cpu_to_le32(0x80000000)
88
89 /* Define all supported attributes that have to be accepted by mdadm
90 */
91 #define MPB_ATTRIB_SUPPORTED (MPB_ATTRIB_CHECKSUM_VERIFY | \
92 MPB_ATTRIB_2TB | \
93 MPB_ATTRIB_2TB_DISK | \
94 MPB_ATTRIB_RAID0 | \
95 MPB_ATTRIB_RAID1 | \
96 MPB_ATTRIB_RAID10 | \
97 MPB_ATTRIB_RAID5 | \
98 MPB_ATTRIB_EXP_STRIPE_SIZE | \
99 MPB_ATTRIB_RAID10_EXT | \
100 MPB_ATTRIB_BBM)
101
102 /* Define attributes that are unused but not harmful */
103 #define MPB_ATTRIB_IGNORED (MPB_ATTRIB_NEVER_USE)
104
105 #define MPB_SECTOR_CNT 2210
106 #define IMSM_RESERVED_SECTORS 8192
107 #define NUM_BLOCKS_DIRTY_STRIPE_REGION 2048
108 #define SECT_PER_MB_SHIFT 11
109 #define MAX_SECTOR_SIZE 4096
110 #define MULTIPLE_PPL_AREA_SIZE_IMSM (1024 * 1024) /* Size of the whole
111 * mutliple PPL area
112 */
113
114 /*
115 * Internal Write-intent bitmap is stored in the same area where PPL.
116 * Both features are mutually exclusive, so it is not an issue.
117 * The first 8KiB of the area are reserved and shall not be used.
118 */
119 #define IMSM_BITMAP_AREA_RESERVED_SIZE 8192
120
121 #define IMSM_BITMAP_HEADER_OFFSET (IMSM_BITMAP_AREA_RESERVED_SIZE)
122 #define IMSM_BITMAP_HEADER_SIZE MAX_SECTOR_SIZE
123
124 #define IMSM_BITMAP_START_OFFSET (IMSM_BITMAP_HEADER_OFFSET + IMSM_BITMAP_HEADER_SIZE)
125 #define IMSM_BITMAP_AREA_SIZE (MULTIPLE_PPL_AREA_SIZE_IMSM - IMSM_BITMAP_START_OFFSET)
126 #define IMSM_BITMAP_AND_HEADER_SIZE (IMSM_BITMAP_AREA_SIZE + IMSM_BITMAP_HEADER_SIZE)
127
128 #define IMSM_DEFAULT_BITMAP_CHUNKSIZE (64 * 1024 * 1024)
129 #define IMSM_DEFAULT_BITMAP_DAEMON_SLEEP 5
130
131 /*
132 * This macro let's us ensure that no-one accidentally
133 * changes the size of a struct
134 */
135 #define ASSERT_SIZE(_struct, size) \
136 static inline void __assert_size_##_struct(void) \
137 { \
138 switch (0) { \
139 case 0: break; \
140 case (sizeof(struct _struct) == size): break; \
141 } \
142 }
143
144 /* Disk configuration info. */
145 #define IMSM_MAX_DEVICES 255
146 struct imsm_disk {
147 __u8 serial[MAX_RAID_SERIAL_LEN];/* 0xD8 - 0xE7 ascii serial number */
148 __u32 total_blocks_lo; /* 0xE8 - 0xEB total blocks lo */
149 __u32 scsi_id; /* 0xEC - 0xEF scsi ID */
150 #define SPARE_DISK __cpu_to_le32(0x01) /* Spare */
151 #define CONFIGURED_DISK __cpu_to_le32(0x02) /* Member of some RaidDev */
152 #define FAILED_DISK __cpu_to_le32(0x04) /* Permanent failure */
153 #define JOURNAL_DISK __cpu_to_le32(0x2000000) /* Device marked as Journaling Drive */
154 __u32 status; /* 0xF0 - 0xF3 */
155 __u32 owner_cfg_num; /* which config 0,1,2... owns this disk */
156 __u32 total_blocks_hi; /* 0xF4 - 0xF5 total blocks hi */
157 #define IMSM_DISK_FILLERS 3
158 __u32 filler[IMSM_DISK_FILLERS]; /* 0xF5 - 0x107 MPB_DISK_FILLERS for future expansion */
159 };
160 ASSERT_SIZE(imsm_disk, 48)
161
162 /* map selector for map managment
163 */
164 #define MAP_0 0
165 #define MAP_1 1
166 #define MAP_X -1
167
168 /* RAID map configuration infos. */
169 struct imsm_map {
170 __u32 pba_of_lba0_lo; /* start address of partition */
171 __u32 blocks_per_member_lo;/* blocks per member */
172 __u32 num_data_stripes_lo; /* number of data stripes */
173 __u16 blocks_per_strip;
174 __u8 map_state; /* Normal, Uninitialized, Degraded, Failed */
175 #define IMSM_T_STATE_NORMAL 0
176 #define IMSM_T_STATE_UNINITIALIZED 1
177 #define IMSM_T_STATE_DEGRADED 2
178 #define IMSM_T_STATE_FAILED 3
179 __u8 raid_level;
180 #define IMSM_T_RAID0 0
181 #define IMSM_T_RAID1 1
182 #define IMSM_T_RAID5 5
183 #define IMSM_T_RAID10 10
184 __u8 num_members; /* number of member disks */
185 __u8 num_domains; /* number of parity domains */
186 __u8 failed_disk_num; /* valid only when state is degraded */
187 __u8 ddf;
188 __u32 pba_of_lba0_hi;
189 __u32 blocks_per_member_hi;
190 __u32 num_data_stripes_hi;
191 __u32 filler[4]; /* expansion area */
192 #define IMSM_ORD_REBUILD (1 << 24)
193 __u32 disk_ord_tbl[1]; /* disk_ord_tbl[num_members],
194 * top byte contains some flags
195 */
196 };
197 ASSERT_SIZE(imsm_map, 52)
198
199 struct imsm_vol {
200 __u32 curr_migr_unit_lo;
201 __u32 checkpoint_id; /* id to access curr_migr_unit */
202 #define MIGR_STATE_NORMAL 0
203 #define MIGR_STATE_MIGRATING 1
204 __u8 migr_state; /* Normal or Migrating */
205 #define MIGR_INIT 0
206 #define MIGR_REBUILD 1
207 #define MIGR_VERIFY 2 /* analagous to echo check > sync_action */
208 #define MIGR_GEN_MIGR 3
209 #define MIGR_STATE_CHANGE 4
210 #define MIGR_REPAIR 5
211 __u8 migr_type; /* Initializing, Rebuilding, ... */
212 #define RAIDVOL_CLEAN 0
213 #define RAIDVOL_DIRTY 1
214 #define RAIDVOL_DSRECORD_VALID 2
215 __u8 dirty;
216 __u8 fs_state; /* fast-sync state for CnG (0xff == disabled) */
217 __u16 verify_errors; /* number of mismatches */
218 __u16 bad_blocks; /* number of bad blocks during verify */
219 __u32 curr_migr_unit_hi;
220 __u32 filler[3];
221 struct imsm_map map[1];
222 /* here comes another one if migr_state */
223 };
224 ASSERT_SIZE(imsm_vol, 84)
225
226 struct imsm_dev {
227 __u8 volume[MAX_RAID_SERIAL_LEN];
228 __u32 size_low;
229 __u32 size_high;
230 #define DEV_BOOTABLE __cpu_to_le32(0x01)
231 #define DEV_BOOT_DEVICE __cpu_to_le32(0x02)
232 #define DEV_READ_COALESCING __cpu_to_le32(0x04)
233 #define DEV_WRITE_COALESCING __cpu_to_le32(0x08)
234 #define DEV_LAST_SHUTDOWN_DIRTY __cpu_to_le32(0x10)
235 #define DEV_HIDDEN_AT_BOOT __cpu_to_le32(0x20)
236 #define DEV_CURRENTLY_HIDDEN __cpu_to_le32(0x40)
237 #define DEV_VERIFY_AND_FIX __cpu_to_le32(0x80)
238 #define DEV_MAP_STATE_UNINIT __cpu_to_le32(0x100)
239 #define DEV_NO_AUTO_RECOVERY __cpu_to_le32(0x200)
240 #define DEV_CLONE_N_GO __cpu_to_le32(0x400)
241 #define DEV_CLONE_MAN_SYNC __cpu_to_le32(0x800)
242 #define DEV_CNG_MASTER_DISK_NUM __cpu_to_le32(0x1000)
243 __u32 status; /* Persistent RaidDev status */
244 __u32 reserved_blocks; /* Reserved blocks at beginning of volume */
245 __u8 migr_priority;
246 __u8 num_sub_vols;
247 __u8 tid;
248 __u8 cng_master_disk;
249 __u16 cache_policy;
250 __u8 cng_state;
251 __u8 cng_sub_state;
252 __u16 my_vol_raid_dev_num; /* Used in Unique volume Id for this RaidDev */
253
254 /* NVM_EN */
255 __u8 nv_cache_mode;
256 __u8 nv_cache_flags;
257
258 /* Unique Volume Id of the NvCache Volume associated with this volume */
259 __u32 nvc_vol_orig_family_num;
260 __u16 nvc_vol_raid_dev_num;
261
262 #define RWH_OFF 0
263 #define RWH_DISTRIBUTED 1
264 #define RWH_JOURNALING_DRIVE 2
265 #define RWH_MULTIPLE_DISTRIBUTED 3
266 #define RWH_MULTIPLE_PPLS_JOURNALING_DRIVE 4
267 #define RWH_MULTIPLE_OFF 5
268 #define RWH_BITMAP 6
269 __u8 rwh_policy; /* Raid Write Hole Policy */
270 __u8 jd_serial[MAX_RAID_SERIAL_LEN]; /* Journal Drive serial number */
271 __u8 filler1;
272
273 #define IMSM_DEV_FILLERS 3
274 __u32 filler[IMSM_DEV_FILLERS];
275 struct imsm_vol vol;
276 };
277 ASSERT_SIZE(imsm_dev, 164)
278
279 struct imsm_super {
280 __u8 sig[MAX_SIGNATURE_LENGTH]; /* 0x00 - 0x1F */
281 __u32 check_sum; /* 0x20 - 0x23 MPB Checksum */
282 __u32 mpb_size; /* 0x24 - 0x27 Size of MPB */
283 __u32 family_num; /* 0x28 - 0x2B Checksum from first time this config was written */
284 __u32 generation_num; /* 0x2C - 0x2F Incremented each time this array's MPB is written */
285 __u32 error_log_size; /* 0x30 - 0x33 in bytes */
286 __u32 attributes; /* 0x34 - 0x37 */
287 __u8 num_disks; /* 0x38 Number of configured disks */
288 __u8 num_raid_devs; /* 0x39 Number of configured volumes */
289 __u8 error_log_pos; /* 0x3A */
290 __u8 fill[1]; /* 0x3B */
291 __u32 cache_size; /* 0x3c - 0x40 in mb */
292 __u32 orig_family_num; /* 0x40 - 0x43 original family num */
293 __u32 pwr_cycle_count; /* 0x44 - 0x47 simulated power cycle count for array */
294 __u32 bbm_log_size; /* 0x48 - 0x4B - size of bad Block Mgmt Log in bytes */
295 __u16 num_raid_devs_created; /* 0x4C - 0x4D Used for generating unique
296 * volume IDs for raid_dev created in this array
297 * (starts at 1)
298 */
299 __u16 filler1; /* 0x4E - 0x4F */
300 __u64 creation_time; /* 0x50 - 0x57 Array creation time */
301 #define IMSM_FILLERS 32
302 __u32 filler[IMSM_FILLERS]; /* 0x58 - 0xD7 RAID_MPB_FILLERS */
303 struct imsm_disk disk[1]; /* 0xD8 diskTbl[numDisks] */
304 /* here comes imsm_dev[num_raid_devs] */
305 /* here comes BBM logs */
306 };
307 ASSERT_SIZE(imsm_super, 264)
308
309 #define BBM_LOG_MAX_ENTRIES 254
310 #define BBM_LOG_MAX_LBA_ENTRY_VAL 256 /* Represents 256 LBAs */
311 #define BBM_LOG_SIGNATURE 0xabadb10c
312
313 struct bbm_log_block_addr {
314 __u16 w1;
315 __u32 dw1;
316 } __attribute__ ((__packed__));
317
318 struct bbm_log_entry {
319 __u8 marked_count; /* Number of blocks marked - 1 */
320 __u8 disk_ordinal; /* Disk entry within the imsm_super */
321 struct bbm_log_block_addr defective_block_start;
322 } __attribute__ ((__packed__));
323
324 struct bbm_log {
325 __u32 signature; /* 0xABADB10C */
326 __u32 entry_count;
327 struct bbm_log_entry marked_block_entries[BBM_LOG_MAX_ENTRIES];
328 };
329 ASSERT_SIZE(bbm_log, 2040)
330
331 static char *map_state_str[] = { "normal", "uninitialized", "degraded", "failed" };
332
333 #define BLOCKS_PER_KB (1024/512)
334
335 #define RAID_DISK_RESERVED_BLOCKS_IMSM_HI 2209
336
337 #define GEN_MIGR_AREA_SIZE 2048 /* General Migration Copy Area size in blocks */
338
339 #define MIGR_REC_BUF_SECTORS 1 /* size of migr_record i/o buffer in sectors */
340 #define MIGR_REC_SECTOR_POSITION 1 /* migr_record position offset on disk,
341 * MIGR_REC_BUF_SECTORS <= MIGR_REC_SECTOR_POS
342 */
343
344 #define UNIT_SRC_NORMAL 0 /* Source data for curr_migr_unit must
345 * be recovered using srcMap */
346 #define UNIT_SRC_IN_CP_AREA 1 /* Source data for curr_migr_unit has
347 * already been migrated and must
348 * be recovered from checkpoint area */
349
350 #define PPL_ENTRY_SPACE (128 * 1024) /* Size of single PPL, without the header */
351
352 struct migr_record {
353 __u32 rec_status; /* Status used to determine how to restart
354 * migration in case it aborts
355 * in some fashion */
356 __u32 curr_migr_unit_lo; /* 0..numMigrUnits-1 */
357 __u32 family_num; /* Family number of MPB
358 * containing the RaidDev
359 * that is migrating */
360 __u32 ascending_migr; /* True if migrating in increasing
361 * order of lbas */
362 __u32 blocks_per_unit; /* Num disk blocks per unit of operation */
363 __u32 dest_depth_per_unit; /* Num member blocks each destMap
364 * member disk
365 * advances per unit-of-operation */
366 __u32 ckpt_area_pba_lo; /* Pba of first block of ckpt copy area */
367 __u32 dest_1st_member_lba_lo; /* First member lba on first
368 * stripe of destination */
369 __u32 num_migr_units_lo; /* Total num migration units-of-op */
370 __u32 post_migr_vol_cap; /* Size of volume after
371 * migration completes */
372 __u32 post_migr_vol_cap_hi; /* Expansion space for LBA64 */
373 __u32 ckpt_read_disk_num; /* Which member disk in destSubMap[0] the
374 * migration ckpt record was read from
375 * (for recovered migrations) */
376 __u32 curr_migr_unit_hi; /* 0..numMigrUnits-1 high order 32 bits */
377 __u32 ckpt_area_pba_hi; /* Pba of first block of ckpt copy area
378 * high order 32 bits */
379 __u32 dest_1st_member_lba_hi; /* First member lba on first stripe of
380 * destination - high order 32 bits */
381 __u32 num_migr_units_hi; /* Total num migration units-of-op
382 * high order 32 bits */
383 __u32 filler[16];
384 };
385 ASSERT_SIZE(migr_record, 128)
386
387 /**
388 * enum imsm_status - internal IMSM return values representation.
389 * @STATUS_OK: function succeeded.
390 * @STATUS_ERROR: General error ocurred (not specified).
391 *
392 * Typedefed to imsm_status_t.
393 */
394 typedef enum imsm_status {
395 IMSM_STATUS_ERROR = -1,
396 IMSM_STATUS_OK = 0,
397 } imsm_status_t;
398
399 struct md_list {
400 /* usage marker:
401 * 1: load metadata
402 * 2: metadata does not match
403 * 4: already checked
404 */
405 int used;
406 char *devname;
407 int found;
408 int container;
409 dev_t st_rdev;
410 struct md_list *next;
411 };
412
413 static __u8 migr_type(struct imsm_dev *dev)
414 {
415 if (dev->vol.migr_type == MIGR_VERIFY &&
416 dev->status & DEV_VERIFY_AND_FIX)
417 return MIGR_REPAIR;
418 else
419 return dev->vol.migr_type;
420 }
421
422 static void set_migr_type(struct imsm_dev *dev, __u8 migr_type)
423 {
424 /* for compatibility with older oroms convert MIGR_REPAIR, into
425 * MIGR_VERIFY w/ DEV_VERIFY_AND_FIX status
426 */
427 if (migr_type == MIGR_REPAIR) {
428 dev->vol.migr_type = MIGR_VERIFY;
429 dev->status |= DEV_VERIFY_AND_FIX;
430 } else {
431 dev->vol.migr_type = migr_type;
432 dev->status &= ~DEV_VERIFY_AND_FIX;
433 }
434 }
435
436 static unsigned int sector_count(__u32 bytes, unsigned int sector_size)
437 {
438 return ROUND_UP(bytes, sector_size) / sector_size;
439 }
440
441 static unsigned int mpb_sectors(struct imsm_super *mpb,
442 unsigned int sector_size)
443 {
444 return sector_count(__le32_to_cpu(mpb->mpb_size), sector_size);
445 }
446
447 struct intel_dev {
448 struct imsm_dev *dev;
449 struct intel_dev *next;
450 unsigned index;
451 };
452
453 struct intel_hba {
454 enum sys_dev_type type;
455 char *path;
456 char *pci_id;
457 struct intel_hba *next;
458 };
459
460 enum action {
461 DISK_REMOVE = 1,
462 DISK_ADD
463 };
464 /* internal representation of IMSM metadata */
465 struct intel_super {
466 union {
467 void *buf; /* O_DIRECT buffer for reading/writing metadata */
468 struct imsm_super *anchor; /* immovable parameters */
469 };
470 union {
471 void *migr_rec_buf; /* buffer for I/O operations */
472 struct migr_record *migr_rec; /* migration record */
473 };
474 int clean_migration_record_by_mdmon; /* when reshape is switched to next
475 array, it indicates that mdmon is allowed to clean migration
476 record */
477 size_t len; /* size of the 'buf' allocation */
478 size_t extra_space; /* extra space in 'buf' that is not used yet */
479 void *next_buf; /* for realloc'ing buf from the manager */
480 size_t next_len;
481 int updates_pending; /* count of pending updates for mdmon */
482 int current_vol; /* index of raid device undergoing creation */
483 unsigned long long create_offset; /* common start for 'current_vol' */
484 __u32 random; /* random data for seeding new family numbers */
485 struct intel_dev *devlist;
486 unsigned int sector_size; /* sector size of used member drives */
487 struct dl {
488 struct dl *next;
489 int index;
490 __u8 serial[MAX_RAID_SERIAL_LEN];
491 int major, minor;
492 char *devname;
493 struct imsm_disk disk;
494 int fd;
495 int extent_cnt;
496 struct extent *e; /* for determining freespace @ create */
497 int raiddisk; /* slot to fill in autolayout */
498 enum action action;
499 } *disks, *current_disk;
500 struct dl *disk_mgmt_list; /* list of disks to add/remove while mdmon
501 active */
502 struct dl *missing; /* disks removed while we weren't looking */
503 struct bbm_log *bbm_log;
504 struct intel_hba *hba; /* device path of the raid controller for this metadata */
505 const struct imsm_orom *orom; /* platform firmware support */
506 struct intel_super *next; /* (temp) list for disambiguating family_num */
507 struct md_bb bb; /* memory for get_bad_blocks call */
508 };
509
510 struct intel_disk {
511 struct imsm_disk disk;
512 #define IMSM_UNKNOWN_OWNER (-1)
513 int owner;
514 struct intel_disk *next;
515 };
516
517 /**
518 * struct extent - reserved space details.
519 * @start: start offset.
520 * @size: size of reservation, set to 0 for metadata reservation.
521 * @vol: index of the volume, meaningful if &size is set.
522 */
523 struct extent {
524 unsigned long long start, size;
525 int vol;
526 };
527
528 /* definitions of reshape process types */
529 enum imsm_reshape_type {
530 CH_TAKEOVER,
531 CH_MIGRATION,
532 CH_ARRAY_SIZE,
533 CH_ABORT
534 };
535
536 /* definition of messages passed to imsm_process_update */
537 enum imsm_update_type {
538 update_activate_spare,
539 update_create_array,
540 update_kill_array,
541 update_rename_array,
542 update_add_remove_disk,
543 update_reshape_container_disks,
544 update_reshape_migration,
545 update_takeover,
546 update_general_migration_checkpoint,
547 update_size_change,
548 update_prealloc_badblocks_mem,
549 update_rwh_policy,
550 };
551
552 struct imsm_update_activate_spare {
553 enum imsm_update_type type;
554 struct dl *dl;
555 int slot;
556 int array;
557 struct imsm_update_activate_spare *next;
558 };
559
560 struct geo_params {
561 char devnm[32];
562 char *dev_name;
563 unsigned long long size;
564 int level;
565 int layout;
566 int chunksize;
567 int raid_disks;
568 };
569
570 enum takeover_direction {
571 R10_TO_R0,
572 R0_TO_R10
573 };
574 struct imsm_update_takeover {
575 enum imsm_update_type type;
576 int subarray;
577 enum takeover_direction direction;
578 };
579
580 struct imsm_update_reshape {
581 enum imsm_update_type type;
582 int old_raid_disks;
583 int new_raid_disks;
584
585 int new_disks[1]; /* new_raid_disks - old_raid_disks makedev number */
586 };
587
588 struct imsm_update_reshape_migration {
589 enum imsm_update_type type;
590 int old_raid_disks;
591 int new_raid_disks;
592 /* fields for array migration changes
593 */
594 int subdev;
595 int new_level;
596 int new_layout;
597 int new_chunksize;
598
599 int new_disks[1]; /* new_raid_disks - old_raid_disks makedev number */
600 };
601
602 struct imsm_update_size_change {
603 enum imsm_update_type type;
604 int subdev;
605 long long new_size;
606 };
607
608 struct imsm_update_general_migration_checkpoint {
609 enum imsm_update_type type;
610 __u64 curr_migr_unit;
611 };
612
613 struct disk_info {
614 __u8 serial[MAX_RAID_SERIAL_LEN];
615 };
616
617 struct imsm_update_create_array {
618 enum imsm_update_type type;
619 int dev_idx;
620 struct imsm_dev dev;
621 };
622
623 struct imsm_update_kill_array {
624 enum imsm_update_type type;
625 int dev_idx;
626 };
627
628 struct imsm_update_rename_array {
629 enum imsm_update_type type;
630 __u8 name[MAX_RAID_SERIAL_LEN];
631 int dev_idx;
632 };
633
634 struct imsm_update_add_remove_disk {
635 enum imsm_update_type type;
636 };
637
638 struct imsm_update_prealloc_bb_mem {
639 enum imsm_update_type type;
640 };
641
642 struct imsm_update_rwh_policy {
643 enum imsm_update_type type;
644 int new_policy;
645 int dev_idx;
646 };
647
648 enum imsm_sku {
649 SKU_NO_KEY = 0,
650 SKU_STANDARD_KEY = 1,
651 SKU_PREMIUM_KEY = 2,
652 SKU_INTEL_SSD_ONLY_KEY = 3,
653 SKU_RAID1_ONLY_KEY = 4
654 };
655
656 static const char *_sys_dev_type[] = {
657 [SYS_DEV_UNKNOWN] = "Unknown",
658 [SYS_DEV_SAS] = "SAS",
659 [SYS_DEV_SATA] = "SATA",
660 [SYS_DEV_NVME] = "NVMe",
661 [SYS_DEV_VMD] = "VMD",
662 [SYS_DEV_SATA_VMD] = "SATA VMD"
663 };
664
665 struct imsm_chunk_ops {
666 uint chunk;
667 char *chunk_str;
668 };
669
670 static const struct imsm_chunk_ops imsm_chunk_ops[] = {
671 {IMSM_OROM_SSS_2kB, "2k"},
672 {IMSM_OROM_SSS_4kB, "4k"},
673 {IMSM_OROM_SSS_8kB, "8k"},
674 {IMSM_OROM_SSS_16kB, "16k"},
675 {IMSM_OROM_SSS_32kB, "32k"},
676 {IMSM_OROM_SSS_64kB, "64k"},
677 {IMSM_OROM_SSS_128kB, "128k"},
678 {IMSM_OROM_SSS_256kB, "256k"},
679 {IMSM_OROM_SSS_512kB, "512k"},
680 {IMSM_OROM_SSS_1MB, "1M"},
681 {IMSM_OROM_SSS_2MB, "2M"},
682 {IMSM_OROM_SSS_4MB, "4M"},
683 {IMSM_OROM_SSS_8MB, "8M"},
684 {IMSM_OROM_SSS_16MB, "16M"},
685 {IMSM_OROM_SSS_32MB, "32M"},
686 {IMSM_OROM_SSS_64MB, "64M"},
687 {0, NULL}
688 };
689
690 static int no_platform = -1;
691
692 static int check_no_platform(void)
693 {
694 static const char search[] = "mdadm.imsm.test=1";
695 FILE *fp;
696
697 if (no_platform >= 0)
698 return no_platform;
699
700 if (check_env("IMSM_NO_PLATFORM")) {
701 no_platform = 1;
702 return 1;
703 }
704 fp = fopen("/proc/cmdline", "r");
705 if (fp) {
706 char *l = conf_line(fp);
707 char *w = l;
708
709 if (l == NULL) {
710 fclose(fp);
711 return 0;
712 }
713
714 do {
715 if (strcmp(w, search) == 0)
716 no_platform = 1;
717 w = dl_next(w);
718 } while (w != l);
719 free_line(l);
720 fclose(fp);
721 if (no_platform >= 0)
722 return no_platform;
723 }
724 no_platform = 0;
725 return 0;
726 }
727
728 void imsm_set_no_platform(int v)
729 {
730 no_platform = v;
731 }
732
733 const char *get_sys_dev_type(enum sys_dev_type type)
734 {
735 if (type >= SYS_DEV_MAX)
736 type = SYS_DEV_UNKNOWN;
737
738 return _sys_dev_type[type];
739 }
740
741 static struct intel_hba * alloc_intel_hba(struct sys_dev *device)
742 {
743 struct intel_hba *result = xmalloc(sizeof(*result));
744
745 result->type = device->type;
746 result->path = xstrdup(device->path);
747 result->next = NULL;
748 if (result->path && (result->pci_id = strrchr(result->path, '/')) != NULL)
749 result->pci_id++;
750
751 return result;
752 }
753
754 static struct intel_hba * find_intel_hba(struct intel_hba *hba, struct sys_dev *device)
755 {
756 struct intel_hba *result;
757
758 for (result = hba; result; result = result->next) {
759 if (result->type == device->type && strcmp(result->path, device->path) == 0)
760 break;
761 }
762 return result;
763 }
764
765 static int attach_hba_to_super(struct intel_super *super, struct sys_dev *device)
766 {
767 struct intel_hba *hba;
768
769 /* check if disk attached to Intel HBA */
770 hba = find_intel_hba(super->hba, device);
771 if (hba != NULL)
772 return 1;
773 /* Check if HBA is already attached to super */
774 if (super->hba == NULL) {
775 super->hba = alloc_intel_hba(device);
776 return 1;
777 }
778
779 hba = super->hba;
780 /* Intel metadata allows for all disks attached to the same type HBA.
781 * Do not support HBA types mixing
782 */
783 if (device->type != hba->type)
784 return 2;
785
786 /* Multiple same type HBAs can be used if they share the same OROM */
787 const struct imsm_orom *device_orom = get_orom_by_device_id(device->dev_id);
788
789 if (device_orom != super->orom)
790 return 2;
791
792 while (hba->next)
793 hba = hba->next;
794
795 hba->next = alloc_intel_hba(device);
796 return 1;
797 }
798
799 static struct sys_dev* find_disk_attached_hba(int fd, const char *devname)
800 {
801 struct sys_dev *list, *elem;
802 char *disk_path;
803
804 if ((list = find_intel_devices()) == NULL)
805 return 0;
806
807 if (!is_fd_valid(fd))
808 disk_path = (char *) devname;
809 else
810 disk_path = diskfd_to_devpath(fd, 1, NULL);
811
812 if (!disk_path)
813 return 0;
814
815 for (elem = list; elem; elem = elem->next)
816 if (is_path_attached_to_hba(disk_path, elem->path))
817 break;
818
819 if (disk_path != devname)
820 free(disk_path);
821
822 return elem;
823 }
824
825 static int find_intel_hba_capability(int fd, struct intel_super *super,
826 char *devname);
827
828 static struct supertype *match_metadata_desc_imsm(char *arg)
829 {
830 struct supertype *st;
831
832 if (strcmp(arg, "imsm") != 0 &&
833 strcmp(arg, "default") != 0
834 )
835 return NULL;
836
837 st = xcalloc(1, sizeof(*st));
838 st->ss = &super_imsm;
839 st->max_devs = IMSM_MAX_DEVICES;
840 st->minor_version = 0;
841 st->sb = NULL;
842 return st;
843 }
844
845 static __u8 *get_imsm_version(struct imsm_super *mpb)
846 {
847 return &mpb->sig[MPB_SIG_LEN];
848 }
849
850 /* retrieve a disk directly from the anchor when the anchor is known to be
851 * up-to-date, currently only at load time
852 */
853 static struct imsm_disk *__get_imsm_disk(struct imsm_super *mpb, __u8 index)
854 {
855 if (index >= mpb->num_disks)
856 return NULL;
857 return &mpb->disk[index];
858 }
859
860 /* retrieve the disk description based on a index of the disk
861 * in the sub-array
862 */
863 static struct dl *get_imsm_dl_disk(struct intel_super *super, __u8 index)
864 {
865 struct dl *d;
866
867 for (d = super->disks; d; d = d->next)
868 if (d->index == index)
869 return d;
870
871 return NULL;
872 }
873 /* retrieve a disk from the parsed metadata */
874 static struct imsm_disk *get_imsm_disk(struct intel_super *super, __u8 index)
875 {
876 struct dl *dl;
877
878 dl = get_imsm_dl_disk(super, index);
879 if (dl)
880 return &dl->disk;
881
882 return NULL;
883 }
884
885 /* generate a checksum directly from the anchor when the anchor is known to be
886 * up-to-date, currently only at load or write_super after coalescing
887 */
888 static __u32 __gen_imsm_checksum(struct imsm_super *mpb)
889 {
890 __u32 end = mpb->mpb_size / sizeof(end);
891 __u32 *p = (__u32 *) mpb;
892 __u32 sum = 0;
893
894 while (end--) {
895 sum += __le32_to_cpu(*p);
896 p++;
897 }
898
899 return sum - __le32_to_cpu(mpb->check_sum);
900 }
901
902 static size_t sizeof_imsm_map(struct imsm_map *map)
903 {
904 return sizeof(struct imsm_map) + sizeof(__u32) * (map->num_members - 1);
905 }
906
907 struct imsm_map *get_imsm_map(struct imsm_dev *dev, int second_map)
908 {
909 /* A device can have 2 maps if it is in the middle of a migration.
910 * If second_map is:
911 * MAP_0 - we return the first map
912 * MAP_1 - we return the second map if it exists, else NULL
913 * MAP_X - we return the second map if it exists, else the first
914 */
915 struct imsm_map *map = &dev->vol.map[0];
916 struct imsm_map *map2 = NULL;
917
918 if (dev->vol.migr_state)
919 map2 = (void *)map + sizeof_imsm_map(map);
920
921 switch (second_map) {
922 case MAP_0:
923 break;
924 case MAP_1:
925 map = map2;
926 break;
927 case MAP_X:
928 if (map2)
929 map = map2;
930 break;
931 default:
932 map = NULL;
933 }
934 return map;
935
936 }
937
938 /* return the size of the device.
939 * migr_state increases the returned size if map[0] were to be duplicated
940 */
941 static size_t sizeof_imsm_dev(struct imsm_dev *dev, int migr_state)
942 {
943 size_t size = sizeof(*dev) - sizeof(struct imsm_map) +
944 sizeof_imsm_map(get_imsm_map(dev, MAP_0));
945
946 /* migrating means an additional map */
947 if (dev->vol.migr_state)
948 size += sizeof_imsm_map(get_imsm_map(dev, MAP_1));
949 else if (migr_state)
950 size += sizeof_imsm_map(get_imsm_map(dev, MAP_0));
951
952 return size;
953 }
954
955 /* retrieve disk serial number list from a metadata update */
956 static struct disk_info *get_disk_info(struct imsm_update_create_array *update)
957 {
958 void *u = update;
959 struct disk_info *inf;
960
961 inf = u + sizeof(*update) - sizeof(struct imsm_dev) +
962 sizeof_imsm_dev(&update->dev, 0);
963
964 return inf;
965 }
966
967 /**
968 * __get_imsm_dev() - Get device with index from imsm_super.
969 * @mpb: &imsm_super pointer, not NULL.
970 * @index: Device index.
971 *
972 * Function works as non-NULL, aborting in such a case,
973 * when NULL would be returned.
974 *
975 * Device index should be in range 0 up to num_raid_devs.
976 * Function assumes the index was already verified.
977 * Index must be valid, otherwise abort() is called.
978 *
979 * Return: Pointer to corresponding imsm_dev.
980 *
981 */
982 static struct imsm_dev *__get_imsm_dev(struct imsm_super *mpb, __u8 index)
983 {
984 int offset;
985 int i;
986 void *_mpb = mpb;
987
988 if (index >= mpb->num_raid_devs)
989 goto error;
990
991 /* devices start after all disks */
992 offset = ((void *) &mpb->disk[mpb->num_disks]) - _mpb;
993
994 for (i = 0; i <= index; i++, offset += sizeof_imsm_dev(_mpb + offset, 0))
995 if (i == index)
996 return _mpb + offset;
997 error:
998 pr_err("cannot find imsm_dev with index %u in imsm_super\n", index);
999 abort();
1000 }
1001
1002 /**
1003 * get_imsm_dev() - Get device with index from intel_super.
1004 * @super: &intel_super pointer, not NULL.
1005 * @index: Device index.
1006 *
1007 * Function works as non-NULL, aborting in such a case,
1008 * when NULL would be returned.
1009 *
1010 * Device index should be in range 0 up to num_raid_devs.
1011 * Function assumes the index was already verified.
1012 * Index must be valid, otherwise abort() is called.
1013 *
1014 * Return: Pointer to corresponding imsm_dev.
1015 *
1016 */
1017 static struct imsm_dev *get_imsm_dev(struct intel_super *super, __u8 index)
1018 {
1019 struct intel_dev *dv;
1020
1021 if (index >= super->anchor->num_raid_devs)
1022 goto error;
1023
1024 for (dv = super->devlist; dv; dv = dv->next)
1025 if (dv->index == index)
1026 return dv->dev;
1027 error:
1028 pr_err("cannot find imsm_dev with index %u in intel_super\n", index);
1029 abort();
1030 }
1031
1032 static inline unsigned long long __le48_to_cpu(const struct bbm_log_block_addr
1033 *addr)
1034 {
1035 return ((((__u64)__le32_to_cpu(addr->dw1)) << 16) |
1036 __le16_to_cpu(addr->w1));
1037 }
1038
1039 static inline struct bbm_log_block_addr __cpu_to_le48(unsigned long long sec)
1040 {
1041 struct bbm_log_block_addr addr;
1042
1043 addr.w1 = __cpu_to_le16((__u16)(sec & 0xffff));
1044 addr.dw1 = __cpu_to_le32((__u32)(sec >> 16) & 0xffffffff);
1045 return addr;
1046 }
1047
1048 /* get size of the bbm log */
1049 static __u32 get_imsm_bbm_log_size(struct bbm_log *log)
1050 {
1051 if (!log || log->entry_count == 0)
1052 return 0;
1053
1054 return sizeof(log->signature) +
1055 sizeof(log->entry_count) +
1056 log->entry_count * sizeof(struct bbm_log_entry);
1057 }
1058
1059 /* check if bad block is not partially stored in bbm log */
1060 static int is_stored_in_bbm(struct bbm_log *log, const __u8 idx, const unsigned
1061 long long sector, const int length, __u32 *pos)
1062 {
1063 __u32 i;
1064
1065 for (i = *pos; i < log->entry_count; i++) {
1066 struct bbm_log_entry *entry = &log->marked_block_entries[i];
1067 unsigned long long bb_start;
1068 unsigned long long bb_end;
1069
1070 bb_start = __le48_to_cpu(&entry->defective_block_start);
1071 bb_end = bb_start + (entry->marked_count + 1);
1072
1073 if ((entry->disk_ordinal == idx) && (bb_start >= sector) &&
1074 (bb_end <= sector + length)) {
1075 *pos = i;
1076 return 1;
1077 }
1078 }
1079 return 0;
1080 }
1081
1082 /* record new bad block in bbm log */
1083 static int record_new_badblock(struct bbm_log *log, const __u8 idx, unsigned
1084 long long sector, int length)
1085 {
1086 int new_bb = 0;
1087 __u32 pos = 0;
1088 struct bbm_log_entry *entry = NULL;
1089
1090 while (is_stored_in_bbm(log, idx, sector, length, &pos)) {
1091 struct bbm_log_entry *e = &log->marked_block_entries[pos];
1092
1093 if ((e->marked_count + 1 == BBM_LOG_MAX_LBA_ENTRY_VAL) &&
1094 (__le48_to_cpu(&e->defective_block_start) == sector)) {
1095 sector += BBM_LOG_MAX_LBA_ENTRY_VAL;
1096 length -= BBM_LOG_MAX_LBA_ENTRY_VAL;
1097 pos = pos + 1;
1098 continue;
1099 }
1100 entry = e;
1101 break;
1102 }
1103
1104 if (entry) {
1105 int cnt = (length <= BBM_LOG_MAX_LBA_ENTRY_VAL) ? length :
1106 BBM_LOG_MAX_LBA_ENTRY_VAL;
1107 entry->defective_block_start = __cpu_to_le48(sector);
1108 entry->marked_count = cnt - 1;
1109 if (cnt == length)
1110 return 1;
1111 sector += cnt;
1112 length -= cnt;
1113 }
1114
1115 new_bb = ROUND_UP(length, BBM_LOG_MAX_LBA_ENTRY_VAL) /
1116 BBM_LOG_MAX_LBA_ENTRY_VAL;
1117 if (log->entry_count + new_bb > BBM_LOG_MAX_ENTRIES)
1118 return 0;
1119
1120 while (length > 0) {
1121 int cnt = (length <= BBM_LOG_MAX_LBA_ENTRY_VAL) ? length :
1122 BBM_LOG_MAX_LBA_ENTRY_VAL;
1123 struct bbm_log_entry *entry =
1124 &log->marked_block_entries[log->entry_count];
1125
1126 entry->defective_block_start = __cpu_to_le48(sector);
1127 entry->marked_count = cnt - 1;
1128 entry->disk_ordinal = idx;
1129
1130 sector += cnt;
1131 length -= cnt;
1132
1133 log->entry_count++;
1134 }
1135
1136 return new_bb;
1137 }
1138
1139 /* clear all bad blocks for given disk */
1140 static void clear_disk_badblocks(struct bbm_log *log, const __u8 idx)
1141 {
1142 __u32 i = 0;
1143
1144 while (i < log->entry_count) {
1145 struct bbm_log_entry *entries = log->marked_block_entries;
1146
1147 if (entries[i].disk_ordinal == idx) {
1148 if (i < log->entry_count - 1)
1149 entries[i] = entries[log->entry_count - 1];
1150 log->entry_count--;
1151 } else {
1152 i++;
1153 }
1154 }
1155 }
1156
1157 /* clear given bad block */
1158 static int clear_badblock(struct bbm_log *log, const __u8 idx, const unsigned
1159 long long sector, const int length) {
1160 __u32 i = 0;
1161
1162 while (i < log->entry_count) {
1163 struct bbm_log_entry *entries = log->marked_block_entries;
1164
1165 if ((entries[i].disk_ordinal == idx) &&
1166 (__le48_to_cpu(&entries[i].defective_block_start) ==
1167 sector) && (entries[i].marked_count + 1 == length)) {
1168 if (i < log->entry_count - 1)
1169 entries[i] = entries[log->entry_count - 1];
1170 log->entry_count--;
1171 break;
1172 }
1173 i++;
1174 }
1175
1176 return 1;
1177 }
1178
1179 /* allocate and load BBM log from metadata */
1180 static int load_bbm_log(struct intel_super *super)
1181 {
1182 struct imsm_super *mpb = super->anchor;
1183 __u32 bbm_log_size = __le32_to_cpu(mpb->bbm_log_size);
1184
1185 super->bbm_log = xcalloc(1, sizeof(struct bbm_log));
1186 if (!super->bbm_log)
1187 return 1;
1188
1189 if (bbm_log_size) {
1190 struct bbm_log *log = (void *)mpb +
1191 __le32_to_cpu(mpb->mpb_size) - bbm_log_size;
1192
1193 __u32 entry_count;
1194
1195 if (bbm_log_size < sizeof(log->signature) +
1196 sizeof(log->entry_count))
1197 return 2;
1198
1199 entry_count = __le32_to_cpu(log->entry_count);
1200 if ((__le32_to_cpu(log->signature) != BBM_LOG_SIGNATURE) ||
1201 (entry_count > BBM_LOG_MAX_ENTRIES))
1202 return 3;
1203
1204 if (bbm_log_size !=
1205 sizeof(log->signature) + sizeof(log->entry_count) +
1206 entry_count * sizeof(struct bbm_log_entry))
1207 return 4;
1208
1209 memcpy(super->bbm_log, log, bbm_log_size);
1210 } else {
1211 super->bbm_log->signature = __cpu_to_le32(BBM_LOG_SIGNATURE);
1212 super->bbm_log->entry_count = 0;
1213 }
1214
1215 return 0;
1216 }
1217
1218 /* checks if bad block is within volume boundaries */
1219 static int is_bad_block_in_volume(const struct bbm_log_entry *entry,
1220 const unsigned long long start_sector,
1221 const unsigned long long size)
1222 {
1223 unsigned long long bb_start;
1224 unsigned long long bb_end;
1225
1226 bb_start = __le48_to_cpu(&entry->defective_block_start);
1227 bb_end = bb_start + (entry->marked_count + 1);
1228
1229 if (((bb_start >= start_sector) && (bb_start < start_sector + size)) ||
1230 ((bb_end >= start_sector) && (bb_end <= start_sector + size)))
1231 return 1;
1232
1233 return 0;
1234 }
1235
1236 /* get list of bad blocks on a drive for a volume */
1237 static void get_volume_badblocks(const struct bbm_log *log, const __u8 idx,
1238 const unsigned long long start_sector,
1239 const unsigned long long size,
1240 struct md_bb *bbs)
1241 {
1242 __u32 count = 0;
1243 __u32 i;
1244
1245 for (i = 0; i < log->entry_count; i++) {
1246 const struct bbm_log_entry *ent =
1247 &log->marked_block_entries[i];
1248 struct md_bb_entry *bb;
1249
1250 if ((ent->disk_ordinal == idx) &&
1251 is_bad_block_in_volume(ent, start_sector, size)) {
1252
1253 if (!bbs->entries) {
1254 bbs->entries = xmalloc(BBM_LOG_MAX_ENTRIES *
1255 sizeof(*bb));
1256 if (!bbs->entries)
1257 break;
1258 }
1259
1260 bb = &bbs->entries[count++];
1261 bb->sector = __le48_to_cpu(&ent->defective_block_start);
1262 bb->length = ent->marked_count + 1;
1263 }
1264 }
1265 bbs->count = count;
1266 }
1267
1268 /*
1269 * for second_map:
1270 * == MAP_0 get first map
1271 * == MAP_1 get second map
1272 * == MAP_X than get map according to the current migr_state
1273 */
1274 static __u32 get_imsm_ord_tbl_ent(struct imsm_dev *dev,
1275 int slot,
1276 int second_map)
1277 {
1278 struct imsm_map *map;
1279
1280 map = get_imsm_map(dev, second_map);
1281
1282 /* top byte identifies disk under rebuild */
1283 return __le32_to_cpu(map->disk_ord_tbl[slot]);
1284 }
1285
1286 #define ord_to_idx(ord) (((ord) << 8) >> 8)
1287 static __u32 get_imsm_disk_idx(struct imsm_dev *dev, int slot, int second_map)
1288 {
1289 __u32 ord = get_imsm_ord_tbl_ent(dev, slot, second_map);
1290
1291 return ord_to_idx(ord);
1292 }
1293
1294 static void set_imsm_ord_tbl_ent(struct imsm_map *map, int slot, __u32 ord)
1295 {
1296 map->disk_ord_tbl[slot] = __cpu_to_le32(ord);
1297 }
1298
1299 static int get_imsm_disk_slot(struct imsm_map *map, const unsigned int idx)
1300 {
1301 int slot;
1302 __u32 ord;
1303
1304 for (slot = 0; slot < map->num_members; slot++) {
1305 ord = __le32_to_cpu(map->disk_ord_tbl[slot]);
1306 if (ord_to_idx(ord) == idx)
1307 return slot;
1308 }
1309
1310 return IMSM_STATUS_ERROR;
1311 }
1312 /**
1313 * update_imsm_raid_level() - update raid level appropriately in &imsm_map.
1314 * @map: &imsm_map pointer.
1315 * @new_level: MD style level.
1316 *
1317 * For backward compatibility reasons we need to differentiate RAID10.
1318 * In the past IMSM RAID10 was presented as RAID1.
1319 * Keep compatibility unless it is not explicitly updated by UEFI driver.
1320 *
1321 * Routine needs num_members to be set and (optionally) raid_level.
1322 */
1323 static void update_imsm_raid_level(struct imsm_map *map, int new_level)
1324 {
1325 if (new_level != IMSM_T_RAID10) {
1326 map->raid_level = new_level;
1327 return;
1328 }
1329
1330 /*
1331 * RAID0 to RAID10 migration.
1332 * Due to the compatibility with VROC UEFI must be maintained, this case must be handled
1333 * separately, because the map does not have an updated number of disks.
1334 */
1335 if (map->raid_level == IMSM_T_RAID0) {
1336 if (map->num_members == 2)
1337 map->raid_level = IMSM_T_RAID1;
1338 else
1339 map->raid_level = IMSM_T_RAID10;
1340 return;
1341 }
1342
1343 if (map->num_members == 4) {
1344 if (map->raid_level == IMSM_T_RAID10 || map->raid_level == IMSM_T_RAID1)
1345 return;
1346
1347 map->raid_level = IMSM_T_RAID1;
1348 return;
1349 }
1350
1351 map->raid_level = IMSM_T_RAID10;
1352 }
1353
1354 static int get_imsm_raid_level(struct imsm_map *map)
1355 {
1356 if (map->raid_level == IMSM_T_RAID1) {
1357 if (map->num_members == 2)
1358 return IMSM_T_RAID1;
1359 else
1360 return IMSM_T_RAID10;
1361 }
1362
1363 return map->raid_level;
1364 }
1365
1366 /**
1367 * get_disk_slot_in_dev() - retrieve disk slot from &imsm_dev.
1368 * @super: &intel_super pointer, not NULL.
1369 * @dev_idx: imsm device index.
1370 * @idx: disk index.
1371 *
1372 * Return: Slot on success, IMSM_STATUS_ERROR otherwise.
1373 */
1374 static int get_disk_slot_in_dev(struct intel_super *super, const __u8 dev_idx,
1375 const unsigned int idx)
1376 {
1377 struct imsm_dev *dev = get_imsm_dev(super, dev_idx);
1378 struct imsm_map *map = get_imsm_map(dev, MAP_0);
1379
1380 return get_imsm_disk_slot(map, idx);
1381 }
1382
1383 static int cmp_extent(const void *av, const void *bv)
1384 {
1385 const struct extent *a = av;
1386 const struct extent *b = bv;
1387 if (a->start < b->start)
1388 return -1;
1389 if (a->start > b->start)
1390 return 1;
1391 return 0;
1392 }
1393
1394 static int count_memberships(struct dl *dl, struct intel_super *super)
1395 {
1396 int memberships = 0;
1397 int i;
1398
1399 for (i = 0; i < super->anchor->num_raid_devs; i++)
1400 if (get_disk_slot_in_dev(super, i, dl->index) >= 0)
1401 memberships++;
1402
1403 return memberships;
1404 }
1405
1406 static __u32 imsm_min_reserved_sectors(struct intel_super *super);
1407
1408 static int split_ull(unsigned long long n, void *lo, void *hi)
1409 {
1410 if (lo == 0 || hi == 0)
1411 return 1;
1412 __put_unaligned32(__cpu_to_le32((__u32)n), lo);
1413 __put_unaligned32(__cpu_to_le32((n >> 32)), hi);
1414 return 0;
1415 }
1416
1417 static unsigned long long join_u32(__u32 lo, __u32 hi)
1418 {
1419 return (unsigned long long)__le32_to_cpu(lo) |
1420 (((unsigned long long)__le32_to_cpu(hi)) << 32);
1421 }
1422
1423 static unsigned long long total_blocks(struct imsm_disk *disk)
1424 {
1425 if (disk == NULL)
1426 return 0;
1427 return join_u32(disk->total_blocks_lo, disk->total_blocks_hi);
1428 }
1429
1430 /**
1431 * imsm_num_data_members() - get data drives count for an array.
1432 * @map: Map to analyze.
1433 *
1434 * num_data_members value represents minimal count of drives for level.
1435 * The name of the property could be misleading for RAID5 with asymmetric layout
1436 * because some data required to be calculated from parity.
1437 * The property is extracted from level and num_members value.
1438 *
1439 * Return: num_data_members value on success, zero otherwise.
1440 */
1441 static __u8 imsm_num_data_members(struct imsm_map *map)
1442 {
1443 switch (get_imsm_raid_level(map)) {
1444 case 0:
1445 return map->num_members;
1446 case 1:
1447 case 10:
1448 return map->num_members / 2;
1449 case 5:
1450 return map->num_members - 1;
1451 default:
1452 dprintf("unsupported raid level\n");
1453 return 0;
1454 }
1455 }
1456
1457 static unsigned long long pba_of_lba0(struct imsm_map *map)
1458 {
1459 if (map == NULL)
1460 return 0;
1461 return join_u32(map->pba_of_lba0_lo, map->pba_of_lba0_hi);
1462 }
1463
1464 static unsigned long long blocks_per_member(struct imsm_map *map)
1465 {
1466 if (map == NULL)
1467 return 0;
1468 return join_u32(map->blocks_per_member_lo, map->blocks_per_member_hi);
1469 }
1470
1471 static unsigned long long num_data_stripes(struct imsm_map *map)
1472 {
1473 if (map == NULL)
1474 return 0;
1475 return join_u32(map->num_data_stripes_lo, map->num_data_stripes_hi);
1476 }
1477
1478 static unsigned long long vol_curr_migr_unit(struct imsm_dev *dev)
1479 {
1480 if (dev == NULL)
1481 return 0;
1482
1483 return join_u32(dev->vol.curr_migr_unit_lo, dev->vol.curr_migr_unit_hi);
1484 }
1485
1486 static unsigned long long imsm_dev_size(struct imsm_dev *dev)
1487 {
1488 if (dev == NULL)
1489 return 0;
1490 return join_u32(dev->size_low, dev->size_high);
1491 }
1492
1493 static unsigned long long migr_chkp_area_pba(struct migr_record *migr_rec)
1494 {
1495 if (migr_rec == NULL)
1496 return 0;
1497 return join_u32(migr_rec->ckpt_area_pba_lo,
1498 migr_rec->ckpt_area_pba_hi);
1499 }
1500
1501 static unsigned long long current_migr_unit(struct migr_record *migr_rec)
1502 {
1503 if (migr_rec == NULL)
1504 return 0;
1505 return join_u32(migr_rec->curr_migr_unit_lo,
1506 migr_rec->curr_migr_unit_hi);
1507 }
1508
1509 static unsigned long long migr_dest_1st_member_lba(struct migr_record *migr_rec)
1510 {
1511 if (migr_rec == NULL)
1512 return 0;
1513 return join_u32(migr_rec->dest_1st_member_lba_lo,
1514 migr_rec->dest_1st_member_lba_hi);
1515 }
1516
1517 static unsigned long long get_num_migr_units(struct migr_record *migr_rec)
1518 {
1519 if (migr_rec == NULL)
1520 return 0;
1521 return join_u32(migr_rec->num_migr_units_lo,
1522 migr_rec->num_migr_units_hi);
1523 }
1524
1525 static void set_total_blocks(struct imsm_disk *disk, unsigned long long n)
1526 {
1527 split_ull(n, &disk->total_blocks_lo, &disk->total_blocks_hi);
1528 }
1529
1530 /**
1531 * set_num_domains() - Set number of domains for an array.
1532 * @map: Map to be updated.
1533 *
1534 * num_domains property represents copies count of each data drive, thus make
1535 * it meaningful only for RAID1 and RAID10. IMSM supports two domains for
1536 * raid1 and raid10.
1537 */
1538 static void set_num_domains(struct imsm_map *map)
1539 {
1540 int level = get_imsm_raid_level(map);
1541
1542 if (level == 1 || level == 10)
1543 map->num_domains = 2;
1544 else
1545 map->num_domains = 1;
1546 }
1547
1548 static void set_pba_of_lba0(struct imsm_map *map, unsigned long long n)
1549 {
1550 split_ull(n, &map->pba_of_lba0_lo, &map->pba_of_lba0_hi);
1551 }
1552
1553 static void set_blocks_per_member(struct imsm_map *map, unsigned long long n)
1554 {
1555 split_ull(n, &map->blocks_per_member_lo, &map->blocks_per_member_hi);
1556 }
1557
1558 static void set_num_data_stripes(struct imsm_map *map, unsigned long long n)
1559 {
1560 split_ull(n, &map->num_data_stripes_lo, &map->num_data_stripes_hi);
1561 }
1562
1563 /**
1564 * update_num_data_stripes() - Calculate and update num_data_stripes value.
1565 * @map: map to be updated.
1566 * @dev_size: size of volume.
1567 *
1568 * num_data_stripes value is addictionally divided by num_domains, therefore for
1569 * levels where num_domains is not 1, nds is a part of real value.
1570 */
1571 static void update_num_data_stripes(struct imsm_map *map,
1572 unsigned long long dev_size)
1573 {
1574 unsigned long long nds = dev_size / imsm_num_data_members(map);
1575
1576 nds /= map->num_domains;
1577 nds /= map->blocks_per_strip;
1578 set_num_data_stripes(map, nds);
1579 }
1580
1581 static void set_vol_curr_migr_unit(struct imsm_dev *dev, unsigned long long n)
1582 {
1583 if (dev == NULL)
1584 return;
1585
1586 split_ull(n, &dev->vol.curr_migr_unit_lo, &dev->vol.curr_migr_unit_hi);
1587 }
1588
1589 static void set_imsm_dev_size(struct imsm_dev *dev, unsigned long long n)
1590 {
1591 split_ull(n, &dev->size_low, &dev->size_high);
1592 }
1593
1594 static void set_migr_chkp_area_pba(struct migr_record *migr_rec,
1595 unsigned long long n)
1596 {
1597 split_ull(n, &migr_rec->ckpt_area_pba_lo, &migr_rec->ckpt_area_pba_hi);
1598 }
1599
1600 static void set_current_migr_unit(struct migr_record *migr_rec,
1601 unsigned long long n)
1602 {
1603 split_ull(n, &migr_rec->curr_migr_unit_lo,
1604 &migr_rec->curr_migr_unit_hi);
1605 }
1606
1607 static void set_migr_dest_1st_member_lba(struct migr_record *migr_rec,
1608 unsigned long long n)
1609 {
1610 split_ull(n, &migr_rec->dest_1st_member_lba_lo,
1611 &migr_rec->dest_1st_member_lba_hi);
1612 }
1613
1614 static void set_num_migr_units(struct migr_record *migr_rec,
1615 unsigned long long n)
1616 {
1617 split_ull(n, &migr_rec->num_migr_units_lo,
1618 &migr_rec->num_migr_units_hi);
1619 }
1620
1621 static unsigned long long per_dev_array_size(struct imsm_map *map)
1622 {
1623 unsigned long long array_size = 0;
1624
1625 if (map == NULL)
1626 return array_size;
1627
1628 array_size = num_data_stripes(map) * map->blocks_per_strip;
1629 if (get_imsm_raid_level(map) == 1 || get_imsm_raid_level(map) == 10)
1630 array_size *= 2;
1631
1632 return array_size;
1633 }
1634
1635 static struct extent *get_extents(struct intel_super *super, struct dl *dl,
1636 int get_minimal_reservation)
1637 {
1638 /* find a list of used extents on the given physical device */
1639 int memberships = count_memberships(dl, super);
1640 struct extent *rv = xcalloc(memberships + 1, sizeof(struct extent));
1641 struct extent *e = rv;
1642 int i;
1643 __u32 reservation;
1644
1645 /* trim the reserved area for spares, so they can join any array
1646 * regardless of whether the OROM has assigned sectors from the
1647 * IMSM_RESERVED_SECTORS region
1648 */
1649 if (dl->index == -1 || get_minimal_reservation)
1650 reservation = imsm_min_reserved_sectors(super);
1651 else
1652 reservation = MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS;
1653
1654 for (i = 0; i < super->anchor->num_raid_devs; i++) {
1655 struct imsm_dev *dev = get_imsm_dev(super, i);
1656 struct imsm_map *map = get_imsm_map(dev, MAP_0);
1657
1658 if (get_imsm_disk_slot(map, dl->index) >= 0) {
1659 e->start = pba_of_lba0(map);
1660 e->size = per_dev_array_size(map);
1661 e->vol = i;
1662 e++;
1663 }
1664 }
1665 qsort(rv, memberships, sizeof(*rv), cmp_extent);
1666
1667 /* determine the start of the metadata
1668 * when no raid devices are defined use the default
1669 * ...otherwise allow the metadata to truncate the value
1670 * as is the case with older versions of imsm
1671 */
1672 if (memberships) {
1673 struct extent *last = &rv[memberships - 1];
1674 unsigned long long remainder;
1675
1676 remainder = total_blocks(&dl->disk) - (last->start + last->size);
1677 /* round down to 1k block to satisfy precision of the kernel
1678 * 'size' interface
1679 */
1680 remainder &= ~1UL;
1681 /* make sure remainder is still sane */
1682 if (remainder < (unsigned)ROUND_UP(super->len, 512) >> 9)
1683 remainder = ROUND_UP(super->len, 512) >> 9;
1684 if (reservation > remainder)
1685 reservation = remainder;
1686 }
1687 e->start = total_blocks(&dl->disk) - reservation;
1688 e->size = 0;
1689 return rv;
1690 }
1691
1692 /* try to determine how much space is reserved for metadata from
1693 * the last get_extents() entry, otherwise fallback to the
1694 * default
1695 */
1696 static __u32 imsm_reserved_sectors(struct intel_super *super, struct dl *dl)
1697 {
1698 struct extent *e;
1699 int i;
1700 __u32 rv;
1701
1702 /* for spares just return a minimal reservation which will grow
1703 * once the spare is picked up by an array
1704 */
1705 if (dl->index == -1)
1706 return MPB_SECTOR_CNT;
1707
1708 e = get_extents(super, dl, 0);
1709 if (!e)
1710 return MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS;
1711
1712 /* scroll to last entry */
1713 for (i = 0; e[i].size; i++)
1714 continue;
1715
1716 rv = total_blocks(&dl->disk) - e[i].start;
1717
1718 free(e);
1719
1720 return rv;
1721 }
1722
1723 static int is_spare(struct imsm_disk *disk)
1724 {
1725 return (disk->status & SPARE_DISK) == SPARE_DISK;
1726 }
1727
1728 static int is_configured(struct imsm_disk *disk)
1729 {
1730 return (disk->status & CONFIGURED_DISK) == CONFIGURED_DISK;
1731 }
1732
1733 static int is_failed(struct imsm_disk *disk)
1734 {
1735 return (disk->status & FAILED_DISK) == FAILED_DISK;
1736 }
1737
1738 static int is_journal(struct imsm_disk *disk)
1739 {
1740 return (disk->status & JOURNAL_DISK) == JOURNAL_DISK;
1741 }
1742
1743 /**
1744 * round_member_size_to_mb()- Round given size to closest MiB.
1745 * @size: size to round in sectors.
1746 */
1747 static inline unsigned long long round_member_size_to_mb(unsigned long long size)
1748 {
1749 return (size >> SECT_PER_MB_SHIFT) << SECT_PER_MB_SHIFT;
1750 }
1751
1752 /**
1753 * round_size_to_mb()- Round given size.
1754 * @array_size: size to round in sectors.
1755 * @disk_count: count of data members.
1756 *
1757 * Get size per each data member and round it to closest MiB to ensure that data
1758 * splits evenly between members.
1759 *
1760 * Return: Array size, rounded down.
1761 */
1762 static inline unsigned long long round_size_to_mb(unsigned long long array_size,
1763 unsigned int disk_count)
1764 {
1765 return round_member_size_to_mb(array_size / disk_count) * disk_count;
1766 }
1767
1768 static int able_to_resync(int raid_level, int missing_disks)
1769 {
1770 int max_missing_disks = 0;
1771
1772 switch (raid_level) {
1773 case 10:
1774 max_missing_disks = 1;
1775 break;
1776 default:
1777 max_missing_disks = 0;
1778 }
1779 return missing_disks <= max_missing_disks;
1780 }
1781
1782 /* try to determine how much space is reserved for metadata from
1783 * the last get_extents() entry on the smallest active disk,
1784 * otherwise fallback to the default
1785 */
1786 static __u32 imsm_min_reserved_sectors(struct intel_super *super)
1787 {
1788 struct extent *e;
1789 int i;
1790 unsigned long long min_active;
1791 __u32 remainder;
1792 __u32 rv = MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS;
1793 struct dl *dl, *dl_min = NULL;
1794
1795 if (!super)
1796 return rv;
1797
1798 min_active = 0;
1799 for (dl = super->disks; dl; dl = dl->next) {
1800 if (dl->index < 0)
1801 continue;
1802 unsigned long long blocks = total_blocks(&dl->disk);
1803 if (blocks < min_active || min_active == 0) {
1804 dl_min = dl;
1805 min_active = blocks;
1806 }
1807 }
1808 if (!dl_min)
1809 return rv;
1810
1811 /* find last lba used by subarrays on the smallest active disk */
1812 e = get_extents(super, dl_min, 0);
1813 if (!e)
1814 return rv;
1815 for (i = 0; e[i].size; i++)
1816 continue;
1817
1818 remainder = min_active - e[i].start;
1819 free(e);
1820
1821 /* to give priority to recovery we should not require full
1822 IMSM_RESERVED_SECTORS from the spare */
1823 rv = MPB_SECTOR_CNT + NUM_BLOCKS_DIRTY_STRIPE_REGION;
1824
1825 /* if real reservation is smaller use that value */
1826 return (remainder < rv) ? remainder : rv;
1827 }
1828
1829 static bool is_gen_migration(struct imsm_dev *dev);
1830
1831 #define IMSM_4K_DIV 8
1832
1833 static __u64 blocks_per_migr_unit(struct intel_super *super,
1834 struct imsm_dev *dev);
1835
1836 static void print_imsm_dev(struct intel_super *super,
1837 struct imsm_dev *dev,
1838 char *uuid,
1839 int disk_idx)
1840 {
1841 __u64 sz;
1842 int slot, i;
1843 struct imsm_map *map = get_imsm_map(dev, MAP_0);
1844 struct imsm_map *map2 = get_imsm_map(dev, MAP_1);
1845 __u32 ord;
1846
1847 printf("\n");
1848 printf("[%.16s]:\n", dev->volume);
1849 printf(" Subarray : %d\n", super->current_vol);
1850 printf(" UUID : %s\n", uuid);
1851 printf(" RAID Level : %d", get_imsm_raid_level(map));
1852 if (map2)
1853 printf(" <-- %d", get_imsm_raid_level(map2));
1854 printf("\n");
1855 printf(" Members : %d", map->num_members);
1856 if (map2)
1857 printf(" <-- %d", map2->num_members);
1858 printf("\n");
1859 printf(" Slots : [");
1860 for (i = 0; i < map->num_members; i++) {
1861 ord = get_imsm_ord_tbl_ent(dev, i, MAP_0);
1862 printf("%s", ord & IMSM_ORD_REBUILD ? "_" : "U");
1863 }
1864 printf("]");
1865 if (map2) {
1866 printf(" <-- [");
1867 for (i = 0; i < map2->num_members; i++) {
1868 ord = get_imsm_ord_tbl_ent(dev, i, MAP_1);
1869 printf("%s", ord & IMSM_ORD_REBUILD ? "_" : "U");
1870 }
1871 printf("]");
1872 }
1873 printf("\n");
1874 printf(" Failed disk : ");
1875 if (map->failed_disk_num == 0xff)
1876 printf(STR_COMMON_NONE);
1877 else
1878 printf("%i", map->failed_disk_num);
1879 printf("\n");
1880 slot = get_imsm_disk_slot(map, disk_idx);
1881 if (slot >= 0) {
1882 ord = get_imsm_ord_tbl_ent(dev, slot, MAP_X);
1883 printf(" This Slot : %d%s\n", slot,
1884 ord & IMSM_ORD_REBUILD ? " (out-of-sync)" : "");
1885 } else
1886 printf(" This Slot : ?\n");
1887 printf(" Sector Size : %u\n", super->sector_size);
1888 sz = imsm_dev_size(dev);
1889 printf(" Array Size : %llu%s\n",
1890 (unsigned long long)sz * 512 / super->sector_size,
1891 human_size(sz * 512));
1892 sz = blocks_per_member(map);
1893 printf(" Per Dev Size : %llu%s\n",
1894 (unsigned long long)sz * 512 / super->sector_size,
1895 human_size(sz * 512));
1896 printf(" Sector Offset : %llu\n",
1897 pba_of_lba0(map) * 512 / super->sector_size);
1898 printf(" Num Stripes : %llu\n",
1899 num_data_stripes(map));
1900 printf(" Chunk Size : %u KiB",
1901 __le16_to_cpu(map->blocks_per_strip) / 2);
1902 if (map2)
1903 printf(" <-- %u KiB",
1904 __le16_to_cpu(map2->blocks_per_strip) / 2);
1905 printf("\n");
1906 printf(" Reserved : %d\n", __le32_to_cpu(dev->reserved_blocks));
1907 printf(" Migrate State : ");
1908 if (dev->vol.migr_state) {
1909 if (migr_type(dev) == MIGR_INIT)
1910 printf("initialize\n");
1911 else if (migr_type(dev) == MIGR_REBUILD)
1912 printf("rebuild\n");
1913 else if (migr_type(dev) == MIGR_VERIFY)
1914 printf("check\n");
1915 else if (migr_type(dev) == MIGR_GEN_MIGR)
1916 printf("general migration\n");
1917 else if (migr_type(dev) == MIGR_STATE_CHANGE)
1918 printf("state change\n");
1919 else if (migr_type(dev) == MIGR_REPAIR)
1920 printf("repair\n");
1921 else
1922 printf("<unknown:%d>\n", migr_type(dev));
1923 } else
1924 printf("idle\n");
1925 printf(" Map State : %s", map_state_str[map->map_state]);
1926 if (dev->vol.migr_state) {
1927 struct imsm_map *map = get_imsm_map(dev, MAP_1);
1928
1929 printf(" <-- %s", map_state_str[map->map_state]);
1930 printf("\n Checkpoint : %llu ", vol_curr_migr_unit(dev));
1931 if (is_gen_migration(dev) && (slot > 1 || slot < 0))
1932 printf("(N/A)");
1933 else
1934 printf("(%llu)", (unsigned long long)
1935 blocks_per_migr_unit(super, dev));
1936 }
1937 printf("\n");
1938 printf(" Dirty State : %s\n", (dev->vol.dirty & RAIDVOL_DIRTY) ?
1939 "dirty" : "clean");
1940 printf(" RWH Policy : ");
1941 if (dev->rwh_policy == RWH_OFF || dev->rwh_policy == RWH_MULTIPLE_OFF)
1942 printf("off\n");
1943 else if (dev->rwh_policy == RWH_DISTRIBUTED)
1944 printf("PPL distributed\n");
1945 else if (dev->rwh_policy == RWH_JOURNALING_DRIVE)
1946 printf("PPL journaling drive\n");
1947 else if (dev->rwh_policy == RWH_MULTIPLE_DISTRIBUTED)
1948 printf("Multiple distributed PPLs\n");
1949 else if (dev->rwh_policy == RWH_MULTIPLE_PPLS_JOURNALING_DRIVE)
1950 printf("Multiple PPLs on journaling drive\n");
1951 else if (dev->rwh_policy == RWH_BITMAP)
1952 printf("Write-intent bitmap\n");
1953 else
1954 printf("<unknown:%d>\n", dev->rwh_policy);
1955
1956 printf(" Volume ID : %u\n", dev->my_vol_raid_dev_num);
1957 }
1958
1959 static void print_imsm_disk(struct imsm_disk *disk,
1960 int index,
1961 __u32 reserved,
1962 unsigned int sector_size) {
1963 char str[MAX_RAID_SERIAL_LEN + 1];
1964 __u64 sz;
1965
1966 if (index < -1 || !disk)
1967 return;
1968
1969 printf("\n");
1970 snprintf(str, MAX_RAID_SERIAL_LEN + 1, "%s", disk->serial);
1971 if (index >= 0)
1972 printf(" Disk%02d Serial : %s\n", index, str);
1973 else
1974 printf(" Disk Serial : %s\n", str);
1975 printf(" State :%s%s%s%s\n", is_spare(disk) ? " spare" : "",
1976 is_configured(disk) ? " active" : "",
1977 is_failed(disk) ? " failed" : "",
1978 is_journal(disk) ? " journal" : "");
1979 printf(" Id : %08x\n", __le32_to_cpu(disk->scsi_id));
1980 sz = total_blocks(disk) - reserved;
1981 printf(" Usable Size : %llu%s\n",
1982 (unsigned long long)sz * 512 / sector_size,
1983 human_size(sz * 512));
1984 }
1985
1986 void convert_to_4k_imsm_migr_rec(struct intel_super *super)
1987 {
1988 struct migr_record *migr_rec = super->migr_rec;
1989
1990 migr_rec->blocks_per_unit /= IMSM_4K_DIV;
1991 migr_rec->dest_depth_per_unit /= IMSM_4K_DIV;
1992 split_ull((join_u32(migr_rec->post_migr_vol_cap,
1993 migr_rec->post_migr_vol_cap_hi) / IMSM_4K_DIV),
1994 &migr_rec->post_migr_vol_cap, &migr_rec->post_migr_vol_cap_hi);
1995 set_migr_chkp_area_pba(migr_rec,
1996 migr_chkp_area_pba(migr_rec) / IMSM_4K_DIV);
1997 set_migr_dest_1st_member_lba(migr_rec,
1998 migr_dest_1st_member_lba(migr_rec) / IMSM_4K_DIV);
1999 }
2000
2001 void convert_to_4k_imsm_disk(struct imsm_disk *disk)
2002 {
2003 set_total_blocks(disk, (total_blocks(disk)/IMSM_4K_DIV));
2004 }
2005
2006 void convert_to_4k(struct intel_super *super)
2007 {
2008 struct imsm_super *mpb = super->anchor;
2009 struct imsm_disk *disk;
2010 int i;
2011 __u32 bbm_log_size = __le32_to_cpu(mpb->bbm_log_size);
2012
2013 for (i = 0; i < mpb->num_disks ; i++) {
2014 disk = __get_imsm_disk(mpb, i);
2015 /* disk */
2016 convert_to_4k_imsm_disk(disk);
2017 }
2018 for (i = 0; i < mpb->num_raid_devs; i++) {
2019 struct imsm_dev *dev = __get_imsm_dev(mpb, i);
2020 struct imsm_map *map = get_imsm_map(dev, MAP_0);
2021 /* dev */
2022 set_imsm_dev_size(dev, imsm_dev_size(dev)/IMSM_4K_DIV);
2023 set_vol_curr_migr_unit(dev,
2024 vol_curr_migr_unit(dev) / IMSM_4K_DIV);
2025
2026 /* map0 */
2027 set_blocks_per_member(map, blocks_per_member(map)/IMSM_4K_DIV);
2028 map->blocks_per_strip /= IMSM_4K_DIV;
2029 set_pba_of_lba0(map, pba_of_lba0(map)/IMSM_4K_DIV);
2030
2031 if (dev->vol.migr_state) {
2032 /* map1 */
2033 map = get_imsm_map(dev, MAP_1);
2034 set_blocks_per_member(map,
2035 blocks_per_member(map)/IMSM_4K_DIV);
2036 map->blocks_per_strip /= IMSM_4K_DIV;
2037 set_pba_of_lba0(map, pba_of_lba0(map)/IMSM_4K_DIV);
2038 }
2039 }
2040 if (bbm_log_size) {
2041 struct bbm_log *log = (void *)mpb +
2042 __le32_to_cpu(mpb->mpb_size) - bbm_log_size;
2043 __u32 i;
2044
2045 for (i = 0; i < log->entry_count; i++) {
2046 struct bbm_log_entry *entry =
2047 &log->marked_block_entries[i];
2048
2049 __u8 count = entry->marked_count + 1;
2050 unsigned long long sector =
2051 __le48_to_cpu(&entry->defective_block_start);
2052
2053 entry->defective_block_start =
2054 __cpu_to_le48(sector/IMSM_4K_DIV);
2055 entry->marked_count = max(count/IMSM_4K_DIV, 1) - 1;
2056 }
2057 }
2058
2059 mpb->check_sum = __gen_imsm_checksum(mpb);
2060 }
2061
2062 void examine_migr_rec_imsm(struct intel_super *super)
2063 {
2064 struct migr_record *migr_rec = super->migr_rec;
2065 struct imsm_super *mpb = super->anchor;
2066 int i;
2067
2068 for (i = 0; i < mpb->num_raid_devs; i++) {
2069 struct imsm_dev *dev = __get_imsm_dev(mpb, i);
2070 struct imsm_map *map;
2071 int slot = -1;
2072
2073 if (is_gen_migration(dev) == false)
2074 continue;
2075
2076 printf("\nMigration Record Information:");
2077
2078 /* first map under migration */
2079 map = get_imsm_map(dev, MAP_0);
2080
2081 if (map)
2082 slot = get_imsm_disk_slot(map, super->disks->index);
2083 if (map == NULL || slot > 1 || slot < 0) {
2084 printf(" Empty\n ");
2085 printf("Examine one of first two disks in array\n");
2086 break;
2087 }
2088 printf("\n Status : ");
2089 if (__le32_to_cpu(migr_rec->rec_status) == UNIT_SRC_NORMAL)
2090 printf("Normal\n");
2091 else
2092 printf("Contains Data\n");
2093 printf(" Current Unit : %llu\n",
2094 current_migr_unit(migr_rec));
2095 printf(" Family : %u\n",
2096 __le32_to_cpu(migr_rec->family_num));
2097 printf(" Ascending : %u\n",
2098 __le32_to_cpu(migr_rec->ascending_migr));
2099 printf(" Blocks Per Unit : %u\n",
2100 __le32_to_cpu(migr_rec->blocks_per_unit));
2101 printf(" Dest. Depth Per Unit : %u\n",
2102 __le32_to_cpu(migr_rec->dest_depth_per_unit));
2103 printf(" Checkpoint Area pba : %llu\n",
2104 migr_chkp_area_pba(migr_rec));
2105 printf(" First member lba : %llu\n",
2106 migr_dest_1st_member_lba(migr_rec));
2107 printf(" Total Number of Units : %llu\n",
2108 get_num_migr_units(migr_rec));
2109 printf(" Size of volume : %llu\n",
2110 join_u32(migr_rec->post_migr_vol_cap,
2111 migr_rec->post_migr_vol_cap_hi));
2112 printf(" Record was read from : %u\n",
2113 __le32_to_cpu(migr_rec->ckpt_read_disk_num));
2114
2115 break;
2116 }
2117 }
2118
2119 void convert_from_4k_imsm_migr_rec(struct intel_super *super)
2120 {
2121 struct migr_record *migr_rec = super->migr_rec;
2122
2123 migr_rec->blocks_per_unit *= IMSM_4K_DIV;
2124 migr_rec->dest_depth_per_unit *= IMSM_4K_DIV;
2125 split_ull((join_u32(migr_rec->post_migr_vol_cap,
2126 migr_rec->post_migr_vol_cap_hi) * IMSM_4K_DIV),
2127 &migr_rec->post_migr_vol_cap,
2128 &migr_rec->post_migr_vol_cap_hi);
2129 set_migr_chkp_area_pba(migr_rec,
2130 migr_chkp_area_pba(migr_rec) * IMSM_4K_DIV);
2131 set_migr_dest_1st_member_lba(migr_rec,
2132 migr_dest_1st_member_lba(migr_rec) * IMSM_4K_DIV);
2133 }
2134
2135 void convert_from_4k(struct intel_super *super)
2136 {
2137 struct imsm_super *mpb = super->anchor;
2138 struct imsm_disk *disk;
2139 int i;
2140 __u32 bbm_log_size = __le32_to_cpu(mpb->bbm_log_size);
2141
2142 for (i = 0; i < mpb->num_disks ; i++) {
2143 disk = __get_imsm_disk(mpb, i);
2144 /* disk */
2145 set_total_blocks(disk, (total_blocks(disk)*IMSM_4K_DIV));
2146 }
2147
2148 for (i = 0; i < mpb->num_raid_devs; i++) {
2149 struct imsm_dev *dev = __get_imsm_dev(mpb, i);
2150 struct imsm_map *map = get_imsm_map(dev, MAP_0);
2151 /* dev */
2152 set_imsm_dev_size(dev, imsm_dev_size(dev)*IMSM_4K_DIV);
2153 set_vol_curr_migr_unit(dev,
2154 vol_curr_migr_unit(dev) * IMSM_4K_DIV);
2155
2156 /* map0 */
2157 set_blocks_per_member(map, blocks_per_member(map)*IMSM_4K_DIV);
2158 map->blocks_per_strip *= IMSM_4K_DIV;
2159 set_pba_of_lba0(map, pba_of_lba0(map)*IMSM_4K_DIV);
2160
2161 if (dev->vol.migr_state) {
2162 /* map1 */
2163 map = get_imsm_map(dev, MAP_1);
2164 set_blocks_per_member(map,
2165 blocks_per_member(map)*IMSM_4K_DIV);
2166 map->blocks_per_strip *= IMSM_4K_DIV;
2167 set_pba_of_lba0(map, pba_of_lba0(map)*IMSM_4K_DIV);
2168 }
2169 }
2170 if (bbm_log_size) {
2171 struct bbm_log *log = (void *)mpb +
2172 __le32_to_cpu(mpb->mpb_size) - bbm_log_size;
2173 __u32 i;
2174
2175 for (i = 0; i < log->entry_count; i++) {
2176 struct bbm_log_entry *entry =
2177 &log->marked_block_entries[i];
2178
2179 __u8 count = entry->marked_count + 1;
2180 unsigned long long sector =
2181 __le48_to_cpu(&entry->defective_block_start);
2182
2183 entry->defective_block_start =
2184 __cpu_to_le48(sector*IMSM_4K_DIV);
2185 entry->marked_count = count*IMSM_4K_DIV - 1;
2186 }
2187 }
2188
2189 mpb->check_sum = __gen_imsm_checksum(mpb);
2190 }
2191
2192 /**
2193 * imsm_check_attributes() - Check if features represented by attributes flags are supported.
2194 *
2195 * @attributes: attributes read from metadata.
2196 * Returns: true if all features are supported, false otherwise.
2197 */
2198 static bool imsm_check_attributes(__u32 attributes)
2199 {
2200 if ((attributes & (MPB_ATTRIB_SUPPORTED | MPB_ATTRIB_IGNORED)) == attributes)
2201 return true;
2202
2203 return false;
2204 }
2205
2206 static void getinfo_super_imsm(struct supertype *st, struct mdinfo *info, char *map);
2207
2208 static void examine_super_imsm(struct supertype *st, char *homehost)
2209 {
2210 struct intel_super *super = st->sb;
2211 struct imsm_super *mpb = super->anchor;
2212 char str[MAX_SIGNATURE_LENGTH];
2213 int i;
2214 struct mdinfo info;
2215 char nbuf[64];
2216 __u32 sum;
2217 __u32 reserved = imsm_reserved_sectors(super, super->disks);
2218 struct dl *dl;
2219 time_t creation_time;
2220
2221 strncpy(str, (char *)mpb->sig, MPB_SIG_LEN);
2222 str[MPB_SIG_LEN-1] = '\0';
2223 printf(" Magic : %s\n", str);
2224 printf(" Version : %s\n", get_imsm_version(mpb));
2225 printf(" Orig Family : %08x\n", __le32_to_cpu(mpb->orig_family_num));
2226 printf(" Family : %08x\n", __le32_to_cpu(mpb->family_num));
2227 printf(" Generation : %08x\n", __le32_to_cpu(mpb->generation_num));
2228 creation_time = __le64_to_cpu(mpb->creation_time);
2229 printf(" Creation Time : %.24s\n",
2230 creation_time ? ctime(&creation_time) : "Unknown");
2231
2232 printf(" Attributes : %08x (%s)\n", mpb->attributes,
2233 imsm_check_attributes(mpb->attributes) ? "supported" : "not supported");
2234
2235 getinfo_super_imsm(st, &info, NULL);
2236 fname_from_uuid(&info, nbuf);
2237 printf(" UUID : %s\n", nbuf + 5);
2238 sum = __le32_to_cpu(mpb->check_sum);
2239 printf(" Checksum : %08x %s\n", sum,
2240 __gen_imsm_checksum(mpb) == sum ? "correct" : "incorrect");
2241 printf(" MPB Sectors : %d\n", mpb_sectors(mpb, super->sector_size));
2242 printf(" Disks : %d\n", mpb->num_disks);
2243 printf(" RAID Devices : %d\n", mpb->num_raid_devs);
2244 print_imsm_disk(__get_imsm_disk(mpb, super->disks->index),
2245 super->disks->index, reserved, super->sector_size);
2246 if (get_imsm_bbm_log_size(super->bbm_log)) {
2247 struct bbm_log *log = super->bbm_log;
2248
2249 printf("\n");
2250 printf("Bad Block Management Log:\n");
2251 printf(" Log Size : %d\n", __le32_to_cpu(mpb->bbm_log_size));
2252 printf(" Signature : %x\n", __le32_to_cpu(log->signature));
2253 printf(" Entry Count : %d\n", __le32_to_cpu(log->entry_count));
2254 }
2255 for (i = 0; i < mpb->num_raid_devs; i++) {
2256 struct mdinfo info;
2257 struct imsm_dev *dev = __get_imsm_dev(mpb, i);
2258
2259 super->current_vol = i;
2260 getinfo_super_imsm(st, &info, NULL);
2261 fname_from_uuid(&info, nbuf);
2262 print_imsm_dev(super, dev, nbuf + 5, super->disks->index);
2263 }
2264 for (i = 0; i < mpb->num_disks; i++) {
2265 if (i == super->disks->index)
2266 continue;
2267 print_imsm_disk(__get_imsm_disk(mpb, i), i, reserved,
2268 super->sector_size);
2269 }
2270
2271 for (dl = super->disks; dl; dl = dl->next)
2272 if (dl->index == -1)
2273 print_imsm_disk(&dl->disk, -1, reserved,
2274 super->sector_size);
2275
2276 examine_migr_rec_imsm(super);
2277 }
2278
2279 static void brief_examine_super_imsm(struct supertype *st, int verbose)
2280 {
2281 /* We just write a generic IMSM ARRAY entry */
2282 struct mdinfo info;
2283 char nbuf[64];
2284
2285 getinfo_super_imsm(st, &info, NULL);
2286 fname_from_uuid(&info, nbuf);
2287 printf("ARRAY metadata=imsm UUID=%s\n", nbuf + 5);
2288 }
2289
2290 static void brief_examine_subarrays_imsm(struct supertype *st, int verbose)
2291 {
2292 /* We just write a generic IMSM ARRAY entry */
2293 struct mdinfo info;
2294 char nbuf[64];
2295 char nbuf1[64];
2296 struct intel_super *super = st->sb;
2297 int i;
2298
2299 if (!super->anchor->num_raid_devs)
2300 return;
2301
2302 getinfo_super_imsm(st, &info, NULL);
2303 fname_from_uuid(&info, nbuf);
2304 for (i = 0; i < super->anchor->num_raid_devs; i++) {
2305 struct imsm_dev *dev = get_imsm_dev(super, i);
2306
2307 super->current_vol = i;
2308 getinfo_super_imsm(st, &info, NULL);
2309 fname_from_uuid(&info, nbuf1);
2310 printf("ARRAY " DEV_MD_DIR "%.16s container=%s member=%d UUID=%s\n",
2311 dev->volume, nbuf + 5, i, nbuf1 + 5);
2312 }
2313 }
2314
2315 static void export_examine_super_imsm(struct supertype *st)
2316 {
2317 struct intel_super *super = st->sb;
2318 struct imsm_super *mpb = super->anchor;
2319 struct mdinfo info;
2320 char nbuf[64];
2321
2322 getinfo_super_imsm(st, &info, NULL);
2323 fname_from_uuid(&info, nbuf);
2324 printf("MD_METADATA=imsm\n");
2325 printf("MD_LEVEL=container\n");
2326 printf("MD_UUID=%s\n", nbuf+5);
2327 printf("MD_DEVICES=%u\n", mpb->num_disks);
2328 printf("MD_CREATION_TIME=%llu\n",
2329 (unsigned long long)__le64_to_cpu(mpb->creation_time));
2330 }
2331
2332 static void detail_super_imsm(struct supertype *st, char *homehost,
2333 char *subarray)
2334 {
2335 struct mdinfo info;
2336 char nbuf[64];
2337 struct intel_super *super = st->sb;
2338 int temp_vol = super->current_vol;
2339
2340 if (subarray)
2341 super->current_vol = strtoul(subarray, NULL, 10);
2342
2343 getinfo_super_imsm(st, &info, NULL);
2344 fname_from_uuid(&info, nbuf);
2345 printf("\n UUID : %s\n", nbuf + 5);
2346
2347 super->current_vol = temp_vol;
2348 }
2349
2350 static void brief_detail_super_imsm(struct supertype *st, char *subarray)
2351 {
2352 struct mdinfo info;
2353 char nbuf[64];
2354 struct intel_super *super = st->sb;
2355 int temp_vol = super->current_vol;
2356
2357 if (subarray)
2358 super->current_vol = strtoul(subarray, NULL, 10);
2359
2360 getinfo_super_imsm(st, &info, NULL);
2361 fname_from_uuid(&info, nbuf);
2362 printf(" UUID=%s", nbuf + 5);
2363
2364 super->current_vol = temp_vol;
2365 }
2366
2367 static int imsm_read_serial(int fd, char *devname, __u8 *serial,
2368 size_t serial_buf_len);
2369 static void fd2devname(int fd, char *name);
2370
2371 void print_encryption_information(int disk_fd, enum sys_dev_type hba_type)
2372 {
2373 struct encryption_information information = {0};
2374 mdadm_status_t status = MDADM_STATUS_SUCCESS;
2375 const char *indent = " ";
2376
2377 switch (hba_type) {
2378 case SYS_DEV_VMD:
2379 case SYS_DEV_NVME:
2380 status = get_nvme_opal_encryption_information(disk_fd, &information, 1);
2381 break;
2382 case SYS_DEV_SATA:
2383 case SYS_DEV_SATA_VMD:
2384 status = get_ata_encryption_information(disk_fd, &information, 1);
2385 break;
2386 default:
2387 return;
2388 }
2389
2390 if (status) {
2391 pr_err("Failed to get drive encryption information.\n");
2392 return;
2393 }
2394
2395 printf("%sEncryption(Ability|Status): %s|%s\n", indent,
2396 get_encryption_ability_string(information.ability),
2397 get_encryption_status_string(information.status));
2398 }
2399
2400 static int ahci_enumerate_ports(struct sys_dev *hba, unsigned long port_count, int host_base,
2401 int verbose)
2402 {
2403 /* dump an unsorted list of devices attached to AHCI Intel storage
2404 * controller, as well as non-connected ports
2405 */
2406 int hba_len = strlen(hba->path) + 1;
2407 struct dirent *ent;
2408 DIR *dir;
2409 char *path = NULL;
2410 int err = 0;
2411 unsigned long port_mask = (1 << port_count) - 1;
2412
2413 if (port_count > (int)sizeof(port_mask) * 8) {
2414 if (verbose > 0)
2415 pr_err("port_count %ld out of range\n", port_count);
2416 return 2;
2417 }
2418
2419 /* scroll through /sys/dev/block looking for devices attached to
2420 * this hba
2421 */
2422 dir = opendir("/sys/dev/block");
2423 if (!dir)
2424 return 1;
2425
2426 for (ent = readdir(dir); ent; ent = readdir(dir)) {
2427 int fd;
2428 char model[64];
2429 char vendor[64];
2430 char buf[1024];
2431 int major, minor;
2432 char device[PATH_MAX];
2433 char *c;
2434 int port;
2435 int type;
2436
2437 if (sscanf(ent->d_name, "%d:%d", &major, &minor) != 2)
2438 continue;
2439 path = devt_to_devpath(makedev(major, minor), 1, NULL);
2440 if (!path)
2441 continue;
2442 if (!is_path_attached_to_hba(path, hba->path)) {
2443 free(path);
2444 path = NULL;
2445 continue;
2446 }
2447
2448 /* retrieve the scsi device */
2449 if (!devt_to_devpath(makedev(major, minor), 1, device)) {
2450 if (verbose > 0)
2451 pr_err("failed to get device\n");
2452 err = 2;
2453 break;
2454 }
2455 if (devpath_to_char(device, "type", buf, sizeof(buf), 0)) {
2456 err = 2;
2457 break;
2458 }
2459 type = strtoul(buf, NULL, 10);
2460
2461 /* if it's not a disk print the vendor and model */
2462 if (!(type == 0 || type == 7 || type == 14)) {
2463 vendor[0] = '\0';
2464 model[0] = '\0';
2465
2466 if (devpath_to_char(device, "vendor", buf,
2467 sizeof(buf), 0) == 0) {
2468 strncpy(vendor, buf, sizeof(vendor));
2469 vendor[sizeof(vendor) - 1] = '\0';
2470 c = (char *) &vendor[sizeof(vendor) - 1];
2471 while (isspace(*c) || *c == '\0')
2472 *c-- = '\0';
2473
2474 }
2475
2476 if (devpath_to_char(device, "model", buf,
2477 sizeof(buf), 0) == 0) {
2478 strncpy(model, buf, sizeof(model));
2479 model[sizeof(model) - 1] = '\0';
2480 c = (char *) &model[sizeof(model) - 1];
2481 while (isspace(*c) || *c == '\0')
2482 *c-- = '\0';
2483 }
2484
2485 if (vendor[0] && model[0])
2486 sprintf(buf, "%.64s %.64s", vendor, model);
2487 else
2488 switch (type) { /* numbers from hald/linux/device.c */
2489 case 1: sprintf(buf, "tape"); break;
2490 case 2: sprintf(buf, "printer"); break;
2491 case 3: sprintf(buf, "processor"); break;
2492 case 4:
2493 case 5: sprintf(buf, "cdrom"); break;
2494 case 6: sprintf(buf, "scanner"); break;
2495 case 8: sprintf(buf, "media_changer"); break;
2496 case 9: sprintf(buf, "comm"); break;
2497 case 12: sprintf(buf, "raid"); break;
2498 default: sprintf(buf, "unknown");
2499 }
2500 } else
2501 buf[0] = '\0';
2502
2503 /* chop device path to 'host%d' and calculate the port number */
2504 c = strchr(&path[hba_len], '/');
2505 if (!c) {
2506 if (verbose > 0)
2507 pr_err("%s - invalid path name\n", path + hba_len);
2508 err = 2;
2509 break;
2510 }
2511 *c = '\0';
2512 if ((sscanf(&path[hba_len], "ata%d", &port) == 1) ||
2513 ((sscanf(&path[hba_len], "host%d", &port) == 1)))
2514 port -= host_base;
2515 else {
2516 if (verbose > 0) {
2517 *c = '/'; /* repair the full string */
2518 pr_err("failed to determine port number for %s\n",
2519 path);
2520 }
2521 err = 2;
2522 break;
2523 }
2524
2525 /* mark this port as used */
2526 port_mask &= ~(1 << port);
2527
2528 /* print out the device information */
2529 if (buf[0]) {
2530 printf(" Port%d : - non-disk device (%s) -\n", port, buf);
2531 continue;
2532 }
2533
2534 fd = dev_open(ent->d_name, O_RDONLY);
2535 if (!is_fd_valid(fd))
2536 printf(" Port%d : - disk info unavailable -\n", port);
2537 else {
2538 fd2devname(fd, buf);
2539 printf(" Port%d : %s", port, buf);
2540 if (imsm_read_serial(fd, NULL, (__u8 *)buf,
2541 sizeof(buf)) == 0)
2542 printf(" (%s)\n", buf);
2543 else
2544 printf(" ()\n");
2545
2546 print_encryption_information(fd, hba->type);
2547 close(fd);
2548 }
2549 free(path);
2550 path = NULL;
2551 }
2552 if (path)
2553 free(path);
2554 if (dir)
2555 closedir(dir);
2556 if (err == 0) {
2557 unsigned long i;
2558
2559 for (i = 0; i < port_count; i++)
2560 if (port_mask & (1L << i))
2561 printf(" Port%ld : - no device attached -\n", i);
2562 }
2563
2564 return err;
2565 }
2566
2567 static int print_nvme_info(struct sys_dev *hba)
2568 {
2569 struct dirent *ent;
2570 DIR *dir;
2571
2572 dir = opendir("/sys/block/");
2573 if (!dir)
2574 return 1;
2575
2576 for (ent = readdir(dir); ent; ent = readdir(dir)) {
2577 char ns_path[PATH_MAX];
2578 char cntrl_path[PATH_MAX];
2579 char buf[PATH_MAX];
2580 int fd = -1;
2581
2582 if (!strstr(ent->d_name, "nvme"))
2583 goto skip;
2584
2585 fd = open_dev(ent->d_name);
2586 if (!is_fd_valid(fd))
2587 goto skip;
2588
2589 if (!diskfd_to_devpath(fd, 0, ns_path) ||
2590 !diskfd_to_devpath(fd, 1, cntrl_path))
2591 goto skip;
2592
2593 if (!is_path_attached_to_hba(cntrl_path, hba->path))
2594 goto skip;
2595
2596 if (!imsm_is_nvme_namespace_supported(fd, 0))
2597 goto skip;
2598
2599 fd2devname(fd, buf);
2600 if (hba->type == SYS_DEV_VMD)
2601 printf(" NVMe under VMD : %s", buf);
2602 else if (hba->type == SYS_DEV_NVME)
2603 printf(" NVMe Device : %s", buf);
2604
2605 if (!imsm_read_serial(fd, NULL, (__u8 *)buf,
2606 sizeof(buf)))
2607 printf(" (%s)\n", buf);
2608 else
2609 printf("()\n");
2610
2611 print_encryption_information(fd, hba->type);
2612
2613 skip:
2614 close_fd(&fd);
2615 }
2616
2617 closedir(dir);
2618 return 0;
2619 }
2620
2621 static void print_found_intel_controllers(struct sys_dev *elem)
2622 {
2623 for (; elem; elem = elem->next) {
2624 pr_err("found Intel(R) ");
2625 if (elem->type == SYS_DEV_SATA)
2626 fprintf(stderr, "SATA ");
2627 else if (elem->type == SYS_DEV_SAS)
2628 fprintf(stderr, "SAS ");
2629 else if (elem->type == SYS_DEV_NVME)
2630 fprintf(stderr, "NVMe ");
2631
2632 if (elem->type == SYS_DEV_VMD)
2633 fprintf(stderr, "VMD domain");
2634 else if (elem->type == SYS_DEV_SATA_VMD)
2635 fprintf(stderr, "SATA VMD domain");
2636 else
2637 fprintf(stderr, "RAID controller");
2638
2639 if (elem->pci_id)
2640 fprintf(stderr, " at %s", elem->pci_id);
2641 fprintf(stderr, ".\n");
2642 }
2643 fflush(stderr);
2644 }
2645
2646 static int ahci_get_port_count(const char *hba_path, int *port_count)
2647 {
2648 struct dirent *ent;
2649 DIR *dir;
2650 int host_base = -1;
2651
2652 *port_count = 0;
2653 if ((dir = opendir(hba_path)) == NULL)
2654 return -1;
2655
2656 for (ent = readdir(dir); ent; ent = readdir(dir)) {
2657 int host;
2658
2659 if ((sscanf(ent->d_name, "ata%d", &host) != 1) &&
2660 ((sscanf(ent->d_name, "host%d", &host) != 1)))
2661 continue;
2662 if (*port_count == 0)
2663 host_base = host;
2664 else if (host < host_base)
2665 host_base = host;
2666
2667 if (host + 1 > *port_count + host_base)
2668 *port_count = host + 1 - host_base;
2669 }
2670 closedir(dir);
2671 return host_base;
2672 }
2673
2674 static void print_imsm_level_capability(const struct imsm_orom *orom)
2675 {
2676 int idx;
2677
2678 for (idx = 0; imsm_level_ops[idx].name; idx++)
2679 if (imsm_level_ops[idx].is_level_supported(orom))
2680 printf("%s ", imsm_level_ops[idx].name);
2681 }
2682
2683 static void print_imsm_sku_capability(const struct imsm_orom *orom)
2684 {
2685 int key_val;
2686
2687 key_val = (orom->driver_features & IMSM_OROM_CAPABILITIES_SKUMode_LOW) >>
2688 IMSM_OROM_CAPABILITIES_SKUMode_LOW_SHIFT;
2689 key_val |= (orom->driver_features & IMSM_OROM_CAPABILITIES_SKUMode_HIGH) >>
2690 IMSM_OROM_CAPABILITIES_SKUMode_HIGH_SHIFT;
2691
2692 switch (key_val) {
2693 case SKU_NO_KEY:
2694 printf("Pass-through");
2695 break;
2696 case SKU_STANDARD_KEY:
2697 printf("Standard");
2698 break;
2699 case SKU_PREMIUM_KEY:
2700 printf("Premium");
2701 break;
2702 case SKU_INTEL_SSD_ONLY_KEY:
2703 printf("Intel-SSD-only");
2704 break;
2705 case SKU_RAID1_ONLY_KEY:
2706 printf("RAID1 Only");
2707 break;
2708 default:
2709 printf("Unknown");
2710 }
2711
2712 if (orom->driver_features & IMSM_OROM_CAPABILITIES_SKUMode_NON_PRODUCTION)
2713 printf(" - for evaluation only");
2714 }
2715
2716 static void print_imsm_chunk_size_capability(const struct imsm_orom *orom)
2717 {
2718 int idx;
2719
2720 for (idx = 0; imsm_chunk_ops[idx].chunk_str; idx++)
2721 if (imsm_chunk_ops[idx].chunk & orom->sss)
2722 printf("%s ", imsm_chunk_ops[idx].chunk_str);
2723 }
2724
2725
2726 static void print_imsm_capability(const struct orom_entry *entry)
2727 {
2728 const struct imsm_orom *orom = &entry->orom;
2729
2730 printf(" Platform : Intel(R) ");
2731
2732 if (orom->capabilities == 0 && orom->driver_features == 0)
2733 printf("Matrix Storage Manager\n");
2734 else if (imsm_orom_is_enterprise(orom) && orom->major_ver >= 6)
2735 printf("Virtual RAID on CPU\n");
2736 else
2737 printf("Rapid Storage Technology%s\n",
2738 imsm_orom_is_enterprise(orom) ? " enterprise" : "");
2739
2740 if (orom->major_ver || orom->minor_ver || orom->hotfix_ver || orom->build) {
2741 if (imsm_orom_is_vmd_without_efi(orom))
2742 printf(" Version : %d.%d\n", orom->major_ver, orom->minor_ver);
2743 else
2744 printf(" Version : %d.%d.%d.%d\n", orom->major_ver, orom->minor_ver,
2745 orom->hotfix_ver, orom->build);
2746 }
2747
2748 if (entry->type == SYS_DEV_VMD) {
2749 printf(" License : ");
2750 print_imsm_sku_capability(orom);
2751 printf("\n");
2752 }
2753
2754 printf(" RAID Levels : ");
2755 print_imsm_level_capability(orom);
2756 printf("\n");
2757
2758 printf(" Chunk Sizes : ");
2759 print_imsm_chunk_size_capability(orom);
2760 printf("\n");
2761
2762 printf(" 2TB volumes :%s supported\n", (orom->attr & IMSM_OROM_ATTR_2TB) ? "" : " not");
2763
2764 printf(" 2TB disks :%s supported\n",
2765 (orom->attr & IMSM_OROM_ATTR_2TB_DISK) ? "" : " not");
2766
2767 printf(" Max Disks : %d\n", orom->tds);
2768
2769 printf(" Max Volumes : %d per array, %d per %s\n", orom->vpa, orom->vphba,
2770 imsm_orom_is_nvme(orom) ? "platform" : "controller");
2771
2772 if (entry->type == SYS_DEV_VMD || entry->type == SYS_DEV_NVME)
2773 /* This is only meaningful for controllers with nvme support */
2774 printf(" 3rd party NVMe :%s supported\n",
2775 imsm_orom_has_tpv_support(&entry->orom) ? "" : " not");
2776 return;
2777 }
2778
2779 static void print_imsm_capability_export(const struct imsm_orom *orom)
2780 {
2781 printf("MD_FIRMWARE_TYPE=imsm\n");
2782 if (orom->major_ver || orom->minor_ver || orom->hotfix_ver || orom->build)
2783 printf("IMSM_VERSION=%d.%d.%d.%d\n", orom->major_ver, orom->minor_ver,
2784 orom->hotfix_ver, orom->build);
2785
2786 printf("IMSM_SUPPORTED_RAID_LEVELS=");
2787 print_imsm_level_capability(orom);
2788 printf("\n");
2789
2790 printf("IMSM_SUPPORTED_CHUNK_SIZES=");
2791 print_imsm_chunk_size_capability(orom);
2792 printf("\n");
2793
2794 printf("IMSM_2TB_VOLUMES=%s\n",(orom->attr & IMSM_OROM_ATTR_2TB) ? "yes" : "no");
2795 printf("IMSM_2TB_DISKS=%s\n",(orom->attr & IMSM_OROM_ATTR_2TB_DISK) ? "yes" : "no");
2796 printf("IMSM_MAX_DISKS=%d\n",orom->tds);
2797 printf("IMSM_MAX_VOLUMES_PER_ARRAY=%d\n",orom->vpa);
2798 printf("IMSM_MAX_VOLUMES_PER_CONTROLLER=%d\n",orom->vphba);
2799 }
2800
2801 static int detail_platform_imsm(int verbose, int enumerate_only, char *controller_path)
2802 {
2803 /* There are two components to imsm platform support, the ahci SATA
2804 * controller and the option-rom. To find the SATA controller we
2805 * simply look in /sys/bus/pci/drivers/ahci to see if an ahci
2806 * controller with the Intel vendor id is present. This approach
2807 * allows mdadm to leverage the kernel's ahci detection logic, with the
2808 * caveat that if ahci.ko is not loaded mdadm will not be able to
2809 * detect platform raid capabilities. The option-rom resides in a
2810 * platform "Adapter ROM". We scan for its signature to retrieve the
2811 * platform capabilities. If raid support is disabled in the BIOS the
2812 * option-rom capability structure will not be available.
2813 */
2814 const struct orom_entry *entry;
2815 struct sys_dev *list, *hba;
2816 struct devid_list *devid;
2817 int port_count = 0;
2818 int host_base = 0;
2819 int result = 1;
2820
2821 if (enumerate_only) {
2822 if (check_no_platform())
2823 return 0;
2824
2825 list = find_intel_devices();
2826 if (!list)
2827 return 2;
2828
2829 for (hba = list; hba; hba = hba->next)
2830 if (find_imsm_capability(hba))
2831 return 0;
2832 return 2;
2833 }
2834
2835 list = find_intel_devices();
2836 if (!list) {
2837 if (verbose > 0)
2838 pr_err("no active Intel(R) RAID controller found.\n");
2839 return 2;
2840 } else if (verbose > 0)
2841 print_found_intel_controllers(list);
2842
2843 for (hba = list; hba; hba = hba->next) {
2844 if (controller_path && (compare_paths(hba->path, controller_path) != 0))
2845 continue;
2846 if (!find_imsm_capability(hba)) {
2847 char buf[PATH_MAX];
2848
2849 pr_err("imsm capabilities not found for controller: %s (type %s)\n",
2850 hba->type == SYS_DEV_VMD || hba->type == SYS_DEV_SATA_VMD ?
2851 vmd_domain_to_controller(hba, buf) :
2852 hba->path, get_sys_dev_type(hba->type));
2853 continue;
2854 }
2855 result = 0;
2856 }
2857
2858 if (controller_path && result == 1) {
2859 pr_err("no active Intel(R) RAID controller found under %s\n",
2860 controller_path);
2861 return result;
2862 }
2863
2864 for (entry = orom_entries; entry; entry = entry->next) {
2865 print_imsm_capability(entry);
2866
2867 if (entry->type == SYS_DEV_VMD || entry->type == SYS_DEV_NVME) {
2868 for (hba = list; hba; hba = hba->next) {
2869 char buf[PATH_MAX];
2870
2871 if (hba->type != entry->type)
2872 continue;
2873
2874 if (hba->type == SYS_DEV_VMD)
2875 printf(" I/O Controller : %s (%s)\n",
2876 vmd_domain_to_controller(hba, buf),
2877 get_sys_dev_type(hba->type));
2878
2879 print_nvme_info(hba);
2880 }
2881 printf("\n");
2882 continue;
2883 }
2884
2885 for (devid = entry->devid_list; devid; devid = devid->next) {
2886 hba = device_by_id(devid->devid);
2887 if (!hba)
2888 continue;
2889
2890 printf(" I/O Controller : %s (%s)\n",
2891 hba->path, get_sys_dev_type(hba->type));
2892 if (hba->type == SYS_DEV_SATA || hba->type == SYS_DEV_SATA_VMD) {
2893 host_base = ahci_get_port_count(hba->path, &port_count);
2894 if (ahci_enumerate_ports(hba, port_count, host_base, verbose)) {
2895 if (verbose > 0)
2896 pr_err("failed to enumerate ports on %s controller at %s.\n",
2897 get_sys_dev_type(hba->type), hba->pci_id);
2898 result |= 2;
2899 }
2900 }
2901 }
2902 printf("\n");
2903 }
2904
2905 return result;
2906 }
2907
2908 static int export_detail_platform_imsm(int verbose, char *controller_path)
2909 {
2910 struct sys_dev *list, *hba;
2911 int result=1;
2912
2913 list = find_intel_devices();
2914 if (!list) {
2915 if (verbose > 0)
2916 pr_err("IMSM_DETAIL_PLATFORM_ERROR=NO_INTEL_DEVICES\n");
2917 result = 2;
2918 return result;
2919 }
2920
2921 for (hba = list; hba; hba = hba->next) {
2922 if (controller_path && (compare_paths(hba->path,controller_path) != 0))
2923 continue;
2924 if (!find_imsm_capability(hba) && verbose > 0) {
2925 char buf[PATH_MAX];
2926 pr_err("IMSM_DETAIL_PLATFORM_ERROR=NO_IMSM_CAPABLE_DEVICE_UNDER_%s\n",
2927 hba->type == SYS_DEV_VMD || hba->type == SYS_DEV_SATA_VMD ?
2928 vmd_domain_to_controller(hba, buf) : hba->path);
2929 }
2930 else
2931 result = 0;
2932 }
2933
2934 const struct orom_entry *entry;
2935
2936 for (entry = orom_entries; entry; entry = entry->next) {
2937 if (entry->type == SYS_DEV_VMD || entry->type == SYS_DEV_SATA_VMD) {
2938 for (hba = list; hba; hba = hba->next)
2939 print_imsm_capability_export(&entry->orom);
2940 continue;
2941 }
2942 print_imsm_capability_export(&entry->orom);
2943 }
2944
2945 return result;
2946 }
2947
2948 static int match_home_imsm(struct supertype *st, char *homehost)
2949 {
2950 /* the imsm metadata format does not specify any host
2951 * identification information. We return -1 since we can never
2952 * confirm nor deny whether a given array is "meant" for this
2953 * host. We rely on compare_super and the 'family_num' fields to
2954 * exclude member disks that do not belong, and we rely on
2955 * mdadm.conf to specify the arrays that should be assembled.
2956 * Auto-assembly may still pick up "foreign" arrays.
2957 */
2958
2959 return -1;
2960 }
2961
2962 static void uuid_from_super_imsm(struct supertype *st, int uuid[4])
2963 {
2964 /* The uuid returned here is used for:
2965 * uuid to put into bitmap file (Create, Grow)
2966 * uuid for backup header when saving critical section (Grow)
2967 * comparing uuids when re-adding a device into an array
2968 * In these cases the uuid required is that of the data-array,
2969 * not the device-set.
2970 * uuid to recognise same set when adding a missing device back
2971 * to an array. This is a uuid for the device-set.
2972 *
2973 * For each of these we can make do with a truncated
2974 * or hashed uuid rather than the original, as long as
2975 * everyone agrees.
2976 * In each case the uuid required is that of the data-array,
2977 * not the device-set.
2978 */
2979 /* imsm does not track uuid's so we synthesis one using sha1 on
2980 * - The signature (Which is constant for all imsm array, but no matter)
2981 * - the orig_family_num of the container
2982 * - the index number of the volume
2983 * - the 'serial' number of the volume.
2984 * Hopefully these are all constant.
2985 */
2986 struct intel_super *super = st->sb;
2987
2988 char buf[20];
2989 struct sha1_ctx ctx;
2990 struct imsm_dev *dev = NULL;
2991 __u32 family_num;
2992
2993 /* some mdadm versions failed to set ->orig_family_num, in which
2994 * case fall back to ->family_num. orig_family_num will be
2995 * fixed up with the first metadata update.
2996 */
2997 family_num = super->anchor->orig_family_num;
2998 if (family_num == 0)
2999 family_num = super->anchor->family_num;
3000 sha1_init_ctx(&ctx);
3001 sha1_process_bytes(super->anchor->sig, MPB_SIG_LEN, &ctx);
3002 sha1_process_bytes(&family_num, sizeof(__u32), &ctx);
3003 if (super->current_vol >= 0)
3004 dev = get_imsm_dev(super, super->current_vol);
3005 if (dev) {
3006 __u32 vol = super->current_vol;
3007 sha1_process_bytes(&vol, sizeof(vol), &ctx);
3008 sha1_process_bytes(dev->volume, MAX_RAID_SERIAL_LEN, &ctx);
3009 }
3010 sha1_finish_ctx(&ctx, buf);
3011 memcpy(uuid, buf, 4*4);
3012 }
3013
3014 static __u32 migr_strip_blocks_resync(struct imsm_dev *dev)
3015 {
3016 /* migr_strip_size when repairing or initializing parity */
3017 struct imsm_map *map = get_imsm_map(dev, MAP_0);
3018 __u32 chunk = __le32_to_cpu(map->blocks_per_strip);
3019
3020 switch (get_imsm_raid_level(map)) {
3021 case 5:
3022 case 10:
3023 return chunk;
3024 default:
3025 return 128*1024 >> 9;
3026 }
3027 }
3028
3029 static __u32 migr_strip_blocks_rebuild(struct imsm_dev *dev)
3030 {
3031 /* migr_strip_size when rebuilding a degraded disk, no idea why
3032 * this is different than migr_strip_size_resync(), but it's good
3033 * to be compatible
3034 */
3035 struct imsm_map *map = get_imsm_map(dev, MAP_1);
3036 __u32 chunk = __le32_to_cpu(map->blocks_per_strip);
3037
3038 switch (get_imsm_raid_level(map)) {
3039 case 1:
3040 case 10:
3041 if (map->num_members % map->num_domains == 0)
3042 return 128*1024 >> 9;
3043 else
3044 return chunk;
3045 case 5:
3046 return max((__u32) 64*1024 >> 9, chunk);
3047 default:
3048 return 128*1024 >> 9;
3049 }
3050 }
3051
3052 static __u32 num_stripes_per_unit_resync(struct imsm_dev *dev)
3053 {
3054 struct imsm_map *lo = get_imsm_map(dev, MAP_0);
3055 struct imsm_map *hi = get_imsm_map(dev, MAP_1);
3056 __u32 lo_chunk = __le32_to_cpu(lo->blocks_per_strip);
3057 __u32 hi_chunk = __le32_to_cpu(hi->blocks_per_strip);
3058
3059 return max((__u32) 1, hi_chunk / lo_chunk);
3060 }
3061
3062 static __u32 num_stripes_per_unit_rebuild(struct imsm_dev *dev)
3063 {
3064 struct imsm_map *lo = get_imsm_map(dev, MAP_0);
3065 int level = get_imsm_raid_level(lo);
3066
3067 if (level == 1 || level == 10) {
3068 struct imsm_map *hi = get_imsm_map(dev, MAP_1);
3069
3070 return hi->num_domains;
3071 } else
3072 return num_stripes_per_unit_resync(dev);
3073 }
3074
3075 static unsigned long long calc_component_size(struct imsm_map *map,
3076 struct imsm_dev *dev)
3077 {
3078 unsigned long long component_size;
3079 unsigned long long dev_size = imsm_dev_size(dev);
3080 long long calc_dev_size = 0;
3081 unsigned int member_disks = imsm_num_data_members(map);
3082
3083 if (member_disks == 0)
3084 return 0;
3085
3086 component_size = per_dev_array_size(map);
3087 calc_dev_size = component_size * member_disks;
3088
3089 /* Component size is rounded to 1MB so difference between size from
3090 * metadata and size calculated from num_data_stripes equals up to
3091 * 2048 blocks per each device. If the difference is higher it means
3092 * that array size was expanded and num_data_stripes was not updated.
3093 */
3094 if (llabs(calc_dev_size - (long long)dev_size) >
3095 (1 << SECT_PER_MB_SHIFT) * member_disks) {
3096 component_size = dev_size / member_disks;
3097 dprintf("Invalid num_data_stripes in metadata; expected=%llu, found=%llu\n",
3098 component_size / map->blocks_per_strip,
3099 num_data_stripes(map));
3100 }
3101
3102 return component_size;
3103 }
3104
3105 static __u32 parity_segment_depth(struct imsm_dev *dev)
3106 {
3107 struct imsm_map *map = get_imsm_map(dev, MAP_0);
3108 __u32 chunk = __le32_to_cpu(map->blocks_per_strip);
3109
3110 switch(get_imsm_raid_level(map)) {
3111 case 1:
3112 case 10:
3113 return chunk * map->num_domains;
3114 case 5:
3115 return chunk * map->num_members;
3116 default:
3117 return chunk;
3118 }
3119 }
3120
3121 static __u32 map_migr_block(struct imsm_dev *dev, __u32 block)
3122 {
3123 struct imsm_map *map = get_imsm_map(dev, MAP_1);
3124 __u32 chunk = __le32_to_cpu(map->blocks_per_strip);
3125 __u32 strip = block / chunk;
3126
3127 switch (get_imsm_raid_level(map)) {
3128 case 1:
3129 case 10: {
3130 __u32 vol_strip = (strip * map->num_domains) + 1;
3131 __u32 vol_stripe = vol_strip / map->num_members;
3132
3133 return vol_stripe * chunk + block % chunk;
3134 } case 5: {
3135 __u32 stripe = strip / (map->num_members - 1);
3136
3137 return stripe * chunk + block % chunk;
3138 }
3139 default:
3140 return 0;
3141 }
3142 }
3143
3144 static __u64 blocks_per_migr_unit(struct intel_super *super,
3145 struct imsm_dev *dev)
3146 {
3147 /* calculate the conversion factor between per member 'blocks'
3148 * (md/{resync,rebuild}_start) and imsm migration units, return
3149 * 0 for the 'not migrating' and 'unsupported migration' cases
3150 */
3151 if (!dev->vol.migr_state)
3152 return 0;
3153
3154 switch (migr_type(dev)) {
3155 case MIGR_GEN_MIGR: {
3156 struct migr_record *migr_rec = super->migr_rec;
3157 return __le32_to_cpu(migr_rec->blocks_per_unit);
3158 }
3159 case MIGR_VERIFY:
3160 case MIGR_REPAIR:
3161 case MIGR_INIT: {
3162 struct imsm_map *map = get_imsm_map(dev, MAP_0);
3163 __u32 stripes_per_unit;
3164 __u32 blocks_per_unit;
3165 __u32 parity_depth;
3166 __u32 migr_chunk;
3167 __u32 block_map;
3168 __u32 block_rel;
3169 __u32 segment;
3170 __u32 stripe;
3171 __u8 disks;
3172
3173 /* yes, this is really the translation of migr_units to
3174 * per-member blocks in the 'resync' case
3175 */
3176 stripes_per_unit = num_stripes_per_unit_resync(dev);
3177 migr_chunk = migr_strip_blocks_resync(dev);
3178 disks = imsm_num_data_members(map);
3179 blocks_per_unit = stripes_per_unit * migr_chunk * disks;
3180 stripe = __le16_to_cpu(map->blocks_per_strip) * disks;
3181 segment = blocks_per_unit / stripe;
3182 block_rel = blocks_per_unit - segment * stripe;
3183 parity_depth = parity_segment_depth(dev);
3184 block_map = map_migr_block(dev, block_rel);
3185 return block_map + parity_depth * segment;
3186 }
3187 case MIGR_REBUILD: {
3188 __u32 stripes_per_unit;
3189 __u32 migr_chunk;
3190
3191 stripes_per_unit = num_stripes_per_unit_rebuild(dev);
3192 migr_chunk = migr_strip_blocks_rebuild(dev);
3193 return migr_chunk * stripes_per_unit;
3194 }
3195 case MIGR_STATE_CHANGE:
3196 default:
3197 return 0;
3198 }
3199 }
3200
3201 static int imsm_level_to_layout(int level)
3202 {
3203 switch (level) {
3204 case 0:
3205 case 1:
3206 return 0;
3207 case 5:
3208 case 6:
3209 return ALGORITHM_LEFT_ASYMMETRIC;
3210 case 10:
3211 return 0x102;
3212 }
3213 return UnSet;
3214 }
3215
3216 /*******************************************************************************
3217 * Function: read_imsm_migr_rec
3218 * Description: Function reads imsm migration record from last sector of disk
3219 * Parameters:
3220 * fd : disk descriptor
3221 * super : metadata info
3222 * Returns:
3223 * 0 : success,
3224 * -1 : fail
3225 ******************************************************************************/
3226 static int read_imsm_migr_rec(int fd, struct intel_super *super)
3227 {
3228 int ret_val = -1;
3229 unsigned int sector_size = super->sector_size;
3230 unsigned long long dsize;
3231
3232 get_dev_size(fd, NULL, &dsize);
3233 if (lseek64(fd, dsize - (sector_size*MIGR_REC_SECTOR_POSITION),
3234 SEEK_SET) < 0) {
3235 pr_err("Cannot seek to anchor block: %s\n",
3236 strerror(errno));
3237 goto out;
3238 }
3239 if ((unsigned int)read(fd, super->migr_rec_buf,
3240 MIGR_REC_BUF_SECTORS*sector_size) !=
3241 MIGR_REC_BUF_SECTORS*sector_size) {
3242 pr_err("Cannot read migr record block: %s\n",
3243 strerror(errno));
3244 goto out;
3245 }
3246 ret_val = 0;
3247 if (sector_size == 4096)
3248 convert_from_4k_imsm_migr_rec(super);
3249
3250 out:
3251 return ret_val;
3252 }
3253
3254 static struct imsm_dev *imsm_get_device_during_migration(
3255 struct intel_super *super)
3256 {
3257
3258 struct intel_dev *dv;
3259
3260 for (dv = super->devlist; dv; dv = dv->next) {
3261 if (is_gen_migration(dv->dev))
3262 return dv->dev;
3263 }
3264 return NULL;
3265 }
3266
3267 /*******************************************************************************
3268 * Function: load_imsm_migr_rec
3269 * Description: Function reads imsm migration record (it is stored at the last
3270 * sector of disk)
3271 * Parameters:
3272 * super : imsm internal array info
3273 * Returns:
3274 * 0 : success
3275 * -1 : fail
3276 * -2 : no migration in progress
3277 ******************************************************************************/
3278 static int load_imsm_migr_rec(struct intel_super *super)
3279 {
3280 struct dl *dl;
3281 char nm[30];
3282 int retval = -1;
3283 int fd = -1;
3284 struct imsm_dev *dev;
3285 struct imsm_map *map;
3286 int slot = -1;
3287 int keep_fd = 1;
3288
3289 /* find map under migration */
3290 dev = imsm_get_device_during_migration(super);
3291 /* nothing to load,no migration in progress?
3292 */
3293 if (dev == NULL)
3294 return -2;
3295
3296 map = get_imsm_map(dev, MAP_0);
3297 if (!map)
3298 return -1;
3299
3300 for (dl = super->disks; dl; dl = dl->next) {
3301 /* skip spare and failed disks
3302 */
3303 if (dl->index < 0)
3304 continue;
3305 /* read only from one of the first two slots
3306 */
3307 slot = get_imsm_disk_slot(map, dl->index);
3308 if (slot > 1 || slot < 0)
3309 continue;
3310
3311 if (!is_fd_valid(dl->fd)) {
3312 sprintf(nm, "%d:%d", dl->major, dl->minor);
3313 fd = dev_open(nm, O_RDONLY);
3314
3315 if (is_fd_valid(fd)) {
3316 keep_fd = 0;
3317 break;
3318 }
3319 } else {
3320 fd = dl->fd;
3321 break;
3322 }
3323 }
3324
3325 if (!is_fd_valid(fd))
3326 return retval;
3327 retval = read_imsm_migr_rec(fd, super);
3328 if (!keep_fd)
3329 close(fd);
3330
3331 return retval;
3332 }
3333
3334 /*******************************************************************************
3335 * function: imsm_create_metadata_checkpoint_update
3336 * Description: It creates update for checkpoint change.
3337 * Parameters:
3338 * super : imsm internal array info
3339 * u : pointer to prepared update
3340 * Returns:
3341 * Uptate length.
3342 * If length is equal to 0, input pointer u contains no update
3343 ******************************************************************************/
3344 static int imsm_create_metadata_checkpoint_update(
3345 struct intel_super *super,
3346 struct imsm_update_general_migration_checkpoint **u)
3347 {
3348
3349 int update_memory_size = 0;
3350
3351 dprintf("(enter)\n");
3352
3353 if (u == NULL)
3354 return 0;
3355 *u = NULL;
3356
3357 /* size of all update data without anchor */
3358 update_memory_size =
3359 sizeof(struct imsm_update_general_migration_checkpoint);
3360
3361 *u = xcalloc(1, update_memory_size);
3362 if (*u == NULL) {
3363 dprintf("error: cannot get memory\n");
3364 return 0;
3365 }
3366 (*u)->type = update_general_migration_checkpoint;
3367 (*u)->curr_migr_unit = current_migr_unit(super->migr_rec);
3368 dprintf("prepared for %llu\n", (unsigned long long)(*u)->curr_migr_unit);
3369
3370 return update_memory_size;
3371 }
3372
3373 static void imsm_update_metadata_locally(struct supertype *st,
3374 void *buf, int len);
3375
3376 /*******************************************************************************
3377 * Function: write_imsm_migr_rec
3378 * Description: Function writes imsm migration record
3379 * (at the last sector of disk)
3380 * Parameters:
3381 * super : imsm internal array info
3382 * Returns:
3383 * 0 : success
3384 * -1 : if fail
3385 ******************************************************************************/
3386 static int write_imsm_migr_rec(struct supertype *st)
3387 {
3388 struct intel_super *super = st->sb;
3389 unsigned int sector_size = super->sector_size;
3390 unsigned long long dsize;
3391 int retval = -1;
3392 struct dl *sd;
3393 int len;
3394 struct imsm_update_general_migration_checkpoint *u;
3395 struct imsm_dev *dev;
3396 struct imsm_map *map;
3397
3398 /* find map under migration */
3399 dev = imsm_get_device_during_migration(super);
3400 /* if no migration, write buffer anyway to clear migr_record
3401 * on disk based on first available device
3402 */
3403 if (dev == NULL)
3404 dev = get_imsm_dev(super, super->current_vol < 0 ? 0 :
3405 super->current_vol);
3406
3407 map = get_imsm_map(dev, MAP_0);
3408
3409 if (sector_size == 4096)
3410 convert_to_4k_imsm_migr_rec(super);
3411 for (sd = super->disks ; sd ; sd = sd->next) {
3412 int slot = -1;
3413
3414 /* skip failed and spare devices */
3415 if (sd->index < 0)
3416 continue;
3417 /* write to 2 first slots only */
3418 if (map)
3419 slot = get_imsm_disk_slot(map, sd->index);
3420 if (map == NULL || slot > 1 || slot < 0)
3421 continue;
3422
3423 get_dev_size(sd->fd, NULL, &dsize);
3424 if (lseek64(sd->fd, dsize - (MIGR_REC_SECTOR_POSITION *
3425 sector_size),
3426 SEEK_SET) < 0) {
3427 pr_err("Cannot seek to anchor block: %s\n",
3428 strerror(errno));
3429 goto out;
3430 }
3431 if ((unsigned int)write(sd->fd, super->migr_rec_buf,
3432 MIGR_REC_BUF_SECTORS*sector_size) !=
3433 MIGR_REC_BUF_SECTORS*sector_size) {
3434 pr_err("Cannot write migr record block: %s\n",
3435 strerror(errno));
3436 goto out;
3437 }
3438 }
3439 if (sector_size == 4096)
3440 convert_from_4k_imsm_migr_rec(super);
3441 /* update checkpoint information in metadata */
3442 len = imsm_create_metadata_checkpoint_update(super, &u);
3443 if (len <= 0) {
3444 dprintf("imsm: Cannot prepare update\n");
3445 goto out;
3446 }
3447 /* update metadata locally */
3448 imsm_update_metadata_locally(st, u, len);
3449 /* and possibly remotely */
3450 if (st->update_tail) {
3451 append_metadata_update(st, u, len);
3452 /* during reshape we do all work inside metadata handler
3453 * manage_reshape(), so metadata update has to be triggered
3454 * insida it
3455 */
3456 flush_metadata_updates(st);
3457 st->update_tail = &st->updates;
3458 } else
3459 free(u);
3460
3461 retval = 0;
3462 out:
3463 return retval;
3464 }
3465
3466 /* spare/missing disks activations are not allowe when
3467 * array/container performs reshape operation, because
3468 * all arrays in container works on the same disks set
3469 */
3470 int imsm_reshape_blocks_arrays_changes(struct intel_super *super)
3471 {
3472 int rv = 0;
3473 struct intel_dev *i_dev;
3474 struct imsm_dev *dev;
3475
3476 /* check whole container
3477 */
3478 for (i_dev = super->devlist; i_dev; i_dev = i_dev->next) {
3479 dev = i_dev->dev;
3480 if (is_gen_migration(dev)) {
3481 /* No repair during any migration in container
3482 */
3483 rv = 1;
3484 break;
3485 }
3486 }
3487 return rv;
3488 }
3489 static unsigned long long imsm_component_size_alignment_check(int level,
3490 int chunk_size,
3491 unsigned int sector_size,
3492 unsigned long long component_size)
3493 {
3494 unsigned int component_size_alignment;
3495
3496 /* check component size alignment
3497 */
3498 component_size_alignment = component_size % (chunk_size/sector_size);
3499
3500 dprintf("(Level: %i, chunk_size = %i, component_size = %llu), component_size_alignment = %u\n",
3501 level, chunk_size, component_size,
3502 component_size_alignment);
3503
3504 if (component_size_alignment && (level != 1) && (level != UnSet)) {
3505 dprintf("imsm: reported component size aligned from %llu ",
3506 component_size);
3507 component_size -= component_size_alignment;
3508 dprintf_cont("to %llu (%i).\n",
3509 component_size, component_size_alignment);
3510 }
3511
3512 return component_size;
3513 }
3514
3515 /*******************************************************************************
3516 * Function: get_bitmap_header_sector
3517 * Description: Returns the sector where the bitmap header is placed.
3518 * Parameters:
3519 * st : supertype information
3520 * dev_idx : index of the device with bitmap
3521 *
3522 * Returns:
3523 * The sector where the bitmap header is placed
3524 ******************************************************************************/
3525 static unsigned long long get_bitmap_header_sector(struct intel_super *super,
3526 int dev_idx)
3527 {
3528 struct imsm_dev *dev = get_imsm_dev(super, dev_idx);
3529 struct imsm_map *map = get_imsm_map(dev, MAP_0);
3530
3531 if (!super->sector_size) {
3532 dprintf("sector size is not set\n");
3533 return 0;
3534 }
3535
3536 return pba_of_lba0(map) + calc_component_size(map, dev) +
3537 (IMSM_BITMAP_HEADER_OFFSET / super->sector_size);
3538 }
3539
3540 /*******************************************************************************
3541 * Function: get_bitmap_sector
3542 * Description: Returns the sector where the bitmap is placed.
3543 * Parameters:
3544 * st : supertype information
3545 * dev_idx : index of the device with bitmap
3546 *
3547 * Returns:
3548 * The sector where the bitmap is placed
3549 ******************************************************************************/
3550 static unsigned long long get_bitmap_sector(struct intel_super *super,
3551 int dev_idx)
3552 {
3553 if (!super->sector_size) {
3554 dprintf("sector size is not set\n");
3555 return 0;
3556 }
3557
3558 return get_bitmap_header_sector(super, dev_idx) +
3559 (IMSM_BITMAP_HEADER_SIZE / super->sector_size);
3560 }
3561
3562 static unsigned long long get_ppl_sector(struct intel_super *super, int dev_idx)
3563 {
3564 struct imsm_dev *dev = get_imsm_dev(super, dev_idx);
3565 struct imsm_map *map = get_imsm_map(dev, MAP_0);
3566
3567 return pba_of_lba0(map) +
3568 (num_data_stripes(map) * map->blocks_per_strip);
3569 }
3570
3571 static void getinfo_super_imsm_volume(struct supertype *st, struct mdinfo *info, char *dmap)
3572 {
3573 struct intel_super *super = st->sb;
3574 struct migr_record *migr_rec = super->migr_rec;
3575 struct imsm_dev *dev = get_imsm_dev(super, super->current_vol);
3576 struct imsm_map *map = get_imsm_map(dev, MAP_0);
3577 struct imsm_map *prev_map = get_imsm_map(dev, MAP_1);
3578 struct imsm_map *map_to_analyse = map;
3579 struct dl *dl;
3580 int map_disks = info->array.raid_disks;
3581
3582 memset(info, 0, sizeof(*info));
3583 if (prev_map)
3584 map_to_analyse = prev_map;
3585
3586 dl = super->current_disk;
3587
3588 info->container_member = super->current_vol;
3589 info->array.raid_disks = map->num_members;
3590 info->array.level = get_imsm_raid_level(map_to_analyse);
3591 info->array.layout = imsm_level_to_layout(info->array.level);
3592 info->array.md_minor = -1;
3593 info->array.ctime = 0;
3594 info->array.utime = 0;
3595 info->array.chunk_size =
3596 __le16_to_cpu(map_to_analyse->blocks_per_strip) << 9;
3597 info->array.state = !(dev->vol.dirty & RAIDVOL_DIRTY);
3598 info->custom_array_size = imsm_dev_size(dev);
3599 info->recovery_blocked = imsm_reshape_blocks_arrays_changes(st->sb);
3600
3601 if (is_gen_migration(dev)) {
3602 /*
3603 * device prev_map should be added if it is in the middle
3604 * of migration
3605 */
3606 assert(prev_map);
3607
3608 info->reshape_active = 1;
3609 info->new_level = get_imsm_raid_level(map);
3610 info->new_layout = imsm_level_to_layout(info->new_level);
3611 info->new_chunk = __le16_to_cpu(map->blocks_per_strip) << 9;
3612 info->delta_disks = map->num_members - prev_map->num_members;
3613 if (info->delta_disks) {
3614 /* this needs to be applied to every array
3615 * in the container.
3616 */
3617 info->reshape_active = CONTAINER_RESHAPE;
3618 }
3619 /* We shape information that we give to md might have to be
3620 * modify to cope with md's requirement for reshaping arrays.
3621 * For example, when reshaping a RAID0, md requires it to be
3622 * presented as a degraded RAID4.
3623 * Also if a RAID0 is migrating to a RAID5 we need to specify
3624 * the array as already being RAID5, but the 'before' layout
3625 * is a RAID4-like layout.
3626 */
3627 switch (info->array.level) {
3628 case 0:
3629 switch(info->new_level) {
3630 case 0:
3631 /* conversion is happening as RAID4 */
3632 info->array.level = 4;
3633 info->array.raid_disks += 1;
3634 break;
3635 case 5:
3636 /* conversion is happening as RAID5 */
3637 info->array.level = 5;
3638 info->array.layout = ALGORITHM_PARITY_N;
3639 info->delta_disks -= 1;
3640 break;
3641 default:
3642 /* FIXME error message */
3643 info->array.level = UnSet;
3644 break;
3645 }
3646 break;
3647 }
3648 } else {
3649 info->new_level = UnSet;
3650 info->new_layout = UnSet;
3651 info->new_chunk = info->array.chunk_size;
3652 info->delta_disks = 0;
3653 }
3654
3655 if (dl) {
3656 info->disk.major = dl->major;
3657 info->disk.minor = dl->minor;
3658 info->disk.number = dl->index;
3659 info->disk.raid_disk = get_imsm_disk_slot(map_to_analyse,
3660 dl->index);
3661 }
3662
3663 info->data_offset = pba_of_lba0(map_to_analyse);
3664 info->component_size = calc_component_size(map, dev);
3665 info->component_size = imsm_component_size_alignment_check(
3666 info->array.level,
3667 info->array.chunk_size,
3668 super->sector_size,
3669 info->component_size);
3670 info->bb.supported = 1;
3671
3672 memset(info->uuid, 0, sizeof(info->uuid));
3673 info->recovery_start = MaxSector;
3674
3675 if (info->array.level == 5 &&
3676 (dev->rwh_policy == RWH_DISTRIBUTED ||
3677 dev->rwh_policy == RWH_MULTIPLE_DISTRIBUTED)) {
3678 info->consistency_policy = CONSISTENCY_POLICY_PPL;
3679 info->ppl_sector = get_ppl_sector(super, super->current_vol);
3680 if (dev->rwh_policy == RWH_MULTIPLE_DISTRIBUTED)
3681 info->ppl_size = MULTIPLE_PPL_AREA_SIZE_IMSM >> 9;
3682 else
3683 info->ppl_size = (PPL_HEADER_SIZE + PPL_ENTRY_SPACE)
3684 >> 9;
3685 } else if (info->array.level <= 0) {
3686 info->consistency_policy = CONSISTENCY_POLICY_NONE;
3687 } else {
3688 if (dev->rwh_policy == RWH_BITMAP) {
3689 info->bitmap_offset = get_bitmap_sector(super, super->current_vol);
3690 info->consistency_policy = CONSISTENCY_POLICY_BITMAP;
3691 } else {
3692 info->consistency_policy = CONSISTENCY_POLICY_RESYNC;
3693 }
3694 }
3695
3696 info->reshape_progress = 0;
3697 info->resync_start = MaxSector;
3698 if ((map_to_analyse->map_state == IMSM_T_STATE_UNINITIALIZED ||
3699 !(info->array.state & 1)) &&
3700 imsm_reshape_blocks_arrays_changes(super) == 0) {
3701 info->resync_start = 0;
3702 }
3703 if (dev->vol.migr_state) {
3704 switch (migr_type(dev)) {
3705 case MIGR_REPAIR:
3706 case MIGR_INIT: {
3707 __u64 blocks_per_unit = blocks_per_migr_unit(super,
3708 dev);
3709 __u64 units = vol_curr_migr_unit(dev);
3710
3711 info->resync_start = blocks_per_unit * units;
3712 break;
3713 }
3714 case MIGR_GEN_MIGR: {
3715 __u64 blocks_per_unit = blocks_per_migr_unit(super,
3716 dev);
3717 __u64 units = current_migr_unit(migr_rec);
3718 int used_disks;
3719
3720 if (__le32_to_cpu(migr_rec->ascending_migr) &&
3721 (units <
3722 (get_num_migr_units(migr_rec)-1)) &&
3723 (super->migr_rec->rec_status ==
3724 __cpu_to_le32(UNIT_SRC_IN_CP_AREA)))
3725 units++;
3726
3727 info->reshape_progress = blocks_per_unit * units;
3728
3729 dprintf("IMSM: General Migration checkpoint : %llu (%llu) -> read reshape progress : %llu\n",
3730 (unsigned long long)units,
3731 (unsigned long long)blocks_per_unit,
3732 info->reshape_progress);
3733
3734 used_disks = imsm_num_data_members(prev_map);
3735 if (used_disks > 0) {
3736 info->custom_array_size = per_dev_array_size(map) *
3737 used_disks;
3738 }
3739 }
3740 case MIGR_VERIFY:
3741 /* we could emulate the checkpointing of
3742 * 'sync_action=check' migrations, but for now
3743 * we just immediately complete them
3744 */
3745 case MIGR_REBUILD:
3746 /* this is handled by container_content_imsm() */
3747 case MIGR_STATE_CHANGE:
3748 /* FIXME handle other migrations */
3749 default:
3750 /* we are not dirty, so... */
3751 info->resync_start = MaxSector;
3752 }
3753 }
3754
3755 strncpy(info->name, (char *) dev->volume, MAX_RAID_SERIAL_LEN);
3756 info->name[MAX_RAID_SERIAL_LEN] = 0;
3757
3758 info->array.major_version = -1;
3759 info->array.minor_version = -2;
3760 sprintf(info->text_version, "/%s/%d", st->container_devnm, info->container_member);
3761 info->safe_mode_delay = 4000; /* 4 secs like the Matrix driver */
3762 uuid_from_super_imsm(st, info->uuid);
3763
3764 if (dmap) {
3765 int i, j;
3766 for (i=0; i<map_disks; i++) {
3767 dmap[i] = 0;
3768 if (i < info->array.raid_disks) {
3769 struct imsm_disk *dsk;
3770 j = get_imsm_disk_idx(dev, i, MAP_X);
3771 dsk = get_imsm_disk(super, j);
3772 if (dsk && (dsk->status & CONFIGURED_DISK))
3773 dmap[i] = 1;
3774 }
3775 }
3776 }
3777 }
3778
3779 static __u8 imsm_check_degraded(struct intel_super *super, struct imsm_dev *dev,
3780 int failed, int look_in_map);
3781
3782 static int imsm_count_failed(struct intel_super *super, struct imsm_dev *dev,
3783 int look_in_map);
3784
3785 static void manage_second_map(struct intel_super *super, struct imsm_dev *dev)
3786 {
3787 if (is_gen_migration(dev)) {
3788 int failed;
3789 __u8 map_state;
3790 struct imsm_map *map2 = get_imsm_map(dev, MAP_1);
3791
3792 failed = imsm_count_failed(super, dev, MAP_1);
3793 map_state = imsm_check_degraded(super, dev, failed, MAP_1);
3794 if (map2->map_state != map_state) {
3795 map2->map_state = map_state;
3796 super->updates_pending++;
3797 }
3798 }
3799 }
3800
3801 static struct imsm_disk *get_imsm_missing(struct intel_super *super, __u8 index)
3802 {
3803 struct dl *d;
3804
3805 for (d = super->missing; d; d = d->next)
3806 if (d->index == index)
3807 return &d->disk;
3808 return NULL;
3809 }
3810
3811 static void getinfo_super_imsm(struct supertype *st, struct mdinfo *info, char *map)
3812 {
3813 struct intel_super *super = st->sb;
3814 struct imsm_disk *disk;
3815 int map_disks = info->array.raid_disks;
3816 int max_enough = -1;
3817 int i;
3818 struct imsm_super *mpb;
3819
3820 if (super->current_vol >= 0) {
3821 getinfo_super_imsm_volume(st, info, map);
3822 return;
3823 }
3824 memset(info, 0, sizeof(*info));
3825
3826 /* Set raid_disks to zero so that Assemble will always pull in valid
3827 * spares
3828 */
3829 info->array.raid_disks = 0;
3830 info->array.level = LEVEL_CONTAINER;
3831 info->array.layout = 0;
3832 info->array.md_minor = -1;
3833 info->array.ctime = 0; /* N/A for imsm */
3834 info->array.utime = 0;
3835 info->array.chunk_size = 0;
3836
3837 info->disk.major = 0;
3838 info->disk.minor = 0;
3839 info->disk.raid_disk = -1;
3840 info->reshape_active = 0;
3841 info->array.major_version = -1;
3842 info->array.minor_version = -2;
3843 strcpy(info->text_version, "imsm");
3844 info->safe_mode_delay = 0;
3845 info->disk.number = -1;
3846 info->disk.state = 0;
3847 info->name[0] = 0;
3848 info->recovery_start = MaxSector;
3849 info->recovery_blocked = imsm_reshape_blocks_arrays_changes(st->sb);
3850 info->bb.supported = 1;
3851
3852 /* do we have the all the insync disks that we expect? */
3853 mpb = super->anchor;
3854 info->events = __le32_to_cpu(mpb->generation_num);
3855
3856 for (i = 0; i < mpb->num_raid_devs; i++) {
3857 struct imsm_dev *dev = get_imsm_dev(super, i);
3858 int failed, enough, j, missing = 0;
3859 struct imsm_map *map;
3860 __u8 state;
3861
3862 failed = imsm_count_failed(super, dev, MAP_0);
3863 state = imsm_check_degraded(super, dev, failed, MAP_0);
3864 map = get_imsm_map(dev, MAP_0);
3865
3866 /* any newly missing disks?
3867 * (catches single-degraded vs double-degraded)
3868 */
3869 for (j = 0; j < map->num_members; j++) {
3870 __u32 ord = get_imsm_ord_tbl_ent(dev, j, MAP_0);
3871 __u32 idx = ord_to_idx(ord);
3872
3873 if (super->disks && super->disks->index == (int)idx)
3874 info->disk.raid_disk = j;
3875
3876 if (!(ord & IMSM_ORD_REBUILD) &&
3877 get_imsm_missing(super, idx)) {
3878 missing = 1;
3879 break;
3880 }
3881 }
3882
3883 if (state == IMSM_T_STATE_FAILED)
3884 enough = -1;
3885 else if (state == IMSM_T_STATE_DEGRADED &&
3886 (state != map->map_state || missing))
3887 enough = 0;
3888 else /* we're normal, or already degraded */
3889 enough = 1;
3890 if (is_gen_migration(dev) && missing) {
3891 /* during general migration we need all disks
3892 * that process is running on.
3893 * No new missing disk is allowed.
3894 */
3895 max_enough = -1;
3896 enough = -1;
3897 /* no more checks necessary
3898 */
3899 break;
3900 }
3901 /* in the missing/failed disk case check to see
3902 * if at least one array is runnable
3903 */
3904 max_enough = max(max_enough, enough);
3905 }
3906
3907 info->container_enough = max_enough;
3908
3909 if (super->disks) {
3910 __u32 reserved = imsm_reserved_sectors(super, super->disks);
3911
3912 disk = &super->disks->disk;
3913 info->data_offset = total_blocks(&super->disks->disk) - reserved;
3914 info->component_size = reserved;
3915 info->disk.state = is_configured(disk) ? (1 << MD_DISK_ACTIVE) : 0;
3916 /* we don't change info->disk.raid_disk here because
3917 * this state will be finalized in mdmon after we have
3918 * found the 'most fresh' version of the metadata
3919 */
3920 info->disk.state |= is_failed(disk) ? (1 << MD_DISK_FAULTY) : 0;
3921 info->disk.state |= (is_spare(disk) || is_journal(disk)) ?
3922 0 : (1 << MD_DISK_SYNC);
3923 }
3924
3925 /* only call uuid_from_super_imsm when this disk is part of a populated container,
3926 * ->compare_super may have updated the 'num_raid_devs' field for spares
3927 */
3928 if (info->disk.state & (1 << MD_DISK_SYNC) || super->anchor->num_raid_devs)
3929 uuid_from_super_imsm(st, info->uuid);
3930 else
3931 memcpy(info->uuid, uuid_zero, sizeof(uuid_zero));
3932
3933 /* I don't know how to compute 'map' on imsm, so use safe default */
3934 if (map) {
3935 int i;
3936 for (i = 0; i < map_disks; i++)
3937 map[i] = 1;
3938 }
3939
3940 }
3941
3942 /* allocates memory and fills disk in mdinfo structure
3943 * for each disk in array */
3944 struct mdinfo *getinfo_super_disks_imsm(struct supertype *st)
3945 {
3946 struct mdinfo *mddev;
3947 struct intel_super *super = st->sb;
3948 struct imsm_disk *disk;
3949 int count = 0;
3950 struct dl *dl;
3951 if (!super || !super->disks)
3952 return NULL;
3953 dl = super->disks;
3954 mddev = xcalloc(1, sizeof(*mddev));
3955 while (dl) {
3956 struct mdinfo *tmp;
3957 disk = &dl->disk;
3958 tmp = xcalloc(1, sizeof(*tmp));
3959 if (mddev->devs)
3960 tmp->next = mddev->devs;
3961 mddev->devs = tmp;
3962 tmp->disk.number = count++;
3963 tmp->disk.major = dl->major;
3964 tmp->disk.minor = dl->minor;
3965 tmp->disk.state = is_configured(disk) ?
3966 (1 << MD_DISK_ACTIVE) : 0;
3967 tmp->disk.state |= is_failed(disk) ? (1 << MD_DISK_FAULTY) : 0;
3968 tmp->disk.state |= is_spare(disk) ? 0 : (1 << MD_DISK_SYNC);
3969 tmp->disk.raid_disk = -1;
3970 dl = dl->next;
3971 }
3972 return mddev;
3973 }
3974
3975 static int update_super_imsm(struct supertype *st, struct mdinfo *info,
3976 enum update_opt update, char *devname,
3977 int verbose, int uuid_set, char *homehost)
3978 {
3979 /* For 'assemble' and 'force' we need to return non-zero if any
3980 * change was made. For others, the return value is ignored.
3981 * Update options are:
3982 * force-one : This device looks a bit old but needs to be included,
3983 * update age info appropriately.
3984 * assemble: clear any 'faulty' flag to allow this device to
3985 * be assembled.
3986 * force-array: Array is degraded but being forced, mark it clean
3987 * if that will be needed to assemble it.
3988 *
3989 * newdev: not used ????
3990 * grow: Array has gained a new device - this is currently for
3991 * linear only
3992 * resync: mark as dirty so a resync will happen.
3993 * name: update the name - preserving the homehost
3994 * uuid: Change the uuid of the array to match watch is given
3995 *
3996 * Following are not relevant for this imsm:
3997 * sparc2.2 : update from old dodgey metadata
3998 * super-minor: change the preferred_minor number
3999 * summaries: update redundant counters.
4000 * homehost: update the recorded homehost
4001 * _reshape_progress: record new reshape_progress position.
4002 */
4003 int rv = 1;
4004 struct intel_super *super = st->sb;
4005 struct imsm_super *mpb;
4006
4007 /* we can only update container info */
4008 if (!super || super->current_vol >= 0 || !super->anchor)
4009 return 1;
4010
4011 mpb = super->anchor;
4012
4013 switch (update) {
4014 case UOPT_UUID:
4015 /* We take this to mean that the family_num should be updated.
4016 * However that is much smaller than the uuid so we cannot really
4017 * allow an explicit uuid to be given. And it is hard to reliably
4018 * know if one was.
4019 * So if !uuid_set we know the current uuid is random and just used
4020 * the first 'int' and copy it to the other 3 positions.
4021 * Otherwise we require the 4 'int's to be the same as would be the
4022 * case if we are using a random uuid. So an explicit uuid will be
4023 * accepted as long as all for ints are the same... which shouldn't hurt
4024 */
4025 if (!uuid_set) {
4026 info->uuid[1] = info->uuid[2] = info->uuid[3] = info->uuid[0];
4027 rv = 0;
4028 } else {
4029 if (info->uuid[0] != info->uuid[1] ||
4030 info->uuid[1] != info->uuid[2] ||
4031 info->uuid[2] != info->uuid[3])
4032 rv = -1;
4033 else
4034 rv = 0;
4035 }
4036 if (rv == 0)
4037 mpb->orig_family_num = info->uuid[0];
4038 break;
4039 case UOPT_SPEC_ASSEMBLE:
4040 rv = 0;
4041 break;
4042 default:
4043 rv = -1;
4044 break;
4045 }
4046
4047 /* successful update? recompute checksum */
4048 if (rv == 0)
4049 mpb->check_sum = __le32_to_cpu(__gen_imsm_checksum(mpb));
4050
4051 return rv;
4052 }
4053
4054 static size_t disks_to_mpb_size(int disks)
4055 {
4056 size_t size;
4057
4058 size = sizeof(struct imsm_super);
4059 size += (disks - 1) * sizeof(struct imsm_disk);
4060 size += 2 * sizeof(struct imsm_dev);
4061 /* up to 2 maps per raid device (-2 for imsm_maps in imsm_dev */
4062 size += (4 - 2) * sizeof(struct imsm_map);
4063 /* 4 possible disk_ord_tbl's */
4064 size += 4 * (disks - 1) * sizeof(__u32);
4065 /* maximum bbm log */
4066 size += sizeof(struct bbm_log);
4067
4068 return size;
4069 }
4070
4071 static __u64 avail_size_imsm(struct supertype *st, __u64 devsize,
4072 unsigned long long data_offset)
4073 {
4074 if (devsize < (MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS))
4075 return 0;
4076
4077 return devsize - (MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS);
4078 }
4079
4080 static void free_devlist(struct intel_super *super)
4081 {
4082 struct intel_dev *dv;
4083
4084 while (super->devlist) {
4085 dv = super->devlist->next;
4086 free(super->devlist->dev);
4087 free(super->devlist);
4088 super->devlist = dv;
4089 }
4090 }
4091
4092 static void imsm_copy_dev(struct imsm_dev *dest, struct imsm_dev *src)
4093 {
4094 memcpy(dest, src, sizeof_imsm_dev(src, 0));
4095 }
4096
4097 static int compare_super_imsm(struct supertype *st, struct supertype *tst,
4098 int verbose)
4099 {
4100 /* return:
4101 * 0 same, or first was empty, and second was copied
4102 * 1 sb are different
4103 */
4104 struct intel_super *first = st->sb;
4105 struct intel_super *sec = tst->sb;
4106
4107 if (!first) {
4108 st->sb = tst->sb;
4109 tst->sb = NULL;
4110 return 0;
4111 }
4112
4113 /* in platform dependent environment test if the disks
4114 * use the same Intel hba
4115 * if not on Intel hba at all, allow anything.
4116 * doesn't check HBAs if num_raid_devs is not set, as it means
4117 * it is a free floating spare, and all spares regardless of HBA type
4118 * will fall into separate container during the assembly
4119 */
4120 if (first->hba && sec->hba && first->anchor->num_raid_devs != 0) {
4121 if (first->hba->type != sec->hba->type) {
4122 if (verbose)
4123 pr_err("HBAs of devices do not match %s != %s\n",
4124 get_sys_dev_type(first->hba->type),
4125 get_sys_dev_type(sec->hba->type));
4126 return 1;
4127 }
4128 if (first->orom != sec->orom) {
4129 if (verbose)
4130 pr_err("HBAs of devices do not match %s != %s\n",
4131 first->hba->pci_id, sec->hba->pci_id);
4132 return 1;
4133 }
4134 }
4135
4136 if (first->anchor->num_raid_devs > 0 &&
4137 sec->anchor->num_raid_devs > 0) {
4138 /* Determine if these disks might ever have been
4139 * related. Further disambiguation can only take place
4140 * in load_super_imsm_all
4141 */
4142 __u32 first_family = first->anchor->orig_family_num;
4143 __u32 sec_family = sec->anchor->orig_family_num;
4144
4145 if (memcmp(first->anchor->sig, sec->anchor->sig,
4146 MAX_SIGNATURE_LENGTH) != 0)
4147 return 1;
4148
4149 if (first_family == 0)
4150 first_family = first->anchor->family_num;
4151 if (sec_family == 0)
4152 sec_family = sec->anchor->family_num;
4153
4154 if (first_family != sec_family)
4155 return 1;
4156
4157 }
4158
4159 /* if an anchor does not have num_raid_devs set then it is a free
4160 * floating spare. don't assosiate spare with any array, as during assembly
4161 * spares shall fall into separate container, from which they can be moved
4162 * when necessary
4163 */
4164 if (first->anchor->num_raid_devs ^ sec->anchor->num_raid_devs)
4165 return 1;
4166
4167 return 0;
4168 }
4169
4170 static void fd2devname(int fd, char *name)
4171 {
4172 char *nm;
4173
4174 nm = fd2kname(fd);
4175 if (!nm)
4176 return;
4177
4178 snprintf(name, MAX_RAID_SERIAL_LEN, "/dev/%s", nm);
4179 }
4180
4181 static int nvme_get_serial(int fd, void *buf, size_t buf_len)
4182 {
4183 char path[PATH_MAX];
4184 char *name = fd2kname(fd);
4185
4186 if (!name)
4187 return 1;
4188
4189 if (strncmp(name, "nvme", 4) != 0)
4190 return 1;
4191
4192 if (!diskfd_to_devpath(fd, 1, path))
4193 return 1;
4194
4195 return devpath_to_char(path, "serial", buf, buf_len, 0);
4196 }
4197
4198 mdadm_status_t scsi_get_serial(int fd, void *buf, size_t buf_len)
4199 {
4200 struct sg_io_hdr io_hdr = {0};
4201 unsigned char rsp_buf[255];
4202 unsigned char inq_cmd[] = {INQUIRY, 1, 0x80, 0, sizeof(rsp_buf), 0};
4203 unsigned char sense[32];
4204 unsigned int rsp_len;
4205 int rv;
4206
4207 io_hdr.interface_id = 'S';
4208 io_hdr.cmdp = inq_cmd;
4209 io_hdr.cmd_len = sizeof(inq_cmd);
4210 io_hdr.dxferp = rsp_buf;
4211 io_hdr.dxfer_len = sizeof(rsp_buf);
4212 io_hdr.dxfer_direction = SG_DXFER_FROM_DEV;
4213 io_hdr.sbp = sense;
4214 io_hdr.mx_sb_len = sizeof(sense);
4215 io_hdr.timeout = 5000;
4216
4217 rv = ioctl(fd, SG_IO, &io_hdr);
4218
4219 if (rv)
4220 return MDADM_STATUS_ERROR;
4221
4222 if ((io_hdr.info & SG_INFO_OK_MASK) != SG_INFO_OK)
4223 return MDADM_STATUS_ERROR;
4224
4225 rsp_len = rsp_buf[3];
4226
4227 if (!rsp_len || buf_len < rsp_len)
4228 return MDADM_STATUS_ERROR;
4229
4230 memcpy(buf, &rsp_buf[4], rsp_len);
4231
4232 return MDADM_STATUS_SUCCESS;
4233 }
4234
4235
4236 static int imsm_read_serial(int fd, char *devname,
4237 __u8 *serial, size_t serial_buf_len)
4238 {
4239 char buf[50];
4240 int rv;
4241 size_t len;
4242 char *dest;
4243 char *src;
4244 unsigned int i;
4245
4246 memset(buf, 0, sizeof(buf));
4247
4248 if (check_env("IMSM_DEVNAME_AS_SERIAL")) {
4249 memset(serial, 0, serial_buf_len);
4250 fd2devname(fd, (char *) serial);
4251 return 0;
4252 }
4253
4254 rv = nvme_get_serial(fd, buf, sizeof(buf));
4255
4256 if (rv)
4257 rv = scsi_get_serial(fd, buf, sizeof(buf));
4258
4259 if (rv != 0) {
4260 if (devname)
4261 pr_err("Failed to retrieve serial for %s\n",
4262 devname);
4263 return rv;
4264 }
4265
4266 /* trim all whitespace and non-printable characters and convert
4267 * ':' to ';'
4268 */
4269 for (i = 0, dest = buf; i < sizeof(buf) && buf[i]; i++) {
4270 src = &buf[i];
4271 if (*src > 0x20) {
4272 /* ':' is reserved for use in placeholder serial
4273 * numbers for missing disks
4274 */
4275 if (*src == ':')
4276 *dest++ = ';';
4277 else
4278 *dest++ = *src;
4279 }
4280 }
4281 len = dest - buf;
4282 dest = buf;
4283
4284 if (len > serial_buf_len) {
4285 /* truncate leading characters */
4286 dest += len - serial_buf_len;
4287 len = serial_buf_len;
4288 }
4289
4290 memset(serial, 0, serial_buf_len);
4291 memcpy(serial, dest, len);
4292
4293 return 0;
4294 }
4295
4296 static int serialcmp(__u8 *s1, __u8 *s2)
4297 {
4298 return strncmp((char *) s1, (char *) s2, MAX_RAID_SERIAL_LEN);
4299 }
4300
4301 static void serialcpy(__u8 *dest, __u8 *src)
4302 {
4303 strncpy((char *) dest, (char *) src, MAX_RAID_SERIAL_LEN);
4304 }
4305
4306 static struct dl *serial_to_dl(__u8 *serial, struct intel_super *super)
4307 {
4308 struct dl *dl;
4309
4310 for (dl = super->disks; dl; dl = dl->next)
4311 if (serialcmp(dl->serial, serial) == 0)
4312 break;
4313
4314 return dl;
4315 }
4316
4317 static struct imsm_disk *
4318 __serial_to_disk(__u8 *serial, struct imsm_super *mpb, int *idx)
4319 {
4320 int i;
4321
4322 for (i = 0; i < mpb->num_disks; i++) {
4323 struct imsm_disk *disk = __get_imsm_disk(mpb, i);
4324
4325 if (serialcmp(disk->serial, serial) == 0) {
4326 if (idx)
4327 *idx = i;
4328 return disk;
4329 }
4330 }
4331
4332 return NULL;
4333 }
4334
4335 static int
4336 load_imsm_disk(int fd, struct intel_super *super, char *devname, int keep_fd)
4337 {
4338 struct imsm_disk *disk;
4339 struct dl *dl;
4340 struct stat stb;
4341 int rv;
4342 char name[40];
4343 __u8 serial[MAX_RAID_SERIAL_LEN];
4344
4345 rv = imsm_read_serial(fd, devname, serial, MAX_RAID_SERIAL_LEN);
4346
4347 if (rv != 0)
4348 return 2;
4349
4350 dl = xcalloc(1, sizeof(*dl));
4351
4352 if (fstat(fd, &stb) != 0) {
4353 free(dl);
4354 return 1;
4355 }
4356 dl->major = major(stb.st_rdev);
4357 dl->minor = minor(stb.st_rdev);
4358 dl->next = super->disks;
4359 dl->fd = keep_fd ? fd : -1;
4360 assert(super->disks == NULL);
4361 super->disks = dl;
4362 serialcpy(dl->serial, serial);
4363 dl->index = -2;
4364 dl->e = NULL;
4365 fd2devname(fd, name);
4366 if (devname)
4367 dl->devname = xstrdup(devname);
4368 else
4369 dl->devname = xstrdup(name);
4370
4371 /* look up this disk's index in the current anchor */
4372 disk = __serial_to_disk(dl->serial, super->anchor, &dl->index);
4373 if (disk) {
4374 dl->disk = *disk;
4375 /* only set index on disks that are a member of a
4376 * populated contianer, i.e. one with raid_devs
4377 */
4378 if (is_failed(&dl->disk))
4379 dl->index = -2;
4380 else if (is_spare(&dl->disk) || is_journal(&dl->disk))
4381 dl->index = -1;
4382 }
4383
4384 return 0;
4385 }
4386
4387 /* When migrating map0 contains the 'destination' state while map1
4388 * contains the current state. When not migrating map0 contains the
4389 * current state. This routine assumes that map[0].map_state is set to
4390 * the current array state before being called.
4391 *
4392 * Migration is indicated by one of the following states
4393 * 1/ Idle (migr_state=0 map0state=normal||unitialized||degraded||failed)
4394 * 2/ Initialize (migr_state=1 migr_type=MIGR_INIT map0state=normal
4395 * map1state=unitialized)
4396 * 3/ Repair (Resync) (migr_state=1 migr_type=MIGR_REPAIR map0state=normal
4397 * map1state=normal)
4398 * 4/ Rebuild (migr_state=1 migr_type=MIGR_REBUILD map0state=normal
4399 * map1state=degraded)
4400 * 5/ Migration (mig_state=1 migr_type=MIGR_GEN_MIGR map0state=normal
4401 * map1state=normal)
4402 */
4403 static void migrate(struct imsm_dev *dev, struct intel_super *super,
4404 __u8 to_state, int migr_type)
4405 {
4406 struct imsm_map *dest;
4407 struct imsm_map *src = get_imsm_map(dev, MAP_0);
4408
4409 dev->vol.migr_state = MIGR_STATE_MIGRATING;
4410 set_migr_type(dev, migr_type);
4411 set_vol_curr_migr_unit(dev, 0);
4412 dest = get_imsm_map(dev, MAP_1);
4413
4414 /* duplicate and then set the target end state in map[0] */
4415 memcpy(dest, src, sizeof_imsm_map(src));
4416 if (migr_type == MIGR_GEN_MIGR) {
4417 __u32 ord;
4418 int i;
4419
4420 for (i = 0; i < src->num_members; i++) {
4421 ord = __le32_to_cpu(src->disk_ord_tbl[i]);
4422 set_imsm_ord_tbl_ent(src, i, ord_to_idx(ord));
4423 }
4424 }
4425
4426 if (migr_type == MIGR_GEN_MIGR)
4427 /* Clear migration record */
4428 memset(super->migr_rec, 0, sizeof(struct migr_record));
4429
4430 src->map_state = to_state;
4431 }
4432
4433 static void end_migration(struct imsm_dev *dev, struct intel_super *super,
4434 __u8 map_state)
4435 {
4436 /* To avoid compilation error, saying dev can't be NULL when
4437 * migr_state is assigned.
4438 */
4439 if (dev == NULL)
4440 return;
4441
4442 struct imsm_map *map = get_imsm_map(dev, MAP_0);
4443 struct imsm_map *prev = get_imsm_map(dev, dev->vol.migr_state == MIGR_STATE_NORMAL ?
4444 MAP_0 : MAP_1);
4445 int i, j;
4446
4447 /* merge any IMSM_ORD_REBUILD bits that were not successfully
4448 * completed in the last migration.
4449 *
4450 * FIXME add support for raid-level-migration
4451 */
4452 if (map_state != map->map_state && (is_gen_migration(dev) == false) &&
4453 prev->map_state != IMSM_T_STATE_UNINITIALIZED) {
4454 /* when final map state is other than expected
4455 * merge maps (not for migration)
4456 */
4457 int failed;
4458
4459 for (i = 0; i < prev->num_members; i++)
4460 for (j = 0; j < map->num_members; j++)
4461 /* during online capacity expansion
4462 * disks position can be changed
4463 * if takeover is used
4464 */
4465 if (ord_to_idx(map->disk_ord_tbl[j]) ==
4466 ord_to_idx(prev->disk_ord_tbl[i])) {
4467 map->disk_ord_tbl[j] |=
4468 prev->disk_ord_tbl[i];
4469 break;
4470 }
4471 failed = imsm_count_failed(super, dev, MAP_0);
4472 map_state = imsm_check_degraded(super, dev, failed, MAP_0);
4473 }
4474
4475 dev->vol.migr_state = MIGR_STATE_NORMAL;
4476 set_migr_type(dev, 0);
4477 set_vol_curr_migr_unit(dev, 0);
4478 map->map_state = map_state;
4479 }
4480
4481 static int parse_raid_devices(struct intel_super *super)
4482 {
4483 int i;
4484 struct imsm_dev *dev_new;
4485 size_t len, len_migr;
4486 size_t max_len = 0;
4487 size_t space_needed = 0;
4488 struct imsm_super *mpb = super->anchor;
4489
4490 for (i = 0; i < super->anchor->num_raid_devs; i++) {
4491 struct imsm_dev *dev_iter = __get_imsm_dev(super->anchor, i);
4492 struct intel_dev *dv;
4493
4494 len = sizeof_imsm_dev(dev_iter, 0);
4495 len_migr = sizeof_imsm_dev(dev_iter, 1);
4496 if (len_migr > len)
4497 space_needed += len_migr - len;
4498
4499 dv = xmalloc(sizeof(*dv));
4500 if (max_len < len_migr)
4501 max_len = len_migr;
4502 if (max_len > len_migr)
4503 space_needed += max_len - len_migr;
4504 dev_new = xmalloc(max_len);
4505 imsm_copy_dev(dev_new, dev_iter);
4506 dv->dev = dev_new;
4507 dv->index = i;
4508 dv->next = super->devlist;
4509 super->devlist = dv;
4510 }
4511
4512 /* ensure that super->buf is large enough when all raid devices
4513 * are migrating
4514 */
4515 if (__le32_to_cpu(mpb->mpb_size) + space_needed > super->len) {
4516 void *buf;
4517
4518 len = ROUND_UP(__le32_to_cpu(mpb->mpb_size) + space_needed,
4519 super->sector_size);
4520 if (posix_memalign(&buf, MAX_SECTOR_SIZE, len) != 0)
4521 return 1;
4522
4523 memcpy(buf, super->buf, super->len);
4524 memset(buf + super->len, 0, len - super->len);
4525 free(super->buf);
4526 super->buf = buf;
4527 super->len = len;
4528 }
4529
4530 super->extra_space += space_needed;
4531
4532 return 0;
4533 }
4534
4535 /*******************************************************************************
4536 * Function: check_mpb_migr_compatibility
4537 * Description: Function checks for unsupported migration features:
4538 * - migration optimization area (pba_of_lba0)
4539 * - descending reshape (ascending_migr)
4540 * Parameters:
4541 * super : imsm metadata information
4542 * Returns:
4543 * 0 : migration is compatible
4544 * -1 : migration is not compatible
4545 ******************************************************************************/
4546 int check_mpb_migr_compatibility(struct intel_super *super)
4547 {
4548 struct imsm_map *map0, *map1;
4549 struct migr_record *migr_rec = super->migr_rec;
4550 int i;
4551
4552 for (i = 0; i < super->anchor->num_raid_devs; i++) {
4553 struct imsm_dev *dev_iter = __get_imsm_dev(super->anchor, i);
4554
4555 if (dev_iter->vol.migr_state == MIGR_STATE_MIGRATING &&
4556 dev_iter->vol.migr_type == MIGR_GEN_MIGR) {
4557 /* This device is migrating */
4558 map0 = get_imsm_map(dev_iter, MAP_0);
4559 map1 = get_imsm_map(dev_iter, MAP_1);
4560 if (pba_of_lba0(map0) != pba_of_lba0(map1))
4561 /* migration optimization area was used */
4562 return -1;
4563 if (migr_rec->ascending_migr == 0 &&
4564 migr_rec->dest_depth_per_unit > 0)
4565 /* descending reshape not supported yet */
4566 return -1;
4567 }
4568 }
4569 return 0;
4570 }
4571
4572 static void __free_imsm(struct intel_super *super, int free_disks);
4573
4574 /* load_imsm_mpb - read matrix metadata
4575 * allocates super->mpb to be freed by free_imsm
4576 */
4577 static int load_imsm_mpb(int fd, struct intel_super *super, char *devname)
4578 {
4579 unsigned long long dsize;
4580 unsigned long long sectors;
4581 unsigned int sector_size = super->sector_size;
4582 struct stat;
4583 struct imsm_super *anchor;
4584 __u32 check_sum;
4585
4586 get_dev_size(fd, NULL, &dsize);
4587 if (dsize < 2*sector_size) {
4588 if (devname)
4589 pr_err("%s: device to small for imsm\n",
4590 devname);
4591 return 1;
4592 }
4593
4594 if (lseek64(fd, dsize - (sector_size * 2), SEEK_SET) < 0) {
4595 if (devname)
4596 pr_err("Cannot seek to anchor block on %s: %s\n",
4597 devname, strerror(errno));
4598 return 1;
4599 }
4600
4601 if (posix_memalign((void **)&anchor, sector_size, sector_size) != 0) {
4602 if (devname)
4603 pr_err("Failed to allocate imsm anchor buffer on %s\n", devname);
4604 return 1;
4605 }
4606 if ((unsigned int)read(fd, anchor, sector_size) != sector_size) {
4607 if (devname)
4608 pr_err("Cannot read anchor block on %s: %s\n",
4609 devname, strerror(errno));
4610 free(anchor);
4611 return 1;
4612 }
4613
4614 if (strncmp((char *) anchor->sig, MPB_SIGNATURE, MPB_SIG_LEN) != 0) {
4615 if (devname)
4616 pr_err("no IMSM anchor on %s\n", devname);
4617 free(anchor);
4618 return 2;
4619 }
4620
4621 __free_imsm(super, 0);
4622 /* reload capability and hba */
4623
4624 /* capability and hba must be updated with new super allocation */
4625 find_intel_hba_capability(fd, super, devname);
4626 super->len = ROUND_UP(anchor->mpb_size, sector_size);
4627 if (posix_memalign(&super->buf, MAX_SECTOR_SIZE, super->len) != 0) {
4628 if (devname)
4629 pr_err("unable to allocate %zu byte mpb buffer\n",
4630 super->len);
4631 free(anchor);
4632 return 2;
4633 }
4634 memcpy(super->buf, anchor, sector_size);
4635
4636 sectors = mpb_sectors(anchor, sector_size) - 1;
4637 free(anchor);
4638
4639 if (posix_memalign(&super->migr_rec_buf, MAX_SECTOR_SIZE,
4640 MIGR_REC_BUF_SECTORS*MAX_SECTOR_SIZE) != 0) {
4641 pr_err("could not allocate migr_rec buffer\n");
4642 free(super->buf);
4643 super->buf = NULL;
4644 return 2;
4645 }
4646 super->clean_migration_record_by_mdmon = 0;
4647
4648 if (!sectors) {
4649 check_sum = __gen_imsm_checksum(super->anchor);
4650 if (check_sum != __le32_to_cpu(super->anchor->check_sum)) {
4651 if (devname)
4652 pr_err("IMSM checksum %x != %x on %s\n",
4653 check_sum,
4654 __le32_to_cpu(super->anchor->check_sum),
4655 devname);
4656 return 2;
4657 }
4658
4659 return 0;
4660 }
4661
4662 /* read the extended mpb */
4663 if (lseek64(fd, dsize - (sector_size * (2 + sectors)), SEEK_SET) < 0) {
4664 if (devname)
4665 pr_err("Cannot seek to extended mpb on %s: %s\n",
4666 devname, strerror(errno));
4667 return 1;
4668 }
4669
4670 if ((unsigned int)read(fd, super->buf + sector_size,
4671 super->len - sector_size) != super->len - sector_size) {
4672 if (devname)
4673 pr_err("Cannot read extended mpb on %s: %s\n",
4674 devname, strerror(errno));
4675 return 2;
4676 }
4677
4678 check_sum = __gen_imsm_checksum(super->anchor);
4679 if (check_sum != __le32_to_cpu(super->anchor->check_sum)) {
4680 if (devname)
4681 pr_err("IMSM checksum %x != %x on %s\n",
4682 check_sum, __le32_to_cpu(super->anchor->check_sum),
4683 devname);
4684 return 3;
4685 }
4686
4687 return 0;
4688 }
4689
4690 static int read_imsm_migr_rec(int fd, struct intel_super *super);
4691
4692 /* clears hi bits in metadata if MPB_ATTRIB_2TB_DISK not set */
4693 static void clear_hi(struct intel_super *super)
4694 {
4695 struct imsm_super *mpb = super->anchor;
4696 int i, n;
4697 if (mpb->attributes & MPB_ATTRIB_2TB_DISK)
4698 return;
4699 for (i = 0; i < mpb->num_disks; ++i) {
4700 struct imsm_disk *disk = &mpb->disk[i];
4701 disk->total_blocks_hi = 0;
4702 }
4703 for (i = 0; i < mpb->num_raid_devs; ++i) {
4704 struct imsm_dev *dev = get_imsm_dev(super, i);
4705 for (n = 0; n < 2; ++n) {
4706 struct imsm_map *map = get_imsm_map(dev, n);
4707 if (!map)
4708 continue;
4709 map->pba_of_lba0_hi = 0;
4710 map->blocks_per_member_hi = 0;
4711 map->num_data_stripes_hi = 0;
4712 }
4713 }
4714 }
4715
4716 static int
4717 load_and_parse_mpb(int fd, struct intel_super *super, char *devname, int keep_fd)
4718 {
4719 int err;
4720
4721 err = load_imsm_mpb(fd, super, devname);
4722 if (err)
4723 return err;
4724 if (super->sector_size == 4096)
4725 convert_from_4k(super);
4726 err = load_imsm_disk(fd, super, devname, keep_fd);
4727 if (err)
4728 return err;
4729 err = parse_raid_devices(super);
4730 if (err)
4731 return err;
4732 err = load_bbm_log(super);
4733 clear_hi(super);
4734 return err;
4735 }
4736
4737 static void __free_imsm_disk(struct dl *d, int do_close)
4738 {
4739 if (do_close)
4740 close_fd(&d->fd);
4741 if (d->devname)
4742 free(d->devname);
4743 if (d->e)
4744 free(d->e);
4745 free(d);
4746
4747 }
4748
4749 static void free_imsm_disks(struct intel_super *super)
4750 {
4751 struct dl *d;
4752
4753 while (super->disks) {
4754 d = super->disks;
4755 super->disks = d->next;
4756 __free_imsm_disk(d, 1);
4757 }
4758 while (super->disk_mgmt_list) {
4759 d = super->disk_mgmt_list;
4760 super->disk_mgmt_list = d->next;
4761 __free_imsm_disk(d, 1);
4762 }
4763 while (super->missing) {
4764 d = super->missing;
4765 super->missing = d->next;
4766 __free_imsm_disk(d, 1);
4767 }
4768
4769 }
4770
4771 /* free all the pieces hanging off of a super pointer */
4772 static void __free_imsm(struct intel_super *super, int free_disks)
4773 {
4774 struct intel_hba *elem, *next;
4775
4776 if (super->buf) {
4777 free(super->buf);
4778 super->buf = NULL;
4779 }
4780 /* unlink capability description */
4781 super->orom = NULL;
4782 if (super->migr_rec_buf) {
4783 free(super->migr_rec_buf);
4784 super->migr_rec_buf = NULL;
4785 }
4786 if (free_disks)
4787 free_imsm_disks(super);
4788 free_devlist(super);
4789 elem = super->hba;
4790 while (elem) {
4791 if (elem->path)
4792 free((void *)elem->path);
4793 next = elem->next;
4794 free(elem);
4795 elem = next;
4796 }
4797 if (super->bbm_log)
4798 free(super->bbm_log);
4799 super->hba = NULL;
4800 }
4801
4802 static void free_imsm(struct intel_super *super)
4803 {
4804 __free_imsm(super, 1);
4805 free(super->bb.entries);
4806 free(super);
4807 }
4808
4809 static void free_super_imsm(struct supertype *st)
4810 {
4811 struct intel_super *super = st->sb;
4812
4813 if (!super)
4814 return;
4815
4816 free_imsm(super);
4817 st->sb = NULL;
4818 }
4819
4820 static struct intel_super *alloc_super(void)
4821 {
4822 struct intel_super *super = xcalloc(1, sizeof(*super));
4823
4824 super->current_vol = -1;
4825 super->create_offset = ~((unsigned long long) 0);
4826
4827 super->bb.entries = xmalloc(BBM_LOG_MAX_ENTRIES *
4828 sizeof(struct md_bb_entry));
4829 if (!super->bb.entries) {
4830 free(super);
4831 return NULL;
4832 }
4833
4834 return super;
4835 }
4836
4837 /*
4838 * find and allocate hba and OROM/EFI based on valid fd of RAID component device
4839 */
4840 static int find_intel_hba_capability(int fd, struct intel_super *super, char *devname)
4841 {
4842 struct sys_dev *hba_name;
4843 int rv = 0;
4844
4845 if (is_fd_valid(fd) && test_partition(fd)) {
4846 pr_err("imsm: %s is a partition, cannot be used in IMSM\n",
4847 devname);
4848 return 1;
4849 }
4850 if (!is_fd_valid(fd) || check_no_platform()) {
4851 super->orom = NULL;
4852 super->hba = NULL;
4853 return 0;
4854 }
4855 hba_name = find_disk_attached_hba(fd, NULL);
4856 if (!hba_name) {
4857 if (devname)
4858 pr_err("%s is not attached to Intel(R) RAID controller.\n",
4859 devname);
4860 return 1;
4861 }
4862 rv = attach_hba_to_super(super, hba_name);
4863 if (rv == 2) {
4864 if (devname) {
4865 struct intel_hba *hba = super->hba;
4866
4867 pr_err("%s is attached to Intel(R) %s %s (%s),\n"
4868 " but the container is assigned to Intel(R) %s %s (",
4869 devname,
4870 get_sys_dev_type(hba_name->type),
4871 hba_name->type == SYS_DEV_VMD || hba_name->type == SYS_DEV_SATA_VMD ?
4872 "domain" : "RAID controller",
4873 hba_name->pci_id ? : "Err!",
4874 get_sys_dev_type(super->hba->type),
4875 hba->type == SYS_DEV_VMD || hba_name->type == SYS_DEV_SATA_VMD ?
4876 "domain" : "RAID controller");
4877
4878 while (hba) {
4879 fprintf(stderr, "%s", hba->pci_id ? : "Err!");
4880 if (hba->next)
4881 fprintf(stderr, ", ");
4882 hba = hba->next;
4883 }
4884 fprintf(stderr, ").\n"
4885 " Mixing devices attached to different controllers is not allowed.\n");
4886 }
4887 return 2;
4888 }
4889 super->orom = find_imsm_capability(hba_name);
4890 if (!super->orom)
4891 return 3;
4892
4893 return 0;
4894 }
4895
4896 /* find_missing - helper routine for load_super_imsm_all that identifies
4897 * disks that have disappeared from the system. This routine relies on
4898 * the mpb being uptodate, which it is at load time.
4899 */
4900 static int find_missing(struct intel_super *super)
4901 {
4902 int i;
4903 struct imsm_super *mpb = super->anchor;
4904 struct dl *dl;
4905 struct imsm_disk *disk;
4906
4907 for (i = 0; i < mpb->num_disks; i++) {
4908 disk = __get_imsm_disk(mpb, i);
4909 dl = serial_to_dl(disk->serial, super);
4910 if (dl)
4911 continue;
4912
4913 dl = xmalloc(sizeof(*dl));
4914 dl->major = 0;
4915 dl->minor = 0;
4916 dl->fd = -1;
4917 dl->devname = xstrdup("missing");
4918 dl->index = i;
4919 serialcpy(dl->serial, disk->serial);
4920 dl->disk = *disk;
4921 dl->e = NULL;
4922 dl->next = super->missing;
4923 super->missing = dl;
4924 }
4925
4926 return 0;
4927 }
4928
4929 static struct intel_disk *disk_list_get(__u8 *serial, struct intel_disk *disk_list)
4930 {
4931 struct intel_disk *idisk = disk_list;
4932
4933 while (idisk) {
4934 if (serialcmp(idisk->disk.serial, serial) == 0)
4935 break;
4936 idisk = idisk->next;
4937 }
4938
4939 return idisk;
4940 }
4941
4942 static int __prep_thunderdome(struct intel_super **table, int tbl_size,
4943 struct intel_super *super,
4944 struct intel_disk **disk_list)
4945 {
4946 struct imsm_disk *d = &super->disks->disk;
4947 struct imsm_super *mpb = super->anchor;
4948 int i, j;
4949
4950 for (i = 0; i < tbl_size; i++) {
4951 struct imsm_super *tbl_mpb = table[i]->anchor;
4952 struct imsm_disk *tbl_d = &table[i]->disks->disk;
4953
4954 if (tbl_mpb->family_num == mpb->family_num) {
4955 if (tbl_mpb->check_sum == mpb->check_sum) {
4956 dprintf("mpb from %d:%d matches %d:%d\n",
4957 super->disks->major,
4958 super->disks->minor,
4959 table[i]->disks->major,
4960 table[i]->disks->minor);
4961 break;
4962 }
4963
4964 if (((is_configured(d) && !is_configured(tbl_d)) ||
4965 is_configured(d) == is_configured(tbl_d)) &&
4966 tbl_mpb->generation_num < mpb->generation_num) {
4967 /* current version of the mpb is a
4968 * better candidate than the one in
4969 * super_table, but copy over "cross
4970 * generational" status
4971 */
4972 struct intel_disk *idisk;
4973
4974 dprintf("mpb from %d:%d replaces %d:%d\n",
4975 super->disks->major,
4976 super->disks->minor,
4977 table[i]->disks->major,
4978 table[i]->disks->minor);
4979
4980 idisk = disk_list_get(tbl_d->serial, *disk_list);
4981 if (idisk && is_failed(&idisk->disk))
4982 tbl_d->status |= FAILED_DISK;
4983 break;
4984 } else {
4985 struct intel_disk *idisk;
4986 struct imsm_disk *disk;
4987
4988 /* tbl_mpb is more up to date, but copy
4989 * over cross generational status before
4990 * returning
4991 */
4992 disk = __serial_to_disk(d->serial, mpb, NULL);
4993 if (disk && is_failed(disk))
4994 d->status |= FAILED_DISK;
4995
4996 idisk = disk_list_get(d->serial, *disk_list);
4997 if (idisk) {
4998 idisk->owner = i;
4999 if (disk && is_configured(disk))
5000 idisk->disk.status |= CONFIGURED_DISK;
5001 }
5002
5003 dprintf("mpb from %d:%d prefer %d:%d\n",
5004 super->disks->major,
5005 super->disks->minor,
5006 table[i]->disks->major,
5007 table[i]->disks->minor);
5008
5009 return tbl_size;
5010 }
5011 }
5012 }
5013
5014 if (i >= tbl_size)
5015 table[tbl_size++] = super;
5016 else
5017 table[i] = super;
5018
5019 /* update/extend the merged list of imsm_disk records */
5020 for (j = 0; j < mpb->num_disks; j++) {
5021 struct imsm_disk *disk = __get_imsm_disk(mpb, j);
5022 struct intel_disk *idisk;
5023
5024 idisk = disk_list_get(disk->serial, *disk_list);
5025 if (idisk) {
5026 idisk->disk.status |= disk->status;
5027 if (is_configured(&idisk->disk) ||
5028 is_failed(&idisk->disk))
5029 idisk->disk.status &= ~(SPARE_DISK);
5030 } else {
5031 idisk = xcalloc(1, sizeof(*idisk));
5032 idisk->owner = IMSM_UNKNOWN_OWNER;
5033 idisk->disk = *disk;
5034 idisk->next = *disk_list;
5035 *disk_list = idisk;
5036 }
5037
5038 if (serialcmp(idisk->disk.serial, d->serial) == 0)
5039 idisk->owner = i;
5040 }
5041
5042 return tbl_size;
5043 }
5044
5045 static struct intel_super *
5046 validate_members(struct intel_super *super, struct intel_disk *disk_list,
5047 const int owner)
5048 {
5049 struct imsm_super *mpb = super->anchor;
5050 int ok_count = 0;
5051 int i;
5052
5053 for (i = 0; i < mpb->num_disks; i++) {
5054 struct imsm_disk *disk = __get_imsm_disk(mpb, i);
5055 struct intel_disk *idisk;
5056
5057 idisk = disk_list_get(disk->serial, disk_list);
5058 if (idisk) {
5059 if (idisk->owner == owner ||
5060 idisk->owner == IMSM_UNKNOWN_OWNER)
5061 ok_count++;
5062 else
5063 dprintf("'%.16s' owner %d != %d\n",
5064 disk->serial, idisk->owner,
5065 owner);
5066 } else {
5067 dprintf("unknown disk %x [%d]: %.16s\n",
5068 __le32_to_cpu(mpb->family_num), i,
5069 disk->serial);
5070 break;
5071 }
5072 }
5073
5074 if (ok_count == mpb->num_disks)
5075 return super;
5076 return NULL;
5077 }
5078
5079 static void show_conflicts(__u32 family_num, struct intel_super *super_list)
5080 {
5081 struct intel_super *s;
5082
5083 for (s = super_list; s; s = s->next) {
5084 if (family_num != s->anchor->family_num)
5085 continue;
5086 pr_err("Conflict, offlining family %#x on '%s'\n",
5087 __le32_to_cpu(family_num), s->disks->devname);
5088 }
5089 }
5090
5091 static struct intel_super *
5092 imsm_thunderdome(struct intel_super **super_list, int len)
5093 {
5094 struct intel_super *super_table[len];
5095 struct intel_disk *disk_list = NULL;
5096 struct intel_super *champion, *spare;
5097 struct intel_super *s, **del;
5098 int tbl_size = 0;
5099 int conflict;
5100 int i;
5101
5102 memset(super_table, 0, sizeof(super_table));
5103 for (s = *super_list; s; s = s->next)
5104 tbl_size = __prep_thunderdome(super_table, tbl_size, s, &disk_list);
5105
5106 for (i = 0; i < tbl_size; i++) {
5107 struct imsm_disk *d;
5108 struct intel_disk *idisk;
5109
5110 s = super_table[i];
5111 d = &s->disks->disk;
5112
5113 /* 'd' must appear in merged disk list for its
5114 * configuration to be valid
5115 */
5116 idisk = disk_list_get(d->serial, disk_list);
5117 if (idisk && idisk->owner == i)
5118 s = validate_members(s, disk_list, i);
5119 else
5120 s = NULL;
5121
5122 if (!s)
5123 dprintf("marking family: %#x from %d:%d offline\n",
5124 super_table[i]->anchor->family_num,
5125 super_table[i]->disks->major,
5126 super_table[i]->disks->minor);
5127 super_table[i] = s;
5128 }
5129
5130 /* This is where the mdadm implementation differs from the Windows
5131 * driver which has no strict concept of a container. We can only
5132 * assemble one family from a container, so when returning a prodigal
5133 * array member to this system the code will not be able to disambiguate
5134 * the container contents that should be assembled ("foreign" versus
5135 * "local"). It requires user intervention to set the orig_family_num
5136 * to a new value to establish a new container. The Windows driver in
5137 * this situation fixes up the volume name in place and manages the
5138 * foreign array as an independent entity.
5139 */
5140 s = NULL;
5141 spare = NULL;
5142 conflict = 0;
5143 for (i = 0; i < tbl_size; i++) {
5144 struct intel_super *tbl_ent = super_table[i];
5145 int is_spare = 0;
5146
5147 if (!tbl_ent)
5148 continue;
5149
5150 if (tbl_ent->anchor->num_raid_devs == 0) {
5151 spare = tbl_ent;
5152 is_spare = 1;
5153 }
5154
5155 if (s && !is_spare) {
5156 show_conflicts(tbl_ent->anchor->family_num, *super_list);
5157 conflict++;
5158 } else if (!s && !is_spare)
5159 s = tbl_ent;
5160 }
5161
5162 if (!s)
5163 s = spare;
5164 if (!s) {
5165 champion = NULL;
5166 goto out;
5167 }
5168 champion = s;
5169
5170 if (conflict)
5171 pr_err("Chose family %#x on '%s', assemble conflicts to new container with '--update=uuid'\n",
5172 __le32_to_cpu(s->anchor->family_num), s->disks->devname);
5173
5174 /* collect all dl's onto 'champion', and update them to
5175 * champion's version of the status
5176 */
5177 for (s = *super_list; s; s = s->next) {
5178 struct imsm_super *mpb = champion->anchor;
5179 struct dl *dl = s->disks;
5180
5181 if (s == champion)
5182 continue;
5183
5184 mpb->attributes |= s->anchor->attributes & MPB_ATTRIB_2TB_DISK;
5185
5186 for (i = 0; i < mpb->num_disks; i++) {
5187 struct imsm_disk *disk;
5188
5189 disk = __serial_to_disk(dl->serial, mpb, &dl->index);
5190 if (disk) {
5191 dl->disk = *disk;
5192 /* only set index on disks that are a member of
5193 * a populated contianer, i.e. one with
5194 * raid_devs
5195 */
5196 if (is_failed(&dl->disk))
5197 dl->index = -2;
5198 else if (is_spare(&dl->disk))
5199 dl->index = -1;
5200 break;
5201 }
5202 }
5203
5204 if (i >= mpb->num_disks) {
5205 struct intel_disk *idisk;
5206
5207 idisk = disk_list_get(dl->serial, disk_list);
5208 if (idisk && is_spare(&idisk->disk) &&
5209 !is_failed(&idisk->disk) && !is_configured(&idisk->disk))
5210 dl->index = -1;
5211 else {
5212 dl->index = -2;
5213 continue;
5214 }
5215 }
5216
5217 dl->next = champion->disks;
5218 champion->disks = dl;
5219 s->disks = NULL;
5220 }
5221
5222 /* delete 'champion' from super_list */
5223 for (del = super_list; *del; ) {
5224 if (*del == champion) {
5225 *del = (*del)->next;
5226 break;
5227 } else
5228 del = &(*del)->next;
5229 }
5230 champion->next = NULL;
5231
5232 out:
5233 while (disk_list) {
5234 struct intel_disk *idisk = disk_list;
5235
5236 disk_list = disk_list->next;
5237 free(idisk);
5238 }
5239
5240 return champion;
5241 }
5242
5243 static int
5244 get_sra_super_block(int fd, struct intel_super **super_list, char *devname, int *max, int keep_fd);
5245 static int get_super_block(struct intel_super **super_list, char *devnm, char *devname,
5246 int major, int minor, int keep_fd);
5247 static int
5248 get_devlist_super_block(struct md_list *devlist, struct intel_super **super_list,
5249 int *max, int keep_fd);
5250
5251 static int load_super_imsm_all(struct supertype *st, int fd, void **sbp,
5252 char *devname, struct md_list *devlist,
5253 int keep_fd)
5254 {
5255 struct intel_super *super_list = NULL;
5256 struct intel_super *super = NULL;
5257 int err = 0;
5258 int i = 0;
5259
5260 if (is_fd_valid(fd))
5261 /* 'fd' is an opened container */
5262 err = get_sra_super_block(fd, &super_list, devname, &i, keep_fd);
5263 else
5264 /* get super block from devlist devices */
5265 err = get_devlist_super_block(devlist, &super_list, &i, keep_fd);
5266 if (err)
5267 goto error;
5268 /* all mpbs enter, maybe one leaves */
5269 super = imsm_thunderdome(&super_list, i);
5270 if (!super) {
5271 err = 1;
5272 goto error;
5273 }
5274
5275 if (find_missing(super) != 0) {
5276 free_imsm(super);
5277 err = 2;
5278 goto error;
5279 }
5280
5281 /* load migration record */
5282 err = load_imsm_migr_rec(super);
5283 if (err == -1) {
5284 /* migration is in progress,
5285 * but migr_rec cannot be loaded,
5286 */
5287 err = 4;
5288 goto error;
5289 }
5290
5291 /* Check migration compatibility */
5292 if (err == 0 && check_mpb_migr_compatibility(super) != 0) {
5293 pr_err("Unsupported migration detected");
5294 if (devname)
5295 fprintf(stderr, " on %s\n", devname);
5296 else
5297 fprintf(stderr, " (IMSM).\n");
5298
5299 err = 5;
5300 goto error;
5301 }
5302
5303 err = 0;
5304
5305 error:
5306 while (super_list) {
5307 struct intel_super *s = super_list;
5308
5309 super_list = super_list->next;
5310 free_imsm(s);
5311 }
5312
5313 if (err)
5314 return err;
5315
5316 *sbp = super;
5317 if (is_fd_valid(fd))
5318 strcpy(st->container_devnm, fd2devnm(fd));
5319 else
5320 st->container_devnm[0] = 0;
5321 if (err == 0 && st->ss == NULL) {
5322 st->ss = &super_imsm;
5323 st->minor_version = 0;
5324 st->max_devs = IMSM_MAX_DEVICES;
5325 }
5326 return 0;
5327 }
5328
5329 static int
5330 get_devlist_super_block(struct md_list *devlist, struct intel_super **super_list,
5331 int *max, int keep_fd)
5332 {
5333 struct md_list *tmpdev;
5334 int err = 0;
5335 int i = 0;
5336
5337 for (i = 0, tmpdev = devlist; tmpdev; tmpdev = tmpdev->next) {
5338 if (tmpdev->used != 1)
5339 continue;
5340 if (tmpdev->container == 1) {
5341 int lmax = 0;
5342 int fd = dev_open(tmpdev->devname, O_RDONLY|O_EXCL);
5343 if (!is_fd_valid(fd)) {
5344 pr_err("cannot open device %s: %s\n",
5345 tmpdev->devname, strerror(errno));
5346 err = 8;
5347 goto error;
5348 }
5349 err = get_sra_super_block(fd, super_list,
5350 tmpdev->devname, &lmax,
5351 keep_fd);
5352 i += lmax;
5353 close(fd);
5354 if (err) {
5355 err = 7;
5356 goto error;
5357 }
5358 } else {
5359 int major = major(tmpdev->st_rdev);
5360 int minor = minor(tmpdev->st_rdev);
5361 err = get_super_block(super_list,
5362 NULL,
5363 tmpdev->devname,
5364 major, minor,
5365 keep_fd);
5366 i++;
5367 if (err) {
5368 err = 6;
5369 goto error;
5370 }
5371 }
5372 }
5373 error:
5374 *max = i;
5375 return err;
5376 }
5377
5378 static int get_super_block(struct intel_super **super_list, char *devnm, char *devname,
5379 int major, int minor, int keep_fd)
5380 {
5381 struct intel_super *s;
5382 char nm[32];
5383 int dfd = -1;
5384 int err = 0;
5385 int retry;
5386
5387 s = alloc_super();
5388 if (!s) {
5389 err = 1;
5390 goto error;
5391 }
5392
5393 sprintf(nm, "%d:%d", major, minor);
5394 dfd = dev_open(nm, O_RDWR);
5395 if (!is_fd_valid(dfd)) {
5396 err = 2;
5397 goto error;
5398 }
5399
5400 if (!get_dev_sector_size(dfd, NULL, &s->sector_size)) {
5401 err = 2;
5402 goto error;
5403 }
5404 find_intel_hba_capability(dfd, s, devname);
5405 err = load_and_parse_mpb(dfd, s, NULL, keep_fd);
5406
5407 /* retry the load if we might have raced against mdmon */
5408 if (err == 3 && devnm && mdmon_running(devnm))
5409 for (retry = 0; retry < 3; retry++) {
5410 sleep_for(0, MSEC_TO_NSEC(3), true);
5411 err = load_and_parse_mpb(dfd, s, NULL, keep_fd);
5412 if (err != 3)
5413 break;
5414 }
5415 error:
5416 if (!err) {
5417 s->next = *super_list;
5418 *super_list = s;
5419 } else {
5420 if (s)
5421 free_imsm(s);
5422 close_fd(&dfd);
5423 }
5424 if (!keep_fd)
5425 close_fd(&dfd);
5426 return err;
5427
5428 }
5429
5430 static int
5431 get_sra_super_block(int fd, struct intel_super **super_list, char *devname, int *max, int keep_fd)
5432 {
5433 struct mdinfo *sra;
5434 char *devnm;
5435 struct mdinfo *sd;
5436 int err = 0;
5437 int i = 0;
5438 sra = sysfs_read(fd, NULL, GET_LEVEL|GET_VERSION|GET_DEVS|GET_STATE);
5439 if (!sra)
5440 return 1;
5441
5442 if (sra->array.major_version != -1 ||
5443 sra->array.minor_version != -2 ||
5444 strcmp(sra->text_version, "imsm") != 0) {
5445 err = 1;
5446 goto error;
5447 }
5448 /* load all mpbs */
5449 devnm = fd2devnm(fd);
5450 for (sd = sra->devs, i = 0; sd; sd = sd->next, i++) {
5451 if (get_super_block(super_list, devnm, devname,
5452 sd->disk.major, sd->disk.minor, keep_fd) != 0) {
5453 err = 7;
5454 goto error;
5455 }
5456 }
5457 error:
5458 sysfs_free(sra);
5459 *max = i;
5460 return err;
5461 }
5462
5463 static int load_container_imsm(struct supertype *st, int fd, char *devname)
5464 {
5465 return load_super_imsm_all(st, fd, &st->sb, devname, NULL, 1);
5466 }
5467
5468 static int load_super_imsm(struct supertype *st, int fd, char *devname)
5469 {
5470 struct intel_super *super;
5471 int rv;
5472 int retry;
5473
5474 if (test_partition(fd))
5475 /* IMSM not allowed on partitions */
5476 return 1;
5477
5478 free_super_imsm(st);
5479
5480 super = alloc_super();
5481 if (!super)
5482 return 1;
5483
5484 if (!get_dev_sector_size(fd, NULL, &super->sector_size)) {
5485 free_imsm(super);
5486 return 1;
5487 }
5488 /* Load hba and capabilities if they exist.
5489 * But do not preclude loading metadata in case capabilities or hba are
5490 * non-compliant and ignore_hw_compat is set.
5491 */
5492 rv = find_intel_hba_capability(fd, super, devname);
5493 /* no orom/efi or non-intel hba of the disk */
5494 if (rv != 0 && st->ignore_hw_compat == 0) {
5495 if (devname)
5496 pr_err("No OROM/EFI properties for %s\n", devname);
5497 free_imsm(super);
5498 return 2;
5499 }
5500 rv = load_and_parse_mpb(fd, super, devname, 0);
5501
5502 /* retry the load if we might have raced against mdmon */
5503 if (rv == 3) {
5504 struct mdstat_ent *mdstat = NULL;
5505 char *name = fd2kname(fd);
5506
5507 if (name)
5508 mdstat = mdstat_by_component(name);
5509
5510 if (mdstat && mdmon_running(mdstat->devnm) && getpid() != mdmon_pid(mdstat->devnm)) {
5511 for (retry = 0; retry < 3; retry++) {
5512 sleep_for(0, MSEC_TO_NSEC(3), true);
5513 rv = load_and_parse_mpb(fd, super, devname, 0);
5514 if (rv != 3)
5515 break;
5516 }
5517 }
5518
5519 free_mdstat(mdstat);
5520 }
5521
5522 if (rv) {
5523 if (devname)
5524 pr_err("Failed to load all information sections on %s\n", devname);
5525 free_imsm(super);
5526 return rv;
5527 }
5528
5529 st->sb = super;
5530 if (st->ss == NULL) {
5531 st->ss = &super_imsm;
5532 st->minor_version = 0;
5533 st->max_devs = IMSM_MAX_DEVICES;
5534 }
5535
5536 /* load migration record */
5537 if (load_imsm_migr_rec(super) == 0) {
5538 /* Check for unsupported migration features */
5539 if (check_mpb_migr_compatibility(super) != 0) {
5540 pr_err("Unsupported migration detected");
5541 if (devname)
5542 fprintf(stderr, " on %s\n", devname);
5543 else
5544 fprintf(stderr, " (IMSM).\n");
5545 return 3;
5546 }
5547 }
5548
5549 return 0;
5550 }
5551
5552 static __u16 info_to_blocks_per_strip(mdu_array_info_t *info)
5553 {
5554 if (info->level == 1)
5555 return 128;
5556 return info->chunk_size >> 9;
5557 }
5558
5559 static unsigned long long info_to_blocks_per_member(mdu_array_info_t *info,
5560 unsigned long long size)
5561 {
5562 if (info->level == 1)
5563 return size * 2;
5564 else
5565 return (size * 2) & ~(info_to_blocks_per_strip(info) - 1);
5566 }
5567
5568 static void imsm_write_signature(struct imsm_super *mpb)
5569 {
5570 /* It is safer to eventually truncate version rather than left it not NULL ended */
5571 snprintf((char *) mpb->sig, MAX_SIGNATURE_LENGTH, MPB_SIGNATURE MPB_VERSION_ATTRIBS);
5572 }
5573
5574 static void imsm_update_version_info(struct intel_super *super)
5575 {
5576 /* update the version and attributes */
5577 struct imsm_super *mpb = super->anchor;
5578 struct imsm_dev *dev;
5579 struct imsm_map *map;
5580 int i;
5581
5582 mpb->attributes |= MPB_ATTRIB_CHECKSUM_VERIFY;
5583
5584 for (i = 0; i < mpb->num_raid_devs; i++) {
5585 dev = get_imsm_dev(super, i);
5586 map = get_imsm_map(dev, MAP_0);
5587
5588 if (__le32_to_cpu(dev->size_high) > 0)
5589 mpb->attributes |= MPB_ATTRIB_2TB;
5590
5591 switch (get_imsm_raid_level(map)) {
5592 case IMSM_T_RAID0:
5593 mpb->attributes |= MPB_ATTRIB_RAID0;
5594 break;
5595 case IMSM_T_RAID1:
5596 mpb->attributes |= MPB_ATTRIB_RAID1;
5597 break;
5598 case IMSM_T_RAID5:
5599 mpb->attributes |= MPB_ATTRIB_RAID5;
5600 break;
5601 case IMSM_T_RAID10:
5602 mpb->attributes |= MPB_ATTRIB_RAID10;
5603 if (map->num_members > 4)
5604 mpb->attributes |= MPB_ATTRIB_RAID10_EXT;
5605 break;
5606 }
5607 }
5608
5609 imsm_write_signature(mpb);
5610 }
5611
5612 /**
5613 * imsm_check_name() - check imsm naming criteria.
5614 * @super: &intel_super pointer, not NULL.
5615 * @name: name to check.
5616 * @verbose: verbose level.
5617 *
5618 * Name must be no longer than &MAX_RAID_SERIAL_LEN and must be unique across volumes.
5619 *
5620 * Returns: &true if @name matches, &false otherwise.
5621 */
5622 static bool imsm_is_name_allowed(struct intel_super *super, const char * const name,
5623 const int verbose)
5624 {
5625 struct imsm_super *mpb = super->anchor;
5626 int i;
5627
5628 if (is_string_lq(name, MAX_RAID_SERIAL_LEN + 1) == false) {
5629 pr_vrb("imsm: Name \"%s\" is too long\n", name);
5630 return false;
5631 }
5632
5633 if (name[0] == '.') {
5634 pr_vrb("imsm: Name \"%s\" has forbidden leading dot", name);
5635 return false;
5636 }
5637
5638 if (is_name_posix_compatible(name) == false) {
5639 pr_vrb("imsm: Name \"%s\" doesn't follow POSIX portable file name character set",
5640 name);
5641 return false;
5642 }
5643
5644 for (i = 0; i < mpb->num_raid_devs; i++) {
5645 struct imsm_dev *dev = get_imsm_dev(super, i);
5646
5647 if (strncmp((char *) dev->volume, name, MAX_RAID_SERIAL_LEN) == 0) {
5648 pr_vrb("imsm: Name \"%s\" already exists\n", name);
5649 return false;
5650 }
5651 }
5652
5653 return true;
5654 }
5655
5656 static int init_super_imsm_volume(struct supertype *st, mdu_array_info_t *info,
5657 struct shape *s, char *name,
5658 char *homehost, int *uuid,
5659 long long data_offset)
5660 {
5661 /* We are creating a volume inside a pre-existing container.
5662 * so st->sb is already set.
5663 */
5664 struct intel_super *super = st->sb;
5665 unsigned int sector_size = super->sector_size;
5666 struct imsm_super *mpb = super->anchor;
5667 struct intel_dev *dv;
5668 struct imsm_dev *dev;
5669 struct imsm_vol *vol;
5670 struct imsm_map *map;
5671 int idx = mpb->num_raid_devs;
5672 int i;
5673 int namelen;
5674 unsigned long long array_blocks;
5675 size_t size_old, size_new;
5676 unsigned int data_disks;
5677 unsigned long long size_per_member;
5678
5679 if (super->orom && mpb->num_raid_devs >= super->orom->vpa) {
5680 pr_err("This imsm-container already has the maximum of %d volumes\n", super->orom->vpa);
5681 return 0;
5682 }
5683
5684 /* ensure the mpb is large enough for the new data */
5685 size_old = __le32_to_cpu(mpb->mpb_size);
5686 size_new = disks_to_mpb_size(info->nr_disks);
5687 if (size_new > size_old) {
5688 void *mpb_new;
5689 size_t size_round = ROUND_UP(size_new, sector_size);
5690
5691 if (posix_memalign(&mpb_new, sector_size, size_round) != 0) {
5692 pr_err("could not allocate new mpb\n");
5693 return 0;
5694 }
5695 if (posix_memalign(&super->migr_rec_buf, MAX_SECTOR_SIZE,
5696 MIGR_REC_BUF_SECTORS*
5697 MAX_SECTOR_SIZE) != 0) {
5698 pr_err("could not allocate migr_rec buffer\n");
5699 free(super->buf);
5700 free(super);
5701 free(mpb_new);
5702 return 0;
5703 }
5704 memcpy(mpb_new, mpb, size_old);
5705 free(mpb);
5706 mpb = mpb_new;
5707 super->anchor = mpb_new;
5708 mpb->mpb_size = __cpu_to_le32(size_new);
5709 memset(mpb_new + size_old, 0, size_round - size_old);
5710 super->len = size_round;
5711 }
5712 super->current_vol = idx;
5713
5714 /* handle 'failed_disks' by either:
5715 * a) create dummy disk entries in the table if this the first
5716 * volume in the array. We add them here as this is the only
5717 * opportunity to add them. add_to_super_imsm_volume()
5718 * handles the non-failed disks and continues incrementing
5719 * mpb->num_disks.
5720 * b) validate that 'failed_disks' matches the current number
5721 * of missing disks if the container is populated
5722 */
5723 if (super->current_vol == 0) {
5724 mpb->num_disks = 0;
5725 for (i = 0; i < info->failed_disks; i++) {
5726 struct imsm_disk *disk;
5727
5728 mpb->num_disks++;
5729 disk = __get_imsm_disk(mpb, i);
5730 disk->status = CONFIGURED_DISK | FAILED_DISK;
5731 disk->scsi_id = __cpu_to_le32(~(__u32)0);
5732 snprintf((char *) disk->serial, MAX_RAID_SERIAL_LEN,
5733 "missing:%d", (__u8)i);
5734 }
5735 find_missing(super);
5736 } else {
5737 int missing = 0;
5738 struct dl *d;
5739
5740 for (d = super->missing; d; d = d->next)
5741 missing++;
5742 if (info->failed_disks > missing) {
5743 pr_err("unable to add 'missing' disk to container\n");
5744 return 0;
5745 }
5746 }
5747
5748 if (imsm_is_name_allowed(super, name, 1) == false)
5749 return 0;
5750
5751 dv = xmalloc(sizeof(*dv));
5752 dev = xcalloc(1, sizeof(*dev) + sizeof(__u32) * (info->raid_disks - 1));
5753 /*
5754 * Explicitly allow truncating to not confuse gcc's
5755 * -Werror=stringop-truncation
5756 */
5757 namelen = min((int) strlen(name), MAX_RAID_SERIAL_LEN);
5758 memcpy(dev->volume, name, namelen);
5759 array_blocks = calc_array_size(info->level, info->raid_disks,
5760 info->layout, info->chunk_size,
5761 s->size * BLOCKS_PER_KB);
5762 data_disks = get_data_disks(info->level, info->layout,
5763 info->raid_disks);
5764 array_blocks = round_size_to_mb(array_blocks, data_disks);
5765 size_per_member = array_blocks / data_disks;
5766
5767 set_imsm_dev_size(dev, array_blocks);
5768 dev->status = (DEV_READ_COALESCING | DEV_WRITE_COALESCING);
5769 vol = &dev->vol;
5770 vol->migr_state = MIGR_STATE_NORMAL;
5771 set_migr_type(dev, MIGR_INIT);
5772 vol->dirty = !info->state;
5773 set_vol_curr_migr_unit(dev, 0);
5774 map = get_imsm_map(dev, MAP_0);
5775 set_pba_of_lba0(map, super->create_offset);
5776 map->blocks_per_strip = __cpu_to_le16(info_to_blocks_per_strip(info));
5777 map->failed_disk_num = ~0;
5778 if (info->level > IMSM_T_RAID0)
5779 map->map_state = (info->state ? IMSM_T_STATE_NORMAL
5780 : IMSM_T_STATE_UNINITIALIZED);
5781 else
5782 map->map_state = info->failed_disks ? IMSM_T_STATE_FAILED :
5783 IMSM_T_STATE_NORMAL;
5784 map->ddf = 1;
5785
5786 if (info->level == IMSM_T_RAID1 && info->raid_disks > 2) {
5787 free(dev);
5788 free(dv);
5789 pr_err("imsm does not support more than 2 disks in a raid1 volume\n");
5790 return 0;
5791 }
5792 map->num_members = info->raid_disks;
5793
5794 update_imsm_raid_level(map, info->level);
5795 set_num_domains(map);
5796
5797 size_per_member += NUM_BLOCKS_DIRTY_STRIPE_REGION;
5798 set_blocks_per_member(map, info_to_blocks_per_member(info,
5799 size_per_member /
5800 BLOCKS_PER_KB));
5801
5802 update_num_data_stripes(map, array_blocks);
5803 for (i = 0; i < map->num_members; i++) {
5804 /* initialized in add_to_super */
5805 set_imsm_ord_tbl_ent(map, i, IMSM_ORD_REBUILD);
5806 }
5807 mpb->num_raid_devs++;
5808 mpb->num_raid_devs_created++;
5809 dev->my_vol_raid_dev_num = mpb->num_raid_devs_created;
5810
5811 if (s->consistency_policy <= CONSISTENCY_POLICY_RESYNC) {
5812 dev->rwh_policy = RWH_MULTIPLE_OFF;
5813 } else if (s->consistency_policy == CONSISTENCY_POLICY_PPL) {
5814 dev->rwh_policy = RWH_MULTIPLE_DISTRIBUTED;
5815 } else {
5816 free(dev);
5817 free(dv);
5818 pr_err("imsm does not support consistency policy %s\n",
5819 map_num_s(consistency_policies, s->consistency_policy));
5820 return 0;
5821 }
5822
5823 dv->dev = dev;
5824 dv->index = super->current_vol;
5825 dv->next = super->devlist;
5826 super->devlist = dv;
5827
5828 imsm_update_version_info(super);
5829
5830 return 1;
5831 }
5832
5833 static int init_super_imsm(struct supertype *st, mdu_array_info_t *info,
5834 struct shape *s, char *name,
5835 char *homehost, int *uuid,
5836 unsigned long long data_offset)
5837 {
5838 /* This is primarily called by Create when creating a new array.
5839 * We will then get add_to_super called for each component, and then
5840 * write_init_super called to write it out to each device.
5841 * For IMSM, Create can create on fresh devices or on a pre-existing
5842 * array.
5843 * To create on a pre-existing array a different method will be called.
5844 * This one is just for fresh drives.
5845 */
5846 struct intel_super *super;
5847 struct imsm_super *mpb;
5848 size_t mpb_size;
5849
5850 if (data_offset != INVALID_SECTORS) {
5851 pr_err("data-offset not supported by imsm\n");
5852 return 0;
5853 }
5854
5855 if (st->sb)
5856 return init_super_imsm_volume(st, info, s, name, homehost, uuid,
5857 data_offset);
5858
5859 if (info)
5860 mpb_size = disks_to_mpb_size(info->nr_disks);
5861 else
5862 mpb_size = MAX_SECTOR_SIZE;
5863
5864 super = alloc_super();
5865 if (super &&
5866 posix_memalign(&super->buf, MAX_SECTOR_SIZE, mpb_size) != 0) {
5867 free_imsm(super);
5868 super = NULL;
5869 }
5870 if (!super) {
5871 pr_err("could not allocate superblock\n");
5872 return 0;
5873 }
5874 if (posix_memalign(&super->migr_rec_buf, MAX_SECTOR_SIZE,
5875 MIGR_REC_BUF_SECTORS*MAX_SECTOR_SIZE) != 0) {
5876 pr_err("could not allocate migr_rec buffer\n");
5877 free(super->buf);
5878 free_imsm(super);
5879 return 0;
5880 }
5881 memset(super->buf, 0, mpb_size);
5882 mpb = super->buf;
5883 mpb->mpb_size = __cpu_to_le32(mpb_size);
5884 st->sb = super;
5885
5886 if (info == NULL) {
5887 /* zeroing superblock */
5888 return 0;
5889 }
5890
5891 imsm_update_version_info(super);
5892 return 1;
5893 }
5894
5895 static int drive_validate_sector_size(struct intel_super *super, struct dl *dl)
5896 {
5897 unsigned int member_sector_size;
5898
5899 if (!is_fd_valid(dl->fd)) {
5900 pr_err("Invalid file descriptor for %s\n", dl->devname);
5901 return 0;
5902 }
5903
5904 if (!get_dev_sector_size(dl->fd, dl->devname, &member_sector_size))
5905 return 0;
5906 if (member_sector_size != super->sector_size)
5907 return 0;
5908 return 1;
5909 }
5910
5911 static int add_to_super_imsm_volume(struct supertype *st, mdu_disk_info_t *dk,
5912 int fd, char *devname)
5913 {
5914 struct intel_super *super = st->sb;
5915 struct imsm_super *mpb = super->anchor;
5916 struct imsm_disk *_disk;
5917 struct imsm_dev *dev;
5918 struct imsm_map *map;
5919 struct dl *dl, *df;
5920 int slot;
5921 int autolayout = 0;
5922
5923 if (!is_fd_valid(fd))
5924 autolayout = 1;
5925
5926 dev = get_imsm_dev(super, super->current_vol);
5927 map = get_imsm_map(dev, MAP_0);
5928
5929 if (! (dk->state & (1<<MD_DISK_SYNC))) {
5930 pr_err("%s: Cannot add spare devices to IMSM volume\n",
5931 devname);
5932 return 1;
5933 }
5934
5935 for (dl = super->disks; dl ; dl = dl->next) {
5936 if (autolayout) {
5937 if (dl->raiddisk == dk->raid_disk)
5938 break;
5939 } else if (dl->major == dk->major && dl->minor == dk->minor)
5940 break;
5941 }
5942
5943 if (!dl) {
5944 if (!autolayout)
5945 pr_err("%s is not a member of the same container.\n",
5946 devname);
5947 return 1;
5948 }
5949
5950 if (!autolayout && super->current_vol > 0) {
5951 int _slot = get_disk_slot_in_dev(super, 0, dl->index);
5952
5953 if (_slot != dk->raid_disk) {
5954 pr_err("Member %s is in %d slot for the first volume, but is in %d slot for a new volume.\n",
5955 dl->devname, _slot, dk->raid_disk);
5956 pr_err("Raid members are in different order than for the first volume, aborting.\n");
5957 return 1;
5958 }
5959 }
5960
5961 if (mpb->num_disks == 0)
5962 if (!get_dev_sector_size(dl->fd, dl->devname,
5963 &super->sector_size))
5964 return 1;
5965
5966 if (!drive_validate_sector_size(super, dl)) {
5967 pr_err("Combining drives of different sector size in one volume is not allowed\n");
5968 return 1;
5969 }
5970
5971 /* add a pristine spare to the metadata */
5972 if (dl->index < 0) {
5973 dl->index = super->anchor->num_disks;
5974 super->anchor->num_disks++;
5975 }
5976 /* Check the device has not already been added */
5977 slot = get_imsm_disk_slot(map, dl->index);
5978 if (slot >= 0 &&
5979 (get_imsm_ord_tbl_ent(dev, slot, MAP_X) & IMSM_ORD_REBUILD) == 0) {
5980 pr_err("%s has been included in this array twice\n",
5981 devname);
5982 return 1;
5983 }
5984 set_imsm_ord_tbl_ent(map, dk->raid_disk, dl->index);
5985 dl->disk.status = CONFIGURED_DISK;
5986
5987 /* update size of 'missing' disks to be at least as large as the
5988 * largest acitve member (we only have dummy missing disks when
5989 * creating the first volume)
5990 */
5991 if (super->current_vol == 0) {
5992 for (df = super->missing; df; df = df->next) {
5993 if (total_blocks(&dl->disk) > total_blocks(&df->disk))
5994 set_total_blocks(&df->disk, total_blocks(&dl->disk));
5995 _disk = __get_imsm_disk(mpb, df->index);
5996 *_disk = df->disk;
5997 }
5998 }
5999
6000 /* refresh unset/failed slots to point to valid 'missing' entries */
6001 for (df = super->missing; df; df = df->next)
6002 for (slot = 0; slot < mpb->num_disks; slot++) {
6003 __u32 ord = get_imsm_ord_tbl_ent(dev, slot, MAP_X);
6004
6005 if ((ord & IMSM_ORD_REBUILD) == 0)
6006 continue;
6007 set_imsm_ord_tbl_ent(map, slot, df->index | IMSM_ORD_REBUILD);
6008 if (is_gen_migration(dev)) {
6009 struct imsm_map *map2 = get_imsm_map(dev,
6010 MAP_1);
6011 int slot2 = get_imsm_disk_slot(map2, df->index);
6012 if (slot2 < map2->num_members && slot2 >= 0) {
6013 __u32 ord2 = get_imsm_ord_tbl_ent(dev,
6014 slot2,
6015 MAP_1);
6016 if ((unsigned)df->index ==
6017 ord_to_idx(ord2))
6018 set_imsm_ord_tbl_ent(map2,
6019 slot2,
6020 df->index |
6021 IMSM_ORD_REBUILD);
6022 }
6023 }
6024 dprintf("set slot:%d to missing disk:%d\n", slot, df->index);
6025 break;
6026 }
6027
6028 /* if we are creating the first raid device update the family number */
6029 if (super->current_vol == 0) {
6030 __u32 sum;
6031 struct imsm_dev *_dev = __get_imsm_dev(mpb, 0);
6032
6033 _disk = __get_imsm_disk(mpb, dl->index);
6034 if (!_disk) {
6035 pr_err("BUG mpb setup error\n");
6036 return 1;
6037 }
6038 *_dev = *dev;
6039 *_disk = dl->disk;
6040 sum = random32();
6041 sum += __gen_imsm_checksum(mpb);
6042 mpb->family_num = __cpu_to_le32(sum);
6043 mpb->orig_family_num = mpb->family_num;
6044 mpb->creation_time = __cpu_to_le64((__u64)time(NULL));
6045 }
6046 super->current_disk = dl;
6047 return 0;
6048 }
6049
6050 /* mark_spare()
6051 * Function marks disk as spare and restores disk serial
6052 * in case it was previously marked as failed by takeover operation
6053 * reruns:
6054 * -1 : critical error
6055 * 0 : disk is marked as spare but serial is not set
6056 * 1 : success
6057 */
6058 int mark_spare(struct dl *disk)
6059 {
6060 __u8 serial[MAX_RAID_SERIAL_LEN];
6061 int ret_val = -1;
6062
6063 if (!disk)
6064 return ret_val;
6065
6066 ret_val = 0;
6067 if (!imsm_read_serial(disk->fd, NULL, serial, MAX_RAID_SERIAL_LEN)) {
6068 /* Restore disk serial number, because takeover marks disk
6069 * as failed and adds to serial ':0' before it becomes
6070 * a spare disk.
6071 */
6072 serialcpy(disk->serial, serial);
6073 serialcpy(disk->disk.serial, serial);
6074 ret_val = 1;
6075 }
6076 disk->disk.status = SPARE_DISK;
6077 disk->index = -1;
6078
6079 return ret_val;
6080 }
6081
6082
6083 static int write_super_imsm_spare(struct intel_super *super, struct dl *d);
6084
6085 static int add_to_super_imsm(struct supertype *st, mdu_disk_info_t *dk,
6086 int fd, char *devname,
6087 unsigned long long data_offset)
6088 {
6089 struct intel_super *super = st->sb;
6090 unsigned int member_sector_size;
6091 unsigned long long size;
6092 struct stat stb;
6093 struct dl *dd;
6094 __u32 id;
6095 int rv;
6096
6097 /* If we are on an RAID enabled platform check that the disk is
6098 * attached to the raid controller.
6099 * We do not need to test disks attachment for container based additions,
6100 * they shall be already tested when container was created/assembled.
6101 */
6102 rv = find_intel_hba_capability(fd, super, devname);
6103 /* no orom/efi or non-intel hba of the disk */
6104 if (rv != 0) {
6105 dprintf("capability: %p fd: %d ret: %d\n", super->orom, fd, rv);
6106 return MDADM_STATUS_ERROR;
6107 }
6108
6109 if (super->current_vol >= 0)
6110 return add_to_super_imsm_volume(st, dk, fd, devname);
6111
6112 if (fstat(fd, &stb) != 0)
6113 return MDADM_STATUS_ERROR;
6114
6115 dd = xcalloc(sizeof(*dd), 1);
6116
6117 if (devname)
6118 dd->devname = xstrdup(devname);
6119
6120 if (sysfs_disk_to_scsi_id(fd, &id) == 0)
6121 dd->disk.scsi_id = __cpu_to_le32(id);
6122
6123 dd->major = major(stb.st_rdev);
6124 dd->minor = minor(stb.st_rdev);
6125 dd->action = DISK_ADD;
6126 dd->fd = fd;
6127
6128 rv = imsm_read_serial(fd, devname, dd->serial, MAX_RAID_SERIAL_LEN);
6129 if (rv) {
6130 pr_err("failed to retrieve scsi serial, aborting\n");
6131 goto error;
6132 }
6133
6134 if (super->hba && ((super->hba->type == SYS_DEV_NVME) ||
6135 (super->hba->type == SYS_DEV_VMD))) {
6136 char pci_dev_path[PATH_MAX];
6137 char cntrl_path[PATH_MAX];
6138
6139 if (!diskfd_to_devpath(fd, 2, pci_dev_path) ||
6140 !diskfd_to_devpath(fd, 1, cntrl_path)) {
6141 pr_err("failed to get dev paths, aborting\n");
6142 goto error;
6143 }
6144
6145 if (is_multipath_nvme(fd))
6146 pr_err("%s controller supports Multi-Path I/O, Intel (R) VROC does not support multipathing\n",
6147 basename(cntrl_path));
6148
6149 if (super->orom && devpath_to_vendor(pci_dev_path) != 0x8086 &&
6150 !imsm_orom_has_tpv_support(super->orom)) {
6151 pr_err("\tPlatform configuration does not support non-Intel NVMe drives.\n"
6152 "\tPlease refer to Intel(R) RSTe/VROC user guide.\n");
6153 goto error;
6154 }
6155 }
6156
6157 if (!get_dev_size(fd, NULL, &size) || !get_dev_sector_size(fd, NULL, &member_sector_size))
6158 goto error;
6159
6160 if (super->sector_size == 0)
6161 /* this a first device, so sector_size is not set yet */
6162 super->sector_size = member_sector_size;
6163
6164 /* clear migr_rec when adding disk to container */
6165 memset(super->migr_rec_buf, 0, MIGR_REC_BUF_SECTORS * MAX_SECTOR_SIZE);
6166
6167 if (lseek64(fd, (size - MIGR_REC_SECTOR_POSITION * member_sector_size), SEEK_SET) >= 0) {
6168 unsigned int nbytes = MIGR_REC_BUF_SECTORS * member_sector_size;
6169
6170 if ((unsigned int)write(fd, super->migr_rec_buf, nbytes) != nbytes)
6171 perror("Write migr_rec failed");
6172 }
6173
6174 size /= 512;
6175 serialcpy(dd->disk.serial, dd->serial);
6176 set_total_blocks(&dd->disk, size);
6177
6178 if (__le32_to_cpu(dd->disk.total_blocks_hi) > 0) {
6179 struct imsm_super *mpb = super->anchor;
6180
6181 mpb->attributes |= MPB_ATTRIB_2TB_DISK;
6182 }
6183
6184 mark_spare(dd);
6185
6186 if (st->update_tail) {
6187 dd->next = super->disk_mgmt_list;
6188 super->disk_mgmt_list = dd;
6189 } else {
6190 /* this is called outside of mdmon
6191 * write initial spare metadata
6192 * mdmon will overwrite it.
6193 */
6194 dd->next = super->disks;
6195 super->disks = dd;
6196 write_super_imsm_spare(super, dd);
6197 }
6198
6199 return MDADM_STATUS_SUCCESS;
6200
6201 error:
6202 __free_imsm_disk(dd, 0);
6203 return MDADM_STATUS_ERROR;
6204 }
6205
6206 static int remove_from_super_imsm(struct supertype *st, mdu_disk_info_t *dk)
6207 {
6208 struct intel_super *super = st->sb;
6209 struct dl *dd;
6210
6211 /* remove from super works only in mdmon - for communication
6212 * manager - monitor. Check if communication memory buffer
6213 * is prepared.
6214 */
6215 if (!st->update_tail) {
6216 pr_err("shall be used in mdmon context only\n");
6217 return 1;
6218 }
6219 dd = xcalloc(1, sizeof(*dd));
6220 dd->major = dk->major;
6221 dd->minor = dk->minor;
6222 dd->fd = -1;
6223 mark_spare(dd);
6224 dd->action = DISK_REMOVE;
6225
6226 dd->next = super->disk_mgmt_list;
6227 super->disk_mgmt_list = dd;
6228
6229 return 0;
6230 }
6231
6232 static int store_imsm_mpb(int fd, struct imsm_super *mpb);
6233
6234 static union {
6235 char buf[MAX_SECTOR_SIZE];
6236 struct imsm_super anchor;
6237 } spare_record __attribute__ ((aligned(MAX_SECTOR_SIZE)));
6238
6239
6240 static int write_super_imsm_spare(struct intel_super *super, struct dl *d)
6241 {
6242 struct imsm_super *spare = &spare_record.anchor;
6243 __u32 sum;
6244
6245 if (d->index != -1)
6246 return 1;
6247
6248 spare->mpb_size = __cpu_to_le32(sizeof(struct imsm_super));
6249 spare->generation_num = __cpu_to_le32(1UL);
6250 spare->num_disks = 1;
6251 spare->num_raid_devs = 0;
6252 spare->pwr_cycle_count = __cpu_to_le32(1);
6253
6254 imsm_write_signature(spare);
6255
6256 spare->disk[0] = d->disk;
6257 if (__le32_to_cpu(d->disk.total_blocks_hi) > 0)
6258 spare->attributes |= MPB_ATTRIB_2TB_DISK;
6259
6260 if (super->sector_size == 4096)
6261 convert_to_4k_imsm_disk(&spare->disk[0]);
6262
6263 sum = __gen_imsm_checksum(spare);
6264 spare->family_num = __cpu_to_le32(sum);
6265 spare->orig_family_num = 0;
6266 sum = __gen_imsm_checksum(spare);
6267 spare->check_sum = __cpu_to_le32(sum);
6268
6269 if (store_imsm_mpb(d->fd, spare)) {
6270 pr_err("failed for device %d:%d %s\n",
6271 d->major, d->minor, strerror(errno));
6272 return 1;
6273 }
6274
6275 return 0;
6276 }
6277 /* spare records have their own family number and do not have any defined raid
6278 * devices
6279 */
6280 static int write_super_imsm_spares(struct intel_super *super, int doclose)
6281 {
6282 struct dl *d;
6283
6284 for (d = super->disks; d; d = d->next) {
6285 if (d->index != -1)
6286 continue;
6287
6288 if (write_super_imsm_spare(super, d))
6289 return 1;
6290
6291 if (doclose)
6292 close_fd(&d->fd);
6293 }
6294
6295 return 0;
6296 }
6297
6298 static int write_super_imsm(struct supertype *st, int doclose)
6299 {
6300 struct intel_super *super = st->sb;
6301 unsigned int sector_size = super->sector_size;
6302 struct imsm_super *mpb = super->anchor;
6303 struct dl *d;
6304 __u32 generation;
6305 __u32 sum;
6306 int spares = 0;
6307 int i;
6308 __u32 mpb_size = sizeof(struct imsm_super) - sizeof(struct imsm_disk);
6309 int num_disks = 0;
6310 int clear_migration_record = 1;
6311 __u32 bbm_log_size;
6312
6313 /* 'generation' is incremented everytime the metadata is written */
6314 generation = __le32_to_cpu(mpb->generation_num);
6315 generation++;
6316 mpb->generation_num = __cpu_to_le32(generation);
6317
6318 /* fix up cases where previous mdadm releases failed to set
6319 * orig_family_num
6320 */
6321 if (mpb->orig_family_num == 0)
6322 mpb->orig_family_num = mpb->family_num;
6323
6324 for (d = super->disks; d; d = d->next) {
6325 if (d->index == -1)
6326 spares++;
6327 else {
6328 mpb->disk[d->index] = d->disk;
6329 num_disks++;
6330 }
6331 }
6332 for (d = super->missing; d; d = d->next) {
6333 mpb->disk[d->index] = d->disk;
6334 num_disks++;
6335 }
6336 mpb->num_disks = num_disks;
6337 mpb_size += sizeof(struct imsm_disk) * mpb->num_disks;
6338
6339 for (i = 0; i < mpb->num_raid_devs; i++) {
6340 struct imsm_dev *dev = __get_imsm_dev(mpb, i);
6341 struct imsm_dev *dev2 = get_imsm_dev(super, i);
6342
6343 imsm_copy_dev(dev, dev2);
6344 mpb_size += sizeof_imsm_dev(dev, 0);
6345
6346 if (is_gen_migration(dev2))
6347 clear_migration_record = 0;
6348 }
6349
6350 bbm_log_size = get_imsm_bbm_log_size(super->bbm_log);
6351
6352 if (bbm_log_size) {
6353 memcpy((void *)mpb + mpb_size, super->bbm_log, bbm_log_size);
6354 mpb->attributes |= MPB_ATTRIB_BBM;
6355 } else
6356 mpb->attributes &= ~MPB_ATTRIB_BBM;
6357
6358 super->anchor->bbm_log_size = __cpu_to_le32(bbm_log_size);
6359 mpb_size += bbm_log_size;
6360 mpb->mpb_size = __cpu_to_le32(mpb_size);
6361
6362 #ifdef DEBUG
6363 assert(super->len == 0 || mpb_size <= super->len);
6364 #endif
6365
6366 /* recalculate checksum */
6367 sum = __gen_imsm_checksum(mpb);
6368 mpb->check_sum = __cpu_to_le32(sum);
6369
6370 if (super->clean_migration_record_by_mdmon) {
6371 clear_migration_record = 1;
6372 super->clean_migration_record_by_mdmon = 0;
6373 }
6374 if (clear_migration_record)
6375 memset(super->migr_rec_buf, 0,
6376 MIGR_REC_BUF_SECTORS*MAX_SECTOR_SIZE);
6377
6378 if (sector_size == 4096)
6379 convert_to_4k(super);
6380
6381 /* write the mpb for disks that compose raid devices */
6382 for (d = super->disks; d ; d = d->next) {
6383 if (d->index < 0 || is_failed(&d->disk))
6384 continue;
6385
6386 if (clear_migration_record) {
6387 unsigned long long dsize;
6388
6389 get_dev_size(d->fd, NULL, &dsize);
6390 if (lseek64(d->fd, dsize - sector_size,
6391 SEEK_SET) >= 0) {
6392 if ((unsigned int)write(d->fd,
6393 super->migr_rec_buf,
6394 MIGR_REC_BUF_SECTORS*sector_size) !=
6395 MIGR_REC_BUF_SECTORS*sector_size)
6396 perror("Write migr_rec failed");
6397 }
6398 }
6399
6400 if (store_imsm_mpb(d->fd, mpb))
6401 fprintf(stderr,
6402 "failed for device %d:%d (fd: %d)%s\n",
6403 d->major, d->minor,
6404 d->fd, strerror(errno));
6405
6406 if (doclose)
6407 close_fd(&d->fd);
6408 }
6409
6410 if (spares)
6411 return write_super_imsm_spares(super, doclose);
6412
6413 return 0;
6414 }
6415
6416 static int create_array(struct supertype *st, int dev_idx)
6417 {
6418 size_t len;
6419 struct imsm_update_create_array *u;
6420 struct intel_super *super = st->sb;
6421 struct imsm_dev *dev = get_imsm_dev(super, dev_idx);
6422 struct imsm_map *map = get_imsm_map(dev, MAP_0);
6423 struct disk_info *inf;
6424 struct imsm_disk *disk;
6425 int i;
6426
6427 len = sizeof(*u) - sizeof(*dev) + sizeof_imsm_dev(dev, 0) +
6428 sizeof(*inf) * map->num_members;
6429 u = xmalloc(len);
6430 u->type = update_create_array;
6431 u->dev_idx = dev_idx;
6432 imsm_copy_dev(&u->dev, dev);
6433 inf = get_disk_info(u);
6434 for (i = 0; i < map->num_members; i++) {
6435 int idx = get_imsm_disk_idx(dev, i, MAP_X);
6436
6437 disk = get_imsm_disk(super, idx);
6438 if (!disk)
6439 disk = get_imsm_missing(super, idx);
6440 serialcpy(inf[i].serial, disk->serial);
6441 }
6442 append_metadata_update(st, u, len);
6443
6444 return 0;
6445 }
6446
6447 static int mgmt_disk(struct supertype *st)
6448 {
6449 struct intel_super *super = st->sb;
6450 size_t len;
6451 struct imsm_update_add_remove_disk *u;
6452
6453 if (!super->disk_mgmt_list)
6454 return 0;
6455
6456 len = sizeof(*u);
6457 u = xmalloc(len);
6458 u->type = update_add_remove_disk;
6459 append_metadata_update(st, u, len);
6460
6461 return 0;
6462 }
6463
6464 __u32 crc32c_le(__u32 crc, unsigned char const *p, size_t len);
6465
6466 static int write_ppl_header(unsigned long long ppl_sector, int fd, void *buf)
6467 {
6468 struct ppl_header *ppl_hdr = buf;
6469 int ret;
6470
6471 ppl_hdr->checksum = __cpu_to_le32(~crc32c_le(~0, buf, PPL_HEADER_SIZE));
6472
6473 if (lseek64(fd, ppl_sector * 512, SEEK_SET) < 0) {
6474 ret = -errno;
6475 perror("Failed to seek to PPL header location");
6476 return ret;
6477 }
6478
6479 if (write(fd, buf, PPL_HEADER_SIZE) != PPL_HEADER_SIZE) {
6480 ret = -errno;
6481 perror("Write PPL header failed");
6482 return ret;
6483 }
6484
6485 fsync(fd);
6486
6487 return 0;
6488 }
6489
6490 static int write_init_ppl_imsm(struct supertype *st, struct mdinfo *info, int fd)
6491 {
6492 struct intel_super *super = st->sb;
6493 void *buf;
6494 struct ppl_header *ppl_hdr;
6495 int ret;
6496
6497 /* first clear entire ppl space */
6498 ret = zero_disk_range(fd, info->ppl_sector, info->ppl_size);
6499 if (ret)
6500 return ret;
6501
6502 ret = posix_memalign(&buf, MAX_SECTOR_SIZE, PPL_HEADER_SIZE);
6503 if (ret) {
6504 pr_err("Failed to allocate PPL header buffer\n");
6505 return -ret;
6506 }
6507
6508 memset(buf, 0, PPL_HEADER_SIZE);
6509 ppl_hdr = buf;
6510 memset(ppl_hdr->reserved, 0xff, PPL_HDR_RESERVED);
6511 ppl_hdr->signature = __cpu_to_le32(super->anchor->orig_family_num);
6512
6513 if (info->mismatch_cnt) {
6514 /*
6515 * We are overwriting an invalid ppl. Make one entry with wrong
6516 * checksum to prevent the kernel from skipping resync.
6517 */
6518 ppl_hdr->entries_count = __cpu_to_le32(1);
6519 ppl_hdr->entries[0].checksum = ~0;
6520 }
6521
6522 ret = write_ppl_header(info->ppl_sector, fd, buf);
6523
6524 free(buf);
6525 return ret;
6526 }
6527
6528 static int is_rebuilding(struct imsm_dev *dev);
6529
6530 static int validate_ppl_imsm(struct supertype *st, struct mdinfo *info,
6531 struct mdinfo *disk)
6532 {
6533 struct intel_super *super = st->sb;
6534 struct dl *d;
6535 void *buf_orig, *buf, *buf_prev = NULL;
6536 int ret = 0;
6537 struct ppl_header *ppl_hdr = NULL;
6538 __u32 crc;
6539 struct imsm_dev *dev;
6540 __u32 idx;
6541 unsigned int i;
6542 unsigned long long ppl_offset = 0;
6543 unsigned long long prev_gen_num = 0;
6544
6545 if (disk->disk.raid_disk < 0)
6546 return 0;
6547
6548 dev = get_imsm_dev(super, info->container_member);
6549 idx = get_imsm_disk_idx(dev, disk->disk.raid_disk, MAP_0);
6550 d = get_imsm_dl_disk(super, idx);
6551
6552 if (!d || d->index < 0 || is_failed(&d->disk))
6553 return 0;
6554
6555 if (posix_memalign(&buf_orig, MAX_SECTOR_SIZE, PPL_HEADER_SIZE * 2)) {
6556 pr_err("Failed to allocate PPL header buffer\n");
6557 return -1;
6558 }
6559 buf = buf_orig;
6560
6561 ret = 1;
6562 while (ppl_offset < MULTIPLE_PPL_AREA_SIZE_IMSM) {
6563 void *tmp;
6564
6565 dprintf("Checking potential PPL at offset: %llu\n", ppl_offset);
6566
6567 if (lseek64(d->fd, info->ppl_sector * 512 + ppl_offset,
6568 SEEK_SET) < 0) {
6569 perror("Failed to seek to PPL header location");
6570 ret = -1;
6571 break;
6572 }
6573
6574 if (read(d->fd, buf, PPL_HEADER_SIZE) != PPL_HEADER_SIZE) {
6575 perror("Read PPL header failed");
6576 ret = -1;
6577 break;
6578 }
6579
6580 ppl_hdr = buf;
6581
6582 crc = __le32_to_cpu(ppl_hdr->checksum);
6583 ppl_hdr->checksum = 0;
6584
6585 if (crc != ~crc32c_le(~0, buf, PPL_HEADER_SIZE)) {
6586 dprintf("Wrong PPL header checksum on %s\n",
6587 d->devname);
6588 break;
6589 }
6590
6591 if (prev_gen_num > __le64_to_cpu(ppl_hdr->generation)) {
6592 /* previous was newest, it was already checked */
6593 break;
6594 }
6595
6596 if ((__le32_to_cpu(ppl_hdr->signature) !=
6597 super->anchor->orig_family_num)) {
6598 dprintf("Wrong PPL header signature on %s\n",
6599 d->devname);
6600 ret = 1;
6601 break;
6602 }
6603
6604 ret = 0;
6605 prev_gen_num = __le64_to_cpu(ppl_hdr->generation);
6606
6607 ppl_offset += PPL_HEADER_SIZE;
6608 for (i = 0; i < __le32_to_cpu(ppl_hdr->entries_count); i++)
6609 ppl_offset +=
6610 __le32_to_cpu(ppl_hdr->entries[i].pp_size);
6611
6612 if (!buf_prev)
6613 buf_prev = buf + PPL_HEADER_SIZE;
6614 tmp = buf_prev;
6615 buf_prev = buf;
6616 buf = tmp;
6617 }
6618
6619 if (buf_prev) {
6620 buf = buf_prev;
6621 ppl_hdr = buf_prev;
6622 }
6623
6624 /*
6625 * Update metadata to use mutliple PPLs area (1MB).
6626 * This is done once for all RAID members
6627 */
6628 if (info->consistency_policy == CONSISTENCY_POLICY_PPL &&
6629 info->ppl_size != (MULTIPLE_PPL_AREA_SIZE_IMSM >> 9)) {
6630 char subarray[20];
6631 struct mdinfo *member_dev;
6632
6633 sprintf(subarray, "%d", info->container_member);
6634
6635 if (mdmon_running(st->container_devnm))
6636 st->update_tail = &st->updates;
6637
6638 if (st->ss->update_subarray(st, subarray, UOPT_PPL, NULL)) {
6639 pr_err("Failed to update subarray %s\n",
6640 subarray);
6641 } else {
6642 if (st->update_tail)
6643 flush_metadata_updates(st);
6644 else
6645 st->ss->sync_metadata(st);
6646 info->ppl_size = (MULTIPLE_PPL_AREA_SIZE_IMSM >> 9);
6647 for (member_dev = info->devs; member_dev;
6648 member_dev = member_dev->next)
6649 member_dev->ppl_size =
6650 (MULTIPLE_PPL_AREA_SIZE_IMSM >> 9);
6651 }
6652 }
6653
6654 if (ret == 1) {
6655 struct imsm_map *map = get_imsm_map(dev, MAP_X);
6656
6657 if (map->map_state == IMSM_T_STATE_UNINITIALIZED ||
6658 (map->map_state == IMSM_T_STATE_NORMAL &&
6659 !(dev->vol.dirty & RAIDVOL_DIRTY)) ||
6660 (is_rebuilding(dev) &&
6661 vol_curr_migr_unit(dev) == 0 &&
6662 get_imsm_disk_idx(dev, disk->disk.raid_disk, MAP_1) != idx))
6663 ret = st->ss->write_init_ppl(st, info, d->fd);
6664 else
6665 info->mismatch_cnt++;
6666 } else if (ret == 0 &&
6667 ppl_hdr->entries_count == 0 &&
6668 is_rebuilding(dev) &&
6669 info->resync_start == 0) {
6670 /*
6671 * The header has no entries - add a single empty entry and
6672 * rewrite the header to prevent the kernel from going into
6673 * resync after an interrupted rebuild.
6674 */
6675 ppl_hdr->entries_count = __cpu_to_le32(1);
6676 ret = write_ppl_header(info->ppl_sector, d->fd, buf);
6677 }
6678
6679 free(buf_orig);
6680
6681 return ret;
6682 }
6683
6684 static int write_init_ppl_imsm_all(struct supertype *st, struct mdinfo *info)
6685 {
6686 struct intel_super *super = st->sb;
6687 struct dl *d;
6688 int ret = 0;
6689
6690 if (info->consistency_policy != CONSISTENCY_POLICY_PPL ||
6691 info->array.level != 5)
6692 return 0;
6693
6694 for (d = super->disks; d ; d = d->next) {
6695 if (d->index < 0 || is_failed(&d->disk))
6696 continue;
6697
6698 ret = st->ss->write_init_ppl(st, info, d->fd);
6699 if (ret)
6700 break;
6701 }
6702
6703 return ret;
6704 }
6705
6706 /*******************************************************************************
6707 * Function: write_init_bitmap_imsm_vol
6708 * Description: Write a bitmap header and prepares the area for the bitmap.
6709 * Parameters:
6710 * st : supertype information
6711 * vol_idx : the volume index to use
6712 *
6713 * Returns:
6714 * 0 : success
6715 * -1 : fail
6716 ******************************************************************************/
6717 static int write_init_bitmap_imsm_vol(struct supertype *st, int vol_idx)
6718 {
6719 struct intel_super *super = st->sb;
6720 int prev_current_vol = super->current_vol;
6721 struct dl *d;
6722 int ret = 0;
6723
6724 super->current_vol = vol_idx;
6725 for (d = super->disks; d; d = d->next) {
6726 if (d->index < 0 || is_failed(&d->disk))
6727 continue;
6728 ret = st->ss->write_bitmap(st, d->fd, NoUpdate);
6729 if (ret)
6730 break;
6731 }
6732 super->current_vol = prev_current_vol;
6733 return ret;
6734 }
6735
6736 /*******************************************************************************
6737 * Function: write_init_bitmap_imsm_all
6738 * Description: Write a bitmap header and prepares the area for the bitmap.
6739 * Operation is executed for volumes with CONSISTENCY_POLICY_BITMAP.
6740 * Parameters:
6741 * st : supertype information
6742 * info : info about the volume where the bitmap should be written
6743 * vol_idx : the volume index to use
6744 *
6745 * Returns:
6746 * 0 : success
6747 * -1 : fail
6748 ******************************************************************************/
6749 static int write_init_bitmap_imsm_all(struct supertype *st, struct mdinfo *info,
6750 int vol_idx)
6751 {
6752 int ret = 0;
6753
6754 if (info && (info->consistency_policy == CONSISTENCY_POLICY_BITMAP))
6755 ret = write_init_bitmap_imsm_vol(st, vol_idx);
6756
6757 return ret;
6758 }
6759
6760 static int write_init_super_imsm(struct supertype *st)
6761 {
6762 struct intel_super *super = st->sb;
6763 int current_vol = super->current_vol;
6764 int rv = 0;
6765 struct mdinfo info;
6766
6767 getinfo_super_imsm(st, &info, NULL);
6768
6769 /* we are done with current_vol reset it to point st at the container */
6770 super->current_vol = -1;
6771
6772 if (st->update_tail) {
6773 /* queue the recently created array / added disk
6774 * as a metadata update */
6775
6776 /* determine if we are creating a volume or adding a disk */
6777 if (current_vol < 0) {
6778 /* in the mgmt (add/remove) disk case we are running
6779 * in mdmon context, so don't close fd's
6780 */
6781 rv = mgmt_disk(st);
6782 } else {
6783 /* adding the second volume to the array */
6784 rv = write_init_ppl_imsm_all(st, &info);
6785 if (!rv)
6786 rv = write_init_bitmap_imsm_all(st, &info, current_vol);
6787 if (!rv)
6788 rv = create_array(st, current_vol);
6789 }
6790 } else {
6791 struct dl *d;
6792 for (d = super->disks; d; d = d->next)
6793 Kill(d->devname, NULL, 0, -1, 1);
6794 if (current_vol >= 0) {
6795 rv = write_init_ppl_imsm_all(st, &info);
6796 if (!rv)
6797 rv = write_init_bitmap_imsm_all(st, &info, current_vol);
6798 }
6799
6800 if (!rv)
6801 rv = write_super_imsm(st, 1);
6802 }
6803
6804 return rv;
6805 }
6806
6807 static int store_super_imsm(struct supertype *st, int fd)
6808 {
6809 struct intel_super *super = st->sb;
6810 struct imsm_super *mpb = super ? super->anchor : NULL;
6811
6812 if (!mpb)
6813 return 1;
6814
6815 if (super->sector_size == 4096)
6816 convert_to_4k(super);
6817 return store_imsm_mpb(fd, mpb);
6818 }
6819
6820 static int validate_geometry_imsm_container(struct supertype *st, int level,
6821 int raiddisks,
6822 unsigned long long data_offset,
6823 char *dev,
6824 unsigned long long *freesize,
6825 int verbose)
6826 {
6827 int fd;
6828 unsigned long long ldsize;
6829 struct intel_super *super = NULL;
6830 int rv = 0;
6831
6832 if (!is_container(level))
6833 return 0;
6834 if (!dev)
6835 return 1;
6836
6837 fd = dev_open(dev, O_RDONLY|O_EXCL);
6838 if (!is_fd_valid(fd)) {
6839 pr_vrb("imsm: Cannot open %s: %s\n", dev, strerror(errno));
6840 return 0;
6841 }
6842 if (!get_dev_size(fd, dev, &ldsize))
6843 goto exit;
6844
6845 /* capabilities retrieve could be possible
6846 * note that there is no fd for the disks in array.
6847 */
6848 super = alloc_super();
6849 if (!super)
6850 goto exit;
6851
6852 if (!get_dev_sector_size(fd, NULL, &super->sector_size))
6853 goto exit;
6854
6855 rv = find_intel_hba_capability(fd, super, verbose > 0 ? dev : NULL);
6856 if (rv != 0) {
6857 #if DEBUG
6858 char str[256];
6859 fd2devname(fd, str);
6860 dprintf("fd: %d %s orom: %p rv: %d raiddisk: %d\n",
6861 fd, str, super->orom, rv, raiddisks);
6862 #endif
6863 /* no orom/efi or non-intel hba of the disk */
6864 rv = 0;
6865 goto exit;
6866 }
6867 if (super->orom) {
6868 if (raiddisks > super->orom->tds) {
6869 if (verbose)
6870 pr_err("%d exceeds maximum number of platform supported disks: %d\n",
6871 raiddisks, super->orom->tds);
6872 goto exit;
6873 }
6874 if ((super->orom->attr & IMSM_OROM_ATTR_2TB_DISK) == 0 &&
6875 (ldsize >> 9) >> 32 > 0) {
6876 if (verbose)
6877 pr_err("%s exceeds maximum platform supported size\n", dev);
6878 goto exit;
6879 }
6880
6881 if (super->hba->type == SYS_DEV_VMD ||
6882 super->hba->type == SYS_DEV_NVME) {
6883 if (!imsm_is_nvme_namespace_supported(fd, 1)) {
6884 if (verbose)
6885 pr_err("NVMe namespace %s is not supported by IMSM\n",
6886 basename(dev));
6887 goto exit;
6888 }
6889 }
6890 }
6891 if (freesize)
6892 *freesize = avail_size_imsm(st, ldsize >> 9, data_offset);
6893 rv = 1;
6894 exit:
6895 if (super)
6896 free_imsm(super);
6897 close(fd);
6898
6899 return rv;
6900 }
6901
6902 static unsigned long long find_size(struct extent *e, int *idx, int num_extents)
6903 {
6904 const unsigned long long base_start = e[*idx].start;
6905 unsigned long long end = base_start + e[*idx].size;
6906 int i;
6907
6908 if (base_start == end)
6909 return 0;
6910
6911 *idx = *idx + 1;
6912 for (i = *idx; i < num_extents; i++) {
6913 /* extend overlapping extents */
6914 if (e[i].start >= base_start &&
6915 e[i].start <= end) {
6916 if (e[i].size == 0)
6917 return 0;
6918 if (e[i].start + e[i].size > end)
6919 end = e[i].start + e[i].size;
6920 } else if (e[i].start > end) {
6921 *idx = i;
6922 break;
6923 }
6924 }
6925
6926 return end - base_start;
6927 }
6928
6929 /** merge_extents() - analyze extents and get free size.
6930 * @super: Intel metadata, not NULL.
6931 * @expanding: if set, we are expanding &super->current_vol.
6932 *
6933 * Build a composite disk with all known extents and generate a size given the
6934 * "all disks in an array must share a common start offset" constraint.
6935 * If a volume is expanded, then return free space after the volume.
6936 *
6937 * Return: Free space or 0 on failure.
6938 */
6939 static unsigned long long merge_extents(struct intel_super *super, const bool expanding)
6940 {
6941 struct extent *e;
6942 struct dl *dl;
6943 int i, j, pos_vol_idx = -1;
6944 int extent_idx = 0;
6945 int sum_extents = 0;
6946 unsigned long long pos = 0;
6947 unsigned long long start = 0;
6948 unsigned long long free_size = 0;
6949
6950 unsigned long pre_reservation = 0;
6951 unsigned long post_reservation = IMSM_RESERVED_SECTORS;
6952 unsigned long reservation_size;
6953
6954 for (dl = super->disks; dl; dl = dl->next)
6955 if (dl->e)
6956 sum_extents += dl->extent_cnt;
6957 e = xcalloc(sum_extents, sizeof(struct extent));
6958
6959 /* coalesce and sort all extents. also, check to see if we need to
6960 * reserve space between member arrays
6961 */
6962 j = 0;
6963 for (dl = super->disks; dl; dl = dl->next) {
6964 if (!dl->e)
6965 continue;
6966 for (i = 0; i < dl->extent_cnt; i++)
6967 e[j++] = dl->e[i];
6968 }
6969 qsort(e, sum_extents, sizeof(*e), cmp_extent);
6970
6971 /* merge extents */
6972 i = 0;
6973 j = 0;
6974 while (i < sum_extents) {
6975 e[j].start = e[i].start;
6976 e[j].vol = e[i].vol;
6977 e[j].size = find_size(e, &i, sum_extents);
6978 j++;
6979 if (e[j-1].size == 0)
6980 break;
6981 }
6982
6983 i = 0;
6984 do {
6985 unsigned long long esize = e[i].start - pos;
6986
6987 if (expanding ? pos_vol_idx == super->current_vol : esize >= free_size) {
6988 free_size = esize;
6989 start = pos;
6990 extent_idx = i;
6991 }
6992
6993 pos = e[i].start + e[i].size;
6994 pos_vol_idx = e[i].vol;
6995
6996 i++;
6997 } while (e[i-1].size);
6998
6999 if (free_size == 0) {
7000 dprintf("imsm: Cannot find free size.\n");
7001 free(e);
7002 return 0;
7003 }
7004
7005 if (!expanding && extent_idx != 0)
7006 /*
7007 * Not a real first volume in a container is created, pre_reservation is needed.
7008 */
7009 pre_reservation = IMSM_RESERVED_SECTORS;
7010
7011 if (e[extent_idx].size == 0)
7012 /*
7013 * extent_idx points to the metadata, post_reservation is allready done.
7014 */
7015 post_reservation = 0;
7016 free(e);
7017
7018 reservation_size = pre_reservation + post_reservation;
7019
7020 if (free_size < reservation_size) {
7021 dprintf("imsm: Reservation size is greater than free space.\n");
7022 return 0;
7023 }
7024
7025 super->create_offset = start + pre_reservation;
7026 return free_size - reservation_size;
7027 }
7028
7029 /**
7030 * is_raid_level_supported() - check if this count of drives and level is supported by platform.
7031 * @orom: hardware properties, could be NULL.
7032 * @level: requested raid level.
7033 * @raiddisks: requested disk count.
7034 *
7035 * IMSM UEFI/OROM does not provide information about supported count of raid disks
7036 * for particular level. That is why it is hardcoded.
7037 * It is recommended to not allow of usage other levels than supported,
7038 * IMSM code is not tested against different level implementations.
7039 *
7040 * Return: true if supported, false otherwise.
7041 */
7042 static bool is_raid_level_supported(const struct imsm_orom *orom, int level, int raiddisks)
7043 {
7044 int idx;
7045
7046 for (idx = 0; imsm_level_ops[idx].name; idx++) {
7047 if (imsm_level_ops[idx].level == level)
7048 break;
7049 }
7050
7051 if (!imsm_level_ops[idx].name)
7052 return false;
7053
7054 if (!imsm_level_ops[idx].is_raiddisks_count_supported(raiddisks))
7055 return false;
7056
7057 if (!orom)
7058 return true;
7059
7060 if (imsm_level_ops[idx].is_level_supported(orom))
7061 return true;
7062
7063 return false;
7064 }
7065
7066 static int
7067 active_arrays_by_format(char *name, char* hba, struct md_list **devlist,
7068 int dpa, int verbose)
7069 {
7070 struct mdstat_ent *mdstat = mdstat_read(0, 0);
7071 struct mdstat_ent *memb;
7072 int count = 0;
7073 int num = 0;
7074 struct md_list *dv;
7075 int found;
7076
7077 for (memb = mdstat ; memb ; memb = memb->next) {
7078 if (is_mdstat_ent_external(memb) && !is_subarray(memb->metadata_version + 9) &&
7079 strcmp(&memb->metadata_version[9], name) == 0 && memb->members) {
7080 struct dev_member *dev = memb->members;
7081 int fd = -1;
7082
7083 while (dev && !is_fd_valid(fd)) {
7084 char path[PATH_MAX];
7085
7086 num = snprintf(path, PATH_MAX, "%s%s", "/dev/", dev->name);
7087 if (num > 0)
7088 fd = open(path, O_RDONLY, 0);
7089 if (num <= 0 || !is_fd_valid(fd)) {
7090 pr_vrb("Cannot open %s: %s\n",
7091 dev->name, strerror(errno));
7092 }
7093 dev = dev->next;
7094 }
7095 found = 0;
7096 if (is_fd_valid(fd) && disk_attached_to_hba(fd, hba)) {
7097 struct mdstat_ent *vol;
7098 for (vol = mdstat ; vol ; vol = vol->next) {
7099 if (vol->active > 0 &&
7100 is_container_member(vol, memb->devnm)) {
7101 found++;
7102 count++;
7103 }
7104 }
7105 if (*devlist && (found < dpa)) {
7106 dv = xcalloc(1, sizeof(*dv));
7107 dv->devname = xmalloc(strlen(memb->devnm) + strlen("/dev/") + 1);
7108 sprintf(dv->devname, "%s%s", "/dev/", memb->devnm);
7109 dv->found = found;
7110 dv->used = 0;
7111 dv->next = *devlist;
7112 *devlist = dv;
7113 }
7114 }
7115 close_fd(&fd);
7116 }
7117 }
7118 free_mdstat(mdstat);
7119 return count;
7120 }
7121
7122 #ifdef DEBUG_LOOP
7123 static struct md_list*
7124 get_loop_devices(void)
7125 {
7126 int i;
7127 struct md_list *devlist = NULL;
7128 struct md_list *dv;
7129
7130 for(i = 0; i < 12; i++) {
7131 dv = xcalloc(1, sizeof(*dv));
7132 dv->devname = xmalloc(40);
7133 sprintf(dv->devname, "/dev/loop%d", i);
7134 dv->next = devlist;
7135 devlist = dv;
7136 }
7137 return devlist;
7138 }
7139 #endif
7140
7141 static struct md_list*
7142 get_devices(const char *hba_path)
7143 {
7144 struct md_list *devlist = NULL;
7145 struct md_list *dv;
7146 struct dirent *ent;
7147 DIR *dir;
7148
7149 #if DEBUG_LOOP
7150 devlist = get_loop_devices();
7151 return devlist;
7152 #endif
7153 /* scroll through /sys/dev/block looking for devices attached to
7154 * this hba
7155 */
7156 dir = opendir("/sys/dev/block");
7157 for (ent = dir ? readdir(dir) : NULL; ent; ent = readdir(dir)) {
7158 int fd;
7159 char buf[1024];
7160 int major, minor;
7161 char *path = NULL;
7162 if (sscanf(ent->d_name, "%d:%d", &major, &minor) != 2)
7163 continue;
7164 path = devt_to_devpath(makedev(major, minor), 1, NULL);
7165 if (!path)
7166 continue;
7167 if (!is_path_attached_to_hba(path, hba_path)) {
7168 free(path);
7169 path = NULL;
7170 continue;
7171 }
7172 free(path);
7173 path = NULL;
7174 fd = dev_open(ent->d_name, O_RDONLY);
7175 if (is_fd_valid(fd)) {
7176 fd2devname(fd, buf);
7177 close(fd);
7178 } else {
7179 pr_err("cannot open device: %s\n",
7180 ent->d_name);
7181 continue;
7182 }
7183
7184 dv = xcalloc(1, sizeof(*dv));
7185 dv->devname = xstrdup(buf);
7186 dv->next = devlist;
7187 devlist = dv;
7188 }
7189 closedir(dir);
7190 return devlist;
7191 }
7192
7193 static int
7194 count_volumes_list(struct md_list *devlist, char *homehost,
7195 int verbose, int *found)
7196 {
7197 struct md_list *tmpdev;
7198 int count = 0;
7199 struct supertype *st;
7200
7201 /* first walk the list of devices to find a consistent set
7202 * that match the criterea, if that is possible.
7203 * We flag the ones we like with 'used'.
7204 */
7205 *found = 0;
7206 st = match_metadata_desc_imsm("imsm");
7207 if (st == NULL) {
7208 pr_vrb("cannot allocate memory for imsm supertype\n");
7209 return 0;
7210 }
7211
7212 for (tmpdev = devlist; tmpdev; tmpdev = tmpdev->next) {
7213 char *devname = tmpdev->devname;
7214 dev_t rdev;
7215 struct supertype *tst;
7216 int dfd;
7217 if (tmpdev->used > 1)
7218 continue;
7219 tst = dup_super(st);
7220 if (tst == NULL) {
7221 pr_vrb("cannot allocate memory for imsm supertype\n");
7222 goto err_1;
7223 }
7224 tmpdev->container = 0;
7225 dfd = dev_open(devname, O_RDONLY|O_EXCL);
7226 if (!is_fd_valid(dfd)) {
7227 dprintf("cannot open device %s: %s\n",
7228 devname, strerror(errno));
7229 tmpdev->used = 2;
7230 } else if (!fstat_is_blkdev(dfd, devname, &rdev)) {
7231 tmpdev->used = 2;
7232 } else if (must_be_container(dfd)) {
7233 struct supertype *cst;
7234 cst = super_by_fd(dfd, NULL);
7235 if (cst == NULL) {
7236 dprintf("cannot recognize container type %s\n",
7237 devname);
7238 tmpdev->used = 2;
7239 } else if (tst->ss != st->ss) {
7240 dprintf("non-imsm container - ignore it: %s\n",
7241 devname);
7242 tmpdev->used = 2;
7243 } else if (!tst->ss->load_container ||
7244 tst->ss->load_container(tst, dfd, NULL))
7245 tmpdev->used = 2;
7246 else {
7247 tmpdev->container = 1;
7248 }
7249 if (cst)
7250 cst->ss->free_super(cst);
7251 } else {
7252 tmpdev->st_rdev = rdev;
7253 if (tst->ss->load_super(tst,dfd, NULL)) {
7254 dprintf("no RAID superblock on %s\n",
7255 devname);
7256 tmpdev->used = 2;
7257 } else if (tst->ss->compare_super == NULL) {
7258 dprintf("Cannot assemble %s metadata on %s\n",
7259 tst->ss->name, devname);
7260 tmpdev->used = 2;
7261 }
7262 }
7263 close_fd(&dfd);
7264
7265 if (tmpdev->used == 2 || tmpdev->used == 4) {
7266 /* Ignore unrecognised devices during auto-assembly */
7267 goto loop;
7268 }
7269 else {
7270 struct mdinfo info;
7271 tst->ss->getinfo_super(tst, &info, NULL);
7272
7273 if (st->minor_version == -1)
7274 st->minor_version = tst->minor_version;
7275
7276 if (memcmp(info.uuid, uuid_zero,
7277 sizeof(int[4])) == 0) {
7278 /* this is a floating spare. It cannot define
7279 * an array unless there are no more arrays of
7280 * this type to be found. It can be included
7281 * in an array of this type though.
7282 */
7283 tmpdev->used = 3;
7284 goto loop;
7285 }
7286
7287 if (st->ss != tst->ss ||
7288 st->minor_version != tst->minor_version ||
7289 st->ss->compare_super(st, tst, 1) != 0) {
7290 /* Some mismatch. If exactly one array matches this host,
7291 * we can resolve on that one.
7292 * Or, if we are auto assembling, we just ignore the second
7293 * for now.
7294 */
7295 dprintf("superblock on %s doesn't match others - assembly aborted\n",
7296 devname);
7297 goto loop;
7298 }
7299 tmpdev->used = 1;
7300 *found = 1;
7301 dprintf("found: devname: %s\n", devname);
7302 }
7303 loop:
7304 if (tst)
7305 tst->ss->free_super(tst);
7306 }
7307 if (*found != 0) {
7308 int err;
7309 if ((err = load_super_imsm_all(st, -1, &st->sb, NULL, devlist, 0)) == 0) {
7310 struct mdinfo *iter, *head = st->ss->container_content(st, NULL);
7311 for (iter = head; iter; iter = iter->next) {
7312 dprintf("content->text_version: %s vol\n",
7313 iter->text_version);
7314 if (iter->array.state & (1<<MD_SB_BLOCK_VOLUME)) {
7315 /* do not assemble arrays with unsupported
7316 configurations */
7317 dprintf("Cannot activate member %s.\n",
7318 iter->text_version);
7319 } else
7320 count++;
7321 }
7322 sysfs_free(head);
7323
7324 } else {
7325 dprintf("No valid super block on device list: err: %d %p\n",
7326 err, st->sb);
7327 }
7328 } else {
7329 dprintf("no more devices to examine\n");
7330 }
7331
7332 for (tmpdev = devlist; tmpdev; tmpdev = tmpdev->next) {
7333 if (tmpdev->used == 1 && tmpdev->found) {
7334 if (count) {
7335 if (count < tmpdev->found)
7336 count = 0;
7337 else
7338 count -= tmpdev->found;
7339 }
7340 }
7341 if (tmpdev->used == 1)
7342 tmpdev->used = 4;
7343 }
7344 err_1:
7345 if (st)
7346 st->ss->free_super(st);
7347 return count;
7348 }
7349
7350 static int __count_volumes(char *hba_path, int dpa, int verbose,
7351 int cmp_hba_path)
7352 {
7353 struct sys_dev *idev, *intel_devices = find_intel_devices();
7354 int count = 0;
7355 const struct orom_entry *entry;
7356 struct devid_list *dv, *devid_list;
7357
7358 if (!hba_path)
7359 return 0;
7360
7361 for (idev = intel_devices; idev; idev = idev->next) {
7362 if (strstr(idev->path, hba_path))
7363 break;
7364 }
7365
7366 if (!idev || !idev->dev_id)
7367 return 0;
7368
7369 entry = get_orom_entry_by_device_id(idev->dev_id);
7370
7371 if (!entry || !entry->devid_list)
7372 return 0;
7373
7374 devid_list = entry->devid_list;
7375 for (dv = devid_list; dv; dv = dv->next) {
7376 struct md_list *devlist;
7377 struct sys_dev *device = NULL;
7378 char *hpath;
7379 int found = 0;
7380
7381 if (cmp_hba_path)
7382 device = device_by_id_and_path(dv->devid, hba_path);
7383 else
7384 device = device_by_id(dv->devid);
7385
7386 if (device)
7387 hpath = device->path;
7388 else
7389 return 0;
7390
7391 devlist = get_devices(hpath);
7392 /* if no intel devices return zero volumes */
7393 if (devlist == NULL)
7394 return 0;
7395
7396 count += active_arrays_by_format("imsm", hpath, &devlist, dpa,
7397 verbose);
7398 dprintf("path: %s active arrays: %d\n", hpath, count);
7399 if (devlist == NULL)
7400 return 0;
7401 do {
7402 found = 0;
7403 count += count_volumes_list(devlist,
7404 NULL,
7405 verbose,
7406 &found);
7407 dprintf("found %d count: %d\n", found, count);
7408 } while (found);
7409
7410 dprintf("path: %s total number of volumes: %d\n", hpath, count);
7411
7412 while (devlist) {
7413 struct md_list *dv = devlist;
7414 devlist = devlist->next;
7415 free(dv->devname);
7416 free(dv);
7417 }
7418 }
7419 return count;
7420 }
7421
7422 static int count_volumes(struct intel_hba *hba, int dpa, int verbose)
7423 {
7424 if (!hba)
7425 return 0;
7426 if (hba->type == SYS_DEV_VMD) {
7427 struct sys_dev *dev;
7428 int count = 0;
7429
7430 for (dev = find_intel_devices(); dev; dev = dev->next) {
7431 if (dev->type == SYS_DEV_VMD)
7432 count += __count_volumes(dev->path, dpa,
7433 verbose, 1);
7434 }
7435 return count;
7436 }
7437 return __count_volumes(hba->path, dpa, verbose, 0);
7438 }
7439
7440 static int imsm_default_chunk(const struct imsm_orom *orom)
7441 {
7442 /* up to 512 if the plaform supports it, otherwise the platform max.
7443 * 128 if no platform detected
7444 */
7445 int fs = max(7, orom ? fls(orom->sss) : 0);
7446
7447 return min(512, (1 << fs));
7448 }
7449
7450 static int
7451 validate_geometry_imsm_orom(struct intel_super *super, int level, int layout,
7452 int raiddisks, int *chunk, unsigned long long size, int verbose)
7453 {
7454 /* check/set platform and metadata limits/defaults */
7455 if (super->orom && raiddisks > super->orom->dpa) {
7456 pr_vrb("platform supports a maximum of %d disks per array\n",
7457 super->orom->dpa);
7458 return 0;
7459 }
7460
7461 /* capabilities of OROM tested - copied from validate_geometry_imsm_volume */
7462 if (!is_raid_level_supported(super->orom, level, raiddisks)) {
7463 pr_vrb("platform does not support raid%d with %d disk%s\n",
7464 level, raiddisks, raiddisks > 1 ? "s" : "");
7465 return 0;
7466 }
7467
7468 if (*chunk == 0 || *chunk == UnSet)
7469 *chunk = imsm_default_chunk(super->orom);
7470
7471 if (super->orom && !imsm_orom_has_chunk(super->orom, *chunk)) {
7472 pr_vrb("platform does not support a chunk size of: %d\n", *chunk);
7473 return 0;
7474 }
7475
7476 if (layout != imsm_level_to_layout(level)) {
7477 if (level == 5)
7478 pr_vrb("imsm raid 5 only supports the left-asymmetric layout\n");
7479 else if (level == 10)
7480 pr_vrb("imsm raid 10 only supports the n2 layout\n");
7481 else
7482 pr_vrb("imsm unknown layout %#x for this raid level %d\n",
7483 layout, level);
7484 return 0;
7485 }
7486
7487 if (super->orom && (super->orom->attr & IMSM_OROM_ATTR_2TB) == 0 &&
7488 (calc_array_size(level, raiddisks, layout, *chunk, size) >> 32) > 0) {
7489 pr_vrb("platform does not support a volume size over 2TB\n");
7490 return 0;
7491 }
7492
7493 return 1;
7494 }
7495
7496 /* validate_geometry_imsm_volume - lifted from validate_geometry_ddf_bvd
7497 * FIX ME add ahci details
7498 */
7499 static int validate_geometry_imsm_volume(struct supertype *st, int level,
7500 int layout, int raiddisks, int *chunk,
7501 unsigned long long size,
7502 unsigned long long data_offset,
7503 char *dev,
7504 unsigned long long *freesize,
7505 int verbose)
7506 {
7507 dev_t rdev;
7508 struct intel_super *super = st->sb;
7509 struct imsm_super *mpb;
7510 struct dl *dl;
7511 unsigned long long pos = 0;
7512 unsigned long long maxsize;
7513 struct extent *e;
7514 int i;
7515
7516 /* We must have the container info already read in. */
7517 if (!super)
7518 return 0;
7519
7520 mpb = super->anchor;
7521
7522 if (!validate_geometry_imsm_orom(super, level, layout, raiddisks, chunk, size, verbose)) {
7523 pr_err("RAID geometry validation failed. Cannot proceed with the action(s).\n");
7524 return 0;
7525 }
7526 if (!dev) {
7527 /* General test: make sure there is space for
7528 * 'raiddisks' device extents of size 'size' at a given
7529 * offset
7530 */
7531 unsigned long long minsize = size;
7532 unsigned long long start_offset = MaxSector;
7533 int dcnt = 0;
7534 if (minsize == 0)
7535 minsize = MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS;
7536 for (dl = super->disks; dl ; dl = dl->next) {
7537 int found = 0;
7538
7539 pos = 0;
7540 i = 0;
7541 e = get_extents(super, dl, 0);
7542 if (!e) continue;
7543 do {
7544 unsigned long long esize;
7545 esize = e[i].start - pos;
7546 if (esize >= minsize)
7547 found = 1;
7548 if (found && start_offset == MaxSector) {
7549 start_offset = pos;
7550 break;
7551 } else if (found && pos != start_offset) {
7552 found = 0;
7553 break;
7554 }
7555 pos = e[i].start + e[i].size;
7556 i++;
7557 } while (e[i-1].size);
7558 if (found)
7559 dcnt++;
7560 free(e);
7561 }
7562 if (dcnt < raiddisks) {
7563 if (verbose)
7564 pr_err("imsm: Not enough devices with space for this array (%d < %d)\n",
7565 dcnt, raiddisks);
7566 return 0;
7567 }
7568 return 1;
7569 }
7570
7571 /* This device must be a member of the set */
7572 if (!stat_is_blkdev(dev, &rdev))
7573 return 0;
7574 for (dl = super->disks ; dl ; dl = dl->next) {
7575 if (dl->major == (int)major(rdev) &&
7576 dl->minor == (int)minor(rdev))
7577 break;
7578 }
7579 if (!dl) {
7580 if (verbose)
7581 pr_err("%s is not in the same imsm set\n", dev);
7582 return 0;
7583 } else if (super->orom && dl->index < 0 && mpb->num_raid_devs) {
7584 /* If a volume is present then the current creation attempt
7585 * cannot incorporate new spares because the orom may not
7586 * understand this configuration (all member disks must be
7587 * members of each array in the container).
7588 */
7589 pr_err("%s is a spare and a volume is already defined for this container\n", dev);
7590 pr_err("The option-rom requires all member disks to be a member of all volumes\n");
7591 return 0;
7592 } else if (super->orom && mpb->num_raid_devs > 0 &&
7593 mpb->num_disks != raiddisks) {
7594 pr_err("The option-rom requires all member disks to be a member of all volumes\n");
7595 return 0;
7596 }
7597
7598 /* retrieve the largest free space block */
7599 e = get_extents(super, dl, 0);
7600 maxsize = 0;
7601 i = 0;
7602 if (e) {
7603 do {
7604 unsigned long long esize;
7605
7606 esize = e[i].start - pos;
7607 if (esize >= maxsize)
7608 maxsize = esize;
7609 pos = e[i].start + e[i].size;
7610 i++;
7611 } while (e[i-1].size);
7612 dl->e = e;
7613 dl->extent_cnt = i;
7614 } else {
7615 if (verbose)
7616 pr_err("unable to determine free space for: %s\n",
7617 dev);
7618 return 0;
7619 }
7620 if (maxsize < size) {
7621 if (verbose)
7622 pr_err("%s not enough space (%llu < %llu)\n",
7623 dev, maxsize, size);
7624 return 0;
7625 }
7626
7627 maxsize = merge_extents(super, false);
7628
7629 if (mpb->num_raid_devs > 0 && size && size != maxsize)
7630 pr_err("attempting to create a second volume with size less then remaining space.\n");
7631
7632 if (maxsize < size || maxsize == 0) {
7633 if (verbose) {
7634 if (maxsize == 0)
7635 pr_err("no free space left on device. Aborting...\n");
7636 else
7637 pr_err("not enough space to create volume of given size (%llu < %llu). Aborting...\n",
7638 maxsize, size);
7639 }
7640 return 0;
7641 }
7642
7643 *freesize = maxsize;
7644
7645 if (super->orom) {
7646 int count = count_volumes(super->hba,
7647 super->orom->dpa, verbose);
7648 if (super->orom->vphba <= count) {
7649 pr_vrb("platform does not support more than %d raid volumes.\n",
7650 super->orom->vphba);
7651 return 0;
7652 }
7653 }
7654 return 1;
7655 }
7656
7657 /**
7658 * imsm_get_free_size() - get the biggest, common free space from members.
7659 * @super: &intel_super pointer, not NULL.
7660 * @raiddisks: number of raid disks.
7661 * @size: requested size, could be 0 (means max size).
7662 * @chunk: requested chunk size in KiB.
7663 * @freesize: pointer for returned size value.
7664 *
7665 * Return: &IMSM_STATUS_OK or &IMSM_STATUS_ERROR.
7666 *
7667 * @freesize is set to meaningful value, this can be @size, or calculated
7668 * max free size.
7669 * super->create_offset value is modified and set appropriately in
7670 * merge_extends() for further creation.
7671 */
7672 static imsm_status_t imsm_get_free_size(struct intel_super *super,
7673 const int raiddisks,
7674 unsigned long long size,
7675 const int chunk,
7676 unsigned long long *freesize,
7677 bool expanding)
7678 {
7679 struct imsm_super *mpb = super->anchor;
7680 struct dl *dl;
7681 int i;
7682 struct extent *e;
7683 int cnt = 0;
7684 int used = 0;
7685 unsigned long long maxsize;
7686 unsigned long long minsize = size;
7687
7688 if (minsize == 0)
7689 minsize = chunk * 2;
7690
7691 /* find the largest common start free region of the possible disks */
7692 for (dl = super->disks; dl; dl = dl->next) {
7693 dl->raiddisk = -1;
7694
7695 if (dl->index >= 0)
7696 used++;
7697
7698 /* don't activate new spares if we are orom constrained
7699 * and there is already a volume active in the container
7700 */
7701 if (super->orom && dl->index < 0 && mpb->num_raid_devs)
7702 continue;
7703
7704 e = get_extents(super, dl, 0);
7705 if (!e)
7706 continue;
7707 for (i = 1; e[i-1].size; i++)
7708 ;
7709 dl->e = e;
7710 dl->extent_cnt = i;
7711 cnt++;
7712 }
7713
7714 maxsize = merge_extents(super, expanding);
7715 if (maxsize < minsize) {
7716 pr_err("imsm: Free space is %llu but must be equal or larger than %llu.\n",
7717 maxsize, minsize);
7718 return IMSM_STATUS_ERROR;
7719 }
7720
7721 if (cnt < raiddisks || (super->orom && used && used != raiddisks)) {
7722 pr_err("imsm: Not enough devices with space to create array.\n");
7723 return IMSM_STATUS_ERROR;
7724 }
7725
7726 if (size == 0) {
7727 size = maxsize;
7728 if (chunk) {
7729 size /= 2 * chunk;
7730 size *= 2 * chunk;
7731 }
7732 maxsize = size;
7733 }
7734 if (mpb->num_raid_devs > 0 && size && size != maxsize)
7735 pr_err("attempting to create a second volume with size less then remaining space.\n");
7736 *freesize = size;
7737
7738 dprintf("imsm: imsm_get_free_size() returns : %llu\n", size);
7739
7740 return IMSM_STATUS_OK;
7741 }
7742
7743 /**
7744 * autolayout_imsm() - automatically layout a new volume.
7745 * @super: &intel_super pointer, not NULL.
7746 * @raiddisks: number of raid disks.
7747 * @size: requested size, could be 0 (means max size).
7748 * @chunk: requested chunk.
7749 * @freesize: pointer for returned size value.
7750 *
7751 * We are being asked to automatically layout a new volume based on the current
7752 * contents of the container. If the parameters can be satisfied autolayout_imsm
7753 * will record the disks, start offset, and will return size of the volume to
7754 * be created. See imsm_get_free_size() for details.
7755 * add_to_super() and getinfo_super() detect when autolayout is in progress.
7756 * If first volume exists, slots are set consistently to it.
7757 *
7758 * Return: &IMSM_STATUS_OK on success, &IMSM_STATUS_ERROR otherwise.
7759 *
7760 * Disks are marked for creation via dl->raiddisk.
7761 */
7762 static imsm_status_t autolayout_imsm(struct intel_super *super,
7763 const int raiddisks,
7764 unsigned long long size, const int chunk,
7765 unsigned long long *freesize)
7766 {
7767 int curr_slot = 0;
7768 struct dl *disk;
7769 int vol_cnt = super->anchor->num_raid_devs;
7770 imsm_status_t rv;
7771
7772 rv = imsm_get_free_size(super, raiddisks, size, chunk, freesize, false);
7773 if (rv != IMSM_STATUS_OK)
7774 return IMSM_STATUS_ERROR;
7775
7776 for (disk = super->disks; disk; disk = disk->next) {
7777 if (!disk->e)
7778 continue;
7779
7780 if (curr_slot == raiddisks)
7781 break;
7782
7783 if (vol_cnt == 0) {
7784 disk->raiddisk = curr_slot;
7785 } else {
7786 int _slot = get_disk_slot_in_dev(super, 0, disk->index);
7787
7788 if (_slot == -1) {
7789 pr_err("Disk %s is not used in first volume, aborting\n",
7790 disk->devname);
7791 return IMSM_STATUS_ERROR;
7792 }
7793 disk->raiddisk = _slot;
7794 }
7795 curr_slot++;
7796 }
7797
7798 return IMSM_STATUS_OK;
7799 }
7800
7801 static int validate_geometry_imsm(struct supertype *st, int level, int layout,
7802 int raiddisks, int *chunk, unsigned long long size,
7803 unsigned long long data_offset,
7804 char *dev, unsigned long long *freesize,
7805 int consistency_policy, int verbose)
7806 {
7807 struct intel_super *super = st->sb;
7808 struct mdinfo *sra;
7809 int is_member = 0;
7810 imsm_status_t rv;
7811 int fd, cfd;
7812
7813 /* load capability
7814 * if given unused devices create a container
7815 * if given given devices in a container create a member volume
7816 */
7817 if (is_container(level))
7818 /* Must be a fresh device to add to a container */
7819 return validate_geometry_imsm_container(st, level, raiddisks,
7820 data_offset, dev,
7821 freesize, verbose);
7822
7823 /*
7824 * Size is given in sectors.
7825 */
7826 if (size && (size < 2048)) {
7827 pr_err("Given size must be greater than 1M.\n");
7828 /* Depends on algorithm in Create.c :
7829 * if container was given (dev == NULL) return -1,
7830 * if block device was given ( dev != NULL) return 0.
7831 */
7832 return dev ? -1 : 0;
7833 }
7834
7835 if (!dev) {
7836 /*
7837 * Autolayout mode, st->sb must be set.
7838 */
7839
7840 if (!super) {
7841 pr_vrb("superblock must be set for autolayout, aborting\n");
7842 return 0;
7843 }
7844
7845 if (!validate_geometry_imsm_orom(st->sb, level, layout,
7846 raiddisks, chunk, size,
7847 verbose))
7848 return 0;
7849
7850 if (super->orom) {
7851 int count = count_volumes(super->hba, super->orom->dpa, verbose);
7852
7853 if (super->orom->vphba <= count) {
7854 pr_vrb("platform does not support more than %d raid volumes.\n",
7855 super->orom->vphba);
7856 return 0;
7857 }
7858 }
7859
7860 if (freesize) {
7861 rv = autolayout_imsm(super, raiddisks, size, *chunk, freesize);
7862 if (rv != IMSM_STATUS_OK)
7863 return 0;
7864 }
7865
7866 return 1;
7867 }
7868 if (st->sb) {
7869 /* creating in a given container */
7870 return validate_geometry_imsm_volume(st, level, layout,
7871 raiddisks, chunk, size,
7872 data_offset,
7873 dev, freesize, verbose);
7874 }
7875
7876 /* This device needs to be a device in an 'imsm' container */
7877 fd = open(dev, O_RDONLY|O_EXCL, 0);
7878
7879 if (is_fd_valid(fd)) {
7880 pr_vrb("Cannot create this array on device %s\n", dev);
7881 close(fd);
7882 return 0;
7883 }
7884 if (errno == EBUSY)
7885 fd = open(dev, O_RDONLY, 0);
7886
7887 if (!is_fd_valid(fd)) {
7888 pr_vrb("Cannot open %s: %s\n", dev, strerror(errno));
7889 return 0;
7890 }
7891
7892 /* Well, it is in use by someone, maybe an 'imsm' container. */
7893 cfd = open_container(fd);
7894 close_fd(&fd);
7895
7896 if (!is_fd_valid(cfd)) {
7897 pr_vrb("Cannot use %s: It is busy\n", dev);
7898 return 0;
7899 }
7900 sra = sysfs_read(cfd, NULL, GET_VERSION);
7901 if (sra && sra->array.major_version == -1 &&
7902 strcmp(sra->text_version, "imsm") == 0)
7903 is_member = 1;
7904 sysfs_free(sra);
7905 if (is_member) {
7906 /* This is a member of a imsm container. Load the container
7907 * and try to create a volume
7908 */
7909 struct intel_super *super;
7910
7911 if (load_super_imsm_all(st, cfd, (void **) &super, NULL, NULL, 1) == 0) {
7912 st->sb = super;
7913 strcpy(st->container_devnm, fd2devnm(cfd));
7914 close(cfd);
7915 return validate_geometry_imsm_volume(st, level, layout,
7916 raiddisks, chunk,
7917 size, data_offset, dev,
7918 freesize, 1)
7919 ? 1 : -1;
7920 }
7921 }
7922
7923 if (verbose)
7924 pr_err("failed container membership check\n");
7925
7926 close(cfd);
7927 return 0;
7928 }
7929
7930 static void default_geometry_imsm(struct supertype *st, int *level, int *layout, int *chunk)
7931 {
7932 struct intel_super *super = st->sb;
7933
7934 if (level && *level == UnSet)
7935 *level = LEVEL_CONTAINER;
7936
7937 if (level && layout && *layout == UnSet)
7938 *layout = imsm_level_to_layout(*level);
7939
7940 if (chunk && (*chunk == UnSet || *chunk == 0))
7941 *chunk = imsm_default_chunk(super->orom);
7942 }
7943
7944 static void handle_missing(struct intel_super *super, struct imsm_dev *dev);
7945
7946 static int kill_subarray_imsm(struct supertype *st, char *subarray_id)
7947 {
7948 /* remove the subarray currently referenced by subarray_id */
7949 __u8 i;
7950 struct intel_dev **dp;
7951 struct intel_super *super = st->sb;
7952 __u8 current_vol = strtoul(subarray_id, NULL, 10);
7953 struct imsm_super *mpb = super->anchor;
7954
7955 if (mpb->num_raid_devs == 0)
7956 return 2;
7957
7958 /* block deletions that would change the uuid of active subarrays
7959 *
7960 * FIXME when immutable ids are available, but note that we'll
7961 * also need to fixup the invalidated/active subarray indexes in
7962 * mdstat
7963 */
7964 for (i = 0; i < mpb->num_raid_devs; i++) {
7965 char subarray[4];
7966
7967 if (i < current_vol)
7968 continue;
7969 snprintf(subarray, sizeof(subarray), "%u", i);
7970 if (is_subarray_active(subarray, st->devnm)) {
7971 pr_err("deleting subarray-%d would change the UUID of active subarray-%d, aborting\n",
7972 current_vol, i);
7973
7974 return 2;
7975 }
7976 }
7977
7978 if (st->update_tail) {
7979 struct imsm_update_kill_array *u = xmalloc(sizeof(*u));
7980
7981 u->type = update_kill_array;
7982 u->dev_idx = current_vol;
7983 append_metadata_update(st, u, sizeof(*u));
7984
7985 return 0;
7986 }
7987
7988 for (dp = &super->devlist; *dp;)
7989 if ((*dp)->index == current_vol) {
7990 *dp = (*dp)->next;
7991 } else {
7992 handle_missing(super, (*dp)->dev);
7993 if ((*dp)->index > current_vol)
7994 (*dp)->index--;
7995 dp = &(*dp)->next;
7996 }
7997
7998 /* no more raid devices, all active components are now spares,
7999 * but of course failed are still failed
8000 */
8001 if (--mpb->num_raid_devs == 0) {
8002 struct dl *d;
8003
8004 for (d = super->disks; d; d = d->next)
8005 if (d->index > -2)
8006 mark_spare(d);
8007 }
8008
8009 super->updates_pending++;
8010
8011 return 0;
8012 }
8013
8014 /**
8015 * get_rwh_policy_from_update() - Get the rwh policy for update option.
8016 * @update: Update option.
8017 */
8018 static int get_rwh_policy_from_update(enum update_opt update)
8019 {
8020 switch (update) {
8021 case UOPT_PPL:
8022 return RWH_MULTIPLE_DISTRIBUTED;
8023 case UOPT_NO_PPL:
8024 return RWH_MULTIPLE_OFF;
8025 case UOPT_BITMAP:
8026 return RWH_BITMAP;
8027 case UOPT_NO_BITMAP:
8028 return RWH_OFF;
8029 default:
8030 break;
8031 }
8032 return UOPT_UNDEFINED;
8033 }
8034
8035 static int update_subarray_imsm(struct supertype *st, char *subarray,
8036 enum update_opt update, struct mddev_ident *ident)
8037 {
8038 /* update the subarray currently referenced by ->current_vol */
8039 struct intel_super *super = st->sb;
8040 struct imsm_super *mpb = super->anchor;
8041
8042 if (update == UOPT_NAME) {
8043 char *name = ident->name;
8044 char *ep;
8045 int vol;
8046
8047 if (imsm_is_name_allowed(super, name, 1) == false)
8048 return 2;
8049
8050 vol = strtoul(subarray, &ep, 10);
8051 if (*ep != '\0' || vol >= super->anchor->num_raid_devs)
8052 return 2;
8053
8054 if (st->update_tail) {
8055 struct imsm_update_rename_array *u = xmalloc(sizeof(*u));
8056
8057 u->type = update_rename_array;
8058 u->dev_idx = vol;
8059 strncpy((char *) u->name, name, MAX_RAID_SERIAL_LEN);
8060 u->name[MAX_RAID_SERIAL_LEN-1] = '\0';
8061 append_metadata_update(st, u, sizeof(*u));
8062 } else {
8063 struct imsm_dev *dev;
8064 int i, namelen;
8065
8066 dev = get_imsm_dev(super, vol);
8067 memset(dev->volume, '\0', MAX_RAID_SERIAL_LEN);
8068 namelen = min((int)strlen(name), MAX_RAID_SERIAL_LEN);
8069 memcpy(dev->volume, name, namelen);
8070 for (i = 0; i < mpb->num_raid_devs; i++) {
8071 dev = get_imsm_dev(super, i);
8072 handle_missing(super, dev);
8073 }
8074 super->updates_pending++;
8075 }
8076 } else if (get_rwh_policy_from_update(update) != UOPT_UNDEFINED) {
8077 int new_policy;
8078 char *ep;
8079 int vol = strtoul(subarray, &ep, 10);
8080
8081 if (*ep != '\0' || vol >= super->anchor->num_raid_devs)
8082 return 2;
8083
8084 new_policy = get_rwh_policy_from_update(update);
8085
8086 if (st->update_tail) {
8087 struct imsm_update_rwh_policy *u = xmalloc(sizeof(*u));
8088
8089 u->type = update_rwh_policy;
8090 u->dev_idx = vol;
8091 u->new_policy = new_policy;
8092 append_metadata_update(st, u, sizeof(*u));
8093 } else {
8094 struct imsm_dev *dev;
8095
8096 dev = get_imsm_dev(super, vol);
8097 dev->rwh_policy = new_policy;
8098 super->updates_pending++;
8099 }
8100 if (new_policy == RWH_BITMAP)
8101 return write_init_bitmap_imsm_vol(st, vol);
8102 } else
8103 return 2;
8104
8105 return 0;
8106 }
8107
8108 static bool is_gen_migration(struct imsm_dev *dev)
8109 {
8110 if (dev && dev->vol.migr_state &&
8111 migr_type(dev) == MIGR_GEN_MIGR)
8112 return true;
8113
8114 return false;
8115 }
8116
8117 static int is_rebuilding(struct imsm_dev *dev)
8118 {
8119 struct imsm_map *migr_map;
8120
8121 if (!dev->vol.migr_state)
8122 return 0;
8123
8124 if (migr_type(dev) != MIGR_REBUILD)
8125 return 0;
8126
8127 migr_map = get_imsm_map(dev, MAP_1);
8128
8129 if (migr_map->map_state == IMSM_T_STATE_DEGRADED)
8130 return 1;
8131 else
8132 return 0;
8133 }
8134
8135 static int is_initializing(struct imsm_dev *dev)
8136 {
8137 struct imsm_map *migr_map;
8138
8139 if (!dev->vol.migr_state)
8140 return 0;
8141
8142 if (migr_type(dev) != MIGR_INIT)
8143 return 0;
8144
8145 migr_map = get_imsm_map(dev, MAP_1);
8146
8147 if (migr_map->map_state == IMSM_T_STATE_UNINITIALIZED)
8148 return 1;
8149
8150 return 0;
8151 }
8152
8153 static void update_recovery_start(struct intel_super *super,
8154 struct imsm_dev *dev,
8155 struct mdinfo *array)
8156 {
8157 struct mdinfo *rebuild = NULL;
8158 struct mdinfo *d;
8159 __u32 units;
8160
8161 if (!is_rebuilding(dev))
8162 return;
8163
8164 /* Find the rebuild target, but punt on the dual rebuild case */
8165 for (d = array->devs; d; d = d->next)
8166 if (d->recovery_start == 0) {
8167 if (rebuild)
8168 return;
8169 rebuild = d;
8170 }
8171
8172 if (!rebuild) {
8173 /* (?) none of the disks are marked with
8174 * IMSM_ORD_REBUILD, so assume they are missing and the
8175 * disk_ord_tbl was not correctly updated
8176 */
8177 dprintf("failed to locate out-of-sync disk\n");
8178 return;
8179 }
8180
8181 units = vol_curr_migr_unit(dev);
8182 rebuild->recovery_start = units * blocks_per_migr_unit(super, dev);
8183 }
8184
8185 static int recover_backup_imsm(struct supertype *st, struct mdinfo *info);
8186
8187 static struct mdinfo *container_content_imsm(struct supertype *st, char *subarray)
8188 {
8189 /* Given a container loaded by load_super_imsm_all,
8190 * extract information about all the arrays into
8191 * an mdinfo tree.
8192 * If 'subarray' is given, just extract info about that array.
8193 *
8194 * For each imsm_dev create an mdinfo, fill it in,
8195 * then look for matching devices in super->disks
8196 * and create appropriate device mdinfo.
8197 */
8198 struct intel_super *super = st->sb;
8199 struct imsm_super *mpb = super->anchor;
8200 struct mdinfo *rest = NULL;
8201 unsigned int i;
8202 int sb_errors = 0;
8203 struct dl *d;
8204 int spare_disks = 0;
8205 int current_vol = super->current_vol;
8206
8207 /* do not assemble arrays when not all attributes are supported */
8208 if (imsm_check_attributes(mpb->attributes) == false) {
8209 sb_errors = 1;
8210 pr_err("Unsupported attributes in IMSM metadata. Arrays activation is blocked.\n");
8211 }
8212
8213 /* count spare devices, not used in maps
8214 */
8215 for (d = super->disks; d; d = d->next)
8216 if (d->index == -1)
8217 spare_disks++;
8218
8219 for (i = 0; i < mpb->num_raid_devs; i++) {
8220 struct imsm_dev *dev;
8221 struct imsm_map *map;
8222 struct imsm_map *map2;
8223 struct mdinfo *this;
8224 int slot;
8225 int chunk;
8226 char *ep;
8227 int level;
8228
8229 if (subarray &&
8230 (i != strtoul(subarray, &ep, 10) || *ep != '\0'))
8231 continue;
8232
8233 dev = get_imsm_dev(super, i);
8234 map = get_imsm_map(dev, MAP_0);
8235 map2 = get_imsm_map(dev, MAP_1);
8236 level = get_imsm_raid_level(map);
8237
8238 /* do not publish arrays that are in the middle of an
8239 * unsupported migration
8240 */
8241 if (dev->vol.migr_state &&
8242 (migr_type(dev) == MIGR_STATE_CHANGE)) {
8243 pr_err("cannot assemble volume '%.16s': unsupported migration in progress\n",
8244 dev->volume);
8245 continue;
8246 }
8247 /* do not publish arrays that are not support by controller's
8248 * OROM/EFI
8249 */
8250
8251 this = xmalloc(sizeof(*this));
8252
8253 super->current_vol = i;
8254 getinfo_super_imsm_volume(st, this, NULL);
8255 this->next = rest;
8256 chunk = __le16_to_cpu(map->blocks_per_strip) >> 1;
8257 /* mdadm does not support all metadata features- set the bit in all arrays state */
8258 if (!validate_geometry_imsm_orom(super,
8259 level, /* RAID level */
8260 imsm_level_to_layout(level),
8261 map->num_members, /* raid disks */
8262 &chunk, imsm_dev_size(dev),
8263 1 /* verbose */)) {
8264 pr_err("IMSM RAID geometry validation failed. Array %s activation is blocked.\n",
8265 dev->volume);
8266 this->array.state |=
8267 (1<<MD_SB_BLOCK_CONTAINER_RESHAPE) |
8268 (1<<MD_SB_BLOCK_VOLUME);
8269 }
8270
8271 /* if array has bad blocks, set suitable bit in all arrays state */
8272 if (sb_errors)
8273 this->array.state |=
8274 (1<<MD_SB_BLOCK_CONTAINER_RESHAPE) |
8275 (1<<MD_SB_BLOCK_VOLUME);
8276
8277 for (slot = 0 ; slot < map->num_members; slot++) {
8278 unsigned long long recovery_start;
8279 struct mdinfo *info_d;
8280 struct dl *d;
8281 int idx;
8282 int skip;
8283 __u32 ord;
8284 int missing = 0;
8285
8286 skip = 0;
8287 idx = get_imsm_disk_idx(dev, slot, MAP_0);
8288 ord = get_imsm_ord_tbl_ent(dev, slot, MAP_X);
8289 for (d = super->disks; d ; d = d->next)
8290 if (d->index == idx)
8291 break;
8292
8293 recovery_start = MaxSector;
8294 if (d == NULL)
8295 skip = 1;
8296 if (d && is_failed(&d->disk))
8297 skip = 1;
8298 if (!skip && (ord & IMSM_ORD_REBUILD))
8299 recovery_start = 0;
8300 if (!(ord & IMSM_ORD_REBUILD))
8301 this->array.working_disks++;
8302 /*
8303 * if we skip some disks the array will be assmebled degraded;
8304 * reset resync start to avoid a dirty-degraded
8305 * situation when performing the intial sync
8306 */
8307 if (skip)
8308 missing++;
8309
8310 if (!(dev->vol.dirty & RAIDVOL_DIRTY)) {
8311 if ((!able_to_resync(level, missing) ||
8312 recovery_start == 0))
8313 this->resync_start = MaxSector;
8314 }
8315
8316 if (skip)
8317 continue;
8318
8319 info_d = xcalloc(1, sizeof(*info_d));
8320 info_d->next = this->devs;
8321 this->devs = info_d;
8322
8323 info_d->disk.number = d->index;
8324 info_d->disk.major = d->major;
8325 info_d->disk.minor = d->minor;
8326 info_d->disk.raid_disk = slot;
8327 info_d->recovery_start = recovery_start;
8328 if (map2) {
8329 if (slot < map2->num_members)
8330 info_d->disk.state = (1 << MD_DISK_ACTIVE);
8331 else
8332 this->array.spare_disks++;
8333 } else {
8334 if (slot < map->num_members)
8335 info_d->disk.state = (1 << MD_DISK_ACTIVE);
8336 else
8337 this->array.spare_disks++;
8338 }
8339
8340 info_d->events = __le32_to_cpu(mpb->generation_num);
8341 info_d->data_offset = pba_of_lba0(map);
8342 info_d->component_size = calc_component_size(map, dev);
8343
8344 if (map->raid_level == IMSM_T_RAID5) {
8345 info_d->ppl_sector = this->ppl_sector;
8346 info_d->ppl_size = this->ppl_size;
8347 if (this->consistency_policy == CONSISTENCY_POLICY_PPL &&
8348 recovery_start == 0)
8349 this->resync_start = 0;
8350 }
8351
8352 info_d->bb.supported = 1;
8353 get_volume_badblocks(super->bbm_log, ord_to_idx(ord),
8354 info_d->data_offset,
8355 info_d->component_size,
8356 &info_d->bb);
8357 }
8358 /* now that the disk list is up-to-date fixup recovery_start */
8359 update_recovery_start(super, dev, this);
8360 this->array.spare_disks += spare_disks;
8361
8362 /* check for reshape */
8363 if (this->reshape_active == 1)
8364 recover_backup_imsm(st, this);
8365 rest = this;
8366 }
8367
8368 super->current_vol = current_vol;
8369 return rest;
8370 }
8371
8372 static __u8 imsm_check_degraded(struct intel_super *super, struct imsm_dev *dev,
8373 int failed, int look_in_map)
8374 {
8375 struct imsm_map *map;
8376
8377 map = get_imsm_map(dev, look_in_map);
8378
8379 if (!failed)
8380 return map->map_state == IMSM_T_STATE_UNINITIALIZED ?
8381 IMSM_T_STATE_UNINITIALIZED : IMSM_T_STATE_NORMAL;
8382
8383 switch (get_imsm_raid_level(map)) {
8384 case 0:
8385 return IMSM_T_STATE_FAILED;
8386 break;
8387 case 1:
8388 if (failed < map->num_members)
8389 return IMSM_T_STATE_DEGRADED;
8390 else
8391 return IMSM_T_STATE_FAILED;
8392 break;
8393 case 10:
8394 {
8395 /**
8396 * check to see if any mirrors have failed, otherwise we
8397 * are degraded. Even numbered slots are mirrored on
8398 * slot+1
8399 */
8400 int i;
8401 /* gcc -Os complains that this is unused */
8402 int insync = insync;
8403
8404 for (i = 0; i < map->num_members; i++) {
8405 __u32 ord = get_imsm_ord_tbl_ent(dev, i, MAP_X);
8406 int idx = ord_to_idx(ord);
8407 struct imsm_disk *disk;
8408
8409 /* reset the potential in-sync count on even-numbered
8410 * slots. num_copies is always 2 for imsm raid10
8411 */
8412 if ((i & 1) == 0)
8413 insync = 2;
8414
8415 disk = get_imsm_disk(super, idx);
8416 if (!disk || is_failed(disk) || ord & IMSM_ORD_REBUILD)
8417 insync--;
8418
8419 /* no in-sync disks left in this mirror the
8420 * array has failed
8421 */
8422 if (insync == 0)
8423 return IMSM_T_STATE_FAILED;
8424 }
8425
8426 return IMSM_T_STATE_DEGRADED;
8427 }
8428 case 5:
8429 if (failed < 2)
8430 return IMSM_T_STATE_DEGRADED;
8431 else
8432 return IMSM_T_STATE_FAILED;
8433 break;
8434 default:
8435 break;
8436 }
8437
8438 return map->map_state;
8439 }
8440
8441 static int imsm_count_failed(struct intel_super *super, struct imsm_dev *dev,
8442 int look_in_map)
8443 {
8444 int i;
8445 int failed = 0;
8446 struct imsm_disk *disk;
8447 struct imsm_map *map = get_imsm_map(dev, MAP_0);
8448 struct imsm_map *prev = get_imsm_map(dev, MAP_1);
8449 struct imsm_map *map_for_loop;
8450 __u32 ord;
8451 int idx;
8452 int idx_1;
8453
8454 /* at the beginning of migration we set IMSM_ORD_REBUILD on
8455 * disks that are being rebuilt. New failures are recorded to
8456 * map[0]. So we look through all the disks we started with and
8457 * see if any failures are still present, or if any new ones
8458 * have arrived
8459 */
8460 map_for_loop = map;
8461 if (prev && (map->num_members < prev->num_members))
8462 map_for_loop = prev;
8463
8464 for (i = 0; i < map_for_loop->num_members; i++) {
8465 idx_1 = -255;
8466 /* when MAP_X is passed both maps failures are counted
8467 */
8468 if (prev &&
8469 (look_in_map == MAP_1 || look_in_map == MAP_X) &&
8470 i < prev->num_members) {
8471 ord = __le32_to_cpu(prev->disk_ord_tbl[i]);
8472 idx_1 = ord_to_idx(ord);
8473
8474 disk = get_imsm_disk(super, idx_1);
8475 if (!disk || is_failed(disk) || ord & IMSM_ORD_REBUILD)
8476 failed++;
8477 }
8478 if ((look_in_map == MAP_0 || look_in_map == MAP_X) &&
8479 i < map->num_members) {
8480 ord = __le32_to_cpu(map->disk_ord_tbl[i]);
8481 idx = ord_to_idx(ord);
8482
8483 if (idx != idx_1) {
8484 disk = get_imsm_disk(super, idx);
8485 if (!disk || is_failed(disk) ||
8486 ord & IMSM_ORD_REBUILD)
8487 failed++;
8488 }
8489 }
8490 }
8491
8492 return failed;
8493 }
8494
8495 static int imsm_prepare_update(struct supertype *st,
8496 struct metadata_update *update);
8497 static int imsm_open_new(struct supertype *c, struct active_array *a,
8498 int inst)
8499 {
8500 struct intel_super *super = c->sb;
8501 struct imsm_super *mpb = super->anchor;
8502 struct imsm_update_prealloc_bb_mem *u;
8503 struct metadata_update mu;
8504
8505 if (inst >= mpb->num_raid_devs) {
8506 pr_err("subarry index %d, out of range\n", inst);
8507 return -ENODEV;
8508 }
8509
8510 dprintf("imsm: open_new %d\n", inst);
8511 a->info.container_member = inst;
8512
8513 u = xmalloc(sizeof(*u));
8514 u->type = update_prealloc_badblocks_mem;
8515 mu.len = sizeof(*u);
8516 mu.buf = (char *)u;
8517 imsm_prepare_update(c, &mu);
8518 if (c->update_tail)
8519 append_metadata_update(c, u, sizeof(*u));
8520
8521 return 0;
8522 }
8523
8524 static int is_resyncing(struct imsm_dev *dev)
8525 {
8526 struct imsm_map *migr_map;
8527
8528 if (!dev->vol.migr_state)
8529 return 0;
8530
8531 if (migr_type(dev) == MIGR_INIT ||
8532 migr_type(dev) == MIGR_REPAIR)
8533 return 1;
8534
8535 if (migr_type(dev) == MIGR_GEN_MIGR)
8536 return 0;
8537
8538 migr_map = get_imsm_map(dev, MAP_1);
8539
8540 if (migr_map->map_state == IMSM_T_STATE_NORMAL &&
8541 dev->vol.migr_type != MIGR_GEN_MIGR)
8542 return 1;
8543 else
8544 return 0;
8545 }
8546
8547 /* return true if we recorded new information */
8548 static int mark_failure(struct intel_super *super,
8549 struct imsm_dev *dev, struct imsm_disk *disk, int idx)
8550 {
8551 __u32 ord;
8552 int slot;
8553 struct imsm_map *map;
8554 char buf[MAX_RAID_SERIAL_LEN+3];
8555 unsigned int len, shift = 0;
8556
8557 /* new failures are always set in map[0] */
8558 map = get_imsm_map(dev, MAP_0);
8559
8560 slot = get_imsm_disk_slot(map, idx);
8561 if (slot < 0)
8562 return 0;
8563
8564 ord = __le32_to_cpu(map->disk_ord_tbl[slot]);
8565 if (is_failed(disk) && (ord & IMSM_ORD_REBUILD))
8566 return 0;
8567
8568 memcpy(buf, disk->serial, MAX_RAID_SERIAL_LEN);
8569 buf[MAX_RAID_SERIAL_LEN] = '\000';
8570 strcat(buf, ":0");
8571 if ((len = strlen(buf)) >= MAX_RAID_SERIAL_LEN)
8572 shift = len - MAX_RAID_SERIAL_LEN + 1;
8573 memcpy(disk->serial, &buf[shift], len + 1 - shift);
8574
8575 disk->status |= FAILED_DISK;
8576 set_imsm_ord_tbl_ent(map, slot, idx | IMSM_ORD_REBUILD);
8577 /* mark failures in second map if second map exists and this disk
8578 * in this slot.
8579 * This is valid for migration, initialization and rebuild
8580 */
8581 if (dev->vol.migr_state) {
8582 struct imsm_map *map2 = get_imsm_map(dev, MAP_1);
8583 int slot2 = get_imsm_disk_slot(map2, idx);
8584
8585 if (slot2 < map2->num_members && slot2 >= 0)
8586 set_imsm_ord_tbl_ent(map2, slot2,
8587 idx | IMSM_ORD_REBUILD);
8588 }
8589 if (map->failed_disk_num == 0xff ||
8590 (!is_rebuilding(dev) && map->failed_disk_num > slot))
8591 map->failed_disk_num = slot;
8592
8593 clear_disk_badblocks(super->bbm_log, ord_to_idx(ord));
8594
8595 return 1;
8596 }
8597
8598 static void mark_missing(struct intel_super *super,
8599 struct imsm_dev *dev, struct imsm_disk *disk, int idx)
8600 {
8601 mark_failure(super, dev, disk, idx);
8602
8603 if (disk->scsi_id == __cpu_to_le32(~(__u32)0))
8604 return;
8605
8606 disk->scsi_id = __cpu_to_le32(~(__u32)0);
8607 memmove(&disk->serial[0], &disk->serial[1], MAX_RAID_SERIAL_LEN - 1);
8608 }
8609
8610 static void handle_missing(struct intel_super *super, struct imsm_dev *dev)
8611 {
8612 struct dl *dl;
8613
8614 if (!super->missing)
8615 return;
8616
8617 /* When orom adds replacement for missing disk it does
8618 * not remove entry of missing disk, but just updates map with
8619 * new added disk. So it is not enough just to test if there is
8620 * any missing disk, we have to look if there are any failed disks
8621 * in map to stop migration */
8622
8623 dprintf("imsm: mark missing\n");
8624 /* end process for initialization and rebuild only
8625 */
8626 if (is_gen_migration(dev) == false) {
8627 int failed = imsm_count_failed(super, dev, MAP_0);
8628
8629 if (failed) {
8630 __u8 map_state;
8631 struct imsm_map *map = get_imsm_map(dev, MAP_0);
8632 struct imsm_map *map1;
8633 int i, ord, ord_map1;
8634 int rebuilt = 1;
8635
8636 for (i = 0; i < map->num_members; i++) {
8637 ord = get_imsm_ord_tbl_ent(dev, i, MAP_0);
8638 if (!(ord & IMSM_ORD_REBUILD))
8639 continue;
8640
8641 map1 = get_imsm_map(dev, MAP_1);
8642 if (!map1)
8643 continue;
8644
8645 ord_map1 = __le32_to_cpu(map1->disk_ord_tbl[i]);
8646 if (ord_map1 & IMSM_ORD_REBUILD)
8647 rebuilt = 0;
8648 }
8649
8650 if (rebuilt) {
8651 map_state = imsm_check_degraded(super, dev,
8652 failed, MAP_0);
8653 end_migration(dev, super, map_state);
8654 }
8655 }
8656 }
8657 for (dl = super->missing; dl; dl = dl->next)
8658 mark_missing(super, dev, &dl->disk, dl->index);
8659 super->updates_pending++;
8660 }
8661
8662 static unsigned long long imsm_set_array_size(struct imsm_dev *dev,
8663 long long new_size)
8664 {
8665 unsigned long long array_blocks;
8666 struct imsm_map *map = get_imsm_map(dev, MAP_0);
8667 int used_disks = imsm_num_data_members(map);
8668
8669 if (used_disks == 0) {
8670 /* when problems occures
8671 * return current array_blocks value
8672 */
8673 array_blocks = imsm_dev_size(dev);
8674
8675 return array_blocks;
8676 }
8677
8678 /* set array size in metadata
8679 */
8680 if (new_size <= 0)
8681 /* OLCE size change is caused by added disks
8682 */
8683 array_blocks = per_dev_array_size(map) * used_disks;
8684 else
8685 /* Online Volume Size Change
8686 * Using available free space
8687 */
8688 array_blocks = new_size;
8689
8690 array_blocks = round_size_to_mb(array_blocks, used_disks);
8691 set_imsm_dev_size(dev, array_blocks);
8692
8693 return array_blocks;
8694 }
8695
8696 static void imsm_set_disk(struct active_array *a, int n, int state);
8697
8698 static void imsm_progress_container_reshape(struct intel_super *super)
8699 {
8700 /* if no device has a migr_state, but some device has a
8701 * different number of members than the previous device, start
8702 * changing the number of devices in this device to match
8703 * previous.
8704 */
8705 struct imsm_super *mpb = super->anchor;
8706 int prev_disks = -1;
8707 int i;
8708 int copy_map_size;
8709
8710 for (i = 0; i < mpb->num_raid_devs; i++) {
8711 struct imsm_dev *dev = get_imsm_dev(super, i);
8712 struct imsm_map *map = get_imsm_map(dev, MAP_0);
8713 struct imsm_map *map2;
8714 int prev_num_members;
8715
8716 if (dev->vol.migr_state)
8717 return;
8718
8719 if (prev_disks == -1)
8720 prev_disks = map->num_members;
8721 if (prev_disks == map->num_members)
8722 continue;
8723
8724 /* OK, this array needs to enter reshape mode.
8725 * i.e it needs a migr_state
8726 */
8727
8728 copy_map_size = sizeof_imsm_map(map);
8729 prev_num_members = map->num_members;
8730 map->num_members = prev_disks;
8731 dev->vol.migr_state = MIGR_STATE_MIGRATING;
8732 set_vol_curr_migr_unit(dev, 0);
8733 set_migr_type(dev, MIGR_GEN_MIGR);
8734 for (i = prev_num_members;
8735 i < map->num_members; i++)
8736 set_imsm_ord_tbl_ent(map, i, i);
8737 map2 = get_imsm_map(dev, MAP_1);
8738 /* Copy the current map */
8739 memcpy(map2, map, copy_map_size);
8740 map2->num_members = prev_num_members;
8741
8742 imsm_set_array_size(dev, -1);
8743 super->clean_migration_record_by_mdmon = 1;
8744 super->updates_pending++;
8745 }
8746 }
8747
8748 /* Handle dirty -> clean transititions, resync and reshape. Degraded and rebuild
8749 * states are handled in imsm_set_disk() with one exception, when a
8750 * resync is stopped due to a new failure this routine will set the
8751 * 'degraded' state for the array.
8752 */
8753 static int imsm_set_array_state(struct active_array *a, int consistent)
8754 {
8755 int inst = a->info.container_member;
8756 struct intel_super *super = a->container->sb;
8757 struct imsm_dev *dev = get_imsm_dev(super, inst);
8758 struct imsm_map *map = get_imsm_map(dev, MAP_0);
8759 int failed = imsm_count_failed(super, dev, MAP_0);
8760 __u8 map_state = imsm_check_degraded(super, dev, failed, MAP_0);
8761 __u32 blocks_per_unit;
8762
8763 if (dev->vol.migr_state &&
8764 dev->vol.migr_type == MIGR_GEN_MIGR) {
8765 /* array state change is blocked due to reshape action
8766 * We might need to
8767 * - abort the reshape (if last_checkpoint is 0 and action!= reshape)
8768 * - finish the reshape (if last_checkpoint is big and action != reshape)
8769 * - update vol_curr_migr_unit
8770 */
8771 if (a->curr_action == reshape) {
8772 /* still reshaping, maybe update vol_curr_migr_unit */
8773 goto mark_checkpoint;
8774 } else {
8775 if (a->last_checkpoint >= a->info.component_size) {
8776 unsigned long long array_blocks;
8777 int used_disks;
8778 struct mdinfo *mdi;
8779
8780 used_disks = imsm_num_data_members(map);
8781 if (used_disks > 0) {
8782 array_blocks =
8783 per_dev_array_size(map) *
8784 used_disks;
8785 array_blocks =
8786 round_size_to_mb(array_blocks,
8787 used_disks);
8788 a->info.custom_array_size = array_blocks;
8789 /* encourage manager to update array
8790 * size
8791 */
8792
8793 a->check_reshape = 1;
8794 }
8795 /* finalize online capacity expansion/reshape */
8796 for (mdi = a->info.devs; mdi; mdi = mdi->next)
8797 imsm_set_disk(a,
8798 mdi->disk.raid_disk,
8799 mdi->curr_state);
8800
8801 imsm_progress_container_reshape(super);
8802 }
8803 }
8804 }
8805
8806 /* before we activate this array handle any missing disks */
8807 if (consistent == 2)
8808 handle_missing(super, dev);
8809
8810 if (consistent == 2 &&
8811 (!is_resync_complete(&a->info) ||
8812 map_state != IMSM_T_STATE_NORMAL ||
8813 dev->vol.migr_state))
8814 consistent = 0;
8815
8816 if (is_resync_complete(&a->info)) {
8817 /* complete intialization / resync,
8818 * recovery and interrupted recovery is completed in
8819 * ->set_disk
8820 */
8821 if (is_resyncing(dev)) {
8822 dprintf("imsm: mark resync done\n");
8823 end_migration(dev, super, map_state);
8824 super->updates_pending++;
8825 a->last_checkpoint = 0;
8826 }
8827 } else if ((!is_resyncing(dev) && !failed) &&
8828 (imsm_reshape_blocks_arrays_changes(super) == 0)) {
8829 /* mark the start of the init process if nothing is failed */
8830 dprintf("imsm: mark resync start\n");
8831 if (map->map_state == IMSM_T_STATE_UNINITIALIZED)
8832 migrate(dev, super, IMSM_T_STATE_NORMAL, MIGR_INIT);
8833 else
8834 migrate(dev, super, IMSM_T_STATE_NORMAL, MIGR_REPAIR);
8835 super->updates_pending++;
8836 }
8837
8838 if (a->prev_action == idle)
8839 goto skip_mark_checkpoint;
8840
8841 mark_checkpoint:
8842 /* skip checkpointing for general migration,
8843 * it is controlled in mdadm
8844 */
8845 if (is_gen_migration(dev))
8846 goto skip_mark_checkpoint;
8847
8848 /* check if we can update vol_curr_migr_unit from resync_start,
8849 * recovery_start
8850 */
8851 blocks_per_unit = blocks_per_migr_unit(super, dev);
8852 if (blocks_per_unit) {
8853 set_vol_curr_migr_unit(dev,
8854 a->last_checkpoint / blocks_per_unit);
8855 dprintf("imsm: mark checkpoint (%llu)\n",
8856 vol_curr_migr_unit(dev));
8857 super->updates_pending++;
8858 }
8859
8860 skip_mark_checkpoint:
8861 /* mark dirty / clean */
8862 if (((dev->vol.dirty & RAIDVOL_DIRTY) && consistent) ||
8863 (!(dev->vol.dirty & RAIDVOL_DIRTY) && !consistent)) {
8864 dprintf("imsm: mark '%s'\n", consistent ? "clean" : "dirty");
8865 if (consistent) {
8866 dev->vol.dirty = RAIDVOL_CLEAN;
8867 } else {
8868 dev->vol.dirty = RAIDVOL_DIRTY;
8869 if (dev->rwh_policy == RWH_DISTRIBUTED ||
8870 dev->rwh_policy == RWH_MULTIPLE_DISTRIBUTED)
8871 dev->vol.dirty |= RAIDVOL_DSRECORD_VALID;
8872 }
8873 super->updates_pending++;
8874 }
8875
8876 return consistent;
8877 }
8878
8879 static int imsm_disk_slot_to_ord(struct active_array *a, int slot)
8880 {
8881 int inst = a->info.container_member;
8882 struct intel_super *super = a->container->sb;
8883 struct imsm_dev *dev = get_imsm_dev(super, inst);
8884 struct imsm_map *map = get_imsm_map(dev, MAP_0);
8885
8886 if (slot > map->num_members) {
8887 pr_err("imsm: imsm_disk_slot_to_ord %d out of range 0..%d\n",
8888 slot, map->num_members - 1);
8889 return -1;
8890 }
8891
8892 if (slot < 0)
8893 return -1;
8894
8895 return get_imsm_ord_tbl_ent(dev, slot, MAP_0);
8896 }
8897
8898 static void imsm_set_disk(struct active_array *a, int n, int state)
8899 {
8900 int inst = a->info.container_member;
8901 struct intel_super *super = a->container->sb;
8902 struct imsm_dev *dev = get_imsm_dev(super, inst);
8903 struct imsm_map *map = get_imsm_map(dev, MAP_0);
8904 struct imsm_disk *disk;
8905 struct mdinfo *mdi;
8906 int recovery_not_finished = 0;
8907 int failed;
8908 int ord;
8909 __u8 map_state;
8910 int rebuild_done = 0;
8911 int i;
8912
8913 ord = get_imsm_ord_tbl_ent(dev, n, MAP_X);
8914 if (ord < 0)
8915 return;
8916
8917 dprintf("imsm: set_disk %d:%x\n", n, state);
8918 disk = get_imsm_disk(super, ord_to_idx(ord));
8919
8920 /* check for new failures */
8921 if (disk && (state & DS_FAULTY)) {
8922 if (mark_failure(super, dev, disk, ord_to_idx(ord)))
8923 super->updates_pending++;
8924 }
8925
8926 /* check if in_sync */
8927 if (state & DS_INSYNC && ord & IMSM_ORD_REBUILD && is_rebuilding(dev)) {
8928 struct imsm_map *migr_map = get_imsm_map(dev, MAP_1);
8929
8930 set_imsm_ord_tbl_ent(migr_map, n, ord_to_idx(ord));
8931 rebuild_done = 1;
8932 super->updates_pending++;
8933 }
8934
8935 failed = imsm_count_failed(super, dev, MAP_0);
8936 map_state = imsm_check_degraded(super, dev, failed, MAP_0);
8937
8938 /* check if recovery complete, newly degraded, or failed */
8939 dprintf("imsm: Detected transition to state ");
8940 switch (map_state) {
8941 case IMSM_T_STATE_NORMAL: /* transition to normal state */
8942 dprintf("normal: ");
8943 if (is_rebuilding(dev)) {
8944 dprintf_cont("while rebuilding");
8945 /* check if recovery is really finished */
8946 for (mdi = a->info.devs; mdi ; mdi = mdi->next)
8947 if (mdi->recovery_start != MaxSector) {
8948 recovery_not_finished = 1;
8949 break;
8950 }
8951 if (recovery_not_finished) {
8952 dprintf_cont("\n");
8953 dprintf("Rebuild has not finished yet, state not changed");
8954 if (a->last_checkpoint < mdi->recovery_start) {
8955 a->last_checkpoint = mdi->recovery_start;
8956 super->updates_pending++;
8957 }
8958 break;
8959 }
8960 end_migration(dev, super, map_state);
8961 map->failed_disk_num = ~0;
8962 super->updates_pending++;
8963 a->last_checkpoint = 0;
8964 break;
8965 }
8966 if (is_gen_migration(dev)) {
8967 dprintf_cont("while general migration");
8968 if (a->last_checkpoint >= a->info.component_size)
8969 end_migration(dev, super, map_state);
8970 else
8971 map->map_state = map_state;
8972 map->failed_disk_num = ~0;
8973 super->updates_pending++;
8974 break;
8975 }
8976 break;
8977 case IMSM_T_STATE_DEGRADED: /* transition to degraded state */
8978 dprintf_cont("degraded: ");
8979 if (map->map_state != map_state && !dev->vol.migr_state) {
8980 dprintf_cont("mark degraded");
8981 map->map_state = map_state;
8982 super->updates_pending++;
8983 a->last_checkpoint = 0;
8984 break;
8985 }
8986 if (is_rebuilding(dev)) {
8987 dprintf_cont("while rebuilding ");
8988 if (state & DS_FAULTY) {
8989 dprintf_cont("removing failed drive ");
8990 if (n == map->failed_disk_num) {
8991 dprintf_cont("end migration");
8992 end_migration(dev, super, map_state);
8993 a->last_checkpoint = 0;
8994 } else {
8995 dprintf_cont("fail detected during rebuild, changing map state");
8996 map->map_state = map_state;
8997 }
8998 super->updates_pending++;
8999 }
9000
9001 if (!rebuild_done)
9002 break;
9003
9004 /* check if recovery is really finished */
9005 for (mdi = a->info.devs; mdi ; mdi = mdi->next)
9006 if (mdi->recovery_start != MaxSector) {
9007 recovery_not_finished = 1;
9008 break;
9009 }
9010 if (recovery_not_finished) {
9011 dprintf_cont("\n");
9012 dprintf_cont("Rebuild has not finished yet");
9013 if (a->last_checkpoint < mdi->recovery_start) {
9014 a->last_checkpoint =
9015 mdi->recovery_start;
9016 super->updates_pending++;
9017 }
9018 break;
9019 }
9020
9021 dprintf_cont(" Rebuild done, still degraded");
9022 end_migration(dev, super, map_state);
9023 a->last_checkpoint = 0;
9024 super->updates_pending++;
9025
9026 for (i = 0; i < map->num_members; i++) {
9027 int idx = get_imsm_ord_tbl_ent(dev, i, MAP_0);
9028
9029 if (idx & IMSM_ORD_REBUILD)
9030 map->failed_disk_num = i;
9031 }
9032 super->updates_pending++;
9033 break;
9034 }
9035 if (is_gen_migration(dev)) {
9036 dprintf_cont("while general migration");
9037 if (a->last_checkpoint >= a->info.component_size)
9038 end_migration(dev, super, map_state);
9039 else {
9040 map->map_state = map_state;
9041 manage_second_map(super, dev);
9042 }
9043 super->updates_pending++;
9044 break;
9045 }
9046 if (is_initializing(dev)) {
9047 dprintf_cont("while initialization.");
9048 map->map_state = map_state;
9049 super->updates_pending++;
9050 break;
9051 }
9052 break;
9053 case IMSM_T_STATE_FAILED: /* transition to failed state */
9054 dprintf_cont("failed: ");
9055 if (is_gen_migration(dev)) {
9056 dprintf_cont("while general migration");
9057 map->map_state = map_state;
9058 super->updates_pending++;
9059 break;
9060 }
9061 if (map->map_state != map_state) {
9062 dprintf_cont("mark failed");
9063 end_migration(dev, super, map_state);
9064 super->updates_pending++;
9065 a->last_checkpoint = 0;
9066 break;
9067 }
9068 break;
9069 default:
9070 dprintf_cont("state %i\n", map_state);
9071 }
9072 dprintf_cont("\n");
9073 }
9074
9075 static int store_imsm_mpb(int fd, struct imsm_super *mpb)
9076 {
9077 void *buf = mpb;
9078 __u32 mpb_size = __le32_to_cpu(mpb->mpb_size);
9079 unsigned long long dsize;
9080 unsigned long long sectors;
9081 unsigned int sector_size;
9082
9083 if (!get_dev_sector_size(fd, NULL, &sector_size))
9084 return 1;
9085 get_dev_size(fd, NULL, &dsize);
9086
9087 if (mpb_size > sector_size) {
9088 /* -1 to account for anchor */
9089 sectors = mpb_sectors(mpb, sector_size) - 1;
9090
9091 /* write the extended mpb to the sectors preceeding the anchor */
9092 if (lseek64(fd, dsize - (sector_size * (2 + sectors)),
9093 SEEK_SET) < 0)
9094 return 1;
9095
9096 if ((unsigned long long)write(fd, buf + sector_size,
9097 sector_size * sectors) != sector_size * sectors)
9098 return 1;
9099 }
9100
9101 /* first block is stored on second to last sector of the disk */
9102 if (lseek64(fd, dsize - (sector_size * 2), SEEK_SET) < 0)
9103 return 1;
9104
9105 if ((unsigned int)write(fd, buf, sector_size) != sector_size)
9106 return 1;
9107
9108 return 0;
9109 }
9110
9111 static void imsm_sync_metadata(struct supertype *container)
9112 {
9113 struct intel_super *super = container->sb;
9114
9115 dprintf("sync metadata: %d\n", super->updates_pending);
9116 if (!super->updates_pending)
9117 return;
9118
9119 write_super_imsm(container, 0);
9120
9121 super->updates_pending = 0;
9122 }
9123
9124 static struct dl *imsm_readd(struct intel_super *super, int idx, struct active_array *a)
9125 {
9126 struct imsm_dev *dev = get_imsm_dev(super, a->info.container_member);
9127 int i = get_imsm_disk_idx(dev, idx, MAP_X);
9128 struct dl *dl;
9129
9130 for (dl = super->disks; dl; dl = dl->next)
9131 if (dl->index == i)
9132 break;
9133
9134 if (dl && is_failed(&dl->disk))
9135 dl = NULL;
9136
9137 if (dl)
9138 dprintf("found %x:%x\n", dl->major, dl->minor);
9139
9140 return dl;
9141 }
9142
9143 static struct dl *imsm_add_spare(struct intel_super *super, int slot,
9144 struct active_array *a, int activate_new,
9145 struct mdinfo *additional_test_list)
9146 {
9147 struct imsm_dev *dev = get_imsm_dev(super, a->info.container_member);
9148 int idx = get_imsm_disk_idx(dev, slot, MAP_X);
9149 struct imsm_super *mpb = super->anchor;
9150 struct imsm_map *map;
9151 unsigned long long pos;
9152 struct mdinfo *d;
9153 struct extent *ex;
9154 int i, j;
9155 int found;
9156 __u32 array_start = 0;
9157 __u32 array_end = 0;
9158 struct dl *dl;
9159 struct mdinfo *test_list;
9160
9161 for (dl = super->disks; dl; dl = dl->next) {
9162 /* If in this array, skip */
9163 for (d = a->info.devs ; d ; d = d->next)
9164 if (is_fd_valid(d->state_fd) &&
9165 d->disk.major == dl->major &&
9166 d->disk.minor == dl->minor) {
9167 dprintf("%x:%x already in array\n",
9168 dl->major, dl->minor);
9169 break;
9170 }
9171 if (d)
9172 continue;
9173 test_list = additional_test_list;
9174 while (test_list) {
9175 if (test_list->disk.major == dl->major &&
9176 test_list->disk.minor == dl->minor) {
9177 dprintf("%x:%x already in additional test list\n",
9178 dl->major, dl->minor);
9179 break;
9180 }
9181 test_list = test_list->next;
9182 }
9183 if (test_list)
9184 continue;
9185
9186 /* skip in use or failed drives */
9187 if (is_failed(&dl->disk) || idx == dl->index ||
9188 dl->index == -2) {
9189 dprintf("%x:%x status (failed: %d index: %d)\n",
9190 dl->major, dl->minor, is_failed(&dl->disk), idx);
9191 continue;
9192 }
9193
9194 /* skip pure spares when we are looking for partially
9195 * assimilated drives
9196 */
9197 if (dl->index == -1 && !activate_new)
9198 continue;
9199
9200 if (!drive_validate_sector_size(super, dl))
9201 continue;
9202
9203 /* Does this unused device have the requisite free space?
9204 * It needs to be able to cover all member volumes
9205 */
9206 ex = get_extents(super, dl, 1);
9207 if (!ex) {
9208 dprintf("cannot get extents\n");
9209 continue;
9210 }
9211 for (i = 0; i < mpb->num_raid_devs; i++) {
9212 dev = get_imsm_dev(super, i);
9213 map = get_imsm_map(dev, MAP_0);
9214
9215 /* check if this disk is already a member of
9216 * this array
9217 */
9218 if (get_imsm_disk_slot(map, dl->index) >= 0)
9219 continue;
9220
9221 found = 0;
9222 j = 0;
9223 pos = 0;
9224 array_start = pba_of_lba0(map);
9225 array_end = array_start +
9226 per_dev_array_size(map) - 1;
9227
9228 do {
9229 /* check that we can start at pba_of_lba0 with
9230 * num_data_stripes*blocks_per_stripe of space
9231 */
9232 if (array_start >= pos && array_end < ex[j].start) {
9233 found = 1;
9234 break;
9235 }
9236 pos = ex[j].start + ex[j].size;
9237 j++;
9238 } while (ex[j-1].size);
9239
9240 if (!found)
9241 break;
9242 }
9243
9244 free(ex);
9245 if (i < mpb->num_raid_devs) {
9246 dprintf("%x:%x does not have %u to %u available\n",
9247 dl->major, dl->minor, array_start, array_end);
9248 /* No room */
9249 continue;
9250 }
9251 return dl;
9252 }
9253
9254 return dl;
9255 }
9256
9257 static int imsm_rebuild_allowed(struct supertype *cont, int dev_idx, int failed)
9258 {
9259 struct imsm_dev *dev2;
9260 struct imsm_map *map;
9261 struct dl *idisk;
9262 int slot;
9263 int idx;
9264 __u8 state;
9265
9266 dev2 = get_imsm_dev(cont->sb, dev_idx);
9267
9268 state = imsm_check_degraded(cont->sb, dev2, failed, MAP_0);
9269 if (state == IMSM_T_STATE_FAILED) {
9270 map = get_imsm_map(dev2, MAP_0);
9271 for (slot = 0; slot < map->num_members; slot++) {
9272 /*
9273 * Check if failed disks are deleted from intel
9274 * disk list or are marked to be deleted
9275 */
9276 idx = get_imsm_disk_idx(dev2, slot, MAP_X);
9277 idisk = get_imsm_dl_disk(cont->sb, idx);
9278 /*
9279 * Do not rebuild the array if failed disks
9280 * from failed sub-array are not removed from
9281 * container.
9282 */
9283 if (idisk &&
9284 is_failed(&idisk->disk) &&
9285 (idisk->action != DISK_REMOVE))
9286 return 0;
9287 }
9288 }
9289 return 1;
9290 }
9291
9292 static struct mdinfo *imsm_activate_spare(struct active_array *a,
9293 struct metadata_update **updates)
9294 {
9295 /**
9296 * Find a device with unused free space and use it to replace a
9297 * failed/vacant region in an array. We replace failed regions one a
9298 * array at a time. The result is that a new spare disk will be added
9299 * to the first failed array and after the monitor has finished
9300 * propagating failures the remainder will be consumed.
9301 *
9302 * FIXME add a capability for mdmon to request spares from another
9303 * container.
9304 */
9305
9306 struct intel_super *super = a->container->sb;
9307 int inst = a->info.container_member;
9308 struct imsm_dev *dev = get_imsm_dev(super, inst);
9309 struct imsm_map *map = get_imsm_map(dev, MAP_0);
9310 int failed = a->info.array.raid_disks;
9311 struct mdinfo *rv = NULL;
9312 struct mdinfo *d;
9313 struct mdinfo *di;
9314 struct metadata_update *mu;
9315 struct dl *dl;
9316 struct imsm_update_activate_spare *u;
9317 int num_spares = 0;
9318 int i;
9319 int allowed;
9320
9321 for (d = a->info.devs ; d; d = d->next) {
9322 if (!is_fd_valid(d->state_fd))
9323 continue;
9324
9325 if (d->curr_state & DS_FAULTY)
9326 /* wait for Removal to happen */
9327 return NULL;
9328
9329 failed--;
9330 }
9331
9332 dprintf("imsm: activate spare: inst=%d failed=%d (%d) level=%d\n",
9333 inst, failed, a->info.array.raid_disks, a->info.array.level);
9334
9335 if (imsm_reshape_blocks_arrays_changes(super))
9336 return NULL;
9337
9338 /* Cannot activate another spare if rebuild is in progress already
9339 */
9340 if (is_rebuilding(dev)) {
9341 dprintf("imsm: No spare activation allowed. Rebuild in progress already.\n");
9342 return NULL;
9343 }
9344
9345 if (a->info.array.level == 4)
9346 /* No repair for takeovered array
9347 * imsm doesn't support raid4
9348 */
9349 return NULL;
9350
9351 if (imsm_check_degraded(super, dev, failed, MAP_0) !=
9352 IMSM_T_STATE_DEGRADED)
9353 return NULL;
9354
9355 if (get_imsm_map(dev, MAP_0)->map_state == IMSM_T_STATE_UNINITIALIZED) {
9356 dprintf("imsm: No spare activation allowed. Volume is not initialized.\n");
9357 return NULL;
9358 }
9359
9360 /*
9361 * If there are any failed disks check state of the other volume.
9362 * Block rebuild if the another one is failed until failed disks
9363 * are removed from container.
9364 */
9365 if (failed) {
9366 dprintf("found failed disks in %.*s, check if there anotherfailed sub-array.\n",
9367 MAX_RAID_SERIAL_LEN, dev->volume);
9368 /* check if states of the other volumes allow for rebuild */
9369 for (i = 0; i < super->anchor->num_raid_devs; i++) {
9370 if (i != inst) {
9371 allowed = imsm_rebuild_allowed(a->container,
9372 i, failed);
9373 if (!allowed)
9374 return NULL;
9375 }
9376 }
9377 }
9378
9379 /* For each slot, if it is not working, find a spare */
9380 for (i = 0; i < a->info.array.raid_disks; i++) {
9381 for (d = a->info.devs ; d ; d = d->next)
9382 if (d->disk.raid_disk == i)
9383 break;
9384 dprintf("found %d: %p %x\n", i, d, d?d->curr_state:0);
9385 if (d && is_fd_valid(d->state_fd))
9386 continue;
9387
9388 /*
9389 * OK, this device needs recovery. Try to re-add the
9390 * previous occupant of this slot, if this fails see if
9391 * we can continue the assimilation of a spare that was
9392 * partially assimilated, finally try to activate a new
9393 * spare.
9394 */
9395 dl = imsm_readd(super, i, a);
9396 if (!dl)
9397 dl = imsm_add_spare(super, i, a, 0, rv);
9398 if (!dl)
9399 dl = imsm_add_spare(super, i, a, 1, rv);
9400 if (!dl)
9401 continue;
9402
9403 /* found a usable disk with enough space */
9404 di = xcalloc(1, sizeof(*di));
9405
9406 /* dl->index will be -1 in the case we are activating a
9407 * pristine spare. imsm_process_update() will create a
9408 * new index in this case. Once a disk is found to be
9409 * failed in all member arrays it is kicked from the
9410 * metadata
9411 */
9412 di->disk.number = dl->index;
9413
9414 /* (ab)use di->devs to store a pointer to the device
9415 * we chose
9416 */
9417 di->devs = (struct mdinfo *) dl;
9418
9419 di->disk.raid_disk = i;
9420 di->disk.major = dl->major;
9421 di->disk.minor = dl->minor;
9422 di->disk.state = 0;
9423 di->recovery_start = 0;
9424 di->data_offset = pba_of_lba0(map);
9425 di->component_size = a->info.component_size;
9426 di->container_member = inst;
9427 di->bb.supported = 1;
9428 if (a->info.consistency_policy == CONSISTENCY_POLICY_PPL) {
9429 di->ppl_sector = get_ppl_sector(super, inst);
9430 di->ppl_size = MULTIPLE_PPL_AREA_SIZE_IMSM >> 9;
9431 }
9432 super->random = random32();
9433 di->next = rv;
9434 rv = di;
9435 num_spares++;
9436 dprintf("%x:%x to be %d at %llu\n", dl->major, dl->minor,
9437 i, di->data_offset);
9438 }
9439
9440 if (!rv)
9441 /* No spares found */
9442 return rv;
9443 /* Now 'rv' has a list of devices to return.
9444 * Create a metadata_update record to update the
9445 * disk_ord_tbl for the array
9446 */
9447 mu = xmalloc(sizeof(*mu));
9448 mu->buf = xcalloc(num_spares,
9449 sizeof(struct imsm_update_activate_spare));
9450 mu->space = NULL;
9451 mu->space_list = NULL;
9452 mu->len = sizeof(struct imsm_update_activate_spare) * num_spares;
9453 mu->next = *updates;
9454 u = (struct imsm_update_activate_spare *) mu->buf;
9455
9456 for (di = rv ; di ; di = di->next) {
9457 u->type = update_activate_spare;
9458 u->dl = (struct dl *) di->devs;
9459 di->devs = NULL;
9460 u->slot = di->disk.raid_disk;
9461 u->array = inst;
9462 u->next = u + 1;
9463 u++;
9464 }
9465 (u-1)->next = NULL;
9466 *updates = mu;
9467
9468 return rv;
9469 }
9470
9471 static int disks_overlap(struct intel_super *super, int idx, struct imsm_update_create_array *u)
9472 {
9473 struct imsm_dev *dev = get_imsm_dev(super, idx);
9474 struct imsm_map *map = get_imsm_map(dev, MAP_0);
9475 struct imsm_map *new_map = get_imsm_map(&u->dev, MAP_0);
9476 struct disk_info *inf = get_disk_info(u);
9477 struct imsm_disk *disk;
9478 int i;
9479 int j;
9480
9481 for (i = 0; i < map->num_members; i++) {
9482 disk = get_imsm_disk(super, get_imsm_disk_idx(dev, i, MAP_X));
9483 for (j = 0; j < new_map->num_members; j++)
9484 if (serialcmp(disk->serial, inf[j].serial) == 0)
9485 return 1;
9486 }
9487
9488 return 0;
9489 }
9490
9491 static struct dl *get_disk_super(struct intel_super *super, int major, int minor)
9492 {
9493 struct dl *dl;
9494
9495 for (dl = super->disks; dl; dl = dl->next)
9496 if (dl->major == major && dl->minor == minor)
9497 return dl;
9498 return NULL;
9499 }
9500
9501 static int remove_disk_super(struct intel_super *super, int major, int minor)
9502 {
9503 struct dl *prev;
9504 struct dl *dl;
9505
9506 prev = NULL;
9507 for (dl = super->disks; dl; dl = dl->next) {
9508 if (dl->major == major && dl->minor == minor) {
9509 /* remove */
9510 if (prev)
9511 prev->next = dl->next;
9512 else
9513 super->disks = dl->next;
9514 dl->next = NULL;
9515 __free_imsm_disk(dl, 1);
9516 dprintf("removed %x:%x\n", major, minor);
9517 break;
9518 }
9519 prev = dl;
9520 }
9521 return 0;
9522 }
9523
9524 static void imsm_delete(struct intel_super *super, struct dl **dlp, unsigned index);
9525
9526 static int add_remove_disk_update(struct intel_super *super)
9527 {
9528 int check_degraded = 0;
9529 struct dl *disk;
9530
9531 /* add/remove some spares to/from the metadata/contrainer */
9532 while (super->disk_mgmt_list) {
9533 struct dl *disk_cfg;
9534
9535 disk_cfg = super->disk_mgmt_list;
9536 super->disk_mgmt_list = disk_cfg->next;
9537 disk_cfg->next = NULL;
9538
9539 if (disk_cfg->action == DISK_ADD) {
9540 disk_cfg->next = super->disks;
9541 super->disks = disk_cfg;
9542 check_degraded = 1;
9543 dprintf("added %x:%x\n",
9544 disk_cfg->major, disk_cfg->minor);
9545 } else if (disk_cfg->action == DISK_REMOVE) {
9546 dprintf("Disk remove action processed: %x.%x\n",
9547 disk_cfg->major, disk_cfg->minor);
9548 disk = get_disk_super(super,
9549 disk_cfg->major,
9550 disk_cfg->minor);
9551 if (disk) {
9552 /* store action status */
9553 disk->action = DISK_REMOVE;
9554 /* remove spare disks only */
9555 if (disk->index == -1) {
9556 remove_disk_super(super,
9557 disk_cfg->major,
9558 disk_cfg->minor);
9559 } else {
9560 disk_cfg->fd = disk->fd;
9561 disk->fd = -1;
9562 }
9563 }
9564 /* release allocate disk structure */
9565 __free_imsm_disk(disk_cfg, 1);
9566 }
9567 }
9568 return check_degraded;
9569 }
9570
9571 static int apply_reshape_migration_update(struct imsm_update_reshape_migration *u,
9572 struct intel_super *super,
9573 void ***space_list)
9574 {
9575 struct intel_dev *id;
9576 void **tofree = NULL;
9577 int ret_val = 0;
9578
9579 dprintf("(enter)\n");
9580 if (u->subdev < 0 || u->subdev > 1) {
9581 dprintf("imsm: Error: Wrong subdev: %i\n", u->subdev);
9582 return ret_val;
9583 }
9584 if (space_list == NULL || *space_list == NULL) {
9585 dprintf("imsm: Error: Memory is not allocated\n");
9586 return ret_val;
9587 }
9588
9589 for (id = super->devlist ; id; id = id->next) {
9590 if (id->index == (unsigned)u->subdev) {
9591 struct imsm_dev *dev = get_imsm_dev(super, u->subdev);
9592 struct imsm_map *map;
9593 struct imsm_dev *new_dev =
9594 (struct imsm_dev *)*space_list;
9595 struct imsm_map *migr_map = get_imsm_map(dev, MAP_1);
9596 int to_state;
9597 struct dl *new_disk;
9598
9599 if (new_dev == NULL)
9600 return ret_val;
9601 *space_list = **space_list;
9602 memcpy(new_dev, dev, sizeof_imsm_dev(dev, 0));
9603 map = get_imsm_map(new_dev, MAP_0);
9604 if (migr_map) {
9605 dprintf("imsm: Error: migration in progress");
9606 return ret_val;
9607 }
9608
9609 to_state = map->map_state;
9610 if ((u->new_level == IMSM_T_RAID5) && (map->raid_level == IMSM_T_RAID0)) {
9611 map->num_members++;
9612 /* this should not happen */
9613 if (u->new_disks[0] < 0) {
9614 map->failed_disk_num =
9615 map->num_members - 1;
9616 to_state = IMSM_T_STATE_DEGRADED;
9617 } else
9618 to_state = IMSM_T_STATE_NORMAL;
9619 }
9620 migrate(new_dev, super, to_state, MIGR_GEN_MIGR);
9621
9622 if (u->new_level > -1)
9623 update_imsm_raid_level(map, u->new_level);
9624
9625 migr_map = get_imsm_map(new_dev, MAP_1);
9626 if ((u->new_level == IMSM_T_RAID5) &&
9627 (migr_map->raid_level == IMSM_T_RAID0)) {
9628 int ord = map->num_members - 1;
9629 migr_map->num_members--;
9630 if (u->new_disks[0] < 0)
9631 ord |= IMSM_ORD_REBUILD;
9632 set_imsm_ord_tbl_ent(map,
9633 map->num_members - 1,
9634 ord);
9635 }
9636 id->dev = new_dev;
9637 tofree = (void **)dev;
9638
9639 /* update chunk size
9640 */
9641 if (u->new_chunksize > 0) {
9642 struct imsm_map *dest_map =
9643 get_imsm_map(dev, MAP_0);
9644 int used_disks =
9645 imsm_num_data_members(dest_map);
9646
9647 if (used_disks == 0)
9648 return ret_val;
9649
9650 map->blocks_per_strip =
9651 __cpu_to_le16(u->new_chunksize * 2);
9652 update_num_data_stripes(map, imsm_dev_size(dev));
9653 }
9654
9655 /* ensure blocks_per_member has valid value
9656 */
9657 set_blocks_per_member(map,
9658 per_dev_array_size(map) +
9659 NUM_BLOCKS_DIRTY_STRIPE_REGION);
9660
9661 /* add disk
9662 */
9663 if (u->new_level != IMSM_T_RAID5 || migr_map->raid_level != IMSM_T_RAID0 ||
9664 migr_map->raid_level == map->raid_level)
9665 goto skip_disk_add;
9666
9667 if (u->new_disks[0] >= 0) {
9668 /* use passes spare
9669 */
9670 new_disk = get_disk_super(super,
9671 major(u->new_disks[0]),
9672 minor(u->new_disks[0]));
9673 dprintf("imsm: new disk for reshape is: %i:%i (%p, index = %i)\n",
9674 major(u->new_disks[0]),
9675 minor(u->new_disks[0]),
9676 new_disk, new_disk->index);
9677 if (new_disk == NULL)
9678 goto error_disk_add;
9679
9680 new_disk->index = map->num_members - 1;
9681 /* slot to fill in autolayout
9682 */
9683 new_disk->raiddisk = new_disk->index;
9684 new_disk->disk.status |= CONFIGURED_DISK;
9685 new_disk->disk.status &= ~SPARE_DISK;
9686 } else
9687 goto error_disk_add;
9688
9689 skip_disk_add:
9690 *tofree = *space_list;
9691 /* calculate new size
9692 */
9693 imsm_set_array_size(new_dev, -1);
9694
9695 ret_val = 1;
9696 }
9697 }
9698
9699 if (tofree)
9700 *space_list = tofree;
9701 return ret_val;
9702
9703 error_disk_add:
9704 dprintf("Error: imsm: Cannot find disk.\n");
9705 return ret_val;
9706 }
9707
9708 static int apply_size_change_update(struct imsm_update_size_change *u,
9709 struct intel_super *super)
9710 {
9711 struct intel_dev *id;
9712 int ret_val = 0;
9713
9714 dprintf("(enter)\n");
9715 if (u->subdev < 0 || u->subdev > 1) {
9716 dprintf("imsm: Error: Wrong subdev: %i\n", u->subdev);
9717 return ret_val;
9718 }
9719
9720 for (id = super->devlist ; id; id = id->next) {
9721 if (id->index == (unsigned)u->subdev) {
9722 struct imsm_dev *dev = get_imsm_dev(super, u->subdev);
9723 struct imsm_map *map = get_imsm_map(dev, MAP_0);
9724 int used_disks = imsm_num_data_members(map);
9725 unsigned long long blocks_per_member;
9726 unsigned long long new_size_per_disk;
9727
9728 if (used_disks == 0)
9729 return 0;
9730
9731 /* calculate new size
9732 */
9733 new_size_per_disk = u->new_size / used_disks;
9734 blocks_per_member = new_size_per_disk +
9735 NUM_BLOCKS_DIRTY_STRIPE_REGION;
9736
9737 imsm_set_array_size(dev, u->new_size);
9738 set_blocks_per_member(map, blocks_per_member);
9739 update_num_data_stripes(map, u->new_size);
9740 ret_val = 1;
9741 break;
9742 }
9743 }
9744
9745 return ret_val;
9746 }
9747
9748 static int prepare_spare_to_activate(struct supertype *st,
9749 struct imsm_update_activate_spare *u)
9750 {
9751 struct intel_super *super = st->sb;
9752 int prev_current_vol = super->current_vol;
9753 struct active_array *a;
9754 int ret = 1;
9755
9756 for (a = st->arrays; a; a = a->next)
9757 /*
9758 * Additional initialization (adding bitmap header, filling
9759 * the bitmap area with '1's to force initial rebuild for a whole
9760 * data-area) is required when adding the spare to the volume
9761 * with write-intent bitmap.
9762 */
9763 if (a->info.container_member == u->array &&
9764 a->info.consistency_policy == CONSISTENCY_POLICY_BITMAP) {
9765 struct dl *dl;
9766
9767 for (dl = super->disks; dl; dl = dl->next)
9768 if (dl == u->dl)
9769 break;
9770 if (!dl)
9771 break;
9772
9773 super->current_vol = u->array;
9774 if (st->ss->write_bitmap(st, dl->fd, NoUpdate))
9775 ret = 0;
9776 super->current_vol = prev_current_vol;
9777 }
9778 return ret;
9779 }
9780
9781 static int apply_update_activate_spare(struct imsm_update_activate_spare *u,
9782 struct intel_super *super,
9783 struct active_array *active_array)
9784 {
9785 struct imsm_super *mpb = super->anchor;
9786 struct imsm_dev *dev = get_imsm_dev(super, u->array);
9787 struct imsm_map *map = get_imsm_map(dev, MAP_0);
9788 struct imsm_map *migr_map;
9789 struct active_array *a;
9790 struct imsm_disk *disk;
9791 __u8 to_state;
9792 struct dl *dl;
9793 unsigned int found;
9794 int failed;
9795 int victim;
9796 int i;
9797 int second_map_created = 0;
9798
9799 for (; u; u = u->next) {
9800 victim = get_imsm_disk_idx(dev, u->slot, MAP_X);
9801
9802 if (victim < 0)
9803 return 0;
9804
9805 for (dl = super->disks; dl; dl = dl->next)
9806 if (dl == u->dl)
9807 break;
9808
9809 if (!dl) {
9810 pr_err("error: imsm_activate_spare passed an unknown disk (index: %d)\n",
9811 u->dl->index);
9812 return 0;
9813 }
9814
9815 /* count failures (excluding rebuilds and the victim)
9816 * to determine map[0] state
9817 */
9818 failed = 0;
9819 for (i = 0; i < map->num_members; i++) {
9820 if (i == u->slot)
9821 continue;
9822 disk = get_imsm_disk(super,
9823 get_imsm_disk_idx(dev, i, MAP_X));
9824 if (!disk || is_failed(disk))
9825 failed++;
9826 }
9827
9828 /* adding a pristine spare, assign a new index */
9829 if (dl->index < 0) {
9830 dl->index = super->anchor->num_disks;
9831 super->anchor->num_disks++;
9832 }
9833 disk = &dl->disk;
9834 disk->status |= CONFIGURED_DISK;
9835 disk->status &= ~SPARE_DISK;
9836
9837 /* mark rebuild */
9838 to_state = imsm_check_degraded(super, dev, failed, MAP_0);
9839 if (!second_map_created) {
9840 second_map_created = 1;
9841 map->map_state = IMSM_T_STATE_DEGRADED;
9842 migrate(dev, super, to_state, MIGR_REBUILD);
9843 } else
9844 map->map_state = to_state;
9845 migr_map = get_imsm_map(dev, MAP_1);
9846 set_imsm_ord_tbl_ent(map, u->slot, dl->index);
9847 set_imsm_ord_tbl_ent(migr_map, u->slot,
9848 dl->index | IMSM_ORD_REBUILD);
9849
9850 /* update the family_num to mark a new container
9851 * generation, being careful to record the existing
9852 * family_num in orig_family_num to clean up after
9853 * earlier mdadm versions that neglected to set it.
9854 */
9855 if (mpb->orig_family_num == 0)
9856 mpb->orig_family_num = mpb->family_num;
9857 mpb->family_num += super->random;
9858
9859 /* count arrays using the victim in the metadata */
9860 found = 0;
9861 for (a = active_array; a ; a = a->next) {
9862 int dev_idx = a->info.container_member;
9863
9864 if (get_disk_slot_in_dev(super, dev_idx, victim) >= 0)
9865 found++;
9866 }
9867
9868 /* delete the victim if it is no longer being
9869 * utilized anywhere
9870 */
9871 if (!found) {
9872 struct dl **dlp;
9873
9874 /* We know that 'manager' isn't touching anything,
9875 * so it is safe to delete
9876 */
9877 for (dlp = &super->disks; *dlp; dlp = &(*dlp)->next)
9878 if ((*dlp)->index == victim)
9879 break;
9880
9881 /* victim may be on the missing list */
9882 if (!*dlp)
9883 for (dlp = &super->missing; *dlp;
9884 dlp = &(*dlp)->next)
9885 if ((*dlp)->index == victim)
9886 break;
9887 imsm_delete(super, dlp, victim);
9888 }
9889 }
9890
9891 return 1;
9892 }
9893
9894 static int apply_reshape_container_disks_update(struct imsm_update_reshape *u,
9895 struct intel_super *super,
9896 void ***space_list)
9897 {
9898 struct dl *new_disk;
9899 struct intel_dev *id;
9900 int i;
9901 int delta_disks = u->new_raid_disks - u->old_raid_disks;
9902 int disk_count = u->old_raid_disks;
9903 void **tofree = NULL;
9904 int devices_to_reshape = 1;
9905 struct imsm_super *mpb = super->anchor;
9906 int ret_val = 0;
9907 unsigned int dev_id;
9908
9909 dprintf("(enter)\n");
9910
9911 /* enable spares to use in array */
9912 for (i = 0; i < delta_disks; i++) {
9913 new_disk = get_disk_super(super,
9914 major(u->new_disks[i]),
9915 minor(u->new_disks[i]));
9916 dprintf("imsm: new disk for reshape is: %i:%i (%p, index = %i)\n",
9917 major(u->new_disks[i]), minor(u->new_disks[i]),
9918 new_disk, new_disk->index);
9919 if (new_disk == NULL ||
9920 (new_disk->index >= 0 &&
9921 new_disk->index < u->old_raid_disks))
9922 goto update_reshape_exit;
9923 new_disk->index = disk_count++;
9924 /* slot to fill in autolayout
9925 */
9926 new_disk->raiddisk = new_disk->index;
9927 new_disk->disk.status |=
9928 CONFIGURED_DISK;
9929 new_disk->disk.status &= ~SPARE_DISK;
9930 }
9931
9932 dprintf("imsm: volume set mpb->num_raid_devs = %i\n",
9933 mpb->num_raid_devs);
9934 /* manage changes in volume
9935 */
9936 for (dev_id = 0; dev_id < mpb->num_raid_devs; dev_id++) {
9937 void **sp = *space_list;
9938 struct imsm_dev *newdev;
9939 struct imsm_map *newmap, *oldmap;
9940
9941 for (id = super->devlist ; id; id = id->next) {
9942 if (id->index == dev_id)
9943 break;
9944 }
9945 if (id == NULL)
9946 break;
9947 if (!sp)
9948 continue;
9949 *space_list = *sp;
9950 newdev = (void*)sp;
9951 /* Copy the dev, but not (all of) the map */
9952 memcpy(newdev, id->dev, sizeof(*newdev));
9953 oldmap = get_imsm_map(id->dev, MAP_0);
9954 newmap = get_imsm_map(newdev, MAP_0);
9955 /* Copy the current map */
9956 memcpy(newmap, oldmap, sizeof_imsm_map(oldmap));
9957 /* update one device only
9958 */
9959 if (devices_to_reshape) {
9960 dprintf("imsm: modifying subdev: %i\n",
9961 id->index);
9962 devices_to_reshape--;
9963 newdev->vol.migr_state = MIGR_STATE_MIGRATING;
9964 set_vol_curr_migr_unit(newdev, 0);
9965 set_migr_type(newdev, MIGR_GEN_MIGR);
9966 newmap->num_members = u->new_raid_disks;
9967 for (i = 0; i < delta_disks; i++) {
9968 set_imsm_ord_tbl_ent(newmap,
9969 u->old_raid_disks + i,
9970 u->old_raid_disks + i);
9971 }
9972 /* New map is correct, now need to save old map
9973 */
9974 newmap = get_imsm_map(newdev, MAP_1);
9975 memcpy(newmap, oldmap, sizeof_imsm_map(oldmap));
9976
9977 imsm_set_array_size(newdev, -1);
9978 }
9979
9980 sp = (void **)id->dev;
9981 id->dev = newdev;
9982 *sp = tofree;
9983 tofree = sp;
9984
9985 /* Clear migration record */
9986 memset(super->migr_rec, 0, sizeof(struct migr_record));
9987 }
9988 if (tofree)
9989 *space_list = tofree;
9990 ret_val = 1;
9991
9992 update_reshape_exit:
9993
9994 return ret_val;
9995 }
9996
9997 static int apply_takeover_update(struct imsm_update_takeover *u,
9998 struct intel_super *super,
9999 void ***space_list)
10000 {
10001 struct imsm_dev *dev = NULL;
10002 struct intel_dev *dv;
10003 struct imsm_dev *dev_new;
10004 struct imsm_map *map;
10005 struct dl *dm, *du;
10006 int i;
10007
10008 for (dv = super->devlist; dv; dv = dv->next)
10009 if (dv->index == (unsigned int)u->subarray) {
10010 dev = dv->dev;
10011 break;
10012 }
10013
10014 if (dev == NULL)
10015 return 0;
10016
10017 map = get_imsm_map(dev, MAP_0);
10018
10019 if (u->direction == R10_TO_R0) {
10020 /* Number of failed disks must be half of initial disk number */
10021 if (imsm_count_failed(super, dev, MAP_0) !=
10022 (map->num_members / 2))
10023 return 0;
10024
10025 /* iterate through devices to mark removed disks as spare */
10026 for (dm = super->disks; dm; dm = dm->next) {
10027 if (dm->disk.status & FAILED_DISK) {
10028 int idx = dm->index;
10029 /* update indexes on the disk list */
10030 /* FIXME this loop-with-the-loop looks wrong, I'm not convinced
10031 the index values will end up being correct.... NB */
10032 for (du = super->disks; du; du = du->next)
10033 if (du->index > idx)
10034 du->index--;
10035 /* mark as spare disk */
10036 mark_spare(dm);
10037 }
10038 }
10039 /* update map */
10040 map->num_members /= map->num_domains;
10041 map->map_state = IMSM_T_STATE_NORMAL;
10042 update_imsm_raid_level(map, IMSM_T_RAID0);
10043 set_num_domains(map);
10044 update_num_data_stripes(map, imsm_dev_size(dev));
10045 map->failed_disk_num = -1;
10046 }
10047
10048 if (u->direction == R0_TO_R10) {
10049 void **space;
10050
10051 /* update slots in current disk list */
10052 for (dm = super->disks; dm; dm = dm->next) {
10053 if (dm->index >= 0)
10054 dm->index *= 2;
10055 }
10056 /* create new *missing* disks */
10057 for (i = 0; i < map->num_members; i++) {
10058 space = *space_list;
10059 if (!space)
10060 continue;
10061 *space_list = *space;
10062 du = (void *)space;
10063 memcpy(du, super->disks, sizeof(*du));
10064 du->fd = -1;
10065 du->minor = 0;
10066 du->major = 0;
10067 du->index = (i * 2) + 1;
10068 sprintf((char *)du->disk.serial,
10069 " MISSING_%d", du->index);
10070 sprintf((char *)du->serial,
10071 "MISSING_%d", du->index);
10072 du->next = super->missing;
10073 super->missing = du;
10074 }
10075 /* create new dev and map */
10076 space = *space_list;
10077 if (!space)
10078 return 0;
10079 *space_list = *space;
10080 dev_new = (void *)space;
10081 memcpy(dev_new, dev, sizeof(*dev));
10082 /* update new map */
10083 map = get_imsm_map(dev_new, MAP_0);
10084
10085 map->map_state = IMSM_T_STATE_DEGRADED;
10086 update_imsm_raid_level(map, IMSM_T_RAID10);
10087 set_num_domains(map);
10088 map->num_members = map->num_members * map->num_domains;
10089 update_num_data_stripes(map, imsm_dev_size(dev));
10090
10091 /* replace dev<->dev_new */
10092 dv->dev = dev_new;
10093 }
10094 /* update disk order table */
10095 for (du = super->disks; du; du = du->next)
10096 if (du->index >= 0)
10097 set_imsm_ord_tbl_ent(map, du->index, du->index);
10098 for (du = super->missing; du; du = du->next)
10099 if (du->index >= 0) {
10100 set_imsm_ord_tbl_ent(map, du->index, du->index);
10101 mark_missing(super, dv->dev, &du->disk, du->index);
10102 }
10103
10104 return 1;
10105 }
10106
10107 static void imsm_process_update(struct supertype *st,
10108 struct metadata_update *update)
10109 {
10110 /**
10111 * crack open the metadata_update envelope to find the update record
10112 * update can be one of:
10113 * update_reshape_container_disks - all the arrays in the container
10114 * are being reshaped to have more devices. We need to mark
10115 * the arrays for general migration and convert selected spares
10116 * into active devices.
10117 * update_activate_spare - a spare device has replaced a failed
10118 * device in an array, update the disk_ord_tbl. If this disk is
10119 * present in all member arrays then also clear the SPARE_DISK
10120 * flag
10121 * update_create_array
10122 * update_kill_array
10123 * update_rename_array
10124 * update_add_remove_disk
10125 */
10126 struct intel_super *super = st->sb;
10127 struct imsm_super *mpb;
10128 enum imsm_update_type type = *(enum imsm_update_type *) update->buf;
10129
10130 /* update requires a larger buf but the allocation failed */
10131 if (super->next_len && !super->next_buf) {
10132 super->next_len = 0;
10133 return;
10134 }
10135
10136 if (super->next_buf) {
10137 memcpy(super->next_buf, super->buf, super->len);
10138 free(super->buf);
10139 super->len = super->next_len;
10140 super->buf = super->next_buf;
10141
10142 super->next_len = 0;
10143 super->next_buf = NULL;
10144 }
10145
10146 mpb = super->anchor;
10147
10148 switch (type) {
10149 case update_general_migration_checkpoint: {
10150 struct intel_dev *id;
10151 struct imsm_update_general_migration_checkpoint *u =
10152 (void *)update->buf;
10153
10154 dprintf("called for update_general_migration_checkpoint\n");
10155
10156 /* find device under general migration */
10157 for (id = super->devlist ; id; id = id->next) {
10158 if (is_gen_migration(id->dev)) {
10159 set_vol_curr_migr_unit(id->dev,
10160 u->curr_migr_unit);
10161 super->updates_pending++;
10162 }
10163 }
10164 break;
10165 }
10166 case update_takeover: {
10167 struct imsm_update_takeover *u = (void *)update->buf;
10168 if (apply_takeover_update(u, super, &update->space_list)) {
10169 imsm_update_version_info(super);
10170 super->updates_pending++;
10171 }
10172 break;
10173 }
10174
10175 case update_reshape_container_disks: {
10176 struct imsm_update_reshape *u = (void *)update->buf;
10177 if (apply_reshape_container_disks_update(
10178 u, super, &update->space_list))
10179 super->updates_pending++;
10180 break;
10181 }
10182 case update_reshape_migration: {
10183 struct imsm_update_reshape_migration *u = (void *)update->buf;
10184 if (apply_reshape_migration_update(
10185 u, super, &update->space_list))
10186 super->updates_pending++;
10187 break;
10188 }
10189 case update_size_change: {
10190 struct imsm_update_size_change *u = (void *)update->buf;
10191 if (apply_size_change_update(u, super))
10192 super->updates_pending++;
10193 break;
10194 }
10195 case update_activate_spare: {
10196 struct imsm_update_activate_spare *u = (void *) update->buf;
10197
10198 if (prepare_spare_to_activate(st, u) &&
10199 apply_update_activate_spare(u, super, st->arrays))
10200 super->updates_pending++;
10201 break;
10202 }
10203 case update_create_array: {
10204 /* someone wants to create a new array, we need to be aware of
10205 * a few races/collisions:
10206 * 1/ 'Create' called by two separate instances of mdadm
10207 * 2/ 'Create' versus 'activate_spare': mdadm has chosen
10208 * devices that have since been assimilated via
10209 * activate_spare.
10210 * In the event this update can not be carried out mdadm will
10211 * (FIX ME) notice that its update did not take hold.
10212 */
10213 struct imsm_update_create_array *u = (void *) update->buf;
10214 struct intel_dev *dv;
10215 struct imsm_dev *dev;
10216 struct imsm_map *map, *new_map;
10217 unsigned long long start, end;
10218 unsigned long long new_start, new_end;
10219 int i;
10220 struct disk_info *inf;
10221 struct dl *dl;
10222
10223 /* handle racing creates: first come first serve */
10224 if (u->dev_idx < mpb->num_raid_devs) {
10225 dprintf("subarray %d already defined\n", u->dev_idx);
10226 goto create_error;
10227 }
10228
10229 /* check update is next in sequence */
10230 if (u->dev_idx != mpb->num_raid_devs) {
10231 dprintf("can not create array %d expected index %d\n",
10232 u->dev_idx, mpb->num_raid_devs);
10233 goto create_error;
10234 }
10235
10236 new_map = get_imsm_map(&u->dev, MAP_0);
10237 new_start = pba_of_lba0(new_map);
10238 new_end = new_start + per_dev_array_size(new_map);
10239 inf = get_disk_info(u);
10240
10241 /* handle activate_spare versus create race:
10242 * check to make sure that overlapping arrays do not include
10243 * overalpping disks
10244 */
10245 for (i = 0; i < mpb->num_raid_devs; i++) {
10246 dev = get_imsm_dev(super, i);
10247 map = get_imsm_map(dev, MAP_0);
10248 start = pba_of_lba0(map);
10249 end = start + per_dev_array_size(map);
10250 if ((new_start >= start && new_start <= end) ||
10251 (start >= new_start && start <= new_end))
10252 /* overlap */;
10253 else
10254 continue;
10255
10256 if (disks_overlap(super, i, u)) {
10257 dprintf("arrays overlap\n");
10258 goto create_error;
10259 }
10260 }
10261
10262 /* check that prepare update was successful */
10263 if (!update->space) {
10264 dprintf("prepare update failed\n");
10265 goto create_error;
10266 }
10267
10268 /* check that all disks are still active before committing
10269 * changes. FIXME: could we instead handle this by creating a
10270 * degraded array? That's probably not what the user expects,
10271 * so better to drop this update on the floor.
10272 */
10273 for (i = 0; i < new_map->num_members; i++) {
10274 dl = serial_to_dl(inf[i].serial, super);
10275 if (!dl) {
10276 dprintf("disk disappeared\n");
10277 goto create_error;
10278 }
10279 }
10280
10281 super->updates_pending++;
10282
10283 /* convert spares to members and fixup ord_tbl */
10284 for (i = 0; i < new_map->num_members; i++) {
10285 dl = serial_to_dl(inf[i].serial, super);
10286 if (dl->index == -1) {
10287 dl->index = mpb->num_disks;
10288 mpb->num_disks++;
10289 dl->disk.status |= CONFIGURED_DISK;
10290 dl->disk.status &= ~SPARE_DISK;
10291 }
10292 set_imsm_ord_tbl_ent(new_map, i, dl->index);
10293 }
10294
10295 dv = update->space;
10296 dev = dv->dev;
10297 update->space = NULL;
10298 imsm_copy_dev(dev, &u->dev);
10299 dv->index = u->dev_idx;
10300 dv->next = super->devlist;
10301 super->devlist = dv;
10302 mpb->num_raid_devs++;
10303
10304 imsm_update_version_info(super);
10305 break;
10306 create_error:
10307 /* mdmon knows how to release update->space, but not
10308 * ((struct intel_dev *) update->space)->dev
10309 */
10310 if (update->space) {
10311 dv = update->space;
10312 free(dv->dev);
10313 }
10314 break;
10315 }
10316 case update_kill_array: {
10317 struct imsm_update_kill_array *u = (void *) update->buf;
10318 int victim = u->dev_idx;
10319 struct active_array *a;
10320 struct intel_dev **dp;
10321
10322 /* sanity check that we are not affecting the uuid of
10323 * active arrays, or deleting an active array
10324 *
10325 * FIXME when immutable ids are available, but note that
10326 * we'll also need to fixup the invalidated/active
10327 * subarray indexes in mdstat
10328 */
10329 for (a = st->arrays; a; a = a->next)
10330 if (a->info.container_member >= victim)
10331 break;
10332 /* by definition if mdmon is running at least one array
10333 * is active in the container, so checking
10334 * mpb->num_raid_devs is just extra paranoia
10335 */
10336 if (a || mpb->num_raid_devs == 1 || victim >= super->anchor->num_raid_devs) {
10337 dprintf("failed to delete subarray-%d\n", victim);
10338 break;
10339 }
10340
10341 for (dp = &super->devlist; *dp;)
10342 if ((*dp)->index == (unsigned)super->current_vol) {
10343 *dp = (*dp)->next;
10344 } else {
10345 if ((*dp)->index > (unsigned)victim)
10346 (*dp)->index--;
10347 dp = &(*dp)->next;
10348 }
10349 mpb->num_raid_devs--;
10350 super->updates_pending++;
10351 break;
10352 }
10353 case update_rename_array: {
10354 struct imsm_update_rename_array *u = (void *) update->buf;
10355 char name[MAX_RAID_SERIAL_LEN+1];
10356 int target = u->dev_idx;
10357 struct active_array *a;
10358 struct imsm_dev *dev;
10359
10360 /* sanity check that we are not affecting the uuid of
10361 * an active array
10362 */
10363 memset(name, 0, sizeof(name));
10364 snprintf(name, MAX_RAID_SERIAL_LEN, "%s", (char *) u->name);
10365 name[MAX_RAID_SERIAL_LEN] = '\0';
10366 for (a = st->arrays; a; a = a->next)
10367 if (a->info.container_member == target)
10368 break;
10369 dev = get_imsm_dev(super, u->dev_idx);
10370
10371 if (a || !dev || imsm_is_name_allowed(super, name, 0) == false) {
10372 dprintf("failed to rename subarray-%d\n", target);
10373 break;
10374 }
10375
10376 memcpy(dev->volume, name, MAX_RAID_SERIAL_LEN);
10377 super->updates_pending++;
10378 break;
10379 }
10380 case update_add_remove_disk: {
10381 /* we may be able to repair some arrays if disks are
10382 * being added, check the status of add_remove_disk
10383 * if discs has been added.
10384 */
10385 if (add_remove_disk_update(super)) {
10386 struct active_array *a;
10387
10388 super->updates_pending++;
10389 for (a = st->arrays; a; a = a->next)
10390 a->check_degraded = 1;
10391 }
10392 break;
10393 }
10394 case update_prealloc_badblocks_mem:
10395 break;
10396 case update_rwh_policy: {
10397 struct imsm_update_rwh_policy *u = (void *)update->buf;
10398 int target = u->dev_idx;
10399 struct imsm_dev *dev = get_imsm_dev(super, target);
10400
10401 if (dev->rwh_policy != u->new_policy) {
10402 dev->rwh_policy = u->new_policy;
10403 super->updates_pending++;
10404 }
10405 break;
10406 }
10407 default:
10408 pr_err("error: unsupported process update type:(type: %d)\n", type);
10409 }
10410 }
10411
10412 static struct mdinfo *get_spares_for_grow(struct supertype *st);
10413
10414 static int imsm_prepare_update(struct supertype *st,
10415 struct metadata_update *update)
10416 {
10417 /**
10418 * Allocate space to hold new disk entries, raid-device entries or a new
10419 * mpb if necessary. The manager synchronously waits for updates to
10420 * complete in the monitor, so new mpb buffers allocated here can be
10421 * integrated by the monitor thread without worrying about live pointers
10422 * in the manager thread.
10423 */
10424 enum imsm_update_type type;
10425 struct intel_super *super = st->sb;
10426 unsigned int sector_size = super->sector_size;
10427 struct imsm_super *mpb = super->anchor;
10428 size_t buf_len;
10429 size_t len = 0;
10430
10431 if (update->len < (int)sizeof(type))
10432 return 0;
10433
10434 type = *(enum imsm_update_type *) update->buf;
10435
10436 switch (type) {
10437 case update_general_migration_checkpoint:
10438 if (update->len < (int)sizeof(struct imsm_update_general_migration_checkpoint))
10439 return 0;
10440 dprintf("called for update_general_migration_checkpoint\n");
10441 break;
10442 case update_takeover: {
10443 struct imsm_update_takeover *u = (void *)update->buf;
10444 if (update->len < (int)sizeof(*u))
10445 return 0;
10446 if (u->direction == R0_TO_R10) {
10447 void **tail = (void **)&update->space_list;
10448 struct imsm_dev *dev = get_imsm_dev(super, u->subarray);
10449 struct imsm_map *map = get_imsm_map(dev, MAP_0);
10450 int num_members = map->num_members;
10451 void *space;
10452 int size, i;
10453 /* allocate memory for added disks */
10454 for (i = 0; i < num_members; i++) {
10455 size = sizeof(struct dl);
10456 space = xmalloc(size);
10457 *tail = space;
10458 tail = space;
10459 *tail = NULL;
10460 }
10461 /* allocate memory for new device */
10462 size = sizeof_imsm_dev(super->devlist->dev, 0) +
10463 (num_members * sizeof(__u32));
10464 space = xmalloc(size);
10465 *tail = space;
10466 tail = space;
10467 *tail = NULL;
10468 len = disks_to_mpb_size(num_members * 2);
10469 }
10470
10471 break;
10472 }
10473 case update_reshape_container_disks: {
10474 /* Every raid device in the container is about to
10475 * gain some more devices, and we will enter a
10476 * reconfiguration.
10477 * So each 'imsm_map' will be bigger, and the imsm_vol
10478 * will now hold 2 of them.
10479 * Thus we need new 'struct imsm_dev' allocations sized
10480 * as sizeof_imsm_dev but with more devices in both maps.
10481 */
10482 struct imsm_update_reshape *u = (void *)update->buf;
10483 struct intel_dev *dl;
10484 void **space_tail = (void**)&update->space_list;
10485
10486 if (update->len < (int)sizeof(*u))
10487 return 0;
10488
10489 dprintf("for update_reshape\n");
10490
10491 for (dl = super->devlist; dl; dl = dl->next) {
10492 int size = sizeof_imsm_dev(dl->dev, 1);
10493 void *s;
10494 if (u->new_raid_disks > u->old_raid_disks)
10495 size += sizeof(__u32)*2*
10496 (u->new_raid_disks - u->old_raid_disks);
10497 s = xmalloc(size);
10498 *space_tail = s;
10499 space_tail = s;
10500 *space_tail = NULL;
10501 }
10502
10503 len = disks_to_mpb_size(u->new_raid_disks);
10504 dprintf("New anchor length is %llu\n", (unsigned long long)len);
10505 break;
10506 }
10507 case update_reshape_migration: {
10508 /* for migration level 0->5 we need to add disks
10509 * so the same as for container operation we will copy
10510 * device to the bigger location.
10511 * in memory prepared device and new disk area are prepared
10512 * for usage in process update
10513 */
10514 struct imsm_update_reshape_migration *u = (void *)update->buf;
10515 struct intel_dev *id;
10516 void **space_tail = (void **)&update->space_list;
10517 int size;
10518 void *s;
10519 int current_level = -1;
10520
10521 if (update->len < (int)sizeof(*u))
10522 return 0;
10523
10524 dprintf("for update_reshape\n");
10525
10526 /* add space for bigger array in update
10527 */
10528 for (id = super->devlist; id; id = id->next) {
10529 if (id->index == (unsigned)u->subdev) {
10530 size = sizeof_imsm_dev(id->dev, 1);
10531 if (u->new_raid_disks > u->old_raid_disks)
10532 size += sizeof(__u32)*2*
10533 (u->new_raid_disks - u->old_raid_disks);
10534 s = xmalloc(size);
10535 *space_tail = s;
10536 space_tail = s;
10537 *space_tail = NULL;
10538 break;
10539 }
10540 }
10541 if (update->space_list == NULL)
10542 break;
10543
10544 /* add space for disk in update
10545 */
10546 size = sizeof(struct dl);
10547 s = xmalloc(size);
10548 *space_tail = s;
10549 space_tail = s;
10550 *space_tail = NULL;
10551
10552 /* add spare device to update
10553 */
10554 for (id = super->devlist ; id; id = id->next)
10555 if (id->index == (unsigned)u->subdev) {
10556 struct imsm_dev *dev;
10557 struct imsm_map *map;
10558
10559 dev = get_imsm_dev(super, u->subdev);
10560 map = get_imsm_map(dev, MAP_0);
10561 current_level = map->raid_level;
10562 break;
10563 }
10564 if (u->new_level == 5 && u->new_level != current_level) {
10565 struct mdinfo *spares;
10566
10567 spares = get_spares_for_grow(st);
10568 if (spares) {
10569 struct dl *dl;
10570 struct mdinfo *dev;
10571
10572 dev = spares->devs;
10573 if (dev) {
10574 u->new_disks[0] =
10575 makedev(dev->disk.major,
10576 dev->disk.minor);
10577 dl = get_disk_super(super,
10578 dev->disk.major,
10579 dev->disk.minor);
10580 dl->index = u->old_raid_disks;
10581 dev = dev->next;
10582 }
10583 sysfs_free(spares);
10584 }
10585 }
10586 len = disks_to_mpb_size(u->new_raid_disks);
10587 dprintf("New anchor length is %llu\n", (unsigned long long)len);
10588 break;
10589 }
10590 case update_size_change: {
10591 if (update->len < (int)sizeof(struct imsm_update_size_change))
10592 return 0;
10593 break;
10594 }
10595 case update_activate_spare: {
10596 if (update->len < (int)sizeof(struct imsm_update_activate_spare))
10597 return 0;
10598 break;
10599 }
10600 case update_create_array: {
10601 struct imsm_update_create_array *u = (void *) update->buf;
10602 struct intel_dev *dv;
10603 struct imsm_dev *dev = &u->dev;
10604 struct imsm_map *map = get_imsm_map(dev, MAP_0);
10605 struct dl *dl;
10606 struct disk_info *inf;
10607 int i;
10608 int activate = 0;
10609
10610 if (update->len < (int)sizeof(*u))
10611 return 0;
10612
10613 inf = get_disk_info(u);
10614 len = sizeof_imsm_dev(dev, 1);
10615 /* allocate a new super->devlist entry */
10616 dv = xmalloc(sizeof(*dv));
10617 dv->dev = xmalloc(len);
10618 update->space = dv;
10619
10620 /* count how many spares will be converted to members */
10621 for (i = 0; i < map->num_members; i++) {
10622 dl = serial_to_dl(inf[i].serial, super);
10623 if (!dl) {
10624 /* hmm maybe it failed?, nothing we can do about
10625 * it here
10626 */
10627 continue;
10628 }
10629 if (count_memberships(dl, super) == 0)
10630 activate++;
10631 }
10632 len += activate * sizeof(struct imsm_disk);
10633 break;
10634 }
10635 case update_kill_array: {
10636 if (update->len < (int)sizeof(struct imsm_update_kill_array))
10637 return 0;
10638 break;
10639 }
10640 case update_rename_array: {
10641 if (update->len < (int)sizeof(struct imsm_update_rename_array))
10642 return 0;
10643 break;
10644 }
10645 case update_add_remove_disk:
10646 /* no update->len needed */
10647 break;
10648 case update_prealloc_badblocks_mem:
10649 super->extra_space += sizeof(struct bbm_log) -
10650 get_imsm_bbm_log_size(super->bbm_log);
10651 break;
10652 case update_rwh_policy: {
10653 if (update->len < (int)sizeof(struct imsm_update_rwh_policy))
10654 return 0;
10655 break;
10656 }
10657 default:
10658 return 0;
10659 }
10660
10661 /* check if we need a larger metadata buffer */
10662 if (super->next_buf)
10663 buf_len = super->next_len;
10664 else
10665 buf_len = super->len;
10666
10667 if (__le32_to_cpu(mpb->mpb_size) + super->extra_space + len > buf_len) {
10668 /* ok we need a larger buf than what is currently allocated
10669 * if this allocation fails process_update will notice that
10670 * ->next_len is set and ->next_buf is NULL
10671 */
10672 buf_len = ROUND_UP(__le32_to_cpu(mpb->mpb_size) +
10673 super->extra_space + len, sector_size);
10674 if (super->next_buf)
10675 free(super->next_buf);
10676
10677 super->next_len = buf_len;
10678 if (posix_memalign(&super->next_buf, sector_size, buf_len) == 0)
10679 memset(super->next_buf, 0, buf_len);
10680 else
10681 super->next_buf = NULL;
10682 }
10683 return 1;
10684 }
10685
10686 /* must be called while manager is quiesced */
10687 static void imsm_delete(struct intel_super *super, struct dl **dlp, unsigned index)
10688 {
10689 struct imsm_super *mpb = super->anchor;
10690 struct dl *iter;
10691 struct imsm_dev *dev;
10692 struct imsm_map *map;
10693 unsigned int i, j, num_members;
10694 __u32 ord, ord_map0;
10695 struct bbm_log *log = super->bbm_log;
10696
10697 dprintf("deleting device[%d] from imsm_super\n", index);
10698
10699 /* shift all indexes down one */
10700 for (iter = super->disks; iter; iter = iter->next)
10701 if (iter->index > (int)index)
10702 iter->index--;
10703 for (iter = super->missing; iter; iter = iter->next)
10704 if (iter->index > (int)index)
10705 iter->index--;
10706
10707 for (i = 0; i < mpb->num_raid_devs; i++) {
10708 dev = get_imsm_dev(super, i);
10709 map = get_imsm_map(dev, MAP_0);
10710 num_members = map->num_members;
10711 for (j = 0; j < num_members; j++) {
10712 /* update ord entries being careful not to propagate
10713 * ord-flags to the first map
10714 */
10715 ord = get_imsm_ord_tbl_ent(dev, j, MAP_X);
10716 ord_map0 = get_imsm_ord_tbl_ent(dev, j, MAP_0);
10717
10718 if (ord_to_idx(ord) <= index)
10719 continue;
10720
10721 map = get_imsm_map(dev, MAP_0);
10722 set_imsm_ord_tbl_ent(map, j, ord_map0 - 1);
10723 map = get_imsm_map(dev, MAP_1);
10724 if (map)
10725 set_imsm_ord_tbl_ent(map, j, ord - 1);
10726 }
10727 }
10728
10729 for (i = 0; i < log->entry_count; i++) {
10730 struct bbm_log_entry *entry = &log->marked_block_entries[i];
10731
10732 if (entry->disk_ordinal <= index)
10733 continue;
10734 entry->disk_ordinal--;
10735 }
10736
10737 mpb->num_disks--;
10738 super->updates_pending++;
10739 if (*dlp) {
10740 struct dl *dl = *dlp;
10741
10742 *dlp = (*dlp)->next;
10743 __free_imsm_disk(dl, 1);
10744 }
10745 }
10746
10747 static int imsm_get_allowed_degradation(int level, int raid_disks,
10748 struct intel_super *super,
10749 struct imsm_dev *dev)
10750 {
10751 switch (level) {
10752 case 1:
10753 case 10:{
10754 int ret_val = 0;
10755 struct imsm_map *map;
10756 int i;
10757
10758 ret_val = raid_disks/2;
10759 /* check map if all disks pairs not failed
10760 * in both maps
10761 */
10762 map = get_imsm_map(dev, MAP_0);
10763 for (i = 0; i < ret_val; i++) {
10764 int degradation = 0;
10765 if (get_imsm_disk(super, i) == NULL)
10766 degradation++;
10767 if (get_imsm_disk(super, i + 1) == NULL)
10768 degradation++;
10769 if (degradation == 2)
10770 return 0;
10771 }
10772 map = get_imsm_map(dev, MAP_1);
10773 /* if there is no second map
10774 * result can be returned
10775 */
10776 if (map == NULL)
10777 return ret_val;
10778 /* check degradation in second map
10779 */
10780 for (i = 0; i < ret_val; i++) {
10781 int degradation = 0;
10782 if (get_imsm_disk(super, i) == NULL)
10783 degradation++;
10784 if (get_imsm_disk(super, i + 1) == NULL)
10785 degradation++;
10786 if (degradation == 2)
10787 return 0;
10788 }
10789 return ret_val;
10790 }
10791 case 5:
10792 return 1;
10793 case 6:
10794 return 2;
10795 default:
10796 return 0;
10797 }
10798 }
10799
10800 /*******************************************************************************
10801 * Function: validate_container_imsm
10802 * Description: This routine validates container after assemble,
10803 * eg. if devices in container are under the same controller.
10804 *
10805 * Parameters:
10806 * info : linked list with info about devices used in array
10807 * Returns:
10808 * 1 : HBA mismatch
10809 * 0 : Success
10810 ******************************************************************************/
10811 int validate_container_imsm(struct mdinfo *info)
10812 {
10813 if (check_no_platform())
10814 return 0;
10815
10816 struct sys_dev *idev;
10817 struct sys_dev *hba = NULL;
10818 struct sys_dev *intel_devices = find_intel_devices();
10819 char *dev_path = devt_to_devpath(makedev(info->disk.major,
10820 info->disk.minor), 1, NULL);
10821
10822 for (idev = intel_devices; idev; idev = idev->next) {
10823 if (dev_path && strstr(dev_path, idev->path)) {
10824 hba = idev;
10825 break;
10826 }
10827 }
10828 if (dev_path)
10829 free(dev_path);
10830
10831 if (!hba) {
10832 pr_err("WARNING - Cannot detect HBA for device %s!\n",
10833 devid2kname(makedev(info->disk.major, info->disk.minor)));
10834 return 1;
10835 }
10836
10837 const struct imsm_orom *orom = get_orom_by_device_id(hba->dev_id);
10838 struct mdinfo *dev;
10839
10840 for (dev = info->next; dev; dev = dev->next) {
10841 dev_path = devt_to_devpath(makedev(dev->disk.major,
10842 dev->disk.minor), 1, NULL);
10843
10844 struct sys_dev *hba2 = NULL;
10845 for (idev = intel_devices; idev; idev = idev->next) {
10846 if (dev_path && strstr(dev_path, idev->path)) {
10847 hba2 = idev;
10848 break;
10849 }
10850 }
10851 if (dev_path)
10852 free(dev_path);
10853
10854 const struct imsm_orom *orom2 = hba2 == NULL ? NULL :
10855 get_orom_by_device_id(hba2->dev_id);
10856
10857 if (hba2 && hba->type != hba2->type) {
10858 pr_err("WARNING - HBAs of devices do not match %s != %s\n",
10859 get_sys_dev_type(hba->type), get_sys_dev_type(hba2->type));
10860 return 1;
10861 }
10862
10863 if (orom != orom2) {
10864 pr_err("WARNING - IMSM container assembled with disks under different HBAs!\n"
10865 " This operation is not supported and can lead to data loss.\n");
10866 return 1;
10867 }
10868
10869 if (!orom) {
10870 pr_err("WARNING - IMSM container assembled with disks under HBAs without IMSM platform support!\n"
10871 " This operation is not supported and can lead to data loss.\n");
10872 return 1;
10873 }
10874 }
10875
10876 return 0;
10877 }
10878
10879 /*******************************************************************************
10880 * Function: imsm_record_badblock
10881 * Description: This routine stores new bad block record in BBM log
10882 *
10883 * Parameters:
10884 * a : array containing a bad block
10885 * slot : disk number containing a bad block
10886 * sector : bad block sector
10887 * length : bad block sectors range
10888 * Returns:
10889 * 1 : Success
10890 * 0 : Error
10891 ******************************************************************************/
10892 static int imsm_record_badblock(struct active_array *a, int slot,
10893 unsigned long long sector, int length)
10894 {
10895 struct intel_super *super = a->container->sb;
10896 int ord;
10897 int ret;
10898
10899 ord = imsm_disk_slot_to_ord(a, slot);
10900 if (ord < 0)
10901 return 0;
10902
10903 ret = record_new_badblock(super->bbm_log, ord_to_idx(ord), sector,
10904 length);
10905 if (ret)
10906 super->updates_pending++;
10907
10908 return ret;
10909 }
10910 /*******************************************************************************
10911 * Function: imsm_clear_badblock
10912 * Description: This routine clears bad block record from BBM log
10913 *
10914 * Parameters:
10915 * a : array containing a bad block
10916 * slot : disk number containing a bad block
10917 * sector : bad block sector
10918 * length : bad block sectors range
10919 * Returns:
10920 * 1 : Success
10921 * 0 : Error
10922 ******************************************************************************/
10923 static int imsm_clear_badblock(struct active_array *a, int slot,
10924 unsigned long long sector, int length)
10925 {
10926 struct intel_super *super = a->container->sb;
10927 int ord;
10928 int ret;
10929
10930 ord = imsm_disk_slot_to_ord(a, slot);
10931 if (ord < 0)
10932 return 0;
10933
10934 ret = clear_badblock(super->bbm_log, ord_to_idx(ord), sector, length);
10935 if (ret)
10936 super->updates_pending++;
10937
10938 return ret;
10939 }
10940 /*******************************************************************************
10941 * Function: imsm_get_badblocks
10942 * Description: This routine get list of bad blocks for an array
10943 *
10944 * Parameters:
10945 * a : array
10946 * slot : disk number
10947 * Returns:
10948 * bb : structure containing bad blocks
10949 * NULL : error
10950 ******************************************************************************/
10951 static struct md_bb *imsm_get_badblocks(struct active_array *a, int slot)
10952 {
10953 int inst = a->info.container_member;
10954 struct intel_super *super = a->container->sb;
10955 struct imsm_dev *dev = get_imsm_dev(super, inst);
10956 struct imsm_map *map = get_imsm_map(dev, MAP_0);
10957 int ord;
10958
10959 ord = imsm_disk_slot_to_ord(a, slot);
10960 if (ord < 0)
10961 return NULL;
10962
10963 get_volume_badblocks(super->bbm_log, ord_to_idx(ord), pba_of_lba0(map),
10964 per_dev_array_size(map), &super->bb);
10965
10966 return &super->bb;
10967 }
10968 /*******************************************************************************
10969 * Function: examine_badblocks_imsm
10970 * Description: Prints list of bad blocks on a disk to the standard output
10971 *
10972 * Parameters:
10973 * st : metadata handler
10974 * fd : open file descriptor for device
10975 * devname : device name
10976 * Returns:
10977 * 0 : Success
10978 * 1 : Error
10979 ******************************************************************************/
10980 static int examine_badblocks_imsm(struct supertype *st, int fd, char *devname)
10981 {
10982 struct intel_super *super = st->sb;
10983 struct bbm_log *log = super->bbm_log;
10984 struct dl *d = NULL;
10985 int any = 0;
10986
10987 for (d = super->disks; d ; d = d->next) {
10988 if (strcmp(d->devname, devname) == 0)
10989 break;
10990 }
10991
10992 if ((d == NULL) || (d->index < 0)) { /* serial mismatch probably */
10993 pr_err("%s doesn't appear to be part of a raid array\n",
10994 devname);
10995 return 1;
10996 }
10997
10998 if (log != NULL) {
10999 unsigned int i;
11000 struct bbm_log_entry *entry = &log->marked_block_entries[0];
11001
11002 for (i = 0; i < log->entry_count; i++) {
11003 if (entry[i].disk_ordinal == d->index) {
11004 unsigned long long sector = __le48_to_cpu(
11005 &entry[i].defective_block_start);
11006 int cnt = entry[i].marked_count + 1;
11007
11008 if (!any) {
11009 printf("Bad-blocks on %s:\n", devname);
11010 any = 1;
11011 }
11012
11013 printf("%20llu for %d sectors\n", sector, cnt);
11014 }
11015 }
11016 }
11017
11018 if (!any)
11019 printf("No bad-blocks list configured on %s\n", devname);
11020
11021 return 0;
11022 }
11023 /*******************************************************************************
11024 * Function: init_migr_record_imsm
11025 * Description: Function inits imsm migration record
11026 * Parameters:
11027 * super : imsm internal array info
11028 * dev : device under migration
11029 * info : general array info to find the smallest device
11030 * Returns:
11031 * none
11032 ******************************************************************************/
11033 void init_migr_record_imsm(struct supertype *st, struct imsm_dev *dev,
11034 struct mdinfo *info)
11035 {
11036 struct intel_super *super = st->sb;
11037 struct migr_record *migr_rec = super->migr_rec;
11038 int new_data_disks;
11039 unsigned long long dsize, dev_sectors;
11040 long long unsigned min_dev_sectors = -1LLU;
11041 struct imsm_map *map_dest = get_imsm_map(dev, MAP_0);
11042 struct imsm_map *map_src = get_imsm_map(dev, MAP_1);
11043 unsigned long long num_migr_units;
11044 unsigned long long array_blocks;
11045 struct dl *dl_disk = NULL;
11046
11047 memset(migr_rec, 0, sizeof(struct migr_record));
11048 migr_rec->family_num = __cpu_to_le32(super->anchor->family_num);
11049
11050 /* only ascending reshape supported now */
11051 migr_rec->ascending_migr = __cpu_to_le32(1);
11052
11053 migr_rec->dest_depth_per_unit = GEN_MIGR_AREA_SIZE /
11054 max(map_dest->blocks_per_strip, map_src->blocks_per_strip);
11055 migr_rec->dest_depth_per_unit *=
11056 max(map_dest->blocks_per_strip, map_src->blocks_per_strip);
11057 new_data_disks = imsm_num_data_members(map_dest);
11058 migr_rec->blocks_per_unit =
11059 __cpu_to_le32(migr_rec->dest_depth_per_unit * new_data_disks);
11060 migr_rec->dest_depth_per_unit =
11061 __cpu_to_le32(migr_rec->dest_depth_per_unit);
11062 array_blocks = info->component_size * new_data_disks;
11063 num_migr_units =
11064 array_blocks / __le32_to_cpu(migr_rec->blocks_per_unit);
11065
11066 if (array_blocks % __le32_to_cpu(migr_rec->blocks_per_unit))
11067 num_migr_units++;
11068 set_num_migr_units(migr_rec, num_migr_units);
11069
11070 migr_rec->post_migr_vol_cap = dev->size_low;
11071 migr_rec->post_migr_vol_cap_hi = dev->size_high;
11072
11073 /* Find the smallest dev */
11074 for (dl_disk = super->disks; dl_disk ; dl_disk = dl_disk->next) {
11075 /* ignore spares in container */
11076 if (dl_disk->index < 0)
11077 continue;
11078 get_dev_size(dl_disk->fd, NULL, &dsize);
11079 dev_sectors = dsize / 512;
11080 if (dev_sectors < min_dev_sectors)
11081 min_dev_sectors = dev_sectors;
11082 }
11083 set_migr_chkp_area_pba(migr_rec, min_dev_sectors -
11084 RAID_DISK_RESERVED_BLOCKS_IMSM_HI);
11085
11086 write_imsm_migr_rec(st);
11087
11088 return;
11089 }
11090
11091 /*******************************************************************************
11092 * Function: save_backup_imsm
11093 * Description: Function saves critical data stripes to Migration Copy Area
11094 * and updates the current migration unit status.
11095 * Use restore_stripes() to form a destination stripe,
11096 * and to write it to the Copy Area.
11097 * Parameters:
11098 * st : supertype information
11099 * dev : imsm device that backup is saved for
11100 * info : general array info
11101 * buf : input buffer
11102 * length : length of data to backup (blocks_per_unit)
11103 * Returns:
11104 * 0 : success
11105 *, -1 : fail
11106 ******************************************************************************/
11107 int save_backup_imsm(struct supertype *st,
11108 struct imsm_dev *dev,
11109 struct mdinfo *info,
11110 void *buf,
11111 int length)
11112 {
11113 int rv = -1;
11114 struct intel_super *super = st->sb;
11115 int i;
11116 struct imsm_map *map_dest = get_imsm_map(dev, MAP_0);
11117 int new_disks = map_dest->num_members;
11118 int dest_layout = 0;
11119 int dest_chunk, targets[new_disks];
11120 unsigned long long start, target_offsets[new_disks];
11121 int data_disks = imsm_num_data_members(map_dest);
11122
11123 for (i = 0; i < new_disks; i++) {
11124 struct dl *dl_disk = get_imsm_dl_disk(super, i);
11125 if (dl_disk && is_fd_valid(dl_disk->fd))
11126 targets[i] = dl_disk->fd;
11127 else
11128 goto abort;
11129 }
11130
11131 start = info->reshape_progress * 512;
11132 for (i = 0; i < new_disks; i++) {
11133 target_offsets[i] = migr_chkp_area_pba(super->migr_rec) * 512;
11134 /* move back copy area adderss, it will be moved forward
11135 * in restore_stripes() using start input variable
11136 */
11137 target_offsets[i] -= start/data_disks;
11138 }
11139
11140 dest_layout = imsm_level_to_layout(map_dest->raid_level);
11141 dest_chunk = __le16_to_cpu(map_dest->blocks_per_strip) * 512;
11142
11143 if (restore_stripes(targets, /* list of dest devices */
11144 target_offsets, /* migration record offsets */
11145 new_disks,
11146 dest_chunk,
11147 map_dest->raid_level,
11148 dest_layout,
11149 -1, /* source backup file descriptor */
11150 0, /* input buf offset
11151 * always 0 buf is already offseted */
11152 start,
11153 length,
11154 buf) != 0) {
11155 pr_err("Error restoring stripes\n");
11156 goto abort;
11157 }
11158
11159 rv = 0;
11160
11161 abort:
11162 return rv;
11163 }
11164
11165 /*******************************************************************************
11166 * Function: save_checkpoint_imsm
11167 * Description: Function called for current unit status update
11168 * in the migration record. It writes it to disk.
11169 * Parameters:
11170 * super : imsm internal array info
11171 * info : general array info
11172 * Returns:
11173 * 0: success
11174 * 1: failure
11175 * 2: failure, means no valid migration record
11176 * / no general migration in progress /
11177 ******************************************************************************/
11178 int save_checkpoint_imsm(struct supertype *st, struct mdinfo *info, int state)
11179 {
11180 struct intel_super *super = st->sb;
11181 unsigned long long blocks_per_unit;
11182 unsigned long long curr_migr_unit;
11183
11184 if (load_imsm_migr_rec(super) != 0) {
11185 dprintf("imsm: ERROR: Cannot read migration record for checkpoint save.\n");
11186 return 1;
11187 }
11188
11189 blocks_per_unit = __le32_to_cpu(super->migr_rec->blocks_per_unit);
11190 if (blocks_per_unit == 0) {
11191 dprintf("imsm: no migration in progress.\n");
11192 return 2;
11193 }
11194 curr_migr_unit = info->reshape_progress / blocks_per_unit;
11195 /* check if array is alligned to copy area
11196 * if it is not alligned, add one to current migration unit value
11197 * this can happend on array reshape finish only
11198 */
11199 if (info->reshape_progress % blocks_per_unit)
11200 curr_migr_unit++;
11201
11202 set_current_migr_unit(super->migr_rec, curr_migr_unit);
11203 super->migr_rec->rec_status = __cpu_to_le32(state);
11204 set_migr_dest_1st_member_lba(super->migr_rec,
11205 super->migr_rec->dest_depth_per_unit * curr_migr_unit);
11206
11207 if (write_imsm_migr_rec(st) < 0) {
11208 dprintf("imsm: Cannot write migration record outside backup area\n");
11209 return 1;
11210 }
11211
11212 return 0;
11213 }
11214
11215 /*******************************************************************************
11216 * Function: recover_backup_imsm
11217 * Description: Function recovers critical data from the Migration Copy Area
11218 * while assembling an array.
11219 * Parameters:
11220 * super : imsm internal array info
11221 * info : general array info
11222 * Returns:
11223 * 0 : success (or there is no data to recover)
11224 * 1 : fail
11225 ******************************************************************************/
11226 int recover_backup_imsm(struct supertype *st, struct mdinfo *info)
11227 {
11228 struct intel_super *super = st->sb;
11229 struct migr_record *migr_rec = super->migr_rec;
11230 struct imsm_map *map_dest;
11231 struct intel_dev *id = NULL;
11232 unsigned long long read_offset;
11233 unsigned long long write_offset;
11234 unsigned unit_len;
11235 int new_disks, err;
11236 char *buf = NULL;
11237 int retval = 1;
11238 unsigned int sector_size = super->sector_size;
11239 unsigned long long curr_migr_unit = current_migr_unit(migr_rec);
11240 unsigned long long num_migr_units = get_num_migr_units(migr_rec);
11241 char buffer[SYSFS_MAX_BUF_SIZE];
11242 int skipped_disks = 0;
11243 struct dl *dl_disk;
11244
11245 err = sysfs_get_str(info, NULL, "array_state", (char *)buffer, sizeof(buffer));
11246 if (err < 1)
11247 return 1;
11248
11249 /* recover data only during assemblation */
11250 if (strncmp(buffer, "inactive", 8) != 0)
11251 return 0;
11252 /* no data to recover */
11253 if (__le32_to_cpu(migr_rec->rec_status) == UNIT_SRC_NORMAL)
11254 return 0;
11255 if (curr_migr_unit >= num_migr_units)
11256 return 1;
11257
11258 /* find device during reshape */
11259 for (id = super->devlist; id; id = id->next)
11260 if (is_gen_migration(id->dev))
11261 break;
11262 if (id == NULL)
11263 return 1;
11264
11265 map_dest = get_imsm_map(id->dev, MAP_0);
11266 new_disks = map_dest->num_members;
11267
11268 read_offset = migr_chkp_area_pba(migr_rec) * 512;
11269
11270 write_offset = (migr_dest_1st_member_lba(migr_rec) +
11271 pba_of_lba0(map_dest)) * 512;
11272
11273 unit_len = __le32_to_cpu(migr_rec->dest_depth_per_unit) * 512;
11274 if (posix_memalign((void **)&buf, sector_size, unit_len) != 0)
11275 goto abort;
11276
11277 for (dl_disk = super->disks; dl_disk; dl_disk = dl_disk->next) {
11278 if (dl_disk->index < 0)
11279 continue;
11280
11281 if (!is_fd_valid(dl_disk->fd)) {
11282 skipped_disks++;
11283 continue;
11284 }
11285 if (lseek64(dl_disk->fd, read_offset, SEEK_SET) < 0) {
11286 pr_err("Cannot seek to block: %s\n",
11287 strerror(errno));
11288 skipped_disks++;
11289 continue;
11290 }
11291 if (read(dl_disk->fd, buf, unit_len) != (ssize_t)unit_len) {
11292 pr_err("Cannot read copy area block: %s\n",
11293 strerror(errno));
11294 skipped_disks++;
11295 continue;
11296 }
11297 if (lseek64(dl_disk->fd, write_offset, SEEK_SET) < 0) {
11298 pr_err("Cannot seek to block: %s\n",
11299 strerror(errno));
11300 skipped_disks++;
11301 continue;
11302 }
11303 if (write(dl_disk->fd, buf, unit_len) != (ssize_t)unit_len) {
11304 pr_err("Cannot restore block: %s\n",
11305 strerror(errno));
11306 skipped_disks++;
11307 continue;
11308 }
11309 }
11310
11311 if (skipped_disks > imsm_get_allowed_degradation(info->new_level,
11312 new_disks,
11313 super,
11314 id->dev)) {
11315 pr_err("Cannot restore data from backup. Too many failed disks\n");
11316 goto abort;
11317 }
11318
11319 if (save_checkpoint_imsm(st, info, UNIT_SRC_NORMAL)) {
11320 /* ignore error == 2, this can mean end of reshape here
11321 */
11322 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL) during restart\n");
11323 } else
11324 retval = 0;
11325
11326 abort:
11327 free(buf);
11328 return retval;
11329 }
11330
11331 /**
11332 * test_and_add_drive_controller_policy_imsm() - add disk controller to policies list.
11333 * @type: Policy type to search on list.
11334 * @pols: List of currently recorded policies.
11335 * @disk_fd: File descriptor of the device to check.
11336 * @hba: The hba disk is attached, could be NULL if verification is disabled.
11337 * @verbose: verbose flag.
11338 *
11339 * IMSM cares about drive physical placement. If @hba is not set, it adds unknown policy.
11340 * If there is no controller policy on pols we are free to add first one. If there is a policy then,
11341 * new must be the same - no controller mixing allowed.
11342 */
11343 static mdadm_status_t
11344 test_and_add_drive_controller_policy_imsm(const char * const type, dev_policy_t **pols, int disk_fd,
11345 struct sys_dev *hba, const int verbose)
11346 {
11347 const char *controller_policy = get_sys_dev_type(SYS_DEV_UNKNOWN);
11348 struct dev_policy *pol = pol_find(*pols, (char *)type);
11349 char devname[MAX_RAID_SERIAL_LEN];
11350
11351 if (hba)
11352 controller_policy = get_sys_dev_type(hba->type);
11353
11354 if (!pol) {
11355 pol_add(pols, (char *)type, (char *)controller_policy, "imsm");
11356 return MDADM_STATUS_SUCCESS;
11357 }
11358
11359 if (strcmp(pol->value, controller_policy) == 0)
11360 return MDADM_STATUS_SUCCESS;
11361
11362 fd2devname(disk_fd, devname);
11363 pr_vrb("Intel(R) raid controller \"%s\" found for %s, but \"%s\" was detected earlier\n",
11364 controller_policy, devname, pol->value);
11365 pr_vrb("Disks under different controllers cannot be used, aborting\n");
11366
11367 return MDADM_STATUS_ERROR;
11368 }
11369
11370 /**
11371 * test_and_add_drive_encryption_policy_imsm() - add disk encryption to policies list.
11372 * @type: policy type to search in the list.
11373 * @pols: list of currently recorded policies.
11374 * @disk_fd: file descriptor of the device to check.
11375 * @hba: The hba to which the drive is attached, could be NULL if verification is disabled.
11376 * @verbose: verbose flag.
11377 *
11378 * IMSM cares about drive encryption state. It is not allowed to mix disks with different
11379 * encryption state within one md device.
11380 * If there is no encryption policy on pols we are free to add first one.
11381 * If there is a policy then, new must be the same.
11382 */
11383 static mdadm_status_t
11384 test_and_add_drive_encryption_policy_imsm(const char * const type, dev_policy_t **pols, int disk_fd,
11385 struct sys_dev *hba, const int verbose)
11386 {
11387 struct dev_policy *expected_policy = pol_find(*pols, (char *)type);
11388 struct encryption_information information = {0};
11389 char *encryption_state = "Unknown";
11390 int status = MDADM_STATUS_SUCCESS;
11391 bool encryption_checked = true;
11392 char devname[PATH_MAX];
11393
11394 if (!hba)
11395 goto check_policy;
11396
11397 switch (hba->type) {
11398 case SYS_DEV_NVME:
11399 case SYS_DEV_VMD:
11400 status = get_nvme_opal_encryption_information(disk_fd, &information, verbose);
11401 break;
11402 case SYS_DEV_SATA:
11403 case SYS_DEV_SATA_VMD:
11404 status = get_ata_encryption_information(disk_fd, &information, verbose);
11405 break;
11406 default:
11407 encryption_checked = false;
11408 }
11409
11410 if (status) {
11411 fd2devname(disk_fd, devname);
11412 pr_vrb("Failed to read encryption information of device %s\n", devname);
11413 return MDADM_STATUS_ERROR;
11414 }
11415
11416 if (encryption_checked) {
11417 if (information.status == ENC_STATUS_LOCKED) {
11418 fd2devname(disk_fd, devname);
11419 pr_vrb("Device %s is in Locked state, cannot use. Aborting.\n", devname);
11420 return MDADM_STATUS_ERROR;
11421 }
11422 encryption_state = (char *)get_encryption_status_string(information.status);
11423 }
11424
11425 check_policy:
11426 if (expected_policy) {
11427 if (strcmp(expected_policy->value, encryption_state) == 0)
11428 return MDADM_STATUS_SUCCESS;
11429
11430 fd2devname(disk_fd, devname);
11431 pr_vrb("Encryption status \"%s\" detected for disk %s, but \"%s\" status was detected earlier.\n",
11432 encryption_state, devname, expected_policy->value);
11433 pr_vrb("Disks with different encryption status cannot be used.\n");
11434 return MDADM_STATUS_ERROR;
11435 }
11436
11437 pol_add(pols, (char *)type, encryption_state, "imsm");
11438
11439 return MDADM_STATUS_SUCCESS;
11440 }
11441
11442 struct imsm_drive_policy {
11443 char *type;
11444 mdadm_status_t (*test_and_add_drive_policy)(const char * const type,
11445 struct dev_policy **pols, int disk_fd,
11446 struct sys_dev *hba, const int verbose);
11447 };
11448
11449 struct imsm_drive_policy imsm_policies[] = {
11450 {"controller", test_and_add_drive_controller_policy_imsm},
11451 {"encryption", test_and_add_drive_encryption_policy_imsm}
11452 };
11453
11454 mdadm_status_t test_and_add_drive_policies_imsm(struct dev_policy **pols, int disk_fd,
11455 const int verbose)
11456 {
11457 struct imsm_drive_policy *imsm_pol;
11458 struct sys_dev *hba = NULL;
11459 char path[PATH_MAX];
11460 mdadm_status_t ret;
11461 unsigned int i;
11462
11463 /* If imsm platform verification is disabled, do not search for hba. */
11464 if (check_no_platform() != 1) {
11465 if (!diskfd_to_devpath(disk_fd, 1, path)) {
11466 pr_vrb("IMSM: Failed to retrieve device path by file descriptor.\n");
11467 return MDADM_STATUS_ERROR;
11468 }
11469
11470 hba = find_disk_attached_hba(disk_fd, path);
11471 if (!hba) {
11472 pr_vrb("IMSM: Failed to find hba for %s\n", path);
11473 return MDADM_STATUS_ERROR;
11474 }
11475 }
11476
11477 for (i = 0; i < ARRAY_SIZE(imsm_policies); i++) {
11478 imsm_pol = &imsm_policies[i];
11479
11480 ret = imsm_pol->test_and_add_drive_policy(imsm_pol->type, pols, disk_fd, hba,
11481 verbose);
11482 if (ret != MDADM_STATUS_SUCCESS)
11483 /* Inherit error code */
11484 return ret;
11485 }
11486
11487 return MDADM_STATUS_SUCCESS;
11488 }
11489
11490 /**
11491 * get_spare_criteria_imsm() - set spare criteria.
11492 * @st: supertype.
11493 * @mddev_path: path to md device devnode, it must be container.
11494 * @c: spare_criteria struct to fill, not NULL.
11495 *
11496 * If superblock is not loaded, use mddev_path to load_container. It must be given in this case.
11497 * Filles size and sector size accordingly to superblock.
11498 */
11499 mdadm_status_t get_spare_criteria_imsm(struct supertype *st, char *mddev_path,
11500 struct spare_criteria *c)
11501 {
11502 mdadm_status_t ret = MDADM_STATUS_ERROR;
11503 bool free_superblock = false;
11504 unsigned long long size = 0;
11505 struct intel_super *super;
11506 struct extent *e;
11507 struct dl *dl;
11508 int i;
11509
11510 /* If no superblock and no mddev_path, we cannot load superblock. */
11511 assert(st->sb || mddev_path);
11512
11513 if (mddev_path) {
11514 int fd = open(mddev_path, O_RDONLY);
11515 mdadm_status_t rv;
11516
11517 if (!is_fd_valid(fd))
11518 return MDADM_STATUS_ERROR;
11519
11520 if (!st->sb) {
11521 if (load_container_imsm(st, fd, st->devnm)) {
11522 close(fd);
11523 return MDADM_STATUS_ERROR;
11524 }
11525 free_superblock = true;
11526 }
11527
11528 rv = mddev_test_and_add_drive_policies(st, &c->pols, fd, 0);
11529 close(fd);
11530
11531 if (rv != MDADM_STATUS_SUCCESS)
11532 goto out;
11533 }
11534
11535 super = st->sb;
11536
11537 /* find first active disk in array */
11538 dl = super->disks;
11539 while (dl && (is_failed(&dl->disk) || dl->index == -1))
11540 dl = dl->next;
11541
11542 if (!dl)
11543 goto out;
11544
11545 /* find last lba used by subarrays */
11546 e = get_extents(super, dl, 0);
11547 if (!e)
11548 goto out;
11549
11550 for (i = 0; e[i].size; i++)
11551 continue;
11552 if (i > 0)
11553 size = e[i - 1].start + e[i - 1].size;
11554 free(e);
11555
11556 /* add the amount of space needed for metadata */
11557 size += imsm_min_reserved_sectors(super);
11558
11559 c->min_size = size * 512;
11560 c->sector_size = super->sector_size;
11561 c->criteria_set = true;
11562 ret = MDADM_STATUS_SUCCESS;
11563
11564 out:
11565 if (free_superblock)
11566 free_super_imsm(st);
11567
11568 if (ret != MDADM_STATUS_SUCCESS)
11569 c->criteria_set = false;
11570
11571 return ret;
11572 }
11573
11574 static char *imsm_find_array_devnm_by_subdev(int subdev, char *container)
11575 {
11576 static char devnm[32];
11577 char subdev_name[20];
11578 struct mdstat_ent *mdstat;
11579
11580 sprintf(subdev_name, "%d", subdev);
11581 mdstat = mdstat_by_subdev(subdev_name, container);
11582 if (!mdstat)
11583 return NULL;
11584
11585 strcpy(devnm, mdstat->devnm);
11586 free_mdstat(mdstat);
11587 return devnm;
11588 }
11589
11590 static int imsm_reshape_is_allowed_on_container(struct supertype *st,
11591 struct geo_params *geo,
11592 int *old_raid_disks,
11593 int direction)
11594 {
11595 /* currently we only support increasing the number of devices
11596 * for a container. This increases the number of device for each
11597 * member array. They must all be RAID0 or RAID5.
11598 */
11599 int ret_val = 0;
11600 struct mdinfo *info, *member;
11601 int devices_that_can_grow = 0;
11602
11603 dprintf("imsm: imsm_reshape_is_allowed_on_container(ENTER): st->devnm = (%s)\n", st->devnm);
11604
11605 if (geo->size > 0 ||
11606 geo->level != UnSet ||
11607 geo->layout != UnSet ||
11608 geo->chunksize != 0 ||
11609 geo->raid_disks == UnSet) {
11610 dprintf("imsm: Container operation is allowed for raid disks number change only.\n");
11611 return ret_val;
11612 }
11613
11614 if (direction == ROLLBACK_METADATA_CHANGES) {
11615 dprintf("imsm: Metadata changes rollback is not supported for container operation.\n");
11616 return ret_val;
11617 }
11618
11619 info = container_content_imsm(st, NULL);
11620 for (member = info; member; member = member->next) {
11621 char *result;
11622
11623 dprintf("imsm: checking device_num: %i\n",
11624 member->container_member);
11625
11626 if (geo->raid_disks <= member->array.raid_disks) {
11627 /* we work on container for Online Capacity Expansion
11628 * only so raid_disks has to grow
11629 */
11630 dprintf("imsm: for container operation raid disks increase is required\n");
11631 break;
11632 }
11633
11634 if (info->array.level != 0 && info->array.level != 5) {
11635 /* we cannot use this container with other raid level
11636 */
11637 dprintf("imsm: for container operation wrong raid level (%i) detected\n",
11638 info->array.level);
11639 break;
11640 } else {
11641 /* check for platform support
11642 * for this raid level configuration
11643 */
11644 struct intel_super *super = st->sb;
11645 if (!is_raid_level_supported(super->orom,
11646 member->array.level,
11647 geo->raid_disks)) {
11648 dprintf("platform does not support raid%d with %d disk%s\n",
11649 info->array.level,
11650 geo->raid_disks,
11651 geo->raid_disks > 1 ? "s" : "");
11652 break;
11653 }
11654 /* check if component size is aligned to chunk size
11655 */
11656 if (info->component_size %
11657 (info->array.chunk_size/512)) {
11658 dprintf("Component size is not aligned to chunk size\n");
11659 break;
11660 }
11661 }
11662
11663 if (*old_raid_disks &&
11664 info->array.raid_disks != *old_raid_disks)
11665 break;
11666 *old_raid_disks = info->array.raid_disks;
11667
11668 /* All raid5 and raid0 volumes in container
11669 * have to be ready for Online Capacity Expansion
11670 * so they need to be assembled. We have already
11671 * checked that no recovery etc is happening.
11672 */
11673 result = imsm_find_array_devnm_by_subdev(member->container_member,
11674 st->container_devnm);
11675 if (result == NULL) {
11676 dprintf("imsm: cannot find array\n");
11677 break;
11678 }
11679 devices_that_can_grow++;
11680 }
11681 sysfs_free(info);
11682 if (!member && devices_that_can_grow)
11683 ret_val = 1;
11684
11685 if (ret_val)
11686 dprintf("Container operation allowed\n");
11687 else
11688 dprintf("Error: %i\n", ret_val);
11689
11690 return ret_val;
11691 }
11692
11693 /* Function: get_spares_for_grow
11694 * Description: Allocates memory and creates list of spare devices
11695 * avaliable in container. Checks if spare drive size is acceptable.
11696 * Parameters: Pointer to the supertype structure
11697 * Returns: Pointer to the list of spare devices (mdinfo structure) on success,
11698 * NULL if fail
11699 */
11700 static struct mdinfo *get_spares_for_grow(struct supertype *st)
11701 {
11702 struct spare_criteria sc = {0};
11703 struct mdinfo *spares;
11704
11705 get_spare_criteria_imsm(st, NULL, &sc);
11706 spares = container_choose_spares(st, &sc, NULL, NULL, NULL, 0);
11707
11708 dev_policy_free(sc.pols);
11709
11710 return spares;
11711 }
11712
11713 /******************************************************************************
11714 * function: imsm_create_metadata_update_for_reshape
11715 * Function creates update for whole IMSM container.
11716 *
11717 ******************************************************************************/
11718 static int imsm_create_metadata_update_for_reshape(
11719 struct supertype *st,
11720 struct geo_params *geo,
11721 int old_raid_disks,
11722 struct imsm_update_reshape **updatep)
11723 {
11724 struct intel_super *super = st->sb;
11725 struct imsm_super *mpb = super->anchor;
11726 int update_memory_size;
11727 struct imsm_update_reshape *u;
11728 struct mdinfo *spares;
11729 int i;
11730 int delta_disks;
11731 struct mdinfo *dev;
11732
11733 dprintf("(enter) raid_disks = %i\n", geo->raid_disks);
11734
11735 delta_disks = geo->raid_disks - old_raid_disks;
11736
11737 /* size of all update data without anchor */
11738 update_memory_size = sizeof(struct imsm_update_reshape);
11739
11740 /* now add space for spare disks that we need to add. */
11741 update_memory_size += sizeof(u->new_disks[0]) * (delta_disks - 1);
11742
11743 u = xcalloc(1, update_memory_size);
11744 u->type = update_reshape_container_disks;
11745 u->old_raid_disks = old_raid_disks;
11746 u->new_raid_disks = geo->raid_disks;
11747
11748 /* now get spare disks list
11749 */
11750 spares = get_spares_for_grow(st);
11751
11752 if (spares == NULL || delta_disks > spares->array.spare_disks) {
11753 pr_err("imsm: ERROR: Cannot get spare devices for %s.\n", geo->dev_name);
11754 i = -1;
11755 goto abort;
11756 }
11757
11758 /* we have got spares
11759 * update disk list in imsm_disk list table in anchor
11760 */
11761 dprintf("imsm: %i spares are available.\n\n",
11762 spares->array.spare_disks);
11763
11764 dev = spares->devs;
11765 for (i = 0; i < delta_disks; i++) {
11766 struct dl *dl;
11767
11768 if (dev == NULL)
11769 break;
11770 u->new_disks[i] = makedev(dev->disk.major,
11771 dev->disk.minor);
11772 dl = get_disk_super(super, dev->disk.major, dev->disk.minor);
11773 dl->index = mpb->num_disks;
11774 mpb->num_disks++;
11775 dev = dev->next;
11776 }
11777
11778 abort:
11779 /* free spares
11780 */
11781 sysfs_free(spares);
11782
11783 dprintf("imsm: reshape update preparation :");
11784 if (i == delta_disks) {
11785 dprintf_cont(" OK\n");
11786 *updatep = u;
11787 return update_memory_size;
11788 }
11789 free(u);
11790 dprintf_cont(" Error\n");
11791
11792 return 0;
11793 }
11794
11795 /******************************************************************************
11796 * function: imsm_create_metadata_update_for_size_change()
11797 * Creates update for IMSM array for array size change.
11798 *
11799 ******************************************************************************/
11800 static int imsm_create_metadata_update_for_size_change(
11801 struct supertype *st,
11802 struct geo_params *geo,
11803 struct imsm_update_size_change **updatep)
11804 {
11805 struct intel_super *super = st->sb;
11806 int update_memory_size;
11807 struct imsm_update_size_change *u;
11808
11809 dprintf("(enter) New size = %llu\n", geo->size);
11810
11811 /* size of all update data without anchor */
11812 update_memory_size = sizeof(struct imsm_update_size_change);
11813
11814 u = xcalloc(1, update_memory_size);
11815 u->type = update_size_change;
11816 u->subdev = super->current_vol;
11817 u->new_size = geo->size;
11818
11819 dprintf("imsm: reshape update preparation : OK\n");
11820 *updatep = u;
11821
11822 return update_memory_size;
11823 }
11824
11825 /******************************************************************************
11826 * function: imsm_create_metadata_update_for_migration()
11827 * Creates update for IMSM array.
11828 *
11829 ******************************************************************************/
11830 static int imsm_create_metadata_update_for_migration(
11831 struct supertype *st,
11832 struct geo_params *geo,
11833 struct imsm_update_reshape_migration **updatep)
11834 {
11835 struct intel_super *super = st->sb;
11836 int update_memory_size;
11837 int current_chunk_size;
11838 struct imsm_update_reshape_migration *u;
11839 struct imsm_dev *dev = get_imsm_dev(super, super->current_vol);
11840 struct imsm_map *map = get_imsm_map(dev, MAP_0);
11841 int previous_level = -1;
11842
11843 dprintf("(enter) New Level = %i\n", geo->level);
11844
11845 /* size of all update data without anchor */
11846 update_memory_size = sizeof(struct imsm_update_reshape_migration);
11847
11848 u = xcalloc(1, update_memory_size);
11849 u->type = update_reshape_migration;
11850 u->subdev = super->current_vol;
11851 u->new_level = geo->level;
11852 u->new_layout = geo->layout;
11853 u->new_raid_disks = u->old_raid_disks = geo->raid_disks;
11854 u->new_disks[0] = -1;
11855 u->new_chunksize = -1;
11856
11857 current_chunk_size = __le16_to_cpu(map->blocks_per_strip) / 2;
11858
11859 if (geo->chunksize != current_chunk_size) {
11860 u->new_chunksize = geo->chunksize / 1024;
11861 dprintf("imsm: chunk size change from %i to %i\n",
11862 current_chunk_size, u->new_chunksize);
11863 }
11864 previous_level = map->raid_level;
11865
11866 if (geo->level == 5 && previous_level == 0) {
11867 struct mdinfo *spares = NULL;
11868
11869 u->new_raid_disks++;
11870 spares = get_spares_for_grow(st);
11871 if (spares == NULL || spares->array.spare_disks < 1) {
11872 free(u);
11873 sysfs_free(spares);
11874 update_memory_size = 0;
11875 pr_err("cannot get spare device for requested migration\n");
11876 return 0;
11877 }
11878 sysfs_free(spares);
11879 }
11880 dprintf("imsm: reshape update preparation : OK\n");
11881 *updatep = u;
11882
11883 return update_memory_size;
11884 }
11885
11886 static void imsm_update_metadata_locally(struct supertype *st,
11887 void *buf, int len)
11888 {
11889 struct metadata_update mu;
11890
11891 mu.buf = buf;
11892 mu.len = len;
11893 mu.space = NULL;
11894 mu.space_list = NULL;
11895 mu.next = NULL;
11896 if (imsm_prepare_update(st, &mu))
11897 imsm_process_update(st, &mu);
11898
11899 while (mu.space_list) {
11900 void **space = mu.space_list;
11901 mu.space_list = *space;
11902 free(space);
11903 }
11904 }
11905
11906 /**
11907 * imsm_analyze_expand() - check expand properties and calculate new size.
11908 * @st: imsm supertype.
11909 * @geo: new geometry params.
11910 * @array: array info.
11911 * @direction: reshape direction.
11912 *
11913 * Obtain free space after the &array and verify if expand to requested size is
11914 * possible. If geo->size is set to %MAX_SIZE, assume that max free size is
11915 * requested.
11916 *
11917 * Return:
11918 * On success %IMSM_STATUS_OK is returned, geo->size and geo->raid_disks are
11919 * updated.
11920 * On error, %IMSM_STATUS_ERROR is returned.
11921 */
11922 static imsm_status_t imsm_analyze_expand(struct supertype *st,
11923 struct geo_params *geo,
11924 struct mdinfo *array,
11925 int direction)
11926 {
11927 struct intel_super *super = st->sb;
11928 struct imsm_dev *dev = get_imsm_dev(super, super->current_vol);
11929 struct imsm_map *map = get_imsm_map(dev, MAP_0);
11930 int data_disks = imsm_num_data_members(map);
11931
11932 unsigned long long current_size;
11933 unsigned long long free_size;
11934 unsigned long long new_size;
11935 unsigned long long max_size;
11936
11937 const int chunk_kib = geo->chunksize / 1024;
11938 imsm_status_t rv;
11939
11940 if (direction == ROLLBACK_METADATA_CHANGES) {
11941 /**
11942 * Accept size for rollback only.
11943 */
11944 new_size = geo->size * 2;
11945 goto success;
11946 }
11947
11948 if (data_disks == 0) {
11949 pr_err("imsm: Cannot retrieve data disks.\n");
11950 return IMSM_STATUS_ERROR;
11951 }
11952 current_size = array->custom_array_size / data_disks;
11953
11954 rv = imsm_get_free_size(super, dev->vol.map->num_members, 0, chunk_kib, &free_size, true);
11955 if (rv != IMSM_STATUS_OK) {
11956 pr_err("imsm: Cannot find free space for expand.\n");
11957 return IMSM_STATUS_ERROR;
11958 }
11959 max_size = round_member_size_to_mb(free_size + current_size);
11960
11961 if (geo->size == MAX_SIZE)
11962 new_size = max_size;
11963 else
11964 new_size = round_member_size_to_mb(geo->size * 2);
11965
11966 if (new_size == 0) {
11967 pr_err("imsm: Rounded requested size is 0.\n");
11968 return IMSM_STATUS_ERROR;
11969 }
11970
11971 if (new_size > max_size) {
11972 pr_err("imsm: Rounded requested size (%llu) is larger than free space available (%llu).\n",
11973 new_size, max_size);
11974 return IMSM_STATUS_ERROR;
11975 }
11976
11977 if (new_size == current_size) {
11978 pr_err("imsm: Rounded requested size (%llu) is same as current size (%llu).\n",
11979 new_size, current_size);
11980 return IMSM_STATUS_ERROR;
11981 }
11982
11983 if (new_size < current_size) {
11984 pr_err("imsm: Size reduction is not supported, rounded requested size (%llu) is smaller than current (%llu).\n",
11985 new_size, current_size);
11986 return IMSM_STATUS_ERROR;
11987 }
11988
11989 success:
11990 dprintf("imsm: New size per member is %llu.\n", new_size);
11991 geo->size = data_disks * new_size;
11992 geo->raid_disks = dev->vol.map->num_members;
11993 return IMSM_STATUS_OK;
11994 }
11995
11996 /***************************************************************************
11997 * Function: imsm_analyze_change
11998 * Description: Function analyze change for single volume
11999 * and validate if transition is supported
12000 * Parameters: Geometry parameters, supertype structure,
12001 * metadata change direction (apply/rollback)
12002 * Returns: Operation type code on success, -1 if fail
12003 ****************************************************************************/
12004 enum imsm_reshape_type imsm_analyze_change(struct supertype *st,
12005 struct geo_params *geo,
12006 int direction, struct context *c)
12007 {
12008 struct mdinfo info;
12009 int change = -1;
12010 int check_devs = 0;
12011 int chunk;
12012 /* imsm compatible layout value for array geometry verification */
12013 int imsm_layout = -1;
12014 int raid_disks = geo->raid_disks;
12015 imsm_status_t rv;
12016
12017 getinfo_super_imsm_volume(st, &info, NULL);
12018 if (geo->level != info.array.level && geo->level >= IMSM_T_RAID0 &&
12019 geo->level != UnSet) {
12020 switch (info.array.level) {
12021 case IMSM_T_RAID0:
12022 if (geo->level == IMSM_T_RAID5) {
12023 change = CH_MIGRATION;
12024 if (geo->layout != ALGORITHM_LEFT_ASYMMETRIC) {
12025 pr_err("Error. Requested Layout not supported (left-asymmetric layout is supported only)!\n");
12026 change = -1;
12027 goto analyse_change_exit;
12028 }
12029 imsm_layout = geo->layout;
12030 check_devs = 1;
12031 raid_disks += 1; /* parity disk added */
12032 } else if (geo->level == IMSM_T_RAID10) {
12033 if (geo->level == IMSM_T_RAID10 && geo->raid_disks > 2 &&
12034 !c->force) {
12035 pr_err("Warning! VROC UEFI driver does not support RAID10 in requested layout.\n");
12036 pr_err("Array won't be suitable as boot device.\n");
12037 pr_err("Note: You can omit this check with \"--force\"\n");
12038 if (ask("Do you want to continue") < 1)
12039 return CH_ABORT;
12040 }
12041 change = CH_TAKEOVER;
12042 check_devs = 1;
12043 raid_disks *= 2; /* mirrors added */
12044 imsm_layout = 0x102; /* imsm supported layout */
12045 }
12046 break;
12047 case IMSM_T_RAID1:
12048 case IMSM_T_RAID10:
12049 if (geo->level == 0) {
12050 change = CH_TAKEOVER;
12051 check_devs = 1;
12052 raid_disks /= 2;
12053 imsm_layout = 0; /* imsm raid0 layout */
12054 }
12055 break;
12056 }
12057 if (change == -1) {
12058 pr_err("Error. Level Migration from %d to %d not supported!\n",
12059 info.array.level, geo->level);
12060 goto analyse_change_exit;
12061 }
12062 } else
12063 geo->level = info.array.level;
12064
12065 if (geo->layout != info.array.layout &&
12066 (geo->layout != UnSet && geo->layout != -1)) {
12067 change = CH_MIGRATION;
12068 if (info.array.layout == 0 && info.array.level == IMSM_T_RAID5 &&
12069 geo->layout == 5) {
12070 /* reshape 5 -> 4 */
12071 } else if (info.array.layout == 5 && info.array.level == IMSM_T_RAID5 &&
12072 geo->layout == 0) {
12073 /* reshape 4 -> 5 */
12074 geo->layout = 0;
12075 geo->level = 5;
12076 } else {
12077 pr_err("Error. Layout Migration from %d to %d not supported!\n",
12078 info.array.layout, geo->layout);
12079 change = -1;
12080 goto analyse_change_exit;
12081 }
12082 } else {
12083 geo->layout = info.array.layout;
12084 if (imsm_layout == -1)
12085 imsm_layout = info.array.layout;
12086 }
12087
12088 if (geo->chunksize > 0 && geo->chunksize != UnSet &&
12089 geo->chunksize != info.array.chunk_size) {
12090 if (info.array.level == IMSM_T_RAID10) {
12091 pr_err("Error. Chunk size change for RAID 10 is not supported.\n");
12092 change = -1;
12093 goto analyse_change_exit;
12094 } else if (info.component_size % (geo->chunksize/512)) {
12095 pr_err("New chunk size (%dK) does not evenly divide device size (%lluk). Aborting...\n",
12096 geo->chunksize/1024, info.component_size/2);
12097 change = -1;
12098 goto analyse_change_exit;
12099 }
12100 change = CH_MIGRATION;
12101 } else {
12102 geo->chunksize = info.array.chunk_size;
12103 }
12104
12105 if (geo->size > 0) {
12106 if (change != -1) {
12107 pr_err("Error. Size change should be the only one at a time.\n");
12108 change = -1;
12109 goto analyse_change_exit;
12110 }
12111
12112 rv = imsm_analyze_expand(st, geo, &info, direction);
12113 if (rv != IMSM_STATUS_OK)
12114 goto analyse_change_exit;
12115 raid_disks = geo->raid_disks;
12116 change = CH_ARRAY_SIZE;
12117 }
12118
12119 chunk = geo->chunksize / 1024;
12120
12121 if (!validate_geometry_imsm(st,
12122 geo->level,
12123 imsm_layout,
12124 raid_disks,
12125 &chunk,
12126 geo->size, INVALID_SECTORS,
12127 0, 0, info.consistency_policy, 1))
12128 change = -1;
12129
12130 if (check_devs) {
12131 struct intel_super *super = st->sb;
12132 struct imsm_super *mpb = super->anchor;
12133
12134 if (mpb->num_raid_devs > 1) {
12135 pr_err("Error. Cannot perform operation on %s- for this operation "
12136 "it MUST be single array in container\n", geo->dev_name);
12137 change = -1;
12138 }
12139 }
12140
12141 analyse_change_exit:
12142 if (direction == ROLLBACK_METADATA_CHANGES &&
12143 (change == CH_MIGRATION || change == CH_TAKEOVER)) {
12144 dprintf("imsm: Metadata changes rollback is not supported for migration and takeover operations.\n");
12145 change = -1;
12146 }
12147 return change;
12148 }
12149
12150 int imsm_takeover(struct supertype *st, struct geo_params *geo)
12151 {
12152 struct intel_super *super = st->sb;
12153 struct imsm_update_takeover *u;
12154
12155 u = xmalloc(sizeof(struct imsm_update_takeover));
12156
12157 u->type = update_takeover;
12158 u->subarray = super->current_vol;
12159
12160 /* 10->0 transition */
12161 if (geo->level == 0)
12162 u->direction = R10_TO_R0;
12163
12164 /* 0->10 transition */
12165 if (geo->level == 10)
12166 u->direction = R0_TO_R10;
12167
12168 /* update metadata locally */
12169 imsm_update_metadata_locally(st, u,
12170 sizeof(struct imsm_update_takeover));
12171 /* and possibly remotely */
12172 if (st->update_tail)
12173 append_metadata_update(st, u,
12174 sizeof(struct imsm_update_takeover));
12175 else
12176 free(u);
12177
12178 return 0;
12179 }
12180
12181 /* Flush size update if size calculated by num_data_stripes is higher than
12182 * imsm_dev_size to eliminate differences during reshape.
12183 * Mdmon will recalculate them correctly.
12184 * If subarray index is not set then check whole container.
12185 * Returns:
12186 * 0 - no error occurred
12187 * 1 - error detected
12188 */
12189 static int imsm_fix_size_mismatch(struct supertype *st, int subarray_index)
12190 {
12191 struct intel_super *super = st->sb;
12192 int tmp = super->current_vol;
12193 int ret_val = 1;
12194 int i;
12195
12196 for (i = 0; i < super->anchor->num_raid_devs; i++) {
12197 if (subarray_index >= 0 && i != subarray_index)
12198 continue;
12199 super->current_vol = i;
12200 struct imsm_dev *dev = get_imsm_dev(super, super->current_vol);
12201 struct imsm_map *map = get_imsm_map(dev, MAP_0);
12202 unsigned int disc_count = imsm_num_data_members(map);
12203 struct geo_params geo;
12204 struct imsm_update_size_change *update;
12205 unsigned long long calc_size = per_dev_array_size(map) * disc_count;
12206 unsigned long long d_size = imsm_dev_size(dev);
12207 int u_size;
12208
12209 if (calc_size == d_size)
12210 continue;
12211
12212 /* There is a difference, confirm that imsm_dev_size is
12213 * smaller and push update.
12214 */
12215 if (d_size > calc_size) {
12216 pr_err("imsm: dev size of subarray %d is incorrect\n",
12217 i);
12218 goto exit;
12219 }
12220 memset(&geo, 0, sizeof(struct geo_params));
12221 geo.size = d_size;
12222 u_size = imsm_create_metadata_update_for_size_change(st, &geo,
12223 &update);
12224 imsm_update_metadata_locally(st, update, u_size);
12225 if (st->update_tail) {
12226 append_metadata_update(st, update, u_size);
12227 flush_metadata_updates(st);
12228 st->update_tail = &st->updates;
12229 } else {
12230 imsm_sync_metadata(st);
12231 free(update);
12232 }
12233 }
12234 ret_val = 0;
12235 exit:
12236 super->current_vol = tmp;
12237 return ret_val;
12238 }
12239
12240 /**
12241 * shape_to_geo() - fill geo_params from shape.
12242 *
12243 * @shape: array details.
12244 * @geo: new geometry params.
12245 * Returns: 0 on success, 1 otherwise.
12246 */
12247 static void shape_to_geo(struct shape *shape, struct geo_params *geo)
12248 {
12249 assert(shape);
12250 assert(geo);
12251
12252 geo->dev_name = shape->dev;
12253 geo->size = shape->size;
12254 geo->level = shape->level;
12255 geo->layout = shape->layout;
12256 geo->chunksize = shape->chunk;
12257 geo->raid_disks = shape->raiddisks;
12258 }
12259
12260 static int imsm_reshape_super(struct supertype *st, struct shape *shape, struct context *c)
12261 {
12262 int ret_val = 1;
12263 struct geo_params geo = {0};
12264
12265 dprintf("(enter)\n");
12266
12267 shape_to_geo(shape, &geo);
12268 strcpy(geo.devnm, st->devnm);
12269 if (shape->delta_disks != UnSet)
12270 geo.raid_disks += shape->delta_disks;
12271
12272 dprintf("for level : %i\n", geo.level);
12273 dprintf("for raid_disks : %i\n", geo.raid_disks);
12274
12275 if (strcmp(st->container_devnm, st->devnm) == 0) {
12276 /* On container level we can only increase number of devices. */
12277 dprintf("imsm: info: Container operation\n");
12278 int old_raid_disks = 0;
12279
12280 if (imsm_reshape_is_allowed_on_container(
12281 st, &geo, &old_raid_disks, shape->direction)) {
12282 struct imsm_update_reshape *u = NULL;
12283 int len;
12284
12285 if (imsm_fix_size_mismatch(st, -1)) {
12286 dprintf("imsm: Cannot fix size mismatch\n");
12287 goto exit_imsm_reshape_super;
12288 }
12289
12290 len = imsm_create_metadata_update_for_reshape(
12291 st, &geo, old_raid_disks, &u);
12292
12293 if (len <= 0) {
12294 dprintf("imsm: Cannot prepare update\n");
12295 goto exit_imsm_reshape_super;
12296 }
12297
12298 ret_val = 0;
12299 /* update metadata locally */
12300 imsm_update_metadata_locally(st, u, len);
12301 /* and possibly remotely */
12302 if (st->update_tail)
12303 append_metadata_update(st, u, len);
12304 else
12305 free(u);
12306
12307 } else {
12308 pr_err("(imsm) Operation is not allowed on this container\n");
12309 }
12310 } else {
12311 /* On volume level we support following operations
12312 * - takeover: raid10 -> raid0; raid0 -> raid10
12313 * - chunk size migration
12314 * - migration: raid5 -> raid0; raid0 -> raid5
12315 */
12316 struct intel_super *super = st->sb;
12317 struct intel_dev *dev = super->devlist;
12318 int change;
12319 dprintf("imsm: info: Volume operation\n");
12320 /* find requested device */
12321 while (dev) {
12322 char *devnm =
12323 imsm_find_array_devnm_by_subdev(
12324 dev->index, st->container_devnm);
12325 if (devnm && strcmp(devnm, geo.devnm) == 0)
12326 break;
12327 dev = dev->next;
12328 }
12329 if (dev == NULL) {
12330 pr_err("Cannot find %s (%s) subarray\n",
12331 geo.dev_name, geo.devnm);
12332 goto exit_imsm_reshape_super;
12333 }
12334 super->current_vol = dev->index;
12335 change = imsm_analyze_change(st, &geo, shape->direction, c);
12336 switch (change) {
12337 case CH_TAKEOVER:
12338 ret_val = imsm_takeover(st, &geo);
12339 break;
12340 case CH_MIGRATION: {
12341 struct imsm_update_reshape_migration *u = NULL;
12342 int len =
12343 imsm_create_metadata_update_for_migration(
12344 st, &geo, &u);
12345 if (len < 1) {
12346 dprintf("imsm: Cannot prepare update\n");
12347 break;
12348 }
12349 ret_val = 0;
12350 /* update metadata locally */
12351 imsm_update_metadata_locally(st, u, len);
12352 /* and possibly remotely */
12353 if (st->update_tail)
12354 append_metadata_update(st, u, len);
12355 else
12356 free(u);
12357 }
12358 break;
12359 case CH_ARRAY_SIZE: {
12360 struct imsm_update_size_change *u = NULL;
12361 int len =
12362 imsm_create_metadata_update_for_size_change(
12363 st, &geo, &u);
12364 if (len < 1) {
12365 dprintf("imsm: Cannot prepare update\n");
12366 break;
12367 }
12368 ret_val = 0;
12369 /* update metadata locally */
12370 imsm_update_metadata_locally(st, u, len);
12371 /* and possibly remotely */
12372 if (st->update_tail)
12373 append_metadata_update(st, u, len);
12374 else
12375 free(u);
12376 }
12377 break;
12378 case CH_ABORT:
12379 default:
12380 ret_val = 1;
12381 }
12382 }
12383
12384 exit_imsm_reshape_super:
12385 dprintf("imsm: reshape_super Exit code = %i\n", ret_val);
12386 return ret_val;
12387 }
12388
12389 #define COMPLETED_OK 0
12390 #define COMPLETED_NONE 1
12391 #define COMPLETED_DELAYED 2
12392
12393 static int read_completed(int fd, unsigned long long *val)
12394 {
12395 int ret;
12396 char buf[SYSFS_MAX_BUF_SIZE];
12397
12398 ret = sysfs_fd_get_str(fd, buf, sizeof(buf));
12399 if (ret < 0)
12400 return ret;
12401
12402 ret = COMPLETED_OK;
12403 if (str_is_none(buf) == true) {
12404 ret = COMPLETED_NONE;
12405 } else if (strncmp(buf, "delayed", 7) == 0) {
12406 ret = COMPLETED_DELAYED;
12407 } else {
12408 char *ep;
12409 *val = strtoull(buf, &ep, 0);
12410 if (ep == buf || (*ep != 0 && *ep != '\n' && *ep != ' '))
12411 ret = -1;
12412 }
12413 return ret;
12414 }
12415
12416 /*******************************************************************************
12417 * Function: wait_for_reshape_imsm
12418 * Description: Function writes new sync_max value and waits until
12419 * reshape process reach new position
12420 * Parameters:
12421 * sra : general array info
12422 * ndata : number of disks in new array's layout
12423 * Returns:
12424 * 0 : success,
12425 * 1 : there is no reshape in progress,
12426 * -1 : fail
12427 ******************************************************************************/
12428 int wait_for_reshape_imsm(struct mdinfo *sra, int ndata)
12429 {
12430 int fd = sysfs_get_fd(sra, NULL, "sync_completed");
12431 int retry = 3;
12432 unsigned long long completed;
12433 /* to_complete : new sync_max position */
12434 unsigned long long to_complete = sra->reshape_progress;
12435 unsigned long long position_to_set = to_complete / ndata;
12436
12437 if (!is_fd_valid(fd)) {
12438 dprintf("cannot open reshape_position\n");
12439 return 1;
12440 }
12441
12442 do {
12443 if (sysfs_fd_get_ll(fd, &completed) < 0) {
12444 if (!retry) {
12445 dprintf("cannot read reshape_position (no reshape in progres)\n");
12446 close(fd);
12447 return 1;
12448 }
12449 sleep_for(0, MSEC_TO_NSEC(30), true);
12450 } else
12451 break;
12452 } while (retry--);
12453
12454 if (completed > position_to_set) {
12455 dprintf("wrong next position to set %llu (%llu)\n",
12456 to_complete, position_to_set);
12457 close(fd);
12458 return -1;
12459 }
12460 dprintf("Position set: %llu\n", position_to_set);
12461 if (sysfs_set_num(sra, NULL, "sync_max",
12462 position_to_set) != 0) {
12463 dprintf("cannot set reshape position to %llu\n",
12464 position_to_set);
12465 close(fd);
12466 return -1;
12467 }
12468
12469 do {
12470 int rc;
12471 char action[SYSFS_MAX_BUF_SIZE];
12472 int timeout = 3000;
12473
12474 sysfs_wait(fd, &timeout);
12475 if (sysfs_get_str(sra, NULL, "sync_action",
12476 action, sizeof(action)) > 0 &&
12477 strncmp(action, "reshape", 7) != 0) {
12478 if (strncmp(action, "idle", 4) == 0)
12479 break;
12480 close(fd);
12481 return -1;
12482 }
12483
12484 rc = read_completed(fd, &completed);
12485 if (rc < 0) {
12486 dprintf("cannot read reshape_position (in loop)\n");
12487 close(fd);
12488 return 1;
12489 } else if (rc == COMPLETED_NONE)
12490 break;
12491 } while (completed < position_to_set);
12492
12493 close(fd);
12494 return 0;
12495 }
12496
12497 /*******************************************************************************
12498 * Function: check_degradation_change
12499 * Description: Check that array hasn't become failed.
12500 * Parameters:
12501 * info : for sysfs access
12502 * sources : source disks descriptors
12503 * degraded: previous degradation level
12504 * Returns:
12505 * degradation level
12506 ******************************************************************************/
12507 int check_degradation_change(struct mdinfo *info,
12508 int *sources,
12509 int degraded)
12510 {
12511 unsigned long long new_degraded;
12512 int rv;
12513
12514 rv = sysfs_get_ll(info, NULL, "degraded", &new_degraded);
12515 if (rv == -1 || (new_degraded != (unsigned long long)degraded)) {
12516 /* check each device to ensure it is still working */
12517 struct mdinfo *sd;
12518 new_degraded = 0;
12519 for (sd = info->devs ; sd ; sd = sd->next) {
12520 if (sd->disk.state & (1<<MD_DISK_FAULTY))
12521 continue;
12522 if (sd->disk.state & (1<<MD_DISK_SYNC)) {
12523 char sbuf[SYSFS_MAX_BUF_SIZE];
12524 int raid_disk = sd->disk.raid_disk;
12525
12526 if (sysfs_get_str(info,
12527 sd, "state", sbuf, sizeof(sbuf)) < 0 ||
12528 strstr(sbuf, "faulty") ||
12529 strstr(sbuf, "in_sync") == NULL) {
12530 /* this device is dead */
12531 sd->disk.state = (1<<MD_DISK_FAULTY);
12532 if (raid_disk >= 0)
12533 close_fd(&sources[raid_disk]);
12534 new_degraded++;
12535 }
12536 }
12537 }
12538 }
12539
12540 return new_degraded;
12541 }
12542
12543 /*******************************************************************************
12544 * Function: imsm_manage_reshape
12545 * Description: Function finds array under reshape and it manages reshape
12546 * process. It creates stripes backups (if required) and sets
12547 * checkpoints.
12548 * Parameters:
12549 * afd : Backup handle (nattive) - not used
12550 * sra : general array info
12551 * reshape : reshape parameters - not used
12552 * st : supertype structure
12553 * blocks : size of critical section [blocks]
12554 * fds : table of source device descriptor
12555 * offsets : start of array (offest per devices)
12556 * dests : not used
12557 * destfd : table of destination device descriptor
12558 * destoffsets : table of destination offsets (per device)
12559 * Returns:
12560 * 1 : success, reshape is done
12561 * 0 : fail
12562 ******************************************************************************/
12563 static int imsm_manage_reshape(
12564 int afd, struct mdinfo *sra, struct reshape *reshape,
12565 struct supertype *st, unsigned long backup_blocks,
12566 int *fds, unsigned long long *offsets,
12567 int dests, int *destfd, unsigned long long *destoffsets)
12568 {
12569 int ret_val = 0;
12570 struct intel_super *super = st->sb;
12571 struct intel_dev *dv;
12572 unsigned int sector_size = super->sector_size;
12573 struct imsm_dev *dev = NULL;
12574 struct imsm_map *map_src, *map_dest;
12575 int migr_vol_qan = 0;
12576 int ndata, odata; /* [bytes] */
12577 int chunk; /* [bytes] */
12578 struct migr_record *migr_rec;
12579 char *buf = NULL;
12580 unsigned int buf_size; /* [bytes] */
12581 unsigned long long max_position; /* array size [bytes] */
12582 unsigned long long next_step; /* [blocks]/[bytes] */
12583 unsigned long long old_data_stripe_length;
12584 unsigned long long start_src; /* [bytes] */
12585 unsigned long long start; /* [bytes] */
12586 unsigned long long start_buf_shift; /* [bytes] */
12587 int degraded = 0;
12588 int source_layout = 0;
12589 int subarray_index = -1;
12590
12591 if (!sra)
12592 return ret_val;
12593
12594 if (!fds || !offsets)
12595 goto abort;
12596
12597 /* Find volume during the reshape */
12598 for (dv = super->devlist; dv; dv = dv->next) {
12599 if (dv->dev->vol.migr_type == MIGR_GEN_MIGR &&
12600 dv->dev->vol.migr_state == 1) {
12601 dev = dv->dev;
12602 migr_vol_qan++;
12603 subarray_index = dv->index;
12604 }
12605 }
12606 /* Only one volume can migrate at the same time */
12607 if (migr_vol_qan != 1) {
12608 pr_err("%s", migr_vol_qan ?
12609 "Number of migrating volumes greater than 1\n" :
12610 "There is no volume during migrationg\n");
12611 goto abort;
12612 }
12613
12614 map_dest = get_imsm_map(dev, MAP_0);
12615 map_src = get_imsm_map(dev, MAP_1);
12616 if (map_src == NULL)
12617 goto abort;
12618
12619 ndata = imsm_num_data_members(map_dest);
12620 odata = imsm_num_data_members(map_src);
12621
12622 chunk = __le16_to_cpu(map_src->blocks_per_strip) * 512;
12623 old_data_stripe_length = odata * chunk;
12624
12625 migr_rec = super->migr_rec;
12626
12627 /* initialize migration record for start condition */
12628 if (sra->reshape_progress == 0)
12629 init_migr_record_imsm(st, dev, sra);
12630 else {
12631 if (__le32_to_cpu(migr_rec->rec_status) != UNIT_SRC_NORMAL) {
12632 pr_err("imsm: Cannot restart migration when data are present in copy area.\n"
12633 " Reassemble array to try to restore critical sector.\n");
12634 goto abort;
12635 }
12636 /* Save checkpoint to update migration record for current
12637 * reshape position (in md). It can be farther than current
12638 * reshape position in metadata.
12639 */
12640 if (save_checkpoint_imsm(st, sra, UNIT_SRC_NORMAL) == 1) {
12641 /* ignore error == 2, this can mean end of reshape here
12642 */
12643 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL, initial save)\n");
12644 goto abort;
12645 }
12646 }
12647
12648 /* size for data */
12649 buf_size = __le32_to_cpu(migr_rec->blocks_per_unit) * 512;
12650 /* extend buffer size for parity disk */
12651 buf_size += __le32_to_cpu(migr_rec->dest_depth_per_unit) * 512;
12652 /* add space for stripe alignment */
12653 buf_size += old_data_stripe_length;
12654 if (posix_memalign((void **)&buf, MAX_SECTOR_SIZE, buf_size)) {
12655 dprintf("imsm: Cannot allocate checkpoint buffer\n");
12656 goto abort;
12657 }
12658
12659 max_position = sra->component_size * ndata;
12660 source_layout = imsm_level_to_layout(map_src->raid_level);
12661
12662 while (current_migr_unit(migr_rec) <
12663 get_num_migr_units(migr_rec)) {
12664 /* current reshape position [blocks] */
12665 unsigned long long current_position =
12666 __le32_to_cpu(migr_rec->blocks_per_unit)
12667 * current_migr_unit(migr_rec);
12668 unsigned long long border;
12669
12670 /* Check that array hasn't become failed.
12671 */
12672 degraded = check_degradation_change(sra, fds, degraded);
12673 if (degraded > 1) {
12674 dprintf("imsm: Abort reshape due to degradation level (%i)\n", degraded);
12675 goto abort;
12676 }
12677
12678 next_step = __le32_to_cpu(migr_rec->blocks_per_unit);
12679
12680 if ((current_position + next_step) > max_position)
12681 next_step = max_position - current_position;
12682
12683 start = current_position * 512;
12684
12685 /* align reading start to old geometry */
12686 start_buf_shift = start % old_data_stripe_length;
12687 start_src = start - start_buf_shift;
12688
12689 border = (start_src / odata) - (start / ndata);
12690 border /= 512;
12691 if (border <= __le32_to_cpu(migr_rec->dest_depth_per_unit)) {
12692 /* save critical stripes to buf
12693 * start - start address of current unit
12694 * to backup [bytes]
12695 * start_src - start address of current unit
12696 * to backup alligned to source array
12697 * [bytes]
12698 */
12699 unsigned long long next_step_filler;
12700 unsigned long long copy_length = next_step * 512;
12701
12702 /* allign copy area length to stripe in old geometry */
12703 next_step_filler = ((copy_length + start_buf_shift)
12704 % old_data_stripe_length);
12705 if (next_step_filler)
12706 next_step_filler = (old_data_stripe_length
12707 - next_step_filler);
12708 dprintf("save_stripes() parameters: start = %llu,\tstart_src = %llu,\tnext_step*512 = %llu,\tstart_in_buf_shift = %llu,\tnext_step_filler = %llu\n",
12709 start, start_src, copy_length,
12710 start_buf_shift, next_step_filler);
12711
12712 if (save_stripes(fds, offsets, map_src->num_members,
12713 chunk, map_src->raid_level,
12714 source_layout, 0, NULL, start_src,
12715 copy_length +
12716 next_step_filler + start_buf_shift,
12717 buf)) {
12718 dprintf("imsm: Cannot save stripes to buffer\n");
12719 goto abort;
12720 }
12721 /* Convert data to destination format and store it
12722 * in backup general migration area
12723 */
12724 if (save_backup_imsm(st, dev, sra,
12725 buf + start_buf_shift, copy_length)) {
12726 dprintf("imsm: Cannot save stripes to target devices\n");
12727 goto abort;
12728 }
12729 if (save_checkpoint_imsm(st, sra,
12730 UNIT_SRC_IN_CP_AREA)) {
12731 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_IN_CP_AREA)\n");
12732 goto abort;
12733 }
12734 } else {
12735 /* set next step to use whole border area */
12736 border /= next_step;
12737 if (border > 1)
12738 next_step *= border;
12739 }
12740 /* When data backed up, checkpoint stored,
12741 * kick the kernel to reshape unit of data
12742 */
12743 next_step = next_step + sra->reshape_progress;
12744 /* limit next step to array max position */
12745 if (next_step > max_position)
12746 next_step = max_position;
12747 sysfs_set_num(sra, NULL, "suspend_lo", sra->reshape_progress);
12748 sysfs_set_num(sra, NULL, "suspend_hi", next_step);
12749 sra->reshape_progress = next_step;
12750
12751 /* wait until reshape finish */
12752 if (wait_for_reshape_imsm(sra, ndata)) {
12753 dprintf("wait_for_reshape_imsm returned error!\n");
12754 goto abort;
12755 }
12756
12757 if (save_checkpoint_imsm(st, sra, UNIT_SRC_NORMAL) == 1) {
12758 /* ignore error == 2, this can mean end of reshape here
12759 */
12760 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL)\n");
12761 goto abort;
12762 }
12763
12764 if (sigterm)
12765 goto abort;
12766
12767 }
12768
12769 /* clear migr_rec on disks after successful migration */
12770 struct dl *d;
12771
12772 memset(super->migr_rec_buf, 0, MIGR_REC_BUF_SECTORS*MAX_SECTOR_SIZE);
12773 for (d = super->disks; d; d = d->next) {
12774 if (d->index < 0 || is_failed(&d->disk))
12775 continue;
12776 unsigned long long dsize;
12777
12778 get_dev_size(d->fd, NULL, &dsize);
12779 if (lseek64(d->fd, dsize - MIGR_REC_SECTOR_POSITION*sector_size,
12780 SEEK_SET) >= 0) {
12781 if ((unsigned int)write(d->fd, super->migr_rec_buf,
12782 MIGR_REC_BUF_SECTORS*sector_size) !=
12783 MIGR_REC_BUF_SECTORS*sector_size)
12784 perror("Write migr_rec failed");
12785 }
12786 }
12787
12788 /* return '1' if done */
12789 ret_val = 1;
12790
12791 /* After the reshape eliminate size mismatch in metadata.
12792 * Don't update md/component_size here, volume hasn't
12793 * to take whole space. It is allowed by kernel.
12794 * md/component_size will be set propoperly after next assembly.
12795 */
12796 imsm_fix_size_mismatch(st, subarray_index);
12797
12798 abort:
12799 free(buf);
12800 /* See Grow.c: abort_reshape() for further explanation */
12801 sysfs_set_num(sra, NULL, "suspend_lo", 0x7FFFFFFFFFFFFFFFULL);
12802 sysfs_set_num(sra, NULL, "suspend_hi", 0);
12803 sysfs_set_num(sra, NULL, "suspend_lo", 0);
12804
12805 return ret_val;
12806 }
12807
12808 /*******************************************************************************
12809 * Function: calculate_bitmap_min_chunksize
12810 * Description: Calculates the minimal valid bitmap chunk size
12811 * Parameters:
12812 * max_bits : indicate how many bits can be used for the bitmap
12813 * data_area_size : the size of the data area covered by the bitmap
12814 *
12815 * Returns:
12816 * The bitmap chunk size
12817 ******************************************************************************/
12818 static unsigned long long
12819 calculate_bitmap_min_chunksize(unsigned long long max_bits,
12820 unsigned long long data_area_size)
12821 {
12822 unsigned long long min_chunk =
12823 4096; /* sub-page chunks don't work yet.. */
12824 unsigned long long bits = data_area_size / min_chunk + 1;
12825
12826 while (bits > max_bits) {
12827 min_chunk *= 2;
12828 bits = (bits + 1) / 2;
12829 }
12830 return min_chunk;
12831 }
12832
12833 /*******************************************************************************
12834 * Function: calculate_bitmap_chunksize
12835 * Description: Calculates the bitmap chunk size for the given device
12836 * Parameters:
12837 * st : supertype information
12838 * dev : device for the bitmap
12839 *
12840 * Returns:
12841 * The bitmap chunk size
12842 ******************************************************************************/
12843 static unsigned long long calculate_bitmap_chunksize(struct supertype *st,
12844 struct imsm_dev *dev)
12845 {
12846 struct intel_super *super = st->sb;
12847 unsigned long long min_chunksize;
12848 unsigned long long result = IMSM_DEFAULT_BITMAP_CHUNKSIZE;
12849 size_t dev_size = imsm_dev_size(dev);
12850
12851 min_chunksize = calculate_bitmap_min_chunksize(
12852 IMSM_BITMAP_AREA_SIZE * super->sector_size, dev_size);
12853
12854 if (result < min_chunksize)
12855 result = min_chunksize;
12856
12857 return result;
12858 }
12859
12860 /*******************************************************************************
12861 * Function: init_bitmap_header
12862 * Description: Initialize the bitmap header structure
12863 * Parameters:
12864 * st : supertype information
12865 * bms : bitmap header struct to initialize
12866 * dev : device for the bitmap
12867 *
12868 * Returns:
12869 * 0 : success
12870 * -1 : fail
12871 ******************************************************************************/
12872 static int init_bitmap_header(struct supertype *st, struct bitmap_super_s *bms,
12873 struct imsm_dev *dev)
12874 {
12875 int vol_uuid[4];
12876
12877 if (!bms || !dev)
12878 return -1;
12879
12880 bms->magic = __cpu_to_le32(BITMAP_MAGIC);
12881 bms->version = __cpu_to_le32(BITMAP_MAJOR_HI);
12882 bms->daemon_sleep = __cpu_to_le32(IMSM_DEFAULT_BITMAP_DAEMON_SLEEP);
12883 bms->sync_size = __cpu_to_le64(IMSM_BITMAP_AREA_SIZE);
12884 bms->write_behind = __cpu_to_le32(0);
12885
12886 uuid_from_super_imsm(st, vol_uuid);
12887 memcpy(bms->uuid, vol_uuid, 16);
12888
12889 bms->chunksize = calculate_bitmap_chunksize(st, dev);
12890
12891 return 0;
12892 }
12893
12894 /*******************************************************************************
12895 * Function: validate_internal_bitmap_for_drive
12896 * Description: Verify if the bitmap header for a given drive.
12897 * Parameters:
12898 * st : supertype information
12899 * offset : The offset from the beginning of the drive where to look for
12900 * the bitmap header.
12901 * d : the drive info
12902 *
12903 * Returns:
12904 * 0 : success
12905 * -1 : fail
12906 ******************************************************************************/
12907 static int validate_internal_bitmap_for_drive(struct supertype *st,
12908 unsigned long long offset,
12909 struct dl *d)
12910 {
12911 struct intel_super *super = st->sb;
12912 int ret = -1;
12913 int vol_uuid[4];
12914 bitmap_super_t *bms;
12915 int fd;
12916
12917 if (!d)
12918 return -1;
12919
12920 void *read_buf;
12921
12922 if (posix_memalign(&read_buf, MAX_SECTOR_SIZE, IMSM_BITMAP_HEADER_SIZE))
12923 return -1;
12924
12925 fd = d->fd;
12926 if (!is_fd_valid(fd)) {
12927 fd = open(d->devname, O_RDONLY, 0);
12928
12929 if (!is_fd_valid(fd)) {
12930 dprintf("cannot open the device %s\n", d->devname);
12931 goto abort;
12932 }
12933 }
12934
12935 if (lseek64(fd, offset * super->sector_size, SEEK_SET) < 0)
12936 goto abort;
12937 if (read(fd, read_buf, IMSM_BITMAP_HEADER_SIZE) !=
12938 IMSM_BITMAP_HEADER_SIZE)
12939 goto abort;
12940
12941 uuid_from_super_imsm(st, vol_uuid);
12942
12943 bms = read_buf;
12944 if ((bms->magic != __cpu_to_le32(BITMAP_MAGIC)) ||
12945 (bms->version != __cpu_to_le32(BITMAP_MAJOR_HI)) ||
12946 (!same_uuid((int *)bms->uuid, vol_uuid, st->ss->swapuuid))) {
12947 dprintf("wrong bitmap header detected\n");
12948 goto abort;
12949 }
12950
12951 ret = 0;
12952 abort:
12953 if (!is_fd_valid(d->fd))
12954 close_fd(&fd);
12955
12956 if (read_buf)
12957 free(read_buf);
12958
12959 return ret;
12960 }
12961
12962 /*******************************************************************************
12963 * Function: validate_internal_bitmap_imsm
12964 * Description: Verify if the bitmap header is in place and with proper data.
12965 * Parameters:
12966 * st : supertype information
12967 *
12968 * Returns:
12969 * 0 : success or device w/o RWH_BITMAP
12970 * -1 : fail
12971 ******************************************************************************/
12972 static int validate_internal_bitmap_imsm(struct supertype *st)
12973 {
12974 struct intel_super *super = st->sb;
12975 struct imsm_dev *dev = get_imsm_dev(super, super->current_vol);
12976 unsigned long long offset;
12977 struct dl *d;
12978
12979 if (dev->rwh_policy != RWH_BITMAP)
12980 return 0;
12981
12982 offset = get_bitmap_header_sector(super, super->current_vol);
12983 for (d = super->disks; d; d = d->next) {
12984 if (d->index < 0 || is_failed(&d->disk))
12985 continue;
12986
12987 if (validate_internal_bitmap_for_drive(st, offset, d)) {
12988 pr_err("imsm: bitmap validation failed\n");
12989 return -1;
12990 }
12991 }
12992 return 0;
12993 }
12994
12995 /*******************************************************************************
12996 * Function: add_internal_bitmap_imsm
12997 * Description: Mark the volume to use the bitmap and updates the chunk size value.
12998 * Parameters:
12999 * st : supertype information
13000 * chunkp : bitmap chunk size
13001 * delay : not used for imsm
13002 * write_behind : not used for imsm
13003 * size : not used for imsm
13004 * may_change : not used for imsm
13005 * amajor : not used for imsm
13006 *
13007 * Returns:
13008 * 0 : success
13009 * -1 : fail
13010 ******************************************************************************/
13011 static int add_internal_bitmap_imsm(struct supertype *st, int *chunkp,
13012 int delay, int write_behind,
13013 unsigned long long size, int may_change,
13014 int amajor)
13015 {
13016 struct intel_super *super = st->sb;
13017 int vol_idx = super->current_vol;
13018 struct imsm_dev *dev;
13019
13020 if (!super->devlist || vol_idx == -1 || !chunkp)
13021 return -1;
13022
13023 dev = get_imsm_dev(super, vol_idx);
13024 dev->rwh_policy = RWH_BITMAP;
13025 *chunkp = calculate_bitmap_chunksize(st, dev);
13026 return 0;
13027 }
13028
13029 /*******************************************************************************
13030 * Function: locate_bitmap_imsm
13031 * Description: Seek 'fd' to start of write-intent-bitmap.
13032 * Parameters:
13033 * st : supertype information
13034 * fd : file descriptor for the device
13035 * node_num : not used for imsm
13036 *
13037 * Returns:
13038 * 0 : success
13039 * -1 : fail
13040 ******************************************************************************/
13041 static int locate_bitmap_imsm(struct supertype *st, int fd, int node_num)
13042 {
13043 struct intel_super *super = st->sb;
13044 unsigned long long offset;
13045 int vol_idx = super->current_vol;
13046
13047 if (!super->devlist || vol_idx == -1)
13048 return -1;
13049
13050 offset = get_bitmap_header_sector(super, super->current_vol);
13051 dprintf("bitmap header offset is %llu\n", offset);
13052
13053 lseek64(fd, offset << 9, 0);
13054
13055 return 0;
13056 }
13057
13058 /*******************************************************************************
13059 * Function: write_init_bitmap_imsm
13060 * Description: Write a bitmap header and prepares the area for the bitmap.
13061 * Parameters:
13062 * st : supertype information
13063 * fd : file descriptor for the device
13064 * update : not used for imsm
13065 *
13066 * Returns:
13067 * 0 : success
13068 * -1 : fail
13069 ******************************************************************************/
13070 static int write_init_bitmap_imsm(struct supertype *st, int fd,
13071 enum bitmap_update update)
13072 {
13073 struct intel_super *super = st->sb;
13074 int vol_idx = super->current_vol;
13075 int ret = 0;
13076 unsigned long long offset;
13077 bitmap_super_t bms = { 0 };
13078 size_t written = 0;
13079 size_t to_write;
13080 ssize_t rv_num;
13081 void *buf;
13082
13083 if (!super->devlist || !super->sector_size || vol_idx == -1)
13084 return -1;
13085
13086 struct imsm_dev *dev = get_imsm_dev(super, vol_idx);
13087
13088 /* first clear the space for bitmap header */
13089 unsigned long long bitmap_area_start =
13090 get_bitmap_header_sector(super, vol_idx);
13091
13092 dprintf("zeroing area start (%llu) and size (%u)\n", bitmap_area_start,
13093 IMSM_BITMAP_AND_HEADER_SIZE / super->sector_size);
13094 if (zero_disk_range(fd, bitmap_area_start,
13095 IMSM_BITMAP_HEADER_SIZE / super->sector_size)) {
13096 pr_err("imsm: cannot zeroing the space for the bitmap\n");
13097 return -1;
13098 }
13099
13100 /* The bitmap area should be filled with "1"s to perform initial
13101 * synchronization.
13102 */
13103 if (posix_memalign(&buf, MAX_SECTOR_SIZE, MAX_SECTOR_SIZE))
13104 return -1;
13105 memset(buf, 0xFF, MAX_SECTOR_SIZE);
13106 offset = get_bitmap_sector(super, vol_idx);
13107 lseek64(fd, offset << 9, 0);
13108 while (written < IMSM_BITMAP_AREA_SIZE) {
13109 to_write = IMSM_BITMAP_AREA_SIZE - written;
13110 if (to_write > MAX_SECTOR_SIZE)
13111 to_write = MAX_SECTOR_SIZE;
13112 rv_num = write(fd, buf, MAX_SECTOR_SIZE);
13113 if (rv_num != MAX_SECTOR_SIZE) {
13114 ret = -1;
13115 dprintf("cannot initialize bitmap area\n");
13116 goto abort;
13117 }
13118 written += rv_num;
13119 }
13120
13121 /* write a bitmap header */
13122 init_bitmap_header(st, &bms, dev);
13123 memset(buf, 0, MAX_SECTOR_SIZE);
13124 memcpy(buf, &bms, sizeof(bitmap_super_t));
13125 if (locate_bitmap_imsm(st, fd, 0)) {
13126 ret = -1;
13127 dprintf("cannot locate the bitmap\n");
13128 goto abort;
13129 }
13130 if (write(fd, buf, MAX_SECTOR_SIZE) != MAX_SECTOR_SIZE) {
13131 ret = -1;
13132 dprintf("cannot write the bitmap header\n");
13133 goto abort;
13134 }
13135 fsync(fd);
13136
13137 abort:
13138 free(buf);
13139
13140 return ret;
13141 }
13142
13143 /*******************************************************************************
13144 * Function: is_vol_to_setup_bitmap
13145 * Description: Checks if a bitmap should be activated on the dev.
13146 * Parameters:
13147 * info : info about the volume to setup the bitmap
13148 * dev : the device to check against bitmap creation
13149 *
13150 * Returns:
13151 * 0 : bitmap should be set up on the device
13152 * -1 : otherwise
13153 ******************************************************************************/
13154 static int is_vol_to_setup_bitmap(struct mdinfo *info, struct imsm_dev *dev)
13155 {
13156 if (!dev || !info)
13157 return -1;
13158
13159 if ((strcmp((char *)dev->volume, info->name) == 0) &&
13160 (dev->rwh_policy == RWH_BITMAP))
13161 return -1;
13162
13163 return 0;
13164 }
13165
13166 /*******************************************************************************
13167 * Function: set_bitmap_sysfs
13168 * Description: Set the sysfs atributes of a given volume to activate the bitmap.
13169 * Parameters:
13170 * info : info about the volume where the bitmap should be setup
13171 * chunksize : bitmap chunk size
13172 * location : location of the bitmap
13173 *
13174 * Returns:
13175 * 0 : success
13176 * -1 : fail
13177 ******************************************************************************/
13178 static int set_bitmap_sysfs(struct mdinfo *info, unsigned long long chunksize,
13179 char *location)
13180 {
13181 /* The bitmap/metadata is set to external to allow changing of value for
13182 * bitmap/location. When external is used, the kernel will treat an offset
13183 * related to the device's first lba (in opposition to the "internal" case
13184 * when this value is related to the beginning of the superblock).
13185 */
13186 if (sysfs_set_str(info, NULL, "bitmap/metadata", "external")) {
13187 dprintf("failed to set bitmap/metadata\n");
13188 return -1;
13189 }
13190
13191 /* It can only be changed when no bitmap is active.
13192 * Should be bigger than 512 and must be power of 2.
13193 * It is expecting the value in bytes.
13194 */
13195 if (sysfs_set_num(info, NULL, "bitmap/chunksize",
13196 __cpu_to_le32(chunksize))) {
13197 dprintf("failed to set bitmap/chunksize\n");
13198 return -1;
13199 }
13200
13201 /* It is expecting the value in sectors. */
13202 if (sysfs_set_num(info, NULL, "bitmap/space",
13203 __cpu_to_le64(IMSM_BITMAP_AREA_SIZE))) {
13204 dprintf("failed to set bitmap/space\n");
13205 return -1;
13206 }
13207
13208 /* Determines the delay between the bitmap updates.
13209 * It is expecting the value in seconds.
13210 */
13211 if (sysfs_set_num(info, NULL, "bitmap/time_base",
13212 __cpu_to_le64(IMSM_DEFAULT_BITMAP_DAEMON_SLEEP))) {
13213 dprintf("failed to set bitmap/time_base\n");
13214 return -1;
13215 }
13216
13217 /* It is expecting the value in sectors with a sign at the beginning. */
13218 if (sysfs_set_str(info, NULL, "bitmap/location", location)) {
13219 dprintf("failed to set bitmap/location\n");
13220 return -1;
13221 }
13222
13223 return 0;
13224 }
13225
13226 /*******************************************************************************
13227 * Function: set_bitmap_imsm
13228 * Description: Setup the bitmap for the given volume
13229 * Parameters:
13230 * st : supertype information
13231 * info : info about the volume where the bitmap should be setup
13232 *
13233 * Returns:
13234 * 0 : success
13235 * -1 : fail
13236 ******************************************************************************/
13237 static int set_bitmap_imsm(struct supertype *st, struct mdinfo *info)
13238 {
13239 struct intel_super *super = st->sb;
13240 int prev_current_vol = super->current_vol;
13241 struct imsm_dev *dev;
13242 int ret = -1;
13243 char location[16] = "";
13244 unsigned long long chunksize;
13245 struct intel_dev *dev_it;
13246
13247 for (dev_it = super->devlist; dev_it; dev_it = dev_it->next) {
13248 super->current_vol = dev_it->index;
13249 dev = get_imsm_dev(super, super->current_vol);
13250
13251 if (is_vol_to_setup_bitmap(info, dev)) {
13252 if (validate_internal_bitmap_imsm(st)) {
13253 dprintf("bitmap header validation failed\n");
13254 goto abort;
13255 }
13256
13257 chunksize = calculate_bitmap_chunksize(st, dev);
13258 dprintf("chunk size is %llu\n", chunksize);
13259
13260 snprintf(location, sizeof(location), "+%llu",
13261 get_bitmap_sector(super, super->current_vol));
13262 dprintf("bitmap offset is %s\n", location);
13263
13264 if (set_bitmap_sysfs(info, chunksize, location)) {
13265 dprintf("cannot setup the bitmap\n");
13266 goto abort;
13267 }
13268 }
13269 }
13270 ret = 0;
13271 abort:
13272 super->current_vol = prev_current_vol;
13273 return ret;
13274 }
13275
13276 struct superswitch super_imsm = {
13277 .examine_super = examine_super_imsm,
13278 .brief_examine_super = brief_examine_super_imsm,
13279 .brief_examine_subarrays = brief_examine_subarrays_imsm,
13280 .export_examine_super = export_examine_super_imsm,
13281 .detail_super = detail_super_imsm,
13282 .brief_detail_super = brief_detail_super_imsm,
13283 .write_init_super = write_init_super_imsm,
13284 .validate_geometry = validate_geometry_imsm,
13285 .add_to_super = add_to_super_imsm,
13286 .remove_from_super = remove_from_super_imsm,
13287 .detail_platform = detail_platform_imsm,
13288 .export_detail_platform = export_detail_platform_imsm,
13289 .kill_subarray = kill_subarray_imsm,
13290 .update_subarray = update_subarray_imsm,
13291 .load_container = load_container_imsm,
13292 .default_geometry = default_geometry_imsm,
13293 .test_and_add_drive_policies = test_and_add_drive_policies_imsm,
13294 .reshape_super = imsm_reshape_super,
13295 .manage_reshape = imsm_manage_reshape,
13296 .recover_backup = recover_backup_imsm,
13297 .examine_badblocks = examine_badblocks_imsm,
13298 .match_home = match_home_imsm,
13299 .uuid_from_super= uuid_from_super_imsm,
13300 .getinfo_super = getinfo_super_imsm,
13301 .getinfo_super_disks = getinfo_super_disks_imsm,
13302 .update_super = update_super_imsm,
13303
13304 .avail_size = avail_size_imsm,
13305 .get_spare_criteria = get_spare_criteria_imsm,
13306
13307 .compare_super = compare_super_imsm,
13308
13309 .load_super = load_super_imsm,
13310 .init_super = init_super_imsm,
13311 .store_super = store_super_imsm,
13312 .free_super = free_super_imsm,
13313 .match_metadata_desc = match_metadata_desc_imsm,
13314 .container_content = container_content_imsm,
13315 .validate_container = validate_container_imsm,
13316
13317 .add_internal_bitmap = add_internal_bitmap_imsm,
13318 .locate_bitmap = locate_bitmap_imsm,
13319 .write_bitmap = write_init_bitmap_imsm,
13320 .set_bitmap = set_bitmap_imsm,
13321
13322 .write_init_ppl = write_init_ppl_imsm,
13323 .validate_ppl = validate_ppl_imsm,
13324
13325 .external = 1,
13326 .swapuuid = 0,
13327 .name = "imsm",
13328
13329 /* for mdmon */
13330 .open_new = imsm_open_new,
13331 .set_array_state= imsm_set_array_state,
13332 .set_disk = imsm_set_disk,
13333 .sync_metadata = imsm_sync_metadata,
13334 .activate_spare = imsm_activate_spare,
13335 .process_update = imsm_process_update,
13336 .prepare_update = imsm_prepare_update,
13337 .record_bad_block = imsm_record_badblock,
13338 .clear_bad_block = imsm_clear_badblock,
13339 .get_bad_blocks = imsm_get_badblocks,
13340 };