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