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