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