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