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