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