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