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