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