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