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