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(" UUID : %s\n", uuid
);
1583 printf(" RAID Level : %d", get_imsm_raid_level(map
));
1585 printf(" <-- %d", get_imsm_raid_level(map2
));
1587 printf(" Members : %d", map
->num_members
);
1589 printf(" <-- %d", map2
->num_members
);
1591 printf(" Slots : [");
1592 for (i
= 0; i
< map
->num_members
; i
++) {
1593 ord
= get_imsm_ord_tbl_ent(dev
, i
, MAP_0
);
1594 printf("%s", ord
& IMSM_ORD_REBUILD
? "_" : "U");
1599 for (i
= 0; i
< map2
->num_members
; i
++) {
1600 ord
= get_imsm_ord_tbl_ent(dev
, i
, MAP_1
);
1601 printf("%s", ord
& IMSM_ORD_REBUILD
? "_" : "U");
1606 printf(" Failed disk : ");
1607 if (map
->failed_disk_num
== 0xff)
1610 printf("%i", map
->failed_disk_num
);
1612 slot
= get_imsm_disk_slot(map
, disk_idx
);
1614 ord
= get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
);
1615 printf(" This Slot : %d%s\n", slot
,
1616 ord
& IMSM_ORD_REBUILD
? " (out-of-sync)" : "");
1618 printf(" This Slot : ?\n");
1619 printf(" Sector Size : %u\n", super
->sector_size
);
1620 sz
= imsm_dev_size(dev
);
1621 printf(" Array Size : %llu%s\n",
1622 (unsigned long long)sz
* 512 / super
->sector_size
,
1623 human_size(sz
* 512));
1624 sz
= blocks_per_member(map
);
1625 printf(" Per Dev Size : %llu%s\n",
1626 (unsigned long long)sz
* 512 / super
->sector_size
,
1627 human_size(sz
* 512));
1628 printf(" Sector Offset : %llu\n",
1630 printf(" Num Stripes : %llu\n",
1631 num_data_stripes(map
));
1632 printf(" Chunk Size : %u KiB",
1633 __le16_to_cpu(map
->blocks_per_strip
) / 2);
1635 printf(" <-- %u KiB",
1636 __le16_to_cpu(map2
->blocks_per_strip
) / 2);
1638 printf(" Reserved : %d\n", __le32_to_cpu(dev
->reserved_blocks
));
1639 printf(" Migrate State : ");
1640 if (dev
->vol
.migr_state
) {
1641 if (migr_type(dev
) == MIGR_INIT
)
1642 printf("initialize\n");
1643 else if (migr_type(dev
) == MIGR_REBUILD
)
1644 printf("rebuild\n");
1645 else if (migr_type(dev
) == MIGR_VERIFY
)
1647 else if (migr_type(dev
) == MIGR_GEN_MIGR
)
1648 printf("general migration\n");
1649 else if (migr_type(dev
) == MIGR_STATE_CHANGE
)
1650 printf("state change\n");
1651 else if (migr_type(dev
) == MIGR_REPAIR
)
1654 printf("<unknown:%d>\n", migr_type(dev
));
1657 printf(" Map State : %s", map_state_str
[map
->map_state
]);
1658 if (dev
->vol
.migr_state
) {
1659 struct imsm_map
*map
= get_imsm_map(dev
, MAP_1
);
1661 printf(" <-- %s", map_state_str
[map
->map_state
]);
1662 printf("\n Checkpoint : %u ",
1663 __le32_to_cpu(dev
->vol
.curr_migr_unit
));
1664 if (is_gen_migration(dev
) && (slot
> 1 || slot
< 0))
1667 printf("(%llu)", (unsigned long long)
1668 blocks_per_migr_unit(super
, dev
));
1671 printf(" Dirty State : %s\n", (dev
->vol
.dirty
& RAIDVOL_DIRTY
) ?
1673 printf(" RWH Policy : ");
1674 if (dev
->rwh_policy
== RWH_OFF
|| dev
->rwh_policy
== RWH_MULTIPLE_OFF
)
1676 else if (dev
->rwh_policy
== RWH_DISTRIBUTED
)
1677 printf("PPL distributed\n");
1678 else if (dev
->rwh_policy
== RWH_JOURNALING_DRIVE
)
1679 printf("PPL journaling drive\n");
1680 else if (dev
->rwh_policy
== RWH_MULTIPLE_DISTRIBUTED
)
1681 printf("Multiple distributed PPLs\n");
1682 else if (dev
->rwh_policy
== RWH_MULTIPLE_PPLS_JOURNALING_DRIVE
)
1683 printf("Multiple PPLs on journaling drive\n");
1685 printf("<unknown:%d>\n", dev
->rwh_policy
);
1688 static void print_imsm_disk(struct imsm_disk
*disk
,
1691 unsigned int sector_size
) {
1692 char str
[MAX_RAID_SERIAL_LEN
+ 1];
1695 if (index
< -1 || !disk
)
1699 snprintf(str
, MAX_RAID_SERIAL_LEN
+ 1, "%s", disk
->serial
);
1701 printf(" Disk%02d Serial : %s\n", index
, str
);
1703 printf(" Disk Serial : %s\n", str
);
1704 printf(" State :%s%s%s%s\n", is_spare(disk
) ? " spare" : "",
1705 is_configured(disk
) ? " active" : "",
1706 is_failed(disk
) ? " failed" : "",
1707 is_journal(disk
) ? " journal" : "");
1708 printf(" Id : %08x\n", __le32_to_cpu(disk
->scsi_id
));
1709 sz
= total_blocks(disk
) - reserved
;
1710 printf(" Usable Size : %llu%s\n",
1711 (unsigned long long)sz
* 512 / sector_size
,
1712 human_size(sz
* 512));
1715 void convert_to_4k_imsm_migr_rec(struct intel_super
*super
)
1717 struct migr_record
*migr_rec
= super
->migr_rec
;
1719 migr_rec
->blocks_per_unit
/= IMSM_4K_DIV
;
1720 migr_rec
->dest_depth_per_unit
/= IMSM_4K_DIV
;
1721 split_ull((join_u32(migr_rec
->post_migr_vol_cap
,
1722 migr_rec
->post_migr_vol_cap_hi
) / IMSM_4K_DIV
),
1723 &migr_rec
->post_migr_vol_cap
, &migr_rec
->post_migr_vol_cap_hi
);
1724 set_migr_chkp_area_pba(migr_rec
,
1725 migr_chkp_area_pba(migr_rec
) / IMSM_4K_DIV
);
1726 set_migr_dest_1st_member_lba(migr_rec
,
1727 migr_dest_1st_member_lba(migr_rec
) / IMSM_4K_DIV
);
1730 void convert_to_4k_imsm_disk(struct imsm_disk
*disk
)
1732 set_total_blocks(disk
, (total_blocks(disk
)/IMSM_4K_DIV
));
1735 void convert_to_4k(struct intel_super
*super
)
1737 struct imsm_super
*mpb
= super
->anchor
;
1738 struct imsm_disk
*disk
;
1740 __u32 bbm_log_size
= __le32_to_cpu(mpb
->bbm_log_size
);
1742 for (i
= 0; i
< mpb
->num_disks
; i
++) {
1743 disk
= __get_imsm_disk(mpb
, i
);
1745 convert_to_4k_imsm_disk(disk
);
1747 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
1748 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
1749 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
1751 set_imsm_dev_size(dev
, imsm_dev_size(dev
)/IMSM_4K_DIV
);
1752 dev
->vol
.curr_migr_unit
/= IMSM_4K_DIV
;
1755 set_blocks_per_member(map
, blocks_per_member(map
)/IMSM_4K_DIV
);
1756 map
->blocks_per_strip
/= IMSM_4K_DIV
;
1757 set_pba_of_lba0(map
, pba_of_lba0(map
)/IMSM_4K_DIV
);
1759 if (dev
->vol
.migr_state
) {
1761 map
= get_imsm_map(dev
, MAP_1
);
1762 set_blocks_per_member(map
,
1763 blocks_per_member(map
)/IMSM_4K_DIV
);
1764 map
->blocks_per_strip
/= IMSM_4K_DIV
;
1765 set_pba_of_lba0(map
, pba_of_lba0(map
)/IMSM_4K_DIV
);
1769 struct bbm_log
*log
= (void *)mpb
+
1770 __le32_to_cpu(mpb
->mpb_size
) - bbm_log_size
;
1773 for (i
= 0; i
< log
->entry_count
; i
++) {
1774 struct bbm_log_entry
*entry
=
1775 &log
->marked_block_entries
[i
];
1777 __u8 count
= entry
->marked_count
+ 1;
1778 unsigned long long sector
=
1779 __le48_to_cpu(&entry
->defective_block_start
);
1781 entry
->defective_block_start
=
1782 __cpu_to_le48(sector
/IMSM_4K_DIV
);
1783 entry
->marked_count
= max(count
/IMSM_4K_DIV
, 1) - 1;
1787 mpb
->check_sum
= __gen_imsm_checksum(mpb
);
1790 void examine_migr_rec_imsm(struct intel_super
*super
)
1792 struct migr_record
*migr_rec
= super
->migr_rec
;
1793 struct imsm_super
*mpb
= super
->anchor
;
1796 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
1797 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
1798 struct imsm_map
*map
;
1801 if (is_gen_migration(dev
) == 0)
1804 printf("\nMigration Record Information:");
1806 /* first map under migration */
1807 map
= get_imsm_map(dev
, MAP_0
);
1809 slot
= get_imsm_disk_slot(map
, super
->disks
->index
);
1810 if (map
== NULL
|| slot
> 1 || slot
< 0) {
1811 printf(" Empty\n ");
1812 printf("Examine one of first two disks in array\n");
1815 printf("\n Status : ");
1816 if (__le32_to_cpu(migr_rec
->rec_status
) == UNIT_SRC_NORMAL
)
1819 printf("Contains Data\n");
1820 printf(" Current Unit : %llu\n",
1821 current_migr_unit(migr_rec
));
1822 printf(" Family : %u\n",
1823 __le32_to_cpu(migr_rec
->family_num
));
1824 printf(" Ascending : %u\n",
1825 __le32_to_cpu(migr_rec
->ascending_migr
));
1826 printf(" Blocks Per Unit : %u\n",
1827 __le32_to_cpu(migr_rec
->blocks_per_unit
));
1828 printf(" Dest. Depth Per Unit : %u\n",
1829 __le32_to_cpu(migr_rec
->dest_depth_per_unit
));
1830 printf(" Checkpoint Area pba : %llu\n",
1831 migr_chkp_area_pba(migr_rec
));
1832 printf(" First member lba : %llu\n",
1833 migr_dest_1st_member_lba(migr_rec
));
1834 printf(" Total Number of Units : %llu\n",
1835 get_num_migr_units(migr_rec
));
1836 printf(" Size of volume : %llu\n",
1837 join_u32(migr_rec
->post_migr_vol_cap
,
1838 migr_rec
->post_migr_vol_cap_hi
));
1839 printf(" Record was read from : %u\n",
1840 __le32_to_cpu(migr_rec
->ckpt_read_disk_num
));
1846 void convert_from_4k_imsm_migr_rec(struct intel_super
*super
)
1848 struct migr_record
*migr_rec
= super
->migr_rec
;
1850 migr_rec
->blocks_per_unit
*= IMSM_4K_DIV
;
1851 migr_rec
->dest_depth_per_unit
*= IMSM_4K_DIV
;
1852 split_ull((join_u32(migr_rec
->post_migr_vol_cap
,
1853 migr_rec
->post_migr_vol_cap_hi
) * IMSM_4K_DIV
),
1854 &migr_rec
->post_migr_vol_cap
,
1855 &migr_rec
->post_migr_vol_cap_hi
);
1856 set_migr_chkp_area_pba(migr_rec
,
1857 migr_chkp_area_pba(migr_rec
) * IMSM_4K_DIV
);
1858 set_migr_dest_1st_member_lba(migr_rec
,
1859 migr_dest_1st_member_lba(migr_rec
) * IMSM_4K_DIV
);
1862 void convert_from_4k(struct intel_super
*super
)
1864 struct imsm_super
*mpb
= super
->anchor
;
1865 struct imsm_disk
*disk
;
1867 __u32 bbm_log_size
= __le32_to_cpu(mpb
->bbm_log_size
);
1869 for (i
= 0; i
< mpb
->num_disks
; i
++) {
1870 disk
= __get_imsm_disk(mpb
, i
);
1872 set_total_blocks(disk
, (total_blocks(disk
)*IMSM_4K_DIV
));
1875 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
1876 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
1877 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
1879 set_imsm_dev_size(dev
, imsm_dev_size(dev
)*IMSM_4K_DIV
);
1880 dev
->vol
.curr_migr_unit
*= IMSM_4K_DIV
;
1883 set_blocks_per_member(map
, blocks_per_member(map
)*IMSM_4K_DIV
);
1884 map
->blocks_per_strip
*= IMSM_4K_DIV
;
1885 set_pba_of_lba0(map
, pba_of_lba0(map
)*IMSM_4K_DIV
);
1887 if (dev
->vol
.migr_state
) {
1889 map
= get_imsm_map(dev
, MAP_1
);
1890 set_blocks_per_member(map
,
1891 blocks_per_member(map
)*IMSM_4K_DIV
);
1892 map
->blocks_per_strip
*= IMSM_4K_DIV
;
1893 set_pba_of_lba0(map
, pba_of_lba0(map
)*IMSM_4K_DIV
);
1897 struct bbm_log
*log
= (void *)mpb
+
1898 __le32_to_cpu(mpb
->mpb_size
) - bbm_log_size
;
1901 for (i
= 0; i
< log
->entry_count
; i
++) {
1902 struct bbm_log_entry
*entry
=
1903 &log
->marked_block_entries
[i
];
1905 __u8 count
= entry
->marked_count
+ 1;
1906 unsigned long long sector
=
1907 __le48_to_cpu(&entry
->defective_block_start
);
1909 entry
->defective_block_start
=
1910 __cpu_to_le48(sector
*IMSM_4K_DIV
);
1911 entry
->marked_count
= count
*IMSM_4K_DIV
- 1;
1915 mpb
->check_sum
= __gen_imsm_checksum(mpb
);
1918 /*******************************************************************************
1919 * function: imsm_check_attributes
1920 * Description: Function checks if features represented by attributes flags
1921 * are supported by mdadm.
1923 * attributes - Attributes read from metadata
1925 * 0 - passed attributes contains unsupported features flags
1926 * 1 - all features are supported
1927 ******************************************************************************/
1928 static int imsm_check_attributes(__u32 attributes
)
1931 __u32 not_supported
= MPB_ATTRIB_SUPPORTED
^0xffffffff;
1933 not_supported
&= ~MPB_ATTRIB_IGNORED
;
1935 not_supported
&= attributes
;
1936 if (not_supported
) {
1937 pr_err("(IMSM): Unsupported attributes : %x\n",
1938 (unsigned)__le32_to_cpu(not_supported
));
1939 if (not_supported
& MPB_ATTRIB_CHECKSUM_VERIFY
) {
1940 dprintf("\t\tMPB_ATTRIB_CHECKSUM_VERIFY \n");
1941 not_supported
^= MPB_ATTRIB_CHECKSUM_VERIFY
;
1943 if (not_supported
& MPB_ATTRIB_2TB
) {
1944 dprintf("\t\tMPB_ATTRIB_2TB\n");
1945 not_supported
^= MPB_ATTRIB_2TB
;
1947 if (not_supported
& MPB_ATTRIB_RAID0
) {
1948 dprintf("\t\tMPB_ATTRIB_RAID0\n");
1949 not_supported
^= MPB_ATTRIB_RAID0
;
1951 if (not_supported
& MPB_ATTRIB_RAID1
) {
1952 dprintf("\t\tMPB_ATTRIB_RAID1\n");
1953 not_supported
^= MPB_ATTRIB_RAID1
;
1955 if (not_supported
& MPB_ATTRIB_RAID10
) {
1956 dprintf("\t\tMPB_ATTRIB_RAID10\n");
1957 not_supported
^= MPB_ATTRIB_RAID10
;
1959 if (not_supported
& MPB_ATTRIB_RAID1E
) {
1960 dprintf("\t\tMPB_ATTRIB_RAID1E\n");
1961 not_supported
^= MPB_ATTRIB_RAID1E
;
1963 if (not_supported
& MPB_ATTRIB_RAID5
) {
1964 dprintf("\t\tMPB_ATTRIB_RAID5\n");
1965 not_supported
^= MPB_ATTRIB_RAID5
;
1967 if (not_supported
& MPB_ATTRIB_RAIDCNG
) {
1968 dprintf("\t\tMPB_ATTRIB_RAIDCNG\n");
1969 not_supported
^= MPB_ATTRIB_RAIDCNG
;
1971 if (not_supported
& MPB_ATTRIB_BBM
) {
1972 dprintf("\t\tMPB_ATTRIB_BBM\n");
1973 not_supported
^= MPB_ATTRIB_BBM
;
1975 if (not_supported
& MPB_ATTRIB_CHECKSUM_VERIFY
) {
1976 dprintf("\t\tMPB_ATTRIB_CHECKSUM_VERIFY (== MPB_ATTRIB_LEGACY)\n");
1977 not_supported
^= MPB_ATTRIB_CHECKSUM_VERIFY
;
1979 if (not_supported
& MPB_ATTRIB_EXP_STRIPE_SIZE
) {
1980 dprintf("\t\tMPB_ATTRIB_EXP_STRIP_SIZE\n");
1981 not_supported
^= MPB_ATTRIB_EXP_STRIPE_SIZE
;
1983 if (not_supported
& MPB_ATTRIB_2TB_DISK
) {
1984 dprintf("\t\tMPB_ATTRIB_2TB_DISK\n");
1985 not_supported
^= MPB_ATTRIB_2TB_DISK
;
1987 if (not_supported
& MPB_ATTRIB_NEVER_USE2
) {
1988 dprintf("\t\tMPB_ATTRIB_NEVER_USE2\n");
1989 not_supported
^= MPB_ATTRIB_NEVER_USE2
;
1991 if (not_supported
& MPB_ATTRIB_NEVER_USE
) {
1992 dprintf("\t\tMPB_ATTRIB_NEVER_USE\n");
1993 not_supported
^= MPB_ATTRIB_NEVER_USE
;
1997 dprintf("(IMSM): Unknown attributes : %x\n", not_supported
);
2005 static void getinfo_super_imsm(struct supertype
*st
, struct mdinfo
*info
, char *map
);
2007 static void examine_super_imsm(struct supertype
*st
, char *homehost
)
2009 struct intel_super
*super
= st
->sb
;
2010 struct imsm_super
*mpb
= super
->anchor
;
2011 char str
[MAX_SIGNATURE_LENGTH
];
2016 __u32 reserved
= imsm_reserved_sectors(super
, super
->disks
);
2018 time_t creation_time
;
2020 strncpy(str
, (char *)mpb
->sig
, MPB_SIG_LEN
);
2021 str
[MPB_SIG_LEN
-1] = '\0';
2022 printf(" Magic : %s\n", str
);
2023 printf(" Version : %s\n", get_imsm_version(mpb
));
2024 printf(" Orig Family : %08x\n", __le32_to_cpu(mpb
->orig_family_num
));
2025 printf(" Family : %08x\n", __le32_to_cpu(mpb
->family_num
));
2026 printf(" Generation : %08x\n", __le32_to_cpu(mpb
->generation_num
));
2027 creation_time
= __le64_to_cpu(mpb
->creation_time
);
2028 printf(" Creation Time : %.24s\n",
2029 creation_time
? ctime(&creation_time
) : "Unknown");
2030 printf(" Attributes : ");
2031 if (imsm_check_attributes(mpb
->attributes
))
2032 printf("All supported\n");
2034 printf("not supported\n");
2035 getinfo_super_imsm(st
, &info
, NULL
);
2036 fname_from_uuid(st
, &info
, nbuf
, ':');
2037 printf(" UUID : %s\n", nbuf
+ 5);
2038 sum
= __le32_to_cpu(mpb
->check_sum
);
2039 printf(" Checksum : %08x %s\n", sum
,
2040 __gen_imsm_checksum(mpb
) == sum
? "correct" : "incorrect");
2041 printf(" MPB Sectors : %d\n", mpb_sectors(mpb
, super
->sector_size
));
2042 printf(" Disks : %d\n", mpb
->num_disks
);
2043 printf(" RAID Devices : %d\n", mpb
->num_raid_devs
);
2044 print_imsm_disk(__get_imsm_disk(mpb
, super
->disks
->index
),
2045 super
->disks
->index
, reserved
, super
->sector_size
);
2046 if (get_imsm_bbm_log_size(super
->bbm_log
)) {
2047 struct bbm_log
*log
= super
->bbm_log
;
2050 printf("Bad Block Management Log:\n");
2051 printf(" Log Size : %d\n", __le32_to_cpu(mpb
->bbm_log_size
));
2052 printf(" Signature : %x\n", __le32_to_cpu(log
->signature
));
2053 printf(" Entry Count : %d\n", __le32_to_cpu(log
->entry_count
));
2055 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
2057 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
2059 super
->current_vol
= i
;
2060 getinfo_super_imsm(st
, &info
, NULL
);
2061 fname_from_uuid(st
, &info
, nbuf
, ':');
2062 print_imsm_dev(super
, dev
, nbuf
+ 5, super
->disks
->index
);
2064 for (i
= 0; i
< mpb
->num_disks
; i
++) {
2065 if (i
== super
->disks
->index
)
2067 print_imsm_disk(__get_imsm_disk(mpb
, i
), i
, reserved
,
2068 super
->sector_size
);
2071 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
2072 if (dl
->index
== -1)
2073 print_imsm_disk(&dl
->disk
, -1, reserved
,
2074 super
->sector_size
);
2076 examine_migr_rec_imsm(super
);
2079 static void brief_examine_super_imsm(struct supertype
*st
, int verbose
)
2081 /* We just write a generic IMSM ARRAY entry */
2084 struct intel_super
*super
= st
->sb
;
2086 if (!super
->anchor
->num_raid_devs
) {
2087 printf("ARRAY metadata=imsm\n");
2091 getinfo_super_imsm(st
, &info
, NULL
);
2092 fname_from_uuid(st
, &info
, nbuf
, ':');
2093 printf("ARRAY metadata=imsm UUID=%s\n", nbuf
+ 5);
2096 static void brief_examine_subarrays_imsm(struct supertype
*st
, int verbose
)
2098 /* We just write a generic IMSM ARRAY entry */
2102 struct intel_super
*super
= st
->sb
;
2105 if (!super
->anchor
->num_raid_devs
)
2108 getinfo_super_imsm(st
, &info
, NULL
);
2109 fname_from_uuid(st
, &info
, nbuf
, ':');
2110 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
2111 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
2113 super
->current_vol
= i
;
2114 getinfo_super_imsm(st
, &info
, NULL
);
2115 fname_from_uuid(st
, &info
, nbuf1
, ':');
2116 printf("ARRAY /dev/md/%.16s container=%s member=%d UUID=%s\n",
2117 dev
->volume
, nbuf
+ 5, i
, nbuf1
+ 5);
2121 static void export_examine_super_imsm(struct supertype
*st
)
2123 struct intel_super
*super
= st
->sb
;
2124 struct imsm_super
*mpb
= super
->anchor
;
2128 getinfo_super_imsm(st
, &info
, NULL
);
2129 fname_from_uuid(st
, &info
, nbuf
, ':');
2130 printf("MD_METADATA=imsm\n");
2131 printf("MD_LEVEL=container\n");
2132 printf("MD_UUID=%s\n", nbuf
+5);
2133 printf("MD_DEVICES=%u\n", mpb
->num_disks
);
2134 printf("MD_CREATION_TIME=%llu\n", __le64_to_cpu(mpb
->creation_time
));
2137 static void detail_super_imsm(struct supertype
*st
, char *homehost
,
2142 struct intel_super
*super
= st
->sb
;
2143 int temp_vol
= super
->current_vol
;
2146 super
->current_vol
= strtoul(subarray
, NULL
, 10);
2148 getinfo_super_imsm(st
, &info
, NULL
);
2149 fname_from_uuid(st
, &info
, nbuf
, ':');
2150 printf("\n UUID : %s\n", nbuf
+ 5);
2152 super
->current_vol
= temp_vol
;
2155 static void brief_detail_super_imsm(struct supertype
*st
, char *subarray
)
2159 struct intel_super
*super
= st
->sb
;
2160 int temp_vol
= super
->current_vol
;
2163 super
->current_vol
= strtoul(subarray
, NULL
, 10);
2165 getinfo_super_imsm(st
, &info
, NULL
);
2166 fname_from_uuid(st
, &info
, nbuf
, ':');
2167 printf(" UUID=%s", nbuf
+ 5);
2169 super
->current_vol
= temp_vol
;
2172 static int imsm_read_serial(int fd
, char *devname
, __u8
*serial
,
2173 size_t serial_buf_len
);
2174 static void fd2devname(int fd
, char *name
);
2176 static int ahci_enumerate_ports(const char *hba_path
, int port_count
, int host_base
, int verbose
)
2178 /* dump an unsorted list of devices attached to AHCI Intel storage
2179 * controller, as well as non-connected ports
2181 int hba_len
= strlen(hba_path
) + 1;
2186 unsigned long port_mask
= (1 << port_count
) - 1;
2188 if (port_count
> (int)sizeof(port_mask
) * 8) {
2190 pr_err("port_count %d out of range\n", port_count
);
2194 /* scroll through /sys/dev/block looking for devices attached to
2197 dir
= opendir("/sys/dev/block");
2201 for (ent
= readdir(dir
); ent
; ent
= readdir(dir
)) {
2212 if (sscanf(ent
->d_name
, "%d:%d", &major
, &minor
) != 2)
2214 path
= devt_to_devpath(makedev(major
, minor
));
2217 if (!path_attached_to_hba(path
, hba_path
)) {
2223 /* retrieve the scsi device type */
2224 if (asprintf(&device
, "/sys/dev/block/%d:%d/device/xxxxxxx", major
, minor
) < 0) {
2226 pr_err("failed to allocate 'device'\n");
2230 sprintf(device
, "/sys/dev/block/%d:%d/device/type", major
, minor
);
2231 if (load_sys(device
, buf
, sizeof(buf
)) != 0) {
2233 pr_err("failed to read device type for %s\n",
2239 type
= strtoul(buf
, NULL
, 10);
2241 /* if it's not a disk print the vendor and model */
2242 if (!(type
== 0 || type
== 7 || type
== 14)) {
2245 sprintf(device
, "/sys/dev/block/%d:%d/device/vendor", major
, minor
);
2246 if (load_sys(device
, buf
, sizeof(buf
)) == 0) {
2247 strncpy(vendor
, buf
, sizeof(vendor
));
2248 vendor
[sizeof(vendor
) - 1] = '\0';
2249 c
= (char *) &vendor
[sizeof(vendor
) - 1];
2250 while (isspace(*c
) || *c
== '\0')
2254 sprintf(device
, "/sys/dev/block/%d:%d/device/model", major
, minor
);
2255 if (load_sys(device
, buf
, sizeof(buf
)) == 0) {
2256 strncpy(model
, buf
, sizeof(model
));
2257 model
[sizeof(model
) - 1] = '\0';
2258 c
= (char *) &model
[sizeof(model
) - 1];
2259 while (isspace(*c
) || *c
== '\0')
2263 if (vendor
[0] && model
[0])
2264 sprintf(buf
, "%.64s %.64s", vendor
, model
);
2266 switch (type
) { /* numbers from hald/linux/device.c */
2267 case 1: sprintf(buf
, "tape"); break;
2268 case 2: sprintf(buf
, "printer"); break;
2269 case 3: sprintf(buf
, "processor"); break;
2271 case 5: sprintf(buf
, "cdrom"); break;
2272 case 6: sprintf(buf
, "scanner"); break;
2273 case 8: sprintf(buf
, "media_changer"); break;
2274 case 9: sprintf(buf
, "comm"); break;
2275 case 12: sprintf(buf
, "raid"); break;
2276 default: sprintf(buf
, "unknown");
2282 /* chop device path to 'host%d' and calculate the port number */
2283 c
= strchr(&path
[hba_len
], '/');
2286 pr_err("%s - invalid path name\n", path
+ hba_len
);
2291 if ((sscanf(&path
[hba_len
], "ata%d", &port
) == 1) ||
2292 ((sscanf(&path
[hba_len
], "host%d", &port
) == 1)))
2296 *c
= '/'; /* repair the full string */
2297 pr_err("failed to determine port number for %s\n",
2304 /* mark this port as used */
2305 port_mask
&= ~(1 << port
);
2307 /* print out the device information */
2309 printf(" Port%d : - non-disk device (%s) -\n", port
, buf
);
2313 fd
= dev_open(ent
->d_name
, O_RDONLY
);
2315 printf(" Port%d : - disk info unavailable -\n", port
);
2317 fd2devname(fd
, buf
);
2318 printf(" Port%d : %s", port
, buf
);
2319 if (imsm_read_serial(fd
, NULL
, (__u8
*)buf
,
2321 printf(" (%s)\n", buf
);
2336 for (i
= 0; i
< port_count
; i
++)
2337 if (port_mask
& (1 << i
))
2338 printf(" Port%d : - no device attached -\n", i
);
2344 static int print_nvme_info(struct sys_dev
*hba
)
2352 dir
= opendir("/sys/block/");
2356 for (ent
= readdir(dir
); ent
; ent
= readdir(dir
)) {
2357 if (strstr(ent
->d_name
, "nvme")) {
2358 sprintf(buf
, "/sys/block/%s", ent
->d_name
);
2359 rp
= realpath(buf
, NULL
);
2362 if (path_attached_to_hba(rp
, hba
->path
)) {
2363 fd
= open_dev(ent
->d_name
);
2369 fd2devname(fd
, buf
);
2370 if (hba
->type
== SYS_DEV_VMD
)
2371 printf(" NVMe under VMD : %s", buf
);
2372 else if (hba
->type
== SYS_DEV_NVME
)
2373 printf(" NVMe Device : %s", buf
);
2374 if (!imsm_read_serial(fd
, NULL
, (__u8
*)buf
,
2376 printf(" (%s)\n", buf
);
2389 static void print_found_intel_controllers(struct sys_dev
*elem
)
2391 for (; elem
; elem
= elem
->next
) {
2392 pr_err("found Intel(R) ");
2393 if (elem
->type
== SYS_DEV_SATA
)
2394 fprintf(stderr
, "SATA ");
2395 else if (elem
->type
== SYS_DEV_SAS
)
2396 fprintf(stderr
, "SAS ");
2397 else if (elem
->type
== SYS_DEV_NVME
)
2398 fprintf(stderr
, "NVMe ");
2400 if (elem
->type
== SYS_DEV_VMD
)
2401 fprintf(stderr
, "VMD domain");
2403 fprintf(stderr
, "RAID controller");
2406 fprintf(stderr
, " at %s", elem
->pci_id
);
2407 fprintf(stderr
, ".\n");
2412 static int ahci_get_port_count(const char *hba_path
, int *port_count
)
2419 if ((dir
= opendir(hba_path
)) == NULL
)
2422 for (ent
= readdir(dir
); ent
; ent
= readdir(dir
)) {
2425 if ((sscanf(ent
->d_name
, "ata%d", &host
) != 1) &&
2426 ((sscanf(ent
->d_name
, "host%d", &host
) != 1)))
2428 if (*port_count
== 0)
2430 else if (host
< host_base
)
2433 if (host
+ 1 > *port_count
+ host_base
)
2434 *port_count
= host
+ 1 - host_base
;
2440 static void print_imsm_capability(const struct imsm_orom
*orom
)
2442 printf(" Platform : Intel(R) ");
2443 if (orom
->capabilities
== 0 && orom
->driver_features
== 0)
2444 printf("Matrix Storage Manager\n");
2445 else if (imsm_orom_is_enterprise(orom
) && orom
->major_ver
>= 6)
2446 printf("Virtual RAID on CPU\n");
2448 printf("Rapid Storage Technology%s\n",
2449 imsm_orom_is_enterprise(orom
) ? " enterprise" : "");
2450 if (orom
->major_ver
|| orom
->minor_ver
|| orom
->hotfix_ver
|| orom
->build
)
2451 printf(" Version : %d.%d.%d.%d\n", orom
->major_ver
,
2452 orom
->minor_ver
, orom
->hotfix_ver
, orom
->build
);
2453 printf(" RAID Levels :%s%s%s%s%s\n",
2454 imsm_orom_has_raid0(orom
) ? " raid0" : "",
2455 imsm_orom_has_raid1(orom
) ? " raid1" : "",
2456 imsm_orom_has_raid1e(orom
) ? " raid1e" : "",
2457 imsm_orom_has_raid10(orom
) ? " raid10" : "",
2458 imsm_orom_has_raid5(orom
) ? " raid5" : "");
2459 printf(" Chunk Sizes :%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
2460 imsm_orom_has_chunk(orom
, 2) ? " 2k" : "",
2461 imsm_orom_has_chunk(orom
, 4) ? " 4k" : "",
2462 imsm_orom_has_chunk(orom
, 8) ? " 8k" : "",
2463 imsm_orom_has_chunk(orom
, 16) ? " 16k" : "",
2464 imsm_orom_has_chunk(orom
, 32) ? " 32k" : "",
2465 imsm_orom_has_chunk(orom
, 64) ? " 64k" : "",
2466 imsm_orom_has_chunk(orom
, 128) ? " 128k" : "",
2467 imsm_orom_has_chunk(orom
, 256) ? " 256k" : "",
2468 imsm_orom_has_chunk(orom
, 512) ? " 512k" : "",
2469 imsm_orom_has_chunk(orom
, 1024*1) ? " 1M" : "",
2470 imsm_orom_has_chunk(orom
, 1024*2) ? " 2M" : "",
2471 imsm_orom_has_chunk(orom
, 1024*4) ? " 4M" : "",
2472 imsm_orom_has_chunk(orom
, 1024*8) ? " 8M" : "",
2473 imsm_orom_has_chunk(orom
, 1024*16) ? " 16M" : "",
2474 imsm_orom_has_chunk(orom
, 1024*32) ? " 32M" : "",
2475 imsm_orom_has_chunk(orom
, 1024*64) ? " 64M" : "");
2476 printf(" 2TB volumes :%s supported\n",
2477 (orom
->attr
& IMSM_OROM_ATTR_2TB
)?"":" not");
2478 printf(" 2TB disks :%s supported\n",
2479 (orom
->attr
& IMSM_OROM_ATTR_2TB_DISK
)?"":" not");
2480 printf(" Max Disks : %d\n", orom
->tds
);
2481 printf(" Max Volumes : %d per array, %d per %s\n",
2482 orom
->vpa
, orom
->vphba
,
2483 imsm_orom_is_nvme(orom
) ? "platform" : "controller");
2487 static void print_imsm_capability_export(const struct imsm_orom
*orom
)
2489 printf("MD_FIRMWARE_TYPE=imsm\n");
2490 if (orom
->major_ver
|| orom
->minor_ver
|| orom
->hotfix_ver
|| orom
->build
)
2491 printf("IMSM_VERSION=%d.%d.%d.%d\n", orom
->major_ver
, orom
->minor_ver
,
2492 orom
->hotfix_ver
, orom
->build
);
2493 printf("IMSM_SUPPORTED_RAID_LEVELS=%s%s%s%s%s\n",
2494 imsm_orom_has_raid0(orom
) ? "raid0 " : "",
2495 imsm_orom_has_raid1(orom
) ? "raid1 " : "",
2496 imsm_orom_has_raid1e(orom
) ? "raid1e " : "",
2497 imsm_orom_has_raid5(orom
) ? "raid10 " : "",
2498 imsm_orom_has_raid10(orom
) ? "raid5 " : "");
2499 printf("IMSM_SUPPORTED_CHUNK_SIZES=%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
2500 imsm_orom_has_chunk(orom
, 2) ? "2k " : "",
2501 imsm_orom_has_chunk(orom
, 4) ? "4k " : "",
2502 imsm_orom_has_chunk(orom
, 8) ? "8k " : "",
2503 imsm_orom_has_chunk(orom
, 16) ? "16k " : "",
2504 imsm_orom_has_chunk(orom
, 32) ? "32k " : "",
2505 imsm_orom_has_chunk(orom
, 64) ? "64k " : "",
2506 imsm_orom_has_chunk(orom
, 128) ? "128k " : "",
2507 imsm_orom_has_chunk(orom
, 256) ? "256k " : "",
2508 imsm_orom_has_chunk(orom
, 512) ? "512k " : "",
2509 imsm_orom_has_chunk(orom
, 1024*1) ? "1M " : "",
2510 imsm_orom_has_chunk(orom
, 1024*2) ? "2M " : "",
2511 imsm_orom_has_chunk(orom
, 1024*4) ? "4M " : "",
2512 imsm_orom_has_chunk(orom
, 1024*8) ? "8M " : "",
2513 imsm_orom_has_chunk(orom
, 1024*16) ? "16M " : "",
2514 imsm_orom_has_chunk(orom
, 1024*32) ? "32M " : "",
2515 imsm_orom_has_chunk(orom
, 1024*64) ? "64M " : "");
2516 printf("IMSM_2TB_VOLUMES=%s\n",(orom
->attr
& IMSM_OROM_ATTR_2TB
) ? "yes" : "no");
2517 printf("IMSM_2TB_DISKS=%s\n",(orom
->attr
& IMSM_OROM_ATTR_2TB_DISK
) ? "yes" : "no");
2518 printf("IMSM_MAX_DISKS=%d\n",orom
->tds
);
2519 printf("IMSM_MAX_VOLUMES_PER_ARRAY=%d\n",orom
->vpa
);
2520 printf("IMSM_MAX_VOLUMES_PER_CONTROLLER=%d\n",orom
->vphba
);
2523 static int detail_platform_imsm(int verbose
, int enumerate_only
, char *controller_path
)
2525 /* There are two components to imsm platform support, the ahci SATA
2526 * controller and the option-rom. To find the SATA controller we
2527 * simply look in /sys/bus/pci/drivers/ahci to see if an ahci
2528 * controller with the Intel vendor id is present. This approach
2529 * allows mdadm to leverage the kernel's ahci detection logic, with the
2530 * caveat that if ahci.ko is not loaded mdadm will not be able to
2531 * detect platform raid capabilities. The option-rom resides in a
2532 * platform "Adapter ROM". We scan for its signature to retrieve the
2533 * platform capabilities. If raid support is disabled in the BIOS the
2534 * option-rom capability structure will not be available.
2536 struct sys_dev
*list
, *hba
;
2541 if (enumerate_only
) {
2542 if (check_env("IMSM_NO_PLATFORM"))
2544 list
= find_intel_devices();
2547 for (hba
= list
; hba
; hba
= hba
->next
) {
2548 if (find_imsm_capability(hba
)) {
2558 list
= find_intel_devices();
2561 pr_err("no active Intel(R) RAID controller found.\n");
2563 } else if (verbose
> 0)
2564 print_found_intel_controllers(list
);
2566 for (hba
= list
; hba
; hba
= hba
->next
) {
2567 if (controller_path
&& (compare_paths(hba
->path
, controller_path
) != 0))
2569 if (!find_imsm_capability(hba
)) {
2571 pr_err("imsm capabilities not found for controller: %s (type %s)\n",
2572 hba
->type
== SYS_DEV_VMD
? vmd_domain_to_controller(hba
, buf
) : hba
->path
,
2573 get_sys_dev_type(hba
->type
));
2579 if (controller_path
&& result
== 1) {
2580 pr_err("no active Intel(R) RAID controller found under %s\n",
2585 const struct orom_entry
*entry
;
2587 for (entry
= orom_entries
; entry
; entry
= entry
->next
) {
2588 if (entry
->type
== SYS_DEV_VMD
) {
2589 print_imsm_capability(&entry
->orom
);
2590 printf(" 3rd party NVMe :%s supported\n",
2591 imsm_orom_has_tpv_support(&entry
->orom
)?"":" not");
2592 for (hba
= list
; hba
; hba
= hba
->next
) {
2593 if (hba
->type
== SYS_DEV_VMD
) {
2595 printf(" I/O Controller : %s (%s)\n",
2596 vmd_domain_to_controller(hba
, buf
), get_sys_dev_type(hba
->type
));
2597 if (print_nvme_info(hba
)) {
2599 pr_err("failed to get devices attached to VMD domain.\n");
2608 print_imsm_capability(&entry
->orom
);
2609 if (entry
->type
== SYS_DEV_NVME
) {
2610 for (hba
= list
; hba
; hba
= hba
->next
) {
2611 if (hba
->type
== SYS_DEV_NVME
)
2612 print_nvme_info(hba
);
2618 struct devid_list
*devid
;
2619 for (devid
= entry
->devid_list
; devid
; devid
= devid
->next
) {
2620 hba
= device_by_id(devid
->devid
);
2624 printf(" I/O Controller : %s (%s)\n",
2625 hba
->path
, get_sys_dev_type(hba
->type
));
2626 if (hba
->type
== SYS_DEV_SATA
) {
2627 host_base
= ahci_get_port_count(hba
->path
, &port_count
);
2628 if (ahci_enumerate_ports(hba
->path
, port_count
, host_base
, verbose
)) {
2630 pr_err("failed to enumerate ports on SATA controller at %s.\n", hba
->pci_id
);
2641 static int export_detail_platform_imsm(int verbose
, char *controller_path
)
2643 struct sys_dev
*list
, *hba
;
2646 list
= find_intel_devices();
2649 pr_err("IMSM_DETAIL_PLATFORM_ERROR=NO_INTEL_DEVICES\n");
2654 for (hba
= list
; hba
; hba
= hba
->next
) {
2655 if (controller_path
&& (compare_paths(hba
->path
,controller_path
) != 0))
2657 if (!find_imsm_capability(hba
) && verbose
> 0) {
2659 pr_err("IMSM_DETAIL_PLATFORM_ERROR=NO_IMSM_CAPABLE_DEVICE_UNDER_%s\n",
2660 hba
->type
== SYS_DEV_VMD
? vmd_domain_to_controller(hba
, buf
) : hba
->path
);
2666 const struct orom_entry
*entry
;
2668 for (entry
= orom_entries
; entry
; entry
= entry
->next
) {
2669 if (entry
->type
== SYS_DEV_VMD
) {
2670 for (hba
= list
; hba
; hba
= hba
->next
)
2671 print_imsm_capability_export(&entry
->orom
);
2674 print_imsm_capability_export(&entry
->orom
);
2680 static int match_home_imsm(struct supertype
*st
, char *homehost
)
2682 /* the imsm metadata format does not specify any host
2683 * identification information. We return -1 since we can never
2684 * confirm nor deny whether a given array is "meant" for this
2685 * host. We rely on compare_super and the 'family_num' fields to
2686 * exclude member disks that do not belong, and we rely on
2687 * mdadm.conf to specify the arrays that should be assembled.
2688 * Auto-assembly may still pick up "foreign" arrays.
2694 static void uuid_from_super_imsm(struct supertype
*st
, int uuid
[4])
2696 /* The uuid returned here is used for:
2697 * uuid to put into bitmap file (Create, Grow)
2698 * uuid for backup header when saving critical section (Grow)
2699 * comparing uuids when re-adding a device into an array
2700 * In these cases the uuid required is that of the data-array,
2701 * not the device-set.
2702 * uuid to recognise same set when adding a missing device back
2703 * to an array. This is a uuid for the device-set.
2705 * For each of these we can make do with a truncated
2706 * or hashed uuid rather than the original, as long as
2708 * In each case the uuid required is that of the data-array,
2709 * not the device-set.
2711 /* imsm does not track uuid's so we synthesis one using sha1 on
2712 * - The signature (Which is constant for all imsm array, but no matter)
2713 * - the orig_family_num of the container
2714 * - the index number of the volume
2715 * - the 'serial' number of the volume.
2716 * Hopefully these are all constant.
2718 struct intel_super
*super
= st
->sb
;
2721 struct sha1_ctx ctx
;
2722 struct imsm_dev
*dev
= NULL
;
2725 /* some mdadm versions failed to set ->orig_family_num, in which
2726 * case fall back to ->family_num. orig_family_num will be
2727 * fixed up with the first metadata update.
2729 family_num
= super
->anchor
->orig_family_num
;
2730 if (family_num
== 0)
2731 family_num
= super
->anchor
->family_num
;
2732 sha1_init_ctx(&ctx
);
2733 sha1_process_bytes(super
->anchor
->sig
, MPB_SIG_LEN
, &ctx
);
2734 sha1_process_bytes(&family_num
, sizeof(__u32
), &ctx
);
2735 if (super
->current_vol
>= 0)
2736 dev
= get_imsm_dev(super
, super
->current_vol
);
2738 __u32 vol
= super
->current_vol
;
2739 sha1_process_bytes(&vol
, sizeof(vol
), &ctx
);
2740 sha1_process_bytes(dev
->volume
, MAX_RAID_SERIAL_LEN
, &ctx
);
2742 sha1_finish_ctx(&ctx
, buf
);
2743 memcpy(uuid
, buf
, 4*4);
2748 get_imsm_numerical_version(struct imsm_super
*mpb
, int *m
, int *p
)
2750 __u8
*v
= get_imsm_version(mpb
);
2751 __u8
*end
= mpb
->sig
+ MAX_SIGNATURE_LENGTH
;
2752 char major
[] = { 0, 0, 0 };
2753 char minor
[] = { 0 ,0, 0 };
2754 char patch
[] = { 0, 0, 0 };
2755 char *ver_parse
[] = { major
, minor
, patch
};
2759 while (*v
!= '\0' && v
< end
) {
2760 if (*v
!= '.' && j
< 2)
2761 ver_parse
[i
][j
++] = *v
;
2769 *m
= strtol(minor
, NULL
, 0);
2770 *p
= strtol(patch
, NULL
, 0);
2774 static __u32
migr_strip_blocks_resync(struct imsm_dev
*dev
)
2776 /* migr_strip_size when repairing or initializing parity */
2777 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
2778 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
2780 switch (get_imsm_raid_level(map
)) {
2785 return 128*1024 >> 9;
2789 static __u32
migr_strip_blocks_rebuild(struct imsm_dev
*dev
)
2791 /* migr_strip_size when rebuilding a degraded disk, no idea why
2792 * this is different than migr_strip_size_resync(), but it's good
2795 struct imsm_map
*map
= get_imsm_map(dev
, MAP_1
);
2796 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
2798 switch (get_imsm_raid_level(map
)) {
2801 if (map
->num_members
% map
->num_domains
== 0)
2802 return 128*1024 >> 9;
2806 return max((__u32
) 64*1024 >> 9, chunk
);
2808 return 128*1024 >> 9;
2812 static __u32
num_stripes_per_unit_resync(struct imsm_dev
*dev
)
2814 struct imsm_map
*lo
= get_imsm_map(dev
, MAP_0
);
2815 struct imsm_map
*hi
= get_imsm_map(dev
, MAP_1
);
2816 __u32 lo_chunk
= __le32_to_cpu(lo
->blocks_per_strip
);
2817 __u32 hi_chunk
= __le32_to_cpu(hi
->blocks_per_strip
);
2819 return max((__u32
) 1, hi_chunk
/ lo_chunk
);
2822 static __u32
num_stripes_per_unit_rebuild(struct imsm_dev
*dev
)
2824 struct imsm_map
*lo
= get_imsm_map(dev
, MAP_0
);
2825 int level
= get_imsm_raid_level(lo
);
2827 if (level
== 1 || level
== 10) {
2828 struct imsm_map
*hi
= get_imsm_map(dev
, MAP_1
);
2830 return hi
->num_domains
;
2832 return num_stripes_per_unit_resync(dev
);
2835 static __u8
imsm_num_data_members(struct imsm_map
*map
)
2837 /* named 'imsm_' because raid0, raid1 and raid10
2838 * counter-intuitively have the same number of data disks
2840 switch (get_imsm_raid_level(map
)) {
2842 return map
->num_members
;
2846 return map
->num_members
/2;
2848 return map
->num_members
- 1;
2850 dprintf("unsupported raid level\n");
2855 static unsigned long long calc_component_size(struct imsm_map
*map
,
2856 struct imsm_dev
*dev
)
2858 unsigned long long component_size
;
2859 unsigned long long dev_size
= imsm_dev_size(dev
);
2860 long long calc_dev_size
= 0;
2861 unsigned int member_disks
= imsm_num_data_members(map
);
2863 if (member_disks
== 0)
2866 component_size
= per_dev_array_size(map
);
2867 calc_dev_size
= component_size
* member_disks
;
2869 /* Component size is rounded to 1MB so difference between size from
2870 * metadata and size calculated from num_data_stripes equals up to
2871 * 2048 blocks per each device. If the difference is higher it means
2872 * that array size was expanded and num_data_stripes was not updated.
2874 if (llabs(calc_dev_size
- (long long)dev_size
) >
2875 (1 << SECT_PER_MB_SHIFT
) * member_disks
) {
2876 component_size
= dev_size
/ member_disks
;
2877 dprintf("Invalid num_data_stripes in metadata; expected=%llu, found=%llu\n",
2878 component_size
/ map
->blocks_per_strip
,
2879 num_data_stripes(map
));
2882 return component_size
;
2885 static __u32
parity_segment_depth(struct imsm_dev
*dev
)
2887 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
2888 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
2890 switch(get_imsm_raid_level(map
)) {
2893 return chunk
* map
->num_domains
;
2895 return chunk
* map
->num_members
;
2901 static __u32
map_migr_block(struct imsm_dev
*dev
, __u32 block
)
2903 struct imsm_map
*map
= get_imsm_map(dev
, MAP_1
);
2904 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
2905 __u32 strip
= block
/ chunk
;
2907 switch (get_imsm_raid_level(map
)) {
2910 __u32 vol_strip
= (strip
* map
->num_domains
) + 1;
2911 __u32 vol_stripe
= vol_strip
/ map
->num_members
;
2913 return vol_stripe
* chunk
+ block
% chunk
;
2915 __u32 stripe
= strip
/ (map
->num_members
- 1);
2917 return stripe
* chunk
+ block
% chunk
;
2924 static __u64
blocks_per_migr_unit(struct intel_super
*super
,
2925 struct imsm_dev
*dev
)
2927 /* calculate the conversion factor between per member 'blocks'
2928 * (md/{resync,rebuild}_start) and imsm migration units, return
2929 * 0 for the 'not migrating' and 'unsupported migration' cases
2931 if (!dev
->vol
.migr_state
)
2934 switch (migr_type(dev
)) {
2935 case MIGR_GEN_MIGR
: {
2936 struct migr_record
*migr_rec
= super
->migr_rec
;
2937 return __le32_to_cpu(migr_rec
->blocks_per_unit
);
2942 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
2943 __u32 stripes_per_unit
;
2944 __u32 blocks_per_unit
;
2953 /* yes, this is really the translation of migr_units to
2954 * per-member blocks in the 'resync' case
2956 stripes_per_unit
= num_stripes_per_unit_resync(dev
);
2957 migr_chunk
= migr_strip_blocks_resync(dev
);
2958 disks
= imsm_num_data_members(map
);
2959 blocks_per_unit
= stripes_per_unit
* migr_chunk
* disks
;
2960 stripe
= __le16_to_cpu(map
->blocks_per_strip
) * disks
;
2961 segment
= blocks_per_unit
/ stripe
;
2962 block_rel
= blocks_per_unit
- segment
* stripe
;
2963 parity_depth
= parity_segment_depth(dev
);
2964 block_map
= map_migr_block(dev
, block_rel
);
2965 return block_map
+ parity_depth
* segment
;
2967 case MIGR_REBUILD
: {
2968 __u32 stripes_per_unit
;
2971 stripes_per_unit
= num_stripes_per_unit_rebuild(dev
);
2972 migr_chunk
= migr_strip_blocks_rebuild(dev
);
2973 return migr_chunk
* stripes_per_unit
;
2975 case MIGR_STATE_CHANGE
:
2981 static int imsm_level_to_layout(int level
)
2989 return ALGORITHM_LEFT_ASYMMETRIC
;
2996 /*******************************************************************************
2997 * Function: read_imsm_migr_rec
2998 * Description: Function reads imsm migration record from last sector of disk
3000 * fd : disk descriptor
3001 * super : metadata info
3005 ******************************************************************************/
3006 static int read_imsm_migr_rec(int fd
, struct intel_super
*super
)
3009 unsigned int sector_size
= super
->sector_size
;
3010 unsigned long long dsize
;
3012 get_dev_size(fd
, NULL
, &dsize
);
3013 if (lseek64(fd
, dsize
- (sector_size
*MIGR_REC_SECTOR_POSITION
),
3015 pr_err("Cannot seek to anchor block: %s\n",
3019 if ((unsigned int)read(fd
, super
->migr_rec_buf
,
3020 MIGR_REC_BUF_SECTORS
*sector_size
) !=
3021 MIGR_REC_BUF_SECTORS
*sector_size
) {
3022 pr_err("Cannot read migr record block: %s\n",
3027 if (sector_size
== 4096)
3028 convert_from_4k_imsm_migr_rec(super
);
3034 static struct imsm_dev
*imsm_get_device_during_migration(
3035 struct intel_super
*super
)
3038 struct intel_dev
*dv
;
3040 for (dv
= super
->devlist
; dv
; dv
= dv
->next
) {
3041 if (is_gen_migration(dv
->dev
))
3047 /*******************************************************************************
3048 * Function: load_imsm_migr_rec
3049 * Description: Function reads imsm migration record (it is stored at the last
3052 * super : imsm internal array info
3053 * info : general array info
3057 * -2 : no migration in progress
3058 ******************************************************************************/
3059 static int load_imsm_migr_rec(struct intel_super
*super
, struct mdinfo
*info
)
3066 struct imsm_dev
*dev
;
3067 struct imsm_map
*map
;
3070 /* find map under migration */
3071 dev
= imsm_get_device_during_migration(super
);
3072 /* nothing to load,no migration in progress?
3078 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
3079 /* read only from one of the first two slots */
3080 if ((sd
->disk
.raid_disk
< 0) ||
3081 (sd
->disk
.raid_disk
> 1))
3084 sprintf(nm
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
3085 fd
= dev_open(nm
, O_RDONLY
);
3091 map
= get_imsm_map(dev
, MAP_0
);
3092 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
3093 /* skip spare and failed disks
3097 /* read only from one of the first two slots */
3099 slot
= get_imsm_disk_slot(map
, dl
->index
);
3100 if (map
== NULL
|| slot
> 1 || slot
< 0)
3102 sprintf(nm
, "%d:%d", dl
->major
, dl
->minor
);
3103 fd
= dev_open(nm
, O_RDONLY
);
3110 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
;
3180 struct imsm_update_general_migration_checkpoint
*u
;
3181 struct imsm_dev
*dev
;
3182 struct imsm_map
*map
;
3184 /* find map under migration */
3185 dev
= imsm_get_device_during_migration(super
);
3186 /* if no migration, write buffer anyway to clear migr_record
3187 * on disk based on first available device
3190 dev
= get_imsm_dev(super
, super
->current_vol
< 0 ? 0 :
3191 super
->current_vol
);
3193 map
= get_imsm_map(dev
, MAP_0
);
3195 if (sector_size
== 4096)
3196 convert_to_4k_imsm_migr_rec(super
);
3197 for (sd
= super
->disks
; sd
; sd
= sd
->next
) {
3200 /* skip failed and spare devices */
3203 /* write to 2 first slots only */
3205 slot
= get_imsm_disk_slot(map
, sd
->index
);
3206 if (map
== NULL
|| slot
> 1 || slot
< 0)
3209 sprintf(nm
, "%d:%d", sd
->major
, sd
->minor
);
3210 fd
= dev_open(nm
, O_RDWR
);
3213 get_dev_size(fd
, NULL
, &dsize
);
3214 if (lseek64(fd
, dsize
- (MIGR_REC_SECTOR_POSITION
*sector_size
),
3216 pr_err("Cannot seek to anchor block: %s\n",
3220 if ((unsigned int)write(fd
, super
->migr_rec_buf
,
3221 MIGR_REC_BUF_SECTORS
*sector_size
) !=
3222 MIGR_REC_BUF_SECTORS
*sector_size
) {
3223 pr_err("Cannot write migr record block: %s\n",
3230 if (sector_size
== 4096)
3231 convert_from_4k_imsm_migr_rec(super
);
3232 /* update checkpoint information in metadata */
3233 len
= imsm_create_metadata_checkpoint_update(super
, &u
);
3235 dprintf("imsm: Cannot prepare update\n");
3238 /* update metadata locally */
3239 imsm_update_metadata_locally(st
, u
, len
);
3240 /* and possibly remotely */
3241 if (st
->update_tail
) {
3242 append_metadata_update(st
, u
, len
);
3243 /* during reshape we do all work inside metadata handler
3244 * manage_reshape(), so metadata update has to be triggered
3247 flush_metadata_updates(st
);
3248 st
->update_tail
= &st
->updates
;
3259 /* spare/missing disks activations are not allowe when
3260 * array/container performs reshape operation, because
3261 * all arrays in container works on the same disks set
3263 int imsm_reshape_blocks_arrays_changes(struct intel_super
*super
)
3266 struct intel_dev
*i_dev
;
3267 struct imsm_dev
*dev
;
3269 /* check whole container
3271 for (i_dev
= super
->devlist
; i_dev
; i_dev
= i_dev
->next
) {
3273 if (is_gen_migration(dev
)) {
3274 /* No repair during any migration in container
3282 static unsigned long long imsm_component_size_alignment_check(int level
,
3284 unsigned int sector_size
,
3285 unsigned long long component_size
)
3287 unsigned int component_size_alignment
;
3289 /* check component size alignment
3291 component_size_alignment
= component_size
% (chunk_size
/sector_size
);
3293 dprintf("(Level: %i, chunk_size = %i, component_size = %llu), component_size_alignment = %u\n",
3294 level
, chunk_size
, component_size
,
3295 component_size_alignment
);
3297 if (component_size_alignment
&& (level
!= 1) && (level
!= UnSet
)) {
3298 dprintf("imsm: reported component size aligned from %llu ",
3300 component_size
-= component_size_alignment
;
3301 dprintf_cont("to %llu (%i).\n",
3302 component_size
, component_size_alignment
);
3305 return component_size
;
3308 static unsigned long long get_ppl_sector(struct intel_super
*super
, int dev_idx
)
3310 struct imsm_dev
*dev
= get_imsm_dev(super
, dev_idx
);
3311 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
3313 return pba_of_lba0(map
) +
3314 (num_data_stripes(map
) * map
->blocks_per_strip
);
3317 static void getinfo_super_imsm_volume(struct supertype
*st
, struct mdinfo
*info
, char *dmap
)
3319 struct intel_super
*super
= st
->sb
;
3320 struct migr_record
*migr_rec
= super
->migr_rec
;
3321 struct imsm_dev
*dev
= get_imsm_dev(super
, super
->current_vol
);
3322 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
3323 struct imsm_map
*prev_map
= get_imsm_map(dev
, MAP_1
);
3324 struct imsm_map
*map_to_analyse
= map
;
3326 int map_disks
= info
->array
.raid_disks
;
3328 memset(info
, 0, sizeof(*info
));
3330 map_to_analyse
= prev_map
;
3332 dl
= super
->current_disk
;
3334 info
->container_member
= super
->current_vol
;
3335 info
->array
.raid_disks
= map
->num_members
;
3336 info
->array
.level
= get_imsm_raid_level(map_to_analyse
);
3337 info
->array
.layout
= imsm_level_to_layout(info
->array
.level
);
3338 info
->array
.md_minor
= -1;
3339 info
->array
.ctime
= 0;
3340 info
->array
.utime
= 0;
3341 info
->array
.chunk_size
=
3342 __le16_to_cpu(map_to_analyse
->blocks_per_strip
) << 9;
3343 info
->array
.state
= !(dev
->vol
.dirty
& RAIDVOL_DIRTY
);
3344 info
->custom_array_size
= imsm_dev_size(dev
);
3345 info
->recovery_blocked
= imsm_reshape_blocks_arrays_changes(st
->sb
);
3347 if (is_gen_migration(dev
)) {
3348 info
->reshape_active
= 1;
3349 info
->new_level
= get_imsm_raid_level(map
);
3350 info
->new_layout
= imsm_level_to_layout(info
->new_level
);
3351 info
->new_chunk
= __le16_to_cpu(map
->blocks_per_strip
) << 9;
3352 info
->delta_disks
= map
->num_members
- prev_map
->num_members
;
3353 if (info
->delta_disks
) {
3354 /* this needs to be applied to every array
3357 info
->reshape_active
= CONTAINER_RESHAPE
;
3359 /* We shape information that we give to md might have to be
3360 * modify to cope with md's requirement for reshaping arrays.
3361 * For example, when reshaping a RAID0, md requires it to be
3362 * presented as a degraded RAID4.
3363 * Also if a RAID0 is migrating to a RAID5 we need to specify
3364 * the array as already being RAID5, but the 'before' layout
3365 * is a RAID4-like layout.
3367 switch (info
->array
.level
) {
3369 switch(info
->new_level
) {
3371 /* conversion is happening as RAID4 */
3372 info
->array
.level
= 4;
3373 info
->array
.raid_disks
+= 1;
3376 /* conversion is happening as RAID5 */
3377 info
->array
.level
= 5;
3378 info
->array
.layout
= ALGORITHM_PARITY_N
;
3379 info
->delta_disks
-= 1;
3382 /* FIXME error message */
3383 info
->array
.level
= UnSet
;
3389 info
->new_level
= UnSet
;
3390 info
->new_layout
= UnSet
;
3391 info
->new_chunk
= info
->array
.chunk_size
;
3392 info
->delta_disks
= 0;
3396 info
->disk
.major
= dl
->major
;
3397 info
->disk
.minor
= dl
->minor
;
3398 info
->disk
.number
= dl
->index
;
3399 info
->disk
.raid_disk
= get_imsm_disk_slot(map_to_analyse
,
3403 info
->data_offset
= pba_of_lba0(map_to_analyse
);
3404 info
->component_size
= calc_component_size(map
, dev
);
3405 info
->component_size
= imsm_component_size_alignment_check(
3407 info
->array
.chunk_size
,
3409 info
->component_size
);
3410 info
->bb
.supported
= 1;
3412 memset(info
->uuid
, 0, sizeof(info
->uuid
));
3413 info
->recovery_start
= MaxSector
;
3415 if (info
->array
.level
== 5 &&
3416 (dev
->rwh_policy
== RWH_DISTRIBUTED
||
3417 dev
->rwh_policy
== RWH_MULTIPLE_DISTRIBUTED
)) {
3418 info
->consistency_policy
= CONSISTENCY_POLICY_PPL
;
3419 info
->ppl_sector
= get_ppl_sector(super
, super
->current_vol
);
3420 if (dev
->rwh_policy
== RWH_MULTIPLE_DISTRIBUTED
)
3421 info
->ppl_size
= MULTIPLE_PPL_AREA_SIZE_IMSM
>> 9;
3423 info
->ppl_size
= (PPL_HEADER_SIZE
+ PPL_ENTRY_SPACE
)
3425 } else if (info
->array
.level
<= 0) {
3426 info
->consistency_policy
= CONSISTENCY_POLICY_NONE
;
3428 info
->consistency_policy
= CONSISTENCY_POLICY_RESYNC
;
3431 info
->reshape_progress
= 0;
3432 info
->resync_start
= MaxSector
;
3433 if ((map_to_analyse
->map_state
== IMSM_T_STATE_UNINITIALIZED
||
3434 !(info
->array
.state
& 1)) &&
3435 imsm_reshape_blocks_arrays_changes(super
) == 0) {
3436 info
->resync_start
= 0;
3438 if (dev
->vol
.migr_state
) {
3439 switch (migr_type(dev
)) {
3442 __u64 blocks_per_unit
= blocks_per_migr_unit(super
,
3444 __u64 units
= __le32_to_cpu(dev
->vol
.curr_migr_unit
);
3446 info
->resync_start
= blocks_per_unit
* units
;
3449 case MIGR_GEN_MIGR
: {
3450 __u64 blocks_per_unit
= blocks_per_migr_unit(super
,
3452 __u64 units
= current_migr_unit(migr_rec
);
3453 unsigned long long array_blocks
;
3456 if (__le32_to_cpu(migr_rec
->ascending_migr
) &&
3458 (get_num_migr_units(migr_rec
)-1)) &&
3459 (super
->migr_rec
->rec_status
==
3460 __cpu_to_le32(UNIT_SRC_IN_CP_AREA
)))
3463 info
->reshape_progress
= blocks_per_unit
* units
;
3465 dprintf("IMSM: General Migration checkpoint : %llu (%llu) -> read reshape progress : %llu\n",
3466 (unsigned long long)units
,
3467 (unsigned long long)blocks_per_unit
,
3468 info
->reshape_progress
);
3470 used_disks
= imsm_num_data_members(prev_map
);
3471 if (used_disks
> 0) {
3472 array_blocks
= per_dev_array_size(map
) *
3474 info
->custom_array_size
=
3475 round_size_to_mb(array_blocks
,
3481 /* we could emulate the checkpointing of
3482 * 'sync_action=check' migrations, but for now
3483 * we just immediately complete them
3486 /* this is handled by container_content_imsm() */
3487 case MIGR_STATE_CHANGE
:
3488 /* FIXME handle other migrations */
3490 /* we are not dirty, so... */
3491 info
->resync_start
= MaxSector
;
3495 strncpy(info
->name
, (char *) dev
->volume
, MAX_RAID_SERIAL_LEN
);
3496 info
->name
[MAX_RAID_SERIAL_LEN
] = 0;
3498 info
->array
.major_version
= -1;
3499 info
->array
.minor_version
= -2;
3500 sprintf(info
->text_version
, "/%s/%d", st
->container_devnm
, info
->container_member
);
3501 info
->safe_mode_delay
= 4000; /* 4 secs like the Matrix driver */
3502 uuid_from_super_imsm(st
, info
->uuid
);
3506 for (i
=0; i
<map_disks
; i
++) {
3508 if (i
< info
->array
.raid_disks
) {
3509 struct imsm_disk
*dsk
;
3510 j
= get_imsm_disk_idx(dev
, i
, MAP_X
);
3511 dsk
= get_imsm_disk(super
, j
);
3512 if (dsk
&& (dsk
->status
& CONFIGURED_DISK
))
3519 static __u8
imsm_check_degraded(struct intel_super
*super
, struct imsm_dev
*dev
,
3520 int failed
, int look_in_map
);
3522 static int imsm_count_failed(struct intel_super
*super
, struct imsm_dev
*dev
,
3525 static void manage_second_map(struct intel_super
*super
, struct imsm_dev
*dev
)
3527 if (is_gen_migration(dev
)) {
3530 struct imsm_map
*map2
= get_imsm_map(dev
, MAP_1
);
3532 failed
= imsm_count_failed(super
, dev
, MAP_1
);
3533 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_1
);
3534 if (map2
->map_state
!= map_state
) {
3535 map2
->map_state
= map_state
;
3536 super
->updates_pending
++;
3541 static struct imsm_disk
*get_imsm_missing(struct intel_super
*super
, __u8 index
)
3545 for (d
= super
->missing
; d
; d
= d
->next
)
3546 if (d
->index
== index
)
3551 static void getinfo_super_imsm(struct supertype
*st
, struct mdinfo
*info
, char *map
)
3553 struct intel_super
*super
= st
->sb
;
3554 struct imsm_disk
*disk
;
3555 int map_disks
= info
->array
.raid_disks
;
3556 int max_enough
= -1;
3558 struct imsm_super
*mpb
;
3560 if (super
->current_vol
>= 0) {
3561 getinfo_super_imsm_volume(st
, info
, map
);
3564 memset(info
, 0, sizeof(*info
));
3566 /* Set raid_disks to zero so that Assemble will always pull in valid
3569 info
->array
.raid_disks
= 0;
3570 info
->array
.level
= LEVEL_CONTAINER
;
3571 info
->array
.layout
= 0;
3572 info
->array
.md_minor
= -1;
3573 info
->array
.ctime
= 0; /* N/A for imsm */
3574 info
->array
.utime
= 0;
3575 info
->array
.chunk_size
= 0;
3577 info
->disk
.major
= 0;
3578 info
->disk
.minor
= 0;
3579 info
->disk
.raid_disk
= -1;
3580 info
->reshape_active
= 0;
3581 info
->array
.major_version
= -1;
3582 info
->array
.minor_version
= -2;
3583 strcpy(info
->text_version
, "imsm");
3584 info
->safe_mode_delay
= 0;
3585 info
->disk
.number
= -1;
3586 info
->disk
.state
= 0;
3588 info
->recovery_start
= MaxSector
;
3589 info
->recovery_blocked
= imsm_reshape_blocks_arrays_changes(st
->sb
);
3590 info
->bb
.supported
= 1;
3592 /* do we have the all the insync disks that we expect? */
3593 mpb
= super
->anchor
;
3594 info
->events
= __le32_to_cpu(mpb
->generation_num
);
3596 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
3597 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
3598 int failed
, enough
, j
, missing
= 0;
3599 struct imsm_map
*map
;
3602 failed
= imsm_count_failed(super
, dev
, MAP_0
);
3603 state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
3604 map
= get_imsm_map(dev
, MAP_0
);
3606 /* any newly missing disks?
3607 * (catches single-degraded vs double-degraded)
3609 for (j
= 0; j
< map
->num_members
; j
++) {
3610 __u32 ord
= get_imsm_ord_tbl_ent(dev
, j
, MAP_0
);
3611 __u32 idx
= ord_to_idx(ord
);
3613 if (super
->disks
&& super
->disks
->index
== (int)idx
)
3614 info
->disk
.raid_disk
= j
;
3616 if (!(ord
& IMSM_ORD_REBUILD
) &&
3617 get_imsm_missing(super
, idx
)) {
3623 if (state
== IMSM_T_STATE_FAILED
)
3625 else if (state
== IMSM_T_STATE_DEGRADED
&&
3626 (state
!= map
->map_state
|| missing
))
3628 else /* we're normal, or already degraded */
3630 if (is_gen_migration(dev
) && missing
) {
3631 /* during general migration we need all disks
3632 * that process is running on.
3633 * No new missing disk is allowed.
3637 /* no more checks necessary
3641 /* in the missing/failed disk case check to see
3642 * if at least one array is runnable
3644 max_enough
= max(max_enough
, enough
);
3646 dprintf("enough: %d\n", max_enough
);
3647 info
->container_enough
= max_enough
;
3650 __u32 reserved
= imsm_reserved_sectors(super
, super
->disks
);
3652 disk
= &super
->disks
->disk
;
3653 info
->data_offset
= total_blocks(&super
->disks
->disk
) - reserved
;
3654 info
->component_size
= reserved
;
3655 info
->disk
.state
= is_configured(disk
) ? (1 << MD_DISK_ACTIVE
) : 0;
3656 /* we don't change info->disk.raid_disk here because
3657 * this state will be finalized in mdmon after we have
3658 * found the 'most fresh' version of the metadata
3660 info
->disk
.state
|= is_failed(disk
) ? (1 << MD_DISK_FAULTY
) : 0;
3661 info
->disk
.state
|= (is_spare(disk
) || is_journal(disk
)) ?
3662 0 : (1 << MD_DISK_SYNC
);
3665 /* only call uuid_from_super_imsm when this disk is part of a populated container,
3666 * ->compare_super may have updated the 'num_raid_devs' field for spares
3668 if (info
->disk
.state
& (1 << MD_DISK_SYNC
) || super
->anchor
->num_raid_devs
)
3669 uuid_from_super_imsm(st
, info
->uuid
);
3671 memcpy(info
->uuid
, uuid_zero
, sizeof(uuid_zero
));
3673 /* I don't know how to compute 'map' on imsm, so use safe default */
3676 for (i
= 0; i
< map_disks
; i
++)
3682 /* allocates memory and fills disk in mdinfo structure
3683 * for each disk in array */
3684 struct mdinfo
*getinfo_super_disks_imsm(struct supertype
*st
)
3686 struct mdinfo
*mddev
;
3687 struct intel_super
*super
= st
->sb
;
3688 struct imsm_disk
*disk
;
3691 if (!super
|| !super
->disks
)
3694 mddev
= xcalloc(1, sizeof(*mddev
));
3698 tmp
= xcalloc(1, sizeof(*tmp
));
3700 tmp
->next
= mddev
->devs
;
3702 tmp
->disk
.number
= count
++;
3703 tmp
->disk
.major
= dl
->major
;
3704 tmp
->disk
.minor
= dl
->minor
;
3705 tmp
->disk
.state
= is_configured(disk
) ?
3706 (1 << MD_DISK_ACTIVE
) : 0;
3707 tmp
->disk
.state
|= is_failed(disk
) ? (1 << MD_DISK_FAULTY
) : 0;
3708 tmp
->disk
.state
|= is_spare(disk
) ? 0 : (1 << MD_DISK_SYNC
);
3709 tmp
->disk
.raid_disk
= -1;
3715 static int update_super_imsm(struct supertype
*st
, struct mdinfo
*info
,
3716 char *update
, char *devname
, int verbose
,
3717 int uuid_set
, char *homehost
)
3719 /* For 'assemble' and 'force' we need to return non-zero if any
3720 * change was made. For others, the return value is ignored.
3721 * Update options are:
3722 * force-one : This device looks a bit old but needs to be included,
3723 * update age info appropriately.
3724 * assemble: clear any 'faulty' flag to allow this device to
3726 * force-array: Array is degraded but being forced, mark it clean
3727 * if that will be needed to assemble it.
3729 * newdev: not used ????
3730 * grow: Array has gained a new device - this is currently for
3732 * resync: mark as dirty so a resync will happen.
3733 * name: update the name - preserving the homehost
3734 * uuid: Change the uuid of the array to match watch is given
3736 * Following are not relevant for this imsm:
3737 * sparc2.2 : update from old dodgey metadata
3738 * super-minor: change the preferred_minor number
3739 * summaries: update redundant counters.
3740 * homehost: update the recorded homehost
3741 * _reshape_progress: record new reshape_progress position.
3744 struct intel_super
*super
= st
->sb
;
3745 struct imsm_super
*mpb
;
3747 /* we can only update container info */
3748 if (!super
|| super
->current_vol
>= 0 || !super
->anchor
)
3751 mpb
= super
->anchor
;
3753 if (strcmp(update
, "uuid") == 0) {
3754 /* We take this to mean that the family_num should be updated.
3755 * However that is much smaller than the uuid so we cannot really
3756 * allow an explicit uuid to be given. And it is hard to reliably
3758 * So if !uuid_set we know the current uuid is random and just used
3759 * the first 'int' and copy it to the other 3 positions.
3760 * Otherwise we require the 4 'int's to be the same as would be the
3761 * case if we are using a random uuid. So an explicit uuid will be
3762 * accepted as long as all for ints are the same... which shouldn't hurt
3765 info
->uuid
[1] = info
->uuid
[2] = info
->uuid
[3] = info
->uuid
[0];
3768 if (info
->uuid
[0] != info
->uuid
[1] ||
3769 info
->uuid
[1] != info
->uuid
[2] ||
3770 info
->uuid
[2] != info
->uuid
[3])
3776 mpb
->orig_family_num
= info
->uuid
[0];
3777 } else if (strcmp(update
, "assemble") == 0)
3782 /* successful update? recompute checksum */
3784 mpb
->check_sum
= __le32_to_cpu(__gen_imsm_checksum(mpb
));
3789 static size_t disks_to_mpb_size(int disks
)
3793 size
= sizeof(struct imsm_super
);
3794 size
+= (disks
- 1) * sizeof(struct imsm_disk
);
3795 size
+= 2 * sizeof(struct imsm_dev
);
3796 /* up to 2 maps per raid device (-2 for imsm_maps in imsm_dev */
3797 size
+= (4 - 2) * sizeof(struct imsm_map
);
3798 /* 4 possible disk_ord_tbl's */
3799 size
+= 4 * (disks
- 1) * sizeof(__u32
);
3800 /* maximum bbm log */
3801 size
+= sizeof(struct bbm_log
);
3806 static __u64
avail_size_imsm(struct supertype
*st
, __u64 devsize
,
3807 unsigned long long data_offset
)
3809 if (devsize
< (MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
))
3812 return devsize
- (MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
);
3815 static void free_devlist(struct intel_super
*super
)
3817 struct intel_dev
*dv
;
3819 while (super
->devlist
) {
3820 dv
= super
->devlist
->next
;
3821 free(super
->devlist
->dev
);
3822 free(super
->devlist
);
3823 super
->devlist
= dv
;
3827 static void imsm_copy_dev(struct imsm_dev
*dest
, struct imsm_dev
*src
)
3829 memcpy(dest
, src
, sizeof_imsm_dev(src
, 0));
3832 static int compare_super_imsm(struct supertype
*st
, struct supertype
*tst
)
3836 * 0 same, or first was empty, and second was copied
3837 * 1 second had wrong number
3839 * 3 wrong other info
3841 struct intel_super
*first
= st
->sb
;
3842 struct intel_super
*sec
= tst
->sb
;
3849 /* in platform dependent environment test if the disks
3850 * use the same Intel hba
3851 * If not on Intel hba at all, allow anything.
3853 if (!check_env("IMSM_NO_PLATFORM") && first
->hba
&& sec
->hba
) {
3854 if (first
->hba
->type
!= sec
->hba
->type
) {
3856 "HBAs of devices do not match %s != %s\n",
3857 get_sys_dev_type(first
->hba
->type
),
3858 get_sys_dev_type(sec
->hba
->type
));
3861 if (first
->orom
!= sec
->orom
) {
3863 "HBAs of devices do not match %s != %s\n",
3864 first
->hba
->pci_id
, sec
->hba
->pci_id
);
3869 /* if an anchor does not have num_raid_devs set then it is a free
3872 if (first
->anchor
->num_raid_devs
> 0 &&
3873 sec
->anchor
->num_raid_devs
> 0) {
3874 /* Determine if these disks might ever have been
3875 * related. Further disambiguation can only take place
3876 * in load_super_imsm_all
3878 __u32 first_family
= first
->anchor
->orig_family_num
;
3879 __u32 sec_family
= sec
->anchor
->orig_family_num
;
3881 if (memcmp(first
->anchor
->sig
, sec
->anchor
->sig
,
3882 MAX_SIGNATURE_LENGTH
) != 0)
3885 if (first_family
== 0)
3886 first_family
= first
->anchor
->family_num
;
3887 if (sec_family
== 0)
3888 sec_family
= sec
->anchor
->family_num
;
3890 if (first_family
!= sec_family
)
3895 /* if 'first' is a spare promote it to a populated mpb with sec's
3898 if (first
->anchor
->num_raid_devs
== 0 &&
3899 sec
->anchor
->num_raid_devs
> 0) {
3901 struct intel_dev
*dv
;
3902 struct imsm_dev
*dev
;
3904 /* we need to copy raid device info from sec if an allocation
3905 * fails here we don't associate the spare
3907 for (i
= 0; i
< sec
->anchor
->num_raid_devs
; i
++) {
3908 dv
= xmalloc(sizeof(*dv
));
3909 dev
= xmalloc(sizeof_imsm_dev(get_imsm_dev(sec
, i
), 1));
3912 dv
->next
= first
->devlist
;
3913 first
->devlist
= dv
;
3915 if (i
< sec
->anchor
->num_raid_devs
) {
3916 /* allocation failure */
3917 free_devlist(first
);
3918 pr_err("imsm: failed to associate spare\n");
3921 first
->anchor
->num_raid_devs
= sec
->anchor
->num_raid_devs
;
3922 first
->anchor
->orig_family_num
= sec
->anchor
->orig_family_num
;
3923 first
->anchor
->family_num
= sec
->anchor
->family_num
;
3924 memcpy(first
->anchor
->sig
, sec
->anchor
->sig
, MAX_SIGNATURE_LENGTH
);
3925 for (i
= 0; i
< sec
->anchor
->num_raid_devs
; i
++)
3926 imsm_copy_dev(get_imsm_dev(first
, i
), get_imsm_dev(sec
, i
));
3932 static void fd2devname(int fd
, char *name
)
3936 char dname
[PATH_MAX
];
3941 if (fstat(fd
, &st
) != 0)
3943 sprintf(path
, "/sys/dev/block/%d:%d",
3944 major(st
.st_rdev
), minor(st
.st_rdev
));
3946 rv
= readlink(path
, dname
, sizeof(dname
)-1);
3951 nm
= strrchr(dname
, '/');
3954 snprintf(name
, MAX_RAID_SERIAL_LEN
, "/dev/%s", nm
);
3958 static int nvme_get_serial(int fd
, void *buf
, size_t buf_len
)
3961 char *name
= fd2kname(fd
);
3966 if (strncmp(name
, "nvme", 4) != 0)
3969 snprintf(path
, sizeof(path
) - 1, "/sys/block/%s/device/serial", name
);
3971 return load_sys(path
, buf
, buf_len
);
3974 extern int scsi_get_serial(int fd
, void *buf
, size_t buf_len
);
3976 static int imsm_read_serial(int fd
, char *devname
,
3977 __u8
*serial
, size_t serial_buf_len
)
3986 memset(buf
, 0, sizeof(buf
));
3988 rv
= nvme_get_serial(fd
, buf
, sizeof(buf
));
3991 rv
= scsi_get_serial(fd
, buf
, sizeof(buf
));
3993 if (rv
&& check_env("IMSM_DEVNAME_AS_SERIAL")) {
3994 memset(serial
, 0, MAX_RAID_SERIAL_LEN
);
3995 fd2devname(fd
, (char *) serial
);
4001 pr_err("Failed to retrieve serial for %s\n",
4006 /* trim all whitespace and non-printable characters and convert
4009 for (i
= 0, dest
= buf
; i
< sizeof(buf
) && buf
[i
]; i
++) {
4012 /* ':' is reserved for use in placeholder serial
4013 * numbers for missing disks
4024 if (len
> serial_buf_len
) {
4025 /* truncate leading characters */
4026 dest
+= len
- serial_buf_len
;
4027 len
= serial_buf_len
;
4030 memset(serial
, 0, serial_buf_len
);
4031 memcpy(serial
, dest
, len
);
4036 static int serialcmp(__u8
*s1
, __u8
*s2
)
4038 return strncmp((char *) s1
, (char *) s2
, MAX_RAID_SERIAL_LEN
);
4041 static void serialcpy(__u8
*dest
, __u8
*src
)
4043 strncpy((char *) dest
, (char *) src
, MAX_RAID_SERIAL_LEN
);
4046 static struct dl
*serial_to_dl(__u8
*serial
, struct intel_super
*super
)
4050 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
4051 if (serialcmp(dl
->serial
, serial
) == 0)
4057 static struct imsm_disk
*
4058 __serial_to_disk(__u8
*serial
, struct imsm_super
*mpb
, int *idx
)
4062 for (i
= 0; i
< mpb
->num_disks
; i
++) {
4063 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, i
);
4065 if (serialcmp(disk
->serial
, serial
) == 0) {
4076 load_imsm_disk(int fd
, struct intel_super
*super
, char *devname
, int keep_fd
)
4078 struct imsm_disk
*disk
;
4083 __u8 serial
[MAX_RAID_SERIAL_LEN
];
4085 rv
= imsm_read_serial(fd
, devname
, serial
, MAX_RAID_SERIAL_LEN
);
4090 dl
= xcalloc(1, sizeof(*dl
));
4093 dl
->major
= major(stb
.st_rdev
);
4094 dl
->minor
= minor(stb
.st_rdev
);
4095 dl
->next
= super
->disks
;
4096 dl
->fd
= keep_fd
? fd
: -1;
4097 assert(super
->disks
== NULL
);
4099 serialcpy(dl
->serial
, serial
);
4102 fd2devname(fd
, name
);
4104 dl
->devname
= xstrdup(devname
);
4106 dl
->devname
= xstrdup(name
);
4108 /* look up this disk's index in the current anchor */
4109 disk
= __serial_to_disk(dl
->serial
, super
->anchor
, &dl
->index
);
4112 /* only set index on disks that are a member of a
4113 * populated contianer, i.e. one with raid_devs
4115 if (is_failed(&dl
->disk
))
4117 else if (is_spare(&dl
->disk
) || is_journal(&dl
->disk
))
4124 /* When migrating map0 contains the 'destination' state while map1
4125 * contains the current state. When not migrating map0 contains the
4126 * current state. This routine assumes that map[0].map_state is set to
4127 * the current array state before being called.
4129 * Migration is indicated by one of the following states
4130 * 1/ Idle (migr_state=0 map0state=normal||unitialized||degraded||failed)
4131 * 2/ Initialize (migr_state=1 migr_type=MIGR_INIT map0state=normal
4132 * map1state=unitialized)
4133 * 3/ Repair (Resync) (migr_state=1 migr_type=MIGR_REPAIR map0state=normal
4135 * 4/ Rebuild (migr_state=1 migr_type=MIGR_REBUILD map0state=normal
4136 * map1state=degraded)
4137 * 5/ Migration (mig_state=1 migr_type=MIGR_GEN_MIGR map0state=normal
4140 static void migrate(struct imsm_dev
*dev
, struct intel_super
*super
,
4141 __u8 to_state
, int migr_type
)
4143 struct imsm_map
*dest
;
4144 struct imsm_map
*src
= get_imsm_map(dev
, MAP_0
);
4146 dev
->vol
.migr_state
= 1;
4147 set_migr_type(dev
, migr_type
);
4148 dev
->vol
.curr_migr_unit
= 0;
4149 dest
= get_imsm_map(dev
, MAP_1
);
4151 /* duplicate and then set the target end state in map[0] */
4152 memcpy(dest
, src
, sizeof_imsm_map(src
));
4153 if (migr_type
== MIGR_GEN_MIGR
) {
4157 for (i
= 0; i
< src
->num_members
; i
++) {
4158 ord
= __le32_to_cpu(src
->disk_ord_tbl
[i
]);
4159 set_imsm_ord_tbl_ent(src
, i
, ord_to_idx(ord
));
4163 if (migr_type
== MIGR_GEN_MIGR
)
4164 /* Clear migration record */
4165 memset(super
->migr_rec
, 0, sizeof(struct migr_record
));
4167 src
->map_state
= to_state
;
4170 static void end_migration(struct imsm_dev
*dev
, struct intel_super
*super
,
4173 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
4174 struct imsm_map
*prev
= get_imsm_map(dev
, dev
->vol
.migr_state
== 0 ?
4178 /* merge any IMSM_ORD_REBUILD bits that were not successfully
4179 * completed in the last migration.
4181 * FIXME add support for raid-level-migration
4183 if (map_state
!= map
->map_state
&& (is_gen_migration(dev
) == 0) &&
4184 prev
->map_state
!= IMSM_T_STATE_UNINITIALIZED
) {
4185 /* when final map state is other than expected
4186 * merge maps (not for migration)
4190 for (i
= 0; i
< prev
->num_members
; i
++)
4191 for (j
= 0; j
< map
->num_members
; j
++)
4192 /* during online capacity expansion
4193 * disks position can be changed
4194 * if takeover is used
4196 if (ord_to_idx(map
->disk_ord_tbl
[j
]) ==
4197 ord_to_idx(prev
->disk_ord_tbl
[i
])) {
4198 map
->disk_ord_tbl
[j
] |=
4199 prev
->disk_ord_tbl
[i
];
4202 failed
= imsm_count_failed(super
, dev
, MAP_0
);
4203 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
4206 dev
->vol
.migr_state
= 0;
4207 set_migr_type(dev
, 0);
4208 dev
->vol
.curr_migr_unit
= 0;
4209 map
->map_state
= map_state
;
4212 static int parse_raid_devices(struct intel_super
*super
)
4215 struct imsm_dev
*dev_new
;
4216 size_t len
, len_migr
;
4218 size_t space_needed
= 0;
4219 struct imsm_super
*mpb
= super
->anchor
;
4221 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
4222 struct imsm_dev
*dev_iter
= __get_imsm_dev(super
->anchor
, i
);
4223 struct intel_dev
*dv
;
4225 len
= sizeof_imsm_dev(dev_iter
, 0);
4226 len_migr
= sizeof_imsm_dev(dev_iter
, 1);
4228 space_needed
+= len_migr
- len
;
4230 dv
= xmalloc(sizeof(*dv
));
4231 if (max_len
< len_migr
)
4233 if (max_len
> len_migr
)
4234 space_needed
+= max_len
- len_migr
;
4235 dev_new
= xmalloc(max_len
);
4236 imsm_copy_dev(dev_new
, dev_iter
);
4239 dv
->next
= super
->devlist
;
4240 super
->devlist
= dv
;
4243 /* ensure that super->buf is large enough when all raid devices
4246 if (__le32_to_cpu(mpb
->mpb_size
) + space_needed
> super
->len
) {
4249 len
= ROUND_UP(__le32_to_cpu(mpb
->mpb_size
) + space_needed
,
4250 super
->sector_size
);
4251 if (posix_memalign(&buf
, MAX_SECTOR_SIZE
, len
) != 0)
4254 memcpy(buf
, super
->buf
, super
->len
);
4255 memset(buf
+ super
->len
, 0, len
- super
->len
);
4261 super
->extra_space
+= space_needed
;
4266 /*******************************************************************************
4267 * Function: check_mpb_migr_compatibility
4268 * Description: Function checks for unsupported migration features:
4269 * - migration optimization area (pba_of_lba0)
4270 * - descending reshape (ascending_migr)
4272 * super : imsm metadata information
4274 * 0 : migration is compatible
4275 * -1 : migration is not compatible
4276 ******************************************************************************/
4277 int check_mpb_migr_compatibility(struct intel_super
*super
)
4279 struct imsm_map
*map0
, *map1
;
4280 struct migr_record
*migr_rec
= super
->migr_rec
;
4283 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
4284 struct imsm_dev
*dev_iter
= __get_imsm_dev(super
->anchor
, i
);
4287 dev_iter
->vol
.migr_state
== 1 &&
4288 dev_iter
->vol
.migr_type
== MIGR_GEN_MIGR
) {
4289 /* This device is migrating */
4290 map0
= get_imsm_map(dev_iter
, MAP_0
);
4291 map1
= get_imsm_map(dev_iter
, MAP_1
);
4292 if (pba_of_lba0(map0
) != pba_of_lba0(map1
))
4293 /* migration optimization area was used */
4295 if (migr_rec
->ascending_migr
== 0 &&
4296 migr_rec
->dest_depth_per_unit
> 0)
4297 /* descending reshape not supported yet */
4304 static void __free_imsm(struct intel_super
*super
, int free_disks
);
4306 /* load_imsm_mpb - read matrix metadata
4307 * allocates super->mpb to be freed by free_imsm
4309 static int load_imsm_mpb(int fd
, struct intel_super
*super
, char *devname
)
4311 unsigned long long dsize
;
4312 unsigned long long sectors
;
4313 unsigned int sector_size
= super
->sector_size
;
4315 struct imsm_super
*anchor
;
4318 get_dev_size(fd
, NULL
, &dsize
);
4319 if (dsize
< 2*sector_size
) {
4321 pr_err("%s: device to small for imsm\n",
4326 if (lseek64(fd
, dsize
- (sector_size
* 2), SEEK_SET
) < 0) {
4328 pr_err("Cannot seek to anchor block on %s: %s\n",
4329 devname
, strerror(errno
));
4333 if (posix_memalign((void **)&anchor
, sector_size
, sector_size
) != 0) {
4335 pr_err("Failed to allocate imsm anchor buffer on %s\n", devname
);
4338 if ((unsigned int)read(fd
, anchor
, sector_size
) != sector_size
) {
4340 pr_err("Cannot read anchor block on %s: %s\n",
4341 devname
, strerror(errno
));
4346 if (strncmp((char *) anchor
->sig
, MPB_SIGNATURE
, MPB_SIG_LEN
) != 0) {
4348 pr_err("no IMSM anchor on %s\n", devname
);
4353 __free_imsm(super
, 0);
4354 /* reload capability and hba */
4356 /* capability and hba must be updated with new super allocation */
4357 find_intel_hba_capability(fd
, super
, devname
);
4358 super
->len
= ROUND_UP(anchor
->mpb_size
, sector_size
);
4359 if (posix_memalign(&super
->buf
, MAX_SECTOR_SIZE
, super
->len
) != 0) {
4361 pr_err("unable to allocate %zu byte mpb buffer\n",
4366 memcpy(super
->buf
, anchor
, sector_size
);
4368 sectors
= mpb_sectors(anchor
, sector_size
) - 1;
4371 if (posix_memalign(&super
->migr_rec_buf
, MAX_SECTOR_SIZE
,
4372 MIGR_REC_BUF_SECTORS
*MAX_SECTOR_SIZE
) != 0) {
4373 pr_err("could not allocate migr_rec buffer\n");
4377 super
->clean_migration_record_by_mdmon
= 0;
4380 check_sum
= __gen_imsm_checksum(super
->anchor
);
4381 if (check_sum
!= __le32_to_cpu(super
->anchor
->check_sum
)) {
4383 pr_err("IMSM checksum %x != %x on %s\n",
4385 __le32_to_cpu(super
->anchor
->check_sum
),
4393 /* read the extended mpb */
4394 if (lseek64(fd
, dsize
- (sector_size
* (2 + sectors
)), SEEK_SET
) < 0) {
4396 pr_err("Cannot seek to extended mpb on %s: %s\n",
4397 devname
, strerror(errno
));
4401 if ((unsigned int)read(fd
, super
->buf
+ sector_size
,
4402 super
->len
- sector_size
) != super
->len
- sector_size
) {
4404 pr_err("Cannot read extended mpb on %s: %s\n",
4405 devname
, strerror(errno
));
4409 check_sum
= __gen_imsm_checksum(super
->anchor
);
4410 if (check_sum
!= __le32_to_cpu(super
->anchor
->check_sum
)) {
4412 pr_err("IMSM checksum %x != %x on %s\n",
4413 check_sum
, __le32_to_cpu(super
->anchor
->check_sum
),
4421 static int read_imsm_migr_rec(int fd
, struct intel_super
*super
);
4423 /* clears hi bits in metadata if MPB_ATTRIB_2TB_DISK not set */
4424 static void clear_hi(struct intel_super
*super
)
4426 struct imsm_super
*mpb
= super
->anchor
;
4428 if (mpb
->attributes
& MPB_ATTRIB_2TB_DISK
)
4430 for (i
= 0; i
< mpb
->num_disks
; ++i
) {
4431 struct imsm_disk
*disk
= &mpb
->disk
[i
];
4432 disk
->total_blocks_hi
= 0;
4434 for (i
= 0; i
< mpb
->num_raid_devs
; ++i
) {
4435 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
4438 for (n
= 0; n
< 2; ++n
) {
4439 struct imsm_map
*map
= get_imsm_map(dev
, n
);
4442 map
->pba_of_lba0_hi
= 0;
4443 map
->blocks_per_member_hi
= 0;
4444 map
->num_data_stripes_hi
= 0;
4450 load_and_parse_mpb(int fd
, struct intel_super
*super
, char *devname
, int keep_fd
)
4454 err
= load_imsm_mpb(fd
, super
, devname
);
4457 if (super
->sector_size
== 4096)
4458 convert_from_4k(super
);
4459 err
= load_imsm_disk(fd
, super
, devname
, keep_fd
);
4462 err
= parse_raid_devices(super
);
4465 err
= load_bbm_log(super
);
4470 static void __free_imsm_disk(struct dl
*d
)
4482 static void free_imsm_disks(struct intel_super
*super
)
4486 while (super
->disks
) {
4488 super
->disks
= d
->next
;
4489 __free_imsm_disk(d
);
4491 while (super
->disk_mgmt_list
) {
4492 d
= super
->disk_mgmt_list
;
4493 super
->disk_mgmt_list
= d
->next
;
4494 __free_imsm_disk(d
);
4496 while (super
->missing
) {
4498 super
->missing
= d
->next
;
4499 __free_imsm_disk(d
);
4504 /* free all the pieces hanging off of a super pointer */
4505 static void __free_imsm(struct intel_super
*super
, int free_disks
)
4507 struct intel_hba
*elem
, *next
;
4513 /* unlink capability description */
4515 if (super
->migr_rec_buf
) {
4516 free(super
->migr_rec_buf
);
4517 super
->migr_rec_buf
= NULL
;
4520 free_imsm_disks(super
);
4521 free_devlist(super
);
4525 free((void *)elem
->path
);
4531 free(super
->bbm_log
);
4535 static void free_imsm(struct intel_super
*super
)
4537 __free_imsm(super
, 1);
4538 free(super
->bb
.entries
);
4542 static void free_super_imsm(struct supertype
*st
)
4544 struct intel_super
*super
= st
->sb
;
4553 static struct intel_super
*alloc_super(void)
4555 struct intel_super
*super
= xcalloc(1, sizeof(*super
));
4557 super
->current_vol
= -1;
4558 super
->create_offset
= ~((unsigned long long) 0);
4560 super
->bb
.entries
= xmalloc(BBM_LOG_MAX_ENTRIES
*
4561 sizeof(struct md_bb_entry
));
4562 if (!super
->bb
.entries
) {
4571 * find and allocate hba and OROM/EFI based on valid fd of RAID component device
4573 static int find_intel_hba_capability(int fd
, struct intel_super
*super
, char *devname
)
4575 struct sys_dev
*hba_name
;
4578 if (fd
>= 0 && test_partition(fd
)) {
4579 pr_err("imsm: %s is a partition, cannot be used in IMSM\n",
4583 if (fd
< 0 || check_env("IMSM_NO_PLATFORM")) {
4588 hba_name
= find_disk_attached_hba(fd
, NULL
);
4591 pr_err("%s is not attached to Intel(R) RAID controller.\n",
4595 rv
= attach_hba_to_super(super
, hba_name
);
4598 struct intel_hba
*hba
= super
->hba
;
4600 pr_err("%s is attached to Intel(R) %s %s (%s),\n"
4601 " but the container is assigned to Intel(R) %s %s (",
4603 get_sys_dev_type(hba_name
->type
),
4604 hba_name
->type
== SYS_DEV_VMD
? "domain" : "RAID controller",
4605 hba_name
->pci_id
? : "Err!",
4606 get_sys_dev_type(super
->hba
->type
),
4607 hba
->type
== SYS_DEV_VMD
? "domain" : "RAID controller");
4610 fprintf(stderr
, "%s", hba
->pci_id
? : "Err!");
4612 fprintf(stderr
, ", ");
4615 fprintf(stderr
, ").\n"
4616 " Mixing devices attached to different controllers is not allowed.\n");
4620 super
->orom
= find_imsm_capability(hba_name
);
4627 /* find_missing - helper routine for load_super_imsm_all that identifies
4628 * disks that have disappeared from the system. This routine relies on
4629 * the mpb being uptodate, which it is at load time.
4631 static int find_missing(struct intel_super
*super
)
4634 struct imsm_super
*mpb
= super
->anchor
;
4636 struct imsm_disk
*disk
;
4638 for (i
= 0; i
< mpb
->num_disks
; i
++) {
4639 disk
= __get_imsm_disk(mpb
, i
);
4640 dl
= serial_to_dl(disk
->serial
, super
);
4644 dl
= xmalloc(sizeof(*dl
));
4648 dl
->devname
= xstrdup("missing");
4650 serialcpy(dl
->serial
, disk
->serial
);
4653 dl
->next
= super
->missing
;
4654 super
->missing
= dl
;
4660 static struct intel_disk
*disk_list_get(__u8
*serial
, struct intel_disk
*disk_list
)
4662 struct intel_disk
*idisk
= disk_list
;
4665 if (serialcmp(idisk
->disk
.serial
, serial
) == 0)
4667 idisk
= idisk
->next
;
4673 static int __prep_thunderdome(struct intel_super
**table
, int tbl_size
,
4674 struct intel_super
*super
,
4675 struct intel_disk
**disk_list
)
4677 struct imsm_disk
*d
= &super
->disks
->disk
;
4678 struct imsm_super
*mpb
= super
->anchor
;
4681 for (i
= 0; i
< tbl_size
; i
++) {
4682 struct imsm_super
*tbl_mpb
= table
[i
]->anchor
;
4683 struct imsm_disk
*tbl_d
= &table
[i
]->disks
->disk
;
4685 if (tbl_mpb
->family_num
== mpb
->family_num
) {
4686 if (tbl_mpb
->check_sum
== mpb
->check_sum
) {
4687 dprintf("mpb from %d:%d matches %d:%d\n",
4688 super
->disks
->major
,
4689 super
->disks
->minor
,
4690 table
[i
]->disks
->major
,
4691 table
[i
]->disks
->minor
);
4695 if (((is_configured(d
) && !is_configured(tbl_d
)) ||
4696 is_configured(d
) == is_configured(tbl_d
)) &&
4697 tbl_mpb
->generation_num
< mpb
->generation_num
) {
4698 /* current version of the mpb is a
4699 * better candidate than the one in
4700 * super_table, but copy over "cross
4701 * generational" status
4703 struct intel_disk
*idisk
;
4705 dprintf("mpb from %d:%d replaces %d:%d\n",
4706 super
->disks
->major
,
4707 super
->disks
->minor
,
4708 table
[i
]->disks
->major
,
4709 table
[i
]->disks
->minor
);
4711 idisk
= disk_list_get(tbl_d
->serial
, *disk_list
);
4712 if (idisk
&& is_failed(&idisk
->disk
))
4713 tbl_d
->status
|= FAILED_DISK
;
4716 struct intel_disk
*idisk
;
4717 struct imsm_disk
*disk
;
4719 /* tbl_mpb is more up to date, but copy
4720 * over cross generational status before
4723 disk
= __serial_to_disk(d
->serial
, mpb
, NULL
);
4724 if (disk
&& is_failed(disk
))
4725 d
->status
|= FAILED_DISK
;
4727 idisk
= disk_list_get(d
->serial
, *disk_list
);
4730 if (disk
&& is_configured(disk
))
4731 idisk
->disk
.status
|= CONFIGURED_DISK
;
4734 dprintf("mpb from %d:%d prefer %d:%d\n",
4735 super
->disks
->major
,
4736 super
->disks
->minor
,
4737 table
[i
]->disks
->major
,
4738 table
[i
]->disks
->minor
);
4746 table
[tbl_size
++] = super
;
4750 /* update/extend the merged list of imsm_disk records */
4751 for (j
= 0; j
< mpb
->num_disks
; j
++) {
4752 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, j
);
4753 struct intel_disk
*idisk
;
4755 idisk
= disk_list_get(disk
->serial
, *disk_list
);
4757 idisk
->disk
.status
|= disk
->status
;
4758 if (is_configured(&idisk
->disk
) ||
4759 is_failed(&idisk
->disk
))
4760 idisk
->disk
.status
&= ~(SPARE_DISK
);
4762 idisk
= xcalloc(1, sizeof(*idisk
));
4763 idisk
->owner
= IMSM_UNKNOWN_OWNER
;
4764 idisk
->disk
= *disk
;
4765 idisk
->next
= *disk_list
;
4769 if (serialcmp(idisk
->disk
.serial
, d
->serial
) == 0)
4776 static struct intel_super
*
4777 validate_members(struct intel_super
*super
, struct intel_disk
*disk_list
,
4780 struct imsm_super
*mpb
= super
->anchor
;
4784 for (i
= 0; i
< mpb
->num_disks
; i
++) {
4785 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, i
);
4786 struct intel_disk
*idisk
;
4788 idisk
= disk_list_get(disk
->serial
, disk_list
);
4790 if (idisk
->owner
== owner
||
4791 idisk
->owner
== IMSM_UNKNOWN_OWNER
)
4794 dprintf("'%.16s' owner %d != %d\n",
4795 disk
->serial
, idisk
->owner
,
4798 dprintf("unknown disk %x [%d]: %.16s\n",
4799 __le32_to_cpu(mpb
->family_num
), i
,
4805 if (ok_count
== mpb
->num_disks
)
4810 static void show_conflicts(__u32 family_num
, struct intel_super
*super_list
)
4812 struct intel_super
*s
;
4814 for (s
= super_list
; s
; s
= s
->next
) {
4815 if (family_num
!= s
->anchor
->family_num
)
4817 pr_err("Conflict, offlining family %#x on '%s'\n",
4818 __le32_to_cpu(family_num
), s
->disks
->devname
);
4822 static struct intel_super
*
4823 imsm_thunderdome(struct intel_super
**super_list
, int len
)
4825 struct intel_super
*super_table
[len
];
4826 struct intel_disk
*disk_list
= NULL
;
4827 struct intel_super
*champion
, *spare
;
4828 struct intel_super
*s
, **del
;
4833 memset(super_table
, 0, sizeof(super_table
));
4834 for (s
= *super_list
; s
; s
= s
->next
)
4835 tbl_size
= __prep_thunderdome(super_table
, tbl_size
, s
, &disk_list
);
4837 for (i
= 0; i
< tbl_size
; i
++) {
4838 struct imsm_disk
*d
;
4839 struct intel_disk
*idisk
;
4840 struct imsm_super
*mpb
= super_table
[i
]->anchor
;
4843 d
= &s
->disks
->disk
;
4845 /* 'd' must appear in merged disk list for its
4846 * configuration to be valid
4848 idisk
= disk_list_get(d
->serial
, disk_list
);
4849 if (idisk
&& idisk
->owner
== i
)
4850 s
= validate_members(s
, disk_list
, i
);
4855 dprintf("marking family: %#x from %d:%d offline\n",
4857 super_table
[i
]->disks
->major
,
4858 super_table
[i
]->disks
->minor
);
4862 /* This is where the mdadm implementation differs from the Windows
4863 * driver which has no strict concept of a container. We can only
4864 * assemble one family from a container, so when returning a prodigal
4865 * array member to this system the code will not be able to disambiguate
4866 * the container contents that should be assembled ("foreign" versus
4867 * "local"). It requires user intervention to set the orig_family_num
4868 * to a new value to establish a new container. The Windows driver in
4869 * this situation fixes up the volume name in place and manages the
4870 * foreign array as an independent entity.
4875 for (i
= 0; i
< tbl_size
; i
++) {
4876 struct intel_super
*tbl_ent
= super_table
[i
];
4882 if (tbl_ent
->anchor
->num_raid_devs
== 0) {
4887 if (s
&& !is_spare
) {
4888 show_conflicts(tbl_ent
->anchor
->family_num
, *super_list
);
4890 } else if (!s
&& !is_spare
)
4903 pr_err("Chose family %#x on '%s', assemble conflicts to new container with '--update=uuid'\n",
4904 __le32_to_cpu(s
->anchor
->family_num
), s
->disks
->devname
);
4906 /* collect all dl's onto 'champion', and update them to
4907 * champion's version of the status
4909 for (s
= *super_list
; s
; s
= s
->next
) {
4910 struct imsm_super
*mpb
= champion
->anchor
;
4911 struct dl
*dl
= s
->disks
;
4916 mpb
->attributes
|= s
->anchor
->attributes
& MPB_ATTRIB_2TB_DISK
;
4918 for (i
= 0; i
< mpb
->num_disks
; i
++) {
4919 struct imsm_disk
*disk
;
4921 disk
= __serial_to_disk(dl
->serial
, mpb
, &dl
->index
);
4924 /* only set index on disks that are a member of
4925 * a populated contianer, i.e. one with
4928 if (is_failed(&dl
->disk
))
4930 else if (is_spare(&dl
->disk
))
4936 if (i
>= mpb
->num_disks
) {
4937 struct intel_disk
*idisk
;
4939 idisk
= disk_list_get(dl
->serial
, disk_list
);
4940 if (idisk
&& is_spare(&idisk
->disk
) &&
4941 !is_failed(&idisk
->disk
) && !is_configured(&idisk
->disk
))
4949 dl
->next
= champion
->disks
;
4950 champion
->disks
= dl
;
4954 /* delete 'champion' from super_list */
4955 for (del
= super_list
; *del
; ) {
4956 if (*del
== champion
) {
4957 *del
= (*del
)->next
;
4960 del
= &(*del
)->next
;
4962 champion
->next
= NULL
;
4966 struct intel_disk
*idisk
= disk_list
;
4968 disk_list
= disk_list
->next
;
4976 get_sra_super_block(int fd
, struct intel_super
**super_list
, char *devname
, int *max
, int keep_fd
);
4977 static int get_super_block(struct intel_super
**super_list
, char *devnm
, char *devname
,
4978 int major
, int minor
, int keep_fd
);
4980 get_devlist_super_block(struct md_list
*devlist
, struct intel_super
**super_list
,
4981 int *max
, int keep_fd
);
4983 static int load_super_imsm_all(struct supertype
*st
, int fd
, void **sbp
,
4984 char *devname
, struct md_list
*devlist
,
4987 struct intel_super
*super_list
= NULL
;
4988 struct intel_super
*super
= NULL
;
4993 /* 'fd' is an opened container */
4994 err
= get_sra_super_block(fd
, &super_list
, devname
, &i
, keep_fd
);
4996 /* get super block from devlist devices */
4997 err
= get_devlist_super_block(devlist
, &super_list
, &i
, keep_fd
);
5000 /* all mpbs enter, maybe one leaves */
5001 super
= imsm_thunderdome(&super_list
, i
);
5007 if (find_missing(super
) != 0) {
5013 /* load migration record */
5014 err
= load_imsm_migr_rec(super
, NULL
);
5016 /* migration is in progress,
5017 * but migr_rec cannot be loaded,
5023 /* Check migration compatibility */
5024 if (err
== 0 && check_mpb_migr_compatibility(super
) != 0) {
5025 pr_err("Unsupported migration detected");
5027 fprintf(stderr
, " on %s\n", devname
);
5029 fprintf(stderr
, " (IMSM).\n");
5038 while (super_list
) {
5039 struct intel_super
*s
= super_list
;
5041 super_list
= super_list
->next
;
5050 strcpy(st
->container_devnm
, fd2devnm(fd
));
5052 st
->container_devnm
[0] = 0;
5053 if (err
== 0 && st
->ss
== NULL
) {
5054 st
->ss
= &super_imsm
;
5055 st
->minor_version
= 0;
5056 st
->max_devs
= IMSM_MAX_DEVICES
;
5062 get_devlist_super_block(struct md_list
*devlist
, struct intel_super
**super_list
,
5063 int *max
, int keep_fd
)
5065 struct md_list
*tmpdev
;
5069 for (i
= 0, tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
5070 if (tmpdev
->used
!= 1)
5072 if (tmpdev
->container
== 1) {
5074 int fd
= dev_open(tmpdev
->devname
, O_RDONLY
|O_EXCL
);
5076 pr_err("cannot open device %s: %s\n",
5077 tmpdev
->devname
, strerror(errno
));
5081 err
= get_sra_super_block(fd
, super_list
,
5082 tmpdev
->devname
, &lmax
,
5091 int major
= major(tmpdev
->st_rdev
);
5092 int minor
= minor(tmpdev
->st_rdev
);
5093 err
= get_super_block(super_list
,
5110 static int get_super_block(struct intel_super
**super_list
, char *devnm
, char *devname
,
5111 int major
, int minor
, int keep_fd
)
5113 struct intel_super
*s
;
5125 sprintf(nm
, "%d:%d", major
, minor
);
5126 dfd
= dev_open(nm
, O_RDWR
);
5132 get_dev_sector_size(dfd
, NULL
, &s
->sector_size
);
5133 find_intel_hba_capability(dfd
, s
, devname
);
5134 err
= load_and_parse_mpb(dfd
, s
, NULL
, keep_fd
);
5136 /* retry the load if we might have raced against mdmon */
5137 if (err
== 3 && devnm
&& mdmon_running(devnm
))
5138 for (retry
= 0; retry
< 3; retry
++) {
5140 err
= load_and_parse_mpb(dfd
, s
, NULL
, keep_fd
);
5146 s
->next
= *super_list
;
5154 if (dfd
>= 0 && !keep_fd
)
5161 get_sra_super_block(int fd
, struct intel_super
**super_list
, char *devname
, int *max
, int keep_fd
)
5168 sra
= sysfs_read(fd
, NULL
, GET_LEVEL
|GET_VERSION
|GET_DEVS
|GET_STATE
);
5172 if (sra
->array
.major_version
!= -1 ||
5173 sra
->array
.minor_version
!= -2 ||
5174 strcmp(sra
->text_version
, "imsm") != 0) {
5179 devnm
= fd2devnm(fd
);
5180 for (sd
= sra
->devs
, i
= 0; sd
; sd
= sd
->next
, i
++) {
5181 if (get_super_block(super_list
, devnm
, devname
,
5182 sd
->disk
.major
, sd
->disk
.minor
, keep_fd
) != 0) {
5193 static int load_container_imsm(struct supertype
*st
, int fd
, char *devname
)
5195 return load_super_imsm_all(st
, fd
, &st
->sb
, devname
, NULL
, 1);
5198 static int load_super_imsm(struct supertype
*st
, int fd
, char *devname
)
5200 struct intel_super
*super
;
5204 if (test_partition(fd
))
5205 /* IMSM not allowed on partitions */
5208 free_super_imsm(st
);
5210 super
= alloc_super();
5211 get_dev_sector_size(fd
, NULL
, &super
->sector_size
);
5214 /* Load hba and capabilities if they exist.
5215 * But do not preclude loading metadata in case capabilities or hba are
5216 * non-compliant and ignore_hw_compat is set.
5218 rv
= find_intel_hba_capability(fd
, super
, devname
);
5219 /* no orom/efi or non-intel hba of the disk */
5220 if (rv
!= 0 && st
->ignore_hw_compat
== 0) {
5222 pr_err("No OROM/EFI properties for %s\n", devname
);
5226 rv
= load_and_parse_mpb(fd
, super
, devname
, 0);
5228 /* retry the load if we might have raced against mdmon */
5230 struct mdstat_ent
*mdstat
= NULL
;
5231 char *name
= fd2kname(fd
);
5234 mdstat
= mdstat_by_component(name
);
5236 if (mdstat
&& mdmon_running(mdstat
->devnm
) && getpid() != mdmon_pid(mdstat
->devnm
)) {
5237 for (retry
= 0; retry
< 3; retry
++) {
5239 rv
= load_and_parse_mpb(fd
, super
, devname
, 0);
5245 free_mdstat(mdstat
);
5250 pr_err("Failed to load all information sections on %s\n", devname
);
5256 if (st
->ss
== NULL
) {
5257 st
->ss
= &super_imsm
;
5258 st
->minor_version
= 0;
5259 st
->max_devs
= IMSM_MAX_DEVICES
;
5262 /* load migration record */
5263 if (load_imsm_migr_rec(super
, NULL
) == 0) {
5264 /* Check for unsupported migration features */
5265 if (check_mpb_migr_compatibility(super
) != 0) {
5266 pr_err("Unsupported migration detected");
5268 fprintf(stderr
, " on %s\n", devname
);
5270 fprintf(stderr
, " (IMSM).\n");
5278 static __u16
info_to_blocks_per_strip(mdu_array_info_t
*info
)
5280 if (info
->level
== 1)
5282 return info
->chunk_size
>> 9;
5285 static unsigned long long info_to_blocks_per_member(mdu_array_info_t
*info
,
5286 unsigned long long size
)
5288 if (info
->level
== 1)
5291 return (size
* 2) & ~(info_to_blocks_per_strip(info
) - 1);
5294 static void imsm_update_version_info(struct intel_super
*super
)
5296 /* update the version and attributes */
5297 struct imsm_super
*mpb
= super
->anchor
;
5299 struct imsm_dev
*dev
;
5300 struct imsm_map
*map
;
5303 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
5304 dev
= get_imsm_dev(super
, i
);
5305 map
= get_imsm_map(dev
, MAP_0
);
5306 if (__le32_to_cpu(dev
->size_high
) > 0)
5307 mpb
->attributes
|= MPB_ATTRIB_2TB
;
5309 /* FIXME detect when an array spans a port multiplier */
5311 mpb
->attributes
|= MPB_ATTRIB_PM
;
5314 if (mpb
->num_raid_devs
> 1 ||
5315 mpb
->attributes
!= MPB_ATTRIB_CHECKSUM_VERIFY
) {
5316 version
= MPB_VERSION_ATTRIBS
;
5317 switch (get_imsm_raid_level(map
)) {
5318 case 0: mpb
->attributes
|= MPB_ATTRIB_RAID0
; break;
5319 case 1: mpb
->attributes
|= MPB_ATTRIB_RAID1
; break;
5320 case 10: mpb
->attributes
|= MPB_ATTRIB_RAID10
; break;
5321 case 5: mpb
->attributes
|= MPB_ATTRIB_RAID5
; break;
5324 if (map
->num_members
>= 5)
5325 version
= MPB_VERSION_5OR6_DISK_ARRAY
;
5326 else if (dev
->status
== DEV_CLONE_N_GO
)
5327 version
= MPB_VERSION_CNG
;
5328 else if (get_imsm_raid_level(map
) == 5)
5329 version
= MPB_VERSION_RAID5
;
5330 else if (map
->num_members
>= 3)
5331 version
= MPB_VERSION_3OR4_DISK_ARRAY
;
5332 else if (get_imsm_raid_level(map
) == 1)
5333 version
= MPB_VERSION_RAID1
;
5335 version
= MPB_VERSION_RAID0
;
5337 strcpy(((char *) mpb
->sig
) + strlen(MPB_SIGNATURE
), version
);
5341 static int check_name(struct intel_super
*super
, char *name
, int quiet
)
5343 struct imsm_super
*mpb
= super
->anchor
;
5344 char *reason
= NULL
;
5346 size_t len
= strlen(name
);
5350 while (isspace(start
[len
- 1]))
5352 while (*start
&& isspace(*start
))
5354 memmove(name
, start
, len
+ 1);
5357 if (len
> MAX_RAID_SERIAL_LEN
)
5358 reason
= "must be 16 characters or less";
5360 reason
= "must be a non-empty string";
5362 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
5363 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
5365 if (strncmp((char *) dev
->volume
, name
, MAX_RAID_SERIAL_LEN
) == 0) {
5366 reason
= "already exists";
5371 if (reason
&& !quiet
)
5372 pr_err("imsm volume name %s\n", reason
);
5377 static int init_super_imsm_volume(struct supertype
*st
, mdu_array_info_t
*info
,
5378 struct shape
*s
, char *name
,
5379 char *homehost
, int *uuid
,
5380 long long data_offset
)
5382 /* We are creating a volume inside a pre-existing container.
5383 * so st->sb is already set.
5385 struct intel_super
*super
= st
->sb
;
5386 unsigned int sector_size
= super
->sector_size
;
5387 struct imsm_super
*mpb
= super
->anchor
;
5388 struct intel_dev
*dv
;
5389 struct imsm_dev
*dev
;
5390 struct imsm_vol
*vol
;
5391 struct imsm_map
*map
;
5392 int idx
= mpb
->num_raid_devs
;
5395 unsigned long long array_blocks
;
5396 size_t size_old
, size_new
;
5397 unsigned long long num_data_stripes
;
5398 unsigned int data_disks
;
5399 unsigned long long size_per_member
;
5401 if (super
->orom
&& mpb
->num_raid_devs
>= super
->orom
->vpa
) {
5402 pr_err("This imsm-container already has the maximum of %d volumes\n", super
->orom
->vpa
);
5406 /* ensure the mpb is large enough for the new data */
5407 size_old
= __le32_to_cpu(mpb
->mpb_size
);
5408 size_new
= disks_to_mpb_size(info
->nr_disks
);
5409 if (size_new
> size_old
) {
5411 size_t size_round
= ROUND_UP(size_new
, sector_size
);
5413 if (posix_memalign(&mpb_new
, sector_size
, size_round
) != 0) {
5414 pr_err("could not allocate new mpb\n");
5417 if (posix_memalign(&super
->migr_rec_buf
, MAX_SECTOR_SIZE
,
5418 MIGR_REC_BUF_SECTORS
*
5419 MAX_SECTOR_SIZE
) != 0) {
5420 pr_err("could not allocate migr_rec buffer\n");
5426 memcpy(mpb_new
, mpb
, size_old
);
5429 super
->anchor
= mpb_new
;
5430 mpb
->mpb_size
= __cpu_to_le32(size_new
);
5431 memset(mpb_new
+ size_old
, 0, size_round
- size_old
);
5432 super
->len
= size_round
;
5434 super
->current_vol
= idx
;
5436 /* handle 'failed_disks' by either:
5437 * a) create dummy disk entries in the table if this the first
5438 * volume in the array. We add them here as this is the only
5439 * opportunity to add them. add_to_super_imsm_volume()
5440 * handles the non-failed disks and continues incrementing
5442 * b) validate that 'failed_disks' matches the current number
5443 * of missing disks if the container is populated
5445 if (super
->current_vol
== 0) {
5447 for (i
= 0; i
< info
->failed_disks
; i
++) {
5448 struct imsm_disk
*disk
;
5451 disk
= __get_imsm_disk(mpb
, i
);
5452 disk
->status
= CONFIGURED_DISK
| FAILED_DISK
;
5453 disk
->scsi_id
= __cpu_to_le32(~(__u32
)0);
5454 snprintf((char *) disk
->serial
, MAX_RAID_SERIAL_LEN
,
5455 "missing:%d", (__u8
)i
);
5457 find_missing(super
);
5462 for (d
= super
->missing
; d
; d
= d
->next
)
5464 if (info
->failed_disks
> missing
) {
5465 pr_err("unable to add 'missing' disk to container\n");
5470 if (!check_name(super
, name
, 0))
5472 dv
= xmalloc(sizeof(*dv
));
5473 dev
= xcalloc(1, sizeof(*dev
) + sizeof(__u32
) * (info
->raid_disks
- 1));
5475 * Explicitly allow truncating to not confuse gcc's
5476 * -Werror=stringop-truncation
5478 namelen
= min((int) strlen(name
), MAX_RAID_SERIAL_LEN
);
5479 memcpy(dev
->volume
, name
, namelen
);
5480 array_blocks
= calc_array_size(info
->level
, info
->raid_disks
,
5481 info
->layout
, info
->chunk_size
,
5482 s
->size
* BLOCKS_PER_KB
);
5483 data_disks
= get_data_disks(info
->level
, info
->layout
,
5485 array_blocks
= round_size_to_mb(array_blocks
, data_disks
);
5486 size_per_member
= array_blocks
/ data_disks
;
5488 set_imsm_dev_size(dev
, array_blocks
);
5489 dev
->status
= (DEV_READ_COALESCING
| DEV_WRITE_COALESCING
);
5491 vol
->migr_state
= 0;
5492 set_migr_type(dev
, MIGR_INIT
);
5493 vol
->dirty
= !info
->state
;
5494 vol
->curr_migr_unit
= 0;
5495 map
= get_imsm_map(dev
, MAP_0
);
5496 set_pba_of_lba0(map
, super
->create_offset
);
5497 map
->blocks_per_strip
= __cpu_to_le16(info_to_blocks_per_strip(info
));
5498 map
->failed_disk_num
= ~0;
5499 if (info
->level
> 0)
5500 map
->map_state
= (info
->state
? IMSM_T_STATE_NORMAL
5501 : IMSM_T_STATE_UNINITIALIZED
);
5503 map
->map_state
= info
->failed_disks
? IMSM_T_STATE_FAILED
:
5504 IMSM_T_STATE_NORMAL
;
5507 if (info
->level
== 1 && info
->raid_disks
> 2) {
5510 pr_err("imsm does not support more than 2 disksin a raid1 volume\n");
5514 map
->raid_level
= info
->level
;
5515 if (info
->level
== 10) {
5516 map
->raid_level
= 1;
5517 map
->num_domains
= info
->raid_disks
/ 2;
5518 } else if (info
->level
== 1)
5519 map
->num_domains
= info
->raid_disks
;
5521 map
->num_domains
= 1;
5523 /* info->size is only int so use the 'size' parameter instead */
5524 num_data_stripes
= size_per_member
/ info_to_blocks_per_strip(info
);
5525 num_data_stripes
/= map
->num_domains
;
5526 set_num_data_stripes(map
, num_data_stripes
);
5528 size_per_member
+= NUM_BLOCKS_DIRTY_STRIPE_REGION
;
5529 set_blocks_per_member(map
, info_to_blocks_per_member(info
,
5533 map
->num_members
= info
->raid_disks
;
5534 for (i
= 0; i
< map
->num_members
; i
++) {
5535 /* initialized in add_to_super */
5536 set_imsm_ord_tbl_ent(map
, i
, IMSM_ORD_REBUILD
);
5538 mpb
->num_raid_devs
++;
5539 mpb
->num_raid_devs_created
++;
5540 dev
->my_vol_raid_dev_num
= mpb
->num_raid_devs_created
;
5542 if (s
->consistency_policy
<= CONSISTENCY_POLICY_RESYNC
) {
5543 dev
->rwh_policy
= RWH_MULTIPLE_OFF
;
5544 } else if (s
->consistency_policy
== CONSISTENCY_POLICY_PPL
) {
5545 dev
->rwh_policy
= RWH_MULTIPLE_DISTRIBUTED
;
5549 pr_err("imsm does not support consistency policy %s\n",
5550 map_num(consistency_policies
, s
->consistency_policy
));
5555 dv
->index
= super
->current_vol
;
5556 dv
->next
= super
->devlist
;
5557 super
->devlist
= dv
;
5559 imsm_update_version_info(super
);
5564 static int init_super_imsm(struct supertype
*st
, mdu_array_info_t
*info
,
5565 struct shape
*s
, char *name
,
5566 char *homehost
, int *uuid
,
5567 unsigned long long data_offset
)
5569 /* This is primarily called by Create when creating a new array.
5570 * We will then get add_to_super called for each component, and then
5571 * write_init_super called to write it out to each device.
5572 * For IMSM, Create can create on fresh devices or on a pre-existing
5574 * To create on a pre-existing array a different method will be called.
5575 * This one is just for fresh drives.
5577 struct intel_super
*super
;
5578 struct imsm_super
*mpb
;
5582 if (data_offset
!= INVALID_SECTORS
) {
5583 pr_err("data-offset not supported by imsm\n");
5588 return init_super_imsm_volume(st
, info
, s
, name
, homehost
, uuid
,
5592 mpb_size
= disks_to_mpb_size(info
->nr_disks
);
5594 mpb_size
= MAX_SECTOR_SIZE
;
5596 super
= alloc_super();
5598 posix_memalign(&super
->buf
, MAX_SECTOR_SIZE
, mpb_size
) != 0) {
5603 pr_err("could not allocate superblock\n");
5606 if (posix_memalign(&super
->migr_rec_buf
, MAX_SECTOR_SIZE
,
5607 MIGR_REC_BUF_SECTORS
*MAX_SECTOR_SIZE
) != 0) {
5608 pr_err("could not allocate migr_rec buffer\n");
5613 memset(super
->buf
, 0, mpb_size
);
5615 mpb
->mpb_size
= __cpu_to_le32(mpb_size
);
5619 /* zeroing superblock */
5623 mpb
->attributes
= MPB_ATTRIB_CHECKSUM_VERIFY
;
5625 version
= (char *) mpb
->sig
;
5626 strcpy(version
, MPB_SIGNATURE
);
5627 version
+= strlen(MPB_SIGNATURE
);
5628 strcpy(version
, MPB_VERSION_RAID0
);
5633 static int drive_validate_sector_size(struct intel_super
*super
, struct dl
*dl
)
5635 unsigned int member_sector_size
;
5638 pr_err("Invalid file descriptor for %s\n", dl
->devname
);
5642 if (!get_dev_sector_size(dl
->fd
, dl
->devname
, &member_sector_size
))
5644 if (member_sector_size
!= super
->sector_size
)
5649 static int add_to_super_imsm_volume(struct supertype
*st
, mdu_disk_info_t
*dk
,
5650 int fd
, char *devname
)
5652 struct intel_super
*super
= st
->sb
;
5653 struct imsm_super
*mpb
= super
->anchor
;
5654 struct imsm_disk
*_disk
;
5655 struct imsm_dev
*dev
;
5656 struct imsm_map
*map
;
5660 dev
= get_imsm_dev(super
, super
->current_vol
);
5661 map
= get_imsm_map(dev
, MAP_0
);
5663 if (! (dk
->state
& (1<<MD_DISK_SYNC
))) {
5664 pr_err("%s: Cannot add spare devices to IMSM volume\n",
5670 /* we're doing autolayout so grab the pre-marked (in
5671 * validate_geometry) raid_disk
5673 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
5674 if (dl
->raiddisk
== dk
->raid_disk
)
5677 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
5678 if (dl
->major
== dk
->major
&&
5679 dl
->minor
== dk
->minor
)
5684 pr_err("%s is not a member of the same container\n", devname
);
5688 if (mpb
->num_disks
== 0)
5689 if (!get_dev_sector_size(dl
->fd
, dl
->devname
,
5690 &super
->sector_size
))
5693 if (!drive_validate_sector_size(super
, dl
)) {
5694 pr_err("Combining drives of different sector size in one volume is not allowed\n");
5698 /* add a pristine spare to the metadata */
5699 if (dl
->index
< 0) {
5700 dl
->index
= super
->anchor
->num_disks
;
5701 super
->anchor
->num_disks
++;
5703 /* Check the device has not already been added */
5704 slot
= get_imsm_disk_slot(map
, dl
->index
);
5706 (get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
) & IMSM_ORD_REBUILD
) == 0) {
5707 pr_err("%s has been included in this array twice\n",
5711 set_imsm_ord_tbl_ent(map
, dk
->raid_disk
, dl
->index
);
5712 dl
->disk
.status
= CONFIGURED_DISK
;
5714 /* update size of 'missing' disks to be at least as large as the
5715 * largest acitve member (we only have dummy missing disks when
5716 * creating the first volume)
5718 if (super
->current_vol
== 0) {
5719 for (df
= super
->missing
; df
; df
= df
->next
) {
5720 if (total_blocks(&dl
->disk
) > total_blocks(&df
->disk
))
5721 set_total_blocks(&df
->disk
, total_blocks(&dl
->disk
));
5722 _disk
= __get_imsm_disk(mpb
, df
->index
);
5727 /* refresh unset/failed slots to point to valid 'missing' entries */
5728 for (df
= super
->missing
; df
; df
= df
->next
)
5729 for (slot
= 0; slot
< mpb
->num_disks
; slot
++) {
5730 __u32 ord
= get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
);
5732 if ((ord
& IMSM_ORD_REBUILD
) == 0)
5734 set_imsm_ord_tbl_ent(map
, slot
, df
->index
| IMSM_ORD_REBUILD
);
5735 if (is_gen_migration(dev
)) {
5736 struct imsm_map
*map2
= get_imsm_map(dev
,
5738 int slot2
= get_imsm_disk_slot(map2
, df
->index
);
5739 if (slot2
< map2
->num_members
&& slot2
>= 0) {
5740 __u32 ord2
= get_imsm_ord_tbl_ent(dev
,
5743 if ((unsigned)df
->index
==
5745 set_imsm_ord_tbl_ent(map2
,
5751 dprintf("set slot:%d to missing disk:%d\n", slot
, df
->index
);
5755 /* if we are creating the first raid device update the family number */
5756 if (super
->current_vol
== 0) {
5758 struct imsm_dev
*_dev
= __get_imsm_dev(mpb
, 0);
5760 _disk
= __get_imsm_disk(mpb
, dl
->index
);
5761 if (!_dev
|| !_disk
) {
5762 pr_err("BUG mpb setup error\n");
5768 sum
+= __gen_imsm_checksum(mpb
);
5769 mpb
->family_num
= __cpu_to_le32(sum
);
5770 mpb
->orig_family_num
= mpb
->family_num
;
5771 mpb
->creation_time
= __cpu_to_le64((__u64
)time(NULL
));
5773 super
->current_disk
= dl
;
5778 * Function marks disk as spare and restores disk serial
5779 * in case it was previously marked as failed by takeover operation
5781 * -1 : critical error
5782 * 0 : disk is marked as spare but serial is not set
5785 int mark_spare(struct dl
*disk
)
5787 __u8 serial
[MAX_RAID_SERIAL_LEN
];
5794 if (!imsm_read_serial(disk
->fd
, NULL
, serial
, MAX_RAID_SERIAL_LEN
)) {
5795 /* Restore disk serial number, because takeover marks disk
5796 * as failed and adds to serial ':0' before it becomes
5799 serialcpy(disk
->serial
, serial
);
5800 serialcpy(disk
->disk
.serial
, serial
);
5803 disk
->disk
.status
= SPARE_DISK
;
5809 static int add_to_super_imsm(struct supertype
*st
, mdu_disk_info_t
*dk
,
5810 int fd
, char *devname
,
5811 unsigned long long data_offset
)
5813 struct intel_super
*super
= st
->sb
;
5815 unsigned long long size
;
5816 unsigned int member_sector_size
;
5821 /* If we are on an RAID enabled platform check that the disk is
5822 * attached to the raid controller.
5823 * We do not need to test disks attachment for container based additions,
5824 * they shall be already tested when container was created/assembled.
5826 rv
= find_intel_hba_capability(fd
, super
, devname
);
5827 /* no orom/efi or non-intel hba of the disk */
5829 dprintf("capability: %p fd: %d ret: %d\n",
5830 super
->orom
, fd
, rv
);
5834 if (super
->current_vol
>= 0)
5835 return add_to_super_imsm_volume(st
, dk
, fd
, devname
);
5838 dd
= xcalloc(sizeof(*dd
), 1);
5839 dd
->major
= major(stb
.st_rdev
);
5840 dd
->minor
= minor(stb
.st_rdev
);
5841 dd
->devname
= devname
? xstrdup(devname
) : NULL
;
5844 dd
->action
= DISK_ADD
;
5845 rv
= imsm_read_serial(fd
, devname
, dd
->serial
, MAX_RAID_SERIAL_LEN
);
5847 pr_err("failed to retrieve scsi serial, aborting\n");
5853 if (super
->hba
&& ((super
->hba
->type
== SYS_DEV_NVME
) ||
5854 (super
->hba
->type
== SYS_DEV_VMD
))) {
5856 char *devpath
= diskfd_to_devpath(fd
);
5857 char controller_path
[PATH_MAX
];
5860 pr_err("failed to get devpath, aborting\n");
5867 snprintf(controller_path
, PATH_MAX
-1, "%s/device", devpath
);
5870 if (devpath_to_vendor(controller_path
) == 0x8086) {
5872 * If Intel's NVMe drive has serial ended with
5873 * "-A","-B","-1" or "-2" it means that this is "x8"
5874 * device (double drive on single PCIe card).
5875 * User should be warned about potential data loss.
5877 for (i
= MAX_RAID_SERIAL_LEN
-1; i
> 0; i
--) {
5878 /* Skip empty character at the end */
5879 if (dd
->serial
[i
] == 0)
5882 if (((dd
->serial
[i
] == 'A') ||
5883 (dd
->serial
[i
] == 'B') ||
5884 (dd
->serial
[i
] == '1') ||
5885 (dd
->serial
[i
] == '2')) &&
5886 (dd
->serial
[i
-1] == '-'))
5887 pr_err("\tThe action you are about to take may put your data at risk.\n"
5888 "\tPlease note that x8 devices may consist of two separate x4 devices "
5889 "located on a single PCIe port.\n"
5890 "\tRAID 0 is the only supported configuration for this type of x8 device.\n");
5893 } else if (super
->hba
->type
== SYS_DEV_VMD
&& super
->orom
&&
5894 !imsm_orom_has_tpv_support(super
->orom
)) {
5895 pr_err("\tPlatform configuration does not support non-Intel NVMe drives.\n"
5896 "\tPlease refer to Intel(R) RSTe/VROC user guide.\n");
5903 get_dev_size(fd
, NULL
, &size
);
5904 get_dev_sector_size(fd
, NULL
, &member_sector_size
);
5906 if (super
->sector_size
== 0) {
5907 /* this a first device, so sector_size is not set yet */
5908 super
->sector_size
= member_sector_size
;
5911 /* clear migr_rec when adding disk to container */
5912 memset(super
->migr_rec_buf
, 0, MIGR_REC_BUF_SECTORS
*MAX_SECTOR_SIZE
);
5913 if (lseek64(fd
, size
- MIGR_REC_SECTOR_POSITION
*member_sector_size
,
5915 if ((unsigned int)write(fd
, super
->migr_rec_buf
,
5916 MIGR_REC_BUF_SECTORS
*member_sector_size
) !=
5917 MIGR_REC_BUF_SECTORS
*member_sector_size
)
5918 perror("Write migr_rec failed");
5922 serialcpy(dd
->disk
.serial
, dd
->serial
);
5923 set_total_blocks(&dd
->disk
, size
);
5924 if (__le32_to_cpu(dd
->disk
.total_blocks_hi
) > 0) {
5925 struct imsm_super
*mpb
= super
->anchor
;
5926 mpb
->attributes
|= MPB_ATTRIB_2TB_DISK
;
5929 if (sysfs_disk_to_scsi_id(fd
, &id
) == 0)
5930 dd
->disk
.scsi_id
= __cpu_to_le32(id
);
5932 dd
->disk
.scsi_id
= __cpu_to_le32(0);
5934 if (st
->update_tail
) {
5935 dd
->next
= super
->disk_mgmt_list
;
5936 super
->disk_mgmt_list
= dd
;
5938 dd
->next
= super
->disks
;
5940 super
->updates_pending
++;
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
)));
5979 /* spare records have their own family number and do not have any defined raid
5982 static int write_super_imsm_spares(struct intel_super
*super
, int doclose
)
5984 struct imsm_super
*mpb
= super
->anchor
;
5985 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 for (d
= super
->disks
; d
; d
= d
->next
) {
6004 spare
->disk
[0] = d
->disk
;
6005 if (__le32_to_cpu(d
->disk
.total_blocks_hi
) > 0)
6006 spare
->attributes
|= MPB_ATTRIB_2TB_DISK
;
6008 if (super
->sector_size
== 4096)
6009 convert_to_4k_imsm_disk(&spare
->disk
[0]);
6011 sum
= __gen_imsm_checksum(spare
);
6012 spare
->family_num
= __cpu_to_le32(sum
);
6013 spare
->orig_family_num
= 0;
6014 sum
= __gen_imsm_checksum(spare
);
6015 spare
->check_sum
= __cpu_to_le32(sum
);
6017 if (store_imsm_mpb(d
->fd
, spare
)) {
6018 pr_err("failed for device %d:%d %s\n",
6019 d
->major
, d
->minor
, strerror(errno
));
6031 static int write_super_imsm(struct supertype
*st
, int doclose
)
6033 struct intel_super
*super
= st
->sb
;
6034 unsigned int sector_size
= super
->sector_size
;
6035 struct imsm_super
*mpb
= super
->anchor
;
6041 __u32 mpb_size
= sizeof(struct imsm_super
) - sizeof(struct imsm_disk
);
6043 int clear_migration_record
= 1;
6046 /* 'generation' is incremented everytime the metadata is written */
6047 generation
= __le32_to_cpu(mpb
->generation_num
);
6049 mpb
->generation_num
= __cpu_to_le32(generation
);
6051 /* fix up cases where previous mdadm releases failed to set
6054 if (mpb
->orig_family_num
== 0)
6055 mpb
->orig_family_num
= mpb
->family_num
;
6057 for (d
= super
->disks
; d
; d
= d
->next
) {
6061 mpb
->disk
[d
->index
] = d
->disk
;
6065 for (d
= super
->missing
; d
; d
= d
->next
) {
6066 mpb
->disk
[d
->index
] = d
->disk
;
6069 mpb
->num_disks
= num_disks
;
6070 mpb_size
+= sizeof(struct imsm_disk
) * mpb
->num_disks
;
6072 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
6073 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
6074 struct imsm_dev
*dev2
= get_imsm_dev(super
, i
);
6076 imsm_copy_dev(dev
, dev2
);
6077 mpb_size
+= sizeof_imsm_dev(dev
, 0);
6079 if (is_gen_migration(dev2
))
6080 clear_migration_record
= 0;
6083 bbm_log_size
= get_imsm_bbm_log_size(super
->bbm_log
);
6086 memcpy((void *)mpb
+ mpb_size
, super
->bbm_log
, bbm_log_size
);
6087 mpb
->attributes
|= MPB_ATTRIB_BBM
;
6089 mpb
->attributes
&= ~MPB_ATTRIB_BBM
;
6091 super
->anchor
->bbm_log_size
= __cpu_to_le32(bbm_log_size
);
6092 mpb_size
+= bbm_log_size
;
6093 mpb
->mpb_size
= __cpu_to_le32(mpb_size
);
6096 assert(super
->len
== 0 || mpb_size
<= super
->len
);
6099 /* recalculate checksum */
6100 sum
= __gen_imsm_checksum(mpb
);
6101 mpb
->check_sum
= __cpu_to_le32(sum
);
6103 if (super
->clean_migration_record_by_mdmon
) {
6104 clear_migration_record
= 1;
6105 super
->clean_migration_record_by_mdmon
= 0;
6107 if (clear_migration_record
)
6108 memset(super
->migr_rec_buf
, 0,
6109 MIGR_REC_BUF_SECTORS
*MAX_SECTOR_SIZE
);
6111 if (sector_size
== 4096)
6112 convert_to_4k(super
);
6114 /* write the mpb for disks that compose raid devices */
6115 for (d
= super
->disks
; d
; d
= d
->next
) {
6116 if (d
->index
< 0 || is_failed(&d
->disk
))
6119 if (clear_migration_record
) {
6120 unsigned long long dsize
;
6122 get_dev_size(d
->fd
, NULL
, &dsize
);
6123 if (lseek64(d
->fd
, dsize
- sector_size
,
6125 if ((unsigned int)write(d
->fd
,
6126 super
->migr_rec_buf
,
6127 MIGR_REC_BUF_SECTORS
*sector_size
) !=
6128 MIGR_REC_BUF_SECTORS
*sector_size
)
6129 perror("Write migr_rec failed");
6133 if (store_imsm_mpb(d
->fd
, mpb
))
6135 "failed for device %d:%d (fd: %d)%s\n",
6137 d
->fd
, strerror(errno
));
6146 return write_super_imsm_spares(super
, doclose
);
6151 static int create_array(struct supertype
*st
, int dev_idx
)
6154 struct imsm_update_create_array
*u
;
6155 struct intel_super
*super
= st
->sb
;
6156 struct imsm_dev
*dev
= get_imsm_dev(super
, dev_idx
);
6157 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
6158 struct disk_info
*inf
;
6159 struct imsm_disk
*disk
;
6162 len
= sizeof(*u
) - sizeof(*dev
) + sizeof_imsm_dev(dev
, 0) +
6163 sizeof(*inf
) * map
->num_members
;
6165 u
->type
= update_create_array
;
6166 u
->dev_idx
= dev_idx
;
6167 imsm_copy_dev(&u
->dev
, dev
);
6168 inf
= get_disk_info(u
);
6169 for (i
= 0; i
< map
->num_members
; i
++) {
6170 int idx
= get_imsm_disk_idx(dev
, i
, MAP_X
);
6172 disk
= get_imsm_disk(super
, idx
);
6174 disk
= get_imsm_missing(super
, idx
);
6175 serialcpy(inf
[i
].serial
, disk
->serial
);
6177 append_metadata_update(st
, u
, len
);
6182 static int mgmt_disk(struct supertype
*st
)
6184 struct intel_super
*super
= st
->sb
;
6186 struct imsm_update_add_remove_disk
*u
;
6188 if (!super
->disk_mgmt_list
)
6193 u
->type
= update_add_remove_disk
;
6194 append_metadata_update(st
, u
, len
);
6199 __u32
crc32c_le(__u32 crc
, unsigned char const *p
, size_t len
);
6201 static int write_ppl_header(unsigned long long ppl_sector
, int fd
, void *buf
)
6203 struct ppl_header
*ppl_hdr
= buf
;
6206 ppl_hdr
->checksum
= __cpu_to_le32(~crc32c_le(~0, buf
, PPL_HEADER_SIZE
));
6208 if (lseek64(fd
, ppl_sector
* 512, SEEK_SET
) < 0) {
6210 perror("Failed to seek to PPL header location");
6214 if (write(fd
, buf
, PPL_HEADER_SIZE
) != PPL_HEADER_SIZE
) {
6216 perror("Write PPL header failed");
6225 static int write_init_ppl_imsm(struct supertype
*st
, struct mdinfo
*info
, int fd
)
6227 struct intel_super
*super
= st
->sb
;
6229 struct ppl_header
*ppl_hdr
;
6232 /* first clear entire ppl space */
6233 ret
= zero_disk_range(fd
, info
->ppl_sector
, info
->ppl_size
);
6237 ret
= posix_memalign(&buf
, MAX_SECTOR_SIZE
, PPL_HEADER_SIZE
);
6239 pr_err("Failed to allocate PPL header buffer\n");
6243 memset(buf
, 0, PPL_HEADER_SIZE
);
6245 memset(ppl_hdr
->reserved
, 0xff, PPL_HDR_RESERVED
);
6246 ppl_hdr
->signature
= __cpu_to_le32(super
->anchor
->orig_family_num
);
6248 if (info
->mismatch_cnt
) {
6250 * We are overwriting an invalid ppl. Make one entry with wrong
6251 * checksum to prevent the kernel from skipping resync.
6253 ppl_hdr
->entries_count
= __cpu_to_le32(1);
6254 ppl_hdr
->entries
[0].checksum
= ~0;
6257 ret
= write_ppl_header(info
->ppl_sector
, fd
, buf
);
6263 static int is_rebuilding(struct imsm_dev
*dev
);
6265 static int validate_ppl_imsm(struct supertype
*st
, struct mdinfo
*info
,
6266 struct mdinfo
*disk
)
6268 struct intel_super
*super
= st
->sb
;
6270 void *buf_orig
, *buf
, *buf_prev
= NULL
;
6272 struct ppl_header
*ppl_hdr
= NULL
;
6274 struct imsm_dev
*dev
;
6277 unsigned long long ppl_offset
= 0;
6278 unsigned long long prev_gen_num
= 0;
6280 if (disk
->disk
.raid_disk
< 0)
6283 dev
= get_imsm_dev(super
, info
->container_member
);
6284 idx
= get_imsm_disk_idx(dev
, disk
->disk
.raid_disk
, MAP_0
);
6285 d
= get_imsm_dl_disk(super
, idx
);
6287 if (!d
|| d
->index
< 0 || is_failed(&d
->disk
))
6290 if (posix_memalign(&buf_orig
, MAX_SECTOR_SIZE
, PPL_HEADER_SIZE
* 2)) {
6291 pr_err("Failed to allocate PPL header buffer\n");
6297 while (ppl_offset
< MULTIPLE_PPL_AREA_SIZE_IMSM
) {
6300 dprintf("Checking potential PPL at offset: %llu\n", ppl_offset
);
6302 if (lseek64(d
->fd
, info
->ppl_sector
* 512 + ppl_offset
,
6304 perror("Failed to seek to PPL header location");
6309 if (read(d
->fd
, buf
, PPL_HEADER_SIZE
) != PPL_HEADER_SIZE
) {
6310 perror("Read PPL header failed");
6317 crc
= __le32_to_cpu(ppl_hdr
->checksum
);
6318 ppl_hdr
->checksum
= 0;
6320 if (crc
!= ~crc32c_le(~0, buf
, PPL_HEADER_SIZE
)) {
6321 dprintf("Wrong PPL header checksum on %s\n",
6326 if (prev_gen_num
> __le64_to_cpu(ppl_hdr
->generation
)) {
6327 /* previous was newest, it was already checked */
6331 if ((__le32_to_cpu(ppl_hdr
->signature
) !=
6332 super
->anchor
->orig_family_num
)) {
6333 dprintf("Wrong PPL header signature on %s\n",
6340 prev_gen_num
= __le64_to_cpu(ppl_hdr
->generation
);
6342 ppl_offset
+= PPL_HEADER_SIZE
;
6343 for (i
= 0; i
< __le32_to_cpu(ppl_hdr
->entries_count
); i
++)
6345 __le32_to_cpu(ppl_hdr
->entries
[i
].pp_size
);
6348 buf_prev
= buf
+ PPL_HEADER_SIZE
;
6360 * Update metadata to use mutliple PPLs area (1MB).
6361 * This is done once for all RAID members
6363 if (info
->consistency_policy
== CONSISTENCY_POLICY_PPL
&&
6364 info
->ppl_size
!= (MULTIPLE_PPL_AREA_SIZE_IMSM
>> 9)) {
6366 struct mdinfo
*member_dev
;
6368 sprintf(subarray
, "%d", info
->container_member
);
6370 if (mdmon_running(st
->container_devnm
))
6371 st
->update_tail
= &st
->updates
;
6373 if (st
->ss
->update_subarray(st
, subarray
, "ppl", NULL
)) {
6374 pr_err("Failed to update subarray %s\n",
6377 if (st
->update_tail
)
6378 flush_metadata_updates(st
);
6380 st
->ss
->sync_metadata(st
);
6381 info
->ppl_size
= (MULTIPLE_PPL_AREA_SIZE_IMSM
>> 9);
6382 for (member_dev
= info
->devs
; member_dev
;
6383 member_dev
= member_dev
->next
)
6384 member_dev
->ppl_size
=
6385 (MULTIPLE_PPL_AREA_SIZE_IMSM
>> 9);
6390 struct imsm_map
*map
= get_imsm_map(dev
, MAP_X
);
6392 if (map
->map_state
== IMSM_T_STATE_UNINITIALIZED
||
6393 (map
->map_state
== IMSM_T_STATE_NORMAL
&&
6394 !(dev
->vol
.dirty
& RAIDVOL_DIRTY
)) ||
6395 (is_rebuilding(dev
) &&
6396 dev
->vol
.curr_migr_unit
== 0 &&
6397 get_imsm_disk_idx(dev
, disk
->disk
.raid_disk
, MAP_1
) != idx
))
6398 ret
= st
->ss
->write_init_ppl(st
, info
, d
->fd
);
6400 info
->mismatch_cnt
++;
6401 } else if (ret
== 0 &&
6402 ppl_hdr
->entries_count
== 0 &&
6403 is_rebuilding(dev
) &&
6404 info
->resync_start
== 0) {
6406 * The header has no entries - add a single empty entry and
6407 * rewrite the header to prevent the kernel from going into
6408 * resync after an interrupted rebuild.
6410 ppl_hdr
->entries_count
= __cpu_to_le32(1);
6411 ret
= write_ppl_header(info
->ppl_sector
, d
->fd
, buf
);
6419 static int write_init_ppl_imsm_all(struct supertype
*st
, struct mdinfo
*info
)
6421 struct intel_super
*super
= st
->sb
;
6425 if (info
->consistency_policy
!= CONSISTENCY_POLICY_PPL
||
6426 info
->array
.level
!= 5)
6429 for (d
= super
->disks
; d
; d
= d
->next
) {
6430 if (d
->index
< 0 || is_failed(&d
->disk
))
6433 ret
= st
->ss
->write_init_ppl(st
, info
, d
->fd
);
6441 static int write_init_super_imsm(struct supertype
*st
)
6443 struct intel_super
*super
= st
->sb
;
6444 int current_vol
= super
->current_vol
;
6448 getinfo_super_imsm(st
, &info
, NULL
);
6450 /* we are done with current_vol reset it to point st at the container */
6451 super
->current_vol
= -1;
6453 if (st
->update_tail
) {
6454 /* queue the recently created array / added disk
6455 * as a metadata update */
6457 /* determine if we are creating a volume or adding a disk */
6458 if (current_vol
< 0) {
6459 /* in the mgmt (add/remove) disk case we are running
6460 * in mdmon context, so don't close fd's
6464 rv
= write_init_ppl_imsm_all(st
, &info
);
6466 rv
= create_array(st
, current_vol
);
6470 for (d
= super
->disks
; d
; d
= d
->next
)
6471 Kill(d
->devname
, NULL
, 0, -1, 1);
6472 if (current_vol
>= 0)
6473 rv
= write_init_ppl_imsm_all(st
, &info
);
6475 rv
= write_super_imsm(st
, 1);
6481 static int store_super_imsm(struct supertype
*st
, int fd
)
6483 struct intel_super
*super
= st
->sb
;
6484 struct imsm_super
*mpb
= super
? super
->anchor
: NULL
;
6489 if (super
->sector_size
== 4096)
6490 convert_to_4k(super
);
6491 return store_imsm_mpb(fd
, mpb
);
6494 static int validate_geometry_imsm_container(struct supertype
*st
, int level
,
6495 int layout
, int raiddisks
, int chunk
,
6496 unsigned long long size
,
6497 unsigned long long data_offset
,
6499 unsigned long long *freesize
,
6503 unsigned long long ldsize
;
6504 struct intel_super
*super
;
6507 if (level
!= LEVEL_CONTAINER
)
6512 fd
= open(dev
, O_RDONLY
|O_EXCL
, 0);
6515 pr_err("imsm: Cannot open %s: %s\n",
6516 dev
, strerror(errno
));
6519 if (!get_dev_size(fd
, dev
, &ldsize
)) {
6524 /* capabilities retrieve could be possible
6525 * note that there is no fd for the disks in array.
6527 super
= alloc_super();
6532 if (!get_dev_sector_size(fd
, NULL
, &super
->sector_size
)) {
6538 rv
= find_intel_hba_capability(fd
, super
, verbose
> 0 ? dev
: NULL
);
6542 fd2devname(fd
, str
);
6543 dprintf("fd: %d %s orom: %p rv: %d raiddisk: %d\n",
6544 fd
, str
, super
->orom
, rv
, raiddisks
);
6546 /* no orom/efi or non-intel hba of the disk */
6553 if (raiddisks
> super
->orom
->tds
) {
6555 pr_err("%d exceeds maximum number of platform supported disks: %d\n",
6556 raiddisks
, super
->orom
->tds
);
6560 if ((super
->orom
->attr
& IMSM_OROM_ATTR_2TB_DISK
) == 0 &&
6561 (ldsize
>> 9) >> 32 > 0) {
6563 pr_err("%s exceeds maximum platform supported size\n", dev
);
6569 *freesize
= avail_size_imsm(st
, ldsize
>> 9, data_offset
);
6575 static unsigned long long find_size(struct extent
*e
, int *idx
, int num_extents
)
6577 const unsigned long long base_start
= e
[*idx
].start
;
6578 unsigned long long end
= base_start
+ e
[*idx
].size
;
6581 if (base_start
== end
)
6585 for (i
= *idx
; i
< num_extents
; i
++) {
6586 /* extend overlapping extents */
6587 if (e
[i
].start
>= base_start
&&
6588 e
[i
].start
<= end
) {
6591 if (e
[i
].start
+ e
[i
].size
> end
)
6592 end
= e
[i
].start
+ e
[i
].size
;
6593 } else if (e
[i
].start
> end
) {
6599 return end
- base_start
;
6602 static unsigned long long merge_extents(struct intel_super
*super
, int sum_extents
)
6604 /* build a composite disk with all known extents and generate a new
6605 * 'maxsize' given the "all disks in an array must share a common start
6606 * offset" constraint
6608 struct extent
*e
= xcalloc(sum_extents
, sizeof(*e
));
6612 unsigned long long pos
;
6613 unsigned long long start
= 0;
6614 unsigned long long maxsize
;
6615 unsigned long reserve
;
6617 /* coalesce and sort all extents. also, check to see if we need to
6618 * reserve space between member arrays
6621 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
6624 for (i
= 0; i
< dl
->extent_cnt
; i
++)
6627 qsort(e
, sum_extents
, sizeof(*e
), cmp_extent
);
6632 while (i
< sum_extents
) {
6633 e
[j
].start
= e
[i
].start
;
6634 e
[j
].size
= find_size(e
, &i
, sum_extents
);
6636 if (e
[j
-1].size
== 0)
6645 unsigned long long esize
;
6647 esize
= e
[i
].start
- pos
;
6648 if (esize
>= maxsize
) {
6653 pos
= e
[i
].start
+ e
[i
].size
;
6655 } while (e
[i
-1].size
);
6661 /* FIXME assumes volume at offset 0 is the first volume in a
6664 if (start_extent
> 0)
6665 reserve
= IMSM_RESERVED_SECTORS
; /* gap between raid regions */
6669 if (maxsize
< reserve
)
6672 super
->create_offset
= ~((unsigned long long) 0);
6673 if (start
+ reserve
> super
->create_offset
)
6674 return 0; /* start overflows create_offset */
6675 super
->create_offset
= start
+ reserve
;
6677 return maxsize
- reserve
;
6680 static int is_raid_level_supported(const struct imsm_orom
*orom
, int level
, int raiddisks
)
6682 if (level
< 0 || level
== 6 || level
== 4)
6685 /* if we have an orom prevent invalid raid levels */
6688 case 0: return imsm_orom_has_raid0(orom
);
6691 return imsm_orom_has_raid1e(orom
);
6692 return imsm_orom_has_raid1(orom
) && raiddisks
== 2;
6693 case 10: return imsm_orom_has_raid10(orom
) && raiddisks
== 4;
6694 case 5: return imsm_orom_has_raid5(orom
) && raiddisks
> 2;
6697 return 1; /* not on an Intel RAID platform so anything goes */
6703 active_arrays_by_format(char *name
, char* hba
, struct md_list
**devlist
,
6704 int dpa
, int verbose
)
6706 struct mdstat_ent
*mdstat
= mdstat_read(0, 0);
6707 struct mdstat_ent
*memb
;
6713 for (memb
= mdstat
; memb
; memb
= memb
->next
) {
6714 if (memb
->metadata_version
&&
6715 (strncmp(memb
->metadata_version
, "external:", 9) == 0) &&
6716 (strcmp(&memb
->metadata_version
[9], name
) == 0) &&
6717 !is_subarray(memb
->metadata_version
+9) &&
6719 struct dev_member
*dev
= memb
->members
;
6721 while(dev
&& (fd
< 0)) {
6722 char *path
= xmalloc(strlen(dev
->name
) + strlen("/dev/") + 1);
6723 num
= sprintf(path
, "%s%s", "/dev/", dev
->name
);
6725 fd
= open(path
, O_RDONLY
, 0);
6726 if (num
<= 0 || fd
< 0) {
6727 pr_vrb("Cannot open %s: %s\n",
6728 dev
->name
, strerror(errno
));
6734 if (fd
>= 0 && disk_attached_to_hba(fd
, hba
)) {
6735 struct mdstat_ent
*vol
;
6736 for (vol
= mdstat
; vol
; vol
= vol
->next
) {
6737 if (vol
->active
> 0 &&
6738 vol
->metadata_version
&&
6739 is_container_member(vol
, memb
->devnm
)) {
6744 if (*devlist
&& (found
< dpa
)) {
6745 dv
= xcalloc(1, sizeof(*dv
));
6746 dv
->devname
= xmalloc(strlen(memb
->devnm
) + strlen("/dev/") + 1);
6747 sprintf(dv
->devname
, "%s%s", "/dev/", memb
->devnm
);
6750 dv
->next
= *devlist
;
6758 free_mdstat(mdstat
);
6763 static struct md_list
*
6764 get_loop_devices(void)
6767 struct md_list
*devlist
= NULL
;
6770 for(i
= 0; i
< 12; i
++) {
6771 dv
= xcalloc(1, sizeof(*dv
));
6772 dv
->devname
= xmalloc(40);
6773 sprintf(dv
->devname
, "/dev/loop%d", i
);
6781 static struct md_list
*
6782 get_devices(const char *hba_path
)
6784 struct md_list
*devlist
= NULL
;
6791 devlist
= get_loop_devices();
6794 /* scroll through /sys/dev/block looking for devices attached to
6797 dir
= opendir("/sys/dev/block");
6798 for (ent
= dir
? readdir(dir
) : NULL
; ent
; ent
= readdir(dir
)) {
6803 if (sscanf(ent
->d_name
, "%d:%d", &major
, &minor
) != 2)
6805 path
= devt_to_devpath(makedev(major
, minor
));
6808 if (!path_attached_to_hba(path
, hba_path
)) {
6815 fd
= dev_open(ent
->d_name
, O_RDONLY
);
6817 fd2devname(fd
, buf
);
6820 pr_err("cannot open device: %s\n",
6825 dv
= xcalloc(1, sizeof(*dv
));
6826 dv
->devname
= xstrdup(buf
);
6833 devlist
= devlist
->next
;
6843 count_volumes_list(struct md_list
*devlist
, char *homehost
,
6844 int verbose
, int *found
)
6846 struct md_list
*tmpdev
;
6848 struct supertype
*st
;
6850 /* first walk the list of devices to find a consistent set
6851 * that match the criterea, if that is possible.
6852 * We flag the ones we like with 'used'.
6855 st
= match_metadata_desc_imsm("imsm");
6857 pr_vrb("cannot allocate memory for imsm supertype\n");
6861 for (tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
6862 char *devname
= tmpdev
->devname
;
6864 struct supertype
*tst
;
6866 if (tmpdev
->used
> 1)
6868 tst
= dup_super(st
);
6870 pr_vrb("cannot allocate memory for imsm supertype\n");
6873 tmpdev
->container
= 0;
6874 dfd
= dev_open(devname
, O_RDONLY
|O_EXCL
);
6876 dprintf("cannot open device %s: %s\n",
6877 devname
, strerror(errno
));
6879 } else if (!fstat_is_blkdev(dfd
, devname
, &rdev
)) {
6881 } else if (must_be_container(dfd
)) {
6882 struct supertype
*cst
;
6883 cst
= super_by_fd(dfd
, NULL
);
6885 dprintf("cannot recognize container type %s\n",
6888 } else if (tst
->ss
!= st
->ss
) {
6889 dprintf("non-imsm container - ignore it: %s\n",
6892 } else if (!tst
->ss
->load_container
||
6893 tst
->ss
->load_container(tst
, dfd
, NULL
))
6896 tmpdev
->container
= 1;
6899 cst
->ss
->free_super(cst
);
6901 tmpdev
->st_rdev
= rdev
;
6902 if (tst
->ss
->load_super(tst
,dfd
, NULL
)) {
6903 dprintf("no RAID superblock on %s\n",
6906 } else if (tst
->ss
->compare_super
== NULL
) {
6907 dprintf("Cannot assemble %s metadata on %s\n",
6908 tst
->ss
->name
, devname
);
6914 if (tmpdev
->used
== 2 || tmpdev
->used
== 4) {
6915 /* Ignore unrecognised devices during auto-assembly */
6920 tst
->ss
->getinfo_super(tst
, &info
, NULL
);
6922 if (st
->minor_version
== -1)
6923 st
->minor_version
= tst
->minor_version
;
6925 if (memcmp(info
.uuid
, uuid_zero
,
6926 sizeof(int[4])) == 0) {
6927 /* this is a floating spare. It cannot define
6928 * an array unless there are no more arrays of
6929 * this type to be found. It can be included
6930 * in an array of this type though.
6936 if (st
->ss
!= tst
->ss
||
6937 st
->minor_version
!= tst
->minor_version
||
6938 st
->ss
->compare_super(st
, tst
) != 0) {
6939 /* Some mismatch. If exactly one array matches this host,
6940 * we can resolve on that one.
6941 * Or, if we are auto assembling, we just ignore the second
6944 dprintf("superblock on %s doesn't match others - assembly aborted\n",
6950 dprintf("found: devname: %s\n", devname
);
6954 tst
->ss
->free_super(tst
);
6958 if ((err
= load_super_imsm_all(st
, -1, &st
->sb
, NULL
, devlist
, 0)) == 0) {
6959 struct mdinfo
*iter
, *head
= st
->ss
->container_content(st
, NULL
);
6960 for (iter
= head
; iter
; iter
= iter
->next
) {
6961 dprintf("content->text_version: %s vol\n",
6962 iter
->text_version
);
6963 if (iter
->array
.state
& (1<<MD_SB_BLOCK_VOLUME
)) {
6964 /* do not assemble arrays with unsupported
6966 dprintf("Cannot activate member %s.\n",
6967 iter
->text_version
);
6974 dprintf("No valid super block on device list: err: %d %p\n",
6978 dprintf("no more devices to examine\n");
6981 for (tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
6982 if (tmpdev
->used
== 1 && tmpdev
->found
) {
6984 if (count
< tmpdev
->found
)
6987 count
-= tmpdev
->found
;
6990 if (tmpdev
->used
== 1)
6995 st
->ss
->free_super(st
);
6999 static int __count_volumes(char *hba_path
, int dpa
, int verbose
,
7002 struct sys_dev
*idev
, *intel_devices
= find_intel_devices();
7004 const struct orom_entry
*entry
;
7005 struct devid_list
*dv
, *devid_list
;
7010 for (idev
= intel_devices
; idev
; idev
= idev
->next
) {
7011 if (strstr(idev
->path
, hba_path
))
7015 if (!idev
|| !idev
->dev_id
)
7018 entry
= get_orom_entry_by_device_id(idev
->dev_id
);
7020 if (!entry
|| !entry
->devid_list
)
7023 devid_list
= entry
->devid_list
;
7024 for (dv
= devid_list
; dv
; dv
= dv
->next
) {
7025 struct md_list
*devlist
;
7026 struct sys_dev
*device
= NULL
;
7031 device
= device_by_id_and_path(dv
->devid
, hba_path
);
7033 device
= device_by_id(dv
->devid
);
7036 hpath
= device
->path
;
7040 devlist
= get_devices(hpath
);
7041 /* if no intel devices return zero volumes */
7042 if (devlist
== NULL
)
7045 count
+= active_arrays_by_format("imsm", hpath
, &devlist
, dpa
,
7047 dprintf("path: %s active arrays: %d\n", hpath
, count
);
7048 if (devlist
== NULL
)
7052 count
+= count_volumes_list(devlist
,
7056 dprintf("found %d count: %d\n", found
, count
);
7059 dprintf("path: %s total number of volumes: %d\n", hpath
, count
);
7062 struct md_list
*dv
= devlist
;
7063 devlist
= devlist
->next
;
7071 static int count_volumes(struct intel_hba
*hba
, int dpa
, int verbose
)
7075 if (hba
->type
== SYS_DEV_VMD
) {
7076 struct sys_dev
*dev
;
7079 for (dev
= find_intel_devices(); dev
; dev
= dev
->next
) {
7080 if (dev
->type
== SYS_DEV_VMD
)
7081 count
+= __count_volumes(dev
->path
, dpa
,
7086 return __count_volumes(hba
->path
, dpa
, verbose
, 0);
7089 static int imsm_default_chunk(const struct imsm_orom
*orom
)
7091 /* up to 512 if the plaform supports it, otherwise the platform max.
7092 * 128 if no platform detected
7094 int fs
= max(7, orom
? fls(orom
->sss
) : 0);
7096 return min(512, (1 << fs
));
7100 validate_geometry_imsm_orom(struct intel_super
*super
, int level
, int layout
,
7101 int raiddisks
, int *chunk
, unsigned long long size
, int verbose
)
7103 /* check/set platform and metadata limits/defaults */
7104 if (super
->orom
&& raiddisks
> super
->orom
->dpa
) {
7105 pr_vrb("platform supports a maximum of %d disks per array\n",
7110 /* capabilities of OROM tested - copied from validate_geometry_imsm_volume */
7111 if (!is_raid_level_supported(super
->orom
, level
, raiddisks
)) {
7112 pr_vrb("platform does not support raid%d with %d disk%s\n",
7113 level
, raiddisks
, raiddisks
> 1 ? "s" : "");
7117 if (*chunk
== 0 || *chunk
== UnSet
)
7118 *chunk
= imsm_default_chunk(super
->orom
);
7120 if (super
->orom
&& !imsm_orom_has_chunk(super
->orom
, *chunk
)) {
7121 pr_vrb("platform does not support a chunk size of: %d\n", *chunk
);
7125 if (layout
!= imsm_level_to_layout(level
)) {
7127 pr_vrb("imsm raid 5 only supports the left-asymmetric layout\n");
7128 else if (level
== 10)
7129 pr_vrb("imsm raid 10 only supports the n2 layout\n");
7131 pr_vrb("imsm unknown layout %#x for this raid level %d\n",
7136 if (super
->orom
&& (super
->orom
->attr
& IMSM_OROM_ATTR_2TB
) == 0 &&
7137 (calc_array_size(level
, raiddisks
, layout
, *chunk
, size
) >> 32) > 0) {
7138 pr_vrb("platform does not support a volume size over 2TB\n");
7145 /* validate_geometry_imsm_volume - lifted from validate_geometry_ddf_bvd
7146 * FIX ME add ahci details
7148 static int validate_geometry_imsm_volume(struct supertype
*st
, int level
,
7149 int layout
, int raiddisks
, int *chunk
,
7150 unsigned long long size
,
7151 unsigned long long data_offset
,
7153 unsigned long long *freesize
,
7157 struct intel_super
*super
= st
->sb
;
7158 struct imsm_super
*mpb
;
7160 unsigned long long pos
= 0;
7161 unsigned long long maxsize
;
7165 /* We must have the container info already read in. */
7169 mpb
= super
->anchor
;
7171 if (!validate_geometry_imsm_orom(super
, level
, layout
, raiddisks
, chunk
, size
, verbose
)) {
7172 pr_err("RAID geometry validation failed. Cannot proceed with the action(s).\n");
7176 /* General test: make sure there is space for
7177 * 'raiddisks' device extents of size 'size' at a given
7180 unsigned long long minsize
= size
;
7181 unsigned long long start_offset
= MaxSector
;
7184 minsize
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
7185 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
7190 e
= get_extents(super
, dl
, 0);
7193 unsigned long long esize
;
7194 esize
= e
[i
].start
- pos
;
7195 if (esize
>= minsize
)
7197 if (found
&& start_offset
== MaxSector
) {
7200 } else if (found
&& pos
!= start_offset
) {
7204 pos
= e
[i
].start
+ e
[i
].size
;
7206 } while (e
[i
-1].size
);
7211 if (dcnt
< raiddisks
) {
7213 pr_err("imsm: Not enough devices with space for this array (%d < %d)\n",
7220 /* This device must be a member of the set */
7221 if (!stat_is_blkdev(dev
, &rdev
))
7223 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
7224 if (dl
->major
== (int)major(rdev
) &&
7225 dl
->minor
== (int)minor(rdev
))
7230 pr_err("%s is not in the same imsm set\n", dev
);
7232 } else if (super
->orom
&& dl
->index
< 0 && mpb
->num_raid_devs
) {
7233 /* If a volume is present then the current creation attempt
7234 * cannot incorporate new spares because the orom may not
7235 * understand this configuration (all member disks must be
7236 * members of each array in the container).
7238 pr_err("%s is a spare and a volume is already defined for this container\n", dev
);
7239 pr_err("The option-rom requires all member disks to be a member of all volumes\n");
7241 } else if (super
->orom
&& mpb
->num_raid_devs
> 0 &&
7242 mpb
->num_disks
!= raiddisks
) {
7243 pr_err("The option-rom requires all member disks to be a member of all volumes\n");
7247 /* retrieve the largest free space block */
7248 e
= get_extents(super
, dl
, 0);
7253 unsigned long long esize
;
7255 esize
= e
[i
].start
- pos
;
7256 if (esize
>= maxsize
)
7258 pos
= e
[i
].start
+ e
[i
].size
;
7260 } while (e
[i
-1].size
);
7265 pr_err("unable to determine free space for: %s\n",
7269 if (maxsize
< size
) {
7271 pr_err("%s not enough space (%llu < %llu)\n",
7272 dev
, maxsize
, size
);
7276 /* count total number of extents for merge */
7278 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
7280 i
+= dl
->extent_cnt
;
7282 maxsize
= merge_extents(super
, i
);
7284 if (mpb
->num_raid_devs
> 0 && size
&& size
!= maxsize
)
7285 pr_err("attempting to create a second volume with size less then remaining space.\n");
7287 if (maxsize
< size
|| maxsize
== 0) {
7290 pr_err("no free space left on device. Aborting...\n");
7292 pr_err("not enough space to create volume of given size (%llu < %llu). Aborting...\n",
7298 *freesize
= maxsize
;
7301 int count
= count_volumes(super
->hba
,
7302 super
->orom
->dpa
, verbose
);
7303 if (super
->orom
->vphba
<= count
) {
7304 pr_vrb("platform does not support more than %d raid volumes.\n",
7305 super
->orom
->vphba
);
7312 static int imsm_get_free_size(struct supertype
*st
, int raiddisks
,
7313 unsigned long long size
, int chunk
,
7314 unsigned long long *freesize
)
7316 struct intel_super
*super
= st
->sb
;
7317 struct imsm_super
*mpb
= super
->anchor
;
7322 unsigned long long maxsize
;
7323 unsigned long long minsize
;
7327 /* find the largest common start free region of the possible disks */
7331 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
7337 /* don't activate new spares if we are orom constrained
7338 * and there is already a volume active in the container
7340 if (super
->orom
&& dl
->index
< 0 && mpb
->num_raid_devs
)
7343 e
= get_extents(super
, dl
, 0);
7346 for (i
= 1; e
[i
-1].size
; i
++)
7354 maxsize
= merge_extents(super
, extent_cnt
);
7358 minsize
= chunk
* 2;
7360 if (cnt
< raiddisks
||
7361 (super
->orom
&& used
&& used
!= raiddisks
) ||
7362 maxsize
< minsize
||
7364 pr_err("not enough devices with space to create array.\n");
7365 return 0; /* No enough free spaces large enough */
7376 if (mpb
->num_raid_devs
> 0 && size
&& size
!= maxsize
)
7377 pr_err("attempting to create a second volume with size less then remaining space.\n");
7379 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
7381 dl
->raiddisk
= cnt
++;
7385 dprintf("imsm: imsm_get_free_size() returns : %llu\n", size
);
7390 static int reserve_space(struct supertype
*st
, int raiddisks
,
7391 unsigned long long size
, int chunk
,
7392 unsigned long long *freesize
)
7394 struct intel_super
*super
= st
->sb
;
7399 rv
= imsm_get_free_size(st
, raiddisks
, size
, chunk
, freesize
);
7402 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
7404 dl
->raiddisk
= cnt
++;
7411 static int validate_geometry_imsm(struct supertype
*st
, int level
, int layout
,
7412 int raiddisks
, int *chunk
, unsigned long long size
,
7413 unsigned long long data_offset
,
7414 char *dev
, unsigned long long *freesize
,
7415 int consistency_policy
, int verbose
)
7422 * if given unused devices create a container
7423 * if given given devices in a container create a member volume
7425 if (level
== LEVEL_CONTAINER
) {
7426 /* Must be a fresh device to add to a container */
7427 return validate_geometry_imsm_container(st
, level
, layout
,
7436 * Size is given in sectors.
7438 if (size
&& (size
< 2048)) {
7439 pr_err("Given size must be greater than 1M.\n");
7440 /* Depends on algorithm in Create.c :
7441 * if container was given (dev == NULL) return -1,
7442 * if block device was given ( dev != NULL) return 0.
7444 return dev
? -1 : 0;
7449 struct intel_super
*super
= st
->sb
;
7450 if (!validate_geometry_imsm_orom(st
->sb
, level
, layout
,
7451 raiddisks
, chunk
, size
,
7454 /* we are being asked to automatically layout a
7455 * new volume based on the current contents of
7456 * the container. If the the parameters can be
7457 * satisfied reserve_space will record the disks,
7458 * start offset, and size of the volume to be
7459 * created. add_to_super and getinfo_super
7460 * detect when autolayout is in progress.
7462 /* assuming that freesize is always given when array is
7464 if (super
->orom
&& freesize
) {
7466 count
= count_volumes(super
->hba
,
7467 super
->orom
->dpa
, verbose
);
7468 if (super
->orom
->vphba
<= count
) {
7469 pr_vrb("platform does not support more than %d raid volumes.\n",
7470 super
->orom
->vphba
);
7475 return reserve_space(st
, raiddisks
, size
,
7481 /* creating in a given container */
7482 return validate_geometry_imsm_volume(st
, level
, layout
,
7483 raiddisks
, chunk
, size
,
7485 dev
, freesize
, verbose
);
7488 /* This device needs to be a device in an 'imsm' container */
7489 fd
= open(dev
, O_RDONLY
|O_EXCL
, 0);
7492 pr_err("Cannot create this array on device %s\n",
7497 if (errno
!= EBUSY
|| (fd
= open(dev
, O_RDONLY
, 0)) < 0) {
7499 pr_err("Cannot open %s: %s\n",
7500 dev
, strerror(errno
));
7503 /* Well, it is in use by someone, maybe an 'imsm' container. */
7504 cfd
= open_container(fd
);
7508 pr_err("Cannot use %s: It is busy\n",
7512 sra
= sysfs_read(cfd
, NULL
, GET_VERSION
);
7513 if (sra
&& sra
->array
.major_version
== -1 &&
7514 strcmp(sra
->text_version
, "imsm") == 0)
7518 /* This is a member of a imsm container. Load the container
7519 * and try to create a volume
7521 struct intel_super
*super
;
7523 if (load_super_imsm_all(st
, cfd
, (void **) &super
, NULL
, NULL
, 1) == 0) {
7525 strcpy(st
->container_devnm
, fd2devnm(cfd
));
7527 return validate_geometry_imsm_volume(st
, level
, layout
,
7529 size
, data_offset
, dev
,
7536 pr_err("failed container membership check\n");
7542 static void default_geometry_imsm(struct supertype
*st
, int *level
, int *layout
, int *chunk
)
7544 struct intel_super
*super
= st
->sb
;
7546 if (level
&& *level
== UnSet
)
7547 *level
= LEVEL_CONTAINER
;
7549 if (level
&& layout
&& *layout
== UnSet
)
7550 *layout
= imsm_level_to_layout(*level
);
7552 if (chunk
&& (*chunk
== UnSet
|| *chunk
== 0))
7553 *chunk
= imsm_default_chunk(super
->orom
);
7556 static void handle_missing(struct intel_super
*super
, struct imsm_dev
*dev
);
7558 static int kill_subarray_imsm(struct supertype
*st
, char *subarray_id
)
7560 /* remove the subarray currently referenced by subarray_id */
7562 struct intel_dev
**dp
;
7563 struct intel_super
*super
= st
->sb
;
7564 __u8 current_vol
= strtoul(subarray_id
, NULL
, 10);
7565 struct imsm_super
*mpb
= super
->anchor
;
7567 if (mpb
->num_raid_devs
== 0)
7570 /* block deletions that would change the uuid of active subarrays
7572 * FIXME when immutable ids are available, but note that we'll
7573 * also need to fixup the invalidated/active subarray indexes in
7576 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
7579 if (i
< current_vol
)
7581 sprintf(subarray
, "%u", i
);
7582 if (is_subarray_active(subarray
, st
->devnm
)) {
7583 pr_err("deleting subarray-%d would change the UUID of active subarray-%d, aborting\n",
7590 if (st
->update_tail
) {
7591 struct imsm_update_kill_array
*u
= xmalloc(sizeof(*u
));
7593 u
->type
= update_kill_array
;
7594 u
->dev_idx
= current_vol
;
7595 append_metadata_update(st
, u
, sizeof(*u
));
7600 for (dp
= &super
->devlist
; *dp
;)
7601 if ((*dp
)->index
== current_vol
) {
7604 handle_missing(super
, (*dp
)->dev
);
7605 if ((*dp
)->index
> current_vol
)
7610 /* no more raid devices, all active components are now spares,
7611 * but of course failed are still failed
7613 if (--mpb
->num_raid_devs
== 0) {
7616 for (d
= super
->disks
; d
; d
= d
->next
)
7621 super
->updates_pending
++;
7626 static int update_subarray_imsm(struct supertype
*st
, char *subarray
,
7627 char *update
, struct mddev_ident
*ident
)
7629 /* update the subarray currently referenced by ->current_vol */
7630 struct intel_super
*super
= st
->sb
;
7631 struct imsm_super
*mpb
= super
->anchor
;
7633 if (strcmp(update
, "name") == 0) {
7634 char *name
= ident
->name
;
7638 if (is_subarray_active(subarray
, st
->devnm
)) {
7639 pr_err("Unable to update name of active subarray\n");
7643 if (!check_name(super
, name
, 0))
7646 vol
= strtoul(subarray
, &ep
, 10);
7647 if (*ep
!= '\0' || vol
>= super
->anchor
->num_raid_devs
)
7650 if (st
->update_tail
) {
7651 struct imsm_update_rename_array
*u
= xmalloc(sizeof(*u
));
7653 u
->type
= update_rename_array
;
7655 strncpy((char *) u
->name
, name
, MAX_RAID_SERIAL_LEN
);
7656 u
->name
[MAX_RAID_SERIAL_LEN
-1] = '\0';
7657 append_metadata_update(st
, u
, sizeof(*u
));
7659 struct imsm_dev
*dev
;
7662 dev
= get_imsm_dev(super
, vol
);
7663 memset(dev
->volume
, '\0', MAX_RAID_SERIAL_LEN
);
7664 namelen
= min((int)strlen(name
), MAX_RAID_SERIAL_LEN
);
7665 memcpy(dev
->volume
, name
, namelen
);
7666 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
7667 dev
= get_imsm_dev(super
, i
);
7668 handle_missing(super
, dev
);
7670 super
->updates_pending
++;
7672 } else if (strcmp(update
, "ppl") == 0 ||
7673 strcmp(update
, "no-ppl") == 0) {
7676 int vol
= strtoul(subarray
, &ep
, 10);
7678 if (*ep
!= '\0' || vol
>= super
->anchor
->num_raid_devs
)
7681 if (strcmp(update
, "ppl") == 0)
7682 new_policy
= RWH_MULTIPLE_DISTRIBUTED
;
7684 new_policy
= RWH_MULTIPLE_OFF
;
7686 if (st
->update_tail
) {
7687 struct imsm_update_rwh_policy
*u
= xmalloc(sizeof(*u
));
7689 u
->type
= update_rwh_policy
;
7691 u
->new_policy
= new_policy
;
7692 append_metadata_update(st
, u
, sizeof(*u
));
7694 struct imsm_dev
*dev
;
7696 dev
= get_imsm_dev(super
, vol
);
7697 dev
->rwh_policy
= new_policy
;
7698 super
->updates_pending
++;
7706 static int is_gen_migration(struct imsm_dev
*dev
)
7711 if (!dev
->vol
.migr_state
)
7714 if (migr_type(dev
) == MIGR_GEN_MIGR
)
7720 static int is_rebuilding(struct imsm_dev
*dev
)
7722 struct imsm_map
*migr_map
;
7724 if (!dev
->vol
.migr_state
)
7727 if (migr_type(dev
) != MIGR_REBUILD
)
7730 migr_map
= get_imsm_map(dev
, MAP_1
);
7732 if (migr_map
->map_state
== IMSM_T_STATE_DEGRADED
)
7738 static int is_initializing(struct imsm_dev
*dev
)
7740 struct imsm_map
*migr_map
;
7742 if (!dev
->vol
.migr_state
)
7745 if (migr_type(dev
) != MIGR_INIT
)
7748 migr_map
= get_imsm_map(dev
, MAP_1
);
7750 if (migr_map
->map_state
== IMSM_T_STATE_UNINITIALIZED
)
7756 static void update_recovery_start(struct intel_super
*super
,
7757 struct imsm_dev
*dev
,
7758 struct mdinfo
*array
)
7760 struct mdinfo
*rebuild
= NULL
;
7764 if (!is_rebuilding(dev
))
7767 /* Find the rebuild target, but punt on the dual rebuild case */
7768 for (d
= array
->devs
; d
; d
= d
->next
)
7769 if (d
->recovery_start
== 0) {
7776 /* (?) none of the disks are marked with
7777 * IMSM_ORD_REBUILD, so assume they are missing and the
7778 * disk_ord_tbl was not correctly updated
7780 dprintf("failed to locate out-of-sync disk\n");
7784 units
= __le32_to_cpu(dev
->vol
.curr_migr_unit
);
7785 rebuild
->recovery_start
= units
* blocks_per_migr_unit(super
, dev
);
7788 static int recover_backup_imsm(struct supertype
*st
, struct mdinfo
*info
);
7790 static struct mdinfo
*container_content_imsm(struct supertype
*st
, char *subarray
)
7792 /* Given a container loaded by load_super_imsm_all,
7793 * extract information about all the arrays into
7795 * If 'subarray' is given, just extract info about that array.
7797 * For each imsm_dev create an mdinfo, fill it in,
7798 * then look for matching devices in super->disks
7799 * and create appropriate device mdinfo.
7801 struct intel_super
*super
= st
->sb
;
7802 struct imsm_super
*mpb
= super
->anchor
;
7803 struct mdinfo
*rest
= NULL
;
7807 int spare_disks
= 0;
7808 int current_vol
= super
->current_vol
;
7810 /* do not assemble arrays when not all attributes are supported */
7811 if (imsm_check_attributes(mpb
->attributes
) == 0) {
7813 pr_err("Unsupported attributes in IMSM metadata.Arrays activation is blocked.\n");
7816 /* count spare devices, not used in maps
7818 for (d
= super
->disks
; d
; d
= d
->next
)
7822 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
7823 struct imsm_dev
*dev
;
7824 struct imsm_map
*map
;
7825 struct imsm_map
*map2
;
7826 struct mdinfo
*this;
7833 (i
!= strtoul(subarray
, &ep
, 10) || *ep
!= '\0'))
7836 dev
= get_imsm_dev(super
, i
);
7837 map
= get_imsm_map(dev
, MAP_0
);
7838 map2
= get_imsm_map(dev
, MAP_1
);
7839 level
= get_imsm_raid_level(map
);
7841 /* do not publish arrays that are in the middle of an
7842 * unsupported migration
7844 if (dev
->vol
.migr_state
&&
7845 (migr_type(dev
) == MIGR_STATE_CHANGE
)) {
7846 pr_err("cannot assemble volume '%.16s': unsupported migration in progress\n",
7850 /* do not publish arrays that are not support by controller's
7854 this = xmalloc(sizeof(*this));
7856 super
->current_vol
= i
;
7857 getinfo_super_imsm_volume(st
, this, NULL
);
7859 chunk
= __le16_to_cpu(map
->blocks_per_strip
) >> 1;
7860 /* mdadm does not support all metadata features- set the bit in all arrays state */
7861 if (!validate_geometry_imsm_orom(super
,
7862 level
, /* RAID level */
7863 imsm_level_to_layout(level
),
7864 map
->num_members
, /* raid disks */
7865 &chunk
, imsm_dev_size(dev
),
7867 pr_err("IMSM RAID geometry validation failed. Array %s activation is blocked.\n",
7869 this->array
.state
|=
7870 (1<<MD_SB_BLOCK_CONTAINER_RESHAPE
) |
7871 (1<<MD_SB_BLOCK_VOLUME
);
7874 /* if array has bad blocks, set suitable bit in all arrays state */
7876 this->array
.state
|=
7877 (1<<MD_SB_BLOCK_CONTAINER_RESHAPE
) |
7878 (1<<MD_SB_BLOCK_VOLUME
);
7880 for (slot
= 0 ; slot
< map
->num_members
; slot
++) {
7881 unsigned long long recovery_start
;
7882 struct mdinfo
*info_d
;
7890 idx
= get_imsm_disk_idx(dev
, slot
, MAP_0
);
7891 ord
= get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
);
7892 for (d
= super
->disks
; d
; d
= d
->next
)
7893 if (d
->index
== idx
)
7896 recovery_start
= MaxSector
;
7899 if (d
&& is_failed(&d
->disk
))
7901 if (!skip
&& (ord
& IMSM_ORD_REBUILD
))
7903 if (!(ord
& IMSM_ORD_REBUILD
))
7904 this->array
.working_disks
++;
7906 * if we skip some disks the array will be assmebled degraded;
7907 * reset resync start to avoid a dirty-degraded
7908 * situation when performing the intial sync
7913 if (!(dev
->vol
.dirty
& RAIDVOL_DIRTY
)) {
7914 if ((!able_to_resync(level
, missing
) ||
7915 recovery_start
== 0))
7916 this->resync_start
= MaxSector
;
7919 * FIXME handle dirty degraded
7926 info_d
= xcalloc(1, sizeof(*info_d
));
7927 info_d
->next
= this->devs
;
7928 this->devs
= info_d
;
7930 info_d
->disk
.number
= d
->index
;
7931 info_d
->disk
.major
= d
->major
;
7932 info_d
->disk
.minor
= d
->minor
;
7933 info_d
->disk
.raid_disk
= slot
;
7934 info_d
->recovery_start
= recovery_start
;
7936 if (slot
< map2
->num_members
)
7937 info_d
->disk
.state
= (1 << MD_DISK_ACTIVE
);
7939 this->array
.spare_disks
++;
7941 if (slot
< map
->num_members
)
7942 info_d
->disk
.state
= (1 << MD_DISK_ACTIVE
);
7944 this->array
.spare_disks
++;
7947 info_d
->events
= __le32_to_cpu(mpb
->generation_num
);
7948 info_d
->data_offset
= pba_of_lba0(map
);
7949 info_d
->component_size
= calc_component_size(map
, dev
);
7951 if (map
->raid_level
== 5) {
7952 info_d
->ppl_sector
= this->ppl_sector
;
7953 info_d
->ppl_size
= this->ppl_size
;
7954 if (this->consistency_policy
== CONSISTENCY_POLICY_PPL
&&
7955 recovery_start
== 0)
7956 this->resync_start
= 0;
7959 info_d
->bb
.supported
= 1;
7960 get_volume_badblocks(super
->bbm_log
, ord_to_idx(ord
),
7961 info_d
->data_offset
,
7962 info_d
->component_size
,
7965 /* now that the disk list is up-to-date fixup recovery_start */
7966 update_recovery_start(super
, dev
, this);
7967 this->array
.spare_disks
+= spare_disks
;
7969 /* check for reshape */
7970 if (this->reshape_active
== 1)
7971 recover_backup_imsm(st
, this);
7975 super
->current_vol
= current_vol
;
7979 static __u8
imsm_check_degraded(struct intel_super
*super
, struct imsm_dev
*dev
,
7980 int failed
, int look_in_map
)
7982 struct imsm_map
*map
;
7984 map
= get_imsm_map(dev
, look_in_map
);
7987 return map
->map_state
== IMSM_T_STATE_UNINITIALIZED
?
7988 IMSM_T_STATE_UNINITIALIZED
: IMSM_T_STATE_NORMAL
;
7990 switch (get_imsm_raid_level(map
)) {
7992 return IMSM_T_STATE_FAILED
;
7995 if (failed
< map
->num_members
)
7996 return IMSM_T_STATE_DEGRADED
;
7998 return IMSM_T_STATE_FAILED
;
8003 * check to see if any mirrors have failed, otherwise we
8004 * are degraded. Even numbered slots are mirrored on
8008 /* gcc -Os complains that this is unused */
8009 int insync
= insync
;
8011 for (i
= 0; i
< map
->num_members
; i
++) {
8012 __u32 ord
= get_imsm_ord_tbl_ent(dev
, i
, MAP_X
);
8013 int idx
= ord_to_idx(ord
);
8014 struct imsm_disk
*disk
;
8016 /* reset the potential in-sync count on even-numbered
8017 * slots. num_copies is always 2 for imsm raid10
8022 disk
= get_imsm_disk(super
, idx
);
8023 if (!disk
|| is_failed(disk
) || ord
& IMSM_ORD_REBUILD
)
8026 /* no in-sync disks left in this mirror the
8030 return IMSM_T_STATE_FAILED
;
8033 return IMSM_T_STATE_DEGRADED
;
8037 return IMSM_T_STATE_DEGRADED
;
8039 return IMSM_T_STATE_FAILED
;
8045 return map
->map_state
;
8048 static int imsm_count_failed(struct intel_super
*super
, struct imsm_dev
*dev
,
8053 struct imsm_disk
*disk
;
8054 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8055 struct imsm_map
*prev
= get_imsm_map(dev
, MAP_1
);
8056 struct imsm_map
*map_for_loop
;
8061 /* at the beginning of migration we set IMSM_ORD_REBUILD on
8062 * disks that are being rebuilt. New failures are recorded to
8063 * map[0]. So we look through all the disks we started with and
8064 * see if any failures are still present, or if any new ones
8068 if (prev
&& (map
->num_members
< prev
->num_members
))
8069 map_for_loop
= prev
;
8071 for (i
= 0; i
< map_for_loop
->num_members
; i
++) {
8073 /* when MAP_X is passed both maps failures are counted
8076 (look_in_map
== MAP_1
|| look_in_map
== MAP_X
) &&
8077 i
< prev
->num_members
) {
8078 ord
= __le32_to_cpu(prev
->disk_ord_tbl
[i
]);
8079 idx_1
= ord_to_idx(ord
);
8081 disk
= get_imsm_disk(super
, idx_1
);
8082 if (!disk
|| is_failed(disk
) || ord
& IMSM_ORD_REBUILD
)
8085 if ((look_in_map
== MAP_0
|| look_in_map
== MAP_X
) &&
8086 i
< map
->num_members
) {
8087 ord
= __le32_to_cpu(map
->disk_ord_tbl
[i
]);
8088 idx
= ord_to_idx(ord
);
8091 disk
= get_imsm_disk(super
, idx
);
8092 if (!disk
|| is_failed(disk
) ||
8093 ord
& IMSM_ORD_REBUILD
)
8102 static int imsm_open_new(struct supertype
*c
, struct active_array
*a
,
8105 struct intel_super
*super
= c
->sb
;
8106 struct imsm_super
*mpb
= super
->anchor
;
8107 struct imsm_update_prealloc_bb_mem u
;
8109 if (atoi(inst
) >= mpb
->num_raid_devs
) {
8110 pr_err("subarry index %d, out of range\n", atoi(inst
));
8114 dprintf("imsm: open_new %s\n", inst
);
8115 a
->info
.container_member
= atoi(inst
);
8117 u
.type
= update_prealloc_badblocks_mem
;
8118 imsm_update_metadata_locally(c
, &u
, sizeof(u
));
8123 static int is_resyncing(struct imsm_dev
*dev
)
8125 struct imsm_map
*migr_map
;
8127 if (!dev
->vol
.migr_state
)
8130 if (migr_type(dev
) == MIGR_INIT
||
8131 migr_type(dev
) == MIGR_REPAIR
)
8134 if (migr_type(dev
) == MIGR_GEN_MIGR
)
8137 migr_map
= get_imsm_map(dev
, MAP_1
);
8139 if (migr_map
->map_state
== IMSM_T_STATE_NORMAL
&&
8140 dev
->vol
.migr_type
!= MIGR_GEN_MIGR
)
8146 /* return true if we recorded new information */
8147 static int mark_failure(struct intel_super
*super
,
8148 struct imsm_dev
*dev
, struct imsm_disk
*disk
, int idx
)
8152 struct imsm_map
*map
;
8153 char buf
[MAX_RAID_SERIAL_LEN
+3];
8154 unsigned int len
, shift
= 0;
8156 /* new failures are always set in map[0] */
8157 map
= get_imsm_map(dev
, MAP_0
);
8159 slot
= get_imsm_disk_slot(map
, idx
);
8163 ord
= __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
8164 if (is_failed(disk
) && (ord
& IMSM_ORD_REBUILD
))
8167 memcpy(buf
, disk
->serial
, MAX_RAID_SERIAL_LEN
);
8168 buf
[MAX_RAID_SERIAL_LEN
] = '\000';
8170 if ((len
= strlen(buf
)) >= MAX_RAID_SERIAL_LEN
)
8171 shift
= len
- MAX_RAID_SERIAL_LEN
+ 1;
8172 memcpy(disk
->serial
, &buf
[shift
], len
+ 1 - shift
);
8174 disk
->status
|= FAILED_DISK
;
8175 set_imsm_ord_tbl_ent(map
, slot
, idx
| IMSM_ORD_REBUILD
);
8176 /* mark failures in second map if second map exists and this disk
8178 * This is valid for migration, initialization and rebuild
8180 if (dev
->vol
.migr_state
) {
8181 struct imsm_map
*map2
= get_imsm_map(dev
, MAP_1
);
8182 int slot2
= get_imsm_disk_slot(map2
, idx
);
8184 if (slot2
< map2
->num_members
&& slot2
>= 0)
8185 set_imsm_ord_tbl_ent(map2
, slot2
,
8186 idx
| IMSM_ORD_REBUILD
);
8188 if (map
->failed_disk_num
== 0xff ||
8189 (!is_rebuilding(dev
) && map
->failed_disk_num
> slot
))
8190 map
->failed_disk_num
= slot
;
8192 clear_disk_badblocks(super
->bbm_log
, ord_to_idx(ord
));
8197 static void mark_missing(struct intel_super
*super
,
8198 struct imsm_dev
*dev
, struct imsm_disk
*disk
, int idx
)
8200 mark_failure(super
, dev
, disk
, idx
);
8202 if (disk
->scsi_id
== __cpu_to_le32(~(__u32
)0))
8205 disk
->scsi_id
= __cpu_to_le32(~(__u32
)0);
8206 memmove(&disk
->serial
[0], &disk
->serial
[1], MAX_RAID_SERIAL_LEN
- 1);
8209 static void handle_missing(struct intel_super
*super
, struct imsm_dev
*dev
)
8213 if (!super
->missing
)
8216 /* When orom adds replacement for missing disk it does
8217 * not remove entry of missing disk, but just updates map with
8218 * new added disk. So it is not enough just to test if there is
8219 * any missing disk, we have to look if there are any failed disks
8220 * in map to stop migration */
8222 dprintf("imsm: mark missing\n");
8223 /* end process for initialization and rebuild only
8225 if (is_gen_migration(dev
) == 0) {
8226 int failed
= imsm_count_failed(super
, dev
, MAP_0
);
8230 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8231 struct imsm_map
*map1
;
8232 int i
, ord
, ord_map1
;
8235 for (i
= 0; i
< map
->num_members
; i
++) {
8236 ord
= get_imsm_ord_tbl_ent(dev
, i
, MAP_0
);
8237 if (!(ord
& IMSM_ORD_REBUILD
))
8240 map1
= get_imsm_map(dev
, MAP_1
);
8244 ord_map1
= __le32_to_cpu(map1
->disk_ord_tbl
[i
]);
8245 if (ord_map1
& IMSM_ORD_REBUILD
)
8250 map_state
= imsm_check_degraded(super
, dev
,
8252 end_migration(dev
, super
, map_state
);
8256 for (dl
= super
->missing
; dl
; dl
= dl
->next
)
8257 mark_missing(super
, dev
, &dl
->disk
, dl
->index
);
8258 super
->updates_pending
++;
8261 static unsigned long long imsm_set_array_size(struct imsm_dev
*dev
,
8264 unsigned long long array_blocks
;
8265 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8266 int used_disks
= imsm_num_data_members(map
);
8268 if (used_disks
== 0) {
8269 /* when problems occures
8270 * return current array_blocks value
8272 array_blocks
= imsm_dev_size(dev
);
8274 return array_blocks
;
8277 /* set array size in metadata
8280 /* OLCE size change is caused by added disks
8282 array_blocks
= per_dev_array_size(map
) * used_disks
;
8284 /* Online Volume Size Change
8285 * Using available free space
8287 array_blocks
= new_size
;
8289 array_blocks
= round_size_to_mb(array_blocks
, used_disks
);
8290 set_imsm_dev_size(dev
, array_blocks
);
8292 return array_blocks
;
8295 static void imsm_set_disk(struct active_array
*a
, int n
, int state
);
8297 static void imsm_progress_container_reshape(struct intel_super
*super
)
8299 /* if no device has a migr_state, but some device has a
8300 * different number of members than the previous device, start
8301 * changing the number of devices in this device to match
8304 struct imsm_super
*mpb
= super
->anchor
;
8305 int prev_disks
= -1;
8309 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
8310 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
8311 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8312 struct imsm_map
*map2
;
8313 int prev_num_members
;
8315 if (dev
->vol
.migr_state
)
8318 if (prev_disks
== -1)
8319 prev_disks
= map
->num_members
;
8320 if (prev_disks
== map
->num_members
)
8323 /* OK, this array needs to enter reshape mode.
8324 * i.e it needs a migr_state
8327 copy_map_size
= sizeof_imsm_map(map
);
8328 prev_num_members
= map
->num_members
;
8329 map
->num_members
= prev_disks
;
8330 dev
->vol
.migr_state
= 1;
8331 dev
->vol
.curr_migr_unit
= 0;
8332 set_migr_type(dev
, MIGR_GEN_MIGR
);
8333 for (i
= prev_num_members
;
8334 i
< map
->num_members
; i
++)
8335 set_imsm_ord_tbl_ent(map
, i
, i
);
8336 map2
= get_imsm_map(dev
, MAP_1
);
8337 /* Copy the current map */
8338 memcpy(map2
, map
, copy_map_size
);
8339 map2
->num_members
= prev_num_members
;
8341 imsm_set_array_size(dev
, -1);
8342 super
->clean_migration_record_by_mdmon
= 1;
8343 super
->updates_pending
++;
8347 /* Handle dirty -> clean transititions, resync and reshape. Degraded and rebuild
8348 * states are handled in imsm_set_disk() with one exception, when a
8349 * resync is stopped due to a new failure this routine will set the
8350 * 'degraded' state for the array.
8352 static int imsm_set_array_state(struct active_array
*a
, int consistent
)
8354 int inst
= a
->info
.container_member
;
8355 struct intel_super
*super
= a
->container
->sb
;
8356 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
8357 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8358 int failed
= imsm_count_failed(super
, dev
, MAP_0
);
8359 __u8 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
8360 __u32 blocks_per_unit
;
8362 if (dev
->vol
.migr_state
&&
8363 dev
->vol
.migr_type
== MIGR_GEN_MIGR
) {
8364 /* array state change is blocked due to reshape action
8366 * - abort the reshape (if last_checkpoint is 0 and action!= reshape)
8367 * - finish the reshape (if last_checkpoint is big and action != reshape)
8368 * - update curr_migr_unit
8370 if (a
->curr_action
== reshape
) {
8371 /* still reshaping, maybe update curr_migr_unit */
8372 goto mark_checkpoint
;
8374 if (a
->last_checkpoint
== 0 && a
->prev_action
== reshape
) {
8375 /* for some reason we aborted the reshape.
8377 * disable automatic metadata rollback
8378 * user action is required to recover process
8381 struct imsm_map
*map2
=
8382 get_imsm_map(dev
, MAP_1
);
8383 dev
->vol
.migr_state
= 0;
8384 set_migr_type(dev
, 0);
8385 dev
->vol
.curr_migr_unit
= 0;
8387 sizeof_imsm_map(map2
));
8388 super
->updates_pending
++;
8391 if (a
->last_checkpoint
>= a
->info
.component_size
) {
8392 unsigned long long array_blocks
;
8396 used_disks
= imsm_num_data_members(map
);
8397 if (used_disks
> 0) {
8399 per_dev_array_size(map
) *
8402 round_size_to_mb(array_blocks
,
8404 a
->info
.custom_array_size
= array_blocks
;
8405 /* encourage manager to update array
8409 a
->check_reshape
= 1;
8411 /* finalize online capacity expansion/reshape */
8412 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
)
8414 mdi
->disk
.raid_disk
,
8417 imsm_progress_container_reshape(super
);
8422 /* before we activate this array handle any missing disks */
8423 if (consistent
== 2)
8424 handle_missing(super
, dev
);
8426 if (consistent
== 2 &&
8427 (!is_resync_complete(&a
->info
) ||
8428 map_state
!= IMSM_T_STATE_NORMAL
||
8429 dev
->vol
.migr_state
))
8432 if (is_resync_complete(&a
->info
)) {
8433 /* complete intialization / resync,
8434 * recovery and interrupted recovery is completed in
8437 if (is_resyncing(dev
)) {
8438 dprintf("imsm: mark resync done\n");
8439 end_migration(dev
, super
, map_state
);
8440 super
->updates_pending
++;
8441 a
->last_checkpoint
= 0;
8443 } else if ((!is_resyncing(dev
) && !failed
) &&
8444 (imsm_reshape_blocks_arrays_changes(super
) == 0)) {
8445 /* mark the start of the init process if nothing is failed */
8446 dprintf("imsm: mark resync start\n");
8447 if (map
->map_state
== IMSM_T_STATE_UNINITIALIZED
)
8448 migrate(dev
, super
, IMSM_T_STATE_NORMAL
, MIGR_INIT
);
8450 migrate(dev
, super
, IMSM_T_STATE_NORMAL
, MIGR_REPAIR
);
8451 super
->updates_pending
++;
8455 /* skip checkpointing for general migration,
8456 * it is controlled in mdadm
8458 if (is_gen_migration(dev
))
8459 goto skip_mark_checkpoint
;
8461 /* check if we can update curr_migr_unit from resync_start, recovery_start */
8462 blocks_per_unit
= blocks_per_migr_unit(super
, dev
);
8463 if (blocks_per_unit
) {
8467 units
= a
->last_checkpoint
/ blocks_per_unit
;
8470 /* check that we did not overflow 32-bits, and that
8471 * curr_migr_unit needs updating
8473 if (units32
== units
&&
8475 __le32_to_cpu(dev
->vol
.curr_migr_unit
) != units32
) {
8476 dprintf("imsm: mark checkpoint (%u)\n", units32
);
8477 dev
->vol
.curr_migr_unit
= __cpu_to_le32(units32
);
8478 super
->updates_pending
++;
8482 skip_mark_checkpoint
:
8483 /* mark dirty / clean */
8484 if (((dev
->vol
.dirty
& RAIDVOL_DIRTY
) && consistent
) ||
8485 (!(dev
->vol
.dirty
& RAIDVOL_DIRTY
) && !consistent
)) {
8486 dprintf("imsm: mark '%s'\n", consistent
? "clean" : "dirty");
8488 dev
->vol
.dirty
= RAIDVOL_CLEAN
;
8490 dev
->vol
.dirty
= RAIDVOL_DIRTY
;
8491 if (dev
->rwh_policy
== RWH_DISTRIBUTED
||
8492 dev
->rwh_policy
== RWH_MULTIPLE_DISTRIBUTED
)
8493 dev
->vol
.dirty
|= RAIDVOL_DSRECORD_VALID
;
8495 super
->updates_pending
++;
8501 static int imsm_disk_slot_to_ord(struct active_array
*a
, int slot
)
8503 int inst
= a
->info
.container_member
;
8504 struct intel_super
*super
= a
->container
->sb
;
8505 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
8506 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8508 if (slot
> map
->num_members
) {
8509 pr_err("imsm: imsm_disk_slot_to_ord %d out of range 0..%d\n",
8510 slot
, map
->num_members
- 1);
8517 return get_imsm_ord_tbl_ent(dev
, slot
, MAP_0
);
8520 static void imsm_set_disk(struct active_array
*a
, int n
, int state
)
8522 int inst
= a
->info
.container_member
;
8523 struct intel_super
*super
= a
->container
->sb
;
8524 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
8525 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8526 struct imsm_disk
*disk
;
8528 int recovery_not_finished
= 0;
8532 int rebuild_done
= 0;
8535 ord
= get_imsm_ord_tbl_ent(dev
, n
, MAP_X
);
8539 dprintf("imsm: set_disk %d:%x\n", n
, state
);
8540 disk
= get_imsm_disk(super
, ord_to_idx(ord
));
8542 /* check for new failures */
8543 if (disk
&& (state
& DS_FAULTY
)) {
8544 if (mark_failure(super
, dev
, disk
, ord_to_idx(ord
)))
8545 super
->updates_pending
++;
8548 /* check if in_sync */
8549 if (state
& DS_INSYNC
&& ord
& IMSM_ORD_REBUILD
&& is_rebuilding(dev
)) {
8550 struct imsm_map
*migr_map
= get_imsm_map(dev
, MAP_1
);
8552 set_imsm_ord_tbl_ent(migr_map
, n
, ord_to_idx(ord
));
8554 super
->updates_pending
++;
8557 failed
= imsm_count_failed(super
, dev
, MAP_0
);
8558 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
8560 /* check if recovery complete, newly degraded, or failed */
8561 dprintf("imsm: Detected transition to state ");
8562 switch (map_state
) {
8563 case IMSM_T_STATE_NORMAL
: /* transition to normal state */
8564 dprintf("normal: ");
8565 if (is_rebuilding(dev
)) {
8566 dprintf_cont("while rebuilding");
8567 /* check if recovery is really finished */
8568 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
)
8569 if (mdi
->recovery_start
!= MaxSector
) {
8570 recovery_not_finished
= 1;
8573 if (recovery_not_finished
) {
8575 dprintf("Rebuild has not finished yet, state not changed");
8576 if (a
->last_checkpoint
< mdi
->recovery_start
) {
8577 a
->last_checkpoint
= mdi
->recovery_start
;
8578 super
->updates_pending
++;
8582 end_migration(dev
, super
, map_state
);
8583 map
= get_imsm_map(dev
, MAP_0
);
8584 map
->failed_disk_num
= ~0;
8585 super
->updates_pending
++;
8586 a
->last_checkpoint
= 0;
8589 if (is_gen_migration(dev
)) {
8590 dprintf_cont("while general migration");
8591 if (a
->last_checkpoint
>= a
->info
.component_size
)
8592 end_migration(dev
, super
, map_state
);
8594 map
->map_state
= map_state
;
8595 map
= get_imsm_map(dev
, MAP_0
);
8596 map
->failed_disk_num
= ~0;
8597 super
->updates_pending
++;
8601 case IMSM_T_STATE_DEGRADED
: /* transition to degraded state */
8602 dprintf_cont("degraded: ");
8603 if (map
->map_state
!= map_state
&& !dev
->vol
.migr_state
) {
8604 dprintf_cont("mark degraded");
8605 map
->map_state
= map_state
;
8606 super
->updates_pending
++;
8607 a
->last_checkpoint
= 0;
8610 if (is_rebuilding(dev
)) {
8611 dprintf_cont("while rebuilding ");
8612 if (state
& DS_FAULTY
) {
8613 dprintf_cont("removing failed drive ");
8614 if (n
== map
->failed_disk_num
) {
8615 dprintf_cont("end migration");
8616 end_migration(dev
, super
, map_state
);
8617 a
->last_checkpoint
= 0;
8619 dprintf_cont("fail detected during rebuild, changing map state");
8620 map
->map_state
= map_state
;
8622 super
->updates_pending
++;
8628 /* check if recovery is really finished */
8629 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
)
8630 if (mdi
->recovery_start
!= MaxSector
) {
8631 recovery_not_finished
= 1;
8634 if (recovery_not_finished
) {
8636 dprintf_cont("Rebuild has not finished yet");
8637 if (a
->last_checkpoint
< mdi
->recovery_start
) {
8638 a
->last_checkpoint
=
8639 mdi
->recovery_start
;
8640 super
->updates_pending
++;
8645 dprintf_cont(" Rebuild done, still degraded");
8646 end_migration(dev
, super
, map_state
);
8647 a
->last_checkpoint
= 0;
8648 super
->updates_pending
++;
8650 for (i
= 0; i
< map
->num_members
; i
++) {
8651 int idx
= get_imsm_ord_tbl_ent(dev
, i
, MAP_0
);
8653 if (idx
& IMSM_ORD_REBUILD
)
8654 map
->failed_disk_num
= i
;
8656 super
->updates_pending
++;
8659 if (is_gen_migration(dev
)) {
8660 dprintf_cont("while general migration");
8661 if (a
->last_checkpoint
>= a
->info
.component_size
)
8662 end_migration(dev
, super
, map_state
);
8664 map
->map_state
= map_state
;
8665 manage_second_map(super
, dev
);
8667 super
->updates_pending
++;
8670 if (is_initializing(dev
)) {
8671 dprintf_cont("while initialization.");
8672 map
->map_state
= map_state
;
8673 super
->updates_pending
++;
8677 case IMSM_T_STATE_FAILED
: /* transition to failed state */
8678 dprintf_cont("failed: ");
8679 if (is_gen_migration(dev
)) {
8680 dprintf_cont("while general migration");
8681 map
->map_state
= map_state
;
8682 super
->updates_pending
++;
8685 if (map
->map_state
!= map_state
) {
8686 dprintf_cont("mark failed");
8687 end_migration(dev
, super
, map_state
);
8688 super
->updates_pending
++;
8689 a
->last_checkpoint
= 0;
8694 dprintf_cont("state %i\n", map_state
);
8699 static int store_imsm_mpb(int fd
, struct imsm_super
*mpb
)
8702 __u32 mpb_size
= __le32_to_cpu(mpb
->mpb_size
);
8703 unsigned long long dsize
;
8704 unsigned long long sectors
;
8705 unsigned int sector_size
;
8707 get_dev_sector_size(fd
, NULL
, §or_size
);
8708 get_dev_size(fd
, NULL
, &dsize
);
8710 if (mpb_size
> sector_size
) {
8711 /* -1 to account for anchor */
8712 sectors
= mpb_sectors(mpb
, sector_size
) - 1;
8714 /* write the extended mpb to the sectors preceeding the anchor */
8715 if (lseek64(fd
, dsize
- (sector_size
* (2 + sectors
)),
8719 if ((unsigned long long)write(fd
, buf
+ sector_size
,
8720 sector_size
* sectors
) != sector_size
* sectors
)
8724 /* first block is stored on second to last sector of the disk */
8725 if (lseek64(fd
, dsize
- (sector_size
* 2), SEEK_SET
) < 0)
8728 if ((unsigned int)write(fd
, buf
, sector_size
) != sector_size
)
8734 static void imsm_sync_metadata(struct supertype
*container
)
8736 struct intel_super
*super
= container
->sb
;
8738 dprintf("sync metadata: %d\n", super
->updates_pending
);
8739 if (!super
->updates_pending
)
8742 write_super_imsm(container
, 0);
8744 super
->updates_pending
= 0;
8747 static struct dl
*imsm_readd(struct intel_super
*super
, int idx
, struct active_array
*a
)
8749 struct imsm_dev
*dev
= get_imsm_dev(super
, a
->info
.container_member
);
8750 int i
= get_imsm_disk_idx(dev
, idx
, MAP_X
);
8753 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
8757 if (dl
&& is_failed(&dl
->disk
))
8761 dprintf("found %x:%x\n", dl
->major
, dl
->minor
);
8766 static struct dl
*imsm_add_spare(struct intel_super
*super
, int slot
,
8767 struct active_array
*a
, int activate_new
,
8768 struct mdinfo
*additional_test_list
)
8770 struct imsm_dev
*dev
= get_imsm_dev(super
, a
->info
.container_member
);
8771 int idx
= get_imsm_disk_idx(dev
, slot
, MAP_X
);
8772 struct imsm_super
*mpb
= super
->anchor
;
8773 struct imsm_map
*map
;
8774 unsigned long long pos
;
8779 __u32 array_start
= 0;
8780 __u32 array_end
= 0;
8782 struct mdinfo
*test_list
;
8784 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
8785 /* If in this array, skip */
8786 for (d
= a
->info
.devs
; d
; d
= d
->next
)
8787 if (d
->state_fd
>= 0 &&
8788 d
->disk
.major
== dl
->major
&&
8789 d
->disk
.minor
== dl
->minor
) {
8790 dprintf("%x:%x already in array\n",
8791 dl
->major
, dl
->minor
);
8796 test_list
= additional_test_list
;
8798 if (test_list
->disk
.major
== dl
->major
&&
8799 test_list
->disk
.minor
== dl
->minor
) {
8800 dprintf("%x:%x already in additional test list\n",
8801 dl
->major
, dl
->minor
);
8804 test_list
= test_list
->next
;
8809 /* skip in use or failed drives */
8810 if (is_failed(&dl
->disk
) || idx
== dl
->index
||
8812 dprintf("%x:%x status (failed: %d index: %d)\n",
8813 dl
->major
, dl
->minor
, is_failed(&dl
->disk
), idx
);
8817 /* skip pure spares when we are looking for partially
8818 * assimilated drives
8820 if (dl
->index
== -1 && !activate_new
)
8823 if (!drive_validate_sector_size(super
, dl
))
8826 /* Does this unused device have the requisite free space?
8827 * It needs to be able to cover all member volumes
8829 ex
= get_extents(super
, dl
, 1);
8831 dprintf("cannot get extents\n");
8834 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
8835 dev
= get_imsm_dev(super
, i
);
8836 map
= get_imsm_map(dev
, MAP_0
);
8838 /* check if this disk is already a member of
8841 if (get_imsm_disk_slot(map
, dl
->index
) >= 0)
8847 array_start
= pba_of_lba0(map
);
8848 array_end
= array_start
+
8849 per_dev_array_size(map
) - 1;
8852 /* check that we can start at pba_of_lba0 with
8853 * num_data_stripes*blocks_per_stripe of space
8855 if (array_start
>= pos
&& array_end
< ex
[j
].start
) {
8859 pos
= ex
[j
].start
+ ex
[j
].size
;
8861 } while (ex
[j
-1].size
);
8868 if (i
< mpb
->num_raid_devs
) {
8869 dprintf("%x:%x does not have %u to %u available\n",
8870 dl
->major
, dl
->minor
, array_start
, array_end
);
8880 static int imsm_rebuild_allowed(struct supertype
*cont
, int dev_idx
, int failed
)
8882 struct imsm_dev
*dev2
;
8883 struct imsm_map
*map
;
8889 dev2
= get_imsm_dev(cont
->sb
, dev_idx
);
8891 state
= imsm_check_degraded(cont
->sb
, dev2
, failed
, MAP_0
);
8892 if (state
== IMSM_T_STATE_FAILED
) {
8893 map
= get_imsm_map(dev2
, MAP_0
);
8896 for (slot
= 0; slot
< map
->num_members
; slot
++) {
8898 * Check if failed disks are deleted from intel
8899 * disk list or are marked to be deleted
8901 idx
= get_imsm_disk_idx(dev2
, slot
, MAP_X
);
8902 idisk
= get_imsm_dl_disk(cont
->sb
, idx
);
8904 * Do not rebuild the array if failed disks
8905 * from failed sub-array are not removed from
8909 is_failed(&idisk
->disk
) &&
8910 (idisk
->action
!= DISK_REMOVE
))
8918 static struct mdinfo
*imsm_activate_spare(struct active_array
*a
,
8919 struct metadata_update
**updates
)
8922 * Find a device with unused free space and use it to replace a
8923 * failed/vacant region in an array. We replace failed regions one a
8924 * array at a time. The result is that a new spare disk will be added
8925 * to the first failed array and after the monitor has finished
8926 * propagating failures the remainder will be consumed.
8928 * FIXME add a capability for mdmon to request spares from another
8932 struct intel_super
*super
= a
->container
->sb
;
8933 int inst
= a
->info
.container_member
;
8934 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
8935 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8936 int failed
= a
->info
.array
.raid_disks
;
8937 struct mdinfo
*rv
= NULL
;
8940 struct metadata_update
*mu
;
8942 struct imsm_update_activate_spare
*u
;
8947 for (d
= a
->info
.devs
; d
; d
= d
->next
) {
8948 if ((d
->curr_state
& DS_FAULTY
) &&
8950 /* wait for Removal to happen */
8952 if (d
->state_fd
>= 0)
8956 dprintf("imsm: activate spare: inst=%d failed=%d (%d) level=%d\n",
8957 inst
, failed
, a
->info
.array
.raid_disks
, a
->info
.array
.level
);
8959 if (imsm_reshape_blocks_arrays_changes(super
))
8962 /* Cannot activate another spare if rebuild is in progress already
8964 if (is_rebuilding(dev
)) {
8965 dprintf("imsm: No spare activation allowed. Rebuild in progress already.\n");
8969 if (a
->info
.array
.level
== 4)
8970 /* No repair for takeovered array
8971 * imsm doesn't support raid4
8975 if (imsm_check_degraded(super
, dev
, failed
, MAP_0
) !=
8976 IMSM_T_STATE_DEGRADED
)
8979 if (get_imsm_map(dev
, MAP_0
)->map_state
== IMSM_T_STATE_UNINITIALIZED
) {
8980 dprintf("imsm: No spare activation allowed. Volume is not initialized.\n");
8985 * If there are any failed disks check state of the other volume.
8986 * Block rebuild if the another one is failed until failed disks
8987 * are removed from container.
8990 dprintf("found failed disks in %.*s, check if there anotherfailed sub-array.\n",
8991 MAX_RAID_SERIAL_LEN
, dev
->volume
);
8992 /* check if states of the other volumes allow for rebuild */
8993 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
8995 allowed
= imsm_rebuild_allowed(a
->container
,
9003 /* For each slot, if it is not working, find a spare */
9004 for (i
= 0; i
< a
->info
.array
.raid_disks
; i
++) {
9005 for (d
= a
->info
.devs
; d
; d
= d
->next
)
9006 if (d
->disk
.raid_disk
== i
)
9008 dprintf("found %d: %p %x\n", i
, d
, d
?d
->curr_state
:0);
9009 if (d
&& (d
->state_fd
>= 0))
9013 * OK, this device needs recovery. Try to re-add the
9014 * previous occupant of this slot, if this fails see if
9015 * we can continue the assimilation of a spare that was
9016 * partially assimilated, finally try to activate a new
9019 dl
= imsm_readd(super
, i
, a
);
9021 dl
= imsm_add_spare(super
, i
, a
, 0, rv
);
9023 dl
= imsm_add_spare(super
, i
, a
, 1, rv
);
9027 /* found a usable disk with enough space */
9028 di
= xcalloc(1, sizeof(*di
));
9030 /* dl->index will be -1 in the case we are activating a
9031 * pristine spare. imsm_process_update() will create a
9032 * new index in this case. Once a disk is found to be
9033 * failed in all member arrays it is kicked from the
9036 di
->disk
.number
= dl
->index
;
9038 /* (ab)use di->devs to store a pointer to the device
9041 di
->devs
= (struct mdinfo
*) dl
;
9043 di
->disk
.raid_disk
= i
;
9044 di
->disk
.major
= dl
->major
;
9045 di
->disk
.minor
= dl
->minor
;
9047 di
->recovery_start
= 0;
9048 di
->data_offset
= pba_of_lba0(map
);
9049 di
->component_size
= a
->info
.component_size
;
9050 di
->container_member
= inst
;
9051 di
->bb
.supported
= 1;
9052 if (a
->info
.consistency_policy
== CONSISTENCY_POLICY_PPL
) {
9053 di
->ppl_sector
= get_ppl_sector(super
, inst
);
9054 di
->ppl_size
= MULTIPLE_PPL_AREA_SIZE_IMSM
>> 9;
9056 super
->random
= random32();
9060 dprintf("%x:%x to be %d at %llu\n", dl
->major
, dl
->minor
,
9061 i
, di
->data_offset
);
9065 /* No spares found */
9067 /* Now 'rv' has a list of devices to return.
9068 * Create a metadata_update record to update the
9069 * disk_ord_tbl for the array
9071 mu
= xmalloc(sizeof(*mu
));
9072 mu
->buf
= xcalloc(num_spares
,
9073 sizeof(struct imsm_update_activate_spare
));
9075 mu
->space_list
= NULL
;
9076 mu
->len
= sizeof(struct imsm_update_activate_spare
) * num_spares
;
9077 mu
->next
= *updates
;
9078 u
= (struct imsm_update_activate_spare
*) mu
->buf
;
9080 for (di
= rv
; di
; di
= di
->next
) {
9081 u
->type
= update_activate_spare
;
9082 u
->dl
= (struct dl
*) di
->devs
;
9084 u
->slot
= di
->disk
.raid_disk
;
9095 static int disks_overlap(struct intel_super
*super
, int idx
, struct imsm_update_create_array
*u
)
9097 struct imsm_dev
*dev
= get_imsm_dev(super
, idx
);
9098 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
9099 struct imsm_map
*new_map
= get_imsm_map(&u
->dev
, MAP_0
);
9100 struct disk_info
*inf
= get_disk_info(u
);
9101 struct imsm_disk
*disk
;
9105 for (i
= 0; i
< map
->num_members
; i
++) {
9106 disk
= get_imsm_disk(super
, get_imsm_disk_idx(dev
, i
, MAP_X
));
9107 for (j
= 0; j
< new_map
->num_members
; j
++)
9108 if (serialcmp(disk
->serial
, inf
[j
].serial
) == 0)
9115 static struct dl
*get_disk_super(struct intel_super
*super
, int major
, int minor
)
9119 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
9120 if (dl
->major
== major
&& dl
->minor
== minor
)
9125 static int remove_disk_super(struct intel_super
*super
, int major
, int minor
)
9131 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
9132 if (dl
->major
== major
&& dl
->minor
== minor
) {
9135 prev
->next
= dl
->next
;
9137 super
->disks
= dl
->next
;
9139 __free_imsm_disk(dl
);
9140 dprintf("removed %x:%x\n", major
, minor
);
9148 static void imsm_delete(struct intel_super
*super
, struct dl
**dlp
, unsigned index
);
9150 static int add_remove_disk_update(struct intel_super
*super
)
9152 int check_degraded
= 0;
9155 /* add/remove some spares to/from the metadata/contrainer */
9156 while (super
->disk_mgmt_list
) {
9157 struct dl
*disk_cfg
;
9159 disk_cfg
= super
->disk_mgmt_list
;
9160 super
->disk_mgmt_list
= disk_cfg
->next
;
9161 disk_cfg
->next
= NULL
;
9163 if (disk_cfg
->action
== DISK_ADD
) {
9164 disk_cfg
->next
= super
->disks
;
9165 super
->disks
= disk_cfg
;
9167 dprintf("added %x:%x\n",
9168 disk_cfg
->major
, disk_cfg
->minor
);
9169 } else if (disk_cfg
->action
== DISK_REMOVE
) {
9170 dprintf("Disk remove action processed: %x.%x\n",
9171 disk_cfg
->major
, disk_cfg
->minor
);
9172 disk
= get_disk_super(super
,
9176 /* store action status */
9177 disk
->action
= DISK_REMOVE
;
9178 /* remove spare disks only */
9179 if (disk
->index
== -1) {
9180 remove_disk_super(super
,
9184 disk_cfg
->fd
= disk
->fd
;
9188 /* release allocate disk structure */
9189 __free_imsm_disk(disk_cfg
);
9192 return check_degraded
;
9195 static int apply_reshape_migration_update(struct imsm_update_reshape_migration
*u
,
9196 struct intel_super
*super
,
9199 struct intel_dev
*id
;
9200 void **tofree
= NULL
;
9203 dprintf("(enter)\n");
9204 if (u
->subdev
< 0 || u
->subdev
> 1) {
9205 dprintf("imsm: Error: Wrong subdev: %i\n", u
->subdev
);
9208 if (space_list
== NULL
|| *space_list
== NULL
) {
9209 dprintf("imsm: Error: Memory is not allocated\n");
9213 for (id
= super
->devlist
; id
; id
= id
->next
) {
9214 if (id
->index
== (unsigned)u
->subdev
) {
9215 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->subdev
);
9216 struct imsm_map
*map
;
9217 struct imsm_dev
*new_dev
=
9218 (struct imsm_dev
*)*space_list
;
9219 struct imsm_map
*migr_map
= get_imsm_map(dev
, MAP_1
);
9221 struct dl
*new_disk
;
9223 if (new_dev
== NULL
)
9225 *space_list
= **space_list
;
9226 memcpy(new_dev
, dev
, sizeof_imsm_dev(dev
, 0));
9227 map
= get_imsm_map(new_dev
, MAP_0
);
9229 dprintf("imsm: Error: migration in progress");
9233 to_state
= map
->map_state
;
9234 if ((u
->new_level
== 5) && (map
->raid_level
== 0)) {
9236 /* this should not happen */
9237 if (u
->new_disks
[0] < 0) {
9238 map
->failed_disk_num
=
9239 map
->num_members
- 1;
9240 to_state
= IMSM_T_STATE_DEGRADED
;
9242 to_state
= IMSM_T_STATE_NORMAL
;
9244 migrate(new_dev
, super
, to_state
, MIGR_GEN_MIGR
);
9245 if (u
->new_level
> -1)
9246 map
->raid_level
= u
->new_level
;
9247 migr_map
= get_imsm_map(new_dev
, MAP_1
);
9248 if ((u
->new_level
== 5) &&
9249 (migr_map
->raid_level
== 0)) {
9250 int ord
= map
->num_members
- 1;
9251 migr_map
->num_members
--;
9252 if (u
->new_disks
[0] < 0)
9253 ord
|= IMSM_ORD_REBUILD
;
9254 set_imsm_ord_tbl_ent(map
,
9255 map
->num_members
- 1,
9259 tofree
= (void **)dev
;
9261 /* update chunk size
9263 if (u
->new_chunksize
> 0) {
9264 unsigned long long num_data_stripes
;
9265 struct imsm_map
*dest_map
=
9266 get_imsm_map(dev
, MAP_0
);
9268 imsm_num_data_members(dest_map
);
9270 if (used_disks
== 0)
9273 map
->blocks_per_strip
=
9274 __cpu_to_le16(u
->new_chunksize
* 2);
9276 imsm_dev_size(dev
) / used_disks
;
9277 num_data_stripes
/= map
->blocks_per_strip
;
9278 num_data_stripes
/= map
->num_domains
;
9279 set_num_data_stripes(map
, num_data_stripes
);
9282 /* ensure blocks_per_member has valid value
9284 set_blocks_per_member(map
,
9285 per_dev_array_size(map
) +
9286 NUM_BLOCKS_DIRTY_STRIPE_REGION
);
9290 if (u
->new_level
!= 5 || migr_map
->raid_level
!= 0 ||
9291 migr_map
->raid_level
== map
->raid_level
)
9294 if (u
->new_disks
[0] >= 0) {
9297 new_disk
= get_disk_super(super
,
9298 major(u
->new_disks
[0]),
9299 minor(u
->new_disks
[0]));
9300 dprintf("imsm: new disk for reshape is: %i:%i (%p, index = %i)\n",
9301 major(u
->new_disks
[0]),
9302 minor(u
->new_disks
[0]),
9303 new_disk
, new_disk
->index
);
9304 if (new_disk
== NULL
)
9305 goto error_disk_add
;
9307 new_disk
->index
= map
->num_members
- 1;
9308 /* slot to fill in autolayout
9310 new_disk
->raiddisk
= new_disk
->index
;
9311 new_disk
->disk
.status
|= CONFIGURED_DISK
;
9312 new_disk
->disk
.status
&= ~SPARE_DISK
;
9314 goto error_disk_add
;
9317 *tofree
= *space_list
;
9318 /* calculate new size
9320 imsm_set_array_size(new_dev
, -1);
9327 *space_list
= tofree
;
9331 dprintf("Error: imsm: Cannot find disk.\n");
9335 static int apply_size_change_update(struct imsm_update_size_change
*u
,
9336 struct intel_super
*super
)
9338 struct intel_dev
*id
;
9341 dprintf("(enter)\n");
9342 if (u
->subdev
< 0 || u
->subdev
> 1) {
9343 dprintf("imsm: Error: Wrong subdev: %i\n", u
->subdev
);
9347 for (id
= super
->devlist
; id
; id
= id
->next
) {
9348 if (id
->index
== (unsigned)u
->subdev
) {
9349 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->subdev
);
9350 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
9351 int used_disks
= imsm_num_data_members(map
);
9352 unsigned long long blocks_per_member
;
9353 unsigned long long num_data_stripes
;
9354 unsigned long long new_size_per_disk
;
9356 if (used_disks
== 0)
9359 /* calculate new size
9361 new_size_per_disk
= u
->new_size
/ used_disks
;
9362 blocks_per_member
= new_size_per_disk
+
9363 NUM_BLOCKS_DIRTY_STRIPE_REGION
;
9364 num_data_stripes
= new_size_per_disk
/
9365 map
->blocks_per_strip
;
9366 num_data_stripes
/= map
->num_domains
;
9367 dprintf("(size: %llu, blocks per member: %llu, num_data_stipes: %llu)\n",
9368 u
->new_size
, new_size_per_disk
,
9370 set_blocks_per_member(map
, blocks_per_member
);
9371 set_num_data_stripes(map
, num_data_stripes
);
9372 imsm_set_array_size(dev
, u
->new_size
);
9382 static int apply_update_activate_spare(struct imsm_update_activate_spare
*u
,
9383 struct intel_super
*super
,
9384 struct active_array
*active_array
)
9386 struct imsm_super
*mpb
= super
->anchor
;
9387 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->array
);
9388 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
9389 struct imsm_map
*migr_map
;
9390 struct active_array
*a
;
9391 struct imsm_disk
*disk
;
9398 int second_map_created
= 0;
9400 for (; u
; u
= u
->next
) {
9401 victim
= get_imsm_disk_idx(dev
, u
->slot
, MAP_X
);
9406 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
9411 pr_err("error: imsm_activate_spare passed an unknown disk (index: %d)\n",
9416 /* count failures (excluding rebuilds and the victim)
9417 * to determine map[0] state
9420 for (i
= 0; i
< map
->num_members
; i
++) {
9423 disk
= get_imsm_disk(super
,
9424 get_imsm_disk_idx(dev
, i
, MAP_X
));
9425 if (!disk
|| is_failed(disk
))
9429 /* adding a pristine spare, assign a new index */
9430 if (dl
->index
< 0) {
9431 dl
->index
= super
->anchor
->num_disks
;
9432 super
->anchor
->num_disks
++;
9435 disk
->status
|= CONFIGURED_DISK
;
9436 disk
->status
&= ~SPARE_DISK
;
9439 to_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
9440 if (!second_map_created
) {
9441 second_map_created
= 1;
9442 map
->map_state
= IMSM_T_STATE_DEGRADED
;
9443 migrate(dev
, super
, to_state
, MIGR_REBUILD
);
9445 map
->map_state
= to_state
;
9446 migr_map
= get_imsm_map(dev
, MAP_1
);
9447 set_imsm_ord_tbl_ent(map
, u
->slot
, dl
->index
);
9448 set_imsm_ord_tbl_ent(migr_map
, u
->slot
,
9449 dl
->index
| IMSM_ORD_REBUILD
);
9451 /* update the family_num to mark a new container
9452 * generation, being careful to record the existing
9453 * family_num in orig_family_num to clean up after
9454 * earlier mdadm versions that neglected to set it.
9456 if (mpb
->orig_family_num
== 0)
9457 mpb
->orig_family_num
= mpb
->family_num
;
9458 mpb
->family_num
+= super
->random
;
9460 /* count arrays using the victim in the metadata */
9462 for (a
= active_array
; a
; a
= a
->next
) {
9463 dev
= get_imsm_dev(super
, a
->info
.container_member
);
9464 map
= get_imsm_map(dev
, MAP_0
);
9466 if (get_imsm_disk_slot(map
, victim
) >= 0)
9470 /* delete the victim if it is no longer being
9476 /* We know that 'manager' isn't touching anything,
9477 * so it is safe to delete
9479 for (dlp
= &super
->disks
; *dlp
; dlp
= &(*dlp
)->next
)
9480 if ((*dlp
)->index
== victim
)
9483 /* victim may be on the missing list */
9485 for (dlp
= &super
->missing
; *dlp
;
9486 dlp
= &(*dlp
)->next
)
9487 if ((*dlp
)->index
== victim
)
9489 imsm_delete(super
, dlp
, victim
);
9496 static int apply_reshape_container_disks_update(struct imsm_update_reshape
*u
,
9497 struct intel_super
*super
,
9500 struct dl
*new_disk
;
9501 struct intel_dev
*id
;
9503 int delta_disks
= u
->new_raid_disks
- u
->old_raid_disks
;
9504 int disk_count
= u
->old_raid_disks
;
9505 void **tofree
= NULL
;
9506 int devices_to_reshape
= 1;
9507 struct imsm_super
*mpb
= super
->anchor
;
9509 unsigned int dev_id
;
9511 dprintf("(enter)\n");
9513 /* enable spares to use in array */
9514 for (i
= 0; i
< delta_disks
; i
++) {
9515 new_disk
= get_disk_super(super
,
9516 major(u
->new_disks
[i
]),
9517 minor(u
->new_disks
[i
]));
9518 dprintf("imsm: new disk for reshape is: %i:%i (%p, index = %i)\n",
9519 major(u
->new_disks
[i
]), minor(u
->new_disks
[i
]),
9520 new_disk
, new_disk
->index
);
9521 if (new_disk
== NULL
||
9522 (new_disk
->index
>= 0 &&
9523 new_disk
->index
< u
->old_raid_disks
))
9524 goto update_reshape_exit
;
9525 new_disk
->index
= disk_count
++;
9526 /* slot to fill in autolayout
9528 new_disk
->raiddisk
= new_disk
->index
;
9529 new_disk
->disk
.status
|=
9531 new_disk
->disk
.status
&= ~SPARE_DISK
;
9534 dprintf("imsm: volume set mpb->num_raid_devs = %i\n",
9535 mpb
->num_raid_devs
);
9536 /* manage changes in volume
9538 for (dev_id
= 0; dev_id
< mpb
->num_raid_devs
; dev_id
++) {
9539 void **sp
= *space_list
;
9540 struct imsm_dev
*newdev
;
9541 struct imsm_map
*newmap
, *oldmap
;
9543 for (id
= super
->devlist
; id
; id
= id
->next
) {
9544 if (id
->index
== dev_id
)
9553 /* Copy the dev, but not (all of) the map */
9554 memcpy(newdev
, id
->dev
, sizeof(*newdev
));
9555 oldmap
= get_imsm_map(id
->dev
, MAP_0
);
9556 newmap
= get_imsm_map(newdev
, MAP_0
);
9557 /* Copy the current map */
9558 memcpy(newmap
, oldmap
, sizeof_imsm_map(oldmap
));
9559 /* update one device only
9561 if (devices_to_reshape
) {
9562 dprintf("imsm: modifying subdev: %i\n",
9564 devices_to_reshape
--;
9565 newdev
->vol
.migr_state
= 1;
9566 newdev
->vol
.curr_migr_unit
= 0;
9567 set_migr_type(newdev
, MIGR_GEN_MIGR
);
9568 newmap
->num_members
= u
->new_raid_disks
;
9569 for (i
= 0; i
< delta_disks
; i
++) {
9570 set_imsm_ord_tbl_ent(newmap
,
9571 u
->old_raid_disks
+ i
,
9572 u
->old_raid_disks
+ i
);
9574 /* New map is correct, now need to save old map
9576 newmap
= get_imsm_map(newdev
, MAP_1
);
9577 memcpy(newmap
, oldmap
, sizeof_imsm_map(oldmap
));
9579 imsm_set_array_size(newdev
, -1);
9582 sp
= (void **)id
->dev
;
9587 /* Clear migration record */
9588 memset(super
->migr_rec
, 0, sizeof(struct migr_record
));
9591 *space_list
= tofree
;
9594 update_reshape_exit
:
9599 static int apply_takeover_update(struct imsm_update_takeover
*u
,
9600 struct intel_super
*super
,
9603 struct imsm_dev
*dev
= NULL
;
9604 struct intel_dev
*dv
;
9605 struct imsm_dev
*dev_new
;
9606 struct imsm_map
*map
;
9610 for (dv
= super
->devlist
; dv
; dv
= dv
->next
)
9611 if (dv
->index
== (unsigned int)u
->subarray
) {
9619 map
= get_imsm_map(dev
, MAP_0
);
9621 if (u
->direction
== R10_TO_R0
) {
9622 unsigned long long num_data_stripes
;
9624 /* Number of failed disks must be half of initial disk number */
9625 if (imsm_count_failed(super
, dev
, MAP_0
) !=
9626 (map
->num_members
/ 2))
9629 /* iterate through devices to mark removed disks as spare */
9630 for (dm
= super
->disks
; dm
; dm
= dm
->next
) {
9631 if (dm
->disk
.status
& FAILED_DISK
) {
9632 int idx
= dm
->index
;
9633 /* update indexes on the disk list */
9634 /* FIXME this loop-with-the-loop looks wrong, I'm not convinced
9635 the index values will end up being correct.... NB */
9636 for (du
= super
->disks
; du
; du
= du
->next
)
9637 if (du
->index
> idx
)
9639 /* mark as spare disk */
9644 map
->num_members
= map
->num_members
/ 2;
9645 map
->map_state
= IMSM_T_STATE_NORMAL
;
9646 map
->num_domains
= 1;
9647 map
->raid_level
= 0;
9648 map
->failed_disk_num
= -1;
9649 num_data_stripes
= imsm_dev_size(dev
) / 2;
9650 num_data_stripes
/= map
->blocks_per_strip
;
9651 set_num_data_stripes(map
, num_data_stripes
);
9654 if (u
->direction
== R0_TO_R10
) {
9656 unsigned long long num_data_stripes
;
9658 /* update slots in current disk list */
9659 for (dm
= super
->disks
; dm
; dm
= dm
->next
) {
9663 /* create new *missing* disks */
9664 for (i
= 0; i
< map
->num_members
; i
++) {
9665 space
= *space_list
;
9668 *space_list
= *space
;
9670 memcpy(du
, super
->disks
, sizeof(*du
));
9674 du
->index
= (i
* 2) + 1;
9675 sprintf((char *)du
->disk
.serial
,
9676 " MISSING_%d", du
->index
);
9677 sprintf((char *)du
->serial
,
9678 "MISSING_%d", du
->index
);
9679 du
->next
= super
->missing
;
9680 super
->missing
= du
;
9682 /* create new dev and map */
9683 space
= *space_list
;
9686 *space_list
= *space
;
9687 dev_new
= (void *)space
;
9688 memcpy(dev_new
, dev
, sizeof(*dev
));
9689 /* update new map */
9690 map
= get_imsm_map(dev_new
, MAP_0
);
9691 map
->num_members
= map
->num_members
* 2;
9692 map
->map_state
= IMSM_T_STATE_DEGRADED
;
9693 map
->num_domains
= 2;
9694 map
->raid_level
= 1;
9695 num_data_stripes
= imsm_dev_size(dev
) / 2;
9696 num_data_stripes
/= map
->blocks_per_strip
;
9697 num_data_stripes
/= map
->num_domains
;
9698 set_num_data_stripes(map
, num_data_stripes
);
9700 /* replace dev<->dev_new */
9703 /* update disk order table */
9704 for (du
= super
->disks
; du
; du
= du
->next
)
9706 set_imsm_ord_tbl_ent(map
, du
->index
, du
->index
);
9707 for (du
= super
->missing
; du
; du
= du
->next
)
9708 if (du
->index
>= 0) {
9709 set_imsm_ord_tbl_ent(map
, du
->index
, du
->index
);
9710 mark_missing(super
, dv
->dev
, &du
->disk
, du
->index
);
9716 static void imsm_process_update(struct supertype
*st
,
9717 struct metadata_update
*update
)
9720 * crack open the metadata_update envelope to find the update record
9721 * update can be one of:
9722 * update_reshape_container_disks - all the arrays in the container
9723 * are being reshaped to have more devices. We need to mark
9724 * the arrays for general migration and convert selected spares
9725 * into active devices.
9726 * update_activate_spare - a spare device has replaced a failed
9727 * device in an array, update the disk_ord_tbl. If this disk is
9728 * present in all member arrays then also clear the SPARE_DISK
9730 * update_create_array
9732 * update_rename_array
9733 * update_add_remove_disk
9735 struct intel_super
*super
= st
->sb
;
9736 struct imsm_super
*mpb
;
9737 enum imsm_update_type type
= *(enum imsm_update_type
*) update
->buf
;
9739 /* update requires a larger buf but the allocation failed */
9740 if (super
->next_len
&& !super
->next_buf
) {
9741 super
->next_len
= 0;
9745 if (super
->next_buf
) {
9746 memcpy(super
->next_buf
, super
->buf
, super
->len
);
9748 super
->len
= super
->next_len
;
9749 super
->buf
= super
->next_buf
;
9751 super
->next_len
= 0;
9752 super
->next_buf
= NULL
;
9755 mpb
= super
->anchor
;
9758 case update_general_migration_checkpoint
: {
9759 struct intel_dev
*id
;
9760 struct imsm_update_general_migration_checkpoint
*u
=
9761 (void *)update
->buf
;
9763 dprintf("called for update_general_migration_checkpoint\n");
9765 /* find device under general migration */
9766 for (id
= super
->devlist
; id
; id
= id
->next
) {
9767 if (is_gen_migration(id
->dev
)) {
9768 id
->dev
->vol
.curr_migr_unit
=
9769 __cpu_to_le32(u
->curr_migr_unit
);
9770 super
->updates_pending
++;
9775 case update_takeover
: {
9776 struct imsm_update_takeover
*u
= (void *)update
->buf
;
9777 if (apply_takeover_update(u
, super
, &update
->space_list
)) {
9778 imsm_update_version_info(super
);
9779 super
->updates_pending
++;
9784 case update_reshape_container_disks
: {
9785 struct imsm_update_reshape
*u
= (void *)update
->buf
;
9786 if (apply_reshape_container_disks_update(
9787 u
, super
, &update
->space_list
))
9788 super
->updates_pending
++;
9791 case update_reshape_migration
: {
9792 struct imsm_update_reshape_migration
*u
= (void *)update
->buf
;
9793 if (apply_reshape_migration_update(
9794 u
, super
, &update
->space_list
))
9795 super
->updates_pending
++;
9798 case update_size_change
: {
9799 struct imsm_update_size_change
*u
= (void *)update
->buf
;
9800 if (apply_size_change_update(u
, super
))
9801 super
->updates_pending
++;
9804 case update_activate_spare
: {
9805 struct imsm_update_activate_spare
*u
= (void *) update
->buf
;
9806 if (apply_update_activate_spare(u
, super
, st
->arrays
))
9807 super
->updates_pending
++;
9810 case update_create_array
: {
9811 /* someone wants to create a new array, we need to be aware of
9812 * a few races/collisions:
9813 * 1/ 'Create' called by two separate instances of mdadm
9814 * 2/ 'Create' versus 'activate_spare': mdadm has chosen
9815 * devices that have since been assimilated via
9817 * In the event this update can not be carried out mdadm will
9818 * (FIX ME) notice that its update did not take hold.
9820 struct imsm_update_create_array
*u
= (void *) update
->buf
;
9821 struct intel_dev
*dv
;
9822 struct imsm_dev
*dev
;
9823 struct imsm_map
*map
, *new_map
;
9824 unsigned long long start
, end
;
9825 unsigned long long new_start
, new_end
;
9827 struct disk_info
*inf
;
9830 /* handle racing creates: first come first serve */
9831 if (u
->dev_idx
< mpb
->num_raid_devs
) {
9832 dprintf("subarray %d already defined\n", u
->dev_idx
);
9836 /* check update is next in sequence */
9837 if (u
->dev_idx
!= mpb
->num_raid_devs
) {
9838 dprintf("can not create array %d expected index %d\n",
9839 u
->dev_idx
, mpb
->num_raid_devs
);
9843 new_map
= get_imsm_map(&u
->dev
, MAP_0
);
9844 new_start
= pba_of_lba0(new_map
);
9845 new_end
= new_start
+ per_dev_array_size(new_map
);
9846 inf
= get_disk_info(u
);
9848 /* handle activate_spare versus create race:
9849 * check to make sure that overlapping arrays do not include
9852 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
9853 dev
= get_imsm_dev(super
, i
);
9854 map
= get_imsm_map(dev
, MAP_0
);
9855 start
= pba_of_lba0(map
);
9856 end
= start
+ per_dev_array_size(map
);
9857 if ((new_start
>= start
&& new_start
<= end
) ||
9858 (start
>= new_start
&& start
<= new_end
))
9863 if (disks_overlap(super
, i
, u
)) {
9864 dprintf("arrays overlap\n");
9869 /* check that prepare update was successful */
9870 if (!update
->space
) {
9871 dprintf("prepare update failed\n");
9875 /* check that all disks are still active before committing
9876 * changes. FIXME: could we instead handle this by creating a
9877 * degraded array? That's probably not what the user expects,
9878 * so better to drop this update on the floor.
9880 for (i
= 0; i
< new_map
->num_members
; i
++) {
9881 dl
= serial_to_dl(inf
[i
].serial
, super
);
9883 dprintf("disk disappeared\n");
9888 super
->updates_pending
++;
9890 /* convert spares to members and fixup ord_tbl */
9891 for (i
= 0; i
< new_map
->num_members
; i
++) {
9892 dl
= serial_to_dl(inf
[i
].serial
, super
);
9893 if (dl
->index
== -1) {
9894 dl
->index
= mpb
->num_disks
;
9896 dl
->disk
.status
|= CONFIGURED_DISK
;
9897 dl
->disk
.status
&= ~SPARE_DISK
;
9899 set_imsm_ord_tbl_ent(new_map
, i
, dl
->index
);
9904 update
->space
= NULL
;
9905 imsm_copy_dev(dev
, &u
->dev
);
9906 dv
->index
= u
->dev_idx
;
9907 dv
->next
= super
->devlist
;
9908 super
->devlist
= dv
;
9909 mpb
->num_raid_devs
++;
9911 imsm_update_version_info(super
);
9914 /* mdmon knows how to release update->space, but not
9915 * ((struct intel_dev *) update->space)->dev
9917 if (update
->space
) {
9923 case update_kill_array
: {
9924 struct imsm_update_kill_array
*u
= (void *) update
->buf
;
9925 int victim
= u
->dev_idx
;
9926 struct active_array
*a
;
9927 struct intel_dev
**dp
;
9928 struct imsm_dev
*dev
;
9930 /* sanity check that we are not affecting the uuid of
9931 * active arrays, or deleting an active array
9933 * FIXME when immutable ids are available, but note that
9934 * we'll also need to fixup the invalidated/active
9935 * subarray indexes in mdstat
9937 for (a
= st
->arrays
; a
; a
= a
->next
)
9938 if (a
->info
.container_member
>= victim
)
9940 /* by definition if mdmon is running at least one array
9941 * is active in the container, so checking
9942 * mpb->num_raid_devs is just extra paranoia
9944 dev
= get_imsm_dev(super
, victim
);
9945 if (a
|| !dev
|| mpb
->num_raid_devs
== 1) {
9946 dprintf("failed to delete subarray-%d\n", victim
);
9950 for (dp
= &super
->devlist
; *dp
;)
9951 if ((*dp
)->index
== (unsigned)super
->current_vol
) {
9954 if ((*dp
)->index
> (unsigned)victim
)
9958 mpb
->num_raid_devs
--;
9959 super
->updates_pending
++;
9962 case update_rename_array
: {
9963 struct imsm_update_rename_array
*u
= (void *) update
->buf
;
9964 char name
[MAX_RAID_SERIAL_LEN
+1];
9965 int target
= u
->dev_idx
;
9966 struct active_array
*a
;
9967 struct imsm_dev
*dev
;
9969 /* sanity check that we are not affecting the uuid of
9972 memset(name
, 0, sizeof(name
));
9973 snprintf(name
, MAX_RAID_SERIAL_LEN
, "%s", (char *) u
->name
);
9974 name
[MAX_RAID_SERIAL_LEN
] = '\0';
9975 for (a
= st
->arrays
; a
; a
= a
->next
)
9976 if (a
->info
.container_member
== target
)
9978 dev
= get_imsm_dev(super
, u
->dev_idx
);
9979 if (a
|| !dev
|| !check_name(super
, name
, 1)) {
9980 dprintf("failed to rename subarray-%d\n", target
);
9984 memcpy(dev
->volume
, name
, MAX_RAID_SERIAL_LEN
);
9985 super
->updates_pending
++;
9988 case update_add_remove_disk
: {
9989 /* we may be able to repair some arrays if disks are
9990 * being added, check the status of add_remove_disk
9991 * if discs has been added.
9993 if (add_remove_disk_update(super
)) {
9994 struct active_array
*a
;
9996 super
->updates_pending
++;
9997 for (a
= st
->arrays
; a
; a
= a
->next
)
9998 a
->check_degraded
= 1;
10002 case update_prealloc_badblocks_mem
:
10004 case update_rwh_policy
: {
10005 struct imsm_update_rwh_policy
*u
= (void *)update
->buf
;
10006 int target
= u
->dev_idx
;
10007 struct imsm_dev
*dev
= get_imsm_dev(super
, target
);
10009 dprintf("could not find subarray-%d\n", target
);
10013 if (dev
->rwh_policy
!= u
->new_policy
) {
10014 dev
->rwh_policy
= u
->new_policy
;
10015 super
->updates_pending
++;
10020 pr_err("error: unsupported process update type:(type: %d)\n", type
);
10024 static struct mdinfo
*get_spares_for_grow(struct supertype
*st
);
10026 static int imsm_prepare_update(struct supertype
*st
,
10027 struct metadata_update
*update
)
10030 * Allocate space to hold new disk entries, raid-device entries or a new
10031 * mpb if necessary. The manager synchronously waits for updates to
10032 * complete in the monitor, so new mpb buffers allocated here can be
10033 * integrated by the monitor thread without worrying about live pointers
10034 * in the manager thread.
10036 enum imsm_update_type type
;
10037 struct intel_super
*super
= st
->sb
;
10038 unsigned int sector_size
= super
->sector_size
;
10039 struct imsm_super
*mpb
= super
->anchor
;
10043 if (update
->len
< (int)sizeof(type
))
10046 type
= *(enum imsm_update_type
*) update
->buf
;
10049 case update_general_migration_checkpoint
:
10050 if (update
->len
< (int)sizeof(struct imsm_update_general_migration_checkpoint
))
10052 dprintf("called for update_general_migration_checkpoint\n");
10054 case update_takeover
: {
10055 struct imsm_update_takeover
*u
= (void *)update
->buf
;
10056 if (update
->len
< (int)sizeof(*u
))
10058 if (u
->direction
== R0_TO_R10
) {
10059 void **tail
= (void **)&update
->space_list
;
10060 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->subarray
);
10061 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
10062 int num_members
= map
->num_members
;
10065 /* allocate memory for added disks */
10066 for (i
= 0; i
< num_members
; i
++) {
10067 size
= sizeof(struct dl
);
10068 space
= xmalloc(size
);
10073 /* allocate memory for new device */
10074 size
= sizeof_imsm_dev(super
->devlist
->dev
, 0) +
10075 (num_members
* sizeof(__u32
));
10076 space
= xmalloc(size
);
10080 len
= disks_to_mpb_size(num_members
* 2);
10085 case update_reshape_container_disks
: {
10086 /* Every raid device in the container is about to
10087 * gain some more devices, and we will enter a
10089 * So each 'imsm_map' will be bigger, and the imsm_vol
10090 * will now hold 2 of them.
10091 * Thus we need new 'struct imsm_dev' allocations sized
10092 * as sizeof_imsm_dev but with more devices in both maps.
10094 struct imsm_update_reshape
*u
= (void *)update
->buf
;
10095 struct intel_dev
*dl
;
10096 void **space_tail
= (void**)&update
->space_list
;
10098 if (update
->len
< (int)sizeof(*u
))
10101 dprintf("for update_reshape\n");
10103 for (dl
= super
->devlist
; dl
; dl
= dl
->next
) {
10104 int size
= sizeof_imsm_dev(dl
->dev
, 1);
10106 if (u
->new_raid_disks
> u
->old_raid_disks
)
10107 size
+= sizeof(__u32
)*2*
10108 (u
->new_raid_disks
- u
->old_raid_disks
);
10112 *space_tail
= NULL
;
10115 len
= disks_to_mpb_size(u
->new_raid_disks
);
10116 dprintf("New anchor length is %llu\n", (unsigned long long)len
);
10119 case update_reshape_migration
: {
10120 /* for migration level 0->5 we need to add disks
10121 * so the same as for container operation we will copy
10122 * device to the bigger location.
10123 * in memory prepared device and new disk area are prepared
10124 * for usage in process update
10126 struct imsm_update_reshape_migration
*u
= (void *)update
->buf
;
10127 struct intel_dev
*id
;
10128 void **space_tail
= (void **)&update
->space_list
;
10131 int current_level
= -1;
10133 if (update
->len
< (int)sizeof(*u
))
10136 dprintf("for update_reshape\n");
10138 /* add space for bigger array in update
10140 for (id
= super
->devlist
; id
; id
= id
->next
) {
10141 if (id
->index
== (unsigned)u
->subdev
) {
10142 size
= sizeof_imsm_dev(id
->dev
, 1);
10143 if (u
->new_raid_disks
> u
->old_raid_disks
)
10144 size
+= sizeof(__u32
)*2*
10145 (u
->new_raid_disks
- u
->old_raid_disks
);
10149 *space_tail
= NULL
;
10153 if (update
->space_list
== NULL
)
10156 /* add space for disk in update
10158 size
= sizeof(struct dl
);
10162 *space_tail
= NULL
;
10164 /* add spare device to update
10166 for (id
= super
->devlist
; id
; id
= id
->next
)
10167 if (id
->index
== (unsigned)u
->subdev
) {
10168 struct imsm_dev
*dev
;
10169 struct imsm_map
*map
;
10171 dev
= get_imsm_dev(super
, u
->subdev
);
10172 map
= get_imsm_map(dev
, MAP_0
);
10173 current_level
= map
->raid_level
;
10176 if (u
->new_level
== 5 && u
->new_level
!= current_level
) {
10177 struct mdinfo
*spares
;
10179 spares
= get_spares_for_grow(st
);
10182 struct mdinfo
*dev
;
10184 dev
= spares
->devs
;
10187 makedev(dev
->disk
.major
,
10189 dl
= get_disk_super(super
,
10192 dl
->index
= u
->old_raid_disks
;
10195 sysfs_free(spares
);
10198 len
= disks_to_mpb_size(u
->new_raid_disks
);
10199 dprintf("New anchor length is %llu\n", (unsigned long long)len
);
10202 case update_size_change
: {
10203 if (update
->len
< (int)sizeof(struct imsm_update_size_change
))
10207 case update_activate_spare
: {
10208 if (update
->len
< (int)sizeof(struct imsm_update_activate_spare
))
10212 case update_create_array
: {
10213 struct imsm_update_create_array
*u
= (void *) update
->buf
;
10214 struct intel_dev
*dv
;
10215 struct imsm_dev
*dev
= &u
->dev
;
10216 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
10218 struct disk_info
*inf
;
10222 if (update
->len
< (int)sizeof(*u
))
10225 inf
= get_disk_info(u
);
10226 len
= sizeof_imsm_dev(dev
, 1);
10227 /* allocate a new super->devlist entry */
10228 dv
= xmalloc(sizeof(*dv
));
10229 dv
->dev
= xmalloc(len
);
10230 update
->space
= dv
;
10232 /* count how many spares will be converted to members */
10233 for (i
= 0; i
< map
->num_members
; i
++) {
10234 dl
= serial_to_dl(inf
[i
].serial
, super
);
10236 /* hmm maybe it failed?, nothing we can do about
10241 if (count_memberships(dl
, super
) == 0)
10244 len
+= activate
* sizeof(struct imsm_disk
);
10247 case update_kill_array
: {
10248 if (update
->len
< (int)sizeof(struct imsm_update_kill_array
))
10252 case update_rename_array
: {
10253 if (update
->len
< (int)sizeof(struct imsm_update_rename_array
))
10257 case update_add_remove_disk
:
10258 /* no update->len needed */
10260 case update_prealloc_badblocks_mem
:
10261 super
->extra_space
+= sizeof(struct bbm_log
) -
10262 get_imsm_bbm_log_size(super
->bbm_log
);
10264 case update_rwh_policy
: {
10265 if (update
->len
< (int)sizeof(struct imsm_update_rwh_policy
))
10273 /* check if we need a larger metadata buffer */
10274 if (super
->next_buf
)
10275 buf_len
= super
->next_len
;
10277 buf_len
= super
->len
;
10279 if (__le32_to_cpu(mpb
->mpb_size
) + super
->extra_space
+ len
> buf_len
) {
10280 /* ok we need a larger buf than what is currently allocated
10281 * if this allocation fails process_update will notice that
10282 * ->next_len is set and ->next_buf is NULL
10284 buf_len
= ROUND_UP(__le32_to_cpu(mpb
->mpb_size
) +
10285 super
->extra_space
+ len
, sector_size
);
10286 if (super
->next_buf
)
10287 free(super
->next_buf
);
10289 super
->next_len
= buf_len
;
10290 if (posix_memalign(&super
->next_buf
, sector_size
, buf_len
) == 0)
10291 memset(super
->next_buf
, 0, buf_len
);
10293 super
->next_buf
= NULL
;
10298 /* must be called while manager is quiesced */
10299 static void imsm_delete(struct intel_super
*super
, struct dl
**dlp
, unsigned index
)
10301 struct imsm_super
*mpb
= super
->anchor
;
10303 struct imsm_dev
*dev
;
10304 struct imsm_map
*map
;
10305 unsigned int i
, j
, num_members
;
10306 __u32 ord
, ord_map0
;
10307 struct bbm_log
*log
= super
->bbm_log
;
10309 dprintf("deleting device[%d] from imsm_super\n", index
);
10311 /* shift all indexes down one */
10312 for (iter
= super
->disks
; iter
; iter
= iter
->next
)
10313 if (iter
->index
> (int)index
)
10315 for (iter
= super
->missing
; iter
; iter
= iter
->next
)
10316 if (iter
->index
> (int)index
)
10319 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
10320 dev
= get_imsm_dev(super
, i
);
10321 map
= get_imsm_map(dev
, MAP_0
);
10322 num_members
= map
->num_members
;
10323 for (j
= 0; j
< num_members
; j
++) {
10324 /* update ord entries being careful not to propagate
10325 * ord-flags to the first map
10327 ord
= get_imsm_ord_tbl_ent(dev
, j
, MAP_X
);
10328 ord_map0
= get_imsm_ord_tbl_ent(dev
, j
, MAP_0
);
10330 if (ord_to_idx(ord
) <= index
)
10333 map
= get_imsm_map(dev
, MAP_0
);
10334 set_imsm_ord_tbl_ent(map
, j
, ord_map0
- 1);
10335 map
= get_imsm_map(dev
, MAP_1
);
10337 set_imsm_ord_tbl_ent(map
, j
, ord
- 1);
10341 for (i
= 0; i
< log
->entry_count
; i
++) {
10342 struct bbm_log_entry
*entry
= &log
->marked_block_entries
[i
];
10344 if (entry
->disk_ordinal
<= index
)
10346 entry
->disk_ordinal
--;
10350 super
->updates_pending
++;
10352 struct dl
*dl
= *dlp
;
10354 *dlp
= (*dlp
)->next
;
10355 __free_imsm_disk(dl
);
10359 static void close_targets(int *targets
, int new_disks
)
10366 for (i
= 0; i
< new_disks
; i
++) {
10367 if (targets
[i
] >= 0) {
10374 static int imsm_get_allowed_degradation(int level
, int raid_disks
,
10375 struct intel_super
*super
,
10376 struct imsm_dev
*dev
)
10382 struct imsm_map
*map
;
10385 ret_val
= raid_disks
/2;
10386 /* check map if all disks pairs not failed
10389 map
= get_imsm_map(dev
, MAP_0
);
10390 for (i
= 0; i
< ret_val
; i
++) {
10391 int degradation
= 0;
10392 if (get_imsm_disk(super
, i
) == NULL
)
10394 if (get_imsm_disk(super
, i
+ 1) == NULL
)
10396 if (degradation
== 2)
10399 map
= get_imsm_map(dev
, MAP_1
);
10400 /* if there is no second map
10401 * result can be returned
10405 /* check degradation in second map
10407 for (i
= 0; i
< ret_val
; i
++) {
10408 int degradation
= 0;
10409 if (get_imsm_disk(super
, i
) == NULL
)
10411 if (get_imsm_disk(super
, i
+ 1) == NULL
)
10413 if (degradation
== 2)
10427 /*******************************************************************************
10428 * Function: open_backup_targets
10429 * Description: Function opens file descriptors for all devices given in
10432 * info : general array info
10433 * raid_disks : number of disks
10434 * raid_fds : table of device's file descriptors
10435 * super : intel super for raid10 degradation check
10436 * dev : intel device for raid10 degradation check
10440 ******************************************************************************/
10441 int open_backup_targets(struct mdinfo
*info
, int raid_disks
, int *raid_fds
,
10442 struct intel_super
*super
, struct imsm_dev
*dev
)
10448 for (i
= 0; i
< raid_disks
; i
++)
10451 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
10454 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
)) {
10455 dprintf("disk is faulty!!\n");
10459 if (sd
->disk
.raid_disk
>= raid_disks
|| sd
->disk
.raid_disk
< 0)
10462 dn
= map_dev(sd
->disk
.major
,
10463 sd
->disk
.minor
, 1);
10464 raid_fds
[sd
->disk
.raid_disk
] = dev_open(dn
, O_RDWR
);
10465 if (raid_fds
[sd
->disk
.raid_disk
] < 0) {
10466 pr_err("cannot open component\n");
10471 /* check if maximum array degradation level is not exceeded
10473 if ((raid_disks
- opened
) >
10474 imsm_get_allowed_degradation(info
->new_level
, raid_disks
,
10476 pr_err("Not enough disks can be opened.\n");
10477 close_targets(raid_fds
, raid_disks
);
10483 /*******************************************************************************
10484 * Function: validate_container_imsm
10485 * Description: This routine validates container after assemble,
10486 * eg. if devices in container are under the same controller.
10489 * info : linked list with info about devices used in array
10493 ******************************************************************************/
10494 int validate_container_imsm(struct mdinfo
*info
)
10496 if (check_env("IMSM_NO_PLATFORM"))
10499 struct sys_dev
*idev
;
10500 struct sys_dev
*hba
= NULL
;
10501 struct sys_dev
*intel_devices
= find_intel_devices();
10502 char *dev_path
= devt_to_devpath(makedev(info
->disk
.major
,
10503 info
->disk
.minor
));
10505 for (idev
= intel_devices
; idev
; idev
= idev
->next
) {
10506 if (dev_path
&& strstr(dev_path
, idev
->path
)) {
10515 pr_err("WARNING - Cannot detect HBA for device %s!\n",
10516 devid2kname(makedev(info
->disk
.major
, info
->disk
.minor
)));
10520 const struct imsm_orom
*orom
= get_orom_by_device_id(hba
->dev_id
);
10521 struct mdinfo
*dev
;
10523 for (dev
= info
->next
; dev
; dev
= dev
->next
) {
10524 dev_path
= devt_to_devpath(makedev(dev
->disk
.major
, dev
->disk
.minor
));
10526 struct sys_dev
*hba2
= NULL
;
10527 for (idev
= intel_devices
; idev
; idev
= idev
->next
) {
10528 if (dev_path
&& strstr(dev_path
, idev
->path
)) {
10536 const struct imsm_orom
*orom2
= hba2
== NULL
? NULL
:
10537 get_orom_by_device_id(hba2
->dev_id
);
10539 if (hba2
&& hba
->type
!= hba2
->type
) {
10540 pr_err("WARNING - HBAs of devices do not match %s != %s\n",
10541 get_sys_dev_type(hba
->type
), get_sys_dev_type(hba2
->type
));
10545 if (orom
!= orom2
) {
10546 pr_err("WARNING - IMSM container assembled with disks under different HBAs!\n"
10547 " This operation is not supported and can lead to data loss.\n");
10552 pr_err("WARNING - IMSM container assembled with disks under HBAs without IMSM platform support!\n"
10553 " This operation is not supported and can lead to data loss.\n");
10561 /*******************************************************************************
10562 * Function: imsm_record_badblock
10563 * Description: This routine stores new bad block record in BBM log
10566 * a : array containing a bad block
10567 * slot : disk number containing a bad block
10568 * sector : bad block sector
10569 * length : bad block sectors range
10573 ******************************************************************************/
10574 static int imsm_record_badblock(struct active_array
*a
, int slot
,
10575 unsigned long long sector
, int length
)
10577 struct intel_super
*super
= a
->container
->sb
;
10581 ord
= imsm_disk_slot_to_ord(a
, slot
);
10585 ret
= record_new_badblock(super
->bbm_log
, ord_to_idx(ord
), sector
,
10588 super
->updates_pending
++;
10592 /*******************************************************************************
10593 * Function: imsm_clear_badblock
10594 * Description: This routine clears bad block record from BBM log
10597 * a : array containing a bad block
10598 * slot : disk number containing a bad block
10599 * sector : bad block sector
10600 * length : bad block sectors range
10604 ******************************************************************************/
10605 static int imsm_clear_badblock(struct active_array
*a
, int slot
,
10606 unsigned long long sector
, int length
)
10608 struct intel_super
*super
= a
->container
->sb
;
10612 ord
= imsm_disk_slot_to_ord(a
, slot
);
10616 ret
= clear_badblock(super
->bbm_log
, ord_to_idx(ord
), sector
, length
);
10618 super
->updates_pending
++;
10622 /*******************************************************************************
10623 * Function: imsm_get_badblocks
10624 * Description: This routine get list of bad blocks for an array
10628 * slot : disk number
10630 * bb : structure containing bad blocks
10632 ******************************************************************************/
10633 static struct md_bb
*imsm_get_badblocks(struct active_array
*a
, int slot
)
10635 int inst
= a
->info
.container_member
;
10636 struct intel_super
*super
= a
->container
->sb
;
10637 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
10638 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
10641 ord
= imsm_disk_slot_to_ord(a
, slot
);
10645 get_volume_badblocks(super
->bbm_log
, ord_to_idx(ord
), pba_of_lba0(map
),
10646 per_dev_array_size(map
), &super
->bb
);
10650 /*******************************************************************************
10651 * Function: examine_badblocks_imsm
10652 * Description: Prints list of bad blocks on a disk to the standard output
10655 * st : metadata handler
10656 * fd : open file descriptor for device
10657 * devname : device name
10661 ******************************************************************************/
10662 static int examine_badblocks_imsm(struct supertype
*st
, int fd
, char *devname
)
10664 struct intel_super
*super
= st
->sb
;
10665 struct bbm_log
*log
= super
->bbm_log
;
10666 struct dl
*d
= NULL
;
10669 for (d
= super
->disks
; d
; d
= d
->next
) {
10670 if (strcmp(d
->devname
, devname
) == 0)
10674 if ((d
== NULL
) || (d
->index
< 0)) { /* serial mismatch probably */
10675 pr_err("%s doesn't appear to be part of a raid array\n",
10682 struct bbm_log_entry
*entry
= &log
->marked_block_entries
[0];
10684 for (i
= 0; i
< log
->entry_count
; i
++) {
10685 if (entry
[i
].disk_ordinal
== d
->index
) {
10686 unsigned long long sector
= __le48_to_cpu(
10687 &entry
[i
].defective_block_start
);
10688 int cnt
= entry
[i
].marked_count
+ 1;
10691 printf("Bad-blocks on %s:\n", devname
);
10695 printf("%20llu for %d sectors\n", sector
, cnt
);
10701 printf("No bad-blocks list configured on %s\n", devname
);
10705 /*******************************************************************************
10706 * Function: init_migr_record_imsm
10707 * Description: Function inits imsm migration record
10709 * super : imsm internal array info
10710 * dev : device under migration
10711 * info : general array info to find the smallest device
10714 ******************************************************************************/
10715 void init_migr_record_imsm(struct supertype
*st
, struct imsm_dev
*dev
,
10716 struct mdinfo
*info
)
10718 struct intel_super
*super
= st
->sb
;
10719 struct migr_record
*migr_rec
= super
->migr_rec
;
10720 int new_data_disks
;
10721 unsigned long long dsize
, dev_sectors
;
10722 long long unsigned min_dev_sectors
= -1LLU;
10726 struct imsm_map
*map_dest
= get_imsm_map(dev
, MAP_0
);
10727 struct imsm_map
*map_src
= get_imsm_map(dev
, MAP_1
);
10728 unsigned long long num_migr_units
;
10729 unsigned long long array_blocks
;
10731 memset(migr_rec
, 0, sizeof(struct migr_record
));
10732 migr_rec
->family_num
= __cpu_to_le32(super
->anchor
->family_num
);
10734 /* only ascending reshape supported now */
10735 migr_rec
->ascending_migr
= __cpu_to_le32(1);
10737 migr_rec
->dest_depth_per_unit
= GEN_MIGR_AREA_SIZE
/
10738 max(map_dest
->blocks_per_strip
, map_src
->blocks_per_strip
);
10739 migr_rec
->dest_depth_per_unit
*=
10740 max(map_dest
->blocks_per_strip
, map_src
->blocks_per_strip
);
10741 new_data_disks
= imsm_num_data_members(map_dest
);
10742 migr_rec
->blocks_per_unit
=
10743 __cpu_to_le32(migr_rec
->dest_depth_per_unit
* new_data_disks
);
10744 migr_rec
->dest_depth_per_unit
=
10745 __cpu_to_le32(migr_rec
->dest_depth_per_unit
);
10746 array_blocks
= info
->component_size
* new_data_disks
;
10748 array_blocks
/ __le32_to_cpu(migr_rec
->blocks_per_unit
);
10750 if (array_blocks
% __le32_to_cpu(migr_rec
->blocks_per_unit
))
10752 set_num_migr_units(migr_rec
, num_migr_units
);
10754 migr_rec
->post_migr_vol_cap
= dev
->size_low
;
10755 migr_rec
->post_migr_vol_cap_hi
= dev
->size_high
;
10757 /* Find the smallest dev */
10758 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
10759 sprintf(nm
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
10760 fd
= dev_open(nm
, O_RDONLY
);
10763 get_dev_size(fd
, NULL
, &dsize
);
10764 dev_sectors
= dsize
/ 512;
10765 if (dev_sectors
< min_dev_sectors
)
10766 min_dev_sectors
= dev_sectors
;
10769 set_migr_chkp_area_pba(migr_rec
, min_dev_sectors
-
10770 RAID_DISK_RESERVED_BLOCKS_IMSM_HI
);
10772 write_imsm_migr_rec(st
);
10777 /*******************************************************************************
10778 * Function: save_backup_imsm
10779 * Description: Function saves critical data stripes to Migration Copy Area
10780 * and updates the current migration unit status.
10781 * Use restore_stripes() to form a destination stripe,
10782 * and to write it to the Copy Area.
10784 * st : supertype information
10785 * dev : imsm device that backup is saved for
10786 * info : general array info
10787 * buf : input buffer
10788 * length : length of data to backup (blocks_per_unit)
10792 ******************************************************************************/
10793 int save_backup_imsm(struct supertype
*st
,
10794 struct imsm_dev
*dev
,
10795 struct mdinfo
*info
,
10800 struct intel_super
*super
= st
->sb
;
10801 unsigned long long *target_offsets
;
10804 struct imsm_map
*map_dest
= get_imsm_map(dev
, MAP_0
);
10805 int new_disks
= map_dest
->num_members
;
10806 int dest_layout
= 0;
10808 unsigned long long start
;
10809 int data_disks
= imsm_num_data_members(map_dest
);
10811 targets
= xmalloc(new_disks
* sizeof(int));
10813 for (i
= 0; i
< new_disks
; i
++)
10816 target_offsets
= xcalloc(new_disks
, sizeof(unsigned long long));
10818 start
= info
->reshape_progress
* 512;
10819 for (i
= 0; i
< new_disks
; i
++) {
10820 target_offsets
[i
] = migr_chkp_area_pba(super
->migr_rec
) * 512;
10821 /* move back copy area adderss, it will be moved forward
10822 * in restore_stripes() using start input variable
10824 target_offsets
[i
] -= start
/data_disks
;
10827 if (open_backup_targets(info
, new_disks
, targets
,
10831 dest_layout
= imsm_level_to_layout(map_dest
->raid_level
);
10832 dest_chunk
= __le16_to_cpu(map_dest
->blocks_per_strip
) * 512;
10834 if (restore_stripes(targets
, /* list of dest devices */
10835 target_offsets
, /* migration record offsets */
10838 map_dest
->raid_level
,
10840 -1, /* source backup file descriptor */
10841 0, /* input buf offset
10842 * always 0 buf is already offseted */
10846 pr_err("Error restoring stripes\n");
10854 close_targets(targets
, new_disks
);
10857 free(target_offsets
);
10862 /*******************************************************************************
10863 * Function: save_checkpoint_imsm
10864 * Description: Function called for current unit status update
10865 * in the migration record. It writes it to disk.
10867 * super : imsm internal array info
10868 * info : general array info
10872 * 2: failure, means no valid migration record
10873 * / no general migration in progress /
10874 ******************************************************************************/
10875 int save_checkpoint_imsm(struct supertype
*st
, struct mdinfo
*info
, int state
)
10877 struct intel_super
*super
= st
->sb
;
10878 unsigned long long blocks_per_unit
;
10879 unsigned long long curr_migr_unit
;
10881 if (load_imsm_migr_rec(super
, info
) != 0) {
10882 dprintf("imsm: ERROR: Cannot read migration record for checkpoint save.\n");
10886 blocks_per_unit
= __le32_to_cpu(super
->migr_rec
->blocks_per_unit
);
10887 if (blocks_per_unit
== 0) {
10888 dprintf("imsm: no migration in progress.\n");
10891 curr_migr_unit
= info
->reshape_progress
/ blocks_per_unit
;
10892 /* check if array is alligned to copy area
10893 * if it is not alligned, add one to current migration unit value
10894 * this can happend on array reshape finish only
10896 if (info
->reshape_progress
% blocks_per_unit
)
10899 set_current_migr_unit(super
->migr_rec
, curr_migr_unit
);
10900 super
->migr_rec
->rec_status
= __cpu_to_le32(state
);
10901 set_migr_dest_1st_member_lba(super
->migr_rec
,
10902 super
->migr_rec
->dest_depth_per_unit
* curr_migr_unit
);
10904 if (write_imsm_migr_rec(st
) < 0) {
10905 dprintf("imsm: Cannot write migration record outside backup area\n");
10912 /*******************************************************************************
10913 * Function: recover_backup_imsm
10914 * Description: Function recovers critical data from the Migration Copy Area
10915 * while assembling an array.
10917 * super : imsm internal array info
10918 * info : general array info
10920 * 0 : success (or there is no data to recover)
10922 ******************************************************************************/
10923 int recover_backup_imsm(struct supertype
*st
, struct mdinfo
*info
)
10925 struct intel_super
*super
= st
->sb
;
10926 struct migr_record
*migr_rec
= super
->migr_rec
;
10927 struct imsm_map
*map_dest
;
10928 struct intel_dev
*id
= NULL
;
10929 unsigned long long read_offset
;
10930 unsigned long long write_offset
;
10932 int *targets
= NULL
;
10933 int new_disks
, i
, err
;
10936 unsigned int sector_size
= super
->sector_size
;
10937 unsigned long curr_migr_unit
= current_migr_unit(migr_rec
);
10938 unsigned long num_migr_units
= get_num_migr_units(migr_rec
);
10940 int skipped_disks
= 0;
10942 err
= sysfs_get_str(info
, NULL
, "array_state", (char *)buffer
, 20);
10946 /* recover data only during assemblation */
10947 if (strncmp(buffer
, "inactive", 8) != 0)
10949 /* no data to recover */
10950 if (__le32_to_cpu(migr_rec
->rec_status
) == UNIT_SRC_NORMAL
)
10952 if (curr_migr_unit
>= num_migr_units
)
10955 /* find device during reshape */
10956 for (id
= super
->devlist
; id
; id
= id
->next
)
10957 if (is_gen_migration(id
->dev
))
10962 map_dest
= get_imsm_map(id
->dev
, MAP_0
);
10963 new_disks
= map_dest
->num_members
;
10965 read_offset
= migr_chkp_area_pba(migr_rec
) * 512;
10967 write_offset
= (migr_dest_1st_member_lba(migr_rec
) +
10968 pba_of_lba0(map_dest
)) * 512;
10970 unit_len
= __le32_to_cpu(migr_rec
->dest_depth_per_unit
) * 512;
10971 if (posix_memalign((void **)&buf
, sector_size
, unit_len
) != 0)
10973 targets
= xcalloc(new_disks
, sizeof(int));
10975 if (open_backup_targets(info
, new_disks
, targets
, super
, id
->dev
)) {
10976 pr_err("Cannot open some devices belonging to array.\n");
10980 for (i
= 0; i
< new_disks
; i
++) {
10981 if (targets
[i
] < 0) {
10985 if (lseek64(targets
[i
], read_offset
, SEEK_SET
) < 0) {
10986 pr_err("Cannot seek to block: %s\n",
10991 if ((unsigned)read(targets
[i
], buf
, unit_len
) != unit_len
) {
10992 pr_err("Cannot read copy area block: %s\n",
10997 if (lseek64(targets
[i
], write_offset
, SEEK_SET
) < 0) {
10998 pr_err("Cannot seek to block: %s\n",
11003 if ((unsigned)write(targets
[i
], buf
, unit_len
) != unit_len
) {
11004 pr_err("Cannot restore block: %s\n",
11011 if (skipped_disks
> imsm_get_allowed_degradation(info
->new_level
,
11015 pr_err("Cannot restore data from backup. Too many failed disks\n");
11019 if (save_checkpoint_imsm(st
, info
, UNIT_SRC_NORMAL
)) {
11020 /* ignore error == 2, this can mean end of reshape here
11022 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL) during restart\n");
11028 for (i
= 0; i
< new_disks
; i
++)
11037 static char disk_by_path
[] = "/dev/disk/by-path/";
11039 static const char *imsm_get_disk_controller_domain(const char *path
)
11041 char disk_path
[PATH_MAX
];
11045 strcpy(disk_path
, disk_by_path
);
11046 strncat(disk_path
, path
, PATH_MAX
- strlen(disk_path
) - 1);
11047 if (stat(disk_path
, &st
) == 0) {
11048 struct sys_dev
* hba
;
11051 path
= devt_to_devpath(st
.st_rdev
);
11054 hba
= find_disk_attached_hba(-1, path
);
11055 if (hba
&& hba
->type
== SYS_DEV_SAS
)
11057 else if (hba
&& hba
->type
== SYS_DEV_SATA
)
11059 else if (hba
&& hba
->type
== SYS_DEV_VMD
)
11061 else if (hba
&& hba
->type
== SYS_DEV_NVME
)
11065 dprintf("path: %s hba: %s attached: %s\n",
11066 path
, (hba
) ? hba
->path
: "NULL", drv
);
11072 static char *imsm_find_array_devnm_by_subdev(int subdev
, char *container
)
11074 static char devnm
[32];
11075 char subdev_name
[20];
11076 struct mdstat_ent
*mdstat
;
11078 sprintf(subdev_name
, "%d", subdev
);
11079 mdstat
= mdstat_by_subdev(subdev_name
, container
);
11083 strcpy(devnm
, mdstat
->devnm
);
11084 free_mdstat(mdstat
);
11088 static int imsm_reshape_is_allowed_on_container(struct supertype
*st
,
11089 struct geo_params
*geo
,
11090 int *old_raid_disks
,
11093 /* currently we only support increasing the number of devices
11094 * for a container. This increases the number of device for each
11095 * member array. They must all be RAID0 or RAID5.
11098 struct mdinfo
*info
, *member
;
11099 int devices_that_can_grow
= 0;
11101 dprintf("imsm: imsm_reshape_is_allowed_on_container(ENTER): st->devnm = (%s)\n", st
->devnm
);
11103 if (geo
->size
> 0 ||
11104 geo
->level
!= UnSet
||
11105 geo
->layout
!= UnSet
||
11106 geo
->chunksize
!= 0 ||
11107 geo
->raid_disks
== UnSet
) {
11108 dprintf("imsm: Container operation is allowed for raid disks number change only.\n");
11112 if (direction
== ROLLBACK_METADATA_CHANGES
) {
11113 dprintf("imsm: Metadata changes rollback is not supported for container operation.\n");
11117 info
= container_content_imsm(st
, NULL
);
11118 for (member
= info
; member
; member
= member
->next
) {
11121 dprintf("imsm: checking device_num: %i\n",
11122 member
->container_member
);
11124 if (geo
->raid_disks
<= member
->array
.raid_disks
) {
11125 /* we work on container for Online Capacity Expansion
11126 * only so raid_disks has to grow
11128 dprintf("imsm: for container operation raid disks increase is required\n");
11132 if (info
->array
.level
!= 0 && info
->array
.level
!= 5) {
11133 /* we cannot use this container with other raid level
11135 dprintf("imsm: for container operation wrong raid level (%i) detected\n",
11136 info
->array
.level
);
11139 /* check for platform support
11140 * for this raid level configuration
11142 struct intel_super
*super
= st
->sb
;
11143 if (!is_raid_level_supported(super
->orom
,
11144 member
->array
.level
,
11145 geo
->raid_disks
)) {
11146 dprintf("platform does not support raid%d with %d disk%s\n",
11149 geo
->raid_disks
> 1 ? "s" : "");
11152 /* check if component size is aligned to chunk size
11154 if (info
->component_size
%
11155 (info
->array
.chunk_size
/512)) {
11156 dprintf("Component size is not aligned to chunk size\n");
11161 if (*old_raid_disks
&&
11162 info
->array
.raid_disks
!= *old_raid_disks
)
11164 *old_raid_disks
= info
->array
.raid_disks
;
11166 /* All raid5 and raid0 volumes in container
11167 * have to be ready for Online Capacity Expansion
11168 * so they need to be assembled. We have already
11169 * checked that no recovery etc is happening.
11171 result
= imsm_find_array_devnm_by_subdev(member
->container_member
,
11172 st
->container_devnm
);
11173 if (result
== NULL
) {
11174 dprintf("imsm: cannot find array\n");
11177 devices_that_can_grow
++;
11180 if (!member
&& devices_that_can_grow
)
11184 dprintf("Container operation allowed\n");
11186 dprintf("Error: %i\n", ret_val
);
11191 /* Function: get_spares_for_grow
11192 * Description: Allocates memory and creates list of spare devices
11193 * avaliable in container. Checks if spare drive size is acceptable.
11194 * Parameters: Pointer to the supertype structure
11195 * Returns: Pointer to the list of spare devices (mdinfo structure) on success,
11198 static struct mdinfo
*get_spares_for_grow(struct supertype
*st
)
11200 struct spare_criteria sc
;
11202 get_spare_criteria_imsm(st
, &sc
);
11203 return container_choose_spares(st
, &sc
, NULL
, NULL
, NULL
, 0);
11206 /******************************************************************************
11207 * function: imsm_create_metadata_update_for_reshape
11208 * Function creates update for whole IMSM container.
11210 ******************************************************************************/
11211 static int imsm_create_metadata_update_for_reshape(
11212 struct supertype
*st
,
11213 struct geo_params
*geo
,
11214 int old_raid_disks
,
11215 struct imsm_update_reshape
**updatep
)
11217 struct intel_super
*super
= st
->sb
;
11218 struct imsm_super
*mpb
= super
->anchor
;
11219 int update_memory_size
;
11220 struct imsm_update_reshape
*u
;
11221 struct mdinfo
*spares
;
11224 struct mdinfo
*dev
;
11226 dprintf("(enter) raid_disks = %i\n", geo
->raid_disks
);
11228 delta_disks
= geo
->raid_disks
- old_raid_disks
;
11230 /* size of all update data without anchor */
11231 update_memory_size
= sizeof(struct imsm_update_reshape
);
11233 /* now add space for spare disks that we need to add. */
11234 update_memory_size
+= sizeof(u
->new_disks
[0]) * (delta_disks
- 1);
11236 u
= xcalloc(1, update_memory_size
);
11237 u
->type
= update_reshape_container_disks
;
11238 u
->old_raid_disks
= old_raid_disks
;
11239 u
->new_raid_disks
= geo
->raid_disks
;
11241 /* now get spare disks list
11243 spares
= get_spares_for_grow(st
);
11245 if (spares
== NULL
|| delta_disks
> spares
->array
.spare_disks
) {
11246 pr_err("imsm: ERROR: Cannot get spare devices for %s.\n", geo
->dev_name
);
11251 /* we have got spares
11252 * update disk list in imsm_disk list table in anchor
11254 dprintf("imsm: %i spares are available.\n\n",
11255 spares
->array
.spare_disks
);
11257 dev
= spares
->devs
;
11258 for (i
= 0; i
< delta_disks
; i
++) {
11263 u
->new_disks
[i
] = makedev(dev
->disk
.major
,
11265 dl
= get_disk_super(super
, dev
->disk
.major
, dev
->disk
.minor
);
11266 dl
->index
= mpb
->num_disks
;
11274 sysfs_free(spares
);
11276 dprintf("imsm: reshape update preparation :");
11277 if (i
== delta_disks
) {
11278 dprintf_cont(" OK\n");
11280 return update_memory_size
;
11283 dprintf_cont(" Error\n");
11288 /******************************************************************************
11289 * function: imsm_create_metadata_update_for_size_change()
11290 * Creates update for IMSM array for array size change.
11292 ******************************************************************************/
11293 static int imsm_create_metadata_update_for_size_change(
11294 struct supertype
*st
,
11295 struct geo_params
*geo
,
11296 struct imsm_update_size_change
**updatep
)
11298 struct intel_super
*super
= st
->sb
;
11299 int update_memory_size
;
11300 struct imsm_update_size_change
*u
;
11302 dprintf("(enter) New size = %llu\n", geo
->size
);
11304 /* size of all update data without anchor */
11305 update_memory_size
= sizeof(struct imsm_update_size_change
);
11307 u
= xcalloc(1, update_memory_size
);
11308 u
->type
= update_size_change
;
11309 u
->subdev
= super
->current_vol
;
11310 u
->new_size
= geo
->size
;
11312 dprintf("imsm: reshape update preparation : OK\n");
11315 return update_memory_size
;
11318 /******************************************************************************
11319 * function: imsm_create_metadata_update_for_migration()
11320 * Creates update for IMSM array.
11322 ******************************************************************************/
11323 static int imsm_create_metadata_update_for_migration(
11324 struct supertype
*st
,
11325 struct geo_params
*geo
,
11326 struct imsm_update_reshape_migration
**updatep
)
11328 struct intel_super
*super
= st
->sb
;
11329 int update_memory_size
;
11330 struct imsm_update_reshape_migration
*u
;
11331 struct imsm_dev
*dev
;
11332 int previous_level
= -1;
11334 dprintf("(enter) New Level = %i\n", geo
->level
);
11336 /* size of all update data without anchor */
11337 update_memory_size
= sizeof(struct imsm_update_reshape_migration
);
11339 u
= xcalloc(1, update_memory_size
);
11340 u
->type
= update_reshape_migration
;
11341 u
->subdev
= super
->current_vol
;
11342 u
->new_level
= geo
->level
;
11343 u
->new_layout
= geo
->layout
;
11344 u
->new_raid_disks
= u
->old_raid_disks
= geo
->raid_disks
;
11345 u
->new_disks
[0] = -1;
11346 u
->new_chunksize
= -1;
11348 dev
= get_imsm_dev(super
, u
->subdev
);
11350 struct imsm_map
*map
;
11352 map
= get_imsm_map(dev
, MAP_0
);
11354 int current_chunk_size
=
11355 __le16_to_cpu(map
->blocks_per_strip
) / 2;
11357 if (geo
->chunksize
!= current_chunk_size
) {
11358 u
->new_chunksize
= geo
->chunksize
/ 1024;
11359 dprintf("imsm: chunk size change from %i to %i\n",
11360 current_chunk_size
, u
->new_chunksize
);
11362 previous_level
= map
->raid_level
;
11365 if (geo
->level
== 5 && previous_level
== 0) {
11366 struct mdinfo
*spares
= NULL
;
11368 u
->new_raid_disks
++;
11369 spares
= get_spares_for_grow(st
);
11370 if (spares
== NULL
|| spares
->array
.spare_disks
< 1) {
11372 sysfs_free(spares
);
11373 update_memory_size
= 0;
11374 pr_err("cannot get spare device for requested migration\n");
11377 sysfs_free(spares
);
11379 dprintf("imsm: reshape update preparation : OK\n");
11382 return update_memory_size
;
11385 static void imsm_update_metadata_locally(struct supertype
*st
,
11386 void *buf
, int len
)
11388 struct metadata_update mu
;
11393 mu
.space_list
= NULL
;
11395 if (imsm_prepare_update(st
, &mu
))
11396 imsm_process_update(st
, &mu
);
11398 while (mu
.space_list
) {
11399 void **space
= mu
.space_list
;
11400 mu
.space_list
= *space
;
11405 /***************************************************************************
11406 * Function: imsm_analyze_change
11407 * Description: Function analyze change for single volume
11408 * and validate if transition is supported
11409 * Parameters: Geometry parameters, supertype structure,
11410 * metadata change direction (apply/rollback)
11411 * Returns: Operation type code on success, -1 if fail
11412 ****************************************************************************/
11413 enum imsm_reshape_type
imsm_analyze_change(struct supertype
*st
,
11414 struct geo_params
*geo
,
11417 struct mdinfo info
;
11419 int check_devs
= 0;
11421 /* number of added/removed disks in operation result */
11422 int devNumChange
= 0;
11423 /* imsm compatible layout value for array geometry verification */
11424 int imsm_layout
= -1;
11426 struct imsm_dev
*dev
;
11427 struct imsm_map
*map
;
11428 struct intel_super
*super
;
11429 unsigned long long current_size
;
11430 unsigned long long free_size
;
11431 unsigned long long max_size
;
11434 getinfo_super_imsm_volume(st
, &info
, NULL
);
11435 if (geo
->level
!= info
.array
.level
&& geo
->level
>= 0 &&
11436 geo
->level
!= UnSet
) {
11437 switch (info
.array
.level
) {
11439 if (geo
->level
== 5) {
11440 change
= CH_MIGRATION
;
11441 if (geo
->layout
!= ALGORITHM_LEFT_ASYMMETRIC
) {
11442 pr_err("Error. Requested Layout not supported (left-asymmetric layout is supported only)!\n");
11444 goto analyse_change_exit
;
11446 imsm_layout
= geo
->layout
;
11448 devNumChange
= 1; /* parity disk added */
11449 } else if (geo
->level
== 10) {
11450 change
= CH_TAKEOVER
;
11452 devNumChange
= 2; /* two mirrors added */
11453 imsm_layout
= 0x102; /* imsm supported layout */
11458 if (geo
->level
== 0) {
11459 change
= CH_TAKEOVER
;
11461 devNumChange
= -(geo
->raid_disks
/2);
11462 imsm_layout
= 0; /* imsm raid0 layout */
11466 if (change
== -1) {
11467 pr_err("Error. Level Migration from %d to %d not supported!\n",
11468 info
.array
.level
, geo
->level
);
11469 goto analyse_change_exit
;
11472 geo
->level
= info
.array
.level
;
11474 if (geo
->layout
!= info
.array
.layout
&&
11475 (geo
->layout
!= UnSet
&& geo
->layout
!= -1)) {
11476 change
= CH_MIGRATION
;
11477 if (info
.array
.layout
== 0 && info
.array
.level
== 5 &&
11478 geo
->layout
== 5) {
11479 /* reshape 5 -> 4 */
11480 } else if (info
.array
.layout
== 5 && info
.array
.level
== 5 &&
11481 geo
->layout
== 0) {
11482 /* reshape 4 -> 5 */
11486 pr_err("Error. Layout Migration from %d to %d not supported!\n",
11487 info
.array
.layout
, geo
->layout
);
11489 goto analyse_change_exit
;
11492 geo
->layout
= info
.array
.layout
;
11493 if (imsm_layout
== -1)
11494 imsm_layout
= info
.array
.layout
;
11497 if (geo
->chunksize
> 0 && geo
->chunksize
!= UnSet
&&
11498 geo
->chunksize
!= info
.array
.chunk_size
) {
11499 if (info
.array
.level
== 10) {
11500 pr_err("Error. Chunk size change for RAID 10 is not supported.\n");
11502 goto analyse_change_exit
;
11503 } else if (info
.component_size
% (geo
->chunksize
/512)) {
11504 pr_err("New chunk size (%dK) does not evenly divide device size (%lluk). Aborting...\n",
11505 geo
->chunksize
/1024, info
.component_size
/2);
11507 goto analyse_change_exit
;
11509 change
= CH_MIGRATION
;
11511 geo
->chunksize
= info
.array
.chunk_size
;
11514 chunk
= geo
->chunksize
/ 1024;
11517 dev
= get_imsm_dev(super
, super
->current_vol
);
11518 map
= get_imsm_map(dev
, MAP_0
);
11519 data_disks
= imsm_num_data_members(map
);
11520 /* compute current size per disk member
11522 current_size
= info
.custom_array_size
/ data_disks
;
11524 if (geo
->size
> 0 && geo
->size
!= MAX_SIZE
) {
11525 /* align component size
11527 geo
->size
= imsm_component_size_alignment_check(
11528 get_imsm_raid_level(dev
->vol
.map
),
11529 chunk
* 1024, super
->sector_size
,
11531 if (geo
->size
== 0) {
11532 pr_err("Error. Size expansion is supported only (current size is %llu, requested size /rounded/ is 0).\n",
11534 goto analyse_change_exit
;
11538 if (current_size
!= geo
->size
&& geo
->size
> 0) {
11539 if (change
!= -1) {
11540 pr_err("Error. Size change should be the only one at a time.\n");
11542 goto analyse_change_exit
;
11544 if ((super
->current_vol
+ 1) != super
->anchor
->num_raid_devs
) {
11545 pr_err("Error. The last volume in container can be expanded only (%i/%s).\n",
11546 super
->current_vol
, st
->devnm
);
11547 goto analyse_change_exit
;
11549 /* check the maximum available size
11551 rv
= imsm_get_free_size(st
, dev
->vol
.map
->num_members
,
11552 0, chunk
, &free_size
);
11554 /* Cannot find maximum available space
11558 max_size
= free_size
+ current_size
;
11559 /* align component size
11561 max_size
= imsm_component_size_alignment_check(
11562 get_imsm_raid_level(dev
->vol
.map
),
11563 chunk
* 1024, super
->sector_size
,
11566 if (geo
->size
== MAX_SIZE
) {
11567 /* requested size change to the maximum available size
11569 if (max_size
== 0) {
11570 pr_err("Error. Cannot find maximum available space.\n");
11572 goto analyse_change_exit
;
11574 geo
->size
= max_size
;
11577 if (direction
== ROLLBACK_METADATA_CHANGES
) {
11578 /* accept size for rollback only
11581 /* round size due to metadata compatibility
11583 geo
->size
= (geo
->size
>> SECT_PER_MB_SHIFT
)
11584 << SECT_PER_MB_SHIFT
;
11585 dprintf("Prepare update for size change to %llu\n",
11587 if (current_size
>= geo
->size
) {
11588 pr_err("Error. Size expansion is supported only (current size is %llu, requested size /rounded/ is %llu).\n",
11589 current_size
, geo
->size
);
11590 goto analyse_change_exit
;
11592 if (max_size
&& geo
->size
> max_size
) {
11593 pr_err("Error. Requested size is larger than maximum available size (maximum available size is %llu, requested size /rounded/ is %llu).\n",
11594 max_size
, geo
->size
);
11595 goto analyse_change_exit
;
11598 geo
->size
*= data_disks
;
11599 geo
->raid_disks
= dev
->vol
.map
->num_members
;
11600 change
= CH_ARRAY_SIZE
;
11602 if (!validate_geometry_imsm(st
,
11605 geo
->raid_disks
+ devNumChange
,
11607 geo
->size
, INVALID_SECTORS
,
11608 0, 0, info
.consistency_policy
, 1))
11612 struct intel_super
*super
= st
->sb
;
11613 struct imsm_super
*mpb
= super
->anchor
;
11615 if (mpb
->num_raid_devs
> 1) {
11616 pr_err("Error. Cannot perform operation on %s- for this operation it MUST be single array in container\n",
11622 analyse_change_exit
:
11623 if (direction
== ROLLBACK_METADATA_CHANGES
&&
11624 (change
== CH_MIGRATION
|| change
== CH_TAKEOVER
)) {
11625 dprintf("imsm: Metadata changes rollback is not supported for migration and takeover operations.\n");
11631 int imsm_takeover(struct supertype
*st
, struct geo_params
*geo
)
11633 struct intel_super
*super
= st
->sb
;
11634 struct imsm_update_takeover
*u
;
11636 u
= xmalloc(sizeof(struct imsm_update_takeover
));
11638 u
->type
= update_takeover
;
11639 u
->subarray
= super
->current_vol
;
11641 /* 10->0 transition */
11642 if (geo
->level
== 0)
11643 u
->direction
= R10_TO_R0
;
11645 /* 0->10 transition */
11646 if (geo
->level
== 10)
11647 u
->direction
= R0_TO_R10
;
11649 /* update metadata locally */
11650 imsm_update_metadata_locally(st
, u
,
11651 sizeof(struct imsm_update_takeover
));
11652 /* and possibly remotely */
11653 if (st
->update_tail
)
11654 append_metadata_update(st
, u
,
11655 sizeof(struct imsm_update_takeover
));
11662 static int imsm_reshape_super(struct supertype
*st
, unsigned long long size
,
11664 int layout
, int chunksize
, int raid_disks
,
11665 int delta_disks
, char *backup
, char *dev
,
11666 int direction
, int verbose
)
11669 struct geo_params geo
;
11671 dprintf("(enter)\n");
11673 memset(&geo
, 0, sizeof(struct geo_params
));
11675 geo
.dev_name
= dev
;
11676 strcpy(geo
.devnm
, st
->devnm
);
11679 geo
.layout
= layout
;
11680 geo
.chunksize
= chunksize
;
11681 geo
.raid_disks
= raid_disks
;
11682 if (delta_disks
!= UnSet
)
11683 geo
.raid_disks
+= delta_disks
;
11685 dprintf("for level : %i\n", geo
.level
);
11686 dprintf("for raid_disks : %i\n", geo
.raid_disks
);
11688 if (strcmp(st
->container_devnm
, st
->devnm
) == 0) {
11689 /* On container level we can only increase number of devices. */
11690 dprintf("imsm: info: Container operation\n");
11691 int old_raid_disks
= 0;
11693 if (imsm_reshape_is_allowed_on_container(
11694 st
, &geo
, &old_raid_disks
, direction
)) {
11695 struct imsm_update_reshape
*u
= NULL
;
11698 len
= imsm_create_metadata_update_for_reshape(
11699 st
, &geo
, old_raid_disks
, &u
);
11702 dprintf("imsm: Cannot prepare update\n");
11703 goto exit_imsm_reshape_super
;
11707 /* update metadata locally */
11708 imsm_update_metadata_locally(st
, u
, len
);
11709 /* and possibly remotely */
11710 if (st
->update_tail
)
11711 append_metadata_update(st
, u
, len
);
11716 pr_err("(imsm) Operation is not allowed on this container\n");
11719 /* On volume level we support following operations
11720 * - takeover: raid10 -> raid0; raid0 -> raid10
11721 * - chunk size migration
11722 * - migration: raid5 -> raid0; raid0 -> raid5
11724 struct intel_super
*super
= st
->sb
;
11725 struct intel_dev
*dev
= super
->devlist
;
11727 dprintf("imsm: info: Volume operation\n");
11728 /* find requested device */
11731 imsm_find_array_devnm_by_subdev(
11732 dev
->index
, st
->container_devnm
);
11733 if (devnm
&& strcmp(devnm
, geo
.devnm
) == 0)
11738 pr_err("Cannot find %s (%s) subarray\n",
11739 geo
.dev_name
, geo
.devnm
);
11740 goto exit_imsm_reshape_super
;
11742 super
->current_vol
= dev
->index
;
11743 change
= imsm_analyze_change(st
, &geo
, direction
);
11746 ret_val
= imsm_takeover(st
, &geo
);
11748 case CH_MIGRATION
: {
11749 struct imsm_update_reshape_migration
*u
= NULL
;
11751 imsm_create_metadata_update_for_migration(
11754 dprintf("imsm: Cannot prepare update\n");
11758 /* update metadata locally */
11759 imsm_update_metadata_locally(st
, u
, len
);
11760 /* and possibly remotely */
11761 if (st
->update_tail
)
11762 append_metadata_update(st
, u
, len
);
11767 case CH_ARRAY_SIZE
: {
11768 struct imsm_update_size_change
*u
= NULL
;
11770 imsm_create_metadata_update_for_size_change(
11773 dprintf("imsm: Cannot prepare update\n");
11777 /* update metadata locally */
11778 imsm_update_metadata_locally(st
, u
, len
);
11779 /* and possibly remotely */
11780 if (st
->update_tail
)
11781 append_metadata_update(st
, u
, len
);
11791 exit_imsm_reshape_super
:
11792 dprintf("imsm: reshape_super Exit code = %i\n", ret_val
);
11796 #define COMPLETED_OK 0
11797 #define COMPLETED_NONE 1
11798 #define COMPLETED_DELAYED 2
11800 static int read_completed(int fd
, unsigned long long *val
)
11805 ret
= sysfs_fd_get_str(fd
, buf
, 50);
11809 ret
= COMPLETED_OK
;
11810 if (strncmp(buf
, "none", 4) == 0) {
11811 ret
= COMPLETED_NONE
;
11812 } else if (strncmp(buf
, "delayed", 7) == 0) {
11813 ret
= COMPLETED_DELAYED
;
11816 *val
= strtoull(buf
, &ep
, 0);
11817 if (ep
== buf
|| (*ep
!= 0 && *ep
!= '\n' && *ep
!= ' '))
11823 /*******************************************************************************
11824 * Function: wait_for_reshape_imsm
11825 * Description: Function writes new sync_max value and waits until
11826 * reshape process reach new position
11828 * sra : general array info
11829 * ndata : number of disks in new array's layout
11832 * 1 : there is no reshape in progress,
11834 ******************************************************************************/
11835 int wait_for_reshape_imsm(struct mdinfo
*sra
, int ndata
)
11837 int fd
= sysfs_get_fd(sra
, NULL
, "sync_completed");
11839 unsigned long long completed
;
11840 /* to_complete : new sync_max position */
11841 unsigned long long to_complete
= sra
->reshape_progress
;
11842 unsigned long long position_to_set
= to_complete
/ ndata
;
11845 dprintf("cannot open reshape_position\n");
11850 if (sysfs_fd_get_ll(fd
, &completed
) < 0) {
11852 dprintf("cannot read reshape_position (no reshape in progres)\n");
11861 if (completed
> position_to_set
) {
11862 dprintf("wrong next position to set %llu (%llu)\n",
11863 to_complete
, position_to_set
);
11867 dprintf("Position set: %llu\n", position_to_set
);
11868 if (sysfs_set_num(sra
, NULL
, "sync_max",
11869 position_to_set
) != 0) {
11870 dprintf("cannot set reshape position to %llu\n",
11879 int timeout
= 3000;
11881 sysfs_wait(fd
, &timeout
);
11882 if (sysfs_get_str(sra
, NULL
, "sync_action",
11884 strncmp(action
, "reshape", 7) != 0) {
11885 if (strncmp(action
, "idle", 4) == 0)
11891 rc
= read_completed(fd
, &completed
);
11893 dprintf("cannot read reshape_position (in loop)\n");
11896 } else if (rc
== COMPLETED_NONE
)
11898 } while (completed
< position_to_set
);
11904 /*******************************************************************************
11905 * Function: check_degradation_change
11906 * Description: Check that array hasn't become failed.
11908 * info : for sysfs access
11909 * sources : source disks descriptors
11910 * degraded: previous degradation level
11912 * degradation level
11913 ******************************************************************************/
11914 int check_degradation_change(struct mdinfo
*info
,
11918 unsigned long long new_degraded
;
11921 rv
= sysfs_get_ll(info
, NULL
, "degraded", &new_degraded
);
11922 if (rv
== -1 || (new_degraded
!= (unsigned long long)degraded
)) {
11923 /* check each device to ensure it is still working */
11926 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
11927 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
11929 if (sd
->disk
.state
& (1<<MD_DISK_SYNC
)) {
11932 if (sysfs_get_str(info
,
11933 sd
, "state", sbuf
, sizeof(sbuf
)) < 0 ||
11934 strstr(sbuf
, "faulty") ||
11935 strstr(sbuf
, "in_sync") == NULL
) {
11936 /* this device is dead */
11937 sd
->disk
.state
= (1<<MD_DISK_FAULTY
);
11938 if (sd
->disk
.raid_disk
>= 0 &&
11939 sources
[sd
->disk
.raid_disk
] >= 0) {
11941 sd
->disk
.raid_disk
]);
11942 sources
[sd
->disk
.raid_disk
] =
11951 return new_degraded
;
11954 /*******************************************************************************
11955 * Function: imsm_manage_reshape
11956 * Description: Function finds array under reshape and it manages reshape
11957 * process. It creates stripes backups (if required) and sets
11960 * afd : Backup handle (nattive) - not used
11961 * sra : general array info
11962 * reshape : reshape parameters - not used
11963 * st : supertype structure
11964 * blocks : size of critical section [blocks]
11965 * fds : table of source device descriptor
11966 * offsets : start of array (offest per devices)
11968 * destfd : table of destination device descriptor
11969 * destoffsets : table of destination offsets (per device)
11971 * 1 : success, reshape is done
11973 ******************************************************************************/
11974 static int imsm_manage_reshape(
11975 int afd
, struct mdinfo
*sra
, struct reshape
*reshape
,
11976 struct supertype
*st
, unsigned long backup_blocks
,
11977 int *fds
, unsigned long long *offsets
,
11978 int dests
, int *destfd
, unsigned long long *destoffsets
)
11981 struct intel_super
*super
= st
->sb
;
11982 struct intel_dev
*dv
;
11983 unsigned int sector_size
= super
->sector_size
;
11984 struct imsm_dev
*dev
= NULL
;
11985 struct imsm_map
*map_src
, *map_dest
;
11986 int migr_vol_qan
= 0;
11987 int ndata
, odata
; /* [bytes] */
11988 int chunk
; /* [bytes] */
11989 struct migr_record
*migr_rec
;
11991 unsigned int buf_size
; /* [bytes] */
11992 unsigned long long max_position
; /* array size [bytes] */
11993 unsigned long long next_step
; /* [blocks]/[bytes] */
11994 unsigned long long old_data_stripe_length
;
11995 unsigned long long start_src
; /* [bytes] */
11996 unsigned long long start
; /* [bytes] */
11997 unsigned long long start_buf_shift
; /* [bytes] */
11999 int source_layout
= 0;
12004 if (!fds
|| !offsets
)
12007 /* Find volume during the reshape */
12008 for (dv
= super
->devlist
; dv
; dv
= dv
->next
) {
12009 if (dv
->dev
->vol
.migr_type
== MIGR_GEN_MIGR
&&
12010 dv
->dev
->vol
.migr_state
== 1) {
12015 /* Only one volume can migrate at the same time */
12016 if (migr_vol_qan
!= 1) {
12017 pr_err("%s", migr_vol_qan
?
12018 "Number of migrating volumes greater than 1\n" :
12019 "There is no volume during migrationg\n");
12023 map_dest
= get_imsm_map(dev
, MAP_0
);
12024 map_src
= get_imsm_map(dev
, MAP_1
);
12025 if (map_src
== NULL
)
12028 ndata
= imsm_num_data_members(map_dest
);
12029 odata
= imsm_num_data_members(map_src
);
12031 chunk
= __le16_to_cpu(map_src
->blocks_per_strip
) * 512;
12032 old_data_stripe_length
= odata
* chunk
;
12034 migr_rec
= super
->migr_rec
;
12036 /* initialize migration record for start condition */
12037 if (sra
->reshape_progress
== 0)
12038 init_migr_record_imsm(st
, dev
, sra
);
12040 if (__le32_to_cpu(migr_rec
->rec_status
) != UNIT_SRC_NORMAL
) {
12041 dprintf("imsm: cannot restart migration when data are present in copy area.\n");
12044 /* Save checkpoint to update migration record for current
12045 * reshape position (in md). It can be farther than current
12046 * reshape position in metadata.
12048 if (save_checkpoint_imsm(st
, sra
, UNIT_SRC_NORMAL
) == 1) {
12049 /* ignore error == 2, this can mean end of reshape here
12051 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL, initial save)\n");
12056 /* size for data */
12057 buf_size
= __le32_to_cpu(migr_rec
->blocks_per_unit
) * 512;
12058 /* extend buffer size for parity disk */
12059 buf_size
+= __le32_to_cpu(migr_rec
->dest_depth_per_unit
) * 512;
12060 /* add space for stripe alignment */
12061 buf_size
+= old_data_stripe_length
;
12062 if (posix_memalign((void **)&buf
, MAX_SECTOR_SIZE
, buf_size
)) {
12063 dprintf("imsm: Cannot allocate checkpoint buffer\n");
12067 max_position
= sra
->component_size
* ndata
;
12068 source_layout
= imsm_level_to_layout(map_src
->raid_level
);
12070 while (current_migr_unit(migr_rec
) <
12071 get_num_migr_units(migr_rec
)) {
12072 /* current reshape position [blocks] */
12073 unsigned long long current_position
=
12074 __le32_to_cpu(migr_rec
->blocks_per_unit
)
12075 * current_migr_unit(migr_rec
);
12076 unsigned long long border
;
12078 /* Check that array hasn't become failed.
12080 degraded
= check_degradation_change(sra
, fds
, degraded
);
12081 if (degraded
> 1) {
12082 dprintf("imsm: Abort reshape due to degradation level (%i)\n", degraded
);
12086 next_step
= __le32_to_cpu(migr_rec
->blocks_per_unit
);
12088 if ((current_position
+ next_step
) > max_position
)
12089 next_step
= max_position
- current_position
;
12091 start
= current_position
* 512;
12093 /* align reading start to old geometry */
12094 start_buf_shift
= start
% old_data_stripe_length
;
12095 start_src
= start
- start_buf_shift
;
12097 border
= (start_src
/ odata
) - (start
/ ndata
);
12099 if (border
<= __le32_to_cpu(migr_rec
->dest_depth_per_unit
)) {
12100 /* save critical stripes to buf
12101 * start - start address of current unit
12102 * to backup [bytes]
12103 * start_src - start address of current unit
12104 * to backup alligned to source array
12107 unsigned long long next_step_filler
;
12108 unsigned long long copy_length
= next_step
* 512;
12110 /* allign copy area length to stripe in old geometry */
12111 next_step_filler
= ((copy_length
+ start_buf_shift
)
12112 % old_data_stripe_length
);
12113 if (next_step_filler
)
12114 next_step_filler
= (old_data_stripe_length
12115 - next_step_filler
);
12116 dprintf("save_stripes() parameters: start = %llu,\tstart_src = %llu,\tnext_step*512 = %llu,\tstart_in_buf_shift = %llu,\tnext_step_filler = %llu\n",
12117 start
, start_src
, copy_length
,
12118 start_buf_shift
, next_step_filler
);
12120 if (save_stripes(fds
, offsets
, map_src
->num_members
,
12121 chunk
, map_src
->raid_level
,
12122 source_layout
, 0, NULL
, start_src
,
12124 next_step_filler
+ start_buf_shift
,
12126 dprintf("imsm: Cannot save stripes to buffer\n");
12129 /* Convert data to destination format and store it
12130 * in backup general migration area
12132 if (save_backup_imsm(st
, dev
, sra
,
12133 buf
+ start_buf_shift
, copy_length
)) {
12134 dprintf("imsm: Cannot save stripes to target devices\n");
12137 if (save_checkpoint_imsm(st
, sra
,
12138 UNIT_SRC_IN_CP_AREA
)) {
12139 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_IN_CP_AREA)\n");
12143 /* set next step to use whole border area */
12144 border
/= next_step
;
12146 next_step
*= border
;
12148 /* When data backed up, checkpoint stored,
12149 * kick the kernel to reshape unit of data
12151 next_step
= next_step
+ sra
->reshape_progress
;
12152 /* limit next step to array max position */
12153 if (next_step
> max_position
)
12154 next_step
= max_position
;
12155 sysfs_set_num(sra
, NULL
, "suspend_lo", sra
->reshape_progress
);
12156 sysfs_set_num(sra
, NULL
, "suspend_hi", next_step
);
12157 sra
->reshape_progress
= next_step
;
12159 /* wait until reshape finish */
12160 if (wait_for_reshape_imsm(sra
, ndata
)) {
12161 dprintf("wait_for_reshape_imsm returned error!\n");
12167 if (save_checkpoint_imsm(st
, sra
, UNIT_SRC_NORMAL
) == 1) {
12168 /* ignore error == 2, this can mean end of reshape here
12170 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL)\n");
12176 /* clear migr_rec on disks after successful migration */
12179 memset(super
->migr_rec_buf
, 0, MIGR_REC_BUF_SECTORS
*MAX_SECTOR_SIZE
);
12180 for (d
= super
->disks
; d
; d
= d
->next
) {
12181 if (d
->index
< 0 || is_failed(&d
->disk
))
12183 unsigned long long dsize
;
12185 get_dev_size(d
->fd
, NULL
, &dsize
);
12186 if (lseek64(d
->fd
, dsize
- MIGR_REC_SECTOR_POSITION
*sector_size
,
12188 if ((unsigned int)write(d
->fd
, super
->migr_rec_buf
,
12189 MIGR_REC_BUF_SECTORS
*sector_size
) !=
12190 MIGR_REC_BUF_SECTORS
*sector_size
)
12191 perror("Write migr_rec failed");
12195 /* return '1' if done */
12199 /* See Grow.c: abort_reshape() for further explanation */
12200 sysfs_set_num(sra
, NULL
, "suspend_lo", 0x7FFFFFFFFFFFFFFFULL
);
12201 sysfs_set_num(sra
, NULL
, "suspend_hi", 0);
12202 sysfs_set_num(sra
, NULL
, "suspend_lo", 0);
12207 struct superswitch super_imsm
= {
12208 .examine_super
= examine_super_imsm
,
12209 .brief_examine_super
= brief_examine_super_imsm
,
12210 .brief_examine_subarrays
= brief_examine_subarrays_imsm
,
12211 .export_examine_super
= export_examine_super_imsm
,
12212 .detail_super
= detail_super_imsm
,
12213 .brief_detail_super
= brief_detail_super_imsm
,
12214 .write_init_super
= write_init_super_imsm
,
12215 .validate_geometry
= validate_geometry_imsm
,
12216 .add_to_super
= add_to_super_imsm
,
12217 .remove_from_super
= remove_from_super_imsm
,
12218 .detail_platform
= detail_platform_imsm
,
12219 .export_detail_platform
= export_detail_platform_imsm
,
12220 .kill_subarray
= kill_subarray_imsm
,
12221 .update_subarray
= update_subarray_imsm
,
12222 .load_container
= load_container_imsm
,
12223 .default_geometry
= default_geometry_imsm
,
12224 .get_disk_controller_domain
= imsm_get_disk_controller_domain
,
12225 .reshape_super
= imsm_reshape_super
,
12226 .manage_reshape
= imsm_manage_reshape
,
12227 .recover_backup
= recover_backup_imsm
,
12228 .examine_badblocks
= examine_badblocks_imsm
,
12229 .match_home
= match_home_imsm
,
12230 .uuid_from_super
= uuid_from_super_imsm
,
12231 .getinfo_super
= getinfo_super_imsm
,
12232 .getinfo_super_disks
= getinfo_super_disks_imsm
,
12233 .update_super
= update_super_imsm
,
12235 .avail_size
= avail_size_imsm
,
12236 .get_spare_criteria
= get_spare_criteria_imsm
,
12238 .compare_super
= compare_super_imsm
,
12240 .load_super
= load_super_imsm
,
12241 .init_super
= init_super_imsm
,
12242 .store_super
= store_super_imsm
,
12243 .free_super
= free_super_imsm
,
12244 .match_metadata_desc
= match_metadata_desc_imsm
,
12245 .container_content
= container_content_imsm
,
12246 .validate_container
= validate_container_imsm
,
12248 .write_init_ppl
= write_init_ppl_imsm
,
12249 .validate_ppl
= validate_ppl_imsm
,
12255 .open_new
= imsm_open_new
,
12256 .set_array_state
= imsm_set_array_state
,
12257 .set_disk
= imsm_set_disk
,
12258 .sync_metadata
= imsm_sync_metadata
,
12259 .activate_spare
= imsm_activate_spare
,
12260 .process_update
= imsm_process_update
,
12261 .prepare_update
= imsm_prepare_update
,
12262 .record_bad_block
= imsm_record_badblock
,
12263 .clear_bad_block
= imsm_clear_badblock
,
12264 .get_bad_blocks
= imsm_get_badblocks
,