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