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