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