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