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