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