2 * mdadm - Intel(R) Matrix Storage Manager Support
4 * Copyright (C) 2002-2008 Intel Corporation
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.
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
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.
20 #define HAVE_STDINT_H 1
24 #include "platform-intel.h"
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
45 #define MPB_ATTRIB_RAID0 __cpu_to_le32(0x00000001)
47 #define MPB_ATTRIB_RAID1 __cpu_to_le32(0x00000002)
49 #define MPB_ATTRIB_RAID10 __cpu_to_le32(0x00000004)
51 #define MPB_ATTRIB_RAID1E __cpu_to_le32(0x00000008)
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)
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)
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)
75 /* Define all supported attributes that have to be accepted by mdadm
77 #define MPB_ATTRIB_SUPPORTED (MPB_ATTRIB_CHECKSUM_VERIFY | \
79 MPB_ATTRIB_2TB_DISK | \
84 MPB_ATTRIB_EXP_STRIPE_SIZE | \
87 /* Define attributes that are unused but not harmful */
88 #define MPB_ATTRIB_IGNORED (MPB_ATTRIB_NEVER_USE)
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
96 /* Disk configuration info. */
97 #define IMSM_MAX_DEVICES 255
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 */
112 /* map selector for map managment
118 /* RAID map configuration infos. */
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
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 */
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
145 } __attribute__ ((packed
));
148 __u32 curr_migr_unit
;
149 __u32 checkpoint_id
; /* id to access curr_migr_unit */
150 __u8 migr_state
; /* Normal or Migrating */
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, ... */
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 */
163 struct imsm_map map
[1];
164 /* here comes another one if migr_state */
165 } __attribute__ ((packed
));
168 __u8 volume
[MAX_RAID_SERIAL_LEN
];
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 */
189 __u8 cng_master_disk
;
193 #define IMSM_DEV_FILLERS 10
194 __u32 filler
[IMSM_DEV_FILLERS
];
196 } __attribute__ ((packed
));
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
));
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
225 struct bbm_log_block_addr
{
228 } __attribute__ ((__packed__
));
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__
));
237 __u32 signature
; /* 0xABADB10C */
239 struct bbm_log_entry marked_block_entries
[BBM_LOG_MAX_ENTRIES
];
240 } __attribute__ ((__packed__
));
243 static char *map_state_str
[] = { "normal", "uninitialized", "degraded", "failed" };
246 #define RAID_DISK_RESERVED_BLOCKS_IMSM_HI 2209
248 #define GEN_MIGR_AREA_SIZE 2048 /* General Migration Copy Area size in blocks */
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
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 */
261 __u32 rec_status
; /* Status used to determine how to restart
262 * migration in case it aborts
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
270 __u32 blocks_per_unit
; /* Num disk blocks per unit of operation */
271 __u32 dest_depth_per_unit
; /* Num member blocks each destMap
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__
));
289 * 2: metadata does not match
297 struct md_list
*next
;
300 #define pr_vrb(fmt, arg...) (void) (verbose && pr_err(fmt, ##arg))
302 static __u8
migr_type(struct imsm_dev
*dev
)
304 if (dev
->vol
.migr_type
== MIGR_VERIFY
&&
305 dev
->status
& DEV_VERIFY_AND_FIX
)
308 return dev
->vol
.migr_type
;
311 static void set_migr_type(struct imsm_dev
*dev
, __u8 migr_type
)
313 /* for compatibility with older oroms convert MIGR_REPAIR, into
314 * MIGR_VERIFY w/ DEV_VERIFY_AND_FIX status
316 if (migr_type
== MIGR_REPAIR
) {
317 dev
->vol
.migr_type
= MIGR_VERIFY
;
318 dev
->status
|= DEV_VERIFY_AND_FIX
;
320 dev
->vol
.migr_type
= migr_type
;
321 dev
->status
&= ~DEV_VERIFY_AND_FIX
;
325 static unsigned int sector_count(__u32 bytes
, unsigned int sector_size
)
327 return ROUND_UP(bytes
, sector_size
) / sector_size
;
330 static unsigned int mpb_sectors(struct imsm_super
*mpb
,
331 unsigned int sector_size
)
333 return sector_count(__le32_to_cpu(mpb
->mpb_size
), sector_size
);
337 struct imsm_dev
*dev
;
338 struct intel_dev
*next
;
343 enum sys_dev_type type
;
346 struct intel_hba
*next
;
353 /* internal representation of IMSM metadata */
356 void *buf
; /* O_DIRECT buffer for reading/writing metadata */
357 struct imsm_super
*anchor
; /* immovable parameters */
360 void *migr_rec_buf
; /* buffer for I/O operations */
361 struct migr_record
*migr_rec
; /* migration record */
363 int clean_migration_record_by_mdmon
; /* when reshape is switched to next
364 array, it indicates that mdmon is allowed to clean migration
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 */
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 */
379 __u8 serial
[MAX_RAID_SERIAL_LEN
];
382 struct imsm_disk disk
;
385 struct extent
*e
; /* for determining freespace @ create */
386 int raiddisk
; /* slot to fill in autolayout */
388 } *disks
, *current_disk
;
389 struct dl
*disk_mgmt_list
; /* list of disks to add/remove while mdmon
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 struct md_bb bb
; /* memory for get_bad_blocks call */
400 struct imsm_disk disk
;
401 #define IMSM_UNKNOWN_OWNER (-1)
403 struct intel_disk
*next
;
407 unsigned long long start
, size
;
410 /* definitions of reshape process types */
411 enum imsm_reshape_type
{
417 /* definition of messages passed to imsm_process_update */
418 enum imsm_update_type
{
419 update_activate_spare
,
423 update_add_remove_disk
,
424 update_reshape_container_disks
,
425 update_reshape_migration
,
427 update_general_migration_checkpoint
,
429 update_prealloc_badblocks_mem
,
432 struct imsm_update_activate_spare
{
433 enum imsm_update_type type
;
437 struct imsm_update_activate_spare
*next
;
443 unsigned long long size
;
450 enum takeover_direction
{
454 struct imsm_update_takeover
{
455 enum imsm_update_type type
;
457 enum takeover_direction direction
;
460 struct imsm_update_reshape
{
461 enum imsm_update_type type
;
465 int new_disks
[1]; /* new_raid_disks - old_raid_disks makedev number */
468 struct imsm_update_reshape_migration
{
469 enum imsm_update_type type
;
472 /* fields for array migration changes
479 int new_disks
[1]; /* new_raid_disks - old_raid_disks makedev number */
482 struct imsm_update_size_change
{
483 enum imsm_update_type type
;
488 struct imsm_update_general_migration_checkpoint
{
489 enum imsm_update_type type
;
490 __u32 curr_migr_unit
;
494 __u8 serial
[MAX_RAID_SERIAL_LEN
];
497 struct imsm_update_create_array
{
498 enum imsm_update_type type
;
503 struct imsm_update_kill_array
{
504 enum imsm_update_type type
;
508 struct imsm_update_rename_array
{
509 enum imsm_update_type type
;
510 __u8 name
[MAX_RAID_SERIAL_LEN
];
514 struct imsm_update_add_remove_disk
{
515 enum imsm_update_type type
;
518 struct imsm_update_prealloc_bb_mem
{
519 enum imsm_update_type type
;
522 static const char *_sys_dev_type
[] = {
523 [SYS_DEV_UNKNOWN
] = "Unknown",
524 [SYS_DEV_SAS
] = "SAS",
525 [SYS_DEV_SATA
] = "SATA",
526 [SYS_DEV_NVME
] = "NVMe",
527 [SYS_DEV_VMD
] = "VMD"
530 const char *get_sys_dev_type(enum sys_dev_type type
)
532 if (type
>= SYS_DEV_MAX
)
533 type
= SYS_DEV_UNKNOWN
;
535 return _sys_dev_type
[type
];
538 static struct intel_hba
* alloc_intel_hba(struct sys_dev
*device
)
540 struct intel_hba
*result
= xmalloc(sizeof(*result
));
542 result
->type
= device
->type
;
543 result
->path
= xstrdup(device
->path
);
545 if (result
->path
&& (result
->pci_id
= strrchr(result
->path
, '/')) != NULL
)
551 static struct intel_hba
* find_intel_hba(struct intel_hba
*hba
, struct sys_dev
*device
)
553 struct intel_hba
*result
;
555 for (result
= hba
; result
; result
= result
->next
) {
556 if (result
->type
== device
->type
&& strcmp(result
->path
, device
->path
) == 0)
562 static int attach_hba_to_super(struct intel_super
*super
, struct sys_dev
*device
)
564 struct intel_hba
*hba
;
566 /* check if disk attached to Intel HBA */
567 hba
= find_intel_hba(super
->hba
, device
);
570 /* Check if HBA is already attached to super */
571 if (super
->hba
== NULL
) {
572 super
->hba
= alloc_intel_hba(device
);
577 /* Intel metadata allows for all disks attached to the same type HBA.
578 * Do not support HBA types mixing
580 if (device
->type
!= hba
->type
)
583 /* Multiple same type HBAs can be used if they share the same OROM */
584 const struct imsm_orom
*device_orom
= get_orom_by_device_id(device
->dev_id
);
586 if (device_orom
!= super
->orom
)
592 hba
->next
= alloc_intel_hba(device
);
596 static struct sys_dev
* find_disk_attached_hba(int fd
, const char *devname
)
598 struct sys_dev
*list
, *elem
;
601 if ((list
= find_intel_devices()) == NULL
)
605 disk_path
= (char *) devname
;
607 disk_path
= diskfd_to_devpath(fd
);
612 for (elem
= list
; elem
; elem
= elem
->next
)
613 if (path_attached_to_hba(disk_path
, elem
->path
))
616 if (disk_path
!= devname
)
622 static int find_intel_hba_capability(int fd
, struct intel_super
*super
,
625 static struct supertype
*match_metadata_desc_imsm(char *arg
)
627 struct supertype
*st
;
629 if (strcmp(arg
, "imsm") != 0 &&
630 strcmp(arg
, "default") != 0
634 st
= xcalloc(1, sizeof(*st
));
635 st
->ss
= &super_imsm
;
636 st
->max_devs
= IMSM_MAX_DEVICES
;
637 st
->minor_version
= 0;
643 static __u8
*get_imsm_version(struct imsm_super
*mpb
)
645 return &mpb
->sig
[MPB_SIG_LEN
];
649 /* retrieve a disk directly from the anchor when the anchor is known to be
650 * up-to-date, currently only at load time
652 static struct imsm_disk
*__get_imsm_disk(struct imsm_super
*mpb
, __u8 index
)
654 if (index
>= mpb
->num_disks
)
656 return &mpb
->disk
[index
];
659 /* retrieve the disk description based on a index of the disk
662 static struct dl
*get_imsm_dl_disk(struct intel_super
*super
, __u8 index
)
666 for (d
= super
->disks
; d
; d
= d
->next
)
667 if (d
->index
== index
)
672 /* retrieve a disk from the parsed metadata */
673 static struct imsm_disk
*get_imsm_disk(struct intel_super
*super
, __u8 index
)
677 dl
= get_imsm_dl_disk(super
, index
);
684 /* generate a checksum directly from the anchor when the anchor is known to be
685 * up-to-date, currently only at load or write_super after coalescing
687 static __u32
__gen_imsm_checksum(struct imsm_super
*mpb
)
689 __u32 end
= mpb
->mpb_size
/ sizeof(end
);
690 __u32
*p
= (__u32
*) mpb
;
694 sum
+= __le32_to_cpu(*p
);
698 return sum
- __le32_to_cpu(mpb
->check_sum
);
701 static size_t sizeof_imsm_map(struct imsm_map
*map
)
703 return sizeof(struct imsm_map
) + sizeof(__u32
) * (map
->num_members
- 1);
706 struct imsm_map
*get_imsm_map(struct imsm_dev
*dev
, int second_map
)
708 /* A device can have 2 maps if it is in the middle of a migration.
710 * MAP_0 - we return the first map
711 * MAP_1 - we return the second map if it exists, else NULL
712 * MAP_X - we return the second map if it exists, else the first
714 struct imsm_map
*map
= &dev
->vol
.map
[0];
715 struct imsm_map
*map2
= NULL
;
717 if (dev
->vol
.migr_state
)
718 map2
= (void *)map
+ sizeof_imsm_map(map
);
720 switch (second_map
) {
737 /* return the size of the device.
738 * migr_state increases the returned size if map[0] were to be duplicated
740 static size_t sizeof_imsm_dev(struct imsm_dev
*dev
, int migr_state
)
742 size_t size
= sizeof(*dev
) - sizeof(struct imsm_map
) +
743 sizeof_imsm_map(get_imsm_map(dev
, MAP_0
));
745 /* migrating means an additional map */
746 if (dev
->vol
.migr_state
)
747 size
+= sizeof_imsm_map(get_imsm_map(dev
, MAP_1
));
749 size
+= sizeof_imsm_map(get_imsm_map(dev
, MAP_0
));
755 /* retrieve disk serial number list from a metadata update */
756 static struct disk_info
*get_disk_info(struct imsm_update_create_array
*update
)
759 struct disk_info
*inf
;
761 inf
= u
+ sizeof(*update
) - sizeof(struct imsm_dev
) +
762 sizeof_imsm_dev(&update
->dev
, 0);
768 static struct imsm_dev
*__get_imsm_dev(struct imsm_super
*mpb
, __u8 index
)
774 if (index
>= mpb
->num_raid_devs
)
777 /* devices start after all disks */
778 offset
= ((void *) &mpb
->disk
[mpb
->num_disks
]) - _mpb
;
780 for (i
= 0; i
<= index
; i
++)
782 return _mpb
+ offset
;
784 offset
+= sizeof_imsm_dev(_mpb
+ offset
, 0);
789 static struct imsm_dev
*get_imsm_dev(struct intel_super
*super
, __u8 index
)
791 struct intel_dev
*dv
;
793 if (index
>= super
->anchor
->num_raid_devs
)
795 for (dv
= super
->devlist
; dv
; dv
= dv
->next
)
796 if (dv
->index
== index
)
801 static inline unsigned long long __le48_to_cpu(const struct bbm_log_block_addr
804 return ((((__u64
)__le32_to_cpu(addr
->dw1
)) << 16) |
805 __le16_to_cpu(addr
->w1
));
808 static inline struct bbm_log_block_addr
__cpu_to_le48(unsigned long long sec
)
810 struct bbm_log_block_addr addr
;
812 addr
.w1
= __cpu_to_le16((__u16
)(sec
& 0xffff));
813 addr
.dw1
= __cpu_to_le32((__u32
)(sec
>> 16) & 0xffffffff);
818 /* get size of the bbm log */
819 static __u32
get_imsm_bbm_log_size(struct bbm_log
*log
)
821 if (!log
|| log
->entry_count
== 0)
824 return sizeof(log
->signature
) +
825 sizeof(log
->entry_count
) +
826 log
->entry_count
* sizeof(struct bbm_log_entry
);
829 /* check if bad block is not partially stored in bbm log */
830 static int is_stored_in_bbm(struct bbm_log
*log
, const __u8 idx
, const unsigned
831 long long sector
, const int length
, __u32
*pos
)
835 for (i
= *pos
; i
< log
->entry_count
; i
++) {
836 struct bbm_log_entry
*entry
= &log
->marked_block_entries
[i
];
837 unsigned long long bb_start
;
838 unsigned long long bb_end
;
840 bb_start
= __le48_to_cpu(&entry
->defective_block_start
);
841 bb_end
= bb_start
+ (entry
->marked_count
+ 1);
843 if ((entry
->disk_ordinal
== idx
) && (bb_start
>= sector
) &&
844 (bb_end
<= sector
+ length
)) {
852 /* record new bad block in bbm log */
853 static int record_new_badblock(struct bbm_log
*log
, const __u8 idx
, unsigned
854 long long sector
, int length
)
858 struct bbm_log_entry
*entry
= NULL
;
860 while (is_stored_in_bbm(log
, idx
, sector
, length
, &pos
)) {
861 struct bbm_log_entry
*e
= &log
->marked_block_entries
[pos
];
863 if ((e
->marked_count
+ 1 == BBM_LOG_MAX_LBA_ENTRY_VAL
) &&
864 (__le48_to_cpu(&e
->defective_block_start
) == sector
)) {
865 sector
+= BBM_LOG_MAX_LBA_ENTRY_VAL
;
866 length
-= BBM_LOG_MAX_LBA_ENTRY_VAL
;
875 int cnt
= (length
<= BBM_LOG_MAX_LBA_ENTRY_VAL
) ? length
:
876 BBM_LOG_MAX_LBA_ENTRY_VAL
;
877 entry
->defective_block_start
= __cpu_to_le48(sector
);
878 entry
->marked_count
= cnt
- 1;
885 new_bb
= ROUND_UP(length
, BBM_LOG_MAX_LBA_ENTRY_VAL
) /
886 BBM_LOG_MAX_LBA_ENTRY_VAL
;
887 if (log
->entry_count
+ new_bb
> BBM_LOG_MAX_ENTRIES
)
891 int cnt
= (length
<= BBM_LOG_MAX_LBA_ENTRY_VAL
) ? length
:
892 BBM_LOG_MAX_LBA_ENTRY_VAL
;
893 struct bbm_log_entry
*entry
=
894 &log
->marked_block_entries
[log
->entry_count
];
896 entry
->defective_block_start
= __cpu_to_le48(sector
);
897 entry
->marked_count
= cnt
- 1;
898 entry
->disk_ordinal
= idx
;
909 /* clear all bad blocks for given disk */
910 static void clear_disk_badblocks(struct bbm_log
*log
, const __u8 idx
)
914 while (i
< log
->entry_count
) {
915 struct bbm_log_entry
*entries
= log
->marked_block_entries
;
917 if (entries
[i
].disk_ordinal
== idx
) {
918 if (i
< log
->entry_count
- 1)
919 entries
[i
] = entries
[log
->entry_count
- 1];
927 /* clear given bad block */
928 static int clear_badblock(struct bbm_log
*log
, const __u8 idx
, const unsigned
929 long long sector
, const int length
) {
932 while (i
< log
->entry_count
) {
933 struct bbm_log_entry
*entries
= log
->marked_block_entries
;
935 if ((entries
[i
].disk_ordinal
== idx
) &&
936 (__le48_to_cpu(&entries
[i
].defective_block_start
) ==
937 sector
) && (entries
[i
].marked_count
+ 1 == length
)) {
938 if (i
< log
->entry_count
- 1)
939 entries
[i
] = entries
[log
->entry_count
- 1];
948 #endif /* MDASSEMBLE */
950 /* allocate and load BBM log from metadata */
951 static int load_bbm_log(struct intel_super
*super
)
953 struct imsm_super
*mpb
= super
->anchor
;
954 __u32 bbm_log_size
= __le32_to_cpu(mpb
->bbm_log_size
);
956 super
->bbm_log
= xcalloc(1, sizeof(struct bbm_log
));
961 struct bbm_log
*log
= (void *)mpb
+
962 __le32_to_cpu(mpb
->mpb_size
) - bbm_log_size
;
966 if (bbm_log_size
< sizeof(log
->signature
) +
967 sizeof(log
->entry_count
))
970 entry_count
= __le32_to_cpu(log
->entry_count
);
971 if ((__le32_to_cpu(log
->signature
) != BBM_LOG_SIGNATURE
) ||
972 (entry_count
> BBM_LOG_MAX_ENTRIES
))
976 sizeof(log
->signature
) + sizeof(log
->entry_count
) +
977 entry_count
* sizeof(struct bbm_log_entry
))
980 memcpy(super
->bbm_log
, log
, bbm_log_size
);
982 super
->bbm_log
->signature
= __cpu_to_le32(BBM_LOG_SIGNATURE
);
983 super
->bbm_log
->entry_count
= 0;
989 /* checks if bad block is within volume boundaries */
990 static int is_bad_block_in_volume(const struct bbm_log_entry
*entry
,
991 const unsigned long long start_sector
,
992 const unsigned long long size
)
994 unsigned long long bb_start
;
995 unsigned long long bb_end
;
997 bb_start
= __le48_to_cpu(&entry
->defective_block_start
);
998 bb_end
= bb_start
+ (entry
->marked_count
+ 1);
1000 if (((bb_start
>= start_sector
) && (bb_start
< start_sector
+ size
)) ||
1001 ((bb_end
>= start_sector
) && (bb_end
<= start_sector
+ size
)))
1007 /* get list of bad blocks on a drive for a volume */
1008 static void get_volume_badblocks(const struct bbm_log
*log
, const __u8 idx
,
1009 const unsigned long long start_sector
,
1010 const unsigned long long size
,
1016 for (i
= 0; i
< log
->entry_count
; i
++) {
1017 const struct bbm_log_entry
*ent
=
1018 &log
->marked_block_entries
[i
];
1019 struct md_bb_entry
*bb
;
1021 if ((ent
->disk_ordinal
== idx
) &&
1022 is_bad_block_in_volume(ent
, start_sector
, size
)) {
1024 if (!bbs
->entries
) {
1025 bbs
->entries
= xmalloc(BBM_LOG_MAX_ENTRIES
*
1031 bb
= &bbs
->entries
[count
++];
1032 bb
->sector
= __le48_to_cpu(&ent
->defective_block_start
);
1033 bb
->length
= ent
->marked_count
+ 1;
1041 * == MAP_0 get first map
1042 * == MAP_1 get second map
1043 * == MAP_X than get map according to the current migr_state
1045 static __u32
get_imsm_ord_tbl_ent(struct imsm_dev
*dev
,
1049 struct imsm_map
*map
;
1051 map
= get_imsm_map(dev
, second_map
);
1053 /* top byte identifies disk under rebuild */
1054 return __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
1057 #define ord_to_idx(ord) (((ord) << 8) >> 8)
1058 static __u32
get_imsm_disk_idx(struct imsm_dev
*dev
, int slot
, int second_map
)
1060 __u32 ord
= get_imsm_ord_tbl_ent(dev
, slot
, second_map
);
1062 return ord_to_idx(ord
);
1065 static void set_imsm_ord_tbl_ent(struct imsm_map
*map
, int slot
, __u32 ord
)
1067 map
->disk_ord_tbl
[slot
] = __cpu_to_le32(ord
);
1070 static int get_imsm_disk_slot(struct imsm_map
*map
, unsigned idx
)
1075 for (slot
= 0; slot
< map
->num_members
; slot
++) {
1076 ord
= __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
1077 if (ord_to_idx(ord
) == idx
)
1084 static int get_imsm_raid_level(struct imsm_map
*map
)
1086 if (map
->raid_level
== 1) {
1087 if (map
->num_members
== 2)
1093 return map
->raid_level
;
1096 static int cmp_extent(const void *av
, const void *bv
)
1098 const struct extent
*a
= av
;
1099 const struct extent
*b
= bv
;
1100 if (a
->start
< b
->start
)
1102 if (a
->start
> b
->start
)
1107 static int count_memberships(struct dl
*dl
, struct intel_super
*super
)
1109 int memberships
= 0;
1112 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
1113 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
1114 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
1116 if (get_imsm_disk_slot(map
, dl
->index
) >= 0)
1123 static __u32
imsm_min_reserved_sectors(struct intel_super
*super
);
1125 static int split_ull(unsigned long long n
, __u32
*lo
, __u32
*hi
)
1127 if (lo
== 0 || hi
== 0)
1129 *lo
= __le32_to_cpu((unsigned)n
);
1130 *hi
= __le32_to_cpu((unsigned)(n
>> 32));
1134 static unsigned long long join_u32(__u32 lo
, __u32 hi
)
1136 return (unsigned long long)__le32_to_cpu(lo
) |
1137 (((unsigned long long)__le32_to_cpu(hi
)) << 32);
1140 static unsigned long long total_blocks(struct imsm_disk
*disk
)
1144 return join_u32(disk
->total_blocks_lo
, disk
->total_blocks_hi
);
1147 static unsigned long long pba_of_lba0(struct imsm_map
*map
)
1151 return join_u32(map
->pba_of_lba0_lo
, map
->pba_of_lba0_hi
);
1154 static unsigned long long blocks_per_member(struct imsm_map
*map
)
1158 return join_u32(map
->blocks_per_member_lo
, map
->blocks_per_member_hi
);
1161 static unsigned long long num_data_stripes(struct imsm_map
*map
)
1165 return join_u32(map
->num_data_stripes_lo
, map
->num_data_stripes_hi
);
1168 static void set_total_blocks(struct imsm_disk
*disk
, unsigned long long n
)
1170 split_ull(n
, &disk
->total_blocks_lo
, &disk
->total_blocks_hi
);
1173 static void set_pba_of_lba0(struct imsm_map
*map
, unsigned long long n
)
1175 split_ull(n
, &map
->pba_of_lba0_lo
, &map
->pba_of_lba0_hi
);
1178 static void set_blocks_per_member(struct imsm_map
*map
, unsigned long long n
)
1180 split_ull(n
, &map
->blocks_per_member_lo
, &map
->blocks_per_member_hi
);
1183 static void set_num_data_stripes(struct imsm_map
*map
, unsigned long long n
)
1185 split_ull(n
, &map
->num_data_stripes_lo
, &map
->num_data_stripes_hi
);
1188 static struct extent
*get_extents(struct intel_super
*super
, struct dl
*dl
)
1190 /* find a list of used extents on the given physical device */
1191 struct extent
*rv
, *e
;
1193 int memberships
= count_memberships(dl
, super
);
1196 /* trim the reserved area for spares, so they can join any array
1197 * regardless of whether the OROM has assigned sectors from the
1198 * IMSM_RESERVED_SECTORS region
1200 if (dl
->index
== -1)
1201 reservation
= imsm_min_reserved_sectors(super
);
1203 reservation
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
1205 rv
= xcalloc(sizeof(struct extent
), (memberships
+ 1));
1208 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
1209 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
1210 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
1212 if (get_imsm_disk_slot(map
, dl
->index
) >= 0) {
1213 e
->start
= pba_of_lba0(map
);
1214 e
->size
= blocks_per_member(map
);
1218 qsort(rv
, memberships
, sizeof(*rv
), cmp_extent
);
1220 /* determine the start of the metadata
1221 * when no raid devices are defined use the default
1222 * ...otherwise allow the metadata to truncate the value
1223 * as is the case with older versions of imsm
1226 struct extent
*last
= &rv
[memberships
- 1];
1227 unsigned long long remainder
;
1229 remainder
= total_blocks(&dl
->disk
) - (last
->start
+ last
->size
);
1230 /* round down to 1k block to satisfy precision of the kernel
1234 /* make sure remainder is still sane */
1235 if (remainder
< (unsigned)ROUND_UP(super
->len
, 512) >> 9)
1236 remainder
= ROUND_UP(super
->len
, 512) >> 9;
1237 if (reservation
> remainder
)
1238 reservation
= remainder
;
1240 e
->start
= total_blocks(&dl
->disk
) - reservation
;
1245 /* try to determine how much space is reserved for metadata from
1246 * the last get_extents() entry, otherwise fallback to the
1249 static __u32
imsm_reserved_sectors(struct intel_super
*super
, struct dl
*dl
)
1255 /* for spares just return a minimal reservation which will grow
1256 * once the spare is picked up by an array
1258 if (dl
->index
== -1)
1259 return MPB_SECTOR_CNT
;
1261 e
= get_extents(super
, dl
);
1263 return MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
1265 /* scroll to last entry */
1266 for (i
= 0; e
[i
].size
; i
++)
1269 rv
= total_blocks(&dl
->disk
) - e
[i
].start
;
1276 static int is_spare(struct imsm_disk
*disk
)
1278 return (disk
->status
& SPARE_DISK
) == SPARE_DISK
;
1281 static int is_configured(struct imsm_disk
*disk
)
1283 return (disk
->status
& CONFIGURED_DISK
) == CONFIGURED_DISK
;
1286 static int is_failed(struct imsm_disk
*disk
)
1288 return (disk
->status
& FAILED_DISK
) == FAILED_DISK
;
1291 /* try to determine how much space is reserved for metadata from
1292 * the last get_extents() entry on the smallest active disk,
1293 * otherwise fallback to the default
1295 static __u32
imsm_min_reserved_sectors(struct intel_super
*super
)
1299 unsigned long long min_active
;
1301 __u32 rv
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
1302 struct dl
*dl
, *dl_min
= NULL
;
1308 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
1311 unsigned long long blocks
= total_blocks(&dl
->disk
);
1312 if (blocks
< min_active
|| min_active
== 0) {
1314 min_active
= blocks
;
1320 /* find last lba used by subarrays on the smallest active disk */
1321 e
= get_extents(super
, dl_min
);
1324 for (i
= 0; e
[i
].size
; i
++)
1327 remainder
= min_active
- e
[i
].start
;
1330 /* to give priority to recovery we should not require full
1331 IMSM_RESERVED_SECTORS from the spare */
1332 rv
= MPB_SECTOR_CNT
+ NUM_BLOCKS_DIRTY_STRIPE_REGION
;
1334 /* if real reservation is smaller use that value */
1335 return (remainder
< rv
) ? remainder
: rv
;
1338 /* Return minimum size of a spare that can be used in this array*/
1339 static unsigned long long min_acceptable_spare_size_imsm(struct supertype
*st
)
1341 struct intel_super
*super
= st
->sb
;
1345 unsigned long long rv
= 0;
1349 /* find first active disk in array */
1351 while (dl
&& (is_failed(&dl
->disk
) || dl
->index
== -1))
1355 /* find last lba used by subarrays */
1356 e
= get_extents(super
, dl
);
1359 for (i
= 0; e
[i
].size
; i
++)
1362 rv
= e
[i
-1].start
+ e
[i
-1].size
;
1365 /* add the amount of space needed for metadata */
1366 rv
= rv
+ imsm_min_reserved_sectors(super
);
1371 static int is_gen_migration(struct imsm_dev
*dev
);
1373 #define IMSM_4K_DIV 8
1376 static __u64
blocks_per_migr_unit(struct intel_super
*super
,
1377 struct imsm_dev
*dev
);
1379 static void print_imsm_dev(struct intel_super
*super
,
1380 struct imsm_dev
*dev
,
1386 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
1387 struct imsm_map
*map2
= get_imsm_map(dev
, MAP_1
);
1391 printf("[%.16s]:\n", dev
->volume
);
1392 printf(" UUID : %s\n", uuid
);
1393 printf(" RAID Level : %d", get_imsm_raid_level(map
));
1395 printf(" <-- %d", get_imsm_raid_level(map2
));
1397 printf(" Members : %d", map
->num_members
);
1399 printf(" <-- %d", map2
->num_members
);
1401 printf(" Slots : [");
1402 for (i
= 0; i
< map
->num_members
; i
++) {
1403 ord
= get_imsm_ord_tbl_ent(dev
, i
, MAP_0
);
1404 printf("%s", ord
& IMSM_ORD_REBUILD
? "_" : "U");
1409 for (i
= 0; i
< map2
->num_members
; i
++) {
1410 ord
= get_imsm_ord_tbl_ent(dev
, i
, MAP_1
);
1411 printf("%s", ord
& IMSM_ORD_REBUILD
? "_" : "U");
1416 printf(" Failed disk : ");
1417 if (map
->failed_disk_num
== 0xff)
1420 printf("%i", map
->failed_disk_num
);
1422 slot
= get_imsm_disk_slot(map
, disk_idx
);
1424 ord
= get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
);
1425 printf(" This Slot : %d%s\n", slot
,
1426 ord
& IMSM_ORD_REBUILD
? " (out-of-sync)" : "");
1428 printf(" This Slot : ?\n");
1429 sz
= __le32_to_cpu(dev
->size_high
);
1431 sz
+= __le32_to_cpu(dev
->size_low
);
1432 printf(" Array Size : %llu%s\n", (unsigned long long)sz
,
1433 human_size(sz
* 512));
1434 sz
= blocks_per_member(map
);
1435 printf(" Per Dev Size : %llu%s\n", (unsigned long long)sz
,
1436 human_size(sz
* 512));
1437 printf(" Sector Offset : %llu\n",
1439 printf(" Num Stripes : %llu\n",
1440 num_data_stripes(map
));
1441 printf(" Chunk Size : %u KiB",
1442 __le16_to_cpu(map
->blocks_per_strip
) / 2);
1444 printf(" <-- %u KiB",
1445 __le16_to_cpu(map2
->blocks_per_strip
) / 2);
1447 printf(" Reserved : %d\n", __le32_to_cpu(dev
->reserved_blocks
));
1448 printf(" Migrate State : ");
1449 if (dev
->vol
.migr_state
) {
1450 if (migr_type(dev
) == MIGR_INIT
)
1451 printf("initialize\n");
1452 else if (migr_type(dev
) == MIGR_REBUILD
)
1453 printf("rebuild\n");
1454 else if (migr_type(dev
) == MIGR_VERIFY
)
1456 else if (migr_type(dev
) == MIGR_GEN_MIGR
)
1457 printf("general migration\n");
1458 else if (migr_type(dev
) == MIGR_STATE_CHANGE
)
1459 printf("state change\n");
1460 else if (migr_type(dev
) == MIGR_REPAIR
)
1463 printf("<unknown:%d>\n", migr_type(dev
));
1466 printf(" Map State : %s", map_state_str
[map
->map_state
]);
1467 if (dev
->vol
.migr_state
) {
1468 struct imsm_map
*map
= get_imsm_map(dev
, MAP_1
);
1470 printf(" <-- %s", map_state_str
[map
->map_state
]);
1471 printf("\n Checkpoint : %u ",
1472 __le32_to_cpu(dev
->vol
.curr_migr_unit
));
1473 if (is_gen_migration(dev
) && (slot
> 1 || slot
< 0))
1476 printf("(%llu)", (unsigned long long)
1477 blocks_per_migr_unit(super
, dev
));
1480 printf(" Dirty State : %s\n", dev
->vol
.dirty
? "dirty" : "clean");
1483 static void print_imsm_disk(struct imsm_disk
*disk
, int index
, __u32 reserved
)
1485 char str
[MAX_RAID_SERIAL_LEN
+ 1];
1488 if (index
< -1 || !disk
)
1492 snprintf(str
, MAX_RAID_SERIAL_LEN
+ 1, "%s", disk
->serial
);
1494 printf(" Disk%02d Serial : %s\n", index
, str
);
1496 printf(" Disk Serial : %s\n", str
);
1497 printf(" State :%s%s%s\n", is_spare(disk
) ? " spare" : "",
1498 is_configured(disk
) ? " active" : "",
1499 is_failed(disk
) ? " failed" : "");
1500 printf(" Id : %08x\n", __le32_to_cpu(disk
->scsi_id
));
1501 sz
= total_blocks(disk
) - reserved
;
1502 printf(" Usable Size : %llu%s\n", (unsigned long long)sz
,
1503 human_size(sz
* 512));
1506 void convert_to_4k_imsm_migr_rec(struct intel_super
*super
)
1508 struct migr_record
*migr_rec
= super
->migr_rec
;
1510 migr_rec
->blocks_per_unit
/= IMSM_4K_DIV
;
1511 migr_rec
->ckpt_area_pba
/= IMSM_4K_DIV
;
1512 migr_rec
->dest_1st_member_lba
/= IMSM_4K_DIV
;
1513 migr_rec
->dest_depth_per_unit
/= IMSM_4K_DIV
;
1514 split_ull((join_u32(migr_rec
->post_migr_vol_cap
,
1515 migr_rec
->post_migr_vol_cap_hi
) / IMSM_4K_DIV
),
1516 &migr_rec
->post_migr_vol_cap
, &migr_rec
->post_migr_vol_cap_hi
);
1519 void convert_to_4k_imsm_disk(struct imsm_disk
*disk
)
1521 set_total_blocks(disk
, (total_blocks(disk
)/IMSM_4K_DIV
));
1524 void convert_to_4k(struct intel_super
*super
)
1526 struct imsm_super
*mpb
= super
->anchor
;
1527 struct imsm_disk
*disk
;
1530 for (i
= 0; i
< mpb
->num_disks
; i
++) {
1531 disk
= __get_imsm_disk(mpb
, i
);
1533 convert_to_4k_imsm_disk(disk
);
1535 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
1536 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
1537 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
1539 split_ull((join_u32(dev
->size_low
, dev
->size_high
)/IMSM_4K_DIV
),
1540 &dev
->size_low
, &dev
->size_high
);
1541 dev
->vol
.curr_migr_unit
/= IMSM_4K_DIV
;
1544 set_blocks_per_member(map
, blocks_per_member(map
)/IMSM_4K_DIV
);
1545 map
->blocks_per_strip
/= IMSM_4K_DIV
;
1546 set_pba_of_lba0(map
, pba_of_lba0(map
)/IMSM_4K_DIV
);
1548 if (dev
->vol
.migr_state
) {
1550 map
= get_imsm_map(dev
, MAP_1
);
1551 set_blocks_per_member(map
,
1552 blocks_per_member(map
)/IMSM_4K_DIV
);
1553 map
->blocks_per_strip
/= IMSM_4K_DIV
;
1554 set_pba_of_lba0(map
, pba_of_lba0(map
)/IMSM_4K_DIV
);
1558 mpb
->check_sum
= __gen_imsm_checksum(mpb
);
1561 void examine_migr_rec_imsm(struct intel_super
*super
)
1563 struct migr_record
*migr_rec
= super
->migr_rec
;
1564 struct imsm_super
*mpb
= super
->anchor
;
1567 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
1568 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
1569 struct imsm_map
*map
;
1572 if (is_gen_migration(dev
) == 0)
1575 printf("\nMigration Record Information:");
1577 /* first map under migration */
1578 map
= get_imsm_map(dev
, MAP_0
);
1580 slot
= get_imsm_disk_slot(map
, super
->disks
->index
);
1581 if (map
== NULL
|| slot
> 1 || slot
< 0) {
1582 printf(" Empty\n ");
1583 printf("Examine one of first two disks in array\n");
1586 printf("\n Status : ");
1587 if (__le32_to_cpu(migr_rec
->rec_status
) == UNIT_SRC_NORMAL
)
1590 printf("Contains Data\n");
1591 printf(" Current Unit : %u\n",
1592 __le32_to_cpu(migr_rec
->curr_migr_unit
));
1593 printf(" Family : %u\n",
1594 __le32_to_cpu(migr_rec
->family_num
));
1595 printf(" Ascending : %u\n",
1596 __le32_to_cpu(migr_rec
->ascending_migr
));
1597 printf(" Blocks Per Unit : %u\n",
1598 __le32_to_cpu(migr_rec
->blocks_per_unit
));
1599 printf(" Dest. Depth Per Unit : %u\n",
1600 __le32_to_cpu(migr_rec
->dest_depth_per_unit
));
1601 printf(" Checkpoint Area pba : %u\n",
1602 __le32_to_cpu(migr_rec
->ckpt_area_pba
));
1603 printf(" First member lba : %u\n",
1604 __le32_to_cpu(migr_rec
->dest_1st_member_lba
));
1605 printf(" Total Number of Units : %u\n",
1606 __le32_to_cpu(migr_rec
->num_migr_units
));
1607 printf(" Size of volume : %u\n",
1608 __le32_to_cpu(migr_rec
->post_migr_vol_cap
));
1609 printf(" Expansion space for LBA64 : %u\n",
1610 __le32_to_cpu(migr_rec
->post_migr_vol_cap_hi
));
1611 printf(" Record was read from : %u\n",
1612 __le32_to_cpu(migr_rec
->ckpt_read_disk_num
));
1617 #endif /* MDASSEMBLE */
1619 void convert_from_4k_imsm_migr_rec(struct intel_super
*super
)
1621 struct migr_record
*migr_rec
= super
->migr_rec
;
1623 migr_rec
->blocks_per_unit
*= IMSM_4K_DIV
;
1624 migr_rec
->ckpt_area_pba
*= IMSM_4K_DIV
;
1625 migr_rec
->dest_1st_member_lba
*= IMSM_4K_DIV
;
1626 migr_rec
->dest_depth_per_unit
*= IMSM_4K_DIV
;
1627 split_ull((join_u32(migr_rec
->post_migr_vol_cap
,
1628 migr_rec
->post_migr_vol_cap_hi
) * IMSM_4K_DIV
),
1629 &migr_rec
->post_migr_vol_cap
,
1630 &migr_rec
->post_migr_vol_cap_hi
);
1633 void convert_from_4k(struct intel_super
*super
)
1635 struct imsm_super
*mpb
= super
->anchor
;
1636 struct imsm_disk
*disk
;
1639 for (i
= 0; i
< mpb
->num_disks
; i
++) {
1640 disk
= __get_imsm_disk(mpb
, i
);
1642 set_total_blocks(disk
, (total_blocks(disk
)*IMSM_4K_DIV
));
1645 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
1646 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
1647 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
1649 split_ull((join_u32(dev
->size_low
, dev
->size_high
)*IMSM_4K_DIV
),
1650 &dev
->size_low
, &dev
->size_high
);
1651 dev
->vol
.curr_migr_unit
*= IMSM_4K_DIV
;
1654 set_blocks_per_member(map
, blocks_per_member(map
)*IMSM_4K_DIV
);
1655 map
->blocks_per_strip
*= IMSM_4K_DIV
;
1656 set_pba_of_lba0(map
, pba_of_lba0(map
)*IMSM_4K_DIV
);
1658 if (dev
->vol
.migr_state
) {
1660 map
= get_imsm_map(dev
, MAP_1
);
1661 set_blocks_per_member(map
,
1662 blocks_per_member(map
)*IMSM_4K_DIV
);
1663 map
->blocks_per_strip
*= IMSM_4K_DIV
;
1664 set_pba_of_lba0(map
, pba_of_lba0(map
)*IMSM_4K_DIV
);
1668 mpb
->check_sum
= __gen_imsm_checksum(mpb
);
1671 /*******************************************************************************
1672 * function: imsm_check_attributes
1673 * Description: Function checks if features represented by attributes flags
1674 * are supported by mdadm.
1676 * attributes - Attributes read from metadata
1678 * 0 - passed attributes contains unsupported features flags
1679 * 1 - all features are supported
1680 ******************************************************************************/
1681 static int imsm_check_attributes(__u32 attributes
)
1684 __u32 not_supported
= MPB_ATTRIB_SUPPORTED
^0xffffffff;
1686 not_supported
&= ~MPB_ATTRIB_IGNORED
;
1688 not_supported
&= attributes
;
1689 if (not_supported
) {
1690 pr_err("(IMSM): Unsupported attributes : %x\n",
1691 (unsigned)__le32_to_cpu(not_supported
));
1692 if (not_supported
& MPB_ATTRIB_CHECKSUM_VERIFY
) {
1693 dprintf("\t\tMPB_ATTRIB_CHECKSUM_VERIFY \n");
1694 not_supported
^= MPB_ATTRIB_CHECKSUM_VERIFY
;
1696 if (not_supported
& MPB_ATTRIB_2TB
) {
1697 dprintf("\t\tMPB_ATTRIB_2TB\n");
1698 not_supported
^= MPB_ATTRIB_2TB
;
1700 if (not_supported
& MPB_ATTRIB_RAID0
) {
1701 dprintf("\t\tMPB_ATTRIB_RAID0\n");
1702 not_supported
^= MPB_ATTRIB_RAID0
;
1704 if (not_supported
& MPB_ATTRIB_RAID1
) {
1705 dprintf("\t\tMPB_ATTRIB_RAID1\n");
1706 not_supported
^= MPB_ATTRIB_RAID1
;
1708 if (not_supported
& MPB_ATTRIB_RAID10
) {
1709 dprintf("\t\tMPB_ATTRIB_RAID10\n");
1710 not_supported
^= MPB_ATTRIB_RAID10
;
1712 if (not_supported
& MPB_ATTRIB_RAID1E
) {
1713 dprintf("\t\tMPB_ATTRIB_RAID1E\n");
1714 not_supported
^= MPB_ATTRIB_RAID1E
;
1716 if (not_supported
& MPB_ATTRIB_RAID5
) {
1717 dprintf("\t\tMPB_ATTRIB_RAID5\n");
1718 not_supported
^= MPB_ATTRIB_RAID5
;
1720 if (not_supported
& MPB_ATTRIB_RAIDCNG
) {
1721 dprintf("\t\tMPB_ATTRIB_RAIDCNG\n");
1722 not_supported
^= MPB_ATTRIB_RAIDCNG
;
1724 if (not_supported
& MPB_ATTRIB_BBM
) {
1725 dprintf("\t\tMPB_ATTRIB_BBM\n");
1726 not_supported
^= MPB_ATTRIB_BBM
;
1728 if (not_supported
& MPB_ATTRIB_CHECKSUM_VERIFY
) {
1729 dprintf("\t\tMPB_ATTRIB_CHECKSUM_VERIFY (== MPB_ATTRIB_LEGACY)\n");
1730 not_supported
^= MPB_ATTRIB_CHECKSUM_VERIFY
;
1732 if (not_supported
& MPB_ATTRIB_EXP_STRIPE_SIZE
) {
1733 dprintf("\t\tMPB_ATTRIB_EXP_STRIP_SIZE\n");
1734 not_supported
^= MPB_ATTRIB_EXP_STRIPE_SIZE
;
1736 if (not_supported
& MPB_ATTRIB_2TB_DISK
) {
1737 dprintf("\t\tMPB_ATTRIB_2TB_DISK\n");
1738 not_supported
^= MPB_ATTRIB_2TB_DISK
;
1740 if (not_supported
& MPB_ATTRIB_NEVER_USE2
) {
1741 dprintf("\t\tMPB_ATTRIB_NEVER_USE2\n");
1742 not_supported
^= MPB_ATTRIB_NEVER_USE2
;
1744 if (not_supported
& MPB_ATTRIB_NEVER_USE
) {
1745 dprintf("\t\tMPB_ATTRIB_NEVER_USE\n");
1746 not_supported
^= MPB_ATTRIB_NEVER_USE
;
1750 dprintf("(IMSM): Unknown attributes : %x\n", not_supported
);
1759 static void getinfo_super_imsm(struct supertype
*st
, struct mdinfo
*info
, char *map
);
1761 static void examine_super_imsm(struct supertype
*st
, char *homehost
)
1763 struct intel_super
*super
= st
->sb
;
1764 struct imsm_super
*mpb
= super
->anchor
;
1765 char str
[MAX_SIGNATURE_LENGTH
];
1770 __u32 reserved
= imsm_reserved_sectors(super
, super
->disks
);
1773 snprintf(str
, MPB_SIG_LEN
, "%s", mpb
->sig
);
1774 printf(" Magic : %s\n", str
);
1775 snprintf(str
, strlen(MPB_VERSION_RAID0
), "%s", get_imsm_version(mpb
));
1776 printf(" Version : %s\n", get_imsm_version(mpb
));
1777 printf(" Orig Family : %08x\n", __le32_to_cpu(mpb
->orig_family_num
));
1778 printf(" Family : %08x\n", __le32_to_cpu(mpb
->family_num
));
1779 printf(" Generation : %08x\n", __le32_to_cpu(mpb
->generation_num
));
1780 printf(" Attributes : ");
1781 if (imsm_check_attributes(mpb
->attributes
))
1782 printf("All supported\n");
1784 printf("not supported\n");
1785 getinfo_super_imsm(st
, &info
, NULL
);
1786 fname_from_uuid(st
, &info
, nbuf
, ':');
1787 printf(" UUID : %s\n", nbuf
+ 5);
1788 sum
= __le32_to_cpu(mpb
->check_sum
);
1789 printf(" Checksum : %08x %s\n", sum
,
1790 __gen_imsm_checksum(mpb
) == sum
? "correct" : "incorrect");
1791 printf(" MPB Sectors : %d\n", mpb_sectors(mpb
, super
->sector_size
));
1792 printf(" Disks : %d\n", mpb
->num_disks
);
1793 printf(" RAID Devices : %d\n", mpb
->num_raid_devs
);
1794 print_imsm_disk(__get_imsm_disk(mpb
, super
->disks
->index
), super
->disks
->index
, reserved
);
1795 if (get_imsm_bbm_log_size(super
->bbm_log
)) {
1796 struct bbm_log
*log
= super
->bbm_log
;
1799 printf("Bad Block Management Log:\n");
1800 printf(" Log Size : %d\n", __le32_to_cpu(mpb
->bbm_log_size
));
1801 printf(" Signature : %x\n", __le32_to_cpu(log
->signature
));
1802 printf(" Entry Count : %d\n", __le32_to_cpu(log
->entry_count
));
1804 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
1806 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
1808 super
->current_vol
= i
;
1809 getinfo_super_imsm(st
, &info
, NULL
);
1810 fname_from_uuid(st
, &info
, nbuf
, ':');
1811 print_imsm_dev(super
, dev
, nbuf
+ 5, super
->disks
->index
);
1813 for (i
= 0; i
< mpb
->num_disks
; i
++) {
1814 if (i
== super
->disks
->index
)
1816 print_imsm_disk(__get_imsm_disk(mpb
, i
), i
, reserved
);
1819 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
1820 if (dl
->index
== -1)
1821 print_imsm_disk(&dl
->disk
, -1, reserved
);
1823 examine_migr_rec_imsm(super
);
1826 static void brief_examine_super_imsm(struct supertype
*st
, int verbose
)
1828 /* We just write a generic IMSM ARRAY entry */
1831 struct intel_super
*super
= st
->sb
;
1833 if (!super
->anchor
->num_raid_devs
) {
1834 printf("ARRAY metadata=imsm\n");
1838 getinfo_super_imsm(st
, &info
, NULL
);
1839 fname_from_uuid(st
, &info
, nbuf
, ':');
1840 printf("ARRAY metadata=imsm UUID=%s\n", nbuf
+ 5);
1843 static void brief_examine_subarrays_imsm(struct supertype
*st
, int verbose
)
1845 /* We just write a generic IMSM ARRAY entry */
1849 struct intel_super
*super
= st
->sb
;
1852 if (!super
->anchor
->num_raid_devs
)
1855 getinfo_super_imsm(st
, &info
, NULL
);
1856 fname_from_uuid(st
, &info
, nbuf
, ':');
1857 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
1858 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
1860 super
->current_vol
= i
;
1861 getinfo_super_imsm(st
, &info
, NULL
);
1862 fname_from_uuid(st
, &info
, nbuf1
, ':');
1863 printf("ARRAY /dev/md/%.16s container=%s member=%d UUID=%s\n",
1864 dev
->volume
, nbuf
+ 5, i
, nbuf1
+ 5);
1868 static void export_examine_super_imsm(struct supertype
*st
)
1870 struct intel_super
*super
= st
->sb
;
1871 struct imsm_super
*mpb
= super
->anchor
;
1875 getinfo_super_imsm(st
, &info
, NULL
);
1876 fname_from_uuid(st
, &info
, nbuf
, ':');
1877 printf("MD_METADATA=imsm\n");
1878 printf("MD_LEVEL=container\n");
1879 printf("MD_UUID=%s\n", nbuf
+5);
1880 printf("MD_DEVICES=%u\n", mpb
->num_disks
);
1883 static int copy_metadata_imsm(struct supertype
*st
, int from
, int to
)
1885 /* The second last sector of the device contains
1886 * the "struct imsm_super" metadata.
1887 * This contains mpb_size which is the size in bytes of the
1888 * extended metadata. This is located immediately before
1890 * We want to read all that, plus the last sector which
1891 * may contain a migration record, and write it all
1895 unsigned long long dsize
, offset
;
1897 struct imsm_super
*sb
;
1898 struct intel_super
*super
= st
->sb
;
1899 unsigned int sector_size
= super
->sector_size
;
1900 unsigned int written
= 0;
1902 if (posix_memalign(&buf
, MAX_SECTOR_SIZE
, MAX_SECTOR_SIZE
) != 0)
1905 if (!get_dev_size(from
, NULL
, &dsize
))
1908 if (lseek64(from
, dsize
-(2*sector_size
), 0) < 0)
1910 if (read(from
, buf
, sector_size
) != sector_size
)
1913 if (strncmp((char*)sb
->sig
, MPB_SIGNATURE
, MPB_SIG_LEN
) != 0)
1916 sectors
= mpb_sectors(sb
, sector_size
) + 2;
1917 offset
= dsize
- sectors
* sector_size
;
1918 if (lseek64(from
, offset
, 0) < 0 ||
1919 lseek64(to
, offset
, 0) < 0)
1921 while (written
< sectors
* sector_size
) {
1922 int n
= sectors
*sector_size
- written
;
1925 if (read(from
, buf
, n
) != n
)
1927 if (write(to
, buf
, n
) != n
)
1938 static void detail_super_imsm(struct supertype
*st
, char *homehost
)
1943 getinfo_super_imsm(st
, &info
, NULL
);
1944 fname_from_uuid(st
, &info
, nbuf
, ':');
1945 printf("\n UUID : %s\n", nbuf
+ 5);
1948 static void brief_detail_super_imsm(struct supertype
*st
)
1952 getinfo_super_imsm(st
, &info
, NULL
);
1953 fname_from_uuid(st
, &info
, nbuf
, ':');
1954 printf(" UUID=%s", nbuf
+ 5);
1957 static int imsm_read_serial(int fd
, char *devname
, __u8
*serial
);
1958 static void fd2devname(int fd
, char *name
);
1960 static int ahci_enumerate_ports(const char *hba_path
, int port_count
, int host_base
, int verbose
)
1962 /* dump an unsorted list of devices attached to AHCI Intel storage
1963 * controller, as well as non-connected ports
1965 int hba_len
= strlen(hba_path
) + 1;
1970 unsigned long port_mask
= (1 << port_count
) - 1;
1972 if (port_count
> (int)sizeof(port_mask
) * 8) {
1974 pr_err("port_count %d out of range\n", port_count
);
1978 /* scroll through /sys/dev/block looking for devices attached to
1981 dir
= opendir("/sys/dev/block");
1985 for (ent
= readdir(dir
); ent
; ent
= readdir(dir
)) {
1996 if (sscanf(ent
->d_name
, "%d:%d", &major
, &minor
) != 2)
1998 path
= devt_to_devpath(makedev(major
, minor
));
2001 if (!path_attached_to_hba(path
, hba_path
)) {
2007 /* retrieve the scsi device type */
2008 if (asprintf(&device
, "/sys/dev/block/%d:%d/device/xxxxxxx", major
, minor
) < 0) {
2010 pr_err("failed to allocate 'device'\n");
2014 sprintf(device
, "/sys/dev/block/%d:%d/device/type", major
, minor
);
2015 if (load_sys(device
, buf
, sizeof(buf
)) != 0) {
2017 pr_err("failed to read device type for %s\n",
2023 type
= strtoul(buf
, NULL
, 10);
2025 /* if it's not a disk print the vendor and model */
2026 if (!(type
== 0 || type
== 7 || type
== 14)) {
2029 sprintf(device
, "/sys/dev/block/%d:%d/device/vendor", major
, minor
);
2030 if (load_sys(device
, buf
, sizeof(buf
)) == 0) {
2031 strncpy(vendor
, buf
, sizeof(vendor
));
2032 vendor
[sizeof(vendor
) - 1] = '\0';
2033 c
= (char *) &vendor
[sizeof(vendor
) - 1];
2034 while (isspace(*c
) || *c
== '\0')
2038 sprintf(device
, "/sys/dev/block/%d:%d/device/model", major
, minor
);
2039 if (load_sys(device
, buf
, sizeof(buf
)) == 0) {
2040 strncpy(model
, buf
, sizeof(model
));
2041 model
[sizeof(model
) - 1] = '\0';
2042 c
= (char *) &model
[sizeof(model
) - 1];
2043 while (isspace(*c
) || *c
== '\0')
2047 if (vendor
[0] && model
[0])
2048 sprintf(buf
, "%.64s %.64s", vendor
, model
);
2050 switch (type
) { /* numbers from hald/linux/device.c */
2051 case 1: sprintf(buf
, "tape"); break;
2052 case 2: sprintf(buf
, "printer"); break;
2053 case 3: sprintf(buf
, "processor"); break;
2055 case 5: sprintf(buf
, "cdrom"); break;
2056 case 6: sprintf(buf
, "scanner"); break;
2057 case 8: sprintf(buf
, "media_changer"); break;
2058 case 9: sprintf(buf
, "comm"); break;
2059 case 12: sprintf(buf
, "raid"); break;
2060 default: sprintf(buf
, "unknown");
2066 /* chop device path to 'host%d' and calculate the port number */
2067 c
= strchr(&path
[hba_len
], '/');
2070 pr_err("%s - invalid path name\n", path
+ hba_len
);
2075 if ((sscanf(&path
[hba_len
], "ata%d", &port
) == 1) ||
2076 ((sscanf(&path
[hba_len
], "host%d", &port
) == 1)))
2080 *c
= '/'; /* repair the full string */
2081 pr_err("failed to determine port number for %s\n",
2088 /* mark this port as used */
2089 port_mask
&= ~(1 << port
);
2091 /* print out the device information */
2093 printf(" Port%d : - non-disk device (%s) -\n", port
, buf
);
2097 fd
= dev_open(ent
->d_name
, O_RDONLY
);
2099 printf(" Port%d : - disk info unavailable -\n", port
);
2101 fd2devname(fd
, buf
);
2102 printf(" Port%d : %s", port
, buf
);
2103 if (imsm_read_serial(fd
, NULL
, (__u8
*) buf
) == 0)
2104 printf(" (%.*s)\n", MAX_RAID_SERIAL_LEN
, buf
);
2119 for (i
= 0; i
< port_count
; i
++)
2120 if (port_mask
& (1 << i
))
2121 printf(" Port%d : - no device attached -\n", i
);
2127 static int print_vmd_attached_devs(struct sys_dev
*hba
)
2135 if (hba
->type
!= SYS_DEV_VMD
)
2138 /* scroll through /sys/dev/block looking for devices attached to
2141 dir
= opendir("/sys/bus/pci/drivers/nvme");
2145 for (ent
= readdir(dir
); ent
; ent
= readdir(dir
)) {
2148 /* is 'ent' a device? check that the 'subsystem' link exists and
2149 * that its target matches 'bus'
2151 sprintf(path
, "/sys/bus/pci/drivers/nvme/%s/subsystem",
2153 n
= readlink(path
, link
, sizeof(link
));
2154 if (n
< 0 || n
>= (int)sizeof(link
))
2157 c
= strrchr(link
, '/');
2160 if (strncmp("pci", c
+1, strlen("pci")) != 0)
2163 sprintf(path
, "/sys/bus/pci/drivers/nvme/%s", ent
->d_name
);
2164 /* if not a intel NVMe - skip it*/
2165 if (devpath_to_vendor(path
) != 0x8086)
2168 rp
= realpath(path
, NULL
);
2172 if (path_attached_to_hba(rp
, hba
->path
)) {
2173 printf(" NVMe under VMD : %s\n", rp
);
2182 static void print_found_intel_controllers(struct sys_dev
*elem
)
2184 for (; elem
; elem
= elem
->next
) {
2185 pr_err("found Intel(R) ");
2186 if (elem
->type
== SYS_DEV_SATA
)
2187 fprintf(stderr
, "SATA ");
2188 else if (elem
->type
== SYS_DEV_SAS
)
2189 fprintf(stderr
, "SAS ");
2190 else if (elem
->type
== SYS_DEV_NVME
)
2191 fprintf(stderr
, "NVMe ");
2193 if (elem
->type
== SYS_DEV_VMD
)
2194 fprintf(stderr
, "VMD domain");
2196 fprintf(stderr
, "RAID controller");
2199 fprintf(stderr
, " at %s", elem
->pci_id
);
2200 fprintf(stderr
, ".\n");
2205 static int ahci_get_port_count(const char *hba_path
, int *port_count
)
2212 if ((dir
= opendir(hba_path
)) == NULL
)
2215 for (ent
= readdir(dir
); ent
; ent
= readdir(dir
)) {
2218 if ((sscanf(ent
->d_name
, "ata%d", &host
) != 1) &&
2219 ((sscanf(ent
->d_name
, "host%d", &host
) != 1)))
2221 if (*port_count
== 0)
2223 else if (host
< host_base
)
2226 if (host
+ 1 > *port_count
+ host_base
)
2227 *port_count
= host
+ 1 - host_base
;
2233 static void print_imsm_capability(const struct imsm_orom
*orom
)
2235 printf(" Platform : Intel(R) ");
2236 if (orom
->capabilities
== 0 && orom
->driver_features
== 0)
2237 printf("Matrix Storage Manager\n");
2239 printf("Rapid Storage Technology%s\n",
2240 imsm_orom_is_enterprise(orom
) ? " enterprise" : "");
2241 if (orom
->major_ver
|| orom
->minor_ver
|| orom
->hotfix_ver
|| orom
->build
)
2242 printf(" Version : %d.%d.%d.%d\n", orom
->major_ver
,
2243 orom
->minor_ver
, orom
->hotfix_ver
, orom
->build
);
2244 printf(" RAID Levels :%s%s%s%s%s\n",
2245 imsm_orom_has_raid0(orom
) ? " raid0" : "",
2246 imsm_orom_has_raid1(orom
) ? " raid1" : "",
2247 imsm_orom_has_raid1e(orom
) ? " raid1e" : "",
2248 imsm_orom_has_raid10(orom
) ? " raid10" : "",
2249 imsm_orom_has_raid5(orom
) ? " raid5" : "");
2250 printf(" Chunk Sizes :%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
2251 imsm_orom_has_chunk(orom
, 2) ? " 2k" : "",
2252 imsm_orom_has_chunk(orom
, 4) ? " 4k" : "",
2253 imsm_orom_has_chunk(orom
, 8) ? " 8k" : "",
2254 imsm_orom_has_chunk(orom
, 16) ? " 16k" : "",
2255 imsm_orom_has_chunk(orom
, 32) ? " 32k" : "",
2256 imsm_orom_has_chunk(orom
, 64) ? " 64k" : "",
2257 imsm_orom_has_chunk(orom
, 128) ? " 128k" : "",
2258 imsm_orom_has_chunk(orom
, 256) ? " 256k" : "",
2259 imsm_orom_has_chunk(orom
, 512) ? " 512k" : "",
2260 imsm_orom_has_chunk(orom
, 1024*1) ? " 1M" : "",
2261 imsm_orom_has_chunk(orom
, 1024*2) ? " 2M" : "",
2262 imsm_orom_has_chunk(orom
, 1024*4) ? " 4M" : "",
2263 imsm_orom_has_chunk(orom
, 1024*8) ? " 8M" : "",
2264 imsm_orom_has_chunk(orom
, 1024*16) ? " 16M" : "",
2265 imsm_orom_has_chunk(orom
, 1024*32) ? " 32M" : "",
2266 imsm_orom_has_chunk(orom
, 1024*64) ? " 64M" : "");
2267 printf(" 2TB volumes :%s supported\n",
2268 (orom
->attr
& IMSM_OROM_ATTR_2TB
)?"":" not");
2269 printf(" 2TB disks :%s supported\n",
2270 (orom
->attr
& IMSM_OROM_ATTR_2TB_DISK
)?"":" not");
2271 printf(" Max Disks : %d\n", orom
->tds
);
2272 printf(" Max Volumes : %d per array, %d per %s\n",
2273 orom
->vpa
, orom
->vphba
,
2274 imsm_orom_is_nvme(orom
) ? "platform" : "controller");
2278 static void print_imsm_capability_export(const struct imsm_orom
*orom
)
2280 printf("MD_FIRMWARE_TYPE=imsm\n");
2281 if (orom
->major_ver
|| orom
->minor_ver
|| orom
->hotfix_ver
|| orom
->build
)
2282 printf("IMSM_VERSION=%d.%d.%d.%d\n", orom
->major_ver
, orom
->minor_ver
,
2283 orom
->hotfix_ver
, orom
->build
);
2284 printf("IMSM_SUPPORTED_RAID_LEVELS=%s%s%s%s%s\n",
2285 imsm_orom_has_raid0(orom
) ? "raid0 " : "",
2286 imsm_orom_has_raid1(orom
) ? "raid1 " : "",
2287 imsm_orom_has_raid1e(orom
) ? "raid1e " : "",
2288 imsm_orom_has_raid5(orom
) ? "raid10 " : "",
2289 imsm_orom_has_raid10(orom
) ? "raid5 " : "");
2290 printf("IMSM_SUPPORTED_CHUNK_SIZES=%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
2291 imsm_orom_has_chunk(orom
, 2) ? "2k " : "",
2292 imsm_orom_has_chunk(orom
, 4) ? "4k " : "",
2293 imsm_orom_has_chunk(orom
, 8) ? "8k " : "",
2294 imsm_orom_has_chunk(orom
, 16) ? "16k " : "",
2295 imsm_orom_has_chunk(orom
, 32) ? "32k " : "",
2296 imsm_orom_has_chunk(orom
, 64) ? "64k " : "",
2297 imsm_orom_has_chunk(orom
, 128) ? "128k " : "",
2298 imsm_orom_has_chunk(orom
, 256) ? "256k " : "",
2299 imsm_orom_has_chunk(orom
, 512) ? "512k " : "",
2300 imsm_orom_has_chunk(orom
, 1024*1) ? "1M " : "",
2301 imsm_orom_has_chunk(orom
, 1024*2) ? "2M " : "",
2302 imsm_orom_has_chunk(orom
, 1024*4) ? "4M " : "",
2303 imsm_orom_has_chunk(orom
, 1024*8) ? "8M " : "",
2304 imsm_orom_has_chunk(orom
, 1024*16) ? "16M " : "",
2305 imsm_orom_has_chunk(orom
, 1024*32) ? "32M " : "",
2306 imsm_orom_has_chunk(orom
, 1024*64) ? "64M " : "");
2307 printf("IMSM_2TB_VOLUMES=%s\n",(orom
->attr
& IMSM_OROM_ATTR_2TB
) ? "yes" : "no");
2308 printf("IMSM_2TB_DISKS=%s\n",(orom
->attr
& IMSM_OROM_ATTR_2TB_DISK
) ? "yes" : "no");
2309 printf("IMSM_MAX_DISKS=%d\n",orom
->tds
);
2310 printf("IMSM_MAX_VOLUMES_PER_ARRAY=%d\n",orom
->vpa
);
2311 printf("IMSM_MAX_VOLUMES_PER_CONTROLLER=%d\n",orom
->vphba
);
2314 static int detail_platform_imsm(int verbose
, int enumerate_only
, char *controller_path
)
2316 /* There are two components to imsm platform support, the ahci SATA
2317 * controller and the option-rom. To find the SATA controller we
2318 * simply look in /sys/bus/pci/drivers/ahci to see if an ahci
2319 * controller with the Intel vendor id is present. This approach
2320 * allows mdadm to leverage the kernel's ahci detection logic, with the
2321 * caveat that if ahci.ko is not loaded mdadm will not be able to
2322 * detect platform raid capabilities. The option-rom resides in a
2323 * platform "Adapter ROM". We scan for its signature to retrieve the
2324 * platform capabilities. If raid support is disabled in the BIOS the
2325 * option-rom capability structure will not be available.
2327 struct sys_dev
*list
, *hba
;
2332 if (enumerate_only
) {
2333 if (check_env("IMSM_NO_PLATFORM"))
2335 list
= find_intel_devices();
2338 for (hba
= list
; hba
; hba
= hba
->next
) {
2339 if (find_imsm_capability(hba
)) {
2349 list
= find_intel_devices();
2352 pr_err("no active Intel(R) RAID controller found.\n");
2354 } else if (verbose
> 0)
2355 print_found_intel_controllers(list
);
2357 for (hba
= list
; hba
; hba
= hba
->next
) {
2358 if (controller_path
&& (compare_paths(hba
->path
, controller_path
) != 0))
2360 if (!find_imsm_capability(hba
)) {
2362 pr_err("imsm capabilities not found for controller: %s (type %s)\n",
2363 hba
->type
== SYS_DEV_VMD
? vmd_domain_to_controller(hba
, buf
) : hba
->path
,
2364 get_sys_dev_type(hba
->type
));
2370 if (controller_path
&& result
== 1) {
2371 pr_err("no active Intel(R) RAID controller found under %s\n",
2376 const struct orom_entry
*entry
;
2378 for (entry
= orom_entries
; entry
; entry
= entry
->next
) {
2379 if (entry
->type
== SYS_DEV_VMD
) {
2380 print_imsm_capability(&entry
->orom
);
2381 for (hba
= list
; hba
; hba
= hba
->next
) {
2382 if (hba
->type
== SYS_DEV_VMD
) {
2384 printf(" I/O Controller : %s (%s)\n",
2385 vmd_domain_to_controller(hba
, buf
), get_sys_dev_type(hba
->type
));
2386 if (print_vmd_attached_devs(hba
)) {
2388 pr_err("failed to get devices attached to VMD domain.\n");
2397 print_imsm_capability(&entry
->orom
);
2398 if (entry
->type
== SYS_DEV_NVME
) {
2399 for (hba
= list
; hba
; hba
= hba
->next
) {
2400 if (hba
->type
== SYS_DEV_NVME
)
2401 printf(" NVMe Device : %s\n", hba
->path
);
2407 struct devid_list
*devid
;
2408 for (devid
= entry
->devid_list
; devid
; devid
= devid
->next
) {
2409 hba
= device_by_id(devid
->devid
);
2413 printf(" I/O Controller : %s (%s)\n",
2414 hba
->path
, get_sys_dev_type(hba
->type
));
2415 if (hba
->type
== SYS_DEV_SATA
) {
2416 host_base
= ahci_get_port_count(hba
->path
, &port_count
);
2417 if (ahci_enumerate_ports(hba
->path
, port_count
, host_base
, verbose
)) {
2419 pr_err("failed to enumerate ports on SATA controller at %s.\n", hba
->pci_id
);
2430 static int export_detail_platform_imsm(int verbose
, char *controller_path
)
2432 struct sys_dev
*list
, *hba
;
2435 list
= find_intel_devices();
2438 pr_err("IMSM_DETAIL_PLATFORM_ERROR=NO_INTEL_DEVICES\n");
2443 for (hba
= list
; hba
; hba
= hba
->next
) {
2444 if (controller_path
&& (compare_paths(hba
->path
,controller_path
) != 0))
2446 if (!find_imsm_capability(hba
) && verbose
> 0) {
2448 pr_err("IMSM_DETAIL_PLATFORM_ERROR=NO_IMSM_CAPABLE_DEVICE_UNDER_%s\n",
2449 hba
->type
== SYS_DEV_VMD
? vmd_domain_to_controller(hba
, buf
) : hba
->path
);
2455 const struct orom_entry
*entry
;
2457 for (entry
= orom_entries
; entry
; entry
= entry
->next
) {
2458 if (entry
->type
== SYS_DEV_VMD
) {
2459 for (hba
= list
; hba
; hba
= hba
->next
)
2460 print_imsm_capability_export(&entry
->orom
);
2463 print_imsm_capability_export(&entry
->orom
);
2471 static int match_home_imsm(struct supertype
*st
, char *homehost
)
2473 /* the imsm metadata format does not specify any host
2474 * identification information. We return -1 since we can never
2475 * confirm nor deny whether a given array is "meant" for this
2476 * host. We rely on compare_super and the 'family_num' fields to
2477 * exclude member disks that do not belong, and we rely on
2478 * mdadm.conf to specify the arrays that should be assembled.
2479 * Auto-assembly may still pick up "foreign" arrays.
2485 static void uuid_from_super_imsm(struct supertype
*st
, int uuid
[4])
2487 /* The uuid returned here is used for:
2488 * uuid to put into bitmap file (Create, Grow)
2489 * uuid for backup header when saving critical section (Grow)
2490 * comparing uuids when re-adding a device into an array
2491 * In these cases the uuid required is that of the data-array,
2492 * not the device-set.
2493 * uuid to recognise same set when adding a missing device back
2494 * to an array. This is a uuid for the device-set.
2496 * For each of these we can make do with a truncated
2497 * or hashed uuid rather than the original, as long as
2499 * In each case the uuid required is that of the data-array,
2500 * not the device-set.
2502 /* imsm does not track uuid's so we synthesis one using sha1 on
2503 * - The signature (Which is constant for all imsm array, but no matter)
2504 * - the orig_family_num of the container
2505 * - the index number of the volume
2506 * - the 'serial' number of the volume.
2507 * Hopefully these are all constant.
2509 struct intel_super
*super
= st
->sb
;
2512 struct sha1_ctx ctx
;
2513 struct imsm_dev
*dev
= NULL
;
2516 /* some mdadm versions failed to set ->orig_family_num, in which
2517 * case fall back to ->family_num. orig_family_num will be
2518 * fixed up with the first metadata update.
2520 family_num
= super
->anchor
->orig_family_num
;
2521 if (family_num
== 0)
2522 family_num
= super
->anchor
->family_num
;
2523 sha1_init_ctx(&ctx
);
2524 sha1_process_bytes(super
->anchor
->sig
, MPB_SIG_LEN
, &ctx
);
2525 sha1_process_bytes(&family_num
, sizeof(__u32
), &ctx
);
2526 if (super
->current_vol
>= 0)
2527 dev
= get_imsm_dev(super
, super
->current_vol
);
2529 __u32 vol
= super
->current_vol
;
2530 sha1_process_bytes(&vol
, sizeof(vol
), &ctx
);
2531 sha1_process_bytes(dev
->volume
, MAX_RAID_SERIAL_LEN
, &ctx
);
2533 sha1_finish_ctx(&ctx
, buf
);
2534 memcpy(uuid
, buf
, 4*4);
2539 get_imsm_numerical_version(struct imsm_super
*mpb
, int *m
, int *p
)
2541 __u8
*v
= get_imsm_version(mpb
);
2542 __u8
*end
= mpb
->sig
+ MAX_SIGNATURE_LENGTH
;
2543 char major
[] = { 0, 0, 0 };
2544 char minor
[] = { 0 ,0, 0 };
2545 char patch
[] = { 0, 0, 0 };
2546 char *ver_parse
[] = { major
, minor
, patch
};
2550 while (*v
!= '\0' && v
< end
) {
2551 if (*v
!= '.' && j
< 2)
2552 ver_parse
[i
][j
++] = *v
;
2560 *m
= strtol(minor
, NULL
, 0);
2561 *p
= strtol(patch
, NULL
, 0);
2565 static __u32
migr_strip_blocks_resync(struct imsm_dev
*dev
)
2567 /* migr_strip_size when repairing or initializing parity */
2568 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
2569 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
2571 switch (get_imsm_raid_level(map
)) {
2576 return 128*1024 >> 9;
2580 static __u32
migr_strip_blocks_rebuild(struct imsm_dev
*dev
)
2582 /* migr_strip_size when rebuilding a degraded disk, no idea why
2583 * this is different than migr_strip_size_resync(), but it's good
2586 struct imsm_map
*map
= get_imsm_map(dev
, MAP_1
);
2587 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
2589 switch (get_imsm_raid_level(map
)) {
2592 if (map
->num_members
% map
->num_domains
== 0)
2593 return 128*1024 >> 9;
2597 return max((__u32
) 64*1024 >> 9, chunk
);
2599 return 128*1024 >> 9;
2603 static __u32
num_stripes_per_unit_resync(struct imsm_dev
*dev
)
2605 struct imsm_map
*lo
= get_imsm_map(dev
, MAP_0
);
2606 struct imsm_map
*hi
= get_imsm_map(dev
, MAP_1
);
2607 __u32 lo_chunk
= __le32_to_cpu(lo
->blocks_per_strip
);
2608 __u32 hi_chunk
= __le32_to_cpu(hi
->blocks_per_strip
);
2610 return max((__u32
) 1, hi_chunk
/ lo_chunk
);
2613 static __u32
num_stripes_per_unit_rebuild(struct imsm_dev
*dev
)
2615 struct imsm_map
*lo
= get_imsm_map(dev
, MAP_0
);
2616 int level
= get_imsm_raid_level(lo
);
2618 if (level
== 1 || level
== 10) {
2619 struct imsm_map
*hi
= get_imsm_map(dev
, MAP_1
);
2621 return hi
->num_domains
;
2623 return num_stripes_per_unit_resync(dev
);
2626 static __u8
imsm_num_data_members(struct imsm_dev
*dev
, int second_map
)
2628 /* named 'imsm_' because raid0, raid1 and raid10
2629 * counter-intuitively have the same number of data disks
2631 struct imsm_map
*map
= get_imsm_map(dev
, second_map
);
2633 switch (get_imsm_raid_level(map
)) {
2635 return map
->num_members
;
2639 return map
->num_members
/2;
2641 return map
->num_members
- 1;
2643 dprintf("unsupported raid level\n");
2648 static __u32
parity_segment_depth(struct imsm_dev
*dev
)
2650 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
2651 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
2653 switch(get_imsm_raid_level(map
)) {
2656 return chunk
* map
->num_domains
;
2658 return chunk
* map
->num_members
;
2664 static __u32
map_migr_block(struct imsm_dev
*dev
, __u32 block
)
2666 struct imsm_map
*map
= get_imsm_map(dev
, MAP_1
);
2667 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
2668 __u32 strip
= block
/ chunk
;
2670 switch (get_imsm_raid_level(map
)) {
2673 __u32 vol_strip
= (strip
* map
->num_domains
) + 1;
2674 __u32 vol_stripe
= vol_strip
/ map
->num_members
;
2676 return vol_stripe
* chunk
+ block
% chunk
;
2678 __u32 stripe
= strip
/ (map
->num_members
- 1);
2680 return stripe
* chunk
+ block
% chunk
;
2687 static __u64
blocks_per_migr_unit(struct intel_super
*super
,
2688 struct imsm_dev
*dev
)
2690 /* calculate the conversion factor between per member 'blocks'
2691 * (md/{resync,rebuild}_start) and imsm migration units, return
2692 * 0 for the 'not migrating' and 'unsupported migration' cases
2694 if (!dev
->vol
.migr_state
)
2697 switch (migr_type(dev
)) {
2698 case MIGR_GEN_MIGR
: {
2699 struct migr_record
*migr_rec
= super
->migr_rec
;
2700 return __le32_to_cpu(migr_rec
->blocks_per_unit
);
2705 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
2706 __u32 stripes_per_unit
;
2707 __u32 blocks_per_unit
;
2716 /* yes, this is really the translation of migr_units to
2717 * per-member blocks in the 'resync' case
2719 stripes_per_unit
= num_stripes_per_unit_resync(dev
);
2720 migr_chunk
= migr_strip_blocks_resync(dev
);
2721 disks
= imsm_num_data_members(dev
, MAP_0
);
2722 blocks_per_unit
= stripes_per_unit
* migr_chunk
* disks
;
2723 stripe
= __le16_to_cpu(map
->blocks_per_strip
) * disks
;
2724 segment
= blocks_per_unit
/ stripe
;
2725 block_rel
= blocks_per_unit
- segment
* stripe
;
2726 parity_depth
= parity_segment_depth(dev
);
2727 block_map
= map_migr_block(dev
, block_rel
);
2728 return block_map
+ parity_depth
* segment
;
2730 case MIGR_REBUILD
: {
2731 __u32 stripes_per_unit
;
2734 stripes_per_unit
= num_stripes_per_unit_rebuild(dev
);
2735 migr_chunk
= migr_strip_blocks_rebuild(dev
);
2736 return migr_chunk
* stripes_per_unit
;
2738 case MIGR_STATE_CHANGE
:
2744 static int imsm_level_to_layout(int level
)
2752 return ALGORITHM_LEFT_ASYMMETRIC
;
2759 /*******************************************************************************
2760 * Function: read_imsm_migr_rec
2761 * Description: Function reads imsm migration record from last sector of disk
2763 * fd : disk descriptor
2764 * super : metadata info
2768 ******************************************************************************/
2769 static int read_imsm_migr_rec(int fd
, struct intel_super
*super
)
2772 unsigned int sector_size
= super
->sector_size
;
2773 unsigned long long dsize
;
2775 get_dev_size(fd
, NULL
, &dsize
);
2776 if (lseek64(fd
, dsize
- (sector_size
*MIGR_REC_SECTOR_POSITION
),
2778 pr_err("Cannot seek to anchor block: %s\n",
2782 if (read(fd
, super
->migr_rec_buf
,
2783 MIGR_REC_BUF_SECTORS
*sector_size
) !=
2784 MIGR_REC_BUF_SECTORS
*sector_size
) {
2785 pr_err("Cannot read migr record block: %s\n",
2790 if (sector_size
== 4096)
2791 convert_from_4k_imsm_migr_rec(super
);
2797 static struct imsm_dev
*imsm_get_device_during_migration(
2798 struct intel_super
*super
)
2801 struct intel_dev
*dv
;
2803 for (dv
= super
->devlist
; dv
; dv
= dv
->next
) {
2804 if (is_gen_migration(dv
->dev
))
2810 /*******************************************************************************
2811 * Function: load_imsm_migr_rec
2812 * Description: Function reads imsm migration record (it is stored at the last
2815 * super : imsm internal array info
2816 * info : general array info
2820 * -2 : no migration in progress
2821 ******************************************************************************/
2822 static int load_imsm_migr_rec(struct intel_super
*super
, struct mdinfo
*info
)
2829 struct imsm_dev
*dev
;
2830 struct imsm_map
*map
;
2833 /* find map under migration */
2834 dev
= imsm_get_device_during_migration(super
);
2835 /* nothing to load,no migration in progress?
2841 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
2842 /* read only from one of the first two slots */
2843 if ((sd
->disk
.raid_disk
< 0) ||
2844 (sd
->disk
.raid_disk
> 1))
2847 sprintf(nm
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
2848 fd
= dev_open(nm
, O_RDONLY
);
2854 map
= get_imsm_map(dev
, MAP_0
);
2855 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
2856 /* skip spare and failed disks
2860 /* read only from one of the first two slots */
2862 slot
= get_imsm_disk_slot(map
, dl
->index
);
2863 if (map
== NULL
|| slot
> 1 || slot
< 0)
2865 sprintf(nm
, "%d:%d", dl
->major
, dl
->minor
);
2866 fd
= dev_open(nm
, O_RDONLY
);
2873 retval
= read_imsm_migr_rec(fd
, super
);
2882 /*******************************************************************************
2883 * function: imsm_create_metadata_checkpoint_update
2884 * Description: It creates update for checkpoint change.
2886 * super : imsm internal array info
2887 * u : pointer to prepared update
2890 * If length is equal to 0, input pointer u contains no update
2891 ******************************************************************************/
2892 static int imsm_create_metadata_checkpoint_update(
2893 struct intel_super
*super
,
2894 struct imsm_update_general_migration_checkpoint
**u
)
2897 int update_memory_size
= 0;
2899 dprintf("(enter)\n");
2905 /* size of all update data without anchor */
2906 update_memory_size
=
2907 sizeof(struct imsm_update_general_migration_checkpoint
);
2909 *u
= xcalloc(1, update_memory_size
);
2911 dprintf("error: cannot get memory\n");
2914 (*u
)->type
= update_general_migration_checkpoint
;
2915 (*u
)->curr_migr_unit
= __le32_to_cpu(super
->migr_rec
->curr_migr_unit
);
2916 dprintf("prepared for %u\n", (*u
)->curr_migr_unit
);
2918 return update_memory_size
;
2921 static void imsm_update_metadata_locally(struct supertype
*st
,
2922 void *buf
, int len
);
2924 /*******************************************************************************
2925 * Function: write_imsm_migr_rec
2926 * Description: Function writes imsm migration record
2927 * (at the last sector of disk)
2929 * super : imsm internal array info
2933 ******************************************************************************/
2934 static int write_imsm_migr_rec(struct supertype
*st
)
2936 struct intel_super
*super
= st
->sb
;
2937 unsigned int sector_size
= super
->sector_size
;
2938 unsigned long long dsize
;
2944 struct imsm_update_general_migration_checkpoint
*u
;
2945 struct imsm_dev
*dev
;
2946 struct imsm_map
*map
;
2948 /* find map under migration */
2949 dev
= imsm_get_device_during_migration(super
);
2950 /* if no migration, write buffer anyway to clear migr_record
2951 * on disk based on first available device
2954 dev
= get_imsm_dev(super
, super
->current_vol
< 0 ? 0 :
2955 super
->current_vol
);
2957 map
= get_imsm_map(dev
, MAP_0
);
2959 if (sector_size
== 4096)
2960 convert_to_4k_imsm_migr_rec(super
);
2961 for (sd
= super
->disks
; sd
; sd
= sd
->next
) {
2964 /* skip failed and spare devices */
2967 /* write to 2 first slots only */
2969 slot
= get_imsm_disk_slot(map
, sd
->index
);
2970 if (map
== NULL
|| slot
> 1 || slot
< 0)
2973 sprintf(nm
, "%d:%d", sd
->major
, sd
->minor
);
2974 fd
= dev_open(nm
, O_RDWR
);
2977 get_dev_size(fd
, NULL
, &dsize
);
2978 if (lseek64(fd
, dsize
- (MIGR_REC_SECTOR_POSITION
*sector_size
),
2980 pr_err("Cannot seek to anchor block: %s\n",
2984 if (write(fd
, super
->migr_rec_buf
,
2985 MIGR_REC_BUF_SECTORS
*sector_size
) !=
2986 MIGR_REC_BUF_SECTORS
*sector_size
) {
2987 pr_err("Cannot write migr record block: %s\n",
2994 if (sector_size
== 4096)
2995 convert_from_4k_imsm_migr_rec(super
);
2996 /* update checkpoint information in metadata */
2997 len
= imsm_create_metadata_checkpoint_update(super
, &u
);
2999 dprintf("imsm: Cannot prepare update\n");
3002 /* update metadata locally */
3003 imsm_update_metadata_locally(st
, u
, len
);
3004 /* and possibly remotely */
3005 if (st
->update_tail
) {
3006 append_metadata_update(st
, u
, len
);
3007 /* during reshape we do all work inside metadata handler
3008 * manage_reshape(), so metadata update has to be triggered
3011 flush_metadata_updates(st
);
3012 st
->update_tail
= &st
->updates
;
3022 #endif /* MDASSEMBLE */
3024 /* spare/missing disks activations are not allowe when
3025 * array/container performs reshape operation, because
3026 * all arrays in container works on the same disks set
3028 int imsm_reshape_blocks_arrays_changes(struct intel_super
*super
)
3031 struct intel_dev
*i_dev
;
3032 struct imsm_dev
*dev
;
3034 /* check whole container
3036 for (i_dev
= super
->devlist
; i_dev
; i_dev
= i_dev
->next
) {
3038 if (is_gen_migration(dev
)) {
3039 /* No repair during any migration in container
3047 static unsigned long long imsm_component_size_aligment_check(int level
,
3049 unsigned int sector_size
,
3050 unsigned long long component_size
)
3052 unsigned int component_size_alligment
;
3054 /* check component size aligment
3056 component_size_alligment
= component_size
% (chunk_size
/sector_size
);
3058 dprintf("(Level: %i, chunk_size = %i, component_size = %llu), component_size_alligment = %u\n",
3059 level
, chunk_size
, component_size
,
3060 component_size_alligment
);
3062 if (component_size_alligment
&& (level
!= 1) && (level
!= UnSet
)) {
3063 dprintf("imsm: reported component size alligned from %llu ",
3065 component_size
-= component_size_alligment
;
3066 dprintf_cont("to %llu (%i).\n",
3067 component_size
, component_size_alligment
);
3070 return component_size
;
3073 static void getinfo_super_imsm_volume(struct supertype
*st
, struct mdinfo
*info
, char *dmap
)
3075 struct intel_super
*super
= st
->sb
;
3076 struct migr_record
*migr_rec
= super
->migr_rec
;
3077 struct imsm_dev
*dev
= get_imsm_dev(super
, super
->current_vol
);
3078 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
3079 struct imsm_map
*prev_map
= get_imsm_map(dev
, MAP_1
);
3080 struct imsm_map
*map_to_analyse
= map
;
3082 int map_disks
= info
->array
.raid_disks
;
3084 memset(info
, 0, sizeof(*info
));
3086 map_to_analyse
= prev_map
;
3088 dl
= super
->current_disk
;
3090 info
->container_member
= super
->current_vol
;
3091 info
->array
.raid_disks
= map
->num_members
;
3092 info
->array
.level
= get_imsm_raid_level(map_to_analyse
);
3093 info
->array
.layout
= imsm_level_to_layout(info
->array
.level
);
3094 info
->array
.md_minor
= -1;
3095 info
->array
.ctime
= 0;
3096 info
->array
.utime
= 0;
3097 info
->array
.chunk_size
=
3098 __le16_to_cpu(map_to_analyse
->blocks_per_strip
) << 9;
3099 info
->array
.state
= !dev
->vol
.dirty
;
3100 info
->custom_array_size
= __le32_to_cpu(dev
->size_high
);
3101 info
->custom_array_size
<<= 32;
3102 info
->custom_array_size
|= __le32_to_cpu(dev
->size_low
);
3103 info
->recovery_blocked
= imsm_reshape_blocks_arrays_changes(st
->sb
);
3105 if (is_gen_migration(dev
)) {
3106 info
->reshape_active
= 1;
3107 info
->new_level
= get_imsm_raid_level(map
);
3108 info
->new_layout
= imsm_level_to_layout(info
->new_level
);
3109 info
->new_chunk
= __le16_to_cpu(map
->blocks_per_strip
) << 9;
3110 info
->delta_disks
= map
->num_members
- prev_map
->num_members
;
3111 if (info
->delta_disks
) {
3112 /* this needs to be applied to every array
3115 info
->reshape_active
= CONTAINER_RESHAPE
;
3117 /* We shape information that we give to md might have to be
3118 * modify to cope with md's requirement for reshaping arrays.
3119 * For example, when reshaping a RAID0, md requires it to be
3120 * presented as a degraded RAID4.
3121 * Also if a RAID0 is migrating to a RAID5 we need to specify
3122 * the array as already being RAID5, but the 'before' layout
3123 * is a RAID4-like layout.
3125 switch (info
->array
.level
) {
3127 switch(info
->new_level
) {
3129 /* conversion is happening as RAID4 */
3130 info
->array
.level
= 4;
3131 info
->array
.raid_disks
+= 1;
3134 /* conversion is happening as RAID5 */
3135 info
->array
.level
= 5;
3136 info
->array
.layout
= ALGORITHM_PARITY_N
;
3137 info
->delta_disks
-= 1;
3140 /* FIXME error message */
3141 info
->array
.level
= UnSet
;
3147 info
->new_level
= UnSet
;
3148 info
->new_layout
= UnSet
;
3149 info
->new_chunk
= info
->array
.chunk_size
;
3150 info
->delta_disks
= 0;
3154 info
->disk
.major
= dl
->major
;
3155 info
->disk
.minor
= dl
->minor
;
3156 info
->disk
.number
= dl
->index
;
3157 info
->disk
.raid_disk
= get_imsm_disk_slot(map_to_analyse
,
3161 info
->data_offset
= pba_of_lba0(map_to_analyse
);
3163 if (info
->array
.level
== 5) {
3164 info
->component_size
= num_data_stripes(map_to_analyse
) *
3165 map_to_analyse
->blocks_per_strip
;
3167 info
->component_size
= blocks_per_member(map_to_analyse
);
3170 info
->component_size
= imsm_component_size_aligment_check(
3172 info
->array
.chunk_size
,
3174 info
->component_size
);
3175 info
->bb
.supported
= 0;
3177 memset(info
->uuid
, 0, sizeof(info
->uuid
));
3178 info
->recovery_start
= MaxSector
;
3180 info
->reshape_progress
= 0;
3181 info
->resync_start
= MaxSector
;
3182 if ((map_to_analyse
->map_state
== IMSM_T_STATE_UNINITIALIZED
||
3184 imsm_reshape_blocks_arrays_changes(super
) == 0) {
3185 info
->resync_start
= 0;
3187 if (dev
->vol
.migr_state
) {
3188 switch (migr_type(dev
)) {
3191 __u64 blocks_per_unit
= blocks_per_migr_unit(super
,
3193 __u64 units
= __le32_to_cpu(dev
->vol
.curr_migr_unit
);
3195 info
->resync_start
= blocks_per_unit
* units
;
3198 case MIGR_GEN_MIGR
: {
3199 __u64 blocks_per_unit
= blocks_per_migr_unit(super
,
3201 __u64 units
= __le32_to_cpu(migr_rec
->curr_migr_unit
);
3202 unsigned long long array_blocks
;
3205 if (__le32_to_cpu(migr_rec
->ascending_migr
) &&
3207 (__le32_to_cpu(migr_rec
->num_migr_units
)-1)) &&
3208 (super
->migr_rec
->rec_status
==
3209 __cpu_to_le32(UNIT_SRC_IN_CP_AREA
)))
3212 info
->reshape_progress
= blocks_per_unit
* units
;
3214 dprintf("IMSM: General Migration checkpoint : %llu (%llu) -> read reshape progress : %llu\n",
3215 (unsigned long long)units
,
3216 (unsigned long long)blocks_per_unit
,
3217 info
->reshape_progress
);
3219 used_disks
= imsm_num_data_members(dev
, MAP_1
);
3220 if (used_disks
> 0) {
3221 array_blocks
= blocks_per_member(map
) *
3223 /* round array size down to closest MB
3225 info
->custom_array_size
= (array_blocks
3226 >> SECT_PER_MB_SHIFT
)
3227 << SECT_PER_MB_SHIFT
;
3231 /* we could emulate the checkpointing of
3232 * 'sync_action=check' migrations, but for now
3233 * we just immediately complete them
3236 /* this is handled by container_content_imsm() */
3237 case MIGR_STATE_CHANGE
:
3238 /* FIXME handle other migrations */
3240 /* we are not dirty, so... */
3241 info
->resync_start
= MaxSector
;
3245 strncpy(info
->name
, (char *) dev
->volume
, MAX_RAID_SERIAL_LEN
);
3246 info
->name
[MAX_RAID_SERIAL_LEN
] = 0;
3248 info
->array
.major_version
= -1;
3249 info
->array
.minor_version
= -2;
3250 sprintf(info
->text_version
, "/%s/%d", st
->container_devnm
, info
->container_member
);
3251 info
->safe_mode_delay
= 4000; /* 4 secs like the Matrix driver */
3252 uuid_from_super_imsm(st
, info
->uuid
);
3256 for (i
=0; i
<map_disks
; i
++) {
3258 if (i
< info
->array
.raid_disks
) {
3259 struct imsm_disk
*dsk
;
3260 j
= get_imsm_disk_idx(dev
, i
, MAP_X
);
3261 dsk
= get_imsm_disk(super
, j
);
3262 if (dsk
&& (dsk
->status
& CONFIGURED_DISK
))
3269 static __u8
imsm_check_degraded(struct intel_super
*super
, struct imsm_dev
*dev
,
3270 int failed
, int look_in_map
);
3272 static int imsm_count_failed(struct intel_super
*super
, struct imsm_dev
*dev
,
3276 static void manage_second_map(struct intel_super
*super
, struct imsm_dev
*dev
)
3278 if (is_gen_migration(dev
)) {
3281 struct imsm_map
*map2
= get_imsm_map(dev
, MAP_1
);
3283 failed
= imsm_count_failed(super
, dev
, MAP_1
);
3284 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_1
);
3285 if (map2
->map_state
!= map_state
) {
3286 map2
->map_state
= map_state
;
3287 super
->updates_pending
++;
3293 static struct imsm_disk
*get_imsm_missing(struct intel_super
*super
, __u8 index
)
3297 for (d
= super
->missing
; d
; d
= d
->next
)
3298 if (d
->index
== index
)
3303 static void getinfo_super_imsm(struct supertype
*st
, struct mdinfo
*info
, char *map
)
3305 struct intel_super
*super
= st
->sb
;
3306 struct imsm_disk
*disk
;
3307 int map_disks
= info
->array
.raid_disks
;
3308 int max_enough
= -1;
3310 struct imsm_super
*mpb
;
3312 if (super
->current_vol
>= 0) {
3313 getinfo_super_imsm_volume(st
, info
, map
);
3316 memset(info
, 0, sizeof(*info
));
3318 /* Set raid_disks to zero so that Assemble will always pull in valid
3321 info
->array
.raid_disks
= 0;
3322 info
->array
.level
= LEVEL_CONTAINER
;
3323 info
->array
.layout
= 0;
3324 info
->array
.md_minor
= -1;
3325 info
->array
.ctime
= 0; /* N/A for imsm */
3326 info
->array
.utime
= 0;
3327 info
->array
.chunk_size
= 0;
3329 info
->disk
.major
= 0;
3330 info
->disk
.minor
= 0;
3331 info
->disk
.raid_disk
= -1;
3332 info
->reshape_active
= 0;
3333 info
->array
.major_version
= -1;
3334 info
->array
.minor_version
= -2;
3335 strcpy(info
->text_version
, "imsm");
3336 info
->safe_mode_delay
= 0;
3337 info
->disk
.number
= -1;
3338 info
->disk
.state
= 0;
3340 info
->recovery_start
= MaxSector
;
3341 info
->recovery_blocked
= imsm_reshape_blocks_arrays_changes(st
->sb
);
3342 info
->bb
.supported
= 0;
3344 /* do we have the all the insync disks that we expect? */
3345 mpb
= super
->anchor
;
3347 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
3348 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
3349 int failed
, enough
, j
, missing
= 0;
3350 struct imsm_map
*map
;
3353 failed
= imsm_count_failed(super
, dev
, MAP_0
);
3354 state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
3355 map
= get_imsm_map(dev
, MAP_0
);
3357 /* any newly missing disks?
3358 * (catches single-degraded vs double-degraded)
3360 for (j
= 0; j
< map
->num_members
; j
++) {
3361 __u32 ord
= get_imsm_ord_tbl_ent(dev
, j
, MAP_0
);
3362 __u32 idx
= ord_to_idx(ord
);
3364 if (!(ord
& IMSM_ORD_REBUILD
) &&
3365 get_imsm_missing(super
, idx
)) {
3371 if (state
== IMSM_T_STATE_FAILED
)
3373 else if (state
== IMSM_T_STATE_DEGRADED
&&
3374 (state
!= map
->map_state
|| missing
))
3376 else /* we're normal, or already degraded */
3378 if (is_gen_migration(dev
) && missing
) {
3379 /* during general migration we need all disks
3380 * that process is running on.
3381 * No new missing disk is allowed.
3385 /* no more checks necessary
3389 /* in the missing/failed disk case check to see
3390 * if at least one array is runnable
3392 max_enough
= max(max_enough
, enough
);
3394 dprintf("enough: %d\n", max_enough
);
3395 info
->container_enough
= max_enough
;
3398 __u32 reserved
= imsm_reserved_sectors(super
, super
->disks
);
3400 disk
= &super
->disks
->disk
;
3401 info
->data_offset
= total_blocks(&super
->disks
->disk
) - reserved
;
3402 info
->component_size
= reserved
;
3403 info
->disk
.state
= is_configured(disk
) ? (1 << MD_DISK_ACTIVE
) : 0;
3404 /* we don't change info->disk.raid_disk here because
3405 * this state will be finalized in mdmon after we have
3406 * found the 'most fresh' version of the metadata
3408 info
->disk
.state
|= is_failed(disk
) ? (1 << MD_DISK_FAULTY
) : 0;
3409 info
->disk
.state
|= is_spare(disk
) ? 0 : (1 << MD_DISK_SYNC
);
3412 /* only call uuid_from_super_imsm when this disk is part of a populated container,
3413 * ->compare_super may have updated the 'num_raid_devs' field for spares
3415 if (info
->disk
.state
& (1 << MD_DISK_SYNC
) || super
->anchor
->num_raid_devs
)
3416 uuid_from_super_imsm(st
, info
->uuid
);
3418 memcpy(info
->uuid
, uuid_zero
, sizeof(uuid_zero
));
3420 /* I don't know how to compute 'map' on imsm, so use safe default */
3423 for (i
= 0; i
< map_disks
; i
++)
3429 /* allocates memory and fills disk in mdinfo structure
3430 * for each disk in array */
3431 struct mdinfo
*getinfo_super_disks_imsm(struct supertype
*st
)
3433 struct mdinfo
*mddev
;
3434 struct intel_super
*super
= st
->sb
;
3435 struct imsm_disk
*disk
;
3438 if (!super
|| !super
->disks
)
3441 mddev
= xcalloc(1, sizeof(*mddev
));
3445 tmp
= xcalloc(1, sizeof(*tmp
));
3447 tmp
->next
= mddev
->devs
;
3449 tmp
->disk
.number
= count
++;
3450 tmp
->disk
.major
= dl
->major
;
3451 tmp
->disk
.minor
= dl
->minor
;
3452 tmp
->disk
.state
= is_configured(disk
) ?
3453 (1 << MD_DISK_ACTIVE
) : 0;
3454 tmp
->disk
.state
|= is_failed(disk
) ? (1 << MD_DISK_FAULTY
) : 0;
3455 tmp
->disk
.state
|= is_spare(disk
) ? 0 : (1 << MD_DISK_SYNC
);
3456 tmp
->disk
.raid_disk
= -1;
3462 static int update_super_imsm(struct supertype
*st
, struct mdinfo
*info
,
3463 char *update
, char *devname
, int verbose
,
3464 int uuid_set
, char *homehost
)
3466 /* For 'assemble' and 'force' we need to return non-zero if any
3467 * change was made. For others, the return value is ignored.
3468 * Update options are:
3469 * force-one : This device looks a bit old but needs to be included,
3470 * update age info appropriately.
3471 * assemble: clear any 'faulty' flag to allow this device to
3473 * force-array: Array is degraded but being forced, mark it clean
3474 * if that will be needed to assemble it.
3476 * newdev: not used ????
3477 * grow: Array has gained a new device - this is currently for
3479 * resync: mark as dirty so a resync will happen.
3480 * name: update the name - preserving the homehost
3481 * uuid: Change the uuid of the array to match watch is given
3483 * Following are not relevant for this imsm:
3484 * sparc2.2 : update from old dodgey metadata
3485 * super-minor: change the preferred_minor number
3486 * summaries: update redundant counters.
3487 * homehost: update the recorded homehost
3488 * _reshape_progress: record new reshape_progress position.
3491 struct intel_super
*super
= st
->sb
;
3492 struct imsm_super
*mpb
;
3494 /* we can only update container info */
3495 if (!super
|| super
->current_vol
>= 0 || !super
->anchor
)
3498 mpb
= super
->anchor
;
3500 if (strcmp(update
, "uuid") == 0) {
3501 /* We take this to mean that the family_num should be updated.
3502 * However that is much smaller than the uuid so we cannot really
3503 * allow an explicit uuid to be given. And it is hard to reliably
3505 * So if !uuid_set we know the current uuid is random and just used
3506 * the first 'int' and copy it to the other 3 positions.
3507 * Otherwise we require the 4 'int's to be the same as would be the
3508 * case if we are using a random uuid. So an explicit uuid will be
3509 * accepted as long as all for ints are the same... which shouldn't hurt
3512 info
->uuid
[1] = info
->uuid
[2] = info
->uuid
[3] = info
->uuid
[0];
3515 if (info
->uuid
[0] != info
->uuid
[1] ||
3516 info
->uuid
[1] != info
->uuid
[2] ||
3517 info
->uuid
[2] != info
->uuid
[3])
3523 mpb
->orig_family_num
= info
->uuid
[0];
3524 } else if (strcmp(update
, "assemble") == 0)
3529 /* successful update? recompute checksum */
3531 mpb
->check_sum
= __le32_to_cpu(__gen_imsm_checksum(mpb
));
3536 static size_t disks_to_mpb_size(int disks
)
3540 size
= sizeof(struct imsm_super
);
3541 size
+= (disks
- 1) * sizeof(struct imsm_disk
);
3542 size
+= 2 * sizeof(struct imsm_dev
);
3543 /* up to 2 maps per raid device (-2 for imsm_maps in imsm_dev */
3544 size
+= (4 - 2) * sizeof(struct imsm_map
);
3545 /* 4 possible disk_ord_tbl's */
3546 size
+= 4 * (disks
- 1) * sizeof(__u32
);
3547 /* maximum bbm log */
3548 size
+= sizeof(struct bbm_log
);
3553 static __u64
avail_size_imsm(struct supertype
*st
, __u64 devsize
,
3554 unsigned long long data_offset
)
3556 if (devsize
< (MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
))
3559 return devsize
- (MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
);
3562 static void free_devlist(struct intel_super
*super
)
3564 struct intel_dev
*dv
;
3566 while (super
->devlist
) {
3567 dv
= super
->devlist
->next
;
3568 free(super
->devlist
->dev
);
3569 free(super
->devlist
);
3570 super
->devlist
= dv
;
3574 static void imsm_copy_dev(struct imsm_dev
*dest
, struct imsm_dev
*src
)
3576 memcpy(dest
, src
, sizeof_imsm_dev(src
, 0));
3579 static int compare_super_imsm(struct supertype
*st
, struct supertype
*tst
)
3583 * 0 same, or first was empty, and second was copied
3584 * 1 second had wrong number
3586 * 3 wrong other info
3588 struct intel_super
*first
= st
->sb
;
3589 struct intel_super
*sec
= tst
->sb
;
3596 /* in platform dependent environment test if the disks
3597 * use the same Intel hba
3598 * If not on Intel hba at all, allow anything.
3600 if (!check_env("IMSM_NO_PLATFORM") && first
->hba
&& sec
->hba
) {
3601 if (first
->hba
->type
!= sec
->hba
->type
) {
3603 "HBAs of devices do not match %s != %s\n",
3604 get_sys_dev_type(first
->hba
->type
),
3605 get_sys_dev_type(sec
->hba
->type
));
3608 if (first
->orom
!= sec
->orom
) {
3610 "HBAs of devices do not match %s != %s\n",
3611 first
->hba
->pci_id
, sec
->hba
->pci_id
);
3616 /* if an anchor does not have num_raid_devs set then it is a free
3619 if (first
->anchor
->num_raid_devs
> 0 &&
3620 sec
->anchor
->num_raid_devs
> 0) {
3621 /* Determine if these disks might ever have been
3622 * related. Further disambiguation can only take place
3623 * in load_super_imsm_all
3625 __u32 first_family
= first
->anchor
->orig_family_num
;
3626 __u32 sec_family
= sec
->anchor
->orig_family_num
;
3628 if (memcmp(first
->anchor
->sig
, sec
->anchor
->sig
,
3629 MAX_SIGNATURE_LENGTH
) != 0)
3632 if (first_family
== 0)
3633 first_family
= first
->anchor
->family_num
;
3634 if (sec_family
== 0)
3635 sec_family
= sec
->anchor
->family_num
;
3637 if (first_family
!= sec_family
)
3642 /* if 'first' is a spare promote it to a populated mpb with sec's
3645 if (first
->anchor
->num_raid_devs
== 0 &&
3646 sec
->anchor
->num_raid_devs
> 0) {
3648 struct intel_dev
*dv
;
3649 struct imsm_dev
*dev
;
3651 /* we need to copy raid device info from sec if an allocation
3652 * fails here we don't associate the spare
3654 for (i
= 0; i
< sec
->anchor
->num_raid_devs
; i
++) {
3655 dv
= xmalloc(sizeof(*dv
));
3656 dev
= xmalloc(sizeof_imsm_dev(get_imsm_dev(sec
, i
), 1));
3659 dv
->next
= first
->devlist
;
3660 first
->devlist
= dv
;
3662 if (i
< sec
->anchor
->num_raid_devs
) {
3663 /* allocation failure */
3664 free_devlist(first
);
3665 pr_err("imsm: failed to associate spare\n");
3668 first
->anchor
->num_raid_devs
= sec
->anchor
->num_raid_devs
;
3669 first
->anchor
->orig_family_num
= sec
->anchor
->orig_family_num
;
3670 first
->anchor
->family_num
= sec
->anchor
->family_num
;
3671 memcpy(first
->anchor
->sig
, sec
->anchor
->sig
, MAX_SIGNATURE_LENGTH
);
3672 for (i
= 0; i
< sec
->anchor
->num_raid_devs
; i
++)
3673 imsm_copy_dev(get_imsm_dev(first
, i
), get_imsm_dev(sec
, i
));
3679 static void fd2devname(int fd
, char *name
)
3683 char dname
[PATH_MAX
];
3688 if (fstat(fd
, &st
) != 0)
3690 sprintf(path
, "/sys/dev/block/%d:%d",
3691 major(st
.st_rdev
), minor(st
.st_rdev
));
3693 rv
= readlink(path
, dname
, sizeof(dname
)-1);
3698 nm
= strrchr(dname
, '/');
3701 snprintf(name
, MAX_RAID_SERIAL_LEN
, "/dev/%s", nm
);
3705 static int nvme_get_serial(int fd
, void *buf
, size_t buf_len
)
3708 char *name
= fd2kname(fd
);
3713 if (strncmp(name
, "nvme", 4) != 0)
3716 snprintf(path
, sizeof(path
) - 1, "/sys/block/%s/device/serial", name
);
3718 return load_sys(path
, buf
, buf_len
);
3721 extern int scsi_get_serial(int fd
, void *buf
, size_t buf_len
);
3723 static int imsm_read_serial(int fd
, char *devname
,
3724 __u8 serial
[MAX_RAID_SERIAL_LEN
])
3733 memset(buf
, 0, sizeof(buf
));
3735 rv
= nvme_get_serial(fd
, buf
, sizeof(buf
));
3738 rv
= scsi_get_serial(fd
, buf
, sizeof(buf
));
3740 if (rv
&& check_env("IMSM_DEVNAME_AS_SERIAL")) {
3741 memset(serial
, 0, MAX_RAID_SERIAL_LEN
);
3742 fd2devname(fd
, (char *) serial
);
3748 pr_err("Failed to retrieve serial for %s\n",
3753 /* trim all whitespace and non-printable characters and convert
3756 for (i
= 0, dest
= buf
; i
< sizeof(buf
) && buf
[i
]; i
++) {
3759 /* ':' is reserved for use in placeholder serial
3760 * numbers for missing disks
3771 /* truncate leading characters */
3772 if (len
> MAX_RAID_SERIAL_LEN
) {
3773 dest
+= len
- MAX_RAID_SERIAL_LEN
;
3774 len
= MAX_RAID_SERIAL_LEN
;
3777 memset(serial
, 0, MAX_RAID_SERIAL_LEN
);
3778 memcpy(serial
, dest
, len
);
3783 static int serialcmp(__u8
*s1
, __u8
*s2
)
3785 return strncmp((char *) s1
, (char *) s2
, MAX_RAID_SERIAL_LEN
);
3788 static void serialcpy(__u8
*dest
, __u8
*src
)
3790 strncpy((char *) dest
, (char *) src
, MAX_RAID_SERIAL_LEN
);
3793 static struct dl
*serial_to_dl(__u8
*serial
, struct intel_super
*super
)
3797 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
3798 if (serialcmp(dl
->serial
, serial
) == 0)
3804 static struct imsm_disk
*
3805 __serial_to_disk(__u8
*serial
, struct imsm_super
*mpb
, int *idx
)
3809 for (i
= 0; i
< mpb
->num_disks
; i
++) {
3810 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, i
);
3812 if (serialcmp(disk
->serial
, serial
) == 0) {
3823 load_imsm_disk(int fd
, struct intel_super
*super
, char *devname
, int keep_fd
)
3825 struct imsm_disk
*disk
;
3830 __u8 serial
[MAX_RAID_SERIAL_LEN
];
3832 rv
= imsm_read_serial(fd
, devname
, serial
);
3837 dl
= xcalloc(1, sizeof(*dl
));
3840 dl
->major
= major(stb
.st_rdev
);
3841 dl
->minor
= minor(stb
.st_rdev
);
3842 dl
->next
= super
->disks
;
3843 dl
->fd
= keep_fd
? fd
: -1;
3844 assert(super
->disks
== NULL
);
3846 serialcpy(dl
->serial
, serial
);
3849 fd2devname(fd
, name
);
3851 dl
->devname
= xstrdup(devname
);
3853 dl
->devname
= xstrdup(name
);
3855 /* look up this disk's index in the current anchor */
3856 disk
= __serial_to_disk(dl
->serial
, super
->anchor
, &dl
->index
);
3859 /* only set index on disks that are a member of a
3860 * populated contianer, i.e. one with raid_devs
3862 if (is_failed(&dl
->disk
))
3864 else if (is_spare(&dl
->disk
))
3872 /* When migrating map0 contains the 'destination' state while map1
3873 * contains the current state. When not migrating map0 contains the
3874 * current state. This routine assumes that map[0].map_state is set to
3875 * the current array state before being called.
3877 * Migration is indicated by one of the following states
3878 * 1/ Idle (migr_state=0 map0state=normal||unitialized||degraded||failed)
3879 * 2/ Initialize (migr_state=1 migr_type=MIGR_INIT map0state=normal
3880 * map1state=unitialized)
3881 * 3/ Repair (Resync) (migr_state=1 migr_type=MIGR_REPAIR map0state=normal
3883 * 4/ Rebuild (migr_state=1 migr_type=MIGR_REBUILD map0state=normal
3884 * map1state=degraded)
3885 * 5/ Migration (mig_state=1 migr_type=MIGR_GEN_MIGR map0state=normal
3888 static void migrate(struct imsm_dev
*dev
, struct intel_super
*super
,
3889 __u8 to_state
, int migr_type
)
3891 struct imsm_map
*dest
;
3892 struct imsm_map
*src
= get_imsm_map(dev
, MAP_0
);
3894 dev
->vol
.migr_state
= 1;
3895 set_migr_type(dev
, migr_type
);
3896 dev
->vol
.curr_migr_unit
= 0;
3897 dest
= get_imsm_map(dev
, MAP_1
);
3899 /* duplicate and then set the target end state in map[0] */
3900 memcpy(dest
, src
, sizeof_imsm_map(src
));
3901 if (migr_type
== MIGR_REBUILD
|| migr_type
== MIGR_GEN_MIGR
) {
3905 for (i
= 0; i
< src
->num_members
; i
++) {
3906 ord
= __le32_to_cpu(src
->disk_ord_tbl
[i
]);
3907 set_imsm_ord_tbl_ent(src
, i
, ord_to_idx(ord
));
3911 if (migr_type
== MIGR_GEN_MIGR
)
3912 /* Clear migration record */
3913 memset(super
->migr_rec
, 0, sizeof(struct migr_record
));
3915 src
->map_state
= to_state
;
3918 static void end_migration(struct imsm_dev
*dev
, struct intel_super
*super
,
3921 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
3922 struct imsm_map
*prev
= get_imsm_map(dev
, dev
->vol
.migr_state
== 0 ?
3926 /* merge any IMSM_ORD_REBUILD bits that were not successfully
3927 * completed in the last migration.
3929 * FIXME add support for raid-level-migration
3931 if (map_state
!= map
->map_state
&& (is_gen_migration(dev
) == 0) &&
3932 prev
->map_state
!= IMSM_T_STATE_UNINITIALIZED
) {
3933 /* when final map state is other than expected
3934 * merge maps (not for migration)
3938 for (i
= 0; i
< prev
->num_members
; i
++)
3939 for (j
= 0; j
< map
->num_members
; j
++)
3940 /* during online capacity expansion
3941 * disks position can be changed
3942 * if takeover is used
3944 if (ord_to_idx(map
->disk_ord_tbl
[j
]) ==
3945 ord_to_idx(prev
->disk_ord_tbl
[i
])) {
3946 map
->disk_ord_tbl
[j
] |=
3947 prev
->disk_ord_tbl
[i
];
3950 failed
= imsm_count_failed(super
, dev
, MAP_0
);
3951 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
3954 dev
->vol
.migr_state
= 0;
3955 set_migr_type(dev
, 0);
3956 dev
->vol
.curr_migr_unit
= 0;
3957 map
->map_state
= map_state
;
3961 static int parse_raid_devices(struct intel_super
*super
)
3964 struct imsm_dev
*dev_new
;
3965 size_t len
, len_migr
;
3967 size_t space_needed
= 0;
3968 struct imsm_super
*mpb
= super
->anchor
;
3970 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
3971 struct imsm_dev
*dev_iter
= __get_imsm_dev(super
->anchor
, i
);
3972 struct intel_dev
*dv
;
3974 len
= sizeof_imsm_dev(dev_iter
, 0);
3975 len_migr
= sizeof_imsm_dev(dev_iter
, 1);
3977 space_needed
+= len_migr
- len
;
3979 dv
= xmalloc(sizeof(*dv
));
3980 if (max_len
< len_migr
)
3982 if (max_len
> len_migr
)
3983 space_needed
+= max_len
- len_migr
;
3984 dev_new
= xmalloc(max_len
);
3985 imsm_copy_dev(dev_new
, dev_iter
);
3988 dv
->next
= super
->devlist
;
3989 super
->devlist
= dv
;
3992 /* ensure that super->buf is large enough when all raid devices
3995 if (__le32_to_cpu(mpb
->mpb_size
) + space_needed
> super
->len
) {
3998 len
= ROUND_UP(__le32_to_cpu(mpb
->mpb_size
) + space_needed
,
3999 super
->sector_size
);
4000 if (posix_memalign(&buf
, MAX_SECTOR_SIZE
, len
) != 0)
4003 memcpy(buf
, super
->buf
, super
->len
);
4004 memset(buf
+ super
->len
, 0, len
- super
->len
);
4010 super
->extra_space
+= space_needed
;
4015 /*******************************************************************************
4016 * Function: check_mpb_migr_compatibility
4017 * Description: Function checks for unsupported migration features:
4018 * - migration optimization area (pba_of_lba0)
4019 * - descending reshape (ascending_migr)
4021 * super : imsm metadata information
4023 * 0 : migration is compatible
4024 * -1 : migration is not compatible
4025 ******************************************************************************/
4026 int check_mpb_migr_compatibility(struct intel_super
*super
)
4028 struct imsm_map
*map0
, *map1
;
4029 struct migr_record
*migr_rec
= super
->migr_rec
;
4032 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
4033 struct imsm_dev
*dev_iter
= __get_imsm_dev(super
->anchor
, i
);
4036 dev_iter
->vol
.migr_state
== 1 &&
4037 dev_iter
->vol
.migr_type
== MIGR_GEN_MIGR
) {
4038 /* This device is migrating */
4039 map0
= get_imsm_map(dev_iter
, MAP_0
);
4040 map1
= get_imsm_map(dev_iter
, MAP_1
);
4041 if (pba_of_lba0(map0
) != pba_of_lba0(map1
))
4042 /* migration optimization area was used */
4044 if (migr_rec
->ascending_migr
== 0
4045 && migr_rec
->dest_depth_per_unit
> 0)
4046 /* descending reshape not supported yet */
4053 static void __free_imsm(struct intel_super
*super
, int free_disks
);
4055 /* load_imsm_mpb - read matrix metadata
4056 * allocates super->mpb to be freed by free_imsm
4058 static int load_imsm_mpb(int fd
, struct intel_super
*super
, char *devname
)
4060 unsigned long long dsize
;
4061 unsigned long long sectors
;
4062 unsigned int sector_size
= super
->sector_size
;
4064 struct imsm_super
*anchor
;
4067 get_dev_size(fd
, NULL
, &dsize
);
4068 if (dsize
< 2*sector_size
) {
4070 pr_err("%s: device to small for imsm\n",
4075 if (lseek64(fd
, dsize
- (sector_size
* 2), SEEK_SET
) < 0) {
4077 pr_err("Cannot seek to anchor block on %s: %s\n",
4078 devname
, strerror(errno
));
4082 if (posix_memalign((void **)&anchor
, sector_size
, sector_size
) != 0) {
4084 pr_err("Failed to allocate imsm anchor buffer on %s\n", devname
);
4087 if (read(fd
, anchor
, sector_size
) != sector_size
) {
4089 pr_err("Cannot read anchor block on %s: %s\n",
4090 devname
, strerror(errno
));
4095 if (strncmp((char *) anchor
->sig
, MPB_SIGNATURE
, MPB_SIG_LEN
) != 0) {
4097 pr_err("no IMSM anchor on %s\n", devname
);
4102 __free_imsm(super
, 0);
4103 /* reload capability and hba */
4105 /* capability and hba must be updated with new super allocation */
4106 find_intel_hba_capability(fd
, super
, devname
);
4107 super
->len
= ROUND_UP(anchor
->mpb_size
, sector_size
);
4108 if (posix_memalign(&super
->buf
, MAX_SECTOR_SIZE
, super
->len
) != 0) {
4110 pr_err("unable to allocate %zu byte mpb buffer\n",
4115 memcpy(super
->buf
, anchor
, sector_size
);
4117 sectors
= mpb_sectors(anchor
, sector_size
) - 1;
4120 if (posix_memalign(&super
->migr_rec_buf
, sector_size
,
4121 MIGR_REC_BUF_SECTORS
*sector_size
) != 0) {
4122 pr_err("could not allocate migr_rec buffer\n");
4126 super
->clean_migration_record_by_mdmon
= 0;
4129 check_sum
= __gen_imsm_checksum(super
->anchor
);
4130 if (check_sum
!= __le32_to_cpu(super
->anchor
->check_sum
)) {
4132 pr_err("IMSM checksum %x != %x on %s\n",
4134 __le32_to_cpu(super
->anchor
->check_sum
),
4142 /* read the extended mpb */
4143 if (lseek64(fd
, dsize
- (sector_size
* (2 + sectors
)), SEEK_SET
) < 0) {
4145 pr_err("Cannot seek to extended mpb on %s: %s\n",
4146 devname
, strerror(errno
));
4150 if ((unsigned int)read(fd
, super
->buf
+ sector_size
,
4151 super
->len
- sector_size
) != super
->len
- sector_size
) {
4153 pr_err("Cannot read extended mpb on %s: %s\n",
4154 devname
, strerror(errno
));
4158 check_sum
= __gen_imsm_checksum(super
->anchor
);
4159 if (check_sum
!= __le32_to_cpu(super
->anchor
->check_sum
)) {
4161 pr_err("IMSM checksum %x != %x on %s\n",
4162 check_sum
, __le32_to_cpu(super
->anchor
->check_sum
),
4170 static int read_imsm_migr_rec(int fd
, struct intel_super
*super
);
4172 /* clears hi bits in metadata if MPB_ATTRIB_2TB_DISK not set */
4173 static void clear_hi(struct intel_super
*super
)
4175 struct imsm_super
*mpb
= super
->anchor
;
4177 if (mpb
->attributes
& MPB_ATTRIB_2TB_DISK
)
4179 for (i
= 0; i
< mpb
->num_disks
; ++i
) {
4180 struct imsm_disk
*disk
= &mpb
->disk
[i
];
4181 disk
->total_blocks_hi
= 0;
4183 for (i
= 0; i
< mpb
->num_raid_devs
; ++i
) {
4184 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
4187 for (n
= 0; n
< 2; ++n
) {
4188 struct imsm_map
*map
= get_imsm_map(dev
, n
);
4191 map
->pba_of_lba0_hi
= 0;
4192 map
->blocks_per_member_hi
= 0;
4193 map
->num_data_stripes_hi
= 0;
4199 load_and_parse_mpb(int fd
, struct intel_super
*super
, char *devname
, int keep_fd
)
4203 err
= load_imsm_mpb(fd
, super
, devname
);
4206 if (super
->sector_size
== 4096)
4207 convert_from_4k(super
);
4208 err
= load_imsm_disk(fd
, super
, devname
, keep_fd
);
4211 err
= parse_raid_devices(super
);
4214 err
= load_bbm_log(super
);
4219 static void __free_imsm_disk(struct dl
*d
)
4231 static void free_imsm_disks(struct intel_super
*super
)
4235 while (super
->disks
) {
4237 super
->disks
= d
->next
;
4238 __free_imsm_disk(d
);
4240 while (super
->disk_mgmt_list
) {
4241 d
= super
->disk_mgmt_list
;
4242 super
->disk_mgmt_list
= d
->next
;
4243 __free_imsm_disk(d
);
4245 while (super
->missing
) {
4247 super
->missing
= d
->next
;
4248 __free_imsm_disk(d
);
4253 /* free all the pieces hanging off of a super pointer */
4254 static void __free_imsm(struct intel_super
*super
, int free_disks
)
4256 struct intel_hba
*elem
, *next
;
4262 /* unlink capability description */
4264 if (super
->migr_rec_buf
) {
4265 free(super
->migr_rec_buf
);
4266 super
->migr_rec_buf
= NULL
;
4269 free_imsm_disks(super
);
4270 free_devlist(super
);
4274 free((void *)elem
->path
);
4280 free(super
->bbm_log
);
4284 static void free_imsm(struct intel_super
*super
)
4286 __free_imsm(super
, 1);
4287 free(super
->bb
.entries
);
4291 static void free_super_imsm(struct supertype
*st
)
4293 struct intel_super
*super
= st
->sb
;
4302 static struct intel_super
*alloc_super(void)
4304 struct intel_super
*super
= xcalloc(1, sizeof(*super
));
4306 super
->current_vol
= -1;
4307 super
->create_offset
= ~((unsigned long long) 0);
4309 super
->bb
.entries
= xmalloc(BBM_LOG_MAX_ENTRIES
*
4310 sizeof(struct md_bb_entry
));
4311 if (!super
->bb
.entries
) {
4320 * find and allocate hba and OROM/EFI based on valid fd of RAID component device
4322 static int find_intel_hba_capability(int fd
, struct intel_super
*super
, char *devname
)
4324 struct sys_dev
*hba_name
;
4327 if (fd
< 0 || check_env("IMSM_NO_PLATFORM")) {
4332 hba_name
= find_disk_attached_hba(fd
, NULL
);
4335 pr_err("%s is not attached to Intel(R) RAID controller.\n",
4339 rv
= attach_hba_to_super(super
, hba_name
);
4342 struct intel_hba
*hba
= super
->hba
;
4344 pr_err("%s is attached to Intel(R) %s %s (%s),\n"
4345 " but the container is assigned to Intel(R) %s %s (",
4347 get_sys_dev_type(hba_name
->type
),
4348 hba_name
->type
== SYS_DEV_VMD
? "domain" : "RAID controller",
4349 hba_name
->pci_id
? : "Err!",
4350 get_sys_dev_type(super
->hba
->type
),
4351 hba
->type
== SYS_DEV_VMD
? "domain" : "RAID controller");
4354 fprintf(stderr
, "%s", hba
->pci_id
? : "Err!");
4356 fprintf(stderr
, ", ");
4359 fprintf(stderr
, ").\n"
4360 " Mixing devices attached to different %s is not allowed.\n",
4361 hba_name
->type
== SYS_DEV_VMD
? "VMD domains" : "controllers");
4365 super
->orom
= find_imsm_capability(hba_name
);
4372 /* find_missing - helper routine for load_super_imsm_all that identifies
4373 * disks that have disappeared from the system. This routine relies on
4374 * the mpb being uptodate, which it is at load time.
4376 static int find_missing(struct intel_super
*super
)
4379 struct imsm_super
*mpb
= super
->anchor
;
4381 struct imsm_disk
*disk
;
4383 for (i
= 0; i
< mpb
->num_disks
; i
++) {
4384 disk
= __get_imsm_disk(mpb
, i
);
4385 dl
= serial_to_dl(disk
->serial
, super
);
4389 dl
= xmalloc(sizeof(*dl
));
4393 dl
->devname
= xstrdup("missing");
4395 serialcpy(dl
->serial
, disk
->serial
);
4398 dl
->next
= super
->missing
;
4399 super
->missing
= dl
;
4406 static struct intel_disk
*disk_list_get(__u8
*serial
, struct intel_disk
*disk_list
)
4408 struct intel_disk
*idisk
= disk_list
;
4411 if (serialcmp(idisk
->disk
.serial
, serial
) == 0)
4413 idisk
= idisk
->next
;
4419 static int __prep_thunderdome(struct intel_super
**table
, int tbl_size
,
4420 struct intel_super
*super
,
4421 struct intel_disk
**disk_list
)
4423 struct imsm_disk
*d
= &super
->disks
->disk
;
4424 struct imsm_super
*mpb
= super
->anchor
;
4427 for (i
= 0; i
< tbl_size
; i
++) {
4428 struct imsm_super
*tbl_mpb
= table
[i
]->anchor
;
4429 struct imsm_disk
*tbl_d
= &table
[i
]->disks
->disk
;
4431 if (tbl_mpb
->family_num
== mpb
->family_num
) {
4432 if (tbl_mpb
->check_sum
== mpb
->check_sum
) {
4433 dprintf("mpb from %d:%d matches %d:%d\n",
4434 super
->disks
->major
,
4435 super
->disks
->minor
,
4436 table
[i
]->disks
->major
,
4437 table
[i
]->disks
->minor
);
4441 if (((is_configured(d
) && !is_configured(tbl_d
)) ||
4442 is_configured(d
) == is_configured(tbl_d
)) &&
4443 tbl_mpb
->generation_num
< mpb
->generation_num
) {
4444 /* current version of the mpb is a
4445 * better candidate than the one in
4446 * super_table, but copy over "cross
4447 * generational" status
4449 struct intel_disk
*idisk
;
4451 dprintf("mpb from %d:%d replaces %d:%d\n",
4452 super
->disks
->major
,
4453 super
->disks
->minor
,
4454 table
[i
]->disks
->major
,
4455 table
[i
]->disks
->minor
);
4457 idisk
= disk_list_get(tbl_d
->serial
, *disk_list
);
4458 if (idisk
&& is_failed(&idisk
->disk
))
4459 tbl_d
->status
|= FAILED_DISK
;
4462 struct intel_disk
*idisk
;
4463 struct imsm_disk
*disk
;
4465 /* tbl_mpb is more up to date, but copy
4466 * over cross generational status before
4469 disk
= __serial_to_disk(d
->serial
, mpb
, NULL
);
4470 if (disk
&& is_failed(disk
))
4471 d
->status
|= FAILED_DISK
;
4473 idisk
= disk_list_get(d
->serial
, *disk_list
);
4476 if (disk
&& is_configured(disk
))
4477 idisk
->disk
.status
|= CONFIGURED_DISK
;
4480 dprintf("mpb from %d:%d prefer %d:%d\n",
4481 super
->disks
->major
,
4482 super
->disks
->minor
,
4483 table
[i
]->disks
->major
,
4484 table
[i
]->disks
->minor
);
4492 table
[tbl_size
++] = super
;
4496 /* update/extend the merged list of imsm_disk records */
4497 for (j
= 0; j
< mpb
->num_disks
; j
++) {
4498 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, j
);
4499 struct intel_disk
*idisk
;
4501 idisk
= disk_list_get(disk
->serial
, *disk_list
);
4503 idisk
->disk
.status
|= disk
->status
;
4504 if (is_configured(&idisk
->disk
) ||
4505 is_failed(&idisk
->disk
))
4506 idisk
->disk
.status
&= ~(SPARE_DISK
);
4508 idisk
= xcalloc(1, sizeof(*idisk
));
4509 idisk
->owner
= IMSM_UNKNOWN_OWNER
;
4510 idisk
->disk
= *disk
;
4511 idisk
->next
= *disk_list
;
4515 if (serialcmp(idisk
->disk
.serial
, d
->serial
) == 0)
4522 static struct intel_super
*
4523 validate_members(struct intel_super
*super
, struct intel_disk
*disk_list
,
4526 struct imsm_super
*mpb
= super
->anchor
;
4530 for (i
= 0; i
< mpb
->num_disks
; i
++) {
4531 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, i
);
4532 struct intel_disk
*idisk
;
4534 idisk
= disk_list_get(disk
->serial
, disk_list
);
4536 if (idisk
->owner
== owner
||
4537 idisk
->owner
== IMSM_UNKNOWN_OWNER
)
4540 dprintf("'%.16s' owner %d != %d\n",
4541 disk
->serial
, idisk
->owner
,
4544 dprintf("unknown disk %x [%d]: %.16s\n",
4545 __le32_to_cpu(mpb
->family_num
), i
,
4551 if (ok_count
== mpb
->num_disks
)
4556 static void show_conflicts(__u32 family_num
, struct intel_super
*super_list
)
4558 struct intel_super
*s
;
4560 for (s
= super_list
; s
; s
= s
->next
) {
4561 if (family_num
!= s
->anchor
->family_num
)
4563 pr_err("Conflict, offlining family %#x on '%s'\n",
4564 __le32_to_cpu(family_num
), s
->disks
->devname
);
4568 static struct intel_super
*
4569 imsm_thunderdome(struct intel_super
**super_list
, int len
)
4571 struct intel_super
*super_table
[len
];
4572 struct intel_disk
*disk_list
= NULL
;
4573 struct intel_super
*champion
, *spare
;
4574 struct intel_super
*s
, **del
;
4579 memset(super_table
, 0, sizeof(super_table
));
4580 for (s
= *super_list
; s
; s
= s
->next
)
4581 tbl_size
= __prep_thunderdome(super_table
, tbl_size
, s
, &disk_list
);
4583 for (i
= 0; i
< tbl_size
; i
++) {
4584 struct imsm_disk
*d
;
4585 struct intel_disk
*idisk
;
4586 struct imsm_super
*mpb
= super_table
[i
]->anchor
;
4589 d
= &s
->disks
->disk
;
4591 /* 'd' must appear in merged disk list for its
4592 * configuration to be valid
4594 idisk
= disk_list_get(d
->serial
, disk_list
);
4595 if (idisk
&& idisk
->owner
== i
)
4596 s
= validate_members(s
, disk_list
, i
);
4601 dprintf("marking family: %#x from %d:%d offline\n",
4603 super_table
[i
]->disks
->major
,
4604 super_table
[i
]->disks
->minor
);
4608 /* This is where the mdadm implementation differs from the Windows
4609 * driver which has no strict concept of a container. We can only
4610 * assemble one family from a container, so when returning a prodigal
4611 * array member to this system the code will not be able to disambiguate
4612 * the container contents that should be assembled ("foreign" versus
4613 * "local"). It requires user intervention to set the orig_family_num
4614 * to a new value to establish a new container. The Windows driver in
4615 * this situation fixes up the volume name in place and manages the
4616 * foreign array as an independent entity.
4621 for (i
= 0; i
< tbl_size
; i
++) {
4622 struct intel_super
*tbl_ent
= super_table
[i
];
4628 if (tbl_ent
->anchor
->num_raid_devs
== 0) {
4633 if (s
&& !is_spare
) {
4634 show_conflicts(tbl_ent
->anchor
->family_num
, *super_list
);
4636 } else if (!s
&& !is_spare
)
4649 pr_err("Chose family %#x on '%s', assemble conflicts to new container with '--update=uuid'\n",
4650 __le32_to_cpu(s
->anchor
->family_num
), s
->disks
->devname
);
4652 /* collect all dl's onto 'champion', and update them to
4653 * champion's version of the status
4655 for (s
= *super_list
; s
; s
= s
->next
) {
4656 struct imsm_super
*mpb
= champion
->anchor
;
4657 struct dl
*dl
= s
->disks
;
4662 mpb
->attributes
|= s
->anchor
->attributes
& MPB_ATTRIB_2TB_DISK
;
4664 for (i
= 0; i
< mpb
->num_disks
; i
++) {
4665 struct imsm_disk
*disk
;
4667 disk
= __serial_to_disk(dl
->serial
, mpb
, &dl
->index
);
4670 /* only set index on disks that are a member of
4671 * a populated contianer, i.e. one with
4674 if (is_failed(&dl
->disk
))
4676 else if (is_spare(&dl
->disk
))
4682 if (i
>= mpb
->num_disks
) {
4683 struct intel_disk
*idisk
;
4685 idisk
= disk_list_get(dl
->serial
, disk_list
);
4686 if (idisk
&& is_spare(&idisk
->disk
) &&
4687 !is_failed(&idisk
->disk
) && !is_configured(&idisk
->disk
))
4695 dl
->next
= champion
->disks
;
4696 champion
->disks
= dl
;
4700 /* delete 'champion' from super_list */
4701 for (del
= super_list
; *del
; ) {
4702 if (*del
== champion
) {
4703 *del
= (*del
)->next
;
4706 del
= &(*del
)->next
;
4708 champion
->next
= NULL
;
4712 struct intel_disk
*idisk
= disk_list
;
4714 disk_list
= disk_list
->next
;
4722 get_sra_super_block(int fd
, struct intel_super
**super_list
, char *devname
, int *max
, int keep_fd
);
4723 static int get_super_block(struct intel_super
**super_list
, char *devnm
, char *devname
,
4724 int major
, int minor
, int keep_fd
);
4726 get_devlist_super_block(struct md_list
*devlist
, struct intel_super
**super_list
,
4727 int *max
, int keep_fd
);
4729 static int load_super_imsm_all(struct supertype
*st
, int fd
, void **sbp
,
4730 char *devname
, struct md_list
*devlist
,
4733 struct intel_super
*super_list
= NULL
;
4734 struct intel_super
*super
= NULL
;
4739 /* 'fd' is an opened container */
4740 err
= get_sra_super_block(fd
, &super_list
, devname
, &i
, keep_fd
);
4742 /* get super block from devlist devices */
4743 err
= get_devlist_super_block(devlist
, &super_list
, &i
, keep_fd
);
4746 /* all mpbs enter, maybe one leaves */
4747 super
= imsm_thunderdome(&super_list
, i
);
4753 if (find_missing(super
) != 0) {
4759 /* load migration record */
4760 err
= load_imsm_migr_rec(super
, NULL
);
4762 /* migration is in progress,
4763 * but migr_rec cannot be loaded,
4769 /* Check migration compatibility */
4770 if (err
== 0 && check_mpb_migr_compatibility(super
) != 0) {
4771 pr_err("Unsupported migration detected");
4773 fprintf(stderr
, " on %s\n", devname
);
4775 fprintf(stderr
, " (IMSM).\n");
4784 while (super_list
) {
4785 struct intel_super
*s
= super_list
;
4787 super_list
= super_list
->next
;
4796 strcpy(st
->container_devnm
, fd2devnm(fd
));
4798 st
->container_devnm
[0] = 0;
4799 if (err
== 0 && st
->ss
== NULL
) {
4800 st
->ss
= &super_imsm
;
4801 st
->minor_version
= 0;
4802 st
->max_devs
= IMSM_MAX_DEVICES
;
4808 get_devlist_super_block(struct md_list
*devlist
, struct intel_super
**super_list
,
4809 int *max
, int keep_fd
)
4811 struct md_list
*tmpdev
;
4815 for (i
= 0, tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
4816 if (tmpdev
->used
!= 1)
4818 if (tmpdev
->container
== 1) {
4820 int fd
= dev_open(tmpdev
->devname
, O_RDONLY
|O_EXCL
);
4822 pr_err("cannot open device %s: %s\n",
4823 tmpdev
->devname
, strerror(errno
));
4827 err
= get_sra_super_block(fd
, super_list
,
4828 tmpdev
->devname
, &lmax
,
4837 int major
= major(tmpdev
->st_rdev
);
4838 int minor
= minor(tmpdev
->st_rdev
);
4839 err
= get_super_block(super_list
,
4856 static int get_super_block(struct intel_super
**super_list
, char *devnm
, char *devname
,
4857 int major
, int minor
, int keep_fd
)
4859 struct intel_super
*s
;
4871 sprintf(nm
, "%d:%d", major
, minor
);
4872 dfd
= dev_open(nm
, O_RDWR
);
4878 get_dev_sector_size(dfd
, NULL
, &s
->sector_size
);
4879 find_intel_hba_capability(dfd
, s
, devname
);
4880 err
= load_and_parse_mpb(dfd
, s
, NULL
, keep_fd
);
4882 /* retry the load if we might have raced against mdmon */
4883 if (err
== 3 && devnm
&& mdmon_running(devnm
))
4884 for (retry
= 0; retry
< 3; retry
++) {
4886 err
= load_and_parse_mpb(dfd
, s
, NULL
, keep_fd
);
4892 s
->next
= *super_list
;
4900 if (dfd
>= 0 && !keep_fd
)
4907 get_sra_super_block(int fd
, struct intel_super
**super_list
, char *devname
, int *max
, int keep_fd
)
4914 sra
= sysfs_read(fd
, NULL
, GET_LEVEL
|GET_VERSION
|GET_DEVS
|GET_STATE
);
4918 if (sra
->array
.major_version
!= -1 ||
4919 sra
->array
.minor_version
!= -2 ||
4920 strcmp(sra
->text_version
, "imsm") != 0) {
4925 devnm
= fd2devnm(fd
);
4926 for (sd
= sra
->devs
, i
= 0; sd
; sd
= sd
->next
, i
++) {
4927 if (get_super_block(super_list
, devnm
, devname
,
4928 sd
->disk
.major
, sd
->disk
.minor
, keep_fd
) != 0) {
4939 static int load_container_imsm(struct supertype
*st
, int fd
, char *devname
)
4941 return load_super_imsm_all(st
, fd
, &st
->sb
, devname
, NULL
, 1);
4945 static int load_super_imsm(struct supertype
*st
, int fd
, char *devname
)
4947 struct intel_super
*super
;
4951 if (test_partition(fd
))
4952 /* IMSM not allowed on partitions */
4955 free_super_imsm(st
);
4957 super
= alloc_super();
4958 get_dev_sector_size(fd
, NULL
, &super
->sector_size
);
4961 /* Load hba and capabilities if they exist.
4962 * But do not preclude loading metadata in case capabilities or hba are
4963 * non-compliant and ignore_hw_compat is set.
4965 rv
= find_intel_hba_capability(fd
, super
, devname
);
4966 /* no orom/efi or non-intel hba of the disk */
4967 if (rv
!= 0 && st
->ignore_hw_compat
== 0) {
4969 pr_err("No OROM/EFI properties for %s\n", devname
);
4973 rv
= load_and_parse_mpb(fd
, super
, devname
, 0);
4975 /* retry the load if we might have raced against mdmon */
4977 struct mdstat_ent
*mdstat
= NULL
;
4978 char *name
= fd2kname(fd
);
4981 mdstat
= mdstat_by_component(name
);
4983 if (mdstat
&& mdmon_running(mdstat
->devnm
) && getpid() != mdmon_pid(mdstat
->devnm
)) {
4984 for (retry
= 0; retry
< 3; retry
++) {
4986 rv
= load_and_parse_mpb(fd
, super
, devname
, 0);
4992 free_mdstat(mdstat
);
4997 pr_err("Failed to load all information sections on %s\n", devname
);
5003 if (st
->ss
== NULL
) {
5004 st
->ss
= &super_imsm
;
5005 st
->minor_version
= 0;
5006 st
->max_devs
= IMSM_MAX_DEVICES
;
5009 /* load migration record */
5010 if (load_imsm_migr_rec(super
, NULL
) == 0) {
5011 /* Check for unsupported migration features */
5012 if (check_mpb_migr_compatibility(super
) != 0) {
5013 pr_err("Unsupported migration detected");
5015 fprintf(stderr
, " on %s\n", devname
);
5017 fprintf(stderr
, " (IMSM).\n");
5025 static __u16
info_to_blocks_per_strip(mdu_array_info_t
*info
)
5027 if (info
->level
== 1)
5029 return info
->chunk_size
>> 9;
5032 static unsigned long long info_to_blocks_per_member(mdu_array_info_t
*info
,
5033 unsigned long long size
)
5035 if (info
->level
== 1)
5038 return (size
* 2) & ~(info_to_blocks_per_strip(info
) - 1);
5041 static void imsm_update_version_info(struct intel_super
*super
)
5043 /* update the version and attributes */
5044 struct imsm_super
*mpb
= super
->anchor
;
5046 struct imsm_dev
*dev
;
5047 struct imsm_map
*map
;
5050 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
5051 dev
= get_imsm_dev(super
, i
);
5052 map
= get_imsm_map(dev
, MAP_0
);
5053 if (__le32_to_cpu(dev
->size_high
) > 0)
5054 mpb
->attributes
|= MPB_ATTRIB_2TB
;
5056 /* FIXME detect when an array spans a port multiplier */
5058 mpb
->attributes
|= MPB_ATTRIB_PM
;
5061 if (mpb
->num_raid_devs
> 1 ||
5062 mpb
->attributes
!= MPB_ATTRIB_CHECKSUM_VERIFY
) {
5063 version
= MPB_VERSION_ATTRIBS
;
5064 switch (get_imsm_raid_level(map
)) {
5065 case 0: mpb
->attributes
|= MPB_ATTRIB_RAID0
; break;
5066 case 1: mpb
->attributes
|= MPB_ATTRIB_RAID1
; break;
5067 case 10: mpb
->attributes
|= MPB_ATTRIB_RAID10
; break;
5068 case 5: mpb
->attributes
|= MPB_ATTRIB_RAID5
; break;
5071 if (map
->num_members
>= 5)
5072 version
= MPB_VERSION_5OR6_DISK_ARRAY
;
5073 else if (dev
->status
== DEV_CLONE_N_GO
)
5074 version
= MPB_VERSION_CNG
;
5075 else if (get_imsm_raid_level(map
) == 5)
5076 version
= MPB_VERSION_RAID5
;
5077 else if (map
->num_members
>= 3)
5078 version
= MPB_VERSION_3OR4_DISK_ARRAY
;
5079 else if (get_imsm_raid_level(map
) == 1)
5080 version
= MPB_VERSION_RAID1
;
5082 version
= MPB_VERSION_RAID0
;
5084 strcpy(((char *) mpb
->sig
) + strlen(MPB_SIGNATURE
), version
);
5088 static int check_name(struct intel_super
*super
, char *name
, int quiet
)
5090 struct imsm_super
*mpb
= super
->anchor
;
5091 char *reason
= NULL
;
5094 if (strlen(name
) > MAX_RAID_SERIAL_LEN
)
5095 reason
= "must be 16 characters or less";
5097 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
5098 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
5100 if (strncmp((char *) dev
->volume
, name
, MAX_RAID_SERIAL_LEN
) == 0) {
5101 reason
= "already exists";
5106 if (reason
&& !quiet
)
5107 pr_err("imsm volume name %s\n", reason
);
5112 static int init_super_imsm_volume(struct supertype
*st
, mdu_array_info_t
*info
,
5113 unsigned long long size
, char *name
,
5114 char *homehost
, int *uuid
,
5115 long long data_offset
)
5117 /* We are creating a volume inside a pre-existing container.
5118 * so st->sb is already set.
5120 struct intel_super
*super
= st
->sb
;
5121 unsigned int sector_size
= super
->sector_size
;
5122 struct imsm_super
*mpb
= super
->anchor
;
5123 struct intel_dev
*dv
;
5124 struct imsm_dev
*dev
;
5125 struct imsm_vol
*vol
;
5126 struct imsm_map
*map
;
5127 int idx
= mpb
->num_raid_devs
;
5129 unsigned long long array_blocks
;
5130 size_t size_old
, size_new
;
5131 unsigned long long num_data_stripes
;
5133 if (super
->orom
&& mpb
->num_raid_devs
>= super
->orom
->vpa
) {
5134 pr_err("This imsm-container already has the maximum of %d volumes\n", super
->orom
->vpa
);
5138 /* ensure the mpb is large enough for the new data */
5139 size_old
= __le32_to_cpu(mpb
->mpb_size
);
5140 size_new
= disks_to_mpb_size(info
->nr_disks
);
5141 if (size_new
> size_old
) {
5143 size_t size_round
= ROUND_UP(size_new
, sector_size
);
5145 if (posix_memalign(&mpb_new
, sector_size
, size_round
) != 0) {
5146 pr_err("could not allocate new mpb\n");
5149 if (posix_memalign(&super
->migr_rec_buf
, sector_size
,
5150 MIGR_REC_BUF_SECTORS
*sector_size
) != 0) {
5151 pr_err("could not allocate migr_rec buffer\n");
5157 memcpy(mpb_new
, mpb
, size_old
);
5160 super
->anchor
= mpb_new
;
5161 mpb
->mpb_size
= __cpu_to_le32(size_new
);
5162 memset(mpb_new
+ size_old
, 0, size_round
- size_old
);
5163 super
->len
= size_round
;
5165 super
->current_vol
= idx
;
5167 /* handle 'failed_disks' by either:
5168 * a) create dummy disk entries in the table if this the first
5169 * volume in the array. We add them here as this is the only
5170 * opportunity to add them. add_to_super_imsm_volume()
5171 * handles the non-failed disks and continues incrementing
5173 * b) validate that 'failed_disks' matches the current number
5174 * of missing disks if the container is populated
5176 if (super
->current_vol
== 0) {
5178 for (i
= 0; i
< info
->failed_disks
; i
++) {
5179 struct imsm_disk
*disk
;
5182 disk
= __get_imsm_disk(mpb
, i
);
5183 disk
->status
= CONFIGURED_DISK
| FAILED_DISK
;
5184 disk
->scsi_id
= __cpu_to_le32(~(__u32
)0);
5185 snprintf((char *) disk
->serial
, MAX_RAID_SERIAL_LEN
,
5188 find_missing(super
);
5193 for (d
= super
->missing
; d
; d
= d
->next
)
5195 if (info
->failed_disks
> missing
) {
5196 pr_err("unable to add 'missing' disk to container\n");
5201 if (!check_name(super
, name
, 0))
5203 dv
= xmalloc(sizeof(*dv
));
5204 dev
= xcalloc(1, sizeof(*dev
) + sizeof(__u32
) * (info
->raid_disks
- 1));
5205 strncpy((char *) dev
->volume
, name
, MAX_RAID_SERIAL_LEN
);
5206 array_blocks
= calc_array_size(info
->level
, info
->raid_disks
,
5207 info
->layout
, info
->chunk_size
,
5209 /* round array size down to closest MB */
5210 array_blocks
= (array_blocks
>> SECT_PER_MB_SHIFT
) << SECT_PER_MB_SHIFT
;
5212 dev
->size_low
= __cpu_to_le32((__u32
) array_blocks
);
5213 dev
->size_high
= __cpu_to_le32((__u32
) (array_blocks
>> 32));
5214 dev
->status
= (DEV_READ_COALESCING
| DEV_WRITE_COALESCING
);
5216 vol
->migr_state
= 0;
5217 set_migr_type(dev
, MIGR_INIT
);
5218 vol
->dirty
= !info
->state
;
5219 vol
->curr_migr_unit
= 0;
5220 map
= get_imsm_map(dev
, MAP_0
);
5221 set_pba_of_lba0(map
, super
->create_offset
);
5222 set_blocks_per_member(map
, info_to_blocks_per_member(info
, size
));
5223 map
->blocks_per_strip
= __cpu_to_le16(info_to_blocks_per_strip(info
));
5224 map
->failed_disk_num
= ~0;
5225 if (info
->level
> 0)
5226 map
->map_state
= (info
->state
? IMSM_T_STATE_NORMAL
5227 : IMSM_T_STATE_UNINITIALIZED
);
5229 map
->map_state
= info
->failed_disks
? IMSM_T_STATE_FAILED
:
5230 IMSM_T_STATE_NORMAL
;
5233 if (info
->level
== 1 && info
->raid_disks
> 2) {
5236 pr_err("imsm does not support more than 2 disksin a raid1 volume\n");
5240 map
->raid_level
= info
->level
;
5241 if (info
->level
== 10) {
5242 map
->raid_level
= 1;
5243 map
->num_domains
= info
->raid_disks
/ 2;
5244 } else if (info
->level
== 1)
5245 map
->num_domains
= info
->raid_disks
;
5247 map
->num_domains
= 1;
5249 /* info->size is only int so use the 'size' parameter instead */
5250 num_data_stripes
= (size
* 2) / info_to_blocks_per_strip(info
);
5251 num_data_stripes
/= map
->num_domains
;
5252 set_num_data_stripes(map
, num_data_stripes
);
5254 map
->num_members
= info
->raid_disks
;
5255 for (i
= 0; i
< map
->num_members
; i
++) {
5256 /* initialized in add_to_super */
5257 set_imsm_ord_tbl_ent(map
, i
, IMSM_ORD_REBUILD
);
5259 mpb
->num_raid_devs
++;
5262 dv
->index
= super
->current_vol
;
5263 dv
->next
= super
->devlist
;
5264 super
->devlist
= dv
;
5266 imsm_update_version_info(super
);
5271 static int init_super_imsm(struct supertype
*st
, mdu_array_info_t
*info
,
5272 unsigned long long size
, char *name
,
5273 char *homehost
, int *uuid
,
5274 unsigned long long data_offset
)
5276 /* This is primarily called by Create when creating a new array.
5277 * We will then get add_to_super called for each component, and then
5278 * write_init_super called to write it out to each device.
5279 * For IMSM, Create can create on fresh devices or on a pre-existing
5281 * To create on a pre-existing array a different method will be called.
5282 * This one is just for fresh drives.
5284 struct intel_super
*super
;
5285 struct imsm_super
*mpb
;
5289 if (data_offset
!= INVALID_SECTORS
) {
5290 pr_err("data-offset not supported by imsm\n");
5295 return init_super_imsm_volume(st
, info
, size
, name
, homehost
, uuid
,
5299 mpb_size
= disks_to_mpb_size(info
->nr_disks
);
5301 mpb_size
= MAX_SECTOR_SIZE
;
5303 super
= alloc_super();
5305 posix_memalign(&super
->buf
, MAX_SECTOR_SIZE
, mpb_size
) != 0) {
5310 pr_err("could not allocate superblock\n");
5313 if (posix_memalign(&super
->migr_rec_buf
, MAX_SECTOR_SIZE
,
5314 MIGR_REC_BUF_SECTORS
*MAX_SECTOR_SIZE
) != 0) {
5315 pr_err("could not allocate migr_rec buffer\n");
5320 memset(super
->buf
, 0, mpb_size
);
5322 mpb
->mpb_size
= __cpu_to_le32(mpb_size
);
5326 /* zeroing superblock */
5330 mpb
->attributes
= MPB_ATTRIB_CHECKSUM_VERIFY
;
5332 version
= (char *) mpb
->sig
;
5333 strcpy(version
, MPB_SIGNATURE
);
5334 version
+= strlen(MPB_SIGNATURE
);
5335 strcpy(version
, MPB_VERSION_RAID0
);
5341 static int add_to_super_imsm_volume(struct supertype
*st
, mdu_disk_info_t
*dk
,
5342 int fd
, char *devname
)
5344 struct intel_super
*super
= st
->sb
;
5345 struct imsm_super
*mpb
= super
->anchor
;
5346 struct imsm_disk
*_disk
;
5347 struct imsm_dev
*dev
;
5348 struct imsm_map
*map
;
5352 dev
= get_imsm_dev(super
, super
->current_vol
);
5353 map
= get_imsm_map(dev
, MAP_0
);
5355 if (! (dk
->state
& (1<<MD_DISK_SYNC
))) {
5356 pr_err("%s: Cannot add spare devices to IMSM volume\n",
5362 /* we're doing autolayout so grab the pre-marked (in
5363 * validate_geometry) raid_disk
5365 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
5366 if (dl
->raiddisk
== dk
->raid_disk
)
5369 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
5370 if (dl
->major
== dk
->major
&&
5371 dl
->minor
== dk
->minor
)
5376 pr_err("%s is not a member of the same container\n", devname
);
5380 /* add a pristine spare to the metadata */
5381 if (dl
->index
< 0) {
5382 dl
->index
= super
->anchor
->num_disks
;
5383 super
->anchor
->num_disks
++;
5385 /* Check the device has not already been added */
5386 slot
= get_imsm_disk_slot(map
, dl
->index
);
5388 (get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
) & IMSM_ORD_REBUILD
) == 0) {
5389 pr_err("%s has been included in this array twice\n",
5393 set_imsm_ord_tbl_ent(map
, dk
->raid_disk
, dl
->index
);
5394 dl
->disk
.status
= CONFIGURED_DISK
;
5396 /* update size of 'missing' disks to be at least as large as the
5397 * largest acitve member (we only have dummy missing disks when
5398 * creating the first volume)
5400 if (super
->current_vol
== 0) {
5401 for (df
= super
->missing
; df
; df
= df
->next
) {
5402 if (total_blocks(&dl
->disk
) > total_blocks(&df
->disk
))
5403 set_total_blocks(&df
->disk
, total_blocks(&dl
->disk
));
5404 _disk
= __get_imsm_disk(mpb
, df
->index
);
5409 /* refresh unset/failed slots to point to valid 'missing' entries */
5410 for (df
= super
->missing
; df
; df
= df
->next
)
5411 for (slot
= 0; slot
< mpb
->num_disks
; slot
++) {
5412 __u32 ord
= get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
);
5414 if ((ord
& IMSM_ORD_REBUILD
) == 0)
5416 set_imsm_ord_tbl_ent(map
, slot
, df
->index
| IMSM_ORD_REBUILD
);
5417 if (is_gen_migration(dev
)) {
5418 struct imsm_map
*map2
= get_imsm_map(dev
,
5420 int slot2
= get_imsm_disk_slot(map2
, df
->index
);
5421 if (slot2
< map2
->num_members
&& slot2
>= 0) {
5422 __u32 ord2
= get_imsm_ord_tbl_ent(dev
,
5425 if ((unsigned)df
->index
==
5427 set_imsm_ord_tbl_ent(map2
,
5433 dprintf("set slot:%d to missing disk:%d\n", slot
, df
->index
);
5437 /* if we are creating the first raid device update the family number */
5438 if (super
->current_vol
== 0) {
5440 struct imsm_dev
*_dev
= __get_imsm_dev(mpb
, 0);
5442 _disk
= __get_imsm_disk(mpb
, dl
->index
);
5443 if (!_dev
|| !_disk
) {
5444 pr_err("BUG mpb setup error\n");
5450 sum
+= __gen_imsm_checksum(mpb
);
5451 mpb
->family_num
= __cpu_to_le32(sum
);
5452 mpb
->orig_family_num
= mpb
->family_num
;
5454 super
->current_disk
= dl
;
5459 * Function marks disk as spare and restores disk serial
5460 * in case it was previously marked as failed by takeover operation
5462 * -1 : critical error
5463 * 0 : disk is marked as spare but serial is not set
5466 int mark_spare(struct dl
*disk
)
5468 __u8 serial
[MAX_RAID_SERIAL_LEN
];
5475 if (!imsm_read_serial(disk
->fd
, NULL
, serial
)) {
5476 /* Restore disk serial number, because takeover marks disk
5477 * as failed and adds to serial ':0' before it becomes
5480 serialcpy(disk
->serial
, serial
);
5481 serialcpy(disk
->disk
.serial
, serial
);
5484 disk
->disk
.status
= SPARE_DISK
;
5490 static int add_to_super_imsm(struct supertype
*st
, mdu_disk_info_t
*dk
,
5491 int fd
, char *devname
,
5492 unsigned long long data_offset
)
5494 struct intel_super
*super
= st
->sb
;
5496 unsigned long long size
;
5497 unsigned int member_sector_size
;
5502 /* If we are on an RAID enabled platform check that the disk is
5503 * attached to the raid controller.
5504 * We do not need to test disks attachment for container based additions,
5505 * they shall be already tested when container was created/assembled.
5507 rv
= find_intel_hba_capability(fd
, super
, devname
);
5508 /* no orom/efi or non-intel hba of the disk */
5510 dprintf("capability: %p fd: %d ret: %d\n",
5511 super
->orom
, fd
, rv
);
5515 if (super
->current_vol
>= 0)
5516 return add_to_super_imsm_volume(st
, dk
, fd
, devname
);
5519 dd
= xcalloc(sizeof(*dd
), 1);
5520 dd
->major
= major(stb
.st_rdev
);
5521 dd
->minor
= minor(stb
.st_rdev
);
5522 dd
->devname
= devname
? xstrdup(devname
) : NULL
;
5525 dd
->action
= DISK_ADD
;
5526 rv
= imsm_read_serial(fd
, devname
, dd
->serial
);
5528 pr_err("failed to retrieve scsi serial, aborting\n");
5534 if (super
->hba
&& ((super
->hba
->type
== SYS_DEV_NVME
) ||
5535 (super
->hba
->type
== SYS_DEV_VMD
))) {
5537 char *devpath
= diskfd_to_devpath(fd
);
5538 char controller_path
[PATH_MAX
];
5541 pr_err("failed to get devpath, aborting\n");
5548 snprintf(controller_path
, PATH_MAX
-1, "%s/device", devpath
);
5551 if (devpath_to_vendor(controller_path
) == 0x8086) {
5553 * If Intel's NVMe drive has serial ended with
5554 * "-A","-B","-1" or "-2" it means that this is "x8"
5555 * device (double drive on single PCIe card).
5556 * User should be warned about potential data loss.
5558 for (i
= MAX_RAID_SERIAL_LEN
-1; i
> 0; i
--) {
5559 /* Skip empty character at the end */
5560 if (dd
->serial
[i
] == 0)
5563 if (((dd
->serial
[i
] == 'A') ||
5564 (dd
->serial
[i
] == 'B') ||
5565 (dd
->serial
[i
] == '1') ||
5566 (dd
->serial
[i
] == '2')) &&
5567 (dd
->serial
[i
-1] == '-'))
5568 pr_err("\tThe action you are about to take may put your data at risk.\n"
5569 "\tPlease note that x8 devices may consist of two separate x4 devices "
5570 "located on a single PCIe port.\n"
5571 "\tRAID 0 is the only supported configuration for this type of x8 device.\n");
5577 get_dev_size(fd
, NULL
, &size
);
5578 get_dev_sector_size(fd
, NULL
, &member_sector_size
);
5580 if (super
->sector_size
== 0) {
5581 /* this a first device, so sector_size is not set yet */
5582 super
->sector_size
= member_sector_size
;
5583 } else if (member_sector_size
!= super
->sector_size
) {
5584 pr_err("Mixing between different sector size is forbidden, aborting...\n");
5591 /* clear migr_rec when adding disk to container */
5592 memset(super
->migr_rec_buf
, 0, MIGR_REC_BUF_SECTORS
*super
->sector_size
);
5593 if (lseek64(fd
, size
- MIGR_REC_SECTOR_POSITION
*super
->sector_size
,
5595 if (write(fd
, super
->migr_rec_buf
,
5596 MIGR_REC_BUF_SECTORS
*super
->sector_size
) !=
5597 MIGR_REC_BUF_SECTORS
*super
->sector_size
)
5598 perror("Write migr_rec failed");
5602 serialcpy(dd
->disk
.serial
, dd
->serial
);
5603 set_total_blocks(&dd
->disk
, size
);
5604 if (__le32_to_cpu(dd
->disk
.total_blocks_hi
) > 0) {
5605 struct imsm_super
*mpb
= super
->anchor
;
5606 mpb
->attributes
|= MPB_ATTRIB_2TB_DISK
;
5609 if (sysfs_disk_to_scsi_id(fd
, &id
) == 0)
5610 dd
->disk
.scsi_id
= __cpu_to_le32(id
);
5612 dd
->disk
.scsi_id
= __cpu_to_le32(0);
5614 if (st
->update_tail
) {
5615 dd
->next
= super
->disk_mgmt_list
;
5616 super
->disk_mgmt_list
= dd
;
5618 dd
->next
= super
->disks
;
5620 super
->updates_pending
++;
5626 static int remove_from_super_imsm(struct supertype
*st
, mdu_disk_info_t
*dk
)
5628 struct intel_super
*super
= st
->sb
;
5631 /* remove from super works only in mdmon - for communication
5632 * manager - monitor. Check if communication memory buffer
5635 if (!st
->update_tail
) {
5636 pr_err("shall be used in mdmon context only\n");
5639 dd
= xcalloc(1, sizeof(*dd
));
5640 dd
->major
= dk
->major
;
5641 dd
->minor
= dk
->minor
;
5644 dd
->action
= DISK_REMOVE
;
5646 dd
->next
= super
->disk_mgmt_list
;
5647 super
->disk_mgmt_list
= dd
;
5652 static int store_imsm_mpb(int fd
, struct imsm_super
*mpb
);
5655 char buf
[MAX_SECTOR_SIZE
];
5656 struct imsm_super anchor
;
5657 } spare_record
__attribute__ ((aligned(MAX_SECTOR_SIZE
)));
5659 /* spare records have their own family number and do not have any defined raid
5662 static int write_super_imsm_spares(struct intel_super
*super
, int doclose
)
5664 struct imsm_super
*mpb
= super
->anchor
;
5665 struct imsm_super
*spare
= &spare_record
.anchor
;
5669 spare
->mpb_size
= __cpu_to_le32(sizeof(struct imsm_super
));
5670 spare
->generation_num
= __cpu_to_le32(1UL);
5671 spare
->attributes
= MPB_ATTRIB_CHECKSUM_VERIFY
;
5672 spare
->num_disks
= 1;
5673 spare
->num_raid_devs
= 0;
5674 spare
->cache_size
= mpb
->cache_size
;
5675 spare
->pwr_cycle_count
= __cpu_to_le32(1);
5677 snprintf((char *) spare
->sig
, MAX_SIGNATURE_LENGTH
,
5678 MPB_SIGNATURE MPB_VERSION_RAID0
);
5680 for (d
= super
->disks
; d
; d
= d
->next
) {
5684 spare
->disk
[0] = d
->disk
;
5685 if (__le32_to_cpu(d
->disk
.total_blocks_hi
) > 0)
5686 spare
->attributes
|= MPB_ATTRIB_2TB_DISK
;
5688 if (super
->sector_size
== 4096)
5689 convert_to_4k_imsm_disk(&spare
->disk
[0]);
5691 sum
= __gen_imsm_checksum(spare
);
5692 spare
->family_num
= __cpu_to_le32(sum
);
5693 spare
->orig_family_num
= 0;
5694 sum
= __gen_imsm_checksum(spare
);
5695 spare
->check_sum
= __cpu_to_le32(sum
);
5697 if (store_imsm_mpb(d
->fd
, spare
)) {
5698 pr_err("failed for device %d:%d %s\n",
5699 d
->major
, d
->minor
, strerror(errno
));
5711 static int write_super_imsm(struct supertype
*st
, int doclose
)
5713 struct intel_super
*super
= st
->sb
;
5714 unsigned int sector_size
= super
->sector_size
;
5715 struct imsm_super
*mpb
= super
->anchor
;
5721 __u32 mpb_size
= sizeof(struct imsm_super
) - sizeof(struct imsm_disk
);
5723 int clear_migration_record
= 1;
5726 /* 'generation' is incremented everytime the metadata is written */
5727 generation
= __le32_to_cpu(mpb
->generation_num
);
5729 mpb
->generation_num
= __cpu_to_le32(generation
);
5731 /* fix up cases where previous mdadm releases failed to set
5734 if (mpb
->orig_family_num
== 0)
5735 mpb
->orig_family_num
= mpb
->family_num
;
5737 for (d
= super
->disks
; d
; d
= d
->next
) {
5741 mpb
->disk
[d
->index
] = d
->disk
;
5745 for (d
= super
->missing
; d
; d
= d
->next
) {
5746 mpb
->disk
[d
->index
] = d
->disk
;
5749 mpb
->num_disks
= num_disks
;
5750 mpb_size
+= sizeof(struct imsm_disk
) * mpb
->num_disks
;
5752 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
5753 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
5754 struct imsm_dev
*dev2
= get_imsm_dev(super
, i
);
5756 imsm_copy_dev(dev
, dev2
);
5757 mpb_size
+= sizeof_imsm_dev(dev
, 0);
5759 if (is_gen_migration(dev2
))
5760 clear_migration_record
= 0;
5763 bbm_log_size
= get_imsm_bbm_log_size(super
->bbm_log
);
5766 memcpy((void *)mpb
+ mpb_size
, super
->bbm_log
, bbm_log_size
);
5767 mpb
->attributes
|= MPB_ATTRIB_BBM
;
5769 mpb
->attributes
&= ~MPB_ATTRIB_BBM
;
5771 super
->anchor
->bbm_log_size
= __cpu_to_le32(bbm_log_size
);
5772 mpb_size
+= bbm_log_size
;
5773 mpb
->mpb_size
= __cpu_to_le32(mpb_size
);
5776 assert(super
->len
== 0 || mpb_size
<= super
->len
);
5779 /* recalculate checksum */
5780 sum
= __gen_imsm_checksum(mpb
);
5781 mpb
->check_sum
= __cpu_to_le32(sum
);
5783 if (super
->clean_migration_record_by_mdmon
) {
5784 clear_migration_record
= 1;
5785 super
->clean_migration_record_by_mdmon
= 0;
5787 if (clear_migration_record
)
5788 memset(super
->migr_rec_buf
, 0,
5789 MIGR_REC_BUF_SECTORS
*sector_size
);
5791 if (sector_size
== 4096)
5792 convert_to_4k(super
);
5794 /* write the mpb for disks that compose raid devices */
5795 for (d
= super
->disks
; d
; d
= d
->next
) {
5796 if (d
->index
< 0 || is_failed(&d
->disk
))
5799 if (clear_migration_record
) {
5800 unsigned long long dsize
;
5802 get_dev_size(d
->fd
, NULL
, &dsize
);
5803 if (lseek64(d
->fd
, dsize
- sector_size
,
5805 if (write(d
->fd
, super
->migr_rec_buf
,
5806 MIGR_REC_BUF_SECTORS
*sector_size
) !=
5807 MIGR_REC_BUF_SECTORS
*sector_size
)
5808 perror("Write migr_rec failed");
5812 if (store_imsm_mpb(d
->fd
, mpb
))
5814 "failed for device %d:%d (fd: %d)%s\n",
5816 d
->fd
, strerror(errno
));
5825 return write_super_imsm_spares(super
, doclose
);
5830 static int create_array(struct supertype
*st
, int dev_idx
)
5833 struct imsm_update_create_array
*u
;
5834 struct intel_super
*super
= st
->sb
;
5835 struct imsm_dev
*dev
= get_imsm_dev(super
, dev_idx
);
5836 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
5837 struct disk_info
*inf
;
5838 struct imsm_disk
*disk
;
5841 len
= sizeof(*u
) - sizeof(*dev
) + sizeof_imsm_dev(dev
, 0) +
5842 sizeof(*inf
) * map
->num_members
;
5844 u
->type
= update_create_array
;
5845 u
->dev_idx
= dev_idx
;
5846 imsm_copy_dev(&u
->dev
, dev
);
5847 inf
= get_disk_info(u
);
5848 for (i
= 0; i
< map
->num_members
; i
++) {
5849 int idx
= get_imsm_disk_idx(dev
, i
, MAP_X
);
5851 disk
= get_imsm_disk(super
, idx
);
5853 disk
= get_imsm_missing(super
, idx
);
5854 serialcpy(inf
[i
].serial
, disk
->serial
);
5856 append_metadata_update(st
, u
, len
);
5861 static int mgmt_disk(struct supertype
*st
)
5863 struct intel_super
*super
= st
->sb
;
5865 struct imsm_update_add_remove_disk
*u
;
5867 if (!super
->disk_mgmt_list
)
5872 u
->type
= update_add_remove_disk
;
5873 append_metadata_update(st
, u
, len
);
5878 static int write_init_super_imsm(struct supertype
*st
)
5880 struct intel_super
*super
= st
->sb
;
5881 int current_vol
= super
->current_vol
;
5883 /* we are done with current_vol reset it to point st at the container */
5884 super
->current_vol
= -1;
5886 if (st
->update_tail
) {
5887 /* queue the recently created array / added disk
5888 * as a metadata update */
5891 /* determine if we are creating a volume or adding a disk */
5892 if (current_vol
< 0) {
5893 /* in the mgmt (add/remove) disk case we are running
5894 * in mdmon context, so don't close fd's
5896 return mgmt_disk(st
);
5898 rv
= create_array(st
, current_vol
);
5903 for (d
= super
->disks
; d
; d
= d
->next
)
5904 Kill(d
->devname
, NULL
, 0, -1, 1);
5905 return write_super_imsm(st
, 1);
5910 static int store_super_imsm(struct supertype
*st
, int fd
)
5912 struct intel_super
*super
= st
->sb
;
5913 struct imsm_super
*mpb
= super
? super
->anchor
: NULL
;
5919 if (super
->sector_size
== 4096)
5920 convert_to_4k(super
);
5921 return store_imsm_mpb(fd
, mpb
);
5928 static int validate_geometry_imsm_container(struct supertype
*st
, int level
,
5929 int layout
, int raiddisks
, int chunk
,
5930 unsigned long long size
,
5931 unsigned long long data_offset
,
5933 unsigned long long *freesize
,
5937 unsigned long long ldsize
;
5938 struct intel_super
*super
;
5941 if (level
!= LEVEL_CONTAINER
)
5946 fd
= open(dev
, O_RDONLY
|O_EXCL
, 0);
5949 pr_err("imsm: Cannot open %s: %s\n",
5950 dev
, strerror(errno
));
5953 if (!get_dev_size(fd
, dev
, &ldsize
)) {
5958 /* capabilities retrieve could be possible
5959 * note that there is no fd for the disks in array.
5961 super
= alloc_super();
5966 if (!get_dev_sector_size(fd
, NULL
, &super
->sector_size
)) {
5972 rv
= find_intel_hba_capability(fd
, super
, verbose
> 0 ? dev
: NULL
);
5976 fd2devname(fd
, str
);
5977 dprintf("fd: %d %s orom: %p rv: %d raiddisk: %d\n",
5978 fd
, str
, super
->orom
, rv
, raiddisks
);
5980 /* no orom/efi or non-intel hba of the disk */
5987 if (raiddisks
> super
->orom
->tds
) {
5989 pr_err("%d exceeds maximum number of platform supported disks: %d\n",
5990 raiddisks
, super
->orom
->tds
);
5994 if ((super
->orom
->attr
& IMSM_OROM_ATTR_2TB_DISK
) == 0 &&
5995 (ldsize
>> 9) >> 32 > 0) {
5997 pr_err("%s exceeds maximum platform supported size\n", dev
);
6003 *freesize
= avail_size_imsm(st
, ldsize
>> 9, data_offset
);
6009 static unsigned long long find_size(struct extent
*e
, int *idx
, int num_extents
)
6011 const unsigned long long base_start
= e
[*idx
].start
;
6012 unsigned long long end
= base_start
+ e
[*idx
].size
;
6015 if (base_start
== end
)
6019 for (i
= *idx
; i
< num_extents
; i
++) {
6020 /* extend overlapping extents */
6021 if (e
[i
].start
>= base_start
&&
6022 e
[i
].start
<= end
) {
6025 if (e
[i
].start
+ e
[i
].size
> end
)
6026 end
= e
[i
].start
+ e
[i
].size
;
6027 } else if (e
[i
].start
> end
) {
6033 return end
- base_start
;
6036 static unsigned long long merge_extents(struct intel_super
*super
, int sum_extents
)
6038 /* build a composite disk with all known extents and generate a new
6039 * 'maxsize' given the "all disks in an array must share a common start
6040 * offset" constraint
6042 struct extent
*e
= xcalloc(sum_extents
, sizeof(*e
));
6046 unsigned long long pos
;
6047 unsigned long long start
= 0;
6048 unsigned long long maxsize
;
6049 unsigned long reserve
;
6051 /* coalesce and sort all extents. also, check to see if we need to
6052 * reserve space between member arrays
6055 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
6058 for (i
= 0; i
< dl
->extent_cnt
; i
++)
6061 qsort(e
, sum_extents
, sizeof(*e
), cmp_extent
);
6066 while (i
< sum_extents
) {
6067 e
[j
].start
= e
[i
].start
;
6068 e
[j
].size
= find_size(e
, &i
, sum_extents
);
6070 if (e
[j
-1].size
== 0)
6079 unsigned long long esize
;
6081 esize
= e
[i
].start
- pos
;
6082 if (esize
>= maxsize
) {
6087 pos
= e
[i
].start
+ e
[i
].size
;
6089 } while (e
[i
-1].size
);
6095 /* FIXME assumes volume at offset 0 is the first volume in a
6098 if (start_extent
> 0)
6099 reserve
= IMSM_RESERVED_SECTORS
; /* gap between raid regions */
6103 if (maxsize
< reserve
)
6106 super
->create_offset
= ~((unsigned long long) 0);
6107 if (start
+ reserve
> super
->create_offset
)
6108 return 0; /* start overflows create_offset */
6109 super
->create_offset
= start
+ reserve
;
6111 return maxsize
- reserve
;
6114 static int is_raid_level_supported(const struct imsm_orom
*orom
, int level
, int raiddisks
)
6116 if (level
< 0 || level
== 6 || level
== 4)
6119 /* if we have an orom prevent invalid raid levels */
6122 case 0: return imsm_orom_has_raid0(orom
);
6125 return imsm_orom_has_raid1e(orom
);
6126 return imsm_orom_has_raid1(orom
) && raiddisks
== 2;
6127 case 10: return imsm_orom_has_raid10(orom
) && raiddisks
== 4;
6128 case 5: return imsm_orom_has_raid5(orom
) && raiddisks
> 2;
6131 return 1; /* not on an Intel RAID platform so anything goes */
6137 active_arrays_by_format(char *name
, char* hba
, struct md_list
**devlist
,
6138 int dpa
, int verbose
)
6140 struct mdstat_ent
*mdstat
= mdstat_read(0, 0);
6141 struct mdstat_ent
*memb
;
6147 for (memb
= mdstat
; memb
; memb
= memb
->next
) {
6148 if (memb
->metadata_version
&&
6149 (strncmp(memb
->metadata_version
, "external:", 9) == 0) &&
6150 (strcmp(&memb
->metadata_version
[9], name
) == 0) &&
6151 !is_subarray(memb
->metadata_version
+9) &&
6153 struct dev_member
*dev
= memb
->members
;
6155 while(dev
&& (fd
< 0)) {
6156 char *path
= xmalloc(strlen(dev
->name
) + strlen("/dev/") + 1);
6157 num
= sprintf(path
, "%s%s", "/dev/", dev
->name
);
6159 fd
= open(path
, O_RDONLY
, 0);
6160 if (num
<= 0 || fd
< 0) {
6161 pr_vrb("Cannot open %s: %s\n",
6162 dev
->name
, strerror(errno
));
6168 if (fd
>= 0 && disk_attached_to_hba(fd
, hba
)) {
6169 struct mdstat_ent
*vol
;
6170 for (vol
= mdstat
; vol
; vol
= vol
->next
) {
6171 if (vol
->active
> 0 &&
6172 vol
->metadata_version
&&
6173 is_container_member(vol
, memb
->devnm
)) {
6178 if (*devlist
&& (found
< dpa
)) {
6179 dv
= xcalloc(1, sizeof(*dv
));
6180 dv
->devname
= xmalloc(strlen(memb
->devnm
) + strlen("/dev/") + 1);
6181 sprintf(dv
->devname
, "%s%s", "/dev/", memb
->devnm
);
6184 dv
->next
= *devlist
;
6192 free_mdstat(mdstat
);
6197 static struct md_list
*
6198 get_loop_devices(void)
6201 struct md_list
*devlist
= NULL
;
6204 for(i
= 0; i
< 12; i
++) {
6205 dv
= xcalloc(1, sizeof(*dv
));
6206 dv
->devname
= xmalloc(40);
6207 sprintf(dv
->devname
, "/dev/loop%d", i
);
6215 static struct md_list
*
6216 get_devices(const char *hba_path
)
6218 struct md_list
*devlist
= NULL
;
6225 devlist
= get_loop_devices();
6228 /* scroll through /sys/dev/block looking for devices attached to
6231 dir
= opendir("/sys/dev/block");
6232 for (ent
= dir
? readdir(dir
) : NULL
; ent
; ent
= readdir(dir
)) {
6237 if (sscanf(ent
->d_name
, "%d:%d", &major
, &minor
) != 2)
6239 path
= devt_to_devpath(makedev(major
, minor
));
6242 if (!path_attached_to_hba(path
, hba_path
)) {
6249 fd
= dev_open(ent
->d_name
, O_RDONLY
);
6251 fd2devname(fd
, buf
);
6254 pr_err("cannot open device: %s\n",
6259 dv
= xcalloc(1, sizeof(*dv
));
6260 dv
->devname
= xstrdup(buf
);
6267 devlist
= devlist
->next
;
6277 count_volumes_list(struct md_list
*devlist
, char *homehost
,
6278 int verbose
, int *found
)
6280 struct md_list
*tmpdev
;
6282 struct supertype
*st
;
6284 /* first walk the list of devices to find a consistent set
6285 * that match the criterea, if that is possible.
6286 * We flag the ones we like with 'used'.
6289 st
= match_metadata_desc_imsm("imsm");
6291 pr_vrb("cannot allocate memory for imsm supertype\n");
6295 for (tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
6296 char *devname
= tmpdev
->devname
;
6298 struct supertype
*tst
;
6300 if (tmpdev
->used
> 1)
6302 tst
= dup_super(st
);
6304 pr_vrb("cannot allocate memory for imsm supertype\n");
6307 tmpdev
->container
= 0;
6308 dfd
= dev_open(devname
, O_RDONLY
|O_EXCL
);
6310 dprintf("cannot open device %s: %s\n",
6311 devname
, strerror(errno
));
6313 } else if (fstat(dfd
, &stb
)< 0) {
6315 dprintf("fstat failed for %s: %s\n",
6316 devname
, strerror(errno
));
6318 } else if ((stb
.st_mode
& S_IFMT
) != S_IFBLK
) {
6319 dprintf("%s is not a block device.\n",
6322 } else if (must_be_container(dfd
)) {
6323 struct supertype
*cst
;
6324 cst
= super_by_fd(dfd
, NULL
);
6326 dprintf("cannot recognize container type %s\n",
6329 } else if (tst
->ss
!= st
->ss
) {
6330 dprintf("non-imsm container - ignore it: %s\n",
6333 } else if (!tst
->ss
->load_container
||
6334 tst
->ss
->load_container(tst
, dfd
, NULL
))
6337 tmpdev
->container
= 1;
6340 cst
->ss
->free_super(cst
);
6342 tmpdev
->st_rdev
= stb
.st_rdev
;
6343 if (tst
->ss
->load_super(tst
,dfd
, NULL
)) {
6344 dprintf("no RAID superblock on %s\n",
6347 } else if (tst
->ss
->compare_super
== NULL
) {
6348 dprintf("Cannot assemble %s metadata on %s\n",
6349 tst
->ss
->name
, devname
);
6355 if (tmpdev
->used
== 2 || tmpdev
->used
== 4) {
6356 /* Ignore unrecognised devices during auto-assembly */
6361 tst
->ss
->getinfo_super(tst
, &info
, NULL
);
6363 if (st
->minor_version
== -1)
6364 st
->minor_version
= tst
->minor_version
;
6366 if (memcmp(info
.uuid
, uuid_zero
,
6367 sizeof(int[4])) == 0) {
6368 /* this is a floating spare. It cannot define
6369 * an array unless there are no more arrays of
6370 * this type to be found. It can be included
6371 * in an array of this type though.
6377 if (st
->ss
!= tst
->ss
||
6378 st
->minor_version
!= tst
->minor_version
||
6379 st
->ss
->compare_super(st
, tst
) != 0) {
6380 /* Some mismatch. If exactly one array matches this host,
6381 * we can resolve on that one.
6382 * Or, if we are auto assembling, we just ignore the second
6385 dprintf("superblock on %s doesn't match others - assembly aborted\n",
6391 dprintf("found: devname: %s\n", devname
);
6395 tst
->ss
->free_super(tst
);
6399 if ((err
= load_super_imsm_all(st
, -1, &st
->sb
, NULL
, devlist
, 0)) == 0) {
6400 struct mdinfo
*iter
, *head
= st
->ss
->container_content(st
, NULL
);
6401 for (iter
= head
; iter
; iter
= iter
->next
) {
6402 dprintf("content->text_version: %s vol\n",
6403 iter
->text_version
);
6404 if (iter
->array
.state
& (1<<MD_SB_BLOCK_VOLUME
)) {
6405 /* do not assemble arrays with unsupported
6407 dprintf("Cannot activate member %s.\n",
6408 iter
->text_version
);
6415 dprintf("No valid super block on device list: err: %d %p\n",
6419 dprintf("no more devices to examine\n");
6422 for (tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
6423 if (tmpdev
->used
== 1 && tmpdev
->found
) {
6425 if (count
< tmpdev
->found
)
6428 count
-= tmpdev
->found
;
6431 if (tmpdev
->used
== 1)
6436 st
->ss
->free_super(st
);
6441 count_volumes(struct intel_hba
*hba
, int dpa
, int verbose
)
6443 struct sys_dev
*idev
, *intel_devices
= find_intel_devices();
6445 const struct orom_entry
*entry
;
6446 struct devid_list
*dv
, *devid_list
;
6448 if (!hba
|| !hba
->path
)
6451 for (idev
= intel_devices
; idev
; idev
= idev
->next
) {
6452 if (strstr(idev
->path
, hba
->path
))
6456 if (!idev
|| !idev
->dev_id
)
6459 entry
= get_orom_entry_by_device_id(idev
->dev_id
);
6461 if (!entry
|| !entry
->devid_list
)
6464 devid_list
= entry
->devid_list
;
6465 for (dv
= devid_list
; dv
; dv
= dv
->next
) {
6466 struct md_list
*devlist
;
6467 struct sys_dev
*device
= device_by_id(dv
->devid
);
6472 hba_path
= device
->path
;
6476 devlist
= get_devices(hba_path
);
6477 /* if no intel devices return zero volumes */
6478 if (devlist
== NULL
)
6481 count
+= active_arrays_by_format("imsm", hba_path
, &devlist
, dpa
, verbose
);
6482 dprintf("path: %s active arrays: %d\n", hba_path
, count
);
6483 if (devlist
== NULL
)
6487 count
+= count_volumes_list(devlist
,
6491 dprintf("found %d count: %d\n", found
, count
);
6494 dprintf("path: %s total number of volumes: %d\n", hba_path
, count
);
6497 struct md_list
*dv
= devlist
;
6498 devlist
= devlist
->next
;
6506 static int imsm_default_chunk(const struct imsm_orom
*orom
)
6508 /* up to 512 if the plaform supports it, otherwise the platform max.
6509 * 128 if no platform detected
6511 int fs
= max(7, orom
? fls(orom
->sss
) : 0);
6513 return min(512, (1 << fs
));
6517 validate_geometry_imsm_orom(struct intel_super
*super
, int level
, int layout
,
6518 int raiddisks
, int *chunk
, unsigned long long size
, int verbose
)
6520 /* check/set platform and metadata limits/defaults */
6521 if (super
->orom
&& raiddisks
> super
->orom
->dpa
) {
6522 pr_vrb("platform supports a maximum of %d disks per array\n",
6527 /* capabilities of OROM tested - copied from validate_geometry_imsm_volume */
6528 if (!is_raid_level_supported(super
->orom
, level
, raiddisks
)) {
6529 pr_vrb("platform does not support raid%d with %d disk%s\n",
6530 level
, raiddisks
, raiddisks
> 1 ? "s" : "");
6534 if (*chunk
== 0 || *chunk
== UnSet
)
6535 *chunk
= imsm_default_chunk(super
->orom
);
6537 if (super
->orom
&& !imsm_orom_has_chunk(super
->orom
, *chunk
)) {
6538 pr_vrb("platform does not support a chunk size of: %d\n", *chunk
);
6542 if (layout
!= imsm_level_to_layout(level
)) {
6544 pr_vrb("imsm raid 5 only supports the left-asymmetric layout\n");
6545 else if (level
== 10)
6546 pr_vrb("imsm raid 10 only supports the n2 layout\n");
6548 pr_vrb("imsm unknown layout %#x for this raid level %d\n",
6553 if (super
->orom
&& (super
->orom
->attr
& IMSM_OROM_ATTR_2TB
) == 0 &&
6554 (calc_array_size(level
, raiddisks
, layout
, *chunk
, size
) >> 32) > 0) {
6555 pr_vrb("platform does not support a volume size over 2TB\n");
6562 /* validate_geometry_imsm_volume - lifted from validate_geometry_ddf_bvd
6563 * FIX ME add ahci details
6565 static int validate_geometry_imsm_volume(struct supertype
*st
, int level
,
6566 int layout
, int raiddisks
, int *chunk
,
6567 unsigned long long size
,
6568 unsigned long long data_offset
,
6570 unsigned long long *freesize
,
6574 struct intel_super
*super
= st
->sb
;
6575 struct imsm_super
*mpb
;
6577 unsigned long long pos
= 0;
6578 unsigned long long maxsize
;
6582 /* We must have the container info already read in. */
6586 mpb
= super
->anchor
;
6588 if (!validate_geometry_imsm_orom(super
, level
, layout
, raiddisks
, chunk
, size
, verbose
)) {
6589 pr_err("RAID gemetry validation failed. Cannot proceed with the action(s).\n");
6593 /* General test: make sure there is space for
6594 * 'raiddisks' device extents of size 'size' at a given
6597 unsigned long long minsize
= size
;
6598 unsigned long long start_offset
= MaxSector
;
6601 minsize
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
6602 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
6607 e
= get_extents(super
, dl
);
6610 unsigned long long esize
;
6611 esize
= e
[i
].start
- pos
;
6612 if (esize
>= minsize
)
6614 if (found
&& start_offset
== MaxSector
) {
6617 } else if (found
&& pos
!= start_offset
) {
6621 pos
= e
[i
].start
+ e
[i
].size
;
6623 } while (e
[i
-1].size
);
6628 if (dcnt
< raiddisks
) {
6630 pr_err("imsm: Not enough devices with space for this array (%d < %d)\n",
6637 /* This device must be a member of the set */
6638 if (stat(dev
, &stb
) < 0)
6640 if ((S_IFMT
& stb
.st_mode
) != S_IFBLK
)
6642 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
6643 if (dl
->major
== (int)major(stb
.st_rdev
) &&
6644 dl
->minor
== (int)minor(stb
.st_rdev
))
6649 pr_err("%s is not in the same imsm set\n", dev
);
6651 } else if (super
->orom
&& dl
->index
< 0 && mpb
->num_raid_devs
) {
6652 /* If a volume is present then the current creation attempt
6653 * cannot incorporate new spares because the orom may not
6654 * understand this configuration (all member disks must be
6655 * members of each array in the container).
6657 pr_err("%s is a spare and a volume is already defined for this container\n", dev
);
6658 pr_err("The option-rom requires all member disks to be a member of all volumes\n");
6660 } else if (super
->orom
&& mpb
->num_raid_devs
> 0 &&
6661 mpb
->num_disks
!= raiddisks
) {
6662 pr_err("The option-rom requires all member disks to be a member of all volumes\n");
6666 /* retrieve the largest free space block */
6667 e
= get_extents(super
, dl
);
6672 unsigned long long esize
;
6674 esize
= e
[i
].start
- pos
;
6675 if (esize
>= maxsize
)
6677 pos
= e
[i
].start
+ e
[i
].size
;
6679 } while (e
[i
-1].size
);
6684 pr_err("unable to determine free space for: %s\n",
6688 if (maxsize
< size
) {
6690 pr_err("%s not enough space (%llu < %llu)\n",
6691 dev
, maxsize
, size
);
6695 /* count total number of extents for merge */
6697 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
6699 i
+= dl
->extent_cnt
;
6701 maxsize
= merge_extents(super
, i
);
6703 if (!check_env("IMSM_NO_PLATFORM") &&
6704 mpb
->num_raid_devs
> 0 && size
&& size
!= maxsize
) {
6705 pr_err("attempting to create a second volume with size less then remaining space. Aborting...\n");
6709 if (maxsize
< size
|| maxsize
== 0) {
6712 pr_err("no free space left on device. Aborting...\n");
6714 pr_err("not enough space to create volume of given size (%llu < %llu). Aborting...\n",
6720 *freesize
= maxsize
;
6723 int count
= count_volumes(super
->hba
,
6724 super
->orom
->dpa
, verbose
);
6725 if (super
->orom
->vphba
<= count
) {
6726 pr_vrb("platform does not support more than %d raid volumes.\n",
6727 super
->orom
->vphba
);
6734 static int imsm_get_free_size(struct supertype
*st
, int raiddisks
,
6735 unsigned long long size
, int chunk
,
6736 unsigned long long *freesize
)
6738 struct intel_super
*super
= st
->sb
;
6739 struct imsm_super
*mpb
= super
->anchor
;
6744 unsigned long long maxsize
;
6745 unsigned long long minsize
;
6749 /* find the largest common start free region of the possible disks */
6753 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
6759 /* don't activate new spares if we are orom constrained
6760 * and there is already a volume active in the container
6762 if (super
->orom
&& dl
->index
< 0 && mpb
->num_raid_devs
)
6765 e
= get_extents(super
, dl
);
6768 for (i
= 1; e
[i
-1].size
; i
++)
6776 maxsize
= merge_extents(super
, extent_cnt
);
6780 minsize
= chunk
* 2;
6782 if (cnt
< raiddisks
||
6783 (super
->orom
&& used
&& used
!= raiddisks
) ||
6784 maxsize
< minsize
||
6786 pr_err("not enough devices with space to create array.\n");
6787 return 0; /* No enough free spaces large enough */
6798 if (!check_env("IMSM_NO_PLATFORM") &&
6799 mpb
->num_raid_devs
> 0 && size
&& size
!= maxsize
) {
6800 pr_err("attempting to create a second volume with size less then remaining space. Aborting...\n");
6804 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
6806 dl
->raiddisk
= cnt
++;
6810 dprintf("imsm: imsm_get_free_size() returns : %llu\n", size
);
6815 static int reserve_space(struct supertype
*st
, int raiddisks
,
6816 unsigned long long size
, int chunk
,
6817 unsigned long long *freesize
)
6819 struct intel_super
*super
= st
->sb
;
6824 rv
= imsm_get_free_size(st
, raiddisks
, size
, chunk
, freesize
);
6827 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
6829 dl
->raiddisk
= cnt
++;
6836 static int validate_geometry_imsm(struct supertype
*st
, int level
, int layout
,
6837 int raiddisks
, int *chunk
, unsigned long long size
,
6838 unsigned long long data_offset
,
6839 char *dev
, unsigned long long *freesize
,
6847 * if given unused devices create a container
6848 * if given given devices in a container create a member volume
6850 if (level
== LEVEL_CONTAINER
) {
6851 /* Must be a fresh device to add to a container */
6852 return validate_geometry_imsm_container(st
, level
, layout
,
6862 struct intel_super
*super
= st
->sb
;
6863 if (!validate_geometry_imsm_orom(st
->sb
, level
, layout
,
6864 raiddisks
, chunk
, size
,
6867 /* we are being asked to automatically layout a
6868 * new volume based on the current contents of
6869 * the container. If the the parameters can be
6870 * satisfied reserve_space will record the disks,
6871 * start offset, and size of the volume to be
6872 * created. add_to_super and getinfo_super
6873 * detect when autolayout is in progress.
6875 /* assuming that freesize is always given when array is
6877 if (super
->orom
&& freesize
) {
6879 count
= count_volumes(super
->hba
,
6880 super
->orom
->dpa
, verbose
);
6881 if (super
->orom
->vphba
<= count
) {
6882 pr_vrb("platform does not support more than %d raid volumes.\n",
6883 super
->orom
->vphba
);
6888 return reserve_space(st
, raiddisks
, size
,
6894 /* creating in a given container */
6895 return validate_geometry_imsm_volume(st
, level
, layout
,
6896 raiddisks
, chunk
, size
,
6898 dev
, freesize
, verbose
);
6901 /* This device needs to be a device in an 'imsm' container */
6902 fd
= open(dev
, O_RDONLY
|O_EXCL
, 0);
6905 pr_err("Cannot create this array on device %s\n",
6910 if (errno
!= EBUSY
|| (fd
= open(dev
, O_RDONLY
, 0)) < 0) {
6912 pr_err("Cannot open %s: %s\n",
6913 dev
, strerror(errno
));
6916 /* Well, it is in use by someone, maybe an 'imsm' container. */
6917 cfd
= open_container(fd
);
6921 pr_err("Cannot use %s: It is busy\n",
6925 sra
= sysfs_read(cfd
, NULL
, GET_VERSION
);
6926 if (sra
&& sra
->array
.major_version
== -1 &&
6927 strcmp(sra
->text_version
, "imsm") == 0)
6931 /* This is a member of a imsm container. Load the container
6932 * and try to create a volume
6934 struct intel_super
*super
;
6936 if (load_super_imsm_all(st
, cfd
, (void **) &super
, NULL
, NULL
, 1) == 0) {
6938 strcpy(st
->container_devnm
, fd2devnm(cfd
));
6940 return validate_geometry_imsm_volume(st
, level
, layout
,
6942 size
, data_offset
, dev
,
6949 pr_err("failed container membership check\n");
6955 static void default_geometry_imsm(struct supertype
*st
, int *level
, int *layout
, int *chunk
)
6957 struct intel_super
*super
= st
->sb
;
6959 if (level
&& *level
== UnSet
)
6960 *level
= LEVEL_CONTAINER
;
6962 if (level
&& layout
&& *layout
== UnSet
)
6963 *layout
= imsm_level_to_layout(*level
);
6965 if (chunk
&& (*chunk
== UnSet
|| *chunk
== 0))
6966 *chunk
= imsm_default_chunk(super
->orom
);
6969 static void handle_missing(struct intel_super
*super
, struct imsm_dev
*dev
);
6971 static int kill_subarray_imsm(struct supertype
*st
)
6973 /* remove the subarray currently referenced by ->current_vol */
6975 struct intel_dev
**dp
;
6976 struct intel_super
*super
= st
->sb
;
6977 __u8 current_vol
= super
->current_vol
;
6978 struct imsm_super
*mpb
= super
->anchor
;
6980 if (super
->current_vol
< 0)
6982 super
->current_vol
= -1; /* invalidate subarray cursor */
6984 /* block deletions that would change the uuid of active subarrays
6986 * FIXME when immutable ids are available, but note that we'll
6987 * also need to fixup the invalidated/active subarray indexes in
6990 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
6993 if (i
< current_vol
)
6995 sprintf(subarray
, "%u", i
);
6996 if (is_subarray_active(subarray
, st
->devnm
)) {
6997 pr_err("deleting subarray-%d would change the UUID of active subarray-%d, aborting\n",
7004 if (st
->update_tail
) {
7005 struct imsm_update_kill_array
*u
= xmalloc(sizeof(*u
));
7007 u
->type
= update_kill_array
;
7008 u
->dev_idx
= current_vol
;
7009 append_metadata_update(st
, u
, sizeof(*u
));
7014 for (dp
= &super
->devlist
; *dp
;)
7015 if ((*dp
)->index
== current_vol
) {
7018 handle_missing(super
, (*dp
)->dev
);
7019 if ((*dp
)->index
> current_vol
)
7024 /* no more raid devices, all active components are now spares,
7025 * but of course failed are still failed
7027 if (--mpb
->num_raid_devs
== 0) {
7030 for (d
= super
->disks
; d
; d
= d
->next
)
7035 super
->updates_pending
++;
7040 static int update_subarray_imsm(struct supertype
*st
, char *subarray
,
7041 char *update
, struct mddev_ident
*ident
)
7043 /* update the subarray currently referenced by ->current_vol */
7044 struct intel_super
*super
= st
->sb
;
7045 struct imsm_super
*mpb
= super
->anchor
;
7047 if (strcmp(update
, "name") == 0) {
7048 char *name
= ident
->name
;
7052 if (is_subarray_active(subarray
, st
->devnm
)) {
7053 pr_err("Unable to update name of active subarray\n");
7057 if (!check_name(super
, name
, 0))
7060 vol
= strtoul(subarray
, &ep
, 10);
7061 if (*ep
!= '\0' || vol
>= super
->anchor
->num_raid_devs
)
7064 if (st
->update_tail
) {
7065 struct imsm_update_rename_array
*u
= xmalloc(sizeof(*u
));
7067 u
->type
= update_rename_array
;
7069 snprintf((char *) u
->name
, MAX_RAID_SERIAL_LEN
, "%s", name
);
7070 append_metadata_update(st
, u
, sizeof(*u
));
7072 struct imsm_dev
*dev
;
7075 dev
= get_imsm_dev(super
, vol
);
7076 snprintf((char *) dev
->volume
, MAX_RAID_SERIAL_LEN
, "%s", name
);
7077 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
7078 dev
= get_imsm_dev(super
, i
);
7079 handle_missing(super
, dev
);
7081 super
->updates_pending
++;
7088 #endif /* MDASSEMBLE */
7090 static int is_gen_migration(struct imsm_dev
*dev
)
7095 if (!dev
->vol
.migr_state
)
7098 if (migr_type(dev
) == MIGR_GEN_MIGR
)
7104 static int is_rebuilding(struct imsm_dev
*dev
)
7106 struct imsm_map
*migr_map
;
7108 if (!dev
->vol
.migr_state
)
7111 if (migr_type(dev
) != MIGR_REBUILD
)
7114 migr_map
= get_imsm_map(dev
, MAP_1
);
7116 if (migr_map
->map_state
== IMSM_T_STATE_DEGRADED
)
7123 static int is_initializing(struct imsm_dev
*dev
)
7125 struct imsm_map
*migr_map
;
7127 if (!dev
->vol
.migr_state
)
7130 if (migr_type(dev
) != MIGR_INIT
)
7133 migr_map
= get_imsm_map(dev
, MAP_1
);
7135 if (migr_map
->map_state
== IMSM_T_STATE_UNINITIALIZED
)
7142 static void update_recovery_start(struct intel_super
*super
,
7143 struct imsm_dev
*dev
,
7144 struct mdinfo
*array
)
7146 struct mdinfo
*rebuild
= NULL
;
7150 if (!is_rebuilding(dev
))
7153 /* Find the rebuild target, but punt on the dual rebuild case */
7154 for (d
= array
->devs
; d
; d
= d
->next
)
7155 if (d
->recovery_start
== 0) {
7162 /* (?) none of the disks are marked with
7163 * IMSM_ORD_REBUILD, so assume they are missing and the
7164 * disk_ord_tbl was not correctly updated
7166 dprintf("failed to locate out-of-sync disk\n");
7170 units
= __le32_to_cpu(dev
->vol
.curr_migr_unit
);
7171 rebuild
->recovery_start
= units
* blocks_per_migr_unit(super
, dev
);
7175 static int recover_backup_imsm(struct supertype
*st
, struct mdinfo
*info
);
7178 static struct mdinfo
*container_content_imsm(struct supertype
*st
, char *subarray
)
7180 /* Given a container loaded by load_super_imsm_all,
7181 * extract information about all the arrays into
7183 * If 'subarray' is given, just extract info about that array.
7185 * For each imsm_dev create an mdinfo, fill it in,
7186 * then look for matching devices in super->disks
7187 * and create appropriate device mdinfo.
7189 struct intel_super
*super
= st
->sb
;
7190 struct imsm_super
*mpb
= super
->anchor
;
7191 struct mdinfo
*rest
= NULL
;
7195 int spare_disks
= 0;
7197 /* do not assemble arrays when not all attributes are supported */
7198 if (imsm_check_attributes(mpb
->attributes
) == 0) {
7200 pr_err("Unsupported attributes in IMSM metadata.Arrays activation is blocked.\n");
7203 /* count spare devices, not used in maps
7205 for (d
= super
->disks
; d
; d
= d
->next
)
7209 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
7210 struct imsm_dev
*dev
;
7211 struct imsm_map
*map
;
7212 struct imsm_map
*map2
;
7213 struct mdinfo
*this;
7221 (i
!= strtoul(subarray
, &ep
, 10) || *ep
!= '\0'))
7224 dev
= get_imsm_dev(super
, i
);
7225 map
= get_imsm_map(dev
, MAP_0
);
7226 map2
= get_imsm_map(dev
, MAP_1
);
7228 /* do not publish arrays that are in the middle of an
7229 * unsupported migration
7231 if (dev
->vol
.migr_state
&&
7232 (migr_type(dev
) == MIGR_STATE_CHANGE
)) {
7233 pr_err("cannot assemble volume '%.16s': unsupported migration in progress\n",
7237 /* do not publish arrays that are not support by controller's
7241 this = xmalloc(sizeof(*this));
7243 super
->current_vol
= i
;
7244 getinfo_super_imsm_volume(st
, this, NULL
);
7247 chunk
= __le16_to_cpu(map
->blocks_per_strip
) >> 1;
7248 /* mdadm does not support all metadata features- set the bit in all arrays state */
7249 if (!validate_geometry_imsm_orom(super
,
7250 get_imsm_raid_level(map
), /* RAID level */
7251 imsm_level_to_layout(get_imsm_raid_level(map
)),
7252 map
->num_members
, /* raid disks */
7253 &chunk
, join_u32(dev
->size_low
, dev
->size_high
),
7255 pr_err("IMSM RAID geometry validation failed. Array %s activation is blocked.\n",
7257 this->array
.state
|=
7258 (1<<MD_SB_BLOCK_CONTAINER_RESHAPE
) |
7259 (1<<MD_SB_BLOCK_VOLUME
);
7263 /* if array has bad blocks, set suitable bit in all arrays state */
7265 this->array
.state
|=
7266 (1<<MD_SB_BLOCK_CONTAINER_RESHAPE
) |
7267 (1<<MD_SB_BLOCK_VOLUME
);
7269 for (slot
= 0 ; slot
< map
->num_members
; slot
++) {
7270 unsigned long long recovery_start
;
7271 struct mdinfo
*info_d
;
7278 idx
= get_imsm_disk_idx(dev
, slot
, MAP_0
);
7279 ord
= get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
);
7280 for (d
= super
->disks
; d
; d
= d
->next
)
7281 if (d
->index
== idx
)
7284 recovery_start
= MaxSector
;
7287 if (d
&& is_failed(&d
->disk
))
7289 if (ord
& IMSM_ORD_REBUILD
)
7293 * if we skip some disks the array will be assmebled degraded;
7294 * reset resync start to avoid a dirty-degraded
7295 * situation when performing the intial sync
7297 * FIXME handle dirty degraded
7299 if ((skip
|| recovery_start
== 0) && !dev
->vol
.dirty
)
7300 this->resync_start
= MaxSector
;
7304 info_d
= xcalloc(1, sizeof(*info_d
));
7305 info_d
->next
= this->devs
;
7306 this->devs
= info_d
;
7308 info_d
->disk
.number
= d
->index
;
7309 info_d
->disk
.major
= d
->major
;
7310 info_d
->disk
.minor
= d
->minor
;
7311 info_d
->disk
.raid_disk
= slot
;
7312 info_d
->recovery_start
= recovery_start
;
7314 if (slot
< map2
->num_members
)
7315 info_d
->disk
.state
= (1 << MD_DISK_ACTIVE
);
7317 this->array
.spare_disks
++;
7319 if (slot
< map
->num_members
)
7320 info_d
->disk
.state
= (1 << MD_DISK_ACTIVE
);
7322 this->array
.spare_disks
++;
7324 if (info_d
->recovery_start
== MaxSector
)
7325 this->array
.working_disks
++;
7327 info_d
->events
= __le32_to_cpu(mpb
->generation_num
);
7328 info_d
->data_offset
= pba_of_lba0(map
);
7330 if (map
->raid_level
== 5) {
7331 info_d
->component_size
=
7332 num_data_stripes(map
) *
7333 map
->blocks_per_strip
;
7335 info_d
->component_size
= blocks_per_member(map
);
7338 info_d
->bb
.supported
= 0;
7339 get_volume_badblocks(super
->bbm_log
, ord_to_idx(ord
),
7340 info_d
->data_offset
,
7341 info_d
->component_size
,
7344 /* now that the disk list is up-to-date fixup recovery_start */
7345 update_recovery_start(super
, dev
, this);
7346 this->array
.spare_disks
+= spare_disks
;
7349 /* check for reshape */
7350 if (this->reshape_active
== 1)
7351 recover_backup_imsm(st
, this);
7359 static __u8
imsm_check_degraded(struct intel_super
*super
, struct imsm_dev
*dev
,
7360 int failed
, int look_in_map
)
7362 struct imsm_map
*map
;
7364 map
= get_imsm_map(dev
, look_in_map
);
7367 return map
->map_state
== IMSM_T_STATE_UNINITIALIZED
?
7368 IMSM_T_STATE_UNINITIALIZED
: IMSM_T_STATE_NORMAL
;
7370 switch (get_imsm_raid_level(map
)) {
7372 return IMSM_T_STATE_FAILED
;
7375 if (failed
< map
->num_members
)
7376 return IMSM_T_STATE_DEGRADED
;
7378 return IMSM_T_STATE_FAILED
;
7383 * check to see if any mirrors have failed, otherwise we
7384 * are degraded. Even numbered slots are mirrored on
7388 /* gcc -Os complains that this is unused */
7389 int insync
= insync
;
7391 for (i
= 0; i
< map
->num_members
; i
++) {
7392 __u32 ord
= get_imsm_ord_tbl_ent(dev
, i
, MAP_X
);
7393 int idx
= ord_to_idx(ord
);
7394 struct imsm_disk
*disk
;
7396 /* reset the potential in-sync count on even-numbered
7397 * slots. num_copies is always 2 for imsm raid10
7402 disk
= get_imsm_disk(super
, idx
);
7403 if (!disk
|| is_failed(disk
) || ord
& IMSM_ORD_REBUILD
)
7406 /* no in-sync disks left in this mirror the
7410 return IMSM_T_STATE_FAILED
;
7413 return IMSM_T_STATE_DEGRADED
;
7417 return IMSM_T_STATE_DEGRADED
;
7419 return IMSM_T_STATE_FAILED
;
7425 return map
->map_state
;
7428 static int imsm_count_failed(struct intel_super
*super
, struct imsm_dev
*dev
,
7433 struct imsm_disk
*disk
;
7434 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7435 struct imsm_map
*prev
= get_imsm_map(dev
, MAP_1
);
7436 struct imsm_map
*map_for_loop
;
7441 /* at the beginning of migration we set IMSM_ORD_REBUILD on
7442 * disks that are being rebuilt. New failures are recorded to
7443 * map[0]. So we look through all the disks we started with and
7444 * see if any failures are still present, or if any new ones
7448 if (prev
&& (map
->num_members
< prev
->num_members
))
7449 map_for_loop
= prev
;
7451 for (i
= 0; i
< map_for_loop
->num_members
; i
++) {
7453 /* when MAP_X is passed both maps failures are counted
7456 (look_in_map
== MAP_1
|| look_in_map
== MAP_X
) &&
7457 i
< prev
->num_members
) {
7458 ord
= __le32_to_cpu(prev
->disk_ord_tbl
[i
]);
7459 idx_1
= ord_to_idx(ord
);
7461 disk
= get_imsm_disk(super
, idx_1
);
7462 if (!disk
|| is_failed(disk
) || ord
& IMSM_ORD_REBUILD
)
7465 if ((look_in_map
== MAP_0
|| look_in_map
== MAP_X
) &&
7466 i
< map
->num_members
) {
7467 ord
= __le32_to_cpu(map
->disk_ord_tbl
[i
]);
7468 idx
= ord_to_idx(ord
);
7471 disk
= get_imsm_disk(super
, idx
);
7472 if (!disk
|| is_failed(disk
) ||
7473 ord
& IMSM_ORD_REBUILD
)
7483 static int imsm_open_new(struct supertype
*c
, struct active_array
*a
,
7486 struct intel_super
*super
= c
->sb
;
7487 struct imsm_super
*mpb
= super
->anchor
;
7488 struct imsm_update_prealloc_bb_mem u
;
7490 if (atoi(inst
) >= mpb
->num_raid_devs
) {
7491 pr_err("subarry index %d, out of range\n", atoi(inst
));
7495 dprintf("imsm: open_new %s\n", inst
);
7496 a
->info
.container_member
= atoi(inst
);
7498 u
.type
= update_prealloc_badblocks_mem
;
7499 imsm_update_metadata_locally(c
, &u
, sizeof(u
));
7504 static int is_resyncing(struct imsm_dev
*dev
)
7506 struct imsm_map
*migr_map
;
7508 if (!dev
->vol
.migr_state
)
7511 if (migr_type(dev
) == MIGR_INIT
||
7512 migr_type(dev
) == MIGR_REPAIR
)
7515 if (migr_type(dev
) == MIGR_GEN_MIGR
)
7518 migr_map
= get_imsm_map(dev
, MAP_1
);
7520 if (migr_map
->map_state
== IMSM_T_STATE_NORMAL
&&
7521 dev
->vol
.migr_type
!= MIGR_GEN_MIGR
)
7527 /* return true if we recorded new information */
7528 static int mark_failure(struct intel_super
*super
,
7529 struct imsm_dev
*dev
, struct imsm_disk
*disk
, int idx
)
7533 struct imsm_map
*map
;
7534 char buf
[MAX_RAID_SERIAL_LEN
+3];
7535 unsigned int len
, shift
= 0;
7537 /* new failures are always set in map[0] */
7538 map
= get_imsm_map(dev
, MAP_0
);
7540 slot
= get_imsm_disk_slot(map
, idx
);
7544 ord
= __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
7545 if (is_failed(disk
) && (ord
& IMSM_ORD_REBUILD
))
7548 memcpy(buf
, disk
->serial
, MAX_RAID_SERIAL_LEN
);
7549 buf
[MAX_RAID_SERIAL_LEN
] = '\000';
7551 if ((len
= strlen(buf
)) >= MAX_RAID_SERIAL_LEN
)
7552 shift
= len
- MAX_RAID_SERIAL_LEN
+ 1;
7553 strncpy((char *)disk
->serial
, &buf
[shift
], MAX_RAID_SERIAL_LEN
);
7555 disk
->status
|= FAILED_DISK
;
7556 set_imsm_ord_tbl_ent(map
, slot
, idx
| IMSM_ORD_REBUILD
);
7557 /* mark failures in second map if second map exists and this disk
7559 * This is valid for migration, initialization and rebuild
7561 if (dev
->vol
.migr_state
) {
7562 struct imsm_map
*map2
= get_imsm_map(dev
, MAP_1
);
7563 int slot2
= get_imsm_disk_slot(map2
, idx
);
7565 if (slot2
< map2
->num_members
&& slot2
>= 0)
7566 set_imsm_ord_tbl_ent(map2
, slot2
,
7567 idx
| IMSM_ORD_REBUILD
);
7569 if (map
->failed_disk_num
== 0xff)
7570 map
->failed_disk_num
= slot
;
7572 clear_disk_badblocks(super
->bbm_log
, ord_to_idx(ord
));
7577 static void mark_missing(struct intel_super
*super
,
7578 struct imsm_dev
*dev
, struct imsm_disk
*disk
, int idx
)
7580 mark_failure(super
, dev
, disk
, idx
);
7582 if (disk
->scsi_id
== __cpu_to_le32(~(__u32
)0))
7585 disk
->scsi_id
= __cpu_to_le32(~(__u32
)0);
7586 memmove(&disk
->serial
[0], &disk
->serial
[1], MAX_RAID_SERIAL_LEN
- 1);
7589 static void handle_missing(struct intel_super
*super
, struct imsm_dev
*dev
)
7593 if (!super
->missing
)
7596 /* When orom adds replacement for missing disk it does
7597 * not remove entry of missing disk, but just updates map with
7598 * new added disk. So it is not enough just to test if there is
7599 * any missing disk, we have to look if there are any failed disks
7600 * in map to stop migration */
7602 dprintf("imsm: mark missing\n");
7603 /* end process for initialization and rebuild only
7605 if (is_gen_migration(dev
) == 0) {
7609 failed
= imsm_count_failed(super
, dev
, MAP_0
);
7610 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
7613 end_migration(dev
, super
, map_state
);
7615 for (dl
= super
->missing
; dl
; dl
= dl
->next
)
7616 mark_missing(super
, dev
, &dl
->disk
, dl
->index
);
7617 super
->updates_pending
++;
7620 static unsigned long long imsm_set_array_size(struct imsm_dev
*dev
,
7623 int used_disks
= imsm_num_data_members(dev
, MAP_0
);
7624 unsigned long long array_blocks
;
7625 struct imsm_map
*map
;
7627 if (used_disks
== 0) {
7628 /* when problems occures
7629 * return current array_blocks value
7631 array_blocks
= __le32_to_cpu(dev
->size_high
);
7632 array_blocks
= array_blocks
<< 32;
7633 array_blocks
+= __le32_to_cpu(dev
->size_low
);
7635 return array_blocks
;
7638 /* set array size in metadata
7640 if (new_size
<= 0) {
7641 /* OLCE size change is caused by added disks
7643 map
= get_imsm_map(dev
, MAP_0
);
7644 array_blocks
= blocks_per_member(map
) * used_disks
;
7646 /* Online Volume Size Change
7647 * Using available free space
7649 array_blocks
= new_size
;
7652 /* round array size down to closest MB
7654 array_blocks
= (array_blocks
>> SECT_PER_MB_SHIFT
) << SECT_PER_MB_SHIFT
;
7655 dev
->size_low
= __cpu_to_le32((__u32
)array_blocks
);
7656 dev
->size_high
= __cpu_to_le32((__u32
)(array_blocks
>> 32));
7658 return array_blocks
;
7661 static void imsm_set_disk(struct active_array
*a
, int n
, int state
);
7663 static void imsm_progress_container_reshape(struct intel_super
*super
)
7665 /* if no device has a migr_state, but some device has a
7666 * different number of members than the previous device, start
7667 * changing the number of devices in this device to match
7670 struct imsm_super
*mpb
= super
->anchor
;
7671 int prev_disks
= -1;
7675 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
7676 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
7677 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7678 struct imsm_map
*map2
;
7679 int prev_num_members
;
7681 if (dev
->vol
.migr_state
)
7684 if (prev_disks
== -1)
7685 prev_disks
= map
->num_members
;
7686 if (prev_disks
== map
->num_members
)
7689 /* OK, this array needs to enter reshape mode.
7690 * i.e it needs a migr_state
7693 copy_map_size
= sizeof_imsm_map(map
);
7694 prev_num_members
= map
->num_members
;
7695 map
->num_members
= prev_disks
;
7696 dev
->vol
.migr_state
= 1;
7697 dev
->vol
.curr_migr_unit
= 0;
7698 set_migr_type(dev
, MIGR_GEN_MIGR
);
7699 for (i
= prev_num_members
;
7700 i
< map
->num_members
; i
++)
7701 set_imsm_ord_tbl_ent(map
, i
, i
);
7702 map2
= get_imsm_map(dev
, MAP_1
);
7703 /* Copy the current map */
7704 memcpy(map2
, map
, copy_map_size
);
7705 map2
->num_members
= prev_num_members
;
7707 imsm_set_array_size(dev
, -1);
7708 super
->clean_migration_record_by_mdmon
= 1;
7709 super
->updates_pending
++;
7713 /* Handle dirty -> clean transititions, resync and reshape. Degraded and rebuild
7714 * states are handled in imsm_set_disk() with one exception, when a
7715 * resync is stopped due to a new failure this routine will set the
7716 * 'degraded' state for the array.
7718 static int imsm_set_array_state(struct active_array
*a
, int consistent
)
7720 int inst
= a
->info
.container_member
;
7721 struct intel_super
*super
= a
->container
->sb
;
7722 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
7723 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7724 int failed
= imsm_count_failed(super
, dev
, MAP_0
);
7725 __u8 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
7726 __u32 blocks_per_unit
;
7728 if (dev
->vol
.migr_state
&&
7729 dev
->vol
.migr_type
== MIGR_GEN_MIGR
) {
7730 /* array state change is blocked due to reshape action
7732 * - abort the reshape (if last_checkpoint is 0 and action!= reshape)
7733 * - finish the reshape (if last_checkpoint is big and action != reshape)
7734 * - update curr_migr_unit
7736 if (a
->curr_action
== reshape
) {
7737 /* still reshaping, maybe update curr_migr_unit */
7738 goto mark_checkpoint
;
7740 if (a
->last_checkpoint
== 0 && a
->prev_action
== reshape
) {
7741 /* for some reason we aborted the reshape.
7743 * disable automatic metadata rollback
7744 * user action is required to recover process
7747 struct imsm_map
*map2
=
7748 get_imsm_map(dev
, MAP_1
);
7749 dev
->vol
.migr_state
= 0;
7750 set_migr_type(dev
, 0);
7751 dev
->vol
.curr_migr_unit
= 0;
7753 sizeof_imsm_map(map2
));
7754 super
->updates_pending
++;
7757 if (a
->last_checkpoint
>= a
->info
.component_size
) {
7758 unsigned long long array_blocks
;
7762 used_disks
= imsm_num_data_members(dev
, MAP_0
);
7763 if (used_disks
> 0) {
7765 blocks_per_member(map
) *
7767 /* round array size down to closest MB
7769 array_blocks
= (array_blocks
7770 >> SECT_PER_MB_SHIFT
)
7771 << SECT_PER_MB_SHIFT
;
7772 a
->info
.custom_array_size
= array_blocks
;
7773 /* encourage manager to update array
7777 a
->check_reshape
= 1;
7779 /* finalize online capacity expansion/reshape */
7780 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
)
7782 mdi
->disk
.raid_disk
,
7785 imsm_progress_container_reshape(super
);
7790 /* before we activate this array handle any missing disks */
7791 if (consistent
== 2)
7792 handle_missing(super
, dev
);
7794 if (consistent
== 2 &&
7795 (!is_resync_complete(&a
->info
) ||
7796 map_state
!= IMSM_T_STATE_NORMAL
||
7797 dev
->vol
.migr_state
))
7800 if (is_resync_complete(&a
->info
)) {
7801 /* complete intialization / resync,
7802 * recovery and interrupted recovery is completed in
7805 if (is_resyncing(dev
)) {
7806 dprintf("imsm: mark resync done\n");
7807 end_migration(dev
, super
, map_state
);
7808 super
->updates_pending
++;
7809 a
->last_checkpoint
= 0;
7811 } else if ((!is_resyncing(dev
) && !failed
) &&
7812 (imsm_reshape_blocks_arrays_changes(super
) == 0)) {
7813 /* mark the start of the init process if nothing is failed */
7814 dprintf("imsm: mark resync start\n");
7815 if (map
->map_state
== IMSM_T_STATE_UNINITIALIZED
)
7816 migrate(dev
, super
, IMSM_T_STATE_NORMAL
, MIGR_INIT
);
7818 migrate(dev
, super
, IMSM_T_STATE_NORMAL
, MIGR_REPAIR
);
7819 super
->updates_pending
++;
7823 /* skip checkpointing for general migration,
7824 * it is controlled in mdadm
7826 if (is_gen_migration(dev
))
7827 goto skip_mark_checkpoint
;
7829 /* check if we can update curr_migr_unit from resync_start, recovery_start */
7830 blocks_per_unit
= blocks_per_migr_unit(super
, dev
);
7831 if (blocks_per_unit
) {
7835 units
= a
->last_checkpoint
/ blocks_per_unit
;
7838 /* check that we did not overflow 32-bits, and that
7839 * curr_migr_unit needs updating
7841 if (units32
== units
&&
7843 __le32_to_cpu(dev
->vol
.curr_migr_unit
) != units32
) {
7844 dprintf("imsm: mark checkpoint (%u)\n", units32
);
7845 dev
->vol
.curr_migr_unit
= __cpu_to_le32(units32
);
7846 super
->updates_pending
++;
7850 skip_mark_checkpoint
:
7851 /* mark dirty / clean */
7852 if (dev
->vol
.dirty
!= !consistent
) {
7853 dprintf("imsm: mark '%s'\n", consistent
? "clean" : "dirty");
7858 super
->updates_pending
++;
7864 static int imsm_disk_slot_to_ord(struct active_array
*a
, int slot
)
7866 int inst
= a
->info
.container_member
;
7867 struct intel_super
*super
= a
->container
->sb
;
7868 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
7869 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7871 if (slot
> map
->num_members
) {
7872 pr_err("imsm: imsm_disk_slot_to_ord %d out of range 0..%d\n",
7873 slot
, map
->num_members
- 1);
7880 return get_imsm_ord_tbl_ent(dev
, slot
, MAP_0
);
7883 static void imsm_set_disk(struct active_array
*a
, int n
, int state
)
7885 int inst
= a
->info
.container_member
;
7886 struct intel_super
*super
= a
->container
->sb
;
7887 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
7888 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7889 struct imsm_disk
*disk
;
7891 int recovery_not_finished
= 0;
7896 ord
= imsm_disk_slot_to_ord(a
, n
);
7900 dprintf("imsm: set_disk %d:%x\n", n
, state
);
7901 disk
= get_imsm_disk(super
, ord_to_idx(ord
));
7903 /* check for new failures */
7904 if (state
& DS_FAULTY
) {
7905 if (mark_failure(super
, dev
, disk
, ord_to_idx(ord
)))
7906 super
->updates_pending
++;
7909 /* check if in_sync */
7910 if (state
& DS_INSYNC
&& ord
& IMSM_ORD_REBUILD
&& is_rebuilding(dev
)) {
7911 struct imsm_map
*migr_map
= get_imsm_map(dev
, MAP_1
);
7913 set_imsm_ord_tbl_ent(migr_map
, n
, ord_to_idx(ord
));
7914 super
->updates_pending
++;
7917 failed
= imsm_count_failed(super
, dev
, MAP_0
);
7918 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
7920 /* check if recovery complete, newly degraded, or failed */
7921 dprintf("imsm: Detected transition to state ");
7922 switch (map_state
) {
7923 case IMSM_T_STATE_NORMAL
: /* transition to normal state */
7924 dprintf("normal: ");
7925 if (is_rebuilding(dev
)) {
7926 dprintf_cont("while rebuilding");
7927 /* check if recovery is really finished */
7928 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
)
7929 if (mdi
->recovery_start
!= MaxSector
) {
7930 recovery_not_finished
= 1;
7933 if (recovery_not_finished
) {
7935 dprintf("Rebuild has not finished yet, state not changed");
7936 if (a
->last_checkpoint
< mdi
->recovery_start
) {
7937 a
->last_checkpoint
= mdi
->recovery_start
;
7938 super
->updates_pending
++;
7942 end_migration(dev
, super
, map_state
);
7943 map
= get_imsm_map(dev
, MAP_0
);
7944 map
->failed_disk_num
= ~0;
7945 super
->updates_pending
++;
7946 a
->last_checkpoint
= 0;
7949 if (is_gen_migration(dev
)) {
7950 dprintf_cont("while general migration");
7951 if (a
->last_checkpoint
>= a
->info
.component_size
)
7952 end_migration(dev
, super
, map_state
);
7954 map
->map_state
= map_state
;
7955 map
= get_imsm_map(dev
, MAP_0
);
7956 map
->failed_disk_num
= ~0;
7957 super
->updates_pending
++;
7961 case IMSM_T_STATE_DEGRADED
: /* transition to degraded state */
7962 dprintf_cont("degraded: ");
7963 if (map
->map_state
!= map_state
&& !dev
->vol
.migr_state
) {
7964 dprintf_cont("mark degraded");
7965 map
->map_state
= map_state
;
7966 super
->updates_pending
++;
7967 a
->last_checkpoint
= 0;
7970 if (is_rebuilding(dev
)) {
7971 dprintf_cont("while rebuilding.");
7972 if (map
->map_state
!= map_state
) {
7973 dprintf_cont(" Map state change");
7974 end_migration(dev
, super
, map_state
);
7975 super
->updates_pending
++;
7979 if (is_gen_migration(dev
)) {
7980 dprintf_cont("while general migration");
7981 if (a
->last_checkpoint
>= a
->info
.component_size
)
7982 end_migration(dev
, super
, map_state
);
7984 map
->map_state
= map_state
;
7985 manage_second_map(super
, dev
);
7987 super
->updates_pending
++;
7990 if (is_initializing(dev
)) {
7991 dprintf_cont("while initialization.");
7992 map
->map_state
= map_state
;
7993 super
->updates_pending
++;
7997 case IMSM_T_STATE_FAILED
: /* transition to failed state */
7998 dprintf_cont("failed: ");
7999 if (is_gen_migration(dev
)) {
8000 dprintf_cont("while general migration");
8001 map
->map_state
= map_state
;
8002 super
->updates_pending
++;
8005 if (map
->map_state
!= map_state
) {
8006 dprintf_cont("mark failed");
8007 end_migration(dev
, super
, map_state
);
8008 super
->updates_pending
++;
8009 a
->last_checkpoint
= 0;
8014 dprintf_cont("state %i\n", map_state
);
8019 static int store_imsm_mpb(int fd
, struct imsm_super
*mpb
)
8022 __u32 mpb_size
= __le32_to_cpu(mpb
->mpb_size
);
8023 unsigned long long dsize
;
8024 unsigned long long sectors
;
8025 unsigned int sector_size
;
8027 get_dev_sector_size(fd
, NULL
, §or_size
);
8028 get_dev_size(fd
, NULL
, &dsize
);
8030 if (mpb_size
> sector_size
) {
8031 /* -1 to account for anchor */
8032 sectors
= mpb_sectors(mpb
, sector_size
) - 1;
8034 /* write the extended mpb to the sectors preceeding the anchor */
8035 if (lseek64(fd
, dsize
- (sector_size
* (2 + sectors
)),
8039 if ((unsigned long long)write(fd
, buf
+ sector_size
,
8040 sector_size
* sectors
) != sector_size
* sectors
)
8044 /* first block is stored on second to last sector of the disk */
8045 if (lseek64(fd
, dsize
- (sector_size
* 2), SEEK_SET
) < 0)
8048 if (write(fd
, buf
, sector_size
) != sector_size
)
8054 static void imsm_sync_metadata(struct supertype
*container
)
8056 struct intel_super
*super
= container
->sb
;
8058 dprintf("sync metadata: %d\n", super
->updates_pending
);
8059 if (!super
->updates_pending
)
8062 write_super_imsm(container
, 0);
8064 super
->updates_pending
= 0;
8067 static struct dl
*imsm_readd(struct intel_super
*super
, int idx
, struct active_array
*a
)
8069 struct imsm_dev
*dev
= get_imsm_dev(super
, a
->info
.container_member
);
8070 int i
= get_imsm_disk_idx(dev
, idx
, MAP_X
);
8073 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
8077 if (dl
&& is_failed(&dl
->disk
))
8081 dprintf("found %x:%x\n", dl
->major
, dl
->minor
);
8086 static struct dl
*imsm_add_spare(struct intel_super
*super
, int slot
,
8087 struct active_array
*a
, int activate_new
,
8088 struct mdinfo
*additional_test_list
)
8090 struct imsm_dev
*dev
= get_imsm_dev(super
, a
->info
.container_member
);
8091 int idx
= get_imsm_disk_idx(dev
, slot
, MAP_X
);
8092 struct imsm_super
*mpb
= super
->anchor
;
8093 struct imsm_map
*map
;
8094 unsigned long long pos
;
8099 __u32 array_start
= 0;
8100 __u32 array_end
= 0;
8102 struct mdinfo
*test_list
;
8104 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
8105 /* If in this array, skip */
8106 for (d
= a
->info
.devs
; d
; d
= d
->next
)
8107 if (d
->state_fd
>= 0 &&
8108 d
->disk
.major
== dl
->major
&&
8109 d
->disk
.minor
== dl
->minor
) {
8110 dprintf("%x:%x already in array\n",
8111 dl
->major
, dl
->minor
);
8116 test_list
= additional_test_list
;
8118 if (test_list
->disk
.major
== dl
->major
&&
8119 test_list
->disk
.minor
== dl
->minor
) {
8120 dprintf("%x:%x already in additional test list\n",
8121 dl
->major
, dl
->minor
);
8124 test_list
= test_list
->next
;
8129 /* skip in use or failed drives */
8130 if (is_failed(&dl
->disk
) || idx
== dl
->index
||
8132 dprintf("%x:%x status (failed: %d index: %d)\n",
8133 dl
->major
, dl
->minor
, is_failed(&dl
->disk
), idx
);
8137 /* skip pure spares when we are looking for partially
8138 * assimilated drives
8140 if (dl
->index
== -1 && !activate_new
)
8143 /* Does this unused device have the requisite free space?
8144 * It needs to be able to cover all member volumes
8146 ex
= get_extents(super
, dl
);
8148 dprintf("cannot get extents\n");
8151 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
8152 dev
= get_imsm_dev(super
, i
);
8153 map
= get_imsm_map(dev
, MAP_0
);
8155 /* check if this disk is already a member of
8158 if (get_imsm_disk_slot(map
, dl
->index
) >= 0)
8164 array_start
= pba_of_lba0(map
);
8165 array_end
= array_start
+
8166 blocks_per_member(map
) - 1;
8169 /* check that we can start at pba_of_lba0 with
8170 * blocks_per_member of space
8172 if (array_start
>= pos
&& array_end
< ex
[j
].start
) {
8176 pos
= ex
[j
].start
+ ex
[j
].size
;
8178 } while (ex
[j
-1].size
);
8185 if (i
< mpb
->num_raid_devs
) {
8186 dprintf("%x:%x does not have %u to %u available\n",
8187 dl
->major
, dl
->minor
, array_start
, array_end
);
8197 static int imsm_rebuild_allowed(struct supertype
*cont
, int dev_idx
, int failed
)
8199 struct imsm_dev
*dev2
;
8200 struct imsm_map
*map
;
8206 dev2
= get_imsm_dev(cont
->sb
, dev_idx
);
8208 state
= imsm_check_degraded(cont
->sb
, dev2
, failed
, MAP_0
);
8209 if (state
== IMSM_T_STATE_FAILED
) {
8210 map
= get_imsm_map(dev2
, MAP_0
);
8213 for (slot
= 0; slot
< map
->num_members
; slot
++) {
8215 * Check if failed disks are deleted from intel
8216 * disk list or are marked to be deleted
8218 idx
= get_imsm_disk_idx(dev2
, slot
, MAP_X
);
8219 idisk
= get_imsm_dl_disk(cont
->sb
, idx
);
8221 * Do not rebuild the array if failed disks
8222 * from failed sub-array are not removed from
8226 is_failed(&idisk
->disk
) &&
8227 (idisk
->action
!= DISK_REMOVE
))
8235 static struct mdinfo
*imsm_activate_spare(struct active_array
*a
,
8236 struct metadata_update
**updates
)
8239 * Find a device with unused free space and use it to replace a
8240 * failed/vacant region in an array. We replace failed regions one a
8241 * array at a time. The result is that a new spare disk will be added
8242 * to the first failed array and after the monitor has finished
8243 * propagating failures the remainder will be consumed.
8245 * FIXME add a capability for mdmon to request spares from another
8249 struct intel_super
*super
= a
->container
->sb
;
8250 int inst
= a
->info
.container_member
;
8251 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
8252 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8253 int failed
= a
->info
.array
.raid_disks
;
8254 struct mdinfo
*rv
= NULL
;
8257 struct metadata_update
*mu
;
8259 struct imsm_update_activate_spare
*u
;
8264 for (d
= a
->info
.devs
; d
; d
= d
->next
) {
8265 if ((d
->curr_state
& DS_FAULTY
) &&
8267 /* wait for Removal to happen */
8269 if (d
->state_fd
>= 0)
8273 dprintf("imsm: activate spare: inst=%d failed=%d (%d) level=%d\n",
8274 inst
, failed
, a
->info
.array
.raid_disks
, a
->info
.array
.level
);
8276 if (imsm_reshape_blocks_arrays_changes(super
))
8279 /* Cannot activate another spare if rebuild is in progress already
8281 if (is_rebuilding(dev
)) {
8282 dprintf("imsm: No spare activation allowed. Rebuild in progress already.\n");
8286 if (a
->info
.array
.level
== 4)
8287 /* No repair for takeovered array
8288 * imsm doesn't support raid4
8292 if (imsm_check_degraded(super
, dev
, failed
, MAP_0
) !=
8293 IMSM_T_STATE_DEGRADED
)
8296 if (get_imsm_map(dev
, MAP_0
)->map_state
== IMSM_T_STATE_UNINITIALIZED
) {
8297 dprintf("imsm: No spare activation allowed. Volume is not initialized.\n");
8302 * If there are any failed disks check state of the other volume.
8303 * Block rebuild if the another one is failed until failed disks
8304 * are removed from container.
8307 dprintf("found failed disks in %.*s, check if there anotherfailed sub-array.\n",
8308 MAX_RAID_SERIAL_LEN
, dev
->volume
);
8309 /* check if states of the other volumes allow for rebuild */
8310 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
8312 allowed
= imsm_rebuild_allowed(a
->container
,
8320 /* For each slot, if it is not working, find a spare */
8321 for (i
= 0; i
< a
->info
.array
.raid_disks
; i
++) {
8322 for (d
= a
->info
.devs
; d
; d
= d
->next
)
8323 if (d
->disk
.raid_disk
== i
)
8325 dprintf("found %d: %p %x\n", i
, d
, d
?d
->curr_state
:0);
8326 if (d
&& (d
->state_fd
>= 0))
8330 * OK, this device needs recovery. Try to re-add the
8331 * previous occupant of this slot, if this fails see if
8332 * we can continue the assimilation of a spare that was
8333 * partially assimilated, finally try to activate a new
8336 dl
= imsm_readd(super
, i
, a
);
8338 dl
= imsm_add_spare(super
, i
, a
, 0, rv
);
8340 dl
= imsm_add_spare(super
, i
, a
, 1, rv
);
8344 /* found a usable disk with enough space */
8345 di
= xcalloc(1, sizeof(*di
));
8347 /* dl->index will be -1 in the case we are activating a
8348 * pristine spare. imsm_process_update() will create a
8349 * new index in this case. Once a disk is found to be
8350 * failed in all member arrays it is kicked from the
8353 di
->disk
.number
= dl
->index
;
8355 /* (ab)use di->devs to store a pointer to the device
8358 di
->devs
= (struct mdinfo
*) dl
;
8360 di
->disk
.raid_disk
= i
;
8361 di
->disk
.major
= dl
->major
;
8362 di
->disk
.minor
= dl
->minor
;
8364 di
->recovery_start
= 0;
8365 di
->data_offset
= pba_of_lba0(map
);
8366 di
->component_size
= a
->info
.component_size
;
8367 di
->container_member
= inst
;
8368 di
->bb
.supported
= 0;
8369 super
->random
= random32();
8373 dprintf("%x:%x to be %d at %llu\n", dl
->major
, dl
->minor
,
8374 i
, di
->data_offset
);
8378 /* No spares found */
8380 /* Now 'rv' has a list of devices to return.
8381 * Create a metadata_update record to update the
8382 * disk_ord_tbl for the array
8384 mu
= xmalloc(sizeof(*mu
));
8385 mu
->buf
= xcalloc(num_spares
,
8386 sizeof(struct imsm_update_activate_spare
));
8388 mu
->space_list
= NULL
;
8389 mu
->len
= sizeof(struct imsm_update_activate_spare
) * num_spares
;
8390 mu
->next
= *updates
;
8391 u
= (struct imsm_update_activate_spare
*) mu
->buf
;
8393 for (di
= rv
; di
; di
= di
->next
) {
8394 u
->type
= update_activate_spare
;
8395 u
->dl
= (struct dl
*) di
->devs
;
8397 u
->slot
= di
->disk
.raid_disk
;
8408 static int disks_overlap(struct intel_super
*super
, int idx
, struct imsm_update_create_array
*u
)
8410 struct imsm_dev
*dev
= get_imsm_dev(super
, idx
);
8411 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8412 struct imsm_map
*new_map
= get_imsm_map(&u
->dev
, MAP_0
);
8413 struct disk_info
*inf
= get_disk_info(u
);
8414 struct imsm_disk
*disk
;
8418 for (i
= 0; i
< map
->num_members
; i
++) {
8419 disk
= get_imsm_disk(super
, get_imsm_disk_idx(dev
, i
, MAP_X
));
8420 for (j
= 0; j
< new_map
->num_members
; j
++)
8421 if (serialcmp(disk
->serial
, inf
[j
].serial
) == 0)
8428 static struct dl
*get_disk_super(struct intel_super
*super
, int major
, int minor
)
8432 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
8433 if (dl
->major
== major
&& dl
->minor
== minor
)
8438 static int remove_disk_super(struct intel_super
*super
, int major
, int minor
)
8444 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
8445 if (dl
->major
== major
&& dl
->minor
== minor
) {
8448 prev
->next
= dl
->next
;
8450 super
->disks
= dl
->next
;
8452 __free_imsm_disk(dl
);
8453 dprintf("removed %x:%x\n", major
, minor
);
8461 static void imsm_delete(struct intel_super
*super
, struct dl
**dlp
, unsigned index
);
8463 static int add_remove_disk_update(struct intel_super
*super
)
8465 int check_degraded
= 0;
8468 /* add/remove some spares to/from the metadata/contrainer */
8469 while (super
->disk_mgmt_list
) {
8470 struct dl
*disk_cfg
;
8472 disk_cfg
= super
->disk_mgmt_list
;
8473 super
->disk_mgmt_list
= disk_cfg
->next
;
8474 disk_cfg
->next
= NULL
;
8476 if (disk_cfg
->action
== DISK_ADD
) {
8477 disk_cfg
->next
= super
->disks
;
8478 super
->disks
= disk_cfg
;
8480 dprintf("added %x:%x\n",
8481 disk_cfg
->major
, disk_cfg
->minor
);
8482 } else if (disk_cfg
->action
== DISK_REMOVE
) {
8483 dprintf("Disk remove action processed: %x.%x\n",
8484 disk_cfg
->major
, disk_cfg
->minor
);
8485 disk
= get_disk_super(super
,
8489 /* store action status */
8490 disk
->action
= DISK_REMOVE
;
8491 /* remove spare disks only */
8492 if (disk
->index
== -1) {
8493 remove_disk_super(super
,
8498 /* release allocate disk structure */
8499 __free_imsm_disk(disk_cfg
);
8502 return check_degraded
;
8505 static int apply_reshape_migration_update(struct imsm_update_reshape_migration
*u
,
8506 struct intel_super
*super
,
8509 struct intel_dev
*id
;
8510 void **tofree
= NULL
;
8513 dprintf("(enter)\n");
8514 if (u
->subdev
< 0 || u
->subdev
> 1) {
8515 dprintf("imsm: Error: Wrong subdev: %i\n", u
->subdev
);
8518 if (space_list
== NULL
|| *space_list
== NULL
) {
8519 dprintf("imsm: Error: Memory is not allocated\n");
8523 for (id
= super
->devlist
; id
; id
= id
->next
) {
8524 if (id
->index
== (unsigned)u
->subdev
) {
8525 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->subdev
);
8526 struct imsm_map
*map
;
8527 struct imsm_dev
*new_dev
=
8528 (struct imsm_dev
*)*space_list
;
8529 struct imsm_map
*migr_map
= get_imsm_map(dev
, MAP_1
);
8531 struct dl
*new_disk
;
8533 if (new_dev
== NULL
)
8535 *space_list
= **space_list
;
8536 memcpy(new_dev
, dev
, sizeof_imsm_dev(dev
, 0));
8537 map
= get_imsm_map(new_dev
, MAP_0
);
8539 dprintf("imsm: Error: migration in progress");
8543 to_state
= map
->map_state
;
8544 if ((u
->new_level
== 5) && (map
->raid_level
== 0)) {
8546 /* this should not happen */
8547 if (u
->new_disks
[0] < 0) {
8548 map
->failed_disk_num
=
8549 map
->num_members
- 1;
8550 to_state
= IMSM_T_STATE_DEGRADED
;
8552 to_state
= IMSM_T_STATE_NORMAL
;
8554 migrate(new_dev
, super
, to_state
, MIGR_GEN_MIGR
);
8555 if (u
->new_level
> -1)
8556 map
->raid_level
= u
->new_level
;
8557 migr_map
= get_imsm_map(new_dev
, MAP_1
);
8558 if ((u
->new_level
== 5) &&
8559 (migr_map
->raid_level
== 0)) {
8560 int ord
= map
->num_members
- 1;
8561 migr_map
->num_members
--;
8562 if (u
->new_disks
[0] < 0)
8563 ord
|= IMSM_ORD_REBUILD
;
8564 set_imsm_ord_tbl_ent(map
,
8565 map
->num_members
- 1,
8569 tofree
= (void **)dev
;
8571 /* update chunk size
8573 if (u
->new_chunksize
> 0) {
8574 unsigned long long num_data_stripes
;
8576 imsm_num_data_members(dev
, MAP_0
);
8578 if (used_disks
== 0)
8581 map
->blocks_per_strip
=
8582 __cpu_to_le16(u
->new_chunksize
* 2);
8584 (join_u32(dev
->size_low
, dev
->size_high
)
8586 num_data_stripes
/= map
->blocks_per_strip
;
8587 num_data_stripes
/= map
->num_domains
;
8588 set_num_data_stripes(map
, num_data_stripes
);
8593 if (u
->new_level
!= 5 || migr_map
->raid_level
!= 0 ||
8594 migr_map
->raid_level
== map
->raid_level
)
8597 if (u
->new_disks
[0] >= 0) {
8600 new_disk
= get_disk_super(super
,
8601 major(u
->new_disks
[0]),
8602 minor(u
->new_disks
[0]));
8603 dprintf("imsm: new disk for reshape is: %i:%i (%p, index = %i)\n",
8604 major(u
->new_disks
[0]),
8605 minor(u
->new_disks
[0]),
8606 new_disk
, new_disk
->index
);
8607 if (new_disk
== NULL
)
8608 goto error_disk_add
;
8610 new_disk
->index
= map
->num_members
- 1;
8611 /* slot to fill in autolayout
8613 new_disk
->raiddisk
= new_disk
->index
;
8614 new_disk
->disk
.status
|= CONFIGURED_DISK
;
8615 new_disk
->disk
.status
&= ~SPARE_DISK
;
8617 goto error_disk_add
;
8620 *tofree
= *space_list
;
8621 /* calculate new size
8623 imsm_set_array_size(new_dev
, -1);
8630 *space_list
= tofree
;
8634 dprintf("Error: imsm: Cannot find disk.\n");
8638 static int apply_size_change_update(struct imsm_update_size_change
*u
,
8639 struct intel_super
*super
)
8641 struct intel_dev
*id
;
8644 dprintf("(enter)\n");
8645 if (u
->subdev
< 0 || u
->subdev
> 1) {
8646 dprintf("imsm: Error: Wrong subdev: %i\n", u
->subdev
);
8650 for (id
= super
->devlist
; id
; id
= id
->next
) {
8651 if (id
->index
== (unsigned)u
->subdev
) {
8652 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->subdev
);
8653 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8654 int used_disks
= imsm_num_data_members(dev
, MAP_0
);
8655 unsigned long long blocks_per_member
;
8656 unsigned long long num_data_stripes
;
8658 /* calculate new size
8660 blocks_per_member
= u
->new_size
/ used_disks
;
8661 num_data_stripes
= blocks_per_member
/
8662 map
->blocks_per_strip
;
8663 num_data_stripes
/= map
->num_domains
;
8664 dprintf("(size: %llu, blocks per member: %llu, num_data_stipes: %llu)\n",
8665 u
->new_size
, blocks_per_member
,
8667 set_blocks_per_member(map
, blocks_per_member
);
8668 set_num_data_stripes(map
, num_data_stripes
);
8669 imsm_set_array_size(dev
, u
->new_size
);
8679 static int apply_update_activate_spare(struct imsm_update_activate_spare
*u
,
8680 struct intel_super
*super
,
8681 struct active_array
*active_array
)
8683 struct imsm_super
*mpb
= super
->anchor
;
8684 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->array
);
8685 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8686 struct imsm_map
*migr_map
;
8687 struct active_array
*a
;
8688 struct imsm_disk
*disk
;
8695 int second_map_created
= 0;
8697 for (; u
; u
= u
->next
) {
8698 victim
= get_imsm_disk_idx(dev
, u
->slot
, MAP_X
);
8703 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
8708 pr_err("error: imsm_activate_spare passed an unknown disk (index: %d)\n",
8713 /* count failures (excluding rebuilds and the victim)
8714 * to determine map[0] state
8717 for (i
= 0; i
< map
->num_members
; i
++) {
8720 disk
= get_imsm_disk(super
,
8721 get_imsm_disk_idx(dev
, i
, MAP_X
));
8722 if (!disk
|| is_failed(disk
))
8726 /* adding a pristine spare, assign a new index */
8727 if (dl
->index
< 0) {
8728 dl
->index
= super
->anchor
->num_disks
;
8729 super
->anchor
->num_disks
++;
8732 disk
->status
|= CONFIGURED_DISK
;
8733 disk
->status
&= ~SPARE_DISK
;
8736 to_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
8737 if (!second_map_created
) {
8738 second_map_created
= 1;
8739 map
->map_state
= IMSM_T_STATE_DEGRADED
;
8740 migrate(dev
, super
, to_state
, MIGR_REBUILD
);
8742 map
->map_state
= to_state
;
8743 migr_map
= get_imsm_map(dev
, MAP_1
);
8744 set_imsm_ord_tbl_ent(map
, u
->slot
, dl
->index
);
8745 set_imsm_ord_tbl_ent(migr_map
, u
->slot
,
8746 dl
->index
| IMSM_ORD_REBUILD
);
8748 /* update the family_num to mark a new container
8749 * generation, being careful to record the existing
8750 * family_num in orig_family_num to clean up after
8751 * earlier mdadm versions that neglected to set it.
8753 if (mpb
->orig_family_num
== 0)
8754 mpb
->orig_family_num
= mpb
->family_num
;
8755 mpb
->family_num
+= super
->random
;
8757 /* count arrays using the victim in the metadata */
8759 for (a
= active_array
; a
; a
= a
->next
) {
8760 dev
= get_imsm_dev(super
, a
->info
.container_member
);
8761 map
= get_imsm_map(dev
, MAP_0
);
8763 if (get_imsm_disk_slot(map
, victim
) >= 0)
8767 /* delete the victim if it is no longer being
8773 /* We know that 'manager' isn't touching anything,
8774 * so it is safe to delete
8776 for (dlp
= &super
->disks
; *dlp
; dlp
= &(*dlp
)->next
)
8777 if ((*dlp
)->index
== victim
)
8780 /* victim may be on the missing list */
8782 for (dlp
= &super
->missing
; *dlp
;
8783 dlp
= &(*dlp
)->next
)
8784 if ((*dlp
)->index
== victim
)
8786 imsm_delete(super
, dlp
, victim
);
8793 static int apply_reshape_container_disks_update(struct imsm_update_reshape
*u
,
8794 struct intel_super
*super
,
8797 struct dl
*new_disk
;
8798 struct intel_dev
*id
;
8800 int delta_disks
= u
->new_raid_disks
- u
->old_raid_disks
;
8801 int disk_count
= u
->old_raid_disks
;
8802 void **tofree
= NULL
;
8803 int devices_to_reshape
= 1;
8804 struct imsm_super
*mpb
= super
->anchor
;
8806 unsigned int dev_id
;
8808 dprintf("(enter)\n");
8810 /* enable spares to use in array */
8811 for (i
= 0; i
< delta_disks
; i
++) {
8812 new_disk
= get_disk_super(super
,
8813 major(u
->new_disks
[i
]),
8814 minor(u
->new_disks
[i
]));
8815 dprintf("imsm: new disk for reshape is: %i:%i (%p, index = %i)\n",
8816 major(u
->new_disks
[i
]), minor(u
->new_disks
[i
]),
8817 new_disk
, new_disk
->index
);
8818 if (new_disk
== NULL
||
8819 (new_disk
->index
>= 0 &&
8820 new_disk
->index
< u
->old_raid_disks
))
8821 goto update_reshape_exit
;
8822 new_disk
->index
= disk_count
++;
8823 /* slot to fill in autolayout
8825 new_disk
->raiddisk
= new_disk
->index
;
8826 new_disk
->disk
.status
|=
8828 new_disk
->disk
.status
&= ~SPARE_DISK
;
8831 dprintf("imsm: volume set mpb->num_raid_devs = %i\n",
8832 mpb
->num_raid_devs
);
8833 /* manage changes in volume
8835 for (dev_id
= 0; dev_id
< mpb
->num_raid_devs
; dev_id
++) {
8836 void **sp
= *space_list
;
8837 struct imsm_dev
*newdev
;
8838 struct imsm_map
*newmap
, *oldmap
;
8840 for (id
= super
->devlist
; id
; id
= id
->next
) {
8841 if (id
->index
== dev_id
)
8850 /* Copy the dev, but not (all of) the map */
8851 memcpy(newdev
, id
->dev
, sizeof(*newdev
));
8852 oldmap
= get_imsm_map(id
->dev
, MAP_0
);
8853 newmap
= get_imsm_map(newdev
, MAP_0
);
8854 /* Copy the current map */
8855 memcpy(newmap
, oldmap
, sizeof_imsm_map(oldmap
));
8856 /* update one device only
8858 if (devices_to_reshape
) {
8859 dprintf("imsm: modifying subdev: %i\n",
8861 devices_to_reshape
--;
8862 newdev
->vol
.migr_state
= 1;
8863 newdev
->vol
.curr_migr_unit
= 0;
8864 set_migr_type(newdev
, MIGR_GEN_MIGR
);
8865 newmap
->num_members
= u
->new_raid_disks
;
8866 for (i
= 0; i
< delta_disks
; i
++) {
8867 set_imsm_ord_tbl_ent(newmap
,
8868 u
->old_raid_disks
+ i
,
8869 u
->old_raid_disks
+ i
);
8871 /* New map is correct, now need to save old map
8873 newmap
= get_imsm_map(newdev
, MAP_1
);
8874 memcpy(newmap
, oldmap
, sizeof_imsm_map(oldmap
));
8876 imsm_set_array_size(newdev
, -1);
8879 sp
= (void **)id
->dev
;
8884 /* Clear migration record */
8885 memset(super
->migr_rec
, 0, sizeof(struct migr_record
));
8888 *space_list
= tofree
;
8891 update_reshape_exit
:
8896 static int apply_takeover_update(struct imsm_update_takeover
*u
,
8897 struct intel_super
*super
,
8900 struct imsm_dev
*dev
= NULL
;
8901 struct intel_dev
*dv
;
8902 struct imsm_dev
*dev_new
;
8903 struct imsm_map
*map
;
8907 for (dv
= super
->devlist
; dv
; dv
= dv
->next
)
8908 if (dv
->index
== (unsigned int)u
->subarray
) {
8916 map
= get_imsm_map(dev
, MAP_0
);
8918 if (u
->direction
== R10_TO_R0
) {
8919 unsigned long long num_data_stripes
;
8921 map
->num_domains
= 1;
8922 num_data_stripes
= blocks_per_member(map
);
8923 num_data_stripes
/= map
->blocks_per_strip
;
8924 num_data_stripes
/= map
->num_domains
;
8925 set_num_data_stripes(map
, num_data_stripes
);
8927 /* Number of failed disks must be half of initial disk number */
8928 if (imsm_count_failed(super
, dev
, MAP_0
) !=
8929 (map
->num_members
/ 2))
8932 /* iterate through devices to mark removed disks as spare */
8933 for (dm
= super
->disks
; dm
; dm
= dm
->next
) {
8934 if (dm
->disk
.status
& FAILED_DISK
) {
8935 int idx
= dm
->index
;
8936 /* update indexes on the disk list */
8937 /* FIXME this loop-with-the-loop looks wrong, I'm not convinced
8938 the index values will end up being correct.... NB */
8939 for (du
= super
->disks
; du
; du
= du
->next
)
8940 if (du
->index
> idx
)
8942 /* mark as spare disk */
8947 map
->num_members
= map
->num_members
/ 2;
8948 map
->map_state
= IMSM_T_STATE_NORMAL
;
8949 map
->num_domains
= 1;
8950 map
->raid_level
= 0;
8951 map
->failed_disk_num
= -1;
8954 if (u
->direction
== R0_TO_R10
) {
8956 /* update slots in current disk list */
8957 for (dm
= super
->disks
; dm
; dm
= dm
->next
) {
8961 /* create new *missing* disks */
8962 for (i
= 0; i
< map
->num_members
; i
++) {
8963 space
= *space_list
;
8966 *space_list
= *space
;
8968 memcpy(du
, super
->disks
, sizeof(*du
));
8972 du
->index
= (i
* 2) + 1;
8973 sprintf((char *)du
->disk
.serial
,
8974 " MISSING_%d", du
->index
);
8975 sprintf((char *)du
->serial
,
8976 "MISSING_%d", du
->index
);
8977 du
->next
= super
->missing
;
8978 super
->missing
= du
;
8980 /* create new dev and map */
8981 space
= *space_list
;
8984 *space_list
= *space
;
8985 dev_new
= (void *)space
;
8986 memcpy(dev_new
, dev
, sizeof(*dev
));
8987 /* update new map */
8988 map
= get_imsm_map(dev_new
, MAP_0
);
8989 map
->num_members
= map
->num_members
* 2;
8990 map
->map_state
= IMSM_T_STATE_DEGRADED
;
8991 map
->num_domains
= 2;
8992 map
->raid_level
= 1;
8993 /* replace dev<->dev_new */
8996 /* update disk order table */
8997 for (du
= super
->disks
; du
; du
= du
->next
)
8999 set_imsm_ord_tbl_ent(map
, du
->index
, du
->index
);
9000 for (du
= super
->missing
; du
; du
= du
->next
)
9001 if (du
->index
>= 0) {
9002 set_imsm_ord_tbl_ent(map
, du
->index
, du
->index
);
9003 mark_missing(super
, dv
->dev
, &du
->disk
, du
->index
);
9009 static void imsm_process_update(struct supertype
*st
,
9010 struct metadata_update
*update
)
9013 * crack open the metadata_update envelope to find the update record
9014 * update can be one of:
9015 * update_reshape_container_disks - all the arrays in the container
9016 * are being reshaped to have more devices. We need to mark
9017 * the arrays for general migration and convert selected spares
9018 * into active devices.
9019 * update_activate_spare - a spare device has replaced a failed
9020 * device in an array, update the disk_ord_tbl. If this disk is
9021 * present in all member arrays then also clear the SPARE_DISK
9023 * update_create_array
9025 * update_rename_array
9026 * update_add_remove_disk
9028 struct intel_super
*super
= st
->sb
;
9029 struct imsm_super
*mpb
;
9030 enum imsm_update_type type
= *(enum imsm_update_type
*) update
->buf
;
9032 /* update requires a larger buf but the allocation failed */
9033 if (super
->next_len
&& !super
->next_buf
) {
9034 super
->next_len
= 0;
9038 if (super
->next_buf
) {
9039 memcpy(super
->next_buf
, super
->buf
, super
->len
);
9041 super
->len
= super
->next_len
;
9042 super
->buf
= super
->next_buf
;
9044 super
->next_len
= 0;
9045 super
->next_buf
= NULL
;
9048 mpb
= super
->anchor
;
9051 case update_general_migration_checkpoint
: {
9052 struct intel_dev
*id
;
9053 struct imsm_update_general_migration_checkpoint
*u
=
9054 (void *)update
->buf
;
9056 dprintf("called for update_general_migration_checkpoint\n");
9058 /* find device under general migration */
9059 for (id
= super
->devlist
; id
; id
= id
->next
) {
9060 if (is_gen_migration(id
->dev
)) {
9061 id
->dev
->vol
.curr_migr_unit
=
9062 __cpu_to_le32(u
->curr_migr_unit
);
9063 super
->updates_pending
++;
9068 case update_takeover
: {
9069 struct imsm_update_takeover
*u
= (void *)update
->buf
;
9070 if (apply_takeover_update(u
, super
, &update
->space_list
)) {
9071 imsm_update_version_info(super
);
9072 super
->updates_pending
++;
9077 case update_reshape_container_disks
: {
9078 struct imsm_update_reshape
*u
= (void *)update
->buf
;
9079 if (apply_reshape_container_disks_update(
9080 u
, super
, &update
->space_list
))
9081 super
->updates_pending
++;
9084 case update_reshape_migration
: {
9085 struct imsm_update_reshape_migration
*u
= (void *)update
->buf
;
9086 if (apply_reshape_migration_update(
9087 u
, super
, &update
->space_list
))
9088 super
->updates_pending
++;
9091 case update_size_change
: {
9092 struct imsm_update_size_change
*u
= (void *)update
->buf
;
9093 if (apply_size_change_update(u
, super
))
9094 super
->updates_pending
++;
9097 case update_activate_spare
: {
9098 struct imsm_update_activate_spare
*u
= (void *) update
->buf
;
9099 if (apply_update_activate_spare(u
, super
, st
->arrays
))
9100 super
->updates_pending
++;
9103 case update_create_array
: {
9104 /* someone wants to create a new array, we need to be aware of
9105 * a few races/collisions:
9106 * 1/ 'Create' called by two separate instances of mdadm
9107 * 2/ 'Create' versus 'activate_spare': mdadm has chosen
9108 * devices that have since been assimilated via
9110 * In the event this update can not be carried out mdadm will
9111 * (FIX ME) notice that its update did not take hold.
9113 struct imsm_update_create_array
*u
= (void *) update
->buf
;
9114 struct intel_dev
*dv
;
9115 struct imsm_dev
*dev
;
9116 struct imsm_map
*map
, *new_map
;
9117 unsigned long long start
, end
;
9118 unsigned long long new_start
, new_end
;
9120 struct disk_info
*inf
;
9123 /* handle racing creates: first come first serve */
9124 if (u
->dev_idx
< mpb
->num_raid_devs
) {
9125 dprintf("subarray %d already defined\n", u
->dev_idx
);
9129 /* check update is next in sequence */
9130 if (u
->dev_idx
!= mpb
->num_raid_devs
) {
9131 dprintf("can not create array %d expected index %d\n",
9132 u
->dev_idx
, mpb
->num_raid_devs
);
9136 new_map
= get_imsm_map(&u
->dev
, MAP_0
);
9137 new_start
= pba_of_lba0(new_map
);
9138 new_end
= new_start
+ blocks_per_member(new_map
);
9139 inf
= get_disk_info(u
);
9141 /* handle activate_spare versus create race:
9142 * check to make sure that overlapping arrays do not include
9145 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
9146 dev
= get_imsm_dev(super
, i
);
9147 map
= get_imsm_map(dev
, MAP_0
);
9148 start
= pba_of_lba0(map
);
9149 end
= start
+ blocks_per_member(map
);
9150 if ((new_start
>= start
&& new_start
<= end
) ||
9151 (start
>= new_start
&& start
<= new_end
))
9156 if (disks_overlap(super
, i
, u
)) {
9157 dprintf("arrays overlap\n");
9162 /* check that prepare update was successful */
9163 if (!update
->space
) {
9164 dprintf("prepare update failed\n");
9168 /* check that all disks are still active before committing
9169 * changes. FIXME: could we instead handle this by creating a
9170 * degraded array? That's probably not what the user expects,
9171 * so better to drop this update on the floor.
9173 for (i
= 0; i
< new_map
->num_members
; i
++) {
9174 dl
= serial_to_dl(inf
[i
].serial
, super
);
9176 dprintf("disk disappeared\n");
9181 super
->updates_pending
++;
9183 /* convert spares to members and fixup ord_tbl */
9184 for (i
= 0; i
< new_map
->num_members
; i
++) {
9185 dl
= serial_to_dl(inf
[i
].serial
, super
);
9186 if (dl
->index
== -1) {
9187 dl
->index
= mpb
->num_disks
;
9189 dl
->disk
.status
|= CONFIGURED_DISK
;
9190 dl
->disk
.status
&= ~SPARE_DISK
;
9192 set_imsm_ord_tbl_ent(new_map
, i
, dl
->index
);
9197 update
->space
= NULL
;
9198 imsm_copy_dev(dev
, &u
->dev
);
9199 dv
->index
= u
->dev_idx
;
9200 dv
->next
= super
->devlist
;
9201 super
->devlist
= dv
;
9202 mpb
->num_raid_devs
++;
9204 imsm_update_version_info(super
);
9207 /* mdmon knows how to release update->space, but not
9208 * ((struct intel_dev *) update->space)->dev
9210 if (update
->space
) {
9216 case update_kill_array
: {
9217 struct imsm_update_kill_array
*u
= (void *) update
->buf
;
9218 int victim
= u
->dev_idx
;
9219 struct active_array
*a
;
9220 struct intel_dev
**dp
;
9221 struct imsm_dev
*dev
;
9223 /* sanity check that we are not affecting the uuid of
9224 * active arrays, or deleting an active array
9226 * FIXME when immutable ids are available, but note that
9227 * we'll also need to fixup the invalidated/active
9228 * subarray indexes in mdstat
9230 for (a
= st
->arrays
; a
; a
= a
->next
)
9231 if (a
->info
.container_member
>= victim
)
9233 /* by definition if mdmon is running at least one array
9234 * is active in the container, so checking
9235 * mpb->num_raid_devs is just extra paranoia
9237 dev
= get_imsm_dev(super
, victim
);
9238 if (a
|| !dev
|| mpb
->num_raid_devs
== 1) {
9239 dprintf("failed to delete subarray-%d\n", victim
);
9243 for (dp
= &super
->devlist
; *dp
;)
9244 if ((*dp
)->index
== (unsigned)super
->current_vol
) {
9247 if ((*dp
)->index
> (unsigned)victim
)
9251 mpb
->num_raid_devs
--;
9252 super
->updates_pending
++;
9255 case update_rename_array
: {
9256 struct imsm_update_rename_array
*u
= (void *) update
->buf
;
9257 char name
[MAX_RAID_SERIAL_LEN
+1];
9258 int target
= u
->dev_idx
;
9259 struct active_array
*a
;
9260 struct imsm_dev
*dev
;
9262 /* sanity check that we are not affecting the uuid of
9265 snprintf(name
, MAX_RAID_SERIAL_LEN
, "%s", (char *) u
->name
);
9266 name
[MAX_RAID_SERIAL_LEN
] = '\0';
9267 for (a
= st
->arrays
; a
; a
= a
->next
)
9268 if (a
->info
.container_member
== target
)
9270 dev
= get_imsm_dev(super
, u
->dev_idx
);
9271 if (a
|| !dev
|| !check_name(super
, name
, 1)) {
9272 dprintf("failed to rename subarray-%d\n", target
);
9276 snprintf((char *) dev
->volume
, MAX_RAID_SERIAL_LEN
, "%s", name
);
9277 super
->updates_pending
++;
9280 case update_add_remove_disk
: {
9281 /* we may be able to repair some arrays if disks are
9282 * being added, check the status of add_remove_disk
9283 * if discs has been added.
9285 if (add_remove_disk_update(super
)) {
9286 struct active_array
*a
;
9288 super
->updates_pending
++;
9289 for (a
= st
->arrays
; a
; a
= a
->next
)
9290 a
->check_degraded
= 1;
9294 case update_prealloc_badblocks_mem
:
9297 pr_err("error: unsuported process update type:(type: %d)\n", type
);
9301 static struct mdinfo
*get_spares_for_grow(struct supertype
*st
);
9303 static int imsm_prepare_update(struct supertype
*st
,
9304 struct metadata_update
*update
)
9307 * Allocate space to hold new disk entries, raid-device entries or a new
9308 * mpb if necessary. The manager synchronously waits for updates to
9309 * complete in the monitor, so new mpb buffers allocated here can be
9310 * integrated by the monitor thread without worrying about live pointers
9311 * in the manager thread.
9313 enum imsm_update_type type
;
9314 struct intel_super
*super
= st
->sb
;
9315 unsigned int sector_size
= super
->sector_size
;
9316 struct imsm_super
*mpb
= super
->anchor
;
9320 if (update
->len
< (int)sizeof(type
))
9323 type
= *(enum imsm_update_type
*) update
->buf
;
9326 case update_general_migration_checkpoint
:
9327 if (update
->len
< (int)sizeof(struct imsm_update_general_migration_checkpoint
))
9329 dprintf("called for update_general_migration_checkpoint\n");
9331 case update_takeover
: {
9332 struct imsm_update_takeover
*u
= (void *)update
->buf
;
9333 if (update
->len
< (int)sizeof(*u
))
9335 if (u
->direction
== R0_TO_R10
) {
9336 void **tail
= (void **)&update
->space_list
;
9337 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->subarray
);
9338 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
9339 int num_members
= map
->num_members
;
9342 /* allocate memory for added disks */
9343 for (i
= 0; i
< num_members
; i
++) {
9344 size
= sizeof(struct dl
);
9345 space
= xmalloc(size
);
9350 /* allocate memory for new device */
9351 size
= sizeof_imsm_dev(super
->devlist
->dev
, 0) +
9352 (num_members
* sizeof(__u32
));
9353 space
= xmalloc(size
);
9357 len
= disks_to_mpb_size(num_members
* 2);
9362 case update_reshape_container_disks
: {
9363 /* Every raid device in the container is about to
9364 * gain some more devices, and we will enter a
9366 * So each 'imsm_map' will be bigger, and the imsm_vol
9367 * will now hold 2 of them.
9368 * Thus we need new 'struct imsm_dev' allocations sized
9369 * as sizeof_imsm_dev but with more devices in both maps.
9371 struct imsm_update_reshape
*u
= (void *)update
->buf
;
9372 struct intel_dev
*dl
;
9373 void **space_tail
= (void**)&update
->space_list
;
9375 if (update
->len
< (int)sizeof(*u
))
9378 dprintf("for update_reshape\n");
9380 for (dl
= super
->devlist
; dl
; dl
= dl
->next
) {
9381 int size
= sizeof_imsm_dev(dl
->dev
, 1);
9383 if (u
->new_raid_disks
> u
->old_raid_disks
)
9384 size
+= sizeof(__u32
)*2*
9385 (u
->new_raid_disks
- u
->old_raid_disks
);
9392 len
= disks_to_mpb_size(u
->new_raid_disks
);
9393 dprintf("New anchor length is %llu\n", (unsigned long long)len
);
9396 case update_reshape_migration
: {
9397 /* for migration level 0->5 we need to add disks
9398 * so the same as for container operation we will copy
9399 * device to the bigger location.
9400 * in memory prepared device and new disk area are prepared
9401 * for usage in process update
9403 struct imsm_update_reshape_migration
*u
= (void *)update
->buf
;
9404 struct intel_dev
*id
;
9405 void **space_tail
= (void **)&update
->space_list
;
9408 int current_level
= -1;
9410 if (update
->len
< (int)sizeof(*u
))
9413 dprintf("for update_reshape\n");
9415 /* add space for bigger array in update
9417 for (id
= super
->devlist
; id
; id
= id
->next
) {
9418 if (id
->index
== (unsigned)u
->subdev
) {
9419 size
= sizeof_imsm_dev(id
->dev
, 1);
9420 if (u
->new_raid_disks
> u
->old_raid_disks
)
9421 size
+= sizeof(__u32
)*2*
9422 (u
->new_raid_disks
- u
->old_raid_disks
);
9430 if (update
->space_list
== NULL
)
9433 /* add space for disk in update
9435 size
= sizeof(struct dl
);
9441 /* add spare device to update
9443 for (id
= super
->devlist
; id
; id
= id
->next
)
9444 if (id
->index
== (unsigned)u
->subdev
) {
9445 struct imsm_dev
*dev
;
9446 struct imsm_map
*map
;
9448 dev
= get_imsm_dev(super
, u
->subdev
);
9449 map
= get_imsm_map(dev
, MAP_0
);
9450 current_level
= map
->raid_level
;
9453 if (u
->new_level
== 5 && u
->new_level
!= current_level
) {
9454 struct mdinfo
*spares
;
9456 spares
= get_spares_for_grow(st
);
9464 makedev(dev
->disk
.major
,
9466 dl
= get_disk_super(super
,
9469 dl
->index
= u
->old_raid_disks
;
9475 len
= disks_to_mpb_size(u
->new_raid_disks
);
9476 dprintf("New anchor length is %llu\n", (unsigned long long)len
);
9479 case update_size_change
: {
9480 if (update
->len
< (int)sizeof(struct imsm_update_size_change
))
9484 case update_activate_spare
: {
9485 if (update
->len
< (int)sizeof(struct imsm_update_activate_spare
))
9489 case update_create_array
: {
9490 struct imsm_update_create_array
*u
= (void *) update
->buf
;
9491 struct intel_dev
*dv
;
9492 struct imsm_dev
*dev
= &u
->dev
;
9493 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
9495 struct disk_info
*inf
;
9499 if (update
->len
< (int)sizeof(*u
))
9502 inf
= get_disk_info(u
);
9503 len
= sizeof_imsm_dev(dev
, 1);
9504 /* allocate a new super->devlist entry */
9505 dv
= xmalloc(sizeof(*dv
));
9506 dv
->dev
= xmalloc(len
);
9509 /* count how many spares will be converted to members */
9510 for (i
= 0; i
< map
->num_members
; i
++) {
9511 dl
= serial_to_dl(inf
[i
].serial
, super
);
9513 /* hmm maybe it failed?, nothing we can do about
9518 if (count_memberships(dl
, super
) == 0)
9521 len
+= activate
* sizeof(struct imsm_disk
);
9524 case update_kill_array
: {
9525 if (update
->len
< (int)sizeof(struct imsm_update_kill_array
))
9529 case update_rename_array
: {
9530 if (update
->len
< (int)sizeof(struct imsm_update_rename_array
))
9534 case update_add_remove_disk
:
9535 /* no update->len needed */
9537 case update_prealloc_badblocks_mem
:
9538 super
->extra_space
+= sizeof(struct bbm_log
) -
9539 get_imsm_bbm_log_size(super
->bbm_log
);
9545 /* check if we need a larger metadata buffer */
9546 if (super
->next_buf
)
9547 buf_len
= super
->next_len
;
9549 buf_len
= super
->len
;
9551 if (__le32_to_cpu(mpb
->mpb_size
) + super
->extra_space
+ len
> buf_len
) {
9552 /* ok we need a larger buf than what is currently allocated
9553 * if this allocation fails process_update will notice that
9554 * ->next_len is set and ->next_buf is NULL
9556 buf_len
= ROUND_UP(__le32_to_cpu(mpb
->mpb_size
) +
9557 super
->extra_space
+ len
, sector_size
);
9558 if (super
->next_buf
)
9559 free(super
->next_buf
);
9561 super
->next_len
= buf_len
;
9562 if (posix_memalign(&super
->next_buf
, sector_size
, buf_len
) == 0)
9563 memset(super
->next_buf
, 0, buf_len
);
9565 super
->next_buf
= NULL
;
9570 /* must be called while manager is quiesced */
9571 static void imsm_delete(struct intel_super
*super
, struct dl
**dlp
, unsigned index
)
9573 struct imsm_super
*mpb
= super
->anchor
;
9575 struct imsm_dev
*dev
;
9576 struct imsm_map
*map
;
9577 unsigned int i
, j
, num_members
;
9579 struct bbm_log
*log
= super
->bbm_log
;
9581 dprintf("deleting device[%d] from imsm_super\n", index
);
9583 /* shift all indexes down one */
9584 for (iter
= super
->disks
; iter
; iter
= iter
->next
)
9585 if (iter
->index
> (int)index
)
9587 for (iter
= super
->missing
; iter
; iter
= iter
->next
)
9588 if (iter
->index
> (int)index
)
9591 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
9592 dev
= get_imsm_dev(super
, i
);
9593 map
= get_imsm_map(dev
, MAP_0
);
9594 num_members
= map
->num_members
;
9595 for (j
= 0; j
< num_members
; j
++) {
9596 /* update ord entries being careful not to propagate
9597 * ord-flags to the first map
9599 ord
= get_imsm_ord_tbl_ent(dev
, j
, MAP_X
);
9601 if (ord_to_idx(ord
) <= index
)
9604 map
= get_imsm_map(dev
, MAP_0
);
9605 set_imsm_ord_tbl_ent(map
, j
, ord_to_idx(ord
- 1));
9606 map
= get_imsm_map(dev
, MAP_1
);
9608 set_imsm_ord_tbl_ent(map
, j
, ord
- 1);
9612 for (i
= 0; i
< log
->entry_count
; i
++) {
9613 struct bbm_log_entry
*entry
= &log
->marked_block_entries
[i
];
9615 if (entry
->disk_ordinal
<= index
)
9617 entry
->disk_ordinal
--;
9621 super
->updates_pending
++;
9623 struct dl
*dl
= *dlp
;
9625 *dlp
= (*dlp
)->next
;
9626 __free_imsm_disk(dl
);
9629 #endif /* MDASSEMBLE */
9631 static void close_targets(int *targets
, int new_disks
)
9638 for (i
= 0; i
< new_disks
; i
++) {
9639 if (targets
[i
] >= 0) {
9646 static int imsm_get_allowed_degradation(int level
, int raid_disks
,
9647 struct intel_super
*super
,
9648 struct imsm_dev
*dev
)
9654 struct imsm_map
*map
;
9657 ret_val
= raid_disks
/2;
9658 /* check map if all disks pairs not failed
9661 map
= get_imsm_map(dev
, MAP_0
);
9662 for (i
= 0; i
< ret_val
; i
++) {
9663 int degradation
= 0;
9664 if (get_imsm_disk(super
, i
) == NULL
)
9666 if (get_imsm_disk(super
, i
+ 1) == NULL
)
9668 if (degradation
== 2)
9671 map
= get_imsm_map(dev
, MAP_1
);
9672 /* if there is no second map
9673 * result can be returned
9677 /* check degradation in second map
9679 for (i
= 0; i
< ret_val
; i
++) {
9680 int degradation
= 0;
9681 if (get_imsm_disk(super
, i
) == NULL
)
9683 if (get_imsm_disk(super
, i
+ 1) == NULL
)
9685 if (degradation
== 2)
9699 /*******************************************************************************
9700 * Function: open_backup_targets
9701 * Description: Function opens file descriptors for all devices given in
9704 * info : general array info
9705 * raid_disks : number of disks
9706 * raid_fds : table of device's file descriptors
9707 * super : intel super for raid10 degradation check
9708 * dev : intel device for raid10 degradation check
9712 ******************************************************************************/
9713 int open_backup_targets(struct mdinfo
*info
, int raid_disks
, int *raid_fds
,
9714 struct intel_super
*super
, struct imsm_dev
*dev
)
9720 for (i
= 0; i
< raid_disks
; i
++)
9723 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
9726 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
)) {
9727 dprintf("disk is faulty!!\n");
9731 if (sd
->disk
.raid_disk
>= raid_disks
|| sd
->disk
.raid_disk
< 0)
9734 dn
= map_dev(sd
->disk
.major
,
9736 raid_fds
[sd
->disk
.raid_disk
] = dev_open(dn
, O_RDWR
);
9737 if (raid_fds
[sd
->disk
.raid_disk
] < 0) {
9738 pr_err("cannot open component\n");
9743 /* check if maximum array degradation level is not exceeded
9745 if ((raid_disks
- opened
) >
9746 imsm_get_allowed_degradation(info
->new_level
, raid_disks
,
9748 pr_err("Not enough disks can be opened.\n");
9749 close_targets(raid_fds
, raid_disks
);
9755 /*******************************************************************************
9756 * Function: validate_container_imsm
9757 * Description: This routine validates container after assemble,
9758 * eg. if devices in container are under the same controller.
9761 * info : linked list with info about devices used in array
9765 ******************************************************************************/
9766 int validate_container_imsm(struct mdinfo
*info
)
9768 if (check_env("IMSM_NO_PLATFORM"))
9771 struct sys_dev
*idev
;
9772 struct sys_dev
*hba
= NULL
;
9773 struct sys_dev
*intel_devices
= find_intel_devices();
9774 char *dev_path
= devt_to_devpath(makedev(info
->disk
.major
,
9777 for (idev
= intel_devices
; idev
; idev
= idev
->next
) {
9778 if (dev_path
&& strstr(dev_path
, idev
->path
)) {
9787 pr_err("WARNING - Cannot detect HBA for device %s!\n",
9788 devid2kname(makedev(info
->disk
.major
, info
->disk
.minor
)));
9792 const struct imsm_orom
*orom
= get_orom_by_device_id(hba
->dev_id
);
9795 for (dev
= info
->next
; dev
; dev
= dev
->next
) {
9796 dev_path
= devt_to_devpath(makedev(dev
->disk
.major
, dev
->disk
.minor
));
9798 struct sys_dev
*hba2
= NULL
;
9799 for (idev
= intel_devices
; idev
; idev
= idev
->next
) {
9800 if (dev_path
&& strstr(dev_path
, idev
->path
)) {
9808 const struct imsm_orom
*orom2
= hba2
== NULL
? NULL
:
9809 get_orom_by_device_id(hba2
->dev_id
);
9811 if (hba2
&& hba
->type
!= hba2
->type
) {
9812 pr_err("WARNING - HBAs of devices do not match %s != %s\n",
9813 get_sys_dev_type(hba
->type
), get_sys_dev_type(hba2
->type
));
9817 if (orom
!= orom2
) {
9818 pr_err("WARNING - IMSM container assembled with disks under different HBAs!\n"
9819 " This operation is not supported and can lead to data loss.\n");
9824 pr_err("WARNING - IMSM container assembled with disks under HBAs without IMSM platform support!\n"
9825 " This operation is not supported and can lead to data loss.\n");
9833 /*******************************************************************************
9834 * Function: imsm_record_badblock
9835 * Description: This routine stores new bad block record in BBM log
9838 * a : array containing a bad block
9839 * slot : disk number containing a bad block
9840 * sector : bad block sector
9841 * length : bad block sectors range
9845 ******************************************************************************/
9846 static int imsm_record_badblock(struct active_array
*a
, int slot
,
9847 unsigned long long sector
, int length
)
9849 struct intel_super
*super
= a
->container
->sb
;
9853 ord
= imsm_disk_slot_to_ord(a
, slot
);
9857 ret
= record_new_badblock(super
->bbm_log
, ord_to_idx(ord
), sector
,
9860 super
->updates_pending
++;
9864 /*******************************************************************************
9865 * Function: imsm_clear_badblock
9866 * Description: This routine clears bad block record from BBM log
9869 * a : array containing a bad block
9870 * slot : disk number containing a bad block
9871 * sector : bad block sector
9872 * length : bad block sectors range
9876 ******************************************************************************/
9877 static int imsm_clear_badblock(struct active_array
*a
, int slot
,
9878 unsigned long long sector
, int length
)
9880 struct intel_super
*super
= a
->container
->sb
;
9884 ord
= imsm_disk_slot_to_ord(a
, slot
);
9888 ret
= clear_badblock(super
->bbm_log
, ord_to_idx(ord
), sector
, length
);
9890 super
->updates_pending
++;
9894 /*******************************************************************************
9895 * Function: imsm_get_badblocks
9896 * Description: This routine get list of bad blocks for an array
9900 * slot : disk number
9902 * bb : structure containing bad blocks
9904 ******************************************************************************/
9905 static struct md_bb
*imsm_get_badblocks(struct active_array
*a
, int slot
)
9907 int inst
= a
->info
.container_member
;
9908 struct intel_super
*super
= a
->container
->sb
;
9909 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
9910 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
9913 ord
= imsm_disk_slot_to_ord(a
, slot
);
9917 get_volume_badblocks(super
->bbm_log
, ord_to_idx(ord
), pba_of_lba0(map
),
9918 blocks_per_member(map
), &super
->bb
);
9922 /*******************************************************************************
9923 * Function: init_migr_record_imsm
9924 * Description: Function inits imsm migration record
9926 * super : imsm internal array info
9927 * dev : device under migration
9928 * info : general array info to find the smallest device
9931 ******************************************************************************/
9932 void init_migr_record_imsm(struct supertype
*st
, struct imsm_dev
*dev
,
9933 struct mdinfo
*info
)
9935 struct intel_super
*super
= st
->sb
;
9936 struct migr_record
*migr_rec
= super
->migr_rec
;
9938 unsigned long long dsize
, dev_sectors
;
9939 long long unsigned min_dev_sectors
= -1LLU;
9943 struct imsm_map
*map_dest
= get_imsm_map(dev
, MAP_0
);
9944 struct imsm_map
*map_src
= get_imsm_map(dev
, MAP_1
);
9945 unsigned long long num_migr_units
;
9946 unsigned long long array_blocks
;
9948 memset(migr_rec
, 0, sizeof(struct migr_record
));
9949 migr_rec
->family_num
= __cpu_to_le32(super
->anchor
->family_num
);
9951 /* only ascending reshape supported now */
9952 migr_rec
->ascending_migr
= __cpu_to_le32(1);
9954 migr_rec
->dest_depth_per_unit
= GEN_MIGR_AREA_SIZE
/
9955 max(map_dest
->blocks_per_strip
, map_src
->blocks_per_strip
);
9956 migr_rec
->dest_depth_per_unit
*=
9957 max(map_dest
->blocks_per_strip
, map_src
->blocks_per_strip
);
9958 new_data_disks
= imsm_num_data_members(dev
, MAP_0
);
9959 migr_rec
->blocks_per_unit
=
9960 __cpu_to_le32(migr_rec
->dest_depth_per_unit
* new_data_disks
);
9961 migr_rec
->dest_depth_per_unit
=
9962 __cpu_to_le32(migr_rec
->dest_depth_per_unit
);
9963 array_blocks
= info
->component_size
* new_data_disks
;
9965 array_blocks
/ __le32_to_cpu(migr_rec
->blocks_per_unit
);
9967 if (array_blocks
% __le32_to_cpu(migr_rec
->blocks_per_unit
))
9969 migr_rec
->num_migr_units
= __cpu_to_le32(num_migr_units
);
9971 migr_rec
->post_migr_vol_cap
= dev
->size_low
;
9972 migr_rec
->post_migr_vol_cap_hi
= dev
->size_high
;
9974 /* Find the smallest dev */
9975 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
9976 sprintf(nm
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
9977 fd
= dev_open(nm
, O_RDONLY
);
9980 get_dev_size(fd
, NULL
, &dsize
);
9981 dev_sectors
= dsize
/ 512;
9982 if (dev_sectors
< min_dev_sectors
)
9983 min_dev_sectors
= dev_sectors
;
9986 migr_rec
->ckpt_area_pba
= __cpu_to_le32(min_dev_sectors
-
9987 RAID_DISK_RESERVED_BLOCKS_IMSM_HI
);
9989 write_imsm_migr_rec(st
);
9994 /*******************************************************************************
9995 * Function: save_backup_imsm
9996 * Description: Function saves critical data stripes to Migration Copy Area
9997 * and updates the current migration unit status.
9998 * Use restore_stripes() to form a destination stripe,
9999 * and to write it to the Copy Area.
10001 * st : supertype information
10002 * dev : imsm device that backup is saved for
10003 * info : general array info
10004 * buf : input buffer
10005 * length : length of data to backup (blocks_per_unit)
10009 ******************************************************************************/
10010 int save_backup_imsm(struct supertype
*st
,
10011 struct imsm_dev
*dev
,
10012 struct mdinfo
*info
,
10017 struct intel_super
*super
= st
->sb
;
10018 unsigned long long *target_offsets
;
10021 struct imsm_map
*map_dest
= get_imsm_map(dev
, MAP_0
);
10022 int new_disks
= map_dest
->num_members
;
10023 int dest_layout
= 0;
10025 unsigned long long start
;
10026 int data_disks
= imsm_num_data_members(dev
, MAP_0
);
10028 targets
= xmalloc(new_disks
* sizeof(int));
10030 for (i
= 0; i
< new_disks
; i
++)
10033 target_offsets
= xcalloc(new_disks
, sizeof(unsigned long long));
10035 start
= info
->reshape_progress
* 512;
10036 for (i
= 0; i
< new_disks
; i
++) {
10037 target_offsets
[i
] = (unsigned long long)
10038 __le32_to_cpu(super
->migr_rec
->ckpt_area_pba
) * 512;
10039 /* move back copy area adderss, it will be moved forward
10040 * in restore_stripes() using start input variable
10042 target_offsets
[i
] -= start
/data_disks
;
10045 if (open_backup_targets(info
, new_disks
, targets
,
10049 dest_layout
= imsm_level_to_layout(map_dest
->raid_level
);
10050 dest_chunk
= __le16_to_cpu(map_dest
->blocks_per_strip
) * 512;
10052 if (restore_stripes(targets
, /* list of dest devices */
10053 target_offsets
, /* migration record offsets */
10056 map_dest
->raid_level
,
10058 -1, /* source backup file descriptor */
10059 0, /* input buf offset
10060 * always 0 buf is already offseted */
10064 pr_err("Error restoring stripes\n");
10072 close_targets(targets
, new_disks
);
10075 free(target_offsets
);
10080 /*******************************************************************************
10081 * Function: save_checkpoint_imsm
10082 * Description: Function called for current unit status update
10083 * in the migration record. It writes it to disk.
10085 * super : imsm internal array info
10086 * info : general array info
10090 * 2: failure, means no valid migration record
10091 * / no general migration in progress /
10092 ******************************************************************************/
10093 int save_checkpoint_imsm(struct supertype
*st
, struct mdinfo
*info
, int state
)
10095 struct intel_super
*super
= st
->sb
;
10096 unsigned long long blocks_per_unit
;
10097 unsigned long long curr_migr_unit
;
10099 if (load_imsm_migr_rec(super
, info
) != 0) {
10100 dprintf("imsm: ERROR: Cannot read migration record for checkpoint save.\n");
10104 blocks_per_unit
= __le32_to_cpu(super
->migr_rec
->blocks_per_unit
);
10105 if (blocks_per_unit
== 0) {
10106 dprintf("imsm: no migration in progress.\n");
10109 curr_migr_unit
= info
->reshape_progress
/ blocks_per_unit
;
10110 /* check if array is alligned to copy area
10111 * if it is not alligned, add one to current migration unit value
10112 * this can happend on array reshape finish only
10114 if (info
->reshape_progress
% blocks_per_unit
)
10117 super
->migr_rec
->curr_migr_unit
=
10118 __cpu_to_le32(curr_migr_unit
);
10119 super
->migr_rec
->rec_status
= __cpu_to_le32(state
);
10120 super
->migr_rec
->dest_1st_member_lba
=
10121 __cpu_to_le32(curr_migr_unit
*
10122 __le32_to_cpu(super
->migr_rec
->dest_depth_per_unit
));
10123 if (write_imsm_migr_rec(st
) < 0) {
10124 dprintf("imsm: Cannot write migration record outside backup area\n");
10131 /*******************************************************************************
10132 * Function: recover_backup_imsm
10133 * Description: Function recovers critical data from the Migration Copy Area
10134 * while assembling an array.
10136 * super : imsm internal array info
10137 * info : general array info
10139 * 0 : success (or there is no data to recover)
10141 ******************************************************************************/
10142 int recover_backup_imsm(struct supertype
*st
, struct mdinfo
*info
)
10144 struct intel_super
*super
= st
->sb
;
10145 struct migr_record
*migr_rec
= super
->migr_rec
;
10146 struct imsm_map
*map_dest
;
10147 struct intel_dev
*id
= NULL
;
10148 unsigned long long read_offset
;
10149 unsigned long long write_offset
;
10151 int *targets
= NULL
;
10152 int new_disks
, i
, err
;
10155 unsigned int sector_size
= super
->sector_size
;
10156 unsigned long curr_migr_unit
= __le32_to_cpu(migr_rec
->curr_migr_unit
);
10157 unsigned long num_migr_units
= __le32_to_cpu(migr_rec
->num_migr_units
);
10159 int skipped_disks
= 0;
10161 err
= sysfs_get_str(info
, NULL
, "array_state", (char *)buffer
, 20);
10165 /* recover data only during assemblation */
10166 if (strncmp(buffer
, "inactive", 8) != 0)
10168 /* no data to recover */
10169 if (__le32_to_cpu(migr_rec
->rec_status
) == UNIT_SRC_NORMAL
)
10171 if (curr_migr_unit
>= num_migr_units
)
10174 /* find device during reshape */
10175 for (id
= super
->devlist
; id
; id
= id
->next
)
10176 if (is_gen_migration(id
->dev
))
10181 map_dest
= get_imsm_map(id
->dev
, MAP_0
);
10182 new_disks
= map_dest
->num_members
;
10184 read_offset
= (unsigned long long)
10185 __le32_to_cpu(migr_rec
->ckpt_area_pba
) * 512;
10187 write_offset
= ((unsigned long long)
10188 __le32_to_cpu(migr_rec
->dest_1st_member_lba
) +
10189 pba_of_lba0(map_dest
)) * 512;
10191 unit_len
= __le32_to_cpu(migr_rec
->dest_depth_per_unit
) * 512;
10192 if (posix_memalign((void **)&buf
, sector_size
, unit_len
) != 0)
10194 targets
= xcalloc(new_disks
, sizeof(int));
10196 if (open_backup_targets(info
, new_disks
, targets
, super
, id
->dev
)) {
10197 pr_err("Cannot open some devices belonging to array.\n");
10201 for (i
= 0; i
< new_disks
; i
++) {
10202 if (targets
[i
] < 0) {
10206 if (lseek64(targets
[i
], read_offset
, SEEK_SET
) < 0) {
10207 pr_err("Cannot seek to block: %s\n",
10212 if ((unsigned)read(targets
[i
], buf
, unit_len
) != unit_len
) {
10213 pr_err("Cannot read copy area block: %s\n",
10218 if (lseek64(targets
[i
], write_offset
, SEEK_SET
) < 0) {
10219 pr_err("Cannot seek to block: %s\n",
10224 if ((unsigned)write(targets
[i
], buf
, unit_len
) != unit_len
) {
10225 pr_err("Cannot restore block: %s\n",
10232 if (skipped_disks
> imsm_get_allowed_degradation(info
->new_level
,
10236 pr_err("Cannot restore data from backup. Too many failed disks\n");
10240 if (save_checkpoint_imsm(st
, info
, UNIT_SRC_NORMAL
)) {
10241 /* ignore error == 2, this can mean end of reshape here
10243 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL) during restart\n");
10249 for (i
= 0; i
< new_disks
; i
++)
10258 static char disk_by_path
[] = "/dev/disk/by-path/";
10260 static const char *imsm_get_disk_controller_domain(const char *path
)
10262 char disk_path
[PATH_MAX
];
10266 strcpy(disk_path
, disk_by_path
);
10267 strncat(disk_path
, path
, PATH_MAX
- strlen(disk_path
) - 1);
10268 if (stat(disk_path
, &st
) == 0) {
10269 struct sys_dev
* hba
;
10272 path
= devt_to_devpath(st
.st_rdev
);
10275 hba
= find_disk_attached_hba(-1, path
);
10276 if (hba
&& hba
->type
== SYS_DEV_SAS
)
10278 else if (hba
&& hba
->type
== SYS_DEV_SATA
)
10282 dprintf("path: %s hba: %s attached: %s\n",
10283 path
, (hba
) ? hba
->path
: "NULL", drv
);
10289 static char *imsm_find_array_devnm_by_subdev(int subdev
, char *container
)
10291 static char devnm
[32];
10292 char subdev_name
[20];
10293 struct mdstat_ent
*mdstat
;
10295 sprintf(subdev_name
, "%d", subdev
);
10296 mdstat
= mdstat_by_subdev(subdev_name
, container
);
10300 strcpy(devnm
, mdstat
->devnm
);
10301 free_mdstat(mdstat
);
10305 static int imsm_reshape_is_allowed_on_container(struct supertype
*st
,
10306 struct geo_params
*geo
,
10307 int *old_raid_disks
,
10310 /* currently we only support increasing the number of devices
10311 * for a container. This increases the number of device for each
10312 * member array. They must all be RAID0 or RAID5.
10315 struct mdinfo
*info
, *member
;
10316 int devices_that_can_grow
= 0;
10318 dprintf("imsm: imsm_reshape_is_allowed_on_container(ENTER): st->devnm = (%s)\n", st
->devnm
);
10320 if (geo
->size
> 0 ||
10321 geo
->level
!= UnSet
||
10322 geo
->layout
!= UnSet
||
10323 geo
->chunksize
!= 0 ||
10324 geo
->raid_disks
== UnSet
) {
10325 dprintf("imsm: Container operation is allowed for raid disks number change only.\n");
10329 if (direction
== ROLLBACK_METADATA_CHANGES
) {
10330 dprintf("imsm: Metadata changes rollback is not supported for container operation.\n");
10334 info
= container_content_imsm(st
, NULL
);
10335 for (member
= info
; member
; member
= member
->next
) {
10338 dprintf("imsm: checking device_num: %i\n",
10339 member
->container_member
);
10341 if (geo
->raid_disks
<= member
->array
.raid_disks
) {
10342 /* we work on container for Online Capacity Expansion
10343 * only so raid_disks has to grow
10345 dprintf("imsm: for container operation raid disks increase is required\n");
10349 if (info
->array
.level
!= 0 && info
->array
.level
!= 5) {
10350 /* we cannot use this container with other raid level
10352 dprintf("imsm: for container operation wrong raid level (%i) detected\n",
10353 info
->array
.level
);
10356 /* check for platform support
10357 * for this raid level configuration
10359 struct intel_super
*super
= st
->sb
;
10360 if (!is_raid_level_supported(super
->orom
,
10361 member
->array
.level
,
10362 geo
->raid_disks
)) {
10363 dprintf("platform does not support raid%d with %d disk%s\n",
10366 geo
->raid_disks
> 1 ? "s" : "");
10369 /* check if component size is aligned to chunk size
10371 if (info
->component_size
%
10372 (info
->array
.chunk_size
/512)) {
10373 dprintf("Component size is not aligned to chunk size\n");
10378 if (*old_raid_disks
&&
10379 info
->array
.raid_disks
!= *old_raid_disks
)
10381 *old_raid_disks
= info
->array
.raid_disks
;
10383 /* All raid5 and raid0 volumes in container
10384 * have to be ready for Online Capacity Expansion
10385 * so they need to be assembled. We have already
10386 * checked that no recovery etc is happening.
10388 result
= imsm_find_array_devnm_by_subdev(member
->container_member
,
10389 st
->container_devnm
);
10390 if (result
== NULL
) {
10391 dprintf("imsm: cannot find array\n");
10394 devices_that_can_grow
++;
10397 if (!member
&& devices_that_can_grow
)
10401 dprintf("Container operation allowed\n");
10403 dprintf("Error: %i\n", ret_val
);
10408 /* Function: get_spares_for_grow
10409 * Description: Allocates memory and creates list of spare devices
10410 * avaliable in container. Checks if spare drive size is acceptable.
10411 * Parameters: Pointer to the supertype structure
10412 * Returns: Pointer to the list of spare devices (mdinfo structure) on success,
10415 static struct mdinfo
*get_spares_for_grow(struct supertype
*st
)
10417 unsigned long long min_size
= min_acceptable_spare_size_imsm(st
);
10418 return container_choose_spares(st
, min_size
, NULL
, NULL
, NULL
, 0);
10421 /******************************************************************************
10422 * function: imsm_create_metadata_update_for_reshape
10423 * Function creates update for whole IMSM container.
10425 ******************************************************************************/
10426 static int imsm_create_metadata_update_for_reshape(
10427 struct supertype
*st
,
10428 struct geo_params
*geo
,
10429 int old_raid_disks
,
10430 struct imsm_update_reshape
**updatep
)
10432 struct intel_super
*super
= st
->sb
;
10433 struct imsm_super
*mpb
= super
->anchor
;
10434 int update_memory_size
;
10435 struct imsm_update_reshape
*u
;
10436 struct mdinfo
*spares
;
10439 struct mdinfo
*dev
;
10441 dprintf("(enter) raid_disks = %i\n", geo
->raid_disks
);
10443 delta_disks
= geo
->raid_disks
- old_raid_disks
;
10445 /* size of all update data without anchor */
10446 update_memory_size
= sizeof(struct imsm_update_reshape
);
10448 /* now add space for spare disks that we need to add. */
10449 update_memory_size
+= sizeof(u
->new_disks
[0]) * (delta_disks
- 1);
10451 u
= xcalloc(1, update_memory_size
);
10452 u
->type
= update_reshape_container_disks
;
10453 u
->old_raid_disks
= old_raid_disks
;
10454 u
->new_raid_disks
= geo
->raid_disks
;
10456 /* now get spare disks list
10458 spares
= get_spares_for_grow(st
);
10461 || delta_disks
> spares
->array
.spare_disks
) {
10462 pr_err("imsm: ERROR: Cannot get spare devices for %s.\n", geo
->dev_name
);
10467 /* we have got spares
10468 * update disk list in imsm_disk list table in anchor
10470 dprintf("imsm: %i spares are available.\n\n",
10471 spares
->array
.spare_disks
);
10473 dev
= spares
->devs
;
10474 for (i
= 0; i
< delta_disks
; i
++) {
10479 u
->new_disks
[i
] = makedev(dev
->disk
.major
,
10481 dl
= get_disk_super(super
, dev
->disk
.major
, dev
->disk
.minor
);
10482 dl
->index
= mpb
->num_disks
;
10490 sysfs_free(spares
);
10492 dprintf("imsm: reshape update preparation :");
10493 if (i
== delta_disks
) {
10494 dprintf_cont(" OK\n");
10496 return update_memory_size
;
10499 dprintf_cont(" Error\n");
10504 /******************************************************************************
10505 * function: imsm_create_metadata_update_for_size_change()
10506 * Creates update for IMSM array for array size change.
10508 ******************************************************************************/
10509 static int imsm_create_metadata_update_for_size_change(
10510 struct supertype
*st
,
10511 struct geo_params
*geo
,
10512 struct imsm_update_size_change
**updatep
)
10514 struct intel_super
*super
= st
->sb
;
10515 int update_memory_size
;
10516 struct imsm_update_size_change
*u
;
10518 dprintf("(enter) New size = %llu\n", geo
->size
);
10520 /* size of all update data without anchor */
10521 update_memory_size
= sizeof(struct imsm_update_size_change
);
10523 u
= xcalloc(1, update_memory_size
);
10524 u
->type
= update_size_change
;
10525 u
->subdev
= super
->current_vol
;
10526 u
->new_size
= geo
->size
;
10528 dprintf("imsm: reshape update preparation : OK\n");
10531 return update_memory_size
;
10534 /******************************************************************************
10535 * function: imsm_create_metadata_update_for_migration()
10536 * Creates update for IMSM array.
10538 ******************************************************************************/
10539 static int imsm_create_metadata_update_for_migration(
10540 struct supertype
*st
,
10541 struct geo_params
*geo
,
10542 struct imsm_update_reshape_migration
**updatep
)
10544 struct intel_super
*super
= st
->sb
;
10545 int update_memory_size
;
10546 struct imsm_update_reshape_migration
*u
;
10547 struct imsm_dev
*dev
;
10548 int previous_level
= -1;
10550 dprintf("(enter) New Level = %i\n", geo
->level
);
10552 /* size of all update data without anchor */
10553 update_memory_size
= sizeof(struct imsm_update_reshape_migration
);
10555 u
= xcalloc(1, update_memory_size
);
10556 u
->type
= update_reshape_migration
;
10557 u
->subdev
= super
->current_vol
;
10558 u
->new_level
= geo
->level
;
10559 u
->new_layout
= geo
->layout
;
10560 u
->new_raid_disks
= u
->old_raid_disks
= geo
->raid_disks
;
10561 u
->new_disks
[0] = -1;
10562 u
->new_chunksize
= -1;
10564 dev
= get_imsm_dev(super
, u
->subdev
);
10566 struct imsm_map
*map
;
10568 map
= get_imsm_map(dev
, MAP_0
);
10570 int current_chunk_size
=
10571 __le16_to_cpu(map
->blocks_per_strip
) / 2;
10573 if (geo
->chunksize
!= current_chunk_size
) {
10574 u
->new_chunksize
= geo
->chunksize
/ 1024;
10575 dprintf("imsm: chunk size change from %i to %i\n",
10576 current_chunk_size
, u
->new_chunksize
);
10578 previous_level
= map
->raid_level
;
10581 if (geo
->level
== 5 && previous_level
== 0) {
10582 struct mdinfo
*spares
= NULL
;
10584 u
->new_raid_disks
++;
10585 spares
= get_spares_for_grow(st
);
10586 if (spares
== NULL
|| spares
->array
.spare_disks
< 1) {
10588 sysfs_free(spares
);
10589 update_memory_size
= 0;
10590 dprintf("error: cannot get spare device for requested migration");
10593 sysfs_free(spares
);
10595 dprintf("imsm: reshape update preparation : OK\n");
10598 return update_memory_size
;
10601 static void imsm_update_metadata_locally(struct supertype
*st
,
10602 void *buf
, int len
)
10604 struct metadata_update mu
;
10609 mu
.space_list
= NULL
;
10611 if (imsm_prepare_update(st
, &mu
))
10612 imsm_process_update(st
, &mu
);
10614 while (mu
.space_list
) {
10615 void **space
= mu
.space_list
;
10616 mu
.space_list
= *space
;
10621 /***************************************************************************
10622 * Function: imsm_analyze_change
10623 * Description: Function analyze change for single volume
10624 * and validate if transition is supported
10625 * Parameters: Geometry parameters, supertype structure,
10626 * metadata change direction (apply/rollback)
10627 * Returns: Operation type code on success, -1 if fail
10628 ****************************************************************************/
10629 enum imsm_reshape_type
imsm_analyze_change(struct supertype
*st
,
10630 struct geo_params
*geo
,
10633 struct mdinfo info
;
10635 int check_devs
= 0;
10637 /* number of added/removed disks in operation result */
10638 int devNumChange
= 0;
10639 /* imsm compatible layout value for array geometry verification */
10640 int imsm_layout
= -1;
10642 struct imsm_dev
*dev
;
10643 struct intel_super
*super
;
10644 unsigned long long current_size
;
10645 unsigned long long free_size
;
10646 unsigned long long max_size
;
10649 getinfo_super_imsm_volume(st
, &info
, NULL
);
10650 if (geo
->level
!= info
.array
.level
&& geo
->level
>= 0 &&
10651 geo
->level
!= UnSet
) {
10652 switch (info
.array
.level
) {
10654 if (geo
->level
== 5) {
10655 change
= CH_MIGRATION
;
10656 if (geo
->layout
!= ALGORITHM_LEFT_ASYMMETRIC
) {
10657 pr_err("Error. Requested Layout not supported (left-asymmetric layout is supported only)!\n");
10659 goto analyse_change_exit
;
10661 imsm_layout
= geo
->layout
;
10663 devNumChange
= 1; /* parity disk added */
10664 } else if (geo
->level
== 10) {
10665 change
= CH_TAKEOVER
;
10667 devNumChange
= 2; /* two mirrors added */
10668 imsm_layout
= 0x102; /* imsm supported layout */
10673 if (geo
->level
== 0) {
10674 change
= CH_TAKEOVER
;
10676 devNumChange
= -(geo
->raid_disks
/2);
10677 imsm_layout
= 0; /* imsm raid0 layout */
10681 if (change
== -1) {
10682 pr_err("Error. Level Migration from %d to %d not supported!\n",
10683 info
.array
.level
, geo
->level
);
10684 goto analyse_change_exit
;
10687 geo
->level
= info
.array
.level
;
10689 if (geo
->layout
!= info
.array
.layout
&&
10690 (geo
->layout
!= UnSet
&& geo
->layout
!= -1)) {
10691 change
= CH_MIGRATION
;
10692 if (info
.array
.layout
== 0 && info
.array
.level
== 5 &&
10693 geo
->layout
== 5) {
10694 /* reshape 5 -> 4 */
10695 } else if (info
.array
.layout
== 5 && info
.array
.level
== 5 &&
10696 geo
->layout
== 0) {
10697 /* reshape 4 -> 5 */
10701 pr_err("Error. Layout Migration from %d to %d not supported!\n",
10702 info
.array
.layout
, geo
->layout
);
10704 goto analyse_change_exit
;
10707 geo
->layout
= info
.array
.layout
;
10708 if (imsm_layout
== -1)
10709 imsm_layout
= info
.array
.layout
;
10712 if (geo
->chunksize
> 0 && geo
->chunksize
!= UnSet
&&
10713 geo
->chunksize
!= info
.array
.chunk_size
) {
10714 if (info
.array
.level
== 10) {
10715 pr_err("Error. Chunk size change for RAID 10 is not supported.\n");
10717 goto analyse_change_exit
;
10719 change
= CH_MIGRATION
;
10721 geo
->chunksize
= info
.array
.chunk_size
;
10724 chunk
= geo
->chunksize
/ 1024;
10727 dev
= get_imsm_dev(super
, super
->current_vol
);
10728 data_disks
= imsm_num_data_members(dev
, MAP_0
);
10729 /* compute current size per disk member
10731 current_size
= info
.custom_array_size
/ data_disks
;
10733 if (geo
->size
> 0 && geo
->size
!= MAX_SIZE
) {
10734 /* align component size
10736 geo
->size
= imsm_component_size_aligment_check(
10737 get_imsm_raid_level(dev
->vol
.map
),
10738 chunk
* 1024, super
->sector_size
,
10740 if (geo
->size
== 0) {
10741 pr_err("Error. Size expansion is supported only (current size is %llu, requested size /rounded/ is 0).\n",
10743 goto analyse_change_exit
;
10747 if (current_size
!= geo
->size
&& geo
->size
> 0) {
10748 if (change
!= -1) {
10749 pr_err("Error. Size change should be the only one at a time.\n");
10751 goto analyse_change_exit
;
10753 if ((super
->current_vol
+ 1) != super
->anchor
->num_raid_devs
) {
10754 pr_err("Error. The last volume in container can be expanded only (%i/%s).\n",
10755 super
->current_vol
, st
->devnm
);
10756 goto analyse_change_exit
;
10758 /* check the maximum available size
10760 rv
= imsm_get_free_size(st
, dev
->vol
.map
->num_members
,
10761 0, chunk
, &free_size
);
10763 /* Cannot find maximum available space
10767 max_size
= free_size
+ current_size
;
10768 /* align component size
10770 max_size
= imsm_component_size_aligment_check(
10771 get_imsm_raid_level(dev
->vol
.map
),
10772 chunk
* 1024, super
->sector_size
,
10775 if (geo
->size
== MAX_SIZE
) {
10776 /* requested size change to the maximum available size
10778 if (max_size
== 0) {
10779 pr_err("Error. Cannot find maximum available space.\n");
10781 goto analyse_change_exit
;
10783 geo
->size
= max_size
;
10786 if (direction
== ROLLBACK_METADATA_CHANGES
) {
10787 /* accept size for rollback only
10790 /* round size due to metadata compatibility
10792 geo
->size
= (geo
->size
>> SECT_PER_MB_SHIFT
)
10793 << SECT_PER_MB_SHIFT
;
10794 dprintf("Prepare update for size change to %llu\n",
10796 if (current_size
>= geo
->size
) {
10797 pr_err("Error. Size expansion is supported only (current size is %llu, requested size /rounded/ is %llu).\n",
10798 current_size
, geo
->size
);
10799 goto analyse_change_exit
;
10801 if (max_size
&& geo
->size
> max_size
) {
10802 pr_err("Error. Requested size is larger than maximum available size (maximum available size is %llu, requested size /rounded/ is %llu).\n",
10803 max_size
, geo
->size
);
10804 goto analyse_change_exit
;
10807 geo
->size
*= data_disks
;
10808 geo
->raid_disks
= dev
->vol
.map
->num_members
;
10809 change
= CH_ARRAY_SIZE
;
10811 if (!validate_geometry_imsm(st
,
10814 geo
->raid_disks
+ devNumChange
,
10816 geo
->size
, INVALID_SECTORS
,
10821 struct intel_super
*super
= st
->sb
;
10822 struct imsm_super
*mpb
= super
->anchor
;
10824 if (mpb
->num_raid_devs
> 1) {
10825 pr_err("Error. Cannot perform operation on %s- for this operation it MUST be single array in container\n",
10831 analyse_change_exit
:
10832 if (direction
== ROLLBACK_METADATA_CHANGES
&&
10833 (change
== CH_MIGRATION
|| change
== CH_TAKEOVER
)) {
10834 dprintf("imsm: Metadata changes rollback is not supported for migration and takeover operations.\n");
10840 int imsm_takeover(struct supertype
*st
, struct geo_params
*geo
)
10842 struct intel_super
*super
= st
->sb
;
10843 struct imsm_update_takeover
*u
;
10845 u
= xmalloc(sizeof(struct imsm_update_takeover
));
10847 u
->type
= update_takeover
;
10848 u
->subarray
= super
->current_vol
;
10850 /* 10->0 transition */
10851 if (geo
->level
== 0)
10852 u
->direction
= R10_TO_R0
;
10854 /* 0->10 transition */
10855 if (geo
->level
== 10)
10856 u
->direction
= R0_TO_R10
;
10858 /* update metadata locally */
10859 imsm_update_metadata_locally(st
, u
,
10860 sizeof(struct imsm_update_takeover
));
10861 /* and possibly remotely */
10862 if (st
->update_tail
)
10863 append_metadata_update(st
, u
,
10864 sizeof(struct imsm_update_takeover
));
10871 static int imsm_reshape_super(struct supertype
*st
, unsigned long long size
,
10873 int layout
, int chunksize
, int raid_disks
,
10874 int delta_disks
, char *backup
, char *dev
,
10875 int direction
, int verbose
)
10878 struct geo_params geo
;
10880 dprintf("(enter)\n");
10882 memset(&geo
, 0, sizeof(struct geo_params
));
10884 geo
.dev_name
= dev
;
10885 strcpy(geo
.devnm
, st
->devnm
);
10888 geo
.layout
= layout
;
10889 geo
.chunksize
= chunksize
;
10890 geo
.raid_disks
= raid_disks
;
10891 if (delta_disks
!= UnSet
)
10892 geo
.raid_disks
+= delta_disks
;
10894 dprintf("for level : %i\n", geo
.level
);
10895 dprintf("for raid_disks : %i\n", geo
.raid_disks
);
10897 if (experimental() == 0)
10900 if (strcmp(st
->container_devnm
, st
->devnm
) == 0) {
10901 /* On container level we can only increase number of devices. */
10902 dprintf("imsm: info: Container operation\n");
10903 int old_raid_disks
= 0;
10905 if (imsm_reshape_is_allowed_on_container(
10906 st
, &geo
, &old_raid_disks
, direction
)) {
10907 struct imsm_update_reshape
*u
= NULL
;
10910 len
= imsm_create_metadata_update_for_reshape(
10911 st
, &geo
, old_raid_disks
, &u
);
10914 dprintf("imsm: Cannot prepare update\n");
10915 goto exit_imsm_reshape_super
;
10919 /* update metadata locally */
10920 imsm_update_metadata_locally(st
, u
, len
);
10921 /* and possibly remotely */
10922 if (st
->update_tail
)
10923 append_metadata_update(st
, u
, len
);
10928 pr_err("(imsm) Operation is not allowed on this container\n");
10931 /* On volume level we support following operations
10932 * - takeover: raid10 -> raid0; raid0 -> raid10
10933 * - chunk size migration
10934 * - migration: raid5 -> raid0; raid0 -> raid5
10936 struct intel_super
*super
= st
->sb
;
10937 struct intel_dev
*dev
= super
->devlist
;
10939 dprintf("imsm: info: Volume operation\n");
10940 /* find requested device */
10943 imsm_find_array_devnm_by_subdev(
10944 dev
->index
, st
->container_devnm
);
10945 if (devnm
&& strcmp(devnm
, geo
.devnm
) == 0)
10950 pr_err("Cannot find %s (%s) subarray\n",
10951 geo
.dev_name
, geo
.devnm
);
10952 goto exit_imsm_reshape_super
;
10954 super
->current_vol
= dev
->index
;
10955 change
= imsm_analyze_change(st
, &geo
, direction
);
10958 ret_val
= imsm_takeover(st
, &geo
);
10960 case CH_MIGRATION
: {
10961 struct imsm_update_reshape_migration
*u
= NULL
;
10963 imsm_create_metadata_update_for_migration(
10966 dprintf("imsm: Cannot prepare update\n");
10970 /* update metadata locally */
10971 imsm_update_metadata_locally(st
, u
, len
);
10972 /* and possibly remotely */
10973 if (st
->update_tail
)
10974 append_metadata_update(st
, u
, len
);
10979 case CH_ARRAY_SIZE
: {
10980 struct imsm_update_size_change
*u
= NULL
;
10982 imsm_create_metadata_update_for_size_change(
10985 dprintf("imsm: Cannot prepare update\n");
10989 /* update metadata locally */
10990 imsm_update_metadata_locally(st
, u
, len
);
10991 /* and possibly remotely */
10992 if (st
->update_tail
)
10993 append_metadata_update(st
, u
, len
);
11003 exit_imsm_reshape_super
:
11004 dprintf("imsm: reshape_super Exit code = %i\n", ret_val
);
11008 #define COMPLETED_OK 0
11009 #define COMPLETED_NONE 1
11010 #define COMPLETED_DELAYED 2
11012 static int read_completed(int fd
, unsigned long long *val
)
11017 ret
= sysfs_fd_get_str(fd
, buf
, 50);
11021 ret
= COMPLETED_OK
;
11022 if (strncmp(buf
, "none", 4) == 0) {
11023 ret
= COMPLETED_NONE
;
11024 } else if (strncmp(buf
, "delayed", 7) == 0) {
11025 ret
= COMPLETED_DELAYED
;
11028 *val
= strtoull(buf
, &ep
, 0);
11029 if (ep
== buf
|| (*ep
!= 0 && *ep
!= '\n' && *ep
!= ' '))
11035 /*******************************************************************************
11036 * Function: wait_for_reshape_imsm
11037 * Description: Function writes new sync_max value and waits until
11038 * reshape process reach new position
11040 * sra : general array info
11041 * ndata : number of disks in new array's layout
11044 * 1 : there is no reshape in progress,
11046 ******************************************************************************/
11047 int wait_for_reshape_imsm(struct mdinfo
*sra
, int ndata
)
11049 int fd
= sysfs_get_fd(sra
, NULL
, "sync_completed");
11051 unsigned long long completed
;
11052 /* to_complete : new sync_max position */
11053 unsigned long long to_complete
= sra
->reshape_progress
;
11054 unsigned long long position_to_set
= to_complete
/ ndata
;
11057 dprintf("cannot open reshape_position\n");
11062 if (sysfs_fd_get_ll(fd
, &completed
) < 0) {
11064 dprintf("cannot read reshape_position (no reshape in progres)\n");
11073 if (completed
> position_to_set
) {
11074 dprintf("wrong next position to set %llu (%llu)\n",
11075 to_complete
, position_to_set
);
11079 dprintf("Position set: %llu\n", position_to_set
);
11080 if (sysfs_set_num(sra
, NULL
, "sync_max",
11081 position_to_set
) != 0) {
11082 dprintf("cannot set reshape position to %llu\n",
11091 int timeout
= 3000;
11093 sysfs_wait(fd
, &timeout
);
11094 if (sysfs_get_str(sra
, NULL
, "sync_action",
11096 strncmp(action
, "reshape", 7) != 0) {
11097 if (strncmp(action
, "idle", 4) == 0)
11103 rc
= read_completed(fd
, &completed
);
11105 dprintf("cannot read reshape_position (in loop)\n");
11108 } else if (rc
== COMPLETED_NONE
)
11110 } while (completed
< position_to_set
);
11116 /*******************************************************************************
11117 * Function: check_degradation_change
11118 * Description: Check that array hasn't become failed.
11120 * info : for sysfs access
11121 * sources : source disks descriptors
11122 * degraded: previous degradation level
11124 * degradation level
11125 ******************************************************************************/
11126 int check_degradation_change(struct mdinfo
*info
,
11130 unsigned long long new_degraded
;
11133 rv
= sysfs_get_ll(info
, NULL
, "degraded", &new_degraded
);
11134 if (rv
== -1 || (new_degraded
!= (unsigned long long)degraded
)) {
11135 /* check each device to ensure it is still working */
11138 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
11139 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
11141 if (sd
->disk
.state
& (1<<MD_DISK_SYNC
)) {
11144 if (sysfs_get_str(info
,
11145 sd
, "state", sbuf
, sizeof(sbuf
)) < 0 ||
11146 strstr(sbuf
, "faulty") ||
11147 strstr(sbuf
, "in_sync") == NULL
) {
11148 /* this device is dead */
11149 sd
->disk
.state
= (1<<MD_DISK_FAULTY
);
11150 if (sd
->disk
.raid_disk
>= 0 &&
11151 sources
[sd
->disk
.raid_disk
] >= 0) {
11153 sd
->disk
.raid_disk
]);
11154 sources
[sd
->disk
.raid_disk
] =
11163 return new_degraded
;
11166 /*******************************************************************************
11167 * Function: imsm_manage_reshape
11168 * Description: Function finds array under reshape and it manages reshape
11169 * process. It creates stripes backups (if required) and sets
11172 * afd : Backup handle (nattive) - not used
11173 * sra : general array info
11174 * reshape : reshape parameters - not used
11175 * st : supertype structure
11176 * blocks : size of critical section [blocks]
11177 * fds : table of source device descriptor
11178 * offsets : start of array (offest per devices)
11180 * destfd : table of destination device descriptor
11181 * destoffsets : table of destination offsets (per device)
11183 * 1 : success, reshape is done
11185 ******************************************************************************/
11186 static int imsm_manage_reshape(
11187 int afd
, struct mdinfo
*sra
, struct reshape
*reshape
,
11188 struct supertype
*st
, unsigned long backup_blocks
,
11189 int *fds
, unsigned long long *offsets
,
11190 int dests
, int *destfd
, unsigned long long *destoffsets
)
11193 struct intel_super
*super
= st
->sb
;
11194 struct intel_dev
*dv
;
11195 unsigned int sector_size
= super
->sector_size
;
11196 struct imsm_dev
*dev
= NULL
;
11197 struct imsm_map
*map_src
;
11198 int migr_vol_qan
= 0;
11199 int ndata
, odata
; /* [bytes] */
11200 int chunk
; /* [bytes] */
11201 struct migr_record
*migr_rec
;
11203 unsigned int buf_size
; /* [bytes] */
11204 unsigned long long max_position
; /* array size [bytes] */
11205 unsigned long long next_step
; /* [blocks]/[bytes] */
11206 unsigned long long old_data_stripe_length
;
11207 unsigned long long start_src
; /* [bytes] */
11208 unsigned long long start
; /* [bytes] */
11209 unsigned long long start_buf_shift
; /* [bytes] */
11211 int source_layout
= 0;
11216 if (!fds
|| !offsets
)
11219 /* Find volume during the reshape */
11220 for (dv
= super
->devlist
; dv
; dv
= dv
->next
) {
11221 if (dv
->dev
->vol
.migr_type
== MIGR_GEN_MIGR
11222 && dv
->dev
->vol
.migr_state
== 1) {
11227 /* Only one volume can migrate at the same time */
11228 if (migr_vol_qan
!= 1) {
11229 pr_err("%s", migr_vol_qan
?
11230 "Number of migrating volumes greater than 1\n" :
11231 "There is no volume during migrationg\n");
11235 map_src
= get_imsm_map(dev
, MAP_1
);
11236 if (map_src
== NULL
)
11239 ndata
= imsm_num_data_members(dev
, MAP_0
);
11240 odata
= imsm_num_data_members(dev
, MAP_1
);
11242 chunk
= __le16_to_cpu(map_src
->blocks_per_strip
) * 512;
11243 old_data_stripe_length
= odata
* chunk
;
11245 migr_rec
= super
->migr_rec
;
11247 /* initialize migration record for start condition */
11248 if (sra
->reshape_progress
== 0)
11249 init_migr_record_imsm(st
, dev
, sra
);
11251 if (__le32_to_cpu(migr_rec
->rec_status
) != UNIT_SRC_NORMAL
) {
11252 dprintf("imsm: cannot restart migration when data are present in copy area.\n");
11255 /* Save checkpoint to update migration record for current
11256 * reshape position (in md). It can be farther than current
11257 * reshape position in metadata.
11259 if (save_checkpoint_imsm(st
, sra
, UNIT_SRC_NORMAL
) == 1) {
11260 /* ignore error == 2, this can mean end of reshape here
11262 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL, initial save)\n");
11267 /* size for data */
11268 buf_size
= __le32_to_cpu(migr_rec
->blocks_per_unit
) * 512;
11269 /* extend buffer size for parity disk */
11270 buf_size
+= __le32_to_cpu(migr_rec
->dest_depth_per_unit
) * 512;
11271 /* add space for stripe aligment */
11272 buf_size
+= old_data_stripe_length
;
11273 if (posix_memalign((void **)&buf
, MAX_SECTOR_SIZE
, buf_size
)) {
11274 dprintf("imsm: Cannot allocate checkpoint buffer\n");
11278 max_position
= sra
->component_size
* ndata
;
11279 source_layout
= imsm_level_to_layout(map_src
->raid_level
);
11281 while (__le32_to_cpu(migr_rec
->curr_migr_unit
) <
11282 __le32_to_cpu(migr_rec
->num_migr_units
)) {
11283 /* current reshape position [blocks] */
11284 unsigned long long current_position
=
11285 __le32_to_cpu(migr_rec
->blocks_per_unit
)
11286 * __le32_to_cpu(migr_rec
->curr_migr_unit
);
11287 unsigned long long border
;
11289 /* Check that array hasn't become failed.
11291 degraded
= check_degradation_change(sra
, fds
, degraded
);
11292 if (degraded
> 1) {
11293 dprintf("imsm: Abort reshape due to degradation level (%i)\n", degraded
);
11297 next_step
= __le32_to_cpu(migr_rec
->blocks_per_unit
);
11299 if ((current_position
+ next_step
) > max_position
)
11300 next_step
= max_position
- current_position
;
11302 start
= current_position
* 512;
11304 /* align reading start to old geometry */
11305 start_buf_shift
= start
% old_data_stripe_length
;
11306 start_src
= start
- start_buf_shift
;
11308 border
= (start_src
/ odata
) - (start
/ ndata
);
11310 if (border
<= __le32_to_cpu(migr_rec
->dest_depth_per_unit
)) {
11311 /* save critical stripes to buf
11312 * start - start address of current unit
11313 * to backup [bytes]
11314 * start_src - start address of current unit
11315 * to backup alligned to source array
11318 unsigned long long next_step_filler
;
11319 unsigned long long copy_length
= next_step
* 512;
11321 /* allign copy area length to stripe in old geometry */
11322 next_step_filler
= ((copy_length
+ start_buf_shift
)
11323 % old_data_stripe_length
);
11324 if (next_step_filler
)
11325 next_step_filler
= (old_data_stripe_length
11326 - next_step_filler
);
11327 dprintf("save_stripes() parameters: start = %llu,\tstart_src = %llu,\tnext_step*512 = %llu,\tstart_in_buf_shift = %llu,\tnext_step_filler = %llu\n",
11328 start
, start_src
, copy_length
,
11329 start_buf_shift
, next_step_filler
);
11331 if (save_stripes(fds
, offsets
, map_src
->num_members
,
11332 chunk
, map_src
->raid_level
,
11333 source_layout
, 0, NULL
, start_src
,
11335 next_step_filler
+ start_buf_shift
,
11337 dprintf("imsm: Cannot save stripes to buffer\n");
11340 /* Convert data to destination format and store it
11341 * in backup general migration area
11343 if (save_backup_imsm(st
, dev
, sra
,
11344 buf
+ start_buf_shift
, copy_length
)) {
11345 dprintf("imsm: Cannot save stripes to target devices\n");
11348 if (save_checkpoint_imsm(st
, sra
,
11349 UNIT_SRC_IN_CP_AREA
)) {
11350 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_IN_CP_AREA)\n");
11354 /* set next step to use whole border area */
11355 border
/= next_step
;
11357 next_step
*= border
;
11359 /* When data backed up, checkpoint stored,
11360 * kick the kernel to reshape unit of data
11362 next_step
= next_step
+ sra
->reshape_progress
;
11363 /* limit next step to array max position */
11364 if (next_step
> max_position
)
11365 next_step
= max_position
;
11366 sysfs_set_num(sra
, NULL
, "suspend_lo", sra
->reshape_progress
);
11367 sysfs_set_num(sra
, NULL
, "suspend_hi", next_step
);
11368 sra
->reshape_progress
= next_step
;
11370 /* wait until reshape finish */
11371 if (wait_for_reshape_imsm(sra
, ndata
)) {
11372 dprintf("wait_for_reshape_imsm returned error!\n");
11378 if (save_checkpoint_imsm(st
, sra
, UNIT_SRC_NORMAL
) == 1) {
11379 /* ignore error == 2, this can mean end of reshape here
11381 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL)\n");
11387 /* clear migr_rec on disks after successful migration */
11390 memset(super
->migr_rec_buf
, 0, MIGR_REC_BUF_SECTORS
*sector_size
);
11391 for (d
= super
->disks
; d
; d
= d
->next
) {
11392 if (d
->index
< 0 || is_failed(&d
->disk
))
11394 unsigned long long dsize
;
11396 get_dev_size(d
->fd
, NULL
, &dsize
);
11397 if (lseek64(d
->fd
, dsize
- MIGR_REC_SECTOR_POSITION
*sector_size
,
11399 if (write(d
->fd
, super
->migr_rec_buf
,
11400 MIGR_REC_BUF_SECTORS
*sector_size
) !=
11401 MIGR_REC_BUF_SECTORS
*sector_size
)
11402 perror("Write migr_rec failed");
11406 /* return '1' if done */
11410 /* See Grow.c: abort_reshape() for further explanation */
11411 sysfs_set_num(sra
, NULL
, "suspend_lo", 0x7FFFFFFFFFFFFFFFULL
);
11412 sysfs_set_num(sra
, NULL
, "suspend_hi", 0);
11413 sysfs_set_num(sra
, NULL
, "suspend_lo", 0);
11418 #endif /* MDASSEMBLE */
11420 struct superswitch super_imsm
= {
11422 .examine_super
= examine_super_imsm
,
11423 .brief_examine_super
= brief_examine_super_imsm
,
11424 .brief_examine_subarrays
= brief_examine_subarrays_imsm
,
11425 .export_examine_super
= export_examine_super_imsm
,
11426 .detail_super
= detail_super_imsm
,
11427 .brief_detail_super
= brief_detail_super_imsm
,
11428 .write_init_super
= write_init_super_imsm
,
11429 .validate_geometry
= validate_geometry_imsm
,
11430 .add_to_super
= add_to_super_imsm
,
11431 .remove_from_super
= remove_from_super_imsm
,
11432 .detail_platform
= detail_platform_imsm
,
11433 .export_detail_platform
= export_detail_platform_imsm
,
11434 .kill_subarray
= kill_subarray_imsm
,
11435 .update_subarray
= update_subarray_imsm
,
11436 .load_container
= load_container_imsm
,
11437 .default_geometry
= default_geometry_imsm
,
11438 .get_disk_controller_domain
= imsm_get_disk_controller_domain
,
11439 .reshape_super
= imsm_reshape_super
,
11440 .manage_reshape
= imsm_manage_reshape
,
11441 .recover_backup
= recover_backup_imsm
,
11442 .copy_metadata
= copy_metadata_imsm
,
11444 .match_home
= match_home_imsm
,
11445 .uuid_from_super
= uuid_from_super_imsm
,
11446 .getinfo_super
= getinfo_super_imsm
,
11447 .getinfo_super_disks
= getinfo_super_disks_imsm
,
11448 .update_super
= update_super_imsm
,
11450 .avail_size
= avail_size_imsm
,
11451 .min_acceptable_spare_size
= min_acceptable_spare_size_imsm
,
11453 .compare_super
= compare_super_imsm
,
11455 .load_super
= load_super_imsm
,
11456 .init_super
= init_super_imsm
,
11457 .store_super
= store_super_imsm
,
11458 .free_super
= free_super_imsm
,
11459 .match_metadata_desc
= match_metadata_desc_imsm
,
11460 .container_content
= container_content_imsm
,
11461 .validate_container
= validate_container_imsm
,
11468 .open_new
= imsm_open_new
,
11469 .set_array_state
= imsm_set_array_state
,
11470 .set_disk
= imsm_set_disk
,
11471 .sync_metadata
= imsm_sync_metadata
,
11472 .activate_spare
= imsm_activate_spare
,
11473 .process_update
= imsm_process_update
,
11474 .prepare_update
= imsm_prepare_update
,
11475 .record_bad_block
= imsm_record_badblock
,
11476 .clear_bad_block
= imsm_clear_badblock
,
11477 .get_bad_blocks
= imsm_get_badblocks
,
11478 #endif /* MDASSEMBLE */