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 __u64 creation_time
; /* 0x50 - 0x57 Array creation time */
264 #define IMSM_FILLERS 32
265 __u32 filler
[IMSM_FILLERS
]; /* 0x58 - 0xD7 RAID_MPB_FILLERS */
266 struct imsm_disk disk
[1]; /* 0xD8 diskTbl[numDisks] */
267 /* here comes imsm_dev[num_raid_devs] */
268 /* here comes BBM logs */
270 ASSERT_SIZE(imsm_super
, 264)
272 #define BBM_LOG_MAX_ENTRIES 254
273 #define BBM_LOG_MAX_LBA_ENTRY_VAL 256 /* Represents 256 LBAs */
274 #define BBM_LOG_SIGNATURE 0xabadb10c
276 struct bbm_log_block_addr
{
279 } __attribute__ ((__packed__
));
281 struct bbm_log_entry
{
282 __u8 marked_count
; /* Number of blocks marked - 1 */
283 __u8 disk_ordinal
; /* Disk entry within the imsm_super */
284 struct bbm_log_block_addr defective_block_start
;
285 } __attribute__ ((__packed__
));
288 __u32 signature
; /* 0xABADB10C */
290 struct bbm_log_entry marked_block_entries
[BBM_LOG_MAX_ENTRIES
];
292 ASSERT_SIZE(bbm_log
, 2040)
294 static char *map_state_str
[] = { "normal", "uninitialized", "degraded", "failed" };
296 #define BLOCKS_PER_KB (1024/512)
298 #define RAID_DISK_RESERVED_BLOCKS_IMSM_HI 2209
300 #define GEN_MIGR_AREA_SIZE 2048 /* General Migration Copy Area size in blocks */
302 #define MIGR_REC_BUF_SECTORS 1 /* size of migr_record i/o buffer in sectors */
303 #define MIGR_REC_SECTOR_POSITION 1 /* migr_record position offset on disk,
304 * MIGR_REC_BUF_SECTORS <= MIGR_REC_SECTOR_POS
307 #define UNIT_SRC_NORMAL 0 /* Source data for curr_migr_unit must
308 * be recovered using srcMap */
309 #define UNIT_SRC_IN_CP_AREA 1 /* Source data for curr_migr_unit has
310 * already been migrated and must
311 * be recovered from checkpoint area */
313 #define PPL_ENTRY_SPACE (128 * 1024) /* Size of single PPL, without the header */
316 __u32 rec_status
; /* Status used to determine how to restart
317 * migration in case it aborts
319 __u32 curr_migr_unit_lo
; /* 0..numMigrUnits-1 */
320 __u32 family_num
; /* Family number of MPB
321 * containing the RaidDev
322 * that is migrating */
323 __u32 ascending_migr
; /* True if migrating in increasing
325 __u32 blocks_per_unit
; /* Num disk blocks per unit of operation */
326 __u32 dest_depth_per_unit
; /* Num member blocks each destMap
328 * advances per unit-of-operation */
329 __u32 ckpt_area_pba_lo
; /* Pba of first block of ckpt copy area */
330 __u32 dest_1st_member_lba_lo
; /* First member lba on first
331 * stripe of destination */
332 __u32 num_migr_units_lo
; /* Total num migration units-of-op */
333 __u32 post_migr_vol_cap
; /* Size of volume after
334 * migration completes */
335 __u32 post_migr_vol_cap_hi
; /* Expansion space for LBA64 */
336 __u32 ckpt_read_disk_num
; /* Which member disk in destSubMap[0] the
337 * migration ckpt record was read from
338 * (for recovered migrations) */
339 __u32 curr_migr_unit_hi
; /* 0..numMigrUnits-1 high order 32 bits */
340 __u32 ckpt_area_pba_hi
; /* Pba of first block of ckpt copy area
341 * high order 32 bits */
342 __u32 dest_1st_member_lba_hi
; /* First member lba on first stripe of
343 * destination - high order 32 bits */
344 __u32 num_migr_units_hi
; /* Total num migration units-of-op
345 * high order 32 bits */
347 ASSERT_SIZE(migr_record
, 64)
352 * 2: metadata does not match
360 struct md_list
*next
;
363 #define pr_vrb(fmt, arg...) (void) (verbose && pr_err(fmt, ##arg))
365 static __u8
migr_type(struct imsm_dev
*dev
)
367 if (dev
->vol
.migr_type
== MIGR_VERIFY
&&
368 dev
->status
& DEV_VERIFY_AND_FIX
)
371 return dev
->vol
.migr_type
;
374 static void set_migr_type(struct imsm_dev
*dev
, __u8 migr_type
)
376 /* for compatibility with older oroms convert MIGR_REPAIR, into
377 * MIGR_VERIFY w/ DEV_VERIFY_AND_FIX status
379 if (migr_type
== MIGR_REPAIR
) {
380 dev
->vol
.migr_type
= MIGR_VERIFY
;
381 dev
->status
|= DEV_VERIFY_AND_FIX
;
383 dev
->vol
.migr_type
= migr_type
;
384 dev
->status
&= ~DEV_VERIFY_AND_FIX
;
388 static unsigned int sector_count(__u32 bytes
, unsigned int sector_size
)
390 return ROUND_UP(bytes
, sector_size
) / sector_size
;
393 static unsigned int mpb_sectors(struct imsm_super
*mpb
,
394 unsigned int sector_size
)
396 return sector_count(__le32_to_cpu(mpb
->mpb_size
), sector_size
);
400 struct imsm_dev
*dev
;
401 struct intel_dev
*next
;
406 enum sys_dev_type type
;
409 struct intel_hba
*next
;
416 /* internal representation of IMSM metadata */
419 void *buf
; /* O_DIRECT buffer for reading/writing metadata */
420 struct imsm_super
*anchor
; /* immovable parameters */
423 void *migr_rec_buf
; /* buffer for I/O operations */
424 struct migr_record
*migr_rec
; /* migration record */
426 int clean_migration_record_by_mdmon
; /* when reshape is switched to next
427 array, it indicates that mdmon is allowed to clean migration
429 size_t len
; /* size of the 'buf' allocation */
430 size_t extra_space
; /* extra space in 'buf' that is not used yet */
431 void *next_buf
; /* for realloc'ing buf from the manager */
433 int updates_pending
; /* count of pending updates for mdmon */
434 int current_vol
; /* index of raid device undergoing creation */
435 unsigned long long create_offset
; /* common start for 'current_vol' */
436 __u32 random
; /* random data for seeding new family numbers */
437 struct intel_dev
*devlist
;
438 unsigned int sector_size
; /* sector size of used member drives */
442 __u8 serial
[MAX_RAID_SERIAL_LEN
];
445 struct imsm_disk disk
;
448 struct extent
*e
; /* for determining freespace @ create */
449 int raiddisk
; /* slot to fill in autolayout */
451 } *disks
, *current_disk
;
452 struct dl
*disk_mgmt_list
; /* list of disks to add/remove while mdmon
454 struct dl
*missing
; /* disks removed while we weren't looking */
455 struct bbm_log
*bbm_log
;
456 struct intel_hba
*hba
; /* device path of the raid controller for this metadata */
457 const struct imsm_orom
*orom
; /* platform firmware support */
458 struct intel_super
*next
; /* (temp) list for disambiguating family_num */
459 struct md_bb bb
; /* memory for get_bad_blocks call */
463 struct imsm_disk disk
;
464 #define IMSM_UNKNOWN_OWNER (-1)
466 struct intel_disk
*next
;
470 unsigned long long start
, size
;
473 /* definitions of reshape process types */
474 enum imsm_reshape_type
{
480 /* definition of messages passed to imsm_process_update */
481 enum imsm_update_type
{
482 update_activate_spare
,
486 update_add_remove_disk
,
487 update_reshape_container_disks
,
488 update_reshape_migration
,
490 update_general_migration_checkpoint
,
492 update_prealloc_badblocks_mem
,
496 struct imsm_update_activate_spare
{
497 enum imsm_update_type type
;
501 struct imsm_update_activate_spare
*next
;
507 unsigned long long size
;
514 enum takeover_direction
{
518 struct imsm_update_takeover
{
519 enum imsm_update_type type
;
521 enum takeover_direction direction
;
524 struct imsm_update_reshape
{
525 enum imsm_update_type type
;
529 int new_disks
[1]; /* new_raid_disks - old_raid_disks makedev number */
532 struct imsm_update_reshape_migration
{
533 enum imsm_update_type type
;
536 /* fields for array migration changes
543 int new_disks
[1]; /* new_raid_disks - old_raid_disks makedev number */
546 struct imsm_update_size_change
{
547 enum imsm_update_type type
;
552 struct imsm_update_general_migration_checkpoint
{
553 enum imsm_update_type type
;
554 __u32 curr_migr_unit
;
558 __u8 serial
[MAX_RAID_SERIAL_LEN
];
561 struct imsm_update_create_array
{
562 enum imsm_update_type type
;
567 struct imsm_update_kill_array
{
568 enum imsm_update_type type
;
572 struct imsm_update_rename_array
{
573 enum imsm_update_type type
;
574 __u8 name
[MAX_RAID_SERIAL_LEN
];
578 struct imsm_update_add_remove_disk
{
579 enum imsm_update_type type
;
582 struct imsm_update_prealloc_bb_mem
{
583 enum imsm_update_type type
;
586 struct imsm_update_rwh_policy
{
587 enum imsm_update_type type
;
592 static const char *_sys_dev_type
[] = {
593 [SYS_DEV_UNKNOWN
] = "Unknown",
594 [SYS_DEV_SAS
] = "SAS",
595 [SYS_DEV_SATA
] = "SATA",
596 [SYS_DEV_NVME
] = "NVMe",
597 [SYS_DEV_VMD
] = "VMD"
600 const char *get_sys_dev_type(enum sys_dev_type type
)
602 if (type
>= SYS_DEV_MAX
)
603 type
= SYS_DEV_UNKNOWN
;
605 return _sys_dev_type
[type
];
608 static struct intel_hba
* alloc_intel_hba(struct sys_dev
*device
)
610 struct intel_hba
*result
= xmalloc(sizeof(*result
));
612 result
->type
= device
->type
;
613 result
->path
= xstrdup(device
->path
);
615 if (result
->path
&& (result
->pci_id
= strrchr(result
->path
, '/')) != NULL
)
621 static struct intel_hba
* find_intel_hba(struct intel_hba
*hba
, struct sys_dev
*device
)
623 struct intel_hba
*result
;
625 for (result
= hba
; result
; result
= result
->next
) {
626 if (result
->type
== device
->type
&& strcmp(result
->path
, device
->path
) == 0)
632 static int attach_hba_to_super(struct intel_super
*super
, struct sys_dev
*device
)
634 struct intel_hba
*hba
;
636 /* check if disk attached to Intel HBA */
637 hba
= find_intel_hba(super
->hba
, device
);
640 /* Check if HBA is already attached to super */
641 if (super
->hba
== NULL
) {
642 super
->hba
= alloc_intel_hba(device
);
647 /* Intel metadata allows for all disks attached to the same type HBA.
648 * Do not support HBA types mixing
650 if (device
->type
!= hba
->type
)
653 /* Multiple same type HBAs can be used if they share the same OROM */
654 const struct imsm_orom
*device_orom
= get_orom_by_device_id(device
->dev_id
);
656 if (device_orom
!= super
->orom
)
662 hba
->next
= alloc_intel_hba(device
);
666 static struct sys_dev
* find_disk_attached_hba(int fd
, const char *devname
)
668 struct sys_dev
*list
, *elem
;
671 if ((list
= find_intel_devices()) == NULL
)
675 disk_path
= (char *) devname
;
677 disk_path
= diskfd_to_devpath(fd
);
682 for (elem
= list
; elem
; elem
= elem
->next
)
683 if (path_attached_to_hba(disk_path
, elem
->path
))
686 if (disk_path
!= devname
)
692 static int find_intel_hba_capability(int fd
, struct intel_super
*super
,
695 static struct supertype
*match_metadata_desc_imsm(char *arg
)
697 struct supertype
*st
;
699 if (strcmp(arg
, "imsm") != 0 &&
700 strcmp(arg
, "default") != 0
704 st
= xcalloc(1, sizeof(*st
));
705 st
->ss
= &super_imsm
;
706 st
->max_devs
= IMSM_MAX_DEVICES
;
707 st
->minor_version
= 0;
712 static __u8
*get_imsm_version(struct imsm_super
*mpb
)
714 return &mpb
->sig
[MPB_SIG_LEN
];
717 /* retrieve a disk directly from the anchor when the anchor is known to be
718 * up-to-date, currently only at load time
720 static struct imsm_disk
*__get_imsm_disk(struct imsm_super
*mpb
, __u8 index
)
722 if (index
>= mpb
->num_disks
)
724 return &mpb
->disk
[index
];
727 /* retrieve the disk description based on a index of the disk
730 static struct dl
*get_imsm_dl_disk(struct intel_super
*super
, __u8 index
)
734 for (d
= super
->disks
; d
; d
= d
->next
)
735 if (d
->index
== index
)
740 /* retrieve a disk from the parsed metadata */
741 static struct imsm_disk
*get_imsm_disk(struct intel_super
*super
, __u8 index
)
745 dl
= get_imsm_dl_disk(super
, index
);
752 /* generate a checksum directly from the anchor when the anchor is known to be
753 * up-to-date, currently only at load or write_super after coalescing
755 static __u32
__gen_imsm_checksum(struct imsm_super
*mpb
)
757 __u32 end
= mpb
->mpb_size
/ sizeof(end
);
758 __u32
*p
= (__u32
*) mpb
;
762 sum
+= __le32_to_cpu(*p
);
766 return sum
- __le32_to_cpu(mpb
->check_sum
);
769 static size_t sizeof_imsm_map(struct imsm_map
*map
)
771 return sizeof(struct imsm_map
) + sizeof(__u32
) * (map
->num_members
- 1);
774 struct imsm_map
*get_imsm_map(struct imsm_dev
*dev
, int second_map
)
776 /* A device can have 2 maps if it is in the middle of a migration.
778 * MAP_0 - we return the first map
779 * MAP_1 - we return the second map if it exists, else NULL
780 * MAP_X - we return the second map if it exists, else the first
782 struct imsm_map
*map
= &dev
->vol
.map
[0];
783 struct imsm_map
*map2
= NULL
;
785 if (dev
->vol
.migr_state
)
786 map2
= (void *)map
+ sizeof_imsm_map(map
);
788 switch (second_map
) {
805 /* return the size of the device.
806 * migr_state increases the returned size if map[0] were to be duplicated
808 static size_t sizeof_imsm_dev(struct imsm_dev
*dev
, int migr_state
)
810 size_t size
= sizeof(*dev
) - sizeof(struct imsm_map
) +
811 sizeof_imsm_map(get_imsm_map(dev
, MAP_0
));
813 /* migrating means an additional map */
814 if (dev
->vol
.migr_state
)
815 size
+= sizeof_imsm_map(get_imsm_map(dev
, MAP_1
));
817 size
+= sizeof_imsm_map(get_imsm_map(dev
, MAP_0
));
822 /* retrieve disk serial number list from a metadata update */
823 static struct disk_info
*get_disk_info(struct imsm_update_create_array
*update
)
826 struct disk_info
*inf
;
828 inf
= u
+ sizeof(*update
) - sizeof(struct imsm_dev
) +
829 sizeof_imsm_dev(&update
->dev
, 0);
834 static struct imsm_dev
*__get_imsm_dev(struct imsm_super
*mpb
, __u8 index
)
840 if (index
>= mpb
->num_raid_devs
)
843 /* devices start after all disks */
844 offset
= ((void *) &mpb
->disk
[mpb
->num_disks
]) - _mpb
;
846 for (i
= 0; i
<= index
; i
++)
848 return _mpb
+ offset
;
850 offset
+= sizeof_imsm_dev(_mpb
+ offset
, 0);
855 static struct imsm_dev
*get_imsm_dev(struct intel_super
*super
, __u8 index
)
857 struct intel_dev
*dv
;
859 if (index
>= super
->anchor
->num_raid_devs
)
861 for (dv
= super
->devlist
; dv
; dv
= dv
->next
)
862 if (dv
->index
== index
)
867 static inline unsigned long long __le48_to_cpu(const struct bbm_log_block_addr
870 return ((((__u64
)__le32_to_cpu(addr
->dw1
)) << 16) |
871 __le16_to_cpu(addr
->w1
));
874 static inline struct bbm_log_block_addr
__cpu_to_le48(unsigned long long sec
)
876 struct bbm_log_block_addr addr
;
878 addr
.w1
= __cpu_to_le16((__u16
)(sec
& 0xffff));
879 addr
.dw1
= __cpu_to_le32((__u32
)(sec
>> 16) & 0xffffffff);
883 /* get size of the bbm log */
884 static __u32
get_imsm_bbm_log_size(struct bbm_log
*log
)
886 if (!log
|| log
->entry_count
== 0)
889 return sizeof(log
->signature
) +
890 sizeof(log
->entry_count
) +
891 log
->entry_count
* sizeof(struct bbm_log_entry
);
894 /* check if bad block is not partially stored in bbm log */
895 static int is_stored_in_bbm(struct bbm_log
*log
, const __u8 idx
, const unsigned
896 long long sector
, const int length
, __u32
*pos
)
900 for (i
= *pos
; i
< log
->entry_count
; i
++) {
901 struct bbm_log_entry
*entry
= &log
->marked_block_entries
[i
];
902 unsigned long long bb_start
;
903 unsigned long long bb_end
;
905 bb_start
= __le48_to_cpu(&entry
->defective_block_start
);
906 bb_end
= bb_start
+ (entry
->marked_count
+ 1);
908 if ((entry
->disk_ordinal
== idx
) && (bb_start
>= sector
) &&
909 (bb_end
<= sector
+ length
)) {
917 /* record new bad block in bbm log */
918 static int record_new_badblock(struct bbm_log
*log
, const __u8 idx
, unsigned
919 long long sector
, int length
)
923 struct bbm_log_entry
*entry
= NULL
;
925 while (is_stored_in_bbm(log
, idx
, sector
, length
, &pos
)) {
926 struct bbm_log_entry
*e
= &log
->marked_block_entries
[pos
];
928 if ((e
->marked_count
+ 1 == BBM_LOG_MAX_LBA_ENTRY_VAL
) &&
929 (__le48_to_cpu(&e
->defective_block_start
) == sector
)) {
930 sector
+= BBM_LOG_MAX_LBA_ENTRY_VAL
;
931 length
-= BBM_LOG_MAX_LBA_ENTRY_VAL
;
940 int cnt
= (length
<= BBM_LOG_MAX_LBA_ENTRY_VAL
) ? length
:
941 BBM_LOG_MAX_LBA_ENTRY_VAL
;
942 entry
->defective_block_start
= __cpu_to_le48(sector
);
943 entry
->marked_count
= cnt
- 1;
950 new_bb
= ROUND_UP(length
, BBM_LOG_MAX_LBA_ENTRY_VAL
) /
951 BBM_LOG_MAX_LBA_ENTRY_VAL
;
952 if (log
->entry_count
+ new_bb
> BBM_LOG_MAX_ENTRIES
)
956 int cnt
= (length
<= BBM_LOG_MAX_LBA_ENTRY_VAL
) ? length
:
957 BBM_LOG_MAX_LBA_ENTRY_VAL
;
958 struct bbm_log_entry
*entry
=
959 &log
->marked_block_entries
[log
->entry_count
];
961 entry
->defective_block_start
= __cpu_to_le48(sector
);
962 entry
->marked_count
= cnt
- 1;
963 entry
->disk_ordinal
= idx
;
974 /* clear all bad blocks for given disk */
975 static void clear_disk_badblocks(struct bbm_log
*log
, const __u8 idx
)
979 while (i
< log
->entry_count
) {
980 struct bbm_log_entry
*entries
= log
->marked_block_entries
;
982 if (entries
[i
].disk_ordinal
== idx
) {
983 if (i
< log
->entry_count
- 1)
984 entries
[i
] = entries
[log
->entry_count
- 1];
992 /* clear given bad block */
993 static int clear_badblock(struct bbm_log
*log
, const __u8 idx
, const unsigned
994 long long sector
, const int length
) {
997 while (i
< log
->entry_count
) {
998 struct bbm_log_entry
*entries
= log
->marked_block_entries
;
1000 if ((entries
[i
].disk_ordinal
== idx
) &&
1001 (__le48_to_cpu(&entries
[i
].defective_block_start
) ==
1002 sector
) && (entries
[i
].marked_count
+ 1 == length
)) {
1003 if (i
< log
->entry_count
- 1)
1004 entries
[i
] = entries
[log
->entry_count
- 1];
1014 /* allocate and load BBM log from metadata */
1015 static int load_bbm_log(struct intel_super
*super
)
1017 struct imsm_super
*mpb
= super
->anchor
;
1018 __u32 bbm_log_size
= __le32_to_cpu(mpb
->bbm_log_size
);
1020 super
->bbm_log
= xcalloc(1, sizeof(struct bbm_log
));
1021 if (!super
->bbm_log
)
1025 struct bbm_log
*log
= (void *)mpb
+
1026 __le32_to_cpu(mpb
->mpb_size
) - bbm_log_size
;
1030 if (bbm_log_size
< sizeof(log
->signature
) +
1031 sizeof(log
->entry_count
))
1034 entry_count
= __le32_to_cpu(log
->entry_count
);
1035 if ((__le32_to_cpu(log
->signature
) != BBM_LOG_SIGNATURE
) ||
1036 (entry_count
> BBM_LOG_MAX_ENTRIES
))
1040 sizeof(log
->signature
) + sizeof(log
->entry_count
) +
1041 entry_count
* sizeof(struct bbm_log_entry
))
1044 memcpy(super
->bbm_log
, log
, bbm_log_size
);
1046 super
->bbm_log
->signature
= __cpu_to_le32(BBM_LOG_SIGNATURE
);
1047 super
->bbm_log
->entry_count
= 0;
1053 /* checks if bad block is within volume boundaries */
1054 static int is_bad_block_in_volume(const struct bbm_log_entry
*entry
,
1055 const unsigned long long start_sector
,
1056 const unsigned long long size
)
1058 unsigned long long bb_start
;
1059 unsigned long long bb_end
;
1061 bb_start
= __le48_to_cpu(&entry
->defective_block_start
);
1062 bb_end
= bb_start
+ (entry
->marked_count
+ 1);
1064 if (((bb_start
>= start_sector
) && (bb_start
< start_sector
+ size
)) ||
1065 ((bb_end
>= start_sector
) && (bb_end
<= start_sector
+ size
)))
1071 /* get list of bad blocks on a drive for a volume */
1072 static void get_volume_badblocks(const struct bbm_log
*log
, const __u8 idx
,
1073 const unsigned long long start_sector
,
1074 const unsigned long long size
,
1080 for (i
= 0; i
< log
->entry_count
; i
++) {
1081 const struct bbm_log_entry
*ent
=
1082 &log
->marked_block_entries
[i
];
1083 struct md_bb_entry
*bb
;
1085 if ((ent
->disk_ordinal
== idx
) &&
1086 is_bad_block_in_volume(ent
, start_sector
, size
)) {
1088 if (!bbs
->entries
) {
1089 bbs
->entries
= xmalloc(BBM_LOG_MAX_ENTRIES
*
1095 bb
= &bbs
->entries
[count
++];
1096 bb
->sector
= __le48_to_cpu(&ent
->defective_block_start
);
1097 bb
->length
= ent
->marked_count
+ 1;
1105 * == MAP_0 get first map
1106 * == MAP_1 get second map
1107 * == MAP_X than get map according to the current migr_state
1109 static __u32
get_imsm_ord_tbl_ent(struct imsm_dev
*dev
,
1113 struct imsm_map
*map
;
1115 map
= get_imsm_map(dev
, second_map
);
1117 /* top byte identifies disk under rebuild */
1118 return __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
1121 #define ord_to_idx(ord) (((ord) << 8) >> 8)
1122 static __u32
get_imsm_disk_idx(struct imsm_dev
*dev
, int slot
, int second_map
)
1124 __u32 ord
= get_imsm_ord_tbl_ent(dev
, slot
, second_map
);
1126 return ord_to_idx(ord
);
1129 static void set_imsm_ord_tbl_ent(struct imsm_map
*map
, int slot
, __u32 ord
)
1131 map
->disk_ord_tbl
[slot
] = __cpu_to_le32(ord
);
1134 static int get_imsm_disk_slot(struct imsm_map
*map
, unsigned idx
)
1139 for (slot
= 0; slot
< map
->num_members
; slot
++) {
1140 ord
= __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
1141 if (ord_to_idx(ord
) == idx
)
1148 static int get_imsm_raid_level(struct imsm_map
*map
)
1150 if (map
->raid_level
== 1) {
1151 if (map
->num_members
== 2)
1157 return map
->raid_level
;
1160 static int cmp_extent(const void *av
, const void *bv
)
1162 const struct extent
*a
= av
;
1163 const struct extent
*b
= bv
;
1164 if (a
->start
< b
->start
)
1166 if (a
->start
> b
->start
)
1171 static int count_memberships(struct dl
*dl
, struct intel_super
*super
)
1173 int memberships
= 0;
1176 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
1177 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
1178 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
1180 if (get_imsm_disk_slot(map
, dl
->index
) >= 0)
1187 static __u32
imsm_min_reserved_sectors(struct intel_super
*super
);
1189 static int split_ull(unsigned long long n
, void *lo
, void *hi
)
1191 if (lo
== 0 || hi
== 0)
1193 __put_unaligned32(__cpu_to_le32((__u32
)n
), lo
);
1194 __put_unaligned32(__cpu_to_le32((n
>> 32)), hi
);
1198 static unsigned long long join_u32(__u32 lo
, __u32 hi
)
1200 return (unsigned long long)__le32_to_cpu(lo
) |
1201 (((unsigned long long)__le32_to_cpu(hi
)) << 32);
1204 static unsigned long long total_blocks(struct imsm_disk
*disk
)
1208 return join_u32(disk
->total_blocks_lo
, disk
->total_blocks_hi
);
1211 static unsigned long long pba_of_lba0(struct imsm_map
*map
)
1215 return join_u32(map
->pba_of_lba0_lo
, map
->pba_of_lba0_hi
);
1218 static unsigned long long blocks_per_member(struct imsm_map
*map
)
1222 return join_u32(map
->blocks_per_member_lo
, map
->blocks_per_member_hi
);
1225 static unsigned long long num_data_stripes(struct imsm_map
*map
)
1229 return join_u32(map
->num_data_stripes_lo
, map
->num_data_stripes_hi
);
1232 static unsigned long long imsm_dev_size(struct imsm_dev
*dev
)
1236 return join_u32(dev
->size_low
, dev
->size_high
);
1239 static unsigned long long migr_chkp_area_pba(struct migr_record
*migr_rec
)
1241 if (migr_rec
== NULL
)
1243 return join_u32(migr_rec
->ckpt_area_pba_lo
,
1244 migr_rec
->ckpt_area_pba_hi
);
1247 static unsigned long long current_migr_unit(struct migr_record
*migr_rec
)
1249 if (migr_rec
== NULL
)
1251 return join_u32(migr_rec
->curr_migr_unit_lo
,
1252 migr_rec
->curr_migr_unit_hi
);
1255 static unsigned long long migr_dest_1st_member_lba(struct migr_record
*migr_rec
)
1257 if (migr_rec
== NULL
)
1259 return join_u32(migr_rec
->dest_1st_member_lba_lo
,
1260 migr_rec
->dest_1st_member_lba_hi
);
1263 static unsigned long long get_num_migr_units(struct migr_record
*migr_rec
)
1265 if (migr_rec
== NULL
)
1267 return join_u32(migr_rec
->num_migr_units_lo
,
1268 migr_rec
->num_migr_units_hi
);
1271 static void set_total_blocks(struct imsm_disk
*disk
, unsigned long long n
)
1273 split_ull(n
, &disk
->total_blocks_lo
, &disk
->total_blocks_hi
);
1276 static void set_pba_of_lba0(struct imsm_map
*map
, unsigned long long n
)
1278 split_ull(n
, &map
->pba_of_lba0_lo
, &map
->pba_of_lba0_hi
);
1281 static void set_blocks_per_member(struct imsm_map
*map
, unsigned long long n
)
1283 split_ull(n
, &map
->blocks_per_member_lo
, &map
->blocks_per_member_hi
);
1286 static void set_num_data_stripes(struct imsm_map
*map
, unsigned long long n
)
1288 split_ull(n
, &map
->num_data_stripes_lo
, &map
->num_data_stripes_hi
);
1291 static void set_imsm_dev_size(struct imsm_dev
*dev
, unsigned long long n
)
1293 split_ull(n
, &dev
->size_low
, &dev
->size_high
);
1296 static void set_migr_chkp_area_pba(struct migr_record
*migr_rec
,
1297 unsigned long long n
)
1299 split_ull(n
, &migr_rec
->ckpt_area_pba_lo
, &migr_rec
->ckpt_area_pba_hi
);
1302 static void set_current_migr_unit(struct migr_record
*migr_rec
,
1303 unsigned long long n
)
1305 split_ull(n
, &migr_rec
->curr_migr_unit_lo
,
1306 &migr_rec
->curr_migr_unit_hi
);
1309 static void set_migr_dest_1st_member_lba(struct migr_record
*migr_rec
,
1310 unsigned long long n
)
1312 split_ull(n
, &migr_rec
->dest_1st_member_lba_lo
,
1313 &migr_rec
->dest_1st_member_lba_hi
);
1316 static void set_num_migr_units(struct migr_record
*migr_rec
,
1317 unsigned long long n
)
1319 split_ull(n
, &migr_rec
->num_migr_units_lo
,
1320 &migr_rec
->num_migr_units_hi
);
1323 static unsigned long long per_dev_array_size(struct imsm_map
*map
)
1325 unsigned long long array_size
= 0;
1330 array_size
= num_data_stripes(map
) * map
->blocks_per_strip
;
1331 if (get_imsm_raid_level(map
) == 1 || get_imsm_raid_level(map
) == 10)
1337 static struct extent
*get_extents(struct intel_super
*super
, struct dl
*dl
,
1338 int get_minimal_reservation
)
1340 /* find a list of used extents on the given physical device */
1341 struct extent
*rv
, *e
;
1343 int memberships
= count_memberships(dl
, super
);
1346 /* trim the reserved area for spares, so they can join any array
1347 * regardless of whether the OROM has assigned sectors from the
1348 * IMSM_RESERVED_SECTORS region
1350 if (dl
->index
== -1 || get_minimal_reservation
)
1351 reservation
= imsm_min_reserved_sectors(super
);
1353 reservation
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
1355 rv
= xcalloc(sizeof(struct extent
), (memberships
+ 1));
1358 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
1359 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
1360 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
1362 if (get_imsm_disk_slot(map
, dl
->index
) >= 0) {
1363 e
->start
= pba_of_lba0(map
);
1364 e
->size
= per_dev_array_size(map
);
1368 qsort(rv
, memberships
, sizeof(*rv
), cmp_extent
);
1370 /* determine the start of the metadata
1371 * when no raid devices are defined use the default
1372 * ...otherwise allow the metadata to truncate the value
1373 * as is the case with older versions of imsm
1376 struct extent
*last
= &rv
[memberships
- 1];
1377 unsigned long long remainder
;
1379 remainder
= total_blocks(&dl
->disk
) - (last
->start
+ last
->size
);
1380 /* round down to 1k block to satisfy precision of the kernel
1384 /* make sure remainder is still sane */
1385 if (remainder
< (unsigned)ROUND_UP(super
->len
, 512) >> 9)
1386 remainder
= ROUND_UP(super
->len
, 512) >> 9;
1387 if (reservation
> remainder
)
1388 reservation
= remainder
;
1390 e
->start
= total_blocks(&dl
->disk
) - reservation
;
1395 /* try to determine how much space is reserved for metadata from
1396 * the last get_extents() entry, otherwise fallback to the
1399 static __u32
imsm_reserved_sectors(struct intel_super
*super
, struct dl
*dl
)
1405 /* for spares just return a minimal reservation which will grow
1406 * once the spare is picked up by an array
1408 if (dl
->index
== -1)
1409 return MPB_SECTOR_CNT
;
1411 e
= get_extents(super
, dl
, 0);
1413 return MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
1415 /* scroll to last entry */
1416 for (i
= 0; e
[i
].size
; i
++)
1419 rv
= total_blocks(&dl
->disk
) - e
[i
].start
;
1426 static int is_spare(struct imsm_disk
*disk
)
1428 return (disk
->status
& SPARE_DISK
) == SPARE_DISK
;
1431 static int is_configured(struct imsm_disk
*disk
)
1433 return (disk
->status
& CONFIGURED_DISK
) == CONFIGURED_DISK
;
1436 static int is_failed(struct imsm_disk
*disk
)
1438 return (disk
->status
& FAILED_DISK
) == FAILED_DISK
;
1441 static int is_journal(struct imsm_disk
*disk
)
1443 return (disk
->status
& JOURNAL_DISK
) == JOURNAL_DISK
;
1446 /* round array size down to closest MB and ensure it splits evenly
1449 static unsigned long long round_size_to_mb(unsigned long long size
, unsigned int
1453 size
= (size
>> SECT_PER_MB_SHIFT
) << SECT_PER_MB_SHIFT
;
1459 static int able_to_resync(int raid_level
, int missing_disks
)
1461 int max_missing_disks
= 0;
1463 switch (raid_level
) {
1465 max_missing_disks
= 1;
1468 max_missing_disks
= 0;
1470 return missing_disks
<= max_missing_disks
;
1473 /* try to determine how much space is reserved for metadata from
1474 * the last get_extents() entry on the smallest active disk,
1475 * otherwise fallback to the default
1477 static __u32
imsm_min_reserved_sectors(struct intel_super
*super
)
1481 unsigned long long min_active
;
1483 __u32 rv
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
1484 struct dl
*dl
, *dl_min
= NULL
;
1490 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
1493 unsigned long long blocks
= total_blocks(&dl
->disk
);
1494 if (blocks
< min_active
|| min_active
== 0) {
1496 min_active
= blocks
;
1502 /* find last lba used by subarrays on the smallest active disk */
1503 e
= get_extents(super
, dl_min
, 0);
1506 for (i
= 0; e
[i
].size
; i
++)
1509 remainder
= min_active
- e
[i
].start
;
1512 /* to give priority to recovery we should not require full
1513 IMSM_RESERVED_SECTORS from the spare */
1514 rv
= MPB_SECTOR_CNT
+ NUM_BLOCKS_DIRTY_STRIPE_REGION
;
1516 /* if real reservation is smaller use that value */
1517 return (remainder
< rv
) ? remainder
: rv
;
1521 * Return minimum size of a spare and sector size
1522 * that can be used in this array
1524 int get_spare_criteria_imsm(struct supertype
*st
, struct spare_criteria
*c
)
1526 struct intel_super
*super
= st
->sb
;
1530 unsigned long long size
= 0;
1537 /* find first active disk in array */
1539 while (dl
&& (is_failed(&dl
->disk
) || dl
->index
== -1))
1543 /* find last lba used by subarrays */
1544 e
= get_extents(super
, dl
, 0);
1547 for (i
= 0; e
[i
].size
; i
++)
1550 size
= e
[i
-1].start
+ e
[i
-1].size
;
1553 /* add the amount of space needed for metadata */
1554 size
+= imsm_min_reserved_sectors(super
);
1556 c
->min_size
= size
* 512;
1557 c
->sector_size
= super
->sector_size
;
1562 static int is_gen_migration(struct imsm_dev
*dev
);
1564 #define IMSM_4K_DIV 8
1566 static __u64
blocks_per_migr_unit(struct intel_super
*super
,
1567 struct imsm_dev
*dev
);
1569 static void print_imsm_dev(struct intel_super
*super
,
1570 struct imsm_dev
*dev
,
1576 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
1577 struct imsm_map
*map2
= get_imsm_map(dev
, MAP_1
);
1581 printf("[%.16s]:\n", dev
->volume
);
1582 printf(" Subarray : %d\n", super
->current_vol
);
1583 printf(" UUID : %s\n", uuid
);
1584 printf(" RAID Level : %d", get_imsm_raid_level(map
));
1586 printf(" <-- %d", get_imsm_raid_level(map2
));
1588 printf(" Members : %d", map
->num_members
);
1590 printf(" <-- %d", map2
->num_members
);
1592 printf(" Slots : [");
1593 for (i
= 0; i
< map
->num_members
; i
++) {
1594 ord
= get_imsm_ord_tbl_ent(dev
, i
, MAP_0
);
1595 printf("%s", ord
& IMSM_ORD_REBUILD
? "_" : "U");
1600 for (i
= 0; i
< map2
->num_members
; i
++) {
1601 ord
= get_imsm_ord_tbl_ent(dev
, i
, MAP_1
);
1602 printf("%s", ord
& IMSM_ORD_REBUILD
? "_" : "U");
1607 printf(" Failed disk : ");
1608 if (map
->failed_disk_num
== 0xff)
1611 printf("%i", map
->failed_disk_num
);
1613 slot
= get_imsm_disk_slot(map
, disk_idx
);
1615 ord
= get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
);
1616 printf(" This Slot : %d%s\n", slot
,
1617 ord
& IMSM_ORD_REBUILD
? " (out-of-sync)" : "");
1619 printf(" This Slot : ?\n");
1620 printf(" Sector Size : %u\n", super
->sector_size
);
1621 sz
= imsm_dev_size(dev
);
1622 printf(" Array Size : %llu%s\n",
1623 (unsigned long long)sz
* 512 / super
->sector_size
,
1624 human_size(sz
* 512));
1625 sz
= blocks_per_member(map
);
1626 printf(" Per Dev Size : %llu%s\n",
1627 (unsigned long long)sz
* 512 / super
->sector_size
,
1628 human_size(sz
* 512));
1629 printf(" Sector Offset : %llu\n",
1631 printf(" Num Stripes : %llu\n",
1632 num_data_stripes(map
));
1633 printf(" Chunk Size : %u KiB",
1634 __le16_to_cpu(map
->blocks_per_strip
) / 2);
1636 printf(" <-- %u KiB",
1637 __le16_to_cpu(map2
->blocks_per_strip
) / 2);
1639 printf(" Reserved : %d\n", __le32_to_cpu(dev
->reserved_blocks
));
1640 printf(" Migrate State : ");
1641 if (dev
->vol
.migr_state
) {
1642 if (migr_type(dev
) == MIGR_INIT
)
1643 printf("initialize\n");
1644 else if (migr_type(dev
) == MIGR_REBUILD
)
1645 printf("rebuild\n");
1646 else if (migr_type(dev
) == MIGR_VERIFY
)
1648 else if (migr_type(dev
) == MIGR_GEN_MIGR
)
1649 printf("general migration\n");
1650 else if (migr_type(dev
) == MIGR_STATE_CHANGE
)
1651 printf("state change\n");
1652 else if (migr_type(dev
) == MIGR_REPAIR
)
1655 printf("<unknown:%d>\n", migr_type(dev
));
1658 printf(" Map State : %s", map_state_str
[map
->map_state
]);
1659 if (dev
->vol
.migr_state
) {
1660 struct imsm_map
*map
= get_imsm_map(dev
, MAP_1
);
1662 printf(" <-- %s", map_state_str
[map
->map_state
]);
1663 printf("\n Checkpoint : %u ",
1664 __le32_to_cpu(dev
->vol
.curr_migr_unit
));
1665 if (is_gen_migration(dev
) && (slot
> 1 || slot
< 0))
1668 printf("(%llu)", (unsigned long long)
1669 blocks_per_migr_unit(super
, dev
));
1672 printf(" Dirty State : %s\n", (dev
->vol
.dirty
& RAIDVOL_DIRTY
) ?
1674 printf(" RWH Policy : ");
1675 if (dev
->rwh_policy
== RWH_OFF
|| dev
->rwh_policy
== RWH_MULTIPLE_OFF
)
1677 else if (dev
->rwh_policy
== RWH_DISTRIBUTED
)
1678 printf("PPL distributed\n");
1679 else if (dev
->rwh_policy
== RWH_JOURNALING_DRIVE
)
1680 printf("PPL journaling drive\n");
1681 else if (dev
->rwh_policy
== RWH_MULTIPLE_DISTRIBUTED
)
1682 printf("Multiple distributed PPLs\n");
1683 else if (dev
->rwh_policy
== RWH_MULTIPLE_PPLS_JOURNALING_DRIVE
)
1684 printf("Multiple PPLs on journaling drive\n");
1686 printf("<unknown:%d>\n", dev
->rwh_policy
);
1688 printf(" Volume ID : %u\n", dev
->my_vol_raid_dev_num
);
1691 static void print_imsm_disk(struct imsm_disk
*disk
,
1694 unsigned int sector_size
) {
1695 char str
[MAX_RAID_SERIAL_LEN
+ 1];
1698 if (index
< -1 || !disk
)
1702 snprintf(str
, MAX_RAID_SERIAL_LEN
+ 1, "%s", disk
->serial
);
1704 printf(" Disk%02d Serial : %s\n", index
, str
);
1706 printf(" Disk Serial : %s\n", str
);
1707 printf(" State :%s%s%s%s\n", is_spare(disk
) ? " spare" : "",
1708 is_configured(disk
) ? " active" : "",
1709 is_failed(disk
) ? " failed" : "",
1710 is_journal(disk
) ? " journal" : "");
1711 printf(" Id : %08x\n", __le32_to_cpu(disk
->scsi_id
));
1712 sz
= total_blocks(disk
) - reserved
;
1713 printf(" Usable Size : %llu%s\n",
1714 (unsigned long long)sz
* 512 / sector_size
,
1715 human_size(sz
* 512));
1718 void convert_to_4k_imsm_migr_rec(struct intel_super
*super
)
1720 struct migr_record
*migr_rec
= super
->migr_rec
;
1722 migr_rec
->blocks_per_unit
/= IMSM_4K_DIV
;
1723 migr_rec
->dest_depth_per_unit
/= IMSM_4K_DIV
;
1724 split_ull((join_u32(migr_rec
->post_migr_vol_cap
,
1725 migr_rec
->post_migr_vol_cap_hi
) / IMSM_4K_DIV
),
1726 &migr_rec
->post_migr_vol_cap
, &migr_rec
->post_migr_vol_cap_hi
);
1727 set_migr_chkp_area_pba(migr_rec
,
1728 migr_chkp_area_pba(migr_rec
) / IMSM_4K_DIV
);
1729 set_migr_dest_1st_member_lba(migr_rec
,
1730 migr_dest_1st_member_lba(migr_rec
) / IMSM_4K_DIV
);
1733 void convert_to_4k_imsm_disk(struct imsm_disk
*disk
)
1735 set_total_blocks(disk
, (total_blocks(disk
)/IMSM_4K_DIV
));
1738 void convert_to_4k(struct intel_super
*super
)
1740 struct imsm_super
*mpb
= super
->anchor
;
1741 struct imsm_disk
*disk
;
1743 __u32 bbm_log_size
= __le32_to_cpu(mpb
->bbm_log_size
);
1745 for (i
= 0; i
< mpb
->num_disks
; i
++) {
1746 disk
= __get_imsm_disk(mpb
, i
);
1748 convert_to_4k_imsm_disk(disk
);
1750 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
1751 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
1752 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
1754 set_imsm_dev_size(dev
, imsm_dev_size(dev
)/IMSM_4K_DIV
);
1755 dev
->vol
.curr_migr_unit
/= IMSM_4K_DIV
;
1758 set_blocks_per_member(map
, blocks_per_member(map
)/IMSM_4K_DIV
);
1759 map
->blocks_per_strip
/= IMSM_4K_DIV
;
1760 set_pba_of_lba0(map
, pba_of_lba0(map
)/IMSM_4K_DIV
);
1762 if (dev
->vol
.migr_state
) {
1764 map
= get_imsm_map(dev
, MAP_1
);
1765 set_blocks_per_member(map
,
1766 blocks_per_member(map
)/IMSM_4K_DIV
);
1767 map
->blocks_per_strip
/= IMSM_4K_DIV
;
1768 set_pba_of_lba0(map
, pba_of_lba0(map
)/IMSM_4K_DIV
);
1772 struct bbm_log
*log
= (void *)mpb
+
1773 __le32_to_cpu(mpb
->mpb_size
) - bbm_log_size
;
1776 for (i
= 0; i
< log
->entry_count
; i
++) {
1777 struct bbm_log_entry
*entry
=
1778 &log
->marked_block_entries
[i
];
1780 __u8 count
= entry
->marked_count
+ 1;
1781 unsigned long long sector
=
1782 __le48_to_cpu(&entry
->defective_block_start
);
1784 entry
->defective_block_start
=
1785 __cpu_to_le48(sector
/IMSM_4K_DIV
);
1786 entry
->marked_count
= max(count
/IMSM_4K_DIV
, 1) - 1;
1790 mpb
->check_sum
= __gen_imsm_checksum(mpb
);
1793 void examine_migr_rec_imsm(struct intel_super
*super
)
1795 struct migr_record
*migr_rec
= super
->migr_rec
;
1796 struct imsm_super
*mpb
= super
->anchor
;
1799 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
1800 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
1801 struct imsm_map
*map
;
1804 if (is_gen_migration(dev
) == 0)
1807 printf("\nMigration Record Information:");
1809 /* first map under migration */
1810 map
= get_imsm_map(dev
, MAP_0
);
1812 slot
= get_imsm_disk_slot(map
, super
->disks
->index
);
1813 if (map
== NULL
|| slot
> 1 || slot
< 0) {
1814 printf(" Empty\n ");
1815 printf("Examine one of first two disks in array\n");
1818 printf("\n Status : ");
1819 if (__le32_to_cpu(migr_rec
->rec_status
) == UNIT_SRC_NORMAL
)
1822 printf("Contains Data\n");
1823 printf(" Current Unit : %llu\n",
1824 current_migr_unit(migr_rec
));
1825 printf(" Family : %u\n",
1826 __le32_to_cpu(migr_rec
->family_num
));
1827 printf(" Ascending : %u\n",
1828 __le32_to_cpu(migr_rec
->ascending_migr
));
1829 printf(" Blocks Per Unit : %u\n",
1830 __le32_to_cpu(migr_rec
->blocks_per_unit
));
1831 printf(" Dest. Depth Per Unit : %u\n",
1832 __le32_to_cpu(migr_rec
->dest_depth_per_unit
));
1833 printf(" Checkpoint Area pba : %llu\n",
1834 migr_chkp_area_pba(migr_rec
));
1835 printf(" First member lba : %llu\n",
1836 migr_dest_1st_member_lba(migr_rec
));
1837 printf(" Total Number of Units : %llu\n",
1838 get_num_migr_units(migr_rec
));
1839 printf(" Size of volume : %llu\n",
1840 join_u32(migr_rec
->post_migr_vol_cap
,
1841 migr_rec
->post_migr_vol_cap_hi
));
1842 printf(" Record was read from : %u\n",
1843 __le32_to_cpu(migr_rec
->ckpt_read_disk_num
));
1849 void convert_from_4k_imsm_migr_rec(struct intel_super
*super
)
1851 struct migr_record
*migr_rec
= super
->migr_rec
;
1853 migr_rec
->blocks_per_unit
*= IMSM_4K_DIV
;
1854 migr_rec
->dest_depth_per_unit
*= IMSM_4K_DIV
;
1855 split_ull((join_u32(migr_rec
->post_migr_vol_cap
,
1856 migr_rec
->post_migr_vol_cap_hi
) * IMSM_4K_DIV
),
1857 &migr_rec
->post_migr_vol_cap
,
1858 &migr_rec
->post_migr_vol_cap_hi
);
1859 set_migr_chkp_area_pba(migr_rec
,
1860 migr_chkp_area_pba(migr_rec
) * IMSM_4K_DIV
);
1861 set_migr_dest_1st_member_lba(migr_rec
,
1862 migr_dest_1st_member_lba(migr_rec
) * IMSM_4K_DIV
);
1865 void convert_from_4k(struct intel_super
*super
)
1867 struct imsm_super
*mpb
= super
->anchor
;
1868 struct imsm_disk
*disk
;
1870 __u32 bbm_log_size
= __le32_to_cpu(mpb
->bbm_log_size
);
1872 for (i
= 0; i
< mpb
->num_disks
; i
++) {
1873 disk
= __get_imsm_disk(mpb
, i
);
1875 set_total_blocks(disk
, (total_blocks(disk
)*IMSM_4K_DIV
));
1878 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
1879 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
1880 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
1882 set_imsm_dev_size(dev
, imsm_dev_size(dev
)*IMSM_4K_DIV
);
1883 dev
->vol
.curr_migr_unit
*= IMSM_4K_DIV
;
1886 set_blocks_per_member(map
, blocks_per_member(map
)*IMSM_4K_DIV
);
1887 map
->blocks_per_strip
*= IMSM_4K_DIV
;
1888 set_pba_of_lba0(map
, pba_of_lba0(map
)*IMSM_4K_DIV
);
1890 if (dev
->vol
.migr_state
) {
1892 map
= get_imsm_map(dev
, MAP_1
);
1893 set_blocks_per_member(map
,
1894 blocks_per_member(map
)*IMSM_4K_DIV
);
1895 map
->blocks_per_strip
*= IMSM_4K_DIV
;
1896 set_pba_of_lba0(map
, pba_of_lba0(map
)*IMSM_4K_DIV
);
1900 struct bbm_log
*log
= (void *)mpb
+
1901 __le32_to_cpu(mpb
->mpb_size
) - bbm_log_size
;
1904 for (i
= 0; i
< log
->entry_count
; i
++) {
1905 struct bbm_log_entry
*entry
=
1906 &log
->marked_block_entries
[i
];
1908 __u8 count
= entry
->marked_count
+ 1;
1909 unsigned long long sector
=
1910 __le48_to_cpu(&entry
->defective_block_start
);
1912 entry
->defective_block_start
=
1913 __cpu_to_le48(sector
*IMSM_4K_DIV
);
1914 entry
->marked_count
= count
*IMSM_4K_DIV
- 1;
1918 mpb
->check_sum
= __gen_imsm_checksum(mpb
);
1921 /*******************************************************************************
1922 * function: imsm_check_attributes
1923 * Description: Function checks if features represented by attributes flags
1924 * are supported by mdadm.
1926 * attributes - Attributes read from metadata
1928 * 0 - passed attributes contains unsupported features flags
1929 * 1 - all features are supported
1930 ******************************************************************************/
1931 static int imsm_check_attributes(__u32 attributes
)
1934 __u32 not_supported
= MPB_ATTRIB_SUPPORTED
^0xffffffff;
1936 not_supported
&= ~MPB_ATTRIB_IGNORED
;
1938 not_supported
&= attributes
;
1939 if (not_supported
) {
1940 pr_err("(IMSM): Unsupported attributes : %x\n",
1941 (unsigned)__le32_to_cpu(not_supported
));
1942 if (not_supported
& MPB_ATTRIB_CHECKSUM_VERIFY
) {
1943 dprintf("\t\tMPB_ATTRIB_CHECKSUM_VERIFY \n");
1944 not_supported
^= MPB_ATTRIB_CHECKSUM_VERIFY
;
1946 if (not_supported
& MPB_ATTRIB_2TB
) {
1947 dprintf("\t\tMPB_ATTRIB_2TB\n");
1948 not_supported
^= MPB_ATTRIB_2TB
;
1950 if (not_supported
& MPB_ATTRIB_RAID0
) {
1951 dprintf("\t\tMPB_ATTRIB_RAID0\n");
1952 not_supported
^= MPB_ATTRIB_RAID0
;
1954 if (not_supported
& MPB_ATTRIB_RAID1
) {
1955 dprintf("\t\tMPB_ATTRIB_RAID1\n");
1956 not_supported
^= MPB_ATTRIB_RAID1
;
1958 if (not_supported
& MPB_ATTRIB_RAID10
) {
1959 dprintf("\t\tMPB_ATTRIB_RAID10\n");
1960 not_supported
^= MPB_ATTRIB_RAID10
;
1962 if (not_supported
& MPB_ATTRIB_RAID1E
) {
1963 dprintf("\t\tMPB_ATTRIB_RAID1E\n");
1964 not_supported
^= MPB_ATTRIB_RAID1E
;
1966 if (not_supported
& MPB_ATTRIB_RAID5
) {
1967 dprintf("\t\tMPB_ATTRIB_RAID5\n");
1968 not_supported
^= MPB_ATTRIB_RAID5
;
1970 if (not_supported
& MPB_ATTRIB_RAIDCNG
) {
1971 dprintf("\t\tMPB_ATTRIB_RAIDCNG\n");
1972 not_supported
^= MPB_ATTRIB_RAIDCNG
;
1974 if (not_supported
& MPB_ATTRIB_BBM
) {
1975 dprintf("\t\tMPB_ATTRIB_BBM\n");
1976 not_supported
^= MPB_ATTRIB_BBM
;
1978 if (not_supported
& MPB_ATTRIB_CHECKSUM_VERIFY
) {
1979 dprintf("\t\tMPB_ATTRIB_CHECKSUM_VERIFY (== MPB_ATTRIB_LEGACY)\n");
1980 not_supported
^= MPB_ATTRIB_CHECKSUM_VERIFY
;
1982 if (not_supported
& MPB_ATTRIB_EXP_STRIPE_SIZE
) {
1983 dprintf("\t\tMPB_ATTRIB_EXP_STRIP_SIZE\n");
1984 not_supported
^= MPB_ATTRIB_EXP_STRIPE_SIZE
;
1986 if (not_supported
& MPB_ATTRIB_2TB_DISK
) {
1987 dprintf("\t\tMPB_ATTRIB_2TB_DISK\n");
1988 not_supported
^= MPB_ATTRIB_2TB_DISK
;
1990 if (not_supported
& MPB_ATTRIB_NEVER_USE2
) {
1991 dprintf("\t\tMPB_ATTRIB_NEVER_USE2\n");
1992 not_supported
^= MPB_ATTRIB_NEVER_USE2
;
1994 if (not_supported
& MPB_ATTRIB_NEVER_USE
) {
1995 dprintf("\t\tMPB_ATTRIB_NEVER_USE\n");
1996 not_supported
^= MPB_ATTRIB_NEVER_USE
;
2000 dprintf("(IMSM): Unknown attributes : %x\n", not_supported
);
2008 static void getinfo_super_imsm(struct supertype
*st
, struct mdinfo
*info
, char *map
);
2010 static void examine_super_imsm(struct supertype
*st
, char *homehost
)
2012 struct intel_super
*super
= st
->sb
;
2013 struct imsm_super
*mpb
= super
->anchor
;
2014 char str
[MAX_SIGNATURE_LENGTH
];
2019 __u32 reserved
= imsm_reserved_sectors(super
, super
->disks
);
2021 time_t creation_time
;
2023 strncpy(str
, (char *)mpb
->sig
, MPB_SIG_LEN
);
2024 str
[MPB_SIG_LEN
-1] = '\0';
2025 printf(" Magic : %s\n", str
);
2026 printf(" Version : %s\n", get_imsm_version(mpb
));
2027 printf(" Orig Family : %08x\n", __le32_to_cpu(mpb
->orig_family_num
));
2028 printf(" Family : %08x\n", __le32_to_cpu(mpb
->family_num
));
2029 printf(" Generation : %08x\n", __le32_to_cpu(mpb
->generation_num
));
2030 creation_time
= __le64_to_cpu(mpb
->creation_time
);
2031 printf(" Creation Time : %.24s\n",
2032 creation_time
? ctime(&creation_time
) : "Unknown");
2033 printf(" Attributes : ");
2034 if (imsm_check_attributes(mpb
->attributes
))
2035 printf("All supported\n");
2037 printf("not supported\n");
2038 getinfo_super_imsm(st
, &info
, NULL
);
2039 fname_from_uuid(st
, &info
, nbuf
, ':');
2040 printf(" UUID : %s\n", nbuf
+ 5);
2041 sum
= __le32_to_cpu(mpb
->check_sum
);
2042 printf(" Checksum : %08x %s\n", sum
,
2043 __gen_imsm_checksum(mpb
) == sum
? "correct" : "incorrect");
2044 printf(" MPB Sectors : %d\n", mpb_sectors(mpb
, super
->sector_size
));
2045 printf(" Disks : %d\n", mpb
->num_disks
);
2046 printf(" RAID Devices : %d\n", mpb
->num_raid_devs
);
2047 print_imsm_disk(__get_imsm_disk(mpb
, super
->disks
->index
),
2048 super
->disks
->index
, reserved
, super
->sector_size
);
2049 if (get_imsm_bbm_log_size(super
->bbm_log
)) {
2050 struct bbm_log
*log
= super
->bbm_log
;
2053 printf("Bad Block Management Log:\n");
2054 printf(" Log Size : %d\n", __le32_to_cpu(mpb
->bbm_log_size
));
2055 printf(" Signature : %x\n", __le32_to_cpu(log
->signature
));
2056 printf(" Entry Count : %d\n", __le32_to_cpu(log
->entry_count
));
2058 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
2060 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
2062 super
->current_vol
= i
;
2063 getinfo_super_imsm(st
, &info
, NULL
);
2064 fname_from_uuid(st
, &info
, nbuf
, ':');
2065 print_imsm_dev(super
, dev
, nbuf
+ 5, super
->disks
->index
);
2067 for (i
= 0; i
< mpb
->num_disks
; i
++) {
2068 if (i
== super
->disks
->index
)
2070 print_imsm_disk(__get_imsm_disk(mpb
, i
), i
, reserved
,
2071 super
->sector_size
);
2074 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
2075 if (dl
->index
== -1)
2076 print_imsm_disk(&dl
->disk
, -1, reserved
,
2077 super
->sector_size
);
2079 examine_migr_rec_imsm(super
);
2082 static void brief_examine_super_imsm(struct supertype
*st
, int verbose
)
2084 /* We just write a generic IMSM ARRAY entry */
2087 struct intel_super
*super
= st
->sb
;
2089 if (!super
->anchor
->num_raid_devs
) {
2090 printf("ARRAY metadata=imsm\n");
2094 getinfo_super_imsm(st
, &info
, NULL
);
2095 fname_from_uuid(st
, &info
, nbuf
, ':');
2096 printf("ARRAY metadata=imsm UUID=%s\n", nbuf
+ 5);
2099 static void brief_examine_subarrays_imsm(struct supertype
*st
, int verbose
)
2101 /* We just write a generic IMSM ARRAY entry */
2105 struct intel_super
*super
= st
->sb
;
2108 if (!super
->anchor
->num_raid_devs
)
2111 getinfo_super_imsm(st
, &info
, NULL
);
2112 fname_from_uuid(st
, &info
, nbuf
, ':');
2113 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
2114 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
2116 super
->current_vol
= i
;
2117 getinfo_super_imsm(st
, &info
, NULL
);
2118 fname_from_uuid(st
, &info
, nbuf1
, ':');
2119 printf("ARRAY /dev/md/%.16s container=%s member=%d UUID=%s\n",
2120 dev
->volume
, nbuf
+ 5, i
, nbuf1
+ 5);
2124 static void export_examine_super_imsm(struct supertype
*st
)
2126 struct intel_super
*super
= st
->sb
;
2127 struct imsm_super
*mpb
= super
->anchor
;
2131 getinfo_super_imsm(st
, &info
, NULL
);
2132 fname_from_uuid(st
, &info
, nbuf
, ':');
2133 printf("MD_METADATA=imsm\n");
2134 printf("MD_LEVEL=container\n");
2135 printf("MD_UUID=%s\n", nbuf
+5);
2136 printf("MD_DEVICES=%u\n", mpb
->num_disks
);
2137 printf("MD_CREATION_TIME=%llu\n", __le64_to_cpu(mpb
->creation_time
));
2140 static void detail_super_imsm(struct supertype
*st
, char *homehost
,
2145 struct intel_super
*super
= st
->sb
;
2146 int temp_vol
= super
->current_vol
;
2149 super
->current_vol
= strtoul(subarray
, NULL
, 10);
2151 getinfo_super_imsm(st
, &info
, NULL
);
2152 fname_from_uuid(st
, &info
, nbuf
, ':');
2153 printf("\n UUID : %s\n", nbuf
+ 5);
2155 super
->current_vol
= temp_vol
;
2158 static void brief_detail_super_imsm(struct supertype
*st
, char *subarray
)
2162 struct intel_super
*super
= st
->sb
;
2163 int temp_vol
= super
->current_vol
;
2166 super
->current_vol
= strtoul(subarray
, NULL
, 10);
2168 getinfo_super_imsm(st
, &info
, NULL
);
2169 fname_from_uuid(st
, &info
, nbuf
, ':');
2170 printf(" UUID=%s", nbuf
+ 5);
2172 super
->current_vol
= temp_vol
;
2175 static int imsm_read_serial(int fd
, char *devname
, __u8
*serial
,
2176 size_t serial_buf_len
);
2177 static void fd2devname(int fd
, char *name
);
2179 static int ahci_enumerate_ports(const char *hba_path
, int port_count
, int host_base
, int verbose
)
2181 /* dump an unsorted list of devices attached to AHCI Intel storage
2182 * controller, as well as non-connected ports
2184 int hba_len
= strlen(hba_path
) + 1;
2189 unsigned long port_mask
= (1 << port_count
) - 1;
2191 if (port_count
> (int)sizeof(port_mask
) * 8) {
2193 pr_err("port_count %d out of range\n", port_count
);
2197 /* scroll through /sys/dev/block looking for devices attached to
2200 dir
= opendir("/sys/dev/block");
2204 for (ent
= readdir(dir
); ent
; ent
= readdir(dir
)) {
2215 if (sscanf(ent
->d_name
, "%d:%d", &major
, &minor
) != 2)
2217 path
= devt_to_devpath(makedev(major
, minor
));
2220 if (!path_attached_to_hba(path
, hba_path
)) {
2226 /* retrieve the scsi device type */
2227 if (asprintf(&device
, "/sys/dev/block/%d:%d/device/xxxxxxx", major
, minor
) < 0) {
2229 pr_err("failed to allocate 'device'\n");
2233 sprintf(device
, "/sys/dev/block/%d:%d/device/type", major
, minor
);
2234 if (load_sys(device
, buf
, sizeof(buf
)) != 0) {
2236 pr_err("failed to read device type for %s\n",
2242 type
= strtoul(buf
, NULL
, 10);
2244 /* if it's not a disk print the vendor and model */
2245 if (!(type
== 0 || type
== 7 || type
== 14)) {
2248 sprintf(device
, "/sys/dev/block/%d:%d/device/vendor", major
, minor
);
2249 if (load_sys(device
, buf
, sizeof(buf
)) == 0) {
2250 strncpy(vendor
, buf
, sizeof(vendor
));
2251 vendor
[sizeof(vendor
) - 1] = '\0';
2252 c
= (char *) &vendor
[sizeof(vendor
) - 1];
2253 while (isspace(*c
) || *c
== '\0')
2257 sprintf(device
, "/sys/dev/block/%d:%d/device/model", major
, minor
);
2258 if (load_sys(device
, buf
, sizeof(buf
)) == 0) {
2259 strncpy(model
, buf
, sizeof(model
));
2260 model
[sizeof(model
) - 1] = '\0';
2261 c
= (char *) &model
[sizeof(model
) - 1];
2262 while (isspace(*c
) || *c
== '\0')
2266 if (vendor
[0] && model
[0])
2267 sprintf(buf
, "%.64s %.64s", vendor
, model
);
2269 switch (type
) { /* numbers from hald/linux/device.c */
2270 case 1: sprintf(buf
, "tape"); break;
2271 case 2: sprintf(buf
, "printer"); break;
2272 case 3: sprintf(buf
, "processor"); break;
2274 case 5: sprintf(buf
, "cdrom"); break;
2275 case 6: sprintf(buf
, "scanner"); break;
2276 case 8: sprintf(buf
, "media_changer"); break;
2277 case 9: sprintf(buf
, "comm"); break;
2278 case 12: sprintf(buf
, "raid"); break;
2279 default: sprintf(buf
, "unknown");
2285 /* chop device path to 'host%d' and calculate the port number */
2286 c
= strchr(&path
[hba_len
], '/');
2289 pr_err("%s - invalid path name\n", path
+ hba_len
);
2294 if ((sscanf(&path
[hba_len
], "ata%d", &port
) == 1) ||
2295 ((sscanf(&path
[hba_len
], "host%d", &port
) == 1)))
2299 *c
= '/'; /* repair the full string */
2300 pr_err("failed to determine port number for %s\n",
2307 /* mark this port as used */
2308 port_mask
&= ~(1 << port
);
2310 /* print out the device information */
2312 printf(" Port%d : - non-disk device (%s) -\n", port
, buf
);
2316 fd
= dev_open(ent
->d_name
, O_RDONLY
);
2318 printf(" Port%d : - disk info unavailable -\n", port
);
2320 fd2devname(fd
, buf
);
2321 printf(" Port%d : %s", port
, buf
);
2322 if (imsm_read_serial(fd
, NULL
, (__u8
*)buf
,
2324 printf(" (%s)\n", buf
);
2339 for (i
= 0; i
< port_count
; i
++)
2340 if (port_mask
& (1 << i
))
2341 printf(" Port%d : - no device attached -\n", i
);
2347 static int print_nvme_info(struct sys_dev
*hba
)
2355 dir
= opendir("/sys/block/");
2359 for (ent
= readdir(dir
); ent
; ent
= readdir(dir
)) {
2360 if (strstr(ent
->d_name
, "nvme")) {
2361 sprintf(buf
, "/sys/block/%s", ent
->d_name
);
2362 rp
= realpath(buf
, NULL
);
2365 if (path_attached_to_hba(rp
, hba
->path
)) {
2366 fd
= open_dev(ent
->d_name
);
2367 if (!imsm_is_nvme_supported(fd
, 0)) {
2374 fd2devname(fd
, buf
);
2375 if (hba
->type
== SYS_DEV_VMD
)
2376 printf(" NVMe under VMD : %s", buf
);
2377 else if (hba
->type
== SYS_DEV_NVME
)
2378 printf(" NVMe Device : %s", buf
);
2379 if (!imsm_read_serial(fd
, NULL
, (__u8
*)buf
,
2381 printf(" (%s)\n", buf
);
2394 static void print_found_intel_controllers(struct sys_dev
*elem
)
2396 for (; elem
; elem
= elem
->next
) {
2397 pr_err("found Intel(R) ");
2398 if (elem
->type
== SYS_DEV_SATA
)
2399 fprintf(stderr
, "SATA ");
2400 else if (elem
->type
== SYS_DEV_SAS
)
2401 fprintf(stderr
, "SAS ");
2402 else if (elem
->type
== SYS_DEV_NVME
)
2403 fprintf(stderr
, "NVMe ");
2405 if (elem
->type
== SYS_DEV_VMD
)
2406 fprintf(stderr
, "VMD domain");
2408 fprintf(stderr
, "RAID controller");
2411 fprintf(stderr
, " at %s", elem
->pci_id
);
2412 fprintf(stderr
, ".\n");
2417 static int ahci_get_port_count(const char *hba_path
, int *port_count
)
2424 if ((dir
= opendir(hba_path
)) == NULL
)
2427 for (ent
= readdir(dir
); ent
; ent
= readdir(dir
)) {
2430 if ((sscanf(ent
->d_name
, "ata%d", &host
) != 1) &&
2431 ((sscanf(ent
->d_name
, "host%d", &host
) != 1)))
2433 if (*port_count
== 0)
2435 else if (host
< host_base
)
2438 if (host
+ 1 > *port_count
+ host_base
)
2439 *port_count
= host
+ 1 - host_base
;
2445 static void print_imsm_capability(const struct imsm_orom
*orom
)
2447 printf(" Platform : Intel(R) ");
2448 if (orom
->capabilities
== 0 && orom
->driver_features
== 0)
2449 printf("Matrix Storage Manager\n");
2450 else if (imsm_orom_is_enterprise(orom
) && orom
->major_ver
>= 6)
2451 printf("Virtual RAID on CPU\n");
2453 printf("Rapid Storage Technology%s\n",
2454 imsm_orom_is_enterprise(orom
) ? " enterprise" : "");
2455 if (orom
->major_ver
|| orom
->minor_ver
|| orom
->hotfix_ver
|| orom
->build
)
2456 printf(" Version : %d.%d.%d.%d\n", orom
->major_ver
,
2457 orom
->minor_ver
, orom
->hotfix_ver
, orom
->build
);
2458 printf(" RAID Levels :%s%s%s%s%s\n",
2459 imsm_orom_has_raid0(orom
) ? " raid0" : "",
2460 imsm_orom_has_raid1(orom
) ? " raid1" : "",
2461 imsm_orom_has_raid1e(orom
) ? " raid1e" : "",
2462 imsm_orom_has_raid10(orom
) ? " raid10" : "",
2463 imsm_orom_has_raid5(orom
) ? " raid5" : "");
2464 printf(" Chunk Sizes :%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
2465 imsm_orom_has_chunk(orom
, 2) ? " 2k" : "",
2466 imsm_orom_has_chunk(orom
, 4) ? " 4k" : "",
2467 imsm_orom_has_chunk(orom
, 8) ? " 8k" : "",
2468 imsm_orom_has_chunk(orom
, 16) ? " 16k" : "",
2469 imsm_orom_has_chunk(orom
, 32) ? " 32k" : "",
2470 imsm_orom_has_chunk(orom
, 64) ? " 64k" : "",
2471 imsm_orom_has_chunk(orom
, 128) ? " 128k" : "",
2472 imsm_orom_has_chunk(orom
, 256) ? " 256k" : "",
2473 imsm_orom_has_chunk(orom
, 512) ? " 512k" : "",
2474 imsm_orom_has_chunk(orom
, 1024*1) ? " 1M" : "",
2475 imsm_orom_has_chunk(orom
, 1024*2) ? " 2M" : "",
2476 imsm_orom_has_chunk(orom
, 1024*4) ? " 4M" : "",
2477 imsm_orom_has_chunk(orom
, 1024*8) ? " 8M" : "",
2478 imsm_orom_has_chunk(orom
, 1024*16) ? " 16M" : "",
2479 imsm_orom_has_chunk(orom
, 1024*32) ? " 32M" : "",
2480 imsm_orom_has_chunk(orom
, 1024*64) ? " 64M" : "");
2481 printf(" 2TB volumes :%s supported\n",
2482 (orom
->attr
& IMSM_OROM_ATTR_2TB
)?"":" not");
2483 printf(" 2TB disks :%s supported\n",
2484 (orom
->attr
& IMSM_OROM_ATTR_2TB_DISK
)?"":" not");
2485 printf(" Max Disks : %d\n", orom
->tds
);
2486 printf(" Max Volumes : %d per array, %d per %s\n",
2487 orom
->vpa
, orom
->vphba
,
2488 imsm_orom_is_nvme(orom
) ? "platform" : "controller");
2492 static void print_imsm_capability_export(const struct imsm_orom
*orom
)
2494 printf("MD_FIRMWARE_TYPE=imsm\n");
2495 if (orom
->major_ver
|| orom
->minor_ver
|| orom
->hotfix_ver
|| orom
->build
)
2496 printf("IMSM_VERSION=%d.%d.%d.%d\n", orom
->major_ver
, orom
->minor_ver
,
2497 orom
->hotfix_ver
, orom
->build
);
2498 printf("IMSM_SUPPORTED_RAID_LEVELS=%s%s%s%s%s\n",
2499 imsm_orom_has_raid0(orom
) ? "raid0 " : "",
2500 imsm_orom_has_raid1(orom
) ? "raid1 " : "",
2501 imsm_orom_has_raid1e(orom
) ? "raid1e " : "",
2502 imsm_orom_has_raid5(orom
) ? "raid10 " : "",
2503 imsm_orom_has_raid10(orom
) ? "raid5 " : "");
2504 printf("IMSM_SUPPORTED_CHUNK_SIZES=%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
2505 imsm_orom_has_chunk(orom
, 2) ? "2k " : "",
2506 imsm_orom_has_chunk(orom
, 4) ? "4k " : "",
2507 imsm_orom_has_chunk(orom
, 8) ? "8k " : "",
2508 imsm_orom_has_chunk(orom
, 16) ? "16k " : "",
2509 imsm_orom_has_chunk(orom
, 32) ? "32k " : "",
2510 imsm_orom_has_chunk(orom
, 64) ? "64k " : "",
2511 imsm_orom_has_chunk(orom
, 128) ? "128k " : "",
2512 imsm_orom_has_chunk(orom
, 256) ? "256k " : "",
2513 imsm_orom_has_chunk(orom
, 512) ? "512k " : "",
2514 imsm_orom_has_chunk(orom
, 1024*1) ? "1M " : "",
2515 imsm_orom_has_chunk(orom
, 1024*2) ? "2M " : "",
2516 imsm_orom_has_chunk(orom
, 1024*4) ? "4M " : "",
2517 imsm_orom_has_chunk(orom
, 1024*8) ? "8M " : "",
2518 imsm_orom_has_chunk(orom
, 1024*16) ? "16M " : "",
2519 imsm_orom_has_chunk(orom
, 1024*32) ? "32M " : "",
2520 imsm_orom_has_chunk(orom
, 1024*64) ? "64M " : "");
2521 printf("IMSM_2TB_VOLUMES=%s\n",(orom
->attr
& IMSM_OROM_ATTR_2TB
) ? "yes" : "no");
2522 printf("IMSM_2TB_DISKS=%s\n",(orom
->attr
& IMSM_OROM_ATTR_2TB_DISK
) ? "yes" : "no");
2523 printf("IMSM_MAX_DISKS=%d\n",orom
->tds
);
2524 printf("IMSM_MAX_VOLUMES_PER_ARRAY=%d\n",orom
->vpa
);
2525 printf("IMSM_MAX_VOLUMES_PER_CONTROLLER=%d\n",orom
->vphba
);
2528 static int detail_platform_imsm(int verbose
, int enumerate_only
, char *controller_path
)
2530 /* There are two components to imsm platform support, the ahci SATA
2531 * controller and the option-rom. To find the SATA controller we
2532 * simply look in /sys/bus/pci/drivers/ahci to see if an ahci
2533 * controller with the Intel vendor id is present. This approach
2534 * allows mdadm to leverage the kernel's ahci detection logic, with the
2535 * caveat that if ahci.ko is not loaded mdadm will not be able to
2536 * detect platform raid capabilities. The option-rom resides in a
2537 * platform "Adapter ROM". We scan for its signature to retrieve the
2538 * platform capabilities. If raid support is disabled in the BIOS the
2539 * option-rom capability structure will not be available.
2541 struct sys_dev
*list
, *hba
;
2546 if (enumerate_only
) {
2547 if (check_env("IMSM_NO_PLATFORM"))
2549 list
= find_intel_devices();
2552 for (hba
= list
; hba
; hba
= hba
->next
) {
2553 if (find_imsm_capability(hba
)) {
2563 list
= find_intel_devices();
2566 pr_err("no active Intel(R) RAID controller found.\n");
2568 } else if (verbose
> 0)
2569 print_found_intel_controllers(list
);
2571 for (hba
= list
; hba
; hba
= hba
->next
) {
2572 if (controller_path
&& (compare_paths(hba
->path
, controller_path
) != 0))
2574 if (!find_imsm_capability(hba
)) {
2576 pr_err("imsm capabilities not found for controller: %s (type %s)\n",
2577 hba
->type
== SYS_DEV_VMD
? vmd_domain_to_controller(hba
, buf
) : hba
->path
,
2578 get_sys_dev_type(hba
->type
));
2584 if (controller_path
&& result
== 1) {
2585 pr_err("no active Intel(R) RAID controller found under %s\n",
2590 const struct orom_entry
*entry
;
2592 for (entry
= orom_entries
; entry
; entry
= entry
->next
) {
2593 if (entry
->type
== SYS_DEV_VMD
) {
2594 print_imsm_capability(&entry
->orom
);
2595 printf(" 3rd party NVMe :%s supported\n",
2596 imsm_orom_has_tpv_support(&entry
->orom
)?"":" not");
2597 for (hba
= list
; hba
; hba
= hba
->next
) {
2598 if (hba
->type
== SYS_DEV_VMD
) {
2600 printf(" I/O Controller : %s (%s)\n",
2601 vmd_domain_to_controller(hba
, buf
), get_sys_dev_type(hba
->type
));
2602 if (print_nvme_info(hba
)) {
2604 pr_err("failed to get devices attached to VMD domain.\n");
2613 print_imsm_capability(&entry
->orom
);
2614 if (entry
->type
== SYS_DEV_NVME
) {
2615 for (hba
= list
; hba
; hba
= hba
->next
) {
2616 if (hba
->type
== SYS_DEV_NVME
)
2617 print_nvme_info(hba
);
2623 struct devid_list
*devid
;
2624 for (devid
= entry
->devid_list
; devid
; devid
= devid
->next
) {
2625 hba
= device_by_id(devid
->devid
);
2629 printf(" I/O Controller : %s (%s)\n",
2630 hba
->path
, get_sys_dev_type(hba
->type
));
2631 if (hba
->type
== SYS_DEV_SATA
) {
2632 host_base
= ahci_get_port_count(hba
->path
, &port_count
);
2633 if (ahci_enumerate_ports(hba
->path
, port_count
, host_base
, verbose
)) {
2635 pr_err("failed to enumerate ports on SATA controller at %s.\n", hba
->pci_id
);
2646 static int export_detail_platform_imsm(int verbose
, char *controller_path
)
2648 struct sys_dev
*list
, *hba
;
2651 list
= find_intel_devices();
2654 pr_err("IMSM_DETAIL_PLATFORM_ERROR=NO_INTEL_DEVICES\n");
2659 for (hba
= list
; hba
; hba
= hba
->next
) {
2660 if (controller_path
&& (compare_paths(hba
->path
,controller_path
) != 0))
2662 if (!find_imsm_capability(hba
) && verbose
> 0) {
2664 pr_err("IMSM_DETAIL_PLATFORM_ERROR=NO_IMSM_CAPABLE_DEVICE_UNDER_%s\n",
2665 hba
->type
== SYS_DEV_VMD
? vmd_domain_to_controller(hba
, buf
) : hba
->path
);
2671 const struct orom_entry
*entry
;
2673 for (entry
= orom_entries
; entry
; entry
= entry
->next
) {
2674 if (entry
->type
== SYS_DEV_VMD
) {
2675 for (hba
= list
; hba
; hba
= hba
->next
)
2676 print_imsm_capability_export(&entry
->orom
);
2679 print_imsm_capability_export(&entry
->orom
);
2685 static int match_home_imsm(struct supertype
*st
, char *homehost
)
2687 /* the imsm metadata format does not specify any host
2688 * identification information. We return -1 since we can never
2689 * confirm nor deny whether a given array is "meant" for this
2690 * host. We rely on compare_super and the 'family_num' fields to
2691 * exclude member disks that do not belong, and we rely on
2692 * mdadm.conf to specify the arrays that should be assembled.
2693 * Auto-assembly may still pick up "foreign" arrays.
2699 static void uuid_from_super_imsm(struct supertype
*st
, int uuid
[4])
2701 /* The uuid returned here is used for:
2702 * uuid to put into bitmap file (Create, Grow)
2703 * uuid for backup header when saving critical section (Grow)
2704 * comparing uuids when re-adding a device into an array
2705 * In these cases the uuid required is that of the data-array,
2706 * not the device-set.
2707 * uuid to recognise same set when adding a missing device back
2708 * to an array. This is a uuid for the device-set.
2710 * For each of these we can make do with a truncated
2711 * or hashed uuid rather than the original, as long as
2713 * In each case the uuid required is that of the data-array,
2714 * not the device-set.
2716 /* imsm does not track uuid's so we synthesis one using sha1 on
2717 * - The signature (Which is constant for all imsm array, but no matter)
2718 * - the orig_family_num of the container
2719 * - the index number of the volume
2720 * - the 'serial' number of the volume.
2721 * Hopefully these are all constant.
2723 struct intel_super
*super
= st
->sb
;
2726 struct sha1_ctx ctx
;
2727 struct imsm_dev
*dev
= NULL
;
2730 /* some mdadm versions failed to set ->orig_family_num, in which
2731 * case fall back to ->family_num. orig_family_num will be
2732 * fixed up with the first metadata update.
2734 family_num
= super
->anchor
->orig_family_num
;
2735 if (family_num
== 0)
2736 family_num
= super
->anchor
->family_num
;
2737 sha1_init_ctx(&ctx
);
2738 sha1_process_bytes(super
->anchor
->sig
, MPB_SIG_LEN
, &ctx
);
2739 sha1_process_bytes(&family_num
, sizeof(__u32
), &ctx
);
2740 if (super
->current_vol
>= 0)
2741 dev
= get_imsm_dev(super
, super
->current_vol
);
2743 __u32 vol
= super
->current_vol
;
2744 sha1_process_bytes(&vol
, sizeof(vol
), &ctx
);
2745 sha1_process_bytes(dev
->volume
, MAX_RAID_SERIAL_LEN
, &ctx
);
2747 sha1_finish_ctx(&ctx
, buf
);
2748 memcpy(uuid
, buf
, 4*4);
2753 get_imsm_numerical_version(struct imsm_super
*mpb
, int *m
, int *p
)
2755 __u8
*v
= get_imsm_version(mpb
);
2756 __u8
*end
= mpb
->sig
+ MAX_SIGNATURE_LENGTH
;
2757 char major
[] = { 0, 0, 0 };
2758 char minor
[] = { 0 ,0, 0 };
2759 char patch
[] = { 0, 0, 0 };
2760 char *ver_parse
[] = { major
, minor
, patch
};
2764 while (*v
!= '\0' && v
< end
) {
2765 if (*v
!= '.' && j
< 2)
2766 ver_parse
[i
][j
++] = *v
;
2774 *m
= strtol(minor
, NULL
, 0);
2775 *p
= strtol(patch
, NULL
, 0);
2779 static __u32
migr_strip_blocks_resync(struct imsm_dev
*dev
)
2781 /* migr_strip_size when repairing or initializing parity */
2782 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
2783 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
2785 switch (get_imsm_raid_level(map
)) {
2790 return 128*1024 >> 9;
2794 static __u32
migr_strip_blocks_rebuild(struct imsm_dev
*dev
)
2796 /* migr_strip_size when rebuilding a degraded disk, no idea why
2797 * this is different than migr_strip_size_resync(), but it's good
2800 struct imsm_map
*map
= get_imsm_map(dev
, MAP_1
);
2801 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
2803 switch (get_imsm_raid_level(map
)) {
2806 if (map
->num_members
% map
->num_domains
== 0)
2807 return 128*1024 >> 9;
2811 return max((__u32
) 64*1024 >> 9, chunk
);
2813 return 128*1024 >> 9;
2817 static __u32
num_stripes_per_unit_resync(struct imsm_dev
*dev
)
2819 struct imsm_map
*lo
= get_imsm_map(dev
, MAP_0
);
2820 struct imsm_map
*hi
= get_imsm_map(dev
, MAP_1
);
2821 __u32 lo_chunk
= __le32_to_cpu(lo
->blocks_per_strip
);
2822 __u32 hi_chunk
= __le32_to_cpu(hi
->blocks_per_strip
);
2824 return max((__u32
) 1, hi_chunk
/ lo_chunk
);
2827 static __u32
num_stripes_per_unit_rebuild(struct imsm_dev
*dev
)
2829 struct imsm_map
*lo
= get_imsm_map(dev
, MAP_0
);
2830 int level
= get_imsm_raid_level(lo
);
2832 if (level
== 1 || level
== 10) {
2833 struct imsm_map
*hi
= get_imsm_map(dev
, MAP_1
);
2835 return hi
->num_domains
;
2837 return num_stripes_per_unit_resync(dev
);
2840 static __u8
imsm_num_data_members(struct imsm_map
*map
)
2842 /* named 'imsm_' because raid0, raid1 and raid10
2843 * counter-intuitively have the same number of data disks
2845 switch (get_imsm_raid_level(map
)) {
2847 return map
->num_members
;
2851 return map
->num_members
/2;
2853 return map
->num_members
- 1;
2855 dprintf("unsupported raid level\n");
2860 static unsigned long long calc_component_size(struct imsm_map
*map
,
2861 struct imsm_dev
*dev
)
2863 unsigned long long component_size
;
2864 unsigned long long dev_size
= imsm_dev_size(dev
);
2865 long long calc_dev_size
= 0;
2866 unsigned int member_disks
= imsm_num_data_members(map
);
2868 if (member_disks
== 0)
2871 component_size
= per_dev_array_size(map
);
2872 calc_dev_size
= component_size
* member_disks
;
2874 /* Component size is rounded to 1MB so difference between size from
2875 * metadata and size calculated from num_data_stripes equals up to
2876 * 2048 blocks per each device. If the difference is higher it means
2877 * that array size was expanded and num_data_stripes was not updated.
2879 if (llabs(calc_dev_size
- (long long)dev_size
) >
2880 (1 << SECT_PER_MB_SHIFT
) * member_disks
) {
2881 component_size
= dev_size
/ member_disks
;
2882 dprintf("Invalid num_data_stripes in metadata; expected=%llu, found=%llu\n",
2883 component_size
/ map
->blocks_per_strip
,
2884 num_data_stripes(map
));
2887 return component_size
;
2890 static __u32
parity_segment_depth(struct imsm_dev
*dev
)
2892 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
2893 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
2895 switch(get_imsm_raid_level(map
)) {
2898 return chunk
* map
->num_domains
;
2900 return chunk
* map
->num_members
;
2906 static __u32
map_migr_block(struct imsm_dev
*dev
, __u32 block
)
2908 struct imsm_map
*map
= get_imsm_map(dev
, MAP_1
);
2909 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
2910 __u32 strip
= block
/ chunk
;
2912 switch (get_imsm_raid_level(map
)) {
2915 __u32 vol_strip
= (strip
* map
->num_domains
) + 1;
2916 __u32 vol_stripe
= vol_strip
/ map
->num_members
;
2918 return vol_stripe
* chunk
+ block
% chunk
;
2920 __u32 stripe
= strip
/ (map
->num_members
- 1);
2922 return stripe
* chunk
+ block
% chunk
;
2929 static __u64
blocks_per_migr_unit(struct intel_super
*super
,
2930 struct imsm_dev
*dev
)
2932 /* calculate the conversion factor between per member 'blocks'
2933 * (md/{resync,rebuild}_start) and imsm migration units, return
2934 * 0 for the 'not migrating' and 'unsupported migration' cases
2936 if (!dev
->vol
.migr_state
)
2939 switch (migr_type(dev
)) {
2940 case MIGR_GEN_MIGR
: {
2941 struct migr_record
*migr_rec
= super
->migr_rec
;
2942 return __le32_to_cpu(migr_rec
->blocks_per_unit
);
2947 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
2948 __u32 stripes_per_unit
;
2949 __u32 blocks_per_unit
;
2958 /* yes, this is really the translation of migr_units to
2959 * per-member blocks in the 'resync' case
2961 stripes_per_unit
= num_stripes_per_unit_resync(dev
);
2962 migr_chunk
= migr_strip_blocks_resync(dev
);
2963 disks
= imsm_num_data_members(map
);
2964 blocks_per_unit
= stripes_per_unit
* migr_chunk
* disks
;
2965 stripe
= __le16_to_cpu(map
->blocks_per_strip
) * disks
;
2966 segment
= blocks_per_unit
/ stripe
;
2967 block_rel
= blocks_per_unit
- segment
* stripe
;
2968 parity_depth
= parity_segment_depth(dev
);
2969 block_map
= map_migr_block(dev
, block_rel
);
2970 return block_map
+ parity_depth
* segment
;
2972 case MIGR_REBUILD
: {
2973 __u32 stripes_per_unit
;
2976 stripes_per_unit
= num_stripes_per_unit_rebuild(dev
);
2977 migr_chunk
= migr_strip_blocks_rebuild(dev
);
2978 return migr_chunk
* stripes_per_unit
;
2980 case MIGR_STATE_CHANGE
:
2986 static int imsm_level_to_layout(int level
)
2994 return ALGORITHM_LEFT_ASYMMETRIC
;
3001 /*******************************************************************************
3002 * Function: read_imsm_migr_rec
3003 * Description: Function reads imsm migration record from last sector of disk
3005 * fd : disk descriptor
3006 * super : metadata info
3010 ******************************************************************************/
3011 static int read_imsm_migr_rec(int fd
, struct intel_super
*super
)
3014 unsigned int sector_size
= super
->sector_size
;
3015 unsigned long long dsize
;
3017 get_dev_size(fd
, NULL
, &dsize
);
3018 if (lseek64(fd
, dsize
- (sector_size
*MIGR_REC_SECTOR_POSITION
),
3020 pr_err("Cannot seek to anchor block: %s\n",
3024 if ((unsigned int)read(fd
, super
->migr_rec_buf
,
3025 MIGR_REC_BUF_SECTORS
*sector_size
) !=
3026 MIGR_REC_BUF_SECTORS
*sector_size
) {
3027 pr_err("Cannot read migr record block: %s\n",
3032 if (sector_size
== 4096)
3033 convert_from_4k_imsm_migr_rec(super
);
3039 static struct imsm_dev
*imsm_get_device_during_migration(
3040 struct intel_super
*super
)
3043 struct intel_dev
*dv
;
3045 for (dv
= super
->devlist
; dv
; dv
= dv
->next
) {
3046 if (is_gen_migration(dv
->dev
))
3052 /*******************************************************************************
3053 * Function: load_imsm_migr_rec
3054 * Description: Function reads imsm migration record (it is stored at the last
3057 * super : imsm internal array info
3061 * -2 : no migration in progress
3062 ******************************************************************************/
3063 static int load_imsm_migr_rec(struct intel_super
*super
)
3069 struct imsm_dev
*dev
;
3070 struct imsm_map
*map
;
3074 /* find map under migration */
3075 dev
= imsm_get_device_during_migration(super
);
3076 /* nothing to load,no migration in progress?
3081 map
= get_imsm_map(dev
, MAP_0
);
3085 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
3086 /* skip spare and failed disks
3090 /* read only from one of the first two slots
3092 slot
= get_imsm_disk_slot(map
, dl
->index
);
3093 if (slot
> 1 || slot
< 0)
3097 sprintf(nm
, "%d:%d", dl
->major
, dl
->minor
);
3098 fd
= dev_open(nm
, O_RDONLY
);
3111 retval
= read_imsm_migr_rec(fd
, super
);
3118 /*******************************************************************************
3119 * function: imsm_create_metadata_checkpoint_update
3120 * Description: It creates update for checkpoint change.
3122 * super : imsm internal array info
3123 * u : pointer to prepared update
3126 * If length is equal to 0, input pointer u contains no update
3127 ******************************************************************************/
3128 static int imsm_create_metadata_checkpoint_update(
3129 struct intel_super
*super
,
3130 struct imsm_update_general_migration_checkpoint
**u
)
3133 int update_memory_size
= 0;
3135 dprintf("(enter)\n");
3141 /* size of all update data without anchor */
3142 update_memory_size
=
3143 sizeof(struct imsm_update_general_migration_checkpoint
);
3145 *u
= xcalloc(1, update_memory_size
);
3147 dprintf("error: cannot get memory\n");
3150 (*u
)->type
= update_general_migration_checkpoint
;
3151 (*u
)->curr_migr_unit
= current_migr_unit(super
->migr_rec
);
3152 dprintf("prepared for %u\n", (*u
)->curr_migr_unit
);
3154 return update_memory_size
;
3157 static void imsm_update_metadata_locally(struct supertype
*st
,
3158 void *buf
, int len
);
3160 /*******************************************************************************
3161 * Function: write_imsm_migr_rec
3162 * Description: Function writes imsm migration record
3163 * (at the last sector of disk)
3165 * super : imsm internal array info
3169 ******************************************************************************/
3170 static int write_imsm_migr_rec(struct supertype
*st
)
3172 struct intel_super
*super
= st
->sb
;
3173 unsigned int sector_size
= super
->sector_size
;
3174 unsigned long long dsize
;
3178 struct imsm_update_general_migration_checkpoint
*u
;
3179 struct imsm_dev
*dev
;
3180 struct imsm_map
*map
;
3182 /* find map under migration */
3183 dev
= imsm_get_device_during_migration(super
);
3184 /* if no migration, write buffer anyway to clear migr_record
3185 * on disk based on first available device
3188 dev
= get_imsm_dev(super
, super
->current_vol
< 0 ? 0 :
3189 super
->current_vol
);
3191 map
= get_imsm_map(dev
, MAP_0
);
3193 if (sector_size
== 4096)
3194 convert_to_4k_imsm_migr_rec(super
);
3195 for (sd
= super
->disks
; sd
; sd
= sd
->next
) {
3198 /* skip failed and spare devices */
3201 /* write to 2 first slots only */
3203 slot
= get_imsm_disk_slot(map
, sd
->index
);
3204 if (map
== NULL
|| slot
> 1 || slot
< 0)
3207 get_dev_size(sd
->fd
, NULL
, &dsize
);
3208 if (lseek64(sd
->fd
, dsize
- (MIGR_REC_SECTOR_POSITION
*
3211 pr_err("Cannot seek to anchor block: %s\n",
3215 if ((unsigned int)write(sd
->fd
, super
->migr_rec_buf
,
3216 MIGR_REC_BUF_SECTORS
*sector_size
) !=
3217 MIGR_REC_BUF_SECTORS
*sector_size
) {
3218 pr_err("Cannot write migr record block: %s\n",
3223 if (sector_size
== 4096)
3224 convert_from_4k_imsm_migr_rec(super
);
3225 /* update checkpoint information in metadata */
3226 len
= imsm_create_metadata_checkpoint_update(super
, &u
);
3228 dprintf("imsm: Cannot prepare update\n");
3231 /* update metadata locally */
3232 imsm_update_metadata_locally(st
, u
, len
);
3233 /* and possibly remotely */
3234 if (st
->update_tail
) {
3235 append_metadata_update(st
, u
, len
);
3236 /* during reshape we do all work inside metadata handler
3237 * manage_reshape(), so metadata update has to be triggered
3240 flush_metadata_updates(st
);
3241 st
->update_tail
= &st
->updates
;
3250 /* spare/missing disks activations are not allowe when
3251 * array/container performs reshape operation, because
3252 * all arrays in container works on the same disks set
3254 int imsm_reshape_blocks_arrays_changes(struct intel_super
*super
)
3257 struct intel_dev
*i_dev
;
3258 struct imsm_dev
*dev
;
3260 /* check whole container
3262 for (i_dev
= super
->devlist
; i_dev
; i_dev
= i_dev
->next
) {
3264 if (is_gen_migration(dev
)) {
3265 /* No repair during any migration in container
3273 static unsigned long long imsm_component_size_alignment_check(int level
,
3275 unsigned int sector_size
,
3276 unsigned long long component_size
)
3278 unsigned int component_size_alignment
;
3280 /* check component size alignment
3282 component_size_alignment
= component_size
% (chunk_size
/sector_size
);
3284 dprintf("(Level: %i, chunk_size = %i, component_size = %llu), component_size_alignment = %u\n",
3285 level
, chunk_size
, component_size
,
3286 component_size_alignment
);
3288 if (component_size_alignment
&& (level
!= 1) && (level
!= UnSet
)) {
3289 dprintf("imsm: reported component size aligned from %llu ",
3291 component_size
-= component_size_alignment
;
3292 dprintf_cont("to %llu (%i).\n",
3293 component_size
, component_size_alignment
);
3296 return component_size
;
3299 static unsigned long long get_ppl_sector(struct intel_super
*super
, int dev_idx
)
3301 struct imsm_dev
*dev
= get_imsm_dev(super
, dev_idx
);
3302 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
3304 return pba_of_lba0(map
) +
3305 (num_data_stripes(map
) * map
->blocks_per_strip
);
3308 static void getinfo_super_imsm_volume(struct supertype
*st
, struct mdinfo
*info
, char *dmap
)
3310 struct intel_super
*super
= st
->sb
;
3311 struct migr_record
*migr_rec
= super
->migr_rec
;
3312 struct imsm_dev
*dev
= get_imsm_dev(super
, super
->current_vol
);
3313 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
3314 struct imsm_map
*prev_map
= get_imsm_map(dev
, MAP_1
);
3315 struct imsm_map
*map_to_analyse
= map
;
3317 int map_disks
= info
->array
.raid_disks
;
3319 memset(info
, 0, sizeof(*info
));
3321 map_to_analyse
= prev_map
;
3323 dl
= super
->current_disk
;
3325 info
->container_member
= super
->current_vol
;
3326 info
->array
.raid_disks
= map
->num_members
;
3327 info
->array
.level
= get_imsm_raid_level(map_to_analyse
);
3328 info
->array
.layout
= imsm_level_to_layout(info
->array
.level
);
3329 info
->array
.md_minor
= -1;
3330 info
->array
.ctime
= 0;
3331 info
->array
.utime
= 0;
3332 info
->array
.chunk_size
=
3333 __le16_to_cpu(map_to_analyse
->blocks_per_strip
) << 9;
3334 info
->array
.state
= !(dev
->vol
.dirty
& RAIDVOL_DIRTY
);
3335 info
->custom_array_size
= imsm_dev_size(dev
);
3336 info
->recovery_blocked
= imsm_reshape_blocks_arrays_changes(st
->sb
);
3338 if (is_gen_migration(dev
)) {
3339 info
->reshape_active
= 1;
3340 info
->new_level
= get_imsm_raid_level(map
);
3341 info
->new_layout
= imsm_level_to_layout(info
->new_level
);
3342 info
->new_chunk
= __le16_to_cpu(map
->blocks_per_strip
) << 9;
3343 info
->delta_disks
= map
->num_members
- prev_map
->num_members
;
3344 if (info
->delta_disks
) {
3345 /* this needs to be applied to every array
3348 info
->reshape_active
= CONTAINER_RESHAPE
;
3350 /* We shape information that we give to md might have to be
3351 * modify to cope with md's requirement for reshaping arrays.
3352 * For example, when reshaping a RAID0, md requires it to be
3353 * presented as a degraded RAID4.
3354 * Also if a RAID0 is migrating to a RAID5 we need to specify
3355 * the array as already being RAID5, but the 'before' layout
3356 * is a RAID4-like layout.
3358 switch (info
->array
.level
) {
3360 switch(info
->new_level
) {
3362 /* conversion is happening as RAID4 */
3363 info
->array
.level
= 4;
3364 info
->array
.raid_disks
+= 1;
3367 /* conversion is happening as RAID5 */
3368 info
->array
.level
= 5;
3369 info
->array
.layout
= ALGORITHM_PARITY_N
;
3370 info
->delta_disks
-= 1;
3373 /* FIXME error message */
3374 info
->array
.level
= UnSet
;
3380 info
->new_level
= UnSet
;
3381 info
->new_layout
= UnSet
;
3382 info
->new_chunk
= info
->array
.chunk_size
;
3383 info
->delta_disks
= 0;
3387 info
->disk
.major
= dl
->major
;
3388 info
->disk
.minor
= dl
->minor
;
3389 info
->disk
.number
= dl
->index
;
3390 info
->disk
.raid_disk
= get_imsm_disk_slot(map_to_analyse
,
3394 info
->data_offset
= pba_of_lba0(map_to_analyse
);
3395 info
->component_size
= calc_component_size(map
, dev
);
3396 info
->component_size
= imsm_component_size_alignment_check(
3398 info
->array
.chunk_size
,
3400 info
->component_size
);
3401 info
->bb
.supported
= 1;
3403 memset(info
->uuid
, 0, sizeof(info
->uuid
));
3404 info
->recovery_start
= MaxSector
;
3406 if (info
->array
.level
== 5 &&
3407 (dev
->rwh_policy
== RWH_DISTRIBUTED
||
3408 dev
->rwh_policy
== RWH_MULTIPLE_DISTRIBUTED
)) {
3409 info
->consistency_policy
= CONSISTENCY_POLICY_PPL
;
3410 info
->ppl_sector
= get_ppl_sector(super
, super
->current_vol
);
3411 if (dev
->rwh_policy
== RWH_MULTIPLE_DISTRIBUTED
)
3412 info
->ppl_size
= MULTIPLE_PPL_AREA_SIZE_IMSM
>> 9;
3414 info
->ppl_size
= (PPL_HEADER_SIZE
+ PPL_ENTRY_SPACE
)
3416 } else if (info
->array
.level
<= 0) {
3417 info
->consistency_policy
= CONSISTENCY_POLICY_NONE
;
3419 info
->consistency_policy
= CONSISTENCY_POLICY_RESYNC
;
3422 info
->reshape_progress
= 0;
3423 info
->resync_start
= MaxSector
;
3424 if ((map_to_analyse
->map_state
== IMSM_T_STATE_UNINITIALIZED
||
3425 !(info
->array
.state
& 1)) &&
3426 imsm_reshape_blocks_arrays_changes(super
) == 0) {
3427 info
->resync_start
= 0;
3429 if (dev
->vol
.migr_state
) {
3430 switch (migr_type(dev
)) {
3433 __u64 blocks_per_unit
= blocks_per_migr_unit(super
,
3435 __u64 units
= __le32_to_cpu(dev
->vol
.curr_migr_unit
);
3437 info
->resync_start
= blocks_per_unit
* units
;
3440 case MIGR_GEN_MIGR
: {
3441 __u64 blocks_per_unit
= blocks_per_migr_unit(super
,
3443 __u64 units
= current_migr_unit(migr_rec
);
3446 if (__le32_to_cpu(migr_rec
->ascending_migr
) &&
3448 (get_num_migr_units(migr_rec
)-1)) &&
3449 (super
->migr_rec
->rec_status
==
3450 __cpu_to_le32(UNIT_SRC_IN_CP_AREA
)))
3453 info
->reshape_progress
= blocks_per_unit
* units
;
3455 dprintf("IMSM: General Migration checkpoint : %llu (%llu) -> read reshape progress : %llu\n",
3456 (unsigned long long)units
,
3457 (unsigned long long)blocks_per_unit
,
3458 info
->reshape_progress
);
3460 used_disks
= imsm_num_data_members(prev_map
);
3461 if (used_disks
> 0) {
3462 info
->custom_array_size
= per_dev_array_size(map
) *
3467 /* we could emulate the checkpointing of
3468 * 'sync_action=check' migrations, but for now
3469 * we just immediately complete them
3472 /* this is handled by container_content_imsm() */
3473 case MIGR_STATE_CHANGE
:
3474 /* FIXME handle other migrations */
3476 /* we are not dirty, so... */
3477 info
->resync_start
= MaxSector
;
3481 strncpy(info
->name
, (char *) dev
->volume
, MAX_RAID_SERIAL_LEN
);
3482 info
->name
[MAX_RAID_SERIAL_LEN
] = 0;
3484 info
->array
.major_version
= -1;
3485 info
->array
.minor_version
= -2;
3486 sprintf(info
->text_version
, "/%s/%d", st
->container_devnm
, info
->container_member
);
3487 info
->safe_mode_delay
= 4000; /* 4 secs like the Matrix driver */
3488 uuid_from_super_imsm(st
, info
->uuid
);
3492 for (i
=0; i
<map_disks
; i
++) {
3494 if (i
< info
->array
.raid_disks
) {
3495 struct imsm_disk
*dsk
;
3496 j
= get_imsm_disk_idx(dev
, i
, MAP_X
);
3497 dsk
= get_imsm_disk(super
, j
);
3498 if (dsk
&& (dsk
->status
& CONFIGURED_DISK
))
3505 static __u8
imsm_check_degraded(struct intel_super
*super
, struct imsm_dev
*dev
,
3506 int failed
, int look_in_map
);
3508 static int imsm_count_failed(struct intel_super
*super
, struct imsm_dev
*dev
,
3511 static void manage_second_map(struct intel_super
*super
, struct imsm_dev
*dev
)
3513 if (is_gen_migration(dev
)) {
3516 struct imsm_map
*map2
= get_imsm_map(dev
, MAP_1
);
3518 failed
= imsm_count_failed(super
, dev
, MAP_1
);
3519 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_1
);
3520 if (map2
->map_state
!= map_state
) {
3521 map2
->map_state
= map_state
;
3522 super
->updates_pending
++;
3527 static struct imsm_disk
*get_imsm_missing(struct intel_super
*super
, __u8 index
)
3531 for (d
= super
->missing
; d
; d
= d
->next
)
3532 if (d
->index
== index
)
3537 static void getinfo_super_imsm(struct supertype
*st
, struct mdinfo
*info
, char *map
)
3539 struct intel_super
*super
= st
->sb
;
3540 struct imsm_disk
*disk
;
3541 int map_disks
= info
->array
.raid_disks
;
3542 int max_enough
= -1;
3544 struct imsm_super
*mpb
;
3546 if (super
->current_vol
>= 0) {
3547 getinfo_super_imsm_volume(st
, info
, map
);
3550 memset(info
, 0, sizeof(*info
));
3552 /* Set raid_disks to zero so that Assemble will always pull in valid
3555 info
->array
.raid_disks
= 0;
3556 info
->array
.level
= LEVEL_CONTAINER
;
3557 info
->array
.layout
= 0;
3558 info
->array
.md_minor
= -1;
3559 info
->array
.ctime
= 0; /* N/A for imsm */
3560 info
->array
.utime
= 0;
3561 info
->array
.chunk_size
= 0;
3563 info
->disk
.major
= 0;
3564 info
->disk
.minor
= 0;
3565 info
->disk
.raid_disk
= -1;
3566 info
->reshape_active
= 0;
3567 info
->array
.major_version
= -1;
3568 info
->array
.minor_version
= -2;
3569 strcpy(info
->text_version
, "imsm");
3570 info
->safe_mode_delay
= 0;
3571 info
->disk
.number
= -1;
3572 info
->disk
.state
= 0;
3574 info
->recovery_start
= MaxSector
;
3575 info
->recovery_blocked
= imsm_reshape_blocks_arrays_changes(st
->sb
);
3576 info
->bb
.supported
= 1;
3578 /* do we have the all the insync disks that we expect? */
3579 mpb
= super
->anchor
;
3580 info
->events
= __le32_to_cpu(mpb
->generation_num
);
3582 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
3583 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
3584 int failed
, enough
, j
, missing
= 0;
3585 struct imsm_map
*map
;
3588 failed
= imsm_count_failed(super
, dev
, MAP_0
);
3589 state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
3590 map
= get_imsm_map(dev
, MAP_0
);
3592 /* any newly missing disks?
3593 * (catches single-degraded vs double-degraded)
3595 for (j
= 0; j
< map
->num_members
; j
++) {
3596 __u32 ord
= get_imsm_ord_tbl_ent(dev
, j
, MAP_0
);
3597 __u32 idx
= ord_to_idx(ord
);
3599 if (super
->disks
&& super
->disks
->index
== (int)idx
)
3600 info
->disk
.raid_disk
= j
;
3602 if (!(ord
& IMSM_ORD_REBUILD
) &&
3603 get_imsm_missing(super
, idx
)) {
3609 if (state
== IMSM_T_STATE_FAILED
)
3611 else if (state
== IMSM_T_STATE_DEGRADED
&&
3612 (state
!= map
->map_state
|| missing
))
3614 else /* we're normal, or already degraded */
3616 if (is_gen_migration(dev
) && missing
) {
3617 /* during general migration we need all disks
3618 * that process is running on.
3619 * No new missing disk is allowed.
3623 /* no more checks necessary
3627 /* in the missing/failed disk case check to see
3628 * if at least one array is runnable
3630 max_enough
= max(max_enough
, enough
);
3632 dprintf("enough: %d\n", max_enough
);
3633 info
->container_enough
= max_enough
;
3636 __u32 reserved
= imsm_reserved_sectors(super
, super
->disks
);
3638 disk
= &super
->disks
->disk
;
3639 info
->data_offset
= total_blocks(&super
->disks
->disk
) - reserved
;
3640 info
->component_size
= reserved
;
3641 info
->disk
.state
= is_configured(disk
) ? (1 << MD_DISK_ACTIVE
) : 0;
3642 /* we don't change info->disk.raid_disk here because
3643 * this state will be finalized in mdmon after we have
3644 * found the 'most fresh' version of the metadata
3646 info
->disk
.state
|= is_failed(disk
) ? (1 << MD_DISK_FAULTY
) : 0;
3647 info
->disk
.state
|= (is_spare(disk
) || is_journal(disk
)) ?
3648 0 : (1 << MD_DISK_SYNC
);
3651 /* only call uuid_from_super_imsm when this disk is part of a populated container,
3652 * ->compare_super may have updated the 'num_raid_devs' field for spares
3654 if (info
->disk
.state
& (1 << MD_DISK_SYNC
) || super
->anchor
->num_raid_devs
)
3655 uuid_from_super_imsm(st
, info
->uuid
);
3657 memcpy(info
->uuid
, uuid_zero
, sizeof(uuid_zero
));
3659 /* I don't know how to compute 'map' on imsm, so use safe default */
3662 for (i
= 0; i
< map_disks
; i
++)
3668 /* allocates memory and fills disk in mdinfo structure
3669 * for each disk in array */
3670 struct mdinfo
*getinfo_super_disks_imsm(struct supertype
*st
)
3672 struct mdinfo
*mddev
;
3673 struct intel_super
*super
= st
->sb
;
3674 struct imsm_disk
*disk
;
3677 if (!super
|| !super
->disks
)
3680 mddev
= xcalloc(1, sizeof(*mddev
));
3684 tmp
= xcalloc(1, sizeof(*tmp
));
3686 tmp
->next
= mddev
->devs
;
3688 tmp
->disk
.number
= count
++;
3689 tmp
->disk
.major
= dl
->major
;
3690 tmp
->disk
.minor
= dl
->minor
;
3691 tmp
->disk
.state
= is_configured(disk
) ?
3692 (1 << MD_DISK_ACTIVE
) : 0;
3693 tmp
->disk
.state
|= is_failed(disk
) ? (1 << MD_DISK_FAULTY
) : 0;
3694 tmp
->disk
.state
|= is_spare(disk
) ? 0 : (1 << MD_DISK_SYNC
);
3695 tmp
->disk
.raid_disk
= -1;
3701 static int update_super_imsm(struct supertype
*st
, struct mdinfo
*info
,
3702 char *update
, char *devname
, int verbose
,
3703 int uuid_set
, char *homehost
)
3705 /* For 'assemble' and 'force' we need to return non-zero if any
3706 * change was made. For others, the return value is ignored.
3707 * Update options are:
3708 * force-one : This device looks a bit old but needs to be included,
3709 * update age info appropriately.
3710 * assemble: clear any 'faulty' flag to allow this device to
3712 * force-array: Array is degraded but being forced, mark it clean
3713 * if that will be needed to assemble it.
3715 * newdev: not used ????
3716 * grow: Array has gained a new device - this is currently for
3718 * resync: mark as dirty so a resync will happen.
3719 * name: update the name - preserving the homehost
3720 * uuid: Change the uuid of the array to match watch is given
3722 * Following are not relevant for this imsm:
3723 * sparc2.2 : update from old dodgey metadata
3724 * super-minor: change the preferred_minor number
3725 * summaries: update redundant counters.
3726 * homehost: update the recorded homehost
3727 * _reshape_progress: record new reshape_progress position.
3730 struct intel_super
*super
= st
->sb
;
3731 struct imsm_super
*mpb
;
3733 /* we can only update container info */
3734 if (!super
|| super
->current_vol
>= 0 || !super
->anchor
)
3737 mpb
= super
->anchor
;
3739 if (strcmp(update
, "uuid") == 0) {
3740 /* We take this to mean that the family_num should be updated.
3741 * However that is much smaller than the uuid so we cannot really
3742 * allow an explicit uuid to be given. And it is hard to reliably
3744 * So if !uuid_set we know the current uuid is random and just used
3745 * the first 'int' and copy it to the other 3 positions.
3746 * Otherwise we require the 4 'int's to be the same as would be the
3747 * case if we are using a random uuid. So an explicit uuid will be
3748 * accepted as long as all for ints are the same... which shouldn't hurt
3751 info
->uuid
[1] = info
->uuid
[2] = info
->uuid
[3] = info
->uuid
[0];
3754 if (info
->uuid
[0] != info
->uuid
[1] ||
3755 info
->uuid
[1] != info
->uuid
[2] ||
3756 info
->uuid
[2] != info
->uuid
[3])
3762 mpb
->orig_family_num
= info
->uuid
[0];
3763 } else if (strcmp(update
, "assemble") == 0)
3768 /* successful update? recompute checksum */
3770 mpb
->check_sum
= __le32_to_cpu(__gen_imsm_checksum(mpb
));
3775 static size_t disks_to_mpb_size(int disks
)
3779 size
= sizeof(struct imsm_super
);
3780 size
+= (disks
- 1) * sizeof(struct imsm_disk
);
3781 size
+= 2 * sizeof(struct imsm_dev
);
3782 /* up to 2 maps per raid device (-2 for imsm_maps in imsm_dev */
3783 size
+= (4 - 2) * sizeof(struct imsm_map
);
3784 /* 4 possible disk_ord_tbl's */
3785 size
+= 4 * (disks
- 1) * sizeof(__u32
);
3786 /* maximum bbm log */
3787 size
+= sizeof(struct bbm_log
);
3792 static __u64
avail_size_imsm(struct supertype
*st
, __u64 devsize
,
3793 unsigned long long data_offset
)
3795 if (devsize
< (MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
))
3798 return devsize
- (MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
);
3801 static void free_devlist(struct intel_super
*super
)
3803 struct intel_dev
*dv
;
3805 while (super
->devlist
) {
3806 dv
= super
->devlist
->next
;
3807 free(super
->devlist
->dev
);
3808 free(super
->devlist
);
3809 super
->devlist
= dv
;
3813 static void imsm_copy_dev(struct imsm_dev
*dest
, struct imsm_dev
*src
)
3815 memcpy(dest
, src
, sizeof_imsm_dev(src
, 0));
3818 static int compare_super_imsm(struct supertype
*st
, struct supertype
*tst
)
3822 * 0 same, or first was empty, and second was copied
3823 * 1 second had wrong number
3825 * 3 wrong other info
3827 struct intel_super
*first
= st
->sb
;
3828 struct intel_super
*sec
= tst
->sb
;
3835 /* in platform dependent environment test if the disks
3836 * use the same Intel hba
3837 * If not on Intel hba at all, allow anything.
3839 if (!check_env("IMSM_NO_PLATFORM") && first
->hba
&& sec
->hba
) {
3840 if (first
->hba
->type
!= sec
->hba
->type
) {
3842 "HBAs of devices do not match %s != %s\n",
3843 get_sys_dev_type(first
->hba
->type
),
3844 get_sys_dev_type(sec
->hba
->type
));
3847 if (first
->orom
!= sec
->orom
) {
3849 "HBAs of devices do not match %s != %s\n",
3850 first
->hba
->pci_id
, sec
->hba
->pci_id
);
3855 /* if an anchor does not have num_raid_devs set then it is a free
3858 if (first
->anchor
->num_raid_devs
> 0 &&
3859 sec
->anchor
->num_raid_devs
> 0) {
3860 /* Determine if these disks might ever have been
3861 * related. Further disambiguation can only take place
3862 * in load_super_imsm_all
3864 __u32 first_family
= first
->anchor
->orig_family_num
;
3865 __u32 sec_family
= sec
->anchor
->orig_family_num
;
3867 if (memcmp(first
->anchor
->sig
, sec
->anchor
->sig
,
3868 MAX_SIGNATURE_LENGTH
) != 0)
3871 if (first_family
== 0)
3872 first_family
= first
->anchor
->family_num
;
3873 if (sec_family
== 0)
3874 sec_family
= sec
->anchor
->family_num
;
3876 if (first_family
!= sec_family
)
3881 /* if 'first' is a spare promote it to a populated mpb with sec's
3884 if (first
->anchor
->num_raid_devs
== 0 &&
3885 sec
->anchor
->num_raid_devs
> 0) {
3887 struct intel_dev
*dv
;
3888 struct imsm_dev
*dev
;
3890 /* we need to copy raid device info from sec if an allocation
3891 * fails here we don't associate the spare
3893 for (i
= 0; i
< sec
->anchor
->num_raid_devs
; i
++) {
3894 dv
= xmalloc(sizeof(*dv
));
3895 dev
= xmalloc(sizeof_imsm_dev(get_imsm_dev(sec
, i
), 1));
3898 dv
->next
= first
->devlist
;
3899 first
->devlist
= dv
;
3901 if (i
< sec
->anchor
->num_raid_devs
) {
3902 /* allocation failure */
3903 free_devlist(first
);
3904 pr_err("imsm: failed to associate spare\n");
3907 first
->anchor
->num_raid_devs
= sec
->anchor
->num_raid_devs
;
3908 first
->anchor
->orig_family_num
= sec
->anchor
->orig_family_num
;
3909 first
->anchor
->family_num
= sec
->anchor
->family_num
;
3910 memcpy(first
->anchor
->sig
, sec
->anchor
->sig
, MAX_SIGNATURE_LENGTH
);
3911 for (i
= 0; i
< sec
->anchor
->num_raid_devs
; i
++)
3912 imsm_copy_dev(get_imsm_dev(first
, i
), get_imsm_dev(sec
, i
));
3918 static void fd2devname(int fd
, char *name
)
3922 char dname
[PATH_MAX
];
3927 if (fstat(fd
, &st
) != 0)
3929 sprintf(path
, "/sys/dev/block/%d:%d",
3930 major(st
.st_rdev
), minor(st
.st_rdev
));
3932 rv
= readlink(path
, dname
, sizeof(dname
)-1);
3937 nm
= strrchr(dname
, '/');
3940 snprintf(name
, MAX_RAID_SERIAL_LEN
, "/dev/%s", nm
);
3944 static int nvme_get_serial(int fd
, void *buf
, size_t buf_len
)
3947 char *name
= fd2kname(fd
);
3952 if (strncmp(name
, "nvme", 4) != 0)
3955 snprintf(path
, sizeof(path
) - 1, "/sys/block/%s/device/serial", name
);
3957 return load_sys(path
, buf
, buf_len
);
3960 extern int scsi_get_serial(int fd
, void *buf
, size_t buf_len
);
3962 static int imsm_read_serial(int fd
, char *devname
,
3963 __u8
*serial
, size_t serial_buf_len
)
3972 memset(buf
, 0, sizeof(buf
));
3974 rv
= nvme_get_serial(fd
, buf
, sizeof(buf
));
3977 rv
= scsi_get_serial(fd
, buf
, sizeof(buf
));
3979 if (rv
&& check_env("IMSM_DEVNAME_AS_SERIAL")) {
3980 memset(serial
, 0, MAX_RAID_SERIAL_LEN
);
3981 fd2devname(fd
, (char *) serial
);
3987 pr_err("Failed to retrieve serial for %s\n",
3992 /* trim all whitespace and non-printable characters and convert
3995 for (i
= 0, dest
= buf
; i
< sizeof(buf
) && buf
[i
]; i
++) {
3998 /* ':' is reserved for use in placeholder serial
3999 * numbers for missing disks
4010 if (len
> serial_buf_len
) {
4011 /* truncate leading characters */
4012 dest
+= len
- serial_buf_len
;
4013 len
= serial_buf_len
;
4016 memset(serial
, 0, serial_buf_len
);
4017 memcpy(serial
, dest
, len
);
4022 static int serialcmp(__u8
*s1
, __u8
*s2
)
4024 return strncmp((char *) s1
, (char *) s2
, MAX_RAID_SERIAL_LEN
);
4027 static void serialcpy(__u8
*dest
, __u8
*src
)
4029 strncpy((char *) dest
, (char *) src
, MAX_RAID_SERIAL_LEN
);
4032 static struct dl
*serial_to_dl(__u8
*serial
, struct intel_super
*super
)
4036 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
4037 if (serialcmp(dl
->serial
, serial
) == 0)
4043 static struct imsm_disk
*
4044 __serial_to_disk(__u8
*serial
, struct imsm_super
*mpb
, int *idx
)
4048 for (i
= 0; i
< mpb
->num_disks
; i
++) {
4049 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, i
);
4051 if (serialcmp(disk
->serial
, serial
) == 0) {
4062 load_imsm_disk(int fd
, struct intel_super
*super
, char *devname
, int keep_fd
)
4064 struct imsm_disk
*disk
;
4069 __u8 serial
[MAX_RAID_SERIAL_LEN
];
4071 rv
= imsm_read_serial(fd
, devname
, serial
, MAX_RAID_SERIAL_LEN
);
4076 dl
= xcalloc(1, sizeof(*dl
));
4079 dl
->major
= major(stb
.st_rdev
);
4080 dl
->minor
= minor(stb
.st_rdev
);
4081 dl
->next
= super
->disks
;
4082 dl
->fd
= keep_fd
? fd
: -1;
4083 assert(super
->disks
== NULL
);
4085 serialcpy(dl
->serial
, serial
);
4088 fd2devname(fd
, name
);
4090 dl
->devname
= xstrdup(devname
);
4092 dl
->devname
= xstrdup(name
);
4094 /* look up this disk's index in the current anchor */
4095 disk
= __serial_to_disk(dl
->serial
, super
->anchor
, &dl
->index
);
4098 /* only set index on disks that are a member of a
4099 * populated contianer, i.e. one with raid_devs
4101 if (is_failed(&dl
->disk
))
4103 else if (is_spare(&dl
->disk
) || is_journal(&dl
->disk
))
4110 /* When migrating map0 contains the 'destination' state while map1
4111 * contains the current state. When not migrating map0 contains the
4112 * current state. This routine assumes that map[0].map_state is set to
4113 * the current array state before being called.
4115 * Migration is indicated by one of the following states
4116 * 1/ Idle (migr_state=0 map0state=normal||unitialized||degraded||failed)
4117 * 2/ Initialize (migr_state=1 migr_type=MIGR_INIT map0state=normal
4118 * map1state=unitialized)
4119 * 3/ Repair (Resync) (migr_state=1 migr_type=MIGR_REPAIR map0state=normal
4121 * 4/ Rebuild (migr_state=1 migr_type=MIGR_REBUILD map0state=normal
4122 * map1state=degraded)
4123 * 5/ Migration (mig_state=1 migr_type=MIGR_GEN_MIGR map0state=normal
4126 static void migrate(struct imsm_dev
*dev
, struct intel_super
*super
,
4127 __u8 to_state
, int migr_type
)
4129 struct imsm_map
*dest
;
4130 struct imsm_map
*src
= get_imsm_map(dev
, MAP_0
);
4132 dev
->vol
.migr_state
= 1;
4133 set_migr_type(dev
, migr_type
);
4134 dev
->vol
.curr_migr_unit
= 0;
4135 dest
= get_imsm_map(dev
, MAP_1
);
4137 /* duplicate and then set the target end state in map[0] */
4138 memcpy(dest
, src
, sizeof_imsm_map(src
));
4139 if (migr_type
== MIGR_GEN_MIGR
) {
4143 for (i
= 0; i
< src
->num_members
; i
++) {
4144 ord
= __le32_to_cpu(src
->disk_ord_tbl
[i
]);
4145 set_imsm_ord_tbl_ent(src
, i
, ord_to_idx(ord
));
4149 if (migr_type
== MIGR_GEN_MIGR
)
4150 /* Clear migration record */
4151 memset(super
->migr_rec
, 0, sizeof(struct migr_record
));
4153 src
->map_state
= to_state
;
4156 static void end_migration(struct imsm_dev
*dev
, struct intel_super
*super
,
4159 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
4160 struct imsm_map
*prev
= get_imsm_map(dev
, dev
->vol
.migr_state
== 0 ?
4164 /* merge any IMSM_ORD_REBUILD bits that were not successfully
4165 * completed in the last migration.
4167 * FIXME add support for raid-level-migration
4169 if (map_state
!= map
->map_state
&& (is_gen_migration(dev
) == 0) &&
4170 prev
->map_state
!= IMSM_T_STATE_UNINITIALIZED
) {
4171 /* when final map state is other than expected
4172 * merge maps (not for migration)
4176 for (i
= 0; i
< prev
->num_members
; i
++)
4177 for (j
= 0; j
< map
->num_members
; j
++)
4178 /* during online capacity expansion
4179 * disks position can be changed
4180 * if takeover is used
4182 if (ord_to_idx(map
->disk_ord_tbl
[j
]) ==
4183 ord_to_idx(prev
->disk_ord_tbl
[i
])) {
4184 map
->disk_ord_tbl
[j
] |=
4185 prev
->disk_ord_tbl
[i
];
4188 failed
= imsm_count_failed(super
, dev
, MAP_0
);
4189 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
4192 dev
->vol
.migr_state
= 0;
4193 set_migr_type(dev
, 0);
4194 dev
->vol
.curr_migr_unit
= 0;
4195 map
->map_state
= map_state
;
4198 static int parse_raid_devices(struct intel_super
*super
)
4201 struct imsm_dev
*dev_new
;
4202 size_t len
, len_migr
;
4204 size_t space_needed
= 0;
4205 struct imsm_super
*mpb
= super
->anchor
;
4207 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
4208 struct imsm_dev
*dev_iter
= __get_imsm_dev(super
->anchor
, i
);
4209 struct intel_dev
*dv
;
4211 len
= sizeof_imsm_dev(dev_iter
, 0);
4212 len_migr
= sizeof_imsm_dev(dev_iter
, 1);
4214 space_needed
+= len_migr
- len
;
4216 dv
= xmalloc(sizeof(*dv
));
4217 if (max_len
< len_migr
)
4219 if (max_len
> len_migr
)
4220 space_needed
+= max_len
- len_migr
;
4221 dev_new
= xmalloc(max_len
);
4222 imsm_copy_dev(dev_new
, dev_iter
);
4225 dv
->next
= super
->devlist
;
4226 super
->devlist
= dv
;
4229 /* ensure that super->buf is large enough when all raid devices
4232 if (__le32_to_cpu(mpb
->mpb_size
) + space_needed
> super
->len
) {
4235 len
= ROUND_UP(__le32_to_cpu(mpb
->mpb_size
) + space_needed
,
4236 super
->sector_size
);
4237 if (posix_memalign(&buf
, MAX_SECTOR_SIZE
, len
) != 0)
4240 memcpy(buf
, super
->buf
, super
->len
);
4241 memset(buf
+ super
->len
, 0, len
- super
->len
);
4247 super
->extra_space
+= space_needed
;
4252 /*******************************************************************************
4253 * Function: check_mpb_migr_compatibility
4254 * Description: Function checks for unsupported migration features:
4255 * - migration optimization area (pba_of_lba0)
4256 * - descending reshape (ascending_migr)
4258 * super : imsm metadata information
4260 * 0 : migration is compatible
4261 * -1 : migration is not compatible
4262 ******************************************************************************/
4263 int check_mpb_migr_compatibility(struct intel_super
*super
)
4265 struct imsm_map
*map0
, *map1
;
4266 struct migr_record
*migr_rec
= super
->migr_rec
;
4269 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
4270 struct imsm_dev
*dev_iter
= __get_imsm_dev(super
->anchor
, i
);
4273 dev_iter
->vol
.migr_state
== 1 &&
4274 dev_iter
->vol
.migr_type
== MIGR_GEN_MIGR
) {
4275 /* This device is migrating */
4276 map0
= get_imsm_map(dev_iter
, MAP_0
);
4277 map1
= get_imsm_map(dev_iter
, MAP_1
);
4278 if (pba_of_lba0(map0
) != pba_of_lba0(map1
))
4279 /* migration optimization area was used */
4281 if (migr_rec
->ascending_migr
== 0 &&
4282 migr_rec
->dest_depth_per_unit
> 0)
4283 /* descending reshape not supported yet */
4290 static void __free_imsm(struct intel_super
*super
, int free_disks
);
4292 /* load_imsm_mpb - read matrix metadata
4293 * allocates super->mpb to be freed by free_imsm
4295 static int load_imsm_mpb(int fd
, struct intel_super
*super
, char *devname
)
4297 unsigned long long dsize
;
4298 unsigned long long sectors
;
4299 unsigned int sector_size
= super
->sector_size
;
4301 struct imsm_super
*anchor
;
4304 get_dev_size(fd
, NULL
, &dsize
);
4305 if (dsize
< 2*sector_size
) {
4307 pr_err("%s: device to small for imsm\n",
4312 if (lseek64(fd
, dsize
- (sector_size
* 2), SEEK_SET
) < 0) {
4314 pr_err("Cannot seek to anchor block on %s: %s\n",
4315 devname
, strerror(errno
));
4319 if (posix_memalign((void **)&anchor
, sector_size
, sector_size
) != 0) {
4321 pr_err("Failed to allocate imsm anchor buffer on %s\n", devname
);
4324 if ((unsigned int)read(fd
, anchor
, sector_size
) != sector_size
) {
4326 pr_err("Cannot read anchor block on %s: %s\n",
4327 devname
, strerror(errno
));
4332 if (strncmp((char *) anchor
->sig
, MPB_SIGNATURE
, MPB_SIG_LEN
) != 0) {
4334 pr_err("no IMSM anchor on %s\n", devname
);
4339 __free_imsm(super
, 0);
4340 /* reload capability and hba */
4342 /* capability and hba must be updated with new super allocation */
4343 find_intel_hba_capability(fd
, super
, devname
);
4344 super
->len
= ROUND_UP(anchor
->mpb_size
, sector_size
);
4345 if (posix_memalign(&super
->buf
, MAX_SECTOR_SIZE
, super
->len
) != 0) {
4347 pr_err("unable to allocate %zu byte mpb buffer\n",
4352 memcpy(super
->buf
, anchor
, sector_size
);
4354 sectors
= mpb_sectors(anchor
, sector_size
) - 1;
4357 if (posix_memalign(&super
->migr_rec_buf
, MAX_SECTOR_SIZE
,
4358 MIGR_REC_BUF_SECTORS
*MAX_SECTOR_SIZE
) != 0) {
4359 pr_err("could not allocate migr_rec buffer\n");
4363 super
->clean_migration_record_by_mdmon
= 0;
4366 check_sum
= __gen_imsm_checksum(super
->anchor
);
4367 if (check_sum
!= __le32_to_cpu(super
->anchor
->check_sum
)) {
4369 pr_err("IMSM checksum %x != %x on %s\n",
4371 __le32_to_cpu(super
->anchor
->check_sum
),
4379 /* read the extended mpb */
4380 if (lseek64(fd
, dsize
- (sector_size
* (2 + sectors
)), SEEK_SET
) < 0) {
4382 pr_err("Cannot seek to extended mpb on %s: %s\n",
4383 devname
, strerror(errno
));
4387 if ((unsigned int)read(fd
, super
->buf
+ sector_size
,
4388 super
->len
- sector_size
) != super
->len
- sector_size
) {
4390 pr_err("Cannot read extended mpb on %s: %s\n",
4391 devname
, strerror(errno
));
4395 check_sum
= __gen_imsm_checksum(super
->anchor
);
4396 if (check_sum
!= __le32_to_cpu(super
->anchor
->check_sum
)) {
4398 pr_err("IMSM checksum %x != %x on %s\n",
4399 check_sum
, __le32_to_cpu(super
->anchor
->check_sum
),
4407 static int read_imsm_migr_rec(int fd
, struct intel_super
*super
);
4409 /* clears hi bits in metadata if MPB_ATTRIB_2TB_DISK not set */
4410 static void clear_hi(struct intel_super
*super
)
4412 struct imsm_super
*mpb
= super
->anchor
;
4414 if (mpb
->attributes
& MPB_ATTRIB_2TB_DISK
)
4416 for (i
= 0; i
< mpb
->num_disks
; ++i
) {
4417 struct imsm_disk
*disk
= &mpb
->disk
[i
];
4418 disk
->total_blocks_hi
= 0;
4420 for (i
= 0; i
< mpb
->num_raid_devs
; ++i
) {
4421 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
4424 for (n
= 0; n
< 2; ++n
) {
4425 struct imsm_map
*map
= get_imsm_map(dev
, n
);
4428 map
->pba_of_lba0_hi
= 0;
4429 map
->blocks_per_member_hi
= 0;
4430 map
->num_data_stripes_hi
= 0;
4436 load_and_parse_mpb(int fd
, struct intel_super
*super
, char *devname
, int keep_fd
)
4440 err
= load_imsm_mpb(fd
, super
, devname
);
4443 if (super
->sector_size
== 4096)
4444 convert_from_4k(super
);
4445 err
= load_imsm_disk(fd
, super
, devname
, keep_fd
);
4448 err
= parse_raid_devices(super
);
4451 err
= load_bbm_log(super
);
4456 static void __free_imsm_disk(struct dl
*d
)
4468 static void free_imsm_disks(struct intel_super
*super
)
4472 while (super
->disks
) {
4474 super
->disks
= d
->next
;
4475 __free_imsm_disk(d
);
4477 while (super
->disk_mgmt_list
) {
4478 d
= super
->disk_mgmt_list
;
4479 super
->disk_mgmt_list
= d
->next
;
4480 __free_imsm_disk(d
);
4482 while (super
->missing
) {
4484 super
->missing
= d
->next
;
4485 __free_imsm_disk(d
);
4490 /* free all the pieces hanging off of a super pointer */
4491 static void __free_imsm(struct intel_super
*super
, int free_disks
)
4493 struct intel_hba
*elem
, *next
;
4499 /* unlink capability description */
4501 if (super
->migr_rec_buf
) {
4502 free(super
->migr_rec_buf
);
4503 super
->migr_rec_buf
= NULL
;
4506 free_imsm_disks(super
);
4507 free_devlist(super
);
4511 free((void *)elem
->path
);
4517 free(super
->bbm_log
);
4521 static void free_imsm(struct intel_super
*super
)
4523 __free_imsm(super
, 1);
4524 free(super
->bb
.entries
);
4528 static void free_super_imsm(struct supertype
*st
)
4530 struct intel_super
*super
= st
->sb
;
4539 static struct intel_super
*alloc_super(void)
4541 struct intel_super
*super
= xcalloc(1, sizeof(*super
));
4543 super
->current_vol
= -1;
4544 super
->create_offset
= ~((unsigned long long) 0);
4546 super
->bb
.entries
= xmalloc(BBM_LOG_MAX_ENTRIES
*
4547 sizeof(struct md_bb_entry
));
4548 if (!super
->bb
.entries
) {
4557 * find and allocate hba and OROM/EFI based on valid fd of RAID component device
4559 static int find_intel_hba_capability(int fd
, struct intel_super
*super
, char *devname
)
4561 struct sys_dev
*hba_name
;
4564 if (fd
>= 0 && test_partition(fd
)) {
4565 pr_err("imsm: %s is a partition, cannot be used in IMSM\n",
4569 if (fd
< 0 || check_env("IMSM_NO_PLATFORM")) {
4574 hba_name
= find_disk_attached_hba(fd
, NULL
);
4577 pr_err("%s is not attached to Intel(R) RAID controller.\n",
4581 rv
= attach_hba_to_super(super
, hba_name
);
4584 struct intel_hba
*hba
= super
->hba
;
4586 pr_err("%s is attached to Intel(R) %s %s (%s),\n"
4587 " but the container is assigned to Intel(R) %s %s (",
4589 get_sys_dev_type(hba_name
->type
),
4590 hba_name
->type
== SYS_DEV_VMD
? "domain" : "RAID controller",
4591 hba_name
->pci_id
? : "Err!",
4592 get_sys_dev_type(super
->hba
->type
),
4593 hba
->type
== SYS_DEV_VMD
? "domain" : "RAID controller");
4596 fprintf(stderr
, "%s", hba
->pci_id
? : "Err!");
4598 fprintf(stderr
, ", ");
4601 fprintf(stderr
, ").\n"
4602 " Mixing devices attached to different controllers is not allowed.\n");
4606 super
->orom
= find_imsm_capability(hba_name
);
4613 /* find_missing - helper routine for load_super_imsm_all that identifies
4614 * disks that have disappeared from the system. This routine relies on
4615 * the mpb being uptodate, which it is at load time.
4617 static int find_missing(struct intel_super
*super
)
4620 struct imsm_super
*mpb
= super
->anchor
;
4622 struct imsm_disk
*disk
;
4624 for (i
= 0; i
< mpb
->num_disks
; i
++) {
4625 disk
= __get_imsm_disk(mpb
, i
);
4626 dl
= serial_to_dl(disk
->serial
, super
);
4630 dl
= xmalloc(sizeof(*dl
));
4634 dl
->devname
= xstrdup("missing");
4636 serialcpy(dl
->serial
, disk
->serial
);
4639 dl
->next
= super
->missing
;
4640 super
->missing
= dl
;
4646 static struct intel_disk
*disk_list_get(__u8
*serial
, struct intel_disk
*disk_list
)
4648 struct intel_disk
*idisk
= disk_list
;
4651 if (serialcmp(idisk
->disk
.serial
, serial
) == 0)
4653 idisk
= idisk
->next
;
4659 static int __prep_thunderdome(struct intel_super
**table
, int tbl_size
,
4660 struct intel_super
*super
,
4661 struct intel_disk
**disk_list
)
4663 struct imsm_disk
*d
= &super
->disks
->disk
;
4664 struct imsm_super
*mpb
= super
->anchor
;
4667 for (i
= 0; i
< tbl_size
; i
++) {
4668 struct imsm_super
*tbl_mpb
= table
[i
]->anchor
;
4669 struct imsm_disk
*tbl_d
= &table
[i
]->disks
->disk
;
4671 if (tbl_mpb
->family_num
== mpb
->family_num
) {
4672 if (tbl_mpb
->check_sum
== mpb
->check_sum
) {
4673 dprintf("mpb from %d:%d matches %d:%d\n",
4674 super
->disks
->major
,
4675 super
->disks
->minor
,
4676 table
[i
]->disks
->major
,
4677 table
[i
]->disks
->minor
);
4681 if (((is_configured(d
) && !is_configured(tbl_d
)) ||
4682 is_configured(d
) == is_configured(tbl_d
)) &&
4683 tbl_mpb
->generation_num
< mpb
->generation_num
) {
4684 /* current version of the mpb is a
4685 * better candidate than the one in
4686 * super_table, but copy over "cross
4687 * generational" status
4689 struct intel_disk
*idisk
;
4691 dprintf("mpb from %d:%d replaces %d:%d\n",
4692 super
->disks
->major
,
4693 super
->disks
->minor
,
4694 table
[i
]->disks
->major
,
4695 table
[i
]->disks
->minor
);
4697 idisk
= disk_list_get(tbl_d
->serial
, *disk_list
);
4698 if (idisk
&& is_failed(&idisk
->disk
))
4699 tbl_d
->status
|= FAILED_DISK
;
4702 struct intel_disk
*idisk
;
4703 struct imsm_disk
*disk
;
4705 /* tbl_mpb is more up to date, but copy
4706 * over cross generational status before
4709 disk
= __serial_to_disk(d
->serial
, mpb
, NULL
);
4710 if (disk
&& is_failed(disk
))
4711 d
->status
|= FAILED_DISK
;
4713 idisk
= disk_list_get(d
->serial
, *disk_list
);
4716 if (disk
&& is_configured(disk
))
4717 idisk
->disk
.status
|= CONFIGURED_DISK
;
4720 dprintf("mpb from %d:%d prefer %d:%d\n",
4721 super
->disks
->major
,
4722 super
->disks
->minor
,
4723 table
[i
]->disks
->major
,
4724 table
[i
]->disks
->minor
);
4732 table
[tbl_size
++] = super
;
4736 /* update/extend the merged list of imsm_disk records */
4737 for (j
= 0; j
< mpb
->num_disks
; j
++) {
4738 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, j
);
4739 struct intel_disk
*idisk
;
4741 idisk
= disk_list_get(disk
->serial
, *disk_list
);
4743 idisk
->disk
.status
|= disk
->status
;
4744 if (is_configured(&idisk
->disk
) ||
4745 is_failed(&idisk
->disk
))
4746 idisk
->disk
.status
&= ~(SPARE_DISK
);
4748 idisk
= xcalloc(1, sizeof(*idisk
));
4749 idisk
->owner
= IMSM_UNKNOWN_OWNER
;
4750 idisk
->disk
= *disk
;
4751 idisk
->next
= *disk_list
;
4755 if (serialcmp(idisk
->disk
.serial
, d
->serial
) == 0)
4762 static struct intel_super
*
4763 validate_members(struct intel_super
*super
, struct intel_disk
*disk_list
,
4766 struct imsm_super
*mpb
= super
->anchor
;
4770 for (i
= 0; i
< mpb
->num_disks
; i
++) {
4771 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, i
);
4772 struct intel_disk
*idisk
;
4774 idisk
= disk_list_get(disk
->serial
, disk_list
);
4776 if (idisk
->owner
== owner
||
4777 idisk
->owner
== IMSM_UNKNOWN_OWNER
)
4780 dprintf("'%.16s' owner %d != %d\n",
4781 disk
->serial
, idisk
->owner
,
4784 dprintf("unknown disk %x [%d]: %.16s\n",
4785 __le32_to_cpu(mpb
->family_num
), i
,
4791 if (ok_count
== mpb
->num_disks
)
4796 static void show_conflicts(__u32 family_num
, struct intel_super
*super_list
)
4798 struct intel_super
*s
;
4800 for (s
= super_list
; s
; s
= s
->next
) {
4801 if (family_num
!= s
->anchor
->family_num
)
4803 pr_err("Conflict, offlining family %#x on '%s'\n",
4804 __le32_to_cpu(family_num
), s
->disks
->devname
);
4808 static struct intel_super
*
4809 imsm_thunderdome(struct intel_super
**super_list
, int len
)
4811 struct intel_super
*super_table
[len
];
4812 struct intel_disk
*disk_list
= NULL
;
4813 struct intel_super
*champion
, *spare
;
4814 struct intel_super
*s
, **del
;
4819 memset(super_table
, 0, sizeof(super_table
));
4820 for (s
= *super_list
; s
; s
= s
->next
)
4821 tbl_size
= __prep_thunderdome(super_table
, tbl_size
, s
, &disk_list
);
4823 for (i
= 0; i
< tbl_size
; i
++) {
4824 struct imsm_disk
*d
;
4825 struct intel_disk
*idisk
;
4826 struct imsm_super
*mpb
= super_table
[i
]->anchor
;
4829 d
= &s
->disks
->disk
;
4831 /* 'd' must appear in merged disk list for its
4832 * configuration to be valid
4834 idisk
= disk_list_get(d
->serial
, disk_list
);
4835 if (idisk
&& idisk
->owner
== i
)
4836 s
= validate_members(s
, disk_list
, i
);
4841 dprintf("marking family: %#x from %d:%d offline\n",
4843 super_table
[i
]->disks
->major
,
4844 super_table
[i
]->disks
->minor
);
4848 /* This is where the mdadm implementation differs from the Windows
4849 * driver which has no strict concept of a container. We can only
4850 * assemble one family from a container, so when returning a prodigal
4851 * array member to this system the code will not be able to disambiguate
4852 * the container contents that should be assembled ("foreign" versus
4853 * "local"). It requires user intervention to set the orig_family_num
4854 * to a new value to establish a new container. The Windows driver in
4855 * this situation fixes up the volume name in place and manages the
4856 * foreign array as an independent entity.
4861 for (i
= 0; i
< tbl_size
; i
++) {
4862 struct intel_super
*tbl_ent
= super_table
[i
];
4868 if (tbl_ent
->anchor
->num_raid_devs
== 0) {
4873 if (s
&& !is_spare
) {
4874 show_conflicts(tbl_ent
->anchor
->family_num
, *super_list
);
4876 } else if (!s
&& !is_spare
)
4889 pr_err("Chose family %#x on '%s', assemble conflicts to new container with '--update=uuid'\n",
4890 __le32_to_cpu(s
->anchor
->family_num
), s
->disks
->devname
);
4892 /* collect all dl's onto 'champion', and update them to
4893 * champion's version of the status
4895 for (s
= *super_list
; s
; s
= s
->next
) {
4896 struct imsm_super
*mpb
= champion
->anchor
;
4897 struct dl
*dl
= s
->disks
;
4902 mpb
->attributes
|= s
->anchor
->attributes
& MPB_ATTRIB_2TB_DISK
;
4904 for (i
= 0; i
< mpb
->num_disks
; i
++) {
4905 struct imsm_disk
*disk
;
4907 disk
= __serial_to_disk(dl
->serial
, mpb
, &dl
->index
);
4910 /* only set index on disks that are a member of
4911 * a populated contianer, i.e. one with
4914 if (is_failed(&dl
->disk
))
4916 else if (is_spare(&dl
->disk
))
4922 if (i
>= mpb
->num_disks
) {
4923 struct intel_disk
*idisk
;
4925 idisk
= disk_list_get(dl
->serial
, disk_list
);
4926 if (idisk
&& is_spare(&idisk
->disk
) &&
4927 !is_failed(&idisk
->disk
) && !is_configured(&idisk
->disk
))
4935 dl
->next
= champion
->disks
;
4936 champion
->disks
= dl
;
4940 /* delete 'champion' from super_list */
4941 for (del
= super_list
; *del
; ) {
4942 if (*del
== champion
) {
4943 *del
= (*del
)->next
;
4946 del
= &(*del
)->next
;
4948 champion
->next
= NULL
;
4952 struct intel_disk
*idisk
= disk_list
;
4954 disk_list
= disk_list
->next
;
4962 get_sra_super_block(int fd
, struct intel_super
**super_list
, char *devname
, int *max
, int keep_fd
);
4963 static int get_super_block(struct intel_super
**super_list
, char *devnm
, char *devname
,
4964 int major
, int minor
, int keep_fd
);
4966 get_devlist_super_block(struct md_list
*devlist
, struct intel_super
**super_list
,
4967 int *max
, int keep_fd
);
4969 static int load_super_imsm_all(struct supertype
*st
, int fd
, void **sbp
,
4970 char *devname
, struct md_list
*devlist
,
4973 struct intel_super
*super_list
= NULL
;
4974 struct intel_super
*super
= NULL
;
4979 /* 'fd' is an opened container */
4980 err
= get_sra_super_block(fd
, &super_list
, devname
, &i
, keep_fd
);
4982 /* get super block from devlist devices */
4983 err
= get_devlist_super_block(devlist
, &super_list
, &i
, keep_fd
);
4986 /* all mpbs enter, maybe one leaves */
4987 super
= imsm_thunderdome(&super_list
, i
);
4993 if (find_missing(super
) != 0) {
4999 /* load migration record */
5000 err
= load_imsm_migr_rec(super
);
5002 /* migration is in progress,
5003 * but migr_rec cannot be loaded,
5009 /* Check migration compatibility */
5010 if (err
== 0 && check_mpb_migr_compatibility(super
) != 0) {
5011 pr_err("Unsupported migration detected");
5013 fprintf(stderr
, " on %s\n", devname
);
5015 fprintf(stderr
, " (IMSM).\n");
5024 while (super_list
) {
5025 struct intel_super
*s
= super_list
;
5027 super_list
= super_list
->next
;
5036 strcpy(st
->container_devnm
, fd2devnm(fd
));
5038 st
->container_devnm
[0] = 0;
5039 if (err
== 0 && st
->ss
== NULL
) {
5040 st
->ss
= &super_imsm
;
5041 st
->minor_version
= 0;
5042 st
->max_devs
= IMSM_MAX_DEVICES
;
5048 get_devlist_super_block(struct md_list
*devlist
, struct intel_super
**super_list
,
5049 int *max
, int keep_fd
)
5051 struct md_list
*tmpdev
;
5055 for (i
= 0, tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
5056 if (tmpdev
->used
!= 1)
5058 if (tmpdev
->container
== 1) {
5060 int fd
= dev_open(tmpdev
->devname
, O_RDONLY
|O_EXCL
);
5062 pr_err("cannot open device %s: %s\n",
5063 tmpdev
->devname
, strerror(errno
));
5067 err
= get_sra_super_block(fd
, super_list
,
5068 tmpdev
->devname
, &lmax
,
5077 int major
= major(tmpdev
->st_rdev
);
5078 int minor
= minor(tmpdev
->st_rdev
);
5079 err
= get_super_block(super_list
,
5096 static int get_super_block(struct intel_super
**super_list
, char *devnm
, char *devname
,
5097 int major
, int minor
, int keep_fd
)
5099 struct intel_super
*s
;
5111 sprintf(nm
, "%d:%d", major
, minor
);
5112 dfd
= dev_open(nm
, O_RDWR
);
5118 get_dev_sector_size(dfd
, NULL
, &s
->sector_size
);
5119 find_intel_hba_capability(dfd
, s
, devname
);
5120 err
= load_and_parse_mpb(dfd
, s
, NULL
, keep_fd
);
5122 /* retry the load if we might have raced against mdmon */
5123 if (err
== 3 && devnm
&& mdmon_running(devnm
))
5124 for (retry
= 0; retry
< 3; retry
++) {
5126 err
= load_and_parse_mpb(dfd
, s
, NULL
, keep_fd
);
5132 s
->next
= *super_list
;
5140 if (dfd
>= 0 && !keep_fd
)
5147 get_sra_super_block(int fd
, struct intel_super
**super_list
, char *devname
, int *max
, int keep_fd
)
5154 sra
= sysfs_read(fd
, NULL
, GET_LEVEL
|GET_VERSION
|GET_DEVS
|GET_STATE
);
5158 if (sra
->array
.major_version
!= -1 ||
5159 sra
->array
.minor_version
!= -2 ||
5160 strcmp(sra
->text_version
, "imsm") != 0) {
5165 devnm
= fd2devnm(fd
);
5166 for (sd
= sra
->devs
, i
= 0; sd
; sd
= sd
->next
, i
++) {
5167 if (get_super_block(super_list
, devnm
, devname
,
5168 sd
->disk
.major
, sd
->disk
.minor
, keep_fd
) != 0) {
5179 static int load_container_imsm(struct supertype
*st
, int fd
, char *devname
)
5181 return load_super_imsm_all(st
, fd
, &st
->sb
, devname
, NULL
, 1);
5184 static int load_super_imsm(struct supertype
*st
, int fd
, char *devname
)
5186 struct intel_super
*super
;
5190 if (test_partition(fd
))
5191 /* IMSM not allowed on partitions */
5194 free_super_imsm(st
);
5196 super
= alloc_super();
5197 get_dev_sector_size(fd
, NULL
, &super
->sector_size
);
5200 /* Load hba and capabilities if they exist.
5201 * But do not preclude loading metadata in case capabilities or hba are
5202 * non-compliant and ignore_hw_compat is set.
5204 rv
= find_intel_hba_capability(fd
, super
, devname
);
5205 /* no orom/efi or non-intel hba of the disk */
5206 if (rv
!= 0 && st
->ignore_hw_compat
== 0) {
5208 pr_err("No OROM/EFI properties for %s\n", devname
);
5212 rv
= load_and_parse_mpb(fd
, super
, devname
, 0);
5214 /* retry the load if we might have raced against mdmon */
5216 struct mdstat_ent
*mdstat
= NULL
;
5217 char *name
= fd2kname(fd
);
5220 mdstat
= mdstat_by_component(name
);
5222 if (mdstat
&& mdmon_running(mdstat
->devnm
) && getpid() != mdmon_pid(mdstat
->devnm
)) {
5223 for (retry
= 0; retry
< 3; retry
++) {
5225 rv
= load_and_parse_mpb(fd
, super
, devname
, 0);
5231 free_mdstat(mdstat
);
5236 pr_err("Failed to load all information sections on %s\n", devname
);
5242 if (st
->ss
== NULL
) {
5243 st
->ss
= &super_imsm
;
5244 st
->minor_version
= 0;
5245 st
->max_devs
= IMSM_MAX_DEVICES
;
5248 /* load migration record */
5249 if (load_imsm_migr_rec(super
) == 0) {
5250 /* Check for unsupported migration features */
5251 if (check_mpb_migr_compatibility(super
) != 0) {
5252 pr_err("Unsupported migration detected");
5254 fprintf(stderr
, " on %s\n", devname
);
5256 fprintf(stderr
, " (IMSM).\n");
5264 static __u16
info_to_blocks_per_strip(mdu_array_info_t
*info
)
5266 if (info
->level
== 1)
5268 return info
->chunk_size
>> 9;
5271 static unsigned long long info_to_blocks_per_member(mdu_array_info_t
*info
,
5272 unsigned long long size
)
5274 if (info
->level
== 1)
5277 return (size
* 2) & ~(info_to_blocks_per_strip(info
) - 1);
5280 static void imsm_update_version_info(struct intel_super
*super
)
5282 /* update the version and attributes */
5283 struct imsm_super
*mpb
= super
->anchor
;
5285 struct imsm_dev
*dev
;
5286 struct imsm_map
*map
;
5289 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
5290 dev
= get_imsm_dev(super
, i
);
5291 map
= get_imsm_map(dev
, MAP_0
);
5292 if (__le32_to_cpu(dev
->size_high
) > 0)
5293 mpb
->attributes
|= MPB_ATTRIB_2TB
;
5295 /* FIXME detect when an array spans a port multiplier */
5297 mpb
->attributes
|= MPB_ATTRIB_PM
;
5300 if (mpb
->num_raid_devs
> 1 ||
5301 mpb
->attributes
!= MPB_ATTRIB_CHECKSUM_VERIFY
) {
5302 version
= MPB_VERSION_ATTRIBS
;
5303 switch (get_imsm_raid_level(map
)) {
5304 case 0: mpb
->attributes
|= MPB_ATTRIB_RAID0
; break;
5305 case 1: mpb
->attributes
|= MPB_ATTRIB_RAID1
; break;
5306 case 10: mpb
->attributes
|= MPB_ATTRIB_RAID10
; break;
5307 case 5: mpb
->attributes
|= MPB_ATTRIB_RAID5
; break;
5310 if (map
->num_members
>= 5)
5311 version
= MPB_VERSION_5OR6_DISK_ARRAY
;
5312 else if (dev
->status
== DEV_CLONE_N_GO
)
5313 version
= MPB_VERSION_CNG
;
5314 else if (get_imsm_raid_level(map
) == 5)
5315 version
= MPB_VERSION_RAID5
;
5316 else if (map
->num_members
>= 3)
5317 version
= MPB_VERSION_3OR4_DISK_ARRAY
;
5318 else if (get_imsm_raid_level(map
) == 1)
5319 version
= MPB_VERSION_RAID1
;
5321 version
= MPB_VERSION_RAID0
;
5323 strcpy(((char *) mpb
->sig
) + strlen(MPB_SIGNATURE
), version
);
5327 static int check_name(struct intel_super
*super
, char *name
, int quiet
)
5329 struct imsm_super
*mpb
= super
->anchor
;
5330 char *reason
= NULL
;
5332 size_t len
= strlen(name
);
5336 while (isspace(start
[len
- 1]))
5338 while (*start
&& isspace(*start
))
5340 memmove(name
, start
, len
+ 1);
5343 if (len
> MAX_RAID_SERIAL_LEN
)
5344 reason
= "must be 16 characters or less";
5346 reason
= "must be a non-empty string";
5348 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
5349 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
5351 if (strncmp((char *) dev
->volume
, name
, MAX_RAID_SERIAL_LEN
) == 0) {
5352 reason
= "already exists";
5357 if (reason
&& !quiet
)
5358 pr_err("imsm volume name %s\n", reason
);
5363 static int init_super_imsm_volume(struct supertype
*st
, mdu_array_info_t
*info
,
5364 struct shape
*s
, char *name
,
5365 char *homehost
, int *uuid
,
5366 long long data_offset
)
5368 /* We are creating a volume inside a pre-existing container.
5369 * so st->sb is already set.
5371 struct intel_super
*super
= st
->sb
;
5372 unsigned int sector_size
= super
->sector_size
;
5373 struct imsm_super
*mpb
= super
->anchor
;
5374 struct intel_dev
*dv
;
5375 struct imsm_dev
*dev
;
5376 struct imsm_vol
*vol
;
5377 struct imsm_map
*map
;
5378 int idx
= mpb
->num_raid_devs
;
5381 unsigned long long array_blocks
;
5382 size_t size_old
, size_new
;
5383 unsigned long long num_data_stripes
;
5384 unsigned int data_disks
;
5385 unsigned long long size_per_member
;
5387 if (super
->orom
&& mpb
->num_raid_devs
>= super
->orom
->vpa
) {
5388 pr_err("This imsm-container already has the maximum of %d volumes\n", super
->orom
->vpa
);
5392 /* ensure the mpb is large enough for the new data */
5393 size_old
= __le32_to_cpu(mpb
->mpb_size
);
5394 size_new
= disks_to_mpb_size(info
->nr_disks
);
5395 if (size_new
> size_old
) {
5397 size_t size_round
= ROUND_UP(size_new
, sector_size
);
5399 if (posix_memalign(&mpb_new
, sector_size
, size_round
) != 0) {
5400 pr_err("could not allocate new mpb\n");
5403 if (posix_memalign(&super
->migr_rec_buf
, MAX_SECTOR_SIZE
,
5404 MIGR_REC_BUF_SECTORS
*
5405 MAX_SECTOR_SIZE
) != 0) {
5406 pr_err("could not allocate migr_rec buffer\n");
5412 memcpy(mpb_new
, mpb
, size_old
);
5415 super
->anchor
= mpb_new
;
5416 mpb
->mpb_size
= __cpu_to_le32(size_new
);
5417 memset(mpb_new
+ size_old
, 0, size_round
- size_old
);
5418 super
->len
= size_round
;
5420 super
->current_vol
= idx
;
5422 /* handle 'failed_disks' by either:
5423 * a) create dummy disk entries in the table if this the first
5424 * volume in the array. We add them here as this is the only
5425 * opportunity to add them. add_to_super_imsm_volume()
5426 * handles the non-failed disks and continues incrementing
5428 * b) validate that 'failed_disks' matches the current number
5429 * of missing disks if the container is populated
5431 if (super
->current_vol
== 0) {
5433 for (i
= 0; i
< info
->failed_disks
; i
++) {
5434 struct imsm_disk
*disk
;
5437 disk
= __get_imsm_disk(mpb
, i
);
5438 disk
->status
= CONFIGURED_DISK
| FAILED_DISK
;
5439 disk
->scsi_id
= __cpu_to_le32(~(__u32
)0);
5440 snprintf((char *) disk
->serial
, MAX_RAID_SERIAL_LEN
,
5441 "missing:%d", (__u8
)i
);
5443 find_missing(super
);
5448 for (d
= super
->missing
; d
; d
= d
->next
)
5450 if (info
->failed_disks
> missing
) {
5451 pr_err("unable to add 'missing' disk to container\n");
5456 if (!check_name(super
, name
, 0))
5458 dv
= xmalloc(sizeof(*dv
));
5459 dev
= xcalloc(1, sizeof(*dev
) + sizeof(__u32
) * (info
->raid_disks
- 1));
5461 * Explicitly allow truncating to not confuse gcc's
5462 * -Werror=stringop-truncation
5464 namelen
= min((int) strlen(name
), MAX_RAID_SERIAL_LEN
);
5465 memcpy(dev
->volume
, name
, namelen
);
5466 array_blocks
= calc_array_size(info
->level
, info
->raid_disks
,
5467 info
->layout
, info
->chunk_size
,
5468 s
->size
* BLOCKS_PER_KB
);
5469 data_disks
= get_data_disks(info
->level
, info
->layout
,
5471 array_blocks
= round_size_to_mb(array_blocks
, data_disks
);
5472 size_per_member
= array_blocks
/ data_disks
;
5474 set_imsm_dev_size(dev
, array_blocks
);
5475 dev
->status
= (DEV_READ_COALESCING
| DEV_WRITE_COALESCING
);
5477 vol
->migr_state
= 0;
5478 set_migr_type(dev
, MIGR_INIT
);
5479 vol
->dirty
= !info
->state
;
5480 vol
->curr_migr_unit
= 0;
5481 map
= get_imsm_map(dev
, MAP_0
);
5482 set_pba_of_lba0(map
, super
->create_offset
);
5483 map
->blocks_per_strip
= __cpu_to_le16(info_to_blocks_per_strip(info
));
5484 map
->failed_disk_num
= ~0;
5485 if (info
->level
> 0)
5486 map
->map_state
= (info
->state
? IMSM_T_STATE_NORMAL
5487 : IMSM_T_STATE_UNINITIALIZED
);
5489 map
->map_state
= info
->failed_disks
? IMSM_T_STATE_FAILED
:
5490 IMSM_T_STATE_NORMAL
;
5493 if (info
->level
== 1 && info
->raid_disks
> 2) {
5496 pr_err("imsm does not support more than 2 disksin a raid1 volume\n");
5500 map
->raid_level
= info
->level
;
5501 if (info
->level
== 10) {
5502 map
->raid_level
= 1;
5503 map
->num_domains
= info
->raid_disks
/ 2;
5504 } else if (info
->level
== 1)
5505 map
->num_domains
= info
->raid_disks
;
5507 map
->num_domains
= 1;
5509 /* info->size is only int so use the 'size' parameter instead */
5510 num_data_stripes
= size_per_member
/ info_to_blocks_per_strip(info
);
5511 num_data_stripes
/= map
->num_domains
;
5512 set_num_data_stripes(map
, num_data_stripes
);
5514 size_per_member
+= NUM_BLOCKS_DIRTY_STRIPE_REGION
;
5515 set_blocks_per_member(map
, info_to_blocks_per_member(info
,
5519 map
->num_members
= info
->raid_disks
;
5520 for (i
= 0; i
< map
->num_members
; i
++) {
5521 /* initialized in add_to_super */
5522 set_imsm_ord_tbl_ent(map
, i
, IMSM_ORD_REBUILD
);
5524 mpb
->num_raid_devs
++;
5525 mpb
->num_raid_devs_created
++;
5526 dev
->my_vol_raid_dev_num
= mpb
->num_raid_devs_created
;
5528 if (s
->consistency_policy
<= CONSISTENCY_POLICY_RESYNC
) {
5529 dev
->rwh_policy
= RWH_MULTIPLE_OFF
;
5530 } else if (s
->consistency_policy
== CONSISTENCY_POLICY_PPL
) {
5531 dev
->rwh_policy
= RWH_MULTIPLE_DISTRIBUTED
;
5535 pr_err("imsm does not support consistency policy %s\n",
5536 map_num(consistency_policies
, s
->consistency_policy
));
5541 dv
->index
= super
->current_vol
;
5542 dv
->next
= super
->devlist
;
5543 super
->devlist
= dv
;
5545 imsm_update_version_info(super
);
5550 static int init_super_imsm(struct supertype
*st
, mdu_array_info_t
*info
,
5551 struct shape
*s
, char *name
,
5552 char *homehost
, int *uuid
,
5553 unsigned long long data_offset
)
5555 /* This is primarily called by Create when creating a new array.
5556 * We will then get add_to_super called for each component, and then
5557 * write_init_super called to write it out to each device.
5558 * For IMSM, Create can create on fresh devices or on a pre-existing
5560 * To create on a pre-existing array a different method will be called.
5561 * This one is just for fresh drives.
5563 struct intel_super
*super
;
5564 struct imsm_super
*mpb
;
5568 if (data_offset
!= INVALID_SECTORS
) {
5569 pr_err("data-offset not supported by imsm\n");
5574 return init_super_imsm_volume(st
, info
, s
, name
, homehost
, uuid
,
5578 mpb_size
= disks_to_mpb_size(info
->nr_disks
);
5580 mpb_size
= MAX_SECTOR_SIZE
;
5582 super
= alloc_super();
5584 posix_memalign(&super
->buf
, MAX_SECTOR_SIZE
, mpb_size
) != 0) {
5589 pr_err("could not allocate superblock\n");
5592 if (posix_memalign(&super
->migr_rec_buf
, MAX_SECTOR_SIZE
,
5593 MIGR_REC_BUF_SECTORS
*MAX_SECTOR_SIZE
) != 0) {
5594 pr_err("could not allocate migr_rec buffer\n");
5599 memset(super
->buf
, 0, mpb_size
);
5601 mpb
->mpb_size
= __cpu_to_le32(mpb_size
);
5605 /* zeroing superblock */
5609 mpb
->attributes
= MPB_ATTRIB_CHECKSUM_VERIFY
;
5611 version
= (char *) mpb
->sig
;
5612 strcpy(version
, MPB_SIGNATURE
);
5613 version
+= strlen(MPB_SIGNATURE
);
5614 strcpy(version
, MPB_VERSION_RAID0
);
5619 static int drive_validate_sector_size(struct intel_super
*super
, struct dl
*dl
)
5621 unsigned int member_sector_size
;
5624 pr_err("Invalid file descriptor for %s\n", dl
->devname
);
5628 if (!get_dev_sector_size(dl
->fd
, dl
->devname
, &member_sector_size
))
5630 if (member_sector_size
!= super
->sector_size
)
5635 static int add_to_super_imsm_volume(struct supertype
*st
, mdu_disk_info_t
*dk
,
5636 int fd
, char *devname
)
5638 struct intel_super
*super
= st
->sb
;
5639 struct imsm_super
*mpb
= super
->anchor
;
5640 struct imsm_disk
*_disk
;
5641 struct imsm_dev
*dev
;
5642 struct imsm_map
*map
;
5646 dev
= get_imsm_dev(super
, super
->current_vol
);
5647 map
= get_imsm_map(dev
, MAP_0
);
5649 if (! (dk
->state
& (1<<MD_DISK_SYNC
))) {
5650 pr_err("%s: Cannot add spare devices to IMSM volume\n",
5656 /* we're doing autolayout so grab the pre-marked (in
5657 * validate_geometry) raid_disk
5659 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
5660 if (dl
->raiddisk
== dk
->raid_disk
)
5663 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
5664 if (dl
->major
== dk
->major
&&
5665 dl
->minor
== dk
->minor
)
5670 pr_err("%s is not a member of the same container\n", devname
);
5674 if (mpb
->num_disks
== 0)
5675 if (!get_dev_sector_size(dl
->fd
, dl
->devname
,
5676 &super
->sector_size
))
5679 if (!drive_validate_sector_size(super
, dl
)) {
5680 pr_err("Combining drives of different sector size in one volume is not allowed\n");
5684 /* add a pristine spare to the metadata */
5685 if (dl
->index
< 0) {
5686 dl
->index
= super
->anchor
->num_disks
;
5687 super
->anchor
->num_disks
++;
5689 /* Check the device has not already been added */
5690 slot
= get_imsm_disk_slot(map
, dl
->index
);
5692 (get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
) & IMSM_ORD_REBUILD
) == 0) {
5693 pr_err("%s has been included in this array twice\n",
5697 set_imsm_ord_tbl_ent(map
, dk
->raid_disk
, dl
->index
);
5698 dl
->disk
.status
= CONFIGURED_DISK
;
5700 /* update size of 'missing' disks to be at least as large as the
5701 * largest acitve member (we only have dummy missing disks when
5702 * creating the first volume)
5704 if (super
->current_vol
== 0) {
5705 for (df
= super
->missing
; df
; df
= df
->next
) {
5706 if (total_blocks(&dl
->disk
) > total_blocks(&df
->disk
))
5707 set_total_blocks(&df
->disk
, total_blocks(&dl
->disk
));
5708 _disk
= __get_imsm_disk(mpb
, df
->index
);
5713 /* refresh unset/failed slots to point to valid 'missing' entries */
5714 for (df
= super
->missing
; df
; df
= df
->next
)
5715 for (slot
= 0; slot
< mpb
->num_disks
; slot
++) {
5716 __u32 ord
= get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
);
5718 if ((ord
& IMSM_ORD_REBUILD
) == 0)
5720 set_imsm_ord_tbl_ent(map
, slot
, df
->index
| IMSM_ORD_REBUILD
);
5721 if (is_gen_migration(dev
)) {
5722 struct imsm_map
*map2
= get_imsm_map(dev
,
5724 int slot2
= get_imsm_disk_slot(map2
, df
->index
);
5725 if (slot2
< map2
->num_members
&& slot2
>= 0) {
5726 __u32 ord2
= get_imsm_ord_tbl_ent(dev
,
5729 if ((unsigned)df
->index
==
5731 set_imsm_ord_tbl_ent(map2
,
5737 dprintf("set slot:%d to missing disk:%d\n", slot
, df
->index
);
5741 /* if we are creating the first raid device update the family number */
5742 if (super
->current_vol
== 0) {
5744 struct imsm_dev
*_dev
= __get_imsm_dev(mpb
, 0);
5746 _disk
= __get_imsm_disk(mpb
, dl
->index
);
5747 if (!_dev
|| !_disk
) {
5748 pr_err("BUG mpb setup error\n");
5754 sum
+= __gen_imsm_checksum(mpb
);
5755 mpb
->family_num
= __cpu_to_le32(sum
);
5756 mpb
->orig_family_num
= mpb
->family_num
;
5757 mpb
->creation_time
= __cpu_to_le64((__u64
)time(NULL
));
5759 super
->current_disk
= dl
;
5764 * Function marks disk as spare and restores disk serial
5765 * in case it was previously marked as failed by takeover operation
5767 * -1 : critical error
5768 * 0 : disk is marked as spare but serial is not set
5771 int mark_spare(struct dl
*disk
)
5773 __u8 serial
[MAX_RAID_SERIAL_LEN
];
5780 if (!imsm_read_serial(disk
->fd
, NULL
, serial
, MAX_RAID_SERIAL_LEN
)) {
5781 /* Restore disk serial number, because takeover marks disk
5782 * as failed and adds to serial ':0' before it becomes
5785 serialcpy(disk
->serial
, serial
);
5786 serialcpy(disk
->disk
.serial
, serial
);
5789 disk
->disk
.status
= SPARE_DISK
;
5796 static int write_super_imsm_spare(struct intel_super
*super
, struct dl
*d
);
5798 static int add_to_super_imsm(struct supertype
*st
, mdu_disk_info_t
*dk
,
5799 int fd
, char *devname
,
5800 unsigned long long data_offset
)
5802 struct intel_super
*super
= st
->sb
;
5804 unsigned long long size
;
5805 unsigned int member_sector_size
;
5810 /* If we are on an RAID enabled platform check that the disk is
5811 * attached to the raid controller.
5812 * We do not need to test disks attachment for container based additions,
5813 * they shall be already tested when container was created/assembled.
5815 rv
= find_intel_hba_capability(fd
, super
, devname
);
5816 /* no orom/efi or non-intel hba of the disk */
5818 dprintf("capability: %p fd: %d ret: %d\n",
5819 super
->orom
, fd
, rv
);
5823 if (super
->current_vol
>= 0)
5824 return add_to_super_imsm_volume(st
, dk
, fd
, devname
);
5827 dd
= xcalloc(sizeof(*dd
), 1);
5828 dd
->major
= major(stb
.st_rdev
);
5829 dd
->minor
= minor(stb
.st_rdev
);
5830 dd
->devname
= devname
? xstrdup(devname
) : NULL
;
5833 dd
->action
= DISK_ADD
;
5834 rv
= imsm_read_serial(fd
, devname
, dd
->serial
, MAX_RAID_SERIAL_LEN
);
5836 pr_err("failed to retrieve scsi serial, aborting\n");
5842 if (super
->hba
&& ((super
->hba
->type
== SYS_DEV_NVME
) ||
5843 (super
->hba
->type
== SYS_DEV_VMD
))) {
5845 char *devpath
= diskfd_to_devpath(fd
);
5846 char controller_path
[PATH_MAX
];
5849 pr_err("failed to get devpath, aborting\n");
5856 snprintf(controller_path
, PATH_MAX
-1, "%s/device", devpath
);
5859 if (!imsm_is_nvme_supported(dd
->fd
, 1)) {
5866 if (devpath_to_vendor(controller_path
) == 0x8086) {
5868 * If Intel's NVMe drive has serial ended with
5869 * "-A","-B","-1" or "-2" it means that this is "x8"
5870 * device (double drive on single PCIe card).
5871 * User should be warned about potential data loss.
5873 for (i
= MAX_RAID_SERIAL_LEN
-1; i
> 0; i
--) {
5874 /* Skip empty character at the end */
5875 if (dd
->serial
[i
] == 0)
5878 if (((dd
->serial
[i
] == 'A') ||
5879 (dd
->serial
[i
] == 'B') ||
5880 (dd
->serial
[i
] == '1') ||
5881 (dd
->serial
[i
] == '2')) &&
5882 (dd
->serial
[i
-1] == '-'))
5883 pr_err("\tThe action you are about to take may put your data at risk.\n"
5884 "\tPlease note that x8 devices may consist of two separate x4 devices "
5885 "located on a single PCIe port.\n"
5886 "\tRAID 0 is the only supported configuration for this type of x8 device.\n");
5889 } else if (super
->hba
->type
== SYS_DEV_VMD
&& super
->orom
&&
5890 !imsm_orom_has_tpv_support(super
->orom
)) {
5891 pr_err("\tPlatform configuration does not support non-Intel NVMe drives.\n"
5892 "\tPlease refer to Intel(R) RSTe/VROC user guide.\n");
5899 get_dev_size(fd
, NULL
, &size
);
5900 get_dev_sector_size(fd
, NULL
, &member_sector_size
);
5902 if (super
->sector_size
== 0) {
5903 /* this a first device, so sector_size is not set yet */
5904 super
->sector_size
= member_sector_size
;
5907 /* clear migr_rec when adding disk to container */
5908 memset(super
->migr_rec_buf
, 0, MIGR_REC_BUF_SECTORS
*MAX_SECTOR_SIZE
);
5909 if (lseek64(fd
, size
- MIGR_REC_SECTOR_POSITION
*member_sector_size
,
5911 if ((unsigned int)write(fd
, super
->migr_rec_buf
,
5912 MIGR_REC_BUF_SECTORS
*member_sector_size
) !=
5913 MIGR_REC_BUF_SECTORS
*member_sector_size
)
5914 perror("Write migr_rec failed");
5918 serialcpy(dd
->disk
.serial
, dd
->serial
);
5919 set_total_blocks(&dd
->disk
, size
);
5920 if (__le32_to_cpu(dd
->disk
.total_blocks_hi
) > 0) {
5921 struct imsm_super
*mpb
= super
->anchor
;
5922 mpb
->attributes
|= MPB_ATTRIB_2TB_DISK
;
5925 if (sysfs_disk_to_scsi_id(fd
, &id
) == 0)
5926 dd
->disk
.scsi_id
= __cpu_to_le32(id
);
5928 dd
->disk
.scsi_id
= __cpu_to_le32(0);
5930 if (st
->update_tail
) {
5931 dd
->next
= super
->disk_mgmt_list
;
5932 super
->disk_mgmt_list
= dd
;
5934 /* this is called outside of mdmon
5935 * write initial spare metadata
5936 * mdmon will overwrite it.
5938 dd
->next
= super
->disks
;
5940 write_super_imsm_spare(super
, dd
);
5946 static int remove_from_super_imsm(struct supertype
*st
, mdu_disk_info_t
*dk
)
5948 struct intel_super
*super
= st
->sb
;
5951 /* remove from super works only in mdmon - for communication
5952 * manager - monitor. Check if communication memory buffer
5955 if (!st
->update_tail
) {
5956 pr_err("shall be used in mdmon context only\n");
5959 dd
= xcalloc(1, sizeof(*dd
));
5960 dd
->major
= dk
->major
;
5961 dd
->minor
= dk
->minor
;
5964 dd
->action
= DISK_REMOVE
;
5966 dd
->next
= super
->disk_mgmt_list
;
5967 super
->disk_mgmt_list
= dd
;
5972 static int store_imsm_mpb(int fd
, struct imsm_super
*mpb
);
5975 char buf
[MAX_SECTOR_SIZE
];
5976 struct imsm_super anchor
;
5977 } spare_record
__attribute__ ((aligned(MAX_SECTOR_SIZE
)));
5980 static int write_super_imsm_spare(struct intel_super
*super
, struct dl
*d
)
5982 struct imsm_super
*mpb
= super
->anchor
;
5983 struct imsm_super
*spare
= &spare_record
.anchor
;
5989 spare
->mpb_size
= __cpu_to_le32(sizeof(struct imsm_super
));
5990 spare
->generation_num
= __cpu_to_le32(1UL);
5991 spare
->attributes
= MPB_ATTRIB_CHECKSUM_VERIFY
;
5992 spare
->num_disks
= 1;
5993 spare
->num_raid_devs
= 0;
5994 spare
->cache_size
= mpb
->cache_size
;
5995 spare
->pwr_cycle_count
= __cpu_to_le32(1);
5997 snprintf((char *) spare
->sig
, MAX_SIGNATURE_LENGTH
,
5998 MPB_SIGNATURE MPB_VERSION_RAID0
);
6000 spare
->disk
[0] = d
->disk
;
6001 if (__le32_to_cpu(d
->disk
.total_blocks_hi
) > 0)
6002 spare
->attributes
|= MPB_ATTRIB_2TB_DISK
;
6004 if (super
->sector_size
== 4096)
6005 convert_to_4k_imsm_disk(&spare
->disk
[0]);
6007 sum
= __gen_imsm_checksum(spare
);
6008 spare
->family_num
= __cpu_to_le32(sum
);
6009 spare
->orig_family_num
= 0;
6010 sum
= __gen_imsm_checksum(spare
);
6011 spare
->check_sum
= __cpu_to_le32(sum
);
6013 if (store_imsm_mpb(d
->fd
, spare
)) {
6014 pr_err("failed for device %d:%d %s\n",
6015 d
->major
, d
->minor
, strerror(errno
));
6021 /* spare records have their own family number and do not have any defined raid
6024 static int write_super_imsm_spares(struct intel_super
*super
, int doclose
)
6028 for (d
= super
->disks
; d
; d
= d
->next
) {
6032 if (write_super_imsm_spare(super
, d
))
6044 static int write_super_imsm(struct supertype
*st
, int doclose
)
6046 struct intel_super
*super
= st
->sb
;
6047 unsigned int sector_size
= super
->sector_size
;
6048 struct imsm_super
*mpb
= super
->anchor
;
6054 __u32 mpb_size
= sizeof(struct imsm_super
) - sizeof(struct imsm_disk
);
6056 int clear_migration_record
= 1;
6059 /* 'generation' is incremented everytime the metadata is written */
6060 generation
= __le32_to_cpu(mpb
->generation_num
);
6062 mpb
->generation_num
= __cpu_to_le32(generation
);
6064 /* fix up cases where previous mdadm releases failed to set
6067 if (mpb
->orig_family_num
== 0)
6068 mpb
->orig_family_num
= mpb
->family_num
;
6070 for (d
= super
->disks
; d
; d
= d
->next
) {
6074 mpb
->disk
[d
->index
] = d
->disk
;
6078 for (d
= super
->missing
; d
; d
= d
->next
) {
6079 mpb
->disk
[d
->index
] = d
->disk
;
6082 mpb
->num_disks
= num_disks
;
6083 mpb_size
+= sizeof(struct imsm_disk
) * mpb
->num_disks
;
6085 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
6086 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
6087 struct imsm_dev
*dev2
= get_imsm_dev(super
, i
);
6089 imsm_copy_dev(dev
, dev2
);
6090 mpb_size
+= sizeof_imsm_dev(dev
, 0);
6092 if (is_gen_migration(dev2
))
6093 clear_migration_record
= 0;
6096 bbm_log_size
= get_imsm_bbm_log_size(super
->bbm_log
);
6099 memcpy((void *)mpb
+ mpb_size
, super
->bbm_log
, bbm_log_size
);
6100 mpb
->attributes
|= MPB_ATTRIB_BBM
;
6102 mpb
->attributes
&= ~MPB_ATTRIB_BBM
;
6104 super
->anchor
->bbm_log_size
= __cpu_to_le32(bbm_log_size
);
6105 mpb_size
+= bbm_log_size
;
6106 mpb
->mpb_size
= __cpu_to_le32(mpb_size
);
6109 assert(super
->len
== 0 || mpb_size
<= super
->len
);
6112 /* recalculate checksum */
6113 sum
= __gen_imsm_checksum(mpb
);
6114 mpb
->check_sum
= __cpu_to_le32(sum
);
6116 if (super
->clean_migration_record_by_mdmon
) {
6117 clear_migration_record
= 1;
6118 super
->clean_migration_record_by_mdmon
= 0;
6120 if (clear_migration_record
)
6121 memset(super
->migr_rec_buf
, 0,
6122 MIGR_REC_BUF_SECTORS
*MAX_SECTOR_SIZE
);
6124 if (sector_size
== 4096)
6125 convert_to_4k(super
);
6127 /* write the mpb for disks that compose raid devices */
6128 for (d
= super
->disks
; d
; d
= d
->next
) {
6129 if (d
->index
< 0 || is_failed(&d
->disk
))
6132 if (clear_migration_record
) {
6133 unsigned long long dsize
;
6135 get_dev_size(d
->fd
, NULL
, &dsize
);
6136 if (lseek64(d
->fd
, dsize
- sector_size
,
6138 if ((unsigned int)write(d
->fd
,
6139 super
->migr_rec_buf
,
6140 MIGR_REC_BUF_SECTORS
*sector_size
) !=
6141 MIGR_REC_BUF_SECTORS
*sector_size
)
6142 perror("Write migr_rec failed");
6146 if (store_imsm_mpb(d
->fd
, mpb
))
6148 "failed for device %d:%d (fd: %d)%s\n",
6150 d
->fd
, strerror(errno
));
6159 return write_super_imsm_spares(super
, doclose
);
6164 static int create_array(struct supertype
*st
, int dev_idx
)
6167 struct imsm_update_create_array
*u
;
6168 struct intel_super
*super
= st
->sb
;
6169 struct imsm_dev
*dev
= get_imsm_dev(super
, dev_idx
);
6170 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
6171 struct disk_info
*inf
;
6172 struct imsm_disk
*disk
;
6175 len
= sizeof(*u
) - sizeof(*dev
) + sizeof_imsm_dev(dev
, 0) +
6176 sizeof(*inf
) * map
->num_members
;
6178 u
->type
= update_create_array
;
6179 u
->dev_idx
= dev_idx
;
6180 imsm_copy_dev(&u
->dev
, dev
);
6181 inf
= get_disk_info(u
);
6182 for (i
= 0; i
< map
->num_members
; i
++) {
6183 int idx
= get_imsm_disk_idx(dev
, i
, MAP_X
);
6185 disk
= get_imsm_disk(super
, idx
);
6187 disk
= get_imsm_missing(super
, idx
);
6188 serialcpy(inf
[i
].serial
, disk
->serial
);
6190 append_metadata_update(st
, u
, len
);
6195 static int mgmt_disk(struct supertype
*st
)
6197 struct intel_super
*super
= st
->sb
;
6199 struct imsm_update_add_remove_disk
*u
;
6201 if (!super
->disk_mgmt_list
)
6206 u
->type
= update_add_remove_disk
;
6207 append_metadata_update(st
, u
, len
);
6212 __u32
crc32c_le(__u32 crc
, unsigned char const *p
, size_t len
);
6214 static int write_ppl_header(unsigned long long ppl_sector
, int fd
, void *buf
)
6216 struct ppl_header
*ppl_hdr
= buf
;
6219 ppl_hdr
->checksum
= __cpu_to_le32(~crc32c_le(~0, buf
, PPL_HEADER_SIZE
));
6221 if (lseek64(fd
, ppl_sector
* 512, SEEK_SET
) < 0) {
6223 perror("Failed to seek to PPL header location");
6227 if (write(fd
, buf
, PPL_HEADER_SIZE
) != PPL_HEADER_SIZE
) {
6229 perror("Write PPL header failed");
6238 static int write_init_ppl_imsm(struct supertype
*st
, struct mdinfo
*info
, int fd
)
6240 struct intel_super
*super
= st
->sb
;
6242 struct ppl_header
*ppl_hdr
;
6245 /* first clear entire ppl space */
6246 ret
= zero_disk_range(fd
, info
->ppl_sector
, info
->ppl_size
);
6250 ret
= posix_memalign(&buf
, MAX_SECTOR_SIZE
, PPL_HEADER_SIZE
);
6252 pr_err("Failed to allocate PPL header buffer\n");
6256 memset(buf
, 0, PPL_HEADER_SIZE
);
6258 memset(ppl_hdr
->reserved
, 0xff, PPL_HDR_RESERVED
);
6259 ppl_hdr
->signature
= __cpu_to_le32(super
->anchor
->orig_family_num
);
6261 if (info
->mismatch_cnt
) {
6263 * We are overwriting an invalid ppl. Make one entry with wrong
6264 * checksum to prevent the kernel from skipping resync.
6266 ppl_hdr
->entries_count
= __cpu_to_le32(1);
6267 ppl_hdr
->entries
[0].checksum
= ~0;
6270 ret
= write_ppl_header(info
->ppl_sector
, fd
, buf
);
6276 static int is_rebuilding(struct imsm_dev
*dev
);
6278 static int validate_ppl_imsm(struct supertype
*st
, struct mdinfo
*info
,
6279 struct mdinfo
*disk
)
6281 struct intel_super
*super
= st
->sb
;
6283 void *buf_orig
, *buf
, *buf_prev
= NULL
;
6285 struct ppl_header
*ppl_hdr
= NULL
;
6287 struct imsm_dev
*dev
;
6290 unsigned long long ppl_offset
= 0;
6291 unsigned long long prev_gen_num
= 0;
6293 if (disk
->disk
.raid_disk
< 0)
6296 dev
= get_imsm_dev(super
, info
->container_member
);
6297 idx
= get_imsm_disk_idx(dev
, disk
->disk
.raid_disk
, MAP_0
);
6298 d
= get_imsm_dl_disk(super
, idx
);
6300 if (!d
|| d
->index
< 0 || is_failed(&d
->disk
))
6303 if (posix_memalign(&buf_orig
, MAX_SECTOR_SIZE
, PPL_HEADER_SIZE
* 2)) {
6304 pr_err("Failed to allocate PPL header buffer\n");
6310 while (ppl_offset
< MULTIPLE_PPL_AREA_SIZE_IMSM
) {
6313 dprintf("Checking potential PPL at offset: %llu\n", ppl_offset
);
6315 if (lseek64(d
->fd
, info
->ppl_sector
* 512 + ppl_offset
,
6317 perror("Failed to seek to PPL header location");
6322 if (read(d
->fd
, buf
, PPL_HEADER_SIZE
) != PPL_HEADER_SIZE
) {
6323 perror("Read PPL header failed");
6330 crc
= __le32_to_cpu(ppl_hdr
->checksum
);
6331 ppl_hdr
->checksum
= 0;
6333 if (crc
!= ~crc32c_le(~0, buf
, PPL_HEADER_SIZE
)) {
6334 dprintf("Wrong PPL header checksum on %s\n",
6339 if (prev_gen_num
> __le64_to_cpu(ppl_hdr
->generation
)) {
6340 /* previous was newest, it was already checked */
6344 if ((__le32_to_cpu(ppl_hdr
->signature
) !=
6345 super
->anchor
->orig_family_num
)) {
6346 dprintf("Wrong PPL header signature on %s\n",
6353 prev_gen_num
= __le64_to_cpu(ppl_hdr
->generation
);
6355 ppl_offset
+= PPL_HEADER_SIZE
;
6356 for (i
= 0; i
< __le32_to_cpu(ppl_hdr
->entries_count
); i
++)
6358 __le32_to_cpu(ppl_hdr
->entries
[i
].pp_size
);
6361 buf_prev
= buf
+ PPL_HEADER_SIZE
;
6373 * Update metadata to use mutliple PPLs area (1MB).
6374 * This is done once for all RAID members
6376 if (info
->consistency_policy
== CONSISTENCY_POLICY_PPL
&&
6377 info
->ppl_size
!= (MULTIPLE_PPL_AREA_SIZE_IMSM
>> 9)) {
6379 struct mdinfo
*member_dev
;
6381 sprintf(subarray
, "%d", info
->container_member
);
6383 if (mdmon_running(st
->container_devnm
))
6384 st
->update_tail
= &st
->updates
;
6386 if (st
->ss
->update_subarray(st
, subarray
, "ppl", NULL
)) {
6387 pr_err("Failed to update subarray %s\n",
6390 if (st
->update_tail
)
6391 flush_metadata_updates(st
);
6393 st
->ss
->sync_metadata(st
);
6394 info
->ppl_size
= (MULTIPLE_PPL_AREA_SIZE_IMSM
>> 9);
6395 for (member_dev
= info
->devs
; member_dev
;
6396 member_dev
= member_dev
->next
)
6397 member_dev
->ppl_size
=
6398 (MULTIPLE_PPL_AREA_SIZE_IMSM
>> 9);
6403 struct imsm_map
*map
= get_imsm_map(dev
, MAP_X
);
6405 if (map
->map_state
== IMSM_T_STATE_UNINITIALIZED
||
6406 (map
->map_state
== IMSM_T_STATE_NORMAL
&&
6407 !(dev
->vol
.dirty
& RAIDVOL_DIRTY
)) ||
6408 (is_rebuilding(dev
) &&
6409 dev
->vol
.curr_migr_unit
== 0 &&
6410 get_imsm_disk_idx(dev
, disk
->disk
.raid_disk
, MAP_1
) != idx
))
6411 ret
= st
->ss
->write_init_ppl(st
, info
, d
->fd
);
6413 info
->mismatch_cnt
++;
6414 } else if (ret
== 0 &&
6415 ppl_hdr
->entries_count
== 0 &&
6416 is_rebuilding(dev
) &&
6417 info
->resync_start
== 0) {
6419 * The header has no entries - add a single empty entry and
6420 * rewrite the header to prevent the kernel from going into
6421 * resync after an interrupted rebuild.
6423 ppl_hdr
->entries_count
= __cpu_to_le32(1);
6424 ret
= write_ppl_header(info
->ppl_sector
, d
->fd
, buf
);
6432 static int write_init_ppl_imsm_all(struct supertype
*st
, struct mdinfo
*info
)
6434 struct intel_super
*super
= st
->sb
;
6438 if (info
->consistency_policy
!= CONSISTENCY_POLICY_PPL
||
6439 info
->array
.level
!= 5)
6442 for (d
= super
->disks
; d
; d
= d
->next
) {
6443 if (d
->index
< 0 || is_failed(&d
->disk
))
6446 ret
= st
->ss
->write_init_ppl(st
, info
, d
->fd
);
6454 static int write_init_super_imsm(struct supertype
*st
)
6456 struct intel_super
*super
= st
->sb
;
6457 int current_vol
= super
->current_vol
;
6461 getinfo_super_imsm(st
, &info
, NULL
);
6463 /* we are done with current_vol reset it to point st at the container */
6464 super
->current_vol
= -1;
6466 if (st
->update_tail
) {
6467 /* queue the recently created array / added disk
6468 * as a metadata update */
6470 /* determine if we are creating a volume or adding a disk */
6471 if (current_vol
< 0) {
6472 /* in the mgmt (add/remove) disk case we are running
6473 * in mdmon context, so don't close fd's
6477 rv
= write_init_ppl_imsm_all(st
, &info
);
6479 rv
= create_array(st
, current_vol
);
6483 for (d
= super
->disks
; d
; d
= d
->next
)
6484 Kill(d
->devname
, NULL
, 0, -1, 1);
6485 if (current_vol
>= 0)
6486 rv
= write_init_ppl_imsm_all(st
, &info
);
6488 rv
= write_super_imsm(st
, 1);
6494 static int store_super_imsm(struct supertype
*st
, int fd
)
6496 struct intel_super
*super
= st
->sb
;
6497 struct imsm_super
*mpb
= super
? super
->anchor
: NULL
;
6502 if (super
->sector_size
== 4096)
6503 convert_to_4k(super
);
6504 return store_imsm_mpb(fd
, mpb
);
6507 static int validate_geometry_imsm_container(struct supertype
*st
, int level
,
6508 int layout
, int raiddisks
, int chunk
,
6509 unsigned long long size
,
6510 unsigned long long data_offset
,
6512 unsigned long long *freesize
,
6516 unsigned long long ldsize
;
6517 struct intel_super
*super
;
6520 if (level
!= LEVEL_CONTAINER
)
6525 fd
= open(dev
, O_RDONLY
|O_EXCL
, 0);
6528 pr_err("imsm: Cannot open %s: %s\n",
6529 dev
, strerror(errno
));
6532 if (!get_dev_size(fd
, dev
, &ldsize
)) {
6537 /* capabilities retrieve could be possible
6538 * note that there is no fd for the disks in array.
6540 super
= alloc_super();
6545 if (!get_dev_sector_size(fd
, NULL
, &super
->sector_size
)) {
6551 rv
= find_intel_hba_capability(fd
, super
, verbose
> 0 ? dev
: NULL
);
6555 fd2devname(fd
, str
);
6556 dprintf("fd: %d %s orom: %p rv: %d raiddisk: %d\n",
6557 fd
, str
, super
->orom
, rv
, raiddisks
);
6559 /* no orom/efi or non-intel hba of the disk */
6566 if (raiddisks
> super
->orom
->tds
) {
6568 pr_err("%d exceeds maximum number of platform supported disks: %d\n",
6569 raiddisks
, super
->orom
->tds
);
6573 if ((super
->orom
->attr
& IMSM_OROM_ATTR_2TB_DISK
) == 0 &&
6574 (ldsize
>> 9) >> 32 > 0) {
6576 pr_err("%s exceeds maximum platform supported size\n", dev
);
6582 *freesize
= avail_size_imsm(st
, ldsize
>> 9, data_offset
);
6588 static unsigned long long find_size(struct extent
*e
, int *idx
, int num_extents
)
6590 const unsigned long long base_start
= e
[*idx
].start
;
6591 unsigned long long end
= base_start
+ e
[*idx
].size
;
6594 if (base_start
== end
)
6598 for (i
= *idx
; i
< num_extents
; i
++) {
6599 /* extend overlapping extents */
6600 if (e
[i
].start
>= base_start
&&
6601 e
[i
].start
<= end
) {
6604 if (e
[i
].start
+ e
[i
].size
> end
)
6605 end
= e
[i
].start
+ e
[i
].size
;
6606 } else if (e
[i
].start
> end
) {
6612 return end
- base_start
;
6615 static unsigned long long merge_extents(struct intel_super
*super
, int sum_extents
)
6617 /* build a composite disk with all known extents and generate a new
6618 * 'maxsize' given the "all disks in an array must share a common start
6619 * offset" constraint
6621 struct extent
*e
= xcalloc(sum_extents
, sizeof(*e
));
6625 unsigned long long pos
;
6626 unsigned long long start
= 0;
6627 unsigned long long maxsize
;
6628 unsigned long reserve
;
6630 /* coalesce and sort all extents. also, check to see if we need to
6631 * reserve space between member arrays
6634 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
6637 for (i
= 0; i
< dl
->extent_cnt
; i
++)
6640 qsort(e
, sum_extents
, sizeof(*e
), cmp_extent
);
6645 while (i
< sum_extents
) {
6646 e
[j
].start
= e
[i
].start
;
6647 e
[j
].size
= find_size(e
, &i
, sum_extents
);
6649 if (e
[j
-1].size
== 0)
6658 unsigned long long esize
;
6660 esize
= e
[i
].start
- pos
;
6661 if (esize
>= maxsize
) {
6666 pos
= e
[i
].start
+ e
[i
].size
;
6668 } while (e
[i
-1].size
);
6674 /* FIXME assumes volume at offset 0 is the first volume in a
6677 if (start_extent
> 0)
6678 reserve
= IMSM_RESERVED_SECTORS
; /* gap between raid regions */
6682 if (maxsize
< reserve
)
6685 super
->create_offset
= ~((unsigned long long) 0);
6686 if (start
+ reserve
> super
->create_offset
)
6687 return 0; /* start overflows create_offset */
6688 super
->create_offset
= start
+ reserve
;
6690 return maxsize
- reserve
;
6693 static int is_raid_level_supported(const struct imsm_orom
*orom
, int level
, int raiddisks
)
6695 if (level
< 0 || level
== 6 || level
== 4)
6698 /* if we have an orom prevent invalid raid levels */
6701 case 0: return imsm_orom_has_raid0(orom
);
6704 return imsm_orom_has_raid1e(orom
);
6705 return imsm_orom_has_raid1(orom
) && raiddisks
== 2;
6706 case 10: return imsm_orom_has_raid10(orom
) && raiddisks
== 4;
6707 case 5: return imsm_orom_has_raid5(orom
) && raiddisks
> 2;
6710 return 1; /* not on an Intel RAID platform so anything goes */
6716 active_arrays_by_format(char *name
, char* hba
, struct md_list
**devlist
,
6717 int dpa
, int verbose
)
6719 struct mdstat_ent
*mdstat
= mdstat_read(0, 0);
6720 struct mdstat_ent
*memb
;
6726 for (memb
= mdstat
; memb
; memb
= memb
->next
) {
6727 if (memb
->metadata_version
&&
6728 (strncmp(memb
->metadata_version
, "external:", 9) == 0) &&
6729 (strcmp(&memb
->metadata_version
[9], name
) == 0) &&
6730 !is_subarray(memb
->metadata_version
+9) &&
6732 struct dev_member
*dev
= memb
->members
;
6734 while(dev
&& (fd
< 0)) {
6735 char *path
= xmalloc(strlen(dev
->name
) + strlen("/dev/") + 1);
6736 num
= sprintf(path
, "%s%s", "/dev/", dev
->name
);
6738 fd
= open(path
, O_RDONLY
, 0);
6739 if (num
<= 0 || fd
< 0) {
6740 pr_vrb("Cannot open %s: %s\n",
6741 dev
->name
, strerror(errno
));
6747 if (fd
>= 0 && disk_attached_to_hba(fd
, hba
)) {
6748 struct mdstat_ent
*vol
;
6749 for (vol
= mdstat
; vol
; vol
= vol
->next
) {
6750 if (vol
->active
> 0 &&
6751 vol
->metadata_version
&&
6752 is_container_member(vol
, memb
->devnm
)) {
6757 if (*devlist
&& (found
< dpa
)) {
6758 dv
= xcalloc(1, sizeof(*dv
));
6759 dv
->devname
= xmalloc(strlen(memb
->devnm
) + strlen("/dev/") + 1);
6760 sprintf(dv
->devname
, "%s%s", "/dev/", memb
->devnm
);
6763 dv
->next
= *devlist
;
6771 free_mdstat(mdstat
);
6776 static struct md_list
*
6777 get_loop_devices(void)
6780 struct md_list
*devlist
= NULL
;
6783 for(i
= 0; i
< 12; i
++) {
6784 dv
= xcalloc(1, sizeof(*dv
));
6785 dv
->devname
= xmalloc(40);
6786 sprintf(dv
->devname
, "/dev/loop%d", i
);
6794 static struct md_list
*
6795 get_devices(const char *hba_path
)
6797 struct md_list
*devlist
= NULL
;
6804 devlist
= get_loop_devices();
6807 /* scroll through /sys/dev/block looking for devices attached to
6810 dir
= opendir("/sys/dev/block");
6811 for (ent
= dir
? readdir(dir
) : NULL
; ent
; ent
= readdir(dir
)) {
6816 if (sscanf(ent
->d_name
, "%d:%d", &major
, &minor
) != 2)
6818 path
= devt_to_devpath(makedev(major
, minor
));
6821 if (!path_attached_to_hba(path
, hba_path
)) {
6828 fd
= dev_open(ent
->d_name
, O_RDONLY
);
6830 fd2devname(fd
, buf
);
6833 pr_err("cannot open device: %s\n",
6838 dv
= xcalloc(1, sizeof(*dv
));
6839 dv
->devname
= xstrdup(buf
);
6846 devlist
= devlist
->next
;
6856 count_volumes_list(struct md_list
*devlist
, char *homehost
,
6857 int verbose
, int *found
)
6859 struct md_list
*tmpdev
;
6861 struct supertype
*st
;
6863 /* first walk the list of devices to find a consistent set
6864 * that match the criterea, if that is possible.
6865 * We flag the ones we like with 'used'.
6868 st
= match_metadata_desc_imsm("imsm");
6870 pr_vrb("cannot allocate memory for imsm supertype\n");
6874 for (tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
6875 char *devname
= tmpdev
->devname
;
6877 struct supertype
*tst
;
6879 if (tmpdev
->used
> 1)
6881 tst
= dup_super(st
);
6883 pr_vrb("cannot allocate memory for imsm supertype\n");
6886 tmpdev
->container
= 0;
6887 dfd
= dev_open(devname
, O_RDONLY
|O_EXCL
);
6889 dprintf("cannot open device %s: %s\n",
6890 devname
, strerror(errno
));
6892 } else if (!fstat_is_blkdev(dfd
, devname
, &rdev
)) {
6894 } else if (must_be_container(dfd
)) {
6895 struct supertype
*cst
;
6896 cst
= super_by_fd(dfd
, NULL
);
6898 dprintf("cannot recognize container type %s\n",
6901 } else if (tst
->ss
!= st
->ss
) {
6902 dprintf("non-imsm container - ignore it: %s\n",
6905 } else if (!tst
->ss
->load_container
||
6906 tst
->ss
->load_container(tst
, dfd
, NULL
))
6909 tmpdev
->container
= 1;
6912 cst
->ss
->free_super(cst
);
6914 tmpdev
->st_rdev
= rdev
;
6915 if (tst
->ss
->load_super(tst
,dfd
, NULL
)) {
6916 dprintf("no RAID superblock on %s\n",
6919 } else if (tst
->ss
->compare_super
== NULL
) {
6920 dprintf("Cannot assemble %s metadata on %s\n",
6921 tst
->ss
->name
, devname
);
6927 if (tmpdev
->used
== 2 || tmpdev
->used
== 4) {
6928 /* Ignore unrecognised devices during auto-assembly */
6933 tst
->ss
->getinfo_super(tst
, &info
, NULL
);
6935 if (st
->minor_version
== -1)
6936 st
->minor_version
= tst
->minor_version
;
6938 if (memcmp(info
.uuid
, uuid_zero
,
6939 sizeof(int[4])) == 0) {
6940 /* this is a floating spare. It cannot define
6941 * an array unless there are no more arrays of
6942 * this type to be found. It can be included
6943 * in an array of this type though.
6949 if (st
->ss
!= tst
->ss
||
6950 st
->minor_version
!= tst
->minor_version
||
6951 st
->ss
->compare_super(st
, tst
) != 0) {
6952 /* Some mismatch. If exactly one array matches this host,
6953 * we can resolve on that one.
6954 * Or, if we are auto assembling, we just ignore the second
6957 dprintf("superblock on %s doesn't match others - assembly aborted\n",
6963 dprintf("found: devname: %s\n", devname
);
6967 tst
->ss
->free_super(tst
);
6971 if ((err
= load_super_imsm_all(st
, -1, &st
->sb
, NULL
, devlist
, 0)) == 0) {
6972 struct mdinfo
*iter
, *head
= st
->ss
->container_content(st
, NULL
);
6973 for (iter
= head
; iter
; iter
= iter
->next
) {
6974 dprintf("content->text_version: %s vol\n",
6975 iter
->text_version
);
6976 if (iter
->array
.state
& (1<<MD_SB_BLOCK_VOLUME
)) {
6977 /* do not assemble arrays with unsupported
6979 dprintf("Cannot activate member %s.\n",
6980 iter
->text_version
);
6987 dprintf("No valid super block on device list: err: %d %p\n",
6991 dprintf("no more devices to examine\n");
6994 for (tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
6995 if (tmpdev
->used
== 1 && tmpdev
->found
) {
6997 if (count
< tmpdev
->found
)
7000 count
-= tmpdev
->found
;
7003 if (tmpdev
->used
== 1)
7008 st
->ss
->free_super(st
);
7012 static int __count_volumes(char *hba_path
, int dpa
, int verbose
,
7015 struct sys_dev
*idev
, *intel_devices
= find_intel_devices();
7017 const struct orom_entry
*entry
;
7018 struct devid_list
*dv
, *devid_list
;
7023 for (idev
= intel_devices
; idev
; idev
= idev
->next
) {
7024 if (strstr(idev
->path
, hba_path
))
7028 if (!idev
|| !idev
->dev_id
)
7031 entry
= get_orom_entry_by_device_id(idev
->dev_id
);
7033 if (!entry
|| !entry
->devid_list
)
7036 devid_list
= entry
->devid_list
;
7037 for (dv
= devid_list
; dv
; dv
= dv
->next
) {
7038 struct md_list
*devlist
;
7039 struct sys_dev
*device
= NULL
;
7044 device
= device_by_id_and_path(dv
->devid
, hba_path
);
7046 device
= device_by_id(dv
->devid
);
7049 hpath
= device
->path
;
7053 devlist
= get_devices(hpath
);
7054 /* if no intel devices return zero volumes */
7055 if (devlist
== NULL
)
7058 count
+= active_arrays_by_format("imsm", hpath
, &devlist
, dpa
,
7060 dprintf("path: %s active arrays: %d\n", hpath
, count
);
7061 if (devlist
== NULL
)
7065 count
+= count_volumes_list(devlist
,
7069 dprintf("found %d count: %d\n", found
, count
);
7072 dprintf("path: %s total number of volumes: %d\n", hpath
, count
);
7075 struct md_list
*dv
= devlist
;
7076 devlist
= devlist
->next
;
7084 static int count_volumes(struct intel_hba
*hba
, int dpa
, int verbose
)
7088 if (hba
->type
== SYS_DEV_VMD
) {
7089 struct sys_dev
*dev
;
7092 for (dev
= find_intel_devices(); dev
; dev
= dev
->next
) {
7093 if (dev
->type
== SYS_DEV_VMD
)
7094 count
+= __count_volumes(dev
->path
, dpa
,
7099 return __count_volumes(hba
->path
, dpa
, verbose
, 0);
7102 static int imsm_default_chunk(const struct imsm_orom
*orom
)
7104 /* up to 512 if the plaform supports it, otherwise the platform max.
7105 * 128 if no platform detected
7107 int fs
= max(7, orom
? fls(orom
->sss
) : 0);
7109 return min(512, (1 << fs
));
7113 validate_geometry_imsm_orom(struct intel_super
*super
, int level
, int layout
,
7114 int raiddisks
, int *chunk
, unsigned long long size
, int verbose
)
7116 /* check/set platform and metadata limits/defaults */
7117 if (super
->orom
&& raiddisks
> super
->orom
->dpa
) {
7118 pr_vrb("platform supports a maximum of %d disks per array\n",
7123 /* capabilities of OROM tested - copied from validate_geometry_imsm_volume */
7124 if (!is_raid_level_supported(super
->orom
, level
, raiddisks
)) {
7125 pr_vrb("platform does not support raid%d with %d disk%s\n",
7126 level
, raiddisks
, raiddisks
> 1 ? "s" : "");
7130 if (*chunk
== 0 || *chunk
== UnSet
)
7131 *chunk
= imsm_default_chunk(super
->orom
);
7133 if (super
->orom
&& !imsm_orom_has_chunk(super
->orom
, *chunk
)) {
7134 pr_vrb("platform does not support a chunk size of: %d\n", *chunk
);
7138 if (layout
!= imsm_level_to_layout(level
)) {
7140 pr_vrb("imsm raid 5 only supports the left-asymmetric layout\n");
7141 else if (level
== 10)
7142 pr_vrb("imsm raid 10 only supports the n2 layout\n");
7144 pr_vrb("imsm unknown layout %#x for this raid level %d\n",
7149 if (super
->orom
&& (super
->orom
->attr
& IMSM_OROM_ATTR_2TB
) == 0 &&
7150 (calc_array_size(level
, raiddisks
, layout
, *chunk
, size
) >> 32) > 0) {
7151 pr_vrb("platform does not support a volume size over 2TB\n");
7158 /* validate_geometry_imsm_volume - lifted from validate_geometry_ddf_bvd
7159 * FIX ME add ahci details
7161 static int validate_geometry_imsm_volume(struct supertype
*st
, int level
,
7162 int layout
, int raiddisks
, int *chunk
,
7163 unsigned long long size
,
7164 unsigned long long data_offset
,
7166 unsigned long long *freesize
,
7170 struct intel_super
*super
= st
->sb
;
7171 struct imsm_super
*mpb
;
7173 unsigned long long pos
= 0;
7174 unsigned long long maxsize
;
7178 /* We must have the container info already read in. */
7182 mpb
= super
->anchor
;
7184 if (!validate_geometry_imsm_orom(super
, level
, layout
, raiddisks
, chunk
, size
, verbose
)) {
7185 pr_err("RAID geometry validation failed. Cannot proceed with the action(s).\n");
7189 /* General test: make sure there is space for
7190 * 'raiddisks' device extents of size 'size' at a given
7193 unsigned long long minsize
= size
;
7194 unsigned long long start_offset
= MaxSector
;
7197 minsize
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
7198 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
7203 e
= get_extents(super
, dl
, 0);
7206 unsigned long long esize
;
7207 esize
= e
[i
].start
- pos
;
7208 if (esize
>= minsize
)
7210 if (found
&& start_offset
== MaxSector
) {
7213 } else if (found
&& pos
!= start_offset
) {
7217 pos
= e
[i
].start
+ e
[i
].size
;
7219 } while (e
[i
-1].size
);
7224 if (dcnt
< raiddisks
) {
7226 pr_err("imsm: Not enough devices with space for this array (%d < %d)\n",
7233 /* This device must be a member of the set */
7234 if (!stat_is_blkdev(dev
, &rdev
))
7236 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
7237 if (dl
->major
== (int)major(rdev
) &&
7238 dl
->minor
== (int)minor(rdev
))
7243 pr_err("%s is not in the same imsm set\n", dev
);
7245 } else if (super
->orom
&& dl
->index
< 0 && mpb
->num_raid_devs
) {
7246 /* If a volume is present then the current creation attempt
7247 * cannot incorporate new spares because the orom may not
7248 * understand this configuration (all member disks must be
7249 * members of each array in the container).
7251 pr_err("%s is a spare and a volume is already defined for this container\n", dev
);
7252 pr_err("The option-rom requires all member disks to be a member of all volumes\n");
7254 } else if (super
->orom
&& mpb
->num_raid_devs
> 0 &&
7255 mpb
->num_disks
!= raiddisks
) {
7256 pr_err("The option-rom requires all member disks to be a member of all volumes\n");
7260 /* retrieve the largest free space block */
7261 e
= get_extents(super
, dl
, 0);
7266 unsigned long long esize
;
7268 esize
= e
[i
].start
- pos
;
7269 if (esize
>= maxsize
)
7271 pos
= e
[i
].start
+ e
[i
].size
;
7273 } while (e
[i
-1].size
);
7278 pr_err("unable to determine free space for: %s\n",
7282 if (maxsize
< size
) {
7284 pr_err("%s not enough space (%llu < %llu)\n",
7285 dev
, maxsize
, size
);
7289 /* count total number of extents for merge */
7291 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
7293 i
+= dl
->extent_cnt
;
7295 maxsize
= merge_extents(super
, i
);
7297 if (mpb
->num_raid_devs
> 0 && size
&& size
!= maxsize
)
7298 pr_err("attempting to create a second volume with size less then remaining space.\n");
7300 if (maxsize
< size
|| maxsize
== 0) {
7303 pr_err("no free space left on device. Aborting...\n");
7305 pr_err("not enough space to create volume of given size (%llu < %llu). Aborting...\n",
7311 *freesize
= maxsize
;
7314 int count
= count_volumes(super
->hba
,
7315 super
->orom
->dpa
, verbose
);
7316 if (super
->orom
->vphba
<= count
) {
7317 pr_vrb("platform does not support more than %d raid volumes.\n",
7318 super
->orom
->vphba
);
7325 static int imsm_get_free_size(struct supertype
*st
, int raiddisks
,
7326 unsigned long long size
, int chunk
,
7327 unsigned long long *freesize
)
7329 struct intel_super
*super
= st
->sb
;
7330 struct imsm_super
*mpb
= super
->anchor
;
7335 unsigned long long maxsize
;
7336 unsigned long long minsize
;
7340 /* find the largest common start free region of the possible disks */
7344 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
7350 /* don't activate new spares if we are orom constrained
7351 * and there is already a volume active in the container
7353 if (super
->orom
&& dl
->index
< 0 && mpb
->num_raid_devs
)
7356 e
= get_extents(super
, dl
, 0);
7359 for (i
= 1; e
[i
-1].size
; i
++)
7367 maxsize
= merge_extents(super
, extent_cnt
);
7371 minsize
= chunk
* 2;
7373 if (cnt
< raiddisks
||
7374 (super
->orom
&& used
&& used
!= raiddisks
) ||
7375 maxsize
< minsize
||
7377 pr_err("not enough devices with space to create array.\n");
7378 return 0; /* No enough free spaces large enough */
7389 if (mpb
->num_raid_devs
> 0 && size
&& size
!= maxsize
)
7390 pr_err("attempting to create a second volume with size less then remaining space.\n");
7392 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
7394 dl
->raiddisk
= cnt
++;
7398 dprintf("imsm: imsm_get_free_size() returns : %llu\n", size
);
7403 static int reserve_space(struct supertype
*st
, int raiddisks
,
7404 unsigned long long size
, int chunk
,
7405 unsigned long long *freesize
)
7407 struct intel_super
*super
= st
->sb
;
7412 rv
= imsm_get_free_size(st
, raiddisks
, size
, chunk
, freesize
);
7415 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
7417 dl
->raiddisk
= cnt
++;
7424 static int validate_geometry_imsm(struct supertype
*st
, int level
, int layout
,
7425 int raiddisks
, int *chunk
, unsigned long long size
,
7426 unsigned long long data_offset
,
7427 char *dev
, unsigned long long *freesize
,
7428 int consistency_policy
, int verbose
)
7435 * if given unused devices create a container
7436 * if given given devices in a container create a member volume
7438 if (level
== LEVEL_CONTAINER
) {
7439 /* Must be a fresh device to add to a container */
7440 return validate_geometry_imsm_container(st
, level
, layout
,
7449 * Size is given in sectors.
7451 if (size
&& (size
< 2048)) {
7452 pr_err("Given size must be greater than 1M.\n");
7453 /* Depends on algorithm in Create.c :
7454 * if container was given (dev == NULL) return -1,
7455 * if block device was given ( dev != NULL) return 0.
7457 return dev
? -1 : 0;
7462 struct intel_super
*super
= st
->sb
;
7463 if (!validate_geometry_imsm_orom(st
->sb
, level
, layout
,
7464 raiddisks
, chunk
, size
,
7467 /* we are being asked to automatically layout a
7468 * new volume based on the current contents of
7469 * the container. If the the parameters can be
7470 * satisfied reserve_space will record the disks,
7471 * start offset, and size of the volume to be
7472 * created. add_to_super and getinfo_super
7473 * detect when autolayout is in progress.
7475 /* assuming that freesize is always given when array is
7477 if (super
->orom
&& freesize
) {
7479 count
= count_volumes(super
->hba
,
7480 super
->orom
->dpa
, verbose
);
7481 if (super
->orom
->vphba
<= count
) {
7482 pr_vrb("platform does not support more than %d raid volumes.\n",
7483 super
->orom
->vphba
);
7488 return reserve_space(st
, raiddisks
, size
,
7494 /* creating in a given container */
7495 return validate_geometry_imsm_volume(st
, level
, layout
,
7496 raiddisks
, chunk
, size
,
7498 dev
, freesize
, verbose
);
7501 /* This device needs to be a device in an 'imsm' container */
7502 fd
= open(dev
, O_RDONLY
|O_EXCL
, 0);
7505 pr_err("Cannot create this array on device %s\n",
7510 if (errno
!= EBUSY
|| (fd
= open(dev
, O_RDONLY
, 0)) < 0) {
7512 pr_err("Cannot open %s: %s\n",
7513 dev
, strerror(errno
));
7516 /* Well, it is in use by someone, maybe an 'imsm' container. */
7517 cfd
= open_container(fd
);
7521 pr_err("Cannot use %s: It is busy\n",
7525 sra
= sysfs_read(cfd
, NULL
, GET_VERSION
);
7526 if (sra
&& sra
->array
.major_version
== -1 &&
7527 strcmp(sra
->text_version
, "imsm") == 0)
7531 /* This is a member of a imsm container. Load the container
7532 * and try to create a volume
7534 struct intel_super
*super
;
7536 if (load_super_imsm_all(st
, cfd
, (void **) &super
, NULL
, NULL
, 1) == 0) {
7538 strcpy(st
->container_devnm
, fd2devnm(cfd
));
7540 return validate_geometry_imsm_volume(st
, level
, layout
,
7542 size
, data_offset
, dev
,
7549 pr_err("failed container membership check\n");
7555 static void default_geometry_imsm(struct supertype
*st
, int *level
, int *layout
, int *chunk
)
7557 struct intel_super
*super
= st
->sb
;
7559 if (level
&& *level
== UnSet
)
7560 *level
= LEVEL_CONTAINER
;
7562 if (level
&& layout
&& *layout
== UnSet
)
7563 *layout
= imsm_level_to_layout(*level
);
7565 if (chunk
&& (*chunk
== UnSet
|| *chunk
== 0))
7566 *chunk
= imsm_default_chunk(super
->orom
);
7569 static void handle_missing(struct intel_super
*super
, struct imsm_dev
*dev
);
7571 static int kill_subarray_imsm(struct supertype
*st
, char *subarray_id
)
7573 /* remove the subarray currently referenced by subarray_id */
7575 struct intel_dev
**dp
;
7576 struct intel_super
*super
= st
->sb
;
7577 __u8 current_vol
= strtoul(subarray_id
, NULL
, 10);
7578 struct imsm_super
*mpb
= super
->anchor
;
7580 if (mpb
->num_raid_devs
== 0)
7583 /* block deletions that would change the uuid of active subarrays
7585 * FIXME when immutable ids are available, but note that we'll
7586 * also need to fixup the invalidated/active subarray indexes in
7589 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
7592 if (i
< current_vol
)
7594 sprintf(subarray
, "%u", i
);
7595 if (is_subarray_active(subarray
, st
->devnm
)) {
7596 pr_err("deleting subarray-%d would change the UUID of active subarray-%d, aborting\n",
7603 if (st
->update_tail
) {
7604 struct imsm_update_kill_array
*u
= xmalloc(sizeof(*u
));
7606 u
->type
= update_kill_array
;
7607 u
->dev_idx
= current_vol
;
7608 append_metadata_update(st
, u
, sizeof(*u
));
7613 for (dp
= &super
->devlist
; *dp
;)
7614 if ((*dp
)->index
== current_vol
) {
7617 handle_missing(super
, (*dp
)->dev
);
7618 if ((*dp
)->index
> current_vol
)
7623 /* no more raid devices, all active components are now spares,
7624 * but of course failed are still failed
7626 if (--mpb
->num_raid_devs
== 0) {
7629 for (d
= super
->disks
; d
; d
= d
->next
)
7634 super
->updates_pending
++;
7639 static int update_subarray_imsm(struct supertype
*st
, char *subarray
,
7640 char *update
, struct mddev_ident
*ident
)
7642 /* update the subarray currently referenced by ->current_vol */
7643 struct intel_super
*super
= st
->sb
;
7644 struct imsm_super
*mpb
= super
->anchor
;
7646 if (strcmp(update
, "name") == 0) {
7647 char *name
= ident
->name
;
7651 if (is_subarray_active(subarray
, st
->devnm
)) {
7652 pr_err("Unable to update name of active subarray\n");
7656 if (!check_name(super
, name
, 0))
7659 vol
= strtoul(subarray
, &ep
, 10);
7660 if (*ep
!= '\0' || vol
>= super
->anchor
->num_raid_devs
)
7663 if (st
->update_tail
) {
7664 struct imsm_update_rename_array
*u
= xmalloc(sizeof(*u
));
7666 u
->type
= update_rename_array
;
7668 strncpy((char *) u
->name
, name
, MAX_RAID_SERIAL_LEN
);
7669 u
->name
[MAX_RAID_SERIAL_LEN
-1] = '\0';
7670 append_metadata_update(st
, u
, sizeof(*u
));
7672 struct imsm_dev
*dev
;
7675 dev
= get_imsm_dev(super
, vol
);
7676 memset(dev
->volume
, '\0', MAX_RAID_SERIAL_LEN
);
7677 namelen
= min((int)strlen(name
), MAX_RAID_SERIAL_LEN
);
7678 memcpy(dev
->volume
, name
, namelen
);
7679 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
7680 dev
= get_imsm_dev(super
, i
);
7681 handle_missing(super
, dev
);
7683 super
->updates_pending
++;
7685 } else if (strcmp(update
, "ppl") == 0 ||
7686 strcmp(update
, "no-ppl") == 0) {
7689 int vol
= strtoul(subarray
, &ep
, 10);
7691 if (*ep
!= '\0' || vol
>= super
->anchor
->num_raid_devs
)
7694 if (strcmp(update
, "ppl") == 0)
7695 new_policy
= RWH_MULTIPLE_DISTRIBUTED
;
7697 new_policy
= RWH_MULTIPLE_OFF
;
7699 if (st
->update_tail
) {
7700 struct imsm_update_rwh_policy
*u
= xmalloc(sizeof(*u
));
7702 u
->type
= update_rwh_policy
;
7704 u
->new_policy
= new_policy
;
7705 append_metadata_update(st
, u
, sizeof(*u
));
7707 struct imsm_dev
*dev
;
7709 dev
= get_imsm_dev(super
, vol
);
7710 dev
->rwh_policy
= new_policy
;
7711 super
->updates_pending
++;
7719 static int is_gen_migration(struct imsm_dev
*dev
)
7724 if (!dev
->vol
.migr_state
)
7727 if (migr_type(dev
) == MIGR_GEN_MIGR
)
7733 static int is_rebuilding(struct imsm_dev
*dev
)
7735 struct imsm_map
*migr_map
;
7737 if (!dev
->vol
.migr_state
)
7740 if (migr_type(dev
) != MIGR_REBUILD
)
7743 migr_map
= get_imsm_map(dev
, MAP_1
);
7745 if (migr_map
->map_state
== IMSM_T_STATE_DEGRADED
)
7751 static int is_initializing(struct imsm_dev
*dev
)
7753 struct imsm_map
*migr_map
;
7755 if (!dev
->vol
.migr_state
)
7758 if (migr_type(dev
) != MIGR_INIT
)
7761 migr_map
= get_imsm_map(dev
, MAP_1
);
7763 if (migr_map
->map_state
== IMSM_T_STATE_UNINITIALIZED
)
7769 static void update_recovery_start(struct intel_super
*super
,
7770 struct imsm_dev
*dev
,
7771 struct mdinfo
*array
)
7773 struct mdinfo
*rebuild
= NULL
;
7777 if (!is_rebuilding(dev
))
7780 /* Find the rebuild target, but punt on the dual rebuild case */
7781 for (d
= array
->devs
; d
; d
= d
->next
)
7782 if (d
->recovery_start
== 0) {
7789 /* (?) none of the disks are marked with
7790 * IMSM_ORD_REBUILD, so assume they are missing and the
7791 * disk_ord_tbl was not correctly updated
7793 dprintf("failed to locate out-of-sync disk\n");
7797 units
= __le32_to_cpu(dev
->vol
.curr_migr_unit
);
7798 rebuild
->recovery_start
= units
* blocks_per_migr_unit(super
, dev
);
7801 static int recover_backup_imsm(struct supertype
*st
, struct mdinfo
*info
);
7803 static struct mdinfo
*container_content_imsm(struct supertype
*st
, char *subarray
)
7805 /* Given a container loaded by load_super_imsm_all,
7806 * extract information about all the arrays into
7808 * If 'subarray' is given, just extract info about that array.
7810 * For each imsm_dev create an mdinfo, fill it in,
7811 * then look for matching devices in super->disks
7812 * and create appropriate device mdinfo.
7814 struct intel_super
*super
= st
->sb
;
7815 struct imsm_super
*mpb
= super
->anchor
;
7816 struct mdinfo
*rest
= NULL
;
7820 int spare_disks
= 0;
7821 int current_vol
= super
->current_vol
;
7823 /* do not assemble arrays when not all attributes are supported */
7824 if (imsm_check_attributes(mpb
->attributes
) == 0) {
7826 pr_err("Unsupported attributes in IMSM metadata.Arrays activation is blocked.\n");
7829 /* count spare devices, not used in maps
7831 for (d
= super
->disks
; d
; d
= d
->next
)
7835 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
7836 struct imsm_dev
*dev
;
7837 struct imsm_map
*map
;
7838 struct imsm_map
*map2
;
7839 struct mdinfo
*this;
7846 (i
!= strtoul(subarray
, &ep
, 10) || *ep
!= '\0'))
7849 dev
= get_imsm_dev(super
, i
);
7850 map
= get_imsm_map(dev
, MAP_0
);
7851 map2
= get_imsm_map(dev
, MAP_1
);
7852 level
= get_imsm_raid_level(map
);
7854 /* do not publish arrays that are in the middle of an
7855 * unsupported migration
7857 if (dev
->vol
.migr_state
&&
7858 (migr_type(dev
) == MIGR_STATE_CHANGE
)) {
7859 pr_err("cannot assemble volume '%.16s': unsupported migration in progress\n",
7863 /* do not publish arrays that are not support by controller's
7867 this = xmalloc(sizeof(*this));
7869 super
->current_vol
= i
;
7870 getinfo_super_imsm_volume(st
, this, NULL
);
7872 chunk
= __le16_to_cpu(map
->blocks_per_strip
) >> 1;
7873 /* mdadm does not support all metadata features- set the bit in all arrays state */
7874 if (!validate_geometry_imsm_orom(super
,
7875 level
, /* RAID level */
7876 imsm_level_to_layout(level
),
7877 map
->num_members
, /* raid disks */
7878 &chunk
, imsm_dev_size(dev
),
7880 pr_err("IMSM RAID geometry validation failed. Array %s activation is blocked.\n",
7882 this->array
.state
|=
7883 (1<<MD_SB_BLOCK_CONTAINER_RESHAPE
) |
7884 (1<<MD_SB_BLOCK_VOLUME
);
7887 /* if array has bad blocks, set suitable bit in all arrays state */
7889 this->array
.state
|=
7890 (1<<MD_SB_BLOCK_CONTAINER_RESHAPE
) |
7891 (1<<MD_SB_BLOCK_VOLUME
);
7893 for (slot
= 0 ; slot
< map
->num_members
; slot
++) {
7894 unsigned long long recovery_start
;
7895 struct mdinfo
*info_d
;
7903 idx
= get_imsm_disk_idx(dev
, slot
, MAP_0
);
7904 ord
= get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
);
7905 for (d
= super
->disks
; d
; d
= d
->next
)
7906 if (d
->index
== idx
)
7909 recovery_start
= MaxSector
;
7912 if (d
&& is_failed(&d
->disk
))
7914 if (!skip
&& (ord
& IMSM_ORD_REBUILD
))
7916 if (!(ord
& IMSM_ORD_REBUILD
))
7917 this->array
.working_disks
++;
7919 * if we skip some disks the array will be assmebled degraded;
7920 * reset resync start to avoid a dirty-degraded
7921 * situation when performing the intial sync
7926 if (!(dev
->vol
.dirty
& RAIDVOL_DIRTY
)) {
7927 if ((!able_to_resync(level
, missing
) ||
7928 recovery_start
== 0))
7929 this->resync_start
= MaxSector
;
7932 * FIXME handle dirty degraded
7939 info_d
= xcalloc(1, sizeof(*info_d
));
7940 info_d
->next
= this->devs
;
7941 this->devs
= info_d
;
7943 info_d
->disk
.number
= d
->index
;
7944 info_d
->disk
.major
= d
->major
;
7945 info_d
->disk
.minor
= d
->minor
;
7946 info_d
->disk
.raid_disk
= slot
;
7947 info_d
->recovery_start
= recovery_start
;
7949 if (slot
< map2
->num_members
)
7950 info_d
->disk
.state
= (1 << MD_DISK_ACTIVE
);
7952 this->array
.spare_disks
++;
7954 if (slot
< map
->num_members
)
7955 info_d
->disk
.state
= (1 << MD_DISK_ACTIVE
);
7957 this->array
.spare_disks
++;
7960 info_d
->events
= __le32_to_cpu(mpb
->generation_num
);
7961 info_d
->data_offset
= pba_of_lba0(map
);
7962 info_d
->component_size
= calc_component_size(map
, dev
);
7964 if (map
->raid_level
== 5) {
7965 info_d
->ppl_sector
= this->ppl_sector
;
7966 info_d
->ppl_size
= this->ppl_size
;
7967 if (this->consistency_policy
== CONSISTENCY_POLICY_PPL
&&
7968 recovery_start
== 0)
7969 this->resync_start
= 0;
7972 info_d
->bb
.supported
= 1;
7973 get_volume_badblocks(super
->bbm_log
, ord_to_idx(ord
),
7974 info_d
->data_offset
,
7975 info_d
->component_size
,
7978 /* now that the disk list is up-to-date fixup recovery_start */
7979 update_recovery_start(super
, dev
, this);
7980 this->array
.spare_disks
+= spare_disks
;
7982 /* check for reshape */
7983 if (this->reshape_active
== 1)
7984 recover_backup_imsm(st
, this);
7988 super
->current_vol
= current_vol
;
7992 static __u8
imsm_check_degraded(struct intel_super
*super
, struct imsm_dev
*dev
,
7993 int failed
, int look_in_map
)
7995 struct imsm_map
*map
;
7997 map
= get_imsm_map(dev
, look_in_map
);
8000 return map
->map_state
== IMSM_T_STATE_UNINITIALIZED
?
8001 IMSM_T_STATE_UNINITIALIZED
: IMSM_T_STATE_NORMAL
;
8003 switch (get_imsm_raid_level(map
)) {
8005 return IMSM_T_STATE_FAILED
;
8008 if (failed
< map
->num_members
)
8009 return IMSM_T_STATE_DEGRADED
;
8011 return IMSM_T_STATE_FAILED
;
8016 * check to see if any mirrors have failed, otherwise we
8017 * are degraded. Even numbered slots are mirrored on
8021 /* gcc -Os complains that this is unused */
8022 int insync
= insync
;
8024 for (i
= 0; i
< map
->num_members
; i
++) {
8025 __u32 ord
= get_imsm_ord_tbl_ent(dev
, i
, MAP_X
);
8026 int idx
= ord_to_idx(ord
);
8027 struct imsm_disk
*disk
;
8029 /* reset the potential in-sync count on even-numbered
8030 * slots. num_copies is always 2 for imsm raid10
8035 disk
= get_imsm_disk(super
, idx
);
8036 if (!disk
|| is_failed(disk
) || ord
& IMSM_ORD_REBUILD
)
8039 /* no in-sync disks left in this mirror the
8043 return IMSM_T_STATE_FAILED
;
8046 return IMSM_T_STATE_DEGRADED
;
8050 return IMSM_T_STATE_DEGRADED
;
8052 return IMSM_T_STATE_FAILED
;
8058 return map
->map_state
;
8061 static int imsm_count_failed(struct intel_super
*super
, struct imsm_dev
*dev
,
8066 struct imsm_disk
*disk
;
8067 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8068 struct imsm_map
*prev
= get_imsm_map(dev
, MAP_1
);
8069 struct imsm_map
*map_for_loop
;
8074 /* at the beginning of migration we set IMSM_ORD_REBUILD on
8075 * disks that are being rebuilt. New failures are recorded to
8076 * map[0]. So we look through all the disks we started with and
8077 * see if any failures are still present, or if any new ones
8081 if (prev
&& (map
->num_members
< prev
->num_members
))
8082 map_for_loop
= prev
;
8084 for (i
= 0; i
< map_for_loop
->num_members
; i
++) {
8086 /* when MAP_X is passed both maps failures are counted
8089 (look_in_map
== MAP_1
|| look_in_map
== MAP_X
) &&
8090 i
< prev
->num_members
) {
8091 ord
= __le32_to_cpu(prev
->disk_ord_tbl
[i
]);
8092 idx_1
= ord_to_idx(ord
);
8094 disk
= get_imsm_disk(super
, idx_1
);
8095 if (!disk
|| is_failed(disk
) || ord
& IMSM_ORD_REBUILD
)
8098 if ((look_in_map
== MAP_0
|| look_in_map
== MAP_X
) &&
8099 i
< map
->num_members
) {
8100 ord
= __le32_to_cpu(map
->disk_ord_tbl
[i
]);
8101 idx
= ord_to_idx(ord
);
8104 disk
= get_imsm_disk(super
, idx
);
8105 if (!disk
|| is_failed(disk
) ||
8106 ord
& IMSM_ORD_REBUILD
)
8115 static int imsm_open_new(struct supertype
*c
, struct active_array
*a
,
8118 struct intel_super
*super
= c
->sb
;
8119 struct imsm_super
*mpb
= super
->anchor
;
8120 struct imsm_update_prealloc_bb_mem u
;
8122 if (atoi(inst
) >= mpb
->num_raid_devs
) {
8123 pr_err("subarry index %d, out of range\n", atoi(inst
));
8127 dprintf("imsm: open_new %s\n", inst
);
8128 a
->info
.container_member
= atoi(inst
);
8130 u
.type
= update_prealloc_badblocks_mem
;
8131 imsm_update_metadata_locally(c
, &u
, sizeof(u
));
8136 static int is_resyncing(struct imsm_dev
*dev
)
8138 struct imsm_map
*migr_map
;
8140 if (!dev
->vol
.migr_state
)
8143 if (migr_type(dev
) == MIGR_INIT
||
8144 migr_type(dev
) == MIGR_REPAIR
)
8147 if (migr_type(dev
) == MIGR_GEN_MIGR
)
8150 migr_map
= get_imsm_map(dev
, MAP_1
);
8152 if (migr_map
->map_state
== IMSM_T_STATE_NORMAL
&&
8153 dev
->vol
.migr_type
!= MIGR_GEN_MIGR
)
8159 /* return true if we recorded new information */
8160 static int mark_failure(struct intel_super
*super
,
8161 struct imsm_dev
*dev
, struct imsm_disk
*disk
, int idx
)
8165 struct imsm_map
*map
;
8166 char buf
[MAX_RAID_SERIAL_LEN
+3];
8167 unsigned int len
, shift
= 0;
8169 /* new failures are always set in map[0] */
8170 map
= get_imsm_map(dev
, MAP_0
);
8172 slot
= get_imsm_disk_slot(map
, idx
);
8176 ord
= __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
8177 if (is_failed(disk
) && (ord
& IMSM_ORD_REBUILD
))
8180 memcpy(buf
, disk
->serial
, MAX_RAID_SERIAL_LEN
);
8181 buf
[MAX_RAID_SERIAL_LEN
] = '\000';
8183 if ((len
= strlen(buf
)) >= MAX_RAID_SERIAL_LEN
)
8184 shift
= len
- MAX_RAID_SERIAL_LEN
+ 1;
8185 memcpy(disk
->serial
, &buf
[shift
], len
+ 1 - shift
);
8187 disk
->status
|= FAILED_DISK
;
8188 set_imsm_ord_tbl_ent(map
, slot
, idx
| IMSM_ORD_REBUILD
);
8189 /* mark failures in second map if second map exists and this disk
8191 * This is valid for migration, initialization and rebuild
8193 if (dev
->vol
.migr_state
) {
8194 struct imsm_map
*map2
= get_imsm_map(dev
, MAP_1
);
8195 int slot2
= get_imsm_disk_slot(map2
, idx
);
8197 if (slot2
< map2
->num_members
&& slot2
>= 0)
8198 set_imsm_ord_tbl_ent(map2
, slot2
,
8199 idx
| IMSM_ORD_REBUILD
);
8201 if (map
->failed_disk_num
== 0xff ||
8202 (!is_rebuilding(dev
) && map
->failed_disk_num
> slot
))
8203 map
->failed_disk_num
= slot
;
8205 clear_disk_badblocks(super
->bbm_log
, ord_to_idx(ord
));
8210 static void mark_missing(struct intel_super
*super
,
8211 struct imsm_dev
*dev
, struct imsm_disk
*disk
, int idx
)
8213 mark_failure(super
, dev
, disk
, idx
);
8215 if (disk
->scsi_id
== __cpu_to_le32(~(__u32
)0))
8218 disk
->scsi_id
= __cpu_to_le32(~(__u32
)0);
8219 memmove(&disk
->serial
[0], &disk
->serial
[1], MAX_RAID_SERIAL_LEN
- 1);
8222 static void handle_missing(struct intel_super
*super
, struct imsm_dev
*dev
)
8226 if (!super
->missing
)
8229 /* When orom adds replacement for missing disk it does
8230 * not remove entry of missing disk, but just updates map with
8231 * new added disk. So it is not enough just to test if there is
8232 * any missing disk, we have to look if there are any failed disks
8233 * in map to stop migration */
8235 dprintf("imsm: mark missing\n");
8236 /* end process for initialization and rebuild only
8238 if (is_gen_migration(dev
) == 0) {
8239 int failed
= imsm_count_failed(super
, dev
, MAP_0
);
8243 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8244 struct imsm_map
*map1
;
8245 int i
, ord
, ord_map1
;
8248 for (i
= 0; i
< map
->num_members
; i
++) {
8249 ord
= get_imsm_ord_tbl_ent(dev
, i
, MAP_0
);
8250 if (!(ord
& IMSM_ORD_REBUILD
))
8253 map1
= get_imsm_map(dev
, MAP_1
);
8257 ord_map1
= __le32_to_cpu(map1
->disk_ord_tbl
[i
]);
8258 if (ord_map1
& IMSM_ORD_REBUILD
)
8263 map_state
= imsm_check_degraded(super
, dev
,
8265 end_migration(dev
, super
, map_state
);
8269 for (dl
= super
->missing
; dl
; dl
= dl
->next
)
8270 mark_missing(super
, dev
, &dl
->disk
, dl
->index
);
8271 super
->updates_pending
++;
8274 static unsigned long long imsm_set_array_size(struct imsm_dev
*dev
,
8277 unsigned long long array_blocks
;
8278 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8279 int used_disks
= imsm_num_data_members(map
);
8281 if (used_disks
== 0) {
8282 /* when problems occures
8283 * return current array_blocks value
8285 array_blocks
= imsm_dev_size(dev
);
8287 return array_blocks
;
8290 /* set array size in metadata
8293 /* OLCE size change is caused by added disks
8295 array_blocks
= per_dev_array_size(map
) * used_disks
;
8297 /* Online Volume Size Change
8298 * Using available free space
8300 array_blocks
= new_size
;
8302 array_blocks
= round_size_to_mb(array_blocks
, used_disks
);
8303 set_imsm_dev_size(dev
, array_blocks
);
8305 return array_blocks
;
8308 static void imsm_set_disk(struct active_array
*a
, int n
, int state
);
8310 static void imsm_progress_container_reshape(struct intel_super
*super
)
8312 /* if no device has a migr_state, but some device has a
8313 * different number of members than the previous device, start
8314 * changing the number of devices in this device to match
8317 struct imsm_super
*mpb
= super
->anchor
;
8318 int prev_disks
= -1;
8322 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
8323 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
8324 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8325 struct imsm_map
*map2
;
8326 int prev_num_members
;
8328 if (dev
->vol
.migr_state
)
8331 if (prev_disks
== -1)
8332 prev_disks
= map
->num_members
;
8333 if (prev_disks
== map
->num_members
)
8336 /* OK, this array needs to enter reshape mode.
8337 * i.e it needs a migr_state
8340 copy_map_size
= sizeof_imsm_map(map
);
8341 prev_num_members
= map
->num_members
;
8342 map
->num_members
= prev_disks
;
8343 dev
->vol
.migr_state
= 1;
8344 dev
->vol
.curr_migr_unit
= 0;
8345 set_migr_type(dev
, MIGR_GEN_MIGR
);
8346 for (i
= prev_num_members
;
8347 i
< map
->num_members
; i
++)
8348 set_imsm_ord_tbl_ent(map
, i
, i
);
8349 map2
= get_imsm_map(dev
, MAP_1
);
8350 /* Copy the current map */
8351 memcpy(map2
, map
, copy_map_size
);
8352 map2
->num_members
= prev_num_members
;
8354 imsm_set_array_size(dev
, -1);
8355 super
->clean_migration_record_by_mdmon
= 1;
8356 super
->updates_pending
++;
8360 /* Handle dirty -> clean transititions, resync and reshape. Degraded and rebuild
8361 * states are handled in imsm_set_disk() with one exception, when a
8362 * resync is stopped due to a new failure this routine will set the
8363 * 'degraded' state for the array.
8365 static int imsm_set_array_state(struct active_array
*a
, int consistent
)
8367 int inst
= a
->info
.container_member
;
8368 struct intel_super
*super
= a
->container
->sb
;
8369 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
8370 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8371 int failed
= imsm_count_failed(super
, dev
, MAP_0
);
8372 __u8 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
8373 __u32 blocks_per_unit
;
8375 if (dev
->vol
.migr_state
&&
8376 dev
->vol
.migr_type
== MIGR_GEN_MIGR
) {
8377 /* array state change is blocked due to reshape action
8379 * - abort the reshape (if last_checkpoint is 0 and action!= reshape)
8380 * - finish the reshape (if last_checkpoint is big and action != reshape)
8381 * - update curr_migr_unit
8383 if (a
->curr_action
== reshape
) {
8384 /* still reshaping, maybe update curr_migr_unit */
8385 goto mark_checkpoint
;
8387 if (a
->last_checkpoint
== 0 && a
->prev_action
== reshape
) {
8388 /* for some reason we aborted the reshape.
8390 * disable automatic metadata rollback
8391 * user action is required to recover process
8394 struct imsm_map
*map2
=
8395 get_imsm_map(dev
, MAP_1
);
8396 dev
->vol
.migr_state
= 0;
8397 set_migr_type(dev
, 0);
8398 dev
->vol
.curr_migr_unit
= 0;
8400 sizeof_imsm_map(map2
));
8401 super
->updates_pending
++;
8404 if (a
->last_checkpoint
>= a
->info
.component_size
) {
8405 unsigned long long array_blocks
;
8409 used_disks
= imsm_num_data_members(map
);
8410 if (used_disks
> 0) {
8412 per_dev_array_size(map
) *
8415 round_size_to_mb(array_blocks
,
8417 a
->info
.custom_array_size
= array_blocks
;
8418 /* encourage manager to update array
8422 a
->check_reshape
= 1;
8424 /* finalize online capacity expansion/reshape */
8425 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
)
8427 mdi
->disk
.raid_disk
,
8430 imsm_progress_container_reshape(super
);
8435 /* before we activate this array handle any missing disks */
8436 if (consistent
== 2)
8437 handle_missing(super
, dev
);
8439 if (consistent
== 2 &&
8440 (!is_resync_complete(&a
->info
) ||
8441 map_state
!= IMSM_T_STATE_NORMAL
||
8442 dev
->vol
.migr_state
))
8445 if (is_resync_complete(&a
->info
)) {
8446 /* complete intialization / resync,
8447 * recovery and interrupted recovery is completed in
8450 if (is_resyncing(dev
)) {
8451 dprintf("imsm: mark resync done\n");
8452 end_migration(dev
, super
, map_state
);
8453 super
->updates_pending
++;
8454 a
->last_checkpoint
= 0;
8456 } else if ((!is_resyncing(dev
) && !failed
) &&
8457 (imsm_reshape_blocks_arrays_changes(super
) == 0)) {
8458 /* mark the start of the init process if nothing is failed */
8459 dprintf("imsm: mark resync start\n");
8460 if (map
->map_state
== IMSM_T_STATE_UNINITIALIZED
)
8461 migrate(dev
, super
, IMSM_T_STATE_NORMAL
, MIGR_INIT
);
8463 migrate(dev
, super
, IMSM_T_STATE_NORMAL
, MIGR_REPAIR
);
8464 super
->updates_pending
++;
8468 /* skip checkpointing for general migration,
8469 * it is controlled in mdadm
8471 if (is_gen_migration(dev
))
8472 goto skip_mark_checkpoint
;
8474 /* check if we can update curr_migr_unit from resync_start, recovery_start */
8475 blocks_per_unit
= blocks_per_migr_unit(super
, dev
);
8476 if (blocks_per_unit
) {
8480 units
= a
->last_checkpoint
/ blocks_per_unit
;
8483 /* check that we did not overflow 32-bits, and that
8484 * curr_migr_unit needs updating
8486 if (units32
== units
&&
8488 __le32_to_cpu(dev
->vol
.curr_migr_unit
) != units32
) {
8489 dprintf("imsm: mark checkpoint (%u)\n", units32
);
8490 dev
->vol
.curr_migr_unit
= __cpu_to_le32(units32
);
8491 super
->updates_pending
++;
8495 skip_mark_checkpoint
:
8496 /* mark dirty / clean */
8497 if (((dev
->vol
.dirty
& RAIDVOL_DIRTY
) && consistent
) ||
8498 (!(dev
->vol
.dirty
& RAIDVOL_DIRTY
) && !consistent
)) {
8499 dprintf("imsm: mark '%s'\n", consistent
? "clean" : "dirty");
8501 dev
->vol
.dirty
= RAIDVOL_CLEAN
;
8503 dev
->vol
.dirty
= RAIDVOL_DIRTY
;
8504 if (dev
->rwh_policy
== RWH_DISTRIBUTED
||
8505 dev
->rwh_policy
== RWH_MULTIPLE_DISTRIBUTED
)
8506 dev
->vol
.dirty
|= RAIDVOL_DSRECORD_VALID
;
8508 super
->updates_pending
++;
8514 static int imsm_disk_slot_to_ord(struct active_array
*a
, int slot
)
8516 int inst
= a
->info
.container_member
;
8517 struct intel_super
*super
= a
->container
->sb
;
8518 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
8519 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8521 if (slot
> map
->num_members
) {
8522 pr_err("imsm: imsm_disk_slot_to_ord %d out of range 0..%d\n",
8523 slot
, map
->num_members
- 1);
8530 return get_imsm_ord_tbl_ent(dev
, slot
, MAP_0
);
8533 static void imsm_set_disk(struct active_array
*a
, int n
, int state
)
8535 int inst
= a
->info
.container_member
;
8536 struct intel_super
*super
= a
->container
->sb
;
8537 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
8538 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8539 struct imsm_disk
*disk
;
8541 int recovery_not_finished
= 0;
8545 int rebuild_done
= 0;
8548 ord
= get_imsm_ord_tbl_ent(dev
, n
, MAP_X
);
8552 dprintf("imsm: set_disk %d:%x\n", n
, state
);
8553 disk
= get_imsm_disk(super
, ord_to_idx(ord
));
8555 /* check for new failures */
8556 if (disk
&& (state
& DS_FAULTY
)) {
8557 if (mark_failure(super
, dev
, disk
, ord_to_idx(ord
)))
8558 super
->updates_pending
++;
8561 /* check if in_sync */
8562 if (state
& DS_INSYNC
&& ord
& IMSM_ORD_REBUILD
&& is_rebuilding(dev
)) {
8563 struct imsm_map
*migr_map
= get_imsm_map(dev
, MAP_1
);
8565 set_imsm_ord_tbl_ent(migr_map
, n
, ord_to_idx(ord
));
8567 super
->updates_pending
++;
8570 failed
= imsm_count_failed(super
, dev
, MAP_0
);
8571 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
8573 /* check if recovery complete, newly degraded, or failed */
8574 dprintf("imsm: Detected transition to state ");
8575 switch (map_state
) {
8576 case IMSM_T_STATE_NORMAL
: /* transition to normal state */
8577 dprintf("normal: ");
8578 if (is_rebuilding(dev
)) {
8579 dprintf_cont("while rebuilding");
8580 /* check if recovery is really finished */
8581 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
)
8582 if (mdi
->recovery_start
!= MaxSector
) {
8583 recovery_not_finished
= 1;
8586 if (recovery_not_finished
) {
8588 dprintf("Rebuild has not finished yet, state not changed");
8589 if (a
->last_checkpoint
< mdi
->recovery_start
) {
8590 a
->last_checkpoint
= mdi
->recovery_start
;
8591 super
->updates_pending
++;
8595 end_migration(dev
, super
, map_state
);
8596 map
->failed_disk_num
= ~0;
8597 super
->updates_pending
++;
8598 a
->last_checkpoint
= 0;
8601 if (is_gen_migration(dev
)) {
8602 dprintf_cont("while general migration");
8603 if (a
->last_checkpoint
>= a
->info
.component_size
)
8604 end_migration(dev
, super
, map_state
);
8606 map
->map_state
= map_state
;
8607 map
->failed_disk_num
= ~0;
8608 super
->updates_pending
++;
8612 case IMSM_T_STATE_DEGRADED
: /* transition to degraded state */
8613 dprintf_cont("degraded: ");
8614 if (map
->map_state
!= map_state
&& !dev
->vol
.migr_state
) {
8615 dprintf_cont("mark degraded");
8616 map
->map_state
= map_state
;
8617 super
->updates_pending
++;
8618 a
->last_checkpoint
= 0;
8621 if (is_rebuilding(dev
)) {
8622 dprintf_cont("while rebuilding ");
8623 if (state
& DS_FAULTY
) {
8624 dprintf_cont("removing failed drive ");
8625 if (n
== map
->failed_disk_num
) {
8626 dprintf_cont("end migration");
8627 end_migration(dev
, super
, map_state
);
8628 a
->last_checkpoint
= 0;
8630 dprintf_cont("fail detected during rebuild, changing map state");
8631 map
->map_state
= map_state
;
8633 super
->updates_pending
++;
8639 /* check if recovery is really finished */
8640 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
)
8641 if (mdi
->recovery_start
!= MaxSector
) {
8642 recovery_not_finished
= 1;
8645 if (recovery_not_finished
) {
8647 dprintf_cont("Rebuild has not finished yet");
8648 if (a
->last_checkpoint
< mdi
->recovery_start
) {
8649 a
->last_checkpoint
=
8650 mdi
->recovery_start
;
8651 super
->updates_pending
++;
8656 dprintf_cont(" Rebuild done, still degraded");
8657 end_migration(dev
, super
, map_state
);
8658 a
->last_checkpoint
= 0;
8659 super
->updates_pending
++;
8661 for (i
= 0; i
< map
->num_members
; i
++) {
8662 int idx
= get_imsm_ord_tbl_ent(dev
, i
, MAP_0
);
8664 if (idx
& IMSM_ORD_REBUILD
)
8665 map
->failed_disk_num
= i
;
8667 super
->updates_pending
++;
8670 if (is_gen_migration(dev
)) {
8671 dprintf_cont("while general migration");
8672 if (a
->last_checkpoint
>= a
->info
.component_size
)
8673 end_migration(dev
, super
, map_state
);
8675 map
->map_state
= map_state
;
8676 manage_second_map(super
, dev
);
8678 super
->updates_pending
++;
8681 if (is_initializing(dev
)) {
8682 dprintf_cont("while initialization.");
8683 map
->map_state
= map_state
;
8684 super
->updates_pending
++;
8688 case IMSM_T_STATE_FAILED
: /* transition to failed state */
8689 dprintf_cont("failed: ");
8690 if (is_gen_migration(dev
)) {
8691 dprintf_cont("while general migration");
8692 map
->map_state
= map_state
;
8693 super
->updates_pending
++;
8696 if (map
->map_state
!= map_state
) {
8697 dprintf_cont("mark failed");
8698 end_migration(dev
, super
, map_state
);
8699 super
->updates_pending
++;
8700 a
->last_checkpoint
= 0;
8705 dprintf_cont("state %i\n", map_state
);
8710 static int store_imsm_mpb(int fd
, struct imsm_super
*mpb
)
8713 __u32 mpb_size
= __le32_to_cpu(mpb
->mpb_size
);
8714 unsigned long long dsize
;
8715 unsigned long long sectors
;
8716 unsigned int sector_size
;
8718 get_dev_sector_size(fd
, NULL
, §or_size
);
8719 get_dev_size(fd
, NULL
, &dsize
);
8721 if (mpb_size
> sector_size
) {
8722 /* -1 to account for anchor */
8723 sectors
= mpb_sectors(mpb
, sector_size
) - 1;
8725 /* write the extended mpb to the sectors preceeding the anchor */
8726 if (lseek64(fd
, dsize
- (sector_size
* (2 + sectors
)),
8730 if ((unsigned long long)write(fd
, buf
+ sector_size
,
8731 sector_size
* sectors
) != sector_size
* sectors
)
8735 /* first block is stored on second to last sector of the disk */
8736 if (lseek64(fd
, dsize
- (sector_size
* 2), SEEK_SET
) < 0)
8739 if ((unsigned int)write(fd
, buf
, sector_size
) != sector_size
)
8745 static void imsm_sync_metadata(struct supertype
*container
)
8747 struct intel_super
*super
= container
->sb
;
8749 dprintf("sync metadata: %d\n", super
->updates_pending
);
8750 if (!super
->updates_pending
)
8753 write_super_imsm(container
, 0);
8755 super
->updates_pending
= 0;
8758 static struct dl
*imsm_readd(struct intel_super
*super
, int idx
, struct active_array
*a
)
8760 struct imsm_dev
*dev
= get_imsm_dev(super
, a
->info
.container_member
);
8761 int i
= get_imsm_disk_idx(dev
, idx
, MAP_X
);
8764 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
8768 if (dl
&& is_failed(&dl
->disk
))
8772 dprintf("found %x:%x\n", dl
->major
, dl
->minor
);
8777 static struct dl
*imsm_add_spare(struct intel_super
*super
, int slot
,
8778 struct active_array
*a
, int activate_new
,
8779 struct mdinfo
*additional_test_list
)
8781 struct imsm_dev
*dev
= get_imsm_dev(super
, a
->info
.container_member
);
8782 int idx
= get_imsm_disk_idx(dev
, slot
, MAP_X
);
8783 struct imsm_super
*mpb
= super
->anchor
;
8784 struct imsm_map
*map
;
8785 unsigned long long pos
;
8790 __u32 array_start
= 0;
8791 __u32 array_end
= 0;
8793 struct mdinfo
*test_list
;
8795 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
8796 /* If in this array, skip */
8797 for (d
= a
->info
.devs
; d
; d
= d
->next
)
8798 if (d
->state_fd
>= 0 &&
8799 d
->disk
.major
== dl
->major
&&
8800 d
->disk
.minor
== dl
->minor
) {
8801 dprintf("%x:%x already in array\n",
8802 dl
->major
, dl
->minor
);
8807 test_list
= additional_test_list
;
8809 if (test_list
->disk
.major
== dl
->major
&&
8810 test_list
->disk
.minor
== dl
->minor
) {
8811 dprintf("%x:%x already in additional test list\n",
8812 dl
->major
, dl
->minor
);
8815 test_list
= test_list
->next
;
8820 /* skip in use or failed drives */
8821 if (is_failed(&dl
->disk
) || idx
== dl
->index
||
8823 dprintf("%x:%x status (failed: %d index: %d)\n",
8824 dl
->major
, dl
->minor
, is_failed(&dl
->disk
), idx
);
8828 /* skip pure spares when we are looking for partially
8829 * assimilated drives
8831 if (dl
->index
== -1 && !activate_new
)
8834 if (!drive_validate_sector_size(super
, dl
))
8837 /* Does this unused device have the requisite free space?
8838 * It needs to be able to cover all member volumes
8840 ex
= get_extents(super
, dl
, 1);
8842 dprintf("cannot get extents\n");
8845 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
8846 dev
= get_imsm_dev(super
, i
);
8847 map
= get_imsm_map(dev
, MAP_0
);
8849 /* check if this disk is already a member of
8852 if (get_imsm_disk_slot(map
, dl
->index
) >= 0)
8858 array_start
= pba_of_lba0(map
);
8859 array_end
= array_start
+
8860 per_dev_array_size(map
) - 1;
8863 /* check that we can start at pba_of_lba0 with
8864 * num_data_stripes*blocks_per_stripe of space
8866 if (array_start
>= pos
&& array_end
< ex
[j
].start
) {
8870 pos
= ex
[j
].start
+ ex
[j
].size
;
8872 } while (ex
[j
-1].size
);
8879 if (i
< mpb
->num_raid_devs
) {
8880 dprintf("%x:%x does not have %u to %u available\n",
8881 dl
->major
, dl
->minor
, array_start
, array_end
);
8891 static int imsm_rebuild_allowed(struct supertype
*cont
, int dev_idx
, int failed
)
8893 struct imsm_dev
*dev2
;
8894 struct imsm_map
*map
;
8900 dev2
= get_imsm_dev(cont
->sb
, dev_idx
);
8902 state
= imsm_check_degraded(cont
->sb
, dev2
, failed
, MAP_0
);
8903 if (state
== IMSM_T_STATE_FAILED
) {
8904 map
= get_imsm_map(dev2
, MAP_0
);
8907 for (slot
= 0; slot
< map
->num_members
; slot
++) {
8909 * Check if failed disks are deleted from intel
8910 * disk list or are marked to be deleted
8912 idx
= get_imsm_disk_idx(dev2
, slot
, MAP_X
);
8913 idisk
= get_imsm_dl_disk(cont
->sb
, idx
);
8915 * Do not rebuild the array if failed disks
8916 * from failed sub-array are not removed from
8920 is_failed(&idisk
->disk
) &&
8921 (idisk
->action
!= DISK_REMOVE
))
8929 static struct mdinfo
*imsm_activate_spare(struct active_array
*a
,
8930 struct metadata_update
**updates
)
8933 * Find a device with unused free space and use it to replace a
8934 * failed/vacant region in an array. We replace failed regions one a
8935 * array at a time. The result is that a new spare disk will be added
8936 * to the first failed array and after the monitor has finished
8937 * propagating failures the remainder will be consumed.
8939 * FIXME add a capability for mdmon to request spares from another
8943 struct intel_super
*super
= a
->container
->sb
;
8944 int inst
= a
->info
.container_member
;
8945 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
8946 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8947 int failed
= a
->info
.array
.raid_disks
;
8948 struct mdinfo
*rv
= NULL
;
8951 struct metadata_update
*mu
;
8953 struct imsm_update_activate_spare
*u
;
8958 for (d
= a
->info
.devs
; d
; d
= d
->next
) {
8959 if ((d
->curr_state
& DS_FAULTY
) &&
8961 /* wait for Removal to happen */
8963 if (d
->state_fd
>= 0)
8967 dprintf("imsm: activate spare: inst=%d failed=%d (%d) level=%d\n",
8968 inst
, failed
, a
->info
.array
.raid_disks
, a
->info
.array
.level
);
8970 if (imsm_reshape_blocks_arrays_changes(super
))
8973 /* Cannot activate another spare if rebuild is in progress already
8975 if (is_rebuilding(dev
)) {
8976 dprintf("imsm: No spare activation allowed. Rebuild in progress already.\n");
8980 if (a
->info
.array
.level
== 4)
8981 /* No repair for takeovered array
8982 * imsm doesn't support raid4
8986 if (imsm_check_degraded(super
, dev
, failed
, MAP_0
) !=
8987 IMSM_T_STATE_DEGRADED
)
8990 if (get_imsm_map(dev
, MAP_0
)->map_state
== IMSM_T_STATE_UNINITIALIZED
) {
8991 dprintf("imsm: No spare activation allowed. Volume is not initialized.\n");
8996 * If there are any failed disks check state of the other volume.
8997 * Block rebuild if the another one is failed until failed disks
8998 * are removed from container.
9001 dprintf("found failed disks in %.*s, check if there anotherfailed sub-array.\n",
9002 MAX_RAID_SERIAL_LEN
, dev
->volume
);
9003 /* check if states of the other volumes allow for rebuild */
9004 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
9006 allowed
= imsm_rebuild_allowed(a
->container
,
9014 /* For each slot, if it is not working, find a spare */
9015 for (i
= 0; i
< a
->info
.array
.raid_disks
; i
++) {
9016 for (d
= a
->info
.devs
; d
; d
= d
->next
)
9017 if (d
->disk
.raid_disk
== i
)
9019 dprintf("found %d: %p %x\n", i
, d
, d
?d
->curr_state
:0);
9020 if (d
&& (d
->state_fd
>= 0))
9024 * OK, this device needs recovery. Try to re-add the
9025 * previous occupant of this slot, if this fails see if
9026 * we can continue the assimilation of a spare that was
9027 * partially assimilated, finally try to activate a new
9030 dl
= imsm_readd(super
, i
, a
);
9032 dl
= imsm_add_spare(super
, i
, a
, 0, rv
);
9034 dl
= imsm_add_spare(super
, i
, a
, 1, rv
);
9038 /* found a usable disk with enough space */
9039 di
= xcalloc(1, sizeof(*di
));
9041 /* dl->index will be -1 in the case we are activating a
9042 * pristine spare. imsm_process_update() will create a
9043 * new index in this case. Once a disk is found to be
9044 * failed in all member arrays it is kicked from the
9047 di
->disk
.number
= dl
->index
;
9049 /* (ab)use di->devs to store a pointer to the device
9052 di
->devs
= (struct mdinfo
*) dl
;
9054 di
->disk
.raid_disk
= i
;
9055 di
->disk
.major
= dl
->major
;
9056 di
->disk
.minor
= dl
->minor
;
9058 di
->recovery_start
= 0;
9059 di
->data_offset
= pba_of_lba0(map
);
9060 di
->component_size
= a
->info
.component_size
;
9061 di
->container_member
= inst
;
9062 di
->bb
.supported
= 1;
9063 if (a
->info
.consistency_policy
== CONSISTENCY_POLICY_PPL
) {
9064 di
->ppl_sector
= get_ppl_sector(super
, inst
);
9065 di
->ppl_size
= MULTIPLE_PPL_AREA_SIZE_IMSM
>> 9;
9067 super
->random
= random32();
9071 dprintf("%x:%x to be %d at %llu\n", dl
->major
, dl
->minor
,
9072 i
, di
->data_offset
);
9076 /* No spares found */
9078 /* Now 'rv' has a list of devices to return.
9079 * Create a metadata_update record to update the
9080 * disk_ord_tbl for the array
9082 mu
= xmalloc(sizeof(*mu
));
9083 mu
->buf
= xcalloc(num_spares
,
9084 sizeof(struct imsm_update_activate_spare
));
9086 mu
->space_list
= NULL
;
9087 mu
->len
= sizeof(struct imsm_update_activate_spare
) * num_spares
;
9088 mu
->next
= *updates
;
9089 u
= (struct imsm_update_activate_spare
*) mu
->buf
;
9091 for (di
= rv
; di
; di
= di
->next
) {
9092 u
->type
= update_activate_spare
;
9093 u
->dl
= (struct dl
*) di
->devs
;
9095 u
->slot
= di
->disk
.raid_disk
;
9106 static int disks_overlap(struct intel_super
*super
, int idx
, struct imsm_update_create_array
*u
)
9108 struct imsm_dev
*dev
= get_imsm_dev(super
, idx
);
9109 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
9110 struct imsm_map
*new_map
= get_imsm_map(&u
->dev
, MAP_0
);
9111 struct disk_info
*inf
= get_disk_info(u
);
9112 struct imsm_disk
*disk
;
9116 for (i
= 0; i
< map
->num_members
; i
++) {
9117 disk
= get_imsm_disk(super
, get_imsm_disk_idx(dev
, i
, MAP_X
));
9118 for (j
= 0; j
< new_map
->num_members
; j
++)
9119 if (serialcmp(disk
->serial
, inf
[j
].serial
) == 0)
9126 static struct dl
*get_disk_super(struct intel_super
*super
, int major
, int minor
)
9130 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
9131 if (dl
->major
== major
&& dl
->minor
== minor
)
9136 static int remove_disk_super(struct intel_super
*super
, int major
, int minor
)
9142 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
9143 if (dl
->major
== major
&& dl
->minor
== minor
) {
9146 prev
->next
= dl
->next
;
9148 super
->disks
= dl
->next
;
9150 __free_imsm_disk(dl
);
9151 dprintf("removed %x:%x\n", major
, minor
);
9159 static void imsm_delete(struct intel_super
*super
, struct dl
**dlp
, unsigned index
);
9161 static int add_remove_disk_update(struct intel_super
*super
)
9163 int check_degraded
= 0;
9166 /* add/remove some spares to/from the metadata/contrainer */
9167 while (super
->disk_mgmt_list
) {
9168 struct dl
*disk_cfg
;
9170 disk_cfg
= super
->disk_mgmt_list
;
9171 super
->disk_mgmt_list
= disk_cfg
->next
;
9172 disk_cfg
->next
= NULL
;
9174 if (disk_cfg
->action
== DISK_ADD
) {
9175 disk_cfg
->next
= super
->disks
;
9176 super
->disks
= disk_cfg
;
9178 dprintf("added %x:%x\n",
9179 disk_cfg
->major
, disk_cfg
->minor
);
9180 } else if (disk_cfg
->action
== DISK_REMOVE
) {
9181 dprintf("Disk remove action processed: %x.%x\n",
9182 disk_cfg
->major
, disk_cfg
->minor
);
9183 disk
= get_disk_super(super
,
9187 /* store action status */
9188 disk
->action
= DISK_REMOVE
;
9189 /* remove spare disks only */
9190 if (disk
->index
== -1) {
9191 remove_disk_super(super
,
9195 disk_cfg
->fd
= disk
->fd
;
9199 /* release allocate disk structure */
9200 __free_imsm_disk(disk_cfg
);
9203 return check_degraded
;
9206 static int apply_reshape_migration_update(struct imsm_update_reshape_migration
*u
,
9207 struct intel_super
*super
,
9210 struct intel_dev
*id
;
9211 void **tofree
= NULL
;
9214 dprintf("(enter)\n");
9215 if (u
->subdev
< 0 || u
->subdev
> 1) {
9216 dprintf("imsm: Error: Wrong subdev: %i\n", u
->subdev
);
9219 if (space_list
== NULL
|| *space_list
== NULL
) {
9220 dprintf("imsm: Error: Memory is not allocated\n");
9224 for (id
= super
->devlist
; id
; id
= id
->next
) {
9225 if (id
->index
== (unsigned)u
->subdev
) {
9226 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->subdev
);
9227 struct imsm_map
*map
;
9228 struct imsm_dev
*new_dev
=
9229 (struct imsm_dev
*)*space_list
;
9230 struct imsm_map
*migr_map
= get_imsm_map(dev
, MAP_1
);
9232 struct dl
*new_disk
;
9234 if (new_dev
== NULL
)
9236 *space_list
= **space_list
;
9237 memcpy(new_dev
, dev
, sizeof_imsm_dev(dev
, 0));
9238 map
= get_imsm_map(new_dev
, MAP_0
);
9240 dprintf("imsm: Error: migration in progress");
9244 to_state
= map
->map_state
;
9245 if ((u
->new_level
== 5) && (map
->raid_level
== 0)) {
9247 /* this should not happen */
9248 if (u
->new_disks
[0] < 0) {
9249 map
->failed_disk_num
=
9250 map
->num_members
- 1;
9251 to_state
= IMSM_T_STATE_DEGRADED
;
9253 to_state
= IMSM_T_STATE_NORMAL
;
9255 migrate(new_dev
, super
, to_state
, MIGR_GEN_MIGR
);
9256 if (u
->new_level
> -1)
9257 map
->raid_level
= u
->new_level
;
9258 migr_map
= get_imsm_map(new_dev
, MAP_1
);
9259 if ((u
->new_level
== 5) &&
9260 (migr_map
->raid_level
== 0)) {
9261 int ord
= map
->num_members
- 1;
9262 migr_map
->num_members
--;
9263 if (u
->new_disks
[0] < 0)
9264 ord
|= IMSM_ORD_REBUILD
;
9265 set_imsm_ord_tbl_ent(map
,
9266 map
->num_members
- 1,
9270 tofree
= (void **)dev
;
9272 /* update chunk size
9274 if (u
->new_chunksize
> 0) {
9275 unsigned long long num_data_stripes
;
9276 struct imsm_map
*dest_map
=
9277 get_imsm_map(dev
, MAP_0
);
9279 imsm_num_data_members(dest_map
);
9281 if (used_disks
== 0)
9284 map
->blocks_per_strip
=
9285 __cpu_to_le16(u
->new_chunksize
* 2);
9287 imsm_dev_size(dev
) / used_disks
;
9288 num_data_stripes
/= map
->blocks_per_strip
;
9289 num_data_stripes
/= map
->num_domains
;
9290 set_num_data_stripes(map
, num_data_stripes
);
9293 /* ensure blocks_per_member has valid value
9295 set_blocks_per_member(map
,
9296 per_dev_array_size(map
) +
9297 NUM_BLOCKS_DIRTY_STRIPE_REGION
);
9301 if (u
->new_level
!= 5 || migr_map
->raid_level
!= 0 ||
9302 migr_map
->raid_level
== map
->raid_level
)
9305 if (u
->new_disks
[0] >= 0) {
9308 new_disk
= get_disk_super(super
,
9309 major(u
->new_disks
[0]),
9310 minor(u
->new_disks
[0]));
9311 dprintf("imsm: new disk for reshape is: %i:%i (%p, index = %i)\n",
9312 major(u
->new_disks
[0]),
9313 minor(u
->new_disks
[0]),
9314 new_disk
, new_disk
->index
);
9315 if (new_disk
== NULL
)
9316 goto error_disk_add
;
9318 new_disk
->index
= map
->num_members
- 1;
9319 /* slot to fill in autolayout
9321 new_disk
->raiddisk
= new_disk
->index
;
9322 new_disk
->disk
.status
|= CONFIGURED_DISK
;
9323 new_disk
->disk
.status
&= ~SPARE_DISK
;
9325 goto error_disk_add
;
9328 *tofree
= *space_list
;
9329 /* calculate new size
9331 imsm_set_array_size(new_dev
, -1);
9338 *space_list
= tofree
;
9342 dprintf("Error: imsm: Cannot find disk.\n");
9346 static int apply_size_change_update(struct imsm_update_size_change
*u
,
9347 struct intel_super
*super
)
9349 struct intel_dev
*id
;
9352 dprintf("(enter)\n");
9353 if (u
->subdev
< 0 || u
->subdev
> 1) {
9354 dprintf("imsm: Error: Wrong subdev: %i\n", u
->subdev
);
9358 for (id
= super
->devlist
; id
; id
= id
->next
) {
9359 if (id
->index
== (unsigned)u
->subdev
) {
9360 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->subdev
);
9361 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
9362 int used_disks
= imsm_num_data_members(map
);
9363 unsigned long long blocks_per_member
;
9364 unsigned long long num_data_stripes
;
9365 unsigned long long new_size_per_disk
;
9367 if (used_disks
== 0)
9370 /* calculate new size
9372 new_size_per_disk
= u
->new_size
/ used_disks
;
9373 blocks_per_member
= new_size_per_disk
+
9374 NUM_BLOCKS_DIRTY_STRIPE_REGION
;
9375 num_data_stripes
= new_size_per_disk
/
9376 map
->blocks_per_strip
;
9377 num_data_stripes
/= map
->num_domains
;
9378 dprintf("(size: %llu, blocks per member: %llu, num_data_stipes: %llu)\n",
9379 u
->new_size
, new_size_per_disk
,
9381 set_blocks_per_member(map
, blocks_per_member
);
9382 set_num_data_stripes(map
, num_data_stripes
);
9383 imsm_set_array_size(dev
, u
->new_size
);
9393 static int apply_update_activate_spare(struct imsm_update_activate_spare
*u
,
9394 struct intel_super
*super
,
9395 struct active_array
*active_array
)
9397 struct imsm_super
*mpb
= super
->anchor
;
9398 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->array
);
9399 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
9400 struct imsm_map
*migr_map
;
9401 struct active_array
*a
;
9402 struct imsm_disk
*disk
;
9409 int second_map_created
= 0;
9411 for (; u
; u
= u
->next
) {
9412 victim
= get_imsm_disk_idx(dev
, u
->slot
, MAP_X
);
9417 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
9422 pr_err("error: imsm_activate_spare passed an unknown disk (index: %d)\n",
9427 /* count failures (excluding rebuilds and the victim)
9428 * to determine map[0] state
9431 for (i
= 0; i
< map
->num_members
; i
++) {
9434 disk
= get_imsm_disk(super
,
9435 get_imsm_disk_idx(dev
, i
, MAP_X
));
9436 if (!disk
|| is_failed(disk
))
9440 /* adding a pristine spare, assign a new index */
9441 if (dl
->index
< 0) {
9442 dl
->index
= super
->anchor
->num_disks
;
9443 super
->anchor
->num_disks
++;
9446 disk
->status
|= CONFIGURED_DISK
;
9447 disk
->status
&= ~SPARE_DISK
;
9450 to_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
9451 if (!second_map_created
) {
9452 second_map_created
= 1;
9453 map
->map_state
= IMSM_T_STATE_DEGRADED
;
9454 migrate(dev
, super
, to_state
, MIGR_REBUILD
);
9456 map
->map_state
= to_state
;
9457 migr_map
= get_imsm_map(dev
, MAP_1
);
9458 set_imsm_ord_tbl_ent(map
, u
->slot
, dl
->index
);
9459 set_imsm_ord_tbl_ent(migr_map
, u
->slot
,
9460 dl
->index
| IMSM_ORD_REBUILD
);
9462 /* update the family_num to mark a new container
9463 * generation, being careful to record the existing
9464 * family_num in orig_family_num to clean up after
9465 * earlier mdadm versions that neglected to set it.
9467 if (mpb
->orig_family_num
== 0)
9468 mpb
->orig_family_num
= mpb
->family_num
;
9469 mpb
->family_num
+= super
->random
;
9471 /* count arrays using the victim in the metadata */
9473 for (a
= active_array
; a
; a
= a
->next
) {
9474 dev
= get_imsm_dev(super
, a
->info
.container_member
);
9475 map
= get_imsm_map(dev
, MAP_0
);
9477 if (get_imsm_disk_slot(map
, victim
) >= 0)
9481 /* delete the victim if it is no longer being
9487 /* We know that 'manager' isn't touching anything,
9488 * so it is safe to delete
9490 for (dlp
= &super
->disks
; *dlp
; dlp
= &(*dlp
)->next
)
9491 if ((*dlp
)->index
== victim
)
9494 /* victim may be on the missing list */
9496 for (dlp
= &super
->missing
; *dlp
;
9497 dlp
= &(*dlp
)->next
)
9498 if ((*dlp
)->index
== victim
)
9500 imsm_delete(super
, dlp
, victim
);
9507 static int apply_reshape_container_disks_update(struct imsm_update_reshape
*u
,
9508 struct intel_super
*super
,
9511 struct dl
*new_disk
;
9512 struct intel_dev
*id
;
9514 int delta_disks
= u
->new_raid_disks
- u
->old_raid_disks
;
9515 int disk_count
= u
->old_raid_disks
;
9516 void **tofree
= NULL
;
9517 int devices_to_reshape
= 1;
9518 struct imsm_super
*mpb
= super
->anchor
;
9520 unsigned int dev_id
;
9522 dprintf("(enter)\n");
9524 /* enable spares to use in array */
9525 for (i
= 0; i
< delta_disks
; i
++) {
9526 new_disk
= get_disk_super(super
,
9527 major(u
->new_disks
[i
]),
9528 minor(u
->new_disks
[i
]));
9529 dprintf("imsm: new disk for reshape is: %i:%i (%p, index = %i)\n",
9530 major(u
->new_disks
[i
]), minor(u
->new_disks
[i
]),
9531 new_disk
, new_disk
->index
);
9532 if (new_disk
== NULL
||
9533 (new_disk
->index
>= 0 &&
9534 new_disk
->index
< u
->old_raid_disks
))
9535 goto update_reshape_exit
;
9536 new_disk
->index
= disk_count
++;
9537 /* slot to fill in autolayout
9539 new_disk
->raiddisk
= new_disk
->index
;
9540 new_disk
->disk
.status
|=
9542 new_disk
->disk
.status
&= ~SPARE_DISK
;
9545 dprintf("imsm: volume set mpb->num_raid_devs = %i\n",
9546 mpb
->num_raid_devs
);
9547 /* manage changes in volume
9549 for (dev_id
= 0; dev_id
< mpb
->num_raid_devs
; dev_id
++) {
9550 void **sp
= *space_list
;
9551 struct imsm_dev
*newdev
;
9552 struct imsm_map
*newmap
, *oldmap
;
9554 for (id
= super
->devlist
; id
; id
= id
->next
) {
9555 if (id
->index
== dev_id
)
9564 /* Copy the dev, but not (all of) the map */
9565 memcpy(newdev
, id
->dev
, sizeof(*newdev
));
9566 oldmap
= get_imsm_map(id
->dev
, MAP_0
);
9567 newmap
= get_imsm_map(newdev
, MAP_0
);
9568 /* Copy the current map */
9569 memcpy(newmap
, oldmap
, sizeof_imsm_map(oldmap
));
9570 /* update one device only
9572 if (devices_to_reshape
) {
9573 dprintf("imsm: modifying subdev: %i\n",
9575 devices_to_reshape
--;
9576 newdev
->vol
.migr_state
= 1;
9577 newdev
->vol
.curr_migr_unit
= 0;
9578 set_migr_type(newdev
, MIGR_GEN_MIGR
);
9579 newmap
->num_members
= u
->new_raid_disks
;
9580 for (i
= 0; i
< delta_disks
; i
++) {
9581 set_imsm_ord_tbl_ent(newmap
,
9582 u
->old_raid_disks
+ i
,
9583 u
->old_raid_disks
+ i
);
9585 /* New map is correct, now need to save old map
9587 newmap
= get_imsm_map(newdev
, MAP_1
);
9588 memcpy(newmap
, oldmap
, sizeof_imsm_map(oldmap
));
9590 imsm_set_array_size(newdev
, -1);
9593 sp
= (void **)id
->dev
;
9598 /* Clear migration record */
9599 memset(super
->migr_rec
, 0, sizeof(struct migr_record
));
9602 *space_list
= tofree
;
9605 update_reshape_exit
:
9610 static int apply_takeover_update(struct imsm_update_takeover
*u
,
9611 struct intel_super
*super
,
9614 struct imsm_dev
*dev
= NULL
;
9615 struct intel_dev
*dv
;
9616 struct imsm_dev
*dev_new
;
9617 struct imsm_map
*map
;
9621 for (dv
= super
->devlist
; dv
; dv
= dv
->next
)
9622 if (dv
->index
== (unsigned int)u
->subarray
) {
9630 map
= get_imsm_map(dev
, MAP_0
);
9632 if (u
->direction
== R10_TO_R0
) {
9633 unsigned long long num_data_stripes
;
9635 /* Number of failed disks must be half of initial disk number */
9636 if (imsm_count_failed(super
, dev
, MAP_0
) !=
9637 (map
->num_members
/ 2))
9640 /* iterate through devices to mark removed disks as spare */
9641 for (dm
= super
->disks
; dm
; dm
= dm
->next
) {
9642 if (dm
->disk
.status
& FAILED_DISK
) {
9643 int idx
= dm
->index
;
9644 /* update indexes on the disk list */
9645 /* FIXME this loop-with-the-loop looks wrong, I'm not convinced
9646 the index values will end up being correct.... NB */
9647 for (du
= super
->disks
; du
; du
= du
->next
)
9648 if (du
->index
> idx
)
9650 /* mark as spare disk */
9655 map
->num_members
= map
->num_members
/ 2;
9656 map
->map_state
= IMSM_T_STATE_NORMAL
;
9657 map
->num_domains
= 1;
9658 map
->raid_level
= 0;
9659 map
->failed_disk_num
= -1;
9660 num_data_stripes
= imsm_dev_size(dev
) / 2;
9661 num_data_stripes
/= map
->blocks_per_strip
;
9662 set_num_data_stripes(map
, num_data_stripes
);
9665 if (u
->direction
== R0_TO_R10
) {
9667 unsigned long long num_data_stripes
;
9669 /* update slots in current disk list */
9670 for (dm
= super
->disks
; dm
; dm
= dm
->next
) {
9674 /* create new *missing* disks */
9675 for (i
= 0; i
< map
->num_members
; i
++) {
9676 space
= *space_list
;
9679 *space_list
= *space
;
9681 memcpy(du
, super
->disks
, sizeof(*du
));
9685 du
->index
= (i
* 2) + 1;
9686 sprintf((char *)du
->disk
.serial
,
9687 " MISSING_%d", du
->index
);
9688 sprintf((char *)du
->serial
,
9689 "MISSING_%d", du
->index
);
9690 du
->next
= super
->missing
;
9691 super
->missing
= du
;
9693 /* create new dev and map */
9694 space
= *space_list
;
9697 *space_list
= *space
;
9698 dev_new
= (void *)space
;
9699 memcpy(dev_new
, dev
, sizeof(*dev
));
9700 /* update new map */
9701 map
= get_imsm_map(dev_new
, MAP_0
);
9702 map
->num_members
= map
->num_members
* 2;
9703 map
->map_state
= IMSM_T_STATE_DEGRADED
;
9704 map
->num_domains
= 2;
9705 map
->raid_level
= 1;
9706 num_data_stripes
= imsm_dev_size(dev
) / 2;
9707 num_data_stripes
/= map
->blocks_per_strip
;
9708 num_data_stripes
/= map
->num_domains
;
9709 set_num_data_stripes(map
, num_data_stripes
);
9711 /* replace dev<->dev_new */
9714 /* update disk order table */
9715 for (du
= super
->disks
; du
; du
= du
->next
)
9717 set_imsm_ord_tbl_ent(map
, du
->index
, du
->index
);
9718 for (du
= super
->missing
; du
; du
= du
->next
)
9719 if (du
->index
>= 0) {
9720 set_imsm_ord_tbl_ent(map
, du
->index
, du
->index
);
9721 mark_missing(super
, dv
->dev
, &du
->disk
, du
->index
);
9727 static void imsm_process_update(struct supertype
*st
,
9728 struct metadata_update
*update
)
9731 * crack open the metadata_update envelope to find the update record
9732 * update can be one of:
9733 * update_reshape_container_disks - all the arrays in the container
9734 * are being reshaped to have more devices. We need to mark
9735 * the arrays for general migration and convert selected spares
9736 * into active devices.
9737 * update_activate_spare - a spare device has replaced a failed
9738 * device in an array, update the disk_ord_tbl. If this disk is
9739 * present in all member arrays then also clear the SPARE_DISK
9741 * update_create_array
9743 * update_rename_array
9744 * update_add_remove_disk
9746 struct intel_super
*super
= st
->sb
;
9747 struct imsm_super
*mpb
;
9748 enum imsm_update_type type
= *(enum imsm_update_type
*) update
->buf
;
9750 /* update requires a larger buf but the allocation failed */
9751 if (super
->next_len
&& !super
->next_buf
) {
9752 super
->next_len
= 0;
9756 if (super
->next_buf
) {
9757 memcpy(super
->next_buf
, super
->buf
, super
->len
);
9759 super
->len
= super
->next_len
;
9760 super
->buf
= super
->next_buf
;
9762 super
->next_len
= 0;
9763 super
->next_buf
= NULL
;
9766 mpb
= super
->anchor
;
9769 case update_general_migration_checkpoint
: {
9770 struct intel_dev
*id
;
9771 struct imsm_update_general_migration_checkpoint
*u
=
9772 (void *)update
->buf
;
9774 dprintf("called for update_general_migration_checkpoint\n");
9776 /* find device under general migration */
9777 for (id
= super
->devlist
; id
; id
= id
->next
) {
9778 if (is_gen_migration(id
->dev
)) {
9779 id
->dev
->vol
.curr_migr_unit
=
9780 __cpu_to_le32(u
->curr_migr_unit
);
9781 super
->updates_pending
++;
9786 case update_takeover
: {
9787 struct imsm_update_takeover
*u
= (void *)update
->buf
;
9788 if (apply_takeover_update(u
, super
, &update
->space_list
)) {
9789 imsm_update_version_info(super
);
9790 super
->updates_pending
++;
9795 case update_reshape_container_disks
: {
9796 struct imsm_update_reshape
*u
= (void *)update
->buf
;
9797 if (apply_reshape_container_disks_update(
9798 u
, super
, &update
->space_list
))
9799 super
->updates_pending
++;
9802 case update_reshape_migration
: {
9803 struct imsm_update_reshape_migration
*u
= (void *)update
->buf
;
9804 if (apply_reshape_migration_update(
9805 u
, super
, &update
->space_list
))
9806 super
->updates_pending
++;
9809 case update_size_change
: {
9810 struct imsm_update_size_change
*u
= (void *)update
->buf
;
9811 if (apply_size_change_update(u
, super
))
9812 super
->updates_pending
++;
9815 case update_activate_spare
: {
9816 struct imsm_update_activate_spare
*u
= (void *) update
->buf
;
9817 if (apply_update_activate_spare(u
, super
, st
->arrays
))
9818 super
->updates_pending
++;
9821 case update_create_array
: {
9822 /* someone wants to create a new array, we need to be aware of
9823 * a few races/collisions:
9824 * 1/ 'Create' called by two separate instances of mdadm
9825 * 2/ 'Create' versus 'activate_spare': mdadm has chosen
9826 * devices that have since been assimilated via
9828 * In the event this update can not be carried out mdadm will
9829 * (FIX ME) notice that its update did not take hold.
9831 struct imsm_update_create_array
*u
= (void *) update
->buf
;
9832 struct intel_dev
*dv
;
9833 struct imsm_dev
*dev
;
9834 struct imsm_map
*map
, *new_map
;
9835 unsigned long long start
, end
;
9836 unsigned long long new_start
, new_end
;
9838 struct disk_info
*inf
;
9841 /* handle racing creates: first come first serve */
9842 if (u
->dev_idx
< mpb
->num_raid_devs
) {
9843 dprintf("subarray %d already defined\n", u
->dev_idx
);
9847 /* check update is next in sequence */
9848 if (u
->dev_idx
!= mpb
->num_raid_devs
) {
9849 dprintf("can not create array %d expected index %d\n",
9850 u
->dev_idx
, mpb
->num_raid_devs
);
9854 new_map
= get_imsm_map(&u
->dev
, MAP_0
);
9855 new_start
= pba_of_lba0(new_map
);
9856 new_end
= new_start
+ per_dev_array_size(new_map
);
9857 inf
= get_disk_info(u
);
9859 /* handle activate_spare versus create race:
9860 * check to make sure that overlapping arrays do not include
9863 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
9864 dev
= get_imsm_dev(super
, i
);
9865 map
= get_imsm_map(dev
, MAP_0
);
9866 start
= pba_of_lba0(map
);
9867 end
= start
+ per_dev_array_size(map
);
9868 if ((new_start
>= start
&& new_start
<= end
) ||
9869 (start
>= new_start
&& start
<= new_end
))
9874 if (disks_overlap(super
, i
, u
)) {
9875 dprintf("arrays overlap\n");
9880 /* check that prepare update was successful */
9881 if (!update
->space
) {
9882 dprintf("prepare update failed\n");
9886 /* check that all disks are still active before committing
9887 * changes. FIXME: could we instead handle this by creating a
9888 * degraded array? That's probably not what the user expects,
9889 * so better to drop this update on the floor.
9891 for (i
= 0; i
< new_map
->num_members
; i
++) {
9892 dl
= serial_to_dl(inf
[i
].serial
, super
);
9894 dprintf("disk disappeared\n");
9899 super
->updates_pending
++;
9901 /* convert spares to members and fixup ord_tbl */
9902 for (i
= 0; i
< new_map
->num_members
; i
++) {
9903 dl
= serial_to_dl(inf
[i
].serial
, super
);
9904 if (dl
->index
== -1) {
9905 dl
->index
= mpb
->num_disks
;
9907 dl
->disk
.status
|= CONFIGURED_DISK
;
9908 dl
->disk
.status
&= ~SPARE_DISK
;
9910 set_imsm_ord_tbl_ent(new_map
, i
, dl
->index
);
9915 update
->space
= NULL
;
9916 imsm_copy_dev(dev
, &u
->dev
);
9917 dv
->index
= u
->dev_idx
;
9918 dv
->next
= super
->devlist
;
9919 super
->devlist
= dv
;
9920 mpb
->num_raid_devs
++;
9922 imsm_update_version_info(super
);
9925 /* mdmon knows how to release update->space, but not
9926 * ((struct intel_dev *) update->space)->dev
9928 if (update
->space
) {
9934 case update_kill_array
: {
9935 struct imsm_update_kill_array
*u
= (void *) update
->buf
;
9936 int victim
= u
->dev_idx
;
9937 struct active_array
*a
;
9938 struct intel_dev
**dp
;
9939 struct imsm_dev
*dev
;
9941 /* sanity check that we are not affecting the uuid of
9942 * active arrays, or deleting an active array
9944 * FIXME when immutable ids are available, but note that
9945 * we'll also need to fixup the invalidated/active
9946 * subarray indexes in mdstat
9948 for (a
= st
->arrays
; a
; a
= a
->next
)
9949 if (a
->info
.container_member
>= victim
)
9951 /* by definition if mdmon is running at least one array
9952 * is active in the container, so checking
9953 * mpb->num_raid_devs is just extra paranoia
9955 dev
= get_imsm_dev(super
, victim
);
9956 if (a
|| !dev
|| mpb
->num_raid_devs
== 1) {
9957 dprintf("failed to delete subarray-%d\n", victim
);
9961 for (dp
= &super
->devlist
; *dp
;)
9962 if ((*dp
)->index
== (unsigned)super
->current_vol
) {
9965 if ((*dp
)->index
> (unsigned)victim
)
9969 mpb
->num_raid_devs
--;
9970 super
->updates_pending
++;
9973 case update_rename_array
: {
9974 struct imsm_update_rename_array
*u
= (void *) update
->buf
;
9975 char name
[MAX_RAID_SERIAL_LEN
+1];
9976 int target
= u
->dev_idx
;
9977 struct active_array
*a
;
9978 struct imsm_dev
*dev
;
9980 /* sanity check that we are not affecting the uuid of
9983 memset(name
, 0, sizeof(name
));
9984 snprintf(name
, MAX_RAID_SERIAL_LEN
, "%s", (char *) u
->name
);
9985 name
[MAX_RAID_SERIAL_LEN
] = '\0';
9986 for (a
= st
->arrays
; a
; a
= a
->next
)
9987 if (a
->info
.container_member
== target
)
9989 dev
= get_imsm_dev(super
, u
->dev_idx
);
9990 if (a
|| !dev
|| !check_name(super
, name
, 1)) {
9991 dprintf("failed to rename subarray-%d\n", target
);
9995 memcpy(dev
->volume
, name
, MAX_RAID_SERIAL_LEN
);
9996 super
->updates_pending
++;
9999 case update_add_remove_disk
: {
10000 /* we may be able to repair some arrays if disks are
10001 * being added, check the status of add_remove_disk
10002 * if discs has been added.
10004 if (add_remove_disk_update(super
)) {
10005 struct active_array
*a
;
10007 super
->updates_pending
++;
10008 for (a
= st
->arrays
; a
; a
= a
->next
)
10009 a
->check_degraded
= 1;
10013 case update_prealloc_badblocks_mem
:
10015 case update_rwh_policy
: {
10016 struct imsm_update_rwh_policy
*u
= (void *)update
->buf
;
10017 int target
= u
->dev_idx
;
10018 struct imsm_dev
*dev
= get_imsm_dev(super
, target
);
10020 dprintf("could not find subarray-%d\n", target
);
10024 if (dev
->rwh_policy
!= u
->new_policy
) {
10025 dev
->rwh_policy
= u
->new_policy
;
10026 super
->updates_pending
++;
10031 pr_err("error: unsupported process update type:(type: %d)\n", type
);
10035 static struct mdinfo
*get_spares_for_grow(struct supertype
*st
);
10037 static int imsm_prepare_update(struct supertype
*st
,
10038 struct metadata_update
*update
)
10041 * Allocate space to hold new disk entries, raid-device entries or a new
10042 * mpb if necessary. The manager synchronously waits for updates to
10043 * complete in the monitor, so new mpb buffers allocated here can be
10044 * integrated by the monitor thread without worrying about live pointers
10045 * in the manager thread.
10047 enum imsm_update_type type
;
10048 struct intel_super
*super
= st
->sb
;
10049 unsigned int sector_size
= super
->sector_size
;
10050 struct imsm_super
*mpb
= super
->anchor
;
10054 if (update
->len
< (int)sizeof(type
))
10057 type
= *(enum imsm_update_type
*) update
->buf
;
10060 case update_general_migration_checkpoint
:
10061 if (update
->len
< (int)sizeof(struct imsm_update_general_migration_checkpoint
))
10063 dprintf("called for update_general_migration_checkpoint\n");
10065 case update_takeover
: {
10066 struct imsm_update_takeover
*u
= (void *)update
->buf
;
10067 if (update
->len
< (int)sizeof(*u
))
10069 if (u
->direction
== R0_TO_R10
) {
10070 void **tail
= (void **)&update
->space_list
;
10071 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->subarray
);
10072 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
10073 int num_members
= map
->num_members
;
10076 /* allocate memory for added disks */
10077 for (i
= 0; i
< num_members
; i
++) {
10078 size
= sizeof(struct dl
);
10079 space
= xmalloc(size
);
10084 /* allocate memory for new device */
10085 size
= sizeof_imsm_dev(super
->devlist
->dev
, 0) +
10086 (num_members
* sizeof(__u32
));
10087 space
= xmalloc(size
);
10091 len
= disks_to_mpb_size(num_members
* 2);
10096 case update_reshape_container_disks
: {
10097 /* Every raid device in the container is about to
10098 * gain some more devices, and we will enter a
10100 * So each 'imsm_map' will be bigger, and the imsm_vol
10101 * will now hold 2 of them.
10102 * Thus we need new 'struct imsm_dev' allocations sized
10103 * as sizeof_imsm_dev but with more devices in both maps.
10105 struct imsm_update_reshape
*u
= (void *)update
->buf
;
10106 struct intel_dev
*dl
;
10107 void **space_tail
= (void**)&update
->space_list
;
10109 if (update
->len
< (int)sizeof(*u
))
10112 dprintf("for update_reshape\n");
10114 for (dl
= super
->devlist
; dl
; dl
= dl
->next
) {
10115 int size
= sizeof_imsm_dev(dl
->dev
, 1);
10117 if (u
->new_raid_disks
> u
->old_raid_disks
)
10118 size
+= sizeof(__u32
)*2*
10119 (u
->new_raid_disks
- u
->old_raid_disks
);
10123 *space_tail
= NULL
;
10126 len
= disks_to_mpb_size(u
->new_raid_disks
);
10127 dprintf("New anchor length is %llu\n", (unsigned long long)len
);
10130 case update_reshape_migration
: {
10131 /* for migration level 0->5 we need to add disks
10132 * so the same as for container operation we will copy
10133 * device to the bigger location.
10134 * in memory prepared device and new disk area are prepared
10135 * for usage in process update
10137 struct imsm_update_reshape_migration
*u
= (void *)update
->buf
;
10138 struct intel_dev
*id
;
10139 void **space_tail
= (void **)&update
->space_list
;
10142 int current_level
= -1;
10144 if (update
->len
< (int)sizeof(*u
))
10147 dprintf("for update_reshape\n");
10149 /* add space for bigger array in update
10151 for (id
= super
->devlist
; id
; id
= id
->next
) {
10152 if (id
->index
== (unsigned)u
->subdev
) {
10153 size
= sizeof_imsm_dev(id
->dev
, 1);
10154 if (u
->new_raid_disks
> u
->old_raid_disks
)
10155 size
+= sizeof(__u32
)*2*
10156 (u
->new_raid_disks
- u
->old_raid_disks
);
10160 *space_tail
= NULL
;
10164 if (update
->space_list
== NULL
)
10167 /* add space for disk in update
10169 size
= sizeof(struct dl
);
10173 *space_tail
= NULL
;
10175 /* add spare device to update
10177 for (id
= super
->devlist
; id
; id
= id
->next
)
10178 if (id
->index
== (unsigned)u
->subdev
) {
10179 struct imsm_dev
*dev
;
10180 struct imsm_map
*map
;
10182 dev
= get_imsm_dev(super
, u
->subdev
);
10183 map
= get_imsm_map(dev
, MAP_0
);
10184 current_level
= map
->raid_level
;
10187 if (u
->new_level
== 5 && u
->new_level
!= current_level
) {
10188 struct mdinfo
*spares
;
10190 spares
= get_spares_for_grow(st
);
10193 struct mdinfo
*dev
;
10195 dev
= spares
->devs
;
10198 makedev(dev
->disk
.major
,
10200 dl
= get_disk_super(super
,
10203 dl
->index
= u
->old_raid_disks
;
10206 sysfs_free(spares
);
10209 len
= disks_to_mpb_size(u
->new_raid_disks
);
10210 dprintf("New anchor length is %llu\n", (unsigned long long)len
);
10213 case update_size_change
: {
10214 if (update
->len
< (int)sizeof(struct imsm_update_size_change
))
10218 case update_activate_spare
: {
10219 if (update
->len
< (int)sizeof(struct imsm_update_activate_spare
))
10223 case update_create_array
: {
10224 struct imsm_update_create_array
*u
= (void *) update
->buf
;
10225 struct intel_dev
*dv
;
10226 struct imsm_dev
*dev
= &u
->dev
;
10227 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
10229 struct disk_info
*inf
;
10233 if (update
->len
< (int)sizeof(*u
))
10236 inf
= get_disk_info(u
);
10237 len
= sizeof_imsm_dev(dev
, 1);
10238 /* allocate a new super->devlist entry */
10239 dv
= xmalloc(sizeof(*dv
));
10240 dv
->dev
= xmalloc(len
);
10241 update
->space
= dv
;
10243 /* count how many spares will be converted to members */
10244 for (i
= 0; i
< map
->num_members
; i
++) {
10245 dl
= serial_to_dl(inf
[i
].serial
, super
);
10247 /* hmm maybe it failed?, nothing we can do about
10252 if (count_memberships(dl
, super
) == 0)
10255 len
+= activate
* sizeof(struct imsm_disk
);
10258 case update_kill_array
: {
10259 if (update
->len
< (int)sizeof(struct imsm_update_kill_array
))
10263 case update_rename_array
: {
10264 if (update
->len
< (int)sizeof(struct imsm_update_rename_array
))
10268 case update_add_remove_disk
:
10269 /* no update->len needed */
10271 case update_prealloc_badblocks_mem
:
10272 super
->extra_space
+= sizeof(struct bbm_log
) -
10273 get_imsm_bbm_log_size(super
->bbm_log
);
10275 case update_rwh_policy
: {
10276 if (update
->len
< (int)sizeof(struct imsm_update_rwh_policy
))
10284 /* check if we need a larger metadata buffer */
10285 if (super
->next_buf
)
10286 buf_len
= super
->next_len
;
10288 buf_len
= super
->len
;
10290 if (__le32_to_cpu(mpb
->mpb_size
) + super
->extra_space
+ len
> buf_len
) {
10291 /* ok we need a larger buf than what is currently allocated
10292 * if this allocation fails process_update will notice that
10293 * ->next_len is set and ->next_buf is NULL
10295 buf_len
= ROUND_UP(__le32_to_cpu(mpb
->mpb_size
) +
10296 super
->extra_space
+ len
, sector_size
);
10297 if (super
->next_buf
)
10298 free(super
->next_buf
);
10300 super
->next_len
= buf_len
;
10301 if (posix_memalign(&super
->next_buf
, sector_size
, buf_len
) == 0)
10302 memset(super
->next_buf
, 0, buf_len
);
10304 super
->next_buf
= NULL
;
10309 /* must be called while manager is quiesced */
10310 static void imsm_delete(struct intel_super
*super
, struct dl
**dlp
, unsigned index
)
10312 struct imsm_super
*mpb
= super
->anchor
;
10314 struct imsm_dev
*dev
;
10315 struct imsm_map
*map
;
10316 unsigned int i
, j
, num_members
;
10317 __u32 ord
, ord_map0
;
10318 struct bbm_log
*log
= super
->bbm_log
;
10320 dprintf("deleting device[%d] from imsm_super\n", index
);
10322 /* shift all indexes down one */
10323 for (iter
= super
->disks
; iter
; iter
= iter
->next
)
10324 if (iter
->index
> (int)index
)
10326 for (iter
= super
->missing
; iter
; iter
= iter
->next
)
10327 if (iter
->index
> (int)index
)
10330 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
10331 dev
= get_imsm_dev(super
, i
);
10332 map
= get_imsm_map(dev
, MAP_0
);
10333 num_members
= map
->num_members
;
10334 for (j
= 0; j
< num_members
; j
++) {
10335 /* update ord entries being careful not to propagate
10336 * ord-flags to the first map
10338 ord
= get_imsm_ord_tbl_ent(dev
, j
, MAP_X
);
10339 ord_map0
= get_imsm_ord_tbl_ent(dev
, j
, MAP_0
);
10341 if (ord_to_idx(ord
) <= index
)
10344 map
= get_imsm_map(dev
, MAP_0
);
10345 set_imsm_ord_tbl_ent(map
, j
, ord_map0
- 1);
10346 map
= get_imsm_map(dev
, MAP_1
);
10348 set_imsm_ord_tbl_ent(map
, j
, ord
- 1);
10352 for (i
= 0; i
< log
->entry_count
; i
++) {
10353 struct bbm_log_entry
*entry
= &log
->marked_block_entries
[i
];
10355 if (entry
->disk_ordinal
<= index
)
10357 entry
->disk_ordinal
--;
10361 super
->updates_pending
++;
10363 struct dl
*dl
= *dlp
;
10365 *dlp
= (*dlp
)->next
;
10366 __free_imsm_disk(dl
);
10370 static int imsm_get_allowed_degradation(int level
, int raid_disks
,
10371 struct intel_super
*super
,
10372 struct imsm_dev
*dev
)
10378 struct imsm_map
*map
;
10381 ret_val
= raid_disks
/2;
10382 /* check map if all disks pairs not failed
10385 map
= get_imsm_map(dev
, MAP_0
);
10386 for (i
= 0; i
< ret_val
; i
++) {
10387 int degradation
= 0;
10388 if (get_imsm_disk(super
, i
) == NULL
)
10390 if (get_imsm_disk(super
, i
+ 1) == NULL
)
10392 if (degradation
== 2)
10395 map
= get_imsm_map(dev
, MAP_1
);
10396 /* if there is no second map
10397 * result can be returned
10401 /* check degradation in second map
10403 for (i
= 0; i
< ret_val
; i
++) {
10404 int degradation
= 0;
10405 if (get_imsm_disk(super
, i
) == NULL
)
10407 if (get_imsm_disk(super
, i
+ 1) == NULL
)
10409 if (degradation
== 2)
10423 /*******************************************************************************
10424 * Function: validate_container_imsm
10425 * Description: This routine validates container after assemble,
10426 * eg. if devices in container are under the same controller.
10429 * info : linked list with info about devices used in array
10433 ******************************************************************************/
10434 int validate_container_imsm(struct mdinfo
*info
)
10436 if (check_env("IMSM_NO_PLATFORM"))
10439 struct sys_dev
*idev
;
10440 struct sys_dev
*hba
= NULL
;
10441 struct sys_dev
*intel_devices
= find_intel_devices();
10442 char *dev_path
= devt_to_devpath(makedev(info
->disk
.major
,
10443 info
->disk
.minor
));
10445 for (idev
= intel_devices
; idev
; idev
= idev
->next
) {
10446 if (dev_path
&& strstr(dev_path
, idev
->path
)) {
10455 pr_err("WARNING - Cannot detect HBA for device %s!\n",
10456 devid2kname(makedev(info
->disk
.major
, info
->disk
.minor
)));
10460 const struct imsm_orom
*orom
= get_orom_by_device_id(hba
->dev_id
);
10461 struct mdinfo
*dev
;
10463 for (dev
= info
->next
; dev
; dev
= dev
->next
) {
10464 dev_path
= devt_to_devpath(makedev(dev
->disk
.major
, dev
->disk
.minor
));
10466 struct sys_dev
*hba2
= NULL
;
10467 for (idev
= intel_devices
; idev
; idev
= idev
->next
) {
10468 if (dev_path
&& strstr(dev_path
, idev
->path
)) {
10476 const struct imsm_orom
*orom2
= hba2
== NULL
? NULL
:
10477 get_orom_by_device_id(hba2
->dev_id
);
10479 if (hba2
&& hba
->type
!= hba2
->type
) {
10480 pr_err("WARNING - HBAs of devices do not match %s != %s\n",
10481 get_sys_dev_type(hba
->type
), get_sys_dev_type(hba2
->type
));
10485 if (orom
!= orom2
) {
10486 pr_err("WARNING - IMSM container assembled with disks under different HBAs!\n"
10487 " This operation is not supported and can lead to data loss.\n");
10492 pr_err("WARNING - IMSM container assembled with disks under HBAs without IMSM platform support!\n"
10493 " This operation is not supported and can lead to data loss.\n");
10501 /*******************************************************************************
10502 * Function: imsm_record_badblock
10503 * Description: This routine stores new bad block record in BBM log
10506 * a : array containing a bad block
10507 * slot : disk number containing a bad block
10508 * sector : bad block sector
10509 * length : bad block sectors range
10513 ******************************************************************************/
10514 static int imsm_record_badblock(struct active_array
*a
, int slot
,
10515 unsigned long long sector
, int length
)
10517 struct intel_super
*super
= a
->container
->sb
;
10521 ord
= imsm_disk_slot_to_ord(a
, slot
);
10525 ret
= record_new_badblock(super
->bbm_log
, ord_to_idx(ord
), sector
,
10528 super
->updates_pending
++;
10532 /*******************************************************************************
10533 * Function: imsm_clear_badblock
10534 * Description: This routine clears bad block record from BBM log
10537 * a : array containing a bad block
10538 * slot : disk number containing a bad block
10539 * sector : bad block sector
10540 * length : bad block sectors range
10544 ******************************************************************************/
10545 static int imsm_clear_badblock(struct active_array
*a
, int slot
,
10546 unsigned long long sector
, int length
)
10548 struct intel_super
*super
= a
->container
->sb
;
10552 ord
= imsm_disk_slot_to_ord(a
, slot
);
10556 ret
= clear_badblock(super
->bbm_log
, ord_to_idx(ord
), sector
, length
);
10558 super
->updates_pending
++;
10562 /*******************************************************************************
10563 * Function: imsm_get_badblocks
10564 * Description: This routine get list of bad blocks for an array
10568 * slot : disk number
10570 * bb : structure containing bad blocks
10572 ******************************************************************************/
10573 static struct md_bb
*imsm_get_badblocks(struct active_array
*a
, int slot
)
10575 int inst
= a
->info
.container_member
;
10576 struct intel_super
*super
= a
->container
->sb
;
10577 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
10578 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
10581 ord
= imsm_disk_slot_to_ord(a
, slot
);
10585 get_volume_badblocks(super
->bbm_log
, ord_to_idx(ord
), pba_of_lba0(map
),
10586 per_dev_array_size(map
), &super
->bb
);
10590 /*******************************************************************************
10591 * Function: examine_badblocks_imsm
10592 * Description: Prints list of bad blocks on a disk to the standard output
10595 * st : metadata handler
10596 * fd : open file descriptor for device
10597 * devname : device name
10601 ******************************************************************************/
10602 static int examine_badblocks_imsm(struct supertype
*st
, int fd
, char *devname
)
10604 struct intel_super
*super
= st
->sb
;
10605 struct bbm_log
*log
= super
->bbm_log
;
10606 struct dl
*d
= NULL
;
10609 for (d
= super
->disks
; d
; d
= d
->next
) {
10610 if (strcmp(d
->devname
, devname
) == 0)
10614 if ((d
== NULL
) || (d
->index
< 0)) { /* serial mismatch probably */
10615 pr_err("%s doesn't appear to be part of a raid array\n",
10622 struct bbm_log_entry
*entry
= &log
->marked_block_entries
[0];
10624 for (i
= 0; i
< log
->entry_count
; i
++) {
10625 if (entry
[i
].disk_ordinal
== d
->index
) {
10626 unsigned long long sector
= __le48_to_cpu(
10627 &entry
[i
].defective_block_start
);
10628 int cnt
= entry
[i
].marked_count
+ 1;
10631 printf("Bad-blocks on %s:\n", devname
);
10635 printf("%20llu for %d sectors\n", sector
, cnt
);
10641 printf("No bad-blocks list configured on %s\n", devname
);
10645 /*******************************************************************************
10646 * Function: init_migr_record_imsm
10647 * Description: Function inits imsm migration record
10649 * super : imsm internal array info
10650 * dev : device under migration
10651 * info : general array info to find the smallest device
10654 ******************************************************************************/
10655 void init_migr_record_imsm(struct supertype
*st
, struct imsm_dev
*dev
,
10656 struct mdinfo
*info
)
10658 struct intel_super
*super
= st
->sb
;
10659 struct migr_record
*migr_rec
= super
->migr_rec
;
10660 int new_data_disks
;
10661 unsigned long long dsize
, dev_sectors
;
10662 long long unsigned min_dev_sectors
= -1LLU;
10663 struct imsm_map
*map_dest
= get_imsm_map(dev
, MAP_0
);
10664 struct imsm_map
*map_src
= get_imsm_map(dev
, MAP_1
);
10665 unsigned long long num_migr_units
;
10666 unsigned long long array_blocks
;
10667 struct dl
*dl_disk
= NULL
;
10669 memset(migr_rec
, 0, sizeof(struct migr_record
));
10670 migr_rec
->family_num
= __cpu_to_le32(super
->anchor
->family_num
);
10672 /* only ascending reshape supported now */
10673 migr_rec
->ascending_migr
= __cpu_to_le32(1);
10675 migr_rec
->dest_depth_per_unit
= GEN_MIGR_AREA_SIZE
/
10676 max(map_dest
->blocks_per_strip
, map_src
->blocks_per_strip
);
10677 migr_rec
->dest_depth_per_unit
*=
10678 max(map_dest
->blocks_per_strip
, map_src
->blocks_per_strip
);
10679 new_data_disks
= imsm_num_data_members(map_dest
);
10680 migr_rec
->blocks_per_unit
=
10681 __cpu_to_le32(migr_rec
->dest_depth_per_unit
* new_data_disks
);
10682 migr_rec
->dest_depth_per_unit
=
10683 __cpu_to_le32(migr_rec
->dest_depth_per_unit
);
10684 array_blocks
= info
->component_size
* new_data_disks
;
10686 array_blocks
/ __le32_to_cpu(migr_rec
->blocks_per_unit
);
10688 if (array_blocks
% __le32_to_cpu(migr_rec
->blocks_per_unit
))
10690 set_num_migr_units(migr_rec
, num_migr_units
);
10692 migr_rec
->post_migr_vol_cap
= dev
->size_low
;
10693 migr_rec
->post_migr_vol_cap_hi
= dev
->size_high
;
10695 /* Find the smallest dev */
10696 for (dl_disk
= super
->disks
; dl_disk
; dl_disk
= dl_disk
->next
) {
10697 /* ignore spares in container */
10698 if (dl_disk
->index
< 0)
10700 get_dev_size(dl_disk
->fd
, NULL
, &dsize
);
10701 dev_sectors
= dsize
/ 512;
10702 if (dev_sectors
< min_dev_sectors
)
10703 min_dev_sectors
= dev_sectors
;
10705 set_migr_chkp_area_pba(migr_rec
, min_dev_sectors
-
10706 RAID_DISK_RESERVED_BLOCKS_IMSM_HI
);
10708 write_imsm_migr_rec(st
);
10713 /*******************************************************************************
10714 * Function: save_backup_imsm
10715 * Description: Function saves critical data stripes to Migration Copy Area
10716 * and updates the current migration unit status.
10717 * Use restore_stripes() to form a destination stripe,
10718 * and to write it to the Copy Area.
10720 * st : supertype information
10721 * dev : imsm device that backup is saved for
10722 * info : general array info
10723 * buf : input buffer
10724 * length : length of data to backup (blocks_per_unit)
10728 ******************************************************************************/
10729 int save_backup_imsm(struct supertype
*st
,
10730 struct imsm_dev
*dev
,
10731 struct mdinfo
*info
,
10736 struct intel_super
*super
= st
->sb
;
10737 unsigned long long *target_offsets
;
10740 struct imsm_map
*map_dest
= get_imsm_map(dev
, MAP_0
);
10741 int new_disks
= map_dest
->num_members
;
10742 int dest_layout
= 0;
10744 unsigned long long start
;
10745 int data_disks
= imsm_num_data_members(map_dest
);
10747 targets
= xmalloc(new_disks
* sizeof(int));
10749 for (i
= 0; i
< new_disks
; i
++) {
10750 struct dl
*dl_disk
= get_imsm_dl_disk(super
, i
);
10752 targets
[i
] = dl_disk
->fd
;
10755 target_offsets
= xcalloc(new_disks
, sizeof(unsigned long long));
10757 start
= info
->reshape_progress
* 512;
10758 for (i
= 0; i
< new_disks
; i
++) {
10759 target_offsets
[i
] = migr_chkp_area_pba(super
->migr_rec
) * 512;
10760 /* move back copy area adderss, it will be moved forward
10761 * in restore_stripes() using start input variable
10763 target_offsets
[i
] -= start
/data_disks
;
10766 dest_layout
= imsm_level_to_layout(map_dest
->raid_level
);
10767 dest_chunk
= __le16_to_cpu(map_dest
->blocks_per_strip
) * 512;
10769 if (restore_stripes(targets
, /* list of dest devices */
10770 target_offsets
, /* migration record offsets */
10773 map_dest
->raid_level
,
10775 -1, /* source backup file descriptor */
10776 0, /* input buf offset
10777 * always 0 buf is already offseted */
10781 pr_err("Error restoring stripes\n");
10791 free(target_offsets
);
10796 /*******************************************************************************
10797 * Function: save_checkpoint_imsm
10798 * Description: Function called for current unit status update
10799 * in the migration record. It writes it to disk.
10801 * super : imsm internal array info
10802 * info : general array info
10806 * 2: failure, means no valid migration record
10807 * / no general migration in progress /
10808 ******************************************************************************/
10809 int save_checkpoint_imsm(struct supertype
*st
, struct mdinfo
*info
, int state
)
10811 struct intel_super
*super
= st
->sb
;
10812 unsigned long long blocks_per_unit
;
10813 unsigned long long curr_migr_unit
;
10815 if (load_imsm_migr_rec(super
) != 0) {
10816 dprintf("imsm: ERROR: Cannot read migration record for checkpoint save.\n");
10820 blocks_per_unit
= __le32_to_cpu(super
->migr_rec
->blocks_per_unit
);
10821 if (blocks_per_unit
== 0) {
10822 dprintf("imsm: no migration in progress.\n");
10825 curr_migr_unit
= info
->reshape_progress
/ blocks_per_unit
;
10826 /* check if array is alligned to copy area
10827 * if it is not alligned, add one to current migration unit value
10828 * this can happend on array reshape finish only
10830 if (info
->reshape_progress
% blocks_per_unit
)
10833 set_current_migr_unit(super
->migr_rec
, curr_migr_unit
);
10834 super
->migr_rec
->rec_status
= __cpu_to_le32(state
);
10835 set_migr_dest_1st_member_lba(super
->migr_rec
,
10836 super
->migr_rec
->dest_depth_per_unit
* curr_migr_unit
);
10838 if (write_imsm_migr_rec(st
) < 0) {
10839 dprintf("imsm: Cannot write migration record outside backup area\n");
10846 /*******************************************************************************
10847 * Function: recover_backup_imsm
10848 * Description: Function recovers critical data from the Migration Copy Area
10849 * while assembling an array.
10851 * super : imsm internal array info
10852 * info : general array info
10854 * 0 : success (or there is no data to recover)
10856 ******************************************************************************/
10857 int recover_backup_imsm(struct supertype
*st
, struct mdinfo
*info
)
10859 struct intel_super
*super
= st
->sb
;
10860 struct migr_record
*migr_rec
= super
->migr_rec
;
10861 struct imsm_map
*map_dest
;
10862 struct intel_dev
*id
= NULL
;
10863 unsigned long long read_offset
;
10864 unsigned long long write_offset
;
10866 int new_disks
, err
;
10869 unsigned int sector_size
= super
->sector_size
;
10870 unsigned long curr_migr_unit
= current_migr_unit(migr_rec
);
10871 unsigned long num_migr_units
= get_num_migr_units(migr_rec
);
10873 int skipped_disks
= 0;
10874 struct dl
*dl_disk
;
10876 err
= sysfs_get_str(info
, NULL
, "array_state", (char *)buffer
, 20);
10880 /* recover data only during assemblation */
10881 if (strncmp(buffer
, "inactive", 8) != 0)
10883 /* no data to recover */
10884 if (__le32_to_cpu(migr_rec
->rec_status
) == UNIT_SRC_NORMAL
)
10886 if (curr_migr_unit
>= num_migr_units
)
10889 /* find device during reshape */
10890 for (id
= super
->devlist
; id
; id
= id
->next
)
10891 if (is_gen_migration(id
->dev
))
10896 map_dest
= get_imsm_map(id
->dev
, MAP_0
);
10897 new_disks
= map_dest
->num_members
;
10899 read_offset
= migr_chkp_area_pba(migr_rec
) * 512;
10901 write_offset
= (migr_dest_1st_member_lba(migr_rec
) +
10902 pba_of_lba0(map_dest
)) * 512;
10904 unit_len
= __le32_to_cpu(migr_rec
->dest_depth_per_unit
) * 512;
10905 if (posix_memalign((void **)&buf
, sector_size
, unit_len
) != 0)
10908 for (dl_disk
= super
->disks
; dl_disk
; dl_disk
= dl_disk
->next
) {
10909 if (dl_disk
->index
< 0)
10912 if (dl_disk
->fd
< 0) {
10916 if (lseek64(dl_disk
->fd
, read_offset
, SEEK_SET
) < 0) {
10917 pr_err("Cannot seek to block: %s\n",
10922 if (read(dl_disk
->fd
, buf
, unit_len
) != unit_len
) {
10923 pr_err("Cannot read copy area block: %s\n",
10928 if (lseek64(dl_disk
->fd
, write_offset
, SEEK_SET
) < 0) {
10929 pr_err("Cannot seek to block: %s\n",
10934 if (write(dl_disk
->fd
, buf
, unit_len
) != unit_len
) {
10935 pr_err("Cannot restore block: %s\n",
10942 if (skipped_disks
> imsm_get_allowed_degradation(info
->new_level
,
10946 pr_err("Cannot restore data from backup. Too many failed disks\n");
10950 if (save_checkpoint_imsm(st
, info
, UNIT_SRC_NORMAL
)) {
10951 /* ignore error == 2, this can mean end of reshape here
10953 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL) during restart\n");
10962 static char disk_by_path
[] = "/dev/disk/by-path/";
10964 static const char *imsm_get_disk_controller_domain(const char *path
)
10966 char disk_path
[PATH_MAX
];
10970 strcpy(disk_path
, disk_by_path
);
10971 strncat(disk_path
, path
, PATH_MAX
- strlen(disk_path
) - 1);
10972 if (stat(disk_path
, &st
) == 0) {
10973 struct sys_dev
* hba
;
10976 path
= devt_to_devpath(st
.st_rdev
);
10979 hba
= find_disk_attached_hba(-1, path
);
10980 if (hba
&& hba
->type
== SYS_DEV_SAS
)
10982 else if (hba
&& hba
->type
== SYS_DEV_SATA
)
10984 else if (hba
&& hba
->type
== SYS_DEV_VMD
)
10986 else if (hba
&& hba
->type
== SYS_DEV_NVME
)
10990 dprintf("path: %s hba: %s attached: %s\n",
10991 path
, (hba
) ? hba
->path
: "NULL", drv
);
10997 static char *imsm_find_array_devnm_by_subdev(int subdev
, char *container
)
10999 static char devnm
[32];
11000 char subdev_name
[20];
11001 struct mdstat_ent
*mdstat
;
11003 sprintf(subdev_name
, "%d", subdev
);
11004 mdstat
= mdstat_by_subdev(subdev_name
, container
);
11008 strcpy(devnm
, mdstat
->devnm
);
11009 free_mdstat(mdstat
);
11013 static int imsm_reshape_is_allowed_on_container(struct supertype
*st
,
11014 struct geo_params
*geo
,
11015 int *old_raid_disks
,
11018 /* currently we only support increasing the number of devices
11019 * for a container. This increases the number of device for each
11020 * member array. They must all be RAID0 or RAID5.
11023 struct mdinfo
*info
, *member
;
11024 int devices_that_can_grow
= 0;
11026 dprintf("imsm: imsm_reshape_is_allowed_on_container(ENTER): st->devnm = (%s)\n", st
->devnm
);
11028 if (geo
->size
> 0 ||
11029 geo
->level
!= UnSet
||
11030 geo
->layout
!= UnSet
||
11031 geo
->chunksize
!= 0 ||
11032 geo
->raid_disks
== UnSet
) {
11033 dprintf("imsm: Container operation is allowed for raid disks number change only.\n");
11037 if (direction
== ROLLBACK_METADATA_CHANGES
) {
11038 dprintf("imsm: Metadata changes rollback is not supported for container operation.\n");
11042 info
= container_content_imsm(st
, NULL
);
11043 for (member
= info
; member
; member
= member
->next
) {
11046 dprintf("imsm: checking device_num: %i\n",
11047 member
->container_member
);
11049 if (geo
->raid_disks
<= member
->array
.raid_disks
) {
11050 /* we work on container for Online Capacity Expansion
11051 * only so raid_disks has to grow
11053 dprintf("imsm: for container operation raid disks increase is required\n");
11057 if (info
->array
.level
!= 0 && info
->array
.level
!= 5) {
11058 /* we cannot use this container with other raid level
11060 dprintf("imsm: for container operation wrong raid level (%i) detected\n",
11061 info
->array
.level
);
11064 /* check for platform support
11065 * for this raid level configuration
11067 struct intel_super
*super
= st
->sb
;
11068 if (!is_raid_level_supported(super
->orom
,
11069 member
->array
.level
,
11070 geo
->raid_disks
)) {
11071 dprintf("platform does not support raid%d with %d disk%s\n",
11074 geo
->raid_disks
> 1 ? "s" : "");
11077 /* check if component size is aligned to chunk size
11079 if (info
->component_size
%
11080 (info
->array
.chunk_size
/512)) {
11081 dprintf("Component size is not aligned to chunk size\n");
11086 if (*old_raid_disks
&&
11087 info
->array
.raid_disks
!= *old_raid_disks
)
11089 *old_raid_disks
= info
->array
.raid_disks
;
11091 /* All raid5 and raid0 volumes in container
11092 * have to be ready for Online Capacity Expansion
11093 * so they need to be assembled. We have already
11094 * checked that no recovery etc is happening.
11096 result
= imsm_find_array_devnm_by_subdev(member
->container_member
,
11097 st
->container_devnm
);
11098 if (result
== NULL
) {
11099 dprintf("imsm: cannot find array\n");
11102 devices_that_can_grow
++;
11105 if (!member
&& devices_that_can_grow
)
11109 dprintf("Container operation allowed\n");
11111 dprintf("Error: %i\n", ret_val
);
11116 /* Function: get_spares_for_grow
11117 * Description: Allocates memory and creates list of spare devices
11118 * avaliable in container. Checks if spare drive size is acceptable.
11119 * Parameters: Pointer to the supertype structure
11120 * Returns: Pointer to the list of spare devices (mdinfo structure) on success,
11123 static struct mdinfo
*get_spares_for_grow(struct supertype
*st
)
11125 struct spare_criteria sc
;
11127 get_spare_criteria_imsm(st
, &sc
);
11128 return container_choose_spares(st
, &sc
, NULL
, NULL
, NULL
, 0);
11131 /******************************************************************************
11132 * function: imsm_create_metadata_update_for_reshape
11133 * Function creates update for whole IMSM container.
11135 ******************************************************************************/
11136 static int imsm_create_metadata_update_for_reshape(
11137 struct supertype
*st
,
11138 struct geo_params
*geo
,
11139 int old_raid_disks
,
11140 struct imsm_update_reshape
**updatep
)
11142 struct intel_super
*super
= st
->sb
;
11143 struct imsm_super
*mpb
= super
->anchor
;
11144 int update_memory_size
;
11145 struct imsm_update_reshape
*u
;
11146 struct mdinfo
*spares
;
11149 struct mdinfo
*dev
;
11151 dprintf("(enter) raid_disks = %i\n", geo
->raid_disks
);
11153 delta_disks
= geo
->raid_disks
- old_raid_disks
;
11155 /* size of all update data without anchor */
11156 update_memory_size
= sizeof(struct imsm_update_reshape
);
11158 /* now add space for spare disks that we need to add. */
11159 update_memory_size
+= sizeof(u
->new_disks
[0]) * (delta_disks
- 1);
11161 u
= xcalloc(1, update_memory_size
);
11162 u
->type
= update_reshape_container_disks
;
11163 u
->old_raid_disks
= old_raid_disks
;
11164 u
->new_raid_disks
= geo
->raid_disks
;
11166 /* now get spare disks list
11168 spares
= get_spares_for_grow(st
);
11170 if (spares
== NULL
|| delta_disks
> spares
->array
.spare_disks
) {
11171 pr_err("imsm: ERROR: Cannot get spare devices for %s.\n", geo
->dev_name
);
11176 /* we have got spares
11177 * update disk list in imsm_disk list table in anchor
11179 dprintf("imsm: %i spares are available.\n\n",
11180 spares
->array
.spare_disks
);
11182 dev
= spares
->devs
;
11183 for (i
= 0; i
< delta_disks
; i
++) {
11188 u
->new_disks
[i
] = makedev(dev
->disk
.major
,
11190 dl
= get_disk_super(super
, dev
->disk
.major
, dev
->disk
.minor
);
11191 dl
->index
= mpb
->num_disks
;
11199 sysfs_free(spares
);
11201 dprintf("imsm: reshape update preparation :");
11202 if (i
== delta_disks
) {
11203 dprintf_cont(" OK\n");
11205 return update_memory_size
;
11208 dprintf_cont(" Error\n");
11213 /******************************************************************************
11214 * function: imsm_create_metadata_update_for_size_change()
11215 * Creates update for IMSM array for array size change.
11217 ******************************************************************************/
11218 static int imsm_create_metadata_update_for_size_change(
11219 struct supertype
*st
,
11220 struct geo_params
*geo
,
11221 struct imsm_update_size_change
**updatep
)
11223 struct intel_super
*super
= st
->sb
;
11224 int update_memory_size
;
11225 struct imsm_update_size_change
*u
;
11227 dprintf("(enter) New size = %llu\n", geo
->size
);
11229 /* size of all update data without anchor */
11230 update_memory_size
= sizeof(struct imsm_update_size_change
);
11232 u
= xcalloc(1, update_memory_size
);
11233 u
->type
= update_size_change
;
11234 u
->subdev
= super
->current_vol
;
11235 u
->new_size
= geo
->size
;
11237 dprintf("imsm: reshape update preparation : OK\n");
11240 return update_memory_size
;
11243 /******************************************************************************
11244 * function: imsm_create_metadata_update_for_migration()
11245 * Creates update for IMSM array.
11247 ******************************************************************************/
11248 static int imsm_create_metadata_update_for_migration(
11249 struct supertype
*st
,
11250 struct geo_params
*geo
,
11251 struct imsm_update_reshape_migration
**updatep
)
11253 struct intel_super
*super
= st
->sb
;
11254 int update_memory_size
;
11255 struct imsm_update_reshape_migration
*u
;
11256 struct imsm_dev
*dev
;
11257 int previous_level
= -1;
11259 dprintf("(enter) New Level = %i\n", geo
->level
);
11261 /* size of all update data without anchor */
11262 update_memory_size
= sizeof(struct imsm_update_reshape_migration
);
11264 u
= xcalloc(1, update_memory_size
);
11265 u
->type
= update_reshape_migration
;
11266 u
->subdev
= super
->current_vol
;
11267 u
->new_level
= geo
->level
;
11268 u
->new_layout
= geo
->layout
;
11269 u
->new_raid_disks
= u
->old_raid_disks
= geo
->raid_disks
;
11270 u
->new_disks
[0] = -1;
11271 u
->new_chunksize
= -1;
11273 dev
= get_imsm_dev(super
, u
->subdev
);
11275 struct imsm_map
*map
;
11277 map
= get_imsm_map(dev
, MAP_0
);
11279 int current_chunk_size
=
11280 __le16_to_cpu(map
->blocks_per_strip
) / 2;
11282 if (geo
->chunksize
!= current_chunk_size
) {
11283 u
->new_chunksize
= geo
->chunksize
/ 1024;
11284 dprintf("imsm: chunk size change from %i to %i\n",
11285 current_chunk_size
, u
->new_chunksize
);
11287 previous_level
= map
->raid_level
;
11290 if (geo
->level
== 5 && previous_level
== 0) {
11291 struct mdinfo
*spares
= NULL
;
11293 u
->new_raid_disks
++;
11294 spares
= get_spares_for_grow(st
);
11295 if (spares
== NULL
|| spares
->array
.spare_disks
< 1) {
11297 sysfs_free(spares
);
11298 update_memory_size
= 0;
11299 pr_err("cannot get spare device for requested migration\n");
11302 sysfs_free(spares
);
11304 dprintf("imsm: reshape update preparation : OK\n");
11307 return update_memory_size
;
11310 static void imsm_update_metadata_locally(struct supertype
*st
,
11311 void *buf
, int len
)
11313 struct metadata_update mu
;
11318 mu
.space_list
= NULL
;
11320 if (imsm_prepare_update(st
, &mu
))
11321 imsm_process_update(st
, &mu
);
11323 while (mu
.space_list
) {
11324 void **space
= mu
.space_list
;
11325 mu
.space_list
= *space
;
11330 /***************************************************************************
11331 * Function: imsm_analyze_change
11332 * Description: Function analyze change for single volume
11333 * and validate if transition is supported
11334 * Parameters: Geometry parameters, supertype structure,
11335 * metadata change direction (apply/rollback)
11336 * Returns: Operation type code on success, -1 if fail
11337 ****************************************************************************/
11338 enum imsm_reshape_type
imsm_analyze_change(struct supertype
*st
,
11339 struct geo_params
*geo
,
11342 struct mdinfo info
;
11344 int check_devs
= 0;
11346 /* number of added/removed disks in operation result */
11347 int devNumChange
= 0;
11348 /* imsm compatible layout value for array geometry verification */
11349 int imsm_layout
= -1;
11351 struct imsm_dev
*dev
;
11352 struct imsm_map
*map
;
11353 struct intel_super
*super
;
11354 unsigned long long current_size
;
11355 unsigned long long free_size
;
11356 unsigned long long max_size
;
11359 getinfo_super_imsm_volume(st
, &info
, NULL
);
11360 if (geo
->level
!= info
.array
.level
&& geo
->level
>= 0 &&
11361 geo
->level
!= UnSet
) {
11362 switch (info
.array
.level
) {
11364 if (geo
->level
== 5) {
11365 change
= CH_MIGRATION
;
11366 if (geo
->layout
!= ALGORITHM_LEFT_ASYMMETRIC
) {
11367 pr_err("Error. Requested Layout not supported (left-asymmetric layout is supported only)!\n");
11369 goto analyse_change_exit
;
11371 imsm_layout
= geo
->layout
;
11373 devNumChange
= 1; /* parity disk added */
11374 } else if (geo
->level
== 10) {
11375 change
= CH_TAKEOVER
;
11377 devNumChange
= 2; /* two mirrors added */
11378 imsm_layout
= 0x102; /* imsm supported layout */
11383 if (geo
->level
== 0) {
11384 change
= CH_TAKEOVER
;
11386 devNumChange
= -(geo
->raid_disks
/2);
11387 imsm_layout
= 0; /* imsm raid0 layout */
11391 if (change
== -1) {
11392 pr_err("Error. Level Migration from %d to %d not supported!\n",
11393 info
.array
.level
, geo
->level
);
11394 goto analyse_change_exit
;
11397 geo
->level
= info
.array
.level
;
11399 if (geo
->layout
!= info
.array
.layout
&&
11400 (geo
->layout
!= UnSet
&& geo
->layout
!= -1)) {
11401 change
= CH_MIGRATION
;
11402 if (info
.array
.layout
== 0 && info
.array
.level
== 5 &&
11403 geo
->layout
== 5) {
11404 /* reshape 5 -> 4 */
11405 } else if (info
.array
.layout
== 5 && info
.array
.level
== 5 &&
11406 geo
->layout
== 0) {
11407 /* reshape 4 -> 5 */
11411 pr_err("Error. Layout Migration from %d to %d not supported!\n",
11412 info
.array
.layout
, geo
->layout
);
11414 goto analyse_change_exit
;
11417 geo
->layout
= info
.array
.layout
;
11418 if (imsm_layout
== -1)
11419 imsm_layout
= info
.array
.layout
;
11422 if (geo
->chunksize
> 0 && geo
->chunksize
!= UnSet
&&
11423 geo
->chunksize
!= info
.array
.chunk_size
) {
11424 if (info
.array
.level
== 10) {
11425 pr_err("Error. Chunk size change for RAID 10 is not supported.\n");
11427 goto analyse_change_exit
;
11428 } else if (info
.component_size
% (geo
->chunksize
/512)) {
11429 pr_err("New chunk size (%dK) does not evenly divide device size (%lluk). Aborting...\n",
11430 geo
->chunksize
/1024, info
.component_size
/2);
11432 goto analyse_change_exit
;
11434 change
= CH_MIGRATION
;
11436 geo
->chunksize
= info
.array
.chunk_size
;
11439 chunk
= geo
->chunksize
/ 1024;
11442 dev
= get_imsm_dev(super
, super
->current_vol
);
11443 map
= get_imsm_map(dev
, MAP_0
);
11444 data_disks
= imsm_num_data_members(map
);
11445 /* compute current size per disk member
11447 current_size
= info
.custom_array_size
/ data_disks
;
11449 if (geo
->size
> 0 && geo
->size
!= MAX_SIZE
) {
11450 /* align component size
11452 geo
->size
= imsm_component_size_alignment_check(
11453 get_imsm_raid_level(dev
->vol
.map
),
11454 chunk
* 1024, super
->sector_size
,
11456 if (geo
->size
== 0) {
11457 pr_err("Error. Size expansion is supported only (current size is %llu, requested size /rounded/ is 0).\n",
11459 goto analyse_change_exit
;
11463 if (current_size
!= geo
->size
&& geo
->size
> 0) {
11464 if (change
!= -1) {
11465 pr_err("Error. Size change should be the only one at a time.\n");
11467 goto analyse_change_exit
;
11469 if ((super
->current_vol
+ 1) != super
->anchor
->num_raid_devs
) {
11470 pr_err("Error. The last volume in container can be expanded only (%i/%s).\n",
11471 super
->current_vol
, st
->devnm
);
11472 goto analyse_change_exit
;
11474 /* check the maximum available size
11476 rv
= imsm_get_free_size(st
, dev
->vol
.map
->num_members
,
11477 0, chunk
, &free_size
);
11479 /* Cannot find maximum available space
11483 max_size
= free_size
+ current_size
;
11484 /* align component size
11486 max_size
= imsm_component_size_alignment_check(
11487 get_imsm_raid_level(dev
->vol
.map
),
11488 chunk
* 1024, super
->sector_size
,
11491 if (geo
->size
== MAX_SIZE
) {
11492 /* requested size change to the maximum available size
11494 if (max_size
== 0) {
11495 pr_err("Error. Cannot find maximum available space.\n");
11497 goto analyse_change_exit
;
11499 geo
->size
= max_size
;
11502 if (direction
== ROLLBACK_METADATA_CHANGES
) {
11503 /* accept size for rollback only
11506 /* round size due to metadata compatibility
11508 geo
->size
= (geo
->size
>> SECT_PER_MB_SHIFT
)
11509 << SECT_PER_MB_SHIFT
;
11510 dprintf("Prepare update for size change to %llu\n",
11512 if (current_size
>= geo
->size
) {
11513 pr_err("Error. Size expansion is supported only (current size is %llu, requested size /rounded/ is %llu).\n",
11514 current_size
, geo
->size
);
11515 goto analyse_change_exit
;
11517 if (max_size
&& geo
->size
> max_size
) {
11518 pr_err("Error. Requested size is larger than maximum available size (maximum available size is %llu, requested size /rounded/ is %llu).\n",
11519 max_size
, geo
->size
);
11520 goto analyse_change_exit
;
11523 geo
->size
*= data_disks
;
11524 geo
->raid_disks
= dev
->vol
.map
->num_members
;
11525 change
= CH_ARRAY_SIZE
;
11527 if (!validate_geometry_imsm(st
,
11530 geo
->raid_disks
+ devNumChange
,
11532 geo
->size
, INVALID_SECTORS
,
11533 0, 0, info
.consistency_policy
, 1))
11537 struct intel_super
*super
= st
->sb
;
11538 struct imsm_super
*mpb
= super
->anchor
;
11540 if (mpb
->num_raid_devs
> 1) {
11541 pr_err("Error. Cannot perform operation on %s- for this operation it MUST be single array in container\n",
11547 analyse_change_exit
:
11548 if (direction
== ROLLBACK_METADATA_CHANGES
&&
11549 (change
== CH_MIGRATION
|| change
== CH_TAKEOVER
)) {
11550 dprintf("imsm: Metadata changes rollback is not supported for migration and takeover operations.\n");
11556 int imsm_takeover(struct supertype
*st
, struct geo_params
*geo
)
11558 struct intel_super
*super
= st
->sb
;
11559 struct imsm_update_takeover
*u
;
11561 u
= xmalloc(sizeof(struct imsm_update_takeover
));
11563 u
->type
= update_takeover
;
11564 u
->subarray
= super
->current_vol
;
11566 /* 10->0 transition */
11567 if (geo
->level
== 0)
11568 u
->direction
= R10_TO_R0
;
11570 /* 0->10 transition */
11571 if (geo
->level
== 10)
11572 u
->direction
= R0_TO_R10
;
11574 /* update metadata locally */
11575 imsm_update_metadata_locally(st
, u
,
11576 sizeof(struct imsm_update_takeover
));
11577 /* and possibly remotely */
11578 if (st
->update_tail
)
11579 append_metadata_update(st
, u
,
11580 sizeof(struct imsm_update_takeover
));
11587 /* Flush size update if size calculated by num_data_stripes is higher than
11588 * imsm_dev_size to eliminate differences during reshape.
11589 * Mdmon will recalculate them correctly.
11590 * If subarray index is not set then check whole container.
11592 * 0 - no error occurred
11593 * 1 - error detected
11595 static int imsm_fix_size_mismatch(struct supertype
*st
, int subarray_index
)
11597 struct intel_super
*super
= st
->sb
;
11598 int tmp
= super
->current_vol
;
11602 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
11603 if (subarray_index
>= 0 && i
!= subarray_index
)
11605 super
->current_vol
= i
;
11606 struct imsm_dev
*dev
= get_imsm_dev(super
, super
->current_vol
);
11607 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
11608 unsigned int disc_count
= imsm_num_data_members(map
);
11609 struct geo_params geo
;
11610 struct imsm_update_size_change
*update
;
11611 unsigned long long calc_size
= per_dev_array_size(map
) * disc_count
;
11612 unsigned long long d_size
= imsm_dev_size(dev
);
11615 if (calc_size
== d_size
|| dev
->vol
.migr_type
== MIGR_GEN_MIGR
)
11618 /* There is a difference, verify that imsm_dev_size is
11619 * rounded correctly and push update.
11621 if (d_size
!= round_size_to_mb(d_size
, disc_count
)) {
11622 dprintf("imsm: Size of volume %d is not rounded correctly\n",
11626 memset(&geo
, 0, sizeof(struct geo_params
));
11628 u_size
= imsm_create_metadata_update_for_size_change(st
, &geo
,
11631 dprintf("imsm: Cannot prepare size change update\n");
11634 imsm_update_metadata_locally(st
, update
, u_size
);
11635 if (st
->update_tail
) {
11636 append_metadata_update(st
, update
, u_size
);
11637 flush_metadata_updates(st
);
11638 st
->update_tail
= &st
->updates
;
11640 imsm_sync_metadata(st
);
11645 super
->current_vol
= tmp
;
11649 static int imsm_reshape_super(struct supertype
*st
, unsigned long long size
,
11651 int layout
, int chunksize
, int raid_disks
,
11652 int delta_disks
, char *backup
, char *dev
,
11653 int direction
, int verbose
)
11656 struct geo_params geo
;
11658 dprintf("(enter)\n");
11660 memset(&geo
, 0, sizeof(struct geo_params
));
11662 geo
.dev_name
= dev
;
11663 strcpy(geo
.devnm
, st
->devnm
);
11666 geo
.layout
= layout
;
11667 geo
.chunksize
= chunksize
;
11668 geo
.raid_disks
= raid_disks
;
11669 if (delta_disks
!= UnSet
)
11670 geo
.raid_disks
+= delta_disks
;
11672 dprintf("for level : %i\n", geo
.level
);
11673 dprintf("for raid_disks : %i\n", geo
.raid_disks
);
11675 if (strcmp(st
->container_devnm
, st
->devnm
) == 0) {
11676 /* On container level we can only increase number of devices. */
11677 dprintf("imsm: info: Container operation\n");
11678 int old_raid_disks
= 0;
11680 if (imsm_reshape_is_allowed_on_container(
11681 st
, &geo
, &old_raid_disks
, direction
)) {
11682 struct imsm_update_reshape
*u
= NULL
;
11685 if (imsm_fix_size_mismatch(st
, -1)) {
11686 dprintf("imsm: Cannot fix size mismatch\n");
11687 goto exit_imsm_reshape_super
;
11690 len
= imsm_create_metadata_update_for_reshape(
11691 st
, &geo
, old_raid_disks
, &u
);
11694 dprintf("imsm: Cannot prepare update\n");
11695 goto exit_imsm_reshape_super
;
11699 /* update metadata locally */
11700 imsm_update_metadata_locally(st
, u
, len
);
11701 /* and possibly remotely */
11702 if (st
->update_tail
)
11703 append_metadata_update(st
, u
, len
);
11708 pr_err("(imsm) Operation is not allowed on this container\n");
11711 /* On volume level we support following operations
11712 * - takeover: raid10 -> raid0; raid0 -> raid10
11713 * - chunk size migration
11714 * - migration: raid5 -> raid0; raid0 -> raid5
11716 struct intel_super
*super
= st
->sb
;
11717 struct intel_dev
*dev
= super
->devlist
;
11719 dprintf("imsm: info: Volume operation\n");
11720 /* find requested device */
11723 imsm_find_array_devnm_by_subdev(
11724 dev
->index
, st
->container_devnm
);
11725 if (devnm
&& strcmp(devnm
, geo
.devnm
) == 0)
11730 pr_err("Cannot find %s (%s) subarray\n",
11731 geo
.dev_name
, geo
.devnm
);
11732 goto exit_imsm_reshape_super
;
11734 super
->current_vol
= dev
->index
;
11735 change
= imsm_analyze_change(st
, &geo
, direction
);
11738 ret_val
= imsm_takeover(st
, &geo
);
11740 case CH_MIGRATION
: {
11741 struct imsm_update_reshape_migration
*u
= NULL
;
11743 imsm_create_metadata_update_for_migration(
11746 dprintf("imsm: Cannot prepare update\n");
11750 /* update metadata locally */
11751 imsm_update_metadata_locally(st
, u
, len
);
11752 /* and possibly remotely */
11753 if (st
->update_tail
)
11754 append_metadata_update(st
, u
, len
);
11759 case CH_ARRAY_SIZE
: {
11760 struct imsm_update_size_change
*u
= NULL
;
11762 imsm_create_metadata_update_for_size_change(
11765 dprintf("imsm: Cannot prepare update\n");
11769 /* update metadata locally */
11770 imsm_update_metadata_locally(st
, u
, len
);
11771 /* and possibly remotely */
11772 if (st
->update_tail
)
11773 append_metadata_update(st
, u
, len
);
11783 exit_imsm_reshape_super
:
11784 dprintf("imsm: reshape_super Exit code = %i\n", ret_val
);
11788 #define COMPLETED_OK 0
11789 #define COMPLETED_NONE 1
11790 #define COMPLETED_DELAYED 2
11792 static int read_completed(int fd
, unsigned long long *val
)
11797 ret
= sysfs_fd_get_str(fd
, buf
, 50);
11801 ret
= COMPLETED_OK
;
11802 if (strncmp(buf
, "none", 4) == 0) {
11803 ret
= COMPLETED_NONE
;
11804 } else if (strncmp(buf
, "delayed", 7) == 0) {
11805 ret
= COMPLETED_DELAYED
;
11808 *val
= strtoull(buf
, &ep
, 0);
11809 if (ep
== buf
|| (*ep
!= 0 && *ep
!= '\n' && *ep
!= ' '))
11815 /*******************************************************************************
11816 * Function: wait_for_reshape_imsm
11817 * Description: Function writes new sync_max value and waits until
11818 * reshape process reach new position
11820 * sra : general array info
11821 * ndata : number of disks in new array's layout
11824 * 1 : there is no reshape in progress,
11826 ******************************************************************************/
11827 int wait_for_reshape_imsm(struct mdinfo
*sra
, int ndata
)
11829 int fd
= sysfs_get_fd(sra
, NULL
, "sync_completed");
11831 unsigned long long completed
;
11832 /* to_complete : new sync_max position */
11833 unsigned long long to_complete
= sra
->reshape_progress
;
11834 unsigned long long position_to_set
= to_complete
/ ndata
;
11837 dprintf("cannot open reshape_position\n");
11842 if (sysfs_fd_get_ll(fd
, &completed
) < 0) {
11844 dprintf("cannot read reshape_position (no reshape in progres)\n");
11853 if (completed
> position_to_set
) {
11854 dprintf("wrong next position to set %llu (%llu)\n",
11855 to_complete
, position_to_set
);
11859 dprintf("Position set: %llu\n", position_to_set
);
11860 if (sysfs_set_num(sra
, NULL
, "sync_max",
11861 position_to_set
) != 0) {
11862 dprintf("cannot set reshape position to %llu\n",
11871 int timeout
= 3000;
11873 sysfs_wait(fd
, &timeout
);
11874 if (sysfs_get_str(sra
, NULL
, "sync_action",
11876 strncmp(action
, "reshape", 7) != 0) {
11877 if (strncmp(action
, "idle", 4) == 0)
11883 rc
= read_completed(fd
, &completed
);
11885 dprintf("cannot read reshape_position (in loop)\n");
11888 } else if (rc
== COMPLETED_NONE
)
11890 } while (completed
< position_to_set
);
11896 /*******************************************************************************
11897 * Function: check_degradation_change
11898 * Description: Check that array hasn't become failed.
11900 * info : for sysfs access
11901 * sources : source disks descriptors
11902 * degraded: previous degradation level
11904 * degradation level
11905 ******************************************************************************/
11906 int check_degradation_change(struct mdinfo
*info
,
11910 unsigned long long new_degraded
;
11913 rv
= sysfs_get_ll(info
, NULL
, "degraded", &new_degraded
);
11914 if (rv
== -1 || (new_degraded
!= (unsigned long long)degraded
)) {
11915 /* check each device to ensure it is still working */
11918 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
11919 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
11921 if (sd
->disk
.state
& (1<<MD_DISK_SYNC
)) {
11924 if (sysfs_get_str(info
,
11925 sd
, "state", sbuf
, sizeof(sbuf
)) < 0 ||
11926 strstr(sbuf
, "faulty") ||
11927 strstr(sbuf
, "in_sync") == NULL
) {
11928 /* this device is dead */
11929 sd
->disk
.state
= (1<<MD_DISK_FAULTY
);
11930 if (sd
->disk
.raid_disk
>= 0 &&
11931 sources
[sd
->disk
.raid_disk
] >= 0) {
11933 sd
->disk
.raid_disk
]);
11934 sources
[sd
->disk
.raid_disk
] =
11943 return new_degraded
;
11946 /*******************************************************************************
11947 * Function: imsm_manage_reshape
11948 * Description: Function finds array under reshape and it manages reshape
11949 * process. It creates stripes backups (if required) and sets
11952 * afd : Backup handle (nattive) - not used
11953 * sra : general array info
11954 * reshape : reshape parameters - not used
11955 * st : supertype structure
11956 * blocks : size of critical section [blocks]
11957 * fds : table of source device descriptor
11958 * offsets : start of array (offest per devices)
11960 * destfd : table of destination device descriptor
11961 * destoffsets : table of destination offsets (per device)
11963 * 1 : success, reshape is done
11965 ******************************************************************************/
11966 static int imsm_manage_reshape(
11967 int afd
, struct mdinfo
*sra
, struct reshape
*reshape
,
11968 struct supertype
*st
, unsigned long backup_blocks
,
11969 int *fds
, unsigned long long *offsets
,
11970 int dests
, int *destfd
, unsigned long long *destoffsets
)
11973 struct intel_super
*super
= st
->sb
;
11974 struct intel_dev
*dv
;
11975 unsigned int sector_size
= super
->sector_size
;
11976 struct imsm_dev
*dev
= NULL
;
11977 struct imsm_map
*map_src
, *map_dest
;
11978 int migr_vol_qan
= 0;
11979 int ndata
, odata
; /* [bytes] */
11980 int chunk
; /* [bytes] */
11981 struct migr_record
*migr_rec
;
11983 unsigned int buf_size
; /* [bytes] */
11984 unsigned long long max_position
; /* array size [bytes] */
11985 unsigned long long next_step
; /* [blocks]/[bytes] */
11986 unsigned long long old_data_stripe_length
;
11987 unsigned long long start_src
; /* [bytes] */
11988 unsigned long long start
; /* [bytes] */
11989 unsigned long long start_buf_shift
; /* [bytes] */
11991 int source_layout
= 0;
11992 int subarray_index
= -1;
11997 if (!fds
|| !offsets
)
12000 /* Find volume during the reshape */
12001 for (dv
= super
->devlist
; dv
; dv
= dv
->next
) {
12002 if (dv
->dev
->vol
.migr_type
== MIGR_GEN_MIGR
&&
12003 dv
->dev
->vol
.migr_state
== 1) {
12006 subarray_index
= dv
->index
;
12009 /* Only one volume can migrate at the same time */
12010 if (migr_vol_qan
!= 1) {
12011 pr_err("%s", migr_vol_qan
?
12012 "Number of migrating volumes greater than 1\n" :
12013 "There is no volume during migrationg\n");
12017 map_dest
= get_imsm_map(dev
, MAP_0
);
12018 map_src
= get_imsm_map(dev
, MAP_1
);
12019 if (map_src
== NULL
)
12022 ndata
= imsm_num_data_members(map_dest
);
12023 odata
= imsm_num_data_members(map_src
);
12025 chunk
= __le16_to_cpu(map_src
->blocks_per_strip
) * 512;
12026 old_data_stripe_length
= odata
* chunk
;
12028 migr_rec
= super
->migr_rec
;
12030 /* initialize migration record for start condition */
12031 if (sra
->reshape_progress
== 0)
12032 init_migr_record_imsm(st
, dev
, sra
);
12034 if (__le32_to_cpu(migr_rec
->rec_status
) != UNIT_SRC_NORMAL
) {
12035 dprintf("imsm: cannot restart migration when data are present in copy area.\n");
12038 /* Save checkpoint to update migration record for current
12039 * reshape position (in md). It can be farther than current
12040 * reshape position in metadata.
12042 if (save_checkpoint_imsm(st
, sra
, UNIT_SRC_NORMAL
) == 1) {
12043 /* ignore error == 2, this can mean end of reshape here
12045 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL, initial save)\n");
12050 /* size for data */
12051 buf_size
= __le32_to_cpu(migr_rec
->blocks_per_unit
) * 512;
12052 /* extend buffer size for parity disk */
12053 buf_size
+= __le32_to_cpu(migr_rec
->dest_depth_per_unit
) * 512;
12054 /* add space for stripe alignment */
12055 buf_size
+= old_data_stripe_length
;
12056 if (posix_memalign((void **)&buf
, MAX_SECTOR_SIZE
, buf_size
)) {
12057 dprintf("imsm: Cannot allocate checkpoint buffer\n");
12061 max_position
= sra
->component_size
* ndata
;
12062 source_layout
= imsm_level_to_layout(map_src
->raid_level
);
12064 while (current_migr_unit(migr_rec
) <
12065 get_num_migr_units(migr_rec
)) {
12066 /* current reshape position [blocks] */
12067 unsigned long long current_position
=
12068 __le32_to_cpu(migr_rec
->blocks_per_unit
)
12069 * current_migr_unit(migr_rec
);
12070 unsigned long long border
;
12072 /* Check that array hasn't become failed.
12074 degraded
= check_degradation_change(sra
, fds
, degraded
);
12075 if (degraded
> 1) {
12076 dprintf("imsm: Abort reshape due to degradation level (%i)\n", degraded
);
12080 next_step
= __le32_to_cpu(migr_rec
->blocks_per_unit
);
12082 if ((current_position
+ next_step
) > max_position
)
12083 next_step
= max_position
- current_position
;
12085 start
= current_position
* 512;
12087 /* align reading start to old geometry */
12088 start_buf_shift
= start
% old_data_stripe_length
;
12089 start_src
= start
- start_buf_shift
;
12091 border
= (start_src
/ odata
) - (start
/ ndata
);
12093 if (border
<= __le32_to_cpu(migr_rec
->dest_depth_per_unit
)) {
12094 /* save critical stripes to buf
12095 * start - start address of current unit
12096 * to backup [bytes]
12097 * start_src - start address of current unit
12098 * to backup alligned to source array
12101 unsigned long long next_step_filler
;
12102 unsigned long long copy_length
= next_step
* 512;
12104 /* allign copy area length to stripe in old geometry */
12105 next_step_filler
= ((copy_length
+ start_buf_shift
)
12106 % old_data_stripe_length
);
12107 if (next_step_filler
)
12108 next_step_filler
= (old_data_stripe_length
12109 - next_step_filler
);
12110 dprintf("save_stripes() parameters: start = %llu,\tstart_src = %llu,\tnext_step*512 = %llu,\tstart_in_buf_shift = %llu,\tnext_step_filler = %llu\n",
12111 start
, start_src
, copy_length
,
12112 start_buf_shift
, next_step_filler
);
12114 if (save_stripes(fds
, offsets
, map_src
->num_members
,
12115 chunk
, map_src
->raid_level
,
12116 source_layout
, 0, NULL
, start_src
,
12118 next_step_filler
+ start_buf_shift
,
12120 dprintf("imsm: Cannot save stripes to buffer\n");
12123 /* Convert data to destination format and store it
12124 * in backup general migration area
12126 if (save_backup_imsm(st
, dev
, sra
,
12127 buf
+ start_buf_shift
, copy_length
)) {
12128 dprintf("imsm: Cannot save stripes to target devices\n");
12131 if (save_checkpoint_imsm(st
, sra
,
12132 UNIT_SRC_IN_CP_AREA
)) {
12133 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_IN_CP_AREA)\n");
12137 /* set next step to use whole border area */
12138 border
/= next_step
;
12140 next_step
*= border
;
12142 /* When data backed up, checkpoint stored,
12143 * kick the kernel to reshape unit of data
12145 next_step
= next_step
+ sra
->reshape_progress
;
12146 /* limit next step to array max position */
12147 if (next_step
> max_position
)
12148 next_step
= max_position
;
12149 sysfs_set_num(sra
, NULL
, "suspend_lo", sra
->reshape_progress
);
12150 sysfs_set_num(sra
, NULL
, "suspend_hi", next_step
);
12151 sra
->reshape_progress
= next_step
;
12153 /* wait until reshape finish */
12154 if (wait_for_reshape_imsm(sra
, ndata
)) {
12155 dprintf("wait_for_reshape_imsm returned error!\n");
12161 if (save_checkpoint_imsm(st
, sra
, UNIT_SRC_NORMAL
) == 1) {
12162 /* ignore error == 2, this can mean end of reshape here
12164 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL)\n");
12170 /* clear migr_rec on disks after successful migration */
12173 memset(super
->migr_rec_buf
, 0, MIGR_REC_BUF_SECTORS
*MAX_SECTOR_SIZE
);
12174 for (d
= super
->disks
; d
; d
= d
->next
) {
12175 if (d
->index
< 0 || is_failed(&d
->disk
))
12177 unsigned long long dsize
;
12179 get_dev_size(d
->fd
, NULL
, &dsize
);
12180 if (lseek64(d
->fd
, dsize
- MIGR_REC_SECTOR_POSITION
*sector_size
,
12182 if ((unsigned int)write(d
->fd
, super
->migr_rec_buf
,
12183 MIGR_REC_BUF_SECTORS
*sector_size
) !=
12184 MIGR_REC_BUF_SECTORS
*sector_size
)
12185 perror("Write migr_rec failed");
12189 /* return '1' if done */
12192 /* After the reshape eliminate size mismatch in metadata.
12193 * Don't update md/component_size here, volume hasn't
12194 * to take whole space. It is allowed by kernel.
12195 * md/component_size will be set propoperly after next assembly.
12197 imsm_fix_size_mismatch(st
, subarray_index
);
12201 /* See Grow.c: abort_reshape() for further explanation */
12202 sysfs_set_num(sra
, NULL
, "suspend_lo", 0x7FFFFFFFFFFFFFFFULL
);
12203 sysfs_set_num(sra
, NULL
, "suspend_hi", 0);
12204 sysfs_set_num(sra
, NULL
, "suspend_lo", 0);
12209 struct superswitch super_imsm
= {
12210 .examine_super
= examine_super_imsm
,
12211 .brief_examine_super
= brief_examine_super_imsm
,
12212 .brief_examine_subarrays
= brief_examine_subarrays_imsm
,
12213 .export_examine_super
= export_examine_super_imsm
,
12214 .detail_super
= detail_super_imsm
,
12215 .brief_detail_super
= brief_detail_super_imsm
,
12216 .write_init_super
= write_init_super_imsm
,
12217 .validate_geometry
= validate_geometry_imsm
,
12218 .add_to_super
= add_to_super_imsm
,
12219 .remove_from_super
= remove_from_super_imsm
,
12220 .detail_platform
= detail_platform_imsm
,
12221 .export_detail_platform
= export_detail_platform_imsm
,
12222 .kill_subarray
= kill_subarray_imsm
,
12223 .update_subarray
= update_subarray_imsm
,
12224 .load_container
= load_container_imsm
,
12225 .default_geometry
= default_geometry_imsm
,
12226 .get_disk_controller_domain
= imsm_get_disk_controller_domain
,
12227 .reshape_super
= imsm_reshape_super
,
12228 .manage_reshape
= imsm_manage_reshape
,
12229 .recover_backup
= recover_backup_imsm
,
12230 .examine_badblocks
= examine_badblocks_imsm
,
12231 .match_home
= match_home_imsm
,
12232 .uuid_from_super
= uuid_from_super_imsm
,
12233 .getinfo_super
= getinfo_super_imsm
,
12234 .getinfo_super_disks
= getinfo_super_disks_imsm
,
12235 .update_super
= update_super_imsm
,
12237 .avail_size
= avail_size_imsm
,
12238 .get_spare_criteria
= get_spare_criteria_imsm
,
12240 .compare_super
= compare_super_imsm
,
12242 .load_super
= load_super_imsm
,
12243 .init_super
= init_super_imsm
,
12244 .store_super
= store_super_imsm
,
12245 .free_super
= free_super_imsm
,
12246 .match_metadata_desc
= match_metadata_desc_imsm
,
12247 .container_content
= container_content_imsm
,
12248 .validate_container
= validate_container_imsm
,
12250 .write_init_ppl
= write_init_ppl_imsm
,
12251 .validate_ppl
= validate_ppl_imsm
,
12257 .open_new
= imsm_open_new
,
12258 .set_array_state
= imsm_set_array_state
,
12259 .set_disk
= imsm_set_disk
,
12260 .sync_metadata
= imsm_sync_metadata
,
12261 .activate_spare
= imsm_activate_spare
,
12262 .process_update
= imsm_process_update
,
12263 .prepare_update
= imsm_prepare_update
,
12264 .record_bad_block
= imsm_record_badblock
,
12265 .clear_bad_block
= imsm_clear_badblock
,
12266 .get_bad_blocks
= imsm_get_badblocks
,