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