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