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 * Internal Write-intent bitmap is stored in the same area where PPL.
101 * Both features are mutually exclusive, so it is not an issue.
102 * The first 8KiB of the area are reserved and shall not be used.
104 #define IMSM_BITMAP_AREA_RESERVED_SIZE 8192
106 #define IMSM_BITMAP_HEADER_OFFSET (IMSM_BITMAP_AREA_RESERVED_SIZE)
107 #define IMSM_BITMAP_HEADER_SIZE MAX_SECTOR_SIZE
109 #define IMSM_BITMAP_START_OFFSET (IMSM_BITMAP_HEADER_OFFSET + IMSM_BITMAP_HEADER_SIZE)
110 #define IMSM_BITMAP_AREA_SIZE (MULTIPLE_PPL_AREA_SIZE_IMSM - IMSM_BITMAP_START_OFFSET)
111 #define IMSM_BITMAP_AND_HEADER_SIZE (IMSM_BITMAP_AREA_SIZE + IMSM_BITMAP_HEADER_SIZE)
113 #define IMSM_DEFAULT_BITMAP_CHUNKSIZE (64 * 1024 * 1024)
114 #define IMSM_DEFAULT_BITMAP_DAEMON_SLEEP 5
117 * This macro let's us ensure that no-one accidentally
118 * changes the size of a struct
120 #define ASSERT_SIZE(_struct, size) \
121 static inline void __assert_size_##_struct(void) \
125 case (sizeof(struct _struct) == size): break; \
129 /* Disk configuration info. */
130 #define IMSM_MAX_DEVICES 255
132 __u8 serial
[MAX_RAID_SERIAL_LEN
];/* 0xD8 - 0xE7 ascii serial number */
133 __u32 total_blocks_lo
; /* 0xE8 - 0xEB total blocks lo */
134 __u32 scsi_id
; /* 0xEC - 0xEF scsi ID */
135 #define SPARE_DISK __cpu_to_le32(0x01) /* Spare */
136 #define CONFIGURED_DISK __cpu_to_le32(0x02) /* Member of some RaidDev */
137 #define FAILED_DISK __cpu_to_le32(0x04) /* Permanent failure */
138 #define JOURNAL_DISK __cpu_to_le32(0x2000000) /* Device marked as Journaling Drive */
139 __u32 status
; /* 0xF0 - 0xF3 */
140 __u32 owner_cfg_num
; /* which config 0,1,2... owns this disk */
141 __u32 total_blocks_hi
; /* 0xF4 - 0xF5 total blocks hi */
142 #define IMSM_DISK_FILLERS 3
143 __u32 filler
[IMSM_DISK_FILLERS
]; /* 0xF5 - 0x107 MPB_DISK_FILLERS for future expansion */
145 ASSERT_SIZE(imsm_disk
, 48)
147 /* map selector for map managment
153 /* RAID map configuration infos. */
155 __u32 pba_of_lba0_lo
; /* start address of partition */
156 __u32 blocks_per_member_lo
;/* blocks per member */
157 __u32 num_data_stripes_lo
; /* number of data stripes */
158 __u16 blocks_per_strip
;
159 __u8 map_state
; /* Normal, Uninitialized, Degraded, Failed */
160 #define IMSM_T_STATE_NORMAL 0
161 #define IMSM_T_STATE_UNINITIALIZED 1
162 #define IMSM_T_STATE_DEGRADED 2
163 #define IMSM_T_STATE_FAILED 3
165 #define IMSM_T_RAID0 0
166 #define IMSM_T_RAID1 1
167 #define IMSM_T_RAID5 5 /* since metadata version 1.2.02 ? */
168 __u8 num_members
; /* number of member disks */
169 __u8 num_domains
; /* number of parity domains */
170 __u8 failed_disk_num
; /* valid only when state is degraded */
172 __u32 pba_of_lba0_hi
;
173 __u32 blocks_per_member_hi
;
174 __u32 num_data_stripes_hi
;
175 __u32 filler
[4]; /* expansion area */
176 #define IMSM_ORD_REBUILD (1 << 24)
177 __u32 disk_ord_tbl
[1]; /* disk_ord_tbl[num_members],
178 * top byte contains some flags
181 ASSERT_SIZE(imsm_map
, 52)
184 __u32 curr_migr_unit_lo
;
185 __u32 checkpoint_id
; /* id to access curr_migr_unit */
186 __u8 migr_state
; /* Normal or Migrating */
188 #define MIGR_REBUILD 1
189 #define MIGR_VERIFY 2 /* analagous to echo check > sync_action */
190 #define MIGR_GEN_MIGR 3
191 #define MIGR_STATE_CHANGE 4
192 #define MIGR_REPAIR 5
193 __u8 migr_type
; /* Initializing, Rebuilding, ... */
194 #define RAIDVOL_CLEAN 0
195 #define RAIDVOL_DIRTY 1
196 #define RAIDVOL_DSRECORD_VALID 2
198 __u8 fs_state
; /* fast-sync state for CnG (0xff == disabled) */
199 __u16 verify_errors
; /* number of mismatches */
200 __u16 bad_blocks
; /* number of bad blocks during verify */
201 __u32 curr_migr_unit_hi
;
203 struct imsm_map map
[1];
204 /* here comes another one if migr_state */
206 ASSERT_SIZE(imsm_vol
, 84)
209 __u8 volume
[MAX_RAID_SERIAL_LEN
];
212 #define DEV_BOOTABLE __cpu_to_le32(0x01)
213 #define DEV_BOOT_DEVICE __cpu_to_le32(0x02)
214 #define DEV_READ_COALESCING __cpu_to_le32(0x04)
215 #define DEV_WRITE_COALESCING __cpu_to_le32(0x08)
216 #define DEV_LAST_SHUTDOWN_DIRTY __cpu_to_le32(0x10)
217 #define DEV_HIDDEN_AT_BOOT __cpu_to_le32(0x20)
218 #define DEV_CURRENTLY_HIDDEN __cpu_to_le32(0x40)
219 #define DEV_VERIFY_AND_FIX __cpu_to_le32(0x80)
220 #define DEV_MAP_STATE_UNINIT __cpu_to_le32(0x100)
221 #define DEV_NO_AUTO_RECOVERY __cpu_to_le32(0x200)
222 #define DEV_CLONE_N_GO __cpu_to_le32(0x400)
223 #define DEV_CLONE_MAN_SYNC __cpu_to_le32(0x800)
224 #define DEV_CNG_MASTER_DISK_NUM __cpu_to_le32(0x1000)
225 __u32 status
; /* Persistent RaidDev status */
226 __u32 reserved_blocks
; /* Reserved blocks at beginning of volume */
230 __u8 cng_master_disk
;
234 __u16 my_vol_raid_dev_num
; /* Used in Unique volume Id for this RaidDev */
240 /* Unique Volume Id of the NvCache Volume associated with this volume */
241 __u32 nvc_vol_orig_family_num
;
242 __u16 nvc_vol_raid_dev_num
;
245 #define RWH_DISTRIBUTED 1
246 #define RWH_JOURNALING_DRIVE 2
247 #define RWH_MULTIPLE_DISTRIBUTED 3
248 #define RWH_MULTIPLE_PPLS_JOURNALING_DRIVE 4
249 #define RWH_MULTIPLE_OFF 5
251 __u8 rwh_policy
; /* Raid Write Hole Policy */
252 __u8 jd_serial
[MAX_RAID_SERIAL_LEN
]; /* Journal Drive serial number */
255 #define IMSM_DEV_FILLERS 3
256 __u32 filler
[IMSM_DEV_FILLERS
];
259 ASSERT_SIZE(imsm_dev
, 164)
262 __u8 sig
[MAX_SIGNATURE_LENGTH
]; /* 0x00 - 0x1F */
263 __u32 check_sum
; /* 0x20 - 0x23 MPB Checksum */
264 __u32 mpb_size
; /* 0x24 - 0x27 Size of MPB */
265 __u32 family_num
; /* 0x28 - 0x2B Checksum from first time this config was written */
266 __u32 generation_num
; /* 0x2C - 0x2F Incremented each time this array's MPB is written */
267 __u32 error_log_size
; /* 0x30 - 0x33 in bytes */
268 __u32 attributes
; /* 0x34 - 0x37 */
269 __u8 num_disks
; /* 0x38 Number of configured disks */
270 __u8 num_raid_devs
; /* 0x39 Number of configured volumes */
271 __u8 error_log_pos
; /* 0x3A */
272 __u8 fill
[1]; /* 0x3B */
273 __u32 cache_size
; /* 0x3c - 0x40 in mb */
274 __u32 orig_family_num
; /* 0x40 - 0x43 original family num */
275 __u32 pwr_cycle_count
; /* 0x44 - 0x47 simulated power cycle count for array */
276 __u32 bbm_log_size
; /* 0x48 - 0x4B - size of bad Block Mgmt Log in bytes */
277 __u16 num_raid_devs_created
; /* 0x4C - 0x4D Used for generating unique
278 * volume IDs for raid_dev created in this array
281 __u16 filler1
; /* 0x4E - 0x4F */
282 __u64 creation_time
; /* 0x50 - 0x57 Array creation time */
283 #define IMSM_FILLERS 32
284 __u32 filler
[IMSM_FILLERS
]; /* 0x58 - 0xD7 RAID_MPB_FILLERS */
285 struct imsm_disk disk
[1]; /* 0xD8 diskTbl[numDisks] */
286 /* here comes imsm_dev[num_raid_devs] */
287 /* here comes BBM logs */
289 ASSERT_SIZE(imsm_super
, 264)
291 #define BBM_LOG_MAX_ENTRIES 254
292 #define BBM_LOG_MAX_LBA_ENTRY_VAL 256 /* Represents 256 LBAs */
293 #define BBM_LOG_SIGNATURE 0xabadb10c
295 struct bbm_log_block_addr
{
298 } __attribute__ ((__packed__
));
300 struct bbm_log_entry
{
301 __u8 marked_count
; /* Number of blocks marked - 1 */
302 __u8 disk_ordinal
; /* Disk entry within the imsm_super */
303 struct bbm_log_block_addr defective_block_start
;
304 } __attribute__ ((__packed__
));
307 __u32 signature
; /* 0xABADB10C */
309 struct bbm_log_entry marked_block_entries
[BBM_LOG_MAX_ENTRIES
];
311 ASSERT_SIZE(bbm_log
, 2040)
313 static char *map_state_str
[] = { "normal", "uninitialized", "degraded", "failed" };
315 #define BLOCKS_PER_KB (1024/512)
317 #define RAID_DISK_RESERVED_BLOCKS_IMSM_HI 2209
319 #define GEN_MIGR_AREA_SIZE 2048 /* General Migration Copy Area size in blocks */
321 #define MIGR_REC_BUF_SECTORS 1 /* size of migr_record i/o buffer in sectors */
322 #define MIGR_REC_SECTOR_POSITION 1 /* migr_record position offset on disk,
323 * MIGR_REC_BUF_SECTORS <= MIGR_REC_SECTOR_POS
326 #define UNIT_SRC_NORMAL 0 /* Source data for curr_migr_unit must
327 * be recovered using srcMap */
328 #define UNIT_SRC_IN_CP_AREA 1 /* Source data for curr_migr_unit has
329 * already been migrated and must
330 * be recovered from checkpoint area */
332 #define PPL_ENTRY_SPACE (128 * 1024) /* Size of single PPL, without the header */
335 __u32 rec_status
; /* Status used to determine how to restart
336 * migration in case it aborts
338 __u32 curr_migr_unit_lo
; /* 0..numMigrUnits-1 */
339 __u32 family_num
; /* Family number of MPB
340 * containing the RaidDev
341 * that is migrating */
342 __u32 ascending_migr
; /* True if migrating in increasing
344 __u32 blocks_per_unit
; /* Num disk blocks per unit of operation */
345 __u32 dest_depth_per_unit
; /* Num member blocks each destMap
347 * advances per unit-of-operation */
348 __u32 ckpt_area_pba_lo
; /* Pba of first block of ckpt copy area */
349 __u32 dest_1st_member_lba_lo
; /* First member lba on first
350 * stripe of destination */
351 __u32 num_migr_units_lo
; /* Total num migration units-of-op */
352 __u32 post_migr_vol_cap
; /* Size of volume after
353 * migration completes */
354 __u32 post_migr_vol_cap_hi
; /* Expansion space for LBA64 */
355 __u32 ckpt_read_disk_num
; /* Which member disk in destSubMap[0] the
356 * migration ckpt record was read from
357 * (for recovered migrations) */
358 __u32 curr_migr_unit_hi
; /* 0..numMigrUnits-1 high order 32 bits */
359 __u32 ckpt_area_pba_hi
; /* Pba of first block of ckpt copy area
360 * high order 32 bits */
361 __u32 dest_1st_member_lba_hi
; /* First member lba on first stripe of
362 * destination - high order 32 bits */
363 __u32 num_migr_units_hi
; /* Total num migration units-of-op
364 * high order 32 bits */
367 ASSERT_SIZE(migr_record
, 128)
372 * 2: metadata does not match
380 struct md_list
*next
;
383 #define pr_vrb(fmt, arg...) (void) (verbose && pr_err(fmt, ##arg))
385 static __u8
migr_type(struct imsm_dev
*dev
)
387 if (dev
->vol
.migr_type
== MIGR_VERIFY
&&
388 dev
->status
& DEV_VERIFY_AND_FIX
)
391 return dev
->vol
.migr_type
;
394 static void set_migr_type(struct imsm_dev
*dev
, __u8 migr_type
)
396 /* for compatibility with older oroms convert MIGR_REPAIR, into
397 * MIGR_VERIFY w/ DEV_VERIFY_AND_FIX status
399 if (migr_type
== MIGR_REPAIR
) {
400 dev
->vol
.migr_type
= MIGR_VERIFY
;
401 dev
->status
|= DEV_VERIFY_AND_FIX
;
403 dev
->vol
.migr_type
= migr_type
;
404 dev
->status
&= ~DEV_VERIFY_AND_FIX
;
408 static unsigned int sector_count(__u32 bytes
, unsigned int sector_size
)
410 return ROUND_UP(bytes
, sector_size
) / sector_size
;
413 static unsigned int mpb_sectors(struct imsm_super
*mpb
,
414 unsigned int sector_size
)
416 return sector_count(__le32_to_cpu(mpb
->mpb_size
), sector_size
);
420 struct imsm_dev
*dev
;
421 struct intel_dev
*next
;
426 enum sys_dev_type type
;
429 struct intel_hba
*next
;
436 /* internal representation of IMSM metadata */
439 void *buf
; /* O_DIRECT buffer for reading/writing metadata */
440 struct imsm_super
*anchor
; /* immovable parameters */
443 void *migr_rec_buf
; /* buffer for I/O operations */
444 struct migr_record
*migr_rec
; /* migration record */
446 int clean_migration_record_by_mdmon
; /* when reshape is switched to next
447 array, it indicates that mdmon is allowed to clean migration
449 size_t len
; /* size of the 'buf' allocation */
450 size_t extra_space
; /* extra space in 'buf' that is not used yet */
451 void *next_buf
; /* for realloc'ing buf from the manager */
453 int updates_pending
; /* count of pending updates for mdmon */
454 int current_vol
; /* index of raid device undergoing creation */
455 unsigned long long create_offset
; /* common start for 'current_vol' */
456 __u32 random
; /* random data for seeding new family numbers */
457 struct intel_dev
*devlist
;
458 unsigned int sector_size
; /* sector size of used member drives */
462 __u8 serial
[MAX_RAID_SERIAL_LEN
];
465 struct imsm_disk disk
;
468 struct extent
*e
; /* for determining freespace @ create */
469 int raiddisk
; /* slot to fill in autolayout */
471 } *disks
, *current_disk
;
472 struct dl
*disk_mgmt_list
; /* list of disks to add/remove while mdmon
474 struct dl
*missing
; /* disks removed while we weren't looking */
475 struct bbm_log
*bbm_log
;
476 struct intel_hba
*hba
; /* device path of the raid controller for this metadata */
477 const struct imsm_orom
*orom
; /* platform firmware support */
478 struct intel_super
*next
; /* (temp) list for disambiguating family_num */
479 struct md_bb bb
; /* memory for get_bad_blocks call */
483 struct imsm_disk disk
;
484 #define IMSM_UNKNOWN_OWNER (-1)
486 struct intel_disk
*next
;
490 unsigned long long start
, size
;
493 /* definitions of reshape process types */
494 enum imsm_reshape_type
{
500 /* definition of messages passed to imsm_process_update */
501 enum imsm_update_type
{
502 update_activate_spare
,
506 update_add_remove_disk
,
507 update_reshape_container_disks
,
508 update_reshape_migration
,
510 update_general_migration_checkpoint
,
512 update_prealloc_badblocks_mem
,
516 struct imsm_update_activate_spare
{
517 enum imsm_update_type type
;
521 struct imsm_update_activate_spare
*next
;
527 unsigned long long size
;
534 enum takeover_direction
{
538 struct imsm_update_takeover
{
539 enum imsm_update_type type
;
541 enum takeover_direction direction
;
544 struct imsm_update_reshape
{
545 enum imsm_update_type type
;
549 int new_disks
[1]; /* new_raid_disks - old_raid_disks makedev number */
552 struct imsm_update_reshape_migration
{
553 enum imsm_update_type type
;
556 /* fields for array migration changes
563 int new_disks
[1]; /* new_raid_disks - old_raid_disks makedev number */
566 struct imsm_update_size_change
{
567 enum imsm_update_type type
;
572 struct imsm_update_general_migration_checkpoint
{
573 enum imsm_update_type type
;
574 __u64 curr_migr_unit
;
578 __u8 serial
[MAX_RAID_SERIAL_LEN
];
581 struct imsm_update_create_array
{
582 enum imsm_update_type type
;
587 struct imsm_update_kill_array
{
588 enum imsm_update_type type
;
592 struct imsm_update_rename_array
{
593 enum imsm_update_type type
;
594 __u8 name
[MAX_RAID_SERIAL_LEN
];
598 struct imsm_update_add_remove_disk
{
599 enum imsm_update_type type
;
602 struct imsm_update_prealloc_bb_mem
{
603 enum imsm_update_type type
;
606 struct imsm_update_rwh_policy
{
607 enum imsm_update_type type
;
612 static const char *_sys_dev_type
[] = {
613 [SYS_DEV_UNKNOWN
] = "Unknown",
614 [SYS_DEV_SAS
] = "SAS",
615 [SYS_DEV_SATA
] = "SATA",
616 [SYS_DEV_NVME
] = "NVMe",
617 [SYS_DEV_VMD
] = "VMD"
620 const char *get_sys_dev_type(enum sys_dev_type type
)
622 if (type
>= SYS_DEV_MAX
)
623 type
= SYS_DEV_UNKNOWN
;
625 return _sys_dev_type
[type
];
628 static struct intel_hba
* alloc_intel_hba(struct sys_dev
*device
)
630 struct intel_hba
*result
= xmalloc(sizeof(*result
));
632 result
->type
= device
->type
;
633 result
->path
= xstrdup(device
->path
);
635 if (result
->path
&& (result
->pci_id
= strrchr(result
->path
, '/')) != NULL
)
641 static struct intel_hba
* find_intel_hba(struct intel_hba
*hba
, struct sys_dev
*device
)
643 struct intel_hba
*result
;
645 for (result
= hba
; result
; result
= result
->next
) {
646 if (result
->type
== device
->type
&& strcmp(result
->path
, device
->path
) == 0)
652 static int attach_hba_to_super(struct intel_super
*super
, struct sys_dev
*device
)
654 struct intel_hba
*hba
;
656 /* check if disk attached to Intel HBA */
657 hba
= find_intel_hba(super
->hba
, device
);
660 /* Check if HBA is already attached to super */
661 if (super
->hba
== NULL
) {
662 super
->hba
= alloc_intel_hba(device
);
667 /* Intel metadata allows for all disks attached to the same type HBA.
668 * Do not support HBA types mixing
670 if (device
->type
!= hba
->type
)
673 /* Multiple same type HBAs can be used if they share the same OROM */
674 const struct imsm_orom
*device_orom
= get_orom_by_device_id(device
->dev_id
);
676 if (device_orom
!= super
->orom
)
682 hba
->next
= alloc_intel_hba(device
);
686 static struct sys_dev
* find_disk_attached_hba(int fd
, const char *devname
)
688 struct sys_dev
*list
, *elem
;
691 if ((list
= find_intel_devices()) == NULL
)
694 if (!is_fd_valid(fd
))
695 disk_path
= (char *) devname
;
697 disk_path
= diskfd_to_devpath(fd
, 1, NULL
);
702 for (elem
= list
; elem
; elem
= elem
->next
)
703 if (path_attached_to_hba(disk_path
, elem
->path
))
706 if (disk_path
!= devname
)
712 static int find_intel_hba_capability(int fd
, struct intel_super
*super
,
715 static struct supertype
*match_metadata_desc_imsm(char *arg
)
717 struct supertype
*st
;
719 if (strcmp(arg
, "imsm") != 0 &&
720 strcmp(arg
, "default") != 0
724 st
= xcalloc(1, sizeof(*st
));
725 st
->ss
= &super_imsm
;
726 st
->max_devs
= IMSM_MAX_DEVICES
;
727 st
->minor_version
= 0;
732 static __u8
*get_imsm_version(struct imsm_super
*mpb
)
734 return &mpb
->sig
[MPB_SIG_LEN
];
737 /* retrieve a disk directly from the anchor when the anchor is known to be
738 * up-to-date, currently only at load time
740 static struct imsm_disk
*__get_imsm_disk(struct imsm_super
*mpb
, __u8 index
)
742 if (index
>= mpb
->num_disks
)
744 return &mpb
->disk
[index
];
747 /* retrieve the disk description based on a index of the disk
750 static struct dl
*get_imsm_dl_disk(struct intel_super
*super
, __u8 index
)
754 for (d
= super
->disks
; d
; d
= d
->next
)
755 if (d
->index
== index
)
760 /* retrieve a disk from the parsed metadata */
761 static struct imsm_disk
*get_imsm_disk(struct intel_super
*super
, __u8 index
)
765 dl
= get_imsm_dl_disk(super
, index
);
772 /* generate a checksum directly from the anchor when the anchor is known to be
773 * up-to-date, currently only at load or write_super after coalescing
775 static __u32
__gen_imsm_checksum(struct imsm_super
*mpb
)
777 __u32 end
= mpb
->mpb_size
/ sizeof(end
);
778 __u32
*p
= (__u32
*) mpb
;
782 sum
+= __le32_to_cpu(*p
);
786 return sum
- __le32_to_cpu(mpb
->check_sum
);
789 static size_t sizeof_imsm_map(struct imsm_map
*map
)
791 return sizeof(struct imsm_map
) + sizeof(__u32
) * (map
->num_members
- 1);
794 struct imsm_map
*get_imsm_map(struct imsm_dev
*dev
, int second_map
)
796 /* A device can have 2 maps if it is in the middle of a migration.
798 * MAP_0 - we return the first map
799 * MAP_1 - we return the second map if it exists, else NULL
800 * MAP_X - we return the second map if it exists, else the first
802 struct imsm_map
*map
= &dev
->vol
.map
[0];
803 struct imsm_map
*map2
= NULL
;
805 if (dev
->vol
.migr_state
)
806 map2
= (void *)map
+ sizeof_imsm_map(map
);
808 switch (second_map
) {
825 /* return the size of the device.
826 * migr_state increases the returned size if map[0] were to be duplicated
828 static size_t sizeof_imsm_dev(struct imsm_dev
*dev
, int migr_state
)
830 size_t size
= sizeof(*dev
) - sizeof(struct imsm_map
) +
831 sizeof_imsm_map(get_imsm_map(dev
, MAP_0
));
833 /* migrating means an additional map */
834 if (dev
->vol
.migr_state
)
835 size
+= sizeof_imsm_map(get_imsm_map(dev
, MAP_1
));
837 size
+= sizeof_imsm_map(get_imsm_map(dev
, MAP_0
));
842 /* retrieve disk serial number list from a metadata update */
843 static struct disk_info
*get_disk_info(struct imsm_update_create_array
*update
)
846 struct disk_info
*inf
;
848 inf
= u
+ sizeof(*update
) - sizeof(struct imsm_dev
) +
849 sizeof_imsm_dev(&update
->dev
, 0);
854 static struct imsm_dev
*__get_imsm_dev(struct imsm_super
*mpb
, __u8 index
)
860 if (index
>= mpb
->num_raid_devs
)
863 /* devices start after all disks */
864 offset
= ((void *) &mpb
->disk
[mpb
->num_disks
]) - _mpb
;
866 for (i
= 0; i
<= index
; i
++)
868 return _mpb
+ offset
;
870 offset
+= sizeof_imsm_dev(_mpb
+ offset
, 0);
875 static struct imsm_dev
*get_imsm_dev(struct intel_super
*super
, __u8 index
)
877 struct intel_dev
*dv
;
879 if (index
>= super
->anchor
->num_raid_devs
)
881 for (dv
= super
->devlist
; dv
; dv
= dv
->next
)
882 if (dv
->index
== index
)
887 static inline unsigned long long __le48_to_cpu(const struct bbm_log_block_addr
890 return ((((__u64
)__le32_to_cpu(addr
->dw1
)) << 16) |
891 __le16_to_cpu(addr
->w1
));
894 static inline struct bbm_log_block_addr
__cpu_to_le48(unsigned long long sec
)
896 struct bbm_log_block_addr addr
;
898 addr
.w1
= __cpu_to_le16((__u16
)(sec
& 0xffff));
899 addr
.dw1
= __cpu_to_le32((__u32
)(sec
>> 16) & 0xffffffff);
903 /* get size of the bbm log */
904 static __u32
get_imsm_bbm_log_size(struct bbm_log
*log
)
906 if (!log
|| log
->entry_count
== 0)
909 return sizeof(log
->signature
) +
910 sizeof(log
->entry_count
) +
911 log
->entry_count
* sizeof(struct bbm_log_entry
);
914 /* check if bad block is not partially stored in bbm log */
915 static int is_stored_in_bbm(struct bbm_log
*log
, const __u8 idx
, const unsigned
916 long long sector
, const int length
, __u32
*pos
)
920 for (i
= *pos
; i
< log
->entry_count
; i
++) {
921 struct bbm_log_entry
*entry
= &log
->marked_block_entries
[i
];
922 unsigned long long bb_start
;
923 unsigned long long bb_end
;
925 bb_start
= __le48_to_cpu(&entry
->defective_block_start
);
926 bb_end
= bb_start
+ (entry
->marked_count
+ 1);
928 if ((entry
->disk_ordinal
== idx
) && (bb_start
>= sector
) &&
929 (bb_end
<= sector
+ length
)) {
937 /* record new bad block in bbm log */
938 static int record_new_badblock(struct bbm_log
*log
, const __u8 idx
, unsigned
939 long long sector
, int length
)
943 struct bbm_log_entry
*entry
= NULL
;
945 while (is_stored_in_bbm(log
, idx
, sector
, length
, &pos
)) {
946 struct bbm_log_entry
*e
= &log
->marked_block_entries
[pos
];
948 if ((e
->marked_count
+ 1 == BBM_LOG_MAX_LBA_ENTRY_VAL
) &&
949 (__le48_to_cpu(&e
->defective_block_start
) == sector
)) {
950 sector
+= BBM_LOG_MAX_LBA_ENTRY_VAL
;
951 length
-= BBM_LOG_MAX_LBA_ENTRY_VAL
;
960 int cnt
= (length
<= BBM_LOG_MAX_LBA_ENTRY_VAL
) ? length
:
961 BBM_LOG_MAX_LBA_ENTRY_VAL
;
962 entry
->defective_block_start
= __cpu_to_le48(sector
);
963 entry
->marked_count
= cnt
- 1;
970 new_bb
= ROUND_UP(length
, BBM_LOG_MAX_LBA_ENTRY_VAL
) /
971 BBM_LOG_MAX_LBA_ENTRY_VAL
;
972 if (log
->entry_count
+ new_bb
> BBM_LOG_MAX_ENTRIES
)
976 int cnt
= (length
<= BBM_LOG_MAX_LBA_ENTRY_VAL
) ? length
:
977 BBM_LOG_MAX_LBA_ENTRY_VAL
;
978 struct bbm_log_entry
*entry
=
979 &log
->marked_block_entries
[log
->entry_count
];
981 entry
->defective_block_start
= __cpu_to_le48(sector
);
982 entry
->marked_count
= cnt
- 1;
983 entry
->disk_ordinal
= idx
;
994 /* clear all bad blocks for given disk */
995 static void clear_disk_badblocks(struct bbm_log
*log
, const __u8 idx
)
999 while (i
< log
->entry_count
) {
1000 struct bbm_log_entry
*entries
= log
->marked_block_entries
;
1002 if (entries
[i
].disk_ordinal
== idx
) {
1003 if (i
< log
->entry_count
- 1)
1004 entries
[i
] = entries
[log
->entry_count
- 1];
1012 /* clear given bad block */
1013 static int clear_badblock(struct bbm_log
*log
, const __u8 idx
, const unsigned
1014 long long sector
, const int length
) {
1017 while (i
< log
->entry_count
) {
1018 struct bbm_log_entry
*entries
= log
->marked_block_entries
;
1020 if ((entries
[i
].disk_ordinal
== idx
) &&
1021 (__le48_to_cpu(&entries
[i
].defective_block_start
) ==
1022 sector
) && (entries
[i
].marked_count
+ 1 == length
)) {
1023 if (i
< log
->entry_count
- 1)
1024 entries
[i
] = entries
[log
->entry_count
- 1];
1034 /* allocate and load BBM log from metadata */
1035 static int load_bbm_log(struct intel_super
*super
)
1037 struct imsm_super
*mpb
= super
->anchor
;
1038 __u32 bbm_log_size
= __le32_to_cpu(mpb
->bbm_log_size
);
1040 super
->bbm_log
= xcalloc(1, sizeof(struct bbm_log
));
1041 if (!super
->bbm_log
)
1045 struct bbm_log
*log
= (void *)mpb
+
1046 __le32_to_cpu(mpb
->mpb_size
) - bbm_log_size
;
1050 if (bbm_log_size
< sizeof(log
->signature
) +
1051 sizeof(log
->entry_count
))
1054 entry_count
= __le32_to_cpu(log
->entry_count
);
1055 if ((__le32_to_cpu(log
->signature
) != BBM_LOG_SIGNATURE
) ||
1056 (entry_count
> BBM_LOG_MAX_ENTRIES
))
1060 sizeof(log
->signature
) + sizeof(log
->entry_count
) +
1061 entry_count
* sizeof(struct bbm_log_entry
))
1064 memcpy(super
->bbm_log
, log
, bbm_log_size
);
1066 super
->bbm_log
->signature
= __cpu_to_le32(BBM_LOG_SIGNATURE
);
1067 super
->bbm_log
->entry_count
= 0;
1073 /* checks if bad block is within volume boundaries */
1074 static int is_bad_block_in_volume(const struct bbm_log_entry
*entry
,
1075 const unsigned long long start_sector
,
1076 const unsigned long long size
)
1078 unsigned long long bb_start
;
1079 unsigned long long bb_end
;
1081 bb_start
= __le48_to_cpu(&entry
->defective_block_start
);
1082 bb_end
= bb_start
+ (entry
->marked_count
+ 1);
1084 if (((bb_start
>= start_sector
) && (bb_start
< start_sector
+ size
)) ||
1085 ((bb_end
>= start_sector
) && (bb_end
<= start_sector
+ size
)))
1091 /* get list of bad blocks on a drive for a volume */
1092 static void get_volume_badblocks(const struct bbm_log
*log
, const __u8 idx
,
1093 const unsigned long long start_sector
,
1094 const unsigned long long size
,
1100 for (i
= 0; i
< log
->entry_count
; i
++) {
1101 const struct bbm_log_entry
*ent
=
1102 &log
->marked_block_entries
[i
];
1103 struct md_bb_entry
*bb
;
1105 if ((ent
->disk_ordinal
== idx
) &&
1106 is_bad_block_in_volume(ent
, start_sector
, size
)) {
1108 if (!bbs
->entries
) {
1109 bbs
->entries
= xmalloc(BBM_LOG_MAX_ENTRIES
*
1115 bb
= &bbs
->entries
[count
++];
1116 bb
->sector
= __le48_to_cpu(&ent
->defective_block_start
);
1117 bb
->length
= ent
->marked_count
+ 1;
1125 * == MAP_0 get first map
1126 * == MAP_1 get second map
1127 * == MAP_X than get map according to the current migr_state
1129 static __u32
get_imsm_ord_tbl_ent(struct imsm_dev
*dev
,
1133 struct imsm_map
*map
;
1135 map
= get_imsm_map(dev
, second_map
);
1137 /* top byte identifies disk under rebuild */
1138 return __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
1141 #define ord_to_idx(ord) (((ord) << 8) >> 8)
1142 static __u32
get_imsm_disk_idx(struct imsm_dev
*dev
, int slot
, int second_map
)
1144 __u32 ord
= get_imsm_ord_tbl_ent(dev
, slot
, second_map
);
1146 return ord_to_idx(ord
);
1149 static void set_imsm_ord_tbl_ent(struct imsm_map
*map
, int slot
, __u32 ord
)
1151 map
->disk_ord_tbl
[slot
] = __cpu_to_le32(ord
);
1154 static int get_imsm_disk_slot(struct imsm_map
*map
, unsigned idx
)
1159 for (slot
= 0; slot
< map
->num_members
; slot
++) {
1160 ord
= __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
1161 if (ord_to_idx(ord
) == idx
)
1168 static int get_imsm_raid_level(struct imsm_map
*map
)
1170 if (map
->raid_level
== 1) {
1171 if (map
->num_members
== 2)
1177 return map
->raid_level
;
1180 static int cmp_extent(const void *av
, const void *bv
)
1182 const struct extent
*a
= av
;
1183 const struct extent
*b
= bv
;
1184 if (a
->start
< b
->start
)
1186 if (a
->start
> b
->start
)
1191 static int count_memberships(struct dl
*dl
, struct intel_super
*super
)
1193 int memberships
= 0;
1196 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
1197 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
1198 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
1200 if (get_imsm_disk_slot(map
, dl
->index
) >= 0)
1207 static __u32
imsm_min_reserved_sectors(struct intel_super
*super
);
1209 static int split_ull(unsigned long long n
, void *lo
, void *hi
)
1211 if (lo
== 0 || hi
== 0)
1213 __put_unaligned32(__cpu_to_le32((__u32
)n
), lo
);
1214 __put_unaligned32(__cpu_to_le32((n
>> 32)), hi
);
1218 static unsigned long long join_u32(__u32 lo
, __u32 hi
)
1220 return (unsigned long long)__le32_to_cpu(lo
) |
1221 (((unsigned long long)__le32_to_cpu(hi
)) << 32);
1224 static unsigned long long total_blocks(struct imsm_disk
*disk
)
1228 return join_u32(disk
->total_blocks_lo
, disk
->total_blocks_hi
);
1232 * imsm_num_data_members() - get data drives count for an array.
1233 * @map: Map to analyze.
1235 * num_data_members value represents minimal count of drives for level.
1236 * The name of the property could be misleading for RAID5 with asymmetric layout
1237 * because some data required to be calculated from parity.
1238 * The property is extracted from level and num_members value.
1240 * Return: num_data_members value on success, zero otherwise.
1242 static __u8
imsm_num_data_members(struct imsm_map
*map
)
1244 switch (get_imsm_raid_level(map
)) {
1246 return map
->num_members
;
1249 return map
->num_members
/ 2;
1251 return map
->num_members
- 1;
1253 dprintf("unsupported raid level\n");
1258 static unsigned long long pba_of_lba0(struct imsm_map
*map
)
1262 return join_u32(map
->pba_of_lba0_lo
, map
->pba_of_lba0_hi
);
1265 static unsigned long long blocks_per_member(struct imsm_map
*map
)
1269 return join_u32(map
->blocks_per_member_lo
, map
->blocks_per_member_hi
);
1272 static unsigned long long num_data_stripes(struct imsm_map
*map
)
1276 return join_u32(map
->num_data_stripes_lo
, map
->num_data_stripes_hi
);
1279 static unsigned long long vol_curr_migr_unit(struct imsm_dev
*dev
)
1284 return join_u32(dev
->vol
.curr_migr_unit_lo
, dev
->vol
.curr_migr_unit_hi
);
1287 static unsigned long long imsm_dev_size(struct imsm_dev
*dev
)
1291 return join_u32(dev
->size_low
, dev
->size_high
);
1294 static unsigned long long migr_chkp_area_pba(struct migr_record
*migr_rec
)
1296 if (migr_rec
== NULL
)
1298 return join_u32(migr_rec
->ckpt_area_pba_lo
,
1299 migr_rec
->ckpt_area_pba_hi
);
1302 static unsigned long long current_migr_unit(struct migr_record
*migr_rec
)
1304 if (migr_rec
== NULL
)
1306 return join_u32(migr_rec
->curr_migr_unit_lo
,
1307 migr_rec
->curr_migr_unit_hi
);
1310 static unsigned long long migr_dest_1st_member_lba(struct migr_record
*migr_rec
)
1312 if (migr_rec
== NULL
)
1314 return join_u32(migr_rec
->dest_1st_member_lba_lo
,
1315 migr_rec
->dest_1st_member_lba_hi
);
1318 static unsigned long long get_num_migr_units(struct migr_record
*migr_rec
)
1320 if (migr_rec
== NULL
)
1322 return join_u32(migr_rec
->num_migr_units_lo
,
1323 migr_rec
->num_migr_units_hi
);
1326 static void set_total_blocks(struct imsm_disk
*disk
, unsigned long long n
)
1328 split_ull(n
, &disk
->total_blocks_lo
, &disk
->total_blocks_hi
);
1332 * set_num_domains() - Set number of domains for an array.
1333 * @map: Map to be updated.
1335 * num_domains property represents copies count of each data drive, thus make
1336 * it meaningful only for RAID1 and RAID10. IMSM supports two domains for
1339 static void set_num_domains(struct imsm_map
*map
)
1341 int level
= get_imsm_raid_level(map
);
1343 if (level
== 1 || level
== 10)
1344 map
->num_domains
= 2;
1346 map
->num_domains
= 1;
1349 static void set_pba_of_lba0(struct imsm_map
*map
, unsigned long long n
)
1351 split_ull(n
, &map
->pba_of_lba0_lo
, &map
->pba_of_lba0_hi
);
1354 static void set_blocks_per_member(struct imsm_map
*map
, unsigned long long n
)
1356 split_ull(n
, &map
->blocks_per_member_lo
, &map
->blocks_per_member_hi
);
1359 static void set_num_data_stripes(struct imsm_map
*map
, unsigned long long n
)
1361 split_ull(n
, &map
->num_data_stripes_lo
, &map
->num_data_stripes_hi
);
1365 * update_num_data_stripes() - Calculate and update num_data_stripes value.
1366 * @map: map to be updated.
1367 * @dev_size: size of volume.
1369 * num_data_stripes value is addictionally divided by num_domains, therefore for
1370 * levels where num_domains is not 1, nds is a part of real value.
1372 static void update_num_data_stripes(struct imsm_map
*map
,
1373 unsigned long long dev_size
)
1375 unsigned long long nds
= dev_size
/ imsm_num_data_members(map
);
1377 nds
/= map
->num_domains
;
1378 nds
/= map
->blocks_per_strip
;
1379 set_num_data_stripes(map
, nds
);
1382 static void set_vol_curr_migr_unit(struct imsm_dev
*dev
, unsigned long long n
)
1387 split_ull(n
, &dev
->vol
.curr_migr_unit_lo
, &dev
->vol
.curr_migr_unit_hi
);
1390 static void set_imsm_dev_size(struct imsm_dev
*dev
, unsigned long long n
)
1392 split_ull(n
, &dev
->size_low
, &dev
->size_high
);
1395 static void set_migr_chkp_area_pba(struct migr_record
*migr_rec
,
1396 unsigned long long n
)
1398 split_ull(n
, &migr_rec
->ckpt_area_pba_lo
, &migr_rec
->ckpt_area_pba_hi
);
1401 static void set_current_migr_unit(struct migr_record
*migr_rec
,
1402 unsigned long long n
)
1404 split_ull(n
, &migr_rec
->curr_migr_unit_lo
,
1405 &migr_rec
->curr_migr_unit_hi
);
1408 static void set_migr_dest_1st_member_lba(struct migr_record
*migr_rec
,
1409 unsigned long long n
)
1411 split_ull(n
, &migr_rec
->dest_1st_member_lba_lo
,
1412 &migr_rec
->dest_1st_member_lba_hi
);
1415 static void set_num_migr_units(struct migr_record
*migr_rec
,
1416 unsigned long long n
)
1418 split_ull(n
, &migr_rec
->num_migr_units_lo
,
1419 &migr_rec
->num_migr_units_hi
);
1422 static unsigned long long per_dev_array_size(struct imsm_map
*map
)
1424 unsigned long long array_size
= 0;
1429 array_size
= num_data_stripes(map
) * map
->blocks_per_strip
;
1430 if (get_imsm_raid_level(map
) == 1 || get_imsm_raid_level(map
) == 10)
1436 static struct extent
*get_extents(struct intel_super
*super
, struct dl
*dl
,
1437 int get_minimal_reservation
)
1439 /* find a list of used extents on the given physical device */
1440 struct extent
*rv
, *e
;
1442 int memberships
= count_memberships(dl
, super
);
1445 /* trim the reserved area for spares, so they can join any array
1446 * regardless of whether the OROM has assigned sectors from the
1447 * IMSM_RESERVED_SECTORS region
1449 if (dl
->index
== -1 || get_minimal_reservation
)
1450 reservation
= imsm_min_reserved_sectors(super
);
1452 reservation
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
1454 rv
= xcalloc(sizeof(struct extent
), (memberships
+ 1));
1457 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
1458 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
1459 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
1461 if (get_imsm_disk_slot(map
, dl
->index
) >= 0) {
1462 e
->start
= pba_of_lba0(map
);
1463 e
->size
= per_dev_array_size(map
);
1467 qsort(rv
, memberships
, sizeof(*rv
), cmp_extent
);
1469 /* determine the start of the metadata
1470 * when no raid devices are defined use the default
1471 * ...otherwise allow the metadata to truncate the value
1472 * as is the case with older versions of imsm
1475 struct extent
*last
= &rv
[memberships
- 1];
1476 unsigned long long remainder
;
1478 remainder
= total_blocks(&dl
->disk
) - (last
->start
+ last
->size
);
1479 /* round down to 1k block to satisfy precision of the kernel
1483 /* make sure remainder is still sane */
1484 if (remainder
< (unsigned)ROUND_UP(super
->len
, 512) >> 9)
1485 remainder
= ROUND_UP(super
->len
, 512) >> 9;
1486 if (reservation
> remainder
)
1487 reservation
= remainder
;
1489 e
->start
= total_blocks(&dl
->disk
) - reservation
;
1494 /* try to determine how much space is reserved for metadata from
1495 * the last get_extents() entry, otherwise fallback to the
1498 static __u32
imsm_reserved_sectors(struct intel_super
*super
, struct dl
*dl
)
1504 /* for spares just return a minimal reservation which will grow
1505 * once the spare is picked up by an array
1507 if (dl
->index
== -1)
1508 return MPB_SECTOR_CNT
;
1510 e
= get_extents(super
, dl
, 0);
1512 return MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
1514 /* scroll to last entry */
1515 for (i
= 0; e
[i
].size
; i
++)
1518 rv
= total_blocks(&dl
->disk
) - e
[i
].start
;
1525 static int is_spare(struct imsm_disk
*disk
)
1527 return (disk
->status
& SPARE_DISK
) == SPARE_DISK
;
1530 static int is_configured(struct imsm_disk
*disk
)
1532 return (disk
->status
& CONFIGURED_DISK
) == CONFIGURED_DISK
;
1535 static int is_failed(struct imsm_disk
*disk
)
1537 return (disk
->status
& FAILED_DISK
) == FAILED_DISK
;
1540 static int is_journal(struct imsm_disk
*disk
)
1542 return (disk
->status
& JOURNAL_DISK
) == JOURNAL_DISK
;
1545 /* round array size down to closest MB and ensure it splits evenly
1548 static unsigned long long round_size_to_mb(unsigned long long size
, unsigned int
1552 size
= (size
>> SECT_PER_MB_SHIFT
) << SECT_PER_MB_SHIFT
;
1558 static int able_to_resync(int raid_level
, int missing_disks
)
1560 int max_missing_disks
= 0;
1562 switch (raid_level
) {
1564 max_missing_disks
= 1;
1567 max_missing_disks
= 0;
1569 return missing_disks
<= max_missing_disks
;
1572 /* try to determine how much space is reserved for metadata from
1573 * the last get_extents() entry on the smallest active disk,
1574 * otherwise fallback to the default
1576 static __u32
imsm_min_reserved_sectors(struct intel_super
*super
)
1580 unsigned long long min_active
;
1582 __u32 rv
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
1583 struct dl
*dl
, *dl_min
= NULL
;
1589 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
1592 unsigned long long blocks
= total_blocks(&dl
->disk
);
1593 if (blocks
< min_active
|| min_active
== 0) {
1595 min_active
= blocks
;
1601 /* find last lba used by subarrays on the smallest active disk */
1602 e
= get_extents(super
, dl_min
, 0);
1605 for (i
= 0; e
[i
].size
; i
++)
1608 remainder
= min_active
- e
[i
].start
;
1611 /* to give priority to recovery we should not require full
1612 IMSM_RESERVED_SECTORS from the spare */
1613 rv
= MPB_SECTOR_CNT
+ NUM_BLOCKS_DIRTY_STRIPE_REGION
;
1615 /* if real reservation is smaller use that value */
1616 return (remainder
< rv
) ? remainder
: rv
;
1620 * Return minimum size of a spare and sector size
1621 * that can be used in this array
1623 int get_spare_criteria_imsm(struct supertype
*st
, struct spare_criteria
*c
)
1625 struct intel_super
*super
= st
->sb
;
1629 unsigned long long size
= 0;
1636 /* find first active disk in array */
1638 while (dl
&& (is_failed(&dl
->disk
) || dl
->index
== -1))
1642 /* find last lba used by subarrays */
1643 e
= get_extents(super
, dl
, 0);
1646 for (i
= 0; e
[i
].size
; i
++)
1649 size
= e
[i
-1].start
+ e
[i
-1].size
;
1652 /* add the amount of space needed for metadata */
1653 size
+= imsm_min_reserved_sectors(super
);
1655 c
->min_size
= size
* 512;
1656 c
->sector_size
= super
->sector_size
;
1661 static bool is_gen_migration(struct imsm_dev
*dev
);
1663 #define IMSM_4K_DIV 8
1665 static __u64
blocks_per_migr_unit(struct intel_super
*super
,
1666 struct imsm_dev
*dev
);
1668 static void print_imsm_dev(struct intel_super
*super
,
1669 struct imsm_dev
*dev
,
1675 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
1676 struct imsm_map
*map2
= get_imsm_map(dev
, MAP_1
);
1680 printf("[%.16s]:\n", dev
->volume
);
1681 printf(" Subarray : %d\n", super
->current_vol
);
1682 printf(" UUID : %s\n", uuid
);
1683 printf(" RAID Level : %d", get_imsm_raid_level(map
));
1685 printf(" <-- %d", get_imsm_raid_level(map2
));
1687 printf(" Members : %d", map
->num_members
);
1689 printf(" <-- %d", map2
->num_members
);
1691 printf(" Slots : [");
1692 for (i
= 0; i
< map
->num_members
; i
++) {
1693 ord
= get_imsm_ord_tbl_ent(dev
, i
, MAP_0
);
1694 printf("%s", ord
& IMSM_ORD_REBUILD
? "_" : "U");
1699 for (i
= 0; i
< map2
->num_members
; i
++) {
1700 ord
= get_imsm_ord_tbl_ent(dev
, i
, MAP_1
);
1701 printf("%s", ord
& IMSM_ORD_REBUILD
? "_" : "U");
1706 printf(" Failed disk : ");
1707 if (map
->failed_disk_num
== 0xff)
1710 printf("%i", map
->failed_disk_num
);
1712 slot
= get_imsm_disk_slot(map
, disk_idx
);
1714 ord
= get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
);
1715 printf(" This Slot : %d%s\n", slot
,
1716 ord
& IMSM_ORD_REBUILD
? " (out-of-sync)" : "");
1718 printf(" This Slot : ?\n");
1719 printf(" Sector Size : %u\n", super
->sector_size
);
1720 sz
= imsm_dev_size(dev
);
1721 printf(" Array Size : %llu%s\n",
1722 (unsigned long long)sz
* 512 / super
->sector_size
,
1723 human_size(sz
* 512));
1724 sz
= blocks_per_member(map
);
1725 printf(" Per Dev Size : %llu%s\n",
1726 (unsigned long long)sz
* 512 / super
->sector_size
,
1727 human_size(sz
* 512));
1728 printf(" Sector Offset : %llu\n",
1729 pba_of_lba0(map
) * 512 / super
->sector_size
);
1730 printf(" Num Stripes : %llu\n",
1731 num_data_stripes(map
));
1732 printf(" Chunk Size : %u KiB",
1733 __le16_to_cpu(map
->blocks_per_strip
) / 2);
1735 printf(" <-- %u KiB",
1736 __le16_to_cpu(map2
->blocks_per_strip
) / 2);
1738 printf(" Reserved : %d\n", __le32_to_cpu(dev
->reserved_blocks
));
1739 printf(" Migrate State : ");
1740 if (dev
->vol
.migr_state
) {
1741 if (migr_type(dev
) == MIGR_INIT
)
1742 printf("initialize\n");
1743 else if (migr_type(dev
) == MIGR_REBUILD
)
1744 printf("rebuild\n");
1745 else if (migr_type(dev
) == MIGR_VERIFY
)
1747 else if (migr_type(dev
) == MIGR_GEN_MIGR
)
1748 printf("general migration\n");
1749 else if (migr_type(dev
) == MIGR_STATE_CHANGE
)
1750 printf("state change\n");
1751 else if (migr_type(dev
) == MIGR_REPAIR
)
1754 printf("<unknown:%d>\n", migr_type(dev
));
1757 printf(" Map State : %s", map_state_str
[map
->map_state
]);
1758 if (dev
->vol
.migr_state
) {
1759 struct imsm_map
*map
= get_imsm_map(dev
, MAP_1
);
1761 printf(" <-- %s", map_state_str
[map
->map_state
]);
1762 printf("\n Checkpoint : %llu ", vol_curr_migr_unit(dev
));
1763 if (is_gen_migration(dev
) && (slot
> 1 || slot
< 0))
1766 printf("(%llu)", (unsigned long long)
1767 blocks_per_migr_unit(super
, dev
));
1770 printf(" Dirty State : %s\n", (dev
->vol
.dirty
& RAIDVOL_DIRTY
) ?
1772 printf(" RWH Policy : ");
1773 if (dev
->rwh_policy
== RWH_OFF
|| dev
->rwh_policy
== RWH_MULTIPLE_OFF
)
1775 else if (dev
->rwh_policy
== RWH_DISTRIBUTED
)
1776 printf("PPL distributed\n");
1777 else if (dev
->rwh_policy
== RWH_JOURNALING_DRIVE
)
1778 printf("PPL journaling drive\n");
1779 else if (dev
->rwh_policy
== RWH_MULTIPLE_DISTRIBUTED
)
1780 printf("Multiple distributed PPLs\n");
1781 else if (dev
->rwh_policy
== RWH_MULTIPLE_PPLS_JOURNALING_DRIVE
)
1782 printf("Multiple PPLs on journaling drive\n");
1783 else if (dev
->rwh_policy
== RWH_BITMAP
)
1784 printf("Write-intent bitmap\n");
1786 printf("<unknown:%d>\n", dev
->rwh_policy
);
1788 printf(" Volume ID : %u\n", dev
->my_vol_raid_dev_num
);
1791 static void print_imsm_disk(struct imsm_disk
*disk
,
1794 unsigned int sector_size
) {
1795 char str
[MAX_RAID_SERIAL_LEN
+ 1];
1798 if (index
< -1 || !disk
)
1802 snprintf(str
, MAX_RAID_SERIAL_LEN
+ 1, "%s", disk
->serial
);
1804 printf(" Disk%02d Serial : %s\n", index
, str
);
1806 printf(" Disk Serial : %s\n", str
);
1807 printf(" State :%s%s%s%s\n", is_spare(disk
) ? " spare" : "",
1808 is_configured(disk
) ? " active" : "",
1809 is_failed(disk
) ? " failed" : "",
1810 is_journal(disk
) ? " journal" : "");
1811 printf(" Id : %08x\n", __le32_to_cpu(disk
->scsi_id
));
1812 sz
= total_blocks(disk
) - reserved
;
1813 printf(" Usable Size : %llu%s\n",
1814 (unsigned long long)sz
* 512 / sector_size
,
1815 human_size(sz
* 512));
1818 void convert_to_4k_imsm_migr_rec(struct intel_super
*super
)
1820 struct migr_record
*migr_rec
= super
->migr_rec
;
1822 migr_rec
->blocks_per_unit
/= IMSM_4K_DIV
;
1823 migr_rec
->dest_depth_per_unit
/= IMSM_4K_DIV
;
1824 split_ull((join_u32(migr_rec
->post_migr_vol_cap
,
1825 migr_rec
->post_migr_vol_cap_hi
) / IMSM_4K_DIV
),
1826 &migr_rec
->post_migr_vol_cap
, &migr_rec
->post_migr_vol_cap_hi
);
1827 set_migr_chkp_area_pba(migr_rec
,
1828 migr_chkp_area_pba(migr_rec
) / IMSM_4K_DIV
);
1829 set_migr_dest_1st_member_lba(migr_rec
,
1830 migr_dest_1st_member_lba(migr_rec
) / IMSM_4K_DIV
);
1833 void convert_to_4k_imsm_disk(struct imsm_disk
*disk
)
1835 set_total_blocks(disk
, (total_blocks(disk
)/IMSM_4K_DIV
));
1838 void convert_to_4k(struct intel_super
*super
)
1840 struct imsm_super
*mpb
= super
->anchor
;
1841 struct imsm_disk
*disk
;
1843 __u32 bbm_log_size
= __le32_to_cpu(mpb
->bbm_log_size
);
1845 for (i
= 0; i
< mpb
->num_disks
; i
++) {
1846 disk
= __get_imsm_disk(mpb
, i
);
1848 convert_to_4k_imsm_disk(disk
);
1850 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
1851 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
1852 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
1854 set_imsm_dev_size(dev
, imsm_dev_size(dev
)/IMSM_4K_DIV
);
1855 set_vol_curr_migr_unit(dev
,
1856 vol_curr_migr_unit(dev
) / IMSM_4K_DIV
);
1859 set_blocks_per_member(map
, blocks_per_member(map
)/IMSM_4K_DIV
);
1860 map
->blocks_per_strip
/= IMSM_4K_DIV
;
1861 set_pba_of_lba0(map
, pba_of_lba0(map
)/IMSM_4K_DIV
);
1863 if (dev
->vol
.migr_state
) {
1865 map
= get_imsm_map(dev
, MAP_1
);
1866 set_blocks_per_member(map
,
1867 blocks_per_member(map
)/IMSM_4K_DIV
);
1868 map
->blocks_per_strip
/= IMSM_4K_DIV
;
1869 set_pba_of_lba0(map
, pba_of_lba0(map
)/IMSM_4K_DIV
);
1873 struct bbm_log
*log
= (void *)mpb
+
1874 __le32_to_cpu(mpb
->mpb_size
) - bbm_log_size
;
1877 for (i
= 0; i
< log
->entry_count
; i
++) {
1878 struct bbm_log_entry
*entry
=
1879 &log
->marked_block_entries
[i
];
1881 __u8 count
= entry
->marked_count
+ 1;
1882 unsigned long long sector
=
1883 __le48_to_cpu(&entry
->defective_block_start
);
1885 entry
->defective_block_start
=
1886 __cpu_to_le48(sector
/IMSM_4K_DIV
);
1887 entry
->marked_count
= max(count
/IMSM_4K_DIV
, 1) - 1;
1891 mpb
->check_sum
= __gen_imsm_checksum(mpb
);
1894 void examine_migr_rec_imsm(struct intel_super
*super
)
1896 struct migr_record
*migr_rec
= super
->migr_rec
;
1897 struct imsm_super
*mpb
= super
->anchor
;
1900 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
1901 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
1902 struct imsm_map
*map
;
1905 if (is_gen_migration(dev
) == false)
1908 printf("\nMigration Record Information:");
1910 /* first map under migration */
1911 map
= get_imsm_map(dev
, MAP_0
);
1913 slot
= get_imsm_disk_slot(map
, super
->disks
->index
);
1914 if (map
== NULL
|| slot
> 1 || slot
< 0) {
1915 printf(" Empty\n ");
1916 printf("Examine one of first two disks in array\n");
1919 printf("\n Status : ");
1920 if (__le32_to_cpu(migr_rec
->rec_status
) == UNIT_SRC_NORMAL
)
1923 printf("Contains Data\n");
1924 printf(" Current Unit : %llu\n",
1925 current_migr_unit(migr_rec
));
1926 printf(" Family : %u\n",
1927 __le32_to_cpu(migr_rec
->family_num
));
1928 printf(" Ascending : %u\n",
1929 __le32_to_cpu(migr_rec
->ascending_migr
));
1930 printf(" Blocks Per Unit : %u\n",
1931 __le32_to_cpu(migr_rec
->blocks_per_unit
));
1932 printf(" Dest. Depth Per Unit : %u\n",
1933 __le32_to_cpu(migr_rec
->dest_depth_per_unit
));
1934 printf(" Checkpoint Area pba : %llu\n",
1935 migr_chkp_area_pba(migr_rec
));
1936 printf(" First member lba : %llu\n",
1937 migr_dest_1st_member_lba(migr_rec
));
1938 printf(" Total Number of Units : %llu\n",
1939 get_num_migr_units(migr_rec
));
1940 printf(" Size of volume : %llu\n",
1941 join_u32(migr_rec
->post_migr_vol_cap
,
1942 migr_rec
->post_migr_vol_cap_hi
));
1943 printf(" Record was read from : %u\n",
1944 __le32_to_cpu(migr_rec
->ckpt_read_disk_num
));
1950 void convert_from_4k_imsm_migr_rec(struct intel_super
*super
)
1952 struct migr_record
*migr_rec
= super
->migr_rec
;
1954 migr_rec
->blocks_per_unit
*= IMSM_4K_DIV
;
1955 migr_rec
->dest_depth_per_unit
*= IMSM_4K_DIV
;
1956 split_ull((join_u32(migr_rec
->post_migr_vol_cap
,
1957 migr_rec
->post_migr_vol_cap_hi
) * IMSM_4K_DIV
),
1958 &migr_rec
->post_migr_vol_cap
,
1959 &migr_rec
->post_migr_vol_cap_hi
);
1960 set_migr_chkp_area_pba(migr_rec
,
1961 migr_chkp_area_pba(migr_rec
) * IMSM_4K_DIV
);
1962 set_migr_dest_1st_member_lba(migr_rec
,
1963 migr_dest_1st_member_lba(migr_rec
) * IMSM_4K_DIV
);
1966 void convert_from_4k(struct intel_super
*super
)
1968 struct imsm_super
*mpb
= super
->anchor
;
1969 struct imsm_disk
*disk
;
1971 __u32 bbm_log_size
= __le32_to_cpu(mpb
->bbm_log_size
);
1973 for (i
= 0; i
< mpb
->num_disks
; i
++) {
1974 disk
= __get_imsm_disk(mpb
, i
);
1976 set_total_blocks(disk
, (total_blocks(disk
)*IMSM_4K_DIV
));
1979 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
1980 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
1981 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
1983 set_imsm_dev_size(dev
, imsm_dev_size(dev
)*IMSM_4K_DIV
);
1984 set_vol_curr_migr_unit(dev
,
1985 vol_curr_migr_unit(dev
) * IMSM_4K_DIV
);
1988 set_blocks_per_member(map
, blocks_per_member(map
)*IMSM_4K_DIV
);
1989 map
->blocks_per_strip
*= IMSM_4K_DIV
;
1990 set_pba_of_lba0(map
, pba_of_lba0(map
)*IMSM_4K_DIV
);
1992 if (dev
->vol
.migr_state
) {
1994 map
= get_imsm_map(dev
, MAP_1
);
1995 set_blocks_per_member(map
,
1996 blocks_per_member(map
)*IMSM_4K_DIV
);
1997 map
->blocks_per_strip
*= IMSM_4K_DIV
;
1998 set_pba_of_lba0(map
, pba_of_lba0(map
)*IMSM_4K_DIV
);
2002 struct bbm_log
*log
= (void *)mpb
+
2003 __le32_to_cpu(mpb
->mpb_size
) - bbm_log_size
;
2006 for (i
= 0; i
< log
->entry_count
; i
++) {
2007 struct bbm_log_entry
*entry
=
2008 &log
->marked_block_entries
[i
];
2010 __u8 count
= entry
->marked_count
+ 1;
2011 unsigned long long sector
=
2012 __le48_to_cpu(&entry
->defective_block_start
);
2014 entry
->defective_block_start
=
2015 __cpu_to_le48(sector
*IMSM_4K_DIV
);
2016 entry
->marked_count
= count
*IMSM_4K_DIV
- 1;
2020 mpb
->check_sum
= __gen_imsm_checksum(mpb
);
2023 /*******************************************************************************
2024 * function: imsm_check_attributes
2025 * Description: Function checks if features represented by attributes flags
2026 * are supported by mdadm.
2028 * attributes - Attributes read from metadata
2030 * 0 - passed attributes contains unsupported features flags
2031 * 1 - all features are supported
2032 ******************************************************************************/
2033 static int imsm_check_attributes(__u32 attributes
)
2036 __u32 not_supported
= MPB_ATTRIB_SUPPORTED
^0xffffffff;
2038 not_supported
&= ~MPB_ATTRIB_IGNORED
;
2040 not_supported
&= attributes
;
2041 if (not_supported
) {
2042 pr_err("(IMSM): Unsupported attributes : %x\n",
2043 (unsigned)__le32_to_cpu(not_supported
));
2044 if (not_supported
& MPB_ATTRIB_CHECKSUM_VERIFY
) {
2045 dprintf("\t\tMPB_ATTRIB_CHECKSUM_VERIFY \n");
2046 not_supported
^= MPB_ATTRIB_CHECKSUM_VERIFY
;
2048 if (not_supported
& MPB_ATTRIB_2TB
) {
2049 dprintf("\t\tMPB_ATTRIB_2TB\n");
2050 not_supported
^= MPB_ATTRIB_2TB
;
2052 if (not_supported
& MPB_ATTRIB_RAID0
) {
2053 dprintf("\t\tMPB_ATTRIB_RAID0\n");
2054 not_supported
^= MPB_ATTRIB_RAID0
;
2056 if (not_supported
& MPB_ATTRIB_RAID1
) {
2057 dprintf("\t\tMPB_ATTRIB_RAID1\n");
2058 not_supported
^= MPB_ATTRIB_RAID1
;
2060 if (not_supported
& MPB_ATTRIB_RAID10
) {
2061 dprintf("\t\tMPB_ATTRIB_RAID10\n");
2062 not_supported
^= MPB_ATTRIB_RAID10
;
2064 if (not_supported
& MPB_ATTRIB_RAID1E
) {
2065 dprintf("\t\tMPB_ATTRIB_RAID1E\n");
2066 not_supported
^= MPB_ATTRIB_RAID1E
;
2068 if (not_supported
& MPB_ATTRIB_RAID5
) {
2069 dprintf("\t\tMPB_ATTRIB_RAID5\n");
2070 not_supported
^= MPB_ATTRIB_RAID5
;
2072 if (not_supported
& MPB_ATTRIB_RAIDCNG
) {
2073 dprintf("\t\tMPB_ATTRIB_RAIDCNG\n");
2074 not_supported
^= MPB_ATTRIB_RAIDCNG
;
2076 if (not_supported
& MPB_ATTRIB_BBM
) {
2077 dprintf("\t\tMPB_ATTRIB_BBM\n");
2078 not_supported
^= MPB_ATTRIB_BBM
;
2080 if (not_supported
& MPB_ATTRIB_CHECKSUM_VERIFY
) {
2081 dprintf("\t\tMPB_ATTRIB_CHECKSUM_VERIFY (== MPB_ATTRIB_LEGACY)\n");
2082 not_supported
^= MPB_ATTRIB_CHECKSUM_VERIFY
;
2084 if (not_supported
& MPB_ATTRIB_EXP_STRIPE_SIZE
) {
2085 dprintf("\t\tMPB_ATTRIB_EXP_STRIP_SIZE\n");
2086 not_supported
^= MPB_ATTRIB_EXP_STRIPE_SIZE
;
2088 if (not_supported
& MPB_ATTRIB_2TB_DISK
) {
2089 dprintf("\t\tMPB_ATTRIB_2TB_DISK\n");
2090 not_supported
^= MPB_ATTRIB_2TB_DISK
;
2092 if (not_supported
& MPB_ATTRIB_NEVER_USE2
) {
2093 dprintf("\t\tMPB_ATTRIB_NEVER_USE2\n");
2094 not_supported
^= MPB_ATTRIB_NEVER_USE2
;
2096 if (not_supported
& MPB_ATTRIB_NEVER_USE
) {
2097 dprintf("\t\tMPB_ATTRIB_NEVER_USE\n");
2098 not_supported
^= MPB_ATTRIB_NEVER_USE
;
2102 dprintf("(IMSM): Unknown attributes : %x\n", not_supported
);
2110 static void getinfo_super_imsm(struct supertype
*st
, struct mdinfo
*info
, char *map
);
2112 static void examine_super_imsm(struct supertype
*st
, char *homehost
)
2114 struct intel_super
*super
= st
->sb
;
2115 struct imsm_super
*mpb
= super
->anchor
;
2116 char str
[MAX_SIGNATURE_LENGTH
];
2121 __u32 reserved
= imsm_reserved_sectors(super
, super
->disks
);
2123 time_t creation_time
;
2125 strncpy(str
, (char *)mpb
->sig
, MPB_SIG_LEN
);
2126 str
[MPB_SIG_LEN
-1] = '\0';
2127 printf(" Magic : %s\n", str
);
2128 printf(" Version : %s\n", get_imsm_version(mpb
));
2129 printf(" Orig Family : %08x\n", __le32_to_cpu(mpb
->orig_family_num
));
2130 printf(" Family : %08x\n", __le32_to_cpu(mpb
->family_num
));
2131 printf(" Generation : %08x\n", __le32_to_cpu(mpb
->generation_num
));
2132 creation_time
= __le64_to_cpu(mpb
->creation_time
);
2133 printf(" Creation Time : %.24s\n",
2134 creation_time
? ctime(&creation_time
) : "Unknown");
2135 printf(" Attributes : ");
2136 if (imsm_check_attributes(mpb
->attributes
))
2137 printf("All supported\n");
2139 printf("not supported\n");
2140 getinfo_super_imsm(st
, &info
, NULL
);
2141 fname_from_uuid(st
, &info
, nbuf
, ':');
2142 printf(" UUID : %s\n", nbuf
+ 5);
2143 sum
= __le32_to_cpu(mpb
->check_sum
);
2144 printf(" Checksum : %08x %s\n", sum
,
2145 __gen_imsm_checksum(mpb
) == sum
? "correct" : "incorrect");
2146 printf(" MPB Sectors : %d\n", mpb_sectors(mpb
, super
->sector_size
));
2147 printf(" Disks : %d\n", mpb
->num_disks
);
2148 printf(" RAID Devices : %d\n", mpb
->num_raid_devs
);
2149 print_imsm_disk(__get_imsm_disk(mpb
, super
->disks
->index
),
2150 super
->disks
->index
, reserved
, super
->sector_size
);
2151 if (get_imsm_bbm_log_size(super
->bbm_log
)) {
2152 struct bbm_log
*log
= super
->bbm_log
;
2155 printf("Bad Block Management Log:\n");
2156 printf(" Log Size : %d\n", __le32_to_cpu(mpb
->bbm_log_size
));
2157 printf(" Signature : %x\n", __le32_to_cpu(log
->signature
));
2158 printf(" Entry Count : %d\n", __le32_to_cpu(log
->entry_count
));
2160 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
2162 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
2164 super
->current_vol
= i
;
2165 getinfo_super_imsm(st
, &info
, NULL
);
2166 fname_from_uuid(st
, &info
, nbuf
, ':');
2167 print_imsm_dev(super
, dev
, nbuf
+ 5, super
->disks
->index
);
2169 for (i
= 0; i
< mpb
->num_disks
; i
++) {
2170 if (i
== super
->disks
->index
)
2172 print_imsm_disk(__get_imsm_disk(mpb
, i
), i
, reserved
,
2173 super
->sector_size
);
2176 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
2177 if (dl
->index
== -1)
2178 print_imsm_disk(&dl
->disk
, -1, reserved
,
2179 super
->sector_size
);
2181 examine_migr_rec_imsm(super
);
2184 static void brief_examine_super_imsm(struct supertype
*st
, int verbose
)
2186 /* We just write a generic IMSM ARRAY entry */
2190 getinfo_super_imsm(st
, &info
, NULL
);
2191 fname_from_uuid(st
, &info
, nbuf
, ':');
2192 printf("ARRAY metadata=imsm UUID=%s\n", nbuf
+ 5);
2195 static void brief_examine_subarrays_imsm(struct supertype
*st
, int verbose
)
2197 /* We just write a generic IMSM ARRAY entry */
2201 struct intel_super
*super
= st
->sb
;
2204 if (!super
->anchor
->num_raid_devs
)
2207 getinfo_super_imsm(st
, &info
, NULL
);
2208 fname_from_uuid(st
, &info
, nbuf
, ':');
2209 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
2210 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
2212 super
->current_vol
= i
;
2213 getinfo_super_imsm(st
, &info
, NULL
);
2214 fname_from_uuid(st
, &info
, nbuf1
, ':');
2215 printf("ARRAY /dev/md/%.16s container=%s member=%d UUID=%s\n",
2216 dev
->volume
, nbuf
+ 5, i
, nbuf1
+ 5);
2220 static void export_examine_super_imsm(struct supertype
*st
)
2222 struct intel_super
*super
= st
->sb
;
2223 struct imsm_super
*mpb
= super
->anchor
;
2227 getinfo_super_imsm(st
, &info
, NULL
);
2228 fname_from_uuid(st
, &info
, nbuf
, ':');
2229 printf("MD_METADATA=imsm\n");
2230 printf("MD_LEVEL=container\n");
2231 printf("MD_UUID=%s\n", nbuf
+5);
2232 printf("MD_DEVICES=%u\n", mpb
->num_disks
);
2233 printf("MD_CREATION_TIME=%llu\n", __le64_to_cpu(mpb
->creation_time
));
2236 static void detail_super_imsm(struct supertype
*st
, char *homehost
,
2241 struct intel_super
*super
= st
->sb
;
2242 int temp_vol
= super
->current_vol
;
2245 super
->current_vol
= strtoul(subarray
, NULL
, 10);
2247 getinfo_super_imsm(st
, &info
, NULL
);
2248 fname_from_uuid(st
, &info
, nbuf
, ':');
2249 printf("\n UUID : %s\n", nbuf
+ 5);
2251 super
->current_vol
= temp_vol
;
2254 static void brief_detail_super_imsm(struct supertype
*st
, char *subarray
)
2258 struct intel_super
*super
= st
->sb
;
2259 int temp_vol
= super
->current_vol
;
2262 super
->current_vol
= strtoul(subarray
, NULL
, 10);
2264 getinfo_super_imsm(st
, &info
, NULL
);
2265 fname_from_uuid(st
, &info
, nbuf
, ':');
2266 printf(" UUID=%s", nbuf
+ 5);
2268 super
->current_vol
= temp_vol
;
2271 static int imsm_read_serial(int fd
, char *devname
, __u8
*serial
,
2272 size_t serial_buf_len
);
2273 static void fd2devname(int fd
, char *name
);
2275 static int ahci_enumerate_ports(const char *hba_path
, int port_count
, int host_base
, int verbose
)
2277 /* dump an unsorted list of devices attached to AHCI Intel storage
2278 * controller, as well as non-connected ports
2280 int hba_len
= strlen(hba_path
) + 1;
2285 unsigned long port_mask
= (1 << port_count
) - 1;
2287 if (port_count
> (int)sizeof(port_mask
) * 8) {
2289 pr_err("port_count %d out of range\n", port_count
);
2293 /* scroll through /sys/dev/block looking for devices attached to
2296 dir
= opendir("/sys/dev/block");
2300 for (ent
= readdir(dir
); ent
; ent
= readdir(dir
)) {
2306 char device
[PATH_MAX
];
2311 if (sscanf(ent
->d_name
, "%d:%d", &major
, &minor
) != 2)
2313 path
= devt_to_devpath(makedev(major
, minor
), 1, NULL
);
2316 if (!path_attached_to_hba(path
, hba_path
)) {
2322 /* retrieve the scsi device */
2323 if (!devt_to_devpath(makedev(major
, minor
), 1, device
)) {
2325 pr_err("failed to get device\n");
2329 if (devpath_to_char(device
, "type", buf
, sizeof(buf
), 0)) {
2333 type
= strtoul(buf
, NULL
, 10);
2335 /* if it's not a disk print the vendor and model */
2336 if (!(type
== 0 || type
== 7 || type
== 14)) {
2340 if (devpath_to_char(device
, "vendor", buf
,
2341 sizeof(buf
), 0) == 0) {
2342 strncpy(vendor
, buf
, sizeof(vendor
));
2343 vendor
[sizeof(vendor
) - 1] = '\0';
2344 c
= (char *) &vendor
[sizeof(vendor
) - 1];
2345 while (isspace(*c
) || *c
== '\0')
2350 if (devpath_to_char(device
, "model", buf
,
2351 sizeof(buf
), 0) == 0) {
2352 strncpy(model
, buf
, sizeof(model
));
2353 model
[sizeof(model
) - 1] = '\0';
2354 c
= (char *) &model
[sizeof(model
) - 1];
2355 while (isspace(*c
) || *c
== '\0')
2359 if (vendor
[0] && model
[0])
2360 sprintf(buf
, "%.64s %.64s", vendor
, model
);
2362 switch (type
) { /* numbers from hald/linux/device.c */
2363 case 1: sprintf(buf
, "tape"); break;
2364 case 2: sprintf(buf
, "printer"); break;
2365 case 3: sprintf(buf
, "processor"); break;
2367 case 5: sprintf(buf
, "cdrom"); break;
2368 case 6: sprintf(buf
, "scanner"); break;
2369 case 8: sprintf(buf
, "media_changer"); break;
2370 case 9: sprintf(buf
, "comm"); break;
2371 case 12: sprintf(buf
, "raid"); break;
2372 default: sprintf(buf
, "unknown");
2377 /* chop device path to 'host%d' and calculate the port number */
2378 c
= strchr(&path
[hba_len
], '/');
2381 pr_err("%s - invalid path name\n", path
+ hba_len
);
2386 if ((sscanf(&path
[hba_len
], "ata%d", &port
) == 1) ||
2387 ((sscanf(&path
[hba_len
], "host%d", &port
) == 1)))
2391 *c
= '/'; /* repair the full string */
2392 pr_err("failed to determine port number for %s\n",
2399 /* mark this port as used */
2400 port_mask
&= ~(1 << port
);
2402 /* print out the device information */
2404 printf(" Port%d : - non-disk device (%s) -\n", port
, buf
);
2408 fd
= dev_open(ent
->d_name
, O_RDONLY
);
2409 if (!is_fd_valid(fd
))
2410 printf(" Port%d : - disk info unavailable -\n", port
);
2412 fd2devname(fd
, buf
);
2413 printf(" Port%d : %s", port
, buf
);
2414 if (imsm_read_serial(fd
, NULL
, (__u8
*)buf
,
2416 printf(" (%s)\n", buf
);
2431 for (i
= 0; i
< port_count
; i
++)
2432 if (port_mask
& (1 << i
))
2433 printf(" Port%d : - no device attached -\n", i
);
2439 static int print_nvme_info(struct sys_dev
*hba
)
2444 dir
= opendir("/sys/block/");
2448 for (ent
= readdir(dir
); ent
; ent
= readdir(dir
)) {
2449 char ns_path
[PATH_MAX
];
2450 char cntrl_path
[PATH_MAX
];
2454 if (!strstr(ent
->d_name
, "nvme"))
2457 fd
= open_dev(ent
->d_name
);
2458 if (!is_fd_valid(fd
))
2461 if (!diskfd_to_devpath(fd
, 0, ns_path
) ||
2462 !diskfd_to_devpath(fd
, 1, cntrl_path
))
2465 if (!path_attached_to_hba(cntrl_path
, hba
->path
))
2468 if (!imsm_is_nvme_namespace_supported(fd
, 0))
2471 fd2devname(fd
, buf
);
2472 if (hba
->type
== SYS_DEV_VMD
)
2473 printf(" NVMe under VMD : %s", buf
);
2474 else if (hba
->type
== SYS_DEV_NVME
)
2475 printf(" NVMe Device : %s", buf
);
2477 if (!imsm_read_serial(fd
, NULL
, (__u8
*)buf
,
2479 printf(" (%s)\n", buf
);
2491 static void print_found_intel_controllers(struct sys_dev
*elem
)
2493 for (; elem
; elem
= elem
->next
) {
2494 pr_err("found Intel(R) ");
2495 if (elem
->type
== SYS_DEV_SATA
)
2496 fprintf(stderr
, "SATA ");
2497 else if (elem
->type
== SYS_DEV_SAS
)
2498 fprintf(stderr
, "SAS ");
2499 else if (elem
->type
== SYS_DEV_NVME
)
2500 fprintf(stderr
, "NVMe ");
2502 if (elem
->type
== SYS_DEV_VMD
)
2503 fprintf(stderr
, "VMD domain");
2505 fprintf(stderr
, "RAID controller");
2508 fprintf(stderr
, " at %s", elem
->pci_id
);
2509 fprintf(stderr
, ".\n");
2514 static int ahci_get_port_count(const char *hba_path
, int *port_count
)
2521 if ((dir
= opendir(hba_path
)) == NULL
)
2524 for (ent
= readdir(dir
); ent
; ent
= readdir(dir
)) {
2527 if ((sscanf(ent
->d_name
, "ata%d", &host
) != 1) &&
2528 ((sscanf(ent
->d_name
, "host%d", &host
) != 1)))
2530 if (*port_count
== 0)
2532 else if (host
< host_base
)
2535 if (host
+ 1 > *port_count
+ host_base
)
2536 *port_count
= host
+ 1 - host_base
;
2542 static void print_imsm_capability(const struct imsm_orom
*orom
)
2544 printf(" Platform : Intel(R) ");
2545 if (orom
->capabilities
== 0 && orom
->driver_features
== 0)
2546 printf("Matrix Storage Manager\n");
2547 else if (imsm_orom_is_enterprise(orom
) && orom
->major_ver
>= 6)
2548 printf("Virtual RAID on CPU\n");
2550 printf("Rapid Storage Technology%s\n",
2551 imsm_orom_is_enterprise(orom
) ? " enterprise" : "");
2552 if (orom
->major_ver
|| orom
->minor_ver
|| orom
->hotfix_ver
|| orom
->build
)
2553 printf(" Version : %d.%d.%d.%d\n", orom
->major_ver
,
2554 orom
->minor_ver
, orom
->hotfix_ver
, orom
->build
);
2555 printf(" RAID Levels :%s%s%s%s%s\n",
2556 imsm_orom_has_raid0(orom
) ? " raid0" : "",
2557 imsm_orom_has_raid1(orom
) ? " raid1" : "",
2558 imsm_orom_has_raid1e(orom
) ? " raid1e" : "",
2559 imsm_orom_has_raid10(orom
) ? " raid10" : "",
2560 imsm_orom_has_raid5(orom
) ? " raid5" : "");
2561 printf(" Chunk Sizes :%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
2562 imsm_orom_has_chunk(orom
, 2) ? " 2k" : "",
2563 imsm_orom_has_chunk(orom
, 4) ? " 4k" : "",
2564 imsm_orom_has_chunk(orom
, 8) ? " 8k" : "",
2565 imsm_orom_has_chunk(orom
, 16) ? " 16k" : "",
2566 imsm_orom_has_chunk(orom
, 32) ? " 32k" : "",
2567 imsm_orom_has_chunk(orom
, 64) ? " 64k" : "",
2568 imsm_orom_has_chunk(orom
, 128) ? " 128k" : "",
2569 imsm_orom_has_chunk(orom
, 256) ? " 256k" : "",
2570 imsm_orom_has_chunk(orom
, 512) ? " 512k" : "",
2571 imsm_orom_has_chunk(orom
, 1024*1) ? " 1M" : "",
2572 imsm_orom_has_chunk(orom
, 1024*2) ? " 2M" : "",
2573 imsm_orom_has_chunk(orom
, 1024*4) ? " 4M" : "",
2574 imsm_orom_has_chunk(orom
, 1024*8) ? " 8M" : "",
2575 imsm_orom_has_chunk(orom
, 1024*16) ? " 16M" : "",
2576 imsm_orom_has_chunk(orom
, 1024*32) ? " 32M" : "",
2577 imsm_orom_has_chunk(orom
, 1024*64) ? " 64M" : "");
2578 printf(" 2TB volumes :%s supported\n",
2579 (orom
->attr
& IMSM_OROM_ATTR_2TB
)?"":" not");
2580 printf(" 2TB disks :%s supported\n",
2581 (orom
->attr
& IMSM_OROM_ATTR_2TB_DISK
)?"":" not");
2582 printf(" Max Disks : %d\n", orom
->tds
);
2583 printf(" Max Volumes : %d per array, %d per %s\n",
2584 orom
->vpa
, orom
->vphba
,
2585 imsm_orom_is_nvme(orom
) ? "platform" : "controller");
2589 static void print_imsm_capability_export(const struct imsm_orom
*orom
)
2591 printf("MD_FIRMWARE_TYPE=imsm\n");
2592 if (orom
->major_ver
|| orom
->minor_ver
|| orom
->hotfix_ver
|| orom
->build
)
2593 printf("IMSM_VERSION=%d.%d.%d.%d\n", orom
->major_ver
, orom
->minor_ver
,
2594 orom
->hotfix_ver
, orom
->build
);
2595 printf("IMSM_SUPPORTED_RAID_LEVELS=%s%s%s%s%s\n",
2596 imsm_orom_has_raid0(orom
) ? "raid0 " : "",
2597 imsm_orom_has_raid1(orom
) ? "raid1 " : "",
2598 imsm_orom_has_raid1e(orom
) ? "raid1e " : "",
2599 imsm_orom_has_raid5(orom
) ? "raid10 " : "",
2600 imsm_orom_has_raid10(orom
) ? "raid5 " : "");
2601 printf("IMSM_SUPPORTED_CHUNK_SIZES=%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
2602 imsm_orom_has_chunk(orom
, 2) ? "2k " : "",
2603 imsm_orom_has_chunk(orom
, 4) ? "4k " : "",
2604 imsm_orom_has_chunk(orom
, 8) ? "8k " : "",
2605 imsm_orom_has_chunk(orom
, 16) ? "16k " : "",
2606 imsm_orom_has_chunk(orom
, 32) ? "32k " : "",
2607 imsm_orom_has_chunk(orom
, 64) ? "64k " : "",
2608 imsm_orom_has_chunk(orom
, 128) ? "128k " : "",
2609 imsm_orom_has_chunk(orom
, 256) ? "256k " : "",
2610 imsm_orom_has_chunk(orom
, 512) ? "512k " : "",
2611 imsm_orom_has_chunk(orom
, 1024*1) ? "1M " : "",
2612 imsm_orom_has_chunk(orom
, 1024*2) ? "2M " : "",
2613 imsm_orom_has_chunk(orom
, 1024*4) ? "4M " : "",
2614 imsm_orom_has_chunk(orom
, 1024*8) ? "8M " : "",
2615 imsm_orom_has_chunk(orom
, 1024*16) ? "16M " : "",
2616 imsm_orom_has_chunk(orom
, 1024*32) ? "32M " : "",
2617 imsm_orom_has_chunk(orom
, 1024*64) ? "64M " : "");
2618 printf("IMSM_2TB_VOLUMES=%s\n",(orom
->attr
& IMSM_OROM_ATTR_2TB
) ? "yes" : "no");
2619 printf("IMSM_2TB_DISKS=%s\n",(orom
->attr
& IMSM_OROM_ATTR_2TB_DISK
) ? "yes" : "no");
2620 printf("IMSM_MAX_DISKS=%d\n",orom
->tds
);
2621 printf("IMSM_MAX_VOLUMES_PER_ARRAY=%d\n",orom
->vpa
);
2622 printf("IMSM_MAX_VOLUMES_PER_CONTROLLER=%d\n",orom
->vphba
);
2625 static int detail_platform_imsm(int verbose
, int enumerate_only
, char *controller_path
)
2627 /* There are two components to imsm platform support, the ahci SATA
2628 * controller and the option-rom. To find the SATA controller we
2629 * simply look in /sys/bus/pci/drivers/ahci to see if an ahci
2630 * controller with the Intel vendor id is present. This approach
2631 * allows mdadm to leverage the kernel's ahci detection logic, with the
2632 * caveat that if ahci.ko is not loaded mdadm will not be able to
2633 * detect platform raid capabilities. The option-rom resides in a
2634 * platform "Adapter ROM". We scan for its signature to retrieve the
2635 * platform capabilities. If raid support is disabled in the BIOS the
2636 * option-rom capability structure will not be available.
2638 struct sys_dev
*list
, *hba
;
2643 if (enumerate_only
) {
2644 if (check_env("IMSM_NO_PLATFORM"))
2646 list
= find_intel_devices();
2649 for (hba
= list
; hba
; hba
= hba
->next
) {
2650 if (find_imsm_capability(hba
)) {
2660 list
= find_intel_devices();
2663 pr_err("no active Intel(R) RAID controller found.\n");
2665 } else if (verbose
> 0)
2666 print_found_intel_controllers(list
);
2668 for (hba
= list
; hba
; hba
= hba
->next
) {
2669 if (controller_path
&& (compare_paths(hba
->path
, controller_path
) != 0))
2671 if (!find_imsm_capability(hba
)) {
2673 pr_err("imsm capabilities not found for controller: %s (type %s)\n",
2674 hba
->type
== SYS_DEV_VMD
? vmd_domain_to_controller(hba
, buf
) : hba
->path
,
2675 get_sys_dev_type(hba
->type
));
2681 if (controller_path
&& result
== 1) {
2682 pr_err("no active Intel(R) RAID controller found under %s\n",
2687 const struct orom_entry
*entry
;
2689 for (entry
= orom_entries
; entry
; entry
= entry
->next
) {
2690 if (entry
->type
== SYS_DEV_VMD
) {
2691 print_imsm_capability(&entry
->orom
);
2692 printf(" 3rd party NVMe :%s supported\n",
2693 imsm_orom_has_tpv_support(&entry
->orom
)?"":" not");
2694 for (hba
= list
; hba
; hba
= hba
->next
) {
2695 if (hba
->type
== SYS_DEV_VMD
) {
2697 printf(" I/O Controller : %s (%s)\n",
2698 vmd_domain_to_controller(hba
, buf
), get_sys_dev_type(hba
->type
));
2699 if (print_nvme_info(hba
)) {
2701 pr_err("failed to get devices attached to VMD domain.\n");
2710 print_imsm_capability(&entry
->orom
);
2711 if (entry
->type
== SYS_DEV_NVME
) {
2712 for (hba
= list
; hba
; hba
= hba
->next
) {
2713 if (hba
->type
== SYS_DEV_NVME
)
2714 print_nvme_info(hba
);
2720 struct devid_list
*devid
;
2721 for (devid
= entry
->devid_list
; devid
; devid
= devid
->next
) {
2722 hba
= device_by_id(devid
->devid
);
2726 printf(" I/O Controller : %s (%s)\n",
2727 hba
->path
, get_sys_dev_type(hba
->type
));
2728 if (hba
->type
== SYS_DEV_SATA
) {
2729 host_base
= ahci_get_port_count(hba
->path
, &port_count
);
2730 if (ahci_enumerate_ports(hba
->path
, port_count
, host_base
, verbose
)) {
2732 pr_err("failed to enumerate ports on SATA controller at %s.\n", hba
->pci_id
);
2743 static int export_detail_platform_imsm(int verbose
, char *controller_path
)
2745 struct sys_dev
*list
, *hba
;
2748 list
= find_intel_devices();
2751 pr_err("IMSM_DETAIL_PLATFORM_ERROR=NO_INTEL_DEVICES\n");
2756 for (hba
= list
; hba
; hba
= hba
->next
) {
2757 if (controller_path
&& (compare_paths(hba
->path
,controller_path
) != 0))
2759 if (!find_imsm_capability(hba
) && verbose
> 0) {
2761 pr_err("IMSM_DETAIL_PLATFORM_ERROR=NO_IMSM_CAPABLE_DEVICE_UNDER_%s\n",
2762 hba
->type
== SYS_DEV_VMD
? vmd_domain_to_controller(hba
, buf
) : hba
->path
);
2768 const struct orom_entry
*entry
;
2770 for (entry
= orom_entries
; entry
; entry
= entry
->next
) {
2771 if (entry
->type
== SYS_DEV_VMD
) {
2772 for (hba
= list
; hba
; hba
= hba
->next
)
2773 print_imsm_capability_export(&entry
->orom
);
2776 print_imsm_capability_export(&entry
->orom
);
2782 static int match_home_imsm(struct supertype
*st
, char *homehost
)
2784 /* the imsm metadata format does not specify any host
2785 * identification information. We return -1 since we can never
2786 * confirm nor deny whether a given array is "meant" for this
2787 * host. We rely on compare_super and the 'family_num' fields to
2788 * exclude member disks that do not belong, and we rely on
2789 * mdadm.conf to specify the arrays that should be assembled.
2790 * Auto-assembly may still pick up "foreign" arrays.
2796 static void uuid_from_super_imsm(struct supertype
*st
, int uuid
[4])
2798 /* The uuid returned here is used for:
2799 * uuid to put into bitmap file (Create, Grow)
2800 * uuid for backup header when saving critical section (Grow)
2801 * comparing uuids when re-adding a device into an array
2802 * In these cases the uuid required is that of the data-array,
2803 * not the device-set.
2804 * uuid to recognise same set when adding a missing device back
2805 * to an array. This is a uuid for the device-set.
2807 * For each of these we can make do with a truncated
2808 * or hashed uuid rather than the original, as long as
2810 * In each case the uuid required is that of the data-array,
2811 * not the device-set.
2813 /* imsm does not track uuid's so we synthesis one using sha1 on
2814 * - The signature (Which is constant for all imsm array, but no matter)
2815 * - the orig_family_num of the container
2816 * - the index number of the volume
2817 * - the 'serial' number of the volume.
2818 * Hopefully these are all constant.
2820 struct intel_super
*super
= st
->sb
;
2823 struct sha1_ctx ctx
;
2824 struct imsm_dev
*dev
= NULL
;
2827 /* some mdadm versions failed to set ->orig_family_num, in which
2828 * case fall back to ->family_num. orig_family_num will be
2829 * fixed up with the first metadata update.
2831 family_num
= super
->anchor
->orig_family_num
;
2832 if (family_num
== 0)
2833 family_num
= super
->anchor
->family_num
;
2834 sha1_init_ctx(&ctx
);
2835 sha1_process_bytes(super
->anchor
->sig
, MPB_SIG_LEN
, &ctx
);
2836 sha1_process_bytes(&family_num
, sizeof(__u32
), &ctx
);
2837 if (super
->current_vol
>= 0)
2838 dev
= get_imsm_dev(super
, super
->current_vol
);
2840 __u32 vol
= super
->current_vol
;
2841 sha1_process_bytes(&vol
, sizeof(vol
), &ctx
);
2842 sha1_process_bytes(dev
->volume
, MAX_RAID_SERIAL_LEN
, &ctx
);
2844 sha1_finish_ctx(&ctx
, buf
);
2845 memcpy(uuid
, buf
, 4*4);
2850 get_imsm_numerical_version(struct imsm_super
*mpb
, int *m
, int *p
)
2852 __u8
*v
= get_imsm_version(mpb
);
2853 __u8
*end
= mpb
->sig
+ MAX_SIGNATURE_LENGTH
;
2854 char major
[] = { 0, 0, 0 };
2855 char minor
[] = { 0 ,0, 0 };
2856 char patch
[] = { 0, 0, 0 };
2857 char *ver_parse
[] = { major
, minor
, patch
};
2861 while (*v
!= '\0' && v
< end
) {
2862 if (*v
!= '.' && j
< 2)
2863 ver_parse
[i
][j
++] = *v
;
2871 *m
= strtol(minor
, NULL
, 0);
2872 *p
= strtol(patch
, NULL
, 0);
2876 static __u32
migr_strip_blocks_resync(struct imsm_dev
*dev
)
2878 /* migr_strip_size when repairing or initializing parity */
2879 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
2880 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
2882 switch (get_imsm_raid_level(map
)) {
2887 return 128*1024 >> 9;
2891 static __u32
migr_strip_blocks_rebuild(struct imsm_dev
*dev
)
2893 /* migr_strip_size when rebuilding a degraded disk, no idea why
2894 * this is different than migr_strip_size_resync(), but it's good
2897 struct imsm_map
*map
= get_imsm_map(dev
, MAP_1
);
2898 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
2900 switch (get_imsm_raid_level(map
)) {
2903 if (map
->num_members
% map
->num_domains
== 0)
2904 return 128*1024 >> 9;
2908 return max((__u32
) 64*1024 >> 9, chunk
);
2910 return 128*1024 >> 9;
2914 static __u32
num_stripes_per_unit_resync(struct imsm_dev
*dev
)
2916 struct imsm_map
*lo
= get_imsm_map(dev
, MAP_0
);
2917 struct imsm_map
*hi
= get_imsm_map(dev
, MAP_1
);
2918 __u32 lo_chunk
= __le32_to_cpu(lo
->blocks_per_strip
);
2919 __u32 hi_chunk
= __le32_to_cpu(hi
->blocks_per_strip
);
2921 return max((__u32
) 1, hi_chunk
/ lo_chunk
);
2924 static __u32
num_stripes_per_unit_rebuild(struct imsm_dev
*dev
)
2926 struct imsm_map
*lo
= get_imsm_map(dev
, MAP_0
);
2927 int level
= get_imsm_raid_level(lo
);
2929 if (level
== 1 || level
== 10) {
2930 struct imsm_map
*hi
= get_imsm_map(dev
, MAP_1
);
2932 return hi
->num_domains
;
2934 return num_stripes_per_unit_resync(dev
);
2937 static unsigned long long calc_component_size(struct imsm_map
*map
,
2938 struct imsm_dev
*dev
)
2940 unsigned long long component_size
;
2941 unsigned long long dev_size
= imsm_dev_size(dev
);
2942 long long calc_dev_size
= 0;
2943 unsigned int member_disks
= imsm_num_data_members(map
);
2945 if (member_disks
== 0)
2948 component_size
= per_dev_array_size(map
);
2949 calc_dev_size
= component_size
* member_disks
;
2951 /* Component size is rounded to 1MB so difference between size from
2952 * metadata and size calculated from num_data_stripes equals up to
2953 * 2048 blocks per each device. If the difference is higher it means
2954 * that array size was expanded and num_data_stripes was not updated.
2956 if (llabs(calc_dev_size
- (long long)dev_size
) >
2957 (1 << SECT_PER_MB_SHIFT
) * member_disks
) {
2958 component_size
= dev_size
/ member_disks
;
2959 dprintf("Invalid num_data_stripes in metadata; expected=%llu, found=%llu\n",
2960 component_size
/ map
->blocks_per_strip
,
2961 num_data_stripes(map
));
2964 return component_size
;
2967 static __u32
parity_segment_depth(struct imsm_dev
*dev
)
2969 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
2970 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
2972 switch(get_imsm_raid_level(map
)) {
2975 return chunk
* map
->num_domains
;
2977 return chunk
* map
->num_members
;
2983 static __u32
map_migr_block(struct imsm_dev
*dev
, __u32 block
)
2985 struct imsm_map
*map
= get_imsm_map(dev
, MAP_1
);
2986 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
2987 __u32 strip
= block
/ chunk
;
2989 switch (get_imsm_raid_level(map
)) {
2992 __u32 vol_strip
= (strip
* map
->num_domains
) + 1;
2993 __u32 vol_stripe
= vol_strip
/ map
->num_members
;
2995 return vol_stripe
* chunk
+ block
% chunk
;
2997 __u32 stripe
= strip
/ (map
->num_members
- 1);
2999 return stripe
* chunk
+ block
% chunk
;
3006 static __u64
blocks_per_migr_unit(struct intel_super
*super
,
3007 struct imsm_dev
*dev
)
3009 /* calculate the conversion factor between per member 'blocks'
3010 * (md/{resync,rebuild}_start) and imsm migration units, return
3011 * 0 for the 'not migrating' and 'unsupported migration' cases
3013 if (!dev
->vol
.migr_state
)
3016 switch (migr_type(dev
)) {
3017 case MIGR_GEN_MIGR
: {
3018 struct migr_record
*migr_rec
= super
->migr_rec
;
3019 return __le32_to_cpu(migr_rec
->blocks_per_unit
);
3024 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
3025 __u32 stripes_per_unit
;
3026 __u32 blocks_per_unit
;
3035 /* yes, this is really the translation of migr_units to
3036 * per-member blocks in the 'resync' case
3038 stripes_per_unit
= num_stripes_per_unit_resync(dev
);
3039 migr_chunk
= migr_strip_blocks_resync(dev
);
3040 disks
= imsm_num_data_members(map
);
3041 blocks_per_unit
= stripes_per_unit
* migr_chunk
* disks
;
3042 stripe
= __le16_to_cpu(map
->blocks_per_strip
) * disks
;
3043 segment
= blocks_per_unit
/ stripe
;
3044 block_rel
= blocks_per_unit
- segment
* stripe
;
3045 parity_depth
= parity_segment_depth(dev
);
3046 block_map
= map_migr_block(dev
, block_rel
);
3047 return block_map
+ parity_depth
* segment
;
3049 case MIGR_REBUILD
: {
3050 __u32 stripes_per_unit
;
3053 stripes_per_unit
= num_stripes_per_unit_rebuild(dev
);
3054 migr_chunk
= migr_strip_blocks_rebuild(dev
);
3055 return migr_chunk
* stripes_per_unit
;
3057 case MIGR_STATE_CHANGE
:
3063 static int imsm_level_to_layout(int level
)
3071 return ALGORITHM_LEFT_ASYMMETRIC
;
3078 /*******************************************************************************
3079 * Function: read_imsm_migr_rec
3080 * Description: Function reads imsm migration record from last sector of disk
3082 * fd : disk descriptor
3083 * super : metadata info
3087 ******************************************************************************/
3088 static int read_imsm_migr_rec(int fd
, struct intel_super
*super
)
3091 unsigned int sector_size
= super
->sector_size
;
3092 unsigned long long dsize
;
3094 get_dev_size(fd
, NULL
, &dsize
);
3095 if (lseek64(fd
, dsize
- (sector_size
*MIGR_REC_SECTOR_POSITION
),
3097 pr_err("Cannot seek to anchor block: %s\n",
3101 if ((unsigned int)read(fd
, super
->migr_rec_buf
,
3102 MIGR_REC_BUF_SECTORS
*sector_size
) !=
3103 MIGR_REC_BUF_SECTORS
*sector_size
) {
3104 pr_err("Cannot read migr record block: %s\n",
3109 if (sector_size
== 4096)
3110 convert_from_4k_imsm_migr_rec(super
);
3116 static struct imsm_dev
*imsm_get_device_during_migration(
3117 struct intel_super
*super
)
3120 struct intel_dev
*dv
;
3122 for (dv
= super
->devlist
; dv
; dv
= dv
->next
) {
3123 if (is_gen_migration(dv
->dev
))
3129 /*******************************************************************************
3130 * Function: load_imsm_migr_rec
3131 * Description: Function reads imsm migration record (it is stored at the last
3134 * super : imsm internal array info
3138 * -2 : no migration in progress
3139 ******************************************************************************/
3140 static int load_imsm_migr_rec(struct intel_super
*super
)
3146 struct imsm_dev
*dev
;
3147 struct imsm_map
*map
;
3151 /* find map under migration */
3152 dev
= imsm_get_device_during_migration(super
);
3153 /* nothing to load,no migration in progress?
3158 map
= get_imsm_map(dev
, MAP_0
);
3162 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
3163 /* skip spare and failed disks
3167 /* read only from one of the first two slots
3169 slot
= get_imsm_disk_slot(map
, dl
->index
);
3170 if (slot
> 1 || slot
< 0)
3173 if (!is_fd_valid(dl
->fd
)) {
3174 sprintf(nm
, "%d:%d", dl
->major
, dl
->minor
);
3175 fd
= dev_open(nm
, O_RDONLY
);
3177 if (is_fd_valid(fd
)) {
3187 if (!is_fd_valid(fd
))
3189 retval
= read_imsm_migr_rec(fd
, super
);
3196 /*******************************************************************************
3197 * function: imsm_create_metadata_checkpoint_update
3198 * Description: It creates update for checkpoint change.
3200 * super : imsm internal array info
3201 * u : pointer to prepared update
3204 * If length is equal to 0, input pointer u contains no update
3205 ******************************************************************************/
3206 static int imsm_create_metadata_checkpoint_update(
3207 struct intel_super
*super
,
3208 struct imsm_update_general_migration_checkpoint
**u
)
3211 int update_memory_size
= 0;
3213 dprintf("(enter)\n");
3219 /* size of all update data without anchor */
3220 update_memory_size
=
3221 sizeof(struct imsm_update_general_migration_checkpoint
);
3223 *u
= xcalloc(1, update_memory_size
);
3225 dprintf("error: cannot get memory\n");
3228 (*u
)->type
= update_general_migration_checkpoint
;
3229 (*u
)->curr_migr_unit
= current_migr_unit(super
->migr_rec
);
3230 dprintf("prepared for %llu\n", (unsigned long long)(*u
)->curr_migr_unit
);
3232 return update_memory_size
;
3235 static void imsm_update_metadata_locally(struct supertype
*st
,
3236 void *buf
, int len
);
3238 /*******************************************************************************
3239 * Function: write_imsm_migr_rec
3240 * Description: Function writes imsm migration record
3241 * (at the last sector of disk)
3243 * super : imsm internal array info
3247 ******************************************************************************/
3248 static int write_imsm_migr_rec(struct supertype
*st
)
3250 struct intel_super
*super
= st
->sb
;
3251 unsigned int sector_size
= super
->sector_size
;
3252 unsigned long long dsize
;
3256 struct imsm_update_general_migration_checkpoint
*u
;
3257 struct imsm_dev
*dev
;
3258 struct imsm_map
*map
;
3260 /* find map under migration */
3261 dev
= imsm_get_device_during_migration(super
);
3262 /* if no migration, write buffer anyway to clear migr_record
3263 * on disk based on first available device
3266 dev
= get_imsm_dev(super
, super
->current_vol
< 0 ? 0 :
3267 super
->current_vol
);
3269 map
= get_imsm_map(dev
, MAP_0
);
3271 if (sector_size
== 4096)
3272 convert_to_4k_imsm_migr_rec(super
);
3273 for (sd
= super
->disks
; sd
; sd
= sd
->next
) {
3276 /* skip failed and spare devices */
3279 /* write to 2 first slots only */
3281 slot
= get_imsm_disk_slot(map
, sd
->index
);
3282 if (map
== NULL
|| slot
> 1 || slot
< 0)
3285 get_dev_size(sd
->fd
, NULL
, &dsize
);
3286 if (lseek64(sd
->fd
, dsize
- (MIGR_REC_SECTOR_POSITION
*
3289 pr_err("Cannot seek to anchor block: %s\n",
3293 if ((unsigned int)write(sd
->fd
, super
->migr_rec_buf
,
3294 MIGR_REC_BUF_SECTORS
*sector_size
) !=
3295 MIGR_REC_BUF_SECTORS
*sector_size
) {
3296 pr_err("Cannot write migr record block: %s\n",
3301 if (sector_size
== 4096)
3302 convert_from_4k_imsm_migr_rec(super
);
3303 /* update checkpoint information in metadata */
3304 len
= imsm_create_metadata_checkpoint_update(super
, &u
);
3306 dprintf("imsm: Cannot prepare update\n");
3309 /* update metadata locally */
3310 imsm_update_metadata_locally(st
, u
, len
);
3311 /* and possibly remotely */
3312 if (st
->update_tail
) {
3313 append_metadata_update(st
, u
, len
);
3314 /* during reshape we do all work inside metadata handler
3315 * manage_reshape(), so metadata update has to be triggered
3318 flush_metadata_updates(st
);
3319 st
->update_tail
= &st
->updates
;
3328 /* spare/missing disks activations are not allowe when
3329 * array/container performs reshape operation, because
3330 * all arrays in container works on the same disks set
3332 int imsm_reshape_blocks_arrays_changes(struct intel_super
*super
)
3335 struct intel_dev
*i_dev
;
3336 struct imsm_dev
*dev
;
3338 /* check whole container
3340 for (i_dev
= super
->devlist
; i_dev
; i_dev
= i_dev
->next
) {
3342 if (is_gen_migration(dev
)) {
3343 /* No repair during any migration in container
3351 static unsigned long long imsm_component_size_alignment_check(int level
,
3353 unsigned int sector_size
,
3354 unsigned long long component_size
)
3356 unsigned int component_size_alignment
;
3358 /* check component size alignment
3360 component_size_alignment
= component_size
% (chunk_size
/sector_size
);
3362 dprintf("(Level: %i, chunk_size = %i, component_size = %llu), component_size_alignment = %u\n",
3363 level
, chunk_size
, component_size
,
3364 component_size_alignment
);
3366 if (component_size_alignment
&& (level
!= 1) && (level
!= UnSet
)) {
3367 dprintf("imsm: reported component size aligned from %llu ",
3369 component_size
-= component_size_alignment
;
3370 dprintf_cont("to %llu (%i).\n",
3371 component_size
, component_size_alignment
);
3374 return component_size
;
3377 /*******************************************************************************
3378 * Function: get_bitmap_header_sector
3379 * Description: Returns the sector where the bitmap header is placed.
3381 * st : supertype information
3382 * dev_idx : index of the device with bitmap
3385 * The sector where the bitmap header is placed
3386 ******************************************************************************/
3387 static unsigned long long get_bitmap_header_sector(struct intel_super
*super
,
3390 struct imsm_dev
*dev
= get_imsm_dev(super
, dev_idx
);
3391 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
3393 if (!super
->sector_size
) {
3394 dprintf("sector size is not set\n");
3398 return pba_of_lba0(map
) + calc_component_size(map
, dev
) +
3399 (IMSM_BITMAP_HEADER_OFFSET
/ super
->sector_size
);
3402 /*******************************************************************************
3403 * Function: get_bitmap_sector
3404 * Description: Returns the sector where the bitmap is placed.
3406 * st : supertype information
3407 * dev_idx : index of the device with bitmap
3410 * The sector where the bitmap is placed
3411 ******************************************************************************/
3412 static unsigned long long get_bitmap_sector(struct intel_super
*super
,
3415 if (!super
->sector_size
) {
3416 dprintf("sector size is not set\n");
3420 return get_bitmap_header_sector(super
, dev_idx
) +
3421 (IMSM_BITMAP_HEADER_SIZE
/ super
->sector_size
);
3424 static unsigned long long get_ppl_sector(struct intel_super
*super
, int dev_idx
)
3426 struct imsm_dev
*dev
= get_imsm_dev(super
, dev_idx
);
3427 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
3429 return pba_of_lba0(map
) +
3430 (num_data_stripes(map
) * map
->blocks_per_strip
);
3433 static void getinfo_super_imsm_volume(struct supertype
*st
, struct mdinfo
*info
, char *dmap
)
3435 struct intel_super
*super
= st
->sb
;
3436 struct migr_record
*migr_rec
= super
->migr_rec
;
3437 struct imsm_dev
*dev
= get_imsm_dev(super
, super
->current_vol
);
3438 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
3439 struct imsm_map
*prev_map
= get_imsm_map(dev
, MAP_1
);
3440 struct imsm_map
*map_to_analyse
= map
;
3442 int map_disks
= info
->array
.raid_disks
;
3444 memset(info
, 0, sizeof(*info
));
3446 map_to_analyse
= prev_map
;
3448 dl
= super
->current_disk
;
3450 info
->container_member
= super
->current_vol
;
3451 info
->array
.raid_disks
= map
->num_members
;
3452 info
->array
.level
= get_imsm_raid_level(map_to_analyse
);
3453 info
->array
.layout
= imsm_level_to_layout(info
->array
.level
);
3454 info
->array
.md_minor
= -1;
3455 info
->array
.ctime
= 0;
3456 info
->array
.utime
= 0;
3457 info
->array
.chunk_size
=
3458 __le16_to_cpu(map_to_analyse
->blocks_per_strip
) << 9;
3459 info
->array
.state
= !(dev
->vol
.dirty
& RAIDVOL_DIRTY
);
3460 info
->custom_array_size
= imsm_dev_size(dev
);
3461 info
->recovery_blocked
= imsm_reshape_blocks_arrays_changes(st
->sb
);
3463 if (is_gen_migration(dev
)) {
3465 * device prev_map should be added if it is in the middle
3470 info
->reshape_active
= 1;
3471 info
->new_level
= get_imsm_raid_level(map
);
3472 info
->new_layout
= imsm_level_to_layout(info
->new_level
);
3473 info
->new_chunk
= __le16_to_cpu(map
->blocks_per_strip
) << 9;
3474 info
->delta_disks
= map
->num_members
- prev_map
->num_members
;
3475 if (info
->delta_disks
) {
3476 /* this needs to be applied to every array
3479 info
->reshape_active
= CONTAINER_RESHAPE
;
3481 /* We shape information that we give to md might have to be
3482 * modify to cope with md's requirement for reshaping arrays.
3483 * For example, when reshaping a RAID0, md requires it to be
3484 * presented as a degraded RAID4.
3485 * Also if a RAID0 is migrating to a RAID5 we need to specify
3486 * the array as already being RAID5, but the 'before' layout
3487 * is a RAID4-like layout.
3489 switch (info
->array
.level
) {
3491 switch(info
->new_level
) {
3493 /* conversion is happening as RAID4 */
3494 info
->array
.level
= 4;
3495 info
->array
.raid_disks
+= 1;
3498 /* conversion is happening as RAID5 */
3499 info
->array
.level
= 5;
3500 info
->array
.layout
= ALGORITHM_PARITY_N
;
3501 info
->delta_disks
-= 1;
3504 /* FIXME error message */
3505 info
->array
.level
= UnSet
;
3511 info
->new_level
= UnSet
;
3512 info
->new_layout
= UnSet
;
3513 info
->new_chunk
= info
->array
.chunk_size
;
3514 info
->delta_disks
= 0;
3518 info
->disk
.major
= dl
->major
;
3519 info
->disk
.minor
= dl
->minor
;
3520 info
->disk
.number
= dl
->index
;
3521 info
->disk
.raid_disk
= get_imsm_disk_slot(map_to_analyse
,
3525 info
->data_offset
= pba_of_lba0(map_to_analyse
);
3526 info
->component_size
= calc_component_size(map
, dev
);
3527 info
->component_size
= imsm_component_size_alignment_check(
3529 info
->array
.chunk_size
,
3531 info
->component_size
);
3532 info
->bb
.supported
= 1;
3534 memset(info
->uuid
, 0, sizeof(info
->uuid
));
3535 info
->recovery_start
= MaxSector
;
3537 if (info
->array
.level
== 5 &&
3538 (dev
->rwh_policy
== RWH_DISTRIBUTED
||
3539 dev
->rwh_policy
== RWH_MULTIPLE_DISTRIBUTED
)) {
3540 info
->consistency_policy
= CONSISTENCY_POLICY_PPL
;
3541 info
->ppl_sector
= get_ppl_sector(super
, super
->current_vol
);
3542 if (dev
->rwh_policy
== RWH_MULTIPLE_DISTRIBUTED
)
3543 info
->ppl_size
= MULTIPLE_PPL_AREA_SIZE_IMSM
>> 9;
3545 info
->ppl_size
= (PPL_HEADER_SIZE
+ PPL_ENTRY_SPACE
)
3547 } else if (info
->array
.level
<= 0) {
3548 info
->consistency_policy
= CONSISTENCY_POLICY_NONE
;
3550 if (dev
->rwh_policy
== RWH_BITMAP
) {
3551 info
->bitmap_offset
= get_bitmap_sector(super
, super
->current_vol
);
3552 info
->consistency_policy
= CONSISTENCY_POLICY_BITMAP
;
3554 info
->consistency_policy
= CONSISTENCY_POLICY_RESYNC
;
3558 info
->reshape_progress
= 0;
3559 info
->resync_start
= MaxSector
;
3560 if ((map_to_analyse
->map_state
== IMSM_T_STATE_UNINITIALIZED
||
3561 !(info
->array
.state
& 1)) &&
3562 imsm_reshape_blocks_arrays_changes(super
) == 0) {
3563 info
->resync_start
= 0;
3565 if (dev
->vol
.migr_state
) {
3566 switch (migr_type(dev
)) {
3569 __u64 blocks_per_unit
= blocks_per_migr_unit(super
,
3571 __u64 units
= vol_curr_migr_unit(dev
);
3573 info
->resync_start
= blocks_per_unit
* units
;
3576 case MIGR_GEN_MIGR
: {
3577 __u64 blocks_per_unit
= blocks_per_migr_unit(super
,
3579 __u64 units
= current_migr_unit(migr_rec
);
3582 if (__le32_to_cpu(migr_rec
->ascending_migr
) &&
3584 (get_num_migr_units(migr_rec
)-1)) &&
3585 (super
->migr_rec
->rec_status
==
3586 __cpu_to_le32(UNIT_SRC_IN_CP_AREA
)))
3589 info
->reshape_progress
= blocks_per_unit
* units
;
3591 dprintf("IMSM: General Migration checkpoint : %llu (%llu) -> read reshape progress : %llu\n",
3592 (unsigned long long)units
,
3593 (unsigned long long)blocks_per_unit
,
3594 info
->reshape_progress
);
3596 used_disks
= imsm_num_data_members(prev_map
);
3597 if (used_disks
> 0) {
3598 info
->custom_array_size
= per_dev_array_size(map
) *
3603 /* we could emulate the checkpointing of
3604 * 'sync_action=check' migrations, but for now
3605 * we just immediately complete them
3608 /* this is handled by container_content_imsm() */
3609 case MIGR_STATE_CHANGE
:
3610 /* FIXME handle other migrations */
3612 /* we are not dirty, so... */
3613 info
->resync_start
= MaxSector
;
3617 strncpy(info
->name
, (char *) dev
->volume
, MAX_RAID_SERIAL_LEN
);
3618 info
->name
[MAX_RAID_SERIAL_LEN
] = 0;
3620 info
->array
.major_version
= -1;
3621 info
->array
.minor_version
= -2;
3622 sprintf(info
->text_version
, "/%s/%d", st
->container_devnm
, info
->container_member
);
3623 info
->safe_mode_delay
= 4000; /* 4 secs like the Matrix driver */
3624 uuid_from_super_imsm(st
, info
->uuid
);
3628 for (i
=0; i
<map_disks
; i
++) {
3630 if (i
< info
->array
.raid_disks
) {
3631 struct imsm_disk
*dsk
;
3632 j
= get_imsm_disk_idx(dev
, i
, MAP_X
);
3633 dsk
= get_imsm_disk(super
, j
);
3634 if (dsk
&& (dsk
->status
& CONFIGURED_DISK
))
3641 static __u8
imsm_check_degraded(struct intel_super
*super
, struct imsm_dev
*dev
,
3642 int failed
, int look_in_map
);
3644 static int imsm_count_failed(struct intel_super
*super
, struct imsm_dev
*dev
,
3647 static void manage_second_map(struct intel_super
*super
, struct imsm_dev
*dev
)
3649 if (is_gen_migration(dev
)) {
3652 struct imsm_map
*map2
= get_imsm_map(dev
, MAP_1
);
3654 failed
= imsm_count_failed(super
, dev
, MAP_1
);
3655 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_1
);
3656 if (map2
->map_state
!= map_state
) {
3657 map2
->map_state
= map_state
;
3658 super
->updates_pending
++;
3663 static struct imsm_disk
*get_imsm_missing(struct intel_super
*super
, __u8 index
)
3667 for (d
= super
->missing
; d
; d
= d
->next
)
3668 if (d
->index
== index
)
3673 static void getinfo_super_imsm(struct supertype
*st
, struct mdinfo
*info
, char *map
)
3675 struct intel_super
*super
= st
->sb
;
3676 struct imsm_disk
*disk
;
3677 int map_disks
= info
->array
.raid_disks
;
3678 int max_enough
= -1;
3680 struct imsm_super
*mpb
;
3682 if (super
->current_vol
>= 0) {
3683 getinfo_super_imsm_volume(st
, info
, map
);
3686 memset(info
, 0, sizeof(*info
));
3688 /* Set raid_disks to zero so that Assemble will always pull in valid
3691 info
->array
.raid_disks
= 0;
3692 info
->array
.level
= LEVEL_CONTAINER
;
3693 info
->array
.layout
= 0;
3694 info
->array
.md_minor
= -1;
3695 info
->array
.ctime
= 0; /* N/A for imsm */
3696 info
->array
.utime
= 0;
3697 info
->array
.chunk_size
= 0;
3699 info
->disk
.major
= 0;
3700 info
->disk
.minor
= 0;
3701 info
->disk
.raid_disk
= -1;
3702 info
->reshape_active
= 0;
3703 info
->array
.major_version
= -1;
3704 info
->array
.minor_version
= -2;
3705 strcpy(info
->text_version
, "imsm");
3706 info
->safe_mode_delay
= 0;
3707 info
->disk
.number
= -1;
3708 info
->disk
.state
= 0;
3710 info
->recovery_start
= MaxSector
;
3711 info
->recovery_blocked
= imsm_reshape_blocks_arrays_changes(st
->sb
);
3712 info
->bb
.supported
= 1;
3714 /* do we have the all the insync disks that we expect? */
3715 mpb
= super
->anchor
;
3716 info
->events
= __le32_to_cpu(mpb
->generation_num
);
3718 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
3719 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
3720 int failed
, enough
, j
, missing
= 0;
3721 struct imsm_map
*map
;
3724 failed
= imsm_count_failed(super
, dev
, MAP_0
);
3725 state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
3726 map
= get_imsm_map(dev
, MAP_0
);
3728 /* any newly missing disks?
3729 * (catches single-degraded vs double-degraded)
3731 for (j
= 0; j
< map
->num_members
; j
++) {
3732 __u32 ord
= get_imsm_ord_tbl_ent(dev
, j
, MAP_0
);
3733 __u32 idx
= ord_to_idx(ord
);
3735 if (super
->disks
&& super
->disks
->index
== (int)idx
)
3736 info
->disk
.raid_disk
= j
;
3738 if (!(ord
& IMSM_ORD_REBUILD
) &&
3739 get_imsm_missing(super
, idx
)) {
3745 if (state
== IMSM_T_STATE_FAILED
)
3747 else if (state
== IMSM_T_STATE_DEGRADED
&&
3748 (state
!= map
->map_state
|| missing
))
3750 else /* we're normal, or already degraded */
3752 if (is_gen_migration(dev
) && missing
) {
3753 /* during general migration we need all disks
3754 * that process is running on.
3755 * No new missing disk is allowed.
3759 /* no more checks necessary
3763 /* in the missing/failed disk case check to see
3764 * if at least one array is runnable
3766 max_enough
= max(max_enough
, enough
);
3768 dprintf("enough: %d\n", max_enough
);
3769 info
->container_enough
= max_enough
;
3772 __u32 reserved
= imsm_reserved_sectors(super
, super
->disks
);
3774 disk
= &super
->disks
->disk
;
3775 info
->data_offset
= total_blocks(&super
->disks
->disk
) - reserved
;
3776 info
->component_size
= reserved
;
3777 info
->disk
.state
= is_configured(disk
) ? (1 << MD_DISK_ACTIVE
) : 0;
3778 /* we don't change info->disk.raid_disk here because
3779 * this state will be finalized in mdmon after we have
3780 * found the 'most fresh' version of the metadata
3782 info
->disk
.state
|= is_failed(disk
) ? (1 << MD_DISK_FAULTY
) : 0;
3783 info
->disk
.state
|= (is_spare(disk
) || is_journal(disk
)) ?
3784 0 : (1 << MD_DISK_SYNC
);
3787 /* only call uuid_from_super_imsm when this disk is part of a populated container,
3788 * ->compare_super may have updated the 'num_raid_devs' field for spares
3790 if (info
->disk
.state
& (1 << MD_DISK_SYNC
) || super
->anchor
->num_raid_devs
)
3791 uuid_from_super_imsm(st
, info
->uuid
);
3793 memcpy(info
->uuid
, uuid_zero
, sizeof(uuid_zero
));
3795 /* I don't know how to compute 'map' on imsm, so use safe default */
3798 for (i
= 0; i
< map_disks
; i
++)
3804 /* allocates memory and fills disk in mdinfo structure
3805 * for each disk in array */
3806 struct mdinfo
*getinfo_super_disks_imsm(struct supertype
*st
)
3808 struct mdinfo
*mddev
;
3809 struct intel_super
*super
= st
->sb
;
3810 struct imsm_disk
*disk
;
3813 if (!super
|| !super
->disks
)
3816 mddev
= xcalloc(1, sizeof(*mddev
));
3820 tmp
= xcalloc(1, sizeof(*tmp
));
3822 tmp
->next
= mddev
->devs
;
3824 tmp
->disk
.number
= count
++;
3825 tmp
->disk
.major
= dl
->major
;
3826 tmp
->disk
.minor
= dl
->minor
;
3827 tmp
->disk
.state
= is_configured(disk
) ?
3828 (1 << MD_DISK_ACTIVE
) : 0;
3829 tmp
->disk
.state
|= is_failed(disk
) ? (1 << MD_DISK_FAULTY
) : 0;
3830 tmp
->disk
.state
|= is_spare(disk
) ? 0 : (1 << MD_DISK_SYNC
);
3831 tmp
->disk
.raid_disk
= -1;
3837 static int update_super_imsm(struct supertype
*st
, struct mdinfo
*info
,
3838 char *update
, char *devname
, int verbose
,
3839 int uuid_set
, char *homehost
)
3841 /* For 'assemble' and 'force' we need to return non-zero if any
3842 * change was made. For others, the return value is ignored.
3843 * Update options are:
3844 * force-one : This device looks a bit old but needs to be included,
3845 * update age info appropriately.
3846 * assemble: clear any 'faulty' flag to allow this device to
3848 * force-array: Array is degraded but being forced, mark it clean
3849 * if that will be needed to assemble it.
3851 * newdev: not used ????
3852 * grow: Array has gained a new device - this is currently for
3854 * resync: mark as dirty so a resync will happen.
3855 * name: update the name - preserving the homehost
3856 * uuid: Change the uuid of the array to match watch is given
3858 * Following are not relevant for this imsm:
3859 * sparc2.2 : update from old dodgey metadata
3860 * super-minor: change the preferred_minor number
3861 * summaries: update redundant counters.
3862 * homehost: update the recorded homehost
3863 * _reshape_progress: record new reshape_progress position.
3866 struct intel_super
*super
= st
->sb
;
3867 struct imsm_super
*mpb
;
3869 /* we can only update container info */
3870 if (!super
|| super
->current_vol
>= 0 || !super
->anchor
)
3873 mpb
= super
->anchor
;
3875 if (strcmp(update
, "uuid") == 0) {
3876 /* We take this to mean that the family_num should be updated.
3877 * However that is much smaller than the uuid so we cannot really
3878 * allow an explicit uuid to be given. And it is hard to reliably
3880 * So if !uuid_set we know the current uuid is random and just used
3881 * the first 'int' and copy it to the other 3 positions.
3882 * Otherwise we require the 4 'int's to be the same as would be the
3883 * case if we are using a random uuid. So an explicit uuid will be
3884 * accepted as long as all for ints are the same... which shouldn't hurt
3887 info
->uuid
[1] = info
->uuid
[2] = info
->uuid
[3] = info
->uuid
[0];
3890 if (info
->uuid
[0] != info
->uuid
[1] ||
3891 info
->uuid
[1] != info
->uuid
[2] ||
3892 info
->uuid
[2] != info
->uuid
[3])
3898 mpb
->orig_family_num
= info
->uuid
[0];
3899 } else if (strcmp(update
, "assemble") == 0)
3904 /* successful update? recompute checksum */
3906 mpb
->check_sum
= __le32_to_cpu(__gen_imsm_checksum(mpb
));
3911 static size_t disks_to_mpb_size(int disks
)
3915 size
= sizeof(struct imsm_super
);
3916 size
+= (disks
- 1) * sizeof(struct imsm_disk
);
3917 size
+= 2 * sizeof(struct imsm_dev
);
3918 /* up to 2 maps per raid device (-2 for imsm_maps in imsm_dev */
3919 size
+= (4 - 2) * sizeof(struct imsm_map
);
3920 /* 4 possible disk_ord_tbl's */
3921 size
+= 4 * (disks
- 1) * sizeof(__u32
);
3922 /* maximum bbm log */
3923 size
+= sizeof(struct bbm_log
);
3928 static __u64
avail_size_imsm(struct supertype
*st
, __u64 devsize
,
3929 unsigned long long data_offset
)
3931 if (devsize
< (MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
))
3934 return devsize
- (MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
);
3937 static void free_devlist(struct intel_super
*super
)
3939 struct intel_dev
*dv
;
3941 while (super
->devlist
) {
3942 dv
= super
->devlist
->next
;
3943 free(super
->devlist
->dev
);
3944 free(super
->devlist
);
3945 super
->devlist
= dv
;
3949 static void imsm_copy_dev(struct imsm_dev
*dest
, struct imsm_dev
*src
)
3951 memcpy(dest
, src
, sizeof_imsm_dev(src
, 0));
3954 static int compare_super_imsm(struct supertype
*st
, struct supertype
*tst
,
3958 * 0 same, or first was empty, and second was copied
3959 * 1 sb are different
3961 struct intel_super
*first
= st
->sb
;
3962 struct intel_super
*sec
= tst
->sb
;
3970 /* in platform dependent environment test if the disks
3971 * use the same Intel hba
3972 * if not on Intel hba at all, allow anything.
3973 * doesn't check HBAs if num_raid_devs is not set, as it means
3974 * it is a free floating spare, and all spares regardless of HBA type
3975 * will fall into separate container during the assembly
3977 if (first
->hba
&& sec
->hba
&& first
->anchor
->num_raid_devs
!= 0) {
3978 if (first
->hba
->type
!= sec
->hba
->type
) {
3980 pr_err("HBAs of devices do not match %s != %s\n",
3981 get_sys_dev_type(first
->hba
->type
),
3982 get_sys_dev_type(sec
->hba
->type
));
3985 if (first
->orom
!= sec
->orom
) {
3987 pr_err("HBAs of devices do not match %s != %s\n",
3988 first
->hba
->pci_id
, sec
->hba
->pci_id
);
3993 if (first
->anchor
->num_raid_devs
> 0 &&
3994 sec
->anchor
->num_raid_devs
> 0) {
3995 /* Determine if these disks might ever have been
3996 * related. Further disambiguation can only take place
3997 * in load_super_imsm_all
3999 __u32 first_family
= first
->anchor
->orig_family_num
;
4000 __u32 sec_family
= sec
->anchor
->orig_family_num
;
4002 if (memcmp(first
->anchor
->sig
, sec
->anchor
->sig
,
4003 MAX_SIGNATURE_LENGTH
) != 0)
4006 if (first_family
== 0)
4007 first_family
= first
->anchor
->family_num
;
4008 if (sec_family
== 0)
4009 sec_family
= sec
->anchor
->family_num
;
4011 if (first_family
!= sec_family
)
4016 /* if an anchor does not have num_raid_devs set then it is a free
4017 * floating spare. don't assosiate spare with any array, as during assembly
4018 * spares shall fall into separate container, from which they can be moved
4021 if (first
->anchor
->num_raid_devs
^ sec
->anchor
->num_raid_devs
)
4027 static void fd2devname(int fd
, char *name
)
4035 snprintf(name
, MAX_RAID_SERIAL_LEN
, "/dev/%s", nm
);
4038 static int nvme_get_serial(int fd
, void *buf
, size_t buf_len
)
4040 char path
[PATH_MAX
];
4041 char *name
= fd2kname(fd
);
4046 if (strncmp(name
, "nvme", 4) != 0)
4049 if (!diskfd_to_devpath(fd
, 1, path
))
4052 return devpath_to_char(path
, "serial", buf
, buf_len
, 0);
4055 extern int scsi_get_serial(int fd
, void *buf
, size_t buf_len
);
4057 static int imsm_read_serial(int fd
, char *devname
,
4058 __u8
*serial
, size_t serial_buf_len
)
4067 memset(buf
, 0, sizeof(buf
));
4069 rv
= nvme_get_serial(fd
, buf
, sizeof(buf
));
4072 rv
= scsi_get_serial(fd
, buf
, sizeof(buf
));
4074 if (rv
&& check_env("IMSM_DEVNAME_AS_SERIAL")) {
4075 memset(serial
, 0, MAX_RAID_SERIAL_LEN
);
4076 fd2devname(fd
, (char *) serial
);
4082 pr_err("Failed to retrieve serial for %s\n",
4087 /* trim all whitespace and non-printable characters and convert
4090 for (i
= 0, dest
= buf
; i
< sizeof(buf
) && buf
[i
]; i
++) {
4093 /* ':' is reserved for use in placeholder serial
4094 * numbers for missing disks
4105 if (len
> serial_buf_len
) {
4106 /* truncate leading characters */
4107 dest
+= len
- serial_buf_len
;
4108 len
= serial_buf_len
;
4111 memset(serial
, 0, serial_buf_len
);
4112 memcpy(serial
, dest
, len
);
4117 static int serialcmp(__u8
*s1
, __u8
*s2
)
4119 return strncmp((char *) s1
, (char *) s2
, MAX_RAID_SERIAL_LEN
);
4122 static void serialcpy(__u8
*dest
, __u8
*src
)
4124 strncpy((char *) dest
, (char *) src
, MAX_RAID_SERIAL_LEN
);
4127 static struct dl
*serial_to_dl(__u8
*serial
, struct intel_super
*super
)
4131 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
4132 if (serialcmp(dl
->serial
, serial
) == 0)
4138 static struct imsm_disk
*
4139 __serial_to_disk(__u8
*serial
, struct imsm_super
*mpb
, int *idx
)
4143 for (i
= 0; i
< mpb
->num_disks
; i
++) {
4144 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, i
);
4146 if (serialcmp(disk
->serial
, serial
) == 0) {
4157 load_imsm_disk(int fd
, struct intel_super
*super
, char *devname
, int keep_fd
)
4159 struct imsm_disk
*disk
;
4164 __u8 serial
[MAX_RAID_SERIAL_LEN
];
4166 rv
= imsm_read_serial(fd
, devname
, serial
, MAX_RAID_SERIAL_LEN
);
4171 dl
= xcalloc(1, sizeof(*dl
));
4174 dl
->major
= major(stb
.st_rdev
);
4175 dl
->minor
= minor(stb
.st_rdev
);
4176 dl
->next
= super
->disks
;
4177 dl
->fd
= keep_fd
? fd
: -1;
4178 assert(super
->disks
== NULL
);
4180 serialcpy(dl
->serial
, serial
);
4183 fd2devname(fd
, name
);
4185 dl
->devname
= xstrdup(devname
);
4187 dl
->devname
= xstrdup(name
);
4189 /* look up this disk's index in the current anchor */
4190 disk
= __serial_to_disk(dl
->serial
, super
->anchor
, &dl
->index
);
4193 /* only set index on disks that are a member of a
4194 * populated contianer, i.e. one with raid_devs
4196 if (is_failed(&dl
->disk
))
4198 else if (is_spare(&dl
->disk
) || is_journal(&dl
->disk
))
4205 /* When migrating map0 contains the 'destination' state while map1
4206 * contains the current state. When not migrating map0 contains the
4207 * current state. This routine assumes that map[0].map_state is set to
4208 * the current array state before being called.
4210 * Migration is indicated by one of the following states
4211 * 1/ Idle (migr_state=0 map0state=normal||unitialized||degraded||failed)
4212 * 2/ Initialize (migr_state=1 migr_type=MIGR_INIT map0state=normal
4213 * map1state=unitialized)
4214 * 3/ Repair (Resync) (migr_state=1 migr_type=MIGR_REPAIR map0state=normal
4216 * 4/ Rebuild (migr_state=1 migr_type=MIGR_REBUILD map0state=normal
4217 * map1state=degraded)
4218 * 5/ Migration (mig_state=1 migr_type=MIGR_GEN_MIGR map0state=normal
4221 static void migrate(struct imsm_dev
*dev
, struct intel_super
*super
,
4222 __u8 to_state
, int migr_type
)
4224 struct imsm_map
*dest
;
4225 struct imsm_map
*src
= get_imsm_map(dev
, MAP_0
);
4227 dev
->vol
.migr_state
= 1;
4228 set_migr_type(dev
, migr_type
);
4229 set_vol_curr_migr_unit(dev
, 0);
4230 dest
= get_imsm_map(dev
, MAP_1
);
4232 /* duplicate and then set the target end state in map[0] */
4233 memcpy(dest
, src
, sizeof_imsm_map(src
));
4234 if (migr_type
== MIGR_GEN_MIGR
) {
4238 for (i
= 0; i
< src
->num_members
; i
++) {
4239 ord
= __le32_to_cpu(src
->disk_ord_tbl
[i
]);
4240 set_imsm_ord_tbl_ent(src
, i
, ord_to_idx(ord
));
4244 if (migr_type
== MIGR_GEN_MIGR
)
4245 /* Clear migration record */
4246 memset(super
->migr_rec
, 0, sizeof(struct migr_record
));
4248 src
->map_state
= to_state
;
4251 static void end_migration(struct imsm_dev
*dev
, struct intel_super
*super
,
4254 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
4255 struct imsm_map
*prev
= get_imsm_map(dev
, dev
->vol
.migr_state
== 0 ?
4259 /* merge any IMSM_ORD_REBUILD bits that were not successfully
4260 * completed in the last migration.
4262 * FIXME add support for raid-level-migration
4264 if (map_state
!= map
->map_state
&& (is_gen_migration(dev
) == false) &&
4265 prev
->map_state
!= IMSM_T_STATE_UNINITIALIZED
) {
4266 /* when final map state is other than expected
4267 * merge maps (not for migration)
4271 for (i
= 0; i
< prev
->num_members
; i
++)
4272 for (j
= 0; j
< map
->num_members
; j
++)
4273 /* during online capacity expansion
4274 * disks position can be changed
4275 * if takeover is used
4277 if (ord_to_idx(map
->disk_ord_tbl
[j
]) ==
4278 ord_to_idx(prev
->disk_ord_tbl
[i
])) {
4279 map
->disk_ord_tbl
[j
] |=
4280 prev
->disk_ord_tbl
[i
];
4283 failed
= imsm_count_failed(super
, dev
, MAP_0
);
4284 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
4287 dev
->vol
.migr_state
= 0;
4288 set_migr_type(dev
, 0);
4289 set_vol_curr_migr_unit(dev
, 0);
4290 map
->map_state
= map_state
;
4293 static int parse_raid_devices(struct intel_super
*super
)
4296 struct imsm_dev
*dev_new
;
4297 size_t len
, len_migr
;
4299 size_t space_needed
= 0;
4300 struct imsm_super
*mpb
= super
->anchor
;
4302 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
4303 struct imsm_dev
*dev_iter
= __get_imsm_dev(super
->anchor
, i
);
4304 struct intel_dev
*dv
;
4306 len
= sizeof_imsm_dev(dev_iter
, 0);
4307 len_migr
= sizeof_imsm_dev(dev_iter
, 1);
4309 space_needed
+= len_migr
- len
;
4311 dv
= xmalloc(sizeof(*dv
));
4312 if (max_len
< len_migr
)
4314 if (max_len
> len_migr
)
4315 space_needed
+= max_len
- len_migr
;
4316 dev_new
= xmalloc(max_len
);
4317 imsm_copy_dev(dev_new
, dev_iter
);
4320 dv
->next
= super
->devlist
;
4321 super
->devlist
= dv
;
4324 /* ensure that super->buf is large enough when all raid devices
4327 if (__le32_to_cpu(mpb
->mpb_size
) + space_needed
> super
->len
) {
4330 len
= ROUND_UP(__le32_to_cpu(mpb
->mpb_size
) + space_needed
,
4331 super
->sector_size
);
4332 if (posix_memalign(&buf
, MAX_SECTOR_SIZE
, len
) != 0)
4335 memcpy(buf
, super
->buf
, super
->len
);
4336 memset(buf
+ super
->len
, 0, len
- super
->len
);
4342 super
->extra_space
+= space_needed
;
4347 /*******************************************************************************
4348 * Function: check_mpb_migr_compatibility
4349 * Description: Function checks for unsupported migration features:
4350 * - migration optimization area (pba_of_lba0)
4351 * - descending reshape (ascending_migr)
4353 * super : imsm metadata information
4355 * 0 : migration is compatible
4356 * -1 : migration is not compatible
4357 ******************************************************************************/
4358 int check_mpb_migr_compatibility(struct intel_super
*super
)
4360 struct imsm_map
*map0
, *map1
;
4361 struct migr_record
*migr_rec
= super
->migr_rec
;
4364 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
4365 struct imsm_dev
*dev_iter
= __get_imsm_dev(super
->anchor
, i
);
4368 dev_iter
->vol
.migr_state
== 1 &&
4369 dev_iter
->vol
.migr_type
== MIGR_GEN_MIGR
) {
4370 /* This device is migrating */
4371 map0
= get_imsm_map(dev_iter
, MAP_0
);
4372 map1
= get_imsm_map(dev_iter
, MAP_1
);
4373 if (pba_of_lba0(map0
) != pba_of_lba0(map1
))
4374 /* migration optimization area was used */
4376 if (migr_rec
->ascending_migr
== 0 &&
4377 migr_rec
->dest_depth_per_unit
> 0)
4378 /* descending reshape not supported yet */
4385 static void __free_imsm(struct intel_super
*super
, int free_disks
);
4387 /* load_imsm_mpb - read matrix metadata
4388 * allocates super->mpb to be freed by free_imsm
4390 static int load_imsm_mpb(int fd
, struct intel_super
*super
, char *devname
)
4392 unsigned long long dsize
;
4393 unsigned long long sectors
;
4394 unsigned int sector_size
= super
->sector_size
;
4396 struct imsm_super
*anchor
;
4399 get_dev_size(fd
, NULL
, &dsize
);
4400 if (dsize
< 2*sector_size
) {
4402 pr_err("%s: device to small for imsm\n",
4407 if (lseek64(fd
, dsize
- (sector_size
* 2), SEEK_SET
) < 0) {
4409 pr_err("Cannot seek to anchor block on %s: %s\n",
4410 devname
, strerror(errno
));
4414 if (posix_memalign((void **)&anchor
, sector_size
, sector_size
) != 0) {
4416 pr_err("Failed to allocate imsm anchor buffer on %s\n", devname
);
4419 if ((unsigned int)read(fd
, anchor
, sector_size
) != sector_size
) {
4421 pr_err("Cannot read anchor block on %s: %s\n",
4422 devname
, strerror(errno
));
4427 if (strncmp((char *) anchor
->sig
, MPB_SIGNATURE
, MPB_SIG_LEN
) != 0) {
4429 pr_err("no IMSM anchor on %s\n", devname
);
4434 __free_imsm(super
, 0);
4435 /* reload capability and hba */
4437 /* capability and hba must be updated with new super allocation */
4438 find_intel_hba_capability(fd
, super
, devname
);
4439 super
->len
= ROUND_UP(anchor
->mpb_size
, sector_size
);
4440 if (posix_memalign(&super
->buf
, MAX_SECTOR_SIZE
, super
->len
) != 0) {
4442 pr_err("unable to allocate %zu byte mpb buffer\n",
4447 memcpy(super
->buf
, anchor
, sector_size
);
4449 sectors
= mpb_sectors(anchor
, sector_size
) - 1;
4452 if (posix_memalign(&super
->migr_rec_buf
, MAX_SECTOR_SIZE
,
4453 MIGR_REC_BUF_SECTORS
*MAX_SECTOR_SIZE
) != 0) {
4454 pr_err("could not allocate migr_rec buffer\n");
4458 super
->clean_migration_record_by_mdmon
= 0;
4461 check_sum
= __gen_imsm_checksum(super
->anchor
);
4462 if (check_sum
!= __le32_to_cpu(super
->anchor
->check_sum
)) {
4464 pr_err("IMSM checksum %x != %x on %s\n",
4466 __le32_to_cpu(super
->anchor
->check_sum
),
4474 /* read the extended mpb */
4475 if (lseek64(fd
, dsize
- (sector_size
* (2 + sectors
)), SEEK_SET
) < 0) {
4477 pr_err("Cannot seek to extended mpb on %s: %s\n",
4478 devname
, strerror(errno
));
4482 if ((unsigned int)read(fd
, super
->buf
+ sector_size
,
4483 super
->len
- sector_size
) != super
->len
- sector_size
) {
4485 pr_err("Cannot read extended mpb on %s: %s\n",
4486 devname
, strerror(errno
));
4490 check_sum
= __gen_imsm_checksum(super
->anchor
);
4491 if (check_sum
!= __le32_to_cpu(super
->anchor
->check_sum
)) {
4493 pr_err("IMSM checksum %x != %x on %s\n",
4494 check_sum
, __le32_to_cpu(super
->anchor
->check_sum
),
4502 static int read_imsm_migr_rec(int fd
, struct intel_super
*super
);
4504 /* clears hi bits in metadata if MPB_ATTRIB_2TB_DISK not set */
4505 static void clear_hi(struct intel_super
*super
)
4507 struct imsm_super
*mpb
= super
->anchor
;
4509 if (mpb
->attributes
& MPB_ATTRIB_2TB_DISK
)
4511 for (i
= 0; i
< mpb
->num_disks
; ++i
) {
4512 struct imsm_disk
*disk
= &mpb
->disk
[i
];
4513 disk
->total_blocks_hi
= 0;
4515 for (i
= 0; i
< mpb
->num_raid_devs
; ++i
) {
4516 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
4519 for (n
= 0; n
< 2; ++n
) {
4520 struct imsm_map
*map
= get_imsm_map(dev
, n
);
4523 map
->pba_of_lba0_hi
= 0;
4524 map
->blocks_per_member_hi
= 0;
4525 map
->num_data_stripes_hi
= 0;
4531 load_and_parse_mpb(int fd
, struct intel_super
*super
, char *devname
, int keep_fd
)
4535 err
= load_imsm_mpb(fd
, super
, devname
);
4538 if (super
->sector_size
== 4096)
4539 convert_from_4k(super
);
4540 err
= load_imsm_disk(fd
, super
, devname
, keep_fd
);
4543 err
= parse_raid_devices(super
);
4546 err
= load_bbm_log(super
);
4551 static void __free_imsm_disk(struct dl
*d
, int do_close
)
4563 static void free_imsm_disks(struct intel_super
*super
)
4567 while (super
->disks
) {
4569 super
->disks
= d
->next
;
4570 __free_imsm_disk(d
, 1);
4572 while (super
->disk_mgmt_list
) {
4573 d
= super
->disk_mgmt_list
;
4574 super
->disk_mgmt_list
= d
->next
;
4575 __free_imsm_disk(d
, 1);
4577 while (super
->missing
) {
4579 super
->missing
= d
->next
;
4580 __free_imsm_disk(d
, 1);
4585 /* free all the pieces hanging off of a super pointer */
4586 static void __free_imsm(struct intel_super
*super
, int free_disks
)
4588 struct intel_hba
*elem
, *next
;
4594 /* unlink capability description */
4596 if (super
->migr_rec_buf
) {
4597 free(super
->migr_rec_buf
);
4598 super
->migr_rec_buf
= NULL
;
4601 free_imsm_disks(super
);
4602 free_devlist(super
);
4606 free((void *)elem
->path
);
4612 free(super
->bbm_log
);
4616 static void free_imsm(struct intel_super
*super
)
4618 __free_imsm(super
, 1);
4619 free(super
->bb
.entries
);
4623 static void free_super_imsm(struct supertype
*st
)
4625 struct intel_super
*super
= st
->sb
;
4634 static struct intel_super
*alloc_super(void)
4636 struct intel_super
*super
= xcalloc(1, sizeof(*super
));
4638 super
->current_vol
= -1;
4639 super
->create_offset
= ~((unsigned long long) 0);
4641 super
->bb
.entries
= xmalloc(BBM_LOG_MAX_ENTRIES
*
4642 sizeof(struct md_bb_entry
));
4643 if (!super
->bb
.entries
) {
4652 * find and allocate hba and OROM/EFI based on valid fd of RAID component device
4654 static int find_intel_hba_capability(int fd
, struct intel_super
*super
, char *devname
)
4656 struct sys_dev
*hba_name
;
4659 if (is_fd_valid(fd
) && test_partition(fd
)) {
4660 pr_err("imsm: %s is a partition, cannot be used in IMSM\n",
4664 if (!is_fd_valid(fd
) || check_env("IMSM_NO_PLATFORM")) {
4669 hba_name
= find_disk_attached_hba(fd
, NULL
);
4672 pr_err("%s is not attached to Intel(R) RAID controller.\n",
4676 rv
= attach_hba_to_super(super
, hba_name
);
4679 struct intel_hba
*hba
= super
->hba
;
4681 pr_err("%s is attached to Intel(R) %s %s (%s),\n"
4682 " but the container is assigned to Intel(R) %s %s (",
4684 get_sys_dev_type(hba_name
->type
),
4685 hba_name
->type
== SYS_DEV_VMD
? "domain" : "RAID controller",
4686 hba_name
->pci_id
? : "Err!",
4687 get_sys_dev_type(super
->hba
->type
),
4688 hba
->type
== SYS_DEV_VMD
? "domain" : "RAID controller");
4691 fprintf(stderr
, "%s", hba
->pci_id
? : "Err!");
4693 fprintf(stderr
, ", ");
4696 fprintf(stderr
, ").\n"
4697 " Mixing devices attached to different controllers is not allowed.\n");
4701 super
->orom
= find_imsm_capability(hba_name
);
4708 /* find_missing - helper routine for load_super_imsm_all that identifies
4709 * disks that have disappeared from the system. This routine relies on
4710 * the mpb being uptodate, which it is at load time.
4712 static int find_missing(struct intel_super
*super
)
4715 struct imsm_super
*mpb
= super
->anchor
;
4717 struct imsm_disk
*disk
;
4719 for (i
= 0; i
< mpb
->num_disks
; i
++) {
4720 disk
= __get_imsm_disk(mpb
, i
);
4721 dl
= serial_to_dl(disk
->serial
, super
);
4725 dl
= xmalloc(sizeof(*dl
));
4729 dl
->devname
= xstrdup("missing");
4731 serialcpy(dl
->serial
, disk
->serial
);
4734 dl
->next
= super
->missing
;
4735 super
->missing
= dl
;
4741 static struct intel_disk
*disk_list_get(__u8
*serial
, struct intel_disk
*disk_list
)
4743 struct intel_disk
*idisk
= disk_list
;
4746 if (serialcmp(idisk
->disk
.serial
, serial
) == 0)
4748 idisk
= idisk
->next
;
4754 static int __prep_thunderdome(struct intel_super
**table
, int tbl_size
,
4755 struct intel_super
*super
,
4756 struct intel_disk
**disk_list
)
4758 struct imsm_disk
*d
= &super
->disks
->disk
;
4759 struct imsm_super
*mpb
= super
->anchor
;
4762 for (i
= 0; i
< tbl_size
; i
++) {
4763 struct imsm_super
*tbl_mpb
= table
[i
]->anchor
;
4764 struct imsm_disk
*tbl_d
= &table
[i
]->disks
->disk
;
4766 if (tbl_mpb
->family_num
== mpb
->family_num
) {
4767 if (tbl_mpb
->check_sum
== mpb
->check_sum
) {
4768 dprintf("mpb from %d:%d matches %d:%d\n",
4769 super
->disks
->major
,
4770 super
->disks
->minor
,
4771 table
[i
]->disks
->major
,
4772 table
[i
]->disks
->minor
);
4776 if (((is_configured(d
) && !is_configured(tbl_d
)) ||
4777 is_configured(d
) == is_configured(tbl_d
)) &&
4778 tbl_mpb
->generation_num
< mpb
->generation_num
) {
4779 /* current version of the mpb is a
4780 * better candidate than the one in
4781 * super_table, but copy over "cross
4782 * generational" status
4784 struct intel_disk
*idisk
;
4786 dprintf("mpb from %d:%d replaces %d:%d\n",
4787 super
->disks
->major
,
4788 super
->disks
->minor
,
4789 table
[i
]->disks
->major
,
4790 table
[i
]->disks
->minor
);
4792 idisk
= disk_list_get(tbl_d
->serial
, *disk_list
);
4793 if (idisk
&& is_failed(&idisk
->disk
))
4794 tbl_d
->status
|= FAILED_DISK
;
4797 struct intel_disk
*idisk
;
4798 struct imsm_disk
*disk
;
4800 /* tbl_mpb is more up to date, but copy
4801 * over cross generational status before
4804 disk
= __serial_to_disk(d
->serial
, mpb
, NULL
);
4805 if (disk
&& is_failed(disk
))
4806 d
->status
|= FAILED_DISK
;
4808 idisk
= disk_list_get(d
->serial
, *disk_list
);
4811 if (disk
&& is_configured(disk
))
4812 idisk
->disk
.status
|= CONFIGURED_DISK
;
4815 dprintf("mpb from %d:%d prefer %d:%d\n",
4816 super
->disks
->major
,
4817 super
->disks
->minor
,
4818 table
[i
]->disks
->major
,
4819 table
[i
]->disks
->minor
);
4827 table
[tbl_size
++] = super
;
4831 /* update/extend the merged list of imsm_disk records */
4832 for (j
= 0; j
< mpb
->num_disks
; j
++) {
4833 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, j
);
4834 struct intel_disk
*idisk
;
4836 idisk
= disk_list_get(disk
->serial
, *disk_list
);
4838 idisk
->disk
.status
|= disk
->status
;
4839 if (is_configured(&idisk
->disk
) ||
4840 is_failed(&idisk
->disk
))
4841 idisk
->disk
.status
&= ~(SPARE_DISK
);
4843 idisk
= xcalloc(1, sizeof(*idisk
));
4844 idisk
->owner
= IMSM_UNKNOWN_OWNER
;
4845 idisk
->disk
= *disk
;
4846 idisk
->next
= *disk_list
;
4850 if (serialcmp(idisk
->disk
.serial
, d
->serial
) == 0)
4857 static struct intel_super
*
4858 validate_members(struct intel_super
*super
, struct intel_disk
*disk_list
,
4861 struct imsm_super
*mpb
= super
->anchor
;
4865 for (i
= 0; i
< mpb
->num_disks
; i
++) {
4866 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, i
);
4867 struct intel_disk
*idisk
;
4869 idisk
= disk_list_get(disk
->serial
, disk_list
);
4871 if (idisk
->owner
== owner
||
4872 idisk
->owner
== IMSM_UNKNOWN_OWNER
)
4875 dprintf("'%.16s' owner %d != %d\n",
4876 disk
->serial
, idisk
->owner
,
4879 dprintf("unknown disk %x [%d]: %.16s\n",
4880 __le32_to_cpu(mpb
->family_num
), i
,
4886 if (ok_count
== mpb
->num_disks
)
4891 static void show_conflicts(__u32 family_num
, struct intel_super
*super_list
)
4893 struct intel_super
*s
;
4895 for (s
= super_list
; s
; s
= s
->next
) {
4896 if (family_num
!= s
->anchor
->family_num
)
4898 pr_err("Conflict, offlining family %#x on '%s'\n",
4899 __le32_to_cpu(family_num
), s
->disks
->devname
);
4903 static struct intel_super
*
4904 imsm_thunderdome(struct intel_super
**super_list
, int len
)
4906 struct intel_super
*super_table
[len
];
4907 struct intel_disk
*disk_list
= NULL
;
4908 struct intel_super
*champion
, *spare
;
4909 struct intel_super
*s
, **del
;
4914 memset(super_table
, 0, sizeof(super_table
));
4915 for (s
= *super_list
; s
; s
= s
->next
)
4916 tbl_size
= __prep_thunderdome(super_table
, tbl_size
, s
, &disk_list
);
4918 for (i
= 0; i
< tbl_size
; i
++) {
4919 struct imsm_disk
*d
;
4920 struct intel_disk
*idisk
;
4921 struct imsm_super
*mpb
= super_table
[i
]->anchor
;
4924 d
= &s
->disks
->disk
;
4926 /* 'd' must appear in merged disk list for its
4927 * configuration to be valid
4929 idisk
= disk_list_get(d
->serial
, disk_list
);
4930 if (idisk
&& idisk
->owner
== i
)
4931 s
= validate_members(s
, disk_list
, i
);
4936 dprintf("marking family: %#x from %d:%d offline\n",
4938 super_table
[i
]->disks
->major
,
4939 super_table
[i
]->disks
->minor
);
4943 /* This is where the mdadm implementation differs from the Windows
4944 * driver which has no strict concept of a container. We can only
4945 * assemble one family from a container, so when returning a prodigal
4946 * array member to this system the code will not be able to disambiguate
4947 * the container contents that should be assembled ("foreign" versus
4948 * "local"). It requires user intervention to set the orig_family_num
4949 * to a new value to establish a new container. The Windows driver in
4950 * this situation fixes up the volume name in place and manages the
4951 * foreign array as an independent entity.
4956 for (i
= 0; i
< tbl_size
; i
++) {
4957 struct intel_super
*tbl_ent
= super_table
[i
];
4963 if (tbl_ent
->anchor
->num_raid_devs
== 0) {
4968 if (s
&& !is_spare
) {
4969 show_conflicts(tbl_ent
->anchor
->family_num
, *super_list
);
4971 } else if (!s
&& !is_spare
)
4984 pr_err("Chose family %#x on '%s', assemble conflicts to new container with '--update=uuid'\n",
4985 __le32_to_cpu(s
->anchor
->family_num
), s
->disks
->devname
);
4987 /* collect all dl's onto 'champion', and update them to
4988 * champion's version of the status
4990 for (s
= *super_list
; s
; s
= s
->next
) {
4991 struct imsm_super
*mpb
= champion
->anchor
;
4992 struct dl
*dl
= s
->disks
;
4997 mpb
->attributes
|= s
->anchor
->attributes
& MPB_ATTRIB_2TB_DISK
;
4999 for (i
= 0; i
< mpb
->num_disks
; i
++) {
5000 struct imsm_disk
*disk
;
5002 disk
= __serial_to_disk(dl
->serial
, mpb
, &dl
->index
);
5005 /* only set index on disks that are a member of
5006 * a populated contianer, i.e. one with
5009 if (is_failed(&dl
->disk
))
5011 else if (is_spare(&dl
->disk
))
5017 if (i
>= mpb
->num_disks
) {
5018 struct intel_disk
*idisk
;
5020 idisk
= disk_list_get(dl
->serial
, disk_list
);
5021 if (idisk
&& is_spare(&idisk
->disk
) &&
5022 !is_failed(&idisk
->disk
) && !is_configured(&idisk
->disk
))
5030 dl
->next
= champion
->disks
;
5031 champion
->disks
= dl
;
5035 /* delete 'champion' from super_list */
5036 for (del
= super_list
; *del
; ) {
5037 if (*del
== champion
) {
5038 *del
= (*del
)->next
;
5041 del
= &(*del
)->next
;
5043 champion
->next
= NULL
;
5047 struct intel_disk
*idisk
= disk_list
;
5049 disk_list
= disk_list
->next
;
5057 get_sra_super_block(int fd
, struct intel_super
**super_list
, char *devname
, int *max
, int keep_fd
);
5058 static int get_super_block(struct intel_super
**super_list
, char *devnm
, char *devname
,
5059 int major
, int minor
, int keep_fd
);
5061 get_devlist_super_block(struct md_list
*devlist
, struct intel_super
**super_list
,
5062 int *max
, int keep_fd
);
5064 static int load_super_imsm_all(struct supertype
*st
, int fd
, void **sbp
,
5065 char *devname
, struct md_list
*devlist
,
5068 struct intel_super
*super_list
= NULL
;
5069 struct intel_super
*super
= NULL
;
5073 if (is_fd_valid(fd
))
5074 /* 'fd' is an opened container */
5075 err
= get_sra_super_block(fd
, &super_list
, devname
, &i
, keep_fd
);
5077 /* get super block from devlist devices */
5078 err
= get_devlist_super_block(devlist
, &super_list
, &i
, keep_fd
);
5081 /* all mpbs enter, maybe one leaves */
5082 super
= imsm_thunderdome(&super_list
, i
);
5088 if (find_missing(super
) != 0) {
5094 /* load migration record */
5095 err
= load_imsm_migr_rec(super
);
5097 /* migration is in progress,
5098 * but migr_rec cannot be loaded,
5104 /* Check migration compatibility */
5105 if (err
== 0 && check_mpb_migr_compatibility(super
) != 0) {
5106 pr_err("Unsupported migration detected");
5108 fprintf(stderr
, " on %s\n", devname
);
5110 fprintf(stderr
, " (IMSM).\n");
5119 while (super_list
) {
5120 struct intel_super
*s
= super_list
;
5122 super_list
= super_list
->next
;
5130 if (is_fd_valid(fd
))
5131 strcpy(st
->container_devnm
, fd2devnm(fd
));
5133 st
->container_devnm
[0] = 0;
5134 if (err
== 0 && st
->ss
== NULL
) {
5135 st
->ss
= &super_imsm
;
5136 st
->minor_version
= 0;
5137 st
->max_devs
= IMSM_MAX_DEVICES
;
5143 get_devlist_super_block(struct md_list
*devlist
, struct intel_super
**super_list
,
5144 int *max
, int keep_fd
)
5146 struct md_list
*tmpdev
;
5150 for (i
= 0, tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
5151 if (tmpdev
->used
!= 1)
5153 if (tmpdev
->container
== 1) {
5155 int fd
= dev_open(tmpdev
->devname
, O_RDONLY
|O_EXCL
);
5156 if (!is_fd_valid(fd
)) {
5157 pr_err("cannot open device %s: %s\n",
5158 tmpdev
->devname
, strerror(errno
));
5162 err
= get_sra_super_block(fd
, super_list
,
5163 tmpdev
->devname
, &lmax
,
5172 int major
= major(tmpdev
->st_rdev
);
5173 int minor
= minor(tmpdev
->st_rdev
);
5174 err
= get_super_block(super_list
,
5191 static int get_super_block(struct intel_super
**super_list
, char *devnm
, char *devname
,
5192 int major
, int minor
, int keep_fd
)
5194 struct intel_super
*s
;
5206 sprintf(nm
, "%d:%d", major
, minor
);
5207 dfd
= dev_open(nm
, O_RDWR
);
5208 if (!is_fd_valid(dfd
)) {
5213 if (!get_dev_sector_size(dfd
, NULL
, &s
->sector_size
)) {
5217 find_intel_hba_capability(dfd
, s
, devname
);
5218 err
= load_and_parse_mpb(dfd
, s
, NULL
, keep_fd
);
5220 /* retry the load if we might have raced against mdmon */
5221 if (err
== 3 && devnm
&& mdmon_running(devnm
))
5222 for (retry
= 0; retry
< 3; retry
++) {
5224 err
= load_and_parse_mpb(dfd
, s
, NULL
, keep_fd
);
5230 s
->next
= *super_list
;
5244 get_sra_super_block(int fd
, struct intel_super
**super_list
, char *devname
, int *max
, int keep_fd
)
5251 sra
= sysfs_read(fd
, NULL
, GET_LEVEL
|GET_VERSION
|GET_DEVS
|GET_STATE
);
5255 if (sra
->array
.major_version
!= -1 ||
5256 sra
->array
.minor_version
!= -2 ||
5257 strcmp(sra
->text_version
, "imsm") != 0) {
5262 devnm
= fd2devnm(fd
);
5263 for (sd
= sra
->devs
, i
= 0; sd
; sd
= sd
->next
, i
++) {
5264 if (get_super_block(super_list
, devnm
, devname
,
5265 sd
->disk
.major
, sd
->disk
.minor
, keep_fd
) != 0) {
5276 static int load_container_imsm(struct supertype
*st
, int fd
, char *devname
)
5278 return load_super_imsm_all(st
, fd
, &st
->sb
, devname
, NULL
, 1);
5281 static int load_super_imsm(struct supertype
*st
, int fd
, char *devname
)
5283 struct intel_super
*super
;
5287 if (test_partition(fd
))
5288 /* IMSM not allowed on partitions */
5291 free_super_imsm(st
);
5293 super
= alloc_super();
5297 if (!get_dev_sector_size(fd
, NULL
, &super
->sector_size
)) {
5301 /* Load hba and capabilities if they exist.
5302 * But do not preclude loading metadata in case capabilities or hba are
5303 * non-compliant and ignore_hw_compat is set.
5305 rv
= find_intel_hba_capability(fd
, super
, devname
);
5306 /* no orom/efi or non-intel hba of the disk */
5307 if (rv
!= 0 && st
->ignore_hw_compat
== 0) {
5309 pr_err("No OROM/EFI properties for %s\n", devname
);
5313 rv
= load_and_parse_mpb(fd
, super
, devname
, 0);
5315 /* retry the load if we might have raced against mdmon */
5317 struct mdstat_ent
*mdstat
= NULL
;
5318 char *name
= fd2kname(fd
);
5321 mdstat
= mdstat_by_component(name
);
5323 if (mdstat
&& mdmon_running(mdstat
->devnm
) && getpid() != mdmon_pid(mdstat
->devnm
)) {
5324 for (retry
= 0; retry
< 3; retry
++) {
5326 rv
= load_and_parse_mpb(fd
, super
, devname
, 0);
5332 free_mdstat(mdstat
);
5337 pr_err("Failed to load all information sections on %s\n", devname
);
5343 if (st
->ss
== NULL
) {
5344 st
->ss
= &super_imsm
;
5345 st
->minor_version
= 0;
5346 st
->max_devs
= IMSM_MAX_DEVICES
;
5349 /* load migration record */
5350 if (load_imsm_migr_rec(super
) == 0) {
5351 /* Check for unsupported migration features */
5352 if (check_mpb_migr_compatibility(super
) != 0) {
5353 pr_err("Unsupported migration detected");
5355 fprintf(stderr
, " on %s\n", devname
);
5357 fprintf(stderr
, " (IMSM).\n");
5365 static __u16
info_to_blocks_per_strip(mdu_array_info_t
*info
)
5367 if (info
->level
== 1)
5369 return info
->chunk_size
>> 9;
5372 static unsigned long long info_to_blocks_per_member(mdu_array_info_t
*info
,
5373 unsigned long long size
)
5375 if (info
->level
== 1)
5378 return (size
* 2) & ~(info_to_blocks_per_strip(info
) - 1);
5381 static void imsm_update_version_info(struct intel_super
*super
)
5383 /* update the version and attributes */
5384 struct imsm_super
*mpb
= super
->anchor
;
5386 struct imsm_dev
*dev
;
5387 struct imsm_map
*map
;
5390 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
5391 dev
= get_imsm_dev(super
, i
);
5392 map
= get_imsm_map(dev
, MAP_0
);
5393 if (__le32_to_cpu(dev
->size_high
) > 0)
5394 mpb
->attributes
|= MPB_ATTRIB_2TB
;
5396 /* FIXME detect when an array spans a port multiplier */
5398 mpb
->attributes
|= MPB_ATTRIB_PM
;
5401 if (mpb
->num_raid_devs
> 1 ||
5402 mpb
->attributes
!= MPB_ATTRIB_CHECKSUM_VERIFY
) {
5403 version
= MPB_VERSION_ATTRIBS
;
5404 switch (get_imsm_raid_level(map
)) {
5405 case 0: mpb
->attributes
|= MPB_ATTRIB_RAID0
; break;
5406 case 1: mpb
->attributes
|= MPB_ATTRIB_RAID1
; break;
5407 case 10: mpb
->attributes
|= MPB_ATTRIB_RAID10
; break;
5408 case 5: mpb
->attributes
|= MPB_ATTRIB_RAID5
; break;
5411 if (map
->num_members
>= 5)
5412 version
= MPB_VERSION_5OR6_DISK_ARRAY
;
5413 else if (dev
->status
== DEV_CLONE_N_GO
)
5414 version
= MPB_VERSION_CNG
;
5415 else if (get_imsm_raid_level(map
) == 5)
5416 version
= MPB_VERSION_RAID5
;
5417 else if (map
->num_members
>= 3)
5418 version
= MPB_VERSION_3OR4_DISK_ARRAY
;
5419 else if (get_imsm_raid_level(map
) == 1)
5420 version
= MPB_VERSION_RAID1
;
5422 version
= MPB_VERSION_RAID0
;
5424 strcpy(((char *) mpb
->sig
) + strlen(MPB_SIGNATURE
), version
);
5428 static int check_name(struct intel_super
*super
, char *name
, int quiet
)
5430 struct imsm_super
*mpb
= super
->anchor
;
5431 char *reason
= NULL
;
5433 size_t len
= strlen(name
);
5437 while (isspace(start
[len
- 1]))
5439 while (*start
&& isspace(*start
))
5441 memmove(name
, start
, len
+ 1);
5444 if (len
> MAX_RAID_SERIAL_LEN
)
5445 reason
= "must be 16 characters or less";
5447 reason
= "must be a non-empty string";
5449 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
5450 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
5452 if (strncmp((char *) dev
->volume
, name
, MAX_RAID_SERIAL_LEN
) == 0) {
5453 reason
= "already exists";
5458 if (reason
&& !quiet
)
5459 pr_err("imsm volume name %s\n", reason
);
5464 static int init_super_imsm_volume(struct supertype
*st
, mdu_array_info_t
*info
,
5465 struct shape
*s
, char *name
,
5466 char *homehost
, int *uuid
,
5467 long long data_offset
)
5469 /* We are creating a volume inside a pre-existing container.
5470 * so st->sb is already set.
5472 struct intel_super
*super
= st
->sb
;
5473 unsigned int sector_size
= super
->sector_size
;
5474 struct imsm_super
*mpb
= super
->anchor
;
5475 struct intel_dev
*dv
;
5476 struct imsm_dev
*dev
;
5477 struct imsm_vol
*vol
;
5478 struct imsm_map
*map
;
5479 int idx
= mpb
->num_raid_devs
;
5482 unsigned long long array_blocks
;
5483 size_t size_old
, size_new
;
5484 unsigned int data_disks
;
5485 unsigned long long size_per_member
;
5487 if (super
->orom
&& mpb
->num_raid_devs
>= super
->orom
->vpa
) {
5488 pr_err("This imsm-container already has the maximum of %d volumes\n", super
->orom
->vpa
);
5492 /* ensure the mpb is large enough for the new data */
5493 size_old
= __le32_to_cpu(mpb
->mpb_size
);
5494 size_new
= disks_to_mpb_size(info
->nr_disks
);
5495 if (size_new
> size_old
) {
5497 size_t size_round
= ROUND_UP(size_new
, sector_size
);
5499 if (posix_memalign(&mpb_new
, sector_size
, size_round
) != 0) {
5500 pr_err("could not allocate new mpb\n");
5503 if (posix_memalign(&super
->migr_rec_buf
, MAX_SECTOR_SIZE
,
5504 MIGR_REC_BUF_SECTORS
*
5505 MAX_SECTOR_SIZE
) != 0) {
5506 pr_err("could not allocate migr_rec buffer\n");
5512 memcpy(mpb_new
, mpb
, size_old
);
5515 super
->anchor
= mpb_new
;
5516 mpb
->mpb_size
= __cpu_to_le32(size_new
);
5517 memset(mpb_new
+ size_old
, 0, size_round
- size_old
);
5518 super
->len
= size_round
;
5520 super
->current_vol
= idx
;
5522 /* handle 'failed_disks' by either:
5523 * a) create dummy disk entries in the table if this the first
5524 * volume in the array. We add them here as this is the only
5525 * opportunity to add them. add_to_super_imsm_volume()
5526 * handles the non-failed disks and continues incrementing
5528 * b) validate that 'failed_disks' matches the current number
5529 * of missing disks if the container is populated
5531 if (super
->current_vol
== 0) {
5533 for (i
= 0; i
< info
->failed_disks
; i
++) {
5534 struct imsm_disk
*disk
;
5537 disk
= __get_imsm_disk(mpb
, i
);
5538 disk
->status
= CONFIGURED_DISK
| FAILED_DISK
;
5539 disk
->scsi_id
= __cpu_to_le32(~(__u32
)0);
5540 snprintf((char *) disk
->serial
, MAX_RAID_SERIAL_LEN
,
5541 "missing:%d", (__u8
)i
);
5543 find_missing(super
);
5548 for (d
= super
->missing
; d
; d
= d
->next
)
5550 if (info
->failed_disks
> missing
) {
5551 pr_err("unable to add 'missing' disk to container\n");
5556 if (!check_name(super
, name
, 0))
5558 dv
= xmalloc(sizeof(*dv
));
5559 dev
= xcalloc(1, sizeof(*dev
) + sizeof(__u32
) * (info
->raid_disks
- 1));
5561 * Explicitly allow truncating to not confuse gcc's
5562 * -Werror=stringop-truncation
5564 namelen
= min((int) strlen(name
), MAX_RAID_SERIAL_LEN
);
5565 memcpy(dev
->volume
, name
, namelen
);
5566 array_blocks
= calc_array_size(info
->level
, info
->raid_disks
,
5567 info
->layout
, info
->chunk_size
,
5568 s
->size
* BLOCKS_PER_KB
);
5569 data_disks
= get_data_disks(info
->level
, info
->layout
,
5571 array_blocks
= round_size_to_mb(array_blocks
, data_disks
);
5572 size_per_member
= array_blocks
/ data_disks
;
5574 set_imsm_dev_size(dev
, array_blocks
);
5575 dev
->status
= (DEV_READ_COALESCING
| DEV_WRITE_COALESCING
);
5577 vol
->migr_state
= 0;
5578 set_migr_type(dev
, MIGR_INIT
);
5579 vol
->dirty
= !info
->state
;
5580 set_vol_curr_migr_unit(dev
, 0);
5581 map
= get_imsm_map(dev
, MAP_0
);
5582 set_pba_of_lba0(map
, super
->create_offset
);
5583 map
->blocks_per_strip
= __cpu_to_le16(info_to_blocks_per_strip(info
));
5584 map
->failed_disk_num
= ~0;
5585 if (info
->level
> 0)
5586 map
->map_state
= (info
->state
? IMSM_T_STATE_NORMAL
5587 : IMSM_T_STATE_UNINITIALIZED
);
5589 map
->map_state
= info
->failed_disks
? IMSM_T_STATE_FAILED
:
5590 IMSM_T_STATE_NORMAL
;
5593 if (info
->level
== 1 && info
->raid_disks
> 2) {
5596 pr_err("imsm does not support more than 2 disksin a raid1 volume\n");
5600 map
->raid_level
= info
->level
;
5601 if (info
->level
== 10)
5602 map
->raid_level
= 1;
5603 set_num_domains(map
);
5605 size_per_member
+= NUM_BLOCKS_DIRTY_STRIPE_REGION
;
5606 set_blocks_per_member(map
, info_to_blocks_per_member(info
,
5610 map
->num_members
= info
->raid_disks
;
5611 update_num_data_stripes(map
, array_blocks
);
5612 for (i
= 0; i
< map
->num_members
; i
++) {
5613 /* initialized in add_to_super */
5614 set_imsm_ord_tbl_ent(map
, i
, IMSM_ORD_REBUILD
);
5616 mpb
->num_raid_devs
++;
5617 mpb
->num_raid_devs_created
++;
5618 dev
->my_vol_raid_dev_num
= mpb
->num_raid_devs_created
;
5620 if (s
->consistency_policy
<= CONSISTENCY_POLICY_RESYNC
) {
5621 dev
->rwh_policy
= RWH_MULTIPLE_OFF
;
5622 } else if (s
->consistency_policy
== CONSISTENCY_POLICY_PPL
) {
5623 dev
->rwh_policy
= RWH_MULTIPLE_DISTRIBUTED
;
5627 pr_err("imsm does not support consistency policy %s\n",
5628 map_num(consistency_policies
, s
->consistency_policy
));
5633 dv
->index
= super
->current_vol
;
5634 dv
->next
= super
->devlist
;
5635 super
->devlist
= dv
;
5637 imsm_update_version_info(super
);
5642 static int init_super_imsm(struct supertype
*st
, mdu_array_info_t
*info
,
5643 struct shape
*s
, char *name
,
5644 char *homehost
, int *uuid
,
5645 unsigned long long data_offset
)
5647 /* This is primarily called by Create when creating a new array.
5648 * We will then get add_to_super called for each component, and then
5649 * write_init_super called to write it out to each device.
5650 * For IMSM, Create can create on fresh devices or on a pre-existing
5652 * To create on a pre-existing array a different method will be called.
5653 * This one is just for fresh drives.
5655 struct intel_super
*super
;
5656 struct imsm_super
*mpb
;
5660 if (data_offset
!= INVALID_SECTORS
) {
5661 pr_err("data-offset not supported by imsm\n");
5666 return init_super_imsm_volume(st
, info
, s
, name
, homehost
, uuid
,
5670 mpb_size
= disks_to_mpb_size(info
->nr_disks
);
5672 mpb_size
= MAX_SECTOR_SIZE
;
5674 super
= alloc_super();
5676 posix_memalign(&super
->buf
, MAX_SECTOR_SIZE
, mpb_size
) != 0) {
5681 pr_err("could not allocate superblock\n");
5684 if (posix_memalign(&super
->migr_rec_buf
, MAX_SECTOR_SIZE
,
5685 MIGR_REC_BUF_SECTORS
*MAX_SECTOR_SIZE
) != 0) {
5686 pr_err("could not allocate migr_rec buffer\n");
5691 memset(super
->buf
, 0, mpb_size
);
5693 mpb
->mpb_size
= __cpu_to_le32(mpb_size
);
5697 /* zeroing superblock */
5701 mpb
->attributes
= MPB_ATTRIB_CHECKSUM_VERIFY
;
5703 version
= (char *) mpb
->sig
;
5704 strcpy(version
, MPB_SIGNATURE
);
5705 version
+= strlen(MPB_SIGNATURE
);
5706 strcpy(version
, MPB_VERSION_RAID0
);
5711 static int drive_validate_sector_size(struct intel_super
*super
, struct dl
*dl
)
5713 unsigned int member_sector_size
;
5715 if (!is_fd_valid(dl
->fd
)) {
5716 pr_err("Invalid file descriptor for %s\n", dl
->devname
);
5720 if (!get_dev_sector_size(dl
->fd
, dl
->devname
, &member_sector_size
))
5722 if (member_sector_size
!= super
->sector_size
)
5727 static int add_to_super_imsm_volume(struct supertype
*st
, mdu_disk_info_t
*dk
,
5728 int fd
, char *devname
)
5730 struct intel_super
*super
= st
->sb
;
5731 struct imsm_super
*mpb
= super
->anchor
;
5732 struct imsm_disk
*_disk
;
5733 struct imsm_dev
*dev
;
5734 struct imsm_map
*map
;
5738 dev
= get_imsm_dev(super
, super
->current_vol
);
5739 map
= get_imsm_map(dev
, MAP_0
);
5741 if (! (dk
->state
& (1<<MD_DISK_SYNC
))) {
5742 pr_err("%s: Cannot add spare devices to IMSM volume\n",
5747 if (!is_fd_valid(fd
)) {
5748 /* we're doing autolayout so grab the pre-marked (in
5749 * validate_geometry) raid_disk
5751 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
5752 if (dl
->raiddisk
== dk
->raid_disk
)
5755 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
5756 if (dl
->major
== dk
->major
&&
5757 dl
->minor
== dk
->minor
)
5762 pr_err("%s is not a member of the same container\n", devname
);
5766 if (mpb
->num_disks
== 0)
5767 if (!get_dev_sector_size(dl
->fd
, dl
->devname
,
5768 &super
->sector_size
))
5771 if (!drive_validate_sector_size(super
, dl
)) {
5772 pr_err("Combining drives of different sector size in one volume is not allowed\n");
5776 /* add a pristine spare to the metadata */
5777 if (dl
->index
< 0) {
5778 dl
->index
= super
->anchor
->num_disks
;
5779 super
->anchor
->num_disks
++;
5781 /* Check the device has not already been added */
5782 slot
= get_imsm_disk_slot(map
, dl
->index
);
5784 (get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
) & IMSM_ORD_REBUILD
) == 0) {
5785 pr_err("%s has been included in this array twice\n",
5789 set_imsm_ord_tbl_ent(map
, dk
->raid_disk
, dl
->index
);
5790 dl
->disk
.status
= CONFIGURED_DISK
;
5792 /* update size of 'missing' disks to be at least as large as the
5793 * largest acitve member (we only have dummy missing disks when
5794 * creating the first volume)
5796 if (super
->current_vol
== 0) {
5797 for (df
= super
->missing
; df
; df
= df
->next
) {
5798 if (total_blocks(&dl
->disk
) > total_blocks(&df
->disk
))
5799 set_total_blocks(&df
->disk
, total_blocks(&dl
->disk
));
5800 _disk
= __get_imsm_disk(mpb
, df
->index
);
5805 /* refresh unset/failed slots to point to valid 'missing' entries */
5806 for (df
= super
->missing
; df
; df
= df
->next
)
5807 for (slot
= 0; slot
< mpb
->num_disks
; slot
++) {
5808 __u32 ord
= get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
);
5810 if ((ord
& IMSM_ORD_REBUILD
) == 0)
5812 set_imsm_ord_tbl_ent(map
, slot
, df
->index
| IMSM_ORD_REBUILD
);
5813 if (is_gen_migration(dev
)) {
5814 struct imsm_map
*map2
= get_imsm_map(dev
,
5816 int slot2
= get_imsm_disk_slot(map2
, df
->index
);
5817 if (slot2
< map2
->num_members
&& slot2
>= 0) {
5818 __u32 ord2
= get_imsm_ord_tbl_ent(dev
,
5821 if ((unsigned)df
->index
==
5823 set_imsm_ord_tbl_ent(map2
,
5829 dprintf("set slot:%d to missing disk:%d\n", slot
, df
->index
);
5833 /* if we are creating the first raid device update the family number */
5834 if (super
->current_vol
== 0) {
5836 struct imsm_dev
*_dev
= __get_imsm_dev(mpb
, 0);
5838 _disk
= __get_imsm_disk(mpb
, dl
->index
);
5839 if (!_dev
|| !_disk
) {
5840 pr_err("BUG mpb setup error\n");
5846 sum
+= __gen_imsm_checksum(mpb
);
5847 mpb
->family_num
= __cpu_to_le32(sum
);
5848 mpb
->orig_family_num
= mpb
->family_num
;
5849 mpb
->creation_time
= __cpu_to_le64((__u64
)time(NULL
));
5851 super
->current_disk
= dl
;
5856 * Function marks disk as spare and restores disk serial
5857 * in case it was previously marked as failed by takeover operation
5859 * -1 : critical error
5860 * 0 : disk is marked as spare but serial is not set
5863 int mark_spare(struct dl
*disk
)
5865 __u8 serial
[MAX_RAID_SERIAL_LEN
];
5872 if (!imsm_read_serial(disk
->fd
, NULL
, serial
, MAX_RAID_SERIAL_LEN
)) {
5873 /* Restore disk serial number, because takeover marks disk
5874 * as failed and adds to serial ':0' before it becomes
5877 serialcpy(disk
->serial
, serial
);
5878 serialcpy(disk
->disk
.serial
, serial
);
5881 disk
->disk
.status
= SPARE_DISK
;
5888 static int write_super_imsm_spare(struct intel_super
*super
, struct dl
*d
);
5890 static int add_to_super_imsm(struct supertype
*st
, mdu_disk_info_t
*dk
,
5891 int fd
, char *devname
,
5892 unsigned long long data_offset
)
5894 struct intel_super
*super
= st
->sb
;
5896 unsigned long long size
;
5897 unsigned int member_sector_size
;
5902 /* If we are on an RAID enabled platform check that the disk is
5903 * attached to the raid controller.
5904 * We do not need to test disks attachment for container based additions,
5905 * they shall be already tested when container was created/assembled.
5907 rv
= find_intel_hba_capability(fd
, super
, devname
);
5908 /* no orom/efi or non-intel hba of the disk */
5910 dprintf("capability: %p fd: %d ret: %d\n",
5911 super
->orom
, fd
, rv
);
5915 if (super
->current_vol
>= 0)
5916 return add_to_super_imsm_volume(st
, dk
, fd
, devname
);
5919 dd
= xcalloc(sizeof(*dd
), 1);
5920 dd
->major
= major(stb
.st_rdev
);
5921 dd
->minor
= minor(stb
.st_rdev
);
5922 dd
->devname
= devname
? xstrdup(devname
) : NULL
;
5925 dd
->action
= DISK_ADD
;
5926 rv
= imsm_read_serial(fd
, devname
, dd
->serial
, MAX_RAID_SERIAL_LEN
);
5928 pr_err("failed to retrieve scsi serial, aborting\n");
5929 __free_imsm_disk(dd
, 0);
5933 if (super
->hba
&& ((super
->hba
->type
== SYS_DEV_NVME
) ||
5934 (super
->hba
->type
== SYS_DEV_VMD
))) {
5936 char cntrl_path
[PATH_MAX
];
5938 char pci_dev_path
[PATH_MAX
];
5940 if (!diskfd_to_devpath(fd
, 2, pci_dev_path
) ||
5941 !diskfd_to_devpath(fd
, 1, cntrl_path
)) {
5942 pr_err("failed to get dev paths, aborting\n");
5943 __free_imsm_disk(dd
, 0);
5947 cntrl_name
= basename(cntrl_path
);
5948 if (is_multipath_nvme(fd
))
5949 pr_err("%s controller supports Multi-Path I/O, Intel (R) VROC does not support multipathing\n",
5952 if (devpath_to_vendor(pci_dev_path
) == 0x8086) {
5954 * If Intel's NVMe drive has serial ended with
5955 * "-A","-B","-1" or "-2" it means that this is "x8"
5956 * device (double drive on single PCIe card).
5957 * User should be warned about potential data loss.
5959 for (i
= MAX_RAID_SERIAL_LEN
-1; i
> 0; i
--) {
5960 /* Skip empty character at the end */
5961 if (dd
->serial
[i
] == 0)
5964 if (((dd
->serial
[i
] == 'A') ||
5965 (dd
->serial
[i
] == 'B') ||
5966 (dd
->serial
[i
] == '1') ||
5967 (dd
->serial
[i
] == '2')) &&
5968 (dd
->serial
[i
-1] == '-'))
5969 pr_err("\tThe action you are about to take may put your data at risk.\n"
5970 "\tPlease note that x8 devices may consist of two separate x4 devices "
5971 "located on a single PCIe port.\n"
5972 "\tRAID 0 is the only supported configuration for this type of x8 device.\n");
5975 } else if (super
->hba
->type
== SYS_DEV_VMD
&& super
->orom
&&
5976 !imsm_orom_has_tpv_support(super
->orom
)) {
5977 pr_err("\tPlatform configuration does not support non-Intel NVMe drives.\n"
5978 "\tPlease refer to Intel(R) RSTe/VROC user guide.\n");
5979 __free_imsm_disk(dd
, 0);
5984 get_dev_size(fd
, NULL
, &size
);
5985 if (!get_dev_sector_size(fd
, NULL
, &member_sector_size
)) {
5986 __free_imsm_disk(dd
, 0);
5990 if (super
->sector_size
== 0) {
5991 /* this a first device, so sector_size is not set yet */
5992 super
->sector_size
= member_sector_size
;
5995 /* clear migr_rec when adding disk to container */
5996 memset(super
->migr_rec_buf
, 0, MIGR_REC_BUF_SECTORS
*MAX_SECTOR_SIZE
);
5997 if (lseek64(fd
, size
- MIGR_REC_SECTOR_POSITION
*member_sector_size
,
5999 if ((unsigned int)write(fd
, super
->migr_rec_buf
,
6000 MIGR_REC_BUF_SECTORS
*member_sector_size
) !=
6001 MIGR_REC_BUF_SECTORS
*member_sector_size
)
6002 perror("Write migr_rec failed");
6006 serialcpy(dd
->disk
.serial
, dd
->serial
);
6007 set_total_blocks(&dd
->disk
, size
);
6008 if (__le32_to_cpu(dd
->disk
.total_blocks_hi
) > 0) {
6009 struct imsm_super
*mpb
= super
->anchor
;
6010 mpb
->attributes
|= MPB_ATTRIB_2TB_DISK
;
6013 if (sysfs_disk_to_scsi_id(fd
, &id
) == 0)
6014 dd
->disk
.scsi_id
= __cpu_to_le32(id
);
6016 dd
->disk
.scsi_id
= __cpu_to_le32(0);
6018 if (st
->update_tail
) {
6019 dd
->next
= super
->disk_mgmt_list
;
6020 super
->disk_mgmt_list
= dd
;
6022 /* this is called outside of mdmon
6023 * write initial spare metadata
6024 * mdmon will overwrite it.
6026 dd
->next
= super
->disks
;
6028 write_super_imsm_spare(super
, dd
);
6034 static int remove_from_super_imsm(struct supertype
*st
, mdu_disk_info_t
*dk
)
6036 struct intel_super
*super
= st
->sb
;
6039 /* remove from super works only in mdmon - for communication
6040 * manager - monitor. Check if communication memory buffer
6043 if (!st
->update_tail
) {
6044 pr_err("shall be used in mdmon context only\n");
6047 dd
= xcalloc(1, sizeof(*dd
));
6048 dd
->major
= dk
->major
;
6049 dd
->minor
= dk
->minor
;
6052 dd
->action
= DISK_REMOVE
;
6054 dd
->next
= super
->disk_mgmt_list
;
6055 super
->disk_mgmt_list
= dd
;
6060 static int store_imsm_mpb(int fd
, struct imsm_super
*mpb
);
6063 char buf
[MAX_SECTOR_SIZE
];
6064 struct imsm_super anchor
;
6065 } spare_record
__attribute__ ((aligned(MAX_SECTOR_SIZE
)));
6068 static int write_super_imsm_spare(struct intel_super
*super
, struct dl
*d
)
6070 struct imsm_super
*mpb
= super
->anchor
;
6071 struct imsm_super
*spare
= &spare_record
.anchor
;
6077 spare
->mpb_size
= __cpu_to_le32(sizeof(struct imsm_super
));
6078 spare
->generation_num
= __cpu_to_le32(1UL);
6079 spare
->attributes
= MPB_ATTRIB_CHECKSUM_VERIFY
;
6080 spare
->num_disks
= 1;
6081 spare
->num_raid_devs
= 0;
6082 spare
->cache_size
= mpb
->cache_size
;
6083 spare
->pwr_cycle_count
= __cpu_to_le32(1);
6085 snprintf((char *) spare
->sig
, MAX_SIGNATURE_LENGTH
,
6086 MPB_SIGNATURE MPB_VERSION_RAID0
);
6088 spare
->disk
[0] = d
->disk
;
6089 if (__le32_to_cpu(d
->disk
.total_blocks_hi
) > 0)
6090 spare
->attributes
|= MPB_ATTRIB_2TB_DISK
;
6092 if (super
->sector_size
== 4096)
6093 convert_to_4k_imsm_disk(&spare
->disk
[0]);
6095 sum
= __gen_imsm_checksum(spare
);
6096 spare
->family_num
= __cpu_to_le32(sum
);
6097 spare
->orig_family_num
= 0;
6098 sum
= __gen_imsm_checksum(spare
);
6099 spare
->check_sum
= __cpu_to_le32(sum
);
6101 if (store_imsm_mpb(d
->fd
, spare
)) {
6102 pr_err("failed for device %d:%d %s\n",
6103 d
->major
, d
->minor
, strerror(errno
));
6109 /* spare records have their own family number and do not have any defined raid
6112 static int write_super_imsm_spares(struct intel_super
*super
, int doclose
)
6116 for (d
= super
->disks
; d
; d
= d
->next
) {
6120 if (write_super_imsm_spare(super
, d
))
6130 static int write_super_imsm(struct supertype
*st
, int doclose
)
6132 struct intel_super
*super
= st
->sb
;
6133 unsigned int sector_size
= super
->sector_size
;
6134 struct imsm_super
*mpb
= super
->anchor
;
6140 __u32 mpb_size
= sizeof(struct imsm_super
) - sizeof(struct imsm_disk
);
6142 int clear_migration_record
= 1;
6145 /* 'generation' is incremented everytime the metadata is written */
6146 generation
= __le32_to_cpu(mpb
->generation_num
);
6148 mpb
->generation_num
= __cpu_to_le32(generation
);
6150 /* fix up cases where previous mdadm releases failed to set
6153 if (mpb
->orig_family_num
== 0)
6154 mpb
->orig_family_num
= mpb
->family_num
;
6156 for (d
= super
->disks
; d
; d
= d
->next
) {
6160 mpb
->disk
[d
->index
] = d
->disk
;
6164 for (d
= super
->missing
; d
; d
= d
->next
) {
6165 mpb
->disk
[d
->index
] = d
->disk
;
6168 mpb
->num_disks
= num_disks
;
6169 mpb_size
+= sizeof(struct imsm_disk
) * mpb
->num_disks
;
6171 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
6172 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
6173 struct imsm_dev
*dev2
= get_imsm_dev(super
, i
);
6175 imsm_copy_dev(dev
, dev2
);
6176 mpb_size
+= sizeof_imsm_dev(dev
, 0);
6178 if (is_gen_migration(dev2
))
6179 clear_migration_record
= 0;
6182 bbm_log_size
= get_imsm_bbm_log_size(super
->bbm_log
);
6185 memcpy((void *)mpb
+ mpb_size
, super
->bbm_log
, bbm_log_size
);
6186 mpb
->attributes
|= MPB_ATTRIB_BBM
;
6188 mpb
->attributes
&= ~MPB_ATTRIB_BBM
;
6190 super
->anchor
->bbm_log_size
= __cpu_to_le32(bbm_log_size
);
6191 mpb_size
+= bbm_log_size
;
6192 mpb
->mpb_size
= __cpu_to_le32(mpb_size
);
6195 assert(super
->len
== 0 || mpb_size
<= super
->len
);
6198 /* recalculate checksum */
6199 sum
= __gen_imsm_checksum(mpb
);
6200 mpb
->check_sum
= __cpu_to_le32(sum
);
6202 if (super
->clean_migration_record_by_mdmon
) {
6203 clear_migration_record
= 1;
6204 super
->clean_migration_record_by_mdmon
= 0;
6206 if (clear_migration_record
)
6207 memset(super
->migr_rec_buf
, 0,
6208 MIGR_REC_BUF_SECTORS
*MAX_SECTOR_SIZE
);
6210 if (sector_size
== 4096)
6211 convert_to_4k(super
);
6213 /* write the mpb for disks that compose raid devices */
6214 for (d
= super
->disks
; d
; d
= d
->next
) {
6215 if (d
->index
< 0 || is_failed(&d
->disk
))
6218 if (clear_migration_record
) {
6219 unsigned long long dsize
;
6221 get_dev_size(d
->fd
, NULL
, &dsize
);
6222 if (lseek64(d
->fd
, dsize
- sector_size
,
6224 if ((unsigned int)write(d
->fd
,
6225 super
->migr_rec_buf
,
6226 MIGR_REC_BUF_SECTORS
*sector_size
) !=
6227 MIGR_REC_BUF_SECTORS
*sector_size
)
6228 perror("Write migr_rec failed");
6232 if (store_imsm_mpb(d
->fd
, mpb
))
6234 "failed for device %d:%d (fd: %d)%s\n",
6236 d
->fd
, strerror(errno
));
6243 return write_super_imsm_spares(super
, doclose
);
6248 static int create_array(struct supertype
*st
, int dev_idx
)
6251 struct imsm_update_create_array
*u
;
6252 struct intel_super
*super
= st
->sb
;
6253 struct imsm_dev
*dev
= get_imsm_dev(super
, dev_idx
);
6254 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
6255 struct disk_info
*inf
;
6256 struct imsm_disk
*disk
;
6259 len
= sizeof(*u
) - sizeof(*dev
) + sizeof_imsm_dev(dev
, 0) +
6260 sizeof(*inf
) * map
->num_members
;
6262 u
->type
= update_create_array
;
6263 u
->dev_idx
= dev_idx
;
6264 imsm_copy_dev(&u
->dev
, dev
);
6265 inf
= get_disk_info(u
);
6266 for (i
= 0; i
< map
->num_members
; i
++) {
6267 int idx
= get_imsm_disk_idx(dev
, i
, MAP_X
);
6269 disk
= get_imsm_disk(super
, idx
);
6271 disk
= get_imsm_missing(super
, idx
);
6272 serialcpy(inf
[i
].serial
, disk
->serial
);
6274 append_metadata_update(st
, u
, len
);
6279 static int mgmt_disk(struct supertype
*st
)
6281 struct intel_super
*super
= st
->sb
;
6283 struct imsm_update_add_remove_disk
*u
;
6285 if (!super
->disk_mgmt_list
)
6290 u
->type
= update_add_remove_disk
;
6291 append_metadata_update(st
, u
, len
);
6296 __u32
crc32c_le(__u32 crc
, unsigned char const *p
, size_t len
);
6298 static int write_ppl_header(unsigned long long ppl_sector
, int fd
, void *buf
)
6300 struct ppl_header
*ppl_hdr
= buf
;
6303 ppl_hdr
->checksum
= __cpu_to_le32(~crc32c_le(~0, buf
, PPL_HEADER_SIZE
));
6305 if (lseek64(fd
, ppl_sector
* 512, SEEK_SET
) < 0) {
6307 perror("Failed to seek to PPL header location");
6311 if (write(fd
, buf
, PPL_HEADER_SIZE
) != PPL_HEADER_SIZE
) {
6313 perror("Write PPL header failed");
6322 static int write_init_ppl_imsm(struct supertype
*st
, struct mdinfo
*info
, int fd
)
6324 struct intel_super
*super
= st
->sb
;
6326 struct ppl_header
*ppl_hdr
;
6329 /* first clear entire ppl space */
6330 ret
= zero_disk_range(fd
, info
->ppl_sector
, info
->ppl_size
);
6334 ret
= posix_memalign(&buf
, MAX_SECTOR_SIZE
, PPL_HEADER_SIZE
);
6336 pr_err("Failed to allocate PPL header buffer\n");
6340 memset(buf
, 0, PPL_HEADER_SIZE
);
6342 memset(ppl_hdr
->reserved
, 0xff, PPL_HDR_RESERVED
);
6343 ppl_hdr
->signature
= __cpu_to_le32(super
->anchor
->orig_family_num
);
6345 if (info
->mismatch_cnt
) {
6347 * We are overwriting an invalid ppl. Make one entry with wrong
6348 * checksum to prevent the kernel from skipping resync.
6350 ppl_hdr
->entries_count
= __cpu_to_le32(1);
6351 ppl_hdr
->entries
[0].checksum
= ~0;
6354 ret
= write_ppl_header(info
->ppl_sector
, fd
, buf
);
6360 static int is_rebuilding(struct imsm_dev
*dev
);
6362 static int validate_ppl_imsm(struct supertype
*st
, struct mdinfo
*info
,
6363 struct mdinfo
*disk
)
6365 struct intel_super
*super
= st
->sb
;
6367 void *buf_orig
, *buf
, *buf_prev
= NULL
;
6369 struct ppl_header
*ppl_hdr
= NULL
;
6371 struct imsm_dev
*dev
;
6374 unsigned long long ppl_offset
= 0;
6375 unsigned long long prev_gen_num
= 0;
6377 if (disk
->disk
.raid_disk
< 0)
6380 dev
= get_imsm_dev(super
, info
->container_member
);
6381 idx
= get_imsm_disk_idx(dev
, disk
->disk
.raid_disk
, MAP_0
);
6382 d
= get_imsm_dl_disk(super
, idx
);
6384 if (!d
|| d
->index
< 0 || is_failed(&d
->disk
))
6387 if (posix_memalign(&buf_orig
, MAX_SECTOR_SIZE
, PPL_HEADER_SIZE
* 2)) {
6388 pr_err("Failed to allocate PPL header buffer\n");
6394 while (ppl_offset
< MULTIPLE_PPL_AREA_SIZE_IMSM
) {
6397 dprintf("Checking potential PPL at offset: %llu\n", ppl_offset
);
6399 if (lseek64(d
->fd
, info
->ppl_sector
* 512 + ppl_offset
,
6401 perror("Failed to seek to PPL header location");
6406 if (read(d
->fd
, buf
, PPL_HEADER_SIZE
) != PPL_HEADER_SIZE
) {
6407 perror("Read PPL header failed");
6414 crc
= __le32_to_cpu(ppl_hdr
->checksum
);
6415 ppl_hdr
->checksum
= 0;
6417 if (crc
!= ~crc32c_le(~0, buf
, PPL_HEADER_SIZE
)) {
6418 dprintf("Wrong PPL header checksum on %s\n",
6423 if (prev_gen_num
> __le64_to_cpu(ppl_hdr
->generation
)) {
6424 /* previous was newest, it was already checked */
6428 if ((__le32_to_cpu(ppl_hdr
->signature
) !=
6429 super
->anchor
->orig_family_num
)) {
6430 dprintf("Wrong PPL header signature on %s\n",
6437 prev_gen_num
= __le64_to_cpu(ppl_hdr
->generation
);
6439 ppl_offset
+= PPL_HEADER_SIZE
;
6440 for (i
= 0; i
< __le32_to_cpu(ppl_hdr
->entries_count
); i
++)
6442 __le32_to_cpu(ppl_hdr
->entries
[i
].pp_size
);
6445 buf_prev
= buf
+ PPL_HEADER_SIZE
;
6457 * Update metadata to use mutliple PPLs area (1MB).
6458 * This is done once for all RAID members
6460 if (info
->consistency_policy
== CONSISTENCY_POLICY_PPL
&&
6461 info
->ppl_size
!= (MULTIPLE_PPL_AREA_SIZE_IMSM
>> 9)) {
6463 struct mdinfo
*member_dev
;
6465 sprintf(subarray
, "%d", info
->container_member
);
6467 if (mdmon_running(st
->container_devnm
))
6468 st
->update_tail
= &st
->updates
;
6470 if (st
->ss
->update_subarray(st
, subarray
, "ppl", NULL
)) {
6471 pr_err("Failed to update subarray %s\n",
6474 if (st
->update_tail
)
6475 flush_metadata_updates(st
);
6477 st
->ss
->sync_metadata(st
);
6478 info
->ppl_size
= (MULTIPLE_PPL_AREA_SIZE_IMSM
>> 9);
6479 for (member_dev
= info
->devs
; member_dev
;
6480 member_dev
= member_dev
->next
)
6481 member_dev
->ppl_size
=
6482 (MULTIPLE_PPL_AREA_SIZE_IMSM
>> 9);
6487 struct imsm_map
*map
= get_imsm_map(dev
, MAP_X
);
6489 if (map
->map_state
== IMSM_T_STATE_UNINITIALIZED
||
6490 (map
->map_state
== IMSM_T_STATE_NORMAL
&&
6491 !(dev
->vol
.dirty
& RAIDVOL_DIRTY
)) ||
6492 (is_rebuilding(dev
) &&
6493 vol_curr_migr_unit(dev
) == 0 &&
6494 get_imsm_disk_idx(dev
, disk
->disk
.raid_disk
, MAP_1
) != idx
))
6495 ret
= st
->ss
->write_init_ppl(st
, info
, d
->fd
);
6497 info
->mismatch_cnt
++;
6498 } else if (ret
== 0 &&
6499 ppl_hdr
->entries_count
== 0 &&
6500 is_rebuilding(dev
) &&
6501 info
->resync_start
== 0) {
6503 * The header has no entries - add a single empty entry and
6504 * rewrite the header to prevent the kernel from going into
6505 * resync after an interrupted rebuild.
6507 ppl_hdr
->entries_count
= __cpu_to_le32(1);
6508 ret
= write_ppl_header(info
->ppl_sector
, d
->fd
, buf
);
6516 static int write_init_ppl_imsm_all(struct supertype
*st
, struct mdinfo
*info
)
6518 struct intel_super
*super
= st
->sb
;
6522 if (info
->consistency_policy
!= CONSISTENCY_POLICY_PPL
||
6523 info
->array
.level
!= 5)
6526 for (d
= super
->disks
; d
; d
= d
->next
) {
6527 if (d
->index
< 0 || is_failed(&d
->disk
))
6530 ret
= st
->ss
->write_init_ppl(st
, info
, d
->fd
);
6538 /*******************************************************************************
6539 * Function: write_init_bitmap_imsm_vol
6540 * Description: Write a bitmap header and prepares the area for the bitmap.
6542 * st : supertype information
6543 * vol_idx : the volume index to use
6548 ******************************************************************************/
6549 static int write_init_bitmap_imsm_vol(struct supertype
*st
, int vol_idx
)
6551 struct intel_super
*super
= st
->sb
;
6552 int prev_current_vol
= super
->current_vol
;
6556 super
->current_vol
= vol_idx
;
6557 for (d
= super
->disks
; d
; d
= d
->next
) {
6558 if (d
->index
< 0 || is_failed(&d
->disk
))
6560 ret
= st
->ss
->write_bitmap(st
, d
->fd
, NoUpdate
);
6564 super
->current_vol
= prev_current_vol
;
6568 /*******************************************************************************
6569 * Function: write_init_bitmap_imsm_all
6570 * Description: Write a bitmap header and prepares the area for the bitmap.
6571 * Operation is executed for volumes with CONSISTENCY_POLICY_BITMAP.
6573 * st : supertype information
6574 * info : info about the volume where the bitmap should be written
6575 * vol_idx : the volume index to use
6580 ******************************************************************************/
6581 static int write_init_bitmap_imsm_all(struct supertype
*st
, struct mdinfo
*info
,
6586 if (info
&& (info
->consistency_policy
== CONSISTENCY_POLICY_BITMAP
))
6587 ret
= write_init_bitmap_imsm_vol(st
, vol_idx
);
6592 static int write_init_super_imsm(struct supertype
*st
)
6594 struct intel_super
*super
= st
->sb
;
6595 int current_vol
= super
->current_vol
;
6599 getinfo_super_imsm(st
, &info
, NULL
);
6601 /* we are done with current_vol reset it to point st at the container */
6602 super
->current_vol
= -1;
6604 if (st
->update_tail
) {
6605 /* queue the recently created array / added disk
6606 * as a metadata update */
6608 /* determine if we are creating a volume or adding a disk */
6609 if (current_vol
< 0) {
6610 /* in the mgmt (add/remove) disk case we are running
6611 * in mdmon context, so don't close fd's
6615 /* adding the second volume to the array */
6616 rv
= write_init_ppl_imsm_all(st
, &info
);
6618 rv
= write_init_bitmap_imsm_all(st
, &info
, current_vol
);
6620 rv
= create_array(st
, current_vol
);
6624 for (d
= super
->disks
; d
; d
= d
->next
)
6625 Kill(d
->devname
, NULL
, 0, -1, 1);
6626 if (current_vol
>= 0) {
6627 rv
= write_init_ppl_imsm_all(st
, &info
);
6629 rv
= write_init_bitmap_imsm_all(st
, &info
, current_vol
);
6633 rv
= write_super_imsm(st
, 1);
6639 static int store_super_imsm(struct supertype
*st
, int fd
)
6641 struct intel_super
*super
= st
->sb
;
6642 struct imsm_super
*mpb
= super
? super
->anchor
: NULL
;
6647 if (super
->sector_size
== 4096)
6648 convert_to_4k(super
);
6649 return store_imsm_mpb(fd
, mpb
);
6652 static int validate_geometry_imsm_container(struct supertype
*st
, int level
,
6654 unsigned long long data_offset
,
6656 unsigned long long *freesize
,
6660 unsigned long long ldsize
;
6661 struct intel_super
*super
= NULL
;
6664 if (level
!= LEVEL_CONTAINER
)
6669 fd
= dev_open(dev
, O_RDONLY
|O_EXCL
);
6670 if (!is_fd_valid(fd
)) {
6671 pr_vrb("imsm: Cannot open %s: %s\n", dev
, strerror(errno
));
6674 if (!get_dev_size(fd
, dev
, &ldsize
))
6677 /* capabilities retrieve could be possible
6678 * note that there is no fd for the disks in array.
6680 super
= alloc_super();
6684 if (!get_dev_sector_size(fd
, NULL
, &super
->sector_size
))
6687 rv
= find_intel_hba_capability(fd
, super
, verbose
> 0 ? dev
: NULL
);
6691 fd2devname(fd
, str
);
6692 dprintf("fd: %d %s orom: %p rv: %d raiddisk: %d\n",
6693 fd
, str
, super
->orom
, rv
, raiddisks
);
6695 /* no orom/efi or non-intel hba of the disk */
6700 if (raiddisks
> super
->orom
->tds
) {
6702 pr_err("%d exceeds maximum number of platform supported disks: %d\n",
6703 raiddisks
, super
->orom
->tds
);
6706 if ((super
->orom
->attr
& IMSM_OROM_ATTR_2TB_DISK
) == 0 &&
6707 (ldsize
>> 9) >> 32 > 0) {
6709 pr_err("%s exceeds maximum platform supported size\n", dev
);
6713 if (super
->hba
->type
== SYS_DEV_VMD
||
6714 super
->hba
->type
== SYS_DEV_NVME
) {
6715 if (!imsm_is_nvme_namespace_supported(fd
, 1)) {
6717 pr_err("NVMe namespace %s is not supported by IMSM\n",
6724 *freesize
= avail_size_imsm(st
, ldsize
>> 9, data_offset
);
6734 static unsigned long long find_size(struct extent
*e
, int *idx
, int num_extents
)
6736 const unsigned long long base_start
= e
[*idx
].start
;
6737 unsigned long long end
= base_start
+ e
[*idx
].size
;
6740 if (base_start
== end
)
6744 for (i
= *idx
; i
< num_extents
; i
++) {
6745 /* extend overlapping extents */
6746 if (e
[i
].start
>= base_start
&&
6747 e
[i
].start
<= end
) {
6750 if (e
[i
].start
+ e
[i
].size
> end
)
6751 end
= e
[i
].start
+ e
[i
].size
;
6752 } else if (e
[i
].start
> end
) {
6758 return end
- base_start
;
6761 static unsigned long long merge_extents(struct intel_super
*super
, int sum_extents
)
6763 /* build a composite disk with all known extents and generate a new
6764 * 'maxsize' given the "all disks in an array must share a common start
6765 * offset" constraint
6767 struct extent
*e
= xcalloc(sum_extents
, sizeof(*e
));
6771 unsigned long long pos
;
6772 unsigned long long start
= 0;
6773 unsigned long long maxsize
;
6774 unsigned long reserve
;
6776 /* coalesce and sort all extents. also, check to see if we need to
6777 * reserve space between member arrays
6780 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
6783 for (i
= 0; i
< dl
->extent_cnt
; i
++)
6786 qsort(e
, sum_extents
, sizeof(*e
), cmp_extent
);
6791 while (i
< sum_extents
) {
6792 e
[j
].start
= e
[i
].start
;
6793 e
[j
].size
= find_size(e
, &i
, sum_extents
);
6795 if (e
[j
-1].size
== 0)
6804 unsigned long long esize
;
6806 esize
= e
[i
].start
- pos
;
6807 if (esize
>= maxsize
) {
6812 pos
= e
[i
].start
+ e
[i
].size
;
6814 } while (e
[i
-1].size
);
6820 /* FIXME assumes volume at offset 0 is the first volume in a
6823 if (start_extent
> 0)
6824 reserve
= IMSM_RESERVED_SECTORS
; /* gap between raid regions */
6828 if (maxsize
< reserve
)
6831 super
->create_offset
= ~((unsigned long long) 0);
6832 if (start
+ reserve
> super
->create_offset
)
6833 return 0; /* start overflows create_offset */
6834 super
->create_offset
= start
+ reserve
;
6836 return maxsize
- reserve
;
6839 static int is_raid_level_supported(const struct imsm_orom
*orom
, int level
, int raiddisks
)
6841 if (level
< 0 || level
== 6 || level
== 4)
6844 /* if we have an orom prevent invalid raid levels */
6847 case 0: return imsm_orom_has_raid0(orom
);
6850 return imsm_orom_has_raid1e(orom
);
6851 return imsm_orom_has_raid1(orom
) && raiddisks
== 2;
6852 case 10: return imsm_orom_has_raid10(orom
) && raiddisks
== 4;
6853 case 5: return imsm_orom_has_raid5(orom
) && raiddisks
> 2;
6856 return 1; /* not on an Intel RAID platform so anything goes */
6862 active_arrays_by_format(char *name
, char* hba
, struct md_list
**devlist
,
6863 int dpa
, int verbose
)
6865 struct mdstat_ent
*mdstat
= mdstat_read(0, 0);
6866 struct mdstat_ent
*memb
;
6872 for (memb
= mdstat
; memb
; memb
= memb
->next
) {
6873 if (memb
->metadata_version
&&
6874 (strncmp(memb
->metadata_version
, "external:", 9) == 0) &&
6875 (strcmp(&memb
->metadata_version
[9], name
) == 0) &&
6876 !is_subarray(memb
->metadata_version
+9) &&
6878 struct dev_member
*dev
= memb
->members
;
6880 while (dev
&& !is_fd_valid(fd
)) {
6881 char *path
= xmalloc(strlen(dev
->name
) + strlen("/dev/") + 1);
6882 num
= sprintf(path
, "%s%s", "/dev/", dev
->name
);
6884 fd
= open(path
, O_RDONLY
, 0);
6885 if (num
<= 0 || !is_fd_valid(fd
)) {
6886 pr_vrb("Cannot open %s: %s\n",
6887 dev
->name
, strerror(errno
));
6893 if (is_fd_valid(fd
) && disk_attached_to_hba(fd
, hba
)) {
6894 struct mdstat_ent
*vol
;
6895 for (vol
= mdstat
; vol
; vol
= vol
->next
) {
6896 if (vol
->active
> 0 &&
6897 vol
->metadata_version
&&
6898 is_container_member(vol
, memb
->devnm
)) {
6903 if (*devlist
&& (found
< dpa
)) {
6904 dv
= xcalloc(1, sizeof(*dv
));
6905 dv
->devname
= xmalloc(strlen(memb
->devnm
) + strlen("/dev/") + 1);
6906 sprintf(dv
->devname
, "%s%s", "/dev/", memb
->devnm
);
6909 dv
->next
= *devlist
;
6916 free_mdstat(mdstat
);
6921 static struct md_list
*
6922 get_loop_devices(void)
6925 struct md_list
*devlist
= NULL
;
6928 for(i
= 0; i
< 12; i
++) {
6929 dv
= xcalloc(1, sizeof(*dv
));
6930 dv
->devname
= xmalloc(40);
6931 sprintf(dv
->devname
, "/dev/loop%d", i
);
6939 static struct md_list
*
6940 get_devices(const char *hba_path
)
6942 struct md_list
*devlist
= NULL
;
6949 devlist
= get_loop_devices();
6952 /* scroll through /sys/dev/block looking for devices attached to
6955 dir
= opendir("/sys/dev/block");
6956 for (ent
= dir
? readdir(dir
) : NULL
; ent
; ent
= readdir(dir
)) {
6961 if (sscanf(ent
->d_name
, "%d:%d", &major
, &minor
) != 2)
6963 path
= devt_to_devpath(makedev(major
, minor
), 1, NULL
);
6966 if (!path_attached_to_hba(path
, hba_path
)) {
6973 fd
= dev_open(ent
->d_name
, O_RDONLY
);
6974 if (is_fd_valid(fd
)) {
6975 fd2devname(fd
, buf
);
6978 pr_err("cannot open device: %s\n",
6983 dv
= xcalloc(1, sizeof(*dv
));
6984 dv
->devname
= xstrdup(buf
);
6991 devlist
= devlist
->next
;
7001 count_volumes_list(struct md_list
*devlist
, char *homehost
,
7002 int verbose
, int *found
)
7004 struct md_list
*tmpdev
;
7006 struct supertype
*st
;
7008 /* first walk the list of devices to find a consistent set
7009 * that match the criterea, if that is possible.
7010 * We flag the ones we like with 'used'.
7013 st
= match_metadata_desc_imsm("imsm");
7015 pr_vrb("cannot allocate memory for imsm supertype\n");
7019 for (tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
7020 char *devname
= tmpdev
->devname
;
7022 struct supertype
*tst
;
7024 if (tmpdev
->used
> 1)
7026 tst
= dup_super(st
);
7028 pr_vrb("cannot allocate memory for imsm supertype\n");
7031 tmpdev
->container
= 0;
7032 dfd
= dev_open(devname
, O_RDONLY
|O_EXCL
);
7033 if (!is_fd_valid(dfd
)) {
7034 dprintf("cannot open device %s: %s\n",
7035 devname
, strerror(errno
));
7037 } else if (!fstat_is_blkdev(dfd
, devname
, &rdev
)) {
7039 } else if (must_be_container(dfd
)) {
7040 struct supertype
*cst
;
7041 cst
= super_by_fd(dfd
, NULL
);
7043 dprintf("cannot recognize container type %s\n",
7046 } else if (tst
->ss
!= st
->ss
) {
7047 dprintf("non-imsm container - ignore it: %s\n",
7050 } else if (!tst
->ss
->load_container
||
7051 tst
->ss
->load_container(tst
, dfd
, NULL
))
7054 tmpdev
->container
= 1;
7057 cst
->ss
->free_super(cst
);
7059 tmpdev
->st_rdev
= rdev
;
7060 if (tst
->ss
->load_super(tst
,dfd
, NULL
)) {
7061 dprintf("no RAID superblock on %s\n",
7064 } else if (tst
->ss
->compare_super
== NULL
) {
7065 dprintf("Cannot assemble %s metadata on %s\n",
7066 tst
->ss
->name
, devname
);
7072 if (tmpdev
->used
== 2 || tmpdev
->used
== 4) {
7073 /* Ignore unrecognised devices during auto-assembly */
7078 tst
->ss
->getinfo_super(tst
, &info
, NULL
);
7080 if (st
->minor_version
== -1)
7081 st
->minor_version
= tst
->minor_version
;
7083 if (memcmp(info
.uuid
, uuid_zero
,
7084 sizeof(int[4])) == 0) {
7085 /* this is a floating spare. It cannot define
7086 * an array unless there are no more arrays of
7087 * this type to be found. It can be included
7088 * in an array of this type though.
7094 if (st
->ss
!= tst
->ss
||
7095 st
->minor_version
!= tst
->minor_version
||
7096 st
->ss
->compare_super(st
, tst
, 1) != 0) {
7097 /* Some mismatch. If exactly one array matches this host,
7098 * we can resolve on that one.
7099 * Or, if we are auto assembling, we just ignore the second
7102 dprintf("superblock on %s doesn't match others - assembly aborted\n",
7108 dprintf("found: devname: %s\n", devname
);
7112 tst
->ss
->free_super(tst
);
7116 if ((err
= load_super_imsm_all(st
, -1, &st
->sb
, NULL
, devlist
, 0)) == 0) {
7117 struct mdinfo
*iter
, *head
= st
->ss
->container_content(st
, NULL
);
7118 for (iter
= head
; iter
; iter
= iter
->next
) {
7119 dprintf("content->text_version: %s vol\n",
7120 iter
->text_version
);
7121 if (iter
->array
.state
& (1<<MD_SB_BLOCK_VOLUME
)) {
7122 /* do not assemble arrays with unsupported
7124 dprintf("Cannot activate member %s.\n",
7125 iter
->text_version
);
7132 dprintf("No valid super block on device list: err: %d %p\n",
7136 dprintf("no more devices to examine\n");
7139 for (tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
7140 if (tmpdev
->used
== 1 && tmpdev
->found
) {
7142 if (count
< tmpdev
->found
)
7145 count
-= tmpdev
->found
;
7148 if (tmpdev
->used
== 1)
7153 st
->ss
->free_super(st
);
7157 static int __count_volumes(char *hba_path
, int dpa
, int verbose
,
7160 struct sys_dev
*idev
, *intel_devices
= find_intel_devices();
7162 const struct orom_entry
*entry
;
7163 struct devid_list
*dv
, *devid_list
;
7168 for (idev
= intel_devices
; idev
; idev
= idev
->next
) {
7169 if (strstr(idev
->path
, hba_path
))
7173 if (!idev
|| !idev
->dev_id
)
7176 entry
= get_orom_entry_by_device_id(idev
->dev_id
);
7178 if (!entry
|| !entry
->devid_list
)
7181 devid_list
= entry
->devid_list
;
7182 for (dv
= devid_list
; dv
; dv
= dv
->next
) {
7183 struct md_list
*devlist
;
7184 struct sys_dev
*device
= NULL
;
7189 device
= device_by_id_and_path(dv
->devid
, hba_path
);
7191 device
= device_by_id(dv
->devid
);
7194 hpath
= device
->path
;
7198 devlist
= get_devices(hpath
);
7199 /* if no intel devices return zero volumes */
7200 if (devlist
== NULL
)
7203 count
+= active_arrays_by_format("imsm", hpath
, &devlist
, dpa
,
7205 dprintf("path: %s active arrays: %d\n", hpath
, count
);
7206 if (devlist
== NULL
)
7210 count
+= count_volumes_list(devlist
,
7214 dprintf("found %d count: %d\n", found
, count
);
7217 dprintf("path: %s total number of volumes: %d\n", hpath
, count
);
7220 struct md_list
*dv
= devlist
;
7221 devlist
= devlist
->next
;
7229 static int count_volumes(struct intel_hba
*hba
, int dpa
, int verbose
)
7233 if (hba
->type
== SYS_DEV_VMD
) {
7234 struct sys_dev
*dev
;
7237 for (dev
= find_intel_devices(); dev
; dev
= dev
->next
) {
7238 if (dev
->type
== SYS_DEV_VMD
)
7239 count
+= __count_volumes(dev
->path
, dpa
,
7244 return __count_volumes(hba
->path
, dpa
, verbose
, 0);
7247 static int imsm_default_chunk(const struct imsm_orom
*orom
)
7249 /* up to 512 if the plaform supports it, otherwise the platform max.
7250 * 128 if no platform detected
7252 int fs
= max(7, orom
? fls(orom
->sss
) : 0);
7254 return min(512, (1 << fs
));
7258 validate_geometry_imsm_orom(struct intel_super
*super
, int level
, int layout
,
7259 int raiddisks
, int *chunk
, unsigned long long size
, int verbose
)
7261 /* check/set platform and metadata limits/defaults */
7262 if (super
->orom
&& raiddisks
> super
->orom
->dpa
) {
7263 pr_vrb("platform supports a maximum of %d disks per array\n",
7268 /* capabilities of OROM tested - copied from validate_geometry_imsm_volume */
7269 if (!is_raid_level_supported(super
->orom
, level
, raiddisks
)) {
7270 pr_vrb("platform does not support raid%d with %d disk%s\n",
7271 level
, raiddisks
, raiddisks
> 1 ? "s" : "");
7275 if (*chunk
== 0 || *chunk
== UnSet
)
7276 *chunk
= imsm_default_chunk(super
->orom
);
7278 if (super
->orom
&& !imsm_orom_has_chunk(super
->orom
, *chunk
)) {
7279 pr_vrb("platform does not support a chunk size of: %d\n", *chunk
);
7283 if (layout
!= imsm_level_to_layout(level
)) {
7285 pr_vrb("imsm raid 5 only supports the left-asymmetric layout\n");
7286 else if (level
== 10)
7287 pr_vrb("imsm raid 10 only supports the n2 layout\n");
7289 pr_vrb("imsm unknown layout %#x for this raid level %d\n",
7294 if (super
->orom
&& (super
->orom
->attr
& IMSM_OROM_ATTR_2TB
) == 0 &&
7295 (calc_array_size(level
, raiddisks
, layout
, *chunk
, size
) >> 32) > 0) {
7296 pr_vrb("platform does not support a volume size over 2TB\n");
7303 /* validate_geometry_imsm_volume - lifted from validate_geometry_ddf_bvd
7304 * FIX ME add ahci details
7306 static int validate_geometry_imsm_volume(struct supertype
*st
, int level
,
7307 int layout
, int raiddisks
, int *chunk
,
7308 unsigned long long size
,
7309 unsigned long long data_offset
,
7311 unsigned long long *freesize
,
7315 struct intel_super
*super
= st
->sb
;
7316 struct imsm_super
*mpb
;
7318 unsigned long long pos
= 0;
7319 unsigned long long maxsize
;
7323 /* We must have the container info already read in. */
7327 mpb
= super
->anchor
;
7329 if (!validate_geometry_imsm_orom(super
, level
, layout
, raiddisks
, chunk
, size
, verbose
)) {
7330 pr_err("RAID geometry validation failed. Cannot proceed with the action(s).\n");
7334 /* General test: make sure there is space for
7335 * 'raiddisks' device extents of size 'size' at a given
7338 unsigned long long minsize
= size
;
7339 unsigned long long start_offset
= MaxSector
;
7342 minsize
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
7343 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
7348 e
= get_extents(super
, dl
, 0);
7351 unsigned long long esize
;
7352 esize
= e
[i
].start
- pos
;
7353 if (esize
>= minsize
)
7355 if (found
&& start_offset
== MaxSector
) {
7358 } else if (found
&& pos
!= start_offset
) {
7362 pos
= e
[i
].start
+ e
[i
].size
;
7364 } while (e
[i
-1].size
);
7369 if (dcnt
< raiddisks
) {
7371 pr_err("imsm: Not enough devices with space for this array (%d < %d)\n",
7378 /* This device must be a member of the set */
7379 if (!stat_is_blkdev(dev
, &rdev
))
7381 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
7382 if (dl
->major
== (int)major(rdev
) &&
7383 dl
->minor
== (int)minor(rdev
))
7388 pr_err("%s is not in the same imsm set\n", dev
);
7390 } else if (super
->orom
&& dl
->index
< 0 && mpb
->num_raid_devs
) {
7391 /* If a volume is present then the current creation attempt
7392 * cannot incorporate new spares because the orom may not
7393 * understand this configuration (all member disks must be
7394 * members of each array in the container).
7396 pr_err("%s is a spare and a volume is already defined for this container\n", dev
);
7397 pr_err("The option-rom requires all member disks to be a member of all volumes\n");
7399 } else if (super
->orom
&& mpb
->num_raid_devs
> 0 &&
7400 mpb
->num_disks
!= raiddisks
) {
7401 pr_err("The option-rom requires all member disks to be a member of all volumes\n");
7405 /* retrieve the largest free space block */
7406 e
= get_extents(super
, dl
, 0);
7411 unsigned long long esize
;
7413 esize
= e
[i
].start
- pos
;
7414 if (esize
>= maxsize
)
7416 pos
= e
[i
].start
+ e
[i
].size
;
7418 } while (e
[i
-1].size
);
7423 pr_err("unable to determine free space for: %s\n",
7427 if (maxsize
< size
) {
7429 pr_err("%s not enough space (%llu < %llu)\n",
7430 dev
, maxsize
, size
);
7434 /* count total number of extents for merge */
7436 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
7438 i
+= dl
->extent_cnt
;
7440 maxsize
= merge_extents(super
, i
);
7442 if (mpb
->num_raid_devs
> 0 && size
&& size
!= maxsize
)
7443 pr_err("attempting to create a second volume with size less then remaining space.\n");
7445 if (maxsize
< size
|| maxsize
== 0) {
7448 pr_err("no free space left on device. Aborting...\n");
7450 pr_err("not enough space to create volume of given size (%llu < %llu). Aborting...\n",
7456 *freesize
= maxsize
;
7459 int count
= count_volumes(super
->hba
,
7460 super
->orom
->dpa
, verbose
);
7461 if (super
->orom
->vphba
<= count
) {
7462 pr_vrb("platform does not support more than %d raid volumes.\n",
7463 super
->orom
->vphba
);
7470 static int imsm_get_free_size(struct supertype
*st
, int raiddisks
,
7471 unsigned long long size
, int chunk
,
7472 unsigned long long *freesize
)
7474 struct intel_super
*super
= st
->sb
;
7475 struct imsm_super
*mpb
= super
->anchor
;
7480 unsigned long long maxsize
;
7481 unsigned long long minsize
;
7485 /* find the largest common start free region of the possible disks */
7489 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
7495 /* don't activate new spares if we are orom constrained
7496 * and there is already a volume active in the container
7498 if (super
->orom
&& dl
->index
< 0 && mpb
->num_raid_devs
)
7501 e
= get_extents(super
, dl
, 0);
7504 for (i
= 1; e
[i
-1].size
; i
++)
7512 maxsize
= merge_extents(super
, extent_cnt
);
7516 minsize
= chunk
* 2;
7518 if (cnt
< raiddisks
||
7519 (super
->orom
&& used
&& used
!= raiddisks
) ||
7520 maxsize
< minsize
||
7522 pr_err("not enough devices with space to create array.\n");
7523 return 0; /* No enough free spaces large enough */
7534 if (mpb
->num_raid_devs
> 0 && size
&& size
!= maxsize
)
7535 pr_err("attempting to create a second volume with size less then remaining space.\n");
7537 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
7539 dl
->raiddisk
= cnt
++;
7543 dprintf("imsm: imsm_get_free_size() returns : %llu\n", size
);
7548 static int reserve_space(struct supertype
*st
, int raiddisks
,
7549 unsigned long long size
, int chunk
,
7550 unsigned long long *freesize
)
7552 struct intel_super
*super
= st
->sb
;
7557 rv
= imsm_get_free_size(st
, raiddisks
, size
, chunk
, freesize
);
7560 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
7562 dl
->raiddisk
= cnt
++;
7569 static int validate_geometry_imsm(struct supertype
*st
, int level
, int layout
,
7570 int raiddisks
, int *chunk
, unsigned long long size
,
7571 unsigned long long data_offset
,
7572 char *dev
, unsigned long long *freesize
,
7573 int consistency_policy
, int verbose
)
7580 * if given unused devices create a container
7581 * if given given devices in a container create a member volume
7583 if (level
== LEVEL_CONTAINER
)
7584 /* Must be a fresh device to add to a container */
7585 return validate_geometry_imsm_container(st
, level
, raiddisks
,
7590 * Size is given in sectors.
7592 if (size
&& (size
< 2048)) {
7593 pr_err("Given size must be greater than 1M.\n");
7594 /* Depends on algorithm in Create.c :
7595 * if container was given (dev == NULL) return -1,
7596 * if block device was given ( dev != NULL) return 0.
7598 return dev
? -1 : 0;
7603 struct intel_super
*super
= st
->sb
;
7604 if (!validate_geometry_imsm_orom(st
->sb
, level
, layout
,
7605 raiddisks
, chunk
, size
,
7608 /* we are being asked to automatically layout a
7609 * new volume based on the current contents of
7610 * the container. If the the parameters can be
7611 * satisfied reserve_space will record the disks,
7612 * start offset, and size of the volume to be
7613 * created. add_to_super and getinfo_super
7614 * detect when autolayout is in progress.
7616 /* assuming that freesize is always given when array is
7618 if (super
->orom
&& freesize
) {
7620 count
= count_volumes(super
->hba
,
7621 super
->orom
->dpa
, verbose
);
7622 if (super
->orom
->vphba
<= count
) {
7623 pr_vrb("platform does not support more than %d raid volumes.\n",
7624 super
->orom
->vphba
);
7629 return reserve_space(st
, raiddisks
, size
,
7635 /* creating in a given container */
7636 return validate_geometry_imsm_volume(st
, level
, layout
,
7637 raiddisks
, chunk
, size
,
7639 dev
, freesize
, verbose
);
7642 /* This device needs to be a device in an 'imsm' container */
7643 fd
= open(dev
, O_RDONLY
|O_EXCL
, 0);
7645 if (is_fd_valid(fd
)) {
7646 pr_vrb("Cannot create this array on device %s\n", dev
);
7651 fd
= open(dev
, O_RDONLY
, 0);
7653 if (!is_fd_valid(fd
)) {
7654 pr_vrb("Cannot open %s: %s\n", dev
, strerror(errno
));
7658 /* Well, it is in use by someone, maybe an 'imsm' container. */
7659 cfd
= open_container(fd
);
7662 if (!is_fd_valid(cfd
)) {
7663 pr_vrb("Cannot use %s: It is busy\n", dev
);
7666 sra
= sysfs_read(cfd
, NULL
, GET_VERSION
);
7667 if (sra
&& sra
->array
.major_version
== -1 &&
7668 strcmp(sra
->text_version
, "imsm") == 0)
7672 /* This is a member of a imsm container. Load the container
7673 * and try to create a volume
7675 struct intel_super
*super
;
7677 if (load_super_imsm_all(st
, cfd
, (void **) &super
, NULL
, NULL
, 1) == 0) {
7679 strcpy(st
->container_devnm
, fd2devnm(cfd
));
7681 return validate_geometry_imsm_volume(st
, level
, layout
,
7683 size
, data_offset
, dev
,
7690 pr_err("failed container membership check\n");
7696 static void default_geometry_imsm(struct supertype
*st
, int *level
, int *layout
, int *chunk
)
7698 struct intel_super
*super
= st
->sb
;
7700 if (level
&& *level
== UnSet
)
7701 *level
= LEVEL_CONTAINER
;
7703 if (level
&& layout
&& *layout
== UnSet
)
7704 *layout
= imsm_level_to_layout(*level
);
7706 if (chunk
&& (*chunk
== UnSet
|| *chunk
== 0))
7707 *chunk
= imsm_default_chunk(super
->orom
);
7710 static void handle_missing(struct intel_super
*super
, struct imsm_dev
*dev
);
7712 static int kill_subarray_imsm(struct supertype
*st
, char *subarray_id
)
7714 /* remove the subarray currently referenced by subarray_id */
7716 struct intel_dev
**dp
;
7717 struct intel_super
*super
= st
->sb
;
7718 __u8 current_vol
= strtoul(subarray_id
, NULL
, 10);
7719 struct imsm_super
*mpb
= super
->anchor
;
7721 if (mpb
->num_raid_devs
== 0)
7724 /* block deletions that would change the uuid of active subarrays
7726 * FIXME when immutable ids are available, but note that we'll
7727 * also need to fixup the invalidated/active subarray indexes in
7730 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
7733 if (i
< current_vol
)
7735 sprintf(subarray
, "%u", i
);
7736 if (is_subarray_active(subarray
, st
->devnm
)) {
7737 pr_err("deleting subarray-%d would change the UUID of active subarray-%d, aborting\n",
7744 if (st
->update_tail
) {
7745 struct imsm_update_kill_array
*u
= xmalloc(sizeof(*u
));
7747 u
->type
= update_kill_array
;
7748 u
->dev_idx
= current_vol
;
7749 append_metadata_update(st
, u
, sizeof(*u
));
7754 for (dp
= &super
->devlist
; *dp
;)
7755 if ((*dp
)->index
== current_vol
) {
7758 handle_missing(super
, (*dp
)->dev
);
7759 if ((*dp
)->index
> current_vol
)
7764 /* no more raid devices, all active components are now spares,
7765 * but of course failed are still failed
7767 if (--mpb
->num_raid_devs
== 0) {
7770 for (d
= super
->disks
; d
; d
= d
->next
)
7775 super
->updates_pending
++;
7780 static int get_rwh_policy_from_update(char *update
)
7782 if (strcmp(update
, "ppl") == 0)
7783 return RWH_MULTIPLE_DISTRIBUTED
;
7784 else if (strcmp(update
, "no-ppl") == 0)
7785 return RWH_MULTIPLE_OFF
;
7786 else if (strcmp(update
, "bitmap") == 0)
7788 else if (strcmp(update
, "no-bitmap") == 0)
7793 static int update_subarray_imsm(struct supertype
*st
, char *subarray
,
7794 char *update
, struct mddev_ident
*ident
)
7796 /* update the subarray currently referenced by ->current_vol */
7797 struct intel_super
*super
= st
->sb
;
7798 struct imsm_super
*mpb
= super
->anchor
;
7800 if (strcmp(update
, "name") == 0) {
7801 char *name
= ident
->name
;
7805 if (is_subarray_active(subarray
, st
->devnm
)) {
7806 pr_err("Unable to update name of active subarray\n");
7810 if (!check_name(super
, name
, 0))
7813 vol
= strtoul(subarray
, &ep
, 10);
7814 if (*ep
!= '\0' || vol
>= super
->anchor
->num_raid_devs
)
7817 if (st
->update_tail
) {
7818 struct imsm_update_rename_array
*u
= xmalloc(sizeof(*u
));
7820 u
->type
= update_rename_array
;
7822 strncpy((char *) u
->name
, name
, MAX_RAID_SERIAL_LEN
);
7823 u
->name
[MAX_RAID_SERIAL_LEN
-1] = '\0';
7824 append_metadata_update(st
, u
, sizeof(*u
));
7826 struct imsm_dev
*dev
;
7829 dev
= get_imsm_dev(super
, vol
);
7830 memset(dev
->volume
, '\0', MAX_RAID_SERIAL_LEN
);
7831 namelen
= min((int)strlen(name
), MAX_RAID_SERIAL_LEN
);
7832 memcpy(dev
->volume
, name
, namelen
);
7833 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
7834 dev
= get_imsm_dev(super
, i
);
7835 handle_missing(super
, dev
);
7837 super
->updates_pending
++;
7839 } else if (get_rwh_policy_from_update(update
) != -1) {
7842 int vol
= strtoul(subarray
, &ep
, 10);
7844 if (*ep
!= '\0' || vol
>= super
->anchor
->num_raid_devs
)
7847 new_policy
= get_rwh_policy_from_update(update
);
7849 if (st
->update_tail
) {
7850 struct imsm_update_rwh_policy
*u
= xmalloc(sizeof(*u
));
7852 u
->type
= update_rwh_policy
;
7854 u
->new_policy
= new_policy
;
7855 append_metadata_update(st
, u
, sizeof(*u
));
7857 struct imsm_dev
*dev
;
7859 dev
= get_imsm_dev(super
, vol
);
7860 dev
->rwh_policy
= new_policy
;
7861 super
->updates_pending
++;
7863 if (new_policy
== RWH_BITMAP
)
7864 return write_init_bitmap_imsm_vol(st
, vol
);
7871 static bool is_gen_migration(struct imsm_dev
*dev
)
7873 if (dev
&& dev
->vol
.migr_state
&&
7874 migr_type(dev
) == MIGR_GEN_MIGR
)
7880 static int is_rebuilding(struct imsm_dev
*dev
)
7882 struct imsm_map
*migr_map
;
7884 if (!dev
->vol
.migr_state
)
7887 if (migr_type(dev
) != MIGR_REBUILD
)
7890 migr_map
= get_imsm_map(dev
, MAP_1
);
7892 if (migr_map
->map_state
== IMSM_T_STATE_DEGRADED
)
7898 static int is_initializing(struct imsm_dev
*dev
)
7900 struct imsm_map
*migr_map
;
7902 if (!dev
->vol
.migr_state
)
7905 if (migr_type(dev
) != MIGR_INIT
)
7908 migr_map
= get_imsm_map(dev
, MAP_1
);
7910 if (migr_map
->map_state
== IMSM_T_STATE_UNINITIALIZED
)
7916 static void update_recovery_start(struct intel_super
*super
,
7917 struct imsm_dev
*dev
,
7918 struct mdinfo
*array
)
7920 struct mdinfo
*rebuild
= NULL
;
7924 if (!is_rebuilding(dev
))
7927 /* Find the rebuild target, but punt on the dual rebuild case */
7928 for (d
= array
->devs
; d
; d
= d
->next
)
7929 if (d
->recovery_start
== 0) {
7936 /* (?) none of the disks are marked with
7937 * IMSM_ORD_REBUILD, so assume they are missing and the
7938 * disk_ord_tbl was not correctly updated
7940 dprintf("failed to locate out-of-sync disk\n");
7944 units
= vol_curr_migr_unit(dev
);
7945 rebuild
->recovery_start
= units
* blocks_per_migr_unit(super
, dev
);
7948 static int recover_backup_imsm(struct supertype
*st
, struct mdinfo
*info
);
7950 static struct mdinfo
*container_content_imsm(struct supertype
*st
, char *subarray
)
7952 /* Given a container loaded by load_super_imsm_all,
7953 * extract information about all the arrays into
7955 * If 'subarray' is given, just extract info about that array.
7957 * For each imsm_dev create an mdinfo, fill it in,
7958 * then look for matching devices in super->disks
7959 * and create appropriate device mdinfo.
7961 struct intel_super
*super
= st
->sb
;
7962 struct imsm_super
*mpb
= super
->anchor
;
7963 struct mdinfo
*rest
= NULL
;
7967 int spare_disks
= 0;
7968 int current_vol
= super
->current_vol
;
7970 /* do not assemble arrays when not all attributes are supported */
7971 if (imsm_check_attributes(mpb
->attributes
) == 0) {
7973 pr_err("Unsupported attributes in IMSM metadata.Arrays activation is blocked.\n");
7976 /* count spare devices, not used in maps
7978 for (d
= super
->disks
; d
; d
= d
->next
)
7982 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
7983 struct imsm_dev
*dev
;
7984 struct imsm_map
*map
;
7985 struct imsm_map
*map2
;
7986 struct mdinfo
*this;
7993 (i
!= strtoul(subarray
, &ep
, 10) || *ep
!= '\0'))
7996 dev
= get_imsm_dev(super
, i
);
7997 map
= get_imsm_map(dev
, MAP_0
);
7998 map2
= get_imsm_map(dev
, MAP_1
);
7999 level
= get_imsm_raid_level(map
);
8001 /* do not publish arrays that are in the middle of an
8002 * unsupported migration
8004 if (dev
->vol
.migr_state
&&
8005 (migr_type(dev
) == MIGR_STATE_CHANGE
)) {
8006 pr_err("cannot assemble volume '%.16s': unsupported migration in progress\n",
8010 /* do not publish arrays that are not support by controller's
8014 this = xmalloc(sizeof(*this));
8016 super
->current_vol
= i
;
8017 getinfo_super_imsm_volume(st
, this, NULL
);
8019 chunk
= __le16_to_cpu(map
->blocks_per_strip
) >> 1;
8020 /* mdadm does not support all metadata features- set the bit in all arrays state */
8021 if (!validate_geometry_imsm_orom(super
,
8022 level
, /* RAID level */
8023 imsm_level_to_layout(level
),
8024 map
->num_members
, /* raid disks */
8025 &chunk
, imsm_dev_size(dev
),
8027 pr_err("IMSM RAID geometry validation failed. Array %s activation is blocked.\n",
8029 this->array
.state
|=
8030 (1<<MD_SB_BLOCK_CONTAINER_RESHAPE
) |
8031 (1<<MD_SB_BLOCK_VOLUME
);
8034 /* if array has bad blocks, set suitable bit in all arrays state */
8036 this->array
.state
|=
8037 (1<<MD_SB_BLOCK_CONTAINER_RESHAPE
) |
8038 (1<<MD_SB_BLOCK_VOLUME
);
8040 for (slot
= 0 ; slot
< map
->num_members
; slot
++) {
8041 unsigned long long recovery_start
;
8042 struct mdinfo
*info_d
;
8050 idx
= get_imsm_disk_idx(dev
, slot
, MAP_0
);
8051 ord
= get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
);
8052 for (d
= super
->disks
; d
; d
= d
->next
)
8053 if (d
->index
== idx
)
8056 recovery_start
= MaxSector
;
8059 if (d
&& is_failed(&d
->disk
))
8061 if (!skip
&& (ord
& IMSM_ORD_REBUILD
))
8063 if (!(ord
& IMSM_ORD_REBUILD
))
8064 this->array
.working_disks
++;
8066 * if we skip some disks the array will be assmebled degraded;
8067 * reset resync start to avoid a dirty-degraded
8068 * situation when performing the intial sync
8073 if (!(dev
->vol
.dirty
& RAIDVOL_DIRTY
)) {
8074 if ((!able_to_resync(level
, missing
) ||
8075 recovery_start
== 0))
8076 this->resync_start
= MaxSector
;
8082 info_d
= xcalloc(1, sizeof(*info_d
));
8083 info_d
->next
= this->devs
;
8084 this->devs
= info_d
;
8086 info_d
->disk
.number
= d
->index
;
8087 info_d
->disk
.major
= d
->major
;
8088 info_d
->disk
.minor
= d
->minor
;
8089 info_d
->disk
.raid_disk
= slot
;
8090 info_d
->recovery_start
= recovery_start
;
8092 if (slot
< map2
->num_members
)
8093 info_d
->disk
.state
= (1 << MD_DISK_ACTIVE
);
8095 this->array
.spare_disks
++;
8097 if (slot
< map
->num_members
)
8098 info_d
->disk
.state
= (1 << MD_DISK_ACTIVE
);
8100 this->array
.spare_disks
++;
8103 info_d
->events
= __le32_to_cpu(mpb
->generation_num
);
8104 info_d
->data_offset
= pba_of_lba0(map
);
8105 info_d
->component_size
= calc_component_size(map
, dev
);
8107 if (map
->raid_level
== 5) {
8108 info_d
->ppl_sector
= this->ppl_sector
;
8109 info_d
->ppl_size
= this->ppl_size
;
8110 if (this->consistency_policy
== CONSISTENCY_POLICY_PPL
&&
8111 recovery_start
== 0)
8112 this->resync_start
= 0;
8115 info_d
->bb
.supported
= 1;
8116 get_volume_badblocks(super
->bbm_log
, ord_to_idx(ord
),
8117 info_d
->data_offset
,
8118 info_d
->component_size
,
8121 /* now that the disk list is up-to-date fixup recovery_start */
8122 update_recovery_start(super
, dev
, this);
8123 this->array
.spare_disks
+= spare_disks
;
8125 /* check for reshape */
8126 if (this->reshape_active
== 1)
8127 recover_backup_imsm(st
, this);
8131 super
->current_vol
= current_vol
;
8135 static __u8
imsm_check_degraded(struct intel_super
*super
, struct imsm_dev
*dev
,
8136 int failed
, int look_in_map
)
8138 struct imsm_map
*map
;
8140 map
= get_imsm_map(dev
, look_in_map
);
8143 return map
->map_state
== IMSM_T_STATE_UNINITIALIZED
?
8144 IMSM_T_STATE_UNINITIALIZED
: IMSM_T_STATE_NORMAL
;
8146 switch (get_imsm_raid_level(map
)) {
8148 return IMSM_T_STATE_FAILED
;
8151 if (failed
< map
->num_members
)
8152 return IMSM_T_STATE_DEGRADED
;
8154 return IMSM_T_STATE_FAILED
;
8159 * check to see if any mirrors have failed, otherwise we
8160 * are degraded. Even numbered slots are mirrored on
8164 /* gcc -Os complains that this is unused */
8165 int insync
= insync
;
8167 for (i
= 0; i
< map
->num_members
; i
++) {
8168 __u32 ord
= get_imsm_ord_tbl_ent(dev
, i
, MAP_X
);
8169 int idx
= ord_to_idx(ord
);
8170 struct imsm_disk
*disk
;
8172 /* reset the potential in-sync count on even-numbered
8173 * slots. num_copies is always 2 for imsm raid10
8178 disk
= get_imsm_disk(super
, idx
);
8179 if (!disk
|| is_failed(disk
) || ord
& IMSM_ORD_REBUILD
)
8182 /* no in-sync disks left in this mirror the
8186 return IMSM_T_STATE_FAILED
;
8189 return IMSM_T_STATE_DEGRADED
;
8193 return IMSM_T_STATE_DEGRADED
;
8195 return IMSM_T_STATE_FAILED
;
8201 return map
->map_state
;
8204 static int imsm_count_failed(struct intel_super
*super
, struct imsm_dev
*dev
,
8209 struct imsm_disk
*disk
;
8210 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8211 struct imsm_map
*prev
= get_imsm_map(dev
, MAP_1
);
8212 struct imsm_map
*map_for_loop
;
8217 /* at the beginning of migration we set IMSM_ORD_REBUILD on
8218 * disks that are being rebuilt. New failures are recorded to
8219 * map[0]. So we look through all the disks we started with and
8220 * see if any failures are still present, or if any new ones
8224 if (prev
&& (map
->num_members
< prev
->num_members
))
8225 map_for_loop
= prev
;
8227 for (i
= 0; i
< map_for_loop
->num_members
; i
++) {
8229 /* when MAP_X is passed both maps failures are counted
8232 (look_in_map
== MAP_1
|| look_in_map
== MAP_X
) &&
8233 i
< prev
->num_members
) {
8234 ord
= __le32_to_cpu(prev
->disk_ord_tbl
[i
]);
8235 idx_1
= ord_to_idx(ord
);
8237 disk
= get_imsm_disk(super
, idx_1
);
8238 if (!disk
|| is_failed(disk
) || ord
& IMSM_ORD_REBUILD
)
8241 if ((look_in_map
== MAP_0
|| look_in_map
== MAP_X
) &&
8242 i
< map
->num_members
) {
8243 ord
= __le32_to_cpu(map
->disk_ord_tbl
[i
]);
8244 idx
= ord_to_idx(ord
);
8247 disk
= get_imsm_disk(super
, idx
);
8248 if (!disk
|| is_failed(disk
) ||
8249 ord
& IMSM_ORD_REBUILD
)
8258 static int imsm_open_new(struct supertype
*c
, struct active_array
*a
,
8261 struct intel_super
*super
= c
->sb
;
8262 struct imsm_super
*mpb
= super
->anchor
;
8263 struct imsm_update_prealloc_bb_mem u
;
8265 if (inst
>= mpb
->num_raid_devs
) {
8266 pr_err("subarry index %d, out of range\n", inst
);
8270 dprintf("imsm: open_new %d\n", inst
);
8271 a
->info
.container_member
= inst
;
8273 u
.type
= update_prealloc_badblocks_mem
;
8274 imsm_update_metadata_locally(c
, &u
, sizeof(u
));
8279 static int is_resyncing(struct imsm_dev
*dev
)
8281 struct imsm_map
*migr_map
;
8283 if (!dev
->vol
.migr_state
)
8286 if (migr_type(dev
) == MIGR_INIT
||
8287 migr_type(dev
) == MIGR_REPAIR
)
8290 if (migr_type(dev
) == MIGR_GEN_MIGR
)
8293 migr_map
= get_imsm_map(dev
, MAP_1
);
8295 if (migr_map
->map_state
== IMSM_T_STATE_NORMAL
&&
8296 dev
->vol
.migr_type
!= MIGR_GEN_MIGR
)
8302 /* return true if we recorded new information */
8303 static int mark_failure(struct intel_super
*super
,
8304 struct imsm_dev
*dev
, struct imsm_disk
*disk
, int idx
)
8308 struct imsm_map
*map
;
8309 char buf
[MAX_RAID_SERIAL_LEN
+3];
8310 unsigned int len
, shift
= 0;
8312 /* new failures are always set in map[0] */
8313 map
= get_imsm_map(dev
, MAP_0
);
8315 slot
= get_imsm_disk_slot(map
, idx
);
8319 ord
= __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
8320 if (is_failed(disk
) && (ord
& IMSM_ORD_REBUILD
))
8323 memcpy(buf
, disk
->serial
, MAX_RAID_SERIAL_LEN
);
8324 buf
[MAX_RAID_SERIAL_LEN
] = '\000';
8326 if ((len
= strlen(buf
)) >= MAX_RAID_SERIAL_LEN
)
8327 shift
= len
- MAX_RAID_SERIAL_LEN
+ 1;
8328 memcpy(disk
->serial
, &buf
[shift
], len
+ 1 - shift
);
8330 disk
->status
|= FAILED_DISK
;
8331 set_imsm_ord_tbl_ent(map
, slot
, idx
| IMSM_ORD_REBUILD
);
8332 /* mark failures in second map if second map exists and this disk
8334 * This is valid for migration, initialization and rebuild
8336 if (dev
->vol
.migr_state
) {
8337 struct imsm_map
*map2
= get_imsm_map(dev
, MAP_1
);
8338 int slot2
= get_imsm_disk_slot(map2
, idx
);
8340 if (slot2
< map2
->num_members
&& slot2
>= 0)
8341 set_imsm_ord_tbl_ent(map2
, slot2
,
8342 idx
| IMSM_ORD_REBUILD
);
8344 if (map
->failed_disk_num
== 0xff ||
8345 (!is_rebuilding(dev
) && map
->failed_disk_num
> slot
))
8346 map
->failed_disk_num
= slot
;
8348 clear_disk_badblocks(super
->bbm_log
, ord_to_idx(ord
));
8353 static void mark_missing(struct intel_super
*super
,
8354 struct imsm_dev
*dev
, struct imsm_disk
*disk
, int idx
)
8356 mark_failure(super
, dev
, disk
, idx
);
8358 if (disk
->scsi_id
== __cpu_to_le32(~(__u32
)0))
8361 disk
->scsi_id
= __cpu_to_le32(~(__u32
)0);
8362 memmove(&disk
->serial
[0], &disk
->serial
[1], MAX_RAID_SERIAL_LEN
- 1);
8365 static void handle_missing(struct intel_super
*super
, struct imsm_dev
*dev
)
8369 if (!super
->missing
)
8372 /* When orom adds replacement for missing disk it does
8373 * not remove entry of missing disk, but just updates map with
8374 * new added disk. So it is not enough just to test if there is
8375 * any missing disk, we have to look if there are any failed disks
8376 * in map to stop migration */
8378 dprintf("imsm: mark missing\n");
8379 /* end process for initialization and rebuild only
8381 if (is_gen_migration(dev
) == false) {
8382 int failed
= imsm_count_failed(super
, dev
, MAP_0
);
8386 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8387 struct imsm_map
*map1
;
8388 int i
, ord
, ord_map1
;
8391 for (i
= 0; i
< map
->num_members
; i
++) {
8392 ord
= get_imsm_ord_tbl_ent(dev
, i
, MAP_0
);
8393 if (!(ord
& IMSM_ORD_REBUILD
))
8396 map1
= get_imsm_map(dev
, MAP_1
);
8400 ord_map1
= __le32_to_cpu(map1
->disk_ord_tbl
[i
]);
8401 if (ord_map1
& IMSM_ORD_REBUILD
)
8406 map_state
= imsm_check_degraded(super
, dev
,
8408 end_migration(dev
, super
, map_state
);
8412 for (dl
= super
->missing
; dl
; dl
= dl
->next
)
8413 mark_missing(super
, dev
, &dl
->disk
, dl
->index
);
8414 super
->updates_pending
++;
8417 static unsigned long long imsm_set_array_size(struct imsm_dev
*dev
,
8420 unsigned long long array_blocks
;
8421 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8422 int used_disks
= imsm_num_data_members(map
);
8424 if (used_disks
== 0) {
8425 /* when problems occures
8426 * return current array_blocks value
8428 array_blocks
= imsm_dev_size(dev
);
8430 return array_blocks
;
8433 /* set array size in metadata
8436 /* OLCE size change is caused by added disks
8438 array_blocks
= per_dev_array_size(map
) * used_disks
;
8440 /* Online Volume Size Change
8441 * Using available free space
8443 array_blocks
= new_size
;
8445 array_blocks
= round_size_to_mb(array_blocks
, used_disks
);
8446 set_imsm_dev_size(dev
, array_blocks
);
8448 return array_blocks
;
8451 static void imsm_set_disk(struct active_array
*a
, int n
, int state
);
8453 static void imsm_progress_container_reshape(struct intel_super
*super
)
8455 /* if no device has a migr_state, but some device has a
8456 * different number of members than the previous device, start
8457 * changing the number of devices in this device to match
8460 struct imsm_super
*mpb
= super
->anchor
;
8461 int prev_disks
= -1;
8465 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
8466 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
8467 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8468 struct imsm_map
*map2
;
8469 int prev_num_members
;
8471 if (dev
->vol
.migr_state
)
8474 if (prev_disks
== -1)
8475 prev_disks
= map
->num_members
;
8476 if (prev_disks
== map
->num_members
)
8479 /* OK, this array needs to enter reshape mode.
8480 * i.e it needs a migr_state
8483 copy_map_size
= sizeof_imsm_map(map
);
8484 prev_num_members
= map
->num_members
;
8485 map
->num_members
= prev_disks
;
8486 dev
->vol
.migr_state
= 1;
8487 set_vol_curr_migr_unit(dev
, 0);
8488 set_migr_type(dev
, MIGR_GEN_MIGR
);
8489 for (i
= prev_num_members
;
8490 i
< map
->num_members
; i
++)
8491 set_imsm_ord_tbl_ent(map
, i
, i
);
8492 map2
= get_imsm_map(dev
, MAP_1
);
8493 /* Copy the current map */
8494 memcpy(map2
, map
, copy_map_size
);
8495 map2
->num_members
= prev_num_members
;
8497 imsm_set_array_size(dev
, -1);
8498 super
->clean_migration_record_by_mdmon
= 1;
8499 super
->updates_pending
++;
8503 /* Handle dirty -> clean transititions, resync and reshape. Degraded and rebuild
8504 * states are handled in imsm_set_disk() with one exception, when a
8505 * resync is stopped due to a new failure this routine will set the
8506 * 'degraded' state for the array.
8508 static int imsm_set_array_state(struct active_array
*a
, int consistent
)
8510 int inst
= a
->info
.container_member
;
8511 struct intel_super
*super
= a
->container
->sb
;
8512 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
8513 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8514 int failed
= imsm_count_failed(super
, dev
, MAP_0
);
8515 __u8 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
8516 __u32 blocks_per_unit
;
8518 if (dev
->vol
.migr_state
&&
8519 dev
->vol
.migr_type
== MIGR_GEN_MIGR
) {
8520 /* array state change is blocked due to reshape action
8522 * - abort the reshape (if last_checkpoint is 0 and action!= reshape)
8523 * - finish the reshape (if last_checkpoint is big and action != reshape)
8524 * - update vol_curr_migr_unit
8526 if (a
->curr_action
== reshape
) {
8527 /* still reshaping, maybe update vol_curr_migr_unit */
8528 goto mark_checkpoint
;
8530 if (a
->last_checkpoint
== 0 && a
->prev_action
== reshape
) {
8531 /* for some reason we aborted the reshape.
8533 * disable automatic metadata rollback
8534 * user action is required to recover process
8537 struct imsm_map
*map2
=
8538 get_imsm_map(dev
, MAP_1
);
8539 dev
->vol
.migr_state
= 0;
8540 set_migr_type(dev
, 0);
8541 set_vol_curr_migr_unit(dev
, 0);
8543 sizeof_imsm_map(map2
));
8544 super
->updates_pending
++;
8547 if (a
->last_checkpoint
>= a
->info
.component_size
) {
8548 unsigned long long array_blocks
;
8552 used_disks
= imsm_num_data_members(map
);
8553 if (used_disks
> 0) {
8555 per_dev_array_size(map
) *
8558 round_size_to_mb(array_blocks
,
8560 a
->info
.custom_array_size
= array_blocks
;
8561 /* encourage manager to update array
8565 a
->check_reshape
= 1;
8567 /* finalize online capacity expansion/reshape */
8568 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
)
8570 mdi
->disk
.raid_disk
,
8573 imsm_progress_container_reshape(super
);
8578 /* before we activate this array handle any missing disks */
8579 if (consistent
== 2)
8580 handle_missing(super
, dev
);
8582 if (consistent
== 2 &&
8583 (!is_resync_complete(&a
->info
) ||
8584 map_state
!= IMSM_T_STATE_NORMAL
||
8585 dev
->vol
.migr_state
))
8588 if (is_resync_complete(&a
->info
)) {
8589 /* complete intialization / resync,
8590 * recovery and interrupted recovery is completed in
8593 if (is_resyncing(dev
)) {
8594 dprintf("imsm: mark resync done\n");
8595 end_migration(dev
, super
, map_state
);
8596 super
->updates_pending
++;
8597 a
->last_checkpoint
= 0;
8599 } else if ((!is_resyncing(dev
) && !failed
) &&
8600 (imsm_reshape_blocks_arrays_changes(super
) == 0)) {
8601 /* mark the start of the init process if nothing is failed */
8602 dprintf("imsm: mark resync start\n");
8603 if (map
->map_state
== IMSM_T_STATE_UNINITIALIZED
)
8604 migrate(dev
, super
, IMSM_T_STATE_NORMAL
, MIGR_INIT
);
8606 migrate(dev
, super
, IMSM_T_STATE_NORMAL
, MIGR_REPAIR
);
8607 super
->updates_pending
++;
8611 /* skip checkpointing for general migration,
8612 * it is controlled in mdadm
8614 if (is_gen_migration(dev
))
8615 goto skip_mark_checkpoint
;
8617 /* check if we can update vol_curr_migr_unit from resync_start,
8620 blocks_per_unit
= blocks_per_migr_unit(super
, dev
);
8621 if (blocks_per_unit
) {
8622 set_vol_curr_migr_unit(dev
,
8623 a
->last_checkpoint
/ blocks_per_unit
);
8624 dprintf("imsm: mark checkpoint (%llu)\n",
8625 vol_curr_migr_unit(dev
));
8626 super
->updates_pending
++;
8629 skip_mark_checkpoint
:
8630 /* mark dirty / clean */
8631 if (((dev
->vol
.dirty
& RAIDVOL_DIRTY
) && consistent
) ||
8632 (!(dev
->vol
.dirty
& RAIDVOL_DIRTY
) && !consistent
)) {
8633 dprintf("imsm: mark '%s'\n", consistent
? "clean" : "dirty");
8635 dev
->vol
.dirty
= RAIDVOL_CLEAN
;
8637 dev
->vol
.dirty
= RAIDVOL_DIRTY
;
8638 if (dev
->rwh_policy
== RWH_DISTRIBUTED
||
8639 dev
->rwh_policy
== RWH_MULTIPLE_DISTRIBUTED
)
8640 dev
->vol
.dirty
|= RAIDVOL_DSRECORD_VALID
;
8642 super
->updates_pending
++;
8648 static int imsm_disk_slot_to_ord(struct active_array
*a
, int slot
)
8650 int inst
= a
->info
.container_member
;
8651 struct intel_super
*super
= a
->container
->sb
;
8652 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
8653 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8655 if (slot
> map
->num_members
) {
8656 pr_err("imsm: imsm_disk_slot_to_ord %d out of range 0..%d\n",
8657 slot
, map
->num_members
- 1);
8664 return get_imsm_ord_tbl_ent(dev
, slot
, MAP_0
);
8667 static void imsm_set_disk(struct active_array
*a
, int n
, int state
)
8669 int inst
= a
->info
.container_member
;
8670 struct intel_super
*super
= a
->container
->sb
;
8671 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
8672 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8673 struct imsm_disk
*disk
;
8675 int recovery_not_finished
= 0;
8679 int rebuild_done
= 0;
8682 ord
= get_imsm_ord_tbl_ent(dev
, n
, MAP_X
);
8686 dprintf("imsm: set_disk %d:%x\n", n
, state
);
8687 disk
= get_imsm_disk(super
, ord_to_idx(ord
));
8689 /* check for new failures */
8690 if (disk
&& (state
& DS_FAULTY
)) {
8691 if (mark_failure(super
, dev
, disk
, ord_to_idx(ord
)))
8692 super
->updates_pending
++;
8695 /* check if in_sync */
8696 if (state
& DS_INSYNC
&& ord
& IMSM_ORD_REBUILD
&& is_rebuilding(dev
)) {
8697 struct imsm_map
*migr_map
= get_imsm_map(dev
, MAP_1
);
8699 set_imsm_ord_tbl_ent(migr_map
, n
, ord_to_idx(ord
));
8701 super
->updates_pending
++;
8704 failed
= imsm_count_failed(super
, dev
, MAP_0
);
8705 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
8707 /* check if recovery complete, newly degraded, or failed */
8708 dprintf("imsm: Detected transition to state ");
8709 switch (map_state
) {
8710 case IMSM_T_STATE_NORMAL
: /* transition to normal state */
8711 dprintf("normal: ");
8712 if (is_rebuilding(dev
)) {
8713 dprintf_cont("while rebuilding");
8714 /* check if recovery is really finished */
8715 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
)
8716 if (mdi
->recovery_start
!= MaxSector
) {
8717 recovery_not_finished
= 1;
8720 if (recovery_not_finished
) {
8722 dprintf("Rebuild has not finished yet, state not changed");
8723 if (a
->last_checkpoint
< mdi
->recovery_start
) {
8724 a
->last_checkpoint
= mdi
->recovery_start
;
8725 super
->updates_pending
++;
8729 end_migration(dev
, super
, map_state
);
8730 map
->failed_disk_num
= ~0;
8731 super
->updates_pending
++;
8732 a
->last_checkpoint
= 0;
8735 if (is_gen_migration(dev
)) {
8736 dprintf_cont("while general migration");
8737 if (a
->last_checkpoint
>= a
->info
.component_size
)
8738 end_migration(dev
, super
, map_state
);
8740 map
->map_state
= map_state
;
8741 map
->failed_disk_num
= ~0;
8742 super
->updates_pending
++;
8746 case IMSM_T_STATE_DEGRADED
: /* transition to degraded state */
8747 dprintf_cont("degraded: ");
8748 if (map
->map_state
!= map_state
&& !dev
->vol
.migr_state
) {
8749 dprintf_cont("mark degraded");
8750 map
->map_state
= map_state
;
8751 super
->updates_pending
++;
8752 a
->last_checkpoint
= 0;
8755 if (is_rebuilding(dev
)) {
8756 dprintf_cont("while rebuilding ");
8757 if (state
& DS_FAULTY
) {
8758 dprintf_cont("removing failed drive ");
8759 if (n
== map
->failed_disk_num
) {
8760 dprintf_cont("end migration");
8761 end_migration(dev
, super
, map_state
);
8762 a
->last_checkpoint
= 0;
8764 dprintf_cont("fail detected during rebuild, changing map state");
8765 map
->map_state
= map_state
;
8767 super
->updates_pending
++;
8773 /* check if recovery is really finished */
8774 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
)
8775 if (mdi
->recovery_start
!= MaxSector
) {
8776 recovery_not_finished
= 1;
8779 if (recovery_not_finished
) {
8781 dprintf_cont("Rebuild has not finished yet");
8782 if (a
->last_checkpoint
< mdi
->recovery_start
) {
8783 a
->last_checkpoint
=
8784 mdi
->recovery_start
;
8785 super
->updates_pending
++;
8790 dprintf_cont(" Rebuild done, still degraded");
8791 end_migration(dev
, super
, map_state
);
8792 a
->last_checkpoint
= 0;
8793 super
->updates_pending
++;
8795 for (i
= 0; i
< map
->num_members
; i
++) {
8796 int idx
= get_imsm_ord_tbl_ent(dev
, i
, MAP_0
);
8798 if (idx
& IMSM_ORD_REBUILD
)
8799 map
->failed_disk_num
= i
;
8801 super
->updates_pending
++;
8804 if (is_gen_migration(dev
)) {
8805 dprintf_cont("while general migration");
8806 if (a
->last_checkpoint
>= a
->info
.component_size
)
8807 end_migration(dev
, super
, map_state
);
8809 map
->map_state
= map_state
;
8810 manage_second_map(super
, dev
);
8812 super
->updates_pending
++;
8815 if (is_initializing(dev
)) {
8816 dprintf_cont("while initialization.");
8817 map
->map_state
= map_state
;
8818 super
->updates_pending
++;
8822 case IMSM_T_STATE_FAILED
: /* transition to failed state */
8823 dprintf_cont("failed: ");
8824 if (is_gen_migration(dev
)) {
8825 dprintf_cont("while general migration");
8826 map
->map_state
= map_state
;
8827 super
->updates_pending
++;
8830 if (map
->map_state
!= map_state
) {
8831 dprintf_cont("mark failed");
8832 end_migration(dev
, super
, map_state
);
8833 super
->updates_pending
++;
8834 a
->last_checkpoint
= 0;
8839 dprintf_cont("state %i\n", map_state
);
8844 static int store_imsm_mpb(int fd
, struct imsm_super
*mpb
)
8847 __u32 mpb_size
= __le32_to_cpu(mpb
->mpb_size
);
8848 unsigned long long dsize
;
8849 unsigned long long sectors
;
8850 unsigned int sector_size
;
8852 if (!get_dev_sector_size(fd
, NULL
, §or_size
))
8854 get_dev_size(fd
, NULL
, &dsize
);
8856 if (mpb_size
> sector_size
) {
8857 /* -1 to account for anchor */
8858 sectors
= mpb_sectors(mpb
, sector_size
) - 1;
8860 /* write the extended mpb to the sectors preceeding the anchor */
8861 if (lseek64(fd
, dsize
- (sector_size
* (2 + sectors
)),
8865 if ((unsigned long long)write(fd
, buf
+ sector_size
,
8866 sector_size
* sectors
) != sector_size
* sectors
)
8870 /* first block is stored on second to last sector of the disk */
8871 if (lseek64(fd
, dsize
- (sector_size
* 2), SEEK_SET
) < 0)
8874 if ((unsigned int)write(fd
, buf
, sector_size
) != sector_size
)
8880 static void imsm_sync_metadata(struct supertype
*container
)
8882 struct intel_super
*super
= container
->sb
;
8884 dprintf("sync metadata: %d\n", super
->updates_pending
);
8885 if (!super
->updates_pending
)
8888 write_super_imsm(container
, 0);
8890 super
->updates_pending
= 0;
8893 static struct dl
*imsm_readd(struct intel_super
*super
, int idx
, struct active_array
*a
)
8895 struct imsm_dev
*dev
= get_imsm_dev(super
, a
->info
.container_member
);
8896 int i
= get_imsm_disk_idx(dev
, idx
, MAP_X
);
8899 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
8903 if (dl
&& is_failed(&dl
->disk
))
8907 dprintf("found %x:%x\n", dl
->major
, dl
->minor
);
8912 static struct dl
*imsm_add_spare(struct intel_super
*super
, int slot
,
8913 struct active_array
*a
, int activate_new
,
8914 struct mdinfo
*additional_test_list
)
8916 struct imsm_dev
*dev
= get_imsm_dev(super
, a
->info
.container_member
);
8917 int idx
= get_imsm_disk_idx(dev
, slot
, MAP_X
);
8918 struct imsm_super
*mpb
= super
->anchor
;
8919 struct imsm_map
*map
;
8920 unsigned long long pos
;
8925 __u32 array_start
= 0;
8926 __u32 array_end
= 0;
8928 struct mdinfo
*test_list
;
8930 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
8931 /* If in this array, skip */
8932 for (d
= a
->info
.devs
; d
; d
= d
->next
)
8933 if (is_fd_valid(d
->state_fd
) &&
8934 d
->disk
.major
== dl
->major
&&
8935 d
->disk
.minor
== dl
->minor
) {
8936 dprintf("%x:%x already in array\n",
8937 dl
->major
, dl
->minor
);
8942 test_list
= additional_test_list
;
8944 if (test_list
->disk
.major
== dl
->major
&&
8945 test_list
->disk
.minor
== dl
->minor
) {
8946 dprintf("%x:%x already in additional test list\n",
8947 dl
->major
, dl
->minor
);
8950 test_list
= test_list
->next
;
8955 /* skip in use or failed drives */
8956 if (is_failed(&dl
->disk
) || idx
== dl
->index
||
8958 dprintf("%x:%x status (failed: %d index: %d)\n",
8959 dl
->major
, dl
->minor
, is_failed(&dl
->disk
), idx
);
8963 /* skip pure spares when we are looking for partially
8964 * assimilated drives
8966 if (dl
->index
== -1 && !activate_new
)
8969 if (!drive_validate_sector_size(super
, dl
))
8972 /* Does this unused device have the requisite free space?
8973 * It needs to be able to cover all member volumes
8975 ex
= get_extents(super
, dl
, 1);
8977 dprintf("cannot get extents\n");
8980 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
8981 dev
= get_imsm_dev(super
, i
);
8982 map
= get_imsm_map(dev
, MAP_0
);
8984 /* check if this disk is already a member of
8987 if (get_imsm_disk_slot(map
, dl
->index
) >= 0)
8993 array_start
= pba_of_lba0(map
);
8994 array_end
= array_start
+
8995 per_dev_array_size(map
) - 1;
8998 /* check that we can start at pba_of_lba0 with
8999 * num_data_stripes*blocks_per_stripe of space
9001 if (array_start
>= pos
&& array_end
< ex
[j
].start
) {
9005 pos
= ex
[j
].start
+ ex
[j
].size
;
9007 } while (ex
[j
-1].size
);
9014 if (i
< mpb
->num_raid_devs
) {
9015 dprintf("%x:%x does not have %u to %u available\n",
9016 dl
->major
, dl
->minor
, array_start
, array_end
);
9026 static int imsm_rebuild_allowed(struct supertype
*cont
, int dev_idx
, int failed
)
9028 struct imsm_dev
*dev2
;
9029 struct imsm_map
*map
;
9035 dev2
= get_imsm_dev(cont
->sb
, dev_idx
);
9037 state
= imsm_check_degraded(cont
->sb
, dev2
, failed
, MAP_0
);
9038 if (state
== IMSM_T_STATE_FAILED
) {
9039 map
= get_imsm_map(dev2
, MAP_0
);
9042 for (slot
= 0; slot
< map
->num_members
; slot
++) {
9044 * Check if failed disks are deleted from intel
9045 * disk list or are marked to be deleted
9047 idx
= get_imsm_disk_idx(dev2
, slot
, MAP_X
);
9048 idisk
= get_imsm_dl_disk(cont
->sb
, idx
);
9050 * Do not rebuild the array if failed disks
9051 * from failed sub-array are not removed from
9055 is_failed(&idisk
->disk
) &&
9056 (idisk
->action
!= DISK_REMOVE
))
9064 static struct mdinfo
*imsm_activate_spare(struct active_array
*a
,
9065 struct metadata_update
**updates
)
9068 * Find a device with unused free space and use it to replace a
9069 * failed/vacant region in an array. We replace failed regions one a
9070 * array at a time. The result is that a new spare disk will be added
9071 * to the first failed array and after the monitor has finished
9072 * propagating failures the remainder will be consumed.
9074 * FIXME add a capability for mdmon to request spares from another
9078 struct intel_super
*super
= a
->container
->sb
;
9079 int inst
= a
->info
.container_member
;
9080 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
9081 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
9082 int failed
= a
->info
.array
.raid_disks
;
9083 struct mdinfo
*rv
= NULL
;
9086 struct metadata_update
*mu
;
9088 struct imsm_update_activate_spare
*u
;
9093 for (d
= a
->info
.devs
; d
; d
= d
->next
) {
9094 if (!is_fd_valid(d
->state_fd
))
9097 if (d
->curr_state
& DS_FAULTY
)
9098 /* wait for Removal to happen */
9104 dprintf("imsm: activate spare: inst=%d failed=%d (%d) level=%d\n",
9105 inst
, failed
, a
->info
.array
.raid_disks
, a
->info
.array
.level
);
9107 if (imsm_reshape_blocks_arrays_changes(super
))
9110 /* Cannot activate another spare if rebuild is in progress already
9112 if (is_rebuilding(dev
)) {
9113 dprintf("imsm: No spare activation allowed. Rebuild in progress already.\n");
9117 if (a
->info
.array
.level
== 4)
9118 /* No repair for takeovered array
9119 * imsm doesn't support raid4
9123 if (imsm_check_degraded(super
, dev
, failed
, MAP_0
) !=
9124 IMSM_T_STATE_DEGRADED
)
9127 if (get_imsm_map(dev
, MAP_0
)->map_state
== IMSM_T_STATE_UNINITIALIZED
) {
9128 dprintf("imsm: No spare activation allowed. Volume is not initialized.\n");
9133 * If there are any failed disks check state of the other volume.
9134 * Block rebuild if the another one is failed until failed disks
9135 * are removed from container.
9138 dprintf("found failed disks in %.*s, check if there anotherfailed sub-array.\n",
9139 MAX_RAID_SERIAL_LEN
, dev
->volume
);
9140 /* check if states of the other volumes allow for rebuild */
9141 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
9143 allowed
= imsm_rebuild_allowed(a
->container
,
9151 /* For each slot, if it is not working, find a spare */
9152 for (i
= 0; i
< a
->info
.array
.raid_disks
; i
++) {
9153 for (d
= a
->info
.devs
; d
; d
= d
->next
)
9154 if (d
->disk
.raid_disk
== i
)
9156 dprintf("found %d: %p %x\n", i
, d
, d
?d
->curr_state
:0);
9157 if (d
&& is_fd_valid(d
->state_fd
))
9161 * OK, this device needs recovery. Try to re-add the
9162 * previous occupant of this slot, if this fails see if
9163 * we can continue the assimilation of a spare that was
9164 * partially assimilated, finally try to activate a new
9167 dl
= imsm_readd(super
, i
, a
);
9169 dl
= imsm_add_spare(super
, i
, a
, 0, rv
);
9171 dl
= imsm_add_spare(super
, i
, a
, 1, rv
);
9175 /* found a usable disk with enough space */
9176 di
= xcalloc(1, sizeof(*di
));
9178 /* dl->index will be -1 in the case we are activating a
9179 * pristine spare. imsm_process_update() will create a
9180 * new index in this case. Once a disk is found to be
9181 * failed in all member arrays it is kicked from the
9184 di
->disk
.number
= dl
->index
;
9186 /* (ab)use di->devs to store a pointer to the device
9189 di
->devs
= (struct mdinfo
*) dl
;
9191 di
->disk
.raid_disk
= i
;
9192 di
->disk
.major
= dl
->major
;
9193 di
->disk
.minor
= dl
->minor
;
9195 di
->recovery_start
= 0;
9196 di
->data_offset
= pba_of_lba0(map
);
9197 di
->component_size
= a
->info
.component_size
;
9198 di
->container_member
= inst
;
9199 di
->bb
.supported
= 1;
9200 if (a
->info
.consistency_policy
== CONSISTENCY_POLICY_PPL
) {
9201 di
->ppl_sector
= get_ppl_sector(super
, inst
);
9202 di
->ppl_size
= MULTIPLE_PPL_AREA_SIZE_IMSM
>> 9;
9204 super
->random
= random32();
9208 dprintf("%x:%x to be %d at %llu\n", dl
->major
, dl
->minor
,
9209 i
, di
->data_offset
);
9213 /* No spares found */
9215 /* Now 'rv' has a list of devices to return.
9216 * Create a metadata_update record to update the
9217 * disk_ord_tbl for the array
9219 mu
= xmalloc(sizeof(*mu
));
9220 mu
->buf
= xcalloc(num_spares
,
9221 sizeof(struct imsm_update_activate_spare
));
9223 mu
->space_list
= NULL
;
9224 mu
->len
= sizeof(struct imsm_update_activate_spare
) * num_spares
;
9225 mu
->next
= *updates
;
9226 u
= (struct imsm_update_activate_spare
*) mu
->buf
;
9228 for (di
= rv
; di
; di
= di
->next
) {
9229 u
->type
= update_activate_spare
;
9230 u
->dl
= (struct dl
*) di
->devs
;
9232 u
->slot
= di
->disk
.raid_disk
;
9243 static int disks_overlap(struct intel_super
*super
, int idx
, struct imsm_update_create_array
*u
)
9245 struct imsm_dev
*dev
= get_imsm_dev(super
, idx
);
9246 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
9247 struct imsm_map
*new_map
= get_imsm_map(&u
->dev
, MAP_0
);
9248 struct disk_info
*inf
= get_disk_info(u
);
9249 struct imsm_disk
*disk
;
9253 for (i
= 0; i
< map
->num_members
; i
++) {
9254 disk
= get_imsm_disk(super
, get_imsm_disk_idx(dev
, i
, MAP_X
));
9255 for (j
= 0; j
< new_map
->num_members
; j
++)
9256 if (serialcmp(disk
->serial
, inf
[j
].serial
) == 0)
9263 static struct dl
*get_disk_super(struct intel_super
*super
, int major
, int minor
)
9267 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
9268 if (dl
->major
== major
&& dl
->minor
== minor
)
9273 static int remove_disk_super(struct intel_super
*super
, int major
, int minor
)
9279 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
9280 if (dl
->major
== major
&& dl
->minor
== minor
) {
9283 prev
->next
= dl
->next
;
9285 super
->disks
= dl
->next
;
9287 __free_imsm_disk(dl
, 1);
9288 dprintf("removed %x:%x\n", major
, minor
);
9296 static void imsm_delete(struct intel_super
*super
, struct dl
**dlp
, unsigned index
);
9298 static int add_remove_disk_update(struct intel_super
*super
)
9300 int check_degraded
= 0;
9303 /* add/remove some spares to/from the metadata/contrainer */
9304 while (super
->disk_mgmt_list
) {
9305 struct dl
*disk_cfg
;
9307 disk_cfg
= super
->disk_mgmt_list
;
9308 super
->disk_mgmt_list
= disk_cfg
->next
;
9309 disk_cfg
->next
= NULL
;
9311 if (disk_cfg
->action
== DISK_ADD
) {
9312 disk_cfg
->next
= super
->disks
;
9313 super
->disks
= disk_cfg
;
9315 dprintf("added %x:%x\n",
9316 disk_cfg
->major
, disk_cfg
->minor
);
9317 } else if (disk_cfg
->action
== DISK_REMOVE
) {
9318 dprintf("Disk remove action processed: %x.%x\n",
9319 disk_cfg
->major
, disk_cfg
->minor
);
9320 disk
= get_disk_super(super
,
9324 /* store action status */
9325 disk
->action
= DISK_REMOVE
;
9326 /* remove spare disks only */
9327 if (disk
->index
== -1) {
9328 remove_disk_super(super
,
9332 disk_cfg
->fd
= disk
->fd
;
9336 /* release allocate disk structure */
9337 __free_imsm_disk(disk_cfg
, 1);
9340 return check_degraded
;
9343 static int apply_reshape_migration_update(struct imsm_update_reshape_migration
*u
,
9344 struct intel_super
*super
,
9347 struct intel_dev
*id
;
9348 void **tofree
= NULL
;
9351 dprintf("(enter)\n");
9352 if (u
->subdev
< 0 || u
->subdev
> 1) {
9353 dprintf("imsm: Error: Wrong subdev: %i\n", u
->subdev
);
9356 if (space_list
== NULL
|| *space_list
== NULL
) {
9357 dprintf("imsm: Error: Memory is not allocated\n");
9361 for (id
= super
->devlist
; id
; id
= id
->next
) {
9362 if (id
->index
== (unsigned)u
->subdev
) {
9363 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->subdev
);
9364 struct imsm_map
*map
;
9365 struct imsm_dev
*new_dev
=
9366 (struct imsm_dev
*)*space_list
;
9367 struct imsm_map
*migr_map
= get_imsm_map(dev
, MAP_1
);
9369 struct dl
*new_disk
;
9371 if (new_dev
== NULL
)
9373 *space_list
= **space_list
;
9374 memcpy(new_dev
, dev
, sizeof_imsm_dev(dev
, 0));
9375 map
= get_imsm_map(new_dev
, MAP_0
);
9377 dprintf("imsm: Error: migration in progress");
9381 to_state
= map
->map_state
;
9382 if ((u
->new_level
== 5) && (map
->raid_level
== 0)) {
9384 /* this should not happen */
9385 if (u
->new_disks
[0] < 0) {
9386 map
->failed_disk_num
=
9387 map
->num_members
- 1;
9388 to_state
= IMSM_T_STATE_DEGRADED
;
9390 to_state
= IMSM_T_STATE_NORMAL
;
9392 migrate(new_dev
, super
, to_state
, MIGR_GEN_MIGR
);
9393 if (u
->new_level
> -1)
9394 map
->raid_level
= u
->new_level
;
9395 migr_map
= get_imsm_map(new_dev
, MAP_1
);
9396 if ((u
->new_level
== 5) &&
9397 (migr_map
->raid_level
== 0)) {
9398 int ord
= map
->num_members
- 1;
9399 migr_map
->num_members
--;
9400 if (u
->new_disks
[0] < 0)
9401 ord
|= IMSM_ORD_REBUILD
;
9402 set_imsm_ord_tbl_ent(map
,
9403 map
->num_members
- 1,
9407 tofree
= (void **)dev
;
9409 /* update chunk size
9411 if (u
->new_chunksize
> 0) {
9412 struct imsm_map
*dest_map
=
9413 get_imsm_map(dev
, MAP_0
);
9415 imsm_num_data_members(dest_map
);
9417 if (used_disks
== 0)
9420 map
->blocks_per_strip
=
9421 __cpu_to_le16(u
->new_chunksize
* 2);
9422 update_num_data_stripes(map
, imsm_dev_size(dev
));
9425 /* ensure blocks_per_member has valid value
9427 set_blocks_per_member(map
,
9428 per_dev_array_size(map
) +
9429 NUM_BLOCKS_DIRTY_STRIPE_REGION
);
9433 if (u
->new_level
!= 5 || migr_map
->raid_level
!= 0 ||
9434 migr_map
->raid_level
== map
->raid_level
)
9437 if (u
->new_disks
[0] >= 0) {
9440 new_disk
= get_disk_super(super
,
9441 major(u
->new_disks
[0]),
9442 minor(u
->new_disks
[0]));
9443 dprintf("imsm: new disk for reshape is: %i:%i (%p, index = %i)\n",
9444 major(u
->new_disks
[0]),
9445 minor(u
->new_disks
[0]),
9446 new_disk
, new_disk
->index
);
9447 if (new_disk
== NULL
)
9448 goto error_disk_add
;
9450 new_disk
->index
= map
->num_members
- 1;
9451 /* slot to fill in autolayout
9453 new_disk
->raiddisk
= new_disk
->index
;
9454 new_disk
->disk
.status
|= CONFIGURED_DISK
;
9455 new_disk
->disk
.status
&= ~SPARE_DISK
;
9457 goto error_disk_add
;
9460 *tofree
= *space_list
;
9461 /* calculate new size
9463 imsm_set_array_size(new_dev
, -1);
9470 *space_list
= tofree
;
9474 dprintf("Error: imsm: Cannot find disk.\n");
9478 static int apply_size_change_update(struct imsm_update_size_change
*u
,
9479 struct intel_super
*super
)
9481 struct intel_dev
*id
;
9484 dprintf("(enter)\n");
9485 if (u
->subdev
< 0 || u
->subdev
> 1) {
9486 dprintf("imsm: Error: Wrong subdev: %i\n", u
->subdev
);
9490 for (id
= super
->devlist
; id
; id
= id
->next
) {
9491 if (id
->index
== (unsigned)u
->subdev
) {
9492 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->subdev
);
9493 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
9494 int used_disks
= imsm_num_data_members(map
);
9495 unsigned long long blocks_per_member
;
9496 unsigned long long new_size_per_disk
;
9498 if (used_disks
== 0)
9501 /* calculate new size
9503 new_size_per_disk
= u
->new_size
/ used_disks
;
9504 blocks_per_member
= new_size_per_disk
+
9505 NUM_BLOCKS_DIRTY_STRIPE_REGION
;
9507 imsm_set_array_size(dev
, u
->new_size
);
9508 set_blocks_per_member(map
, blocks_per_member
);
9509 update_num_data_stripes(map
, u
->new_size
);
9518 static int prepare_spare_to_activate(struct supertype
*st
,
9519 struct imsm_update_activate_spare
*u
)
9521 struct intel_super
*super
= st
->sb
;
9522 int prev_current_vol
= super
->current_vol
;
9523 struct active_array
*a
;
9526 for (a
= st
->arrays
; a
; a
= a
->next
)
9528 * Additional initialization (adding bitmap header, filling
9529 * the bitmap area with '1's to force initial rebuild for a whole
9530 * data-area) is required when adding the spare to the volume
9531 * with write-intent bitmap.
9533 if (a
->info
.container_member
== u
->array
&&
9534 a
->info
.consistency_policy
== CONSISTENCY_POLICY_BITMAP
) {
9537 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
9543 super
->current_vol
= u
->array
;
9544 if (st
->ss
->write_bitmap(st
, dl
->fd
, NoUpdate
))
9546 super
->current_vol
= prev_current_vol
;
9551 static int apply_update_activate_spare(struct imsm_update_activate_spare
*u
,
9552 struct intel_super
*super
,
9553 struct active_array
*active_array
)
9555 struct imsm_super
*mpb
= super
->anchor
;
9556 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->array
);
9557 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
9558 struct imsm_map
*migr_map
;
9559 struct active_array
*a
;
9560 struct imsm_disk
*disk
;
9567 int second_map_created
= 0;
9569 for (; u
; u
= u
->next
) {
9570 victim
= get_imsm_disk_idx(dev
, u
->slot
, MAP_X
);
9575 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
9580 pr_err("error: imsm_activate_spare passed an unknown disk (index: %d)\n",
9585 /* count failures (excluding rebuilds and the victim)
9586 * to determine map[0] state
9589 for (i
= 0; i
< map
->num_members
; i
++) {
9592 disk
= get_imsm_disk(super
,
9593 get_imsm_disk_idx(dev
, i
, MAP_X
));
9594 if (!disk
|| is_failed(disk
))
9598 /* adding a pristine spare, assign a new index */
9599 if (dl
->index
< 0) {
9600 dl
->index
= super
->anchor
->num_disks
;
9601 super
->anchor
->num_disks
++;
9604 disk
->status
|= CONFIGURED_DISK
;
9605 disk
->status
&= ~SPARE_DISK
;
9608 to_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
9609 if (!second_map_created
) {
9610 second_map_created
= 1;
9611 map
->map_state
= IMSM_T_STATE_DEGRADED
;
9612 migrate(dev
, super
, to_state
, MIGR_REBUILD
);
9614 map
->map_state
= to_state
;
9615 migr_map
= get_imsm_map(dev
, MAP_1
);
9616 set_imsm_ord_tbl_ent(map
, u
->slot
, dl
->index
);
9617 set_imsm_ord_tbl_ent(migr_map
, u
->slot
,
9618 dl
->index
| IMSM_ORD_REBUILD
);
9620 /* update the family_num to mark a new container
9621 * generation, being careful to record the existing
9622 * family_num in orig_family_num to clean up after
9623 * earlier mdadm versions that neglected to set it.
9625 if (mpb
->orig_family_num
== 0)
9626 mpb
->orig_family_num
= mpb
->family_num
;
9627 mpb
->family_num
+= super
->random
;
9629 /* count arrays using the victim in the metadata */
9631 for (a
= active_array
; a
; a
= a
->next
) {
9632 dev
= get_imsm_dev(super
, a
->info
.container_member
);
9633 map
= get_imsm_map(dev
, MAP_0
);
9635 if (get_imsm_disk_slot(map
, victim
) >= 0)
9639 /* delete the victim if it is no longer being
9645 /* We know that 'manager' isn't touching anything,
9646 * so it is safe to delete
9648 for (dlp
= &super
->disks
; *dlp
; dlp
= &(*dlp
)->next
)
9649 if ((*dlp
)->index
== victim
)
9652 /* victim may be on the missing list */
9654 for (dlp
= &super
->missing
; *dlp
;
9655 dlp
= &(*dlp
)->next
)
9656 if ((*dlp
)->index
== victim
)
9658 imsm_delete(super
, dlp
, victim
);
9665 static int apply_reshape_container_disks_update(struct imsm_update_reshape
*u
,
9666 struct intel_super
*super
,
9669 struct dl
*new_disk
;
9670 struct intel_dev
*id
;
9672 int delta_disks
= u
->new_raid_disks
- u
->old_raid_disks
;
9673 int disk_count
= u
->old_raid_disks
;
9674 void **tofree
= NULL
;
9675 int devices_to_reshape
= 1;
9676 struct imsm_super
*mpb
= super
->anchor
;
9678 unsigned int dev_id
;
9680 dprintf("(enter)\n");
9682 /* enable spares to use in array */
9683 for (i
= 0; i
< delta_disks
; i
++) {
9684 new_disk
= get_disk_super(super
,
9685 major(u
->new_disks
[i
]),
9686 minor(u
->new_disks
[i
]));
9687 dprintf("imsm: new disk for reshape is: %i:%i (%p, index = %i)\n",
9688 major(u
->new_disks
[i
]), minor(u
->new_disks
[i
]),
9689 new_disk
, new_disk
->index
);
9690 if (new_disk
== NULL
||
9691 (new_disk
->index
>= 0 &&
9692 new_disk
->index
< u
->old_raid_disks
))
9693 goto update_reshape_exit
;
9694 new_disk
->index
= disk_count
++;
9695 /* slot to fill in autolayout
9697 new_disk
->raiddisk
= new_disk
->index
;
9698 new_disk
->disk
.status
|=
9700 new_disk
->disk
.status
&= ~SPARE_DISK
;
9703 dprintf("imsm: volume set mpb->num_raid_devs = %i\n",
9704 mpb
->num_raid_devs
);
9705 /* manage changes in volume
9707 for (dev_id
= 0; dev_id
< mpb
->num_raid_devs
; dev_id
++) {
9708 void **sp
= *space_list
;
9709 struct imsm_dev
*newdev
;
9710 struct imsm_map
*newmap
, *oldmap
;
9712 for (id
= super
->devlist
; id
; id
= id
->next
) {
9713 if (id
->index
== dev_id
)
9722 /* Copy the dev, but not (all of) the map */
9723 memcpy(newdev
, id
->dev
, sizeof(*newdev
));
9724 oldmap
= get_imsm_map(id
->dev
, MAP_0
);
9725 newmap
= get_imsm_map(newdev
, MAP_0
);
9726 /* Copy the current map */
9727 memcpy(newmap
, oldmap
, sizeof_imsm_map(oldmap
));
9728 /* update one device only
9730 if (devices_to_reshape
) {
9731 dprintf("imsm: modifying subdev: %i\n",
9733 devices_to_reshape
--;
9734 newdev
->vol
.migr_state
= 1;
9735 set_vol_curr_migr_unit(newdev
, 0);
9736 set_migr_type(newdev
, MIGR_GEN_MIGR
);
9737 newmap
->num_members
= u
->new_raid_disks
;
9738 for (i
= 0; i
< delta_disks
; i
++) {
9739 set_imsm_ord_tbl_ent(newmap
,
9740 u
->old_raid_disks
+ i
,
9741 u
->old_raid_disks
+ i
);
9743 /* New map is correct, now need to save old map
9745 newmap
= get_imsm_map(newdev
, MAP_1
);
9746 memcpy(newmap
, oldmap
, sizeof_imsm_map(oldmap
));
9748 imsm_set_array_size(newdev
, -1);
9751 sp
= (void **)id
->dev
;
9756 /* Clear migration record */
9757 memset(super
->migr_rec
, 0, sizeof(struct migr_record
));
9760 *space_list
= tofree
;
9763 update_reshape_exit
:
9768 static int apply_takeover_update(struct imsm_update_takeover
*u
,
9769 struct intel_super
*super
,
9772 struct imsm_dev
*dev
= NULL
;
9773 struct intel_dev
*dv
;
9774 struct imsm_dev
*dev_new
;
9775 struct imsm_map
*map
;
9779 for (dv
= super
->devlist
; dv
; dv
= dv
->next
)
9780 if (dv
->index
== (unsigned int)u
->subarray
) {
9788 map
= get_imsm_map(dev
, MAP_0
);
9790 if (u
->direction
== R10_TO_R0
) {
9791 /* Number of failed disks must be half of initial disk number */
9792 if (imsm_count_failed(super
, dev
, MAP_0
) !=
9793 (map
->num_members
/ 2))
9796 /* iterate through devices to mark removed disks as spare */
9797 for (dm
= super
->disks
; dm
; dm
= dm
->next
) {
9798 if (dm
->disk
.status
& FAILED_DISK
) {
9799 int idx
= dm
->index
;
9800 /* update indexes on the disk list */
9801 /* FIXME this loop-with-the-loop looks wrong, I'm not convinced
9802 the index values will end up being correct.... NB */
9803 for (du
= super
->disks
; du
; du
= du
->next
)
9804 if (du
->index
> idx
)
9806 /* mark as spare disk */
9811 map
->num_members
/= map
->num_domains
;
9812 map
->map_state
= IMSM_T_STATE_NORMAL
;
9813 map
->raid_level
= 0;
9814 set_num_domains(map
);
9815 update_num_data_stripes(map
, imsm_dev_size(dev
));
9816 map
->failed_disk_num
= -1;
9819 if (u
->direction
== R0_TO_R10
) {
9822 /* update slots in current disk list */
9823 for (dm
= super
->disks
; dm
; dm
= dm
->next
) {
9827 /* create new *missing* disks */
9828 for (i
= 0; i
< map
->num_members
; i
++) {
9829 space
= *space_list
;
9832 *space_list
= *space
;
9834 memcpy(du
, super
->disks
, sizeof(*du
));
9838 du
->index
= (i
* 2) + 1;
9839 sprintf((char *)du
->disk
.serial
,
9840 " MISSING_%d", du
->index
);
9841 sprintf((char *)du
->serial
,
9842 "MISSING_%d", du
->index
);
9843 du
->next
= super
->missing
;
9844 super
->missing
= du
;
9846 /* create new dev and map */
9847 space
= *space_list
;
9850 *space_list
= *space
;
9851 dev_new
= (void *)space
;
9852 memcpy(dev_new
, dev
, sizeof(*dev
));
9853 /* update new map */
9854 map
= get_imsm_map(dev_new
, MAP_0
);
9856 map
->map_state
= IMSM_T_STATE_DEGRADED
;
9857 map
->raid_level
= 1;
9858 set_num_domains(map
);
9859 map
->num_members
= map
->num_members
* map
->num_domains
;
9860 update_num_data_stripes(map
, imsm_dev_size(dev
));
9862 /* replace dev<->dev_new */
9865 /* update disk order table */
9866 for (du
= super
->disks
; du
; du
= du
->next
)
9868 set_imsm_ord_tbl_ent(map
, du
->index
, du
->index
);
9869 for (du
= super
->missing
; du
; du
= du
->next
)
9870 if (du
->index
>= 0) {
9871 set_imsm_ord_tbl_ent(map
, du
->index
, du
->index
);
9872 mark_missing(super
, dv
->dev
, &du
->disk
, du
->index
);
9878 static void imsm_process_update(struct supertype
*st
,
9879 struct metadata_update
*update
)
9882 * crack open the metadata_update envelope to find the update record
9883 * update can be one of:
9884 * update_reshape_container_disks - all the arrays in the container
9885 * are being reshaped to have more devices. We need to mark
9886 * the arrays for general migration and convert selected spares
9887 * into active devices.
9888 * update_activate_spare - a spare device has replaced a failed
9889 * device in an array, update the disk_ord_tbl. If this disk is
9890 * present in all member arrays then also clear the SPARE_DISK
9892 * update_create_array
9894 * update_rename_array
9895 * update_add_remove_disk
9897 struct intel_super
*super
= st
->sb
;
9898 struct imsm_super
*mpb
;
9899 enum imsm_update_type type
= *(enum imsm_update_type
*) update
->buf
;
9901 /* update requires a larger buf but the allocation failed */
9902 if (super
->next_len
&& !super
->next_buf
) {
9903 super
->next_len
= 0;
9907 if (super
->next_buf
) {
9908 memcpy(super
->next_buf
, super
->buf
, super
->len
);
9910 super
->len
= super
->next_len
;
9911 super
->buf
= super
->next_buf
;
9913 super
->next_len
= 0;
9914 super
->next_buf
= NULL
;
9917 mpb
= super
->anchor
;
9920 case update_general_migration_checkpoint
: {
9921 struct intel_dev
*id
;
9922 struct imsm_update_general_migration_checkpoint
*u
=
9923 (void *)update
->buf
;
9925 dprintf("called for update_general_migration_checkpoint\n");
9927 /* find device under general migration */
9928 for (id
= super
->devlist
; id
; id
= id
->next
) {
9929 if (is_gen_migration(id
->dev
)) {
9930 set_vol_curr_migr_unit(id
->dev
,
9932 super
->updates_pending
++;
9937 case update_takeover
: {
9938 struct imsm_update_takeover
*u
= (void *)update
->buf
;
9939 if (apply_takeover_update(u
, super
, &update
->space_list
)) {
9940 imsm_update_version_info(super
);
9941 super
->updates_pending
++;
9946 case update_reshape_container_disks
: {
9947 struct imsm_update_reshape
*u
= (void *)update
->buf
;
9948 if (apply_reshape_container_disks_update(
9949 u
, super
, &update
->space_list
))
9950 super
->updates_pending
++;
9953 case update_reshape_migration
: {
9954 struct imsm_update_reshape_migration
*u
= (void *)update
->buf
;
9955 if (apply_reshape_migration_update(
9956 u
, super
, &update
->space_list
))
9957 super
->updates_pending
++;
9960 case update_size_change
: {
9961 struct imsm_update_size_change
*u
= (void *)update
->buf
;
9962 if (apply_size_change_update(u
, super
))
9963 super
->updates_pending
++;
9966 case update_activate_spare
: {
9967 struct imsm_update_activate_spare
*u
= (void *) update
->buf
;
9969 if (prepare_spare_to_activate(st
, u
) &&
9970 apply_update_activate_spare(u
, super
, st
->arrays
))
9971 super
->updates_pending
++;
9974 case update_create_array
: {
9975 /* someone wants to create a new array, we need to be aware of
9976 * a few races/collisions:
9977 * 1/ 'Create' called by two separate instances of mdadm
9978 * 2/ 'Create' versus 'activate_spare': mdadm has chosen
9979 * devices that have since been assimilated via
9981 * In the event this update can not be carried out mdadm will
9982 * (FIX ME) notice that its update did not take hold.
9984 struct imsm_update_create_array
*u
= (void *) update
->buf
;
9985 struct intel_dev
*dv
;
9986 struct imsm_dev
*dev
;
9987 struct imsm_map
*map
, *new_map
;
9988 unsigned long long start
, end
;
9989 unsigned long long new_start
, new_end
;
9991 struct disk_info
*inf
;
9994 /* handle racing creates: first come first serve */
9995 if (u
->dev_idx
< mpb
->num_raid_devs
) {
9996 dprintf("subarray %d already defined\n", u
->dev_idx
);
10000 /* check update is next in sequence */
10001 if (u
->dev_idx
!= mpb
->num_raid_devs
) {
10002 dprintf("can not create array %d expected index %d\n",
10003 u
->dev_idx
, mpb
->num_raid_devs
);
10007 new_map
= get_imsm_map(&u
->dev
, MAP_0
);
10008 new_start
= pba_of_lba0(new_map
);
10009 new_end
= new_start
+ per_dev_array_size(new_map
);
10010 inf
= get_disk_info(u
);
10012 /* handle activate_spare versus create race:
10013 * check to make sure that overlapping arrays do not include
10014 * overalpping disks
10016 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
10017 dev
= get_imsm_dev(super
, i
);
10018 map
= get_imsm_map(dev
, MAP_0
);
10019 start
= pba_of_lba0(map
);
10020 end
= start
+ per_dev_array_size(map
);
10021 if ((new_start
>= start
&& new_start
<= end
) ||
10022 (start
>= new_start
&& start
<= new_end
))
10027 if (disks_overlap(super
, i
, u
)) {
10028 dprintf("arrays overlap\n");
10033 /* check that prepare update was successful */
10034 if (!update
->space
) {
10035 dprintf("prepare update failed\n");
10039 /* check that all disks are still active before committing
10040 * changes. FIXME: could we instead handle this by creating a
10041 * degraded array? That's probably not what the user expects,
10042 * so better to drop this update on the floor.
10044 for (i
= 0; i
< new_map
->num_members
; i
++) {
10045 dl
= serial_to_dl(inf
[i
].serial
, super
);
10047 dprintf("disk disappeared\n");
10052 super
->updates_pending
++;
10054 /* convert spares to members and fixup ord_tbl */
10055 for (i
= 0; i
< new_map
->num_members
; i
++) {
10056 dl
= serial_to_dl(inf
[i
].serial
, super
);
10057 if (dl
->index
== -1) {
10058 dl
->index
= mpb
->num_disks
;
10060 dl
->disk
.status
|= CONFIGURED_DISK
;
10061 dl
->disk
.status
&= ~SPARE_DISK
;
10063 set_imsm_ord_tbl_ent(new_map
, i
, dl
->index
);
10066 dv
= update
->space
;
10068 update
->space
= NULL
;
10069 imsm_copy_dev(dev
, &u
->dev
);
10070 dv
->index
= u
->dev_idx
;
10071 dv
->next
= super
->devlist
;
10072 super
->devlist
= dv
;
10073 mpb
->num_raid_devs
++;
10075 imsm_update_version_info(super
);
10078 /* mdmon knows how to release update->space, but not
10079 * ((struct intel_dev *) update->space)->dev
10081 if (update
->space
) {
10082 dv
= update
->space
;
10087 case update_kill_array
: {
10088 struct imsm_update_kill_array
*u
= (void *) update
->buf
;
10089 int victim
= u
->dev_idx
;
10090 struct active_array
*a
;
10091 struct intel_dev
**dp
;
10092 struct imsm_dev
*dev
;
10094 /* sanity check that we are not affecting the uuid of
10095 * active arrays, or deleting an active array
10097 * FIXME when immutable ids are available, but note that
10098 * we'll also need to fixup the invalidated/active
10099 * subarray indexes in mdstat
10101 for (a
= st
->arrays
; a
; a
= a
->next
)
10102 if (a
->info
.container_member
>= victim
)
10104 /* by definition if mdmon is running at least one array
10105 * is active in the container, so checking
10106 * mpb->num_raid_devs is just extra paranoia
10108 dev
= get_imsm_dev(super
, victim
);
10109 if (a
|| !dev
|| mpb
->num_raid_devs
== 1) {
10110 dprintf("failed to delete subarray-%d\n", victim
);
10114 for (dp
= &super
->devlist
; *dp
;)
10115 if ((*dp
)->index
== (unsigned)super
->current_vol
) {
10118 if ((*dp
)->index
> (unsigned)victim
)
10122 mpb
->num_raid_devs
--;
10123 super
->updates_pending
++;
10126 case update_rename_array
: {
10127 struct imsm_update_rename_array
*u
= (void *) update
->buf
;
10128 char name
[MAX_RAID_SERIAL_LEN
+1];
10129 int target
= u
->dev_idx
;
10130 struct active_array
*a
;
10131 struct imsm_dev
*dev
;
10133 /* sanity check that we are not affecting the uuid of
10136 memset(name
, 0, sizeof(name
));
10137 snprintf(name
, MAX_RAID_SERIAL_LEN
, "%s", (char *) u
->name
);
10138 name
[MAX_RAID_SERIAL_LEN
] = '\0';
10139 for (a
= st
->arrays
; a
; a
= a
->next
)
10140 if (a
->info
.container_member
== target
)
10142 dev
= get_imsm_dev(super
, u
->dev_idx
);
10143 if (a
|| !dev
|| !check_name(super
, name
, 1)) {
10144 dprintf("failed to rename subarray-%d\n", target
);
10148 memcpy(dev
->volume
, name
, MAX_RAID_SERIAL_LEN
);
10149 super
->updates_pending
++;
10152 case update_add_remove_disk
: {
10153 /* we may be able to repair some arrays if disks are
10154 * being added, check the status of add_remove_disk
10155 * if discs has been added.
10157 if (add_remove_disk_update(super
)) {
10158 struct active_array
*a
;
10160 super
->updates_pending
++;
10161 for (a
= st
->arrays
; a
; a
= a
->next
)
10162 a
->check_degraded
= 1;
10166 case update_prealloc_badblocks_mem
:
10168 case update_rwh_policy
: {
10169 struct imsm_update_rwh_policy
*u
= (void *)update
->buf
;
10170 int target
= u
->dev_idx
;
10171 struct imsm_dev
*dev
= get_imsm_dev(super
, target
);
10173 dprintf("could not find subarray-%d\n", target
);
10177 if (dev
->rwh_policy
!= u
->new_policy
) {
10178 dev
->rwh_policy
= u
->new_policy
;
10179 super
->updates_pending
++;
10184 pr_err("error: unsupported process update type:(type: %d)\n", type
);
10188 static struct mdinfo
*get_spares_for_grow(struct supertype
*st
);
10190 static int imsm_prepare_update(struct supertype
*st
,
10191 struct metadata_update
*update
)
10194 * Allocate space to hold new disk entries, raid-device entries or a new
10195 * mpb if necessary. The manager synchronously waits for updates to
10196 * complete in the monitor, so new mpb buffers allocated here can be
10197 * integrated by the monitor thread without worrying about live pointers
10198 * in the manager thread.
10200 enum imsm_update_type type
;
10201 struct intel_super
*super
= st
->sb
;
10202 unsigned int sector_size
= super
->sector_size
;
10203 struct imsm_super
*mpb
= super
->anchor
;
10207 if (update
->len
< (int)sizeof(type
))
10210 type
= *(enum imsm_update_type
*) update
->buf
;
10213 case update_general_migration_checkpoint
:
10214 if (update
->len
< (int)sizeof(struct imsm_update_general_migration_checkpoint
))
10216 dprintf("called for update_general_migration_checkpoint\n");
10218 case update_takeover
: {
10219 struct imsm_update_takeover
*u
= (void *)update
->buf
;
10220 if (update
->len
< (int)sizeof(*u
))
10222 if (u
->direction
== R0_TO_R10
) {
10223 void **tail
= (void **)&update
->space_list
;
10224 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->subarray
);
10225 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
10226 int num_members
= map
->num_members
;
10229 /* allocate memory for added disks */
10230 for (i
= 0; i
< num_members
; i
++) {
10231 size
= sizeof(struct dl
);
10232 space
= xmalloc(size
);
10237 /* allocate memory for new device */
10238 size
= sizeof_imsm_dev(super
->devlist
->dev
, 0) +
10239 (num_members
* sizeof(__u32
));
10240 space
= xmalloc(size
);
10244 len
= disks_to_mpb_size(num_members
* 2);
10249 case update_reshape_container_disks
: {
10250 /* Every raid device in the container is about to
10251 * gain some more devices, and we will enter a
10253 * So each 'imsm_map' will be bigger, and the imsm_vol
10254 * will now hold 2 of them.
10255 * Thus we need new 'struct imsm_dev' allocations sized
10256 * as sizeof_imsm_dev but with more devices in both maps.
10258 struct imsm_update_reshape
*u
= (void *)update
->buf
;
10259 struct intel_dev
*dl
;
10260 void **space_tail
= (void**)&update
->space_list
;
10262 if (update
->len
< (int)sizeof(*u
))
10265 dprintf("for update_reshape\n");
10267 for (dl
= super
->devlist
; dl
; dl
= dl
->next
) {
10268 int size
= sizeof_imsm_dev(dl
->dev
, 1);
10270 if (u
->new_raid_disks
> u
->old_raid_disks
)
10271 size
+= sizeof(__u32
)*2*
10272 (u
->new_raid_disks
- u
->old_raid_disks
);
10276 *space_tail
= NULL
;
10279 len
= disks_to_mpb_size(u
->new_raid_disks
);
10280 dprintf("New anchor length is %llu\n", (unsigned long long)len
);
10283 case update_reshape_migration
: {
10284 /* for migration level 0->5 we need to add disks
10285 * so the same as for container operation we will copy
10286 * device to the bigger location.
10287 * in memory prepared device and new disk area are prepared
10288 * for usage in process update
10290 struct imsm_update_reshape_migration
*u
= (void *)update
->buf
;
10291 struct intel_dev
*id
;
10292 void **space_tail
= (void **)&update
->space_list
;
10295 int current_level
= -1;
10297 if (update
->len
< (int)sizeof(*u
))
10300 dprintf("for update_reshape\n");
10302 /* add space for bigger array in update
10304 for (id
= super
->devlist
; id
; id
= id
->next
) {
10305 if (id
->index
== (unsigned)u
->subdev
) {
10306 size
= sizeof_imsm_dev(id
->dev
, 1);
10307 if (u
->new_raid_disks
> u
->old_raid_disks
)
10308 size
+= sizeof(__u32
)*2*
10309 (u
->new_raid_disks
- u
->old_raid_disks
);
10313 *space_tail
= NULL
;
10317 if (update
->space_list
== NULL
)
10320 /* add space for disk in update
10322 size
= sizeof(struct dl
);
10326 *space_tail
= NULL
;
10328 /* add spare device to update
10330 for (id
= super
->devlist
; id
; id
= id
->next
)
10331 if (id
->index
== (unsigned)u
->subdev
) {
10332 struct imsm_dev
*dev
;
10333 struct imsm_map
*map
;
10335 dev
= get_imsm_dev(super
, u
->subdev
);
10336 map
= get_imsm_map(dev
, MAP_0
);
10337 current_level
= map
->raid_level
;
10340 if (u
->new_level
== 5 && u
->new_level
!= current_level
) {
10341 struct mdinfo
*spares
;
10343 spares
= get_spares_for_grow(st
);
10346 struct mdinfo
*dev
;
10348 dev
= spares
->devs
;
10351 makedev(dev
->disk
.major
,
10353 dl
= get_disk_super(super
,
10356 dl
->index
= u
->old_raid_disks
;
10359 sysfs_free(spares
);
10362 len
= disks_to_mpb_size(u
->new_raid_disks
);
10363 dprintf("New anchor length is %llu\n", (unsigned long long)len
);
10366 case update_size_change
: {
10367 if (update
->len
< (int)sizeof(struct imsm_update_size_change
))
10371 case update_activate_spare
: {
10372 if (update
->len
< (int)sizeof(struct imsm_update_activate_spare
))
10376 case update_create_array
: {
10377 struct imsm_update_create_array
*u
= (void *) update
->buf
;
10378 struct intel_dev
*dv
;
10379 struct imsm_dev
*dev
= &u
->dev
;
10380 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
10382 struct disk_info
*inf
;
10386 if (update
->len
< (int)sizeof(*u
))
10389 inf
= get_disk_info(u
);
10390 len
= sizeof_imsm_dev(dev
, 1);
10391 /* allocate a new super->devlist entry */
10392 dv
= xmalloc(sizeof(*dv
));
10393 dv
->dev
= xmalloc(len
);
10394 update
->space
= dv
;
10396 /* count how many spares will be converted to members */
10397 for (i
= 0; i
< map
->num_members
; i
++) {
10398 dl
= serial_to_dl(inf
[i
].serial
, super
);
10400 /* hmm maybe it failed?, nothing we can do about
10405 if (count_memberships(dl
, super
) == 0)
10408 len
+= activate
* sizeof(struct imsm_disk
);
10411 case update_kill_array
: {
10412 if (update
->len
< (int)sizeof(struct imsm_update_kill_array
))
10416 case update_rename_array
: {
10417 if (update
->len
< (int)sizeof(struct imsm_update_rename_array
))
10421 case update_add_remove_disk
:
10422 /* no update->len needed */
10424 case update_prealloc_badblocks_mem
:
10425 super
->extra_space
+= sizeof(struct bbm_log
) -
10426 get_imsm_bbm_log_size(super
->bbm_log
);
10428 case update_rwh_policy
: {
10429 if (update
->len
< (int)sizeof(struct imsm_update_rwh_policy
))
10437 /* check if we need a larger metadata buffer */
10438 if (super
->next_buf
)
10439 buf_len
= super
->next_len
;
10441 buf_len
= super
->len
;
10443 if (__le32_to_cpu(mpb
->mpb_size
) + super
->extra_space
+ len
> buf_len
) {
10444 /* ok we need a larger buf than what is currently allocated
10445 * if this allocation fails process_update will notice that
10446 * ->next_len is set and ->next_buf is NULL
10448 buf_len
= ROUND_UP(__le32_to_cpu(mpb
->mpb_size
) +
10449 super
->extra_space
+ len
, sector_size
);
10450 if (super
->next_buf
)
10451 free(super
->next_buf
);
10453 super
->next_len
= buf_len
;
10454 if (posix_memalign(&super
->next_buf
, sector_size
, buf_len
) == 0)
10455 memset(super
->next_buf
, 0, buf_len
);
10457 super
->next_buf
= NULL
;
10462 /* must be called while manager is quiesced */
10463 static void imsm_delete(struct intel_super
*super
, struct dl
**dlp
, unsigned index
)
10465 struct imsm_super
*mpb
= super
->anchor
;
10467 struct imsm_dev
*dev
;
10468 struct imsm_map
*map
;
10469 unsigned int i
, j
, num_members
;
10470 __u32 ord
, ord_map0
;
10471 struct bbm_log
*log
= super
->bbm_log
;
10473 dprintf("deleting device[%d] from imsm_super\n", index
);
10475 /* shift all indexes down one */
10476 for (iter
= super
->disks
; iter
; iter
= iter
->next
)
10477 if (iter
->index
> (int)index
)
10479 for (iter
= super
->missing
; iter
; iter
= iter
->next
)
10480 if (iter
->index
> (int)index
)
10483 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
10484 dev
= get_imsm_dev(super
, i
);
10485 map
= get_imsm_map(dev
, MAP_0
);
10486 num_members
= map
->num_members
;
10487 for (j
= 0; j
< num_members
; j
++) {
10488 /* update ord entries being careful not to propagate
10489 * ord-flags to the first map
10491 ord
= get_imsm_ord_tbl_ent(dev
, j
, MAP_X
);
10492 ord_map0
= get_imsm_ord_tbl_ent(dev
, j
, MAP_0
);
10494 if (ord_to_idx(ord
) <= index
)
10497 map
= get_imsm_map(dev
, MAP_0
);
10498 set_imsm_ord_tbl_ent(map
, j
, ord_map0
- 1);
10499 map
= get_imsm_map(dev
, MAP_1
);
10501 set_imsm_ord_tbl_ent(map
, j
, ord
- 1);
10505 for (i
= 0; i
< log
->entry_count
; i
++) {
10506 struct bbm_log_entry
*entry
= &log
->marked_block_entries
[i
];
10508 if (entry
->disk_ordinal
<= index
)
10510 entry
->disk_ordinal
--;
10514 super
->updates_pending
++;
10516 struct dl
*dl
= *dlp
;
10518 *dlp
= (*dlp
)->next
;
10519 __free_imsm_disk(dl
, 1);
10523 static int imsm_get_allowed_degradation(int level
, int raid_disks
,
10524 struct intel_super
*super
,
10525 struct imsm_dev
*dev
)
10531 struct imsm_map
*map
;
10534 ret_val
= raid_disks
/2;
10535 /* check map if all disks pairs not failed
10538 map
= get_imsm_map(dev
, MAP_0
);
10539 for (i
= 0; i
< ret_val
; i
++) {
10540 int degradation
= 0;
10541 if (get_imsm_disk(super
, i
) == NULL
)
10543 if (get_imsm_disk(super
, i
+ 1) == NULL
)
10545 if (degradation
== 2)
10548 map
= get_imsm_map(dev
, MAP_1
);
10549 /* if there is no second map
10550 * result can be returned
10554 /* check degradation in second map
10556 for (i
= 0; i
< ret_val
; i
++) {
10557 int degradation
= 0;
10558 if (get_imsm_disk(super
, i
) == NULL
)
10560 if (get_imsm_disk(super
, i
+ 1) == NULL
)
10562 if (degradation
== 2)
10576 /*******************************************************************************
10577 * Function: validate_container_imsm
10578 * Description: This routine validates container after assemble,
10579 * eg. if devices in container are under the same controller.
10582 * info : linked list with info about devices used in array
10586 ******************************************************************************/
10587 int validate_container_imsm(struct mdinfo
*info
)
10589 if (check_env("IMSM_NO_PLATFORM"))
10592 struct sys_dev
*idev
;
10593 struct sys_dev
*hba
= NULL
;
10594 struct sys_dev
*intel_devices
= find_intel_devices();
10595 char *dev_path
= devt_to_devpath(makedev(info
->disk
.major
,
10596 info
->disk
.minor
), 1, NULL
);
10598 for (idev
= intel_devices
; idev
; idev
= idev
->next
) {
10599 if (dev_path
&& strstr(dev_path
, idev
->path
)) {
10608 pr_err("WARNING - Cannot detect HBA for device %s!\n",
10609 devid2kname(makedev(info
->disk
.major
, info
->disk
.minor
)));
10613 const struct imsm_orom
*orom
= get_orom_by_device_id(hba
->dev_id
);
10614 struct mdinfo
*dev
;
10616 for (dev
= info
->next
; dev
; dev
= dev
->next
) {
10617 dev_path
= devt_to_devpath(makedev(dev
->disk
.major
,
10618 dev
->disk
.minor
), 1, NULL
);
10620 struct sys_dev
*hba2
= NULL
;
10621 for (idev
= intel_devices
; idev
; idev
= idev
->next
) {
10622 if (dev_path
&& strstr(dev_path
, idev
->path
)) {
10630 const struct imsm_orom
*orom2
= hba2
== NULL
? NULL
:
10631 get_orom_by_device_id(hba2
->dev_id
);
10633 if (hba2
&& hba
->type
!= hba2
->type
) {
10634 pr_err("WARNING - HBAs of devices do not match %s != %s\n",
10635 get_sys_dev_type(hba
->type
), get_sys_dev_type(hba2
->type
));
10639 if (orom
!= orom2
) {
10640 pr_err("WARNING - IMSM container assembled with disks under different HBAs!\n"
10641 " This operation is not supported and can lead to data loss.\n");
10646 pr_err("WARNING - IMSM container assembled with disks under HBAs without IMSM platform support!\n"
10647 " This operation is not supported and can lead to data loss.\n");
10655 /*******************************************************************************
10656 * Function: imsm_record_badblock
10657 * Description: This routine stores new bad block record in BBM log
10660 * a : array containing a bad block
10661 * slot : disk number containing a bad block
10662 * sector : bad block sector
10663 * length : bad block sectors range
10667 ******************************************************************************/
10668 static int imsm_record_badblock(struct active_array
*a
, int slot
,
10669 unsigned long long sector
, int length
)
10671 struct intel_super
*super
= a
->container
->sb
;
10675 ord
= imsm_disk_slot_to_ord(a
, slot
);
10679 ret
= record_new_badblock(super
->bbm_log
, ord_to_idx(ord
), sector
,
10682 super
->updates_pending
++;
10686 /*******************************************************************************
10687 * Function: imsm_clear_badblock
10688 * Description: This routine clears bad block record from BBM log
10691 * a : array containing a bad block
10692 * slot : disk number containing a bad block
10693 * sector : bad block sector
10694 * length : bad block sectors range
10698 ******************************************************************************/
10699 static int imsm_clear_badblock(struct active_array
*a
, int slot
,
10700 unsigned long long sector
, int length
)
10702 struct intel_super
*super
= a
->container
->sb
;
10706 ord
= imsm_disk_slot_to_ord(a
, slot
);
10710 ret
= clear_badblock(super
->bbm_log
, ord_to_idx(ord
), sector
, length
);
10712 super
->updates_pending
++;
10716 /*******************************************************************************
10717 * Function: imsm_get_badblocks
10718 * Description: This routine get list of bad blocks for an array
10722 * slot : disk number
10724 * bb : structure containing bad blocks
10726 ******************************************************************************/
10727 static struct md_bb
*imsm_get_badblocks(struct active_array
*a
, int slot
)
10729 int inst
= a
->info
.container_member
;
10730 struct intel_super
*super
= a
->container
->sb
;
10731 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
10732 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
10735 ord
= imsm_disk_slot_to_ord(a
, slot
);
10739 get_volume_badblocks(super
->bbm_log
, ord_to_idx(ord
), pba_of_lba0(map
),
10740 per_dev_array_size(map
), &super
->bb
);
10744 /*******************************************************************************
10745 * Function: examine_badblocks_imsm
10746 * Description: Prints list of bad blocks on a disk to the standard output
10749 * st : metadata handler
10750 * fd : open file descriptor for device
10751 * devname : device name
10755 ******************************************************************************/
10756 static int examine_badblocks_imsm(struct supertype
*st
, int fd
, char *devname
)
10758 struct intel_super
*super
= st
->sb
;
10759 struct bbm_log
*log
= super
->bbm_log
;
10760 struct dl
*d
= NULL
;
10763 for (d
= super
->disks
; d
; d
= d
->next
) {
10764 if (strcmp(d
->devname
, devname
) == 0)
10768 if ((d
== NULL
) || (d
->index
< 0)) { /* serial mismatch probably */
10769 pr_err("%s doesn't appear to be part of a raid array\n",
10776 struct bbm_log_entry
*entry
= &log
->marked_block_entries
[0];
10778 for (i
= 0; i
< log
->entry_count
; i
++) {
10779 if (entry
[i
].disk_ordinal
== d
->index
) {
10780 unsigned long long sector
= __le48_to_cpu(
10781 &entry
[i
].defective_block_start
);
10782 int cnt
= entry
[i
].marked_count
+ 1;
10785 printf("Bad-blocks on %s:\n", devname
);
10789 printf("%20llu for %d sectors\n", sector
, cnt
);
10795 printf("No bad-blocks list configured on %s\n", devname
);
10799 /*******************************************************************************
10800 * Function: init_migr_record_imsm
10801 * Description: Function inits imsm migration record
10803 * super : imsm internal array info
10804 * dev : device under migration
10805 * info : general array info to find the smallest device
10808 ******************************************************************************/
10809 void init_migr_record_imsm(struct supertype
*st
, struct imsm_dev
*dev
,
10810 struct mdinfo
*info
)
10812 struct intel_super
*super
= st
->sb
;
10813 struct migr_record
*migr_rec
= super
->migr_rec
;
10814 int new_data_disks
;
10815 unsigned long long dsize
, dev_sectors
;
10816 long long unsigned min_dev_sectors
= -1LLU;
10817 struct imsm_map
*map_dest
= get_imsm_map(dev
, MAP_0
);
10818 struct imsm_map
*map_src
= get_imsm_map(dev
, MAP_1
);
10819 unsigned long long num_migr_units
;
10820 unsigned long long array_blocks
;
10821 struct dl
*dl_disk
= NULL
;
10823 memset(migr_rec
, 0, sizeof(struct migr_record
));
10824 migr_rec
->family_num
= __cpu_to_le32(super
->anchor
->family_num
);
10826 /* only ascending reshape supported now */
10827 migr_rec
->ascending_migr
= __cpu_to_le32(1);
10829 migr_rec
->dest_depth_per_unit
= GEN_MIGR_AREA_SIZE
/
10830 max(map_dest
->blocks_per_strip
, map_src
->blocks_per_strip
);
10831 migr_rec
->dest_depth_per_unit
*=
10832 max(map_dest
->blocks_per_strip
, map_src
->blocks_per_strip
);
10833 new_data_disks
= imsm_num_data_members(map_dest
);
10834 migr_rec
->blocks_per_unit
=
10835 __cpu_to_le32(migr_rec
->dest_depth_per_unit
* new_data_disks
);
10836 migr_rec
->dest_depth_per_unit
=
10837 __cpu_to_le32(migr_rec
->dest_depth_per_unit
);
10838 array_blocks
= info
->component_size
* new_data_disks
;
10840 array_blocks
/ __le32_to_cpu(migr_rec
->blocks_per_unit
);
10842 if (array_blocks
% __le32_to_cpu(migr_rec
->blocks_per_unit
))
10844 set_num_migr_units(migr_rec
, num_migr_units
);
10846 migr_rec
->post_migr_vol_cap
= dev
->size_low
;
10847 migr_rec
->post_migr_vol_cap_hi
= dev
->size_high
;
10849 /* Find the smallest dev */
10850 for (dl_disk
= super
->disks
; dl_disk
; dl_disk
= dl_disk
->next
) {
10851 /* ignore spares in container */
10852 if (dl_disk
->index
< 0)
10854 get_dev_size(dl_disk
->fd
, NULL
, &dsize
);
10855 dev_sectors
= dsize
/ 512;
10856 if (dev_sectors
< min_dev_sectors
)
10857 min_dev_sectors
= dev_sectors
;
10859 set_migr_chkp_area_pba(migr_rec
, min_dev_sectors
-
10860 RAID_DISK_RESERVED_BLOCKS_IMSM_HI
);
10862 write_imsm_migr_rec(st
);
10867 /*******************************************************************************
10868 * Function: save_backup_imsm
10869 * Description: Function saves critical data stripes to Migration Copy Area
10870 * and updates the current migration unit status.
10871 * Use restore_stripes() to form a destination stripe,
10872 * and to write it to the Copy Area.
10874 * st : supertype information
10875 * dev : imsm device that backup is saved for
10876 * info : general array info
10877 * buf : input buffer
10878 * length : length of data to backup (blocks_per_unit)
10882 ******************************************************************************/
10883 int save_backup_imsm(struct supertype
*st
,
10884 struct imsm_dev
*dev
,
10885 struct mdinfo
*info
,
10890 struct intel_super
*super
= st
->sb
;
10892 struct imsm_map
*map_dest
= get_imsm_map(dev
, MAP_0
);
10893 int new_disks
= map_dest
->num_members
;
10894 int dest_layout
= 0;
10895 int dest_chunk
, targets
[new_disks
];
10896 unsigned long long start
, target_offsets
[new_disks
];
10897 int data_disks
= imsm_num_data_members(map_dest
);
10899 for (i
= 0; i
< new_disks
; i
++) {
10900 struct dl
*dl_disk
= get_imsm_dl_disk(super
, i
);
10901 if (dl_disk
&& is_fd_valid(dl_disk
->fd
))
10902 targets
[i
] = dl_disk
->fd
;
10907 start
= info
->reshape_progress
* 512;
10908 for (i
= 0; i
< new_disks
; i
++) {
10909 target_offsets
[i
] = migr_chkp_area_pba(super
->migr_rec
) * 512;
10910 /* move back copy area adderss, it will be moved forward
10911 * in restore_stripes() using start input variable
10913 target_offsets
[i
] -= start
/data_disks
;
10916 dest_layout
= imsm_level_to_layout(map_dest
->raid_level
);
10917 dest_chunk
= __le16_to_cpu(map_dest
->blocks_per_strip
) * 512;
10919 if (restore_stripes(targets
, /* list of dest devices */
10920 target_offsets
, /* migration record offsets */
10923 map_dest
->raid_level
,
10925 -1, /* source backup file descriptor */
10926 0, /* input buf offset
10927 * always 0 buf is already offseted */
10931 pr_err("Error restoring stripes\n");
10941 /*******************************************************************************
10942 * Function: save_checkpoint_imsm
10943 * Description: Function called for current unit status update
10944 * in the migration record. It writes it to disk.
10946 * super : imsm internal array info
10947 * info : general array info
10951 * 2: failure, means no valid migration record
10952 * / no general migration in progress /
10953 ******************************************************************************/
10954 int save_checkpoint_imsm(struct supertype
*st
, struct mdinfo
*info
, int state
)
10956 struct intel_super
*super
= st
->sb
;
10957 unsigned long long blocks_per_unit
;
10958 unsigned long long curr_migr_unit
;
10960 if (load_imsm_migr_rec(super
) != 0) {
10961 dprintf("imsm: ERROR: Cannot read migration record for checkpoint save.\n");
10965 blocks_per_unit
= __le32_to_cpu(super
->migr_rec
->blocks_per_unit
);
10966 if (blocks_per_unit
== 0) {
10967 dprintf("imsm: no migration in progress.\n");
10970 curr_migr_unit
= info
->reshape_progress
/ blocks_per_unit
;
10971 /* check if array is alligned to copy area
10972 * if it is not alligned, add one to current migration unit value
10973 * this can happend on array reshape finish only
10975 if (info
->reshape_progress
% blocks_per_unit
)
10978 set_current_migr_unit(super
->migr_rec
, curr_migr_unit
);
10979 super
->migr_rec
->rec_status
= __cpu_to_le32(state
);
10980 set_migr_dest_1st_member_lba(super
->migr_rec
,
10981 super
->migr_rec
->dest_depth_per_unit
* curr_migr_unit
);
10983 if (write_imsm_migr_rec(st
) < 0) {
10984 dprintf("imsm: Cannot write migration record outside backup area\n");
10991 /*******************************************************************************
10992 * Function: recover_backup_imsm
10993 * Description: Function recovers critical data from the Migration Copy Area
10994 * while assembling an array.
10996 * super : imsm internal array info
10997 * info : general array info
10999 * 0 : success (or there is no data to recover)
11001 ******************************************************************************/
11002 int recover_backup_imsm(struct supertype
*st
, struct mdinfo
*info
)
11004 struct intel_super
*super
= st
->sb
;
11005 struct migr_record
*migr_rec
= super
->migr_rec
;
11006 struct imsm_map
*map_dest
;
11007 struct intel_dev
*id
= NULL
;
11008 unsigned long long read_offset
;
11009 unsigned long long write_offset
;
11011 int new_disks
, err
;
11014 unsigned int sector_size
= super
->sector_size
;
11015 unsigned long long curr_migr_unit
= current_migr_unit(migr_rec
);
11016 unsigned long long num_migr_units
= get_num_migr_units(migr_rec
);
11018 int skipped_disks
= 0;
11019 struct dl
*dl_disk
;
11021 err
= sysfs_get_str(info
, NULL
, "array_state", (char *)buffer
, 20);
11025 /* recover data only during assemblation */
11026 if (strncmp(buffer
, "inactive", 8) != 0)
11028 /* no data to recover */
11029 if (__le32_to_cpu(migr_rec
->rec_status
) == UNIT_SRC_NORMAL
)
11031 if (curr_migr_unit
>= num_migr_units
)
11034 /* find device during reshape */
11035 for (id
= super
->devlist
; id
; id
= id
->next
)
11036 if (is_gen_migration(id
->dev
))
11041 map_dest
= get_imsm_map(id
->dev
, MAP_0
);
11042 new_disks
= map_dest
->num_members
;
11044 read_offset
= migr_chkp_area_pba(migr_rec
) * 512;
11046 write_offset
= (migr_dest_1st_member_lba(migr_rec
) +
11047 pba_of_lba0(map_dest
)) * 512;
11049 unit_len
= __le32_to_cpu(migr_rec
->dest_depth_per_unit
) * 512;
11050 if (posix_memalign((void **)&buf
, sector_size
, unit_len
) != 0)
11053 for (dl_disk
= super
->disks
; dl_disk
; dl_disk
= dl_disk
->next
) {
11054 if (dl_disk
->index
< 0)
11057 if (!is_fd_valid(dl_disk
->fd
)) {
11061 if (lseek64(dl_disk
->fd
, read_offset
, SEEK_SET
) < 0) {
11062 pr_err("Cannot seek to block: %s\n",
11067 if (read(dl_disk
->fd
, buf
, unit_len
) != (ssize_t
)unit_len
) {
11068 pr_err("Cannot read copy area block: %s\n",
11073 if (lseek64(dl_disk
->fd
, write_offset
, SEEK_SET
) < 0) {
11074 pr_err("Cannot seek to block: %s\n",
11079 if (write(dl_disk
->fd
, buf
, unit_len
) != (ssize_t
)unit_len
) {
11080 pr_err("Cannot restore block: %s\n",
11087 if (skipped_disks
> imsm_get_allowed_degradation(info
->new_level
,
11091 pr_err("Cannot restore data from backup. Too many failed disks\n");
11095 if (save_checkpoint_imsm(st
, info
, UNIT_SRC_NORMAL
)) {
11096 /* ignore error == 2, this can mean end of reshape here
11098 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL) during restart\n");
11107 static char disk_by_path
[] = "/dev/disk/by-path/";
11109 static const char *imsm_get_disk_controller_domain(const char *path
)
11111 char disk_path
[PATH_MAX
];
11115 strcpy(disk_path
, disk_by_path
);
11116 strncat(disk_path
, path
, PATH_MAX
- strlen(disk_path
) - 1);
11117 if (stat(disk_path
, &st
) == 0) {
11118 struct sys_dev
* hba
;
11121 path
= devt_to_devpath(st
.st_rdev
, 1, NULL
);
11124 hba
= find_disk_attached_hba(-1, path
);
11125 if (hba
&& hba
->type
== SYS_DEV_SAS
)
11127 else if (hba
&& hba
->type
== SYS_DEV_SATA
)
11129 else if (hba
&& hba
->type
== SYS_DEV_VMD
)
11131 else if (hba
&& hba
->type
== SYS_DEV_NVME
)
11135 dprintf("path: %s hba: %s attached: %s\n",
11136 path
, (hba
) ? hba
->path
: "NULL", drv
);
11142 static char *imsm_find_array_devnm_by_subdev(int subdev
, char *container
)
11144 static char devnm
[32];
11145 char subdev_name
[20];
11146 struct mdstat_ent
*mdstat
;
11148 sprintf(subdev_name
, "%d", subdev
);
11149 mdstat
= mdstat_by_subdev(subdev_name
, container
);
11153 strcpy(devnm
, mdstat
->devnm
);
11154 free_mdstat(mdstat
);
11158 static int imsm_reshape_is_allowed_on_container(struct supertype
*st
,
11159 struct geo_params
*geo
,
11160 int *old_raid_disks
,
11163 /* currently we only support increasing the number of devices
11164 * for a container. This increases the number of device for each
11165 * member array. They must all be RAID0 or RAID5.
11168 struct mdinfo
*info
, *member
;
11169 int devices_that_can_grow
= 0;
11171 dprintf("imsm: imsm_reshape_is_allowed_on_container(ENTER): st->devnm = (%s)\n", st
->devnm
);
11173 if (geo
->size
> 0 ||
11174 geo
->level
!= UnSet
||
11175 geo
->layout
!= UnSet
||
11176 geo
->chunksize
!= 0 ||
11177 geo
->raid_disks
== UnSet
) {
11178 dprintf("imsm: Container operation is allowed for raid disks number change only.\n");
11182 if (direction
== ROLLBACK_METADATA_CHANGES
) {
11183 dprintf("imsm: Metadata changes rollback is not supported for container operation.\n");
11187 info
= container_content_imsm(st
, NULL
);
11188 for (member
= info
; member
; member
= member
->next
) {
11191 dprintf("imsm: checking device_num: %i\n",
11192 member
->container_member
);
11194 if (geo
->raid_disks
<= member
->array
.raid_disks
) {
11195 /* we work on container for Online Capacity Expansion
11196 * only so raid_disks has to grow
11198 dprintf("imsm: for container operation raid disks increase is required\n");
11202 if (info
->array
.level
!= 0 && info
->array
.level
!= 5) {
11203 /* we cannot use this container with other raid level
11205 dprintf("imsm: for container operation wrong raid level (%i) detected\n",
11206 info
->array
.level
);
11209 /* check for platform support
11210 * for this raid level configuration
11212 struct intel_super
*super
= st
->sb
;
11213 if (!is_raid_level_supported(super
->orom
,
11214 member
->array
.level
,
11215 geo
->raid_disks
)) {
11216 dprintf("platform does not support raid%d with %d disk%s\n",
11219 geo
->raid_disks
> 1 ? "s" : "");
11222 /* check if component size is aligned to chunk size
11224 if (info
->component_size
%
11225 (info
->array
.chunk_size
/512)) {
11226 dprintf("Component size is not aligned to chunk size\n");
11231 if (*old_raid_disks
&&
11232 info
->array
.raid_disks
!= *old_raid_disks
)
11234 *old_raid_disks
= info
->array
.raid_disks
;
11236 /* All raid5 and raid0 volumes in container
11237 * have to be ready for Online Capacity Expansion
11238 * so they need to be assembled. We have already
11239 * checked that no recovery etc is happening.
11241 result
= imsm_find_array_devnm_by_subdev(member
->container_member
,
11242 st
->container_devnm
);
11243 if (result
== NULL
) {
11244 dprintf("imsm: cannot find array\n");
11247 devices_that_can_grow
++;
11250 if (!member
&& devices_that_can_grow
)
11254 dprintf("Container operation allowed\n");
11256 dprintf("Error: %i\n", ret_val
);
11261 /* Function: get_spares_for_grow
11262 * Description: Allocates memory and creates list of spare devices
11263 * avaliable in container. Checks if spare drive size is acceptable.
11264 * Parameters: Pointer to the supertype structure
11265 * Returns: Pointer to the list of spare devices (mdinfo structure) on success,
11268 static struct mdinfo
*get_spares_for_grow(struct supertype
*st
)
11270 struct spare_criteria sc
;
11272 get_spare_criteria_imsm(st
, &sc
);
11273 return container_choose_spares(st
, &sc
, NULL
, NULL
, NULL
, 0);
11276 /******************************************************************************
11277 * function: imsm_create_metadata_update_for_reshape
11278 * Function creates update for whole IMSM container.
11280 ******************************************************************************/
11281 static int imsm_create_metadata_update_for_reshape(
11282 struct supertype
*st
,
11283 struct geo_params
*geo
,
11284 int old_raid_disks
,
11285 struct imsm_update_reshape
**updatep
)
11287 struct intel_super
*super
= st
->sb
;
11288 struct imsm_super
*mpb
= super
->anchor
;
11289 int update_memory_size
;
11290 struct imsm_update_reshape
*u
;
11291 struct mdinfo
*spares
;
11294 struct mdinfo
*dev
;
11296 dprintf("(enter) raid_disks = %i\n", geo
->raid_disks
);
11298 delta_disks
= geo
->raid_disks
- old_raid_disks
;
11300 /* size of all update data without anchor */
11301 update_memory_size
= sizeof(struct imsm_update_reshape
);
11303 /* now add space for spare disks that we need to add. */
11304 update_memory_size
+= sizeof(u
->new_disks
[0]) * (delta_disks
- 1);
11306 u
= xcalloc(1, update_memory_size
);
11307 u
->type
= update_reshape_container_disks
;
11308 u
->old_raid_disks
= old_raid_disks
;
11309 u
->new_raid_disks
= geo
->raid_disks
;
11311 /* now get spare disks list
11313 spares
= get_spares_for_grow(st
);
11315 if (spares
== NULL
|| delta_disks
> spares
->array
.spare_disks
) {
11316 pr_err("imsm: ERROR: Cannot get spare devices for %s.\n", geo
->dev_name
);
11321 /* we have got spares
11322 * update disk list in imsm_disk list table in anchor
11324 dprintf("imsm: %i spares are available.\n\n",
11325 spares
->array
.spare_disks
);
11327 dev
= spares
->devs
;
11328 for (i
= 0; i
< delta_disks
; i
++) {
11333 u
->new_disks
[i
] = makedev(dev
->disk
.major
,
11335 dl
= get_disk_super(super
, dev
->disk
.major
, dev
->disk
.minor
);
11336 dl
->index
= mpb
->num_disks
;
11344 sysfs_free(spares
);
11346 dprintf("imsm: reshape update preparation :");
11347 if (i
== delta_disks
) {
11348 dprintf_cont(" OK\n");
11350 return update_memory_size
;
11353 dprintf_cont(" Error\n");
11358 /******************************************************************************
11359 * function: imsm_create_metadata_update_for_size_change()
11360 * Creates update for IMSM array for array size change.
11362 ******************************************************************************/
11363 static int imsm_create_metadata_update_for_size_change(
11364 struct supertype
*st
,
11365 struct geo_params
*geo
,
11366 struct imsm_update_size_change
**updatep
)
11368 struct intel_super
*super
= st
->sb
;
11369 int update_memory_size
;
11370 struct imsm_update_size_change
*u
;
11372 dprintf("(enter) New size = %llu\n", geo
->size
);
11374 /* size of all update data without anchor */
11375 update_memory_size
= sizeof(struct imsm_update_size_change
);
11377 u
= xcalloc(1, update_memory_size
);
11378 u
->type
= update_size_change
;
11379 u
->subdev
= super
->current_vol
;
11380 u
->new_size
= geo
->size
;
11382 dprintf("imsm: reshape update preparation : OK\n");
11385 return update_memory_size
;
11388 /******************************************************************************
11389 * function: imsm_create_metadata_update_for_migration()
11390 * Creates update for IMSM array.
11392 ******************************************************************************/
11393 static int imsm_create_metadata_update_for_migration(
11394 struct supertype
*st
,
11395 struct geo_params
*geo
,
11396 struct imsm_update_reshape_migration
**updatep
)
11398 struct intel_super
*super
= st
->sb
;
11399 int update_memory_size
;
11400 struct imsm_update_reshape_migration
*u
;
11401 struct imsm_dev
*dev
;
11402 int previous_level
= -1;
11404 dprintf("(enter) New Level = %i\n", geo
->level
);
11406 /* size of all update data without anchor */
11407 update_memory_size
= sizeof(struct imsm_update_reshape_migration
);
11409 u
= xcalloc(1, update_memory_size
);
11410 u
->type
= update_reshape_migration
;
11411 u
->subdev
= super
->current_vol
;
11412 u
->new_level
= geo
->level
;
11413 u
->new_layout
= geo
->layout
;
11414 u
->new_raid_disks
= u
->old_raid_disks
= geo
->raid_disks
;
11415 u
->new_disks
[0] = -1;
11416 u
->new_chunksize
= -1;
11418 dev
= get_imsm_dev(super
, u
->subdev
);
11420 struct imsm_map
*map
;
11422 map
= get_imsm_map(dev
, MAP_0
);
11424 int current_chunk_size
=
11425 __le16_to_cpu(map
->blocks_per_strip
) / 2;
11427 if (geo
->chunksize
!= current_chunk_size
) {
11428 u
->new_chunksize
= geo
->chunksize
/ 1024;
11429 dprintf("imsm: chunk size change from %i to %i\n",
11430 current_chunk_size
, u
->new_chunksize
);
11432 previous_level
= map
->raid_level
;
11435 if (geo
->level
== 5 && previous_level
== 0) {
11436 struct mdinfo
*spares
= NULL
;
11438 u
->new_raid_disks
++;
11439 spares
= get_spares_for_grow(st
);
11440 if (spares
== NULL
|| spares
->array
.spare_disks
< 1) {
11442 sysfs_free(spares
);
11443 update_memory_size
= 0;
11444 pr_err("cannot get spare device for requested migration\n");
11447 sysfs_free(spares
);
11449 dprintf("imsm: reshape update preparation : OK\n");
11452 return update_memory_size
;
11455 static void imsm_update_metadata_locally(struct supertype
*st
,
11456 void *buf
, int len
)
11458 struct metadata_update mu
;
11463 mu
.space_list
= NULL
;
11465 if (imsm_prepare_update(st
, &mu
))
11466 imsm_process_update(st
, &mu
);
11468 while (mu
.space_list
) {
11469 void **space
= mu
.space_list
;
11470 mu
.space_list
= *space
;
11475 /***************************************************************************
11476 * Function: imsm_analyze_change
11477 * Description: Function analyze change for single volume
11478 * and validate if transition is supported
11479 * Parameters: Geometry parameters, supertype structure,
11480 * metadata change direction (apply/rollback)
11481 * Returns: Operation type code on success, -1 if fail
11482 ****************************************************************************/
11483 enum imsm_reshape_type
imsm_analyze_change(struct supertype
*st
,
11484 struct geo_params
*geo
,
11487 struct mdinfo info
;
11489 int check_devs
= 0;
11491 /* number of added/removed disks in operation result */
11492 int devNumChange
= 0;
11493 /* imsm compatible layout value for array geometry verification */
11494 int imsm_layout
= -1;
11496 struct imsm_dev
*dev
;
11497 struct imsm_map
*map
;
11498 struct intel_super
*super
;
11499 unsigned long long current_size
;
11500 unsigned long long free_size
;
11501 unsigned long long max_size
;
11504 getinfo_super_imsm_volume(st
, &info
, NULL
);
11505 if (geo
->level
!= info
.array
.level
&& geo
->level
>= 0 &&
11506 geo
->level
!= UnSet
) {
11507 switch (info
.array
.level
) {
11509 if (geo
->level
== 5) {
11510 change
= CH_MIGRATION
;
11511 if (geo
->layout
!= ALGORITHM_LEFT_ASYMMETRIC
) {
11512 pr_err("Error. Requested Layout not supported (left-asymmetric layout is supported only)!\n");
11514 goto analyse_change_exit
;
11516 imsm_layout
= geo
->layout
;
11518 devNumChange
= 1; /* parity disk added */
11519 } else if (geo
->level
== 10) {
11520 change
= CH_TAKEOVER
;
11522 devNumChange
= 2; /* two mirrors added */
11523 imsm_layout
= 0x102; /* imsm supported layout */
11528 if (geo
->level
== 0) {
11529 change
= CH_TAKEOVER
;
11531 devNumChange
= -(geo
->raid_disks
/2);
11532 imsm_layout
= 0; /* imsm raid0 layout */
11536 if (change
== -1) {
11537 pr_err("Error. Level Migration from %d to %d not supported!\n",
11538 info
.array
.level
, geo
->level
);
11539 goto analyse_change_exit
;
11542 geo
->level
= info
.array
.level
;
11544 if (geo
->layout
!= info
.array
.layout
&&
11545 (geo
->layout
!= UnSet
&& geo
->layout
!= -1)) {
11546 change
= CH_MIGRATION
;
11547 if (info
.array
.layout
== 0 && info
.array
.level
== 5 &&
11548 geo
->layout
== 5) {
11549 /* reshape 5 -> 4 */
11550 } else if (info
.array
.layout
== 5 && info
.array
.level
== 5 &&
11551 geo
->layout
== 0) {
11552 /* reshape 4 -> 5 */
11556 pr_err("Error. Layout Migration from %d to %d not supported!\n",
11557 info
.array
.layout
, geo
->layout
);
11559 goto analyse_change_exit
;
11562 geo
->layout
= info
.array
.layout
;
11563 if (imsm_layout
== -1)
11564 imsm_layout
= info
.array
.layout
;
11567 if (geo
->chunksize
> 0 && geo
->chunksize
!= UnSet
&&
11568 geo
->chunksize
!= info
.array
.chunk_size
) {
11569 if (info
.array
.level
== 10) {
11570 pr_err("Error. Chunk size change for RAID 10 is not supported.\n");
11572 goto analyse_change_exit
;
11573 } else if (info
.component_size
% (geo
->chunksize
/512)) {
11574 pr_err("New chunk size (%dK) does not evenly divide device size (%lluk). Aborting...\n",
11575 geo
->chunksize
/1024, info
.component_size
/2);
11577 goto analyse_change_exit
;
11579 change
= CH_MIGRATION
;
11581 geo
->chunksize
= info
.array
.chunk_size
;
11584 chunk
= geo
->chunksize
/ 1024;
11587 dev
= get_imsm_dev(super
, super
->current_vol
);
11588 map
= get_imsm_map(dev
, MAP_0
);
11589 data_disks
= imsm_num_data_members(map
);
11590 /* compute current size per disk member
11592 current_size
= info
.custom_array_size
/ data_disks
;
11594 if (geo
->size
> 0 && geo
->size
!= MAX_SIZE
) {
11595 /* align component size
11597 geo
->size
= imsm_component_size_alignment_check(
11598 get_imsm_raid_level(dev
->vol
.map
),
11599 chunk
* 1024, super
->sector_size
,
11601 if (geo
->size
== 0) {
11602 pr_err("Error. Size expansion is supported only (current size is %llu, requested size /rounded/ is 0).\n",
11604 goto analyse_change_exit
;
11608 if (current_size
!= geo
->size
&& geo
->size
> 0) {
11609 if (change
!= -1) {
11610 pr_err("Error. Size change should be the only one at a time.\n");
11612 goto analyse_change_exit
;
11614 if ((super
->current_vol
+ 1) != super
->anchor
->num_raid_devs
) {
11615 pr_err("Error. The last volume in container can be expanded only (%i/%s).\n",
11616 super
->current_vol
, st
->devnm
);
11617 goto analyse_change_exit
;
11619 /* check the maximum available size
11621 rv
= imsm_get_free_size(st
, dev
->vol
.map
->num_members
,
11622 0, chunk
, &free_size
);
11624 /* Cannot find maximum available space
11628 max_size
= free_size
+ current_size
;
11629 /* align component size
11631 max_size
= imsm_component_size_alignment_check(
11632 get_imsm_raid_level(dev
->vol
.map
),
11633 chunk
* 1024, super
->sector_size
,
11636 if (geo
->size
== MAX_SIZE
) {
11637 /* requested size change to the maximum available size
11639 if (max_size
== 0) {
11640 pr_err("Error. Cannot find maximum available space.\n");
11642 goto analyse_change_exit
;
11644 geo
->size
= max_size
;
11647 if (direction
== ROLLBACK_METADATA_CHANGES
) {
11648 /* accept size for rollback only
11651 /* round size due to metadata compatibility
11653 geo
->size
= (geo
->size
>> SECT_PER_MB_SHIFT
)
11654 << SECT_PER_MB_SHIFT
;
11655 dprintf("Prepare update for size change to %llu\n",
11657 if (current_size
>= geo
->size
) {
11658 pr_err("Error. Size expansion is supported only (current size is %llu, requested size /rounded/ is %llu).\n",
11659 current_size
, geo
->size
);
11660 goto analyse_change_exit
;
11662 if (max_size
&& geo
->size
> max_size
) {
11663 pr_err("Error. Requested size is larger than maximum available size (maximum available size is %llu, requested size /rounded/ is %llu).\n",
11664 max_size
, geo
->size
);
11665 goto analyse_change_exit
;
11668 geo
->size
*= data_disks
;
11669 geo
->raid_disks
= dev
->vol
.map
->num_members
;
11670 change
= CH_ARRAY_SIZE
;
11672 if (!validate_geometry_imsm(st
,
11675 geo
->raid_disks
+ devNumChange
,
11677 geo
->size
, INVALID_SECTORS
,
11678 0, 0, info
.consistency_policy
, 1))
11682 struct intel_super
*super
= st
->sb
;
11683 struct imsm_super
*mpb
= super
->anchor
;
11685 if (mpb
->num_raid_devs
> 1) {
11686 pr_err("Error. Cannot perform operation on %s- for this operation "
11687 "it MUST be single array in container\n", geo
->dev_name
);
11692 analyse_change_exit
:
11693 if (direction
== ROLLBACK_METADATA_CHANGES
&&
11694 (change
== CH_MIGRATION
|| change
== CH_TAKEOVER
)) {
11695 dprintf("imsm: Metadata changes rollback is not supported for migration and takeover operations.\n");
11701 int imsm_takeover(struct supertype
*st
, struct geo_params
*geo
)
11703 struct intel_super
*super
= st
->sb
;
11704 struct imsm_update_takeover
*u
;
11706 u
= xmalloc(sizeof(struct imsm_update_takeover
));
11708 u
->type
= update_takeover
;
11709 u
->subarray
= super
->current_vol
;
11711 /* 10->0 transition */
11712 if (geo
->level
== 0)
11713 u
->direction
= R10_TO_R0
;
11715 /* 0->10 transition */
11716 if (geo
->level
== 10)
11717 u
->direction
= R0_TO_R10
;
11719 /* update metadata locally */
11720 imsm_update_metadata_locally(st
, u
,
11721 sizeof(struct imsm_update_takeover
));
11722 /* and possibly remotely */
11723 if (st
->update_tail
)
11724 append_metadata_update(st
, u
,
11725 sizeof(struct imsm_update_takeover
));
11732 /* Flush size update if size calculated by num_data_stripes is higher than
11733 * imsm_dev_size to eliminate differences during reshape.
11734 * Mdmon will recalculate them correctly.
11735 * If subarray index is not set then check whole container.
11737 * 0 - no error occurred
11738 * 1 - error detected
11740 static int imsm_fix_size_mismatch(struct supertype
*st
, int subarray_index
)
11742 struct intel_super
*super
= st
->sb
;
11743 int tmp
= super
->current_vol
;
11747 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
11748 if (subarray_index
>= 0 && i
!= subarray_index
)
11750 super
->current_vol
= i
;
11751 struct imsm_dev
*dev
= get_imsm_dev(super
, super
->current_vol
);
11752 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
11753 unsigned int disc_count
= imsm_num_data_members(map
);
11754 struct geo_params geo
;
11755 struct imsm_update_size_change
*update
;
11756 unsigned long long calc_size
= per_dev_array_size(map
) * disc_count
;
11757 unsigned long long d_size
= imsm_dev_size(dev
);
11760 if (calc_size
== d_size
|| dev
->vol
.migr_type
== MIGR_GEN_MIGR
)
11763 /* There is a difference, confirm that imsm_dev_size is
11764 * smaller and push update.
11766 if (d_size
> calc_size
) {
11767 pr_err("imsm: dev size of subarray %d is incorrect\n",
11771 memset(&geo
, 0, sizeof(struct geo_params
));
11773 u_size
= imsm_create_metadata_update_for_size_change(st
, &geo
,
11776 dprintf("imsm: Cannot prepare size change update\n");
11779 imsm_update_metadata_locally(st
, update
, u_size
);
11780 if (st
->update_tail
) {
11781 append_metadata_update(st
, update
, u_size
);
11782 flush_metadata_updates(st
);
11783 st
->update_tail
= &st
->updates
;
11785 imsm_sync_metadata(st
);
11791 super
->current_vol
= tmp
;
11795 static int imsm_reshape_super(struct supertype
*st
, unsigned long long size
,
11797 int layout
, int chunksize
, int raid_disks
,
11798 int delta_disks
, char *backup
, char *dev
,
11799 int direction
, int verbose
)
11802 struct geo_params geo
;
11804 dprintf("(enter)\n");
11806 memset(&geo
, 0, sizeof(struct geo_params
));
11808 geo
.dev_name
= dev
;
11809 strcpy(geo
.devnm
, st
->devnm
);
11812 geo
.layout
= layout
;
11813 geo
.chunksize
= chunksize
;
11814 geo
.raid_disks
= raid_disks
;
11815 if (delta_disks
!= UnSet
)
11816 geo
.raid_disks
+= delta_disks
;
11818 dprintf("for level : %i\n", geo
.level
);
11819 dprintf("for raid_disks : %i\n", geo
.raid_disks
);
11821 if (strcmp(st
->container_devnm
, st
->devnm
) == 0) {
11822 /* On container level we can only increase number of devices. */
11823 dprintf("imsm: info: Container operation\n");
11824 int old_raid_disks
= 0;
11826 if (imsm_reshape_is_allowed_on_container(
11827 st
, &geo
, &old_raid_disks
, direction
)) {
11828 struct imsm_update_reshape
*u
= NULL
;
11831 if (imsm_fix_size_mismatch(st
, -1)) {
11832 dprintf("imsm: Cannot fix size mismatch\n");
11833 goto exit_imsm_reshape_super
;
11836 len
= imsm_create_metadata_update_for_reshape(
11837 st
, &geo
, old_raid_disks
, &u
);
11840 dprintf("imsm: Cannot prepare update\n");
11841 goto exit_imsm_reshape_super
;
11845 /* update metadata locally */
11846 imsm_update_metadata_locally(st
, u
, len
);
11847 /* and possibly remotely */
11848 if (st
->update_tail
)
11849 append_metadata_update(st
, u
, len
);
11854 pr_err("(imsm) Operation is not allowed on this container\n");
11857 /* On volume level we support following operations
11858 * - takeover: raid10 -> raid0; raid0 -> raid10
11859 * - chunk size migration
11860 * - migration: raid5 -> raid0; raid0 -> raid5
11862 struct intel_super
*super
= st
->sb
;
11863 struct intel_dev
*dev
= super
->devlist
;
11865 dprintf("imsm: info: Volume operation\n");
11866 /* find requested device */
11869 imsm_find_array_devnm_by_subdev(
11870 dev
->index
, st
->container_devnm
);
11871 if (devnm
&& strcmp(devnm
, geo
.devnm
) == 0)
11876 pr_err("Cannot find %s (%s) subarray\n",
11877 geo
.dev_name
, geo
.devnm
);
11878 goto exit_imsm_reshape_super
;
11880 super
->current_vol
= dev
->index
;
11881 change
= imsm_analyze_change(st
, &geo
, direction
);
11884 ret_val
= imsm_takeover(st
, &geo
);
11886 case CH_MIGRATION
: {
11887 struct imsm_update_reshape_migration
*u
= NULL
;
11889 imsm_create_metadata_update_for_migration(
11892 dprintf("imsm: Cannot prepare update\n");
11896 /* update metadata locally */
11897 imsm_update_metadata_locally(st
, u
, len
);
11898 /* and possibly remotely */
11899 if (st
->update_tail
)
11900 append_metadata_update(st
, u
, len
);
11905 case CH_ARRAY_SIZE
: {
11906 struct imsm_update_size_change
*u
= NULL
;
11908 imsm_create_metadata_update_for_size_change(
11911 dprintf("imsm: Cannot prepare update\n");
11915 /* update metadata locally */
11916 imsm_update_metadata_locally(st
, u
, len
);
11917 /* and possibly remotely */
11918 if (st
->update_tail
)
11919 append_metadata_update(st
, u
, len
);
11929 exit_imsm_reshape_super
:
11930 dprintf("imsm: reshape_super Exit code = %i\n", ret_val
);
11934 #define COMPLETED_OK 0
11935 #define COMPLETED_NONE 1
11936 #define COMPLETED_DELAYED 2
11938 static int read_completed(int fd
, unsigned long long *val
)
11943 ret
= sysfs_fd_get_str(fd
, buf
, 50);
11947 ret
= COMPLETED_OK
;
11948 if (strncmp(buf
, "none", 4) == 0) {
11949 ret
= COMPLETED_NONE
;
11950 } else if (strncmp(buf
, "delayed", 7) == 0) {
11951 ret
= COMPLETED_DELAYED
;
11954 *val
= strtoull(buf
, &ep
, 0);
11955 if (ep
== buf
|| (*ep
!= 0 && *ep
!= '\n' && *ep
!= ' '))
11961 /*******************************************************************************
11962 * Function: wait_for_reshape_imsm
11963 * Description: Function writes new sync_max value and waits until
11964 * reshape process reach new position
11966 * sra : general array info
11967 * ndata : number of disks in new array's layout
11970 * 1 : there is no reshape in progress,
11972 ******************************************************************************/
11973 int wait_for_reshape_imsm(struct mdinfo
*sra
, int ndata
)
11975 int fd
= sysfs_get_fd(sra
, NULL
, "sync_completed");
11977 unsigned long long completed
;
11978 /* to_complete : new sync_max position */
11979 unsigned long long to_complete
= sra
->reshape_progress
;
11980 unsigned long long position_to_set
= to_complete
/ ndata
;
11982 if (!is_fd_valid(fd
)) {
11983 dprintf("cannot open reshape_position\n");
11988 if (sysfs_fd_get_ll(fd
, &completed
) < 0) {
11990 dprintf("cannot read reshape_position (no reshape in progres)\n");
11999 if (completed
> position_to_set
) {
12000 dprintf("wrong next position to set %llu (%llu)\n",
12001 to_complete
, position_to_set
);
12005 dprintf("Position set: %llu\n", position_to_set
);
12006 if (sysfs_set_num(sra
, NULL
, "sync_max",
12007 position_to_set
) != 0) {
12008 dprintf("cannot set reshape position to %llu\n",
12017 int timeout
= 3000;
12019 sysfs_wait(fd
, &timeout
);
12020 if (sysfs_get_str(sra
, NULL
, "sync_action",
12022 strncmp(action
, "reshape", 7) != 0) {
12023 if (strncmp(action
, "idle", 4) == 0)
12029 rc
= read_completed(fd
, &completed
);
12031 dprintf("cannot read reshape_position (in loop)\n");
12034 } else if (rc
== COMPLETED_NONE
)
12036 } while (completed
< position_to_set
);
12042 /*******************************************************************************
12043 * Function: check_degradation_change
12044 * Description: Check that array hasn't become failed.
12046 * info : for sysfs access
12047 * sources : source disks descriptors
12048 * degraded: previous degradation level
12050 * degradation level
12051 ******************************************************************************/
12052 int check_degradation_change(struct mdinfo
*info
,
12056 unsigned long long new_degraded
;
12059 rv
= sysfs_get_ll(info
, NULL
, "degraded", &new_degraded
);
12060 if (rv
== -1 || (new_degraded
!= (unsigned long long)degraded
)) {
12061 /* check each device to ensure it is still working */
12064 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
12065 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
12067 if (sd
->disk
.state
& (1<<MD_DISK_SYNC
)) {
12069 int raid_disk
= sd
->disk
.raid_disk
;
12071 if (sysfs_get_str(info
,
12072 sd
, "state", sbuf
, sizeof(sbuf
)) < 0 ||
12073 strstr(sbuf
, "faulty") ||
12074 strstr(sbuf
, "in_sync") == NULL
) {
12075 /* this device is dead */
12076 sd
->disk
.state
= (1<<MD_DISK_FAULTY
);
12077 if (raid_disk
>= 0)
12078 close_fd(&sources
[raid_disk
]);
12085 return new_degraded
;
12088 /*******************************************************************************
12089 * Function: imsm_manage_reshape
12090 * Description: Function finds array under reshape and it manages reshape
12091 * process. It creates stripes backups (if required) and sets
12094 * afd : Backup handle (nattive) - not used
12095 * sra : general array info
12096 * reshape : reshape parameters - not used
12097 * st : supertype structure
12098 * blocks : size of critical section [blocks]
12099 * fds : table of source device descriptor
12100 * offsets : start of array (offest per devices)
12102 * destfd : table of destination device descriptor
12103 * destoffsets : table of destination offsets (per device)
12105 * 1 : success, reshape is done
12107 ******************************************************************************/
12108 static int imsm_manage_reshape(
12109 int afd
, struct mdinfo
*sra
, struct reshape
*reshape
,
12110 struct supertype
*st
, unsigned long backup_blocks
,
12111 int *fds
, unsigned long long *offsets
,
12112 int dests
, int *destfd
, unsigned long long *destoffsets
)
12115 struct intel_super
*super
= st
->sb
;
12116 struct intel_dev
*dv
;
12117 unsigned int sector_size
= super
->sector_size
;
12118 struct imsm_dev
*dev
= NULL
;
12119 struct imsm_map
*map_src
, *map_dest
;
12120 int migr_vol_qan
= 0;
12121 int ndata
, odata
; /* [bytes] */
12122 int chunk
; /* [bytes] */
12123 struct migr_record
*migr_rec
;
12125 unsigned int buf_size
; /* [bytes] */
12126 unsigned long long max_position
; /* array size [bytes] */
12127 unsigned long long next_step
; /* [blocks]/[bytes] */
12128 unsigned long long old_data_stripe_length
;
12129 unsigned long long start_src
; /* [bytes] */
12130 unsigned long long start
; /* [bytes] */
12131 unsigned long long start_buf_shift
; /* [bytes] */
12133 int source_layout
= 0;
12134 int subarray_index
= -1;
12139 if (!fds
|| !offsets
)
12142 /* Find volume during the reshape */
12143 for (dv
= super
->devlist
; dv
; dv
= dv
->next
) {
12144 if (dv
->dev
->vol
.migr_type
== MIGR_GEN_MIGR
&&
12145 dv
->dev
->vol
.migr_state
== 1) {
12148 subarray_index
= dv
->index
;
12151 /* Only one volume can migrate at the same time */
12152 if (migr_vol_qan
!= 1) {
12153 pr_err("%s", migr_vol_qan
?
12154 "Number of migrating volumes greater than 1\n" :
12155 "There is no volume during migrationg\n");
12159 map_dest
= get_imsm_map(dev
, MAP_0
);
12160 map_src
= get_imsm_map(dev
, MAP_1
);
12161 if (map_src
== NULL
)
12164 ndata
= imsm_num_data_members(map_dest
);
12165 odata
= imsm_num_data_members(map_src
);
12167 chunk
= __le16_to_cpu(map_src
->blocks_per_strip
) * 512;
12168 old_data_stripe_length
= odata
* chunk
;
12170 migr_rec
= super
->migr_rec
;
12172 /* initialize migration record for start condition */
12173 if (sra
->reshape_progress
== 0)
12174 init_migr_record_imsm(st
, dev
, sra
);
12176 if (__le32_to_cpu(migr_rec
->rec_status
) != UNIT_SRC_NORMAL
) {
12177 dprintf("imsm: cannot restart migration when data are present in copy area.\n");
12180 /* Save checkpoint to update migration record for current
12181 * reshape position (in md). It can be farther than current
12182 * reshape position in metadata.
12184 if (save_checkpoint_imsm(st
, sra
, UNIT_SRC_NORMAL
) == 1) {
12185 /* ignore error == 2, this can mean end of reshape here
12187 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL, initial save)\n");
12192 /* size for data */
12193 buf_size
= __le32_to_cpu(migr_rec
->blocks_per_unit
) * 512;
12194 /* extend buffer size for parity disk */
12195 buf_size
+= __le32_to_cpu(migr_rec
->dest_depth_per_unit
) * 512;
12196 /* add space for stripe alignment */
12197 buf_size
+= old_data_stripe_length
;
12198 if (posix_memalign((void **)&buf
, MAX_SECTOR_SIZE
, buf_size
)) {
12199 dprintf("imsm: Cannot allocate checkpoint buffer\n");
12203 max_position
= sra
->component_size
* ndata
;
12204 source_layout
= imsm_level_to_layout(map_src
->raid_level
);
12206 while (current_migr_unit(migr_rec
) <
12207 get_num_migr_units(migr_rec
)) {
12208 /* current reshape position [blocks] */
12209 unsigned long long current_position
=
12210 __le32_to_cpu(migr_rec
->blocks_per_unit
)
12211 * current_migr_unit(migr_rec
);
12212 unsigned long long border
;
12214 /* Check that array hasn't become failed.
12216 degraded
= check_degradation_change(sra
, fds
, degraded
);
12217 if (degraded
> 1) {
12218 dprintf("imsm: Abort reshape due to degradation level (%i)\n", degraded
);
12222 next_step
= __le32_to_cpu(migr_rec
->blocks_per_unit
);
12224 if ((current_position
+ next_step
) > max_position
)
12225 next_step
= max_position
- current_position
;
12227 start
= current_position
* 512;
12229 /* align reading start to old geometry */
12230 start_buf_shift
= start
% old_data_stripe_length
;
12231 start_src
= start
- start_buf_shift
;
12233 border
= (start_src
/ odata
) - (start
/ ndata
);
12235 if (border
<= __le32_to_cpu(migr_rec
->dest_depth_per_unit
)) {
12236 /* save critical stripes to buf
12237 * start - start address of current unit
12238 * to backup [bytes]
12239 * start_src - start address of current unit
12240 * to backup alligned to source array
12243 unsigned long long next_step_filler
;
12244 unsigned long long copy_length
= next_step
* 512;
12246 /* allign copy area length to stripe in old geometry */
12247 next_step_filler
= ((copy_length
+ start_buf_shift
)
12248 % old_data_stripe_length
);
12249 if (next_step_filler
)
12250 next_step_filler
= (old_data_stripe_length
12251 - next_step_filler
);
12252 dprintf("save_stripes() parameters: start = %llu,\tstart_src = %llu,\tnext_step*512 = %llu,\tstart_in_buf_shift = %llu,\tnext_step_filler = %llu\n",
12253 start
, start_src
, copy_length
,
12254 start_buf_shift
, next_step_filler
);
12256 if (save_stripes(fds
, offsets
, map_src
->num_members
,
12257 chunk
, map_src
->raid_level
,
12258 source_layout
, 0, NULL
, start_src
,
12260 next_step_filler
+ start_buf_shift
,
12262 dprintf("imsm: Cannot save stripes to buffer\n");
12265 /* Convert data to destination format and store it
12266 * in backup general migration area
12268 if (save_backup_imsm(st
, dev
, sra
,
12269 buf
+ start_buf_shift
, copy_length
)) {
12270 dprintf("imsm: Cannot save stripes to target devices\n");
12273 if (save_checkpoint_imsm(st
, sra
,
12274 UNIT_SRC_IN_CP_AREA
)) {
12275 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_IN_CP_AREA)\n");
12279 /* set next step to use whole border area */
12280 border
/= next_step
;
12282 next_step
*= border
;
12284 /* When data backed up, checkpoint stored,
12285 * kick the kernel to reshape unit of data
12287 next_step
= next_step
+ sra
->reshape_progress
;
12288 /* limit next step to array max position */
12289 if (next_step
> max_position
)
12290 next_step
= max_position
;
12291 sysfs_set_num(sra
, NULL
, "suspend_lo", sra
->reshape_progress
);
12292 sysfs_set_num(sra
, NULL
, "suspend_hi", next_step
);
12293 sra
->reshape_progress
= next_step
;
12295 /* wait until reshape finish */
12296 if (wait_for_reshape_imsm(sra
, ndata
)) {
12297 dprintf("wait_for_reshape_imsm returned error!\n");
12303 if (save_checkpoint_imsm(st
, sra
, UNIT_SRC_NORMAL
) == 1) {
12304 /* ignore error == 2, this can mean end of reshape here
12306 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL)\n");
12312 /* clear migr_rec on disks after successful migration */
12315 memset(super
->migr_rec_buf
, 0, MIGR_REC_BUF_SECTORS
*MAX_SECTOR_SIZE
);
12316 for (d
= super
->disks
; d
; d
= d
->next
) {
12317 if (d
->index
< 0 || is_failed(&d
->disk
))
12319 unsigned long long dsize
;
12321 get_dev_size(d
->fd
, NULL
, &dsize
);
12322 if (lseek64(d
->fd
, dsize
- MIGR_REC_SECTOR_POSITION
*sector_size
,
12324 if ((unsigned int)write(d
->fd
, super
->migr_rec_buf
,
12325 MIGR_REC_BUF_SECTORS
*sector_size
) !=
12326 MIGR_REC_BUF_SECTORS
*sector_size
)
12327 perror("Write migr_rec failed");
12331 /* return '1' if done */
12334 /* After the reshape eliminate size mismatch in metadata.
12335 * Don't update md/component_size here, volume hasn't
12336 * to take whole space. It is allowed by kernel.
12337 * md/component_size will be set propoperly after next assembly.
12339 imsm_fix_size_mismatch(st
, subarray_index
);
12343 /* See Grow.c: abort_reshape() for further explanation */
12344 sysfs_set_num(sra
, NULL
, "suspend_lo", 0x7FFFFFFFFFFFFFFFULL
);
12345 sysfs_set_num(sra
, NULL
, "suspend_hi", 0);
12346 sysfs_set_num(sra
, NULL
, "suspend_lo", 0);
12351 /*******************************************************************************
12352 * Function: calculate_bitmap_min_chunksize
12353 * Description: Calculates the minimal valid bitmap chunk size
12355 * max_bits : indicate how many bits can be used for the bitmap
12356 * data_area_size : the size of the data area covered by the bitmap
12359 * The bitmap chunk size
12360 ******************************************************************************/
12361 static unsigned long long
12362 calculate_bitmap_min_chunksize(unsigned long long max_bits
,
12363 unsigned long long data_area_size
)
12365 unsigned long long min_chunk
=
12366 4096; /* sub-page chunks don't work yet.. */
12367 unsigned long long bits
= data_area_size
/ min_chunk
+ 1;
12369 while (bits
> max_bits
) {
12371 bits
= (bits
+ 1) / 2;
12376 /*******************************************************************************
12377 * Function: calculate_bitmap_chunksize
12378 * Description: Calculates the bitmap chunk size for the given device
12380 * st : supertype information
12381 * dev : device for the bitmap
12384 * The bitmap chunk size
12385 ******************************************************************************/
12386 static unsigned long long calculate_bitmap_chunksize(struct supertype
*st
,
12387 struct imsm_dev
*dev
)
12389 struct intel_super
*super
= st
->sb
;
12390 unsigned long long min_chunksize
;
12391 unsigned long long result
= IMSM_DEFAULT_BITMAP_CHUNKSIZE
;
12392 size_t dev_size
= imsm_dev_size(dev
);
12394 min_chunksize
= calculate_bitmap_min_chunksize(
12395 IMSM_BITMAP_AREA_SIZE
* super
->sector_size
, dev_size
);
12397 if (result
< min_chunksize
)
12398 result
= min_chunksize
;
12403 /*******************************************************************************
12404 * Function: init_bitmap_header
12405 * Description: Initialize the bitmap header structure
12407 * st : supertype information
12408 * bms : bitmap header struct to initialize
12409 * dev : device for the bitmap
12414 ******************************************************************************/
12415 static int init_bitmap_header(struct supertype
*st
, struct bitmap_super_s
*bms
,
12416 struct imsm_dev
*dev
)
12423 bms
->magic
= __cpu_to_le32(BITMAP_MAGIC
);
12424 bms
->version
= __cpu_to_le32(BITMAP_MAJOR_HI
);
12425 bms
->daemon_sleep
= __cpu_to_le32(IMSM_DEFAULT_BITMAP_DAEMON_SLEEP
);
12426 bms
->sync_size
= __cpu_to_le64(IMSM_BITMAP_AREA_SIZE
);
12427 bms
->write_behind
= __cpu_to_le32(0);
12429 uuid_from_super_imsm(st
, vol_uuid
);
12430 memcpy(bms
->uuid
, vol_uuid
, 16);
12432 bms
->chunksize
= calculate_bitmap_chunksize(st
, dev
);
12437 /*******************************************************************************
12438 * Function: validate_internal_bitmap_for_drive
12439 * Description: Verify if the bitmap header for a given drive.
12441 * st : supertype information
12442 * offset : The offset from the beginning of the drive where to look for
12443 * the bitmap header.
12444 * d : the drive info
12449 ******************************************************************************/
12450 static int validate_internal_bitmap_for_drive(struct supertype
*st
,
12451 unsigned long long offset
,
12454 struct intel_super
*super
= st
->sb
;
12457 bitmap_super_t
*bms
;
12465 if (posix_memalign(&read_buf
, MAX_SECTOR_SIZE
, IMSM_BITMAP_HEADER_SIZE
))
12469 if (!is_fd_valid(fd
)) {
12470 fd
= open(d
->devname
, O_RDONLY
, 0);
12472 if (!is_fd_valid(fd
)) {
12473 dprintf("cannot open the device %s\n", d
->devname
);
12478 if (lseek64(fd
, offset
* super
->sector_size
, SEEK_SET
) < 0)
12480 if (read(fd
, read_buf
, IMSM_BITMAP_HEADER_SIZE
) !=
12481 IMSM_BITMAP_HEADER_SIZE
)
12484 uuid_from_super_imsm(st
, vol_uuid
);
12487 if ((bms
->magic
!= __cpu_to_le32(BITMAP_MAGIC
)) ||
12488 (bms
->version
!= __cpu_to_le32(BITMAP_MAJOR_HI
)) ||
12489 (!same_uuid((int *)bms
->uuid
, vol_uuid
, st
->ss
->swapuuid
))) {
12490 dprintf("wrong bitmap header detected\n");
12496 if (!is_fd_valid(d
->fd
))
12505 /*******************************************************************************
12506 * Function: validate_internal_bitmap_imsm
12507 * Description: Verify if the bitmap header is in place and with proper data.
12509 * st : supertype information
12512 * 0 : success or device w/o RWH_BITMAP
12514 ******************************************************************************/
12515 static int validate_internal_bitmap_imsm(struct supertype
*st
)
12517 struct intel_super
*super
= st
->sb
;
12518 struct imsm_dev
*dev
= get_imsm_dev(super
, super
->current_vol
);
12519 unsigned long long offset
;
12525 if (dev
->rwh_policy
!= RWH_BITMAP
)
12528 offset
= get_bitmap_header_sector(super
, super
->current_vol
);
12529 for (d
= super
->disks
; d
; d
= d
->next
) {
12530 if (d
->index
< 0 || is_failed(&d
->disk
))
12533 if (validate_internal_bitmap_for_drive(st
, offset
, d
)) {
12534 pr_err("imsm: bitmap validation failed\n");
12541 /*******************************************************************************
12542 * Function: add_internal_bitmap_imsm
12543 * Description: Mark the volume to use the bitmap and updates the chunk size value.
12545 * st : supertype information
12546 * chunkp : bitmap chunk size
12547 * delay : not used for imsm
12548 * write_behind : not used for imsm
12549 * size : not used for imsm
12550 * may_change : not used for imsm
12551 * amajor : not used for imsm
12556 ******************************************************************************/
12557 static int add_internal_bitmap_imsm(struct supertype
*st
, int *chunkp
,
12558 int delay
, int write_behind
,
12559 unsigned long long size
, int may_change
,
12562 struct intel_super
*super
= st
->sb
;
12563 int vol_idx
= super
->current_vol
;
12564 struct imsm_dev
*dev
;
12566 if (!super
->devlist
|| vol_idx
== -1 || !chunkp
)
12569 dev
= get_imsm_dev(super
, vol_idx
);
12572 dprintf("cannot find the device for volume index %d\n",
12576 dev
->rwh_policy
= RWH_BITMAP
;
12578 *chunkp
= calculate_bitmap_chunksize(st
, dev
);
12583 /*******************************************************************************
12584 * Function: locate_bitmap_imsm
12585 * Description: Seek 'fd' to start of write-intent-bitmap.
12587 * st : supertype information
12588 * fd : file descriptor for the device
12589 * node_num : not used for imsm
12594 ******************************************************************************/
12595 static int locate_bitmap_imsm(struct supertype
*st
, int fd
, int node_num
)
12597 struct intel_super
*super
= st
->sb
;
12598 unsigned long long offset
;
12599 int vol_idx
= super
->current_vol
;
12601 if (!super
->devlist
|| vol_idx
== -1)
12604 offset
= get_bitmap_header_sector(super
, super
->current_vol
);
12605 dprintf("bitmap header offset is %llu\n", offset
);
12607 lseek64(fd
, offset
<< 9, 0);
12612 /*******************************************************************************
12613 * Function: write_init_bitmap_imsm
12614 * Description: Write a bitmap header and prepares the area for the bitmap.
12616 * st : supertype information
12617 * fd : file descriptor for the device
12618 * update : not used for imsm
12623 ******************************************************************************/
12624 static int write_init_bitmap_imsm(struct supertype
*st
, int fd
,
12625 enum bitmap_update update
)
12627 struct intel_super
*super
= st
->sb
;
12628 int vol_idx
= super
->current_vol
;
12630 unsigned long long offset
;
12631 bitmap_super_t bms
= { 0 };
12632 size_t written
= 0;
12637 if (!super
->devlist
|| !super
->sector_size
|| vol_idx
== -1)
12640 struct imsm_dev
*dev
= get_imsm_dev(super
, vol_idx
);
12642 /* first clear the space for bitmap header */
12643 unsigned long long bitmap_area_start
=
12644 get_bitmap_header_sector(super
, vol_idx
);
12646 dprintf("zeroing area start (%llu) and size (%u)\n", bitmap_area_start
,
12647 IMSM_BITMAP_AND_HEADER_SIZE
/ super
->sector_size
);
12648 if (zero_disk_range(fd
, bitmap_area_start
,
12649 IMSM_BITMAP_HEADER_SIZE
/ super
->sector_size
)) {
12650 pr_err("imsm: cannot zeroing the space for the bitmap\n");
12654 /* The bitmap area should be filled with "1"s to perform initial
12657 if (posix_memalign(&buf
, MAX_SECTOR_SIZE
, MAX_SECTOR_SIZE
))
12659 memset(buf
, 0xFF, MAX_SECTOR_SIZE
);
12660 offset
= get_bitmap_sector(super
, vol_idx
);
12661 lseek64(fd
, offset
<< 9, 0);
12662 while (written
< IMSM_BITMAP_AREA_SIZE
) {
12663 to_write
= IMSM_BITMAP_AREA_SIZE
- written
;
12664 if (to_write
> MAX_SECTOR_SIZE
)
12665 to_write
= MAX_SECTOR_SIZE
;
12666 rv_num
= write(fd
, buf
, MAX_SECTOR_SIZE
);
12667 if (rv_num
!= MAX_SECTOR_SIZE
) {
12669 dprintf("cannot initialize bitmap area\n");
12675 /* write a bitmap header */
12676 init_bitmap_header(st
, &bms
, dev
);
12677 memset(buf
, 0, MAX_SECTOR_SIZE
);
12678 memcpy(buf
, &bms
, sizeof(bitmap_super_t
));
12679 if (locate_bitmap_imsm(st
, fd
, 0)) {
12681 dprintf("cannot locate the bitmap\n");
12684 if (write(fd
, buf
, MAX_SECTOR_SIZE
) != MAX_SECTOR_SIZE
) {
12686 dprintf("cannot write the bitmap header\n");
12697 /*******************************************************************************
12698 * Function: is_vol_to_setup_bitmap
12699 * Description: Checks if a bitmap should be activated on the dev.
12701 * info : info about the volume to setup the bitmap
12702 * dev : the device to check against bitmap creation
12705 * 0 : bitmap should be set up on the device
12707 ******************************************************************************/
12708 static int is_vol_to_setup_bitmap(struct mdinfo
*info
, struct imsm_dev
*dev
)
12713 if ((strcmp((char *)dev
->volume
, info
->name
) == 0) &&
12714 (dev
->rwh_policy
== RWH_BITMAP
))
12720 /*******************************************************************************
12721 * Function: set_bitmap_sysfs
12722 * Description: Set the sysfs atributes of a given volume to activate the bitmap.
12724 * info : info about the volume where the bitmap should be setup
12725 * chunksize : bitmap chunk size
12726 * location : location of the bitmap
12731 ******************************************************************************/
12732 static int set_bitmap_sysfs(struct mdinfo
*info
, unsigned long long chunksize
,
12735 /* The bitmap/metadata is set to external to allow changing of value for
12736 * bitmap/location. When external is used, the kernel will treat an offset
12737 * related to the device's first lba (in opposition to the "internal" case
12738 * when this value is related to the beginning of the superblock).
12740 if (sysfs_set_str(info
, NULL
, "bitmap/metadata", "external")) {
12741 dprintf("failed to set bitmap/metadata\n");
12745 /* It can only be changed when no bitmap is active.
12746 * Should be bigger than 512 and must be power of 2.
12747 * It is expecting the value in bytes.
12749 if (sysfs_set_num(info
, NULL
, "bitmap/chunksize",
12750 __cpu_to_le32(chunksize
))) {
12751 dprintf("failed to set bitmap/chunksize\n");
12755 /* It is expecting the value in sectors. */
12756 if (sysfs_set_num(info
, NULL
, "bitmap/space",
12757 __cpu_to_le64(IMSM_BITMAP_AREA_SIZE
))) {
12758 dprintf("failed to set bitmap/space\n");
12762 /* Determines the delay between the bitmap updates.
12763 * It is expecting the value in seconds.
12765 if (sysfs_set_num(info
, NULL
, "bitmap/time_base",
12766 __cpu_to_le64(IMSM_DEFAULT_BITMAP_DAEMON_SLEEP
))) {
12767 dprintf("failed to set bitmap/time_base\n");
12771 /* It is expecting the value in sectors with a sign at the beginning. */
12772 if (sysfs_set_str(info
, NULL
, "bitmap/location", location
)) {
12773 dprintf("failed to set bitmap/location\n");
12780 /*******************************************************************************
12781 * Function: set_bitmap_imsm
12782 * Description: Setup the bitmap for the given volume
12784 * st : supertype information
12785 * info : info about the volume where the bitmap should be setup
12790 ******************************************************************************/
12791 static int set_bitmap_imsm(struct supertype
*st
, struct mdinfo
*info
)
12793 struct intel_super
*super
= st
->sb
;
12794 int prev_current_vol
= super
->current_vol
;
12795 struct imsm_dev
*dev
;
12797 char location
[16] = "";
12798 unsigned long long chunksize
;
12799 struct intel_dev
*dev_it
;
12801 for (dev_it
= super
->devlist
; dev_it
; dev_it
= dev_it
->next
) {
12802 super
->current_vol
= dev_it
->index
;
12803 dev
= get_imsm_dev(super
, super
->current_vol
);
12805 if (is_vol_to_setup_bitmap(info
, dev
)) {
12806 if (validate_internal_bitmap_imsm(st
)) {
12807 dprintf("bitmap header validation failed\n");
12811 chunksize
= calculate_bitmap_chunksize(st
, dev
);
12812 dprintf("chunk size is %llu\n", chunksize
);
12814 snprintf(location
, sizeof(location
), "+%llu",
12815 get_bitmap_sector(super
, super
->current_vol
));
12816 dprintf("bitmap offset is %s\n", location
);
12818 if (set_bitmap_sysfs(info
, chunksize
, location
)) {
12819 dprintf("cannot setup the bitmap\n");
12826 super
->current_vol
= prev_current_vol
;
12830 struct superswitch super_imsm
= {
12831 .examine_super
= examine_super_imsm
,
12832 .brief_examine_super
= brief_examine_super_imsm
,
12833 .brief_examine_subarrays
= brief_examine_subarrays_imsm
,
12834 .export_examine_super
= export_examine_super_imsm
,
12835 .detail_super
= detail_super_imsm
,
12836 .brief_detail_super
= brief_detail_super_imsm
,
12837 .write_init_super
= write_init_super_imsm
,
12838 .validate_geometry
= validate_geometry_imsm
,
12839 .add_to_super
= add_to_super_imsm
,
12840 .remove_from_super
= remove_from_super_imsm
,
12841 .detail_platform
= detail_platform_imsm
,
12842 .export_detail_platform
= export_detail_platform_imsm
,
12843 .kill_subarray
= kill_subarray_imsm
,
12844 .update_subarray
= update_subarray_imsm
,
12845 .load_container
= load_container_imsm
,
12846 .default_geometry
= default_geometry_imsm
,
12847 .get_disk_controller_domain
= imsm_get_disk_controller_domain
,
12848 .reshape_super
= imsm_reshape_super
,
12849 .manage_reshape
= imsm_manage_reshape
,
12850 .recover_backup
= recover_backup_imsm
,
12851 .examine_badblocks
= examine_badblocks_imsm
,
12852 .match_home
= match_home_imsm
,
12853 .uuid_from_super
= uuid_from_super_imsm
,
12854 .getinfo_super
= getinfo_super_imsm
,
12855 .getinfo_super_disks
= getinfo_super_disks_imsm
,
12856 .update_super
= update_super_imsm
,
12858 .avail_size
= avail_size_imsm
,
12859 .get_spare_criteria
= get_spare_criteria_imsm
,
12861 .compare_super
= compare_super_imsm
,
12863 .load_super
= load_super_imsm
,
12864 .init_super
= init_super_imsm
,
12865 .store_super
= store_super_imsm
,
12866 .free_super
= free_super_imsm
,
12867 .match_metadata_desc
= match_metadata_desc_imsm
,
12868 .container_content
= container_content_imsm
,
12869 .validate_container
= validate_container_imsm
,
12871 .add_internal_bitmap
= add_internal_bitmap_imsm
,
12872 .locate_bitmap
= locate_bitmap_imsm
,
12873 .write_bitmap
= write_init_bitmap_imsm
,
12874 .set_bitmap
= set_bitmap_imsm
,
12876 .write_init_ppl
= write_init_ppl_imsm
,
12877 .validate_ppl
= validate_ppl_imsm
,
12883 .open_new
= imsm_open_new
,
12884 .set_array_state
= imsm_set_array_state
,
12885 .set_disk
= imsm_set_disk
,
12886 .sync_metadata
= imsm_sync_metadata
,
12887 .activate_spare
= imsm_activate_spare
,
12888 .process_update
= imsm_process_update
,
12889 .prepare_update
= imsm_prepare_update
,
12890 .record_bad_block
= imsm_record_badblock
,
12891 .clear_bad_block
= imsm_clear_badblock
,
12892 .get_bad_blocks
= imsm_get_badblocks
,