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