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