]> git.ipfire.org Git - thirdparty/mdadm.git/blob - super-intel.c
Create.c: fix uclibc build
[thirdparty/mdadm.git] / super-intel.c
1 /*
2 * mdadm - Intel(R) Matrix Storage Manager Support
3 *
4 * Copyright (C) 2002-2008 Intel Corporation
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #define HAVE_STDINT_H 1
21 #include "mdadm.h"
22 #include "mdmon.h"
23 #include "dlink.h"
24 #include "sha1.h"
25 #include "platform-intel.h"
26 #include <values.h>
27 #include <scsi/sg.h>
28 #include <ctype.h>
29 #include <dirent.h>
30 #include "drive_encryption.h"
31
32 /* MPB == Metadata Parameter Block */
33 #define MPB_SIGNATURE "Intel Raid ISM Cfg Sig. "
34 #define MPB_SIG_LEN (strlen(MPB_SIGNATURE))
35 #define MPB_VERSION_RAID0 "1.0.00"
36 #define MPB_VERSION_RAID1 "1.1.00"
37 #define MPB_VERSION_MANY_VOLUMES_PER_ARRAY "1.2.00"
38 #define MPB_VERSION_3OR4_DISK_ARRAY "1.2.01"
39 #define MPB_VERSION_RAID5 "1.2.02"
40 #define MPB_VERSION_5OR6_DISK_ARRAY "1.2.04"
41 #define MPB_VERSION_CNG "1.2.06"
42 #define MPB_VERSION_ATTRIBS "1.3.00"
43 #define MAX_SIGNATURE_LENGTH 32
44 #define MAX_RAID_SERIAL_LEN 16
45
46 /* supports RAID0 */
47 #define MPB_ATTRIB_RAID0 __cpu_to_le32(0x00000001)
48 /* supports RAID1 */
49 #define MPB_ATTRIB_RAID1 __cpu_to_le32(0x00000002)
50 /* supports RAID10 */
51 #define MPB_ATTRIB_RAID10 __cpu_to_le32(0x00000004)
52 /* supports RAID1E */
53 #define MPB_ATTRIB_RAID1E __cpu_to_le32(0x00000008)
54 /* supports RAID5 */
55 #define MPB_ATTRIB_RAID5 __cpu_to_le32(0x00000010)
56 /* supports RAID CNG */
57 #define MPB_ATTRIB_RAIDCNG __cpu_to_le32(0x00000020)
58 /* supports expanded stripe sizes of 256K, 512K and 1MB */
59 #define MPB_ATTRIB_EXP_STRIPE_SIZE __cpu_to_le32(0x00000040)
60
61 /* The OROM Support RST Caching of Volumes */
62 #define MPB_ATTRIB_NVM __cpu_to_le32(0x02000000)
63 /* The OROM supports creating disks greater than 2TB */
64 #define MPB_ATTRIB_2TB_DISK __cpu_to_le32(0x04000000)
65 /* The OROM supports Bad Block Management */
66 #define MPB_ATTRIB_BBM __cpu_to_le32(0x08000000)
67
68 /* THe OROM Supports NVM Caching of Volumes */
69 #define MPB_ATTRIB_NEVER_USE2 __cpu_to_le32(0x10000000)
70 /* The OROM supports creating volumes greater than 2TB */
71 #define MPB_ATTRIB_2TB __cpu_to_le32(0x20000000)
72 /* originally for PMP, now it's wasted b/c. Never use this bit! */
73 #define MPB_ATTRIB_NEVER_USE __cpu_to_le32(0x40000000)
74 /* Verify MPB contents against checksum after reading MPB */
75 #define MPB_ATTRIB_CHECKSUM_VERIFY __cpu_to_le32(0x80000000)
76
77 /* Define all supported attributes that have to be accepted by mdadm
78 */
79 #define MPB_ATTRIB_SUPPORTED (MPB_ATTRIB_CHECKSUM_VERIFY | \
80 MPB_ATTRIB_2TB | \
81 MPB_ATTRIB_2TB_DISK | \
82 MPB_ATTRIB_RAID0 | \
83 MPB_ATTRIB_RAID1 | \
84 MPB_ATTRIB_RAID10 | \
85 MPB_ATTRIB_RAID5 | \
86 MPB_ATTRIB_EXP_STRIPE_SIZE | \
87 MPB_ATTRIB_BBM)
88
89 /* Define attributes that are unused but not harmful */
90 #define MPB_ATTRIB_IGNORED (MPB_ATTRIB_NEVER_USE)
91
92 #define MPB_SECTOR_CNT 2210
93 #define IMSM_RESERVED_SECTORS 8192
94 #define NUM_BLOCKS_DIRTY_STRIPE_REGION 2048
95 #define SECT_PER_MB_SHIFT 11
96 #define MAX_SECTOR_SIZE 4096
97 #define MULTIPLE_PPL_AREA_SIZE_IMSM (1024 * 1024) /* Size of the whole
98 * mutliple PPL area
99 */
100
101 /*
102 * Internal Write-intent bitmap is stored in the same area where PPL.
103 * Both features are mutually exclusive, so it is not an issue.
104 * The first 8KiB of the area are reserved and shall not be used.
105 */
106 #define IMSM_BITMAP_AREA_RESERVED_SIZE 8192
107
108 #define IMSM_BITMAP_HEADER_OFFSET (IMSM_BITMAP_AREA_RESERVED_SIZE)
109 #define IMSM_BITMAP_HEADER_SIZE MAX_SECTOR_SIZE
110
111 #define IMSM_BITMAP_START_OFFSET (IMSM_BITMAP_HEADER_OFFSET + IMSM_BITMAP_HEADER_SIZE)
112 #define IMSM_BITMAP_AREA_SIZE (MULTIPLE_PPL_AREA_SIZE_IMSM - IMSM_BITMAP_START_OFFSET)
113 #define IMSM_BITMAP_AND_HEADER_SIZE (IMSM_BITMAP_AREA_SIZE + IMSM_BITMAP_HEADER_SIZE)
114
115 #define IMSM_DEFAULT_BITMAP_CHUNKSIZE (64 * 1024 * 1024)
116 #define IMSM_DEFAULT_BITMAP_DAEMON_SLEEP 5
117
118 /*
119 * This macro let's us ensure that no-one accidentally
120 * changes the size of a struct
121 */
122 #define ASSERT_SIZE(_struct, size) \
123 static inline void __assert_size_##_struct(void) \
124 { \
125 switch (0) { \
126 case 0: break; \
127 case (sizeof(struct _struct) == size): break; \
128 } \
129 }
130
131 /* Disk configuration info. */
132 #define IMSM_MAX_DEVICES 255
133 struct imsm_disk {
134 __u8 serial[MAX_RAID_SERIAL_LEN];/* 0xD8 - 0xE7 ascii serial number */
135 __u32 total_blocks_lo; /* 0xE8 - 0xEB total blocks lo */
136 __u32 scsi_id; /* 0xEC - 0xEF scsi ID */
137 #define SPARE_DISK __cpu_to_le32(0x01) /* Spare */
138 #define CONFIGURED_DISK __cpu_to_le32(0x02) /* Member of some RaidDev */
139 #define FAILED_DISK __cpu_to_le32(0x04) /* Permanent failure */
140 #define JOURNAL_DISK __cpu_to_le32(0x2000000) /* Device marked as Journaling Drive */
141 __u32 status; /* 0xF0 - 0xF3 */
142 __u32 owner_cfg_num; /* which config 0,1,2... owns this disk */
143 __u32 total_blocks_hi; /* 0xF4 - 0xF5 total blocks hi */
144 #define IMSM_DISK_FILLERS 3
145 __u32 filler[IMSM_DISK_FILLERS]; /* 0xF5 - 0x107 MPB_DISK_FILLERS for future expansion */
146 };
147 ASSERT_SIZE(imsm_disk, 48)
148
149 /* map selector for map managment
150 */
151 #define MAP_0 0
152 #define MAP_1 1
153 #define MAP_X -1
154
155 /* RAID map configuration infos. */
156 struct imsm_map {
157 __u32 pba_of_lba0_lo; /* start address of partition */
158 __u32 blocks_per_member_lo;/* blocks per member */
159 __u32 num_data_stripes_lo; /* number of data stripes */
160 __u16 blocks_per_strip;
161 __u8 map_state; /* Normal, Uninitialized, Degraded, Failed */
162 #define IMSM_T_STATE_NORMAL 0
163 #define IMSM_T_STATE_UNINITIALIZED 1
164 #define IMSM_T_STATE_DEGRADED 2
165 #define IMSM_T_STATE_FAILED 3
166 __u8 raid_level;
167 #define IMSM_T_RAID0 0
168 #define IMSM_T_RAID1 1
169 #define IMSM_T_RAID5 5 /* since metadata version 1.2.02 ? */
170 __u8 num_members; /* number of member disks */
171 __u8 num_domains; /* number of parity domains */
172 __u8 failed_disk_num; /* valid only when state is degraded */
173 __u8 ddf;
174 __u32 pba_of_lba0_hi;
175 __u32 blocks_per_member_hi;
176 __u32 num_data_stripes_hi;
177 __u32 filler[4]; /* expansion area */
178 #define IMSM_ORD_REBUILD (1 << 24)
179 __u32 disk_ord_tbl[1]; /* disk_ord_tbl[num_members],
180 * top byte contains some flags
181 */
182 };
183 ASSERT_SIZE(imsm_map, 52)
184
185 struct imsm_vol {
186 __u32 curr_migr_unit_lo;
187 __u32 checkpoint_id; /* id to access curr_migr_unit */
188 __u8 migr_state; /* Normal or Migrating */
189 #define MIGR_INIT 0
190 #define MIGR_REBUILD 1
191 #define MIGR_VERIFY 2 /* analagous to echo check > sync_action */
192 #define MIGR_GEN_MIGR 3
193 #define MIGR_STATE_CHANGE 4
194 #define MIGR_REPAIR 5
195 __u8 migr_type; /* Initializing, Rebuilding, ... */
196 #define RAIDVOL_CLEAN 0
197 #define RAIDVOL_DIRTY 1
198 #define RAIDVOL_DSRECORD_VALID 2
199 __u8 dirty;
200 __u8 fs_state; /* fast-sync state for CnG (0xff == disabled) */
201 __u16 verify_errors; /* number of mismatches */
202 __u16 bad_blocks; /* number of bad blocks during verify */
203 __u32 curr_migr_unit_hi;
204 __u32 filler[3];
205 struct imsm_map map[1];
206 /* here comes another one if migr_state */
207 };
208 ASSERT_SIZE(imsm_vol, 84)
209
210 struct imsm_dev {
211 __u8 volume[MAX_RAID_SERIAL_LEN];
212 __u32 size_low;
213 __u32 size_high;
214 #define DEV_BOOTABLE __cpu_to_le32(0x01)
215 #define DEV_BOOT_DEVICE __cpu_to_le32(0x02)
216 #define DEV_READ_COALESCING __cpu_to_le32(0x04)
217 #define DEV_WRITE_COALESCING __cpu_to_le32(0x08)
218 #define DEV_LAST_SHUTDOWN_DIRTY __cpu_to_le32(0x10)
219 #define DEV_HIDDEN_AT_BOOT __cpu_to_le32(0x20)
220 #define DEV_CURRENTLY_HIDDEN __cpu_to_le32(0x40)
221 #define DEV_VERIFY_AND_FIX __cpu_to_le32(0x80)
222 #define DEV_MAP_STATE_UNINIT __cpu_to_le32(0x100)
223 #define DEV_NO_AUTO_RECOVERY __cpu_to_le32(0x200)
224 #define DEV_CLONE_N_GO __cpu_to_le32(0x400)
225 #define DEV_CLONE_MAN_SYNC __cpu_to_le32(0x800)
226 #define DEV_CNG_MASTER_DISK_NUM __cpu_to_le32(0x1000)
227 __u32 status; /* Persistent RaidDev status */
228 __u32 reserved_blocks; /* Reserved blocks at beginning of volume */
229 __u8 migr_priority;
230 __u8 num_sub_vols;
231 __u8 tid;
232 __u8 cng_master_disk;
233 __u16 cache_policy;
234 __u8 cng_state;
235 __u8 cng_sub_state;
236 __u16 my_vol_raid_dev_num; /* Used in Unique volume Id for this RaidDev */
237
238 /* NVM_EN */
239 __u8 nv_cache_mode;
240 __u8 nv_cache_flags;
241
242 /* Unique Volume Id of the NvCache Volume associated with this volume */
243 __u32 nvc_vol_orig_family_num;
244 __u16 nvc_vol_raid_dev_num;
245
246 #define RWH_OFF 0
247 #define RWH_DISTRIBUTED 1
248 #define RWH_JOURNALING_DRIVE 2
249 #define RWH_MULTIPLE_DISTRIBUTED 3
250 #define RWH_MULTIPLE_PPLS_JOURNALING_DRIVE 4
251 #define RWH_MULTIPLE_OFF 5
252 #define RWH_BITMAP 6
253 __u8 rwh_policy; /* Raid Write Hole Policy */
254 __u8 jd_serial[MAX_RAID_SERIAL_LEN]; /* Journal Drive serial number */
255 __u8 filler1;
256
257 #define IMSM_DEV_FILLERS 3
258 __u32 filler[IMSM_DEV_FILLERS];
259 struct imsm_vol vol;
260 };
261 ASSERT_SIZE(imsm_dev, 164)
262
263 struct imsm_super {
264 __u8 sig[MAX_SIGNATURE_LENGTH]; /* 0x00 - 0x1F */
265 __u32 check_sum; /* 0x20 - 0x23 MPB Checksum */
266 __u32 mpb_size; /* 0x24 - 0x27 Size of MPB */
267 __u32 family_num; /* 0x28 - 0x2B Checksum from first time this config was written */
268 __u32 generation_num; /* 0x2C - 0x2F Incremented each time this array's MPB is written */
269 __u32 error_log_size; /* 0x30 - 0x33 in bytes */
270 __u32 attributes; /* 0x34 - 0x37 */
271 __u8 num_disks; /* 0x38 Number of configured disks */
272 __u8 num_raid_devs; /* 0x39 Number of configured volumes */
273 __u8 error_log_pos; /* 0x3A */
274 __u8 fill[1]; /* 0x3B */
275 __u32 cache_size; /* 0x3c - 0x40 in mb */
276 __u32 orig_family_num; /* 0x40 - 0x43 original family num */
277 __u32 pwr_cycle_count; /* 0x44 - 0x47 simulated power cycle count for array */
278 __u32 bbm_log_size; /* 0x48 - 0x4B - size of bad Block Mgmt Log in bytes */
279 __u16 num_raid_devs_created; /* 0x4C - 0x4D Used for generating unique
280 * volume IDs for raid_dev created in this array
281 * (starts at 1)
282 */
283 __u16 filler1; /* 0x4E - 0x4F */
284 __u64 creation_time; /* 0x50 - 0x57 Array creation time */
285 #define IMSM_FILLERS 32
286 __u32 filler[IMSM_FILLERS]; /* 0x58 - 0xD7 RAID_MPB_FILLERS */
287 struct imsm_disk disk[1]; /* 0xD8 diskTbl[numDisks] */
288 /* here comes imsm_dev[num_raid_devs] */
289 /* here comes BBM logs */
290 };
291 ASSERT_SIZE(imsm_super, 264)
292
293 #define BBM_LOG_MAX_ENTRIES 254
294 #define BBM_LOG_MAX_LBA_ENTRY_VAL 256 /* Represents 256 LBAs */
295 #define BBM_LOG_SIGNATURE 0xabadb10c
296
297 struct bbm_log_block_addr {
298 __u16 w1;
299 __u32 dw1;
300 } __attribute__ ((__packed__));
301
302 struct bbm_log_entry {
303 __u8 marked_count; /* Number of blocks marked - 1 */
304 __u8 disk_ordinal; /* Disk entry within the imsm_super */
305 struct bbm_log_block_addr defective_block_start;
306 } __attribute__ ((__packed__));
307
308 struct bbm_log {
309 __u32 signature; /* 0xABADB10C */
310 __u32 entry_count;
311 struct bbm_log_entry marked_block_entries[BBM_LOG_MAX_ENTRIES];
312 };
313 ASSERT_SIZE(bbm_log, 2040)
314
315 static char *map_state_str[] = { "normal", "uninitialized", "degraded", "failed" };
316
317 #define BLOCKS_PER_KB (1024/512)
318
319 #define RAID_DISK_RESERVED_BLOCKS_IMSM_HI 2209
320
321 #define GEN_MIGR_AREA_SIZE 2048 /* General Migration Copy Area size in blocks */
322
323 #define MIGR_REC_BUF_SECTORS 1 /* size of migr_record i/o buffer in sectors */
324 #define MIGR_REC_SECTOR_POSITION 1 /* migr_record position offset on disk,
325 * MIGR_REC_BUF_SECTORS <= MIGR_REC_SECTOR_POS
326 */
327
328 #define UNIT_SRC_NORMAL 0 /* Source data for curr_migr_unit must
329 * be recovered using srcMap */
330 #define UNIT_SRC_IN_CP_AREA 1 /* Source data for curr_migr_unit has
331 * already been migrated and must
332 * be recovered from checkpoint area */
333
334 #define PPL_ENTRY_SPACE (128 * 1024) /* Size of single PPL, without the header */
335
336 struct migr_record {
337 __u32 rec_status; /* Status used to determine how to restart
338 * migration in case it aborts
339 * in some fashion */
340 __u32 curr_migr_unit_lo; /* 0..numMigrUnits-1 */
341 __u32 family_num; /* Family number of MPB
342 * containing the RaidDev
343 * that is migrating */
344 __u32 ascending_migr; /* True if migrating in increasing
345 * order of lbas */
346 __u32 blocks_per_unit; /* Num disk blocks per unit of operation */
347 __u32 dest_depth_per_unit; /* Num member blocks each destMap
348 * member disk
349 * advances per unit-of-operation */
350 __u32 ckpt_area_pba_lo; /* Pba of first block of ckpt copy area */
351 __u32 dest_1st_member_lba_lo; /* First member lba on first
352 * stripe of destination */
353 __u32 num_migr_units_lo; /* Total num migration units-of-op */
354 __u32 post_migr_vol_cap; /* Size of volume after
355 * migration completes */
356 __u32 post_migr_vol_cap_hi; /* Expansion space for LBA64 */
357 __u32 ckpt_read_disk_num; /* Which member disk in destSubMap[0] the
358 * migration ckpt record was read from
359 * (for recovered migrations) */
360 __u32 curr_migr_unit_hi; /* 0..numMigrUnits-1 high order 32 bits */
361 __u32 ckpt_area_pba_hi; /* Pba of first block of ckpt copy area
362 * high order 32 bits */
363 __u32 dest_1st_member_lba_hi; /* First member lba on first stripe of
364 * destination - high order 32 bits */
365 __u32 num_migr_units_hi; /* Total num migration units-of-op
366 * high order 32 bits */
367 __u32 filler[16];
368 };
369 ASSERT_SIZE(migr_record, 128)
370
371 /**
372 * enum imsm_status - internal IMSM return values representation.
373 * @STATUS_OK: function succeeded.
374 * @STATUS_ERROR: General error ocurred (not specified).
375 *
376 * Typedefed to imsm_status_t.
377 */
378 typedef enum imsm_status {
379 IMSM_STATUS_ERROR = -1,
380 IMSM_STATUS_OK = 0,
381 } imsm_status_t;
382
383 struct md_list {
384 /* usage marker:
385 * 1: load metadata
386 * 2: metadata does not match
387 * 4: already checked
388 */
389 int used;
390 char *devname;
391 int found;
392 int container;
393 dev_t st_rdev;
394 struct md_list *next;
395 };
396
397 static __u8 migr_type(struct imsm_dev *dev)
398 {
399 if (dev->vol.migr_type == MIGR_VERIFY &&
400 dev->status & DEV_VERIFY_AND_FIX)
401 return MIGR_REPAIR;
402 else
403 return dev->vol.migr_type;
404 }
405
406 static void set_migr_type(struct imsm_dev *dev, __u8 migr_type)
407 {
408 /* for compatibility with older oroms convert MIGR_REPAIR, into
409 * MIGR_VERIFY w/ DEV_VERIFY_AND_FIX status
410 */
411 if (migr_type == MIGR_REPAIR) {
412 dev->vol.migr_type = MIGR_VERIFY;
413 dev->status |= DEV_VERIFY_AND_FIX;
414 } else {
415 dev->vol.migr_type = migr_type;
416 dev->status &= ~DEV_VERIFY_AND_FIX;
417 }
418 }
419
420 static unsigned int sector_count(__u32 bytes, unsigned int sector_size)
421 {
422 return ROUND_UP(bytes, sector_size) / sector_size;
423 }
424
425 static unsigned int mpb_sectors(struct imsm_super *mpb,
426 unsigned int sector_size)
427 {
428 return sector_count(__le32_to_cpu(mpb->mpb_size), sector_size);
429 }
430
431 struct intel_dev {
432 struct imsm_dev *dev;
433 struct intel_dev *next;
434 unsigned index;
435 };
436
437 struct intel_hba {
438 enum sys_dev_type type;
439 char *path;
440 char *pci_id;
441 struct intel_hba *next;
442 };
443
444 enum action {
445 DISK_REMOVE = 1,
446 DISK_ADD
447 };
448 /* internal representation of IMSM metadata */
449 struct intel_super {
450 union {
451 void *buf; /* O_DIRECT buffer for reading/writing metadata */
452 struct imsm_super *anchor; /* immovable parameters */
453 };
454 union {
455 void *migr_rec_buf; /* buffer for I/O operations */
456 struct migr_record *migr_rec; /* migration record */
457 };
458 int clean_migration_record_by_mdmon; /* when reshape is switched to next
459 array, it indicates that mdmon is allowed to clean migration
460 record */
461 size_t len; /* size of the 'buf' allocation */
462 size_t extra_space; /* extra space in 'buf' that is not used yet */
463 void *next_buf; /* for realloc'ing buf from the manager */
464 size_t next_len;
465 int updates_pending; /* count of pending updates for mdmon */
466 int current_vol; /* index of raid device undergoing creation */
467 unsigned long long create_offset; /* common start for 'current_vol' */
468 __u32 random; /* random data for seeding new family numbers */
469 struct intel_dev *devlist;
470 unsigned int sector_size; /* sector size of used member drives */
471 struct dl {
472 struct dl *next;
473 int index;
474 __u8 serial[MAX_RAID_SERIAL_LEN];
475 int major, minor;
476 char *devname;
477 struct imsm_disk disk;
478 int fd;
479 int extent_cnt;
480 struct extent *e; /* for determining freespace @ create */
481 int raiddisk; /* slot to fill in autolayout */
482 enum action action;
483 } *disks, *current_disk;
484 struct dl *disk_mgmt_list; /* list of disks to add/remove while mdmon
485 active */
486 struct dl *missing; /* disks removed while we weren't looking */
487 struct bbm_log *bbm_log;
488 struct intel_hba *hba; /* device path of the raid controller for this metadata */
489 const struct imsm_orom *orom; /* platform firmware support */
490 struct intel_super *next; /* (temp) list for disambiguating family_num */
491 struct md_bb bb; /* memory for get_bad_blocks call */
492 };
493
494 struct intel_disk {
495 struct imsm_disk disk;
496 #define IMSM_UNKNOWN_OWNER (-1)
497 int owner;
498 struct intel_disk *next;
499 };
500
501 /**
502 * struct extent - reserved space details.
503 * @start: start offset.
504 * @size: size of reservation, set to 0 for metadata reservation.
505 * @vol: index of the volume, meaningful if &size is set.
506 */
507 struct extent {
508 unsigned long long start, size;
509 int vol;
510 };
511
512 /* definitions of reshape process types */
513 enum imsm_reshape_type {
514 CH_TAKEOVER,
515 CH_MIGRATION,
516 CH_ARRAY_SIZE,
517 };
518
519 /* definition of messages passed to imsm_process_update */
520 enum imsm_update_type {
521 update_activate_spare,
522 update_create_array,
523 update_kill_array,
524 update_rename_array,
525 update_add_remove_disk,
526 update_reshape_container_disks,
527 update_reshape_migration,
528 update_takeover,
529 update_general_migration_checkpoint,
530 update_size_change,
531 update_prealloc_badblocks_mem,
532 update_rwh_policy,
533 };
534
535 struct imsm_update_activate_spare {
536 enum imsm_update_type type;
537 struct dl *dl;
538 int slot;
539 int array;
540 struct imsm_update_activate_spare *next;
541 };
542
543 struct geo_params {
544 char devnm[32];
545 char *dev_name;
546 unsigned long long size;
547 int level;
548 int layout;
549 int chunksize;
550 int raid_disks;
551 };
552
553 enum takeover_direction {
554 R10_TO_R0,
555 R0_TO_R10
556 };
557 struct imsm_update_takeover {
558 enum imsm_update_type type;
559 int subarray;
560 enum takeover_direction direction;
561 };
562
563 struct imsm_update_reshape {
564 enum imsm_update_type type;
565 int old_raid_disks;
566 int new_raid_disks;
567
568 int new_disks[1]; /* new_raid_disks - old_raid_disks makedev number */
569 };
570
571 struct imsm_update_reshape_migration {
572 enum imsm_update_type type;
573 int old_raid_disks;
574 int new_raid_disks;
575 /* fields for array migration changes
576 */
577 int subdev;
578 int new_level;
579 int new_layout;
580 int new_chunksize;
581
582 int new_disks[1]; /* new_raid_disks - old_raid_disks makedev number */
583 };
584
585 struct imsm_update_size_change {
586 enum imsm_update_type type;
587 int subdev;
588 long long new_size;
589 };
590
591 struct imsm_update_general_migration_checkpoint {
592 enum imsm_update_type type;
593 __u64 curr_migr_unit;
594 };
595
596 struct disk_info {
597 __u8 serial[MAX_RAID_SERIAL_LEN];
598 };
599
600 struct imsm_update_create_array {
601 enum imsm_update_type type;
602 int dev_idx;
603 struct imsm_dev dev;
604 };
605
606 struct imsm_update_kill_array {
607 enum imsm_update_type type;
608 int dev_idx;
609 };
610
611 struct imsm_update_rename_array {
612 enum imsm_update_type type;
613 __u8 name[MAX_RAID_SERIAL_LEN];
614 int dev_idx;
615 };
616
617 struct imsm_update_add_remove_disk {
618 enum imsm_update_type type;
619 };
620
621 struct imsm_update_prealloc_bb_mem {
622 enum imsm_update_type type;
623 };
624
625 struct imsm_update_rwh_policy {
626 enum imsm_update_type type;
627 int new_policy;
628 int dev_idx;
629 };
630
631 static const char *_sys_dev_type[] = {
632 [SYS_DEV_UNKNOWN] = "Unknown",
633 [SYS_DEV_SAS] = "SAS",
634 [SYS_DEV_SATA] = "SATA",
635 [SYS_DEV_NVME] = "NVMe",
636 [SYS_DEV_VMD] = "VMD",
637 [SYS_DEV_SATA_VMD] = "SATA VMD"
638 };
639
640 static int no_platform = -1;
641
642 static int check_no_platform(void)
643 {
644 static const char search[] = "mdadm.imsm.test=1";
645 FILE *fp;
646
647 if (no_platform >= 0)
648 return no_platform;
649
650 if (check_env("IMSM_NO_PLATFORM")) {
651 no_platform = 1;
652 return 1;
653 }
654 fp = fopen("/proc/cmdline", "r");
655 if (fp) {
656 char *l = conf_line(fp);
657 char *w = l;
658
659 if (l == NULL) {
660 fclose(fp);
661 return 0;
662 }
663
664 do {
665 if (strcmp(w, search) == 0)
666 no_platform = 1;
667 w = dl_next(w);
668 } while (w != l);
669 free_line(l);
670 fclose(fp);
671 if (no_platform >= 0)
672 return no_platform;
673 }
674 no_platform = 0;
675 return 0;
676 }
677
678 void imsm_set_no_platform(int v)
679 {
680 no_platform = v;
681 }
682
683 const char *get_sys_dev_type(enum sys_dev_type type)
684 {
685 if (type >= SYS_DEV_MAX)
686 type = SYS_DEV_UNKNOWN;
687
688 return _sys_dev_type[type];
689 }
690
691 static struct intel_hba * alloc_intel_hba(struct sys_dev *device)
692 {
693 struct intel_hba *result = xmalloc(sizeof(*result));
694
695 result->type = device->type;
696 result->path = xstrdup(device->path);
697 result->next = NULL;
698 if (result->path && (result->pci_id = strrchr(result->path, '/')) != NULL)
699 result->pci_id++;
700
701 return result;
702 }
703
704 static struct intel_hba * find_intel_hba(struct intel_hba *hba, struct sys_dev *device)
705 {
706 struct intel_hba *result;
707
708 for (result = hba; result; result = result->next) {
709 if (result->type == device->type && strcmp(result->path, device->path) == 0)
710 break;
711 }
712 return result;
713 }
714
715 static int attach_hba_to_super(struct intel_super *super, struct sys_dev *device)
716 {
717 struct intel_hba *hba;
718
719 /* check if disk attached to Intel HBA */
720 hba = find_intel_hba(super->hba, device);
721 if (hba != NULL)
722 return 1;
723 /* Check if HBA is already attached to super */
724 if (super->hba == NULL) {
725 super->hba = alloc_intel_hba(device);
726 return 1;
727 }
728
729 hba = super->hba;
730 /* Intel metadata allows for all disks attached to the same type HBA.
731 * Do not support HBA types mixing
732 */
733 if (device->type != hba->type)
734 return 2;
735
736 /* Multiple same type HBAs can be used if they share the same OROM */
737 const struct imsm_orom *device_orom = get_orom_by_device_id(device->dev_id);
738
739 if (device_orom != super->orom)
740 return 2;
741
742 while (hba->next)
743 hba = hba->next;
744
745 hba->next = alloc_intel_hba(device);
746 return 1;
747 }
748
749 static struct sys_dev* find_disk_attached_hba(int fd, const char *devname)
750 {
751 struct sys_dev *list, *elem;
752 char *disk_path;
753
754 if ((list = find_intel_devices()) == NULL)
755 return 0;
756
757 if (!is_fd_valid(fd))
758 disk_path = (char *) devname;
759 else
760 disk_path = diskfd_to_devpath(fd, 1, NULL);
761
762 if (!disk_path)
763 return 0;
764
765 for (elem = list; elem; elem = elem->next)
766 if (path_attached_to_hba(disk_path, elem->path))
767 break;
768
769 if (disk_path != devname)
770 free(disk_path);
771
772 return elem;
773 }
774
775 static int find_intel_hba_capability(int fd, struct intel_super *super,
776 char *devname);
777
778 static struct supertype *match_metadata_desc_imsm(char *arg)
779 {
780 struct supertype *st;
781
782 if (strcmp(arg, "imsm") != 0 &&
783 strcmp(arg, "default") != 0
784 )
785 return NULL;
786
787 st = xcalloc(1, sizeof(*st));
788 st->ss = &super_imsm;
789 st->max_devs = IMSM_MAX_DEVICES;
790 st->minor_version = 0;
791 st->sb = NULL;
792 return st;
793 }
794
795 static __u8 *get_imsm_version(struct imsm_super *mpb)
796 {
797 return &mpb->sig[MPB_SIG_LEN];
798 }
799
800 /* retrieve a disk directly from the anchor when the anchor is known to be
801 * up-to-date, currently only at load time
802 */
803 static struct imsm_disk *__get_imsm_disk(struct imsm_super *mpb, __u8 index)
804 {
805 if (index >= mpb->num_disks)
806 return NULL;
807 return &mpb->disk[index];
808 }
809
810 /* retrieve the disk description based on a index of the disk
811 * in the sub-array
812 */
813 static struct dl *get_imsm_dl_disk(struct intel_super *super, __u8 index)
814 {
815 struct dl *d;
816
817 for (d = super->disks; d; d = d->next)
818 if (d->index == index)
819 return d;
820
821 return NULL;
822 }
823 /* retrieve a disk from the parsed metadata */
824 static struct imsm_disk *get_imsm_disk(struct intel_super *super, __u8 index)
825 {
826 struct dl *dl;
827
828 dl = get_imsm_dl_disk(super, index);
829 if (dl)
830 return &dl->disk;
831
832 return NULL;
833 }
834
835 /* generate a checksum directly from the anchor when the anchor is known to be
836 * up-to-date, currently only at load or write_super after coalescing
837 */
838 static __u32 __gen_imsm_checksum(struct imsm_super *mpb)
839 {
840 __u32 end = mpb->mpb_size / sizeof(end);
841 __u32 *p = (__u32 *) mpb;
842 __u32 sum = 0;
843
844 while (end--) {
845 sum += __le32_to_cpu(*p);
846 p++;
847 }
848
849 return sum - __le32_to_cpu(mpb->check_sum);
850 }
851
852 static size_t sizeof_imsm_map(struct imsm_map *map)
853 {
854 return sizeof(struct imsm_map) + sizeof(__u32) * (map->num_members - 1);
855 }
856
857 struct imsm_map *get_imsm_map(struct imsm_dev *dev, int second_map)
858 {
859 /* A device can have 2 maps if it is in the middle of a migration.
860 * If second_map is:
861 * MAP_0 - we return the first map
862 * MAP_1 - we return the second map if it exists, else NULL
863 * MAP_X - we return the second map if it exists, else the first
864 */
865 struct imsm_map *map = &dev->vol.map[0];
866 struct imsm_map *map2 = NULL;
867
868 if (dev->vol.migr_state)
869 map2 = (void *)map + sizeof_imsm_map(map);
870
871 switch (second_map) {
872 case MAP_0:
873 break;
874 case MAP_1:
875 map = map2;
876 break;
877 case MAP_X:
878 if (map2)
879 map = map2;
880 break;
881 default:
882 map = NULL;
883 }
884 return map;
885
886 }
887
888 /* return the size of the device.
889 * migr_state increases the returned size if map[0] were to be duplicated
890 */
891 static size_t sizeof_imsm_dev(struct imsm_dev *dev, int migr_state)
892 {
893 size_t size = sizeof(*dev) - sizeof(struct imsm_map) +
894 sizeof_imsm_map(get_imsm_map(dev, MAP_0));
895
896 /* migrating means an additional map */
897 if (dev->vol.migr_state)
898 size += sizeof_imsm_map(get_imsm_map(dev, MAP_1));
899 else if (migr_state)
900 size += sizeof_imsm_map(get_imsm_map(dev, MAP_0));
901
902 return size;
903 }
904
905 /* retrieve disk serial number list from a metadata update */
906 static struct disk_info *get_disk_info(struct imsm_update_create_array *update)
907 {
908 void *u = update;
909 struct disk_info *inf;
910
911 inf = u + sizeof(*update) - sizeof(struct imsm_dev) +
912 sizeof_imsm_dev(&update->dev, 0);
913
914 return inf;
915 }
916
917 /**
918 * __get_imsm_dev() - Get device with index from imsm_super.
919 * @mpb: &imsm_super pointer, not NULL.
920 * @index: Device index.
921 *
922 * Function works as non-NULL, aborting in such a case,
923 * when NULL would be returned.
924 *
925 * Device index should be in range 0 up to num_raid_devs.
926 * Function assumes the index was already verified.
927 * Index must be valid, otherwise abort() is called.
928 *
929 * Return: Pointer to corresponding imsm_dev.
930 *
931 */
932 static struct imsm_dev *__get_imsm_dev(struct imsm_super *mpb, __u8 index)
933 {
934 int offset;
935 int i;
936 void *_mpb = mpb;
937
938 if (index >= mpb->num_raid_devs)
939 goto error;
940
941 /* devices start after all disks */
942 offset = ((void *) &mpb->disk[mpb->num_disks]) - _mpb;
943
944 for (i = 0; i <= index; i++, offset += sizeof_imsm_dev(_mpb + offset, 0))
945 if (i == index)
946 return _mpb + offset;
947 error:
948 pr_err("cannot find imsm_dev with index %u in imsm_super\n", index);
949 abort();
950 }
951
952 /**
953 * get_imsm_dev() - Get device with index from intel_super.
954 * @super: &intel_super pointer, not NULL.
955 * @index: Device index.
956 *
957 * Function works as non-NULL, aborting in such a case,
958 * when NULL would be returned.
959 *
960 * Device index should be in range 0 up to num_raid_devs.
961 * Function assumes the index was already verified.
962 * Index must be valid, otherwise abort() is called.
963 *
964 * Return: Pointer to corresponding imsm_dev.
965 *
966 */
967 static struct imsm_dev *get_imsm_dev(struct intel_super *super, __u8 index)
968 {
969 struct intel_dev *dv;
970
971 if (index >= super->anchor->num_raid_devs)
972 goto error;
973
974 for (dv = super->devlist; dv; dv = dv->next)
975 if (dv->index == index)
976 return dv->dev;
977 error:
978 pr_err("cannot find imsm_dev with index %u in intel_super\n", index);
979 abort();
980 }
981
982 static inline unsigned long long __le48_to_cpu(const struct bbm_log_block_addr
983 *addr)
984 {
985 return ((((__u64)__le32_to_cpu(addr->dw1)) << 16) |
986 __le16_to_cpu(addr->w1));
987 }
988
989 static inline struct bbm_log_block_addr __cpu_to_le48(unsigned long long sec)
990 {
991 struct bbm_log_block_addr addr;
992
993 addr.w1 = __cpu_to_le16((__u16)(sec & 0xffff));
994 addr.dw1 = __cpu_to_le32((__u32)(sec >> 16) & 0xffffffff);
995 return addr;
996 }
997
998 /* get size of the bbm log */
999 static __u32 get_imsm_bbm_log_size(struct bbm_log *log)
1000 {
1001 if (!log || log->entry_count == 0)
1002 return 0;
1003
1004 return sizeof(log->signature) +
1005 sizeof(log->entry_count) +
1006 log->entry_count * sizeof(struct bbm_log_entry);
1007 }
1008
1009 /* check if bad block is not partially stored in bbm log */
1010 static int is_stored_in_bbm(struct bbm_log *log, const __u8 idx, const unsigned
1011 long long sector, const int length, __u32 *pos)
1012 {
1013 __u32 i;
1014
1015 for (i = *pos; i < log->entry_count; i++) {
1016 struct bbm_log_entry *entry = &log->marked_block_entries[i];
1017 unsigned long long bb_start;
1018 unsigned long long bb_end;
1019
1020 bb_start = __le48_to_cpu(&entry->defective_block_start);
1021 bb_end = bb_start + (entry->marked_count + 1);
1022
1023 if ((entry->disk_ordinal == idx) && (bb_start >= sector) &&
1024 (bb_end <= sector + length)) {
1025 *pos = i;
1026 return 1;
1027 }
1028 }
1029 return 0;
1030 }
1031
1032 /* record new bad block in bbm log */
1033 static int record_new_badblock(struct bbm_log *log, const __u8 idx, unsigned
1034 long long sector, int length)
1035 {
1036 int new_bb = 0;
1037 __u32 pos = 0;
1038 struct bbm_log_entry *entry = NULL;
1039
1040 while (is_stored_in_bbm(log, idx, sector, length, &pos)) {
1041 struct bbm_log_entry *e = &log->marked_block_entries[pos];
1042
1043 if ((e->marked_count + 1 == BBM_LOG_MAX_LBA_ENTRY_VAL) &&
1044 (__le48_to_cpu(&e->defective_block_start) == sector)) {
1045 sector += BBM_LOG_MAX_LBA_ENTRY_VAL;
1046 length -= BBM_LOG_MAX_LBA_ENTRY_VAL;
1047 pos = pos + 1;
1048 continue;
1049 }
1050 entry = e;
1051 break;
1052 }
1053
1054 if (entry) {
1055 int cnt = (length <= BBM_LOG_MAX_LBA_ENTRY_VAL) ? length :
1056 BBM_LOG_MAX_LBA_ENTRY_VAL;
1057 entry->defective_block_start = __cpu_to_le48(sector);
1058 entry->marked_count = cnt - 1;
1059 if (cnt == length)
1060 return 1;
1061 sector += cnt;
1062 length -= cnt;
1063 }
1064
1065 new_bb = ROUND_UP(length, BBM_LOG_MAX_LBA_ENTRY_VAL) /
1066 BBM_LOG_MAX_LBA_ENTRY_VAL;
1067 if (log->entry_count + new_bb > BBM_LOG_MAX_ENTRIES)
1068 return 0;
1069
1070 while (length > 0) {
1071 int cnt = (length <= BBM_LOG_MAX_LBA_ENTRY_VAL) ? length :
1072 BBM_LOG_MAX_LBA_ENTRY_VAL;
1073 struct bbm_log_entry *entry =
1074 &log->marked_block_entries[log->entry_count];
1075
1076 entry->defective_block_start = __cpu_to_le48(sector);
1077 entry->marked_count = cnt - 1;
1078 entry->disk_ordinal = idx;
1079
1080 sector += cnt;
1081 length -= cnt;
1082
1083 log->entry_count++;
1084 }
1085
1086 return new_bb;
1087 }
1088
1089 /* clear all bad blocks for given disk */
1090 static void clear_disk_badblocks(struct bbm_log *log, const __u8 idx)
1091 {
1092 __u32 i = 0;
1093
1094 while (i < log->entry_count) {
1095 struct bbm_log_entry *entries = log->marked_block_entries;
1096
1097 if (entries[i].disk_ordinal == idx) {
1098 if (i < log->entry_count - 1)
1099 entries[i] = entries[log->entry_count - 1];
1100 log->entry_count--;
1101 } else {
1102 i++;
1103 }
1104 }
1105 }
1106
1107 /* clear given bad block */
1108 static int clear_badblock(struct bbm_log *log, const __u8 idx, const unsigned
1109 long long sector, const int length) {
1110 __u32 i = 0;
1111
1112 while (i < log->entry_count) {
1113 struct bbm_log_entry *entries = log->marked_block_entries;
1114
1115 if ((entries[i].disk_ordinal == idx) &&
1116 (__le48_to_cpu(&entries[i].defective_block_start) ==
1117 sector) && (entries[i].marked_count + 1 == length)) {
1118 if (i < log->entry_count - 1)
1119 entries[i] = entries[log->entry_count - 1];
1120 log->entry_count--;
1121 break;
1122 }
1123 i++;
1124 }
1125
1126 return 1;
1127 }
1128
1129 /* allocate and load BBM log from metadata */
1130 static int load_bbm_log(struct intel_super *super)
1131 {
1132 struct imsm_super *mpb = super->anchor;
1133 __u32 bbm_log_size = __le32_to_cpu(mpb->bbm_log_size);
1134
1135 super->bbm_log = xcalloc(1, sizeof(struct bbm_log));
1136 if (!super->bbm_log)
1137 return 1;
1138
1139 if (bbm_log_size) {
1140 struct bbm_log *log = (void *)mpb +
1141 __le32_to_cpu(mpb->mpb_size) - bbm_log_size;
1142
1143 __u32 entry_count;
1144
1145 if (bbm_log_size < sizeof(log->signature) +
1146 sizeof(log->entry_count))
1147 return 2;
1148
1149 entry_count = __le32_to_cpu(log->entry_count);
1150 if ((__le32_to_cpu(log->signature) != BBM_LOG_SIGNATURE) ||
1151 (entry_count > BBM_LOG_MAX_ENTRIES))
1152 return 3;
1153
1154 if (bbm_log_size !=
1155 sizeof(log->signature) + sizeof(log->entry_count) +
1156 entry_count * sizeof(struct bbm_log_entry))
1157 return 4;
1158
1159 memcpy(super->bbm_log, log, bbm_log_size);
1160 } else {
1161 super->bbm_log->signature = __cpu_to_le32(BBM_LOG_SIGNATURE);
1162 super->bbm_log->entry_count = 0;
1163 }
1164
1165 return 0;
1166 }
1167
1168 /* checks if bad block is within volume boundaries */
1169 static int is_bad_block_in_volume(const struct bbm_log_entry *entry,
1170 const unsigned long long start_sector,
1171 const unsigned long long size)
1172 {
1173 unsigned long long bb_start;
1174 unsigned long long bb_end;
1175
1176 bb_start = __le48_to_cpu(&entry->defective_block_start);
1177 bb_end = bb_start + (entry->marked_count + 1);
1178
1179 if (((bb_start >= start_sector) && (bb_start < start_sector + size)) ||
1180 ((bb_end >= start_sector) && (bb_end <= start_sector + size)))
1181 return 1;
1182
1183 return 0;
1184 }
1185
1186 /* get list of bad blocks on a drive for a volume */
1187 static void get_volume_badblocks(const struct bbm_log *log, const __u8 idx,
1188 const unsigned long long start_sector,
1189 const unsigned long long size,
1190 struct md_bb *bbs)
1191 {
1192 __u32 count = 0;
1193 __u32 i;
1194
1195 for (i = 0; i < log->entry_count; i++) {
1196 const struct bbm_log_entry *ent =
1197 &log->marked_block_entries[i];
1198 struct md_bb_entry *bb;
1199
1200 if ((ent->disk_ordinal == idx) &&
1201 is_bad_block_in_volume(ent, start_sector, size)) {
1202
1203 if (!bbs->entries) {
1204 bbs->entries = xmalloc(BBM_LOG_MAX_ENTRIES *
1205 sizeof(*bb));
1206 if (!bbs->entries)
1207 break;
1208 }
1209
1210 bb = &bbs->entries[count++];
1211 bb->sector = __le48_to_cpu(&ent->defective_block_start);
1212 bb->length = ent->marked_count + 1;
1213 }
1214 }
1215 bbs->count = count;
1216 }
1217
1218 /*
1219 * for second_map:
1220 * == MAP_0 get first map
1221 * == MAP_1 get second map
1222 * == MAP_X than get map according to the current migr_state
1223 */
1224 static __u32 get_imsm_ord_tbl_ent(struct imsm_dev *dev,
1225 int slot,
1226 int second_map)
1227 {
1228 struct imsm_map *map;
1229
1230 map = get_imsm_map(dev, second_map);
1231
1232 /* top byte identifies disk under rebuild */
1233 return __le32_to_cpu(map->disk_ord_tbl[slot]);
1234 }
1235
1236 #define ord_to_idx(ord) (((ord) << 8) >> 8)
1237 static __u32 get_imsm_disk_idx(struct imsm_dev *dev, int slot, int second_map)
1238 {
1239 __u32 ord = get_imsm_ord_tbl_ent(dev, slot, second_map);
1240
1241 return ord_to_idx(ord);
1242 }
1243
1244 static void set_imsm_ord_tbl_ent(struct imsm_map *map, int slot, __u32 ord)
1245 {
1246 map->disk_ord_tbl[slot] = __cpu_to_le32(ord);
1247 }
1248
1249 static int get_imsm_disk_slot(struct imsm_map *map, const unsigned int idx)
1250 {
1251 int slot;
1252 __u32 ord;
1253
1254 for (slot = 0; slot < map->num_members; slot++) {
1255 ord = __le32_to_cpu(map->disk_ord_tbl[slot]);
1256 if (ord_to_idx(ord) == idx)
1257 return slot;
1258 }
1259
1260 return IMSM_STATUS_ERROR;
1261 }
1262
1263 static int get_imsm_raid_level(struct imsm_map *map)
1264 {
1265 if (map->raid_level == 1) {
1266 if (map->num_members == 2)
1267 return 1;
1268 else
1269 return 10;
1270 }
1271
1272 return map->raid_level;
1273 }
1274
1275 /**
1276 * get_disk_slot_in_dev() - retrieve disk slot from &imsm_dev.
1277 * @super: &intel_super pointer, not NULL.
1278 * @dev_idx: imsm device index.
1279 * @idx: disk index.
1280 *
1281 * Return: Slot on success, IMSM_STATUS_ERROR otherwise.
1282 */
1283 static int get_disk_slot_in_dev(struct intel_super *super, const __u8 dev_idx,
1284 const unsigned int idx)
1285 {
1286 struct imsm_dev *dev = get_imsm_dev(super, dev_idx);
1287 struct imsm_map *map = get_imsm_map(dev, MAP_0);
1288
1289 return get_imsm_disk_slot(map, idx);
1290 }
1291
1292 static int cmp_extent(const void *av, const void *bv)
1293 {
1294 const struct extent *a = av;
1295 const struct extent *b = bv;
1296 if (a->start < b->start)
1297 return -1;
1298 if (a->start > b->start)
1299 return 1;
1300 return 0;
1301 }
1302
1303 static int count_memberships(struct dl *dl, struct intel_super *super)
1304 {
1305 int memberships = 0;
1306 int i;
1307
1308 for (i = 0; i < super->anchor->num_raid_devs; i++)
1309 if (get_disk_slot_in_dev(super, i, dl->index) >= 0)
1310 memberships++;
1311
1312 return memberships;
1313 }
1314
1315 static __u32 imsm_min_reserved_sectors(struct intel_super *super);
1316
1317 static int split_ull(unsigned long long n, void *lo, void *hi)
1318 {
1319 if (lo == 0 || hi == 0)
1320 return 1;
1321 __put_unaligned32(__cpu_to_le32((__u32)n), lo);
1322 __put_unaligned32(__cpu_to_le32((n >> 32)), hi);
1323 return 0;
1324 }
1325
1326 static unsigned long long join_u32(__u32 lo, __u32 hi)
1327 {
1328 return (unsigned long long)__le32_to_cpu(lo) |
1329 (((unsigned long long)__le32_to_cpu(hi)) << 32);
1330 }
1331
1332 static unsigned long long total_blocks(struct imsm_disk *disk)
1333 {
1334 if (disk == NULL)
1335 return 0;
1336 return join_u32(disk->total_blocks_lo, disk->total_blocks_hi);
1337 }
1338
1339 /**
1340 * imsm_num_data_members() - get data drives count for an array.
1341 * @map: Map to analyze.
1342 *
1343 * num_data_members value represents minimal count of drives for level.
1344 * The name of the property could be misleading for RAID5 with asymmetric layout
1345 * because some data required to be calculated from parity.
1346 * The property is extracted from level and num_members value.
1347 *
1348 * Return: num_data_members value on success, zero otherwise.
1349 */
1350 static __u8 imsm_num_data_members(struct imsm_map *map)
1351 {
1352 switch (get_imsm_raid_level(map)) {
1353 case 0:
1354 return map->num_members;
1355 case 1:
1356 case 10:
1357 return map->num_members / 2;
1358 case 5:
1359 return map->num_members - 1;
1360 default:
1361 dprintf("unsupported raid level\n");
1362 return 0;
1363 }
1364 }
1365
1366 static unsigned long long pba_of_lba0(struct imsm_map *map)
1367 {
1368 if (map == NULL)
1369 return 0;
1370 return join_u32(map->pba_of_lba0_lo, map->pba_of_lba0_hi);
1371 }
1372
1373 static unsigned long long blocks_per_member(struct imsm_map *map)
1374 {
1375 if (map == NULL)
1376 return 0;
1377 return join_u32(map->blocks_per_member_lo, map->blocks_per_member_hi);
1378 }
1379
1380 static unsigned long long num_data_stripes(struct imsm_map *map)
1381 {
1382 if (map == NULL)
1383 return 0;
1384 return join_u32(map->num_data_stripes_lo, map->num_data_stripes_hi);
1385 }
1386
1387 static unsigned long long vol_curr_migr_unit(struct imsm_dev *dev)
1388 {
1389 if (dev == NULL)
1390 return 0;
1391
1392 return join_u32(dev->vol.curr_migr_unit_lo, dev->vol.curr_migr_unit_hi);
1393 }
1394
1395 static unsigned long long imsm_dev_size(struct imsm_dev *dev)
1396 {
1397 if (dev == NULL)
1398 return 0;
1399 return join_u32(dev->size_low, dev->size_high);
1400 }
1401
1402 static unsigned long long migr_chkp_area_pba(struct migr_record *migr_rec)
1403 {
1404 if (migr_rec == NULL)
1405 return 0;
1406 return join_u32(migr_rec->ckpt_area_pba_lo,
1407 migr_rec->ckpt_area_pba_hi);
1408 }
1409
1410 static unsigned long long current_migr_unit(struct migr_record *migr_rec)
1411 {
1412 if (migr_rec == NULL)
1413 return 0;
1414 return join_u32(migr_rec->curr_migr_unit_lo,
1415 migr_rec->curr_migr_unit_hi);
1416 }
1417
1418 static unsigned long long migr_dest_1st_member_lba(struct migr_record *migr_rec)
1419 {
1420 if (migr_rec == NULL)
1421 return 0;
1422 return join_u32(migr_rec->dest_1st_member_lba_lo,
1423 migr_rec->dest_1st_member_lba_hi);
1424 }
1425
1426 static unsigned long long get_num_migr_units(struct migr_record *migr_rec)
1427 {
1428 if (migr_rec == NULL)
1429 return 0;
1430 return join_u32(migr_rec->num_migr_units_lo,
1431 migr_rec->num_migr_units_hi);
1432 }
1433
1434 static void set_total_blocks(struct imsm_disk *disk, unsigned long long n)
1435 {
1436 split_ull(n, &disk->total_blocks_lo, &disk->total_blocks_hi);
1437 }
1438
1439 /**
1440 * set_num_domains() - Set number of domains for an array.
1441 * @map: Map to be updated.
1442 *
1443 * num_domains property represents copies count of each data drive, thus make
1444 * it meaningful only for RAID1 and RAID10. IMSM supports two domains for
1445 * raid1 and raid10.
1446 */
1447 static void set_num_domains(struct imsm_map *map)
1448 {
1449 int level = get_imsm_raid_level(map);
1450
1451 if (level == 1 || level == 10)
1452 map->num_domains = 2;
1453 else
1454 map->num_domains = 1;
1455 }
1456
1457 static void set_pba_of_lba0(struct imsm_map *map, unsigned long long n)
1458 {
1459 split_ull(n, &map->pba_of_lba0_lo, &map->pba_of_lba0_hi);
1460 }
1461
1462 static void set_blocks_per_member(struct imsm_map *map, unsigned long long n)
1463 {
1464 split_ull(n, &map->blocks_per_member_lo, &map->blocks_per_member_hi);
1465 }
1466
1467 static void set_num_data_stripes(struct imsm_map *map, unsigned long long n)
1468 {
1469 split_ull(n, &map->num_data_stripes_lo, &map->num_data_stripes_hi);
1470 }
1471
1472 /**
1473 * update_num_data_stripes() - Calculate and update num_data_stripes value.
1474 * @map: map to be updated.
1475 * @dev_size: size of volume.
1476 *
1477 * num_data_stripes value is addictionally divided by num_domains, therefore for
1478 * levels where num_domains is not 1, nds is a part of real value.
1479 */
1480 static void update_num_data_stripes(struct imsm_map *map,
1481 unsigned long long dev_size)
1482 {
1483 unsigned long long nds = dev_size / imsm_num_data_members(map);
1484
1485 nds /= map->num_domains;
1486 nds /= map->blocks_per_strip;
1487 set_num_data_stripes(map, nds);
1488 }
1489
1490 static void set_vol_curr_migr_unit(struct imsm_dev *dev, unsigned long long n)
1491 {
1492 if (dev == NULL)
1493 return;
1494
1495 split_ull(n, &dev->vol.curr_migr_unit_lo, &dev->vol.curr_migr_unit_hi);
1496 }
1497
1498 static void set_imsm_dev_size(struct imsm_dev *dev, unsigned long long n)
1499 {
1500 split_ull(n, &dev->size_low, &dev->size_high);
1501 }
1502
1503 static void set_migr_chkp_area_pba(struct migr_record *migr_rec,
1504 unsigned long long n)
1505 {
1506 split_ull(n, &migr_rec->ckpt_area_pba_lo, &migr_rec->ckpt_area_pba_hi);
1507 }
1508
1509 static void set_current_migr_unit(struct migr_record *migr_rec,
1510 unsigned long long n)
1511 {
1512 split_ull(n, &migr_rec->curr_migr_unit_lo,
1513 &migr_rec->curr_migr_unit_hi);
1514 }
1515
1516 static void set_migr_dest_1st_member_lba(struct migr_record *migr_rec,
1517 unsigned long long n)
1518 {
1519 split_ull(n, &migr_rec->dest_1st_member_lba_lo,
1520 &migr_rec->dest_1st_member_lba_hi);
1521 }
1522
1523 static void set_num_migr_units(struct migr_record *migr_rec,
1524 unsigned long long n)
1525 {
1526 split_ull(n, &migr_rec->num_migr_units_lo,
1527 &migr_rec->num_migr_units_hi);
1528 }
1529
1530 static unsigned long long per_dev_array_size(struct imsm_map *map)
1531 {
1532 unsigned long long array_size = 0;
1533
1534 if (map == NULL)
1535 return array_size;
1536
1537 array_size = num_data_stripes(map) * map->blocks_per_strip;
1538 if (get_imsm_raid_level(map) == 1 || get_imsm_raid_level(map) == 10)
1539 array_size *= 2;
1540
1541 return array_size;
1542 }
1543
1544 static struct extent *get_extents(struct intel_super *super, struct dl *dl,
1545 int get_minimal_reservation)
1546 {
1547 /* find a list of used extents on the given physical device */
1548 int memberships = count_memberships(dl, super);
1549 struct extent *rv = xcalloc(memberships + 1, sizeof(struct extent));
1550 struct extent *e = rv;
1551 int i;
1552 __u32 reservation;
1553
1554 /* trim the reserved area for spares, so they can join any array
1555 * regardless of whether the OROM has assigned sectors from the
1556 * IMSM_RESERVED_SECTORS region
1557 */
1558 if (dl->index == -1 || get_minimal_reservation)
1559 reservation = imsm_min_reserved_sectors(super);
1560 else
1561 reservation = MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS;
1562
1563 for (i = 0; i < super->anchor->num_raid_devs; i++) {
1564 struct imsm_dev *dev = get_imsm_dev(super, i);
1565 struct imsm_map *map = get_imsm_map(dev, MAP_0);
1566
1567 if (get_imsm_disk_slot(map, dl->index) >= 0) {
1568 e->start = pba_of_lba0(map);
1569 e->size = per_dev_array_size(map);
1570 e->vol = i;
1571 e++;
1572 }
1573 }
1574 qsort(rv, memberships, sizeof(*rv), cmp_extent);
1575
1576 /* determine the start of the metadata
1577 * when no raid devices are defined use the default
1578 * ...otherwise allow the metadata to truncate the value
1579 * as is the case with older versions of imsm
1580 */
1581 if (memberships) {
1582 struct extent *last = &rv[memberships - 1];
1583 unsigned long long remainder;
1584
1585 remainder = total_blocks(&dl->disk) - (last->start + last->size);
1586 /* round down to 1k block to satisfy precision of the kernel
1587 * 'size' interface
1588 */
1589 remainder &= ~1UL;
1590 /* make sure remainder is still sane */
1591 if (remainder < (unsigned)ROUND_UP(super->len, 512) >> 9)
1592 remainder = ROUND_UP(super->len, 512) >> 9;
1593 if (reservation > remainder)
1594 reservation = remainder;
1595 }
1596 e->start = total_blocks(&dl->disk) - reservation;
1597 e->size = 0;
1598 return rv;
1599 }
1600
1601 /* try to determine how much space is reserved for metadata from
1602 * the last get_extents() entry, otherwise fallback to the
1603 * default
1604 */
1605 static __u32 imsm_reserved_sectors(struct intel_super *super, struct dl *dl)
1606 {
1607 struct extent *e;
1608 int i;
1609 __u32 rv;
1610
1611 /* for spares just return a minimal reservation which will grow
1612 * once the spare is picked up by an array
1613 */
1614 if (dl->index == -1)
1615 return MPB_SECTOR_CNT;
1616
1617 e = get_extents(super, dl, 0);
1618 if (!e)
1619 return MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS;
1620
1621 /* scroll to last entry */
1622 for (i = 0; e[i].size; i++)
1623 continue;
1624
1625 rv = total_blocks(&dl->disk) - e[i].start;
1626
1627 free(e);
1628
1629 return rv;
1630 }
1631
1632 static int is_spare(struct imsm_disk *disk)
1633 {
1634 return (disk->status & SPARE_DISK) == SPARE_DISK;
1635 }
1636
1637 static int is_configured(struct imsm_disk *disk)
1638 {
1639 return (disk->status & CONFIGURED_DISK) == CONFIGURED_DISK;
1640 }
1641
1642 static int is_failed(struct imsm_disk *disk)
1643 {
1644 return (disk->status & FAILED_DISK) == FAILED_DISK;
1645 }
1646
1647 static int is_journal(struct imsm_disk *disk)
1648 {
1649 return (disk->status & JOURNAL_DISK) == JOURNAL_DISK;
1650 }
1651
1652 /**
1653 * round_member_size_to_mb()- Round given size to closest MiB.
1654 * @size: size to round in sectors.
1655 */
1656 static inline unsigned long long round_member_size_to_mb(unsigned long long size)
1657 {
1658 return (size >> SECT_PER_MB_SHIFT) << SECT_PER_MB_SHIFT;
1659 }
1660
1661 /**
1662 * round_size_to_mb()- Round given size.
1663 * @array_size: size to round in sectors.
1664 * @disk_count: count of data members.
1665 *
1666 * Get size per each data member and round it to closest MiB to ensure that data
1667 * splits evenly between members.
1668 *
1669 * Return: Array size, rounded down.
1670 */
1671 static inline unsigned long long round_size_to_mb(unsigned long long array_size,
1672 unsigned int disk_count)
1673 {
1674 return round_member_size_to_mb(array_size / disk_count) * disk_count;
1675 }
1676
1677 static int able_to_resync(int raid_level, int missing_disks)
1678 {
1679 int max_missing_disks = 0;
1680
1681 switch (raid_level) {
1682 case 10:
1683 max_missing_disks = 1;
1684 break;
1685 default:
1686 max_missing_disks = 0;
1687 }
1688 return missing_disks <= max_missing_disks;
1689 }
1690
1691 /* try to determine how much space is reserved for metadata from
1692 * the last get_extents() entry on the smallest active disk,
1693 * otherwise fallback to the default
1694 */
1695 static __u32 imsm_min_reserved_sectors(struct intel_super *super)
1696 {
1697 struct extent *e;
1698 int i;
1699 unsigned long long min_active;
1700 __u32 remainder;
1701 __u32 rv = MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS;
1702 struct dl *dl, *dl_min = NULL;
1703
1704 if (!super)
1705 return rv;
1706
1707 min_active = 0;
1708 for (dl = super->disks; dl; dl = dl->next) {
1709 if (dl->index < 0)
1710 continue;
1711 unsigned long long blocks = total_blocks(&dl->disk);
1712 if (blocks < min_active || min_active == 0) {
1713 dl_min = dl;
1714 min_active = blocks;
1715 }
1716 }
1717 if (!dl_min)
1718 return rv;
1719
1720 /* find last lba used by subarrays on the smallest active disk */
1721 e = get_extents(super, dl_min, 0);
1722 if (!e)
1723 return rv;
1724 for (i = 0; e[i].size; i++)
1725 continue;
1726
1727 remainder = min_active - e[i].start;
1728 free(e);
1729
1730 /* to give priority to recovery we should not require full
1731 IMSM_RESERVED_SECTORS from the spare */
1732 rv = MPB_SECTOR_CNT + NUM_BLOCKS_DIRTY_STRIPE_REGION;
1733
1734 /* if real reservation is smaller use that value */
1735 return (remainder < rv) ? remainder : rv;
1736 }
1737
1738 static bool is_gen_migration(struct imsm_dev *dev);
1739
1740 #define IMSM_4K_DIV 8
1741
1742 static __u64 blocks_per_migr_unit(struct intel_super *super,
1743 struct imsm_dev *dev);
1744
1745 static void print_imsm_dev(struct intel_super *super,
1746 struct imsm_dev *dev,
1747 char *uuid,
1748 int disk_idx)
1749 {
1750 __u64 sz;
1751 int slot, i;
1752 struct imsm_map *map = get_imsm_map(dev, MAP_0);
1753 struct imsm_map *map2 = get_imsm_map(dev, MAP_1);
1754 __u32 ord;
1755
1756 printf("\n");
1757 printf("[%.16s]:\n", dev->volume);
1758 printf(" Subarray : %d\n", super->current_vol);
1759 printf(" UUID : %s\n", uuid);
1760 printf(" RAID Level : %d", get_imsm_raid_level(map));
1761 if (map2)
1762 printf(" <-- %d", get_imsm_raid_level(map2));
1763 printf("\n");
1764 printf(" Members : %d", map->num_members);
1765 if (map2)
1766 printf(" <-- %d", map2->num_members);
1767 printf("\n");
1768 printf(" Slots : [");
1769 for (i = 0; i < map->num_members; i++) {
1770 ord = get_imsm_ord_tbl_ent(dev, i, MAP_0);
1771 printf("%s", ord & IMSM_ORD_REBUILD ? "_" : "U");
1772 }
1773 printf("]");
1774 if (map2) {
1775 printf(" <-- [");
1776 for (i = 0; i < map2->num_members; i++) {
1777 ord = get_imsm_ord_tbl_ent(dev, i, MAP_1);
1778 printf("%s", ord & IMSM_ORD_REBUILD ? "_" : "U");
1779 }
1780 printf("]");
1781 }
1782 printf("\n");
1783 printf(" Failed disk : ");
1784 if (map->failed_disk_num == 0xff)
1785 printf(STR_COMMON_NONE);
1786 else
1787 printf("%i", map->failed_disk_num);
1788 printf("\n");
1789 slot = get_imsm_disk_slot(map, disk_idx);
1790 if (slot >= 0) {
1791 ord = get_imsm_ord_tbl_ent(dev, slot, MAP_X);
1792 printf(" This Slot : %d%s\n", slot,
1793 ord & IMSM_ORD_REBUILD ? " (out-of-sync)" : "");
1794 } else
1795 printf(" This Slot : ?\n");
1796 printf(" Sector Size : %u\n", super->sector_size);
1797 sz = imsm_dev_size(dev);
1798 printf(" Array Size : %llu%s\n",
1799 (unsigned long long)sz * 512 / super->sector_size,
1800 human_size(sz * 512));
1801 sz = blocks_per_member(map);
1802 printf(" Per Dev Size : %llu%s\n",
1803 (unsigned long long)sz * 512 / super->sector_size,
1804 human_size(sz * 512));
1805 printf(" Sector Offset : %llu\n",
1806 pba_of_lba0(map) * 512 / super->sector_size);
1807 printf(" Num Stripes : %llu\n",
1808 num_data_stripes(map));
1809 printf(" Chunk Size : %u KiB",
1810 __le16_to_cpu(map->blocks_per_strip) / 2);
1811 if (map2)
1812 printf(" <-- %u KiB",
1813 __le16_to_cpu(map2->blocks_per_strip) / 2);
1814 printf("\n");
1815 printf(" Reserved : %d\n", __le32_to_cpu(dev->reserved_blocks));
1816 printf(" Migrate State : ");
1817 if (dev->vol.migr_state) {
1818 if (migr_type(dev) == MIGR_INIT)
1819 printf("initialize\n");
1820 else if (migr_type(dev) == MIGR_REBUILD)
1821 printf("rebuild\n");
1822 else if (migr_type(dev) == MIGR_VERIFY)
1823 printf("check\n");
1824 else if (migr_type(dev) == MIGR_GEN_MIGR)
1825 printf("general migration\n");
1826 else if (migr_type(dev) == MIGR_STATE_CHANGE)
1827 printf("state change\n");
1828 else if (migr_type(dev) == MIGR_REPAIR)
1829 printf("repair\n");
1830 else
1831 printf("<unknown:%d>\n", migr_type(dev));
1832 } else
1833 printf("idle\n");
1834 printf(" Map State : %s", map_state_str[map->map_state]);
1835 if (dev->vol.migr_state) {
1836 struct imsm_map *map = get_imsm_map(dev, MAP_1);
1837
1838 printf(" <-- %s", map_state_str[map->map_state]);
1839 printf("\n Checkpoint : %llu ", vol_curr_migr_unit(dev));
1840 if (is_gen_migration(dev) && (slot > 1 || slot < 0))
1841 printf("(N/A)");
1842 else
1843 printf("(%llu)", (unsigned long long)
1844 blocks_per_migr_unit(super, dev));
1845 }
1846 printf("\n");
1847 printf(" Dirty State : %s\n", (dev->vol.dirty & RAIDVOL_DIRTY) ?
1848 "dirty" : "clean");
1849 printf(" RWH Policy : ");
1850 if (dev->rwh_policy == RWH_OFF || dev->rwh_policy == RWH_MULTIPLE_OFF)
1851 printf("off\n");
1852 else if (dev->rwh_policy == RWH_DISTRIBUTED)
1853 printf("PPL distributed\n");
1854 else if (dev->rwh_policy == RWH_JOURNALING_DRIVE)
1855 printf("PPL journaling drive\n");
1856 else if (dev->rwh_policy == RWH_MULTIPLE_DISTRIBUTED)
1857 printf("Multiple distributed PPLs\n");
1858 else if (dev->rwh_policy == RWH_MULTIPLE_PPLS_JOURNALING_DRIVE)
1859 printf("Multiple PPLs on journaling drive\n");
1860 else if (dev->rwh_policy == RWH_BITMAP)
1861 printf("Write-intent bitmap\n");
1862 else
1863 printf("<unknown:%d>\n", dev->rwh_policy);
1864
1865 printf(" Volume ID : %u\n", dev->my_vol_raid_dev_num);
1866 }
1867
1868 static void print_imsm_disk(struct imsm_disk *disk,
1869 int index,
1870 __u32 reserved,
1871 unsigned int sector_size) {
1872 char str[MAX_RAID_SERIAL_LEN + 1];
1873 __u64 sz;
1874
1875 if (index < -1 || !disk)
1876 return;
1877
1878 printf("\n");
1879 snprintf(str, MAX_RAID_SERIAL_LEN + 1, "%s", disk->serial);
1880 if (index >= 0)
1881 printf(" Disk%02d Serial : %s\n", index, str);
1882 else
1883 printf(" Disk Serial : %s\n", str);
1884 printf(" State :%s%s%s%s\n", is_spare(disk) ? " spare" : "",
1885 is_configured(disk) ? " active" : "",
1886 is_failed(disk) ? " failed" : "",
1887 is_journal(disk) ? " journal" : "");
1888 printf(" Id : %08x\n", __le32_to_cpu(disk->scsi_id));
1889 sz = total_blocks(disk) - reserved;
1890 printf(" Usable Size : %llu%s\n",
1891 (unsigned long long)sz * 512 / sector_size,
1892 human_size(sz * 512));
1893 }
1894
1895 void convert_to_4k_imsm_migr_rec(struct intel_super *super)
1896 {
1897 struct migr_record *migr_rec = super->migr_rec;
1898
1899 migr_rec->blocks_per_unit /= IMSM_4K_DIV;
1900 migr_rec->dest_depth_per_unit /= IMSM_4K_DIV;
1901 split_ull((join_u32(migr_rec->post_migr_vol_cap,
1902 migr_rec->post_migr_vol_cap_hi) / IMSM_4K_DIV),
1903 &migr_rec->post_migr_vol_cap, &migr_rec->post_migr_vol_cap_hi);
1904 set_migr_chkp_area_pba(migr_rec,
1905 migr_chkp_area_pba(migr_rec) / IMSM_4K_DIV);
1906 set_migr_dest_1st_member_lba(migr_rec,
1907 migr_dest_1st_member_lba(migr_rec) / IMSM_4K_DIV);
1908 }
1909
1910 void convert_to_4k_imsm_disk(struct imsm_disk *disk)
1911 {
1912 set_total_blocks(disk, (total_blocks(disk)/IMSM_4K_DIV));
1913 }
1914
1915 void convert_to_4k(struct intel_super *super)
1916 {
1917 struct imsm_super *mpb = super->anchor;
1918 struct imsm_disk *disk;
1919 int i;
1920 __u32 bbm_log_size = __le32_to_cpu(mpb->bbm_log_size);
1921
1922 for (i = 0; i < mpb->num_disks ; i++) {
1923 disk = __get_imsm_disk(mpb, i);
1924 /* disk */
1925 convert_to_4k_imsm_disk(disk);
1926 }
1927 for (i = 0; i < mpb->num_raid_devs; i++) {
1928 struct imsm_dev *dev = __get_imsm_dev(mpb, i);
1929 struct imsm_map *map = get_imsm_map(dev, MAP_0);
1930 /* dev */
1931 set_imsm_dev_size(dev, imsm_dev_size(dev)/IMSM_4K_DIV);
1932 set_vol_curr_migr_unit(dev,
1933 vol_curr_migr_unit(dev) / IMSM_4K_DIV);
1934
1935 /* map0 */
1936 set_blocks_per_member(map, blocks_per_member(map)/IMSM_4K_DIV);
1937 map->blocks_per_strip /= IMSM_4K_DIV;
1938 set_pba_of_lba0(map, pba_of_lba0(map)/IMSM_4K_DIV);
1939
1940 if (dev->vol.migr_state) {
1941 /* map1 */
1942 map = get_imsm_map(dev, MAP_1);
1943 set_blocks_per_member(map,
1944 blocks_per_member(map)/IMSM_4K_DIV);
1945 map->blocks_per_strip /= IMSM_4K_DIV;
1946 set_pba_of_lba0(map, pba_of_lba0(map)/IMSM_4K_DIV);
1947 }
1948 }
1949 if (bbm_log_size) {
1950 struct bbm_log *log = (void *)mpb +
1951 __le32_to_cpu(mpb->mpb_size) - bbm_log_size;
1952 __u32 i;
1953
1954 for (i = 0; i < log->entry_count; i++) {
1955 struct bbm_log_entry *entry =
1956 &log->marked_block_entries[i];
1957
1958 __u8 count = entry->marked_count + 1;
1959 unsigned long long sector =
1960 __le48_to_cpu(&entry->defective_block_start);
1961
1962 entry->defective_block_start =
1963 __cpu_to_le48(sector/IMSM_4K_DIV);
1964 entry->marked_count = max(count/IMSM_4K_DIV, 1) - 1;
1965 }
1966 }
1967
1968 mpb->check_sum = __gen_imsm_checksum(mpb);
1969 }
1970
1971 void examine_migr_rec_imsm(struct intel_super *super)
1972 {
1973 struct migr_record *migr_rec = super->migr_rec;
1974 struct imsm_super *mpb = super->anchor;
1975 int i;
1976
1977 for (i = 0; i < mpb->num_raid_devs; i++) {
1978 struct imsm_dev *dev = __get_imsm_dev(mpb, i);
1979 struct imsm_map *map;
1980 int slot = -1;
1981
1982 if (is_gen_migration(dev) == false)
1983 continue;
1984
1985 printf("\nMigration Record Information:");
1986
1987 /* first map under migration */
1988 map = get_imsm_map(dev, MAP_0);
1989
1990 if (map)
1991 slot = get_imsm_disk_slot(map, super->disks->index);
1992 if (map == NULL || slot > 1 || slot < 0) {
1993 printf(" Empty\n ");
1994 printf("Examine one of first two disks in array\n");
1995 break;
1996 }
1997 printf("\n Status : ");
1998 if (__le32_to_cpu(migr_rec->rec_status) == UNIT_SRC_NORMAL)
1999 printf("Normal\n");
2000 else
2001 printf("Contains Data\n");
2002 printf(" Current Unit : %llu\n",
2003 current_migr_unit(migr_rec));
2004 printf(" Family : %u\n",
2005 __le32_to_cpu(migr_rec->family_num));
2006 printf(" Ascending : %u\n",
2007 __le32_to_cpu(migr_rec->ascending_migr));
2008 printf(" Blocks Per Unit : %u\n",
2009 __le32_to_cpu(migr_rec->blocks_per_unit));
2010 printf(" Dest. Depth Per Unit : %u\n",
2011 __le32_to_cpu(migr_rec->dest_depth_per_unit));
2012 printf(" Checkpoint Area pba : %llu\n",
2013 migr_chkp_area_pba(migr_rec));
2014 printf(" First member lba : %llu\n",
2015 migr_dest_1st_member_lba(migr_rec));
2016 printf(" Total Number of Units : %llu\n",
2017 get_num_migr_units(migr_rec));
2018 printf(" Size of volume : %llu\n",
2019 join_u32(migr_rec->post_migr_vol_cap,
2020 migr_rec->post_migr_vol_cap_hi));
2021 printf(" Record was read from : %u\n",
2022 __le32_to_cpu(migr_rec->ckpt_read_disk_num));
2023
2024 break;
2025 }
2026 }
2027
2028 void convert_from_4k_imsm_migr_rec(struct intel_super *super)
2029 {
2030 struct migr_record *migr_rec = super->migr_rec;
2031
2032 migr_rec->blocks_per_unit *= IMSM_4K_DIV;
2033 migr_rec->dest_depth_per_unit *= IMSM_4K_DIV;
2034 split_ull((join_u32(migr_rec->post_migr_vol_cap,
2035 migr_rec->post_migr_vol_cap_hi) * IMSM_4K_DIV),
2036 &migr_rec->post_migr_vol_cap,
2037 &migr_rec->post_migr_vol_cap_hi);
2038 set_migr_chkp_area_pba(migr_rec,
2039 migr_chkp_area_pba(migr_rec) * IMSM_4K_DIV);
2040 set_migr_dest_1st_member_lba(migr_rec,
2041 migr_dest_1st_member_lba(migr_rec) * IMSM_4K_DIV);
2042 }
2043
2044 void convert_from_4k(struct intel_super *super)
2045 {
2046 struct imsm_super *mpb = super->anchor;
2047 struct imsm_disk *disk;
2048 int i;
2049 __u32 bbm_log_size = __le32_to_cpu(mpb->bbm_log_size);
2050
2051 for (i = 0; i < mpb->num_disks ; i++) {
2052 disk = __get_imsm_disk(mpb, i);
2053 /* disk */
2054 set_total_blocks(disk, (total_blocks(disk)*IMSM_4K_DIV));
2055 }
2056
2057 for (i = 0; i < mpb->num_raid_devs; i++) {
2058 struct imsm_dev *dev = __get_imsm_dev(mpb, i);
2059 struct imsm_map *map = get_imsm_map(dev, MAP_0);
2060 /* dev */
2061 set_imsm_dev_size(dev, imsm_dev_size(dev)*IMSM_4K_DIV);
2062 set_vol_curr_migr_unit(dev,
2063 vol_curr_migr_unit(dev) * IMSM_4K_DIV);
2064
2065 /* map0 */
2066 set_blocks_per_member(map, blocks_per_member(map)*IMSM_4K_DIV);
2067 map->blocks_per_strip *= IMSM_4K_DIV;
2068 set_pba_of_lba0(map, pba_of_lba0(map)*IMSM_4K_DIV);
2069
2070 if (dev->vol.migr_state) {
2071 /* map1 */
2072 map = get_imsm_map(dev, MAP_1);
2073 set_blocks_per_member(map,
2074 blocks_per_member(map)*IMSM_4K_DIV);
2075 map->blocks_per_strip *= IMSM_4K_DIV;
2076 set_pba_of_lba0(map, pba_of_lba0(map)*IMSM_4K_DIV);
2077 }
2078 }
2079 if (bbm_log_size) {
2080 struct bbm_log *log = (void *)mpb +
2081 __le32_to_cpu(mpb->mpb_size) - bbm_log_size;
2082 __u32 i;
2083
2084 for (i = 0; i < log->entry_count; i++) {
2085 struct bbm_log_entry *entry =
2086 &log->marked_block_entries[i];
2087
2088 __u8 count = entry->marked_count + 1;
2089 unsigned long long sector =
2090 __le48_to_cpu(&entry->defective_block_start);
2091
2092 entry->defective_block_start =
2093 __cpu_to_le48(sector*IMSM_4K_DIV);
2094 entry->marked_count = count*IMSM_4K_DIV - 1;
2095 }
2096 }
2097
2098 mpb->check_sum = __gen_imsm_checksum(mpb);
2099 }
2100
2101 /*******************************************************************************
2102 * function: imsm_check_attributes
2103 * Description: Function checks if features represented by attributes flags
2104 * are supported by mdadm.
2105 * Parameters:
2106 * attributes - Attributes read from metadata
2107 * Returns:
2108 * 0 - passed attributes contains unsupported features flags
2109 * 1 - all features are supported
2110 ******************************************************************************/
2111 static int imsm_check_attributes(__u32 attributes)
2112 {
2113 int ret_val = 1;
2114 __u32 not_supported = MPB_ATTRIB_SUPPORTED^0xffffffff;
2115
2116 not_supported &= ~MPB_ATTRIB_IGNORED;
2117
2118 not_supported &= attributes;
2119 if (not_supported) {
2120 pr_err("(IMSM): Unsupported attributes : %x\n",
2121 (unsigned)__le32_to_cpu(not_supported));
2122 if (not_supported & MPB_ATTRIB_CHECKSUM_VERIFY) {
2123 dprintf("\t\tMPB_ATTRIB_CHECKSUM_VERIFY \n");
2124 not_supported ^= MPB_ATTRIB_CHECKSUM_VERIFY;
2125 }
2126 if (not_supported & MPB_ATTRIB_2TB) {
2127 dprintf("\t\tMPB_ATTRIB_2TB\n");
2128 not_supported ^= MPB_ATTRIB_2TB;
2129 }
2130 if (not_supported & MPB_ATTRIB_RAID0) {
2131 dprintf("\t\tMPB_ATTRIB_RAID0\n");
2132 not_supported ^= MPB_ATTRIB_RAID0;
2133 }
2134 if (not_supported & MPB_ATTRIB_RAID1) {
2135 dprintf("\t\tMPB_ATTRIB_RAID1\n");
2136 not_supported ^= MPB_ATTRIB_RAID1;
2137 }
2138 if (not_supported & MPB_ATTRIB_RAID10) {
2139 dprintf("\t\tMPB_ATTRIB_RAID10\n");
2140 not_supported ^= MPB_ATTRIB_RAID10;
2141 }
2142 if (not_supported & MPB_ATTRIB_RAID1E) {
2143 dprintf("\t\tMPB_ATTRIB_RAID1E\n");
2144 not_supported ^= MPB_ATTRIB_RAID1E;
2145 }
2146 if (not_supported & MPB_ATTRIB_RAID5) {
2147 dprintf("\t\tMPB_ATTRIB_RAID5\n");
2148 not_supported ^= MPB_ATTRIB_RAID5;
2149 }
2150 if (not_supported & MPB_ATTRIB_RAIDCNG) {
2151 dprintf("\t\tMPB_ATTRIB_RAIDCNG\n");
2152 not_supported ^= MPB_ATTRIB_RAIDCNG;
2153 }
2154 if (not_supported & MPB_ATTRIB_BBM) {
2155 dprintf("\t\tMPB_ATTRIB_BBM\n");
2156 not_supported ^= MPB_ATTRIB_BBM;
2157 }
2158 if (not_supported & MPB_ATTRIB_CHECKSUM_VERIFY) {
2159 dprintf("\t\tMPB_ATTRIB_CHECKSUM_VERIFY (== MPB_ATTRIB_LEGACY)\n");
2160 not_supported ^= MPB_ATTRIB_CHECKSUM_VERIFY;
2161 }
2162 if (not_supported & MPB_ATTRIB_EXP_STRIPE_SIZE) {
2163 dprintf("\t\tMPB_ATTRIB_EXP_STRIP_SIZE\n");
2164 not_supported ^= MPB_ATTRIB_EXP_STRIPE_SIZE;
2165 }
2166 if (not_supported & MPB_ATTRIB_2TB_DISK) {
2167 dprintf("\t\tMPB_ATTRIB_2TB_DISK\n");
2168 not_supported ^= MPB_ATTRIB_2TB_DISK;
2169 }
2170 if (not_supported & MPB_ATTRIB_NEVER_USE2) {
2171 dprintf("\t\tMPB_ATTRIB_NEVER_USE2\n");
2172 not_supported ^= MPB_ATTRIB_NEVER_USE2;
2173 }
2174 if (not_supported & MPB_ATTRIB_NEVER_USE) {
2175 dprintf("\t\tMPB_ATTRIB_NEVER_USE\n");
2176 not_supported ^= MPB_ATTRIB_NEVER_USE;
2177 }
2178
2179 if (not_supported)
2180 dprintf("(IMSM): Unknown attributes : %x\n", not_supported);
2181
2182 ret_val = 0;
2183 }
2184
2185 return ret_val;
2186 }
2187
2188 static void getinfo_super_imsm(struct supertype *st, struct mdinfo *info, char *map);
2189
2190 static void examine_super_imsm(struct supertype *st, char *homehost)
2191 {
2192 struct intel_super *super = st->sb;
2193 struct imsm_super *mpb = super->anchor;
2194 char str[MAX_SIGNATURE_LENGTH];
2195 int i;
2196 struct mdinfo info;
2197 char nbuf[64];
2198 __u32 sum;
2199 __u32 reserved = imsm_reserved_sectors(super, super->disks);
2200 struct dl *dl;
2201 time_t creation_time;
2202
2203 strncpy(str, (char *)mpb->sig, MPB_SIG_LEN);
2204 str[MPB_SIG_LEN-1] = '\0';
2205 printf(" Magic : %s\n", str);
2206 printf(" Version : %s\n", get_imsm_version(mpb));
2207 printf(" Orig Family : %08x\n", __le32_to_cpu(mpb->orig_family_num));
2208 printf(" Family : %08x\n", __le32_to_cpu(mpb->family_num));
2209 printf(" Generation : %08x\n", __le32_to_cpu(mpb->generation_num));
2210 creation_time = __le64_to_cpu(mpb->creation_time);
2211 printf(" Creation Time : %.24s\n",
2212 creation_time ? ctime(&creation_time) : "Unknown");
2213 printf(" Attributes : ");
2214 if (imsm_check_attributes(mpb->attributes))
2215 printf("All supported\n");
2216 else
2217 printf("not supported\n");
2218 getinfo_super_imsm(st, &info, NULL);
2219 fname_from_uuid(&info, nbuf);
2220 printf(" UUID : %s\n", nbuf + 5);
2221 sum = __le32_to_cpu(mpb->check_sum);
2222 printf(" Checksum : %08x %s\n", sum,
2223 __gen_imsm_checksum(mpb) == sum ? "correct" : "incorrect");
2224 printf(" MPB Sectors : %d\n", mpb_sectors(mpb, super->sector_size));
2225 printf(" Disks : %d\n", mpb->num_disks);
2226 printf(" RAID Devices : %d\n", mpb->num_raid_devs);
2227 print_imsm_disk(__get_imsm_disk(mpb, super->disks->index),
2228 super->disks->index, reserved, super->sector_size);
2229 if (get_imsm_bbm_log_size(super->bbm_log)) {
2230 struct bbm_log *log = super->bbm_log;
2231
2232 printf("\n");
2233 printf("Bad Block Management Log:\n");
2234 printf(" Log Size : %d\n", __le32_to_cpu(mpb->bbm_log_size));
2235 printf(" Signature : %x\n", __le32_to_cpu(log->signature));
2236 printf(" Entry Count : %d\n", __le32_to_cpu(log->entry_count));
2237 }
2238 for (i = 0; i < mpb->num_raid_devs; i++) {
2239 struct mdinfo info;
2240 struct imsm_dev *dev = __get_imsm_dev(mpb, i);
2241
2242 super->current_vol = i;
2243 getinfo_super_imsm(st, &info, NULL);
2244 fname_from_uuid(&info, nbuf);
2245 print_imsm_dev(super, dev, nbuf + 5, super->disks->index);
2246 }
2247 for (i = 0; i < mpb->num_disks; i++) {
2248 if (i == super->disks->index)
2249 continue;
2250 print_imsm_disk(__get_imsm_disk(mpb, i), i, reserved,
2251 super->sector_size);
2252 }
2253
2254 for (dl = super->disks; dl; dl = dl->next)
2255 if (dl->index == -1)
2256 print_imsm_disk(&dl->disk, -1, reserved,
2257 super->sector_size);
2258
2259 examine_migr_rec_imsm(super);
2260 }
2261
2262 static void brief_examine_super_imsm(struct supertype *st, int verbose)
2263 {
2264 /* We just write a generic IMSM ARRAY entry */
2265 struct mdinfo info;
2266 char nbuf[64];
2267
2268 getinfo_super_imsm(st, &info, NULL);
2269 fname_from_uuid(&info, nbuf);
2270 printf("ARRAY metadata=imsm UUID=%s\n", nbuf + 5);
2271 }
2272
2273 static void brief_examine_subarrays_imsm(struct supertype *st, int verbose)
2274 {
2275 /* We just write a generic IMSM ARRAY entry */
2276 struct mdinfo info;
2277 char nbuf[64];
2278 char nbuf1[64];
2279 struct intel_super *super = st->sb;
2280 int i;
2281
2282 if (!super->anchor->num_raid_devs)
2283 return;
2284
2285 getinfo_super_imsm(st, &info, NULL);
2286 fname_from_uuid(&info, nbuf);
2287 for (i = 0; i < super->anchor->num_raid_devs; i++) {
2288 struct imsm_dev *dev = get_imsm_dev(super, i);
2289
2290 super->current_vol = i;
2291 getinfo_super_imsm(st, &info, NULL);
2292 fname_from_uuid(&info, nbuf1);
2293 printf("ARRAY " DEV_MD_DIR "%.16s container=%s member=%d UUID=%s\n",
2294 dev->volume, nbuf + 5, i, nbuf1 + 5);
2295 }
2296 }
2297
2298 static void export_examine_super_imsm(struct supertype *st)
2299 {
2300 struct intel_super *super = st->sb;
2301 struct imsm_super *mpb = super->anchor;
2302 struct mdinfo info;
2303 char nbuf[64];
2304
2305 getinfo_super_imsm(st, &info, NULL);
2306 fname_from_uuid(&info, nbuf);
2307 printf("MD_METADATA=imsm\n");
2308 printf("MD_LEVEL=container\n");
2309 printf("MD_UUID=%s\n", nbuf+5);
2310 printf("MD_DEVICES=%u\n", mpb->num_disks);
2311 printf("MD_CREATION_TIME=%llu\n", __le64_to_cpu(mpb->creation_time));
2312 }
2313
2314 static void detail_super_imsm(struct supertype *st, char *homehost,
2315 char *subarray)
2316 {
2317 struct mdinfo info;
2318 char nbuf[64];
2319 struct intel_super *super = st->sb;
2320 int temp_vol = super->current_vol;
2321
2322 if (subarray)
2323 super->current_vol = strtoul(subarray, NULL, 10);
2324
2325 getinfo_super_imsm(st, &info, NULL);
2326 fname_from_uuid(&info, nbuf);
2327 printf("\n UUID : %s\n", nbuf + 5);
2328
2329 super->current_vol = temp_vol;
2330 }
2331
2332 static void brief_detail_super_imsm(struct supertype *st, char *subarray)
2333 {
2334 struct mdinfo info;
2335 char nbuf[64];
2336 struct intel_super *super = st->sb;
2337 int temp_vol = super->current_vol;
2338
2339 if (subarray)
2340 super->current_vol = strtoul(subarray, NULL, 10);
2341
2342 getinfo_super_imsm(st, &info, NULL);
2343 fname_from_uuid(&info, nbuf);
2344 printf(" UUID=%s", nbuf + 5);
2345
2346 super->current_vol = temp_vol;
2347 }
2348
2349 static int imsm_read_serial(int fd, char *devname, __u8 *serial,
2350 size_t serial_buf_len);
2351 static void fd2devname(int fd, char *name);
2352
2353 void print_encryption_information(int disk_fd, enum sys_dev_type hba_type)
2354 {
2355 struct encryption_information information = {0};
2356 mdadm_status_t status = MDADM_STATUS_SUCCESS;
2357 const char *indent = " ";
2358
2359 switch (hba_type) {
2360 case SYS_DEV_VMD:
2361 case SYS_DEV_NVME:
2362 status = get_nvme_opal_encryption_information(disk_fd, &information, 1);
2363 break;
2364 case SYS_DEV_SATA:
2365 case SYS_DEV_SATA_VMD:
2366 status = get_ata_encryption_information(disk_fd, &information, 1);
2367 break;
2368 default:
2369 return;
2370 }
2371
2372 if (status) {
2373 pr_err("Failed to get drive encryption information.\n");
2374 return;
2375 }
2376
2377 printf("%sEncryption(Ability|Status): %s|%s\n", indent,
2378 get_encryption_ability_string(information.ability),
2379 get_encryption_status_string(information.status));
2380 }
2381
2382 static int ahci_enumerate_ports(struct sys_dev *hba, int port_count, int host_base, int verbose)
2383 {
2384 /* dump an unsorted list of devices attached to AHCI Intel storage
2385 * controller, as well as non-connected ports
2386 */
2387 int hba_len = strlen(hba->path) + 1;
2388 struct dirent *ent;
2389 DIR *dir;
2390 char *path = NULL;
2391 int err = 0;
2392 unsigned long port_mask = (1 << port_count) - 1;
2393
2394 if (port_count > (int)sizeof(port_mask) * 8) {
2395 if (verbose > 0)
2396 pr_err("port_count %d out of range\n", port_count);
2397 return 2;
2398 }
2399
2400 /* scroll through /sys/dev/block looking for devices attached to
2401 * this hba
2402 */
2403 dir = opendir("/sys/dev/block");
2404 if (!dir)
2405 return 1;
2406
2407 for (ent = readdir(dir); ent; ent = readdir(dir)) {
2408 int fd;
2409 char model[64];
2410 char vendor[64];
2411 char buf[1024];
2412 int major, minor;
2413 char device[PATH_MAX];
2414 char *c;
2415 int port;
2416 int type;
2417
2418 if (sscanf(ent->d_name, "%d:%d", &major, &minor) != 2)
2419 continue;
2420 path = devt_to_devpath(makedev(major, minor), 1, NULL);
2421 if (!path)
2422 continue;
2423 if (!path_attached_to_hba(path, hba->path)) {
2424 free(path);
2425 path = NULL;
2426 continue;
2427 }
2428
2429 /* retrieve the scsi device */
2430 if (!devt_to_devpath(makedev(major, minor), 1, device)) {
2431 if (verbose > 0)
2432 pr_err("failed to get device\n");
2433 err = 2;
2434 break;
2435 }
2436 if (devpath_to_char(device, "type", buf, sizeof(buf), 0)) {
2437 err = 2;
2438 break;
2439 }
2440 type = strtoul(buf, NULL, 10);
2441
2442 /* if it's not a disk print the vendor and model */
2443 if (!(type == 0 || type == 7 || type == 14)) {
2444 vendor[0] = '\0';
2445 model[0] = '\0';
2446
2447 if (devpath_to_char(device, "vendor", buf,
2448 sizeof(buf), 0) == 0) {
2449 strncpy(vendor, buf, sizeof(vendor));
2450 vendor[sizeof(vendor) - 1] = '\0';
2451 c = (char *) &vendor[sizeof(vendor) - 1];
2452 while (isspace(*c) || *c == '\0')
2453 *c-- = '\0';
2454
2455 }
2456
2457 if (devpath_to_char(device, "model", buf,
2458 sizeof(buf), 0) == 0) {
2459 strncpy(model, buf, sizeof(model));
2460 model[sizeof(model) - 1] = '\0';
2461 c = (char *) &model[sizeof(model) - 1];
2462 while (isspace(*c) || *c == '\0')
2463 *c-- = '\0';
2464 }
2465
2466 if (vendor[0] && model[0])
2467 sprintf(buf, "%.64s %.64s", vendor, model);
2468 else
2469 switch (type) { /* numbers from hald/linux/device.c */
2470 case 1: sprintf(buf, "tape"); break;
2471 case 2: sprintf(buf, "printer"); break;
2472 case 3: sprintf(buf, "processor"); break;
2473 case 4:
2474 case 5: sprintf(buf, "cdrom"); break;
2475 case 6: sprintf(buf, "scanner"); break;
2476 case 8: sprintf(buf, "media_changer"); break;
2477 case 9: sprintf(buf, "comm"); break;
2478 case 12: sprintf(buf, "raid"); break;
2479 default: sprintf(buf, "unknown");
2480 }
2481 } else
2482 buf[0] = '\0';
2483
2484 /* chop device path to 'host%d' and calculate the port number */
2485 c = strchr(&path[hba_len], '/');
2486 if (!c) {
2487 if (verbose > 0)
2488 pr_err("%s - invalid path name\n", path + hba_len);
2489 err = 2;
2490 break;
2491 }
2492 *c = '\0';
2493 if ((sscanf(&path[hba_len], "ata%d", &port) == 1) ||
2494 ((sscanf(&path[hba_len], "host%d", &port) == 1)))
2495 port -= host_base;
2496 else {
2497 if (verbose > 0) {
2498 *c = '/'; /* repair the full string */
2499 pr_err("failed to determine port number for %s\n",
2500 path);
2501 }
2502 err = 2;
2503 break;
2504 }
2505
2506 /* mark this port as used */
2507 port_mask &= ~(1 << port);
2508
2509 /* print out the device information */
2510 if (buf[0]) {
2511 printf(" Port%d : - non-disk device (%s) -\n", port, buf);
2512 continue;
2513 }
2514
2515 fd = dev_open(ent->d_name, O_RDONLY);
2516 if (!is_fd_valid(fd))
2517 printf(" Port%d : - disk info unavailable -\n", port);
2518 else {
2519 fd2devname(fd, buf);
2520 printf(" Port%d : %s", port, buf);
2521 if (imsm_read_serial(fd, NULL, (__u8 *)buf,
2522 sizeof(buf)) == 0)
2523 printf(" (%s)\n", buf);
2524 else
2525 printf(" ()\n");
2526
2527 print_encryption_information(fd, hba->type);
2528 close(fd);
2529 }
2530 free(path);
2531 path = NULL;
2532 }
2533 if (path)
2534 free(path);
2535 if (dir)
2536 closedir(dir);
2537 if (err == 0) {
2538 int i;
2539
2540 for (i = 0; i < port_count; i++)
2541 if (port_mask & (1 << i))
2542 printf(" Port%d : - no device attached -\n", i);
2543 }
2544
2545 return err;
2546 }
2547
2548 static int print_nvme_info(struct sys_dev *hba)
2549 {
2550 struct dirent *ent;
2551 DIR *dir;
2552
2553 dir = opendir("/sys/block/");
2554 if (!dir)
2555 return 1;
2556
2557 for (ent = readdir(dir); ent; ent = readdir(dir)) {
2558 char ns_path[PATH_MAX];
2559 char cntrl_path[PATH_MAX];
2560 char buf[PATH_MAX];
2561 int fd = -1;
2562
2563 if (!strstr(ent->d_name, "nvme"))
2564 goto skip;
2565
2566 fd = open_dev(ent->d_name);
2567 if (!is_fd_valid(fd))
2568 goto skip;
2569
2570 if (!diskfd_to_devpath(fd, 0, ns_path) ||
2571 !diskfd_to_devpath(fd, 1, cntrl_path))
2572 goto skip;
2573
2574 if (!path_attached_to_hba(cntrl_path, hba->path))
2575 goto skip;
2576
2577 if (!imsm_is_nvme_namespace_supported(fd, 0))
2578 goto skip;
2579
2580 fd2devname(fd, buf);
2581 if (hba->type == SYS_DEV_VMD)
2582 printf(" NVMe under VMD : %s", buf);
2583 else if (hba->type == SYS_DEV_NVME)
2584 printf(" NVMe Device : %s", buf);
2585
2586 if (!imsm_read_serial(fd, NULL, (__u8 *)buf,
2587 sizeof(buf)))
2588 printf(" (%s)\n", buf);
2589 else
2590 printf("()\n");
2591
2592 print_encryption_information(fd, hba->type);
2593
2594 skip:
2595 close_fd(&fd);
2596 }
2597
2598 closedir(dir);
2599 return 0;
2600 }
2601
2602 static void print_found_intel_controllers(struct sys_dev *elem)
2603 {
2604 for (; elem; elem = elem->next) {
2605 pr_err("found Intel(R) ");
2606 if (elem->type == SYS_DEV_SATA)
2607 fprintf(stderr, "SATA ");
2608 else if (elem->type == SYS_DEV_SAS)
2609 fprintf(stderr, "SAS ");
2610 else if (elem->type == SYS_DEV_NVME)
2611 fprintf(stderr, "NVMe ");
2612
2613 if (elem->type == SYS_DEV_VMD)
2614 fprintf(stderr, "VMD domain");
2615 else if (elem->type == SYS_DEV_SATA_VMD)
2616 fprintf(stderr, "SATA VMD domain");
2617 else
2618 fprintf(stderr, "RAID controller");
2619
2620 if (elem->pci_id)
2621 fprintf(stderr, " at %s", elem->pci_id);
2622 fprintf(stderr, ".\n");
2623 }
2624 fflush(stderr);
2625 }
2626
2627 static int ahci_get_port_count(const char *hba_path, int *port_count)
2628 {
2629 struct dirent *ent;
2630 DIR *dir;
2631 int host_base = -1;
2632
2633 *port_count = 0;
2634 if ((dir = opendir(hba_path)) == NULL)
2635 return -1;
2636
2637 for (ent = readdir(dir); ent; ent = readdir(dir)) {
2638 int host;
2639
2640 if ((sscanf(ent->d_name, "ata%d", &host) != 1) &&
2641 ((sscanf(ent->d_name, "host%d", &host) != 1)))
2642 continue;
2643 if (*port_count == 0)
2644 host_base = host;
2645 else if (host < host_base)
2646 host_base = host;
2647
2648 if (host + 1 > *port_count + host_base)
2649 *port_count = host + 1 - host_base;
2650 }
2651 closedir(dir);
2652 return host_base;
2653 }
2654
2655 static void print_imsm_capability(const struct imsm_orom *orom)
2656 {
2657 printf(" Platform : Intel(R) ");
2658 if (orom->capabilities == 0 && orom->driver_features == 0)
2659 printf("Matrix Storage Manager\n");
2660 else if (imsm_orom_is_enterprise(orom) && orom->major_ver >= 6)
2661 printf("Virtual RAID on CPU\n");
2662 else
2663 printf("Rapid Storage Technology%s\n",
2664 imsm_orom_is_enterprise(orom) ? " enterprise" : "");
2665 if (orom->major_ver || orom->minor_ver || orom->hotfix_ver || orom->build) {
2666 if (imsm_orom_is_vmd_without_efi(orom))
2667 printf(" Version : %d.%d\n", orom->major_ver,
2668 orom->minor_ver);
2669 else
2670 printf(" Version : %d.%d.%d.%d\n", orom->major_ver,
2671 orom->minor_ver, orom->hotfix_ver, orom->build);
2672 }
2673 printf(" RAID Levels :%s%s%s%s%s\n",
2674 imsm_orom_has_raid0(orom) ? " raid0" : "",
2675 imsm_orom_has_raid1(orom) ? " raid1" : "",
2676 imsm_orom_has_raid1e(orom) ? " raid1e" : "",
2677 imsm_orom_has_raid10(orom) ? " raid10" : "",
2678 imsm_orom_has_raid5(orom) ? " raid5" : "");
2679 printf(" Chunk Sizes :%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
2680 imsm_orom_has_chunk(orom, 2) ? " 2k" : "",
2681 imsm_orom_has_chunk(orom, 4) ? " 4k" : "",
2682 imsm_orom_has_chunk(orom, 8) ? " 8k" : "",
2683 imsm_orom_has_chunk(orom, 16) ? " 16k" : "",
2684 imsm_orom_has_chunk(orom, 32) ? " 32k" : "",
2685 imsm_orom_has_chunk(orom, 64) ? " 64k" : "",
2686 imsm_orom_has_chunk(orom, 128) ? " 128k" : "",
2687 imsm_orom_has_chunk(orom, 256) ? " 256k" : "",
2688 imsm_orom_has_chunk(orom, 512) ? " 512k" : "",
2689 imsm_orom_has_chunk(orom, 1024*1) ? " 1M" : "",
2690 imsm_orom_has_chunk(orom, 1024*2) ? " 2M" : "",
2691 imsm_orom_has_chunk(orom, 1024*4) ? " 4M" : "",
2692 imsm_orom_has_chunk(orom, 1024*8) ? " 8M" : "",
2693 imsm_orom_has_chunk(orom, 1024*16) ? " 16M" : "",
2694 imsm_orom_has_chunk(orom, 1024*32) ? " 32M" : "",
2695 imsm_orom_has_chunk(orom, 1024*64) ? " 64M" : "");
2696 printf(" 2TB volumes :%s supported\n",
2697 (orom->attr & IMSM_OROM_ATTR_2TB)?"":" not");
2698 printf(" 2TB disks :%s supported\n",
2699 (orom->attr & IMSM_OROM_ATTR_2TB_DISK)?"":" not");
2700 printf(" Max Disks : %d\n", orom->tds);
2701 printf(" Max Volumes : %d per array, %d per %s\n",
2702 orom->vpa, orom->vphba,
2703 imsm_orom_is_nvme(orom) ? "platform" : "controller");
2704 return;
2705 }
2706
2707 static void print_imsm_capability_export(const struct imsm_orom *orom)
2708 {
2709 printf("MD_FIRMWARE_TYPE=imsm\n");
2710 if (orom->major_ver || orom->minor_ver || orom->hotfix_ver || orom->build)
2711 printf("IMSM_VERSION=%d.%d.%d.%d\n", orom->major_ver, orom->minor_ver,
2712 orom->hotfix_ver, orom->build);
2713 printf("IMSM_SUPPORTED_RAID_LEVELS=%s%s%s%s%s\n",
2714 imsm_orom_has_raid0(orom) ? "raid0 " : "",
2715 imsm_orom_has_raid1(orom) ? "raid1 " : "",
2716 imsm_orom_has_raid1e(orom) ? "raid1e " : "",
2717 imsm_orom_has_raid5(orom) ? "raid10 " : "",
2718 imsm_orom_has_raid10(orom) ? "raid5 " : "");
2719 printf("IMSM_SUPPORTED_CHUNK_SIZES=%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
2720 imsm_orom_has_chunk(orom, 2) ? "2k " : "",
2721 imsm_orom_has_chunk(orom, 4) ? "4k " : "",
2722 imsm_orom_has_chunk(orom, 8) ? "8k " : "",
2723 imsm_orom_has_chunk(orom, 16) ? "16k " : "",
2724 imsm_orom_has_chunk(orom, 32) ? "32k " : "",
2725 imsm_orom_has_chunk(orom, 64) ? "64k " : "",
2726 imsm_orom_has_chunk(orom, 128) ? "128k " : "",
2727 imsm_orom_has_chunk(orom, 256) ? "256k " : "",
2728 imsm_orom_has_chunk(orom, 512) ? "512k " : "",
2729 imsm_orom_has_chunk(orom, 1024*1) ? "1M " : "",
2730 imsm_orom_has_chunk(orom, 1024*2) ? "2M " : "",
2731 imsm_orom_has_chunk(orom, 1024*4) ? "4M " : "",
2732 imsm_orom_has_chunk(orom, 1024*8) ? "8M " : "",
2733 imsm_orom_has_chunk(orom, 1024*16) ? "16M " : "",
2734 imsm_orom_has_chunk(orom, 1024*32) ? "32M " : "",
2735 imsm_orom_has_chunk(orom, 1024*64) ? "64M " : "");
2736 printf("IMSM_2TB_VOLUMES=%s\n",(orom->attr & IMSM_OROM_ATTR_2TB) ? "yes" : "no");
2737 printf("IMSM_2TB_DISKS=%s\n",(orom->attr & IMSM_OROM_ATTR_2TB_DISK) ? "yes" : "no");
2738 printf("IMSM_MAX_DISKS=%d\n",orom->tds);
2739 printf("IMSM_MAX_VOLUMES_PER_ARRAY=%d\n",orom->vpa);
2740 printf("IMSM_MAX_VOLUMES_PER_CONTROLLER=%d\n",orom->vphba);
2741 }
2742
2743 static int detail_platform_imsm(int verbose, int enumerate_only, char *controller_path)
2744 {
2745 /* There are two components to imsm platform support, the ahci SATA
2746 * controller and the option-rom. To find the SATA controller we
2747 * simply look in /sys/bus/pci/drivers/ahci to see if an ahci
2748 * controller with the Intel vendor id is present. This approach
2749 * allows mdadm to leverage the kernel's ahci detection logic, with the
2750 * caveat that if ahci.ko is not loaded mdadm will not be able to
2751 * detect platform raid capabilities. The option-rom resides in a
2752 * platform "Adapter ROM". We scan for its signature to retrieve the
2753 * platform capabilities. If raid support is disabled in the BIOS the
2754 * option-rom capability structure will not be available.
2755 */
2756 struct sys_dev *list, *hba;
2757 int host_base = 0;
2758 int port_count = 0;
2759 int result=1;
2760
2761 if (enumerate_only) {
2762 if (check_no_platform())
2763 return 0;
2764 list = find_intel_devices();
2765 if (!list)
2766 return 2;
2767 for (hba = list; hba; hba = hba->next) {
2768 if (find_imsm_capability(hba)) {
2769 result = 0;
2770 break;
2771 }
2772 else
2773 result = 2;
2774 }
2775 return result;
2776 }
2777
2778 list = find_intel_devices();
2779 if (!list) {
2780 if (verbose > 0)
2781 pr_err("no active Intel(R) RAID controller found.\n");
2782 return 2;
2783 } else if (verbose > 0)
2784 print_found_intel_controllers(list);
2785
2786 for (hba = list; hba; hba = hba->next) {
2787 if (controller_path && (compare_paths(hba->path, controller_path) != 0))
2788 continue;
2789 if (!find_imsm_capability(hba)) {
2790 char buf[PATH_MAX];
2791 pr_err("imsm capabilities not found for controller: %s (type %s)\n",
2792 hba->type == SYS_DEV_VMD || hba->type == SYS_DEV_SATA_VMD ?
2793 vmd_domain_to_controller(hba, buf) :
2794 hba->path, get_sys_dev_type(hba->type));
2795 continue;
2796 }
2797 result = 0;
2798 }
2799
2800 if (controller_path && result == 1) {
2801 pr_err("no active Intel(R) RAID controller found under %s\n",
2802 controller_path);
2803 return result;
2804 }
2805
2806 const struct orom_entry *entry;
2807
2808 for (entry = orom_entries; entry; entry = entry->next) {
2809 if (entry->type == SYS_DEV_VMD) {
2810 print_imsm_capability(&entry->orom);
2811 printf(" 3rd party NVMe :%s supported\n",
2812 imsm_orom_has_tpv_support(&entry->orom)?"":" not");
2813 for (hba = list; hba; hba = hba->next) {
2814 if (hba->type == SYS_DEV_VMD) {
2815 char buf[PATH_MAX];
2816 printf(" I/O Controller : %s (%s)\n",
2817 vmd_domain_to_controller(hba, buf), get_sys_dev_type(hba->type));
2818 if (print_nvme_info(hba)) {
2819 if (verbose > 0)
2820 pr_err("failed to get devices attached to VMD domain.\n");
2821 result |= 2;
2822 }
2823 }
2824 }
2825 printf("\n");
2826 continue;
2827 }
2828
2829 print_imsm_capability(&entry->orom);
2830 if (entry->type == SYS_DEV_NVME) {
2831 for (hba = list; hba; hba = hba->next) {
2832 if (hba->type == SYS_DEV_NVME)
2833 print_nvme_info(hba);
2834 }
2835 printf("\n");
2836 continue;
2837 }
2838
2839 struct devid_list *devid;
2840 for (devid = entry->devid_list; devid; devid = devid->next) {
2841 hba = device_by_id(devid->devid);
2842 if (!hba)
2843 continue;
2844
2845 printf(" I/O Controller : %s (%s)\n",
2846 hba->path, get_sys_dev_type(hba->type));
2847 if (hba->type == SYS_DEV_SATA || hba->type == SYS_DEV_SATA_VMD) {
2848 host_base = ahci_get_port_count(hba->path, &port_count);
2849 if (ahci_enumerate_ports(hba, port_count, host_base, verbose)) {
2850 if (verbose > 0)
2851 pr_err("failed to enumerate ports on %s controller at %s.\n",
2852 get_sys_dev_type(hba->type), hba->pci_id);
2853 result |= 2;
2854 }
2855 }
2856 }
2857 printf("\n");
2858 }
2859
2860 return result;
2861 }
2862
2863 static int export_detail_platform_imsm(int verbose, char *controller_path)
2864 {
2865 struct sys_dev *list, *hba;
2866 int result=1;
2867
2868 list = find_intel_devices();
2869 if (!list) {
2870 if (verbose > 0)
2871 pr_err("IMSM_DETAIL_PLATFORM_ERROR=NO_INTEL_DEVICES\n");
2872 result = 2;
2873 return result;
2874 }
2875
2876 for (hba = list; hba; hba = hba->next) {
2877 if (controller_path && (compare_paths(hba->path,controller_path) != 0))
2878 continue;
2879 if (!find_imsm_capability(hba) && verbose > 0) {
2880 char buf[PATH_MAX];
2881 pr_err("IMSM_DETAIL_PLATFORM_ERROR=NO_IMSM_CAPABLE_DEVICE_UNDER_%s\n",
2882 hba->type == SYS_DEV_VMD || hba->type == SYS_DEV_SATA_VMD ?
2883 vmd_domain_to_controller(hba, buf) : hba->path);
2884 }
2885 else
2886 result = 0;
2887 }
2888
2889 const struct orom_entry *entry;
2890
2891 for (entry = orom_entries; entry; entry = entry->next) {
2892 if (entry->type == SYS_DEV_VMD || entry->type == SYS_DEV_SATA_VMD) {
2893 for (hba = list; hba; hba = hba->next)
2894 print_imsm_capability_export(&entry->orom);
2895 continue;
2896 }
2897 print_imsm_capability_export(&entry->orom);
2898 }
2899
2900 return result;
2901 }
2902
2903 static int match_home_imsm(struct supertype *st, char *homehost)
2904 {
2905 /* the imsm metadata format does not specify any host
2906 * identification information. We return -1 since we can never
2907 * confirm nor deny whether a given array is "meant" for this
2908 * host. We rely on compare_super and the 'family_num' fields to
2909 * exclude member disks that do not belong, and we rely on
2910 * mdadm.conf to specify the arrays that should be assembled.
2911 * Auto-assembly may still pick up "foreign" arrays.
2912 */
2913
2914 return -1;
2915 }
2916
2917 static void uuid_from_super_imsm(struct supertype *st, int uuid[4])
2918 {
2919 /* The uuid returned here is used for:
2920 * uuid to put into bitmap file (Create, Grow)
2921 * uuid for backup header when saving critical section (Grow)
2922 * comparing uuids when re-adding a device into an array
2923 * In these cases the uuid required is that of the data-array,
2924 * not the device-set.
2925 * uuid to recognise same set when adding a missing device back
2926 * to an array. This is a uuid for the device-set.
2927 *
2928 * For each of these we can make do with a truncated
2929 * or hashed uuid rather than the original, as long as
2930 * everyone agrees.
2931 * In each case the uuid required is that of the data-array,
2932 * not the device-set.
2933 */
2934 /* imsm does not track uuid's so we synthesis one using sha1 on
2935 * - The signature (Which is constant for all imsm array, but no matter)
2936 * - the orig_family_num of the container
2937 * - the index number of the volume
2938 * - the 'serial' number of the volume.
2939 * Hopefully these are all constant.
2940 */
2941 struct intel_super *super = st->sb;
2942
2943 char buf[20];
2944 struct sha1_ctx ctx;
2945 struct imsm_dev *dev = NULL;
2946 __u32 family_num;
2947
2948 /* some mdadm versions failed to set ->orig_family_num, in which
2949 * case fall back to ->family_num. orig_family_num will be
2950 * fixed up with the first metadata update.
2951 */
2952 family_num = super->anchor->orig_family_num;
2953 if (family_num == 0)
2954 family_num = super->anchor->family_num;
2955 sha1_init_ctx(&ctx);
2956 sha1_process_bytes(super->anchor->sig, MPB_SIG_LEN, &ctx);
2957 sha1_process_bytes(&family_num, sizeof(__u32), &ctx);
2958 if (super->current_vol >= 0)
2959 dev = get_imsm_dev(super, super->current_vol);
2960 if (dev) {
2961 __u32 vol = super->current_vol;
2962 sha1_process_bytes(&vol, sizeof(vol), &ctx);
2963 sha1_process_bytes(dev->volume, MAX_RAID_SERIAL_LEN, &ctx);
2964 }
2965 sha1_finish_ctx(&ctx, buf);
2966 memcpy(uuid, buf, 4*4);
2967 }
2968
2969 static __u32 migr_strip_blocks_resync(struct imsm_dev *dev)
2970 {
2971 /* migr_strip_size when repairing or initializing parity */
2972 struct imsm_map *map = get_imsm_map(dev, MAP_0);
2973 __u32 chunk = __le32_to_cpu(map->blocks_per_strip);
2974
2975 switch (get_imsm_raid_level(map)) {
2976 case 5:
2977 case 10:
2978 return chunk;
2979 default:
2980 return 128*1024 >> 9;
2981 }
2982 }
2983
2984 static __u32 migr_strip_blocks_rebuild(struct imsm_dev *dev)
2985 {
2986 /* migr_strip_size when rebuilding a degraded disk, no idea why
2987 * this is different than migr_strip_size_resync(), but it's good
2988 * to be compatible
2989 */
2990 struct imsm_map *map = get_imsm_map(dev, MAP_1);
2991 __u32 chunk = __le32_to_cpu(map->blocks_per_strip);
2992
2993 switch (get_imsm_raid_level(map)) {
2994 case 1:
2995 case 10:
2996 if (map->num_members % map->num_domains == 0)
2997 return 128*1024 >> 9;
2998 else
2999 return chunk;
3000 case 5:
3001 return max((__u32) 64*1024 >> 9, chunk);
3002 default:
3003 return 128*1024 >> 9;
3004 }
3005 }
3006
3007 static __u32 num_stripes_per_unit_resync(struct imsm_dev *dev)
3008 {
3009 struct imsm_map *lo = get_imsm_map(dev, MAP_0);
3010 struct imsm_map *hi = get_imsm_map(dev, MAP_1);
3011 __u32 lo_chunk = __le32_to_cpu(lo->blocks_per_strip);
3012 __u32 hi_chunk = __le32_to_cpu(hi->blocks_per_strip);
3013
3014 return max((__u32) 1, hi_chunk / lo_chunk);
3015 }
3016
3017 static __u32 num_stripes_per_unit_rebuild(struct imsm_dev *dev)
3018 {
3019 struct imsm_map *lo = get_imsm_map(dev, MAP_0);
3020 int level = get_imsm_raid_level(lo);
3021
3022 if (level == 1 || level == 10) {
3023 struct imsm_map *hi = get_imsm_map(dev, MAP_1);
3024
3025 return hi->num_domains;
3026 } else
3027 return num_stripes_per_unit_resync(dev);
3028 }
3029
3030 static unsigned long long calc_component_size(struct imsm_map *map,
3031 struct imsm_dev *dev)
3032 {
3033 unsigned long long component_size;
3034 unsigned long long dev_size = imsm_dev_size(dev);
3035 long long calc_dev_size = 0;
3036 unsigned int member_disks = imsm_num_data_members(map);
3037
3038 if (member_disks == 0)
3039 return 0;
3040
3041 component_size = per_dev_array_size(map);
3042 calc_dev_size = component_size * member_disks;
3043
3044 /* Component size is rounded to 1MB so difference between size from
3045 * metadata and size calculated from num_data_stripes equals up to
3046 * 2048 blocks per each device. If the difference is higher it means
3047 * that array size was expanded and num_data_stripes was not updated.
3048 */
3049 if (llabs(calc_dev_size - (long long)dev_size) >
3050 (1 << SECT_PER_MB_SHIFT) * member_disks) {
3051 component_size = dev_size / member_disks;
3052 dprintf("Invalid num_data_stripes in metadata; expected=%llu, found=%llu\n",
3053 component_size / map->blocks_per_strip,
3054 num_data_stripes(map));
3055 }
3056
3057 return component_size;
3058 }
3059
3060 static __u32 parity_segment_depth(struct imsm_dev *dev)
3061 {
3062 struct imsm_map *map = get_imsm_map(dev, MAP_0);
3063 __u32 chunk = __le32_to_cpu(map->blocks_per_strip);
3064
3065 switch(get_imsm_raid_level(map)) {
3066 case 1:
3067 case 10:
3068 return chunk * map->num_domains;
3069 case 5:
3070 return chunk * map->num_members;
3071 default:
3072 return chunk;
3073 }
3074 }
3075
3076 static __u32 map_migr_block(struct imsm_dev *dev, __u32 block)
3077 {
3078 struct imsm_map *map = get_imsm_map(dev, MAP_1);
3079 __u32 chunk = __le32_to_cpu(map->blocks_per_strip);
3080 __u32 strip = block / chunk;
3081
3082 switch (get_imsm_raid_level(map)) {
3083 case 1:
3084 case 10: {
3085 __u32 vol_strip = (strip * map->num_domains) + 1;
3086 __u32 vol_stripe = vol_strip / map->num_members;
3087
3088 return vol_stripe * chunk + block % chunk;
3089 } case 5: {
3090 __u32 stripe = strip / (map->num_members - 1);
3091
3092 return stripe * chunk + block % chunk;
3093 }
3094 default:
3095 return 0;
3096 }
3097 }
3098
3099 static __u64 blocks_per_migr_unit(struct intel_super *super,
3100 struct imsm_dev *dev)
3101 {
3102 /* calculate the conversion factor between per member 'blocks'
3103 * (md/{resync,rebuild}_start) and imsm migration units, return
3104 * 0 for the 'not migrating' and 'unsupported migration' cases
3105 */
3106 if (!dev->vol.migr_state)
3107 return 0;
3108
3109 switch (migr_type(dev)) {
3110 case MIGR_GEN_MIGR: {
3111 struct migr_record *migr_rec = super->migr_rec;
3112 return __le32_to_cpu(migr_rec->blocks_per_unit);
3113 }
3114 case MIGR_VERIFY:
3115 case MIGR_REPAIR:
3116 case MIGR_INIT: {
3117 struct imsm_map *map = get_imsm_map(dev, MAP_0);
3118 __u32 stripes_per_unit;
3119 __u32 blocks_per_unit;
3120 __u32 parity_depth;
3121 __u32 migr_chunk;
3122 __u32 block_map;
3123 __u32 block_rel;
3124 __u32 segment;
3125 __u32 stripe;
3126 __u8 disks;
3127
3128 /* yes, this is really the translation of migr_units to
3129 * per-member blocks in the 'resync' case
3130 */
3131 stripes_per_unit = num_stripes_per_unit_resync(dev);
3132 migr_chunk = migr_strip_blocks_resync(dev);
3133 disks = imsm_num_data_members(map);
3134 blocks_per_unit = stripes_per_unit * migr_chunk * disks;
3135 stripe = __le16_to_cpu(map->blocks_per_strip) * disks;
3136 segment = blocks_per_unit / stripe;
3137 block_rel = blocks_per_unit - segment * stripe;
3138 parity_depth = parity_segment_depth(dev);
3139 block_map = map_migr_block(dev, block_rel);
3140 return block_map + parity_depth * segment;
3141 }
3142 case MIGR_REBUILD: {
3143 __u32 stripes_per_unit;
3144 __u32 migr_chunk;
3145
3146 stripes_per_unit = num_stripes_per_unit_rebuild(dev);
3147 migr_chunk = migr_strip_blocks_rebuild(dev);
3148 return migr_chunk * stripes_per_unit;
3149 }
3150 case MIGR_STATE_CHANGE:
3151 default:
3152 return 0;
3153 }
3154 }
3155
3156 static int imsm_level_to_layout(int level)
3157 {
3158 switch (level) {
3159 case 0:
3160 case 1:
3161 return 0;
3162 case 5:
3163 case 6:
3164 return ALGORITHM_LEFT_ASYMMETRIC;
3165 case 10:
3166 return 0x102;
3167 }
3168 return UnSet;
3169 }
3170
3171 /*******************************************************************************
3172 * Function: read_imsm_migr_rec
3173 * Description: Function reads imsm migration record from last sector of disk
3174 * Parameters:
3175 * fd : disk descriptor
3176 * super : metadata info
3177 * Returns:
3178 * 0 : success,
3179 * -1 : fail
3180 ******************************************************************************/
3181 static int read_imsm_migr_rec(int fd, struct intel_super *super)
3182 {
3183 int ret_val = -1;
3184 unsigned int sector_size = super->sector_size;
3185 unsigned long long dsize;
3186
3187 get_dev_size(fd, NULL, &dsize);
3188 if (lseek64(fd, dsize - (sector_size*MIGR_REC_SECTOR_POSITION),
3189 SEEK_SET) < 0) {
3190 pr_err("Cannot seek to anchor block: %s\n",
3191 strerror(errno));
3192 goto out;
3193 }
3194 if ((unsigned int)read(fd, super->migr_rec_buf,
3195 MIGR_REC_BUF_SECTORS*sector_size) !=
3196 MIGR_REC_BUF_SECTORS*sector_size) {
3197 pr_err("Cannot read migr record block: %s\n",
3198 strerror(errno));
3199 goto out;
3200 }
3201 ret_val = 0;
3202 if (sector_size == 4096)
3203 convert_from_4k_imsm_migr_rec(super);
3204
3205 out:
3206 return ret_val;
3207 }
3208
3209 static struct imsm_dev *imsm_get_device_during_migration(
3210 struct intel_super *super)
3211 {
3212
3213 struct intel_dev *dv;
3214
3215 for (dv = super->devlist; dv; dv = dv->next) {
3216 if (is_gen_migration(dv->dev))
3217 return dv->dev;
3218 }
3219 return NULL;
3220 }
3221
3222 /*******************************************************************************
3223 * Function: load_imsm_migr_rec
3224 * Description: Function reads imsm migration record (it is stored at the last
3225 * sector of disk)
3226 * Parameters:
3227 * super : imsm internal array info
3228 * Returns:
3229 * 0 : success
3230 * -1 : fail
3231 * -2 : no migration in progress
3232 ******************************************************************************/
3233 static int load_imsm_migr_rec(struct intel_super *super)
3234 {
3235 struct dl *dl;
3236 char nm[30];
3237 int retval = -1;
3238 int fd = -1;
3239 struct imsm_dev *dev;
3240 struct imsm_map *map;
3241 int slot = -1;
3242 int keep_fd = 1;
3243
3244 /* find map under migration */
3245 dev = imsm_get_device_during_migration(super);
3246 /* nothing to load,no migration in progress?
3247 */
3248 if (dev == NULL)
3249 return -2;
3250
3251 map = get_imsm_map(dev, MAP_0);
3252 if (!map)
3253 return -1;
3254
3255 for (dl = super->disks; dl; dl = dl->next) {
3256 /* skip spare and failed disks
3257 */
3258 if (dl->index < 0)
3259 continue;
3260 /* read only from one of the first two slots
3261 */
3262 slot = get_imsm_disk_slot(map, dl->index);
3263 if (slot > 1 || slot < 0)
3264 continue;
3265
3266 if (!is_fd_valid(dl->fd)) {
3267 sprintf(nm, "%d:%d", dl->major, dl->minor);
3268 fd = dev_open(nm, O_RDONLY);
3269
3270 if (is_fd_valid(fd)) {
3271 keep_fd = 0;
3272 break;
3273 }
3274 } else {
3275 fd = dl->fd;
3276 break;
3277 }
3278 }
3279
3280 if (!is_fd_valid(fd))
3281 return retval;
3282 retval = read_imsm_migr_rec(fd, super);
3283 if (!keep_fd)
3284 close(fd);
3285
3286 return retval;
3287 }
3288
3289 /*******************************************************************************
3290 * function: imsm_create_metadata_checkpoint_update
3291 * Description: It creates update for checkpoint change.
3292 * Parameters:
3293 * super : imsm internal array info
3294 * u : pointer to prepared update
3295 * Returns:
3296 * Uptate length.
3297 * If length is equal to 0, input pointer u contains no update
3298 ******************************************************************************/
3299 static int imsm_create_metadata_checkpoint_update(
3300 struct intel_super *super,
3301 struct imsm_update_general_migration_checkpoint **u)
3302 {
3303
3304 int update_memory_size = 0;
3305
3306 dprintf("(enter)\n");
3307
3308 if (u == NULL)
3309 return 0;
3310 *u = NULL;
3311
3312 /* size of all update data without anchor */
3313 update_memory_size =
3314 sizeof(struct imsm_update_general_migration_checkpoint);
3315
3316 *u = xcalloc(1, update_memory_size);
3317 if (*u == NULL) {
3318 dprintf("error: cannot get memory\n");
3319 return 0;
3320 }
3321 (*u)->type = update_general_migration_checkpoint;
3322 (*u)->curr_migr_unit = current_migr_unit(super->migr_rec);
3323 dprintf("prepared for %llu\n", (unsigned long long)(*u)->curr_migr_unit);
3324
3325 return update_memory_size;
3326 }
3327
3328 static void imsm_update_metadata_locally(struct supertype *st,
3329 void *buf, int len);
3330
3331 /*******************************************************************************
3332 * Function: write_imsm_migr_rec
3333 * Description: Function writes imsm migration record
3334 * (at the last sector of disk)
3335 * Parameters:
3336 * super : imsm internal array info
3337 * Returns:
3338 * 0 : success
3339 * -1 : if fail
3340 ******************************************************************************/
3341 static int write_imsm_migr_rec(struct supertype *st)
3342 {
3343 struct intel_super *super = st->sb;
3344 unsigned int sector_size = super->sector_size;
3345 unsigned long long dsize;
3346 int retval = -1;
3347 struct dl *sd;
3348 int len;
3349 struct imsm_update_general_migration_checkpoint *u;
3350 struct imsm_dev *dev;
3351 struct imsm_map *map;
3352
3353 /* find map under migration */
3354 dev = imsm_get_device_during_migration(super);
3355 /* if no migration, write buffer anyway to clear migr_record
3356 * on disk based on first available device
3357 */
3358 if (dev == NULL)
3359 dev = get_imsm_dev(super, super->current_vol < 0 ? 0 :
3360 super->current_vol);
3361
3362 map = get_imsm_map(dev, MAP_0);
3363
3364 if (sector_size == 4096)
3365 convert_to_4k_imsm_migr_rec(super);
3366 for (sd = super->disks ; sd ; sd = sd->next) {
3367 int slot = -1;
3368
3369 /* skip failed and spare devices */
3370 if (sd->index < 0)
3371 continue;
3372 /* write to 2 first slots only */
3373 if (map)
3374 slot = get_imsm_disk_slot(map, sd->index);
3375 if (map == NULL || slot > 1 || slot < 0)
3376 continue;
3377
3378 get_dev_size(sd->fd, NULL, &dsize);
3379 if (lseek64(sd->fd, dsize - (MIGR_REC_SECTOR_POSITION *
3380 sector_size),
3381 SEEK_SET) < 0) {
3382 pr_err("Cannot seek to anchor block: %s\n",
3383 strerror(errno));
3384 goto out;
3385 }
3386 if ((unsigned int)write(sd->fd, super->migr_rec_buf,
3387 MIGR_REC_BUF_SECTORS*sector_size) !=
3388 MIGR_REC_BUF_SECTORS*sector_size) {
3389 pr_err("Cannot write migr record block: %s\n",
3390 strerror(errno));
3391 goto out;
3392 }
3393 }
3394 if (sector_size == 4096)
3395 convert_from_4k_imsm_migr_rec(super);
3396 /* update checkpoint information in metadata */
3397 len = imsm_create_metadata_checkpoint_update(super, &u);
3398 if (len <= 0) {
3399 dprintf("imsm: Cannot prepare update\n");
3400 goto out;
3401 }
3402 /* update metadata locally */
3403 imsm_update_metadata_locally(st, u, len);
3404 /* and possibly remotely */
3405 if (st->update_tail) {
3406 append_metadata_update(st, u, len);
3407 /* during reshape we do all work inside metadata handler
3408 * manage_reshape(), so metadata update has to be triggered
3409 * insida it
3410 */
3411 flush_metadata_updates(st);
3412 st->update_tail = &st->updates;
3413 } else
3414 free(u);
3415
3416 retval = 0;
3417 out:
3418 return retval;
3419 }
3420
3421 /* spare/missing disks activations are not allowe when
3422 * array/container performs reshape operation, because
3423 * all arrays in container works on the same disks set
3424 */
3425 int imsm_reshape_blocks_arrays_changes(struct intel_super *super)
3426 {
3427 int rv = 0;
3428 struct intel_dev *i_dev;
3429 struct imsm_dev *dev;
3430
3431 /* check whole container
3432 */
3433 for (i_dev = super->devlist; i_dev; i_dev = i_dev->next) {
3434 dev = i_dev->dev;
3435 if (is_gen_migration(dev)) {
3436 /* No repair during any migration in container
3437 */
3438 rv = 1;
3439 break;
3440 }
3441 }
3442 return rv;
3443 }
3444 static unsigned long long imsm_component_size_alignment_check(int level,
3445 int chunk_size,
3446 unsigned int sector_size,
3447 unsigned long long component_size)
3448 {
3449 unsigned int component_size_alignment;
3450
3451 /* check component size alignment
3452 */
3453 component_size_alignment = component_size % (chunk_size/sector_size);
3454
3455 dprintf("(Level: %i, chunk_size = %i, component_size = %llu), component_size_alignment = %u\n",
3456 level, chunk_size, component_size,
3457 component_size_alignment);
3458
3459 if (component_size_alignment && (level != 1) && (level != UnSet)) {
3460 dprintf("imsm: reported component size aligned from %llu ",
3461 component_size);
3462 component_size -= component_size_alignment;
3463 dprintf_cont("to %llu (%i).\n",
3464 component_size, component_size_alignment);
3465 }
3466
3467 return component_size;
3468 }
3469
3470 /*******************************************************************************
3471 * Function: get_bitmap_header_sector
3472 * Description: Returns the sector where the bitmap header is placed.
3473 * Parameters:
3474 * st : supertype information
3475 * dev_idx : index of the device with bitmap
3476 *
3477 * Returns:
3478 * The sector where the bitmap header is placed
3479 ******************************************************************************/
3480 static unsigned long long get_bitmap_header_sector(struct intel_super *super,
3481 int dev_idx)
3482 {
3483 struct imsm_dev *dev = get_imsm_dev(super, dev_idx);
3484 struct imsm_map *map = get_imsm_map(dev, MAP_0);
3485
3486 if (!super->sector_size) {
3487 dprintf("sector size is not set\n");
3488 return 0;
3489 }
3490
3491 return pba_of_lba0(map) + calc_component_size(map, dev) +
3492 (IMSM_BITMAP_HEADER_OFFSET / super->sector_size);
3493 }
3494
3495 /*******************************************************************************
3496 * Function: get_bitmap_sector
3497 * Description: Returns the sector where the bitmap is placed.
3498 * Parameters:
3499 * st : supertype information
3500 * dev_idx : index of the device with bitmap
3501 *
3502 * Returns:
3503 * The sector where the bitmap is placed
3504 ******************************************************************************/
3505 static unsigned long long get_bitmap_sector(struct intel_super *super,
3506 int dev_idx)
3507 {
3508 if (!super->sector_size) {
3509 dprintf("sector size is not set\n");
3510 return 0;
3511 }
3512
3513 return get_bitmap_header_sector(super, dev_idx) +
3514 (IMSM_BITMAP_HEADER_SIZE / super->sector_size);
3515 }
3516
3517 static unsigned long long get_ppl_sector(struct intel_super *super, int dev_idx)
3518 {
3519 struct imsm_dev *dev = get_imsm_dev(super, dev_idx);
3520 struct imsm_map *map = get_imsm_map(dev, MAP_0);
3521
3522 return pba_of_lba0(map) +
3523 (num_data_stripes(map) * map->blocks_per_strip);
3524 }
3525
3526 static void getinfo_super_imsm_volume(struct supertype *st, struct mdinfo *info, char *dmap)
3527 {
3528 struct intel_super *super = st->sb;
3529 struct migr_record *migr_rec = super->migr_rec;
3530 struct imsm_dev *dev = get_imsm_dev(super, super->current_vol);
3531 struct imsm_map *map = get_imsm_map(dev, MAP_0);
3532 struct imsm_map *prev_map = get_imsm_map(dev, MAP_1);
3533 struct imsm_map *map_to_analyse = map;
3534 struct dl *dl;
3535 int map_disks = info->array.raid_disks;
3536
3537 memset(info, 0, sizeof(*info));
3538 if (prev_map)
3539 map_to_analyse = prev_map;
3540
3541 dl = super->current_disk;
3542
3543 info->container_member = super->current_vol;
3544 info->array.raid_disks = map->num_members;
3545 info->array.level = get_imsm_raid_level(map_to_analyse);
3546 info->array.layout = imsm_level_to_layout(info->array.level);
3547 info->array.md_minor = -1;
3548 info->array.ctime = 0;
3549 info->array.utime = 0;
3550 info->array.chunk_size =
3551 __le16_to_cpu(map_to_analyse->blocks_per_strip) << 9;
3552 info->array.state = !(dev->vol.dirty & RAIDVOL_DIRTY);
3553 info->custom_array_size = imsm_dev_size(dev);
3554 info->recovery_blocked = imsm_reshape_blocks_arrays_changes(st->sb);
3555
3556 if (is_gen_migration(dev)) {
3557 /*
3558 * device prev_map should be added if it is in the middle
3559 * of migration
3560 */
3561 assert(prev_map);
3562
3563 info->reshape_active = 1;
3564 info->new_level = get_imsm_raid_level(map);
3565 info->new_layout = imsm_level_to_layout(info->new_level);
3566 info->new_chunk = __le16_to_cpu(map->blocks_per_strip) << 9;
3567 info->delta_disks = map->num_members - prev_map->num_members;
3568 if (info->delta_disks) {
3569 /* this needs to be applied to every array
3570 * in the container.
3571 */
3572 info->reshape_active = CONTAINER_RESHAPE;
3573 }
3574 /* We shape information that we give to md might have to be
3575 * modify to cope with md's requirement for reshaping arrays.
3576 * For example, when reshaping a RAID0, md requires it to be
3577 * presented as a degraded RAID4.
3578 * Also if a RAID0 is migrating to a RAID5 we need to specify
3579 * the array as already being RAID5, but the 'before' layout
3580 * is a RAID4-like layout.
3581 */
3582 switch (info->array.level) {
3583 case 0:
3584 switch(info->new_level) {
3585 case 0:
3586 /* conversion is happening as RAID4 */
3587 info->array.level = 4;
3588 info->array.raid_disks += 1;
3589 break;
3590 case 5:
3591 /* conversion is happening as RAID5 */
3592 info->array.level = 5;
3593 info->array.layout = ALGORITHM_PARITY_N;
3594 info->delta_disks -= 1;
3595 break;
3596 default:
3597 /* FIXME error message */
3598 info->array.level = UnSet;
3599 break;
3600 }
3601 break;
3602 }
3603 } else {
3604 info->new_level = UnSet;
3605 info->new_layout = UnSet;
3606 info->new_chunk = info->array.chunk_size;
3607 info->delta_disks = 0;
3608 }
3609
3610 if (dl) {
3611 info->disk.major = dl->major;
3612 info->disk.minor = dl->minor;
3613 info->disk.number = dl->index;
3614 info->disk.raid_disk = get_imsm_disk_slot(map_to_analyse,
3615 dl->index);
3616 }
3617
3618 info->data_offset = pba_of_lba0(map_to_analyse);
3619 info->component_size = calc_component_size(map, dev);
3620 info->component_size = imsm_component_size_alignment_check(
3621 info->array.level,
3622 info->array.chunk_size,
3623 super->sector_size,
3624 info->component_size);
3625 info->bb.supported = 1;
3626
3627 memset(info->uuid, 0, sizeof(info->uuid));
3628 info->recovery_start = MaxSector;
3629
3630 if (info->array.level == 5 &&
3631 (dev->rwh_policy == RWH_DISTRIBUTED ||
3632 dev->rwh_policy == RWH_MULTIPLE_DISTRIBUTED)) {
3633 info->consistency_policy = CONSISTENCY_POLICY_PPL;
3634 info->ppl_sector = get_ppl_sector(super, super->current_vol);
3635 if (dev->rwh_policy == RWH_MULTIPLE_DISTRIBUTED)
3636 info->ppl_size = MULTIPLE_PPL_AREA_SIZE_IMSM >> 9;
3637 else
3638 info->ppl_size = (PPL_HEADER_SIZE + PPL_ENTRY_SPACE)
3639 >> 9;
3640 } else if (info->array.level <= 0) {
3641 info->consistency_policy = CONSISTENCY_POLICY_NONE;
3642 } else {
3643 if (dev->rwh_policy == RWH_BITMAP) {
3644 info->bitmap_offset = get_bitmap_sector(super, super->current_vol);
3645 info->consistency_policy = CONSISTENCY_POLICY_BITMAP;
3646 } else {
3647 info->consistency_policy = CONSISTENCY_POLICY_RESYNC;
3648 }
3649 }
3650
3651 info->reshape_progress = 0;
3652 info->resync_start = MaxSector;
3653 if ((map_to_analyse->map_state == IMSM_T_STATE_UNINITIALIZED ||
3654 !(info->array.state & 1)) &&
3655 imsm_reshape_blocks_arrays_changes(super) == 0) {
3656 info->resync_start = 0;
3657 }
3658 if (dev->vol.migr_state) {
3659 switch (migr_type(dev)) {
3660 case MIGR_REPAIR:
3661 case MIGR_INIT: {
3662 __u64 blocks_per_unit = blocks_per_migr_unit(super,
3663 dev);
3664 __u64 units = vol_curr_migr_unit(dev);
3665
3666 info->resync_start = blocks_per_unit * units;
3667 break;
3668 }
3669 case MIGR_GEN_MIGR: {
3670 __u64 blocks_per_unit = blocks_per_migr_unit(super,
3671 dev);
3672 __u64 units = current_migr_unit(migr_rec);
3673 int used_disks;
3674
3675 if (__le32_to_cpu(migr_rec->ascending_migr) &&
3676 (units <
3677 (get_num_migr_units(migr_rec)-1)) &&
3678 (super->migr_rec->rec_status ==
3679 __cpu_to_le32(UNIT_SRC_IN_CP_AREA)))
3680 units++;
3681
3682 info->reshape_progress = blocks_per_unit * units;
3683
3684 dprintf("IMSM: General Migration checkpoint : %llu (%llu) -> read reshape progress : %llu\n",
3685 (unsigned long long)units,
3686 (unsigned long long)blocks_per_unit,
3687 info->reshape_progress);
3688
3689 used_disks = imsm_num_data_members(prev_map);
3690 if (used_disks > 0) {
3691 info->custom_array_size = per_dev_array_size(map) *
3692 used_disks;
3693 }
3694 }
3695 case MIGR_VERIFY:
3696 /* we could emulate the checkpointing of
3697 * 'sync_action=check' migrations, but for now
3698 * we just immediately complete them
3699 */
3700 case MIGR_REBUILD:
3701 /* this is handled by container_content_imsm() */
3702 case MIGR_STATE_CHANGE:
3703 /* FIXME handle other migrations */
3704 default:
3705 /* we are not dirty, so... */
3706 info->resync_start = MaxSector;
3707 }
3708 }
3709
3710 strncpy(info->name, (char *) dev->volume, MAX_RAID_SERIAL_LEN);
3711 info->name[MAX_RAID_SERIAL_LEN] = 0;
3712
3713 info->array.major_version = -1;
3714 info->array.minor_version = -2;
3715 sprintf(info->text_version, "/%s/%d", st->container_devnm, info->container_member);
3716 info->safe_mode_delay = 4000; /* 4 secs like the Matrix driver */
3717 uuid_from_super_imsm(st, info->uuid);
3718
3719 if (dmap) {
3720 int i, j;
3721 for (i=0; i<map_disks; i++) {
3722 dmap[i] = 0;
3723 if (i < info->array.raid_disks) {
3724 struct imsm_disk *dsk;
3725 j = get_imsm_disk_idx(dev, i, MAP_X);
3726 dsk = get_imsm_disk(super, j);
3727 if (dsk && (dsk->status & CONFIGURED_DISK))
3728 dmap[i] = 1;
3729 }
3730 }
3731 }
3732 }
3733
3734 static __u8 imsm_check_degraded(struct intel_super *super, struct imsm_dev *dev,
3735 int failed, int look_in_map);
3736
3737 static int imsm_count_failed(struct intel_super *super, struct imsm_dev *dev,
3738 int look_in_map);
3739
3740 static void manage_second_map(struct intel_super *super, struct imsm_dev *dev)
3741 {
3742 if (is_gen_migration(dev)) {
3743 int failed;
3744 __u8 map_state;
3745 struct imsm_map *map2 = get_imsm_map(dev, MAP_1);
3746
3747 failed = imsm_count_failed(super, dev, MAP_1);
3748 map_state = imsm_check_degraded(super, dev, failed, MAP_1);
3749 if (map2->map_state != map_state) {
3750 map2->map_state = map_state;
3751 super->updates_pending++;
3752 }
3753 }
3754 }
3755
3756 static struct imsm_disk *get_imsm_missing(struct intel_super *super, __u8 index)
3757 {
3758 struct dl *d;
3759
3760 for (d = super->missing; d; d = d->next)
3761 if (d->index == index)
3762 return &d->disk;
3763 return NULL;
3764 }
3765
3766 static void getinfo_super_imsm(struct supertype *st, struct mdinfo *info, char *map)
3767 {
3768 struct intel_super *super = st->sb;
3769 struct imsm_disk *disk;
3770 int map_disks = info->array.raid_disks;
3771 int max_enough = -1;
3772 int i;
3773 struct imsm_super *mpb;
3774
3775 if (super->current_vol >= 0) {
3776 getinfo_super_imsm_volume(st, info, map);
3777 return;
3778 }
3779 memset(info, 0, sizeof(*info));
3780
3781 /* Set raid_disks to zero so that Assemble will always pull in valid
3782 * spares
3783 */
3784 info->array.raid_disks = 0;
3785 info->array.level = LEVEL_CONTAINER;
3786 info->array.layout = 0;
3787 info->array.md_minor = -1;
3788 info->array.ctime = 0; /* N/A for imsm */
3789 info->array.utime = 0;
3790 info->array.chunk_size = 0;
3791
3792 info->disk.major = 0;
3793 info->disk.minor = 0;
3794 info->disk.raid_disk = -1;
3795 info->reshape_active = 0;
3796 info->array.major_version = -1;
3797 info->array.minor_version = -2;
3798 strcpy(info->text_version, "imsm");
3799 info->safe_mode_delay = 0;
3800 info->disk.number = -1;
3801 info->disk.state = 0;
3802 info->name[0] = 0;
3803 info->recovery_start = MaxSector;
3804 info->recovery_blocked = imsm_reshape_blocks_arrays_changes(st->sb);
3805 info->bb.supported = 1;
3806
3807 /* do we have the all the insync disks that we expect? */
3808 mpb = super->anchor;
3809 info->events = __le32_to_cpu(mpb->generation_num);
3810
3811 for (i = 0; i < mpb->num_raid_devs; i++) {
3812 struct imsm_dev *dev = get_imsm_dev(super, i);
3813 int failed, enough, j, missing = 0;
3814 struct imsm_map *map;
3815 __u8 state;
3816
3817 failed = imsm_count_failed(super, dev, MAP_0);
3818 state = imsm_check_degraded(super, dev, failed, MAP_0);
3819 map = get_imsm_map(dev, MAP_0);
3820
3821 /* any newly missing disks?
3822 * (catches single-degraded vs double-degraded)
3823 */
3824 for (j = 0; j < map->num_members; j++) {
3825 __u32 ord = get_imsm_ord_tbl_ent(dev, j, MAP_0);
3826 __u32 idx = ord_to_idx(ord);
3827
3828 if (super->disks && super->disks->index == (int)idx)
3829 info->disk.raid_disk = j;
3830
3831 if (!(ord & IMSM_ORD_REBUILD) &&
3832 get_imsm_missing(super, idx)) {
3833 missing = 1;
3834 break;
3835 }
3836 }
3837
3838 if (state == IMSM_T_STATE_FAILED)
3839 enough = -1;
3840 else if (state == IMSM_T_STATE_DEGRADED &&
3841 (state != map->map_state || missing))
3842 enough = 0;
3843 else /* we're normal, or already degraded */
3844 enough = 1;
3845 if (is_gen_migration(dev) && missing) {
3846 /* during general migration we need all disks
3847 * that process is running on.
3848 * No new missing disk is allowed.
3849 */
3850 max_enough = -1;
3851 enough = -1;
3852 /* no more checks necessary
3853 */
3854 break;
3855 }
3856 /* in the missing/failed disk case check to see
3857 * if at least one array is runnable
3858 */
3859 max_enough = max(max_enough, enough);
3860 }
3861
3862 info->container_enough = max_enough;
3863
3864 if (super->disks) {
3865 __u32 reserved = imsm_reserved_sectors(super, super->disks);
3866
3867 disk = &super->disks->disk;
3868 info->data_offset = total_blocks(&super->disks->disk) - reserved;
3869 info->component_size = reserved;
3870 info->disk.state = is_configured(disk) ? (1 << MD_DISK_ACTIVE) : 0;
3871 /* we don't change info->disk.raid_disk here because
3872 * this state will be finalized in mdmon after we have
3873 * found the 'most fresh' version of the metadata
3874 */
3875 info->disk.state |= is_failed(disk) ? (1 << MD_DISK_FAULTY) : 0;
3876 info->disk.state |= (is_spare(disk) || is_journal(disk)) ?
3877 0 : (1 << MD_DISK_SYNC);
3878 }
3879
3880 /* only call uuid_from_super_imsm when this disk is part of a populated container,
3881 * ->compare_super may have updated the 'num_raid_devs' field for spares
3882 */
3883 if (info->disk.state & (1 << MD_DISK_SYNC) || super->anchor->num_raid_devs)
3884 uuid_from_super_imsm(st, info->uuid);
3885 else
3886 memcpy(info->uuid, uuid_zero, sizeof(uuid_zero));
3887
3888 /* I don't know how to compute 'map' on imsm, so use safe default */
3889 if (map) {
3890 int i;
3891 for (i = 0; i < map_disks; i++)
3892 map[i] = 1;
3893 }
3894
3895 }
3896
3897 /* allocates memory and fills disk in mdinfo structure
3898 * for each disk in array */
3899 struct mdinfo *getinfo_super_disks_imsm(struct supertype *st)
3900 {
3901 struct mdinfo *mddev;
3902 struct intel_super *super = st->sb;
3903 struct imsm_disk *disk;
3904 int count = 0;
3905 struct dl *dl;
3906 if (!super || !super->disks)
3907 return NULL;
3908 dl = super->disks;
3909 mddev = xcalloc(1, sizeof(*mddev));
3910 while (dl) {
3911 struct mdinfo *tmp;
3912 disk = &dl->disk;
3913 tmp = xcalloc(1, sizeof(*tmp));
3914 if (mddev->devs)
3915 tmp->next = mddev->devs;
3916 mddev->devs = tmp;
3917 tmp->disk.number = count++;
3918 tmp->disk.major = dl->major;
3919 tmp->disk.minor = dl->minor;
3920 tmp->disk.state = is_configured(disk) ?
3921 (1 << MD_DISK_ACTIVE) : 0;
3922 tmp->disk.state |= is_failed(disk) ? (1 << MD_DISK_FAULTY) : 0;
3923 tmp->disk.state |= is_spare(disk) ? 0 : (1 << MD_DISK_SYNC);
3924 tmp->disk.raid_disk = -1;
3925 dl = dl->next;
3926 }
3927 return mddev;
3928 }
3929
3930 static int update_super_imsm(struct supertype *st, struct mdinfo *info,
3931 enum update_opt update, char *devname,
3932 int verbose, int uuid_set, char *homehost)
3933 {
3934 /* For 'assemble' and 'force' we need to return non-zero if any
3935 * change was made. For others, the return value is ignored.
3936 * Update options are:
3937 * force-one : This device looks a bit old but needs to be included,
3938 * update age info appropriately.
3939 * assemble: clear any 'faulty' flag to allow this device to
3940 * be assembled.
3941 * force-array: Array is degraded but being forced, mark it clean
3942 * if that will be needed to assemble it.
3943 *
3944 * newdev: not used ????
3945 * grow: Array has gained a new device - this is currently for
3946 * linear only
3947 * resync: mark as dirty so a resync will happen.
3948 * name: update the name - preserving the homehost
3949 * uuid: Change the uuid of the array to match watch is given
3950 *
3951 * Following are not relevant for this imsm:
3952 * sparc2.2 : update from old dodgey metadata
3953 * super-minor: change the preferred_minor number
3954 * summaries: update redundant counters.
3955 * homehost: update the recorded homehost
3956 * _reshape_progress: record new reshape_progress position.
3957 */
3958 int rv = 1;
3959 struct intel_super *super = st->sb;
3960 struct imsm_super *mpb;
3961
3962 /* we can only update container info */
3963 if (!super || super->current_vol >= 0 || !super->anchor)
3964 return 1;
3965
3966 mpb = super->anchor;
3967
3968 switch (update) {
3969 case UOPT_UUID:
3970 /* We take this to mean that the family_num should be updated.
3971 * However that is much smaller than the uuid so we cannot really
3972 * allow an explicit uuid to be given. And it is hard to reliably
3973 * know if one was.
3974 * So if !uuid_set we know the current uuid is random and just used
3975 * the first 'int' and copy it to the other 3 positions.
3976 * Otherwise we require the 4 'int's to be the same as would be the
3977 * case if we are using a random uuid. So an explicit uuid will be
3978 * accepted as long as all for ints are the same... which shouldn't hurt
3979 */
3980 if (!uuid_set) {
3981 info->uuid[1] = info->uuid[2] = info->uuid[3] = info->uuid[0];
3982 rv = 0;
3983 } else {
3984 if (info->uuid[0] != info->uuid[1] ||
3985 info->uuid[1] != info->uuid[2] ||
3986 info->uuid[2] != info->uuid[3])
3987 rv = -1;
3988 else
3989 rv = 0;
3990 }
3991 if (rv == 0)
3992 mpb->orig_family_num = info->uuid[0];
3993 break;
3994 case UOPT_SPEC_ASSEMBLE:
3995 rv = 0;
3996 break;
3997 default:
3998 rv = -1;
3999 break;
4000 }
4001
4002 /* successful update? recompute checksum */
4003 if (rv == 0)
4004 mpb->check_sum = __le32_to_cpu(__gen_imsm_checksum(mpb));
4005
4006 return rv;
4007 }
4008
4009 static size_t disks_to_mpb_size(int disks)
4010 {
4011 size_t size;
4012
4013 size = sizeof(struct imsm_super);
4014 size += (disks - 1) * sizeof(struct imsm_disk);
4015 size += 2 * sizeof(struct imsm_dev);
4016 /* up to 2 maps per raid device (-2 for imsm_maps in imsm_dev */
4017 size += (4 - 2) * sizeof(struct imsm_map);
4018 /* 4 possible disk_ord_tbl's */
4019 size += 4 * (disks - 1) * sizeof(__u32);
4020 /* maximum bbm log */
4021 size += sizeof(struct bbm_log);
4022
4023 return size;
4024 }
4025
4026 static __u64 avail_size_imsm(struct supertype *st, __u64 devsize,
4027 unsigned long long data_offset)
4028 {
4029 if (devsize < (MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS))
4030 return 0;
4031
4032 return devsize - (MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS);
4033 }
4034
4035 static void free_devlist(struct intel_super *super)
4036 {
4037 struct intel_dev *dv;
4038
4039 while (super->devlist) {
4040 dv = super->devlist->next;
4041 free(super->devlist->dev);
4042 free(super->devlist);
4043 super->devlist = dv;
4044 }
4045 }
4046
4047 static void imsm_copy_dev(struct imsm_dev *dest, struct imsm_dev *src)
4048 {
4049 memcpy(dest, src, sizeof_imsm_dev(src, 0));
4050 }
4051
4052 static int compare_super_imsm(struct supertype *st, struct supertype *tst,
4053 int verbose)
4054 {
4055 /* return:
4056 * 0 same, or first was empty, and second was copied
4057 * 1 sb are different
4058 */
4059 struct intel_super *first = st->sb;
4060 struct intel_super *sec = tst->sb;
4061
4062 if (!first) {
4063 st->sb = tst->sb;
4064 tst->sb = NULL;
4065 return 0;
4066 }
4067
4068 /* in platform dependent environment test if the disks
4069 * use the same Intel hba
4070 * if not on Intel hba at all, allow anything.
4071 * doesn't check HBAs if num_raid_devs is not set, as it means
4072 * it is a free floating spare, and all spares regardless of HBA type
4073 * will fall into separate container during the assembly
4074 */
4075 if (first->hba && sec->hba && first->anchor->num_raid_devs != 0) {
4076 if (first->hba->type != sec->hba->type) {
4077 if (verbose)
4078 pr_err("HBAs of devices do not match %s != %s\n",
4079 get_sys_dev_type(first->hba->type),
4080 get_sys_dev_type(sec->hba->type));
4081 return 1;
4082 }
4083 if (first->orom != sec->orom) {
4084 if (verbose)
4085 pr_err("HBAs of devices do not match %s != %s\n",
4086 first->hba->pci_id, sec->hba->pci_id);
4087 return 1;
4088 }
4089 }
4090
4091 if (first->anchor->num_raid_devs > 0 &&
4092 sec->anchor->num_raid_devs > 0) {
4093 /* Determine if these disks might ever have been
4094 * related. Further disambiguation can only take place
4095 * in load_super_imsm_all
4096 */
4097 __u32 first_family = first->anchor->orig_family_num;
4098 __u32 sec_family = sec->anchor->orig_family_num;
4099
4100 if (memcmp(first->anchor->sig, sec->anchor->sig,
4101 MAX_SIGNATURE_LENGTH) != 0)
4102 return 1;
4103
4104 if (first_family == 0)
4105 first_family = first->anchor->family_num;
4106 if (sec_family == 0)
4107 sec_family = sec->anchor->family_num;
4108
4109 if (first_family != sec_family)
4110 return 1;
4111
4112 }
4113
4114 /* if an anchor does not have num_raid_devs set then it is a free
4115 * floating spare. don't assosiate spare with any array, as during assembly
4116 * spares shall fall into separate container, from which they can be moved
4117 * when necessary
4118 */
4119 if (first->anchor->num_raid_devs ^ sec->anchor->num_raid_devs)
4120 return 1;
4121
4122 return 0;
4123 }
4124
4125 static void fd2devname(int fd, char *name)
4126 {
4127 char *nm;
4128
4129 nm = fd2kname(fd);
4130 if (!nm)
4131 return;
4132
4133 snprintf(name, MAX_RAID_SERIAL_LEN, "/dev/%s", nm);
4134 }
4135
4136 static int nvme_get_serial(int fd, void *buf, size_t buf_len)
4137 {
4138 char path[PATH_MAX];
4139 char *name = fd2kname(fd);
4140
4141 if (!name)
4142 return 1;
4143
4144 if (strncmp(name, "nvme", 4) != 0)
4145 return 1;
4146
4147 if (!diskfd_to_devpath(fd, 1, path))
4148 return 1;
4149
4150 return devpath_to_char(path, "serial", buf, buf_len, 0);
4151 }
4152
4153 extern int scsi_get_serial(int fd, void *buf, size_t buf_len);
4154
4155 static int imsm_read_serial(int fd, char *devname,
4156 __u8 *serial, size_t serial_buf_len)
4157 {
4158 char buf[50];
4159 int rv;
4160 size_t len;
4161 char *dest;
4162 char *src;
4163 unsigned int i;
4164
4165 memset(buf, 0, sizeof(buf));
4166
4167 if (check_env("IMSM_DEVNAME_AS_SERIAL")) {
4168 memset(serial, 0, serial_buf_len);
4169 fd2devname(fd, (char *) serial);
4170 return 0;
4171 }
4172
4173 rv = nvme_get_serial(fd, buf, sizeof(buf));
4174
4175 if (rv)
4176 rv = scsi_get_serial(fd, buf, sizeof(buf));
4177
4178 if (rv != 0) {
4179 if (devname)
4180 pr_err("Failed to retrieve serial for %s\n",
4181 devname);
4182 return rv;
4183 }
4184
4185 /* trim all whitespace and non-printable characters and convert
4186 * ':' to ';'
4187 */
4188 for (i = 0, dest = buf; i < sizeof(buf) && buf[i]; i++) {
4189 src = &buf[i];
4190 if (*src > 0x20) {
4191 /* ':' is reserved for use in placeholder serial
4192 * numbers for missing disks
4193 */
4194 if (*src == ':')
4195 *dest++ = ';';
4196 else
4197 *dest++ = *src;
4198 }
4199 }
4200 len = dest - buf;
4201 dest = buf;
4202
4203 if (len > serial_buf_len) {
4204 /* truncate leading characters */
4205 dest += len - serial_buf_len;
4206 len = serial_buf_len;
4207 }
4208
4209 memset(serial, 0, serial_buf_len);
4210 memcpy(serial, dest, len);
4211
4212 return 0;
4213 }
4214
4215 static int serialcmp(__u8 *s1, __u8 *s2)
4216 {
4217 return strncmp((char *) s1, (char *) s2, MAX_RAID_SERIAL_LEN);
4218 }
4219
4220 static void serialcpy(__u8 *dest, __u8 *src)
4221 {
4222 strncpy((char *) dest, (char *) src, MAX_RAID_SERIAL_LEN);
4223 }
4224
4225 static struct dl *serial_to_dl(__u8 *serial, struct intel_super *super)
4226 {
4227 struct dl *dl;
4228
4229 for (dl = super->disks; dl; dl = dl->next)
4230 if (serialcmp(dl->serial, serial) == 0)
4231 break;
4232
4233 return dl;
4234 }
4235
4236 static struct imsm_disk *
4237 __serial_to_disk(__u8 *serial, struct imsm_super *mpb, int *idx)
4238 {
4239 int i;
4240
4241 for (i = 0; i < mpb->num_disks; i++) {
4242 struct imsm_disk *disk = __get_imsm_disk(mpb, i);
4243
4244 if (serialcmp(disk->serial, serial) == 0) {
4245 if (idx)
4246 *idx = i;
4247 return disk;
4248 }
4249 }
4250
4251 return NULL;
4252 }
4253
4254 static int
4255 load_imsm_disk(int fd, struct intel_super *super, char *devname, int keep_fd)
4256 {
4257 struct imsm_disk *disk;
4258 struct dl *dl;
4259 struct stat stb;
4260 int rv;
4261 char name[40];
4262 __u8 serial[MAX_RAID_SERIAL_LEN];
4263
4264 rv = imsm_read_serial(fd, devname, serial, MAX_RAID_SERIAL_LEN);
4265
4266 if (rv != 0)
4267 return 2;
4268
4269 dl = xcalloc(1, sizeof(*dl));
4270
4271 fstat(fd, &stb);
4272 dl->major = major(stb.st_rdev);
4273 dl->minor = minor(stb.st_rdev);
4274 dl->next = super->disks;
4275 dl->fd = keep_fd ? fd : -1;
4276 assert(super->disks == NULL);
4277 super->disks = dl;
4278 serialcpy(dl->serial, serial);
4279 dl->index = -2;
4280 dl->e = NULL;
4281 fd2devname(fd, name);
4282 if (devname)
4283 dl->devname = xstrdup(devname);
4284 else
4285 dl->devname = xstrdup(name);
4286
4287 /* look up this disk's index in the current anchor */
4288 disk = __serial_to_disk(dl->serial, super->anchor, &dl->index);
4289 if (disk) {
4290 dl->disk = *disk;
4291 /* only set index on disks that are a member of a
4292 * populated contianer, i.e. one with raid_devs
4293 */
4294 if (is_failed(&dl->disk))
4295 dl->index = -2;
4296 else if (is_spare(&dl->disk) || is_journal(&dl->disk))
4297 dl->index = -1;
4298 }
4299
4300 return 0;
4301 }
4302
4303 /* When migrating map0 contains the 'destination' state while map1
4304 * contains the current state. When not migrating map0 contains the
4305 * current state. This routine assumes that map[0].map_state is set to
4306 * the current array state before being called.
4307 *
4308 * Migration is indicated by one of the following states
4309 * 1/ Idle (migr_state=0 map0state=normal||unitialized||degraded||failed)
4310 * 2/ Initialize (migr_state=1 migr_type=MIGR_INIT map0state=normal
4311 * map1state=unitialized)
4312 * 3/ Repair (Resync) (migr_state=1 migr_type=MIGR_REPAIR map0state=normal
4313 * map1state=normal)
4314 * 4/ Rebuild (migr_state=1 migr_type=MIGR_REBUILD map0state=normal
4315 * map1state=degraded)
4316 * 5/ Migration (mig_state=1 migr_type=MIGR_GEN_MIGR map0state=normal
4317 * map1state=normal)
4318 */
4319 static void migrate(struct imsm_dev *dev, struct intel_super *super,
4320 __u8 to_state, int migr_type)
4321 {
4322 struct imsm_map *dest;
4323 struct imsm_map *src = get_imsm_map(dev, MAP_0);
4324
4325 dev->vol.migr_state = 1;
4326 set_migr_type(dev, migr_type);
4327 set_vol_curr_migr_unit(dev, 0);
4328 dest = get_imsm_map(dev, MAP_1);
4329
4330 /* duplicate and then set the target end state in map[0] */
4331 memcpy(dest, src, sizeof_imsm_map(src));
4332 if (migr_type == MIGR_GEN_MIGR) {
4333 __u32 ord;
4334 int i;
4335
4336 for (i = 0; i < src->num_members; i++) {
4337 ord = __le32_to_cpu(src->disk_ord_tbl[i]);
4338 set_imsm_ord_tbl_ent(src, i, ord_to_idx(ord));
4339 }
4340 }
4341
4342 if (migr_type == MIGR_GEN_MIGR)
4343 /* Clear migration record */
4344 memset(super->migr_rec, 0, sizeof(struct migr_record));
4345
4346 src->map_state = to_state;
4347 }
4348
4349 static void end_migration(struct imsm_dev *dev, struct intel_super *super,
4350 __u8 map_state)
4351 {
4352 struct imsm_map *map = get_imsm_map(dev, MAP_0);
4353 struct imsm_map *prev = get_imsm_map(dev, dev->vol.migr_state == 0 ?
4354 MAP_0 : MAP_1);
4355 int i, j;
4356
4357 /* merge any IMSM_ORD_REBUILD bits that were not successfully
4358 * completed in the last migration.
4359 *
4360 * FIXME add support for raid-level-migration
4361 */
4362 if (map_state != map->map_state && (is_gen_migration(dev) == false) &&
4363 prev->map_state != IMSM_T_STATE_UNINITIALIZED) {
4364 /* when final map state is other than expected
4365 * merge maps (not for migration)
4366 */
4367 int failed;
4368
4369 for (i = 0; i < prev->num_members; i++)
4370 for (j = 0; j < map->num_members; j++)
4371 /* during online capacity expansion
4372 * disks position can be changed
4373 * if takeover is used
4374 */
4375 if (ord_to_idx(map->disk_ord_tbl[j]) ==
4376 ord_to_idx(prev->disk_ord_tbl[i])) {
4377 map->disk_ord_tbl[j] |=
4378 prev->disk_ord_tbl[i];
4379 break;
4380 }
4381 failed = imsm_count_failed(super, dev, MAP_0);
4382 map_state = imsm_check_degraded(super, dev, failed, MAP_0);
4383 }
4384
4385 dev->vol.migr_state = 0;
4386 set_migr_type(dev, 0);
4387 set_vol_curr_migr_unit(dev, 0);
4388 map->map_state = map_state;
4389 }
4390
4391 static int parse_raid_devices(struct intel_super *super)
4392 {
4393 int i;
4394 struct imsm_dev *dev_new;
4395 size_t len, len_migr;
4396 size_t max_len = 0;
4397 size_t space_needed = 0;
4398 struct imsm_super *mpb = super->anchor;
4399
4400 for (i = 0; i < super->anchor->num_raid_devs; i++) {
4401 struct imsm_dev *dev_iter = __get_imsm_dev(super->anchor, i);
4402 struct intel_dev *dv;
4403
4404 len = sizeof_imsm_dev(dev_iter, 0);
4405 len_migr = sizeof_imsm_dev(dev_iter, 1);
4406 if (len_migr > len)
4407 space_needed += len_migr - len;
4408
4409 dv = xmalloc(sizeof(*dv));
4410 if (max_len < len_migr)
4411 max_len = len_migr;
4412 if (max_len > len_migr)
4413 space_needed += max_len - len_migr;
4414 dev_new = xmalloc(max_len);
4415 imsm_copy_dev(dev_new, dev_iter);
4416 dv->dev = dev_new;
4417 dv->index = i;
4418 dv->next = super->devlist;
4419 super->devlist = dv;
4420 }
4421
4422 /* ensure that super->buf is large enough when all raid devices
4423 * are migrating
4424 */
4425 if (__le32_to_cpu(mpb->mpb_size) + space_needed > super->len) {
4426 void *buf;
4427
4428 len = ROUND_UP(__le32_to_cpu(mpb->mpb_size) + space_needed,
4429 super->sector_size);
4430 if (posix_memalign(&buf, MAX_SECTOR_SIZE, len) != 0)
4431 return 1;
4432
4433 memcpy(buf, super->buf, super->len);
4434 memset(buf + super->len, 0, len - super->len);
4435 free(super->buf);
4436 super->buf = buf;
4437 super->len = len;
4438 }
4439
4440 super->extra_space += space_needed;
4441
4442 return 0;
4443 }
4444
4445 /*******************************************************************************
4446 * Function: check_mpb_migr_compatibility
4447 * Description: Function checks for unsupported migration features:
4448 * - migration optimization area (pba_of_lba0)
4449 * - descending reshape (ascending_migr)
4450 * Parameters:
4451 * super : imsm metadata information
4452 * Returns:
4453 * 0 : migration is compatible
4454 * -1 : migration is not compatible
4455 ******************************************************************************/
4456 int check_mpb_migr_compatibility(struct intel_super *super)
4457 {
4458 struct imsm_map *map0, *map1;
4459 struct migr_record *migr_rec = super->migr_rec;
4460 int i;
4461
4462 for (i = 0; i < super->anchor->num_raid_devs; i++) {
4463 struct imsm_dev *dev_iter = __get_imsm_dev(super->anchor, i);
4464
4465 if (dev_iter->vol.migr_state == 1 &&
4466 dev_iter->vol.migr_type == MIGR_GEN_MIGR) {
4467 /* This device is migrating */
4468 map0 = get_imsm_map(dev_iter, MAP_0);
4469 map1 = get_imsm_map(dev_iter, MAP_1);
4470 if (pba_of_lba0(map0) != pba_of_lba0(map1))
4471 /* migration optimization area was used */
4472 return -1;
4473 if (migr_rec->ascending_migr == 0 &&
4474 migr_rec->dest_depth_per_unit > 0)
4475 /* descending reshape not supported yet */
4476 return -1;
4477 }
4478 }
4479 return 0;
4480 }
4481
4482 static void __free_imsm(struct intel_super *super, int free_disks);
4483
4484 /* load_imsm_mpb - read matrix metadata
4485 * allocates super->mpb to be freed by free_imsm
4486 */
4487 static int load_imsm_mpb(int fd, struct intel_super *super, char *devname)
4488 {
4489 unsigned long long dsize;
4490 unsigned long long sectors;
4491 unsigned int sector_size = super->sector_size;
4492 struct stat;
4493 struct imsm_super *anchor;
4494 __u32 check_sum;
4495
4496 get_dev_size(fd, NULL, &dsize);
4497 if (dsize < 2*sector_size) {
4498 if (devname)
4499 pr_err("%s: device to small for imsm\n",
4500 devname);
4501 return 1;
4502 }
4503
4504 if (lseek64(fd, dsize - (sector_size * 2), SEEK_SET) < 0) {
4505 if (devname)
4506 pr_err("Cannot seek to anchor block on %s: %s\n",
4507 devname, strerror(errno));
4508 return 1;
4509 }
4510
4511 if (posix_memalign((void **)&anchor, sector_size, sector_size) != 0) {
4512 if (devname)
4513 pr_err("Failed to allocate imsm anchor buffer on %s\n", devname);
4514 return 1;
4515 }
4516 if ((unsigned int)read(fd, anchor, sector_size) != sector_size) {
4517 if (devname)
4518 pr_err("Cannot read anchor block on %s: %s\n",
4519 devname, strerror(errno));
4520 free(anchor);
4521 return 1;
4522 }
4523
4524 if (strncmp((char *) anchor->sig, MPB_SIGNATURE, MPB_SIG_LEN) != 0) {
4525 if (devname)
4526 pr_err("no IMSM anchor on %s\n", devname);
4527 free(anchor);
4528 return 2;
4529 }
4530
4531 __free_imsm(super, 0);
4532 /* reload capability and hba */
4533
4534 /* capability and hba must be updated with new super allocation */
4535 find_intel_hba_capability(fd, super, devname);
4536 super->len = ROUND_UP(anchor->mpb_size, sector_size);
4537 if (posix_memalign(&super->buf, MAX_SECTOR_SIZE, super->len) != 0) {
4538 if (devname)
4539 pr_err("unable to allocate %zu byte mpb buffer\n",
4540 super->len);
4541 free(anchor);
4542 return 2;
4543 }
4544 memcpy(super->buf, anchor, sector_size);
4545
4546 sectors = mpb_sectors(anchor, sector_size) - 1;
4547 free(anchor);
4548
4549 if (posix_memalign(&super->migr_rec_buf, MAX_SECTOR_SIZE,
4550 MIGR_REC_BUF_SECTORS*MAX_SECTOR_SIZE) != 0) {
4551 pr_err("could not allocate migr_rec buffer\n");
4552 free(super->buf);
4553 super->buf = NULL;
4554 return 2;
4555 }
4556 super->clean_migration_record_by_mdmon = 0;
4557
4558 if (!sectors) {
4559 check_sum = __gen_imsm_checksum(super->anchor);
4560 if (check_sum != __le32_to_cpu(super->anchor->check_sum)) {
4561 if (devname)
4562 pr_err("IMSM checksum %x != %x on %s\n",
4563 check_sum,
4564 __le32_to_cpu(super->anchor->check_sum),
4565 devname);
4566 return 2;
4567 }
4568
4569 return 0;
4570 }
4571
4572 /* read the extended mpb */
4573 if (lseek64(fd, dsize - (sector_size * (2 + sectors)), SEEK_SET) < 0) {
4574 if (devname)
4575 pr_err("Cannot seek to extended mpb on %s: %s\n",
4576 devname, strerror(errno));
4577 return 1;
4578 }
4579
4580 if ((unsigned int)read(fd, super->buf + sector_size,
4581 super->len - sector_size) != super->len - sector_size) {
4582 if (devname)
4583 pr_err("Cannot read extended mpb on %s: %s\n",
4584 devname, strerror(errno));
4585 return 2;
4586 }
4587
4588 check_sum = __gen_imsm_checksum(super->anchor);
4589 if (check_sum != __le32_to_cpu(super->anchor->check_sum)) {
4590 if (devname)
4591 pr_err("IMSM checksum %x != %x on %s\n",
4592 check_sum, __le32_to_cpu(super->anchor->check_sum),
4593 devname);
4594 return 3;
4595 }
4596
4597 return 0;
4598 }
4599
4600 static int read_imsm_migr_rec(int fd, struct intel_super *super);
4601
4602 /* clears hi bits in metadata if MPB_ATTRIB_2TB_DISK not set */
4603 static void clear_hi(struct intel_super *super)
4604 {
4605 struct imsm_super *mpb = super->anchor;
4606 int i, n;
4607 if (mpb->attributes & MPB_ATTRIB_2TB_DISK)
4608 return;
4609 for (i = 0; i < mpb->num_disks; ++i) {
4610 struct imsm_disk *disk = &mpb->disk[i];
4611 disk->total_blocks_hi = 0;
4612 }
4613 for (i = 0; i < mpb->num_raid_devs; ++i) {
4614 struct imsm_dev *dev = get_imsm_dev(super, i);
4615 for (n = 0; n < 2; ++n) {
4616 struct imsm_map *map = get_imsm_map(dev, n);
4617 if (!map)
4618 continue;
4619 map->pba_of_lba0_hi = 0;
4620 map->blocks_per_member_hi = 0;
4621 map->num_data_stripes_hi = 0;
4622 }
4623 }
4624 }
4625
4626 static int
4627 load_and_parse_mpb(int fd, struct intel_super *super, char *devname, int keep_fd)
4628 {
4629 int err;
4630
4631 err = load_imsm_mpb(fd, super, devname);
4632 if (err)
4633 return err;
4634 if (super->sector_size == 4096)
4635 convert_from_4k(super);
4636 err = load_imsm_disk(fd, super, devname, keep_fd);
4637 if (err)
4638 return err;
4639 err = parse_raid_devices(super);
4640 if (err)
4641 return err;
4642 err = load_bbm_log(super);
4643 clear_hi(super);
4644 return err;
4645 }
4646
4647 static void __free_imsm_disk(struct dl *d, int do_close)
4648 {
4649 if (do_close)
4650 close_fd(&d->fd);
4651 if (d->devname)
4652 free(d->devname);
4653 if (d->e)
4654 free(d->e);
4655 free(d);
4656
4657 }
4658
4659 static void free_imsm_disks(struct intel_super *super)
4660 {
4661 struct dl *d;
4662
4663 while (super->disks) {
4664 d = super->disks;
4665 super->disks = d->next;
4666 __free_imsm_disk(d, 1);
4667 }
4668 while (super->disk_mgmt_list) {
4669 d = super->disk_mgmt_list;
4670 super->disk_mgmt_list = d->next;
4671 __free_imsm_disk(d, 1);
4672 }
4673 while (super->missing) {
4674 d = super->missing;
4675 super->missing = d->next;
4676 __free_imsm_disk(d, 1);
4677 }
4678
4679 }
4680
4681 /* free all the pieces hanging off of a super pointer */
4682 static void __free_imsm(struct intel_super *super, int free_disks)
4683 {
4684 struct intel_hba *elem, *next;
4685
4686 if (super->buf) {
4687 free(super->buf);
4688 super->buf = NULL;
4689 }
4690 /* unlink capability description */
4691 super->orom = NULL;
4692 if (super->migr_rec_buf) {
4693 free(super->migr_rec_buf);
4694 super->migr_rec_buf = NULL;
4695 }
4696 if (free_disks)
4697 free_imsm_disks(super);
4698 free_devlist(super);
4699 elem = super->hba;
4700 while (elem) {
4701 if (elem->path)
4702 free((void *)elem->path);
4703 next = elem->next;
4704 free(elem);
4705 elem = next;
4706 }
4707 if (super->bbm_log)
4708 free(super->bbm_log);
4709 super->hba = NULL;
4710 }
4711
4712 static void free_imsm(struct intel_super *super)
4713 {
4714 __free_imsm(super, 1);
4715 free(super->bb.entries);
4716 free(super);
4717 }
4718
4719 static void free_super_imsm(struct supertype *st)
4720 {
4721 struct intel_super *super = st->sb;
4722
4723 if (!super)
4724 return;
4725
4726 free_imsm(super);
4727 st->sb = NULL;
4728 }
4729
4730 static struct intel_super *alloc_super(void)
4731 {
4732 struct intel_super *super = xcalloc(1, sizeof(*super));
4733
4734 super->current_vol = -1;
4735 super->create_offset = ~((unsigned long long) 0);
4736
4737 super->bb.entries = xmalloc(BBM_LOG_MAX_ENTRIES *
4738 sizeof(struct md_bb_entry));
4739 if (!super->bb.entries) {
4740 free(super);
4741 return NULL;
4742 }
4743
4744 return super;
4745 }
4746
4747 /*
4748 * find and allocate hba and OROM/EFI based on valid fd of RAID component device
4749 */
4750 static int find_intel_hba_capability(int fd, struct intel_super *super, char *devname)
4751 {
4752 struct sys_dev *hba_name;
4753 int rv = 0;
4754
4755 if (is_fd_valid(fd) && test_partition(fd)) {
4756 pr_err("imsm: %s is a partition, cannot be used in IMSM\n",
4757 devname);
4758 return 1;
4759 }
4760 if (!is_fd_valid(fd) || check_no_platform()) {
4761 super->orom = NULL;
4762 super->hba = NULL;
4763 return 0;
4764 }
4765 hba_name = find_disk_attached_hba(fd, NULL);
4766 if (!hba_name) {
4767 if (devname)
4768 pr_err("%s is not attached to Intel(R) RAID controller.\n",
4769 devname);
4770 return 1;
4771 }
4772 rv = attach_hba_to_super(super, hba_name);
4773 if (rv == 2) {
4774 if (devname) {
4775 struct intel_hba *hba = super->hba;
4776
4777 pr_err("%s is attached to Intel(R) %s %s (%s),\n"
4778 " but the container is assigned to Intel(R) %s %s (",
4779 devname,
4780 get_sys_dev_type(hba_name->type),
4781 hba_name->type == SYS_DEV_VMD || hba_name->type == SYS_DEV_SATA_VMD ?
4782 "domain" : "RAID controller",
4783 hba_name->pci_id ? : "Err!",
4784 get_sys_dev_type(super->hba->type),
4785 hba->type == SYS_DEV_VMD || hba_name->type == SYS_DEV_SATA_VMD ?
4786 "domain" : "RAID controller");
4787
4788 while (hba) {
4789 fprintf(stderr, "%s", hba->pci_id ? : "Err!");
4790 if (hba->next)
4791 fprintf(stderr, ", ");
4792 hba = hba->next;
4793 }
4794 fprintf(stderr, ").\n"
4795 " Mixing devices attached to different controllers is not allowed.\n");
4796 }
4797 return 2;
4798 }
4799 super->orom = find_imsm_capability(hba_name);
4800 if (!super->orom)
4801 return 3;
4802
4803 return 0;
4804 }
4805
4806 /* find_missing - helper routine for load_super_imsm_all that identifies
4807 * disks that have disappeared from the system. This routine relies on
4808 * the mpb being uptodate, which it is at load time.
4809 */
4810 static int find_missing(struct intel_super *super)
4811 {
4812 int i;
4813 struct imsm_super *mpb = super->anchor;
4814 struct dl *dl;
4815 struct imsm_disk *disk;
4816
4817 for (i = 0; i < mpb->num_disks; i++) {
4818 disk = __get_imsm_disk(mpb, i);
4819 dl = serial_to_dl(disk->serial, super);
4820 if (dl)
4821 continue;
4822
4823 dl = xmalloc(sizeof(*dl));
4824 dl->major = 0;
4825 dl->minor = 0;
4826 dl->fd = -1;
4827 dl->devname = xstrdup("missing");
4828 dl->index = i;
4829 serialcpy(dl->serial, disk->serial);
4830 dl->disk = *disk;
4831 dl->e = NULL;
4832 dl->next = super->missing;
4833 super->missing = dl;
4834 }
4835
4836 return 0;
4837 }
4838
4839 static struct intel_disk *disk_list_get(__u8 *serial, struct intel_disk *disk_list)
4840 {
4841 struct intel_disk *idisk = disk_list;
4842
4843 while (idisk) {
4844 if (serialcmp(idisk->disk.serial, serial) == 0)
4845 break;
4846 idisk = idisk->next;
4847 }
4848
4849 return idisk;
4850 }
4851
4852 static int __prep_thunderdome(struct intel_super **table, int tbl_size,
4853 struct intel_super *super,
4854 struct intel_disk **disk_list)
4855 {
4856 struct imsm_disk *d = &super->disks->disk;
4857 struct imsm_super *mpb = super->anchor;
4858 int i, j;
4859
4860 for (i = 0; i < tbl_size; i++) {
4861 struct imsm_super *tbl_mpb = table[i]->anchor;
4862 struct imsm_disk *tbl_d = &table[i]->disks->disk;
4863
4864 if (tbl_mpb->family_num == mpb->family_num) {
4865 if (tbl_mpb->check_sum == mpb->check_sum) {
4866 dprintf("mpb from %d:%d matches %d:%d\n",
4867 super->disks->major,
4868 super->disks->minor,
4869 table[i]->disks->major,
4870 table[i]->disks->minor);
4871 break;
4872 }
4873
4874 if (((is_configured(d) && !is_configured(tbl_d)) ||
4875 is_configured(d) == is_configured(tbl_d)) &&
4876 tbl_mpb->generation_num < mpb->generation_num) {
4877 /* current version of the mpb is a
4878 * better candidate than the one in
4879 * super_table, but copy over "cross
4880 * generational" status
4881 */
4882 struct intel_disk *idisk;
4883
4884 dprintf("mpb from %d:%d replaces %d:%d\n",
4885 super->disks->major,
4886 super->disks->minor,
4887 table[i]->disks->major,
4888 table[i]->disks->minor);
4889
4890 idisk = disk_list_get(tbl_d->serial, *disk_list);
4891 if (idisk && is_failed(&idisk->disk))
4892 tbl_d->status |= FAILED_DISK;
4893 break;
4894 } else {
4895 struct intel_disk *idisk;
4896 struct imsm_disk *disk;
4897
4898 /* tbl_mpb is more up to date, but copy
4899 * over cross generational status before
4900 * returning
4901 */
4902 disk = __serial_to_disk(d->serial, mpb, NULL);
4903 if (disk && is_failed(disk))
4904 d->status |= FAILED_DISK;
4905
4906 idisk = disk_list_get(d->serial, *disk_list);
4907 if (idisk) {
4908 idisk->owner = i;
4909 if (disk && is_configured(disk))
4910 idisk->disk.status |= CONFIGURED_DISK;
4911 }
4912
4913 dprintf("mpb from %d:%d prefer %d:%d\n",
4914 super->disks->major,
4915 super->disks->minor,
4916 table[i]->disks->major,
4917 table[i]->disks->minor);
4918
4919 return tbl_size;
4920 }
4921 }
4922 }
4923
4924 if (i >= tbl_size)
4925 table[tbl_size++] = super;
4926 else
4927 table[i] = super;
4928
4929 /* update/extend the merged list of imsm_disk records */
4930 for (j = 0; j < mpb->num_disks; j++) {
4931 struct imsm_disk *disk = __get_imsm_disk(mpb, j);
4932 struct intel_disk *idisk;
4933
4934 idisk = disk_list_get(disk->serial, *disk_list);
4935 if (idisk) {
4936 idisk->disk.status |= disk->status;
4937 if (is_configured(&idisk->disk) ||
4938 is_failed(&idisk->disk))
4939 idisk->disk.status &= ~(SPARE_DISK);
4940 } else {
4941 idisk = xcalloc(1, sizeof(*idisk));
4942 idisk->owner = IMSM_UNKNOWN_OWNER;
4943 idisk->disk = *disk;
4944 idisk->next = *disk_list;
4945 *disk_list = idisk;
4946 }
4947
4948 if (serialcmp(idisk->disk.serial, d->serial) == 0)
4949 idisk->owner = i;
4950 }
4951
4952 return tbl_size;
4953 }
4954
4955 static struct intel_super *
4956 validate_members(struct intel_super *super, struct intel_disk *disk_list,
4957 const int owner)
4958 {
4959 struct imsm_super *mpb = super->anchor;
4960 int ok_count = 0;
4961 int i;
4962
4963 for (i = 0; i < mpb->num_disks; i++) {
4964 struct imsm_disk *disk = __get_imsm_disk(mpb, i);
4965 struct intel_disk *idisk;
4966
4967 idisk = disk_list_get(disk->serial, disk_list);
4968 if (idisk) {
4969 if (idisk->owner == owner ||
4970 idisk->owner == IMSM_UNKNOWN_OWNER)
4971 ok_count++;
4972 else
4973 dprintf("'%.16s' owner %d != %d\n",
4974 disk->serial, idisk->owner,
4975 owner);
4976 } else {
4977 dprintf("unknown disk %x [%d]: %.16s\n",
4978 __le32_to_cpu(mpb->family_num), i,
4979 disk->serial);
4980 break;
4981 }
4982 }
4983
4984 if (ok_count == mpb->num_disks)
4985 return super;
4986 return NULL;
4987 }
4988
4989 static void show_conflicts(__u32 family_num, struct intel_super *super_list)
4990 {
4991 struct intel_super *s;
4992
4993 for (s = super_list; s; s = s->next) {
4994 if (family_num != s->anchor->family_num)
4995 continue;
4996 pr_err("Conflict, offlining family %#x on '%s'\n",
4997 __le32_to_cpu(family_num), s->disks->devname);
4998 }
4999 }
5000
5001 static struct intel_super *
5002 imsm_thunderdome(struct intel_super **super_list, int len)
5003 {
5004 struct intel_super *super_table[len];
5005 struct intel_disk *disk_list = NULL;
5006 struct intel_super *champion, *spare;
5007 struct intel_super *s, **del;
5008 int tbl_size = 0;
5009 int conflict;
5010 int i;
5011
5012 memset(super_table, 0, sizeof(super_table));
5013 for (s = *super_list; s; s = s->next)
5014 tbl_size = __prep_thunderdome(super_table, tbl_size, s, &disk_list);
5015
5016 for (i = 0; i < tbl_size; i++) {
5017 struct imsm_disk *d;
5018 struct intel_disk *idisk;
5019 struct imsm_super *mpb = super_table[i]->anchor;
5020
5021 s = super_table[i];
5022 d = &s->disks->disk;
5023
5024 /* 'd' must appear in merged disk list for its
5025 * configuration to be valid
5026 */
5027 idisk = disk_list_get(d->serial, disk_list);
5028 if (idisk && idisk->owner == i)
5029 s = validate_members(s, disk_list, i);
5030 else
5031 s = NULL;
5032
5033 if (!s)
5034 dprintf("marking family: %#x from %d:%d offline\n",
5035 mpb->family_num,
5036 super_table[i]->disks->major,
5037 super_table[i]->disks->minor);
5038 super_table[i] = s;
5039 }
5040
5041 /* This is where the mdadm implementation differs from the Windows
5042 * driver which has no strict concept of a container. We can only
5043 * assemble one family from a container, so when returning a prodigal
5044 * array member to this system the code will not be able to disambiguate
5045 * the container contents that should be assembled ("foreign" versus
5046 * "local"). It requires user intervention to set the orig_family_num
5047 * to a new value to establish a new container. The Windows driver in
5048 * this situation fixes up the volume name in place and manages the
5049 * foreign array as an independent entity.
5050 */
5051 s = NULL;
5052 spare = NULL;
5053 conflict = 0;
5054 for (i = 0; i < tbl_size; i++) {
5055 struct intel_super *tbl_ent = super_table[i];
5056 int is_spare = 0;
5057
5058 if (!tbl_ent)
5059 continue;
5060
5061 if (tbl_ent->anchor->num_raid_devs == 0) {
5062 spare = tbl_ent;
5063 is_spare = 1;
5064 }
5065
5066 if (s && !is_spare) {
5067 show_conflicts(tbl_ent->anchor->family_num, *super_list);
5068 conflict++;
5069 } else if (!s && !is_spare)
5070 s = tbl_ent;
5071 }
5072
5073 if (!s)
5074 s = spare;
5075 if (!s) {
5076 champion = NULL;
5077 goto out;
5078 }
5079 champion = s;
5080
5081 if (conflict)
5082 pr_err("Chose family %#x on '%s', assemble conflicts to new container with '--update=uuid'\n",
5083 __le32_to_cpu(s->anchor->family_num), s->disks->devname);
5084
5085 /* collect all dl's onto 'champion', and update them to
5086 * champion's version of the status
5087 */
5088 for (s = *super_list; s; s = s->next) {
5089 struct imsm_super *mpb = champion->anchor;
5090 struct dl *dl = s->disks;
5091
5092 if (s == champion)
5093 continue;
5094
5095 mpb->attributes |= s->anchor->attributes & MPB_ATTRIB_2TB_DISK;
5096
5097 for (i = 0; i < mpb->num_disks; i++) {
5098 struct imsm_disk *disk;
5099
5100 disk = __serial_to_disk(dl->serial, mpb, &dl->index);
5101 if (disk) {
5102 dl->disk = *disk;
5103 /* only set index on disks that are a member of
5104 * a populated contianer, i.e. one with
5105 * raid_devs
5106 */
5107 if (is_failed(&dl->disk))
5108 dl->index = -2;
5109 else if (is_spare(&dl->disk))
5110 dl->index = -1;
5111 break;
5112 }
5113 }
5114
5115 if (i >= mpb->num_disks) {
5116 struct intel_disk *idisk;
5117
5118 idisk = disk_list_get(dl->serial, disk_list);
5119 if (idisk && is_spare(&idisk->disk) &&
5120 !is_failed(&idisk->disk) && !is_configured(&idisk->disk))
5121 dl->index = -1;
5122 else {
5123 dl->index = -2;
5124 continue;
5125 }
5126 }
5127
5128 dl->next = champion->disks;
5129 champion->disks = dl;
5130 s->disks = NULL;
5131 }
5132
5133 /* delete 'champion' from super_list */
5134 for (del = super_list; *del; ) {
5135 if (*del == champion) {
5136 *del = (*del)->next;
5137 break;
5138 } else
5139 del = &(*del)->next;
5140 }
5141 champion->next = NULL;
5142
5143 out:
5144 while (disk_list) {
5145 struct intel_disk *idisk = disk_list;
5146
5147 disk_list = disk_list->next;
5148 free(idisk);
5149 }
5150
5151 return champion;
5152 }
5153
5154 static int
5155 get_sra_super_block(int fd, struct intel_super **super_list, char *devname, int *max, int keep_fd);
5156 static int get_super_block(struct intel_super **super_list, char *devnm, char *devname,
5157 int major, int minor, int keep_fd);
5158 static int
5159 get_devlist_super_block(struct md_list *devlist, struct intel_super **super_list,
5160 int *max, int keep_fd);
5161
5162 static int load_super_imsm_all(struct supertype *st, int fd, void **sbp,
5163 char *devname, struct md_list *devlist,
5164 int keep_fd)
5165 {
5166 struct intel_super *super_list = NULL;
5167 struct intel_super *super = NULL;
5168 int err = 0;
5169 int i = 0;
5170
5171 if (is_fd_valid(fd))
5172 /* 'fd' is an opened container */
5173 err = get_sra_super_block(fd, &super_list, devname, &i, keep_fd);
5174 else
5175 /* get super block from devlist devices */
5176 err = get_devlist_super_block(devlist, &super_list, &i, keep_fd);
5177 if (err)
5178 goto error;
5179 /* all mpbs enter, maybe one leaves */
5180 super = imsm_thunderdome(&super_list, i);
5181 if (!super) {
5182 err = 1;
5183 goto error;
5184 }
5185
5186 if (find_missing(super) != 0) {
5187 free_imsm(super);
5188 err = 2;
5189 goto error;
5190 }
5191
5192 /* load migration record */
5193 err = load_imsm_migr_rec(super);
5194 if (err == -1) {
5195 /* migration is in progress,
5196 * but migr_rec cannot be loaded,
5197 */
5198 err = 4;
5199 goto error;
5200 }
5201
5202 /* Check migration compatibility */
5203 if (err == 0 && check_mpb_migr_compatibility(super) != 0) {
5204 pr_err("Unsupported migration detected");
5205 if (devname)
5206 fprintf(stderr, " on %s\n", devname);
5207 else
5208 fprintf(stderr, " (IMSM).\n");
5209
5210 err = 5;
5211 goto error;
5212 }
5213
5214 err = 0;
5215
5216 error:
5217 while (super_list) {
5218 struct intel_super *s = super_list;
5219
5220 super_list = super_list->next;
5221 free_imsm(s);
5222 }
5223
5224 if (err)
5225 return err;
5226
5227 *sbp = super;
5228 if (is_fd_valid(fd))
5229 strcpy(st->container_devnm, fd2devnm(fd));
5230 else
5231 st->container_devnm[0] = 0;
5232 if (err == 0 && st->ss == NULL) {
5233 st->ss = &super_imsm;
5234 st->minor_version = 0;
5235 st->max_devs = IMSM_MAX_DEVICES;
5236 }
5237 return 0;
5238 }
5239
5240 static int
5241 get_devlist_super_block(struct md_list *devlist, struct intel_super **super_list,
5242 int *max, int keep_fd)
5243 {
5244 struct md_list *tmpdev;
5245 int err = 0;
5246 int i = 0;
5247
5248 for (i = 0, tmpdev = devlist; tmpdev; tmpdev = tmpdev->next) {
5249 if (tmpdev->used != 1)
5250 continue;
5251 if (tmpdev->container == 1) {
5252 int lmax = 0;
5253 int fd = dev_open(tmpdev->devname, O_RDONLY|O_EXCL);
5254 if (!is_fd_valid(fd)) {
5255 pr_err("cannot open device %s: %s\n",
5256 tmpdev->devname, strerror(errno));
5257 err = 8;
5258 goto error;
5259 }
5260 err = get_sra_super_block(fd, super_list,
5261 tmpdev->devname, &lmax,
5262 keep_fd);
5263 i += lmax;
5264 close(fd);
5265 if (err) {
5266 err = 7;
5267 goto error;
5268 }
5269 } else {
5270 int major = major(tmpdev->st_rdev);
5271 int minor = minor(tmpdev->st_rdev);
5272 err = get_super_block(super_list,
5273 NULL,
5274 tmpdev->devname,
5275 major, minor,
5276 keep_fd);
5277 i++;
5278 if (err) {
5279 err = 6;
5280 goto error;
5281 }
5282 }
5283 }
5284 error:
5285 *max = i;
5286 return err;
5287 }
5288
5289 static int get_super_block(struct intel_super **super_list, char *devnm, char *devname,
5290 int major, int minor, int keep_fd)
5291 {
5292 struct intel_super *s;
5293 char nm[32];
5294 int dfd = -1;
5295 int err = 0;
5296 int retry;
5297
5298 s = alloc_super();
5299 if (!s) {
5300 err = 1;
5301 goto error;
5302 }
5303
5304 sprintf(nm, "%d:%d", major, minor);
5305 dfd = dev_open(nm, O_RDWR);
5306 if (!is_fd_valid(dfd)) {
5307 err = 2;
5308 goto error;
5309 }
5310
5311 if (!get_dev_sector_size(dfd, NULL, &s->sector_size)) {
5312 err = 2;
5313 goto error;
5314 }
5315 find_intel_hba_capability(dfd, s, devname);
5316 err = load_and_parse_mpb(dfd, s, NULL, keep_fd);
5317
5318 /* retry the load if we might have raced against mdmon */
5319 if (err == 3 && devnm && mdmon_running(devnm))
5320 for (retry = 0; retry < 3; retry++) {
5321 sleep_for(0, MSEC_TO_NSEC(3), true);
5322 err = load_and_parse_mpb(dfd, s, NULL, keep_fd);
5323 if (err != 3)
5324 break;
5325 }
5326 error:
5327 if (!err) {
5328 s->next = *super_list;
5329 *super_list = s;
5330 } else {
5331 if (s)
5332 free_imsm(s);
5333 close_fd(&dfd);
5334 }
5335 if (!keep_fd)
5336 close_fd(&dfd);
5337 return err;
5338
5339 }
5340
5341 static int
5342 get_sra_super_block(int fd, struct intel_super **super_list, char *devname, int *max, int keep_fd)
5343 {
5344 struct mdinfo *sra;
5345 char *devnm;
5346 struct mdinfo *sd;
5347 int err = 0;
5348 int i = 0;
5349 sra = sysfs_read(fd, NULL, GET_LEVEL|GET_VERSION|GET_DEVS|GET_STATE);
5350 if (!sra)
5351 return 1;
5352
5353 if (sra->array.major_version != -1 ||
5354 sra->array.minor_version != -2 ||
5355 strcmp(sra->text_version, "imsm") != 0) {
5356 err = 1;
5357 goto error;
5358 }
5359 /* load all mpbs */
5360 devnm = fd2devnm(fd);
5361 for (sd = sra->devs, i = 0; sd; sd = sd->next, i++) {
5362 if (get_super_block(super_list, devnm, devname,
5363 sd->disk.major, sd->disk.minor, keep_fd) != 0) {
5364 err = 7;
5365 goto error;
5366 }
5367 }
5368 error:
5369 sysfs_free(sra);
5370 *max = i;
5371 return err;
5372 }
5373
5374 static int load_container_imsm(struct supertype *st, int fd, char *devname)
5375 {
5376 return load_super_imsm_all(st, fd, &st->sb, devname, NULL, 1);
5377 }
5378
5379 static int load_super_imsm(struct supertype *st, int fd, char *devname)
5380 {
5381 struct intel_super *super;
5382 int rv;
5383 int retry;
5384
5385 if (test_partition(fd))
5386 /* IMSM not allowed on partitions */
5387 return 1;
5388
5389 free_super_imsm(st);
5390
5391 super = alloc_super();
5392 if (!super)
5393 return 1;
5394
5395 if (!get_dev_sector_size(fd, NULL, &super->sector_size)) {
5396 free_imsm(super);
5397 return 1;
5398 }
5399 /* Load hba and capabilities if they exist.
5400 * But do not preclude loading metadata in case capabilities or hba are
5401 * non-compliant and ignore_hw_compat is set.
5402 */
5403 rv = find_intel_hba_capability(fd, super, devname);
5404 /* no orom/efi or non-intel hba of the disk */
5405 if (rv != 0 && st->ignore_hw_compat == 0) {
5406 if (devname)
5407 pr_err("No OROM/EFI properties for %s\n", devname);
5408 free_imsm(super);
5409 return 2;
5410 }
5411 rv = load_and_parse_mpb(fd, super, devname, 0);
5412
5413 /* retry the load if we might have raced against mdmon */
5414 if (rv == 3) {
5415 struct mdstat_ent *mdstat = NULL;
5416 char *name = fd2kname(fd);
5417
5418 if (name)
5419 mdstat = mdstat_by_component(name);
5420
5421 if (mdstat && mdmon_running(mdstat->devnm) && getpid() != mdmon_pid(mdstat->devnm)) {
5422 for (retry = 0; retry < 3; retry++) {
5423 sleep_for(0, MSEC_TO_NSEC(3), true);
5424 rv = load_and_parse_mpb(fd, super, devname, 0);
5425 if (rv != 3)
5426 break;
5427 }
5428 }
5429
5430 free_mdstat(mdstat);
5431 }
5432
5433 if (rv) {
5434 if (devname)
5435 pr_err("Failed to load all information sections on %s\n", devname);
5436 free_imsm(super);
5437 return rv;
5438 }
5439
5440 st->sb = super;
5441 if (st->ss == NULL) {
5442 st->ss = &super_imsm;
5443 st->minor_version = 0;
5444 st->max_devs = IMSM_MAX_DEVICES;
5445 }
5446
5447 /* load migration record */
5448 if (load_imsm_migr_rec(super) == 0) {
5449 /* Check for unsupported migration features */
5450 if (check_mpb_migr_compatibility(super) != 0) {
5451 pr_err("Unsupported migration detected");
5452 if (devname)
5453 fprintf(stderr, " on %s\n", devname);
5454 else
5455 fprintf(stderr, " (IMSM).\n");
5456 return 3;
5457 }
5458 }
5459
5460 return 0;
5461 }
5462
5463 static __u16 info_to_blocks_per_strip(mdu_array_info_t *info)
5464 {
5465 if (info->level == 1)
5466 return 128;
5467 return info->chunk_size >> 9;
5468 }
5469
5470 static unsigned long long info_to_blocks_per_member(mdu_array_info_t *info,
5471 unsigned long long size)
5472 {
5473 if (info->level == 1)
5474 return size * 2;
5475 else
5476 return (size * 2) & ~(info_to_blocks_per_strip(info) - 1);
5477 }
5478
5479 static void imsm_update_version_info(struct intel_super *super)
5480 {
5481 /* update the version and attributes */
5482 struct imsm_super *mpb = super->anchor;
5483 char *version;
5484 struct imsm_dev *dev;
5485 struct imsm_map *map;
5486 int i;
5487
5488 for (i = 0; i < mpb->num_raid_devs; i++) {
5489 dev = get_imsm_dev(super, i);
5490 map = get_imsm_map(dev, MAP_0);
5491 if (__le32_to_cpu(dev->size_high) > 0)
5492 mpb->attributes |= MPB_ATTRIB_2TB;
5493
5494 /* FIXME detect when an array spans a port multiplier */
5495 #if 0
5496 mpb->attributes |= MPB_ATTRIB_PM;
5497 #endif
5498
5499 if (mpb->num_raid_devs > 1 ||
5500 mpb->attributes != MPB_ATTRIB_CHECKSUM_VERIFY) {
5501 version = MPB_VERSION_ATTRIBS;
5502 switch (get_imsm_raid_level(map)) {
5503 case 0: mpb->attributes |= MPB_ATTRIB_RAID0; break;
5504 case 1: mpb->attributes |= MPB_ATTRIB_RAID1; break;
5505 case 10: mpb->attributes |= MPB_ATTRIB_RAID10; break;
5506 case 5: mpb->attributes |= MPB_ATTRIB_RAID5; break;
5507 }
5508 } else {
5509 if (map->num_members >= 5)
5510 version = MPB_VERSION_5OR6_DISK_ARRAY;
5511 else if (dev->status == DEV_CLONE_N_GO)
5512 version = MPB_VERSION_CNG;
5513 else if (get_imsm_raid_level(map) == 5)
5514 version = MPB_VERSION_RAID5;
5515 else if (map->num_members >= 3)
5516 version = MPB_VERSION_3OR4_DISK_ARRAY;
5517 else if (get_imsm_raid_level(map) == 1)
5518 version = MPB_VERSION_RAID1;
5519 else
5520 version = MPB_VERSION_RAID0;
5521 }
5522 strcpy(((char *) mpb->sig) + strlen(MPB_SIGNATURE), version);
5523 }
5524 }
5525
5526 /**
5527 * imsm_check_name() - check imsm naming criteria.
5528 * @super: &intel_super pointer, not NULL.
5529 * @name: name to check.
5530 * @verbose: verbose level.
5531 *
5532 * Name must be no longer than &MAX_RAID_SERIAL_LEN and must be unique across volumes.
5533 *
5534 * Returns: &true if @name matches, &false otherwise.
5535 */
5536 static bool imsm_is_name_allowed(struct intel_super *super, const char * const name,
5537 const int verbose)
5538 {
5539 struct imsm_super *mpb = super->anchor;
5540 int i;
5541
5542 if (is_string_lq(name, MAX_RAID_SERIAL_LEN + 1) == false) {
5543 pr_vrb("imsm: Name \"%s\" is too long\n", name);
5544 return false;
5545 }
5546
5547 for (i = 0; i < mpb->num_raid_devs; i++) {
5548 struct imsm_dev *dev = get_imsm_dev(super, i);
5549
5550 if (strncmp((char *) dev->volume, name, MAX_RAID_SERIAL_LEN) == 0) {
5551 pr_vrb("imsm: Name \"%s\" already exists\n", name);
5552 return false;
5553 }
5554 }
5555
5556 return true;
5557 }
5558
5559 static int init_super_imsm_volume(struct supertype *st, mdu_array_info_t *info,
5560 struct shape *s, char *name,
5561 char *homehost, int *uuid,
5562 long long data_offset)
5563 {
5564 /* We are creating a volume inside a pre-existing container.
5565 * so st->sb is already set.
5566 */
5567 struct intel_super *super = st->sb;
5568 unsigned int sector_size = super->sector_size;
5569 struct imsm_super *mpb = super->anchor;
5570 struct intel_dev *dv;
5571 struct imsm_dev *dev;
5572 struct imsm_vol *vol;
5573 struct imsm_map *map;
5574 int idx = mpb->num_raid_devs;
5575 int i;
5576 int namelen;
5577 unsigned long long array_blocks;
5578 size_t size_old, size_new;
5579 unsigned int data_disks;
5580 unsigned long long size_per_member;
5581
5582 if (super->orom && mpb->num_raid_devs >= super->orom->vpa) {
5583 pr_err("This imsm-container already has the maximum of %d volumes\n", super->orom->vpa);
5584 return 0;
5585 }
5586
5587 /* ensure the mpb is large enough for the new data */
5588 size_old = __le32_to_cpu(mpb->mpb_size);
5589 size_new = disks_to_mpb_size(info->nr_disks);
5590 if (size_new > size_old) {
5591 void *mpb_new;
5592 size_t size_round = ROUND_UP(size_new, sector_size);
5593
5594 if (posix_memalign(&mpb_new, sector_size, size_round) != 0) {
5595 pr_err("could not allocate new mpb\n");
5596 return 0;
5597 }
5598 if (posix_memalign(&super->migr_rec_buf, MAX_SECTOR_SIZE,
5599 MIGR_REC_BUF_SECTORS*
5600 MAX_SECTOR_SIZE) != 0) {
5601 pr_err("could not allocate migr_rec buffer\n");
5602 free(super->buf);
5603 free(super);
5604 free(mpb_new);
5605 return 0;
5606 }
5607 memcpy(mpb_new, mpb, size_old);
5608 free(mpb);
5609 mpb = mpb_new;
5610 super->anchor = mpb_new;
5611 mpb->mpb_size = __cpu_to_le32(size_new);
5612 memset(mpb_new + size_old, 0, size_round - size_old);
5613 super->len = size_round;
5614 }
5615 super->current_vol = idx;
5616
5617 /* handle 'failed_disks' by either:
5618 * a) create dummy disk entries in the table if this the first
5619 * volume in the array. We add them here as this is the only
5620 * opportunity to add them. add_to_super_imsm_volume()
5621 * handles the non-failed disks and continues incrementing
5622 * mpb->num_disks.
5623 * b) validate that 'failed_disks' matches the current number
5624 * of missing disks if the container is populated
5625 */
5626 if (super->current_vol == 0) {
5627 mpb->num_disks = 0;
5628 for (i = 0; i < info->failed_disks; i++) {
5629 struct imsm_disk *disk;
5630
5631 mpb->num_disks++;
5632 disk = __get_imsm_disk(mpb, i);
5633 disk->status = CONFIGURED_DISK | FAILED_DISK;
5634 disk->scsi_id = __cpu_to_le32(~(__u32)0);
5635 snprintf((char *) disk->serial, MAX_RAID_SERIAL_LEN,
5636 "missing:%d", (__u8)i);
5637 }
5638 find_missing(super);
5639 } else {
5640 int missing = 0;
5641 struct dl *d;
5642
5643 for (d = super->missing; d; d = d->next)
5644 missing++;
5645 if (info->failed_disks > missing) {
5646 pr_err("unable to add 'missing' disk to container\n");
5647 return 0;
5648 }
5649 }
5650
5651 if (imsm_is_name_allowed(super, name, 1) == false)
5652 return 0;
5653
5654 dv = xmalloc(sizeof(*dv));
5655 dev = xcalloc(1, sizeof(*dev) + sizeof(__u32) * (info->raid_disks - 1));
5656 /*
5657 * Explicitly allow truncating to not confuse gcc's
5658 * -Werror=stringop-truncation
5659 */
5660 namelen = min((int) strlen(name), MAX_RAID_SERIAL_LEN);
5661 memcpy(dev->volume, name, namelen);
5662 array_blocks = calc_array_size(info->level, info->raid_disks,
5663 info->layout, info->chunk_size,
5664 s->size * BLOCKS_PER_KB);
5665 data_disks = get_data_disks(info->level, info->layout,
5666 info->raid_disks);
5667 array_blocks = round_size_to_mb(array_blocks, data_disks);
5668 size_per_member = array_blocks / data_disks;
5669
5670 set_imsm_dev_size(dev, array_blocks);
5671 dev->status = (DEV_READ_COALESCING | DEV_WRITE_COALESCING);
5672 vol = &dev->vol;
5673 vol->migr_state = 0;
5674 set_migr_type(dev, MIGR_INIT);
5675 vol->dirty = !info->state;
5676 set_vol_curr_migr_unit(dev, 0);
5677 map = get_imsm_map(dev, MAP_0);
5678 set_pba_of_lba0(map, super->create_offset);
5679 map->blocks_per_strip = __cpu_to_le16(info_to_blocks_per_strip(info));
5680 map->failed_disk_num = ~0;
5681 if (info->level > 0)
5682 map->map_state = (info->state ? IMSM_T_STATE_NORMAL
5683 : IMSM_T_STATE_UNINITIALIZED);
5684 else
5685 map->map_state = info->failed_disks ? IMSM_T_STATE_FAILED :
5686 IMSM_T_STATE_NORMAL;
5687 map->ddf = 1;
5688
5689 if (info->level == 1 && info->raid_disks > 2) {
5690 free(dev);
5691 free(dv);
5692 pr_err("imsm does not support more than 2 disksin a raid1 volume\n");
5693 return 0;
5694 }
5695
5696 map->raid_level = info->level;
5697 if (info->level == 10)
5698 map->raid_level = 1;
5699 set_num_domains(map);
5700
5701 size_per_member += NUM_BLOCKS_DIRTY_STRIPE_REGION;
5702 set_blocks_per_member(map, info_to_blocks_per_member(info,
5703 size_per_member /
5704 BLOCKS_PER_KB));
5705
5706 map->num_members = info->raid_disks;
5707 update_num_data_stripes(map, array_blocks);
5708 for (i = 0; i < map->num_members; i++) {
5709 /* initialized in add_to_super */
5710 set_imsm_ord_tbl_ent(map, i, IMSM_ORD_REBUILD);
5711 }
5712 mpb->num_raid_devs++;
5713 mpb->num_raid_devs_created++;
5714 dev->my_vol_raid_dev_num = mpb->num_raid_devs_created;
5715
5716 if (s->consistency_policy <= CONSISTENCY_POLICY_RESYNC) {
5717 dev->rwh_policy = RWH_MULTIPLE_OFF;
5718 } else if (s->consistency_policy == CONSISTENCY_POLICY_PPL) {
5719 dev->rwh_policy = RWH_MULTIPLE_DISTRIBUTED;
5720 } else {
5721 free(dev);
5722 free(dv);
5723 pr_err("imsm does not support consistency policy %s\n",
5724 map_num_s(consistency_policies, s->consistency_policy));
5725 return 0;
5726 }
5727
5728 dv->dev = dev;
5729 dv->index = super->current_vol;
5730 dv->next = super->devlist;
5731 super->devlist = dv;
5732
5733 imsm_update_version_info(super);
5734
5735 return 1;
5736 }
5737
5738 static int init_super_imsm(struct supertype *st, mdu_array_info_t *info,
5739 struct shape *s, char *name,
5740 char *homehost, int *uuid,
5741 unsigned long long data_offset)
5742 {
5743 /* This is primarily called by Create when creating a new array.
5744 * We will then get add_to_super called for each component, and then
5745 * write_init_super called to write it out to each device.
5746 * For IMSM, Create can create on fresh devices or on a pre-existing
5747 * array.
5748 * To create on a pre-existing array a different method will be called.
5749 * This one is just for fresh drives.
5750 */
5751 struct intel_super *super;
5752 struct imsm_super *mpb;
5753 size_t mpb_size;
5754 char *version;
5755
5756 if (data_offset != INVALID_SECTORS) {
5757 pr_err("data-offset not supported by imsm\n");
5758 return 0;
5759 }
5760
5761 if (st->sb)
5762 return init_super_imsm_volume(st, info, s, name, homehost, uuid,
5763 data_offset);
5764
5765 if (info)
5766 mpb_size = disks_to_mpb_size(info->nr_disks);
5767 else
5768 mpb_size = MAX_SECTOR_SIZE;
5769
5770 super = alloc_super();
5771 if (super &&
5772 posix_memalign(&super->buf, MAX_SECTOR_SIZE, mpb_size) != 0) {
5773 free_imsm(super);
5774 super = NULL;
5775 }
5776 if (!super) {
5777 pr_err("could not allocate superblock\n");
5778 return 0;
5779 }
5780 if (posix_memalign(&super->migr_rec_buf, MAX_SECTOR_SIZE,
5781 MIGR_REC_BUF_SECTORS*MAX_SECTOR_SIZE) != 0) {
5782 pr_err("could not allocate migr_rec buffer\n");
5783 free(super->buf);
5784 free_imsm(super);
5785 return 0;
5786 }
5787 memset(super->buf, 0, mpb_size);
5788 mpb = super->buf;
5789 mpb->mpb_size = __cpu_to_le32(mpb_size);
5790 st->sb = super;
5791
5792 if (info == NULL) {
5793 /* zeroing superblock */
5794 return 0;
5795 }
5796
5797 mpb->attributes = MPB_ATTRIB_CHECKSUM_VERIFY;
5798
5799 version = (char *) mpb->sig;
5800 strcpy(version, MPB_SIGNATURE);
5801 version += strlen(MPB_SIGNATURE);
5802 strcpy(version, MPB_VERSION_RAID0);
5803
5804 return 1;
5805 }
5806
5807 static int drive_validate_sector_size(struct intel_super *super, struct dl *dl)
5808 {
5809 unsigned int member_sector_size;
5810
5811 if (!is_fd_valid(dl->fd)) {
5812 pr_err("Invalid file descriptor for %s\n", dl->devname);
5813 return 0;
5814 }
5815
5816 if (!get_dev_sector_size(dl->fd, dl->devname, &member_sector_size))
5817 return 0;
5818 if (member_sector_size != super->sector_size)
5819 return 0;
5820 return 1;
5821 }
5822
5823 static int add_to_super_imsm_volume(struct supertype *st, mdu_disk_info_t *dk,
5824 int fd, char *devname)
5825 {
5826 struct intel_super *super = st->sb;
5827 struct imsm_super *mpb = super->anchor;
5828 struct imsm_disk *_disk;
5829 struct imsm_dev *dev;
5830 struct imsm_map *map;
5831 struct dl *dl, *df;
5832 int slot;
5833 int autolayout = 0;
5834
5835 if (!is_fd_valid(fd))
5836 autolayout = 1;
5837
5838 dev = get_imsm_dev(super, super->current_vol);
5839 map = get_imsm_map(dev, MAP_0);
5840
5841 if (! (dk->state & (1<<MD_DISK_SYNC))) {
5842 pr_err("%s: Cannot add spare devices to IMSM volume\n",
5843 devname);
5844 return 1;
5845 }
5846
5847 for (dl = super->disks; dl ; dl = dl->next) {
5848 if (autolayout) {
5849 if (dl->raiddisk == dk->raid_disk)
5850 break;
5851 } else if (dl->major == dk->major && dl->minor == dk->minor)
5852 break;
5853 }
5854
5855 if (!dl) {
5856 if (!autolayout)
5857 pr_err("%s is not a member of the same container.\n",
5858 devname);
5859 return 1;
5860 }
5861
5862 if (!autolayout && super->current_vol > 0) {
5863 int _slot = get_disk_slot_in_dev(super, 0, dl->index);
5864
5865 if (_slot != dk->raid_disk) {
5866 pr_err("Member %s is in %d slot for the first volume, but is in %d slot for a new volume.\n",
5867 dl->devname, _slot, dk->raid_disk);
5868 pr_err("Raid members are in different order than for the first volume, aborting.\n");
5869 return 1;
5870 }
5871 }
5872
5873 if (mpb->num_disks == 0)
5874 if (!get_dev_sector_size(dl->fd, dl->devname,
5875 &super->sector_size))
5876 return 1;
5877
5878 if (!drive_validate_sector_size(super, dl)) {
5879 pr_err("Combining drives of different sector size in one volume is not allowed\n");
5880 return 1;
5881 }
5882
5883 /* add a pristine spare to the metadata */
5884 if (dl->index < 0) {
5885 dl->index = super->anchor->num_disks;
5886 super->anchor->num_disks++;
5887 }
5888 /* Check the device has not already been added */
5889 slot = get_imsm_disk_slot(map, dl->index);
5890 if (slot >= 0 &&
5891 (get_imsm_ord_tbl_ent(dev, slot, MAP_X) & IMSM_ORD_REBUILD) == 0) {
5892 pr_err("%s has been included in this array twice\n",
5893 devname);
5894 return 1;
5895 }
5896 set_imsm_ord_tbl_ent(map, dk->raid_disk, dl->index);
5897 dl->disk.status = CONFIGURED_DISK;
5898
5899 /* update size of 'missing' disks to be at least as large as the
5900 * largest acitve member (we only have dummy missing disks when
5901 * creating the first volume)
5902 */
5903 if (super->current_vol == 0) {
5904 for (df = super->missing; df; df = df->next) {
5905 if (total_blocks(&dl->disk) > total_blocks(&df->disk))
5906 set_total_blocks(&df->disk, total_blocks(&dl->disk));
5907 _disk = __get_imsm_disk(mpb, df->index);
5908 *_disk = df->disk;
5909 }
5910 }
5911
5912 /* refresh unset/failed slots to point to valid 'missing' entries */
5913 for (df = super->missing; df; df = df->next)
5914 for (slot = 0; slot < mpb->num_disks; slot++) {
5915 __u32 ord = get_imsm_ord_tbl_ent(dev, slot, MAP_X);
5916
5917 if ((ord & IMSM_ORD_REBUILD) == 0)
5918 continue;
5919 set_imsm_ord_tbl_ent(map, slot, df->index | IMSM_ORD_REBUILD);
5920 if (is_gen_migration(dev)) {
5921 struct imsm_map *map2 = get_imsm_map(dev,
5922 MAP_1);
5923 int slot2 = get_imsm_disk_slot(map2, df->index);
5924 if (slot2 < map2->num_members && slot2 >= 0) {
5925 __u32 ord2 = get_imsm_ord_tbl_ent(dev,
5926 slot2,
5927 MAP_1);
5928 if ((unsigned)df->index ==
5929 ord_to_idx(ord2))
5930 set_imsm_ord_tbl_ent(map2,
5931 slot2,
5932 df->index |
5933 IMSM_ORD_REBUILD);
5934 }
5935 }
5936 dprintf("set slot:%d to missing disk:%d\n", slot, df->index);
5937 break;
5938 }
5939
5940 /* if we are creating the first raid device update the family number */
5941 if (super->current_vol == 0) {
5942 __u32 sum;
5943 struct imsm_dev *_dev = __get_imsm_dev(mpb, 0);
5944
5945 _disk = __get_imsm_disk(mpb, dl->index);
5946 if (!_disk) {
5947 pr_err("BUG mpb setup error\n");
5948 return 1;
5949 }
5950 *_dev = *dev;
5951 *_disk = dl->disk;
5952 sum = random32();
5953 sum += __gen_imsm_checksum(mpb);
5954 mpb->family_num = __cpu_to_le32(sum);
5955 mpb->orig_family_num = mpb->family_num;
5956 mpb->creation_time = __cpu_to_le64((__u64)time(NULL));
5957 }
5958 super->current_disk = dl;
5959 return 0;
5960 }
5961
5962 /* mark_spare()
5963 * Function marks disk as spare and restores disk serial
5964 * in case it was previously marked as failed by takeover operation
5965 * reruns:
5966 * -1 : critical error
5967 * 0 : disk is marked as spare but serial is not set
5968 * 1 : success
5969 */
5970 int mark_spare(struct dl *disk)
5971 {
5972 __u8 serial[MAX_RAID_SERIAL_LEN];
5973 int ret_val = -1;
5974
5975 if (!disk)
5976 return ret_val;
5977
5978 ret_val = 0;
5979 if (!imsm_read_serial(disk->fd, NULL, serial, MAX_RAID_SERIAL_LEN)) {
5980 /* Restore disk serial number, because takeover marks disk
5981 * as failed and adds to serial ':0' before it becomes
5982 * a spare disk.
5983 */
5984 serialcpy(disk->serial, serial);
5985 serialcpy(disk->disk.serial, serial);
5986 ret_val = 1;
5987 }
5988 disk->disk.status = SPARE_DISK;
5989 disk->index = -1;
5990
5991 return ret_val;
5992 }
5993
5994
5995 static int write_super_imsm_spare(struct intel_super *super, struct dl *d);
5996
5997 static int add_to_super_imsm(struct supertype *st, mdu_disk_info_t *dk,
5998 int fd, char *devname,
5999 unsigned long long data_offset)
6000 {
6001 struct intel_super *super = st->sb;
6002 struct dl *dd;
6003 unsigned long long size;
6004 unsigned int member_sector_size;
6005 __u32 id;
6006 int rv;
6007 struct stat stb;
6008
6009 /* If we are on an RAID enabled platform check that the disk is
6010 * attached to the raid controller.
6011 * We do not need to test disks attachment for container based additions,
6012 * they shall be already tested when container was created/assembled.
6013 */
6014 rv = find_intel_hba_capability(fd, super, devname);
6015 /* no orom/efi or non-intel hba of the disk */
6016 if (rv != 0) {
6017 dprintf("capability: %p fd: %d ret: %d\n",
6018 super->orom, fd, rv);
6019 return 1;
6020 }
6021
6022 if (super->current_vol >= 0)
6023 return add_to_super_imsm_volume(st, dk, fd, devname);
6024
6025 fstat(fd, &stb);
6026 dd = xcalloc(sizeof(*dd), 1);
6027 dd->major = major(stb.st_rdev);
6028 dd->minor = minor(stb.st_rdev);
6029 dd->devname = devname ? xstrdup(devname) : NULL;
6030 dd->fd = fd;
6031 dd->e = NULL;
6032 dd->action = DISK_ADD;
6033 rv = imsm_read_serial(fd, devname, dd->serial, MAX_RAID_SERIAL_LEN);
6034 if (rv) {
6035 pr_err("failed to retrieve scsi serial, aborting\n");
6036 __free_imsm_disk(dd, 0);
6037 abort();
6038 }
6039
6040 if (super->hba && ((super->hba->type == SYS_DEV_NVME) ||
6041 (super->hba->type == SYS_DEV_VMD))) {
6042 int i;
6043 char cntrl_path[PATH_MAX];
6044 char *cntrl_name;
6045 char pci_dev_path[PATH_MAX];
6046
6047 if (!diskfd_to_devpath(fd, 2, pci_dev_path) ||
6048 !diskfd_to_devpath(fd, 1, cntrl_path)) {
6049 pr_err("failed to get dev paths, aborting\n");
6050 __free_imsm_disk(dd, 0);
6051 return 1;
6052 }
6053
6054 cntrl_name = basename(cntrl_path);
6055 if (is_multipath_nvme(fd))
6056 pr_err("%s controller supports Multi-Path I/O, Intel (R) VROC does not support multipathing\n",
6057 cntrl_name);
6058
6059 if (devpath_to_vendor(pci_dev_path) == 0x8086) {
6060 /*
6061 * If Intel's NVMe drive has serial ended with
6062 * "-A","-B","-1" or "-2" it means that this is "x8"
6063 * device (double drive on single PCIe card).
6064 * User should be warned about potential data loss.
6065 */
6066 for (i = MAX_RAID_SERIAL_LEN-1; i > 0; i--) {
6067 /* Skip empty character at the end */
6068 if (dd->serial[i] == 0)
6069 continue;
6070
6071 if (((dd->serial[i] == 'A') ||
6072 (dd->serial[i] == 'B') ||
6073 (dd->serial[i] == '1') ||
6074 (dd->serial[i] == '2')) &&
6075 (dd->serial[i-1] == '-'))
6076 pr_err("\tThe action you are about to take may put your data at risk.\n"
6077 "\tPlease note that x8 devices may consist of two separate x4 devices "
6078 "located on a single PCIe port.\n"
6079 "\tRAID 0 is the only supported configuration for this type of x8 device.\n");
6080 break;
6081 }
6082 } else if (super->hba->type == SYS_DEV_VMD && super->orom &&
6083 !imsm_orom_has_tpv_support(super->orom)) {
6084 pr_err("\tPlatform configuration does not support non-Intel NVMe drives.\n"
6085 "\tPlease refer to Intel(R) RSTe/VROC user guide.\n");
6086 __free_imsm_disk(dd, 0);
6087 return 1;
6088 }
6089 }
6090
6091 get_dev_size(fd, NULL, &size);
6092 if (!get_dev_sector_size(fd, NULL, &member_sector_size)) {
6093 __free_imsm_disk(dd, 0);
6094 return 1;
6095 }
6096
6097 if (super->sector_size == 0) {
6098 /* this a first device, so sector_size is not set yet */
6099 super->sector_size = member_sector_size;
6100 }
6101
6102 /* clear migr_rec when adding disk to container */
6103 memset(super->migr_rec_buf, 0, MIGR_REC_BUF_SECTORS*MAX_SECTOR_SIZE);
6104 if (lseek64(fd, size - MIGR_REC_SECTOR_POSITION*member_sector_size,
6105 SEEK_SET) >= 0) {
6106 if ((unsigned int)write(fd, super->migr_rec_buf,
6107 MIGR_REC_BUF_SECTORS*member_sector_size) !=
6108 MIGR_REC_BUF_SECTORS*member_sector_size)
6109 perror("Write migr_rec failed");
6110 }
6111
6112 size /= 512;
6113 serialcpy(dd->disk.serial, dd->serial);
6114 set_total_blocks(&dd->disk, size);
6115 if (__le32_to_cpu(dd->disk.total_blocks_hi) > 0) {
6116 struct imsm_super *mpb = super->anchor;
6117 mpb->attributes |= MPB_ATTRIB_2TB_DISK;
6118 }
6119 mark_spare(dd);
6120 if (sysfs_disk_to_scsi_id(fd, &id) == 0)
6121 dd->disk.scsi_id = __cpu_to_le32(id);
6122 else
6123 dd->disk.scsi_id = __cpu_to_le32(0);
6124
6125 if (st->update_tail) {
6126 dd->next = super->disk_mgmt_list;
6127 super->disk_mgmt_list = dd;
6128 } else {
6129 /* this is called outside of mdmon
6130 * write initial spare metadata
6131 * mdmon will overwrite it.
6132 */
6133 dd->next = super->disks;
6134 super->disks = dd;
6135 write_super_imsm_spare(super, dd);
6136 }
6137
6138 return 0;
6139 }
6140
6141 static int remove_from_super_imsm(struct supertype *st, mdu_disk_info_t *dk)
6142 {
6143 struct intel_super *super = st->sb;
6144 struct dl *dd;
6145
6146 /* remove from super works only in mdmon - for communication
6147 * manager - monitor. Check if communication memory buffer
6148 * is prepared.
6149 */
6150 if (!st->update_tail) {
6151 pr_err("shall be used in mdmon context only\n");
6152 return 1;
6153 }
6154 dd = xcalloc(1, sizeof(*dd));
6155 dd->major = dk->major;
6156 dd->minor = dk->minor;
6157 dd->fd = -1;
6158 mark_spare(dd);
6159 dd->action = DISK_REMOVE;
6160
6161 dd->next = super->disk_mgmt_list;
6162 super->disk_mgmt_list = dd;
6163
6164 return 0;
6165 }
6166
6167 static int store_imsm_mpb(int fd, struct imsm_super *mpb);
6168
6169 static union {
6170 char buf[MAX_SECTOR_SIZE];
6171 struct imsm_super anchor;
6172 } spare_record __attribute__ ((aligned(MAX_SECTOR_SIZE)));
6173
6174
6175 static int write_super_imsm_spare(struct intel_super *super, struct dl *d)
6176 {
6177 struct imsm_super *mpb = super->anchor;
6178 struct imsm_super *spare = &spare_record.anchor;
6179 __u32 sum;
6180
6181 if (d->index != -1)
6182 return 1;
6183
6184 spare->mpb_size = __cpu_to_le32(sizeof(struct imsm_super));
6185 spare->generation_num = __cpu_to_le32(1UL);
6186 spare->attributes = MPB_ATTRIB_CHECKSUM_VERIFY;
6187 spare->num_disks = 1;
6188 spare->num_raid_devs = 0;
6189 spare->cache_size = mpb->cache_size;
6190 spare->pwr_cycle_count = __cpu_to_le32(1);
6191
6192 snprintf((char *) spare->sig, MAX_SIGNATURE_LENGTH,
6193 MPB_SIGNATURE MPB_VERSION_RAID0);
6194
6195 spare->disk[0] = d->disk;
6196 if (__le32_to_cpu(d->disk.total_blocks_hi) > 0)
6197 spare->attributes |= MPB_ATTRIB_2TB_DISK;
6198
6199 if (super->sector_size == 4096)
6200 convert_to_4k_imsm_disk(&spare->disk[0]);
6201
6202 sum = __gen_imsm_checksum(spare);
6203 spare->family_num = __cpu_to_le32(sum);
6204 spare->orig_family_num = 0;
6205 sum = __gen_imsm_checksum(spare);
6206 spare->check_sum = __cpu_to_le32(sum);
6207
6208 if (store_imsm_mpb(d->fd, spare)) {
6209 pr_err("failed for device %d:%d %s\n",
6210 d->major, d->minor, strerror(errno));
6211 return 1;
6212 }
6213
6214 return 0;
6215 }
6216 /* spare records have their own family number and do not have any defined raid
6217 * devices
6218 */
6219 static int write_super_imsm_spares(struct intel_super *super, int doclose)
6220 {
6221 struct dl *d;
6222
6223 for (d = super->disks; d; d = d->next) {
6224 if (d->index != -1)
6225 continue;
6226
6227 if (write_super_imsm_spare(super, d))
6228 return 1;
6229
6230 if (doclose)
6231 close_fd(&d->fd);
6232 }
6233
6234 return 0;
6235 }
6236
6237 static int write_super_imsm(struct supertype *st, int doclose)
6238 {
6239 struct intel_super *super = st->sb;
6240 unsigned int sector_size = super->sector_size;
6241 struct imsm_super *mpb = super->anchor;
6242 struct dl *d;
6243 __u32 generation;
6244 __u32 sum;
6245 int spares = 0;
6246 int i;
6247 __u32 mpb_size = sizeof(struct imsm_super) - sizeof(struct imsm_disk);
6248 int num_disks = 0;
6249 int clear_migration_record = 1;
6250 __u32 bbm_log_size;
6251
6252 /* 'generation' is incremented everytime the metadata is written */
6253 generation = __le32_to_cpu(mpb->generation_num);
6254 generation++;
6255 mpb->generation_num = __cpu_to_le32(generation);
6256
6257 /* fix up cases where previous mdadm releases failed to set
6258 * orig_family_num
6259 */
6260 if (mpb->orig_family_num == 0)
6261 mpb->orig_family_num = mpb->family_num;
6262
6263 for (d = super->disks; d; d = d->next) {
6264 if (d->index == -1)
6265 spares++;
6266 else {
6267 mpb->disk[d->index] = d->disk;
6268 num_disks++;
6269 }
6270 }
6271 for (d = super->missing; d; d = d->next) {
6272 mpb->disk[d->index] = d->disk;
6273 num_disks++;
6274 }
6275 mpb->num_disks = num_disks;
6276 mpb_size += sizeof(struct imsm_disk) * mpb->num_disks;
6277
6278 for (i = 0; i < mpb->num_raid_devs; i++) {
6279 struct imsm_dev *dev = __get_imsm_dev(mpb, i);
6280 struct imsm_dev *dev2 = get_imsm_dev(super, i);
6281
6282 imsm_copy_dev(dev, dev2);
6283 mpb_size += sizeof_imsm_dev(dev, 0);
6284
6285 if (is_gen_migration(dev2))
6286 clear_migration_record = 0;
6287 }
6288
6289 bbm_log_size = get_imsm_bbm_log_size(super->bbm_log);
6290
6291 if (bbm_log_size) {
6292 memcpy((void *)mpb + mpb_size, super->bbm_log, bbm_log_size);
6293 mpb->attributes |= MPB_ATTRIB_BBM;
6294 } else
6295 mpb->attributes &= ~MPB_ATTRIB_BBM;
6296
6297 super->anchor->bbm_log_size = __cpu_to_le32(bbm_log_size);
6298 mpb_size += bbm_log_size;
6299 mpb->mpb_size = __cpu_to_le32(mpb_size);
6300
6301 #ifdef DEBUG
6302 assert(super->len == 0 || mpb_size <= super->len);
6303 #endif
6304
6305 /* recalculate checksum */
6306 sum = __gen_imsm_checksum(mpb);
6307 mpb->check_sum = __cpu_to_le32(sum);
6308
6309 if (super->clean_migration_record_by_mdmon) {
6310 clear_migration_record = 1;
6311 super->clean_migration_record_by_mdmon = 0;
6312 }
6313 if (clear_migration_record)
6314 memset(super->migr_rec_buf, 0,
6315 MIGR_REC_BUF_SECTORS*MAX_SECTOR_SIZE);
6316
6317 if (sector_size == 4096)
6318 convert_to_4k(super);
6319
6320 /* write the mpb for disks that compose raid devices */
6321 for (d = super->disks; d ; d = d->next) {
6322 if (d->index < 0 || is_failed(&d->disk))
6323 continue;
6324
6325 if (clear_migration_record) {
6326 unsigned long long dsize;
6327
6328 get_dev_size(d->fd, NULL, &dsize);
6329 if (lseek64(d->fd, dsize - sector_size,
6330 SEEK_SET) >= 0) {
6331 if ((unsigned int)write(d->fd,
6332 super->migr_rec_buf,
6333 MIGR_REC_BUF_SECTORS*sector_size) !=
6334 MIGR_REC_BUF_SECTORS*sector_size)
6335 perror("Write migr_rec failed");
6336 }
6337 }
6338
6339 if (store_imsm_mpb(d->fd, mpb))
6340 fprintf(stderr,
6341 "failed for device %d:%d (fd: %d)%s\n",
6342 d->major, d->minor,
6343 d->fd, strerror(errno));
6344
6345 if (doclose)
6346 close_fd(&d->fd);
6347 }
6348
6349 if (spares)
6350 return write_super_imsm_spares(super, doclose);
6351
6352 return 0;
6353 }
6354
6355 static int create_array(struct supertype *st, int dev_idx)
6356 {
6357 size_t len;
6358 struct imsm_update_create_array *u;
6359 struct intel_super *super = st->sb;
6360 struct imsm_dev *dev = get_imsm_dev(super, dev_idx);
6361 struct imsm_map *map = get_imsm_map(dev, MAP_0);
6362 struct disk_info *inf;
6363 struct imsm_disk *disk;
6364 int i;
6365
6366 len = sizeof(*u) - sizeof(*dev) + sizeof_imsm_dev(dev, 0) +
6367 sizeof(*inf) * map->num_members;
6368 u = xmalloc(len);
6369 u->type = update_create_array;
6370 u->dev_idx = dev_idx;
6371 imsm_copy_dev(&u->dev, dev);
6372 inf = get_disk_info(u);
6373 for (i = 0; i < map->num_members; i++) {
6374 int idx = get_imsm_disk_idx(dev, i, MAP_X);
6375
6376 disk = get_imsm_disk(super, idx);
6377 if (!disk)
6378 disk = get_imsm_missing(super, idx);
6379 serialcpy(inf[i].serial, disk->serial);
6380 }
6381 append_metadata_update(st, u, len);
6382
6383 return 0;
6384 }
6385
6386 static int mgmt_disk(struct supertype *st)
6387 {
6388 struct intel_super *super = st->sb;
6389 size_t len;
6390 struct imsm_update_add_remove_disk *u;
6391
6392 if (!super->disk_mgmt_list)
6393 return 0;
6394
6395 len = sizeof(*u);
6396 u = xmalloc(len);
6397 u->type = update_add_remove_disk;
6398 append_metadata_update(st, u, len);
6399
6400 return 0;
6401 }
6402
6403 __u32 crc32c_le(__u32 crc, unsigned char const *p, size_t len);
6404
6405 static int write_ppl_header(unsigned long long ppl_sector, int fd, void *buf)
6406 {
6407 struct ppl_header *ppl_hdr = buf;
6408 int ret;
6409
6410 ppl_hdr->checksum = __cpu_to_le32(~crc32c_le(~0, buf, PPL_HEADER_SIZE));
6411
6412 if (lseek64(fd, ppl_sector * 512, SEEK_SET) < 0) {
6413 ret = -errno;
6414 perror("Failed to seek to PPL header location");
6415 return ret;
6416 }
6417
6418 if (write(fd, buf, PPL_HEADER_SIZE) != PPL_HEADER_SIZE) {
6419 ret = -errno;
6420 perror("Write PPL header failed");
6421 return ret;
6422 }
6423
6424 fsync(fd);
6425
6426 return 0;
6427 }
6428
6429 static int write_init_ppl_imsm(struct supertype *st, struct mdinfo *info, int fd)
6430 {
6431 struct intel_super *super = st->sb;
6432 void *buf;
6433 struct ppl_header *ppl_hdr;
6434 int ret;
6435
6436 /* first clear entire ppl space */
6437 ret = zero_disk_range(fd, info->ppl_sector, info->ppl_size);
6438 if (ret)
6439 return ret;
6440
6441 ret = posix_memalign(&buf, MAX_SECTOR_SIZE, PPL_HEADER_SIZE);
6442 if (ret) {
6443 pr_err("Failed to allocate PPL header buffer\n");
6444 return -ret;
6445 }
6446
6447 memset(buf, 0, PPL_HEADER_SIZE);
6448 ppl_hdr = buf;
6449 memset(ppl_hdr->reserved, 0xff, PPL_HDR_RESERVED);
6450 ppl_hdr->signature = __cpu_to_le32(super->anchor->orig_family_num);
6451
6452 if (info->mismatch_cnt) {
6453 /*
6454 * We are overwriting an invalid ppl. Make one entry with wrong
6455 * checksum to prevent the kernel from skipping resync.
6456 */
6457 ppl_hdr->entries_count = __cpu_to_le32(1);
6458 ppl_hdr->entries[0].checksum = ~0;
6459 }
6460
6461 ret = write_ppl_header(info->ppl_sector, fd, buf);
6462
6463 free(buf);
6464 return ret;
6465 }
6466
6467 static int is_rebuilding(struct imsm_dev *dev);
6468
6469 static int validate_ppl_imsm(struct supertype *st, struct mdinfo *info,
6470 struct mdinfo *disk)
6471 {
6472 struct intel_super *super = st->sb;
6473 struct dl *d;
6474 void *buf_orig, *buf, *buf_prev = NULL;
6475 int ret = 0;
6476 struct ppl_header *ppl_hdr = NULL;
6477 __u32 crc;
6478 struct imsm_dev *dev;
6479 __u32 idx;
6480 unsigned int i;
6481 unsigned long long ppl_offset = 0;
6482 unsigned long long prev_gen_num = 0;
6483
6484 if (disk->disk.raid_disk < 0)
6485 return 0;
6486
6487 dev = get_imsm_dev(super, info->container_member);
6488 idx = get_imsm_disk_idx(dev, disk->disk.raid_disk, MAP_0);
6489 d = get_imsm_dl_disk(super, idx);
6490
6491 if (!d || d->index < 0 || is_failed(&d->disk))
6492 return 0;
6493
6494 if (posix_memalign(&buf_orig, MAX_SECTOR_SIZE, PPL_HEADER_SIZE * 2)) {
6495 pr_err("Failed to allocate PPL header buffer\n");
6496 return -1;
6497 }
6498 buf = buf_orig;
6499
6500 ret = 1;
6501 while (ppl_offset < MULTIPLE_PPL_AREA_SIZE_IMSM) {
6502 void *tmp;
6503
6504 dprintf("Checking potential PPL at offset: %llu\n", ppl_offset);
6505
6506 if (lseek64(d->fd, info->ppl_sector * 512 + ppl_offset,
6507 SEEK_SET) < 0) {
6508 perror("Failed to seek to PPL header location");
6509 ret = -1;
6510 break;
6511 }
6512
6513 if (read(d->fd, buf, PPL_HEADER_SIZE) != PPL_HEADER_SIZE) {
6514 perror("Read PPL header failed");
6515 ret = -1;
6516 break;
6517 }
6518
6519 ppl_hdr = buf;
6520
6521 crc = __le32_to_cpu(ppl_hdr->checksum);
6522 ppl_hdr->checksum = 0;
6523
6524 if (crc != ~crc32c_le(~0, buf, PPL_HEADER_SIZE)) {
6525 dprintf("Wrong PPL header checksum on %s\n",
6526 d->devname);
6527 break;
6528 }
6529
6530 if (prev_gen_num > __le64_to_cpu(ppl_hdr->generation)) {
6531 /* previous was newest, it was already checked */
6532 break;
6533 }
6534
6535 if ((__le32_to_cpu(ppl_hdr->signature) !=
6536 super->anchor->orig_family_num)) {
6537 dprintf("Wrong PPL header signature on %s\n",
6538 d->devname);
6539 ret = 1;
6540 break;
6541 }
6542
6543 ret = 0;
6544 prev_gen_num = __le64_to_cpu(ppl_hdr->generation);
6545
6546 ppl_offset += PPL_HEADER_SIZE;
6547 for (i = 0; i < __le32_to_cpu(ppl_hdr->entries_count); i++)
6548 ppl_offset +=
6549 __le32_to_cpu(ppl_hdr->entries[i].pp_size);
6550
6551 if (!buf_prev)
6552 buf_prev = buf + PPL_HEADER_SIZE;
6553 tmp = buf_prev;
6554 buf_prev = buf;
6555 buf = tmp;
6556 }
6557
6558 if (buf_prev) {
6559 buf = buf_prev;
6560 ppl_hdr = buf_prev;
6561 }
6562
6563 /*
6564 * Update metadata to use mutliple PPLs area (1MB).
6565 * This is done once for all RAID members
6566 */
6567 if (info->consistency_policy == CONSISTENCY_POLICY_PPL &&
6568 info->ppl_size != (MULTIPLE_PPL_AREA_SIZE_IMSM >> 9)) {
6569 char subarray[20];
6570 struct mdinfo *member_dev;
6571
6572 sprintf(subarray, "%d", info->container_member);
6573
6574 if (mdmon_running(st->container_devnm))
6575 st->update_tail = &st->updates;
6576
6577 if (st->ss->update_subarray(st, subarray, UOPT_PPL, NULL)) {
6578 pr_err("Failed to update subarray %s\n",
6579 subarray);
6580 } else {
6581 if (st->update_tail)
6582 flush_metadata_updates(st);
6583 else
6584 st->ss->sync_metadata(st);
6585 info->ppl_size = (MULTIPLE_PPL_AREA_SIZE_IMSM >> 9);
6586 for (member_dev = info->devs; member_dev;
6587 member_dev = member_dev->next)
6588 member_dev->ppl_size =
6589 (MULTIPLE_PPL_AREA_SIZE_IMSM >> 9);
6590 }
6591 }
6592
6593 if (ret == 1) {
6594 struct imsm_map *map = get_imsm_map(dev, MAP_X);
6595
6596 if (map->map_state == IMSM_T_STATE_UNINITIALIZED ||
6597 (map->map_state == IMSM_T_STATE_NORMAL &&
6598 !(dev->vol.dirty & RAIDVOL_DIRTY)) ||
6599 (is_rebuilding(dev) &&
6600 vol_curr_migr_unit(dev) == 0 &&
6601 get_imsm_disk_idx(dev, disk->disk.raid_disk, MAP_1) != idx))
6602 ret = st->ss->write_init_ppl(st, info, d->fd);
6603 else
6604 info->mismatch_cnt++;
6605 } else if (ret == 0 &&
6606 ppl_hdr->entries_count == 0 &&
6607 is_rebuilding(dev) &&
6608 info->resync_start == 0) {
6609 /*
6610 * The header has no entries - add a single empty entry and
6611 * rewrite the header to prevent the kernel from going into
6612 * resync after an interrupted rebuild.
6613 */
6614 ppl_hdr->entries_count = __cpu_to_le32(1);
6615 ret = write_ppl_header(info->ppl_sector, d->fd, buf);
6616 }
6617
6618 free(buf_orig);
6619
6620 return ret;
6621 }
6622
6623 static int write_init_ppl_imsm_all(struct supertype *st, struct mdinfo *info)
6624 {
6625 struct intel_super *super = st->sb;
6626 struct dl *d;
6627 int ret = 0;
6628
6629 if (info->consistency_policy != CONSISTENCY_POLICY_PPL ||
6630 info->array.level != 5)
6631 return 0;
6632
6633 for (d = super->disks; d ; d = d->next) {
6634 if (d->index < 0 || is_failed(&d->disk))
6635 continue;
6636
6637 ret = st->ss->write_init_ppl(st, info, d->fd);
6638 if (ret)
6639 break;
6640 }
6641
6642 return ret;
6643 }
6644
6645 /*******************************************************************************
6646 * Function: write_init_bitmap_imsm_vol
6647 * Description: Write a bitmap header and prepares the area for the bitmap.
6648 * Parameters:
6649 * st : supertype information
6650 * vol_idx : the volume index to use
6651 *
6652 * Returns:
6653 * 0 : success
6654 * -1 : fail
6655 ******************************************************************************/
6656 static int write_init_bitmap_imsm_vol(struct supertype *st, int vol_idx)
6657 {
6658 struct intel_super *super = st->sb;
6659 int prev_current_vol = super->current_vol;
6660 struct dl *d;
6661 int ret = 0;
6662
6663 super->current_vol = vol_idx;
6664 for (d = super->disks; d; d = d->next) {
6665 if (d->index < 0 || is_failed(&d->disk))
6666 continue;
6667 ret = st->ss->write_bitmap(st, d->fd, NoUpdate);
6668 if (ret)
6669 break;
6670 }
6671 super->current_vol = prev_current_vol;
6672 return ret;
6673 }
6674
6675 /*******************************************************************************
6676 * Function: write_init_bitmap_imsm_all
6677 * Description: Write a bitmap header and prepares the area for the bitmap.
6678 * Operation is executed for volumes with CONSISTENCY_POLICY_BITMAP.
6679 * Parameters:
6680 * st : supertype information
6681 * info : info about the volume where the bitmap should be written
6682 * vol_idx : the volume index to use
6683 *
6684 * Returns:
6685 * 0 : success
6686 * -1 : fail
6687 ******************************************************************************/
6688 static int write_init_bitmap_imsm_all(struct supertype *st, struct mdinfo *info,
6689 int vol_idx)
6690 {
6691 int ret = 0;
6692
6693 if (info && (info->consistency_policy == CONSISTENCY_POLICY_BITMAP))
6694 ret = write_init_bitmap_imsm_vol(st, vol_idx);
6695
6696 return ret;
6697 }
6698
6699 static int write_init_super_imsm(struct supertype *st)
6700 {
6701 struct intel_super *super = st->sb;
6702 int current_vol = super->current_vol;
6703 int rv = 0;
6704 struct mdinfo info;
6705
6706 getinfo_super_imsm(st, &info, NULL);
6707
6708 /* we are done with current_vol reset it to point st at the container */
6709 super->current_vol = -1;
6710
6711 if (st->update_tail) {
6712 /* queue the recently created array / added disk
6713 * as a metadata update */
6714
6715 /* determine if we are creating a volume or adding a disk */
6716 if (current_vol < 0) {
6717 /* in the mgmt (add/remove) disk case we are running
6718 * in mdmon context, so don't close fd's
6719 */
6720 rv = mgmt_disk(st);
6721 } else {
6722 /* adding the second volume to the array */
6723 rv = write_init_ppl_imsm_all(st, &info);
6724 if (!rv)
6725 rv = write_init_bitmap_imsm_all(st, &info, current_vol);
6726 if (!rv)
6727 rv = create_array(st, current_vol);
6728 }
6729 } else {
6730 struct dl *d;
6731 for (d = super->disks; d; d = d->next)
6732 Kill(d->devname, NULL, 0, -1, 1);
6733 if (current_vol >= 0) {
6734 rv = write_init_ppl_imsm_all(st, &info);
6735 if (!rv)
6736 rv = write_init_bitmap_imsm_all(st, &info, current_vol);
6737 }
6738
6739 if (!rv)
6740 rv = write_super_imsm(st, 1);
6741 }
6742
6743 return rv;
6744 }
6745
6746 static int store_super_imsm(struct supertype *st, int fd)
6747 {
6748 struct intel_super *super = st->sb;
6749 struct imsm_super *mpb = super ? super->anchor : NULL;
6750
6751 if (!mpb)
6752 return 1;
6753
6754 if (super->sector_size == 4096)
6755 convert_to_4k(super);
6756 return store_imsm_mpb(fd, mpb);
6757 }
6758
6759 static int validate_geometry_imsm_container(struct supertype *st, int level,
6760 int raiddisks,
6761 unsigned long long data_offset,
6762 char *dev,
6763 unsigned long long *freesize,
6764 int verbose)
6765 {
6766 int fd;
6767 unsigned long long ldsize;
6768 struct intel_super *super = NULL;
6769 int rv = 0;
6770
6771 if (!is_container(level))
6772 return 0;
6773 if (!dev)
6774 return 1;
6775
6776 fd = dev_open(dev, O_RDONLY|O_EXCL);
6777 if (!is_fd_valid(fd)) {
6778 pr_vrb("imsm: Cannot open %s: %s\n", dev, strerror(errno));
6779 return 0;
6780 }
6781 if (!get_dev_size(fd, dev, &ldsize))
6782 goto exit;
6783
6784 /* capabilities retrieve could be possible
6785 * note that there is no fd for the disks in array.
6786 */
6787 super = alloc_super();
6788 if (!super)
6789 goto exit;
6790
6791 if (!get_dev_sector_size(fd, NULL, &super->sector_size))
6792 goto exit;
6793
6794 rv = find_intel_hba_capability(fd, super, verbose > 0 ? dev : NULL);
6795 if (rv != 0) {
6796 #if DEBUG
6797 char str[256];
6798 fd2devname(fd, str);
6799 dprintf("fd: %d %s orom: %p rv: %d raiddisk: %d\n",
6800 fd, str, super->orom, rv, raiddisks);
6801 #endif
6802 /* no orom/efi or non-intel hba of the disk */
6803 rv = 0;
6804 goto exit;
6805 }
6806 if (super->orom) {
6807 if (raiddisks > super->orom->tds) {
6808 if (verbose)
6809 pr_err("%d exceeds maximum number of platform supported disks: %d\n",
6810 raiddisks, super->orom->tds);
6811 goto exit;
6812 }
6813 if ((super->orom->attr & IMSM_OROM_ATTR_2TB_DISK) == 0 &&
6814 (ldsize >> 9) >> 32 > 0) {
6815 if (verbose)
6816 pr_err("%s exceeds maximum platform supported size\n", dev);
6817 goto exit;
6818 }
6819
6820 if (super->hba->type == SYS_DEV_VMD ||
6821 super->hba->type == SYS_DEV_NVME) {
6822 if (!imsm_is_nvme_namespace_supported(fd, 1)) {
6823 if (verbose)
6824 pr_err("NVMe namespace %s is not supported by IMSM\n",
6825 basename(dev));
6826 goto exit;
6827 }
6828 }
6829 }
6830 if (freesize)
6831 *freesize = avail_size_imsm(st, ldsize >> 9, data_offset);
6832 rv = 1;
6833 exit:
6834 if (super)
6835 free_imsm(super);
6836 close(fd);
6837
6838 return rv;
6839 }
6840
6841 static unsigned long long find_size(struct extent *e, int *idx, int num_extents)
6842 {
6843 const unsigned long long base_start = e[*idx].start;
6844 unsigned long long end = base_start + e[*idx].size;
6845 int i;
6846
6847 if (base_start == end)
6848 return 0;
6849
6850 *idx = *idx + 1;
6851 for (i = *idx; i < num_extents; i++) {
6852 /* extend overlapping extents */
6853 if (e[i].start >= base_start &&
6854 e[i].start <= end) {
6855 if (e[i].size == 0)
6856 return 0;
6857 if (e[i].start + e[i].size > end)
6858 end = e[i].start + e[i].size;
6859 } else if (e[i].start > end) {
6860 *idx = i;
6861 break;
6862 }
6863 }
6864
6865 return end - base_start;
6866 }
6867
6868 /** merge_extents() - analyze extents and get free size.
6869 * @super: Intel metadata, not NULL.
6870 * @expanding: if set, we are expanding &super->current_vol.
6871 *
6872 * Build a composite disk with all known extents and generate a size given the
6873 * "all disks in an array must share a common start offset" constraint.
6874 * If a volume is expanded, then return free space after the volume.
6875 *
6876 * Return: Free space or 0 on failure.
6877 */
6878 static unsigned long long merge_extents(struct intel_super *super, const bool expanding)
6879 {
6880 struct extent *e;
6881 struct dl *dl;
6882 int i, j, pos_vol_idx = -1;
6883 int extent_idx = 0;
6884 int sum_extents = 0;
6885 unsigned long long pos = 0;
6886 unsigned long long start = 0;
6887 unsigned long long free_size = 0;
6888
6889 unsigned long pre_reservation = 0;
6890 unsigned long post_reservation = IMSM_RESERVED_SECTORS;
6891 unsigned long reservation_size;
6892
6893 for (dl = super->disks; dl; dl = dl->next)
6894 if (dl->e)
6895 sum_extents += dl->extent_cnt;
6896 e = xcalloc(sum_extents, sizeof(struct extent));
6897
6898 /* coalesce and sort all extents. also, check to see if we need to
6899 * reserve space between member arrays
6900 */
6901 j = 0;
6902 for (dl = super->disks; dl; dl = dl->next) {
6903 if (!dl->e)
6904 continue;
6905 for (i = 0; i < dl->extent_cnt; i++)
6906 e[j++] = dl->e[i];
6907 }
6908 qsort(e, sum_extents, sizeof(*e), cmp_extent);
6909
6910 /* merge extents */
6911 i = 0;
6912 j = 0;
6913 while (i < sum_extents) {
6914 e[j].start = e[i].start;
6915 e[j].vol = e[i].vol;
6916 e[j].size = find_size(e, &i, sum_extents);
6917 j++;
6918 if (e[j-1].size == 0)
6919 break;
6920 }
6921
6922 i = 0;
6923 do {
6924 unsigned long long esize = e[i].start - pos;
6925
6926 if (expanding ? pos_vol_idx == super->current_vol : esize >= free_size) {
6927 free_size = esize;
6928 start = pos;
6929 extent_idx = i;
6930 }
6931
6932 pos = e[i].start + e[i].size;
6933 pos_vol_idx = e[i].vol;
6934
6935 i++;
6936 } while (e[i-1].size);
6937
6938 if (free_size == 0) {
6939 dprintf("imsm: Cannot find free size.\n");
6940 free(e);
6941 return 0;
6942 }
6943
6944 if (!expanding && extent_idx != 0)
6945 /*
6946 * Not a real first volume in a container is created, pre_reservation is needed.
6947 */
6948 pre_reservation = IMSM_RESERVED_SECTORS;
6949
6950 if (e[extent_idx].size == 0)
6951 /*
6952 * extent_idx points to the metadata, post_reservation is allready done.
6953 */
6954 post_reservation = 0;
6955 free(e);
6956
6957 reservation_size = pre_reservation + post_reservation;
6958
6959 if (free_size < reservation_size) {
6960 dprintf("imsm: Reservation size is greater than free space.\n");
6961 return 0;
6962 }
6963
6964 super->create_offset = start + pre_reservation;
6965 return free_size - reservation_size;
6966 }
6967
6968 static int is_raid_level_supported(const struct imsm_orom *orom, int level, int raiddisks)
6969 {
6970 if (level < 0 || level == 6 || level == 4)
6971 return 0;
6972
6973 /* if we have an orom prevent invalid raid levels */
6974 if (orom)
6975 switch (level) {
6976 case 0: return imsm_orom_has_raid0(orom);
6977 case 1:
6978 if (raiddisks > 2)
6979 return imsm_orom_has_raid1e(orom);
6980 return imsm_orom_has_raid1(orom) && raiddisks == 2;
6981 case 10: return imsm_orom_has_raid10(orom) && raiddisks == 4;
6982 case 5: return imsm_orom_has_raid5(orom) && raiddisks > 2;
6983 }
6984 else
6985 return 1; /* not on an Intel RAID platform so anything goes */
6986
6987 return 0;
6988 }
6989
6990 static int
6991 active_arrays_by_format(char *name, char* hba, struct md_list **devlist,
6992 int dpa, int verbose)
6993 {
6994 struct mdstat_ent *mdstat = mdstat_read(0, 0);
6995 struct mdstat_ent *memb;
6996 int count = 0;
6997 int num = 0;
6998 struct md_list *dv;
6999 int found;
7000
7001 for (memb = mdstat ; memb ; memb = memb->next) {
7002 if (memb->metadata_version &&
7003 (strncmp(memb->metadata_version, "external:", 9) == 0) &&
7004 (strcmp(&memb->metadata_version[9], name) == 0) &&
7005 !is_subarray(memb->metadata_version+9) &&
7006 memb->members) {
7007 struct dev_member *dev = memb->members;
7008 int fd = -1;
7009 while (dev && !is_fd_valid(fd)) {
7010 char *path = xmalloc(strlen(dev->name) + strlen("/dev/") + 1);
7011 num = snprintf(path, PATH_MAX, "%s%s", "/dev/", dev->name);
7012 if (num > 0)
7013 fd = open(path, O_RDONLY, 0);
7014 if (num <= 0 || !is_fd_valid(fd)) {
7015 pr_vrb("Cannot open %s: %s\n",
7016 dev->name, strerror(errno));
7017 }
7018 free(path);
7019 dev = dev->next;
7020 }
7021 found = 0;
7022 if (is_fd_valid(fd) && disk_attached_to_hba(fd, hba)) {
7023 struct mdstat_ent *vol;
7024 for (vol = mdstat ; vol ; vol = vol->next) {
7025 if (vol->active > 0 &&
7026 vol->metadata_version &&
7027 is_container_member(vol, memb->devnm)) {
7028 found++;
7029 count++;
7030 }
7031 }
7032 if (*devlist && (found < dpa)) {
7033 dv = xcalloc(1, sizeof(*dv));
7034 dv->devname = xmalloc(strlen(memb->devnm) + strlen("/dev/") + 1);
7035 sprintf(dv->devname, "%s%s", "/dev/", memb->devnm);
7036 dv->found = found;
7037 dv->used = 0;
7038 dv->next = *devlist;
7039 *devlist = dv;
7040 }
7041 }
7042 close_fd(&fd);
7043 }
7044 }
7045 free_mdstat(mdstat);
7046 return count;
7047 }
7048
7049 #ifdef DEBUG_LOOP
7050 static struct md_list*
7051 get_loop_devices(void)
7052 {
7053 int i;
7054 struct md_list *devlist = NULL;
7055 struct md_list *dv;
7056
7057 for(i = 0; i < 12; i++) {
7058 dv = xcalloc(1, sizeof(*dv));
7059 dv->devname = xmalloc(40);
7060 sprintf(dv->devname, "/dev/loop%d", i);
7061 dv->next = devlist;
7062 devlist = dv;
7063 }
7064 return devlist;
7065 }
7066 #endif
7067
7068 static struct md_list*
7069 get_devices(const char *hba_path)
7070 {
7071 struct md_list *devlist = NULL;
7072 struct md_list *dv;
7073 struct dirent *ent;
7074 DIR *dir;
7075 int err = 0;
7076
7077 #if DEBUG_LOOP
7078 devlist = get_loop_devices();
7079 return devlist;
7080 #endif
7081 /* scroll through /sys/dev/block looking for devices attached to
7082 * this hba
7083 */
7084 dir = opendir("/sys/dev/block");
7085 for (ent = dir ? readdir(dir) : NULL; ent; ent = readdir(dir)) {
7086 int fd;
7087 char buf[1024];
7088 int major, minor;
7089 char *path = NULL;
7090 if (sscanf(ent->d_name, "%d:%d", &major, &minor) != 2)
7091 continue;
7092 path = devt_to_devpath(makedev(major, minor), 1, NULL);
7093 if (!path)
7094 continue;
7095 if (!path_attached_to_hba(path, hba_path)) {
7096 free(path);
7097 path = NULL;
7098 continue;
7099 }
7100 free(path);
7101 path = NULL;
7102 fd = dev_open(ent->d_name, O_RDONLY);
7103 if (is_fd_valid(fd)) {
7104 fd2devname(fd, buf);
7105 close(fd);
7106 } else {
7107 pr_err("cannot open device: %s\n",
7108 ent->d_name);
7109 continue;
7110 }
7111
7112 dv = xcalloc(1, sizeof(*dv));
7113 dv->devname = xstrdup(buf);
7114 dv->next = devlist;
7115 devlist = dv;
7116 }
7117 if (err) {
7118 while(devlist) {
7119 dv = devlist;
7120 devlist = devlist->next;
7121 free(dv->devname);
7122 free(dv);
7123 }
7124 }
7125 closedir(dir);
7126 return devlist;
7127 }
7128
7129 static int
7130 count_volumes_list(struct md_list *devlist, char *homehost,
7131 int verbose, int *found)
7132 {
7133 struct md_list *tmpdev;
7134 int count = 0;
7135 struct supertype *st;
7136
7137 /* first walk the list of devices to find a consistent set
7138 * that match the criterea, if that is possible.
7139 * We flag the ones we like with 'used'.
7140 */
7141 *found = 0;
7142 st = match_metadata_desc_imsm("imsm");
7143 if (st == NULL) {
7144 pr_vrb("cannot allocate memory for imsm supertype\n");
7145 return 0;
7146 }
7147
7148 for (tmpdev = devlist; tmpdev; tmpdev = tmpdev->next) {
7149 char *devname = tmpdev->devname;
7150 dev_t rdev;
7151 struct supertype *tst;
7152 int dfd;
7153 if (tmpdev->used > 1)
7154 continue;
7155 tst = dup_super(st);
7156 if (tst == NULL) {
7157 pr_vrb("cannot allocate memory for imsm supertype\n");
7158 goto err_1;
7159 }
7160 tmpdev->container = 0;
7161 dfd = dev_open(devname, O_RDONLY|O_EXCL);
7162 if (!is_fd_valid(dfd)) {
7163 dprintf("cannot open device %s: %s\n",
7164 devname, strerror(errno));
7165 tmpdev->used = 2;
7166 } else if (!fstat_is_blkdev(dfd, devname, &rdev)) {
7167 tmpdev->used = 2;
7168 } else if (must_be_container(dfd)) {
7169 struct supertype *cst;
7170 cst = super_by_fd(dfd, NULL);
7171 if (cst == NULL) {
7172 dprintf("cannot recognize container type %s\n",
7173 devname);
7174 tmpdev->used = 2;
7175 } else if (tst->ss != st->ss) {
7176 dprintf("non-imsm container - ignore it: %s\n",
7177 devname);
7178 tmpdev->used = 2;
7179 } else if (!tst->ss->load_container ||
7180 tst->ss->load_container(tst, dfd, NULL))
7181 tmpdev->used = 2;
7182 else {
7183 tmpdev->container = 1;
7184 }
7185 if (cst)
7186 cst->ss->free_super(cst);
7187 } else {
7188 tmpdev->st_rdev = rdev;
7189 if (tst->ss->load_super(tst,dfd, NULL)) {
7190 dprintf("no RAID superblock on %s\n",
7191 devname);
7192 tmpdev->used = 2;
7193 } else if (tst->ss->compare_super == NULL) {
7194 dprintf("Cannot assemble %s metadata on %s\n",
7195 tst->ss->name, devname);
7196 tmpdev->used = 2;
7197 }
7198 }
7199 close_fd(&dfd);
7200
7201 if (tmpdev->used == 2 || tmpdev->used == 4) {
7202 /* Ignore unrecognised devices during auto-assembly */
7203 goto loop;
7204 }
7205 else {
7206 struct mdinfo info;
7207 tst->ss->getinfo_super(tst, &info, NULL);
7208
7209 if (st->minor_version == -1)
7210 st->minor_version = tst->minor_version;
7211
7212 if (memcmp(info.uuid, uuid_zero,
7213 sizeof(int[4])) == 0) {
7214 /* this is a floating spare. It cannot define
7215 * an array unless there are no more arrays of
7216 * this type to be found. It can be included
7217 * in an array of this type though.
7218 */
7219 tmpdev->used = 3;
7220 goto loop;
7221 }
7222
7223 if (st->ss != tst->ss ||
7224 st->minor_version != tst->minor_version ||
7225 st->ss->compare_super(st, tst, 1) != 0) {
7226 /* Some mismatch. If exactly one array matches this host,
7227 * we can resolve on that one.
7228 * Or, if we are auto assembling, we just ignore the second
7229 * for now.
7230 */
7231 dprintf("superblock on %s doesn't match others - assembly aborted\n",
7232 devname);
7233 goto loop;
7234 }
7235 tmpdev->used = 1;
7236 *found = 1;
7237 dprintf("found: devname: %s\n", devname);
7238 }
7239 loop:
7240 if (tst)
7241 tst->ss->free_super(tst);
7242 }
7243 if (*found != 0) {
7244 int err;
7245 if ((err = load_super_imsm_all(st, -1, &st->sb, NULL, devlist, 0)) == 0) {
7246 struct mdinfo *iter, *head = st->ss->container_content(st, NULL);
7247 for (iter = head; iter; iter = iter->next) {
7248 dprintf("content->text_version: %s vol\n",
7249 iter->text_version);
7250 if (iter->array.state & (1<<MD_SB_BLOCK_VOLUME)) {
7251 /* do not assemble arrays with unsupported
7252 configurations */
7253 dprintf("Cannot activate member %s.\n",
7254 iter->text_version);
7255 } else
7256 count++;
7257 }
7258 sysfs_free(head);
7259
7260 } else {
7261 dprintf("No valid super block on device list: err: %d %p\n",
7262 err, st->sb);
7263 }
7264 } else {
7265 dprintf("no more devices to examine\n");
7266 }
7267
7268 for (tmpdev = devlist; tmpdev; tmpdev = tmpdev->next) {
7269 if (tmpdev->used == 1 && tmpdev->found) {
7270 if (count) {
7271 if (count < tmpdev->found)
7272 count = 0;
7273 else
7274 count -= tmpdev->found;
7275 }
7276 }
7277 if (tmpdev->used == 1)
7278 tmpdev->used = 4;
7279 }
7280 err_1:
7281 if (st)
7282 st->ss->free_super(st);
7283 return count;
7284 }
7285
7286 static int __count_volumes(char *hba_path, int dpa, int verbose,
7287 int cmp_hba_path)
7288 {
7289 struct sys_dev *idev, *intel_devices = find_intel_devices();
7290 int count = 0;
7291 const struct orom_entry *entry;
7292 struct devid_list *dv, *devid_list;
7293
7294 if (!hba_path)
7295 return 0;
7296
7297 for (idev = intel_devices; idev; idev = idev->next) {
7298 if (strstr(idev->path, hba_path))
7299 break;
7300 }
7301
7302 if (!idev || !idev->dev_id)
7303 return 0;
7304
7305 entry = get_orom_entry_by_device_id(idev->dev_id);
7306
7307 if (!entry || !entry->devid_list)
7308 return 0;
7309
7310 devid_list = entry->devid_list;
7311 for (dv = devid_list; dv; dv = dv->next) {
7312 struct md_list *devlist;
7313 struct sys_dev *device = NULL;
7314 char *hpath;
7315 int found = 0;
7316
7317 if (cmp_hba_path)
7318 device = device_by_id_and_path(dv->devid, hba_path);
7319 else
7320 device = device_by_id(dv->devid);
7321
7322 if (device)
7323 hpath = device->path;
7324 else
7325 return 0;
7326
7327 devlist = get_devices(hpath);
7328 /* if no intel devices return zero volumes */
7329 if (devlist == NULL)
7330 return 0;
7331
7332 count += active_arrays_by_format("imsm", hpath, &devlist, dpa,
7333 verbose);
7334 dprintf("path: %s active arrays: %d\n", hpath, count);
7335 if (devlist == NULL)
7336 return 0;
7337 do {
7338 found = 0;
7339 count += count_volumes_list(devlist,
7340 NULL,
7341 verbose,
7342 &found);
7343 dprintf("found %d count: %d\n", found, count);
7344 } while (found);
7345
7346 dprintf("path: %s total number of volumes: %d\n", hpath, count);
7347
7348 while (devlist) {
7349 struct md_list *dv = devlist;
7350 devlist = devlist->next;
7351 free(dv->devname);
7352 free(dv);
7353 }
7354 }
7355 return count;
7356 }
7357
7358 static int count_volumes(struct intel_hba *hba, int dpa, int verbose)
7359 {
7360 if (!hba)
7361 return 0;
7362 if (hba->type == SYS_DEV_VMD) {
7363 struct sys_dev *dev;
7364 int count = 0;
7365
7366 for (dev = find_intel_devices(); dev; dev = dev->next) {
7367 if (dev->type == SYS_DEV_VMD)
7368 count += __count_volumes(dev->path, dpa,
7369 verbose, 1);
7370 }
7371 return count;
7372 }
7373 return __count_volumes(hba->path, dpa, verbose, 0);
7374 }
7375
7376 static int imsm_default_chunk(const struct imsm_orom *orom)
7377 {
7378 /* up to 512 if the plaform supports it, otherwise the platform max.
7379 * 128 if no platform detected
7380 */
7381 int fs = max(7, orom ? fls(orom->sss) : 0);
7382
7383 return min(512, (1 << fs));
7384 }
7385
7386 static int
7387 validate_geometry_imsm_orom(struct intel_super *super, int level, int layout,
7388 int raiddisks, int *chunk, unsigned long long size, int verbose)
7389 {
7390 /* check/set platform and metadata limits/defaults */
7391 if (super->orom && raiddisks > super->orom->dpa) {
7392 pr_vrb("platform supports a maximum of %d disks per array\n",
7393 super->orom->dpa);
7394 return 0;
7395 }
7396
7397 /* capabilities of OROM tested - copied from validate_geometry_imsm_volume */
7398 if (!is_raid_level_supported(super->orom, level, raiddisks)) {
7399 pr_vrb("platform does not support raid%d with %d disk%s\n",
7400 level, raiddisks, raiddisks > 1 ? "s" : "");
7401 return 0;
7402 }
7403
7404 if (*chunk == 0 || *chunk == UnSet)
7405 *chunk = imsm_default_chunk(super->orom);
7406
7407 if (super->orom && !imsm_orom_has_chunk(super->orom, *chunk)) {
7408 pr_vrb("platform does not support a chunk size of: %d\n", *chunk);
7409 return 0;
7410 }
7411
7412 if (layout != imsm_level_to_layout(level)) {
7413 if (level == 5)
7414 pr_vrb("imsm raid 5 only supports the left-asymmetric layout\n");
7415 else if (level == 10)
7416 pr_vrb("imsm raid 10 only supports the n2 layout\n");
7417 else
7418 pr_vrb("imsm unknown layout %#x for this raid level %d\n",
7419 layout, level);
7420 return 0;
7421 }
7422
7423 if (super->orom && (super->orom->attr & IMSM_OROM_ATTR_2TB) == 0 &&
7424 (calc_array_size(level, raiddisks, layout, *chunk, size) >> 32) > 0) {
7425 pr_vrb("platform does not support a volume size over 2TB\n");
7426 return 0;
7427 }
7428
7429 return 1;
7430 }
7431
7432 /* validate_geometry_imsm_volume - lifted from validate_geometry_ddf_bvd
7433 * FIX ME add ahci details
7434 */
7435 static int validate_geometry_imsm_volume(struct supertype *st, int level,
7436 int layout, int raiddisks, int *chunk,
7437 unsigned long long size,
7438 unsigned long long data_offset,
7439 char *dev,
7440 unsigned long long *freesize,
7441 int verbose)
7442 {
7443 dev_t rdev;
7444 struct intel_super *super = st->sb;
7445 struct imsm_super *mpb;
7446 struct dl *dl;
7447 unsigned long long pos = 0;
7448 unsigned long long maxsize;
7449 struct extent *e;
7450 int i;
7451
7452 /* We must have the container info already read in. */
7453 if (!super)
7454 return 0;
7455
7456 mpb = super->anchor;
7457
7458 if (!validate_geometry_imsm_orom(super, level, layout, raiddisks, chunk, size, verbose)) {
7459 pr_err("RAID geometry validation failed. Cannot proceed with the action(s).\n");
7460 return 0;
7461 }
7462 if (!dev) {
7463 /* General test: make sure there is space for
7464 * 'raiddisks' device extents of size 'size' at a given
7465 * offset
7466 */
7467 unsigned long long minsize = size;
7468 unsigned long long start_offset = MaxSector;
7469 int dcnt = 0;
7470 if (minsize == 0)
7471 minsize = MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS;
7472 for (dl = super->disks; dl ; dl = dl->next) {
7473 int found = 0;
7474
7475 pos = 0;
7476 i = 0;
7477 e = get_extents(super, dl, 0);
7478 if (!e) continue;
7479 do {
7480 unsigned long long esize;
7481 esize = e[i].start - pos;
7482 if (esize >= minsize)
7483 found = 1;
7484 if (found && start_offset == MaxSector) {
7485 start_offset = pos;
7486 break;
7487 } else if (found && pos != start_offset) {
7488 found = 0;
7489 break;
7490 }
7491 pos = e[i].start + e[i].size;
7492 i++;
7493 } while (e[i-1].size);
7494 if (found)
7495 dcnt++;
7496 free(e);
7497 }
7498 if (dcnt < raiddisks) {
7499 if (verbose)
7500 pr_err("imsm: Not enough devices with space for this array (%d < %d)\n",
7501 dcnt, raiddisks);
7502 return 0;
7503 }
7504 return 1;
7505 }
7506
7507 /* This device must be a member of the set */
7508 if (!stat_is_blkdev(dev, &rdev))
7509 return 0;
7510 for (dl = super->disks ; dl ; dl = dl->next) {
7511 if (dl->major == (int)major(rdev) &&
7512 dl->minor == (int)minor(rdev))
7513 break;
7514 }
7515 if (!dl) {
7516 if (verbose)
7517 pr_err("%s is not in the same imsm set\n", dev);
7518 return 0;
7519 } else if (super->orom && dl->index < 0 && mpb->num_raid_devs) {
7520 /* If a volume is present then the current creation attempt
7521 * cannot incorporate new spares because the orom may not
7522 * understand this configuration (all member disks must be
7523 * members of each array in the container).
7524 */
7525 pr_err("%s is a spare and a volume is already defined for this container\n", dev);
7526 pr_err("The option-rom requires all member disks to be a member of all volumes\n");
7527 return 0;
7528 } else if (super->orom && mpb->num_raid_devs > 0 &&
7529 mpb->num_disks != raiddisks) {
7530 pr_err("The option-rom requires all member disks to be a member of all volumes\n");
7531 return 0;
7532 }
7533
7534 /* retrieve the largest free space block */
7535 e = get_extents(super, dl, 0);
7536 maxsize = 0;
7537 i = 0;
7538 if (e) {
7539 do {
7540 unsigned long long esize;
7541
7542 esize = e[i].start - pos;
7543 if (esize >= maxsize)
7544 maxsize = esize;
7545 pos = e[i].start + e[i].size;
7546 i++;
7547 } while (e[i-1].size);
7548 dl->e = e;
7549 dl->extent_cnt = i;
7550 } else {
7551 if (verbose)
7552 pr_err("unable to determine free space for: %s\n",
7553 dev);
7554 return 0;
7555 }
7556 if (maxsize < size) {
7557 if (verbose)
7558 pr_err("%s not enough space (%llu < %llu)\n",
7559 dev, maxsize, size);
7560 return 0;
7561 }
7562
7563 maxsize = merge_extents(super, false);
7564
7565 if (mpb->num_raid_devs > 0 && size && size != maxsize)
7566 pr_err("attempting to create a second volume with size less then remaining space.\n");
7567
7568 if (maxsize < size || maxsize == 0) {
7569 if (verbose) {
7570 if (maxsize == 0)
7571 pr_err("no free space left on device. Aborting...\n");
7572 else
7573 pr_err("not enough space to create volume of given size (%llu < %llu). Aborting...\n",
7574 maxsize, size);
7575 }
7576 return 0;
7577 }
7578
7579 *freesize = maxsize;
7580
7581 if (super->orom) {
7582 int count = count_volumes(super->hba,
7583 super->orom->dpa, verbose);
7584 if (super->orom->vphba <= count) {
7585 pr_vrb("platform does not support more than %d raid volumes.\n",
7586 super->orom->vphba);
7587 return 0;
7588 }
7589 }
7590 return 1;
7591 }
7592
7593 /**
7594 * imsm_get_free_size() - get the biggest, common free space from members.
7595 * @super: &intel_super pointer, not NULL.
7596 * @raiddisks: number of raid disks.
7597 * @size: requested size, could be 0 (means max size).
7598 * @chunk: requested chunk size in KiB.
7599 * @freesize: pointer for returned size value.
7600 *
7601 * Return: &IMSM_STATUS_OK or &IMSM_STATUS_ERROR.
7602 *
7603 * @freesize is set to meaningful value, this can be @size, or calculated
7604 * max free size.
7605 * super->create_offset value is modified and set appropriately in
7606 * merge_extends() for further creation.
7607 */
7608 static imsm_status_t imsm_get_free_size(struct intel_super *super,
7609 const int raiddisks,
7610 unsigned long long size,
7611 const int chunk,
7612 unsigned long long *freesize,
7613 bool expanding)
7614 {
7615 struct imsm_super *mpb = super->anchor;
7616 struct dl *dl;
7617 int i;
7618 struct extent *e;
7619 int cnt = 0;
7620 int used = 0;
7621 unsigned long long maxsize;
7622 unsigned long long minsize = size;
7623
7624 if (minsize == 0)
7625 minsize = chunk * 2;
7626
7627 /* find the largest common start free region of the possible disks */
7628 for (dl = super->disks; dl; dl = dl->next) {
7629 dl->raiddisk = -1;
7630
7631 if (dl->index >= 0)
7632 used++;
7633
7634 /* don't activate new spares if we are orom constrained
7635 * and there is already a volume active in the container
7636 */
7637 if (super->orom && dl->index < 0 && mpb->num_raid_devs)
7638 continue;
7639
7640 e = get_extents(super, dl, 0);
7641 if (!e)
7642 continue;
7643 for (i = 1; e[i-1].size; i++)
7644 ;
7645 dl->e = e;
7646 dl->extent_cnt = i;
7647 cnt++;
7648 }
7649
7650 maxsize = merge_extents(super, expanding);
7651 if (maxsize < minsize) {
7652 pr_err("imsm: Free space is %llu but must be equal or larger than %llu.\n",
7653 maxsize, minsize);
7654 return IMSM_STATUS_ERROR;
7655 }
7656
7657 if (cnt < raiddisks || (super->orom && used && used != raiddisks)) {
7658 pr_err("imsm: Not enough devices with space to create array.\n");
7659 return IMSM_STATUS_ERROR;
7660 }
7661
7662 if (size == 0) {
7663 size = maxsize;
7664 if (chunk) {
7665 size /= 2 * chunk;
7666 size *= 2 * chunk;
7667 }
7668 maxsize = size;
7669 }
7670 if (mpb->num_raid_devs > 0 && size && size != maxsize)
7671 pr_err("attempting to create a second volume with size less then remaining space.\n");
7672 *freesize = size;
7673
7674 dprintf("imsm: imsm_get_free_size() returns : %llu\n", size);
7675
7676 return IMSM_STATUS_OK;
7677 }
7678
7679 /**
7680 * autolayout_imsm() - automatically layout a new volume.
7681 * @super: &intel_super pointer, not NULL.
7682 * @raiddisks: number of raid disks.
7683 * @size: requested size, could be 0 (means max size).
7684 * @chunk: requested chunk.
7685 * @freesize: pointer for returned size value.
7686 *
7687 * We are being asked to automatically layout a new volume based on the current
7688 * contents of the container. If the parameters can be satisfied autolayout_imsm
7689 * will record the disks, start offset, and will return size of the volume to
7690 * be created. See imsm_get_free_size() for details.
7691 * add_to_super() and getinfo_super() detect when autolayout is in progress.
7692 * If first volume exists, slots are set consistently to it.
7693 *
7694 * Return: &IMSM_STATUS_OK on success, &IMSM_STATUS_ERROR otherwise.
7695 *
7696 * Disks are marked for creation via dl->raiddisk.
7697 */
7698 static imsm_status_t autolayout_imsm(struct intel_super *super,
7699 const int raiddisks,
7700 unsigned long long size, const int chunk,
7701 unsigned long long *freesize)
7702 {
7703 int curr_slot = 0;
7704 struct dl *disk;
7705 int vol_cnt = super->anchor->num_raid_devs;
7706 imsm_status_t rv;
7707
7708 rv = imsm_get_free_size(super, raiddisks, size, chunk, freesize, false);
7709 if (rv != IMSM_STATUS_OK)
7710 return IMSM_STATUS_ERROR;
7711
7712 for (disk = super->disks; disk; disk = disk->next) {
7713 if (!disk->e)
7714 continue;
7715
7716 if (curr_slot == raiddisks)
7717 break;
7718
7719 if (vol_cnt == 0) {
7720 disk->raiddisk = curr_slot;
7721 } else {
7722 int _slot = get_disk_slot_in_dev(super, 0, disk->index);
7723
7724 if (_slot == -1) {
7725 pr_err("Disk %s is not used in first volume, aborting\n",
7726 disk->devname);
7727 return IMSM_STATUS_ERROR;
7728 }
7729 disk->raiddisk = _slot;
7730 }
7731 curr_slot++;
7732 }
7733
7734 return IMSM_STATUS_OK;
7735 }
7736
7737 static int validate_geometry_imsm(struct supertype *st, int level, int layout,
7738 int raiddisks, int *chunk, unsigned long long size,
7739 unsigned long long data_offset,
7740 char *dev, unsigned long long *freesize,
7741 int consistency_policy, int verbose)
7742 {
7743 int fd, cfd;
7744 struct mdinfo *sra;
7745 int is_member = 0;
7746
7747 /* load capability
7748 * if given unused devices create a container
7749 * if given given devices in a container create a member volume
7750 */
7751 if (is_container(level))
7752 /* Must be a fresh device to add to a container */
7753 return validate_geometry_imsm_container(st, level, raiddisks,
7754 data_offset, dev,
7755 freesize, verbose);
7756
7757 /*
7758 * Size is given in sectors.
7759 */
7760 if (size && (size < 2048)) {
7761 pr_err("Given size must be greater than 1M.\n");
7762 /* Depends on algorithm in Create.c :
7763 * if container was given (dev == NULL) return -1,
7764 * if block device was given ( dev != NULL) return 0.
7765 */
7766 return dev ? -1 : 0;
7767 }
7768
7769 if (!dev) {
7770 struct intel_super *super = st->sb;
7771
7772 /*
7773 * Autolayout mode, st->sb must be set.
7774 */
7775 if (!super) {
7776 pr_vrb("superblock must be set for autolayout, aborting\n");
7777 return 0;
7778 }
7779
7780 if (!validate_geometry_imsm_orom(st->sb, level, layout,
7781 raiddisks, chunk, size,
7782 verbose))
7783 return 0;
7784
7785 if (super->orom && freesize) {
7786 imsm_status_t rv;
7787 int count = count_volumes(super->hba, super->orom->dpa,
7788 verbose);
7789 if (super->orom->vphba <= count) {
7790 pr_vrb("platform does not support more than %d raid volumes.\n",
7791 super->orom->vphba);
7792 return 0;
7793 }
7794
7795 rv = autolayout_imsm(super, raiddisks, size, *chunk,
7796 freesize);
7797 if (rv != IMSM_STATUS_OK)
7798 return 0;
7799 }
7800 return 1;
7801 }
7802 if (st->sb) {
7803 /* creating in a given container */
7804 return validate_geometry_imsm_volume(st, level, layout,
7805 raiddisks, chunk, size,
7806 data_offset,
7807 dev, freesize, verbose);
7808 }
7809
7810 /* This device needs to be a device in an 'imsm' container */
7811 fd = open(dev, O_RDONLY|O_EXCL, 0);
7812
7813 if (is_fd_valid(fd)) {
7814 pr_vrb("Cannot create this array on device %s\n", dev);
7815 close(fd);
7816 return 0;
7817 }
7818 if (errno == EBUSY)
7819 fd = open(dev, O_RDONLY, 0);
7820
7821 if (!is_fd_valid(fd)) {
7822 pr_vrb("Cannot open %s: %s\n", dev, strerror(errno));
7823 return 0;
7824 }
7825
7826 /* Well, it is in use by someone, maybe an 'imsm' container. */
7827 cfd = open_container(fd);
7828 close_fd(&fd);
7829
7830 if (!is_fd_valid(cfd)) {
7831 pr_vrb("Cannot use %s: It is busy\n", dev);
7832 return 0;
7833 }
7834 sra = sysfs_read(cfd, NULL, GET_VERSION);
7835 if (sra && sra->array.major_version == -1 &&
7836 strcmp(sra->text_version, "imsm") == 0)
7837 is_member = 1;
7838 sysfs_free(sra);
7839 if (is_member) {
7840 /* This is a member of a imsm container. Load the container
7841 * and try to create a volume
7842 */
7843 struct intel_super *super;
7844
7845 if (load_super_imsm_all(st, cfd, (void **) &super, NULL, NULL, 1) == 0) {
7846 st->sb = super;
7847 strcpy(st->container_devnm, fd2devnm(cfd));
7848 close(cfd);
7849 return validate_geometry_imsm_volume(st, level, layout,
7850 raiddisks, chunk,
7851 size, data_offset, dev,
7852 freesize, 1)
7853 ? 1 : -1;
7854 }
7855 }
7856
7857 if (verbose)
7858 pr_err("failed container membership check\n");
7859
7860 close(cfd);
7861 return 0;
7862 }
7863
7864 static void default_geometry_imsm(struct supertype *st, int *level, int *layout, int *chunk)
7865 {
7866 struct intel_super *super = st->sb;
7867
7868 if (level && *level == UnSet)
7869 *level = LEVEL_CONTAINER;
7870
7871 if (level && layout && *layout == UnSet)
7872 *layout = imsm_level_to_layout(*level);
7873
7874 if (chunk && (*chunk == UnSet || *chunk == 0))
7875 *chunk = imsm_default_chunk(super->orom);
7876 }
7877
7878 static void handle_missing(struct intel_super *super, struct imsm_dev *dev);
7879
7880 static int kill_subarray_imsm(struct supertype *st, char *subarray_id)
7881 {
7882 /* remove the subarray currently referenced by subarray_id */
7883 __u8 i;
7884 struct intel_dev **dp;
7885 struct intel_super *super = st->sb;
7886 __u8 current_vol = strtoul(subarray_id, NULL, 10);
7887 struct imsm_super *mpb = super->anchor;
7888
7889 if (mpb->num_raid_devs == 0)
7890 return 2;
7891
7892 /* block deletions that would change the uuid of active subarrays
7893 *
7894 * FIXME when immutable ids are available, but note that we'll
7895 * also need to fixup the invalidated/active subarray indexes in
7896 * mdstat
7897 */
7898 for (i = 0; i < mpb->num_raid_devs; i++) {
7899 char subarray[4];
7900
7901 if (i < current_vol)
7902 continue;
7903 snprintf(subarray, sizeof(subarray), "%u", i);
7904 if (is_subarray_active(subarray, st->devnm)) {
7905 pr_err("deleting subarray-%d would change the UUID of active subarray-%d, aborting\n",
7906 current_vol, i);
7907
7908 return 2;
7909 }
7910 }
7911
7912 if (st->update_tail) {
7913 struct imsm_update_kill_array *u = xmalloc(sizeof(*u));
7914
7915 u->type = update_kill_array;
7916 u->dev_idx = current_vol;
7917 append_metadata_update(st, u, sizeof(*u));
7918
7919 return 0;
7920 }
7921
7922 for (dp = &super->devlist; *dp;)
7923 if ((*dp)->index == current_vol) {
7924 *dp = (*dp)->next;
7925 } else {
7926 handle_missing(super, (*dp)->dev);
7927 if ((*dp)->index > current_vol)
7928 (*dp)->index--;
7929 dp = &(*dp)->next;
7930 }
7931
7932 /* no more raid devices, all active components are now spares,
7933 * but of course failed are still failed
7934 */
7935 if (--mpb->num_raid_devs == 0) {
7936 struct dl *d;
7937
7938 for (d = super->disks; d; d = d->next)
7939 if (d->index > -2)
7940 mark_spare(d);
7941 }
7942
7943 super->updates_pending++;
7944
7945 return 0;
7946 }
7947
7948 /**
7949 * get_rwh_policy_from_update() - Get the rwh policy for update option.
7950 * @update: Update option.
7951 */
7952 static int get_rwh_policy_from_update(enum update_opt update)
7953 {
7954 switch (update) {
7955 case UOPT_PPL:
7956 return RWH_MULTIPLE_DISTRIBUTED;
7957 case UOPT_NO_PPL:
7958 return RWH_MULTIPLE_OFF;
7959 case UOPT_BITMAP:
7960 return RWH_BITMAP;
7961 case UOPT_NO_BITMAP:
7962 return RWH_OFF;
7963 default:
7964 break;
7965 }
7966 return UOPT_UNDEFINED;
7967 }
7968
7969 static int update_subarray_imsm(struct supertype *st, char *subarray,
7970 enum update_opt update, struct mddev_ident *ident)
7971 {
7972 /* update the subarray currently referenced by ->current_vol */
7973 struct intel_super *super = st->sb;
7974 struct imsm_super *mpb = super->anchor;
7975
7976 if (update == UOPT_NAME) {
7977 char *name = ident->name;
7978 char *ep;
7979 int vol;
7980
7981 if (imsm_is_name_allowed(super, name, 1) == false)
7982 return 2;
7983
7984 vol = strtoul(subarray, &ep, 10);
7985 if (*ep != '\0' || vol >= super->anchor->num_raid_devs)
7986 return 2;
7987
7988 if (st->update_tail) {
7989 struct imsm_update_rename_array *u = xmalloc(sizeof(*u));
7990
7991 u->type = update_rename_array;
7992 u->dev_idx = vol;
7993 strncpy((char *) u->name, name, MAX_RAID_SERIAL_LEN);
7994 u->name[MAX_RAID_SERIAL_LEN-1] = '\0';
7995 append_metadata_update(st, u, sizeof(*u));
7996 } else {
7997 struct imsm_dev *dev;
7998 int i, namelen;
7999
8000 dev = get_imsm_dev(super, vol);
8001 memset(dev->volume, '\0', MAX_RAID_SERIAL_LEN);
8002 namelen = min((int)strlen(name), MAX_RAID_SERIAL_LEN);
8003 memcpy(dev->volume, name, namelen);
8004 for (i = 0; i < mpb->num_raid_devs; i++) {
8005 dev = get_imsm_dev(super, i);
8006 handle_missing(super, dev);
8007 }
8008 super->updates_pending++;
8009 }
8010 } else if (get_rwh_policy_from_update(update) != UOPT_UNDEFINED) {
8011 int new_policy;
8012 char *ep;
8013 int vol = strtoul(subarray, &ep, 10);
8014
8015 if (*ep != '\0' || vol >= super->anchor->num_raid_devs)
8016 return 2;
8017
8018 new_policy = get_rwh_policy_from_update(update);
8019
8020 if (st->update_tail) {
8021 struct imsm_update_rwh_policy *u = xmalloc(sizeof(*u));
8022
8023 u->type = update_rwh_policy;
8024 u->dev_idx = vol;
8025 u->new_policy = new_policy;
8026 append_metadata_update(st, u, sizeof(*u));
8027 } else {
8028 struct imsm_dev *dev;
8029
8030 dev = get_imsm_dev(super, vol);
8031 dev->rwh_policy = new_policy;
8032 super->updates_pending++;
8033 }
8034 if (new_policy == RWH_BITMAP)
8035 return write_init_bitmap_imsm_vol(st, vol);
8036 } else
8037 return 2;
8038
8039 return 0;
8040 }
8041
8042 static bool is_gen_migration(struct imsm_dev *dev)
8043 {
8044 if (dev && dev->vol.migr_state &&
8045 migr_type(dev) == MIGR_GEN_MIGR)
8046 return true;
8047
8048 return false;
8049 }
8050
8051 static int is_rebuilding(struct imsm_dev *dev)
8052 {
8053 struct imsm_map *migr_map;
8054
8055 if (!dev->vol.migr_state)
8056 return 0;
8057
8058 if (migr_type(dev) != MIGR_REBUILD)
8059 return 0;
8060
8061 migr_map = get_imsm_map(dev, MAP_1);
8062
8063 if (migr_map->map_state == IMSM_T_STATE_DEGRADED)
8064 return 1;
8065 else
8066 return 0;
8067 }
8068
8069 static int is_initializing(struct imsm_dev *dev)
8070 {
8071 struct imsm_map *migr_map;
8072
8073 if (!dev->vol.migr_state)
8074 return 0;
8075
8076 if (migr_type(dev) != MIGR_INIT)
8077 return 0;
8078
8079 migr_map = get_imsm_map(dev, MAP_1);
8080
8081 if (migr_map->map_state == IMSM_T_STATE_UNINITIALIZED)
8082 return 1;
8083
8084 return 0;
8085 }
8086
8087 static void update_recovery_start(struct intel_super *super,
8088 struct imsm_dev *dev,
8089 struct mdinfo *array)
8090 {
8091 struct mdinfo *rebuild = NULL;
8092 struct mdinfo *d;
8093 __u32 units;
8094
8095 if (!is_rebuilding(dev))
8096 return;
8097
8098 /* Find the rebuild target, but punt on the dual rebuild case */
8099 for (d = array->devs; d; d = d->next)
8100 if (d->recovery_start == 0) {
8101 if (rebuild)
8102 return;
8103 rebuild = d;
8104 }
8105
8106 if (!rebuild) {
8107 /* (?) none of the disks are marked with
8108 * IMSM_ORD_REBUILD, so assume they are missing and the
8109 * disk_ord_tbl was not correctly updated
8110 */
8111 dprintf("failed to locate out-of-sync disk\n");
8112 return;
8113 }
8114
8115 units = vol_curr_migr_unit(dev);
8116 rebuild->recovery_start = units * blocks_per_migr_unit(super, dev);
8117 }
8118
8119 static int recover_backup_imsm(struct supertype *st, struct mdinfo *info);
8120
8121 static struct mdinfo *container_content_imsm(struct supertype *st, char *subarray)
8122 {
8123 /* Given a container loaded by load_super_imsm_all,
8124 * extract information about all the arrays into
8125 * an mdinfo tree.
8126 * If 'subarray' is given, just extract info about that array.
8127 *
8128 * For each imsm_dev create an mdinfo, fill it in,
8129 * then look for matching devices in super->disks
8130 * and create appropriate device mdinfo.
8131 */
8132 struct intel_super *super = st->sb;
8133 struct imsm_super *mpb = super->anchor;
8134 struct mdinfo *rest = NULL;
8135 unsigned int i;
8136 int sb_errors = 0;
8137 struct dl *d;
8138 int spare_disks = 0;
8139 int current_vol = super->current_vol;
8140
8141 /* do not assemble arrays when not all attributes are supported */
8142 if (imsm_check_attributes(mpb->attributes) == 0) {
8143 sb_errors = 1;
8144 pr_err("Unsupported attributes in IMSM metadata.Arrays activation is blocked.\n");
8145 }
8146
8147 /* count spare devices, not used in maps
8148 */
8149 for (d = super->disks; d; d = d->next)
8150 if (d->index == -1)
8151 spare_disks++;
8152
8153 for (i = 0; i < mpb->num_raid_devs; i++) {
8154 struct imsm_dev *dev;
8155 struct imsm_map *map;
8156 struct imsm_map *map2;
8157 struct mdinfo *this;
8158 int slot;
8159 int chunk;
8160 char *ep;
8161 int level;
8162
8163 if (subarray &&
8164 (i != strtoul(subarray, &ep, 10) || *ep != '\0'))
8165 continue;
8166
8167 dev = get_imsm_dev(super, i);
8168 map = get_imsm_map(dev, MAP_0);
8169 map2 = get_imsm_map(dev, MAP_1);
8170 level = get_imsm_raid_level(map);
8171
8172 /* do not publish arrays that are in the middle of an
8173 * unsupported migration
8174 */
8175 if (dev->vol.migr_state &&
8176 (migr_type(dev) == MIGR_STATE_CHANGE)) {
8177 pr_err("cannot assemble volume '%.16s': unsupported migration in progress\n",
8178 dev->volume);
8179 continue;
8180 }
8181 /* do not publish arrays that are not support by controller's
8182 * OROM/EFI
8183 */
8184
8185 this = xmalloc(sizeof(*this));
8186
8187 super->current_vol = i;
8188 getinfo_super_imsm_volume(st, this, NULL);
8189 this->next = rest;
8190 chunk = __le16_to_cpu(map->blocks_per_strip) >> 1;
8191 /* mdadm does not support all metadata features- set the bit in all arrays state */
8192 if (!validate_geometry_imsm_orom(super,
8193 level, /* RAID level */
8194 imsm_level_to_layout(level),
8195 map->num_members, /* raid disks */
8196 &chunk, imsm_dev_size(dev),
8197 1 /* verbose */)) {
8198 pr_err("IMSM RAID geometry validation failed. Array %s activation is blocked.\n",
8199 dev->volume);
8200 this->array.state |=
8201 (1<<MD_SB_BLOCK_CONTAINER_RESHAPE) |
8202 (1<<MD_SB_BLOCK_VOLUME);
8203 }
8204
8205 /* if array has bad blocks, set suitable bit in all arrays state */
8206 if (sb_errors)
8207 this->array.state |=
8208 (1<<MD_SB_BLOCK_CONTAINER_RESHAPE) |
8209 (1<<MD_SB_BLOCK_VOLUME);
8210
8211 for (slot = 0 ; slot < map->num_members; slot++) {
8212 unsigned long long recovery_start;
8213 struct mdinfo *info_d;
8214 struct dl *d;
8215 int idx;
8216 int skip;
8217 __u32 ord;
8218 int missing = 0;
8219
8220 skip = 0;
8221 idx = get_imsm_disk_idx(dev, slot, MAP_0);
8222 ord = get_imsm_ord_tbl_ent(dev, slot, MAP_X);
8223 for (d = super->disks; d ; d = d->next)
8224 if (d->index == idx)
8225 break;
8226
8227 recovery_start = MaxSector;
8228 if (d == NULL)
8229 skip = 1;
8230 if (d && is_failed(&d->disk))
8231 skip = 1;
8232 if (!skip && (ord & IMSM_ORD_REBUILD))
8233 recovery_start = 0;
8234 if (!(ord & IMSM_ORD_REBUILD))
8235 this->array.working_disks++;
8236 /*
8237 * if we skip some disks the array will be assmebled degraded;
8238 * reset resync start to avoid a dirty-degraded
8239 * situation when performing the intial sync
8240 */
8241 if (skip)
8242 missing++;
8243
8244 if (!(dev->vol.dirty & RAIDVOL_DIRTY)) {
8245 if ((!able_to_resync(level, missing) ||
8246 recovery_start == 0))
8247 this->resync_start = MaxSector;
8248 }
8249
8250 if (skip)
8251 continue;
8252
8253 info_d = xcalloc(1, sizeof(*info_d));
8254 info_d->next = this->devs;
8255 this->devs = info_d;
8256
8257 info_d->disk.number = d->index;
8258 info_d->disk.major = d->major;
8259 info_d->disk.minor = d->minor;
8260 info_d->disk.raid_disk = slot;
8261 info_d->recovery_start = recovery_start;
8262 if (map2) {
8263 if (slot < map2->num_members)
8264 info_d->disk.state = (1 << MD_DISK_ACTIVE);
8265 else
8266 this->array.spare_disks++;
8267 } else {
8268 if (slot < map->num_members)
8269 info_d->disk.state = (1 << MD_DISK_ACTIVE);
8270 else
8271 this->array.spare_disks++;
8272 }
8273
8274 info_d->events = __le32_to_cpu(mpb->generation_num);
8275 info_d->data_offset = pba_of_lba0(map);
8276 info_d->component_size = calc_component_size(map, dev);
8277
8278 if (map->raid_level == 5) {
8279 info_d->ppl_sector = this->ppl_sector;
8280 info_d->ppl_size = this->ppl_size;
8281 if (this->consistency_policy == CONSISTENCY_POLICY_PPL &&
8282 recovery_start == 0)
8283 this->resync_start = 0;
8284 }
8285
8286 info_d->bb.supported = 1;
8287 get_volume_badblocks(super->bbm_log, ord_to_idx(ord),
8288 info_d->data_offset,
8289 info_d->component_size,
8290 &info_d->bb);
8291 }
8292 /* now that the disk list is up-to-date fixup recovery_start */
8293 update_recovery_start(super, dev, this);
8294 this->array.spare_disks += spare_disks;
8295
8296 /* check for reshape */
8297 if (this->reshape_active == 1)
8298 recover_backup_imsm(st, this);
8299 rest = this;
8300 }
8301
8302 super->current_vol = current_vol;
8303 return rest;
8304 }
8305
8306 static __u8 imsm_check_degraded(struct intel_super *super, struct imsm_dev *dev,
8307 int failed, int look_in_map)
8308 {
8309 struct imsm_map *map;
8310
8311 map = get_imsm_map(dev, look_in_map);
8312
8313 if (!failed)
8314 return map->map_state == IMSM_T_STATE_UNINITIALIZED ?
8315 IMSM_T_STATE_UNINITIALIZED : IMSM_T_STATE_NORMAL;
8316
8317 switch (get_imsm_raid_level(map)) {
8318 case 0:
8319 return IMSM_T_STATE_FAILED;
8320 break;
8321 case 1:
8322 if (failed < map->num_members)
8323 return IMSM_T_STATE_DEGRADED;
8324 else
8325 return IMSM_T_STATE_FAILED;
8326 break;
8327 case 10:
8328 {
8329 /**
8330 * check to see if any mirrors have failed, otherwise we
8331 * are degraded. Even numbered slots are mirrored on
8332 * slot+1
8333 */
8334 int i;
8335 /* gcc -Os complains that this is unused */
8336 int insync = insync;
8337
8338 for (i = 0; i < map->num_members; i++) {
8339 __u32 ord = get_imsm_ord_tbl_ent(dev, i, MAP_X);
8340 int idx = ord_to_idx(ord);
8341 struct imsm_disk *disk;
8342
8343 /* reset the potential in-sync count on even-numbered
8344 * slots. num_copies is always 2 for imsm raid10
8345 */
8346 if ((i & 1) == 0)
8347 insync = 2;
8348
8349 disk = get_imsm_disk(super, idx);
8350 if (!disk || is_failed(disk) || ord & IMSM_ORD_REBUILD)
8351 insync--;
8352
8353 /* no in-sync disks left in this mirror the
8354 * array has failed
8355 */
8356 if (insync == 0)
8357 return IMSM_T_STATE_FAILED;
8358 }
8359
8360 return IMSM_T_STATE_DEGRADED;
8361 }
8362 case 5:
8363 if (failed < 2)
8364 return IMSM_T_STATE_DEGRADED;
8365 else
8366 return IMSM_T_STATE_FAILED;
8367 break;
8368 default:
8369 break;
8370 }
8371
8372 return map->map_state;
8373 }
8374
8375 static int imsm_count_failed(struct intel_super *super, struct imsm_dev *dev,
8376 int look_in_map)
8377 {
8378 int i;
8379 int failed = 0;
8380 struct imsm_disk *disk;
8381 struct imsm_map *map = get_imsm_map(dev, MAP_0);
8382 struct imsm_map *prev = get_imsm_map(dev, MAP_1);
8383 struct imsm_map *map_for_loop;
8384 __u32 ord;
8385 int idx;
8386 int idx_1;
8387
8388 /* at the beginning of migration we set IMSM_ORD_REBUILD on
8389 * disks that are being rebuilt. New failures are recorded to
8390 * map[0]. So we look through all the disks we started with and
8391 * see if any failures are still present, or if any new ones
8392 * have arrived
8393 */
8394 map_for_loop = map;
8395 if (prev && (map->num_members < prev->num_members))
8396 map_for_loop = prev;
8397
8398 for (i = 0; i < map_for_loop->num_members; i++) {
8399 idx_1 = -255;
8400 /* when MAP_X is passed both maps failures are counted
8401 */
8402 if (prev &&
8403 (look_in_map == MAP_1 || look_in_map == MAP_X) &&
8404 i < prev->num_members) {
8405 ord = __le32_to_cpu(prev->disk_ord_tbl[i]);
8406 idx_1 = ord_to_idx(ord);
8407
8408 disk = get_imsm_disk(super, idx_1);
8409 if (!disk || is_failed(disk) || ord & IMSM_ORD_REBUILD)
8410 failed++;
8411 }
8412 if ((look_in_map == MAP_0 || look_in_map == MAP_X) &&
8413 i < map->num_members) {
8414 ord = __le32_to_cpu(map->disk_ord_tbl[i]);
8415 idx = ord_to_idx(ord);
8416
8417 if (idx != idx_1) {
8418 disk = get_imsm_disk(super, idx);
8419 if (!disk || is_failed(disk) ||
8420 ord & IMSM_ORD_REBUILD)
8421 failed++;
8422 }
8423 }
8424 }
8425
8426 return failed;
8427 }
8428
8429 static int imsm_open_new(struct supertype *c, struct active_array *a,
8430 int inst)
8431 {
8432 struct intel_super *super = c->sb;
8433 struct imsm_super *mpb = super->anchor;
8434 struct imsm_update_prealloc_bb_mem u;
8435
8436 if (inst >= mpb->num_raid_devs) {
8437 pr_err("subarry index %d, out of range\n", inst);
8438 return -ENODEV;
8439 }
8440
8441 dprintf("imsm: open_new %d\n", inst);
8442 a->info.container_member = inst;
8443
8444 u.type = update_prealloc_badblocks_mem;
8445 imsm_update_metadata_locally(c, &u, sizeof(u));
8446
8447 return 0;
8448 }
8449
8450 static int is_resyncing(struct imsm_dev *dev)
8451 {
8452 struct imsm_map *migr_map;
8453
8454 if (!dev->vol.migr_state)
8455 return 0;
8456
8457 if (migr_type(dev) == MIGR_INIT ||
8458 migr_type(dev) == MIGR_REPAIR)
8459 return 1;
8460
8461 if (migr_type(dev) == MIGR_GEN_MIGR)
8462 return 0;
8463
8464 migr_map = get_imsm_map(dev, MAP_1);
8465
8466 if (migr_map->map_state == IMSM_T_STATE_NORMAL &&
8467 dev->vol.migr_type != MIGR_GEN_MIGR)
8468 return 1;
8469 else
8470 return 0;
8471 }
8472
8473 /* return true if we recorded new information */
8474 static int mark_failure(struct intel_super *super,
8475 struct imsm_dev *dev, struct imsm_disk *disk, int idx)
8476 {
8477 __u32 ord;
8478 int slot;
8479 struct imsm_map *map;
8480 char buf[MAX_RAID_SERIAL_LEN+3];
8481 unsigned int len, shift = 0;
8482
8483 /* new failures are always set in map[0] */
8484 map = get_imsm_map(dev, MAP_0);
8485
8486 slot = get_imsm_disk_slot(map, idx);
8487 if (slot < 0)
8488 return 0;
8489
8490 ord = __le32_to_cpu(map->disk_ord_tbl[slot]);
8491 if (is_failed(disk) && (ord & IMSM_ORD_REBUILD))
8492 return 0;
8493
8494 memcpy(buf, disk->serial, MAX_RAID_SERIAL_LEN);
8495 buf[MAX_RAID_SERIAL_LEN] = '\000';
8496 strcat(buf, ":0");
8497 if ((len = strlen(buf)) >= MAX_RAID_SERIAL_LEN)
8498 shift = len - MAX_RAID_SERIAL_LEN + 1;
8499 memcpy(disk->serial, &buf[shift], len + 1 - shift);
8500
8501 disk->status |= FAILED_DISK;
8502 set_imsm_ord_tbl_ent(map, slot, idx | IMSM_ORD_REBUILD);
8503 /* mark failures in second map if second map exists and this disk
8504 * in this slot.
8505 * This is valid for migration, initialization and rebuild
8506 */
8507 if (dev->vol.migr_state) {
8508 struct imsm_map *map2 = get_imsm_map(dev, MAP_1);
8509 int slot2 = get_imsm_disk_slot(map2, idx);
8510
8511 if (slot2 < map2->num_members && slot2 >= 0)
8512 set_imsm_ord_tbl_ent(map2, slot2,
8513 idx | IMSM_ORD_REBUILD);
8514 }
8515 if (map->failed_disk_num == 0xff ||
8516 (!is_rebuilding(dev) && map->failed_disk_num > slot))
8517 map->failed_disk_num = slot;
8518
8519 clear_disk_badblocks(super->bbm_log, ord_to_idx(ord));
8520
8521 return 1;
8522 }
8523
8524 static void mark_missing(struct intel_super *super,
8525 struct imsm_dev *dev, struct imsm_disk *disk, int idx)
8526 {
8527 mark_failure(super, dev, disk, idx);
8528
8529 if (disk->scsi_id == __cpu_to_le32(~(__u32)0))
8530 return;
8531
8532 disk->scsi_id = __cpu_to_le32(~(__u32)0);
8533 memmove(&disk->serial[0], &disk->serial[1], MAX_RAID_SERIAL_LEN - 1);
8534 }
8535
8536 static void handle_missing(struct intel_super *super, struct imsm_dev *dev)
8537 {
8538 struct dl *dl;
8539
8540 if (!super->missing)
8541 return;
8542
8543 /* When orom adds replacement for missing disk it does
8544 * not remove entry of missing disk, but just updates map with
8545 * new added disk. So it is not enough just to test if there is
8546 * any missing disk, we have to look if there are any failed disks
8547 * in map to stop migration */
8548
8549 dprintf("imsm: mark missing\n");
8550 /* end process for initialization and rebuild only
8551 */
8552 if (is_gen_migration(dev) == false) {
8553 int failed = imsm_count_failed(super, dev, MAP_0);
8554
8555 if (failed) {
8556 __u8 map_state;
8557 struct imsm_map *map = get_imsm_map(dev, MAP_0);
8558 struct imsm_map *map1;
8559 int i, ord, ord_map1;
8560 int rebuilt = 1;
8561
8562 for (i = 0; i < map->num_members; i++) {
8563 ord = get_imsm_ord_tbl_ent(dev, i, MAP_0);
8564 if (!(ord & IMSM_ORD_REBUILD))
8565 continue;
8566
8567 map1 = get_imsm_map(dev, MAP_1);
8568 if (!map1)
8569 continue;
8570
8571 ord_map1 = __le32_to_cpu(map1->disk_ord_tbl[i]);
8572 if (ord_map1 & IMSM_ORD_REBUILD)
8573 rebuilt = 0;
8574 }
8575
8576 if (rebuilt) {
8577 map_state = imsm_check_degraded(super, dev,
8578 failed, MAP_0);
8579 end_migration(dev, super, map_state);
8580 }
8581 }
8582 }
8583 for (dl = super->missing; dl; dl = dl->next)
8584 mark_missing(super, dev, &dl->disk, dl->index);
8585 super->updates_pending++;
8586 }
8587
8588 static unsigned long long imsm_set_array_size(struct imsm_dev *dev,
8589 long long new_size)
8590 {
8591 unsigned long long array_blocks;
8592 struct imsm_map *map = get_imsm_map(dev, MAP_0);
8593 int used_disks = imsm_num_data_members(map);
8594
8595 if (used_disks == 0) {
8596 /* when problems occures
8597 * return current array_blocks value
8598 */
8599 array_blocks = imsm_dev_size(dev);
8600
8601 return array_blocks;
8602 }
8603
8604 /* set array size in metadata
8605 */
8606 if (new_size <= 0)
8607 /* OLCE size change is caused by added disks
8608 */
8609 array_blocks = per_dev_array_size(map) * used_disks;
8610 else
8611 /* Online Volume Size Change
8612 * Using available free space
8613 */
8614 array_blocks = new_size;
8615
8616 array_blocks = round_size_to_mb(array_blocks, used_disks);
8617 set_imsm_dev_size(dev, array_blocks);
8618
8619 return array_blocks;
8620 }
8621
8622 static void imsm_set_disk(struct active_array *a, int n, int state);
8623
8624 static void imsm_progress_container_reshape(struct intel_super *super)
8625 {
8626 /* if no device has a migr_state, but some device has a
8627 * different number of members than the previous device, start
8628 * changing the number of devices in this device to match
8629 * previous.
8630 */
8631 struct imsm_super *mpb = super->anchor;
8632 int prev_disks = -1;
8633 int i;
8634 int copy_map_size;
8635
8636 for (i = 0; i < mpb->num_raid_devs; i++) {
8637 struct imsm_dev *dev = get_imsm_dev(super, i);
8638 struct imsm_map *map = get_imsm_map(dev, MAP_0);
8639 struct imsm_map *map2;
8640 int prev_num_members;
8641
8642 if (dev->vol.migr_state)
8643 return;
8644
8645 if (prev_disks == -1)
8646 prev_disks = map->num_members;
8647 if (prev_disks == map->num_members)
8648 continue;
8649
8650 /* OK, this array needs to enter reshape mode.
8651 * i.e it needs a migr_state
8652 */
8653
8654 copy_map_size = sizeof_imsm_map(map);
8655 prev_num_members = map->num_members;
8656 map->num_members = prev_disks;
8657 dev->vol.migr_state = 1;
8658 set_vol_curr_migr_unit(dev, 0);
8659 set_migr_type(dev, MIGR_GEN_MIGR);
8660 for (i = prev_num_members;
8661 i < map->num_members; i++)
8662 set_imsm_ord_tbl_ent(map, i, i);
8663 map2 = get_imsm_map(dev, MAP_1);
8664 /* Copy the current map */
8665 memcpy(map2, map, copy_map_size);
8666 map2->num_members = prev_num_members;
8667
8668 imsm_set_array_size(dev, -1);
8669 super->clean_migration_record_by_mdmon = 1;
8670 super->updates_pending++;
8671 }
8672 }
8673
8674 /* Handle dirty -> clean transititions, resync and reshape. Degraded and rebuild
8675 * states are handled in imsm_set_disk() with one exception, when a
8676 * resync is stopped due to a new failure this routine will set the
8677 * 'degraded' state for the array.
8678 */
8679 static int imsm_set_array_state(struct active_array *a, int consistent)
8680 {
8681 int inst = a->info.container_member;
8682 struct intel_super *super = a->container->sb;
8683 struct imsm_dev *dev = get_imsm_dev(super, inst);
8684 struct imsm_map *map = get_imsm_map(dev, MAP_0);
8685 int failed = imsm_count_failed(super, dev, MAP_0);
8686 __u8 map_state = imsm_check_degraded(super, dev, failed, MAP_0);
8687 __u32 blocks_per_unit;
8688
8689 if (dev->vol.migr_state &&
8690 dev->vol.migr_type == MIGR_GEN_MIGR) {
8691 /* array state change is blocked due to reshape action
8692 * We might need to
8693 * - abort the reshape (if last_checkpoint is 0 and action!= reshape)
8694 * - finish the reshape (if last_checkpoint is big and action != reshape)
8695 * - update vol_curr_migr_unit
8696 */
8697 if (a->curr_action == reshape) {
8698 /* still reshaping, maybe update vol_curr_migr_unit */
8699 goto mark_checkpoint;
8700 } else {
8701 if (a->last_checkpoint >= a->info.component_size) {
8702 unsigned long long array_blocks;
8703 int used_disks;
8704 struct mdinfo *mdi;
8705
8706 used_disks = imsm_num_data_members(map);
8707 if (used_disks > 0) {
8708 array_blocks =
8709 per_dev_array_size(map) *
8710 used_disks;
8711 array_blocks =
8712 round_size_to_mb(array_blocks,
8713 used_disks);
8714 a->info.custom_array_size = array_blocks;
8715 /* encourage manager to update array
8716 * size
8717 */
8718
8719 a->check_reshape = 1;
8720 }
8721 /* finalize online capacity expansion/reshape */
8722 for (mdi = a->info.devs; mdi; mdi = mdi->next)
8723 imsm_set_disk(a,
8724 mdi->disk.raid_disk,
8725 mdi->curr_state);
8726
8727 imsm_progress_container_reshape(super);
8728 }
8729 }
8730 }
8731
8732 /* before we activate this array handle any missing disks */
8733 if (consistent == 2)
8734 handle_missing(super, dev);
8735
8736 if (consistent == 2 &&
8737 (!is_resync_complete(&a->info) ||
8738 map_state != IMSM_T_STATE_NORMAL ||
8739 dev->vol.migr_state))
8740 consistent = 0;
8741
8742 if (is_resync_complete(&a->info)) {
8743 /* complete intialization / resync,
8744 * recovery and interrupted recovery is completed in
8745 * ->set_disk
8746 */
8747 if (is_resyncing(dev)) {
8748 dprintf("imsm: mark resync done\n");
8749 end_migration(dev, super, map_state);
8750 super->updates_pending++;
8751 a->last_checkpoint = 0;
8752 }
8753 } else if ((!is_resyncing(dev) && !failed) &&
8754 (imsm_reshape_blocks_arrays_changes(super) == 0)) {
8755 /* mark the start of the init process if nothing is failed */
8756 dprintf("imsm: mark resync start\n");
8757 if (map->map_state == IMSM_T_STATE_UNINITIALIZED)
8758 migrate(dev, super, IMSM_T_STATE_NORMAL, MIGR_INIT);
8759 else
8760 migrate(dev, super, IMSM_T_STATE_NORMAL, MIGR_REPAIR);
8761 super->updates_pending++;
8762 }
8763
8764 if (a->prev_action == idle)
8765 goto skip_mark_checkpoint;
8766
8767 mark_checkpoint:
8768 /* skip checkpointing for general migration,
8769 * it is controlled in mdadm
8770 */
8771 if (is_gen_migration(dev))
8772 goto skip_mark_checkpoint;
8773
8774 /* check if we can update vol_curr_migr_unit from resync_start,
8775 * recovery_start
8776 */
8777 blocks_per_unit = blocks_per_migr_unit(super, dev);
8778 if (blocks_per_unit) {
8779 set_vol_curr_migr_unit(dev,
8780 a->last_checkpoint / blocks_per_unit);
8781 dprintf("imsm: mark checkpoint (%llu)\n",
8782 vol_curr_migr_unit(dev));
8783 super->updates_pending++;
8784 }
8785
8786 skip_mark_checkpoint:
8787 /* mark dirty / clean */
8788 if (((dev->vol.dirty & RAIDVOL_DIRTY) && consistent) ||
8789 (!(dev->vol.dirty & RAIDVOL_DIRTY) && !consistent)) {
8790 dprintf("imsm: mark '%s'\n", consistent ? "clean" : "dirty");
8791 if (consistent) {
8792 dev->vol.dirty = RAIDVOL_CLEAN;
8793 } else {
8794 dev->vol.dirty = RAIDVOL_DIRTY;
8795 if (dev->rwh_policy == RWH_DISTRIBUTED ||
8796 dev->rwh_policy == RWH_MULTIPLE_DISTRIBUTED)
8797 dev->vol.dirty |= RAIDVOL_DSRECORD_VALID;
8798 }
8799 super->updates_pending++;
8800 }
8801
8802 return consistent;
8803 }
8804
8805 static int imsm_disk_slot_to_ord(struct active_array *a, int slot)
8806 {
8807 int inst = a->info.container_member;
8808 struct intel_super *super = a->container->sb;
8809 struct imsm_dev *dev = get_imsm_dev(super, inst);
8810 struct imsm_map *map = get_imsm_map(dev, MAP_0);
8811
8812 if (slot > map->num_members) {
8813 pr_err("imsm: imsm_disk_slot_to_ord %d out of range 0..%d\n",
8814 slot, map->num_members - 1);
8815 return -1;
8816 }
8817
8818 if (slot < 0)
8819 return -1;
8820
8821 return get_imsm_ord_tbl_ent(dev, slot, MAP_0);
8822 }
8823
8824 static void imsm_set_disk(struct active_array *a, int n, int state)
8825 {
8826 int inst = a->info.container_member;
8827 struct intel_super *super = a->container->sb;
8828 struct imsm_dev *dev = get_imsm_dev(super, inst);
8829 struct imsm_map *map = get_imsm_map(dev, MAP_0);
8830 struct imsm_disk *disk;
8831 struct mdinfo *mdi;
8832 int recovery_not_finished = 0;
8833 int failed;
8834 int ord;
8835 __u8 map_state;
8836 int rebuild_done = 0;
8837 int i;
8838
8839 ord = get_imsm_ord_tbl_ent(dev, n, MAP_X);
8840 if (ord < 0)
8841 return;
8842
8843 dprintf("imsm: set_disk %d:%x\n", n, state);
8844 disk = get_imsm_disk(super, ord_to_idx(ord));
8845
8846 /* check for new failures */
8847 if (disk && (state & DS_FAULTY)) {
8848 if (mark_failure(super, dev, disk, ord_to_idx(ord)))
8849 super->updates_pending++;
8850 }
8851
8852 /* check if in_sync */
8853 if (state & DS_INSYNC && ord & IMSM_ORD_REBUILD && is_rebuilding(dev)) {
8854 struct imsm_map *migr_map = get_imsm_map(dev, MAP_1);
8855
8856 set_imsm_ord_tbl_ent(migr_map, n, ord_to_idx(ord));
8857 rebuild_done = 1;
8858 super->updates_pending++;
8859 }
8860
8861 failed = imsm_count_failed(super, dev, MAP_0);
8862 map_state = imsm_check_degraded(super, dev, failed, MAP_0);
8863
8864 /* check if recovery complete, newly degraded, or failed */
8865 dprintf("imsm: Detected transition to state ");
8866 switch (map_state) {
8867 case IMSM_T_STATE_NORMAL: /* transition to normal state */
8868 dprintf("normal: ");
8869 if (is_rebuilding(dev)) {
8870 dprintf_cont("while rebuilding");
8871 /* check if recovery is really finished */
8872 for (mdi = a->info.devs; mdi ; mdi = mdi->next)
8873 if (mdi->recovery_start != MaxSector) {
8874 recovery_not_finished = 1;
8875 break;
8876 }
8877 if (recovery_not_finished) {
8878 dprintf_cont("\n");
8879 dprintf("Rebuild has not finished yet, state not changed");
8880 if (a->last_checkpoint < mdi->recovery_start) {
8881 a->last_checkpoint = mdi->recovery_start;
8882 super->updates_pending++;
8883 }
8884 break;
8885 }
8886 end_migration(dev, super, map_state);
8887 map->failed_disk_num = ~0;
8888 super->updates_pending++;
8889 a->last_checkpoint = 0;
8890 break;
8891 }
8892 if (is_gen_migration(dev)) {
8893 dprintf_cont("while general migration");
8894 if (a->last_checkpoint >= a->info.component_size)
8895 end_migration(dev, super, map_state);
8896 else
8897 map->map_state = map_state;
8898 map->failed_disk_num = ~0;
8899 super->updates_pending++;
8900 break;
8901 }
8902 break;
8903 case IMSM_T_STATE_DEGRADED: /* transition to degraded state */
8904 dprintf_cont("degraded: ");
8905 if (map->map_state != map_state && !dev->vol.migr_state) {
8906 dprintf_cont("mark degraded");
8907 map->map_state = map_state;
8908 super->updates_pending++;
8909 a->last_checkpoint = 0;
8910 break;
8911 }
8912 if (is_rebuilding(dev)) {
8913 dprintf_cont("while rebuilding ");
8914 if (state & DS_FAULTY) {
8915 dprintf_cont("removing failed drive ");
8916 if (n == map->failed_disk_num) {
8917 dprintf_cont("end migration");
8918 end_migration(dev, super, map_state);
8919 a->last_checkpoint = 0;
8920 } else {
8921 dprintf_cont("fail detected during rebuild, changing map state");
8922 map->map_state = map_state;
8923 }
8924 super->updates_pending++;
8925 }
8926
8927 if (!rebuild_done)
8928 break;
8929
8930 /* check if recovery is really finished */
8931 for (mdi = a->info.devs; mdi ; mdi = mdi->next)
8932 if (mdi->recovery_start != MaxSector) {
8933 recovery_not_finished = 1;
8934 break;
8935 }
8936 if (recovery_not_finished) {
8937 dprintf_cont("\n");
8938 dprintf_cont("Rebuild has not finished yet");
8939 if (a->last_checkpoint < mdi->recovery_start) {
8940 a->last_checkpoint =
8941 mdi->recovery_start;
8942 super->updates_pending++;
8943 }
8944 break;
8945 }
8946
8947 dprintf_cont(" Rebuild done, still degraded");
8948 end_migration(dev, super, map_state);
8949 a->last_checkpoint = 0;
8950 super->updates_pending++;
8951
8952 for (i = 0; i < map->num_members; i++) {
8953 int idx = get_imsm_ord_tbl_ent(dev, i, MAP_0);
8954
8955 if (idx & IMSM_ORD_REBUILD)
8956 map->failed_disk_num = i;
8957 }
8958 super->updates_pending++;
8959 break;
8960 }
8961 if (is_gen_migration(dev)) {
8962 dprintf_cont("while general migration");
8963 if (a->last_checkpoint >= a->info.component_size)
8964 end_migration(dev, super, map_state);
8965 else {
8966 map->map_state = map_state;
8967 manage_second_map(super, dev);
8968 }
8969 super->updates_pending++;
8970 break;
8971 }
8972 if (is_initializing(dev)) {
8973 dprintf_cont("while initialization.");
8974 map->map_state = map_state;
8975 super->updates_pending++;
8976 break;
8977 }
8978 break;
8979 case IMSM_T_STATE_FAILED: /* transition to failed state */
8980 dprintf_cont("failed: ");
8981 if (is_gen_migration(dev)) {
8982 dprintf_cont("while general migration");
8983 map->map_state = map_state;
8984 super->updates_pending++;
8985 break;
8986 }
8987 if (map->map_state != map_state) {
8988 dprintf_cont("mark failed");
8989 end_migration(dev, super, map_state);
8990 super->updates_pending++;
8991 a->last_checkpoint = 0;
8992 break;
8993 }
8994 break;
8995 default:
8996 dprintf_cont("state %i\n", map_state);
8997 }
8998 dprintf_cont("\n");
8999 }
9000
9001 static int store_imsm_mpb(int fd, struct imsm_super *mpb)
9002 {
9003 void *buf = mpb;
9004 __u32 mpb_size = __le32_to_cpu(mpb->mpb_size);
9005 unsigned long long dsize;
9006 unsigned long long sectors;
9007 unsigned int sector_size;
9008
9009 if (!get_dev_sector_size(fd, NULL, &sector_size))
9010 return 1;
9011 get_dev_size(fd, NULL, &dsize);
9012
9013 if (mpb_size > sector_size) {
9014 /* -1 to account for anchor */
9015 sectors = mpb_sectors(mpb, sector_size) - 1;
9016
9017 /* write the extended mpb to the sectors preceeding the anchor */
9018 if (lseek64(fd, dsize - (sector_size * (2 + sectors)),
9019 SEEK_SET) < 0)
9020 return 1;
9021
9022 if ((unsigned long long)write(fd, buf + sector_size,
9023 sector_size * sectors) != sector_size * sectors)
9024 return 1;
9025 }
9026
9027 /* first block is stored on second to last sector of the disk */
9028 if (lseek64(fd, dsize - (sector_size * 2), SEEK_SET) < 0)
9029 return 1;
9030
9031 if ((unsigned int)write(fd, buf, sector_size) != sector_size)
9032 return 1;
9033
9034 return 0;
9035 }
9036
9037 static void imsm_sync_metadata(struct supertype *container)
9038 {
9039 struct intel_super *super = container->sb;
9040
9041 dprintf("sync metadata: %d\n", super->updates_pending);
9042 if (!super->updates_pending)
9043 return;
9044
9045 write_super_imsm(container, 0);
9046
9047 super->updates_pending = 0;
9048 }
9049
9050 static struct dl *imsm_readd(struct intel_super *super, int idx, struct active_array *a)
9051 {
9052 struct imsm_dev *dev = get_imsm_dev(super, a->info.container_member);
9053 int i = get_imsm_disk_idx(dev, idx, MAP_X);
9054 struct dl *dl;
9055
9056 for (dl = super->disks; dl; dl = dl->next)
9057 if (dl->index == i)
9058 break;
9059
9060 if (dl && is_failed(&dl->disk))
9061 dl = NULL;
9062
9063 if (dl)
9064 dprintf("found %x:%x\n", dl->major, dl->minor);
9065
9066 return dl;
9067 }
9068
9069 static struct dl *imsm_add_spare(struct intel_super *super, int slot,
9070 struct active_array *a, int activate_new,
9071 struct mdinfo *additional_test_list)
9072 {
9073 struct imsm_dev *dev = get_imsm_dev(super, a->info.container_member);
9074 int idx = get_imsm_disk_idx(dev, slot, MAP_X);
9075 struct imsm_super *mpb = super->anchor;
9076 struct imsm_map *map;
9077 unsigned long long pos;
9078 struct mdinfo *d;
9079 struct extent *ex;
9080 int i, j;
9081 int found;
9082 __u32 array_start = 0;
9083 __u32 array_end = 0;
9084 struct dl *dl;
9085 struct mdinfo *test_list;
9086
9087 for (dl = super->disks; dl; dl = dl->next) {
9088 /* If in this array, skip */
9089 for (d = a->info.devs ; d ; d = d->next)
9090 if (is_fd_valid(d->state_fd) &&
9091 d->disk.major == dl->major &&
9092 d->disk.minor == dl->minor) {
9093 dprintf("%x:%x already in array\n",
9094 dl->major, dl->minor);
9095 break;
9096 }
9097 if (d)
9098 continue;
9099 test_list = additional_test_list;
9100 while (test_list) {
9101 if (test_list->disk.major == dl->major &&
9102 test_list->disk.minor == dl->minor) {
9103 dprintf("%x:%x already in additional test list\n",
9104 dl->major, dl->minor);
9105 break;
9106 }
9107 test_list = test_list->next;
9108 }
9109 if (test_list)
9110 continue;
9111
9112 /* skip in use or failed drives */
9113 if (is_failed(&dl->disk) || idx == dl->index ||
9114 dl->index == -2) {
9115 dprintf("%x:%x status (failed: %d index: %d)\n",
9116 dl->major, dl->minor, is_failed(&dl->disk), idx);
9117 continue;
9118 }
9119
9120 /* skip pure spares when we are looking for partially
9121 * assimilated drives
9122 */
9123 if (dl->index == -1 && !activate_new)
9124 continue;
9125
9126 if (!drive_validate_sector_size(super, dl))
9127 continue;
9128
9129 /* Does this unused device have the requisite free space?
9130 * It needs to be able to cover all member volumes
9131 */
9132 ex = get_extents(super, dl, 1);
9133 if (!ex) {
9134 dprintf("cannot get extents\n");
9135 continue;
9136 }
9137 for (i = 0; i < mpb->num_raid_devs; i++) {
9138 dev = get_imsm_dev(super, i);
9139 map = get_imsm_map(dev, MAP_0);
9140
9141 /* check if this disk is already a member of
9142 * this array
9143 */
9144 if (get_imsm_disk_slot(map, dl->index) >= 0)
9145 continue;
9146
9147 found = 0;
9148 j = 0;
9149 pos = 0;
9150 array_start = pba_of_lba0(map);
9151 array_end = array_start +
9152 per_dev_array_size(map) - 1;
9153
9154 do {
9155 /* check that we can start at pba_of_lba0 with
9156 * num_data_stripes*blocks_per_stripe of space
9157 */
9158 if (array_start >= pos && array_end < ex[j].start) {
9159 found = 1;
9160 break;
9161 }
9162 pos = ex[j].start + ex[j].size;
9163 j++;
9164 } while (ex[j-1].size);
9165
9166 if (!found)
9167 break;
9168 }
9169
9170 free(ex);
9171 if (i < mpb->num_raid_devs) {
9172 dprintf("%x:%x does not have %u to %u available\n",
9173 dl->major, dl->minor, array_start, array_end);
9174 /* No room */
9175 continue;
9176 }
9177 return dl;
9178 }
9179
9180 return dl;
9181 }
9182
9183 static int imsm_rebuild_allowed(struct supertype *cont, int dev_idx, int failed)
9184 {
9185 struct imsm_dev *dev2;
9186 struct imsm_map *map;
9187 struct dl *idisk;
9188 int slot;
9189 int idx;
9190 __u8 state;
9191
9192 dev2 = get_imsm_dev(cont->sb, dev_idx);
9193
9194 state = imsm_check_degraded(cont->sb, dev2, failed, MAP_0);
9195 if (state == IMSM_T_STATE_FAILED) {
9196 map = get_imsm_map(dev2, MAP_0);
9197 for (slot = 0; slot < map->num_members; slot++) {
9198 /*
9199 * Check if failed disks are deleted from intel
9200 * disk list or are marked to be deleted
9201 */
9202 idx = get_imsm_disk_idx(dev2, slot, MAP_X);
9203 idisk = get_imsm_dl_disk(cont->sb, idx);
9204 /*
9205 * Do not rebuild the array if failed disks
9206 * from failed sub-array are not removed from
9207 * container.
9208 */
9209 if (idisk &&
9210 is_failed(&idisk->disk) &&
9211 (idisk->action != DISK_REMOVE))
9212 return 0;
9213 }
9214 }
9215 return 1;
9216 }
9217
9218 static struct mdinfo *imsm_activate_spare(struct active_array *a,
9219 struct metadata_update **updates)
9220 {
9221 /**
9222 * Find a device with unused free space and use it to replace a
9223 * failed/vacant region in an array. We replace failed regions one a
9224 * array at a time. The result is that a new spare disk will be added
9225 * to the first failed array and after the monitor has finished
9226 * propagating failures the remainder will be consumed.
9227 *
9228 * FIXME add a capability for mdmon to request spares from another
9229 * container.
9230 */
9231
9232 struct intel_super *super = a->container->sb;
9233 int inst = a->info.container_member;
9234 struct imsm_dev *dev = get_imsm_dev(super, inst);
9235 struct imsm_map *map = get_imsm_map(dev, MAP_0);
9236 int failed = a->info.array.raid_disks;
9237 struct mdinfo *rv = NULL;
9238 struct mdinfo *d;
9239 struct mdinfo *di;
9240 struct metadata_update *mu;
9241 struct dl *dl;
9242 struct imsm_update_activate_spare *u;
9243 int num_spares = 0;
9244 int i;
9245 int allowed;
9246
9247 for (d = a->info.devs ; d; d = d->next) {
9248 if (!is_fd_valid(d->state_fd))
9249 continue;
9250
9251 if (d->curr_state & DS_FAULTY)
9252 /* wait for Removal to happen */
9253 return NULL;
9254
9255 failed--;
9256 }
9257
9258 dprintf("imsm: activate spare: inst=%d failed=%d (%d) level=%d\n",
9259 inst, failed, a->info.array.raid_disks, a->info.array.level);
9260
9261 if (imsm_reshape_blocks_arrays_changes(super))
9262 return NULL;
9263
9264 /* Cannot activate another spare if rebuild is in progress already
9265 */
9266 if (is_rebuilding(dev)) {
9267 dprintf("imsm: No spare activation allowed. Rebuild in progress already.\n");
9268 return NULL;
9269 }
9270
9271 if (a->info.array.level == 4)
9272 /* No repair for takeovered array
9273 * imsm doesn't support raid4
9274 */
9275 return NULL;
9276
9277 if (imsm_check_degraded(super, dev, failed, MAP_0) !=
9278 IMSM_T_STATE_DEGRADED)
9279 return NULL;
9280
9281 if (get_imsm_map(dev, MAP_0)->map_state == IMSM_T_STATE_UNINITIALIZED) {
9282 dprintf("imsm: No spare activation allowed. Volume is not initialized.\n");
9283 return NULL;
9284 }
9285
9286 /*
9287 * If there are any failed disks check state of the other volume.
9288 * Block rebuild if the another one is failed until failed disks
9289 * are removed from container.
9290 */
9291 if (failed) {
9292 dprintf("found failed disks in %.*s, check if there anotherfailed sub-array.\n",
9293 MAX_RAID_SERIAL_LEN, dev->volume);
9294 /* check if states of the other volumes allow for rebuild */
9295 for (i = 0; i < super->anchor->num_raid_devs; i++) {
9296 if (i != inst) {
9297 allowed = imsm_rebuild_allowed(a->container,
9298 i, failed);
9299 if (!allowed)
9300 return NULL;
9301 }
9302 }
9303 }
9304
9305 /* For each slot, if it is not working, find a spare */
9306 for (i = 0; i < a->info.array.raid_disks; i++) {
9307 for (d = a->info.devs ; d ; d = d->next)
9308 if (d->disk.raid_disk == i)
9309 break;
9310 dprintf("found %d: %p %x\n", i, d, d?d->curr_state:0);
9311 if (d && is_fd_valid(d->state_fd))
9312 continue;
9313
9314 /*
9315 * OK, this device needs recovery. Try to re-add the
9316 * previous occupant of this slot, if this fails see if
9317 * we can continue the assimilation of a spare that was
9318 * partially assimilated, finally try to activate a new
9319 * spare.
9320 */
9321 dl = imsm_readd(super, i, a);
9322 if (!dl)
9323 dl = imsm_add_spare(super, i, a, 0, rv);
9324 if (!dl)
9325 dl = imsm_add_spare(super, i, a, 1, rv);
9326 if (!dl)
9327 continue;
9328
9329 /* found a usable disk with enough space */
9330 di = xcalloc(1, sizeof(*di));
9331
9332 /* dl->index will be -1 in the case we are activating a
9333 * pristine spare. imsm_process_update() will create a
9334 * new index in this case. Once a disk is found to be
9335 * failed in all member arrays it is kicked from the
9336 * metadata
9337 */
9338 di->disk.number = dl->index;
9339
9340 /* (ab)use di->devs to store a pointer to the device
9341 * we chose
9342 */
9343 di->devs = (struct mdinfo *) dl;
9344
9345 di->disk.raid_disk = i;
9346 di->disk.major = dl->major;
9347 di->disk.minor = dl->minor;
9348 di->disk.state = 0;
9349 di->recovery_start = 0;
9350 di->data_offset = pba_of_lba0(map);
9351 di->component_size = a->info.component_size;
9352 di->container_member = inst;
9353 di->bb.supported = 1;
9354 if (a->info.consistency_policy == CONSISTENCY_POLICY_PPL) {
9355 di->ppl_sector = get_ppl_sector(super, inst);
9356 di->ppl_size = MULTIPLE_PPL_AREA_SIZE_IMSM >> 9;
9357 }
9358 super->random = random32();
9359 di->next = rv;
9360 rv = di;
9361 num_spares++;
9362 dprintf("%x:%x to be %d at %llu\n", dl->major, dl->minor,
9363 i, di->data_offset);
9364 }
9365
9366 if (!rv)
9367 /* No spares found */
9368 return rv;
9369 /* Now 'rv' has a list of devices to return.
9370 * Create a metadata_update record to update the
9371 * disk_ord_tbl for the array
9372 */
9373 mu = xmalloc(sizeof(*mu));
9374 mu->buf = xcalloc(num_spares,
9375 sizeof(struct imsm_update_activate_spare));
9376 mu->space = NULL;
9377 mu->space_list = NULL;
9378 mu->len = sizeof(struct imsm_update_activate_spare) * num_spares;
9379 mu->next = *updates;
9380 u = (struct imsm_update_activate_spare *) mu->buf;
9381
9382 for (di = rv ; di ; di = di->next) {
9383 u->type = update_activate_spare;
9384 u->dl = (struct dl *) di->devs;
9385 di->devs = NULL;
9386 u->slot = di->disk.raid_disk;
9387 u->array = inst;
9388 u->next = u + 1;
9389 u++;
9390 }
9391 (u-1)->next = NULL;
9392 *updates = mu;
9393
9394 return rv;
9395 }
9396
9397 static int disks_overlap(struct intel_super *super, int idx, struct imsm_update_create_array *u)
9398 {
9399 struct imsm_dev *dev = get_imsm_dev(super, idx);
9400 struct imsm_map *map = get_imsm_map(dev, MAP_0);
9401 struct imsm_map *new_map = get_imsm_map(&u->dev, MAP_0);
9402 struct disk_info *inf = get_disk_info(u);
9403 struct imsm_disk *disk;
9404 int i;
9405 int j;
9406
9407 for (i = 0; i < map->num_members; i++) {
9408 disk = get_imsm_disk(super, get_imsm_disk_idx(dev, i, MAP_X));
9409 for (j = 0; j < new_map->num_members; j++)
9410 if (serialcmp(disk->serial, inf[j].serial) == 0)
9411 return 1;
9412 }
9413
9414 return 0;
9415 }
9416
9417 static struct dl *get_disk_super(struct intel_super *super, int major, int minor)
9418 {
9419 struct dl *dl;
9420
9421 for (dl = super->disks; dl; dl = dl->next)
9422 if (dl->major == major && dl->minor == minor)
9423 return dl;
9424 return NULL;
9425 }
9426
9427 static int remove_disk_super(struct intel_super *super, int major, int minor)
9428 {
9429 struct dl *prev;
9430 struct dl *dl;
9431
9432 prev = NULL;
9433 for (dl = super->disks; dl; dl = dl->next) {
9434 if (dl->major == major && dl->minor == minor) {
9435 /* remove */
9436 if (prev)
9437 prev->next = dl->next;
9438 else
9439 super->disks = dl->next;
9440 dl->next = NULL;
9441 __free_imsm_disk(dl, 1);
9442 dprintf("removed %x:%x\n", major, minor);
9443 break;
9444 }
9445 prev = dl;
9446 }
9447 return 0;
9448 }
9449
9450 static void imsm_delete(struct intel_super *super, struct dl **dlp, unsigned index);
9451
9452 static int add_remove_disk_update(struct intel_super *super)
9453 {
9454 int check_degraded = 0;
9455 struct dl *disk;
9456
9457 /* add/remove some spares to/from the metadata/contrainer */
9458 while (super->disk_mgmt_list) {
9459 struct dl *disk_cfg;
9460
9461 disk_cfg = super->disk_mgmt_list;
9462 super->disk_mgmt_list = disk_cfg->next;
9463 disk_cfg->next = NULL;
9464
9465 if (disk_cfg->action == DISK_ADD) {
9466 disk_cfg->next = super->disks;
9467 super->disks = disk_cfg;
9468 check_degraded = 1;
9469 dprintf("added %x:%x\n",
9470 disk_cfg->major, disk_cfg->minor);
9471 } else if (disk_cfg->action == DISK_REMOVE) {
9472 dprintf("Disk remove action processed: %x.%x\n",
9473 disk_cfg->major, disk_cfg->minor);
9474 disk = get_disk_super(super,
9475 disk_cfg->major,
9476 disk_cfg->minor);
9477 if (disk) {
9478 /* store action status */
9479 disk->action = DISK_REMOVE;
9480 /* remove spare disks only */
9481 if (disk->index == -1) {
9482 remove_disk_super(super,
9483 disk_cfg->major,
9484 disk_cfg->minor);
9485 } else {
9486 disk_cfg->fd = disk->fd;
9487 disk->fd = -1;
9488 }
9489 }
9490 /* release allocate disk structure */
9491 __free_imsm_disk(disk_cfg, 1);
9492 }
9493 }
9494 return check_degraded;
9495 }
9496
9497 static int apply_reshape_migration_update(struct imsm_update_reshape_migration *u,
9498 struct intel_super *super,
9499 void ***space_list)
9500 {
9501 struct intel_dev *id;
9502 void **tofree = NULL;
9503 int ret_val = 0;
9504
9505 dprintf("(enter)\n");
9506 if (u->subdev < 0 || u->subdev > 1) {
9507 dprintf("imsm: Error: Wrong subdev: %i\n", u->subdev);
9508 return ret_val;
9509 }
9510 if (space_list == NULL || *space_list == NULL) {
9511 dprintf("imsm: Error: Memory is not allocated\n");
9512 return ret_val;
9513 }
9514
9515 for (id = super->devlist ; id; id = id->next) {
9516 if (id->index == (unsigned)u->subdev) {
9517 struct imsm_dev *dev = get_imsm_dev(super, u->subdev);
9518 struct imsm_map *map;
9519 struct imsm_dev *new_dev =
9520 (struct imsm_dev *)*space_list;
9521 struct imsm_map *migr_map = get_imsm_map(dev, MAP_1);
9522 int to_state;
9523 struct dl *new_disk;
9524
9525 if (new_dev == NULL)
9526 return ret_val;
9527 *space_list = **space_list;
9528 memcpy(new_dev, dev, sizeof_imsm_dev(dev, 0));
9529 map = get_imsm_map(new_dev, MAP_0);
9530 if (migr_map) {
9531 dprintf("imsm: Error: migration in progress");
9532 return ret_val;
9533 }
9534
9535 to_state = map->map_state;
9536 if ((u->new_level == 5) && (map->raid_level == 0)) {
9537 map->num_members++;
9538 /* this should not happen */
9539 if (u->new_disks[0] < 0) {
9540 map->failed_disk_num =
9541 map->num_members - 1;
9542 to_state = IMSM_T_STATE_DEGRADED;
9543 } else
9544 to_state = IMSM_T_STATE_NORMAL;
9545 }
9546 migrate(new_dev, super, to_state, MIGR_GEN_MIGR);
9547 if (u->new_level > -1)
9548 map->raid_level = u->new_level;
9549 migr_map = get_imsm_map(new_dev, MAP_1);
9550 if ((u->new_level == 5) &&
9551 (migr_map->raid_level == 0)) {
9552 int ord = map->num_members - 1;
9553 migr_map->num_members--;
9554 if (u->new_disks[0] < 0)
9555 ord |= IMSM_ORD_REBUILD;
9556 set_imsm_ord_tbl_ent(map,
9557 map->num_members - 1,
9558 ord);
9559 }
9560 id->dev = new_dev;
9561 tofree = (void **)dev;
9562
9563 /* update chunk size
9564 */
9565 if (u->new_chunksize > 0) {
9566 struct imsm_map *dest_map =
9567 get_imsm_map(dev, MAP_0);
9568 int used_disks =
9569 imsm_num_data_members(dest_map);
9570
9571 if (used_disks == 0)
9572 return ret_val;
9573
9574 map->blocks_per_strip =
9575 __cpu_to_le16(u->new_chunksize * 2);
9576 update_num_data_stripes(map, imsm_dev_size(dev));
9577 }
9578
9579 /* ensure blocks_per_member has valid value
9580 */
9581 set_blocks_per_member(map,
9582 per_dev_array_size(map) +
9583 NUM_BLOCKS_DIRTY_STRIPE_REGION);
9584
9585 /* add disk
9586 */
9587 if (u->new_level != 5 || migr_map->raid_level != 0 ||
9588 migr_map->raid_level == map->raid_level)
9589 goto skip_disk_add;
9590
9591 if (u->new_disks[0] >= 0) {
9592 /* use passes spare
9593 */
9594 new_disk = get_disk_super(super,
9595 major(u->new_disks[0]),
9596 minor(u->new_disks[0]));
9597 dprintf("imsm: new disk for reshape is: %i:%i (%p, index = %i)\n",
9598 major(u->new_disks[0]),
9599 minor(u->new_disks[0]),
9600 new_disk, new_disk->index);
9601 if (new_disk == NULL)
9602 goto error_disk_add;
9603
9604 new_disk->index = map->num_members - 1;
9605 /* slot to fill in autolayout
9606 */
9607 new_disk->raiddisk = new_disk->index;
9608 new_disk->disk.status |= CONFIGURED_DISK;
9609 new_disk->disk.status &= ~SPARE_DISK;
9610 } else
9611 goto error_disk_add;
9612
9613 skip_disk_add:
9614 *tofree = *space_list;
9615 /* calculate new size
9616 */
9617 imsm_set_array_size(new_dev, -1);
9618
9619 ret_val = 1;
9620 }
9621 }
9622
9623 if (tofree)
9624 *space_list = tofree;
9625 return ret_val;
9626
9627 error_disk_add:
9628 dprintf("Error: imsm: Cannot find disk.\n");
9629 return ret_val;
9630 }
9631
9632 static int apply_size_change_update(struct imsm_update_size_change *u,
9633 struct intel_super *super)
9634 {
9635 struct intel_dev *id;
9636 int ret_val = 0;
9637
9638 dprintf("(enter)\n");
9639 if (u->subdev < 0 || u->subdev > 1) {
9640 dprintf("imsm: Error: Wrong subdev: %i\n", u->subdev);
9641 return ret_val;
9642 }
9643
9644 for (id = super->devlist ; id; id = id->next) {
9645 if (id->index == (unsigned)u->subdev) {
9646 struct imsm_dev *dev = get_imsm_dev(super, u->subdev);
9647 struct imsm_map *map = get_imsm_map(dev, MAP_0);
9648 int used_disks = imsm_num_data_members(map);
9649 unsigned long long blocks_per_member;
9650 unsigned long long new_size_per_disk;
9651
9652 if (used_disks == 0)
9653 return 0;
9654
9655 /* calculate new size
9656 */
9657 new_size_per_disk = u->new_size / used_disks;
9658 blocks_per_member = new_size_per_disk +
9659 NUM_BLOCKS_DIRTY_STRIPE_REGION;
9660
9661 imsm_set_array_size(dev, u->new_size);
9662 set_blocks_per_member(map, blocks_per_member);
9663 update_num_data_stripes(map, u->new_size);
9664 ret_val = 1;
9665 break;
9666 }
9667 }
9668
9669 return ret_val;
9670 }
9671
9672 static int prepare_spare_to_activate(struct supertype *st,
9673 struct imsm_update_activate_spare *u)
9674 {
9675 struct intel_super *super = st->sb;
9676 int prev_current_vol = super->current_vol;
9677 struct active_array *a;
9678 int ret = 1;
9679
9680 for (a = st->arrays; a; a = a->next)
9681 /*
9682 * Additional initialization (adding bitmap header, filling
9683 * the bitmap area with '1's to force initial rebuild for a whole
9684 * data-area) is required when adding the spare to the volume
9685 * with write-intent bitmap.
9686 */
9687 if (a->info.container_member == u->array &&
9688 a->info.consistency_policy == CONSISTENCY_POLICY_BITMAP) {
9689 struct dl *dl;
9690
9691 for (dl = super->disks; dl; dl = dl->next)
9692 if (dl == u->dl)
9693 break;
9694 if (!dl)
9695 break;
9696
9697 super->current_vol = u->array;
9698 if (st->ss->write_bitmap(st, dl->fd, NoUpdate))
9699 ret = 0;
9700 super->current_vol = prev_current_vol;
9701 }
9702 return ret;
9703 }
9704
9705 static int apply_update_activate_spare(struct imsm_update_activate_spare *u,
9706 struct intel_super *super,
9707 struct active_array *active_array)
9708 {
9709 struct imsm_super *mpb = super->anchor;
9710 struct imsm_dev *dev = get_imsm_dev(super, u->array);
9711 struct imsm_map *map = get_imsm_map(dev, MAP_0);
9712 struct imsm_map *migr_map;
9713 struct active_array *a;
9714 struct imsm_disk *disk;
9715 __u8 to_state;
9716 struct dl *dl;
9717 unsigned int found;
9718 int failed;
9719 int victim;
9720 int i;
9721 int second_map_created = 0;
9722
9723 for (; u; u = u->next) {
9724 victim = get_imsm_disk_idx(dev, u->slot, MAP_X);
9725
9726 if (victim < 0)
9727 return 0;
9728
9729 for (dl = super->disks; dl; dl = dl->next)
9730 if (dl == u->dl)
9731 break;
9732
9733 if (!dl) {
9734 pr_err("error: imsm_activate_spare passed an unknown disk (index: %d)\n",
9735 u->dl->index);
9736 return 0;
9737 }
9738
9739 /* count failures (excluding rebuilds and the victim)
9740 * to determine map[0] state
9741 */
9742 failed = 0;
9743 for (i = 0; i < map->num_members; i++) {
9744 if (i == u->slot)
9745 continue;
9746 disk = get_imsm_disk(super,
9747 get_imsm_disk_idx(dev, i, MAP_X));
9748 if (!disk || is_failed(disk))
9749 failed++;
9750 }
9751
9752 /* adding a pristine spare, assign a new index */
9753 if (dl->index < 0) {
9754 dl->index = super->anchor->num_disks;
9755 super->anchor->num_disks++;
9756 }
9757 disk = &dl->disk;
9758 disk->status |= CONFIGURED_DISK;
9759 disk->status &= ~SPARE_DISK;
9760
9761 /* mark rebuild */
9762 to_state = imsm_check_degraded(super, dev, failed, MAP_0);
9763 if (!second_map_created) {
9764 second_map_created = 1;
9765 map->map_state = IMSM_T_STATE_DEGRADED;
9766 migrate(dev, super, to_state, MIGR_REBUILD);
9767 } else
9768 map->map_state = to_state;
9769 migr_map = get_imsm_map(dev, MAP_1);
9770 set_imsm_ord_tbl_ent(map, u->slot, dl->index);
9771 set_imsm_ord_tbl_ent(migr_map, u->slot,
9772 dl->index | IMSM_ORD_REBUILD);
9773
9774 /* update the family_num to mark a new container
9775 * generation, being careful to record the existing
9776 * family_num in orig_family_num to clean up after
9777 * earlier mdadm versions that neglected to set it.
9778 */
9779 if (mpb->orig_family_num == 0)
9780 mpb->orig_family_num = mpb->family_num;
9781 mpb->family_num += super->random;
9782
9783 /* count arrays using the victim in the metadata */
9784 found = 0;
9785 for (a = active_array; a ; a = a->next) {
9786 int dev_idx = a->info.container_member;
9787
9788 if (get_disk_slot_in_dev(super, dev_idx, victim) >= 0)
9789 found++;
9790 }
9791
9792 /* delete the victim if it is no longer being
9793 * utilized anywhere
9794 */
9795 if (!found) {
9796 struct dl **dlp;
9797
9798 /* We know that 'manager' isn't touching anything,
9799 * so it is safe to delete
9800 */
9801 for (dlp = &super->disks; *dlp; dlp = &(*dlp)->next)
9802 if ((*dlp)->index == victim)
9803 break;
9804
9805 /* victim may be on the missing list */
9806 if (!*dlp)
9807 for (dlp = &super->missing; *dlp;
9808 dlp = &(*dlp)->next)
9809 if ((*dlp)->index == victim)
9810 break;
9811 imsm_delete(super, dlp, victim);
9812 }
9813 }
9814
9815 return 1;
9816 }
9817
9818 static int apply_reshape_container_disks_update(struct imsm_update_reshape *u,
9819 struct intel_super *super,
9820 void ***space_list)
9821 {
9822 struct dl *new_disk;
9823 struct intel_dev *id;
9824 int i;
9825 int delta_disks = u->new_raid_disks - u->old_raid_disks;
9826 int disk_count = u->old_raid_disks;
9827 void **tofree = NULL;
9828 int devices_to_reshape = 1;
9829 struct imsm_super *mpb = super->anchor;
9830 int ret_val = 0;
9831 unsigned int dev_id;
9832
9833 dprintf("(enter)\n");
9834
9835 /* enable spares to use in array */
9836 for (i = 0; i < delta_disks; i++) {
9837 new_disk = get_disk_super(super,
9838 major(u->new_disks[i]),
9839 minor(u->new_disks[i]));
9840 dprintf("imsm: new disk for reshape is: %i:%i (%p, index = %i)\n",
9841 major(u->new_disks[i]), minor(u->new_disks[i]),
9842 new_disk, new_disk->index);
9843 if (new_disk == NULL ||
9844 (new_disk->index >= 0 &&
9845 new_disk->index < u->old_raid_disks))
9846 goto update_reshape_exit;
9847 new_disk->index = disk_count++;
9848 /* slot to fill in autolayout
9849 */
9850 new_disk->raiddisk = new_disk->index;
9851 new_disk->disk.status |=
9852 CONFIGURED_DISK;
9853 new_disk->disk.status &= ~SPARE_DISK;
9854 }
9855
9856 dprintf("imsm: volume set mpb->num_raid_devs = %i\n",
9857 mpb->num_raid_devs);
9858 /* manage changes in volume
9859 */
9860 for (dev_id = 0; dev_id < mpb->num_raid_devs; dev_id++) {
9861 void **sp = *space_list;
9862 struct imsm_dev *newdev;
9863 struct imsm_map *newmap, *oldmap;
9864
9865 for (id = super->devlist ; id; id = id->next) {
9866 if (id->index == dev_id)
9867 break;
9868 }
9869 if (id == NULL)
9870 break;
9871 if (!sp)
9872 continue;
9873 *space_list = *sp;
9874 newdev = (void*)sp;
9875 /* Copy the dev, but not (all of) the map */
9876 memcpy(newdev, id->dev, sizeof(*newdev));
9877 oldmap = get_imsm_map(id->dev, MAP_0);
9878 newmap = get_imsm_map(newdev, MAP_0);
9879 /* Copy the current map */
9880 memcpy(newmap, oldmap, sizeof_imsm_map(oldmap));
9881 /* update one device only
9882 */
9883 if (devices_to_reshape) {
9884 dprintf("imsm: modifying subdev: %i\n",
9885 id->index);
9886 devices_to_reshape--;
9887 newdev->vol.migr_state = 1;
9888 set_vol_curr_migr_unit(newdev, 0);
9889 set_migr_type(newdev, MIGR_GEN_MIGR);
9890 newmap->num_members = u->new_raid_disks;
9891 for (i = 0; i < delta_disks; i++) {
9892 set_imsm_ord_tbl_ent(newmap,
9893 u->old_raid_disks + i,
9894 u->old_raid_disks + i);
9895 }
9896 /* New map is correct, now need to save old map
9897 */
9898 newmap = get_imsm_map(newdev, MAP_1);
9899 memcpy(newmap, oldmap, sizeof_imsm_map(oldmap));
9900
9901 imsm_set_array_size(newdev, -1);
9902 }
9903
9904 sp = (void **)id->dev;
9905 id->dev = newdev;
9906 *sp = tofree;
9907 tofree = sp;
9908
9909 /* Clear migration record */
9910 memset(super->migr_rec, 0, sizeof(struct migr_record));
9911 }
9912 if (tofree)
9913 *space_list = tofree;
9914 ret_val = 1;
9915
9916 update_reshape_exit:
9917
9918 return ret_val;
9919 }
9920
9921 static int apply_takeover_update(struct imsm_update_takeover *u,
9922 struct intel_super *super,
9923 void ***space_list)
9924 {
9925 struct imsm_dev *dev = NULL;
9926 struct intel_dev *dv;
9927 struct imsm_dev *dev_new;
9928 struct imsm_map *map;
9929 struct dl *dm, *du;
9930 int i;
9931
9932 for (dv = super->devlist; dv; dv = dv->next)
9933 if (dv->index == (unsigned int)u->subarray) {
9934 dev = dv->dev;
9935 break;
9936 }
9937
9938 if (dev == NULL)
9939 return 0;
9940
9941 map = get_imsm_map(dev, MAP_0);
9942
9943 if (u->direction == R10_TO_R0) {
9944 /* Number of failed disks must be half of initial disk number */
9945 if (imsm_count_failed(super, dev, MAP_0) !=
9946 (map->num_members / 2))
9947 return 0;
9948
9949 /* iterate through devices to mark removed disks as spare */
9950 for (dm = super->disks; dm; dm = dm->next) {
9951 if (dm->disk.status & FAILED_DISK) {
9952 int idx = dm->index;
9953 /* update indexes on the disk list */
9954 /* FIXME this loop-with-the-loop looks wrong, I'm not convinced
9955 the index values will end up being correct.... NB */
9956 for (du = super->disks; du; du = du->next)
9957 if (du->index > idx)
9958 du->index--;
9959 /* mark as spare disk */
9960 mark_spare(dm);
9961 }
9962 }
9963 /* update map */
9964 map->num_members /= map->num_domains;
9965 map->map_state = IMSM_T_STATE_NORMAL;
9966 map->raid_level = 0;
9967 set_num_domains(map);
9968 update_num_data_stripes(map, imsm_dev_size(dev));
9969 map->failed_disk_num = -1;
9970 }
9971
9972 if (u->direction == R0_TO_R10) {
9973 void **space;
9974
9975 /* update slots in current disk list */
9976 for (dm = super->disks; dm; dm = dm->next) {
9977 if (dm->index >= 0)
9978 dm->index *= 2;
9979 }
9980 /* create new *missing* disks */
9981 for (i = 0; i < map->num_members; i++) {
9982 space = *space_list;
9983 if (!space)
9984 continue;
9985 *space_list = *space;
9986 du = (void *)space;
9987 memcpy(du, super->disks, sizeof(*du));
9988 du->fd = -1;
9989 du->minor = 0;
9990 du->major = 0;
9991 du->index = (i * 2) + 1;
9992 sprintf((char *)du->disk.serial,
9993 " MISSING_%d", du->index);
9994 sprintf((char *)du->serial,
9995 "MISSING_%d", du->index);
9996 du->next = super->missing;
9997 super->missing = du;
9998 }
9999 /* create new dev and map */
10000 space = *space_list;
10001 if (!space)
10002 return 0;
10003 *space_list = *space;
10004 dev_new = (void *)space;
10005 memcpy(dev_new, dev, sizeof(*dev));
10006 /* update new map */
10007 map = get_imsm_map(dev_new, MAP_0);
10008
10009 map->map_state = IMSM_T_STATE_DEGRADED;
10010 map->raid_level = 1;
10011 set_num_domains(map);
10012 map->num_members = map->num_members * map->num_domains;
10013 update_num_data_stripes(map, imsm_dev_size(dev));
10014
10015 /* replace dev<->dev_new */
10016 dv->dev = dev_new;
10017 }
10018 /* update disk order table */
10019 for (du = super->disks; du; du = du->next)
10020 if (du->index >= 0)
10021 set_imsm_ord_tbl_ent(map, du->index, du->index);
10022 for (du = super->missing; du; du = du->next)
10023 if (du->index >= 0) {
10024 set_imsm_ord_tbl_ent(map, du->index, du->index);
10025 mark_missing(super, dv->dev, &du->disk, du->index);
10026 }
10027
10028 return 1;
10029 }
10030
10031 static void imsm_process_update(struct supertype *st,
10032 struct metadata_update *update)
10033 {
10034 /**
10035 * crack open the metadata_update envelope to find the update record
10036 * update can be one of:
10037 * update_reshape_container_disks - all the arrays in the container
10038 * are being reshaped to have more devices. We need to mark
10039 * the arrays for general migration and convert selected spares
10040 * into active devices.
10041 * update_activate_spare - a spare device has replaced a failed
10042 * device in an array, update the disk_ord_tbl. If this disk is
10043 * present in all member arrays then also clear the SPARE_DISK
10044 * flag
10045 * update_create_array
10046 * update_kill_array
10047 * update_rename_array
10048 * update_add_remove_disk
10049 */
10050 struct intel_super *super = st->sb;
10051 struct imsm_super *mpb;
10052 enum imsm_update_type type = *(enum imsm_update_type *) update->buf;
10053
10054 /* update requires a larger buf but the allocation failed */
10055 if (super->next_len && !super->next_buf) {
10056 super->next_len = 0;
10057 return;
10058 }
10059
10060 if (super->next_buf) {
10061 memcpy(super->next_buf, super->buf, super->len);
10062 free(super->buf);
10063 super->len = super->next_len;
10064 super->buf = super->next_buf;
10065
10066 super->next_len = 0;
10067 super->next_buf = NULL;
10068 }
10069
10070 mpb = super->anchor;
10071
10072 switch (type) {
10073 case update_general_migration_checkpoint: {
10074 struct intel_dev *id;
10075 struct imsm_update_general_migration_checkpoint *u =
10076 (void *)update->buf;
10077
10078 dprintf("called for update_general_migration_checkpoint\n");
10079
10080 /* find device under general migration */
10081 for (id = super->devlist ; id; id = id->next) {
10082 if (is_gen_migration(id->dev)) {
10083 set_vol_curr_migr_unit(id->dev,
10084 u->curr_migr_unit);
10085 super->updates_pending++;
10086 }
10087 }
10088 break;
10089 }
10090 case update_takeover: {
10091 struct imsm_update_takeover *u = (void *)update->buf;
10092 if (apply_takeover_update(u, super, &update->space_list)) {
10093 imsm_update_version_info(super);
10094 super->updates_pending++;
10095 }
10096 break;
10097 }
10098
10099 case update_reshape_container_disks: {
10100 struct imsm_update_reshape *u = (void *)update->buf;
10101 if (apply_reshape_container_disks_update(
10102 u, super, &update->space_list))
10103 super->updates_pending++;
10104 break;
10105 }
10106 case update_reshape_migration: {
10107 struct imsm_update_reshape_migration *u = (void *)update->buf;
10108 if (apply_reshape_migration_update(
10109 u, super, &update->space_list))
10110 super->updates_pending++;
10111 break;
10112 }
10113 case update_size_change: {
10114 struct imsm_update_size_change *u = (void *)update->buf;
10115 if (apply_size_change_update(u, super))
10116 super->updates_pending++;
10117 break;
10118 }
10119 case update_activate_spare: {
10120 struct imsm_update_activate_spare *u = (void *) update->buf;
10121
10122 if (prepare_spare_to_activate(st, u) &&
10123 apply_update_activate_spare(u, super, st->arrays))
10124 super->updates_pending++;
10125 break;
10126 }
10127 case update_create_array: {
10128 /* someone wants to create a new array, we need to be aware of
10129 * a few races/collisions:
10130 * 1/ 'Create' called by two separate instances of mdadm
10131 * 2/ 'Create' versus 'activate_spare': mdadm has chosen
10132 * devices that have since been assimilated via
10133 * activate_spare.
10134 * In the event this update can not be carried out mdadm will
10135 * (FIX ME) notice that its update did not take hold.
10136 */
10137 struct imsm_update_create_array *u = (void *) update->buf;
10138 struct intel_dev *dv;
10139 struct imsm_dev *dev;
10140 struct imsm_map *map, *new_map;
10141 unsigned long long start, end;
10142 unsigned long long new_start, new_end;
10143 int i;
10144 struct disk_info *inf;
10145 struct dl *dl;
10146
10147 /* handle racing creates: first come first serve */
10148 if (u->dev_idx < mpb->num_raid_devs) {
10149 dprintf("subarray %d already defined\n", u->dev_idx);
10150 goto create_error;
10151 }
10152
10153 /* check update is next in sequence */
10154 if (u->dev_idx != mpb->num_raid_devs) {
10155 dprintf("can not create array %d expected index %d\n",
10156 u->dev_idx, mpb->num_raid_devs);
10157 goto create_error;
10158 }
10159
10160 new_map = get_imsm_map(&u->dev, MAP_0);
10161 new_start = pba_of_lba0(new_map);
10162 new_end = new_start + per_dev_array_size(new_map);
10163 inf = get_disk_info(u);
10164
10165 /* handle activate_spare versus create race:
10166 * check to make sure that overlapping arrays do not include
10167 * overalpping disks
10168 */
10169 for (i = 0; i < mpb->num_raid_devs; i++) {
10170 dev = get_imsm_dev(super, i);
10171 map = get_imsm_map(dev, MAP_0);
10172 start = pba_of_lba0(map);
10173 end = start + per_dev_array_size(map);
10174 if ((new_start >= start && new_start <= end) ||
10175 (start >= new_start && start <= new_end))
10176 /* overlap */;
10177 else
10178 continue;
10179
10180 if (disks_overlap(super, i, u)) {
10181 dprintf("arrays overlap\n");
10182 goto create_error;
10183 }
10184 }
10185
10186 /* check that prepare update was successful */
10187 if (!update->space) {
10188 dprintf("prepare update failed\n");
10189 goto create_error;
10190 }
10191
10192 /* check that all disks are still active before committing
10193 * changes. FIXME: could we instead handle this by creating a
10194 * degraded array? That's probably not what the user expects,
10195 * so better to drop this update on the floor.
10196 */
10197 for (i = 0; i < new_map->num_members; i++) {
10198 dl = serial_to_dl(inf[i].serial, super);
10199 if (!dl) {
10200 dprintf("disk disappeared\n");
10201 goto create_error;
10202 }
10203 }
10204
10205 super->updates_pending++;
10206
10207 /* convert spares to members and fixup ord_tbl */
10208 for (i = 0; i < new_map->num_members; i++) {
10209 dl = serial_to_dl(inf[i].serial, super);
10210 if (dl->index == -1) {
10211 dl->index = mpb->num_disks;
10212 mpb->num_disks++;
10213 dl->disk.status |= CONFIGURED_DISK;
10214 dl->disk.status &= ~SPARE_DISK;
10215 }
10216 set_imsm_ord_tbl_ent(new_map, i, dl->index);
10217 }
10218
10219 dv = update->space;
10220 dev = dv->dev;
10221 update->space = NULL;
10222 imsm_copy_dev(dev, &u->dev);
10223 dv->index = u->dev_idx;
10224 dv->next = super->devlist;
10225 super->devlist = dv;
10226 mpb->num_raid_devs++;
10227
10228 imsm_update_version_info(super);
10229 break;
10230 create_error:
10231 /* mdmon knows how to release update->space, but not
10232 * ((struct intel_dev *) update->space)->dev
10233 */
10234 if (update->space) {
10235 dv = update->space;
10236 free(dv->dev);
10237 }
10238 break;
10239 }
10240 case update_kill_array: {
10241 struct imsm_update_kill_array *u = (void *) update->buf;
10242 int victim = u->dev_idx;
10243 struct active_array *a;
10244 struct intel_dev **dp;
10245
10246 /* sanity check that we are not affecting the uuid of
10247 * active arrays, or deleting an active array
10248 *
10249 * FIXME when immutable ids are available, but note that
10250 * we'll also need to fixup the invalidated/active
10251 * subarray indexes in mdstat
10252 */
10253 for (a = st->arrays; a; a = a->next)
10254 if (a->info.container_member >= victim)
10255 break;
10256 /* by definition if mdmon is running at least one array
10257 * is active in the container, so checking
10258 * mpb->num_raid_devs is just extra paranoia
10259 */
10260 if (a || mpb->num_raid_devs == 1 || victim >= super->anchor->num_raid_devs) {
10261 dprintf("failed to delete subarray-%d\n", victim);
10262 break;
10263 }
10264
10265 for (dp = &super->devlist; *dp;)
10266 if ((*dp)->index == (unsigned)super->current_vol) {
10267 *dp = (*dp)->next;
10268 } else {
10269 if ((*dp)->index > (unsigned)victim)
10270 (*dp)->index--;
10271 dp = &(*dp)->next;
10272 }
10273 mpb->num_raid_devs--;
10274 super->updates_pending++;
10275 break;
10276 }
10277 case update_rename_array: {
10278 struct imsm_update_rename_array *u = (void *) update->buf;
10279 char name[MAX_RAID_SERIAL_LEN+1];
10280 int target = u->dev_idx;
10281 struct active_array *a;
10282 struct imsm_dev *dev;
10283
10284 /* sanity check that we are not affecting the uuid of
10285 * an active array
10286 */
10287 memset(name, 0, sizeof(name));
10288 snprintf(name, MAX_RAID_SERIAL_LEN, "%s", (char *) u->name);
10289 name[MAX_RAID_SERIAL_LEN] = '\0';
10290 for (a = st->arrays; a; a = a->next)
10291 if (a->info.container_member == target)
10292 break;
10293 dev = get_imsm_dev(super, u->dev_idx);
10294
10295 if (a || !dev || imsm_is_name_allowed(super, name, 0) == false) {
10296 dprintf("failed to rename subarray-%d\n", target);
10297 break;
10298 }
10299
10300 memcpy(dev->volume, name, MAX_RAID_SERIAL_LEN);
10301 super->updates_pending++;
10302 break;
10303 }
10304 case update_add_remove_disk: {
10305 /* we may be able to repair some arrays if disks are
10306 * being added, check the status of add_remove_disk
10307 * if discs has been added.
10308 */
10309 if (add_remove_disk_update(super)) {
10310 struct active_array *a;
10311
10312 super->updates_pending++;
10313 for (a = st->arrays; a; a = a->next)
10314 a->check_degraded = 1;
10315 }
10316 break;
10317 }
10318 case update_prealloc_badblocks_mem:
10319 break;
10320 case update_rwh_policy: {
10321 struct imsm_update_rwh_policy *u = (void *)update->buf;
10322 int target = u->dev_idx;
10323 struct imsm_dev *dev = get_imsm_dev(super, target);
10324
10325 if (dev->rwh_policy != u->new_policy) {
10326 dev->rwh_policy = u->new_policy;
10327 super->updates_pending++;
10328 }
10329 break;
10330 }
10331 default:
10332 pr_err("error: unsupported process update type:(type: %d)\n", type);
10333 }
10334 }
10335
10336 static struct mdinfo *get_spares_for_grow(struct supertype *st);
10337
10338 static int imsm_prepare_update(struct supertype *st,
10339 struct metadata_update *update)
10340 {
10341 /**
10342 * Allocate space to hold new disk entries, raid-device entries or a new
10343 * mpb if necessary. The manager synchronously waits for updates to
10344 * complete in the monitor, so new mpb buffers allocated here can be
10345 * integrated by the monitor thread without worrying about live pointers
10346 * in the manager thread.
10347 */
10348 enum imsm_update_type type;
10349 struct intel_super *super = st->sb;
10350 unsigned int sector_size = super->sector_size;
10351 struct imsm_super *mpb = super->anchor;
10352 size_t buf_len;
10353 size_t len = 0;
10354
10355 if (update->len < (int)sizeof(type))
10356 return 0;
10357
10358 type = *(enum imsm_update_type *) update->buf;
10359
10360 switch (type) {
10361 case update_general_migration_checkpoint:
10362 if (update->len < (int)sizeof(struct imsm_update_general_migration_checkpoint))
10363 return 0;
10364 dprintf("called for update_general_migration_checkpoint\n");
10365 break;
10366 case update_takeover: {
10367 struct imsm_update_takeover *u = (void *)update->buf;
10368 if (update->len < (int)sizeof(*u))
10369 return 0;
10370 if (u->direction == R0_TO_R10) {
10371 void **tail = (void **)&update->space_list;
10372 struct imsm_dev *dev = get_imsm_dev(super, u->subarray);
10373 struct imsm_map *map = get_imsm_map(dev, MAP_0);
10374 int num_members = map->num_members;
10375 void *space;
10376 int size, i;
10377 /* allocate memory for added disks */
10378 for (i = 0; i < num_members; i++) {
10379 size = sizeof(struct dl);
10380 space = xmalloc(size);
10381 *tail = space;
10382 tail = space;
10383 *tail = NULL;
10384 }
10385 /* allocate memory for new device */
10386 size = sizeof_imsm_dev(super->devlist->dev, 0) +
10387 (num_members * sizeof(__u32));
10388 space = xmalloc(size);
10389 *tail = space;
10390 tail = space;
10391 *tail = NULL;
10392 len = disks_to_mpb_size(num_members * 2);
10393 }
10394
10395 break;
10396 }
10397 case update_reshape_container_disks: {
10398 /* Every raid device in the container is about to
10399 * gain some more devices, and we will enter a
10400 * reconfiguration.
10401 * So each 'imsm_map' will be bigger, and the imsm_vol
10402 * will now hold 2 of them.
10403 * Thus we need new 'struct imsm_dev' allocations sized
10404 * as sizeof_imsm_dev but with more devices in both maps.
10405 */
10406 struct imsm_update_reshape *u = (void *)update->buf;
10407 struct intel_dev *dl;
10408 void **space_tail = (void**)&update->space_list;
10409
10410 if (update->len < (int)sizeof(*u))
10411 return 0;
10412
10413 dprintf("for update_reshape\n");
10414
10415 for (dl = super->devlist; dl; dl = dl->next) {
10416 int size = sizeof_imsm_dev(dl->dev, 1);
10417 void *s;
10418 if (u->new_raid_disks > u->old_raid_disks)
10419 size += sizeof(__u32)*2*
10420 (u->new_raid_disks - u->old_raid_disks);
10421 s = xmalloc(size);
10422 *space_tail = s;
10423 space_tail = s;
10424 *space_tail = NULL;
10425 }
10426
10427 len = disks_to_mpb_size(u->new_raid_disks);
10428 dprintf("New anchor length is %llu\n", (unsigned long long)len);
10429 break;
10430 }
10431 case update_reshape_migration: {
10432 /* for migration level 0->5 we need to add disks
10433 * so the same as for container operation we will copy
10434 * device to the bigger location.
10435 * in memory prepared device and new disk area are prepared
10436 * for usage in process update
10437 */
10438 struct imsm_update_reshape_migration *u = (void *)update->buf;
10439 struct intel_dev *id;
10440 void **space_tail = (void **)&update->space_list;
10441 int size;
10442 void *s;
10443 int current_level = -1;
10444
10445 if (update->len < (int)sizeof(*u))
10446 return 0;
10447
10448 dprintf("for update_reshape\n");
10449
10450 /* add space for bigger array in update
10451 */
10452 for (id = super->devlist; id; id = id->next) {
10453 if (id->index == (unsigned)u->subdev) {
10454 size = sizeof_imsm_dev(id->dev, 1);
10455 if (u->new_raid_disks > u->old_raid_disks)
10456 size += sizeof(__u32)*2*
10457 (u->new_raid_disks - u->old_raid_disks);
10458 s = xmalloc(size);
10459 *space_tail = s;
10460 space_tail = s;
10461 *space_tail = NULL;
10462 break;
10463 }
10464 }
10465 if (update->space_list == NULL)
10466 break;
10467
10468 /* add space for disk in update
10469 */
10470 size = sizeof(struct dl);
10471 s = xmalloc(size);
10472 *space_tail = s;
10473 space_tail = s;
10474 *space_tail = NULL;
10475
10476 /* add spare device to update
10477 */
10478 for (id = super->devlist ; id; id = id->next)
10479 if (id->index == (unsigned)u->subdev) {
10480 struct imsm_dev *dev;
10481 struct imsm_map *map;
10482
10483 dev = get_imsm_dev(super, u->subdev);
10484 map = get_imsm_map(dev, MAP_0);
10485 current_level = map->raid_level;
10486 break;
10487 }
10488 if (u->new_level == 5 && u->new_level != current_level) {
10489 struct mdinfo *spares;
10490
10491 spares = get_spares_for_grow(st);
10492 if (spares) {
10493 struct dl *dl;
10494 struct mdinfo *dev;
10495
10496 dev = spares->devs;
10497 if (dev) {
10498 u->new_disks[0] =
10499 makedev(dev->disk.major,
10500 dev->disk.minor);
10501 dl = get_disk_super(super,
10502 dev->disk.major,
10503 dev->disk.minor);
10504 dl->index = u->old_raid_disks;
10505 dev = dev->next;
10506 }
10507 sysfs_free(spares);
10508 }
10509 }
10510 len = disks_to_mpb_size(u->new_raid_disks);
10511 dprintf("New anchor length is %llu\n", (unsigned long long)len);
10512 break;
10513 }
10514 case update_size_change: {
10515 if (update->len < (int)sizeof(struct imsm_update_size_change))
10516 return 0;
10517 break;
10518 }
10519 case update_activate_spare: {
10520 if (update->len < (int)sizeof(struct imsm_update_activate_spare))
10521 return 0;
10522 break;
10523 }
10524 case update_create_array: {
10525 struct imsm_update_create_array *u = (void *) update->buf;
10526 struct intel_dev *dv;
10527 struct imsm_dev *dev = &u->dev;
10528 struct imsm_map *map = get_imsm_map(dev, MAP_0);
10529 struct dl *dl;
10530 struct disk_info *inf;
10531 int i;
10532 int activate = 0;
10533
10534 if (update->len < (int)sizeof(*u))
10535 return 0;
10536
10537 inf = get_disk_info(u);
10538 len = sizeof_imsm_dev(dev, 1);
10539 /* allocate a new super->devlist entry */
10540 dv = xmalloc(sizeof(*dv));
10541 dv->dev = xmalloc(len);
10542 update->space = dv;
10543
10544 /* count how many spares will be converted to members */
10545 for (i = 0; i < map->num_members; i++) {
10546 dl = serial_to_dl(inf[i].serial, super);
10547 if (!dl) {
10548 /* hmm maybe it failed?, nothing we can do about
10549 * it here
10550 */
10551 continue;
10552 }
10553 if (count_memberships(dl, super) == 0)
10554 activate++;
10555 }
10556 len += activate * sizeof(struct imsm_disk);
10557 break;
10558 }
10559 case update_kill_array: {
10560 if (update->len < (int)sizeof(struct imsm_update_kill_array))
10561 return 0;
10562 break;
10563 }
10564 case update_rename_array: {
10565 if (update->len < (int)sizeof(struct imsm_update_rename_array))
10566 return 0;
10567 break;
10568 }
10569 case update_add_remove_disk:
10570 /* no update->len needed */
10571 break;
10572 case update_prealloc_badblocks_mem:
10573 super->extra_space += sizeof(struct bbm_log) -
10574 get_imsm_bbm_log_size(super->bbm_log);
10575 break;
10576 case update_rwh_policy: {
10577 if (update->len < (int)sizeof(struct imsm_update_rwh_policy))
10578 return 0;
10579 break;
10580 }
10581 default:
10582 return 0;
10583 }
10584
10585 /* check if we need a larger metadata buffer */
10586 if (super->next_buf)
10587 buf_len = super->next_len;
10588 else
10589 buf_len = super->len;
10590
10591 if (__le32_to_cpu(mpb->mpb_size) + super->extra_space + len > buf_len) {
10592 /* ok we need a larger buf than what is currently allocated
10593 * if this allocation fails process_update will notice that
10594 * ->next_len is set and ->next_buf is NULL
10595 */
10596 buf_len = ROUND_UP(__le32_to_cpu(mpb->mpb_size) +
10597 super->extra_space + len, sector_size);
10598 if (super->next_buf)
10599 free(super->next_buf);
10600
10601 super->next_len = buf_len;
10602 if (posix_memalign(&super->next_buf, sector_size, buf_len) == 0)
10603 memset(super->next_buf, 0, buf_len);
10604 else
10605 super->next_buf = NULL;
10606 }
10607 return 1;
10608 }
10609
10610 /* must be called while manager is quiesced */
10611 static void imsm_delete(struct intel_super *super, struct dl **dlp, unsigned index)
10612 {
10613 struct imsm_super *mpb = super->anchor;
10614 struct dl *iter;
10615 struct imsm_dev *dev;
10616 struct imsm_map *map;
10617 unsigned int i, j, num_members;
10618 __u32 ord, ord_map0;
10619 struct bbm_log *log = super->bbm_log;
10620
10621 dprintf("deleting device[%d] from imsm_super\n", index);
10622
10623 /* shift all indexes down one */
10624 for (iter = super->disks; iter; iter = iter->next)
10625 if (iter->index > (int)index)
10626 iter->index--;
10627 for (iter = super->missing; iter; iter = iter->next)
10628 if (iter->index > (int)index)
10629 iter->index--;
10630
10631 for (i = 0; i < mpb->num_raid_devs; i++) {
10632 dev = get_imsm_dev(super, i);
10633 map = get_imsm_map(dev, MAP_0);
10634 num_members = map->num_members;
10635 for (j = 0; j < num_members; j++) {
10636 /* update ord entries being careful not to propagate
10637 * ord-flags to the first map
10638 */
10639 ord = get_imsm_ord_tbl_ent(dev, j, MAP_X);
10640 ord_map0 = get_imsm_ord_tbl_ent(dev, j, MAP_0);
10641
10642 if (ord_to_idx(ord) <= index)
10643 continue;
10644
10645 map = get_imsm_map(dev, MAP_0);
10646 set_imsm_ord_tbl_ent(map, j, ord_map0 - 1);
10647 map = get_imsm_map(dev, MAP_1);
10648 if (map)
10649 set_imsm_ord_tbl_ent(map, j, ord - 1);
10650 }
10651 }
10652
10653 for (i = 0; i < log->entry_count; i++) {
10654 struct bbm_log_entry *entry = &log->marked_block_entries[i];
10655
10656 if (entry->disk_ordinal <= index)
10657 continue;
10658 entry->disk_ordinal--;
10659 }
10660
10661 mpb->num_disks--;
10662 super->updates_pending++;
10663 if (*dlp) {
10664 struct dl *dl = *dlp;
10665
10666 *dlp = (*dlp)->next;
10667 __free_imsm_disk(dl, 1);
10668 }
10669 }
10670
10671 static int imsm_get_allowed_degradation(int level, int raid_disks,
10672 struct intel_super *super,
10673 struct imsm_dev *dev)
10674 {
10675 switch (level) {
10676 case 1:
10677 case 10:{
10678 int ret_val = 0;
10679 struct imsm_map *map;
10680 int i;
10681
10682 ret_val = raid_disks/2;
10683 /* check map if all disks pairs not failed
10684 * in both maps
10685 */
10686 map = get_imsm_map(dev, MAP_0);
10687 for (i = 0; i < ret_val; i++) {
10688 int degradation = 0;
10689 if (get_imsm_disk(super, i) == NULL)
10690 degradation++;
10691 if (get_imsm_disk(super, i + 1) == NULL)
10692 degradation++;
10693 if (degradation == 2)
10694 return 0;
10695 }
10696 map = get_imsm_map(dev, MAP_1);
10697 /* if there is no second map
10698 * result can be returned
10699 */
10700 if (map == NULL)
10701 return ret_val;
10702 /* check degradation in second map
10703 */
10704 for (i = 0; i < ret_val; i++) {
10705 int degradation = 0;
10706 if (get_imsm_disk(super, i) == NULL)
10707 degradation++;
10708 if (get_imsm_disk(super, i + 1) == NULL)
10709 degradation++;
10710 if (degradation == 2)
10711 return 0;
10712 }
10713 return ret_val;
10714 }
10715 case 5:
10716 return 1;
10717 case 6:
10718 return 2;
10719 default:
10720 return 0;
10721 }
10722 }
10723
10724 /*******************************************************************************
10725 * Function: validate_container_imsm
10726 * Description: This routine validates container after assemble,
10727 * eg. if devices in container are under the same controller.
10728 *
10729 * Parameters:
10730 * info : linked list with info about devices used in array
10731 * Returns:
10732 * 1 : HBA mismatch
10733 * 0 : Success
10734 ******************************************************************************/
10735 int validate_container_imsm(struct mdinfo *info)
10736 {
10737 if (check_no_platform())
10738 return 0;
10739
10740 struct sys_dev *idev;
10741 struct sys_dev *hba = NULL;
10742 struct sys_dev *intel_devices = find_intel_devices();
10743 char *dev_path = devt_to_devpath(makedev(info->disk.major,
10744 info->disk.minor), 1, NULL);
10745
10746 for (idev = intel_devices; idev; idev = idev->next) {
10747 if (dev_path && strstr(dev_path, idev->path)) {
10748 hba = idev;
10749 break;
10750 }
10751 }
10752 if (dev_path)
10753 free(dev_path);
10754
10755 if (!hba) {
10756 pr_err("WARNING - Cannot detect HBA for device %s!\n",
10757 devid2kname(makedev(info->disk.major, info->disk.minor)));
10758 return 1;
10759 }
10760
10761 const struct imsm_orom *orom = get_orom_by_device_id(hba->dev_id);
10762 struct mdinfo *dev;
10763
10764 for (dev = info->next; dev; dev = dev->next) {
10765 dev_path = devt_to_devpath(makedev(dev->disk.major,
10766 dev->disk.minor), 1, NULL);
10767
10768 struct sys_dev *hba2 = NULL;
10769 for (idev = intel_devices; idev; idev = idev->next) {
10770 if (dev_path && strstr(dev_path, idev->path)) {
10771 hba2 = idev;
10772 break;
10773 }
10774 }
10775 if (dev_path)
10776 free(dev_path);
10777
10778 const struct imsm_orom *orom2 = hba2 == NULL ? NULL :
10779 get_orom_by_device_id(hba2->dev_id);
10780
10781 if (hba2 && hba->type != hba2->type) {
10782 pr_err("WARNING - HBAs of devices do not match %s != %s\n",
10783 get_sys_dev_type(hba->type), get_sys_dev_type(hba2->type));
10784 return 1;
10785 }
10786
10787 if (orom != orom2) {
10788 pr_err("WARNING - IMSM container assembled with disks under different HBAs!\n"
10789 " This operation is not supported and can lead to data loss.\n");
10790 return 1;
10791 }
10792
10793 if (!orom) {
10794 pr_err("WARNING - IMSM container assembled with disks under HBAs without IMSM platform support!\n"
10795 " This operation is not supported and can lead to data loss.\n");
10796 return 1;
10797 }
10798 }
10799
10800 return 0;
10801 }
10802
10803 /*******************************************************************************
10804 * Function: imsm_record_badblock
10805 * Description: This routine stores new bad block record in BBM log
10806 *
10807 * Parameters:
10808 * a : array containing a bad block
10809 * slot : disk number containing a bad block
10810 * sector : bad block sector
10811 * length : bad block sectors range
10812 * Returns:
10813 * 1 : Success
10814 * 0 : Error
10815 ******************************************************************************/
10816 static int imsm_record_badblock(struct active_array *a, int slot,
10817 unsigned long long sector, int length)
10818 {
10819 struct intel_super *super = a->container->sb;
10820 int ord;
10821 int ret;
10822
10823 ord = imsm_disk_slot_to_ord(a, slot);
10824 if (ord < 0)
10825 return 0;
10826
10827 ret = record_new_badblock(super->bbm_log, ord_to_idx(ord), sector,
10828 length);
10829 if (ret)
10830 super->updates_pending++;
10831
10832 return ret;
10833 }
10834 /*******************************************************************************
10835 * Function: imsm_clear_badblock
10836 * Description: This routine clears bad block record from BBM log
10837 *
10838 * Parameters:
10839 * a : array containing a bad block
10840 * slot : disk number containing a bad block
10841 * sector : bad block sector
10842 * length : bad block sectors range
10843 * Returns:
10844 * 1 : Success
10845 * 0 : Error
10846 ******************************************************************************/
10847 static int imsm_clear_badblock(struct active_array *a, int slot,
10848 unsigned long long sector, int length)
10849 {
10850 struct intel_super *super = a->container->sb;
10851 int ord;
10852 int ret;
10853
10854 ord = imsm_disk_slot_to_ord(a, slot);
10855 if (ord < 0)
10856 return 0;
10857
10858 ret = clear_badblock(super->bbm_log, ord_to_idx(ord), sector, length);
10859 if (ret)
10860 super->updates_pending++;
10861
10862 return ret;
10863 }
10864 /*******************************************************************************
10865 * Function: imsm_get_badblocks
10866 * Description: This routine get list of bad blocks for an array
10867 *
10868 * Parameters:
10869 * a : array
10870 * slot : disk number
10871 * Returns:
10872 * bb : structure containing bad blocks
10873 * NULL : error
10874 ******************************************************************************/
10875 static struct md_bb *imsm_get_badblocks(struct active_array *a, int slot)
10876 {
10877 int inst = a->info.container_member;
10878 struct intel_super *super = a->container->sb;
10879 struct imsm_dev *dev = get_imsm_dev(super, inst);
10880 struct imsm_map *map = get_imsm_map(dev, MAP_0);
10881 int ord;
10882
10883 ord = imsm_disk_slot_to_ord(a, slot);
10884 if (ord < 0)
10885 return NULL;
10886
10887 get_volume_badblocks(super->bbm_log, ord_to_idx(ord), pba_of_lba0(map),
10888 per_dev_array_size(map), &super->bb);
10889
10890 return &super->bb;
10891 }
10892 /*******************************************************************************
10893 * Function: examine_badblocks_imsm
10894 * Description: Prints list of bad blocks on a disk to the standard output
10895 *
10896 * Parameters:
10897 * st : metadata handler
10898 * fd : open file descriptor for device
10899 * devname : device name
10900 * Returns:
10901 * 0 : Success
10902 * 1 : Error
10903 ******************************************************************************/
10904 static int examine_badblocks_imsm(struct supertype *st, int fd, char *devname)
10905 {
10906 struct intel_super *super = st->sb;
10907 struct bbm_log *log = super->bbm_log;
10908 struct dl *d = NULL;
10909 int any = 0;
10910
10911 for (d = super->disks; d ; d = d->next) {
10912 if (strcmp(d->devname, devname) == 0)
10913 break;
10914 }
10915
10916 if ((d == NULL) || (d->index < 0)) { /* serial mismatch probably */
10917 pr_err("%s doesn't appear to be part of a raid array\n",
10918 devname);
10919 return 1;
10920 }
10921
10922 if (log != NULL) {
10923 unsigned int i;
10924 struct bbm_log_entry *entry = &log->marked_block_entries[0];
10925
10926 for (i = 0; i < log->entry_count; i++) {
10927 if (entry[i].disk_ordinal == d->index) {
10928 unsigned long long sector = __le48_to_cpu(
10929 &entry[i].defective_block_start);
10930 int cnt = entry[i].marked_count + 1;
10931
10932 if (!any) {
10933 printf("Bad-blocks on %s:\n", devname);
10934 any = 1;
10935 }
10936
10937 printf("%20llu for %d sectors\n", sector, cnt);
10938 }
10939 }
10940 }
10941
10942 if (!any)
10943 printf("No bad-blocks list configured on %s\n", devname);
10944
10945 return 0;
10946 }
10947 /*******************************************************************************
10948 * Function: init_migr_record_imsm
10949 * Description: Function inits imsm migration record
10950 * Parameters:
10951 * super : imsm internal array info
10952 * dev : device under migration
10953 * info : general array info to find the smallest device
10954 * Returns:
10955 * none
10956 ******************************************************************************/
10957 void init_migr_record_imsm(struct supertype *st, struct imsm_dev *dev,
10958 struct mdinfo *info)
10959 {
10960 struct intel_super *super = st->sb;
10961 struct migr_record *migr_rec = super->migr_rec;
10962 int new_data_disks;
10963 unsigned long long dsize, dev_sectors;
10964 long long unsigned min_dev_sectors = -1LLU;
10965 struct imsm_map *map_dest = get_imsm_map(dev, MAP_0);
10966 struct imsm_map *map_src = get_imsm_map(dev, MAP_1);
10967 unsigned long long num_migr_units;
10968 unsigned long long array_blocks;
10969 struct dl *dl_disk = NULL;
10970
10971 memset(migr_rec, 0, sizeof(struct migr_record));
10972 migr_rec->family_num = __cpu_to_le32(super->anchor->family_num);
10973
10974 /* only ascending reshape supported now */
10975 migr_rec->ascending_migr = __cpu_to_le32(1);
10976
10977 migr_rec->dest_depth_per_unit = GEN_MIGR_AREA_SIZE /
10978 max(map_dest->blocks_per_strip, map_src->blocks_per_strip);
10979 migr_rec->dest_depth_per_unit *=
10980 max(map_dest->blocks_per_strip, map_src->blocks_per_strip);
10981 new_data_disks = imsm_num_data_members(map_dest);
10982 migr_rec->blocks_per_unit =
10983 __cpu_to_le32(migr_rec->dest_depth_per_unit * new_data_disks);
10984 migr_rec->dest_depth_per_unit =
10985 __cpu_to_le32(migr_rec->dest_depth_per_unit);
10986 array_blocks = info->component_size * new_data_disks;
10987 num_migr_units =
10988 array_blocks / __le32_to_cpu(migr_rec->blocks_per_unit);
10989
10990 if (array_blocks % __le32_to_cpu(migr_rec->blocks_per_unit))
10991 num_migr_units++;
10992 set_num_migr_units(migr_rec, num_migr_units);
10993
10994 migr_rec->post_migr_vol_cap = dev->size_low;
10995 migr_rec->post_migr_vol_cap_hi = dev->size_high;
10996
10997 /* Find the smallest dev */
10998 for (dl_disk = super->disks; dl_disk ; dl_disk = dl_disk->next) {
10999 /* ignore spares in container */
11000 if (dl_disk->index < 0)
11001 continue;
11002 get_dev_size(dl_disk->fd, NULL, &dsize);
11003 dev_sectors = dsize / 512;
11004 if (dev_sectors < min_dev_sectors)
11005 min_dev_sectors = dev_sectors;
11006 }
11007 set_migr_chkp_area_pba(migr_rec, min_dev_sectors -
11008 RAID_DISK_RESERVED_BLOCKS_IMSM_HI);
11009
11010 write_imsm_migr_rec(st);
11011
11012 return;
11013 }
11014
11015 /*******************************************************************************
11016 * Function: save_backup_imsm
11017 * Description: Function saves critical data stripes to Migration Copy Area
11018 * and updates the current migration unit status.
11019 * Use restore_stripes() to form a destination stripe,
11020 * and to write it to the Copy Area.
11021 * Parameters:
11022 * st : supertype information
11023 * dev : imsm device that backup is saved for
11024 * info : general array info
11025 * buf : input buffer
11026 * length : length of data to backup (blocks_per_unit)
11027 * Returns:
11028 * 0 : success
11029 *, -1 : fail
11030 ******************************************************************************/
11031 int save_backup_imsm(struct supertype *st,
11032 struct imsm_dev *dev,
11033 struct mdinfo *info,
11034 void *buf,
11035 int length)
11036 {
11037 int rv = -1;
11038 struct intel_super *super = st->sb;
11039 int i;
11040 struct imsm_map *map_dest = get_imsm_map(dev, MAP_0);
11041 int new_disks = map_dest->num_members;
11042 int dest_layout = 0;
11043 int dest_chunk, targets[new_disks];
11044 unsigned long long start, target_offsets[new_disks];
11045 int data_disks = imsm_num_data_members(map_dest);
11046
11047 for (i = 0; i < new_disks; i++) {
11048 struct dl *dl_disk = get_imsm_dl_disk(super, i);
11049 if (dl_disk && is_fd_valid(dl_disk->fd))
11050 targets[i] = dl_disk->fd;
11051 else
11052 goto abort;
11053 }
11054
11055 start = info->reshape_progress * 512;
11056 for (i = 0; i < new_disks; i++) {
11057 target_offsets[i] = migr_chkp_area_pba(super->migr_rec) * 512;
11058 /* move back copy area adderss, it will be moved forward
11059 * in restore_stripes() using start input variable
11060 */
11061 target_offsets[i] -= start/data_disks;
11062 }
11063
11064 dest_layout = imsm_level_to_layout(map_dest->raid_level);
11065 dest_chunk = __le16_to_cpu(map_dest->blocks_per_strip) * 512;
11066
11067 if (restore_stripes(targets, /* list of dest devices */
11068 target_offsets, /* migration record offsets */
11069 new_disks,
11070 dest_chunk,
11071 map_dest->raid_level,
11072 dest_layout,
11073 -1, /* source backup file descriptor */
11074 0, /* input buf offset
11075 * always 0 buf is already offseted */
11076 start,
11077 length,
11078 buf) != 0) {
11079 pr_err("Error restoring stripes\n");
11080 goto abort;
11081 }
11082
11083 rv = 0;
11084
11085 abort:
11086 return rv;
11087 }
11088
11089 /*******************************************************************************
11090 * Function: save_checkpoint_imsm
11091 * Description: Function called for current unit status update
11092 * in the migration record. It writes it to disk.
11093 * Parameters:
11094 * super : imsm internal array info
11095 * info : general array info
11096 * Returns:
11097 * 0: success
11098 * 1: failure
11099 * 2: failure, means no valid migration record
11100 * / no general migration in progress /
11101 ******************************************************************************/
11102 int save_checkpoint_imsm(struct supertype *st, struct mdinfo *info, int state)
11103 {
11104 struct intel_super *super = st->sb;
11105 unsigned long long blocks_per_unit;
11106 unsigned long long curr_migr_unit;
11107
11108 if (load_imsm_migr_rec(super) != 0) {
11109 dprintf("imsm: ERROR: Cannot read migration record for checkpoint save.\n");
11110 return 1;
11111 }
11112
11113 blocks_per_unit = __le32_to_cpu(super->migr_rec->blocks_per_unit);
11114 if (blocks_per_unit == 0) {
11115 dprintf("imsm: no migration in progress.\n");
11116 return 2;
11117 }
11118 curr_migr_unit = info->reshape_progress / blocks_per_unit;
11119 /* check if array is alligned to copy area
11120 * if it is not alligned, add one to current migration unit value
11121 * this can happend on array reshape finish only
11122 */
11123 if (info->reshape_progress % blocks_per_unit)
11124 curr_migr_unit++;
11125
11126 set_current_migr_unit(super->migr_rec, curr_migr_unit);
11127 super->migr_rec->rec_status = __cpu_to_le32(state);
11128 set_migr_dest_1st_member_lba(super->migr_rec,
11129 super->migr_rec->dest_depth_per_unit * curr_migr_unit);
11130
11131 if (write_imsm_migr_rec(st) < 0) {
11132 dprintf("imsm: Cannot write migration record outside backup area\n");
11133 return 1;
11134 }
11135
11136 return 0;
11137 }
11138
11139 /*******************************************************************************
11140 * Function: recover_backup_imsm
11141 * Description: Function recovers critical data from the Migration Copy Area
11142 * while assembling an array.
11143 * Parameters:
11144 * super : imsm internal array info
11145 * info : general array info
11146 * Returns:
11147 * 0 : success (or there is no data to recover)
11148 * 1 : fail
11149 ******************************************************************************/
11150 int recover_backup_imsm(struct supertype *st, struct mdinfo *info)
11151 {
11152 struct intel_super *super = st->sb;
11153 struct migr_record *migr_rec = super->migr_rec;
11154 struct imsm_map *map_dest;
11155 struct intel_dev *id = NULL;
11156 unsigned long long read_offset;
11157 unsigned long long write_offset;
11158 unsigned unit_len;
11159 int new_disks, err;
11160 char *buf = NULL;
11161 int retval = 1;
11162 unsigned int sector_size = super->sector_size;
11163 unsigned long long curr_migr_unit = current_migr_unit(migr_rec);
11164 unsigned long long num_migr_units = get_num_migr_units(migr_rec);
11165 char buffer[SYSFS_MAX_BUF_SIZE];
11166 int skipped_disks = 0;
11167 struct dl *dl_disk;
11168
11169 err = sysfs_get_str(info, NULL, "array_state", (char *)buffer, sizeof(buffer));
11170 if (err < 1)
11171 return 1;
11172
11173 /* recover data only during assemblation */
11174 if (strncmp(buffer, "inactive", 8) != 0)
11175 return 0;
11176 /* no data to recover */
11177 if (__le32_to_cpu(migr_rec->rec_status) == UNIT_SRC_NORMAL)
11178 return 0;
11179 if (curr_migr_unit >= num_migr_units)
11180 return 1;
11181
11182 /* find device during reshape */
11183 for (id = super->devlist; id; id = id->next)
11184 if (is_gen_migration(id->dev))
11185 break;
11186 if (id == NULL)
11187 return 1;
11188
11189 map_dest = get_imsm_map(id->dev, MAP_0);
11190 new_disks = map_dest->num_members;
11191
11192 read_offset = migr_chkp_area_pba(migr_rec) * 512;
11193
11194 write_offset = (migr_dest_1st_member_lba(migr_rec) +
11195 pba_of_lba0(map_dest)) * 512;
11196
11197 unit_len = __le32_to_cpu(migr_rec->dest_depth_per_unit) * 512;
11198 if (posix_memalign((void **)&buf, sector_size, unit_len) != 0)
11199 goto abort;
11200
11201 for (dl_disk = super->disks; dl_disk; dl_disk = dl_disk->next) {
11202 if (dl_disk->index < 0)
11203 continue;
11204
11205 if (!is_fd_valid(dl_disk->fd)) {
11206 skipped_disks++;
11207 continue;
11208 }
11209 if (lseek64(dl_disk->fd, read_offset, SEEK_SET) < 0) {
11210 pr_err("Cannot seek to block: %s\n",
11211 strerror(errno));
11212 skipped_disks++;
11213 continue;
11214 }
11215 if (read(dl_disk->fd, buf, unit_len) != (ssize_t)unit_len) {
11216 pr_err("Cannot read copy area block: %s\n",
11217 strerror(errno));
11218 skipped_disks++;
11219 continue;
11220 }
11221 if (lseek64(dl_disk->fd, write_offset, SEEK_SET) < 0) {
11222 pr_err("Cannot seek to block: %s\n",
11223 strerror(errno));
11224 skipped_disks++;
11225 continue;
11226 }
11227 if (write(dl_disk->fd, buf, unit_len) != (ssize_t)unit_len) {
11228 pr_err("Cannot restore block: %s\n",
11229 strerror(errno));
11230 skipped_disks++;
11231 continue;
11232 }
11233 }
11234
11235 if (skipped_disks > imsm_get_allowed_degradation(info->new_level,
11236 new_disks,
11237 super,
11238 id->dev)) {
11239 pr_err("Cannot restore data from backup. Too many failed disks\n");
11240 goto abort;
11241 }
11242
11243 if (save_checkpoint_imsm(st, info, UNIT_SRC_NORMAL)) {
11244 /* ignore error == 2, this can mean end of reshape here
11245 */
11246 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL) during restart\n");
11247 } else
11248 retval = 0;
11249
11250 abort:
11251 free(buf);
11252 return retval;
11253 }
11254
11255 /**
11256 * test_and_add_drive_controller_policy_imsm() - add disk controller to policies list.
11257 * @type: Policy type to search on list.
11258 * @pols: List of currently recorded policies.
11259 * @disk_fd: File descriptor of the device to check.
11260 * @hba: The hba disk is attached, could be NULL if verification is disabled.
11261 * @verbose: verbose flag.
11262 *
11263 * IMSM cares about drive physical placement. If @hba is not set, it adds unknown policy.
11264 * If there is no controller policy on pols we are free to add first one. If there is a policy then,
11265 * new must be the same - no controller mixing allowed.
11266 */
11267 static mdadm_status_t
11268 test_and_add_drive_controller_policy_imsm(const char * const type, dev_policy_t **pols, int disk_fd,
11269 struct sys_dev *hba, const int verbose)
11270 {
11271 const char *controller_policy = get_sys_dev_type(SYS_DEV_UNKNOWN);
11272 struct dev_policy *pol = pol_find(*pols, (char *)type);
11273 char devname[MAX_RAID_SERIAL_LEN];
11274
11275 if (hba)
11276 controller_policy = get_sys_dev_type(hba->type);
11277
11278 if (!pol) {
11279 pol_add(pols, (char *)type, (char *)controller_policy, "imsm");
11280 return MDADM_STATUS_SUCCESS;
11281 }
11282
11283 if (strcmp(pol->value, controller_policy) == 0)
11284 return MDADM_STATUS_SUCCESS;
11285
11286 fd2devname(disk_fd, devname);
11287 pr_vrb("Intel(R) raid controller \"%s\" found for %s, but \"%s\" was detected earlier\n",
11288 controller_policy, devname, pol->value);
11289 pr_vrb("Disks under different controllers cannot be used, aborting\n");
11290
11291 return MDADM_STATUS_ERROR;
11292 }
11293
11294 /**
11295 * test_and_add_drive_encryption_policy_imsm() - add disk encryption to policies list.
11296 * @type: policy type to search in the list.
11297 * @pols: list of currently recorded policies.
11298 * @disk_fd: file descriptor of the device to check.
11299 * @hba: The hba to which the drive is attached, could be NULL if verification is disabled.
11300 * @verbose: verbose flag.
11301 *
11302 * IMSM cares about drive encryption state. It is not allowed to mix disks with different
11303 * encryption state within one md device.
11304 * If there is no encryption policy on pols we are free to add first one.
11305 * If there is a policy then, new must be the same.
11306 */
11307 static mdadm_status_t
11308 test_and_add_drive_encryption_policy_imsm(const char * const type, dev_policy_t **pols, int disk_fd,
11309 struct sys_dev *hba, const int verbose)
11310 {
11311 struct dev_policy *expected_policy = pol_find(*pols, (char *)type);
11312 struct encryption_information information = {0};
11313 char *encryption_state = "Unknown";
11314 int status = MDADM_STATUS_SUCCESS;
11315 bool encryption_checked = true;
11316 char devname[PATH_MAX];
11317
11318 if (!hba)
11319 goto check_policy;
11320
11321 switch (hba->type) {
11322 case SYS_DEV_NVME:
11323 case SYS_DEV_VMD:
11324 status = get_nvme_opal_encryption_information(disk_fd, &information, verbose);
11325 break;
11326 case SYS_DEV_SATA:
11327 case SYS_DEV_SATA_VMD:
11328 status = get_ata_encryption_information(disk_fd, &information, verbose);
11329 break;
11330 default:
11331 encryption_checked = false;
11332 }
11333
11334 if (status) {
11335 fd2devname(disk_fd, devname);
11336 pr_vrb("Failed to read encryption information of device %s\n", devname);
11337 return MDADM_STATUS_ERROR;
11338 }
11339
11340 if (encryption_checked) {
11341 if (information.status == ENC_STATUS_LOCKED) {
11342 fd2devname(disk_fd, devname);
11343 pr_vrb("Device %s is in Locked state, cannot use. Aborting.\n", devname);
11344 return MDADM_STATUS_ERROR;
11345 }
11346 encryption_state = (char *)get_encryption_status_string(information.status);
11347 }
11348
11349 check_policy:
11350 if (expected_policy) {
11351 if (strcmp(expected_policy->value, encryption_state) == 0)
11352 return MDADM_STATUS_SUCCESS;
11353
11354 fd2devname(disk_fd, devname);
11355 pr_vrb("Encryption status \"%s\" detected for disk %s, but \"%s\" status was detected eariler.\n",
11356 encryption_state, devname, expected_policy->value);
11357 pr_vrb("Disks with different encryption status cannot be used.\n");
11358 return MDADM_STATUS_ERROR;
11359 }
11360
11361 pol_add(pols, (char *)type, encryption_state, "imsm");
11362
11363 return MDADM_STATUS_SUCCESS;
11364 }
11365
11366 struct imsm_drive_policy {
11367 char *type;
11368 mdadm_status_t (*test_and_add_drive_policy)(const char * const type,
11369 struct dev_policy **pols, int disk_fd,
11370 struct sys_dev *hba, const int verbose);
11371 };
11372
11373 struct imsm_drive_policy imsm_policies[] = {
11374 {"controller", test_and_add_drive_controller_policy_imsm},
11375 {"encryption", test_and_add_drive_encryption_policy_imsm}
11376 };
11377
11378 mdadm_status_t test_and_add_drive_policies_imsm(struct dev_policy **pols, int disk_fd,
11379 const int verbose)
11380 {
11381 struct imsm_drive_policy *imsm_pol;
11382 struct sys_dev *hba = NULL;
11383 char path[PATH_MAX];
11384 mdadm_status_t ret;
11385 unsigned int i;
11386
11387 /* If imsm platform verification is disabled, do not search for hba. */
11388 if (check_no_platform() != 1) {
11389 if (!diskfd_to_devpath(disk_fd, 1, path)) {
11390 pr_vrb("IMSM: Failed to retrieve device path by file descriptor.\n");
11391 return MDADM_STATUS_ERROR;
11392 }
11393
11394 hba = find_disk_attached_hba(disk_fd, path);
11395 if (!hba) {
11396 pr_vrb("IMSM: Failed to find hba for %s\n", path);
11397 return MDADM_STATUS_ERROR;
11398 }
11399 }
11400
11401 for (i = 0; i < ARRAY_SIZE(imsm_policies); i++) {
11402 imsm_pol = &imsm_policies[i];
11403
11404 ret = imsm_pol->test_and_add_drive_policy(imsm_pol->type, pols, disk_fd, hba,
11405 verbose);
11406 if (ret != MDADM_STATUS_SUCCESS)
11407 /* Inherit error code */
11408 return ret;
11409 }
11410
11411 return MDADM_STATUS_SUCCESS;
11412 }
11413
11414 /**
11415 * get_spare_criteria_imsm() - set spare criteria.
11416 * @st: supertype.
11417 * @mddev_path: path to md device devnode, it must be container.
11418 * @c: spare_criteria struct to fill, not NULL.
11419 *
11420 * If superblock is not loaded, use mddev_path to load_container. It must be given in this case.
11421 * Filles size and sector size accordingly to superblock.
11422 */
11423 mdadm_status_t get_spare_criteria_imsm(struct supertype *st, char *mddev_path,
11424 struct spare_criteria *c)
11425 {
11426 mdadm_status_t ret = MDADM_STATUS_ERROR;
11427 bool free_superblock = false;
11428 unsigned long long size = 0;
11429 struct intel_super *super;
11430 struct extent *e;
11431 struct dl *dl;
11432 int i;
11433
11434 /* If no superblock and no mddev_path, we cannot load superblock. */
11435 assert(st->sb || mddev_path);
11436
11437 if (mddev_path) {
11438 int fd = open(mddev_path, O_RDONLY);
11439 mdadm_status_t rv;
11440
11441 if (!is_fd_valid(fd))
11442 return MDADM_STATUS_ERROR;
11443
11444 if (!st->sb) {
11445 if (load_container_imsm(st, fd, st->devnm)) {
11446 close(fd);
11447 return MDADM_STATUS_ERROR;
11448 }
11449 free_superblock = true;
11450 }
11451
11452 rv = mddev_test_and_add_drive_policies(st, &c->pols, fd, 0);
11453 close(fd);
11454
11455 if (rv != MDADM_STATUS_SUCCESS)
11456 goto out;
11457 }
11458
11459 super = st->sb;
11460
11461 /* find first active disk in array */
11462 dl = super->disks;
11463 while (dl && (is_failed(&dl->disk) || dl->index == -1))
11464 dl = dl->next;
11465
11466 if (!dl)
11467 goto out;
11468
11469 /* find last lba used by subarrays */
11470 e = get_extents(super, dl, 0);
11471 if (!e)
11472 goto out;
11473
11474 for (i = 0; e[i].size; i++)
11475 continue;
11476 if (i > 0)
11477 size = e[i - 1].start + e[i - 1].size;
11478 free(e);
11479
11480 /* add the amount of space needed for metadata */
11481 size += imsm_min_reserved_sectors(super);
11482
11483 c->min_size = size * 512;
11484 c->sector_size = super->sector_size;
11485 c->criteria_set = true;
11486 ret = MDADM_STATUS_SUCCESS;
11487
11488 out:
11489 if (free_superblock)
11490 free_super_imsm(st);
11491
11492 if (ret != MDADM_STATUS_SUCCESS)
11493 c->criteria_set = false;
11494
11495 return ret;
11496 }
11497
11498 static char *imsm_find_array_devnm_by_subdev(int subdev, char *container)
11499 {
11500 static char devnm[32];
11501 char subdev_name[20];
11502 struct mdstat_ent *mdstat;
11503
11504 sprintf(subdev_name, "%d", subdev);
11505 mdstat = mdstat_by_subdev(subdev_name, container);
11506 if (!mdstat)
11507 return NULL;
11508
11509 strcpy(devnm, mdstat->devnm);
11510 free_mdstat(mdstat);
11511 return devnm;
11512 }
11513
11514 static int imsm_reshape_is_allowed_on_container(struct supertype *st,
11515 struct geo_params *geo,
11516 int *old_raid_disks,
11517 int direction)
11518 {
11519 /* currently we only support increasing the number of devices
11520 * for a container. This increases the number of device for each
11521 * member array. They must all be RAID0 or RAID5.
11522 */
11523 int ret_val = 0;
11524 struct mdinfo *info, *member;
11525 int devices_that_can_grow = 0;
11526
11527 dprintf("imsm: imsm_reshape_is_allowed_on_container(ENTER): st->devnm = (%s)\n", st->devnm);
11528
11529 if (geo->size > 0 ||
11530 geo->level != UnSet ||
11531 geo->layout != UnSet ||
11532 geo->chunksize != 0 ||
11533 geo->raid_disks == UnSet) {
11534 dprintf("imsm: Container operation is allowed for raid disks number change only.\n");
11535 return ret_val;
11536 }
11537
11538 if (direction == ROLLBACK_METADATA_CHANGES) {
11539 dprintf("imsm: Metadata changes rollback is not supported for container operation.\n");
11540 return ret_val;
11541 }
11542
11543 info = container_content_imsm(st, NULL);
11544 for (member = info; member; member = member->next) {
11545 char *result;
11546
11547 dprintf("imsm: checking device_num: %i\n",
11548 member->container_member);
11549
11550 if (geo->raid_disks <= member->array.raid_disks) {
11551 /* we work on container for Online Capacity Expansion
11552 * only so raid_disks has to grow
11553 */
11554 dprintf("imsm: for container operation raid disks increase is required\n");
11555 break;
11556 }
11557
11558 if (info->array.level != 0 && info->array.level != 5) {
11559 /* we cannot use this container with other raid level
11560 */
11561 dprintf("imsm: for container operation wrong raid level (%i) detected\n",
11562 info->array.level);
11563 break;
11564 } else {
11565 /* check for platform support
11566 * for this raid level configuration
11567 */
11568 struct intel_super *super = st->sb;
11569 if (!is_raid_level_supported(super->orom,
11570 member->array.level,
11571 geo->raid_disks)) {
11572 dprintf("platform does not support raid%d with %d disk%s\n",
11573 info->array.level,
11574 geo->raid_disks,
11575 geo->raid_disks > 1 ? "s" : "");
11576 break;
11577 }
11578 /* check if component size is aligned to chunk size
11579 */
11580 if (info->component_size %
11581 (info->array.chunk_size/512)) {
11582 dprintf("Component size is not aligned to chunk size\n");
11583 break;
11584 }
11585 }
11586
11587 if (*old_raid_disks &&
11588 info->array.raid_disks != *old_raid_disks)
11589 break;
11590 *old_raid_disks = info->array.raid_disks;
11591
11592 /* All raid5 and raid0 volumes in container
11593 * have to be ready for Online Capacity Expansion
11594 * so they need to be assembled. We have already
11595 * checked that no recovery etc is happening.
11596 */
11597 result = imsm_find_array_devnm_by_subdev(member->container_member,
11598 st->container_devnm);
11599 if (result == NULL) {
11600 dprintf("imsm: cannot find array\n");
11601 break;
11602 }
11603 devices_that_can_grow++;
11604 }
11605 sysfs_free(info);
11606 if (!member && devices_that_can_grow)
11607 ret_val = 1;
11608
11609 if (ret_val)
11610 dprintf("Container operation allowed\n");
11611 else
11612 dprintf("Error: %i\n", ret_val);
11613
11614 return ret_val;
11615 }
11616
11617 /* Function: get_spares_for_grow
11618 * Description: Allocates memory and creates list of spare devices
11619 * avaliable in container. Checks if spare drive size is acceptable.
11620 * Parameters: Pointer to the supertype structure
11621 * Returns: Pointer to the list of spare devices (mdinfo structure) on success,
11622 * NULL if fail
11623 */
11624 static struct mdinfo *get_spares_for_grow(struct supertype *st)
11625 {
11626 struct spare_criteria sc = {0};
11627 struct mdinfo *spares;
11628
11629 get_spare_criteria_imsm(st, NULL, &sc);
11630 spares = container_choose_spares(st, &sc, NULL, NULL, NULL, 0);
11631
11632 dev_policy_free(sc.pols);
11633
11634 return spares;
11635 }
11636
11637 /******************************************************************************
11638 * function: imsm_create_metadata_update_for_reshape
11639 * Function creates update for whole IMSM container.
11640 *
11641 ******************************************************************************/
11642 static int imsm_create_metadata_update_for_reshape(
11643 struct supertype *st,
11644 struct geo_params *geo,
11645 int old_raid_disks,
11646 struct imsm_update_reshape **updatep)
11647 {
11648 struct intel_super *super = st->sb;
11649 struct imsm_super *mpb = super->anchor;
11650 int update_memory_size;
11651 struct imsm_update_reshape *u;
11652 struct mdinfo *spares;
11653 int i;
11654 int delta_disks;
11655 struct mdinfo *dev;
11656
11657 dprintf("(enter) raid_disks = %i\n", geo->raid_disks);
11658
11659 delta_disks = geo->raid_disks - old_raid_disks;
11660
11661 /* size of all update data without anchor */
11662 update_memory_size = sizeof(struct imsm_update_reshape);
11663
11664 /* now add space for spare disks that we need to add. */
11665 update_memory_size += sizeof(u->new_disks[0]) * (delta_disks - 1);
11666
11667 u = xcalloc(1, update_memory_size);
11668 u->type = update_reshape_container_disks;
11669 u->old_raid_disks = old_raid_disks;
11670 u->new_raid_disks = geo->raid_disks;
11671
11672 /* now get spare disks list
11673 */
11674 spares = get_spares_for_grow(st);
11675
11676 if (spares == NULL || delta_disks > spares->array.spare_disks) {
11677 pr_err("imsm: ERROR: Cannot get spare devices for %s.\n", geo->dev_name);
11678 i = -1;
11679 goto abort;
11680 }
11681
11682 /* we have got spares
11683 * update disk list in imsm_disk list table in anchor
11684 */
11685 dprintf("imsm: %i spares are available.\n\n",
11686 spares->array.spare_disks);
11687
11688 dev = spares->devs;
11689 for (i = 0; i < delta_disks; i++) {
11690 struct dl *dl;
11691
11692 if (dev == NULL)
11693 break;
11694 u->new_disks[i] = makedev(dev->disk.major,
11695 dev->disk.minor);
11696 dl = get_disk_super(super, dev->disk.major, dev->disk.minor);
11697 dl->index = mpb->num_disks;
11698 mpb->num_disks++;
11699 dev = dev->next;
11700 }
11701
11702 abort:
11703 /* free spares
11704 */
11705 sysfs_free(spares);
11706
11707 dprintf("imsm: reshape update preparation :");
11708 if (i == delta_disks) {
11709 dprintf_cont(" OK\n");
11710 *updatep = u;
11711 return update_memory_size;
11712 }
11713 free(u);
11714 dprintf_cont(" Error\n");
11715
11716 return 0;
11717 }
11718
11719 /******************************************************************************
11720 * function: imsm_create_metadata_update_for_size_change()
11721 * Creates update for IMSM array for array size change.
11722 *
11723 ******************************************************************************/
11724 static int imsm_create_metadata_update_for_size_change(
11725 struct supertype *st,
11726 struct geo_params *geo,
11727 struct imsm_update_size_change **updatep)
11728 {
11729 struct intel_super *super = st->sb;
11730 int update_memory_size;
11731 struct imsm_update_size_change *u;
11732
11733 dprintf("(enter) New size = %llu\n", geo->size);
11734
11735 /* size of all update data without anchor */
11736 update_memory_size = sizeof(struct imsm_update_size_change);
11737
11738 u = xcalloc(1, update_memory_size);
11739 u->type = update_size_change;
11740 u->subdev = super->current_vol;
11741 u->new_size = geo->size;
11742
11743 dprintf("imsm: reshape update preparation : OK\n");
11744 *updatep = u;
11745
11746 return update_memory_size;
11747 }
11748
11749 /******************************************************************************
11750 * function: imsm_create_metadata_update_for_migration()
11751 * Creates update for IMSM array.
11752 *
11753 ******************************************************************************/
11754 static int imsm_create_metadata_update_for_migration(
11755 struct supertype *st,
11756 struct geo_params *geo,
11757 struct imsm_update_reshape_migration **updatep)
11758 {
11759 struct intel_super *super = st->sb;
11760 int update_memory_size;
11761 int current_chunk_size;
11762 struct imsm_update_reshape_migration *u;
11763 struct imsm_dev *dev = get_imsm_dev(super, super->current_vol);
11764 struct imsm_map *map = get_imsm_map(dev, MAP_0);
11765 int previous_level = -1;
11766
11767 dprintf("(enter) New Level = %i\n", geo->level);
11768
11769 /* size of all update data without anchor */
11770 update_memory_size = sizeof(struct imsm_update_reshape_migration);
11771
11772 u = xcalloc(1, update_memory_size);
11773 u->type = update_reshape_migration;
11774 u->subdev = super->current_vol;
11775 u->new_level = geo->level;
11776 u->new_layout = geo->layout;
11777 u->new_raid_disks = u->old_raid_disks = geo->raid_disks;
11778 u->new_disks[0] = -1;
11779 u->new_chunksize = -1;
11780
11781 current_chunk_size = __le16_to_cpu(map->blocks_per_strip) / 2;
11782
11783 if (geo->chunksize != current_chunk_size) {
11784 u->new_chunksize = geo->chunksize / 1024;
11785 dprintf("imsm: chunk size change from %i to %i\n",
11786 current_chunk_size, u->new_chunksize);
11787 }
11788 previous_level = map->raid_level;
11789
11790 if (geo->level == 5 && previous_level == 0) {
11791 struct mdinfo *spares = NULL;
11792
11793 u->new_raid_disks++;
11794 spares = get_spares_for_grow(st);
11795 if (spares == NULL || spares->array.spare_disks < 1) {
11796 free(u);
11797 sysfs_free(spares);
11798 update_memory_size = 0;
11799 pr_err("cannot get spare device for requested migration\n");
11800 return 0;
11801 }
11802 sysfs_free(spares);
11803 }
11804 dprintf("imsm: reshape update preparation : OK\n");
11805 *updatep = u;
11806
11807 return update_memory_size;
11808 }
11809
11810 static void imsm_update_metadata_locally(struct supertype *st,
11811 void *buf, int len)
11812 {
11813 struct metadata_update mu;
11814
11815 mu.buf = buf;
11816 mu.len = len;
11817 mu.space = NULL;
11818 mu.space_list = NULL;
11819 mu.next = NULL;
11820 if (imsm_prepare_update(st, &mu))
11821 imsm_process_update(st, &mu);
11822
11823 while (mu.space_list) {
11824 void **space = mu.space_list;
11825 mu.space_list = *space;
11826 free(space);
11827 }
11828 }
11829
11830 /**
11831 * imsm_analyze_expand() - check expand properties and calculate new size.
11832 * @st: imsm supertype.
11833 * @geo: new geometry params.
11834 * @array: array info.
11835 * @direction: reshape direction.
11836 *
11837 * Obtain free space after the &array and verify if expand to requested size is
11838 * possible. If geo->size is set to %MAX_SIZE, assume that max free size is
11839 * requested.
11840 *
11841 * Return:
11842 * On success %IMSM_STATUS_OK is returned, geo->size and geo->raid_disks are
11843 * updated.
11844 * On error, %IMSM_STATUS_ERROR is returned.
11845 */
11846 static imsm_status_t imsm_analyze_expand(struct supertype *st,
11847 struct geo_params *geo,
11848 struct mdinfo *array,
11849 int direction)
11850 {
11851 struct intel_super *super = st->sb;
11852 struct imsm_dev *dev = get_imsm_dev(super, super->current_vol);
11853 struct imsm_map *map = get_imsm_map(dev, MAP_0);
11854 int data_disks = imsm_num_data_members(map);
11855
11856 unsigned long long current_size;
11857 unsigned long long free_size;
11858 unsigned long long new_size;
11859 unsigned long long max_size;
11860
11861 const int chunk_kib = geo->chunksize / 1024;
11862 imsm_status_t rv;
11863
11864 if (direction == ROLLBACK_METADATA_CHANGES) {
11865 /**
11866 * Accept size for rollback only.
11867 */
11868 new_size = geo->size * 2;
11869 goto success;
11870 }
11871
11872 if (data_disks == 0) {
11873 pr_err("imsm: Cannot retrieve data disks.\n");
11874 return IMSM_STATUS_ERROR;
11875 }
11876 current_size = array->custom_array_size / data_disks;
11877
11878 rv = imsm_get_free_size(super, dev->vol.map->num_members, 0, chunk_kib, &free_size, true);
11879 if (rv != IMSM_STATUS_OK) {
11880 pr_err("imsm: Cannot find free space for expand.\n");
11881 return IMSM_STATUS_ERROR;
11882 }
11883 max_size = round_member_size_to_mb(free_size + current_size);
11884
11885 if (geo->size == MAX_SIZE)
11886 new_size = max_size;
11887 else
11888 new_size = round_member_size_to_mb(geo->size * 2);
11889
11890 if (new_size == 0) {
11891 pr_err("imsm: Rounded requested size is 0.\n");
11892 return IMSM_STATUS_ERROR;
11893 }
11894
11895 if (new_size > max_size) {
11896 pr_err("imsm: Rounded requested size (%llu) is larger than free space available (%llu).\n",
11897 new_size, max_size);
11898 return IMSM_STATUS_ERROR;
11899 }
11900
11901 if (new_size == current_size) {
11902 pr_err("imsm: Rounded requested size (%llu) is same as current size (%llu).\n",
11903 new_size, current_size);
11904 return IMSM_STATUS_ERROR;
11905 }
11906
11907 if (new_size < current_size) {
11908 pr_err("imsm: Size reduction is not supported, rounded requested size (%llu) is smaller than current (%llu).\n",
11909 new_size, current_size);
11910 return IMSM_STATUS_ERROR;
11911 }
11912
11913 success:
11914 dprintf("imsm: New size per member is %llu.\n", new_size);
11915 geo->size = data_disks * new_size;
11916 geo->raid_disks = dev->vol.map->num_members;
11917 return IMSM_STATUS_OK;
11918 }
11919
11920 /***************************************************************************
11921 * Function: imsm_analyze_change
11922 * Description: Function analyze change for single volume
11923 * and validate if transition is supported
11924 * Parameters: Geometry parameters, supertype structure,
11925 * metadata change direction (apply/rollback)
11926 * Returns: Operation type code on success, -1 if fail
11927 ****************************************************************************/
11928 enum imsm_reshape_type imsm_analyze_change(struct supertype *st,
11929 struct geo_params *geo,
11930 int direction)
11931 {
11932 struct mdinfo info;
11933 int change = -1;
11934 int check_devs = 0;
11935 int chunk;
11936 /* number of added/removed disks in operation result */
11937 int devNumChange = 0;
11938 /* imsm compatible layout value for array geometry verification */
11939 int imsm_layout = -1;
11940 imsm_status_t rv;
11941
11942 getinfo_super_imsm_volume(st, &info, NULL);
11943 if (geo->level != info.array.level && geo->level >= 0 &&
11944 geo->level != UnSet) {
11945 switch (info.array.level) {
11946 case 0:
11947 if (geo->level == 5) {
11948 change = CH_MIGRATION;
11949 if (geo->layout != ALGORITHM_LEFT_ASYMMETRIC) {
11950 pr_err("Error. Requested Layout not supported (left-asymmetric layout is supported only)!\n");
11951 change = -1;
11952 goto analyse_change_exit;
11953 }
11954 imsm_layout = geo->layout;
11955 check_devs = 1;
11956 devNumChange = 1; /* parity disk added */
11957 } else if (geo->level == 10) {
11958 change = CH_TAKEOVER;
11959 check_devs = 1;
11960 devNumChange = 2; /* two mirrors added */
11961 imsm_layout = 0x102; /* imsm supported layout */
11962 }
11963 break;
11964 case 1:
11965 case 10:
11966 if (geo->level == 0) {
11967 change = CH_TAKEOVER;
11968 check_devs = 1;
11969 devNumChange = -(geo->raid_disks/2);
11970 imsm_layout = 0; /* imsm raid0 layout */
11971 }
11972 break;
11973 }
11974 if (change == -1) {
11975 pr_err("Error. Level Migration from %d to %d not supported!\n",
11976 info.array.level, geo->level);
11977 goto analyse_change_exit;
11978 }
11979 } else
11980 geo->level = info.array.level;
11981
11982 if (geo->layout != info.array.layout &&
11983 (geo->layout != UnSet && geo->layout != -1)) {
11984 change = CH_MIGRATION;
11985 if (info.array.layout == 0 && info.array.level == 5 &&
11986 geo->layout == 5) {
11987 /* reshape 5 -> 4 */
11988 } else if (info.array.layout == 5 && info.array.level == 5 &&
11989 geo->layout == 0) {
11990 /* reshape 4 -> 5 */
11991 geo->layout = 0;
11992 geo->level = 5;
11993 } else {
11994 pr_err("Error. Layout Migration from %d to %d not supported!\n",
11995 info.array.layout, geo->layout);
11996 change = -1;
11997 goto analyse_change_exit;
11998 }
11999 } else {
12000 geo->layout = info.array.layout;
12001 if (imsm_layout == -1)
12002 imsm_layout = info.array.layout;
12003 }
12004
12005 if (geo->chunksize > 0 && geo->chunksize != UnSet &&
12006 geo->chunksize != info.array.chunk_size) {
12007 if (info.array.level == 10) {
12008 pr_err("Error. Chunk size change for RAID 10 is not supported.\n");
12009 change = -1;
12010 goto analyse_change_exit;
12011 } else if (info.component_size % (geo->chunksize/512)) {
12012 pr_err("New chunk size (%dK) does not evenly divide device size (%lluk). Aborting...\n",
12013 geo->chunksize/1024, info.component_size/2);
12014 change = -1;
12015 goto analyse_change_exit;
12016 }
12017 change = CH_MIGRATION;
12018 } else {
12019 geo->chunksize = info.array.chunk_size;
12020 }
12021
12022 if (geo->size > 0) {
12023 if (change != -1) {
12024 pr_err("Error. Size change should be the only one at a time.\n");
12025 change = -1;
12026 goto analyse_change_exit;
12027 }
12028
12029 rv = imsm_analyze_expand(st, geo, &info, direction);
12030 if (rv != IMSM_STATUS_OK)
12031 goto analyse_change_exit;
12032 change = CH_ARRAY_SIZE;
12033 }
12034
12035 chunk = geo->chunksize / 1024;
12036 if (!validate_geometry_imsm(st,
12037 geo->level,
12038 imsm_layout,
12039 geo->raid_disks + devNumChange,
12040 &chunk,
12041 geo->size, INVALID_SECTORS,
12042 0, 0, info.consistency_policy, 1))
12043 change = -1;
12044
12045 if (check_devs) {
12046 struct intel_super *super = st->sb;
12047 struct imsm_super *mpb = super->anchor;
12048
12049 if (mpb->num_raid_devs > 1) {
12050 pr_err("Error. Cannot perform operation on %s- for this operation "
12051 "it MUST be single array in container\n", geo->dev_name);
12052 change = -1;
12053 }
12054 }
12055
12056 analyse_change_exit:
12057 if (direction == ROLLBACK_METADATA_CHANGES &&
12058 (change == CH_MIGRATION || change == CH_TAKEOVER)) {
12059 dprintf("imsm: Metadata changes rollback is not supported for migration and takeover operations.\n");
12060 change = -1;
12061 }
12062 return change;
12063 }
12064
12065 int imsm_takeover(struct supertype *st, struct geo_params *geo)
12066 {
12067 struct intel_super *super = st->sb;
12068 struct imsm_update_takeover *u;
12069
12070 u = xmalloc(sizeof(struct imsm_update_takeover));
12071
12072 u->type = update_takeover;
12073 u->subarray = super->current_vol;
12074
12075 /* 10->0 transition */
12076 if (geo->level == 0)
12077 u->direction = R10_TO_R0;
12078
12079 /* 0->10 transition */
12080 if (geo->level == 10)
12081 u->direction = R0_TO_R10;
12082
12083 /* update metadata locally */
12084 imsm_update_metadata_locally(st, u,
12085 sizeof(struct imsm_update_takeover));
12086 /* and possibly remotely */
12087 if (st->update_tail)
12088 append_metadata_update(st, u,
12089 sizeof(struct imsm_update_takeover));
12090 else
12091 free(u);
12092
12093 return 0;
12094 }
12095
12096 /* Flush size update if size calculated by num_data_stripes is higher than
12097 * imsm_dev_size to eliminate differences during reshape.
12098 * Mdmon will recalculate them correctly.
12099 * If subarray index is not set then check whole container.
12100 * Returns:
12101 * 0 - no error occurred
12102 * 1 - error detected
12103 */
12104 static int imsm_fix_size_mismatch(struct supertype *st, int subarray_index)
12105 {
12106 struct intel_super *super = st->sb;
12107 int tmp = super->current_vol;
12108 int ret_val = 1;
12109 int i;
12110
12111 for (i = 0; i < super->anchor->num_raid_devs; i++) {
12112 if (subarray_index >= 0 && i != subarray_index)
12113 continue;
12114 super->current_vol = i;
12115 struct imsm_dev *dev = get_imsm_dev(super, super->current_vol);
12116 struct imsm_map *map = get_imsm_map(dev, MAP_0);
12117 unsigned int disc_count = imsm_num_data_members(map);
12118 struct geo_params geo;
12119 struct imsm_update_size_change *update;
12120 unsigned long long calc_size = per_dev_array_size(map) * disc_count;
12121 unsigned long long d_size = imsm_dev_size(dev);
12122 int u_size;
12123
12124 if (calc_size == d_size)
12125 continue;
12126
12127 /* There is a difference, confirm that imsm_dev_size is
12128 * smaller and push update.
12129 */
12130 if (d_size > calc_size) {
12131 pr_err("imsm: dev size of subarray %d is incorrect\n",
12132 i);
12133 goto exit;
12134 }
12135 memset(&geo, 0, sizeof(struct geo_params));
12136 geo.size = d_size;
12137 u_size = imsm_create_metadata_update_for_size_change(st, &geo,
12138 &update);
12139 imsm_update_metadata_locally(st, update, u_size);
12140 if (st->update_tail) {
12141 append_metadata_update(st, update, u_size);
12142 flush_metadata_updates(st);
12143 st->update_tail = &st->updates;
12144 } else {
12145 imsm_sync_metadata(st);
12146 free(update);
12147 }
12148 }
12149 ret_val = 0;
12150 exit:
12151 super->current_vol = tmp;
12152 return ret_val;
12153 }
12154
12155 static int imsm_reshape_super(struct supertype *st, unsigned long long size,
12156 int level,
12157 int layout, int chunksize, int raid_disks,
12158 int delta_disks, char *backup, char *dev,
12159 int direction, int verbose)
12160 {
12161 int ret_val = 1;
12162 struct geo_params geo;
12163
12164 dprintf("(enter)\n");
12165
12166 memset(&geo, 0, sizeof(struct geo_params));
12167
12168 geo.dev_name = dev;
12169 strcpy(geo.devnm, st->devnm);
12170 geo.size = size;
12171 geo.level = level;
12172 geo.layout = layout;
12173 geo.chunksize = chunksize;
12174 geo.raid_disks = raid_disks;
12175 if (delta_disks != UnSet)
12176 geo.raid_disks += delta_disks;
12177
12178 dprintf("for level : %i\n", geo.level);
12179 dprintf("for raid_disks : %i\n", geo.raid_disks);
12180
12181 if (strcmp(st->container_devnm, st->devnm) == 0) {
12182 /* On container level we can only increase number of devices. */
12183 dprintf("imsm: info: Container operation\n");
12184 int old_raid_disks = 0;
12185
12186 if (imsm_reshape_is_allowed_on_container(
12187 st, &geo, &old_raid_disks, direction)) {
12188 struct imsm_update_reshape *u = NULL;
12189 int len;
12190
12191 if (imsm_fix_size_mismatch(st, -1)) {
12192 dprintf("imsm: Cannot fix size mismatch\n");
12193 goto exit_imsm_reshape_super;
12194 }
12195
12196 len = imsm_create_metadata_update_for_reshape(
12197 st, &geo, old_raid_disks, &u);
12198
12199 if (len <= 0) {
12200 dprintf("imsm: Cannot prepare update\n");
12201 goto exit_imsm_reshape_super;
12202 }
12203
12204 ret_val = 0;
12205 /* update metadata locally */
12206 imsm_update_metadata_locally(st, u, len);
12207 /* and possibly remotely */
12208 if (st->update_tail)
12209 append_metadata_update(st, u, len);
12210 else
12211 free(u);
12212
12213 } else {
12214 pr_err("(imsm) Operation is not allowed on this container\n");
12215 }
12216 } else {
12217 /* On volume level we support following operations
12218 * - takeover: raid10 -> raid0; raid0 -> raid10
12219 * - chunk size migration
12220 * - migration: raid5 -> raid0; raid0 -> raid5
12221 */
12222 struct intel_super *super = st->sb;
12223 struct intel_dev *dev = super->devlist;
12224 int change;
12225 dprintf("imsm: info: Volume operation\n");
12226 /* find requested device */
12227 while (dev) {
12228 char *devnm =
12229 imsm_find_array_devnm_by_subdev(
12230 dev->index, st->container_devnm);
12231 if (devnm && strcmp(devnm, geo.devnm) == 0)
12232 break;
12233 dev = dev->next;
12234 }
12235 if (dev == NULL) {
12236 pr_err("Cannot find %s (%s) subarray\n",
12237 geo.dev_name, geo.devnm);
12238 goto exit_imsm_reshape_super;
12239 }
12240 super->current_vol = dev->index;
12241 change = imsm_analyze_change(st, &geo, direction);
12242 switch (change) {
12243 case CH_TAKEOVER:
12244 ret_val = imsm_takeover(st, &geo);
12245 break;
12246 case CH_MIGRATION: {
12247 struct imsm_update_reshape_migration *u = NULL;
12248 int len =
12249 imsm_create_metadata_update_for_migration(
12250 st, &geo, &u);
12251 if (len < 1) {
12252 dprintf("imsm: Cannot prepare update\n");
12253 break;
12254 }
12255 ret_val = 0;
12256 /* update metadata locally */
12257 imsm_update_metadata_locally(st, u, len);
12258 /* and possibly remotely */
12259 if (st->update_tail)
12260 append_metadata_update(st, u, len);
12261 else
12262 free(u);
12263 }
12264 break;
12265 case CH_ARRAY_SIZE: {
12266 struct imsm_update_size_change *u = NULL;
12267 int len =
12268 imsm_create_metadata_update_for_size_change(
12269 st, &geo, &u);
12270 if (len < 1) {
12271 dprintf("imsm: Cannot prepare update\n");
12272 break;
12273 }
12274 ret_val = 0;
12275 /* update metadata locally */
12276 imsm_update_metadata_locally(st, u, len);
12277 /* and possibly remotely */
12278 if (st->update_tail)
12279 append_metadata_update(st, u, len);
12280 else
12281 free(u);
12282 }
12283 break;
12284 default:
12285 ret_val = 1;
12286 }
12287 }
12288
12289 exit_imsm_reshape_super:
12290 dprintf("imsm: reshape_super Exit code = %i\n", ret_val);
12291 return ret_val;
12292 }
12293
12294 #define COMPLETED_OK 0
12295 #define COMPLETED_NONE 1
12296 #define COMPLETED_DELAYED 2
12297
12298 static int read_completed(int fd, unsigned long long *val)
12299 {
12300 int ret;
12301 char buf[SYSFS_MAX_BUF_SIZE];
12302
12303 ret = sysfs_fd_get_str(fd, buf, sizeof(buf));
12304 if (ret < 0)
12305 return ret;
12306
12307 ret = COMPLETED_OK;
12308 if (str_is_none(buf) == true) {
12309 ret = COMPLETED_NONE;
12310 } else if (strncmp(buf, "delayed", 7) == 0) {
12311 ret = COMPLETED_DELAYED;
12312 } else {
12313 char *ep;
12314 *val = strtoull(buf, &ep, 0);
12315 if (ep == buf || (*ep != 0 && *ep != '\n' && *ep != ' '))
12316 ret = -1;
12317 }
12318 return ret;
12319 }
12320
12321 /*******************************************************************************
12322 * Function: wait_for_reshape_imsm
12323 * Description: Function writes new sync_max value and waits until
12324 * reshape process reach new position
12325 * Parameters:
12326 * sra : general array info
12327 * ndata : number of disks in new array's layout
12328 * Returns:
12329 * 0 : success,
12330 * 1 : there is no reshape in progress,
12331 * -1 : fail
12332 ******************************************************************************/
12333 int wait_for_reshape_imsm(struct mdinfo *sra, int ndata)
12334 {
12335 int fd = sysfs_get_fd(sra, NULL, "sync_completed");
12336 int retry = 3;
12337 unsigned long long completed;
12338 /* to_complete : new sync_max position */
12339 unsigned long long to_complete = sra->reshape_progress;
12340 unsigned long long position_to_set = to_complete / ndata;
12341
12342 if (!is_fd_valid(fd)) {
12343 dprintf("cannot open reshape_position\n");
12344 return 1;
12345 }
12346
12347 do {
12348 if (sysfs_fd_get_ll(fd, &completed) < 0) {
12349 if (!retry) {
12350 dprintf("cannot read reshape_position (no reshape in progres)\n");
12351 close(fd);
12352 return 1;
12353 }
12354 sleep_for(0, MSEC_TO_NSEC(30), true);
12355 } else
12356 break;
12357 } while (retry--);
12358
12359 if (completed > position_to_set) {
12360 dprintf("wrong next position to set %llu (%llu)\n",
12361 to_complete, position_to_set);
12362 close(fd);
12363 return -1;
12364 }
12365 dprintf("Position set: %llu\n", position_to_set);
12366 if (sysfs_set_num(sra, NULL, "sync_max",
12367 position_to_set) != 0) {
12368 dprintf("cannot set reshape position to %llu\n",
12369 position_to_set);
12370 close(fd);
12371 return -1;
12372 }
12373
12374 do {
12375 int rc;
12376 char action[SYSFS_MAX_BUF_SIZE];
12377 int timeout = 3000;
12378
12379 sysfs_wait(fd, &timeout);
12380 if (sysfs_get_str(sra, NULL, "sync_action",
12381 action, sizeof(action)) > 0 &&
12382 strncmp(action, "reshape", 7) != 0) {
12383 if (strncmp(action, "idle", 4) == 0)
12384 break;
12385 close(fd);
12386 return -1;
12387 }
12388
12389 rc = read_completed(fd, &completed);
12390 if (rc < 0) {
12391 dprintf("cannot read reshape_position (in loop)\n");
12392 close(fd);
12393 return 1;
12394 } else if (rc == COMPLETED_NONE)
12395 break;
12396 } while (completed < position_to_set);
12397
12398 close(fd);
12399 return 0;
12400 }
12401
12402 /*******************************************************************************
12403 * Function: check_degradation_change
12404 * Description: Check that array hasn't become failed.
12405 * Parameters:
12406 * info : for sysfs access
12407 * sources : source disks descriptors
12408 * degraded: previous degradation level
12409 * Returns:
12410 * degradation level
12411 ******************************************************************************/
12412 int check_degradation_change(struct mdinfo *info,
12413 int *sources,
12414 int degraded)
12415 {
12416 unsigned long long new_degraded;
12417 int rv;
12418
12419 rv = sysfs_get_ll(info, NULL, "degraded", &new_degraded);
12420 if (rv == -1 || (new_degraded != (unsigned long long)degraded)) {
12421 /* check each device to ensure it is still working */
12422 struct mdinfo *sd;
12423 new_degraded = 0;
12424 for (sd = info->devs ; sd ; sd = sd->next) {
12425 if (sd->disk.state & (1<<MD_DISK_FAULTY))
12426 continue;
12427 if (sd->disk.state & (1<<MD_DISK_SYNC)) {
12428 char sbuf[SYSFS_MAX_BUF_SIZE];
12429 int raid_disk = sd->disk.raid_disk;
12430
12431 if (sysfs_get_str(info,
12432 sd, "state", sbuf, sizeof(sbuf)) < 0 ||
12433 strstr(sbuf, "faulty") ||
12434 strstr(sbuf, "in_sync") == NULL) {
12435 /* this device is dead */
12436 sd->disk.state = (1<<MD_DISK_FAULTY);
12437 if (raid_disk >= 0)
12438 close_fd(&sources[raid_disk]);
12439 new_degraded++;
12440 }
12441 }
12442 }
12443 }
12444
12445 return new_degraded;
12446 }
12447
12448 /*******************************************************************************
12449 * Function: imsm_manage_reshape
12450 * Description: Function finds array under reshape and it manages reshape
12451 * process. It creates stripes backups (if required) and sets
12452 * checkpoints.
12453 * Parameters:
12454 * afd : Backup handle (nattive) - not used
12455 * sra : general array info
12456 * reshape : reshape parameters - not used
12457 * st : supertype structure
12458 * blocks : size of critical section [blocks]
12459 * fds : table of source device descriptor
12460 * offsets : start of array (offest per devices)
12461 * dests : not used
12462 * destfd : table of destination device descriptor
12463 * destoffsets : table of destination offsets (per device)
12464 * Returns:
12465 * 1 : success, reshape is done
12466 * 0 : fail
12467 ******************************************************************************/
12468 static int imsm_manage_reshape(
12469 int afd, struct mdinfo *sra, struct reshape *reshape,
12470 struct supertype *st, unsigned long backup_blocks,
12471 int *fds, unsigned long long *offsets,
12472 int dests, int *destfd, unsigned long long *destoffsets)
12473 {
12474 int ret_val = 0;
12475 struct intel_super *super = st->sb;
12476 struct intel_dev *dv;
12477 unsigned int sector_size = super->sector_size;
12478 struct imsm_dev *dev = NULL;
12479 struct imsm_map *map_src, *map_dest;
12480 int migr_vol_qan = 0;
12481 int ndata, odata; /* [bytes] */
12482 int chunk; /* [bytes] */
12483 struct migr_record *migr_rec;
12484 char *buf = NULL;
12485 unsigned int buf_size; /* [bytes] */
12486 unsigned long long max_position; /* array size [bytes] */
12487 unsigned long long next_step; /* [blocks]/[bytes] */
12488 unsigned long long old_data_stripe_length;
12489 unsigned long long start_src; /* [bytes] */
12490 unsigned long long start; /* [bytes] */
12491 unsigned long long start_buf_shift; /* [bytes] */
12492 int degraded = 0;
12493 int source_layout = 0;
12494 int subarray_index = -1;
12495
12496 if (!sra)
12497 return ret_val;
12498
12499 if (!fds || !offsets)
12500 goto abort;
12501
12502 /* Find volume during the reshape */
12503 for (dv = super->devlist; dv; dv = dv->next) {
12504 if (dv->dev->vol.migr_type == MIGR_GEN_MIGR &&
12505 dv->dev->vol.migr_state == 1) {
12506 dev = dv->dev;
12507 migr_vol_qan++;
12508 subarray_index = dv->index;
12509 }
12510 }
12511 /* Only one volume can migrate at the same time */
12512 if (migr_vol_qan != 1) {
12513 pr_err("%s", migr_vol_qan ?
12514 "Number of migrating volumes greater than 1\n" :
12515 "There is no volume during migrationg\n");
12516 goto abort;
12517 }
12518
12519 map_dest = get_imsm_map(dev, MAP_0);
12520 map_src = get_imsm_map(dev, MAP_1);
12521 if (map_src == NULL)
12522 goto abort;
12523
12524 ndata = imsm_num_data_members(map_dest);
12525 odata = imsm_num_data_members(map_src);
12526
12527 chunk = __le16_to_cpu(map_src->blocks_per_strip) * 512;
12528 old_data_stripe_length = odata * chunk;
12529
12530 migr_rec = super->migr_rec;
12531
12532 /* initialize migration record for start condition */
12533 if (sra->reshape_progress == 0)
12534 init_migr_record_imsm(st, dev, sra);
12535 else {
12536 if (__le32_to_cpu(migr_rec->rec_status) != UNIT_SRC_NORMAL) {
12537 dprintf("imsm: cannot restart migration when data are present in copy area.\n");
12538 goto abort;
12539 }
12540 /* Save checkpoint to update migration record for current
12541 * reshape position (in md). It can be farther than current
12542 * reshape position in metadata.
12543 */
12544 if (save_checkpoint_imsm(st, sra, UNIT_SRC_NORMAL) == 1) {
12545 /* ignore error == 2, this can mean end of reshape here
12546 */
12547 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL, initial save)\n");
12548 goto abort;
12549 }
12550 }
12551
12552 /* size for data */
12553 buf_size = __le32_to_cpu(migr_rec->blocks_per_unit) * 512;
12554 /* extend buffer size for parity disk */
12555 buf_size += __le32_to_cpu(migr_rec->dest_depth_per_unit) * 512;
12556 /* add space for stripe alignment */
12557 buf_size += old_data_stripe_length;
12558 if (posix_memalign((void **)&buf, MAX_SECTOR_SIZE, buf_size)) {
12559 dprintf("imsm: Cannot allocate checkpoint buffer\n");
12560 goto abort;
12561 }
12562
12563 max_position = sra->component_size * ndata;
12564 source_layout = imsm_level_to_layout(map_src->raid_level);
12565
12566 while (current_migr_unit(migr_rec) <
12567 get_num_migr_units(migr_rec)) {
12568 /* current reshape position [blocks] */
12569 unsigned long long current_position =
12570 __le32_to_cpu(migr_rec->blocks_per_unit)
12571 * current_migr_unit(migr_rec);
12572 unsigned long long border;
12573
12574 /* Check that array hasn't become failed.
12575 */
12576 degraded = check_degradation_change(sra, fds, degraded);
12577 if (degraded > 1) {
12578 dprintf("imsm: Abort reshape due to degradation level (%i)\n", degraded);
12579 goto abort;
12580 }
12581
12582 next_step = __le32_to_cpu(migr_rec->blocks_per_unit);
12583
12584 if ((current_position + next_step) > max_position)
12585 next_step = max_position - current_position;
12586
12587 start = current_position * 512;
12588
12589 /* align reading start to old geometry */
12590 start_buf_shift = start % old_data_stripe_length;
12591 start_src = start - start_buf_shift;
12592
12593 border = (start_src / odata) - (start / ndata);
12594 border /= 512;
12595 if (border <= __le32_to_cpu(migr_rec->dest_depth_per_unit)) {
12596 /* save critical stripes to buf
12597 * start - start address of current unit
12598 * to backup [bytes]
12599 * start_src - start address of current unit
12600 * to backup alligned to source array
12601 * [bytes]
12602 */
12603 unsigned long long next_step_filler;
12604 unsigned long long copy_length = next_step * 512;
12605
12606 /* allign copy area length to stripe in old geometry */
12607 next_step_filler = ((copy_length + start_buf_shift)
12608 % old_data_stripe_length);
12609 if (next_step_filler)
12610 next_step_filler = (old_data_stripe_length
12611 - next_step_filler);
12612 dprintf("save_stripes() parameters: start = %llu,\tstart_src = %llu,\tnext_step*512 = %llu,\tstart_in_buf_shift = %llu,\tnext_step_filler = %llu\n",
12613 start, start_src, copy_length,
12614 start_buf_shift, next_step_filler);
12615
12616 if (save_stripes(fds, offsets, map_src->num_members,
12617 chunk, map_src->raid_level,
12618 source_layout, 0, NULL, start_src,
12619 copy_length +
12620 next_step_filler + start_buf_shift,
12621 buf)) {
12622 dprintf("imsm: Cannot save stripes to buffer\n");
12623 goto abort;
12624 }
12625 /* Convert data to destination format and store it
12626 * in backup general migration area
12627 */
12628 if (save_backup_imsm(st, dev, sra,
12629 buf + start_buf_shift, copy_length)) {
12630 dprintf("imsm: Cannot save stripes to target devices\n");
12631 goto abort;
12632 }
12633 if (save_checkpoint_imsm(st, sra,
12634 UNIT_SRC_IN_CP_AREA)) {
12635 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_IN_CP_AREA)\n");
12636 goto abort;
12637 }
12638 } else {
12639 /* set next step to use whole border area */
12640 border /= next_step;
12641 if (border > 1)
12642 next_step *= border;
12643 }
12644 /* When data backed up, checkpoint stored,
12645 * kick the kernel to reshape unit of data
12646 */
12647 next_step = next_step + sra->reshape_progress;
12648 /* limit next step to array max position */
12649 if (next_step > max_position)
12650 next_step = max_position;
12651 sysfs_set_num(sra, NULL, "suspend_lo", sra->reshape_progress);
12652 sysfs_set_num(sra, NULL, "suspend_hi", next_step);
12653 sra->reshape_progress = next_step;
12654
12655 /* wait until reshape finish */
12656 if (wait_for_reshape_imsm(sra, ndata)) {
12657 dprintf("wait_for_reshape_imsm returned error!\n");
12658 goto abort;
12659 }
12660 if (sigterm)
12661 goto abort;
12662
12663 if (save_checkpoint_imsm(st, sra, UNIT_SRC_NORMAL) == 1) {
12664 /* ignore error == 2, this can mean end of reshape here
12665 */
12666 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL)\n");
12667 goto abort;
12668 }
12669
12670 }
12671
12672 /* clear migr_rec on disks after successful migration */
12673 struct dl *d;
12674
12675 memset(super->migr_rec_buf, 0, MIGR_REC_BUF_SECTORS*MAX_SECTOR_SIZE);
12676 for (d = super->disks; d; d = d->next) {
12677 if (d->index < 0 || is_failed(&d->disk))
12678 continue;
12679 unsigned long long dsize;
12680
12681 get_dev_size(d->fd, NULL, &dsize);
12682 if (lseek64(d->fd, dsize - MIGR_REC_SECTOR_POSITION*sector_size,
12683 SEEK_SET) >= 0) {
12684 if ((unsigned int)write(d->fd, super->migr_rec_buf,
12685 MIGR_REC_BUF_SECTORS*sector_size) !=
12686 MIGR_REC_BUF_SECTORS*sector_size)
12687 perror("Write migr_rec failed");
12688 }
12689 }
12690
12691 /* return '1' if done */
12692 ret_val = 1;
12693
12694 /* After the reshape eliminate size mismatch in metadata.
12695 * Don't update md/component_size here, volume hasn't
12696 * to take whole space. It is allowed by kernel.
12697 * md/component_size will be set propoperly after next assembly.
12698 */
12699 imsm_fix_size_mismatch(st, subarray_index);
12700
12701 abort:
12702 free(buf);
12703 /* See Grow.c: abort_reshape() for further explanation */
12704 sysfs_set_num(sra, NULL, "suspend_lo", 0x7FFFFFFFFFFFFFFFULL);
12705 sysfs_set_num(sra, NULL, "suspend_hi", 0);
12706 sysfs_set_num(sra, NULL, "suspend_lo", 0);
12707
12708 return ret_val;
12709 }
12710
12711 /*******************************************************************************
12712 * Function: calculate_bitmap_min_chunksize
12713 * Description: Calculates the minimal valid bitmap chunk size
12714 * Parameters:
12715 * max_bits : indicate how many bits can be used for the bitmap
12716 * data_area_size : the size of the data area covered by the bitmap
12717 *
12718 * Returns:
12719 * The bitmap chunk size
12720 ******************************************************************************/
12721 static unsigned long long
12722 calculate_bitmap_min_chunksize(unsigned long long max_bits,
12723 unsigned long long data_area_size)
12724 {
12725 unsigned long long min_chunk =
12726 4096; /* sub-page chunks don't work yet.. */
12727 unsigned long long bits = data_area_size / min_chunk + 1;
12728
12729 while (bits > max_bits) {
12730 min_chunk *= 2;
12731 bits = (bits + 1) / 2;
12732 }
12733 return min_chunk;
12734 }
12735
12736 /*******************************************************************************
12737 * Function: calculate_bitmap_chunksize
12738 * Description: Calculates the bitmap chunk size for the given device
12739 * Parameters:
12740 * st : supertype information
12741 * dev : device for the bitmap
12742 *
12743 * Returns:
12744 * The bitmap chunk size
12745 ******************************************************************************/
12746 static unsigned long long calculate_bitmap_chunksize(struct supertype *st,
12747 struct imsm_dev *dev)
12748 {
12749 struct intel_super *super = st->sb;
12750 unsigned long long min_chunksize;
12751 unsigned long long result = IMSM_DEFAULT_BITMAP_CHUNKSIZE;
12752 size_t dev_size = imsm_dev_size(dev);
12753
12754 min_chunksize = calculate_bitmap_min_chunksize(
12755 IMSM_BITMAP_AREA_SIZE * super->sector_size, dev_size);
12756
12757 if (result < min_chunksize)
12758 result = min_chunksize;
12759
12760 return result;
12761 }
12762
12763 /*******************************************************************************
12764 * Function: init_bitmap_header
12765 * Description: Initialize the bitmap header structure
12766 * Parameters:
12767 * st : supertype information
12768 * bms : bitmap header struct to initialize
12769 * dev : device for the bitmap
12770 *
12771 * Returns:
12772 * 0 : success
12773 * -1 : fail
12774 ******************************************************************************/
12775 static int init_bitmap_header(struct supertype *st, struct bitmap_super_s *bms,
12776 struct imsm_dev *dev)
12777 {
12778 int vol_uuid[4];
12779
12780 if (!bms || !dev)
12781 return -1;
12782
12783 bms->magic = __cpu_to_le32(BITMAP_MAGIC);
12784 bms->version = __cpu_to_le32(BITMAP_MAJOR_HI);
12785 bms->daemon_sleep = __cpu_to_le32(IMSM_DEFAULT_BITMAP_DAEMON_SLEEP);
12786 bms->sync_size = __cpu_to_le64(IMSM_BITMAP_AREA_SIZE);
12787 bms->write_behind = __cpu_to_le32(0);
12788
12789 uuid_from_super_imsm(st, vol_uuid);
12790 memcpy(bms->uuid, vol_uuid, 16);
12791
12792 bms->chunksize = calculate_bitmap_chunksize(st, dev);
12793
12794 return 0;
12795 }
12796
12797 /*******************************************************************************
12798 * Function: validate_internal_bitmap_for_drive
12799 * Description: Verify if the bitmap header for a given drive.
12800 * Parameters:
12801 * st : supertype information
12802 * offset : The offset from the beginning of the drive where to look for
12803 * the bitmap header.
12804 * d : the drive info
12805 *
12806 * Returns:
12807 * 0 : success
12808 * -1 : fail
12809 ******************************************************************************/
12810 static int validate_internal_bitmap_for_drive(struct supertype *st,
12811 unsigned long long offset,
12812 struct dl *d)
12813 {
12814 struct intel_super *super = st->sb;
12815 int ret = -1;
12816 int vol_uuid[4];
12817 bitmap_super_t *bms;
12818 int fd;
12819
12820 if (!d)
12821 return -1;
12822
12823 void *read_buf;
12824
12825 if (posix_memalign(&read_buf, MAX_SECTOR_SIZE, IMSM_BITMAP_HEADER_SIZE))
12826 return -1;
12827
12828 fd = d->fd;
12829 if (!is_fd_valid(fd)) {
12830 fd = open(d->devname, O_RDONLY, 0);
12831
12832 if (!is_fd_valid(fd)) {
12833 dprintf("cannot open the device %s\n", d->devname);
12834 goto abort;
12835 }
12836 }
12837
12838 if (lseek64(fd, offset * super->sector_size, SEEK_SET) < 0)
12839 goto abort;
12840 if (read(fd, read_buf, IMSM_BITMAP_HEADER_SIZE) !=
12841 IMSM_BITMAP_HEADER_SIZE)
12842 goto abort;
12843
12844 uuid_from_super_imsm(st, vol_uuid);
12845
12846 bms = read_buf;
12847 if ((bms->magic != __cpu_to_le32(BITMAP_MAGIC)) ||
12848 (bms->version != __cpu_to_le32(BITMAP_MAJOR_HI)) ||
12849 (!same_uuid((int *)bms->uuid, vol_uuid, st->ss->swapuuid))) {
12850 dprintf("wrong bitmap header detected\n");
12851 goto abort;
12852 }
12853
12854 ret = 0;
12855 abort:
12856 if (!is_fd_valid(d->fd))
12857 close_fd(&fd);
12858
12859 if (read_buf)
12860 free(read_buf);
12861
12862 return ret;
12863 }
12864
12865 /*******************************************************************************
12866 * Function: validate_internal_bitmap_imsm
12867 * Description: Verify if the bitmap header is in place and with proper data.
12868 * Parameters:
12869 * st : supertype information
12870 *
12871 * Returns:
12872 * 0 : success or device w/o RWH_BITMAP
12873 * -1 : fail
12874 ******************************************************************************/
12875 static int validate_internal_bitmap_imsm(struct supertype *st)
12876 {
12877 struct intel_super *super = st->sb;
12878 struct imsm_dev *dev = get_imsm_dev(super, super->current_vol);
12879 unsigned long long offset;
12880 struct dl *d;
12881
12882 if (dev->rwh_policy != RWH_BITMAP)
12883 return 0;
12884
12885 offset = get_bitmap_header_sector(super, super->current_vol);
12886 for (d = super->disks; d; d = d->next) {
12887 if (d->index < 0 || is_failed(&d->disk))
12888 continue;
12889
12890 if (validate_internal_bitmap_for_drive(st, offset, d)) {
12891 pr_err("imsm: bitmap validation failed\n");
12892 return -1;
12893 }
12894 }
12895 return 0;
12896 }
12897
12898 /*******************************************************************************
12899 * Function: add_internal_bitmap_imsm
12900 * Description: Mark the volume to use the bitmap and updates the chunk size value.
12901 * Parameters:
12902 * st : supertype information
12903 * chunkp : bitmap chunk size
12904 * delay : not used for imsm
12905 * write_behind : not used for imsm
12906 * size : not used for imsm
12907 * may_change : not used for imsm
12908 * amajor : not used for imsm
12909 *
12910 * Returns:
12911 * 0 : success
12912 * -1 : fail
12913 ******************************************************************************/
12914 static int add_internal_bitmap_imsm(struct supertype *st, int *chunkp,
12915 int delay, int write_behind,
12916 unsigned long long size, int may_change,
12917 int amajor)
12918 {
12919 struct intel_super *super = st->sb;
12920 int vol_idx = super->current_vol;
12921 struct imsm_dev *dev;
12922
12923 if (!super->devlist || vol_idx == -1 || !chunkp)
12924 return -1;
12925
12926 dev = get_imsm_dev(super, vol_idx);
12927 dev->rwh_policy = RWH_BITMAP;
12928 *chunkp = calculate_bitmap_chunksize(st, dev);
12929 return 0;
12930 }
12931
12932 /*******************************************************************************
12933 * Function: locate_bitmap_imsm
12934 * Description: Seek 'fd' to start of write-intent-bitmap.
12935 * Parameters:
12936 * st : supertype information
12937 * fd : file descriptor for the device
12938 * node_num : not used for imsm
12939 *
12940 * Returns:
12941 * 0 : success
12942 * -1 : fail
12943 ******************************************************************************/
12944 static int locate_bitmap_imsm(struct supertype *st, int fd, int node_num)
12945 {
12946 struct intel_super *super = st->sb;
12947 unsigned long long offset;
12948 int vol_idx = super->current_vol;
12949
12950 if (!super->devlist || vol_idx == -1)
12951 return -1;
12952
12953 offset = get_bitmap_header_sector(super, super->current_vol);
12954 dprintf("bitmap header offset is %llu\n", offset);
12955
12956 lseek64(fd, offset << 9, 0);
12957
12958 return 0;
12959 }
12960
12961 /*******************************************************************************
12962 * Function: write_init_bitmap_imsm
12963 * Description: Write a bitmap header and prepares the area for the bitmap.
12964 * Parameters:
12965 * st : supertype information
12966 * fd : file descriptor for the device
12967 * update : not used for imsm
12968 *
12969 * Returns:
12970 * 0 : success
12971 * -1 : fail
12972 ******************************************************************************/
12973 static int write_init_bitmap_imsm(struct supertype *st, int fd,
12974 enum bitmap_update update)
12975 {
12976 struct intel_super *super = st->sb;
12977 int vol_idx = super->current_vol;
12978 int ret = 0;
12979 unsigned long long offset;
12980 bitmap_super_t bms = { 0 };
12981 size_t written = 0;
12982 size_t to_write;
12983 ssize_t rv_num;
12984 void *buf;
12985
12986 if (!super->devlist || !super->sector_size || vol_idx == -1)
12987 return -1;
12988
12989 struct imsm_dev *dev = get_imsm_dev(super, vol_idx);
12990
12991 /* first clear the space for bitmap header */
12992 unsigned long long bitmap_area_start =
12993 get_bitmap_header_sector(super, vol_idx);
12994
12995 dprintf("zeroing area start (%llu) and size (%u)\n", bitmap_area_start,
12996 IMSM_BITMAP_AND_HEADER_SIZE / super->sector_size);
12997 if (zero_disk_range(fd, bitmap_area_start,
12998 IMSM_BITMAP_HEADER_SIZE / super->sector_size)) {
12999 pr_err("imsm: cannot zeroing the space for the bitmap\n");
13000 return -1;
13001 }
13002
13003 /* The bitmap area should be filled with "1"s to perform initial
13004 * synchronization.
13005 */
13006 if (posix_memalign(&buf, MAX_SECTOR_SIZE, MAX_SECTOR_SIZE))
13007 return -1;
13008 memset(buf, 0xFF, MAX_SECTOR_SIZE);
13009 offset = get_bitmap_sector(super, vol_idx);
13010 lseek64(fd, offset << 9, 0);
13011 while (written < IMSM_BITMAP_AREA_SIZE) {
13012 to_write = IMSM_BITMAP_AREA_SIZE - written;
13013 if (to_write > MAX_SECTOR_SIZE)
13014 to_write = MAX_SECTOR_SIZE;
13015 rv_num = write(fd, buf, MAX_SECTOR_SIZE);
13016 if (rv_num != MAX_SECTOR_SIZE) {
13017 ret = -1;
13018 dprintf("cannot initialize bitmap area\n");
13019 goto abort;
13020 }
13021 written += rv_num;
13022 }
13023
13024 /* write a bitmap header */
13025 init_bitmap_header(st, &bms, dev);
13026 memset(buf, 0, MAX_SECTOR_SIZE);
13027 memcpy(buf, &bms, sizeof(bitmap_super_t));
13028 if (locate_bitmap_imsm(st, fd, 0)) {
13029 ret = -1;
13030 dprintf("cannot locate the bitmap\n");
13031 goto abort;
13032 }
13033 if (write(fd, buf, MAX_SECTOR_SIZE) != MAX_SECTOR_SIZE) {
13034 ret = -1;
13035 dprintf("cannot write the bitmap header\n");
13036 goto abort;
13037 }
13038 fsync(fd);
13039
13040 abort:
13041 free(buf);
13042
13043 return ret;
13044 }
13045
13046 /*******************************************************************************
13047 * Function: is_vol_to_setup_bitmap
13048 * Description: Checks if a bitmap should be activated on the dev.
13049 * Parameters:
13050 * info : info about the volume to setup the bitmap
13051 * dev : the device to check against bitmap creation
13052 *
13053 * Returns:
13054 * 0 : bitmap should be set up on the device
13055 * -1 : otherwise
13056 ******************************************************************************/
13057 static int is_vol_to_setup_bitmap(struct mdinfo *info, struct imsm_dev *dev)
13058 {
13059 if (!dev || !info)
13060 return -1;
13061
13062 if ((strcmp((char *)dev->volume, info->name) == 0) &&
13063 (dev->rwh_policy == RWH_BITMAP))
13064 return -1;
13065
13066 return 0;
13067 }
13068
13069 /*******************************************************************************
13070 * Function: set_bitmap_sysfs
13071 * Description: Set the sysfs atributes of a given volume to activate the bitmap.
13072 * Parameters:
13073 * info : info about the volume where the bitmap should be setup
13074 * chunksize : bitmap chunk size
13075 * location : location of the bitmap
13076 *
13077 * Returns:
13078 * 0 : success
13079 * -1 : fail
13080 ******************************************************************************/
13081 static int set_bitmap_sysfs(struct mdinfo *info, unsigned long long chunksize,
13082 char *location)
13083 {
13084 /* The bitmap/metadata is set to external to allow changing of value for
13085 * bitmap/location. When external is used, the kernel will treat an offset
13086 * related to the device's first lba (in opposition to the "internal" case
13087 * when this value is related to the beginning of the superblock).
13088 */
13089 if (sysfs_set_str(info, NULL, "bitmap/metadata", "external")) {
13090 dprintf("failed to set bitmap/metadata\n");
13091 return -1;
13092 }
13093
13094 /* It can only be changed when no bitmap is active.
13095 * Should be bigger than 512 and must be power of 2.
13096 * It is expecting the value in bytes.
13097 */
13098 if (sysfs_set_num(info, NULL, "bitmap/chunksize",
13099 __cpu_to_le32(chunksize))) {
13100 dprintf("failed to set bitmap/chunksize\n");
13101 return -1;
13102 }
13103
13104 /* It is expecting the value in sectors. */
13105 if (sysfs_set_num(info, NULL, "bitmap/space",
13106 __cpu_to_le64(IMSM_BITMAP_AREA_SIZE))) {
13107 dprintf("failed to set bitmap/space\n");
13108 return -1;
13109 }
13110
13111 /* Determines the delay between the bitmap updates.
13112 * It is expecting the value in seconds.
13113 */
13114 if (sysfs_set_num(info, NULL, "bitmap/time_base",
13115 __cpu_to_le64(IMSM_DEFAULT_BITMAP_DAEMON_SLEEP))) {
13116 dprintf("failed to set bitmap/time_base\n");
13117 return -1;
13118 }
13119
13120 /* It is expecting the value in sectors with a sign at the beginning. */
13121 if (sysfs_set_str(info, NULL, "bitmap/location", location)) {
13122 dprintf("failed to set bitmap/location\n");
13123 return -1;
13124 }
13125
13126 return 0;
13127 }
13128
13129 /*******************************************************************************
13130 * Function: set_bitmap_imsm
13131 * Description: Setup the bitmap for the given volume
13132 * Parameters:
13133 * st : supertype information
13134 * info : info about the volume where the bitmap should be setup
13135 *
13136 * Returns:
13137 * 0 : success
13138 * -1 : fail
13139 ******************************************************************************/
13140 static int set_bitmap_imsm(struct supertype *st, struct mdinfo *info)
13141 {
13142 struct intel_super *super = st->sb;
13143 int prev_current_vol = super->current_vol;
13144 struct imsm_dev *dev;
13145 int ret = -1;
13146 char location[16] = "";
13147 unsigned long long chunksize;
13148 struct intel_dev *dev_it;
13149
13150 for (dev_it = super->devlist; dev_it; dev_it = dev_it->next) {
13151 super->current_vol = dev_it->index;
13152 dev = get_imsm_dev(super, super->current_vol);
13153
13154 if (is_vol_to_setup_bitmap(info, dev)) {
13155 if (validate_internal_bitmap_imsm(st)) {
13156 dprintf("bitmap header validation failed\n");
13157 goto abort;
13158 }
13159
13160 chunksize = calculate_bitmap_chunksize(st, dev);
13161 dprintf("chunk size is %llu\n", chunksize);
13162
13163 snprintf(location, sizeof(location), "+%llu",
13164 get_bitmap_sector(super, super->current_vol));
13165 dprintf("bitmap offset is %s\n", location);
13166
13167 if (set_bitmap_sysfs(info, chunksize, location)) {
13168 dprintf("cannot setup the bitmap\n");
13169 goto abort;
13170 }
13171 }
13172 }
13173 ret = 0;
13174 abort:
13175 super->current_vol = prev_current_vol;
13176 return ret;
13177 }
13178
13179 struct superswitch super_imsm = {
13180 .examine_super = examine_super_imsm,
13181 .brief_examine_super = brief_examine_super_imsm,
13182 .brief_examine_subarrays = brief_examine_subarrays_imsm,
13183 .export_examine_super = export_examine_super_imsm,
13184 .detail_super = detail_super_imsm,
13185 .brief_detail_super = brief_detail_super_imsm,
13186 .write_init_super = write_init_super_imsm,
13187 .validate_geometry = validate_geometry_imsm,
13188 .add_to_super = add_to_super_imsm,
13189 .remove_from_super = remove_from_super_imsm,
13190 .detail_platform = detail_platform_imsm,
13191 .export_detail_platform = export_detail_platform_imsm,
13192 .kill_subarray = kill_subarray_imsm,
13193 .update_subarray = update_subarray_imsm,
13194 .load_container = load_container_imsm,
13195 .default_geometry = default_geometry_imsm,
13196 .test_and_add_drive_policies = test_and_add_drive_policies_imsm,
13197 .reshape_super = imsm_reshape_super,
13198 .manage_reshape = imsm_manage_reshape,
13199 .recover_backup = recover_backup_imsm,
13200 .examine_badblocks = examine_badblocks_imsm,
13201 .match_home = match_home_imsm,
13202 .uuid_from_super= uuid_from_super_imsm,
13203 .getinfo_super = getinfo_super_imsm,
13204 .getinfo_super_disks = getinfo_super_disks_imsm,
13205 .update_super = update_super_imsm,
13206
13207 .avail_size = avail_size_imsm,
13208 .get_spare_criteria = get_spare_criteria_imsm,
13209
13210 .compare_super = compare_super_imsm,
13211
13212 .load_super = load_super_imsm,
13213 .init_super = init_super_imsm,
13214 .store_super = store_super_imsm,
13215 .free_super = free_super_imsm,
13216 .match_metadata_desc = match_metadata_desc_imsm,
13217 .container_content = container_content_imsm,
13218 .validate_container = validate_container_imsm,
13219
13220 .add_internal_bitmap = add_internal_bitmap_imsm,
13221 .locate_bitmap = locate_bitmap_imsm,
13222 .write_bitmap = write_init_bitmap_imsm,
13223 .set_bitmap = set_bitmap_imsm,
13224
13225 .write_init_ppl = write_init_ppl_imsm,
13226 .validate_ppl = validate_ppl_imsm,
13227
13228 .external = 1,
13229 .swapuuid = 0,
13230 .name = "imsm",
13231
13232 /* for mdmon */
13233 .open_new = imsm_open_new,
13234 .set_array_state= imsm_set_array_state,
13235 .set_disk = imsm_set_disk,
13236 .sync_metadata = imsm_sync_metadata,
13237 .activate_spare = imsm_activate_spare,
13238 .process_update = imsm_process_update,
13239 .prepare_update = imsm_prepare_update,
13240 .record_bad_block = imsm_record_badblock,
13241 .clear_bad_block = imsm_clear_badblock,
13242 .get_bad_blocks = imsm_get_badblocks,
13243 };