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 8192
92 #define NUM_BLOCKS_DIRTY_STRIPE_REGION 2048
93 #define SECT_PER_MB_SHIFT 11
94 #define MAX_SECTOR_SIZE 4096
95 #define MULTIPLE_PPL_AREA_SIZE_IMSM (1024 * 1024) /* Size of the whole
100 * This macro let's us ensure that no-one accidentally
101 * changes the size of a struct
103 #define ASSERT_SIZE(_struct, size) \
104 static inline void __assert_size_##_struct(void) \
108 case (sizeof(struct _struct) == size): break; \
112 /* Disk configuration info. */
113 #define IMSM_MAX_DEVICES 255
115 __u8 serial
[MAX_RAID_SERIAL_LEN
];/* 0xD8 - 0xE7 ascii serial number */
116 __u32 total_blocks_lo
; /* 0xE8 - 0xEB total blocks lo */
117 __u32 scsi_id
; /* 0xEC - 0xEF scsi ID */
118 #define SPARE_DISK __cpu_to_le32(0x01) /* Spare */
119 #define CONFIGURED_DISK __cpu_to_le32(0x02) /* Member of some RaidDev */
120 #define FAILED_DISK __cpu_to_le32(0x04) /* Permanent failure */
121 #define JOURNAL_DISK __cpu_to_le32(0x2000000) /* Device marked as Journaling Drive */
122 __u32 status
; /* 0xF0 - 0xF3 */
123 __u32 owner_cfg_num
; /* which config 0,1,2... owns this disk */
124 __u32 total_blocks_hi
; /* 0xF4 - 0xF5 total blocks hi */
125 #define IMSM_DISK_FILLERS 3
126 __u32 filler
[IMSM_DISK_FILLERS
]; /* 0xF5 - 0x107 MPB_DISK_FILLERS for future expansion */
128 ASSERT_SIZE(imsm_disk
, 48)
130 /* map selector for map managment
136 /* RAID map configuration infos. */
138 __u32 pba_of_lba0_lo
; /* start address of partition */
139 __u32 blocks_per_member_lo
;/* blocks per member */
140 __u32 num_data_stripes_lo
; /* number of data stripes */
141 __u16 blocks_per_strip
;
142 __u8 map_state
; /* Normal, Uninitialized, Degraded, Failed */
143 #define IMSM_T_STATE_NORMAL 0
144 #define IMSM_T_STATE_UNINITIALIZED 1
145 #define IMSM_T_STATE_DEGRADED 2
146 #define IMSM_T_STATE_FAILED 3
148 #define IMSM_T_RAID0 0
149 #define IMSM_T_RAID1 1
150 #define IMSM_T_RAID5 5 /* since metadata version 1.2.02 ? */
151 __u8 num_members
; /* number of member disks */
152 __u8 num_domains
; /* number of parity domains */
153 __u8 failed_disk_num
; /* valid only when state is degraded */
155 __u32 pba_of_lba0_hi
;
156 __u32 blocks_per_member_hi
;
157 __u32 num_data_stripes_hi
;
158 __u32 filler
[4]; /* expansion area */
159 #define IMSM_ORD_REBUILD (1 << 24)
160 __u32 disk_ord_tbl
[1]; /* disk_ord_tbl[num_members],
161 * top byte contains some flags
164 ASSERT_SIZE(imsm_map
, 52)
167 __u32 curr_migr_unit
;
168 __u32 checkpoint_id
; /* id to access curr_migr_unit */
169 __u8 migr_state
; /* Normal or Migrating */
171 #define MIGR_REBUILD 1
172 #define MIGR_VERIFY 2 /* analagous to echo check > sync_action */
173 #define MIGR_GEN_MIGR 3
174 #define MIGR_STATE_CHANGE 4
175 #define MIGR_REPAIR 5
176 __u8 migr_type
; /* Initializing, Rebuilding, ... */
177 #define RAIDVOL_CLEAN 0
178 #define RAIDVOL_DIRTY 1
179 #define RAIDVOL_DSRECORD_VALID 2
181 __u8 fs_state
; /* fast-sync state for CnG (0xff == disabled) */
182 __u16 verify_errors
; /* number of mismatches */
183 __u16 bad_blocks
; /* number of bad blocks during verify */
185 struct imsm_map map
[1];
186 /* here comes another one if migr_state */
188 ASSERT_SIZE(imsm_vol
, 84)
191 __u8 volume
[MAX_RAID_SERIAL_LEN
];
194 #define DEV_BOOTABLE __cpu_to_le32(0x01)
195 #define DEV_BOOT_DEVICE __cpu_to_le32(0x02)
196 #define DEV_READ_COALESCING __cpu_to_le32(0x04)
197 #define DEV_WRITE_COALESCING __cpu_to_le32(0x08)
198 #define DEV_LAST_SHUTDOWN_DIRTY __cpu_to_le32(0x10)
199 #define DEV_HIDDEN_AT_BOOT __cpu_to_le32(0x20)
200 #define DEV_CURRENTLY_HIDDEN __cpu_to_le32(0x40)
201 #define DEV_VERIFY_AND_FIX __cpu_to_le32(0x80)
202 #define DEV_MAP_STATE_UNINIT __cpu_to_le32(0x100)
203 #define DEV_NO_AUTO_RECOVERY __cpu_to_le32(0x200)
204 #define DEV_CLONE_N_GO __cpu_to_le32(0x400)
205 #define DEV_CLONE_MAN_SYNC __cpu_to_le32(0x800)
206 #define DEV_CNG_MASTER_DISK_NUM __cpu_to_le32(0x1000)
207 __u32 status
; /* Persistent RaidDev status */
208 __u32 reserved_blocks
; /* Reserved blocks at beginning of volume */
212 __u8 cng_master_disk
;
216 __u16 my_vol_raid_dev_num
; /* Used in Unique volume Id for this RaidDev */
222 /* Unique Volume Id of the NvCache Volume associated with this volume */
223 __u32 nvc_vol_orig_family_num
;
224 __u16 nvc_vol_raid_dev_num
;
227 #define RWH_DISTRIBUTED 1
228 #define RWH_JOURNALING_DRIVE 2
229 #define RWH_MULTIPLE_DISTRIBUTED 3
230 #define RWH_MULTIPLE_PPLS_JOURNALING_DRIVE 4
231 #define RWH_MULTIPLE_OFF 5
232 __u8 rwh_policy
; /* Raid Write Hole Policy */
233 __u8 jd_serial
[MAX_RAID_SERIAL_LEN
]; /* Journal Drive serial number */
236 #define IMSM_DEV_FILLERS 3
237 __u32 filler
[IMSM_DEV_FILLERS
];
240 ASSERT_SIZE(imsm_dev
, 164)
243 __u8 sig
[MAX_SIGNATURE_LENGTH
]; /* 0x00 - 0x1F */
244 __u32 check_sum
; /* 0x20 - 0x23 MPB Checksum */
245 __u32 mpb_size
; /* 0x24 - 0x27 Size of MPB */
246 __u32 family_num
; /* 0x28 - 0x2B Checksum from first time this config was written */
247 __u32 generation_num
; /* 0x2C - 0x2F Incremented each time this array's MPB is written */
248 __u32 error_log_size
; /* 0x30 - 0x33 in bytes */
249 __u32 attributes
; /* 0x34 - 0x37 */
250 __u8 num_disks
; /* 0x38 Number of configured disks */
251 __u8 num_raid_devs
; /* 0x39 Number of configured volumes */
252 __u8 error_log_pos
; /* 0x3A */
253 __u8 fill
[1]; /* 0x3B */
254 __u32 cache_size
; /* 0x3c - 0x40 in mb */
255 __u32 orig_family_num
; /* 0x40 - 0x43 original family num */
256 __u32 pwr_cycle_count
; /* 0x44 - 0x47 simulated power cycle count for array */
257 __u32 bbm_log_size
; /* 0x48 - 0x4B - size of bad Block Mgmt Log in bytes */
258 __u16 num_raid_devs_created
; /* 0x4C - 0x4D Used for generating unique
259 * volume IDs for raid_dev created in this array
262 __u16 filler1
; /* 0x4E - 0x4F */
263 #define IMSM_FILLERS 34
264 __u32 filler
[IMSM_FILLERS
]; /* 0x50 - 0xD7 RAID_MPB_FILLERS */
265 struct imsm_disk disk
[1]; /* 0xD8 diskTbl[numDisks] */
266 /* here comes imsm_dev[num_raid_devs] */
267 /* here comes BBM logs */
269 ASSERT_SIZE(imsm_super
, 264)
271 #define BBM_LOG_MAX_ENTRIES 254
272 #define BBM_LOG_MAX_LBA_ENTRY_VAL 256 /* Represents 256 LBAs */
273 #define BBM_LOG_SIGNATURE 0xabadb10c
275 struct bbm_log_block_addr
{
278 } __attribute__ ((__packed__
));
280 struct bbm_log_entry
{
281 __u8 marked_count
; /* Number of blocks marked - 1 */
282 __u8 disk_ordinal
; /* Disk entry within the imsm_super */
283 struct bbm_log_block_addr defective_block_start
;
284 } __attribute__ ((__packed__
));
287 __u32 signature
; /* 0xABADB10C */
289 struct bbm_log_entry marked_block_entries
[BBM_LOG_MAX_ENTRIES
];
291 ASSERT_SIZE(bbm_log
, 2040)
293 static char *map_state_str
[] = { "normal", "uninitialized", "degraded", "failed" };
295 #define BLOCKS_PER_KB (1024/512)
297 #define RAID_DISK_RESERVED_BLOCKS_IMSM_HI 2209
299 #define GEN_MIGR_AREA_SIZE 2048 /* General Migration Copy Area size in blocks */
301 #define MIGR_REC_BUF_SECTORS 1 /* size of migr_record i/o buffer in sectors */
302 #define MIGR_REC_SECTOR_POSITION 1 /* migr_record position offset on disk,
303 * MIGR_REC_BUF_SECTORS <= MIGR_REC_SECTOR_POS
306 #define UNIT_SRC_NORMAL 0 /* Source data for curr_migr_unit must
307 * be recovered using srcMap */
308 #define UNIT_SRC_IN_CP_AREA 1 /* Source data for curr_migr_unit has
309 * already been migrated and must
310 * be recovered from checkpoint area */
312 #define PPL_ENTRY_SPACE (128 * 1024) /* Size of single PPL, without the header */
315 __u32 rec_status
; /* Status used to determine how to restart
316 * migration in case it aborts
318 __u32 curr_migr_unit_lo
; /* 0..numMigrUnits-1 */
319 __u32 family_num
; /* Family number of MPB
320 * containing the RaidDev
321 * that is migrating */
322 __u32 ascending_migr
; /* True if migrating in increasing
324 __u32 blocks_per_unit
; /* Num disk blocks per unit of operation */
325 __u32 dest_depth_per_unit
; /* Num member blocks each destMap
327 * advances per unit-of-operation */
328 __u32 ckpt_area_pba_lo
; /* Pba of first block of ckpt copy area */
329 __u32 dest_1st_member_lba_lo
; /* First member lba on first
330 * stripe of destination */
331 __u32 num_migr_units_lo
; /* Total num migration units-of-op */
332 __u32 post_migr_vol_cap
; /* Size of volume after
333 * migration completes */
334 __u32 post_migr_vol_cap_hi
; /* Expansion space for LBA64 */
335 __u32 ckpt_read_disk_num
; /* Which member disk in destSubMap[0] the
336 * migration ckpt record was read from
337 * (for recovered migrations) */
338 __u32 curr_migr_unit_hi
; /* 0..numMigrUnits-1 high order 32 bits */
339 __u32 ckpt_area_pba_hi
; /* Pba of first block of ckpt copy area
340 * high order 32 bits */
341 __u32 dest_1st_member_lba_hi
; /* First member lba on first stripe of
342 * destination - high order 32 bits */
343 __u32 num_migr_units_hi
; /* Total num migration units-of-op
344 * high order 32 bits */
346 ASSERT_SIZE(migr_record
, 64)
351 * 2: metadata does not match
359 struct md_list
*next
;
362 #define pr_vrb(fmt, arg...) (void) (verbose && pr_err(fmt, ##arg))
364 static __u8
migr_type(struct imsm_dev
*dev
)
366 if (dev
->vol
.migr_type
== MIGR_VERIFY
&&
367 dev
->status
& DEV_VERIFY_AND_FIX
)
370 return dev
->vol
.migr_type
;
373 static void set_migr_type(struct imsm_dev
*dev
, __u8 migr_type
)
375 /* for compatibility with older oroms convert MIGR_REPAIR, into
376 * MIGR_VERIFY w/ DEV_VERIFY_AND_FIX status
378 if (migr_type
== MIGR_REPAIR
) {
379 dev
->vol
.migr_type
= MIGR_VERIFY
;
380 dev
->status
|= DEV_VERIFY_AND_FIX
;
382 dev
->vol
.migr_type
= migr_type
;
383 dev
->status
&= ~DEV_VERIFY_AND_FIX
;
387 static unsigned int sector_count(__u32 bytes
, unsigned int sector_size
)
389 return ROUND_UP(bytes
, sector_size
) / sector_size
;
392 static unsigned int mpb_sectors(struct imsm_super
*mpb
,
393 unsigned int sector_size
)
395 return sector_count(__le32_to_cpu(mpb
->mpb_size
), sector_size
);
399 struct imsm_dev
*dev
;
400 struct intel_dev
*next
;
405 enum sys_dev_type type
;
408 struct intel_hba
*next
;
415 /* internal representation of IMSM metadata */
418 void *buf
; /* O_DIRECT buffer for reading/writing metadata */
419 struct imsm_super
*anchor
; /* immovable parameters */
422 void *migr_rec_buf
; /* buffer for I/O operations */
423 struct migr_record
*migr_rec
; /* migration record */
425 int clean_migration_record_by_mdmon
; /* when reshape is switched to next
426 array, it indicates that mdmon is allowed to clean migration
428 size_t len
; /* size of the 'buf' allocation */
429 size_t extra_space
; /* extra space in 'buf' that is not used yet */
430 void *next_buf
; /* for realloc'ing buf from the manager */
432 int updates_pending
; /* count of pending updates for mdmon */
433 int current_vol
; /* index of raid device undergoing creation */
434 unsigned long long create_offset
; /* common start for 'current_vol' */
435 __u32 random
; /* random data for seeding new family numbers */
436 struct intel_dev
*devlist
;
437 unsigned int sector_size
; /* sector size of used member drives */
441 __u8 serial
[MAX_RAID_SERIAL_LEN
];
444 struct imsm_disk disk
;
447 struct extent
*e
; /* for determining freespace @ create */
448 int raiddisk
; /* slot to fill in autolayout */
450 } *disks
, *current_disk
;
451 struct dl
*disk_mgmt_list
; /* list of disks to add/remove while mdmon
453 struct dl
*missing
; /* disks removed while we weren't looking */
454 struct bbm_log
*bbm_log
;
455 struct intel_hba
*hba
; /* device path of the raid controller for this metadata */
456 const struct imsm_orom
*orom
; /* platform firmware support */
457 struct intel_super
*next
; /* (temp) list for disambiguating family_num */
458 struct md_bb bb
; /* memory for get_bad_blocks call */
462 struct imsm_disk disk
;
463 #define IMSM_UNKNOWN_OWNER (-1)
465 struct intel_disk
*next
;
469 unsigned long long start
, size
;
472 /* definitions of reshape process types */
473 enum imsm_reshape_type
{
479 /* definition of messages passed to imsm_process_update */
480 enum imsm_update_type
{
481 update_activate_spare
,
485 update_add_remove_disk
,
486 update_reshape_container_disks
,
487 update_reshape_migration
,
489 update_general_migration_checkpoint
,
491 update_prealloc_badblocks_mem
,
495 struct imsm_update_activate_spare
{
496 enum imsm_update_type type
;
500 struct imsm_update_activate_spare
*next
;
506 unsigned long long size
;
513 enum takeover_direction
{
517 struct imsm_update_takeover
{
518 enum imsm_update_type type
;
520 enum takeover_direction direction
;
523 struct imsm_update_reshape
{
524 enum imsm_update_type type
;
528 int new_disks
[1]; /* new_raid_disks - old_raid_disks makedev number */
531 struct imsm_update_reshape_migration
{
532 enum imsm_update_type type
;
535 /* fields for array migration changes
542 int new_disks
[1]; /* new_raid_disks - old_raid_disks makedev number */
545 struct imsm_update_size_change
{
546 enum imsm_update_type type
;
551 struct imsm_update_general_migration_checkpoint
{
552 enum imsm_update_type type
;
553 __u32 curr_migr_unit
;
557 __u8 serial
[MAX_RAID_SERIAL_LEN
];
560 struct imsm_update_create_array
{
561 enum imsm_update_type type
;
566 struct imsm_update_kill_array
{
567 enum imsm_update_type type
;
571 struct imsm_update_rename_array
{
572 enum imsm_update_type type
;
573 __u8 name
[MAX_RAID_SERIAL_LEN
];
577 struct imsm_update_add_remove_disk
{
578 enum imsm_update_type type
;
581 struct imsm_update_prealloc_bb_mem
{
582 enum imsm_update_type type
;
585 struct imsm_update_rwh_policy
{
586 enum imsm_update_type type
;
591 static const char *_sys_dev_type
[] = {
592 [SYS_DEV_UNKNOWN
] = "Unknown",
593 [SYS_DEV_SAS
] = "SAS",
594 [SYS_DEV_SATA
] = "SATA",
595 [SYS_DEV_NVME
] = "NVMe",
596 [SYS_DEV_VMD
] = "VMD"
599 const char *get_sys_dev_type(enum sys_dev_type type
)
601 if (type
>= SYS_DEV_MAX
)
602 type
= SYS_DEV_UNKNOWN
;
604 return _sys_dev_type
[type
];
607 static struct intel_hba
* alloc_intel_hba(struct sys_dev
*device
)
609 struct intel_hba
*result
= xmalloc(sizeof(*result
));
611 result
->type
= device
->type
;
612 result
->path
= xstrdup(device
->path
);
614 if (result
->path
&& (result
->pci_id
= strrchr(result
->path
, '/')) != NULL
)
620 static struct intel_hba
* find_intel_hba(struct intel_hba
*hba
, struct sys_dev
*device
)
622 struct intel_hba
*result
;
624 for (result
= hba
; result
; result
= result
->next
) {
625 if (result
->type
== device
->type
&& strcmp(result
->path
, device
->path
) == 0)
631 static int attach_hba_to_super(struct intel_super
*super
, struct sys_dev
*device
)
633 struct intel_hba
*hba
;
635 /* check if disk attached to Intel HBA */
636 hba
= find_intel_hba(super
->hba
, device
);
639 /* Check if HBA is already attached to super */
640 if (super
->hba
== NULL
) {
641 super
->hba
= alloc_intel_hba(device
);
646 /* Intel metadata allows for all disks attached to the same type HBA.
647 * Do not support HBA types mixing
649 if (device
->type
!= hba
->type
)
652 /* Multiple same type HBAs can be used if they share the same OROM */
653 const struct imsm_orom
*device_orom
= get_orom_by_device_id(device
->dev_id
);
655 if (device_orom
!= super
->orom
)
661 hba
->next
= alloc_intel_hba(device
);
665 static struct sys_dev
* find_disk_attached_hba(int fd
, const char *devname
)
667 struct sys_dev
*list
, *elem
;
670 if ((list
= find_intel_devices()) == NULL
)
674 disk_path
= (char *) devname
;
676 disk_path
= diskfd_to_devpath(fd
);
681 for (elem
= list
; elem
; elem
= elem
->next
)
682 if (path_attached_to_hba(disk_path
, elem
->path
))
685 if (disk_path
!= devname
)
691 static int find_intel_hba_capability(int fd
, struct intel_super
*super
,
694 static struct supertype
*match_metadata_desc_imsm(char *arg
)
696 struct supertype
*st
;
698 if (strcmp(arg
, "imsm") != 0 &&
699 strcmp(arg
, "default") != 0
703 st
= xcalloc(1, sizeof(*st
));
704 st
->ss
= &super_imsm
;
705 st
->max_devs
= IMSM_MAX_DEVICES
;
706 st
->minor_version
= 0;
711 static __u8
*get_imsm_version(struct imsm_super
*mpb
)
713 return &mpb
->sig
[MPB_SIG_LEN
];
716 /* retrieve a disk directly from the anchor when the anchor is known to be
717 * up-to-date, currently only at load time
719 static struct imsm_disk
*__get_imsm_disk(struct imsm_super
*mpb
, __u8 index
)
721 if (index
>= mpb
->num_disks
)
723 return &mpb
->disk
[index
];
726 /* retrieve the disk description based on a index of the disk
729 static struct dl
*get_imsm_dl_disk(struct intel_super
*super
, __u8 index
)
733 for (d
= super
->disks
; d
; d
= d
->next
)
734 if (d
->index
== index
)
739 /* retrieve a disk from the parsed metadata */
740 static struct imsm_disk
*get_imsm_disk(struct intel_super
*super
, __u8 index
)
744 dl
= get_imsm_dl_disk(super
, index
);
751 /* generate a checksum directly from the anchor when the anchor is known to be
752 * up-to-date, currently only at load or write_super after coalescing
754 static __u32
__gen_imsm_checksum(struct imsm_super
*mpb
)
756 __u32 end
= mpb
->mpb_size
/ sizeof(end
);
757 __u32
*p
= (__u32
*) mpb
;
761 sum
+= __le32_to_cpu(*p
);
765 return sum
- __le32_to_cpu(mpb
->check_sum
);
768 static size_t sizeof_imsm_map(struct imsm_map
*map
)
770 return sizeof(struct imsm_map
) + sizeof(__u32
) * (map
->num_members
- 1);
773 struct imsm_map
*get_imsm_map(struct imsm_dev
*dev
, int second_map
)
775 /* A device can have 2 maps if it is in the middle of a migration.
777 * MAP_0 - we return the first map
778 * MAP_1 - we return the second map if it exists, else NULL
779 * MAP_X - we return the second map if it exists, else the first
781 struct imsm_map
*map
= &dev
->vol
.map
[0];
782 struct imsm_map
*map2
= NULL
;
784 if (dev
->vol
.migr_state
)
785 map2
= (void *)map
+ sizeof_imsm_map(map
);
787 switch (second_map
) {
804 /* return the size of the device.
805 * migr_state increases the returned size if map[0] were to be duplicated
807 static size_t sizeof_imsm_dev(struct imsm_dev
*dev
, int migr_state
)
809 size_t size
= sizeof(*dev
) - sizeof(struct imsm_map
) +
810 sizeof_imsm_map(get_imsm_map(dev
, MAP_0
));
812 /* migrating means an additional map */
813 if (dev
->vol
.migr_state
)
814 size
+= sizeof_imsm_map(get_imsm_map(dev
, MAP_1
));
816 size
+= sizeof_imsm_map(get_imsm_map(dev
, MAP_0
));
821 /* retrieve disk serial number list from a metadata update */
822 static struct disk_info
*get_disk_info(struct imsm_update_create_array
*update
)
825 struct disk_info
*inf
;
827 inf
= u
+ sizeof(*update
) - sizeof(struct imsm_dev
) +
828 sizeof_imsm_dev(&update
->dev
, 0);
833 static struct imsm_dev
*__get_imsm_dev(struct imsm_super
*mpb
, __u8 index
)
839 if (index
>= mpb
->num_raid_devs
)
842 /* devices start after all disks */
843 offset
= ((void *) &mpb
->disk
[mpb
->num_disks
]) - _mpb
;
845 for (i
= 0; i
<= index
; i
++)
847 return _mpb
+ offset
;
849 offset
+= sizeof_imsm_dev(_mpb
+ offset
, 0);
854 static struct imsm_dev
*get_imsm_dev(struct intel_super
*super
, __u8 index
)
856 struct intel_dev
*dv
;
858 if (index
>= super
->anchor
->num_raid_devs
)
860 for (dv
= super
->devlist
; dv
; dv
= dv
->next
)
861 if (dv
->index
== index
)
866 static inline unsigned long long __le48_to_cpu(const struct bbm_log_block_addr
869 return ((((__u64
)__le32_to_cpu(addr
->dw1
)) << 16) |
870 __le16_to_cpu(addr
->w1
));
873 static inline struct bbm_log_block_addr
__cpu_to_le48(unsigned long long sec
)
875 struct bbm_log_block_addr addr
;
877 addr
.w1
= __cpu_to_le16((__u16
)(sec
& 0xffff));
878 addr
.dw1
= __cpu_to_le32((__u32
)(sec
>> 16) & 0xffffffff);
882 /* get size of the bbm log */
883 static __u32
get_imsm_bbm_log_size(struct bbm_log
*log
)
885 if (!log
|| log
->entry_count
== 0)
888 return sizeof(log
->signature
) +
889 sizeof(log
->entry_count
) +
890 log
->entry_count
* sizeof(struct bbm_log_entry
);
893 /* check if bad block is not partially stored in bbm log */
894 static int is_stored_in_bbm(struct bbm_log
*log
, const __u8 idx
, const unsigned
895 long long sector
, const int length
, __u32
*pos
)
899 for (i
= *pos
; i
< log
->entry_count
; i
++) {
900 struct bbm_log_entry
*entry
= &log
->marked_block_entries
[i
];
901 unsigned long long bb_start
;
902 unsigned long long bb_end
;
904 bb_start
= __le48_to_cpu(&entry
->defective_block_start
);
905 bb_end
= bb_start
+ (entry
->marked_count
+ 1);
907 if ((entry
->disk_ordinal
== idx
) && (bb_start
>= sector
) &&
908 (bb_end
<= sector
+ length
)) {
916 /* record new bad block in bbm log */
917 static int record_new_badblock(struct bbm_log
*log
, const __u8 idx
, unsigned
918 long long sector
, int length
)
922 struct bbm_log_entry
*entry
= NULL
;
924 while (is_stored_in_bbm(log
, idx
, sector
, length
, &pos
)) {
925 struct bbm_log_entry
*e
= &log
->marked_block_entries
[pos
];
927 if ((e
->marked_count
+ 1 == BBM_LOG_MAX_LBA_ENTRY_VAL
) &&
928 (__le48_to_cpu(&e
->defective_block_start
) == sector
)) {
929 sector
+= BBM_LOG_MAX_LBA_ENTRY_VAL
;
930 length
-= BBM_LOG_MAX_LBA_ENTRY_VAL
;
939 int cnt
= (length
<= BBM_LOG_MAX_LBA_ENTRY_VAL
) ? length
:
940 BBM_LOG_MAX_LBA_ENTRY_VAL
;
941 entry
->defective_block_start
= __cpu_to_le48(sector
);
942 entry
->marked_count
= cnt
- 1;
949 new_bb
= ROUND_UP(length
, BBM_LOG_MAX_LBA_ENTRY_VAL
) /
950 BBM_LOG_MAX_LBA_ENTRY_VAL
;
951 if (log
->entry_count
+ new_bb
> BBM_LOG_MAX_ENTRIES
)
955 int cnt
= (length
<= BBM_LOG_MAX_LBA_ENTRY_VAL
) ? length
:
956 BBM_LOG_MAX_LBA_ENTRY_VAL
;
957 struct bbm_log_entry
*entry
=
958 &log
->marked_block_entries
[log
->entry_count
];
960 entry
->defective_block_start
= __cpu_to_le48(sector
);
961 entry
->marked_count
= cnt
- 1;
962 entry
->disk_ordinal
= idx
;
973 /* clear all bad blocks for given disk */
974 static void clear_disk_badblocks(struct bbm_log
*log
, const __u8 idx
)
978 while (i
< log
->entry_count
) {
979 struct bbm_log_entry
*entries
= log
->marked_block_entries
;
981 if (entries
[i
].disk_ordinal
== idx
) {
982 if (i
< log
->entry_count
- 1)
983 entries
[i
] = entries
[log
->entry_count
- 1];
991 /* clear given bad block */
992 static int clear_badblock(struct bbm_log
*log
, const __u8 idx
, const unsigned
993 long long sector
, const int length
) {
996 while (i
< log
->entry_count
) {
997 struct bbm_log_entry
*entries
= log
->marked_block_entries
;
999 if ((entries
[i
].disk_ordinal
== idx
) &&
1000 (__le48_to_cpu(&entries
[i
].defective_block_start
) ==
1001 sector
) && (entries
[i
].marked_count
+ 1 == length
)) {
1002 if (i
< log
->entry_count
- 1)
1003 entries
[i
] = entries
[log
->entry_count
- 1];
1013 /* allocate and load BBM log from metadata */
1014 static int load_bbm_log(struct intel_super
*super
)
1016 struct imsm_super
*mpb
= super
->anchor
;
1017 __u32 bbm_log_size
= __le32_to_cpu(mpb
->bbm_log_size
);
1019 super
->bbm_log
= xcalloc(1, sizeof(struct bbm_log
));
1020 if (!super
->bbm_log
)
1024 struct bbm_log
*log
= (void *)mpb
+
1025 __le32_to_cpu(mpb
->mpb_size
) - bbm_log_size
;
1029 if (bbm_log_size
< sizeof(log
->signature
) +
1030 sizeof(log
->entry_count
))
1033 entry_count
= __le32_to_cpu(log
->entry_count
);
1034 if ((__le32_to_cpu(log
->signature
) != BBM_LOG_SIGNATURE
) ||
1035 (entry_count
> BBM_LOG_MAX_ENTRIES
))
1039 sizeof(log
->signature
) + sizeof(log
->entry_count
) +
1040 entry_count
* sizeof(struct bbm_log_entry
))
1043 memcpy(super
->bbm_log
, log
, bbm_log_size
);
1045 super
->bbm_log
->signature
= __cpu_to_le32(BBM_LOG_SIGNATURE
);
1046 super
->bbm_log
->entry_count
= 0;
1052 /* checks if bad block is within volume boundaries */
1053 static int is_bad_block_in_volume(const struct bbm_log_entry
*entry
,
1054 const unsigned long long start_sector
,
1055 const unsigned long long size
)
1057 unsigned long long bb_start
;
1058 unsigned long long bb_end
;
1060 bb_start
= __le48_to_cpu(&entry
->defective_block_start
);
1061 bb_end
= bb_start
+ (entry
->marked_count
+ 1);
1063 if (((bb_start
>= start_sector
) && (bb_start
< start_sector
+ size
)) ||
1064 ((bb_end
>= start_sector
) && (bb_end
<= start_sector
+ size
)))
1070 /* get list of bad blocks on a drive for a volume */
1071 static void get_volume_badblocks(const struct bbm_log
*log
, const __u8 idx
,
1072 const unsigned long long start_sector
,
1073 const unsigned long long size
,
1079 for (i
= 0; i
< log
->entry_count
; i
++) {
1080 const struct bbm_log_entry
*ent
=
1081 &log
->marked_block_entries
[i
];
1082 struct md_bb_entry
*bb
;
1084 if ((ent
->disk_ordinal
== idx
) &&
1085 is_bad_block_in_volume(ent
, start_sector
, size
)) {
1087 if (!bbs
->entries
) {
1088 bbs
->entries
= xmalloc(BBM_LOG_MAX_ENTRIES
*
1094 bb
= &bbs
->entries
[count
++];
1095 bb
->sector
= __le48_to_cpu(&ent
->defective_block_start
);
1096 bb
->length
= ent
->marked_count
+ 1;
1104 * == MAP_0 get first map
1105 * == MAP_1 get second map
1106 * == MAP_X than get map according to the current migr_state
1108 static __u32
get_imsm_ord_tbl_ent(struct imsm_dev
*dev
,
1112 struct imsm_map
*map
;
1114 map
= get_imsm_map(dev
, second_map
);
1116 /* top byte identifies disk under rebuild */
1117 return __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
1120 #define ord_to_idx(ord) (((ord) << 8) >> 8)
1121 static __u32
get_imsm_disk_idx(struct imsm_dev
*dev
, int slot
, int second_map
)
1123 __u32 ord
= get_imsm_ord_tbl_ent(dev
, slot
, second_map
);
1125 return ord_to_idx(ord
);
1128 static void set_imsm_ord_tbl_ent(struct imsm_map
*map
, int slot
, __u32 ord
)
1130 map
->disk_ord_tbl
[slot
] = __cpu_to_le32(ord
);
1133 static int get_imsm_disk_slot(struct imsm_map
*map
, unsigned idx
)
1138 for (slot
= 0; slot
< map
->num_members
; slot
++) {
1139 ord
= __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
1140 if (ord_to_idx(ord
) == idx
)
1147 static int get_imsm_raid_level(struct imsm_map
*map
)
1149 if (map
->raid_level
== 1) {
1150 if (map
->num_members
== 2)
1156 return map
->raid_level
;
1159 static int cmp_extent(const void *av
, const void *bv
)
1161 const struct extent
*a
= av
;
1162 const struct extent
*b
= bv
;
1163 if (a
->start
< b
->start
)
1165 if (a
->start
> b
->start
)
1170 static int count_memberships(struct dl
*dl
, struct intel_super
*super
)
1172 int memberships
= 0;
1175 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
1176 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
1177 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
1179 if (get_imsm_disk_slot(map
, dl
->index
) >= 0)
1186 static __u32
imsm_min_reserved_sectors(struct intel_super
*super
);
1188 static int split_ull(unsigned long long n
, void *lo
, void *hi
)
1190 if (lo
== 0 || hi
== 0)
1192 __put_unaligned32(__cpu_to_le32((__u32
)n
), lo
);
1193 __put_unaligned32(__cpu_to_le32((n
>> 32)), hi
);
1197 static unsigned long long join_u32(__u32 lo
, __u32 hi
)
1199 return (unsigned long long)__le32_to_cpu(lo
) |
1200 (((unsigned long long)__le32_to_cpu(hi
)) << 32);
1203 static unsigned long long total_blocks(struct imsm_disk
*disk
)
1207 return join_u32(disk
->total_blocks_lo
, disk
->total_blocks_hi
);
1210 static unsigned long long pba_of_lba0(struct imsm_map
*map
)
1214 return join_u32(map
->pba_of_lba0_lo
, map
->pba_of_lba0_hi
);
1217 static unsigned long long blocks_per_member(struct imsm_map
*map
)
1221 return join_u32(map
->blocks_per_member_lo
, map
->blocks_per_member_hi
);
1224 static unsigned long long num_data_stripes(struct imsm_map
*map
)
1228 return join_u32(map
->num_data_stripes_lo
, map
->num_data_stripes_hi
);
1231 static unsigned long long imsm_dev_size(struct imsm_dev
*dev
)
1235 return join_u32(dev
->size_low
, dev
->size_high
);
1238 static unsigned long long migr_chkp_area_pba(struct migr_record
*migr_rec
)
1240 if (migr_rec
== NULL
)
1242 return join_u32(migr_rec
->ckpt_area_pba_lo
,
1243 migr_rec
->ckpt_area_pba_hi
);
1246 static unsigned long long current_migr_unit(struct migr_record
*migr_rec
)
1248 if (migr_rec
== NULL
)
1250 return join_u32(migr_rec
->curr_migr_unit_lo
,
1251 migr_rec
->curr_migr_unit_hi
);
1254 static unsigned long long migr_dest_1st_member_lba(struct migr_record
*migr_rec
)
1256 if (migr_rec
== NULL
)
1258 return join_u32(migr_rec
->dest_1st_member_lba_lo
,
1259 migr_rec
->dest_1st_member_lba_hi
);
1262 static unsigned long long get_num_migr_units(struct migr_record
*migr_rec
)
1264 if (migr_rec
== NULL
)
1266 return join_u32(migr_rec
->num_migr_units_lo
,
1267 migr_rec
->num_migr_units_hi
);
1270 static void set_total_blocks(struct imsm_disk
*disk
, unsigned long long n
)
1272 split_ull(n
, &disk
->total_blocks_lo
, &disk
->total_blocks_hi
);
1275 static void set_pba_of_lba0(struct imsm_map
*map
, unsigned long long n
)
1277 split_ull(n
, &map
->pba_of_lba0_lo
, &map
->pba_of_lba0_hi
);
1280 static void set_blocks_per_member(struct imsm_map
*map
, unsigned long long n
)
1282 split_ull(n
, &map
->blocks_per_member_lo
, &map
->blocks_per_member_hi
);
1285 static void set_num_data_stripes(struct imsm_map
*map
, unsigned long long n
)
1287 split_ull(n
, &map
->num_data_stripes_lo
, &map
->num_data_stripes_hi
);
1290 static void set_imsm_dev_size(struct imsm_dev
*dev
, unsigned long long n
)
1292 split_ull(n
, &dev
->size_low
, &dev
->size_high
);
1295 static void set_migr_chkp_area_pba(struct migr_record
*migr_rec
,
1296 unsigned long long n
)
1298 split_ull(n
, &migr_rec
->ckpt_area_pba_lo
, &migr_rec
->ckpt_area_pba_hi
);
1301 static void set_current_migr_unit(struct migr_record
*migr_rec
,
1302 unsigned long long n
)
1304 split_ull(n
, &migr_rec
->curr_migr_unit_lo
,
1305 &migr_rec
->curr_migr_unit_hi
);
1308 static void set_migr_dest_1st_member_lba(struct migr_record
*migr_rec
,
1309 unsigned long long n
)
1311 split_ull(n
, &migr_rec
->dest_1st_member_lba_lo
,
1312 &migr_rec
->dest_1st_member_lba_hi
);
1315 static void set_num_migr_units(struct migr_record
*migr_rec
,
1316 unsigned long long n
)
1318 split_ull(n
, &migr_rec
->num_migr_units_lo
,
1319 &migr_rec
->num_migr_units_hi
);
1322 static unsigned long long per_dev_array_size(struct imsm_map
*map
)
1324 unsigned long long array_size
= 0;
1329 array_size
= num_data_stripes(map
) * map
->blocks_per_strip
;
1330 if (get_imsm_raid_level(map
) == 1 || get_imsm_raid_level(map
) == 10)
1336 static struct extent
*get_extents(struct intel_super
*super
, struct dl
*dl
,
1337 int get_minimal_reservation
)
1339 /* find a list of used extents on the given physical device */
1340 struct extent
*rv
, *e
;
1342 int memberships
= count_memberships(dl
, super
);
1345 /* trim the reserved area for spares, so they can join any array
1346 * regardless of whether the OROM has assigned sectors from the
1347 * IMSM_RESERVED_SECTORS region
1349 if (dl
->index
== -1 || get_minimal_reservation
)
1350 reservation
= imsm_min_reserved_sectors(super
);
1352 reservation
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
1354 rv
= xcalloc(sizeof(struct extent
), (memberships
+ 1));
1357 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
1358 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
1359 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
1361 if (get_imsm_disk_slot(map
, dl
->index
) >= 0) {
1362 e
->start
= pba_of_lba0(map
);
1363 e
->size
= per_dev_array_size(map
);
1367 qsort(rv
, memberships
, sizeof(*rv
), cmp_extent
);
1369 /* determine the start of the metadata
1370 * when no raid devices are defined use the default
1371 * ...otherwise allow the metadata to truncate the value
1372 * as is the case with older versions of imsm
1375 struct extent
*last
= &rv
[memberships
- 1];
1376 unsigned long long remainder
;
1378 remainder
= total_blocks(&dl
->disk
) - (last
->start
+ last
->size
);
1379 /* round down to 1k block to satisfy precision of the kernel
1383 /* make sure remainder is still sane */
1384 if (remainder
< (unsigned)ROUND_UP(super
->len
, 512) >> 9)
1385 remainder
= ROUND_UP(super
->len
, 512) >> 9;
1386 if (reservation
> remainder
)
1387 reservation
= remainder
;
1389 e
->start
= total_blocks(&dl
->disk
) - reservation
;
1394 /* try to determine how much space is reserved for metadata from
1395 * the last get_extents() entry, otherwise fallback to the
1398 static __u32
imsm_reserved_sectors(struct intel_super
*super
, struct dl
*dl
)
1404 /* for spares just return a minimal reservation which will grow
1405 * once the spare is picked up by an array
1407 if (dl
->index
== -1)
1408 return MPB_SECTOR_CNT
;
1410 e
= get_extents(super
, dl
, 0);
1412 return MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
1414 /* scroll to last entry */
1415 for (i
= 0; e
[i
].size
; i
++)
1418 rv
= total_blocks(&dl
->disk
) - e
[i
].start
;
1425 static int is_spare(struct imsm_disk
*disk
)
1427 return (disk
->status
& SPARE_DISK
) == SPARE_DISK
;
1430 static int is_configured(struct imsm_disk
*disk
)
1432 return (disk
->status
& CONFIGURED_DISK
) == CONFIGURED_DISK
;
1435 static int is_failed(struct imsm_disk
*disk
)
1437 return (disk
->status
& FAILED_DISK
) == FAILED_DISK
;
1440 static int is_journal(struct imsm_disk
*disk
)
1442 return (disk
->status
& JOURNAL_DISK
) == JOURNAL_DISK
;
1445 /* round array size down to closest MB and ensure it splits evenly
1448 static unsigned long long round_size_to_mb(unsigned long long size
, unsigned int
1452 size
= (size
>> SECT_PER_MB_SHIFT
) << SECT_PER_MB_SHIFT
;
1458 static int able_to_resync(int raid_level
, int missing_disks
)
1460 int max_missing_disks
= 0;
1462 switch (raid_level
) {
1464 max_missing_disks
= 1;
1467 max_missing_disks
= 0;
1469 return missing_disks
<= max_missing_disks
;
1472 /* try to determine how much space is reserved for metadata from
1473 * the last get_extents() entry on the smallest active disk,
1474 * otherwise fallback to the default
1476 static __u32
imsm_min_reserved_sectors(struct intel_super
*super
)
1480 unsigned long long min_active
;
1482 __u32 rv
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
1483 struct dl
*dl
, *dl_min
= NULL
;
1489 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
1492 unsigned long long blocks
= total_blocks(&dl
->disk
);
1493 if (blocks
< min_active
|| min_active
== 0) {
1495 min_active
= blocks
;
1501 /* find last lba used by subarrays on the smallest active disk */
1502 e
= get_extents(super
, dl_min
, 0);
1505 for (i
= 0; e
[i
].size
; i
++)
1508 remainder
= min_active
- e
[i
].start
;
1511 /* to give priority to recovery we should not require full
1512 IMSM_RESERVED_SECTORS from the spare */
1513 rv
= MPB_SECTOR_CNT
+ NUM_BLOCKS_DIRTY_STRIPE_REGION
;
1515 /* if real reservation is smaller use that value */
1516 return (remainder
< rv
) ? remainder
: rv
;
1520 * Return minimum size of a spare and sector size
1521 * that can be used in this array
1523 int get_spare_criteria_imsm(struct supertype
*st
, struct spare_criteria
*c
)
1525 struct intel_super
*super
= st
->sb
;
1529 unsigned long long size
= 0;
1536 /* find first active disk in array */
1538 while (dl
&& (is_failed(&dl
->disk
) || dl
->index
== -1))
1542 /* find last lba used by subarrays */
1543 e
= get_extents(super
, dl
, 0);
1546 for (i
= 0; e
[i
].size
; i
++)
1549 size
= e
[i
-1].start
+ e
[i
-1].size
;
1552 /* add the amount of space needed for metadata */
1553 size
+= imsm_min_reserved_sectors(super
);
1555 c
->min_size
= size
* 512;
1556 c
->sector_size
= super
->sector_size
;
1561 static int is_gen_migration(struct imsm_dev
*dev
);
1563 #define IMSM_4K_DIV 8
1565 static __u64
blocks_per_migr_unit(struct intel_super
*super
,
1566 struct imsm_dev
*dev
);
1568 static void print_imsm_dev(struct intel_super
*super
,
1569 struct imsm_dev
*dev
,
1575 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
1576 struct imsm_map
*map2
= get_imsm_map(dev
, MAP_1
);
1580 printf("[%.16s]:\n", dev
->volume
);
1581 printf(" UUID : %s\n", uuid
);
1582 printf(" RAID Level : %d", get_imsm_raid_level(map
));
1584 printf(" <-- %d", get_imsm_raid_level(map2
));
1586 printf(" Members : %d", map
->num_members
);
1588 printf(" <-- %d", map2
->num_members
);
1590 printf(" Slots : [");
1591 for (i
= 0; i
< map
->num_members
; i
++) {
1592 ord
= get_imsm_ord_tbl_ent(dev
, i
, MAP_0
);
1593 printf("%s", ord
& IMSM_ORD_REBUILD
? "_" : "U");
1598 for (i
= 0; i
< map2
->num_members
; i
++) {
1599 ord
= get_imsm_ord_tbl_ent(dev
, i
, MAP_1
);
1600 printf("%s", ord
& IMSM_ORD_REBUILD
? "_" : "U");
1605 printf(" Failed disk : ");
1606 if (map
->failed_disk_num
== 0xff)
1609 printf("%i", map
->failed_disk_num
);
1611 slot
= get_imsm_disk_slot(map
, disk_idx
);
1613 ord
= get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
);
1614 printf(" This Slot : %d%s\n", slot
,
1615 ord
& IMSM_ORD_REBUILD
? " (out-of-sync)" : "");
1617 printf(" This Slot : ?\n");
1618 printf(" Sector Size : %u\n", super
->sector_size
);
1619 sz
= imsm_dev_size(dev
);
1620 printf(" Array Size : %llu%s\n",
1621 (unsigned long long)sz
* 512 / super
->sector_size
,
1622 human_size(sz
* 512));
1623 sz
= blocks_per_member(map
);
1624 printf(" Per Dev Size : %llu%s\n",
1625 (unsigned long long)sz
* 512 / super
->sector_size
,
1626 human_size(sz
* 512));
1627 printf(" Sector Offset : %llu\n",
1629 printf(" Num Stripes : %llu\n",
1630 num_data_stripes(map
));
1631 printf(" Chunk Size : %u KiB",
1632 __le16_to_cpu(map
->blocks_per_strip
) / 2);
1634 printf(" <-- %u KiB",
1635 __le16_to_cpu(map2
->blocks_per_strip
) / 2);
1637 printf(" Reserved : %d\n", __le32_to_cpu(dev
->reserved_blocks
));
1638 printf(" Migrate State : ");
1639 if (dev
->vol
.migr_state
) {
1640 if (migr_type(dev
) == MIGR_INIT
)
1641 printf("initialize\n");
1642 else if (migr_type(dev
) == MIGR_REBUILD
)
1643 printf("rebuild\n");
1644 else if (migr_type(dev
) == MIGR_VERIFY
)
1646 else if (migr_type(dev
) == MIGR_GEN_MIGR
)
1647 printf("general migration\n");
1648 else if (migr_type(dev
) == MIGR_STATE_CHANGE
)
1649 printf("state change\n");
1650 else if (migr_type(dev
) == MIGR_REPAIR
)
1653 printf("<unknown:%d>\n", migr_type(dev
));
1656 printf(" Map State : %s", map_state_str
[map
->map_state
]);
1657 if (dev
->vol
.migr_state
) {
1658 struct imsm_map
*map
= get_imsm_map(dev
, MAP_1
);
1660 printf(" <-- %s", map_state_str
[map
->map_state
]);
1661 printf("\n Checkpoint : %u ",
1662 __le32_to_cpu(dev
->vol
.curr_migr_unit
));
1663 if (is_gen_migration(dev
) && (slot
> 1 || slot
< 0))
1666 printf("(%llu)", (unsigned long long)
1667 blocks_per_migr_unit(super
, dev
));
1670 printf(" Dirty State : %s\n", (dev
->vol
.dirty
& RAIDVOL_DIRTY
) ?
1672 printf(" RWH Policy : ");
1673 if (dev
->rwh_policy
== RWH_OFF
|| dev
->rwh_policy
== RWH_MULTIPLE_OFF
)
1675 else if (dev
->rwh_policy
== RWH_DISTRIBUTED
)
1676 printf("PPL distributed\n");
1677 else if (dev
->rwh_policy
== RWH_JOURNALING_DRIVE
)
1678 printf("PPL journaling drive\n");
1679 else if (dev
->rwh_policy
== RWH_MULTIPLE_DISTRIBUTED
)
1680 printf("Multiple distributed PPLs\n");
1681 else if (dev
->rwh_policy
== RWH_MULTIPLE_PPLS_JOURNALING_DRIVE
)
1682 printf("Multiple PPLs on journaling drive\n");
1684 printf("<unknown:%d>\n", dev
->rwh_policy
);
1687 static void print_imsm_disk(struct imsm_disk
*disk
,
1690 unsigned int sector_size
) {
1691 char str
[MAX_RAID_SERIAL_LEN
+ 1];
1694 if (index
< -1 || !disk
)
1698 snprintf(str
, MAX_RAID_SERIAL_LEN
+ 1, "%s", disk
->serial
);
1700 printf(" Disk%02d Serial : %s\n", index
, str
);
1702 printf(" Disk Serial : %s\n", str
);
1703 printf(" State :%s%s%s%s\n", is_spare(disk
) ? " spare" : "",
1704 is_configured(disk
) ? " active" : "",
1705 is_failed(disk
) ? " failed" : "",
1706 is_journal(disk
) ? " journal" : "");
1707 printf(" Id : %08x\n", __le32_to_cpu(disk
->scsi_id
));
1708 sz
= total_blocks(disk
) - reserved
;
1709 printf(" Usable Size : %llu%s\n",
1710 (unsigned long long)sz
* 512 / sector_size
,
1711 human_size(sz
* 512));
1714 void convert_to_4k_imsm_migr_rec(struct intel_super
*super
)
1716 struct migr_record
*migr_rec
= super
->migr_rec
;
1718 migr_rec
->blocks_per_unit
/= IMSM_4K_DIV
;
1719 migr_rec
->dest_depth_per_unit
/= IMSM_4K_DIV
;
1720 split_ull((join_u32(migr_rec
->post_migr_vol_cap
,
1721 migr_rec
->post_migr_vol_cap_hi
) / IMSM_4K_DIV
),
1722 &migr_rec
->post_migr_vol_cap
, &migr_rec
->post_migr_vol_cap_hi
);
1723 set_migr_chkp_area_pba(migr_rec
,
1724 migr_chkp_area_pba(migr_rec
) / IMSM_4K_DIV
);
1725 set_migr_dest_1st_member_lba(migr_rec
,
1726 migr_dest_1st_member_lba(migr_rec
) / IMSM_4K_DIV
);
1729 void convert_to_4k_imsm_disk(struct imsm_disk
*disk
)
1731 set_total_blocks(disk
, (total_blocks(disk
)/IMSM_4K_DIV
));
1734 void convert_to_4k(struct intel_super
*super
)
1736 struct imsm_super
*mpb
= super
->anchor
;
1737 struct imsm_disk
*disk
;
1739 __u32 bbm_log_size
= __le32_to_cpu(mpb
->bbm_log_size
);
1741 for (i
= 0; i
< mpb
->num_disks
; i
++) {
1742 disk
= __get_imsm_disk(mpb
, i
);
1744 convert_to_4k_imsm_disk(disk
);
1746 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
1747 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
1748 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
1750 set_imsm_dev_size(dev
, imsm_dev_size(dev
)/IMSM_4K_DIV
);
1751 dev
->vol
.curr_migr_unit
/= IMSM_4K_DIV
;
1754 set_blocks_per_member(map
, blocks_per_member(map
)/IMSM_4K_DIV
);
1755 map
->blocks_per_strip
/= IMSM_4K_DIV
;
1756 set_pba_of_lba0(map
, pba_of_lba0(map
)/IMSM_4K_DIV
);
1758 if (dev
->vol
.migr_state
) {
1760 map
= get_imsm_map(dev
, MAP_1
);
1761 set_blocks_per_member(map
,
1762 blocks_per_member(map
)/IMSM_4K_DIV
);
1763 map
->blocks_per_strip
/= IMSM_4K_DIV
;
1764 set_pba_of_lba0(map
, pba_of_lba0(map
)/IMSM_4K_DIV
);
1768 struct bbm_log
*log
= (void *)mpb
+
1769 __le32_to_cpu(mpb
->mpb_size
) - bbm_log_size
;
1772 for (i
= 0; i
< log
->entry_count
; i
++) {
1773 struct bbm_log_entry
*entry
=
1774 &log
->marked_block_entries
[i
];
1776 __u8 count
= entry
->marked_count
+ 1;
1777 unsigned long long sector
=
1778 __le48_to_cpu(&entry
->defective_block_start
);
1780 entry
->defective_block_start
=
1781 __cpu_to_le48(sector
/IMSM_4K_DIV
);
1782 entry
->marked_count
= max(count
/IMSM_4K_DIV
, 1) - 1;
1786 mpb
->check_sum
= __gen_imsm_checksum(mpb
);
1789 void examine_migr_rec_imsm(struct intel_super
*super
)
1791 struct migr_record
*migr_rec
= super
->migr_rec
;
1792 struct imsm_super
*mpb
= super
->anchor
;
1795 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
1796 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
1797 struct imsm_map
*map
;
1800 if (is_gen_migration(dev
) == 0)
1803 printf("\nMigration Record Information:");
1805 /* first map under migration */
1806 map
= get_imsm_map(dev
, MAP_0
);
1808 slot
= get_imsm_disk_slot(map
, super
->disks
->index
);
1809 if (map
== NULL
|| slot
> 1 || slot
< 0) {
1810 printf(" Empty\n ");
1811 printf("Examine one of first two disks in array\n");
1814 printf("\n Status : ");
1815 if (__le32_to_cpu(migr_rec
->rec_status
) == UNIT_SRC_NORMAL
)
1818 printf("Contains Data\n");
1819 printf(" Current Unit : %llu\n",
1820 current_migr_unit(migr_rec
));
1821 printf(" Family : %u\n",
1822 __le32_to_cpu(migr_rec
->family_num
));
1823 printf(" Ascending : %u\n",
1824 __le32_to_cpu(migr_rec
->ascending_migr
));
1825 printf(" Blocks Per Unit : %u\n",
1826 __le32_to_cpu(migr_rec
->blocks_per_unit
));
1827 printf(" Dest. Depth Per Unit : %u\n",
1828 __le32_to_cpu(migr_rec
->dest_depth_per_unit
));
1829 printf(" Checkpoint Area pba : %llu\n",
1830 migr_chkp_area_pba(migr_rec
));
1831 printf(" First member lba : %llu\n",
1832 migr_dest_1st_member_lba(migr_rec
));
1833 printf(" Total Number of Units : %llu\n",
1834 get_num_migr_units(migr_rec
));
1835 printf(" Size of volume : %llu\n",
1836 join_u32(migr_rec
->post_migr_vol_cap
,
1837 migr_rec
->post_migr_vol_cap_hi
));
1838 printf(" Record was read from : %u\n",
1839 __le32_to_cpu(migr_rec
->ckpt_read_disk_num
));
1845 void convert_from_4k_imsm_migr_rec(struct intel_super
*super
)
1847 struct migr_record
*migr_rec
= super
->migr_rec
;
1849 migr_rec
->blocks_per_unit
*= IMSM_4K_DIV
;
1850 migr_rec
->dest_depth_per_unit
*= IMSM_4K_DIV
;
1851 split_ull((join_u32(migr_rec
->post_migr_vol_cap
,
1852 migr_rec
->post_migr_vol_cap_hi
) * IMSM_4K_DIV
),
1853 &migr_rec
->post_migr_vol_cap
,
1854 &migr_rec
->post_migr_vol_cap_hi
);
1855 set_migr_chkp_area_pba(migr_rec
,
1856 migr_chkp_area_pba(migr_rec
) * IMSM_4K_DIV
);
1857 set_migr_dest_1st_member_lba(migr_rec
,
1858 migr_dest_1st_member_lba(migr_rec
) * IMSM_4K_DIV
);
1861 void convert_from_4k(struct intel_super
*super
)
1863 struct imsm_super
*mpb
= super
->anchor
;
1864 struct imsm_disk
*disk
;
1866 __u32 bbm_log_size
= __le32_to_cpu(mpb
->bbm_log_size
);
1868 for (i
= 0; i
< mpb
->num_disks
; i
++) {
1869 disk
= __get_imsm_disk(mpb
, i
);
1871 set_total_blocks(disk
, (total_blocks(disk
)*IMSM_4K_DIV
));
1874 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
1875 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
1876 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
1878 set_imsm_dev_size(dev
, imsm_dev_size(dev
)*IMSM_4K_DIV
);
1879 dev
->vol
.curr_migr_unit
*= IMSM_4K_DIV
;
1882 set_blocks_per_member(map
, blocks_per_member(map
)*IMSM_4K_DIV
);
1883 map
->blocks_per_strip
*= IMSM_4K_DIV
;
1884 set_pba_of_lba0(map
, pba_of_lba0(map
)*IMSM_4K_DIV
);
1886 if (dev
->vol
.migr_state
) {
1888 map
= get_imsm_map(dev
, MAP_1
);
1889 set_blocks_per_member(map
,
1890 blocks_per_member(map
)*IMSM_4K_DIV
);
1891 map
->blocks_per_strip
*= IMSM_4K_DIV
;
1892 set_pba_of_lba0(map
, pba_of_lba0(map
)*IMSM_4K_DIV
);
1896 struct bbm_log
*log
= (void *)mpb
+
1897 __le32_to_cpu(mpb
->mpb_size
) - bbm_log_size
;
1900 for (i
= 0; i
< log
->entry_count
; i
++) {
1901 struct bbm_log_entry
*entry
=
1902 &log
->marked_block_entries
[i
];
1904 __u8 count
= entry
->marked_count
+ 1;
1905 unsigned long long sector
=
1906 __le48_to_cpu(&entry
->defective_block_start
);
1908 entry
->defective_block_start
=
1909 __cpu_to_le48(sector
*IMSM_4K_DIV
);
1910 entry
->marked_count
= count
*IMSM_4K_DIV
- 1;
1914 mpb
->check_sum
= __gen_imsm_checksum(mpb
);
1917 /*******************************************************************************
1918 * function: imsm_check_attributes
1919 * Description: Function checks if features represented by attributes flags
1920 * are supported by mdadm.
1922 * attributes - Attributes read from metadata
1924 * 0 - passed attributes contains unsupported features flags
1925 * 1 - all features are supported
1926 ******************************************************************************/
1927 static int imsm_check_attributes(__u32 attributes
)
1930 __u32 not_supported
= MPB_ATTRIB_SUPPORTED
^0xffffffff;
1932 not_supported
&= ~MPB_ATTRIB_IGNORED
;
1934 not_supported
&= attributes
;
1935 if (not_supported
) {
1936 pr_err("(IMSM): Unsupported attributes : %x\n",
1937 (unsigned)__le32_to_cpu(not_supported
));
1938 if (not_supported
& MPB_ATTRIB_CHECKSUM_VERIFY
) {
1939 dprintf("\t\tMPB_ATTRIB_CHECKSUM_VERIFY \n");
1940 not_supported
^= MPB_ATTRIB_CHECKSUM_VERIFY
;
1942 if (not_supported
& MPB_ATTRIB_2TB
) {
1943 dprintf("\t\tMPB_ATTRIB_2TB\n");
1944 not_supported
^= MPB_ATTRIB_2TB
;
1946 if (not_supported
& MPB_ATTRIB_RAID0
) {
1947 dprintf("\t\tMPB_ATTRIB_RAID0\n");
1948 not_supported
^= MPB_ATTRIB_RAID0
;
1950 if (not_supported
& MPB_ATTRIB_RAID1
) {
1951 dprintf("\t\tMPB_ATTRIB_RAID1\n");
1952 not_supported
^= MPB_ATTRIB_RAID1
;
1954 if (not_supported
& MPB_ATTRIB_RAID10
) {
1955 dprintf("\t\tMPB_ATTRIB_RAID10\n");
1956 not_supported
^= MPB_ATTRIB_RAID10
;
1958 if (not_supported
& MPB_ATTRIB_RAID1E
) {
1959 dprintf("\t\tMPB_ATTRIB_RAID1E\n");
1960 not_supported
^= MPB_ATTRIB_RAID1E
;
1962 if (not_supported
& MPB_ATTRIB_RAID5
) {
1963 dprintf("\t\tMPB_ATTRIB_RAID5\n");
1964 not_supported
^= MPB_ATTRIB_RAID5
;
1966 if (not_supported
& MPB_ATTRIB_RAIDCNG
) {
1967 dprintf("\t\tMPB_ATTRIB_RAIDCNG\n");
1968 not_supported
^= MPB_ATTRIB_RAIDCNG
;
1970 if (not_supported
& MPB_ATTRIB_BBM
) {
1971 dprintf("\t\tMPB_ATTRIB_BBM\n");
1972 not_supported
^= MPB_ATTRIB_BBM
;
1974 if (not_supported
& MPB_ATTRIB_CHECKSUM_VERIFY
) {
1975 dprintf("\t\tMPB_ATTRIB_CHECKSUM_VERIFY (== MPB_ATTRIB_LEGACY)\n");
1976 not_supported
^= MPB_ATTRIB_CHECKSUM_VERIFY
;
1978 if (not_supported
& MPB_ATTRIB_EXP_STRIPE_SIZE
) {
1979 dprintf("\t\tMPB_ATTRIB_EXP_STRIP_SIZE\n");
1980 not_supported
^= MPB_ATTRIB_EXP_STRIPE_SIZE
;
1982 if (not_supported
& MPB_ATTRIB_2TB_DISK
) {
1983 dprintf("\t\tMPB_ATTRIB_2TB_DISK\n");
1984 not_supported
^= MPB_ATTRIB_2TB_DISK
;
1986 if (not_supported
& MPB_ATTRIB_NEVER_USE2
) {
1987 dprintf("\t\tMPB_ATTRIB_NEVER_USE2\n");
1988 not_supported
^= MPB_ATTRIB_NEVER_USE2
;
1990 if (not_supported
& MPB_ATTRIB_NEVER_USE
) {
1991 dprintf("\t\tMPB_ATTRIB_NEVER_USE\n");
1992 not_supported
^= MPB_ATTRIB_NEVER_USE
;
1996 dprintf("(IMSM): Unknown attributes : %x\n", not_supported
);
2004 static void getinfo_super_imsm(struct supertype
*st
, struct mdinfo
*info
, char *map
);
2006 static void examine_super_imsm(struct supertype
*st
, char *homehost
)
2008 struct intel_super
*super
= st
->sb
;
2009 struct imsm_super
*mpb
= super
->anchor
;
2010 char str
[MAX_SIGNATURE_LENGTH
];
2015 __u32 reserved
= imsm_reserved_sectors(super
, super
->disks
);
2018 strncpy(str
, (char *)mpb
->sig
, MPB_SIG_LEN
);
2019 str
[MPB_SIG_LEN
-1] = '\0';
2020 printf(" Magic : %s\n", str
);
2021 printf(" Version : %s\n", get_imsm_version(mpb
));
2022 printf(" Orig Family : %08x\n", __le32_to_cpu(mpb
->orig_family_num
));
2023 printf(" Family : %08x\n", __le32_to_cpu(mpb
->family_num
));
2024 printf(" Generation : %08x\n", __le32_to_cpu(mpb
->generation_num
));
2025 printf(" Attributes : ");
2026 if (imsm_check_attributes(mpb
->attributes
))
2027 printf("All supported\n");
2029 printf("not supported\n");
2030 getinfo_super_imsm(st
, &info
, NULL
);
2031 fname_from_uuid(st
, &info
, nbuf
, ':');
2032 printf(" UUID : %s\n", nbuf
+ 5);
2033 sum
= __le32_to_cpu(mpb
->check_sum
);
2034 printf(" Checksum : %08x %s\n", sum
,
2035 __gen_imsm_checksum(mpb
) == sum
? "correct" : "incorrect");
2036 printf(" MPB Sectors : %d\n", mpb_sectors(mpb
, super
->sector_size
));
2037 printf(" Disks : %d\n", mpb
->num_disks
);
2038 printf(" RAID Devices : %d\n", mpb
->num_raid_devs
);
2039 print_imsm_disk(__get_imsm_disk(mpb
, super
->disks
->index
),
2040 super
->disks
->index
, reserved
, super
->sector_size
);
2041 if (get_imsm_bbm_log_size(super
->bbm_log
)) {
2042 struct bbm_log
*log
= super
->bbm_log
;
2045 printf("Bad Block Management Log:\n");
2046 printf(" Log Size : %d\n", __le32_to_cpu(mpb
->bbm_log_size
));
2047 printf(" Signature : %x\n", __le32_to_cpu(log
->signature
));
2048 printf(" Entry Count : %d\n", __le32_to_cpu(log
->entry_count
));
2050 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
2052 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
2054 super
->current_vol
= i
;
2055 getinfo_super_imsm(st
, &info
, NULL
);
2056 fname_from_uuid(st
, &info
, nbuf
, ':');
2057 print_imsm_dev(super
, dev
, nbuf
+ 5, super
->disks
->index
);
2059 for (i
= 0; i
< mpb
->num_disks
; i
++) {
2060 if (i
== super
->disks
->index
)
2062 print_imsm_disk(__get_imsm_disk(mpb
, i
), i
, reserved
,
2063 super
->sector_size
);
2066 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
2067 if (dl
->index
== -1)
2068 print_imsm_disk(&dl
->disk
, -1, reserved
,
2069 super
->sector_size
);
2071 examine_migr_rec_imsm(super
);
2074 static void brief_examine_super_imsm(struct supertype
*st
, int verbose
)
2076 /* We just write a generic IMSM ARRAY entry */
2079 struct intel_super
*super
= st
->sb
;
2081 if (!super
->anchor
->num_raid_devs
) {
2082 printf("ARRAY metadata=imsm\n");
2086 getinfo_super_imsm(st
, &info
, NULL
);
2087 fname_from_uuid(st
, &info
, nbuf
, ':');
2088 printf("ARRAY metadata=imsm UUID=%s\n", nbuf
+ 5);
2091 static void brief_examine_subarrays_imsm(struct supertype
*st
, int verbose
)
2093 /* We just write a generic IMSM ARRAY entry */
2097 struct intel_super
*super
= st
->sb
;
2100 if (!super
->anchor
->num_raid_devs
)
2103 getinfo_super_imsm(st
, &info
, NULL
);
2104 fname_from_uuid(st
, &info
, nbuf
, ':');
2105 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
2106 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
2108 super
->current_vol
= i
;
2109 getinfo_super_imsm(st
, &info
, NULL
);
2110 fname_from_uuid(st
, &info
, nbuf1
, ':');
2111 printf("ARRAY /dev/md/%.16s container=%s member=%d UUID=%s\n",
2112 dev
->volume
, nbuf
+ 5, i
, nbuf1
+ 5);
2116 static void export_examine_super_imsm(struct supertype
*st
)
2118 struct intel_super
*super
= st
->sb
;
2119 struct imsm_super
*mpb
= super
->anchor
;
2123 getinfo_super_imsm(st
, &info
, NULL
);
2124 fname_from_uuid(st
, &info
, nbuf
, ':');
2125 printf("MD_METADATA=imsm\n");
2126 printf("MD_LEVEL=container\n");
2127 printf("MD_UUID=%s\n", nbuf
+5);
2128 printf("MD_DEVICES=%u\n", mpb
->num_disks
);
2131 static int copy_metadata_imsm(struct supertype
*st
, int from
, int to
)
2133 /* The second last sector of the device contains
2134 * the "struct imsm_super" metadata.
2135 * This contains mpb_size which is the size in bytes of the
2136 * extended metadata. This is located immediately before
2138 * We want to read all that, plus the last sector which
2139 * may contain a migration record, and write it all
2143 unsigned long long dsize
, offset
;
2145 struct imsm_super
*sb
;
2146 struct intel_super
*super
= st
->sb
;
2147 unsigned int sector_size
= super
->sector_size
;
2148 unsigned int written
= 0;
2150 if (posix_memalign(&buf
, MAX_SECTOR_SIZE
, MAX_SECTOR_SIZE
) != 0)
2153 if (!get_dev_size(from
, NULL
, &dsize
))
2156 if (lseek64(from
, dsize
-(2*sector_size
), 0) < 0)
2158 if ((unsigned int)read(from
, buf
, sector_size
) != sector_size
)
2161 if (strncmp((char*)sb
->sig
, MPB_SIGNATURE
, MPB_SIG_LEN
) != 0)
2164 sectors
= mpb_sectors(sb
, sector_size
) + 2;
2165 offset
= dsize
- sectors
* sector_size
;
2166 if (lseek64(from
, offset
, 0) < 0 ||
2167 lseek64(to
, offset
, 0) < 0)
2169 while (written
< sectors
* sector_size
) {
2170 int n
= sectors
*sector_size
- written
;
2173 if (read(from
, buf
, n
) != n
)
2175 if (write(to
, buf
, n
) != n
)
2186 static void detail_super_imsm(struct supertype
*st
, char *homehost
,
2191 struct intel_super
*super
= st
->sb
;
2192 int temp_vol
= super
->current_vol
;
2195 super
->current_vol
= strtoul(subarray
, NULL
, 10);
2197 getinfo_super_imsm(st
, &info
, NULL
);
2198 fname_from_uuid(st
, &info
, nbuf
, ':');
2199 printf("\n UUID : %s\n", nbuf
+ 5);
2201 super
->current_vol
= temp_vol
;
2204 static void brief_detail_super_imsm(struct supertype
*st
, char *subarray
)
2208 struct intel_super
*super
= st
->sb
;
2209 int temp_vol
= super
->current_vol
;
2212 super
->current_vol
= strtoul(subarray
, NULL
, 10);
2214 getinfo_super_imsm(st
, &info
, NULL
);
2215 fname_from_uuid(st
, &info
, nbuf
, ':');
2216 printf(" UUID=%s", nbuf
+ 5);
2218 super
->current_vol
= temp_vol
;
2221 static int imsm_read_serial(int fd
, char *devname
, __u8
*serial
);
2222 static void fd2devname(int fd
, char *name
);
2224 static int ahci_enumerate_ports(const char *hba_path
, int port_count
, int host_base
, int verbose
)
2226 /* dump an unsorted list of devices attached to AHCI Intel storage
2227 * controller, as well as non-connected ports
2229 int hba_len
= strlen(hba_path
) + 1;
2234 unsigned long port_mask
= (1 << port_count
) - 1;
2236 if (port_count
> (int)sizeof(port_mask
) * 8) {
2238 pr_err("port_count %d out of range\n", port_count
);
2242 /* scroll through /sys/dev/block looking for devices attached to
2245 dir
= opendir("/sys/dev/block");
2249 for (ent
= readdir(dir
); ent
; ent
= readdir(dir
)) {
2260 if (sscanf(ent
->d_name
, "%d:%d", &major
, &minor
) != 2)
2262 path
= devt_to_devpath(makedev(major
, minor
));
2265 if (!path_attached_to_hba(path
, hba_path
)) {
2271 /* retrieve the scsi device type */
2272 if (asprintf(&device
, "/sys/dev/block/%d:%d/device/xxxxxxx", major
, minor
) < 0) {
2274 pr_err("failed to allocate 'device'\n");
2278 sprintf(device
, "/sys/dev/block/%d:%d/device/type", major
, minor
);
2279 if (load_sys(device
, buf
, sizeof(buf
)) != 0) {
2281 pr_err("failed to read device type for %s\n",
2287 type
= strtoul(buf
, NULL
, 10);
2289 /* if it's not a disk print the vendor and model */
2290 if (!(type
== 0 || type
== 7 || type
== 14)) {
2293 sprintf(device
, "/sys/dev/block/%d:%d/device/vendor", major
, minor
);
2294 if (load_sys(device
, buf
, sizeof(buf
)) == 0) {
2295 strncpy(vendor
, buf
, sizeof(vendor
));
2296 vendor
[sizeof(vendor
) - 1] = '\0';
2297 c
= (char *) &vendor
[sizeof(vendor
) - 1];
2298 while (isspace(*c
) || *c
== '\0')
2302 sprintf(device
, "/sys/dev/block/%d:%d/device/model", major
, minor
);
2303 if (load_sys(device
, buf
, sizeof(buf
)) == 0) {
2304 strncpy(model
, buf
, sizeof(model
));
2305 model
[sizeof(model
) - 1] = '\0';
2306 c
= (char *) &model
[sizeof(model
) - 1];
2307 while (isspace(*c
) || *c
== '\0')
2311 if (vendor
[0] && model
[0])
2312 sprintf(buf
, "%.64s %.64s", vendor
, model
);
2314 switch (type
) { /* numbers from hald/linux/device.c */
2315 case 1: sprintf(buf
, "tape"); break;
2316 case 2: sprintf(buf
, "printer"); break;
2317 case 3: sprintf(buf
, "processor"); break;
2319 case 5: sprintf(buf
, "cdrom"); break;
2320 case 6: sprintf(buf
, "scanner"); break;
2321 case 8: sprintf(buf
, "media_changer"); break;
2322 case 9: sprintf(buf
, "comm"); break;
2323 case 12: sprintf(buf
, "raid"); break;
2324 default: sprintf(buf
, "unknown");
2330 /* chop device path to 'host%d' and calculate the port number */
2331 c
= strchr(&path
[hba_len
], '/');
2334 pr_err("%s - invalid path name\n", path
+ hba_len
);
2339 if ((sscanf(&path
[hba_len
], "ata%d", &port
) == 1) ||
2340 ((sscanf(&path
[hba_len
], "host%d", &port
) == 1)))
2344 *c
= '/'; /* repair the full string */
2345 pr_err("failed to determine port number for %s\n",
2352 /* mark this port as used */
2353 port_mask
&= ~(1 << port
);
2355 /* print out the device information */
2357 printf(" Port%d : - non-disk device (%s) -\n", port
, buf
);
2361 fd
= dev_open(ent
->d_name
, O_RDONLY
);
2363 printf(" Port%d : - disk info unavailable -\n", port
);
2365 fd2devname(fd
, buf
);
2366 printf(" Port%d : %s", port
, buf
);
2367 if (imsm_read_serial(fd
, NULL
, (__u8
*) buf
) == 0)
2368 printf(" (%.*s)\n", MAX_RAID_SERIAL_LEN
, buf
);
2383 for (i
= 0; i
< port_count
; i
++)
2384 if (port_mask
& (1 << i
))
2385 printf(" Port%d : - no device attached -\n", i
);
2391 static int print_vmd_attached_devs(struct sys_dev
*hba
)
2399 if (hba
->type
!= SYS_DEV_VMD
)
2402 /* scroll through /sys/dev/block looking for devices attached to
2405 dir
= opendir("/sys/bus/pci/drivers/nvme");
2409 for (ent
= readdir(dir
); ent
; ent
= readdir(dir
)) {
2412 /* is 'ent' a device? check that the 'subsystem' link exists and
2413 * that its target matches 'bus'
2415 sprintf(path
, "/sys/bus/pci/drivers/nvme/%s/subsystem",
2417 n
= readlink(path
, link
, sizeof(link
));
2418 if (n
< 0 || n
>= (int)sizeof(link
))
2421 c
= strrchr(link
, '/');
2424 if (strncmp("pci", c
+1, strlen("pci")) != 0)
2427 sprintf(path
, "/sys/bus/pci/drivers/nvme/%s", ent
->d_name
);
2429 rp
= realpath(path
, NULL
);
2433 if (path_attached_to_hba(rp
, hba
->path
)) {
2434 printf(" NVMe under VMD : %s\n", rp
);
2443 static void print_found_intel_controllers(struct sys_dev
*elem
)
2445 for (; elem
; elem
= elem
->next
) {
2446 pr_err("found Intel(R) ");
2447 if (elem
->type
== SYS_DEV_SATA
)
2448 fprintf(stderr
, "SATA ");
2449 else if (elem
->type
== SYS_DEV_SAS
)
2450 fprintf(stderr
, "SAS ");
2451 else if (elem
->type
== SYS_DEV_NVME
)
2452 fprintf(stderr
, "NVMe ");
2454 if (elem
->type
== SYS_DEV_VMD
)
2455 fprintf(stderr
, "VMD domain");
2457 fprintf(stderr
, "RAID controller");
2460 fprintf(stderr
, " at %s", elem
->pci_id
);
2461 fprintf(stderr
, ".\n");
2466 static int ahci_get_port_count(const char *hba_path
, int *port_count
)
2473 if ((dir
= opendir(hba_path
)) == NULL
)
2476 for (ent
= readdir(dir
); ent
; ent
= readdir(dir
)) {
2479 if ((sscanf(ent
->d_name
, "ata%d", &host
) != 1) &&
2480 ((sscanf(ent
->d_name
, "host%d", &host
) != 1)))
2482 if (*port_count
== 0)
2484 else if (host
< host_base
)
2487 if (host
+ 1 > *port_count
+ host_base
)
2488 *port_count
= host
+ 1 - host_base
;
2494 static void print_imsm_capability(const struct imsm_orom
*orom
)
2496 printf(" Platform : Intel(R) ");
2497 if (orom
->capabilities
== 0 && orom
->driver_features
== 0)
2498 printf("Matrix Storage Manager\n");
2499 else if (imsm_orom_is_enterprise(orom
) && orom
->major_ver
>= 6)
2500 printf("Virtual RAID on CPU\n");
2502 printf("Rapid Storage Technology%s\n",
2503 imsm_orom_is_enterprise(orom
) ? " enterprise" : "");
2504 if (orom
->major_ver
|| orom
->minor_ver
|| orom
->hotfix_ver
|| orom
->build
)
2505 printf(" Version : %d.%d.%d.%d\n", orom
->major_ver
,
2506 orom
->minor_ver
, orom
->hotfix_ver
, orom
->build
);
2507 printf(" RAID Levels :%s%s%s%s%s\n",
2508 imsm_orom_has_raid0(orom
) ? " raid0" : "",
2509 imsm_orom_has_raid1(orom
) ? " raid1" : "",
2510 imsm_orom_has_raid1e(orom
) ? " raid1e" : "",
2511 imsm_orom_has_raid10(orom
) ? " raid10" : "",
2512 imsm_orom_has_raid5(orom
) ? " raid5" : "");
2513 printf(" Chunk Sizes :%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
2514 imsm_orom_has_chunk(orom
, 2) ? " 2k" : "",
2515 imsm_orom_has_chunk(orom
, 4) ? " 4k" : "",
2516 imsm_orom_has_chunk(orom
, 8) ? " 8k" : "",
2517 imsm_orom_has_chunk(orom
, 16) ? " 16k" : "",
2518 imsm_orom_has_chunk(orom
, 32) ? " 32k" : "",
2519 imsm_orom_has_chunk(orom
, 64) ? " 64k" : "",
2520 imsm_orom_has_chunk(orom
, 128) ? " 128k" : "",
2521 imsm_orom_has_chunk(orom
, 256) ? " 256k" : "",
2522 imsm_orom_has_chunk(orom
, 512) ? " 512k" : "",
2523 imsm_orom_has_chunk(orom
, 1024*1) ? " 1M" : "",
2524 imsm_orom_has_chunk(orom
, 1024*2) ? " 2M" : "",
2525 imsm_orom_has_chunk(orom
, 1024*4) ? " 4M" : "",
2526 imsm_orom_has_chunk(orom
, 1024*8) ? " 8M" : "",
2527 imsm_orom_has_chunk(orom
, 1024*16) ? " 16M" : "",
2528 imsm_orom_has_chunk(orom
, 1024*32) ? " 32M" : "",
2529 imsm_orom_has_chunk(orom
, 1024*64) ? " 64M" : "");
2530 printf(" 2TB volumes :%s supported\n",
2531 (orom
->attr
& IMSM_OROM_ATTR_2TB
)?"":" not");
2532 printf(" 2TB disks :%s supported\n",
2533 (orom
->attr
& IMSM_OROM_ATTR_2TB_DISK
)?"":" not");
2534 printf(" Max Disks : %d\n", orom
->tds
);
2535 printf(" Max Volumes : %d per array, %d per %s\n",
2536 orom
->vpa
, orom
->vphba
,
2537 imsm_orom_is_nvme(orom
) ? "platform" : "controller");
2541 static void print_imsm_capability_export(const struct imsm_orom
*orom
)
2543 printf("MD_FIRMWARE_TYPE=imsm\n");
2544 if (orom
->major_ver
|| orom
->minor_ver
|| orom
->hotfix_ver
|| orom
->build
)
2545 printf("IMSM_VERSION=%d.%d.%d.%d\n", orom
->major_ver
, orom
->minor_ver
,
2546 orom
->hotfix_ver
, orom
->build
);
2547 printf("IMSM_SUPPORTED_RAID_LEVELS=%s%s%s%s%s\n",
2548 imsm_orom_has_raid0(orom
) ? "raid0 " : "",
2549 imsm_orom_has_raid1(orom
) ? "raid1 " : "",
2550 imsm_orom_has_raid1e(orom
) ? "raid1e " : "",
2551 imsm_orom_has_raid5(orom
) ? "raid10 " : "",
2552 imsm_orom_has_raid10(orom
) ? "raid5 " : "");
2553 printf("IMSM_SUPPORTED_CHUNK_SIZES=%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
2554 imsm_orom_has_chunk(orom
, 2) ? "2k " : "",
2555 imsm_orom_has_chunk(orom
, 4) ? "4k " : "",
2556 imsm_orom_has_chunk(orom
, 8) ? "8k " : "",
2557 imsm_orom_has_chunk(orom
, 16) ? "16k " : "",
2558 imsm_orom_has_chunk(orom
, 32) ? "32k " : "",
2559 imsm_orom_has_chunk(orom
, 64) ? "64k " : "",
2560 imsm_orom_has_chunk(orom
, 128) ? "128k " : "",
2561 imsm_orom_has_chunk(orom
, 256) ? "256k " : "",
2562 imsm_orom_has_chunk(orom
, 512) ? "512k " : "",
2563 imsm_orom_has_chunk(orom
, 1024*1) ? "1M " : "",
2564 imsm_orom_has_chunk(orom
, 1024*2) ? "2M " : "",
2565 imsm_orom_has_chunk(orom
, 1024*4) ? "4M " : "",
2566 imsm_orom_has_chunk(orom
, 1024*8) ? "8M " : "",
2567 imsm_orom_has_chunk(orom
, 1024*16) ? "16M " : "",
2568 imsm_orom_has_chunk(orom
, 1024*32) ? "32M " : "",
2569 imsm_orom_has_chunk(orom
, 1024*64) ? "64M " : "");
2570 printf("IMSM_2TB_VOLUMES=%s\n",(orom
->attr
& IMSM_OROM_ATTR_2TB
) ? "yes" : "no");
2571 printf("IMSM_2TB_DISKS=%s\n",(orom
->attr
& IMSM_OROM_ATTR_2TB_DISK
) ? "yes" : "no");
2572 printf("IMSM_MAX_DISKS=%d\n",orom
->tds
);
2573 printf("IMSM_MAX_VOLUMES_PER_ARRAY=%d\n",orom
->vpa
);
2574 printf("IMSM_MAX_VOLUMES_PER_CONTROLLER=%d\n",orom
->vphba
);
2577 static int detail_platform_imsm(int verbose
, int enumerate_only
, char *controller_path
)
2579 /* There are two components to imsm platform support, the ahci SATA
2580 * controller and the option-rom. To find the SATA controller we
2581 * simply look in /sys/bus/pci/drivers/ahci to see if an ahci
2582 * controller with the Intel vendor id is present. This approach
2583 * allows mdadm to leverage the kernel's ahci detection logic, with the
2584 * caveat that if ahci.ko is not loaded mdadm will not be able to
2585 * detect platform raid capabilities. The option-rom resides in a
2586 * platform "Adapter ROM". We scan for its signature to retrieve the
2587 * platform capabilities. If raid support is disabled in the BIOS the
2588 * option-rom capability structure will not be available.
2590 struct sys_dev
*list
, *hba
;
2595 if (enumerate_only
) {
2596 if (check_env("IMSM_NO_PLATFORM"))
2598 list
= find_intel_devices();
2601 for (hba
= list
; hba
; hba
= hba
->next
) {
2602 if (find_imsm_capability(hba
)) {
2612 list
= find_intel_devices();
2615 pr_err("no active Intel(R) RAID controller found.\n");
2617 } else if (verbose
> 0)
2618 print_found_intel_controllers(list
);
2620 for (hba
= list
; hba
; hba
= hba
->next
) {
2621 if (controller_path
&& (compare_paths(hba
->path
, controller_path
) != 0))
2623 if (!find_imsm_capability(hba
)) {
2625 pr_err("imsm capabilities not found for controller: %s (type %s)\n",
2626 hba
->type
== SYS_DEV_VMD
? vmd_domain_to_controller(hba
, buf
) : hba
->path
,
2627 get_sys_dev_type(hba
->type
));
2633 if (controller_path
&& result
== 1) {
2634 pr_err("no active Intel(R) RAID controller found under %s\n",
2639 const struct orom_entry
*entry
;
2641 for (entry
= orom_entries
; entry
; entry
= entry
->next
) {
2642 if (entry
->type
== SYS_DEV_VMD
) {
2643 print_imsm_capability(&entry
->orom
);
2644 printf(" 3rd party NVMe :%s supported\n",
2645 imsm_orom_has_tpv_support(&entry
->orom
)?"":" not");
2646 for (hba
= list
; hba
; hba
= hba
->next
) {
2647 if (hba
->type
== SYS_DEV_VMD
) {
2649 printf(" I/O Controller : %s (%s)\n",
2650 vmd_domain_to_controller(hba
, buf
), get_sys_dev_type(hba
->type
));
2651 if (print_vmd_attached_devs(hba
)) {
2653 pr_err("failed to get devices attached to VMD domain.\n");
2662 print_imsm_capability(&entry
->orom
);
2663 if (entry
->type
== SYS_DEV_NVME
) {
2664 for (hba
= list
; hba
; hba
= hba
->next
) {
2665 if (hba
->type
== SYS_DEV_NVME
)
2666 printf(" NVMe Device : %s\n", hba
->path
);
2672 struct devid_list
*devid
;
2673 for (devid
= entry
->devid_list
; devid
; devid
= devid
->next
) {
2674 hba
= device_by_id(devid
->devid
);
2678 printf(" I/O Controller : %s (%s)\n",
2679 hba
->path
, get_sys_dev_type(hba
->type
));
2680 if (hba
->type
== SYS_DEV_SATA
) {
2681 host_base
= ahci_get_port_count(hba
->path
, &port_count
);
2682 if (ahci_enumerate_ports(hba
->path
, port_count
, host_base
, verbose
)) {
2684 pr_err("failed to enumerate ports on SATA controller at %s.\n", hba
->pci_id
);
2695 static int export_detail_platform_imsm(int verbose
, char *controller_path
)
2697 struct sys_dev
*list
, *hba
;
2700 list
= find_intel_devices();
2703 pr_err("IMSM_DETAIL_PLATFORM_ERROR=NO_INTEL_DEVICES\n");
2708 for (hba
= list
; hba
; hba
= hba
->next
) {
2709 if (controller_path
&& (compare_paths(hba
->path
,controller_path
) != 0))
2711 if (!find_imsm_capability(hba
) && verbose
> 0) {
2713 pr_err("IMSM_DETAIL_PLATFORM_ERROR=NO_IMSM_CAPABLE_DEVICE_UNDER_%s\n",
2714 hba
->type
== SYS_DEV_VMD
? vmd_domain_to_controller(hba
, buf
) : hba
->path
);
2720 const struct orom_entry
*entry
;
2722 for (entry
= orom_entries
; entry
; entry
= entry
->next
) {
2723 if (entry
->type
== SYS_DEV_VMD
) {
2724 for (hba
= list
; hba
; hba
= hba
->next
)
2725 print_imsm_capability_export(&entry
->orom
);
2728 print_imsm_capability_export(&entry
->orom
);
2734 static int match_home_imsm(struct supertype
*st
, char *homehost
)
2736 /* the imsm metadata format does not specify any host
2737 * identification information. We return -1 since we can never
2738 * confirm nor deny whether a given array is "meant" for this
2739 * host. We rely on compare_super and the 'family_num' fields to
2740 * exclude member disks that do not belong, and we rely on
2741 * mdadm.conf to specify the arrays that should be assembled.
2742 * Auto-assembly may still pick up "foreign" arrays.
2748 static void uuid_from_super_imsm(struct supertype
*st
, int uuid
[4])
2750 /* The uuid returned here is used for:
2751 * uuid to put into bitmap file (Create, Grow)
2752 * uuid for backup header when saving critical section (Grow)
2753 * comparing uuids when re-adding a device into an array
2754 * In these cases the uuid required is that of the data-array,
2755 * not the device-set.
2756 * uuid to recognise same set when adding a missing device back
2757 * to an array. This is a uuid for the device-set.
2759 * For each of these we can make do with a truncated
2760 * or hashed uuid rather than the original, as long as
2762 * In each case the uuid required is that of the data-array,
2763 * not the device-set.
2765 /* imsm does not track uuid's so we synthesis one using sha1 on
2766 * - The signature (Which is constant for all imsm array, but no matter)
2767 * - the orig_family_num of the container
2768 * - the index number of the volume
2769 * - the 'serial' number of the volume.
2770 * Hopefully these are all constant.
2772 struct intel_super
*super
= st
->sb
;
2775 struct sha1_ctx ctx
;
2776 struct imsm_dev
*dev
= NULL
;
2779 /* some mdadm versions failed to set ->orig_family_num, in which
2780 * case fall back to ->family_num. orig_family_num will be
2781 * fixed up with the first metadata update.
2783 family_num
= super
->anchor
->orig_family_num
;
2784 if (family_num
== 0)
2785 family_num
= super
->anchor
->family_num
;
2786 sha1_init_ctx(&ctx
);
2787 sha1_process_bytes(super
->anchor
->sig
, MPB_SIG_LEN
, &ctx
);
2788 sha1_process_bytes(&family_num
, sizeof(__u32
), &ctx
);
2789 if (super
->current_vol
>= 0)
2790 dev
= get_imsm_dev(super
, super
->current_vol
);
2792 __u32 vol
= super
->current_vol
;
2793 sha1_process_bytes(&vol
, sizeof(vol
), &ctx
);
2794 sha1_process_bytes(dev
->volume
, MAX_RAID_SERIAL_LEN
, &ctx
);
2796 sha1_finish_ctx(&ctx
, buf
);
2797 memcpy(uuid
, buf
, 4*4);
2802 get_imsm_numerical_version(struct imsm_super
*mpb
, int *m
, int *p
)
2804 __u8
*v
= get_imsm_version(mpb
);
2805 __u8
*end
= mpb
->sig
+ MAX_SIGNATURE_LENGTH
;
2806 char major
[] = { 0, 0, 0 };
2807 char minor
[] = { 0 ,0, 0 };
2808 char patch
[] = { 0, 0, 0 };
2809 char *ver_parse
[] = { major
, minor
, patch
};
2813 while (*v
!= '\0' && v
< end
) {
2814 if (*v
!= '.' && j
< 2)
2815 ver_parse
[i
][j
++] = *v
;
2823 *m
= strtol(minor
, NULL
, 0);
2824 *p
= strtol(patch
, NULL
, 0);
2828 static __u32
migr_strip_blocks_resync(struct imsm_dev
*dev
)
2830 /* migr_strip_size when repairing or initializing parity */
2831 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
2832 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
2834 switch (get_imsm_raid_level(map
)) {
2839 return 128*1024 >> 9;
2843 static __u32
migr_strip_blocks_rebuild(struct imsm_dev
*dev
)
2845 /* migr_strip_size when rebuilding a degraded disk, no idea why
2846 * this is different than migr_strip_size_resync(), but it's good
2849 struct imsm_map
*map
= get_imsm_map(dev
, MAP_1
);
2850 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
2852 switch (get_imsm_raid_level(map
)) {
2855 if (map
->num_members
% map
->num_domains
== 0)
2856 return 128*1024 >> 9;
2860 return max((__u32
) 64*1024 >> 9, chunk
);
2862 return 128*1024 >> 9;
2866 static __u32
num_stripes_per_unit_resync(struct imsm_dev
*dev
)
2868 struct imsm_map
*lo
= get_imsm_map(dev
, MAP_0
);
2869 struct imsm_map
*hi
= get_imsm_map(dev
, MAP_1
);
2870 __u32 lo_chunk
= __le32_to_cpu(lo
->blocks_per_strip
);
2871 __u32 hi_chunk
= __le32_to_cpu(hi
->blocks_per_strip
);
2873 return max((__u32
) 1, hi_chunk
/ lo_chunk
);
2876 static __u32
num_stripes_per_unit_rebuild(struct imsm_dev
*dev
)
2878 struct imsm_map
*lo
= get_imsm_map(dev
, MAP_0
);
2879 int level
= get_imsm_raid_level(lo
);
2881 if (level
== 1 || level
== 10) {
2882 struct imsm_map
*hi
= get_imsm_map(dev
, MAP_1
);
2884 return hi
->num_domains
;
2886 return num_stripes_per_unit_resync(dev
);
2889 static __u8
imsm_num_data_members(struct imsm_map
*map
)
2891 /* named 'imsm_' because raid0, raid1 and raid10
2892 * counter-intuitively have the same number of data disks
2894 switch (get_imsm_raid_level(map
)) {
2896 return map
->num_members
;
2900 return map
->num_members
/2;
2902 return map
->num_members
- 1;
2904 dprintf("unsupported raid level\n");
2909 static unsigned long long calc_component_size(struct imsm_map
*map
,
2910 struct imsm_dev
*dev
)
2912 unsigned long long component_size
;
2913 unsigned long long dev_size
= imsm_dev_size(dev
);
2914 long long calc_dev_size
= 0;
2915 unsigned int member_disks
= imsm_num_data_members(map
);
2917 if (member_disks
== 0)
2920 component_size
= per_dev_array_size(map
);
2921 calc_dev_size
= component_size
* member_disks
;
2923 /* Component size is rounded to 1MB so difference between size from
2924 * metadata and size calculated from num_data_stripes equals up to
2925 * 2048 blocks per each device. If the difference is higher it means
2926 * that array size was expanded and num_data_stripes was not updated.
2928 if (llabs(calc_dev_size
- (long long)dev_size
) >
2929 (1 << SECT_PER_MB_SHIFT
) * member_disks
) {
2930 component_size
= dev_size
/ member_disks
;
2931 dprintf("Invalid num_data_stripes in metadata; expected=%llu, found=%llu\n",
2932 component_size
/ map
->blocks_per_strip
,
2933 num_data_stripes(map
));
2936 return component_size
;
2939 static __u32
parity_segment_depth(struct imsm_dev
*dev
)
2941 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
2942 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
2944 switch(get_imsm_raid_level(map
)) {
2947 return chunk
* map
->num_domains
;
2949 return chunk
* map
->num_members
;
2955 static __u32
map_migr_block(struct imsm_dev
*dev
, __u32 block
)
2957 struct imsm_map
*map
= get_imsm_map(dev
, MAP_1
);
2958 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
2959 __u32 strip
= block
/ chunk
;
2961 switch (get_imsm_raid_level(map
)) {
2964 __u32 vol_strip
= (strip
* map
->num_domains
) + 1;
2965 __u32 vol_stripe
= vol_strip
/ map
->num_members
;
2967 return vol_stripe
* chunk
+ block
% chunk
;
2969 __u32 stripe
= strip
/ (map
->num_members
- 1);
2971 return stripe
* chunk
+ block
% chunk
;
2978 static __u64
blocks_per_migr_unit(struct intel_super
*super
,
2979 struct imsm_dev
*dev
)
2981 /* calculate the conversion factor between per member 'blocks'
2982 * (md/{resync,rebuild}_start) and imsm migration units, return
2983 * 0 for the 'not migrating' and 'unsupported migration' cases
2985 if (!dev
->vol
.migr_state
)
2988 switch (migr_type(dev
)) {
2989 case MIGR_GEN_MIGR
: {
2990 struct migr_record
*migr_rec
= super
->migr_rec
;
2991 return __le32_to_cpu(migr_rec
->blocks_per_unit
);
2996 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
2997 __u32 stripes_per_unit
;
2998 __u32 blocks_per_unit
;
3007 /* yes, this is really the translation of migr_units to
3008 * per-member blocks in the 'resync' case
3010 stripes_per_unit
= num_stripes_per_unit_resync(dev
);
3011 migr_chunk
= migr_strip_blocks_resync(dev
);
3012 disks
= imsm_num_data_members(map
);
3013 blocks_per_unit
= stripes_per_unit
* migr_chunk
* disks
;
3014 stripe
= __le16_to_cpu(map
->blocks_per_strip
) * disks
;
3015 segment
= blocks_per_unit
/ stripe
;
3016 block_rel
= blocks_per_unit
- segment
* stripe
;
3017 parity_depth
= parity_segment_depth(dev
);
3018 block_map
= map_migr_block(dev
, block_rel
);
3019 return block_map
+ parity_depth
* segment
;
3021 case MIGR_REBUILD
: {
3022 __u32 stripes_per_unit
;
3025 stripes_per_unit
= num_stripes_per_unit_rebuild(dev
);
3026 migr_chunk
= migr_strip_blocks_rebuild(dev
);
3027 return migr_chunk
* stripes_per_unit
;
3029 case MIGR_STATE_CHANGE
:
3035 static int imsm_level_to_layout(int level
)
3043 return ALGORITHM_LEFT_ASYMMETRIC
;
3050 /*******************************************************************************
3051 * Function: read_imsm_migr_rec
3052 * Description: Function reads imsm migration record from last sector of disk
3054 * fd : disk descriptor
3055 * super : metadata info
3059 ******************************************************************************/
3060 static int read_imsm_migr_rec(int fd
, struct intel_super
*super
)
3063 unsigned int sector_size
= super
->sector_size
;
3064 unsigned long long dsize
;
3066 get_dev_size(fd
, NULL
, &dsize
);
3067 if (lseek64(fd
, dsize
- (sector_size
*MIGR_REC_SECTOR_POSITION
),
3069 pr_err("Cannot seek to anchor block: %s\n",
3073 if ((unsigned int)read(fd
, super
->migr_rec_buf
,
3074 MIGR_REC_BUF_SECTORS
*sector_size
) !=
3075 MIGR_REC_BUF_SECTORS
*sector_size
) {
3076 pr_err("Cannot read migr record block: %s\n",
3081 if (sector_size
== 4096)
3082 convert_from_4k_imsm_migr_rec(super
);
3088 static struct imsm_dev
*imsm_get_device_during_migration(
3089 struct intel_super
*super
)
3092 struct intel_dev
*dv
;
3094 for (dv
= super
->devlist
; dv
; dv
= dv
->next
) {
3095 if (is_gen_migration(dv
->dev
))
3101 /*******************************************************************************
3102 * Function: load_imsm_migr_rec
3103 * Description: Function reads imsm migration record (it is stored at the last
3106 * super : imsm internal array info
3107 * info : general array info
3111 * -2 : no migration in progress
3112 ******************************************************************************/
3113 static int load_imsm_migr_rec(struct intel_super
*super
, struct mdinfo
*info
)
3120 struct imsm_dev
*dev
;
3121 struct imsm_map
*map
;
3124 /* find map under migration */
3125 dev
= imsm_get_device_during_migration(super
);
3126 /* nothing to load,no migration in progress?
3132 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
3133 /* read only from one of the first two slots */
3134 if ((sd
->disk
.raid_disk
< 0) ||
3135 (sd
->disk
.raid_disk
> 1))
3138 sprintf(nm
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
3139 fd
= dev_open(nm
, O_RDONLY
);
3145 map
= get_imsm_map(dev
, MAP_0
);
3146 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
3147 /* skip spare and failed disks
3151 /* read only from one of the first two slots */
3153 slot
= get_imsm_disk_slot(map
, dl
->index
);
3154 if (map
== NULL
|| slot
> 1 || slot
< 0)
3156 sprintf(nm
, "%d:%d", dl
->major
, dl
->minor
);
3157 fd
= dev_open(nm
, O_RDONLY
);
3164 retval
= read_imsm_migr_rec(fd
, super
);
3172 /*******************************************************************************
3173 * function: imsm_create_metadata_checkpoint_update
3174 * Description: It creates update for checkpoint change.
3176 * super : imsm internal array info
3177 * u : pointer to prepared update
3180 * If length is equal to 0, input pointer u contains no update
3181 ******************************************************************************/
3182 static int imsm_create_metadata_checkpoint_update(
3183 struct intel_super
*super
,
3184 struct imsm_update_general_migration_checkpoint
**u
)
3187 int update_memory_size
= 0;
3189 dprintf("(enter)\n");
3195 /* size of all update data without anchor */
3196 update_memory_size
=
3197 sizeof(struct imsm_update_general_migration_checkpoint
);
3199 *u
= xcalloc(1, update_memory_size
);
3201 dprintf("error: cannot get memory\n");
3204 (*u
)->type
= update_general_migration_checkpoint
;
3205 (*u
)->curr_migr_unit
= current_migr_unit(super
->migr_rec
);
3206 dprintf("prepared for %u\n", (*u
)->curr_migr_unit
);
3208 return update_memory_size
;
3211 static void imsm_update_metadata_locally(struct supertype
*st
,
3212 void *buf
, int len
);
3214 /*******************************************************************************
3215 * Function: write_imsm_migr_rec
3216 * Description: Function writes imsm migration record
3217 * (at the last sector of disk)
3219 * super : imsm internal array info
3223 ******************************************************************************/
3224 static int write_imsm_migr_rec(struct supertype
*st
)
3226 struct intel_super
*super
= st
->sb
;
3227 unsigned int sector_size
= super
->sector_size
;
3228 unsigned long long dsize
;
3234 struct imsm_update_general_migration_checkpoint
*u
;
3235 struct imsm_dev
*dev
;
3236 struct imsm_map
*map
;
3238 /* find map under migration */
3239 dev
= imsm_get_device_during_migration(super
);
3240 /* if no migration, write buffer anyway to clear migr_record
3241 * on disk based on first available device
3244 dev
= get_imsm_dev(super
, super
->current_vol
< 0 ? 0 :
3245 super
->current_vol
);
3247 map
= get_imsm_map(dev
, MAP_0
);
3249 if (sector_size
== 4096)
3250 convert_to_4k_imsm_migr_rec(super
);
3251 for (sd
= super
->disks
; sd
; sd
= sd
->next
) {
3254 /* skip failed and spare devices */
3257 /* write to 2 first slots only */
3259 slot
= get_imsm_disk_slot(map
, sd
->index
);
3260 if (map
== NULL
|| slot
> 1 || slot
< 0)
3263 sprintf(nm
, "%d:%d", sd
->major
, sd
->minor
);
3264 fd
= dev_open(nm
, O_RDWR
);
3267 get_dev_size(fd
, NULL
, &dsize
);
3268 if (lseek64(fd
, dsize
- (MIGR_REC_SECTOR_POSITION
*sector_size
),
3270 pr_err("Cannot seek to anchor block: %s\n",
3274 if ((unsigned int)write(fd
, super
->migr_rec_buf
,
3275 MIGR_REC_BUF_SECTORS
*sector_size
) !=
3276 MIGR_REC_BUF_SECTORS
*sector_size
) {
3277 pr_err("Cannot write migr record block: %s\n",
3284 if (sector_size
== 4096)
3285 convert_from_4k_imsm_migr_rec(super
);
3286 /* update checkpoint information in metadata */
3287 len
= imsm_create_metadata_checkpoint_update(super
, &u
);
3289 dprintf("imsm: Cannot prepare update\n");
3292 /* update metadata locally */
3293 imsm_update_metadata_locally(st
, u
, len
);
3294 /* and possibly remotely */
3295 if (st
->update_tail
) {
3296 append_metadata_update(st
, u
, len
);
3297 /* during reshape we do all work inside metadata handler
3298 * manage_reshape(), so metadata update has to be triggered
3301 flush_metadata_updates(st
);
3302 st
->update_tail
= &st
->updates
;
3313 /* spare/missing disks activations are not allowe when
3314 * array/container performs reshape operation, because
3315 * all arrays in container works on the same disks set
3317 int imsm_reshape_blocks_arrays_changes(struct intel_super
*super
)
3320 struct intel_dev
*i_dev
;
3321 struct imsm_dev
*dev
;
3323 /* check whole container
3325 for (i_dev
= super
->devlist
; i_dev
; i_dev
= i_dev
->next
) {
3327 if (is_gen_migration(dev
)) {
3328 /* No repair during any migration in container
3336 static unsigned long long imsm_component_size_alignment_check(int level
,
3338 unsigned int sector_size
,
3339 unsigned long long component_size
)
3341 unsigned int component_size_alignment
;
3343 /* check component size alignment
3345 component_size_alignment
= component_size
% (chunk_size
/sector_size
);
3347 dprintf("(Level: %i, chunk_size = %i, component_size = %llu), component_size_alignment = %u\n",
3348 level
, chunk_size
, component_size
,
3349 component_size_alignment
);
3351 if (component_size_alignment
&& (level
!= 1) && (level
!= UnSet
)) {
3352 dprintf("imsm: reported component size aligned from %llu ",
3354 component_size
-= component_size_alignment
;
3355 dprintf_cont("to %llu (%i).\n",
3356 component_size
, component_size_alignment
);
3359 return component_size
;
3362 static unsigned long long get_ppl_sector(struct intel_super
*super
, int dev_idx
)
3364 struct imsm_dev
*dev
= get_imsm_dev(super
, dev_idx
);
3365 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
3367 return pba_of_lba0(map
) +
3368 (num_data_stripes(map
) * map
->blocks_per_strip
);
3371 static void getinfo_super_imsm_volume(struct supertype
*st
, struct mdinfo
*info
, char *dmap
)
3373 struct intel_super
*super
= st
->sb
;
3374 struct migr_record
*migr_rec
= super
->migr_rec
;
3375 struct imsm_dev
*dev
= get_imsm_dev(super
, super
->current_vol
);
3376 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
3377 struct imsm_map
*prev_map
= get_imsm_map(dev
, MAP_1
);
3378 struct imsm_map
*map_to_analyse
= map
;
3380 int map_disks
= info
->array
.raid_disks
;
3382 memset(info
, 0, sizeof(*info
));
3384 map_to_analyse
= prev_map
;
3386 dl
= super
->current_disk
;
3388 info
->container_member
= super
->current_vol
;
3389 info
->array
.raid_disks
= map
->num_members
;
3390 info
->array
.level
= get_imsm_raid_level(map_to_analyse
);
3391 info
->array
.layout
= imsm_level_to_layout(info
->array
.level
);
3392 info
->array
.md_minor
= -1;
3393 info
->array
.ctime
= 0;
3394 info
->array
.utime
= 0;
3395 info
->array
.chunk_size
=
3396 __le16_to_cpu(map_to_analyse
->blocks_per_strip
) << 9;
3397 info
->array
.state
= !(dev
->vol
.dirty
& RAIDVOL_DIRTY
);
3398 info
->custom_array_size
= imsm_dev_size(dev
);
3399 info
->recovery_blocked
= imsm_reshape_blocks_arrays_changes(st
->sb
);
3401 if (is_gen_migration(dev
)) {
3402 info
->reshape_active
= 1;
3403 info
->new_level
= get_imsm_raid_level(map
);
3404 info
->new_layout
= imsm_level_to_layout(info
->new_level
);
3405 info
->new_chunk
= __le16_to_cpu(map
->blocks_per_strip
) << 9;
3406 info
->delta_disks
= map
->num_members
- prev_map
->num_members
;
3407 if (info
->delta_disks
) {
3408 /* this needs to be applied to every array
3411 info
->reshape_active
= CONTAINER_RESHAPE
;
3413 /* We shape information that we give to md might have to be
3414 * modify to cope with md's requirement for reshaping arrays.
3415 * For example, when reshaping a RAID0, md requires it to be
3416 * presented as a degraded RAID4.
3417 * Also if a RAID0 is migrating to a RAID5 we need to specify
3418 * the array as already being RAID5, but the 'before' layout
3419 * is a RAID4-like layout.
3421 switch (info
->array
.level
) {
3423 switch(info
->new_level
) {
3425 /* conversion is happening as RAID4 */
3426 info
->array
.level
= 4;
3427 info
->array
.raid_disks
+= 1;
3430 /* conversion is happening as RAID5 */
3431 info
->array
.level
= 5;
3432 info
->array
.layout
= ALGORITHM_PARITY_N
;
3433 info
->delta_disks
-= 1;
3436 /* FIXME error message */
3437 info
->array
.level
= UnSet
;
3443 info
->new_level
= UnSet
;
3444 info
->new_layout
= UnSet
;
3445 info
->new_chunk
= info
->array
.chunk_size
;
3446 info
->delta_disks
= 0;
3450 info
->disk
.major
= dl
->major
;
3451 info
->disk
.minor
= dl
->minor
;
3452 info
->disk
.number
= dl
->index
;
3453 info
->disk
.raid_disk
= get_imsm_disk_slot(map_to_analyse
,
3457 info
->data_offset
= pba_of_lba0(map_to_analyse
);
3458 info
->component_size
= calc_component_size(map
, dev
);
3459 info
->component_size
= imsm_component_size_alignment_check(
3461 info
->array
.chunk_size
,
3463 info
->component_size
);
3464 info
->bb
.supported
= 1;
3466 memset(info
->uuid
, 0, sizeof(info
->uuid
));
3467 info
->recovery_start
= MaxSector
;
3469 if (info
->array
.level
== 5 &&
3470 (dev
->rwh_policy
== RWH_DISTRIBUTED
||
3471 dev
->rwh_policy
== RWH_MULTIPLE_DISTRIBUTED
)) {
3472 info
->consistency_policy
= CONSISTENCY_POLICY_PPL
;
3473 info
->ppl_sector
= get_ppl_sector(super
, super
->current_vol
);
3474 if (dev
->rwh_policy
== RWH_MULTIPLE_DISTRIBUTED
)
3475 info
->ppl_size
= MULTIPLE_PPL_AREA_SIZE_IMSM
>> 9;
3477 info
->ppl_size
= (PPL_HEADER_SIZE
+ PPL_ENTRY_SPACE
)
3479 } else if (info
->array
.level
<= 0) {
3480 info
->consistency_policy
= CONSISTENCY_POLICY_NONE
;
3482 info
->consistency_policy
= CONSISTENCY_POLICY_RESYNC
;
3485 info
->reshape_progress
= 0;
3486 info
->resync_start
= MaxSector
;
3487 if ((map_to_analyse
->map_state
== IMSM_T_STATE_UNINITIALIZED
||
3488 !(info
->array
.state
& 1)) &&
3489 imsm_reshape_blocks_arrays_changes(super
) == 0) {
3490 info
->resync_start
= 0;
3492 if (dev
->vol
.migr_state
) {
3493 switch (migr_type(dev
)) {
3496 __u64 blocks_per_unit
= blocks_per_migr_unit(super
,
3498 __u64 units
= __le32_to_cpu(dev
->vol
.curr_migr_unit
);
3500 info
->resync_start
= blocks_per_unit
* units
;
3503 case MIGR_GEN_MIGR
: {
3504 __u64 blocks_per_unit
= blocks_per_migr_unit(super
,
3506 __u64 units
= current_migr_unit(migr_rec
);
3507 unsigned long long array_blocks
;
3510 if (__le32_to_cpu(migr_rec
->ascending_migr
) &&
3512 (get_num_migr_units(migr_rec
)-1)) &&
3513 (super
->migr_rec
->rec_status
==
3514 __cpu_to_le32(UNIT_SRC_IN_CP_AREA
)))
3517 info
->reshape_progress
= blocks_per_unit
* units
;
3519 dprintf("IMSM: General Migration checkpoint : %llu (%llu) -> read reshape progress : %llu\n",
3520 (unsigned long long)units
,
3521 (unsigned long long)blocks_per_unit
,
3522 info
->reshape_progress
);
3524 used_disks
= imsm_num_data_members(prev_map
);
3525 if (used_disks
> 0) {
3526 array_blocks
= per_dev_array_size(map
) *
3528 info
->custom_array_size
=
3529 round_size_to_mb(array_blocks
,
3535 /* we could emulate the checkpointing of
3536 * 'sync_action=check' migrations, but for now
3537 * we just immediately complete them
3540 /* this is handled by container_content_imsm() */
3541 case MIGR_STATE_CHANGE
:
3542 /* FIXME handle other migrations */
3544 /* we are not dirty, so... */
3545 info
->resync_start
= MaxSector
;
3549 strncpy(info
->name
, (char *) dev
->volume
, MAX_RAID_SERIAL_LEN
);
3550 info
->name
[MAX_RAID_SERIAL_LEN
] = 0;
3552 info
->array
.major_version
= -1;
3553 info
->array
.minor_version
= -2;
3554 sprintf(info
->text_version
, "/%s/%d", st
->container_devnm
, info
->container_member
);
3555 info
->safe_mode_delay
= 4000; /* 4 secs like the Matrix driver */
3556 uuid_from_super_imsm(st
, info
->uuid
);
3560 for (i
=0; i
<map_disks
; i
++) {
3562 if (i
< info
->array
.raid_disks
) {
3563 struct imsm_disk
*dsk
;
3564 j
= get_imsm_disk_idx(dev
, i
, MAP_X
);
3565 dsk
= get_imsm_disk(super
, j
);
3566 if (dsk
&& (dsk
->status
& CONFIGURED_DISK
))
3573 static __u8
imsm_check_degraded(struct intel_super
*super
, struct imsm_dev
*dev
,
3574 int failed
, int look_in_map
);
3576 static int imsm_count_failed(struct intel_super
*super
, struct imsm_dev
*dev
,
3579 static void manage_second_map(struct intel_super
*super
, struct imsm_dev
*dev
)
3581 if (is_gen_migration(dev
)) {
3584 struct imsm_map
*map2
= get_imsm_map(dev
, MAP_1
);
3586 failed
= imsm_count_failed(super
, dev
, MAP_1
);
3587 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_1
);
3588 if (map2
->map_state
!= map_state
) {
3589 map2
->map_state
= map_state
;
3590 super
->updates_pending
++;
3595 static struct imsm_disk
*get_imsm_missing(struct intel_super
*super
, __u8 index
)
3599 for (d
= super
->missing
; d
; d
= d
->next
)
3600 if (d
->index
== index
)
3605 static void getinfo_super_imsm(struct supertype
*st
, struct mdinfo
*info
, char *map
)
3607 struct intel_super
*super
= st
->sb
;
3608 struct imsm_disk
*disk
;
3609 int map_disks
= info
->array
.raid_disks
;
3610 int max_enough
= -1;
3612 struct imsm_super
*mpb
;
3614 if (super
->current_vol
>= 0) {
3615 getinfo_super_imsm_volume(st
, info
, map
);
3618 memset(info
, 0, sizeof(*info
));
3620 /* Set raid_disks to zero so that Assemble will always pull in valid
3623 info
->array
.raid_disks
= 0;
3624 info
->array
.level
= LEVEL_CONTAINER
;
3625 info
->array
.layout
= 0;
3626 info
->array
.md_minor
= -1;
3627 info
->array
.ctime
= 0; /* N/A for imsm */
3628 info
->array
.utime
= 0;
3629 info
->array
.chunk_size
= 0;
3631 info
->disk
.major
= 0;
3632 info
->disk
.minor
= 0;
3633 info
->disk
.raid_disk
= -1;
3634 info
->reshape_active
= 0;
3635 info
->array
.major_version
= -1;
3636 info
->array
.minor_version
= -2;
3637 strcpy(info
->text_version
, "imsm");
3638 info
->safe_mode_delay
= 0;
3639 info
->disk
.number
= -1;
3640 info
->disk
.state
= 0;
3642 info
->recovery_start
= MaxSector
;
3643 info
->recovery_blocked
= imsm_reshape_blocks_arrays_changes(st
->sb
);
3644 info
->bb
.supported
= 1;
3646 /* do we have the all the insync disks that we expect? */
3647 mpb
= super
->anchor
;
3648 info
->events
= __le32_to_cpu(mpb
->generation_num
);
3650 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
3651 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
3652 int failed
, enough
, j
, missing
= 0;
3653 struct imsm_map
*map
;
3656 failed
= imsm_count_failed(super
, dev
, MAP_0
);
3657 state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
3658 map
= get_imsm_map(dev
, MAP_0
);
3660 /* any newly missing disks?
3661 * (catches single-degraded vs double-degraded)
3663 for (j
= 0; j
< map
->num_members
; j
++) {
3664 __u32 ord
= get_imsm_ord_tbl_ent(dev
, j
, MAP_0
);
3665 __u32 idx
= ord_to_idx(ord
);
3667 if (super
->disks
&& super
->disks
->index
== (int)idx
)
3668 info
->disk
.raid_disk
= j
;
3670 if (!(ord
& IMSM_ORD_REBUILD
) &&
3671 get_imsm_missing(super
, idx
)) {
3677 if (state
== IMSM_T_STATE_FAILED
)
3679 else if (state
== IMSM_T_STATE_DEGRADED
&&
3680 (state
!= map
->map_state
|| missing
))
3682 else /* we're normal, or already degraded */
3684 if (is_gen_migration(dev
) && missing
) {
3685 /* during general migration we need all disks
3686 * that process is running on.
3687 * No new missing disk is allowed.
3691 /* no more checks necessary
3695 /* in the missing/failed disk case check to see
3696 * if at least one array is runnable
3698 max_enough
= max(max_enough
, enough
);
3700 dprintf("enough: %d\n", max_enough
);
3701 info
->container_enough
= max_enough
;
3704 __u32 reserved
= imsm_reserved_sectors(super
, super
->disks
);
3706 disk
= &super
->disks
->disk
;
3707 info
->data_offset
= total_blocks(&super
->disks
->disk
) - reserved
;
3708 info
->component_size
= reserved
;
3709 info
->disk
.state
= is_configured(disk
) ? (1 << MD_DISK_ACTIVE
) : 0;
3710 /* we don't change info->disk.raid_disk here because
3711 * this state will be finalized in mdmon after we have
3712 * found the 'most fresh' version of the metadata
3714 info
->disk
.state
|= is_failed(disk
) ? (1 << MD_DISK_FAULTY
) : 0;
3715 info
->disk
.state
|= (is_spare(disk
) || is_journal(disk
)) ?
3716 0 : (1 << MD_DISK_SYNC
);
3719 /* only call uuid_from_super_imsm when this disk is part of a populated container,
3720 * ->compare_super may have updated the 'num_raid_devs' field for spares
3722 if (info
->disk
.state
& (1 << MD_DISK_SYNC
) || super
->anchor
->num_raid_devs
)
3723 uuid_from_super_imsm(st
, info
->uuid
);
3725 memcpy(info
->uuid
, uuid_zero
, sizeof(uuid_zero
));
3727 /* I don't know how to compute 'map' on imsm, so use safe default */
3730 for (i
= 0; i
< map_disks
; i
++)
3736 /* allocates memory and fills disk in mdinfo structure
3737 * for each disk in array */
3738 struct mdinfo
*getinfo_super_disks_imsm(struct supertype
*st
)
3740 struct mdinfo
*mddev
;
3741 struct intel_super
*super
= st
->sb
;
3742 struct imsm_disk
*disk
;
3745 if (!super
|| !super
->disks
)
3748 mddev
= xcalloc(1, sizeof(*mddev
));
3752 tmp
= xcalloc(1, sizeof(*tmp
));
3754 tmp
->next
= mddev
->devs
;
3756 tmp
->disk
.number
= count
++;
3757 tmp
->disk
.major
= dl
->major
;
3758 tmp
->disk
.minor
= dl
->minor
;
3759 tmp
->disk
.state
= is_configured(disk
) ?
3760 (1 << MD_DISK_ACTIVE
) : 0;
3761 tmp
->disk
.state
|= is_failed(disk
) ? (1 << MD_DISK_FAULTY
) : 0;
3762 tmp
->disk
.state
|= is_spare(disk
) ? 0 : (1 << MD_DISK_SYNC
);
3763 tmp
->disk
.raid_disk
= -1;
3769 static int update_super_imsm(struct supertype
*st
, struct mdinfo
*info
,
3770 char *update
, char *devname
, int verbose
,
3771 int uuid_set
, char *homehost
)
3773 /* For 'assemble' and 'force' we need to return non-zero if any
3774 * change was made. For others, the return value is ignored.
3775 * Update options are:
3776 * force-one : This device looks a bit old but needs to be included,
3777 * update age info appropriately.
3778 * assemble: clear any 'faulty' flag to allow this device to
3780 * force-array: Array is degraded but being forced, mark it clean
3781 * if that will be needed to assemble it.
3783 * newdev: not used ????
3784 * grow: Array has gained a new device - this is currently for
3786 * resync: mark as dirty so a resync will happen.
3787 * name: update the name - preserving the homehost
3788 * uuid: Change the uuid of the array to match watch is given
3790 * Following are not relevant for this imsm:
3791 * sparc2.2 : update from old dodgey metadata
3792 * super-minor: change the preferred_minor number
3793 * summaries: update redundant counters.
3794 * homehost: update the recorded homehost
3795 * _reshape_progress: record new reshape_progress position.
3798 struct intel_super
*super
= st
->sb
;
3799 struct imsm_super
*mpb
;
3801 /* we can only update container info */
3802 if (!super
|| super
->current_vol
>= 0 || !super
->anchor
)
3805 mpb
= super
->anchor
;
3807 if (strcmp(update
, "uuid") == 0) {
3808 /* We take this to mean that the family_num should be updated.
3809 * However that is much smaller than the uuid so we cannot really
3810 * allow an explicit uuid to be given. And it is hard to reliably
3812 * So if !uuid_set we know the current uuid is random and just used
3813 * the first 'int' and copy it to the other 3 positions.
3814 * Otherwise we require the 4 'int's to be the same as would be the
3815 * case if we are using a random uuid. So an explicit uuid will be
3816 * accepted as long as all for ints are the same... which shouldn't hurt
3819 info
->uuid
[1] = info
->uuid
[2] = info
->uuid
[3] = info
->uuid
[0];
3822 if (info
->uuid
[0] != info
->uuid
[1] ||
3823 info
->uuid
[1] != info
->uuid
[2] ||
3824 info
->uuid
[2] != info
->uuid
[3])
3830 mpb
->orig_family_num
= info
->uuid
[0];
3831 } else if (strcmp(update
, "assemble") == 0)
3836 /* successful update? recompute checksum */
3838 mpb
->check_sum
= __le32_to_cpu(__gen_imsm_checksum(mpb
));
3843 static size_t disks_to_mpb_size(int disks
)
3847 size
= sizeof(struct imsm_super
);
3848 size
+= (disks
- 1) * sizeof(struct imsm_disk
);
3849 size
+= 2 * sizeof(struct imsm_dev
);
3850 /* up to 2 maps per raid device (-2 for imsm_maps in imsm_dev */
3851 size
+= (4 - 2) * sizeof(struct imsm_map
);
3852 /* 4 possible disk_ord_tbl's */
3853 size
+= 4 * (disks
- 1) * sizeof(__u32
);
3854 /* maximum bbm log */
3855 size
+= sizeof(struct bbm_log
);
3860 static __u64
avail_size_imsm(struct supertype
*st
, __u64 devsize
,
3861 unsigned long long data_offset
)
3863 if (devsize
< (MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
))
3866 return devsize
- (MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
);
3869 static void free_devlist(struct intel_super
*super
)
3871 struct intel_dev
*dv
;
3873 while (super
->devlist
) {
3874 dv
= super
->devlist
->next
;
3875 free(super
->devlist
->dev
);
3876 free(super
->devlist
);
3877 super
->devlist
= dv
;
3881 static void imsm_copy_dev(struct imsm_dev
*dest
, struct imsm_dev
*src
)
3883 memcpy(dest
, src
, sizeof_imsm_dev(src
, 0));
3886 static int compare_super_imsm(struct supertype
*st
, struct supertype
*tst
)
3890 * 0 same, or first was empty, and second was copied
3891 * 1 second had wrong number
3893 * 3 wrong other info
3895 struct intel_super
*first
= st
->sb
;
3896 struct intel_super
*sec
= tst
->sb
;
3903 /* in platform dependent environment test if the disks
3904 * use the same Intel hba
3905 * If not on Intel hba at all, allow anything.
3907 if (!check_env("IMSM_NO_PLATFORM") && first
->hba
&& sec
->hba
) {
3908 if (first
->hba
->type
!= sec
->hba
->type
) {
3910 "HBAs of devices do not match %s != %s\n",
3911 get_sys_dev_type(first
->hba
->type
),
3912 get_sys_dev_type(sec
->hba
->type
));
3915 if (first
->orom
!= sec
->orom
) {
3917 "HBAs of devices do not match %s != %s\n",
3918 first
->hba
->pci_id
, sec
->hba
->pci_id
);
3923 /* if an anchor does not have num_raid_devs set then it is a free
3926 if (first
->anchor
->num_raid_devs
> 0 &&
3927 sec
->anchor
->num_raid_devs
> 0) {
3928 /* Determine if these disks might ever have been
3929 * related. Further disambiguation can only take place
3930 * in load_super_imsm_all
3932 __u32 first_family
= first
->anchor
->orig_family_num
;
3933 __u32 sec_family
= sec
->anchor
->orig_family_num
;
3935 if (memcmp(first
->anchor
->sig
, sec
->anchor
->sig
,
3936 MAX_SIGNATURE_LENGTH
) != 0)
3939 if (first_family
== 0)
3940 first_family
= first
->anchor
->family_num
;
3941 if (sec_family
== 0)
3942 sec_family
= sec
->anchor
->family_num
;
3944 if (first_family
!= sec_family
)
3949 /* if 'first' is a spare promote it to a populated mpb with sec's
3952 if (first
->anchor
->num_raid_devs
== 0 &&
3953 sec
->anchor
->num_raid_devs
> 0) {
3955 struct intel_dev
*dv
;
3956 struct imsm_dev
*dev
;
3958 /* we need to copy raid device info from sec if an allocation
3959 * fails here we don't associate the spare
3961 for (i
= 0; i
< sec
->anchor
->num_raid_devs
; i
++) {
3962 dv
= xmalloc(sizeof(*dv
));
3963 dev
= xmalloc(sizeof_imsm_dev(get_imsm_dev(sec
, i
), 1));
3966 dv
->next
= first
->devlist
;
3967 first
->devlist
= dv
;
3969 if (i
< sec
->anchor
->num_raid_devs
) {
3970 /* allocation failure */
3971 free_devlist(first
);
3972 pr_err("imsm: failed to associate spare\n");
3975 first
->anchor
->num_raid_devs
= sec
->anchor
->num_raid_devs
;
3976 first
->anchor
->orig_family_num
= sec
->anchor
->orig_family_num
;
3977 first
->anchor
->family_num
= sec
->anchor
->family_num
;
3978 memcpy(first
->anchor
->sig
, sec
->anchor
->sig
, MAX_SIGNATURE_LENGTH
);
3979 for (i
= 0; i
< sec
->anchor
->num_raid_devs
; i
++)
3980 imsm_copy_dev(get_imsm_dev(first
, i
), get_imsm_dev(sec
, i
));
3986 static void fd2devname(int fd
, char *name
)
3990 char dname
[PATH_MAX
];
3995 if (fstat(fd
, &st
) != 0)
3997 sprintf(path
, "/sys/dev/block/%d:%d",
3998 major(st
.st_rdev
), minor(st
.st_rdev
));
4000 rv
= readlink(path
, dname
, sizeof(dname
)-1);
4005 nm
= strrchr(dname
, '/');
4008 snprintf(name
, MAX_RAID_SERIAL_LEN
, "/dev/%s", nm
);
4012 static int nvme_get_serial(int fd
, void *buf
, size_t buf_len
)
4015 char *name
= fd2kname(fd
);
4020 if (strncmp(name
, "nvme", 4) != 0)
4023 snprintf(path
, sizeof(path
) - 1, "/sys/block/%s/device/serial", name
);
4025 return load_sys(path
, buf
, buf_len
);
4028 extern int scsi_get_serial(int fd
, void *buf
, size_t buf_len
);
4030 static int imsm_read_serial(int fd
, char *devname
,
4031 __u8 serial
[MAX_RAID_SERIAL_LEN
])
4040 memset(buf
, 0, sizeof(buf
));
4042 rv
= nvme_get_serial(fd
, buf
, sizeof(buf
));
4045 rv
= scsi_get_serial(fd
, buf
, sizeof(buf
));
4047 if (rv
&& check_env("IMSM_DEVNAME_AS_SERIAL")) {
4048 memset(serial
, 0, MAX_RAID_SERIAL_LEN
);
4049 fd2devname(fd
, (char *) serial
);
4055 pr_err("Failed to retrieve serial for %s\n",
4060 /* trim all whitespace and non-printable characters and convert
4063 for (i
= 0, dest
= buf
; i
< sizeof(buf
) && buf
[i
]; i
++) {
4066 /* ':' is reserved for use in placeholder serial
4067 * numbers for missing disks
4078 /* truncate leading characters */
4079 if (len
> MAX_RAID_SERIAL_LEN
) {
4080 dest
+= len
- MAX_RAID_SERIAL_LEN
;
4081 len
= MAX_RAID_SERIAL_LEN
;
4084 memset(serial
, 0, MAX_RAID_SERIAL_LEN
);
4085 memcpy(serial
, dest
, len
);
4090 static int serialcmp(__u8
*s1
, __u8
*s2
)
4092 return strncmp((char *) s1
, (char *) s2
, MAX_RAID_SERIAL_LEN
);
4095 static void serialcpy(__u8
*dest
, __u8
*src
)
4097 strncpy((char *) dest
, (char *) src
, MAX_RAID_SERIAL_LEN
);
4100 static struct dl
*serial_to_dl(__u8
*serial
, struct intel_super
*super
)
4104 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
4105 if (serialcmp(dl
->serial
, serial
) == 0)
4111 static struct imsm_disk
*
4112 __serial_to_disk(__u8
*serial
, struct imsm_super
*mpb
, int *idx
)
4116 for (i
= 0; i
< mpb
->num_disks
; i
++) {
4117 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, i
);
4119 if (serialcmp(disk
->serial
, serial
) == 0) {
4130 load_imsm_disk(int fd
, struct intel_super
*super
, char *devname
, int keep_fd
)
4132 struct imsm_disk
*disk
;
4137 __u8 serial
[MAX_RAID_SERIAL_LEN
];
4139 rv
= imsm_read_serial(fd
, devname
, serial
);
4144 dl
= xcalloc(1, sizeof(*dl
));
4147 dl
->major
= major(stb
.st_rdev
);
4148 dl
->minor
= minor(stb
.st_rdev
);
4149 dl
->next
= super
->disks
;
4150 dl
->fd
= keep_fd
? fd
: -1;
4151 assert(super
->disks
== NULL
);
4153 serialcpy(dl
->serial
, serial
);
4156 fd2devname(fd
, name
);
4158 dl
->devname
= xstrdup(devname
);
4160 dl
->devname
= xstrdup(name
);
4162 /* look up this disk's index in the current anchor */
4163 disk
= __serial_to_disk(dl
->serial
, super
->anchor
, &dl
->index
);
4166 /* only set index on disks that are a member of a
4167 * populated contianer, i.e. one with raid_devs
4169 if (is_failed(&dl
->disk
))
4171 else if (is_spare(&dl
->disk
) || is_journal(&dl
->disk
))
4178 /* When migrating map0 contains the 'destination' state while map1
4179 * contains the current state. When not migrating map0 contains the
4180 * current state. This routine assumes that map[0].map_state is set to
4181 * the current array state before being called.
4183 * Migration is indicated by one of the following states
4184 * 1/ Idle (migr_state=0 map0state=normal||unitialized||degraded||failed)
4185 * 2/ Initialize (migr_state=1 migr_type=MIGR_INIT map0state=normal
4186 * map1state=unitialized)
4187 * 3/ Repair (Resync) (migr_state=1 migr_type=MIGR_REPAIR map0state=normal
4189 * 4/ Rebuild (migr_state=1 migr_type=MIGR_REBUILD map0state=normal
4190 * map1state=degraded)
4191 * 5/ Migration (mig_state=1 migr_type=MIGR_GEN_MIGR map0state=normal
4194 static void migrate(struct imsm_dev
*dev
, struct intel_super
*super
,
4195 __u8 to_state
, int migr_type
)
4197 struct imsm_map
*dest
;
4198 struct imsm_map
*src
= get_imsm_map(dev
, MAP_0
);
4200 dev
->vol
.migr_state
= 1;
4201 set_migr_type(dev
, migr_type
);
4202 dev
->vol
.curr_migr_unit
= 0;
4203 dest
= get_imsm_map(dev
, MAP_1
);
4205 /* duplicate and then set the target end state in map[0] */
4206 memcpy(dest
, src
, sizeof_imsm_map(src
));
4207 if (migr_type
== MIGR_GEN_MIGR
) {
4211 for (i
= 0; i
< src
->num_members
; i
++) {
4212 ord
= __le32_to_cpu(src
->disk_ord_tbl
[i
]);
4213 set_imsm_ord_tbl_ent(src
, i
, ord_to_idx(ord
));
4217 if (migr_type
== MIGR_GEN_MIGR
)
4218 /* Clear migration record */
4219 memset(super
->migr_rec
, 0, sizeof(struct migr_record
));
4221 src
->map_state
= to_state
;
4224 static void end_migration(struct imsm_dev
*dev
, struct intel_super
*super
,
4227 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
4228 struct imsm_map
*prev
= get_imsm_map(dev
, dev
->vol
.migr_state
== 0 ?
4232 /* merge any IMSM_ORD_REBUILD bits that were not successfully
4233 * completed in the last migration.
4235 * FIXME add support for raid-level-migration
4237 if (map_state
!= map
->map_state
&& (is_gen_migration(dev
) == 0) &&
4238 prev
->map_state
!= IMSM_T_STATE_UNINITIALIZED
) {
4239 /* when final map state is other than expected
4240 * merge maps (not for migration)
4244 for (i
= 0; i
< prev
->num_members
; i
++)
4245 for (j
= 0; j
< map
->num_members
; j
++)
4246 /* during online capacity expansion
4247 * disks position can be changed
4248 * if takeover is used
4250 if (ord_to_idx(map
->disk_ord_tbl
[j
]) ==
4251 ord_to_idx(prev
->disk_ord_tbl
[i
])) {
4252 map
->disk_ord_tbl
[j
] |=
4253 prev
->disk_ord_tbl
[i
];
4256 failed
= imsm_count_failed(super
, dev
, MAP_0
);
4257 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
4260 dev
->vol
.migr_state
= 0;
4261 set_migr_type(dev
, 0);
4262 dev
->vol
.curr_migr_unit
= 0;
4263 map
->map_state
= map_state
;
4266 static int parse_raid_devices(struct intel_super
*super
)
4269 struct imsm_dev
*dev_new
;
4270 size_t len
, len_migr
;
4272 size_t space_needed
= 0;
4273 struct imsm_super
*mpb
= super
->anchor
;
4275 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
4276 struct imsm_dev
*dev_iter
= __get_imsm_dev(super
->anchor
, i
);
4277 struct intel_dev
*dv
;
4279 len
= sizeof_imsm_dev(dev_iter
, 0);
4280 len_migr
= sizeof_imsm_dev(dev_iter
, 1);
4282 space_needed
+= len_migr
- len
;
4284 dv
= xmalloc(sizeof(*dv
));
4285 if (max_len
< len_migr
)
4287 if (max_len
> len_migr
)
4288 space_needed
+= max_len
- len_migr
;
4289 dev_new
= xmalloc(max_len
);
4290 imsm_copy_dev(dev_new
, dev_iter
);
4293 dv
->next
= super
->devlist
;
4294 super
->devlist
= dv
;
4297 /* ensure that super->buf is large enough when all raid devices
4300 if (__le32_to_cpu(mpb
->mpb_size
) + space_needed
> super
->len
) {
4303 len
= ROUND_UP(__le32_to_cpu(mpb
->mpb_size
) + space_needed
,
4304 super
->sector_size
);
4305 if (posix_memalign(&buf
, MAX_SECTOR_SIZE
, len
) != 0)
4308 memcpy(buf
, super
->buf
, super
->len
);
4309 memset(buf
+ super
->len
, 0, len
- super
->len
);
4315 super
->extra_space
+= space_needed
;
4320 /*******************************************************************************
4321 * Function: check_mpb_migr_compatibility
4322 * Description: Function checks for unsupported migration features:
4323 * - migration optimization area (pba_of_lba0)
4324 * - descending reshape (ascending_migr)
4326 * super : imsm metadata information
4328 * 0 : migration is compatible
4329 * -1 : migration is not compatible
4330 ******************************************************************************/
4331 int check_mpb_migr_compatibility(struct intel_super
*super
)
4333 struct imsm_map
*map0
, *map1
;
4334 struct migr_record
*migr_rec
= super
->migr_rec
;
4337 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
4338 struct imsm_dev
*dev_iter
= __get_imsm_dev(super
->anchor
, i
);
4341 dev_iter
->vol
.migr_state
== 1 &&
4342 dev_iter
->vol
.migr_type
== MIGR_GEN_MIGR
) {
4343 /* This device is migrating */
4344 map0
= get_imsm_map(dev_iter
, MAP_0
);
4345 map1
= get_imsm_map(dev_iter
, MAP_1
);
4346 if (pba_of_lba0(map0
) != pba_of_lba0(map1
))
4347 /* migration optimization area was used */
4349 if (migr_rec
->ascending_migr
== 0 &&
4350 migr_rec
->dest_depth_per_unit
> 0)
4351 /* descending reshape not supported yet */
4358 static void __free_imsm(struct intel_super
*super
, int free_disks
);
4360 /* load_imsm_mpb - read matrix metadata
4361 * allocates super->mpb to be freed by free_imsm
4363 static int load_imsm_mpb(int fd
, struct intel_super
*super
, char *devname
)
4365 unsigned long long dsize
;
4366 unsigned long long sectors
;
4367 unsigned int sector_size
= super
->sector_size
;
4369 struct imsm_super
*anchor
;
4372 get_dev_size(fd
, NULL
, &dsize
);
4373 if (dsize
< 2*sector_size
) {
4375 pr_err("%s: device to small for imsm\n",
4380 if (lseek64(fd
, dsize
- (sector_size
* 2), SEEK_SET
) < 0) {
4382 pr_err("Cannot seek to anchor block on %s: %s\n",
4383 devname
, strerror(errno
));
4387 if (posix_memalign((void **)&anchor
, sector_size
, sector_size
) != 0) {
4389 pr_err("Failed to allocate imsm anchor buffer on %s\n", devname
);
4392 if ((unsigned int)read(fd
, anchor
, sector_size
) != sector_size
) {
4394 pr_err("Cannot read anchor block on %s: %s\n",
4395 devname
, strerror(errno
));
4400 if (strncmp((char *) anchor
->sig
, MPB_SIGNATURE
, MPB_SIG_LEN
) != 0) {
4402 pr_err("no IMSM anchor on %s\n", devname
);
4407 __free_imsm(super
, 0);
4408 /* reload capability and hba */
4410 /* capability and hba must be updated with new super allocation */
4411 find_intel_hba_capability(fd
, super
, devname
);
4412 super
->len
= ROUND_UP(anchor
->mpb_size
, sector_size
);
4413 if (posix_memalign(&super
->buf
, MAX_SECTOR_SIZE
, super
->len
) != 0) {
4415 pr_err("unable to allocate %zu byte mpb buffer\n",
4420 memcpy(super
->buf
, anchor
, sector_size
);
4422 sectors
= mpb_sectors(anchor
, sector_size
) - 1;
4425 if (posix_memalign(&super
->migr_rec_buf
, MAX_SECTOR_SIZE
,
4426 MIGR_REC_BUF_SECTORS
*MAX_SECTOR_SIZE
) != 0) {
4427 pr_err("could not allocate migr_rec buffer\n");
4431 super
->clean_migration_record_by_mdmon
= 0;
4434 check_sum
= __gen_imsm_checksum(super
->anchor
);
4435 if (check_sum
!= __le32_to_cpu(super
->anchor
->check_sum
)) {
4437 pr_err("IMSM checksum %x != %x on %s\n",
4439 __le32_to_cpu(super
->anchor
->check_sum
),
4447 /* read the extended mpb */
4448 if (lseek64(fd
, dsize
- (sector_size
* (2 + sectors
)), SEEK_SET
) < 0) {
4450 pr_err("Cannot seek to extended mpb on %s: %s\n",
4451 devname
, strerror(errno
));
4455 if ((unsigned int)read(fd
, super
->buf
+ sector_size
,
4456 super
->len
- sector_size
) != super
->len
- sector_size
) {
4458 pr_err("Cannot read extended mpb on %s: %s\n",
4459 devname
, strerror(errno
));
4463 check_sum
= __gen_imsm_checksum(super
->anchor
);
4464 if (check_sum
!= __le32_to_cpu(super
->anchor
->check_sum
)) {
4466 pr_err("IMSM checksum %x != %x on %s\n",
4467 check_sum
, __le32_to_cpu(super
->anchor
->check_sum
),
4475 static int read_imsm_migr_rec(int fd
, struct intel_super
*super
);
4477 /* clears hi bits in metadata if MPB_ATTRIB_2TB_DISK not set */
4478 static void clear_hi(struct intel_super
*super
)
4480 struct imsm_super
*mpb
= super
->anchor
;
4482 if (mpb
->attributes
& MPB_ATTRIB_2TB_DISK
)
4484 for (i
= 0; i
< mpb
->num_disks
; ++i
) {
4485 struct imsm_disk
*disk
= &mpb
->disk
[i
];
4486 disk
->total_blocks_hi
= 0;
4488 for (i
= 0; i
< mpb
->num_raid_devs
; ++i
) {
4489 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
4492 for (n
= 0; n
< 2; ++n
) {
4493 struct imsm_map
*map
= get_imsm_map(dev
, n
);
4496 map
->pba_of_lba0_hi
= 0;
4497 map
->blocks_per_member_hi
= 0;
4498 map
->num_data_stripes_hi
= 0;
4504 load_and_parse_mpb(int fd
, struct intel_super
*super
, char *devname
, int keep_fd
)
4508 err
= load_imsm_mpb(fd
, super
, devname
);
4511 if (super
->sector_size
== 4096)
4512 convert_from_4k(super
);
4513 err
= load_imsm_disk(fd
, super
, devname
, keep_fd
);
4516 err
= parse_raid_devices(super
);
4519 err
= load_bbm_log(super
);
4524 static void __free_imsm_disk(struct dl
*d
)
4536 static void free_imsm_disks(struct intel_super
*super
)
4540 while (super
->disks
) {
4542 super
->disks
= d
->next
;
4543 __free_imsm_disk(d
);
4545 while (super
->disk_mgmt_list
) {
4546 d
= super
->disk_mgmt_list
;
4547 super
->disk_mgmt_list
= d
->next
;
4548 __free_imsm_disk(d
);
4550 while (super
->missing
) {
4552 super
->missing
= d
->next
;
4553 __free_imsm_disk(d
);
4558 /* free all the pieces hanging off of a super pointer */
4559 static void __free_imsm(struct intel_super
*super
, int free_disks
)
4561 struct intel_hba
*elem
, *next
;
4567 /* unlink capability description */
4569 if (super
->migr_rec_buf
) {
4570 free(super
->migr_rec_buf
);
4571 super
->migr_rec_buf
= NULL
;
4574 free_imsm_disks(super
);
4575 free_devlist(super
);
4579 free((void *)elem
->path
);
4585 free(super
->bbm_log
);
4589 static void free_imsm(struct intel_super
*super
)
4591 __free_imsm(super
, 1);
4592 free(super
->bb
.entries
);
4596 static void free_super_imsm(struct supertype
*st
)
4598 struct intel_super
*super
= st
->sb
;
4607 static struct intel_super
*alloc_super(void)
4609 struct intel_super
*super
= xcalloc(1, sizeof(*super
));
4611 super
->current_vol
= -1;
4612 super
->create_offset
= ~((unsigned long long) 0);
4614 super
->bb
.entries
= xmalloc(BBM_LOG_MAX_ENTRIES
*
4615 sizeof(struct md_bb_entry
));
4616 if (!super
->bb
.entries
) {
4625 * find and allocate hba and OROM/EFI based on valid fd of RAID component device
4627 static int find_intel_hba_capability(int fd
, struct intel_super
*super
, char *devname
)
4629 struct sys_dev
*hba_name
;
4632 if (fd
>= 0 && test_partition(fd
)) {
4633 pr_err("imsm: %s is a partition, cannot be used in IMSM\n",
4637 if (fd
< 0 || check_env("IMSM_NO_PLATFORM")) {
4642 hba_name
= find_disk_attached_hba(fd
, NULL
);
4645 pr_err("%s is not attached to Intel(R) RAID controller.\n",
4649 rv
= attach_hba_to_super(super
, hba_name
);
4652 struct intel_hba
*hba
= super
->hba
;
4654 pr_err("%s is attached to Intel(R) %s %s (%s),\n"
4655 " but the container is assigned to Intel(R) %s %s (",
4657 get_sys_dev_type(hba_name
->type
),
4658 hba_name
->type
== SYS_DEV_VMD
? "domain" : "RAID controller",
4659 hba_name
->pci_id
? : "Err!",
4660 get_sys_dev_type(super
->hba
->type
),
4661 hba
->type
== SYS_DEV_VMD
? "domain" : "RAID controller");
4664 fprintf(stderr
, "%s", hba
->pci_id
? : "Err!");
4666 fprintf(stderr
, ", ");
4669 fprintf(stderr
, ").\n"
4670 " Mixing devices attached to different controllers is not allowed.\n");
4674 super
->orom
= find_imsm_capability(hba_name
);
4681 /* find_missing - helper routine for load_super_imsm_all that identifies
4682 * disks that have disappeared from the system. This routine relies on
4683 * the mpb being uptodate, which it is at load time.
4685 static int find_missing(struct intel_super
*super
)
4688 struct imsm_super
*mpb
= super
->anchor
;
4690 struct imsm_disk
*disk
;
4692 for (i
= 0; i
< mpb
->num_disks
; i
++) {
4693 disk
= __get_imsm_disk(mpb
, i
);
4694 dl
= serial_to_dl(disk
->serial
, super
);
4698 dl
= xmalloc(sizeof(*dl
));
4702 dl
->devname
= xstrdup("missing");
4704 serialcpy(dl
->serial
, disk
->serial
);
4707 dl
->next
= super
->missing
;
4708 super
->missing
= dl
;
4714 static struct intel_disk
*disk_list_get(__u8
*serial
, struct intel_disk
*disk_list
)
4716 struct intel_disk
*idisk
= disk_list
;
4719 if (serialcmp(idisk
->disk
.serial
, serial
) == 0)
4721 idisk
= idisk
->next
;
4727 static int __prep_thunderdome(struct intel_super
**table
, int tbl_size
,
4728 struct intel_super
*super
,
4729 struct intel_disk
**disk_list
)
4731 struct imsm_disk
*d
= &super
->disks
->disk
;
4732 struct imsm_super
*mpb
= super
->anchor
;
4735 for (i
= 0; i
< tbl_size
; i
++) {
4736 struct imsm_super
*tbl_mpb
= table
[i
]->anchor
;
4737 struct imsm_disk
*tbl_d
= &table
[i
]->disks
->disk
;
4739 if (tbl_mpb
->family_num
== mpb
->family_num
) {
4740 if (tbl_mpb
->check_sum
== mpb
->check_sum
) {
4741 dprintf("mpb from %d:%d matches %d:%d\n",
4742 super
->disks
->major
,
4743 super
->disks
->minor
,
4744 table
[i
]->disks
->major
,
4745 table
[i
]->disks
->minor
);
4749 if (((is_configured(d
) && !is_configured(tbl_d
)) ||
4750 is_configured(d
) == is_configured(tbl_d
)) &&
4751 tbl_mpb
->generation_num
< mpb
->generation_num
) {
4752 /* current version of the mpb is a
4753 * better candidate than the one in
4754 * super_table, but copy over "cross
4755 * generational" status
4757 struct intel_disk
*idisk
;
4759 dprintf("mpb from %d:%d replaces %d:%d\n",
4760 super
->disks
->major
,
4761 super
->disks
->minor
,
4762 table
[i
]->disks
->major
,
4763 table
[i
]->disks
->minor
);
4765 idisk
= disk_list_get(tbl_d
->serial
, *disk_list
);
4766 if (idisk
&& is_failed(&idisk
->disk
))
4767 tbl_d
->status
|= FAILED_DISK
;
4770 struct intel_disk
*idisk
;
4771 struct imsm_disk
*disk
;
4773 /* tbl_mpb is more up to date, but copy
4774 * over cross generational status before
4777 disk
= __serial_to_disk(d
->serial
, mpb
, NULL
);
4778 if (disk
&& is_failed(disk
))
4779 d
->status
|= FAILED_DISK
;
4781 idisk
= disk_list_get(d
->serial
, *disk_list
);
4784 if (disk
&& is_configured(disk
))
4785 idisk
->disk
.status
|= CONFIGURED_DISK
;
4788 dprintf("mpb from %d:%d prefer %d:%d\n",
4789 super
->disks
->major
,
4790 super
->disks
->minor
,
4791 table
[i
]->disks
->major
,
4792 table
[i
]->disks
->minor
);
4800 table
[tbl_size
++] = super
;
4804 /* update/extend the merged list of imsm_disk records */
4805 for (j
= 0; j
< mpb
->num_disks
; j
++) {
4806 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, j
);
4807 struct intel_disk
*idisk
;
4809 idisk
= disk_list_get(disk
->serial
, *disk_list
);
4811 idisk
->disk
.status
|= disk
->status
;
4812 if (is_configured(&idisk
->disk
) ||
4813 is_failed(&idisk
->disk
))
4814 idisk
->disk
.status
&= ~(SPARE_DISK
);
4816 idisk
= xcalloc(1, sizeof(*idisk
));
4817 idisk
->owner
= IMSM_UNKNOWN_OWNER
;
4818 idisk
->disk
= *disk
;
4819 idisk
->next
= *disk_list
;
4823 if (serialcmp(idisk
->disk
.serial
, d
->serial
) == 0)
4830 static struct intel_super
*
4831 validate_members(struct intel_super
*super
, struct intel_disk
*disk_list
,
4834 struct imsm_super
*mpb
= super
->anchor
;
4838 for (i
= 0; i
< mpb
->num_disks
; i
++) {
4839 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, i
);
4840 struct intel_disk
*idisk
;
4842 idisk
= disk_list_get(disk
->serial
, disk_list
);
4844 if (idisk
->owner
== owner
||
4845 idisk
->owner
== IMSM_UNKNOWN_OWNER
)
4848 dprintf("'%.16s' owner %d != %d\n",
4849 disk
->serial
, idisk
->owner
,
4852 dprintf("unknown disk %x [%d]: %.16s\n",
4853 __le32_to_cpu(mpb
->family_num
), i
,
4859 if (ok_count
== mpb
->num_disks
)
4864 static void show_conflicts(__u32 family_num
, struct intel_super
*super_list
)
4866 struct intel_super
*s
;
4868 for (s
= super_list
; s
; s
= s
->next
) {
4869 if (family_num
!= s
->anchor
->family_num
)
4871 pr_err("Conflict, offlining family %#x on '%s'\n",
4872 __le32_to_cpu(family_num
), s
->disks
->devname
);
4876 static struct intel_super
*
4877 imsm_thunderdome(struct intel_super
**super_list
, int len
)
4879 struct intel_super
*super_table
[len
];
4880 struct intel_disk
*disk_list
= NULL
;
4881 struct intel_super
*champion
, *spare
;
4882 struct intel_super
*s
, **del
;
4887 memset(super_table
, 0, sizeof(super_table
));
4888 for (s
= *super_list
; s
; s
= s
->next
)
4889 tbl_size
= __prep_thunderdome(super_table
, tbl_size
, s
, &disk_list
);
4891 for (i
= 0; i
< tbl_size
; i
++) {
4892 struct imsm_disk
*d
;
4893 struct intel_disk
*idisk
;
4894 struct imsm_super
*mpb
= super_table
[i
]->anchor
;
4897 d
= &s
->disks
->disk
;
4899 /* 'd' must appear in merged disk list for its
4900 * configuration to be valid
4902 idisk
= disk_list_get(d
->serial
, disk_list
);
4903 if (idisk
&& idisk
->owner
== i
)
4904 s
= validate_members(s
, disk_list
, i
);
4909 dprintf("marking family: %#x from %d:%d offline\n",
4911 super_table
[i
]->disks
->major
,
4912 super_table
[i
]->disks
->minor
);
4916 /* This is where the mdadm implementation differs from the Windows
4917 * driver which has no strict concept of a container. We can only
4918 * assemble one family from a container, so when returning a prodigal
4919 * array member to this system the code will not be able to disambiguate
4920 * the container contents that should be assembled ("foreign" versus
4921 * "local"). It requires user intervention to set the orig_family_num
4922 * to a new value to establish a new container. The Windows driver in
4923 * this situation fixes up the volume name in place and manages the
4924 * foreign array as an independent entity.
4929 for (i
= 0; i
< tbl_size
; i
++) {
4930 struct intel_super
*tbl_ent
= super_table
[i
];
4936 if (tbl_ent
->anchor
->num_raid_devs
== 0) {
4941 if (s
&& !is_spare
) {
4942 show_conflicts(tbl_ent
->anchor
->family_num
, *super_list
);
4944 } else if (!s
&& !is_spare
)
4957 pr_err("Chose family %#x on '%s', assemble conflicts to new container with '--update=uuid'\n",
4958 __le32_to_cpu(s
->anchor
->family_num
), s
->disks
->devname
);
4960 /* collect all dl's onto 'champion', and update them to
4961 * champion's version of the status
4963 for (s
= *super_list
; s
; s
= s
->next
) {
4964 struct imsm_super
*mpb
= champion
->anchor
;
4965 struct dl
*dl
= s
->disks
;
4970 mpb
->attributes
|= s
->anchor
->attributes
& MPB_ATTRIB_2TB_DISK
;
4972 for (i
= 0; i
< mpb
->num_disks
; i
++) {
4973 struct imsm_disk
*disk
;
4975 disk
= __serial_to_disk(dl
->serial
, mpb
, &dl
->index
);
4978 /* only set index on disks that are a member of
4979 * a populated contianer, i.e. one with
4982 if (is_failed(&dl
->disk
))
4984 else if (is_spare(&dl
->disk
))
4990 if (i
>= mpb
->num_disks
) {
4991 struct intel_disk
*idisk
;
4993 idisk
= disk_list_get(dl
->serial
, disk_list
);
4994 if (idisk
&& is_spare(&idisk
->disk
) &&
4995 !is_failed(&idisk
->disk
) && !is_configured(&idisk
->disk
))
5003 dl
->next
= champion
->disks
;
5004 champion
->disks
= dl
;
5008 /* delete 'champion' from super_list */
5009 for (del
= super_list
; *del
; ) {
5010 if (*del
== champion
) {
5011 *del
= (*del
)->next
;
5014 del
= &(*del
)->next
;
5016 champion
->next
= NULL
;
5020 struct intel_disk
*idisk
= disk_list
;
5022 disk_list
= disk_list
->next
;
5030 get_sra_super_block(int fd
, struct intel_super
**super_list
, char *devname
, int *max
, int keep_fd
);
5031 static int get_super_block(struct intel_super
**super_list
, char *devnm
, char *devname
,
5032 int major
, int minor
, int keep_fd
);
5034 get_devlist_super_block(struct md_list
*devlist
, struct intel_super
**super_list
,
5035 int *max
, int keep_fd
);
5037 static int load_super_imsm_all(struct supertype
*st
, int fd
, void **sbp
,
5038 char *devname
, struct md_list
*devlist
,
5041 struct intel_super
*super_list
= NULL
;
5042 struct intel_super
*super
= NULL
;
5047 /* 'fd' is an opened container */
5048 err
= get_sra_super_block(fd
, &super_list
, devname
, &i
, keep_fd
);
5050 /* get super block from devlist devices */
5051 err
= get_devlist_super_block(devlist
, &super_list
, &i
, keep_fd
);
5054 /* all mpbs enter, maybe one leaves */
5055 super
= imsm_thunderdome(&super_list
, i
);
5061 if (find_missing(super
) != 0) {
5067 /* load migration record */
5068 err
= load_imsm_migr_rec(super
, NULL
);
5070 /* migration is in progress,
5071 * but migr_rec cannot be loaded,
5077 /* Check migration compatibility */
5078 if (err
== 0 && check_mpb_migr_compatibility(super
) != 0) {
5079 pr_err("Unsupported migration detected");
5081 fprintf(stderr
, " on %s\n", devname
);
5083 fprintf(stderr
, " (IMSM).\n");
5092 while (super_list
) {
5093 struct intel_super
*s
= super_list
;
5095 super_list
= super_list
->next
;
5104 strcpy(st
->container_devnm
, fd2devnm(fd
));
5106 st
->container_devnm
[0] = 0;
5107 if (err
== 0 && st
->ss
== NULL
) {
5108 st
->ss
= &super_imsm
;
5109 st
->minor_version
= 0;
5110 st
->max_devs
= IMSM_MAX_DEVICES
;
5116 get_devlist_super_block(struct md_list
*devlist
, struct intel_super
**super_list
,
5117 int *max
, int keep_fd
)
5119 struct md_list
*tmpdev
;
5123 for (i
= 0, tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
5124 if (tmpdev
->used
!= 1)
5126 if (tmpdev
->container
== 1) {
5128 int fd
= dev_open(tmpdev
->devname
, O_RDONLY
|O_EXCL
);
5130 pr_err("cannot open device %s: %s\n",
5131 tmpdev
->devname
, strerror(errno
));
5135 err
= get_sra_super_block(fd
, super_list
,
5136 tmpdev
->devname
, &lmax
,
5145 int major
= major(tmpdev
->st_rdev
);
5146 int minor
= minor(tmpdev
->st_rdev
);
5147 err
= get_super_block(super_list
,
5164 static int get_super_block(struct intel_super
**super_list
, char *devnm
, char *devname
,
5165 int major
, int minor
, int keep_fd
)
5167 struct intel_super
*s
;
5179 sprintf(nm
, "%d:%d", major
, minor
);
5180 dfd
= dev_open(nm
, O_RDWR
);
5186 get_dev_sector_size(dfd
, NULL
, &s
->sector_size
);
5187 find_intel_hba_capability(dfd
, s
, devname
);
5188 err
= load_and_parse_mpb(dfd
, s
, NULL
, keep_fd
);
5190 /* retry the load if we might have raced against mdmon */
5191 if (err
== 3 && devnm
&& mdmon_running(devnm
))
5192 for (retry
= 0; retry
< 3; retry
++) {
5194 err
= load_and_parse_mpb(dfd
, s
, NULL
, keep_fd
);
5200 s
->next
= *super_list
;
5208 if (dfd
>= 0 && !keep_fd
)
5215 get_sra_super_block(int fd
, struct intel_super
**super_list
, char *devname
, int *max
, int keep_fd
)
5222 sra
= sysfs_read(fd
, NULL
, GET_LEVEL
|GET_VERSION
|GET_DEVS
|GET_STATE
);
5226 if (sra
->array
.major_version
!= -1 ||
5227 sra
->array
.minor_version
!= -2 ||
5228 strcmp(sra
->text_version
, "imsm") != 0) {
5233 devnm
= fd2devnm(fd
);
5234 for (sd
= sra
->devs
, i
= 0; sd
; sd
= sd
->next
, i
++) {
5235 if (get_super_block(super_list
, devnm
, devname
,
5236 sd
->disk
.major
, sd
->disk
.minor
, keep_fd
) != 0) {
5247 static int load_container_imsm(struct supertype
*st
, int fd
, char *devname
)
5249 return load_super_imsm_all(st
, fd
, &st
->sb
, devname
, NULL
, 1);
5252 static int load_super_imsm(struct supertype
*st
, int fd
, char *devname
)
5254 struct intel_super
*super
;
5258 if (test_partition(fd
))
5259 /* IMSM not allowed on partitions */
5262 free_super_imsm(st
);
5264 super
= alloc_super();
5265 get_dev_sector_size(fd
, NULL
, &super
->sector_size
);
5268 /* Load hba and capabilities if they exist.
5269 * But do not preclude loading metadata in case capabilities or hba are
5270 * non-compliant and ignore_hw_compat is set.
5272 rv
= find_intel_hba_capability(fd
, super
, devname
);
5273 /* no orom/efi or non-intel hba of the disk */
5274 if (rv
!= 0 && st
->ignore_hw_compat
== 0) {
5276 pr_err("No OROM/EFI properties for %s\n", devname
);
5280 rv
= load_and_parse_mpb(fd
, super
, devname
, 0);
5282 /* retry the load if we might have raced against mdmon */
5284 struct mdstat_ent
*mdstat
= NULL
;
5285 char *name
= fd2kname(fd
);
5288 mdstat
= mdstat_by_component(name
);
5290 if (mdstat
&& mdmon_running(mdstat
->devnm
) && getpid() != mdmon_pid(mdstat
->devnm
)) {
5291 for (retry
= 0; retry
< 3; retry
++) {
5293 rv
= load_and_parse_mpb(fd
, super
, devname
, 0);
5299 free_mdstat(mdstat
);
5304 pr_err("Failed to load all information sections on %s\n", devname
);
5310 if (st
->ss
== NULL
) {
5311 st
->ss
= &super_imsm
;
5312 st
->minor_version
= 0;
5313 st
->max_devs
= IMSM_MAX_DEVICES
;
5316 /* load migration record */
5317 if (load_imsm_migr_rec(super
, NULL
) == 0) {
5318 /* Check for unsupported migration features */
5319 if (check_mpb_migr_compatibility(super
) != 0) {
5320 pr_err("Unsupported migration detected");
5322 fprintf(stderr
, " on %s\n", devname
);
5324 fprintf(stderr
, " (IMSM).\n");
5332 static __u16
info_to_blocks_per_strip(mdu_array_info_t
*info
)
5334 if (info
->level
== 1)
5336 return info
->chunk_size
>> 9;
5339 static unsigned long long info_to_blocks_per_member(mdu_array_info_t
*info
,
5340 unsigned long long size
)
5342 if (info
->level
== 1)
5345 return (size
* 2) & ~(info_to_blocks_per_strip(info
) - 1);
5348 static void imsm_update_version_info(struct intel_super
*super
)
5350 /* update the version and attributes */
5351 struct imsm_super
*mpb
= super
->anchor
;
5353 struct imsm_dev
*dev
;
5354 struct imsm_map
*map
;
5357 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
5358 dev
= get_imsm_dev(super
, i
);
5359 map
= get_imsm_map(dev
, MAP_0
);
5360 if (__le32_to_cpu(dev
->size_high
) > 0)
5361 mpb
->attributes
|= MPB_ATTRIB_2TB
;
5363 /* FIXME detect when an array spans a port multiplier */
5365 mpb
->attributes
|= MPB_ATTRIB_PM
;
5368 if (mpb
->num_raid_devs
> 1 ||
5369 mpb
->attributes
!= MPB_ATTRIB_CHECKSUM_VERIFY
) {
5370 version
= MPB_VERSION_ATTRIBS
;
5371 switch (get_imsm_raid_level(map
)) {
5372 case 0: mpb
->attributes
|= MPB_ATTRIB_RAID0
; break;
5373 case 1: mpb
->attributes
|= MPB_ATTRIB_RAID1
; break;
5374 case 10: mpb
->attributes
|= MPB_ATTRIB_RAID10
; break;
5375 case 5: mpb
->attributes
|= MPB_ATTRIB_RAID5
; break;
5378 if (map
->num_members
>= 5)
5379 version
= MPB_VERSION_5OR6_DISK_ARRAY
;
5380 else if (dev
->status
== DEV_CLONE_N_GO
)
5381 version
= MPB_VERSION_CNG
;
5382 else if (get_imsm_raid_level(map
) == 5)
5383 version
= MPB_VERSION_RAID5
;
5384 else if (map
->num_members
>= 3)
5385 version
= MPB_VERSION_3OR4_DISK_ARRAY
;
5386 else if (get_imsm_raid_level(map
) == 1)
5387 version
= MPB_VERSION_RAID1
;
5389 version
= MPB_VERSION_RAID0
;
5391 strcpy(((char *) mpb
->sig
) + strlen(MPB_SIGNATURE
), version
);
5395 static int check_name(struct intel_super
*super
, char *name
, int quiet
)
5397 struct imsm_super
*mpb
= super
->anchor
;
5398 char *reason
= NULL
;
5400 size_t len
= strlen(name
);
5404 while (isspace(start
[len
- 1]))
5406 while (*start
&& isspace(*start
))
5408 memmove(name
, start
, len
+ 1);
5411 if (len
> MAX_RAID_SERIAL_LEN
)
5412 reason
= "must be 16 characters or less";
5414 reason
= "must be a non-empty string";
5416 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
5417 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
5419 if (strncmp((char *) dev
->volume
, name
, MAX_RAID_SERIAL_LEN
) == 0) {
5420 reason
= "already exists";
5425 if (reason
&& !quiet
)
5426 pr_err("imsm volume name %s\n", reason
);
5431 static int init_super_imsm_volume(struct supertype
*st
, mdu_array_info_t
*info
,
5432 struct shape
*s
, char *name
,
5433 char *homehost
, int *uuid
,
5434 long long data_offset
)
5436 /* We are creating a volume inside a pre-existing container.
5437 * so st->sb is already set.
5439 struct intel_super
*super
= st
->sb
;
5440 unsigned int sector_size
= super
->sector_size
;
5441 struct imsm_super
*mpb
= super
->anchor
;
5442 struct intel_dev
*dv
;
5443 struct imsm_dev
*dev
;
5444 struct imsm_vol
*vol
;
5445 struct imsm_map
*map
;
5446 int idx
= mpb
->num_raid_devs
;
5449 unsigned long long array_blocks
;
5450 size_t size_old
, size_new
;
5451 unsigned long long num_data_stripes
;
5452 unsigned int data_disks
;
5453 unsigned long long size_per_member
;
5455 if (super
->orom
&& mpb
->num_raid_devs
>= super
->orom
->vpa
) {
5456 pr_err("This imsm-container already has the maximum of %d volumes\n", super
->orom
->vpa
);
5460 /* ensure the mpb is large enough for the new data */
5461 size_old
= __le32_to_cpu(mpb
->mpb_size
);
5462 size_new
= disks_to_mpb_size(info
->nr_disks
);
5463 if (size_new
> size_old
) {
5465 size_t size_round
= ROUND_UP(size_new
, sector_size
);
5467 if (posix_memalign(&mpb_new
, sector_size
, size_round
) != 0) {
5468 pr_err("could not allocate new mpb\n");
5471 if (posix_memalign(&super
->migr_rec_buf
, MAX_SECTOR_SIZE
,
5472 MIGR_REC_BUF_SECTORS
*
5473 MAX_SECTOR_SIZE
) != 0) {
5474 pr_err("could not allocate migr_rec buffer\n");
5480 memcpy(mpb_new
, mpb
, size_old
);
5483 super
->anchor
= mpb_new
;
5484 mpb
->mpb_size
= __cpu_to_le32(size_new
);
5485 memset(mpb_new
+ size_old
, 0, size_round
- size_old
);
5486 super
->len
= size_round
;
5488 super
->current_vol
= idx
;
5490 /* handle 'failed_disks' by either:
5491 * a) create dummy disk entries in the table if this the first
5492 * volume in the array. We add them here as this is the only
5493 * opportunity to add them. add_to_super_imsm_volume()
5494 * handles the non-failed disks and continues incrementing
5496 * b) validate that 'failed_disks' matches the current number
5497 * of missing disks if the container is populated
5499 if (super
->current_vol
== 0) {
5501 for (i
= 0; i
< info
->failed_disks
; i
++) {
5502 struct imsm_disk
*disk
;
5505 disk
= __get_imsm_disk(mpb
, i
);
5506 disk
->status
= CONFIGURED_DISK
| FAILED_DISK
;
5507 disk
->scsi_id
= __cpu_to_le32(~(__u32
)0);
5508 snprintf((char *) disk
->serial
, MAX_RAID_SERIAL_LEN
,
5509 "missing:%d", (__u8
)i
);
5511 find_missing(super
);
5516 for (d
= super
->missing
; d
; d
= d
->next
)
5518 if (info
->failed_disks
> missing
) {
5519 pr_err("unable to add 'missing' disk to container\n");
5524 if (!check_name(super
, name
, 0))
5526 dv
= xmalloc(sizeof(*dv
));
5527 dev
= xcalloc(1, sizeof(*dev
) + sizeof(__u32
) * (info
->raid_disks
- 1));
5529 * Explicitly allow truncating to not confuse gcc's
5530 * -Werror=stringop-truncation
5532 namelen
= min((int) strlen(name
), MAX_RAID_SERIAL_LEN
);
5533 memcpy(dev
->volume
, name
, namelen
);
5534 array_blocks
= calc_array_size(info
->level
, info
->raid_disks
,
5535 info
->layout
, info
->chunk_size
,
5536 s
->size
* BLOCKS_PER_KB
);
5537 data_disks
= get_data_disks(info
->level
, info
->layout
,
5539 array_blocks
= round_size_to_mb(array_blocks
, data_disks
);
5540 size_per_member
= array_blocks
/ data_disks
;
5542 set_imsm_dev_size(dev
, array_blocks
);
5543 dev
->status
= (DEV_READ_COALESCING
| DEV_WRITE_COALESCING
);
5545 vol
->migr_state
= 0;
5546 set_migr_type(dev
, MIGR_INIT
);
5547 vol
->dirty
= !info
->state
;
5548 vol
->curr_migr_unit
= 0;
5549 map
= get_imsm_map(dev
, MAP_0
);
5550 set_pba_of_lba0(map
, super
->create_offset
);
5551 map
->blocks_per_strip
= __cpu_to_le16(info_to_blocks_per_strip(info
));
5552 map
->failed_disk_num
= ~0;
5553 if (info
->level
> 0)
5554 map
->map_state
= (info
->state
? IMSM_T_STATE_NORMAL
5555 : IMSM_T_STATE_UNINITIALIZED
);
5557 map
->map_state
= info
->failed_disks
? IMSM_T_STATE_FAILED
:
5558 IMSM_T_STATE_NORMAL
;
5561 if (info
->level
== 1 && info
->raid_disks
> 2) {
5564 pr_err("imsm does not support more than 2 disksin a raid1 volume\n");
5568 map
->raid_level
= info
->level
;
5569 if (info
->level
== 10) {
5570 map
->raid_level
= 1;
5571 map
->num_domains
= info
->raid_disks
/ 2;
5572 } else if (info
->level
== 1)
5573 map
->num_domains
= info
->raid_disks
;
5575 map
->num_domains
= 1;
5577 /* info->size is only int so use the 'size' parameter instead */
5578 num_data_stripes
= size_per_member
/ info_to_blocks_per_strip(info
);
5579 num_data_stripes
/= map
->num_domains
;
5580 set_num_data_stripes(map
, num_data_stripes
);
5582 size_per_member
+= NUM_BLOCKS_DIRTY_STRIPE_REGION
;
5583 set_blocks_per_member(map
, info_to_blocks_per_member(info
,
5587 map
->num_members
= info
->raid_disks
;
5588 for (i
= 0; i
< map
->num_members
; i
++) {
5589 /* initialized in add_to_super */
5590 set_imsm_ord_tbl_ent(map
, i
, IMSM_ORD_REBUILD
);
5592 mpb
->num_raid_devs
++;
5593 mpb
->num_raid_devs_created
++;
5594 dev
->my_vol_raid_dev_num
= mpb
->num_raid_devs_created
;
5596 if (s
->consistency_policy
<= CONSISTENCY_POLICY_RESYNC
) {
5597 dev
->rwh_policy
= RWH_MULTIPLE_OFF
;
5598 } else if (s
->consistency_policy
== CONSISTENCY_POLICY_PPL
) {
5599 dev
->rwh_policy
= RWH_MULTIPLE_DISTRIBUTED
;
5603 pr_err("imsm does not support consistency policy %s\n",
5604 map_num(consistency_policies
, s
->consistency_policy
));
5609 dv
->index
= super
->current_vol
;
5610 dv
->next
= super
->devlist
;
5611 super
->devlist
= dv
;
5613 imsm_update_version_info(super
);
5618 static int init_super_imsm(struct supertype
*st
, mdu_array_info_t
*info
,
5619 struct shape
*s
, char *name
,
5620 char *homehost
, int *uuid
,
5621 unsigned long long data_offset
)
5623 /* This is primarily called by Create when creating a new array.
5624 * We will then get add_to_super called for each component, and then
5625 * write_init_super called to write it out to each device.
5626 * For IMSM, Create can create on fresh devices or on a pre-existing
5628 * To create on a pre-existing array a different method will be called.
5629 * This one is just for fresh drives.
5631 struct intel_super
*super
;
5632 struct imsm_super
*mpb
;
5636 if (data_offset
!= INVALID_SECTORS
) {
5637 pr_err("data-offset not supported by imsm\n");
5642 return init_super_imsm_volume(st
, info
, s
, name
, homehost
, uuid
,
5646 mpb_size
= disks_to_mpb_size(info
->nr_disks
);
5648 mpb_size
= MAX_SECTOR_SIZE
;
5650 super
= alloc_super();
5652 posix_memalign(&super
->buf
, MAX_SECTOR_SIZE
, mpb_size
) != 0) {
5657 pr_err("could not allocate superblock\n");
5660 if (posix_memalign(&super
->migr_rec_buf
, MAX_SECTOR_SIZE
,
5661 MIGR_REC_BUF_SECTORS
*MAX_SECTOR_SIZE
) != 0) {
5662 pr_err("could not allocate migr_rec buffer\n");
5667 memset(super
->buf
, 0, mpb_size
);
5669 mpb
->mpb_size
= __cpu_to_le32(mpb_size
);
5673 /* zeroing superblock */
5677 mpb
->attributes
= MPB_ATTRIB_CHECKSUM_VERIFY
;
5679 version
= (char *) mpb
->sig
;
5680 strcpy(version
, MPB_SIGNATURE
);
5681 version
+= strlen(MPB_SIGNATURE
);
5682 strcpy(version
, MPB_VERSION_RAID0
);
5687 static int drive_validate_sector_size(struct intel_super
*super
, struct dl
*dl
)
5689 unsigned int member_sector_size
;
5692 pr_err("Invalid file descriptor for %s\n", dl
->devname
);
5696 if (!get_dev_sector_size(dl
->fd
, dl
->devname
, &member_sector_size
))
5698 if (member_sector_size
!= super
->sector_size
)
5703 static int add_to_super_imsm_volume(struct supertype
*st
, mdu_disk_info_t
*dk
,
5704 int fd
, char *devname
)
5706 struct intel_super
*super
= st
->sb
;
5707 struct imsm_super
*mpb
= super
->anchor
;
5708 struct imsm_disk
*_disk
;
5709 struct imsm_dev
*dev
;
5710 struct imsm_map
*map
;
5714 dev
= get_imsm_dev(super
, super
->current_vol
);
5715 map
= get_imsm_map(dev
, MAP_0
);
5717 if (! (dk
->state
& (1<<MD_DISK_SYNC
))) {
5718 pr_err("%s: Cannot add spare devices to IMSM volume\n",
5724 /* we're doing autolayout so grab the pre-marked (in
5725 * validate_geometry) raid_disk
5727 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
5728 if (dl
->raiddisk
== dk
->raid_disk
)
5731 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
5732 if (dl
->major
== dk
->major
&&
5733 dl
->minor
== dk
->minor
)
5738 pr_err("%s is not a member of the same container\n", devname
);
5742 if (mpb
->num_disks
== 0)
5743 if (!get_dev_sector_size(dl
->fd
, dl
->devname
,
5744 &super
->sector_size
))
5747 if (!drive_validate_sector_size(super
, dl
)) {
5748 pr_err("Combining drives of different sector size in one volume is not allowed\n");
5752 /* add a pristine spare to the metadata */
5753 if (dl
->index
< 0) {
5754 dl
->index
= super
->anchor
->num_disks
;
5755 super
->anchor
->num_disks
++;
5757 /* Check the device has not already been added */
5758 slot
= get_imsm_disk_slot(map
, dl
->index
);
5760 (get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
) & IMSM_ORD_REBUILD
) == 0) {
5761 pr_err("%s has been included in this array twice\n",
5765 set_imsm_ord_tbl_ent(map
, dk
->raid_disk
, dl
->index
);
5766 dl
->disk
.status
= CONFIGURED_DISK
;
5768 /* update size of 'missing' disks to be at least as large as the
5769 * largest acitve member (we only have dummy missing disks when
5770 * creating the first volume)
5772 if (super
->current_vol
== 0) {
5773 for (df
= super
->missing
; df
; df
= df
->next
) {
5774 if (total_blocks(&dl
->disk
) > total_blocks(&df
->disk
))
5775 set_total_blocks(&df
->disk
, total_blocks(&dl
->disk
));
5776 _disk
= __get_imsm_disk(mpb
, df
->index
);
5781 /* refresh unset/failed slots to point to valid 'missing' entries */
5782 for (df
= super
->missing
; df
; df
= df
->next
)
5783 for (slot
= 0; slot
< mpb
->num_disks
; slot
++) {
5784 __u32 ord
= get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
);
5786 if ((ord
& IMSM_ORD_REBUILD
) == 0)
5788 set_imsm_ord_tbl_ent(map
, slot
, df
->index
| IMSM_ORD_REBUILD
);
5789 if (is_gen_migration(dev
)) {
5790 struct imsm_map
*map2
= get_imsm_map(dev
,
5792 int slot2
= get_imsm_disk_slot(map2
, df
->index
);
5793 if (slot2
< map2
->num_members
&& slot2
>= 0) {
5794 __u32 ord2
= get_imsm_ord_tbl_ent(dev
,
5797 if ((unsigned)df
->index
==
5799 set_imsm_ord_tbl_ent(map2
,
5805 dprintf("set slot:%d to missing disk:%d\n", slot
, df
->index
);
5809 /* if we are creating the first raid device update the family number */
5810 if (super
->current_vol
== 0) {
5812 struct imsm_dev
*_dev
= __get_imsm_dev(mpb
, 0);
5814 _disk
= __get_imsm_disk(mpb
, dl
->index
);
5815 if (!_dev
|| !_disk
) {
5816 pr_err("BUG mpb setup error\n");
5822 sum
+= __gen_imsm_checksum(mpb
);
5823 mpb
->family_num
= __cpu_to_le32(sum
);
5824 mpb
->orig_family_num
= mpb
->family_num
;
5826 super
->current_disk
= dl
;
5831 * Function marks disk as spare and restores disk serial
5832 * in case it was previously marked as failed by takeover operation
5834 * -1 : critical error
5835 * 0 : disk is marked as spare but serial is not set
5838 int mark_spare(struct dl
*disk
)
5840 __u8 serial
[MAX_RAID_SERIAL_LEN
];
5847 if (!imsm_read_serial(disk
->fd
, NULL
, serial
)) {
5848 /* Restore disk serial number, because takeover marks disk
5849 * as failed and adds to serial ':0' before it becomes
5852 serialcpy(disk
->serial
, serial
);
5853 serialcpy(disk
->disk
.serial
, serial
);
5856 disk
->disk
.status
= SPARE_DISK
;
5862 static int add_to_super_imsm(struct supertype
*st
, mdu_disk_info_t
*dk
,
5863 int fd
, char *devname
,
5864 unsigned long long data_offset
)
5866 struct intel_super
*super
= st
->sb
;
5868 unsigned long long size
;
5869 unsigned int member_sector_size
;
5874 /* If we are on an RAID enabled platform check that the disk is
5875 * attached to the raid controller.
5876 * We do not need to test disks attachment for container based additions,
5877 * they shall be already tested when container was created/assembled.
5879 rv
= find_intel_hba_capability(fd
, super
, devname
);
5880 /* no orom/efi or non-intel hba of the disk */
5882 dprintf("capability: %p fd: %d ret: %d\n",
5883 super
->orom
, fd
, rv
);
5887 if (super
->current_vol
>= 0)
5888 return add_to_super_imsm_volume(st
, dk
, fd
, devname
);
5891 dd
= xcalloc(sizeof(*dd
), 1);
5892 dd
->major
= major(stb
.st_rdev
);
5893 dd
->minor
= minor(stb
.st_rdev
);
5894 dd
->devname
= devname
? xstrdup(devname
) : NULL
;
5897 dd
->action
= DISK_ADD
;
5898 rv
= imsm_read_serial(fd
, devname
, dd
->serial
);
5900 pr_err("failed to retrieve scsi serial, aborting\n");
5906 if (super
->hba
&& ((super
->hba
->type
== SYS_DEV_NVME
) ||
5907 (super
->hba
->type
== SYS_DEV_VMD
))) {
5909 char *devpath
= diskfd_to_devpath(fd
);
5910 char controller_path
[PATH_MAX
];
5913 pr_err("failed to get devpath, aborting\n");
5920 snprintf(controller_path
, PATH_MAX
-1, "%s/device", devpath
);
5923 if (devpath_to_vendor(controller_path
) == 0x8086) {
5925 * If Intel's NVMe drive has serial ended with
5926 * "-A","-B","-1" or "-2" it means that this is "x8"
5927 * device (double drive on single PCIe card).
5928 * User should be warned about potential data loss.
5930 for (i
= MAX_RAID_SERIAL_LEN
-1; i
> 0; i
--) {
5931 /* Skip empty character at the end */
5932 if (dd
->serial
[i
] == 0)
5935 if (((dd
->serial
[i
] == 'A') ||
5936 (dd
->serial
[i
] == 'B') ||
5937 (dd
->serial
[i
] == '1') ||
5938 (dd
->serial
[i
] == '2')) &&
5939 (dd
->serial
[i
-1] == '-'))
5940 pr_err("\tThe action you are about to take may put your data at risk.\n"
5941 "\tPlease note that x8 devices may consist of two separate x4 devices "
5942 "located on a single PCIe port.\n"
5943 "\tRAID 0 is the only supported configuration for this type of x8 device.\n");
5946 } else if (super
->hba
->type
== SYS_DEV_VMD
&& super
->orom
&&
5947 !imsm_orom_has_tpv_support(super
->orom
)) {
5948 pr_err("\tPlatform configuration does not support non-Intel NVMe drives.\n"
5949 "\tPlease refer to Intel(R) RSTe/VROC user guide.\n");
5956 get_dev_size(fd
, NULL
, &size
);
5957 get_dev_sector_size(fd
, NULL
, &member_sector_size
);
5959 if (super
->sector_size
== 0) {
5960 /* this a first device, so sector_size is not set yet */
5961 super
->sector_size
= member_sector_size
;
5964 /* clear migr_rec when adding disk to container */
5965 memset(super
->migr_rec_buf
, 0, MIGR_REC_BUF_SECTORS
*MAX_SECTOR_SIZE
);
5966 if (lseek64(fd
, size
- MIGR_REC_SECTOR_POSITION
*member_sector_size
,
5968 if ((unsigned int)write(fd
, super
->migr_rec_buf
,
5969 MIGR_REC_BUF_SECTORS
*member_sector_size
) !=
5970 MIGR_REC_BUF_SECTORS
*member_sector_size
)
5971 perror("Write migr_rec failed");
5975 serialcpy(dd
->disk
.serial
, dd
->serial
);
5976 set_total_blocks(&dd
->disk
, size
);
5977 if (__le32_to_cpu(dd
->disk
.total_blocks_hi
) > 0) {
5978 struct imsm_super
*mpb
= super
->anchor
;
5979 mpb
->attributes
|= MPB_ATTRIB_2TB_DISK
;
5982 if (sysfs_disk_to_scsi_id(fd
, &id
) == 0)
5983 dd
->disk
.scsi_id
= __cpu_to_le32(id
);
5985 dd
->disk
.scsi_id
= __cpu_to_le32(0);
5987 if (st
->update_tail
) {
5988 dd
->next
= super
->disk_mgmt_list
;
5989 super
->disk_mgmt_list
= dd
;
5991 dd
->next
= super
->disks
;
5993 super
->updates_pending
++;
5999 static int remove_from_super_imsm(struct supertype
*st
, mdu_disk_info_t
*dk
)
6001 struct intel_super
*super
= st
->sb
;
6004 /* remove from super works only in mdmon - for communication
6005 * manager - monitor. Check if communication memory buffer
6008 if (!st
->update_tail
) {
6009 pr_err("shall be used in mdmon context only\n");
6012 dd
= xcalloc(1, sizeof(*dd
));
6013 dd
->major
= dk
->major
;
6014 dd
->minor
= dk
->minor
;
6017 dd
->action
= DISK_REMOVE
;
6019 dd
->next
= super
->disk_mgmt_list
;
6020 super
->disk_mgmt_list
= dd
;
6025 static int store_imsm_mpb(int fd
, struct imsm_super
*mpb
);
6028 char buf
[MAX_SECTOR_SIZE
];
6029 struct imsm_super anchor
;
6030 } spare_record
__attribute__ ((aligned(MAX_SECTOR_SIZE
)));
6032 /* spare records have their own family number and do not have any defined raid
6035 static int write_super_imsm_spares(struct intel_super
*super
, int doclose
)
6037 struct imsm_super
*mpb
= super
->anchor
;
6038 struct imsm_super
*spare
= &spare_record
.anchor
;
6042 spare
->mpb_size
= __cpu_to_le32(sizeof(struct imsm_super
));
6043 spare
->generation_num
= __cpu_to_le32(1UL);
6044 spare
->attributes
= MPB_ATTRIB_CHECKSUM_VERIFY
;
6045 spare
->num_disks
= 1;
6046 spare
->num_raid_devs
= 0;
6047 spare
->cache_size
= mpb
->cache_size
;
6048 spare
->pwr_cycle_count
= __cpu_to_le32(1);
6050 snprintf((char *) spare
->sig
, MAX_SIGNATURE_LENGTH
,
6051 MPB_SIGNATURE MPB_VERSION_RAID0
);
6053 for (d
= super
->disks
; d
; d
= d
->next
) {
6057 spare
->disk
[0] = d
->disk
;
6058 if (__le32_to_cpu(d
->disk
.total_blocks_hi
) > 0)
6059 spare
->attributes
|= MPB_ATTRIB_2TB_DISK
;
6061 if (super
->sector_size
== 4096)
6062 convert_to_4k_imsm_disk(&spare
->disk
[0]);
6064 sum
= __gen_imsm_checksum(spare
);
6065 spare
->family_num
= __cpu_to_le32(sum
);
6066 spare
->orig_family_num
= 0;
6067 sum
= __gen_imsm_checksum(spare
);
6068 spare
->check_sum
= __cpu_to_le32(sum
);
6070 if (store_imsm_mpb(d
->fd
, spare
)) {
6071 pr_err("failed for device %d:%d %s\n",
6072 d
->major
, d
->minor
, strerror(errno
));
6084 static int write_super_imsm(struct supertype
*st
, int doclose
)
6086 struct intel_super
*super
= st
->sb
;
6087 unsigned int sector_size
= super
->sector_size
;
6088 struct imsm_super
*mpb
= super
->anchor
;
6094 __u32 mpb_size
= sizeof(struct imsm_super
) - sizeof(struct imsm_disk
);
6096 int clear_migration_record
= 1;
6099 /* 'generation' is incremented everytime the metadata is written */
6100 generation
= __le32_to_cpu(mpb
->generation_num
);
6102 mpb
->generation_num
= __cpu_to_le32(generation
);
6104 /* fix up cases where previous mdadm releases failed to set
6107 if (mpb
->orig_family_num
== 0)
6108 mpb
->orig_family_num
= mpb
->family_num
;
6110 for (d
= super
->disks
; d
; d
= d
->next
) {
6114 mpb
->disk
[d
->index
] = d
->disk
;
6118 for (d
= super
->missing
; d
; d
= d
->next
) {
6119 mpb
->disk
[d
->index
] = d
->disk
;
6122 mpb
->num_disks
= num_disks
;
6123 mpb_size
+= sizeof(struct imsm_disk
) * mpb
->num_disks
;
6125 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
6126 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
6127 struct imsm_dev
*dev2
= get_imsm_dev(super
, i
);
6129 imsm_copy_dev(dev
, dev2
);
6130 mpb_size
+= sizeof_imsm_dev(dev
, 0);
6132 if (is_gen_migration(dev2
))
6133 clear_migration_record
= 0;
6136 bbm_log_size
= get_imsm_bbm_log_size(super
->bbm_log
);
6139 memcpy((void *)mpb
+ mpb_size
, super
->bbm_log
, bbm_log_size
);
6140 mpb
->attributes
|= MPB_ATTRIB_BBM
;
6142 mpb
->attributes
&= ~MPB_ATTRIB_BBM
;
6144 super
->anchor
->bbm_log_size
= __cpu_to_le32(bbm_log_size
);
6145 mpb_size
+= bbm_log_size
;
6146 mpb
->mpb_size
= __cpu_to_le32(mpb_size
);
6149 assert(super
->len
== 0 || mpb_size
<= super
->len
);
6152 /* recalculate checksum */
6153 sum
= __gen_imsm_checksum(mpb
);
6154 mpb
->check_sum
= __cpu_to_le32(sum
);
6156 if (super
->clean_migration_record_by_mdmon
) {
6157 clear_migration_record
= 1;
6158 super
->clean_migration_record_by_mdmon
= 0;
6160 if (clear_migration_record
)
6161 memset(super
->migr_rec_buf
, 0,
6162 MIGR_REC_BUF_SECTORS
*MAX_SECTOR_SIZE
);
6164 if (sector_size
== 4096)
6165 convert_to_4k(super
);
6167 /* write the mpb for disks that compose raid devices */
6168 for (d
= super
->disks
; d
; d
= d
->next
) {
6169 if (d
->index
< 0 || is_failed(&d
->disk
))
6172 if (clear_migration_record
) {
6173 unsigned long long dsize
;
6175 get_dev_size(d
->fd
, NULL
, &dsize
);
6176 if (lseek64(d
->fd
, dsize
- sector_size
,
6178 if ((unsigned int)write(d
->fd
,
6179 super
->migr_rec_buf
,
6180 MIGR_REC_BUF_SECTORS
*sector_size
) !=
6181 MIGR_REC_BUF_SECTORS
*sector_size
)
6182 perror("Write migr_rec failed");
6186 if (store_imsm_mpb(d
->fd
, mpb
))
6188 "failed for device %d:%d (fd: %d)%s\n",
6190 d
->fd
, strerror(errno
));
6199 return write_super_imsm_spares(super
, doclose
);
6204 static int create_array(struct supertype
*st
, int dev_idx
)
6207 struct imsm_update_create_array
*u
;
6208 struct intel_super
*super
= st
->sb
;
6209 struct imsm_dev
*dev
= get_imsm_dev(super
, dev_idx
);
6210 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
6211 struct disk_info
*inf
;
6212 struct imsm_disk
*disk
;
6215 len
= sizeof(*u
) - sizeof(*dev
) + sizeof_imsm_dev(dev
, 0) +
6216 sizeof(*inf
) * map
->num_members
;
6218 u
->type
= update_create_array
;
6219 u
->dev_idx
= dev_idx
;
6220 imsm_copy_dev(&u
->dev
, dev
);
6221 inf
= get_disk_info(u
);
6222 for (i
= 0; i
< map
->num_members
; i
++) {
6223 int idx
= get_imsm_disk_idx(dev
, i
, MAP_X
);
6225 disk
= get_imsm_disk(super
, idx
);
6227 disk
= get_imsm_missing(super
, idx
);
6228 serialcpy(inf
[i
].serial
, disk
->serial
);
6230 append_metadata_update(st
, u
, len
);
6235 static int mgmt_disk(struct supertype
*st
)
6237 struct intel_super
*super
= st
->sb
;
6239 struct imsm_update_add_remove_disk
*u
;
6241 if (!super
->disk_mgmt_list
)
6246 u
->type
= update_add_remove_disk
;
6247 append_metadata_update(st
, u
, len
);
6252 __u32
crc32c_le(__u32 crc
, unsigned char const *p
, size_t len
);
6254 static int write_ppl_header(unsigned long long ppl_sector
, int fd
, void *buf
)
6256 struct ppl_header
*ppl_hdr
= buf
;
6259 ppl_hdr
->checksum
= __cpu_to_le32(~crc32c_le(~0, buf
, PPL_HEADER_SIZE
));
6261 if (lseek64(fd
, ppl_sector
* 512, SEEK_SET
) < 0) {
6263 perror("Failed to seek to PPL header location");
6267 if (write(fd
, buf
, PPL_HEADER_SIZE
) != PPL_HEADER_SIZE
) {
6269 perror("Write PPL header failed");
6278 static int write_init_ppl_imsm(struct supertype
*st
, struct mdinfo
*info
, int fd
)
6280 struct intel_super
*super
= st
->sb
;
6282 struct ppl_header
*ppl_hdr
;
6285 /* first clear entire ppl space */
6286 ret
= zero_disk_range(fd
, info
->ppl_sector
, info
->ppl_size
);
6290 ret
= posix_memalign(&buf
, MAX_SECTOR_SIZE
, PPL_HEADER_SIZE
);
6292 pr_err("Failed to allocate PPL header buffer\n");
6296 memset(buf
, 0, PPL_HEADER_SIZE
);
6298 memset(ppl_hdr
->reserved
, 0xff, PPL_HDR_RESERVED
);
6299 ppl_hdr
->signature
= __cpu_to_le32(super
->anchor
->orig_family_num
);
6301 if (info
->mismatch_cnt
) {
6303 * We are overwriting an invalid ppl. Make one entry with wrong
6304 * checksum to prevent the kernel from skipping resync.
6306 ppl_hdr
->entries_count
= __cpu_to_le32(1);
6307 ppl_hdr
->entries
[0].checksum
= ~0;
6310 ret
= write_ppl_header(info
->ppl_sector
, fd
, buf
);
6316 static int is_rebuilding(struct imsm_dev
*dev
);
6318 static int validate_ppl_imsm(struct supertype
*st
, struct mdinfo
*info
,
6319 struct mdinfo
*disk
)
6321 struct intel_super
*super
= st
->sb
;
6323 void *buf_orig
, *buf
, *buf_prev
= NULL
;
6325 struct ppl_header
*ppl_hdr
= NULL
;
6327 struct imsm_dev
*dev
;
6330 unsigned long long ppl_offset
= 0;
6331 unsigned long long prev_gen_num
= 0;
6333 if (disk
->disk
.raid_disk
< 0)
6336 dev
= get_imsm_dev(super
, info
->container_member
);
6337 idx
= get_imsm_disk_idx(dev
, disk
->disk
.raid_disk
, MAP_0
);
6338 d
= get_imsm_dl_disk(super
, idx
);
6340 if (!d
|| d
->index
< 0 || is_failed(&d
->disk
))
6343 if (posix_memalign(&buf_orig
, MAX_SECTOR_SIZE
, PPL_HEADER_SIZE
* 2)) {
6344 pr_err("Failed to allocate PPL header buffer\n");
6350 while (ppl_offset
< MULTIPLE_PPL_AREA_SIZE_IMSM
) {
6353 dprintf("Checking potential PPL at offset: %llu\n", ppl_offset
);
6355 if (lseek64(d
->fd
, info
->ppl_sector
* 512 + ppl_offset
,
6357 perror("Failed to seek to PPL header location");
6362 if (read(d
->fd
, buf
, PPL_HEADER_SIZE
) != PPL_HEADER_SIZE
) {
6363 perror("Read PPL header failed");
6370 crc
= __le32_to_cpu(ppl_hdr
->checksum
);
6371 ppl_hdr
->checksum
= 0;
6373 if (crc
!= ~crc32c_le(~0, buf
, PPL_HEADER_SIZE
)) {
6374 dprintf("Wrong PPL header checksum on %s\n",
6379 if (prev_gen_num
> __le64_to_cpu(ppl_hdr
->generation
)) {
6380 /* previous was newest, it was already checked */
6384 if ((__le32_to_cpu(ppl_hdr
->signature
) !=
6385 super
->anchor
->orig_family_num
)) {
6386 dprintf("Wrong PPL header signature on %s\n",
6393 prev_gen_num
= __le64_to_cpu(ppl_hdr
->generation
);
6395 ppl_offset
+= PPL_HEADER_SIZE
;
6396 for (i
= 0; i
< __le32_to_cpu(ppl_hdr
->entries_count
); i
++)
6398 __le32_to_cpu(ppl_hdr
->entries
[i
].pp_size
);
6401 buf_prev
= buf
+ PPL_HEADER_SIZE
;
6413 * Update metadata to use mutliple PPLs area (1MB).
6414 * This is done once for all RAID members
6416 if (info
->consistency_policy
== CONSISTENCY_POLICY_PPL
&&
6417 info
->ppl_size
!= (MULTIPLE_PPL_AREA_SIZE_IMSM
>> 9)) {
6419 struct mdinfo
*member_dev
;
6421 sprintf(subarray
, "%d", info
->container_member
);
6423 if (mdmon_running(st
->container_devnm
))
6424 st
->update_tail
= &st
->updates
;
6426 if (st
->ss
->update_subarray(st
, subarray
, "ppl", NULL
)) {
6427 pr_err("Failed to update subarray %s\n",
6430 if (st
->update_tail
)
6431 flush_metadata_updates(st
);
6433 st
->ss
->sync_metadata(st
);
6434 info
->ppl_size
= (MULTIPLE_PPL_AREA_SIZE_IMSM
>> 9);
6435 for (member_dev
= info
->devs
; member_dev
;
6436 member_dev
= member_dev
->next
)
6437 member_dev
->ppl_size
=
6438 (MULTIPLE_PPL_AREA_SIZE_IMSM
>> 9);
6443 struct imsm_map
*map
= get_imsm_map(dev
, MAP_X
);
6445 if (map
->map_state
== IMSM_T_STATE_UNINITIALIZED
||
6446 (map
->map_state
== IMSM_T_STATE_NORMAL
&&
6447 !(dev
->vol
.dirty
& RAIDVOL_DIRTY
)) ||
6448 (is_rebuilding(dev
) &&
6449 dev
->vol
.curr_migr_unit
== 0 &&
6450 get_imsm_disk_idx(dev
, disk
->disk
.raid_disk
, MAP_1
) != idx
))
6451 ret
= st
->ss
->write_init_ppl(st
, info
, d
->fd
);
6453 info
->mismatch_cnt
++;
6454 } else if (ret
== 0 &&
6455 ppl_hdr
->entries_count
== 0 &&
6456 is_rebuilding(dev
) &&
6457 info
->resync_start
== 0) {
6459 * The header has no entries - add a single empty entry and
6460 * rewrite the header to prevent the kernel from going into
6461 * resync after an interrupted rebuild.
6463 ppl_hdr
->entries_count
= __cpu_to_le32(1);
6464 ret
= write_ppl_header(info
->ppl_sector
, d
->fd
, buf
);
6472 static int write_init_ppl_imsm_all(struct supertype
*st
, struct mdinfo
*info
)
6474 struct intel_super
*super
= st
->sb
;
6478 if (info
->consistency_policy
!= CONSISTENCY_POLICY_PPL
||
6479 info
->array
.level
!= 5)
6482 for (d
= super
->disks
; d
; d
= d
->next
) {
6483 if (d
->index
< 0 || is_failed(&d
->disk
))
6486 ret
= st
->ss
->write_init_ppl(st
, info
, d
->fd
);
6494 static int write_init_super_imsm(struct supertype
*st
)
6496 struct intel_super
*super
= st
->sb
;
6497 int current_vol
= super
->current_vol
;
6501 getinfo_super_imsm(st
, &info
, NULL
);
6503 /* we are done with current_vol reset it to point st at the container */
6504 super
->current_vol
= -1;
6506 if (st
->update_tail
) {
6507 /* queue the recently created array / added disk
6508 * as a metadata update */
6510 /* determine if we are creating a volume or adding a disk */
6511 if (current_vol
< 0) {
6512 /* in the mgmt (add/remove) disk case we are running
6513 * in mdmon context, so don't close fd's
6517 rv
= write_init_ppl_imsm_all(st
, &info
);
6519 rv
= create_array(st
, current_vol
);
6523 for (d
= super
->disks
; d
; d
= d
->next
)
6524 Kill(d
->devname
, NULL
, 0, -1, 1);
6525 if (current_vol
>= 0)
6526 rv
= write_init_ppl_imsm_all(st
, &info
);
6528 rv
= write_super_imsm(st
, 1);
6534 static int store_super_imsm(struct supertype
*st
, int fd
)
6536 struct intel_super
*super
= st
->sb
;
6537 struct imsm_super
*mpb
= super
? super
->anchor
: NULL
;
6542 if (super
->sector_size
== 4096)
6543 convert_to_4k(super
);
6544 return store_imsm_mpb(fd
, mpb
);
6547 static int validate_geometry_imsm_container(struct supertype
*st
, int level
,
6548 int layout
, int raiddisks
, int chunk
,
6549 unsigned long long size
,
6550 unsigned long long data_offset
,
6552 unsigned long long *freesize
,
6556 unsigned long long ldsize
;
6557 struct intel_super
*super
;
6560 if (level
!= LEVEL_CONTAINER
)
6565 fd
= open(dev
, O_RDONLY
|O_EXCL
, 0);
6568 pr_err("imsm: Cannot open %s: %s\n",
6569 dev
, strerror(errno
));
6572 if (!get_dev_size(fd
, dev
, &ldsize
)) {
6577 /* capabilities retrieve could be possible
6578 * note that there is no fd for the disks in array.
6580 super
= alloc_super();
6585 if (!get_dev_sector_size(fd
, NULL
, &super
->sector_size
)) {
6591 rv
= find_intel_hba_capability(fd
, super
, verbose
> 0 ? dev
: NULL
);
6595 fd2devname(fd
, str
);
6596 dprintf("fd: %d %s orom: %p rv: %d raiddisk: %d\n",
6597 fd
, str
, super
->orom
, rv
, raiddisks
);
6599 /* no orom/efi or non-intel hba of the disk */
6606 if (raiddisks
> super
->orom
->tds
) {
6608 pr_err("%d exceeds maximum number of platform supported disks: %d\n",
6609 raiddisks
, super
->orom
->tds
);
6613 if ((super
->orom
->attr
& IMSM_OROM_ATTR_2TB_DISK
) == 0 &&
6614 (ldsize
>> 9) >> 32 > 0) {
6616 pr_err("%s exceeds maximum platform supported size\n", dev
);
6622 *freesize
= avail_size_imsm(st
, ldsize
>> 9, data_offset
);
6628 static unsigned long long find_size(struct extent
*e
, int *idx
, int num_extents
)
6630 const unsigned long long base_start
= e
[*idx
].start
;
6631 unsigned long long end
= base_start
+ e
[*idx
].size
;
6634 if (base_start
== end
)
6638 for (i
= *idx
; i
< num_extents
; i
++) {
6639 /* extend overlapping extents */
6640 if (e
[i
].start
>= base_start
&&
6641 e
[i
].start
<= end
) {
6644 if (e
[i
].start
+ e
[i
].size
> end
)
6645 end
= e
[i
].start
+ e
[i
].size
;
6646 } else if (e
[i
].start
> end
) {
6652 return end
- base_start
;
6655 static unsigned long long merge_extents(struct intel_super
*super
, int sum_extents
)
6657 /* build a composite disk with all known extents and generate a new
6658 * 'maxsize' given the "all disks in an array must share a common start
6659 * offset" constraint
6661 struct extent
*e
= xcalloc(sum_extents
, sizeof(*e
));
6665 unsigned long long pos
;
6666 unsigned long long start
= 0;
6667 unsigned long long maxsize
;
6668 unsigned long reserve
;
6670 /* coalesce and sort all extents. also, check to see if we need to
6671 * reserve space between member arrays
6674 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
6677 for (i
= 0; i
< dl
->extent_cnt
; i
++)
6680 qsort(e
, sum_extents
, sizeof(*e
), cmp_extent
);
6685 while (i
< sum_extents
) {
6686 e
[j
].start
= e
[i
].start
;
6687 e
[j
].size
= find_size(e
, &i
, sum_extents
);
6689 if (e
[j
-1].size
== 0)
6698 unsigned long long esize
;
6700 esize
= e
[i
].start
- pos
;
6701 if (esize
>= maxsize
) {
6706 pos
= e
[i
].start
+ e
[i
].size
;
6708 } while (e
[i
-1].size
);
6714 /* FIXME assumes volume at offset 0 is the first volume in a
6717 if (start_extent
> 0)
6718 reserve
= IMSM_RESERVED_SECTORS
; /* gap between raid regions */
6722 if (maxsize
< reserve
)
6725 super
->create_offset
= ~((unsigned long long) 0);
6726 if (start
+ reserve
> super
->create_offset
)
6727 return 0; /* start overflows create_offset */
6728 super
->create_offset
= start
+ reserve
;
6730 return maxsize
- reserve
;
6733 static int is_raid_level_supported(const struct imsm_orom
*orom
, int level
, int raiddisks
)
6735 if (level
< 0 || level
== 6 || level
== 4)
6738 /* if we have an orom prevent invalid raid levels */
6741 case 0: return imsm_orom_has_raid0(orom
);
6744 return imsm_orom_has_raid1e(orom
);
6745 return imsm_orom_has_raid1(orom
) && raiddisks
== 2;
6746 case 10: return imsm_orom_has_raid10(orom
) && raiddisks
== 4;
6747 case 5: return imsm_orom_has_raid5(orom
) && raiddisks
> 2;
6750 return 1; /* not on an Intel RAID platform so anything goes */
6756 active_arrays_by_format(char *name
, char* hba
, struct md_list
**devlist
,
6757 int dpa
, int verbose
)
6759 struct mdstat_ent
*mdstat
= mdstat_read(0, 0);
6760 struct mdstat_ent
*memb
;
6766 for (memb
= mdstat
; memb
; memb
= memb
->next
) {
6767 if (memb
->metadata_version
&&
6768 (strncmp(memb
->metadata_version
, "external:", 9) == 0) &&
6769 (strcmp(&memb
->metadata_version
[9], name
) == 0) &&
6770 !is_subarray(memb
->metadata_version
+9) &&
6772 struct dev_member
*dev
= memb
->members
;
6774 while(dev
&& (fd
< 0)) {
6775 char *path
= xmalloc(strlen(dev
->name
) + strlen("/dev/") + 1);
6776 num
= sprintf(path
, "%s%s", "/dev/", dev
->name
);
6778 fd
= open(path
, O_RDONLY
, 0);
6779 if (num
<= 0 || fd
< 0) {
6780 pr_vrb("Cannot open %s: %s\n",
6781 dev
->name
, strerror(errno
));
6787 if (fd
>= 0 && disk_attached_to_hba(fd
, hba
)) {
6788 struct mdstat_ent
*vol
;
6789 for (vol
= mdstat
; vol
; vol
= vol
->next
) {
6790 if (vol
->active
> 0 &&
6791 vol
->metadata_version
&&
6792 is_container_member(vol
, memb
->devnm
)) {
6797 if (*devlist
&& (found
< dpa
)) {
6798 dv
= xcalloc(1, sizeof(*dv
));
6799 dv
->devname
= xmalloc(strlen(memb
->devnm
) + strlen("/dev/") + 1);
6800 sprintf(dv
->devname
, "%s%s", "/dev/", memb
->devnm
);
6803 dv
->next
= *devlist
;
6811 free_mdstat(mdstat
);
6816 static struct md_list
*
6817 get_loop_devices(void)
6820 struct md_list
*devlist
= NULL
;
6823 for(i
= 0; i
< 12; i
++) {
6824 dv
= xcalloc(1, sizeof(*dv
));
6825 dv
->devname
= xmalloc(40);
6826 sprintf(dv
->devname
, "/dev/loop%d", i
);
6834 static struct md_list
*
6835 get_devices(const char *hba_path
)
6837 struct md_list
*devlist
= NULL
;
6844 devlist
= get_loop_devices();
6847 /* scroll through /sys/dev/block looking for devices attached to
6850 dir
= opendir("/sys/dev/block");
6851 for (ent
= dir
? readdir(dir
) : NULL
; ent
; ent
= readdir(dir
)) {
6856 if (sscanf(ent
->d_name
, "%d:%d", &major
, &minor
) != 2)
6858 path
= devt_to_devpath(makedev(major
, minor
));
6861 if (!path_attached_to_hba(path
, hba_path
)) {
6868 fd
= dev_open(ent
->d_name
, O_RDONLY
);
6870 fd2devname(fd
, buf
);
6873 pr_err("cannot open device: %s\n",
6878 dv
= xcalloc(1, sizeof(*dv
));
6879 dv
->devname
= xstrdup(buf
);
6886 devlist
= devlist
->next
;
6896 count_volumes_list(struct md_list
*devlist
, char *homehost
,
6897 int verbose
, int *found
)
6899 struct md_list
*tmpdev
;
6901 struct supertype
*st
;
6903 /* first walk the list of devices to find a consistent set
6904 * that match the criterea, if that is possible.
6905 * We flag the ones we like with 'used'.
6908 st
= match_metadata_desc_imsm("imsm");
6910 pr_vrb("cannot allocate memory for imsm supertype\n");
6914 for (tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
6915 char *devname
= tmpdev
->devname
;
6917 struct supertype
*tst
;
6919 if (tmpdev
->used
> 1)
6921 tst
= dup_super(st
);
6923 pr_vrb("cannot allocate memory for imsm supertype\n");
6926 tmpdev
->container
= 0;
6927 dfd
= dev_open(devname
, O_RDONLY
|O_EXCL
);
6929 dprintf("cannot open device %s: %s\n",
6930 devname
, strerror(errno
));
6932 } else if (!fstat_is_blkdev(dfd
, devname
, &rdev
)) {
6934 } else if (must_be_container(dfd
)) {
6935 struct supertype
*cst
;
6936 cst
= super_by_fd(dfd
, NULL
);
6938 dprintf("cannot recognize container type %s\n",
6941 } else if (tst
->ss
!= st
->ss
) {
6942 dprintf("non-imsm container - ignore it: %s\n",
6945 } else if (!tst
->ss
->load_container
||
6946 tst
->ss
->load_container(tst
, dfd
, NULL
))
6949 tmpdev
->container
= 1;
6952 cst
->ss
->free_super(cst
);
6954 tmpdev
->st_rdev
= rdev
;
6955 if (tst
->ss
->load_super(tst
,dfd
, NULL
)) {
6956 dprintf("no RAID superblock on %s\n",
6959 } else if (tst
->ss
->compare_super
== NULL
) {
6960 dprintf("Cannot assemble %s metadata on %s\n",
6961 tst
->ss
->name
, devname
);
6967 if (tmpdev
->used
== 2 || tmpdev
->used
== 4) {
6968 /* Ignore unrecognised devices during auto-assembly */
6973 tst
->ss
->getinfo_super(tst
, &info
, NULL
);
6975 if (st
->minor_version
== -1)
6976 st
->minor_version
= tst
->minor_version
;
6978 if (memcmp(info
.uuid
, uuid_zero
,
6979 sizeof(int[4])) == 0) {
6980 /* this is a floating spare. It cannot define
6981 * an array unless there are no more arrays of
6982 * this type to be found. It can be included
6983 * in an array of this type though.
6989 if (st
->ss
!= tst
->ss
||
6990 st
->minor_version
!= tst
->minor_version
||
6991 st
->ss
->compare_super(st
, tst
) != 0) {
6992 /* Some mismatch. If exactly one array matches this host,
6993 * we can resolve on that one.
6994 * Or, if we are auto assembling, we just ignore the second
6997 dprintf("superblock on %s doesn't match others - assembly aborted\n",
7003 dprintf("found: devname: %s\n", devname
);
7007 tst
->ss
->free_super(tst
);
7011 if ((err
= load_super_imsm_all(st
, -1, &st
->sb
, NULL
, devlist
, 0)) == 0) {
7012 struct mdinfo
*iter
, *head
= st
->ss
->container_content(st
, NULL
);
7013 for (iter
= head
; iter
; iter
= iter
->next
) {
7014 dprintf("content->text_version: %s vol\n",
7015 iter
->text_version
);
7016 if (iter
->array
.state
& (1<<MD_SB_BLOCK_VOLUME
)) {
7017 /* do not assemble arrays with unsupported
7019 dprintf("Cannot activate member %s.\n",
7020 iter
->text_version
);
7027 dprintf("No valid super block on device list: err: %d %p\n",
7031 dprintf("no more devices to examine\n");
7034 for (tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
7035 if (tmpdev
->used
== 1 && tmpdev
->found
) {
7037 if (count
< tmpdev
->found
)
7040 count
-= tmpdev
->found
;
7043 if (tmpdev
->used
== 1)
7048 st
->ss
->free_super(st
);
7052 static int __count_volumes(char *hba_path
, int dpa
, int verbose
,
7055 struct sys_dev
*idev
, *intel_devices
= find_intel_devices();
7057 const struct orom_entry
*entry
;
7058 struct devid_list
*dv
, *devid_list
;
7063 for (idev
= intel_devices
; idev
; idev
= idev
->next
) {
7064 if (strstr(idev
->path
, hba_path
))
7068 if (!idev
|| !idev
->dev_id
)
7071 entry
= get_orom_entry_by_device_id(idev
->dev_id
);
7073 if (!entry
|| !entry
->devid_list
)
7076 devid_list
= entry
->devid_list
;
7077 for (dv
= devid_list
; dv
; dv
= dv
->next
) {
7078 struct md_list
*devlist
;
7079 struct sys_dev
*device
= NULL
;
7084 device
= device_by_id_and_path(dv
->devid
, hba_path
);
7086 device
= device_by_id(dv
->devid
);
7089 hpath
= device
->path
;
7093 devlist
= get_devices(hpath
);
7094 /* if no intel devices return zero volumes */
7095 if (devlist
== NULL
)
7098 count
+= active_arrays_by_format("imsm", hpath
, &devlist
, dpa
,
7100 dprintf("path: %s active arrays: %d\n", hpath
, count
);
7101 if (devlist
== NULL
)
7105 count
+= count_volumes_list(devlist
,
7109 dprintf("found %d count: %d\n", found
, count
);
7112 dprintf("path: %s total number of volumes: %d\n", hpath
, count
);
7115 struct md_list
*dv
= devlist
;
7116 devlist
= devlist
->next
;
7124 static int count_volumes(struct intel_hba
*hba
, int dpa
, int verbose
)
7128 if (hba
->type
== SYS_DEV_VMD
) {
7129 struct sys_dev
*dev
;
7132 for (dev
= find_intel_devices(); dev
; dev
= dev
->next
) {
7133 if (dev
->type
== SYS_DEV_VMD
)
7134 count
+= __count_volumes(dev
->path
, dpa
,
7139 return __count_volumes(hba
->path
, dpa
, verbose
, 0);
7142 static int imsm_default_chunk(const struct imsm_orom
*orom
)
7144 /* up to 512 if the plaform supports it, otherwise the platform max.
7145 * 128 if no platform detected
7147 int fs
= max(7, orom
? fls(orom
->sss
) : 0);
7149 return min(512, (1 << fs
));
7153 validate_geometry_imsm_orom(struct intel_super
*super
, int level
, int layout
,
7154 int raiddisks
, int *chunk
, unsigned long long size
, int verbose
)
7156 /* check/set platform and metadata limits/defaults */
7157 if (super
->orom
&& raiddisks
> super
->orom
->dpa
) {
7158 pr_vrb("platform supports a maximum of %d disks per array\n",
7163 /* capabilities of OROM tested - copied from validate_geometry_imsm_volume */
7164 if (!is_raid_level_supported(super
->orom
, level
, raiddisks
)) {
7165 pr_vrb("platform does not support raid%d with %d disk%s\n",
7166 level
, raiddisks
, raiddisks
> 1 ? "s" : "");
7170 if (*chunk
== 0 || *chunk
== UnSet
)
7171 *chunk
= imsm_default_chunk(super
->orom
);
7173 if (super
->orom
&& !imsm_orom_has_chunk(super
->orom
, *chunk
)) {
7174 pr_vrb("platform does not support a chunk size of: %d\n", *chunk
);
7178 if (layout
!= imsm_level_to_layout(level
)) {
7180 pr_vrb("imsm raid 5 only supports the left-asymmetric layout\n");
7181 else if (level
== 10)
7182 pr_vrb("imsm raid 10 only supports the n2 layout\n");
7184 pr_vrb("imsm unknown layout %#x for this raid level %d\n",
7189 if (super
->orom
&& (super
->orom
->attr
& IMSM_OROM_ATTR_2TB
) == 0 &&
7190 (calc_array_size(level
, raiddisks
, layout
, *chunk
, size
) >> 32) > 0) {
7191 pr_vrb("platform does not support a volume size over 2TB\n");
7198 /* validate_geometry_imsm_volume - lifted from validate_geometry_ddf_bvd
7199 * FIX ME add ahci details
7201 static int validate_geometry_imsm_volume(struct supertype
*st
, int level
,
7202 int layout
, int raiddisks
, int *chunk
,
7203 unsigned long long size
,
7204 unsigned long long data_offset
,
7206 unsigned long long *freesize
,
7210 struct intel_super
*super
= st
->sb
;
7211 struct imsm_super
*mpb
;
7213 unsigned long long pos
= 0;
7214 unsigned long long maxsize
;
7218 /* We must have the container info already read in. */
7222 mpb
= super
->anchor
;
7224 if (!validate_geometry_imsm_orom(super
, level
, layout
, raiddisks
, chunk
, size
, verbose
)) {
7225 pr_err("RAID geometry validation failed. Cannot proceed with the action(s).\n");
7229 /* General test: make sure there is space for
7230 * 'raiddisks' device extents of size 'size' at a given
7233 unsigned long long minsize
= size
;
7234 unsigned long long start_offset
= MaxSector
;
7237 minsize
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
7238 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
7243 e
= get_extents(super
, dl
, 0);
7246 unsigned long long esize
;
7247 esize
= e
[i
].start
- pos
;
7248 if (esize
>= minsize
)
7250 if (found
&& start_offset
== MaxSector
) {
7253 } else if (found
&& pos
!= start_offset
) {
7257 pos
= e
[i
].start
+ e
[i
].size
;
7259 } while (e
[i
-1].size
);
7264 if (dcnt
< raiddisks
) {
7266 pr_err("imsm: Not enough devices with space for this array (%d < %d)\n",
7273 /* This device must be a member of the set */
7274 if (!stat_is_blkdev(dev
, &rdev
))
7276 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
7277 if (dl
->major
== (int)major(rdev
) &&
7278 dl
->minor
== (int)minor(rdev
))
7283 pr_err("%s is not in the same imsm set\n", dev
);
7285 } else if (super
->orom
&& dl
->index
< 0 && mpb
->num_raid_devs
) {
7286 /* If a volume is present then the current creation attempt
7287 * cannot incorporate new spares because the orom may not
7288 * understand this configuration (all member disks must be
7289 * members of each array in the container).
7291 pr_err("%s is a spare and a volume is already defined for this container\n", dev
);
7292 pr_err("The option-rom requires all member disks to be a member of all volumes\n");
7294 } else if (super
->orom
&& mpb
->num_raid_devs
> 0 &&
7295 mpb
->num_disks
!= raiddisks
) {
7296 pr_err("The option-rom requires all member disks to be a member of all volumes\n");
7300 /* retrieve the largest free space block */
7301 e
= get_extents(super
, dl
, 0);
7306 unsigned long long esize
;
7308 esize
= e
[i
].start
- pos
;
7309 if (esize
>= maxsize
)
7311 pos
= e
[i
].start
+ e
[i
].size
;
7313 } while (e
[i
-1].size
);
7318 pr_err("unable to determine free space for: %s\n",
7322 if (maxsize
< size
) {
7324 pr_err("%s not enough space (%llu < %llu)\n",
7325 dev
, maxsize
, size
);
7329 /* count total number of extents for merge */
7331 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
7333 i
+= dl
->extent_cnt
;
7335 maxsize
= merge_extents(super
, i
);
7337 if (mpb
->num_raid_devs
> 0 && size
&& size
!= maxsize
)
7338 pr_err("attempting to create a second volume with size less then remaining space.\n");
7340 if (maxsize
< size
|| maxsize
== 0) {
7343 pr_err("no free space left on device. Aborting...\n");
7345 pr_err("not enough space to create volume of given size (%llu < %llu). Aborting...\n",
7351 *freesize
= maxsize
;
7354 int count
= count_volumes(super
->hba
,
7355 super
->orom
->dpa
, verbose
);
7356 if (super
->orom
->vphba
<= count
) {
7357 pr_vrb("platform does not support more than %d raid volumes.\n",
7358 super
->orom
->vphba
);
7365 static int imsm_get_free_size(struct supertype
*st
, int raiddisks
,
7366 unsigned long long size
, int chunk
,
7367 unsigned long long *freesize
)
7369 struct intel_super
*super
= st
->sb
;
7370 struct imsm_super
*mpb
= super
->anchor
;
7375 unsigned long long maxsize
;
7376 unsigned long long minsize
;
7380 /* find the largest common start free region of the possible disks */
7384 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
7390 /* don't activate new spares if we are orom constrained
7391 * and there is already a volume active in the container
7393 if (super
->orom
&& dl
->index
< 0 && mpb
->num_raid_devs
)
7396 e
= get_extents(super
, dl
, 0);
7399 for (i
= 1; e
[i
-1].size
; i
++)
7407 maxsize
= merge_extents(super
, extent_cnt
);
7411 minsize
= chunk
* 2;
7413 if (cnt
< raiddisks
||
7414 (super
->orom
&& used
&& used
!= raiddisks
) ||
7415 maxsize
< minsize
||
7417 pr_err("not enough devices with space to create array.\n");
7418 return 0; /* No enough free spaces large enough */
7429 if (mpb
->num_raid_devs
> 0 && size
&& size
!= maxsize
)
7430 pr_err("attempting to create a second volume with size less then remaining space.\n");
7432 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
7434 dl
->raiddisk
= cnt
++;
7438 dprintf("imsm: imsm_get_free_size() returns : %llu\n", size
);
7443 static int reserve_space(struct supertype
*st
, int raiddisks
,
7444 unsigned long long size
, int chunk
,
7445 unsigned long long *freesize
)
7447 struct intel_super
*super
= st
->sb
;
7452 rv
= imsm_get_free_size(st
, raiddisks
, size
, chunk
, freesize
);
7455 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
7457 dl
->raiddisk
= cnt
++;
7464 static int validate_geometry_imsm(struct supertype
*st
, int level
, int layout
,
7465 int raiddisks
, int *chunk
, unsigned long long size
,
7466 unsigned long long data_offset
,
7467 char *dev
, unsigned long long *freesize
,
7468 int consistency_policy
, int verbose
)
7475 * if given unused devices create a container
7476 * if given given devices in a container create a member volume
7478 if (level
== LEVEL_CONTAINER
) {
7479 /* Must be a fresh device to add to a container */
7480 return validate_geometry_imsm_container(st
, level
, layout
,
7488 if (size
&& (size
< 1024)) {
7489 pr_err("Given size must be greater than 1M.\n");
7490 /* Depends on algorithm in Create.c :
7491 * if container was given (dev == NULL) return -1,
7492 * if block device was given ( dev != NULL) return 0.
7494 return dev
? -1 : 0;
7499 struct intel_super
*super
= st
->sb
;
7500 if (!validate_geometry_imsm_orom(st
->sb
, level
, layout
,
7501 raiddisks
, chunk
, size
,
7504 /* we are being asked to automatically layout a
7505 * new volume based on the current contents of
7506 * the container. If the the parameters can be
7507 * satisfied reserve_space will record the disks,
7508 * start offset, and size of the volume to be
7509 * created. add_to_super and getinfo_super
7510 * detect when autolayout is in progress.
7512 /* assuming that freesize is always given when array is
7514 if (super
->orom
&& freesize
) {
7516 count
= count_volumes(super
->hba
,
7517 super
->orom
->dpa
, verbose
);
7518 if (super
->orom
->vphba
<= count
) {
7519 pr_vrb("platform does not support more than %d raid volumes.\n",
7520 super
->orom
->vphba
);
7525 return reserve_space(st
, raiddisks
, size
,
7531 /* creating in a given container */
7532 return validate_geometry_imsm_volume(st
, level
, layout
,
7533 raiddisks
, chunk
, size
,
7535 dev
, freesize
, verbose
);
7538 /* This device needs to be a device in an 'imsm' container */
7539 fd
= open(dev
, O_RDONLY
|O_EXCL
, 0);
7542 pr_err("Cannot create this array on device %s\n",
7547 if (errno
!= EBUSY
|| (fd
= open(dev
, O_RDONLY
, 0)) < 0) {
7549 pr_err("Cannot open %s: %s\n",
7550 dev
, strerror(errno
));
7553 /* Well, it is in use by someone, maybe an 'imsm' container. */
7554 cfd
= open_container(fd
);
7558 pr_err("Cannot use %s: It is busy\n",
7562 sra
= sysfs_read(cfd
, NULL
, GET_VERSION
);
7563 if (sra
&& sra
->array
.major_version
== -1 &&
7564 strcmp(sra
->text_version
, "imsm") == 0)
7568 /* This is a member of a imsm container. Load the container
7569 * and try to create a volume
7571 struct intel_super
*super
;
7573 if (load_super_imsm_all(st
, cfd
, (void **) &super
, NULL
, NULL
, 1) == 0) {
7575 strcpy(st
->container_devnm
, fd2devnm(cfd
));
7577 return validate_geometry_imsm_volume(st
, level
, layout
,
7579 size
, data_offset
, dev
,
7586 pr_err("failed container membership check\n");
7592 static void default_geometry_imsm(struct supertype
*st
, int *level
, int *layout
, int *chunk
)
7594 struct intel_super
*super
= st
->sb
;
7596 if (level
&& *level
== UnSet
)
7597 *level
= LEVEL_CONTAINER
;
7599 if (level
&& layout
&& *layout
== UnSet
)
7600 *layout
= imsm_level_to_layout(*level
);
7602 if (chunk
&& (*chunk
== UnSet
|| *chunk
== 0))
7603 *chunk
= imsm_default_chunk(super
->orom
);
7606 static void handle_missing(struct intel_super
*super
, struct imsm_dev
*dev
);
7608 static int kill_subarray_imsm(struct supertype
*st
)
7610 /* remove the subarray currently referenced by ->current_vol */
7612 struct intel_dev
**dp
;
7613 struct intel_super
*super
= st
->sb
;
7614 __u8 current_vol
= super
->current_vol
;
7615 struct imsm_super
*mpb
= super
->anchor
;
7617 if (super
->current_vol
< 0)
7619 super
->current_vol
= -1; /* invalidate subarray cursor */
7621 /* block deletions that would change the uuid of active subarrays
7623 * FIXME when immutable ids are available, but note that we'll
7624 * also need to fixup the invalidated/active subarray indexes in
7627 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
7630 if (i
< current_vol
)
7632 sprintf(subarray
, "%u", i
);
7633 if (is_subarray_active(subarray
, st
->devnm
)) {
7634 pr_err("deleting subarray-%d would change the UUID of active subarray-%d, aborting\n",
7641 if (st
->update_tail
) {
7642 struct imsm_update_kill_array
*u
= xmalloc(sizeof(*u
));
7644 u
->type
= update_kill_array
;
7645 u
->dev_idx
= current_vol
;
7646 append_metadata_update(st
, u
, sizeof(*u
));
7651 for (dp
= &super
->devlist
; *dp
;)
7652 if ((*dp
)->index
== current_vol
) {
7655 handle_missing(super
, (*dp
)->dev
);
7656 if ((*dp
)->index
> current_vol
)
7661 /* no more raid devices, all active components are now spares,
7662 * but of course failed are still failed
7664 if (--mpb
->num_raid_devs
== 0) {
7667 for (d
= super
->disks
; d
; d
= d
->next
)
7672 super
->updates_pending
++;
7677 static int update_subarray_imsm(struct supertype
*st
, char *subarray
,
7678 char *update
, struct mddev_ident
*ident
)
7680 /* update the subarray currently referenced by ->current_vol */
7681 struct intel_super
*super
= st
->sb
;
7682 struct imsm_super
*mpb
= super
->anchor
;
7684 if (strcmp(update
, "name") == 0) {
7685 char *name
= ident
->name
;
7689 if (is_subarray_active(subarray
, st
->devnm
)) {
7690 pr_err("Unable to update name of active subarray\n");
7694 if (!check_name(super
, name
, 0))
7697 vol
= strtoul(subarray
, &ep
, 10);
7698 if (*ep
!= '\0' || vol
>= super
->anchor
->num_raid_devs
)
7701 if (st
->update_tail
) {
7702 struct imsm_update_rename_array
*u
= xmalloc(sizeof(*u
));
7704 u
->type
= update_rename_array
;
7706 strncpy((char *) u
->name
, name
, MAX_RAID_SERIAL_LEN
);
7707 u
->name
[MAX_RAID_SERIAL_LEN
-1] = '\0';
7708 append_metadata_update(st
, u
, sizeof(*u
));
7710 struct imsm_dev
*dev
;
7713 dev
= get_imsm_dev(super
, vol
);
7714 memset(dev
->volume
, '\0', MAX_RAID_SERIAL_LEN
);
7715 namelen
= min((int)strlen(name
), MAX_RAID_SERIAL_LEN
);
7716 memcpy(dev
->volume
, name
, namelen
);
7717 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
7718 dev
= get_imsm_dev(super
, i
);
7719 handle_missing(super
, dev
);
7721 super
->updates_pending
++;
7723 } else if (strcmp(update
, "ppl") == 0 ||
7724 strcmp(update
, "no-ppl") == 0) {
7727 int vol
= strtoul(subarray
, &ep
, 10);
7729 if (*ep
!= '\0' || vol
>= super
->anchor
->num_raid_devs
)
7732 if (strcmp(update
, "ppl") == 0)
7733 new_policy
= RWH_MULTIPLE_DISTRIBUTED
;
7735 new_policy
= RWH_MULTIPLE_OFF
;
7737 if (st
->update_tail
) {
7738 struct imsm_update_rwh_policy
*u
= xmalloc(sizeof(*u
));
7740 u
->type
= update_rwh_policy
;
7742 u
->new_policy
= new_policy
;
7743 append_metadata_update(st
, u
, sizeof(*u
));
7745 struct imsm_dev
*dev
;
7747 dev
= get_imsm_dev(super
, vol
);
7748 dev
->rwh_policy
= new_policy
;
7749 super
->updates_pending
++;
7757 static int is_gen_migration(struct imsm_dev
*dev
)
7762 if (!dev
->vol
.migr_state
)
7765 if (migr_type(dev
) == MIGR_GEN_MIGR
)
7771 static int is_rebuilding(struct imsm_dev
*dev
)
7773 struct imsm_map
*migr_map
;
7775 if (!dev
->vol
.migr_state
)
7778 if (migr_type(dev
) != MIGR_REBUILD
)
7781 migr_map
= get_imsm_map(dev
, MAP_1
);
7783 if (migr_map
->map_state
== IMSM_T_STATE_DEGRADED
)
7789 static int is_initializing(struct imsm_dev
*dev
)
7791 struct imsm_map
*migr_map
;
7793 if (!dev
->vol
.migr_state
)
7796 if (migr_type(dev
) != MIGR_INIT
)
7799 migr_map
= get_imsm_map(dev
, MAP_1
);
7801 if (migr_map
->map_state
== IMSM_T_STATE_UNINITIALIZED
)
7807 static void update_recovery_start(struct intel_super
*super
,
7808 struct imsm_dev
*dev
,
7809 struct mdinfo
*array
)
7811 struct mdinfo
*rebuild
= NULL
;
7815 if (!is_rebuilding(dev
))
7818 /* Find the rebuild target, but punt on the dual rebuild case */
7819 for (d
= array
->devs
; d
; d
= d
->next
)
7820 if (d
->recovery_start
== 0) {
7827 /* (?) none of the disks are marked with
7828 * IMSM_ORD_REBUILD, so assume they are missing and the
7829 * disk_ord_tbl was not correctly updated
7831 dprintf("failed to locate out-of-sync disk\n");
7835 units
= __le32_to_cpu(dev
->vol
.curr_migr_unit
);
7836 rebuild
->recovery_start
= units
* blocks_per_migr_unit(super
, dev
);
7839 static int recover_backup_imsm(struct supertype
*st
, struct mdinfo
*info
);
7841 static struct mdinfo
*container_content_imsm(struct supertype
*st
, char *subarray
)
7843 /* Given a container loaded by load_super_imsm_all,
7844 * extract information about all the arrays into
7846 * If 'subarray' is given, just extract info about that array.
7848 * For each imsm_dev create an mdinfo, fill it in,
7849 * then look for matching devices in super->disks
7850 * and create appropriate device mdinfo.
7852 struct intel_super
*super
= st
->sb
;
7853 struct imsm_super
*mpb
= super
->anchor
;
7854 struct mdinfo
*rest
= NULL
;
7858 int spare_disks
= 0;
7859 int current_vol
= super
->current_vol
;
7861 /* do not assemble arrays when not all attributes are supported */
7862 if (imsm_check_attributes(mpb
->attributes
) == 0) {
7864 pr_err("Unsupported attributes in IMSM metadata.Arrays activation is blocked.\n");
7867 /* count spare devices, not used in maps
7869 for (d
= super
->disks
; d
; d
= d
->next
)
7873 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
7874 struct imsm_dev
*dev
;
7875 struct imsm_map
*map
;
7876 struct imsm_map
*map2
;
7877 struct mdinfo
*this;
7884 (i
!= strtoul(subarray
, &ep
, 10) || *ep
!= '\0'))
7887 dev
= get_imsm_dev(super
, i
);
7888 map
= get_imsm_map(dev
, MAP_0
);
7889 map2
= get_imsm_map(dev
, MAP_1
);
7890 level
= get_imsm_raid_level(map
);
7892 /* do not publish arrays that are in the middle of an
7893 * unsupported migration
7895 if (dev
->vol
.migr_state
&&
7896 (migr_type(dev
) == MIGR_STATE_CHANGE
)) {
7897 pr_err("cannot assemble volume '%.16s': unsupported migration in progress\n",
7901 /* do not publish arrays that are not support by controller's
7905 this = xmalloc(sizeof(*this));
7907 super
->current_vol
= i
;
7908 getinfo_super_imsm_volume(st
, this, NULL
);
7910 chunk
= __le16_to_cpu(map
->blocks_per_strip
) >> 1;
7911 /* mdadm does not support all metadata features- set the bit in all arrays state */
7912 if (!validate_geometry_imsm_orom(super
,
7913 level
, /* RAID level */
7914 imsm_level_to_layout(level
),
7915 map
->num_members
, /* raid disks */
7916 &chunk
, imsm_dev_size(dev
),
7918 pr_err("IMSM RAID geometry validation failed. Array %s activation is blocked.\n",
7920 this->array
.state
|=
7921 (1<<MD_SB_BLOCK_CONTAINER_RESHAPE
) |
7922 (1<<MD_SB_BLOCK_VOLUME
);
7925 /* if array has bad blocks, set suitable bit in all arrays state */
7927 this->array
.state
|=
7928 (1<<MD_SB_BLOCK_CONTAINER_RESHAPE
) |
7929 (1<<MD_SB_BLOCK_VOLUME
);
7931 for (slot
= 0 ; slot
< map
->num_members
; slot
++) {
7932 unsigned long long recovery_start
;
7933 struct mdinfo
*info_d
;
7941 idx
= get_imsm_disk_idx(dev
, slot
, MAP_0
);
7942 ord
= get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
);
7943 for (d
= super
->disks
; d
; d
= d
->next
)
7944 if (d
->index
== idx
)
7947 recovery_start
= MaxSector
;
7950 if (d
&& is_failed(&d
->disk
))
7952 if (!skip
&& (ord
& IMSM_ORD_REBUILD
))
7956 * if we skip some disks the array will be assmebled degraded;
7957 * reset resync start to avoid a dirty-degraded
7958 * situation when performing the intial sync
7963 if (!(dev
->vol
.dirty
& RAIDVOL_DIRTY
)) {
7964 if ((!able_to_resync(level
, missing
) ||
7965 recovery_start
== 0))
7966 this->resync_start
= MaxSector
;
7969 * FIXME handle dirty degraded
7976 info_d
= xcalloc(1, sizeof(*info_d
));
7977 info_d
->next
= this->devs
;
7978 this->devs
= info_d
;
7980 info_d
->disk
.number
= d
->index
;
7981 info_d
->disk
.major
= d
->major
;
7982 info_d
->disk
.minor
= d
->minor
;
7983 info_d
->disk
.raid_disk
= slot
;
7984 info_d
->recovery_start
= recovery_start
;
7986 if (slot
< map2
->num_members
)
7987 info_d
->disk
.state
= (1 << MD_DISK_ACTIVE
);
7989 this->array
.spare_disks
++;
7991 if (slot
< map
->num_members
)
7992 info_d
->disk
.state
= (1 << MD_DISK_ACTIVE
);
7994 this->array
.spare_disks
++;
7996 if (info_d
->recovery_start
== MaxSector
)
7997 this->array
.working_disks
++;
7999 info_d
->events
= __le32_to_cpu(mpb
->generation_num
);
8000 info_d
->data_offset
= pba_of_lba0(map
);
8001 info_d
->component_size
= calc_component_size(map
, dev
);
8003 if (map
->raid_level
== 5) {
8004 info_d
->ppl_sector
= this->ppl_sector
;
8005 info_d
->ppl_size
= this->ppl_size
;
8006 if (this->consistency_policy
== CONSISTENCY_POLICY_PPL
&&
8007 recovery_start
== 0)
8008 this->resync_start
= 0;
8011 info_d
->bb
.supported
= 1;
8012 get_volume_badblocks(super
->bbm_log
, ord_to_idx(ord
),
8013 info_d
->data_offset
,
8014 info_d
->component_size
,
8017 /* now that the disk list is up-to-date fixup recovery_start */
8018 update_recovery_start(super
, dev
, this);
8019 this->array
.spare_disks
+= spare_disks
;
8021 /* check for reshape */
8022 if (this->reshape_active
== 1)
8023 recover_backup_imsm(st
, this);
8027 super
->current_vol
= current_vol
;
8031 static __u8
imsm_check_degraded(struct intel_super
*super
, struct imsm_dev
*dev
,
8032 int failed
, int look_in_map
)
8034 struct imsm_map
*map
;
8036 map
= get_imsm_map(dev
, look_in_map
);
8039 return map
->map_state
== IMSM_T_STATE_UNINITIALIZED
?
8040 IMSM_T_STATE_UNINITIALIZED
: IMSM_T_STATE_NORMAL
;
8042 switch (get_imsm_raid_level(map
)) {
8044 return IMSM_T_STATE_FAILED
;
8047 if (failed
< map
->num_members
)
8048 return IMSM_T_STATE_DEGRADED
;
8050 return IMSM_T_STATE_FAILED
;
8055 * check to see if any mirrors have failed, otherwise we
8056 * are degraded. Even numbered slots are mirrored on
8060 /* gcc -Os complains that this is unused */
8061 int insync
= insync
;
8063 for (i
= 0; i
< map
->num_members
; i
++) {
8064 __u32 ord
= get_imsm_ord_tbl_ent(dev
, i
, MAP_X
);
8065 int idx
= ord_to_idx(ord
);
8066 struct imsm_disk
*disk
;
8068 /* reset the potential in-sync count on even-numbered
8069 * slots. num_copies is always 2 for imsm raid10
8074 disk
= get_imsm_disk(super
, idx
);
8075 if (!disk
|| is_failed(disk
) || ord
& IMSM_ORD_REBUILD
)
8078 /* no in-sync disks left in this mirror the
8082 return IMSM_T_STATE_FAILED
;
8085 return IMSM_T_STATE_DEGRADED
;
8089 return IMSM_T_STATE_DEGRADED
;
8091 return IMSM_T_STATE_FAILED
;
8097 return map
->map_state
;
8100 static int imsm_count_failed(struct intel_super
*super
, struct imsm_dev
*dev
,
8105 struct imsm_disk
*disk
;
8106 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8107 struct imsm_map
*prev
= get_imsm_map(dev
, MAP_1
);
8108 struct imsm_map
*map_for_loop
;
8113 /* at the beginning of migration we set IMSM_ORD_REBUILD on
8114 * disks that are being rebuilt. New failures are recorded to
8115 * map[0]. So we look through all the disks we started with and
8116 * see if any failures are still present, or if any new ones
8120 if (prev
&& (map
->num_members
< prev
->num_members
))
8121 map_for_loop
= prev
;
8123 for (i
= 0; i
< map_for_loop
->num_members
; i
++) {
8125 /* when MAP_X is passed both maps failures are counted
8128 (look_in_map
== MAP_1
|| look_in_map
== MAP_X
) &&
8129 i
< prev
->num_members
) {
8130 ord
= __le32_to_cpu(prev
->disk_ord_tbl
[i
]);
8131 idx_1
= ord_to_idx(ord
);
8133 disk
= get_imsm_disk(super
, idx_1
);
8134 if (!disk
|| is_failed(disk
) || ord
& IMSM_ORD_REBUILD
)
8137 if ((look_in_map
== MAP_0
|| look_in_map
== MAP_X
) &&
8138 i
< map
->num_members
) {
8139 ord
= __le32_to_cpu(map
->disk_ord_tbl
[i
]);
8140 idx
= ord_to_idx(ord
);
8143 disk
= get_imsm_disk(super
, idx
);
8144 if (!disk
|| is_failed(disk
) ||
8145 ord
& IMSM_ORD_REBUILD
)
8154 static int imsm_open_new(struct supertype
*c
, struct active_array
*a
,
8157 struct intel_super
*super
= c
->sb
;
8158 struct imsm_super
*mpb
= super
->anchor
;
8159 struct imsm_update_prealloc_bb_mem u
;
8161 if (atoi(inst
) >= mpb
->num_raid_devs
) {
8162 pr_err("subarry index %d, out of range\n", atoi(inst
));
8166 dprintf("imsm: open_new %s\n", inst
);
8167 a
->info
.container_member
= atoi(inst
);
8169 u
.type
= update_prealloc_badblocks_mem
;
8170 imsm_update_metadata_locally(c
, &u
, sizeof(u
));
8175 static int is_resyncing(struct imsm_dev
*dev
)
8177 struct imsm_map
*migr_map
;
8179 if (!dev
->vol
.migr_state
)
8182 if (migr_type(dev
) == MIGR_INIT
||
8183 migr_type(dev
) == MIGR_REPAIR
)
8186 if (migr_type(dev
) == MIGR_GEN_MIGR
)
8189 migr_map
= get_imsm_map(dev
, MAP_1
);
8191 if (migr_map
->map_state
== IMSM_T_STATE_NORMAL
&&
8192 dev
->vol
.migr_type
!= MIGR_GEN_MIGR
)
8198 /* return true if we recorded new information */
8199 static int mark_failure(struct intel_super
*super
,
8200 struct imsm_dev
*dev
, struct imsm_disk
*disk
, int idx
)
8204 struct imsm_map
*map
;
8205 char buf
[MAX_RAID_SERIAL_LEN
+3];
8206 unsigned int len
, shift
= 0;
8208 /* new failures are always set in map[0] */
8209 map
= get_imsm_map(dev
, MAP_0
);
8211 slot
= get_imsm_disk_slot(map
, idx
);
8215 ord
= __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
8216 if (is_failed(disk
) && (ord
& IMSM_ORD_REBUILD
))
8219 memcpy(buf
, disk
->serial
, MAX_RAID_SERIAL_LEN
);
8220 buf
[MAX_RAID_SERIAL_LEN
] = '\000';
8222 if ((len
= strlen(buf
)) >= MAX_RAID_SERIAL_LEN
)
8223 shift
= len
- MAX_RAID_SERIAL_LEN
+ 1;
8224 memcpy(disk
->serial
, &buf
[shift
], len
+ 1 - shift
);
8226 disk
->status
|= FAILED_DISK
;
8227 set_imsm_ord_tbl_ent(map
, slot
, idx
| IMSM_ORD_REBUILD
);
8228 /* mark failures in second map if second map exists and this disk
8230 * This is valid for migration, initialization and rebuild
8232 if (dev
->vol
.migr_state
) {
8233 struct imsm_map
*map2
= get_imsm_map(dev
, MAP_1
);
8234 int slot2
= get_imsm_disk_slot(map2
, idx
);
8236 if (slot2
< map2
->num_members
&& slot2
>= 0)
8237 set_imsm_ord_tbl_ent(map2
, slot2
,
8238 idx
| IMSM_ORD_REBUILD
);
8240 if (map
->failed_disk_num
== 0xff ||
8241 (!is_rebuilding(dev
) && map
->failed_disk_num
> slot
))
8242 map
->failed_disk_num
= slot
;
8244 clear_disk_badblocks(super
->bbm_log
, ord_to_idx(ord
));
8249 static void mark_missing(struct intel_super
*super
,
8250 struct imsm_dev
*dev
, struct imsm_disk
*disk
, int idx
)
8252 mark_failure(super
, dev
, disk
, idx
);
8254 if (disk
->scsi_id
== __cpu_to_le32(~(__u32
)0))
8257 disk
->scsi_id
= __cpu_to_le32(~(__u32
)0);
8258 memmove(&disk
->serial
[0], &disk
->serial
[1], MAX_RAID_SERIAL_LEN
- 1);
8261 static void handle_missing(struct intel_super
*super
, struct imsm_dev
*dev
)
8265 if (!super
->missing
)
8268 /* When orom adds replacement for missing disk it does
8269 * not remove entry of missing disk, but just updates map with
8270 * new added disk. So it is not enough just to test if there is
8271 * any missing disk, we have to look if there are any failed disks
8272 * in map to stop migration */
8274 dprintf("imsm: mark missing\n");
8275 /* end process for initialization and rebuild only
8277 if (is_gen_migration(dev
) == 0) {
8278 int failed
= imsm_count_failed(super
, dev
, MAP_0
);
8282 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8283 struct imsm_map
*map1
;
8284 int i
, ord
, ord_map1
;
8287 for (i
= 0; i
< map
->num_members
; i
++) {
8288 ord
= get_imsm_ord_tbl_ent(dev
, i
, MAP_0
);
8289 if (!(ord
& IMSM_ORD_REBUILD
))
8292 map1
= get_imsm_map(dev
, MAP_1
);
8296 ord_map1
= __le32_to_cpu(map1
->disk_ord_tbl
[i
]);
8297 if (ord_map1
& IMSM_ORD_REBUILD
)
8302 map_state
= imsm_check_degraded(super
, dev
,
8304 end_migration(dev
, super
, map_state
);
8308 for (dl
= super
->missing
; dl
; dl
= dl
->next
)
8309 mark_missing(super
, dev
, &dl
->disk
, dl
->index
);
8310 super
->updates_pending
++;
8313 static unsigned long long imsm_set_array_size(struct imsm_dev
*dev
,
8316 unsigned long long array_blocks
;
8317 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8318 int used_disks
= imsm_num_data_members(map
);
8320 if (used_disks
== 0) {
8321 /* when problems occures
8322 * return current array_blocks value
8324 array_blocks
= imsm_dev_size(dev
);
8326 return array_blocks
;
8329 /* set array size in metadata
8332 /* OLCE size change is caused by added disks
8334 array_blocks
= per_dev_array_size(map
) * used_disks
;
8336 /* Online Volume Size Change
8337 * Using available free space
8339 array_blocks
= new_size
;
8341 array_blocks
= round_size_to_mb(array_blocks
, used_disks
);
8342 set_imsm_dev_size(dev
, array_blocks
);
8344 return array_blocks
;
8347 static void imsm_set_disk(struct active_array
*a
, int n
, int state
);
8349 static void imsm_progress_container_reshape(struct intel_super
*super
)
8351 /* if no device has a migr_state, but some device has a
8352 * different number of members than the previous device, start
8353 * changing the number of devices in this device to match
8356 struct imsm_super
*mpb
= super
->anchor
;
8357 int prev_disks
= -1;
8361 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
8362 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
8363 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8364 struct imsm_map
*map2
;
8365 int prev_num_members
;
8367 if (dev
->vol
.migr_state
)
8370 if (prev_disks
== -1)
8371 prev_disks
= map
->num_members
;
8372 if (prev_disks
== map
->num_members
)
8375 /* OK, this array needs to enter reshape mode.
8376 * i.e it needs a migr_state
8379 copy_map_size
= sizeof_imsm_map(map
);
8380 prev_num_members
= map
->num_members
;
8381 map
->num_members
= prev_disks
;
8382 dev
->vol
.migr_state
= 1;
8383 dev
->vol
.curr_migr_unit
= 0;
8384 set_migr_type(dev
, MIGR_GEN_MIGR
);
8385 for (i
= prev_num_members
;
8386 i
< map
->num_members
; i
++)
8387 set_imsm_ord_tbl_ent(map
, i
, i
);
8388 map2
= get_imsm_map(dev
, MAP_1
);
8389 /* Copy the current map */
8390 memcpy(map2
, map
, copy_map_size
);
8391 map2
->num_members
= prev_num_members
;
8393 imsm_set_array_size(dev
, -1);
8394 super
->clean_migration_record_by_mdmon
= 1;
8395 super
->updates_pending
++;
8399 /* Handle dirty -> clean transititions, resync and reshape. Degraded and rebuild
8400 * states are handled in imsm_set_disk() with one exception, when a
8401 * resync is stopped due to a new failure this routine will set the
8402 * 'degraded' state for the array.
8404 static int imsm_set_array_state(struct active_array
*a
, int consistent
)
8406 int inst
= a
->info
.container_member
;
8407 struct intel_super
*super
= a
->container
->sb
;
8408 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
8409 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8410 int failed
= imsm_count_failed(super
, dev
, MAP_0
);
8411 __u8 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
8412 __u32 blocks_per_unit
;
8414 if (dev
->vol
.migr_state
&&
8415 dev
->vol
.migr_type
== MIGR_GEN_MIGR
) {
8416 /* array state change is blocked due to reshape action
8418 * - abort the reshape (if last_checkpoint is 0 and action!= reshape)
8419 * - finish the reshape (if last_checkpoint is big and action != reshape)
8420 * - update curr_migr_unit
8422 if (a
->curr_action
== reshape
) {
8423 /* still reshaping, maybe update curr_migr_unit */
8424 goto mark_checkpoint
;
8426 if (a
->last_checkpoint
== 0 && a
->prev_action
== reshape
) {
8427 /* for some reason we aborted the reshape.
8429 * disable automatic metadata rollback
8430 * user action is required to recover process
8433 struct imsm_map
*map2
=
8434 get_imsm_map(dev
, MAP_1
);
8435 dev
->vol
.migr_state
= 0;
8436 set_migr_type(dev
, 0);
8437 dev
->vol
.curr_migr_unit
= 0;
8439 sizeof_imsm_map(map2
));
8440 super
->updates_pending
++;
8443 if (a
->last_checkpoint
>= a
->info
.component_size
) {
8444 unsigned long long array_blocks
;
8448 used_disks
= imsm_num_data_members(map
);
8449 if (used_disks
> 0) {
8451 per_dev_array_size(map
) *
8454 round_size_to_mb(array_blocks
,
8456 a
->info
.custom_array_size
= array_blocks
;
8457 /* encourage manager to update array
8461 a
->check_reshape
= 1;
8463 /* finalize online capacity expansion/reshape */
8464 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
)
8466 mdi
->disk
.raid_disk
,
8469 imsm_progress_container_reshape(super
);
8474 /* before we activate this array handle any missing disks */
8475 if (consistent
== 2)
8476 handle_missing(super
, dev
);
8478 if (consistent
== 2 &&
8479 (!is_resync_complete(&a
->info
) ||
8480 map_state
!= IMSM_T_STATE_NORMAL
||
8481 dev
->vol
.migr_state
))
8484 if (is_resync_complete(&a
->info
)) {
8485 /* complete intialization / resync,
8486 * recovery and interrupted recovery is completed in
8489 if (is_resyncing(dev
)) {
8490 dprintf("imsm: mark resync done\n");
8491 end_migration(dev
, super
, map_state
);
8492 super
->updates_pending
++;
8493 a
->last_checkpoint
= 0;
8495 } else if ((!is_resyncing(dev
) && !failed
) &&
8496 (imsm_reshape_blocks_arrays_changes(super
) == 0)) {
8497 /* mark the start of the init process if nothing is failed */
8498 dprintf("imsm: mark resync start\n");
8499 if (map
->map_state
== IMSM_T_STATE_UNINITIALIZED
)
8500 migrate(dev
, super
, IMSM_T_STATE_NORMAL
, MIGR_INIT
);
8502 migrate(dev
, super
, IMSM_T_STATE_NORMAL
, MIGR_REPAIR
);
8503 super
->updates_pending
++;
8507 /* skip checkpointing for general migration,
8508 * it is controlled in mdadm
8510 if (is_gen_migration(dev
))
8511 goto skip_mark_checkpoint
;
8513 /* check if we can update curr_migr_unit from resync_start, recovery_start */
8514 blocks_per_unit
= blocks_per_migr_unit(super
, dev
);
8515 if (blocks_per_unit
) {
8519 units
= a
->last_checkpoint
/ blocks_per_unit
;
8522 /* check that we did not overflow 32-bits, and that
8523 * curr_migr_unit needs updating
8525 if (units32
== units
&&
8527 __le32_to_cpu(dev
->vol
.curr_migr_unit
) != units32
) {
8528 dprintf("imsm: mark checkpoint (%u)\n", units32
);
8529 dev
->vol
.curr_migr_unit
= __cpu_to_le32(units32
);
8530 super
->updates_pending
++;
8534 skip_mark_checkpoint
:
8535 /* mark dirty / clean */
8536 if (((dev
->vol
.dirty
& RAIDVOL_DIRTY
) && consistent
) ||
8537 (!(dev
->vol
.dirty
& RAIDVOL_DIRTY
) && !consistent
)) {
8538 dprintf("imsm: mark '%s'\n", consistent
? "clean" : "dirty");
8540 dev
->vol
.dirty
= RAIDVOL_CLEAN
;
8542 dev
->vol
.dirty
= RAIDVOL_DIRTY
;
8543 if (dev
->rwh_policy
== RWH_DISTRIBUTED
||
8544 dev
->rwh_policy
== RWH_MULTIPLE_DISTRIBUTED
)
8545 dev
->vol
.dirty
|= RAIDVOL_DSRECORD_VALID
;
8547 super
->updates_pending
++;
8553 static int imsm_disk_slot_to_ord(struct active_array
*a
, int slot
)
8555 int inst
= a
->info
.container_member
;
8556 struct intel_super
*super
= a
->container
->sb
;
8557 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
8558 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8560 if (slot
> map
->num_members
) {
8561 pr_err("imsm: imsm_disk_slot_to_ord %d out of range 0..%d\n",
8562 slot
, map
->num_members
- 1);
8569 return get_imsm_ord_tbl_ent(dev
, slot
, MAP_0
);
8572 static void imsm_set_disk(struct active_array
*a
, int n
, int state
)
8574 int inst
= a
->info
.container_member
;
8575 struct intel_super
*super
= a
->container
->sb
;
8576 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
8577 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8578 struct imsm_disk
*disk
;
8580 int recovery_not_finished
= 0;
8584 int rebuild_done
= 0;
8587 ord
= get_imsm_ord_tbl_ent(dev
, n
, MAP_X
);
8591 dprintf("imsm: set_disk %d:%x\n", n
, state
);
8592 disk
= get_imsm_disk(super
, ord_to_idx(ord
));
8594 /* check for new failures */
8595 if (disk
&& (state
& DS_FAULTY
)) {
8596 if (mark_failure(super
, dev
, disk
, ord_to_idx(ord
)))
8597 super
->updates_pending
++;
8600 /* check if in_sync */
8601 if (state
& DS_INSYNC
&& ord
& IMSM_ORD_REBUILD
&& is_rebuilding(dev
)) {
8602 struct imsm_map
*migr_map
= get_imsm_map(dev
, MAP_1
);
8604 set_imsm_ord_tbl_ent(migr_map
, n
, ord_to_idx(ord
));
8606 super
->updates_pending
++;
8609 failed
= imsm_count_failed(super
, dev
, MAP_0
);
8610 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
8612 /* check if recovery complete, newly degraded, or failed */
8613 dprintf("imsm: Detected transition to state ");
8614 switch (map_state
) {
8615 case IMSM_T_STATE_NORMAL
: /* transition to normal state */
8616 dprintf("normal: ");
8617 if (is_rebuilding(dev
)) {
8618 dprintf_cont("while rebuilding");
8619 /* check if recovery is really finished */
8620 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
)
8621 if (mdi
->recovery_start
!= MaxSector
) {
8622 recovery_not_finished
= 1;
8625 if (recovery_not_finished
) {
8627 dprintf("Rebuild has not finished yet, state not changed");
8628 if (a
->last_checkpoint
< mdi
->recovery_start
) {
8629 a
->last_checkpoint
= mdi
->recovery_start
;
8630 super
->updates_pending
++;
8634 end_migration(dev
, super
, map_state
);
8635 map
= get_imsm_map(dev
, MAP_0
);
8636 map
->failed_disk_num
= ~0;
8637 super
->updates_pending
++;
8638 a
->last_checkpoint
= 0;
8641 if (is_gen_migration(dev
)) {
8642 dprintf_cont("while general migration");
8643 if (a
->last_checkpoint
>= a
->info
.component_size
)
8644 end_migration(dev
, super
, map_state
);
8646 map
->map_state
= map_state
;
8647 map
= get_imsm_map(dev
, MAP_0
);
8648 map
->failed_disk_num
= ~0;
8649 super
->updates_pending
++;
8653 case IMSM_T_STATE_DEGRADED
: /* transition to degraded state */
8654 dprintf_cont("degraded: ");
8655 if (map
->map_state
!= map_state
&& !dev
->vol
.migr_state
) {
8656 dprintf_cont("mark degraded");
8657 map
->map_state
= map_state
;
8658 super
->updates_pending
++;
8659 a
->last_checkpoint
= 0;
8662 if (is_rebuilding(dev
)) {
8663 dprintf_cont("while rebuilding ");
8664 if (state
& DS_FAULTY
) {
8665 dprintf_cont("removing failed drive ");
8666 if (n
== map
->failed_disk_num
) {
8667 dprintf_cont("end migration");
8668 end_migration(dev
, super
, map_state
);
8669 a
->last_checkpoint
= 0;
8671 dprintf_cont("fail detected during rebuild, changing map state");
8672 map
->map_state
= map_state
;
8674 super
->updates_pending
++;
8680 /* check if recovery is really finished */
8681 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
)
8682 if (mdi
->recovery_start
!= MaxSector
) {
8683 recovery_not_finished
= 1;
8686 if (recovery_not_finished
) {
8688 dprintf_cont("Rebuild has not finished yet");
8689 if (a
->last_checkpoint
< mdi
->recovery_start
) {
8690 a
->last_checkpoint
=
8691 mdi
->recovery_start
;
8692 super
->updates_pending
++;
8697 dprintf_cont(" Rebuild done, still degraded");
8698 end_migration(dev
, super
, map_state
);
8699 a
->last_checkpoint
= 0;
8700 super
->updates_pending
++;
8702 for (i
= 0; i
< map
->num_members
; i
++) {
8703 int idx
= get_imsm_ord_tbl_ent(dev
, i
, MAP_0
);
8705 if (idx
& IMSM_ORD_REBUILD
)
8706 map
->failed_disk_num
= i
;
8708 super
->updates_pending
++;
8711 if (is_gen_migration(dev
)) {
8712 dprintf_cont("while general migration");
8713 if (a
->last_checkpoint
>= a
->info
.component_size
)
8714 end_migration(dev
, super
, map_state
);
8716 map
->map_state
= map_state
;
8717 manage_second_map(super
, dev
);
8719 super
->updates_pending
++;
8722 if (is_initializing(dev
)) {
8723 dprintf_cont("while initialization.");
8724 map
->map_state
= map_state
;
8725 super
->updates_pending
++;
8729 case IMSM_T_STATE_FAILED
: /* transition to failed state */
8730 dprintf_cont("failed: ");
8731 if (is_gen_migration(dev
)) {
8732 dprintf_cont("while general migration");
8733 map
->map_state
= map_state
;
8734 super
->updates_pending
++;
8737 if (map
->map_state
!= map_state
) {
8738 dprintf_cont("mark failed");
8739 end_migration(dev
, super
, map_state
);
8740 super
->updates_pending
++;
8741 a
->last_checkpoint
= 0;
8746 dprintf_cont("state %i\n", map_state
);
8751 static int store_imsm_mpb(int fd
, struct imsm_super
*mpb
)
8754 __u32 mpb_size
= __le32_to_cpu(mpb
->mpb_size
);
8755 unsigned long long dsize
;
8756 unsigned long long sectors
;
8757 unsigned int sector_size
;
8759 get_dev_sector_size(fd
, NULL
, §or_size
);
8760 get_dev_size(fd
, NULL
, &dsize
);
8762 if (mpb_size
> sector_size
) {
8763 /* -1 to account for anchor */
8764 sectors
= mpb_sectors(mpb
, sector_size
) - 1;
8766 /* write the extended mpb to the sectors preceeding the anchor */
8767 if (lseek64(fd
, dsize
- (sector_size
* (2 + sectors
)),
8771 if ((unsigned long long)write(fd
, buf
+ sector_size
,
8772 sector_size
* sectors
) != sector_size
* sectors
)
8776 /* first block is stored on second to last sector of the disk */
8777 if (lseek64(fd
, dsize
- (sector_size
* 2), SEEK_SET
) < 0)
8780 if ((unsigned int)write(fd
, buf
, sector_size
) != sector_size
)
8786 static void imsm_sync_metadata(struct supertype
*container
)
8788 struct intel_super
*super
= container
->sb
;
8790 dprintf("sync metadata: %d\n", super
->updates_pending
);
8791 if (!super
->updates_pending
)
8794 write_super_imsm(container
, 0);
8796 super
->updates_pending
= 0;
8799 static struct dl
*imsm_readd(struct intel_super
*super
, int idx
, struct active_array
*a
)
8801 struct imsm_dev
*dev
= get_imsm_dev(super
, a
->info
.container_member
);
8802 int i
= get_imsm_disk_idx(dev
, idx
, MAP_X
);
8805 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
8809 if (dl
&& is_failed(&dl
->disk
))
8813 dprintf("found %x:%x\n", dl
->major
, dl
->minor
);
8818 static struct dl
*imsm_add_spare(struct intel_super
*super
, int slot
,
8819 struct active_array
*a
, int activate_new
,
8820 struct mdinfo
*additional_test_list
)
8822 struct imsm_dev
*dev
= get_imsm_dev(super
, a
->info
.container_member
);
8823 int idx
= get_imsm_disk_idx(dev
, slot
, MAP_X
);
8824 struct imsm_super
*mpb
= super
->anchor
;
8825 struct imsm_map
*map
;
8826 unsigned long long pos
;
8831 __u32 array_start
= 0;
8832 __u32 array_end
= 0;
8834 struct mdinfo
*test_list
;
8836 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
8837 /* If in this array, skip */
8838 for (d
= a
->info
.devs
; d
; d
= d
->next
)
8839 if (d
->state_fd
>= 0 &&
8840 d
->disk
.major
== dl
->major
&&
8841 d
->disk
.minor
== dl
->minor
) {
8842 dprintf("%x:%x already in array\n",
8843 dl
->major
, dl
->minor
);
8848 test_list
= additional_test_list
;
8850 if (test_list
->disk
.major
== dl
->major
&&
8851 test_list
->disk
.minor
== dl
->minor
) {
8852 dprintf("%x:%x already in additional test list\n",
8853 dl
->major
, dl
->minor
);
8856 test_list
= test_list
->next
;
8861 /* skip in use or failed drives */
8862 if (is_failed(&dl
->disk
) || idx
== dl
->index
||
8864 dprintf("%x:%x status (failed: %d index: %d)\n",
8865 dl
->major
, dl
->minor
, is_failed(&dl
->disk
), idx
);
8869 /* skip pure spares when we are looking for partially
8870 * assimilated drives
8872 if (dl
->index
== -1 && !activate_new
)
8875 if (!drive_validate_sector_size(super
, dl
))
8878 /* Does this unused device have the requisite free space?
8879 * It needs to be able to cover all member volumes
8881 ex
= get_extents(super
, dl
, 1);
8883 dprintf("cannot get extents\n");
8886 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
8887 dev
= get_imsm_dev(super
, i
);
8888 map
= get_imsm_map(dev
, MAP_0
);
8890 /* check if this disk is already a member of
8893 if (get_imsm_disk_slot(map
, dl
->index
) >= 0)
8899 array_start
= pba_of_lba0(map
);
8900 array_end
= array_start
+
8901 per_dev_array_size(map
) - 1;
8904 /* check that we can start at pba_of_lba0 with
8905 * num_data_stripes*blocks_per_stripe of space
8907 if (array_start
>= pos
&& array_end
< ex
[j
].start
) {
8911 pos
= ex
[j
].start
+ ex
[j
].size
;
8913 } while (ex
[j
-1].size
);
8920 if (i
< mpb
->num_raid_devs
) {
8921 dprintf("%x:%x does not have %u to %u available\n",
8922 dl
->major
, dl
->minor
, array_start
, array_end
);
8932 static int imsm_rebuild_allowed(struct supertype
*cont
, int dev_idx
, int failed
)
8934 struct imsm_dev
*dev2
;
8935 struct imsm_map
*map
;
8941 dev2
= get_imsm_dev(cont
->sb
, dev_idx
);
8943 state
= imsm_check_degraded(cont
->sb
, dev2
, failed
, MAP_0
);
8944 if (state
== IMSM_T_STATE_FAILED
) {
8945 map
= get_imsm_map(dev2
, MAP_0
);
8948 for (slot
= 0; slot
< map
->num_members
; slot
++) {
8950 * Check if failed disks are deleted from intel
8951 * disk list or are marked to be deleted
8953 idx
= get_imsm_disk_idx(dev2
, slot
, MAP_X
);
8954 idisk
= get_imsm_dl_disk(cont
->sb
, idx
);
8956 * Do not rebuild the array if failed disks
8957 * from failed sub-array are not removed from
8961 is_failed(&idisk
->disk
) &&
8962 (idisk
->action
!= DISK_REMOVE
))
8970 static struct mdinfo
*imsm_activate_spare(struct active_array
*a
,
8971 struct metadata_update
**updates
)
8974 * Find a device with unused free space and use it to replace a
8975 * failed/vacant region in an array. We replace failed regions one a
8976 * array at a time. The result is that a new spare disk will be added
8977 * to the first failed array and after the monitor has finished
8978 * propagating failures the remainder will be consumed.
8980 * FIXME add a capability for mdmon to request spares from another
8984 struct intel_super
*super
= a
->container
->sb
;
8985 int inst
= a
->info
.container_member
;
8986 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
8987 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8988 int failed
= a
->info
.array
.raid_disks
;
8989 struct mdinfo
*rv
= NULL
;
8992 struct metadata_update
*mu
;
8994 struct imsm_update_activate_spare
*u
;
8999 for (d
= a
->info
.devs
; d
; d
= d
->next
) {
9000 if ((d
->curr_state
& DS_FAULTY
) &&
9002 /* wait for Removal to happen */
9004 if (d
->state_fd
>= 0)
9008 dprintf("imsm: activate spare: inst=%d failed=%d (%d) level=%d\n",
9009 inst
, failed
, a
->info
.array
.raid_disks
, a
->info
.array
.level
);
9011 if (imsm_reshape_blocks_arrays_changes(super
))
9014 /* Cannot activate another spare if rebuild is in progress already
9016 if (is_rebuilding(dev
)) {
9017 dprintf("imsm: No spare activation allowed. Rebuild in progress already.\n");
9021 if (a
->info
.array
.level
== 4)
9022 /* No repair for takeovered array
9023 * imsm doesn't support raid4
9027 if (imsm_check_degraded(super
, dev
, failed
, MAP_0
) !=
9028 IMSM_T_STATE_DEGRADED
)
9031 if (get_imsm_map(dev
, MAP_0
)->map_state
== IMSM_T_STATE_UNINITIALIZED
) {
9032 dprintf("imsm: No spare activation allowed. Volume is not initialized.\n");
9037 * If there are any failed disks check state of the other volume.
9038 * Block rebuild if the another one is failed until failed disks
9039 * are removed from container.
9042 dprintf("found failed disks in %.*s, check if there anotherfailed sub-array.\n",
9043 MAX_RAID_SERIAL_LEN
, dev
->volume
);
9044 /* check if states of the other volumes allow for rebuild */
9045 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
9047 allowed
= imsm_rebuild_allowed(a
->container
,
9055 /* For each slot, if it is not working, find a spare */
9056 for (i
= 0; i
< a
->info
.array
.raid_disks
; i
++) {
9057 for (d
= a
->info
.devs
; d
; d
= d
->next
)
9058 if (d
->disk
.raid_disk
== i
)
9060 dprintf("found %d: %p %x\n", i
, d
, d
?d
->curr_state
:0);
9061 if (d
&& (d
->state_fd
>= 0))
9065 * OK, this device needs recovery. Try to re-add the
9066 * previous occupant of this slot, if this fails see if
9067 * we can continue the assimilation of a spare that was
9068 * partially assimilated, finally try to activate a new
9071 dl
= imsm_readd(super
, i
, a
);
9073 dl
= imsm_add_spare(super
, i
, a
, 0, rv
);
9075 dl
= imsm_add_spare(super
, i
, a
, 1, rv
);
9079 /* found a usable disk with enough space */
9080 di
= xcalloc(1, sizeof(*di
));
9082 /* dl->index will be -1 in the case we are activating a
9083 * pristine spare. imsm_process_update() will create a
9084 * new index in this case. Once a disk is found to be
9085 * failed in all member arrays it is kicked from the
9088 di
->disk
.number
= dl
->index
;
9090 /* (ab)use di->devs to store a pointer to the device
9093 di
->devs
= (struct mdinfo
*) dl
;
9095 di
->disk
.raid_disk
= i
;
9096 di
->disk
.major
= dl
->major
;
9097 di
->disk
.minor
= dl
->minor
;
9099 di
->recovery_start
= 0;
9100 di
->data_offset
= pba_of_lba0(map
);
9101 di
->component_size
= a
->info
.component_size
;
9102 di
->container_member
= inst
;
9103 di
->bb
.supported
= 1;
9104 if (a
->info
.consistency_policy
== CONSISTENCY_POLICY_PPL
) {
9105 di
->ppl_sector
= get_ppl_sector(super
, inst
);
9106 di
->ppl_size
= MULTIPLE_PPL_AREA_SIZE_IMSM
>> 9;
9108 super
->random
= random32();
9112 dprintf("%x:%x to be %d at %llu\n", dl
->major
, dl
->minor
,
9113 i
, di
->data_offset
);
9117 /* No spares found */
9119 /* Now 'rv' has a list of devices to return.
9120 * Create a metadata_update record to update the
9121 * disk_ord_tbl for the array
9123 mu
= xmalloc(sizeof(*mu
));
9124 mu
->buf
= xcalloc(num_spares
,
9125 sizeof(struct imsm_update_activate_spare
));
9127 mu
->space_list
= NULL
;
9128 mu
->len
= sizeof(struct imsm_update_activate_spare
) * num_spares
;
9129 mu
->next
= *updates
;
9130 u
= (struct imsm_update_activate_spare
*) mu
->buf
;
9132 for (di
= rv
; di
; di
= di
->next
) {
9133 u
->type
= update_activate_spare
;
9134 u
->dl
= (struct dl
*) di
->devs
;
9136 u
->slot
= di
->disk
.raid_disk
;
9147 static int disks_overlap(struct intel_super
*super
, int idx
, struct imsm_update_create_array
*u
)
9149 struct imsm_dev
*dev
= get_imsm_dev(super
, idx
);
9150 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
9151 struct imsm_map
*new_map
= get_imsm_map(&u
->dev
, MAP_0
);
9152 struct disk_info
*inf
= get_disk_info(u
);
9153 struct imsm_disk
*disk
;
9157 for (i
= 0; i
< map
->num_members
; i
++) {
9158 disk
= get_imsm_disk(super
, get_imsm_disk_idx(dev
, i
, MAP_X
));
9159 for (j
= 0; j
< new_map
->num_members
; j
++)
9160 if (serialcmp(disk
->serial
, inf
[j
].serial
) == 0)
9167 static struct dl
*get_disk_super(struct intel_super
*super
, int major
, int minor
)
9171 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
9172 if (dl
->major
== major
&& dl
->minor
== minor
)
9177 static int remove_disk_super(struct intel_super
*super
, int major
, int minor
)
9183 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
9184 if (dl
->major
== major
&& dl
->minor
== minor
) {
9187 prev
->next
= dl
->next
;
9189 super
->disks
= dl
->next
;
9191 __free_imsm_disk(dl
);
9192 dprintf("removed %x:%x\n", major
, minor
);
9200 static void imsm_delete(struct intel_super
*super
, struct dl
**dlp
, unsigned index
);
9202 static int add_remove_disk_update(struct intel_super
*super
)
9204 int check_degraded
= 0;
9207 /* add/remove some spares to/from the metadata/contrainer */
9208 while (super
->disk_mgmt_list
) {
9209 struct dl
*disk_cfg
;
9211 disk_cfg
= super
->disk_mgmt_list
;
9212 super
->disk_mgmt_list
= disk_cfg
->next
;
9213 disk_cfg
->next
= NULL
;
9215 if (disk_cfg
->action
== DISK_ADD
) {
9216 disk_cfg
->next
= super
->disks
;
9217 super
->disks
= disk_cfg
;
9219 dprintf("added %x:%x\n",
9220 disk_cfg
->major
, disk_cfg
->minor
);
9221 } else if (disk_cfg
->action
== DISK_REMOVE
) {
9222 dprintf("Disk remove action processed: %x.%x\n",
9223 disk_cfg
->major
, disk_cfg
->minor
);
9224 disk
= get_disk_super(super
,
9228 /* store action status */
9229 disk
->action
= DISK_REMOVE
;
9230 /* remove spare disks only */
9231 if (disk
->index
== -1) {
9232 remove_disk_super(super
,
9236 disk_cfg
->fd
= disk
->fd
;
9240 /* release allocate disk structure */
9241 __free_imsm_disk(disk_cfg
);
9244 return check_degraded
;
9247 static int apply_reshape_migration_update(struct imsm_update_reshape_migration
*u
,
9248 struct intel_super
*super
,
9251 struct intel_dev
*id
;
9252 void **tofree
= NULL
;
9255 dprintf("(enter)\n");
9256 if (u
->subdev
< 0 || u
->subdev
> 1) {
9257 dprintf("imsm: Error: Wrong subdev: %i\n", u
->subdev
);
9260 if (space_list
== NULL
|| *space_list
== NULL
) {
9261 dprintf("imsm: Error: Memory is not allocated\n");
9265 for (id
= super
->devlist
; id
; id
= id
->next
) {
9266 if (id
->index
== (unsigned)u
->subdev
) {
9267 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->subdev
);
9268 struct imsm_map
*map
;
9269 struct imsm_dev
*new_dev
=
9270 (struct imsm_dev
*)*space_list
;
9271 struct imsm_map
*migr_map
= get_imsm_map(dev
, MAP_1
);
9273 struct dl
*new_disk
;
9275 if (new_dev
== NULL
)
9277 *space_list
= **space_list
;
9278 memcpy(new_dev
, dev
, sizeof_imsm_dev(dev
, 0));
9279 map
= get_imsm_map(new_dev
, MAP_0
);
9281 dprintf("imsm: Error: migration in progress");
9285 to_state
= map
->map_state
;
9286 if ((u
->new_level
== 5) && (map
->raid_level
== 0)) {
9288 /* this should not happen */
9289 if (u
->new_disks
[0] < 0) {
9290 map
->failed_disk_num
=
9291 map
->num_members
- 1;
9292 to_state
= IMSM_T_STATE_DEGRADED
;
9294 to_state
= IMSM_T_STATE_NORMAL
;
9296 migrate(new_dev
, super
, to_state
, MIGR_GEN_MIGR
);
9297 if (u
->new_level
> -1)
9298 map
->raid_level
= u
->new_level
;
9299 migr_map
= get_imsm_map(new_dev
, MAP_1
);
9300 if ((u
->new_level
== 5) &&
9301 (migr_map
->raid_level
== 0)) {
9302 int ord
= map
->num_members
- 1;
9303 migr_map
->num_members
--;
9304 if (u
->new_disks
[0] < 0)
9305 ord
|= IMSM_ORD_REBUILD
;
9306 set_imsm_ord_tbl_ent(map
,
9307 map
->num_members
- 1,
9311 tofree
= (void **)dev
;
9313 /* update chunk size
9315 if (u
->new_chunksize
> 0) {
9316 unsigned long long num_data_stripes
;
9317 struct imsm_map
*dest_map
=
9318 get_imsm_map(dev
, MAP_0
);
9320 imsm_num_data_members(dest_map
);
9322 if (used_disks
== 0)
9325 map
->blocks_per_strip
=
9326 __cpu_to_le16(u
->new_chunksize
* 2);
9328 imsm_dev_size(dev
) / used_disks
;
9329 num_data_stripes
/= map
->blocks_per_strip
;
9330 num_data_stripes
/= map
->num_domains
;
9331 set_num_data_stripes(map
, num_data_stripes
);
9334 /* ensure blocks_per_member has valid value
9336 set_blocks_per_member(map
,
9337 per_dev_array_size(map
) +
9338 NUM_BLOCKS_DIRTY_STRIPE_REGION
);
9342 if (u
->new_level
!= 5 || migr_map
->raid_level
!= 0 ||
9343 migr_map
->raid_level
== map
->raid_level
)
9346 if (u
->new_disks
[0] >= 0) {
9349 new_disk
= get_disk_super(super
,
9350 major(u
->new_disks
[0]),
9351 minor(u
->new_disks
[0]));
9352 dprintf("imsm: new disk for reshape is: %i:%i (%p, index = %i)\n",
9353 major(u
->new_disks
[0]),
9354 minor(u
->new_disks
[0]),
9355 new_disk
, new_disk
->index
);
9356 if (new_disk
== NULL
)
9357 goto error_disk_add
;
9359 new_disk
->index
= map
->num_members
- 1;
9360 /* slot to fill in autolayout
9362 new_disk
->raiddisk
= new_disk
->index
;
9363 new_disk
->disk
.status
|= CONFIGURED_DISK
;
9364 new_disk
->disk
.status
&= ~SPARE_DISK
;
9366 goto error_disk_add
;
9369 *tofree
= *space_list
;
9370 /* calculate new size
9372 imsm_set_array_size(new_dev
, -1);
9379 *space_list
= tofree
;
9383 dprintf("Error: imsm: Cannot find disk.\n");
9387 static int apply_size_change_update(struct imsm_update_size_change
*u
,
9388 struct intel_super
*super
)
9390 struct intel_dev
*id
;
9393 dprintf("(enter)\n");
9394 if (u
->subdev
< 0 || u
->subdev
> 1) {
9395 dprintf("imsm: Error: Wrong subdev: %i\n", u
->subdev
);
9399 for (id
= super
->devlist
; id
; id
= id
->next
) {
9400 if (id
->index
== (unsigned)u
->subdev
) {
9401 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->subdev
);
9402 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
9403 int used_disks
= imsm_num_data_members(map
);
9404 unsigned long long blocks_per_member
;
9405 unsigned long long num_data_stripes
;
9406 unsigned long long new_size_per_disk
;
9408 if (used_disks
== 0)
9411 /* calculate new size
9413 new_size_per_disk
= u
->new_size
/ used_disks
;
9414 blocks_per_member
= new_size_per_disk
+
9415 NUM_BLOCKS_DIRTY_STRIPE_REGION
;
9416 num_data_stripes
= new_size_per_disk
/
9417 map
->blocks_per_strip
;
9418 num_data_stripes
/= map
->num_domains
;
9419 dprintf("(size: %llu, blocks per member: %llu, num_data_stipes: %llu)\n",
9420 u
->new_size
, new_size_per_disk
,
9422 set_blocks_per_member(map
, blocks_per_member
);
9423 set_num_data_stripes(map
, num_data_stripes
);
9424 imsm_set_array_size(dev
, u
->new_size
);
9434 static int apply_update_activate_spare(struct imsm_update_activate_spare
*u
,
9435 struct intel_super
*super
,
9436 struct active_array
*active_array
)
9438 struct imsm_super
*mpb
= super
->anchor
;
9439 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->array
);
9440 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
9441 struct imsm_map
*migr_map
;
9442 struct active_array
*a
;
9443 struct imsm_disk
*disk
;
9450 int second_map_created
= 0;
9452 for (; u
; u
= u
->next
) {
9453 victim
= get_imsm_disk_idx(dev
, u
->slot
, MAP_X
);
9458 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
9463 pr_err("error: imsm_activate_spare passed an unknown disk (index: %d)\n",
9468 /* count failures (excluding rebuilds and the victim)
9469 * to determine map[0] state
9472 for (i
= 0; i
< map
->num_members
; i
++) {
9475 disk
= get_imsm_disk(super
,
9476 get_imsm_disk_idx(dev
, i
, MAP_X
));
9477 if (!disk
|| is_failed(disk
))
9481 /* adding a pristine spare, assign a new index */
9482 if (dl
->index
< 0) {
9483 dl
->index
= super
->anchor
->num_disks
;
9484 super
->anchor
->num_disks
++;
9487 disk
->status
|= CONFIGURED_DISK
;
9488 disk
->status
&= ~SPARE_DISK
;
9491 to_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
9492 if (!second_map_created
) {
9493 second_map_created
= 1;
9494 map
->map_state
= IMSM_T_STATE_DEGRADED
;
9495 migrate(dev
, super
, to_state
, MIGR_REBUILD
);
9497 map
->map_state
= to_state
;
9498 migr_map
= get_imsm_map(dev
, MAP_1
);
9499 set_imsm_ord_tbl_ent(map
, u
->slot
, dl
->index
);
9500 set_imsm_ord_tbl_ent(migr_map
, u
->slot
,
9501 dl
->index
| IMSM_ORD_REBUILD
);
9503 /* update the family_num to mark a new container
9504 * generation, being careful to record the existing
9505 * family_num in orig_family_num to clean up after
9506 * earlier mdadm versions that neglected to set it.
9508 if (mpb
->orig_family_num
== 0)
9509 mpb
->orig_family_num
= mpb
->family_num
;
9510 mpb
->family_num
+= super
->random
;
9512 /* count arrays using the victim in the metadata */
9514 for (a
= active_array
; a
; a
= a
->next
) {
9515 dev
= get_imsm_dev(super
, a
->info
.container_member
);
9516 map
= get_imsm_map(dev
, MAP_0
);
9518 if (get_imsm_disk_slot(map
, victim
) >= 0)
9522 /* delete the victim if it is no longer being
9528 /* We know that 'manager' isn't touching anything,
9529 * so it is safe to delete
9531 for (dlp
= &super
->disks
; *dlp
; dlp
= &(*dlp
)->next
)
9532 if ((*dlp
)->index
== victim
)
9535 /* victim may be on the missing list */
9537 for (dlp
= &super
->missing
; *dlp
;
9538 dlp
= &(*dlp
)->next
)
9539 if ((*dlp
)->index
== victim
)
9541 imsm_delete(super
, dlp
, victim
);
9548 static int apply_reshape_container_disks_update(struct imsm_update_reshape
*u
,
9549 struct intel_super
*super
,
9552 struct dl
*new_disk
;
9553 struct intel_dev
*id
;
9555 int delta_disks
= u
->new_raid_disks
- u
->old_raid_disks
;
9556 int disk_count
= u
->old_raid_disks
;
9557 void **tofree
= NULL
;
9558 int devices_to_reshape
= 1;
9559 struct imsm_super
*mpb
= super
->anchor
;
9561 unsigned int dev_id
;
9563 dprintf("(enter)\n");
9565 /* enable spares to use in array */
9566 for (i
= 0; i
< delta_disks
; i
++) {
9567 new_disk
= get_disk_super(super
,
9568 major(u
->new_disks
[i
]),
9569 minor(u
->new_disks
[i
]));
9570 dprintf("imsm: new disk for reshape is: %i:%i (%p, index = %i)\n",
9571 major(u
->new_disks
[i
]), minor(u
->new_disks
[i
]),
9572 new_disk
, new_disk
->index
);
9573 if (new_disk
== NULL
||
9574 (new_disk
->index
>= 0 &&
9575 new_disk
->index
< u
->old_raid_disks
))
9576 goto update_reshape_exit
;
9577 new_disk
->index
= disk_count
++;
9578 /* slot to fill in autolayout
9580 new_disk
->raiddisk
= new_disk
->index
;
9581 new_disk
->disk
.status
|=
9583 new_disk
->disk
.status
&= ~SPARE_DISK
;
9586 dprintf("imsm: volume set mpb->num_raid_devs = %i\n",
9587 mpb
->num_raid_devs
);
9588 /* manage changes in volume
9590 for (dev_id
= 0; dev_id
< mpb
->num_raid_devs
; dev_id
++) {
9591 void **sp
= *space_list
;
9592 struct imsm_dev
*newdev
;
9593 struct imsm_map
*newmap
, *oldmap
;
9595 for (id
= super
->devlist
; id
; id
= id
->next
) {
9596 if (id
->index
== dev_id
)
9605 /* Copy the dev, but not (all of) the map */
9606 memcpy(newdev
, id
->dev
, sizeof(*newdev
));
9607 oldmap
= get_imsm_map(id
->dev
, MAP_0
);
9608 newmap
= get_imsm_map(newdev
, MAP_0
);
9609 /* Copy the current map */
9610 memcpy(newmap
, oldmap
, sizeof_imsm_map(oldmap
));
9611 /* update one device only
9613 if (devices_to_reshape
) {
9614 dprintf("imsm: modifying subdev: %i\n",
9616 devices_to_reshape
--;
9617 newdev
->vol
.migr_state
= 1;
9618 newdev
->vol
.curr_migr_unit
= 0;
9619 set_migr_type(newdev
, MIGR_GEN_MIGR
);
9620 newmap
->num_members
= u
->new_raid_disks
;
9621 for (i
= 0; i
< delta_disks
; i
++) {
9622 set_imsm_ord_tbl_ent(newmap
,
9623 u
->old_raid_disks
+ i
,
9624 u
->old_raid_disks
+ i
);
9626 /* New map is correct, now need to save old map
9628 newmap
= get_imsm_map(newdev
, MAP_1
);
9629 memcpy(newmap
, oldmap
, sizeof_imsm_map(oldmap
));
9631 imsm_set_array_size(newdev
, -1);
9634 sp
= (void **)id
->dev
;
9639 /* Clear migration record */
9640 memset(super
->migr_rec
, 0, sizeof(struct migr_record
));
9643 *space_list
= tofree
;
9646 update_reshape_exit
:
9651 static int apply_takeover_update(struct imsm_update_takeover
*u
,
9652 struct intel_super
*super
,
9655 struct imsm_dev
*dev
= NULL
;
9656 struct intel_dev
*dv
;
9657 struct imsm_dev
*dev_new
;
9658 struct imsm_map
*map
;
9662 for (dv
= super
->devlist
; dv
; dv
= dv
->next
)
9663 if (dv
->index
== (unsigned int)u
->subarray
) {
9671 map
= get_imsm_map(dev
, MAP_0
);
9673 if (u
->direction
== R10_TO_R0
) {
9674 unsigned long long num_data_stripes
;
9676 /* Number of failed disks must be half of initial disk number */
9677 if (imsm_count_failed(super
, dev
, MAP_0
) !=
9678 (map
->num_members
/ 2))
9681 /* iterate through devices to mark removed disks as spare */
9682 for (dm
= super
->disks
; dm
; dm
= dm
->next
) {
9683 if (dm
->disk
.status
& FAILED_DISK
) {
9684 int idx
= dm
->index
;
9685 /* update indexes on the disk list */
9686 /* FIXME this loop-with-the-loop looks wrong, I'm not convinced
9687 the index values will end up being correct.... NB */
9688 for (du
= super
->disks
; du
; du
= du
->next
)
9689 if (du
->index
> idx
)
9691 /* mark as spare disk */
9696 map
->num_members
= map
->num_members
/ 2;
9697 map
->map_state
= IMSM_T_STATE_NORMAL
;
9698 map
->num_domains
= 1;
9699 map
->raid_level
= 0;
9700 map
->failed_disk_num
= -1;
9701 num_data_stripes
= imsm_dev_size(dev
) / 2;
9702 num_data_stripes
/= map
->blocks_per_strip
;
9703 set_num_data_stripes(map
, num_data_stripes
);
9706 if (u
->direction
== R0_TO_R10
) {
9708 unsigned long long num_data_stripes
;
9710 /* update slots in current disk list */
9711 for (dm
= super
->disks
; dm
; dm
= dm
->next
) {
9715 /* create new *missing* disks */
9716 for (i
= 0; i
< map
->num_members
; i
++) {
9717 space
= *space_list
;
9720 *space_list
= *space
;
9722 memcpy(du
, super
->disks
, sizeof(*du
));
9726 du
->index
= (i
* 2) + 1;
9727 sprintf((char *)du
->disk
.serial
,
9728 " MISSING_%d", du
->index
);
9729 sprintf((char *)du
->serial
,
9730 "MISSING_%d", du
->index
);
9731 du
->next
= super
->missing
;
9732 super
->missing
= du
;
9734 /* create new dev and map */
9735 space
= *space_list
;
9738 *space_list
= *space
;
9739 dev_new
= (void *)space
;
9740 memcpy(dev_new
, dev
, sizeof(*dev
));
9741 /* update new map */
9742 map
= get_imsm_map(dev_new
, MAP_0
);
9743 map
->num_members
= map
->num_members
* 2;
9744 map
->map_state
= IMSM_T_STATE_DEGRADED
;
9745 map
->num_domains
= 2;
9746 map
->raid_level
= 1;
9747 num_data_stripes
= imsm_dev_size(dev
) / 2;
9748 num_data_stripes
/= map
->blocks_per_strip
;
9749 num_data_stripes
/= map
->num_domains
;
9750 set_num_data_stripes(map
, num_data_stripes
);
9752 /* replace dev<->dev_new */
9755 /* update disk order table */
9756 for (du
= super
->disks
; du
; du
= du
->next
)
9758 set_imsm_ord_tbl_ent(map
, du
->index
, du
->index
);
9759 for (du
= super
->missing
; du
; du
= du
->next
)
9760 if (du
->index
>= 0) {
9761 set_imsm_ord_tbl_ent(map
, du
->index
, du
->index
);
9762 mark_missing(super
, dv
->dev
, &du
->disk
, du
->index
);
9768 static void imsm_process_update(struct supertype
*st
,
9769 struct metadata_update
*update
)
9772 * crack open the metadata_update envelope to find the update record
9773 * update can be one of:
9774 * update_reshape_container_disks - all the arrays in the container
9775 * are being reshaped to have more devices. We need to mark
9776 * the arrays for general migration and convert selected spares
9777 * into active devices.
9778 * update_activate_spare - a spare device has replaced a failed
9779 * device in an array, update the disk_ord_tbl. If this disk is
9780 * present in all member arrays then also clear the SPARE_DISK
9782 * update_create_array
9784 * update_rename_array
9785 * update_add_remove_disk
9787 struct intel_super
*super
= st
->sb
;
9788 struct imsm_super
*mpb
;
9789 enum imsm_update_type type
= *(enum imsm_update_type
*) update
->buf
;
9791 /* update requires a larger buf but the allocation failed */
9792 if (super
->next_len
&& !super
->next_buf
) {
9793 super
->next_len
= 0;
9797 if (super
->next_buf
) {
9798 memcpy(super
->next_buf
, super
->buf
, super
->len
);
9800 super
->len
= super
->next_len
;
9801 super
->buf
= super
->next_buf
;
9803 super
->next_len
= 0;
9804 super
->next_buf
= NULL
;
9807 mpb
= super
->anchor
;
9810 case update_general_migration_checkpoint
: {
9811 struct intel_dev
*id
;
9812 struct imsm_update_general_migration_checkpoint
*u
=
9813 (void *)update
->buf
;
9815 dprintf("called for update_general_migration_checkpoint\n");
9817 /* find device under general migration */
9818 for (id
= super
->devlist
; id
; id
= id
->next
) {
9819 if (is_gen_migration(id
->dev
)) {
9820 id
->dev
->vol
.curr_migr_unit
=
9821 __cpu_to_le32(u
->curr_migr_unit
);
9822 super
->updates_pending
++;
9827 case update_takeover
: {
9828 struct imsm_update_takeover
*u
= (void *)update
->buf
;
9829 if (apply_takeover_update(u
, super
, &update
->space_list
)) {
9830 imsm_update_version_info(super
);
9831 super
->updates_pending
++;
9836 case update_reshape_container_disks
: {
9837 struct imsm_update_reshape
*u
= (void *)update
->buf
;
9838 if (apply_reshape_container_disks_update(
9839 u
, super
, &update
->space_list
))
9840 super
->updates_pending
++;
9843 case update_reshape_migration
: {
9844 struct imsm_update_reshape_migration
*u
= (void *)update
->buf
;
9845 if (apply_reshape_migration_update(
9846 u
, super
, &update
->space_list
))
9847 super
->updates_pending
++;
9850 case update_size_change
: {
9851 struct imsm_update_size_change
*u
= (void *)update
->buf
;
9852 if (apply_size_change_update(u
, super
))
9853 super
->updates_pending
++;
9856 case update_activate_spare
: {
9857 struct imsm_update_activate_spare
*u
= (void *) update
->buf
;
9858 if (apply_update_activate_spare(u
, super
, st
->arrays
))
9859 super
->updates_pending
++;
9862 case update_create_array
: {
9863 /* someone wants to create a new array, we need to be aware of
9864 * a few races/collisions:
9865 * 1/ 'Create' called by two separate instances of mdadm
9866 * 2/ 'Create' versus 'activate_spare': mdadm has chosen
9867 * devices that have since been assimilated via
9869 * In the event this update can not be carried out mdadm will
9870 * (FIX ME) notice that its update did not take hold.
9872 struct imsm_update_create_array
*u
= (void *) update
->buf
;
9873 struct intel_dev
*dv
;
9874 struct imsm_dev
*dev
;
9875 struct imsm_map
*map
, *new_map
;
9876 unsigned long long start
, end
;
9877 unsigned long long new_start
, new_end
;
9879 struct disk_info
*inf
;
9882 /* handle racing creates: first come first serve */
9883 if (u
->dev_idx
< mpb
->num_raid_devs
) {
9884 dprintf("subarray %d already defined\n", u
->dev_idx
);
9888 /* check update is next in sequence */
9889 if (u
->dev_idx
!= mpb
->num_raid_devs
) {
9890 dprintf("can not create array %d expected index %d\n",
9891 u
->dev_idx
, mpb
->num_raid_devs
);
9895 new_map
= get_imsm_map(&u
->dev
, MAP_0
);
9896 new_start
= pba_of_lba0(new_map
);
9897 new_end
= new_start
+ per_dev_array_size(new_map
);
9898 inf
= get_disk_info(u
);
9900 /* handle activate_spare versus create race:
9901 * check to make sure that overlapping arrays do not include
9904 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
9905 dev
= get_imsm_dev(super
, i
);
9906 map
= get_imsm_map(dev
, MAP_0
);
9907 start
= pba_of_lba0(map
);
9908 end
= start
+ per_dev_array_size(map
);
9909 if ((new_start
>= start
&& new_start
<= end
) ||
9910 (start
>= new_start
&& start
<= new_end
))
9915 if (disks_overlap(super
, i
, u
)) {
9916 dprintf("arrays overlap\n");
9921 /* check that prepare update was successful */
9922 if (!update
->space
) {
9923 dprintf("prepare update failed\n");
9927 /* check that all disks are still active before committing
9928 * changes. FIXME: could we instead handle this by creating a
9929 * degraded array? That's probably not what the user expects,
9930 * so better to drop this update on the floor.
9932 for (i
= 0; i
< new_map
->num_members
; i
++) {
9933 dl
= serial_to_dl(inf
[i
].serial
, super
);
9935 dprintf("disk disappeared\n");
9940 super
->updates_pending
++;
9942 /* convert spares to members and fixup ord_tbl */
9943 for (i
= 0; i
< new_map
->num_members
; i
++) {
9944 dl
= serial_to_dl(inf
[i
].serial
, super
);
9945 if (dl
->index
== -1) {
9946 dl
->index
= mpb
->num_disks
;
9948 dl
->disk
.status
|= CONFIGURED_DISK
;
9949 dl
->disk
.status
&= ~SPARE_DISK
;
9951 set_imsm_ord_tbl_ent(new_map
, i
, dl
->index
);
9956 update
->space
= NULL
;
9957 imsm_copy_dev(dev
, &u
->dev
);
9958 dv
->index
= u
->dev_idx
;
9959 dv
->next
= super
->devlist
;
9960 super
->devlist
= dv
;
9961 mpb
->num_raid_devs
++;
9963 imsm_update_version_info(super
);
9966 /* mdmon knows how to release update->space, but not
9967 * ((struct intel_dev *) update->space)->dev
9969 if (update
->space
) {
9975 case update_kill_array
: {
9976 struct imsm_update_kill_array
*u
= (void *) update
->buf
;
9977 int victim
= u
->dev_idx
;
9978 struct active_array
*a
;
9979 struct intel_dev
**dp
;
9980 struct imsm_dev
*dev
;
9982 /* sanity check that we are not affecting the uuid of
9983 * active arrays, or deleting an active array
9985 * FIXME when immutable ids are available, but note that
9986 * we'll also need to fixup the invalidated/active
9987 * subarray indexes in mdstat
9989 for (a
= st
->arrays
; a
; a
= a
->next
)
9990 if (a
->info
.container_member
>= victim
)
9992 /* by definition if mdmon is running at least one array
9993 * is active in the container, so checking
9994 * mpb->num_raid_devs is just extra paranoia
9996 dev
= get_imsm_dev(super
, victim
);
9997 if (a
|| !dev
|| mpb
->num_raid_devs
== 1) {
9998 dprintf("failed to delete subarray-%d\n", victim
);
10002 for (dp
= &super
->devlist
; *dp
;)
10003 if ((*dp
)->index
== (unsigned)super
->current_vol
) {
10006 if ((*dp
)->index
> (unsigned)victim
)
10010 mpb
->num_raid_devs
--;
10011 super
->updates_pending
++;
10014 case update_rename_array
: {
10015 struct imsm_update_rename_array
*u
= (void *) update
->buf
;
10016 char name
[MAX_RAID_SERIAL_LEN
+1];
10017 int target
= u
->dev_idx
;
10018 struct active_array
*a
;
10019 struct imsm_dev
*dev
;
10021 /* sanity check that we are not affecting the uuid of
10024 memset(name
, 0, sizeof(name
));
10025 snprintf(name
, MAX_RAID_SERIAL_LEN
, "%s", (char *) u
->name
);
10026 name
[MAX_RAID_SERIAL_LEN
] = '\0';
10027 for (a
= st
->arrays
; a
; a
= a
->next
)
10028 if (a
->info
.container_member
== target
)
10030 dev
= get_imsm_dev(super
, u
->dev_idx
);
10031 if (a
|| !dev
|| !check_name(super
, name
, 1)) {
10032 dprintf("failed to rename subarray-%d\n", target
);
10036 memcpy(dev
->volume
, name
, MAX_RAID_SERIAL_LEN
);
10037 super
->updates_pending
++;
10040 case update_add_remove_disk
: {
10041 /* we may be able to repair some arrays if disks are
10042 * being added, check the status of add_remove_disk
10043 * if discs has been added.
10045 if (add_remove_disk_update(super
)) {
10046 struct active_array
*a
;
10048 super
->updates_pending
++;
10049 for (a
= st
->arrays
; a
; a
= a
->next
)
10050 a
->check_degraded
= 1;
10054 case update_prealloc_badblocks_mem
:
10056 case update_rwh_policy
: {
10057 struct imsm_update_rwh_policy
*u
= (void *)update
->buf
;
10058 int target
= u
->dev_idx
;
10059 struct imsm_dev
*dev
= get_imsm_dev(super
, target
);
10061 dprintf("could not find subarray-%d\n", target
);
10065 if (dev
->rwh_policy
!= u
->new_policy
) {
10066 dev
->rwh_policy
= u
->new_policy
;
10067 super
->updates_pending
++;
10072 pr_err("error: unsupported process update type:(type: %d)\n", type
);
10076 static struct mdinfo
*get_spares_for_grow(struct supertype
*st
);
10078 static int imsm_prepare_update(struct supertype
*st
,
10079 struct metadata_update
*update
)
10082 * Allocate space to hold new disk entries, raid-device entries or a new
10083 * mpb if necessary. The manager synchronously waits for updates to
10084 * complete in the monitor, so new mpb buffers allocated here can be
10085 * integrated by the monitor thread without worrying about live pointers
10086 * in the manager thread.
10088 enum imsm_update_type type
;
10089 struct intel_super
*super
= st
->sb
;
10090 unsigned int sector_size
= super
->sector_size
;
10091 struct imsm_super
*mpb
= super
->anchor
;
10095 if (update
->len
< (int)sizeof(type
))
10098 type
= *(enum imsm_update_type
*) update
->buf
;
10101 case update_general_migration_checkpoint
:
10102 if (update
->len
< (int)sizeof(struct imsm_update_general_migration_checkpoint
))
10104 dprintf("called for update_general_migration_checkpoint\n");
10106 case update_takeover
: {
10107 struct imsm_update_takeover
*u
= (void *)update
->buf
;
10108 if (update
->len
< (int)sizeof(*u
))
10110 if (u
->direction
== R0_TO_R10
) {
10111 void **tail
= (void **)&update
->space_list
;
10112 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->subarray
);
10113 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
10114 int num_members
= map
->num_members
;
10117 /* allocate memory for added disks */
10118 for (i
= 0; i
< num_members
; i
++) {
10119 size
= sizeof(struct dl
);
10120 space
= xmalloc(size
);
10125 /* allocate memory for new device */
10126 size
= sizeof_imsm_dev(super
->devlist
->dev
, 0) +
10127 (num_members
* sizeof(__u32
));
10128 space
= xmalloc(size
);
10132 len
= disks_to_mpb_size(num_members
* 2);
10137 case update_reshape_container_disks
: {
10138 /* Every raid device in the container is about to
10139 * gain some more devices, and we will enter a
10141 * So each 'imsm_map' will be bigger, and the imsm_vol
10142 * will now hold 2 of them.
10143 * Thus we need new 'struct imsm_dev' allocations sized
10144 * as sizeof_imsm_dev but with more devices in both maps.
10146 struct imsm_update_reshape
*u
= (void *)update
->buf
;
10147 struct intel_dev
*dl
;
10148 void **space_tail
= (void**)&update
->space_list
;
10150 if (update
->len
< (int)sizeof(*u
))
10153 dprintf("for update_reshape\n");
10155 for (dl
= super
->devlist
; dl
; dl
= dl
->next
) {
10156 int size
= sizeof_imsm_dev(dl
->dev
, 1);
10158 if (u
->new_raid_disks
> u
->old_raid_disks
)
10159 size
+= sizeof(__u32
)*2*
10160 (u
->new_raid_disks
- u
->old_raid_disks
);
10164 *space_tail
= NULL
;
10167 len
= disks_to_mpb_size(u
->new_raid_disks
);
10168 dprintf("New anchor length is %llu\n", (unsigned long long)len
);
10171 case update_reshape_migration
: {
10172 /* for migration level 0->5 we need to add disks
10173 * so the same as for container operation we will copy
10174 * device to the bigger location.
10175 * in memory prepared device and new disk area are prepared
10176 * for usage in process update
10178 struct imsm_update_reshape_migration
*u
= (void *)update
->buf
;
10179 struct intel_dev
*id
;
10180 void **space_tail
= (void **)&update
->space_list
;
10183 int current_level
= -1;
10185 if (update
->len
< (int)sizeof(*u
))
10188 dprintf("for update_reshape\n");
10190 /* add space for bigger array in update
10192 for (id
= super
->devlist
; id
; id
= id
->next
) {
10193 if (id
->index
== (unsigned)u
->subdev
) {
10194 size
= sizeof_imsm_dev(id
->dev
, 1);
10195 if (u
->new_raid_disks
> u
->old_raid_disks
)
10196 size
+= sizeof(__u32
)*2*
10197 (u
->new_raid_disks
- u
->old_raid_disks
);
10201 *space_tail
= NULL
;
10205 if (update
->space_list
== NULL
)
10208 /* add space for disk in update
10210 size
= sizeof(struct dl
);
10214 *space_tail
= NULL
;
10216 /* add spare device to update
10218 for (id
= super
->devlist
; id
; id
= id
->next
)
10219 if (id
->index
== (unsigned)u
->subdev
) {
10220 struct imsm_dev
*dev
;
10221 struct imsm_map
*map
;
10223 dev
= get_imsm_dev(super
, u
->subdev
);
10224 map
= get_imsm_map(dev
, MAP_0
);
10225 current_level
= map
->raid_level
;
10228 if (u
->new_level
== 5 && u
->new_level
!= current_level
) {
10229 struct mdinfo
*spares
;
10231 spares
= get_spares_for_grow(st
);
10234 struct mdinfo
*dev
;
10236 dev
= spares
->devs
;
10239 makedev(dev
->disk
.major
,
10241 dl
= get_disk_super(super
,
10244 dl
->index
= u
->old_raid_disks
;
10247 sysfs_free(spares
);
10250 len
= disks_to_mpb_size(u
->new_raid_disks
);
10251 dprintf("New anchor length is %llu\n", (unsigned long long)len
);
10254 case update_size_change
: {
10255 if (update
->len
< (int)sizeof(struct imsm_update_size_change
))
10259 case update_activate_spare
: {
10260 if (update
->len
< (int)sizeof(struct imsm_update_activate_spare
))
10264 case update_create_array
: {
10265 struct imsm_update_create_array
*u
= (void *) update
->buf
;
10266 struct intel_dev
*dv
;
10267 struct imsm_dev
*dev
= &u
->dev
;
10268 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
10270 struct disk_info
*inf
;
10274 if (update
->len
< (int)sizeof(*u
))
10277 inf
= get_disk_info(u
);
10278 len
= sizeof_imsm_dev(dev
, 1);
10279 /* allocate a new super->devlist entry */
10280 dv
= xmalloc(sizeof(*dv
));
10281 dv
->dev
= xmalloc(len
);
10282 update
->space
= dv
;
10284 /* count how many spares will be converted to members */
10285 for (i
= 0; i
< map
->num_members
; i
++) {
10286 dl
= serial_to_dl(inf
[i
].serial
, super
);
10288 /* hmm maybe it failed?, nothing we can do about
10293 if (count_memberships(dl
, super
) == 0)
10296 len
+= activate
* sizeof(struct imsm_disk
);
10299 case update_kill_array
: {
10300 if (update
->len
< (int)sizeof(struct imsm_update_kill_array
))
10304 case update_rename_array
: {
10305 if (update
->len
< (int)sizeof(struct imsm_update_rename_array
))
10309 case update_add_remove_disk
:
10310 /* no update->len needed */
10312 case update_prealloc_badblocks_mem
:
10313 super
->extra_space
+= sizeof(struct bbm_log
) -
10314 get_imsm_bbm_log_size(super
->bbm_log
);
10316 case update_rwh_policy
: {
10317 if (update
->len
< (int)sizeof(struct imsm_update_rwh_policy
))
10325 /* check if we need a larger metadata buffer */
10326 if (super
->next_buf
)
10327 buf_len
= super
->next_len
;
10329 buf_len
= super
->len
;
10331 if (__le32_to_cpu(mpb
->mpb_size
) + super
->extra_space
+ len
> buf_len
) {
10332 /* ok we need a larger buf than what is currently allocated
10333 * if this allocation fails process_update will notice that
10334 * ->next_len is set and ->next_buf is NULL
10336 buf_len
= ROUND_UP(__le32_to_cpu(mpb
->mpb_size
) +
10337 super
->extra_space
+ len
, sector_size
);
10338 if (super
->next_buf
)
10339 free(super
->next_buf
);
10341 super
->next_len
= buf_len
;
10342 if (posix_memalign(&super
->next_buf
, sector_size
, buf_len
) == 0)
10343 memset(super
->next_buf
, 0, buf_len
);
10345 super
->next_buf
= NULL
;
10350 /* must be called while manager is quiesced */
10351 static void imsm_delete(struct intel_super
*super
, struct dl
**dlp
, unsigned index
)
10353 struct imsm_super
*mpb
= super
->anchor
;
10355 struct imsm_dev
*dev
;
10356 struct imsm_map
*map
;
10357 unsigned int i
, j
, num_members
;
10358 __u32 ord
, ord_map0
;
10359 struct bbm_log
*log
= super
->bbm_log
;
10361 dprintf("deleting device[%d] from imsm_super\n", index
);
10363 /* shift all indexes down one */
10364 for (iter
= super
->disks
; iter
; iter
= iter
->next
)
10365 if (iter
->index
> (int)index
)
10367 for (iter
= super
->missing
; iter
; iter
= iter
->next
)
10368 if (iter
->index
> (int)index
)
10371 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
10372 dev
= get_imsm_dev(super
, i
);
10373 map
= get_imsm_map(dev
, MAP_0
);
10374 num_members
= map
->num_members
;
10375 for (j
= 0; j
< num_members
; j
++) {
10376 /* update ord entries being careful not to propagate
10377 * ord-flags to the first map
10379 ord
= get_imsm_ord_tbl_ent(dev
, j
, MAP_X
);
10380 ord_map0
= get_imsm_ord_tbl_ent(dev
, j
, MAP_0
);
10382 if (ord_to_idx(ord
) <= index
)
10385 map
= get_imsm_map(dev
, MAP_0
);
10386 set_imsm_ord_tbl_ent(map
, j
, ord_map0
- 1);
10387 map
= get_imsm_map(dev
, MAP_1
);
10389 set_imsm_ord_tbl_ent(map
, j
, ord
- 1);
10393 for (i
= 0; i
< log
->entry_count
; i
++) {
10394 struct bbm_log_entry
*entry
= &log
->marked_block_entries
[i
];
10396 if (entry
->disk_ordinal
<= index
)
10398 entry
->disk_ordinal
--;
10402 super
->updates_pending
++;
10404 struct dl
*dl
= *dlp
;
10406 *dlp
= (*dlp
)->next
;
10407 __free_imsm_disk(dl
);
10411 static void close_targets(int *targets
, int new_disks
)
10418 for (i
= 0; i
< new_disks
; i
++) {
10419 if (targets
[i
] >= 0) {
10426 static int imsm_get_allowed_degradation(int level
, int raid_disks
,
10427 struct intel_super
*super
,
10428 struct imsm_dev
*dev
)
10434 struct imsm_map
*map
;
10437 ret_val
= raid_disks
/2;
10438 /* check map if all disks pairs not failed
10441 map
= get_imsm_map(dev
, MAP_0
);
10442 for (i
= 0; i
< ret_val
; i
++) {
10443 int degradation
= 0;
10444 if (get_imsm_disk(super
, i
) == NULL
)
10446 if (get_imsm_disk(super
, i
+ 1) == NULL
)
10448 if (degradation
== 2)
10451 map
= get_imsm_map(dev
, MAP_1
);
10452 /* if there is no second map
10453 * result can be returned
10457 /* check degradation in second map
10459 for (i
= 0; i
< ret_val
; i
++) {
10460 int degradation
= 0;
10461 if (get_imsm_disk(super
, i
) == NULL
)
10463 if (get_imsm_disk(super
, i
+ 1) == NULL
)
10465 if (degradation
== 2)
10479 /*******************************************************************************
10480 * Function: open_backup_targets
10481 * Description: Function opens file descriptors for all devices given in
10484 * info : general array info
10485 * raid_disks : number of disks
10486 * raid_fds : table of device's file descriptors
10487 * super : intel super for raid10 degradation check
10488 * dev : intel device for raid10 degradation check
10492 ******************************************************************************/
10493 int open_backup_targets(struct mdinfo
*info
, int raid_disks
, int *raid_fds
,
10494 struct intel_super
*super
, struct imsm_dev
*dev
)
10500 for (i
= 0; i
< raid_disks
; i
++)
10503 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
10506 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
)) {
10507 dprintf("disk is faulty!!\n");
10511 if (sd
->disk
.raid_disk
>= raid_disks
|| sd
->disk
.raid_disk
< 0)
10514 dn
= map_dev(sd
->disk
.major
,
10515 sd
->disk
.minor
, 1);
10516 raid_fds
[sd
->disk
.raid_disk
] = dev_open(dn
, O_RDWR
);
10517 if (raid_fds
[sd
->disk
.raid_disk
] < 0) {
10518 pr_err("cannot open component\n");
10523 /* check if maximum array degradation level is not exceeded
10525 if ((raid_disks
- opened
) >
10526 imsm_get_allowed_degradation(info
->new_level
, raid_disks
,
10528 pr_err("Not enough disks can be opened.\n");
10529 close_targets(raid_fds
, raid_disks
);
10535 /*******************************************************************************
10536 * Function: validate_container_imsm
10537 * Description: This routine validates container after assemble,
10538 * eg. if devices in container are under the same controller.
10541 * info : linked list with info about devices used in array
10545 ******************************************************************************/
10546 int validate_container_imsm(struct mdinfo
*info
)
10548 if (check_env("IMSM_NO_PLATFORM"))
10551 struct sys_dev
*idev
;
10552 struct sys_dev
*hba
= NULL
;
10553 struct sys_dev
*intel_devices
= find_intel_devices();
10554 char *dev_path
= devt_to_devpath(makedev(info
->disk
.major
,
10555 info
->disk
.minor
));
10557 for (idev
= intel_devices
; idev
; idev
= idev
->next
) {
10558 if (dev_path
&& strstr(dev_path
, idev
->path
)) {
10567 pr_err("WARNING - Cannot detect HBA for device %s!\n",
10568 devid2kname(makedev(info
->disk
.major
, info
->disk
.minor
)));
10572 const struct imsm_orom
*orom
= get_orom_by_device_id(hba
->dev_id
);
10573 struct mdinfo
*dev
;
10575 for (dev
= info
->next
; dev
; dev
= dev
->next
) {
10576 dev_path
= devt_to_devpath(makedev(dev
->disk
.major
, dev
->disk
.minor
));
10578 struct sys_dev
*hba2
= NULL
;
10579 for (idev
= intel_devices
; idev
; idev
= idev
->next
) {
10580 if (dev_path
&& strstr(dev_path
, idev
->path
)) {
10588 const struct imsm_orom
*orom2
= hba2
== NULL
? NULL
:
10589 get_orom_by_device_id(hba2
->dev_id
);
10591 if (hba2
&& hba
->type
!= hba2
->type
) {
10592 pr_err("WARNING - HBAs of devices do not match %s != %s\n",
10593 get_sys_dev_type(hba
->type
), get_sys_dev_type(hba2
->type
));
10597 if (orom
!= orom2
) {
10598 pr_err("WARNING - IMSM container assembled with disks under different HBAs!\n"
10599 " This operation is not supported and can lead to data loss.\n");
10604 pr_err("WARNING - IMSM container assembled with disks under HBAs without IMSM platform support!\n"
10605 " This operation is not supported and can lead to data loss.\n");
10613 /*******************************************************************************
10614 * Function: imsm_record_badblock
10615 * Description: This routine stores new bad block record in BBM log
10618 * a : array containing a bad block
10619 * slot : disk number containing a bad block
10620 * sector : bad block sector
10621 * length : bad block sectors range
10625 ******************************************************************************/
10626 static int imsm_record_badblock(struct active_array
*a
, int slot
,
10627 unsigned long long sector
, int length
)
10629 struct intel_super
*super
= a
->container
->sb
;
10633 ord
= imsm_disk_slot_to_ord(a
, slot
);
10637 ret
= record_new_badblock(super
->bbm_log
, ord_to_idx(ord
), sector
,
10640 super
->updates_pending
++;
10644 /*******************************************************************************
10645 * Function: imsm_clear_badblock
10646 * Description: This routine clears bad block record from BBM log
10649 * a : array containing a bad block
10650 * slot : disk number containing a bad block
10651 * sector : bad block sector
10652 * length : bad block sectors range
10656 ******************************************************************************/
10657 static int imsm_clear_badblock(struct active_array
*a
, int slot
,
10658 unsigned long long sector
, int length
)
10660 struct intel_super
*super
= a
->container
->sb
;
10664 ord
= imsm_disk_slot_to_ord(a
, slot
);
10668 ret
= clear_badblock(super
->bbm_log
, ord_to_idx(ord
), sector
, length
);
10670 super
->updates_pending
++;
10674 /*******************************************************************************
10675 * Function: imsm_get_badblocks
10676 * Description: This routine get list of bad blocks for an array
10680 * slot : disk number
10682 * bb : structure containing bad blocks
10684 ******************************************************************************/
10685 static struct md_bb
*imsm_get_badblocks(struct active_array
*a
, int slot
)
10687 int inst
= a
->info
.container_member
;
10688 struct intel_super
*super
= a
->container
->sb
;
10689 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
10690 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
10693 ord
= imsm_disk_slot_to_ord(a
, slot
);
10697 get_volume_badblocks(super
->bbm_log
, ord_to_idx(ord
), pba_of_lba0(map
),
10698 per_dev_array_size(map
), &super
->bb
);
10702 /*******************************************************************************
10703 * Function: examine_badblocks_imsm
10704 * Description: Prints list of bad blocks on a disk to the standard output
10707 * st : metadata handler
10708 * fd : open file descriptor for device
10709 * devname : device name
10713 ******************************************************************************/
10714 static int examine_badblocks_imsm(struct supertype
*st
, int fd
, char *devname
)
10716 struct intel_super
*super
= st
->sb
;
10717 struct bbm_log
*log
= super
->bbm_log
;
10718 struct dl
*d
= NULL
;
10721 for (d
= super
->disks
; d
; d
= d
->next
) {
10722 if (strcmp(d
->devname
, devname
) == 0)
10726 if ((d
== NULL
) || (d
->index
< 0)) { /* serial mismatch probably */
10727 pr_err("%s doesn't appear to be part of a raid array\n",
10734 struct bbm_log_entry
*entry
= &log
->marked_block_entries
[0];
10736 for (i
= 0; i
< log
->entry_count
; i
++) {
10737 if (entry
[i
].disk_ordinal
== d
->index
) {
10738 unsigned long long sector
= __le48_to_cpu(
10739 &entry
[i
].defective_block_start
);
10740 int cnt
= entry
[i
].marked_count
+ 1;
10743 printf("Bad-blocks on %s:\n", devname
);
10747 printf("%20llu for %d sectors\n", sector
, cnt
);
10753 printf("No bad-blocks list configured on %s\n", devname
);
10757 /*******************************************************************************
10758 * Function: init_migr_record_imsm
10759 * Description: Function inits imsm migration record
10761 * super : imsm internal array info
10762 * dev : device under migration
10763 * info : general array info to find the smallest device
10766 ******************************************************************************/
10767 void init_migr_record_imsm(struct supertype
*st
, struct imsm_dev
*dev
,
10768 struct mdinfo
*info
)
10770 struct intel_super
*super
= st
->sb
;
10771 struct migr_record
*migr_rec
= super
->migr_rec
;
10772 int new_data_disks
;
10773 unsigned long long dsize
, dev_sectors
;
10774 long long unsigned min_dev_sectors
= -1LLU;
10778 struct imsm_map
*map_dest
= get_imsm_map(dev
, MAP_0
);
10779 struct imsm_map
*map_src
= get_imsm_map(dev
, MAP_1
);
10780 unsigned long long num_migr_units
;
10781 unsigned long long array_blocks
;
10783 memset(migr_rec
, 0, sizeof(struct migr_record
));
10784 migr_rec
->family_num
= __cpu_to_le32(super
->anchor
->family_num
);
10786 /* only ascending reshape supported now */
10787 migr_rec
->ascending_migr
= __cpu_to_le32(1);
10789 migr_rec
->dest_depth_per_unit
= GEN_MIGR_AREA_SIZE
/
10790 max(map_dest
->blocks_per_strip
, map_src
->blocks_per_strip
);
10791 migr_rec
->dest_depth_per_unit
*=
10792 max(map_dest
->blocks_per_strip
, map_src
->blocks_per_strip
);
10793 new_data_disks
= imsm_num_data_members(map_dest
);
10794 migr_rec
->blocks_per_unit
=
10795 __cpu_to_le32(migr_rec
->dest_depth_per_unit
* new_data_disks
);
10796 migr_rec
->dest_depth_per_unit
=
10797 __cpu_to_le32(migr_rec
->dest_depth_per_unit
);
10798 array_blocks
= info
->component_size
* new_data_disks
;
10800 array_blocks
/ __le32_to_cpu(migr_rec
->blocks_per_unit
);
10802 if (array_blocks
% __le32_to_cpu(migr_rec
->blocks_per_unit
))
10804 set_num_migr_units(migr_rec
, num_migr_units
);
10806 migr_rec
->post_migr_vol_cap
= dev
->size_low
;
10807 migr_rec
->post_migr_vol_cap_hi
= dev
->size_high
;
10809 /* Find the smallest dev */
10810 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
10811 sprintf(nm
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
10812 fd
= dev_open(nm
, O_RDONLY
);
10815 get_dev_size(fd
, NULL
, &dsize
);
10816 dev_sectors
= dsize
/ 512;
10817 if (dev_sectors
< min_dev_sectors
)
10818 min_dev_sectors
= dev_sectors
;
10821 set_migr_chkp_area_pba(migr_rec
, min_dev_sectors
-
10822 RAID_DISK_RESERVED_BLOCKS_IMSM_HI
);
10824 write_imsm_migr_rec(st
);
10829 /*******************************************************************************
10830 * Function: save_backup_imsm
10831 * Description: Function saves critical data stripes to Migration Copy Area
10832 * and updates the current migration unit status.
10833 * Use restore_stripes() to form a destination stripe,
10834 * and to write it to the Copy Area.
10836 * st : supertype information
10837 * dev : imsm device that backup is saved for
10838 * info : general array info
10839 * buf : input buffer
10840 * length : length of data to backup (blocks_per_unit)
10844 ******************************************************************************/
10845 int save_backup_imsm(struct supertype
*st
,
10846 struct imsm_dev
*dev
,
10847 struct mdinfo
*info
,
10852 struct intel_super
*super
= st
->sb
;
10853 unsigned long long *target_offsets
;
10856 struct imsm_map
*map_dest
= get_imsm_map(dev
, MAP_0
);
10857 int new_disks
= map_dest
->num_members
;
10858 int dest_layout
= 0;
10860 unsigned long long start
;
10861 int data_disks
= imsm_num_data_members(map_dest
);
10863 targets
= xmalloc(new_disks
* sizeof(int));
10865 for (i
= 0; i
< new_disks
; i
++)
10868 target_offsets
= xcalloc(new_disks
, sizeof(unsigned long long));
10870 start
= info
->reshape_progress
* 512;
10871 for (i
= 0; i
< new_disks
; i
++) {
10872 target_offsets
[i
] = migr_chkp_area_pba(super
->migr_rec
) * 512;
10873 /* move back copy area adderss, it will be moved forward
10874 * in restore_stripes() using start input variable
10876 target_offsets
[i
] -= start
/data_disks
;
10879 if (open_backup_targets(info
, new_disks
, targets
,
10883 dest_layout
= imsm_level_to_layout(map_dest
->raid_level
);
10884 dest_chunk
= __le16_to_cpu(map_dest
->blocks_per_strip
) * 512;
10886 if (restore_stripes(targets
, /* list of dest devices */
10887 target_offsets
, /* migration record offsets */
10890 map_dest
->raid_level
,
10892 -1, /* source backup file descriptor */
10893 0, /* input buf offset
10894 * always 0 buf is already offseted */
10898 pr_err("Error restoring stripes\n");
10906 close_targets(targets
, new_disks
);
10909 free(target_offsets
);
10914 /*******************************************************************************
10915 * Function: save_checkpoint_imsm
10916 * Description: Function called for current unit status update
10917 * in the migration record. It writes it to disk.
10919 * super : imsm internal array info
10920 * info : general array info
10924 * 2: failure, means no valid migration record
10925 * / no general migration in progress /
10926 ******************************************************************************/
10927 int save_checkpoint_imsm(struct supertype
*st
, struct mdinfo
*info
, int state
)
10929 struct intel_super
*super
= st
->sb
;
10930 unsigned long long blocks_per_unit
;
10931 unsigned long long curr_migr_unit
;
10933 if (load_imsm_migr_rec(super
, info
) != 0) {
10934 dprintf("imsm: ERROR: Cannot read migration record for checkpoint save.\n");
10938 blocks_per_unit
= __le32_to_cpu(super
->migr_rec
->blocks_per_unit
);
10939 if (blocks_per_unit
== 0) {
10940 dprintf("imsm: no migration in progress.\n");
10943 curr_migr_unit
= info
->reshape_progress
/ blocks_per_unit
;
10944 /* check if array is alligned to copy area
10945 * if it is not alligned, add one to current migration unit value
10946 * this can happend on array reshape finish only
10948 if (info
->reshape_progress
% blocks_per_unit
)
10951 set_current_migr_unit(super
->migr_rec
, curr_migr_unit
);
10952 super
->migr_rec
->rec_status
= __cpu_to_le32(state
);
10953 set_migr_dest_1st_member_lba(super
->migr_rec
,
10954 super
->migr_rec
->dest_depth_per_unit
* curr_migr_unit
);
10956 if (write_imsm_migr_rec(st
) < 0) {
10957 dprintf("imsm: Cannot write migration record outside backup area\n");
10964 /*******************************************************************************
10965 * Function: recover_backup_imsm
10966 * Description: Function recovers critical data from the Migration Copy Area
10967 * while assembling an array.
10969 * super : imsm internal array info
10970 * info : general array info
10972 * 0 : success (or there is no data to recover)
10974 ******************************************************************************/
10975 int recover_backup_imsm(struct supertype
*st
, struct mdinfo
*info
)
10977 struct intel_super
*super
= st
->sb
;
10978 struct migr_record
*migr_rec
= super
->migr_rec
;
10979 struct imsm_map
*map_dest
;
10980 struct intel_dev
*id
= NULL
;
10981 unsigned long long read_offset
;
10982 unsigned long long write_offset
;
10984 int *targets
= NULL
;
10985 int new_disks
, i
, err
;
10988 unsigned int sector_size
= super
->sector_size
;
10989 unsigned long curr_migr_unit
= current_migr_unit(migr_rec
);
10990 unsigned long num_migr_units
= get_num_migr_units(migr_rec
);
10992 int skipped_disks
= 0;
10994 err
= sysfs_get_str(info
, NULL
, "array_state", (char *)buffer
, 20);
10998 /* recover data only during assemblation */
10999 if (strncmp(buffer
, "inactive", 8) != 0)
11001 /* no data to recover */
11002 if (__le32_to_cpu(migr_rec
->rec_status
) == UNIT_SRC_NORMAL
)
11004 if (curr_migr_unit
>= num_migr_units
)
11007 /* find device during reshape */
11008 for (id
= super
->devlist
; id
; id
= id
->next
)
11009 if (is_gen_migration(id
->dev
))
11014 map_dest
= get_imsm_map(id
->dev
, MAP_0
);
11015 new_disks
= map_dest
->num_members
;
11017 read_offset
= migr_chkp_area_pba(migr_rec
) * 512;
11019 write_offset
= (migr_dest_1st_member_lba(migr_rec
) +
11020 pba_of_lba0(map_dest
)) * 512;
11022 unit_len
= __le32_to_cpu(migr_rec
->dest_depth_per_unit
) * 512;
11023 if (posix_memalign((void **)&buf
, sector_size
, unit_len
) != 0)
11025 targets
= xcalloc(new_disks
, sizeof(int));
11027 if (open_backup_targets(info
, new_disks
, targets
, super
, id
->dev
)) {
11028 pr_err("Cannot open some devices belonging to array.\n");
11032 for (i
= 0; i
< new_disks
; i
++) {
11033 if (targets
[i
] < 0) {
11037 if (lseek64(targets
[i
], read_offset
, SEEK_SET
) < 0) {
11038 pr_err("Cannot seek to block: %s\n",
11043 if ((unsigned)read(targets
[i
], buf
, unit_len
) != unit_len
) {
11044 pr_err("Cannot read copy area block: %s\n",
11049 if (lseek64(targets
[i
], write_offset
, SEEK_SET
) < 0) {
11050 pr_err("Cannot seek to block: %s\n",
11055 if ((unsigned)write(targets
[i
], buf
, unit_len
) != unit_len
) {
11056 pr_err("Cannot restore block: %s\n",
11063 if (skipped_disks
> imsm_get_allowed_degradation(info
->new_level
,
11067 pr_err("Cannot restore data from backup. Too many failed disks\n");
11071 if (save_checkpoint_imsm(st
, info
, UNIT_SRC_NORMAL
)) {
11072 /* ignore error == 2, this can mean end of reshape here
11074 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL) during restart\n");
11080 for (i
= 0; i
< new_disks
; i
++)
11089 static char disk_by_path
[] = "/dev/disk/by-path/";
11091 static const char *imsm_get_disk_controller_domain(const char *path
)
11093 char disk_path
[PATH_MAX
];
11097 strcpy(disk_path
, disk_by_path
);
11098 strncat(disk_path
, path
, PATH_MAX
- strlen(disk_path
) - 1);
11099 if (stat(disk_path
, &st
) == 0) {
11100 struct sys_dev
* hba
;
11103 path
= devt_to_devpath(st
.st_rdev
);
11106 hba
= find_disk_attached_hba(-1, path
);
11107 if (hba
&& hba
->type
== SYS_DEV_SAS
)
11109 else if (hba
&& hba
->type
== SYS_DEV_SATA
)
11111 else if (hba
&& hba
->type
== SYS_DEV_VMD
)
11113 else if (hba
&& hba
->type
== SYS_DEV_NVME
)
11117 dprintf("path: %s hba: %s attached: %s\n",
11118 path
, (hba
) ? hba
->path
: "NULL", drv
);
11124 static char *imsm_find_array_devnm_by_subdev(int subdev
, char *container
)
11126 static char devnm
[32];
11127 char subdev_name
[20];
11128 struct mdstat_ent
*mdstat
;
11130 sprintf(subdev_name
, "%d", subdev
);
11131 mdstat
= mdstat_by_subdev(subdev_name
, container
);
11135 strcpy(devnm
, mdstat
->devnm
);
11136 free_mdstat(mdstat
);
11140 static int imsm_reshape_is_allowed_on_container(struct supertype
*st
,
11141 struct geo_params
*geo
,
11142 int *old_raid_disks
,
11145 /* currently we only support increasing the number of devices
11146 * for a container. This increases the number of device for each
11147 * member array. They must all be RAID0 or RAID5.
11150 struct mdinfo
*info
, *member
;
11151 int devices_that_can_grow
= 0;
11153 dprintf("imsm: imsm_reshape_is_allowed_on_container(ENTER): st->devnm = (%s)\n", st
->devnm
);
11155 if (geo
->size
> 0 ||
11156 geo
->level
!= UnSet
||
11157 geo
->layout
!= UnSet
||
11158 geo
->chunksize
!= 0 ||
11159 geo
->raid_disks
== UnSet
) {
11160 dprintf("imsm: Container operation is allowed for raid disks number change only.\n");
11164 if (direction
== ROLLBACK_METADATA_CHANGES
) {
11165 dprintf("imsm: Metadata changes rollback is not supported for container operation.\n");
11169 info
= container_content_imsm(st
, NULL
);
11170 for (member
= info
; member
; member
= member
->next
) {
11173 dprintf("imsm: checking device_num: %i\n",
11174 member
->container_member
);
11176 if (geo
->raid_disks
<= member
->array
.raid_disks
) {
11177 /* we work on container for Online Capacity Expansion
11178 * only so raid_disks has to grow
11180 dprintf("imsm: for container operation raid disks increase is required\n");
11184 if (info
->array
.level
!= 0 && info
->array
.level
!= 5) {
11185 /* we cannot use this container with other raid level
11187 dprintf("imsm: for container operation wrong raid level (%i) detected\n",
11188 info
->array
.level
);
11191 /* check for platform support
11192 * for this raid level configuration
11194 struct intel_super
*super
= st
->sb
;
11195 if (!is_raid_level_supported(super
->orom
,
11196 member
->array
.level
,
11197 geo
->raid_disks
)) {
11198 dprintf("platform does not support raid%d with %d disk%s\n",
11201 geo
->raid_disks
> 1 ? "s" : "");
11204 /* check if component size is aligned to chunk size
11206 if (info
->component_size
%
11207 (info
->array
.chunk_size
/512)) {
11208 dprintf("Component size is not aligned to chunk size\n");
11213 if (*old_raid_disks
&&
11214 info
->array
.raid_disks
!= *old_raid_disks
)
11216 *old_raid_disks
= info
->array
.raid_disks
;
11218 /* All raid5 and raid0 volumes in container
11219 * have to be ready for Online Capacity Expansion
11220 * so they need to be assembled. We have already
11221 * checked that no recovery etc is happening.
11223 result
= imsm_find_array_devnm_by_subdev(member
->container_member
,
11224 st
->container_devnm
);
11225 if (result
== NULL
) {
11226 dprintf("imsm: cannot find array\n");
11229 devices_that_can_grow
++;
11232 if (!member
&& devices_that_can_grow
)
11236 dprintf("Container operation allowed\n");
11238 dprintf("Error: %i\n", ret_val
);
11243 /* Function: get_spares_for_grow
11244 * Description: Allocates memory and creates list of spare devices
11245 * avaliable in container. Checks if spare drive size is acceptable.
11246 * Parameters: Pointer to the supertype structure
11247 * Returns: Pointer to the list of spare devices (mdinfo structure) on success,
11250 static struct mdinfo
*get_spares_for_grow(struct supertype
*st
)
11252 struct spare_criteria sc
;
11254 get_spare_criteria_imsm(st
, &sc
);
11255 return container_choose_spares(st
, &sc
, NULL
, NULL
, NULL
, 0);
11258 /******************************************************************************
11259 * function: imsm_create_metadata_update_for_reshape
11260 * Function creates update for whole IMSM container.
11262 ******************************************************************************/
11263 static int imsm_create_metadata_update_for_reshape(
11264 struct supertype
*st
,
11265 struct geo_params
*geo
,
11266 int old_raid_disks
,
11267 struct imsm_update_reshape
**updatep
)
11269 struct intel_super
*super
= st
->sb
;
11270 struct imsm_super
*mpb
= super
->anchor
;
11271 int update_memory_size
;
11272 struct imsm_update_reshape
*u
;
11273 struct mdinfo
*spares
;
11276 struct mdinfo
*dev
;
11278 dprintf("(enter) raid_disks = %i\n", geo
->raid_disks
);
11280 delta_disks
= geo
->raid_disks
- old_raid_disks
;
11282 /* size of all update data without anchor */
11283 update_memory_size
= sizeof(struct imsm_update_reshape
);
11285 /* now add space for spare disks that we need to add. */
11286 update_memory_size
+= sizeof(u
->new_disks
[0]) * (delta_disks
- 1);
11288 u
= xcalloc(1, update_memory_size
);
11289 u
->type
= update_reshape_container_disks
;
11290 u
->old_raid_disks
= old_raid_disks
;
11291 u
->new_raid_disks
= geo
->raid_disks
;
11293 /* now get spare disks list
11295 spares
= get_spares_for_grow(st
);
11297 if (spares
== NULL
|| delta_disks
> spares
->array
.spare_disks
) {
11298 pr_err("imsm: ERROR: Cannot get spare devices for %s.\n", geo
->dev_name
);
11303 /* we have got spares
11304 * update disk list in imsm_disk list table in anchor
11306 dprintf("imsm: %i spares are available.\n\n",
11307 spares
->array
.spare_disks
);
11309 dev
= spares
->devs
;
11310 for (i
= 0; i
< delta_disks
; i
++) {
11315 u
->new_disks
[i
] = makedev(dev
->disk
.major
,
11317 dl
= get_disk_super(super
, dev
->disk
.major
, dev
->disk
.minor
);
11318 dl
->index
= mpb
->num_disks
;
11326 sysfs_free(spares
);
11328 dprintf("imsm: reshape update preparation :");
11329 if (i
== delta_disks
) {
11330 dprintf_cont(" OK\n");
11332 return update_memory_size
;
11335 dprintf_cont(" Error\n");
11340 /******************************************************************************
11341 * function: imsm_create_metadata_update_for_size_change()
11342 * Creates update for IMSM array for array size change.
11344 ******************************************************************************/
11345 static int imsm_create_metadata_update_for_size_change(
11346 struct supertype
*st
,
11347 struct geo_params
*geo
,
11348 struct imsm_update_size_change
**updatep
)
11350 struct intel_super
*super
= st
->sb
;
11351 int update_memory_size
;
11352 struct imsm_update_size_change
*u
;
11354 dprintf("(enter) New size = %llu\n", geo
->size
);
11356 /* size of all update data without anchor */
11357 update_memory_size
= sizeof(struct imsm_update_size_change
);
11359 u
= xcalloc(1, update_memory_size
);
11360 u
->type
= update_size_change
;
11361 u
->subdev
= super
->current_vol
;
11362 u
->new_size
= geo
->size
;
11364 dprintf("imsm: reshape update preparation : OK\n");
11367 return update_memory_size
;
11370 /******************************************************************************
11371 * function: imsm_create_metadata_update_for_migration()
11372 * Creates update for IMSM array.
11374 ******************************************************************************/
11375 static int imsm_create_metadata_update_for_migration(
11376 struct supertype
*st
,
11377 struct geo_params
*geo
,
11378 struct imsm_update_reshape_migration
**updatep
)
11380 struct intel_super
*super
= st
->sb
;
11381 int update_memory_size
;
11382 struct imsm_update_reshape_migration
*u
;
11383 struct imsm_dev
*dev
;
11384 int previous_level
= -1;
11386 dprintf("(enter) New Level = %i\n", geo
->level
);
11388 /* size of all update data without anchor */
11389 update_memory_size
= sizeof(struct imsm_update_reshape_migration
);
11391 u
= xcalloc(1, update_memory_size
);
11392 u
->type
= update_reshape_migration
;
11393 u
->subdev
= super
->current_vol
;
11394 u
->new_level
= geo
->level
;
11395 u
->new_layout
= geo
->layout
;
11396 u
->new_raid_disks
= u
->old_raid_disks
= geo
->raid_disks
;
11397 u
->new_disks
[0] = -1;
11398 u
->new_chunksize
= -1;
11400 dev
= get_imsm_dev(super
, u
->subdev
);
11402 struct imsm_map
*map
;
11404 map
= get_imsm_map(dev
, MAP_0
);
11406 int current_chunk_size
=
11407 __le16_to_cpu(map
->blocks_per_strip
) / 2;
11409 if (geo
->chunksize
!= current_chunk_size
) {
11410 u
->new_chunksize
= geo
->chunksize
/ 1024;
11411 dprintf("imsm: chunk size change from %i to %i\n",
11412 current_chunk_size
, u
->new_chunksize
);
11414 previous_level
= map
->raid_level
;
11417 if (geo
->level
== 5 && previous_level
== 0) {
11418 struct mdinfo
*spares
= NULL
;
11420 u
->new_raid_disks
++;
11421 spares
= get_spares_for_grow(st
);
11422 if (spares
== NULL
|| spares
->array
.spare_disks
< 1) {
11424 sysfs_free(spares
);
11425 update_memory_size
= 0;
11426 pr_err("cannot get spare device for requested migration\n");
11429 sysfs_free(spares
);
11431 dprintf("imsm: reshape update preparation : OK\n");
11434 return update_memory_size
;
11437 static void imsm_update_metadata_locally(struct supertype
*st
,
11438 void *buf
, int len
)
11440 struct metadata_update mu
;
11445 mu
.space_list
= NULL
;
11447 if (imsm_prepare_update(st
, &mu
))
11448 imsm_process_update(st
, &mu
);
11450 while (mu
.space_list
) {
11451 void **space
= mu
.space_list
;
11452 mu
.space_list
= *space
;
11457 /***************************************************************************
11458 * Function: imsm_analyze_change
11459 * Description: Function analyze change for single volume
11460 * and validate if transition is supported
11461 * Parameters: Geometry parameters, supertype structure,
11462 * metadata change direction (apply/rollback)
11463 * Returns: Operation type code on success, -1 if fail
11464 ****************************************************************************/
11465 enum imsm_reshape_type
imsm_analyze_change(struct supertype
*st
,
11466 struct geo_params
*geo
,
11469 struct mdinfo info
;
11471 int check_devs
= 0;
11473 /* number of added/removed disks in operation result */
11474 int devNumChange
= 0;
11475 /* imsm compatible layout value for array geometry verification */
11476 int imsm_layout
= -1;
11478 struct imsm_dev
*dev
;
11479 struct imsm_map
*map
;
11480 struct intel_super
*super
;
11481 unsigned long long current_size
;
11482 unsigned long long free_size
;
11483 unsigned long long max_size
;
11486 getinfo_super_imsm_volume(st
, &info
, NULL
);
11487 if (geo
->level
!= info
.array
.level
&& geo
->level
>= 0 &&
11488 geo
->level
!= UnSet
) {
11489 switch (info
.array
.level
) {
11491 if (geo
->level
== 5) {
11492 change
= CH_MIGRATION
;
11493 if (geo
->layout
!= ALGORITHM_LEFT_ASYMMETRIC
) {
11494 pr_err("Error. Requested Layout not supported (left-asymmetric layout is supported only)!\n");
11496 goto analyse_change_exit
;
11498 imsm_layout
= geo
->layout
;
11500 devNumChange
= 1; /* parity disk added */
11501 } else if (geo
->level
== 10) {
11502 change
= CH_TAKEOVER
;
11504 devNumChange
= 2; /* two mirrors added */
11505 imsm_layout
= 0x102; /* imsm supported layout */
11510 if (geo
->level
== 0) {
11511 change
= CH_TAKEOVER
;
11513 devNumChange
= -(geo
->raid_disks
/2);
11514 imsm_layout
= 0; /* imsm raid0 layout */
11518 if (change
== -1) {
11519 pr_err("Error. Level Migration from %d to %d not supported!\n",
11520 info
.array
.level
, geo
->level
);
11521 goto analyse_change_exit
;
11524 geo
->level
= info
.array
.level
;
11526 if (geo
->layout
!= info
.array
.layout
&&
11527 (geo
->layout
!= UnSet
&& geo
->layout
!= -1)) {
11528 change
= CH_MIGRATION
;
11529 if (info
.array
.layout
== 0 && info
.array
.level
== 5 &&
11530 geo
->layout
== 5) {
11531 /* reshape 5 -> 4 */
11532 } else if (info
.array
.layout
== 5 && info
.array
.level
== 5 &&
11533 geo
->layout
== 0) {
11534 /* reshape 4 -> 5 */
11538 pr_err("Error. Layout Migration from %d to %d not supported!\n",
11539 info
.array
.layout
, geo
->layout
);
11541 goto analyse_change_exit
;
11544 geo
->layout
= info
.array
.layout
;
11545 if (imsm_layout
== -1)
11546 imsm_layout
= info
.array
.layout
;
11549 if (geo
->chunksize
> 0 && geo
->chunksize
!= UnSet
&&
11550 geo
->chunksize
!= info
.array
.chunk_size
) {
11551 if (info
.array
.level
== 10) {
11552 pr_err("Error. Chunk size change for RAID 10 is not supported.\n");
11554 goto analyse_change_exit
;
11555 } else if (info
.component_size
% (geo
->chunksize
/512)) {
11556 pr_err("New chunk size (%dK) does not evenly divide device size (%lluk). Aborting...\n",
11557 geo
->chunksize
/1024, info
.component_size
/2);
11559 goto analyse_change_exit
;
11561 change
= CH_MIGRATION
;
11563 geo
->chunksize
= info
.array
.chunk_size
;
11566 chunk
= geo
->chunksize
/ 1024;
11569 dev
= get_imsm_dev(super
, super
->current_vol
);
11570 map
= get_imsm_map(dev
, MAP_0
);
11571 data_disks
= imsm_num_data_members(map
);
11572 /* compute current size per disk member
11574 current_size
= info
.custom_array_size
/ data_disks
;
11576 if (geo
->size
> 0 && geo
->size
!= MAX_SIZE
) {
11577 /* align component size
11579 geo
->size
= imsm_component_size_alignment_check(
11580 get_imsm_raid_level(dev
->vol
.map
),
11581 chunk
* 1024, super
->sector_size
,
11583 if (geo
->size
== 0) {
11584 pr_err("Error. Size expansion is supported only (current size is %llu, requested size /rounded/ is 0).\n",
11586 goto analyse_change_exit
;
11590 if (current_size
!= geo
->size
&& geo
->size
> 0) {
11591 if (change
!= -1) {
11592 pr_err("Error. Size change should be the only one at a time.\n");
11594 goto analyse_change_exit
;
11596 if ((super
->current_vol
+ 1) != super
->anchor
->num_raid_devs
) {
11597 pr_err("Error. The last volume in container can be expanded only (%i/%s).\n",
11598 super
->current_vol
, st
->devnm
);
11599 goto analyse_change_exit
;
11601 /* check the maximum available size
11603 rv
= imsm_get_free_size(st
, dev
->vol
.map
->num_members
,
11604 0, chunk
, &free_size
);
11606 /* Cannot find maximum available space
11610 max_size
= free_size
+ current_size
;
11611 /* align component size
11613 max_size
= imsm_component_size_alignment_check(
11614 get_imsm_raid_level(dev
->vol
.map
),
11615 chunk
* 1024, super
->sector_size
,
11618 if (geo
->size
== MAX_SIZE
) {
11619 /* requested size change to the maximum available size
11621 if (max_size
== 0) {
11622 pr_err("Error. Cannot find maximum available space.\n");
11624 goto analyse_change_exit
;
11626 geo
->size
= max_size
;
11629 if (direction
== ROLLBACK_METADATA_CHANGES
) {
11630 /* accept size for rollback only
11633 /* round size due to metadata compatibility
11635 geo
->size
= (geo
->size
>> SECT_PER_MB_SHIFT
)
11636 << SECT_PER_MB_SHIFT
;
11637 dprintf("Prepare update for size change to %llu\n",
11639 if (current_size
>= geo
->size
) {
11640 pr_err("Error. Size expansion is supported only (current size is %llu, requested size /rounded/ is %llu).\n",
11641 current_size
, geo
->size
);
11642 goto analyse_change_exit
;
11644 if (max_size
&& geo
->size
> max_size
) {
11645 pr_err("Error. Requested size is larger than maximum available size (maximum available size is %llu, requested size /rounded/ is %llu).\n",
11646 max_size
, geo
->size
);
11647 goto analyse_change_exit
;
11650 geo
->size
*= data_disks
;
11651 geo
->raid_disks
= dev
->vol
.map
->num_members
;
11652 change
= CH_ARRAY_SIZE
;
11654 if (!validate_geometry_imsm(st
,
11657 geo
->raid_disks
+ devNumChange
,
11659 geo
->size
, INVALID_SECTORS
,
11660 0, 0, info
.consistency_policy
, 1))
11664 struct intel_super
*super
= st
->sb
;
11665 struct imsm_super
*mpb
= super
->anchor
;
11667 if (mpb
->num_raid_devs
> 1) {
11668 pr_err("Error. Cannot perform operation on %s- for this operation it MUST be single array in container\n",
11674 analyse_change_exit
:
11675 if (direction
== ROLLBACK_METADATA_CHANGES
&&
11676 (change
== CH_MIGRATION
|| change
== CH_TAKEOVER
)) {
11677 dprintf("imsm: Metadata changes rollback is not supported for migration and takeover operations.\n");
11683 int imsm_takeover(struct supertype
*st
, struct geo_params
*geo
)
11685 struct intel_super
*super
= st
->sb
;
11686 struct imsm_update_takeover
*u
;
11688 u
= xmalloc(sizeof(struct imsm_update_takeover
));
11690 u
->type
= update_takeover
;
11691 u
->subarray
= super
->current_vol
;
11693 /* 10->0 transition */
11694 if (geo
->level
== 0)
11695 u
->direction
= R10_TO_R0
;
11697 /* 0->10 transition */
11698 if (geo
->level
== 10)
11699 u
->direction
= R0_TO_R10
;
11701 /* update metadata locally */
11702 imsm_update_metadata_locally(st
, u
,
11703 sizeof(struct imsm_update_takeover
));
11704 /* and possibly remotely */
11705 if (st
->update_tail
)
11706 append_metadata_update(st
, u
,
11707 sizeof(struct imsm_update_takeover
));
11714 static int imsm_reshape_super(struct supertype
*st
, unsigned long long size
,
11716 int layout
, int chunksize
, int raid_disks
,
11717 int delta_disks
, char *backup
, char *dev
,
11718 int direction
, int verbose
)
11721 struct geo_params geo
;
11723 dprintf("(enter)\n");
11725 memset(&geo
, 0, sizeof(struct geo_params
));
11727 geo
.dev_name
= dev
;
11728 strcpy(geo
.devnm
, st
->devnm
);
11731 geo
.layout
= layout
;
11732 geo
.chunksize
= chunksize
;
11733 geo
.raid_disks
= raid_disks
;
11734 if (delta_disks
!= UnSet
)
11735 geo
.raid_disks
+= delta_disks
;
11737 dprintf("for level : %i\n", geo
.level
);
11738 dprintf("for raid_disks : %i\n", geo
.raid_disks
);
11740 if (strcmp(st
->container_devnm
, st
->devnm
) == 0) {
11741 /* On container level we can only increase number of devices. */
11742 dprintf("imsm: info: Container operation\n");
11743 int old_raid_disks
= 0;
11745 if (imsm_reshape_is_allowed_on_container(
11746 st
, &geo
, &old_raid_disks
, direction
)) {
11747 struct imsm_update_reshape
*u
= NULL
;
11750 len
= imsm_create_metadata_update_for_reshape(
11751 st
, &geo
, old_raid_disks
, &u
);
11754 dprintf("imsm: Cannot prepare update\n");
11755 goto exit_imsm_reshape_super
;
11759 /* update metadata locally */
11760 imsm_update_metadata_locally(st
, u
, len
);
11761 /* and possibly remotely */
11762 if (st
->update_tail
)
11763 append_metadata_update(st
, u
, len
);
11768 pr_err("(imsm) Operation is not allowed on this container\n");
11771 /* On volume level we support following operations
11772 * - takeover: raid10 -> raid0; raid0 -> raid10
11773 * - chunk size migration
11774 * - migration: raid5 -> raid0; raid0 -> raid5
11776 struct intel_super
*super
= st
->sb
;
11777 struct intel_dev
*dev
= super
->devlist
;
11779 dprintf("imsm: info: Volume operation\n");
11780 /* find requested device */
11783 imsm_find_array_devnm_by_subdev(
11784 dev
->index
, st
->container_devnm
);
11785 if (devnm
&& strcmp(devnm
, geo
.devnm
) == 0)
11790 pr_err("Cannot find %s (%s) subarray\n",
11791 geo
.dev_name
, geo
.devnm
);
11792 goto exit_imsm_reshape_super
;
11794 super
->current_vol
= dev
->index
;
11795 change
= imsm_analyze_change(st
, &geo
, direction
);
11798 ret_val
= imsm_takeover(st
, &geo
);
11800 case CH_MIGRATION
: {
11801 struct imsm_update_reshape_migration
*u
= NULL
;
11803 imsm_create_metadata_update_for_migration(
11806 dprintf("imsm: Cannot prepare update\n");
11810 /* update metadata locally */
11811 imsm_update_metadata_locally(st
, u
, len
);
11812 /* and possibly remotely */
11813 if (st
->update_tail
)
11814 append_metadata_update(st
, u
, len
);
11819 case CH_ARRAY_SIZE
: {
11820 struct imsm_update_size_change
*u
= NULL
;
11822 imsm_create_metadata_update_for_size_change(
11825 dprintf("imsm: Cannot prepare update\n");
11829 /* update metadata locally */
11830 imsm_update_metadata_locally(st
, u
, len
);
11831 /* and possibly remotely */
11832 if (st
->update_tail
)
11833 append_metadata_update(st
, u
, len
);
11843 exit_imsm_reshape_super
:
11844 dprintf("imsm: reshape_super Exit code = %i\n", ret_val
);
11848 #define COMPLETED_OK 0
11849 #define COMPLETED_NONE 1
11850 #define COMPLETED_DELAYED 2
11852 static int read_completed(int fd
, unsigned long long *val
)
11857 ret
= sysfs_fd_get_str(fd
, buf
, 50);
11861 ret
= COMPLETED_OK
;
11862 if (strncmp(buf
, "none", 4) == 0) {
11863 ret
= COMPLETED_NONE
;
11864 } else if (strncmp(buf
, "delayed", 7) == 0) {
11865 ret
= COMPLETED_DELAYED
;
11868 *val
= strtoull(buf
, &ep
, 0);
11869 if (ep
== buf
|| (*ep
!= 0 && *ep
!= '\n' && *ep
!= ' '))
11875 /*******************************************************************************
11876 * Function: wait_for_reshape_imsm
11877 * Description: Function writes new sync_max value and waits until
11878 * reshape process reach new position
11880 * sra : general array info
11881 * ndata : number of disks in new array's layout
11884 * 1 : there is no reshape in progress,
11886 ******************************************************************************/
11887 int wait_for_reshape_imsm(struct mdinfo
*sra
, int ndata
)
11889 int fd
= sysfs_get_fd(sra
, NULL
, "sync_completed");
11891 unsigned long long completed
;
11892 /* to_complete : new sync_max position */
11893 unsigned long long to_complete
= sra
->reshape_progress
;
11894 unsigned long long position_to_set
= to_complete
/ ndata
;
11897 dprintf("cannot open reshape_position\n");
11902 if (sysfs_fd_get_ll(fd
, &completed
) < 0) {
11904 dprintf("cannot read reshape_position (no reshape in progres)\n");
11913 if (completed
> position_to_set
) {
11914 dprintf("wrong next position to set %llu (%llu)\n",
11915 to_complete
, position_to_set
);
11919 dprintf("Position set: %llu\n", position_to_set
);
11920 if (sysfs_set_num(sra
, NULL
, "sync_max",
11921 position_to_set
) != 0) {
11922 dprintf("cannot set reshape position to %llu\n",
11931 int timeout
= 3000;
11933 sysfs_wait(fd
, &timeout
);
11934 if (sysfs_get_str(sra
, NULL
, "sync_action",
11936 strncmp(action
, "reshape", 7) != 0) {
11937 if (strncmp(action
, "idle", 4) == 0)
11943 rc
= read_completed(fd
, &completed
);
11945 dprintf("cannot read reshape_position (in loop)\n");
11948 } else if (rc
== COMPLETED_NONE
)
11950 } while (completed
< position_to_set
);
11956 /*******************************************************************************
11957 * Function: check_degradation_change
11958 * Description: Check that array hasn't become failed.
11960 * info : for sysfs access
11961 * sources : source disks descriptors
11962 * degraded: previous degradation level
11964 * degradation level
11965 ******************************************************************************/
11966 int check_degradation_change(struct mdinfo
*info
,
11970 unsigned long long new_degraded
;
11973 rv
= sysfs_get_ll(info
, NULL
, "degraded", &new_degraded
);
11974 if (rv
== -1 || (new_degraded
!= (unsigned long long)degraded
)) {
11975 /* check each device to ensure it is still working */
11978 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
11979 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
11981 if (sd
->disk
.state
& (1<<MD_DISK_SYNC
)) {
11984 if (sysfs_get_str(info
,
11985 sd
, "state", sbuf
, sizeof(sbuf
)) < 0 ||
11986 strstr(sbuf
, "faulty") ||
11987 strstr(sbuf
, "in_sync") == NULL
) {
11988 /* this device is dead */
11989 sd
->disk
.state
= (1<<MD_DISK_FAULTY
);
11990 if (sd
->disk
.raid_disk
>= 0 &&
11991 sources
[sd
->disk
.raid_disk
] >= 0) {
11993 sd
->disk
.raid_disk
]);
11994 sources
[sd
->disk
.raid_disk
] =
12003 return new_degraded
;
12006 /*******************************************************************************
12007 * Function: imsm_manage_reshape
12008 * Description: Function finds array under reshape and it manages reshape
12009 * process. It creates stripes backups (if required) and sets
12012 * afd : Backup handle (nattive) - not used
12013 * sra : general array info
12014 * reshape : reshape parameters - not used
12015 * st : supertype structure
12016 * blocks : size of critical section [blocks]
12017 * fds : table of source device descriptor
12018 * offsets : start of array (offest per devices)
12020 * destfd : table of destination device descriptor
12021 * destoffsets : table of destination offsets (per device)
12023 * 1 : success, reshape is done
12025 ******************************************************************************/
12026 static int imsm_manage_reshape(
12027 int afd
, struct mdinfo
*sra
, struct reshape
*reshape
,
12028 struct supertype
*st
, unsigned long backup_blocks
,
12029 int *fds
, unsigned long long *offsets
,
12030 int dests
, int *destfd
, unsigned long long *destoffsets
)
12033 struct intel_super
*super
= st
->sb
;
12034 struct intel_dev
*dv
;
12035 unsigned int sector_size
= super
->sector_size
;
12036 struct imsm_dev
*dev
= NULL
;
12037 struct imsm_map
*map_src
, *map_dest
;
12038 int migr_vol_qan
= 0;
12039 int ndata
, odata
; /* [bytes] */
12040 int chunk
; /* [bytes] */
12041 struct migr_record
*migr_rec
;
12043 unsigned int buf_size
; /* [bytes] */
12044 unsigned long long max_position
; /* array size [bytes] */
12045 unsigned long long next_step
; /* [blocks]/[bytes] */
12046 unsigned long long old_data_stripe_length
;
12047 unsigned long long start_src
; /* [bytes] */
12048 unsigned long long start
; /* [bytes] */
12049 unsigned long long start_buf_shift
; /* [bytes] */
12051 int source_layout
= 0;
12056 if (!fds
|| !offsets
)
12059 /* Find volume during the reshape */
12060 for (dv
= super
->devlist
; dv
; dv
= dv
->next
) {
12061 if (dv
->dev
->vol
.migr_type
== MIGR_GEN_MIGR
&&
12062 dv
->dev
->vol
.migr_state
== 1) {
12067 /* Only one volume can migrate at the same time */
12068 if (migr_vol_qan
!= 1) {
12069 pr_err("%s", migr_vol_qan
?
12070 "Number of migrating volumes greater than 1\n" :
12071 "There is no volume during migrationg\n");
12075 map_dest
= get_imsm_map(dev
, MAP_0
);
12076 map_src
= get_imsm_map(dev
, MAP_1
);
12077 if (map_src
== NULL
)
12080 ndata
= imsm_num_data_members(map_dest
);
12081 odata
= imsm_num_data_members(map_src
);
12083 chunk
= __le16_to_cpu(map_src
->blocks_per_strip
) * 512;
12084 old_data_stripe_length
= odata
* chunk
;
12086 migr_rec
= super
->migr_rec
;
12088 /* initialize migration record for start condition */
12089 if (sra
->reshape_progress
== 0)
12090 init_migr_record_imsm(st
, dev
, sra
);
12092 if (__le32_to_cpu(migr_rec
->rec_status
) != UNIT_SRC_NORMAL
) {
12093 dprintf("imsm: cannot restart migration when data are present in copy area.\n");
12096 /* Save checkpoint to update migration record for current
12097 * reshape position (in md). It can be farther than current
12098 * reshape position in metadata.
12100 if (save_checkpoint_imsm(st
, sra
, UNIT_SRC_NORMAL
) == 1) {
12101 /* ignore error == 2, this can mean end of reshape here
12103 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL, initial save)\n");
12108 /* size for data */
12109 buf_size
= __le32_to_cpu(migr_rec
->blocks_per_unit
) * 512;
12110 /* extend buffer size for parity disk */
12111 buf_size
+= __le32_to_cpu(migr_rec
->dest_depth_per_unit
) * 512;
12112 /* add space for stripe alignment */
12113 buf_size
+= old_data_stripe_length
;
12114 if (posix_memalign((void **)&buf
, MAX_SECTOR_SIZE
, buf_size
)) {
12115 dprintf("imsm: Cannot allocate checkpoint buffer\n");
12119 max_position
= sra
->component_size
* ndata
;
12120 source_layout
= imsm_level_to_layout(map_src
->raid_level
);
12122 while (current_migr_unit(migr_rec
) <
12123 get_num_migr_units(migr_rec
)) {
12124 /* current reshape position [blocks] */
12125 unsigned long long current_position
=
12126 __le32_to_cpu(migr_rec
->blocks_per_unit
)
12127 * current_migr_unit(migr_rec
);
12128 unsigned long long border
;
12130 /* Check that array hasn't become failed.
12132 degraded
= check_degradation_change(sra
, fds
, degraded
);
12133 if (degraded
> 1) {
12134 dprintf("imsm: Abort reshape due to degradation level (%i)\n", degraded
);
12138 next_step
= __le32_to_cpu(migr_rec
->blocks_per_unit
);
12140 if ((current_position
+ next_step
) > max_position
)
12141 next_step
= max_position
- current_position
;
12143 start
= current_position
* 512;
12145 /* align reading start to old geometry */
12146 start_buf_shift
= start
% old_data_stripe_length
;
12147 start_src
= start
- start_buf_shift
;
12149 border
= (start_src
/ odata
) - (start
/ ndata
);
12151 if (border
<= __le32_to_cpu(migr_rec
->dest_depth_per_unit
)) {
12152 /* save critical stripes to buf
12153 * start - start address of current unit
12154 * to backup [bytes]
12155 * start_src - start address of current unit
12156 * to backup alligned to source array
12159 unsigned long long next_step_filler
;
12160 unsigned long long copy_length
= next_step
* 512;
12162 /* allign copy area length to stripe in old geometry */
12163 next_step_filler
= ((copy_length
+ start_buf_shift
)
12164 % old_data_stripe_length
);
12165 if (next_step_filler
)
12166 next_step_filler
= (old_data_stripe_length
12167 - next_step_filler
);
12168 dprintf("save_stripes() parameters: start = %llu,\tstart_src = %llu,\tnext_step*512 = %llu,\tstart_in_buf_shift = %llu,\tnext_step_filler = %llu\n",
12169 start
, start_src
, copy_length
,
12170 start_buf_shift
, next_step_filler
);
12172 if (save_stripes(fds
, offsets
, map_src
->num_members
,
12173 chunk
, map_src
->raid_level
,
12174 source_layout
, 0, NULL
, start_src
,
12176 next_step_filler
+ start_buf_shift
,
12178 dprintf("imsm: Cannot save stripes to buffer\n");
12181 /* Convert data to destination format and store it
12182 * in backup general migration area
12184 if (save_backup_imsm(st
, dev
, sra
,
12185 buf
+ start_buf_shift
, copy_length
)) {
12186 dprintf("imsm: Cannot save stripes to target devices\n");
12189 if (save_checkpoint_imsm(st
, sra
,
12190 UNIT_SRC_IN_CP_AREA
)) {
12191 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_IN_CP_AREA)\n");
12195 /* set next step to use whole border area */
12196 border
/= next_step
;
12198 next_step
*= border
;
12200 /* When data backed up, checkpoint stored,
12201 * kick the kernel to reshape unit of data
12203 next_step
= next_step
+ sra
->reshape_progress
;
12204 /* limit next step to array max position */
12205 if (next_step
> max_position
)
12206 next_step
= max_position
;
12207 sysfs_set_num(sra
, NULL
, "suspend_lo", sra
->reshape_progress
);
12208 sysfs_set_num(sra
, NULL
, "suspend_hi", next_step
);
12209 sra
->reshape_progress
= next_step
;
12211 /* wait until reshape finish */
12212 if (wait_for_reshape_imsm(sra
, ndata
)) {
12213 dprintf("wait_for_reshape_imsm returned error!\n");
12219 if (save_checkpoint_imsm(st
, sra
, UNIT_SRC_NORMAL
) == 1) {
12220 /* ignore error == 2, this can mean end of reshape here
12222 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL)\n");
12228 /* clear migr_rec on disks after successful migration */
12231 memset(super
->migr_rec_buf
, 0, MIGR_REC_BUF_SECTORS
*MAX_SECTOR_SIZE
);
12232 for (d
= super
->disks
; d
; d
= d
->next
) {
12233 if (d
->index
< 0 || is_failed(&d
->disk
))
12235 unsigned long long dsize
;
12237 get_dev_size(d
->fd
, NULL
, &dsize
);
12238 if (lseek64(d
->fd
, dsize
- MIGR_REC_SECTOR_POSITION
*sector_size
,
12240 if ((unsigned int)write(d
->fd
, super
->migr_rec_buf
,
12241 MIGR_REC_BUF_SECTORS
*sector_size
) !=
12242 MIGR_REC_BUF_SECTORS
*sector_size
)
12243 perror("Write migr_rec failed");
12247 /* return '1' if done */
12251 /* See Grow.c: abort_reshape() for further explanation */
12252 sysfs_set_num(sra
, NULL
, "suspend_lo", 0x7FFFFFFFFFFFFFFFULL
);
12253 sysfs_set_num(sra
, NULL
, "suspend_hi", 0);
12254 sysfs_set_num(sra
, NULL
, "suspend_lo", 0);
12259 struct superswitch super_imsm
= {
12260 .examine_super
= examine_super_imsm
,
12261 .brief_examine_super
= brief_examine_super_imsm
,
12262 .brief_examine_subarrays
= brief_examine_subarrays_imsm
,
12263 .export_examine_super
= export_examine_super_imsm
,
12264 .detail_super
= detail_super_imsm
,
12265 .brief_detail_super
= brief_detail_super_imsm
,
12266 .write_init_super
= write_init_super_imsm
,
12267 .validate_geometry
= validate_geometry_imsm
,
12268 .add_to_super
= add_to_super_imsm
,
12269 .remove_from_super
= remove_from_super_imsm
,
12270 .detail_platform
= detail_platform_imsm
,
12271 .export_detail_platform
= export_detail_platform_imsm
,
12272 .kill_subarray
= kill_subarray_imsm
,
12273 .update_subarray
= update_subarray_imsm
,
12274 .load_container
= load_container_imsm
,
12275 .default_geometry
= default_geometry_imsm
,
12276 .get_disk_controller_domain
= imsm_get_disk_controller_domain
,
12277 .reshape_super
= imsm_reshape_super
,
12278 .manage_reshape
= imsm_manage_reshape
,
12279 .recover_backup
= recover_backup_imsm
,
12280 .copy_metadata
= copy_metadata_imsm
,
12281 .examine_badblocks
= examine_badblocks_imsm
,
12282 .match_home
= match_home_imsm
,
12283 .uuid_from_super
= uuid_from_super_imsm
,
12284 .getinfo_super
= getinfo_super_imsm
,
12285 .getinfo_super_disks
= getinfo_super_disks_imsm
,
12286 .update_super
= update_super_imsm
,
12288 .avail_size
= avail_size_imsm
,
12289 .get_spare_criteria
= get_spare_criteria_imsm
,
12291 .compare_super
= compare_super_imsm
,
12293 .load_super
= load_super_imsm
,
12294 .init_super
= init_super_imsm
,
12295 .store_super
= store_super_imsm
,
12296 .free_super
= free_super_imsm
,
12297 .match_metadata_desc
= match_metadata_desc_imsm
,
12298 .container_content
= container_content_imsm
,
12299 .validate_container
= validate_container_imsm
,
12301 .write_init_ppl
= write_init_ppl_imsm
,
12302 .validate_ppl
= validate_ppl_imsm
,
12308 .open_new
= imsm_open_new
,
12309 .set_array_state
= imsm_set_array_state
,
12310 .set_disk
= imsm_set_disk
,
12311 .sync_metadata
= imsm_sync_metadata
,
12312 .activate_spare
= imsm_activate_spare
,
12313 .process_update
= imsm_process_update
,
12314 .prepare_update
= imsm_prepare_update
,
12315 .record_bad_block
= imsm_record_badblock
,
12316 .clear_bad_block
= imsm_clear_badblock
,
12317 .get_bad_blocks
= imsm_get_badblocks
,