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