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