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