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