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