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