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