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
25 #include "platform-intel.h"
30 #include "drive_encryption.h"
32 /* MPB == Metadata Parameter Block */
33 #define MPB_SIGNATURE "Intel Raid ISM Cfg Sig. "
34 #define MPB_SIG_LEN (strlen(MPB_SIGNATURE))
35 #define MPB_VERSION_RAID0 "1.0.00"
36 #define MPB_VERSION_RAID1 "1.1.00"
37 #define MPB_VERSION_MANY_VOLUMES_PER_ARRAY "1.2.00"
38 #define MPB_VERSION_3OR4_DISK_ARRAY "1.2.01"
39 #define MPB_VERSION_RAID5 "1.2.02"
40 #define MPB_VERSION_5OR6_DISK_ARRAY "1.2.04"
41 #define MPB_VERSION_CNG "1.2.06"
42 #define MPB_VERSION_ATTRIBS "1.3.00"
43 #define MAX_SIGNATURE_LENGTH 32
44 #define MAX_RAID_SERIAL_LEN 16
47 #define MPB_ATTRIB_RAID0 __cpu_to_le32(0x00000001)
49 #define MPB_ATTRIB_RAID1 __cpu_to_le32(0x00000002)
51 #define MPB_ATTRIB_RAID10 __cpu_to_le32(0x00000004)
53 #define MPB_ATTRIB_RAID1E __cpu_to_le32(0x00000008)
55 #define MPB_ATTRIB_RAID5 __cpu_to_le32(0x00000010)
56 /* supports RAID CNG */
57 #define MPB_ATTRIB_RAIDCNG __cpu_to_le32(0x00000020)
58 /* supports expanded stripe sizes of 256K, 512K and 1MB */
59 #define MPB_ATTRIB_EXP_STRIPE_SIZE __cpu_to_le32(0x00000040)
61 /* The OROM Support RST Caching of Volumes */
62 #define MPB_ATTRIB_NVM __cpu_to_le32(0x02000000)
63 /* The OROM supports creating disks greater than 2TB */
64 #define MPB_ATTRIB_2TB_DISK __cpu_to_le32(0x04000000)
65 /* The OROM supports Bad Block Management */
66 #define MPB_ATTRIB_BBM __cpu_to_le32(0x08000000)
68 /* THe OROM Supports NVM Caching of Volumes */
69 #define MPB_ATTRIB_NEVER_USE2 __cpu_to_le32(0x10000000)
70 /* The OROM supports creating volumes greater than 2TB */
71 #define MPB_ATTRIB_2TB __cpu_to_le32(0x20000000)
72 /* originally for PMP, now it's wasted b/c. Never use this bit! */
73 #define MPB_ATTRIB_NEVER_USE __cpu_to_le32(0x40000000)
74 /* Verify MPB contents against checksum after reading MPB */
75 #define MPB_ATTRIB_CHECKSUM_VERIFY __cpu_to_le32(0x80000000)
77 /* Define all supported attributes that have to be accepted by mdadm
79 #define MPB_ATTRIB_SUPPORTED (MPB_ATTRIB_CHECKSUM_VERIFY | \
81 MPB_ATTRIB_2TB_DISK | \
86 MPB_ATTRIB_EXP_STRIPE_SIZE | \
89 /* Define attributes that are unused but not harmful */
90 #define MPB_ATTRIB_IGNORED (MPB_ATTRIB_NEVER_USE)
92 #define MPB_SECTOR_CNT 2210
93 #define IMSM_RESERVED_SECTORS 8192
94 #define NUM_BLOCKS_DIRTY_STRIPE_REGION 2048
95 #define SECT_PER_MB_SHIFT 11
96 #define MAX_SECTOR_SIZE 4096
97 #define MULTIPLE_PPL_AREA_SIZE_IMSM (1024 * 1024) /* Size of the whole
102 * Internal Write-intent bitmap is stored in the same area where PPL.
103 * Both features are mutually exclusive, so it is not an issue.
104 * The first 8KiB of the area are reserved and shall not be used.
106 #define IMSM_BITMAP_AREA_RESERVED_SIZE 8192
108 #define IMSM_BITMAP_HEADER_OFFSET (IMSM_BITMAP_AREA_RESERVED_SIZE)
109 #define IMSM_BITMAP_HEADER_SIZE MAX_SECTOR_SIZE
111 #define IMSM_BITMAP_START_OFFSET (IMSM_BITMAP_HEADER_OFFSET + IMSM_BITMAP_HEADER_SIZE)
112 #define IMSM_BITMAP_AREA_SIZE (MULTIPLE_PPL_AREA_SIZE_IMSM - IMSM_BITMAP_START_OFFSET)
113 #define IMSM_BITMAP_AND_HEADER_SIZE (IMSM_BITMAP_AREA_SIZE + IMSM_BITMAP_HEADER_SIZE)
115 #define IMSM_DEFAULT_BITMAP_CHUNKSIZE (64 * 1024 * 1024)
116 #define IMSM_DEFAULT_BITMAP_DAEMON_SLEEP 5
119 * This macro let's us ensure that no-one accidentally
120 * changes the size of a struct
122 #define ASSERT_SIZE(_struct, size) \
123 static inline void __assert_size_##_struct(void) \
127 case (sizeof(struct _struct) == size): break; \
131 /* Disk configuration info. */
132 #define IMSM_MAX_DEVICES 255
134 __u8 serial
[MAX_RAID_SERIAL_LEN
];/* 0xD8 - 0xE7 ascii serial number */
135 __u32 total_blocks_lo
; /* 0xE8 - 0xEB total blocks lo */
136 __u32 scsi_id
; /* 0xEC - 0xEF scsi ID */
137 #define SPARE_DISK __cpu_to_le32(0x01) /* Spare */
138 #define CONFIGURED_DISK __cpu_to_le32(0x02) /* Member of some RaidDev */
139 #define FAILED_DISK __cpu_to_le32(0x04) /* Permanent failure */
140 #define JOURNAL_DISK __cpu_to_le32(0x2000000) /* Device marked as Journaling Drive */
141 __u32 status
; /* 0xF0 - 0xF3 */
142 __u32 owner_cfg_num
; /* which config 0,1,2... owns this disk */
143 __u32 total_blocks_hi
; /* 0xF4 - 0xF5 total blocks hi */
144 #define IMSM_DISK_FILLERS 3
145 __u32 filler
[IMSM_DISK_FILLERS
]; /* 0xF5 - 0x107 MPB_DISK_FILLERS for future expansion */
147 ASSERT_SIZE(imsm_disk
, 48)
149 /* map selector for map managment
155 /* RAID map configuration infos. */
157 __u32 pba_of_lba0_lo
; /* start address of partition */
158 __u32 blocks_per_member_lo
;/* blocks per member */
159 __u32 num_data_stripes_lo
; /* number of data stripes */
160 __u16 blocks_per_strip
;
161 __u8 map_state
; /* Normal, Uninitialized, Degraded, Failed */
162 #define IMSM_T_STATE_NORMAL 0
163 #define IMSM_T_STATE_UNINITIALIZED 1
164 #define IMSM_T_STATE_DEGRADED 2
165 #define IMSM_T_STATE_FAILED 3
167 #define IMSM_T_RAID0 0
168 #define IMSM_T_RAID1 1
169 #define IMSM_T_RAID5 5 /* since metadata version 1.2.02 ? */
170 __u8 num_members
; /* number of member disks */
171 __u8 num_domains
; /* number of parity domains */
172 __u8 failed_disk_num
; /* valid only when state is degraded */
174 __u32 pba_of_lba0_hi
;
175 __u32 blocks_per_member_hi
;
176 __u32 num_data_stripes_hi
;
177 __u32 filler
[4]; /* expansion area */
178 #define IMSM_ORD_REBUILD (1 << 24)
179 __u32 disk_ord_tbl
[1]; /* disk_ord_tbl[num_members],
180 * top byte contains some flags
183 ASSERT_SIZE(imsm_map
, 52)
186 __u32 curr_migr_unit_lo
;
187 __u32 checkpoint_id
; /* id to access curr_migr_unit */
188 __u8 migr_state
; /* Normal or Migrating */
190 #define MIGR_REBUILD 1
191 #define MIGR_VERIFY 2 /* analagous to echo check > sync_action */
192 #define MIGR_GEN_MIGR 3
193 #define MIGR_STATE_CHANGE 4
194 #define MIGR_REPAIR 5
195 __u8 migr_type
; /* Initializing, Rebuilding, ... */
196 #define RAIDVOL_CLEAN 0
197 #define RAIDVOL_DIRTY 1
198 #define RAIDVOL_DSRECORD_VALID 2
200 __u8 fs_state
; /* fast-sync state for CnG (0xff == disabled) */
201 __u16 verify_errors
; /* number of mismatches */
202 __u16 bad_blocks
; /* number of bad blocks during verify */
203 __u32 curr_migr_unit_hi
;
205 struct imsm_map map
[1];
206 /* here comes another one if migr_state */
208 ASSERT_SIZE(imsm_vol
, 84)
211 __u8 volume
[MAX_RAID_SERIAL_LEN
];
214 #define DEV_BOOTABLE __cpu_to_le32(0x01)
215 #define DEV_BOOT_DEVICE __cpu_to_le32(0x02)
216 #define DEV_READ_COALESCING __cpu_to_le32(0x04)
217 #define DEV_WRITE_COALESCING __cpu_to_le32(0x08)
218 #define DEV_LAST_SHUTDOWN_DIRTY __cpu_to_le32(0x10)
219 #define DEV_HIDDEN_AT_BOOT __cpu_to_le32(0x20)
220 #define DEV_CURRENTLY_HIDDEN __cpu_to_le32(0x40)
221 #define DEV_VERIFY_AND_FIX __cpu_to_le32(0x80)
222 #define DEV_MAP_STATE_UNINIT __cpu_to_le32(0x100)
223 #define DEV_NO_AUTO_RECOVERY __cpu_to_le32(0x200)
224 #define DEV_CLONE_N_GO __cpu_to_le32(0x400)
225 #define DEV_CLONE_MAN_SYNC __cpu_to_le32(0x800)
226 #define DEV_CNG_MASTER_DISK_NUM __cpu_to_le32(0x1000)
227 __u32 status
; /* Persistent RaidDev status */
228 __u32 reserved_blocks
; /* Reserved blocks at beginning of volume */
232 __u8 cng_master_disk
;
236 __u16 my_vol_raid_dev_num
; /* Used in Unique volume Id for this RaidDev */
242 /* Unique Volume Id of the NvCache Volume associated with this volume */
243 __u32 nvc_vol_orig_family_num
;
244 __u16 nvc_vol_raid_dev_num
;
247 #define RWH_DISTRIBUTED 1
248 #define RWH_JOURNALING_DRIVE 2
249 #define RWH_MULTIPLE_DISTRIBUTED 3
250 #define RWH_MULTIPLE_PPLS_JOURNALING_DRIVE 4
251 #define RWH_MULTIPLE_OFF 5
253 __u8 rwh_policy
; /* Raid Write Hole Policy */
254 __u8 jd_serial
[MAX_RAID_SERIAL_LEN
]; /* Journal Drive serial number */
257 #define IMSM_DEV_FILLERS 3
258 __u32 filler
[IMSM_DEV_FILLERS
];
261 ASSERT_SIZE(imsm_dev
, 164)
264 __u8 sig
[MAX_SIGNATURE_LENGTH
]; /* 0x00 - 0x1F */
265 __u32 check_sum
; /* 0x20 - 0x23 MPB Checksum */
266 __u32 mpb_size
; /* 0x24 - 0x27 Size of MPB */
267 __u32 family_num
; /* 0x28 - 0x2B Checksum from first time this config was written */
268 __u32 generation_num
; /* 0x2C - 0x2F Incremented each time this array's MPB is written */
269 __u32 error_log_size
; /* 0x30 - 0x33 in bytes */
270 __u32 attributes
; /* 0x34 - 0x37 */
271 __u8 num_disks
; /* 0x38 Number of configured disks */
272 __u8 num_raid_devs
; /* 0x39 Number of configured volumes */
273 __u8 error_log_pos
; /* 0x3A */
274 __u8 fill
[1]; /* 0x3B */
275 __u32 cache_size
; /* 0x3c - 0x40 in mb */
276 __u32 orig_family_num
; /* 0x40 - 0x43 original family num */
277 __u32 pwr_cycle_count
; /* 0x44 - 0x47 simulated power cycle count for array */
278 __u32 bbm_log_size
; /* 0x48 - 0x4B - size of bad Block Mgmt Log in bytes */
279 __u16 num_raid_devs_created
; /* 0x4C - 0x4D Used for generating unique
280 * volume IDs for raid_dev created in this array
283 __u16 filler1
; /* 0x4E - 0x4F */
284 __u64 creation_time
; /* 0x50 - 0x57 Array creation time */
285 #define IMSM_FILLERS 32
286 __u32 filler
[IMSM_FILLERS
]; /* 0x58 - 0xD7 RAID_MPB_FILLERS */
287 struct imsm_disk disk
[1]; /* 0xD8 diskTbl[numDisks] */
288 /* here comes imsm_dev[num_raid_devs] */
289 /* here comes BBM logs */
291 ASSERT_SIZE(imsm_super
, 264)
293 #define BBM_LOG_MAX_ENTRIES 254
294 #define BBM_LOG_MAX_LBA_ENTRY_VAL 256 /* Represents 256 LBAs */
295 #define BBM_LOG_SIGNATURE 0xabadb10c
297 struct bbm_log_block_addr
{
300 } __attribute__ ((__packed__
));
302 struct bbm_log_entry
{
303 __u8 marked_count
; /* Number of blocks marked - 1 */
304 __u8 disk_ordinal
; /* Disk entry within the imsm_super */
305 struct bbm_log_block_addr defective_block_start
;
306 } __attribute__ ((__packed__
));
309 __u32 signature
; /* 0xABADB10C */
311 struct bbm_log_entry marked_block_entries
[BBM_LOG_MAX_ENTRIES
];
313 ASSERT_SIZE(bbm_log
, 2040)
315 static char *map_state_str
[] = { "normal", "uninitialized", "degraded", "failed" };
317 #define BLOCKS_PER_KB (1024/512)
319 #define RAID_DISK_RESERVED_BLOCKS_IMSM_HI 2209
321 #define GEN_MIGR_AREA_SIZE 2048 /* General Migration Copy Area size in blocks */
323 #define MIGR_REC_BUF_SECTORS 1 /* size of migr_record i/o buffer in sectors */
324 #define MIGR_REC_SECTOR_POSITION 1 /* migr_record position offset on disk,
325 * MIGR_REC_BUF_SECTORS <= MIGR_REC_SECTOR_POS
328 #define UNIT_SRC_NORMAL 0 /* Source data for curr_migr_unit must
329 * be recovered using srcMap */
330 #define UNIT_SRC_IN_CP_AREA 1 /* Source data for curr_migr_unit has
331 * already been migrated and must
332 * be recovered from checkpoint area */
334 #define PPL_ENTRY_SPACE (128 * 1024) /* Size of single PPL, without the header */
337 __u32 rec_status
; /* Status used to determine how to restart
338 * migration in case it aborts
340 __u32 curr_migr_unit_lo
; /* 0..numMigrUnits-1 */
341 __u32 family_num
; /* Family number of MPB
342 * containing the RaidDev
343 * that is migrating */
344 __u32 ascending_migr
; /* True if migrating in increasing
346 __u32 blocks_per_unit
; /* Num disk blocks per unit of operation */
347 __u32 dest_depth_per_unit
; /* Num member blocks each destMap
349 * advances per unit-of-operation */
350 __u32 ckpt_area_pba_lo
; /* Pba of first block of ckpt copy area */
351 __u32 dest_1st_member_lba_lo
; /* First member lba on first
352 * stripe of destination */
353 __u32 num_migr_units_lo
; /* Total num migration units-of-op */
354 __u32 post_migr_vol_cap
; /* Size of volume after
355 * migration completes */
356 __u32 post_migr_vol_cap_hi
; /* Expansion space for LBA64 */
357 __u32 ckpt_read_disk_num
; /* Which member disk in destSubMap[0] the
358 * migration ckpt record was read from
359 * (for recovered migrations) */
360 __u32 curr_migr_unit_hi
; /* 0..numMigrUnits-1 high order 32 bits */
361 __u32 ckpt_area_pba_hi
; /* Pba of first block of ckpt copy area
362 * high order 32 bits */
363 __u32 dest_1st_member_lba_hi
; /* First member lba on first stripe of
364 * destination - high order 32 bits */
365 __u32 num_migr_units_hi
; /* Total num migration units-of-op
366 * high order 32 bits */
369 ASSERT_SIZE(migr_record
, 128)
372 * enum imsm_status - internal IMSM return values representation.
373 * @STATUS_OK: function succeeded.
374 * @STATUS_ERROR: General error ocurred (not specified).
376 * Typedefed to imsm_status_t.
378 typedef enum imsm_status
{
379 IMSM_STATUS_ERROR
= -1,
386 * 2: metadata does not match
394 struct md_list
*next
;
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 * struct extent - reserved space details.
503 * @start: start offset.
504 * @size: size of reservation, set to 0 for metadata reservation.
505 * @vol: index of the volume, meaningful if &size is set.
508 unsigned long long start
, size
;
512 /* definitions of reshape process types */
513 enum imsm_reshape_type
{
519 /* definition of messages passed to imsm_process_update */
520 enum imsm_update_type
{
521 update_activate_spare
,
525 update_add_remove_disk
,
526 update_reshape_container_disks
,
527 update_reshape_migration
,
529 update_general_migration_checkpoint
,
531 update_prealloc_badblocks_mem
,
535 struct imsm_update_activate_spare
{
536 enum imsm_update_type type
;
540 struct imsm_update_activate_spare
*next
;
546 unsigned long long size
;
553 enum takeover_direction
{
557 struct imsm_update_takeover
{
558 enum imsm_update_type type
;
560 enum takeover_direction direction
;
563 struct imsm_update_reshape
{
564 enum imsm_update_type type
;
568 int new_disks
[1]; /* new_raid_disks - old_raid_disks makedev number */
571 struct imsm_update_reshape_migration
{
572 enum imsm_update_type type
;
575 /* fields for array migration changes
582 int new_disks
[1]; /* new_raid_disks - old_raid_disks makedev number */
585 struct imsm_update_size_change
{
586 enum imsm_update_type type
;
591 struct imsm_update_general_migration_checkpoint
{
592 enum imsm_update_type type
;
593 __u64 curr_migr_unit
;
597 __u8 serial
[MAX_RAID_SERIAL_LEN
];
600 struct imsm_update_create_array
{
601 enum imsm_update_type type
;
606 struct imsm_update_kill_array
{
607 enum imsm_update_type type
;
611 struct imsm_update_rename_array
{
612 enum imsm_update_type type
;
613 __u8 name
[MAX_RAID_SERIAL_LEN
];
617 struct imsm_update_add_remove_disk
{
618 enum imsm_update_type type
;
621 struct imsm_update_prealloc_bb_mem
{
622 enum imsm_update_type type
;
625 struct imsm_update_rwh_policy
{
626 enum imsm_update_type type
;
631 static const char *_sys_dev_type
[] = {
632 [SYS_DEV_UNKNOWN
] = "Unknown",
633 [SYS_DEV_SAS
] = "SAS",
634 [SYS_DEV_SATA
] = "SATA",
635 [SYS_DEV_NVME
] = "NVMe",
636 [SYS_DEV_VMD
] = "VMD",
637 [SYS_DEV_SATA_VMD
] = "SATA VMD"
640 static int no_platform
= -1;
642 static int check_no_platform(void)
644 static const char search
[] = "mdadm.imsm.test=1";
647 if (no_platform
>= 0)
650 if (check_env("IMSM_NO_PLATFORM")) {
654 fp
= fopen("/proc/cmdline", "r");
656 char *l
= conf_line(fp
);
665 if (strcmp(w
, search
) == 0)
671 if (no_platform
>= 0)
678 void imsm_set_no_platform(int v
)
683 const char *get_sys_dev_type(enum sys_dev_type type
)
685 if (type
>= SYS_DEV_MAX
)
686 type
= SYS_DEV_UNKNOWN
;
688 return _sys_dev_type
[type
];
691 static struct intel_hba
* alloc_intel_hba(struct sys_dev
*device
)
693 struct intel_hba
*result
= xmalloc(sizeof(*result
));
695 result
->type
= device
->type
;
696 result
->path
= xstrdup(device
->path
);
698 if (result
->path
&& (result
->pci_id
= strrchr(result
->path
, '/')) != NULL
)
704 static struct intel_hba
* find_intel_hba(struct intel_hba
*hba
, struct sys_dev
*device
)
706 struct intel_hba
*result
;
708 for (result
= hba
; result
; result
= result
->next
) {
709 if (result
->type
== device
->type
&& strcmp(result
->path
, device
->path
) == 0)
715 static int attach_hba_to_super(struct intel_super
*super
, struct sys_dev
*device
)
717 struct intel_hba
*hba
;
719 /* check if disk attached to Intel HBA */
720 hba
= find_intel_hba(super
->hba
, device
);
723 /* Check if HBA is already attached to super */
724 if (super
->hba
== NULL
) {
725 super
->hba
= alloc_intel_hba(device
);
730 /* Intel metadata allows for all disks attached to the same type HBA.
731 * Do not support HBA types mixing
733 if (device
->type
!= hba
->type
)
736 /* Multiple same type HBAs can be used if they share the same OROM */
737 const struct imsm_orom
*device_orom
= get_orom_by_device_id(device
->dev_id
);
739 if (device_orom
!= super
->orom
)
745 hba
->next
= alloc_intel_hba(device
);
749 static struct sys_dev
* find_disk_attached_hba(int fd
, const char *devname
)
751 struct sys_dev
*list
, *elem
;
754 if ((list
= find_intel_devices()) == NULL
)
757 if (!is_fd_valid(fd
))
758 disk_path
= (char *) devname
;
760 disk_path
= diskfd_to_devpath(fd
, 1, NULL
);
765 for (elem
= list
; elem
; elem
= elem
->next
)
766 if (path_attached_to_hba(disk_path
, elem
->path
))
769 if (disk_path
!= devname
)
775 static int find_intel_hba_capability(int fd
, struct intel_super
*super
,
778 static struct supertype
*match_metadata_desc_imsm(char *arg
)
780 struct supertype
*st
;
782 if (strcmp(arg
, "imsm") != 0 &&
783 strcmp(arg
, "default") != 0
787 st
= xcalloc(1, sizeof(*st
));
788 st
->ss
= &super_imsm
;
789 st
->max_devs
= IMSM_MAX_DEVICES
;
790 st
->minor_version
= 0;
795 static __u8
*get_imsm_version(struct imsm_super
*mpb
)
797 return &mpb
->sig
[MPB_SIG_LEN
];
800 /* retrieve a disk directly from the anchor when the anchor is known to be
801 * up-to-date, currently only at load time
803 static struct imsm_disk
*__get_imsm_disk(struct imsm_super
*mpb
, __u8 index
)
805 if (index
>= mpb
->num_disks
)
807 return &mpb
->disk
[index
];
810 /* retrieve the disk description based on a index of the disk
813 static struct dl
*get_imsm_dl_disk(struct intel_super
*super
, __u8 index
)
817 for (d
= super
->disks
; d
; d
= d
->next
)
818 if (d
->index
== index
)
823 /* retrieve a disk from the parsed metadata */
824 static struct imsm_disk
*get_imsm_disk(struct intel_super
*super
, __u8 index
)
828 dl
= get_imsm_dl_disk(super
, index
);
835 /* generate a checksum directly from the anchor when the anchor is known to be
836 * up-to-date, currently only at load or write_super after coalescing
838 static __u32
__gen_imsm_checksum(struct imsm_super
*mpb
)
840 __u32 end
= mpb
->mpb_size
/ sizeof(end
);
841 __u32
*p
= (__u32
*) mpb
;
845 sum
+= __le32_to_cpu(*p
);
849 return sum
- __le32_to_cpu(mpb
->check_sum
);
852 static size_t sizeof_imsm_map(struct imsm_map
*map
)
854 return sizeof(struct imsm_map
) + sizeof(__u32
) * (map
->num_members
- 1);
857 struct imsm_map
*get_imsm_map(struct imsm_dev
*dev
, int second_map
)
859 /* A device can have 2 maps if it is in the middle of a migration.
861 * MAP_0 - we return the first map
862 * MAP_1 - we return the second map if it exists, else NULL
863 * MAP_X - we return the second map if it exists, else the first
865 struct imsm_map
*map
= &dev
->vol
.map
[0];
866 struct imsm_map
*map2
= NULL
;
868 if (dev
->vol
.migr_state
)
869 map2
= (void *)map
+ sizeof_imsm_map(map
);
871 switch (second_map
) {
888 /* return the size of the device.
889 * migr_state increases the returned size if map[0] were to be duplicated
891 static size_t sizeof_imsm_dev(struct imsm_dev
*dev
, int migr_state
)
893 size_t size
= sizeof(*dev
) - sizeof(struct imsm_map
) +
894 sizeof_imsm_map(get_imsm_map(dev
, MAP_0
));
896 /* migrating means an additional map */
897 if (dev
->vol
.migr_state
)
898 size
+= sizeof_imsm_map(get_imsm_map(dev
, MAP_1
));
900 size
+= sizeof_imsm_map(get_imsm_map(dev
, MAP_0
));
905 /* retrieve disk serial number list from a metadata update */
906 static struct disk_info
*get_disk_info(struct imsm_update_create_array
*update
)
909 struct disk_info
*inf
;
911 inf
= u
+ sizeof(*update
) - sizeof(struct imsm_dev
) +
912 sizeof_imsm_dev(&update
->dev
, 0);
918 * __get_imsm_dev() - Get device with index from imsm_super.
919 * @mpb: &imsm_super pointer, not NULL.
920 * @index: Device index.
922 * Function works as non-NULL, aborting in such a case,
923 * when NULL would be returned.
925 * Device index should be in range 0 up to num_raid_devs.
926 * Function assumes the index was already verified.
927 * Index must be valid, otherwise abort() is called.
929 * Return: Pointer to corresponding imsm_dev.
932 static struct imsm_dev
*__get_imsm_dev(struct imsm_super
*mpb
, __u8 index
)
938 if (index
>= mpb
->num_raid_devs
)
941 /* devices start after all disks */
942 offset
= ((void *) &mpb
->disk
[mpb
->num_disks
]) - _mpb
;
944 for (i
= 0; i
<= index
; i
++, offset
+= sizeof_imsm_dev(_mpb
+ offset
, 0))
946 return _mpb
+ offset
;
948 pr_err("cannot find imsm_dev with index %u in imsm_super\n", index
);
953 * get_imsm_dev() - Get device with index from intel_super.
954 * @super: &intel_super pointer, not NULL.
955 * @index: Device index.
957 * Function works as non-NULL, aborting in such a case,
958 * when NULL would be returned.
960 * Device index should be in range 0 up to num_raid_devs.
961 * Function assumes the index was already verified.
962 * Index must be valid, otherwise abort() is called.
964 * Return: Pointer to corresponding imsm_dev.
967 static struct imsm_dev
*get_imsm_dev(struct intel_super
*super
, __u8 index
)
969 struct intel_dev
*dv
;
971 if (index
>= super
->anchor
->num_raid_devs
)
974 for (dv
= super
->devlist
; dv
; dv
= dv
->next
)
975 if (dv
->index
== index
)
978 pr_err("cannot find imsm_dev with index %u in intel_super\n", index
);
982 static inline unsigned long long __le48_to_cpu(const struct bbm_log_block_addr
985 return ((((__u64
)__le32_to_cpu(addr
->dw1
)) << 16) |
986 __le16_to_cpu(addr
->w1
));
989 static inline struct bbm_log_block_addr
__cpu_to_le48(unsigned long long sec
)
991 struct bbm_log_block_addr addr
;
993 addr
.w1
= __cpu_to_le16((__u16
)(sec
& 0xffff));
994 addr
.dw1
= __cpu_to_le32((__u32
)(sec
>> 16) & 0xffffffff);
998 /* get size of the bbm log */
999 static __u32
get_imsm_bbm_log_size(struct bbm_log
*log
)
1001 if (!log
|| log
->entry_count
== 0)
1004 return sizeof(log
->signature
) +
1005 sizeof(log
->entry_count
) +
1006 log
->entry_count
* sizeof(struct bbm_log_entry
);
1009 /* check if bad block is not partially stored in bbm log */
1010 static int is_stored_in_bbm(struct bbm_log
*log
, const __u8 idx
, const unsigned
1011 long long sector
, const int length
, __u32
*pos
)
1015 for (i
= *pos
; i
< log
->entry_count
; i
++) {
1016 struct bbm_log_entry
*entry
= &log
->marked_block_entries
[i
];
1017 unsigned long long bb_start
;
1018 unsigned long long bb_end
;
1020 bb_start
= __le48_to_cpu(&entry
->defective_block_start
);
1021 bb_end
= bb_start
+ (entry
->marked_count
+ 1);
1023 if ((entry
->disk_ordinal
== idx
) && (bb_start
>= sector
) &&
1024 (bb_end
<= sector
+ length
)) {
1032 /* record new bad block in bbm log */
1033 static int record_new_badblock(struct bbm_log
*log
, const __u8 idx
, unsigned
1034 long long sector
, int length
)
1038 struct bbm_log_entry
*entry
= NULL
;
1040 while (is_stored_in_bbm(log
, idx
, sector
, length
, &pos
)) {
1041 struct bbm_log_entry
*e
= &log
->marked_block_entries
[pos
];
1043 if ((e
->marked_count
+ 1 == BBM_LOG_MAX_LBA_ENTRY_VAL
) &&
1044 (__le48_to_cpu(&e
->defective_block_start
) == sector
)) {
1045 sector
+= BBM_LOG_MAX_LBA_ENTRY_VAL
;
1046 length
-= BBM_LOG_MAX_LBA_ENTRY_VAL
;
1055 int cnt
= (length
<= BBM_LOG_MAX_LBA_ENTRY_VAL
) ? length
:
1056 BBM_LOG_MAX_LBA_ENTRY_VAL
;
1057 entry
->defective_block_start
= __cpu_to_le48(sector
);
1058 entry
->marked_count
= cnt
- 1;
1065 new_bb
= ROUND_UP(length
, BBM_LOG_MAX_LBA_ENTRY_VAL
) /
1066 BBM_LOG_MAX_LBA_ENTRY_VAL
;
1067 if (log
->entry_count
+ new_bb
> BBM_LOG_MAX_ENTRIES
)
1070 while (length
> 0) {
1071 int cnt
= (length
<= BBM_LOG_MAX_LBA_ENTRY_VAL
) ? length
:
1072 BBM_LOG_MAX_LBA_ENTRY_VAL
;
1073 struct bbm_log_entry
*entry
=
1074 &log
->marked_block_entries
[log
->entry_count
];
1076 entry
->defective_block_start
= __cpu_to_le48(sector
);
1077 entry
->marked_count
= cnt
- 1;
1078 entry
->disk_ordinal
= idx
;
1089 /* clear all bad blocks for given disk */
1090 static void clear_disk_badblocks(struct bbm_log
*log
, const __u8 idx
)
1094 while (i
< log
->entry_count
) {
1095 struct bbm_log_entry
*entries
= log
->marked_block_entries
;
1097 if (entries
[i
].disk_ordinal
== idx
) {
1098 if (i
< log
->entry_count
- 1)
1099 entries
[i
] = entries
[log
->entry_count
- 1];
1107 /* clear given bad block */
1108 static int clear_badblock(struct bbm_log
*log
, const __u8 idx
, const unsigned
1109 long long sector
, const int length
) {
1112 while (i
< log
->entry_count
) {
1113 struct bbm_log_entry
*entries
= log
->marked_block_entries
;
1115 if ((entries
[i
].disk_ordinal
== idx
) &&
1116 (__le48_to_cpu(&entries
[i
].defective_block_start
) ==
1117 sector
) && (entries
[i
].marked_count
+ 1 == length
)) {
1118 if (i
< log
->entry_count
- 1)
1119 entries
[i
] = entries
[log
->entry_count
- 1];
1129 /* allocate and load BBM log from metadata */
1130 static int load_bbm_log(struct intel_super
*super
)
1132 struct imsm_super
*mpb
= super
->anchor
;
1133 __u32 bbm_log_size
= __le32_to_cpu(mpb
->bbm_log_size
);
1135 super
->bbm_log
= xcalloc(1, sizeof(struct bbm_log
));
1136 if (!super
->bbm_log
)
1140 struct bbm_log
*log
= (void *)mpb
+
1141 __le32_to_cpu(mpb
->mpb_size
) - bbm_log_size
;
1145 if (bbm_log_size
< sizeof(log
->signature
) +
1146 sizeof(log
->entry_count
))
1149 entry_count
= __le32_to_cpu(log
->entry_count
);
1150 if ((__le32_to_cpu(log
->signature
) != BBM_LOG_SIGNATURE
) ||
1151 (entry_count
> BBM_LOG_MAX_ENTRIES
))
1155 sizeof(log
->signature
) + sizeof(log
->entry_count
) +
1156 entry_count
* sizeof(struct bbm_log_entry
))
1159 memcpy(super
->bbm_log
, log
, bbm_log_size
);
1161 super
->bbm_log
->signature
= __cpu_to_le32(BBM_LOG_SIGNATURE
);
1162 super
->bbm_log
->entry_count
= 0;
1168 /* checks if bad block is within volume boundaries */
1169 static int is_bad_block_in_volume(const struct bbm_log_entry
*entry
,
1170 const unsigned long long start_sector
,
1171 const unsigned long long size
)
1173 unsigned long long bb_start
;
1174 unsigned long long bb_end
;
1176 bb_start
= __le48_to_cpu(&entry
->defective_block_start
);
1177 bb_end
= bb_start
+ (entry
->marked_count
+ 1);
1179 if (((bb_start
>= start_sector
) && (bb_start
< start_sector
+ size
)) ||
1180 ((bb_end
>= start_sector
) && (bb_end
<= start_sector
+ size
)))
1186 /* get list of bad blocks on a drive for a volume */
1187 static void get_volume_badblocks(const struct bbm_log
*log
, const __u8 idx
,
1188 const unsigned long long start_sector
,
1189 const unsigned long long size
,
1195 for (i
= 0; i
< log
->entry_count
; i
++) {
1196 const struct bbm_log_entry
*ent
=
1197 &log
->marked_block_entries
[i
];
1198 struct md_bb_entry
*bb
;
1200 if ((ent
->disk_ordinal
== idx
) &&
1201 is_bad_block_in_volume(ent
, start_sector
, size
)) {
1203 if (!bbs
->entries
) {
1204 bbs
->entries
= xmalloc(BBM_LOG_MAX_ENTRIES
*
1210 bb
= &bbs
->entries
[count
++];
1211 bb
->sector
= __le48_to_cpu(&ent
->defective_block_start
);
1212 bb
->length
= ent
->marked_count
+ 1;
1220 * == MAP_0 get first map
1221 * == MAP_1 get second map
1222 * == MAP_X than get map according to the current migr_state
1224 static __u32
get_imsm_ord_tbl_ent(struct imsm_dev
*dev
,
1228 struct imsm_map
*map
;
1230 map
= get_imsm_map(dev
, second_map
);
1232 /* top byte identifies disk under rebuild */
1233 return __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
1236 #define ord_to_idx(ord) (((ord) << 8) >> 8)
1237 static __u32
get_imsm_disk_idx(struct imsm_dev
*dev
, int slot
, int second_map
)
1239 __u32 ord
= get_imsm_ord_tbl_ent(dev
, slot
, second_map
);
1241 return ord_to_idx(ord
);
1244 static void set_imsm_ord_tbl_ent(struct imsm_map
*map
, int slot
, __u32 ord
)
1246 map
->disk_ord_tbl
[slot
] = __cpu_to_le32(ord
);
1249 static int get_imsm_disk_slot(struct imsm_map
*map
, const unsigned int idx
)
1254 for (slot
= 0; slot
< map
->num_members
; slot
++) {
1255 ord
= __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
1256 if (ord_to_idx(ord
) == idx
)
1260 return IMSM_STATUS_ERROR
;
1263 static int get_imsm_raid_level(struct imsm_map
*map
)
1265 if (map
->raid_level
== 1) {
1266 if (map
->num_members
== 2)
1272 return map
->raid_level
;
1276 * get_disk_slot_in_dev() - retrieve disk slot from &imsm_dev.
1277 * @super: &intel_super pointer, not NULL.
1278 * @dev_idx: imsm device index.
1281 * Return: Slot on success, IMSM_STATUS_ERROR otherwise.
1283 static int get_disk_slot_in_dev(struct intel_super
*super
, const __u8 dev_idx
,
1284 const unsigned int idx
)
1286 struct imsm_dev
*dev
= get_imsm_dev(super
, dev_idx
);
1287 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
1289 return get_imsm_disk_slot(map
, idx
);
1292 static int cmp_extent(const void *av
, const void *bv
)
1294 const struct extent
*a
= av
;
1295 const struct extent
*b
= bv
;
1296 if (a
->start
< b
->start
)
1298 if (a
->start
> b
->start
)
1303 static int count_memberships(struct dl
*dl
, struct intel_super
*super
)
1305 int memberships
= 0;
1308 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++)
1309 if (get_disk_slot_in_dev(super
, i
, dl
->index
) >= 0)
1315 static __u32
imsm_min_reserved_sectors(struct intel_super
*super
);
1317 static int split_ull(unsigned long long n
, void *lo
, void *hi
)
1319 if (lo
== 0 || hi
== 0)
1321 __put_unaligned32(__cpu_to_le32((__u32
)n
), lo
);
1322 __put_unaligned32(__cpu_to_le32((n
>> 32)), hi
);
1326 static unsigned long long join_u32(__u32 lo
, __u32 hi
)
1328 return (unsigned long long)__le32_to_cpu(lo
) |
1329 (((unsigned long long)__le32_to_cpu(hi
)) << 32);
1332 static unsigned long long total_blocks(struct imsm_disk
*disk
)
1336 return join_u32(disk
->total_blocks_lo
, disk
->total_blocks_hi
);
1340 * imsm_num_data_members() - get data drives count for an array.
1341 * @map: Map to analyze.
1343 * num_data_members value represents minimal count of drives for level.
1344 * The name of the property could be misleading for RAID5 with asymmetric layout
1345 * because some data required to be calculated from parity.
1346 * The property is extracted from level and num_members value.
1348 * Return: num_data_members value on success, zero otherwise.
1350 static __u8
imsm_num_data_members(struct imsm_map
*map
)
1352 switch (get_imsm_raid_level(map
)) {
1354 return map
->num_members
;
1357 return map
->num_members
/ 2;
1359 return map
->num_members
- 1;
1361 dprintf("unsupported raid level\n");
1366 static unsigned long long pba_of_lba0(struct imsm_map
*map
)
1370 return join_u32(map
->pba_of_lba0_lo
, map
->pba_of_lba0_hi
);
1373 static unsigned long long blocks_per_member(struct imsm_map
*map
)
1377 return join_u32(map
->blocks_per_member_lo
, map
->blocks_per_member_hi
);
1380 static unsigned long long num_data_stripes(struct imsm_map
*map
)
1384 return join_u32(map
->num_data_stripes_lo
, map
->num_data_stripes_hi
);
1387 static unsigned long long vol_curr_migr_unit(struct imsm_dev
*dev
)
1392 return join_u32(dev
->vol
.curr_migr_unit_lo
, dev
->vol
.curr_migr_unit_hi
);
1395 static unsigned long long imsm_dev_size(struct imsm_dev
*dev
)
1399 return join_u32(dev
->size_low
, dev
->size_high
);
1402 static unsigned long long migr_chkp_area_pba(struct migr_record
*migr_rec
)
1404 if (migr_rec
== NULL
)
1406 return join_u32(migr_rec
->ckpt_area_pba_lo
,
1407 migr_rec
->ckpt_area_pba_hi
);
1410 static unsigned long long current_migr_unit(struct migr_record
*migr_rec
)
1412 if (migr_rec
== NULL
)
1414 return join_u32(migr_rec
->curr_migr_unit_lo
,
1415 migr_rec
->curr_migr_unit_hi
);
1418 static unsigned long long migr_dest_1st_member_lba(struct migr_record
*migr_rec
)
1420 if (migr_rec
== NULL
)
1422 return join_u32(migr_rec
->dest_1st_member_lba_lo
,
1423 migr_rec
->dest_1st_member_lba_hi
);
1426 static unsigned long long get_num_migr_units(struct migr_record
*migr_rec
)
1428 if (migr_rec
== NULL
)
1430 return join_u32(migr_rec
->num_migr_units_lo
,
1431 migr_rec
->num_migr_units_hi
);
1434 static void set_total_blocks(struct imsm_disk
*disk
, unsigned long long n
)
1436 split_ull(n
, &disk
->total_blocks_lo
, &disk
->total_blocks_hi
);
1440 * set_num_domains() - Set number of domains for an array.
1441 * @map: Map to be updated.
1443 * num_domains property represents copies count of each data drive, thus make
1444 * it meaningful only for RAID1 and RAID10. IMSM supports two domains for
1447 static void set_num_domains(struct imsm_map
*map
)
1449 int level
= get_imsm_raid_level(map
);
1451 if (level
== 1 || level
== 10)
1452 map
->num_domains
= 2;
1454 map
->num_domains
= 1;
1457 static void set_pba_of_lba0(struct imsm_map
*map
, unsigned long long n
)
1459 split_ull(n
, &map
->pba_of_lba0_lo
, &map
->pba_of_lba0_hi
);
1462 static void set_blocks_per_member(struct imsm_map
*map
, unsigned long long n
)
1464 split_ull(n
, &map
->blocks_per_member_lo
, &map
->blocks_per_member_hi
);
1467 static void set_num_data_stripes(struct imsm_map
*map
, unsigned long long n
)
1469 split_ull(n
, &map
->num_data_stripes_lo
, &map
->num_data_stripes_hi
);
1473 * update_num_data_stripes() - Calculate and update num_data_stripes value.
1474 * @map: map to be updated.
1475 * @dev_size: size of volume.
1477 * num_data_stripes value is addictionally divided by num_domains, therefore for
1478 * levels where num_domains is not 1, nds is a part of real value.
1480 static void update_num_data_stripes(struct imsm_map
*map
,
1481 unsigned long long dev_size
)
1483 unsigned long long nds
= dev_size
/ imsm_num_data_members(map
);
1485 nds
/= map
->num_domains
;
1486 nds
/= map
->blocks_per_strip
;
1487 set_num_data_stripes(map
, nds
);
1490 static void set_vol_curr_migr_unit(struct imsm_dev
*dev
, unsigned long long n
)
1495 split_ull(n
, &dev
->vol
.curr_migr_unit_lo
, &dev
->vol
.curr_migr_unit_hi
);
1498 static void set_imsm_dev_size(struct imsm_dev
*dev
, unsigned long long n
)
1500 split_ull(n
, &dev
->size_low
, &dev
->size_high
);
1503 static void set_migr_chkp_area_pba(struct migr_record
*migr_rec
,
1504 unsigned long long n
)
1506 split_ull(n
, &migr_rec
->ckpt_area_pba_lo
, &migr_rec
->ckpt_area_pba_hi
);
1509 static void set_current_migr_unit(struct migr_record
*migr_rec
,
1510 unsigned long long n
)
1512 split_ull(n
, &migr_rec
->curr_migr_unit_lo
,
1513 &migr_rec
->curr_migr_unit_hi
);
1516 static void set_migr_dest_1st_member_lba(struct migr_record
*migr_rec
,
1517 unsigned long long n
)
1519 split_ull(n
, &migr_rec
->dest_1st_member_lba_lo
,
1520 &migr_rec
->dest_1st_member_lba_hi
);
1523 static void set_num_migr_units(struct migr_record
*migr_rec
,
1524 unsigned long long n
)
1526 split_ull(n
, &migr_rec
->num_migr_units_lo
,
1527 &migr_rec
->num_migr_units_hi
);
1530 static unsigned long long per_dev_array_size(struct imsm_map
*map
)
1532 unsigned long long array_size
= 0;
1537 array_size
= num_data_stripes(map
) * map
->blocks_per_strip
;
1538 if (get_imsm_raid_level(map
) == 1 || get_imsm_raid_level(map
) == 10)
1544 static struct extent
*get_extents(struct intel_super
*super
, struct dl
*dl
,
1545 int get_minimal_reservation
)
1547 /* find a list of used extents on the given physical device */
1548 int memberships
= count_memberships(dl
, super
);
1549 struct extent
*rv
= xcalloc(memberships
+ 1, sizeof(struct extent
));
1550 struct extent
*e
= rv
;
1554 /* trim the reserved area for spares, so they can join any array
1555 * regardless of whether the OROM has assigned sectors from the
1556 * IMSM_RESERVED_SECTORS region
1558 if (dl
->index
== -1 || get_minimal_reservation
)
1559 reservation
= imsm_min_reserved_sectors(super
);
1561 reservation
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
1563 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
1564 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
1565 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
1567 if (get_imsm_disk_slot(map
, dl
->index
) >= 0) {
1568 e
->start
= pba_of_lba0(map
);
1569 e
->size
= per_dev_array_size(map
);
1574 qsort(rv
, memberships
, sizeof(*rv
), cmp_extent
);
1576 /* determine the start of the metadata
1577 * when no raid devices are defined use the default
1578 * ...otherwise allow the metadata to truncate the value
1579 * as is the case with older versions of imsm
1582 struct extent
*last
= &rv
[memberships
- 1];
1583 unsigned long long remainder
;
1585 remainder
= total_blocks(&dl
->disk
) - (last
->start
+ last
->size
);
1586 /* round down to 1k block to satisfy precision of the kernel
1590 /* make sure remainder is still sane */
1591 if (remainder
< (unsigned)ROUND_UP(super
->len
, 512) >> 9)
1592 remainder
= ROUND_UP(super
->len
, 512) >> 9;
1593 if (reservation
> remainder
)
1594 reservation
= remainder
;
1596 e
->start
= total_blocks(&dl
->disk
) - reservation
;
1601 /* try to determine how much space is reserved for metadata from
1602 * the last get_extents() entry, otherwise fallback to the
1605 static __u32
imsm_reserved_sectors(struct intel_super
*super
, struct dl
*dl
)
1611 /* for spares just return a minimal reservation which will grow
1612 * once the spare is picked up by an array
1614 if (dl
->index
== -1)
1615 return MPB_SECTOR_CNT
;
1617 e
= get_extents(super
, dl
, 0);
1619 return MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
1621 /* scroll to last entry */
1622 for (i
= 0; e
[i
].size
; i
++)
1625 rv
= total_blocks(&dl
->disk
) - e
[i
].start
;
1632 static int is_spare(struct imsm_disk
*disk
)
1634 return (disk
->status
& SPARE_DISK
) == SPARE_DISK
;
1637 static int is_configured(struct imsm_disk
*disk
)
1639 return (disk
->status
& CONFIGURED_DISK
) == CONFIGURED_DISK
;
1642 static int is_failed(struct imsm_disk
*disk
)
1644 return (disk
->status
& FAILED_DISK
) == FAILED_DISK
;
1647 static int is_journal(struct imsm_disk
*disk
)
1649 return (disk
->status
& JOURNAL_DISK
) == JOURNAL_DISK
;
1653 * round_member_size_to_mb()- Round given size to closest MiB.
1654 * @size: size to round in sectors.
1656 static inline unsigned long long round_member_size_to_mb(unsigned long long size
)
1658 return (size
>> SECT_PER_MB_SHIFT
) << SECT_PER_MB_SHIFT
;
1662 * round_size_to_mb()- Round given size.
1663 * @array_size: size to round in sectors.
1664 * @disk_count: count of data members.
1666 * Get size per each data member and round it to closest MiB to ensure that data
1667 * splits evenly between members.
1669 * Return: Array size, rounded down.
1671 static inline unsigned long long round_size_to_mb(unsigned long long array_size
,
1672 unsigned int disk_count
)
1674 return round_member_size_to_mb(array_size
/ disk_count
) * disk_count
;
1677 static int able_to_resync(int raid_level
, int missing_disks
)
1679 int max_missing_disks
= 0;
1681 switch (raid_level
) {
1683 max_missing_disks
= 1;
1686 max_missing_disks
= 0;
1688 return missing_disks
<= max_missing_disks
;
1691 /* try to determine how much space is reserved for metadata from
1692 * the last get_extents() entry on the smallest active disk,
1693 * otherwise fallback to the default
1695 static __u32
imsm_min_reserved_sectors(struct intel_super
*super
)
1699 unsigned long long min_active
;
1701 __u32 rv
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
1702 struct dl
*dl
, *dl_min
= NULL
;
1708 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
1711 unsigned long long blocks
= total_blocks(&dl
->disk
);
1712 if (blocks
< min_active
|| min_active
== 0) {
1714 min_active
= blocks
;
1720 /* find last lba used by subarrays on the smallest active disk */
1721 e
= get_extents(super
, dl_min
, 0);
1724 for (i
= 0; e
[i
].size
; i
++)
1727 remainder
= min_active
- e
[i
].start
;
1730 /* to give priority to recovery we should not require full
1731 IMSM_RESERVED_SECTORS from the spare */
1732 rv
= MPB_SECTOR_CNT
+ NUM_BLOCKS_DIRTY_STRIPE_REGION
;
1734 /* if real reservation is smaller use that value */
1735 return (remainder
< rv
) ? remainder
: rv
;
1738 static bool is_gen_migration(struct imsm_dev
*dev
);
1740 #define IMSM_4K_DIV 8
1742 static __u64
blocks_per_migr_unit(struct intel_super
*super
,
1743 struct imsm_dev
*dev
);
1745 static void print_imsm_dev(struct intel_super
*super
,
1746 struct imsm_dev
*dev
,
1752 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
1753 struct imsm_map
*map2
= get_imsm_map(dev
, MAP_1
);
1757 printf("[%.16s]:\n", dev
->volume
);
1758 printf(" Subarray : %d\n", super
->current_vol
);
1759 printf(" UUID : %s\n", uuid
);
1760 printf(" RAID Level : %d", get_imsm_raid_level(map
));
1762 printf(" <-- %d", get_imsm_raid_level(map2
));
1764 printf(" Members : %d", map
->num_members
);
1766 printf(" <-- %d", map2
->num_members
);
1768 printf(" Slots : [");
1769 for (i
= 0; i
< map
->num_members
; i
++) {
1770 ord
= get_imsm_ord_tbl_ent(dev
, i
, MAP_0
);
1771 printf("%s", ord
& IMSM_ORD_REBUILD
? "_" : "U");
1776 for (i
= 0; i
< map2
->num_members
; i
++) {
1777 ord
= get_imsm_ord_tbl_ent(dev
, i
, MAP_1
);
1778 printf("%s", ord
& IMSM_ORD_REBUILD
? "_" : "U");
1783 printf(" Failed disk : ");
1784 if (map
->failed_disk_num
== 0xff)
1785 printf(STR_COMMON_NONE
);
1787 printf("%i", map
->failed_disk_num
);
1789 slot
= get_imsm_disk_slot(map
, disk_idx
);
1791 ord
= get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
);
1792 printf(" This Slot : %d%s\n", slot
,
1793 ord
& IMSM_ORD_REBUILD
? " (out-of-sync)" : "");
1795 printf(" This Slot : ?\n");
1796 printf(" Sector Size : %u\n", super
->sector_size
);
1797 sz
= imsm_dev_size(dev
);
1798 printf(" Array Size : %llu%s\n",
1799 (unsigned long long)sz
* 512 / super
->sector_size
,
1800 human_size(sz
* 512));
1801 sz
= blocks_per_member(map
);
1802 printf(" Per Dev Size : %llu%s\n",
1803 (unsigned long long)sz
* 512 / super
->sector_size
,
1804 human_size(sz
* 512));
1805 printf(" Sector Offset : %llu\n",
1806 pba_of_lba0(map
) * 512 / super
->sector_size
);
1807 printf(" Num Stripes : %llu\n",
1808 num_data_stripes(map
));
1809 printf(" Chunk Size : %u KiB",
1810 __le16_to_cpu(map
->blocks_per_strip
) / 2);
1812 printf(" <-- %u KiB",
1813 __le16_to_cpu(map2
->blocks_per_strip
) / 2);
1815 printf(" Reserved : %d\n", __le32_to_cpu(dev
->reserved_blocks
));
1816 printf(" Migrate State : ");
1817 if (dev
->vol
.migr_state
) {
1818 if (migr_type(dev
) == MIGR_INIT
)
1819 printf("initialize\n");
1820 else if (migr_type(dev
) == MIGR_REBUILD
)
1821 printf("rebuild\n");
1822 else if (migr_type(dev
) == MIGR_VERIFY
)
1824 else if (migr_type(dev
) == MIGR_GEN_MIGR
)
1825 printf("general migration\n");
1826 else if (migr_type(dev
) == MIGR_STATE_CHANGE
)
1827 printf("state change\n");
1828 else if (migr_type(dev
) == MIGR_REPAIR
)
1831 printf("<unknown:%d>\n", migr_type(dev
));
1834 printf(" Map State : %s", map_state_str
[map
->map_state
]);
1835 if (dev
->vol
.migr_state
) {
1836 struct imsm_map
*map
= get_imsm_map(dev
, MAP_1
);
1838 printf(" <-- %s", map_state_str
[map
->map_state
]);
1839 printf("\n Checkpoint : %llu ", vol_curr_migr_unit(dev
));
1840 if (is_gen_migration(dev
) && (slot
> 1 || slot
< 0))
1843 printf("(%llu)", (unsigned long long)
1844 blocks_per_migr_unit(super
, dev
));
1847 printf(" Dirty State : %s\n", (dev
->vol
.dirty
& RAIDVOL_DIRTY
) ?
1849 printf(" RWH Policy : ");
1850 if (dev
->rwh_policy
== RWH_OFF
|| dev
->rwh_policy
== RWH_MULTIPLE_OFF
)
1852 else if (dev
->rwh_policy
== RWH_DISTRIBUTED
)
1853 printf("PPL distributed\n");
1854 else if (dev
->rwh_policy
== RWH_JOURNALING_DRIVE
)
1855 printf("PPL journaling drive\n");
1856 else if (dev
->rwh_policy
== RWH_MULTIPLE_DISTRIBUTED
)
1857 printf("Multiple distributed PPLs\n");
1858 else if (dev
->rwh_policy
== RWH_MULTIPLE_PPLS_JOURNALING_DRIVE
)
1859 printf("Multiple PPLs on journaling drive\n");
1860 else if (dev
->rwh_policy
== RWH_BITMAP
)
1861 printf("Write-intent bitmap\n");
1863 printf("<unknown:%d>\n", dev
->rwh_policy
);
1865 printf(" Volume ID : %u\n", dev
->my_vol_raid_dev_num
);
1868 static void print_imsm_disk(struct imsm_disk
*disk
,
1871 unsigned int sector_size
) {
1872 char str
[MAX_RAID_SERIAL_LEN
+ 1];
1875 if (index
< -1 || !disk
)
1879 snprintf(str
, MAX_RAID_SERIAL_LEN
+ 1, "%s", disk
->serial
);
1881 printf(" Disk%02d Serial : %s\n", index
, str
);
1883 printf(" Disk Serial : %s\n", str
);
1884 printf(" State :%s%s%s%s\n", is_spare(disk
) ? " spare" : "",
1885 is_configured(disk
) ? " active" : "",
1886 is_failed(disk
) ? " failed" : "",
1887 is_journal(disk
) ? " journal" : "");
1888 printf(" Id : %08x\n", __le32_to_cpu(disk
->scsi_id
));
1889 sz
= total_blocks(disk
) - reserved
;
1890 printf(" Usable Size : %llu%s\n",
1891 (unsigned long long)sz
* 512 / sector_size
,
1892 human_size(sz
* 512));
1895 void convert_to_4k_imsm_migr_rec(struct intel_super
*super
)
1897 struct migr_record
*migr_rec
= super
->migr_rec
;
1899 migr_rec
->blocks_per_unit
/= IMSM_4K_DIV
;
1900 migr_rec
->dest_depth_per_unit
/= IMSM_4K_DIV
;
1901 split_ull((join_u32(migr_rec
->post_migr_vol_cap
,
1902 migr_rec
->post_migr_vol_cap_hi
) / IMSM_4K_DIV
),
1903 &migr_rec
->post_migr_vol_cap
, &migr_rec
->post_migr_vol_cap_hi
);
1904 set_migr_chkp_area_pba(migr_rec
,
1905 migr_chkp_area_pba(migr_rec
) / IMSM_4K_DIV
);
1906 set_migr_dest_1st_member_lba(migr_rec
,
1907 migr_dest_1st_member_lba(migr_rec
) / IMSM_4K_DIV
);
1910 void convert_to_4k_imsm_disk(struct imsm_disk
*disk
)
1912 set_total_blocks(disk
, (total_blocks(disk
)/IMSM_4K_DIV
));
1915 void convert_to_4k(struct intel_super
*super
)
1917 struct imsm_super
*mpb
= super
->anchor
;
1918 struct imsm_disk
*disk
;
1920 __u32 bbm_log_size
= __le32_to_cpu(mpb
->bbm_log_size
);
1922 for (i
= 0; i
< mpb
->num_disks
; i
++) {
1923 disk
= __get_imsm_disk(mpb
, i
);
1925 convert_to_4k_imsm_disk(disk
);
1927 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
1928 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
1929 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
1931 set_imsm_dev_size(dev
, imsm_dev_size(dev
)/IMSM_4K_DIV
);
1932 set_vol_curr_migr_unit(dev
,
1933 vol_curr_migr_unit(dev
) / IMSM_4K_DIV
);
1936 set_blocks_per_member(map
, blocks_per_member(map
)/IMSM_4K_DIV
);
1937 map
->blocks_per_strip
/= IMSM_4K_DIV
;
1938 set_pba_of_lba0(map
, pba_of_lba0(map
)/IMSM_4K_DIV
);
1940 if (dev
->vol
.migr_state
) {
1942 map
= get_imsm_map(dev
, MAP_1
);
1943 set_blocks_per_member(map
,
1944 blocks_per_member(map
)/IMSM_4K_DIV
);
1945 map
->blocks_per_strip
/= IMSM_4K_DIV
;
1946 set_pba_of_lba0(map
, pba_of_lba0(map
)/IMSM_4K_DIV
);
1950 struct bbm_log
*log
= (void *)mpb
+
1951 __le32_to_cpu(mpb
->mpb_size
) - bbm_log_size
;
1954 for (i
= 0; i
< log
->entry_count
; i
++) {
1955 struct bbm_log_entry
*entry
=
1956 &log
->marked_block_entries
[i
];
1958 __u8 count
= entry
->marked_count
+ 1;
1959 unsigned long long sector
=
1960 __le48_to_cpu(&entry
->defective_block_start
);
1962 entry
->defective_block_start
=
1963 __cpu_to_le48(sector
/IMSM_4K_DIV
);
1964 entry
->marked_count
= max(count
/IMSM_4K_DIV
, 1) - 1;
1968 mpb
->check_sum
= __gen_imsm_checksum(mpb
);
1971 void examine_migr_rec_imsm(struct intel_super
*super
)
1973 struct migr_record
*migr_rec
= super
->migr_rec
;
1974 struct imsm_super
*mpb
= super
->anchor
;
1977 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
1978 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
1979 struct imsm_map
*map
;
1982 if (is_gen_migration(dev
) == false)
1985 printf("\nMigration Record Information:");
1987 /* first map under migration */
1988 map
= get_imsm_map(dev
, MAP_0
);
1991 slot
= get_imsm_disk_slot(map
, super
->disks
->index
);
1992 if (map
== NULL
|| slot
> 1 || slot
< 0) {
1993 printf(" Empty\n ");
1994 printf("Examine one of first two disks in array\n");
1997 printf("\n Status : ");
1998 if (__le32_to_cpu(migr_rec
->rec_status
) == UNIT_SRC_NORMAL
)
2001 printf("Contains Data\n");
2002 printf(" Current Unit : %llu\n",
2003 current_migr_unit(migr_rec
));
2004 printf(" Family : %u\n",
2005 __le32_to_cpu(migr_rec
->family_num
));
2006 printf(" Ascending : %u\n",
2007 __le32_to_cpu(migr_rec
->ascending_migr
));
2008 printf(" Blocks Per Unit : %u\n",
2009 __le32_to_cpu(migr_rec
->blocks_per_unit
));
2010 printf(" Dest. Depth Per Unit : %u\n",
2011 __le32_to_cpu(migr_rec
->dest_depth_per_unit
));
2012 printf(" Checkpoint Area pba : %llu\n",
2013 migr_chkp_area_pba(migr_rec
));
2014 printf(" First member lba : %llu\n",
2015 migr_dest_1st_member_lba(migr_rec
));
2016 printf(" Total Number of Units : %llu\n",
2017 get_num_migr_units(migr_rec
));
2018 printf(" Size of volume : %llu\n",
2019 join_u32(migr_rec
->post_migr_vol_cap
,
2020 migr_rec
->post_migr_vol_cap_hi
));
2021 printf(" Record was read from : %u\n",
2022 __le32_to_cpu(migr_rec
->ckpt_read_disk_num
));
2028 void convert_from_4k_imsm_migr_rec(struct intel_super
*super
)
2030 struct migr_record
*migr_rec
= super
->migr_rec
;
2032 migr_rec
->blocks_per_unit
*= IMSM_4K_DIV
;
2033 migr_rec
->dest_depth_per_unit
*= IMSM_4K_DIV
;
2034 split_ull((join_u32(migr_rec
->post_migr_vol_cap
,
2035 migr_rec
->post_migr_vol_cap_hi
) * IMSM_4K_DIV
),
2036 &migr_rec
->post_migr_vol_cap
,
2037 &migr_rec
->post_migr_vol_cap_hi
);
2038 set_migr_chkp_area_pba(migr_rec
,
2039 migr_chkp_area_pba(migr_rec
) * IMSM_4K_DIV
);
2040 set_migr_dest_1st_member_lba(migr_rec
,
2041 migr_dest_1st_member_lba(migr_rec
) * IMSM_4K_DIV
);
2044 void convert_from_4k(struct intel_super
*super
)
2046 struct imsm_super
*mpb
= super
->anchor
;
2047 struct imsm_disk
*disk
;
2049 __u32 bbm_log_size
= __le32_to_cpu(mpb
->bbm_log_size
);
2051 for (i
= 0; i
< mpb
->num_disks
; i
++) {
2052 disk
= __get_imsm_disk(mpb
, i
);
2054 set_total_blocks(disk
, (total_blocks(disk
)*IMSM_4K_DIV
));
2057 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
2058 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
2059 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
2061 set_imsm_dev_size(dev
, imsm_dev_size(dev
)*IMSM_4K_DIV
);
2062 set_vol_curr_migr_unit(dev
,
2063 vol_curr_migr_unit(dev
) * IMSM_4K_DIV
);
2066 set_blocks_per_member(map
, blocks_per_member(map
)*IMSM_4K_DIV
);
2067 map
->blocks_per_strip
*= IMSM_4K_DIV
;
2068 set_pba_of_lba0(map
, pba_of_lba0(map
)*IMSM_4K_DIV
);
2070 if (dev
->vol
.migr_state
) {
2072 map
= get_imsm_map(dev
, MAP_1
);
2073 set_blocks_per_member(map
,
2074 blocks_per_member(map
)*IMSM_4K_DIV
);
2075 map
->blocks_per_strip
*= IMSM_4K_DIV
;
2076 set_pba_of_lba0(map
, pba_of_lba0(map
)*IMSM_4K_DIV
);
2080 struct bbm_log
*log
= (void *)mpb
+
2081 __le32_to_cpu(mpb
->mpb_size
) - bbm_log_size
;
2084 for (i
= 0; i
< log
->entry_count
; i
++) {
2085 struct bbm_log_entry
*entry
=
2086 &log
->marked_block_entries
[i
];
2088 __u8 count
= entry
->marked_count
+ 1;
2089 unsigned long long sector
=
2090 __le48_to_cpu(&entry
->defective_block_start
);
2092 entry
->defective_block_start
=
2093 __cpu_to_le48(sector
*IMSM_4K_DIV
);
2094 entry
->marked_count
= count
*IMSM_4K_DIV
- 1;
2098 mpb
->check_sum
= __gen_imsm_checksum(mpb
);
2101 /*******************************************************************************
2102 * function: imsm_check_attributes
2103 * Description: Function checks if features represented by attributes flags
2104 * are supported by mdadm.
2106 * attributes - Attributes read from metadata
2108 * 0 - passed attributes contains unsupported features flags
2109 * 1 - all features are supported
2110 ******************************************************************************/
2111 static int imsm_check_attributes(__u32 attributes
)
2114 __u32 not_supported
= MPB_ATTRIB_SUPPORTED
^0xffffffff;
2116 not_supported
&= ~MPB_ATTRIB_IGNORED
;
2118 not_supported
&= attributes
;
2119 if (not_supported
) {
2120 pr_err("(IMSM): Unsupported attributes : %x\n",
2121 (unsigned)__le32_to_cpu(not_supported
));
2122 if (not_supported
& MPB_ATTRIB_CHECKSUM_VERIFY
) {
2123 dprintf("\t\tMPB_ATTRIB_CHECKSUM_VERIFY \n");
2124 not_supported
^= MPB_ATTRIB_CHECKSUM_VERIFY
;
2126 if (not_supported
& MPB_ATTRIB_2TB
) {
2127 dprintf("\t\tMPB_ATTRIB_2TB\n");
2128 not_supported
^= MPB_ATTRIB_2TB
;
2130 if (not_supported
& MPB_ATTRIB_RAID0
) {
2131 dprintf("\t\tMPB_ATTRIB_RAID0\n");
2132 not_supported
^= MPB_ATTRIB_RAID0
;
2134 if (not_supported
& MPB_ATTRIB_RAID1
) {
2135 dprintf("\t\tMPB_ATTRIB_RAID1\n");
2136 not_supported
^= MPB_ATTRIB_RAID1
;
2138 if (not_supported
& MPB_ATTRIB_RAID10
) {
2139 dprintf("\t\tMPB_ATTRIB_RAID10\n");
2140 not_supported
^= MPB_ATTRIB_RAID10
;
2142 if (not_supported
& MPB_ATTRIB_RAID1E
) {
2143 dprintf("\t\tMPB_ATTRIB_RAID1E\n");
2144 not_supported
^= MPB_ATTRIB_RAID1E
;
2146 if (not_supported
& MPB_ATTRIB_RAID5
) {
2147 dprintf("\t\tMPB_ATTRIB_RAID5\n");
2148 not_supported
^= MPB_ATTRIB_RAID5
;
2150 if (not_supported
& MPB_ATTRIB_RAIDCNG
) {
2151 dprintf("\t\tMPB_ATTRIB_RAIDCNG\n");
2152 not_supported
^= MPB_ATTRIB_RAIDCNG
;
2154 if (not_supported
& MPB_ATTRIB_BBM
) {
2155 dprintf("\t\tMPB_ATTRIB_BBM\n");
2156 not_supported
^= MPB_ATTRIB_BBM
;
2158 if (not_supported
& MPB_ATTRIB_CHECKSUM_VERIFY
) {
2159 dprintf("\t\tMPB_ATTRIB_CHECKSUM_VERIFY (== MPB_ATTRIB_LEGACY)\n");
2160 not_supported
^= MPB_ATTRIB_CHECKSUM_VERIFY
;
2162 if (not_supported
& MPB_ATTRIB_EXP_STRIPE_SIZE
) {
2163 dprintf("\t\tMPB_ATTRIB_EXP_STRIP_SIZE\n");
2164 not_supported
^= MPB_ATTRIB_EXP_STRIPE_SIZE
;
2166 if (not_supported
& MPB_ATTRIB_2TB_DISK
) {
2167 dprintf("\t\tMPB_ATTRIB_2TB_DISK\n");
2168 not_supported
^= MPB_ATTRIB_2TB_DISK
;
2170 if (not_supported
& MPB_ATTRIB_NEVER_USE2
) {
2171 dprintf("\t\tMPB_ATTRIB_NEVER_USE2\n");
2172 not_supported
^= MPB_ATTRIB_NEVER_USE2
;
2174 if (not_supported
& MPB_ATTRIB_NEVER_USE
) {
2175 dprintf("\t\tMPB_ATTRIB_NEVER_USE\n");
2176 not_supported
^= MPB_ATTRIB_NEVER_USE
;
2180 dprintf("(IMSM): Unknown attributes : %x\n", not_supported
);
2188 static void getinfo_super_imsm(struct supertype
*st
, struct mdinfo
*info
, char *map
);
2190 static void examine_super_imsm(struct supertype
*st
, char *homehost
)
2192 struct intel_super
*super
= st
->sb
;
2193 struct imsm_super
*mpb
= super
->anchor
;
2194 char str
[MAX_SIGNATURE_LENGTH
];
2199 __u32 reserved
= imsm_reserved_sectors(super
, super
->disks
);
2201 time_t creation_time
;
2203 strncpy(str
, (char *)mpb
->sig
, MPB_SIG_LEN
);
2204 str
[MPB_SIG_LEN
-1] = '\0';
2205 printf(" Magic : %s\n", str
);
2206 printf(" Version : %s\n", get_imsm_version(mpb
));
2207 printf(" Orig Family : %08x\n", __le32_to_cpu(mpb
->orig_family_num
));
2208 printf(" Family : %08x\n", __le32_to_cpu(mpb
->family_num
));
2209 printf(" Generation : %08x\n", __le32_to_cpu(mpb
->generation_num
));
2210 creation_time
= __le64_to_cpu(mpb
->creation_time
);
2211 printf(" Creation Time : %.24s\n",
2212 creation_time
? ctime(&creation_time
) : "Unknown");
2213 printf(" Attributes : ");
2214 if (imsm_check_attributes(mpb
->attributes
))
2215 printf("All supported\n");
2217 printf("not supported\n");
2218 getinfo_super_imsm(st
, &info
, NULL
);
2219 fname_from_uuid(&info
, nbuf
);
2220 printf(" UUID : %s\n", nbuf
+ 5);
2221 sum
= __le32_to_cpu(mpb
->check_sum
);
2222 printf(" Checksum : %08x %s\n", sum
,
2223 __gen_imsm_checksum(mpb
) == sum
? "correct" : "incorrect");
2224 printf(" MPB Sectors : %d\n", mpb_sectors(mpb
, super
->sector_size
));
2225 printf(" Disks : %d\n", mpb
->num_disks
);
2226 printf(" RAID Devices : %d\n", mpb
->num_raid_devs
);
2227 print_imsm_disk(__get_imsm_disk(mpb
, super
->disks
->index
),
2228 super
->disks
->index
, reserved
, super
->sector_size
);
2229 if (get_imsm_bbm_log_size(super
->bbm_log
)) {
2230 struct bbm_log
*log
= super
->bbm_log
;
2233 printf("Bad Block Management Log:\n");
2234 printf(" Log Size : %d\n", __le32_to_cpu(mpb
->bbm_log_size
));
2235 printf(" Signature : %x\n", __le32_to_cpu(log
->signature
));
2236 printf(" Entry Count : %d\n", __le32_to_cpu(log
->entry_count
));
2238 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
2240 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
2242 super
->current_vol
= i
;
2243 getinfo_super_imsm(st
, &info
, NULL
);
2244 fname_from_uuid(&info
, nbuf
);
2245 print_imsm_dev(super
, dev
, nbuf
+ 5, super
->disks
->index
);
2247 for (i
= 0; i
< mpb
->num_disks
; i
++) {
2248 if (i
== super
->disks
->index
)
2250 print_imsm_disk(__get_imsm_disk(mpb
, i
), i
, reserved
,
2251 super
->sector_size
);
2254 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
2255 if (dl
->index
== -1)
2256 print_imsm_disk(&dl
->disk
, -1, reserved
,
2257 super
->sector_size
);
2259 examine_migr_rec_imsm(super
);
2262 static void brief_examine_super_imsm(struct supertype
*st
, int verbose
)
2264 /* We just write a generic IMSM ARRAY entry */
2268 getinfo_super_imsm(st
, &info
, NULL
);
2269 fname_from_uuid(&info
, nbuf
);
2270 printf("ARRAY metadata=imsm UUID=%s\n", nbuf
+ 5);
2273 static void brief_examine_subarrays_imsm(struct supertype
*st
, int verbose
)
2275 /* We just write a generic IMSM ARRAY entry */
2279 struct intel_super
*super
= st
->sb
;
2282 if (!super
->anchor
->num_raid_devs
)
2285 getinfo_super_imsm(st
, &info
, NULL
);
2286 fname_from_uuid(&info
, nbuf
);
2287 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
2288 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
2290 super
->current_vol
= i
;
2291 getinfo_super_imsm(st
, &info
, NULL
);
2292 fname_from_uuid(&info
, nbuf1
);
2293 printf("ARRAY " DEV_MD_DIR
"%.16s container=%s member=%d UUID=%s\n",
2294 dev
->volume
, nbuf
+ 5, i
, nbuf1
+ 5);
2298 static void export_examine_super_imsm(struct supertype
*st
)
2300 struct intel_super
*super
= st
->sb
;
2301 struct imsm_super
*mpb
= super
->anchor
;
2305 getinfo_super_imsm(st
, &info
, NULL
);
2306 fname_from_uuid(&info
, nbuf
);
2307 printf("MD_METADATA=imsm\n");
2308 printf("MD_LEVEL=container\n");
2309 printf("MD_UUID=%s\n", nbuf
+5);
2310 printf("MD_DEVICES=%u\n", mpb
->num_disks
);
2311 printf("MD_CREATION_TIME=%llu\n", __le64_to_cpu(mpb
->creation_time
));
2314 static void detail_super_imsm(struct supertype
*st
, char *homehost
,
2319 struct intel_super
*super
= st
->sb
;
2320 int temp_vol
= super
->current_vol
;
2323 super
->current_vol
= strtoul(subarray
, NULL
, 10);
2325 getinfo_super_imsm(st
, &info
, NULL
);
2326 fname_from_uuid(&info
, nbuf
);
2327 printf("\n UUID : %s\n", nbuf
+ 5);
2329 super
->current_vol
= temp_vol
;
2332 static void brief_detail_super_imsm(struct supertype
*st
, char *subarray
)
2336 struct intel_super
*super
= st
->sb
;
2337 int temp_vol
= super
->current_vol
;
2340 super
->current_vol
= strtoul(subarray
, NULL
, 10);
2342 getinfo_super_imsm(st
, &info
, NULL
);
2343 fname_from_uuid(&info
, nbuf
);
2344 printf(" UUID=%s", nbuf
+ 5);
2346 super
->current_vol
= temp_vol
;
2349 static int imsm_read_serial(int fd
, char *devname
, __u8
*serial
,
2350 size_t serial_buf_len
);
2351 static void fd2devname(int fd
, char *name
);
2353 void print_encryption_information(int disk_fd
, enum sys_dev_type hba_type
)
2355 struct encryption_information information
= {0};
2356 mdadm_status_t status
= MDADM_STATUS_SUCCESS
;
2357 const char *indent
= " ";
2362 status
= get_nvme_opal_encryption_information(disk_fd
, &information
, 1);
2365 case SYS_DEV_SATA_VMD
:
2366 status
= get_ata_encryption_information(disk_fd
, &information
, 1);
2373 pr_err("Failed to get drive encryption information.\n");
2377 printf("%sEncryption(Ability|Status): %s|%s\n", indent
,
2378 get_encryption_ability_string(information
.ability
),
2379 get_encryption_status_string(information
.status
));
2382 static int ahci_enumerate_ports(struct sys_dev
*hba
, int port_count
, int host_base
, int verbose
)
2384 /* dump an unsorted list of devices attached to AHCI Intel storage
2385 * controller, as well as non-connected ports
2387 int hba_len
= strlen(hba
->path
) + 1;
2392 unsigned long port_mask
= (1 << port_count
) - 1;
2394 if (port_count
> (int)sizeof(port_mask
) * 8) {
2396 pr_err("port_count %d out of range\n", port_count
);
2400 /* scroll through /sys/dev/block looking for devices attached to
2403 dir
= opendir("/sys/dev/block");
2407 for (ent
= readdir(dir
); ent
; ent
= readdir(dir
)) {
2413 char device
[PATH_MAX
];
2418 if (sscanf(ent
->d_name
, "%d:%d", &major
, &minor
) != 2)
2420 path
= devt_to_devpath(makedev(major
, minor
), 1, NULL
);
2423 if (!path_attached_to_hba(path
, hba
->path
)) {
2429 /* retrieve the scsi device */
2430 if (!devt_to_devpath(makedev(major
, minor
), 1, device
)) {
2432 pr_err("failed to get device\n");
2436 if (devpath_to_char(device
, "type", buf
, sizeof(buf
), 0)) {
2440 type
= strtoul(buf
, NULL
, 10);
2442 /* if it's not a disk print the vendor and model */
2443 if (!(type
== 0 || type
== 7 || type
== 14)) {
2447 if (devpath_to_char(device
, "vendor", buf
,
2448 sizeof(buf
), 0) == 0) {
2449 strncpy(vendor
, buf
, sizeof(vendor
));
2450 vendor
[sizeof(vendor
) - 1] = '\0';
2451 c
= (char *) &vendor
[sizeof(vendor
) - 1];
2452 while (isspace(*c
) || *c
== '\0')
2457 if (devpath_to_char(device
, "model", buf
,
2458 sizeof(buf
), 0) == 0) {
2459 strncpy(model
, buf
, sizeof(model
));
2460 model
[sizeof(model
) - 1] = '\0';
2461 c
= (char *) &model
[sizeof(model
) - 1];
2462 while (isspace(*c
) || *c
== '\0')
2466 if (vendor
[0] && model
[0])
2467 sprintf(buf
, "%.64s %.64s", vendor
, model
);
2469 switch (type
) { /* numbers from hald/linux/device.c */
2470 case 1: sprintf(buf
, "tape"); break;
2471 case 2: sprintf(buf
, "printer"); break;
2472 case 3: sprintf(buf
, "processor"); break;
2474 case 5: sprintf(buf
, "cdrom"); break;
2475 case 6: sprintf(buf
, "scanner"); break;
2476 case 8: sprintf(buf
, "media_changer"); break;
2477 case 9: sprintf(buf
, "comm"); break;
2478 case 12: sprintf(buf
, "raid"); break;
2479 default: sprintf(buf
, "unknown");
2484 /* chop device path to 'host%d' and calculate the port number */
2485 c
= strchr(&path
[hba_len
], '/');
2488 pr_err("%s - invalid path name\n", path
+ hba_len
);
2493 if ((sscanf(&path
[hba_len
], "ata%d", &port
) == 1) ||
2494 ((sscanf(&path
[hba_len
], "host%d", &port
) == 1)))
2498 *c
= '/'; /* repair the full string */
2499 pr_err("failed to determine port number for %s\n",
2506 /* mark this port as used */
2507 port_mask
&= ~(1 << port
);
2509 /* print out the device information */
2511 printf(" Port%d : - non-disk device (%s) -\n", port
, buf
);
2515 fd
= dev_open(ent
->d_name
, O_RDONLY
);
2516 if (!is_fd_valid(fd
))
2517 printf(" Port%d : - disk info unavailable -\n", port
);
2519 fd2devname(fd
, buf
);
2520 printf(" Port%d : %s", port
, buf
);
2521 if (imsm_read_serial(fd
, NULL
, (__u8
*)buf
,
2523 printf(" (%s)\n", buf
);
2527 print_encryption_information(fd
, hba
->type
);
2540 for (i
= 0; i
< port_count
; i
++)
2541 if (port_mask
& (1 << i
))
2542 printf(" Port%d : - no device attached -\n", i
);
2548 static int print_nvme_info(struct sys_dev
*hba
)
2553 dir
= opendir("/sys/block/");
2557 for (ent
= readdir(dir
); ent
; ent
= readdir(dir
)) {
2558 char ns_path
[PATH_MAX
];
2559 char cntrl_path
[PATH_MAX
];
2563 if (!strstr(ent
->d_name
, "nvme"))
2566 fd
= open_dev(ent
->d_name
);
2567 if (!is_fd_valid(fd
))
2570 if (!diskfd_to_devpath(fd
, 0, ns_path
) ||
2571 !diskfd_to_devpath(fd
, 1, cntrl_path
))
2574 if (!path_attached_to_hba(cntrl_path
, hba
->path
))
2577 if (!imsm_is_nvme_namespace_supported(fd
, 0))
2580 fd2devname(fd
, buf
);
2581 if (hba
->type
== SYS_DEV_VMD
)
2582 printf(" NVMe under VMD : %s", buf
);
2583 else if (hba
->type
== SYS_DEV_NVME
)
2584 printf(" NVMe Device : %s", buf
);
2586 if (!imsm_read_serial(fd
, NULL
, (__u8
*)buf
,
2588 printf(" (%s)\n", buf
);
2592 print_encryption_information(fd
, hba
->type
);
2602 static void print_found_intel_controllers(struct sys_dev
*elem
)
2604 for (; elem
; elem
= elem
->next
) {
2605 pr_err("found Intel(R) ");
2606 if (elem
->type
== SYS_DEV_SATA
)
2607 fprintf(stderr
, "SATA ");
2608 else if (elem
->type
== SYS_DEV_SAS
)
2609 fprintf(stderr
, "SAS ");
2610 else if (elem
->type
== SYS_DEV_NVME
)
2611 fprintf(stderr
, "NVMe ");
2613 if (elem
->type
== SYS_DEV_VMD
)
2614 fprintf(stderr
, "VMD domain");
2615 else if (elem
->type
== SYS_DEV_SATA_VMD
)
2616 fprintf(stderr
, "SATA VMD domain");
2618 fprintf(stderr
, "RAID controller");
2621 fprintf(stderr
, " at %s", elem
->pci_id
);
2622 fprintf(stderr
, ".\n");
2627 static int ahci_get_port_count(const char *hba_path
, int *port_count
)
2634 if ((dir
= opendir(hba_path
)) == NULL
)
2637 for (ent
= readdir(dir
); ent
; ent
= readdir(dir
)) {
2640 if ((sscanf(ent
->d_name
, "ata%d", &host
) != 1) &&
2641 ((sscanf(ent
->d_name
, "host%d", &host
) != 1)))
2643 if (*port_count
== 0)
2645 else if (host
< host_base
)
2648 if (host
+ 1 > *port_count
+ host_base
)
2649 *port_count
= host
+ 1 - host_base
;
2655 static void print_imsm_capability(const struct imsm_orom
*orom
)
2657 printf(" Platform : Intel(R) ");
2658 if (orom
->capabilities
== 0 && orom
->driver_features
== 0)
2659 printf("Matrix Storage Manager\n");
2660 else if (imsm_orom_is_enterprise(orom
) && orom
->major_ver
>= 6)
2661 printf("Virtual RAID on CPU\n");
2663 printf("Rapid Storage Technology%s\n",
2664 imsm_orom_is_enterprise(orom
) ? " enterprise" : "");
2665 if (orom
->major_ver
|| orom
->minor_ver
|| orom
->hotfix_ver
|| orom
->build
) {
2666 if (imsm_orom_is_vmd_without_efi(orom
))
2667 printf(" Version : %d.%d\n", orom
->major_ver
,
2670 printf(" Version : %d.%d.%d.%d\n", orom
->major_ver
,
2671 orom
->minor_ver
, orom
->hotfix_ver
, orom
->build
);
2673 printf(" RAID Levels :%s%s%s%s%s\n",
2674 imsm_orom_has_raid0(orom
) ? " raid0" : "",
2675 imsm_orom_has_raid1(orom
) ? " raid1" : "",
2676 imsm_orom_has_raid1e(orom
) ? " raid1e" : "",
2677 imsm_orom_has_raid10(orom
) ? " raid10" : "",
2678 imsm_orom_has_raid5(orom
) ? " raid5" : "");
2679 printf(" Chunk Sizes :%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
2680 imsm_orom_has_chunk(orom
, 2) ? " 2k" : "",
2681 imsm_orom_has_chunk(orom
, 4) ? " 4k" : "",
2682 imsm_orom_has_chunk(orom
, 8) ? " 8k" : "",
2683 imsm_orom_has_chunk(orom
, 16) ? " 16k" : "",
2684 imsm_orom_has_chunk(orom
, 32) ? " 32k" : "",
2685 imsm_orom_has_chunk(orom
, 64) ? " 64k" : "",
2686 imsm_orom_has_chunk(orom
, 128) ? " 128k" : "",
2687 imsm_orom_has_chunk(orom
, 256) ? " 256k" : "",
2688 imsm_orom_has_chunk(orom
, 512) ? " 512k" : "",
2689 imsm_orom_has_chunk(orom
, 1024*1) ? " 1M" : "",
2690 imsm_orom_has_chunk(orom
, 1024*2) ? " 2M" : "",
2691 imsm_orom_has_chunk(orom
, 1024*4) ? " 4M" : "",
2692 imsm_orom_has_chunk(orom
, 1024*8) ? " 8M" : "",
2693 imsm_orom_has_chunk(orom
, 1024*16) ? " 16M" : "",
2694 imsm_orom_has_chunk(orom
, 1024*32) ? " 32M" : "",
2695 imsm_orom_has_chunk(orom
, 1024*64) ? " 64M" : "");
2696 printf(" 2TB volumes :%s supported\n",
2697 (orom
->attr
& IMSM_OROM_ATTR_2TB
)?"":" not");
2698 printf(" 2TB disks :%s supported\n",
2699 (orom
->attr
& IMSM_OROM_ATTR_2TB_DISK
)?"":" not");
2700 printf(" Max Disks : %d\n", orom
->tds
);
2701 printf(" Max Volumes : %d per array, %d per %s\n",
2702 orom
->vpa
, orom
->vphba
,
2703 imsm_orom_is_nvme(orom
) ? "platform" : "controller");
2707 static void print_imsm_capability_export(const struct imsm_orom
*orom
)
2709 printf("MD_FIRMWARE_TYPE=imsm\n");
2710 if (orom
->major_ver
|| orom
->minor_ver
|| orom
->hotfix_ver
|| orom
->build
)
2711 printf("IMSM_VERSION=%d.%d.%d.%d\n", orom
->major_ver
, orom
->minor_ver
,
2712 orom
->hotfix_ver
, orom
->build
);
2713 printf("IMSM_SUPPORTED_RAID_LEVELS=%s%s%s%s%s\n",
2714 imsm_orom_has_raid0(orom
) ? "raid0 " : "",
2715 imsm_orom_has_raid1(orom
) ? "raid1 " : "",
2716 imsm_orom_has_raid1e(orom
) ? "raid1e " : "",
2717 imsm_orom_has_raid5(orom
) ? "raid10 " : "",
2718 imsm_orom_has_raid10(orom
) ? "raid5 " : "");
2719 printf("IMSM_SUPPORTED_CHUNK_SIZES=%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
2720 imsm_orom_has_chunk(orom
, 2) ? "2k " : "",
2721 imsm_orom_has_chunk(orom
, 4) ? "4k " : "",
2722 imsm_orom_has_chunk(orom
, 8) ? "8k " : "",
2723 imsm_orom_has_chunk(orom
, 16) ? "16k " : "",
2724 imsm_orom_has_chunk(orom
, 32) ? "32k " : "",
2725 imsm_orom_has_chunk(orom
, 64) ? "64k " : "",
2726 imsm_orom_has_chunk(orom
, 128) ? "128k " : "",
2727 imsm_orom_has_chunk(orom
, 256) ? "256k " : "",
2728 imsm_orom_has_chunk(orom
, 512) ? "512k " : "",
2729 imsm_orom_has_chunk(orom
, 1024*1) ? "1M " : "",
2730 imsm_orom_has_chunk(orom
, 1024*2) ? "2M " : "",
2731 imsm_orom_has_chunk(orom
, 1024*4) ? "4M " : "",
2732 imsm_orom_has_chunk(orom
, 1024*8) ? "8M " : "",
2733 imsm_orom_has_chunk(orom
, 1024*16) ? "16M " : "",
2734 imsm_orom_has_chunk(orom
, 1024*32) ? "32M " : "",
2735 imsm_orom_has_chunk(orom
, 1024*64) ? "64M " : "");
2736 printf("IMSM_2TB_VOLUMES=%s\n",(orom
->attr
& IMSM_OROM_ATTR_2TB
) ? "yes" : "no");
2737 printf("IMSM_2TB_DISKS=%s\n",(orom
->attr
& IMSM_OROM_ATTR_2TB_DISK
) ? "yes" : "no");
2738 printf("IMSM_MAX_DISKS=%d\n",orom
->tds
);
2739 printf("IMSM_MAX_VOLUMES_PER_ARRAY=%d\n",orom
->vpa
);
2740 printf("IMSM_MAX_VOLUMES_PER_CONTROLLER=%d\n",orom
->vphba
);
2743 static int detail_platform_imsm(int verbose
, int enumerate_only
, char *controller_path
)
2745 /* There are two components to imsm platform support, the ahci SATA
2746 * controller and the option-rom. To find the SATA controller we
2747 * simply look in /sys/bus/pci/drivers/ahci to see if an ahci
2748 * controller with the Intel vendor id is present. This approach
2749 * allows mdadm to leverage the kernel's ahci detection logic, with the
2750 * caveat that if ahci.ko is not loaded mdadm will not be able to
2751 * detect platform raid capabilities. The option-rom resides in a
2752 * platform "Adapter ROM". We scan for its signature to retrieve the
2753 * platform capabilities. If raid support is disabled in the BIOS the
2754 * option-rom capability structure will not be available.
2756 struct sys_dev
*list
, *hba
;
2761 if (enumerate_only
) {
2762 if (check_no_platform())
2764 list
= find_intel_devices();
2767 for (hba
= list
; hba
; hba
= hba
->next
) {
2768 if (find_imsm_capability(hba
)) {
2778 list
= find_intel_devices();
2781 pr_err("no active Intel(R) RAID controller found.\n");
2783 } else if (verbose
> 0)
2784 print_found_intel_controllers(list
);
2786 for (hba
= list
; hba
; hba
= hba
->next
) {
2787 if (controller_path
&& (compare_paths(hba
->path
, controller_path
) != 0))
2789 if (!find_imsm_capability(hba
)) {
2791 pr_err("imsm capabilities not found for controller: %s (type %s)\n",
2792 hba
->type
== SYS_DEV_VMD
|| hba
->type
== SYS_DEV_SATA_VMD
?
2793 vmd_domain_to_controller(hba
, buf
) :
2794 hba
->path
, get_sys_dev_type(hba
->type
));
2800 if (controller_path
&& result
== 1) {
2801 pr_err("no active Intel(R) RAID controller found under %s\n",
2806 const struct orom_entry
*entry
;
2808 for (entry
= orom_entries
; entry
; entry
= entry
->next
) {
2809 if (entry
->type
== SYS_DEV_VMD
) {
2810 print_imsm_capability(&entry
->orom
);
2811 printf(" 3rd party NVMe :%s supported\n",
2812 imsm_orom_has_tpv_support(&entry
->orom
)?"":" not");
2813 for (hba
= list
; hba
; hba
= hba
->next
) {
2814 if (hba
->type
== SYS_DEV_VMD
) {
2816 printf(" I/O Controller : %s (%s)\n",
2817 vmd_domain_to_controller(hba
, buf
), get_sys_dev_type(hba
->type
));
2818 if (print_nvme_info(hba
)) {
2820 pr_err("failed to get devices attached to VMD domain.\n");
2829 print_imsm_capability(&entry
->orom
);
2830 if (entry
->type
== SYS_DEV_NVME
) {
2831 for (hba
= list
; hba
; hba
= hba
->next
) {
2832 if (hba
->type
== SYS_DEV_NVME
)
2833 print_nvme_info(hba
);
2839 struct devid_list
*devid
;
2840 for (devid
= entry
->devid_list
; devid
; devid
= devid
->next
) {
2841 hba
= device_by_id(devid
->devid
);
2845 printf(" I/O Controller : %s (%s)\n",
2846 hba
->path
, get_sys_dev_type(hba
->type
));
2847 if (hba
->type
== SYS_DEV_SATA
|| hba
->type
== SYS_DEV_SATA_VMD
) {
2848 host_base
= ahci_get_port_count(hba
->path
, &port_count
);
2849 if (ahci_enumerate_ports(hba
, port_count
, host_base
, verbose
)) {
2851 pr_err("failed to enumerate ports on %s controller at %s.\n",
2852 get_sys_dev_type(hba
->type
), hba
->pci_id
);
2863 static int export_detail_platform_imsm(int verbose
, char *controller_path
)
2865 struct sys_dev
*list
, *hba
;
2868 list
= find_intel_devices();
2871 pr_err("IMSM_DETAIL_PLATFORM_ERROR=NO_INTEL_DEVICES\n");
2876 for (hba
= list
; hba
; hba
= hba
->next
) {
2877 if (controller_path
&& (compare_paths(hba
->path
,controller_path
) != 0))
2879 if (!find_imsm_capability(hba
) && verbose
> 0) {
2881 pr_err("IMSM_DETAIL_PLATFORM_ERROR=NO_IMSM_CAPABLE_DEVICE_UNDER_%s\n",
2882 hba
->type
== SYS_DEV_VMD
|| hba
->type
== SYS_DEV_SATA_VMD
?
2883 vmd_domain_to_controller(hba
, buf
) : hba
->path
);
2889 const struct orom_entry
*entry
;
2891 for (entry
= orom_entries
; entry
; entry
= entry
->next
) {
2892 if (entry
->type
== SYS_DEV_VMD
|| entry
->type
== SYS_DEV_SATA_VMD
) {
2893 for (hba
= list
; hba
; hba
= hba
->next
)
2894 print_imsm_capability_export(&entry
->orom
);
2897 print_imsm_capability_export(&entry
->orom
);
2903 static int match_home_imsm(struct supertype
*st
, char *homehost
)
2905 /* the imsm metadata format does not specify any host
2906 * identification information. We return -1 since we can never
2907 * confirm nor deny whether a given array is "meant" for this
2908 * host. We rely on compare_super and the 'family_num' fields to
2909 * exclude member disks that do not belong, and we rely on
2910 * mdadm.conf to specify the arrays that should be assembled.
2911 * Auto-assembly may still pick up "foreign" arrays.
2917 static void uuid_from_super_imsm(struct supertype
*st
, int uuid
[4])
2919 /* The uuid returned here is used for:
2920 * uuid to put into bitmap file (Create, Grow)
2921 * uuid for backup header when saving critical section (Grow)
2922 * comparing uuids when re-adding a device into an array
2923 * In these cases the uuid required is that of the data-array,
2924 * not the device-set.
2925 * uuid to recognise same set when adding a missing device back
2926 * to an array. This is a uuid for the device-set.
2928 * For each of these we can make do with a truncated
2929 * or hashed uuid rather than the original, as long as
2931 * In each case the uuid required is that of the data-array,
2932 * not the device-set.
2934 /* imsm does not track uuid's so we synthesis one using sha1 on
2935 * - The signature (Which is constant for all imsm array, but no matter)
2936 * - the orig_family_num of the container
2937 * - the index number of the volume
2938 * - the 'serial' number of the volume.
2939 * Hopefully these are all constant.
2941 struct intel_super
*super
= st
->sb
;
2944 struct sha1_ctx ctx
;
2945 struct imsm_dev
*dev
= NULL
;
2948 /* some mdadm versions failed to set ->orig_family_num, in which
2949 * case fall back to ->family_num. orig_family_num will be
2950 * fixed up with the first metadata update.
2952 family_num
= super
->anchor
->orig_family_num
;
2953 if (family_num
== 0)
2954 family_num
= super
->anchor
->family_num
;
2955 sha1_init_ctx(&ctx
);
2956 sha1_process_bytes(super
->anchor
->sig
, MPB_SIG_LEN
, &ctx
);
2957 sha1_process_bytes(&family_num
, sizeof(__u32
), &ctx
);
2958 if (super
->current_vol
>= 0)
2959 dev
= get_imsm_dev(super
, super
->current_vol
);
2961 __u32 vol
= super
->current_vol
;
2962 sha1_process_bytes(&vol
, sizeof(vol
), &ctx
);
2963 sha1_process_bytes(dev
->volume
, MAX_RAID_SERIAL_LEN
, &ctx
);
2965 sha1_finish_ctx(&ctx
, buf
);
2966 memcpy(uuid
, buf
, 4*4);
2969 static __u32
migr_strip_blocks_resync(struct imsm_dev
*dev
)
2971 /* migr_strip_size when repairing or initializing parity */
2972 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
2973 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
2975 switch (get_imsm_raid_level(map
)) {
2980 return 128*1024 >> 9;
2984 static __u32
migr_strip_blocks_rebuild(struct imsm_dev
*dev
)
2986 /* migr_strip_size when rebuilding a degraded disk, no idea why
2987 * this is different than migr_strip_size_resync(), but it's good
2990 struct imsm_map
*map
= get_imsm_map(dev
, MAP_1
);
2991 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
2993 switch (get_imsm_raid_level(map
)) {
2996 if (map
->num_members
% map
->num_domains
== 0)
2997 return 128*1024 >> 9;
3001 return max((__u32
) 64*1024 >> 9, chunk
);
3003 return 128*1024 >> 9;
3007 static __u32
num_stripes_per_unit_resync(struct imsm_dev
*dev
)
3009 struct imsm_map
*lo
= get_imsm_map(dev
, MAP_0
);
3010 struct imsm_map
*hi
= get_imsm_map(dev
, MAP_1
);
3011 __u32 lo_chunk
= __le32_to_cpu(lo
->blocks_per_strip
);
3012 __u32 hi_chunk
= __le32_to_cpu(hi
->blocks_per_strip
);
3014 return max((__u32
) 1, hi_chunk
/ lo_chunk
);
3017 static __u32
num_stripes_per_unit_rebuild(struct imsm_dev
*dev
)
3019 struct imsm_map
*lo
= get_imsm_map(dev
, MAP_0
);
3020 int level
= get_imsm_raid_level(lo
);
3022 if (level
== 1 || level
== 10) {
3023 struct imsm_map
*hi
= get_imsm_map(dev
, MAP_1
);
3025 return hi
->num_domains
;
3027 return num_stripes_per_unit_resync(dev
);
3030 static unsigned long long calc_component_size(struct imsm_map
*map
,
3031 struct imsm_dev
*dev
)
3033 unsigned long long component_size
;
3034 unsigned long long dev_size
= imsm_dev_size(dev
);
3035 long long calc_dev_size
= 0;
3036 unsigned int member_disks
= imsm_num_data_members(map
);
3038 if (member_disks
== 0)
3041 component_size
= per_dev_array_size(map
);
3042 calc_dev_size
= component_size
* member_disks
;
3044 /* Component size is rounded to 1MB so difference between size from
3045 * metadata and size calculated from num_data_stripes equals up to
3046 * 2048 blocks per each device. If the difference is higher it means
3047 * that array size was expanded and num_data_stripes was not updated.
3049 if (llabs(calc_dev_size
- (long long)dev_size
) >
3050 (1 << SECT_PER_MB_SHIFT
) * member_disks
) {
3051 component_size
= dev_size
/ member_disks
;
3052 dprintf("Invalid num_data_stripes in metadata; expected=%llu, found=%llu\n",
3053 component_size
/ map
->blocks_per_strip
,
3054 num_data_stripes(map
));
3057 return component_size
;
3060 static __u32
parity_segment_depth(struct imsm_dev
*dev
)
3062 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
3063 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
3065 switch(get_imsm_raid_level(map
)) {
3068 return chunk
* map
->num_domains
;
3070 return chunk
* map
->num_members
;
3076 static __u32
map_migr_block(struct imsm_dev
*dev
, __u32 block
)
3078 struct imsm_map
*map
= get_imsm_map(dev
, MAP_1
);
3079 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
3080 __u32 strip
= block
/ chunk
;
3082 switch (get_imsm_raid_level(map
)) {
3085 __u32 vol_strip
= (strip
* map
->num_domains
) + 1;
3086 __u32 vol_stripe
= vol_strip
/ map
->num_members
;
3088 return vol_stripe
* chunk
+ block
% chunk
;
3090 __u32 stripe
= strip
/ (map
->num_members
- 1);
3092 return stripe
* chunk
+ block
% chunk
;
3099 static __u64
blocks_per_migr_unit(struct intel_super
*super
,
3100 struct imsm_dev
*dev
)
3102 /* calculate the conversion factor between per member 'blocks'
3103 * (md/{resync,rebuild}_start) and imsm migration units, return
3104 * 0 for the 'not migrating' and 'unsupported migration' cases
3106 if (!dev
->vol
.migr_state
)
3109 switch (migr_type(dev
)) {
3110 case MIGR_GEN_MIGR
: {
3111 struct migr_record
*migr_rec
= super
->migr_rec
;
3112 return __le32_to_cpu(migr_rec
->blocks_per_unit
);
3117 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
3118 __u32 stripes_per_unit
;
3119 __u32 blocks_per_unit
;
3128 /* yes, this is really the translation of migr_units to
3129 * per-member blocks in the 'resync' case
3131 stripes_per_unit
= num_stripes_per_unit_resync(dev
);
3132 migr_chunk
= migr_strip_blocks_resync(dev
);
3133 disks
= imsm_num_data_members(map
);
3134 blocks_per_unit
= stripes_per_unit
* migr_chunk
* disks
;
3135 stripe
= __le16_to_cpu(map
->blocks_per_strip
) * disks
;
3136 segment
= blocks_per_unit
/ stripe
;
3137 block_rel
= blocks_per_unit
- segment
* stripe
;
3138 parity_depth
= parity_segment_depth(dev
);
3139 block_map
= map_migr_block(dev
, block_rel
);
3140 return block_map
+ parity_depth
* segment
;
3142 case MIGR_REBUILD
: {
3143 __u32 stripes_per_unit
;
3146 stripes_per_unit
= num_stripes_per_unit_rebuild(dev
);
3147 migr_chunk
= migr_strip_blocks_rebuild(dev
);
3148 return migr_chunk
* stripes_per_unit
;
3150 case MIGR_STATE_CHANGE
:
3156 static int imsm_level_to_layout(int level
)
3164 return ALGORITHM_LEFT_ASYMMETRIC
;
3171 /*******************************************************************************
3172 * Function: read_imsm_migr_rec
3173 * Description: Function reads imsm migration record from last sector of disk
3175 * fd : disk descriptor
3176 * super : metadata info
3180 ******************************************************************************/
3181 static int read_imsm_migr_rec(int fd
, struct intel_super
*super
)
3184 unsigned int sector_size
= super
->sector_size
;
3185 unsigned long long dsize
;
3187 get_dev_size(fd
, NULL
, &dsize
);
3188 if (lseek64(fd
, dsize
- (sector_size
*MIGR_REC_SECTOR_POSITION
),
3190 pr_err("Cannot seek to anchor block: %s\n",
3194 if ((unsigned int)read(fd
, super
->migr_rec_buf
,
3195 MIGR_REC_BUF_SECTORS
*sector_size
) !=
3196 MIGR_REC_BUF_SECTORS
*sector_size
) {
3197 pr_err("Cannot read migr record block: %s\n",
3202 if (sector_size
== 4096)
3203 convert_from_4k_imsm_migr_rec(super
);
3209 static struct imsm_dev
*imsm_get_device_during_migration(
3210 struct intel_super
*super
)
3213 struct intel_dev
*dv
;
3215 for (dv
= super
->devlist
; dv
; dv
= dv
->next
) {
3216 if (is_gen_migration(dv
->dev
))
3222 /*******************************************************************************
3223 * Function: load_imsm_migr_rec
3224 * Description: Function reads imsm migration record (it is stored at the last
3227 * super : imsm internal array info
3231 * -2 : no migration in progress
3232 ******************************************************************************/
3233 static int load_imsm_migr_rec(struct intel_super
*super
)
3239 struct imsm_dev
*dev
;
3240 struct imsm_map
*map
;
3244 /* find map under migration */
3245 dev
= imsm_get_device_during_migration(super
);
3246 /* nothing to load,no migration in progress?
3251 map
= get_imsm_map(dev
, MAP_0
);
3255 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
3256 /* skip spare and failed disks
3260 /* read only from one of the first two slots
3262 slot
= get_imsm_disk_slot(map
, dl
->index
);
3263 if (slot
> 1 || slot
< 0)
3266 if (!is_fd_valid(dl
->fd
)) {
3267 sprintf(nm
, "%d:%d", dl
->major
, dl
->minor
);
3268 fd
= dev_open(nm
, O_RDONLY
);
3270 if (is_fd_valid(fd
)) {
3280 if (!is_fd_valid(fd
))
3282 retval
= read_imsm_migr_rec(fd
, super
);
3289 /*******************************************************************************
3290 * function: imsm_create_metadata_checkpoint_update
3291 * Description: It creates update for checkpoint change.
3293 * super : imsm internal array info
3294 * u : pointer to prepared update
3297 * If length is equal to 0, input pointer u contains no update
3298 ******************************************************************************/
3299 static int imsm_create_metadata_checkpoint_update(
3300 struct intel_super
*super
,
3301 struct imsm_update_general_migration_checkpoint
**u
)
3304 int update_memory_size
= 0;
3306 dprintf("(enter)\n");
3312 /* size of all update data without anchor */
3313 update_memory_size
=
3314 sizeof(struct imsm_update_general_migration_checkpoint
);
3316 *u
= xcalloc(1, update_memory_size
);
3318 dprintf("error: cannot get memory\n");
3321 (*u
)->type
= update_general_migration_checkpoint
;
3322 (*u
)->curr_migr_unit
= current_migr_unit(super
->migr_rec
);
3323 dprintf("prepared for %llu\n", (unsigned long long)(*u
)->curr_migr_unit
);
3325 return update_memory_size
;
3328 static void imsm_update_metadata_locally(struct supertype
*st
,
3329 void *buf
, int len
);
3331 /*******************************************************************************
3332 * Function: write_imsm_migr_rec
3333 * Description: Function writes imsm migration record
3334 * (at the last sector of disk)
3336 * super : imsm internal array info
3340 ******************************************************************************/
3341 static int write_imsm_migr_rec(struct supertype
*st
)
3343 struct intel_super
*super
= st
->sb
;
3344 unsigned int sector_size
= super
->sector_size
;
3345 unsigned long long dsize
;
3349 struct imsm_update_general_migration_checkpoint
*u
;
3350 struct imsm_dev
*dev
;
3351 struct imsm_map
*map
;
3353 /* find map under migration */
3354 dev
= imsm_get_device_during_migration(super
);
3355 /* if no migration, write buffer anyway to clear migr_record
3356 * on disk based on first available device
3359 dev
= get_imsm_dev(super
, super
->current_vol
< 0 ? 0 :
3360 super
->current_vol
);
3362 map
= get_imsm_map(dev
, MAP_0
);
3364 if (sector_size
== 4096)
3365 convert_to_4k_imsm_migr_rec(super
);
3366 for (sd
= super
->disks
; sd
; sd
= sd
->next
) {
3369 /* skip failed and spare devices */
3372 /* write to 2 first slots only */
3374 slot
= get_imsm_disk_slot(map
, sd
->index
);
3375 if (map
== NULL
|| slot
> 1 || slot
< 0)
3378 get_dev_size(sd
->fd
, NULL
, &dsize
);
3379 if (lseek64(sd
->fd
, dsize
- (MIGR_REC_SECTOR_POSITION
*
3382 pr_err("Cannot seek to anchor block: %s\n",
3386 if ((unsigned int)write(sd
->fd
, super
->migr_rec_buf
,
3387 MIGR_REC_BUF_SECTORS
*sector_size
) !=
3388 MIGR_REC_BUF_SECTORS
*sector_size
) {
3389 pr_err("Cannot write migr record block: %s\n",
3394 if (sector_size
== 4096)
3395 convert_from_4k_imsm_migr_rec(super
);
3396 /* update checkpoint information in metadata */
3397 len
= imsm_create_metadata_checkpoint_update(super
, &u
);
3399 dprintf("imsm: Cannot prepare update\n");
3402 /* update metadata locally */
3403 imsm_update_metadata_locally(st
, u
, len
);
3404 /* and possibly remotely */
3405 if (st
->update_tail
) {
3406 append_metadata_update(st
, u
, len
);
3407 /* during reshape we do all work inside metadata handler
3408 * manage_reshape(), so metadata update has to be triggered
3411 flush_metadata_updates(st
);
3412 st
->update_tail
= &st
->updates
;
3421 /* spare/missing disks activations are not allowe when
3422 * array/container performs reshape operation, because
3423 * all arrays in container works on the same disks set
3425 int imsm_reshape_blocks_arrays_changes(struct intel_super
*super
)
3428 struct intel_dev
*i_dev
;
3429 struct imsm_dev
*dev
;
3431 /* check whole container
3433 for (i_dev
= super
->devlist
; i_dev
; i_dev
= i_dev
->next
) {
3435 if (is_gen_migration(dev
)) {
3436 /* No repair during any migration in container
3444 static unsigned long long imsm_component_size_alignment_check(int level
,
3446 unsigned int sector_size
,
3447 unsigned long long component_size
)
3449 unsigned int component_size_alignment
;
3451 /* check component size alignment
3453 component_size_alignment
= component_size
% (chunk_size
/sector_size
);
3455 dprintf("(Level: %i, chunk_size = %i, component_size = %llu), component_size_alignment = %u\n",
3456 level
, chunk_size
, component_size
,
3457 component_size_alignment
);
3459 if (component_size_alignment
&& (level
!= 1) && (level
!= UnSet
)) {
3460 dprintf("imsm: reported component size aligned from %llu ",
3462 component_size
-= component_size_alignment
;
3463 dprintf_cont("to %llu (%i).\n",
3464 component_size
, component_size_alignment
);
3467 return component_size
;
3470 /*******************************************************************************
3471 * Function: get_bitmap_header_sector
3472 * Description: Returns the sector where the bitmap header is placed.
3474 * st : supertype information
3475 * dev_idx : index of the device with bitmap
3478 * The sector where the bitmap header is placed
3479 ******************************************************************************/
3480 static unsigned long long get_bitmap_header_sector(struct intel_super
*super
,
3483 struct imsm_dev
*dev
= get_imsm_dev(super
, dev_idx
);
3484 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
3486 if (!super
->sector_size
) {
3487 dprintf("sector size is not set\n");
3491 return pba_of_lba0(map
) + calc_component_size(map
, dev
) +
3492 (IMSM_BITMAP_HEADER_OFFSET
/ super
->sector_size
);
3495 /*******************************************************************************
3496 * Function: get_bitmap_sector
3497 * Description: Returns the sector where the bitmap is placed.
3499 * st : supertype information
3500 * dev_idx : index of the device with bitmap
3503 * The sector where the bitmap is placed
3504 ******************************************************************************/
3505 static unsigned long long get_bitmap_sector(struct intel_super
*super
,
3508 if (!super
->sector_size
) {
3509 dprintf("sector size is not set\n");
3513 return get_bitmap_header_sector(super
, dev_idx
) +
3514 (IMSM_BITMAP_HEADER_SIZE
/ super
->sector_size
);
3517 static unsigned long long get_ppl_sector(struct intel_super
*super
, int dev_idx
)
3519 struct imsm_dev
*dev
= get_imsm_dev(super
, dev_idx
);
3520 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
3522 return pba_of_lba0(map
) +
3523 (num_data_stripes(map
) * map
->blocks_per_strip
);
3526 static void getinfo_super_imsm_volume(struct supertype
*st
, struct mdinfo
*info
, char *dmap
)
3528 struct intel_super
*super
= st
->sb
;
3529 struct migr_record
*migr_rec
= super
->migr_rec
;
3530 struct imsm_dev
*dev
= get_imsm_dev(super
, super
->current_vol
);
3531 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
3532 struct imsm_map
*prev_map
= get_imsm_map(dev
, MAP_1
);
3533 struct imsm_map
*map_to_analyse
= map
;
3535 int map_disks
= info
->array
.raid_disks
;
3537 memset(info
, 0, sizeof(*info
));
3539 map_to_analyse
= prev_map
;
3541 dl
= super
->current_disk
;
3543 info
->container_member
= super
->current_vol
;
3544 info
->array
.raid_disks
= map
->num_members
;
3545 info
->array
.level
= get_imsm_raid_level(map_to_analyse
);
3546 info
->array
.layout
= imsm_level_to_layout(info
->array
.level
);
3547 info
->array
.md_minor
= -1;
3548 info
->array
.ctime
= 0;
3549 info
->array
.utime
= 0;
3550 info
->array
.chunk_size
=
3551 __le16_to_cpu(map_to_analyse
->blocks_per_strip
) << 9;
3552 info
->array
.state
= !(dev
->vol
.dirty
& RAIDVOL_DIRTY
);
3553 info
->custom_array_size
= imsm_dev_size(dev
);
3554 info
->recovery_blocked
= imsm_reshape_blocks_arrays_changes(st
->sb
);
3556 if (is_gen_migration(dev
)) {
3558 * device prev_map should be added if it is in the middle
3563 info
->reshape_active
= 1;
3564 info
->new_level
= get_imsm_raid_level(map
);
3565 info
->new_layout
= imsm_level_to_layout(info
->new_level
);
3566 info
->new_chunk
= __le16_to_cpu(map
->blocks_per_strip
) << 9;
3567 info
->delta_disks
= map
->num_members
- prev_map
->num_members
;
3568 if (info
->delta_disks
) {
3569 /* this needs to be applied to every array
3572 info
->reshape_active
= CONTAINER_RESHAPE
;
3574 /* We shape information that we give to md might have to be
3575 * modify to cope with md's requirement for reshaping arrays.
3576 * For example, when reshaping a RAID0, md requires it to be
3577 * presented as a degraded RAID4.
3578 * Also if a RAID0 is migrating to a RAID5 we need to specify
3579 * the array as already being RAID5, but the 'before' layout
3580 * is a RAID4-like layout.
3582 switch (info
->array
.level
) {
3584 switch(info
->new_level
) {
3586 /* conversion is happening as RAID4 */
3587 info
->array
.level
= 4;
3588 info
->array
.raid_disks
+= 1;
3591 /* conversion is happening as RAID5 */
3592 info
->array
.level
= 5;
3593 info
->array
.layout
= ALGORITHM_PARITY_N
;
3594 info
->delta_disks
-= 1;
3597 /* FIXME error message */
3598 info
->array
.level
= UnSet
;
3604 info
->new_level
= UnSet
;
3605 info
->new_layout
= UnSet
;
3606 info
->new_chunk
= info
->array
.chunk_size
;
3607 info
->delta_disks
= 0;
3611 info
->disk
.major
= dl
->major
;
3612 info
->disk
.minor
= dl
->minor
;
3613 info
->disk
.number
= dl
->index
;
3614 info
->disk
.raid_disk
= get_imsm_disk_slot(map_to_analyse
,
3618 info
->data_offset
= pba_of_lba0(map_to_analyse
);
3619 info
->component_size
= calc_component_size(map
, dev
);
3620 info
->component_size
= imsm_component_size_alignment_check(
3622 info
->array
.chunk_size
,
3624 info
->component_size
);
3625 info
->bb
.supported
= 1;
3627 memset(info
->uuid
, 0, sizeof(info
->uuid
));
3628 info
->recovery_start
= MaxSector
;
3630 if (info
->array
.level
== 5 &&
3631 (dev
->rwh_policy
== RWH_DISTRIBUTED
||
3632 dev
->rwh_policy
== RWH_MULTIPLE_DISTRIBUTED
)) {
3633 info
->consistency_policy
= CONSISTENCY_POLICY_PPL
;
3634 info
->ppl_sector
= get_ppl_sector(super
, super
->current_vol
);
3635 if (dev
->rwh_policy
== RWH_MULTIPLE_DISTRIBUTED
)
3636 info
->ppl_size
= MULTIPLE_PPL_AREA_SIZE_IMSM
>> 9;
3638 info
->ppl_size
= (PPL_HEADER_SIZE
+ PPL_ENTRY_SPACE
)
3640 } else if (info
->array
.level
<= 0) {
3641 info
->consistency_policy
= CONSISTENCY_POLICY_NONE
;
3643 if (dev
->rwh_policy
== RWH_BITMAP
) {
3644 info
->bitmap_offset
= get_bitmap_sector(super
, super
->current_vol
);
3645 info
->consistency_policy
= CONSISTENCY_POLICY_BITMAP
;
3647 info
->consistency_policy
= CONSISTENCY_POLICY_RESYNC
;
3651 info
->reshape_progress
= 0;
3652 info
->resync_start
= MaxSector
;
3653 if ((map_to_analyse
->map_state
== IMSM_T_STATE_UNINITIALIZED
||
3654 !(info
->array
.state
& 1)) &&
3655 imsm_reshape_blocks_arrays_changes(super
) == 0) {
3656 info
->resync_start
= 0;
3658 if (dev
->vol
.migr_state
) {
3659 switch (migr_type(dev
)) {
3662 __u64 blocks_per_unit
= blocks_per_migr_unit(super
,
3664 __u64 units
= vol_curr_migr_unit(dev
);
3666 info
->resync_start
= blocks_per_unit
* units
;
3669 case MIGR_GEN_MIGR
: {
3670 __u64 blocks_per_unit
= blocks_per_migr_unit(super
,
3672 __u64 units
= current_migr_unit(migr_rec
);
3675 if (__le32_to_cpu(migr_rec
->ascending_migr
) &&
3677 (get_num_migr_units(migr_rec
)-1)) &&
3678 (super
->migr_rec
->rec_status
==
3679 __cpu_to_le32(UNIT_SRC_IN_CP_AREA
)))
3682 info
->reshape_progress
= blocks_per_unit
* units
;
3684 dprintf("IMSM: General Migration checkpoint : %llu (%llu) -> read reshape progress : %llu\n",
3685 (unsigned long long)units
,
3686 (unsigned long long)blocks_per_unit
,
3687 info
->reshape_progress
);
3689 used_disks
= imsm_num_data_members(prev_map
);
3690 if (used_disks
> 0) {
3691 info
->custom_array_size
= per_dev_array_size(map
) *
3696 /* we could emulate the checkpointing of
3697 * 'sync_action=check' migrations, but for now
3698 * we just immediately complete them
3701 /* this is handled by container_content_imsm() */
3702 case MIGR_STATE_CHANGE
:
3703 /* FIXME handle other migrations */
3705 /* we are not dirty, so... */
3706 info
->resync_start
= MaxSector
;
3710 strncpy(info
->name
, (char *) dev
->volume
, MAX_RAID_SERIAL_LEN
);
3711 info
->name
[MAX_RAID_SERIAL_LEN
] = 0;
3713 info
->array
.major_version
= -1;
3714 info
->array
.minor_version
= -2;
3715 sprintf(info
->text_version
, "/%s/%d", st
->container_devnm
, info
->container_member
);
3716 info
->safe_mode_delay
= 4000; /* 4 secs like the Matrix driver */
3717 uuid_from_super_imsm(st
, info
->uuid
);
3721 for (i
=0; i
<map_disks
; i
++) {
3723 if (i
< info
->array
.raid_disks
) {
3724 struct imsm_disk
*dsk
;
3725 j
= get_imsm_disk_idx(dev
, i
, MAP_X
);
3726 dsk
= get_imsm_disk(super
, j
);
3727 if (dsk
&& (dsk
->status
& CONFIGURED_DISK
))
3734 static __u8
imsm_check_degraded(struct intel_super
*super
, struct imsm_dev
*dev
,
3735 int failed
, int look_in_map
);
3737 static int imsm_count_failed(struct intel_super
*super
, struct imsm_dev
*dev
,
3740 static void manage_second_map(struct intel_super
*super
, struct imsm_dev
*dev
)
3742 if (is_gen_migration(dev
)) {
3745 struct imsm_map
*map2
= get_imsm_map(dev
, MAP_1
);
3747 failed
= imsm_count_failed(super
, dev
, MAP_1
);
3748 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_1
);
3749 if (map2
->map_state
!= map_state
) {
3750 map2
->map_state
= map_state
;
3751 super
->updates_pending
++;
3756 static struct imsm_disk
*get_imsm_missing(struct intel_super
*super
, __u8 index
)
3760 for (d
= super
->missing
; d
; d
= d
->next
)
3761 if (d
->index
== index
)
3766 static void getinfo_super_imsm(struct supertype
*st
, struct mdinfo
*info
, char *map
)
3768 struct intel_super
*super
= st
->sb
;
3769 struct imsm_disk
*disk
;
3770 int map_disks
= info
->array
.raid_disks
;
3771 int max_enough
= -1;
3773 struct imsm_super
*mpb
;
3775 if (super
->current_vol
>= 0) {
3776 getinfo_super_imsm_volume(st
, info
, map
);
3779 memset(info
, 0, sizeof(*info
));
3781 /* Set raid_disks to zero so that Assemble will always pull in valid
3784 info
->array
.raid_disks
= 0;
3785 info
->array
.level
= LEVEL_CONTAINER
;
3786 info
->array
.layout
= 0;
3787 info
->array
.md_minor
= -1;
3788 info
->array
.ctime
= 0; /* N/A for imsm */
3789 info
->array
.utime
= 0;
3790 info
->array
.chunk_size
= 0;
3792 info
->disk
.major
= 0;
3793 info
->disk
.minor
= 0;
3794 info
->disk
.raid_disk
= -1;
3795 info
->reshape_active
= 0;
3796 info
->array
.major_version
= -1;
3797 info
->array
.minor_version
= -2;
3798 strcpy(info
->text_version
, "imsm");
3799 info
->safe_mode_delay
= 0;
3800 info
->disk
.number
= -1;
3801 info
->disk
.state
= 0;
3803 info
->recovery_start
= MaxSector
;
3804 info
->recovery_blocked
= imsm_reshape_blocks_arrays_changes(st
->sb
);
3805 info
->bb
.supported
= 1;
3807 /* do we have the all the insync disks that we expect? */
3808 mpb
= super
->anchor
;
3809 info
->events
= __le32_to_cpu(mpb
->generation_num
);
3811 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
3812 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
3813 int failed
, enough
, j
, missing
= 0;
3814 struct imsm_map
*map
;
3817 failed
= imsm_count_failed(super
, dev
, MAP_0
);
3818 state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
3819 map
= get_imsm_map(dev
, MAP_0
);
3821 /* any newly missing disks?
3822 * (catches single-degraded vs double-degraded)
3824 for (j
= 0; j
< map
->num_members
; j
++) {
3825 __u32 ord
= get_imsm_ord_tbl_ent(dev
, j
, MAP_0
);
3826 __u32 idx
= ord_to_idx(ord
);
3828 if (super
->disks
&& super
->disks
->index
== (int)idx
)
3829 info
->disk
.raid_disk
= j
;
3831 if (!(ord
& IMSM_ORD_REBUILD
) &&
3832 get_imsm_missing(super
, idx
)) {
3838 if (state
== IMSM_T_STATE_FAILED
)
3840 else if (state
== IMSM_T_STATE_DEGRADED
&&
3841 (state
!= map
->map_state
|| missing
))
3843 else /* we're normal, or already degraded */
3845 if (is_gen_migration(dev
) && missing
) {
3846 /* during general migration we need all disks
3847 * that process is running on.
3848 * No new missing disk is allowed.
3852 /* no more checks necessary
3856 /* in the missing/failed disk case check to see
3857 * if at least one array is runnable
3859 max_enough
= max(max_enough
, enough
);
3862 info
->container_enough
= max_enough
;
3865 __u32 reserved
= imsm_reserved_sectors(super
, super
->disks
);
3867 disk
= &super
->disks
->disk
;
3868 info
->data_offset
= total_blocks(&super
->disks
->disk
) - reserved
;
3869 info
->component_size
= reserved
;
3870 info
->disk
.state
= is_configured(disk
) ? (1 << MD_DISK_ACTIVE
) : 0;
3871 /* we don't change info->disk.raid_disk here because
3872 * this state will be finalized in mdmon after we have
3873 * found the 'most fresh' version of the metadata
3875 info
->disk
.state
|= is_failed(disk
) ? (1 << MD_DISK_FAULTY
) : 0;
3876 info
->disk
.state
|= (is_spare(disk
) || is_journal(disk
)) ?
3877 0 : (1 << MD_DISK_SYNC
);
3880 /* only call uuid_from_super_imsm when this disk is part of a populated container,
3881 * ->compare_super may have updated the 'num_raid_devs' field for spares
3883 if (info
->disk
.state
& (1 << MD_DISK_SYNC
) || super
->anchor
->num_raid_devs
)
3884 uuid_from_super_imsm(st
, info
->uuid
);
3886 memcpy(info
->uuid
, uuid_zero
, sizeof(uuid_zero
));
3888 /* I don't know how to compute 'map' on imsm, so use safe default */
3891 for (i
= 0; i
< map_disks
; i
++)
3897 /* allocates memory and fills disk in mdinfo structure
3898 * for each disk in array */
3899 struct mdinfo
*getinfo_super_disks_imsm(struct supertype
*st
)
3901 struct mdinfo
*mddev
;
3902 struct intel_super
*super
= st
->sb
;
3903 struct imsm_disk
*disk
;
3906 if (!super
|| !super
->disks
)
3909 mddev
= xcalloc(1, sizeof(*mddev
));
3913 tmp
= xcalloc(1, sizeof(*tmp
));
3915 tmp
->next
= mddev
->devs
;
3917 tmp
->disk
.number
= count
++;
3918 tmp
->disk
.major
= dl
->major
;
3919 tmp
->disk
.minor
= dl
->minor
;
3920 tmp
->disk
.state
= is_configured(disk
) ?
3921 (1 << MD_DISK_ACTIVE
) : 0;
3922 tmp
->disk
.state
|= is_failed(disk
) ? (1 << MD_DISK_FAULTY
) : 0;
3923 tmp
->disk
.state
|= is_spare(disk
) ? 0 : (1 << MD_DISK_SYNC
);
3924 tmp
->disk
.raid_disk
= -1;
3930 static int update_super_imsm(struct supertype
*st
, struct mdinfo
*info
,
3931 enum update_opt update
, char *devname
,
3932 int verbose
, int uuid_set
, char *homehost
)
3934 /* For 'assemble' and 'force' we need to return non-zero if any
3935 * change was made. For others, the return value is ignored.
3936 * Update options are:
3937 * force-one : This device looks a bit old but needs to be included,
3938 * update age info appropriately.
3939 * assemble: clear any 'faulty' flag to allow this device to
3941 * force-array: Array is degraded but being forced, mark it clean
3942 * if that will be needed to assemble it.
3944 * newdev: not used ????
3945 * grow: Array has gained a new device - this is currently for
3947 * resync: mark as dirty so a resync will happen.
3948 * name: update the name - preserving the homehost
3949 * uuid: Change the uuid of the array to match watch is given
3951 * Following are not relevant for this imsm:
3952 * sparc2.2 : update from old dodgey metadata
3953 * super-minor: change the preferred_minor number
3954 * summaries: update redundant counters.
3955 * homehost: update the recorded homehost
3956 * _reshape_progress: record new reshape_progress position.
3959 struct intel_super
*super
= st
->sb
;
3960 struct imsm_super
*mpb
;
3962 /* we can only update container info */
3963 if (!super
|| super
->current_vol
>= 0 || !super
->anchor
)
3966 mpb
= super
->anchor
;
3970 /* We take this to mean that the family_num should be updated.
3971 * However that is much smaller than the uuid so we cannot really
3972 * allow an explicit uuid to be given. And it is hard to reliably
3974 * So if !uuid_set we know the current uuid is random and just used
3975 * the first 'int' and copy it to the other 3 positions.
3976 * Otherwise we require the 4 'int's to be the same as would be the
3977 * case if we are using a random uuid. So an explicit uuid will be
3978 * accepted as long as all for ints are the same... which shouldn't hurt
3981 info
->uuid
[1] = info
->uuid
[2] = info
->uuid
[3] = info
->uuid
[0];
3984 if (info
->uuid
[0] != info
->uuid
[1] ||
3985 info
->uuid
[1] != info
->uuid
[2] ||
3986 info
->uuid
[2] != info
->uuid
[3])
3992 mpb
->orig_family_num
= info
->uuid
[0];
3994 case UOPT_SPEC_ASSEMBLE
:
4002 /* successful update? recompute checksum */
4004 mpb
->check_sum
= __le32_to_cpu(__gen_imsm_checksum(mpb
));
4009 static size_t disks_to_mpb_size(int disks
)
4013 size
= sizeof(struct imsm_super
);
4014 size
+= (disks
- 1) * sizeof(struct imsm_disk
);
4015 size
+= 2 * sizeof(struct imsm_dev
);
4016 /* up to 2 maps per raid device (-2 for imsm_maps in imsm_dev */
4017 size
+= (4 - 2) * sizeof(struct imsm_map
);
4018 /* 4 possible disk_ord_tbl's */
4019 size
+= 4 * (disks
- 1) * sizeof(__u32
);
4020 /* maximum bbm log */
4021 size
+= sizeof(struct bbm_log
);
4026 static __u64
avail_size_imsm(struct supertype
*st
, __u64 devsize
,
4027 unsigned long long data_offset
)
4029 if (devsize
< (MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
))
4032 return devsize
- (MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
);
4035 static void free_devlist(struct intel_super
*super
)
4037 struct intel_dev
*dv
;
4039 while (super
->devlist
) {
4040 dv
= super
->devlist
->next
;
4041 free(super
->devlist
->dev
);
4042 free(super
->devlist
);
4043 super
->devlist
= dv
;
4047 static void imsm_copy_dev(struct imsm_dev
*dest
, struct imsm_dev
*src
)
4049 memcpy(dest
, src
, sizeof_imsm_dev(src
, 0));
4052 static int compare_super_imsm(struct supertype
*st
, struct supertype
*tst
,
4056 * 0 same, or first was empty, and second was copied
4057 * 1 sb are different
4059 struct intel_super
*first
= st
->sb
;
4060 struct intel_super
*sec
= tst
->sb
;
4068 /* in platform dependent environment test if the disks
4069 * use the same Intel hba
4070 * if not on Intel hba at all, allow anything.
4071 * doesn't check HBAs if num_raid_devs is not set, as it means
4072 * it is a free floating spare, and all spares regardless of HBA type
4073 * will fall into separate container during the assembly
4075 if (first
->hba
&& sec
->hba
&& first
->anchor
->num_raid_devs
!= 0) {
4076 if (first
->hba
->type
!= sec
->hba
->type
) {
4078 pr_err("HBAs of devices do not match %s != %s\n",
4079 get_sys_dev_type(first
->hba
->type
),
4080 get_sys_dev_type(sec
->hba
->type
));
4083 if (first
->orom
!= sec
->orom
) {
4085 pr_err("HBAs of devices do not match %s != %s\n",
4086 first
->hba
->pci_id
, sec
->hba
->pci_id
);
4091 if (first
->anchor
->num_raid_devs
> 0 &&
4092 sec
->anchor
->num_raid_devs
> 0) {
4093 /* Determine if these disks might ever have been
4094 * related. Further disambiguation can only take place
4095 * in load_super_imsm_all
4097 __u32 first_family
= first
->anchor
->orig_family_num
;
4098 __u32 sec_family
= sec
->anchor
->orig_family_num
;
4100 if (memcmp(first
->anchor
->sig
, sec
->anchor
->sig
,
4101 MAX_SIGNATURE_LENGTH
) != 0)
4104 if (first_family
== 0)
4105 first_family
= first
->anchor
->family_num
;
4106 if (sec_family
== 0)
4107 sec_family
= sec
->anchor
->family_num
;
4109 if (first_family
!= sec_family
)
4114 /* if an anchor does not have num_raid_devs set then it is a free
4115 * floating spare. don't assosiate spare with any array, as during assembly
4116 * spares shall fall into separate container, from which they can be moved
4119 if (first
->anchor
->num_raid_devs
^ sec
->anchor
->num_raid_devs
)
4125 static void fd2devname(int fd
, char *name
)
4133 snprintf(name
, MAX_RAID_SERIAL_LEN
, "/dev/%s", nm
);
4136 static int nvme_get_serial(int fd
, void *buf
, size_t buf_len
)
4138 char path
[PATH_MAX
];
4139 char *name
= fd2kname(fd
);
4144 if (strncmp(name
, "nvme", 4) != 0)
4147 if (!diskfd_to_devpath(fd
, 1, path
))
4150 return devpath_to_char(path
, "serial", buf
, buf_len
, 0);
4153 extern int scsi_get_serial(int fd
, void *buf
, size_t buf_len
);
4155 static int imsm_read_serial(int fd
, char *devname
,
4156 __u8
*serial
, size_t serial_buf_len
)
4165 memset(buf
, 0, sizeof(buf
));
4167 if (check_env("IMSM_DEVNAME_AS_SERIAL")) {
4168 memset(serial
, 0, serial_buf_len
);
4169 fd2devname(fd
, (char *) serial
);
4173 rv
= nvme_get_serial(fd
, buf
, sizeof(buf
));
4176 rv
= scsi_get_serial(fd
, buf
, sizeof(buf
));
4180 pr_err("Failed to retrieve serial for %s\n",
4185 /* trim all whitespace and non-printable characters and convert
4188 for (i
= 0, dest
= buf
; i
< sizeof(buf
) && buf
[i
]; i
++) {
4191 /* ':' is reserved for use in placeholder serial
4192 * numbers for missing disks
4203 if (len
> serial_buf_len
) {
4204 /* truncate leading characters */
4205 dest
+= len
- serial_buf_len
;
4206 len
= serial_buf_len
;
4209 memset(serial
, 0, serial_buf_len
);
4210 memcpy(serial
, dest
, len
);
4215 static int serialcmp(__u8
*s1
, __u8
*s2
)
4217 return strncmp((char *) s1
, (char *) s2
, MAX_RAID_SERIAL_LEN
);
4220 static void serialcpy(__u8
*dest
, __u8
*src
)
4222 strncpy((char *) dest
, (char *) src
, MAX_RAID_SERIAL_LEN
);
4225 static struct dl
*serial_to_dl(__u8
*serial
, struct intel_super
*super
)
4229 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
4230 if (serialcmp(dl
->serial
, serial
) == 0)
4236 static struct imsm_disk
*
4237 __serial_to_disk(__u8
*serial
, struct imsm_super
*mpb
, int *idx
)
4241 for (i
= 0; i
< mpb
->num_disks
; i
++) {
4242 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, i
);
4244 if (serialcmp(disk
->serial
, serial
) == 0) {
4255 load_imsm_disk(int fd
, struct intel_super
*super
, char *devname
, int keep_fd
)
4257 struct imsm_disk
*disk
;
4262 __u8 serial
[MAX_RAID_SERIAL_LEN
];
4264 rv
= imsm_read_serial(fd
, devname
, serial
, MAX_RAID_SERIAL_LEN
);
4269 dl
= xcalloc(1, sizeof(*dl
));
4272 dl
->major
= major(stb
.st_rdev
);
4273 dl
->minor
= minor(stb
.st_rdev
);
4274 dl
->next
= super
->disks
;
4275 dl
->fd
= keep_fd
? fd
: -1;
4276 assert(super
->disks
== NULL
);
4278 serialcpy(dl
->serial
, serial
);
4281 fd2devname(fd
, name
);
4283 dl
->devname
= xstrdup(devname
);
4285 dl
->devname
= xstrdup(name
);
4287 /* look up this disk's index in the current anchor */
4288 disk
= __serial_to_disk(dl
->serial
, super
->anchor
, &dl
->index
);
4291 /* only set index on disks that are a member of a
4292 * populated contianer, i.e. one with raid_devs
4294 if (is_failed(&dl
->disk
))
4296 else if (is_spare(&dl
->disk
) || is_journal(&dl
->disk
))
4303 /* When migrating map0 contains the 'destination' state while map1
4304 * contains the current state. When not migrating map0 contains the
4305 * current state. This routine assumes that map[0].map_state is set to
4306 * the current array state before being called.
4308 * Migration is indicated by one of the following states
4309 * 1/ Idle (migr_state=0 map0state=normal||unitialized||degraded||failed)
4310 * 2/ Initialize (migr_state=1 migr_type=MIGR_INIT map0state=normal
4311 * map1state=unitialized)
4312 * 3/ Repair (Resync) (migr_state=1 migr_type=MIGR_REPAIR map0state=normal
4314 * 4/ Rebuild (migr_state=1 migr_type=MIGR_REBUILD map0state=normal
4315 * map1state=degraded)
4316 * 5/ Migration (mig_state=1 migr_type=MIGR_GEN_MIGR map0state=normal
4319 static void migrate(struct imsm_dev
*dev
, struct intel_super
*super
,
4320 __u8 to_state
, int migr_type
)
4322 struct imsm_map
*dest
;
4323 struct imsm_map
*src
= get_imsm_map(dev
, MAP_0
);
4325 dev
->vol
.migr_state
= 1;
4326 set_migr_type(dev
, migr_type
);
4327 set_vol_curr_migr_unit(dev
, 0);
4328 dest
= get_imsm_map(dev
, MAP_1
);
4330 /* duplicate and then set the target end state in map[0] */
4331 memcpy(dest
, src
, sizeof_imsm_map(src
));
4332 if (migr_type
== MIGR_GEN_MIGR
) {
4336 for (i
= 0; i
< src
->num_members
; i
++) {
4337 ord
= __le32_to_cpu(src
->disk_ord_tbl
[i
]);
4338 set_imsm_ord_tbl_ent(src
, i
, ord_to_idx(ord
));
4342 if (migr_type
== MIGR_GEN_MIGR
)
4343 /* Clear migration record */
4344 memset(super
->migr_rec
, 0, sizeof(struct migr_record
));
4346 src
->map_state
= to_state
;
4349 static void end_migration(struct imsm_dev
*dev
, struct intel_super
*super
,
4352 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
4353 struct imsm_map
*prev
= get_imsm_map(dev
, dev
->vol
.migr_state
== 0 ?
4357 /* merge any IMSM_ORD_REBUILD bits that were not successfully
4358 * completed in the last migration.
4360 * FIXME add support for raid-level-migration
4362 if (map_state
!= map
->map_state
&& (is_gen_migration(dev
) == false) &&
4363 prev
->map_state
!= IMSM_T_STATE_UNINITIALIZED
) {
4364 /* when final map state is other than expected
4365 * merge maps (not for migration)
4369 for (i
= 0; i
< prev
->num_members
; i
++)
4370 for (j
= 0; j
< map
->num_members
; j
++)
4371 /* during online capacity expansion
4372 * disks position can be changed
4373 * if takeover is used
4375 if (ord_to_idx(map
->disk_ord_tbl
[j
]) ==
4376 ord_to_idx(prev
->disk_ord_tbl
[i
])) {
4377 map
->disk_ord_tbl
[j
] |=
4378 prev
->disk_ord_tbl
[i
];
4381 failed
= imsm_count_failed(super
, dev
, MAP_0
);
4382 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
4385 dev
->vol
.migr_state
= 0;
4386 set_migr_type(dev
, 0);
4387 set_vol_curr_migr_unit(dev
, 0);
4388 map
->map_state
= map_state
;
4391 static int parse_raid_devices(struct intel_super
*super
)
4394 struct imsm_dev
*dev_new
;
4395 size_t len
, len_migr
;
4397 size_t space_needed
= 0;
4398 struct imsm_super
*mpb
= super
->anchor
;
4400 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
4401 struct imsm_dev
*dev_iter
= __get_imsm_dev(super
->anchor
, i
);
4402 struct intel_dev
*dv
;
4404 len
= sizeof_imsm_dev(dev_iter
, 0);
4405 len_migr
= sizeof_imsm_dev(dev_iter
, 1);
4407 space_needed
+= len_migr
- len
;
4409 dv
= xmalloc(sizeof(*dv
));
4410 if (max_len
< len_migr
)
4412 if (max_len
> len_migr
)
4413 space_needed
+= max_len
- len_migr
;
4414 dev_new
= xmalloc(max_len
);
4415 imsm_copy_dev(dev_new
, dev_iter
);
4418 dv
->next
= super
->devlist
;
4419 super
->devlist
= dv
;
4422 /* ensure that super->buf is large enough when all raid devices
4425 if (__le32_to_cpu(mpb
->mpb_size
) + space_needed
> super
->len
) {
4428 len
= ROUND_UP(__le32_to_cpu(mpb
->mpb_size
) + space_needed
,
4429 super
->sector_size
);
4430 if (posix_memalign(&buf
, MAX_SECTOR_SIZE
, len
) != 0)
4433 memcpy(buf
, super
->buf
, super
->len
);
4434 memset(buf
+ super
->len
, 0, len
- super
->len
);
4440 super
->extra_space
+= space_needed
;
4445 /*******************************************************************************
4446 * Function: check_mpb_migr_compatibility
4447 * Description: Function checks for unsupported migration features:
4448 * - migration optimization area (pba_of_lba0)
4449 * - descending reshape (ascending_migr)
4451 * super : imsm metadata information
4453 * 0 : migration is compatible
4454 * -1 : migration is not compatible
4455 ******************************************************************************/
4456 int check_mpb_migr_compatibility(struct intel_super
*super
)
4458 struct imsm_map
*map0
, *map1
;
4459 struct migr_record
*migr_rec
= super
->migr_rec
;
4462 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
4463 struct imsm_dev
*dev_iter
= __get_imsm_dev(super
->anchor
, i
);
4465 if (dev_iter
->vol
.migr_state
== 1 &&
4466 dev_iter
->vol
.migr_type
== MIGR_GEN_MIGR
) {
4467 /* This device is migrating */
4468 map0
= get_imsm_map(dev_iter
, MAP_0
);
4469 map1
= get_imsm_map(dev_iter
, MAP_1
);
4470 if (pba_of_lba0(map0
) != pba_of_lba0(map1
))
4471 /* migration optimization area was used */
4473 if (migr_rec
->ascending_migr
== 0 &&
4474 migr_rec
->dest_depth_per_unit
> 0)
4475 /* descending reshape not supported yet */
4482 static void __free_imsm(struct intel_super
*super
, int free_disks
);
4484 /* load_imsm_mpb - read matrix metadata
4485 * allocates super->mpb to be freed by free_imsm
4487 static int load_imsm_mpb(int fd
, struct intel_super
*super
, char *devname
)
4489 unsigned long long dsize
;
4490 unsigned long long sectors
;
4491 unsigned int sector_size
= super
->sector_size
;
4493 struct imsm_super
*anchor
;
4496 get_dev_size(fd
, NULL
, &dsize
);
4497 if (dsize
< 2*sector_size
) {
4499 pr_err("%s: device to small for imsm\n",
4504 if (lseek64(fd
, dsize
- (sector_size
* 2), SEEK_SET
) < 0) {
4506 pr_err("Cannot seek to anchor block on %s: %s\n",
4507 devname
, strerror(errno
));
4511 if (posix_memalign((void **)&anchor
, sector_size
, sector_size
) != 0) {
4513 pr_err("Failed to allocate imsm anchor buffer on %s\n", devname
);
4516 if ((unsigned int)read(fd
, anchor
, sector_size
) != sector_size
) {
4518 pr_err("Cannot read anchor block on %s: %s\n",
4519 devname
, strerror(errno
));
4524 if (strncmp((char *) anchor
->sig
, MPB_SIGNATURE
, MPB_SIG_LEN
) != 0) {
4526 pr_err("no IMSM anchor on %s\n", devname
);
4531 __free_imsm(super
, 0);
4532 /* reload capability and hba */
4534 /* capability and hba must be updated with new super allocation */
4535 find_intel_hba_capability(fd
, super
, devname
);
4536 super
->len
= ROUND_UP(anchor
->mpb_size
, sector_size
);
4537 if (posix_memalign(&super
->buf
, MAX_SECTOR_SIZE
, super
->len
) != 0) {
4539 pr_err("unable to allocate %zu byte mpb buffer\n",
4544 memcpy(super
->buf
, anchor
, sector_size
);
4546 sectors
= mpb_sectors(anchor
, sector_size
) - 1;
4549 if (posix_memalign(&super
->migr_rec_buf
, MAX_SECTOR_SIZE
,
4550 MIGR_REC_BUF_SECTORS
*MAX_SECTOR_SIZE
) != 0) {
4551 pr_err("could not allocate migr_rec buffer\n");
4556 super
->clean_migration_record_by_mdmon
= 0;
4559 check_sum
= __gen_imsm_checksum(super
->anchor
);
4560 if (check_sum
!= __le32_to_cpu(super
->anchor
->check_sum
)) {
4562 pr_err("IMSM checksum %x != %x on %s\n",
4564 __le32_to_cpu(super
->anchor
->check_sum
),
4572 /* read the extended mpb */
4573 if (lseek64(fd
, dsize
- (sector_size
* (2 + sectors
)), SEEK_SET
) < 0) {
4575 pr_err("Cannot seek to extended mpb on %s: %s\n",
4576 devname
, strerror(errno
));
4580 if ((unsigned int)read(fd
, super
->buf
+ sector_size
,
4581 super
->len
- sector_size
) != super
->len
- sector_size
) {
4583 pr_err("Cannot read extended mpb on %s: %s\n",
4584 devname
, strerror(errno
));
4588 check_sum
= __gen_imsm_checksum(super
->anchor
);
4589 if (check_sum
!= __le32_to_cpu(super
->anchor
->check_sum
)) {
4591 pr_err("IMSM checksum %x != %x on %s\n",
4592 check_sum
, __le32_to_cpu(super
->anchor
->check_sum
),
4600 static int read_imsm_migr_rec(int fd
, struct intel_super
*super
);
4602 /* clears hi bits in metadata if MPB_ATTRIB_2TB_DISK not set */
4603 static void clear_hi(struct intel_super
*super
)
4605 struct imsm_super
*mpb
= super
->anchor
;
4607 if (mpb
->attributes
& MPB_ATTRIB_2TB_DISK
)
4609 for (i
= 0; i
< mpb
->num_disks
; ++i
) {
4610 struct imsm_disk
*disk
= &mpb
->disk
[i
];
4611 disk
->total_blocks_hi
= 0;
4613 for (i
= 0; i
< mpb
->num_raid_devs
; ++i
) {
4614 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
4615 for (n
= 0; n
< 2; ++n
) {
4616 struct imsm_map
*map
= get_imsm_map(dev
, n
);
4619 map
->pba_of_lba0_hi
= 0;
4620 map
->blocks_per_member_hi
= 0;
4621 map
->num_data_stripes_hi
= 0;
4627 load_and_parse_mpb(int fd
, struct intel_super
*super
, char *devname
, int keep_fd
)
4631 err
= load_imsm_mpb(fd
, super
, devname
);
4634 if (super
->sector_size
== 4096)
4635 convert_from_4k(super
);
4636 err
= load_imsm_disk(fd
, super
, devname
, keep_fd
);
4639 err
= parse_raid_devices(super
);
4642 err
= load_bbm_log(super
);
4647 static void __free_imsm_disk(struct dl
*d
, int do_close
)
4659 static void free_imsm_disks(struct intel_super
*super
)
4663 while (super
->disks
) {
4665 super
->disks
= d
->next
;
4666 __free_imsm_disk(d
, 1);
4668 while (super
->disk_mgmt_list
) {
4669 d
= super
->disk_mgmt_list
;
4670 super
->disk_mgmt_list
= d
->next
;
4671 __free_imsm_disk(d
, 1);
4673 while (super
->missing
) {
4675 super
->missing
= d
->next
;
4676 __free_imsm_disk(d
, 1);
4681 /* free all the pieces hanging off of a super pointer */
4682 static void __free_imsm(struct intel_super
*super
, int free_disks
)
4684 struct intel_hba
*elem
, *next
;
4690 /* unlink capability description */
4692 if (super
->migr_rec_buf
) {
4693 free(super
->migr_rec_buf
);
4694 super
->migr_rec_buf
= NULL
;
4697 free_imsm_disks(super
);
4698 free_devlist(super
);
4702 free((void *)elem
->path
);
4708 free(super
->bbm_log
);
4712 static void free_imsm(struct intel_super
*super
)
4714 __free_imsm(super
, 1);
4715 free(super
->bb
.entries
);
4719 static void free_super_imsm(struct supertype
*st
)
4721 struct intel_super
*super
= st
->sb
;
4730 static struct intel_super
*alloc_super(void)
4732 struct intel_super
*super
= xcalloc(1, sizeof(*super
));
4734 super
->current_vol
= -1;
4735 super
->create_offset
= ~((unsigned long long) 0);
4737 super
->bb
.entries
= xmalloc(BBM_LOG_MAX_ENTRIES
*
4738 sizeof(struct md_bb_entry
));
4739 if (!super
->bb
.entries
) {
4748 * find and allocate hba and OROM/EFI based on valid fd of RAID component device
4750 static int find_intel_hba_capability(int fd
, struct intel_super
*super
, char *devname
)
4752 struct sys_dev
*hba_name
;
4755 if (is_fd_valid(fd
) && test_partition(fd
)) {
4756 pr_err("imsm: %s is a partition, cannot be used in IMSM\n",
4760 if (!is_fd_valid(fd
) || check_no_platform()) {
4765 hba_name
= find_disk_attached_hba(fd
, NULL
);
4768 pr_err("%s is not attached to Intel(R) RAID controller.\n",
4772 rv
= attach_hba_to_super(super
, hba_name
);
4775 struct intel_hba
*hba
= super
->hba
;
4777 pr_err("%s is attached to Intel(R) %s %s (%s),\n"
4778 " but the container is assigned to Intel(R) %s %s (",
4780 get_sys_dev_type(hba_name
->type
),
4781 hba_name
->type
== SYS_DEV_VMD
|| hba_name
->type
== SYS_DEV_SATA_VMD
?
4782 "domain" : "RAID controller",
4783 hba_name
->pci_id
? : "Err!",
4784 get_sys_dev_type(super
->hba
->type
),
4785 hba
->type
== SYS_DEV_VMD
|| hba_name
->type
== SYS_DEV_SATA_VMD
?
4786 "domain" : "RAID controller");
4789 fprintf(stderr
, "%s", hba
->pci_id
? : "Err!");
4791 fprintf(stderr
, ", ");
4794 fprintf(stderr
, ").\n"
4795 " Mixing devices attached to different controllers is not allowed.\n");
4799 super
->orom
= find_imsm_capability(hba_name
);
4806 /* find_missing - helper routine for load_super_imsm_all that identifies
4807 * disks that have disappeared from the system. This routine relies on
4808 * the mpb being uptodate, which it is at load time.
4810 static int find_missing(struct intel_super
*super
)
4813 struct imsm_super
*mpb
= super
->anchor
;
4815 struct imsm_disk
*disk
;
4817 for (i
= 0; i
< mpb
->num_disks
; i
++) {
4818 disk
= __get_imsm_disk(mpb
, i
);
4819 dl
= serial_to_dl(disk
->serial
, super
);
4823 dl
= xmalloc(sizeof(*dl
));
4827 dl
->devname
= xstrdup("missing");
4829 serialcpy(dl
->serial
, disk
->serial
);
4832 dl
->next
= super
->missing
;
4833 super
->missing
= dl
;
4839 static struct intel_disk
*disk_list_get(__u8
*serial
, struct intel_disk
*disk_list
)
4841 struct intel_disk
*idisk
= disk_list
;
4844 if (serialcmp(idisk
->disk
.serial
, serial
) == 0)
4846 idisk
= idisk
->next
;
4852 static int __prep_thunderdome(struct intel_super
**table
, int tbl_size
,
4853 struct intel_super
*super
,
4854 struct intel_disk
**disk_list
)
4856 struct imsm_disk
*d
= &super
->disks
->disk
;
4857 struct imsm_super
*mpb
= super
->anchor
;
4860 for (i
= 0; i
< tbl_size
; i
++) {
4861 struct imsm_super
*tbl_mpb
= table
[i
]->anchor
;
4862 struct imsm_disk
*tbl_d
= &table
[i
]->disks
->disk
;
4864 if (tbl_mpb
->family_num
== mpb
->family_num
) {
4865 if (tbl_mpb
->check_sum
== mpb
->check_sum
) {
4866 dprintf("mpb from %d:%d matches %d:%d\n",
4867 super
->disks
->major
,
4868 super
->disks
->minor
,
4869 table
[i
]->disks
->major
,
4870 table
[i
]->disks
->minor
);
4874 if (((is_configured(d
) && !is_configured(tbl_d
)) ||
4875 is_configured(d
) == is_configured(tbl_d
)) &&
4876 tbl_mpb
->generation_num
< mpb
->generation_num
) {
4877 /* current version of the mpb is a
4878 * better candidate than the one in
4879 * super_table, but copy over "cross
4880 * generational" status
4882 struct intel_disk
*idisk
;
4884 dprintf("mpb from %d:%d replaces %d:%d\n",
4885 super
->disks
->major
,
4886 super
->disks
->minor
,
4887 table
[i
]->disks
->major
,
4888 table
[i
]->disks
->minor
);
4890 idisk
= disk_list_get(tbl_d
->serial
, *disk_list
);
4891 if (idisk
&& is_failed(&idisk
->disk
))
4892 tbl_d
->status
|= FAILED_DISK
;
4895 struct intel_disk
*idisk
;
4896 struct imsm_disk
*disk
;
4898 /* tbl_mpb is more up to date, but copy
4899 * over cross generational status before
4902 disk
= __serial_to_disk(d
->serial
, mpb
, NULL
);
4903 if (disk
&& is_failed(disk
))
4904 d
->status
|= FAILED_DISK
;
4906 idisk
= disk_list_get(d
->serial
, *disk_list
);
4909 if (disk
&& is_configured(disk
))
4910 idisk
->disk
.status
|= CONFIGURED_DISK
;
4913 dprintf("mpb from %d:%d prefer %d:%d\n",
4914 super
->disks
->major
,
4915 super
->disks
->minor
,
4916 table
[i
]->disks
->major
,
4917 table
[i
]->disks
->minor
);
4925 table
[tbl_size
++] = super
;
4929 /* update/extend the merged list of imsm_disk records */
4930 for (j
= 0; j
< mpb
->num_disks
; j
++) {
4931 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, j
);
4932 struct intel_disk
*idisk
;
4934 idisk
= disk_list_get(disk
->serial
, *disk_list
);
4936 idisk
->disk
.status
|= disk
->status
;
4937 if (is_configured(&idisk
->disk
) ||
4938 is_failed(&idisk
->disk
))
4939 idisk
->disk
.status
&= ~(SPARE_DISK
);
4941 idisk
= xcalloc(1, sizeof(*idisk
));
4942 idisk
->owner
= IMSM_UNKNOWN_OWNER
;
4943 idisk
->disk
= *disk
;
4944 idisk
->next
= *disk_list
;
4948 if (serialcmp(idisk
->disk
.serial
, d
->serial
) == 0)
4955 static struct intel_super
*
4956 validate_members(struct intel_super
*super
, struct intel_disk
*disk_list
,
4959 struct imsm_super
*mpb
= super
->anchor
;
4963 for (i
= 0; i
< mpb
->num_disks
; i
++) {
4964 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, i
);
4965 struct intel_disk
*idisk
;
4967 idisk
= disk_list_get(disk
->serial
, disk_list
);
4969 if (idisk
->owner
== owner
||
4970 idisk
->owner
== IMSM_UNKNOWN_OWNER
)
4973 dprintf("'%.16s' owner %d != %d\n",
4974 disk
->serial
, idisk
->owner
,
4977 dprintf("unknown disk %x [%d]: %.16s\n",
4978 __le32_to_cpu(mpb
->family_num
), i
,
4984 if (ok_count
== mpb
->num_disks
)
4989 static void show_conflicts(__u32 family_num
, struct intel_super
*super_list
)
4991 struct intel_super
*s
;
4993 for (s
= super_list
; s
; s
= s
->next
) {
4994 if (family_num
!= s
->anchor
->family_num
)
4996 pr_err("Conflict, offlining family %#x on '%s'\n",
4997 __le32_to_cpu(family_num
), s
->disks
->devname
);
5001 static struct intel_super
*
5002 imsm_thunderdome(struct intel_super
**super_list
, int len
)
5004 struct intel_super
*super_table
[len
];
5005 struct intel_disk
*disk_list
= NULL
;
5006 struct intel_super
*champion
, *spare
;
5007 struct intel_super
*s
, **del
;
5012 memset(super_table
, 0, sizeof(super_table
));
5013 for (s
= *super_list
; s
; s
= s
->next
)
5014 tbl_size
= __prep_thunderdome(super_table
, tbl_size
, s
, &disk_list
);
5016 for (i
= 0; i
< tbl_size
; i
++) {
5017 struct imsm_disk
*d
;
5018 struct intel_disk
*idisk
;
5019 struct imsm_super
*mpb
= super_table
[i
]->anchor
;
5022 d
= &s
->disks
->disk
;
5024 /* 'd' must appear in merged disk list for its
5025 * configuration to be valid
5027 idisk
= disk_list_get(d
->serial
, disk_list
);
5028 if (idisk
&& idisk
->owner
== i
)
5029 s
= validate_members(s
, disk_list
, i
);
5034 dprintf("marking family: %#x from %d:%d offline\n",
5036 super_table
[i
]->disks
->major
,
5037 super_table
[i
]->disks
->minor
);
5041 /* This is where the mdadm implementation differs from the Windows
5042 * driver which has no strict concept of a container. We can only
5043 * assemble one family from a container, so when returning a prodigal
5044 * array member to this system the code will not be able to disambiguate
5045 * the container contents that should be assembled ("foreign" versus
5046 * "local"). It requires user intervention to set the orig_family_num
5047 * to a new value to establish a new container. The Windows driver in
5048 * this situation fixes up the volume name in place and manages the
5049 * foreign array as an independent entity.
5054 for (i
= 0; i
< tbl_size
; i
++) {
5055 struct intel_super
*tbl_ent
= super_table
[i
];
5061 if (tbl_ent
->anchor
->num_raid_devs
== 0) {
5066 if (s
&& !is_spare
) {
5067 show_conflicts(tbl_ent
->anchor
->family_num
, *super_list
);
5069 } else if (!s
&& !is_spare
)
5082 pr_err("Chose family %#x on '%s', assemble conflicts to new container with '--update=uuid'\n",
5083 __le32_to_cpu(s
->anchor
->family_num
), s
->disks
->devname
);
5085 /* collect all dl's onto 'champion', and update them to
5086 * champion's version of the status
5088 for (s
= *super_list
; s
; s
= s
->next
) {
5089 struct imsm_super
*mpb
= champion
->anchor
;
5090 struct dl
*dl
= s
->disks
;
5095 mpb
->attributes
|= s
->anchor
->attributes
& MPB_ATTRIB_2TB_DISK
;
5097 for (i
= 0; i
< mpb
->num_disks
; i
++) {
5098 struct imsm_disk
*disk
;
5100 disk
= __serial_to_disk(dl
->serial
, mpb
, &dl
->index
);
5103 /* only set index on disks that are a member of
5104 * a populated contianer, i.e. one with
5107 if (is_failed(&dl
->disk
))
5109 else if (is_spare(&dl
->disk
))
5115 if (i
>= mpb
->num_disks
) {
5116 struct intel_disk
*idisk
;
5118 idisk
= disk_list_get(dl
->serial
, disk_list
);
5119 if (idisk
&& is_spare(&idisk
->disk
) &&
5120 !is_failed(&idisk
->disk
) && !is_configured(&idisk
->disk
))
5128 dl
->next
= champion
->disks
;
5129 champion
->disks
= dl
;
5133 /* delete 'champion' from super_list */
5134 for (del
= super_list
; *del
; ) {
5135 if (*del
== champion
) {
5136 *del
= (*del
)->next
;
5139 del
= &(*del
)->next
;
5141 champion
->next
= NULL
;
5145 struct intel_disk
*idisk
= disk_list
;
5147 disk_list
= disk_list
->next
;
5155 get_sra_super_block(int fd
, struct intel_super
**super_list
, char *devname
, int *max
, int keep_fd
);
5156 static int get_super_block(struct intel_super
**super_list
, char *devnm
, char *devname
,
5157 int major
, int minor
, int keep_fd
);
5159 get_devlist_super_block(struct md_list
*devlist
, struct intel_super
**super_list
,
5160 int *max
, int keep_fd
);
5162 static int load_super_imsm_all(struct supertype
*st
, int fd
, void **sbp
,
5163 char *devname
, struct md_list
*devlist
,
5166 struct intel_super
*super_list
= NULL
;
5167 struct intel_super
*super
= NULL
;
5171 if (is_fd_valid(fd
))
5172 /* 'fd' is an opened container */
5173 err
= get_sra_super_block(fd
, &super_list
, devname
, &i
, keep_fd
);
5175 /* get super block from devlist devices */
5176 err
= get_devlist_super_block(devlist
, &super_list
, &i
, keep_fd
);
5179 /* all mpbs enter, maybe one leaves */
5180 super
= imsm_thunderdome(&super_list
, i
);
5186 if (find_missing(super
) != 0) {
5192 /* load migration record */
5193 err
= load_imsm_migr_rec(super
);
5195 /* migration is in progress,
5196 * but migr_rec cannot be loaded,
5202 /* Check migration compatibility */
5203 if (err
== 0 && check_mpb_migr_compatibility(super
) != 0) {
5204 pr_err("Unsupported migration detected");
5206 fprintf(stderr
, " on %s\n", devname
);
5208 fprintf(stderr
, " (IMSM).\n");
5217 while (super_list
) {
5218 struct intel_super
*s
= super_list
;
5220 super_list
= super_list
->next
;
5228 if (is_fd_valid(fd
))
5229 strcpy(st
->container_devnm
, fd2devnm(fd
));
5231 st
->container_devnm
[0] = 0;
5232 if (err
== 0 && st
->ss
== NULL
) {
5233 st
->ss
= &super_imsm
;
5234 st
->minor_version
= 0;
5235 st
->max_devs
= IMSM_MAX_DEVICES
;
5241 get_devlist_super_block(struct md_list
*devlist
, struct intel_super
**super_list
,
5242 int *max
, int keep_fd
)
5244 struct md_list
*tmpdev
;
5248 for (i
= 0, tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
5249 if (tmpdev
->used
!= 1)
5251 if (tmpdev
->container
== 1) {
5253 int fd
= dev_open(tmpdev
->devname
, O_RDONLY
|O_EXCL
);
5254 if (!is_fd_valid(fd
)) {
5255 pr_err("cannot open device %s: %s\n",
5256 tmpdev
->devname
, strerror(errno
));
5260 err
= get_sra_super_block(fd
, super_list
,
5261 tmpdev
->devname
, &lmax
,
5270 int major
= major(tmpdev
->st_rdev
);
5271 int minor
= minor(tmpdev
->st_rdev
);
5272 err
= get_super_block(super_list
,
5289 static int get_super_block(struct intel_super
**super_list
, char *devnm
, char *devname
,
5290 int major
, int minor
, int keep_fd
)
5292 struct intel_super
*s
;
5304 sprintf(nm
, "%d:%d", major
, minor
);
5305 dfd
= dev_open(nm
, O_RDWR
);
5306 if (!is_fd_valid(dfd
)) {
5311 if (!get_dev_sector_size(dfd
, NULL
, &s
->sector_size
)) {
5315 find_intel_hba_capability(dfd
, s
, devname
);
5316 err
= load_and_parse_mpb(dfd
, s
, NULL
, keep_fd
);
5318 /* retry the load if we might have raced against mdmon */
5319 if (err
== 3 && devnm
&& mdmon_running(devnm
))
5320 for (retry
= 0; retry
< 3; retry
++) {
5321 sleep_for(0, MSEC_TO_NSEC(3), true);
5322 err
= load_and_parse_mpb(dfd
, s
, NULL
, keep_fd
);
5328 s
->next
= *super_list
;
5342 get_sra_super_block(int fd
, struct intel_super
**super_list
, char *devname
, int *max
, int keep_fd
)
5349 sra
= sysfs_read(fd
, NULL
, GET_LEVEL
|GET_VERSION
|GET_DEVS
|GET_STATE
);
5353 if (sra
->array
.major_version
!= -1 ||
5354 sra
->array
.minor_version
!= -2 ||
5355 strcmp(sra
->text_version
, "imsm") != 0) {
5360 devnm
= fd2devnm(fd
);
5361 for (sd
= sra
->devs
, i
= 0; sd
; sd
= sd
->next
, i
++) {
5362 if (get_super_block(super_list
, devnm
, devname
,
5363 sd
->disk
.major
, sd
->disk
.minor
, keep_fd
) != 0) {
5374 static int load_container_imsm(struct supertype
*st
, int fd
, char *devname
)
5376 return load_super_imsm_all(st
, fd
, &st
->sb
, devname
, NULL
, 1);
5379 static int load_super_imsm(struct supertype
*st
, int fd
, char *devname
)
5381 struct intel_super
*super
;
5385 if (test_partition(fd
))
5386 /* IMSM not allowed on partitions */
5389 free_super_imsm(st
);
5391 super
= alloc_super();
5395 if (!get_dev_sector_size(fd
, NULL
, &super
->sector_size
)) {
5399 /* Load hba and capabilities if they exist.
5400 * But do not preclude loading metadata in case capabilities or hba are
5401 * non-compliant and ignore_hw_compat is set.
5403 rv
= find_intel_hba_capability(fd
, super
, devname
);
5404 /* no orom/efi or non-intel hba of the disk */
5405 if (rv
!= 0 && st
->ignore_hw_compat
== 0) {
5407 pr_err("No OROM/EFI properties for %s\n", devname
);
5411 rv
= load_and_parse_mpb(fd
, super
, devname
, 0);
5413 /* retry the load if we might have raced against mdmon */
5415 struct mdstat_ent
*mdstat
= NULL
;
5416 char *name
= fd2kname(fd
);
5419 mdstat
= mdstat_by_component(name
);
5421 if (mdstat
&& mdmon_running(mdstat
->devnm
) && getpid() != mdmon_pid(mdstat
->devnm
)) {
5422 for (retry
= 0; retry
< 3; retry
++) {
5423 sleep_for(0, MSEC_TO_NSEC(3), true);
5424 rv
= load_and_parse_mpb(fd
, super
, devname
, 0);
5430 free_mdstat(mdstat
);
5435 pr_err("Failed to load all information sections on %s\n", devname
);
5441 if (st
->ss
== NULL
) {
5442 st
->ss
= &super_imsm
;
5443 st
->minor_version
= 0;
5444 st
->max_devs
= IMSM_MAX_DEVICES
;
5447 /* load migration record */
5448 if (load_imsm_migr_rec(super
) == 0) {
5449 /* Check for unsupported migration features */
5450 if (check_mpb_migr_compatibility(super
) != 0) {
5451 pr_err("Unsupported migration detected");
5453 fprintf(stderr
, " on %s\n", devname
);
5455 fprintf(stderr
, " (IMSM).\n");
5463 static __u16
info_to_blocks_per_strip(mdu_array_info_t
*info
)
5465 if (info
->level
== 1)
5467 return info
->chunk_size
>> 9;
5470 static unsigned long long info_to_blocks_per_member(mdu_array_info_t
*info
,
5471 unsigned long long size
)
5473 if (info
->level
== 1)
5476 return (size
* 2) & ~(info_to_blocks_per_strip(info
) - 1);
5479 static void imsm_update_version_info(struct intel_super
*super
)
5481 /* update the version and attributes */
5482 struct imsm_super
*mpb
= super
->anchor
;
5484 struct imsm_dev
*dev
;
5485 struct imsm_map
*map
;
5488 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
5489 dev
= get_imsm_dev(super
, i
);
5490 map
= get_imsm_map(dev
, MAP_0
);
5491 if (__le32_to_cpu(dev
->size_high
) > 0)
5492 mpb
->attributes
|= MPB_ATTRIB_2TB
;
5494 /* FIXME detect when an array spans a port multiplier */
5496 mpb
->attributes
|= MPB_ATTRIB_PM
;
5499 if (mpb
->num_raid_devs
> 1 ||
5500 mpb
->attributes
!= MPB_ATTRIB_CHECKSUM_VERIFY
) {
5501 version
= MPB_VERSION_ATTRIBS
;
5502 switch (get_imsm_raid_level(map
)) {
5503 case 0: mpb
->attributes
|= MPB_ATTRIB_RAID0
; break;
5504 case 1: mpb
->attributes
|= MPB_ATTRIB_RAID1
; break;
5505 case 10: mpb
->attributes
|= MPB_ATTRIB_RAID10
; break;
5506 case 5: mpb
->attributes
|= MPB_ATTRIB_RAID5
; break;
5509 if (map
->num_members
>= 5)
5510 version
= MPB_VERSION_5OR6_DISK_ARRAY
;
5511 else if (dev
->status
== DEV_CLONE_N_GO
)
5512 version
= MPB_VERSION_CNG
;
5513 else if (get_imsm_raid_level(map
) == 5)
5514 version
= MPB_VERSION_RAID5
;
5515 else if (map
->num_members
>= 3)
5516 version
= MPB_VERSION_3OR4_DISK_ARRAY
;
5517 else if (get_imsm_raid_level(map
) == 1)
5518 version
= MPB_VERSION_RAID1
;
5520 version
= MPB_VERSION_RAID0
;
5522 strcpy(((char *) mpb
->sig
) + strlen(MPB_SIGNATURE
), version
);
5527 * imsm_check_name() - check imsm naming criteria.
5528 * @super: &intel_super pointer, not NULL.
5529 * @name: name to check.
5530 * @verbose: verbose level.
5532 * Name must be no longer than &MAX_RAID_SERIAL_LEN and must be unique across volumes.
5534 * Returns: &true if @name matches, &false otherwise.
5536 static bool imsm_is_name_allowed(struct intel_super
*super
, const char * const name
,
5539 struct imsm_super
*mpb
= super
->anchor
;
5542 if (is_string_lq(name
, MAX_RAID_SERIAL_LEN
+ 1) == false) {
5543 pr_vrb("imsm: Name \"%s\" is too long\n", name
);
5547 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
5548 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
5550 if (strncmp((char *) dev
->volume
, name
, MAX_RAID_SERIAL_LEN
) == 0) {
5551 pr_vrb("imsm: Name \"%s\" already exists\n", name
);
5559 static int init_super_imsm_volume(struct supertype
*st
, mdu_array_info_t
*info
,
5560 struct shape
*s
, char *name
,
5561 char *homehost
, int *uuid
,
5562 long long data_offset
)
5564 /* We are creating a volume inside a pre-existing container.
5565 * so st->sb is already set.
5567 struct intel_super
*super
= st
->sb
;
5568 unsigned int sector_size
= super
->sector_size
;
5569 struct imsm_super
*mpb
= super
->anchor
;
5570 struct intel_dev
*dv
;
5571 struct imsm_dev
*dev
;
5572 struct imsm_vol
*vol
;
5573 struct imsm_map
*map
;
5574 int idx
= mpb
->num_raid_devs
;
5577 unsigned long long array_blocks
;
5578 size_t size_old
, size_new
;
5579 unsigned int data_disks
;
5580 unsigned long long size_per_member
;
5582 if (super
->orom
&& mpb
->num_raid_devs
>= super
->orom
->vpa
) {
5583 pr_err("This imsm-container already has the maximum of %d volumes\n", super
->orom
->vpa
);
5587 /* ensure the mpb is large enough for the new data */
5588 size_old
= __le32_to_cpu(mpb
->mpb_size
);
5589 size_new
= disks_to_mpb_size(info
->nr_disks
);
5590 if (size_new
> size_old
) {
5592 size_t size_round
= ROUND_UP(size_new
, sector_size
);
5594 if (posix_memalign(&mpb_new
, sector_size
, size_round
) != 0) {
5595 pr_err("could not allocate new mpb\n");
5598 if (posix_memalign(&super
->migr_rec_buf
, MAX_SECTOR_SIZE
,
5599 MIGR_REC_BUF_SECTORS
*
5600 MAX_SECTOR_SIZE
) != 0) {
5601 pr_err("could not allocate migr_rec buffer\n");
5607 memcpy(mpb_new
, mpb
, size_old
);
5610 super
->anchor
= mpb_new
;
5611 mpb
->mpb_size
= __cpu_to_le32(size_new
);
5612 memset(mpb_new
+ size_old
, 0, size_round
- size_old
);
5613 super
->len
= size_round
;
5615 super
->current_vol
= idx
;
5617 /* handle 'failed_disks' by either:
5618 * a) create dummy disk entries in the table if this the first
5619 * volume in the array. We add them here as this is the only
5620 * opportunity to add them. add_to_super_imsm_volume()
5621 * handles the non-failed disks and continues incrementing
5623 * b) validate that 'failed_disks' matches the current number
5624 * of missing disks if the container is populated
5626 if (super
->current_vol
== 0) {
5628 for (i
= 0; i
< info
->failed_disks
; i
++) {
5629 struct imsm_disk
*disk
;
5632 disk
= __get_imsm_disk(mpb
, i
);
5633 disk
->status
= CONFIGURED_DISK
| FAILED_DISK
;
5634 disk
->scsi_id
= __cpu_to_le32(~(__u32
)0);
5635 snprintf((char *) disk
->serial
, MAX_RAID_SERIAL_LEN
,
5636 "missing:%d", (__u8
)i
);
5638 find_missing(super
);
5643 for (d
= super
->missing
; d
; d
= d
->next
)
5645 if (info
->failed_disks
> missing
) {
5646 pr_err("unable to add 'missing' disk to container\n");
5651 if (imsm_is_name_allowed(super
, name
, 1) == false)
5654 dv
= xmalloc(sizeof(*dv
));
5655 dev
= xcalloc(1, sizeof(*dev
) + sizeof(__u32
) * (info
->raid_disks
- 1));
5657 * Explicitly allow truncating to not confuse gcc's
5658 * -Werror=stringop-truncation
5660 namelen
= min((int) strlen(name
), MAX_RAID_SERIAL_LEN
);
5661 memcpy(dev
->volume
, name
, namelen
);
5662 array_blocks
= calc_array_size(info
->level
, info
->raid_disks
,
5663 info
->layout
, info
->chunk_size
,
5664 s
->size
* BLOCKS_PER_KB
);
5665 data_disks
= get_data_disks(info
->level
, info
->layout
,
5667 array_blocks
= round_size_to_mb(array_blocks
, data_disks
);
5668 size_per_member
= array_blocks
/ data_disks
;
5670 set_imsm_dev_size(dev
, array_blocks
);
5671 dev
->status
= (DEV_READ_COALESCING
| DEV_WRITE_COALESCING
);
5673 vol
->migr_state
= 0;
5674 set_migr_type(dev
, MIGR_INIT
);
5675 vol
->dirty
= !info
->state
;
5676 set_vol_curr_migr_unit(dev
, 0);
5677 map
= get_imsm_map(dev
, MAP_0
);
5678 set_pba_of_lba0(map
, super
->create_offset
);
5679 map
->blocks_per_strip
= __cpu_to_le16(info_to_blocks_per_strip(info
));
5680 map
->failed_disk_num
= ~0;
5681 if (info
->level
> 0)
5682 map
->map_state
= (info
->state
? IMSM_T_STATE_NORMAL
5683 : IMSM_T_STATE_UNINITIALIZED
);
5685 map
->map_state
= info
->failed_disks
? IMSM_T_STATE_FAILED
:
5686 IMSM_T_STATE_NORMAL
;
5689 if (info
->level
== 1 && info
->raid_disks
> 2) {
5692 pr_err("imsm does not support more than 2 disksin a raid1 volume\n");
5696 map
->raid_level
= info
->level
;
5697 if (info
->level
== 10)
5698 map
->raid_level
= 1;
5699 set_num_domains(map
);
5701 size_per_member
+= NUM_BLOCKS_DIRTY_STRIPE_REGION
;
5702 set_blocks_per_member(map
, info_to_blocks_per_member(info
,
5706 map
->num_members
= info
->raid_disks
;
5707 update_num_data_stripes(map
, array_blocks
);
5708 for (i
= 0; i
< map
->num_members
; i
++) {
5709 /* initialized in add_to_super */
5710 set_imsm_ord_tbl_ent(map
, i
, IMSM_ORD_REBUILD
);
5712 mpb
->num_raid_devs
++;
5713 mpb
->num_raid_devs_created
++;
5714 dev
->my_vol_raid_dev_num
= mpb
->num_raid_devs_created
;
5716 if (s
->consistency_policy
<= CONSISTENCY_POLICY_RESYNC
) {
5717 dev
->rwh_policy
= RWH_MULTIPLE_OFF
;
5718 } else if (s
->consistency_policy
== CONSISTENCY_POLICY_PPL
) {
5719 dev
->rwh_policy
= RWH_MULTIPLE_DISTRIBUTED
;
5723 pr_err("imsm does not support consistency policy %s\n",
5724 map_num_s(consistency_policies
, s
->consistency_policy
));
5729 dv
->index
= super
->current_vol
;
5730 dv
->next
= super
->devlist
;
5731 super
->devlist
= dv
;
5733 imsm_update_version_info(super
);
5738 static int init_super_imsm(struct supertype
*st
, mdu_array_info_t
*info
,
5739 struct shape
*s
, char *name
,
5740 char *homehost
, int *uuid
,
5741 unsigned long long data_offset
)
5743 /* This is primarily called by Create when creating a new array.
5744 * We will then get add_to_super called for each component, and then
5745 * write_init_super called to write it out to each device.
5746 * For IMSM, Create can create on fresh devices or on a pre-existing
5748 * To create on a pre-existing array a different method will be called.
5749 * This one is just for fresh drives.
5751 struct intel_super
*super
;
5752 struct imsm_super
*mpb
;
5756 if (data_offset
!= INVALID_SECTORS
) {
5757 pr_err("data-offset not supported by imsm\n");
5762 return init_super_imsm_volume(st
, info
, s
, name
, homehost
, uuid
,
5766 mpb_size
= disks_to_mpb_size(info
->nr_disks
);
5768 mpb_size
= MAX_SECTOR_SIZE
;
5770 super
= alloc_super();
5772 posix_memalign(&super
->buf
, MAX_SECTOR_SIZE
, mpb_size
) != 0) {
5777 pr_err("could not allocate superblock\n");
5780 if (posix_memalign(&super
->migr_rec_buf
, MAX_SECTOR_SIZE
,
5781 MIGR_REC_BUF_SECTORS
*MAX_SECTOR_SIZE
) != 0) {
5782 pr_err("could not allocate migr_rec buffer\n");
5787 memset(super
->buf
, 0, mpb_size
);
5789 mpb
->mpb_size
= __cpu_to_le32(mpb_size
);
5793 /* zeroing superblock */
5797 mpb
->attributes
= MPB_ATTRIB_CHECKSUM_VERIFY
;
5799 version
= (char *) mpb
->sig
;
5800 strcpy(version
, MPB_SIGNATURE
);
5801 version
+= strlen(MPB_SIGNATURE
);
5802 strcpy(version
, MPB_VERSION_RAID0
);
5807 static int drive_validate_sector_size(struct intel_super
*super
, struct dl
*dl
)
5809 unsigned int member_sector_size
;
5811 if (!is_fd_valid(dl
->fd
)) {
5812 pr_err("Invalid file descriptor for %s\n", dl
->devname
);
5816 if (!get_dev_sector_size(dl
->fd
, dl
->devname
, &member_sector_size
))
5818 if (member_sector_size
!= super
->sector_size
)
5823 static int add_to_super_imsm_volume(struct supertype
*st
, mdu_disk_info_t
*dk
,
5824 int fd
, char *devname
)
5826 struct intel_super
*super
= st
->sb
;
5827 struct imsm_super
*mpb
= super
->anchor
;
5828 struct imsm_disk
*_disk
;
5829 struct imsm_dev
*dev
;
5830 struct imsm_map
*map
;
5835 if (!is_fd_valid(fd
))
5838 dev
= get_imsm_dev(super
, super
->current_vol
);
5839 map
= get_imsm_map(dev
, MAP_0
);
5841 if (! (dk
->state
& (1<<MD_DISK_SYNC
))) {
5842 pr_err("%s: Cannot add spare devices to IMSM volume\n",
5847 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
5849 if (dl
->raiddisk
== dk
->raid_disk
)
5851 } else if (dl
->major
== dk
->major
&& dl
->minor
== dk
->minor
)
5857 pr_err("%s is not a member of the same container.\n",
5862 if (!autolayout
&& super
->current_vol
> 0) {
5863 int _slot
= get_disk_slot_in_dev(super
, 0, dl
->index
);
5865 if (_slot
!= dk
->raid_disk
) {
5866 pr_err("Member %s is in %d slot for the first volume, but is in %d slot for a new volume.\n",
5867 dl
->devname
, _slot
, dk
->raid_disk
);
5868 pr_err("Raid members are in different order than for the first volume, aborting.\n");
5873 if (mpb
->num_disks
== 0)
5874 if (!get_dev_sector_size(dl
->fd
, dl
->devname
,
5875 &super
->sector_size
))
5878 if (!drive_validate_sector_size(super
, dl
)) {
5879 pr_err("Combining drives of different sector size in one volume is not allowed\n");
5883 /* add a pristine spare to the metadata */
5884 if (dl
->index
< 0) {
5885 dl
->index
= super
->anchor
->num_disks
;
5886 super
->anchor
->num_disks
++;
5888 /* Check the device has not already been added */
5889 slot
= get_imsm_disk_slot(map
, dl
->index
);
5891 (get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
) & IMSM_ORD_REBUILD
) == 0) {
5892 pr_err("%s has been included in this array twice\n",
5896 set_imsm_ord_tbl_ent(map
, dk
->raid_disk
, dl
->index
);
5897 dl
->disk
.status
= CONFIGURED_DISK
;
5899 /* update size of 'missing' disks to be at least as large as the
5900 * largest acitve member (we only have dummy missing disks when
5901 * creating the first volume)
5903 if (super
->current_vol
== 0) {
5904 for (df
= super
->missing
; df
; df
= df
->next
) {
5905 if (total_blocks(&dl
->disk
) > total_blocks(&df
->disk
))
5906 set_total_blocks(&df
->disk
, total_blocks(&dl
->disk
));
5907 _disk
= __get_imsm_disk(mpb
, df
->index
);
5912 /* refresh unset/failed slots to point to valid 'missing' entries */
5913 for (df
= super
->missing
; df
; df
= df
->next
)
5914 for (slot
= 0; slot
< mpb
->num_disks
; slot
++) {
5915 __u32 ord
= get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
);
5917 if ((ord
& IMSM_ORD_REBUILD
) == 0)
5919 set_imsm_ord_tbl_ent(map
, slot
, df
->index
| IMSM_ORD_REBUILD
);
5920 if (is_gen_migration(dev
)) {
5921 struct imsm_map
*map2
= get_imsm_map(dev
,
5923 int slot2
= get_imsm_disk_slot(map2
, df
->index
);
5924 if (slot2
< map2
->num_members
&& slot2
>= 0) {
5925 __u32 ord2
= get_imsm_ord_tbl_ent(dev
,
5928 if ((unsigned)df
->index
==
5930 set_imsm_ord_tbl_ent(map2
,
5936 dprintf("set slot:%d to missing disk:%d\n", slot
, df
->index
);
5940 /* if we are creating the first raid device update the family number */
5941 if (super
->current_vol
== 0) {
5943 struct imsm_dev
*_dev
= __get_imsm_dev(mpb
, 0);
5945 _disk
= __get_imsm_disk(mpb
, dl
->index
);
5947 pr_err("BUG mpb setup error\n");
5953 sum
+= __gen_imsm_checksum(mpb
);
5954 mpb
->family_num
= __cpu_to_le32(sum
);
5955 mpb
->orig_family_num
= mpb
->family_num
;
5956 mpb
->creation_time
= __cpu_to_le64((__u64
)time(NULL
));
5958 super
->current_disk
= dl
;
5963 * Function marks disk as spare and restores disk serial
5964 * in case it was previously marked as failed by takeover operation
5966 * -1 : critical error
5967 * 0 : disk is marked as spare but serial is not set
5970 int mark_spare(struct dl
*disk
)
5972 __u8 serial
[MAX_RAID_SERIAL_LEN
];
5979 if (!imsm_read_serial(disk
->fd
, NULL
, serial
, MAX_RAID_SERIAL_LEN
)) {
5980 /* Restore disk serial number, because takeover marks disk
5981 * as failed and adds to serial ':0' before it becomes
5984 serialcpy(disk
->serial
, serial
);
5985 serialcpy(disk
->disk
.serial
, serial
);
5988 disk
->disk
.status
= SPARE_DISK
;
5995 static int write_super_imsm_spare(struct intel_super
*super
, struct dl
*d
);
5997 static int add_to_super_imsm(struct supertype
*st
, mdu_disk_info_t
*dk
,
5998 int fd
, char *devname
,
5999 unsigned long long data_offset
)
6001 struct intel_super
*super
= st
->sb
;
6003 unsigned long long size
;
6004 unsigned int member_sector_size
;
6009 /* If we are on an RAID enabled platform check that the disk is
6010 * attached to the raid controller.
6011 * We do not need to test disks attachment for container based additions,
6012 * they shall be already tested when container was created/assembled.
6014 rv
= find_intel_hba_capability(fd
, super
, devname
);
6015 /* no orom/efi or non-intel hba of the disk */
6017 dprintf("capability: %p fd: %d ret: %d\n",
6018 super
->orom
, fd
, rv
);
6022 if (super
->current_vol
>= 0)
6023 return add_to_super_imsm_volume(st
, dk
, fd
, devname
);
6026 dd
= xcalloc(sizeof(*dd
), 1);
6027 dd
->major
= major(stb
.st_rdev
);
6028 dd
->minor
= minor(stb
.st_rdev
);
6029 dd
->devname
= devname
? xstrdup(devname
) : NULL
;
6032 dd
->action
= DISK_ADD
;
6033 rv
= imsm_read_serial(fd
, devname
, dd
->serial
, MAX_RAID_SERIAL_LEN
);
6035 pr_err("failed to retrieve scsi serial, aborting\n");
6036 __free_imsm_disk(dd
, 0);
6040 if (super
->hba
&& ((super
->hba
->type
== SYS_DEV_NVME
) ||
6041 (super
->hba
->type
== SYS_DEV_VMD
))) {
6043 char cntrl_path
[PATH_MAX
];
6045 char pci_dev_path
[PATH_MAX
];
6047 if (!diskfd_to_devpath(fd
, 2, pci_dev_path
) ||
6048 !diskfd_to_devpath(fd
, 1, cntrl_path
)) {
6049 pr_err("failed to get dev paths, aborting\n");
6050 __free_imsm_disk(dd
, 0);
6054 cntrl_name
= basename(cntrl_path
);
6055 if (is_multipath_nvme(fd
))
6056 pr_err("%s controller supports Multi-Path I/O, Intel (R) VROC does not support multipathing\n",
6059 if (devpath_to_vendor(pci_dev_path
) == 0x8086) {
6061 * If Intel's NVMe drive has serial ended with
6062 * "-A","-B","-1" or "-2" it means that this is "x8"
6063 * device (double drive on single PCIe card).
6064 * User should be warned about potential data loss.
6066 for (i
= MAX_RAID_SERIAL_LEN
-1; i
> 0; i
--) {
6067 /* Skip empty character at the end */
6068 if (dd
->serial
[i
] == 0)
6071 if (((dd
->serial
[i
] == 'A') ||
6072 (dd
->serial
[i
] == 'B') ||
6073 (dd
->serial
[i
] == '1') ||
6074 (dd
->serial
[i
] == '2')) &&
6075 (dd
->serial
[i
-1] == '-'))
6076 pr_err("\tThe action you are about to take may put your data at risk.\n"
6077 "\tPlease note that x8 devices may consist of two separate x4 devices "
6078 "located on a single PCIe port.\n"
6079 "\tRAID 0 is the only supported configuration for this type of x8 device.\n");
6082 } else if (super
->hba
->type
== SYS_DEV_VMD
&& super
->orom
&&
6083 !imsm_orom_has_tpv_support(super
->orom
)) {
6084 pr_err("\tPlatform configuration does not support non-Intel NVMe drives.\n"
6085 "\tPlease refer to Intel(R) RSTe/VROC user guide.\n");
6086 __free_imsm_disk(dd
, 0);
6091 get_dev_size(fd
, NULL
, &size
);
6092 if (!get_dev_sector_size(fd
, NULL
, &member_sector_size
)) {
6093 __free_imsm_disk(dd
, 0);
6097 if (super
->sector_size
== 0) {
6098 /* this a first device, so sector_size is not set yet */
6099 super
->sector_size
= member_sector_size
;
6102 /* clear migr_rec when adding disk to container */
6103 memset(super
->migr_rec_buf
, 0, MIGR_REC_BUF_SECTORS
*MAX_SECTOR_SIZE
);
6104 if (lseek64(fd
, size
- MIGR_REC_SECTOR_POSITION
*member_sector_size
,
6106 if ((unsigned int)write(fd
, super
->migr_rec_buf
,
6107 MIGR_REC_BUF_SECTORS
*member_sector_size
) !=
6108 MIGR_REC_BUF_SECTORS
*member_sector_size
)
6109 perror("Write migr_rec failed");
6113 serialcpy(dd
->disk
.serial
, dd
->serial
);
6114 set_total_blocks(&dd
->disk
, size
);
6115 if (__le32_to_cpu(dd
->disk
.total_blocks_hi
) > 0) {
6116 struct imsm_super
*mpb
= super
->anchor
;
6117 mpb
->attributes
|= MPB_ATTRIB_2TB_DISK
;
6120 if (sysfs_disk_to_scsi_id(fd
, &id
) == 0)
6121 dd
->disk
.scsi_id
= __cpu_to_le32(id
);
6123 dd
->disk
.scsi_id
= __cpu_to_le32(0);
6125 if (st
->update_tail
) {
6126 dd
->next
= super
->disk_mgmt_list
;
6127 super
->disk_mgmt_list
= dd
;
6129 /* this is called outside of mdmon
6130 * write initial spare metadata
6131 * mdmon will overwrite it.
6133 dd
->next
= super
->disks
;
6135 write_super_imsm_spare(super
, dd
);
6141 static int remove_from_super_imsm(struct supertype
*st
, mdu_disk_info_t
*dk
)
6143 struct intel_super
*super
= st
->sb
;
6146 /* remove from super works only in mdmon - for communication
6147 * manager - monitor. Check if communication memory buffer
6150 if (!st
->update_tail
) {
6151 pr_err("shall be used in mdmon context only\n");
6154 dd
= xcalloc(1, sizeof(*dd
));
6155 dd
->major
= dk
->major
;
6156 dd
->minor
= dk
->minor
;
6159 dd
->action
= DISK_REMOVE
;
6161 dd
->next
= super
->disk_mgmt_list
;
6162 super
->disk_mgmt_list
= dd
;
6167 static int store_imsm_mpb(int fd
, struct imsm_super
*mpb
);
6170 char buf
[MAX_SECTOR_SIZE
];
6171 struct imsm_super anchor
;
6172 } spare_record
__attribute__ ((aligned(MAX_SECTOR_SIZE
)));
6175 static int write_super_imsm_spare(struct intel_super
*super
, struct dl
*d
)
6177 struct imsm_super
*mpb
= super
->anchor
;
6178 struct imsm_super
*spare
= &spare_record
.anchor
;
6184 spare
->mpb_size
= __cpu_to_le32(sizeof(struct imsm_super
));
6185 spare
->generation_num
= __cpu_to_le32(1UL);
6186 spare
->attributes
= MPB_ATTRIB_CHECKSUM_VERIFY
;
6187 spare
->num_disks
= 1;
6188 spare
->num_raid_devs
= 0;
6189 spare
->cache_size
= mpb
->cache_size
;
6190 spare
->pwr_cycle_count
= __cpu_to_le32(1);
6192 snprintf((char *) spare
->sig
, MAX_SIGNATURE_LENGTH
,
6193 MPB_SIGNATURE MPB_VERSION_RAID0
);
6195 spare
->disk
[0] = d
->disk
;
6196 if (__le32_to_cpu(d
->disk
.total_blocks_hi
) > 0)
6197 spare
->attributes
|= MPB_ATTRIB_2TB_DISK
;
6199 if (super
->sector_size
== 4096)
6200 convert_to_4k_imsm_disk(&spare
->disk
[0]);
6202 sum
= __gen_imsm_checksum(spare
);
6203 spare
->family_num
= __cpu_to_le32(sum
);
6204 spare
->orig_family_num
= 0;
6205 sum
= __gen_imsm_checksum(spare
);
6206 spare
->check_sum
= __cpu_to_le32(sum
);
6208 if (store_imsm_mpb(d
->fd
, spare
)) {
6209 pr_err("failed for device %d:%d %s\n",
6210 d
->major
, d
->minor
, strerror(errno
));
6216 /* spare records have their own family number and do not have any defined raid
6219 static int write_super_imsm_spares(struct intel_super
*super
, int doclose
)
6223 for (d
= super
->disks
; d
; d
= d
->next
) {
6227 if (write_super_imsm_spare(super
, d
))
6237 static int write_super_imsm(struct supertype
*st
, int doclose
)
6239 struct intel_super
*super
= st
->sb
;
6240 unsigned int sector_size
= super
->sector_size
;
6241 struct imsm_super
*mpb
= super
->anchor
;
6247 __u32 mpb_size
= sizeof(struct imsm_super
) - sizeof(struct imsm_disk
);
6249 int clear_migration_record
= 1;
6252 /* 'generation' is incremented everytime the metadata is written */
6253 generation
= __le32_to_cpu(mpb
->generation_num
);
6255 mpb
->generation_num
= __cpu_to_le32(generation
);
6257 /* fix up cases where previous mdadm releases failed to set
6260 if (mpb
->orig_family_num
== 0)
6261 mpb
->orig_family_num
= mpb
->family_num
;
6263 for (d
= super
->disks
; d
; d
= d
->next
) {
6267 mpb
->disk
[d
->index
] = d
->disk
;
6271 for (d
= super
->missing
; d
; d
= d
->next
) {
6272 mpb
->disk
[d
->index
] = d
->disk
;
6275 mpb
->num_disks
= num_disks
;
6276 mpb_size
+= sizeof(struct imsm_disk
) * mpb
->num_disks
;
6278 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
6279 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
6280 struct imsm_dev
*dev2
= get_imsm_dev(super
, i
);
6282 imsm_copy_dev(dev
, dev2
);
6283 mpb_size
+= sizeof_imsm_dev(dev
, 0);
6285 if (is_gen_migration(dev2
))
6286 clear_migration_record
= 0;
6289 bbm_log_size
= get_imsm_bbm_log_size(super
->bbm_log
);
6292 memcpy((void *)mpb
+ mpb_size
, super
->bbm_log
, bbm_log_size
);
6293 mpb
->attributes
|= MPB_ATTRIB_BBM
;
6295 mpb
->attributes
&= ~MPB_ATTRIB_BBM
;
6297 super
->anchor
->bbm_log_size
= __cpu_to_le32(bbm_log_size
);
6298 mpb_size
+= bbm_log_size
;
6299 mpb
->mpb_size
= __cpu_to_le32(mpb_size
);
6302 assert(super
->len
== 0 || mpb_size
<= super
->len
);
6305 /* recalculate checksum */
6306 sum
= __gen_imsm_checksum(mpb
);
6307 mpb
->check_sum
= __cpu_to_le32(sum
);
6309 if (super
->clean_migration_record_by_mdmon
) {
6310 clear_migration_record
= 1;
6311 super
->clean_migration_record_by_mdmon
= 0;
6313 if (clear_migration_record
)
6314 memset(super
->migr_rec_buf
, 0,
6315 MIGR_REC_BUF_SECTORS
*MAX_SECTOR_SIZE
);
6317 if (sector_size
== 4096)
6318 convert_to_4k(super
);
6320 /* write the mpb for disks that compose raid devices */
6321 for (d
= super
->disks
; d
; d
= d
->next
) {
6322 if (d
->index
< 0 || is_failed(&d
->disk
))
6325 if (clear_migration_record
) {
6326 unsigned long long dsize
;
6328 get_dev_size(d
->fd
, NULL
, &dsize
);
6329 if (lseek64(d
->fd
, dsize
- sector_size
,
6331 if ((unsigned int)write(d
->fd
,
6332 super
->migr_rec_buf
,
6333 MIGR_REC_BUF_SECTORS
*sector_size
) !=
6334 MIGR_REC_BUF_SECTORS
*sector_size
)
6335 perror("Write migr_rec failed");
6339 if (store_imsm_mpb(d
->fd
, mpb
))
6341 "failed for device %d:%d (fd: %d)%s\n",
6343 d
->fd
, strerror(errno
));
6350 return write_super_imsm_spares(super
, doclose
);
6355 static int create_array(struct supertype
*st
, int dev_idx
)
6358 struct imsm_update_create_array
*u
;
6359 struct intel_super
*super
= st
->sb
;
6360 struct imsm_dev
*dev
= get_imsm_dev(super
, dev_idx
);
6361 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
6362 struct disk_info
*inf
;
6363 struct imsm_disk
*disk
;
6366 len
= sizeof(*u
) - sizeof(*dev
) + sizeof_imsm_dev(dev
, 0) +
6367 sizeof(*inf
) * map
->num_members
;
6369 u
->type
= update_create_array
;
6370 u
->dev_idx
= dev_idx
;
6371 imsm_copy_dev(&u
->dev
, dev
);
6372 inf
= get_disk_info(u
);
6373 for (i
= 0; i
< map
->num_members
; i
++) {
6374 int idx
= get_imsm_disk_idx(dev
, i
, MAP_X
);
6376 disk
= get_imsm_disk(super
, idx
);
6378 disk
= get_imsm_missing(super
, idx
);
6379 serialcpy(inf
[i
].serial
, disk
->serial
);
6381 append_metadata_update(st
, u
, len
);
6386 static int mgmt_disk(struct supertype
*st
)
6388 struct intel_super
*super
= st
->sb
;
6390 struct imsm_update_add_remove_disk
*u
;
6392 if (!super
->disk_mgmt_list
)
6397 u
->type
= update_add_remove_disk
;
6398 append_metadata_update(st
, u
, len
);
6403 __u32
crc32c_le(__u32 crc
, unsigned char const *p
, size_t len
);
6405 static int write_ppl_header(unsigned long long ppl_sector
, int fd
, void *buf
)
6407 struct ppl_header
*ppl_hdr
= buf
;
6410 ppl_hdr
->checksum
= __cpu_to_le32(~crc32c_le(~0, buf
, PPL_HEADER_SIZE
));
6412 if (lseek64(fd
, ppl_sector
* 512, SEEK_SET
) < 0) {
6414 perror("Failed to seek to PPL header location");
6418 if (write(fd
, buf
, PPL_HEADER_SIZE
) != PPL_HEADER_SIZE
) {
6420 perror("Write PPL header failed");
6429 static int write_init_ppl_imsm(struct supertype
*st
, struct mdinfo
*info
, int fd
)
6431 struct intel_super
*super
= st
->sb
;
6433 struct ppl_header
*ppl_hdr
;
6436 /* first clear entire ppl space */
6437 ret
= zero_disk_range(fd
, info
->ppl_sector
, info
->ppl_size
);
6441 ret
= posix_memalign(&buf
, MAX_SECTOR_SIZE
, PPL_HEADER_SIZE
);
6443 pr_err("Failed to allocate PPL header buffer\n");
6447 memset(buf
, 0, PPL_HEADER_SIZE
);
6449 memset(ppl_hdr
->reserved
, 0xff, PPL_HDR_RESERVED
);
6450 ppl_hdr
->signature
= __cpu_to_le32(super
->anchor
->orig_family_num
);
6452 if (info
->mismatch_cnt
) {
6454 * We are overwriting an invalid ppl. Make one entry with wrong
6455 * checksum to prevent the kernel from skipping resync.
6457 ppl_hdr
->entries_count
= __cpu_to_le32(1);
6458 ppl_hdr
->entries
[0].checksum
= ~0;
6461 ret
= write_ppl_header(info
->ppl_sector
, fd
, buf
);
6467 static int is_rebuilding(struct imsm_dev
*dev
);
6469 static int validate_ppl_imsm(struct supertype
*st
, struct mdinfo
*info
,
6470 struct mdinfo
*disk
)
6472 struct intel_super
*super
= st
->sb
;
6474 void *buf_orig
, *buf
, *buf_prev
= NULL
;
6476 struct ppl_header
*ppl_hdr
= NULL
;
6478 struct imsm_dev
*dev
;
6481 unsigned long long ppl_offset
= 0;
6482 unsigned long long prev_gen_num
= 0;
6484 if (disk
->disk
.raid_disk
< 0)
6487 dev
= get_imsm_dev(super
, info
->container_member
);
6488 idx
= get_imsm_disk_idx(dev
, disk
->disk
.raid_disk
, MAP_0
);
6489 d
= get_imsm_dl_disk(super
, idx
);
6491 if (!d
|| d
->index
< 0 || is_failed(&d
->disk
))
6494 if (posix_memalign(&buf_orig
, MAX_SECTOR_SIZE
, PPL_HEADER_SIZE
* 2)) {
6495 pr_err("Failed to allocate PPL header buffer\n");
6501 while (ppl_offset
< MULTIPLE_PPL_AREA_SIZE_IMSM
) {
6504 dprintf("Checking potential PPL at offset: %llu\n", ppl_offset
);
6506 if (lseek64(d
->fd
, info
->ppl_sector
* 512 + ppl_offset
,
6508 perror("Failed to seek to PPL header location");
6513 if (read(d
->fd
, buf
, PPL_HEADER_SIZE
) != PPL_HEADER_SIZE
) {
6514 perror("Read PPL header failed");
6521 crc
= __le32_to_cpu(ppl_hdr
->checksum
);
6522 ppl_hdr
->checksum
= 0;
6524 if (crc
!= ~crc32c_le(~0, buf
, PPL_HEADER_SIZE
)) {
6525 dprintf("Wrong PPL header checksum on %s\n",
6530 if (prev_gen_num
> __le64_to_cpu(ppl_hdr
->generation
)) {
6531 /* previous was newest, it was already checked */
6535 if ((__le32_to_cpu(ppl_hdr
->signature
) !=
6536 super
->anchor
->orig_family_num
)) {
6537 dprintf("Wrong PPL header signature on %s\n",
6544 prev_gen_num
= __le64_to_cpu(ppl_hdr
->generation
);
6546 ppl_offset
+= PPL_HEADER_SIZE
;
6547 for (i
= 0; i
< __le32_to_cpu(ppl_hdr
->entries_count
); i
++)
6549 __le32_to_cpu(ppl_hdr
->entries
[i
].pp_size
);
6552 buf_prev
= buf
+ PPL_HEADER_SIZE
;
6564 * Update metadata to use mutliple PPLs area (1MB).
6565 * This is done once for all RAID members
6567 if (info
->consistency_policy
== CONSISTENCY_POLICY_PPL
&&
6568 info
->ppl_size
!= (MULTIPLE_PPL_AREA_SIZE_IMSM
>> 9)) {
6570 struct mdinfo
*member_dev
;
6572 sprintf(subarray
, "%d", info
->container_member
);
6574 if (mdmon_running(st
->container_devnm
))
6575 st
->update_tail
= &st
->updates
;
6577 if (st
->ss
->update_subarray(st
, subarray
, UOPT_PPL
, NULL
)) {
6578 pr_err("Failed to update subarray %s\n",
6581 if (st
->update_tail
)
6582 flush_metadata_updates(st
);
6584 st
->ss
->sync_metadata(st
);
6585 info
->ppl_size
= (MULTIPLE_PPL_AREA_SIZE_IMSM
>> 9);
6586 for (member_dev
= info
->devs
; member_dev
;
6587 member_dev
= member_dev
->next
)
6588 member_dev
->ppl_size
=
6589 (MULTIPLE_PPL_AREA_SIZE_IMSM
>> 9);
6594 struct imsm_map
*map
= get_imsm_map(dev
, MAP_X
);
6596 if (map
->map_state
== IMSM_T_STATE_UNINITIALIZED
||
6597 (map
->map_state
== IMSM_T_STATE_NORMAL
&&
6598 !(dev
->vol
.dirty
& RAIDVOL_DIRTY
)) ||
6599 (is_rebuilding(dev
) &&
6600 vol_curr_migr_unit(dev
) == 0 &&
6601 get_imsm_disk_idx(dev
, disk
->disk
.raid_disk
, MAP_1
) != idx
))
6602 ret
= st
->ss
->write_init_ppl(st
, info
, d
->fd
);
6604 info
->mismatch_cnt
++;
6605 } else if (ret
== 0 &&
6606 ppl_hdr
->entries_count
== 0 &&
6607 is_rebuilding(dev
) &&
6608 info
->resync_start
== 0) {
6610 * The header has no entries - add a single empty entry and
6611 * rewrite the header to prevent the kernel from going into
6612 * resync after an interrupted rebuild.
6614 ppl_hdr
->entries_count
= __cpu_to_le32(1);
6615 ret
= write_ppl_header(info
->ppl_sector
, d
->fd
, buf
);
6623 static int write_init_ppl_imsm_all(struct supertype
*st
, struct mdinfo
*info
)
6625 struct intel_super
*super
= st
->sb
;
6629 if (info
->consistency_policy
!= CONSISTENCY_POLICY_PPL
||
6630 info
->array
.level
!= 5)
6633 for (d
= super
->disks
; d
; d
= d
->next
) {
6634 if (d
->index
< 0 || is_failed(&d
->disk
))
6637 ret
= st
->ss
->write_init_ppl(st
, info
, d
->fd
);
6645 /*******************************************************************************
6646 * Function: write_init_bitmap_imsm_vol
6647 * Description: Write a bitmap header and prepares the area for the bitmap.
6649 * st : supertype information
6650 * vol_idx : the volume index to use
6655 ******************************************************************************/
6656 static int write_init_bitmap_imsm_vol(struct supertype
*st
, int vol_idx
)
6658 struct intel_super
*super
= st
->sb
;
6659 int prev_current_vol
= super
->current_vol
;
6663 super
->current_vol
= vol_idx
;
6664 for (d
= super
->disks
; d
; d
= d
->next
) {
6665 if (d
->index
< 0 || is_failed(&d
->disk
))
6667 ret
= st
->ss
->write_bitmap(st
, d
->fd
, NoUpdate
);
6671 super
->current_vol
= prev_current_vol
;
6675 /*******************************************************************************
6676 * Function: write_init_bitmap_imsm_all
6677 * Description: Write a bitmap header and prepares the area for the bitmap.
6678 * Operation is executed for volumes with CONSISTENCY_POLICY_BITMAP.
6680 * st : supertype information
6681 * info : info about the volume where the bitmap should be written
6682 * vol_idx : the volume index to use
6687 ******************************************************************************/
6688 static int write_init_bitmap_imsm_all(struct supertype
*st
, struct mdinfo
*info
,
6693 if (info
&& (info
->consistency_policy
== CONSISTENCY_POLICY_BITMAP
))
6694 ret
= write_init_bitmap_imsm_vol(st
, vol_idx
);
6699 static int write_init_super_imsm(struct supertype
*st
)
6701 struct intel_super
*super
= st
->sb
;
6702 int current_vol
= super
->current_vol
;
6706 getinfo_super_imsm(st
, &info
, NULL
);
6708 /* we are done with current_vol reset it to point st at the container */
6709 super
->current_vol
= -1;
6711 if (st
->update_tail
) {
6712 /* queue the recently created array / added disk
6713 * as a metadata update */
6715 /* determine if we are creating a volume or adding a disk */
6716 if (current_vol
< 0) {
6717 /* in the mgmt (add/remove) disk case we are running
6718 * in mdmon context, so don't close fd's
6722 /* adding the second volume to the array */
6723 rv
= write_init_ppl_imsm_all(st
, &info
);
6725 rv
= write_init_bitmap_imsm_all(st
, &info
, current_vol
);
6727 rv
= create_array(st
, current_vol
);
6731 for (d
= super
->disks
; d
; d
= d
->next
)
6732 Kill(d
->devname
, NULL
, 0, -1, 1);
6733 if (current_vol
>= 0) {
6734 rv
= write_init_ppl_imsm_all(st
, &info
);
6736 rv
= write_init_bitmap_imsm_all(st
, &info
, current_vol
);
6740 rv
= write_super_imsm(st
, 1);
6746 static int store_super_imsm(struct supertype
*st
, int fd
)
6748 struct intel_super
*super
= st
->sb
;
6749 struct imsm_super
*mpb
= super
? super
->anchor
: NULL
;
6754 if (super
->sector_size
== 4096)
6755 convert_to_4k(super
);
6756 return store_imsm_mpb(fd
, mpb
);
6759 static int validate_geometry_imsm_container(struct supertype
*st
, int level
,
6761 unsigned long long data_offset
,
6763 unsigned long long *freesize
,
6767 unsigned long long ldsize
;
6768 struct intel_super
*super
= NULL
;
6771 if (!is_container(level
))
6776 fd
= dev_open(dev
, O_RDONLY
|O_EXCL
);
6777 if (!is_fd_valid(fd
)) {
6778 pr_vrb("imsm: Cannot open %s: %s\n", dev
, strerror(errno
));
6781 if (!get_dev_size(fd
, dev
, &ldsize
))
6784 /* capabilities retrieve could be possible
6785 * note that there is no fd for the disks in array.
6787 super
= alloc_super();
6791 if (!get_dev_sector_size(fd
, NULL
, &super
->sector_size
))
6794 rv
= find_intel_hba_capability(fd
, super
, verbose
> 0 ? dev
: NULL
);
6798 fd2devname(fd
, str
);
6799 dprintf("fd: %d %s orom: %p rv: %d raiddisk: %d\n",
6800 fd
, str
, super
->orom
, rv
, raiddisks
);
6802 /* no orom/efi or non-intel hba of the disk */
6807 if (raiddisks
> super
->orom
->tds
) {
6809 pr_err("%d exceeds maximum number of platform supported disks: %d\n",
6810 raiddisks
, super
->orom
->tds
);
6813 if ((super
->orom
->attr
& IMSM_OROM_ATTR_2TB_DISK
) == 0 &&
6814 (ldsize
>> 9) >> 32 > 0) {
6816 pr_err("%s exceeds maximum platform supported size\n", dev
);
6820 if (super
->hba
->type
== SYS_DEV_VMD
||
6821 super
->hba
->type
== SYS_DEV_NVME
) {
6822 if (!imsm_is_nvme_namespace_supported(fd
, 1)) {
6824 pr_err("NVMe namespace %s is not supported by IMSM\n",
6831 *freesize
= avail_size_imsm(st
, ldsize
>> 9, data_offset
);
6841 static unsigned long long find_size(struct extent
*e
, int *idx
, int num_extents
)
6843 const unsigned long long base_start
= e
[*idx
].start
;
6844 unsigned long long end
= base_start
+ e
[*idx
].size
;
6847 if (base_start
== end
)
6851 for (i
= *idx
; i
< num_extents
; i
++) {
6852 /* extend overlapping extents */
6853 if (e
[i
].start
>= base_start
&&
6854 e
[i
].start
<= end
) {
6857 if (e
[i
].start
+ e
[i
].size
> end
)
6858 end
= e
[i
].start
+ e
[i
].size
;
6859 } else if (e
[i
].start
> end
) {
6865 return end
- base_start
;
6868 /** merge_extents() - analyze extents and get free size.
6869 * @super: Intel metadata, not NULL.
6870 * @expanding: if set, we are expanding &super->current_vol.
6872 * Build a composite disk with all known extents and generate a size given the
6873 * "all disks in an array must share a common start offset" constraint.
6874 * If a volume is expanded, then return free space after the volume.
6876 * Return: Free space or 0 on failure.
6878 static unsigned long long merge_extents(struct intel_super
*super
, const bool expanding
)
6882 int i
, j
, pos_vol_idx
= -1;
6884 int sum_extents
= 0;
6885 unsigned long long pos
= 0;
6886 unsigned long long start
= 0;
6887 unsigned long long free_size
= 0;
6889 unsigned long pre_reservation
= 0;
6890 unsigned long post_reservation
= IMSM_RESERVED_SECTORS
;
6891 unsigned long reservation_size
;
6893 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
6895 sum_extents
+= dl
->extent_cnt
;
6896 e
= xcalloc(sum_extents
, sizeof(struct extent
));
6898 /* coalesce and sort all extents. also, check to see if we need to
6899 * reserve space between member arrays
6902 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
6905 for (i
= 0; i
< dl
->extent_cnt
; i
++)
6908 qsort(e
, sum_extents
, sizeof(*e
), cmp_extent
);
6913 while (i
< sum_extents
) {
6914 e
[j
].start
= e
[i
].start
;
6915 e
[j
].vol
= e
[i
].vol
;
6916 e
[j
].size
= find_size(e
, &i
, sum_extents
);
6918 if (e
[j
-1].size
== 0)
6924 unsigned long long esize
= e
[i
].start
- pos
;
6926 if (expanding
? pos_vol_idx
== super
->current_vol
: esize
>= free_size
) {
6932 pos
= e
[i
].start
+ e
[i
].size
;
6933 pos_vol_idx
= e
[i
].vol
;
6936 } while (e
[i
-1].size
);
6938 if (free_size
== 0) {
6939 dprintf("imsm: Cannot find free size.\n");
6944 if (!expanding
&& extent_idx
!= 0)
6946 * Not a real first volume in a container is created, pre_reservation is needed.
6948 pre_reservation
= IMSM_RESERVED_SECTORS
;
6950 if (e
[extent_idx
].size
== 0)
6952 * extent_idx points to the metadata, post_reservation is allready done.
6954 post_reservation
= 0;
6957 reservation_size
= pre_reservation
+ post_reservation
;
6959 if (free_size
< reservation_size
) {
6960 dprintf("imsm: Reservation size is greater than free space.\n");
6964 super
->create_offset
= start
+ pre_reservation
;
6965 return free_size
- reservation_size
;
6968 static int is_raid_level_supported(const struct imsm_orom
*orom
, int level
, int raiddisks
)
6970 if (level
< 0 || level
== 6 || level
== 4)
6973 /* if we have an orom prevent invalid raid levels */
6976 case 0: return imsm_orom_has_raid0(orom
);
6979 return imsm_orom_has_raid1e(orom
);
6980 return imsm_orom_has_raid1(orom
) && raiddisks
== 2;
6981 case 10: return imsm_orom_has_raid10(orom
) && raiddisks
== 4;
6982 case 5: return imsm_orom_has_raid5(orom
) && raiddisks
> 2;
6985 return 1; /* not on an Intel RAID platform so anything goes */
6991 active_arrays_by_format(char *name
, char* hba
, struct md_list
**devlist
,
6992 int dpa
, int verbose
)
6994 struct mdstat_ent
*mdstat
= mdstat_read(0, 0);
6995 struct mdstat_ent
*memb
;
7001 for (memb
= mdstat
; memb
; memb
= memb
->next
) {
7002 if (memb
->metadata_version
&&
7003 (strncmp(memb
->metadata_version
, "external:", 9) == 0) &&
7004 (strcmp(&memb
->metadata_version
[9], name
) == 0) &&
7005 !is_subarray(memb
->metadata_version
+9) &&
7007 struct dev_member
*dev
= memb
->members
;
7009 while (dev
&& !is_fd_valid(fd
)) {
7010 char *path
= xmalloc(strlen(dev
->name
) + strlen("/dev/") + 1);
7011 num
= snprintf(path
, PATH_MAX
, "%s%s", "/dev/", dev
->name
);
7013 fd
= open(path
, O_RDONLY
, 0);
7014 if (num
<= 0 || !is_fd_valid(fd
)) {
7015 pr_vrb("Cannot open %s: %s\n",
7016 dev
->name
, strerror(errno
));
7022 if (is_fd_valid(fd
) && disk_attached_to_hba(fd
, hba
)) {
7023 struct mdstat_ent
*vol
;
7024 for (vol
= mdstat
; vol
; vol
= vol
->next
) {
7025 if (vol
->active
> 0 &&
7026 vol
->metadata_version
&&
7027 is_container_member(vol
, memb
->devnm
)) {
7032 if (*devlist
&& (found
< dpa
)) {
7033 dv
= xcalloc(1, sizeof(*dv
));
7034 dv
->devname
= xmalloc(strlen(memb
->devnm
) + strlen("/dev/") + 1);
7035 sprintf(dv
->devname
, "%s%s", "/dev/", memb
->devnm
);
7038 dv
->next
= *devlist
;
7045 free_mdstat(mdstat
);
7050 static struct md_list
*
7051 get_loop_devices(void)
7054 struct md_list
*devlist
= NULL
;
7057 for(i
= 0; i
< 12; i
++) {
7058 dv
= xcalloc(1, sizeof(*dv
));
7059 dv
->devname
= xmalloc(40);
7060 sprintf(dv
->devname
, "/dev/loop%d", i
);
7068 static struct md_list
*
7069 get_devices(const char *hba_path
)
7071 struct md_list
*devlist
= NULL
;
7078 devlist
= get_loop_devices();
7081 /* scroll through /sys/dev/block looking for devices attached to
7084 dir
= opendir("/sys/dev/block");
7085 for (ent
= dir
? readdir(dir
) : NULL
; ent
; ent
= readdir(dir
)) {
7090 if (sscanf(ent
->d_name
, "%d:%d", &major
, &minor
) != 2)
7092 path
= devt_to_devpath(makedev(major
, minor
), 1, NULL
);
7095 if (!path_attached_to_hba(path
, hba_path
)) {
7102 fd
= dev_open(ent
->d_name
, O_RDONLY
);
7103 if (is_fd_valid(fd
)) {
7104 fd2devname(fd
, buf
);
7107 pr_err("cannot open device: %s\n",
7112 dv
= xcalloc(1, sizeof(*dv
));
7113 dv
->devname
= xstrdup(buf
);
7120 devlist
= devlist
->next
;
7130 count_volumes_list(struct md_list
*devlist
, char *homehost
,
7131 int verbose
, int *found
)
7133 struct md_list
*tmpdev
;
7135 struct supertype
*st
;
7137 /* first walk the list of devices to find a consistent set
7138 * that match the criterea, if that is possible.
7139 * We flag the ones we like with 'used'.
7142 st
= match_metadata_desc_imsm("imsm");
7144 pr_vrb("cannot allocate memory for imsm supertype\n");
7148 for (tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
7149 char *devname
= tmpdev
->devname
;
7151 struct supertype
*tst
;
7153 if (tmpdev
->used
> 1)
7155 tst
= dup_super(st
);
7157 pr_vrb("cannot allocate memory for imsm supertype\n");
7160 tmpdev
->container
= 0;
7161 dfd
= dev_open(devname
, O_RDONLY
|O_EXCL
);
7162 if (!is_fd_valid(dfd
)) {
7163 dprintf("cannot open device %s: %s\n",
7164 devname
, strerror(errno
));
7166 } else if (!fstat_is_blkdev(dfd
, devname
, &rdev
)) {
7168 } else if (must_be_container(dfd
)) {
7169 struct supertype
*cst
;
7170 cst
= super_by_fd(dfd
, NULL
);
7172 dprintf("cannot recognize container type %s\n",
7175 } else if (tst
->ss
!= st
->ss
) {
7176 dprintf("non-imsm container - ignore it: %s\n",
7179 } else if (!tst
->ss
->load_container
||
7180 tst
->ss
->load_container(tst
, dfd
, NULL
))
7183 tmpdev
->container
= 1;
7186 cst
->ss
->free_super(cst
);
7188 tmpdev
->st_rdev
= rdev
;
7189 if (tst
->ss
->load_super(tst
,dfd
, NULL
)) {
7190 dprintf("no RAID superblock on %s\n",
7193 } else if (tst
->ss
->compare_super
== NULL
) {
7194 dprintf("Cannot assemble %s metadata on %s\n",
7195 tst
->ss
->name
, devname
);
7201 if (tmpdev
->used
== 2 || tmpdev
->used
== 4) {
7202 /* Ignore unrecognised devices during auto-assembly */
7207 tst
->ss
->getinfo_super(tst
, &info
, NULL
);
7209 if (st
->minor_version
== -1)
7210 st
->minor_version
= tst
->minor_version
;
7212 if (memcmp(info
.uuid
, uuid_zero
,
7213 sizeof(int[4])) == 0) {
7214 /* this is a floating spare. It cannot define
7215 * an array unless there are no more arrays of
7216 * this type to be found. It can be included
7217 * in an array of this type though.
7223 if (st
->ss
!= tst
->ss
||
7224 st
->minor_version
!= tst
->minor_version
||
7225 st
->ss
->compare_super(st
, tst
, 1) != 0) {
7226 /* Some mismatch. If exactly one array matches this host,
7227 * we can resolve on that one.
7228 * Or, if we are auto assembling, we just ignore the second
7231 dprintf("superblock on %s doesn't match others - assembly aborted\n",
7237 dprintf("found: devname: %s\n", devname
);
7241 tst
->ss
->free_super(tst
);
7245 if ((err
= load_super_imsm_all(st
, -1, &st
->sb
, NULL
, devlist
, 0)) == 0) {
7246 struct mdinfo
*iter
, *head
= st
->ss
->container_content(st
, NULL
);
7247 for (iter
= head
; iter
; iter
= iter
->next
) {
7248 dprintf("content->text_version: %s vol\n",
7249 iter
->text_version
);
7250 if (iter
->array
.state
& (1<<MD_SB_BLOCK_VOLUME
)) {
7251 /* do not assemble arrays with unsupported
7253 dprintf("Cannot activate member %s.\n",
7254 iter
->text_version
);
7261 dprintf("No valid super block on device list: err: %d %p\n",
7265 dprintf("no more devices to examine\n");
7268 for (tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
7269 if (tmpdev
->used
== 1 && tmpdev
->found
) {
7271 if (count
< tmpdev
->found
)
7274 count
-= tmpdev
->found
;
7277 if (tmpdev
->used
== 1)
7282 st
->ss
->free_super(st
);
7286 static int __count_volumes(char *hba_path
, int dpa
, int verbose
,
7289 struct sys_dev
*idev
, *intel_devices
= find_intel_devices();
7291 const struct orom_entry
*entry
;
7292 struct devid_list
*dv
, *devid_list
;
7297 for (idev
= intel_devices
; idev
; idev
= idev
->next
) {
7298 if (strstr(idev
->path
, hba_path
))
7302 if (!idev
|| !idev
->dev_id
)
7305 entry
= get_orom_entry_by_device_id(idev
->dev_id
);
7307 if (!entry
|| !entry
->devid_list
)
7310 devid_list
= entry
->devid_list
;
7311 for (dv
= devid_list
; dv
; dv
= dv
->next
) {
7312 struct md_list
*devlist
;
7313 struct sys_dev
*device
= NULL
;
7318 device
= device_by_id_and_path(dv
->devid
, hba_path
);
7320 device
= device_by_id(dv
->devid
);
7323 hpath
= device
->path
;
7327 devlist
= get_devices(hpath
);
7328 /* if no intel devices return zero volumes */
7329 if (devlist
== NULL
)
7332 count
+= active_arrays_by_format("imsm", hpath
, &devlist
, dpa
,
7334 dprintf("path: %s active arrays: %d\n", hpath
, count
);
7335 if (devlist
== NULL
)
7339 count
+= count_volumes_list(devlist
,
7343 dprintf("found %d count: %d\n", found
, count
);
7346 dprintf("path: %s total number of volumes: %d\n", hpath
, count
);
7349 struct md_list
*dv
= devlist
;
7350 devlist
= devlist
->next
;
7358 static int count_volumes(struct intel_hba
*hba
, int dpa
, int verbose
)
7362 if (hba
->type
== SYS_DEV_VMD
) {
7363 struct sys_dev
*dev
;
7366 for (dev
= find_intel_devices(); dev
; dev
= dev
->next
) {
7367 if (dev
->type
== SYS_DEV_VMD
)
7368 count
+= __count_volumes(dev
->path
, dpa
,
7373 return __count_volumes(hba
->path
, dpa
, verbose
, 0);
7376 static int imsm_default_chunk(const struct imsm_orom
*orom
)
7378 /* up to 512 if the plaform supports it, otherwise the platform max.
7379 * 128 if no platform detected
7381 int fs
= max(7, orom
? fls(orom
->sss
) : 0);
7383 return min(512, (1 << fs
));
7387 validate_geometry_imsm_orom(struct intel_super
*super
, int level
, int layout
,
7388 int raiddisks
, int *chunk
, unsigned long long size
, int verbose
)
7390 /* check/set platform and metadata limits/defaults */
7391 if (super
->orom
&& raiddisks
> super
->orom
->dpa
) {
7392 pr_vrb("platform supports a maximum of %d disks per array\n",
7397 /* capabilities of OROM tested - copied from validate_geometry_imsm_volume */
7398 if (!is_raid_level_supported(super
->orom
, level
, raiddisks
)) {
7399 pr_vrb("platform does not support raid%d with %d disk%s\n",
7400 level
, raiddisks
, raiddisks
> 1 ? "s" : "");
7404 if (*chunk
== 0 || *chunk
== UnSet
)
7405 *chunk
= imsm_default_chunk(super
->orom
);
7407 if (super
->orom
&& !imsm_orom_has_chunk(super
->orom
, *chunk
)) {
7408 pr_vrb("platform does not support a chunk size of: %d\n", *chunk
);
7412 if (layout
!= imsm_level_to_layout(level
)) {
7414 pr_vrb("imsm raid 5 only supports the left-asymmetric layout\n");
7415 else if (level
== 10)
7416 pr_vrb("imsm raid 10 only supports the n2 layout\n");
7418 pr_vrb("imsm unknown layout %#x for this raid level %d\n",
7423 if (super
->orom
&& (super
->orom
->attr
& IMSM_OROM_ATTR_2TB
) == 0 &&
7424 (calc_array_size(level
, raiddisks
, layout
, *chunk
, size
) >> 32) > 0) {
7425 pr_vrb("platform does not support a volume size over 2TB\n");
7432 /* validate_geometry_imsm_volume - lifted from validate_geometry_ddf_bvd
7433 * FIX ME add ahci details
7435 static int validate_geometry_imsm_volume(struct supertype
*st
, int level
,
7436 int layout
, int raiddisks
, int *chunk
,
7437 unsigned long long size
,
7438 unsigned long long data_offset
,
7440 unsigned long long *freesize
,
7444 struct intel_super
*super
= st
->sb
;
7445 struct imsm_super
*mpb
;
7447 unsigned long long pos
= 0;
7448 unsigned long long maxsize
;
7452 /* We must have the container info already read in. */
7456 mpb
= super
->anchor
;
7458 if (!validate_geometry_imsm_orom(super
, level
, layout
, raiddisks
, chunk
, size
, verbose
)) {
7459 pr_err("RAID geometry validation failed. Cannot proceed with the action(s).\n");
7463 /* General test: make sure there is space for
7464 * 'raiddisks' device extents of size 'size' at a given
7467 unsigned long long minsize
= size
;
7468 unsigned long long start_offset
= MaxSector
;
7471 minsize
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
7472 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
7477 e
= get_extents(super
, dl
, 0);
7480 unsigned long long esize
;
7481 esize
= e
[i
].start
- pos
;
7482 if (esize
>= minsize
)
7484 if (found
&& start_offset
== MaxSector
) {
7487 } else if (found
&& pos
!= start_offset
) {
7491 pos
= e
[i
].start
+ e
[i
].size
;
7493 } while (e
[i
-1].size
);
7498 if (dcnt
< raiddisks
) {
7500 pr_err("imsm: Not enough devices with space for this array (%d < %d)\n",
7507 /* This device must be a member of the set */
7508 if (!stat_is_blkdev(dev
, &rdev
))
7510 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
7511 if (dl
->major
== (int)major(rdev
) &&
7512 dl
->minor
== (int)minor(rdev
))
7517 pr_err("%s is not in the same imsm set\n", dev
);
7519 } else if (super
->orom
&& dl
->index
< 0 && mpb
->num_raid_devs
) {
7520 /* If a volume is present then the current creation attempt
7521 * cannot incorporate new spares because the orom may not
7522 * understand this configuration (all member disks must be
7523 * members of each array in the container).
7525 pr_err("%s is a spare and a volume is already defined for this container\n", dev
);
7526 pr_err("The option-rom requires all member disks to be a member of all volumes\n");
7528 } else if (super
->orom
&& mpb
->num_raid_devs
> 0 &&
7529 mpb
->num_disks
!= raiddisks
) {
7530 pr_err("The option-rom requires all member disks to be a member of all volumes\n");
7534 /* retrieve the largest free space block */
7535 e
= get_extents(super
, dl
, 0);
7540 unsigned long long esize
;
7542 esize
= e
[i
].start
- pos
;
7543 if (esize
>= maxsize
)
7545 pos
= e
[i
].start
+ e
[i
].size
;
7547 } while (e
[i
-1].size
);
7552 pr_err("unable to determine free space for: %s\n",
7556 if (maxsize
< size
) {
7558 pr_err("%s not enough space (%llu < %llu)\n",
7559 dev
, maxsize
, size
);
7563 maxsize
= merge_extents(super
, false);
7565 if (mpb
->num_raid_devs
> 0 && size
&& size
!= maxsize
)
7566 pr_err("attempting to create a second volume with size less then remaining space.\n");
7568 if (maxsize
< size
|| maxsize
== 0) {
7571 pr_err("no free space left on device. Aborting...\n");
7573 pr_err("not enough space to create volume of given size (%llu < %llu). Aborting...\n",
7579 *freesize
= maxsize
;
7582 int count
= count_volumes(super
->hba
,
7583 super
->orom
->dpa
, verbose
);
7584 if (super
->orom
->vphba
<= count
) {
7585 pr_vrb("platform does not support more than %d raid volumes.\n",
7586 super
->orom
->vphba
);
7594 * imsm_get_free_size() - get the biggest, common free space from members.
7595 * @super: &intel_super pointer, not NULL.
7596 * @raiddisks: number of raid disks.
7597 * @size: requested size, could be 0 (means max size).
7598 * @chunk: requested chunk size in KiB.
7599 * @freesize: pointer for returned size value.
7601 * Return: &IMSM_STATUS_OK or &IMSM_STATUS_ERROR.
7603 * @freesize is set to meaningful value, this can be @size, or calculated
7605 * super->create_offset value is modified and set appropriately in
7606 * merge_extends() for further creation.
7608 static imsm_status_t
imsm_get_free_size(struct intel_super
*super
,
7609 const int raiddisks
,
7610 unsigned long long size
,
7612 unsigned long long *freesize
,
7615 struct imsm_super
*mpb
= super
->anchor
;
7621 unsigned long long maxsize
;
7622 unsigned long long minsize
= size
;
7625 minsize
= chunk
* 2;
7627 /* find the largest common start free region of the possible disks */
7628 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
7634 /* don't activate new spares if we are orom constrained
7635 * and there is already a volume active in the container
7637 if (super
->orom
&& dl
->index
< 0 && mpb
->num_raid_devs
)
7640 e
= get_extents(super
, dl
, 0);
7643 for (i
= 1; e
[i
-1].size
; i
++)
7650 maxsize
= merge_extents(super
, expanding
);
7651 if (maxsize
< minsize
) {
7652 pr_err("imsm: Free space is %llu but must be equal or larger than %llu.\n",
7654 return IMSM_STATUS_ERROR
;
7657 if (cnt
< raiddisks
|| (super
->orom
&& used
&& used
!= raiddisks
)) {
7658 pr_err("imsm: Not enough devices with space to create array.\n");
7659 return IMSM_STATUS_ERROR
;
7670 if (mpb
->num_raid_devs
> 0 && size
&& size
!= maxsize
)
7671 pr_err("attempting to create a second volume with size less then remaining space.\n");
7674 dprintf("imsm: imsm_get_free_size() returns : %llu\n", size
);
7676 return IMSM_STATUS_OK
;
7680 * autolayout_imsm() - automatically layout a new volume.
7681 * @super: &intel_super pointer, not NULL.
7682 * @raiddisks: number of raid disks.
7683 * @size: requested size, could be 0 (means max size).
7684 * @chunk: requested chunk.
7685 * @freesize: pointer for returned size value.
7687 * We are being asked to automatically layout a new volume based on the current
7688 * contents of the container. If the parameters can be satisfied autolayout_imsm
7689 * will record the disks, start offset, and will return size of the volume to
7690 * be created. See imsm_get_free_size() for details.
7691 * add_to_super() and getinfo_super() detect when autolayout is in progress.
7692 * If first volume exists, slots are set consistently to it.
7694 * Return: &IMSM_STATUS_OK on success, &IMSM_STATUS_ERROR otherwise.
7696 * Disks are marked for creation via dl->raiddisk.
7698 static imsm_status_t
autolayout_imsm(struct intel_super
*super
,
7699 const int raiddisks
,
7700 unsigned long long size
, const int chunk
,
7701 unsigned long long *freesize
)
7705 int vol_cnt
= super
->anchor
->num_raid_devs
;
7708 rv
= imsm_get_free_size(super
, raiddisks
, size
, chunk
, freesize
, false);
7709 if (rv
!= IMSM_STATUS_OK
)
7710 return IMSM_STATUS_ERROR
;
7712 for (disk
= super
->disks
; disk
; disk
= disk
->next
) {
7716 if (curr_slot
== raiddisks
)
7720 disk
->raiddisk
= curr_slot
;
7722 int _slot
= get_disk_slot_in_dev(super
, 0, disk
->index
);
7725 pr_err("Disk %s is not used in first volume, aborting\n",
7727 return IMSM_STATUS_ERROR
;
7729 disk
->raiddisk
= _slot
;
7734 return IMSM_STATUS_OK
;
7737 static int validate_geometry_imsm(struct supertype
*st
, int level
, int layout
,
7738 int raiddisks
, int *chunk
, unsigned long long size
,
7739 unsigned long long data_offset
,
7740 char *dev
, unsigned long long *freesize
,
7741 int consistency_policy
, int verbose
)
7748 * if given unused devices create a container
7749 * if given given devices in a container create a member volume
7751 if (is_container(level
))
7752 /* Must be a fresh device to add to a container */
7753 return validate_geometry_imsm_container(st
, level
, raiddisks
,
7758 * Size is given in sectors.
7760 if (size
&& (size
< 2048)) {
7761 pr_err("Given size must be greater than 1M.\n");
7762 /* Depends on algorithm in Create.c :
7763 * if container was given (dev == NULL) return -1,
7764 * if block device was given ( dev != NULL) return 0.
7766 return dev
? -1 : 0;
7770 struct intel_super
*super
= st
->sb
;
7773 * Autolayout mode, st->sb must be set.
7776 pr_vrb("superblock must be set for autolayout, aborting\n");
7780 if (!validate_geometry_imsm_orom(st
->sb
, level
, layout
,
7781 raiddisks
, chunk
, size
,
7785 if (super
->orom
&& freesize
) {
7787 int count
= count_volumes(super
->hba
, super
->orom
->dpa
,
7789 if (super
->orom
->vphba
<= count
) {
7790 pr_vrb("platform does not support more than %d raid volumes.\n",
7791 super
->orom
->vphba
);
7795 rv
= autolayout_imsm(super
, raiddisks
, size
, *chunk
,
7797 if (rv
!= IMSM_STATUS_OK
)
7803 /* creating in a given container */
7804 return validate_geometry_imsm_volume(st
, level
, layout
,
7805 raiddisks
, chunk
, size
,
7807 dev
, freesize
, verbose
);
7810 /* This device needs to be a device in an 'imsm' container */
7811 fd
= open(dev
, O_RDONLY
|O_EXCL
, 0);
7813 if (is_fd_valid(fd
)) {
7814 pr_vrb("Cannot create this array on device %s\n", dev
);
7819 fd
= open(dev
, O_RDONLY
, 0);
7821 if (!is_fd_valid(fd
)) {
7822 pr_vrb("Cannot open %s: %s\n", dev
, strerror(errno
));
7826 /* Well, it is in use by someone, maybe an 'imsm' container. */
7827 cfd
= open_container(fd
);
7830 if (!is_fd_valid(cfd
)) {
7831 pr_vrb("Cannot use %s: It is busy\n", dev
);
7834 sra
= sysfs_read(cfd
, NULL
, GET_VERSION
);
7835 if (sra
&& sra
->array
.major_version
== -1 &&
7836 strcmp(sra
->text_version
, "imsm") == 0)
7840 /* This is a member of a imsm container. Load the container
7841 * and try to create a volume
7843 struct intel_super
*super
;
7845 if (load_super_imsm_all(st
, cfd
, (void **) &super
, NULL
, NULL
, 1) == 0) {
7847 strcpy(st
->container_devnm
, fd2devnm(cfd
));
7849 return validate_geometry_imsm_volume(st
, level
, layout
,
7851 size
, data_offset
, dev
,
7858 pr_err("failed container membership check\n");
7864 static void default_geometry_imsm(struct supertype
*st
, int *level
, int *layout
, int *chunk
)
7866 struct intel_super
*super
= st
->sb
;
7868 if (level
&& *level
== UnSet
)
7869 *level
= LEVEL_CONTAINER
;
7871 if (level
&& layout
&& *layout
== UnSet
)
7872 *layout
= imsm_level_to_layout(*level
);
7874 if (chunk
&& (*chunk
== UnSet
|| *chunk
== 0))
7875 *chunk
= imsm_default_chunk(super
->orom
);
7878 static void handle_missing(struct intel_super
*super
, struct imsm_dev
*dev
);
7880 static int kill_subarray_imsm(struct supertype
*st
, char *subarray_id
)
7882 /* remove the subarray currently referenced by subarray_id */
7884 struct intel_dev
**dp
;
7885 struct intel_super
*super
= st
->sb
;
7886 __u8 current_vol
= strtoul(subarray_id
, NULL
, 10);
7887 struct imsm_super
*mpb
= super
->anchor
;
7889 if (mpb
->num_raid_devs
== 0)
7892 /* block deletions that would change the uuid of active subarrays
7894 * FIXME when immutable ids are available, but note that we'll
7895 * also need to fixup the invalidated/active subarray indexes in
7898 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
7901 if (i
< current_vol
)
7903 snprintf(subarray
, sizeof(subarray
), "%u", i
);
7904 if (is_subarray_active(subarray
, st
->devnm
)) {
7905 pr_err("deleting subarray-%d would change the UUID of active subarray-%d, aborting\n",
7912 if (st
->update_tail
) {
7913 struct imsm_update_kill_array
*u
= xmalloc(sizeof(*u
));
7915 u
->type
= update_kill_array
;
7916 u
->dev_idx
= current_vol
;
7917 append_metadata_update(st
, u
, sizeof(*u
));
7922 for (dp
= &super
->devlist
; *dp
;)
7923 if ((*dp
)->index
== current_vol
) {
7926 handle_missing(super
, (*dp
)->dev
);
7927 if ((*dp
)->index
> current_vol
)
7932 /* no more raid devices, all active components are now spares,
7933 * but of course failed are still failed
7935 if (--mpb
->num_raid_devs
== 0) {
7938 for (d
= super
->disks
; d
; d
= d
->next
)
7943 super
->updates_pending
++;
7949 * get_rwh_policy_from_update() - Get the rwh policy for update option.
7950 * @update: Update option.
7952 static int get_rwh_policy_from_update(enum update_opt update
)
7956 return RWH_MULTIPLE_DISTRIBUTED
;
7958 return RWH_MULTIPLE_OFF
;
7961 case UOPT_NO_BITMAP
:
7966 return UOPT_UNDEFINED
;
7969 static int update_subarray_imsm(struct supertype
*st
, char *subarray
,
7970 enum update_opt update
, struct mddev_ident
*ident
)
7972 /* update the subarray currently referenced by ->current_vol */
7973 struct intel_super
*super
= st
->sb
;
7974 struct imsm_super
*mpb
= super
->anchor
;
7976 if (update
== UOPT_NAME
) {
7977 char *name
= ident
->name
;
7981 if (imsm_is_name_allowed(super
, name
, 1) == false)
7984 vol
= strtoul(subarray
, &ep
, 10);
7985 if (*ep
!= '\0' || vol
>= super
->anchor
->num_raid_devs
)
7988 if (st
->update_tail
) {
7989 struct imsm_update_rename_array
*u
= xmalloc(sizeof(*u
));
7991 u
->type
= update_rename_array
;
7993 strncpy((char *) u
->name
, name
, MAX_RAID_SERIAL_LEN
);
7994 u
->name
[MAX_RAID_SERIAL_LEN
-1] = '\0';
7995 append_metadata_update(st
, u
, sizeof(*u
));
7997 struct imsm_dev
*dev
;
8000 dev
= get_imsm_dev(super
, vol
);
8001 memset(dev
->volume
, '\0', MAX_RAID_SERIAL_LEN
);
8002 namelen
= min((int)strlen(name
), MAX_RAID_SERIAL_LEN
);
8003 memcpy(dev
->volume
, name
, namelen
);
8004 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
8005 dev
= get_imsm_dev(super
, i
);
8006 handle_missing(super
, dev
);
8008 super
->updates_pending
++;
8010 } else if (get_rwh_policy_from_update(update
) != UOPT_UNDEFINED
) {
8013 int vol
= strtoul(subarray
, &ep
, 10);
8015 if (*ep
!= '\0' || vol
>= super
->anchor
->num_raid_devs
)
8018 new_policy
= get_rwh_policy_from_update(update
);
8020 if (st
->update_tail
) {
8021 struct imsm_update_rwh_policy
*u
= xmalloc(sizeof(*u
));
8023 u
->type
= update_rwh_policy
;
8025 u
->new_policy
= new_policy
;
8026 append_metadata_update(st
, u
, sizeof(*u
));
8028 struct imsm_dev
*dev
;
8030 dev
= get_imsm_dev(super
, vol
);
8031 dev
->rwh_policy
= new_policy
;
8032 super
->updates_pending
++;
8034 if (new_policy
== RWH_BITMAP
)
8035 return write_init_bitmap_imsm_vol(st
, vol
);
8042 static bool is_gen_migration(struct imsm_dev
*dev
)
8044 if (dev
&& dev
->vol
.migr_state
&&
8045 migr_type(dev
) == MIGR_GEN_MIGR
)
8051 static int is_rebuilding(struct imsm_dev
*dev
)
8053 struct imsm_map
*migr_map
;
8055 if (!dev
->vol
.migr_state
)
8058 if (migr_type(dev
) != MIGR_REBUILD
)
8061 migr_map
= get_imsm_map(dev
, MAP_1
);
8063 if (migr_map
->map_state
== IMSM_T_STATE_DEGRADED
)
8069 static int is_initializing(struct imsm_dev
*dev
)
8071 struct imsm_map
*migr_map
;
8073 if (!dev
->vol
.migr_state
)
8076 if (migr_type(dev
) != MIGR_INIT
)
8079 migr_map
= get_imsm_map(dev
, MAP_1
);
8081 if (migr_map
->map_state
== IMSM_T_STATE_UNINITIALIZED
)
8087 static void update_recovery_start(struct intel_super
*super
,
8088 struct imsm_dev
*dev
,
8089 struct mdinfo
*array
)
8091 struct mdinfo
*rebuild
= NULL
;
8095 if (!is_rebuilding(dev
))
8098 /* Find the rebuild target, but punt on the dual rebuild case */
8099 for (d
= array
->devs
; d
; d
= d
->next
)
8100 if (d
->recovery_start
== 0) {
8107 /* (?) none of the disks are marked with
8108 * IMSM_ORD_REBUILD, so assume they are missing and the
8109 * disk_ord_tbl was not correctly updated
8111 dprintf("failed to locate out-of-sync disk\n");
8115 units
= vol_curr_migr_unit(dev
);
8116 rebuild
->recovery_start
= units
* blocks_per_migr_unit(super
, dev
);
8119 static int recover_backup_imsm(struct supertype
*st
, struct mdinfo
*info
);
8121 static struct mdinfo
*container_content_imsm(struct supertype
*st
, char *subarray
)
8123 /* Given a container loaded by load_super_imsm_all,
8124 * extract information about all the arrays into
8126 * If 'subarray' is given, just extract info about that array.
8128 * For each imsm_dev create an mdinfo, fill it in,
8129 * then look for matching devices in super->disks
8130 * and create appropriate device mdinfo.
8132 struct intel_super
*super
= st
->sb
;
8133 struct imsm_super
*mpb
= super
->anchor
;
8134 struct mdinfo
*rest
= NULL
;
8138 int spare_disks
= 0;
8139 int current_vol
= super
->current_vol
;
8141 /* do not assemble arrays when not all attributes are supported */
8142 if (imsm_check_attributes(mpb
->attributes
) == 0) {
8144 pr_err("Unsupported attributes in IMSM metadata.Arrays activation is blocked.\n");
8147 /* count spare devices, not used in maps
8149 for (d
= super
->disks
; d
; d
= d
->next
)
8153 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
8154 struct imsm_dev
*dev
;
8155 struct imsm_map
*map
;
8156 struct imsm_map
*map2
;
8157 struct mdinfo
*this;
8164 (i
!= strtoul(subarray
, &ep
, 10) || *ep
!= '\0'))
8167 dev
= get_imsm_dev(super
, i
);
8168 map
= get_imsm_map(dev
, MAP_0
);
8169 map2
= get_imsm_map(dev
, MAP_1
);
8170 level
= get_imsm_raid_level(map
);
8172 /* do not publish arrays that are in the middle of an
8173 * unsupported migration
8175 if (dev
->vol
.migr_state
&&
8176 (migr_type(dev
) == MIGR_STATE_CHANGE
)) {
8177 pr_err("cannot assemble volume '%.16s': unsupported migration in progress\n",
8181 /* do not publish arrays that are not support by controller's
8185 this = xmalloc(sizeof(*this));
8187 super
->current_vol
= i
;
8188 getinfo_super_imsm_volume(st
, this, NULL
);
8190 chunk
= __le16_to_cpu(map
->blocks_per_strip
) >> 1;
8191 /* mdadm does not support all metadata features- set the bit in all arrays state */
8192 if (!validate_geometry_imsm_orom(super
,
8193 level
, /* RAID level */
8194 imsm_level_to_layout(level
),
8195 map
->num_members
, /* raid disks */
8196 &chunk
, imsm_dev_size(dev
),
8198 pr_err("IMSM RAID geometry validation failed. Array %s activation is blocked.\n",
8200 this->array
.state
|=
8201 (1<<MD_SB_BLOCK_CONTAINER_RESHAPE
) |
8202 (1<<MD_SB_BLOCK_VOLUME
);
8205 /* if array has bad blocks, set suitable bit in all arrays state */
8207 this->array
.state
|=
8208 (1<<MD_SB_BLOCK_CONTAINER_RESHAPE
) |
8209 (1<<MD_SB_BLOCK_VOLUME
);
8211 for (slot
= 0 ; slot
< map
->num_members
; slot
++) {
8212 unsigned long long recovery_start
;
8213 struct mdinfo
*info_d
;
8221 idx
= get_imsm_disk_idx(dev
, slot
, MAP_0
);
8222 ord
= get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
);
8223 for (d
= super
->disks
; d
; d
= d
->next
)
8224 if (d
->index
== idx
)
8227 recovery_start
= MaxSector
;
8230 if (d
&& is_failed(&d
->disk
))
8232 if (!skip
&& (ord
& IMSM_ORD_REBUILD
))
8234 if (!(ord
& IMSM_ORD_REBUILD
))
8235 this->array
.working_disks
++;
8237 * if we skip some disks the array will be assmebled degraded;
8238 * reset resync start to avoid a dirty-degraded
8239 * situation when performing the intial sync
8244 if (!(dev
->vol
.dirty
& RAIDVOL_DIRTY
)) {
8245 if ((!able_to_resync(level
, missing
) ||
8246 recovery_start
== 0))
8247 this->resync_start
= MaxSector
;
8253 info_d
= xcalloc(1, sizeof(*info_d
));
8254 info_d
->next
= this->devs
;
8255 this->devs
= info_d
;
8257 info_d
->disk
.number
= d
->index
;
8258 info_d
->disk
.major
= d
->major
;
8259 info_d
->disk
.minor
= d
->minor
;
8260 info_d
->disk
.raid_disk
= slot
;
8261 info_d
->recovery_start
= recovery_start
;
8263 if (slot
< map2
->num_members
)
8264 info_d
->disk
.state
= (1 << MD_DISK_ACTIVE
);
8266 this->array
.spare_disks
++;
8268 if (slot
< map
->num_members
)
8269 info_d
->disk
.state
= (1 << MD_DISK_ACTIVE
);
8271 this->array
.spare_disks
++;
8274 info_d
->events
= __le32_to_cpu(mpb
->generation_num
);
8275 info_d
->data_offset
= pba_of_lba0(map
);
8276 info_d
->component_size
= calc_component_size(map
, dev
);
8278 if (map
->raid_level
== 5) {
8279 info_d
->ppl_sector
= this->ppl_sector
;
8280 info_d
->ppl_size
= this->ppl_size
;
8281 if (this->consistency_policy
== CONSISTENCY_POLICY_PPL
&&
8282 recovery_start
== 0)
8283 this->resync_start
= 0;
8286 info_d
->bb
.supported
= 1;
8287 get_volume_badblocks(super
->bbm_log
, ord_to_idx(ord
),
8288 info_d
->data_offset
,
8289 info_d
->component_size
,
8292 /* now that the disk list is up-to-date fixup recovery_start */
8293 update_recovery_start(super
, dev
, this);
8294 this->array
.spare_disks
+= spare_disks
;
8296 /* check for reshape */
8297 if (this->reshape_active
== 1)
8298 recover_backup_imsm(st
, this);
8302 super
->current_vol
= current_vol
;
8306 static __u8
imsm_check_degraded(struct intel_super
*super
, struct imsm_dev
*dev
,
8307 int failed
, int look_in_map
)
8309 struct imsm_map
*map
;
8311 map
= get_imsm_map(dev
, look_in_map
);
8314 return map
->map_state
== IMSM_T_STATE_UNINITIALIZED
?
8315 IMSM_T_STATE_UNINITIALIZED
: IMSM_T_STATE_NORMAL
;
8317 switch (get_imsm_raid_level(map
)) {
8319 return IMSM_T_STATE_FAILED
;
8322 if (failed
< map
->num_members
)
8323 return IMSM_T_STATE_DEGRADED
;
8325 return IMSM_T_STATE_FAILED
;
8330 * check to see if any mirrors have failed, otherwise we
8331 * are degraded. Even numbered slots are mirrored on
8335 /* gcc -Os complains that this is unused */
8336 int insync
= insync
;
8338 for (i
= 0; i
< map
->num_members
; i
++) {
8339 __u32 ord
= get_imsm_ord_tbl_ent(dev
, i
, MAP_X
);
8340 int idx
= ord_to_idx(ord
);
8341 struct imsm_disk
*disk
;
8343 /* reset the potential in-sync count on even-numbered
8344 * slots. num_copies is always 2 for imsm raid10
8349 disk
= get_imsm_disk(super
, idx
);
8350 if (!disk
|| is_failed(disk
) || ord
& IMSM_ORD_REBUILD
)
8353 /* no in-sync disks left in this mirror the
8357 return IMSM_T_STATE_FAILED
;
8360 return IMSM_T_STATE_DEGRADED
;
8364 return IMSM_T_STATE_DEGRADED
;
8366 return IMSM_T_STATE_FAILED
;
8372 return map
->map_state
;
8375 static int imsm_count_failed(struct intel_super
*super
, struct imsm_dev
*dev
,
8380 struct imsm_disk
*disk
;
8381 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8382 struct imsm_map
*prev
= get_imsm_map(dev
, MAP_1
);
8383 struct imsm_map
*map_for_loop
;
8388 /* at the beginning of migration we set IMSM_ORD_REBUILD on
8389 * disks that are being rebuilt. New failures are recorded to
8390 * map[0]. So we look through all the disks we started with and
8391 * see if any failures are still present, or if any new ones
8395 if (prev
&& (map
->num_members
< prev
->num_members
))
8396 map_for_loop
= prev
;
8398 for (i
= 0; i
< map_for_loop
->num_members
; i
++) {
8400 /* when MAP_X is passed both maps failures are counted
8403 (look_in_map
== MAP_1
|| look_in_map
== MAP_X
) &&
8404 i
< prev
->num_members
) {
8405 ord
= __le32_to_cpu(prev
->disk_ord_tbl
[i
]);
8406 idx_1
= ord_to_idx(ord
);
8408 disk
= get_imsm_disk(super
, idx_1
);
8409 if (!disk
|| is_failed(disk
) || ord
& IMSM_ORD_REBUILD
)
8412 if ((look_in_map
== MAP_0
|| look_in_map
== MAP_X
) &&
8413 i
< map
->num_members
) {
8414 ord
= __le32_to_cpu(map
->disk_ord_tbl
[i
]);
8415 idx
= ord_to_idx(ord
);
8418 disk
= get_imsm_disk(super
, idx
);
8419 if (!disk
|| is_failed(disk
) ||
8420 ord
& IMSM_ORD_REBUILD
)
8429 static int imsm_open_new(struct supertype
*c
, struct active_array
*a
,
8432 struct intel_super
*super
= c
->sb
;
8433 struct imsm_super
*mpb
= super
->anchor
;
8434 struct imsm_update_prealloc_bb_mem u
;
8436 if (inst
>= mpb
->num_raid_devs
) {
8437 pr_err("subarry index %d, out of range\n", inst
);
8441 dprintf("imsm: open_new %d\n", inst
);
8442 a
->info
.container_member
= inst
;
8444 u
.type
= update_prealloc_badblocks_mem
;
8445 imsm_update_metadata_locally(c
, &u
, sizeof(u
));
8450 static int is_resyncing(struct imsm_dev
*dev
)
8452 struct imsm_map
*migr_map
;
8454 if (!dev
->vol
.migr_state
)
8457 if (migr_type(dev
) == MIGR_INIT
||
8458 migr_type(dev
) == MIGR_REPAIR
)
8461 if (migr_type(dev
) == MIGR_GEN_MIGR
)
8464 migr_map
= get_imsm_map(dev
, MAP_1
);
8466 if (migr_map
->map_state
== IMSM_T_STATE_NORMAL
&&
8467 dev
->vol
.migr_type
!= MIGR_GEN_MIGR
)
8473 /* return true if we recorded new information */
8474 static int mark_failure(struct intel_super
*super
,
8475 struct imsm_dev
*dev
, struct imsm_disk
*disk
, int idx
)
8479 struct imsm_map
*map
;
8480 char buf
[MAX_RAID_SERIAL_LEN
+3];
8481 unsigned int len
, shift
= 0;
8483 /* new failures are always set in map[0] */
8484 map
= get_imsm_map(dev
, MAP_0
);
8486 slot
= get_imsm_disk_slot(map
, idx
);
8490 ord
= __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
8491 if (is_failed(disk
) && (ord
& IMSM_ORD_REBUILD
))
8494 memcpy(buf
, disk
->serial
, MAX_RAID_SERIAL_LEN
);
8495 buf
[MAX_RAID_SERIAL_LEN
] = '\000';
8497 if ((len
= strlen(buf
)) >= MAX_RAID_SERIAL_LEN
)
8498 shift
= len
- MAX_RAID_SERIAL_LEN
+ 1;
8499 memcpy(disk
->serial
, &buf
[shift
], len
+ 1 - shift
);
8501 disk
->status
|= FAILED_DISK
;
8502 set_imsm_ord_tbl_ent(map
, slot
, idx
| IMSM_ORD_REBUILD
);
8503 /* mark failures in second map if second map exists and this disk
8505 * This is valid for migration, initialization and rebuild
8507 if (dev
->vol
.migr_state
) {
8508 struct imsm_map
*map2
= get_imsm_map(dev
, MAP_1
);
8509 int slot2
= get_imsm_disk_slot(map2
, idx
);
8511 if (slot2
< map2
->num_members
&& slot2
>= 0)
8512 set_imsm_ord_tbl_ent(map2
, slot2
,
8513 idx
| IMSM_ORD_REBUILD
);
8515 if (map
->failed_disk_num
== 0xff ||
8516 (!is_rebuilding(dev
) && map
->failed_disk_num
> slot
))
8517 map
->failed_disk_num
= slot
;
8519 clear_disk_badblocks(super
->bbm_log
, ord_to_idx(ord
));
8524 static void mark_missing(struct intel_super
*super
,
8525 struct imsm_dev
*dev
, struct imsm_disk
*disk
, int idx
)
8527 mark_failure(super
, dev
, disk
, idx
);
8529 if (disk
->scsi_id
== __cpu_to_le32(~(__u32
)0))
8532 disk
->scsi_id
= __cpu_to_le32(~(__u32
)0);
8533 memmove(&disk
->serial
[0], &disk
->serial
[1], MAX_RAID_SERIAL_LEN
- 1);
8536 static void handle_missing(struct intel_super
*super
, struct imsm_dev
*dev
)
8540 if (!super
->missing
)
8543 /* When orom adds replacement for missing disk it does
8544 * not remove entry of missing disk, but just updates map with
8545 * new added disk. So it is not enough just to test if there is
8546 * any missing disk, we have to look if there are any failed disks
8547 * in map to stop migration */
8549 dprintf("imsm: mark missing\n");
8550 /* end process for initialization and rebuild only
8552 if (is_gen_migration(dev
) == false) {
8553 int failed
= imsm_count_failed(super
, dev
, MAP_0
);
8557 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8558 struct imsm_map
*map1
;
8559 int i
, ord
, ord_map1
;
8562 for (i
= 0; i
< map
->num_members
; i
++) {
8563 ord
= get_imsm_ord_tbl_ent(dev
, i
, MAP_0
);
8564 if (!(ord
& IMSM_ORD_REBUILD
))
8567 map1
= get_imsm_map(dev
, MAP_1
);
8571 ord_map1
= __le32_to_cpu(map1
->disk_ord_tbl
[i
]);
8572 if (ord_map1
& IMSM_ORD_REBUILD
)
8577 map_state
= imsm_check_degraded(super
, dev
,
8579 end_migration(dev
, super
, map_state
);
8583 for (dl
= super
->missing
; dl
; dl
= dl
->next
)
8584 mark_missing(super
, dev
, &dl
->disk
, dl
->index
);
8585 super
->updates_pending
++;
8588 static unsigned long long imsm_set_array_size(struct imsm_dev
*dev
,
8591 unsigned long long array_blocks
;
8592 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8593 int used_disks
= imsm_num_data_members(map
);
8595 if (used_disks
== 0) {
8596 /* when problems occures
8597 * return current array_blocks value
8599 array_blocks
= imsm_dev_size(dev
);
8601 return array_blocks
;
8604 /* set array size in metadata
8607 /* OLCE size change is caused by added disks
8609 array_blocks
= per_dev_array_size(map
) * used_disks
;
8611 /* Online Volume Size Change
8612 * Using available free space
8614 array_blocks
= new_size
;
8616 array_blocks
= round_size_to_mb(array_blocks
, used_disks
);
8617 set_imsm_dev_size(dev
, array_blocks
);
8619 return array_blocks
;
8622 static void imsm_set_disk(struct active_array
*a
, int n
, int state
);
8624 static void imsm_progress_container_reshape(struct intel_super
*super
)
8626 /* if no device has a migr_state, but some device has a
8627 * different number of members than the previous device, start
8628 * changing the number of devices in this device to match
8631 struct imsm_super
*mpb
= super
->anchor
;
8632 int prev_disks
= -1;
8636 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
8637 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
8638 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8639 struct imsm_map
*map2
;
8640 int prev_num_members
;
8642 if (dev
->vol
.migr_state
)
8645 if (prev_disks
== -1)
8646 prev_disks
= map
->num_members
;
8647 if (prev_disks
== map
->num_members
)
8650 /* OK, this array needs to enter reshape mode.
8651 * i.e it needs a migr_state
8654 copy_map_size
= sizeof_imsm_map(map
);
8655 prev_num_members
= map
->num_members
;
8656 map
->num_members
= prev_disks
;
8657 dev
->vol
.migr_state
= 1;
8658 set_vol_curr_migr_unit(dev
, 0);
8659 set_migr_type(dev
, MIGR_GEN_MIGR
);
8660 for (i
= prev_num_members
;
8661 i
< map
->num_members
; i
++)
8662 set_imsm_ord_tbl_ent(map
, i
, i
);
8663 map2
= get_imsm_map(dev
, MAP_1
);
8664 /* Copy the current map */
8665 memcpy(map2
, map
, copy_map_size
);
8666 map2
->num_members
= prev_num_members
;
8668 imsm_set_array_size(dev
, -1);
8669 super
->clean_migration_record_by_mdmon
= 1;
8670 super
->updates_pending
++;
8674 /* Handle dirty -> clean transititions, resync and reshape. Degraded and rebuild
8675 * states are handled in imsm_set_disk() with one exception, when a
8676 * resync is stopped due to a new failure this routine will set the
8677 * 'degraded' state for the array.
8679 static int imsm_set_array_state(struct active_array
*a
, int consistent
)
8681 int inst
= a
->info
.container_member
;
8682 struct intel_super
*super
= a
->container
->sb
;
8683 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
8684 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8685 int failed
= imsm_count_failed(super
, dev
, MAP_0
);
8686 __u8 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
8687 __u32 blocks_per_unit
;
8689 if (dev
->vol
.migr_state
&&
8690 dev
->vol
.migr_type
== MIGR_GEN_MIGR
) {
8691 /* array state change is blocked due to reshape action
8693 * - abort the reshape (if last_checkpoint is 0 and action!= reshape)
8694 * - finish the reshape (if last_checkpoint is big and action != reshape)
8695 * - update vol_curr_migr_unit
8697 if (a
->curr_action
== reshape
) {
8698 /* still reshaping, maybe update vol_curr_migr_unit */
8699 goto mark_checkpoint
;
8701 if (a
->last_checkpoint
>= a
->info
.component_size
) {
8702 unsigned long long array_blocks
;
8706 used_disks
= imsm_num_data_members(map
);
8707 if (used_disks
> 0) {
8709 per_dev_array_size(map
) *
8712 round_size_to_mb(array_blocks
,
8714 a
->info
.custom_array_size
= array_blocks
;
8715 /* encourage manager to update array
8719 a
->check_reshape
= 1;
8721 /* finalize online capacity expansion/reshape */
8722 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
)
8724 mdi
->disk
.raid_disk
,
8727 imsm_progress_container_reshape(super
);
8732 /* before we activate this array handle any missing disks */
8733 if (consistent
== 2)
8734 handle_missing(super
, dev
);
8736 if (consistent
== 2 &&
8737 (!is_resync_complete(&a
->info
) ||
8738 map_state
!= IMSM_T_STATE_NORMAL
||
8739 dev
->vol
.migr_state
))
8742 if (is_resync_complete(&a
->info
)) {
8743 /* complete intialization / resync,
8744 * recovery and interrupted recovery is completed in
8747 if (is_resyncing(dev
)) {
8748 dprintf("imsm: mark resync done\n");
8749 end_migration(dev
, super
, map_state
);
8750 super
->updates_pending
++;
8751 a
->last_checkpoint
= 0;
8753 } else if ((!is_resyncing(dev
) && !failed
) &&
8754 (imsm_reshape_blocks_arrays_changes(super
) == 0)) {
8755 /* mark the start of the init process if nothing is failed */
8756 dprintf("imsm: mark resync start\n");
8757 if (map
->map_state
== IMSM_T_STATE_UNINITIALIZED
)
8758 migrate(dev
, super
, IMSM_T_STATE_NORMAL
, MIGR_INIT
);
8760 migrate(dev
, super
, IMSM_T_STATE_NORMAL
, MIGR_REPAIR
);
8761 super
->updates_pending
++;
8764 if (a
->prev_action
== idle
)
8765 goto skip_mark_checkpoint
;
8768 /* skip checkpointing for general migration,
8769 * it is controlled in mdadm
8771 if (is_gen_migration(dev
))
8772 goto skip_mark_checkpoint
;
8774 /* check if we can update vol_curr_migr_unit from resync_start,
8777 blocks_per_unit
= blocks_per_migr_unit(super
, dev
);
8778 if (blocks_per_unit
) {
8779 set_vol_curr_migr_unit(dev
,
8780 a
->last_checkpoint
/ blocks_per_unit
);
8781 dprintf("imsm: mark checkpoint (%llu)\n",
8782 vol_curr_migr_unit(dev
));
8783 super
->updates_pending
++;
8786 skip_mark_checkpoint
:
8787 /* mark dirty / clean */
8788 if (((dev
->vol
.dirty
& RAIDVOL_DIRTY
) && consistent
) ||
8789 (!(dev
->vol
.dirty
& RAIDVOL_DIRTY
) && !consistent
)) {
8790 dprintf("imsm: mark '%s'\n", consistent
? "clean" : "dirty");
8792 dev
->vol
.dirty
= RAIDVOL_CLEAN
;
8794 dev
->vol
.dirty
= RAIDVOL_DIRTY
;
8795 if (dev
->rwh_policy
== RWH_DISTRIBUTED
||
8796 dev
->rwh_policy
== RWH_MULTIPLE_DISTRIBUTED
)
8797 dev
->vol
.dirty
|= RAIDVOL_DSRECORD_VALID
;
8799 super
->updates_pending
++;
8805 static int imsm_disk_slot_to_ord(struct active_array
*a
, int slot
)
8807 int inst
= a
->info
.container_member
;
8808 struct intel_super
*super
= a
->container
->sb
;
8809 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
8810 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8812 if (slot
> map
->num_members
) {
8813 pr_err("imsm: imsm_disk_slot_to_ord %d out of range 0..%d\n",
8814 slot
, map
->num_members
- 1);
8821 return get_imsm_ord_tbl_ent(dev
, slot
, MAP_0
);
8824 static void imsm_set_disk(struct active_array
*a
, int n
, int state
)
8826 int inst
= a
->info
.container_member
;
8827 struct intel_super
*super
= a
->container
->sb
;
8828 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
8829 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8830 struct imsm_disk
*disk
;
8832 int recovery_not_finished
= 0;
8836 int rebuild_done
= 0;
8839 ord
= get_imsm_ord_tbl_ent(dev
, n
, MAP_X
);
8843 dprintf("imsm: set_disk %d:%x\n", n
, state
);
8844 disk
= get_imsm_disk(super
, ord_to_idx(ord
));
8846 /* check for new failures */
8847 if (disk
&& (state
& DS_FAULTY
)) {
8848 if (mark_failure(super
, dev
, disk
, ord_to_idx(ord
)))
8849 super
->updates_pending
++;
8852 /* check if in_sync */
8853 if (state
& DS_INSYNC
&& ord
& IMSM_ORD_REBUILD
&& is_rebuilding(dev
)) {
8854 struct imsm_map
*migr_map
= get_imsm_map(dev
, MAP_1
);
8856 set_imsm_ord_tbl_ent(migr_map
, n
, ord_to_idx(ord
));
8858 super
->updates_pending
++;
8861 failed
= imsm_count_failed(super
, dev
, MAP_0
);
8862 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
8864 /* check if recovery complete, newly degraded, or failed */
8865 dprintf("imsm: Detected transition to state ");
8866 switch (map_state
) {
8867 case IMSM_T_STATE_NORMAL
: /* transition to normal state */
8868 dprintf("normal: ");
8869 if (is_rebuilding(dev
)) {
8870 dprintf_cont("while rebuilding");
8871 /* check if recovery is really finished */
8872 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
)
8873 if (mdi
->recovery_start
!= MaxSector
) {
8874 recovery_not_finished
= 1;
8877 if (recovery_not_finished
) {
8879 dprintf("Rebuild has not finished yet, state not changed");
8880 if (a
->last_checkpoint
< mdi
->recovery_start
) {
8881 a
->last_checkpoint
= mdi
->recovery_start
;
8882 super
->updates_pending
++;
8886 end_migration(dev
, super
, map_state
);
8887 map
->failed_disk_num
= ~0;
8888 super
->updates_pending
++;
8889 a
->last_checkpoint
= 0;
8892 if (is_gen_migration(dev
)) {
8893 dprintf_cont("while general migration");
8894 if (a
->last_checkpoint
>= a
->info
.component_size
)
8895 end_migration(dev
, super
, map_state
);
8897 map
->map_state
= map_state
;
8898 map
->failed_disk_num
= ~0;
8899 super
->updates_pending
++;
8903 case IMSM_T_STATE_DEGRADED
: /* transition to degraded state */
8904 dprintf_cont("degraded: ");
8905 if (map
->map_state
!= map_state
&& !dev
->vol
.migr_state
) {
8906 dprintf_cont("mark degraded");
8907 map
->map_state
= map_state
;
8908 super
->updates_pending
++;
8909 a
->last_checkpoint
= 0;
8912 if (is_rebuilding(dev
)) {
8913 dprintf_cont("while rebuilding ");
8914 if (state
& DS_FAULTY
) {
8915 dprintf_cont("removing failed drive ");
8916 if (n
== map
->failed_disk_num
) {
8917 dprintf_cont("end migration");
8918 end_migration(dev
, super
, map_state
);
8919 a
->last_checkpoint
= 0;
8921 dprintf_cont("fail detected during rebuild, changing map state");
8922 map
->map_state
= map_state
;
8924 super
->updates_pending
++;
8930 /* check if recovery is really finished */
8931 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
)
8932 if (mdi
->recovery_start
!= MaxSector
) {
8933 recovery_not_finished
= 1;
8936 if (recovery_not_finished
) {
8938 dprintf_cont("Rebuild has not finished yet");
8939 if (a
->last_checkpoint
< mdi
->recovery_start
) {
8940 a
->last_checkpoint
=
8941 mdi
->recovery_start
;
8942 super
->updates_pending
++;
8947 dprintf_cont(" Rebuild done, still degraded");
8948 end_migration(dev
, super
, map_state
);
8949 a
->last_checkpoint
= 0;
8950 super
->updates_pending
++;
8952 for (i
= 0; i
< map
->num_members
; i
++) {
8953 int idx
= get_imsm_ord_tbl_ent(dev
, i
, MAP_0
);
8955 if (idx
& IMSM_ORD_REBUILD
)
8956 map
->failed_disk_num
= i
;
8958 super
->updates_pending
++;
8961 if (is_gen_migration(dev
)) {
8962 dprintf_cont("while general migration");
8963 if (a
->last_checkpoint
>= a
->info
.component_size
)
8964 end_migration(dev
, super
, map_state
);
8966 map
->map_state
= map_state
;
8967 manage_second_map(super
, dev
);
8969 super
->updates_pending
++;
8972 if (is_initializing(dev
)) {
8973 dprintf_cont("while initialization.");
8974 map
->map_state
= map_state
;
8975 super
->updates_pending
++;
8979 case IMSM_T_STATE_FAILED
: /* transition to failed state */
8980 dprintf_cont("failed: ");
8981 if (is_gen_migration(dev
)) {
8982 dprintf_cont("while general migration");
8983 map
->map_state
= map_state
;
8984 super
->updates_pending
++;
8987 if (map
->map_state
!= map_state
) {
8988 dprintf_cont("mark failed");
8989 end_migration(dev
, super
, map_state
);
8990 super
->updates_pending
++;
8991 a
->last_checkpoint
= 0;
8996 dprintf_cont("state %i\n", map_state
);
9001 static int store_imsm_mpb(int fd
, struct imsm_super
*mpb
)
9004 __u32 mpb_size
= __le32_to_cpu(mpb
->mpb_size
);
9005 unsigned long long dsize
;
9006 unsigned long long sectors
;
9007 unsigned int sector_size
;
9009 if (!get_dev_sector_size(fd
, NULL
, §or_size
))
9011 get_dev_size(fd
, NULL
, &dsize
);
9013 if (mpb_size
> sector_size
) {
9014 /* -1 to account for anchor */
9015 sectors
= mpb_sectors(mpb
, sector_size
) - 1;
9017 /* write the extended mpb to the sectors preceeding the anchor */
9018 if (lseek64(fd
, dsize
- (sector_size
* (2 + sectors
)),
9022 if ((unsigned long long)write(fd
, buf
+ sector_size
,
9023 sector_size
* sectors
) != sector_size
* sectors
)
9027 /* first block is stored on second to last sector of the disk */
9028 if (lseek64(fd
, dsize
- (sector_size
* 2), SEEK_SET
) < 0)
9031 if ((unsigned int)write(fd
, buf
, sector_size
) != sector_size
)
9037 static void imsm_sync_metadata(struct supertype
*container
)
9039 struct intel_super
*super
= container
->sb
;
9041 dprintf("sync metadata: %d\n", super
->updates_pending
);
9042 if (!super
->updates_pending
)
9045 write_super_imsm(container
, 0);
9047 super
->updates_pending
= 0;
9050 static struct dl
*imsm_readd(struct intel_super
*super
, int idx
, struct active_array
*a
)
9052 struct imsm_dev
*dev
= get_imsm_dev(super
, a
->info
.container_member
);
9053 int i
= get_imsm_disk_idx(dev
, idx
, MAP_X
);
9056 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
9060 if (dl
&& is_failed(&dl
->disk
))
9064 dprintf("found %x:%x\n", dl
->major
, dl
->minor
);
9069 static struct dl
*imsm_add_spare(struct intel_super
*super
, int slot
,
9070 struct active_array
*a
, int activate_new
,
9071 struct mdinfo
*additional_test_list
)
9073 struct imsm_dev
*dev
= get_imsm_dev(super
, a
->info
.container_member
);
9074 int idx
= get_imsm_disk_idx(dev
, slot
, MAP_X
);
9075 struct imsm_super
*mpb
= super
->anchor
;
9076 struct imsm_map
*map
;
9077 unsigned long long pos
;
9082 __u32 array_start
= 0;
9083 __u32 array_end
= 0;
9085 struct mdinfo
*test_list
;
9087 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
9088 /* If in this array, skip */
9089 for (d
= a
->info
.devs
; d
; d
= d
->next
)
9090 if (is_fd_valid(d
->state_fd
) &&
9091 d
->disk
.major
== dl
->major
&&
9092 d
->disk
.minor
== dl
->minor
) {
9093 dprintf("%x:%x already in array\n",
9094 dl
->major
, dl
->minor
);
9099 test_list
= additional_test_list
;
9101 if (test_list
->disk
.major
== dl
->major
&&
9102 test_list
->disk
.minor
== dl
->minor
) {
9103 dprintf("%x:%x already in additional test list\n",
9104 dl
->major
, dl
->minor
);
9107 test_list
= test_list
->next
;
9112 /* skip in use or failed drives */
9113 if (is_failed(&dl
->disk
) || idx
== dl
->index
||
9115 dprintf("%x:%x status (failed: %d index: %d)\n",
9116 dl
->major
, dl
->minor
, is_failed(&dl
->disk
), idx
);
9120 /* skip pure spares when we are looking for partially
9121 * assimilated drives
9123 if (dl
->index
== -1 && !activate_new
)
9126 if (!drive_validate_sector_size(super
, dl
))
9129 /* Does this unused device have the requisite free space?
9130 * It needs to be able to cover all member volumes
9132 ex
= get_extents(super
, dl
, 1);
9134 dprintf("cannot get extents\n");
9137 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
9138 dev
= get_imsm_dev(super
, i
);
9139 map
= get_imsm_map(dev
, MAP_0
);
9141 /* check if this disk is already a member of
9144 if (get_imsm_disk_slot(map
, dl
->index
) >= 0)
9150 array_start
= pba_of_lba0(map
);
9151 array_end
= array_start
+
9152 per_dev_array_size(map
) - 1;
9155 /* check that we can start at pba_of_lba0 with
9156 * num_data_stripes*blocks_per_stripe of space
9158 if (array_start
>= pos
&& array_end
< ex
[j
].start
) {
9162 pos
= ex
[j
].start
+ ex
[j
].size
;
9164 } while (ex
[j
-1].size
);
9171 if (i
< mpb
->num_raid_devs
) {
9172 dprintf("%x:%x does not have %u to %u available\n",
9173 dl
->major
, dl
->minor
, array_start
, array_end
);
9183 static int imsm_rebuild_allowed(struct supertype
*cont
, int dev_idx
, int failed
)
9185 struct imsm_dev
*dev2
;
9186 struct imsm_map
*map
;
9192 dev2
= get_imsm_dev(cont
->sb
, dev_idx
);
9194 state
= imsm_check_degraded(cont
->sb
, dev2
, failed
, MAP_0
);
9195 if (state
== IMSM_T_STATE_FAILED
) {
9196 map
= get_imsm_map(dev2
, MAP_0
);
9197 for (slot
= 0; slot
< map
->num_members
; slot
++) {
9199 * Check if failed disks are deleted from intel
9200 * disk list or are marked to be deleted
9202 idx
= get_imsm_disk_idx(dev2
, slot
, MAP_X
);
9203 idisk
= get_imsm_dl_disk(cont
->sb
, idx
);
9205 * Do not rebuild the array if failed disks
9206 * from failed sub-array are not removed from
9210 is_failed(&idisk
->disk
) &&
9211 (idisk
->action
!= DISK_REMOVE
))
9218 static struct mdinfo
*imsm_activate_spare(struct active_array
*a
,
9219 struct metadata_update
**updates
)
9222 * Find a device with unused free space and use it to replace a
9223 * failed/vacant region in an array. We replace failed regions one a
9224 * array at a time. The result is that a new spare disk will be added
9225 * to the first failed array and after the monitor has finished
9226 * propagating failures the remainder will be consumed.
9228 * FIXME add a capability for mdmon to request spares from another
9232 struct intel_super
*super
= a
->container
->sb
;
9233 int inst
= a
->info
.container_member
;
9234 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
9235 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
9236 int failed
= a
->info
.array
.raid_disks
;
9237 struct mdinfo
*rv
= NULL
;
9240 struct metadata_update
*mu
;
9242 struct imsm_update_activate_spare
*u
;
9247 for (d
= a
->info
.devs
; d
; d
= d
->next
) {
9248 if (!is_fd_valid(d
->state_fd
))
9251 if (d
->curr_state
& DS_FAULTY
)
9252 /* wait for Removal to happen */
9258 dprintf("imsm: activate spare: inst=%d failed=%d (%d) level=%d\n",
9259 inst
, failed
, a
->info
.array
.raid_disks
, a
->info
.array
.level
);
9261 if (imsm_reshape_blocks_arrays_changes(super
))
9264 /* Cannot activate another spare if rebuild is in progress already
9266 if (is_rebuilding(dev
)) {
9267 dprintf("imsm: No spare activation allowed. Rebuild in progress already.\n");
9271 if (a
->info
.array
.level
== 4)
9272 /* No repair for takeovered array
9273 * imsm doesn't support raid4
9277 if (imsm_check_degraded(super
, dev
, failed
, MAP_0
) !=
9278 IMSM_T_STATE_DEGRADED
)
9281 if (get_imsm_map(dev
, MAP_0
)->map_state
== IMSM_T_STATE_UNINITIALIZED
) {
9282 dprintf("imsm: No spare activation allowed. Volume is not initialized.\n");
9287 * If there are any failed disks check state of the other volume.
9288 * Block rebuild if the another one is failed until failed disks
9289 * are removed from container.
9292 dprintf("found failed disks in %.*s, check if there anotherfailed sub-array.\n",
9293 MAX_RAID_SERIAL_LEN
, dev
->volume
);
9294 /* check if states of the other volumes allow for rebuild */
9295 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
9297 allowed
= imsm_rebuild_allowed(a
->container
,
9305 /* For each slot, if it is not working, find a spare */
9306 for (i
= 0; i
< a
->info
.array
.raid_disks
; i
++) {
9307 for (d
= a
->info
.devs
; d
; d
= d
->next
)
9308 if (d
->disk
.raid_disk
== i
)
9310 dprintf("found %d: %p %x\n", i
, d
, d
?d
->curr_state
:0);
9311 if (d
&& is_fd_valid(d
->state_fd
))
9315 * OK, this device needs recovery. Try to re-add the
9316 * previous occupant of this slot, if this fails see if
9317 * we can continue the assimilation of a spare that was
9318 * partially assimilated, finally try to activate a new
9321 dl
= imsm_readd(super
, i
, a
);
9323 dl
= imsm_add_spare(super
, i
, a
, 0, rv
);
9325 dl
= imsm_add_spare(super
, i
, a
, 1, rv
);
9329 /* found a usable disk with enough space */
9330 di
= xcalloc(1, sizeof(*di
));
9332 /* dl->index will be -1 in the case we are activating a
9333 * pristine spare. imsm_process_update() will create a
9334 * new index in this case. Once a disk is found to be
9335 * failed in all member arrays it is kicked from the
9338 di
->disk
.number
= dl
->index
;
9340 /* (ab)use di->devs to store a pointer to the device
9343 di
->devs
= (struct mdinfo
*) dl
;
9345 di
->disk
.raid_disk
= i
;
9346 di
->disk
.major
= dl
->major
;
9347 di
->disk
.minor
= dl
->minor
;
9349 di
->recovery_start
= 0;
9350 di
->data_offset
= pba_of_lba0(map
);
9351 di
->component_size
= a
->info
.component_size
;
9352 di
->container_member
= inst
;
9353 di
->bb
.supported
= 1;
9354 if (a
->info
.consistency_policy
== CONSISTENCY_POLICY_PPL
) {
9355 di
->ppl_sector
= get_ppl_sector(super
, inst
);
9356 di
->ppl_size
= MULTIPLE_PPL_AREA_SIZE_IMSM
>> 9;
9358 super
->random
= random32();
9362 dprintf("%x:%x to be %d at %llu\n", dl
->major
, dl
->minor
,
9363 i
, di
->data_offset
);
9367 /* No spares found */
9369 /* Now 'rv' has a list of devices to return.
9370 * Create a metadata_update record to update the
9371 * disk_ord_tbl for the array
9373 mu
= xmalloc(sizeof(*mu
));
9374 mu
->buf
= xcalloc(num_spares
,
9375 sizeof(struct imsm_update_activate_spare
));
9377 mu
->space_list
= NULL
;
9378 mu
->len
= sizeof(struct imsm_update_activate_spare
) * num_spares
;
9379 mu
->next
= *updates
;
9380 u
= (struct imsm_update_activate_spare
*) mu
->buf
;
9382 for (di
= rv
; di
; di
= di
->next
) {
9383 u
->type
= update_activate_spare
;
9384 u
->dl
= (struct dl
*) di
->devs
;
9386 u
->slot
= di
->disk
.raid_disk
;
9397 static int disks_overlap(struct intel_super
*super
, int idx
, struct imsm_update_create_array
*u
)
9399 struct imsm_dev
*dev
= get_imsm_dev(super
, idx
);
9400 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
9401 struct imsm_map
*new_map
= get_imsm_map(&u
->dev
, MAP_0
);
9402 struct disk_info
*inf
= get_disk_info(u
);
9403 struct imsm_disk
*disk
;
9407 for (i
= 0; i
< map
->num_members
; i
++) {
9408 disk
= get_imsm_disk(super
, get_imsm_disk_idx(dev
, i
, MAP_X
));
9409 for (j
= 0; j
< new_map
->num_members
; j
++)
9410 if (serialcmp(disk
->serial
, inf
[j
].serial
) == 0)
9417 static struct dl
*get_disk_super(struct intel_super
*super
, int major
, int minor
)
9421 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
9422 if (dl
->major
== major
&& dl
->minor
== minor
)
9427 static int remove_disk_super(struct intel_super
*super
, int major
, int minor
)
9433 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
9434 if (dl
->major
== major
&& dl
->minor
== minor
) {
9437 prev
->next
= dl
->next
;
9439 super
->disks
= dl
->next
;
9441 __free_imsm_disk(dl
, 1);
9442 dprintf("removed %x:%x\n", major
, minor
);
9450 static void imsm_delete(struct intel_super
*super
, struct dl
**dlp
, unsigned index
);
9452 static int add_remove_disk_update(struct intel_super
*super
)
9454 int check_degraded
= 0;
9457 /* add/remove some spares to/from the metadata/contrainer */
9458 while (super
->disk_mgmt_list
) {
9459 struct dl
*disk_cfg
;
9461 disk_cfg
= super
->disk_mgmt_list
;
9462 super
->disk_mgmt_list
= disk_cfg
->next
;
9463 disk_cfg
->next
= NULL
;
9465 if (disk_cfg
->action
== DISK_ADD
) {
9466 disk_cfg
->next
= super
->disks
;
9467 super
->disks
= disk_cfg
;
9469 dprintf("added %x:%x\n",
9470 disk_cfg
->major
, disk_cfg
->minor
);
9471 } else if (disk_cfg
->action
== DISK_REMOVE
) {
9472 dprintf("Disk remove action processed: %x.%x\n",
9473 disk_cfg
->major
, disk_cfg
->minor
);
9474 disk
= get_disk_super(super
,
9478 /* store action status */
9479 disk
->action
= DISK_REMOVE
;
9480 /* remove spare disks only */
9481 if (disk
->index
== -1) {
9482 remove_disk_super(super
,
9486 disk_cfg
->fd
= disk
->fd
;
9490 /* release allocate disk structure */
9491 __free_imsm_disk(disk_cfg
, 1);
9494 return check_degraded
;
9497 static int apply_reshape_migration_update(struct imsm_update_reshape_migration
*u
,
9498 struct intel_super
*super
,
9501 struct intel_dev
*id
;
9502 void **tofree
= NULL
;
9505 dprintf("(enter)\n");
9506 if (u
->subdev
< 0 || u
->subdev
> 1) {
9507 dprintf("imsm: Error: Wrong subdev: %i\n", u
->subdev
);
9510 if (space_list
== NULL
|| *space_list
== NULL
) {
9511 dprintf("imsm: Error: Memory is not allocated\n");
9515 for (id
= super
->devlist
; id
; id
= id
->next
) {
9516 if (id
->index
== (unsigned)u
->subdev
) {
9517 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->subdev
);
9518 struct imsm_map
*map
;
9519 struct imsm_dev
*new_dev
=
9520 (struct imsm_dev
*)*space_list
;
9521 struct imsm_map
*migr_map
= get_imsm_map(dev
, MAP_1
);
9523 struct dl
*new_disk
;
9525 if (new_dev
== NULL
)
9527 *space_list
= **space_list
;
9528 memcpy(new_dev
, dev
, sizeof_imsm_dev(dev
, 0));
9529 map
= get_imsm_map(new_dev
, MAP_0
);
9531 dprintf("imsm: Error: migration in progress");
9535 to_state
= map
->map_state
;
9536 if ((u
->new_level
== 5) && (map
->raid_level
== 0)) {
9538 /* this should not happen */
9539 if (u
->new_disks
[0] < 0) {
9540 map
->failed_disk_num
=
9541 map
->num_members
- 1;
9542 to_state
= IMSM_T_STATE_DEGRADED
;
9544 to_state
= IMSM_T_STATE_NORMAL
;
9546 migrate(new_dev
, super
, to_state
, MIGR_GEN_MIGR
);
9547 if (u
->new_level
> -1)
9548 map
->raid_level
= u
->new_level
;
9549 migr_map
= get_imsm_map(new_dev
, MAP_1
);
9550 if ((u
->new_level
== 5) &&
9551 (migr_map
->raid_level
== 0)) {
9552 int ord
= map
->num_members
- 1;
9553 migr_map
->num_members
--;
9554 if (u
->new_disks
[0] < 0)
9555 ord
|= IMSM_ORD_REBUILD
;
9556 set_imsm_ord_tbl_ent(map
,
9557 map
->num_members
- 1,
9561 tofree
= (void **)dev
;
9563 /* update chunk size
9565 if (u
->new_chunksize
> 0) {
9566 struct imsm_map
*dest_map
=
9567 get_imsm_map(dev
, MAP_0
);
9569 imsm_num_data_members(dest_map
);
9571 if (used_disks
== 0)
9574 map
->blocks_per_strip
=
9575 __cpu_to_le16(u
->new_chunksize
* 2);
9576 update_num_data_stripes(map
, imsm_dev_size(dev
));
9579 /* ensure blocks_per_member has valid value
9581 set_blocks_per_member(map
,
9582 per_dev_array_size(map
) +
9583 NUM_BLOCKS_DIRTY_STRIPE_REGION
);
9587 if (u
->new_level
!= 5 || migr_map
->raid_level
!= 0 ||
9588 migr_map
->raid_level
== map
->raid_level
)
9591 if (u
->new_disks
[0] >= 0) {
9594 new_disk
= get_disk_super(super
,
9595 major(u
->new_disks
[0]),
9596 minor(u
->new_disks
[0]));
9597 dprintf("imsm: new disk for reshape is: %i:%i (%p, index = %i)\n",
9598 major(u
->new_disks
[0]),
9599 minor(u
->new_disks
[0]),
9600 new_disk
, new_disk
->index
);
9601 if (new_disk
== NULL
)
9602 goto error_disk_add
;
9604 new_disk
->index
= map
->num_members
- 1;
9605 /* slot to fill in autolayout
9607 new_disk
->raiddisk
= new_disk
->index
;
9608 new_disk
->disk
.status
|= CONFIGURED_DISK
;
9609 new_disk
->disk
.status
&= ~SPARE_DISK
;
9611 goto error_disk_add
;
9614 *tofree
= *space_list
;
9615 /* calculate new size
9617 imsm_set_array_size(new_dev
, -1);
9624 *space_list
= tofree
;
9628 dprintf("Error: imsm: Cannot find disk.\n");
9632 static int apply_size_change_update(struct imsm_update_size_change
*u
,
9633 struct intel_super
*super
)
9635 struct intel_dev
*id
;
9638 dprintf("(enter)\n");
9639 if (u
->subdev
< 0 || u
->subdev
> 1) {
9640 dprintf("imsm: Error: Wrong subdev: %i\n", u
->subdev
);
9644 for (id
= super
->devlist
; id
; id
= id
->next
) {
9645 if (id
->index
== (unsigned)u
->subdev
) {
9646 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->subdev
);
9647 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
9648 int used_disks
= imsm_num_data_members(map
);
9649 unsigned long long blocks_per_member
;
9650 unsigned long long new_size_per_disk
;
9652 if (used_disks
== 0)
9655 /* calculate new size
9657 new_size_per_disk
= u
->new_size
/ used_disks
;
9658 blocks_per_member
= new_size_per_disk
+
9659 NUM_BLOCKS_DIRTY_STRIPE_REGION
;
9661 imsm_set_array_size(dev
, u
->new_size
);
9662 set_blocks_per_member(map
, blocks_per_member
);
9663 update_num_data_stripes(map
, u
->new_size
);
9672 static int prepare_spare_to_activate(struct supertype
*st
,
9673 struct imsm_update_activate_spare
*u
)
9675 struct intel_super
*super
= st
->sb
;
9676 int prev_current_vol
= super
->current_vol
;
9677 struct active_array
*a
;
9680 for (a
= st
->arrays
; a
; a
= a
->next
)
9682 * Additional initialization (adding bitmap header, filling
9683 * the bitmap area with '1's to force initial rebuild for a whole
9684 * data-area) is required when adding the spare to the volume
9685 * with write-intent bitmap.
9687 if (a
->info
.container_member
== u
->array
&&
9688 a
->info
.consistency_policy
== CONSISTENCY_POLICY_BITMAP
) {
9691 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
9697 super
->current_vol
= u
->array
;
9698 if (st
->ss
->write_bitmap(st
, dl
->fd
, NoUpdate
))
9700 super
->current_vol
= prev_current_vol
;
9705 static int apply_update_activate_spare(struct imsm_update_activate_spare
*u
,
9706 struct intel_super
*super
,
9707 struct active_array
*active_array
)
9709 struct imsm_super
*mpb
= super
->anchor
;
9710 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->array
);
9711 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
9712 struct imsm_map
*migr_map
;
9713 struct active_array
*a
;
9714 struct imsm_disk
*disk
;
9721 int second_map_created
= 0;
9723 for (; u
; u
= u
->next
) {
9724 victim
= get_imsm_disk_idx(dev
, u
->slot
, MAP_X
);
9729 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
9734 pr_err("error: imsm_activate_spare passed an unknown disk (index: %d)\n",
9739 /* count failures (excluding rebuilds and the victim)
9740 * to determine map[0] state
9743 for (i
= 0; i
< map
->num_members
; i
++) {
9746 disk
= get_imsm_disk(super
,
9747 get_imsm_disk_idx(dev
, i
, MAP_X
));
9748 if (!disk
|| is_failed(disk
))
9752 /* adding a pristine spare, assign a new index */
9753 if (dl
->index
< 0) {
9754 dl
->index
= super
->anchor
->num_disks
;
9755 super
->anchor
->num_disks
++;
9758 disk
->status
|= CONFIGURED_DISK
;
9759 disk
->status
&= ~SPARE_DISK
;
9762 to_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
9763 if (!second_map_created
) {
9764 second_map_created
= 1;
9765 map
->map_state
= IMSM_T_STATE_DEGRADED
;
9766 migrate(dev
, super
, to_state
, MIGR_REBUILD
);
9768 map
->map_state
= to_state
;
9769 migr_map
= get_imsm_map(dev
, MAP_1
);
9770 set_imsm_ord_tbl_ent(map
, u
->slot
, dl
->index
);
9771 set_imsm_ord_tbl_ent(migr_map
, u
->slot
,
9772 dl
->index
| IMSM_ORD_REBUILD
);
9774 /* update the family_num to mark a new container
9775 * generation, being careful to record the existing
9776 * family_num in orig_family_num to clean up after
9777 * earlier mdadm versions that neglected to set it.
9779 if (mpb
->orig_family_num
== 0)
9780 mpb
->orig_family_num
= mpb
->family_num
;
9781 mpb
->family_num
+= super
->random
;
9783 /* count arrays using the victim in the metadata */
9785 for (a
= active_array
; a
; a
= a
->next
) {
9786 int dev_idx
= a
->info
.container_member
;
9788 if (get_disk_slot_in_dev(super
, dev_idx
, victim
) >= 0)
9792 /* delete the victim if it is no longer being
9798 /* We know that 'manager' isn't touching anything,
9799 * so it is safe to delete
9801 for (dlp
= &super
->disks
; *dlp
; dlp
= &(*dlp
)->next
)
9802 if ((*dlp
)->index
== victim
)
9805 /* victim may be on the missing list */
9807 for (dlp
= &super
->missing
; *dlp
;
9808 dlp
= &(*dlp
)->next
)
9809 if ((*dlp
)->index
== victim
)
9811 imsm_delete(super
, dlp
, victim
);
9818 static int apply_reshape_container_disks_update(struct imsm_update_reshape
*u
,
9819 struct intel_super
*super
,
9822 struct dl
*new_disk
;
9823 struct intel_dev
*id
;
9825 int delta_disks
= u
->new_raid_disks
- u
->old_raid_disks
;
9826 int disk_count
= u
->old_raid_disks
;
9827 void **tofree
= NULL
;
9828 int devices_to_reshape
= 1;
9829 struct imsm_super
*mpb
= super
->anchor
;
9831 unsigned int dev_id
;
9833 dprintf("(enter)\n");
9835 /* enable spares to use in array */
9836 for (i
= 0; i
< delta_disks
; i
++) {
9837 new_disk
= get_disk_super(super
,
9838 major(u
->new_disks
[i
]),
9839 minor(u
->new_disks
[i
]));
9840 dprintf("imsm: new disk for reshape is: %i:%i (%p, index = %i)\n",
9841 major(u
->new_disks
[i
]), minor(u
->new_disks
[i
]),
9842 new_disk
, new_disk
->index
);
9843 if (new_disk
== NULL
||
9844 (new_disk
->index
>= 0 &&
9845 new_disk
->index
< u
->old_raid_disks
))
9846 goto update_reshape_exit
;
9847 new_disk
->index
= disk_count
++;
9848 /* slot to fill in autolayout
9850 new_disk
->raiddisk
= new_disk
->index
;
9851 new_disk
->disk
.status
|=
9853 new_disk
->disk
.status
&= ~SPARE_DISK
;
9856 dprintf("imsm: volume set mpb->num_raid_devs = %i\n",
9857 mpb
->num_raid_devs
);
9858 /* manage changes in volume
9860 for (dev_id
= 0; dev_id
< mpb
->num_raid_devs
; dev_id
++) {
9861 void **sp
= *space_list
;
9862 struct imsm_dev
*newdev
;
9863 struct imsm_map
*newmap
, *oldmap
;
9865 for (id
= super
->devlist
; id
; id
= id
->next
) {
9866 if (id
->index
== dev_id
)
9875 /* Copy the dev, but not (all of) the map */
9876 memcpy(newdev
, id
->dev
, sizeof(*newdev
));
9877 oldmap
= get_imsm_map(id
->dev
, MAP_0
);
9878 newmap
= get_imsm_map(newdev
, MAP_0
);
9879 /* Copy the current map */
9880 memcpy(newmap
, oldmap
, sizeof_imsm_map(oldmap
));
9881 /* update one device only
9883 if (devices_to_reshape
) {
9884 dprintf("imsm: modifying subdev: %i\n",
9886 devices_to_reshape
--;
9887 newdev
->vol
.migr_state
= 1;
9888 set_vol_curr_migr_unit(newdev
, 0);
9889 set_migr_type(newdev
, MIGR_GEN_MIGR
);
9890 newmap
->num_members
= u
->new_raid_disks
;
9891 for (i
= 0; i
< delta_disks
; i
++) {
9892 set_imsm_ord_tbl_ent(newmap
,
9893 u
->old_raid_disks
+ i
,
9894 u
->old_raid_disks
+ i
);
9896 /* New map is correct, now need to save old map
9898 newmap
= get_imsm_map(newdev
, MAP_1
);
9899 memcpy(newmap
, oldmap
, sizeof_imsm_map(oldmap
));
9901 imsm_set_array_size(newdev
, -1);
9904 sp
= (void **)id
->dev
;
9909 /* Clear migration record */
9910 memset(super
->migr_rec
, 0, sizeof(struct migr_record
));
9913 *space_list
= tofree
;
9916 update_reshape_exit
:
9921 static int apply_takeover_update(struct imsm_update_takeover
*u
,
9922 struct intel_super
*super
,
9925 struct imsm_dev
*dev
= NULL
;
9926 struct intel_dev
*dv
;
9927 struct imsm_dev
*dev_new
;
9928 struct imsm_map
*map
;
9932 for (dv
= super
->devlist
; dv
; dv
= dv
->next
)
9933 if (dv
->index
== (unsigned int)u
->subarray
) {
9941 map
= get_imsm_map(dev
, MAP_0
);
9943 if (u
->direction
== R10_TO_R0
) {
9944 /* Number of failed disks must be half of initial disk number */
9945 if (imsm_count_failed(super
, dev
, MAP_0
) !=
9946 (map
->num_members
/ 2))
9949 /* iterate through devices to mark removed disks as spare */
9950 for (dm
= super
->disks
; dm
; dm
= dm
->next
) {
9951 if (dm
->disk
.status
& FAILED_DISK
) {
9952 int idx
= dm
->index
;
9953 /* update indexes on the disk list */
9954 /* FIXME this loop-with-the-loop looks wrong, I'm not convinced
9955 the index values will end up being correct.... NB */
9956 for (du
= super
->disks
; du
; du
= du
->next
)
9957 if (du
->index
> idx
)
9959 /* mark as spare disk */
9964 map
->num_members
/= map
->num_domains
;
9965 map
->map_state
= IMSM_T_STATE_NORMAL
;
9966 map
->raid_level
= 0;
9967 set_num_domains(map
);
9968 update_num_data_stripes(map
, imsm_dev_size(dev
));
9969 map
->failed_disk_num
= -1;
9972 if (u
->direction
== R0_TO_R10
) {
9975 /* update slots in current disk list */
9976 for (dm
= super
->disks
; dm
; dm
= dm
->next
) {
9980 /* create new *missing* disks */
9981 for (i
= 0; i
< map
->num_members
; i
++) {
9982 space
= *space_list
;
9985 *space_list
= *space
;
9987 memcpy(du
, super
->disks
, sizeof(*du
));
9991 du
->index
= (i
* 2) + 1;
9992 sprintf((char *)du
->disk
.serial
,
9993 " MISSING_%d", du
->index
);
9994 sprintf((char *)du
->serial
,
9995 "MISSING_%d", du
->index
);
9996 du
->next
= super
->missing
;
9997 super
->missing
= du
;
9999 /* create new dev and map */
10000 space
= *space_list
;
10003 *space_list
= *space
;
10004 dev_new
= (void *)space
;
10005 memcpy(dev_new
, dev
, sizeof(*dev
));
10006 /* update new map */
10007 map
= get_imsm_map(dev_new
, MAP_0
);
10009 map
->map_state
= IMSM_T_STATE_DEGRADED
;
10010 map
->raid_level
= 1;
10011 set_num_domains(map
);
10012 map
->num_members
= map
->num_members
* map
->num_domains
;
10013 update_num_data_stripes(map
, imsm_dev_size(dev
));
10015 /* replace dev<->dev_new */
10018 /* update disk order table */
10019 for (du
= super
->disks
; du
; du
= du
->next
)
10020 if (du
->index
>= 0)
10021 set_imsm_ord_tbl_ent(map
, du
->index
, du
->index
);
10022 for (du
= super
->missing
; du
; du
= du
->next
)
10023 if (du
->index
>= 0) {
10024 set_imsm_ord_tbl_ent(map
, du
->index
, du
->index
);
10025 mark_missing(super
, dv
->dev
, &du
->disk
, du
->index
);
10031 static void imsm_process_update(struct supertype
*st
,
10032 struct metadata_update
*update
)
10035 * crack open the metadata_update envelope to find the update record
10036 * update can be one of:
10037 * update_reshape_container_disks - all the arrays in the container
10038 * are being reshaped to have more devices. We need to mark
10039 * the arrays for general migration and convert selected spares
10040 * into active devices.
10041 * update_activate_spare - a spare device has replaced a failed
10042 * device in an array, update the disk_ord_tbl. If this disk is
10043 * present in all member arrays then also clear the SPARE_DISK
10045 * update_create_array
10046 * update_kill_array
10047 * update_rename_array
10048 * update_add_remove_disk
10050 struct intel_super
*super
= st
->sb
;
10051 struct imsm_super
*mpb
;
10052 enum imsm_update_type type
= *(enum imsm_update_type
*) update
->buf
;
10054 /* update requires a larger buf but the allocation failed */
10055 if (super
->next_len
&& !super
->next_buf
) {
10056 super
->next_len
= 0;
10060 if (super
->next_buf
) {
10061 memcpy(super
->next_buf
, super
->buf
, super
->len
);
10063 super
->len
= super
->next_len
;
10064 super
->buf
= super
->next_buf
;
10066 super
->next_len
= 0;
10067 super
->next_buf
= NULL
;
10070 mpb
= super
->anchor
;
10073 case update_general_migration_checkpoint
: {
10074 struct intel_dev
*id
;
10075 struct imsm_update_general_migration_checkpoint
*u
=
10076 (void *)update
->buf
;
10078 dprintf("called for update_general_migration_checkpoint\n");
10080 /* find device under general migration */
10081 for (id
= super
->devlist
; id
; id
= id
->next
) {
10082 if (is_gen_migration(id
->dev
)) {
10083 set_vol_curr_migr_unit(id
->dev
,
10084 u
->curr_migr_unit
);
10085 super
->updates_pending
++;
10090 case update_takeover
: {
10091 struct imsm_update_takeover
*u
= (void *)update
->buf
;
10092 if (apply_takeover_update(u
, super
, &update
->space_list
)) {
10093 imsm_update_version_info(super
);
10094 super
->updates_pending
++;
10099 case update_reshape_container_disks
: {
10100 struct imsm_update_reshape
*u
= (void *)update
->buf
;
10101 if (apply_reshape_container_disks_update(
10102 u
, super
, &update
->space_list
))
10103 super
->updates_pending
++;
10106 case update_reshape_migration
: {
10107 struct imsm_update_reshape_migration
*u
= (void *)update
->buf
;
10108 if (apply_reshape_migration_update(
10109 u
, super
, &update
->space_list
))
10110 super
->updates_pending
++;
10113 case update_size_change
: {
10114 struct imsm_update_size_change
*u
= (void *)update
->buf
;
10115 if (apply_size_change_update(u
, super
))
10116 super
->updates_pending
++;
10119 case update_activate_spare
: {
10120 struct imsm_update_activate_spare
*u
= (void *) update
->buf
;
10122 if (prepare_spare_to_activate(st
, u
) &&
10123 apply_update_activate_spare(u
, super
, st
->arrays
))
10124 super
->updates_pending
++;
10127 case update_create_array
: {
10128 /* someone wants to create a new array, we need to be aware of
10129 * a few races/collisions:
10130 * 1/ 'Create' called by two separate instances of mdadm
10131 * 2/ 'Create' versus 'activate_spare': mdadm has chosen
10132 * devices that have since been assimilated via
10134 * In the event this update can not be carried out mdadm will
10135 * (FIX ME) notice that its update did not take hold.
10137 struct imsm_update_create_array
*u
= (void *) update
->buf
;
10138 struct intel_dev
*dv
;
10139 struct imsm_dev
*dev
;
10140 struct imsm_map
*map
, *new_map
;
10141 unsigned long long start
, end
;
10142 unsigned long long new_start
, new_end
;
10144 struct disk_info
*inf
;
10147 /* handle racing creates: first come first serve */
10148 if (u
->dev_idx
< mpb
->num_raid_devs
) {
10149 dprintf("subarray %d already defined\n", u
->dev_idx
);
10153 /* check update is next in sequence */
10154 if (u
->dev_idx
!= mpb
->num_raid_devs
) {
10155 dprintf("can not create array %d expected index %d\n",
10156 u
->dev_idx
, mpb
->num_raid_devs
);
10160 new_map
= get_imsm_map(&u
->dev
, MAP_0
);
10161 new_start
= pba_of_lba0(new_map
);
10162 new_end
= new_start
+ per_dev_array_size(new_map
);
10163 inf
= get_disk_info(u
);
10165 /* handle activate_spare versus create race:
10166 * check to make sure that overlapping arrays do not include
10167 * overalpping disks
10169 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
10170 dev
= get_imsm_dev(super
, i
);
10171 map
= get_imsm_map(dev
, MAP_0
);
10172 start
= pba_of_lba0(map
);
10173 end
= start
+ per_dev_array_size(map
);
10174 if ((new_start
>= start
&& new_start
<= end
) ||
10175 (start
>= new_start
&& start
<= new_end
))
10180 if (disks_overlap(super
, i
, u
)) {
10181 dprintf("arrays overlap\n");
10186 /* check that prepare update was successful */
10187 if (!update
->space
) {
10188 dprintf("prepare update failed\n");
10192 /* check that all disks are still active before committing
10193 * changes. FIXME: could we instead handle this by creating a
10194 * degraded array? That's probably not what the user expects,
10195 * so better to drop this update on the floor.
10197 for (i
= 0; i
< new_map
->num_members
; i
++) {
10198 dl
= serial_to_dl(inf
[i
].serial
, super
);
10200 dprintf("disk disappeared\n");
10205 super
->updates_pending
++;
10207 /* convert spares to members and fixup ord_tbl */
10208 for (i
= 0; i
< new_map
->num_members
; i
++) {
10209 dl
= serial_to_dl(inf
[i
].serial
, super
);
10210 if (dl
->index
== -1) {
10211 dl
->index
= mpb
->num_disks
;
10213 dl
->disk
.status
|= CONFIGURED_DISK
;
10214 dl
->disk
.status
&= ~SPARE_DISK
;
10216 set_imsm_ord_tbl_ent(new_map
, i
, dl
->index
);
10219 dv
= update
->space
;
10221 update
->space
= NULL
;
10222 imsm_copy_dev(dev
, &u
->dev
);
10223 dv
->index
= u
->dev_idx
;
10224 dv
->next
= super
->devlist
;
10225 super
->devlist
= dv
;
10226 mpb
->num_raid_devs
++;
10228 imsm_update_version_info(super
);
10231 /* mdmon knows how to release update->space, but not
10232 * ((struct intel_dev *) update->space)->dev
10234 if (update
->space
) {
10235 dv
= update
->space
;
10240 case update_kill_array
: {
10241 struct imsm_update_kill_array
*u
= (void *) update
->buf
;
10242 int victim
= u
->dev_idx
;
10243 struct active_array
*a
;
10244 struct intel_dev
**dp
;
10246 /* sanity check that we are not affecting the uuid of
10247 * active arrays, or deleting an active array
10249 * FIXME when immutable ids are available, but note that
10250 * we'll also need to fixup the invalidated/active
10251 * subarray indexes in mdstat
10253 for (a
= st
->arrays
; a
; a
= a
->next
)
10254 if (a
->info
.container_member
>= victim
)
10256 /* by definition if mdmon is running at least one array
10257 * is active in the container, so checking
10258 * mpb->num_raid_devs is just extra paranoia
10260 if (a
|| mpb
->num_raid_devs
== 1 || victim
>= super
->anchor
->num_raid_devs
) {
10261 dprintf("failed to delete subarray-%d\n", victim
);
10265 for (dp
= &super
->devlist
; *dp
;)
10266 if ((*dp
)->index
== (unsigned)super
->current_vol
) {
10269 if ((*dp
)->index
> (unsigned)victim
)
10273 mpb
->num_raid_devs
--;
10274 super
->updates_pending
++;
10277 case update_rename_array
: {
10278 struct imsm_update_rename_array
*u
= (void *) update
->buf
;
10279 char name
[MAX_RAID_SERIAL_LEN
+1];
10280 int target
= u
->dev_idx
;
10281 struct active_array
*a
;
10282 struct imsm_dev
*dev
;
10284 /* sanity check that we are not affecting the uuid of
10287 memset(name
, 0, sizeof(name
));
10288 snprintf(name
, MAX_RAID_SERIAL_LEN
, "%s", (char *) u
->name
);
10289 name
[MAX_RAID_SERIAL_LEN
] = '\0';
10290 for (a
= st
->arrays
; a
; a
= a
->next
)
10291 if (a
->info
.container_member
== target
)
10293 dev
= get_imsm_dev(super
, u
->dev_idx
);
10295 if (a
|| !dev
|| imsm_is_name_allowed(super
, name
, 0) == false) {
10296 dprintf("failed to rename subarray-%d\n", target
);
10300 memcpy(dev
->volume
, name
, MAX_RAID_SERIAL_LEN
);
10301 super
->updates_pending
++;
10304 case update_add_remove_disk
: {
10305 /* we may be able to repair some arrays if disks are
10306 * being added, check the status of add_remove_disk
10307 * if discs has been added.
10309 if (add_remove_disk_update(super
)) {
10310 struct active_array
*a
;
10312 super
->updates_pending
++;
10313 for (a
= st
->arrays
; a
; a
= a
->next
)
10314 a
->check_degraded
= 1;
10318 case update_prealloc_badblocks_mem
:
10320 case update_rwh_policy
: {
10321 struct imsm_update_rwh_policy
*u
= (void *)update
->buf
;
10322 int target
= u
->dev_idx
;
10323 struct imsm_dev
*dev
= get_imsm_dev(super
, target
);
10325 if (dev
->rwh_policy
!= u
->new_policy
) {
10326 dev
->rwh_policy
= u
->new_policy
;
10327 super
->updates_pending
++;
10332 pr_err("error: unsupported process update type:(type: %d)\n", type
);
10336 static struct mdinfo
*get_spares_for_grow(struct supertype
*st
);
10338 static int imsm_prepare_update(struct supertype
*st
,
10339 struct metadata_update
*update
)
10342 * Allocate space to hold new disk entries, raid-device entries or a new
10343 * mpb if necessary. The manager synchronously waits for updates to
10344 * complete in the monitor, so new mpb buffers allocated here can be
10345 * integrated by the monitor thread without worrying about live pointers
10346 * in the manager thread.
10348 enum imsm_update_type type
;
10349 struct intel_super
*super
= st
->sb
;
10350 unsigned int sector_size
= super
->sector_size
;
10351 struct imsm_super
*mpb
= super
->anchor
;
10355 if (update
->len
< (int)sizeof(type
))
10358 type
= *(enum imsm_update_type
*) update
->buf
;
10361 case update_general_migration_checkpoint
:
10362 if (update
->len
< (int)sizeof(struct imsm_update_general_migration_checkpoint
))
10364 dprintf("called for update_general_migration_checkpoint\n");
10366 case update_takeover
: {
10367 struct imsm_update_takeover
*u
= (void *)update
->buf
;
10368 if (update
->len
< (int)sizeof(*u
))
10370 if (u
->direction
== R0_TO_R10
) {
10371 void **tail
= (void **)&update
->space_list
;
10372 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->subarray
);
10373 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
10374 int num_members
= map
->num_members
;
10377 /* allocate memory for added disks */
10378 for (i
= 0; i
< num_members
; i
++) {
10379 size
= sizeof(struct dl
);
10380 space
= xmalloc(size
);
10385 /* allocate memory for new device */
10386 size
= sizeof_imsm_dev(super
->devlist
->dev
, 0) +
10387 (num_members
* sizeof(__u32
));
10388 space
= xmalloc(size
);
10392 len
= disks_to_mpb_size(num_members
* 2);
10397 case update_reshape_container_disks
: {
10398 /* Every raid device in the container is about to
10399 * gain some more devices, and we will enter a
10401 * So each 'imsm_map' will be bigger, and the imsm_vol
10402 * will now hold 2 of them.
10403 * Thus we need new 'struct imsm_dev' allocations sized
10404 * as sizeof_imsm_dev but with more devices in both maps.
10406 struct imsm_update_reshape
*u
= (void *)update
->buf
;
10407 struct intel_dev
*dl
;
10408 void **space_tail
= (void**)&update
->space_list
;
10410 if (update
->len
< (int)sizeof(*u
))
10413 dprintf("for update_reshape\n");
10415 for (dl
= super
->devlist
; dl
; dl
= dl
->next
) {
10416 int size
= sizeof_imsm_dev(dl
->dev
, 1);
10418 if (u
->new_raid_disks
> u
->old_raid_disks
)
10419 size
+= sizeof(__u32
)*2*
10420 (u
->new_raid_disks
- u
->old_raid_disks
);
10424 *space_tail
= NULL
;
10427 len
= disks_to_mpb_size(u
->new_raid_disks
);
10428 dprintf("New anchor length is %llu\n", (unsigned long long)len
);
10431 case update_reshape_migration
: {
10432 /* for migration level 0->5 we need to add disks
10433 * so the same as for container operation we will copy
10434 * device to the bigger location.
10435 * in memory prepared device and new disk area are prepared
10436 * for usage in process update
10438 struct imsm_update_reshape_migration
*u
= (void *)update
->buf
;
10439 struct intel_dev
*id
;
10440 void **space_tail
= (void **)&update
->space_list
;
10443 int current_level
= -1;
10445 if (update
->len
< (int)sizeof(*u
))
10448 dprintf("for update_reshape\n");
10450 /* add space for bigger array in update
10452 for (id
= super
->devlist
; id
; id
= id
->next
) {
10453 if (id
->index
== (unsigned)u
->subdev
) {
10454 size
= sizeof_imsm_dev(id
->dev
, 1);
10455 if (u
->new_raid_disks
> u
->old_raid_disks
)
10456 size
+= sizeof(__u32
)*2*
10457 (u
->new_raid_disks
- u
->old_raid_disks
);
10461 *space_tail
= NULL
;
10465 if (update
->space_list
== NULL
)
10468 /* add space for disk in update
10470 size
= sizeof(struct dl
);
10474 *space_tail
= NULL
;
10476 /* add spare device to update
10478 for (id
= super
->devlist
; id
; id
= id
->next
)
10479 if (id
->index
== (unsigned)u
->subdev
) {
10480 struct imsm_dev
*dev
;
10481 struct imsm_map
*map
;
10483 dev
= get_imsm_dev(super
, u
->subdev
);
10484 map
= get_imsm_map(dev
, MAP_0
);
10485 current_level
= map
->raid_level
;
10488 if (u
->new_level
== 5 && u
->new_level
!= current_level
) {
10489 struct mdinfo
*spares
;
10491 spares
= get_spares_for_grow(st
);
10494 struct mdinfo
*dev
;
10496 dev
= spares
->devs
;
10499 makedev(dev
->disk
.major
,
10501 dl
= get_disk_super(super
,
10504 dl
->index
= u
->old_raid_disks
;
10507 sysfs_free(spares
);
10510 len
= disks_to_mpb_size(u
->new_raid_disks
);
10511 dprintf("New anchor length is %llu\n", (unsigned long long)len
);
10514 case update_size_change
: {
10515 if (update
->len
< (int)sizeof(struct imsm_update_size_change
))
10519 case update_activate_spare
: {
10520 if (update
->len
< (int)sizeof(struct imsm_update_activate_spare
))
10524 case update_create_array
: {
10525 struct imsm_update_create_array
*u
= (void *) update
->buf
;
10526 struct intel_dev
*dv
;
10527 struct imsm_dev
*dev
= &u
->dev
;
10528 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
10530 struct disk_info
*inf
;
10534 if (update
->len
< (int)sizeof(*u
))
10537 inf
= get_disk_info(u
);
10538 len
= sizeof_imsm_dev(dev
, 1);
10539 /* allocate a new super->devlist entry */
10540 dv
= xmalloc(sizeof(*dv
));
10541 dv
->dev
= xmalloc(len
);
10542 update
->space
= dv
;
10544 /* count how many spares will be converted to members */
10545 for (i
= 0; i
< map
->num_members
; i
++) {
10546 dl
= serial_to_dl(inf
[i
].serial
, super
);
10548 /* hmm maybe it failed?, nothing we can do about
10553 if (count_memberships(dl
, super
) == 0)
10556 len
+= activate
* sizeof(struct imsm_disk
);
10559 case update_kill_array
: {
10560 if (update
->len
< (int)sizeof(struct imsm_update_kill_array
))
10564 case update_rename_array
: {
10565 if (update
->len
< (int)sizeof(struct imsm_update_rename_array
))
10569 case update_add_remove_disk
:
10570 /* no update->len needed */
10572 case update_prealloc_badblocks_mem
:
10573 super
->extra_space
+= sizeof(struct bbm_log
) -
10574 get_imsm_bbm_log_size(super
->bbm_log
);
10576 case update_rwh_policy
: {
10577 if (update
->len
< (int)sizeof(struct imsm_update_rwh_policy
))
10585 /* check if we need a larger metadata buffer */
10586 if (super
->next_buf
)
10587 buf_len
= super
->next_len
;
10589 buf_len
= super
->len
;
10591 if (__le32_to_cpu(mpb
->mpb_size
) + super
->extra_space
+ len
> buf_len
) {
10592 /* ok we need a larger buf than what is currently allocated
10593 * if this allocation fails process_update will notice that
10594 * ->next_len is set and ->next_buf is NULL
10596 buf_len
= ROUND_UP(__le32_to_cpu(mpb
->mpb_size
) +
10597 super
->extra_space
+ len
, sector_size
);
10598 if (super
->next_buf
)
10599 free(super
->next_buf
);
10601 super
->next_len
= buf_len
;
10602 if (posix_memalign(&super
->next_buf
, sector_size
, buf_len
) == 0)
10603 memset(super
->next_buf
, 0, buf_len
);
10605 super
->next_buf
= NULL
;
10610 /* must be called while manager is quiesced */
10611 static void imsm_delete(struct intel_super
*super
, struct dl
**dlp
, unsigned index
)
10613 struct imsm_super
*mpb
= super
->anchor
;
10615 struct imsm_dev
*dev
;
10616 struct imsm_map
*map
;
10617 unsigned int i
, j
, num_members
;
10618 __u32 ord
, ord_map0
;
10619 struct bbm_log
*log
= super
->bbm_log
;
10621 dprintf("deleting device[%d] from imsm_super\n", index
);
10623 /* shift all indexes down one */
10624 for (iter
= super
->disks
; iter
; iter
= iter
->next
)
10625 if (iter
->index
> (int)index
)
10627 for (iter
= super
->missing
; iter
; iter
= iter
->next
)
10628 if (iter
->index
> (int)index
)
10631 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
10632 dev
= get_imsm_dev(super
, i
);
10633 map
= get_imsm_map(dev
, MAP_0
);
10634 num_members
= map
->num_members
;
10635 for (j
= 0; j
< num_members
; j
++) {
10636 /* update ord entries being careful not to propagate
10637 * ord-flags to the first map
10639 ord
= get_imsm_ord_tbl_ent(dev
, j
, MAP_X
);
10640 ord_map0
= get_imsm_ord_tbl_ent(dev
, j
, MAP_0
);
10642 if (ord_to_idx(ord
) <= index
)
10645 map
= get_imsm_map(dev
, MAP_0
);
10646 set_imsm_ord_tbl_ent(map
, j
, ord_map0
- 1);
10647 map
= get_imsm_map(dev
, MAP_1
);
10649 set_imsm_ord_tbl_ent(map
, j
, ord
- 1);
10653 for (i
= 0; i
< log
->entry_count
; i
++) {
10654 struct bbm_log_entry
*entry
= &log
->marked_block_entries
[i
];
10656 if (entry
->disk_ordinal
<= index
)
10658 entry
->disk_ordinal
--;
10662 super
->updates_pending
++;
10664 struct dl
*dl
= *dlp
;
10666 *dlp
= (*dlp
)->next
;
10667 __free_imsm_disk(dl
, 1);
10671 static int imsm_get_allowed_degradation(int level
, int raid_disks
,
10672 struct intel_super
*super
,
10673 struct imsm_dev
*dev
)
10679 struct imsm_map
*map
;
10682 ret_val
= raid_disks
/2;
10683 /* check map if all disks pairs not failed
10686 map
= get_imsm_map(dev
, MAP_0
);
10687 for (i
= 0; i
< ret_val
; i
++) {
10688 int degradation
= 0;
10689 if (get_imsm_disk(super
, i
) == NULL
)
10691 if (get_imsm_disk(super
, i
+ 1) == NULL
)
10693 if (degradation
== 2)
10696 map
= get_imsm_map(dev
, MAP_1
);
10697 /* if there is no second map
10698 * result can be returned
10702 /* check degradation in second map
10704 for (i
= 0; i
< ret_val
; i
++) {
10705 int degradation
= 0;
10706 if (get_imsm_disk(super
, i
) == NULL
)
10708 if (get_imsm_disk(super
, i
+ 1) == NULL
)
10710 if (degradation
== 2)
10724 /*******************************************************************************
10725 * Function: validate_container_imsm
10726 * Description: This routine validates container after assemble,
10727 * eg. if devices in container are under the same controller.
10730 * info : linked list with info about devices used in array
10734 ******************************************************************************/
10735 int validate_container_imsm(struct mdinfo
*info
)
10737 if (check_no_platform())
10740 struct sys_dev
*idev
;
10741 struct sys_dev
*hba
= NULL
;
10742 struct sys_dev
*intel_devices
= find_intel_devices();
10743 char *dev_path
= devt_to_devpath(makedev(info
->disk
.major
,
10744 info
->disk
.minor
), 1, NULL
);
10746 for (idev
= intel_devices
; idev
; idev
= idev
->next
) {
10747 if (dev_path
&& strstr(dev_path
, idev
->path
)) {
10756 pr_err("WARNING - Cannot detect HBA for device %s!\n",
10757 devid2kname(makedev(info
->disk
.major
, info
->disk
.minor
)));
10761 const struct imsm_orom
*orom
= get_orom_by_device_id(hba
->dev_id
);
10762 struct mdinfo
*dev
;
10764 for (dev
= info
->next
; dev
; dev
= dev
->next
) {
10765 dev_path
= devt_to_devpath(makedev(dev
->disk
.major
,
10766 dev
->disk
.minor
), 1, NULL
);
10768 struct sys_dev
*hba2
= NULL
;
10769 for (idev
= intel_devices
; idev
; idev
= idev
->next
) {
10770 if (dev_path
&& strstr(dev_path
, idev
->path
)) {
10778 const struct imsm_orom
*orom2
= hba2
== NULL
? NULL
:
10779 get_orom_by_device_id(hba2
->dev_id
);
10781 if (hba2
&& hba
->type
!= hba2
->type
) {
10782 pr_err("WARNING - HBAs of devices do not match %s != %s\n",
10783 get_sys_dev_type(hba
->type
), get_sys_dev_type(hba2
->type
));
10787 if (orom
!= orom2
) {
10788 pr_err("WARNING - IMSM container assembled with disks under different HBAs!\n"
10789 " This operation is not supported and can lead to data loss.\n");
10794 pr_err("WARNING - IMSM container assembled with disks under HBAs without IMSM platform support!\n"
10795 " This operation is not supported and can lead to data loss.\n");
10803 /*******************************************************************************
10804 * Function: imsm_record_badblock
10805 * Description: This routine stores new bad block record in BBM log
10808 * a : array containing a bad block
10809 * slot : disk number containing a bad block
10810 * sector : bad block sector
10811 * length : bad block sectors range
10815 ******************************************************************************/
10816 static int imsm_record_badblock(struct active_array
*a
, int slot
,
10817 unsigned long long sector
, int length
)
10819 struct intel_super
*super
= a
->container
->sb
;
10823 ord
= imsm_disk_slot_to_ord(a
, slot
);
10827 ret
= record_new_badblock(super
->bbm_log
, ord_to_idx(ord
), sector
,
10830 super
->updates_pending
++;
10834 /*******************************************************************************
10835 * Function: imsm_clear_badblock
10836 * Description: This routine clears bad block record from BBM log
10839 * a : array containing a bad block
10840 * slot : disk number containing a bad block
10841 * sector : bad block sector
10842 * length : bad block sectors range
10846 ******************************************************************************/
10847 static int imsm_clear_badblock(struct active_array
*a
, int slot
,
10848 unsigned long long sector
, int length
)
10850 struct intel_super
*super
= a
->container
->sb
;
10854 ord
= imsm_disk_slot_to_ord(a
, slot
);
10858 ret
= clear_badblock(super
->bbm_log
, ord_to_idx(ord
), sector
, length
);
10860 super
->updates_pending
++;
10864 /*******************************************************************************
10865 * Function: imsm_get_badblocks
10866 * Description: This routine get list of bad blocks for an array
10870 * slot : disk number
10872 * bb : structure containing bad blocks
10874 ******************************************************************************/
10875 static struct md_bb
*imsm_get_badblocks(struct active_array
*a
, int slot
)
10877 int inst
= a
->info
.container_member
;
10878 struct intel_super
*super
= a
->container
->sb
;
10879 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
10880 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
10883 ord
= imsm_disk_slot_to_ord(a
, slot
);
10887 get_volume_badblocks(super
->bbm_log
, ord_to_idx(ord
), pba_of_lba0(map
),
10888 per_dev_array_size(map
), &super
->bb
);
10892 /*******************************************************************************
10893 * Function: examine_badblocks_imsm
10894 * Description: Prints list of bad blocks on a disk to the standard output
10897 * st : metadata handler
10898 * fd : open file descriptor for device
10899 * devname : device name
10903 ******************************************************************************/
10904 static int examine_badblocks_imsm(struct supertype
*st
, int fd
, char *devname
)
10906 struct intel_super
*super
= st
->sb
;
10907 struct bbm_log
*log
= super
->bbm_log
;
10908 struct dl
*d
= NULL
;
10911 for (d
= super
->disks
; d
; d
= d
->next
) {
10912 if (strcmp(d
->devname
, devname
) == 0)
10916 if ((d
== NULL
) || (d
->index
< 0)) { /* serial mismatch probably */
10917 pr_err("%s doesn't appear to be part of a raid array\n",
10924 struct bbm_log_entry
*entry
= &log
->marked_block_entries
[0];
10926 for (i
= 0; i
< log
->entry_count
; i
++) {
10927 if (entry
[i
].disk_ordinal
== d
->index
) {
10928 unsigned long long sector
= __le48_to_cpu(
10929 &entry
[i
].defective_block_start
);
10930 int cnt
= entry
[i
].marked_count
+ 1;
10933 printf("Bad-blocks on %s:\n", devname
);
10937 printf("%20llu for %d sectors\n", sector
, cnt
);
10943 printf("No bad-blocks list configured on %s\n", devname
);
10947 /*******************************************************************************
10948 * Function: init_migr_record_imsm
10949 * Description: Function inits imsm migration record
10951 * super : imsm internal array info
10952 * dev : device under migration
10953 * info : general array info to find the smallest device
10956 ******************************************************************************/
10957 void init_migr_record_imsm(struct supertype
*st
, struct imsm_dev
*dev
,
10958 struct mdinfo
*info
)
10960 struct intel_super
*super
= st
->sb
;
10961 struct migr_record
*migr_rec
= super
->migr_rec
;
10962 int new_data_disks
;
10963 unsigned long long dsize
, dev_sectors
;
10964 long long unsigned min_dev_sectors
= -1LLU;
10965 struct imsm_map
*map_dest
= get_imsm_map(dev
, MAP_0
);
10966 struct imsm_map
*map_src
= get_imsm_map(dev
, MAP_1
);
10967 unsigned long long num_migr_units
;
10968 unsigned long long array_blocks
;
10969 struct dl
*dl_disk
= NULL
;
10971 memset(migr_rec
, 0, sizeof(struct migr_record
));
10972 migr_rec
->family_num
= __cpu_to_le32(super
->anchor
->family_num
);
10974 /* only ascending reshape supported now */
10975 migr_rec
->ascending_migr
= __cpu_to_le32(1);
10977 migr_rec
->dest_depth_per_unit
= GEN_MIGR_AREA_SIZE
/
10978 max(map_dest
->blocks_per_strip
, map_src
->blocks_per_strip
);
10979 migr_rec
->dest_depth_per_unit
*=
10980 max(map_dest
->blocks_per_strip
, map_src
->blocks_per_strip
);
10981 new_data_disks
= imsm_num_data_members(map_dest
);
10982 migr_rec
->blocks_per_unit
=
10983 __cpu_to_le32(migr_rec
->dest_depth_per_unit
* new_data_disks
);
10984 migr_rec
->dest_depth_per_unit
=
10985 __cpu_to_le32(migr_rec
->dest_depth_per_unit
);
10986 array_blocks
= info
->component_size
* new_data_disks
;
10988 array_blocks
/ __le32_to_cpu(migr_rec
->blocks_per_unit
);
10990 if (array_blocks
% __le32_to_cpu(migr_rec
->blocks_per_unit
))
10992 set_num_migr_units(migr_rec
, num_migr_units
);
10994 migr_rec
->post_migr_vol_cap
= dev
->size_low
;
10995 migr_rec
->post_migr_vol_cap_hi
= dev
->size_high
;
10997 /* Find the smallest dev */
10998 for (dl_disk
= super
->disks
; dl_disk
; dl_disk
= dl_disk
->next
) {
10999 /* ignore spares in container */
11000 if (dl_disk
->index
< 0)
11002 get_dev_size(dl_disk
->fd
, NULL
, &dsize
);
11003 dev_sectors
= dsize
/ 512;
11004 if (dev_sectors
< min_dev_sectors
)
11005 min_dev_sectors
= dev_sectors
;
11007 set_migr_chkp_area_pba(migr_rec
, min_dev_sectors
-
11008 RAID_DISK_RESERVED_BLOCKS_IMSM_HI
);
11010 write_imsm_migr_rec(st
);
11015 /*******************************************************************************
11016 * Function: save_backup_imsm
11017 * Description: Function saves critical data stripes to Migration Copy Area
11018 * and updates the current migration unit status.
11019 * Use restore_stripes() to form a destination stripe,
11020 * and to write it to the Copy Area.
11022 * st : supertype information
11023 * dev : imsm device that backup is saved for
11024 * info : general array info
11025 * buf : input buffer
11026 * length : length of data to backup (blocks_per_unit)
11030 ******************************************************************************/
11031 int save_backup_imsm(struct supertype
*st
,
11032 struct imsm_dev
*dev
,
11033 struct mdinfo
*info
,
11038 struct intel_super
*super
= st
->sb
;
11040 struct imsm_map
*map_dest
= get_imsm_map(dev
, MAP_0
);
11041 int new_disks
= map_dest
->num_members
;
11042 int dest_layout
= 0;
11043 int dest_chunk
, targets
[new_disks
];
11044 unsigned long long start
, target_offsets
[new_disks
];
11045 int data_disks
= imsm_num_data_members(map_dest
);
11047 for (i
= 0; i
< new_disks
; i
++) {
11048 struct dl
*dl_disk
= get_imsm_dl_disk(super
, i
);
11049 if (dl_disk
&& is_fd_valid(dl_disk
->fd
))
11050 targets
[i
] = dl_disk
->fd
;
11055 start
= info
->reshape_progress
* 512;
11056 for (i
= 0; i
< new_disks
; i
++) {
11057 target_offsets
[i
] = migr_chkp_area_pba(super
->migr_rec
) * 512;
11058 /* move back copy area adderss, it will be moved forward
11059 * in restore_stripes() using start input variable
11061 target_offsets
[i
] -= start
/data_disks
;
11064 dest_layout
= imsm_level_to_layout(map_dest
->raid_level
);
11065 dest_chunk
= __le16_to_cpu(map_dest
->blocks_per_strip
) * 512;
11067 if (restore_stripes(targets
, /* list of dest devices */
11068 target_offsets
, /* migration record offsets */
11071 map_dest
->raid_level
,
11073 -1, /* source backup file descriptor */
11074 0, /* input buf offset
11075 * always 0 buf is already offseted */
11079 pr_err("Error restoring stripes\n");
11089 /*******************************************************************************
11090 * Function: save_checkpoint_imsm
11091 * Description: Function called for current unit status update
11092 * in the migration record. It writes it to disk.
11094 * super : imsm internal array info
11095 * info : general array info
11099 * 2: failure, means no valid migration record
11100 * / no general migration in progress /
11101 ******************************************************************************/
11102 int save_checkpoint_imsm(struct supertype
*st
, struct mdinfo
*info
, int state
)
11104 struct intel_super
*super
= st
->sb
;
11105 unsigned long long blocks_per_unit
;
11106 unsigned long long curr_migr_unit
;
11108 if (load_imsm_migr_rec(super
) != 0) {
11109 dprintf("imsm: ERROR: Cannot read migration record for checkpoint save.\n");
11113 blocks_per_unit
= __le32_to_cpu(super
->migr_rec
->blocks_per_unit
);
11114 if (blocks_per_unit
== 0) {
11115 dprintf("imsm: no migration in progress.\n");
11118 curr_migr_unit
= info
->reshape_progress
/ blocks_per_unit
;
11119 /* check if array is alligned to copy area
11120 * if it is not alligned, add one to current migration unit value
11121 * this can happend on array reshape finish only
11123 if (info
->reshape_progress
% blocks_per_unit
)
11126 set_current_migr_unit(super
->migr_rec
, curr_migr_unit
);
11127 super
->migr_rec
->rec_status
= __cpu_to_le32(state
);
11128 set_migr_dest_1st_member_lba(super
->migr_rec
,
11129 super
->migr_rec
->dest_depth_per_unit
* curr_migr_unit
);
11131 if (write_imsm_migr_rec(st
) < 0) {
11132 dprintf("imsm: Cannot write migration record outside backup area\n");
11139 /*******************************************************************************
11140 * Function: recover_backup_imsm
11141 * Description: Function recovers critical data from the Migration Copy Area
11142 * while assembling an array.
11144 * super : imsm internal array info
11145 * info : general array info
11147 * 0 : success (or there is no data to recover)
11149 ******************************************************************************/
11150 int recover_backup_imsm(struct supertype
*st
, struct mdinfo
*info
)
11152 struct intel_super
*super
= st
->sb
;
11153 struct migr_record
*migr_rec
= super
->migr_rec
;
11154 struct imsm_map
*map_dest
;
11155 struct intel_dev
*id
= NULL
;
11156 unsigned long long read_offset
;
11157 unsigned long long write_offset
;
11159 int new_disks
, err
;
11162 unsigned int sector_size
= super
->sector_size
;
11163 unsigned long long curr_migr_unit
= current_migr_unit(migr_rec
);
11164 unsigned long long num_migr_units
= get_num_migr_units(migr_rec
);
11165 char buffer
[SYSFS_MAX_BUF_SIZE
];
11166 int skipped_disks
= 0;
11167 struct dl
*dl_disk
;
11169 err
= sysfs_get_str(info
, NULL
, "array_state", (char *)buffer
, sizeof(buffer
));
11173 /* recover data only during assemblation */
11174 if (strncmp(buffer
, "inactive", 8) != 0)
11176 /* no data to recover */
11177 if (__le32_to_cpu(migr_rec
->rec_status
) == UNIT_SRC_NORMAL
)
11179 if (curr_migr_unit
>= num_migr_units
)
11182 /* find device during reshape */
11183 for (id
= super
->devlist
; id
; id
= id
->next
)
11184 if (is_gen_migration(id
->dev
))
11189 map_dest
= get_imsm_map(id
->dev
, MAP_0
);
11190 new_disks
= map_dest
->num_members
;
11192 read_offset
= migr_chkp_area_pba(migr_rec
) * 512;
11194 write_offset
= (migr_dest_1st_member_lba(migr_rec
) +
11195 pba_of_lba0(map_dest
)) * 512;
11197 unit_len
= __le32_to_cpu(migr_rec
->dest_depth_per_unit
) * 512;
11198 if (posix_memalign((void **)&buf
, sector_size
, unit_len
) != 0)
11201 for (dl_disk
= super
->disks
; dl_disk
; dl_disk
= dl_disk
->next
) {
11202 if (dl_disk
->index
< 0)
11205 if (!is_fd_valid(dl_disk
->fd
)) {
11209 if (lseek64(dl_disk
->fd
, read_offset
, SEEK_SET
) < 0) {
11210 pr_err("Cannot seek to block: %s\n",
11215 if (read(dl_disk
->fd
, buf
, unit_len
) != (ssize_t
)unit_len
) {
11216 pr_err("Cannot read copy area block: %s\n",
11221 if (lseek64(dl_disk
->fd
, write_offset
, SEEK_SET
) < 0) {
11222 pr_err("Cannot seek to block: %s\n",
11227 if (write(dl_disk
->fd
, buf
, unit_len
) != (ssize_t
)unit_len
) {
11228 pr_err("Cannot restore block: %s\n",
11235 if (skipped_disks
> imsm_get_allowed_degradation(info
->new_level
,
11239 pr_err("Cannot restore data from backup. Too many failed disks\n");
11243 if (save_checkpoint_imsm(st
, info
, UNIT_SRC_NORMAL
)) {
11244 /* ignore error == 2, this can mean end of reshape here
11246 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL) during restart\n");
11256 * test_and_add_drive_controller_policy_imsm() - add disk controller to policies list.
11257 * @type: Policy type to search on list.
11258 * @pols: List of currently recorded policies.
11259 * @disk_fd: File descriptor of the device to check.
11260 * @hba: The hba disk is attached, could be NULL if verification is disabled.
11261 * @verbose: verbose flag.
11263 * IMSM cares about drive physical placement. If @hba is not set, it adds unknown policy.
11264 * If there is no controller policy on pols we are free to add first one. If there is a policy then,
11265 * new must be the same - no controller mixing allowed.
11267 static mdadm_status_t
11268 test_and_add_drive_controller_policy_imsm(const char * const type
, dev_policy_t
**pols
, int disk_fd
,
11269 struct sys_dev
*hba
, const int verbose
)
11271 const char *controller_policy
= get_sys_dev_type(SYS_DEV_UNKNOWN
);
11272 struct dev_policy
*pol
= pol_find(*pols
, (char *)type
);
11273 char devname
[MAX_RAID_SERIAL_LEN
];
11276 controller_policy
= get_sys_dev_type(hba
->type
);
11279 pol_add(pols
, (char *)type
, (char *)controller_policy
, "imsm");
11280 return MDADM_STATUS_SUCCESS
;
11283 if (strcmp(pol
->value
, controller_policy
) == 0)
11284 return MDADM_STATUS_SUCCESS
;
11286 fd2devname(disk_fd
, devname
);
11287 pr_vrb("Intel(R) raid controller \"%s\" found for %s, but \"%s\" was detected earlier\n",
11288 controller_policy
, devname
, pol
->value
);
11289 pr_vrb("Disks under different controllers cannot be used, aborting\n");
11291 return MDADM_STATUS_ERROR
;
11295 * test_and_add_drive_encryption_policy_imsm() - add disk encryption to policies list.
11296 * @type: policy type to search in the list.
11297 * @pols: list of currently recorded policies.
11298 * @disk_fd: file descriptor of the device to check.
11299 * @hba: The hba to which the drive is attached, could be NULL if verification is disabled.
11300 * @verbose: verbose flag.
11302 * IMSM cares about drive encryption state. It is not allowed to mix disks with different
11303 * encryption state within one md device.
11304 * If there is no encryption policy on pols we are free to add first one.
11305 * If there is a policy then, new must be the same.
11307 static mdadm_status_t
11308 test_and_add_drive_encryption_policy_imsm(const char * const type
, dev_policy_t
**pols
, int disk_fd
,
11309 struct sys_dev
*hba
, const int verbose
)
11311 struct dev_policy
*expected_policy
= pol_find(*pols
, (char *)type
);
11312 struct encryption_information information
= {0};
11313 char *encryption_state
= "Unknown";
11314 int status
= MDADM_STATUS_SUCCESS
;
11315 bool encryption_checked
= true;
11316 char devname
[PATH_MAX
];
11321 switch (hba
->type
) {
11324 status
= get_nvme_opal_encryption_information(disk_fd
, &information
, verbose
);
11327 case SYS_DEV_SATA_VMD
:
11328 status
= get_ata_encryption_information(disk_fd
, &information
, verbose
);
11331 encryption_checked
= false;
11335 fd2devname(disk_fd
, devname
);
11336 pr_vrb("Failed to read encryption information of device %s\n", devname
);
11337 return MDADM_STATUS_ERROR
;
11340 if (encryption_checked
) {
11341 if (information
.status
== ENC_STATUS_LOCKED
) {
11342 fd2devname(disk_fd
, devname
);
11343 pr_vrb("Device %s is in Locked state, cannot use. Aborting.\n", devname
);
11344 return MDADM_STATUS_ERROR
;
11346 encryption_state
= (char *)get_encryption_status_string(information
.status
);
11350 if (expected_policy
) {
11351 if (strcmp(expected_policy
->value
, encryption_state
) == 0)
11352 return MDADM_STATUS_SUCCESS
;
11354 fd2devname(disk_fd
, devname
);
11355 pr_vrb("Encryption status \"%s\" detected for disk %s, but \"%s\" status was detected eariler.\n",
11356 encryption_state
, devname
, expected_policy
->value
);
11357 pr_vrb("Disks with different encryption status cannot be used.\n");
11358 return MDADM_STATUS_ERROR
;
11361 pol_add(pols
, (char *)type
, encryption_state
, "imsm");
11363 return MDADM_STATUS_SUCCESS
;
11366 struct imsm_drive_policy
{
11368 mdadm_status_t (*test_and_add_drive_policy
)(const char * const type
,
11369 struct dev_policy
**pols
, int disk_fd
,
11370 struct sys_dev
*hba
, const int verbose
);
11373 struct imsm_drive_policy imsm_policies
[] = {
11374 {"controller", test_and_add_drive_controller_policy_imsm
},
11375 {"encryption", test_and_add_drive_encryption_policy_imsm
}
11378 mdadm_status_t
test_and_add_drive_policies_imsm(struct dev_policy
**pols
, int disk_fd
,
11381 struct imsm_drive_policy
*imsm_pol
;
11382 struct sys_dev
*hba
= NULL
;
11383 char path
[PATH_MAX
];
11384 mdadm_status_t ret
;
11387 /* If imsm platform verification is disabled, do not search for hba. */
11388 if (check_no_platform() != 1) {
11389 if (!diskfd_to_devpath(disk_fd
, 1, path
)) {
11390 pr_vrb("IMSM: Failed to retrieve device path by file descriptor.\n");
11391 return MDADM_STATUS_ERROR
;
11394 hba
= find_disk_attached_hba(disk_fd
, path
);
11396 pr_vrb("IMSM: Failed to find hba for %s\n", path
);
11397 return MDADM_STATUS_ERROR
;
11401 for (i
= 0; i
< ARRAY_SIZE(imsm_policies
); i
++) {
11402 imsm_pol
= &imsm_policies
[i
];
11404 ret
= imsm_pol
->test_and_add_drive_policy(imsm_pol
->type
, pols
, disk_fd
, hba
,
11406 if (ret
!= MDADM_STATUS_SUCCESS
)
11407 /* Inherit error code */
11411 return MDADM_STATUS_SUCCESS
;
11415 * get_spare_criteria_imsm() - set spare criteria.
11417 * @mddev_path: path to md device devnode, it must be container.
11418 * @c: spare_criteria struct to fill, not NULL.
11420 * If superblock is not loaded, use mddev_path to load_container. It must be given in this case.
11421 * Filles size and sector size accordingly to superblock.
11423 mdadm_status_t
get_spare_criteria_imsm(struct supertype
*st
, char *mddev_path
,
11424 struct spare_criteria
*c
)
11426 mdadm_status_t ret
= MDADM_STATUS_ERROR
;
11427 bool free_superblock
= false;
11428 unsigned long long size
= 0;
11429 struct intel_super
*super
;
11434 /* If no superblock and no mddev_path, we cannot load superblock. */
11435 assert(st
->sb
|| mddev_path
);
11438 int fd
= open(mddev_path
, O_RDONLY
);
11441 if (!is_fd_valid(fd
))
11442 return MDADM_STATUS_ERROR
;
11445 if (load_container_imsm(st
, fd
, st
->devnm
)) {
11447 return MDADM_STATUS_ERROR
;
11449 free_superblock
= true;
11452 rv
= mddev_test_and_add_drive_policies(st
, &c
->pols
, fd
, 0);
11455 if (rv
!= MDADM_STATUS_SUCCESS
)
11461 /* find first active disk in array */
11463 while (dl
&& (is_failed(&dl
->disk
) || dl
->index
== -1))
11469 /* find last lba used by subarrays */
11470 e
= get_extents(super
, dl
, 0);
11474 for (i
= 0; e
[i
].size
; i
++)
11477 size
= e
[i
- 1].start
+ e
[i
- 1].size
;
11480 /* add the amount of space needed for metadata */
11481 size
+= imsm_min_reserved_sectors(super
);
11483 c
->min_size
= size
* 512;
11484 c
->sector_size
= super
->sector_size
;
11485 c
->criteria_set
= true;
11486 ret
= MDADM_STATUS_SUCCESS
;
11489 if (free_superblock
)
11490 free_super_imsm(st
);
11492 if (ret
!= MDADM_STATUS_SUCCESS
)
11493 c
->criteria_set
= false;
11498 static char *imsm_find_array_devnm_by_subdev(int subdev
, char *container
)
11500 static char devnm
[32];
11501 char subdev_name
[20];
11502 struct mdstat_ent
*mdstat
;
11504 sprintf(subdev_name
, "%d", subdev
);
11505 mdstat
= mdstat_by_subdev(subdev_name
, container
);
11509 strcpy(devnm
, mdstat
->devnm
);
11510 free_mdstat(mdstat
);
11514 static int imsm_reshape_is_allowed_on_container(struct supertype
*st
,
11515 struct geo_params
*geo
,
11516 int *old_raid_disks
,
11519 /* currently we only support increasing the number of devices
11520 * for a container. This increases the number of device for each
11521 * member array. They must all be RAID0 or RAID5.
11524 struct mdinfo
*info
, *member
;
11525 int devices_that_can_grow
= 0;
11527 dprintf("imsm: imsm_reshape_is_allowed_on_container(ENTER): st->devnm = (%s)\n", st
->devnm
);
11529 if (geo
->size
> 0 ||
11530 geo
->level
!= UnSet
||
11531 geo
->layout
!= UnSet
||
11532 geo
->chunksize
!= 0 ||
11533 geo
->raid_disks
== UnSet
) {
11534 dprintf("imsm: Container operation is allowed for raid disks number change only.\n");
11538 if (direction
== ROLLBACK_METADATA_CHANGES
) {
11539 dprintf("imsm: Metadata changes rollback is not supported for container operation.\n");
11543 info
= container_content_imsm(st
, NULL
);
11544 for (member
= info
; member
; member
= member
->next
) {
11547 dprintf("imsm: checking device_num: %i\n",
11548 member
->container_member
);
11550 if (geo
->raid_disks
<= member
->array
.raid_disks
) {
11551 /* we work on container for Online Capacity Expansion
11552 * only so raid_disks has to grow
11554 dprintf("imsm: for container operation raid disks increase is required\n");
11558 if (info
->array
.level
!= 0 && info
->array
.level
!= 5) {
11559 /* we cannot use this container with other raid level
11561 dprintf("imsm: for container operation wrong raid level (%i) detected\n",
11562 info
->array
.level
);
11565 /* check for platform support
11566 * for this raid level configuration
11568 struct intel_super
*super
= st
->sb
;
11569 if (!is_raid_level_supported(super
->orom
,
11570 member
->array
.level
,
11571 geo
->raid_disks
)) {
11572 dprintf("platform does not support raid%d with %d disk%s\n",
11575 geo
->raid_disks
> 1 ? "s" : "");
11578 /* check if component size is aligned to chunk size
11580 if (info
->component_size
%
11581 (info
->array
.chunk_size
/512)) {
11582 dprintf("Component size is not aligned to chunk size\n");
11587 if (*old_raid_disks
&&
11588 info
->array
.raid_disks
!= *old_raid_disks
)
11590 *old_raid_disks
= info
->array
.raid_disks
;
11592 /* All raid5 and raid0 volumes in container
11593 * have to be ready for Online Capacity Expansion
11594 * so they need to be assembled. We have already
11595 * checked that no recovery etc is happening.
11597 result
= imsm_find_array_devnm_by_subdev(member
->container_member
,
11598 st
->container_devnm
);
11599 if (result
== NULL
) {
11600 dprintf("imsm: cannot find array\n");
11603 devices_that_can_grow
++;
11606 if (!member
&& devices_that_can_grow
)
11610 dprintf("Container operation allowed\n");
11612 dprintf("Error: %i\n", ret_val
);
11617 /* Function: get_spares_for_grow
11618 * Description: Allocates memory and creates list of spare devices
11619 * avaliable in container. Checks if spare drive size is acceptable.
11620 * Parameters: Pointer to the supertype structure
11621 * Returns: Pointer to the list of spare devices (mdinfo structure) on success,
11624 static struct mdinfo
*get_spares_for_grow(struct supertype
*st
)
11626 struct spare_criteria sc
= {0};
11627 struct mdinfo
*spares
;
11629 get_spare_criteria_imsm(st
, NULL
, &sc
);
11630 spares
= container_choose_spares(st
, &sc
, NULL
, NULL
, NULL
, 0);
11632 dev_policy_free(sc
.pols
);
11637 /******************************************************************************
11638 * function: imsm_create_metadata_update_for_reshape
11639 * Function creates update for whole IMSM container.
11641 ******************************************************************************/
11642 static int imsm_create_metadata_update_for_reshape(
11643 struct supertype
*st
,
11644 struct geo_params
*geo
,
11645 int old_raid_disks
,
11646 struct imsm_update_reshape
**updatep
)
11648 struct intel_super
*super
= st
->sb
;
11649 struct imsm_super
*mpb
= super
->anchor
;
11650 int update_memory_size
;
11651 struct imsm_update_reshape
*u
;
11652 struct mdinfo
*spares
;
11655 struct mdinfo
*dev
;
11657 dprintf("(enter) raid_disks = %i\n", geo
->raid_disks
);
11659 delta_disks
= geo
->raid_disks
- old_raid_disks
;
11661 /* size of all update data without anchor */
11662 update_memory_size
= sizeof(struct imsm_update_reshape
);
11664 /* now add space for spare disks that we need to add. */
11665 update_memory_size
+= sizeof(u
->new_disks
[0]) * (delta_disks
- 1);
11667 u
= xcalloc(1, update_memory_size
);
11668 u
->type
= update_reshape_container_disks
;
11669 u
->old_raid_disks
= old_raid_disks
;
11670 u
->new_raid_disks
= geo
->raid_disks
;
11672 /* now get spare disks list
11674 spares
= get_spares_for_grow(st
);
11676 if (spares
== NULL
|| delta_disks
> spares
->array
.spare_disks
) {
11677 pr_err("imsm: ERROR: Cannot get spare devices for %s.\n", geo
->dev_name
);
11682 /* we have got spares
11683 * update disk list in imsm_disk list table in anchor
11685 dprintf("imsm: %i spares are available.\n\n",
11686 spares
->array
.spare_disks
);
11688 dev
= spares
->devs
;
11689 for (i
= 0; i
< delta_disks
; i
++) {
11694 u
->new_disks
[i
] = makedev(dev
->disk
.major
,
11696 dl
= get_disk_super(super
, dev
->disk
.major
, dev
->disk
.minor
);
11697 dl
->index
= mpb
->num_disks
;
11705 sysfs_free(spares
);
11707 dprintf("imsm: reshape update preparation :");
11708 if (i
== delta_disks
) {
11709 dprintf_cont(" OK\n");
11711 return update_memory_size
;
11714 dprintf_cont(" Error\n");
11719 /******************************************************************************
11720 * function: imsm_create_metadata_update_for_size_change()
11721 * Creates update for IMSM array for array size change.
11723 ******************************************************************************/
11724 static int imsm_create_metadata_update_for_size_change(
11725 struct supertype
*st
,
11726 struct geo_params
*geo
,
11727 struct imsm_update_size_change
**updatep
)
11729 struct intel_super
*super
= st
->sb
;
11730 int update_memory_size
;
11731 struct imsm_update_size_change
*u
;
11733 dprintf("(enter) New size = %llu\n", geo
->size
);
11735 /* size of all update data without anchor */
11736 update_memory_size
= sizeof(struct imsm_update_size_change
);
11738 u
= xcalloc(1, update_memory_size
);
11739 u
->type
= update_size_change
;
11740 u
->subdev
= super
->current_vol
;
11741 u
->new_size
= geo
->size
;
11743 dprintf("imsm: reshape update preparation : OK\n");
11746 return update_memory_size
;
11749 /******************************************************************************
11750 * function: imsm_create_metadata_update_for_migration()
11751 * Creates update for IMSM array.
11753 ******************************************************************************/
11754 static int imsm_create_metadata_update_for_migration(
11755 struct supertype
*st
,
11756 struct geo_params
*geo
,
11757 struct imsm_update_reshape_migration
**updatep
)
11759 struct intel_super
*super
= st
->sb
;
11760 int update_memory_size
;
11761 int current_chunk_size
;
11762 struct imsm_update_reshape_migration
*u
;
11763 struct imsm_dev
*dev
= get_imsm_dev(super
, super
->current_vol
);
11764 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
11765 int previous_level
= -1;
11767 dprintf("(enter) New Level = %i\n", geo
->level
);
11769 /* size of all update data without anchor */
11770 update_memory_size
= sizeof(struct imsm_update_reshape_migration
);
11772 u
= xcalloc(1, update_memory_size
);
11773 u
->type
= update_reshape_migration
;
11774 u
->subdev
= super
->current_vol
;
11775 u
->new_level
= geo
->level
;
11776 u
->new_layout
= geo
->layout
;
11777 u
->new_raid_disks
= u
->old_raid_disks
= geo
->raid_disks
;
11778 u
->new_disks
[0] = -1;
11779 u
->new_chunksize
= -1;
11781 current_chunk_size
= __le16_to_cpu(map
->blocks_per_strip
) / 2;
11783 if (geo
->chunksize
!= current_chunk_size
) {
11784 u
->new_chunksize
= geo
->chunksize
/ 1024;
11785 dprintf("imsm: chunk size change from %i to %i\n",
11786 current_chunk_size
, u
->new_chunksize
);
11788 previous_level
= map
->raid_level
;
11790 if (geo
->level
== 5 && previous_level
== 0) {
11791 struct mdinfo
*spares
= NULL
;
11793 u
->new_raid_disks
++;
11794 spares
= get_spares_for_grow(st
);
11795 if (spares
== NULL
|| spares
->array
.spare_disks
< 1) {
11797 sysfs_free(spares
);
11798 update_memory_size
= 0;
11799 pr_err("cannot get spare device for requested migration\n");
11802 sysfs_free(spares
);
11804 dprintf("imsm: reshape update preparation : OK\n");
11807 return update_memory_size
;
11810 static void imsm_update_metadata_locally(struct supertype
*st
,
11811 void *buf
, int len
)
11813 struct metadata_update mu
;
11818 mu
.space_list
= NULL
;
11820 if (imsm_prepare_update(st
, &mu
))
11821 imsm_process_update(st
, &mu
);
11823 while (mu
.space_list
) {
11824 void **space
= mu
.space_list
;
11825 mu
.space_list
= *space
;
11831 * imsm_analyze_expand() - check expand properties and calculate new size.
11832 * @st: imsm supertype.
11833 * @geo: new geometry params.
11834 * @array: array info.
11835 * @direction: reshape direction.
11837 * Obtain free space after the &array and verify if expand to requested size is
11838 * possible. If geo->size is set to %MAX_SIZE, assume that max free size is
11842 * On success %IMSM_STATUS_OK is returned, geo->size and geo->raid_disks are
11844 * On error, %IMSM_STATUS_ERROR is returned.
11846 static imsm_status_t
imsm_analyze_expand(struct supertype
*st
,
11847 struct geo_params
*geo
,
11848 struct mdinfo
*array
,
11851 struct intel_super
*super
= st
->sb
;
11852 struct imsm_dev
*dev
= get_imsm_dev(super
, super
->current_vol
);
11853 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
11854 int data_disks
= imsm_num_data_members(map
);
11856 unsigned long long current_size
;
11857 unsigned long long free_size
;
11858 unsigned long long new_size
;
11859 unsigned long long max_size
;
11861 const int chunk_kib
= geo
->chunksize
/ 1024;
11864 if (direction
== ROLLBACK_METADATA_CHANGES
) {
11866 * Accept size for rollback only.
11868 new_size
= geo
->size
* 2;
11872 if (data_disks
== 0) {
11873 pr_err("imsm: Cannot retrieve data disks.\n");
11874 return IMSM_STATUS_ERROR
;
11876 current_size
= array
->custom_array_size
/ data_disks
;
11878 rv
= imsm_get_free_size(super
, dev
->vol
.map
->num_members
, 0, chunk_kib
, &free_size
, true);
11879 if (rv
!= IMSM_STATUS_OK
) {
11880 pr_err("imsm: Cannot find free space for expand.\n");
11881 return IMSM_STATUS_ERROR
;
11883 max_size
= round_member_size_to_mb(free_size
+ current_size
);
11885 if (geo
->size
== MAX_SIZE
)
11886 new_size
= max_size
;
11888 new_size
= round_member_size_to_mb(geo
->size
* 2);
11890 if (new_size
== 0) {
11891 pr_err("imsm: Rounded requested size is 0.\n");
11892 return IMSM_STATUS_ERROR
;
11895 if (new_size
> max_size
) {
11896 pr_err("imsm: Rounded requested size (%llu) is larger than free space available (%llu).\n",
11897 new_size
, max_size
);
11898 return IMSM_STATUS_ERROR
;
11901 if (new_size
== current_size
) {
11902 pr_err("imsm: Rounded requested size (%llu) is same as current size (%llu).\n",
11903 new_size
, current_size
);
11904 return IMSM_STATUS_ERROR
;
11907 if (new_size
< current_size
) {
11908 pr_err("imsm: Size reduction is not supported, rounded requested size (%llu) is smaller than current (%llu).\n",
11909 new_size
, current_size
);
11910 return IMSM_STATUS_ERROR
;
11914 dprintf("imsm: New size per member is %llu.\n", new_size
);
11915 geo
->size
= data_disks
* new_size
;
11916 geo
->raid_disks
= dev
->vol
.map
->num_members
;
11917 return IMSM_STATUS_OK
;
11920 /***************************************************************************
11921 * Function: imsm_analyze_change
11922 * Description: Function analyze change for single volume
11923 * and validate if transition is supported
11924 * Parameters: Geometry parameters, supertype structure,
11925 * metadata change direction (apply/rollback)
11926 * Returns: Operation type code on success, -1 if fail
11927 ****************************************************************************/
11928 enum imsm_reshape_type
imsm_analyze_change(struct supertype
*st
,
11929 struct geo_params
*geo
,
11932 struct mdinfo info
;
11934 int check_devs
= 0;
11936 /* number of added/removed disks in operation result */
11937 int devNumChange
= 0;
11938 /* imsm compatible layout value for array geometry verification */
11939 int imsm_layout
= -1;
11942 getinfo_super_imsm_volume(st
, &info
, NULL
);
11943 if (geo
->level
!= info
.array
.level
&& geo
->level
>= 0 &&
11944 geo
->level
!= UnSet
) {
11945 switch (info
.array
.level
) {
11947 if (geo
->level
== 5) {
11948 change
= CH_MIGRATION
;
11949 if (geo
->layout
!= ALGORITHM_LEFT_ASYMMETRIC
) {
11950 pr_err("Error. Requested Layout not supported (left-asymmetric layout is supported only)!\n");
11952 goto analyse_change_exit
;
11954 imsm_layout
= geo
->layout
;
11956 devNumChange
= 1; /* parity disk added */
11957 } else if (geo
->level
== 10) {
11958 change
= CH_TAKEOVER
;
11960 devNumChange
= 2; /* two mirrors added */
11961 imsm_layout
= 0x102; /* imsm supported layout */
11966 if (geo
->level
== 0) {
11967 change
= CH_TAKEOVER
;
11969 devNumChange
= -(geo
->raid_disks
/2);
11970 imsm_layout
= 0; /* imsm raid0 layout */
11974 if (change
== -1) {
11975 pr_err("Error. Level Migration from %d to %d not supported!\n",
11976 info
.array
.level
, geo
->level
);
11977 goto analyse_change_exit
;
11980 geo
->level
= info
.array
.level
;
11982 if (geo
->layout
!= info
.array
.layout
&&
11983 (geo
->layout
!= UnSet
&& geo
->layout
!= -1)) {
11984 change
= CH_MIGRATION
;
11985 if (info
.array
.layout
== 0 && info
.array
.level
== 5 &&
11986 geo
->layout
== 5) {
11987 /* reshape 5 -> 4 */
11988 } else if (info
.array
.layout
== 5 && info
.array
.level
== 5 &&
11989 geo
->layout
== 0) {
11990 /* reshape 4 -> 5 */
11994 pr_err("Error. Layout Migration from %d to %d not supported!\n",
11995 info
.array
.layout
, geo
->layout
);
11997 goto analyse_change_exit
;
12000 geo
->layout
= info
.array
.layout
;
12001 if (imsm_layout
== -1)
12002 imsm_layout
= info
.array
.layout
;
12005 if (geo
->chunksize
> 0 && geo
->chunksize
!= UnSet
&&
12006 geo
->chunksize
!= info
.array
.chunk_size
) {
12007 if (info
.array
.level
== 10) {
12008 pr_err("Error. Chunk size change for RAID 10 is not supported.\n");
12010 goto analyse_change_exit
;
12011 } else if (info
.component_size
% (geo
->chunksize
/512)) {
12012 pr_err("New chunk size (%dK) does not evenly divide device size (%lluk). Aborting...\n",
12013 geo
->chunksize
/1024, info
.component_size
/2);
12015 goto analyse_change_exit
;
12017 change
= CH_MIGRATION
;
12019 geo
->chunksize
= info
.array
.chunk_size
;
12022 if (geo
->size
> 0) {
12023 if (change
!= -1) {
12024 pr_err("Error. Size change should be the only one at a time.\n");
12026 goto analyse_change_exit
;
12029 rv
= imsm_analyze_expand(st
, geo
, &info
, direction
);
12030 if (rv
!= IMSM_STATUS_OK
)
12031 goto analyse_change_exit
;
12032 change
= CH_ARRAY_SIZE
;
12035 chunk
= geo
->chunksize
/ 1024;
12036 if (!validate_geometry_imsm(st
,
12039 geo
->raid_disks
+ devNumChange
,
12041 geo
->size
, INVALID_SECTORS
,
12042 0, 0, info
.consistency_policy
, 1))
12046 struct intel_super
*super
= st
->sb
;
12047 struct imsm_super
*mpb
= super
->anchor
;
12049 if (mpb
->num_raid_devs
> 1) {
12050 pr_err("Error. Cannot perform operation on %s- for this operation "
12051 "it MUST be single array in container\n", geo
->dev_name
);
12056 analyse_change_exit
:
12057 if (direction
== ROLLBACK_METADATA_CHANGES
&&
12058 (change
== CH_MIGRATION
|| change
== CH_TAKEOVER
)) {
12059 dprintf("imsm: Metadata changes rollback is not supported for migration and takeover operations.\n");
12065 int imsm_takeover(struct supertype
*st
, struct geo_params
*geo
)
12067 struct intel_super
*super
= st
->sb
;
12068 struct imsm_update_takeover
*u
;
12070 u
= xmalloc(sizeof(struct imsm_update_takeover
));
12072 u
->type
= update_takeover
;
12073 u
->subarray
= super
->current_vol
;
12075 /* 10->0 transition */
12076 if (geo
->level
== 0)
12077 u
->direction
= R10_TO_R0
;
12079 /* 0->10 transition */
12080 if (geo
->level
== 10)
12081 u
->direction
= R0_TO_R10
;
12083 /* update metadata locally */
12084 imsm_update_metadata_locally(st
, u
,
12085 sizeof(struct imsm_update_takeover
));
12086 /* and possibly remotely */
12087 if (st
->update_tail
)
12088 append_metadata_update(st
, u
,
12089 sizeof(struct imsm_update_takeover
));
12096 /* Flush size update if size calculated by num_data_stripes is higher than
12097 * imsm_dev_size to eliminate differences during reshape.
12098 * Mdmon will recalculate them correctly.
12099 * If subarray index is not set then check whole container.
12101 * 0 - no error occurred
12102 * 1 - error detected
12104 static int imsm_fix_size_mismatch(struct supertype
*st
, int subarray_index
)
12106 struct intel_super
*super
= st
->sb
;
12107 int tmp
= super
->current_vol
;
12111 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
12112 if (subarray_index
>= 0 && i
!= subarray_index
)
12114 super
->current_vol
= i
;
12115 struct imsm_dev
*dev
= get_imsm_dev(super
, super
->current_vol
);
12116 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
12117 unsigned int disc_count
= imsm_num_data_members(map
);
12118 struct geo_params geo
;
12119 struct imsm_update_size_change
*update
;
12120 unsigned long long calc_size
= per_dev_array_size(map
) * disc_count
;
12121 unsigned long long d_size
= imsm_dev_size(dev
);
12124 if (calc_size
== d_size
)
12127 /* There is a difference, confirm that imsm_dev_size is
12128 * smaller and push update.
12130 if (d_size
> calc_size
) {
12131 pr_err("imsm: dev size of subarray %d is incorrect\n",
12135 memset(&geo
, 0, sizeof(struct geo_params
));
12137 u_size
= imsm_create_metadata_update_for_size_change(st
, &geo
,
12139 imsm_update_metadata_locally(st
, update
, u_size
);
12140 if (st
->update_tail
) {
12141 append_metadata_update(st
, update
, u_size
);
12142 flush_metadata_updates(st
);
12143 st
->update_tail
= &st
->updates
;
12145 imsm_sync_metadata(st
);
12151 super
->current_vol
= tmp
;
12155 static int imsm_reshape_super(struct supertype
*st
, unsigned long long size
,
12157 int layout
, int chunksize
, int raid_disks
,
12158 int delta_disks
, char *backup
, char *dev
,
12159 int direction
, int verbose
)
12162 struct geo_params geo
;
12164 dprintf("(enter)\n");
12166 memset(&geo
, 0, sizeof(struct geo_params
));
12168 geo
.dev_name
= dev
;
12169 strcpy(geo
.devnm
, st
->devnm
);
12172 geo
.layout
= layout
;
12173 geo
.chunksize
= chunksize
;
12174 geo
.raid_disks
= raid_disks
;
12175 if (delta_disks
!= UnSet
)
12176 geo
.raid_disks
+= delta_disks
;
12178 dprintf("for level : %i\n", geo
.level
);
12179 dprintf("for raid_disks : %i\n", geo
.raid_disks
);
12181 if (strcmp(st
->container_devnm
, st
->devnm
) == 0) {
12182 /* On container level we can only increase number of devices. */
12183 dprintf("imsm: info: Container operation\n");
12184 int old_raid_disks
= 0;
12186 if (imsm_reshape_is_allowed_on_container(
12187 st
, &geo
, &old_raid_disks
, direction
)) {
12188 struct imsm_update_reshape
*u
= NULL
;
12191 if (imsm_fix_size_mismatch(st
, -1)) {
12192 dprintf("imsm: Cannot fix size mismatch\n");
12193 goto exit_imsm_reshape_super
;
12196 len
= imsm_create_metadata_update_for_reshape(
12197 st
, &geo
, old_raid_disks
, &u
);
12200 dprintf("imsm: Cannot prepare update\n");
12201 goto exit_imsm_reshape_super
;
12205 /* update metadata locally */
12206 imsm_update_metadata_locally(st
, u
, len
);
12207 /* and possibly remotely */
12208 if (st
->update_tail
)
12209 append_metadata_update(st
, u
, len
);
12214 pr_err("(imsm) Operation is not allowed on this container\n");
12217 /* On volume level we support following operations
12218 * - takeover: raid10 -> raid0; raid0 -> raid10
12219 * - chunk size migration
12220 * - migration: raid5 -> raid0; raid0 -> raid5
12222 struct intel_super
*super
= st
->sb
;
12223 struct intel_dev
*dev
= super
->devlist
;
12225 dprintf("imsm: info: Volume operation\n");
12226 /* find requested device */
12229 imsm_find_array_devnm_by_subdev(
12230 dev
->index
, st
->container_devnm
);
12231 if (devnm
&& strcmp(devnm
, geo
.devnm
) == 0)
12236 pr_err("Cannot find %s (%s) subarray\n",
12237 geo
.dev_name
, geo
.devnm
);
12238 goto exit_imsm_reshape_super
;
12240 super
->current_vol
= dev
->index
;
12241 change
= imsm_analyze_change(st
, &geo
, direction
);
12244 ret_val
= imsm_takeover(st
, &geo
);
12246 case CH_MIGRATION
: {
12247 struct imsm_update_reshape_migration
*u
= NULL
;
12249 imsm_create_metadata_update_for_migration(
12252 dprintf("imsm: Cannot prepare update\n");
12256 /* update metadata locally */
12257 imsm_update_metadata_locally(st
, u
, len
);
12258 /* and possibly remotely */
12259 if (st
->update_tail
)
12260 append_metadata_update(st
, u
, len
);
12265 case CH_ARRAY_SIZE
: {
12266 struct imsm_update_size_change
*u
= NULL
;
12268 imsm_create_metadata_update_for_size_change(
12271 dprintf("imsm: Cannot prepare update\n");
12275 /* update metadata locally */
12276 imsm_update_metadata_locally(st
, u
, len
);
12277 /* and possibly remotely */
12278 if (st
->update_tail
)
12279 append_metadata_update(st
, u
, len
);
12289 exit_imsm_reshape_super
:
12290 dprintf("imsm: reshape_super Exit code = %i\n", ret_val
);
12294 #define COMPLETED_OK 0
12295 #define COMPLETED_NONE 1
12296 #define COMPLETED_DELAYED 2
12298 static int read_completed(int fd
, unsigned long long *val
)
12301 char buf
[SYSFS_MAX_BUF_SIZE
];
12303 ret
= sysfs_fd_get_str(fd
, buf
, sizeof(buf
));
12307 ret
= COMPLETED_OK
;
12308 if (str_is_none(buf
) == true) {
12309 ret
= COMPLETED_NONE
;
12310 } else if (strncmp(buf
, "delayed", 7) == 0) {
12311 ret
= COMPLETED_DELAYED
;
12314 *val
= strtoull(buf
, &ep
, 0);
12315 if (ep
== buf
|| (*ep
!= 0 && *ep
!= '\n' && *ep
!= ' '))
12321 /*******************************************************************************
12322 * Function: wait_for_reshape_imsm
12323 * Description: Function writes new sync_max value and waits until
12324 * reshape process reach new position
12326 * sra : general array info
12327 * ndata : number of disks in new array's layout
12330 * 1 : there is no reshape in progress,
12332 ******************************************************************************/
12333 int wait_for_reshape_imsm(struct mdinfo
*sra
, int ndata
)
12335 int fd
= sysfs_get_fd(sra
, NULL
, "sync_completed");
12337 unsigned long long completed
;
12338 /* to_complete : new sync_max position */
12339 unsigned long long to_complete
= sra
->reshape_progress
;
12340 unsigned long long position_to_set
= to_complete
/ ndata
;
12342 if (!is_fd_valid(fd
)) {
12343 dprintf("cannot open reshape_position\n");
12348 if (sysfs_fd_get_ll(fd
, &completed
) < 0) {
12350 dprintf("cannot read reshape_position (no reshape in progres)\n");
12354 sleep_for(0, MSEC_TO_NSEC(30), true);
12359 if (completed
> position_to_set
) {
12360 dprintf("wrong next position to set %llu (%llu)\n",
12361 to_complete
, position_to_set
);
12365 dprintf("Position set: %llu\n", position_to_set
);
12366 if (sysfs_set_num(sra
, NULL
, "sync_max",
12367 position_to_set
) != 0) {
12368 dprintf("cannot set reshape position to %llu\n",
12376 char action
[SYSFS_MAX_BUF_SIZE
];
12377 int timeout
= 3000;
12379 sysfs_wait(fd
, &timeout
);
12380 if (sysfs_get_str(sra
, NULL
, "sync_action",
12381 action
, sizeof(action
)) > 0 &&
12382 strncmp(action
, "reshape", 7) != 0) {
12383 if (strncmp(action
, "idle", 4) == 0)
12389 rc
= read_completed(fd
, &completed
);
12391 dprintf("cannot read reshape_position (in loop)\n");
12394 } else if (rc
== COMPLETED_NONE
)
12396 } while (completed
< position_to_set
);
12402 /*******************************************************************************
12403 * Function: check_degradation_change
12404 * Description: Check that array hasn't become failed.
12406 * info : for sysfs access
12407 * sources : source disks descriptors
12408 * degraded: previous degradation level
12410 * degradation level
12411 ******************************************************************************/
12412 int check_degradation_change(struct mdinfo
*info
,
12416 unsigned long long new_degraded
;
12419 rv
= sysfs_get_ll(info
, NULL
, "degraded", &new_degraded
);
12420 if (rv
== -1 || (new_degraded
!= (unsigned long long)degraded
)) {
12421 /* check each device to ensure it is still working */
12424 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
12425 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
12427 if (sd
->disk
.state
& (1<<MD_DISK_SYNC
)) {
12428 char sbuf
[SYSFS_MAX_BUF_SIZE
];
12429 int raid_disk
= sd
->disk
.raid_disk
;
12431 if (sysfs_get_str(info
,
12432 sd
, "state", sbuf
, sizeof(sbuf
)) < 0 ||
12433 strstr(sbuf
, "faulty") ||
12434 strstr(sbuf
, "in_sync") == NULL
) {
12435 /* this device is dead */
12436 sd
->disk
.state
= (1<<MD_DISK_FAULTY
);
12437 if (raid_disk
>= 0)
12438 close_fd(&sources
[raid_disk
]);
12445 return new_degraded
;
12448 /*******************************************************************************
12449 * Function: imsm_manage_reshape
12450 * Description: Function finds array under reshape and it manages reshape
12451 * process. It creates stripes backups (if required) and sets
12454 * afd : Backup handle (nattive) - not used
12455 * sra : general array info
12456 * reshape : reshape parameters - not used
12457 * st : supertype structure
12458 * blocks : size of critical section [blocks]
12459 * fds : table of source device descriptor
12460 * offsets : start of array (offest per devices)
12462 * destfd : table of destination device descriptor
12463 * destoffsets : table of destination offsets (per device)
12465 * 1 : success, reshape is done
12467 ******************************************************************************/
12468 static int imsm_manage_reshape(
12469 int afd
, struct mdinfo
*sra
, struct reshape
*reshape
,
12470 struct supertype
*st
, unsigned long backup_blocks
,
12471 int *fds
, unsigned long long *offsets
,
12472 int dests
, int *destfd
, unsigned long long *destoffsets
)
12475 struct intel_super
*super
= st
->sb
;
12476 struct intel_dev
*dv
;
12477 unsigned int sector_size
= super
->sector_size
;
12478 struct imsm_dev
*dev
= NULL
;
12479 struct imsm_map
*map_src
, *map_dest
;
12480 int migr_vol_qan
= 0;
12481 int ndata
, odata
; /* [bytes] */
12482 int chunk
; /* [bytes] */
12483 struct migr_record
*migr_rec
;
12485 unsigned int buf_size
; /* [bytes] */
12486 unsigned long long max_position
; /* array size [bytes] */
12487 unsigned long long next_step
; /* [blocks]/[bytes] */
12488 unsigned long long old_data_stripe_length
;
12489 unsigned long long start_src
; /* [bytes] */
12490 unsigned long long start
; /* [bytes] */
12491 unsigned long long start_buf_shift
; /* [bytes] */
12493 int source_layout
= 0;
12494 int subarray_index
= -1;
12499 if (!fds
|| !offsets
)
12502 /* Find volume during the reshape */
12503 for (dv
= super
->devlist
; dv
; dv
= dv
->next
) {
12504 if (dv
->dev
->vol
.migr_type
== MIGR_GEN_MIGR
&&
12505 dv
->dev
->vol
.migr_state
== 1) {
12508 subarray_index
= dv
->index
;
12511 /* Only one volume can migrate at the same time */
12512 if (migr_vol_qan
!= 1) {
12513 pr_err("%s", migr_vol_qan
?
12514 "Number of migrating volumes greater than 1\n" :
12515 "There is no volume during migrationg\n");
12519 map_dest
= get_imsm_map(dev
, MAP_0
);
12520 map_src
= get_imsm_map(dev
, MAP_1
);
12521 if (map_src
== NULL
)
12524 ndata
= imsm_num_data_members(map_dest
);
12525 odata
= imsm_num_data_members(map_src
);
12527 chunk
= __le16_to_cpu(map_src
->blocks_per_strip
) * 512;
12528 old_data_stripe_length
= odata
* chunk
;
12530 migr_rec
= super
->migr_rec
;
12532 /* initialize migration record for start condition */
12533 if (sra
->reshape_progress
== 0)
12534 init_migr_record_imsm(st
, dev
, sra
);
12536 if (__le32_to_cpu(migr_rec
->rec_status
) != UNIT_SRC_NORMAL
) {
12537 dprintf("imsm: cannot restart migration when data are present in copy area.\n");
12540 /* Save checkpoint to update migration record for current
12541 * reshape position (in md). It can be farther than current
12542 * reshape position in metadata.
12544 if (save_checkpoint_imsm(st
, sra
, UNIT_SRC_NORMAL
) == 1) {
12545 /* ignore error == 2, this can mean end of reshape here
12547 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL, initial save)\n");
12552 /* size for data */
12553 buf_size
= __le32_to_cpu(migr_rec
->blocks_per_unit
) * 512;
12554 /* extend buffer size for parity disk */
12555 buf_size
+= __le32_to_cpu(migr_rec
->dest_depth_per_unit
) * 512;
12556 /* add space for stripe alignment */
12557 buf_size
+= old_data_stripe_length
;
12558 if (posix_memalign((void **)&buf
, MAX_SECTOR_SIZE
, buf_size
)) {
12559 dprintf("imsm: Cannot allocate checkpoint buffer\n");
12563 max_position
= sra
->component_size
* ndata
;
12564 source_layout
= imsm_level_to_layout(map_src
->raid_level
);
12566 while (current_migr_unit(migr_rec
) <
12567 get_num_migr_units(migr_rec
)) {
12568 /* current reshape position [blocks] */
12569 unsigned long long current_position
=
12570 __le32_to_cpu(migr_rec
->blocks_per_unit
)
12571 * current_migr_unit(migr_rec
);
12572 unsigned long long border
;
12574 /* Check that array hasn't become failed.
12576 degraded
= check_degradation_change(sra
, fds
, degraded
);
12577 if (degraded
> 1) {
12578 dprintf("imsm: Abort reshape due to degradation level (%i)\n", degraded
);
12582 next_step
= __le32_to_cpu(migr_rec
->blocks_per_unit
);
12584 if ((current_position
+ next_step
) > max_position
)
12585 next_step
= max_position
- current_position
;
12587 start
= current_position
* 512;
12589 /* align reading start to old geometry */
12590 start_buf_shift
= start
% old_data_stripe_length
;
12591 start_src
= start
- start_buf_shift
;
12593 border
= (start_src
/ odata
) - (start
/ ndata
);
12595 if (border
<= __le32_to_cpu(migr_rec
->dest_depth_per_unit
)) {
12596 /* save critical stripes to buf
12597 * start - start address of current unit
12598 * to backup [bytes]
12599 * start_src - start address of current unit
12600 * to backup alligned to source array
12603 unsigned long long next_step_filler
;
12604 unsigned long long copy_length
= next_step
* 512;
12606 /* allign copy area length to stripe in old geometry */
12607 next_step_filler
= ((copy_length
+ start_buf_shift
)
12608 % old_data_stripe_length
);
12609 if (next_step_filler
)
12610 next_step_filler
= (old_data_stripe_length
12611 - next_step_filler
);
12612 dprintf("save_stripes() parameters: start = %llu,\tstart_src = %llu,\tnext_step*512 = %llu,\tstart_in_buf_shift = %llu,\tnext_step_filler = %llu\n",
12613 start
, start_src
, copy_length
,
12614 start_buf_shift
, next_step_filler
);
12616 if (save_stripes(fds
, offsets
, map_src
->num_members
,
12617 chunk
, map_src
->raid_level
,
12618 source_layout
, 0, NULL
, start_src
,
12620 next_step_filler
+ start_buf_shift
,
12622 dprintf("imsm: Cannot save stripes to buffer\n");
12625 /* Convert data to destination format and store it
12626 * in backup general migration area
12628 if (save_backup_imsm(st
, dev
, sra
,
12629 buf
+ start_buf_shift
, copy_length
)) {
12630 dprintf("imsm: Cannot save stripes to target devices\n");
12633 if (save_checkpoint_imsm(st
, sra
,
12634 UNIT_SRC_IN_CP_AREA
)) {
12635 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_IN_CP_AREA)\n");
12639 /* set next step to use whole border area */
12640 border
/= next_step
;
12642 next_step
*= border
;
12644 /* When data backed up, checkpoint stored,
12645 * kick the kernel to reshape unit of data
12647 next_step
= next_step
+ sra
->reshape_progress
;
12648 /* limit next step to array max position */
12649 if (next_step
> max_position
)
12650 next_step
= max_position
;
12651 sysfs_set_num(sra
, NULL
, "suspend_lo", sra
->reshape_progress
);
12652 sysfs_set_num(sra
, NULL
, "suspend_hi", next_step
);
12653 sra
->reshape_progress
= next_step
;
12655 /* wait until reshape finish */
12656 if (wait_for_reshape_imsm(sra
, ndata
)) {
12657 dprintf("wait_for_reshape_imsm returned error!\n");
12663 if (save_checkpoint_imsm(st
, sra
, UNIT_SRC_NORMAL
) == 1) {
12664 /* ignore error == 2, this can mean end of reshape here
12666 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL)\n");
12672 /* clear migr_rec on disks after successful migration */
12675 memset(super
->migr_rec_buf
, 0, MIGR_REC_BUF_SECTORS
*MAX_SECTOR_SIZE
);
12676 for (d
= super
->disks
; d
; d
= d
->next
) {
12677 if (d
->index
< 0 || is_failed(&d
->disk
))
12679 unsigned long long dsize
;
12681 get_dev_size(d
->fd
, NULL
, &dsize
);
12682 if (lseek64(d
->fd
, dsize
- MIGR_REC_SECTOR_POSITION
*sector_size
,
12684 if ((unsigned int)write(d
->fd
, super
->migr_rec_buf
,
12685 MIGR_REC_BUF_SECTORS
*sector_size
) !=
12686 MIGR_REC_BUF_SECTORS
*sector_size
)
12687 perror("Write migr_rec failed");
12691 /* return '1' if done */
12694 /* After the reshape eliminate size mismatch in metadata.
12695 * Don't update md/component_size here, volume hasn't
12696 * to take whole space. It is allowed by kernel.
12697 * md/component_size will be set propoperly after next assembly.
12699 imsm_fix_size_mismatch(st
, subarray_index
);
12703 /* See Grow.c: abort_reshape() for further explanation */
12704 sysfs_set_num(sra
, NULL
, "suspend_lo", 0x7FFFFFFFFFFFFFFFULL
);
12705 sysfs_set_num(sra
, NULL
, "suspend_hi", 0);
12706 sysfs_set_num(sra
, NULL
, "suspend_lo", 0);
12711 /*******************************************************************************
12712 * Function: calculate_bitmap_min_chunksize
12713 * Description: Calculates the minimal valid bitmap chunk size
12715 * max_bits : indicate how many bits can be used for the bitmap
12716 * data_area_size : the size of the data area covered by the bitmap
12719 * The bitmap chunk size
12720 ******************************************************************************/
12721 static unsigned long long
12722 calculate_bitmap_min_chunksize(unsigned long long max_bits
,
12723 unsigned long long data_area_size
)
12725 unsigned long long min_chunk
=
12726 4096; /* sub-page chunks don't work yet.. */
12727 unsigned long long bits
= data_area_size
/ min_chunk
+ 1;
12729 while (bits
> max_bits
) {
12731 bits
= (bits
+ 1) / 2;
12736 /*******************************************************************************
12737 * Function: calculate_bitmap_chunksize
12738 * Description: Calculates the bitmap chunk size for the given device
12740 * st : supertype information
12741 * dev : device for the bitmap
12744 * The bitmap chunk size
12745 ******************************************************************************/
12746 static unsigned long long calculate_bitmap_chunksize(struct supertype
*st
,
12747 struct imsm_dev
*dev
)
12749 struct intel_super
*super
= st
->sb
;
12750 unsigned long long min_chunksize
;
12751 unsigned long long result
= IMSM_DEFAULT_BITMAP_CHUNKSIZE
;
12752 size_t dev_size
= imsm_dev_size(dev
);
12754 min_chunksize
= calculate_bitmap_min_chunksize(
12755 IMSM_BITMAP_AREA_SIZE
* super
->sector_size
, dev_size
);
12757 if (result
< min_chunksize
)
12758 result
= min_chunksize
;
12763 /*******************************************************************************
12764 * Function: init_bitmap_header
12765 * Description: Initialize the bitmap header structure
12767 * st : supertype information
12768 * bms : bitmap header struct to initialize
12769 * dev : device for the bitmap
12774 ******************************************************************************/
12775 static int init_bitmap_header(struct supertype
*st
, struct bitmap_super_s
*bms
,
12776 struct imsm_dev
*dev
)
12783 bms
->magic
= __cpu_to_le32(BITMAP_MAGIC
);
12784 bms
->version
= __cpu_to_le32(BITMAP_MAJOR_HI
);
12785 bms
->daemon_sleep
= __cpu_to_le32(IMSM_DEFAULT_BITMAP_DAEMON_SLEEP
);
12786 bms
->sync_size
= __cpu_to_le64(IMSM_BITMAP_AREA_SIZE
);
12787 bms
->write_behind
= __cpu_to_le32(0);
12789 uuid_from_super_imsm(st
, vol_uuid
);
12790 memcpy(bms
->uuid
, vol_uuid
, 16);
12792 bms
->chunksize
= calculate_bitmap_chunksize(st
, dev
);
12797 /*******************************************************************************
12798 * Function: validate_internal_bitmap_for_drive
12799 * Description: Verify if the bitmap header for a given drive.
12801 * st : supertype information
12802 * offset : The offset from the beginning of the drive where to look for
12803 * the bitmap header.
12804 * d : the drive info
12809 ******************************************************************************/
12810 static int validate_internal_bitmap_for_drive(struct supertype
*st
,
12811 unsigned long long offset
,
12814 struct intel_super
*super
= st
->sb
;
12817 bitmap_super_t
*bms
;
12825 if (posix_memalign(&read_buf
, MAX_SECTOR_SIZE
, IMSM_BITMAP_HEADER_SIZE
))
12829 if (!is_fd_valid(fd
)) {
12830 fd
= open(d
->devname
, O_RDONLY
, 0);
12832 if (!is_fd_valid(fd
)) {
12833 dprintf("cannot open the device %s\n", d
->devname
);
12838 if (lseek64(fd
, offset
* super
->sector_size
, SEEK_SET
) < 0)
12840 if (read(fd
, read_buf
, IMSM_BITMAP_HEADER_SIZE
) !=
12841 IMSM_BITMAP_HEADER_SIZE
)
12844 uuid_from_super_imsm(st
, vol_uuid
);
12847 if ((bms
->magic
!= __cpu_to_le32(BITMAP_MAGIC
)) ||
12848 (bms
->version
!= __cpu_to_le32(BITMAP_MAJOR_HI
)) ||
12849 (!same_uuid((int *)bms
->uuid
, vol_uuid
, st
->ss
->swapuuid
))) {
12850 dprintf("wrong bitmap header detected\n");
12856 if (!is_fd_valid(d
->fd
))
12865 /*******************************************************************************
12866 * Function: validate_internal_bitmap_imsm
12867 * Description: Verify if the bitmap header is in place and with proper data.
12869 * st : supertype information
12872 * 0 : success or device w/o RWH_BITMAP
12874 ******************************************************************************/
12875 static int validate_internal_bitmap_imsm(struct supertype
*st
)
12877 struct intel_super
*super
= st
->sb
;
12878 struct imsm_dev
*dev
= get_imsm_dev(super
, super
->current_vol
);
12879 unsigned long long offset
;
12882 if (dev
->rwh_policy
!= RWH_BITMAP
)
12885 offset
= get_bitmap_header_sector(super
, super
->current_vol
);
12886 for (d
= super
->disks
; d
; d
= d
->next
) {
12887 if (d
->index
< 0 || is_failed(&d
->disk
))
12890 if (validate_internal_bitmap_for_drive(st
, offset
, d
)) {
12891 pr_err("imsm: bitmap validation failed\n");
12898 /*******************************************************************************
12899 * Function: add_internal_bitmap_imsm
12900 * Description: Mark the volume to use the bitmap and updates the chunk size value.
12902 * st : supertype information
12903 * chunkp : bitmap chunk size
12904 * delay : not used for imsm
12905 * write_behind : not used for imsm
12906 * size : not used for imsm
12907 * may_change : not used for imsm
12908 * amajor : not used for imsm
12913 ******************************************************************************/
12914 static int add_internal_bitmap_imsm(struct supertype
*st
, int *chunkp
,
12915 int delay
, int write_behind
,
12916 unsigned long long size
, int may_change
,
12919 struct intel_super
*super
= st
->sb
;
12920 int vol_idx
= super
->current_vol
;
12921 struct imsm_dev
*dev
;
12923 if (!super
->devlist
|| vol_idx
== -1 || !chunkp
)
12926 dev
= get_imsm_dev(super
, vol_idx
);
12927 dev
->rwh_policy
= RWH_BITMAP
;
12928 *chunkp
= calculate_bitmap_chunksize(st
, dev
);
12932 /*******************************************************************************
12933 * Function: locate_bitmap_imsm
12934 * Description: Seek 'fd' to start of write-intent-bitmap.
12936 * st : supertype information
12937 * fd : file descriptor for the device
12938 * node_num : not used for imsm
12943 ******************************************************************************/
12944 static int locate_bitmap_imsm(struct supertype
*st
, int fd
, int node_num
)
12946 struct intel_super
*super
= st
->sb
;
12947 unsigned long long offset
;
12948 int vol_idx
= super
->current_vol
;
12950 if (!super
->devlist
|| vol_idx
== -1)
12953 offset
= get_bitmap_header_sector(super
, super
->current_vol
);
12954 dprintf("bitmap header offset is %llu\n", offset
);
12956 lseek64(fd
, offset
<< 9, 0);
12961 /*******************************************************************************
12962 * Function: write_init_bitmap_imsm
12963 * Description: Write a bitmap header and prepares the area for the bitmap.
12965 * st : supertype information
12966 * fd : file descriptor for the device
12967 * update : not used for imsm
12972 ******************************************************************************/
12973 static int write_init_bitmap_imsm(struct supertype
*st
, int fd
,
12974 enum bitmap_update update
)
12976 struct intel_super
*super
= st
->sb
;
12977 int vol_idx
= super
->current_vol
;
12979 unsigned long long offset
;
12980 bitmap_super_t bms
= { 0 };
12981 size_t written
= 0;
12986 if (!super
->devlist
|| !super
->sector_size
|| vol_idx
== -1)
12989 struct imsm_dev
*dev
= get_imsm_dev(super
, vol_idx
);
12991 /* first clear the space for bitmap header */
12992 unsigned long long bitmap_area_start
=
12993 get_bitmap_header_sector(super
, vol_idx
);
12995 dprintf("zeroing area start (%llu) and size (%u)\n", bitmap_area_start
,
12996 IMSM_BITMAP_AND_HEADER_SIZE
/ super
->sector_size
);
12997 if (zero_disk_range(fd
, bitmap_area_start
,
12998 IMSM_BITMAP_HEADER_SIZE
/ super
->sector_size
)) {
12999 pr_err("imsm: cannot zeroing the space for the bitmap\n");
13003 /* The bitmap area should be filled with "1"s to perform initial
13006 if (posix_memalign(&buf
, MAX_SECTOR_SIZE
, MAX_SECTOR_SIZE
))
13008 memset(buf
, 0xFF, MAX_SECTOR_SIZE
);
13009 offset
= get_bitmap_sector(super
, vol_idx
);
13010 lseek64(fd
, offset
<< 9, 0);
13011 while (written
< IMSM_BITMAP_AREA_SIZE
) {
13012 to_write
= IMSM_BITMAP_AREA_SIZE
- written
;
13013 if (to_write
> MAX_SECTOR_SIZE
)
13014 to_write
= MAX_SECTOR_SIZE
;
13015 rv_num
= write(fd
, buf
, MAX_SECTOR_SIZE
);
13016 if (rv_num
!= MAX_SECTOR_SIZE
) {
13018 dprintf("cannot initialize bitmap area\n");
13024 /* write a bitmap header */
13025 init_bitmap_header(st
, &bms
, dev
);
13026 memset(buf
, 0, MAX_SECTOR_SIZE
);
13027 memcpy(buf
, &bms
, sizeof(bitmap_super_t
));
13028 if (locate_bitmap_imsm(st
, fd
, 0)) {
13030 dprintf("cannot locate the bitmap\n");
13033 if (write(fd
, buf
, MAX_SECTOR_SIZE
) != MAX_SECTOR_SIZE
) {
13035 dprintf("cannot write the bitmap header\n");
13046 /*******************************************************************************
13047 * Function: is_vol_to_setup_bitmap
13048 * Description: Checks if a bitmap should be activated on the dev.
13050 * info : info about the volume to setup the bitmap
13051 * dev : the device to check against bitmap creation
13054 * 0 : bitmap should be set up on the device
13056 ******************************************************************************/
13057 static int is_vol_to_setup_bitmap(struct mdinfo
*info
, struct imsm_dev
*dev
)
13062 if ((strcmp((char *)dev
->volume
, info
->name
) == 0) &&
13063 (dev
->rwh_policy
== RWH_BITMAP
))
13069 /*******************************************************************************
13070 * Function: set_bitmap_sysfs
13071 * Description: Set the sysfs atributes of a given volume to activate the bitmap.
13073 * info : info about the volume where the bitmap should be setup
13074 * chunksize : bitmap chunk size
13075 * location : location of the bitmap
13080 ******************************************************************************/
13081 static int set_bitmap_sysfs(struct mdinfo
*info
, unsigned long long chunksize
,
13084 /* The bitmap/metadata is set to external to allow changing of value for
13085 * bitmap/location. When external is used, the kernel will treat an offset
13086 * related to the device's first lba (in opposition to the "internal" case
13087 * when this value is related to the beginning of the superblock).
13089 if (sysfs_set_str(info
, NULL
, "bitmap/metadata", "external")) {
13090 dprintf("failed to set bitmap/metadata\n");
13094 /* It can only be changed when no bitmap is active.
13095 * Should be bigger than 512 and must be power of 2.
13096 * It is expecting the value in bytes.
13098 if (sysfs_set_num(info
, NULL
, "bitmap/chunksize",
13099 __cpu_to_le32(chunksize
))) {
13100 dprintf("failed to set bitmap/chunksize\n");
13104 /* It is expecting the value in sectors. */
13105 if (sysfs_set_num(info
, NULL
, "bitmap/space",
13106 __cpu_to_le64(IMSM_BITMAP_AREA_SIZE
))) {
13107 dprintf("failed to set bitmap/space\n");
13111 /* Determines the delay between the bitmap updates.
13112 * It is expecting the value in seconds.
13114 if (sysfs_set_num(info
, NULL
, "bitmap/time_base",
13115 __cpu_to_le64(IMSM_DEFAULT_BITMAP_DAEMON_SLEEP
))) {
13116 dprintf("failed to set bitmap/time_base\n");
13120 /* It is expecting the value in sectors with a sign at the beginning. */
13121 if (sysfs_set_str(info
, NULL
, "bitmap/location", location
)) {
13122 dprintf("failed to set bitmap/location\n");
13129 /*******************************************************************************
13130 * Function: set_bitmap_imsm
13131 * Description: Setup the bitmap for the given volume
13133 * st : supertype information
13134 * info : info about the volume where the bitmap should be setup
13139 ******************************************************************************/
13140 static int set_bitmap_imsm(struct supertype
*st
, struct mdinfo
*info
)
13142 struct intel_super
*super
= st
->sb
;
13143 int prev_current_vol
= super
->current_vol
;
13144 struct imsm_dev
*dev
;
13146 char location
[16] = "";
13147 unsigned long long chunksize
;
13148 struct intel_dev
*dev_it
;
13150 for (dev_it
= super
->devlist
; dev_it
; dev_it
= dev_it
->next
) {
13151 super
->current_vol
= dev_it
->index
;
13152 dev
= get_imsm_dev(super
, super
->current_vol
);
13154 if (is_vol_to_setup_bitmap(info
, dev
)) {
13155 if (validate_internal_bitmap_imsm(st
)) {
13156 dprintf("bitmap header validation failed\n");
13160 chunksize
= calculate_bitmap_chunksize(st
, dev
);
13161 dprintf("chunk size is %llu\n", chunksize
);
13163 snprintf(location
, sizeof(location
), "+%llu",
13164 get_bitmap_sector(super
, super
->current_vol
));
13165 dprintf("bitmap offset is %s\n", location
);
13167 if (set_bitmap_sysfs(info
, chunksize
, location
)) {
13168 dprintf("cannot setup the bitmap\n");
13175 super
->current_vol
= prev_current_vol
;
13179 struct superswitch super_imsm
= {
13180 .examine_super
= examine_super_imsm
,
13181 .brief_examine_super
= brief_examine_super_imsm
,
13182 .brief_examine_subarrays
= brief_examine_subarrays_imsm
,
13183 .export_examine_super
= export_examine_super_imsm
,
13184 .detail_super
= detail_super_imsm
,
13185 .brief_detail_super
= brief_detail_super_imsm
,
13186 .write_init_super
= write_init_super_imsm
,
13187 .validate_geometry
= validate_geometry_imsm
,
13188 .add_to_super
= add_to_super_imsm
,
13189 .remove_from_super
= remove_from_super_imsm
,
13190 .detail_platform
= detail_platform_imsm
,
13191 .export_detail_platform
= export_detail_platform_imsm
,
13192 .kill_subarray
= kill_subarray_imsm
,
13193 .update_subarray
= update_subarray_imsm
,
13194 .load_container
= load_container_imsm
,
13195 .default_geometry
= default_geometry_imsm
,
13196 .test_and_add_drive_policies
= test_and_add_drive_policies_imsm
,
13197 .reshape_super
= imsm_reshape_super
,
13198 .manage_reshape
= imsm_manage_reshape
,
13199 .recover_backup
= recover_backup_imsm
,
13200 .examine_badblocks
= examine_badblocks_imsm
,
13201 .match_home
= match_home_imsm
,
13202 .uuid_from_super
= uuid_from_super_imsm
,
13203 .getinfo_super
= getinfo_super_imsm
,
13204 .getinfo_super_disks
= getinfo_super_disks_imsm
,
13205 .update_super
= update_super_imsm
,
13207 .avail_size
= avail_size_imsm
,
13208 .get_spare_criteria
= get_spare_criteria_imsm
,
13210 .compare_super
= compare_super_imsm
,
13212 .load_super
= load_super_imsm
,
13213 .init_super
= init_super_imsm
,
13214 .store_super
= store_super_imsm
,
13215 .free_super
= free_super_imsm
,
13216 .match_metadata_desc
= match_metadata_desc_imsm
,
13217 .container_content
= container_content_imsm
,
13218 .validate_container
= validate_container_imsm
,
13220 .add_internal_bitmap
= add_internal_bitmap_imsm
,
13221 .locate_bitmap
= locate_bitmap_imsm
,
13222 .write_bitmap
= write_init_bitmap_imsm
,
13223 .set_bitmap
= set_bitmap_imsm
,
13225 .write_init_ppl
= write_init_ppl_imsm
,
13226 .validate_ppl
= validate_ppl_imsm
,
13233 .open_new
= imsm_open_new
,
13234 .set_array_state
= imsm_set_array_state
,
13235 .set_disk
= imsm_set_disk
,
13236 .sync_metadata
= imsm_sync_metadata
,
13237 .activate_spare
= imsm_activate_spare
,
13238 .process_update
= imsm_process_update
,
13239 .prepare_update
= imsm_prepare_update
,
13240 .record_bad_block
= imsm_record_badblock
,
13241 .clear_bad_block
= imsm_clear_badblock
,
13242 .get_bad_blocks
= imsm_get_badblocks
,