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