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