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