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