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