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)
370 * enum imsm_status - internal IMSM return values representation.
371 * @STATUS_OK: function succeeded.
372 * @STATUS_ERROR: General error ocurred (not specified).
374 * Typedefed to imsm_status_t.
376 typedef enum imsm_status
{
377 IMSM_STATUS_ERROR
= -1,
384 * 2: metadata does not match
392 struct md_list
*next
;
395 #define pr_vrb(fmt, arg...) (void) (verbose && pr_err(fmt, ##arg))
397 static __u8
migr_type(struct imsm_dev
*dev
)
399 if (dev
->vol
.migr_type
== MIGR_VERIFY
&&
400 dev
->status
& DEV_VERIFY_AND_FIX
)
403 return dev
->vol
.migr_type
;
406 static void set_migr_type(struct imsm_dev
*dev
, __u8 migr_type
)
408 /* for compatibility with older oroms convert MIGR_REPAIR, into
409 * MIGR_VERIFY w/ DEV_VERIFY_AND_FIX status
411 if (migr_type
== MIGR_REPAIR
) {
412 dev
->vol
.migr_type
= MIGR_VERIFY
;
413 dev
->status
|= DEV_VERIFY_AND_FIX
;
415 dev
->vol
.migr_type
= migr_type
;
416 dev
->status
&= ~DEV_VERIFY_AND_FIX
;
420 static unsigned int sector_count(__u32 bytes
, unsigned int sector_size
)
422 return ROUND_UP(bytes
, sector_size
) / sector_size
;
425 static unsigned int mpb_sectors(struct imsm_super
*mpb
,
426 unsigned int sector_size
)
428 return sector_count(__le32_to_cpu(mpb
->mpb_size
), sector_size
);
432 struct imsm_dev
*dev
;
433 struct intel_dev
*next
;
438 enum sys_dev_type type
;
441 struct intel_hba
*next
;
448 /* internal representation of IMSM metadata */
451 void *buf
; /* O_DIRECT buffer for reading/writing metadata */
452 struct imsm_super
*anchor
; /* immovable parameters */
455 void *migr_rec_buf
; /* buffer for I/O operations */
456 struct migr_record
*migr_rec
; /* migration record */
458 int clean_migration_record_by_mdmon
; /* when reshape is switched to next
459 array, it indicates that mdmon is allowed to clean migration
461 size_t len
; /* size of the 'buf' allocation */
462 size_t extra_space
; /* extra space in 'buf' that is not used yet */
463 void *next_buf
; /* for realloc'ing buf from the manager */
465 int updates_pending
; /* count of pending updates for mdmon */
466 int current_vol
; /* index of raid device undergoing creation */
467 unsigned long long create_offset
; /* common start for 'current_vol' */
468 __u32 random
; /* random data for seeding new family numbers */
469 struct intel_dev
*devlist
;
470 unsigned int sector_size
; /* sector size of used member drives */
474 __u8 serial
[MAX_RAID_SERIAL_LEN
];
477 struct imsm_disk disk
;
480 struct extent
*e
; /* for determining freespace @ create */
481 int raiddisk
; /* slot to fill in autolayout */
483 } *disks
, *current_disk
;
484 struct dl
*disk_mgmt_list
; /* list of disks to add/remove while mdmon
486 struct dl
*missing
; /* disks removed while we weren't looking */
487 struct bbm_log
*bbm_log
;
488 struct intel_hba
*hba
; /* device path of the raid controller for this metadata */
489 const struct imsm_orom
*orom
; /* platform firmware support */
490 struct intel_super
*next
; /* (temp) list for disambiguating family_num */
491 struct md_bb bb
; /* memory for get_bad_blocks call */
495 struct imsm_disk disk
;
496 #define IMSM_UNKNOWN_OWNER (-1)
498 struct intel_disk
*next
;
502 unsigned long long start
, size
;
505 /* definitions of reshape process types */
506 enum imsm_reshape_type
{
512 /* definition of messages passed to imsm_process_update */
513 enum imsm_update_type
{
514 update_activate_spare
,
518 update_add_remove_disk
,
519 update_reshape_container_disks
,
520 update_reshape_migration
,
522 update_general_migration_checkpoint
,
524 update_prealloc_badblocks_mem
,
528 struct imsm_update_activate_spare
{
529 enum imsm_update_type type
;
533 struct imsm_update_activate_spare
*next
;
539 unsigned long long size
;
546 enum takeover_direction
{
550 struct imsm_update_takeover
{
551 enum imsm_update_type type
;
553 enum takeover_direction direction
;
556 struct imsm_update_reshape
{
557 enum imsm_update_type type
;
561 int new_disks
[1]; /* new_raid_disks - old_raid_disks makedev number */
564 struct imsm_update_reshape_migration
{
565 enum imsm_update_type type
;
568 /* fields for array migration changes
575 int new_disks
[1]; /* new_raid_disks - old_raid_disks makedev number */
578 struct imsm_update_size_change
{
579 enum imsm_update_type type
;
584 struct imsm_update_general_migration_checkpoint
{
585 enum imsm_update_type type
;
586 __u64 curr_migr_unit
;
590 __u8 serial
[MAX_RAID_SERIAL_LEN
];
593 struct imsm_update_create_array
{
594 enum imsm_update_type type
;
599 struct imsm_update_kill_array
{
600 enum imsm_update_type type
;
604 struct imsm_update_rename_array
{
605 enum imsm_update_type type
;
606 __u8 name
[MAX_RAID_SERIAL_LEN
];
610 struct imsm_update_add_remove_disk
{
611 enum imsm_update_type type
;
614 struct imsm_update_prealloc_bb_mem
{
615 enum imsm_update_type type
;
618 struct imsm_update_rwh_policy
{
619 enum imsm_update_type type
;
624 static const char *_sys_dev_type
[] = {
625 [SYS_DEV_UNKNOWN
] = "Unknown",
626 [SYS_DEV_SAS
] = "SAS",
627 [SYS_DEV_SATA
] = "SATA",
628 [SYS_DEV_NVME
] = "NVMe",
629 [SYS_DEV_VMD
] = "VMD"
632 const char *get_sys_dev_type(enum sys_dev_type type
)
634 if (type
>= SYS_DEV_MAX
)
635 type
= SYS_DEV_UNKNOWN
;
637 return _sys_dev_type
[type
];
640 static struct intel_hba
* alloc_intel_hba(struct sys_dev
*device
)
642 struct intel_hba
*result
= xmalloc(sizeof(*result
));
644 result
->type
= device
->type
;
645 result
->path
= xstrdup(device
->path
);
647 if (result
->path
&& (result
->pci_id
= strrchr(result
->path
, '/')) != NULL
)
653 static struct intel_hba
* find_intel_hba(struct intel_hba
*hba
, struct sys_dev
*device
)
655 struct intel_hba
*result
;
657 for (result
= hba
; result
; result
= result
->next
) {
658 if (result
->type
== device
->type
&& strcmp(result
->path
, device
->path
) == 0)
664 static int attach_hba_to_super(struct intel_super
*super
, struct sys_dev
*device
)
666 struct intel_hba
*hba
;
668 /* check if disk attached to Intel HBA */
669 hba
= find_intel_hba(super
->hba
, device
);
672 /* Check if HBA is already attached to super */
673 if (super
->hba
== NULL
) {
674 super
->hba
= alloc_intel_hba(device
);
679 /* Intel metadata allows for all disks attached to the same type HBA.
680 * Do not support HBA types mixing
682 if (device
->type
!= hba
->type
)
685 /* Multiple same type HBAs can be used if they share the same OROM */
686 const struct imsm_orom
*device_orom
= get_orom_by_device_id(device
->dev_id
);
688 if (device_orom
!= super
->orom
)
694 hba
->next
= alloc_intel_hba(device
);
698 static struct sys_dev
* find_disk_attached_hba(int fd
, const char *devname
)
700 struct sys_dev
*list
, *elem
;
703 if ((list
= find_intel_devices()) == NULL
)
706 if (!is_fd_valid(fd
))
707 disk_path
= (char *) devname
;
709 disk_path
= diskfd_to_devpath(fd
, 1, NULL
);
714 for (elem
= list
; elem
; elem
= elem
->next
)
715 if (path_attached_to_hba(disk_path
, elem
->path
))
718 if (disk_path
!= devname
)
724 static int find_intel_hba_capability(int fd
, struct intel_super
*super
,
727 static struct supertype
*match_metadata_desc_imsm(char *arg
)
729 struct supertype
*st
;
731 if (strcmp(arg
, "imsm") != 0 &&
732 strcmp(arg
, "default") != 0
736 st
= xcalloc(1, sizeof(*st
));
737 st
->ss
= &super_imsm
;
738 st
->max_devs
= IMSM_MAX_DEVICES
;
739 st
->minor_version
= 0;
744 static __u8
*get_imsm_version(struct imsm_super
*mpb
)
746 return &mpb
->sig
[MPB_SIG_LEN
];
749 /* retrieve a disk directly from the anchor when the anchor is known to be
750 * up-to-date, currently only at load time
752 static struct imsm_disk
*__get_imsm_disk(struct imsm_super
*mpb
, __u8 index
)
754 if (index
>= mpb
->num_disks
)
756 return &mpb
->disk
[index
];
759 /* retrieve the disk description based on a index of the disk
762 static struct dl
*get_imsm_dl_disk(struct intel_super
*super
, __u8 index
)
766 for (d
= super
->disks
; d
; d
= d
->next
)
767 if (d
->index
== index
)
772 /* retrieve a disk from the parsed metadata */
773 static struct imsm_disk
*get_imsm_disk(struct intel_super
*super
, __u8 index
)
777 dl
= get_imsm_dl_disk(super
, index
);
784 /* generate a checksum directly from the anchor when the anchor is known to be
785 * up-to-date, currently only at load or write_super after coalescing
787 static __u32
__gen_imsm_checksum(struct imsm_super
*mpb
)
789 __u32 end
= mpb
->mpb_size
/ sizeof(end
);
790 __u32
*p
= (__u32
*) mpb
;
794 sum
+= __le32_to_cpu(*p
);
798 return sum
- __le32_to_cpu(mpb
->check_sum
);
801 static size_t sizeof_imsm_map(struct imsm_map
*map
)
803 return sizeof(struct imsm_map
) + sizeof(__u32
) * (map
->num_members
- 1);
806 struct imsm_map
*get_imsm_map(struct imsm_dev
*dev
, int second_map
)
808 /* A device can have 2 maps if it is in the middle of a migration.
810 * MAP_0 - we return the first map
811 * MAP_1 - we return the second map if it exists, else NULL
812 * MAP_X - we return the second map if it exists, else the first
814 struct imsm_map
*map
= &dev
->vol
.map
[0];
815 struct imsm_map
*map2
= NULL
;
817 if (dev
->vol
.migr_state
)
818 map2
= (void *)map
+ sizeof_imsm_map(map
);
820 switch (second_map
) {
837 /* return the size of the device.
838 * migr_state increases the returned size if map[0] were to be duplicated
840 static size_t sizeof_imsm_dev(struct imsm_dev
*dev
, int migr_state
)
842 size_t size
= sizeof(*dev
) - sizeof(struct imsm_map
) +
843 sizeof_imsm_map(get_imsm_map(dev
, MAP_0
));
845 /* migrating means an additional map */
846 if (dev
->vol
.migr_state
)
847 size
+= sizeof_imsm_map(get_imsm_map(dev
, MAP_1
));
849 size
+= sizeof_imsm_map(get_imsm_map(dev
, MAP_0
));
854 /* retrieve disk serial number list from a metadata update */
855 static struct disk_info
*get_disk_info(struct imsm_update_create_array
*update
)
858 struct disk_info
*inf
;
860 inf
= u
+ sizeof(*update
) - sizeof(struct imsm_dev
) +
861 sizeof_imsm_dev(&update
->dev
, 0);
867 * __get_imsm_dev() - Get device with index from imsm_super.
868 * @mpb: &imsm_super pointer, not NULL.
869 * @index: Device index.
871 * Function works as non-NULL, aborting in such a case,
872 * when NULL would be returned.
874 * Device index should be in range 0 up to num_raid_devs.
875 * Function assumes the index was already verified.
876 * Index must be valid, otherwise abort() is called.
878 * Return: Pointer to corresponding imsm_dev.
881 static struct imsm_dev
*__get_imsm_dev(struct imsm_super
*mpb
, __u8 index
)
887 if (index
>= mpb
->num_raid_devs
)
890 /* devices start after all disks */
891 offset
= ((void *) &mpb
->disk
[mpb
->num_disks
]) - _mpb
;
893 for (i
= 0; i
<= index
; i
++, offset
+= sizeof_imsm_dev(_mpb
+ offset
, 0))
895 return _mpb
+ offset
;
897 pr_err("cannot find imsm_dev with index %u in imsm_super\n", index
);
902 * get_imsm_dev() - Get device with index from intel_super.
903 * @super: &intel_super pointer, not NULL.
904 * @index: Device index.
906 * Function works as non-NULL, aborting in such a case,
907 * when NULL would be returned.
909 * Device index should be in range 0 up to num_raid_devs.
910 * Function assumes the index was already verified.
911 * Index must be valid, otherwise abort() is called.
913 * Return: Pointer to corresponding imsm_dev.
916 static struct imsm_dev
*get_imsm_dev(struct intel_super
*super
, __u8 index
)
918 struct intel_dev
*dv
;
920 if (index
>= super
->anchor
->num_raid_devs
)
923 for (dv
= super
->devlist
; dv
; dv
= dv
->next
)
924 if (dv
->index
== index
)
927 pr_err("cannot find imsm_dev with index %u in intel_super\n", index
);
931 static inline unsigned long long __le48_to_cpu(const struct bbm_log_block_addr
934 return ((((__u64
)__le32_to_cpu(addr
->dw1
)) << 16) |
935 __le16_to_cpu(addr
->w1
));
938 static inline struct bbm_log_block_addr
__cpu_to_le48(unsigned long long sec
)
940 struct bbm_log_block_addr addr
;
942 addr
.w1
= __cpu_to_le16((__u16
)(sec
& 0xffff));
943 addr
.dw1
= __cpu_to_le32((__u32
)(sec
>> 16) & 0xffffffff);
947 /* get size of the bbm log */
948 static __u32
get_imsm_bbm_log_size(struct bbm_log
*log
)
950 if (!log
|| log
->entry_count
== 0)
953 return sizeof(log
->signature
) +
954 sizeof(log
->entry_count
) +
955 log
->entry_count
* sizeof(struct bbm_log_entry
);
958 /* check if bad block is not partially stored in bbm log */
959 static int is_stored_in_bbm(struct bbm_log
*log
, const __u8 idx
, const unsigned
960 long long sector
, const int length
, __u32
*pos
)
964 for (i
= *pos
; i
< log
->entry_count
; i
++) {
965 struct bbm_log_entry
*entry
= &log
->marked_block_entries
[i
];
966 unsigned long long bb_start
;
967 unsigned long long bb_end
;
969 bb_start
= __le48_to_cpu(&entry
->defective_block_start
);
970 bb_end
= bb_start
+ (entry
->marked_count
+ 1);
972 if ((entry
->disk_ordinal
== idx
) && (bb_start
>= sector
) &&
973 (bb_end
<= sector
+ length
)) {
981 /* record new bad block in bbm log */
982 static int record_new_badblock(struct bbm_log
*log
, const __u8 idx
, unsigned
983 long long sector
, int length
)
987 struct bbm_log_entry
*entry
= NULL
;
989 while (is_stored_in_bbm(log
, idx
, sector
, length
, &pos
)) {
990 struct bbm_log_entry
*e
= &log
->marked_block_entries
[pos
];
992 if ((e
->marked_count
+ 1 == BBM_LOG_MAX_LBA_ENTRY_VAL
) &&
993 (__le48_to_cpu(&e
->defective_block_start
) == sector
)) {
994 sector
+= BBM_LOG_MAX_LBA_ENTRY_VAL
;
995 length
-= BBM_LOG_MAX_LBA_ENTRY_VAL
;
1004 int cnt
= (length
<= BBM_LOG_MAX_LBA_ENTRY_VAL
) ? length
:
1005 BBM_LOG_MAX_LBA_ENTRY_VAL
;
1006 entry
->defective_block_start
= __cpu_to_le48(sector
);
1007 entry
->marked_count
= cnt
- 1;
1014 new_bb
= ROUND_UP(length
, BBM_LOG_MAX_LBA_ENTRY_VAL
) /
1015 BBM_LOG_MAX_LBA_ENTRY_VAL
;
1016 if (log
->entry_count
+ new_bb
> BBM_LOG_MAX_ENTRIES
)
1019 while (length
> 0) {
1020 int cnt
= (length
<= BBM_LOG_MAX_LBA_ENTRY_VAL
) ? length
:
1021 BBM_LOG_MAX_LBA_ENTRY_VAL
;
1022 struct bbm_log_entry
*entry
=
1023 &log
->marked_block_entries
[log
->entry_count
];
1025 entry
->defective_block_start
= __cpu_to_le48(sector
);
1026 entry
->marked_count
= cnt
- 1;
1027 entry
->disk_ordinal
= idx
;
1038 /* clear all bad blocks for given disk */
1039 static void clear_disk_badblocks(struct bbm_log
*log
, const __u8 idx
)
1043 while (i
< log
->entry_count
) {
1044 struct bbm_log_entry
*entries
= log
->marked_block_entries
;
1046 if (entries
[i
].disk_ordinal
== idx
) {
1047 if (i
< log
->entry_count
- 1)
1048 entries
[i
] = entries
[log
->entry_count
- 1];
1056 /* clear given bad block */
1057 static int clear_badblock(struct bbm_log
*log
, const __u8 idx
, const unsigned
1058 long long sector
, const int length
) {
1061 while (i
< log
->entry_count
) {
1062 struct bbm_log_entry
*entries
= log
->marked_block_entries
;
1064 if ((entries
[i
].disk_ordinal
== idx
) &&
1065 (__le48_to_cpu(&entries
[i
].defective_block_start
) ==
1066 sector
) && (entries
[i
].marked_count
+ 1 == length
)) {
1067 if (i
< log
->entry_count
- 1)
1068 entries
[i
] = entries
[log
->entry_count
- 1];
1078 /* allocate and load BBM log from metadata */
1079 static int load_bbm_log(struct intel_super
*super
)
1081 struct imsm_super
*mpb
= super
->anchor
;
1082 __u32 bbm_log_size
= __le32_to_cpu(mpb
->bbm_log_size
);
1084 super
->bbm_log
= xcalloc(1, sizeof(struct bbm_log
));
1085 if (!super
->bbm_log
)
1089 struct bbm_log
*log
= (void *)mpb
+
1090 __le32_to_cpu(mpb
->mpb_size
) - bbm_log_size
;
1094 if (bbm_log_size
< sizeof(log
->signature
) +
1095 sizeof(log
->entry_count
))
1098 entry_count
= __le32_to_cpu(log
->entry_count
);
1099 if ((__le32_to_cpu(log
->signature
) != BBM_LOG_SIGNATURE
) ||
1100 (entry_count
> BBM_LOG_MAX_ENTRIES
))
1104 sizeof(log
->signature
) + sizeof(log
->entry_count
) +
1105 entry_count
* sizeof(struct bbm_log_entry
))
1108 memcpy(super
->bbm_log
, log
, bbm_log_size
);
1110 super
->bbm_log
->signature
= __cpu_to_le32(BBM_LOG_SIGNATURE
);
1111 super
->bbm_log
->entry_count
= 0;
1117 /* checks if bad block is within volume boundaries */
1118 static int is_bad_block_in_volume(const struct bbm_log_entry
*entry
,
1119 const unsigned long long start_sector
,
1120 const unsigned long long size
)
1122 unsigned long long bb_start
;
1123 unsigned long long bb_end
;
1125 bb_start
= __le48_to_cpu(&entry
->defective_block_start
);
1126 bb_end
= bb_start
+ (entry
->marked_count
+ 1);
1128 if (((bb_start
>= start_sector
) && (bb_start
< start_sector
+ size
)) ||
1129 ((bb_end
>= start_sector
) && (bb_end
<= start_sector
+ size
)))
1135 /* get list of bad blocks on a drive for a volume */
1136 static void get_volume_badblocks(const struct bbm_log
*log
, const __u8 idx
,
1137 const unsigned long long start_sector
,
1138 const unsigned long long size
,
1144 for (i
= 0; i
< log
->entry_count
; i
++) {
1145 const struct bbm_log_entry
*ent
=
1146 &log
->marked_block_entries
[i
];
1147 struct md_bb_entry
*bb
;
1149 if ((ent
->disk_ordinal
== idx
) &&
1150 is_bad_block_in_volume(ent
, start_sector
, size
)) {
1152 if (!bbs
->entries
) {
1153 bbs
->entries
= xmalloc(BBM_LOG_MAX_ENTRIES
*
1159 bb
= &bbs
->entries
[count
++];
1160 bb
->sector
= __le48_to_cpu(&ent
->defective_block_start
);
1161 bb
->length
= ent
->marked_count
+ 1;
1169 * == MAP_0 get first map
1170 * == MAP_1 get second map
1171 * == MAP_X than get map according to the current migr_state
1173 static __u32
get_imsm_ord_tbl_ent(struct imsm_dev
*dev
,
1177 struct imsm_map
*map
;
1179 map
= get_imsm_map(dev
, second_map
);
1181 /* top byte identifies disk under rebuild */
1182 return __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
1185 #define ord_to_idx(ord) (((ord) << 8) >> 8)
1186 static __u32
get_imsm_disk_idx(struct imsm_dev
*dev
, int slot
, int second_map
)
1188 __u32 ord
= get_imsm_ord_tbl_ent(dev
, slot
, second_map
);
1190 return ord_to_idx(ord
);
1193 static void set_imsm_ord_tbl_ent(struct imsm_map
*map
, int slot
, __u32 ord
)
1195 map
->disk_ord_tbl
[slot
] = __cpu_to_le32(ord
);
1198 static int get_imsm_disk_slot(struct imsm_map
*map
, const unsigned int idx
)
1203 for (slot
= 0; slot
< map
->num_members
; slot
++) {
1204 ord
= __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
1205 if (ord_to_idx(ord
) == idx
)
1209 return IMSM_STATUS_ERROR
;
1212 static int get_imsm_raid_level(struct imsm_map
*map
)
1214 if (map
->raid_level
== 1) {
1215 if (map
->num_members
== 2)
1221 return map
->raid_level
;
1225 * get_disk_slot_in_dev() - retrieve disk slot from &imsm_dev.
1226 * @super: &intel_super pointer, not NULL.
1227 * @dev_idx: imsm device index.
1230 * Return: Slot on success, IMSM_STATUS_ERROR otherwise.
1232 static int get_disk_slot_in_dev(struct intel_super
*super
, const __u8 dev_idx
,
1233 const unsigned int idx
)
1235 struct imsm_dev
*dev
= get_imsm_dev(super
, dev_idx
);
1236 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
1238 return get_imsm_disk_slot(map
, idx
);
1241 static int cmp_extent(const void *av
, const void *bv
)
1243 const struct extent
*a
= av
;
1244 const struct extent
*b
= bv
;
1245 if (a
->start
< b
->start
)
1247 if (a
->start
> b
->start
)
1252 static int count_memberships(struct dl
*dl
, struct intel_super
*super
)
1254 int memberships
= 0;
1257 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++)
1258 if (get_disk_slot_in_dev(super
, i
, dl
->index
) >= 0)
1264 static __u32
imsm_min_reserved_sectors(struct intel_super
*super
);
1266 static int split_ull(unsigned long long n
, void *lo
, void *hi
)
1268 if (lo
== 0 || hi
== 0)
1270 __put_unaligned32(__cpu_to_le32((__u32
)n
), lo
);
1271 __put_unaligned32(__cpu_to_le32((n
>> 32)), hi
);
1275 static unsigned long long join_u32(__u32 lo
, __u32 hi
)
1277 return (unsigned long long)__le32_to_cpu(lo
) |
1278 (((unsigned long long)__le32_to_cpu(hi
)) << 32);
1281 static unsigned long long total_blocks(struct imsm_disk
*disk
)
1285 return join_u32(disk
->total_blocks_lo
, disk
->total_blocks_hi
);
1289 * imsm_num_data_members() - get data drives count for an array.
1290 * @map: Map to analyze.
1292 * num_data_members value represents minimal count of drives for level.
1293 * The name of the property could be misleading for RAID5 with asymmetric layout
1294 * because some data required to be calculated from parity.
1295 * The property is extracted from level and num_members value.
1297 * Return: num_data_members value on success, zero otherwise.
1299 static __u8
imsm_num_data_members(struct imsm_map
*map
)
1301 switch (get_imsm_raid_level(map
)) {
1303 return map
->num_members
;
1306 return map
->num_members
/ 2;
1308 return map
->num_members
- 1;
1310 dprintf("unsupported raid level\n");
1315 static unsigned long long pba_of_lba0(struct imsm_map
*map
)
1319 return join_u32(map
->pba_of_lba0_lo
, map
->pba_of_lba0_hi
);
1322 static unsigned long long blocks_per_member(struct imsm_map
*map
)
1326 return join_u32(map
->blocks_per_member_lo
, map
->blocks_per_member_hi
);
1329 static unsigned long long num_data_stripes(struct imsm_map
*map
)
1333 return join_u32(map
->num_data_stripes_lo
, map
->num_data_stripes_hi
);
1336 static unsigned long long vol_curr_migr_unit(struct imsm_dev
*dev
)
1341 return join_u32(dev
->vol
.curr_migr_unit_lo
, dev
->vol
.curr_migr_unit_hi
);
1344 static unsigned long long imsm_dev_size(struct imsm_dev
*dev
)
1348 return join_u32(dev
->size_low
, dev
->size_high
);
1351 static unsigned long long migr_chkp_area_pba(struct migr_record
*migr_rec
)
1353 if (migr_rec
== NULL
)
1355 return join_u32(migr_rec
->ckpt_area_pba_lo
,
1356 migr_rec
->ckpt_area_pba_hi
);
1359 static unsigned long long current_migr_unit(struct migr_record
*migr_rec
)
1361 if (migr_rec
== NULL
)
1363 return join_u32(migr_rec
->curr_migr_unit_lo
,
1364 migr_rec
->curr_migr_unit_hi
);
1367 static unsigned long long migr_dest_1st_member_lba(struct migr_record
*migr_rec
)
1369 if (migr_rec
== NULL
)
1371 return join_u32(migr_rec
->dest_1st_member_lba_lo
,
1372 migr_rec
->dest_1st_member_lba_hi
);
1375 static unsigned long long get_num_migr_units(struct migr_record
*migr_rec
)
1377 if (migr_rec
== NULL
)
1379 return join_u32(migr_rec
->num_migr_units_lo
,
1380 migr_rec
->num_migr_units_hi
);
1383 static void set_total_blocks(struct imsm_disk
*disk
, unsigned long long n
)
1385 split_ull(n
, &disk
->total_blocks_lo
, &disk
->total_blocks_hi
);
1389 * set_num_domains() - Set number of domains for an array.
1390 * @map: Map to be updated.
1392 * num_domains property represents copies count of each data drive, thus make
1393 * it meaningful only for RAID1 and RAID10. IMSM supports two domains for
1396 static void set_num_domains(struct imsm_map
*map
)
1398 int level
= get_imsm_raid_level(map
);
1400 if (level
== 1 || level
== 10)
1401 map
->num_domains
= 2;
1403 map
->num_domains
= 1;
1406 static void set_pba_of_lba0(struct imsm_map
*map
, unsigned long long n
)
1408 split_ull(n
, &map
->pba_of_lba0_lo
, &map
->pba_of_lba0_hi
);
1411 static void set_blocks_per_member(struct imsm_map
*map
, unsigned long long n
)
1413 split_ull(n
, &map
->blocks_per_member_lo
, &map
->blocks_per_member_hi
);
1416 static void set_num_data_stripes(struct imsm_map
*map
, unsigned long long n
)
1418 split_ull(n
, &map
->num_data_stripes_lo
, &map
->num_data_stripes_hi
);
1422 * update_num_data_stripes() - Calculate and update num_data_stripes value.
1423 * @map: map to be updated.
1424 * @dev_size: size of volume.
1426 * num_data_stripes value is addictionally divided by num_domains, therefore for
1427 * levels where num_domains is not 1, nds is a part of real value.
1429 static void update_num_data_stripes(struct imsm_map
*map
,
1430 unsigned long long dev_size
)
1432 unsigned long long nds
= dev_size
/ imsm_num_data_members(map
);
1434 nds
/= map
->num_domains
;
1435 nds
/= map
->blocks_per_strip
;
1436 set_num_data_stripes(map
, nds
);
1439 static void set_vol_curr_migr_unit(struct imsm_dev
*dev
, unsigned long long n
)
1444 split_ull(n
, &dev
->vol
.curr_migr_unit_lo
, &dev
->vol
.curr_migr_unit_hi
);
1447 static void set_imsm_dev_size(struct imsm_dev
*dev
, unsigned long long n
)
1449 split_ull(n
, &dev
->size_low
, &dev
->size_high
);
1452 static void set_migr_chkp_area_pba(struct migr_record
*migr_rec
,
1453 unsigned long long n
)
1455 split_ull(n
, &migr_rec
->ckpt_area_pba_lo
, &migr_rec
->ckpt_area_pba_hi
);
1458 static void set_current_migr_unit(struct migr_record
*migr_rec
,
1459 unsigned long long n
)
1461 split_ull(n
, &migr_rec
->curr_migr_unit_lo
,
1462 &migr_rec
->curr_migr_unit_hi
);
1465 static void set_migr_dest_1st_member_lba(struct migr_record
*migr_rec
,
1466 unsigned long long n
)
1468 split_ull(n
, &migr_rec
->dest_1st_member_lba_lo
,
1469 &migr_rec
->dest_1st_member_lba_hi
);
1472 static void set_num_migr_units(struct migr_record
*migr_rec
,
1473 unsigned long long n
)
1475 split_ull(n
, &migr_rec
->num_migr_units_lo
,
1476 &migr_rec
->num_migr_units_hi
);
1479 static unsigned long long per_dev_array_size(struct imsm_map
*map
)
1481 unsigned long long array_size
= 0;
1486 array_size
= num_data_stripes(map
) * map
->blocks_per_strip
;
1487 if (get_imsm_raid_level(map
) == 1 || get_imsm_raid_level(map
) == 10)
1493 static struct extent
*get_extents(struct intel_super
*super
, struct dl
*dl
,
1494 int get_minimal_reservation
)
1496 /* find a list of used extents on the given physical device */
1497 struct extent
*rv
, *e
;
1499 int memberships
= count_memberships(dl
, super
);
1502 /* trim the reserved area for spares, so they can join any array
1503 * regardless of whether the OROM has assigned sectors from the
1504 * IMSM_RESERVED_SECTORS region
1506 if (dl
->index
== -1 || get_minimal_reservation
)
1507 reservation
= imsm_min_reserved_sectors(super
);
1509 reservation
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
1511 rv
= xcalloc(sizeof(struct extent
), (memberships
+ 1));
1514 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
1515 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
1516 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
1518 if (get_imsm_disk_slot(map
, dl
->index
) >= 0) {
1519 e
->start
= pba_of_lba0(map
);
1520 e
->size
= per_dev_array_size(map
);
1524 qsort(rv
, memberships
, sizeof(*rv
), cmp_extent
);
1526 /* determine the start of the metadata
1527 * when no raid devices are defined use the default
1528 * ...otherwise allow the metadata to truncate the value
1529 * as is the case with older versions of imsm
1532 struct extent
*last
= &rv
[memberships
- 1];
1533 unsigned long long remainder
;
1535 remainder
= total_blocks(&dl
->disk
) - (last
->start
+ last
->size
);
1536 /* round down to 1k block to satisfy precision of the kernel
1540 /* make sure remainder is still sane */
1541 if (remainder
< (unsigned)ROUND_UP(super
->len
, 512) >> 9)
1542 remainder
= ROUND_UP(super
->len
, 512) >> 9;
1543 if (reservation
> remainder
)
1544 reservation
= remainder
;
1546 e
->start
= total_blocks(&dl
->disk
) - reservation
;
1551 /* try to determine how much space is reserved for metadata from
1552 * the last get_extents() entry, otherwise fallback to the
1555 static __u32
imsm_reserved_sectors(struct intel_super
*super
, struct dl
*dl
)
1561 /* for spares just return a minimal reservation which will grow
1562 * once the spare is picked up by an array
1564 if (dl
->index
== -1)
1565 return MPB_SECTOR_CNT
;
1567 e
= get_extents(super
, dl
, 0);
1569 return MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
1571 /* scroll to last entry */
1572 for (i
= 0; e
[i
].size
; i
++)
1575 rv
= total_blocks(&dl
->disk
) - e
[i
].start
;
1582 static int is_spare(struct imsm_disk
*disk
)
1584 return (disk
->status
& SPARE_DISK
) == SPARE_DISK
;
1587 static int is_configured(struct imsm_disk
*disk
)
1589 return (disk
->status
& CONFIGURED_DISK
) == CONFIGURED_DISK
;
1592 static int is_failed(struct imsm_disk
*disk
)
1594 return (disk
->status
& FAILED_DISK
) == FAILED_DISK
;
1597 static int is_journal(struct imsm_disk
*disk
)
1599 return (disk
->status
& JOURNAL_DISK
) == JOURNAL_DISK
;
1602 /* round array size down to closest MB and ensure it splits evenly
1605 static unsigned long long round_size_to_mb(unsigned long long size
, unsigned int
1609 size
= (size
>> SECT_PER_MB_SHIFT
) << SECT_PER_MB_SHIFT
;
1615 static int able_to_resync(int raid_level
, int missing_disks
)
1617 int max_missing_disks
= 0;
1619 switch (raid_level
) {
1621 max_missing_disks
= 1;
1624 max_missing_disks
= 0;
1626 return missing_disks
<= max_missing_disks
;
1629 /* try to determine how much space is reserved for metadata from
1630 * the last get_extents() entry on the smallest active disk,
1631 * otherwise fallback to the default
1633 static __u32
imsm_min_reserved_sectors(struct intel_super
*super
)
1637 unsigned long long min_active
;
1639 __u32 rv
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
1640 struct dl
*dl
, *dl_min
= NULL
;
1646 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
1649 unsigned long long blocks
= total_blocks(&dl
->disk
);
1650 if (blocks
< min_active
|| min_active
== 0) {
1652 min_active
= blocks
;
1658 /* find last lba used by subarrays on the smallest active disk */
1659 e
= get_extents(super
, dl_min
, 0);
1662 for (i
= 0; e
[i
].size
; i
++)
1665 remainder
= min_active
- e
[i
].start
;
1668 /* to give priority to recovery we should not require full
1669 IMSM_RESERVED_SECTORS from the spare */
1670 rv
= MPB_SECTOR_CNT
+ NUM_BLOCKS_DIRTY_STRIPE_REGION
;
1672 /* if real reservation is smaller use that value */
1673 return (remainder
< rv
) ? remainder
: rv
;
1677 * Return minimum size of a spare and sector size
1678 * that can be used in this array
1680 int get_spare_criteria_imsm(struct supertype
*st
, struct spare_criteria
*c
)
1682 struct intel_super
*super
= st
->sb
;
1686 unsigned long long size
= 0;
1693 /* find first active disk in array */
1695 while (dl
&& (is_failed(&dl
->disk
) || dl
->index
== -1))
1699 /* find last lba used by subarrays */
1700 e
= get_extents(super
, dl
, 0);
1703 for (i
= 0; e
[i
].size
; i
++)
1706 size
= e
[i
-1].start
+ e
[i
-1].size
;
1709 /* add the amount of space needed for metadata */
1710 size
+= imsm_min_reserved_sectors(super
);
1712 c
->min_size
= size
* 512;
1713 c
->sector_size
= super
->sector_size
;
1718 static bool is_gen_migration(struct imsm_dev
*dev
);
1720 #define IMSM_4K_DIV 8
1722 static __u64
blocks_per_migr_unit(struct intel_super
*super
,
1723 struct imsm_dev
*dev
);
1725 static void print_imsm_dev(struct intel_super
*super
,
1726 struct imsm_dev
*dev
,
1732 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
1733 struct imsm_map
*map2
= get_imsm_map(dev
, MAP_1
);
1737 printf("[%.16s]:\n", dev
->volume
);
1738 printf(" Subarray : %d\n", super
->current_vol
);
1739 printf(" UUID : %s\n", uuid
);
1740 printf(" RAID Level : %d", get_imsm_raid_level(map
));
1742 printf(" <-- %d", get_imsm_raid_level(map2
));
1744 printf(" Members : %d", map
->num_members
);
1746 printf(" <-- %d", map2
->num_members
);
1748 printf(" Slots : [");
1749 for (i
= 0; i
< map
->num_members
; i
++) {
1750 ord
= get_imsm_ord_tbl_ent(dev
, i
, MAP_0
);
1751 printf("%s", ord
& IMSM_ORD_REBUILD
? "_" : "U");
1756 for (i
= 0; i
< map2
->num_members
; i
++) {
1757 ord
= get_imsm_ord_tbl_ent(dev
, i
, MAP_1
);
1758 printf("%s", ord
& IMSM_ORD_REBUILD
? "_" : "U");
1763 printf(" Failed disk : ");
1764 if (map
->failed_disk_num
== 0xff)
1767 printf("%i", map
->failed_disk_num
);
1769 slot
= get_imsm_disk_slot(map
, disk_idx
);
1771 ord
= get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
);
1772 printf(" This Slot : %d%s\n", slot
,
1773 ord
& IMSM_ORD_REBUILD
? " (out-of-sync)" : "");
1775 printf(" This Slot : ?\n");
1776 printf(" Sector Size : %u\n", super
->sector_size
);
1777 sz
= imsm_dev_size(dev
);
1778 printf(" Array Size : %llu%s\n",
1779 (unsigned long long)sz
* 512 / super
->sector_size
,
1780 human_size(sz
* 512));
1781 sz
= blocks_per_member(map
);
1782 printf(" Per Dev Size : %llu%s\n",
1783 (unsigned long long)sz
* 512 / super
->sector_size
,
1784 human_size(sz
* 512));
1785 printf(" Sector Offset : %llu\n",
1786 pba_of_lba0(map
) * 512 / super
->sector_size
);
1787 printf(" Num Stripes : %llu\n",
1788 num_data_stripes(map
));
1789 printf(" Chunk Size : %u KiB",
1790 __le16_to_cpu(map
->blocks_per_strip
) / 2);
1792 printf(" <-- %u KiB",
1793 __le16_to_cpu(map2
->blocks_per_strip
) / 2);
1795 printf(" Reserved : %d\n", __le32_to_cpu(dev
->reserved_blocks
));
1796 printf(" Migrate State : ");
1797 if (dev
->vol
.migr_state
) {
1798 if (migr_type(dev
) == MIGR_INIT
)
1799 printf("initialize\n");
1800 else if (migr_type(dev
) == MIGR_REBUILD
)
1801 printf("rebuild\n");
1802 else if (migr_type(dev
) == MIGR_VERIFY
)
1804 else if (migr_type(dev
) == MIGR_GEN_MIGR
)
1805 printf("general migration\n");
1806 else if (migr_type(dev
) == MIGR_STATE_CHANGE
)
1807 printf("state change\n");
1808 else if (migr_type(dev
) == MIGR_REPAIR
)
1811 printf("<unknown:%d>\n", migr_type(dev
));
1814 printf(" Map State : %s", map_state_str
[map
->map_state
]);
1815 if (dev
->vol
.migr_state
) {
1816 struct imsm_map
*map
= get_imsm_map(dev
, MAP_1
);
1818 printf(" <-- %s", map_state_str
[map
->map_state
]);
1819 printf("\n Checkpoint : %llu ", vol_curr_migr_unit(dev
));
1820 if (is_gen_migration(dev
) && (slot
> 1 || slot
< 0))
1823 printf("(%llu)", (unsigned long long)
1824 blocks_per_migr_unit(super
, dev
));
1827 printf(" Dirty State : %s\n", (dev
->vol
.dirty
& RAIDVOL_DIRTY
) ?
1829 printf(" RWH Policy : ");
1830 if (dev
->rwh_policy
== RWH_OFF
|| dev
->rwh_policy
== RWH_MULTIPLE_OFF
)
1832 else if (dev
->rwh_policy
== RWH_DISTRIBUTED
)
1833 printf("PPL distributed\n");
1834 else if (dev
->rwh_policy
== RWH_JOURNALING_DRIVE
)
1835 printf("PPL journaling drive\n");
1836 else if (dev
->rwh_policy
== RWH_MULTIPLE_DISTRIBUTED
)
1837 printf("Multiple distributed PPLs\n");
1838 else if (dev
->rwh_policy
== RWH_MULTIPLE_PPLS_JOURNALING_DRIVE
)
1839 printf("Multiple PPLs on journaling drive\n");
1840 else if (dev
->rwh_policy
== RWH_BITMAP
)
1841 printf("Write-intent bitmap\n");
1843 printf("<unknown:%d>\n", dev
->rwh_policy
);
1845 printf(" Volume ID : %u\n", dev
->my_vol_raid_dev_num
);
1848 static void print_imsm_disk(struct imsm_disk
*disk
,
1851 unsigned int sector_size
) {
1852 char str
[MAX_RAID_SERIAL_LEN
+ 1];
1855 if (index
< -1 || !disk
)
1859 snprintf(str
, MAX_RAID_SERIAL_LEN
+ 1, "%s", disk
->serial
);
1861 printf(" Disk%02d Serial : %s\n", index
, str
);
1863 printf(" Disk Serial : %s\n", str
);
1864 printf(" State :%s%s%s%s\n", is_spare(disk
) ? " spare" : "",
1865 is_configured(disk
) ? " active" : "",
1866 is_failed(disk
) ? " failed" : "",
1867 is_journal(disk
) ? " journal" : "");
1868 printf(" Id : %08x\n", __le32_to_cpu(disk
->scsi_id
));
1869 sz
= total_blocks(disk
) - reserved
;
1870 printf(" Usable Size : %llu%s\n",
1871 (unsigned long long)sz
* 512 / sector_size
,
1872 human_size(sz
* 512));
1875 void convert_to_4k_imsm_migr_rec(struct intel_super
*super
)
1877 struct migr_record
*migr_rec
= super
->migr_rec
;
1879 migr_rec
->blocks_per_unit
/= IMSM_4K_DIV
;
1880 migr_rec
->dest_depth_per_unit
/= IMSM_4K_DIV
;
1881 split_ull((join_u32(migr_rec
->post_migr_vol_cap
,
1882 migr_rec
->post_migr_vol_cap_hi
) / IMSM_4K_DIV
),
1883 &migr_rec
->post_migr_vol_cap
, &migr_rec
->post_migr_vol_cap_hi
);
1884 set_migr_chkp_area_pba(migr_rec
,
1885 migr_chkp_area_pba(migr_rec
) / IMSM_4K_DIV
);
1886 set_migr_dest_1st_member_lba(migr_rec
,
1887 migr_dest_1st_member_lba(migr_rec
) / IMSM_4K_DIV
);
1890 void convert_to_4k_imsm_disk(struct imsm_disk
*disk
)
1892 set_total_blocks(disk
, (total_blocks(disk
)/IMSM_4K_DIV
));
1895 void convert_to_4k(struct intel_super
*super
)
1897 struct imsm_super
*mpb
= super
->anchor
;
1898 struct imsm_disk
*disk
;
1900 __u32 bbm_log_size
= __le32_to_cpu(mpb
->bbm_log_size
);
1902 for (i
= 0; i
< mpb
->num_disks
; i
++) {
1903 disk
= __get_imsm_disk(mpb
, i
);
1905 convert_to_4k_imsm_disk(disk
);
1907 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
1908 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
1909 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
1911 set_imsm_dev_size(dev
, imsm_dev_size(dev
)/IMSM_4K_DIV
);
1912 set_vol_curr_migr_unit(dev
,
1913 vol_curr_migr_unit(dev
) / IMSM_4K_DIV
);
1916 set_blocks_per_member(map
, blocks_per_member(map
)/IMSM_4K_DIV
);
1917 map
->blocks_per_strip
/= IMSM_4K_DIV
;
1918 set_pba_of_lba0(map
, pba_of_lba0(map
)/IMSM_4K_DIV
);
1920 if (dev
->vol
.migr_state
) {
1922 map
= get_imsm_map(dev
, MAP_1
);
1923 set_blocks_per_member(map
,
1924 blocks_per_member(map
)/IMSM_4K_DIV
);
1925 map
->blocks_per_strip
/= IMSM_4K_DIV
;
1926 set_pba_of_lba0(map
, pba_of_lba0(map
)/IMSM_4K_DIV
);
1930 struct bbm_log
*log
= (void *)mpb
+
1931 __le32_to_cpu(mpb
->mpb_size
) - bbm_log_size
;
1934 for (i
= 0; i
< log
->entry_count
; i
++) {
1935 struct bbm_log_entry
*entry
=
1936 &log
->marked_block_entries
[i
];
1938 __u8 count
= entry
->marked_count
+ 1;
1939 unsigned long long sector
=
1940 __le48_to_cpu(&entry
->defective_block_start
);
1942 entry
->defective_block_start
=
1943 __cpu_to_le48(sector
/IMSM_4K_DIV
);
1944 entry
->marked_count
= max(count
/IMSM_4K_DIV
, 1) - 1;
1948 mpb
->check_sum
= __gen_imsm_checksum(mpb
);
1951 void examine_migr_rec_imsm(struct intel_super
*super
)
1953 struct migr_record
*migr_rec
= super
->migr_rec
;
1954 struct imsm_super
*mpb
= super
->anchor
;
1957 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
1958 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
1959 struct imsm_map
*map
;
1962 if (is_gen_migration(dev
) == false)
1965 printf("\nMigration Record Information:");
1967 /* first map under migration */
1968 map
= get_imsm_map(dev
, MAP_0
);
1971 slot
= get_imsm_disk_slot(map
, super
->disks
->index
);
1972 if (map
== NULL
|| slot
> 1 || slot
< 0) {
1973 printf(" Empty\n ");
1974 printf("Examine one of first two disks in array\n");
1977 printf("\n Status : ");
1978 if (__le32_to_cpu(migr_rec
->rec_status
) == UNIT_SRC_NORMAL
)
1981 printf("Contains Data\n");
1982 printf(" Current Unit : %llu\n",
1983 current_migr_unit(migr_rec
));
1984 printf(" Family : %u\n",
1985 __le32_to_cpu(migr_rec
->family_num
));
1986 printf(" Ascending : %u\n",
1987 __le32_to_cpu(migr_rec
->ascending_migr
));
1988 printf(" Blocks Per Unit : %u\n",
1989 __le32_to_cpu(migr_rec
->blocks_per_unit
));
1990 printf(" Dest. Depth Per Unit : %u\n",
1991 __le32_to_cpu(migr_rec
->dest_depth_per_unit
));
1992 printf(" Checkpoint Area pba : %llu\n",
1993 migr_chkp_area_pba(migr_rec
));
1994 printf(" First member lba : %llu\n",
1995 migr_dest_1st_member_lba(migr_rec
));
1996 printf(" Total Number of Units : %llu\n",
1997 get_num_migr_units(migr_rec
));
1998 printf(" Size of volume : %llu\n",
1999 join_u32(migr_rec
->post_migr_vol_cap
,
2000 migr_rec
->post_migr_vol_cap_hi
));
2001 printf(" Record was read from : %u\n",
2002 __le32_to_cpu(migr_rec
->ckpt_read_disk_num
));
2008 void convert_from_4k_imsm_migr_rec(struct intel_super
*super
)
2010 struct migr_record
*migr_rec
= super
->migr_rec
;
2012 migr_rec
->blocks_per_unit
*= IMSM_4K_DIV
;
2013 migr_rec
->dest_depth_per_unit
*= IMSM_4K_DIV
;
2014 split_ull((join_u32(migr_rec
->post_migr_vol_cap
,
2015 migr_rec
->post_migr_vol_cap_hi
) * IMSM_4K_DIV
),
2016 &migr_rec
->post_migr_vol_cap
,
2017 &migr_rec
->post_migr_vol_cap_hi
);
2018 set_migr_chkp_area_pba(migr_rec
,
2019 migr_chkp_area_pba(migr_rec
) * IMSM_4K_DIV
);
2020 set_migr_dest_1st_member_lba(migr_rec
,
2021 migr_dest_1st_member_lba(migr_rec
) * IMSM_4K_DIV
);
2024 void convert_from_4k(struct intel_super
*super
)
2026 struct imsm_super
*mpb
= super
->anchor
;
2027 struct imsm_disk
*disk
;
2029 __u32 bbm_log_size
= __le32_to_cpu(mpb
->bbm_log_size
);
2031 for (i
= 0; i
< mpb
->num_disks
; i
++) {
2032 disk
= __get_imsm_disk(mpb
, i
);
2034 set_total_blocks(disk
, (total_blocks(disk
)*IMSM_4K_DIV
));
2037 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
2038 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
2039 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
2041 set_imsm_dev_size(dev
, imsm_dev_size(dev
)*IMSM_4K_DIV
);
2042 set_vol_curr_migr_unit(dev
,
2043 vol_curr_migr_unit(dev
) * IMSM_4K_DIV
);
2046 set_blocks_per_member(map
, blocks_per_member(map
)*IMSM_4K_DIV
);
2047 map
->blocks_per_strip
*= IMSM_4K_DIV
;
2048 set_pba_of_lba0(map
, pba_of_lba0(map
)*IMSM_4K_DIV
);
2050 if (dev
->vol
.migr_state
) {
2052 map
= get_imsm_map(dev
, MAP_1
);
2053 set_blocks_per_member(map
,
2054 blocks_per_member(map
)*IMSM_4K_DIV
);
2055 map
->blocks_per_strip
*= IMSM_4K_DIV
;
2056 set_pba_of_lba0(map
, pba_of_lba0(map
)*IMSM_4K_DIV
);
2060 struct bbm_log
*log
= (void *)mpb
+
2061 __le32_to_cpu(mpb
->mpb_size
) - bbm_log_size
;
2064 for (i
= 0; i
< log
->entry_count
; i
++) {
2065 struct bbm_log_entry
*entry
=
2066 &log
->marked_block_entries
[i
];
2068 __u8 count
= entry
->marked_count
+ 1;
2069 unsigned long long sector
=
2070 __le48_to_cpu(&entry
->defective_block_start
);
2072 entry
->defective_block_start
=
2073 __cpu_to_le48(sector
*IMSM_4K_DIV
);
2074 entry
->marked_count
= count
*IMSM_4K_DIV
- 1;
2078 mpb
->check_sum
= __gen_imsm_checksum(mpb
);
2081 /*******************************************************************************
2082 * function: imsm_check_attributes
2083 * Description: Function checks if features represented by attributes flags
2084 * are supported by mdadm.
2086 * attributes - Attributes read from metadata
2088 * 0 - passed attributes contains unsupported features flags
2089 * 1 - all features are supported
2090 ******************************************************************************/
2091 static int imsm_check_attributes(__u32 attributes
)
2094 __u32 not_supported
= MPB_ATTRIB_SUPPORTED
^0xffffffff;
2096 not_supported
&= ~MPB_ATTRIB_IGNORED
;
2098 not_supported
&= attributes
;
2099 if (not_supported
) {
2100 pr_err("(IMSM): Unsupported attributes : %x\n",
2101 (unsigned)__le32_to_cpu(not_supported
));
2102 if (not_supported
& MPB_ATTRIB_CHECKSUM_VERIFY
) {
2103 dprintf("\t\tMPB_ATTRIB_CHECKSUM_VERIFY \n");
2104 not_supported
^= MPB_ATTRIB_CHECKSUM_VERIFY
;
2106 if (not_supported
& MPB_ATTRIB_2TB
) {
2107 dprintf("\t\tMPB_ATTRIB_2TB\n");
2108 not_supported
^= MPB_ATTRIB_2TB
;
2110 if (not_supported
& MPB_ATTRIB_RAID0
) {
2111 dprintf("\t\tMPB_ATTRIB_RAID0\n");
2112 not_supported
^= MPB_ATTRIB_RAID0
;
2114 if (not_supported
& MPB_ATTRIB_RAID1
) {
2115 dprintf("\t\tMPB_ATTRIB_RAID1\n");
2116 not_supported
^= MPB_ATTRIB_RAID1
;
2118 if (not_supported
& MPB_ATTRIB_RAID10
) {
2119 dprintf("\t\tMPB_ATTRIB_RAID10\n");
2120 not_supported
^= MPB_ATTRIB_RAID10
;
2122 if (not_supported
& MPB_ATTRIB_RAID1E
) {
2123 dprintf("\t\tMPB_ATTRIB_RAID1E\n");
2124 not_supported
^= MPB_ATTRIB_RAID1E
;
2126 if (not_supported
& MPB_ATTRIB_RAID5
) {
2127 dprintf("\t\tMPB_ATTRIB_RAID5\n");
2128 not_supported
^= MPB_ATTRIB_RAID5
;
2130 if (not_supported
& MPB_ATTRIB_RAIDCNG
) {
2131 dprintf("\t\tMPB_ATTRIB_RAIDCNG\n");
2132 not_supported
^= MPB_ATTRIB_RAIDCNG
;
2134 if (not_supported
& MPB_ATTRIB_BBM
) {
2135 dprintf("\t\tMPB_ATTRIB_BBM\n");
2136 not_supported
^= MPB_ATTRIB_BBM
;
2138 if (not_supported
& MPB_ATTRIB_CHECKSUM_VERIFY
) {
2139 dprintf("\t\tMPB_ATTRIB_CHECKSUM_VERIFY (== MPB_ATTRIB_LEGACY)\n");
2140 not_supported
^= MPB_ATTRIB_CHECKSUM_VERIFY
;
2142 if (not_supported
& MPB_ATTRIB_EXP_STRIPE_SIZE
) {
2143 dprintf("\t\tMPB_ATTRIB_EXP_STRIP_SIZE\n");
2144 not_supported
^= MPB_ATTRIB_EXP_STRIPE_SIZE
;
2146 if (not_supported
& MPB_ATTRIB_2TB_DISK
) {
2147 dprintf("\t\tMPB_ATTRIB_2TB_DISK\n");
2148 not_supported
^= MPB_ATTRIB_2TB_DISK
;
2150 if (not_supported
& MPB_ATTRIB_NEVER_USE2
) {
2151 dprintf("\t\tMPB_ATTRIB_NEVER_USE2\n");
2152 not_supported
^= MPB_ATTRIB_NEVER_USE2
;
2154 if (not_supported
& MPB_ATTRIB_NEVER_USE
) {
2155 dprintf("\t\tMPB_ATTRIB_NEVER_USE\n");
2156 not_supported
^= MPB_ATTRIB_NEVER_USE
;
2160 dprintf("(IMSM): Unknown attributes : %x\n", not_supported
);
2168 static void getinfo_super_imsm(struct supertype
*st
, struct mdinfo
*info
, char *map
);
2170 static void examine_super_imsm(struct supertype
*st
, char *homehost
)
2172 struct intel_super
*super
= st
->sb
;
2173 struct imsm_super
*mpb
= super
->anchor
;
2174 char str
[MAX_SIGNATURE_LENGTH
];
2179 __u32 reserved
= imsm_reserved_sectors(super
, super
->disks
);
2181 time_t creation_time
;
2183 strncpy(str
, (char *)mpb
->sig
, MPB_SIG_LEN
);
2184 str
[MPB_SIG_LEN
-1] = '\0';
2185 printf(" Magic : %s\n", str
);
2186 printf(" Version : %s\n", get_imsm_version(mpb
));
2187 printf(" Orig Family : %08x\n", __le32_to_cpu(mpb
->orig_family_num
));
2188 printf(" Family : %08x\n", __le32_to_cpu(mpb
->family_num
));
2189 printf(" Generation : %08x\n", __le32_to_cpu(mpb
->generation_num
));
2190 creation_time
= __le64_to_cpu(mpb
->creation_time
);
2191 printf(" Creation Time : %.24s\n",
2192 creation_time
? ctime(&creation_time
) : "Unknown");
2193 printf(" Attributes : ");
2194 if (imsm_check_attributes(mpb
->attributes
))
2195 printf("All supported\n");
2197 printf("not supported\n");
2198 getinfo_super_imsm(st
, &info
, NULL
);
2199 fname_from_uuid(st
, &info
, nbuf
, ':');
2200 printf(" UUID : %s\n", nbuf
+ 5);
2201 sum
= __le32_to_cpu(mpb
->check_sum
);
2202 printf(" Checksum : %08x %s\n", sum
,
2203 __gen_imsm_checksum(mpb
) == sum
? "correct" : "incorrect");
2204 printf(" MPB Sectors : %d\n", mpb_sectors(mpb
, super
->sector_size
));
2205 printf(" Disks : %d\n", mpb
->num_disks
);
2206 printf(" RAID Devices : %d\n", mpb
->num_raid_devs
);
2207 print_imsm_disk(__get_imsm_disk(mpb
, super
->disks
->index
),
2208 super
->disks
->index
, reserved
, super
->sector_size
);
2209 if (get_imsm_bbm_log_size(super
->bbm_log
)) {
2210 struct bbm_log
*log
= super
->bbm_log
;
2213 printf("Bad Block Management Log:\n");
2214 printf(" Log Size : %d\n", __le32_to_cpu(mpb
->bbm_log_size
));
2215 printf(" Signature : %x\n", __le32_to_cpu(log
->signature
));
2216 printf(" Entry Count : %d\n", __le32_to_cpu(log
->entry_count
));
2218 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
2220 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
2222 super
->current_vol
= i
;
2223 getinfo_super_imsm(st
, &info
, NULL
);
2224 fname_from_uuid(st
, &info
, nbuf
, ':');
2225 print_imsm_dev(super
, dev
, nbuf
+ 5, super
->disks
->index
);
2227 for (i
= 0; i
< mpb
->num_disks
; i
++) {
2228 if (i
== super
->disks
->index
)
2230 print_imsm_disk(__get_imsm_disk(mpb
, i
), i
, reserved
,
2231 super
->sector_size
);
2234 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
2235 if (dl
->index
== -1)
2236 print_imsm_disk(&dl
->disk
, -1, reserved
,
2237 super
->sector_size
);
2239 examine_migr_rec_imsm(super
);
2242 static void brief_examine_super_imsm(struct supertype
*st
, int verbose
)
2244 /* We just write a generic IMSM ARRAY entry */
2248 getinfo_super_imsm(st
, &info
, NULL
);
2249 fname_from_uuid(st
, &info
, nbuf
, ':');
2250 printf("ARRAY metadata=imsm UUID=%s\n", nbuf
+ 5);
2253 static void brief_examine_subarrays_imsm(struct supertype
*st
, int verbose
)
2255 /* We just write a generic IMSM ARRAY entry */
2259 struct intel_super
*super
= st
->sb
;
2262 if (!super
->anchor
->num_raid_devs
)
2265 getinfo_super_imsm(st
, &info
, NULL
);
2266 fname_from_uuid(st
, &info
, nbuf
, ':');
2267 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
2268 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
2270 super
->current_vol
= i
;
2271 getinfo_super_imsm(st
, &info
, NULL
);
2272 fname_from_uuid(st
, &info
, nbuf1
, ':');
2273 printf("ARRAY /dev/md/%.16s container=%s member=%d UUID=%s\n",
2274 dev
->volume
, nbuf
+ 5, i
, nbuf1
+ 5);
2278 static void export_examine_super_imsm(struct supertype
*st
)
2280 struct intel_super
*super
= st
->sb
;
2281 struct imsm_super
*mpb
= super
->anchor
;
2285 getinfo_super_imsm(st
, &info
, NULL
);
2286 fname_from_uuid(st
, &info
, nbuf
, ':');
2287 printf("MD_METADATA=imsm\n");
2288 printf("MD_LEVEL=container\n");
2289 printf("MD_UUID=%s\n", nbuf
+5);
2290 printf("MD_DEVICES=%u\n", mpb
->num_disks
);
2291 printf("MD_CREATION_TIME=%llu\n", __le64_to_cpu(mpb
->creation_time
));
2294 static void detail_super_imsm(struct supertype
*st
, char *homehost
,
2299 struct intel_super
*super
= st
->sb
;
2300 int temp_vol
= super
->current_vol
;
2303 super
->current_vol
= strtoul(subarray
, NULL
, 10);
2305 getinfo_super_imsm(st
, &info
, NULL
);
2306 fname_from_uuid(st
, &info
, nbuf
, ':');
2307 printf("\n UUID : %s\n", nbuf
+ 5);
2309 super
->current_vol
= temp_vol
;
2312 static void brief_detail_super_imsm(struct supertype
*st
, char *subarray
)
2316 struct intel_super
*super
= st
->sb
;
2317 int temp_vol
= super
->current_vol
;
2320 super
->current_vol
= strtoul(subarray
, NULL
, 10);
2322 getinfo_super_imsm(st
, &info
, NULL
);
2323 fname_from_uuid(st
, &info
, nbuf
, ':');
2324 printf(" UUID=%s", nbuf
+ 5);
2326 super
->current_vol
= temp_vol
;
2329 static int imsm_read_serial(int fd
, char *devname
, __u8
*serial
,
2330 size_t serial_buf_len
);
2331 static void fd2devname(int fd
, char *name
);
2333 static int ahci_enumerate_ports(const char *hba_path
, int port_count
, int host_base
, int verbose
)
2335 /* dump an unsorted list of devices attached to AHCI Intel storage
2336 * controller, as well as non-connected ports
2338 int hba_len
= strlen(hba_path
) + 1;
2343 unsigned long port_mask
= (1 << port_count
) - 1;
2345 if (port_count
> (int)sizeof(port_mask
) * 8) {
2347 pr_err("port_count %d out of range\n", port_count
);
2351 /* scroll through /sys/dev/block looking for devices attached to
2354 dir
= opendir("/sys/dev/block");
2358 for (ent
= readdir(dir
); ent
; ent
= readdir(dir
)) {
2364 char device
[PATH_MAX
];
2369 if (sscanf(ent
->d_name
, "%d:%d", &major
, &minor
) != 2)
2371 path
= devt_to_devpath(makedev(major
, minor
), 1, NULL
);
2374 if (!path_attached_to_hba(path
, hba_path
)) {
2380 /* retrieve the scsi device */
2381 if (!devt_to_devpath(makedev(major
, minor
), 1, device
)) {
2383 pr_err("failed to get device\n");
2387 if (devpath_to_char(device
, "type", buf
, sizeof(buf
), 0)) {
2391 type
= strtoul(buf
, NULL
, 10);
2393 /* if it's not a disk print the vendor and model */
2394 if (!(type
== 0 || type
== 7 || type
== 14)) {
2398 if (devpath_to_char(device
, "vendor", buf
,
2399 sizeof(buf
), 0) == 0) {
2400 strncpy(vendor
, buf
, sizeof(vendor
));
2401 vendor
[sizeof(vendor
) - 1] = '\0';
2402 c
= (char *) &vendor
[sizeof(vendor
) - 1];
2403 while (isspace(*c
) || *c
== '\0')
2408 if (devpath_to_char(device
, "model", buf
,
2409 sizeof(buf
), 0) == 0) {
2410 strncpy(model
, buf
, sizeof(model
));
2411 model
[sizeof(model
) - 1] = '\0';
2412 c
= (char *) &model
[sizeof(model
) - 1];
2413 while (isspace(*c
) || *c
== '\0')
2417 if (vendor
[0] && model
[0])
2418 sprintf(buf
, "%.64s %.64s", vendor
, model
);
2420 switch (type
) { /* numbers from hald/linux/device.c */
2421 case 1: sprintf(buf
, "tape"); break;
2422 case 2: sprintf(buf
, "printer"); break;
2423 case 3: sprintf(buf
, "processor"); break;
2425 case 5: sprintf(buf
, "cdrom"); break;
2426 case 6: sprintf(buf
, "scanner"); break;
2427 case 8: sprintf(buf
, "media_changer"); break;
2428 case 9: sprintf(buf
, "comm"); break;
2429 case 12: sprintf(buf
, "raid"); break;
2430 default: sprintf(buf
, "unknown");
2435 /* chop device path to 'host%d' and calculate the port number */
2436 c
= strchr(&path
[hba_len
], '/');
2439 pr_err("%s - invalid path name\n", path
+ hba_len
);
2444 if ((sscanf(&path
[hba_len
], "ata%d", &port
) == 1) ||
2445 ((sscanf(&path
[hba_len
], "host%d", &port
) == 1)))
2449 *c
= '/'; /* repair the full string */
2450 pr_err("failed to determine port number for %s\n",
2457 /* mark this port as used */
2458 port_mask
&= ~(1 << port
);
2460 /* print out the device information */
2462 printf(" Port%d : - non-disk device (%s) -\n", port
, buf
);
2466 fd
= dev_open(ent
->d_name
, O_RDONLY
);
2467 if (!is_fd_valid(fd
))
2468 printf(" Port%d : - disk info unavailable -\n", port
);
2470 fd2devname(fd
, buf
);
2471 printf(" Port%d : %s", port
, buf
);
2472 if (imsm_read_serial(fd
, NULL
, (__u8
*)buf
,
2474 printf(" (%s)\n", buf
);
2489 for (i
= 0; i
< port_count
; i
++)
2490 if (port_mask
& (1 << i
))
2491 printf(" Port%d : - no device attached -\n", i
);
2497 static int print_nvme_info(struct sys_dev
*hba
)
2502 dir
= opendir("/sys/block/");
2506 for (ent
= readdir(dir
); ent
; ent
= readdir(dir
)) {
2507 char ns_path
[PATH_MAX
];
2508 char cntrl_path
[PATH_MAX
];
2512 if (!strstr(ent
->d_name
, "nvme"))
2515 fd
= open_dev(ent
->d_name
);
2516 if (!is_fd_valid(fd
))
2519 if (!diskfd_to_devpath(fd
, 0, ns_path
) ||
2520 !diskfd_to_devpath(fd
, 1, cntrl_path
))
2523 if (!path_attached_to_hba(cntrl_path
, hba
->path
))
2526 if (!imsm_is_nvme_namespace_supported(fd
, 0))
2529 fd2devname(fd
, buf
);
2530 if (hba
->type
== SYS_DEV_VMD
)
2531 printf(" NVMe under VMD : %s", buf
);
2532 else if (hba
->type
== SYS_DEV_NVME
)
2533 printf(" NVMe Device : %s", buf
);
2535 if (!imsm_read_serial(fd
, NULL
, (__u8
*)buf
,
2537 printf(" (%s)\n", buf
);
2549 static void print_found_intel_controllers(struct sys_dev
*elem
)
2551 for (; elem
; elem
= elem
->next
) {
2552 pr_err("found Intel(R) ");
2553 if (elem
->type
== SYS_DEV_SATA
)
2554 fprintf(stderr
, "SATA ");
2555 else if (elem
->type
== SYS_DEV_SAS
)
2556 fprintf(stderr
, "SAS ");
2557 else if (elem
->type
== SYS_DEV_NVME
)
2558 fprintf(stderr
, "NVMe ");
2560 if (elem
->type
== SYS_DEV_VMD
)
2561 fprintf(stderr
, "VMD domain");
2563 fprintf(stderr
, "RAID controller");
2566 fprintf(stderr
, " at %s", elem
->pci_id
);
2567 fprintf(stderr
, ".\n");
2572 static int ahci_get_port_count(const char *hba_path
, int *port_count
)
2579 if ((dir
= opendir(hba_path
)) == NULL
)
2582 for (ent
= readdir(dir
); ent
; ent
= readdir(dir
)) {
2585 if ((sscanf(ent
->d_name
, "ata%d", &host
) != 1) &&
2586 ((sscanf(ent
->d_name
, "host%d", &host
) != 1)))
2588 if (*port_count
== 0)
2590 else if (host
< host_base
)
2593 if (host
+ 1 > *port_count
+ host_base
)
2594 *port_count
= host
+ 1 - host_base
;
2600 static void print_imsm_capability(const struct imsm_orom
*orom
)
2602 printf(" Platform : Intel(R) ");
2603 if (orom
->capabilities
== 0 && orom
->driver_features
== 0)
2604 printf("Matrix Storage Manager\n");
2605 else if (imsm_orom_is_enterprise(orom
) && orom
->major_ver
>= 6)
2606 printf("Virtual RAID on CPU\n");
2608 printf("Rapid Storage Technology%s\n",
2609 imsm_orom_is_enterprise(orom
) ? " enterprise" : "");
2610 if (orom
->major_ver
|| orom
->minor_ver
|| orom
->hotfix_ver
|| orom
->build
)
2611 printf(" Version : %d.%d.%d.%d\n", orom
->major_ver
,
2612 orom
->minor_ver
, orom
->hotfix_ver
, orom
->build
);
2613 printf(" RAID Levels :%s%s%s%s%s\n",
2614 imsm_orom_has_raid0(orom
) ? " raid0" : "",
2615 imsm_orom_has_raid1(orom
) ? " raid1" : "",
2616 imsm_orom_has_raid1e(orom
) ? " raid1e" : "",
2617 imsm_orom_has_raid10(orom
) ? " raid10" : "",
2618 imsm_orom_has_raid5(orom
) ? " raid5" : "");
2619 printf(" Chunk Sizes :%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
2620 imsm_orom_has_chunk(orom
, 2) ? " 2k" : "",
2621 imsm_orom_has_chunk(orom
, 4) ? " 4k" : "",
2622 imsm_orom_has_chunk(orom
, 8) ? " 8k" : "",
2623 imsm_orom_has_chunk(orom
, 16) ? " 16k" : "",
2624 imsm_orom_has_chunk(orom
, 32) ? " 32k" : "",
2625 imsm_orom_has_chunk(orom
, 64) ? " 64k" : "",
2626 imsm_orom_has_chunk(orom
, 128) ? " 128k" : "",
2627 imsm_orom_has_chunk(orom
, 256) ? " 256k" : "",
2628 imsm_orom_has_chunk(orom
, 512) ? " 512k" : "",
2629 imsm_orom_has_chunk(orom
, 1024*1) ? " 1M" : "",
2630 imsm_orom_has_chunk(orom
, 1024*2) ? " 2M" : "",
2631 imsm_orom_has_chunk(orom
, 1024*4) ? " 4M" : "",
2632 imsm_orom_has_chunk(orom
, 1024*8) ? " 8M" : "",
2633 imsm_orom_has_chunk(orom
, 1024*16) ? " 16M" : "",
2634 imsm_orom_has_chunk(orom
, 1024*32) ? " 32M" : "",
2635 imsm_orom_has_chunk(orom
, 1024*64) ? " 64M" : "");
2636 printf(" 2TB volumes :%s supported\n",
2637 (orom
->attr
& IMSM_OROM_ATTR_2TB
)?"":" not");
2638 printf(" 2TB disks :%s supported\n",
2639 (orom
->attr
& IMSM_OROM_ATTR_2TB_DISK
)?"":" not");
2640 printf(" Max Disks : %d\n", orom
->tds
);
2641 printf(" Max Volumes : %d per array, %d per %s\n",
2642 orom
->vpa
, orom
->vphba
,
2643 imsm_orom_is_nvme(orom
) ? "platform" : "controller");
2647 static void print_imsm_capability_export(const struct imsm_orom
*orom
)
2649 printf("MD_FIRMWARE_TYPE=imsm\n");
2650 if (orom
->major_ver
|| orom
->minor_ver
|| orom
->hotfix_ver
|| orom
->build
)
2651 printf("IMSM_VERSION=%d.%d.%d.%d\n", orom
->major_ver
, orom
->minor_ver
,
2652 orom
->hotfix_ver
, orom
->build
);
2653 printf("IMSM_SUPPORTED_RAID_LEVELS=%s%s%s%s%s\n",
2654 imsm_orom_has_raid0(orom
) ? "raid0 " : "",
2655 imsm_orom_has_raid1(orom
) ? "raid1 " : "",
2656 imsm_orom_has_raid1e(orom
) ? "raid1e " : "",
2657 imsm_orom_has_raid5(orom
) ? "raid10 " : "",
2658 imsm_orom_has_raid10(orom
) ? "raid5 " : "");
2659 printf("IMSM_SUPPORTED_CHUNK_SIZES=%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
2660 imsm_orom_has_chunk(orom
, 2) ? "2k " : "",
2661 imsm_orom_has_chunk(orom
, 4) ? "4k " : "",
2662 imsm_orom_has_chunk(orom
, 8) ? "8k " : "",
2663 imsm_orom_has_chunk(orom
, 16) ? "16k " : "",
2664 imsm_orom_has_chunk(orom
, 32) ? "32k " : "",
2665 imsm_orom_has_chunk(orom
, 64) ? "64k " : "",
2666 imsm_orom_has_chunk(orom
, 128) ? "128k " : "",
2667 imsm_orom_has_chunk(orom
, 256) ? "256k " : "",
2668 imsm_orom_has_chunk(orom
, 512) ? "512k " : "",
2669 imsm_orom_has_chunk(orom
, 1024*1) ? "1M " : "",
2670 imsm_orom_has_chunk(orom
, 1024*2) ? "2M " : "",
2671 imsm_orom_has_chunk(orom
, 1024*4) ? "4M " : "",
2672 imsm_orom_has_chunk(orom
, 1024*8) ? "8M " : "",
2673 imsm_orom_has_chunk(orom
, 1024*16) ? "16M " : "",
2674 imsm_orom_has_chunk(orom
, 1024*32) ? "32M " : "",
2675 imsm_orom_has_chunk(orom
, 1024*64) ? "64M " : "");
2676 printf("IMSM_2TB_VOLUMES=%s\n",(orom
->attr
& IMSM_OROM_ATTR_2TB
) ? "yes" : "no");
2677 printf("IMSM_2TB_DISKS=%s\n",(orom
->attr
& IMSM_OROM_ATTR_2TB_DISK
) ? "yes" : "no");
2678 printf("IMSM_MAX_DISKS=%d\n",orom
->tds
);
2679 printf("IMSM_MAX_VOLUMES_PER_ARRAY=%d\n",orom
->vpa
);
2680 printf("IMSM_MAX_VOLUMES_PER_CONTROLLER=%d\n",orom
->vphba
);
2683 static int detail_platform_imsm(int verbose
, int enumerate_only
, char *controller_path
)
2685 /* There are two components to imsm platform support, the ahci SATA
2686 * controller and the option-rom. To find the SATA controller we
2687 * simply look in /sys/bus/pci/drivers/ahci to see if an ahci
2688 * controller with the Intel vendor id is present. This approach
2689 * allows mdadm to leverage the kernel's ahci detection logic, with the
2690 * caveat that if ahci.ko is not loaded mdadm will not be able to
2691 * detect platform raid capabilities. The option-rom resides in a
2692 * platform "Adapter ROM". We scan for its signature to retrieve the
2693 * platform capabilities. If raid support is disabled in the BIOS the
2694 * option-rom capability structure will not be available.
2696 struct sys_dev
*list
, *hba
;
2701 if (enumerate_only
) {
2702 if (check_env("IMSM_NO_PLATFORM"))
2704 list
= find_intel_devices();
2707 for (hba
= list
; hba
; hba
= hba
->next
) {
2708 if (find_imsm_capability(hba
)) {
2718 list
= find_intel_devices();
2721 pr_err("no active Intel(R) RAID controller found.\n");
2723 } else if (verbose
> 0)
2724 print_found_intel_controllers(list
);
2726 for (hba
= list
; hba
; hba
= hba
->next
) {
2727 if (controller_path
&& (compare_paths(hba
->path
, controller_path
) != 0))
2729 if (!find_imsm_capability(hba
)) {
2731 pr_err("imsm capabilities not found for controller: %s (type %s)\n",
2732 hba
->type
== SYS_DEV_VMD
? vmd_domain_to_controller(hba
, buf
) : hba
->path
,
2733 get_sys_dev_type(hba
->type
));
2739 if (controller_path
&& result
== 1) {
2740 pr_err("no active Intel(R) RAID controller found under %s\n",
2745 const struct orom_entry
*entry
;
2747 for (entry
= orom_entries
; entry
; entry
= entry
->next
) {
2748 if (entry
->type
== SYS_DEV_VMD
) {
2749 print_imsm_capability(&entry
->orom
);
2750 printf(" 3rd party NVMe :%s supported\n",
2751 imsm_orom_has_tpv_support(&entry
->orom
)?"":" not");
2752 for (hba
= list
; hba
; hba
= hba
->next
) {
2753 if (hba
->type
== SYS_DEV_VMD
) {
2755 printf(" I/O Controller : %s (%s)\n",
2756 vmd_domain_to_controller(hba
, buf
), get_sys_dev_type(hba
->type
));
2757 if (print_nvme_info(hba
)) {
2759 pr_err("failed to get devices attached to VMD domain.\n");
2768 print_imsm_capability(&entry
->orom
);
2769 if (entry
->type
== SYS_DEV_NVME
) {
2770 for (hba
= list
; hba
; hba
= hba
->next
) {
2771 if (hba
->type
== SYS_DEV_NVME
)
2772 print_nvme_info(hba
);
2778 struct devid_list
*devid
;
2779 for (devid
= entry
->devid_list
; devid
; devid
= devid
->next
) {
2780 hba
= device_by_id(devid
->devid
);
2784 printf(" I/O Controller : %s (%s)\n",
2785 hba
->path
, get_sys_dev_type(hba
->type
));
2786 if (hba
->type
== SYS_DEV_SATA
) {
2787 host_base
= ahci_get_port_count(hba
->path
, &port_count
);
2788 if (ahci_enumerate_ports(hba
->path
, port_count
, host_base
, verbose
)) {
2790 pr_err("failed to enumerate ports on SATA controller at %s.\n", hba
->pci_id
);
2801 static int export_detail_platform_imsm(int verbose
, char *controller_path
)
2803 struct sys_dev
*list
, *hba
;
2806 list
= find_intel_devices();
2809 pr_err("IMSM_DETAIL_PLATFORM_ERROR=NO_INTEL_DEVICES\n");
2814 for (hba
= list
; hba
; hba
= hba
->next
) {
2815 if (controller_path
&& (compare_paths(hba
->path
,controller_path
) != 0))
2817 if (!find_imsm_capability(hba
) && verbose
> 0) {
2819 pr_err("IMSM_DETAIL_PLATFORM_ERROR=NO_IMSM_CAPABLE_DEVICE_UNDER_%s\n",
2820 hba
->type
== SYS_DEV_VMD
? vmd_domain_to_controller(hba
, buf
) : hba
->path
);
2826 const struct orom_entry
*entry
;
2828 for (entry
= orom_entries
; entry
; entry
= entry
->next
) {
2829 if (entry
->type
== SYS_DEV_VMD
) {
2830 for (hba
= list
; hba
; hba
= hba
->next
)
2831 print_imsm_capability_export(&entry
->orom
);
2834 print_imsm_capability_export(&entry
->orom
);
2840 static int match_home_imsm(struct supertype
*st
, char *homehost
)
2842 /* the imsm metadata format does not specify any host
2843 * identification information. We return -1 since we can never
2844 * confirm nor deny whether a given array is "meant" for this
2845 * host. We rely on compare_super and the 'family_num' fields to
2846 * exclude member disks that do not belong, and we rely on
2847 * mdadm.conf to specify the arrays that should be assembled.
2848 * Auto-assembly may still pick up "foreign" arrays.
2854 static void uuid_from_super_imsm(struct supertype
*st
, int uuid
[4])
2856 /* The uuid returned here is used for:
2857 * uuid to put into bitmap file (Create, Grow)
2858 * uuid for backup header when saving critical section (Grow)
2859 * comparing uuids when re-adding a device into an array
2860 * In these cases the uuid required is that of the data-array,
2861 * not the device-set.
2862 * uuid to recognise same set when adding a missing device back
2863 * to an array. This is a uuid for the device-set.
2865 * For each of these we can make do with a truncated
2866 * or hashed uuid rather than the original, as long as
2868 * In each case the uuid required is that of the data-array,
2869 * not the device-set.
2871 /* imsm does not track uuid's so we synthesis one using sha1 on
2872 * - The signature (Which is constant for all imsm array, but no matter)
2873 * - the orig_family_num of the container
2874 * - the index number of the volume
2875 * - the 'serial' number of the volume.
2876 * Hopefully these are all constant.
2878 struct intel_super
*super
= st
->sb
;
2881 struct sha1_ctx ctx
;
2882 struct imsm_dev
*dev
= NULL
;
2885 /* some mdadm versions failed to set ->orig_family_num, in which
2886 * case fall back to ->family_num. orig_family_num will be
2887 * fixed up with the first metadata update.
2889 family_num
= super
->anchor
->orig_family_num
;
2890 if (family_num
== 0)
2891 family_num
= super
->anchor
->family_num
;
2892 sha1_init_ctx(&ctx
);
2893 sha1_process_bytes(super
->anchor
->sig
, MPB_SIG_LEN
, &ctx
);
2894 sha1_process_bytes(&family_num
, sizeof(__u32
), &ctx
);
2895 if (super
->current_vol
>= 0)
2896 dev
= get_imsm_dev(super
, super
->current_vol
);
2898 __u32 vol
= super
->current_vol
;
2899 sha1_process_bytes(&vol
, sizeof(vol
), &ctx
);
2900 sha1_process_bytes(dev
->volume
, MAX_RAID_SERIAL_LEN
, &ctx
);
2902 sha1_finish_ctx(&ctx
, buf
);
2903 memcpy(uuid
, buf
, 4*4);
2908 get_imsm_numerical_version(struct imsm_super
*mpb
, int *m
, int *p
)
2910 __u8
*v
= get_imsm_version(mpb
);
2911 __u8
*end
= mpb
->sig
+ MAX_SIGNATURE_LENGTH
;
2912 char major
[] = { 0, 0, 0 };
2913 char minor
[] = { 0 ,0, 0 };
2914 char patch
[] = { 0, 0, 0 };
2915 char *ver_parse
[] = { major
, minor
, patch
};
2919 while (*v
!= '\0' && v
< end
) {
2920 if (*v
!= '.' && j
< 2)
2921 ver_parse
[i
][j
++] = *v
;
2929 *m
= strtol(minor
, NULL
, 0);
2930 *p
= strtol(patch
, NULL
, 0);
2934 static __u32
migr_strip_blocks_resync(struct imsm_dev
*dev
)
2936 /* migr_strip_size when repairing or initializing parity */
2937 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
2938 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
2940 switch (get_imsm_raid_level(map
)) {
2945 return 128*1024 >> 9;
2949 static __u32
migr_strip_blocks_rebuild(struct imsm_dev
*dev
)
2951 /* migr_strip_size when rebuilding a degraded disk, no idea why
2952 * this is different than migr_strip_size_resync(), but it's good
2955 struct imsm_map
*map
= get_imsm_map(dev
, MAP_1
);
2956 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
2958 switch (get_imsm_raid_level(map
)) {
2961 if (map
->num_members
% map
->num_domains
== 0)
2962 return 128*1024 >> 9;
2966 return max((__u32
) 64*1024 >> 9, chunk
);
2968 return 128*1024 >> 9;
2972 static __u32
num_stripes_per_unit_resync(struct imsm_dev
*dev
)
2974 struct imsm_map
*lo
= get_imsm_map(dev
, MAP_0
);
2975 struct imsm_map
*hi
= get_imsm_map(dev
, MAP_1
);
2976 __u32 lo_chunk
= __le32_to_cpu(lo
->blocks_per_strip
);
2977 __u32 hi_chunk
= __le32_to_cpu(hi
->blocks_per_strip
);
2979 return max((__u32
) 1, hi_chunk
/ lo_chunk
);
2982 static __u32
num_stripes_per_unit_rebuild(struct imsm_dev
*dev
)
2984 struct imsm_map
*lo
= get_imsm_map(dev
, MAP_0
);
2985 int level
= get_imsm_raid_level(lo
);
2987 if (level
== 1 || level
== 10) {
2988 struct imsm_map
*hi
= get_imsm_map(dev
, MAP_1
);
2990 return hi
->num_domains
;
2992 return num_stripes_per_unit_resync(dev
);
2995 static unsigned long long calc_component_size(struct imsm_map
*map
,
2996 struct imsm_dev
*dev
)
2998 unsigned long long component_size
;
2999 unsigned long long dev_size
= imsm_dev_size(dev
);
3000 long long calc_dev_size
= 0;
3001 unsigned int member_disks
= imsm_num_data_members(map
);
3003 if (member_disks
== 0)
3006 component_size
= per_dev_array_size(map
);
3007 calc_dev_size
= component_size
* member_disks
;
3009 /* Component size is rounded to 1MB so difference between size from
3010 * metadata and size calculated from num_data_stripes equals up to
3011 * 2048 blocks per each device. If the difference is higher it means
3012 * that array size was expanded and num_data_stripes was not updated.
3014 if (llabs(calc_dev_size
- (long long)dev_size
) >
3015 (1 << SECT_PER_MB_SHIFT
) * member_disks
) {
3016 component_size
= dev_size
/ member_disks
;
3017 dprintf("Invalid num_data_stripes in metadata; expected=%llu, found=%llu\n",
3018 component_size
/ map
->blocks_per_strip
,
3019 num_data_stripes(map
));
3022 return component_size
;
3025 static __u32
parity_segment_depth(struct imsm_dev
*dev
)
3027 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
3028 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
3030 switch(get_imsm_raid_level(map
)) {
3033 return chunk
* map
->num_domains
;
3035 return chunk
* map
->num_members
;
3041 static __u32
map_migr_block(struct imsm_dev
*dev
, __u32 block
)
3043 struct imsm_map
*map
= get_imsm_map(dev
, MAP_1
);
3044 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
3045 __u32 strip
= block
/ chunk
;
3047 switch (get_imsm_raid_level(map
)) {
3050 __u32 vol_strip
= (strip
* map
->num_domains
) + 1;
3051 __u32 vol_stripe
= vol_strip
/ map
->num_members
;
3053 return vol_stripe
* chunk
+ block
% chunk
;
3055 __u32 stripe
= strip
/ (map
->num_members
- 1);
3057 return stripe
* chunk
+ block
% chunk
;
3064 static __u64
blocks_per_migr_unit(struct intel_super
*super
,
3065 struct imsm_dev
*dev
)
3067 /* calculate the conversion factor between per member 'blocks'
3068 * (md/{resync,rebuild}_start) and imsm migration units, return
3069 * 0 for the 'not migrating' and 'unsupported migration' cases
3071 if (!dev
->vol
.migr_state
)
3074 switch (migr_type(dev
)) {
3075 case MIGR_GEN_MIGR
: {
3076 struct migr_record
*migr_rec
= super
->migr_rec
;
3077 return __le32_to_cpu(migr_rec
->blocks_per_unit
);
3082 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
3083 __u32 stripes_per_unit
;
3084 __u32 blocks_per_unit
;
3093 /* yes, this is really the translation of migr_units to
3094 * per-member blocks in the 'resync' case
3096 stripes_per_unit
= num_stripes_per_unit_resync(dev
);
3097 migr_chunk
= migr_strip_blocks_resync(dev
);
3098 disks
= imsm_num_data_members(map
);
3099 blocks_per_unit
= stripes_per_unit
* migr_chunk
* disks
;
3100 stripe
= __le16_to_cpu(map
->blocks_per_strip
) * disks
;
3101 segment
= blocks_per_unit
/ stripe
;
3102 block_rel
= blocks_per_unit
- segment
* stripe
;
3103 parity_depth
= parity_segment_depth(dev
);
3104 block_map
= map_migr_block(dev
, block_rel
);
3105 return block_map
+ parity_depth
* segment
;
3107 case MIGR_REBUILD
: {
3108 __u32 stripes_per_unit
;
3111 stripes_per_unit
= num_stripes_per_unit_rebuild(dev
);
3112 migr_chunk
= migr_strip_blocks_rebuild(dev
);
3113 return migr_chunk
* stripes_per_unit
;
3115 case MIGR_STATE_CHANGE
:
3121 static int imsm_level_to_layout(int level
)
3129 return ALGORITHM_LEFT_ASYMMETRIC
;
3136 /*******************************************************************************
3137 * Function: read_imsm_migr_rec
3138 * Description: Function reads imsm migration record from last sector of disk
3140 * fd : disk descriptor
3141 * super : metadata info
3145 ******************************************************************************/
3146 static int read_imsm_migr_rec(int fd
, struct intel_super
*super
)
3149 unsigned int sector_size
= super
->sector_size
;
3150 unsigned long long dsize
;
3152 get_dev_size(fd
, NULL
, &dsize
);
3153 if (lseek64(fd
, dsize
- (sector_size
*MIGR_REC_SECTOR_POSITION
),
3155 pr_err("Cannot seek to anchor block: %s\n",
3159 if ((unsigned int)read(fd
, super
->migr_rec_buf
,
3160 MIGR_REC_BUF_SECTORS
*sector_size
) !=
3161 MIGR_REC_BUF_SECTORS
*sector_size
) {
3162 pr_err("Cannot read migr record block: %s\n",
3167 if (sector_size
== 4096)
3168 convert_from_4k_imsm_migr_rec(super
);
3174 static struct imsm_dev
*imsm_get_device_during_migration(
3175 struct intel_super
*super
)
3178 struct intel_dev
*dv
;
3180 for (dv
= super
->devlist
; dv
; dv
= dv
->next
) {
3181 if (is_gen_migration(dv
->dev
))
3187 /*******************************************************************************
3188 * Function: load_imsm_migr_rec
3189 * Description: Function reads imsm migration record (it is stored at the last
3192 * super : imsm internal array info
3196 * -2 : no migration in progress
3197 ******************************************************************************/
3198 static int load_imsm_migr_rec(struct intel_super
*super
)
3204 struct imsm_dev
*dev
;
3205 struct imsm_map
*map
;
3209 /* find map under migration */
3210 dev
= imsm_get_device_during_migration(super
);
3211 /* nothing to load,no migration in progress?
3216 map
= get_imsm_map(dev
, MAP_0
);
3220 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
3221 /* skip spare and failed disks
3225 /* read only from one of the first two slots
3227 slot
= get_imsm_disk_slot(map
, dl
->index
);
3228 if (slot
> 1 || slot
< 0)
3231 if (!is_fd_valid(dl
->fd
)) {
3232 sprintf(nm
, "%d:%d", dl
->major
, dl
->minor
);
3233 fd
= dev_open(nm
, O_RDONLY
);
3235 if (is_fd_valid(fd
)) {
3245 if (!is_fd_valid(fd
))
3247 retval
= read_imsm_migr_rec(fd
, super
);
3254 /*******************************************************************************
3255 * function: imsm_create_metadata_checkpoint_update
3256 * Description: It creates update for checkpoint change.
3258 * super : imsm internal array info
3259 * u : pointer to prepared update
3262 * If length is equal to 0, input pointer u contains no update
3263 ******************************************************************************/
3264 static int imsm_create_metadata_checkpoint_update(
3265 struct intel_super
*super
,
3266 struct imsm_update_general_migration_checkpoint
**u
)
3269 int update_memory_size
= 0;
3271 dprintf("(enter)\n");
3277 /* size of all update data without anchor */
3278 update_memory_size
=
3279 sizeof(struct imsm_update_general_migration_checkpoint
);
3281 *u
= xcalloc(1, update_memory_size
);
3283 dprintf("error: cannot get memory\n");
3286 (*u
)->type
= update_general_migration_checkpoint
;
3287 (*u
)->curr_migr_unit
= current_migr_unit(super
->migr_rec
);
3288 dprintf("prepared for %llu\n", (unsigned long long)(*u
)->curr_migr_unit
);
3290 return update_memory_size
;
3293 static void imsm_update_metadata_locally(struct supertype
*st
,
3294 void *buf
, int len
);
3296 /*******************************************************************************
3297 * Function: write_imsm_migr_rec
3298 * Description: Function writes imsm migration record
3299 * (at the last sector of disk)
3301 * super : imsm internal array info
3305 ******************************************************************************/
3306 static int write_imsm_migr_rec(struct supertype
*st
)
3308 struct intel_super
*super
= st
->sb
;
3309 unsigned int sector_size
= super
->sector_size
;
3310 unsigned long long dsize
;
3314 struct imsm_update_general_migration_checkpoint
*u
;
3315 struct imsm_dev
*dev
;
3316 struct imsm_map
*map
;
3318 /* find map under migration */
3319 dev
= imsm_get_device_during_migration(super
);
3320 /* if no migration, write buffer anyway to clear migr_record
3321 * on disk based on first available device
3324 dev
= get_imsm_dev(super
, super
->current_vol
< 0 ? 0 :
3325 super
->current_vol
);
3327 map
= get_imsm_map(dev
, MAP_0
);
3329 if (sector_size
== 4096)
3330 convert_to_4k_imsm_migr_rec(super
);
3331 for (sd
= super
->disks
; sd
; sd
= sd
->next
) {
3334 /* skip failed and spare devices */
3337 /* write to 2 first slots only */
3339 slot
= get_imsm_disk_slot(map
, sd
->index
);
3340 if (map
== NULL
|| slot
> 1 || slot
< 0)
3343 get_dev_size(sd
->fd
, NULL
, &dsize
);
3344 if (lseek64(sd
->fd
, dsize
- (MIGR_REC_SECTOR_POSITION
*
3347 pr_err("Cannot seek to anchor block: %s\n",
3351 if ((unsigned int)write(sd
->fd
, super
->migr_rec_buf
,
3352 MIGR_REC_BUF_SECTORS
*sector_size
) !=
3353 MIGR_REC_BUF_SECTORS
*sector_size
) {
3354 pr_err("Cannot write migr record block: %s\n",
3359 if (sector_size
== 4096)
3360 convert_from_4k_imsm_migr_rec(super
);
3361 /* update checkpoint information in metadata */
3362 len
= imsm_create_metadata_checkpoint_update(super
, &u
);
3364 dprintf("imsm: Cannot prepare update\n");
3367 /* update metadata locally */
3368 imsm_update_metadata_locally(st
, u
, len
);
3369 /* and possibly remotely */
3370 if (st
->update_tail
) {
3371 append_metadata_update(st
, u
, len
);
3372 /* during reshape we do all work inside metadata handler
3373 * manage_reshape(), so metadata update has to be triggered
3376 flush_metadata_updates(st
);
3377 st
->update_tail
= &st
->updates
;
3386 /* spare/missing disks activations are not allowe when
3387 * array/container performs reshape operation, because
3388 * all arrays in container works on the same disks set
3390 int imsm_reshape_blocks_arrays_changes(struct intel_super
*super
)
3393 struct intel_dev
*i_dev
;
3394 struct imsm_dev
*dev
;
3396 /* check whole container
3398 for (i_dev
= super
->devlist
; i_dev
; i_dev
= i_dev
->next
) {
3400 if (is_gen_migration(dev
)) {
3401 /* No repair during any migration in container
3409 static unsigned long long imsm_component_size_alignment_check(int level
,
3411 unsigned int sector_size
,
3412 unsigned long long component_size
)
3414 unsigned int component_size_alignment
;
3416 /* check component size alignment
3418 component_size_alignment
= component_size
% (chunk_size
/sector_size
);
3420 dprintf("(Level: %i, chunk_size = %i, component_size = %llu), component_size_alignment = %u\n",
3421 level
, chunk_size
, component_size
,
3422 component_size_alignment
);
3424 if (component_size_alignment
&& (level
!= 1) && (level
!= UnSet
)) {
3425 dprintf("imsm: reported component size aligned from %llu ",
3427 component_size
-= component_size_alignment
;
3428 dprintf_cont("to %llu (%i).\n",
3429 component_size
, component_size_alignment
);
3432 return component_size
;
3435 /*******************************************************************************
3436 * Function: get_bitmap_header_sector
3437 * Description: Returns the sector where the bitmap header is placed.
3439 * st : supertype information
3440 * dev_idx : index of the device with bitmap
3443 * The sector where the bitmap header is placed
3444 ******************************************************************************/
3445 static unsigned long long get_bitmap_header_sector(struct intel_super
*super
,
3448 struct imsm_dev
*dev
= get_imsm_dev(super
, dev_idx
);
3449 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
3451 if (!super
->sector_size
) {
3452 dprintf("sector size is not set\n");
3456 return pba_of_lba0(map
) + calc_component_size(map
, dev
) +
3457 (IMSM_BITMAP_HEADER_OFFSET
/ super
->sector_size
);
3460 /*******************************************************************************
3461 * Function: get_bitmap_sector
3462 * Description: Returns the sector where the bitmap is placed.
3464 * st : supertype information
3465 * dev_idx : index of the device with bitmap
3468 * The sector where the bitmap is placed
3469 ******************************************************************************/
3470 static unsigned long long get_bitmap_sector(struct intel_super
*super
,
3473 if (!super
->sector_size
) {
3474 dprintf("sector size is not set\n");
3478 return get_bitmap_header_sector(super
, dev_idx
) +
3479 (IMSM_BITMAP_HEADER_SIZE
/ super
->sector_size
);
3482 static unsigned long long get_ppl_sector(struct intel_super
*super
, int dev_idx
)
3484 struct imsm_dev
*dev
= get_imsm_dev(super
, dev_idx
);
3485 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
3487 return pba_of_lba0(map
) +
3488 (num_data_stripes(map
) * map
->blocks_per_strip
);
3491 static void getinfo_super_imsm_volume(struct supertype
*st
, struct mdinfo
*info
, char *dmap
)
3493 struct intel_super
*super
= st
->sb
;
3494 struct migr_record
*migr_rec
= super
->migr_rec
;
3495 struct imsm_dev
*dev
= get_imsm_dev(super
, super
->current_vol
);
3496 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
3497 struct imsm_map
*prev_map
= get_imsm_map(dev
, MAP_1
);
3498 struct imsm_map
*map_to_analyse
= map
;
3500 int map_disks
= info
->array
.raid_disks
;
3502 memset(info
, 0, sizeof(*info
));
3504 map_to_analyse
= prev_map
;
3506 dl
= super
->current_disk
;
3508 info
->container_member
= super
->current_vol
;
3509 info
->array
.raid_disks
= map
->num_members
;
3510 info
->array
.level
= get_imsm_raid_level(map_to_analyse
);
3511 info
->array
.layout
= imsm_level_to_layout(info
->array
.level
);
3512 info
->array
.md_minor
= -1;
3513 info
->array
.ctime
= 0;
3514 info
->array
.utime
= 0;
3515 info
->array
.chunk_size
=
3516 __le16_to_cpu(map_to_analyse
->blocks_per_strip
) << 9;
3517 info
->array
.state
= !(dev
->vol
.dirty
& RAIDVOL_DIRTY
);
3518 info
->custom_array_size
= imsm_dev_size(dev
);
3519 info
->recovery_blocked
= imsm_reshape_blocks_arrays_changes(st
->sb
);
3521 if (is_gen_migration(dev
)) {
3523 * device prev_map should be added if it is in the middle
3528 info
->reshape_active
= 1;
3529 info
->new_level
= get_imsm_raid_level(map
);
3530 info
->new_layout
= imsm_level_to_layout(info
->new_level
);
3531 info
->new_chunk
= __le16_to_cpu(map
->blocks_per_strip
) << 9;
3532 info
->delta_disks
= map
->num_members
- prev_map
->num_members
;
3533 if (info
->delta_disks
) {
3534 /* this needs to be applied to every array
3537 info
->reshape_active
= CONTAINER_RESHAPE
;
3539 /* We shape information that we give to md might have to be
3540 * modify to cope with md's requirement for reshaping arrays.
3541 * For example, when reshaping a RAID0, md requires it to be
3542 * presented as a degraded RAID4.
3543 * Also if a RAID0 is migrating to a RAID5 we need to specify
3544 * the array as already being RAID5, but the 'before' layout
3545 * is a RAID4-like layout.
3547 switch (info
->array
.level
) {
3549 switch(info
->new_level
) {
3551 /* conversion is happening as RAID4 */
3552 info
->array
.level
= 4;
3553 info
->array
.raid_disks
+= 1;
3556 /* conversion is happening as RAID5 */
3557 info
->array
.level
= 5;
3558 info
->array
.layout
= ALGORITHM_PARITY_N
;
3559 info
->delta_disks
-= 1;
3562 /* FIXME error message */
3563 info
->array
.level
= UnSet
;
3569 info
->new_level
= UnSet
;
3570 info
->new_layout
= UnSet
;
3571 info
->new_chunk
= info
->array
.chunk_size
;
3572 info
->delta_disks
= 0;
3576 info
->disk
.major
= dl
->major
;
3577 info
->disk
.minor
= dl
->minor
;
3578 info
->disk
.number
= dl
->index
;
3579 info
->disk
.raid_disk
= get_imsm_disk_slot(map_to_analyse
,
3583 info
->data_offset
= pba_of_lba0(map_to_analyse
);
3584 info
->component_size
= calc_component_size(map
, dev
);
3585 info
->component_size
= imsm_component_size_alignment_check(
3587 info
->array
.chunk_size
,
3589 info
->component_size
);
3590 info
->bb
.supported
= 1;
3592 memset(info
->uuid
, 0, sizeof(info
->uuid
));
3593 info
->recovery_start
= MaxSector
;
3595 if (info
->array
.level
== 5 &&
3596 (dev
->rwh_policy
== RWH_DISTRIBUTED
||
3597 dev
->rwh_policy
== RWH_MULTIPLE_DISTRIBUTED
)) {
3598 info
->consistency_policy
= CONSISTENCY_POLICY_PPL
;
3599 info
->ppl_sector
= get_ppl_sector(super
, super
->current_vol
);
3600 if (dev
->rwh_policy
== RWH_MULTIPLE_DISTRIBUTED
)
3601 info
->ppl_size
= MULTIPLE_PPL_AREA_SIZE_IMSM
>> 9;
3603 info
->ppl_size
= (PPL_HEADER_SIZE
+ PPL_ENTRY_SPACE
)
3605 } else if (info
->array
.level
<= 0) {
3606 info
->consistency_policy
= CONSISTENCY_POLICY_NONE
;
3608 if (dev
->rwh_policy
== RWH_BITMAP
) {
3609 info
->bitmap_offset
= get_bitmap_sector(super
, super
->current_vol
);
3610 info
->consistency_policy
= CONSISTENCY_POLICY_BITMAP
;
3612 info
->consistency_policy
= CONSISTENCY_POLICY_RESYNC
;
3616 info
->reshape_progress
= 0;
3617 info
->resync_start
= MaxSector
;
3618 if ((map_to_analyse
->map_state
== IMSM_T_STATE_UNINITIALIZED
||
3619 !(info
->array
.state
& 1)) &&
3620 imsm_reshape_blocks_arrays_changes(super
) == 0) {
3621 info
->resync_start
= 0;
3623 if (dev
->vol
.migr_state
) {
3624 switch (migr_type(dev
)) {
3627 __u64 blocks_per_unit
= blocks_per_migr_unit(super
,
3629 __u64 units
= vol_curr_migr_unit(dev
);
3631 info
->resync_start
= blocks_per_unit
* units
;
3634 case MIGR_GEN_MIGR
: {
3635 __u64 blocks_per_unit
= blocks_per_migr_unit(super
,
3637 __u64 units
= current_migr_unit(migr_rec
);
3640 if (__le32_to_cpu(migr_rec
->ascending_migr
) &&
3642 (get_num_migr_units(migr_rec
)-1)) &&
3643 (super
->migr_rec
->rec_status
==
3644 __cpu_to_le32(UNIT_SRC_IN_CP_AREA
)))
3647 info
->reshape_progress
= blocks_per_unit
* units
;
3649 dprintf("IMSM: General Migration checkpoint : %llu (%llu) -> read reshape progress : %llu\n",
3650 (unsigned long long)units
,
3651 (unsigned long long)blocks_per_unit
,
3652 info
->reshape_progress
);
3654 used_disks
= imsm_num_data_members(prev_map
);
3655 if (used_disks
> 0) {
3656 info
->custom_array_size
= per_dev_array_size(map
) *
3661 /* we could emulate the checkpointing of
3662 * 'sync_action=check' migrations, but for now
3663 * we just immediately complete them
3666 /* this is handled by container_content_imsm() */
3667 case MIGR_STATE_CHANGE
:
3668 /* FIXME handle other migrations */
3670 /* we are not dirty, so... */
3671 info
->resync_start
= MaxSector
;
3675 strncpy(info
->name
, (char *) dev
->volume
, MAX_RAID_SERIAL_LEN
);
3676 info
->name
[MAX_RAID_SERIAL_LEN
] = 0;
3678 info
->array
.major_version
= -1;
3679 info
->array
.minor_version
= -2;
3680 sprintf(info
->text_version
, "/%s/%d", st
->container_devnm
, info
->container_member
);
3681 info
->safe_mode_delay
= 4000; /* 4 secs like the Matrix driver */
3682 uuid_from_super_imsm(st
, info
->uuid
);
3686 for (i
=0; i
<map_disks
; i
++) {
3688 if (i
< info
->array
.raid_disks
) {
3689 struct imsm_disk
*dsk
;
3690 j
= get_imsm_disk_idx(dev
, i
, MAP_X
);
3691 dsk
= get_imsm_disk(super
, j
);
3692 if (dsk
&& (dsk
->status
& CONFIGURED_DISK
))
3699 static __u8
imsm_check_degraded(struct intel_super
*super
, struct imsm_dev
*dev
,
3700 int failed
, int look_in_map
);
3702 static int imsm_count_failed(struct intel_super
*super
, struct imsm_dev
*dev
,
3705 static void manage_second_map(struct intel_super
*super
, struct imsm_dev
*dev
)
3707 if (is_gen_migration(dev
)) {
3710 struct imsm_map
*map2
= get_imsm_map(dev
, MAP_1
);
3712 failed
= imsm_count_failed(super
, dev
, MAP_1
);
3713 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_1
);
3714 if (map2
->map_state
!= map_state
) {
3715 map2
->map_state
= map_state
;
3716 super
->updates_pending
++;
3721 static struct imsm_disk
*get_imsm_missing(struct intel_super
*super
, __u8 index
)
3725 for (d
= super
->missing
; d
; d
= d
->next
)
3726 if (d
->index
== index
)
3731 static void getinfo_super_imsm(struct supertype
*st
, struct mdinfo
*info
, char *map
)
3733 struct intel_super
*super
= st
->sb
;
3734 struct imsm_disk
*disk
;
3735 int map_disks
= info
->array
.raid_disks
;
3736 int max_enough
= -1;
3738 struct imsm_super
*mpb
;
3740 if (super
->current_vol
>= 0) {
3741 getinfo_super_imsm_volume(st
, info
, map
);
3744 memset(info
, 0, sizeof(*info
));
3746 /* Set raid_disks to zero so that Assemble will always pull in valid
3749 info
->array
.raid_disks
= 0;
3750 info
->array
.level
= LEVEL_CONTAINER
;
3751 info
->array
.layout
= 0;
3752 info
->array
.md_minor
= -1;
3753 info
->array
.ctime
= 0; /* N/A for imsm */
3754 info
->array
.utime
= 0;
3755 info
->array
.chunk_size
= 0;
3757 info
->disk
.major
= 0;
3758 info
->disk
.minor
= 0;
3759 info
->disk
.raid_disk
= -1;
3760 info
->reshape_active
= 0;
3761 info
->array
.major_version
= -1;
3762 info
->array
.minor_version
= -2;
3763 strcpy(info
->text_version
, "imsm");
3764 info
->safe_mode_delay
= 0;
3765 info
->disk
.number
= -1;
3766 info
->disk
.state
= 0;
3768 info
->recovery_start
= MaxSector
;
3769 info
->recovery_blocked
= imsm_reshape_blocks_arrays_changes(st
->sb
);
3770 info
->bb
.supported
= 1;
3772 /* do we have the all the insync disks that we expect? */
3773 mpb
= super
->anchor
;
3774 info
->events
= __le32_to_cpu(mpb
->generation_num
);
3776 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
3777 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
3778 int failed
, enough
, j
, missing
= 0;
3779 struct imsm_map
*map
;
3782 failed
= imsm_count_failed(super
, dev
, MAP_0
);
3783 state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
3784 map
= get_imsm_map(dev
, MAP_0
);
3786 /* any newly missing disks?
3787 * (catches single-degraded vs double-degraded)
3789 for (j
= 0; j
< map
->num_members
; j
++) {
3790 __u32 ord
= get_imsm_ord_tbl_ent(dev
, j
, MAP_0
);
3791 __u32 idx
= ord_to_idx(ord
);
3793 if (super
->disks
&& super
->disks
->index
== (int)idx
)
3794 info
->disk
.raid_disk
= j
;
3796 if (!(ord
& IMSM_ORD_REBUILD
) &&
3797 get_imsm_missing(super
, idx
)) {
3803 if (state
== IMSM_T_STATE_FAILED
)
3805 else if (state
== IMSM_T_STATE_DEGRADED
&&
3806 (state
!= map
->map_state
|| missing
))
3808 else /* we're normal, or already degraded */
3810 if (is_gen_migration(dev
) && missing
) {
3811 /* during general migration we need all disks
3812 * that process is running on.
3813 * No new missing disk is allowed.
3817 /* no more checks necessary
3821 /* in the missing/failed disk case check to see
3822 * if at least one array is runnable
3824 max_enough
= max(max_enough
, enough
);
3826 dprintf("enough: %d\n", max_enough
);
3827 info
->container_enough
= max_enough
;
3830 __u32 reserved
= imsm_reserved_sectors(super
, super
->disks
);
3832 disk
= &super
->disks
->disk
;
3833 info
->data_offset
= total_blocks(&super
->disks
->disk
) - reserved
;
3834 info
->component_size
= reserved
;
3835 info
->disk
.state
= is_configured(disk
) ? (1 << MD_DISK_ACTIVE
) : 0;
3836 /* we don't change info->disk.raid_disk here because
3837 * this state will be finalized in mdmon after we have
3838 * found the 'most fresh' version of the metadata
3840 info
->disk
.state
|= is_failed(disk
) ? (1 << MD_DISK_FAULTY
) : 0;
3841 info
->disk
.state
|= (is_spare(disk
) || is_journal(disk
)) ?
3842 0 : (1 << MD_DISK_SYNC
);
3845 /* only call uuid_from_super_imsm when this disk is part of a populated container,
3846 * ->compare_super may have updated the 'num_raid_devs' field for spares
3848 if (info
->disk
.state
& (1 << MD_DISK_SYNC
) || super
->anchor
->num_raid_devs
)
3849 uuid_from_super_imsm(st
, info
->uuid
);
3851 memcpy(info
->uuid
, uuid_zero
, sizeof(uuid_zero
));
3853 /* I don't know how to compute 'map' on imsm, so use safe default */
3856 for (i
= 0; i
< map_disks
; i
++)
3862 /* allocates memory and fills disk in mdinfo structure
3863 * for each disk in array */
3864 struct mdinfo
*getinfo_super_disks_imsm(struct supertype
*st
)
3866 struct mdinfo
*mddev
;
3867 struct intel_super
*super
= st
->sb
;
3868 struct imsm_disk
*disk
;
3871 if (!super
|| !super
->disks
)
3874 mddev
= xcalloc(1, sizeof(*mddev
));
3878 tmp
= xcalloc(1, sizeof(*tmp
));
3880 tmp
->next
= mddev
->devs
;
3882 tmp
->disk
.number
= count
++;
3883 tmp
->disk
.major
= dl
->major
;
3884 tmp
->disk
.minor
= dl
->minor
;
3885 tmp
->disk
.state
= is_configured(disk
) ?
3886 (1 << MD_DISK_ACTIVE
) : 0;
3887 tmp
->disk
.state
|= is_failed(disk
) ? (1 << MD_DISK_FAULTY
) : 0;
3888 tmp
->disk
.state
|= is_spare(disk
) ? 0 : (1 << MD_DISK_SYNC
);
3889 tmp
->disk
.raid_disk
= -1;
3895 static int update_super_imsm(struct supertype
*st
, struct mdinfo
*info
,
3896 char *update
, char *devname
, int verbose
,
3897 int uuid_set
, char *homehost
)
3899 /* For 'assemble' and 'force' we need to return non-zero if any
3900 * change was made. For others, the return value is ignored.
3901 * Update options are:
3902 * force-one : This device looks a bit old but needs to be included,
3903 * update age info appropriately.
3904 * assemble: clear any 'faulty' flag to allow this device to
3906 * force-array: Array is degraded but being forced, mark it clean
3907 * if that will be needed to assemble it.
3909 * newdev: not used ????
3910 * grow: Array has gained a new device - this is currently for
3912 * resync: mark as dirty so a resync will happen.
3913 * name: update the name - preserving the homehost
3914 * uuid: Change the uuid of the array to match watch is given
3916 * Following are not relevant for this imsm:
3917 * sparc2.2 : update from old dodgey metadata
3918 * super-minor: change the preferred_minor number
3919 * summaries: update redundant counters.
3920 * homehost: update the recorded homehost
3921 * _reshape_progress: record new reshape_progress position.
3924 struct intel_super
*super
= st
->sb
;
3925 struct imsm_super
*mpb
;
3927 /* we can only update container info */
3928 if (!super
|| super
->current_vol
>= 0 || !super
->anchor
)
3931 mpb
= super
->anchor
;
3933 if (strcmp(update
, "uuid") == 0) {
3934 /* We take this to mean that the family_num should be updated.
3935 * However that is much smaller than the uuid so we cannot really
3936 * allow an explicit uuid to be given. And it is hard to reliably
3938 * So if !uuid_set we know the current uuid is random and just used
3939 * the first 'int' and copy it to the other 3 positions.
3940 * Otherwise we require the 4 'int's to be the same as would be the
3941 * case if we are using a random uuid. So an explicit uuid will be
3942 * accepted as long as all for ints are the same... which shouldn't hurt
3945 info
->uuid
[1] = info
->uuid
[2] = info
->uuid
[3] = info
->uuid
[0];
3948 if (info
->uuid
[0] != info
->uuid
[1] ||
3949 info
->uuid
[1] != info
->uuid
[2] ||
3950 info
->uuid
[2] != info
->uuid
[3])
3956 mpb
->orig_family_num
= info
->uuid
[0];
3957 } else if (strcmp(update
, "assemble") == 0)
3962 /* successful update? recompute checksum */
3964 mpb
->check_sum
= __le32_to_cpu(__gen_imsm_checksum(mpb
));
3969 static size_t disks_to_mpb_size(int disks
)
3973 size
= sizeof(struct imsm_super
);
3974 size
+= (disks
- 1) * sizeof(struct imsm_disk
);
3975 size
+= 2 * sizeof(struct imsm_dev
);
3976 /* up to 2 maps per raid device (-2 for imsm_maps in imsm_dev */
3977 size
+= (4 - 2) * sizeof(struct imsm_map
);
3978 /* 4 possible disk_ord_tbl's */
3979 size
+= 4 * (disks
- 1) * sizeof(__u32
);
3980 /* maximum bbm log */
3981 size
+= sizeof(struct bbm_log
);
3986 static __u64
avail_size_imsm(struct supertype
*st
, __u64 devsize
,
3987 unsigned long long data_offset
)
3989 if (devsize
< (MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
))
3992 return devsize
- (MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
);
3995 static void free_devlist(struct intel_super
*super
)
3997 struct intel_dev
*dv
;
3999 while (super
->devlist
) {
4000 dv
= super
->devlist
->next
;
4001 free(super
->devlist
->dev
);
4002 free(super
->devlist
);
4003 super
->devlist
= dv
;
4007 static void imsm_copy_dev(struct imsm_dev
*dest
, struct imsm_dev
*src
)
4009 memcpy(dest
, src
, sizeof_imsm_dev(src
, 0));
4012 static int compare_super_imsm(struct supertype
*st
, struct supertype
*tst
,
4016 * 0 same, or first was empty, and second was copied
4017 * 1 sb are different
4019 struct intel_super
*first
= st
->sb
;
4020 struct intel_super
*sec
= tst
->sb
;
4028 /* in platform dependent environment test if the disks
4029 * use the same Intel hba
4030 * if not on Intel hba at all, allow anything.
4031 * doesn't check HBAs if num_raid_devs is not set, as it means
4032 * it is a free floating spare, and all spares regardless of HBA type
4033 * will fall into separate container during the assembly
4035 if (first
->hba
&& sec
->hba
&& first
->anchor
->num_raid_devs
!= 0) {
4036 if (first
->hba
->type
!= sec
->hba
->type
) {
4038 pr_err("HBAs of devices do not match %s != %s\n",
4039 get_sys_dev_type(first
->hba
->type
),
4040 get_sys_dev_type(sec
->hba
->type
));
4043 if (first
->orom
!= sec
->orom
) {
4045 pr_err("HBAs of devices do not match %s != %s\n",
4046 first
->hba
->pci_id
, sec
->hba
->pci_id
);
4051 if (first
->anchor
->num_raid_devs
> 0 &&
4052 sec
->anchor
->num_raid_devs
> 0) {
4053 /* Determine if these disks might ever have been
4054 * related. Further disambiguation can only take place
4055 * in load_super_imsm_all
4057 __u32 first_family
= first
->anchor
->orig_family_num
;
4058 __u32 sec_family
= sec
->anchor
->orig_family_num
;
4060 if (memcmp(first
->anchor
->sig
, sec
->anchor
->sig
,
4061 MAX_SIGNATURE_LENGTH
) != 0)
4064 if (first_family
== 0)
4065 first_family
= first
->anchor
->family_num
;
4066 if (sec_family
== 0)
4067 sec_family
= sec
->anchor
->family_num
;
4069 if (first_family
!= sec_family
)
4074 /* if an anchor does not have num_raid_devs set then it is a free
4075 * floating spare. don't assosiate spare with any array, as during assembly
4076 * spares shall fall into separate container, from which they can be moved
4079 if (first
->anchor
->num_raid_devs
^ sec
->anchor
->num_raid_devs
)
4085 static void fd2devname(int fd
, char *name
)
4093 snprintf(name
, MAX_RAID_SERIAL_LEN
, "/dev/%s", nm
);
4096 static int nvme_get_serial(int fd
, void *buf
, size_t buf_len
)
4098 char path
[PATH_MAX
];
4099 char *name
= fd2kname(fd
);
4104 if (strncmp(name
, "nvme", 4) != 0)
4107 if (!diskfd_to_devpath(fd
, 1, path
))
4110 return devpath_to_char(path
, "serial", buf
, buf_len
, 0);
4113 extern int scsi_get_serial(int fd
, void *buf
, size_t buf_len
);
4115 static int imsm_read_serial(int fd
, char *devname
,
4116 __u8
*serial
, size_t serial_buf_len
)
4125 memset(buf
, 0, sizeof(buf
));
4127 rv
= nvme_get_serial(fd
, buf
, sizeof(buf
));
4130 rv
= scsi_get_serial(fd
, buf
, sizeof(buf
));
4132 if (rv
&& check_env("IMSM_DEVNAME_AS_SERIAL")) {
4133 memset(serial
, 0, MAX_RAID_SERIAL_LEN
);
4134 fd2devname(fd
, (char *) serial
);
4140 pr_err("Failed to retrieve serial for %s\n",
4145 /* trim all whitespace and non-printable characters and convert
4148 for (i
= 0, dest
= buf
; i
< sizeof(buf
) && buf
[i
]; i
++) {
4151 /* ':' is reserved for use in placeholder serial
4152 * numbers for missing disks
4163 if (len
> serial_buf_len
) {
4164 /* truncate leading characters */
4165 dest
+= len
- serial_buf_len
;
4166 len
= serial_buf_len
;
4169 memset(serial
, 0, serial_buf_len
);
4170 memcpy(serial
, dest
, len
);
4175 static int serialcmp(__u8
*s1
, __u8
*s2
)
4177 return strncmp((char *) s1
, (char *) s2
, MAX_RAID_SERIAL_LEN
);
4180 static void serialcpy(__u8
*dest
, __u8
*src
)
4182 strncpy((char *) dest
, (char *) src
, MAX_RAID_SERIAL_LEN
);
4185 static struct dl
*serial_to_dl(__u8
*serial
, struct intel_super
*super
)
4189 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
4190 if (serialcmp(dl
->serial
, serial
) == 0)
4196 static struct imsm_disk
*
4197 __serial_to_disk(__u8
*serial
, struct imsm_super
*mpb
, int *idx
)
4201 for (i
= 0; i
< mpb
->num_disks
; i
++) {
4202 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, i
);
4204 if (serialcmp(disk
->serial
, serial
) == 0) {
4215 load_imsm_disk(int fd
, struct intel_super
*super
, char *devname
, int keep_fd
)
4217 struct imsm_disk
*disk
;
4222 __u8 serial
[MAX_RAID_SERIAL_LEN
];
4224 rv
= imsm_read_serial(fd
, devname
, serial
, MAX_RAID_SERIAL_LEN
);
4229 dl
= xcalloc(1, sizeof(*dl
));
4232 dl
->major
= major(stb
.st_rdev
);
4233 dl
->minor
= minor(stb
.st_rdev
);
4234 dl
->next
= super
->disks
;
4235 dl
->fd
= keep_fd
? fd
: -1;
4236 assert(super
->disks
== NULL
);
4238 serialcpy(dl
->serial
, serial
);
4241 fd2devname(fd
, name
);
4243 dl
->devname
= xstrdup(devname
);
4245 dl
->devname
= xstrdup(name
);
4247 /* look up this disk's index in the current anchor */
4248 disk
= __serial_to_disk(dl
->serial
, super
->anchor
, &dl
->index
);
4251 /* only set index on disks that are a member of a
4252 * populated contianer, i.e. one with raid_devs
4254 if (is_failed(&dl
->disk
))
4256 else if (is_spare(&dl
->disk
) || is_journal(&dl
->disk
))
4263 /* When migrating map0 contains the 'destination' state while map1
4264 * contains the current state. When not migrating map0 contains the
4265 * current state. This routine assumes that map[0].map_state is set to
4266 * the current array state before being called.
4268 * Migration is indicated by one of the following states
4269 * 1/ Idle (migr_state=0 map0state=normal||unitialized||degraded||failed)
4270 * 2/ Initialize (migr_state=1 migr_type=MIGR_INIT map0state=normal
4271 * map1state=unitialized)
4272 * 3/ Repair (Resync) (migr_state=1 migr_type=MIGR_REPAIR map0state=normal
4274 * 4/ Rebuild (migr_state=1 migr_type=MIGR_REBUILD map0state=normal
4275 * map1state=degraded)
4276 * 5/ Migration (mig_state=1 migr_type=MIGR_GEN_MIGR map0state=normal
4279 static void migrate(struct imsm_dev
*dev
, struct intel_super
*super
,
4280 __u8 to_state
, int migr_type
)
4282 struct imsm_map
*dest
;
4283 struct imsm_map
*src
= get_imsm_map(dev
, MAP_0
);
4285 dev
->vol
.migr_state
= 1;
4286 set_migr_type(dev
, migr_type
);
4287 set_vol_curr_migr_unit(dev
, 0);
4288 dest
= get_imsm_map(dev
, MAP_1
);
4290 /* duplicate and then set the target end state in map[0] */
4291 memcpy(dest
, src
, sizeof_imsm_map(src
));
4292 if (migr_type
== MIGR_GEN_MIGR
) {
4296 for (i
= 0; i
< src
->num_members
; i
++) {
4297 ord
= __le32_to_cpu(src
->disk_ord_tbl
[i
]);
4298 set_imsm_ord_tbl_ent(src
, i
, ord_to_idx(ord
));
4302 if (migr_type
== MIGR_GEN_MIGR
)
4303 /* Clear migration record */
4304 memset(super
->migr_rec
, 0, sizeof(struct migr_record
));
4306 src
->map_state
= to_state
;
4309 static void end_migration(struct imsm_dev
*dev
, struct intel_super
*super
,
4312 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
4313 struct imsm_map
*prev
= get_imsm_map(dev
, dev
->vol
.migr_state
== 0 ?
4317 /* merge any IMSM_ORD_REBUILD bits that were not successfully
4318 * completed in the last migration.
4320 * FIXME add support for raid-level-migration
4322 if (map_state
!= map
->map_state
&& (is_gen_migration(dev
) == false) &&
4323 prev
->map_state
!= IMSM_T_STATE_UNINITIALIZED
) {
4324 /* when final map state is other than expected
4325 * merge maps (not for migration)
4329 for (i
= 0; i
< prev
->num_members
; i
++)
4330 for (j
= 0; j
< map
->num_members
; j
++)
4331 /* during online capacity expansion
4332 * disks position can be changed
4333 * if takeover is used
4335 if (ord_to_idx(map
->disk_ord_tbl
[j
]) ==
4336 ord_to_idx(prev
->disk_ord_tbl
[i
])) {
4337 map
->disk_ord_tbl
[j
] |=
4338 prev
->disk_ord_tbl
[i
];
4341 failed
= imsm_count_failed(super
, dev
, MAP_0
);
4342 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
4345 dev
->vol
.migr_state
= 0;
4346 set_migr_type(dev
, 0);
4347 set_vol_curr_migr_unit(dev
, 0);
4348 map
->map_state
= map_state
;
4351 static int parse_raid_devices(struct intel_super
*super
)
4354 struct imsm_dev
*dev_new
;
4355 size_t len
, len_migr
;
4357 size_t space_needed
= 0;
4358 struct imsm_super
*mpb
= super
->anchor
;
4360 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
4361 struct imsm_dev
*dev_iter
= __get_imsm_dev(super
->anchor
, i
);
4362 struct intel_dev
*dv
;
4364 len
= sizeof_imsm_dev(dev_iter
, 0);
4365 len_migr
= sizeof_imsm_dev(dev_iter
, 1);
4367 space_needed
+= len_migr
- len
;
4369 dv
= xmalloc(sizeof(*dv
));
4370 if (max_len
< len_migr
)
4372 if (max_len
> len_migr
)
4373 space_needed
+= max_len
- len_migr
;
4374 dev_new
= xmalloc(max_len
);
4375 imsm_copy_dev(dev_new
, dev_iter
);
4378 dv
->next
= super
->devlist
;
4379 super
->devlist
= dv
;
4382 /* ensure that super->buf is large enough when all raid devices
4385 if (__le32_to_cpu(mpb
->mpb_size
) + space_needed
> super
->len
) {
4388 len
= ROUND_UP(__le32_to_cpu(mpb
->mpb_size
) + space_needed
,
4389 super
->sector_size
);
4390 if (posix_memalign(&buf
, MAX_SECTOR_SIZE
, len
) != 0)
4393 memcpy(buf
, super
->buf
, super
->len
);
4394 memset(buf
+ super
->len
, 0, len
- super
->len
);
4400 super
->extra_space
+= space_needed
;
4405 /*******************************************************************************
4406 * Function: check_mpb_migr_compatibility
4407 * Description: Function checks for unsupported migration features:
4408 * - migration optimization area (pba_of_lba0)
4409 * - descending reshape (ascending_migr)
4411 * super : imsm metadata information
4413 * 0 : migration is compatible
4414 * -1 : migration is not compatible
4415 ******************************************************************************/
4416 int check_mpb_migr_compatibility(struct intel_super
*super
)
4418 struct imsm_map
*map0
, *map1
;
4419 struct migr_record
*migr_rec
= super
->migr_rec
;
4422 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
4423 struct imsm_dev
*dev_iter
= __get_imsm_dev(super
->anchor
, i
);
4425 if (dev_iter
->vol
.migr_state
== 1 &&
4426 dev_iter
->vol
.migr_type
== MIGR_GEN_MIGR
) {
4427 /* This device is migrating */
4428 map0
= get_imsm_map(dev_iter
, MAP_0
);
4429 map1
= get_imsm_map(dev_iter
, MAP_1
);
4430 if (pba_of_lba0(map0
) != pba_of_lba0(map1
))
4431 /* migration optimization area was used */
4433 if (migr_rec
->ascending_migr
== 0 &&
4434 migr_rec
->dest_depth_per_unit
> 0)
4435 /* descending reshape not supported yet */
4442 static void __free_imsm(struct intel_super
*super
, int free_disks
);
4444 /* load_imsm_mpb - read matrix metadata
4445 * allocates super->mpb to be freed by free_imsm
4447 static int load_imsm_mpb(int fd
, struct intel_super
*super
, char *devname
)
4449 unsigned long long dsize
;
4450 unsigned long long sectors
;
4451 unsigned int sector_size
= super
->sector_size
;
4453 struct imsm_super
*anchor
;
4456 get_dev_size(fd
, NULL
, &dsize
);
4457 if (dsize
< 2*sector_size
) {
4459 pr_err("%s: device to small for imsm\n",
4464 if (lseek64(fd
, dsize
- (sector_size
* 2), SEEK_SET
) < 0) {
4466 pr_err("Cannot seek to anchor block on %s: %s\n",
4467 devname
, strerror(errno
));
4471 if (posix_memalign((void **)&anchor
, sector_size
, sector_size
) != 0) {
4473 pr_err("Failed to allocate imsm anchor buffer on %s\n", devname
);
4476 if ((unsigned int)read(fd
, anchor
, sector_size
) != sector_size
) {
4478 pr_err("Cannot read anchor block on %s: %s\n",
4479 devname
, strerror(errno
));
4484 if (strncmp((char *) anchor
->sig
, MPB_SIGNATURE
, MPB_SIG_LEN
) != 0) {
4486 pr_err("no IMSM anchor on %s\n", devname
);
4491 __free_imsm(super
, 0);
4492 /* reload capability and hba */
4494 /* capability and hba must be updated with new super allocation */
4495 find_intel_hba_capability(fd
, super
, devname
);
4496 super
->len
= ROUND_UP(anchor
->mpb_size
, sector_size
);
4497 if (posix_memalign(&super
->buf
, MAX_SECTOR_SIZE
, super
->len
) != 0) {
4499 pr_err("unable to allocate %zu byte mpb buffer\n",
4504 memcpy(super
->buf
, anchor
, sector_size
);
4506 sectors
= mpb_sectors(anchor
, sector_size
) - 1;
4509 if (posix_memalign(&super
->migr_rec_buf
, MAX_SECTOR_SIZE
,
4510 MIGR_REC_BUF_SECTORS
*MAX_SECTOR_SIZE
) != 0) {
4511 pr_err("could not allocate migr_rec buffer\n");
4515 super
->clean_migration_record_by_mdmon
= 0;
4518 check_sum
= __gen_imsm_checksum(super
->anchor
);
4519 if (check_sum
!= __le32_to_cpu(super
->anchor
->check_sum
)) {
4521 pr_err("IMSM checksum %x != %x on %s\n",
4523 __le32_to_cpu(super
->anchor
->check_sum
),
4531 /* read the extended mpb */
4532 if (lseek64(fd
, dsize
- (sector_size
* (2 + sectors
)), SEEK_SET
) < 0) {
4534 pr_err("Cannot seek to extended mpb on %s: %s\n",
4535 devname
, strerror(errno
));
4539 if ((unsigned int)read(fd
, super
->buf
+ sector_size
,
4540 super
->len
- sector_size
) != super
->len
- sector_size
) {
4542 pr_err("Cannot read extended mpb on %s: %s\n",
4543 devname
, strerror(errno
));
4547 check_sum
= __gen_imsm_checksum(super
->anchor
);
4548 if (check_sum
!= __le32_to_cpu(super
->anchor
->check_sum
)) {
4550 pr_err("IMSM checksum %x != %x on %s\n",
4551 check_sum
, __le32_to_cpu(super
->anchor
->check_sum
),
4559 static int read_imsm_migr_rec(int fd
, struct intel_super
*super
);
4561 /* clears hi bits in metadata if MPB_ATTRIB_2TB_DISK not set */
4562 static void clear_hi(struct intel_super
*super
)
4564 struct imsm_super
*mpb
= super
->anchor
;
4566 if (mpb
->attributes
& MPB_ATTRIB_2TB_DISK
)
4568 for (i
= 0; i
< mpb
->num_disks
; ++i
) {
4569 struct imsm_disk
*disk
= &mpb
->disk
[i
];
4570 disk
->total_blocks_hi
= 0;
4572 for (i
= 0; i
< mpb
->num_raid_devs
; ++i
) {
4573 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
4574 for (n
= 0; n
< 2; ++n
) {
4575 struct imsm_map
*map
= get_imsm_map(dev
, n
);
4578 map
->pba_of_lba0_hi
= 0;
4579 map
->blocks_per_member_hi
= 0;
4580 map
->num_data_stripes_hi
= 0;
4586 load_and_parse_mpb(int fd
, struct intel_super
*super
, char *devname
, int keep_fd
)
4590 err
= load_imsm_mpb(fd
, super
, devname
);
4593 if (super
->sector_size
== 4096)
4594 convert_from_4k(super
);
4595 err
= load_imsm_disk(fd
, super
, devname
, keep_fd
);
4598 err
= parse_raid_devices(super
);
4601 err
= load_bbm_log(super
);
4606 static void __free_imsm_disk(struct dl
*d
, int do_close
)
4618 static void free_imsm_disks(struct intel_super
*super
)
4622 while (super
->disks
) {
4624 super
->disks
= d
->next
;
4625 __free_imsm_disk(d
, 1);
4627 while (super
->disk_mgmt_list
) {
4628 d
= super
->disk_mgmt_list
;
4629 super
->disk_mgmt_list
= d
->next
;
4630 __free_imsm_disk(d
, 1);
4632 while (super
->missing
) {
4634 super
->missing
= d
->next
;
4635 __free_imsm_disk(d
, 1);
4640 /* free all the pieces hanging off of a super pointer */
4641 static void __free_imsm(struct intel_super
*super
, int free_disks
)
4643 struct intel_hba
*elem
, *next
;
4649 /* unlink capability description */
4651 if (super
->migr_rec_buf
) {
4652 free(super
->migr_rec_buf
);
4653 super
->migr_rec_buf
= NULL
;
4656 free_imsm_disks(super
);
4657 free_devlist(super
);
4661 free((void *)elem
->path
);
4667 free(super
->bbm_log
);
4671 static void free_imsm(struct intel_super
*super
)
4673 __free_imsm(super
, 1);
4674 free(super
->bb
.entries
);
4678 static void free_super_imsm(struct supertype
*st
)
4680 struct intel_super
*super
= st
->sb
;
4689 static struct intel_super
*alloc_super(void)
4691 struct intel_super
*super
= xcalloc(1, sizeof(*super
));
4693 super
->current_vol
= -1;
4694 super
->create_offset
= ~((unsigned long long) 0);
4696 super
->bb
.entries
= xmalloc(BBM_LOG_MAX_ENTRIES
*
4697 sizeof(struct md_bb_entry
));
4698 if (!super
->bb
.entries
) {
4707 * find and allocate hba and OROM/EFI based on valid fd of RAID component device
4709 static int find_intel_hba_capability(int fd
, struct intel_super
*super
, char *devname
)
4711 struct sys_dev
*hba_name
;
4714 if (is_fd_valid(fd
) && test_partition(fd
)) {
4715 pr_err("imsm: %s is a partition, cannot be used in IMSM\n",
4719 if (!is_fd_valid(fd
) || check_env("IMSM_NO_PLATFORM")) {
4724 hba_name
= find_disk_attached_hba(fd
, NULL
);
4727 pr_err("%s is not attached to Intel(R) RAID controller.\n",
4731 rv
= attach_hba_to_super(super
, hba_name
);
4734 struct intel_hba
*hba
= super
->hba
;
4736 pr_err("%s is attached to Intel(R) %s %s (%s),\n"
4737 " but the container is assigned to Intel(R) %s %s (",
4739 get_sys_dev_type(hba_name
->type
),
4740 hba_name
->type
== SYS_DEV_VMD
? "domain" : "RAID controller",
4741 hba_name
->pci_id
? : "Err!",
4742 get_sys_dev_type(super
->hba
->type
),
4743 hba
->type
== SYS_DEV_VMD
? "domain" : "RAID controller");
4746 fprintf(stderr
, "%s", hba
->pci_id
? : "Err!");
4748 fprintf(stderr
, ", ");
4751 fprintf(stderr
, ").\n"
4752 " Mixing devices attached to different controllers is not allowed.\n");
4756 super
->orom
= find_imsm_capability(hba_name
);
4763 /* find_missing - helper routine for load_super_imsm_all that identifies
4764 * disks that have disappeared from the system. This routine relies on
4765 * the mpb being uptodate, which it is at load time.
4767 static int find_missing(struct intel_super
*super
)
4770 struct imsm_super
*mpb
= super
->anchor
;
4772 struct imsm_disk
*disk
;
4774 for (i
= 0; i
< mpb
->num_disks
; i
++) {
4775 disk
= __get_imsm_disk(mpb
, i
);
4776 dl
= serial_to_dl(disk
->serial
, super
);
4780 dl
= xmalloc(sizeof(*dl
));
4784 dl
->devname
= xstrdup("missing");
4786 serialcpy(dl
->serial
, disk
->serial
);
4789 dl
->next
= super
->missing
;
4790 super
->missing
= dl
;
4796 static struct intel_disk
*disk_list_get(__u8
*serial
, struct intel_disk
*disk_list
)
4798 struct intel_disk
*idisk
= disk_list
;
4801 if (serialcmp(idisk
->disk
.serial
, serial
) == 0)
4803 idisk
= idisk
->next
;
4809 static int __prep_thunderdome(struct intel_super
**table
, int tbl_size
,
4810 struct intel_super
*super
,
4811 struct intel_disk
**disk_list
)
4813 struct imsm_disk
*d
= &super
->disks
->disk
;
4814 struct imsm_super
*mpb
= super
->anchor
;
4817 for (i
= 0; i
< tbl_size
; i
++) {
4818 struct imsm_super
*tbl_mpb
= table
[i
]->anchor
;
4819 struct imsm_disk
*tbl_d
= &table
[i
]->disks
->disk
;
4821 if (tbl_mpb
->family_num
== mpb
->family_num
) {
4822 if (tbl_mpb
->check_sum
== mpb
->check_sum
) {
4823 dprintf("mpb from %d:%d matches %d:%d\n",
4824 super
->disks
->major
,
4825 super
->disks
->minor
,
4826 table
[i
]->disks
->major
,
4827 table
[i
]->disks
->minor
);
4831 if (((is_configured(d
) && !is_configured(tbl_d
)) ||
4832 is_configured(d
) == is_configured(tbl_d
)) &&
4833 tbl_mpb
->generation_num
< mpb
->generation_num
) {
4834 /* current version of the mpb is a
4835 * better candidate than the one in
4836 * super_table, but copy over "cross
4837 * generational" status
4839 struct intel_disk
*idisk
;
4841 dprintf("mpb from %d:%d replaces %d:%d\n",
4842 super
->disks
->major
,
4843 super
->disks
->minor
,
4844 table
[i
]->disks
->major
,
4845 table
[i
]->disks
->minor
);
4847 idisk
= disk_list_get(tbl_d
->serial
, *disk_list
);
4848 if (idisk
&& is_failed(&idisk
->disk
))
4849 tbl_d
->status
|= FAILED_DISK
;
4852 struct intel_disk
*idisk
;
4853 struct imsm_disk
*disk
;
4855 /* tbl_mpb is more up to date, but copy
4856 * over cross generational status before
4859 disk
= __serial_to_disk(d
->serial
, mpb
, NULL
);
4860 if (disk
&& is_failed(disk
))
4861 d
->status
|= FAILED_DISK
;
4863 idisk
= disk_list_get(d
->serial
, *disk_list
);
4866 if (disk
&& is_configured(disk
))
4867 idisk
->disk
.status
|= CONFIGURED_DISK
;
4870 dprintf("mpb from %d:%d prefer %d:%d\n",
4871 super
->disks
->major
,
4872 super
->disks
->minor
,
4873 table
[i
]->disks
->major
,
4874 table
[i
]->disks
->minor
);
4882 table
[tbl_size
++] = super
;
4886 /* update/extend the merged list of imsm_disk records */
4887 for (j
= 0; j
< mpb
->num_disks
; j
++) {
4888 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, j
);
4889 struct intel_disk
*idisk
;
4891 idisk
= disk_list_get(disk
->serial
, *disk_list
);
4893 idisk
->disk
.status
|= disk
->status
;
4894 if (is_configured(&idisk
->disk
) ||
4895 is_failed(&idisk
->disk
))
4896 idisk
->disk
.status
&= ~(SPARE_DISK
);
4898 idisk
= xcalloc(1, sizeof(*idisk
));
4899 idisk
->owner
= IMSM_UNKNOWN_OWNER
;
4900 idisk
->disk
= *disk
;
4901 idisk
->next
= *disk_list
;
4905 if (serialcmp(idisk
->disk
.serial
, d
->serial
) == 0)
4912 static struct intel_super
*
4913 validate_members(struct intel_super
*super
, struct intel_disk
*disk_list
,
4916 struct imsm_super
*mpb
= super
->anchor
;
4920 for (i
= 0; i
< mpb
->num_disks
; i
++) {
4921 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, i
);
4922 struct intel_disk
*idisk
;
4924 idisk
= disk_list_get(disk
->serial
, disk_list
);
4926 if (idisk
->owner
== owner
||
4927 idisk
->owner
== IMSM_UNKNOWN_OWNER
)
4930 dprintf("'%.16s' owner %d != %d\n",
4931 disk
->serial
, idisk
->owner
,
4934 dprintf("unknown disk %x [%d]: %.16s\n",
4935 __le32_to_cpu(mpb
->family_num
), i
,
4941 if (ok_count
== mpb
->num_disks
)
4946 static void show_conflicts(__u32 family_num
, struct intel_super
*super_list
)
4948 struct intel_super
*s
;
4950 for (s
= super_list
; s
; s
= s
->next
) {
4951 if (family_num
!= s
->anchor
->family_num
)
4953 pr_err("Conflict, offlining family %#x on '%s'\n",
4954 __le32_to_cpu(family_num
), s
->disks
->devname
);
4958 static struct intel_super
*
4959 imsm_thunderdome(struct intel_super
**super_list
, int len
)
4961 struct intel_super
*super_table
[len
];
4962 struct intel_disk
*disk_list
= NULL
;
4963 struct intel_super
*champion
, *spare
;
4964 struct intel_super
*s
, **del
;
4969 memset(super_table
, 0, sizeof(super_table
));
4970 for (s
= *super_list
; s
; s
= s
->next
)
4971 tbl_size
= __prep_thunderdome(super_table
, tbl_size
, s
, &disk_list
);
4973 for (i
= 0; i
< tbl_size
; i
++) {
4974 struct imsm_disk
*d
;
4975 struct intel_disk
*idisk
;
4976 struct imsm_super
*mpb
= super_table
[i
]->anchor
;
4979 d
= &s
->disks
->disk
;
4981 /* 'd' must appear in merged disk list for its
4982 * configuration to be valid
4984 idisk
= disk_list_get(d
->serial
, disk_list
);
4985 if (idisk
&& idisk
->owner
== i
)
4986 s
= validate_members(s
, disk_list
, i
);
4991 dprintf("marking family: %#x from %d:%d offline\n",
4993 super_table
[i
]->disks
->major
,
4994 super_table
[i
]->disks
->minor
);
4998 /* This is where the mdadm implementation differs from the Windows
4999 * driver which has no strict concept of a container. We can only
5000 * assemble one family from a container, so when returning a prodigal
5001 * array member to this system the code will not be able to disambiguate
5002 * the container contents that should be assembled ("foreign" versus
5003 * "local"). It requires user intervention to set the orig_family_num
5004 * to a new value to establish a new container. The Windows driver in
5005 * this situation fixes up the volume name in place and manages the
5006 * foreign array as an independent entity.
5011 for (i
= 0; i
< tbl_size
; i
++) {
5012 struct intel_super
*tbl_ent
= super_table
[i
];
5018 if (tbl_ent
->anchor
->num_raid_devs
== 0) {
5023 if (s
&& !is_spare
) {
5024 show_conflicts(tbl_ent
->anchor
->family_num
, *super_list
);
5026 } else if (!s
&& !is_spare
)
5039 pr_err("Chose family %#x on '%s', assemble conflicts to new container with '--update=uuid'\n",
5040 __le32_to_cpu(s
->anchor
->family_num
), s
->disks
->devname
);
5042 /* collect all dl's onto 'champion', and update them to
5043 * champion's version of the status
5045 for (s
= *super_list
; s
; s
= s
->next
) {
5046 struct imsm_super
*mpb
= champion
->anchor
;
5047 struct dl
*dl
= s
->disks
;
5052 mpb
->attributes
|= s
->anchor
->attributes
& MPB_ATTRIB_2TB_DISK
;
5054 for (i
= 0; i
< mpb
->num_disks
; i
++) {
5055 struct imsm_disk
*disk
;
5057 disk
= __serial_to_disk(dl
->serial
, mpb
, &dl
->index
);
5060 /* only set index on disks that are a member of
5061 * a populated contianer, i.e. one with
5064 if (is_failed(&dl
->disk
))
5066 else if (is_spare(&dl
->disk
))
5072 if (i
>= mpb
->num_disks
) {
5073 struct intel_disk
*idisk
;
5075 idisk
= disk_list_get(dl
->serial
, disk_list
);
5076 if (idisk
&& is_spare(&idisk
->disk
) &&
5077 !is_failed(&idisk
->disk
) && !is_configured(&idisk
->disk
))
5085 dl
->next
= champion
->disks
;
5086 champion
->disks
= dl
;
5090 /* delete 'champion' from super_list */
5091 for (del
= super_list
; *del
; ) {
5092 if (*del
== champion
) {
5093 *del
= (*del
)->next
;
5096 del
= &(*del
)->next
;
5098 champion
->next
= NULL
;
5102 struct intel_disk
*idisk
= disk_list
;
5104 disk_list
= disk_list
->next
;
5112 get_sra_super_block(int fd
, struct intel_super
**super_list
, char *devname
, int *max
, int keep_fd
);
5113 static int get_super_block(struct intel_super
**super_list
, char *devnm
, char *devname
,
5114 int major
, int minor
, int keep_fd
);
5116 get_devlist_super_block(struct md_list
*devlist
, struct intel_super
**super_list
,
5117 int *max
, int keep_fd
);
5119 static int load_super_imsm_all(struct supertype
*st
, int fd
, void **sbp
,
5120 char *devname
, struct md_list
*devlist
,
5123 struct intel_super
*super_list
= NULL
;
5124 struct intel_super
*super
= NULL
;
5128 if (is_fd_valid(fd
))
5129 /* 'fd' is an opened container */
5130 err
= get_sra_super_block(fd
, &super_list
, devname
, &i
, keep_fd
);
5132 /* get super block from devlist devices */
5133 err
= get_devlist_super_block(devlist
, &super_list
, &i
, keep_fd
);
5136 /* all mpbs enter, maybe one leaves */
5137 super
= imsm_thunderdome(&super_list
, i
);
5143 if (find_missing(super
) != 0) {
5149 /* load migration record */
5150 err
= load_imsm_migr_rec(super
);
5152 /* migration is in progress,
5153 * but migr_rec cannot be loaded,
5159 /* Check migration compatibility */
5160 if (err
== 0 && check_mpb_migr_compatibility(super
) != 0) {
5161 pr_err("Unsupported migration detected");
5163 fprintf(stderr
, " on %s\n", devname
);
5165 fprintf(stderr
, " (IMSM).\n");
5174 while (super_list
) {
5175 struct intel_super
*s
= super_list
;
5177 super_list
= super_list
->next
;
5185 if (is_fd_valid(fd
))
5186 strcpy(st
->container_devnm
, fd2devnm(fd
));
5188 st
->container_devnm
[0] = 0;
5189 if (err
== 0 && st
->ss
== NULL
) {
5190 st
->ss
= &super_imsm
;
5191 st
->minor_version
= 0;
5192 st
->max_devs
= IMSM_MAX_DEVICES
;
5198 get_devlist_super_block(struct md_list
*devlist
, struct intel_super
**super_list
,
5199 int *max
, int keep_fd
)
5201 struct md_list
*tmpdev
;
5205 for (i
= 0, tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
5206 if (tmpdev
->used
!= 1)
5208 if (tmpdev
->container
== 1) {
5210 int fd
= dev_open(tmpdev
->devname
, O_RDONLY
|O_EXCL
);
5211 if (!is_fd_valid(fd
)) {
5212 pr_err("cannot open device %s: %s\n",
5213 tmpdev
->devname
, strerror(errno
));
5217 err
= get_sra_super_block(fd
, super_list
,
5218 tmpdev
->devname
, &lmax
,
5227 int major
= major(tmpdev
->st_rdev
);
5228 int minor
= minor(tmpdev
->st_rdev
);
5229 err
= get_super_block(super_list
,
5246 static int get_super_block(struct intel_super
**super_list
, char *devnm
, char *devname
,
5247 int major
, int minor
, int keep_fd
)
5249 struct intel_super
*s
;
5261 sprintf(nm
, "%d:%d", major
, minor
);
5262 dfd
= dev_open(nm
, O_RDWR
);
5263 if (!is_fd_valid(dfd
)) {
5268 if (!get_dev_sector_size(dfd
, NULL
, &s
->sector_size
)) {
5272 find_intel_hba_capability(dfd
, s
, devname
);
5273 err
= load_and_parse_mpb(dfd
, s
, NULL
, keep_fd
);
5275 /* retry the load if we might have raced against mdmon */
5276 if (err
== 3 && devnm
&& mdmon_running(devnm
))
5277 for (retry
= 0; retry
< 3; retry
++) {
5279 err
= load_and_parse_mpb(dfd
, s
, NULL
, keep_fd
);
5285 s
->next
= *super_list
;
5299 get_sra_super_block(int fd
, struct intel_super
**super_list
, char *devname
, int *max
, int keep_fd
)
5306 sra
= sysfs_read(fd
, NULL
, GET_LEVEL
|GET_VERSION
|GET_DEVS
|GET_STATE
);
5310 if (sra
->array
.major_version
!= -1 ||
5311 sra
->array
.minor_version
!= -2 ||
5312 strcmp(sra
->text_version
, "imsm") != 0) {
5317 devnm
= fd2devnm(fd
);
5318 for (sd
= sra
->devs
, i
= 0; sd
; sd
= sd
->next
, i
++) {
5319 if (get_super_block(super_list
, devnm
, devname
,
5320 sd
->disk
.major
, sd
->disk
.minor
, keep_fd
) != 0) {
5331 static int load_container_imsm(struct supertype
*st
, int fd
, char *devname
)
5333 return load_super_imsm_all(st
, fd
, &st
->sb
, devname
, NULL
, 1);
5336 static int load_super_imsm(struct supertype
*st
, int fd
, char *devname
)
5338 struct intel_super
*super
;
5342 if (test_partition(fd
))
5343 /* IMSM not allowed on partitions */
5346 free_super_imsm(st
);
5348 super
= alloc_super();
5352 if (!get_dev_sector_size(fd
, NULL
, &super
->sector_size
)) {
5356 /* Load hba and capabilities if they exist.
5357 * But do not preclude loading metadata in case capabilities or hba are
5358 * non-compliant and ignore_hw_compat is set.
5360 rv
= find_intel_hba_capability(fd
, super
, devname
);
5361 /* no orom/efi or non-intel hba of the disk */
5362 if (rv
!= 0 && st
->ignore_hw_compat
== 0) {
5364 pr_err("No OROM/EFI properties for %s\n", devname
);
5368 rv
= load_and_parse_mpb(fd
, super
, devname
, 0);
5370 /* retry the load if we might have raced against mdmon */
5372 struct mdstat_ent
*mdstat
= NULL
;
5373 char *name
= fd2kname(fd
);
5376 mdstat
= mdstat_by_component(name
);
5378 if (mdstat
&& mdmon_running(mdstat
->devnm
) && getpid() != mdmon_pid(mdstat
->devnm
)) {
5379 for (retry
= 0; retry
< 3; retry
++) {
5381 rv
= load_and_parse_mpb(fd
, super
, devname
, 0);
5387 free_mdstat(mdstat
);
5392 pr_err("Failed to load all information sections on %s\n", devname
);
5398 if (st
->ss
== NULL
) {
5399 st
->ss
= &super_imsm
;
5400 st
->minor_version
= 0;
5401 st
->max_devs
= IMSM_MAX_DEVICES
;
5404 /* load migration record */
5405 if (load_imsm_migr_rec(super
) == 0) {
5406 /* Check for unsupported migration features */
5407 if (check_mpb_migr_compatibility(super
) != 0) {
5408 pr_err("Unsupported migration detected");
5410 fprintf(stderr
, " on %s\n", devname
);
5412 fprintf(stderr
, " (IMSM).\n");
5420 static __u16
info_to_blocks_per_strip(mdu_array_info_t
*info
)
5422 if (info
->level
== 1)
5424 return info
->chunk_size
>> 9;
5427 static unsigned long long info_to_blocks_per_member(mdu_array_info_t
*info
,
5428 unsigned long long size
)
5430 if (info
->level
== 1)
5433 return (size
* 2) & ~(info_to_blocks_per_strip(info
) - 1);
5436 static void imsm_update_version_info(struct intel_super
*super
)
5438 /* update the version and attributes */
5439 struct imsm_super
*mpb
= super
->anchor
;
5441 struct imsm_dev
*dev
;
5442 struct imsm_map
*map
;
5445 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
5446 dev
= get_imsm_dev(super
, i
);
5447 map
= get_imsm_map(dev
, MAP_0
);
5448 if (__le32_to_cpu(dev
->size_high
) > 0)
5449 mpb
->attributes
|= MPB_ATTRIB_2TB
;
5451 /* FIXME detect when an array spans a port multiplier */
5453 mpb
->attributes
|= MPB_ATTRIB_PM
;
5456 if (mpb
->num_raid_devs
> 1 ||
5457 mpb
->attributes
!= MPB_ATTRIB_CHECKSUM_VERIFY
) {
5458 version
= MPB_VERSION_ATTRIBS
;
5459 switch (get_imsm_raid_level(map
)) {
5460 case 0: mpb
->attributes
|= MPB_ATTRIB_RAID0
; break;
5461 case 1: mpb
->attributes
|= MPB_ATTRIB_RAID1
; break;
5462 case 10: mpb
->attributes
|= MPB_ATTRIB_RAID10
; break;
5463 case 5: mpb
->attributes
|= MPB_ATTRIB_RAID5
; break;
5466 if (map
->num_members
>= 5)
5467 version
= MPB_VERSION_5OR6_DISK_ARRAY
;
5468 else if (dev
->status
== DEV_CLONE_N_GO
)
5469 version
= MPB_VERSION_CNG
;
5470 else if (get_imsm_raid_level(map
) == 5)
5471 version
= MPB_VERSION_RAID5
;
5472 else if (map
->num_members
>= 3)
5473 version
= MPB_VERSION_3OR4_DISK_ARRAY
;
5474 else if (get_imsm_raid_level(map
) == 1)
5475 version
= MPB_VERSION_RAID1
;
5477 version
= MPB_VERSION_RAID0
;
5479 strcpy(((char *) mpb
->sig
) + strlen(MPB_SIGNATURE
), version
);
5483 static int check_name(struct intel_super
*super
, char *name
, int quiet
)
5485 struct imsm_super
*mpb
= super
->anchor
;
5486 char *reason
= NULL
;
5488 size_t len
= strlen(name
);
5492 while (isspace(start
[len
- 1]))
5494 while (*start
&& isspace(*start
))
5496 memmove(name
, start
, len
+ 1);
5499 if (len
> MAX_RAID_SERIAL_LEN
)
5500 reason
= "must be 16 characters or less";
5502 reason
= "must be a non-empty string";
5504 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
5505 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
5507 if (strncmp((char *) dev
->volume
, name
, MAX_RAID_SERIAL_LEN
) == 0) {
5508 reason
= "already exists";
5513 if (reason
&& !quiet
)
5514 pr_err("imsm volume name %s\n", reason
);
5519 static int init_super_imsm_volume(struct supertype
*st
, mdu_array_info_t
*info
,
5520 struct shape
*s
, char *name
,
5521 char *homehost
, int *uuid
,
5522 long long data_offset
)
5524 /* We are creating a volume inside a pre-existing container.
5525 * so st->sb is already set.
5527 struct intel_super
*super
= st
->sb
;
5528 unsigned int sector_size
= super
->sector_size
;
5529 struct imsm_super
*mpb
= super
->anchor
;
5530 struct intel_dev
*dv
;
5531 struct imsm_dev
*dev
;
5532 struct imsm_vol
*vol
;
5533 struct imsm_map
*map
;
5534 int idx
= mpb
->num_raid_devs
;
5537 unsigned long long array_blocks
;
5538 size_t size_old
, size_new
;
5539 unsigned int data_disks
;
5540 unsigned long long size_per_member
;
5542 if (super
->orom
&& mpb
->num_raid_devs
>= super
->orom
->vpa
) {
5543 pr_err("This imsm-container already has the maximum of %d volumes\n", super
->orom
->vpa
);
5547 /* ensure the mpb is large enough for the new data */
5548 size_old
= __le32_to_cpu(mpb
->mpb_size
);
5549 size_new
= disks_to_mpb_size(info
->nr_disks
);
5550 if (size_new
> size_old
) {
5552 size_t size_round
= ROUND_UP(size_new
, sector_size
);
5554 if (posix_memalign(&mpb_new
, sector_size
, size_round
) != 0) {
5555 pr_err("could not allocate new mpb\n");
5558 if (posix_memalign(&super
->migr_rec_buf
, MAX_SECTOR_SIZE
,
5559 MIGR_REC_BUF_SECTORS
*
5560 MAX_SECTOR_SIZE
) != 0) {
5561 pr_err("could not allocate migr_rec buffer\n");
5567 memcpy(mpb_new
, mpb
, size_old
);
5570 super
->anchor
= mpb_new
;
5571 mpb
->mpb_size
= __cpu_to_le32(size_new
);
5572 memset(mpb_new
+ size_old
, 0, size_round
- size_old
);
5573 super
->len
= size_round
;
5575 super
->current_vol
= idx
;
5577 /* handle 'failed_disks' by either:
5578 * a) create dummy disk entries in the table if this the first
5579 * volume in the array. We add them here as this is the only
5580 * opportunity to add them. add_to_super_imsm_volume()
5581 * handles the non-failed disks and continues incrementing
5583 * b) validate that 'failed_disks' matches the current number
5584 * of missing disks if the container is populated
5586 if (super
->current_vol
== 0) {
5588 for (i
= 0; i
< info
->failed_disks
; i
++) {
5589 struct imsm_disk
*disk
;
5592 disk
= __get_imsm_disk(mpb
, i
);
5593 disk
->status
= CONFIGURED_DISK
| FAILED_DISK
;
5594 disk
->scsi_id
= __cpu_to_le32(~(__u32
)0);
5595 snprintf((char *) disk
->serial
, MAX_RAID_SERIAL_LEN
,
5596 "missing:%d", (__u8
)i
);
5598 find_missing(super
);
5603 for (d
= super
->missing
; d
; d
= d
->next
)
5605 if (info
->failed_disks
> missing
) {
5606 pr_err("unable to add 'missing' disk to container\n");
5611 if (!check_name(super
, name
, 0))
5613 dv
= xmalloc(sizeof(*dv
));
5614 dev
= xcalloc(1, sizeof(*dev
) + sizeof(__u32
) * (info
->raid_disks
- 1));
5616 * Explicitly allow truncating to not confuse gcc's
5617 * -Werror=stringop-truncation
5619 namelen
= min((int) strlen(name
), MAX_RAID_SERIAL_LEN
);
5620 memcpy(dev
->volume
, name
, namelen
);
5621 array_blocks
= calc_array_size(info
->level
, info
->raid_disks
,
5622 info
->layout
, info
->chunk_size
,
5623 s
->size
* BLOCKS_PER_KB
);
5624 data_disks
= get_data_disks(info
->level
, info
->layout
,
5626 array_blocks
= round_size_to_mb(array_blocks
, data_disks
);
5627 size_per_member
= array_blocks
/ data_disks
;
5629 set_imsm_dev_size(dev
, array_blocks
);
5630 dev
->status
= (DEV_READ_COALESCING
| DEV_WRITE_COALESCING
);
5632 vol
->migr_state
= 0;
5633 set_migr_type(dev
, MIGR_INIT
);
5634 vol
->dirty
= !info
->state
;
5635 set_vol_curr_migr_unit(dev
, 0);
5636 map
= get_imsm_map(dev
, MAP_0
);
5637 set_pba_of_lba0(map
, super
->create_offset
);
5638 map
->blocks_per_strip
= __cpu_to_le16(info_to_blocks_per_strip(info
));
5639 map
->failed_disk_num
= ~0;
5640 if (info
->level
> 0)
5641 map
->map_state
= (info
->state
? IMSM_T_STATE_NORMAL
5642 : IMSM_T_STATE_UNINITIALIZED
);
5644 map
->map_state
= info
->failed_disks
? IMSM_T_STATE_FAILED
:
5645 IMSM_T_STATE_NORMAL
;
5648 if (info
->level
== 1 && info
->raid_disks
> 2) {
5651 pr_err("imsm does not support more than 2 disksin a raid1 volume\n");
5655 map
->raid_level
= info
->level
;
5656 if (info
->level
== 10)
5657 map
->raid_level
= 1;
5658 set_num_domains(map
);
5660 size_per_member
+= NUM_BLOCKS_DIRTY_STRIPE_REGION
;
5661 set_blocks_per_member(map
, info_to_blocks_per_member(info
,
5665 map
->num_members
= info
->raid_disks
;
5666 update_num_data_stripes(map
, array_blocks
);
5667 for (i
= 0; i
< map
->num_members
; i
++) {
5668 /* initialized in add_to_super */
5669 set_imsm_ord_tbl_ent(map
, i
, IMSM_ORD_REBUILD
);
5671 mpb
->num_raid_devs
++;
5672 mpb
->num_raid_devs_created
++;
5673 dev
->my_vol_raid_dev_num
= mpb
->num_raid_devs_created
;
5675 if (s
->consistency_policy
<= CONSISTENCY_POLICY_RESYNC
) {
5676 dev
->rwh_policy
= RWH_MULTIPLE_OFF
;
5677 } else if (s
->consistency_policy
== CONSISTENCY_POLICY_PPL
) {
5678 dev
->rwh_policy
= RWH_MULTIPLE_DISTRIBUTED
;
5682 pr_err("imsm does not support consistency policy %s\n",
5683 map_num_s(consistency_policies
, s
->consistency_policy
));
5688 dv
->index
= super
->current_vol
;
5689 dv
->next
= super
->devlist
;
5690 super
->devlist
= dv
;
5692 imsm_update_version_info(super
);
5697 static int init_super_imsm(struct supertype
*st
, mdu_array_info_t
*info
,
5698 struct shape
*s
, char *name
,
5699 char *homehost
, int *uuid
,
5700 unsigned long long data_offset
)
5702 /* This is primarily called by Create when creating a new array.
5703 * We will then get add_to_super called for each component, and then
5704 * write_init_super called to write it out to each device.
5705 * For IMSM, Create can create on fresh devices or on a pre-existing
5707 * To create on a pre-existing array a different method will be called.
5708 * This one is just for fresh drives.
5710 struct intel_super
*super
;
5711 struct imsm_super
*mpb
;
5715 if (data_offset
!= INVALID_SECTORS
) {
5716 pr_err("data-offset not supported by imsm\n");
5721 return init_super_imsm_volume(st
, info
, s
, name
, homehost
, uuid
,
5725 mpb_size
= disks_to_mpb_size(info
->nr_disks
);
5727 mpb_size
= MAX_SECTOR_SIZE
;
5729 super
= alloc_super();
5731 posix_memalign(&super
->buf
, MAX_SECTOR_SIZE
, mpb_size
) != 0) {
5736 pr_err("could not allocate superblock\n");
5739 if (posix_memalign(&super
->migr_rec_buf
, MAX_SECTOR_SIZE
,
5740 MIGR_REC_BUF_SECTORS
*MAX_SECTOR_SIZE
) != 0) {
5741 pr_err("could not allocate migr_rec buffer\n");
5746 memset(super
->buf
, 0, mpb_size
);
5748 mpb
->mpb_size
= __cpu_to_le32(mpb_size
);
5752 /* zeroing superblock */
5756 mpb
->attributes
= MPB_ATTRIB_CHECKSUM_VERIFY
;
5758 version
= (char *) mpb
->sig
;
5759 strcpy(version
, MPB_SIGNATURE
);
5760 version
+= strlen(MPB_SIGNATURE
);
5761 strcpy(version
, MPB_VERSION_RAID0
);
5766 static int drive_validate_sector_size(struct intel_super
*super
, struct dl
*dl
)
5768 unsigned int member_sector_size
;
5770 if (!is_fd_valid(dl
->fd
)) {
5771 pr_err("Invalid file descriptor for %s\n", dl
->devname
);
5775 if (!get_dev_sector_size(dl
->fd
, dl
->devname
, &member_sector_size
))
5777 if (member_sector_size
!= super
->sector_size
)
5782 static int add_to_super_imsm_volume(struct supertype
*st
, mdu_disk_info_t
*dk
,
5783 int fd
, char *devname
)
5785 struct intel_super
*super
= st
->sb
;
5786 struct imsm_super
*mpb
= super
->anchor
;
5787 struct imsm_disk
*_disk
;
5788 struct imsm_dev
*dev
;
5789 struct imsm_map
*map
;
5794 if (!is_fd_valid(fd
))
5797 dev
= get_imsm_dev(super
, super
->current_vol
);
5798 map
= get_imsm_map(dev
, MAP_0
);
5800 if (! (dk
->state
& (1<<MD_DISK_SYNC
))) {
5801 pr_err("%s: Cannot add spare devices to IMSM volume\n",
5806 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
5808 if (dl
->raiddisk
== dk
->raid_disk
)
5810 } else if (dl
->major
== dk
->major
&& dl
->minor
== dk
->minor
)
5816 pr_err("%s is not a member of the same container.\n",
5821 if (!autolayout
&& super
->current_vol
> 0) {
5822 int _slot
= get_disk_slot_in_dev(super
, 0, dl
->index
);
5824 if (_slot
!= dk
->raid_disk
) {
5825 pr_err("Member %s is in %d slot for the first volume, but is in %d slot for a new volume.\n",
5826 dl
->devname
, _slot
, dk
->raid_disk
);
5827 pr_err("Raid members are in different order than for the first volume, aborting.\n");
5832 if (mpb
->num_disks
== 0)
5833 if (!get_dev_sector_size(dl
->fd
, dl
->devname
,
5834 &super
->sector_size
))
5837 if (!drive_validate_sector_size(super
, dl
)) {
5838 pr_err("Combining drives of different sector size in one volume is not allowed\n");
5842 /* add a pristine spare to the metadata */
5843 if (dl
->index
< 0) {
5844 dl
->index
= super
->anchor
->num_disks
;
5845 super
->anchor
->num_disks
++;
5847 /* Check the device has not already been added */
5848 slot
= get_imsm_disk_slot(map
, dl
->index
);
5850 (get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
) & IMSM_ORD_REBUILD
) == 0) {
5851 pr_err("%s has been included in this array twice\n",
5855 set_imsm_ord_tbl_ent(map
, dk
->raid_disk
, dl
->index
);
5856 dl
->disk
.status
= CONFIGURED_DISK
;
5858 /* update size of 'missing' disks to be at least as large as the
5859 * largest acitve member (we only have dummy missing disks when
5860 * creating the first volume)
5862 if (super
->current_vol
== 0) {
5863 for (df
= super
->missing
; df
; df
= df
->next
) {
5864 if (total_blocks(&dl
->disk
) > total_blocks(&df
->disk
))
5865 set_total_blocks(&df
->disk
, total_blocks(&dl
->disk
));
5866 _disk
= __get_imsm_disk(mpb
, df
->index
);
5871 /* refresh unset/failed slots to point to valid 'missing' entries */
5872 for (df
= super
->missing
; df
; df
= df
->next
)
5873 for (slot
= 0; slot
< mpb
->num_disks
; slot
++) {
5874 __u32 ord
= get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
);
5876 if ((ord
& IMSM_ORD_REBUILD
) == 0)
5878 set_imsm_ord_tbl_ent(map
, slot
, df
->index
| IMSM_ORD_REBUILD
);
5879 if (is_gen_migration(dev
)) {
5880 struct imsm_map
*map2
= get_imsm_map(dev
,
5882 int slot2
= get_imsm_disk_slot(map2
, df
->index
);
5883 if (slot2
< map2
->num_members
&& slot2
>= 0) {
5884 __u32 ord2
= get_imsm_ord_tbl_ent(dev
,
5887 if ((unsigned)df
->index
==
5889 set_imsm_ord_tbl_ent(map2
,
5895 dprintf("set slot:%d to missing disk:%d\n", slot
, df
->index
);
5899 /* if we are creating the first raid device update the family number */
5900 if (super
->current_vol
== 0) {
5902 struct imsm_dev
*_dev
= __get_imsm_dev(mpb
, 0);
5904 _disk
= __get_imsm_disk(mpb
, dl
->index
);
5906 pr_err("BUG mpb setup error\n");
5912 sum
+= __gen_imsm_checksum(mpb
);
5913 mpb
->family_num
= __cpu_to_le32(sum
);
5914 mpb
->orig_family_num
= mpb
->family_num
;
5915 mpb
->creation_time
= __cpu_to_le64((__u64
)time(NULL
));
5917 super
->current_disk
= dl
;
5922 * Function marks disk as spare and restores disk serial
5923 * in case it was previously marked as failed by takeover operation
5925 * -1 : critical error
5926 * 0 : disk is marked as spare but serial is not set
5929 int mark_spare(struct dl
*disk
)
5931 __u8 serial
[MAX_RAID_SERIAL_LEN
];
5938 if (!imsm_read_serial(disk
->fd
, NULL
, serial
, MAX_RAID_SERIAL_LEN
)) {
5939 /* Restore disk serial number, because takeover marks disk
5940 * as failed and adds to serial ':0' before it becomes
5943 serialcpy(disk
->serial
, serial
);
5944 serialcpy(disk
->disk
.serial
, serial
);
5947 disk
->disk
.status
= SPARE_DISK
;
5954 static int write_super_imsm_spare(struct intel_super
*super
, struct dl
*d
);
5956 static int add_to_super_imsm(struct supertype
*st
, mdu_disk_info_t
*dk
,
5957 int fd
, char *devname
,
5958 unsigned long long data_offset
)
5960 struct intel_super
*super
= st
->sb
;
5962 unsigned long long size
;
5963 unsigned int member_sector_size
;
5968 /* If we are on an RAID enabled platform check that the disk is
5969 * attached to the raid controller.
5970 * We do not need to test disks attachment for container based additions,
5971 * they shall be already tested when container was created/assembled.
5973 rv
= find_intel_hba_capability(fd
, super
, devname
);
5974 /* no orom/efi or non-intel hba of the disk */
5976 dprintf("capability: %p fd: %d ret: %d\n",
5977 super
->orom
, fd
, rv
);
5981 if (super
->current_vol
>= 0)
5982 return add_to_super_imsm_volume(st
, dk
, fd
, devname
);
5985 dd
= xcalloc(sizeof(*dd
), 1);
5986 dd
->major
= major(stb
.st_rdev
);
5987 dd
->minor
= minor(stb
.st_rdev
);
5988 dd
->devname
= devname
? xstrdup(devname
) : NULL
;
5991 dd
->action
= DISK_ADD
;
5992 rv
= imsm_read_serial(fd
, devname
, dd
->serial
, MAX_RAID_SERIAL_LEN
);
5994 pr_err("failed to retrieve scsi serial, aborting\n");
5995 __free_imsm_disk(dd
, 0);
5999 if (super
->hba
&& ((super
->hba
->type
== SYS_DEV_NVME
) ||
6000 (super
->hba
->type
== SYS_DEV_VMD
))) {
6002 char cntrl_path
[PATH_MAX
];
6004 char pci_dev_path
[PATH_MAX
];
6006 if (!diskfd_to_devpath(fd
, 2, pci_dev_path
) ||
6007 !diskfd_to_devpath(fd
, 1, cntrl_path
)) {
6008 pr_err("failed to get dev paths, aborting\n");
6009 __free_imsm_disk(dd
, 0);
6013 cntrl_name
= basename(cntrl_path
);
6014 if (is_multipath_nvme(fd
))
6015 pr_err("%s controller supports Multi-Path I/O, Intel (R) VROC does not support multipathing\n",
6018 if (devpath_to_vendor(pci_dev_path
) == 0x8086) {
6020 * If Intel's NVMe drive has serial ended with
6021 * "-A","-B","-1" or "-2" it means that this is "x8"
6022 * device (double drive on single PCIe card).
6023 * User should be warned about potential data loss.
6025 for (i
= MAX_RAID_SERIAL_LEN
-1; i
> 0; i
--) {
6026 /* Skip empty character at the end */
6027 if (dd
->serial
[i
] == 0)
6030 if (((dd
->serial
[i
] == 'A') ||
6031 (dd
->serial
[i
] == 'B') ||
6032 (dd
->serial
[i
] == '1') ||
6033 (dd
->serial
[i
] == '2')) &&
6034 (dd
->serial
[i
-1] == '-'))
6035 pr_err("\tThe action you are about to take may put your data at risk.\n"
6036 "\tPlease note that x8 devices may consist of two separate x4 devices "
6037 "located on a single PCIe port.\n"
6038 "\tRAID 0 is the only supported configuration for this type of x8 device.\n");
6041 } else if (super
->hba
->type
== SYS_DEV_VMD
&& super
->orom
&&
6042 !imsm_orom_has_tpv_support(super
->orom
)) {
6043 pr_err("\tPlatform configuration does not support non-Intel NVMe drives.\n"
6044 "\tPlease refer to Intel(R) RSTe/VROC user guide.\n");
6045 __free_imsm_disk(dd
, 0);
6050 get_dev_size(fd
, NULL
, &size
);
6051 if (!get_dev_sector_size(fd
, NULL
, &member_sector_size
)) {
6052 __free_imsm_disk(dd
, 0);
6056 if (super
->sector_size
== 0) {
6057 /* this a first device, so sector_size is not set yet */
6058 super
->sector_size
= member_sector_size
;
6061 /* clear migr_rec when adding disk to container */
6062 memset(super
->migr_rec_buf
, 0, MIGR_REC_BUF_SECTORS
*MAX_SECTOR_SIZE
);
6063 if (lseek64(fd
, size
- MIGR_REC_SECTOR_POSITION
*member_sector_size
,
6065 if ((unsigned int)write(fd
, super
->migr_rec_buf
,
6066 MIGR_REC_BUF_SECTORS
*member_sector_size
) !=
6067 MIGR_REC_BUF_SECTORS
*member_sector_size
)
6068 perror("Write migr_rec failed");
6072 serialcpy(dd
->disk
.serial
, dd
->serial
);
6073 set_total_blocks(&dd
->disk
, size
);
6074 if (__le32_to_cpu(dd
->disk
.total_blocks_hi
) > 0) {
6075 struct imsm_super
*mpb
= super
->anchor
;
6076 mpb
->attributes
|= MPB_ATTRIB_2TB_DISK
;
6079 if (sysfs_disk_to_scsi_id(fd
, &id
) == 0)
6080 dd
->disk
.scsi_id
= __cpu_to_le32(id
);
6082 dd
->disk
.scsi_id
= __cpu_to_le32(0);
6084 if (st
->update_tail
) {
6085 dd
->next
= super
->disk_mgmt_list
;
6086 super
->disk_mgmt_list
= dd
;
6088 /* this is called outside of mdmon
6089 * write initial spare metadata
6090 * mdmon will overwrite it.
6092 dd
->next
= super
->disks
;
6094 write_super_imsm_spare(super
, dd
);
6100 static int remove_from_super_imsm(struct supertype
*st
, mdu_disk_info_t
*dk
)
6102 struct intel_super
*super
= st
->sb
;
6105 /* remove from super works only in mdmon - for communication
6106 * manager - monitor. Check if communication memory buffer
6109 if (!st
->update_tail
) {
6110 pr_err("shall be used in mdmon context only\n");
6113 dd
= xcalloc(1, sizeof(*dd
));
6114 dd
->major
= dk
->major
;
6115 dd
->minor
= dk
->minor
;
6118 dd
->action
= DISK_REMOVE
;
6120 dd
->next
= super
->disk_mgmt_list
;
6121 super
->disk_mgmt_list
= dd
;
6126 static int store_imsm_mpb(int fd
, struct imsm_super
*mpb
);
6129 char buf
[MAX_SECTOR_SIZE
];
6130 struct imsm_super anchor
;
6131 } spare_record
__attribute__ ((aligned(MAX_SECTOR_SIZE
)));
6134 static int write_super_imsm_spare(struct intel_super
*super
, struct dl
*d
)
6136 struct imsm_super
*mpb
= super
->anchor
;
6137 struct imsm_super
*spare
= &spare_record
.anchor
;
6143 spare
->mpb_size
= __cpu_to_le32(sizeof(struct imsm_super
));
6144 spare
->generation_num
= __cpu_to_le32(1UL);
6145 spare
->attributes
= MPB_ATTRIB_CHECKSUM_VERIFY
;
6146 spare
->num_disks
= 1;
6147 spare
->num_raid_devs
= 0;
6148 spare
->cache_size
= mpb
->cache_size
;
6149 spare
->pwr_cycle_count
= __cpu_to_le32(1);
6151 snprintf((char *) spare
->sig
, MAX_SIGNATURE_LENGTH
,
6152 MPB_SIGNATURE MPB_VERSION_RAID0
);
6154 spare
->disk
[0] = d
->disk
;
6155 if (__le32_to_cpu(d
->disk
.total_blocks_hi
) > 0)
6156 spare
->attributes
|= MPB_ATTRIB_2TB_DISK
;
6158 if (super
->sector_size
== 4096)
6159 convert_to_4k_imsm_disk(&spare
->disk
[0]);
6161 sum
= __gen_imsm_checksum(spare
);
6162 spare
->family_num
= __cpu_to_le32(sum
);
6163 spare
->orig_family_num
= 0;
6164 sum
= __gen_imsm_checksum(spare
);
6165 spare
->check_sum
= __cpu_to_le32(sum
);
6167 if (store_imsm_mpb(d
->fd
, spare
)) {
6168 pr_err("failed for device %d:%d %s\n",
6169 d
->major
, d
->minor
, strerror(errno
));
6175 /* spare records have their own family number and do not have any defined raid
6178 static int write_super_imsm_spares(struct intel_super
*super
, int doclose
)
6182 for (d
= super
->disks
; d
; d
= d
->next
) {
6186 if (write_super_imsm_spare(super
, d
))
6196 static int write_super_imsm(struct supertype
*st
, int doclose
)
6198 struct intel_super
*super
= st
->sb
;
6199 unsigned int sector_size
= super
->sector_size
;
6200 struct imsm_super
*mpb
= super
->anchor
;
6206 __u32 mpb_size
= sizeof(struct imsm_super
) - sizeof(struct imsm_disk
);
6208 int clear_migration_record
= 1;
6211 /* 'generation' is incremented everytime the metadata is written */
6212 generation
= __le32_to_cpu(mpb
->generation_num
);
6214 mpb
->generation_num
= __cpu_to_le32(generation
);
6216 /* fix up cases where previous mdadm releases failed to set
6219 if (mpb
->orig_family_num
== 0)
6220 mpb
->orig_family_num
= mpb
->family_num
;
6222 for (d
= super
->disks
; d
; d
= d
->next
) {
6226 mpb
->disk
[d
->index
] = d
->disk
;
6230 for (d
= super
->missing
; d
; d
= d
->next
) {
6231 mpb
->disk
[d
->index
] = d
->disk
;
6234 mpb
->num_disks
= num_disks
;
6235 mpb_size
+= sizeof(struct imsm_disk
) * mpb
->num_disks
;
6237 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
6238 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
6239 struct imsm_dev
*dev2
= get_imsm_dev(super
, i
);
6241 imsm_copy_dev(dev
, dev2
);
6242 mpb_size
+= sizeof_imsm_dev(dev
, 0);
6244 if (is_gen_migration(dev2
))
6245 clear_migration_record
= 0;
6248 bbm_log_size
= get_imsm_bbm_log_size(super
->bbm_log
);
6251 memcpy((void *)mpb
+ mpb_size
, super
->bbm_log
, bbm_log_size
);
6252 mpb
->attributes
|= MPB_ATTRIB_BBM
;
6254 mpb
->attributes
&= ~MPB_ATTRIB_BBM
;
6256 super
->anchor
->bbm_log_size
= __cpu_to_le32(bbm_log_size
);
6257 mpb_size
+= bbm_log_size
;
6258 mpb
->mpb_size
= __cpu_to_le32(mpb_size
);
6261 assert(super
->len
== 0 || mpb_size
<= super
->len
);
6264 /* recalculate checksum */
6265 sum
= __gen_imsm_checksum(mpb
);
6266 mpb
->check_sum
= __cpu_to_le32(sum
);
6268 if (super
->clean_migration_record_by_mdmon
) {
6269 clear_migration_record
= 1;
6270 super
->clean_migration_record_by_mdmon
= 0;
6272 if (clear_migration_record
)
6273 memset(super
->migr_rec_buf
, 0,
6274 MIGR_REC_BUF_SECTORS
*MAX_SECTOR_SIZE
);
6276 if (sector_size
== 4096)
6277 convert_to_4k(super
);
6279 /* write the mpb for disks that compose raid devices */
6280 for (d
= super
->disks
; d
; d
= d
->next
) {
6281 if (d
->index
< 0 || is_failed(&d
->disk
))
6284 if (clear_migration_record
) {
6285 unsigned long long dsize
;
6287 get_dev_size(d
->fd
, NULL
, &dsize
);
6288 if (lseek64(d
->fd
, dsize
- sector_size
,
6290 if ((unsigned int)write(d
->fd
,
6291 super
->migr_rec_buf
,
6292 MIGR_REC_BUF_SECTORS
*sector_size
) !=
6293 MIGR_REC_BUF_SECTORS
*sector_size
)
6294 perror("Write migr_rec failed");
6298 if (store_imsm_mpb(d
->fd
, mpb
))
6300 "failed for device %d:%d (fd: %d)%s\n",
6302 d
->fd
, strerror(errno
));
6309 return write_super_imsm_spares(super
, doclose
);
6314 static int create_array(struct supertype
*st
, int dev_idx
)
6317 struct imsm_update_create_array
*u
;
6318 struct intel_super
*super
= st
->sb
;
6319 struct imsm_dev
*dev
= get_imsm_dev(super
, dev_idx
);
6320 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
6321 struct disk_info
*inf
;
6322 struct imsm_disk
*disk
;
6325 len
= sizeof(*u
) - sizeof(*dev
) + sizeof_imsm_dev(dev
, 0) +
6326 sizeof(*inf
) * map
->num_members
;
6328 u
->type
= update_create_array
;
6329 u
->dev_idx
= dev_idx
;
6330 imsm_copy_dev(&u
->dev
, dev
);
6331 inf
= get_disk_info(u
);
6332 for (i
= 0; i
< map
->num_members
; i
++) {
6333 int idx
= get_imsm_disk_idx(dev
, i
, MAP_X
);
6335 disk
= get_imsm_disk(super
, idx
);
6337 disk
= get_imsm_missing(super
, idx
);
6338 serialcpy(inf
[i
].serial
, disk
->serial
);
6340 append_metadata_update(st
, u
, len
);
6345 static int mgmt_disk(struct supertype
*st
)
6347 struct intel_super
*super
= st
->sb
;
6349 struct imsm_update_add_remove_disk
*u
;
6351 if (!super
->disk_mgmt_list
)
6356 u
->type
= update_add_remove_disk
;
6357 append_metadata_update(st
, u
, len
);
6362 __u32
crc32c_le(__u32 crc
, unsigned char const *p
, size_t len
);
6364 static int write_ppl_header(unsigned long long ppl_sector
, int fd
, void *buf
)
6366 struct ppl_header
*ppl_hdr
= buf
;
6369 ppl_hdr
->checksum
= __cpu_to_le32(~crc32c_le(~0, buf
, PPL_HEADER_SIZE
));
6371 if (lseek64(fd
, ppl_sector
* 512, SEEK_SET
) < 0) {
6373 perror("Failed to seek to PPL header location");
6377 if (write(fd
, buf
, PPL_HEADER_SIZE
) != PPL_HEADER_SIZE
) {
6379 perror("Write PPL header failed");
6388 static int write_init_ppl_imsm(struct supertype
*st
, struct mdinfo
*info
, int fd
)
6390 struct intel_super
*super
= st
->sb
;
6392 struct ppl_header
*ppl_hdr
;
6395 /* first clear entire ppl space */
6396 ret
= zero_disk_range(fd
, info
->ppl_sector
, info
->ppl_size
);
6400 ret
= posix_memalign(&buf
, MAX_SECTOR_SIZE
, PPL_HEADER_SIZE
);
6402 pr_err("Failed to allocate PPL header buffer\n");
6406 memset(buf
, 0, PPL_HEADER_SIZE
);
6408 memset(ppl_hdr
->reserved
, 0xff, PPL_HDR_RESERVED
);
6409 ppl_hdr
->signature
= __cpu_to_le32(super
->anchor
->orig_family_num
);
6411 if (info
->mismatch_cnt
) {
6413 * We are overwriting an invalid ppl. Make one entry with wrong
6414 * checksum to prevent the kernel from skipping resync.
6416 ppl_hdr
->entries_count
= __cpu_to_le32(1);
6417 ppl_hdr
->entries
[0].checksum
= ~0;
6420 ret
= write_ppl_header(info
->ppl_sector
, fd
, buf
);
6426 static int is_rebuilding(struct imsm_dev
*dev
);
6428 static int validate_ppl_imsm(struct supertype
*st
, struct mdinfo
*info
,
6429 struct mdinfo
*disk
)
6431 struct intel_super
*super
= st
->sb
;
6433 void *buf_orig
, *buf
, *buf_prev
= NULL
;
6435 struct ppl_header
*ppl_hdr
= NULL
;
6437 struct imsm_dev
*dev
;
6440 unsigned long long ppl_offset
= 0;
6441 unsigned long long prev_gen_num
= 0;
6443 if (disk
->disk
.raid_disk
< 0)
6446 dev
= get_imsm_dev(super
, info
->container_member
);
6447 idx
= get_imsm_disk_idx(dev
, disk
->disk
.raid_disk
, MAP_0
);
6448 d
= get_imsm_dl_disk(super
, idx
);
6450 if (!d
|| d
->index
< 0 || is_failed(&d
->disk
))
6453 if (posix_memalign(&buf_orig
, MAX_SECTOR_SIZE
, PPL_HEADER_SIZE
* 2)) {
6454 pr_err("Failed to allocate PPL header buffer\n");
6460 while (ppl_offset
< MULTIPLE_PPL_AREA_SIZE_IMSM
) {
6463 dprintf("Checking potential PPL at offset: %llu\n", ppl_offset
);
6465 if (lseek64(d
->fd
, info
->ppl_sector
* 512 + ppl_offset
,
6467 perror("Failed to seek to PPL header location");
6472 if (read(d
->fd
, buf
, PPL_HEADER_SIZE
) != PPL_HEADER_SIZE
) {
6473 perror("Read PPL header failed");
6480 crc
= __le32_to_cpu(ppl_hdr
->checksum
);
6481 ppl_hdr
->checksum
= 0;
6483 if (crc
!= ~crc32c_le(~0, buf
, PPL_HEADER_SIZE
)) {
6484 dprintf("Wrong PPL header checksum on %s\n",
6489 if (prev_gen_num
> __le64_to_cpu(ppl_hdr
->generation
)) {
6490 /* previous was newest, it was already checked */
6494 if ((__le32_to_cpu(ppl_hdr
->signature
) !=
6495 super
->anchor
->orig_family_num
)) {
6496 dprintf("Wrong PPL header signature on %s\n",
6503 prev_gen_num
= __le64_to_cpu(ppl_hdr
->generation
);
6505 ppl_offset
+= PPL_HEADER_SIZE
;
6506 for (i
= 0; i
< __le32_to_cpu(ppl_hdr
->entries_count
); i
++)
6508 __le32_to_cpu(ppl_hdr
->entries
[i
].pp_size
);
6511 buf_prev
= buf
+ PPL_HEADER_SIZE
;
6523 * Update metadata to use mutliple PPLs area (1MB).
6524 * This is done once for all RAID members
6526 if (info
->consistency_policy
== CONSISTENCY_POLICY_PPL
&&
6527 info
->ppl_size
!= (MULTIPLE_PPL_AREA_SIZE_IMSM
>> 9)) {
6529 struct mdinfo
*member_dev
;
6531 sprintf(subarray
, "%d", info
->container_member
);
6533 if (mdmon_running(st
->container_devnm
))
6534 st
->update_tail
= &st
->updates
;
6536 if (st
->ss
->update_subarray(st
, subarray
, "ppl", NULL
)) {
6537 pr_err("Failed to update subarray %s\n",
6540 if (st
->update_tail
)
6541 flush_metadata_updates(st
);
6543 st
->ss
->sync_metadata(st
);
6544 info
->ppl_size
= (MULTIPLE_PPL_AREA_SIZE_IMSM
>> 9);
6545 for (member_dev
= info
->devs
; member_dev
;
6546 member_dev
= member_dev
->next
)
6547 member_dev
->ppl_size
=
6548 (MULTIPLE_PPL_AREA_SIZE_IMSM
>> 9);
6553 struct imsm_map
*map
= get_imsm_map(dev
, MAP_X
);
6555 if (map
->map_state
== IMSM_T_STATE_UNINITIALIZED
||
6556 (map
->map_state
== IMSM_T_STATE_NORMAL
&&
6557 !(dev
->vol
.dirty
& RAIDVOL_DIRTY
)) ||
6558 (is_rebuilding(dev
) &&
6559 vol_curr_migr_unit(dev
) == 0 &&
6560 get_imsm_disk_idx(dev
, disk
->disk
.raid_disk
, MAP_1
) != idx
))
6561 ret
= st
->ss
->write_init_ppl(st
, info
, d
->fd
);
6563 info
->mismatch_cnt
++;
6564 } else if (ret
== 0 &&
6565 ppl_hdr
->entries_count
== 0 &&
6566 is_rebuilding(dev
) &&
6567 info
->resync_start
== 0) {
6569 * The header has no entries - add a single empty entry and
6570 * rewrite the header to prevent the kernel from going into
6571 * resync after an interrupted rebuild.
6573 ppl_hdr
->entries_count
= __cpu_to_le32(1);
6574 ret
= write_ppl_header(info
->ppl_sector
, d
->fd
, buf
);
6582 static int write_init_ppl_imsm_all(struct supertype
*st
, struct mdinfo
*info
)
6584 struct intel_super
*super
= st
->sb
;
6588 if (info
->consistency_policy
!= CONSISTENCY_POLICY_PPL
||
6589 info
->array
.level
!= 5)
6592 for (d
= super
->disks
; d
; d
= d
->next
) {
6593 if (d
->index
< 0 || is_failed(&d
->disk
))
6596 ret
= st
->ss
->write_init_ppl(st
, info
, d
->fd
);
6604 /*******************************************************************************
6605 * Function: write_init_bitmap_imsm_vol
6606 * Description: Write a bitmap header and prepares the area for the bitmap.
6608 * st : supertype information
6609 * vol_idx : the volume index to use
6614 ******************************************************************************/
6615 static int write_init_bitmap_imsm_vol(struct supertype
*st
, int vol_idx
)
6617 struct intel_super
*super
= st
->sb
;
6618 int prev_current_vol
= super
->current_vol
;
6622 super
->current_vol
= vol_idx
;
6623 for (d
= super
->disks
; d
; d
= d
->next
) {
6624 if (d
->index
< 0 || is_failed(&d
->disk
))
6626 ret
= st
->ss
->write_bitmap(st
, d
->fd
, NoUpdate
);
6630 super
->current_vol
= prev_current_vol
;
6634 /*******************************************************************************
6635 * Function: write_init_bitmap_imsm_all
6636 * Description: Write a bitmap header and prepares the area for the bitmap.
6637 * Operation is executed for volumes with CONSISTENCY_POLICY_BITMAP.
6639 * st : supertype information
6640 * info : info about the volume where the bitmap should be written
6641 * vol_idx : the volume index to use
6646 ******************************************************************************/
6647 static int write_init_bitmap_imsm_all(struct supertype
*st
, struct mdinfo
*info
,
6652 if (info
&& (info
->consistency_policy
== CONSISTENCY_POLICY_BITMAP
))
6653 ret
= write_init_bitmap_imsm_vol(st
, vol_idx
);
6658 static int write_init_super_imsm(struct supertype
*st
)
6660 struct intel_super
*super
= st
->sb
;
6661 int current_vol
= super
->current_vol
;
6665 getinfo_super_imsm(st
, &info
, NULL
);
6667 /* we are done with current_vol reset it to point st at the container */
6668 super
->current_vol
= -1;
6670 if (st
->update_tail
) {
6671 /* queue the recently created array / added disk
6672 * as a metadata update */
6674 /* determine if we are creating a volume or adding a disk */
6675 if (current_vol
< 0) {
6676 /* in the mgmt (add/remove) disk case we are running
6677 * in mdmon context, so don't close fd's
6681 /* adding the second volume to the array */
6682 rv
= write_init_ppl_imsm_all(st
, &info
);
6684 rv
= write_init_bitmap_imsm_all(st
, &info
, current_vol
);
6686 rv
= create_array(st
, current_vol
);
6690 for (d
= super
->disks
; d
; d
= d
->next
)
6691 Kill(d
->devname
, NULL
, 0, -1, 1);
6692 if (current_vol
>= 0) {
6693 rv
= write_init_ppl_imsm_all(st
, &info
);
6695 rv
= write_init_bitmap_imsm_all(st
, &info
, current_vol
);
6699 rv
= write_super_imsm(st
, 1);
6705 static int store_super_imsm(struct supertype
*st
, int fd
)
6707 struct intel_super
*super
= st
->sb
;
6708 struct imsm_super
*mpb
= super
? super
->anchor
: NULL
;
6713 if (super
->sector_size
== 4096)
6714 convert_to_4k(super
);
6715 return store_imsm_mpb(fd
, mpb
);
6718 static int validate_geometry_imsm_container(struct supertype
*st
, int level
,
6720 unsigned long long data_offset
,
6722 unsigned long long *freesize
,
6726 unsigned long long ldsize
;
6727 struct intel_super
*super
= NULL
;
6730 if (level
!= LEVEL_CONTAINER
)
6735 fd
= dev_open(dev
, O_RDONLY
|O_EXCL
);
6736 if (!is_fd_valid(fd
)) {
6737 pr_vrb("imsm: Cannot open %s: %s\n", dev
, strerror(errno
));
6740 if (!get_dev_size(fd
, dev
, &ldsize
))
6743 /* capabilities retrieve could be possible
6744 * note that there is no fd for the disks in array.
6746 super
= alloc_super();
6750 if (!get_dev_sector_size(fd
, NULL
, &super
->sector_size
))
6753 rv
= find_intel_hba_capability(fd
, super
, verbose
> 0 ? dev
: NULL
);
6757 fd2devname(fd
, str
);
6758 dprintf("fd: %d %s orom: %p rv: %d raiddisk: %d\n",
6759 fd
, str
, super
->orom
, rv
, raiddisks
);
6761 /* no orom/efi or non-intel hba of the disk */
6766 if (raiddisks
> super
->orom
->tds
) {
6768 pr_err("%d exceeds maximum number of platform supported disks: %d\n",
6769 raiddisks
, super
->orom
->tds
);
6772 if ((super
->orom
->attr
& IMSM_OROM_ATTR_2TB_DISK
) == 0 &&
6773 (ldsize
>> 9) >> 32 > 0) {
6775 pr_err("%s exceeds maximum platform supported size\n", dev
);
6779 if (super
->hba
->type
== SYS_DEV_VMD
||
6780 super
->hba
->type
== SYS_DEV_NVME
) {
6781 if (!imsm_is_nvme_namespace_supported(fd
, 1)) {
6783 pr_err("NVMe namespace %s is not supported by IMSM\n",
6790 *freesize
= avail_size_imsm(st
, ldsize
>> 9, data_offset
);
6800 static unsigned long long find_size(struct extent
*e
, int *idx
, int num_extents
)
6802 const unsigned long long base_start
= e
[*idx
].start
;
6803 unsigned long long end
= base_start
+ e
[*idx
].size
;
6806 if (base_start
== end
)
6810 for (i
= *idx
; i
< num_extents
; i
++) {
6811 /* extend overlapping extents */
6812 if (e
[i
].start
>= base_start
&&
6813 e
[i
].start
<= end
) {
6816 if (e
[i
].start
+ e
[i
].size
> end
)
6817 end
= e
[i
].start
+ e
[i
].size
;
6818 } else if (e
[i
].start
> end
) {
6824 return end
- base_start
;
6827 static unsigned long long merge_extents(struct intel_super
*super
, int sum_extents
)
6829 /* build a composite disk with all known extents and generate a new
6830 * 'maxsize' given the "all disks in an array must share a common start
6831 * offset" constraint
6833 struct extent
*e
= xcalloc(sum_extents
, sizeof(*e
));
6837 unsigned long long pos
;
6838 unsigned long long start
= 0;
6839 unsigned long long maxsize
;
6840 unsigned long reserve
;
6842 /* coalesce and sort all extents. also, check to see if we need to
6843 * reserve space between member arrays
6846 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
6849 for (i
= 0; i
< dl
->extent_cnt
; i
++)
6852 qsort(e
, sum_extents
, sizeof(*e
), cmp_extent
);
6857 while (i
< sum_extents
) {
6858 e
[j
].start
= e
[i
].start
;
6859 e
[j
].size
= find_size(e
, &i
, sum_extents
);
6861 if (e
[j
-1].size
== 0)
6870 unsigned long long esize
;
6872 esize
= e
[i
].start
- pos
;
6873 if (esize
>= maxsize
) {
6878 pos
= e
[i
].start
+ e
[i
].size
;
6880 } while (e
[i
-1].size
);
6886 /* FIXME assumes volume at offset 0 is the first volume in a
6889 if (start_extent
> 0)
6890 reserve
= IMSM_RESERVED_SECTORS
; /* gap between raid regions */
6894 if (maxsize
< reserve
)
6897 super
->create_offset
= ~((unsigned long long) 0);
6898 if (start
+ reserve
> super
->create_offset
)
6899 return 0; /* start overflows create_offset */
6900 super
->create_offset
= start
+ reserve
;
6902 return maxsize
- reserve
;
6905 static int is_raid_level_supported(const struct imsm_orom
*orom
, int level
, int raiddisks
)
6907 if (level
< 0 || level
== 6 || level
== 4)
6910 /* if we have an orom prevent invalid raid levels */
6913 case 0: return imsm_orom_has_raid0(orom
);
6916 return imsm_orom_has_raid1e(orom
);
6917 return imsm_orom_has_raid1(orom
) && raiddisks
== 2;
6918 case 10: return imsm_orom_has_raid10(orom
) && raiddisks
== 4;
6919 case 5: return imsm_orom_has_raid5(orom
) && raiddisks
> 2;
6922 return 1; /* not on an Intel RAID platform so anything goes */
6928 active_arrays_by_format(char *name
, char* hba
, struct md_list
**devlist
,
6929 int dpa
, int verbose
)
6931 struct mdstat_ent
*mdstat
= mdstat_read(0, 0);
6932 struct mdstat_ent
*memb
;
6938 for (memb
= mdstat
; memb
; memb
= memb
->next
) {
6939 if (memb
->metadata_version
&&
6940 (strncmp(memb
->metadata_version
, "external:", 9) == 0) &&
6941 (strcmp(&memb
->metadata_version
[9], name
) == 0) &&
6942 !is_subarray(memb
->metadata_version
+9) &&
6944 struct dev_member
*dev
= memb
->members
;
6946 while (dev
&& !is_fd_valid(fd
)) {
6947 char *path
= xmalloc(strlen(dev
->name
) + strlen("/dev/") + 1);
6948 num
= sprintf(path
, "%s%s", "/dev/", dev
->name
);
6950 fd
= open(path
, O_RDONLY
, 0);
6951 if (num
<= 0 || !is_fd_valid(fd
)) {
6952 pr_vrb("Cannot open %s: %s\n",
6953 dev
->name
, strerror(errno
));
6959 if (is_fd_valid(fd
) && disk_attached_to_hba(fd
, hba
)) {
6960 struct mdstat_ent
*vol
;
6961 for (vol
= mdstat
; vol
; vol
= vol
->next
) {
6962 if (vol
->active
> 0 &&
6963 vol
->metadata_version
&&
6964 is_container_member(vol
, memb
->devnm
)) {
6969 if (*devlist
&& (found
< dpa
)) {
6970 dv
= xcalloc(1, sizeof(*dv
));
6971 dv
->devname
= xmalloc(strlen(memb
->devnm
) + strlen("/dev/") + 1);
6972 sprintf(dv
->devname
, "%s%s", "/dev/", memb
->devnm
);
6975 dv
->next
= *devlist
;
6982 free_mdstat(mdstat
);
6987 static struct md_list
*
6988 get_loop_devices(void)
6991 struct md_list
*devlist
= NULL
;
6994 for(i
= 0; i
< 12; i
++) {
6995 dv
= xcalloc(1, sizeof(*dv
));
6996 dv
->devname
= xmalloc(40);
6997 sprintf(dv
->devname
, "/dev/loop%d", i
);
7005 static struct md_list
*
7006 get_devices(const char *hba_path
)
7008 struct md_list
*devlist
= NULL
;
7015 devlist
= get_loop_devices();
7018 /* scroll through /sys/dev/block looking for devices attached to
7021 dir
= opendir("/sys/dev/block");
7022 for (ent
= dir
? readdir(dir
) : NULL
; ent
; ent
= readdir(dir
)) {
7027 if (sscanf(ent
->d_name
, "%d:%d", &major
, &minor
) != 2)
7029 path
= devt_to_devpath(makedev(major
, minor
), 1, NULL
);
7032 if (!path_attached_to_hba(path
, hba_path
)) {
7039 fd
= dev_open(ent
->d_name
, O_RDONLY
);
7040 if (is_fd_valid(fd
)) {
7041 fd2devname(fd
, buf
);
7044 pr_err("cannot open device: %s\n",
7049 dv
= xcalloc(1, sizeof(*dv
));
7050 dv
->devname
= xstrdup(buf
);
7057 devlist
= devlist
->next
;
7067 count_volumes_list(struct md_list
*devlist
, char *homehost
,
7068 int verbose
, int *found
)
7070 struct md_list
*tmpdev
;
7072 struct supertype
*st
;
7074 /* first walk the list of devices to find a consistent set
7075 * that match the criterea, if that is possible.
7076 * We flag the ones we like with 'used'.
7079 st
= match_metadata_desc_imsm("imsm");
7081 pr_vrb("cannot allocate memory for imsm supertype\n");
7085 for (tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
7086 char *devname
= tmpdev
->devname
;
7088 struct supertype
*tst
;
7090 if (tmpdev
->used
> 1)
7092 tst
= dup_super(st
);
7094 pr_vrb("cannot allocate memory for imsm supertype\n");
7097 tmpdev
->container
= 0;
7098 dfd
= dev_open(devname
, O_RDONLY
|O_EXCL
);
7099 if (!is_fd_valid(dfd
)) {
7100 dprintf("cannot open device %s: %s\n",
7101 devname
, strerror(errno
));
7103 } else if (!fstat_is_blkdev(dfd
, devname
, &rdev
)) {
7105 } else if (must_be_container(dfd
)) {
7106 struct supertype
*cst
;
7107 cst
= super_by_fd(dfd
, NULL
);
7109 dprintf("cannot recognize container type %s\n",
7112 } else if (tst
->ss
!= st
->ss
) {
7113 dprintf("non-imsm container - ignore it: %s\n",
7116 } else if (!tst
->ss
->load_container
||
7117 tst
->ss
->load_container(tst
, dfd
, NULL
))
7120 tmpdev
->container
= 1;
7123 cst
->ss
->free_super(cst
);
7125 tmpdev
->st_rdev
= rdev
;
7126 if (tst
->ss
->load_super(tst
,dfd
, NULL
)) {
7127 dprintf("no RAID superblock on %s\n",
7130 } else if (tst
->ss
->compare_super
== NULL
) {
7131 dprintf("Cannot assemble %s metadata on %s\n",
7132 tst
->ss
->name
, devname
);
7138 if (tmpdev
->used
== 2 || tmpdev
->used
== 4) {
7139 /* Ignore unrecognised devices during auto-assembly */
7144 tst
->ss
->getinfo_super(tst
, &info
, NULL
);
7146 if (st
->minor_version
== -1)
7147 st
->minor_version
= tst
->minor_version
;
7149 if (memcmp(info
.uuid
, uuid_zero
,
7150 sizeof(int[4])) == 0) {
7151 /* this is a floating spare. It cannot define
7152 * an array unless there are no more arrays of
7153 * this type to be found. It can be included
7154 * in an array of this type though.
7160 if (st
->ss
!= tst
->ss
||
7161 st
->minor_version
!= tst
->minor_version
||
7162 st
->ss
->compare_super(st
, tst
, 1) != 0) {
7163 /* Some mismatch. If exactly one array matches this host,
7164 * we can resolve on that one.
7165 * Or, if we are auto assembling, we just ignore the second
7168 dprintf("superblock on %s doesn't match others - assembly aborted\n",
7174 dprintf("found: devname: %s\n", devname
);
7178 tst
->ss
->free_super(tst
);
7182 if ((err
= load_super_imsm_all(st
, -1, &st
->sb
, NULL
, devlist
, 0)) == 0) {
7183 struct mdinfo
*iter
, *head
= st
->ss
->container_content(st
, NULL
);
7184 for (iter
= head
; iter
; iter
= iter
->next
) {
7185 dprintf("content->text_version: %s vol\n",
7186 iter
->text_version
);
7187 if (iter
->array
.state
& (1<<MD_SB_BLOCK_VOLUME
)) {
7188 /* do not assemble arrays with unsupported
7190 dprintf("Cannot activate member %s.\n",
7191 iter
->text_version
);
7198 dprintf("No valid super block on device list: err: %d %p\n",
7202 dprintf("no more devices to examine\n");
7205 for (tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
7206 if (tmpdev
->used
== 1 && tmpdev
->found
) {
7208 if (count
< tmpdev
->found
)
7211 count
-= tmpdev
->found
;
7214 if (tmpdev
->used
== 1)
7219 st
->ss
->free_super(st
);
7223 static int __count_volumes(char *hba_path
, int dpa
, int verbose
,
7226 struct sys_dev
*idev
, *intel_devices
= find_intel_devices();
7228 const struct orom_entry
*entry
;
7229 struct devid_list
*dv
, *devid_list
;
7234 for (idev
= intel_devices
; idev
; idev
= idev
->next
) {
7235 if (strstr(idev
->path
, hba_path
))
7239 if (!idev
|| !idev
->dev_id
)
7242 entry
= get_orom_entry_by_device_id(idev
->dev_id
);
7244 if (!entry
|| !entry
->devid_list
)
7247 devid_list
= entry
->devid_list
;
7248 for (dv
= devid_list
; dv
; dv
= dv
->next
) {
7249 struct md_list
*devlist
;
7250 struct sys_dev
*device
= NULL
;
7255 device
= device_by_id_and_path(dv
->devid
, hba_path
);
7257 device
= device_by_id(dv
->devid
);
7260 hpath
= device
->path
;
7264 devlist
= get_devices(hpath
);
7265 /* if no intel devices return zero volumes */
7266 if (devlist
== NULL
)
7269 count
+= active_arrays_by_format("imsm", hpath
, &devlist
, dpa
,
7271 dprintf("path: %s active arrays: %d\n", hpath
, count
);
7272 if (devlist
== NULL
)
7276 count
+= count_volumes_list(devlist
,
7280 dprintf("found %d count: %d\n", found
, count
);
7283 dprintf("path: %s total number of volumes: %d\n", hpath
, count
);
7286 struct md_list
*dv
= devlist
;
7287 devlist
= devlist
->next
;
7295 static int count_volumes(struct intel_hba
*hba
, int dpa
, int verbose
)
7299 if (hba
->type
== SYS_DEV_VMD
) {
7300 struct sys_dev
*dev
;
7303 for (dev
= find_intel_devices(); dev
; dev
= dev
->next
) {
7304 if (dev
->type
== SYS_DEV_VMD
)
7305 count
+= __count_volumes(dev
->path
, dpa
,
7310 return __count_volumes(hba
->path
, dpa
, verbose
, 0);
7313 static int imsm_default_chunk(const struct imsm_orom
*orom
)
7315 /* up to 512 if the plaform supports it, otherwise the platform max.
7316 * 128 if no platform detected
7318 int fs
= max(7, orom
? fls(orom
->sss
) : 0);
7320 return min(512, (1 << fs
));
7324 validate_geometry_imsm_orom(struct intel_super
*super
, int level
, int layout
,
7325 int raiddisks
, int *chunk
, unsigned long long size
, int verbose
)
7327 /* check/set platform and metadata limits/defaults */
7328 if (super
->orom
&& raiddisks
> super
->orom
->dpa
) {
7329 pr_vrb("platform supports a maximum of %d disks per array\n",
7334 /* capabilities of OROM tested - copied from validate_geometry_imsm_volume */
7335 if (!is_raid_level_supported(super
->orom
, level
, raiddisks
)) {
7336 pr_vrb("platform does not support raid%d with %d disk%s\n",
7337 level
, raiddisks
, raiddisks
> 1 ? "s" : "");
7341 if (*chunk
== 0 || *chunk
== UnSet
)
7342 *chunk
= imsm_default_chunk(super
->orom
);
7344 if (super
->orom
&& !imsm_orom_has_chunk(super
->orom
, *chunk
)) {
7345 pr_vrb("platform does not support a chunk size of: %d\n", *chunk
);
7349 if (layout
!= imsm_level_to_layout(level
)) {
7351 pr_vrb("imsm raid 5 only supports the left-asymmetric layout\n");
7352 else if (level
== 10)
7353 pr_vrb("imsm raid 10 only supports the n2 layout\n");
7355 pr_vrb("imsm unknown layout %#x for this raid level %d\n",
7360 if (super
->orom
&& (super
->orom
->attr
& IMSM_OROM_ATTR_2TB
) == 0 &&
7361 (calc_array_size(level
, raiddisks
, layout
, *chunk
, size
) >> 32) > 0) {
7362 pr_vrb("platform does not support a volume size over 2TB\n");
7369 /* validate_geometry_imsm_volume - lifted from validate_geometry_ddf_bvd
7370 * FIX ME add ahci details
7372 static int validate_geometry_imsm_volume(struct supertype
*st
, int level
,
7373 int layout
, int raiddisks
, int *chunk
,
7374 unsigned long long size
,
7375 unsigned long long data_offset
,
7377 unsigned long long *freesize
,
7381 struct intel_super
*super
= st
->sb
;
7382 struct imsm_super
*mpb
;
7384 unsigned long long pos
= 0;
7385 unsigned long long maxsize
;
7389 /* We must have the container info already read in. */
7393 mpb
= super
->anchor
;
7395 if (!validate_geometry_imsm_orom(super
, level
, layout
, raiddisks
, chunk
, size
, verbose
)) {
7396 pr_err("RAID geometry validation failed. Cannot proceed with the action(s).\n");
7400 /* General test: make sure there is space for
7401 * 'raiddisks' device extents of size 'size' at a given
7404 unsigned long long minsize
= size
;
7405 unsigned long long start_offset
= MaxSector
;
7408 minsize
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
7409 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
7414 e
= get_extents(super
, dl
, 0);
7417 unsigned long long esize
;
7418 esize
= e
[i
].start
- pos
;
7419 if (esize
>= minsize
)
7421 if (found
&& start_offset
== MaxSector
) {
7424 } else if (found
&& pos
!= start_offset
) {
7428 pos
= e
[i
].start
+ e
[i
].size
;
7430 } while (e
[i
-1].size
);
7435 if (dcnt
< raiddisks
) {
7437 pr_err("imsm: Not enough devices with space for this array (%d < %d)\n",
7444 /* This device must be a member of the set */
7445 if (!stat_is_blkdev(dev
, &rdev
))
7447 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
7448 if (dl
->major
== (int)major(rdev
) &&
7449 dl
->minor
== (int)minor(rdev
))
7454 pr_err("%s is not in the same imsm set\n", dev
);
7456 } else if (super
->orom
&& dl
->index
< 0 && mpb
->num_raid_devs
) {
7457 /* If a volume is present then the current creation attempt
7458 * cannot incorporate new spares because the orom may not
7459 * understand this configuration (all member disks must be
7460 * members of each array in the container).
7462 pr_err("%s is a spare and a volume is already defined for this container\n", dev
);
7463 pr_err("The option-rom requires all member disks to be a member of all volumes\n");
7465 } else if (super
->orom
&& mpb
->num_raid_devs
> 0 &&
7466 mpb
->num_disks
!= raiddisks
) {
7467 pr_err("The option-rom requires all member disks to be a member of all volumes\n");
7471 /* retrieve the largest free space block */
7472 e
= get_extents(super
, dl
, 0);
7477 unsigned long long esize
;
7479 esize
= e
[i
].start
- pos
;
7480 if (esize
>= maxsize
)
7482 pos
= e
[i
].start
+ e
[i
].size
;
7484 } while (e
[i
-1].size
);
7489 pr_err("unable to determine free space for: %s\n",
7493 if (maxsize
< size
) {
7495 pr_err("%s not enough space (%llu < %llu)\n",
7496 dev
, maxsize
, size
);
7500 /* count total number of extents for merge */
7502 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
7504 i
+= dl
->extent_cnt
;
7506 maxsize
= merge_extents(super
, i
);
7508 if (mpb
->num_raid_devs
> 0 && size
&& size
!= maxsize
)
7509 pr_err("attempting to create a second volume with size less then remaining space.\n");
7511 if (maxsize
< size
|| maxsize
== 0) {
7514 pr_err("no free space left on device. Aborting...\n");
7516 pr_err("not enough space to create volume of given size (%llu < %llu). Aborting...\n",
7522 *freesize
= maxsize
;
7525 int count
= count_volumes(super
->hba
,
7526 super
->orom
->dpa
, verbose
);
7527 if (super
->orom
->vphba
<= count
) {
7528 pr_vrb("platform does not support more than %d raid volumes.\n",
7529 super
->orom
->vphba
);
7537 * imsm_get_free_size() - get the biggest, common free space from members.
7538 * @super: &intel_super pointer, not NULL.
7539 * @raiddisks: number of raid disks.
7540 * @size: requested size, could be 0 (means max size).
7541 * @chunk: requested chunk.
7542 * @freesize: pointer for returned size value.
7544 * Return: &IMSM_STATUS_OK or &IMSM_STATUS_ERROR.
7546 * @freesize is set to meaningful value, this can be @size, or calculated
7548 * super->create_offset value is modified and set appropriately in
7549 * merge_extends() for further creation.
7551 static imsm_status_t
imsm_get_free_size(struct intel_super
*super
,
7552 const int raiddisks
,
7553 unsigned long long size
,
7555 unsigned long long *freesize
)
7557 struct imsm_super
*mpb
= super
->anchor
;
7562 unsigned long long maxsize
;
7563 unsigned long long minsize
;
7567 /* find the largest common start free region of the possible disks */
7571 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
7577 /* don't activate new spares if we are orom constrained
7578 * and there is already a volume active in the container
7580 if (super
->orom
&& dl
->index
< 0 && mpb
->num_raid_devs
)
7583 e
= get_extents(super
, dl
, 0);
7586 for (i
= 1; e
[i
-1].size
; i
++)
7594 maxsize
= merge_extents(super
, extent_cnt
);
7598 minsize
= chunk
* 2;
7600 if (cnt
< raiddisks
|| (super
->orom
&& used
&& used
!= raiddisks
) ||
7601 maxsize
< minsize
|| maxsize
== 0) {
7602 pr_err("not enough devices with space to create array.\n");
7603 return IMSM_STATUS_ERROR
;
7614 if (mpb
->num_raid_devs
> 0 && size
&& size
!= maxsize
)
7615 pr_err("attempting to create a second volume with size less then remaining space.\n");
7618 dprintf("imsm: imsm_get_free_size() returns : %llu\n", size
);
7620 return IMSM_STATUS_OK
;
7624 * autolayout_imsm() - automatically layout a new volume.
7625 * @super: &intel_super pointer, not NULL.
7626 * @raiddisks: number of raid disks.
7627 * @size: requested size, could be 0 (means max size).
7628 * @chunk: requested chunk.
7629 * @freesize: pointer for returned size value.
7631 * We are being asked to automatically layout a new volume based on the current
7632 * contents of the container. If the parameters can be satisfied autolayout_imsm
7633 * will record the disks, start offset, and will return size of the volume to
7634 * be created. See imsm_get_free_size() for details.
7635 * add_to_super() and getinfo_super() detect when autolayout is in progress.
7636 * If first volume exists, slots are set consistently to it.
7638 * Return: &IMSM_STATUS_OK on success, &IMSM_STATUS_ERROR otherwise.
7640 * Disks are marked for creation via dl->raiddisk.
7642 static imsm_status_t
autolayout_imsm(struct intel_super
*super
,
7643 const int raiddisks
,
7644 unsigned long long size
, const int chunk
,
7645 unsigned long long *freesize
)
7649 int vol_cnt
= super
->anchor
->num_raid_devs
;
7652 rv
= imsm_get_free_size(super
, raiddisks
, size
, chunk
, freesize
);
7653 if (rv
!= IMSM_STATUS_OK
)
7654 return IMSM_STATUS_ERROR
;
7656 for (disk
= super
->disks
; disk
; disk
= disk
->next
) {
7660 if (curr_slot
== raiddisks
)
7664 disk
->raiddisk
= curr_slot
;
7666 int _slot
= get_disk_slot_in_dev(super
, 0, disk
->index
);
7669 pr_err("Disk %s is not used in first volume, aborting\n",
7671 return IMSM_STATUS_ERROR
;
7673 disk
->raiddisk
= _slot
;
7678 return IMSM_STATUS_OK
;
7681 static int validate_geometry_imsm(struct supertype
*st
, int level
, int layout
,
7682 int raiddisks
, int *chunk
, unsigned long long size
,
7683 unsigned long long data_offset
,
7684 char *dev
, unsigned long long *freesize
,
7685 int consistency_policy
, int verbose
)
7692 * if given unused devices create a container
7693 * if given given devices in a container create a member volume
7695 if (level
== LEVEL_CONTAINER
)
7696 /* Must be a fresh device to add to a container */
7697 return validate_geometry_imsm_container(st
, level
, raiddisks
,
7702 * Size is given in sectors.
7704 if (size
&& (size
< 2048)) {
7705 pr_err("Given size must be greater than 1M.\n");
7706 /* Depends on algorithm in Create.c :
7707 * if container was given (dev == NULL) return -1,
7708 * if block device was given ( dev != NULL) return 0.
7710 return dev
? -1 : 0;
7714 struct intel_super
*super
= st
->sb
;
7717 * Autolayout mode, st->sb and freesize must be set.
7719 if (!super
|| !freesize
) {
7720 pr_vrb("freesize and superblock must be set for autolayout, aborting\n");
7724 if (!validate_geometry_imsm_orom(st
->sb
, level
, layout
,
7725 raiddisks
, chunk
, size
,
7731 int count
= count_volumes(super
->hba
, super
->orom
->dpa
,
7733 if (super
->orom
->vphba
<= count
) {
7734 pr_vrb("platform does not support more than %d raid volumes.\n",
7735 super
->orom
->vphba
);
7739 rv
= autolayout_imsm(super
, raiddisks
, size
, *chunk
,
7741 if (rv
!= IMSM_STATUS_OK
)
7747 /* creating in a given container */
7748 return validate_geometry_imsm_volume(st
, level
, layout
,
7749 raiddisks
, chunk
, size
,
7751 dev
, freesize
, verbose
);
7754 /* This device needs to be a device in an 'imsm' container */
7755 fd
= open(dev
, O_RDONLY
|O_EXCL
, 0);
7757 if (is_fd_valid(fd
)) {
7758 pr_vrb("Cannot create this array on device %s\n", dev
);
7763 fd
= open(dev
, O_RDONLY
, 0);
7765 if (!is_fd_valid(fd
)) {
7766 pr_vrb("Cannot open %s: %s\n", dev
, strerror(errno
));
7770 /* Well, it is in use by someone, maybe an 'imsm' container. */
7771 cfd
= open_container(fd
);
7774 if (!is_fd_valid(cfd
)) {
7775 pr_vrb("Cannot use %s: It is busy\n", dev
);
7778 sra
= sysfs_read(cfd
, NULL
, GET_VERSION
);
7779 if (sra
&& sra
->array
.major_version
== -1 &&
7780 strcmp(sra
->text_version
, "imsm") == 0)
7784 /* This is a member of a imsm container. Load the container
7785 * and try to create a volume
7787 struct intel_super
*super
;
7789 if (load_super_imsm_all(st
, cfd
, (void **) &super
, NULL
, NULL
, 1) == 0) {
7791 strcpy(st
->container_devnm
, fd2devnm(cfd
));
7793 return validate_geometry_imsm_volume(st
, level
, layout
,
7795 size
, data_offset
, dev
,
7802 pr_err("failed container membership check\n");
7808 static void default_geometry_imsm(struct supertype
*st
, int *level
, int *layout
, int *chunk
)
7810 struct intel_super
*super
= st
->sb
;
7812 if (level
&& *level
== UnSet
)
7813 *level
= LEVEL_CONTAINER
;
7815 if (level
&& layout
&& *layout
== UnSet
)
7816 *layout
= imsm_level_to_layout(*level
);
7818 if (chunk
&& (*chunk
== UnSet
|| *chunk
== 0))
7819 *chunk
= imsm_default_chunk(super
->orom
);
7822 static void handle_missing(struct intel_super
*super
, struct imsm_dev
*dev
);
7824 static int kill_subarray_imsm(struct supertype
*st
, char *subarray_id
)
7826 /* remove the subarray currently referenced by subarray_id */
7828 struct intel_dev
**dp
;
7829 struct intel_super
*super
= st
->sb
;
7830 __u8 current_vol
= strtoul(subarray_id
, NULL
, 10);
7831 struct imsm_super
*mpb
= super
->anchor
;
7833 if (mpb
->num_raid_devs
== 0)
7836 /* block deletions that would change the uuid of active subarrays
7838 * FIXME when immutable ids are available, but note that we'll
7839 * also need to fixup the invalidated/active subarray indexes in
7842 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
7845 if (i
< current_vol
)
7847 sprintf(subarray
, "%u", i
);
7848 if (is_subarray_active(subarray
, st
->devnm
)) {
7849 pr_err("deleting subarray-%d would change the UUID of active subarray-%d, aborting\n",
7856 if (st
->update_tail
) {
7857 struct imsm_update_kill_array
*u
= xmalloc(sizeof(*u
));
7859 u
->type
= update_kill_array
;
7860 u
->dev_idx
= current_vol
;
7861 append_metadata_update(st
, u
, sizeof(*u
));
7866 for (dp
= &super
->devlist
; *dp
;)
7867 if ((*dp
)->index
== current_vol
) {
7870 handle_missing(super
, (*dp
)->dev
);
7871 if ((*dp
)->index
> current_vol
)
7876 /* no more raid devices, all active components are now spares,
7877 * but of course failed are still failed
7879 if (--mpb
->num_raid_devs
== 0) {
7882 for (d
= super
->disks
; d
; d
= d
->next
)
7887 super
->updates_pending
++;
7892 static int get_rwh_policy_from_update(char *update
)
7894 if (strcmp(update
, "ppl") == 0)
7895 return RWH_MULTIPLE_DISTRIBUTED
;
7896 else if (strcmp(update
, "no-ppl") == 0)
7897 return RWH_MULTIPLE_OFF
;
7898 else if (strcmp(update
, "bitmap") == 0)
7900 else if (strcmp(update
, "no-bitmap") == 0)
7905 static int update_subarray_imsm(struct supertype
*st
, char *subarray
,
7906 char *update
, struct mddev_ident
*ident
)
7908 /* update the subarray currently referenced by ->current_vol */
7909 struct intel_super
*super
= st
->sb
;
7910 struct imsm_super
*mpb
= super
->anchor
;
7912 if (strcmp(update
, "name") == 0) {
7913 char *name
= ident
->name
;
7917 if (is_subarray_active(subarray
, st
->devnm
)) {
7918 pr_err("Unable to update name of active subarray\n");
7922 if (!check_name(super
, name
, 0))
7925 vol
= strtoul(subarray
, &ep
, 10);
7926 if (*ep
!= '\0' || vol
>= super
->anchor
->num_raid_devs
)
7929 if (st
->update_tail
) {
7930 struct imsm_update_rename_array
*u
= xmalloc(sizeof(*u
));
7932 u
->type
= update_rename_array
;
7934 strncpy((char *) u
->name
, name
, MAX_RAID_SERIAL_LEN
);
7935 u
->name
[MAX_RAID_SERIAL_LEN
-1] = '\0';
7936 append_metadata_update(st
, u
, sizeof(*u
));
7938 struct imsm_dev
*dev
;
7941 dev
= get_imsm_dev(super
, vol
);
7942 memset(dev
->volume
, '\0', MAX_RAID_SERIAL_LEN
);
7943 namelen
= min((int)strlen(name
), MAX_RAID_SERIAL_LEN
);
7944 memcpy(dev
->volume
, name
, namelen
);
7945 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
7946 dev
= get_imsm_dev(super
, i
);
7947 handle_missing(super
, dev
);
7949 super
->updates_pending
++;
7951 } else if (get_rwh_policy_from_update(update
) != -1) {
7954 int vol
= strtoul(subarray
, &ep
, 10);
7956 if (*ep
!= '\0' || vol
>= super
->anchor
->num_raid_devs
)
7959 new_policy
= get_rwh_policy_from_update(update
);
7961 if (st
->update_tail
) {
7962 struct imsm_update_rwh_policy
*u
= xmalloc(sizeof(*u
));
7964 u
->type
= update_rwh_policy
;
7966 u
->new_policy
= new_policy
;
7967 append_metadata_update(st
, u
, sizeof(*u
));
7969 struct imsm_dev
*dev
;
7971 dev
= get_imsm_dev(super
, vol
);
7972 dev
->rwh_policy
= new_policy
;
7973 super
->updates_pending
++;
7975 if (new_policy
== RWH_BITMAP
)
7976 return write_init_bitmap_imsm_vol(st
, vol
);
7983 static bool is_gen_migration(struct imsm_dev
*dev
)
7985 if (dev
&& dev
->vol
.migr_state
&&
7986 migr_type(dev
) == MIGR_GEN_MIGR
)
7992 static int is_rebuilding(struct imsm_dev
*dev
)
7994 struct imsm_map
*migr_map
;
7996 if (!dev
->vol
.migr_state
)
7999 if (migr_type(dev
) != MIGR_REBUILD
)
8002 migr_map
= get_imsm_map(dev
, MAP_1
);
8004 if (migr_map
->map_state
== IMSM_T_STATE_DEGRADED
)
8010 static int is_initializing(struct imsm_dev
*dev
)
8012 struct imsm_map
*migr_map
;
8014 if (!dev
->vol
.migr_state
)
8017 if (migr_type(dev
) != MIGR_INIT
)
8020 migr_map
= get_imsm_map(dev
, MAP_1
);
8022 if (migr_map
->map_state
== IMSM_T_STATE_UNINITIALIZED
)
8028 static void update_recovery_start(struct intel_super
*super
,
8029 struct imsm_dev
*dev
,
8030 struct mdinfo
*array
)
8032 struct mdinfo
*rebuild
= NULL
;
8036 if (!is_rebuilding(dev
))
8039 /* Find the rebuild target, but punt on the dual rebuild case */
8040 for (d
= array
->devs
; d
; d
= d
->next
)
8041 if (d
->recovery_start
== 0) {
8048 /* (?) none of the disks are marked with
8049 * IMSM_ORD_REBUILD, so assume they are missing and the
8050 * disk_ord_tbl was not correctly updated
8052 dprintf("failed to locate out-of-sync disk\n");
8056 units
= vol_curr_migr_unit(dev
);
8057 rebuild
->recovery_start
= units
* blocks_per_migr_unit(super
, dev
);
8060 static int recover_backup_imsm(struct supertype
*st
, struct mdinfo
*info
);
8062 static struct mdinfo
*container_content_imsm(struct supertype
*st
, char *subarray
)
8064 /* Given a container loaded by load_super_imsm_all,
8065 * extract information about all the arrays into
8067 * If 'subarray' is given, just extract info about that array.
8069 * For each imsm_dev create an mdinfo, fill it in,
8070 * then look for matching devices in super->disks
8071 * and create appropriate device mdinfo.
8073 struct intel_super
*super
= st
->sb
;
8074 struct imsm_super
*mpb
= super
->anchor
;
8075 struct mdinfo
*rest
= NULL
;
8079 int spare_disks
= 0;
8080 int current_vol
= super
->current_vol
;
8082 /* do not assemble arrays when not all attributes are supported */
8083 if (imsm_check_attributes(mpb
->attributes
) == 0) {
8085 pr_err("Unsupported attributes in IMSM metadata.Arrays activation is blocked.\n");
8088 /* count spare devices, not used in maps
8090 for (d
= super
->disks
; d
; d
= d
->next
)
8094 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
8095 struct imsm_dev
*dev
;
8096 struct imsm_map
*map
;
8097 struct imsm_map
*map2
;
8098 struct mdinfo
*this;
8105 (i
!= strtoul(subarray
, &ep
, 10) || *ep
!= '\0'))
8108 dev
= get_imsm_dev(super
, i
);
8109 map
= get_imsm_map(dev
, MAP_0
);
8110 map2
= get_imsm_map(dev
, MAP_1
);
8111 level
= get_imsm_raid_level(map
);
8113 /* do not publish arrays that are in the middle of an
8114 * unsupported migration
8116 if (dev
->vol
.migr_state
&&
8117 (migr_type(dev
) == MIGR_STATE_CHANGE
)) {
8118 pr_err("cannot assemble volume '%.16s': unsupported migration in progress\n",
8122 /* do not publish arrays that are not support by controller's
8126 this = xmalloc(sizeof(*this));
8128 super
->current_vol
= i
;
8129 getinfo_super_imsm_volume(st
, this, NULL
);
8131 chunk
= __le16_to_cpu(map
->blocks_per_strip
) >> 1;
8132 /* mdadm does not support all metadata features- set the bit in all arrays state */
8133 if (!validate_geometry_imsm_orom(super
,
8134 level
, /* RAID level */
8135 imsm_level_to_layout(level
),
8136 map
->num_members
, /* raid disks */
8137 &chunk
, imsm_dev_size(dev
),
8139 pr_err("IMSM RAID geometry validation failed. Array %s activation is blocked.\n",
8141 this->array
.state
|=
8142 (1<<MD_SB_BLOCK_CONTAINER_RESHAPE
) |
8143 (1<<MD_SB_BLOCK_VOLUME
);
8146 /* if array has bad blocks, set suitable bit in all arrays state */
8148 this->array
.state
|=
8149 (1<<MD_SB_BLOCK_CONTAINER_RESHAPE
) |
8150 (1<<MD_SB_BLOCK_VOLUME
);
8152 for (slot
= 0 ; slot
< map
->num_members
; slot
++) {
8153 unsigned long long recovery_start
;
8154 struct mdinfo
*info_d
;
8162 idx
= get_imsm_disk_idx(dev
, slot
, MAP_0
);
8163 ord
= get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
);
8164 for (d
= super
->disks
; d
; d
= d
->next
)
8165 if (d
->index
== idx
)
8168 recovery_start
= MaxSector
;
8171 if (d
&& is_failed(&d
->disk
))
8173 if (!skip
&& (ord
& IMSM_ORD_REBUILD
))
8175 if (!(ord
& IMSM_ORD_REBUILD
))
8176 this->array
.working_disks
++;
8178 * if we skip some disks the array will be assmebled degraded;
8179 * reset resync start to avoid a dirty-degraded
8180 * situation when performing the intial sync
8185 if (!(dev
->vol
.dirty
& RAIDVOL_DIRTY
)) {
8186 if ((!able_to_resync(level
, missing
) ||
8187 recovery_start
== 0))
8188 this->resync_start
= MaxSector
;
8194 info_d
= xcalloc(1, sizeof(*info_d
));
8195 info_d
->next
= this->devs
;
8196 this->devs
= info_d
;
8198 info_d
->disk
.number
= d
->index
;
8199 info_d
->disk
.major
= d
->major
;
8200 info_d
->disk
.minor
= d
->minor
;
8201 info_d
->disk
.raid_disk
= slot
;
8202 info_d
->recovery_start
= recovery_start
;
8204 if (slot
< map2
->num_members
)
8205 info_d
->disk
.state
= (1 << MD_DISK_ACTIVE
);
8207 this->array
.spare_disks
++;
8209 if (slot
< map
->num_members
)
8210 info_d
->disk
.state
= (1 << MD_DISK_ACTIVE
);
8212 this->array
.spare_disks
++;
8215 info_d
->events
= __le32_to_cpu(mpb
->generation_num
);
8216 info_d
->data_offset
= pba_of_lba0(map
);
8217 info_d
->component_size
= calc_component_size(map
, dev
);
8219 if (map
->raid_level
== 5) {
8220 info_d
->ppl_sector
= this->ppl_sector
;
8221 info_d
->ppl_size
= this->ppl_size
;
8222 if (this->consistency_policy
== CONSISTENCY_POLICY_PPL
&&
8223 recovery_start
== 0)
8224 this->resync_start
= 0;
8227 info_d
->bb
.supported
= 1;
8228 get_volume_badblocks(super
->bbm_log
, ord_to_idx(ord
),
8229 info_d
->data_offset
,
8230 info_d
->component_size
,
8233 /* now that the disk list is up-to-date fixup recovery_start */
8234 update_recovery_start(super
, dev
, this);
8235 this->array
.spare_disks
+= spare_disks
;
8237 /* check for reshape */
8238 if (this->reshape_active
== 1)
8239 recover_backup_imsm(st
, this);
8243 super
->current_vol
= current_vol
;
8247 static __u8
imsm_check_degraded(struct intel_super
*super
, struct imsm_dev
*dev
,
8248 int failed
, int look_in_map
)
8250 struct imsm_map
*map
;
8252 map
= get_imsm_map(dev
, look_in_map
);
8255 return map
->map_state
== IMSM_T_STATE_UNINITIALIZED
?
8256 IMSM_T_STATE_UNINITIALIZED
: IMSM_T_STATE_NORMAL
;
8258 switch (get_imsm_raid_level(map
)) {
8260 return IMSM_T_STATE_FAILED
;
8263 if (failed
< map
->num_members
)
8264 return IMSM_T_STATE_DEGRADED
;
8266 return IMSM_T_STATE_FAILED
;
8271 * check to see if any mirrors have failed, otherwise we
8272 * are degraded. Even numbered slots are mirrored on
8276 /* gcc -Os complains that this is unused */
8277 int insync
= insync
;
8279 for (i
= 0; i
< map
->num_members
; i
++) {
8280 __u32 ord
= get_imsm_ord_tbl_ent(dev
, i
, MAP_X
);
8281 int idx
= ord_to_idx(ord
);
8282 struct imsm_disk
*disk
;
8284 /* reset the potential in-sync count on even-numbered
8285 * slots. num_copies is always 2 for imsm raid10
8290 disk
= get_imsm_disk(super
, idx
);
8291 if (!disk
|| is_failed(disk
) || ord
& IMSM_ORD_REBUILD
)
8294 /* no in-sync disks left in this mirror the
8298 return IMSM_T_STATE_FAILED
;
8301 return IMSM_T_STATE_DEGRADED
;
8305 return IMSM_T_STATE_DEGRADED
;
8307 return IMSM_T_STATE_FAILED
;
8313 return map
->map_state
;
8316 static int imsm_count_failed(struct intel_super
*super
, struct imsm_dev
*dev
,
8321 struct imsm_disk
*disk
;
8322 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8323 struct imsm_map
*prev
= get_imsm_map(dev
, MAP_1
);
8324 struct imsm_map
*map_for_loop
;
8329 /* at the beginning of migration we set IMSM_ORD_REBUILD on
8330 * disks that are being rebuilt. New failures are recorded to
8331 * map[0]. So we look through all the disks we started with and
8332 * see if any failures are still present, or if any new ones
8336 if (prev
&& (map
->num_members
< prev
->num_members
))
8337 map_for_loop
= prev
;
8339 for (i
= 0; i
< map_for_loop
->num_members
; i
++) {
8341 /* when MAP_X is passed both maps failures are counted
8344 (look_in_map
== MAP_1
|| look_in_map
== MAP_X
) &&
8345 i
< prev
->num_members
) {
8346 ord
= __le32_to_cpu(prev
->disk_ord_tbl
[i
]);
8347 idx_1
= ord_to_idx(ord
);
8349 disk
= get_imsm_disk(super
, idx_1
);
8350 if (!disk
|| is_failed(disk
) || ord
& IMSM_ORD_REBUILD
)
8353 if ((look_in_map
== MAP_0
|| look_in_map
== MAP_X
) &&
8354 i
< map
->num_members
) {
8355 ord
= __le32_to_cpu(map
->disk_ord_tbl
[i
]);
8356 idx
= ord_to_idx(ord
);
8359 disk
= get_imsm_disk(super
, idx
);
8360 if (!disk
|| is_failed(disk
) ||
8361 ord
& IMSM_ORD_REBUILD
)
8370 static int imsm_open_new(struct supertype
*c
, struct active_array
*a
,
8373 struct intel_super
*super
= c
->sb
;
8374 struct imsm_super
*mpb
= super
->anchor
;
8375 struct imsm_update_prealloc_bb_mem u
;
8377 if (inst
>= mpb
->num_raid_devs
) {
8378 pr_err("subarry index %d, out of range\n", inst
);
8382 dprintf("imsm: open_new %d\n", inst
);
8383 a
->info
.container_member
= inst
;
8385 u
.type
= update_prealloc_badblocks_mem
;
8386 imsm_update_metadata_locally(c
, &u
, sizeof(u
));
8391 static int is_resyncing(struct imsm_dev
*dev
)
8393 struct imsm_map
*migr_map
;
8395 if (!dev
->vol
.migr_state
)
8398 if (migr_type(dev
) == MIGR_INIT
||
8399 migr_type(dev
) == MIGR_REPAIR
)
8402 if (migr_type(dev
) == MIGR_GEN_MIGR
)
8405 migr_map
= get_imsm_map(dev
, MAP_1
);
8407 if (migr_map
->map_state
== IMSM_T_STATE_NORMAL
&&
8408 dev
->vol
.migr_type
!= MIGR_GEN_MIGR
)
8414 /* return true if we recorded new information */
8415 static int mark_failure(struct intel_super
*super
,
8416 struct imsm_dev
*dev
, struct imsm_disk
*disk
, int idx
)
8420 struct imsm_map
*map
;
8421 char buf
[MAX_RAID_SERIAL_LEN
+3];
8422 unsigned int len
, shift
= 0;
8424 /* new failures are always set in map[0] */
8425 map
= get_imsm_map(dev
, MAP_0
);
8427 slot
= get_imsm_disk_slot(map
, idx
);
8431 ord
= __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
8432 if (is_failed(disk
) && (ord
& IMSM_ORD_REBUILD
))
8435 memcpy(buf
, disk
->serial
, MAX_RAID_SERIAL_LEN
);
8436 buf
[MAX_RAID_SERIAL_LEN
] = '\000';
8438 if ((len
= strlen(buf
)) >= MAX_RAID_SERIAL_LEN
)
8439 shift
= len
- MAX_RAID_SERIAL_LEN
+ 1;
8440 memcpy(disk
->serial
, &buf
[shift
], len
+ 1 - shift
);
8442 disk
->status
|= FAILED_DISK
;
8443 set_imsm_ord_tbl_ent(map
, slot
, idx
| IMSM_ORD_REBUILD
);
8444 /* mark failures in second map if second map exists and this disk
8446 * This is valid for migration, initialization and rebuild
8448 if (dev
->vol
.migr_state
) {
8449 struct imsm_map
*map2
= get_imsm_map(dev
, MAP_1
);
8450 int slot2
= get_imsm_disk_slot(map2
, idx
);
8452 if (slot2
< map2
->num_members
&& slot2
>= 0)
8453 set_imsm_ord_tbl_ent(map2
, slot2
,
8454 idx
| IMSM_ORD_REBUILD
);
8456 if (map
->failed_disk_num
== 0xff ||
8457 (!is_rebuilding(dev
) && map
->failed_disk_num
> slot
))
8458 map
->failed_disk_num
= slot
;
8460 clear_disk_badblocks(super
->bbm_log
, ord_to_idx(ord
));
8465 static void mark_missing(struct intel_super
*super
,
8466 struct imsm_dev
*dev
, struct imsm_disk
*disk
, int idx
)
8468 mark_failure(super
, dev
, disk
, idx
);
8470 if (disk
->scsi_id
== __cpu_to_le32(~(__u32
)0))
8473 disk
->scsi_id
= __cpu_to_le32(~(__u32
)0);
8474 memmove(&disk
->serial
[0], &disk
->serial
[1], MAX_RAID_SERIAL_LEN
- 1);
8477 static void handle_missing(struct intel_super
*super
, struct imsm_dev
*dev
)
8481 if (!super
->missing
)
8484 /* When orom adds replacement for missing disk it does
8485 * not remove entry of missing disk, but just updates map with
8486 * new added disk. So it is not enough just to test if there is
8487 * any missing disk, we have to look if there are any failed disks
8488 * in map to stop migration */
8490 dprintf("imsm: mark missing\n");
8491 /* end process for initialization and rebuild only
8493 if (is_gen_migration(dev
) == false) {
8494 int failed
= imsm_count_failed(super
, dev
, MAP_0
);
8498 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8499 struct imsm_map
*map1
;
8500 int i
, ord
, ord_map1
;
8503 for (i
= 0; i
< map
->num_members
; i
++) {
8504 ord
= get_imsm_ord_tbl_ent(dev
, i
, MAP_0
);
8505 if (!(ord
& IMSM_ORD_REBUILD
))
8508 map1
= get_imsm_map(dev
, MAP_1
);
8512 ord_map1
= __le32_to_cpu(map1
->disk_ord_tbl
[i
]);
8513 if (ord_map1
& IMSM_ORD_REBUILD
)
8518 map_state
= imsm_check_degraded(super
, dev
,
8520 end_migration(dev
, super
, map_state
);
8524 for (dl
= super
->missing
; dl
; dl
= dl
->next
)
8525 mark_missing(super
, dev
, &dl
->disk
, dl
->index
);
8526 super
->updates_pending
++;
8529 static unsigned long long imsm_set_array_size(struct imsm_dev
*dev
,
8532 unsigned long long array_blocks
;
8533 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8534 int used_disks
= imsm_num_data_members(map
);
8536 if (used_disks
== 0) {
8537 /* when problems occures
8538 * return current array_blocks value
8540 array_blocks
= imsm_dev_size(dev
);
8542 return array_blocks
;
8545 /* set array size in metadata
8548 /* OLCE size change is caused by added disks
8550 array_blocks
= per_dev_array_size(map
) * used_disks
;
8552 /* Online Volume Size Change
8553 * Using available free space
8555 array_blocks
= new_size
;
8557 array_blocks
= round_size_to_mb(array_blocks
, used_disks
);
8558 set_imsm_dev_size(dev
, array_blocks
);
8560 return array_blocks
;
8563 static void imsm_set_disk(struct active_array
*a
, int n
, int state
);
8565 static void imsm_progress_container_reshape(struct intel_super
*super
)
8567 /* if no device has a migr_state, but some device has a
8568 * different number of members than the previous device, start
8569 * changing the number of devices in this device to match
8572 struct imsm_super
*mpb
= super
->anchor
;
8573 int prev_disks
= -1;
8577 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
8578 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
8579 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8580 struct imsm_map
*map2
;
8581 int prev_num_members
;
8583 if (dev
->vol
.migr_state
)
8586 if (prev_disks
== -1)
8587 prev_disks
= map
->num_members
;
8588 if (prev_disks
== map
->num_members
)
8591 /* OK, this array needs to enter reshape mode.
8592 * i.e it needs a migr_state
8595 copy_map_size
= sizeof_imsm_map(map
);
8596 prev_num_members
= map
->num_members
;
8597 map
->num_members
= prev_disks
;
8598 dev
->vol
.migr_state
= 1;
8599 set_vol_curr_migr_unit(dev
, 0);
8600 set_migr_type(dev
, MIGR_GEN_MIGR
);
8601 for (i
= prev_num_members
;
8602 i
< map
->num_members
; i
++)
8603 set_imsm_ord_tbl_ent(map
, i
, i
);
8604 map2
= get_imsm_map(dev
, MAP_1
);
8605 /* Copy the current map */
8606 memcpy(map2
, map
, copy_map_size
);
8607 map2
->num_members
= prev_num_members
;
8609 imsm_set_array_size(dev
, -1);
8610 super
->clean_migration_record_by_mdmon
= 1;
8611 super
->updates_pending
++;
8615 /* Handle dirty -> clean transititions, resync and reshape. Degraded and rebuild
8616 * states are handled in imsm_set_disk() with one exception, when a
8617 * resync is stopped due to a new failure this routine will set the
8618 * 'degraded' state for the array.
8620 static int imsm_set_array_state(struct active_array
*a
, int consistent
)
8622 int inst
= a
->info
.container_member
;
8623 struct intel_super
*super
= a
->container
->sb
;
8624 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
8625 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8626 int failed
= imsm_count_failed(super
, dev
, MAP_0
);
8627 __u8 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
8628 __u32 blocks_per_unit
;
8630 if (dev
->vol
.migr_state
&&
8631 dev
->vol
.migr_type
== MIGR_GEN_MIGR
) {
8632 /* array state change is blocked due to reshape action
8634 * - abort the reshape (if last_checkpoint is 0 and action!= reshape)
8635 * - finish the reshape (if last_checkpoint is big and action != reshape)
8636 * - update vol_curr_migr_unit
8638 if (a
->curr_action
== reshape
) {
8639 /* still reshaping, maybe update vol_curr_migr_unit */
8640 goto mark_checkpoint
;
8642 if (a
->last_checkpoint
== 0 && a
->prev_action
== reshape
) {
8643 /* for some reason we aborted the reshape.
8645 * disable automatic metadata rollback
8646 * user action is required to recover process
8649 struct imsm_map
*map2
=
8650 get_imsm_map(dev
, MAP_1
);
8651 dev
->vol
.migr_state
= 0;
8652 set_migr_type(dev
, 0);
8653 set_vol_curr_migr_unit(dev
, 0);
8655 sizeof_imsm_map(map2
));
8656 super
->updates_pending
++;
8659 if (a
->last_checkpoint
>= a
->info
.component_size
) {
8660 unsigned long long array_blocks
;
8664 used_disks
= imsm_num_data_members(map
);
8665 if (used_disks
> 0) {
8667 per_dev_array_size(map
) *
8670 round_size_to_mb(array_blocks
,
8672 a
->info
.custom_array_size
= array_blocks
;
8673 /* encourage manager to update array
8677 a
->check_reshape
= 1;
8679 /* finalize online capacity expansion/reshape */
8680 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
)
8682 mdi
->disk
.raid_disk
,
8685 imsm_progress_container_reshape(super
);
8690 /* before we activate this array handle any missing disks */
8691 if (consistent
== 2)
8692 handle_missing(super
, dev
);
8694 if (consistent
== 2 &&
8695 (!is_resync_complete(&a
->info
) ||
8696 map_state
!= IMSM_T_STATE_NORMAL
||
8697 dev
->vol
.migr_state
))
8700 if (is_resync_complete(&a
->info
)) {
8701 /* complete intialization / resync,
8702 * recovery and interrupted recovery is completed in
8705 if (is_resyncing(dev
)) {
8706 dprintf("imsm: mark resync done\n");
8707 end_migration(dev
, super
, map_state
);
8708 super
->updates_pending
++;
8709 a
->last_checkpoint
= 0;
8711 } else if ((!is_resyncing(dev
) && !failed
) &&
8712 (imsm_reshape_blocks_arrays_changes(super
) == 0)) {
8713 /* mark the start of the init process if nothing is failed */
8714 dprintf("imsm: mark resync start\n");
8715 if (map
->map_state
== IMSM_T_STATE_UNINITIALIZED
)
8716 migrate(dev
, super
, IMSM_T_STATE_NORMAL
, MIGR_INIT
);
8718 migrate(dev
, super
, IMSM_T_STATE_NORMAL
, MIGR_REPAIR
);
8719 super
->updates_pending
++;
8723 /* skip checkpointing for general migration,
8724 * it is controlled in mdadm
8726 if (is_gen_migration(dev
))
8727 goto skip_mark_checkpoint
;
8729 /* check if we can update vol_curr_migr_unit from resync_start,
8732 blocks_per_unit
= blocks_per_migr_unit(super
, dev
);
8733 if (blocks_per_unit
) {
8734 set_vol_curr_migr_unit(dev
,
8735 a
->last_checkpoint
/ blocks_per_unit
);
8736 dprintf("imsm: mark checkpoint (%llu)\n",
8737 vol_curr_migr_unit(dev
));
8738 super
->updates_pending
++;
8741 skip_mark_checkpoint
:
8742 /* mark dirty / clean */
8743 if (((dev
->vol
.dirty
& RAIDVOL_DIRTY
) && consistent
) ||
8744 (!(dev
->vol
.dirty
& RAIDVOL_DIRTY
) && !consistent
)) {
8745 dprintf("imsm: mark '%s'\n", consistent
? "clean" : "dirty");
8747 dev
->vol
.dirty
= RAIDVOL_CLEAN
;
8749 dev
->vol
.dirty
= RAIDVOL_DIRTY
;
8750 if (dev
->rwh_policy
== RWH_DISTRIBUTED
||
8751 dev
->rwh_policy
== RWH_MULTIPLE_DISTRIBUTED
)
8752 dev
->vol
.dirty
|= RAIDVOL_DSRECORD_VALID
;
8754 super
->updates_pending
++;
8760 static int imsm_disk_slot_to_ord(struct active_array
*a
, int slot
)
8762 int inst
= a
->info
.container_member
;
8763 struct intel_super
*super
= a
->container
->sb
;
8764 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
8765 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8767 if (slot
> map
->num_members
) {
8768 pr_err("imsm: imsm_disk_slot_to_ord %d out of range 0..%d\n",
8769 slot
, map
->num_members
- 1);
8776 return get_imsm_ord_tbl_ent(dev
, slot
, MAP_0
);
8779 static void imsm_set_disk(struct active_array
*a
, int n
, int state
)
8781 int inst
= a
->info
.container_member
;
8782 struct intel_super
*super
= a
->container
->sb
;
8783 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
8784 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8785 struct imsm_disk
*disk
;
8787 int recovery_not_finished
= 0;
8791 int rebuild_done
= 0;
8794 ord
= get_imsm_ord_tbl_ent(dev
, n
, MAP_X
);
8798 dprintf("imsm: set_disk %d:%x\n", n
, state
);
8799 disk
= get_imsm_disk(super
, ord_to_idx(ord
));
8801 /* check for new failures */
8802 if (disk
&& (state
& DS_FAULTY
)) {
8803 if (mark_failure(super
, dev
, disk
, ord_to_idx(ord
)))
8804 super
->updates_pending
++;
8807 /* check if in_sync */
8808 if (state
& DS_INSYNC
&& ord
& IMSM_ORD_REBUILD
&& is_rebuilding(dev
)) {
8809 struct imsm_map
*migr_map
= get_imsm_map(dev
, MAP_1
);
8811 set_imsm_ord_tbl_ent(migr_map
, n
, ord_to_idx(ord
));
8813 super
->updates_pending
++;
8816 failed
= imsm_count_failed(super
, dev
, MAP_0
);
8817 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
8819 /* check if recovery complete, newly degraded, or failed */
8820 dprintf("imsm: Detected transition to state ");
8821 switch (map_state
) {
8822 case IMSM_T_STATE_NORMAL
: /* transition to normal state */
8823 dprintf("normal: ");
8824 if (is_rebuilding(dev
)) {
8825 dprintf_cont("while rebuilding");
8826 /* check if recovery is really finished */
8827 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
)
8828 if (mdi
->recovery_start
!= MaxSector
) {
8829 recovery_not_finished
= 1;
8832 if (recovery_not_finished
) {
8834 dprintf("Rebuild has not finished yet, state not changed");
8835 if (a
->last_checkpoint
< mdi
->recovery_start
) {
8836 a
->last_checkpoint
= mdi
->recovery_start
;
8837 super
->updates_pending
++;
8841 end_migration(dev
, super
, map_state
);
8842 map
->failed_disk_num
= ~0;
8843 super
->updates_pending
++;
8844 a
->last_checkpoint
= 0;
8847 if (is_gen_migration(dev
)) {
8848 dprintf_cont("while general migration");
8849 if (a
->last_checkpoint
>= a
->info
.component_size
)
8850 end_migration(dev
, super
, map_state
);
8852 map
->map_state
= map_state
;
8853 map
->failed_disk_num
= ~0;
8854 super
->updates_pending
++;
8858 case IMSM_T_STATE_DEGRADED
: /* transition to degraded state */
8859 dprintf_cont("degraded: ");
8860 if (map
->map_state
!= map_state
&& !dev
->vol
.migr_state
) {
8861 dprintf_cont("mark degraded");
8862 map
->map_state
= map_state
;
8863 super
->updates_pending
++;
8864 a
->last_checkpoint
= 0;
8867 if (is_rebuilding(dev
)) {
8868 dprintf_cont("while rebuilding ");
8869 if (state
& DS_FAULTY
) {
8870 dprintf_cont("removing failed drive ");
8871 if (n
== map
->failed_disk_num
) {
8872 dprintf_cont("end migration");
8873 end_migration(dev
, super
, map_state
);
8874 a
->last_checkpoint
= 0;
8876 dprintf_cont("fail detected during rebuild, changing map state");
8877 map
->map_state
= map_state
;
8879 super
->updates_pending
++;
8885 /* check if recovery is really finished */
8886 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
)
8887 if (mdi
->recovery_start
!= MaxSector
) {
8888 recovery_not_finished
= 1;
8891 if (recovery_not_finished
) {
8893 dprintf_cont("Rebuild has not finished yet");
8894 if (a
->last_checkpoint
< mdi
->recovery_start
) {
8895 a
->last_checkpoint
=
8896 mdi
->recovery_start
;
8897 super
->updates_pending
++;
8902 dprintf_cont(" Rebuild done, still degraded");
8903 end_migration(dev
, super
, map_state
);
8904 a
->last_checkpoint
= 0;
8905 super
->updates_pending
++;
8907 for (i
= 0; i
< map
->num_members
; i
++) {
8908 int idx
= get_imsm_ord_tbl_ent(dev
, i
, MAP_0
);
8910 if (idx
& IMSM_ORD_REBUILD
)
8911 map
->failed_disk_num
= i
;
8913 super
->updates_pending
++;
8916 if (is_gen_migration(dev
)) {
8917 dprintf_cont("while general migration");
8918 if (a
->last_checkpoint
>= a
->info
.component_size
)
8919 end_migration(dev
, super
, map_state
);
8921 map
->map_state
= map_state
;
8922 manage_second_map(super
, dev
);
8924 super
->updates_pending
++;
8927 if (is_initializing(dev
)) {
8928 dprintf_cont("while initialization.");
8929 map
->map_state
= map_state
;
8930 super
->updates_pending
++;
8934 case IMSM_T_STATE_FAILED
: /* transition to failed state */
8935 dprintf_cont("failed: ");
8936 if (is_gen_migration(dev
)) {
8937 dprintf_cont("while general migration");
8938 map
->map_state
= map_state
;
8939 super
->updates_pending
++;
8942 if (map
->map_state
!= map_state
) {
8943 dprintf_cont("mark failed");
8944 end_migration(dev
, super
, map_state
);
8945 super
->updates_pending
++;
8946 a
->last_checkpoint
= 0;
8951 dprintf_cont("state %i\n", map_state
);
8956 static int store_imsm_mpb(int fd
, struct imsm_super
*mpb
)
8959 __u32 mpb_size
= __le32_to_cpu(mpb
->mpb_size
);
8960 unsigned long long dsize
;
8961 unsigned long long sectors
;
8962 unsigned int sector_size
;
8964 if (!get_dev_sector_size(fd
, NULL
, §or_size
))
8966 get_dev_size(fd
, NULL
, &dsize
);
8968 if (mpb_size
> sector_size
) {
8969 /* -1 to account for anchor */
8970 sectors
= mpb_sectors(mpb
, sector_size
) - 1;
8972 /* write the extended mpb to the sectors preceeding the anchor */
8973 if (lseek64(fd
, dsize
- (sector_size
* (2 + sectors
)),
8977 if ((unsigned long long)write(fd
, buf
+ sector_size
,
8978 sector_size
* sectors
) != sector_size
* sectors
)
8982 /* first block is stored on second to last sector of the disk */
8983 if (lseek64(fd
, dsize
- (sector_size
* 2), SEEK_SET
) < 0)
8986 if ((unsigned int)write(fd
, buf
, sector_size
) != sector_size
)
8992 static void imsm_sync_metadata(struct supertype
*container
)
8994 struct intel_super
*super
= container
->sb
;
8996 dprintf("sync metadata: %d\n", super
->updates_pending
);
8997 if (!super
->updates_pending
)
9000 write_super_imsm(container
, 0);
9002 super
->updates_pending
= 0;
9005 static struct dl
*imsm_readd(struct intel_super
*super
, int idx
, struct active_array
*a
)
9007 struct imsm_dev
*dev
= get_imsm_dev(super
, a
->info
.container_member
);
9008 int i
= get_imsm_disk_idx(dev
, idx
, MAP_X
);
9011 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
9015 if (dl
&& is_failed(&dl
->disk
))
9019 dprintf("found %x:%x\n", dl
->major
, dl
->minor
);
9024 static struct dl
*imsm_add_spare(struct intel_super
*super
, int slot
,
9025 struct active_array
*a
, int activate_new
,
9026 struct mdinfo
*additional_test_list
)
9028 struct imsm_dev
*dev
= get_imsm_dev(super
, a
->info
.container_member
);
9029 int idx
= get_imsm_disk_idx(dev
, slot
, MAP_X
);
9030 struct imsm_super
*mpb
= super
->anchor
;
9031 struct imsm_map
*map
;
9032 unsigned long long pos
;
9037 __u32 array_start
= 0;
9038 __u32 array_end
= 0;
9040 struct mdinfo
*test_list
;
9042 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
9043 /* If in this array, skip */
9044 for (d
= a
->info
.devs
; d
; d
= d
->next
)
9045 if (is_fd_valid(d
->state_fd
) &&
9046 d
->disk
.major
== dl
->major
&&
9047 d
->disk
.minor
== dl
->minor
) {
9048 dprintf("%x:%x already in array\n",
9049 dl
->major
, dl
->minor
);
9054 test_list
= additional_test_list
;
9056 if (test_list
->disk
.major
== dl
->major
&&
9057 test_list
->disk
.minor
== dl
->minor
) {
9058 dprintf("%x:%x already in additional test list\n",
9059 dl
->major
, dl
->minor
);
9062 test_list
= test_list
->next
;
9067 /* skip in use or failed drives */
9068 if (is_failed(&dl
->disk
) || idx
== dl
->index
||
9070 dprintf("%x:%x status (failed: %d index: %d)\n",
9071 dl
->major
, dl
->minor
, is_failed(&dl
->disk
), idx
);
9075 /* skip pure spares when we are looking for partially
9076 * assimilated drives
9078 if (dl
->index
== -1 && !activate_new
)
9081 if (!drive_validate_sector_size(super
, dl
))
9084 /* Does this unused device have the requisite free space?
9085 * It needs to be able to cover all member volumes
9087 ex
= get_extents(super
, dl
, 1);
9089 dprintf("cannot get extents\n");
9092 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
9093 dev
= get_imsm_dev(super
, i
);
9094 map
= get_imsm_map(dev
, MAP_0
);
9096 /* check if this disk is already a member of
9099 if (get_imsm_disk_slot(map
, dl
->index
) >= 0)
9105 array_start
= pba_of_lba0(map
);
9106 array_end
= array_start
+
9107 per_dev_array_size(map
) - 1;
9110 /* check that we can start at pba_of_lba0 with
9111 * num_data_stripes*blocks_per_stripe of space
9113 if (array_start
>= pos
&& array_end
< ex
[j
].start
) {
9117 pos
= ex
[j
].start
+ ex
[j
].size
;
9119 } while (ex
[j
-1].size
);
9126 if (i
< mpb
->num_raid_devs
) {
9127 dprintf("%x:%x does not have %u to %u available\n",
9128 dl
->major
, dl
->minor
, array_start
, array_end
);
9138 static int imsm_rebuild_allowed(struct supertype
*cont
, int dev_idx
, int failed
)
9140 struct imsm_dev
*dev2
;
9141 struct imsm_map
*map
;
9147 dev2
= get_imsm_dev(cont
->sb
, dev_idx
);
9149 state
= imsm_check_degraded(cont
->sb
, dev2
, failed
, MAP_0
);
9150 if (state
== IMSM_T_STATE_FAILED
) {
9151 map
= get_imsm_map(dev2
, MAP_0
);
9152 for (slot
= 0; slot
< map
->num_members
; slot
++) {
9154 * Check if failed disks are deleted from intel
9155 * disk list or are marked to be deleted
9157 idx
= get_imsm_disk_idx(dev2
, slot
, MAP_X
);
9158 idisk
= get_imsm_dl_disk(cont
->sb
, idx
);
9160 * Do not rebuild the array if failed disks
9161 * from failed sub-array are not removed from
9165 is_failed(&idisk
->disk
) &&
9166 (idisk
->action
!= DISK_REMOVE
))
9173 static struct mdinfo
*imsm_activate_spare(struct active_array
*a
,
9174 struct metadata_update
**updates
)
9177 * Find a device with unused free space and use it to replace a
9178 * failed/vacant region in an array. We replace failed regions one a
9179 * array at a time. The result is that a new spare disk will be added
9180 * to the first failed array and after the monitor has finished
9181 * propagating failures the remainder will be consumed.
9183 * FIXME add a capability for mdmon to request spares from another
9187 struct intel_super
*super
= a
->container
->sb
;
9188 int inst
= a
->info
.container_member
;
9189 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
9190 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
9191 int failed
= a
->info
.array
.raid_disks
;
9192 struct mdinfo
*rv
= NULL
;
9195 struct metadata_update
*mu
;
9197 struct imsm_update_activate_spare
*u
;
9202 for (d
= a
->info
.devs
; d
; d
= d
->next
) {
9203 if (!is_fd_valid(d
->state_fd
))
9206 if (d
->curr_state
& DS_FAULTY
)
9207 /* wait for Removal to happen */
9213 dprintf("imsm: activate spare: inst=%d failed=%d (%d) level=%d\n",
9214 inst
, failed
, a
->info
.array
.raid_disks
, a
->info
.array
.level
);
9216 if (imsm_reshape_blocks_arrays_changes(super
))
9219 /* Cannot activate another spare if rebuild is in progress already
9221 if (is_rebuilding(dev
)) {
9222 dprintf("imsm: No spare activation allowed. Rebuild in progress already.\n");
9226 if (a
->info
.array
.level
== 4)
9227 /* No repair for takeovered array
9228 * imsm doesn't support raid4
9232 if (imsm_check_degraded(super
, dev
, failed
, MAP_0
) !=
9233 IMSM_T_STATE_DEGRADED
)
9236 if (get_imsm_map(dev
, MAP_0
)->map_state
== IMSM_T_STATE_UNINITIALIZED
) {
9237 dprintf("imsm: No spare activation allowed. Volume is not initialized.\n");
9242 * If there are any failed disks check state of the other volume.
9243 * Block rebuild if the another one is failed until failed disks
9244 * are removed from container.
9247 dprintf("found failed disks in %.*s, check if there anotherfailed sub-array.\n",
9248 MAX_RAID_SERIAL_LEN
, dev
->volume
);
9249 /* check if states of the other volumes allow for rebuild */
9250 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
9252 allowed
= imsm_rebuild_allowed(a
->container
,
9260 /* For each slot, if it is not working, find a spare */
9261 for (i
= 0; i
< a
->info
.array
.raid_disks
; i
++) {
9262 for (d
= a
->info
.devs
; d
; d
= d
->next
)
9263 if (d
->disk
.raid_disk
== i
)
9265 dprintf("found %d: %p %x\n", i
, d
, d
?d
->curr_state
:0);
9266 if (d
&& is_fd_valid(d
->state_fd
))
9270 * OK, this device needs recovery. Try to re-add the
9271 * previous occupant of this slot, if this fails see if
9272 * we can continue the assimilation of a spare that was
9273 * partially assimilated, finally try to activate a new
9276 dl
= imsm_readd(super
, i
, a
);
9278 dl
= imsm_add_spare(super
, i
, a
, 0, rv
);
9280 dl
= imsm_add_spare(super
, i
, a
, 1, rv
);
9284 /* found a usable disk with enough space */
9285 di
= xcalloc(1, sizeof(*di
));
9287 /* dl->index will be -1 in the case we are activating a
9288 * pristine spare. imsm_process_update() will create a
9289 * new index in this case. Once a disk is found to be
9290 * failed in all member arrays it is kicked from the
9293 di
->disk
.number
= dl
->index
;
9295 /* (ab)use di->devs to store a pointer to the device
9298 di
->devs
= (struct mdinfo
*) dl
;
9300 di
->disk
.raid_disk
= i
;
9301 di
->disk
.major
= dl
->major
;
9302 di
->disk
.minor
= dl
->minor
;
9304 di
->recovery_start
= 0;
9305 di
->data_offset
= pba_of_lba0(map
);
9306 di
->component_size
= a
->info
.component_size
;
9307 di
->container_member
= inst
;
9308 di
->bb
.supported
= 1;
9309 if (a
->info
.consistency_policy
== CONSISTENCY_POLICY_PPL
) {
9310 di
->ppl_sector
= get_ppl_sector(super
, inst
);
9311 di
->ppl_size
= MULTIPLE_PPL_AREA_SIZE_IMSM
>> 9;
9313 super
->random
= random32();
9317 dprintf("%x:%x to be %d at %llu\n", dl
->major
, dl
->minor
,
9318 i
, di
->data_offset
);
9322 /* No spares found */
9324 /* Now 'rv' has a list of devices to return.
9325 * Create a metadata_update record to update the
9326 * disk_ord_tbl for the array
9328 mu
= xmalloc(sizeof(*mu
));
9329 mu
->buf
= xcalloc(num_spares
,
9330 sizeof(struct imsm_update_activate_spare
));
9332 mu
->space_list
= NULL
;
9333 mu
->len
= sizeof(struct imsm_update_activate_spare
) * num_spares
;
9334 mu
->next
= *updates
;
9335 u
= (struct imsm_update_activate_spare
*) mu
->buf
;
9337 for (di
= rv
; di
; di
= di
->next
) {
9338 u
->type
= update_activate_spare
;
9339 u
->dl
= (struct dl
*) di
->devs
;
9341 u
->slot
= di
->disk
.raid_disk
;
9352 static int disks_overlap(struct intel_super
*super
, int idx
, struct imsm_update_create_array
*u
)
9354 struct imsm_dev
*dev
= get_imsm_dev(super
, idx
);
9355 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
9356 struct imsm_map
*new_map
= get_imsm_map(&u
->dev
, MAP_0
);
9357 struct disk_info
*inf
= get_disk_info(u
);
9358 struct imsm_disk
*disk
;
9362 for (i
= 0; i
< map
->num_members
; i
++) {
9363 disk
= get_imsm_disk(super
, get_imsm_disk_idx(dev
, i
, MAP_X
));
9364 for (j
= 0; j
< new_map
->num_members
; j
++)
9365 if (serialcmp(disk
->serial
, inf
[j
].serial
) == 0)
9372 static struct dl
*get_disk_super(struct intel_super
*super
, int major
, int minor
)
9376 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
9377 if (dl
->major
== major
&& dl
->minor
== minor
)
9382 static int remove_disk_super(struct intel_super
*super
, int major
, int minor
)
9388 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
9389 if (dl
->major
== major
&& dl
->minor
== minor
) {
9392 prev
->next
= dl
->next
;
9394 super
->disks
= dl
->next
;
9396 __free_imsm_disk(dl
, 1);
9397 dprintf("removed %x:%x\n", major
, minor
);
9405 static void imsm_delete(struct intel_super
*super
, struct dl
**dlp
, unsigned index
);
9407 static int add_remove_disk_update(struct intel_super
*super
)
9409 int check_degraded
= 0;
9412 /* add/remove some spares to/from the metadata/contrainer */
9413 while (super
->disk_mgmt_list
) {
9414 struct dl
*disk_cfg
;
9416 disk_cfg
= super
->disk_mgmt_list
;
9417 super
->disk_mgmt_list
= disk_cfg
->next
;
9418 disk_cfg
->next
= NULL
;
9420 if (disk_cfg
->action
== DISK_ADD
) {
9421 disk_cfg
->next
= super
->disks
;
9422 super
->disks
= disk_cfg
;
9424 dprintf("added %x:%x\n",
9425 disk_cfg
->major
, disk_cfg
->minor
);
9426 } else if (disk_cfg
->action
== DISK_REMOVE
) {
9427 dprintf("Disk remove action processed: %x.%x\n",
9428 disk_cfg
->major
, disk_cfg
->minor
);
9429 disk
= get_disk_super(super
,
9433 /* store action status */
9434 disk
->action
= DISK_REMOVE
;
9435 /* remove spare disks only */
9436 if (disk
->index
== -1) {
9437 remove_disk_super(super
,
9441 disk_cfg
->fd
= disk
->fd
;
9445 /* release allocate disk structure */
9446 __free_imsm_disk(disk_cfg
, 1);
9449 return check_degraded
;
9452 static int apply_reshape_migration_update(struct imsm_update_reshape_migration
*u
,
9453 struct intel_super
*super
,
9456 struct intel_dev
*id
;
9457 void **tofree
= NULL
;
9460 dprintf("(enter)\n");
9461 if (u
->subdev
< 0 || u
->subdev
> 1) {
9462 dprintf("imsm: Error: Wrong subdev: %i\n", u
->subdev
);
9465 if (space_list
== NULL
|| *space_list
== NULL
) {
9466 dprintf("imsm: Error: Memory is not allocated\n");
9470 for (id
= super
->devlist
; id
; id
= id
->next
) {
9471 if (id
->index
== (unsigned)u
->subdev
) {
9472 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->subdev
);
9473 struct imsm_map
*map
;
9474 struct imsm_dev
*new_dev
=
9475 (struct imsm_dev
*)*space_list
;
9476 struct imsm_map
*migr_map
= get_imsm_map(dev
, MAP_1
);
9478 struct dl
*new_disk
;
9480 if (new_dev
== NULL
)
9482 *space_list
= **space_list
;
9483 memcpy(new_dev
, dev
, sizeof_imsm_dev(dev
, 0));
9484 map
= get_imsm_map(new_dev
, MAP_0
);
9486 dprintf("imsm: Error: migration in progress");
9490 to_state
= map
->map_state
;
9491 if ((u
->new_level
== 5) && (map
->raid_level
== 0)) {
9493 /* this should not happen */
9494 if (u
->new_disks
[0] < 0) {
9495 map
->failed_disk_num
=
9496 map
->num_members
- 1;
9497 to_state
= IMSM_T_STATE_DEGRADED
;
9499 to_state
= IMSM_T_STATE_NORMAL
;
9501 migrate(new_dev
, super
, to_state
, MIGR_GEN_MIGR
);
9502 if (u
->new_level
> -1)
9503 map
->raid_level
= u
->new_level
;
9504 migr_map
= get_imsm_map(new_dev
, MAP_1
);
9505 if ((u
->new_level
== 5) &&
9506 (migr_map
->raid_level
== 0)) {
9507 int ord
= map
->num_members
- 1;
9508 migr_map
->num_members
--;
9509 if (u
->new_disks
[0] < 0)
9510 ord
|= IMSM_ORD_REBUILD
;
9511 set_imsm_ord_tbl_ent(map
,
9512 map
->num_members
- 1,
9516 tofree
= (void **)dev
;
9518 /* update chunk size
9520 if (u
->new_chunksize
> 0) {
9521 struct imsm_map
*dest_map
=
9522 get_imsm_map(dev
, MAP_0
);
9524 imsm_num_data_members(dest_map
);
9526 if (used_disks
== 0)
9529 map
->blocks_per_strip
=
9530 __cpu_to_le16(u
->new_chunksize
* 2);
9531 update_num_data_stripes(map
, imsm_dev_size(dev
));
9534 /* ensure blocks_per_member has valid value
9536 set_blocks_per_member(map
,
9537 per_dev_array_size(map
) +
9538 NUM_BLOCKS_DIRTY_STRIPE_REGION
);
9542 if (u
->new_level
!= 5 || migr_map
->raid_level
!= 0 ||
9543 migr_map
->raid_level
== map
->raid_level
)
9546 if (u
->new_disks
[0] >= 0) {
9549 new_disk
= get_disk_super(super
,
9550 major(u
->new_disks
[0]),
9551 minor(u
->new_disks
[0]));
9552 dprintf("imsm: new disk for reshape is: %i:%i (%p, index = %i)\n",
9553 major(u
->new_disks
[0]),
9554 minor(u
->new_disks
[0]),
9555 new_disk
, new_disk
->index
);
9556 if (new_disk
== NULL
)
9557 goto error_disk_add
;
9559 new_disk
->index
= map
->num_members
- 1;
9560 /* slot to fill in autolayout
9562 new_disk
->raiddisk
= new_disk
->index
;
9563 new_disk
->disk
.status
|= CONFIGURED_DISK
;
9564 new_disk
->disk
.status
&= ~SPARE_DISK
;
9566 goto error_disk_add
;
9569 *tofree
= *space_list
;
9570 /* calculate new size
9572 imsm_set_array_size(new_dev
, -1);
9579 *space_list
= tofree
;
9583 dprintf("Error: imsm: Cannot find disk.\n");
9587 static int apply_size_change_update(struct imsm_update_size_change
*u
,
9588 struct intel_super
*super
)
9590 struct intel_dev
*id
;
9593 dprintf("(enter)\n");
9594 if (u
->subdev
< 0 || u
->subdev
> 1) {
9595 dprintf("imsm: Error: Wrong subdev: %i\n", u
->subdev
);
9599 for (id
= super
->devlist
; id
; id
= id
->next
) {
9600 if (id
->index
== (unsigned)u
->subdev
) {
9601 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->subdev
);
9602 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
9603 int used_disks
= imsm_num_data_members(map
);
9604 unsigned long long blocks_per_member
;
9605 unsigned long long new_size_per_disk
;
9607 if (used_disks
== 0)
9610 /* calculate new size
9612 new_size_per_disk
= u
->new_size
/ used_disks
;
9613 blocks_per_member
= new_size_per_disk
+
9614 NUM_BLOCKS_DIRTY_STRIPE_REGION
;
9616 imsm_set_array_size(dev
, u
->new_size
);
9617 set_blocks_per_member(map
, blocks_per_member
);
9618 update_num_data_stripes(map
, u
->new_size
);
9627 static int prepare_spare_to_activate(struct supertype
*st
,
9628 struct imsm_update_activate_spare
*u
)
9630 struct intel_super
*super
= st
->sb
;
9631 int prev_current_vol
= super
->current_vol
;
9632 struct active_array
*a
;
9635 for (a
= st
->arrays
; a
; a
= a
->next
)
9637 * Additional initialization (adding bitmap header, filling
9638 * the bitmap area with '1's to force initial rebuild for a whole
9639 * data-area) is required when adding the spare to the volume
9640 * with write-intent bitmap.
9642 if (a
->info
.container_member
== u
->array
&&
9643 a
->info
.consistency_policy
== CONSISTENCY_POLICY_BITMAP
) {
9646 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
9652 super
->current_vol
= u
->array
;
9653 if (st
->ss
->write_bitmap(st
, dl
->fd
, NoUpdate
))
9655 super
->current_vol
= prev_current_vol
;
9660 static int apply_update_activate_spare(struct imsm_update_activate_spare
*u
,
9661 struct intel_super
*super
,
9662 struct active_array
*active_array
)
9664 struct imsm_super
*mpb
= super
->anchor
;
9665 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->array
);
9666 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
9667 struct imsm_map
*migr_map
;
9668 struct active_array
*a
;
9669 struct imsm_disk
*disk
;
9676 int second_map_created
= 0;
9678 for (; u
; u
= u
->next
) {
9679 victim
= get_imsm_disk_idx(dev
, u
->slot
, MAP_X
);
9684 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
9689 pr_err("error: imsm_activate_spare passed an unknown disk (index: %d)\n",
9694 /* count failures (excluding rebuilds and the victim)
9695 * to determine map[0] state
9698 for (i
= 0; i
< map
->num_members
; i
++) {
9701 disk
= get_imsm_disk(super
,
9702 get_imsm_disk_idx(dev
, i
, MAP_X
));
9703 if (!disk
|| is_failed(disk
))
9707 /* adding a pristine spare, assign a new index */
9708 if (dl
->index
< 0) {
9709 dl
->index
= super
->anchor
->num_disks
;
9710 super
->anchor
->num_disks
++;
9713 disk
->status
|= CONFIGURED_DISK
;
9714 disk
->status
&= ~SPARE_DISK
;
9717 to_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
9718 if (!second_map_created
) {
9719 second_map_created
= 1;
9720 map
->map_state
= IMSM_T_STATE_DEGRADED
;
9721 migrate(dev
, super
, to_state
, MIGR_REBUILD
);
9723 map
->map_state
= to_state
;
9724 migr_map
= get_imsm_map(dev
, MAP_1
);
9725 set_imsm_ord_tbl_ent(map
, u
->slot
, dl
->index
);
9726 set_imsm_ord_tbl_ent(migr_map
, u
->slot
,
9727 dl
->index
| IMSM_ORD_REBUILD
);
9729 /* update the family_num to mark a new container
9730 * generation, being careful to record the existing
9731 * family_num in orig_family_num to clean up after
9732 * earlier mdadm versions that neglected to set it.
9734 if (mpb
->orig_family_num
== 0)
9735 mpb
->orig_family_num
= mpb
->family_num
;
9736 mpb
->family_num
+= super
->random
;
9738 /* count arrays using the victim in the metadata */
9740 for (a
= active_array
; a
; a
= a
->next
) {
9741 int dev_idx
= a
->info
.container_member
;
9743 if (get_disk_slot_in_dev(super
, dev_idx
, victim
) >= 0)
9747 /* delete the victim if it is no longer being
9753 /* We know that 'manager' isn't touching anything,
9754 * so it is safe to delete
9756 for (dlp
= &super
->disks
; *dlp
; dlp
= &(*dlp
)->next
)
9757 if ((*dlp
)->index
== victim
)
9760 /* victim may be on the missing list */
9762 for (dlp
= &super
->missing
; *dlp
;
9763 dlp
= &(*dlp
)->next
)
9764 if ((*dlp
)->index
== victim
)
9766 imsm_delete(super
, dlp
, victim
);
9773 static int apply_reshape_container_disks_update(struct imsm_update_reshape
*u
,
9774 struct intel_super
*super
,
9777 struct dl
*new_disk
;
9778 struct intel_dev
*id
;
9780 int delta_disks
= u
->new_raid_disks
- u
->old_raid_disks
;
9781 int disk_count
= u
->old_raid_disks
;
9782 void **tofree
= NULL
;
9783 int devices_to_reshape
= 1;
9784 struct imsm_super
*mpb
= super
->anchor
;
9786 unsigned int dev_id
;
9788 dprintf("(enter)\n");
9790 /* enable spares to use in array */
9791 for (i
= 0; i
< delta_disks
; i
++) {
9792 new_disk
= get_disk_super(super
,
9793 major(u
->new_disks
[i
]),
9794 minor(u
->new_disks
[i
]));
9795 dprintf("imsm: new disk for reshape is: %i:%i (%p, index = %i)\n",
9796 major(u
->new_disks
[i
]), minor(u
->new_disks
[i
]),
9797 new_disk
, new_disk
->index
);
9798 if (new_disk
== NULL
||
9799 (new_disk
->index
>= 0 &&
9800 new_disk
->index
< u
->old_raid_disks
))
9801 goto update_reshape_exit
;
9802 new_disk
->index
= disk_count
++;
9803 /* slot to fill in autolayout
9805 new_disk
->raiddisk
= new_disk
->index
;
9806 new_disk
->disk
.status
|=
9808 new_disk
->disk
.status
&= ~SPARE_DISK
;
9811 dprintf("imsm: volume set mpb->num_raid_devs = %i\n",
9812 mpb
->num_raid_devs
);
9813 /* manage changes in volume
9815 for (dev_id
= 0; dev_id
< mpb
->num_raid_devs
; dev_id
++) {
9816 void **sp
= *space_list
;
9817 struct imsm_dev
*newdev
;
9818 struct imsm_map
*newmap
, *oldmap
;
9820 for (id
= super
->devlist
; id
; id
= id
->next
) {
9821 if (id
->index
== dev_id
)
9830 /* Copy the dev, but not (all of) the map */
9831 memcpy(newdev
, id
->dev
, sizeof(*newdev
));
9832 oldmap
= get_imsm_map(id
->dev
, MAP_0
);
9833 newmap
= get_imsm_map(newdev
, MAP_0
);
9834 /* Copy the current map */
9835 memcpy(newmap
, oldmap
, sizeof_imsm_map(oldmap
));
9836 /* update one device only
9838 if (devices_to_reshape
) {
9839 dprintf("imsm: modifying subdev: %i\n",
9841 devices_to_reshape
--;
9842 newdev
->vol
.migr_state
= 1;
9843 set_vol_curr_migr_unit(newdev
, 0);
9844 set_migr_type(newdev
, MIGR_GEN_MIGR
);
9845 newmap
->num_members
= u
->new_raid_disks
;
9846 for (i
= 0; i
< delta_disks
; i
++) {
9847 set_imsm_ord_tbl_ent(newmap
,
9848 u
->old_raid_disks
+ i
,
9849 u
->old_raid_disks
+ i
);
9851 /* New map is correct, now need to save old map
9853 newmap
= get_imsm_map(newdev
, MAP_1
);
9854 memcpy(newmap
, oldmap
, sizeof_imsm_map(oldmap
));
9856 imsm_set_array_size(newdev
, -1);
9859 sp
= (void **)id
->dev
;
9864 /* Clear migration record */
9865 memset(super
->migr_rec
, 0, sizeof(struct migr_record
));
9868 *space_list
= tofree
;
9871 update_reshape_exit
:
9876 static int apply_takeover_update(struct imsm_update_takeover
*u
,
9877 struct intel_super
*super
,
9880 struct imsm_dev
*dev
= NULL
;
9881 struct intel_dev
*dv
;
9882 struct imsm_dev
*dev_new
;
9883 struct imsm_map
*map
;
9887 for (dv
= super
->devlist
; dv
; dv
= dv
->next
)
9888 if (dv
->index
== (unsigned int)u
->subarray
) {
9896 map
= get_imsm_map(dev
, MAP_0
);
9898 if (u
->direction
== R10_TO_R0
) {
9899 /* Number of failed disks must be half of initial disk number */
9900 if (imsm_count_failed(super
, dev
, MAP_0
) !=
9901 (map
->num_members
/ 2))
9904 /* iterate through devices to mark removed disks as spare */
9905 for (dm
= super
->disks
; dm
; dm
= dm
->next
) {
9906 if (dm
->disk
.status
& FAILED_DISK
) {
9907 int idx
= dm
->index
;
9908 /* update indexes on the disk list */
9909 /* FIXME this loop-with-the-loop looks wrong, I'm not convinced
9910 the index values will end up being correct.... NB */
9911 for (du
= super
->disks
; du
; du
= du
->next
)
9912 if (du
->index
> idx
)
9914 /* mark as spare disk */
9919 map
->num_members
/= map
->num_domains
;
9920 map
->map_state
= IMSM_T_STATE_NORMAL
;
9921 map
->raid_level
= 0;
9922 set_num_domains(map
);
9923 update_num_data_stripes(map
, imsm_dev_size(dev
));
9924 map
->failed_disk_num
= -1;
9927 if (u
->direction
== R0_TO_R10
) {
9930 /* update slots in current disk list */
9931 for (dm
= super
->disks
; dm
; dm
= dm
->next
) {
9935 /* create new *missing* disks */
9936 for (i
= 0; i
< map
->num_members
; i
++) {
9937 space
= *space_list
;
9940 *space_list
= *space
;
9942 memcpy(du
, super
->disks
, sizeof(*du
));
9946 du
->index
= (i
* 2) + 1;
9947 sprintf((char *)du
->disk
.serial
,
9948 " MISSING_%d", du
->index
);
9949 sprintf((char *)du
->serial
,
9950 "MISSING_%d", du
->index
);
9951 du
->next
= super
->missing
;
9952 super
->missing
= du
;
9954 /* create new dev and map */
9955 space
= *space_list
;
9958 *space_list
= *space
;
9959 dev_new
= (void *)space
;
9960 memcpy(dev_new
, dev
, sizeof(*dev
));
9961 /* update new map */
9962 map
= get_imsm_map(dev_new
, MAP_0
);
9964 map
->map_state
= IMSM_T_STATE_DEGRADED
;
9965 map
->raid_level
= 1;
9966 set_num_domains(map
);
9967 map
->num_members
= map
->num_members
* map
->num_domains
;
9968 update_num_data_stripes(map
, imsm_dev_size(dev
));
9970 /* replace dev<->dev_new */
9973 /* update disk order table */
9974 for (du
= super
->disks
; du
; du
= du
->next
)
9976 set_imsm_ord_tbl_ent(map
, du
->index
, du
->index
);
9977 for (du
= super
->missing
; du
; du
= du
->next
)
9978 if (du
->index
>= 0) {
9979 set_imsm_ord_tbl_ent(map
, du
->index
, du
->index
);
9980 mark_missing(super
, dv
->dev
, &du
->disk
, du
->index
);
9986 static void imsm_process_update(struct supertype
*st
,
9987 struct metadata_update
*update
)
9990 * crack open the metadata_update envelope to find the update record
9991 * update can be one of:
9992 * update_reshape_container_disks - all the arrays in the container
9993 * are being reshaped to have more devices. We need to mark
9994 * the arrays for general migration and convert selected spares
9995 * into active devices.
9996 * update_activate_spare - a spare device has replaced a failed
9997 * device in an array, update the disk_ord_tbl. If this disk is
9998 * present in all member arrays then also clear the SPARE_DISK
10000 * update_create_array
10001 * update_kill_array
10002 * update_rename_array
10003 * update_add_remove_disk
10005 struct intel_super
*super
= st
->sb
;
10006 struct imsm_super
*mpb
;
10007 enum imsm_update_type type
= *(enum imsm_update_type
*) update
->buf
;
10009 /* update requires a larger buf but the allocation failed */
10010 if (super
->next_len
&& !super
->next_buf
) {
10011 super
->next_len
= 0;
10015 if (super
->next_buf
) {
10016 memcpy(super
->next_buf
, super
->buf
, super
->len
);
10018 super
->len
= super
->next_len
;
10019 super
->buf
= super
->next_buf
;
10021 super
->next_len
= 0;
10022 super
->next_buf
= NULL
;
10025 mpb
= super
->anchor
;
10028 case update_general_migration_checkpoint
: {
10029 struct intel_dev
*id
;
10030 struct imsm_update_general_migration_checkpoint
*u
=
10031 (void *)update
->buf
;
10033 dprintf("called for update_general_migration_checkpoint\n");
10035 /* find device under general migration */
10036 for (id
= super
->devlist
; id
; id
= id
->next
) {
10037 if (is_gen_migration(id
->dev
)) {
10038 set_vol_curr_migr_unit(id
->dev
,
10039 u
->curr_migr_unit
);
10040 super
->updates_pending
++;
10045 case update_takeover
: {
10046 struct imsm_update_takeover
*u
= (void *)update
->buf
;
10047 if (apply_takeover_update(u
, super
, &update
->space_list
)) {
10048 imsm_update_version_info(super
);
10049 super
->updates_pending
++;
10054 case update_reshape_container_disks
: {
10055 struct imsm_update_reshape
*u
= (void *)update
->buf
;
10056 if (apply_reshape_container_disks_update(
10057 u
, super
, &update
->space_list
))
10058 super
->updates_pending
++;
10061 case update_reshape_migration
: {
10062 struct imsm_update_reshape_migration
*u
= (void *)update
->buf
;
10063 if (apply_reshape_migration_update(
10064 u
, super
, &update
->space_list
))
10065 super
->updates_pending
++;
10068 case update_size_change
: {
10069 struct imsm_update_size_change
*u
= (void *)update
->buf
;
10070 if (apply_size_change_update(u
, super
))
10071 super
->updates_pending
++;
10074 case update_activate_spare
: {
10075 struct imsm_update_activate_spare
*u
= (void *) update
->buf
;
10077 if (prepare_spare_to_activate(st
, u
) &&
10078 apply_update_activate_spare(u
, super
, st
->arrays
))
10079 super
->updates_pending
++;
10082 case update_create_array
: {
10083 /* someone wants to create a new array, we need to be aware of
10084 * a few races/collisions:
10085 * 1/ 'Create' called by two separate instances of mdadm
10086 * 2/ 'Create' versus 'activate_spare': mdadm has chosen
10087 * devices that have since been assimilated via
10089 * In the event this update can not be carried out mdadm will
10090 * (FIX ME) notice that its update did not take hold.
10092 struct imsm_update_create_array
*u
= (void *) update
->buf
;
10093 struct intel_dev
*dv
;
10094 struct imsm_dev
*dev
;
10095 struct imsm_map
*map
, *new_map
;
10096 unsigned long long start
, end
;
10097 unsigned long long new_start
, new_end
;
10099 struct disk_info
*inf
;
10102 /* handle racing creates: first come first serve */
10103 if (u
->dev_idx
< mpb
->num_raid_devs
) {
10104 dprintf("subarray %d already defined\n", u
->dev_idx
);
10108 /* check update is next in sequence */
10109 if (u
->dev_idx
!= mpb
->num_raid_devs
) {
10110 dprintf("can not create array %d expected index %d\n",
10111 u
->dev_idx
, mpb
->num_raid_devs
);
10115 new_map
= get_imsm_map(&u
->dev
, MAP_0
);
10116 new_start
= pba_of_lba0(new_map
);
10117 new_end
= new_start
+ per_dev_array_size(new_map
);
10118 inf
= get_disk_info(u
);
10120 /* handle activate_spare versus create race:
10121 * check to make sure that overlapping arrays do not include
10122 * overalpping disks
10124 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
10125 dev
= get_imsm_dev(super
, i
);
10126 map
= get_imsm_map(dev
, MAP_0
);
10127 start
= pba_of_lba0(map
);
10128 end
= start
+ per_dev_array_size(map
);
10129 if ((new_start
>= start
&& new_start
<= end
) ||
10130 (start
>= new_start
&& start
<= new_end
))
10135 if (disks_overlap(super
, i
, u
)) {
10136 dprintf("arrays overlap\n");
10141 /* check that prepare update was successful */
10142 if (!update
->space
) {
10143 dprintf("prepare update failed\n");
10147 /* check that all disks are still active before committing
10148 * changes. FIXME: could we instead handle this by creating a
10149 * degraded array? That's probably not what the user expects,
10150 * so better to drop this update on the floor.
10152 for (i
= 0; i
< new_map
->num_members
; i
++) {
10153 dl
= serial_to_dl(inf
[i
].serial
, super
);
10155 dprintf("disk disappeared\n");
10160 super
->updates_pending
++;
10162 /* convert spares to members and fixup ord_tbl */
10163 for (i
= 0; i
< new_map
->num_members
; i
++) {
10164 dl
= serial_to_dl(inf
[i
].serial
, super
);
10165 if (dl
->index
== -1) {
10166 dl
->index
= mpb
->num_disks
;
10168 dl
->disk
.status
|= CONFIGURED_DISK
;
10169 dl
->disk
.status
&= ~SPARE_DISK
;
10171 set_imsm_ord_tbl_ent(new_map
, i
, dl
->index
);
10174 dv
= update
->space
;
10176 update
->space
= NULL
;
10177 imsm_copy_dev(dev
, &u
->dev
);
10178 dv
->index
= u
->dev_idx
;
10179 dv
->next
= super
->devlist
;
10180 super
->devlist
= dv
;
10181 mpb
->num_raid_devs
++;
10183 imsm_update_version_info(super
);
10186 /* mdmon knows how to release update->space, but not
10187 * ((struct intel_dev *) update->space)->dev
10189 if (update
->space
) {
10190 dv
= update
->space
;
10195 case update_kill_array
: {
10196 struct imsm_update_kill_array
*u
= (void *) update
->buf
;
10197 int victim
= u
->dev_idx
;
10198 struct active_array
*a
;
10199 struct intel_dev
**dp
;
10201 /* sanity check that we are not affecting the uuid of
10202 * active arrays, or deleting an active array
10204 * FIXME when immutable ids are available, but note that
10205 * we'll also need to fixup the invalidated/active
10206 * subarray indexes in mdstat
10208 for (a
= st
->arrays
; a
; a
= a
->next
)
10209 if (a
->info
.container_member
>= victim
)
10211 /* by definition if mdmon is running at least one array
10212 * is active in the container, so checking
10213 * mpb->num_raid_devs is just extra paranoia
10215 if (a
|| mpb
->num_raid_devs
== 1 || victim
>= super
->anchor
->num_raid_devs
) {
10216 dprintf("failed to delete subarray-%d\n", victim
);
10220 for (dp
= &super
->devlist
; *dp
;)
10221 if ((*dp
)->index
== (unsigned)super
->current_vol
) {
10224 if ((*dp
)->index
> (unsigned)victim
)
10228 mpb
->num_raid_devs
--;
10229 super
->updates_pending
++;
10232 case update_rename_array
: {
10233 struct imsm_update_rename_array
*u
= (void *) update
->buf
;
10234 char name
[MAX_RAID_SERIAL_LEN
+1];
10235 int target
= u
->dev_idx
;
10236 struct active_array
*a
;
10237 struct imsm_dev
*dev
;
10239 /* sanity check that we are not affecting the uuid of
10242 memset(name
, 0, sizeof(name
));
10243 snprintf(name
, MAX_RAID_SERIAL_LEN
, "%s", (char *) u
->name
);
10244 name
[MAX_RAID_SERIAL_LEN
] = '\0';
10245 for (a
= st
->arrays
; a
; a
= a
->next
)
10246 if (a
->info
.container_member
== target
)
10248 dev
= get_imsm_dev(super
, u
->dev_idx
);
10249 if (a
|| !check_name(super
, name
, 1)) {
10250 dprintf("failed to rename subarray-%d\n", target
);
10254 memcpy(dev
->volume
, name
, MAX_RAID_SERIAL_LEN
);
10255 super
->updates_pending
++;
10258 case update_add_remove_disk
: {
10259 /* we may be able to repair some arrays if disks are
10260 * being added, check the status of add_remove_disk
10261 * if discs has been added.
10263 if (add_remove_disk_update(super
)) {
10264 struct active_array
*a
;
10266 super
->updates_pending
++;
10267 for (a
= st
->arrays
; a
; a
= a
->next
)
10268 a
->check_degraded
= 1;
10272 case update_prealloc_badblocks_mem
:
10274 case update_rwh_policy
: {
10275 struct imsm_update_rwh_policy
*u
= (void *)update
->buf
;
10276 int target
= u
->dev_idx
;
10277 struct imsm_dev
*dev
= get_imsm_dev(super
, target
);
10279 if (dev
->rwh_policy
!= u
->new_policy
) {
10280 dev
->rwh_policy
= u
->new_policy
;
10281 super
->updates_pending
++;
10286 pr_err("error: unsupported process update type:(type: %d)\n", type
);
10290 static struct mdinfo
*get_spares_for_grow(struct supertype
*st
);
10292 static int imsm_prepare_update(struct supertype
*st
,
10293 struct metadata_update
*update
)
10296 * Allocate space to hold new disk entries, raid-device entries or a new
10297 * mpb if necessary. The manager synchronously waits for updates to
10298 * complete in the monitor, so new mpb buffers allocated here can be
10299 * integrated by the monitor thread without worrying about live pointers
10300 * in the manager thread.
10302 enum imsm_update_type type
;
10303 struct intel_super
*super
= st
->sb
;
10304 unsigned int sector_size
= super
->sector_size
;
10305 struct imsm_super
*mpb
= super
->anchor
;
10309 if (update
->len
< (int)sizeof(type
))
10312 type
= *(enum imsm_update_type
*) update
->buf
;
10315 case update_general_migration_checkpoint
:
10316 if (update
->len
< (int)sizeof(struct imsm_update_general_migration_checkpoint
))
10318 dprintf("called for update_general_migration_checkpoint\n");
10320 case update_takeover
: {
10321 struct imsm_update_takeover
*u
= (void *)update
->buf
;
10322 if (update
->len
< (int)sizeof(*u
))
10324 if (u
->direction
== R0_TO_R10
) {
10325 void **tail
= (void **)&update
->space_list
;
10326 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->subarray
);
10327 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
10328 int num_members
= map
->num_members
;
10331 /* allocate memory for added disks */
10332 for (i
= 0; i
< num_members
; i
++) {
10333 size
= sizeof(struct dl
);
10334 space
= xmalloc(size
);
10339 /* allocate memory for new device */
10340 size
= sizeof_imsm_dev(super
->devlist
->dev
, 0) +
10341 (num_members
* sizeof(__u32
));
10342 space
= xmalloc(size
);
10346 len
= disks_to_mpb_size(num_members
* 2);
10351 case update_reshape_container_disks
: {
10352 /* Every raid device in the container is about to
10353 * gain some more devices, and we will enter a
10355 * So each 'imsm_map' will be bigger, and the imsm_vol
10356 * will now hold 2 of them.
10357 * Thus we need new 'struct imsm_dev' allocations sized
10358 * as sizeof_imsm_dev but with more devices in both maps.
10360 struct imsm_update_reshape
*u
= (void *)update
->buf
;
10361 struct intel_dev
*dl
;
10362 void **space_tail
= (void**)&update
->space_list
;
10364 if (update
->len
< (int)sizeof(*u
))
10367 dprintf("for update_reshape\n");
10369 for (dl
= super
->devlist
; dl
; dl
= dl
->next
) {
10370 int size
= sizeof_imsm_dev(dl
->dev
, 1);
10372 if (u
->new_raid_disks
> u
->old_raid_disks
)
10373 size
+= sizeof(__u32
)*2*
10374 (u
->new_raid_disks
- u
->old_raid_disks
);
10378 *space_tail
= NULL
;
10381 len
= disks_to_mpb_size(u
->new_raid_disks
);
10382 dprintf("New anchor length is %llu\n", (unsigned long long)len
);
10385 case update_reshape_migration
: {
10386 /* for migration level 0->5 we need to add disks
10387 * so the same as for container operation we will copy
10388 * device to the bigger location.
10389 * in memory prepared device and new disk area are prepared
10390 * for usage in process update
10392 struct imsm_update_reshape_migration
*u
= (void *)update
->buf
;
10393 struct intel_dev
*id
;
10394 void **space_tail
= (void **)&update
->space_list
;
10397 int current_level
= -1;
10399 if (update
->len
< (int)sizeof(*u
))
10402 dprintf("for update_reshape\n");
10404 /* add space for bigger array in update
10406 for (id
= super
->devlist
; id
; id
= id
->next
) {
10407 if (id
->index
== (unsigned)u
->subdev
) {
10408 size
= sizeof_imsm_dev(id
->dev
, 1);
10409 if (u
->new_raid_disks
> u
->old_raid_disks
)
10410 size
+= sizeof(__u32
)*2*
10411 (u
->new_raid_disks
- u
->old_raid_disks
);
10415 *space_tail
= NULL
;
10419 if (update
->space_list
== NULL
)
10422 /* add space for disk in update
10424 size
= sizeof(struct dl
);
10428 *space_tail
= NULL
;
10430 /* add spare device to update
10432 for (id
= super
->devlist
; id
; id
= id
->next
)
10433 if (id
->index
== (unsigned)u
->subdev
) {
10434 struct imsm_dev
*dev
;
10435 struct imsm_map
*map
;
10437 dev
= get_imsm_dev(super
, u
->subdev
);
10438 map
= get_imsm_map(dev
, MAP_0
);
10439 current_level
= map
->raid_level
;
10442 if (u
->new_level
== 5 && u
->new_level
!= current_level
) {
10443 struct mdinfo
*spares
;
10445 spares
= get_spares_for_grow(st
);
10448 struct mdinfo
*dev
;
10450 dev
= spares
->devs
;
10453 makedev(dev
->disk
.major
,
10455 dl
= get_disk_super(super
,
10458 dl
->index
= u
->old_raid_disks
;
10461 sysfs_free(spares
);
10464 len
= disks_to_mpb_size(u
->new_raid_disks
);
10465 dprintf("New anchor length is %llu\n", (unsigned long long)len
);
10468 case update_size_change
: {
10469 if (update
->len
< (int)sizeof(struct imsm_update_size_change
))
10473 case update_activate_spare
: {
10474 if (update
->len
< (int)sizeof(struct imsm_update_activate_spare
))
10478 case update_create_array
: {
10479 struct imsm_update_create_array
*u
= (void *) update
->buf
;
10480 struct intel_dev
*dv
;
10481 struct imsm_dev
*dev
= &u
->dev
;
10482 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
10484 struct disk_info
*inf
;
10488 if (update
->len
< (int)sizeof(*u
))
10491 inf
= get_disk_info(u
);
10492 len
= sizeof_imsm_dev(dev
, 1);
10493 /* allocate a new super->devlist entry */
10494 dv
= xmalloc(sizeof(*dv
));
10495 dv
->dev
= xmalloc(len
);
10496 update
->space
= dv
;
10498 /* count how many spares will be converted to members */
10499 for (i
= 0; i
< map
->num_members
; i
++) {
10500 dl
= serial_to_dl(inf
[i
].serial
, super
);
10502 /* hmm maybe it failed?, nothing we can do about
10507 if (count_memberships(dl
, super
) == 0)
10510 len
+= activate
* sizeof(struct imsm_disk
);
10513 case update_kill_array
: {
10514 if (update
->len
< (int)sizeof(struct imsm_update_kill_array
))
10518 case update_rename_array
: {
10519 if (update
->len
< (int)sizeof(struct imsm_update_rename_array
))
10523 case update_add_remove_disk
:
10524 /* no update->len needed */
10526 case update_prealloc_badblocks_mem
:
10527 super
->extra_space
+= sizeof(struct bbm_log
) -
10528 get_imsm_bbm_log_size(super
->bbm_log
);
10530 case update_rwh_policy
: {
10531 if (update
->len
< (int)sizeof(struct imsm_update_rwh_policy
))
10539 /* check if we need a larger metadata buffer */
10540 if (super
->next_buf
)
10541 buf_len
= super
->next_len
;
10543 buf_len
= super
->len
;
10545 if (__le32_to_cpu(mpb
->mpb_size
) + super
->extra_space
+ len
> buf_len
) {
10546 /* ok we need a larger buf than what is currently allocated
10547 * if this allocation fails process_update will notice that
10548 * ->next_len is set and ->next_buf is NULL
10550 buf_len
= ROUND_UP(__le32_to_cpu(mpb
->mpb_size
) +
10551 super
->extra_space
+ len
, sector_size
);
10552 if (super
->next_buf
)
10553 free(super
->next_buf
);
10555 super
->next_len
= buf_len
;
10556 if (posix_memalign(&super
->next_buf
, sector_size
, buf_len
) == 0)
10557 memset(super
->next_buf
, 0, buf_len
);
10559 super
->next_buf
= NULL
;
10564 /* must be called while manager is quiesced */
10565 static void imsm_delete(struct intel_super
*super
, struct dl
**dlp
, unsigned index
)
10567 struct imsm_super
*mpb
= super
->anchor
;
10569 struct imsm_dev
*dev
;
10570 struct imsm_map
*map
;
10571 unsigned int i
, j
, num_members
;
10572 __u32 ord
, ord_map0
;
10573 struct bbm_log
*log
= super
->bbm_log
;
10575 dprintf("deleting device[%d] from imsm_super\n", index
);
10577 /* shift all indexes down one */
10578 for (iter
= super
->disks
; iter
; iter
= iter
->next
)
10579 if (iter
->index
> (int)index
)
10581 for (iter
= super
->missing
; iter
; iter
= iter
->next
)
10582 if (iter
->index
> (int)index
)
10585 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
10586 dev
= get_imsm_dev(super
, i
);
10587 map
= get_imsm_map(dev
, MAP_0
);
10588 num_members
= map
->num_members
;
10589 for (j
= 0; j
< num_members
; j
++) {
10590 /* update ord entries being careful not to propagate
10591 * ord-flags to the first map
10593 ord
= get_imsm_ord_tbl_ent(dev
, j
, MAP_X
);
10594 ord_map0
= get_imsm_ord_tbl_ent(dev
, j
, MAP_0
);
10596 if (ord_to_idx(ord
) <= index
)
10599 map
= get_imsm_map(dev
, MAP_0
);
10600 set_imsm_ord_tbl_ent(map
, j
, ord_map0
- 1);
10601 map
= get_imsm_map(dev
, MAP_1
);
10603 set_imsm_ord_tbl_ent(map
, j
, ord
- 1);
10607 for (i
= 0; i
< log
->entry_count
; i
++) {
10608 struct bbm_log_entry
*entry
= &log
->marked_block_entries
[i
];
10610 if (entry
->disk_ordinal
<= index
)
10612 entry
->disk_ordinal
--;
10616 super
->updates_pending
++;
10618 struct dl
*dl
= *dlp
;
10620 *dlp
= (*dlp
)->next
;
10621 __free_imsm_disk(dl
, 1);
10625 static int imsm_get_allowed_degradation(int level
, int raid_disks
,
10626 struct intel_super
*super
,
10627 struct imsm_dev
*dev
)
10633 struct imsm_map
*map
;
10636 ret_val
= raid_disks
/2;
10637 /* check map if all disks pairs not failed
10640 map
= get_imsm_map(dev
, MAP_0
);
10641 for (i
= 0; i
< ret_val
; i
++) {
10642 int degradation
= 0;
10643 if (get_imsm_disk(super
, i
) == NULL
)
10645 if (get_imsm_disk(super
, i
+ 1) == NULL
)
10647 if (degradation
== 2)
10650 map
= get_imsm_map(dev
, MAP_1
);
10651 /* if there is no second map
10652 * result can be returned
10656 /* check degradation in second map
10658 for (i
= 0; i
< ret_val
; i
++) {
10659 int degradation
= 0;
10660 if (get_imsm_disk(super
, i
) == NULL
)
10662 if (get_imsm_disk(super
, i
+ 1) == NULL
)
10664 if (degradation
== 2)
10678 /*******************************************************************************
10679 * Function: validate_container_imsm
10680 * Description: This routine validates container after assemble,
10681 * eg. if devices in container are under the same controller.
10684 * info : linked list with info about devices used in array
10688 ******************************************************************************/
10689 int validate_container_imsm(struct mdinfo
*info
)
10691 if (check_env("IMSM_NO_PLATFORM"))
10694 struct sys_dev
*idev
;
10695 struct sys_dev
*hba
= NULL
;
10696 struct sys_dev
*intel_devices
= find_intel_devices();
10697 char *dev_path
= devt_to_devpath(makedev(info
->disk
.major
,
10698 info
->disk
.minor
), 1, NULL
);
10700 for (idev
= intel_devices
; idev
; idev
= idev
->next
) {
10701 if (dev_path
&& strstr(dev_path
, idev
->path
)) {
10710 pr_err("WARNING - Cannot detect HBA for device %s!\n",
10711 devid2kname(makedev(info
->disk
.major
, info
->disk
.minor
)));
10715 const struct imsm_orom
*orom
= get_orom_by_device_id(hba
->dev_id
);
10716 struct mdinfo
*dev
;
10718 for (dev
= info
->next
; dev
; dev
= dev
->next
) {
10719 dev_path
= devt_to_devpath(makedev(dev
->disk
.major
,
10720 dev
->disk
.minor
), 1, NULL
);
10722 struct sys_dev
*hba2
= NULL
;
10723 for (idev
= intel_devices
; idev
; idev
= idev
->next
) {
10724 if (dev_path
&& strstr(dev_path
, idev
->path
)) {
10732 const struct imsm_orom
*orom2
= hba2
== NULL
? NULL
:
10733 get_orom_by_device_id(hba2
->dev_id
);
10735 if (hba2
&& hba
->type
!= hba2
->type
) {
10736 pr_err("WARNING - HBAs of devices do not match %s != %s\n",
10737 get_sys_dev_type(hba
->type
), get_sys_dev_type(hba2
->type
));
10741 if (orom
!= orom2
) {
10742 pr_err("WARNING - IMSM container assembled with disks under different HBAs!\n"
10743 " This operation is not supported and can lead to data loss.\n");
10748 pr_err("WARNING - IMSM container assembled with disks under HBAs without IMSM platform support!\n"
10749 " This operation is not supported and can lead to data loss.\n");
10757 /*******************************************************************************
10758 * Function: imsm_record_badblock
10759 * Description: This routine stores new bad block record in BBM log
10762 * a : array containing a bad block
10763 * slot : disk number containing a bad block
10764 * sector : bad block sector
10765 * length : bad block sectors range
10769 ******************************************************************************/
10770 static int imsm_record_badblock(struct active_array
*a
, int slot
,
10771 unsigned long long sector
, int length
)
10773 struct intel_super
*super
= a
->container
->sb
;
10777 ord
= imsm_disk_slot_to_ord(a
, slot
);
10781 ret
= record_new_badblock(super
->bbm_log
, ord_to_idx(ord
), sector
,
10784 super
->updates_pending
++;
10788 /*******************************************************************************
10789 * Function: imsm_clear_badblock
10790 * Description: This routine clears bad block record from BBM log
10793 * a : array containing a bad block
10794 * slot : disk number containing a bad block
10795 * sector : bad block sector
10796 * length : bad block sectors range
10800 ******************************************************************************/
10801 static int imsm_clear_badblock(struct active_array
*a
, int slot
,
10802 unsigned long long sector
, int length
)
10804 struct intel_super
*super
= a
->container
->sb
;
10808 ord
= imsm_disk_slot_to_ord(a
, slot
);
10812 ret
= clear_badblock(super
->bbm_log
, ord_to_idx(ord
), sector
, length
);
10814 super
->updates_pending
++;
10818 /*******************************************************************************
10819 * Function: imsm_get_badblocks
10820 * Description: This routine get list of bad blocks for an array
10824 * slot : disk number
10826 * bb : structure containing bad blocks
10828 ******************************************************************************/
10829 static struct md_bb
*imsm_get_badblocks(struct active_array
*a
, int slot
)
10831 int inst
= a
->info
.container_member
;
10832 struct intel_super
*super
= a
->container
->sb
;
10833 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
10834 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
10837 ord
= imsm_disk_slot_to_ord(a
, slot
);
10841 get_volume_badblocks(super
->bbm_log
, ord_to_idx(ord
), pba_of_lba0(map
),
10842 per_dev_array_size(map
), &super
->bb
);
10846 /*******************************************************************************
10847 * Function: examine_badblocks_imsm
10848 * Description: Prints list of bad blocks on a disk to the standard output
10851 * st : metadata handler
10852 * fd : open file descriptor for device
10853 * devname : device name
10857 ******************************************************************************/
10858 static int examine_badblocks_imsm(struct supertype
*st
, int fd
, char *devname
)
10860 struct intel_super
*super
= st
->sb
;
10861 struct bbm_log
*log
= super
->bbm_log
;
10862 struct dl
*d
= NULL
;
10865 for (d
= super
->disks
; d
; d
= d
->next
) {
10866 if (strcmp(d
->devname
, devname
) == 0)
10870 if ((d
== NULL
) || (d
->index
< 0)) { /* serial mismatch probably */
10871 pr_err("%s doesn't appear to be part of a raid array\n",
10878 struct bbm_log_entry
*entry
= &log
->marked_block_entries
[0];
10880 for (i
= 0; i
< log
->entry_count
; i
++) {
10881 if (entry
[i
].disk_ordinal
== d
->index
) {
10882 unsigned long long sector
= __le48_to_cpu(
10883 &entry
[i
].defective_block_start
);
10884 int cnt
= entry
[i
].marked_count
+ 1;
10887 printf("Bad-blocks on %s:\n", devname
);
10891 printf("%20llu for %d sectors\n", sector
, cnt
);
10897 printf("No bad-blocks list configured on %s\n", devname
);
10901 /*******************************************************************************
10902 * Function: init_migr_record_imsm
10903 * Description: Function inits imsm migration record
10905 * super : imsm internal array info
10906 * dev : device under migration
10907 * info : general array info to find the smallest device
10910 ******************************************************************************/
10911 void init_migr_record_imsm(struct supertype
*st
, struct imsm_dev
*dev
,
10912 struct mdinfo
*info
)
10914 struct intel_super
*super
= st
->sb
;
10915 struct migr_record
*migr_rec
= super
->migr_rec
;
10916 int new_data_disks
;
10917 unsigned long long dsize
, dev_sectors
;
10918 long long unsigned min_dev_sectors
= -1LLU;
10919 struct imsm_map
*map_dest
= get_imsm_map(dev
, MAP_0
);
10920 struct imsm_map
*map_src
= get_imsm_map(dev
, MAP_1
);
10921 unsigned long long num_migr_units
;
10922 unsigned long long array_blocks
;
10923 struct dl
*dl_disk
= NULL
;
10925 memset(migr_rec
, 0, sizeof(struct migr_record
));
10926 migr_rec
->family_num
= __cpu_to_le32(super
->anchor
->family_num
);
10928 /* only ascending reshape supported now */
10929 migr_rec
->ascending_migr
= __cpu_to_le32(1);
10931 migr_rec
->dest_depth_per_unit
= GEN_MIGR_AREA_SIZE
/
10932 max(map_dest
->blocks_per_strip
, map_src
->blocks_per_strip
);
10933 migr_rec
->dest_depth_per_unit
*=
10934 max(map_dest
->blocks_per_strip
, map_src
->blocks_per_strip
);
10935 new_data_disks
= imsm_num_data_members(map_dest
);
10936 migr_rec
->blocks_per_unit
=
10937 __cpu_to_le32(migr_rec
->dest_depth_per_unit
* new_data_disks
);
10938 migr_rec
->dest_depth_per_unit
=
10939 __cpu_to_le32(migr_rec
->dest_depth_per_unit
);
10940 array_blocks
= info
->component_size
* new_data_disks
;
10942 array_blocks
/ __le32_to_cpu(migr_rec
->blocks_per_unit
);
10944 if (array_blocks
% __le32_to_cpu(migr_rec
->blocks_per_unit
))
10946 set_num_migr_units(migr_rec
, num_migr_units
);
10948 migr_rec
->post_migr_vol_cap
= dev
->size_low
;
10949 migr_rec
->post_migr_vol_cap_hi
= dev
->size_high
;
10951 /* Find the smallest dev */
10952 for (dl_disk
= super
->disks
; dl_disk
; dl_disk
= dl_disk
->next
) {
10953 /* ignore spares in container */
10954 if (dl_disk
->index
< 0)
10956 get_dev_size(dl_disk
->fd
, NULL
, &dsize
);
10957 dev_sectors
= dsize
/ 512;
10958 if (dev_sectors
< min_dev_sectors
)
10959 min_dev_sectors
= dev_sectors
;
10961 set_migr_chkp_area_pba(migr_rec
, min_dev_sectors
-
10962 RAID_DISK_RESERVED_BLOCKS_IMSM_HI
);
10964 write_imsm_migr_rec(st
);
10969 /*******************************************************************************
10970 * Function: save_backup_imsm
10971 * Description: Function saves critical data stripes to Migration Copy Area
10972 * and updates the current migration unit status.
10973 * Use restore_stripes() to form a destination stripe,
10974 * and to write it to the Copy Area.
10976 * st : supertype information
10977 * dev : imsm device that backup is saved for
10978 * info : general array info
10979 * buf : input buffer
10980 * length : length of data to backup (blocks_per_unit)
10984 ******************************************************************************/
10985 int save_backup_imsm(struct supertype
*st
,
10986 struct imsm_dev
*dev
,
10987 struct mdinfo
*info
,
10992 struct intel_super
*super
= st
->sb
;
10994 struct imsm_map
*map_dest
= get_imsm_map(dev
, MAP_0
);
10995 int new_disks
= map_dest
->num_members
;
10996 int dest_layout
= 0;
10997 int dest_chunk
, targets
[new_disks
];
10998 unsigned long long start
, target_offsets
[new_disks
];
10999 int data_disks
= imsm_num_data_members(map_dest
);
11001 for (i
= 0; i
< new_disks
; i
++) {
11002 struct dl
*dl_disk
= get_imsm_dl_disk(super
, i
);
11003 if (dl_disk
&& is_fd_valid(dl_disk
->fd
))
11004 targets
[i
] = dl_disk
->fd
;
11009 start
= info
->reshape_progress
* 512;
11010 for (i
= 0; i
< new_disks
; i
++) {
11011 target_offsets
[i
] = migr_chkp_area_pba(super
->migr_rec
) * 512;
11012 /* move back copy area adderss, it will be moved forward
11013 * in restore_stripes() using start input variable
11015 target_offsets
[i
] -= start
/data_disks
;
11018 dest_layout
= imsm_level_to_layout(map_dest
->raid_level
);
11019 dest_chunk
= __le16_to_cpu(map_dest
->blocks_per_strip
) * 512;
11021 if (restore_stripes(targets
, /* list of dest devices */
11022 target_offsets
, /* migration record offsets */
11025 map_dest
->raid_level
,
11027 -1, /* source backup file descriptor */
11028 0, /* input buf offset
11029 * always 0 buf is already offseted */
11033 pr_err("Error restoring stripes\n");
11043 /*******************************************************************************
11044 * Function: save_checkpoint_imsm
11045 * Description: Function called for current unit status update
11046 * in the migration record. It writes it to disk.
11048 * super : imsm internal array info
11049 * info : general array info
11053 * 2: failure, means no valid migration record
11054 * / no general migration in progress /
11055 ******************************************************************************/
11056 int save_checkpoint_imsm(struct supertype
*st
, struct mdinfo
*info
, int state
)
11058 struct intel_super
*super
= st
->sb
;
11059 unsigned long long blocks_per_unit
;
11060 unsigned long long curr_migr_unit
;
11062 if (load_imsm_migr_rec(super
) != 0) {
11063 dprintf("imsm: ERROR: Cannot read migration record for checkpoint save.\n");
11067 blocks_per_unit
= __le32_to_cpu(super
->migr_rec
->blocks_per_unit
);
11068 if (blocks_per_unit
== 0) {
11069 dprintf("imsm: no migration in progress.\n");
11072 curr_migr_unit
= info
->reshape_progress
/ blocks_per_unit
;
11073 /* check if array is alligned to copy area
11074 * if it is not alligned, add one to current migration unit value
11075 * this can happend on array reshape finish only
11077 if (info
->reshape_progress
% blocks_per_unit
)
11080 set_current_migr_unit(super
->migr_rec
, curr_migr_unit
);
11081 super
->migr_rec
->rec_status
= __cpu_to_le32(state
);
11082 set_migr_dest_1st_member_lba(super
->migr_rec
,
11083 super
->migr_rec
->dest_depth_per_unit
* curr_migr_unit
);
11085 if (write_imsm_migr_rec(st
) < 0) {
11086 dprintf("imsm: Cannot write migration record outside backup area\n");
11093 /*******************************************************************************
11094 * Function: recover_backup_imsm
11095 * Description: Function recovers critical data from the Migration Copy Area
11096 * while assembling an array.
11098 * super : imsm internal array info
11099 * info : general array info
11101 * 0 : success (or there is no data to recover)
11103 ******************************************************************************/
11104 int recover_backup_imsm(struct supertype
*st
, struct mdinfo
*info
)
11106 struct intel_super
*super
= st
->sb
;
11107 struct migr_record
*migr_rec
= super
->migr_rec
;
11108 struct imsm_map
*map_dest
;
11109 struct intel_dev
*id
= NULL
;
11110 unsigned long long read_offset
;
11111 unsigned long long write_offset
;
11113 int new_disks
, err
;
11116 unsigned int sector_size
= super
->sector_size
;
11117 unsigned long long curr_migr_unit
= current_migr_unit(migr_rec
);
11118 unsigned long long num_migr_units
= get_num_migr_units(migr_rec
);
11120 int skipped_disks
= 0;
11121 struct dl
*dl_disk
;
11123 err
= sysfs_get_str(info
, NULL
, "array_state", (char *)buffer
, 20);
11127 /* recover data only during assemblation */
11128 if (strncmp(buffer
, "inactive", 8) != 0)
11130 /* no data to recover */
11131 if (__le32_to_cpu(migr_rec
->rec_status
) == UNIT_SRC_NORMAL
)
11133 if (curr_migr_unit
>= num_migr_units
)
11136 /* find device during reshape */
11137 for (id
= super
->devlist
; id
; id
= id
->next
)
11138 if (is_gen_migration(id
->dev
))
11143 map_dest
= get_imsm_map(id
->dev
, MAP_0
);
11144 new_disks
= map_dest
->num_members
;
11146 read_offset
= migr_chkp_area_pba(migr_rec
) * 512;
11148 write_offset
= (migr_dest_1st_member_lba(migr_rec
) +
11149 pba_of_lba0(map_dest
)) * 512;
11151 unit_len
= __le32_to_cpu(migr_rec
->dest_depth_per_unit
) * 512;
11152 if (posix_memalign((void **)&buf
, sector_size
, unit_len
) != 0)
11155 for (dl_disk
= super
->disks
; dl_disk
; dl_disk
= dl_disk
->next
) {
11156 if (dl_disk
->index
< 0)
11159 if (!is_fd_valid(dl_disk
->fd
)) {
11163 if (lseek64(dl_disk
->fd
, read_offset
, SEEK_SET
) < 0) {
11164 pr_err("Cannot seek to block: %s\n",
11169 if (read(dl_disk
->fd
, buf
, unit_len
) != (ssize_t
)unit_len
) {
11170 pr_err("Cannot read copy area block: %s\n",
11175 if (lseek64(dl_disk
->fd
, write_offset
, SEEK_SET
) < 0) {
11176 pr_err("Cannot seek to block: %s\n",
11181 if (write(dl_disk
->fd
, buf
, unit_len
) != (ssize_t
)unit_len
) {
11182 pr_err("Cannot restore block: %s\n",
11189 if (skipped_disks
> imsm_get_allowed_degradation(info
->new_level
,
11193 pr_err("Cannot restore data from backup. Too many failed disks\n");
11197 if (save_checkpoint_imsm(st
, info
, UNIT_SRC_NORMAL
)) {
11198 /* ignore error == 2, this can mean end of reshape here
11200 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL) during restart\n");
11209 static char disk_by_path
[] = "/dev/disk/by-path/";
11211 static const char *imsm_get_disk_controller_domain(const char *path
)
11213 char disk_path
[PATH_MAX
];
11217 strcpy(disk_path
, disk_by_path
);
11218 strncat(disk_path
, path
, PATH_MAX
- strlen(disk_path
) - 1);
11219 if (stat(disk_path
, &st
) == 0) {
11220 struct sys_dev
* hba
;
11223 path
= devt_to_devpath(st
.st_rdev
, 1, NULL
);
11226 hba
= find_disk_attached_hba(-1, path
);
11227 if (hba
&& hba
->type
== SYS_DEV_SAS
)
11229 else if (hba
&& hba
->type
== SYS_DEV_SATA
)
11231 else if (hba
&& hba
->type
== SYS_DEV_VMD
)
11233 else if (hba
&& hba
->type
== SYS_DEV_NVME
)
11237 dprintf("path: %s hba: %s attached: %s\n",
11238 path
, (hba
) ? hba
->path
: "NULL", drv
);
11244 static char *imsm_find_array_devnm_by_subdev(int subdev
, char *container
)
11246 static char devnm
[32];
11247 char subdev_name
[20];
11248 struct mdstat_ent
*mdstat
;
11250 sprintf(subdev_name
, "%d", subdev
);
11251 mdstat
= mdstat_by_subdev(subdev_name
, container
);
11255 strcpy(devnm
, mdstat
->devnm
);
11256 free_mdstat(mdstat
);
11260 static int imsm_reshape_is_allowed_on_container(struct supertype
*st
,
11261 struct geo_params
*geo
,
11262 int *old_raid_disks
,
11265 /* currently we only support increasing the number of devices
11266 * for a container. This increases the number of device for each
11267 * member array. They must all be RAID0 or RAID5.
11270 struct mdinfo
*info
, *member
;
11271 int devices_that_can_grow
= 0;
11273 dprintf("imsm: imsm_reshape_is_allowed_on_container(ENTER): st->devnm = (%s)\n", st
->devnm
);
11275 if (geo
->size
> 0 ||
11276 geo
->level
!= UnSet
||
11277 geo
->layout
!= UnSet
||
11278 geo
->chunksize
!= 0 ||
11279 geo
->raid_disks
== UnSet
) {
11280 dprintf("imsm: Container operation is allowed for raid disks number change only.\n");
11284 if (direction
== ROLLBACK_METADATA_CHANGES
) {
11285 dprintf("imsm: Metadata changes rollback is not supported for container operation.\n");
11289 info
= container_content_imsm(st
, NULL
);
11290 for (member
= info
; member
; member
= member
->next
) {
11293 dprintf("imsm: checking device_num: %i\n",
11294 member
->container_member
);
11296 if (geo
->raid_disks
<= member
->array
.raid_disks
) {
11297 /* we work on container for Online Capacity Expansion
11298 * only so raid_disks has to grow
11300 dprintf("imsm: for container operation raid disks increase is required\n");
11304 if (info
->array
.level
!= 0 && info
->array
.level
!= 5) {
11305 /* we cannot use this container with other raid level
11307 dprintf("imsm: for container operation wrong raid level (%i) detected\n",
11308 info
->array
.level
);
11311 /* check for platform support
11312 * for this raid level configuration
11314 struct intel_super
*super
= st
->sb
;
11315 if (!is_raid_level_supported(super
->orom
,
11316 member
->array
.level
,
11317 geo
->raid_disks
)) {
11318 dprintf("platform does not support raid%d with %d disk%s\n",
11321 geo
->raid_disks
> 1 ? "s" : "");
11324 /* check if component size is aligned to chunk size
11326 if (info
->component_size
%
11327 (info
->array
.chunk_size
/512)) {
11328 dprintf("Component size is not aligned to chunk size\n");
11333 if (*old_raid_disks
&&
11334 info
->array
.raid_disks
!= *old_raid_disks
)
11336 *old_raid_disks
= info
->array
.raid_disks
;
11338 /* All raid5 and raid0 volumes in container
11339 * have to be ready for Online Capacity Expansion
11340 * so they need to be assembled. We have already
11341 * checked that no recovery etc is happening.
11343 result
= imsm_find_array_devnm_by_subdev(member
->container_member
,
11344 st
->container_devnm
);
11345 if (result
== NULL
) {
11346 dprintf("imsm: cannot find array\n");
11349 devices_that_can_grow
++;
11352 if (!member
&& devices_that_can_grow
)
11356 dprintf("Container operation allowed\n");
11358 dprintf("Error: %i\n", ret_val
);
11363 /* Function: get_spares_for_grow
11364 * Description: Allocates memory and creates list of spare devices
11365 * avaliable in container. Checks if spare drive size is acceptable.
11366 * Parameters: Pointer to the supertype structure
11367 * Returns: Pointer to the list of spare devices (mdinfo structure) on success,
11370 static struct mdinfo
*get_spares_for_grow(struct supertype
*st
)
11372 struct spare_criteria sc
;
11374 get_spare_criteria_imsm(st
, &sc
);
11375 return container_choose_spares(st
, &sc
, NULL
, NULL
, NULL
, 0);
11378 /******************************************************************************
11379 * function: imsm_create_metadata_update_for_reshape
11380 * Function creates update for whole IMSM container.
11382 ******************************************************************************/
11383 static int imsm_create_metadata_update_for_reshape(
11384 struct supertype
*st
,
11385 struct geo_params
*geo
,
11386 int old_raid_disks
,
11387 struct imsm_update_reshape
**updatep
)
11389 struct intel_super
*super
= st
->sb
;
11390 struct imsm_super
*mpb
= super
->anchor
;
11391 int update_memory_size
;
11392 struct imsm_update_reshape
*u
;
11393 struct mdinfo
*spares
;
11396 struct mdinfo
*dev
;
11398 dprintf("(enter) raid_disks = %i\n", geo
->raid_disks
);
11400 delta_disks
= geo
->raid_disks
- old_raid_disks
;
11402 /* size of all update data without anchor */
11403 update_memory_size
= sizeof(struct imsm_update_reshape
);
11405 /* now add space for spare disks that we need to add. */
11406 update_memory_size
+= sizeof(u
->new_disks
[0]) * (delta_disks
- 1);
11408 u
= xcalloc(1, update_memory_size
);
11409 u
->type
= update_reshape_container_disks
;
11410 u
->old_raid_disks
= old_raid_disks
;
11411 u
->new_raid_disks
= geo
->raid_disks
;
11413 /* now get spare disks list
11415 spares
= get_spares_for_grow(st
);
11417 if (spares
== NULL
|| delta_disks
> spares
->array
.spare_disks
) {
11418 pr_err("imsm: ERROR: Cannot get spare devices for %s.\n", geo
->dev_name
);
11423 /* we have got spares
11424 * update disk list in imsm_disk list table in anchor
11426 dprintf("imsm: %i spares are available.\n\n",
11427 spares
->array
.spare_disks
);
11429 dev
= spares
->devs
;
11430 for (i
= 0; i
< delta_disks
; i
++) {
11435 u
->new_disks
[i
] = makedev(dev
->disk
.major
,
11437 dl
= get_disk_super(super
, dev
->disk
.major
, dev
->disk
.minor
);
11438 dl
->index
= mpb
->num_disks
;
11446 sysfs_free(spares
);
11448 dprintf("imsm: reshape update preparation :");
11449 if (i
== delta_disks
) {
11450 dprintf_cont(" OK\n");
11452 return update_memory_size
;
11455 dprintf_cont(" Error\n");
11460 /******************************************************************************
11461 * function: imsm_create_metadata_update_for_size_change()
11462 * Creates update for IMSM array for array size change.
11464 ******************************************************************************/
11465 static int imsm_create_metadata_update_for_size_change(
11466 struct supertype
*st
,
11467 struct geo_params
*geo
,
11468 struct imsm_update_size_change
**updatep
)
11470 struct intel_super
*super
= st
->sb
;
11471 int update_memory_size
;
11472 struct imsm_update_size_change
*u
;
11474 dprintf("(enter) New size = %llu\n", geo
->size
);
11476 /* size of all update data without anchor */
11477 update_memory_size
= sizeof(struct imsm_update_size_change
);
11479 u
= xcalloc(1, update_memory_size
);
11480 u
->type
= update_size_change
;
11481 u
->subdev
= super
->current_vol
;
11482 u
->new_size
= geo
->size
;
11484 dprintf("imsm: reshape update preparation : OK\n");
11487 return update_memory_size
;
11490 /******************************************************************************
11491 * function: imsm_create_metadata_update_for_migration()
11492 * Creates update for IMSM array.
11494 ******************************************************************************/
11495 static int imsm_create_metadata_update_for_migration(
11496 struct supertype
*st
,
11497 struct geo_params
*geo
,
11498 struct imsm_update_reshape_migration
**updatep
)
11500 struct intel_super
*super
= st
->sb
;
11501 int update_memory_size
;
11502 int current_chunk_size
;
11503 struct imsm_update_reshape_migration
*u
;
11504 struct imsm_dev
*dev
= get_imsm_dev(super
, super
->current_vol
);
11505 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
11506 int previous_level
= -1;
11508 dprintf("(enter) New Level = %i\n", geo
->level
);
11510 /* size of all update data without anchor */
11511 update_memory_size
= sizeof(struct imsm_update_reshape_migration
);
11513 u
= xcalloc(1, update_memory_size
);
11514 u
->type
= update_reshape_migration
;
11515 u
->subdev
= super
->current_vol
;
11516 u
->new_level
= geo
->level
;
11517 u
->new_layout
= geo
->layout
;
11518 u
->new_raid_disks
= u
->old_raid_disks
= geo
->raid_disks
;
11519 u
->new_disks
[0] = -1;
11520 u
->new_chunksize
= -1;
11522 current_chunk_size
= __le16_to_cpu(map
->blocks_per_strip
) / 2;
11524 if (geo
->chunksize
!= current_chunk_size
) {
11525 u
->new_chunksize
= geo
->chunksize
/ 1024;
11526 dprintf("imsm: chunk size change from %i to %i\n",
11527 current_chunk_size
, u
->new_chunksize
);
11529 previous_level
= map
->raid_level
;
11531 if (geo
->level
== 5 && previous_level
== 0) {
11532 struct mdinfo
*spares
= NULL
;
11534 u
->new_raid_disks
++;
11535 spares
= get_spares_for_grow(st
);
11536 if (spares
== NULL
|| spares
->array
.spare_disks
< 1) {
11538 sysfs_free(spares
);
11539 update_memory_size
= 0;
11540 pr_err("cannot get spare device for requested migration\n");
11543 sysfs_free(spares
);
11545 dprintf("imsm: reshape update preparation : OK\n");
11548 return update_memory_size
;
11551 static void imsm_update_metadata_locally(struct supertype
*st
,
11552 void *buf
, int len
)
11554 struct metadata_update mu
;
11559 mu
.space_list
= NULL
;
11561 if (imsm_prepare_update(st
, &mu
))
11562 imsm_process_update(st
, &mu
);
11564 while (mu
.space_list
) {
11565 void **space
= mu
.space_list
;
11566 mu
.space_list
= *space
;
11571 /***************************************************************************
11572 * Function: imsm_analyze_change
11573 * Description: Function analyze change for single volume
11574 * and validate if transition is supported
11575 * Parameters: Geometry parameters, supertype structure,
11576 * metadata change direction (apply/rollback)
11577 * Returns: Operation type code on success, -1 if fail
11578 ****************************************************************************/
11579 enum imsm_reshape_type
imsm_analyze_change(struct supertype
*st
,
11580 struct geo_params
*geo
,
11583 struct mdinfo info
;
11585 int check_devs
= 0;
11587 /* number of added/removed disks in operation result */
11588 int devNumChange
= 0;
11589 /* imsm compatible layout value for array geometry verification */
11590 int imsm_layout
= -1;
11592 struct imsm_dev
*dev
;
11593 struct imsm_map
*map
;
11594 struct intel_super
*super
;
11595 unsigned long long current_size
;
11596 unsigned long long free_size
;
11597 unsigned long long max_size
;
11600 getinfo_super_imsm_volume(st
, &info
, NULL
);
11601 if (geo
->level
!= info
.array
.level
&& geo
->level
>= 0 &&
11602 geo
->level
!= UnSet
) {
11603 switch (info
.array
.level
) {
11605 if (geo
->level
== 5) {
11606 change
= CH_MIGRATION
;
11607 if (geo
->layout
!= ALGORITHM_LEFT_ASYMMETRIC
) {
11608 pr_err("Error. Requested Layout not supported (left-asymmetric layout is supported only)!\n");
11610 goto analyse_change_exit
;
11612 imsm_layout
= geo
->layout
;
11614 devNumChange
= 1; /* parity disk added */
11615 } else if (geo
->level
== 10) {
11616 change
= CH_TAKEOVER
;
11618 devNumChange
= 2; /* two mirrors added */
11619 imsm_layout
= 0x102; /* imsm supported layout */
11624 if (geo
->level
== 0) {
11625 change
= CH_TAKEOVER
;
11627 devNumChange
= -(geo
->raid_disks
/2);
11628 imsm_layout
= 0; /* imsm raid0 layout */
11632 if (change
== -1) {
11633 pr_err("Error. Level Migration from %d to %d not supported!\n",
11634 info
.array
.level
, geo
->level
);
11635 goto analyse_change_exit
;
11638 geo
->level
= info
.array
.level
;
11640 if (geo
->layout
!= info
.array
.layout
&&
11641 (geo
->layout
!= UnSet
&& geo
->layout
!= -1)) {
11642 change
= CH_MIGRATION
;
11643 if (info
.array
.layout
== 0 && info
.array
.level
== 5 &&
11644 geo
->layout
== 5) {
11645 /* reshape 5 -> 4 */
11646 } else if (info
.array
.layout
== 5 && info
.array
.level
== 5 &&
11647 geo
->layout
== 0) {
11648 /* reshape 4 -> 5 */
11652 pr_err("Error. Layout Migration from %d to %d not supported!\n",
11653 info
.array
.layout
, geo
->layout
);
11655 goto analyse_change_exit
;
11658 geo
->layout
= info
.array
.layout
;
11659 if (imsm_layout
== -1)
11660 imsm_layout
= info
.array
.layout
;
11663 if (geo
->chunksize
> 0 && geo
->chunksize
!= UnSet
&&
11664 geo
->chunksize
!= info
.array
.chunk_size
) {
11665 if (info
.array
.level
== 10) {
11666 pr_err("Error. Chunk size change for RAID 10 is not supported.\n");
11668 goto analyse_change_exit
;
11669 } else if (info
.component_size
% (geo
->chunksize
/512)) {
11670 pr_err("New chunk size (%dK) does not evenly divide device size (%lluk). Aborting...\n",
11671 geo
->chunksize
/1024, info
.component_size
/2);
11673 goto analyse_change_exit
;
11675 change
= CH_MIGRATION
;
11677 geo
->chunksize
= info
.array
.chunk_size
;
11680 chunk
= geo
->chunksize
/ 1024;
11683 dev
= get_imsm_dev(super
, super
->current_vol
);
11684 map
= get_imsm_map(dev
, MAP_0
);
11685 data_disks
= imsm_num_data_members(map
);
11686 /* compute current size per disk member
11688 current_size
= info
.custom_array_size
/ data_disks
;
11690 if (geo
->size
> 0 && geo
->size
!= MAX_SIZE
) {
11691 /* align component size
11693 geo
->size
= imsm_component_size_alignment_check(
11694 get_imsm_raid_level(dev
->vol
.map
),
11695 chunk
* 1024, super
->sector_size
,
11697 if (geo
->size
== 0) {
11698 pr_err("Error. Size expansion is supported only (current size is %llu, requested size /rounded/ is 0).\n",
11700 goto analyse_change_exit
;
11704 if (current_size
!= geo
->size
&& geo
->size
> 0) {
11705 if (change
!= -1) {
11706 pr_err("Error. Size change should be the only one at a time.\n");
11708 goto analyse_change_exit
;
11710 if ((super
->current_vol
+ 1) != super
->anchor
->num_raid_devs
) {
11711 pr_err("Error. The last volume in container can be expanded only (%i/%s).\n",
11712 super
->current_vol
, st
->devnm
);
11713 goto analyse_change_exit
;
11715 /* check the maximum available size
11717 rv
= imsm_get_free_size(super
, dev
->vol
.map
->num_members
,
11718 0, chunk
, &free_size
);
11720 if (rv
!= IMSM_STATUS_OK
)
11721 /* Cannot find maximum available space
11725 max_size
= free_size
+ current_size
;
11726 /* align component size
11728 max_size
= imsm_component_size_alignment_check(
11729 get_imsm_raid_level(dev
->vol
.map
),
11730 chunk
* 1024, super
->sector_size
,
11733 if (geo
->size
== MAX_SIZE
) {
11734 /* requested size change to the maximum available size
11736 if (max_size
== 0) {
11737 pr_err("Error. Cannot find maximum available space.\n");
11739 goto analyse_change_exit
;
11741 geo
->size
= max_size
;
11744 if (direction
== ROLLBACK_METADATA_CHANGES
) {
11745 /* accept size for rollback only
11748 /* round size due to metadata compatibility
11750 geo
->size
= (geo
->size
>> SECT_PER_MB_SHIFT
)
11751 << SECT_PER_MB_SHIFT
;
11752 dprintf("Prepare update for size change to %llu\n",
11754 if (current_size
>= geo
->size
) {
11755 pr_err("Error. Size expansion is supported only (current size is %llu, requested size /rounded/ is %llu).\n",
11756 current_size
, geo
->size
);
11757 goto analyse_change_exit
;
11759 if (max_size
&& geo
->size
> max_size
) {
11760 pr_err("Error. Requested size is larger than maximum available size (maximum available size is %llu, requested size /rounded/ is %llu).\n",
11761 max_size
, geo
->size
);
11762 goto analyse_change_exit
;
11765 geo
->size
*= data_disks
;
11766 geo
->raid_disks
= dev
->vol
.map
->num_members
;
11767 change
= CH_ARRAY_SIZE
;
11769 if (!validate_geometry_imsm(st
,
11772 geo
->raid_disks
+ devNumChange
,
11774 geo
->size
, INVALID_SECTORS
,
11775 0, 0, info
.consistency_policy
, 1))
11779 struct intel_super
*super
= st
->sb
;
11780 struct imsm_super
*mpb
= super
->anchor
;
11782 if (mpb
->num_raid_devs
> 1) {
11783 pr_err("Error. Cannot perform operation on %s- for this operation "
11784 "it MUST be single array in container\n", geo
->dev_name
);
11789 analyse_change_exit
:
11790 if (direction
== ROLLBACK_METADATA_CHANGES
&&
11791 (change
== CH_MIGRATION
|| change
== CH_TAKEOVER
)) {
11792 dprintf("imsm: Metadata changes rollback is not supported for migration and takeover operations.\n");
11798 int imsm_takeover(struct supertype
*st
, struct geo_params
*geo
)
11800 struct intel_super
*super
= st
->sb
;
11801 struct imsm_update_takeover
*u
;
11803 u
= xmalloc(sizeof(struct imsm_update_takeover
));
11805 u
->type
= update_takeover
;
11806 u
->subarray
= super
->current_vol
;
11808 /* 10->0 transition */
11809 if (geo
->level
== 0)
11810 u
->direction
= R10_TO_R0
;
11812 /* 0->10 transition */
11813 if (geo
->level
== 10)
11814 u
->direction
= R0_TO_R10
;
11816 /* update metadata locally */
11817 imsm_update_metadata_locally(st
, u
,
11818 sizeof(struct imsm_update_takeover
));
11819 /* and possibly remotely */
11820 if (st
->update_tail
)
11821 append_metadata_update(st
, u
,
11822 sizeof(struct imsm_update_takeover
));
11829 /* Flush size update if size calculated by num_data_stripes is higher than
11830 * imsm_dev_size to eliminate differences during reshape.
11831 * Mdmon will recalculate them correctly.
11832 * If subarray index is not set then check whole container.
11834 * 0 - no error occurred
11835 * 1 - error detected
11837 static int imsm_fix_size_mismatch(struct supertype
*st
, int subarray_index
)
11839 struct intel_super
*super
= st
->sb
;
11840 int tmp
= super
->current_vol
;
11844 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
11845 if (subarray_index
>= 0 && i
!= subarray_index
)
11847 super
->current_vol
= i
;
11848 struct imsm_dev
*dev
= get_imsm_dev(super
, super
->current_vol
);
11849 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
11850 unsigned int disc_count
= imsm_num_data_members(map
);
11851 struct geo_params geo
;
11852 struct imsm_update_size_change
*update
;
11853 unsigned long long calc_size
= per_dev_array_size(map
) * disc_count
;
11854 unsigned long long d_size
= imsm_dev_size(dev
);
11857 if (calc_size
== d_size
)
11860 /* There is a difference, confirm that imsm_dev_size is
11861 * smaller and push update.
11863 if (d_size
> calc_size
) {
11864 pr_err("imsm: dev size of subarray %d is incorrect\n",
11868 memset(&geo
, 0, sizeof(struct geo_params
));
11870 u_size
= imsm_create_metadata_update_for_size_change(st
, &geo
,
11873 dprintf("imsm: Cannot prepare size change update\n");
11876 imsm_update_metadata_locally(st
, update
, u_size
);
11877 if (st
->update_tail
) {
11878 append_metadata_update(st
, update
, u_size
);
11879 flush_metadata_updates(st
);
11880 st
->update_tail
= &st
->updates
;
11882 imsm_sync_metadata(st
);
11888 super
->current_vol
= tmp
;
11892 static int imsm_reshape_super(struct supertype
*st
, unsigned long long size
,
11894 int layout
, int chunksize
, int raid_disks
,
11895 int delta_disks
, char *backup
, char *dev
,
11896 int direction
, int verbose
)
11899 struct geo_params geo
;
11901 dprintf("(enter)\n");
11903 memset(&geo
, 0, sizeof(struct geo_params
));
11905 geo
.dev_name
= dev
;
11906 strcpy(geo
.devnm
, st
->devnm
);
11909 geo
.layout
= layout
;
11910 geo
.chunksize
= chunksize
;
11911 geo
.raid_disks
= raid_disks
;
11912 if (delta_disks
!= UnSet
)
11913 geo
.raid_disks
+= delta_disks
;
11915 dprintf("for level : %i\n", geo
.level
);
11916 dprintf("for raid_disks : %i\n", geo
.raid_disks
);
11918 if (strcmp(st
->container_devnm
, st
->devnm
) == 0) {
11919 /* On container level we can only increase number of devices. */
11920 dprintf("imsm: info: Container operation\n");
11921 int old_raid_disks
= 0;
11923 if (imsm_reshape_is_allowed_on_container(
11924 st
, &geo
, &old_raid_disks
, direction
)) {
11925 struct imsm_update_reshape
*u
= NULL
;
11928 if (imsm_fix_size_mismatch(st
, -1)) {
11929 dprintf("imsm: Cannot fix size mismatch\n");
11930 goto exit_imsm_reshape_super
;
11933 len
= imsm_create_metadata_update_for_reshape(
11934 st
, &geo
, old_raid_disks
, &u
);
11937 dprintf("imsm: Cannot prepare update\n");
11938 goto exit_imsm_reshape_super
;
11942 /* update metadata locally */
11943 imsm_update_metadata_locally(st
, u
, len
);
11944 /* and possibly remotely */
11945 if (st
->update_tail
)
11946 append_metadata_update(st
, u
, len
);
11951 pr_err("(imsm) Operation is not allowed on this container\n");
11954 /* On volume level we support following operations
11955 * - takeover: raid10 -> raid0; raid0 -> raid10
11956 * - chunk size migration
11957 * - migration: raid5 -> raid0; raid0 -> raid5
11959 struct intel_super
*super
= st
->sb
;
11960 struct intel_dev
*dev
= super
->devlist
;
11962 dprintf("imsm: info: Volume operation\n");
11963 /* find requested device */
11966 imsm_find_array_devnm_by_subdev(
11967 dev
->index
, st
->container_devnm
);
11968 if (devnm
&& strcmp(devnm
, geo
.devnm
) == 0)
11973 pr_err("Cannot find %s (%s) subarray\n",
11974 geo
.dev_name
, geo
.devnm
);
11975 goto exit_imsm_reshape_super
;
11977 super
->current_vol
= dev
->index
;
11978 change
= imsm_analyze_change(st
, &geo
, direction
);
11981 ret_val
= imsm_takeover(st
, &geo
);
11983 case CH_MIGRATION
: {
11984 struct imsm_update_reshape_migration
*u
= NULL
;
11986 imsm_create_metadata_update_for_migration(
11989 dprintf("imsm: Cannot prepare update\n");
11993 /* update metadata locally */
11994 imsm_update_metadata_locally(st
, u
, len
);
11995 /* and possibly remotely */
11996 if (st
->update_tail
)
11997 append_metadata_update(st
, u
, len
);
12002 case CH_ARRAY_SIZE
: {
12003 struct imsm_update_size_change
*u
= NULL
;
12005 imsm_create_metadata_update_for_size_change(
12008 dprintf("imsm: Cannot prepare update\n");
12012 /* update metadata locally */
12013 imsm_update_metadata_locally(st
, u
, len
);
12014 /* and possibly remotely */
12015 if (st
->update_tail
)
12016 append_metadata_update(st
, u
, len
);
12026 exit_imsm_reshape_super
:
12027 dprintf("imsm: reshape_super Exit code = %i\n", ret_val
);
12031 #define COMPLETED_OK 0
12032 #define COMPLETED_NONE 1
12033 #define COMPLETED_DELAYED 2
12035 static int read_completed(int fd
, unsigned long long *val
)
12040 ret
= sysfs_fd_get_str(fd
, buf
, 50);
12044 ret
= COMPLETED_OK
;
12045 if (strncmp(buf
, "none", 4) == 0) {
12046 ret
= COMPLETED_NONE
;
12047 } else if (strncmp(buf
, "delayed", 7) == 0) {
12048 ret
= COMPLETED_DELAYED
;
12051 *val
= strtoull(buf
, &ep
, 0);
12052 if (ep
== buf
|| (*ep
!= 0 && *ep
!= '\n' && *ep
!= ' '))
12058 /*******************************************************************************
12059 * Function: wait_for_reshape_imsm
12060 * Description: Function writes new sync_max value and waits until
12061 * reshape process reach new position
12063 * sra : general array info
12064 * ndata : number of disks in new array's layout
12067 * 1 : there is no reshape in progress,
12069 ******************************************************************************/
12070 int wait_for_reshape_imsm(struct mdinfo
*sra
, int ndata
)
12072 int fd
= sysfs_get_fd(sra
, NULL
, "sync_completed");
12074 unsigned long long completed
;
12075 /* to_complete : new sync_max position */
12076 unsigned long long to_complete
= sra
->reshape_progress
;
12077 unsigned long long position_to_set
= to_complete
/ ndata
;
12079 if (!is_fd_valid(fd
)) {
12080 dprintf("cannot open reshape_position\n");
12085 if (sysfs_fd_get_ll(fd
, &completed
) < 0) {
12087 dprintf("cannot read reshape_position (no reshape in progres)\n");
12096 if (completed
> position_to_set
) {
12097 dprintf("wrong next position to set %llu (%llu)\n",
12098 to_complete
, position_to_set
);
12102 dprintf("Position set: %llu\n", position_to_set
);
12103 if (sysfs_set_num(sra
, NULL
, "sync_max",
12104 position_to_set
) != 0) {
12105 dprintf("cannot set reshape position to %llu\n",
12114 int timeout
= 3000;
12116 sysfs_wait(fd
, &timeout
);
12117 if (sysfs_get_str(sra
, NULL
, "sync_action",
12119 strncmp(action
, "reshape", 7) != 0) {
12120 if (strncmp(action
, "idle", 4) == 0)
12126 rc
= read_completed(fd
, &completed
);
12128 dprintf("cannot read reshape_position (in loop)\n");
12131 } else if (rc
== COMPLETED_NONE
)
12133 } while (completed
< position_to_set
);
12139 /*******************************************************************************
12140 * Function: check_degradation_change
12141 * Description: Check that array hasn't become failed.
12143 * info : for sysfs access
12144 * sources : source disks descriptors
12145 * degraded: previous degradation level
12147 * degradation level
12148 ******************************************************************************/
12149 int check_degradation_change(struct mdinfo
*info
,
12153 unsigned long long new_degraded
;
12156 rv
= sysfs_get_ll(info
, NULL
, "degraded", &new_degraded
);
12157 if (rv
== -1 || (new_degraded
!= (unsigned long long)degraded
)) {
12158 /* check each device to ensure it is still working */
12161 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
12162 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
12164 if (sd
->disk
.state
& (1<<MD_DISK_SYNC
)) {
12166 int raid_disk
= sd
->disk
.raid_disk
;
12168 if (sysfs_get_str(info
,
12169 sd
, "state", sbuf
, sizeof(sbuf
)) < 0 ||
12170 strstr(sbuf
, "faulty") ||
12171 strstr(sbuf
, "in_sync") == NULL
) {
12172 /* this device is dead */
12173 sd
->disk
.state
= (1<<MD_DISK_FAULTY
);
12174 if (raid_disk
>= 0)
12175 close_fd(&sources
[raid_disk
]);
12182 return new_degraded
;
12185 /*******************************************************************************
12186 * Function: imsm_manage_reshape
12187 * Description: Function finds array under reshape and it manages reshape
12188 * process. It creates stripes backups (if required) and sets
12191 * afd : Backup handle (nattive) - not used
12192 * sra : general array info
12193 * reshape : reshape parameters - not used
12194 * st : supertype structure
12195 * blocks : size of critical section [blocks]
12196 * fds : table of source device descriptor
12197 * offsets : start of array (offest per devices)
12199 * destfd : table of destination device descriptor
12200 * destoffsets : table of destination offsets (per device)
12202 * 1 : success, reshape is done
12204 ******************************************************************************/
12205 static int imsm_manage_reshape(
12206 int afd
, struct mdinfo
*sra
, struct reshape
*reshape
,
12207 struct supertype
*st
, unsigned long backup_blocks
,
12208 int *fds
, unsigned long long *offsets
,
12209 int dests
, int *destfd
, unsigned long long *destoffsets
)
12212 struct intel_super
*super
= st
->sb
;
12213 struct intel_dev
*dv
;
12214 unsigned int sector_size
= super
->sector_size
;
12215 struct imsm_dev
*dev
= NULL
;
12216 struct imsm_map
*map_src
, *map_dest
;
12217 int migr_vol_qan
= 0;
12218 int ndata
, odata
; /* [bytes] */
12219 int chunk
; /* [bytes] */
12220 struct migr_record
*migr_rec
;
12222 unsigned int buf_size
; /* [bytes] */
12223 unsigned long long max_position
; /* array size [bytes] */
12224 unsigned long long next_step
; /* [blocks]/[bytes] */
12225 unsigned long long old_data_stripe_length
;
12226 unsigned long long start_src
; /* [bytes] */
12227 unsigned long long start
; /* [bytes] */
12228 unsigned long long start_buf_shift
; /* [bytes] */
12230 int source_layout
= 0;
12231 int subarray_index
= -1;
12236 if (!fds
|| !offsets
)
12239 /* Find volume during the reshape */
12240 for (dv
= super
->devlist
; dv
; dv
= dv
->next
) {
12241 if (dv
->dev
->vol
.migr_type
== MIGR_GEN_MIGR
&&
12242 dv
->dev
->vol
.migr_state
== 1) {
12245 subarray_index
= dv
->index
;
12248 /* Only one volume can migrate at the same time */
12249 if (migr_vol_qan
!= 1) {
12250 pr_err("%s", migr_vol_qan
?
12251 "Number of migrating volumes greater than 1\n" :
12252 "There is no volume during migrationg\n");
12256 map_dest
= get_imsm_map(dev
, MAP_0
);
12257 map_src
= get_imsm_map(dev
, MAP_1
);
12258 if (map_src
== NULL
)
12261 ndata
= imsm_num_data_members(map_dest
);
12262 odata
= imsm_num_data_members(map_src
);
12264 chunk
= __le16_to_cpu(map_src
->blocks_per_strip
) * 512;
12265 old_data_stripe_length
= odata
* chunk
;
12267 migr_rec
= super
->migr_rec
;
12269 /* initialize migration record for start condition */
12270 if (sra
->reshape_progress
== 0)
12271 init_migr_record_imsm(st
, dev
, sra
);
12273 if (__le32_to_cpu(migr_rec
->rec_status
) != UNIT_SRC_NORMAL
) {
12274 dprintf("imsm: cannot restart migration when data are present in copy area.\n");
12277 /* Save checkpoint to update migration record for current
12278 * reshape position (in md). It can be farther than current
12279 * reshape position in metadata.
12281 if (save_checkpoint_imsm(st
, sra
, UNIT_SRC_NORMAL
) == 1) {
12282 /* ignore error == 2, this can mean end of reshape here
12284 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL, initial save)\n");
12289 /* size for data */
12290 buf_size
= __le32_to_cpu(migr_rec
->blocks_per_unit
) * 512;
12291 /* extend buffer size for parity disk */
12292 buf_size
+= __le32_to_cpu(migr_rec
->dest_depth_per_unit
) * 512;
12293 /* add space for stripe alignment */
12294 buf_size
+= old_data_stripe_length
;
12295 if (posix_memalign((void **)&buf
, MAX_SECTOR_SIZE
, buf_size
)) {
12296 dprintf("imsm: Cannot allocate checkpoint buffer\n");
12300 max_position
= sra
->component_size
* ndata
;
12301 source_layout
= imsm_level_to_layout(map_src
->raid_level
);
12303 while (current_migr_unit(migr_rec
) <
12304 get_num_migr_units(migr_rec
)) {
12305 /* current reshape position [blocks] */
12306 unsigned long long current_position
=
12307 __le32_to_cpu(migr_rec
->blocks_per_unit
)
12308 * current_migr_unit(migr_rec
);
12309 unsigned long long border
;
12311 /* Check that array hasn't become failed.
12313 degraded
= check_degradation_change(sra
, fds
, degraded
);
12314 if (degraded
> 1) {
12315 dprintf("imsm: Abort reshape due to degradation level (%i)\n", degraded
);
12319 next_step
= __le32_to_cpu(migr_rec
->blocks_per_unit
);
12321 if ((current_position
+ next_step
) > max_position
)
12322 next_step
= max_position
- current_position
;
12324 start
= current_position
* 512;
12326 /* align reading start to old geometry */
12327 start_buf_shift
= start
% old_data_stripe_length
;
12328 start_src
= start
- start_buf_shift
;
12330 border
= (start_src
/ odata
) - (start
/ ndata
);
12332 if (border
<= __le32_to_cpu(migr_rec
->dest_depth_per_unit
)) {
12333 /* save critical stripes to buf
12334 * start - start address of current unit
12335 * to backup [bytes]
12336 * start_src - start address of current unit
12337 * to backup alligned to source array
12340 unsigned long long next_step_filler
;
12341 unsigned long long copy_length
= next_step
* 512;
12343 /* allign copy area length to stripe in old geometry */
12344 next_step_filler
= ((copy_length
+ start_buf_shift
)
12345 % old_data_stripe_length
);
12346 if (next_step_filler
)
12347 next_step_filler
= (old_data_stripe_length
12348 - next_step_filler
);
12349 dprintf("save_stripes() parameters: start = %llu,\tstart_src = %llu,\tnext_step*512 = %llu,\tstart_in_buf_shift = %llu,\tnext_step_filler = %llu\n",
12350 start
, start_src
, copy_length
,
12351 start_buf_shift
, next_step_filler
);
12353 if (save_stripes(fds
, offsets
, map_src
->num_members
,
12354 chunk
, map_src
->raid_level
,
12355 source_layout
, 0, NULL
, start_src
,
12357 next_step_filler
+ start_buf_shift
,
12359 dprintf("imsm: Cannot save stripes to buffer\n");
12362 /* Convert data to destination format and store it
12363 * in backup general migration area
12365 if (save_backup_imsm(st
, dev
, sra
,
12366 buf
+ start_buf_shift
, copy_length
)) {
12367 dprintf("imsm: Cannot save stripes to target devices\n");
12370 if (save_checkpoint_imsm(st
, sra
,
12371 UNIT_SRC_IN_CP_AREA
)) {
12372 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_IN_CP_AREA)\n");
12376 /* set next step to use whole border area */
12377 border
/= next_step
;
12379 next_step
*= border
;
12381 /* When data backed up, checkpoint stored,
12382 * kick the kernel to reshape unit of data
12384 next_step
= next_step
+ sra
->reshape_progress
;
12385 /* limit next step to array max position */
12386 if (next_step
> max_position
)
12387 next_step
= max_position
;
12388 sysfs_set_num(sra
, NULL
, "suspend_lo", sra
->reshape_progress
);
12389 sysfs_set_num(sra
, NULL
, "suspend_hi", next_step
);
12390 sra
->reshape_progress
= next_step
;
12392 /* wait until reshape finish */
12393 if (wait_for_reshape_imsm(sra
, ndata
)) {
12394 dprintf("wait_for_reshape_imsm returned error!\n");
12400 if (save_checkpoint_imsm(st
, sra
, UNIT_SRC_NORMAL
) == 1) {
12401 /* ignore error == 2, this can mean end of reshape here
12403 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL)\n");
12409 /* clear migr_rec on disks after successful migration */
12412 memset(super
->migr_rec_buf
, 0, MIGR_REC_BUF_SECTORS
*MAX_SECTOR_SIZE
);
12413 for (d
= super
->disks
; d
; d
= d
->next
) {
12414 if (d
->index
< 0 || is_failed(&d
->disk
))
12416 unsigned long long dsize
;
12418 get_dev_size(d
->fd
, NULL
, &dsize
);
12419 if (lseek64(d
->fd
, dsize
- MIGR_REC_SECTOR_POSITION
*sector_size
,
12421 if ((unsigned int)write(d
->fd
, super
->migr_rec_buf
,
12422 MIGR_REC_BUF_SECTORS
*sector_size
) !=
12423 MIGR_REC_BUF_SECTORS
*sector_size
)
12424 perror("Write migr_rec failed");
12428 /* return '1' if done */
12431 /* After the reshape eliminate size mismatch in metadata.
12432 * Don't update md/component_size here, volume hasn't
12433 * to take whole space. It is allowed by kernel.
12434 * md/component_size will be set propoperly after next assembly.
12436 imsm_fix_size_mismatch(st
, subarray_index
);
12440 /* See Grow.c: abort_reshape() for further explanation */
12441 sysfs_set_num(sra
, NULL
, "suspend_lo", 0x7FFFFFFFFFFFFFFFULL
);
12442 sysfs_set_num(sra
, NULL
, "suspend_hi", 0);
12443 sysfs_set_num(sra
, NULL
, "suspend_lo", 0);
12448 /*******************************************************************************
12449 * Function: calculate_bitmap_min_chunksize
12450 * Description: Calculates the minimal valid bitmap chunk size
12452 * max_bits : indicate how many bits can be used for the bitmap
12453 * data_area_size : the size of the data area covered by the bitmap
12456 * The bitmap chunk size
12457 ******************************************************************************/
12458 static unsigned long long
12459 calculate_bitmap_min_chunksize(unsigned long long max_bits
,
12460 unsigned long long data_area_size
)
12462 unsigned long long min_chunk
=
12463 4096; /* sub-page chunks don't work yet.. */
12464 unsigned long long bits
= data_area_size
/ min_chunk
+ 1;
12466 while (bits
> max_bits
) {
12468 bits
= (bits
+ 1) / 2;
12473 /*******************************************************************************
12474 * Function: calculate_bitmap_chunksize
12475 * Description: Calculates the bitmap chunk size for the given device
12477 * st : supertype information
12478 * dev : device for the bitmap
12481 * The bitmap chunk size
12482 ******************************************************************************/
12483 static unsigned long long calculate_bitmap_chunksize(struct supertype
*st
,
12484 struct imsm_dev
*dev
)
12486 struct intel_super
*super
= st
->sb
;
12487 unsigned long long min_chunksize
;
12488 unsigned long long result
= IMSM_DEFAULT_BITMAP_CHUNKSIZE
;
12489 size_t dev_size
= imsm_dev_size(dev
);
12491 min_chunksize
= calculate_bitmap_min_chunksize(
12492 IMSM_BITMAP_AREA_SIZE
* super
->sector_size
, dev_size
);
12494 if (result
< min_chunksize
)
12495 result
= min_chunksize
;
12500 /*******************************************************************************
12501 * Function: init_bitmap_header
12502 * Description: Initialize the bitmap header structure
12504 * st : supertype information
12505 * bms : bitmap header struct to initialize
12506 * dev : device for the bitmap
12511 ******************************************************************************/
12512 static int init_bitmap_header(struct supertype
*st
, struct bitmap_super_s
*bms
,
12513 struct imsm_dev
*dev
)
12520 bms
->magic
= __cpu_to_le32(BITMAP_MAGIC
);
12521 bms
->version
= __cpu_to_le32(BITMAP_MAJOR_HI
);
12522 bms
->daemon_sleep
= __cpu_to_le32(IMSM_DEFAULT_BITMAP_DAEMON_SLEEP
);
12523 bms
->sync_size
= __cpu_to_le64(IMSM_BITMAP_AREA_SIZE
);
12524 bms
->write_behind
= __cpu_to_le32(0);
12526 uuid_from_super_imsm(st
, vol_uuid
);
12527 memcpy(bms
->uuid
, vol_uuid
, 16);
12529 bms
->chunksize
= calculate_bitmap_chunksize(st
, dev
);
12534 /*******************************************************************************
12535 * Function: validate_internal_bitmap_for_drive
12536 * Description: Verify if the bitmap header for a given drive.
12538 * st : supertype information
12539 * offset : The offset from the beginning of the drive where to look for
12540 * the bitmap header.
12541 * d : the drive info
12546 ******************************************************************************/
12547 static int validate_internal_bitmap_for_drive(struct supertype
*st
,
12548 unsigned long long offset
,
12551 struct intel_super
*super
= st
->sb
;
12554 bitmap_super_t
*bms
;
12562 if (posix_memalign(&read_buf
, MAX_SECTOR_SIZE
, IMSM_BITMAP_HEADER_SIZE
))
12566 if (!is_fd_valid(fd
)) {
12567 fd
= open(d
->devname
, O_RDONLY
, 0);
12569 if (!is_fd_valid(fd
)) {
12570 dprintf("cannot open the device %s\n", d
->devname
);
12575 if (lseek64(fd
, offset
* super
->sector_size
, SEEK_SET
) < 0)
12577 if (read(fd
, read_buf
, IMSM_BITMAP_HEADER_SIZE
) !=
12578 IMSM_BITMAP_HEADER_SIZE
)
12581 uuid_from_super_imsm(st
, vol_uuid
);
12584 if ((bms
->magic
!= __cpu_to_le32(BITMAP_MAGIC
)) ||
12585 (bms
->version
!= __cpu_to_le32(BITMAP_MAJOR_HI
)) ||
12586 (!same_uuid((int *)bms
->uuid
, vol_uuid
, st
->ss
->swapuuid
))) {
12587 dprintf("wrong bitmap header detected\n");
12593 if (!is_fd_valid(d
->fd
))
12602 /*******************************************************************************
12603 * Function: validate_internal_bitmap_imsm
12604 * Description: Verify if the bitmap header is in place and with proper data.
12606 * st : supertype information
12609 * 0 : success or device w/o RWH_BITMAP
12611 ******************************************************************************/
12612 static int validate_internal_bitmap_imsm(struct supertype
*st
)
12614 struct intel_super
*super
= st
->sb
;
12615 struct imsm_dev
*dev
= get_imsm_dev(super
, super
->current_vol
);
12616 unsigned long long offset
;
12619 if (dev
->rwh_policy
!= RWH_BITMAP
)
12622 offset
= get_bitmap_header_sector(super
, super
->current_vol
);
12623 for (d
= super
->disks
; d
; d
= d
->next
) {
12624 if (d
->index
< 0 || is_failed(&d
->disk
))
12627 if (validate_internal_bitmap_for_drive(st
, offset
, d
)) {
12628 pr_err("imsm: bitmap validation failed\n");
12635 /*******************************************************************************
12636 * Function: add_internal_bitmap_imsm
12637 * Description: Mark the volume to use the bitmap and updates the chunk size value.
12639 * st : supertype information
12640 * chunkp : bitmap chunk size
12641 * delay : not used for imsm
12642 * write_behind : not used for imsm
12643 * size : not used for imsm
12644 * may_change : not used for imsm
12645 * amajor : not used for imsm
12650 ******************************************************************************/
12651 static int add_internal_bitmap_imsm(struct supertype
*st
, int *chunkp
,
12652 int delay
, int write_behind
,
12653 unsigned long long size
, int may_change
,
12656 struct intel_super
*super
= st
->sb
;
12657 int vol_idx
= super
->current_vol
;
12658 struct imsm_dev
*dev
;
12660 if (!super
->devlist
|| vol_idx
== -1 || !chunkp
)
12663 dev
= get_imsm_dev(super
, vol_idx
);
12664 dev
->rwh_policy
= RWH_BITMAP
;
12665 *chunkp
= calculate_bitmap_chunksize(st
, dev
);
12669 /*******************************************************************************
12670 * Function: locate_bitmap_imsm
12671 * Description: Seek 'fd' to start of write-intent-bitmap.
12673 * st : supertype information
12674 * fd : file descriptor for the device
12675 * node_num : not used for imsm
12680 ******************************************************************************/
12681 static int locate_bitmap_imsm(struct supertype
*st
, int fd
, int node_num
)
12683 struct intel_super
*super
= st
->sb
;
12684 unsigned long long offset
;
12685 int vol_idx
= super
->current_vol
;
12687 if (!super
->devlist
|| vol_idx
== -1)
12690 offset
= get_bitmap_header_sector(super
, super
->current_vol
);
12691 dprintf("bitmap header offset is %llu\n", offset
);
12693 lseek64(fd
, offset
<< 9, 0);
12698 /*******************************************************************************
12699 * Function: write_init_bitmap_imsm
12700 * Description: Write a bitmap header and prepares the area for the bitmap.
12702 * st : supertype information
12703 * fd : file descriptor for the device
12704 * update : not used for imsm
12709 ******************************************************************************/
12710 static int write_init_bitmap_imsm(struct supertype
*st
, int fd
,
12711 enum bitmap_update update
)
12713 struct intel_super
*super
= st
->sb
;
12714 int vol_idx
= super
->current_vol
;
12716 unsigned long long offset
;
12717 bitmap_super_t bms
= { 0 };
12718 size_t written
= 0;
12723 if (!super
->devlist
|| !super
->sector_size
|| vol_idx
== -1)
12726 struct imsm_dev
*dev
= get_imsm_dev(super
, vol_idx
);
12728 /* first clear the space for bitmap header */
12729 unsigned long long bitmap_area_start
=
12730 get_bitmap_header_sector(super
, vol_idx
);
12732 dprintf("zeroing area start (%llu) and size (%u)\n", bitmap_area_start
,
12733 IMSM_BITMAP_AND_HEADER_SIZE
/ super
->sector_size
);
12734 if (zero_disk_range(fd
, bitmap_area_start
,
12735 IMSM_BITMAP_HEADER_SIZE
/ super
->sector_size
)) {
12736 pr_err("imsm: cannot zeroing the space for the bitmap\n");
12740 /* The bitmap area should be filled with "1"s to perform initial
12743 if (posix_memalign(&buf
, MAX_SECTOR_SIZE
, MAX_SECTOR_SIZE
))
12745 memset(buf
, 0xFF, MAX_SECTOR_SIZE
);
12746 offset
= get_bitmap_sector(super
, vol_idx
);
12747 lseek64(fd
, offset
<< 9, 0);
12748 while (written
< IMSM_BITMAP_AREA_SIZE
) {
12749 to_write
= IMSM_BITMAP_AREA_SIZE
- written
;
12750 if (to_write
> MAX_SECTOR_SIZE
)
12751 to_write
= MAX_SECTOR_SIZE
;
12752 rv_num
= write(fd
, buf
, MAX_SECTOR_SIZE
);
12753 if (rv_num
!= MAX_SECTOR_SIZE
) {
12755 dprintf("cannot initialize bitmap area\n");
12761 /* write a bitmap header */
12762 init_bitmap_header(st
, &bms
, dev
);
12763 memset(buf
, 0, MAX_SECTOR_SIZE
);
12764 memcpy(buf
, &bms
, sizeof(bitmap_super_t
));
12765 if (locate_bitmap_imsm(st
, fd
, 0)) {
12767 dprintf("cannot locate the bitmap\n");
12770 if (write(fd
, buf
, MAX_SECTOR_SIZE
) != MAX_SECTOR_SIZE
) {
12772 dprintf("cannot write the bitmap header\n");
12783 /*******************************************************************************
12784 * Function: is_vol_to_setup_bitmap
12785 * Description: Checks if a bitmap should be activated on the dev.
12787 * info : info about the volume to setup the bitmap
12788 * dev : the device to check against bitmap creation
12791 * 0 : bitmap should be set up on the device
12793 ******************************************************************************/
12794 static int is_vol_to_setup_bitmap(struct mdinfo
*info
, struct imsm_dev
*dev
)
12799 if ((strcmp((char *)dev
->volume
, info
->name
) == 0) &&
12800 (dev
->rwh_policy
== RWH_BITMAP
))
12806 /*******************************************************************************
12807 * Function: set_bitmap_sysfs
12808 * Description: Set the sysfs atributes of a given volume to activate the bitmap.
12810 * info : info about the volume where the bitmap should be setup
12811 * chunksize : bitmap chunk size
12812 * location : location of the bitmap
12817 ******************************************************************************/
12818 static int set_bitmap_sysfs(struct mdinfo
*info
, unsigned long long chunksize
,
12821 /* The bitmap/metadata is set to external to allow changing of value for
12822 * bitmap/location. When external is used, the kernel will treat an offset
12823 * related to the device's first lba (in opposition to the "internal" case
12824 * when this value is related to the beginning of the superblock).
12826 if (sysfs_set_str(info
, NULL
, "bitmap/metadata", "external")) {
12827 dprintf("failed to set bitmap/metadata\n");
12831 /* It can only be changed when no bitmap is active.
12832 * Should be bigger than 512 and must be power of 2.
12833 * It is expecting the value in bytes.
12835 if (sysfs_set_num(info
, NULL
, "bitmap/chunksize",
12836 __cpu_to_le32(chunksize
))) {
12837 dprintf("failed to set bitmap/chunksize\n");
12841 /* It is expecting the value in sectors. */
12842 if (sysfs_set_num(info
, NULL
, "bitmap/space",
12843 __cpu_to_le64(IMSM_BITMAP_AREA_SIZE
))) {
12844 dprintf("failed to set bitmap/space\n");
12848 /* Determines the delay between the bitmap updates.
12849 * It is expecting the value in seconds.
12851 if (sysfs_set_num(info
, NULL
, "bitmap/time_base",
12852 __cpu_to_le64(IMSM_DEFAULT_BITMAP_DAEMON_SLEEP
))) {
12853 dprintf("failed to set bitmap/time_base\n");
12857 /* It is expecting the value in sectors with a sign at the beginning. */
12858 if (sysfs_set_str(info
, NULL
, "bitmap/location", location
)) {
12859 dprintf("failed to set bitmap/location\n");
12866 /*******************************************************************************
12867 * Function: set_bitmap_imsm
12868 * Description: Setup the bitmap for the given volume
12870 * st : supertype information
12871 * info : info about the volume where the bitmap should be setup
12876 ******************************************************************************/
12877 static int set_bitmap_imsm(struct supertype
*st
, struct mdinfo
*info
)
12879 struct intel_super
*super
= st
->sb
;
12880 int prev_current_vol
= super
->current_vol
;
12881 struct imsm_dev
*dev
;
12883 char location
[16] = "";
12884 unsigned long long chunksize
;
12885 struct intel_dev
*dev_it
;
12887 for (dev_it
= super
->devlist
; dev_it
; dev_it
= dev_it
->next
) {
12888 super
->current_vol
= dev_it
->index
;
12889 dev
= get_imsm_dev(super
, super
->current_vol
);
12891 if (is_vol_to_setup_bitmap(info
, dev
)) {
12892 if (validate_internal_bitmap_imsm(st
)) {
12893 dprintf("bitmap header validation failed\n");
12897 chunksize
= calculate_bitmap_chunksize(st
, dev
);
12898 dprintf("chunk size is %llu\n", chunksize
);
12900 snprintf(location
, sizeof(location
), "+%llu",
12901 get_bitmap_sector(super
, super
->current_vol
));
12902 dprintf("bitmap offset is %s\n", location
);
12904 if (set_bitmap_sysfs(info
, chunksize
, location
)) {
12905 dprintf("cannot setup the bitmap\n");
12912 super
->current_vol
= prev_current_vol
;
12916 struct superswitch super_imsm
= {
12917 .examine_super
= examine_super_imsm
,
12918 .brief_examine_super
= brief_examine_super_imsm
,
12919 .brief_examine_subarrays
= brief_examine_subarrays_imsm
,
12920 .export_examine_super
= export_examine_super_imsm
,
12921 .detail_super
= detail_super_imsm
,
12922 .brief_detail_super
= brief_detail_super_imsm
,
12923 .write_init_super
= write_init_super_imsm
,
12924 .validate_geometry
= validate_geometry_imsm
,
12925 .add_to_super
= add_to_super_imsm
,
12926 .remove_from_super
= remove_from_super_imsm
,
12927 .detail_platform
= detail_platform_imsm
,
12928 .export_detail_platform
= export_detail_platform_imsm
,
12929 .kill_subarray
= kill_subarray_imsm
,
12930 .update_subarray
= update_subarray_imsm
,
12931 .load_container
= load_container_imsm
,
12932 .default_geometry
= default_geometry_imsm
,
12933 .get_disk_controller_domain
= imsm_get_disk_controller_domain
,
12934 .reshape_super
= imsm_reshape_super
,
12935 .manage_reshape
= imsm_manage_reshape
,
12936 .recover_backup
= recover_backup_imsm
,
12937 .examine_badblocks
= examine_badblocks_imsm
,
12938 .match_home
= match_home_imsm
,
12939 .uuid_from_super
= uuid_from_super_imsm
,
12940 .getinfo_super
= getinfo_super_imsm
,
12941 .getinfo_super_disks
= getinfo_super_disks_imsm
,
12942 .update_super
= update_super_imsm
,
12944 .avail_size
= avail_size_imsm
,
12945 .get_spare_criteria
= get_spare_criteria_imsm
,
12947 .compare_super
= compare_super_imsm
,
12949 .load_super
= load_super_imsm
,
12950 .init_super
= init_super_imsm
,
12951 .store_super
= store_super_imsm
,
12952 .free_super
= free_super_imsm
,
12953 .match_metadata_desc
= match_metadata_desc_imsm
,
12954 .container_content
= container_content_imsm
,
12955 .validate_container
= validate_container_imsm
,
12957 .add_internal_bitmap
= add_internal_bitmap_imsm
,
12958 .locate_bitmap
= locate_bitmap_imsm
,
12959 .write_bitmap
= write_init_bitmap_imsm
,
12960 .set_bitmap
= set_bitmap_imsm
,
12962 .write_init_ppl
= write_init_ppl_imsm
,
12963 .validate_ppl
= validate_ppl_imsm
,
12969 .open_new
= imsm_open_new
,
12970 .set_array_state
= imsm_set_array_state
,
12971 .set_disk
= imsm_set_disk
,
12972 .sync_metadata
= imsm_sync_metadata
,
12973 .activate_spare
= imsm_activate_spare
,
12974 .process_update
= imsm_process_update
,
12975 .prepare_update
= imsm_prepare_update
,
12976 .record_bad_block
= imsm_record_badblock
,
12977 .clear_bad_block
= imsm_clear_badblock
,
12978 .get_bad_blocks
= imsm_get_badblocks
,