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