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