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