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