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