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