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"
31 /* MPB == Metadata Parameter Block */
32 #define MPB_SIGNATURE "Intel Raid ISM Cfg Sig. "
33 #define MPB_SIG_LEN (strlen(MPB_SIGNATURE))
34 #define MPB_VERSION_RAID0 "1.0.00"
35 #define MPB_VERSION_RAID1 "1.1.00"
36 #define MPB_VERSION_MANY_VOLUMES_PER_ARRAY "1.2.00"
37 #define MPB_VERSION_3OR4_DISK_ARRAY "1.2.01"
38 #define MPB_VERSION_RAID5 "1.2.02"
39 #define MPB_VERSION_5OR6_DISK_ARRAY "1.2.04"
40 #define MPB_VERSION_CNG "1.2.06"
41 #define MPB_VERSION_ATTRIBS "1.3.00"
42 #define MAX_SIGNATURE_LENGTH 32
43 #define MAX_RAID_SERIAL_LEN 16
46 #define MPB_ATTRIB_RAID0 __cpu_to_le32(0x00000001)
48 #define MPB_ATTRIB_RAID1 __cpu_to_le32(0x00000002)
50 #define MPB_ATTRIB_RAID10 __cpu_to_le32(0x00000004)
52 #define MPB_ATTRIB_RAID1E __cpu_to_le32(0x00000008)
54 #define MPB_ATTRIB_RAID5 __cpu_to_le32(0x00000010)
55 /* supports RAID CNG */
56 #define MPB_ATTRIB_RAIDCNG __cpu_to_le32(0x00000020)
57 /* supports expanded stripe sizes of 256K, 512K and 1MB */
58 #define MPB_ATTRIB_EXP_STRIPE_SIZE __cpu_to_le32(0x00000040)
60 /* The OROM Support RST Caching of Volumes */
61 #define MPB_ATTRIB_NVM __cpu_to_le32(0x02000000)
62 /* The OROM supports creating disks greater than 2TB */
63 #define MPB_ATTRIB_2TB_DISK __cpu_to_le32(0x04000000)
64 /* The OROM supports Bad Block Management */
65 #define MPB_ATTRIB_BBM __cpu_to_le32(0x08000000)
67 /* THe OROM Supports NVM Caching of Volumes */
68 #define MPB_ATTRIB_NEVER_USE2 __cpu_to_le32(0x10000000)
69 /* The OROM supports creating volumes greater than 2TB */
70 #define MPB_ATTRIB_2TB __cpu_to_le32(0x20000000)
71 /* originally for PMP, now it's wasted b/c. Never use this bit! */
72 #define MPB_ATTRIB_NEVER_USE __cpu_to_le32(0x40000000)
73 /* Verify MPB contents against checksum after reading MPB */
74 #define MPB_ATTRIB_CHECKSUM_VERIFY __cpu_to_le32(0x80000000)
76 /* Define all supported attributes that have to be accepted by mdadm
78 #define MPB_ATTRIB_SUPPORTED (MPB_ATTRIB_CHECKSUM_VERIFY | \
80 MPB_ATTRIB_2TB_DISK | \
85 MPB_ATTRIB_EXP_STRIPE_SIZE | \
88 /* Define attributes that are unused but not harmful */
89 #define MPB_ATTRIB_IGNORED (MPB_ATTRIB_NEVER_USE)
91 #define MPB_SECTOR_CNT 2210
92 #define IMSM_RESERVED_SECTORS 8192
93 #define NUM_BLOCKS_DIRTY_STRIPE_REGION 2048
94 #define SECT_PER_MB_SHIFT 11
95 #define MAX_SECTOR_SIZE 4096
96 #define MULTIPLE_PPL_AREA_SIZE_IMSM (1024 * 1024) /* Size of the whole
101 * Internal Write-intent bitmap is stored in the same area where PPL.
102 * Both features are mutually exclusive, so it is not an issue.
103 * The first 8KiB of the area are reserved and shall not be used.
105 #define IMSM_BITMAP_AREA_RESERVED_SIZE 8192
107 #define IMSM_BITMAP_HEADER_OFFSET (IMSM_BITMAP_AREA_RESERVED_SIZE)
108 #define IMSM_BITMAP_HEADER_SIZE MAX_SECTOR_SIZE
110 #define IMSM_BITMAP_START_OFFSET (IMSM_BITMAP_HEADER_OFFSET + IMSM_BITMAP_HEADER_SIZE)
111 #define IMSM_BITMAP_AREA_SIZE (MULTIPLE_PPL_AREA_SIZE_IMSM - IMSM_BITMAP_START_OFFSET)
112 #define IMSM_BITMAP_AND_HEADER_SIZE (IMSM_BITMAP_AREA_SIZE + IMSM_BITMAP_HEADER_SIZE)
114 #define IMSM_DEFAULT_BITMAP_CHUNKSIZE (64 * 1024 * 1024)
115 #define IMSM_DEFAULT_BITMAP_DAEMON_SLEEP 5
118 * This macro let's us ensure that no-one accidentally
119 * changes the size of a struct
121 #define ASSERT_SIZE(_struct, size) \
122 static inline void __assert_size_##_struct(void) \
126 case (sizeof(struct _struct) == size): break; \
130 /* Disk configuration info. */
131 #define IMSM_MAX_DEVICES 255
133 __u8 serial
[MAX_RAID_SERIAL_LEN
];/* 0xD8 - 0xE7 ascii serial number */
134 __u32 total_blocks_lo
; /* 0xE8 - 0xEB total blocks lo */
135 __u32 scsi_id
; /* 0xEC - 0xEF scsi ID */
136 #define SPARE_DISK __cpu_to_le32(0x01) /* Spare */
137 #define CONFIGURED_DISK __cpu_to_le32(0x02) /* Member of some RaidDev */
138 #define FAILED_DISK __cpu_to_le32(0x04) /* Permanent failure */
139 #define JOURNAL_DISK __cpu_to_le32(0x2000000) /* Device marked as Journaling Drive */
140 __u32 status
; /* 0xF0 - 0xF3 */
141 __u32 owner_cfg_num
; /* which config 0,1,2... owns this disk */
142 __u32 total_blocks_hi
; /* 0xF4 - 0xF5 total blocks hi */
143 #define IMSM_DISK_FILLERS 3
144 __u32 filler
[IMSM_DISK_FILLERS
]; /* 0xF5 - 0x107 MPB_DISK_FILLERS for future expansion */
146 ASSERT_SIZE(imsm_disk
, 48)
148 /* map selector for map managment
154 /* RAID map configuration infos. */
156 __u32 pba_of_lba0_lo
; /* start address of partition */
157 __u32 blocks_per_member_lo
;/* blocks per member */
158 __u32 num_data_stripes_lo
; /* number of data stripes */
159 __u16 blocks_per_strip
;
160 __u8 map_state
; /* Normal, Uninitialized, Degraded, Failed */
161 #define IMSM_T_STATE_NORMAL 0
162 #define IMSM_T_STATE_UNINITIALIZED 1
163 #define IMSM_T_STATE_DEGRADED 2
164 #define IMSM_T_STATE_FAILED 3
166 #define IMSM_T_RAID0 0
167 #define IMSM_T_RAID1 1
168 #define IMSM_T_RAID5 5 /* since metadata version 1.2.02 ? */
169 __u8 num_members
; /* number of member disks */
170 __u8 num_domains
; /* number of parity domains */
171 __u8 failed_disk_num
; /* valid only when state is degraded */
173 __u32 pba_of_lba0_hi
;
174 __u32 blocks_per_member_hi
;
175 __u32 num_data_stripes_hi
;
176 __u32 filler
[4]; /* expansion area */
177 #define IMSM_ORD_REBUILD (1 << 24)
178 __u32 disk_ord_tbl
[1]; /* disk_ord_tbl[num_members],
179 * top byte contains some flags
182 ASSERT_SIZE(imsm_map
, 52)
185 __u32 curr_migr_unit_lo
;
186 __u32 checkpoint_id
; /* id to access curr_migr_unit */
187 __u8 migr_state
; /* Normal or Migrating */
189 #define MIGR_REBUILD 1
190 #define MIGR_VERIFY 2 /* analagous to echo check > sync_action */
191 #define MIGR_GEN_MIGR 3
192 #define MIGR_STATE_CHANGE 4
193 #define MIGR_REPAIR 5
194 __u8 migr_type
; /* Initializing, Rebuilding, ... */
195 #define RAIDVOL_CLEAN 0
196 #define RAIDVOL_DIRTY 1
197 #define RAIDVOL_DSRECORD_VALID 2
199 __u8 fs_state
; /* fast-sync state for CnG (0xff == disabled) */
200 __u16 verify_errors
; /* number of mismatches */
201 __u16 bad_blocks
; /* number of bad blocks during verify */
202 __u32 curr_migr_unit_hi
;
204 struct imsm_map map
[1];
205 /* here comes another one if migr_state */
207 ASSERT_SIZE(imsm_vol
, 84)
210 __u8 volume
[MAX_RAID_SERIAL_LEN
];
213 #define DEV_BOOTABLE __cpu_to_le32(0x01)
214 #define DEV_BOOT_DEVICE __cpu_to_le32(0x02)
215 #define DEV_READ_COALESCING __cpu_to_le32(0x04)
216 #define DEV_WRITE_COALESCING __cpu_to_le32(0x08)
217 #define DEV_LAST_SHUTDOWN_DIRTY __cpu_to_le32(0x10)
218 #define DEV_HIDDEN_AT_BOOT __cpu_to_le32(0x20)
219 #define DEV_CURRENTLY_HIDDEN __cpu_to_le32(0x40)
220 #define DEV_VERIFY_AND_FIX __cpu_to_le32(0x80)
221 #define DEV_MAP_STATE_UNINIT __cpu_to_le32(0x100)
222 #define DEV_NO_AUTO_RECOVERY __cpu_to_le32(0x200)
223 #define DEV_CLONE_N_GO __cpu_to_le32(0x400)
224 #define DEV_CLONE_MAN_SYNC __cpu_to_le32(0x800)
225 #define DEV_CNG_MASTER_DISK_NUM __cpu_to_le32(0x1000)
226 __u32 status
; /* Persistent RaidDev status */
227 __u32 reserved_blocks
; /* Reserved blocks at beginning of volume */
231 __u8 cng_master_disk
;
235 __u16 my_vol_raid_dev_num
; /* Used in Unique volume Id for this RaidDev */
241 /* Unique Volume Id of the NvCache Volume associated with this volume */
242 __u32 nvc_vol_orig_family_num
;
243 __u16 nvc_vol_raid_dev_num
;
246 #define RWH_DISTRIBUTED 1
247 #define RWH_JOURNALING_DRIVE 2
248 #define RWH_MULTIPLE_DISTRIBUTED 3
249 #define RWH_MULTIPLE_PPLS_JOURNALING_DRIVE 4
250 #define RWH_MULTIPLE_OFF 5
252 __u8 rwh_policy
; /* Raid Write Hole Policy */
253 __u8 jd_serial
[MAX_RAID_SERIAL_LEN
]; /* Journal Drive serial number */
256 #define IMSM_DEV_FILLERS 3
257 __u32 filler
[IMSM_DEV_FILLERS
];
260 ASSERT_SIZE(imsm_dev
, 164)
263 __u8 sig
[MAX_SIGNATURE_LENGTH
]; /* 0x00 - 0x1F */
264 __u32 check_sum
; /* 0x20 - 0x23 MPB Checksum */
265 __u32 mpb_size
; /* 0x24 - 0x27 Size of MPB */
266 __u32 family_num
; /* 0x28 - 0x2B Checksum from first time this config was written */
267 __u32 generation_num
; /* 0x2C - 0x2F Incremented each time this array's MPB is written */
268 __u32 error_log_size
; /* 0x30 - 0x33 in bytes */
269 __u32 attributes
; /* 0x34 - 0x37 */
270 __u8 num_disks
; /* 0x38 Number of configured disks */
271 __u8 num_raid_devs
; /* 0x39 Number of configured volumes */
272 __u8 error_log_pos
; /* 0x3A */
273 __u8 fill
[1]; /* 0x3B */
274 __u32 cache_size
; /* 0x3c - 0x40 in mb */
275 __u32 orig_family_num
; /* 0x40 - 0x43 original family num */
276 __u32 pwr_cycle_count
; /* 0x44 - 0x47 simulated power cycle count for array */
277 __u32 bbm_log_size
; /* 0x48 - 0x4B - size of bad Block Mgmt Log in bytes */
278 __u16 num_raid_devs_created
; /* 0x4C - 0x4D Used for generating unique
279 * volume IDs for raid_dev created in this array
282 __u16 filler1
; /* 0x4E - 0x4F */
283 __u64 creation_time
; /* 0x50 - 0x57 Array creation time */
284 #define IMSM_FILLERS 32
285 __u32 filler
[IMSM_FILLERS
]; /* 0x58 - 0xD7 RAID_MPB_FILLERS */
286 struct imsm_disk disk
[1]; /* 0xD8 diskTbl[numDisks] */
287 /* here comes imsm_dev[num_raid_devs] */
288 /* here comes BBM logs */
290 ASSERT_SIZE(imsm_super
, 264)
292 #define BBM_LOG_MAX_ENTRIES 254
293 #define BBM_LOG_MAX_LBA_ENTRY_VAL 256 /* Represents 256 LBAs */
294 #define BBM_LOG_SIGNATURE 0xabadb10c
296 struct bbm_log_block_addr
{
299 } __attribute__ ((__packed__
));
301 struct bbm_log_entry
{
302 __u8 marked_count
; /* Number of blocks marked - 1 */
303 __u8 disk_ordinal
; /* Disk entry within the imsm_super */
304 struct bbm_log_block_addr defective_block_start
;
305 } __attribute__ ((__packed__
));
308 __u32 signature
; /* 0xABADB10C */
310 struct bbm_log_entry marked_block_entries
[BBM_LOG_MAX_ENTRIES
];
312 ASSERT_SIZE(bbm_log
, 2040)
314 static char *map_state_str
[] = { "normal", "uninitialized", "degraded", "failed" };
316 #define BLOCKS_PER_KB (1024/512)
318 #define RAID_DISK_RESERVED_BLOCKS_IMSM_HI 2209
320 #define GEN_MIGR_AREA_SIZE 2048 /* General Migration Copy Area size in blocks */
322 #define MIGR_REC_BUF_SECTORS 1 /* size of migr_record i/o buffer in sectors */
323 #define MIGR_REC_SECTOR_POSITION 1 /* migr_record position offset on disk,
324 * MIGR_REC_BUF_SECTORS <= MIGR_REC_SECTOR_POS
327 #define UNIT_SRC_NORMAL 0 /* Source data for curr_migr_unit must
328 * be recovered using srcMap */
329 #define UNIT_SRC_IN_CP_AREA 1 /* Source data for curr_migr_unit has
330 * already been migrated and must
331 * be recovered from checkpoint area */
333 #define PPL_ENTRY_SPACE (128 * 1024) /* Size of single PPL, without the header */
336 __u32 rec_status
; /* Status used to determine how to restart
337 * migration in case it aborts
339 __u32 curr_migr_unit_lo
; /* 0..numMigrUnits-1 */
340 __u32 family_num
; /* Family number of MPB
341 * containing the RaidDev
342 * that is migrating */
343 __u32 ascending_migr
; /* True if migrating in increasing
345 __u32 blocks_per_unit
; /* Num disk blocks per unit of operation */
346 __u32 dest_depth_per_unit
; /* Num member blocks each destMap
348 * advances per unit-of-operation */
349 __u32 ckpt_area_pba_lo
; /* Pba of first block of ckpt copy area */
350 __u32 dest_1st_member_lba_lo
; /* First member lba on first
351 * stripe of destination */
352 __u32 num_migr_units_lo
; /* Total num migration units-of-op */
353 __u32 post_migr_vol_cap
; /* Size of volume after
354 * migration completes */
355 __u32 post_migr_vol_cap_hi
; /* Expansion space for LBA64 */
356 __u32 ckpt_read_disk_num
; /* Which member disk in destSubMap[0] the
357 * migration ckpt record was read from
358 * (for recovered migrations) */
359 __u32 curr_migr_unit_hi
; /* 0..numMigrUnits-1 high order 32 bits */
360 __u32 ckpt_area_pba_hi
; /* Pba of first block of ckpt copy area
361 * high order 32 bits */
362 __u32 dest_1st_member_lba_hi
; /* First member lba on first stripe of
363 * destination - high order 32 bits */
364 __u32 num_migr_units_hi
; /* Total num migration units-of-op
365 * high order 32 bits */
368 ASSERT_SIZE(migr_record
, 128)
371 * enum imsm_status - internal IMSM return values representation.
372 * @STATUS_OK: function succeeded.
373 * @STATUS_ERROR: General error ocurred (not specified).
375 * Typedefed to imsm_status_t.
377 typedef enum imsm_status
{
378 IMSM_STATUS_ERROR
= -1,
385 * 2: metadata does not match
393 struct md_list
*next
;
396 static __u8
migr_type(struct imsm_dev
*dev
)
398 if (dev
->vol
.migr_type
== MIGR_VERIFY
&&
399 dev
->status
& DEV_VERIFY_AND_FIX
)
402 return dev
->vol
.migr_type
;
405 static void set_migr_type(struct imsm_dev
*dev
, __u8 migr_type
)
407 /* for compatibility with older oroms convert MIGR_REPAIR, into
408 * MIGR_VERIFY w/ DEV_VERIFY_AND_FIX status
410 if (migr_type
== MIGR_REPAIR
) {
411 dev
->vol
.migr_type
= MIGR_VERIFY
;
412 dev
->status
|= DEV_VERIFY_AND_FIX
;
414 dev
->vol
.migr_type
= migr_type
;
415 dev
->status
&= ~DEV_VERIFY_AND_FIX
;
419 static unsigned int sector_count(__u32 bytes
, unsigned int sector_size
)
421 return ROUND_UP(bytes
, sector_size
) / sector_size
;
424 static unsigned int mpb_sectors(struct imsm_super
*mpb
,
425 unsigned int sector_size
)
427 return sector_count(__le32_to_cpu(mpb
->mpb_size
), sector_size
);
431 struct imsm_dev
*dev
;
432 struct intel_dev
*next
;
437 enum sys_dev_type type
;
440 struct intel_hba
*next
;
447 /* internal representation of IMSM metadata */
450 void *buf
; /* O_DIRECT buffer for reading/writing metadata */
451 struct imsm_super
*anchor
; /* immovable parameters */
454 void *migr_rec_buf
; /* buffer for I/O operations */
455 struct migr_record
*migr_rec
; /* migration record */
457 int clean_migration_record_by_mdmon
; /* when reshape is switched to next
458 array, it indicates that mdmon is allowed to clean migration
460 size_t len
; /* size of the 'buf' allocation */
461 size_t extra_space
; /* extra space in 'buf' that is not used yet */
462 void *next_buf
; /* for realloc'ing buf from the manager */
464 int updates_pending
; /* count of pending updates for mdmon */
465 int current_vol
; /* index of raid device undergoing creation */
466 unsigned long long create_offset
; /* common start for 'current_vol' */
467 __u32 random
; /* random data for seeding new family numbers */
468 struct intel_dev
*devlist
;
469 unsigned int sector_size
; /* sector size of used member drives */
473 __u8 serial
[MAX_RAID_SERIAL_LEN
];
476 struct imsm_disk disk
;
479 struct extent
*e
; /* for determining freespace @ create */
480 int raiddisk
; /* slot to fill in autolayout */
482 } *disks
, *current_disk
;
483 struct dl
*disk_mgmt_list
; /* list of disks to add/remove while mdmon
485 struct dl
*missing
; /* disks removed while we weren't looking */
486 struct bbm_log
*bbm_log
;
487 struct intel_hba
*hba
; /* device path of the raid controller for this metadata */
488 const struct imsm_orom
*orom
; /* platform firmware support */
489 struct intel_super
*next
; /* (temp) list for disambiguating family_num */
490 struct md_bb bb
; /* memory for get_bad_blocks call */
494 struct imsm_disk disk
;
495 #define IMSM_UNKNOWN_OWNER (-1)
497 struct intel_disk
*next
;
501 * struct extent - reserved space details.
502 * @start: start offset.
503 * @size: size of reservation, set to 0 for metadata reservation.
504 * @vol: index of the volume, meaningful if &size is set.
507 unsigned long long start
, size
;
511 /* definitions of reshape process types */
512 enum imsm_reshape_type
{
518 /* definition of messages passed to imsm_process_update */
519 enum imsm_update_type
{
520 update_activate_spare
,
524 update_add_remove_disk
,
525 update_reshape_container_disks
,
526 update_reshape_migration
,
528 update_general_migration_checkpoint
,
530 update_prealloc_badblocks_mem
,
534 struct imsm_update_activate_spare
{
535 enum imsm_update_type type
;
539 struct imsm_update_activate_spare
*next
;
545 unsigned long long size
;
552 enum takeover_direction
{
556 struct imsm_update_takeover
{
557 enum imsm_update_type type
;
559 enum takeover_direction direction
;
562 struct imsm_update_reshape
{
563 enum imsm_update_type type
;
567 int new_disks
[1]; /* new_raid_disks - old_raid_disks makedev number */
570 struct imsm_update_reshape_migration
{
571 enum imsm_update_type type
;
574 /* fields for array migration changes
581 int new_disks
[1]; /* new_raid_disks - old_raid_disks makedev number */
584 struct imsm_update_size_change
{
585 enum imsm_update_type type
;
590 struct imsm_update_general_migration_checkpoint
{
591 enum imsm_update_type type
;
592 __u64 curr_migr_unit
;
596 __u8 serial
[MAX_RAID_SERIAL_LEN
];
599 struct imsm_update_create_array
{
600 enum imsm_update_type type
;
605 struct imsm_update_kill_array
{
606 enum imsm_update_type type
;
610 struct imsm_update_rename_array
{
611 enum imsm_update_type type
;
612 __u8 name
[MAX_RAID_SERIAL_LEN
];
616 struct imsm_update_add_remove_disk
{
617 enum imsm_update_type type
;
620 struct imsm_update_prealloc_bb_mem
{
621 enum imsm_update_type type
;
624 struct imsm_update_rwh_policy
{
625 enum imsm_update_type type
;
630 static const char *_sys_dev_type
[] = {
631 [SYS_DEV_UNKNOWN
] = "Unknown",
632 [SYS_DEV_SAS
] = "SAS",
633 [SYS_DEV_SATA
] = "SATA",
634 [SYS_DEV_NVME
] = "NVMe",
635 [SYS_DEV_VMD
] = "VMD",
636 [SYS_DEV_SATA_VMD
] = "SATA VMD"
639 static int no_platform
= -1;
641 static int check_no_platform(void)
643 static const char search
[] = "mdadm.imsm.test=1";
646 if (no_platform
>= 0)
649 if (check_env("IMSM_NO_PLATFORM")) {
653 fp
= fopen("/proc/cmdline", "r");
655 char *l
= conf_line(fp
);
664 if (strcmp(w
, search
) == 0)
670 if (no_platform
>= 0)
677 void imsm_set_no_platform(int v
)
682 const char *get_sys_dev_type(enum sys_dev_type type
)
684 if (type
>= SYS_DEV_MAX
)
685 type
= SYS_DEV_UNKNOWN
;
687 return _sys_dev_type
[type
];
690 static struct intel_hba
* alloc_intel_hba(struct sys_dev
*device
)
692 struct intel_hba
*result
= xmalloc(sizeof(*result
));
694 result
->type
= device
->type
;
695 result
->path
= xstrdup(device
->path
);
697 if (result
->path
&& (result
->pci_id
= strrchr(result
->path
, '/')) != NULL
)
703 static struct intel_hba
* find_intel_hba(struct intel_hba
*hba
, struct sys_dev
*device
)
705 struct intel_hba
*result
;
707 for (result
= hba
; result
; result
= result
->next
) {
708 if (result
->type
== device
->type
&& strcmp(result
->path
, device
->path
) == 0)
714 static int attach_hba_to_super(struct intel_super
*super
, struct sys_dev
*device
)
716 struct intel_hba
*hba
;
718 /* check if disk attached to Intel HBA */
719 hba
= find_intel_hba(super
->hba
, device
);
722 /* Check if HBA is already attached to super */
723 if (super
->hba
== NULL
) {
724 super
->hba
= alloc_intel_hba(device
);
729 /* Intel metadata allows for all disks attached to the same type HBA.
730 * Do not support HBA types mixing
732 if (device
->type
!= hba
->type
)
735 /* Multiple same type HBAs can be used if they share the same OROM */
736 const struct imsm_orom
*device_orom
= get_orom_by_device_id(device
->dev_id
);
738 if (device_orom
!= super
->orom
)
744 hba
->next
= alloc_intel_hba(device
);
748 static struct sys_dev
* find_disk_attached_hba(int fd
, const char *devname
)
750 struct sys_dev
*list
, *elem
;
753 if ((list
= find_intel_devices()) == NULL
)
756 if (!is_fd_valid(fd
))
757 disk_path
= (char *) devname
;
759 disk_path
= diskfd_to_devpath(fd
, 1, NULL
);
764 for (elem
= list
; elem
; elem
= elem
->next
)
765 if (path_attached_to_hba(disk_path
, elem
->path
))
768 if (disk_path
!= devname
)
774 static int find_intel_hba_capability(int fd
, struct intel_super
*super
,
777 static struct supertype
*match_metadata_desc_imsm(char *arg
)
779 struct supertype
*st
;
781 if (strcmp(arg
, "imsm") != 0 &&
782 strcmp(arg
, "default") != 0
786 st
= xcalloc(1, sizeof(*st
));
787 st
->ss
= &super_imsm
;
788 st
->max_devs
= IMSM_MAX_DEVICES
;
789 st
->minor_version
= 0;
794 static __u8
*get_imsm_version(struct imsm_super
*mpb
)
796 return &mpb
->sig
[MPB_SIG_LEN
];
799 /* retrieve a disk directly from the anchor when the anchor is known to be
800 * up-to-date, currently only at load time
802 static struct imsm_disk
*__get_imsm_disk(struct imsm_super
*mpb
, __u8 index
)
804 if (index
>= mpb
->num_disks
)
806 return &mpb
->disk
[index
];
809 /* retrieve the disk description based on a index of the disk
812 static struct dl
*get_imsm_dl_disk(struct intel_super
*super
, __u8 index
)
816 for (d
= super
->disks
; d
; d
= d
->next
)
817 if (d
->index
== index
)
822 /* retrieve a disk from the parsed metadata */
823 static struct imsm_disk
*get_imsm_disk(struct intel_super
*super
, __u8 index
)
827 dl
= get_imsm_dl_disk(super
, index
);
834 /* generate a checksum directly from the anchor when the anchor is known to be
835 * up-to-date, currently only at load or write_super after coalescing
837 static __u32
__gen_imsm_checksum(struct imsm_super
*mpb
)
839 __u32 end
= mpb
->mpb_size
/ sizeof(end
);
840 __u32
*p
= (__u32
*) mpb
;
844 sum
+= __le32_to_cpu(*p
);
848 return sum
- __le32_to_cpu(mpb
->check_sum
);
851 static size_t sizeof_imsm_map(struct imsm_map
*map
)
853 return sizeof(struct imsm_map
) + sizeof(__u32
) * (map
->num_members
- 1);
856 struct imsm_map
*get_imsm_map(struct imsm_dev
*dev
, int second_map
)
858 /* A device can have 2 maps if it is in the middle of a migration.
860 * MAP_0 - we return the first map
861 * MAP_1 - we return the second map if it exists, else NULL
862 * MAP_X - we return the second map if it exists, else the first
864 struct imsm_map
*map
= &dev
->vol
.map
[0];
865 struct imsm_map
*map2
= NULL
;
867 if (dev
->vol
.migr_state
)
868 map2
= (void *)map
+ sizeof_imsm_map(map
);
870 switch (second_map
) {
887 /* return the size of the device.
888 * migr_state increases the returned size if map[0] were to be duplicated
890 static size_t sizeof_imsm_dev(struct imsm_dev
*dev
, int migr_state
)
892 size_t size
= sizeof(*dev
) - sizeof(struct imsm_map
) +
893 sizeof_imsm_map(get_imsm_map(dev
, MAP_0
));
895 /* migrating means an additional map */
896 if (dev
->vol
.migr_state
)
897 size
+= sizeof_imsm_map(get_imsm_map(dev
, MAP_1
));
899 size
+= sizeof_imsm_map(get_imsm_map(dev
, MAP_0
));
904 /* retrieve disk serial number list from a metadata update */
905 static struct disk_info
*get_disk_info(struct imsm_update_create_array
*update
)
908 struct disk_info
*inf
;
910 inf
= u
+ sizeof(*update
) - sizeof(struct imsm_dev
) +
911 sizeof_imsm_dev(&update
->dev
, 0);
917 * __get_imsm_dev() - Get device with index from imsm_super.
918 * @mpb: &imsm_super pointer, not NULL.
919 * @index: Device index.
921 * Function works as non-NULL, aborting in such a case,
922 * when NULL would be returned.
924 * Device index should be in range 0 up to num_raid_devs.
925 * Function assumes the index was already verified.
926 * Index must be valid, otherwise abort() is called.
928 * Return: Pointer to corresponding imsm_dev.
931 static struct imsm_dev
*__get_imsm_dev(struct imsm_super
*mpb
, __u8 index
)
937 if (index
>= mpb
->num_raid_devs
)
940 /* devices start after all disks */
941 offset
= ((void *) &mpb
->disk
[mpb
->num_disks
]) - _mpb
;
943 for (i
= 0; i
<= index
; i
++, offset
+= sizeof_imsm_dev(_mpb
+ offset
, 0))
945 return _mpb
+ offset
;
947 pr_err("cannot find imsm_dev with index %u in imsm_super\n", index
);
952 * get_imsm_dev() - Get device with index from intel_super.
953 * @super: &intel_super pointer, not NULL.
954 * @index: Device index.
956 * Function works as non-NULL, aborting in such a case,
957 * when NULL would be returned.
959 * Device index should be in range 0 up to num_raid_devs.
960 * Function assumes the index was already verified.
961 * Index must be valid, otherwise abort() is called.
963 * Return: Pointer to corresponding imsm_dev.
966 static struct imsm_dev
*get_imsm_dev(struct intel_super
*super
, __u8 index
)
968 struct intel_dev
*dv
;
970 if (index
>= super
->anchor
->num_raid_devs
)
973 for (dv
= super
->devlist
; dv
; dv
= dv
->next
)
974 if (dv
->index
== index
)
977 pr_err("cannot find imsm_dev with index %u in intel_super\n", index
);
981 static inline unsigned long long __le48_to_cpu(const struct bbm_log_block_addr
984 return ((((__u64
)__le32_to_cpu(addr
->dw1
)) << 16) |
985 __le16_to_cpu(addr
->w1
));
988 static inline struct bbm_log_block_addr
__cpu_to_le48(unsigned long long sec
)
990 struct bbm_log_block_addr addr
;
992 addr
.w1
= __cpu_to_le16((__u16
)(sec
& 0xffff));
993 addr
.dw1
= __cpu_to_le32((__u32
)(sec
>> 16) & 0xffffffff);
997 /* get size of the bbm log */
998 static __u32
get_imsm_bbm_log_size(struct bbm_log
*log
)
1000 if (!log
|| log
->entry_count
== 0)
1003 return sizeof(log
->signature
) +
1004 sizeof(log
->entry_count
) +
1005 log
->entry_count
* sizeof(struct bbm_log_entry
);
1008 /* check if bad block is not partially stored in bbm log */
1009 static int is_stored_in_bbm(struct bbm_log
*log
, const __u8 idx
, const unsigned
1010 long long sector
, const int length
, __u32
*pos
)
1014 for (i
= *pos
; i
< log
->entry_count
; i
++) {
1015 struct bbm_log_entry
*entry
= &log
->marked_block_entries
[i
];
1016 unsigned long long bb_start
;
1017 unsigned long long bb_end
;
1019 bb_start
= __le48_to_cpu(&entry
->defective_block_start
);
1020 bb_end
= bb_start
+ (entry
->marked_count
+ 1);
1022 if ((entry
->disk_ordinal
== idx
) && (bb_start
>= sector
) &&
1023 (bb_end
<= sector
+ length
)) {
1031 /* record new bad block in bbm log */
1032 static int record_new_badblock(struct bbm_log
*log
, const __u8 idx
, unsigned
1033 long long sector
, int length
)
1037 struct bbm_log_entry
*entry
= NULL
;
1039 while (is_stored_in_bbm(log
, idx
, sector
, length
, &pos
)) {
1040 struct bbm_log_entry
*e
= &log
->marked_block_entries
[pos
];
1042 if ((e
->marked_count
+ 1 == BBM_LOG_MAX_LBA_ENTRY_VAL
) &&
1043 (__le48_to_cpu(&e
->defective_block_start
) == sector
)) {
1044 sector
+= BBM_LOG_MAX_LBA_ENTRY_VAL
;
1045 length
-= BBM_LOG_MAX_LBA_ENTRY_VAL
;
1054 int cnt
= (length
<= BBM_LOG_MAX_LBA_ENTRY_VAL
) ? length
:
1055 BBM_LOG_MAX_LBA_ENTRY_VAL
;
1056 entry
->defective_block_start
= __cpu_to_le48(sector
);
1057 entry
->marked_count
= cnt
- 1;
1064 new_bb
= ROUND_UP(length
, BBM_LOG_MAX_LBA_ENTRY_VAL
) /
1065 BBM_LOG_MAX_LBA_ENTRY_VAL
;
1066 if (log
->entry_count
+ new_bb
> BBM_LOG_MAX_ENTRIES
)
1069 while (length
> 0) {
1070 int cnt
= (length
<= BBM_LOG_MAX_LBA_ENTRY_VAL
) ? length
:
1071 BBM_LOG_MAX_LBA_ENTRY_VAL
;
1072 struct bbm_log_entry
*entry
=
1073 &log
->marked_block_entries
[log
->entry_count
];
1075 entry
->defective_block_start
= __cpu_to_le48(sector
);
1076 entry
->marked_count
= cnt
- 1;
1077 entry
->disk_ordinal
= idx
;
1088 /* clear all bad blocks for given disk */
1089 static void clear_disk_badblocks(struct bbm_log
*log
, const __u8 idx
)
1093 while (i
< log
->entry_count
) {
1094 struct bbm_log_entry
*entries
= log
->marked_block_entries
;
1096 if (entries
[i
].disk_ordinal
== idx
) {
1097 if (i
< log
->entry_count
- 1)
1098 entries
[i
] = entries
[log
->entry_count
- 1];
1106 /* clear given bad block */
1107 static int clear_badblock(struct bbm_log
*log
, const __u8 idx
, const unsigned
1108 long long sector
, const int length
) {
1111 while (i
< log
->entry_count
) {
1112 struct bbm_log_entry
*entries
= log
->marked_block_entries
;
1114 if ((entries
[i
].disk_ordinal
== idx
) &&
1115 (__le48_to_cpu(&entries
[i
].defective_block_start
) ==
1116 sector
) && (entries
[i
].marked_count
+ 1 == length
)) {
1117 if (i
< log
->entry_count
- 1)
1118 entries
[i
] = entries
[log
->entry_count
- 1];
1128 /* allocate and load BBM log from metadata */
1129 static int load_bbm_log(struct intel_super
*super
)
1131 struct imsm_super
*mpb
= super
->anchor
;
1132 __u32 bbm_log_size
= __le32_to_cpu(mpb
->bbm_log_size
);
1134 super
->bbm_log
= xcalloc(1, sizeof(struct bbm_log
));
1135 if (!super
->bbm_log
)
1139 struct bbm_log
*log
= (void *)mpb
+
1140 __le32_to_cpu(mpb
->mpb_size
) - bbm_log_size
;
1144 if (bbm_log_size
< sizeof(log
->signature
) +
1145 sizeof(log
->entry_count
))
1148 entry_count
= __le32_to_cpu(log
->entry_count
);
1149 if ((__le32_to_cpu(log
->signature
) != BBM_LOG_SIGNATURE
) ||
1150 (entry_count
> BBM_LOG_MAX_ENTRIES
))
1154 sizeof(log
->signature
) + sizeof(log
->entry_count
) +
1155 entry_count
* sizeof(struct bbm_log_entry
))
1158 memcpy(super
->bbm_log
, log
, bbm_log_size
);
1160 super
->bbm_log
->signature
= __cpu_to_le32(BBM_LOG_SIGNATURE
);
1161 super
->bbm_log
->entry_count
= 0;
1167 /* checks if bad block is within volume boundaries */
1168 static int is_bad_block_in_volume(const struct bbm_log_entry
*entry
,
1169 const unsigned long long start_sector
,
1170 const unsigned long long size
)
1172 unsigned long long bb_start
;
1173 unsigned long long bb_end
;
1175 bb_start
= __le48_to_cpu(&entry
->defective_block_start
);
1176 bb_end
= bb_start
+ (entry
->marked_count
+ 1);
1178 if (((bb_start
>= start_sector
) && (bb_start
< start_sector
+ size
)) ||
1179 ((bb_end
>= start_sector
) && (bb_end
<= start_sector
+ size
)))
1185 /* get list of bad blocks on a drive for a volume */
1186 static void get_volume_badblocks(const struct bbm_log
*log
, const __u8 idx
,
1187 const unsigned long long start_sector
,
1188 const unsigned long long size
,
1194 for (i
= 0; i
< log
->entry_count
; i
++) {
1195 const struct bbm_log_entry
*ent
=
1196 &log
->marked_block_entries
[i
];
1197 struct md_bb_entry
*bb
;
1199 if ((ent
->disk_ordinal
== idx
) &&
1200 is_bad_block_in_volume(ent
, start_sector
, size
)) {
1202 if (!bbs
->entries
) {
1203 bbs
->entries
= xmalloc(BBM_LOG_MAX_ENTRIES
*
1209 bb
= &bbs
->entries
[count
++];
1210 bb
->sector
= __le48_to_cpu(&ent
->defective_block_start
);
1211 bb
->length
= ent
->marked_count
+ 1;
1219 * == MAP_0 get first map
1220 * == MAP_1 get second map
1221 * == MAP_X than get map according to the current migr_state
1223 static __u32
get_imsm_ord_tbl_ent(struct imsm_dev
*dev
,
1227 struct imsm_map
*map
;
1229 map
= get_imsm_map(dev
, second_map
);
1231 /* top byte identifies disk under rebuild */
1232 return __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
1235 #define ord_to_idx(ord) (((ord) << 8) >> 8)
1236 static __u32
get_imsm_disk_idx(struct imsm_dev
*dev
, int slot
, int second_map
)
1238 __u32 ord
= get_imsm_ord_tbl_ent(dev
, slot
, second_map
);
1240 return ord_to_idx(ord
);
1243 static void set_imsm_ord_tbl_ent(struct imsm_map
*map
, int slot
, __u32 ord
)
1245 map
->disk_ord_tbl
[slot
] = __cpu_to_le32(ord
);
1248 static int get_imsm_disk_slot(struct imsm_map
*map
, const unsigned int idx
)
1253 for (slot
= 0; slot
< map
->num_members
; slot
++) {
1254 ord
= __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
1255 if (ord_to_idx(ord
) == idx
)
1259 return IMSM_STATUS_ERROR
;
1262 static int get_imsm_raid_level(struct imsm_map
*map
)
1264 if (map
->raid_level
== 1) {
1265 if (map
->num_members
== 2)
1271 return map
->raid_level
;
1275 * get_disk_slot_in_dev() - retrieve disk slot from &imsm_dev.
1276 * @super: &intel_super pointer, not NULL.
1277 * @dev_idx: imsm device index.
1280 * Return: Slot on success, IMSM_STATUS_ERROR otherwise.
1282 static int get_disk_slot_in_dev(struct intel_super
*super
, const __u8 dev_idx
,
1283 const unsigned int idx
)
1285 struct imsm_dev
*dev
= get_imsm_dev(super
, dev_idx
);
1286 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
1288 return get_imsm_disk_slot(map
, idx
);
1291 static int cmp_extent(const void *av
, const void *bv
)
1293 const struct extent
*a
= av
;
1294 const struct extent
*b
= bv
;
1295 if (a
->start
< b
->start
)
1297 if (a
->start
> b
->start
)
1302 static int count_memberships(struct dl
*dl
, struct intel_super
*super
)
1304 int memberships
= 0;
1307 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++)
1308 if (get_disk_slot_in_dev(super
, i
, dl
->index
) >= 0)
1314 static __u32
imsm_min_reserved_sectors(struct intel_super
*super
);
1316 static int split_ull(unsigned long long n
, void *lo
, void *hi
)
1318 if (lo
== 0 || hi
== 0)
1320 __put_unaligned32(__cpu_to_le32((__u32
)n
), lo
);
1321 __put_unaligned32(__cpu_to_le32((n
>> 32)), hi
);
1325 static unsigned long long join_u32(__u32 lo
, __u32 hi
)
1327 return (unsigned long long)__le32_to_cpu(lo
) |
1328 (((unsigned long long)__le32_to_cpu(hi
)) << 32);
1331 static unsigned long long total_blocks(struct imsm_disk
*disk
)
1335 return join_u32(disk
->total_blocks_lo
, disk
->total_blocks_hi
);
1339 * imsm_num_data_members() - get data drives count for an array.
1340 * @map: Map to analyze.
1342 * num_data_members value represents minimal count of drives for level.
1343 * The name of the property could be misleading for RAID5 with asymmetric layout
1344 * because some data required to be calculated from parity.
1345 * The property is extracted from level and num_members value.
1347 * Return: num_data_members value on success, zero otherwise.
1349 static __u8
imsm_num_data_members(struct imsm_map
*map
)
1351 switch (get_imsm_raid_level(map
)) {
1353 return map
->num_members
;
1356 return map
->num_members
/ 2;
1358 return map
->num_members
- 1;
1360 dprintf("unsupported raid level\n");
1365 static unsigned long long pba_of_lba0(struct imsm_map
*map
)
1369 return join_u32(map
->pba_of_lba0_lo
, map
->pba_of_lba0_hi
);
1372 static unsigned long long blocks_per_member(struct imsm_map
*map
)
1376 return join_u32(map
->blocks_per_member_lo
, map
->blocks_per_member_hi
);
1379 static unsigned long long num_data_stripes(struct imsm_map
*map
)
1383 return join_u32(map
->num_data_stripes_lo
, map
->num_data_stripes_hi
);
1386 static unsigned long long vol_curr_migr_unit(struct imsm_dev
*dev
)
1391 return join_u32(dev
->vol
.curr_migr_unit_lo
, dev
->vol
.curr_migr_unit_hi
);
1394 static unsigned long long imsm_dev_size(struct imsm_dev
*dev
)
1398 return join_u32(dev
->size_low
, dev
->size_high
);
1401 static unsigned long long migr_chkp_area_pba(struct migr_record
*migr_rec
)
1403 if (migr_rec
== NULL
)
1405 return join_u32(migr_rec
->ckpt_area_pba_lo
,
1406 migr_rec
->ckpt_area_pba_hi
);
1409 static unsigned long long current_migr_unit(struct migr_record
*migr_rec
)
1411 if (migr_rec
== NULL
)
1413 return join_u32(migr_rec
->curr_migr_unit_lo
,
1414 migr_rec
->curr_migr_unit_hi
);
1417 static unsigned long long migr_dest_1st_member_lba(struct migr_record
*migr_rec
)
1419 if (migr_rec
== NULL
)
1421 return join_u32(migr_rec
->dest_1st_member_lba_lo
,
1422 migr_rec
->dest_1st_member_lba_hi
);
1425 static unsigned long long get_num_migr_units(struct migr_record
*migr_rec
)
1427 if (migr_rec
== NULL
)
1429 return join_u32(migr_rec
->num_migr_units_lo
,
1430 migr_rec
->num_migr_units_hi
);
1433 static void set_total_blocks(struct imsm_disk
*disk
, unsigned long long n
)
1435 split_ull(n
, &disk
->total_blocks_lo
, &disk
->total_blocks_hi
);
1439 * set_num_domains() - Set number of domains for an array.
1440 * @map: Map to be updated.
1442 * num_domains property represents copies count of each data drive, thus make
1443 * it meaningful only for RAID1 and RAID10. IMSM supports two domains for
1446 static void set_num_domains(struct imsm_map
*map
)
1448 int level
= get_imsm_raid_level(map
);
1450 if (level
== 1 || level
== 10)
1451 map
->num_domains
= 2;
1453 map
->num_domains
= 1;
1456 static void set_pba_of_lba0(struct imsm_map
*map
, unsigned long long n
)
1458 split_ull(n
, &map
->pba_of_lba0_lo
, &map
->pba_of_lba0_hi
);
1461 static void set_blocks_per_member(struct imsm_map
*map
, unsigned long long n
)
1463 split_ull(n
, &map
->blocks_per_member_lo
, &map
->blocks_per_member_hi
);
1466 static void set_num_data_stripes(struct imsm_map
*map
, unsigned long long n
)
1468 split_ull(n
, &map
->num_data_stripes_lo
, &map
->num_data_stripes_hi
);
1472 * update_num_data_stripes() - Calculate and update num_data_stripes value.
1473 * @map: map to be updated.
1474 * @dev_size: size of volume.
1476 * num_data_stripes value is addictionally divided by num_domains, therefore for
1477 * levels where num_domains is not 1, nds is a part of real value.
1479 static void update_num_data_stripes(struct imsm_map
*map
,
1480 unsigned long long dev_size
)
1482 unsigned long long nds
= dev_size
/ imsm_num_data_members(map
);
1484 nds
/= map
->num_domains
;
1485 nds
/= map
->blocks_per_strip
;
1486 set_num_data_stripes(map
, nds
);
1489 static void set_vol_curr_migr_unit(struct imsm_dev
*dev
, unsigned long long n
)
1494 split_ull(n
, &dev
->vol
.curr_migr_unit_lo
, &dev
->vol
.curr_migr_unit_hi
);
1497 static void set_imsm_dev_size(struct imsm_dev
*dev
, unsigned long long n
)
1499 split_ull(n
, &dev
->size_low
, &dev
->size_high
);
1502 static void set_migr_chkp_area_pba(struct migr_record
*migr_rec
,
1503 unsigned long long n
)
1505 split_ull(n
, &migr_rec
->ckpt_area_pba_lo
, &migr_rec
->ckpt_area_pba_hi
);
1508 static void set_current_migr_unit(struct migr_record
*migr_rec
,
1509 unsigned long long n
)
1511 split_ull(n
, &migr_rec
->curr_migr_unit_lo
,
1512 &migr_rec
->curr_migr_unit_hi
);
1515 static void set_migr_dest_1st_member_lba(struct migr_record
*migr_rec
,
1516 unsigned long long n
)
1518 split_ull(n
, &migr_rec
->dest_1st_member_lba_lo
,
1519 &migr_rec
->dest_1st_member_lba_hi
);
1522 static void set_num_migr_units(struct migr_record
*migr_rec
,
1523 unsigned long long n
)
1525 split_ull(n
, &migr_rec
->num_migr_units_lo
,
1526 &migr_rec
->num_migr_units_hi
);
1529 static unsigned long long per_dev_array_size(struct imsm_map
*map
)
1531 unsigned long long array_size
= 0;
1536 array_size
= num_data_stripes(map
) * map
->blocks_per_strip
;
1537 if (get_imsm_raid_level(map
) == 1 || get_imsm_raid_level(map
) == 10)
1543 static struct extent
*get_extents(struct intel_super
*super
, struct dl
*dl
,
1544 int get_minimal_reservation
)
1546 /* find a list of used extents on the given physical device */
1547 int memberships
= count_memberships(dl
, super
);
1548 struct extent
*rv
= xcalloc(memberships
+ 1, sizeof(struct extent
));
1549 struct extent
*e
= rv
;
1553 /* trim the reserved area for spares, so they can join any array
1554 * regardless of whether the OROM has assigned sectors from the
1555 * IMSM_RESERVED_SECTORS region
1557 if (dl
->index
== -1 || get_minimal_reservation
)
1558 reservation
= imsm_min_reserved_sectors(super
);
1560 reservation
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
1562 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
1563 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
1564 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
1566 if (get_imsm_disk_slot(map
, dl
->index
) >= 0) {
1567 e
->start
= pba_of_lba0(map
);
1568 e
->size
= per_dev_array_size(map
);
1573 qsort(rv
, memberships
, sizeof(*rv
), cmp_extent
);
1575 /* determine the start of the metadata
1576 * when no raid devices are defined use the default
1577 * ...otherwise allow the metadata to truncate the value
1578 * as is the case with older versions of imsm
1581 struct extent
*last
= &rv
[memberships
- 1];
1582 unsigned long long remainder
;
1584 remainder
= total_blocks(&dl
->disk
) - (last
->start
+ last
->size
);
1585 /* round down to 1k block to satisfy precision of the kernel
1589 /* make sure remainder is still sane */
1590 if (remainder
< (unsigned)ROUND_UP(super
->len
, 512) >> 9)
1591 remainder
= ROUND_UP(super
->len
, 512) >> 9;
1592 if (reservation
> remainder
)
1593 reservation
= remainder
;
1595 e
->start
= total_blocks(&dl
->disk
) - reservation
;
1600 /* try to determine how much space is reserved for metadata from
1601 * the last get_extents() entry, otherwise fallback to the
1604 static __u32
imsm_reserved_sectors(struct intel_super
*super
, struct dl
*dl
)
1610 /* for spares just return a minimal reservation which will grow
1611 * once the spare is picked up by an array
1613 if (dl
->index
== -1)
1614 return MPB_SECTOR_CNT
;
1616 e
= get_extents(super
, dl
, 0);
1618 return MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
1620 /* scroll to last entry */
1621 for (i
= 0; e
[i
].size
; i
++)
1624 rv
= total_blocks(&dl
->disk
) - e
[i
].start
;
1631 static int is_spare(struct imsm_disk
*disk
)
1633 return (disk
->status
& SPARE_DISK
) == SPARE_DISK
;
1636 static int is_configured(struct imsm_disk
*disk
)
1638 return (disk
->status
& CONFIGURED_DISK
) == CONFIGURED_DISK
;
1641 static int is_failed(struct imsm_disk
*disk
)
1643 return (disk
->status
& FAILED_DISK
) == FAILED_DISK
;
1646 static int is_journal(struct imsm_disk
*disk
)
1648 return (disk
->status
& JOURNAL_DISK
) == JOURNAL_DISK
;
1652 * round_member_size_to_mb()- Round given size to closest MiB.
1653 * @size: size to round in sectors.
1655 static inline unsigned long long round_member_size_to_mb(unsigned long long size
)
1657 return (size
>> SECT_PER_MB_SHIFT
) << SECT_PER_MB_SHIFT
;
1661 * round_size_to_mb()- Round given size.
1662 * @array_size: size to round in sectors.
1663 * @disk_count: count of data members.
1665 * Get size per each data member and round it to closest MiB to ensure that data
1666 * splits evenly between members.
1668 * Return: Array size, rounded down.
1670 static inline unsigned long long round_size_to_mb(unsigned long long array_size
,
1671 unsigned int disk_count
)
1673 return round_member_size_to_mb(array_size
/ disk_count
) * disk_count
;
1676 static int able_to_resync(int raid_level
, int missing_disks
)
1678 int max_missing_disks
= 0;
1680 switch (raid_level
) {
1682 max_missing_disks
= 1;
1685 max_missing_disks
= 0;
1687 return missing_disks
<= max_missing_disks
;
1690 /* try to determine how much space is reserved for metadata from
1691 * the last get_extents() entry on the smallest active disk,
1692 * otherwise fallback to the default
1694 static __u32
imsm_min_reserved_sectors(struct intel_super
*super
)
1698 unsigned long long min_active
;
1700 __u32 rv
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
1701 struct dl
*dl
, *dl_min
= NULL
;
1707 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
1710 unsigned long long blocks
= total_blocks(&dl
->disk
);
1711 if (blocks
< min_active
|| min_active
== 0) {
1713 min_active
= blocks
;
1719 /* find last lba used by subarrays on the smallest active disk */
1720 e
= get_extents(super
, dl_min
, 0);
1723 for (i
= 0; e
[i
].size
; i
++)
1726 remainder
= min_active
- e
[i
].start
;
1729 /* to give priority to recovery we should not require full
1730 IMSM_RESERVED_SECTORS from the spare */
1731 rv
= MPB_SECTOR_CNT
+ NUM_BLOCKS_DIRTY_STRIPE_REGION
;
1733 /* if real reservation is smaller use that value */
1734 return (remainder
< rv
) ? remainder
: rv
;
1737 static bool is_gen_migration(struct imsm_dev
*dev
);
1739 #define IMSM_4K_DIV 8
1741 static __u64
blocks_per_migr_unit(struct intel_super
*super
,
1742 struct imsm_dev
*dev
);
1744 static void print_imsm_dev(struct intel_super
*super
,
1745 struct imsm_dev
*dev
,
1751 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
1752 struct imsm_map
*map2
= get_imsm_map(dev
, MAP_1
);
1756 printf("[%.16s]:\n", dev
->volume
);
1757 printf(" Subarray : %d\n", super
->current_vol
);
1758 printf(" UUID : %s\n", uuid
);
1759 printf(" RAID Level : %d", get_imsm_raid_level(map
));
1761 printf(" <-- %d", get_imsm_raid_level(map2
));
1763 printf(" Members : %d", map
->num_members
);
1765 printf(" <-- %d", map2
->num_members
);
1767 printf(" Slots : [");
1768 for (i
= 0; i
< map
->num_members
; i
++) {
1769 ord
= get_imsm_ord_tbl_ent(dev
, i
, MAP_0
);
1770 printf("%s", ord
& IMSM_ORD_REBUILD
? "_" : "U");
1775 for (i
= 0; i
< map2
->num_members
; i
++) {
1776 ord
= get_imsm_ord_tbl_ent(dev
, i
, MAP_1
);
1777 printf("%s", ord
& IMSM_ORD_REBUILD
? "_" : "U");
1782 printf(" Failed disk : ");
1783 if (map
->failed_disk_num
== 0xff)
1784 printf(STR_COMMON_NONE
);
1786 printf("%i", map
->failed_disk_num
);
1788 slot
= get_imsm_disk_slot(map
, disk_idx
);
1790 ord
= get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
);
1791 printf(" This Slot : %d%s\n", slot
,
1792 ord
& IMSM_ORD_REBUILD
? " (out-of-sync)" : "");
1794 printf(" This Slot : ?\n");
1795 printf(" Sector Size : %u\n", super
->sector_size
);
1796 sz
= imsm_dev_size(dev
);
1797 printf(" Array Size : %llu%s\n",
1798 (unsigned long long)sz
* 512 / super
->sector_size
,
1799 human_size(sz
* 512));
1800 sz
= blocks_per_member(map
);
1801 printf(" Per Dev Size : %llu%s\n",
1802 (unsigned long long)sz
* 512 / super
->sector_size
,
1803 human_size(sz
* 512));
1804 printf(" Sector Offset : %llu\n",
1805 pba_of_lba0(map
) * 512 / super
->sector_size
);
1806 printf(" Num Stripes : %llu\n",
1807 num_data_stripes(map
));
1808 printf(" Chunk Size : %u KiB",
1809 __le16_to_cpu(map
->blocks_per_strip
) / 2);
1811 printf(" <-- %u KiB",
1812 __le16_to_cpu(map2
->blocks_per_strip
) / 2);
1814 printf(" Reserved : %d\n", __le32_to_cpu(dev
->reserved_blocks
));
1815 printf(" Migrate State : ");
1816 if (dev
->vol
.migr_state
) {
1817 if (migr_type(dev
) == MIGR_INIT
)
1818 printf("initialize\n");
1819 else if (migr_type(dev
) == MIGR_REBUILD
)
1820 printf("rebuild\n");
1821 else if (migr_type(dev
) == MIGR_VERIFY
)
1823 else if (migr_type(dev
) == MIGR_GEN_MIGR
)
1824 printf("general migration\n");
1825 else if (migr_type(dev
) == MIGR_STATE_CHANGE
)
1826 printf("state change\n");
1827 else if (migr_type(dev
) == MIGR_REPAIR
)
1830 printf("<unknown:%d>\n", migr_type(dev
));
1833 printf(" Map State : %s", map_state_str
[map
->map_state
]);
1834 if (dev
->vol
.migr_state
) {
1835 struct imsm_map
*map
= get_imsm_map(dev
, MAP_1
);
1837 printf(" <-- %s", map_state_str
[map
->map_state
]);
1838 printf("\n Checkpoint : %llu ", vol_curr_migr_unit(dev
));
1839 if (is_gen_migration(dev
) && (slot
> 1 || slot
< 0))
1842 printf("(%llu)", (unsigned long long)
1843 blocks_per_migr_unit(super
, dev
));
1846 printf(" Dirty State : %s\n", (dev
->vol
.dirty
& RAIDVOL_DIRTY
) ?
1848 printf(" RWH Policy : ");
1849 if (dev
->rwh_policy
== RWH_OFF
|| dev
->rwh_policy
== RWH_MULTIPLE_OFF
)
1851 else if (dev
->rwh_policy
== RWH_DISTRIBUTED
)
1852 printf("PPL distributed\n");
1853 else if (dev
->rwh_policy
== RWH_JOURNALING_DRIVE
)
1854 printf("PPL journaling drive\n");
1855 else if (dev
->rwh_policy
== RWH_MULTIPLE_DISTRIBUTED
)
1856 printf("Multiple distributed PPLs\n");
1857 else if (dev
->rwh_policy
== RWH_MULTIPLE_PPLS_JOURNALING_DRIVE
)
1858 printf("Multiple PPLs on journaling drive\n");
1859 else if (dev
->rwh_policy
== RWH_BITMAP
)
1860 printf("Write-intent bitmap\n");
1862 printf("<unknown:%d>\n", dev
->rwh_policy
);
1864 printf(" Volume ID : %u\n", dev
->my_vol_raid_dev_num
);
1867 static void print_imsm_disk(struct imsm_disk
*disk
,
1870 unsigned int sector_size
) {
1871 char str
[MAX_RAID_SERIAL_LEN
+ 1];
1874 if (index
< -1 || !disk
)
1878 snprintf(str
, MAX_RAID_SERIAL_LEN
+ 1, "%s", disk
->serial
);
1880 printf(" Disk%02d Serial : %s\n", index
, str
);
1882 printf(" Disk Serial : %s\n", str
);
1883 printf(" State :%s%s%s%s\n", is_spare(disk
) ? " spare" : "",
1884 is_configured(disk
) ? " active" : "",
1885 is_failed(disk
) ? " failed" : "",
1886 is_journal(disk
) ? " journal" : "");
1887 printf(" Id : %08x\n", __le32_to_cpu(disk
->scsi_id
));
1888 sz
= total_blocks(disk
) - reserved
;
1889 printf(" Usable Size : %llu%s\n",
1890 (unsigned long long)sz
* 512 / sector_size
,
1891 human_size(sz
* 512));
1894 void convert_to_4k_imsm_migr_rec(struct intel_super
*super
)
1896 struct migr_record
*migr_rec
= super
->migr_rec
;
1898 migr_rec
->blocks_per_unit
/= IMSM_4K_DIV
;
1899 migr_rec
->dest_depth_per_unit
/= IMSM_4K_DIV
;
1900 split_ull((join_u32(migr_rec
->post_migr_vol_cap
,
1901 migr_rec
->post_migr_vol_cap_hi
) / IMSM_4K_DIV
),
1902 &migr_rec
->post_migr_vol_cap
, &migr_rec
->post_migr_vol_cap_hi
);
1903 set_migr_chkp_area_pba(migr_rec
,
1904 migr_chkp_area_pba(migr_rec
) / IMSM_4K_DIV
);
1905 set_migr_dest_1st_member_lba(migr_rec
,
1906 migr_dest_1st_member_lba(migr_rec
) / IMSM_4K_DIV
);
1909 void convert_to_4k_imsm_disk(struct imsm_disk
*disk
)
1911 set_total_blocks(disk
, (total_blocks(disk
)/IMSM_4K_DIV
));
1914 void convert_to_4k(struct intel_super
*super
)
1916 struct imsm_super
*mpb
= super
->anchor
;
1917 struct imsm_disk
*disk
;
1919 __u32 bbm_log_size
= __le32_to_cpu(mpb
->bbm_log_size
);
1921 for (i
= 0; i
< mpb
->num_disks
; i
++) {
1922 disk
= __get_imsm_disk(mpb
, i
);
1924 convert_to_4k_imsm_disk(disk
);
1926 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
1927 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
1928 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
1930 set_imsm_dev_size(dev
, imsm_dev_size(dev
)/IMSM_4K_DIV
);
1931 set_vol_curr_migr_unit(dev
,
1932 vol_curr_migr_unit(dev
) / IMSM_4K_DIV
);
1935 set_blocks_per_member(map
, blocks_per_member(map
)/IMSM_4K_DIV
);
1936 map
->blocks_per_strip
/= IMSM_4K_DIV
;
1937 set_pba_of_lba0(map
, pba_of_lba0(map
)/IMSM_4K_DIV
);
1939 if (dev
->vol
.migr_state
) {
1941 map
= get_imsm_map(dev
, MAP_1
);
1942 set_blocks_per_member(map
,
1943 blocks_per_member(map
)/IMSM_4K_DIV
);
1944 map
->blocks_per_strip
/= IMSM_4K_DIV
;
1945 set_pba_of_lba0(map
, pba_of_lba0(map
)/IMSM_4K_DIV
);
1949 struct bbm_log
*log
= (void *)mpb
+
1950 __le32_to_cpu(mpb
->mpb_size
) - bbm_log_size
;
1953 for (i
= 0; i
< log
->entry_count
; i
++) {
1954 struct bbm_log_entry
*entry
=
1955 &log
->marked_block_entries
[i
];
1957 __u8 count
= entry
->marked_count
+ 1;
1958 unsigned long long sector
=
1959 __le48_to_cpu(&entry
->defective_block_start
);
1961 entry
->defective_block_start
=
1962 __cpu_to_le48(sector
/IMSM_4K_DIV
);
1963 entry
->marked_count
= max(count
/IMSM_4K_DIV
, 1) - 1;
1967 mpb
->check_sum
= __gen_imsm_checksum(mpb
);
1970 void examine_migr_rec_imsm(struct intel_super
*super
)
1972 struct migr_record
*migr_rec
= super
->migr_rec
;
1973 struct imsm_super
*mpb
= super
->anchor
;
1976 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
1977 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
1978 struct imsm_map
*map
;
1981 if (is_gen_migration(dev
) == false)
1984 printf("\nMigration Record Information:");
1986 /* first map under migration */
1987 map
= get_imsm_map(dev
, MAP_0
);
1990 slot
= get_imsm_disk_slot(map
, super
->disks
->index
);
1991 if (map
== NULL
|| slot
> 1 || slot
< 0) {
1992 printf(" Empty\n ");
1993 printf("Examine one of first two disks in array\n");
1996 printf("\n Status : ");
1997 if (__le32_to_cpu(migr_rec
->rec_status
) == UNIT_SRC_NORMAL
)
2000 printf("Contains Data\n");
2001 printf(" Current Unit : %llu\n",
2002 current_migr_unit(migr_rec
));
2003 printf(" Family : %u\n",
2004 __le32_to_cpu(migr_rec
->family_num
));
2005 printf(" Ascending : %u\n",
2006 __le32_to_cpu(migr_rec
->ascending_migr
));
2007 printf(" Blocks Per Unit : %u\n",
2008 __le32_to_cpu(migr_rec
->blocks_per_unit
));
2009 printf(" Dest. Depth Per Unit : %u\n",
2010 __le32_to_cpu(migr_rec
->dest_depth_per_unit
));
2011 printf(" Checkpoint Area pba : %llu\n",
2012 migr_chkp_area_pba(migr_rec
));
2013 printf(" First member lba : %llu\n",
2014 migr_dest_1st_member_lba(migr_rec
));
2015 printf(" Total Number of Units : %llu\n",
2016 get_num_migr_units(migr_rec
));
2017 printf(" Size of volume : %llu\n",
2018 join_u32(migr_rec
->post_migr_vol_cap
,
2019 migr_rec
->post_migr_vol_cap_hi
));
2020 printf(" Record was read from : %u\n",
2021 __le32_to_cpu(migr_rec
->ckpt_read_disk_num
));
2027 void convert_from_4k_imsm_migr_rec(struct intel_super
*super
)
2029 struct migr_record
*migr_rec
= super
->migr_rec
;
2031 migr_rec
->blocks_per_unit
*= IMSM_4K_DIV
;
2032 migr_rec
->dest_depth_per_unit
*= IMSM_4K_DIV
;
2033 split_ull((join_u32(migr_rec
->post_migr_vol_cap
,
2034 migr_rec
->post_migr_vol_cap_hi
) * IMSM_4K_DIV
),
2035 &migr_rec
->post_migr_vol_cap
,
2036 &migr_rec
->post_migr_vol_cap_hi
);
2037 set_migr_chkp_area_pba(migr_rec
,
2038 migr_chkp_area_pba(migr_rec
) * IMSM_4K_DIV
);
2039 set_migr_dest_1st_member_lba(migr_rec
,
2040 migr_dest_1st_member_lba(migr_rec
) * IMSM_4K_DIV
);
2043 void convert_from_4k(struct intel_super
*super
)
2045 struct imsm_super
*mpb
= super
->anchor
;
2046 struct imsm_disk
*disk
;
2048 __u32 bbm_log_size
= __le32_to_cpu(mpb
->bbm_log_size
);
2050 for (i
= 0; i
< mpb
->num_disks
; i
++) {
2051 disk
= __get_imsm_disk(mpb
, i
);
2053 set_total_blocks(disk
, (total_blocks(disk
)*IMSM_4K_DIV
));
2056 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
2057 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
2058 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
2060 set_imsm_dev_size(dev
, imsm_dev_size(dev
)*IMSM_4K_DIV
);
2061 set_vol_curr_migr_unit(dev
,
2062 vol_curr_migr_unit(dev
) * IMSM_4K_DIV
);
2065 set_blocks_per_member(map
, blocks_per_member(map
)*IMSM_4K_DIV
);
2066 map
->blocks_per_strip
*= IMSM_4K_DIV
;
2067 set_pba_of_lba0(map
, pba_of_lba0(map
)*IMSM_4K_DIV
);
2069 if (dev
->vol
.migr_state
) {
2071 map
= get_imsm_map(dev
, MAP_1
);
2072 set_blocks_per_member(map
,
2073 blocks_per_member(map
)*IMSM_4K_DIV
);
2074 map
->blocks_per_strip
*= IMSM_4K_DIV
;
2075 set_pba_of_lba0(map
, pba_of_lba0(map
)*IMSM_4K_DIV
);
2079 struct bbm_log
*log
= (void *)mpb
+
2080 __le32_to_cpu(mpb
->mpb_size
) - bbm_log_size
;
2083 for (i
= 0; i
< log
->entry_count
; i
++) {
2084 struct bbm_log_entry
*entry
=
2085 &log
->marked_block_entries
[i
];
2087 __u8 count
= entry
->marked_count
+ 1;
2088 unsigned long long sector
=
2089 __le48_to_cpu(&entry
->defective_block_start
);
2091 entry
->defective_block_start
=
2092 __cpu_to_le48(sector
*IMSM_4K_DIV
);
2093 entry
->marked_count
= count
*IMSM_4K_DIV
- 1;
2097 mpb
->check_sum
= __gen_imsm_checksum(mpb
);
2100 /*******************************************************************************
2101 * function: imsm_check_attributes
2102 * Description: Function checks if features represented by attributes flags
2103 * are supported by mdadm.
2105 * attributes - Attributes read from metadata
2107 * 0 - passed attributes contains unsupported features flags
2108 * 1 - all features are supported
2109 ******************************************************************************/
2110 static int imsm_check_attributes(__u32 attributes
)
2113 __u32 not_supported
= MPB_ATTRIB_SUPPORTED
^0xffffffff;
2115 not_supported
&= ~MPB_ATTRIB_IGNORED
;
2117 not_supported
&= attributes
;
2118 if (not_supported
) {
2119 pr_err("(IMSM): Unsupported attributes : %x\n",
2120 (unsigned)__le32_to_cpu(not_supported
));
2121 if (not_supported
& MPB_ATTRIB_CHECKSUM_VERIFY
) {
2122 dprintf("\t\tMPB_ATTRIB_CHECKSUM_VERIFY \n");
2123 not_supported
^= MPB_ATTRIB_CHECKSUM_VERIFY
;
2125 if (not_supported
& MPB_ATTRIB_2TB
) {
2126 dprintf("\t\tMPB_ATTRIB_2TB\n");
2127 not_supported
^= MPB_ATTRIB_2TB
;
2129 if (not_supported
& MPB_ATTRIB_RAID0
) {
2130 dprintf("\t\tMPB_ATTRIB_RAID0\n");
2131 not_supported
^= MPB_ATTRIB_RAID0
;
2133 if (not_supported
& MPB_ATTRIB_RAID1
) {
2134 dprintf("\t\tMPB_ATTRIB_RAID1\n");
2135 not_supported
^= MPB_ATTRIB_RAID1
;
2137 if (not_supported
& MPB_ATTRIB_RAID10
) {
2138 dprintf("\t\tMPB_ATTRIB_RAID10\n");
2139 not_supported
^= MPB_ATTRIB_RAID10
;
2141 if (not_supported
& MPB_ATTRIB_RAID1E
) {
2142 dprintf("\t\tMPB_ATTRIB_RAID1E\n");
2143 not_supported
^= MPB_ATTRIB_RAID1E
;
2145 if (not_supported
& MPB_ATTRIB_RAID5
) {
2146 dprintf("\t\tMPB_ATTRIB_RAID5\n");
2147 not_supported
^= MPB_ATTRIB_RAID5
;
2149 if (not_supported
& MPB_ATTRIB_RAIDCNG
) {
2150 dprintf("\t\tMPB_ATTRIB_RAIDCNG\n");
2151 not_supported
^= MPB_ATTRIB_RAIDCNG
;
2153 if (not_supported
& MPB_ATTRIB_BBM
) {
2154 dprintf("\t\tMPB_ATTRIB_BBM\n");
2155 not_supported
^= MPB_ATTRIB_BBM
;
2157 if (not_supported
& MPB_ATTRIB_CHECKSUM_VERIFY
) {
2158 dprintf("\t\tMPB_ATTRIB_CHECKSUM_VERIFY (== MPB_ATTRIB_LEGACY)\n");
2159 not_supported
^= MPB_ATTRIB_CHECKSUM_VERIFY
;
2161 if (not_supported
& MPB_ATTRIB_EXP_STRIPE_SIZE
) {
2162 dprintf("\t\tMPB_ATTRIB_EXP_STRIP_SIZE\n");
2163 not_supported
^= MPB_ATTRIB_EXP_STRIPE_SIZE
;
2165 if (not_supported
& MPB_ATTRIB_2TB_DISK
) {
2166 dprintf("\t\tMPB_ATTRIB_2TB_DISK\n");
2167 not_supported
^= MPB_ATTRIB_2TB_DISK
;
2169 if (not_supported
& MPB_ATTRIB_NEVER_USE2
) {
2170 dprintf("\t\tMPB_ATTRIB_NEVER_USE2\n");
2171 not_supported
^= MPB_ATTRIB_NEVER_USE2
;
2173 if (not_supported
& MPB_ATTRIB_NEVER_USE
) {
2174 dprintf("\t\tMPB_ATTRIB_NEVER_USE\n");
2175 not_supported
^= MPB_ATTRIB_NEVER_USE
;
2179 dprintf("(IMSM): Unknown attributes : %x\n", not_supported
);
2187 static void getinfo_super_imsm(struct supertype
*st
, struct mdinfo
*info
, char *map
);
2189 static void examine_super_imsm(struct supertype
*st
, char *homehost
)
2191 struct intel_super
*super
= st
->sb
;
2192 struct imsm_super
*mpb
= super
->anchor
;
2193 char str
[MAX_SIGNATURE_LENGTH
];
2198 __u32 reserved
= imsm_reserved_sectors(super
, super
->disks
);
2200 time_t creation_time
;
2202 strncpy(str
, (char *)mpb
->sig
, MPB_SIG_LEN
);
2203 str
[MPB_SIG_LEN
-1] = '\0';
2204 printf(" Magic : %s\n", str
);
2205 printf(" Version : %s\n", get_imsm_version(mpb
));
2206 printf(" Orig Family : %08x\n", __le32_to_cpu(mpb
->orig_family_num
));
2207 printf(" Family : %08x\n", __le32_to_cpu(mpb
->family_num
));
2208 printf(" Generation : %08x\n", __le32_to_cpu(mpb
->generation_num
));
2209 creation_time
= __le64_to_cpu(mpb
->creation_time
);
2210 printf(" Creation Time : %.24s\n",
2211 creation_time
? ctime(&creation_time
) : "Unknown");
2212 printf(" Attributes : ");
2213 if (imsm_check_attributes(mpb
->attributes
))
2214 printf("All supported\n");
2216 printf("not supported\n");
2217 getinfo_super_imsm(st
, &info
, NULL
);
2218 fname_from_uuid(&info
, nbuf
);
2219 printf(" UUID : %s\n", nbuf
+ 5);
2220 sum
= __le32_to_cpu(mpb
->check_sum
);
2221 printf(" Checksum : %08x %s\n", sum
,
2222 __gen_imsm_checksum(mpb
) == sum
? "correct" : "incorrect");
2223 printf(" MPB Sectors : %d\n", mpb_sectors(mpb
, super
->sector_size
));
2224 printf(" Disks : %d\n", mpb
->num_disks
);
2225 printf(" RAID Devices : %d\n", mpb
->num_raid_devs
);
2226 print_imsm_disk(__get_imsm_disk(mpb
, super
->disks
->index
),
2227 super
->disks
->index
, reserved
, super
->sector_size
);
2228 if (get_imsm_bbm_log_size(super
->bbm_log
)) {
2229 struct bbm_log
*log
= super
->bbm_log
;
2232 printf("Bad Block Management Log:\n");
2233 printf(" Log Size : %d\n", __le32_to_cpu(mpb
->bbm_log_size
));
2234 printf(" Signature : %x\n", __le32_to_cpu(log
->signature
));
2235 printf(" Entry Count : %d\n", __le32_to_cpu(log
->entry_count
));
2237 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
2239 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
2241 super
->current_vol
= i
;
2242 getinfo_super_imsm(st
, &info
, NULL
);
2243 fname_from_uuid(&info
, nbuf
);
2244 print_imsm_dev(super
, dev
, nbuf
+ 5, super
->disks
->index
);
2246 for (i
= 0; i
< mpb
->num_disks
; i
++) {
2247 if (i
== super
->disks
->index
)
2249 print_imsm_disk(__get_imsm_disk(mpb
, i
), i
, reserved
,
2250 super
->sector_size
);
2253 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
2254 if (dl
->index
== -1)
2255 print_imsm_disk(&dl
->disk
, -1, reserved
,
2256 super
->sector_size
);
2258 examine_migr_rec_imsm(super
);
2261 static void brief_examine_super_imsm(struct supertype
*st
, int verbose
)
2263 /* We just write a generic IMSM ARRAY entry */
2267 getinfo_super_imsm(st
, &info
, NULL
);
2268 fname_from_uuid(&info
, nbuf
);
2269 printf("ARRAY metadata=imsm UUID=%s\n", nbuf
+ 5);
2272 static void brief_examine_subarrays_imsm(struct supertype
*st
, int verbose
)
2274 /* We just write a generic IMSM ARRAY entry */
2278 struct intel_super
*super
= st
->sb
;
2281 if (!super
->anchor
->num_raid_devs
)
2284 getinfo_super_imsm(st
, &info
, NULL
);
2285 fname_from_uuid(&info
, nbuf
);
2286 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
2287 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
2289 super
->current_vol
= i
;
2290 getinfo_super_imsm(st
, &info
, NULL
);
2291 fname_from_uuid(&info
, nbuf1
);
2292 printf("ARRAY " DEV_MD_DIR
"%.16s container=%s member=%d UUID=%s\n",
2293 dev
->volume
, nbuf
+ 5, i
, nbuf1
+ 5);
2297 static void export_examine_super_imsm(struct supertype
*st
)
2299 struct intel_super
*super
= st
->sb
;
2300 struct imsm_super
*mpb
= super
->anchor
;
2304 getinfo_super_imsm(st
, &info
, NULL
);
2305 fname_from_uuid(&info
, nbuf
);
2306 printf("MD_METADATA=imsm\n");
2307 printf("MD_LEVEL=container\n");
2308 printf("MD_UUID=%s\n", nbuf
+5);
2309 printf("MD_DEVICES=%u\n", mpb
->num_disks
);
2310 printf("MD_CREATION_TIME=%llu\n", __le64_to_cpu(mpb
->creation_time
));
2313 static void detail_super_imsm(struct supertype
*st
, char *homehost
,
2318 struct intel_super
*super
= st
->sb
;
2319 int temp_vol
= super
->current_vol
;
2322 super
->current_vol
= strtoul(subarray
, NULL
, 10);
2324 getinfo_super_imsm(st
, &info
, NULL
);
2325 fname_from_uuid(&info
, nbuf
);
2326 printf("\n UUID : %s\n", nbuf
+ 5);
2328 super
->current_vol
= temp_vol
;
2331 static void brief_detail_super_imsm(struct supertype
*st
, char *subarray
)
2335 struct intel_super
*super
= st
->sb
;
2336 int temp_vol
= super
->current_vol
;
2339 super
->current_vol
= strtoul(subarray
, NULL
, 10);
2341 getinfo_super_imsm(st
, &info
, NULL
);
2342 fname_from_uuid(&info
, nbuf
);
2343 printf(" UUID=%s", nbuf
+ 5);
2345 super
->current_vol
= temp_vol
;
2348 static int imsm_read_serial(int fd
, char *devname
, __u8
*serial
,
2349 size_t serial_buf_len
);
2350 static void fd2devname(int fd
, char *name
);
2352 static int ahci_enumerate_ports(const char *hba_path
, int port_count
, int host_base
, int verbose
)
2354 /* dump an unsorted list of devices attached to AHCI Intel storage
2355 * controller, as well as non-connected ports
2357 int hba_len
= strlen(hba_path
) + 1;
2362 unsigned long port_mask
= (1 << port_count
) - 1;
2364 if (port_count
> (int)sizeof(port_mask
) * 8) {
2366 pr_err("port_count %d out of range\n", port_count
);
2370 /* scroll through /sys/dev/block looking for devices attached to
2373 dir
= opendir("/sys/dev/block");
2377 for (ent
= readdir(dir
); ent
; ent
= readdir(dir
)) {
2383 char device
[PATH_MAX
];
2388 if (sscanf(ent
->d_name
, "%d:%d", &major
, &minor
) != 2)
2390 path
= devt_to_devpath(makedev(major
, minor
), 1, NULL
);
2393 if (!path_attached_to_hba(path
, hba_path
)) {
2399 /* retrieve the scsi device */
2400 if (!devt_to_devpath(makedev(major
, minor
), 1, device
)) {
2402 pr_err("failed to get device\n");
2406 if (devpath_to_char(device
, "type", buf
, sizeof(buf
), 0)) {
2410 type
= strtoul(buf
, NULL
, 10);
2412 /* if it's not a disk print the vendor and model */
2413 if (!(type
== 0 || type
== 7 || type
== 14)) {
2417 if (devpath_to_char(device
, "vendor", buf
,
2418 sizeof(buf
), 0) == 0) {
2419 strncpy(vendor
, buf
, sizeof(vendor
));
2420 vendor
[sizeof(vendor
) - 1] = '\0';
2421 c
= (char *) &vendor
[sizeof(vendor
) - 1];
2422 while (isspace(*c
) || *c
== '\0')
2427 if (devpath_to_char(device
, "model", buf
,
2428 sizeof(buf
), 0) == 0) {
2429 strncpy(model
, buf
, sizeof(model
));
2430 model
[sizeof(model
) - 1] = '\0';
2431 c
= (char *) &model
[sizeof(model
) - 1];
2432 while (isspace(*c
) || *c
== '\0')
2436 if (vendor
[0] && model
[0])
2437 sprintf(buf
, "%.64s %.64s", vendor
, model
);
2439 switch (type
) { /* numbers from hald/linux/device.c */
2440 case 1: sprintf(buf
, "tape"); break;
2441 case 2: sprintf(buf
, "printer"); break;
2442 case 3: sprintf(buf
, "processor"); break;
2444 case 5: sprintf(buf
, "cdrom"); break;
2445 case 6: sprintf(buf
, "scanner"); break;
2446 case 8: sprintf(buf
, "media_changer"); break;
2447 case 9: sprintf(buf
, "comm"); break;
2448 case 12: sprintf(buf
, "raid"); break;
2449 default: sprintf(buf
, "unknown");
2454 /* chop device path to 'host%d' and calculate the port number */
2455 c
= strchr(&path
[hba_len
], '/');
2458 pr_err("%s - invalid path name\n", path
+ hba_len
);
2463 if ((sscanf(&path
[hba_len
], "ata%d", &port
) == 1) ||
2464 ((sscanf(&path
[hba_len
], "host%d", &port
) == 1)))
2468 *c
= '/'; /* repair the full string */
2469 pr_err("failed to determine port number for %s\n",
2476 /* mark this port as used */
2477 port_mask
&= ~(1 << port
);
2479 /* print out the device information */
2481 printf(" Port%d : - non-disk device (%s) -\n", port
, buf
);
2485 fd
= dev_open(ent
->d_name
, O_RDONLY
);
2486 if (!is_fd_valid(fd
))
2487 printf(" Port%d : - disk info unavailable -\n", port
);
2489 fd2devname(fd
, buf
);
2490 printf(" Port%d : %s", port
, buf
);
2491 if (imsm_read_serial(fd
, NULL
, (__u8
*)buf
,
2493 printf(" (%s)\n", buf
);
2508 for (i
= 0; i
< port_count
; i
++)
2509 if (port_mask
& (1 << i
))
2510 printf(" Port%d : - no device attached -\n", i
);
2516 static int print_nvme_info(struct sys_dev
*hba
)
2521 dir
= opendir("/sys/block/");
2525 for (ent
= readdir(dir
); ent
; ent
= readdir(dir
)) {
2526 char ns_path
[PATH_MAX
];
2527 char cntrl_path
[PATH_MAX
];
2531 if (!strstr(ent
->d_name
, "nvme"))
2534 fd
= open_dev(ent
->d_name
);
2535 if (!is_fd_valid(fd
))
2538 if (!diskfd_to_devpath(fd
, 0, ns_path
) ||
2539 !diskfd_to_devpath(fd
, 1, cntrl_path
))
2542 if (!path_attached_to_hba(cntrl_path
, hba
->path
))
2545 if (!imsm_is_nvme_namespace_supported(fd
, 0))
2548 fd2devname(fd
, buf
);
2549 if (hba
->type
== SYS_DEV_VMD
)
2550 printf(" NVMe under VMD : %s", buf
);
2551 else if (hba
->type
== SYS_DEV_NVME
)
2552 printf(" NVMe Device : %s", buf
);
2554 if (!imsm_read_serial(fd
, NULL
, (__u8
*)buf
,
2556 printf(" (%s)\n", buf
);
2568 static void print_found_intel_controllers(struct sys_dev
*elem
)
2570 for (; elem
; elem
= elem
->next
) {
2571 pr_err("found Intel(R) ");
2572 if (elem
->type
== SYS_DEV_SATA
)
2573 fprintf(stderr
, "SATA ");
2574 else if (elem
->type
== SYS_DEV_SAS
)
2575 fprintf(stderr
, "SAS ");
2576 else if (elem
->type
== SYS_DEV_NVME
)
2577 fprintf(stderr
, "NVMe ");
2579 if (elem
->type
== SYS_DEV_VMD
)
2580 fprintf(stderr
, "VMD domain");
2581 else if (elem
->type
== SYS_DEV_SATA_VMD
)
2582 fprintf(stderr
, "SATA VMD domain");
2584 fprintf(stderr
, "RAID controller");
2587 fprintf(stderr
, " at %s", elem
->pci_id
);
2588 fprintf(stderr
, ".\n");
2593 static int ahci_get_port_count(const char *hba_path
, int *port_count
)
2600 if ((dir
= opendir(hba_path
)) == NULL
)
2603 for (ent
= readdir(dir
); ent
; ent
= readdir(dir
)) {
2606 if ((sscanf(ent
->d_name
, "ata%d", &host
) != 1) &&
2607 ((sscanf(ent
->d_name
, "host%d", &host
) != 1)))
2609 if (*port_count
== 0)
2611 else if (host
< host_base
)
2614 if (host
+ 1 > *port_count
+ host_base
)
2615 *port_count
= host
+ 1 - host_base
;
2621 static void print_imsm_capability(const struct imsm_orom
*orom
)
2623 printf(" Platform : Intel(R) ");
2624 if (orom
->capabilities
== 0 && orom
->driver_features
== 0)
2625 printf("Matrix Storage Manager\n");
2626 else if (imsm_orom_is_enterprise(orom
) && orom
->major_ver
>= 6)
2627 printf("Virtual RAID on CPU\n");
2629 printf("Rapid Storage Technology%s\n",
2630 imsm_orom_is_enterprise(orom
) ? " enterprise" : "");
2631 if (orom
->major_ver
|| orom
->minor_ver
|| orom
->hotfix_ver
|| orom
->build
) {
2632 if (imsm_orom_is_vmd_without_efi(orom
))
2633 printf(" Version : %d.%d\n", orom
->major_ver
,
2636 printf(" Version : %d.%d.%d.%d\n", orom
->major_ver
,
2637 orom
->minor_ver
, orom
->hotfix_ver
, orom
->build
);
2639 printf(" RAID Levels :%s%s%s%s%s\n",
2640 imsm_orom_has_raid0(orom
) ? " raid0" : "",
2641 imsm_orom_has_raid1(orom
) ? " raid1" : "",
2642 imsm_orom_has_raid1e(orom
) ? " raid1e" : "",
2643 imsm_orom_has_raid10(orom
) ? " raid10" : "",
2644 imsm_orom_has_raid5(orom
) ? " raid5" : "");
2645 printf(" Chunk Sizes :%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
2646 imsm_orom_has_chunk(orom
, 2) ? " 2k" : "",
2647 imsm_orom_has_chunk(orom
, 4) ? " 4k" : "",
2648 imsm_orom_has_chunk(orom
, 8) ? " 8k" : "",
2649 imsm_orom_has_chunk(orom
, 16) ? " 16k" : "",
2650 imsm_orom_has_chunk(orom
, 32) ? " 32k" : "",
2651 imsm_orom_has_chunk(orom
, 64) ? " 64k" : "",
2652 imsm_orom_has_chunk(orom
, 128) ? " 128k" : "",
2653 imsm_orom_has_chunk(orom
, 256) ? " 256k" : "",
2654 imsm_orom_has_chunk(orom
, 512) ? " 512k" : "",
2655 imsm_orom_has_chunk(orom
, 1024*1) ? " 1M" : "",
2656 imsm_orom_has_chunk(orom
, 1024*2) ? " 2M" : "",
2657 imsm_orom_has_chunk(orom
, 1024*4) ? " 4M" : "",
2658 imsm_orom_has_chunk(orom
, 1024*8) ? " 8M" : "",
2659 imsm_orom_has_chunk(orom
, 1024*16) ? " 16M" : "",
2660 imsm_orom_has_chunk(orom
, 1024*32) ? " 32M" : "",
2661 imsm_orom_has_chunk(orom
, 1024*64) ? " 64M" : "");
2662 printf(" 2TB volumes :%s supported\n",
2663 (orom
->attr
& IMSM_OROM_ATTR_2TB
)?"":" not");
2664 printf(" 2TB disks :%s supported\n",
2665 (orom
->attr
& IMSM_OROM_ATTR_2TB_DISK
)?"":" not");
2666 printf(" Max Disks : %d\n", orom
->tds
);
2667 printf(" Max Volumes : %d per array, %d per %s\n",
2668 orom
->vpa
, orom
->vphba
,
2669 imsm_orom_is_nvme(orom
) ? "platform" : "controller");
2673 static void print_imsm_capability_export(const struct imsm_orom
*orom
)
2675 printf("MD_FIRMWARE_TYPE=imsm\n");
2676 if (orom
->major_ver
|| orom
->minor_ver
|| orom
->hotfix_ver
|| orom
->build
)
2677 printf("IMSM_VERSION=%d.%d.%d.%d\n", orom
->major_ver
, orom
->minor_ver
,
2678 orom
->hotfix_ver
, orom
->build
);
2679 printf("IMSM_SUPPORTED_RAID_LEVELS=%s%s%s%s%s\n",
2680 imsm_orom_has_raid0(orom
) ? "raid0 " : "",
2681 imsm_orom_has_raid1(orom
) ? "raid1 " : "",
2682 imsm_orom_has_raid1e(orom
) ? "raid1e " : "",
2683 imsm_orom_has_raid5(orom
) ? "raid10 " : "",
2684 imsm_orom_has_raid10(orom
) ? "raid5 " : "");
2685 printf("IMSM_SUPPORTED_CHUNK_SIZES=%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
2686 imsm_orom_has_chunk(orom
, 2) ? "2k " : "",
2687 imsm_orom_has_chunk(orom
, 4) ? "4k " : "",
2688 imsm_orom_has_chunk(orom
, 8) ? "8k " : "",
2689 imsm_orom_has_chunk(orom
, 16) ? "16k " : "",
2690 imsm_orom_has_chunk(orom
, 32) ? "32k " : "",
2691 imsm_orom_has_chunk(orom
, 64) ? "64k " : "",
2692 imsm_orom_has_chunk(orom
, 128) ? "128k " : "",
2693 imsm_orom_has_chunk(orom
, 256) ? "256k " : "",
2694 imsm_orom_has_chunk(orom
, 512) ? "512k " : "",
2695 imsm_orom_has_chunk(orom
, 1024*1) ? "1M " : "",
2696 imsm_orom_has_chunk(orom
, 1024*2) ? "2M " : "",
2697 imsm_orom_has_chunk(orom
, 1024*4) ? "4M " : "",
2698 imsm_orom_has_chunk(orom
, 1024*8) ? "8M " : "",
2699 imsm_orom_has_chunk(orom
, 1024*16) ? "16M " : "",
2700 imsm_orom_has_chunk(orom
, 1024*32) ? "32M " : "",
2701 imsm_orom_has_chunk(orom
, 1024*64) ? "64M " : "");
2702 printf("IMSM_2TB_VOLUMES=%s\n",(orom
->attr
& IMSM_OROM_ATTR_2TB
) ? "yes" : "no");
2703 printf("IMSM_2TB_DISKS=%s\n",(orom
->attr
& IMSM_OROM_ATTR_2TB_DISK
) ? "yes" : "no");
2704 printf("IMSM_MAX_DISKS=%d\n",orom
->tds
);
2705 printf("IMSM_MAX_VOLUMES_PER_ARRAY=%d\n",orom
->vpa
);
2706 printf("IMSM_MAX_VOLUMES_PER_CONTROLLER=%d\n",orom
->vphba
);
2709 static int detail_platform_imsm(int verbose
, int enumerate_only
, char *controller_path
)
2711 /* There are two components to imsm platform support, the ahci SATA
2712 * controller and the option-rom. To find the SATA controller we
2713 * simply look in /sys/bus/pci/drivers/ahci to see if an ahci
2714 * controller with the Intel vendor id is present. This approach
2715 * allows mdadm to leverage the kernel's ahci detection logic, with the
2716 * caveat that if ahci.ko is not loaded mdadm will not be able to
2717 * detect platform raid capabilities. The option-rom resides in a
2718 * platform "Adapter ROM". We scan for its signature to retrieve the
2719 * platform capabilities. If raid support is disabled in the BIOS the
2720 * option-rom capability structure will not be available.
2722 struct sys_dev
*list
, *hba
;
2727 if (enumerate_only
) {
2728 if (check_no_platform())
2730 list
= find_intel_devices();
2733 for (hba
= list
; hba
; hba
= hba
->next
) {
2734 if (find_imsm_capability(hba
)) {
2744 list
= find_intel_devices();
2747 pr_err("no active Intel(R) RAID controller found.\n");
2749 } else if (verbose
> 0)
2750 print_found_intel_controllers(list
);
2752 for (hba
= list
; hba
; hba
= hba
->next
) {
2753 if (controller_path
&& (compare_paths(hba
->path
, controller_path
) != 0))
2755 if (!find_imsm_capability(hba
)) {
2757 pr_err("imsm capabilities not found for controller: %s (type %s)\n",
2758 hba
->type
== SYS_DEV_VMD
|| hba
->type
== SYS_DEV_SATA_VMD
?
2759 vmd_domain_to_controller(hba
, buf
) :
2760 hba
->path
, get_sys_dev_type(hba
->type
));
2766 if (controller_path
&& result
== 1) {
2767 pr_err("no active Intel(R) RAID controller found under %s\n",
2772 const struct orom_entry
*entry
;
2774 for (entry
= orom_entries
; entry
; entry
= entry
->next
) {
2775 if (entry
->type
== SYS_DEV_VMD
) {
2776 print_imsm_capability(&entry
->orom
);
2777 printf(" 3rd party NVMe :%s supported\n",
2778 imsm_orom_has_tpv_support(&entry
->orom
)?"":" not");
2779 for (hba
= list
; hba
; hba
= hba
->next
) {
2780 if (hba
->type
== SYS_DEV_VMD
) {
2782 printf(" I/O Controller : %s (%s)\n",
2783 vmd_domain_to_controller(hba
, buf
), get_sys_dev_type(hba
->type
));
2784 if (print_nvme_info(hba
)) {
2786 pr_err("failed to get devices attached to VMD domain.\n");
2795 print_imsm_capability(&entry
->orom
);
2796 if (entry
->type
== SYS_DEV_NVME
) {
2797 for (hba
= list
; hba
; hba
= hba
->next
) {
2798 if (hba
->type
== SYS_DEV_NVME
)
2799 print_nvme_info(hba
);
2805 struct devid_list
*devid
;
2806 for (devid
= entry
->devid_list
; devid
; devid
= devid
->next
) {
2807 hba
= device_by_id(devid
->devid
);
2811 printf(" I/O Controller : %s (%s)\n",
2812 hba
->path
, get_sys_dev_type(hba
->type
));
2813 if (hba
->type
== SYS_DEV_SATA
|| hba
->type
== SYS_DEV_SATA_VMD
) {
2814 host_base
= ahci_get_port_count(hba
->path
, &port_count
);
2815 if (ahci_enumerate_ports(hba
->path
, port_count
, host_base
, verbose
)) {
2817 pr_err("failed to enumerate ports on %s controller at %s.\n",
2818 get_sys_dev_type(hba
->type
), hba
->pci_id
);
2829 static int export_detail_platform_imsm(int verbose
, char *controller_path
)
2831 struct sys_dev
*list
, *hba
;
2834 list
= find_intel_devices();
2837 pr_err("IMSM_DETAIL_PLATFORM_ERROR=NO_INTEL_DEVICES\n");
2842 for (hba
= list
; hba
; hba
= hba
->next
) {
2843 if (controller_path
&& (compare_paths(hba
->path
,controller_path
) != 0))
2845 if (!find_imsm_capability(hba
) && verbose
> 0) {
2847 pr_err("IMSM_DETAIL_PLATFORM_ERROR=NO_IMSM_CAPABLE_DEVICE_UNDER_%s\n",
2848 hba
->type
== SYS_DEV_VMD
|| hba
->type
== SYS_DEV_SATA_VMD
?
2849 vmd_domain_to_controller(hba
, buf
) : hba
->path
);
2855 const struct orom_entry
*entry
;
2857 for (entry
= orom_entries
; entry
; entry
= entry
->next
) {
2858 if (entry
->type
== SYS_DEV_VMD
|| entry
->type
== SYS_DEV_SATA_VMD
) {
2859 for (hba
= list
; hba
; hba
= hba
->next
)
2860 print_imsm_capability_export(&entry
->orom
);
2863 print_imsm_capability_export(&entry
->orom
);
2869 static int match_home_imsm(struct supertype
*st
, char *homehost
)
2871 /* the imsm metadata format does not specify any host
2872 * identification information. We return -1 since we can never
2873 * confirm nor deny whether a given array is "meant" for this
2874 * host. We rely on compare_super and the 'family_num' fields to
2875 * exclude member disks that do not belong, and we rely on
2876 * mdadm.conf to specify the arrays that should be assembled.
2877 * Auto-assembly may still pick up "foreign" arrays.
2883 static void uuid_from_super_imsm(struct supertype
*st
, int uuid
[4])
2885 /* The uuid returned here is used for:
2886 * uuid to put into bitmap file (Create, Grow)
2887 * uuid for backup header when saving critical section (Grow)
2888 * comparing uuids when re-adding a device into an array
2889 * In these cases the uuid required is that of the data-array,
2890 * not the device-set.
2891 * uuid to recognise same set when adding a missing device back
2892 * to an array. This is a uuid for the device-set.
2894 * For each of these we can make do with a truncated
2895 * or hashed uuid rather than the original, as long as
2897 * In each case the uuid required is that of the data-array,
2898 * not the device-set.
2900 /* imsm does not track uuid's so we synthesis one using sha1 on
2901 * - The signature (Which is constant for all imsm array, but no matter)
2902 * - the orig_family_num of the container
2903 * - the index number of the volume
2904 * - the 'serial' number of the volume.
2905 * Hopefully these are all constant.
2907 struct intel_super
*super
= st
->sb
;
2910 struct sha1_ctx ctx
;
2911 struct imsm_dev
*dev
= NULL
;
2914 /* some mdadm versions failed to set ->orig_family_num, in which
2915 * case fall back to ->family_num. orig_family_num will be
2916 * fixed up with the first metadata update.
2918 family_num
= super
->anchor
->orig_family_num
;
2919 if (family_num
== 0)
2920 family_num
= super
->anchor
->family_num
;
2921 sha1_init_ctx(&ctx
);
2922 sha1_process_bytes(super
->anchor
->sig
, MPB_SIG_LEN
, &ctx
);
2923 sha1_process_bytes(&family_num
, sizeof(__u32
), &ctx
);
2924 if (super
->current_vol
>= 0)
2925 dev
= get_imsm_dev(super
, super
->current_vol
);
2927 __u32 vol
= super
->current_vol
;
2928 sha1_process_bytes(&vol
, sizeof(vol
), &ctx
);
2929 sha1_process_bytes(dev
->volume
, MAX_RAID_SERIAL_LEN
, &ctx
);
2931 sha1_finish_ctx(&ctx
, buf
);
2932 memcpy(uuid
, buf
, 4*4);
2935 static __u32
migr_strip_blocks_resync(struct imsm_dev
*dev
)
2937 /* migr_strip_size when repairing or initializing parity */
2938 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
2939 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
2941 switch (get_imsm_raid_level(map
)) {
2946 return 128*1024 >> 9;
2950 static __u32
migr_strip_blocks_rebuild(struct imsm_dev
*dev
)
2952 /* migr_strip_size when rebuilding a degraded disk, no idea why
2953 * this is different than migr_strip_size_resync(), but it's good
2956 struct imsm_map
*map
= get_imsm_map(dev
, MAP_1
);
2957 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
2959 switch (get_imsm_raid_level(map
)) {
2962 if (map
->num_members
% map
->num_domains
== 0)
2963 return 128*1024 >> 9;
2967 return max((__u32
) 64*1024 >> 9, chunk
);
2969 return 128*1024 >> 9;
2973 static __u32
num_stripes_per_unit_resync(struct imsm_dev
*dev
)
2975 struct imsm_map
*lo
= get_imsm_map(dev
, MAP_0
);
2976 struct imsm_map
*hi
= get_imsm_map(dev
, MAP_1
);
2977 __u32 lo_chunk
= __le32_to_cpu(lo
->blocks_per_strip
);
2978 __u32 hi_chunk
= __le32_to_cpu(hi
->blocks_per_strip
);
2980 return max((__u32
) 1, hi_chunk
/ lo_chunk
);
2983 static __u32
num_stripes_per_unit_rebuild(struct imsm_dev
*dev
)
2985 struct imsm_map
*lo
= get_imsm_map(dev
, MAP_0
);
2986 int level
= get_imsm_raid_level(lo
);
2988 if (level
== 1 || level
== 10) {
2989 struct imsm_map
*hi
= get_imsm_map(dev
, MAP_1
);
2991 return hi
->num_domains
;
2993 return num_stripes_per_unit_resync(dev
);
2996 static unsigned long long calc_component_size(struct imsm_map
*map
,
2997 struct imsm_dev
*dev
)
2999 unsigned long long component_size
;
3000 unsigned long long dev_size
= imsm_dev_size(dev
);
3001 long long calc_dev_size
= 0;
3002 unsigned int member_disks
= imsm_num_data_members(map
);
3004 if (member_disks
== 0)
3007 component_size
= per_dev_array_size(map
);
3008 calc_dev_size
= component_size
* member_disks
;
3010 /* Component size is rounded to 1MB so difference between size from
3011 * metadata and size calculated from num_data_stripes equals up to
3012 * 2048 blocks per each device. If the difference is higher it means
3013 * that array size was expanded and num_data_stripes was not updated.
3015 if (llabs(calc_dev_size
- (long long)dev_size
) >
3016 (1 << SECT_PER_MB_SHIFT
) * member_disks
) {
3017 component_size
= dev_size
/ member_disks
;
3018 dprintf("Invalid num_data_stripes in metadata; expected=%llu, found=%llu\n",
3019 component_size
/ map
->blocks_per_strip
,
3020 num_data_stripes(map
));
3023 return component_size
;
3026 static __u32
parity_segment_depth(struct imsm_dev
*dev
)
3028 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
3029 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
3031 switch(get_imsm_raid_level(map
)) {
3034 return chunk
* map
->num_domains
;
3036 return chunk
* map
->num_members
;
3042 static __u32
map_migr_block(struct imsm_dev
*dev
, __u32 block
)
3044 struct imsm_map
*map
= get_imsm_map(dev
, MAP_1
);
3045 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
3046 __u32 strip
= block
/ chunk
;
3048 switch (get_imsm_raid_level(map
)) {
3051 __u32 vol_strip
= (strip
* map
->num_domains
) + 1;
3052 __u32 vol_stripe
= vol_strip
/ map
->num_members
;
3054 return vol_stripe
* chunk
+ block
% chunk
;
3056 __u32 stripe
= strip
/ (map
->num_members
- 1);
3058 return stripe
* chunk
+ block
% chunk
;
3065 static __u64
blocks_per_migr_unit(struct intel_super
*super
,
3066 struct imsm_dev
*dev
)
3068 /* calculate the conversion factor between per member 'blocks'
3069 * (md/{resync,rebuild}_start) and imsm migration units, return
3070 * 0 for the 'not migrating' and 'unsupported migration' cases
3072 if (!dev
->vol
.migr_state
)
3075 switch (migr_type(dev
)) {
3076 case MIGR_GEN_MIGR
: {
3077 struct migr_record
*migr_rec
= super
->migr_rec
;
3078 return __le32_to_cpu(migr_rec
->blocks_per_unit
);
3083 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
3084 __u32 stripes_per_unit
;
3085 __u32 blocks_per_unit
;
3094 /* yes, this is really the translation of migr_units to
3095 * per-member blocks in the 'resync' case
3097 stripes_per_unit
= num_stripes_per_unit_resync(dev
);
3098 migr_chunk
= migr_strip_blocks_resync(dev
);
3099 disks
= imsm_num_data_members(map
);
3100 blocks_per_unit
= stripes_per_unit
* migr_chunk
* disks
;
3101 stripe
= __le16_to_cpu(map
->blocks_per_strip
) * disks
;
3102 segment
= blocks_per_unit
/ stripe
;
3103 block_rel
= blocks_per_unit
- segment
* stripe
;
3104 parity_depth
= parity_segment_depth(dev
);
3105 block_map
= map_migr_block(dev
, block_rel
);
3106 return block_map
+ parity_depth
* segment
;
3108 case MIGR_REBUILD
: {
3109 __u32 stripes_per_unit
;
3112 stripes_per_unit
= num_stripes_per_unit_rebuild(dev
);
3113 migr_chunk
= migr_strip_blocks_rebuild(dev
);
3114 return migr_chunk
* stripes_per_unit
;
3116 case MIGR_STATE_CHANGE
:
3122 static int imsm_level_to_layout(int level
)
3130 return ALGORITHM_LEFT_ASYMMETRIC
;
3137 /*******************************************************************************
3138 * Function: read_imsm_migr_rec
3139 * Description: Function reads imsm migration record from last sector of disk
3141 * fd : disk descriptor
3142 * super : metadata info
3146 ******************************************************************************/
3147 static int read_imsm_migr_rec(int fd
, struct intel_super
*super
)
3150 unsigned int sector_size
= super
->sector_size
;
3151 unsigned long long dsize
;
3153 get_dev_size(fd
, NULL
, &dsize
);
3154 if (lseek64(fd
, dsize
- (sector_size
*MIGR_REC_SECTOR_POSITION
),
3156 pr_err("Cannot seek to anchor block: %s\n",
3160 if ((unsigned int)read(fd
, super
->migr_rec_buf
,
3161 MIGR_REC_BUF_SECTORS
*sector_size
) !=
3162 MIGR_REC_BUF_SECTORS
*sector_size
) {
3163 pr_err("Cannot read migr record block: %s\n",
3168 if (sector_size
== 4096)
3169 convert_from_4k_imsm_migr_rec(super
);
3175 static struct imsm_dev
*imsm_get_device_during_migration(
3176 struct intel_super
*super
)
3179 struct intel_dev
*dv
;
3181 for (dv
= super
->devlist
; dv
; dv
= dv
->next
) {
3182 if (is_gen_migration(dv
->dev
))
3188 /*******************************************************************************
3189 * Function: load_imsm_migr_rec
3190 * Description: Function reads imsm migration record (it is stored at the last
3193 * super : imsm internal array info
3197 * -2 : no migration in progress
3198 ******************************************************************************/
3199 static int load_imsm_migr_rec(struct intel_super
*super
)
3205 struct imsm_dev
*dev
;
3206 struct imsm_map
*map
;
3210 /* find map under migration */
3211 dev
= imsm_get_device_during_migration(super
);
3212 /* nothing to load,no migration in progress?
3217 map
= get_imsm_map(dev
, MAP_0
);
3221 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
3222 /* skip spare and failed disks
3226 /* read only from one of the first two slots
3228 slot
= get_imsm_disk_slot(map
, dl
->index
);
3229 if (slot
> 1 || slot
< 0)
3232 if (!is_fd_valid(dl
->fd
)) {
3233 sprintf(nm
, "%d:%d", dl
->major
, dl
->minor
);
3234 fd
= dev_open(nm
, O_RDONLY
);
3236 if (is_fd_valid(fd
)) {
3246 if (!is_fd_valid(fd
))
3248 retval
= read_imsm_migr_rec(fd
, super
);
3255 /*******************************************************************************
3256 * function: imsm_create_metadata_checkpoint_update
3257 * Description: It creates update for checkpoint change.
3259 * super : imsm internal array info
3260 * u : pointer to prepared update
3263 * If length is equal to 0, input pointer u contains no update
3264 ******************************************************************************/
3265 static int imsm_create_metadata_checkpoint_update(
3266 struct intel_super
*super
,
3267 struct imsm_update_general_migration_checkpoint
**u
)
3270 int update_memory_size
= 0;
3272 dprintf("(enter)\n");
3278 /* size of all update data without anchor */
3279 update_memory_size
=
3280 sizeof(struct imsm_update_general_migration_checkpoint
);
3282 *u
= xcalloc(1, update_memory_size
);
3284 dprintf("error: cannot get memory\n");
3287 (*u
)->type
= update_general_migration_checkpoint
;
3288 (*u
)->curr_migr_unit
= current_migr_unit(super
->migr_rec
);
3289 dprintf("prepared for %llu\n", (unsigned long long)(*u
)->curr_migr_unit
);
3291 return update_memory_size
;
3294 static void imsm_update_metadata_locally(struct supertype
*st
,
3295 void *buf
, int len
);
3297 /*******************************************************************************
3298 * Function: write_imsm_migr_rec
3299 * Description: Function writes imsm migration record
3300 * (at the last sector of disk)
3302 * super : imsm internal array info
3306 ******************************************************************************/
3307 static int write_imsm_migr_rec(struct supertype
*st
)
3309 struct intel_super
*super
= st
->sb
;
3310 unsigned int sector_size
= super
->sector_size
;
3311 unsigned long long dsize
;
3315 struct imsm_update_general_migration_checkpoint
*u
;
3316 struct imsm_dev
*dev
;
3317 struct imsm_map
*map
;
3319 /* find map under migration */
3320 dev
= imsm_get_device_during_migration(super
);
3321 /* if no migration, write buffer anyway to clear migr_record
3322 * on disk based on first available device
3325 dev
= get_imsm_dev(super
, super
->current_vol
< 0 ? 0 :
3326 super
->current_vol
);
3328 map
= get_imsm_map(dev
, MAP_0
);
3330 if (sector_size
== 4096)
3331 convert_to_4k_imsm_migr_rec(super
);
3332 for (sd
= super
->disks
; sd
; sd
= sd
->next
) {
3335 /* skip failed and spare devices */
3338 /* write to 2 first slots only */
3340 slot
= get_imsm_disk_slot(map
, sd
->index
);
3341 if (map
== NULL
|| slot
> 1 || slot
< 0)
3344 get_dev_size(sd
->fd
, NULL
, &dsize
);
3345 if (lseek64(sd
->fd
, dsize
- (MIGR_REC_SECTOR_POSITION
*
3348 pr_err("Cannot seek to anchor block: %s\n",
3352 if ((unsigned int)write(sd
->fd
, super
->migr_rec_buf
,
3353 MIGR_REC_BUF_SECTORS
*sector_size
) !=
3354 MIGR_REC_BUF_SECTORS
*sector_size
) {
3355 pr_err("Cannot write migr record block: %s\n",
3360 if (sector_size
== 4096)
3361 convert_from_4k_imsm_migr_rec(super
);
3362 /* update checkpoint information in metadata */
3363 len
= imsm_create_metadata_checkpoint_update(super
, &u
);
3365 dprintf("imsm: Cannot prepare update\n");
3368 /* update metadata locally */
3369 imsm_update_metadata_locally(st
, u
, len
);
3370 /* and possibly remotely */
3371 if (st
->update_tail
) {
3372 append_metadata_update(st
, u
, len
);
3373 /* during reshape we do all work inside metadata handler
3374 * manage_reshape(), so metadata update has to be triggered
3377 flush_metadata_updates(st
);
3378 st
->update_tail
= &st
->updates
;
3387 /* spare/missing disks activations are not allowe when
3388 * array/container performs reshape operation, because
3389 * all arrays in container works on the same disks set
3391 int imsm_reshape_blocks_arrays_changes(struct intel_super
*super
)
3394 struct intel_dev
*i_dev
;
3395 struct imsm_dev
*dev
;
3397 /* check whole container
3399 for (i_dev
= super
->devlist
; i_dev
; i_dev
= i_dev
->next
) {
3401 if (is_gen_migration(dev
)) {
3402 /* No repair during any migration in container
3410 static unsigned long long imsm_component_size_alignment_check(int level
,
3412 unsigned int sector_size
,
3413 unsigned long long component_size
)
3415 unsigned int component_size_alignment
;
3417 /* check component size alignment
3419 component_size_alignment
= component_size
% (chunk_size
/sector_size
);
3421 dprintf("(Level: %i, chunk_size = %i, component_size = %llu), component_size_alignment = %u\n",
3422 level
, chunk_size
, component_size
,
3423 component_size_alignment
);
3425 if (component_size_alignment
&& (level
!= 1) && (level
!= UnSet
)) {
3426 dprintf("imsm: reported component size aligned from %llu ",
3428 component_size
-= component_size_alignment
;
3429 dprintf_cont("to %llu (%i).\n",
3430 component_size
, component_size_alignment
);
3433 return component_size
;
3436 /*******************************************************************************
3437 * Function: get_bitmap_header_sector
3438 * Description: Returns the sector where the bitmap header is placed.
3440 * st : supertype information
3441 * dev_idx : index of the device with bitmap
3444 * The sector where the bitmap header is placed
3445 ******************************************************************************/
3446 static unsigned long long get_bitmap_header_sector(struct intel_super
*super
,
3449 struct imsm_dev
*dev
= get_imsm_dev(super
, dev_idx
);
3450 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
3452 if (!super
->sector_size
) {
3453 dprintf("sector size is not set\n");
3457 return pba_of_lba0(map
) + calc_component_size(map
, dev
) +
3458 (IMSM_BITMAP_HEADER_OFFSET
/ super
->sector_size
);
3461 /*******************************************************************************
3462 * Function: get_bitmap_sector
3463 * Description: Returns the sector where the bitmap is placed.
3465 * st : supertype information
3466 * dev_idx : index of the device with bitmap
3469 * The sector where the bitmap is placed
3470 ******************************************************************************/
3471 static unsigned long long get_bitmap_sector(struct intel_super
*super
,
3474 if (!super
->sector_size
) {
3475 dprintf("sector size is not set\n");
3479 return get_bitmap_header_sector(super
, dev_idx
) +
3480 (IMSM_BITMAP_HEADER_SIZE
/ super
->sector_size
);
3483 static unsigned long long get_ppl_sector(struct intel_super
*super
, int dev_idx
)
3485 struct imsm_dev
*dev
= get_imsm_dev(super
, dev_idx
);
3486 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
3488 return pba_of_lba0(map
) +
3489 (num_data_stripes(map
) * map
->blocks_per_strip
);
3492 static void getinfo_super_imsm_volume(struct supertype
*st
, struct mdinfo
*info
, char *dmap
)
3494 struct intel_super
*super
= st
->sb
;
3495 struct migr_record
*migr_rec
= super
->migr_rec
;
3496 struct imsm_dev
*dev
= get_imsm_dev(super
, super
->current_vol
);
3497 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
3498 struct imsm_map
*prev_map
= get_imsm_map(dev
, MAP_1
);
3499 struct imsm_map
*map_to_analyse
= map
;
3501 int map_disks
= info
->array
.raid_disks
;
3503 memset(info
, 0, sizeof(*info
));
3505 map_to_analyse
= prev_map
;
3507 dl
= super
->current_disk
;
3509 info
->container_member
= super
->current_vol
;
3510 info
->array
.raid_disks
= map
->num_members
;
3511 info
->array
.level
= get_imsm_raid_level(map_to_analyse
);
3512 info
->array
.layout
= imsm_level_to_layout(info
->array
.level
);
3513 info
->array
.md_minor
= -1;
3514 info
->array
.ctime
= 0;
3515 info
->array
.utime
= 0;
3516 info
->array
.chunk_size
=
3517 __le16_to_cpu(map_to_analyse
->blocks_per_strip
) << 9;
3518 info
->array
.state
= !(dev
->vol
.dirty
& RAIDVOL_DIRTY
);
3519 info
->custom_array_size
= imsm_dev_size(dev
);
3520 info
->recovery_blocked
= imsm_reshape_blocks_arrays_changes(st
->sb
);
3522 if (is_gen_migration(dev
)) {
3524 * device prev_map should be added if it is in the middle
3529 info
->reshape_active
= 1;
3530 info
->new_level
= get_imsm_raid_level(map
);
3531 info
->new_layout
= imsm_level_to_layout(info
->new_level
);
3532 info
->new_chunk
= __le16_to_cpu(map
->blocks_per_strip
) << 9;
3533 info
->delta_disks
= map
->num_members
- prev_map
->num_members
;
3534 if (info
->delta_disks
) {
3535 /* this needs to be applied to every array
3538 info
->reshape_active
= CONTAINER_RESHAPE
;
3540 /* We shape information that we give to md might have to be
3541 * modify to cope with md's requirement for reshaping arrays.
3542 * For example, when reshaping a RAID0, md requires it to be
3543 * presented as a degraded RAID4.
3544 * Also if a RAID0 is migrating to a RAID5 we need to specify
3545 * the array as already being RAID5, but the 'before' layout
3546 * is a RAID4-like layout.
3548 switch (info
->array
.level
) {
3550 switch(info
->new_level
) {
3552 /* conversion is happening as RAID4 */
3553 info
->array
.level
= 4;
3554 info
->array
.raid_disks
+= 1;
3557 /* conversion is happening as RAID5 */
3558 info
->array
.level
= 5;
3559 info
->array
.layout
= ALGORITHM_PARITY_N
;
3560 info
->delta_disks
-= 1;
3563 /* FIXME error message */
3564 info
->array
.level
= UnSet
;
3570 info
->new_level
= UnSet
;
3571 info
->new_layout
= UnSet
;
3572 info
->new_chunk
= info
->array
.chunk_size
;
3573 info
->delta_disks
= 0;
3577 info
->disk
.major
= dl
->major
;
3578 info
->disk
.minor
= dl
->minor
;
3579 info
->disk
.number
= dl
->index
;
3580 info
->disk
.raid_disk
= get_imsm_disk_slot(map_to_analyse
,
3584 info
->data_offset
= pba_of_lba0(map_to_analyse
);
3585 info
->component_size
= calc_component_size(map
, dev
);
3586 info
->component_size
= imsm_component_size_alignment_check(
3588 info
->array
.chunk_size
,
3590 info
->component_size
);
3591 info
->bb
.supported
= 1;
3593 memset(info
->uuid
, 0, sizeof(info
->uuid
));
3594 info
->recovery_start
= MaxSector
;
3596 if (info
->array
.level
== 5 &&
3597 (dev
->rwh_policy
== RWH_DISTRIBUTED
||
3598 dev
->rwh_policy
== RWH_MULTIPLE_DISTRIBUTED
)) {
3599 info
->consistency_policy
= CONSISTENCY_POLICY_PPL
;
3600 info
->ppl_sector
= get_ppl_sector(super
, super
->current_vol
);
3601 if (dev
->rwh_policy
== RWH_MULTIPLE_DISTRIBUTED
)
3602 info
->ppl_size
= MULTIPLE_PPL_AREA_SIZE_IMSM
>> 9;
3604 info
->ppl_size
= (PPL_HEADER_SIZE
+ PPL_ENTRY_SPACE
)
3606 } else if (info
->array
.level
<= 0) {
3607 info
->consistency_policy
= CONSISTENCY_POLICY_NONE
;
3609 if (dev
->rwh_policy
== RWH_BITMAP
) {
3610 info
->bitmap_offset
= get_bitmap_sector(super
, super
->current_vol
);
3611 info
->consistency_policy
= CONSISTENCY_POLICY_BITMAP
;
3613 info
->consistency_policy
= CONSISTENCY_POLICY_RESYNC
;
3617 info
->reshape_progress
= 0;
3618 info
->resync_start
= MaxSector
;
3619 if ((map_to_analyse
->map_state
== IMSM_T_STATE_UNINITIALIZED
||
3620 !(info
->array
.state
& 1)) &&
3621 imsm_reshape_blocks_arrays_changes(super
) == 0) {
3622 info
->resync_start
= 0;
3624 if (dev
->vol
.migr_state
) {
3625 switch (migr_type(dev
)) {
3628 __u64 blocks_per_unit
= blocks_per_migr_unit(super
,
3630 __u64 units
= vol_curr_migr_unit(dev
);
3632 info
->resync_start
= blocks_per_unit
* units
;
3635 case MIGR_GEN_MIGR
: {
3636 __u64 blocks_per_unit
= blocks_per_migr_unit(super
,
3638 __u64 units
= current_migr_unit(migr_rec
);
3641 if (__le32_to_cpu(migr_rec
->ascending_migr
) &&
3643 (get_num_migr_units(migr_rec
)-1)) &&
3644 (super
->migr_rec
->rec_status
==
3645 __cpu_to_le32(UNIT_SRC_IN_CP_AREA
)))
3648 info
->reshape_progress
= blocks_per_unit
* units
;
3650 dprintf("IMSM: General Migration checkpoint : %llu (%llu) -> read reshape progress : %llu\n",
3651 (unsigned long long)units
,
3652 (unsigned long long)blocks_per_unit
,
3653 info
->reshape_progress
);
3655 used_disks
= imsm_num_data_members(prev_map
);
3656 if (used_disks
> 0) {
3657 info
->custom_array_size
= per_dev_array_size(map
) *
3662 /* we could emulate the checkpointing of
3663 * 'sync_action=check' migrations, but for now
3664 * we just immediately complete them
3667 /* this is handled by container_content_imsm() */
3668 case MIGR_STATE_CHANGE
:
3669 /* FIXME handle other migrations */
3671 /* we are not dirty, so... */
3672 info
->resync_start
= MaxSector
;
3676 strncpy(info
->name
, (char *) dev
->volume
, MAX_RAID_SERIAL_LEN
);
3677 info
->name
[MAX_RAID_SERIAL_LEN
] = 0;
3679 info
->array
.major_version
= -1;
3680 info
->array
.minor_version
= -2;
3681 sprintf(info
->text_version
, "/%s/%d", st
->container_devnm
, info
->container_member
);
3682 info
->safe_mode_delay
= 4000; /* 4 secs like the Matrix driver */
3683 uuid_from_super_imsm(st
, info
->uuid
);
3687 for (i
=0; i
<map_disks
; i
++) {
3689 if (i
< info
->array
.raid_disks
) {
3690 struct imsm_disk
*dsk
;
3691 j
= get_imsm_disk_idx(dev
, i
, MAP_X
);
3692 dsk
= get_imsm_disk(super
, j
);
3693 if (dsk
&& (dsk
->status
& CONFIGURED_DISK
))
3700 static __u8
imsm_check_degraded(struct intel_super
*super
, struct imsm_dev
*dev
,
3701 int failed
, int look_in_map
);
3703 static int imsm_count_failed(struct intel_super
*super
, struct imsm_dev
*dev
,
3706 static void manage_second_map(struct intel_super
*super
, struct imsm_dev
*dev
)
3708 if (is_gen_migration(dev
)) {
3711 struct imsm_map
*map2
= get_imsm_map(dev
, MAP_1
);
3713 failed
= imsm_count_failed(super
, dev
, MAP_1
);
3714 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_1
);
3715 if (map2
->map_state
!= map_state
) {
3716 map2
->map_state
= map_state
;
3717 super
->updates_pending
++;
3722 static struct imsm_disk
*get_imsm_missing(struct intel_super
*super
, __u8 index
)
3726 for (d
= super
->missing
; d
; d
= d
->next
)
3727 if (d
->index
== index
)
3732 static void getinfo_super_imsm(struct supertype
*st
, struct mdinfo
*info
, char *map
)
3734 struct intel_super
*super
= st
->sb
;
3735 struct imsm_disk
*disk
;
3736 int map_disks
= info
->array
.raid_disks
;
3737 int max_enough
= -1;
3739 struct imsm_super
*mpb
;
3741 if (super
->current_vol
>= 0) {
3742 getinfo_super_imsm_volume(st
, info
, map
);
3745 memset(info
, 0, sizeof(*info
));
3747 /* Set raid_disks to zero so that Assemble will always pull in valid
3750 info
->array
.raid_disks
= 0;
3751 info
->array
.level
= LEVEL_CONTAINER
;
3752 info
->array
.layout
= 0;
3753 info
->array
.md_minor
= -1;
3754 info
->array
.ctime
= 0; /* N/A for imsm */
3755 info
->array
.utime
= 0;
3756 info
->array
.chunk_size
= 0;
3758 info
->disk
.major
= 0;
3759 info
->disk
.minor
= 0;
3760 info
->disk
.raid_disk
= -1;
3761 info
->reshape_active
= 0;
3762 info
->array
.major_version
= -1;
3763 info
->array
.minor_version
= -2;
3764 strcpy(info
->text_version
, "imsm");
3765 info
->safe_mode_delay
= 0;
3766 info
->disk
.number
= -1;
3767 info
->disk
.state
= 0;
3769 info
->recovery_start
= MaxSector
;
3770 info
->recovery_blocked
= imsm_reshape_blocks_arrays_changes(st
->sb
);
3771 info
->bb
.supported
= 1;
3773 /* do we have the all the insync disks that we expect? */
3774 mpb
= super
->anchor
;
3775 info
->events
= __le32_to_cpu(mpb
->generation_num
);
3777 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
3778 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
3779 int failed
, enough
, j
, missing
= 0;
3780 struct imsm_map
*map
;
3783 failed
= imsm_count_failed(super
, dev
, MAP_0
);
3784 state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
3785 map
= get_imsm_map(dev
, MAP_0
);
3787 /* any newly missing disks?
3788 * (catches single-degraded vs double-degraded)
3790 for (j
= 0; j
< map
->num_members
; j
++) {
3791 __u32 ord
= get_imsm_ord_tbl_ent(dev
, j
, MAP_0
);
3792 __u32 idx
= ord_to_idx(ord
);
3794 if (super
->disks
&& super
->disks
->index
== (int)idx
)
3795 info
->disk
.raid_disk
= j
;
3797 if (!(ord
& IMSM_ORD_REBUILD
) &&
3798 get_imsm_missing(super
, idx
)) {
3804 if (state
== IMSM_T_STATE_FAILED
)
3806 else if (state
== IMSM_T_STATE_DEGRADED
&&
3807 (state
!= map
->map_state
|| missing
))
3809 else /* we're normal, or already degraded */
3811 if (is_gen_migration(dev
) && missing
) {
3812 /* during general migration we need all disks
3813 * that process is running on.
3814 * No new missing disk is allowed.
3818 /* no more checks necessary
3822 /* in the missing/failed disk case check to see
3823 * if at least one array is runnable
3825 max_enough
= max(max_enough
, enough
);
3828 info
->container_enough
= max_enough
;
3831 __u32 reserved
= imsm_reserved_sectors(super
, super
->disks
);
3833 disk
= &super
->disks
->disk
;
3834 info
->data_offset
= total_blocks(&super
->disks
->disk
) - reserved
;
3835 info
->component_size
= reserved
;
3836 info
->disk
.state
= is_configured(disk
) ? (1 << MD_DISK_ACTIVE
) : 0;
3837 /* we don't change info->disk.raid_disk here because
3838 * this state will be finalized in mdmon after we have
3839 * found the 'most fresh' version of the metadata
3841 info
->disk
.state
|= is_failed(disk
) ? (1 << MD_DISK_FAULTY
) : 0;
3842 info
->disk
.state
|= (is_spare(disk
) || is_journal(disk
)) ?
3843 0 : (1 << MD_DISK_SYNC
);
3846 /* only call uuid_from_super_imsm when this disk is part of a populated container,
3847 * ->compare_super may have updated the 'num_raid_devs' field for spares
3849 if (info
->disk
.state
& (1 << MD_DISK_SYNC
) || super
->anchor
->num_raid_devs
)
3850 uuid_from_super_imsm(st
, info
->uuid
);
3852 memcpy(info
->uuid
, uuid_zero
, sizeof(uuid_zero
));
3854 /* I don't know how to compute 'map' on imsm, so use safe default */
3857 for (i
= 0; i
< map_disks
; i
++)
3863 /* allocates memory and fills disk in mdinfo structure
3864 * for each disk in array */
3865 struct mdinfo
*getinfo_super_disks_imsm(struct supertype
*st
)
3867 struct mdinfo
*mddev
;
3868 struct intel_super
*super
= st
->sb
;
3869 struct imsm_disk
*disk
;
3872 if (!super
|| !super
->disks
)
3875 mddev
= xcalloc(1, sizeof(*mddev
));
3879 tmp
= xcalloc(1, sizeof(*tmp
));
3881 tmp
->next
= mddev
->devs
;
3883 tmp
->disk
.number
= count
++;
3884 tmp
->disk
.major
= dl
->major
;
3885 tmp
->disk
.minor
= dl
->minor
;
3886 tmp
->disk
.state
= is_configured(disk
) ?
3887 (1 << MD_DISK_ACTIVE
) : 0;
3888 tmp
->disk
.state
|= is_failed(disk
) ? (1 << MD_DISK_FAULTY
) : 0;
3889 tmp
->disk
.state
|= is_spare(disk
) ? 0 : (1 << MD_DISK_SYNC
);
3890 tmp
->disk
.raid_disk
= -1;
3896 static int update_super_imsm(struct supertype
*st
, struct mdinfo
*info
,
3897 enum update_opt update
, char *devname
,
3898 int verbose
, int uuid_set
, char *homehost
)
3900 /* For 'assemble' and 'force' we need to return non-zero if any
3901 * change was made. For others, the return value is ignored.
3902 * Update options are:
3903 * force-one : This device looks a bit old but needs to be included,
3904 * update age info appropriately.
3905 * assemble: clear any 'faulty' flag to allow this device to
3907 * force-array: Array is degraded but being forced, mark it clean
3908 * if that will be needed to assemble it.
3910 * newdev: not used ????
3911 * grow: Array has gained a new device - this is currently for
3913 * resync: mark as dirty so a resync will happen.
3914 * name: update the name - preserving the homehost
3915 * uuid: Change the uuid of the array to match watch is given
3917 * Following are not relevant for this imsm:
3918 * sparc2.2 : update from old dodgey metadata
3919 * super-minor: change the preferred_minor number
3920 * summaries: update redundant counters.
3921 * homehost: update the recorded homehost
3922 * _reshape_progress: record new reshape_progress position.
3925 struct intel_super
*super
= st
->sb
;
3926 struct imsm_super
*mpb
;
3928 /* we can only update container info */
3929 if (!super
|| super
->current_vol
>= 0 || !super
->anchor
)
3932 mpb
= super
->anchor
;
3936 /* We take this to mean that the family_num should be updated.
3937 * However that is much smaller than the uuid so we cannot really
3938 * allow an explicit uuid to be given. And it is hard to reliably
3940 * So if !uuid_set we know the current uuid is random and just used
3941 * the first 'int' and copy it to the other 3 positions.
3942 * Otherwise we require the 4 'int's to be the same as would be the
3943 * case if we are using a random uuid. So an explicit uuid will be
3944 * accepted as long as all for ints are the same... which shouldn't hurt
3947 info
->uuid
[1] = info
->uuid
[2] = info
->uuid
[3] = info
->uuid
[0];
3950 if (info
->uuid
[0] != info
->uuid
[1] ||
3951 info
->uuid
[1] != info
->uuid
[2] ||
3952 info
->uuid
[2] != info
->uuid
[3])
3958 mpb
->orig_family_num
= info
->uuid
[0];
3960 case UOPT_SPEC_ASSEMBLE
:
3968 /* successful update? recompute checksum */
3970 mpb
->check_sum
= __le32_to_cpu(__gen_imsm_checksum(mpb
));
3975 static size_t disks_to_mpb_size(int disks
)
3979 size
= sizeof(struct imsm_super
);
3980 size
+= (disks
- 1) * sizeof(struct imsm_disk
);
3981 size
+= 2 * sizeof(struct imsm_dev
);
3982 /* up to 2 maps per raid device (-2 for imsm_maps in imsm_dev */
3983 size
+= (4 - 2) * sizeof(struct imsm_map
);
3984 /* 4 possible disk_ord_tbl's */
3985 size
+= 4 * (disks
- 1) * sizeof(__u32
);
3986 /* maximum bbm log */
3987 size
+= sizeof(struct bbm_log
);
3992 static __u64
avail_size_imsm(struct supertype
*st
, __u64 devsize
,
3993 unsigned long long data_offset
)
3995 if (devsize
< (MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
))
3998 return devsize
- (MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
);
4001 static void free_devlist(struct intel_super
*super
)
4003 struct intel_dev
*dv
;
4005 while (super
->devlist
) {
4006 dv
= super
->devlist
->next
;
4007 free(super
->devlist
->dev
);
4008 free(super
->devlist
);
4009 super
->devlist
= dv
;
4013 static void imsm_copy_dev(struct imsm_dev
*dest
, struct imsm_dev
*src
)
4015 memcpy(dest
, src
, sizeof_imsm_dev(src
, 0));
4018 static int compare_super_imsm(struct supertype
*st
, struct supertype
*tst
,
4022 * 0 same, or first was empty, and second was copied
4023 * 1 sb are different
4025 struct intel_super
*first
= st
->sb
;
4026 struct intel_super
*sec
= tst
->sb
;
4034 /* in platform dependent environment test if the disks
4035 * use the same Intel hba
4036 * if not on Intel hba at all, allow anything.
4037 * doesn't check HBAs if num_raid_devs is not set, as it means
4038 * it is a free floating spare, and all spares regardless of HBA type
4039 * will fall into separate container during the assembly
4041 if (first
->hba
&& sec
->hba
&& first
->anchor
->num_raid_devs
!= 0) {
4042 if (first
->hba
->type
!= sec
->hba
->type
) {
4044 pr_err("HBAs of devices do not match %s != %s\n",
4045 get_sys_dev_type(first
->hba
->type
),
4046 get_sys_dev_type(sec
->hba
->type
));
4049 if (first
->orom
!= sec
->orom
) {
4051 pr_err("HBAs of devices do not match %s != %s\n",
4052 first
->hba
->pci_id
, sec
->hba
->pci_id
);
4057 if (first
->anchor
->num_raid_devs
> 0 &&
4058 sec
->anchor
->num_raid_devs
> 0) {
4059 /* Determine if these disks might ever have been
4060 * related. Further disambiguation can only take place
4061 * in load_super_imsm_all
4063 __u32 first_family
= first
->anchor
->orig_family_num
;
4064 __u32 sec_family
= sec
->anchor
->orig_family_num
;
4066 if (memcmp(first
->anchor
->sig
, sec
->anchor
->sig
,
4067 MAX_SIGNATURE_LENGTH
) != 0)
4070 if (first_family
== 0)
4071 first_family
= first
->anchor
->family_num
;
4072 if (sec_family
== 0)
4073 sec_family
= sec
->anchor
->family_num
;
4075 if (first_family
!= sec_family
)
4080 /* if an anchor does not have num_raid_devs set then it is a free
4081 * floating spare. don't assosiate spare with any array, as during assembly
4082 * spares shall fall into separate container, from which they can be moved
4085 if (first
->anchor
->num_raid_devs
^ sec
->anchor
->num_raid_devs
)
4091 static void fd2devname(int fd
, char *name
)
4099 snprintf(name
, MAX_RAID_SERIAL_LEN
, "/dev/%s", nm
);
4102 static int nvme_get_serial(int fd
, void *buf
, size_t buf_len
)
4104 char path
[PATH_MAX
];
4105 char *name
= fd2kname(fd
);
4110 if (strncmp(name
, "nvme", 4) != 0)
4113 if (!diskfd_to_devpath(fd
, 1, path
))
4116 return devpath_to_char(path
, "serial", buf
, buf_len
, 0);
4119 extern int scsi_get_serial(int fd
, void *buf
, size_t buf_len
);
4121 static int imsm_read_serial(int fd
, char *devname
,
4122 __u8
*serial
, size_t serial_buf_len
)
4131 memset(buf
, 0, sizeof(buf
));
4133 if (check_env("IMSM_DEVNAME_AS_SERIAL")) {
4134 memset(serial
, 0, serial_buf_len
);
4135 fd2devname(fd
, (char *) serial
);
4139 rv
= nvme_get_serial(fd
, buf
, sizeof(buf
));
4142 rv
= scsi_get_serial(fd
, buf
, sizeof(buf
));
4146 pr_err("Failed to retrieve serial for %s\n",
4151 /* trim all whitespace and non-printable characters and convert
4154 for (i
= 0, dest
= buf
; i
< sizeof(buf
) && buf
[i
]; i
++) {
4157 /* ':' is reserved for use in placeholder serial
4158 * numbers for missing disks
4169 if (len
> serial_buf_len
) {
4170 /* truncate leading characters */
4171 dest
+= len
- serial_buf_len
;
4172 len
= serial_buf_len
;
4175 memset(serial
, 0, serial_buf_len
);
4176 memcpy(serial
, dest
, len
);
4181 static int serialcmp(__u8
*s1
, __u8
*s2
)
4183 return strncmp((char *) s1
, (char *) s2
, MAX_RAID_SERIAL_LEN
);
4186 static void serialcpy(__u8
*dest
, __u8
*src
)
4188 strncpy((char *) dest
, (char *) src
, MAX_RAID_SERIAL_LEN
);
4191 static struct dl
*serial_to_dl(__u8
*serial
, struct intel_super
*super
)
4195 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
4196 if (serialcmp(dl
->serial
, serial
) == 0)
4202 static struct imsm_disk
*
4203 __serial_to_disk(__u8
*serial
, struct imsm_super
*mpb
, int *idx
)
4207 for (i
= 0; i
< mpb
->num_disks
; i
++) {
4208 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, i
);
4210 if (serialcmp(disk
->serial
, serial
) == 0) {
4221 load_imsm_disk(int fd
, struct intel_super
*super
, char *devname
, int keep_fd
)
4223 struct imsm_disk
*disk
;
4228 __u8 serial
[MAX_RAID_SERIAL_LEN
];
4230 rv
= imsm_read_serial(fd
, devname
, serial
, MAX_RAID_SERIAL_LEN
);
4235 dl
= xcalloc(1, sizeof(*dl
));
4238 dl
->major
= major(stb
.st_rdev
);
4239 dl
->minor
= minor(stb
.st_rdev
);
4240 dl
->next
= super
->disks
;
4241 dl
->fd
= keep_fd
? fd
: -1;
4242 assert(super
->disks
== NULL
);
4244 serialcpy(dl
->serial
, serial
);
4247 fd2devname(fd
, name
);
4249 dl
->devname
= xstrdup(devname
);
4251 dl
->devname
= xstrdup(name
);
4253 /* look up this disk's index in the current anchor */
4254 disk
= __serial_to_disk(dl
->serial
, super
->anchor
, &dl
->index
);
4257 /* only set index on disks that are a member of a
4258 * populated contianer, i.e. one with raid_devs
4260 if (is_failed(&dl
->disk
))
4262 else if (is_spare(&dl
->disk
) || is_journal(&dl
->disk
))
4269 /* When migrating map0 contains the 'destination' state while map1
4270 * contains the current state. When not migrating map0 contains the
4271 * current state. This routine assumes that map[0].map_state is set to
4272 * the current array state before being called.
4274 * Migration is indicated by one of the following states
4275 * 1/ Idle (migr_state=0 map0state=normal||unitialized||degraded||failed)
4276 * 2/ Initialize (migr_state=1 migr_type=MIGR_INIT map0state=normal
4277 * map1state=unitialized)
4278 * 3/ Repair (Resync) (migr_state=1 migr_type=MIGR_REPAIR map0state=normal
4280 * 4/ Rebuild (migr_state=1 migr_type=MIGR_REBUILD map0state=normal
4281 * map1state=degraded)
4282 * 5/ Migration (mig_state=1 migr_type=MIGR_GEN_MIGR map0state=normal
4285 static void migrate(struct imsm_dev
*dev
, struct intel_super
*super
,
4286 __u8 to_state
, int migr_type
)
4288 struct imsm_map
*dest
;
4289 struct imsm_map
*src
= get_imsm_map(dev
, MAP_0
);
4291 dev
->vol
.migr_state
= 1;
4292 set_migr_type(dev
, migr_type
);
4293 set_vol_curr_migr_unit(dev
, 0);
4294 dest
= get_imsm_map(dev
, MAP_1
);
4296 /* duplicate and then set the target end state in map[0] */
4297 memcpy(dest
, src
, sizeof_imsm_map(src
));
4298 if (migr_type
== MIGR_GEN_MIGR
) {
4302 for (i
= 0; i
< src
->num_members
; i
++) {
4303 ord
= __le32_to_cpu(src
->disk_ord_tbl
[i
]);
4304 set_imsm_ord_tbl_ent(src
, i
, ord_to_idx(ord
));
4308 if (migr_type
== MIGR_GEN_MIGR
)
4309 /* Clear migration record */
4310 memset(super
->migr_rec
, 0, sizeof(struct migr_record
));
4312 src
->map_state
= to_state
;
4315 static void end_migration(struct imsm_dev
*dev
, struct intel_super
*super
,
4318 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
4319 struct imsm_map
*prev
= get_imsm_map(dev
, dev
->vol
.migr_state
== 0 ?
4323 /* merge any IMSM_ORD_REBUILD bits that were not successfully
4324 * completed in the last migration.
4326 * FIXME add support for raid-level-migration
4328 if (map_state
!= map
->map_state
&& (is_gen_migration(dev
) == false) &&
4329 prev
->map_state
!= IMSM_T_STATE_UNINITIALIZED
) {
4330 /* when final map state is other than expected
4331 * merge maps (not for migration)
4335 for (i
= 0; i
< prev
->num_members
; i
++)
4336 for (j
= 0; j
< map
->num_members
; j
++)
4337 /* during online capacity expansion
4338 * disks position can be changed
4339 * if takeover is used
4341 if (ord_to_idx(map
->disk_ord_tbl
[j
]) ==
4342 ord_to_idx(prev
->disk_ord_tbl
[i
])) {
4343 map
->disk_ord_tbl
[j
] |=
4344 prev
->disk_ord_tbl
[i
];
4347 failed
= imsm_count_failed(super
, dev
, MAP_0
);
4348 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
4351 dev
->vol
.migr_state
= 0;
4352 set_migr_type(dev
, 0);
4353 set_vol_curr_migr_unit(dev
, 0);
4354 map
->map_state
= map_state
;
4357 static int parse_raid_devices(struct intel_super
*super
)
4360 struct imsm_dev
*dev_new
;
4361 size_t len
, len_migr
;
4363 size_t space_needed
= 0;
4364 struct imsm_super
*mpb
= super
->anchor
;
4366 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
4367 struct imsm_dev
*dev_iter
= __get_imsm_dev(super
->anchor
, i
);
4368 struct intel_dev
*dv
;
4370 len
= sizeof_imsm_dev(dev_iter
, 0);
4371 len_migr
= sizeof_imsm_dev(dev_iter
, 1);
4373 space_needed
+= len_migr
- len
;
4375 dv
= xmalloc(sizeof(*dv
));
4376 if (max_len
< len_migr
)
4378 if (max_len
> len_migr
)
4379 space_needed
+= max_len
- len_migr
;
4380 dev_new
= xmalloc(max_len
);
4381 imsm_copy_dev(dev_new
, dev_iter
);
4384 dv
->next
= super
->devlist
;
4385 super
->devlist
= dv
;
4388 /* ensure that super->buf is large enough when all raid devices
4391 if (__le32_to_cpu(mpb
->mpb_size
) + space_needed
> super
->len
) {
4394 len
= ROUND_UP(__le32_to_cpu(mpb
->mpb_size
) + space_needed
,
4395 super
->sector_size
);
4396 if (posix_memalign(&buf
, MAX_SECTOR_SIZE
, len
) != 0)
4399 memcpy(buf
, super
->buf
, super
->len
);
4400 memset(buf
+ super
->len
, 0, len
- super
->len
);
4406 super
->extra_space
+= space_needed
;
4411 /*******************************************************************************
4412 * Function: check_mpb_migr_compatibility
4413 * Description: Function checks for unsupported migration features:
4414 * - migration optimization area (pba_of_lba0)
4415 * - descending reshape (ascending_migr)
4417 * super : imsm metadata information
4419 * 0 : migration is compatible
4420 * -1 : migration is not compatible
4421 ******************************************************************************/
4422 int check_mpb_migr_compatibility(struct intel_super
*super
)
4424 struct imsm_map
*map0
, *map1
;
4425 struct migr_record
*migr_rec
= super
->migr_rec
;
4428 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
4429 struct imsm_dev
*dev_iter
= __get_imsm_dev(super
->anchor
, i
);
4431 if (dev_iter
->vol
.migr_state
== 1 &&
4432 dev_iter
->vol
.migr_type
== MIGR_GEN_MIGR
) {
4433 /* This device is migrating */
4434 map0
= get_imsm_map(dev_iter
, MAP_0
);
4435 map1
= get_imsm_map(dev_iter
, MAP_1
);
4436 if (pba_of_lba0(map0
) != pba_of_lba0(map1
))
4437 /* migration optimization area was used */
4439 if (migr_rec
->ascending_migr
== 0 &&
4440 migr_rec
->dest_depth_per_unit
> 0)
4441 /* descending reshape not supported yet */
4448 static void __free_imsm(struct intel_super
*super
, int free_disks
);
4450 /* load_imsm_mpb - read matrix metadata
4451 * allocates super->mpb to be freed by free_imsm
4453 static int load_imsm_mpb(int fd
, struct intel_super
*super
, char *devname
)
4455 unsigned long long dsize
;
4456 unsigned long long sectors
;
4457 unsigned int sector_size
= super
->sector_size
;
4459 struct imsm_super
*anchor
;
4462 get_dev_size(fd
, NULL
, &dsize
);
4463 if (dsize
< 2*sector_size
) {
4465 pr_err("%s: device to small for imsm\n",
4470 if (lseek64(fd
, dsize
- (sector_size
* 2), SEEK_SET
) < 0) {
4472 pr_err("Cannot seek to anchor block on %s: %s\n",
4473 devname
, strerror(errno
));
4477 if (posix_memalign((void **)&anchor
, sector_size
, sector_size
) != 0) {
4479 pr_err("Failed to allocate imsm anchor buffer on %s\n", devname
);
4482 if ((unsigned int)read(fd
, anchor
, sector_size
) != sector_size
) {
4484 pr_err("Cannot read anchor block on %s: %s\n",
4485 devname
, strerror(errno
));
4490 if (strncmp((char *) anchor
->sig
, MPB_SIGNATURE
, MPB_SIG_LEN
) != 0) {
4492 pr_err("no IMSM anchor on %s\n", devname
);
4497 __free_imsm(super
, 0);
4498 /* reload capability and hba */
4500 /* capability and hba must be updated with new super allocation */
4501 find_intel_hba_capability(fd
, super
, devname
);
4502 super
->len
= ROUND_UP(anchor
->mpb_size
, sector_size
);
4503 if (posix_memalign(&super
->buf
, MAX_SECTOR_SIZE
, super
->len
) != 0) {
4505 pr_err("unable to allocate %zu byte mpb buffer\n",
4510 memcpy(super
->buf
, anchor
, sector_size
);
4512 sectors
= mpb_sectors(anchor
, sector_size
) - 1;
4515 if (posix_memalign(&super
->migr_rec_buf
, MAX_SECTOR_SIZE
,
4516 MIGR_REC_BUF_SECTORS
*MAX_SECTOR_SIZE
) != 0) {
4517 pr_err("could not allocate migr_rec buffer\n");
4522 super
->clean_migration_record_by_mdmon
= 0;
4525 check_sum
= __gen_imsm_checksum(super
->anchor
);
4526 if (check_sum
!= __le32_to_cpu(super
->anchor
->check_sum
)) {
4528 pr_err("IMSM checksum %x != %x on %s\n",
4530 __le32_to_cpu(super
->anchor
->check_sum
),
4538 /* read the extended mpb */
4539 if (lseek64(fd
, dsize
- (sector_size
* (2 + sectors
)), SEEK_SET
) < 0) {
4541 pr_err("Cannot seek to extended mpb on %s: %s\n",
4542 devname
, strerror(errno
));
4546 if ((unsigned int)read(fd
, super
->buf
+ sector_size
,
4547 super
->len
- sector_size
) != super
->len
- sector_size
) {
4549 pr_err("Cannot read extended mpb on %s: %s\n",
4550 devname
, strerror(errno
));
4554 check_sum
= __gen_imsm_checksum(super
->anchor
);
4555 if (check_sum
!= __le32_to_cpu(super
->anchor
->check_sum
)) {
4557 pr_err("IMSM checksum %x != %x on %s\n",
4558 check_sum
, __le32_to_cpu(super
->anchor
->check_sum
),
4566 static int read_imsm_migr_rec(int fd
, struct intel_super
*super
);
4568 /* clears hi bits in metadata if MPB_ATTRIB_2TB_DISK not set */
4569 static void clear_hi(struct intel_super
*super
)
4571 struct imsm_super
*mpb
= super
->anchor
;
4573 if (mpb
->attributes
& MPB_ATTRIB_2TB_DISK
)
4575 for (i
= 0; i
< mpb
->num_disks
; ++i
) {
4576 struct imsm_disk
*disk
= &mpb
->disk
[i
];
4577 disk
->total_blocks_hi
= 0;
4579 for (i
= 0; i
< mpb
->num_raid_devs
; ++i
) {
4580 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
4581 for (n
= 0; n
< 2; ++n
) {
4582 struct imsm_map
*map
= get_imsm_map(dev
, n
);
4585 map
->pba_of_lba0_hi
= 0;
4586 map
->blocks_per_member_hi
= 0;
4587 map
->num_data_stripes_hi
= 0;
4593 load_and_parse_mpb(int fd
, struct intel_super
*super
, char *devname
, int keep_fd
)
4597 err
= load_imsm_mpb(fd
, super
, devname
);
4600 if (super
->sector_size
== 4096)
4601 convert_from_4k(super
);
4602 err
= load_imsm_disk(fd
, super
, devname
, keep_fd
);
4605 err
= parse_raid_devices(super
);
4608 err
= load_bbm_log(super
);
4613 static void __free_imsm_disk(struct dl
*d
, int do_close
)
4625 static void free_imsm_disks(struct intel_super
*super
)
4629 while (super
->disks
) {
4631 super
->disks
= d
->next
;
4632 __free_imsm_disk(d
, 1);
4634 while (super
->disk_mgmt_list
) {
4635 d
= super
->disk_mgmt_list
;
4636 super
->disk_mgmt_list
= d
->next
;
4637 __free_imsm_disk(d
, 1);
4639 while (super
->missing
) {
4641 super
->missing
= d
->next
;
4642 __free_imsm_disk(d
, 1);
4647 /* free all the pieces hanging off of a super pointer */
4648 static void __free_imsm(struct intel_super
*super
, int free_disks
)
4650 struct intel_hba
*elem
, *next
;
4656 /* unlink capability description */
4658 if (super
->migr_rec_buf
) {
4659 free(super
->migr_rec_buf
);
4660 super
->migr_rec_buf
= NULL
;
4663 free_imsm_disks(super
);
4664 free_devlist(super
);
4668 free((void *)elem
->path
);
4674 free(super
->bbm_log
);
4678 static void free_imsm(struct intel_super
*super
)
4680 __free_imsm(super
, 1);
4681 free(super
->bb
.entries
);
4685 static void free_super_imsm(struct supertype
*st
)
4687 struct intel_super
*super
= st
->sb
;
4696 static struct intel_super
*alloc_super(void)
4698 struct intel_super
*super
= xcalloc(1, sizeof(*super
));
4700 super
->current_vol
= -1;
4701 super
->create_offset
= ~((unsigned long long) 0);
4703 super
->bb
.entries
= xmalloc(BBM_LOG_MAX_ENTRIES
*
4704 sizeof(struct md_bb_entry
));
4705 if (!super
->bb
.entries
) {
4714 * find and allocate hba and OROM/EFI based on valid fd of RAID component device
4716 static int find_intel_hba_capability(int fd
, struct intel_super
*super
, char *devname
)
4718 struct sys_dev
*hba_name
;
4721 if (is_fd_valid(fd
) && test_partition(fd
)) {
4722 pr_err("imsm: %s is a partition, cannot be used in IMSM\n",
4726 if (!is_fd_valid(fd
) || check_no_platform()) {
4731 hba_name
= find_disk_attached_hba(fd
, NULL
);
4734 pr_err("%s is not attached to Intel(R) RAID controller.\n",
4738 rv
= attach_hba_to_super(super
, hba_name
);
4741 struct intel_hba
*hba
= super
->hba
;
4743 pr_err("%s is attached to Intel(R) %s %s (%s),\n"
4744 " but the container is assigned to Intel(R) %s %s (",
4746 get_sys_dev_type(hba_name
->type
),
4747 hba_name
->type
== SYS_DEV_VMD
|| hba_name
->type
== SYS_DEV_SATA_VMD
?
4748 "domain" : "RAID controller",
4749 hba_name
->pci_id
? : "Err!",
4750 get_sys_dev_type(super
->hba
->type
),
4751 hba
->type
== SYS_DEV_VMD
|| hba_name
->type
== SYS_DEV_SATA_VMD
?
4752 "domain" : "RAID controller");
4755 fprintf(stderr
, "%s", hba
->pci_id
? : "Err!");
4757 fprintf(stderr
, ", ");
4760 fprintf(stderr
, ").\n"
4761 " Mixing devices attached to different controllers is not allowed.\n");
4765 super
->orom
= find_imsm_capability(hba_name
);
4772 /* find_missing - helper routine for load_super_imsm_all that identifies
4773 * disks that have disappeared from the system. This routine relies on
4774 * the mpb being uptodate, which it is at load time.
4776 static int find_missing(struct intel_super
*super
)
4779 struct imsm_super
*mpb
= super
->anchor
;
4781 struct imsm_disk
*disk
;
4783 for (i
= 0; i
< mpb
->num_disks
; i
++) {
4784 disk
= __get_imsm_disk(mpb
, i
);
4785 dl
= serial_to_dl(disk
->serial
, super
);
4789 dl
= xmalloc(sizeof(*dl
));
4793 dl
->devname
= xstrdup("missing");
4795 serialcpy(dl
->serial
, disk
->serial
);
4798 dl
->next
= super
->missing
;
4799 super
->missing
= dl
;
4805 static struct intel_disk
*disk_list_get(__u8
*serial
, struct intel_disk
*disk_list
)
4807 struct intel_disk
*idisk
= disk_list
;
4810 if (serialcmp(idisk
->disk
.serial
, serial
) == 0)
4812 idisk
= idisk
->next
;
4818 static int __prep_thunderdome(struct intel_super
**table
, int tbl_size
,
4819 struct intel_super
*super
,
4820 struct intel_disk
**disk_list
)
4822 struct imsm_disk
*d
= &super
->disks
->disk
;
4823 struct imsm_super
*mpb
= super
->anchor
;
4826 for (i
= 0; i
< tbl_size
; i
++) {
4827 struct imsm_super
*tbl_mpb
= table
[i
]->anchor
;
4828 struct imsm_disk
*tbl_d
= &table
[i
]->disks
->disk
;
4830 if (tbl_mpb
->family_num
== mpb
->family_num
) {
4831 if (tbl_mpb
->check_sum
== mpb
->check_sum
) {
4832 dprintf("mpb from %d:%d matches %d:%d\n",
4833 super
->disks
->major
,
4834 super
->disks
->minor
,
4835 table
[i
]->disks
->major
,
4836 table
[i
]->disks
->minor
);
4840 if (((is_configured(d
) && !is_configured(tbl_d
)) ||
4841 is_configured(d
) == is_configured(tbl_d
)) &&
4842 tbl_mpb
->generation_num
< mpb
->generation_num
) {
4843 /* current version of the mpb is a
4844 * better candidate than the one in
4845 * super_table, but copy over "cross
4846 * generational" status
4848 struct intel_disk
*idisk
;
4850 dprintf("mpb from %d:%d replaces %d:%d\n",
4851 super
->disks
->major
,
4852 super
->disks
->minor
,
4853 table
[i
]->disks
->major
,
4854 table
[i
]->disks
->minor
);
4856 idisk
= disk_list_get(tbl_d
->serial
, *disk_list
);
4857 if (idisk
&& is_failed(&idisk
->disk
))
4858 tbl_d
->status
|= FAILED_DISK
;
4861 struct intel_disk
*idisk
;
4862 struct imsm_disk
*disk
;
4864 /* tbl_mpb is more up to date, but copy
4865 * over cross generational status before
4868 disk
= __serial_to_disk(d
->serial
, mpb
, NULL
);
4869 if (disk
&& is_failed(disk
))
4870 d
->status
|= FAILED_DISK
;
4872 idisk
= disk_list_get(d
->serial
, *disk_list
);
4875 if (disk
&& is_configured(disk
))
4876 idisk
->disk
.status
|= CONFIGURED_DISK
;
4879 dprintf("mpb from %d:%d prefer %d:%d\n",
4880 super
->disks
->major
,
4881 super
->disks
->minor
,
4882 table
[i
]->disks
->major
,
4883 table
[i
]->disks
->minor
);
4891 table
[tbl_size
++] = super
;
4895 /* update/extend the merged list of imsm_disk records */
4896 for (j
= 0; j
< mpb
->num_disks
; j
++) {
4897 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, j
);
4898 struct intel_disk
*idisk
;
4900 idisk
= disk_list_get(disk
->serial
, *disk_list
);
4902 idisk
->disk
.status
|= disk
->status
;
4903 if (is_configured(&idisk
->disk
) ||
4904 is_failed(&idisk
->disk
))
4905 idisk
->disk
.status
&= ~(SPARE_DISK
);
4907 idisk
= xcalloc(1, sizeof(*idisk
));
4908 idisk
->owner
= IMSM_UNKNOWN_OWNER
;
4909 idisk
->disk
= *disk
;
4910 idisk
->next
= *disk_list
;
4914 if (serialcmp(idisk
->disk
.serial
, d
->serial
) == 0)
4921 static struct intel_super
*
4922 validate_members(struct intel_super
*super
, struct intel_disk
*disk_list
,
4925 struct imsm_super
*mpb
= super
->anchor
;
4929 for (i
= 0; i
< mpb
->num_disks
; i
++) {
4930 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, i
);
4931 struct intel_disk
*idisk
;
4933 idisk
= disk_list_get(disk
->serial
, disk_list
);
4935 if (idisk
->owner
== owner
||
4936 idisk
->owner
== IMSM_UNKNOWN_OWNER
)
4939 dprintf("'%.16s' owner %d != %d\n",
4940 disk
->serial
, idisk
->owner
,
4943 dprintf("unknown disk %x [%d]: %.16s\n",
4944 __le32_to_cpu(mpb
->family_num
), i
,
4950 if (ok_count
== mpb
->num_disks
)
4955 static void show_conflicts(__u32 family_num
, struct intel_super
*super_list
)
4957 struct intel_super
*s
;
4959 for (s
= super_list
; s
; s
= s
->next
) {
4960 if (family_num
!= s
->anchor
->family_num
)
4962 pr_err("Conflict, offlining family %#x on '%s'\n",
4963 __le32_to_cpu(family_num
), s
->disks
->devname
);
4967 static struct intel_super
*
4968 imsm_thunderdome(struct intel_super
**super_list
, int len
)
4970 struct intel_super
*super_table
[len
];
4971 struct intel_disk
*disk_list
= NULL
;
4972 struct intel_super
*champion
, *spare
;
4973 struct intel_super
*s
, **del
;
4978 memset(super_table
, 0, sizeof(super_table
));
4979 for (s
= *super_list
; s
; s
= s
->next
)
4980 tbl_size
= __prep_thunderdome(super_table
, tbl_size
, s
, &disk_list
);
4982 for (i
= 0; i
< tbl_size
; i
++) {
4983 struct imsm_disk
*d
;
4984 struct intel_disk
*idisk
;
4985 struct imsm_super
*mpb
= super_table
[i
]->anchor
;
4988 d
= &s
->disks
->disk
;
4990 /* 'd' must appear in merged disk list for its
4991 * configuration to be valid
4993 idisk
= disk_list_get(d
->serial
, disk_list
);
4994 if (idisk
&& idisk
->owner
== i
)
4995 s
= validate_members(s
, disk_list
, i
);
5000 dprintf("marking family: %#x from %d:%d offline\n",
5002 super_table
[i
]->disks
->major
,
5003 super_table
[i
]->disks
->minor
);
5007 /* This is where the mdadm implementation differs from the Windows
5008 * driver which has no strict concept of a container. We can only
5009 * assemble one family from a container, so when returning a prodigal
5010 * array member to this system the code will not be able to disambiguate
5011 * the container contents that should be assembled ("foreign" versus
5012 * "local"). It requires user intervention to set the orig_family_num
5013 * to a new value to establish a new container. The Windows driver in
5014 * this situation fixes up the volume name in place and manages the
5015 * foreign array as an independent entity.
5020 for (i
= 0; i
< tbl_size
; i
++) {
5021 struct intel_super
*tbl_ent
= super_table
[i
];
5027 if (tbl_ent
->anchor
->num_raid_devs
== 0) {
5032 if (s
&& !is_spare
) {
5033 show_conflicts(tbl_ent
->anchor
->family_num
, *super_list
);
5035 } else if (!s
&& !is_spare
)
5048 pr_err("Chose family %#x on '%s', assemble conflicts to new container with '--update=uuid'\n",
5049 __le32_to_cpu(s
->anchor
->family_num
), s
->disks
->devname
);
5051 /* collect all dl's onto 'champion', and update them to
5052 * champion's version of the status
5054 for (s
= *super_list
; s
; s
= s
->next
) {
5055 struct imsm_super
*mpb
= champion
->anchor
;
5056 struct dl
*dl
= s
->disks
;
5061 mpb
->attributes
|= s
->anchor
->attributes
& MPB_ATTRIB_2TB_DISK
;
5063 for (i
= 0; i
< mpb
->num_disks
; i
++) {
5064 struct imsm_disk
*disk
;
5066 disk
= __serial_to_disk(dl
->serial
, mpb
, &dl
->index
);
5069 /* only set index on disks that are a member of
5070 * a populated contianer, i.e. one with
5073 if (is_failed(&dl
->disk
))
5075 else if (is_spare(&dl
->disk
))
5081 if (i
>= mpb
->num_disks
) {
5082 struct intel_disk
*idisk
;
5084 idisk
= disk_list_get(dl
->serial
, disk_list
);
5085 if (idisk
&& is_spare(&idisk
->disk
) &&
5086 !is_failed(&idisk
->disk
) && !is_configured(&idisk
->disk
))
5094 dl
->next
= champion
->disks
;
5095 champion
->disks
= dl
;
5099 /* delete 'champion' from super_list */
5100 for (del
= super_list
; *del
; ) {
5101 if (*del
== champion
) {
5102 *del
= (*del
)->next
;
5105 del
= &(*del
)->next
;
5107 champion
->next
= NULL
;
5111 struct intel_disk
*idisk
= disk_list
;
5113 disk_list
= disk_list
->next
;
5121 get_sra_super_block(int fd
, struct intel_super
**super_list
, char *devname
, int *max
, int keep_fd
);
5122 static int get_super_block(struct intel_super
**super_list
, char *devnm
, char *devname
,
5123 int major
, int minor
, int keep_fd
);
5125 get_devlist_super_block(struct md_list
*devlist
, struct intel_super
**super_list
,
5126 int *max
, int keep_fd
);
5128 static int load_super_imsm_all(struct supertype
*st
, int fd
, void **sbp
,
5129 char *devname
, struct md_list
*devlist
,
5132 struct intel_super
*super_list
= NULL
;
5133 struct intel_super
*super
= NULL
;
5137 if (is_fd_valid(fd
))
5138 /* 'fd' is an opened container */
5139 err
= get_sra_super_block(fd
, &super_list
, devname
, &i
, keep_fd
);
5141 /* get super block from devlist devices */
5142 err
= get_devlist_super_block(devlist
, &super_list
, &i
, keep_fd
);
5145 /* all mpbs enter, maybe one leaves */
5146 super
= imsm_thunderdome(&super_list
, i
);
5152 if (find_missing(super
) != 0) {
5158 /* load migration record */
5159 err
= load_imsm_migr_rec(super
);
5161 /* migration is in progress,
5162 * but migr_rec cannot be loaded,
5168 /* Check migration compatibility */
5169 if (err
== 0 && check_mpb_migr_compatibility(super
) != 0) {
5170 pr_err("Unsupported migration detected");
5172 fprintf(stderr
, " on %s\n", devname
);
5174 fprintf(stderr
, " (IMSM).\n");
5183 while (super_list
) {
5184 struct intel_super
*s
= super_list
;
5186 super_list
= super_list
->next
;
5194 if (is_fd_valid(fd
))
5195 strcpy(st
->container_devnm
, fd2devnm(fd
));
5197 st
->container_devnm
[0] = 0;
5198 if (err
== 0 && st
->ss
== NULL
) {
5199 st
->ss
= &super_imsm
;
5200 st
->minor_version
= 0;
5201 st
->max_devs
= IMSM_MAX_DEVICES
;
5207 get_devlist_super_block(struct md_list
*devlist
, struct intel_super
**super_list
,
5208 int *max
, int keep_fd
)
5210 struct md_list
*tmpdev
;
5214 for (i
= 0, tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
5215 if (tmpdev
->used
!= 1)
5217 if (tmpdev
->container
== 1) {
5219 int fd
= dev_open(tmpdev
->devname
, O_RDONLY
|O_EXCL
);
5220 if (!is_fd_valid(fd
)) {
5221 pr_err("cannot open device %s: %s\n",
5222 tmpdev
->devname
, strerror(errno
));
5226 err
= get_sra_super_block(fd
, super_list
,
5227 tmpdev
->devname
, &lmax
,
5236 int major
= major(tmpdev
->st_rdev
);
5237 int minor
= minor(tmpdev
->st_rdev
);
5238 err
= get_super_block(super_list
,
5255 static int get_super_block(struct intel_super
**super_list
, char *devnm
, char *devname
,
5256 int major
, int minor
, int keep_fd
)
5258 struct intel_super
*s
;
5270 sprintf(nm
, "%d:%d", major
, minor
);
5271 dfd
= dev_open(nm
, O_RDWR
);
5272 if (!is_fd_valid(dfd
)) {
5277 if (!get_dev_sector_size(dfd
, NULL
, &s
->sector_size
)) {
5281 find_intel_hba_capability(dfd
, s
, devname
);
5282 err
= load_and_parse_mpb(dfd
, s
, NULL
, keep_fd
);
5284 /* retry the load if we might have raced against mdmon */
5285 if (err
== 3 && devnm
&& mdmon_running(devnm
))
5286 for (retry
= 0; retry
< 3; retry
++) {
5287 sleep_for(0, MSEC_TO_NSEC(3), true);
5288 err
= load_and_parse_mpb(dfd
, s
, NULL
, keep_fd
);
5294 s
->next
= *super_list
;
5308 get_sra_super_block(int fd
, struct intel_super
**super_list
, char *devname
, int *max
, int keep_fd
)
5315 sra
= sysfs_read(fd
, NULL
, GET_LEVEL
|GET_VERSION
|GET_DEVS
|GET_STATE
);
5319 if (sra
->array
.major_version
!= -1 ||
5320 sra
->array
.minor_version
!= -2 ||
5321 strcmp(sra
->text_version
, "imsm") != 0) {
5326 devnm
= fd2devnm(fd
);
5327 for (sd
= sra
->devs
, i
= 0; sd
; sd
= sd
->next
, i
++) {
5328 if (get_super_block(super_list
, devnm
, devname
,
5329 sd
->disk
.major
, sd
->disk
.minor
, keep_fd
) != 0) {
5340 static int load_container_imsm(struct supertype
*st
, int fd
, char *devname
)
5342 return load_super_imsm_all(st
, fd
, &st
->sb
, devname
, NULL
, 1);
5345 static int load_super_imsm(struct supertype
*st
, int fd
, char *devname
)
5347 struct intel_super
*super
;
5351 if (test_partition(fd
))
5352 /* IMSM not allowed on partitions */
5355 free_super_imsm(st
);
5357 super
= alloc_super();
5361 if (!get_dev_sector_size(fd
, NULL
, &super
->sector_size
)) {
5365 /* Load hba and capabilities if they exist.
5366 * But do not preclude loading metadata in case capabilities or hba are
5367 * non-compliant and ignore_hw_compat is set.
5369 rv
= find_intel_hba_capability(fd
, super
, devname
);
5370 /* no orom/efi or non-intel hba of the disk */
5371 if (rv
!= 0 && st
->ignore_hw_compat
== 0) {
5373 pr_err("No OROM/EFI properties for %s\n", devname
);
5377 rv
= load_and_parse_mpb(fd
, super
, devname
, 0);
5379 /* retry the load if we might have raced against mdmon */
5381 struct mdstat_ent
*mdstat
= NULL
;
5382 char *name
= fd2kname(fd
);
5385 mdstat
= mdstat_by_component(name
);
5387 if (mdstat
&& mdmon_running(mdstat
->devnm
) && getpid() != mdmon_pid(mdstat
->devnm
)) {
5388 for (retry
= 0; retry
< 3; retry
++) {
5389 sleep_for(0, MSEC_TO_NSEC(3), true);
5390 rv
= load_and_parse_mpb(fd
, super
, devname
, 0);
5396 free_mdstat(mdstat
);
5401 pr_err("Failed to load all information sections on %s\n", devname
);
5407 if (st
->ss
== NULL
) {
5408 st
->ss
= &super_imsm
;
5409 st
->minor_version
= 0;
5410 st
->max_devs
= IMSM_MAX_DEVICES
;
5413 /* load migration record */
5414 if (load_imsm_migr_rec(super
) == 0) {
5415 /* Check for unsupported migration features */
5416 if (check_mpb_migr_compatibility(super
) != 0) {
5417 pr_err("Unsupported migration detected");
5419 fprintf(stderr
, " on %s\n", devname
);
5421 fprintf(stderr
, " (IMSM).\n");
5429 static __u16
info_to_blocks_per_strip(mdu_array_info_t
*info
)
5431 if (info
->level
== 1)
5433 return info
->chunk_size
>> 9;
5436 static unsigned long long info_to_blocks_per_member(mdu_array_info_t
*info
,
5437 unsigned long long size
)
5439 if (info
->level
== 1)
5442 return (size
* 2) & ~(info_to_blocks_per_strip(info
) - 1);
5445 static void imsm_update_version_info(struct intel_super
*super
)
5447 /* update the version and attributes */
5448 struct imsm_super
*mpb
= super
->anchor
;
5450 struct imsm_dev
*dev
;
5451 struct imsm_map
*map
;
5454 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
5455 dev
= get_imsm_dev(super
, i
);
5456 map
= get_imsm_map(dev
, MAP_0
);
5457 if (__le32_to_cpu(dev
->size_high
) > 0)
5458 mpb
->attributes
|= MPB_ATTRIB_2TB
;
5460 /* FIXME detect when an array spans a port multiplier */
5462 mpb
->attributes
|= MPB_ATTRIB_PM
;
5465 if (mpb
->num_raid_devs
> 1 ||
5466 mpb
->attributes
!= MPB_ATTRIB_CHECKSUM_VERIFY
) {
5467 version
= MPB_VERSION_ATTRIBS
;
5468 switch (get_imsm_raid_level(map
)) {
5469 case 0: mpb
->attributes
|= MPB_ATTRIB_RAID0
; break;
5470 case 1: mpb
->attributes
|= MPB_ATTRIB_RAID1
; break;
5471 case 10: mpb
->attributes
|= MPB_ATTRIB_RAID10
; break;
5472 case 5: mpb
->attributes
|= MPB_ATTRIB_RAID5
; break;
5475 if (map
->num_members
>= 5)
5476 version
= MPB_VERSION_5OR6_DISK_ARRAY
;
5477 else if (dev
->status
== DEV_CLONE_N_GO
)
5478 version
= MPB_VERSION_CNG
;
5479 else if (get_imsm_raid_level(map
) == 5)
5480 version
= MPB_VERSION_RAID5
;
5481 else if (map
->num_members
>= 3)
5482 version
= MPB_VERSION_3OR4_DISK_ARRAY
;
5483 else if (get_imsm_raid_level(map
) == 1)
5484 version
= MPB_VERSION_RAID1
;
5486 version
= MPB_VERSION_RAID0
;
5488 strcpy(((char *) mpb
->sig
) + strlen(MPB_SIGNATURE
), version
);
5493 * imsm_check_name() - check imsm naming criteria.
5494 * @super: &intel_super pointer, not NULL.
5495 * @name: name to check.
5496 * @verbose: verbose level.
5498 * Name must be no longer than &MAX_RAID_SERIAL_LEN and must be unique across volumes.
5500 * Returns: &true if @name matches, &false otherwise.
5502 static bool imsm_is_name_allowed(struct intel_super
*super
, const char * const name
,
5505 struct imsm_super
*mpb
= super
->anchor
;
5508 if (is_string_lq(name
, MAX_RAID_SERIAL_LEN
+ 1) == false) {
5509 pr_vrb("imsm: Name \"%s\" is too long\n", name
);
5513 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
5514 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
5516 if (strncmp((char *) dev
->volume
, name
, MAX_RAID_SERIAL_LEN
) == 0) {
5517 pr_vrb("imsm: Name \"%s\" already exists\n", name
);
5525 static int init_super_imsm_volume(struct supertype
*st
, mdu_array_info_t
*info
,
5526 struct shape
*s
, char *name
,
5527 char *homehost
, int *uuid
,
5528 long long data_offset
)
5530 /* We are creating a volume inside a pre-existing container.
5531 * so st->sb is already set.
5533 struct intel_super
*super
= st
->sb
;
5534 unsigned int sector_size
= super
->sector_size
;
5535 struct imsm_super
*mpb
= super
->anchor
;
5536 struct intel_dev
*dv
;
5537 struct imsm_dev
*dev
;
5538 struct imsm_vol
*vol
;
5539 struct imsm_map
*map
;
5540 int idx
= mpb
->num_raid_devs
;
5543 unsigned long long array_blocks
;
5544 size_t size_old
, size_new
;
5545 unsigned int data_disks
;
5546 unsigned long long size_per_member
;
5548 if (super
->orom
&& mpb
->num_raid_devs
>= super
->orom
->vpa
) {
5549 pr_err("This imsm-container already has the maximum of %d volumes\n", super
->orom
->vpa
);
5553 /* ensure the mpb is large enough for the new data */
5554 size_old
= __le32_to_cpu(mpb
->mpb_size
);
5555 size_new
= disks_to_mpb_size(info
->nr_disks
);
5556 if (size_new
> size_old
) {
5558 size_t size_round
= ROUND_UP(size_new
, sector_size
);
5560 if (posix_memalign(&mpb_new
, sector_size
, size_round
) != 0) {
5561 pr_err("could not allocate new mpb\n");
5564 if (posix_memalign(&super
->migr_rec_buf
, MAX_SECTOR_SIZE
,
5565 MIGR_REC_BUF_SECTORS
*
5566 MAX_SECTOR_SIZE
) != 0) {
5567 pr_err("could not allocate migr_rec buffer\n");
5573 memcpy(mpb_new
, mpb
, size_old
);
5576 super
->anchor
= mpb_new
;
5577 mpb
->mpb_size
= __cpu_to_le32(size_new
);
5578 memset(mpb_new
+ size_old
, 0, size_round
- size_old
);
5579 super
->len
= size_round
;
5581 super
->current_vol
= idx
;
5583 /* handle 'failed_disks' by either:
5584 * a) create dummy disk entries in the table if this the first
5585 * volume in the array. We add them here as this is the only
5586 * opportunity to add them. add_to_super_imsm_volume()
5587 * handles the non-failed disks and continues incrementing
5589 * b) validate that 'failed_disks' matches the current number
5590 * of missing disks if the container is populated
5592 if (super
->current_vol
== 0) {
5594 for (i
= 0; i
< info
->failed_disks
; i
++) {
5595 struct imsm_disk
*disk
;
5598 disk
= __get_imsm_disk(mpb
, i
);
5599 disk
->status
= CONFIGURED_DISK
| FAILED_DISK
;
5600 disk
->scsi_id
= __cpu_to_le32(~(__u32
)0);
5601 snprintf((char *) disk
->serial
, MAX_RAID_SERIAL_LEN
,
5602 "missing:%d", (__u8
)i
);
5604 find_missing(super
);
5609 for (d
= super
->missing
; d
; d
= d
->next
)
5611 if (info
->failed_disks
> missing
) {
5612 pr_err("unable to add 'missing' disk to container\n");
5617 if (imsm_is_name_allowed(super
, name
, 1) == false)
5620 dv
= xmalloc(sizeof(*dv
));
5621 dev
= xcalloc(1, sizeof(*dev
) + sizeof(__u32
) * (info
->raid_disks
- 1));
5623 * Explicitly allow truncating to not confuse gcc's
5624 * -Werror=stringop-truncation
5626 namelen
= min((int) strlen(name
), MAX_RAID_SERIAL_LEN
);
5627 memcpy(dev
->volume
, name
, namelen
);
5628 array_blocks
= calc_array_size(info
->level
, info
->raid_disks
,
5629 info
->layout
, info
->chunk_size
,
5630 s
->size
* BLOCKS_PER_KB
);
5631 data_disks
= get_data_disks(info
->level
, info
->layout
,
5633 array_blocks
= round_size_to_mb(array_blocks
, data_disks
);
5634 size_per_member
= array_blocks
/ data_disks
;
5636 set_imsm_dev_size(dev
, array_blocks
);
5637 dev
->status
= (DEV_READ_COALESCING
| DEV_WRITE_COALESCING
);
5639 vol
->migr_state
= 0;
5640 set_migr_type(dev
, MIGR_INIT
);
5641 vol
->dirty
= !info
->state
;
5642 set_vol_curr_migr_unit(dev
, 0);
5643 map
= get_imsm_map(dev
, MAP_0
);
5644 set_pba_of_lba0(map
, super
->create_offset
);
5645 map
->blocks_per_strip
= __cpu_to_le16(info_to_blocks_per_strip(info
));
5646 map
->failed_disk_num
= ~0;
5647 if (info
->level
> 0)
5648 map
->map_state
= (info
->state
? IMSM_T_STATE_NORMAL
5649 : IMSM_T_STATE_UNINITIALIZED
);
5651 map
->map_state
= info
->failed_disks
? IMSM_T_STATE_FAILED
:
5652 IMSM_T_STATE_NORMAL
;
5655 if (info
->level
== 1 && info
->raid_disks
> 2) {
5658 pr_err("imsm does not support more than 2 disksin a raid1 volume\n");
5662 map
->raid_level
= info
->level
;
5663 if (info
->level
== 10)
5664 map
->raid_level
= 1;
5665 set_num_domains(map
);
5667 size_per_member
+= NUM_BLOCKS_DIRTY_STRIPE_REGION
;
5668 set_blocks_per_member(map
, info_to_blocks_per_member(info
,
5672 map
->num_members
= info
->raid_disks
;
5673 update_num_data_stripes(map
, array_blocks
);
5674 for (i
= 0; i
< map
->num_members
; i
++) {
5675 /* initialized in add_to_super */
5676 set_imsm_ord_tbl_ent(map
, i
, IMSM_ORD_REBUILD
);
5678 mpb
->num_raid_devs
++;
5679 mpb
->num_raid_devs_created
++;
5680 dev
->my_vol_raid_dev_num
= mpb
->num_raid_devs_created
;
5682 if (s
->consistency_policy
<= CONSISTENCY_POLICY_RESYNC
) {
5683 dev
->rwh_policy
= RWH_MULTIPLE_OFF
;
5684 } else if (s
->consistency_policy
== CONSISTENCY_POLICY_PPL
) {
5685 dev
->rwh_policy
= RWH_MULTIPLE_DISTRIBUTED
;
5689 pr_err("imsm does not support consistency policy %s\n",
5690 map_num_s(consistency_policies
, s
->consistency_policy
));
5695 dv
->index
= super
->current_vol
;
5696 dv
->next
= super
->devlist
;
5697 super
->devlist
= dv
;
5699 imsm_update_version_info(super
);
5704 static int init_super_imsm(struct supertype
*st
, mdu_array_info_t
*info
,
5705 struct shape
*s
, char *name
,
5706 char *homehost
, int *uuid
,
5707 unsigned long long data_offset
)
5709 /* This is primarily called by Create when creating a new array.
5710 * We will then get add_to_super called for each component, and then
5711 * write_init_super called to write it out to each device.
5712 * For IMSM, Create can create on fresh devices or on a pre-existing
5714 * To create on a pre-existing array a different method will be called.
5715 * This one is just for fresh drives.
5717 struct intel_super
*super
;
5718 struct imsm_super
*mpb
;
5722 if (data_offset
!= INVALID_SECTORS
) {
5723 pr_err("data-offset not supported by imsm\n");
5728 return init_super_imsm_volume(st
, info
, s
, name
, homehost
, uuid
,
5732 mpb_size
= disks_to_mpb_size(info
->nr_disks
);
5734 mpb_size
= MAX_SECTOR_SIZE
;
5736 super
= alloc_super();
5738 posix_memalign(&super
->buf
, MAX_SECTOR_SIZE
, mpb_size
) != 0) {
5743 pr_err("could not allocate superblock\n");
5746 if (posix_memalign(&super
->migr_rec_buf
, MAX_SECTOR_SIZE
,
5747 MIGR_REC_BUF_SECTORS
*MAX_SECTOR_SIZE
) != 0) {
5748 pr_err("could not allocate migr_rec buffer\n");
5753 memset(super
->buf
, 0, mpb_size
);
5755 mpb
->mpb_size
= __cpu_to_le32(mpb_size
);
5759 /* zeroing superblock */
5763 mpb
->attributes
= MPB_ATTRIB_CHECKSUM_VERIFY
;
5765 version
= (char *) mpb
->sig
;
5766 strcpy(version
, MPB_SIGNATURE
);
5767 version
+= strlen(MPB_SIGNATURE
);
5768 strcpy(version
, MPB_VERSION_RAID0
);
5773 static int drive_validate_sector_size(struct intel_super
*super
, struct dl
*dl
)
5775 unsigned int member_sector_size
;
5777 if (!is_fd_valid(dl
->fd
)) {
5778 pr_err("Invalid file descriptor for %s\n", dl
->devname
);
5782 if (!get_dev_sector_size(dl
->fd
, dl
->devname
, &member_sector_size
))
5784 if (member_sector_size
!= super
->sector_size
)
5789 static int add_to_super_imsm_volume(struct supertype
*st
, mdu_disk_info_t
*dk
,
5790 int fd
, char *devname
)
5792 struct intel_super
*super
= st
->sb
;
5793 struct imsm_super
*mpb
= super
->anchor
;
5794 struct imsm_disk
*_disk
;
5795 struct imsm_dev
*dev
;
5796 struct imsm_map
*map
;
5801 if (!is_fd_valid(fd
))
5804 dev
= get_imsm_dev(super
, super
->current_vol
);
5805 map
= get_imsm_map(dev
, MAP_0
);
5807 if (! (dk
->state
& (1<<MD_DISK_SYNC
))) {
5808 pr_err("%s: Cannot add spare devices to IMSM volume\n",
5813 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
5815 if (dl
->raiddisk
== dk
->raid_disk
)
5817 } else if (dl
->major
== dk
->major
&& dl
->minor
== dk
->minor
)
5823 pr_err("%s is not a member of the same container.\n",
5828 if (!autolayout
&& super
->current_vol
> 0) {
5829 int _slot
= get_disk_slot_in_dev(super
, 0, dl
->index
);
5831 if (_slot
!= dk
->raid_disk
) {
5832 pr_err("Member %s is in %d slot for the first volume, but is in %d slot for a new volume.\n",
5833 dl
->devname
, _slot
, dk
->raid_disk
);
5834 pr_err("Raid members are in different order than for the first volume, aborting.\n");
5839 if (mpb
->num_disks
== 0)
5840 if (!get_dev_sector_size(dl
->fd
, dl
->devname
,
5841 &super
->sector_size
))
5844 if (!drive_validate_sector_size(super
, dl
)) {
5845 pr_err("Combining drives of different sector size in one volume is not allowed\n");
5849 /* add a pristine spare to the metadata */
5850 if (dl
->index
< 0) {
5851 dl
->index
= super
->anchor
->num_disks
;
5852 super
->anchor
->num_disks
++;
5854 /* Check the device has not already been added */
5855 slot
= get_imsm_disk_slot(map
, dl
->index
);
5857 (get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
) & IMSM_ORD_REBUILD
) == 0) {
5858 pr_err("%s has been included in this array twice\n",
5862 set_imsm_ord_tbl_ent(map
, dk
->raid_disk
, dl
->index
);
5863 dl
->disk
.status
= CONFIGURED_DISK
;
5865 /* update size of 'missing' disks to be at least as large as the
5866 * largest acitve member (we only have dummy missing disks when
5867 * creating the first volume)
5869 if (super
->current_vol
== 0) {
5870 for (df
= super
->missing
; df
; df
= df
->next
) {
5871 if (total_blocks(&dl
->disk
) > total_blocks(&df
->disk
))
5872 set_total_blocks(&df
->disk
, total_blocks(&dl
->disk
));
5873 _disk
= __get_imsm_disk(mpb
, df
->index
);
5878 /* refresh unset/failed slots to point to valid 'missing' entries */
5879 for (df
= super
->missing
; df
; df
= df
->next
)
5880 for (slot
= 0; slot
< mpb
->num_disks
; slot
++) {
5881 __u32 ord
= get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
);
5883 if ((ord
& IMSM_ORD_REBUILD
) == 0)
5885 set_imsm_ord_tbl_ent(map
, slot
, df
->index
| IMSM_ORD_REBUILD
);
5886 if (is_gen_migration(dev
)) {
5887 struct imsm_map
*map2
= get_imsm_map(dev
,
5889 int slot2
= get_imsm_disk_slot(map2
, df
->index
);
5890 if (slot2
< map2
->num_members
&& slot2
>= 0) {
5891 __u32 ord2
= get_imsm_ord_tbl_ent(dev
,
5894 if ((unsigned)df
->index
==
5896 set_imsm_ord_tbl_ent(map2
,
5902 dprintf("set slot:%d to missing disk:%d\n", slot
, df
->index
);
5906 /* if we are creating the first raid device update the family number */
5907 if (super
->current_vol
== 0) {
5909 struct imsm_dev
*_dev
= __get_imsm_dev(mpb
, 0);
5911 _disk
= __get_imsm_disk(mpb
, dl
->index
);
5913 pr_err("BUG mpb setup error\n");
5919 sum
+= __gen_imsm_checksum(mpb
);
5920 mpb
->family_num
= __cpu_to_le32(sum
);
5921 mpb
->orig_family_num
= mpb
->family_num
;
5922 mpb
->creation_time
= __cpu_to_le64((__u64
)time(NULL
));
5924 super
->current_disk
= dl
;
5929 * Function marks disk as spare and restores disk serial
5930 * in case it was previously marked as failed by takeover operation
5932 * -1 : critical error
5933 * 0 : disk is marked as spare but serial is not set
5936 int mark_spare(struct dl
*disk
)
5938 __u8 serial
[MAX_RAID_SERIAL_LEN
];
5945 if (!imsm_read_serial(disk
->fd
, NULL
, serial
, MAX_RAID_SERIAL_LEN
)) {
5946 /* Restore disk serial number, because takeover marks disk
5947 * as failed and adds to serial ':0' before it becomes
5950 serialcpy(disk
->serial
, serial
);
5951 serialcpy(disk
->disk
.serial
, serial
);
5954 disk
->disk
.status
= SPARE_DISK
;
5961 static int write_super_imsm_spare(struct intel_super
*super
, struct dl
*d
);
5963 static int add_to_super_imsm(struct supertype
*st
, mdu_disk_info_t
*dk
,
5964 int fd
, char *devname
,
5965 unsigned long long data_offset
)
5967 struct intel_super
*super
= st
->sb
;
5969 unsigned long long size
;
5970 unsigned int member_sector_size
;
5975 /* If we are on an RAID enabled platform check that the disk is
5976 * attached to the raid controller.
5977 * We do not need to test disks attachment for container based additions,
5978 * they shall be already tested when container was created/assembled.
5980 rv
= find_intel_hba_capability(fd
, super
, devname
);
5981 /* no orom/efi or non-intel hba of the disk */
5983 dprintf("capability: %p fd: %d ret: %d\n",
5984 super
->orom
, fd
, rv
);
5988 if (super
->current_vol
>= 0)
5989 return add_to_super_imsm_volume(st
, dk
, fd
, devname
);
5992 dd
= xcalloc(sizeof(*dd
), 1);
5993 dd
->major
= major(stb
.st_rdev
);
5994 dd
->minor
= minor(stb
.st_rdev
);
5995 dd
->devname
= devname
? xstrdup(devname
) : NULL
;
5998 dd
->action
= DISK_ADD
;
5999 rv
= imsm_read_serial(fd
, devname
, dd
->serial
, MAX_RAID_SERIAL_LEN
);
6001 pr_err("failed to retrieve scsi serial, aborting\n");
6002 __free_imsm_disk(dd
, 0);
6006 if (super
->hba
&& ((super
->hba
->type
== SYS_DEV_NVME
) ||
6007 (super
->hba
->type
== SYS_DEV_VMD
))) {
6009 char cntrl_path
[PATH_MAX
];
6011 char pci_dev_path
[PATH_MAX
];
6013 if (!diskfd_to_devpath(fd
, 2, pci_dev_path
) ||
6014 !diskfd_to_devpath(fd
, 1, cntrl_path
)) {
6015 pr_err("failed to get dev paths, aborting\n");
6016 __free_imsm_disk(dd
, 0);
6020 cntrl_name
= basename(cntrl_path
);
6021 if (is_multipath_nvme(fd
))
6022 pr_err("%s controller supports Multi-Path I/O, Intel (R) VROC does not support multipathing\n",
6025 if (devpath_to_vendor(pci_dev_path
) == 0x8086) {
6027 * If Intel's NVMe drive has serial ended with
6028 * "-A","-B","-1" or "-2" it means that this is "x8"
6029 * device (double drive on single PCIe card).
6030 * User should be warned about potential data loss.
6032 for (i
= MAX_RAID_SERIAL_LEN
-1; i
> 0; i
--) {
6033 /* Skip empty character at the end */
6034 if (dd
->serial
[i
] == 0)
6037 if (((dd
->serial
[i
] == 'A') ||
6038 (dd
->serial
[i
] == 'B') ||
6039 (dd
->serial
[i
] == '1') ||
6040 (dd
->serial
[i
] == '2')) &&
6041 (dd
->serial
[i
-1] == '-'))
6042 pr_err("\tThe action you are about to take may put your data at risk.\n"
6043 "\tPlease note that x8 devices may consist of two separate x4 devices "
6044 "located on a single PCIe port.\n"
6045 "\tRAID 0 is the only supported configuration for this type of x8 device.\n");
6048 } else if (super
->hba
->type
== SYS_DEV_VMD
&& super
->orom
&&
6049 !imsm_orom_has_tpv_support(super
->orom
)) {
6050 pr_err("\tPlatform configuration does not support non-Intel NVMe drives.\n"
6051 "\tPlease refer to Intel(R) RSTe/VROC user guide.\n");
6052 __free_imsm_disk(dd
, 0);
6057 get_dev_size(fd
, NULL
, &size
);
6058 if (!get_dev_sector_size(fd
, NULL
, &member_sector_size
)) {
6059 __free_imsm_disk(dd
, 0);
6063 if (super
->sector_size
== 0) {
6064 /* this a first device, so sector_size is not set yet */
6065 super
->sector_size
= member_sector_size
;
6068 /* clear migr_rec when adding disk to container */
6069 memset(super
->migr_rec_buf
, 0, MIGR_REC_BUF_SECTORS
*MAX_SECTOR_SIZE
);
6070 if (lseek64(fd
, size
- MIGR_REC_SECTOR_POSITION
*member_sector_size
,
6072 if ((unsigned int)write(fd
, super
->migr_rec_buf
,
6073 MIGR_REC_BUF_SECTORS
*member_sector_size
) !=
6074 MIGR_REC_BUF_SECTORS
*member_sector_size
)
6075 perror("Write migr_rec failed");
6079 serialcpy(dd
->disk
.serial
, dd
->serial
);
6080 set_total_blocks(&dd
->disk
, size
);
6081 if (__le32_to_cpu(dd
->disk
.total_blocks_hi
) > 0) {
6082 struct imsm_super
*mpb
= super
->anchor
;
6083 mpb
->attributes
|= MPB_ATTRIB_2TB_DISK
;
6086 if (sysfs_disk_to_scsi_id(fd
, &id
) == 0)
6087 dd
->disk
.scsi_id
= __cpu_to_le32(id
);
6089 dd
->disk
.scsi_id
= __cpu_to_le32(0);
6091 if (st
->update_tail
) {
6092 dd
->next
= super
->disk_mgmt_list
;
6093 super
->disk_mgmt_list
= dd
;
6095 /* this is called outside of mdmon
6096 * write initial spare metadata
6097 * mdmon will overwrite it.
6099 dd
->next
= super
->disks
;
6101 write_super_imsm_spare(super
, dd
);
6107 static int remove_from_super_imsm(struct supertype
*st
, mdu_disk_info_t
*dk
)
6109 struct intel_super
*super
= st
->sb
;
6112 /* remove from super works only in mdmon - for communication
6113 * manager - monitor. Check if communication memory buffer
6116 if (!st
->update_tail
) {
6117 pr_err("shall be used in mdmon context only\n");
6120 dd
= xcalloc(1, sizeof(*dd
));
6121 dd
->major
= dk
->major
;
6122 dd
->minor
= dk
->minor
;
6125 dd
->action
= DISK_REMOVE
;
6127 dd
->next
= super
->disk_mgmt_list
;
6128 super
->disk_mgmt_list
= dd
;
6133 static int store_imsm_mpb(int fd
, struct imsm_super
*mpb
);
6136 char buf
[MAX_SECTOR_SIZE
];
6137 struct imsm_super anchor
;
6138 } spare_record
__attribute__ ((aligned(MAX_SECTOR_SIZE
)));
6141 static int write_super_imsm_spare(struct intel_super
*super
, struct dl
*d
)
6143 struct imsm_super
*mpb
= super
->anchor
;
6144 struct imsm_super
*spare
= &spare_record
.anchor
;
6150 spare
->mpb_size
= __cpu_to_le32(sizeof(struct imsm_super
));
6151 spare
->generation_num
= __cpu_to_le32(1UL);
6152 spare
->attributes
= MPB_ATTRIB_CHECKSUM_VERIFY
;
6153 spare
->num_disks
= 1;
6154 spare
->num_raid_devs
= 0;
6155 spare
->cache_size
= mpb
->cache_size
;
6156 spare
->pwr_cycle_count
= __cpu_to_le32(1);
6158 snprintf((char *) spare
->sig
, MAX_SIGNATURE_LENGTH
,
6159 MPB_SIGNATURE MPB_VERSION_RAID0
);
6161 spare
->disk
[0] = d
->disk
;
6162 if (__le32_to_cpu(d
->disk
.total_blocks_hi
) > 0)
6163 spare
->attributes
|= MPB_ATTRIB_2TB_DISK
;
6165 if (super
->sector_size
== 4096)
6166 convert_to_4k_imsm_disk(&spare
->disk
[0]);
6168 sum
= __gen_imsm_checksum(spare
);
6169 spare
->family_num
= __cpu_to_le32(sum
);
6170 spare
->orig_family_num
= 0;
6171 sum
= __gen_imsm_checksum(spare
);
6172 spare
->check_sum
= __cpu_to_le32(sum
);
6174 if (store_imsm_mpb(d
->fd
, spare
)) {
6175 pr_err("failed for device %d:%d %s\n",
6176 d
->major
, d
->minor
, strerror(errno
));
6182 /* spare records have their own family number and do not have any defined raid
6185 static int write_super_imsm_spares(struct intel_super
*super
, int doclose
)
6189 for (d
= super
->disks
; d
; d
= d
->next
) {
6193 if (write_super_imsm_spare(super
, d
))
6203 static int write_super_imsm(struct supertype
*st
, int doclose
)
6205 struct intel_super
*super
= st
->sb
;
6206 unsigned int sector_size
= super
->sector_size
;
6207 struct imsm_super
*mpb
= super
->anchor
;
6213 __u32 mpb_size
= sizeof(struct imsm_super
) - sizeof(struct imsm_disk
);
6215 int clear_migration_record
= 1;
6218 /* 'generation' is incremented everytime the metadata is written */
6219 generation
= __le32_to_cpu(mpb
->generation_num
);
6221 mpb
->generation_num
= __cpu_to_le32(generation
);
6223 /* fix up cases where previous mdadm releases failed to set
6226 if (mpb
->orig_family_num
== 0)
6227 mpb
->orig_family_num
= mpb
->family_num
;
6229 for (d
= super
->disks
; d
; d
= d
->next
) {
6233 mpb
->disk
[d
->index
] = d
->disk
;
6237 for (d
= super
->missing
; d
; d
= d
->next
) {
6238 mpb
->disk
[d
->index
] = d
->disk
;
6241 mpb
->num_disks
= num_disks
;
6242 mpb_size
+= sizeof(struct imsm_disk
) * mpb
->num_disks
;
6244 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
6245 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
6246 struct imsm_dev
*dev2
= get_imsm_dev(super
, i
);
6248 imsm_copy_dev(dev
, dev2
);
6249 mpb_size
+= sizeof_imsm_dev(dev
, 0);
6251 if (is_gen_migration(dev2
))
6252 clear_migration_record
= 0;
6255 bbm_log_size
= get_imsm_bbm_log_size(super
->bbm_log
);
6258 memcpy((void *)mpb
+ mpb_size
, super
->bbm_log
, bbm_log_size
);
6259 mpb
->attributes
|= MPB_ATTRIB_BBM
;
6261 mpb
->attributes
&= ~MPB_ATTRIB_BBM
;
6263 super
->anchor
->bbm_log_size
= __cpu_to_le32(bbm_log_size
);
6264 mpb_size
+= bbm_log_size
;
6265 mpb
->mpb_size
= __cpu_to_le32(mpb_size
);
6268 assert(super
->len
== 0 || mpb_size
<= super
->len
);
6271 /* recalculate checksum */
6272 sum
= __gen_imsm_checksum(mpb
);
6273 mpb
->check_sum
= __cpu_to_le32(sum
);
6275 if (super
->clean_migration_record_by_mdmon
) {
6276 clear_migration_record
= 1;
6277 super
->clean_migration_record_by_mdmon
= 0;
6279 if (clear_migration_record
)
6280 memset(super
->migr_rec_buf
, 0,
6281 MIGR_REC_BUF_SECTORS
*MAX_SECTOR_SIZE
);
6283 if (sector_size
== 4096)
6284 convert_to_4k(super
);
6286 /* write the mpb for disks that compose raid devices */
6287 for (d
= super
->disks
; d
; d
= d
->next
) {
6288 if (d
->index
< 0 || is_failed(&d
->disk
))
6291 if (clear_migration_record
) {
6292 unsigned long long dsize
;
6294 get_dev_size(d
->fd
, NULL
, &dsize
);
6295 if (lseek64(d
->fd
, dsize
- sector_size
,
6297 if ((unsigned int)write(d
->fd
,
6298 super
->migr_rec_buf
,
6299 MIGR_REC_BUF_SECTORS
*sector_size
) !=
6300 MIGR_REC_BUF_SECTORS
*sector_size
)
6301 perror("Write migr_rec failed");
6305 if (store_imsm_mpb(d
->fd
, mpb
))
6307 "failed for device %d:%d (fd: %d)%s\n",
6309 d
->fd
, strerror(errno
));
6316 return write_super_imsm_spares(super
, doclose
);
6321 static int create_array(struct supertype
*st
, int dev_idx
)
6324 struct imsm_update_create_array
*u
;
6325 struct intel_super
*super
= st
->sb
;
6326 struct imsm_dev
*dev
= get_imsm_dev(super
, dev_idx
);
6327 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
6328 struct disk_info
*inf
;
6329 struct imsm_disk
*disk
;
6332 len
= sizeof(*u
) - sizeof(*dev
) + sizeof_imsm_dev(dev
, 0) +
6333 sizeof(*inf
) * map
->num_members
;
6335 u
->type
= update_create_array
;
6336 u
->dev_idx
= dev_idx
;
6337 imsm_copy_dev(&u
->dev
, dev
);
6338 inf
= get_disk_info(u
);
6339 for (i
= 0; i
< map
->num_members
; i
++) {
6340 int idx
= get_imsm_disk_idx(dev
, i
, MAP_X
);
6342 disk
= get_imsm_disk(super
, idx
);
6344 disk
= get_imsm_missing(super
, idx
);
6345 serialcpy(inf
[i
].serial
, disk
->serial
);
6347 append_metadata_update(st
, u
, len
);
6352 static int mgmt_disk(struct supertype
*st
)
6354 struct intel_super
*super
= st
->sb
;
6356 struct imsm_update_add_remove_disk
*u
;
6358 if (!super
->disk_mgmt_list
)
6363 u
->type
= update_add_remove_disk
;
6364 append_metadata_update(st
, u
, len
);
6369 __u32
crc32c_le(__u32 crc
, unsigned char const *p
, size_t len
);
6371 static int write_ppl_header(unsigned long long ppl_sector
, int fd
, void *buf
)
6373 struct ppl_header
*ppl_hdr
= buf
;
6376 ppl_hdr
->checksum
= __cpu_to_le32(~crc32c_le(~0, buf
, PPL_HEADER_SIZE
));
6378 if (lseek64(fd
, ppl_sector
* 512, SEEK_SET
) < 0) {
6380 perror("Failed to seek to PPL header location");
6384 if (write(fd
, buf
, PPL_HEADER_SIZE
) != PPL_HEADER_SIZE
) {
6386 perror("Write PPL header failed");
6395 static int write_init_ppl_imsm(struct supertype
*st
, struct mdinfo
*info
, int fd
)
6397 struct intel_super
*super
= st
->sb
;
6399 struct ppl_header
*ppl_hdr
;
6402 /* first clear entire ppl space */
6403 ret
= zero_disk_range(fd
, info
->ppl_sector
, info
->ppl_size
);
6407 ret
= posix_memalign(&buf
, MAX_SECTOR_SIZE
, PPL_HEADER_SIZE
);
6409 pr_err("Failed to allocate PPL header buffer\n");
6413 memset(buf
, 0, PPL_HEADER_SIZE
);
6415 memset(ppl_hdr
->reserved
, 0xff, PPL_HDR_RESERVED
);
6416 ppl_hdr
->signature
= __cpu_to_le32(super
->anchor
->orig_family_num
);
6418 if (info
->mismatch_cnt
) {
6420 * We are overwriting an invalid ppl. Make one entry with wrong
6421 * checksum to prevent the kernel from skipping resync.
6423 ppl_hdr
->entries_count
= __cpu_to_le32(1);
6424 ppl_hdr
->entries
[0].checksum
= ~0;
6427 ret
= write_ppl_header(info
->ppl_sector
, fd
, buf
);
6433 static int is_rebuilding(struct imsm_dev
*dev
);
6435 static int validate_ppl_imsm(struct supertype
*st
, struct mdinfo
*info
,
6436 struct mdinfo
*disk
)
6438 struct intel_super
*super
= st
->sb
;
6440 void *buf_orig
, *buf
, *buf_prev
= NULL
;
6442 struct ppl_header
*ppl_hdr
= NULL
;
6444 struct imsm_dev
*dev
;
6447 unsigned long long ppl_offset
= 0;
6448 unsigned long long prev_gen_num
= 0;
6450 if (disk
->disk
.raid_disk
< 0)
6453 dev
= get_imsm_dev(super
, info
->container_member
);
6454 idx
= get_imsm_disk_idx(dev
, disk
->disk
.raid_disk
, MAP_0
);
6455 d
= get_imsm_dl_disk(super
, idx
);
6457 if (!d
|| d
->index
< 0 || is_failed(&d
->disk
))
6460 if (posix_memalign(&buf_orig
, MAX_SECTOR_SIZE
, PPL_HEADER_SIZE
* 2)) {
6461 pr_err("Failed to allocate PPL header buffer\n");
6467 while (ppl_offset
< MULTIPLE_PPL_AREA_SIZE_IMSM
) {
6470 dprintf("Checking potential PPL at offset: %llu\n", ppl_offset
);
6472 if (lseek64(d
->fd
, info
->ppl_sector
* 512 + ppl_offset
,
6474 perror("Failed to seek to PPL header location");
6479 if (read(d
->fd
, buf
, PPL_HEADER_SIZE
) != PPL_HEADER_SIZE
) {
6480 perror("Read PPL header failed");
6487 crc
= __le32_to_cpu(ppl_hdr
->checksum
);
6488 ppl_hdr
->checksum
= 0;
6490 if (crc
!= ~crc32c_le(~0, buf
, PPL_HEADER_SIZE
)) {
6491 dprintf("Wrong PPL header checksum on %s\n",
6496 if (prev_gen_num
> __le64_to_cpu(ppl_hdr
->generation
)) {
6497 /* previous was newest, it was already checked */
6501 if ((__le32_to_cpu(ppl_hdr
->signature
) !=
6502 super
->anchor
->orig_family_num
)) {
6503 dprintf("Wrong PPL header signature on %s\n",
6510 prev_gen_num
= __le64_to_cpu(ppl_hdr
->generation
);
6512 ppl_offset
+= PPL_HEADER_SIZE
;
6513 for (i
= 0; i
< __le32_to_cpu(ppl_hdr
->entries_count
); i
++)
6515 __le32_to_cpu(ppl_hdr
->entries
[i
].pp_size
);
6518 buf_prev
= buf
+ PPL_HEADER_SIZE
;
6530 * Update metadata to use mutliple PPLs area (1MB).
6531 * This is done once for all RAID members
6533 if (info
->consistency_policy
== CONSISTENCY_POLICY_PPL
&&
6534 info
->ppl_size
!= (MULTIPLE_PPL_AREA_SIZE_IMSM
>> 9)) {
6536 struct mdinfo
*member_dev
;
6538 sprintf(subarray
, "%d", info
->container_member
);
6540 if (mdmon_running(st
->container_devnm
))
6541 st
->update_tail
= &st
->updates
;
6543 if (st
->ss
->update_subarray(st
, subarray
, UOPT_PPL
, NULL
)) {
6544 pr_err("Failed to update subarray %s\n",
6547 if (st
->update_tail
)
6548 flush_metadata_updates(st
);
6550 st
->ss
->sync_metadata(st
);
6551 info
->ppl_size
= (MULTIPLE_PPL_AREA_SIZE_IMSM
>> 9);
6552 for (member_dev
= info
->devs
; member_dev
;
6553 member_dev
= member_dev
->next
)
6554 member_dev
->ppl_size
=
6555 (MULTIPLE_PPL_AREA_SIZE_IMSM
>> 9);
6560 struct imsm_map
*map
= get_imsm_map(dev
, MAP_X
);
6562 if (map
->map_state
== IMSM_T_STATE_UNINITIALIZED
||
6563 (map
->map_state
== IMSM_T_STATE_NORMAL
&&
6564 !(dev
->vol
.dirty
& RAIDVOL_DIRTY
)) ||
6565 (is_rebuilding(dev
) &&
6566 vol_curr_migr_unit(dev
) == 0 &&
6567 get_imsm_disk_idx(dev
, disk
->disk
.raid_disk
, MAP_1
) != idx
))
6568 ret
= st
->ss
->write_init_ppl(st
, info
, d
->fd
);
6570 info
->mismatch_cnt
++;
6571 } else if (ret
== 0 &&
6572 ppl_hdr
->entries_count
== 0 &&
6573 is_rebuilding(dev
) &&
6574 info
->resync_start
== 0) {
6576 * The header has no entries - add a single empty entry and
6577 * rewrite the header to prevent the kernel from going into
6578 * resync after an interrupted rebuild.
6580 ppl_hdr
->entries_count
= __cpu_to_le32(1);
6581 ret
= write_ppl_header(info
->ppl_sector
, d
->fd
, buf
);
6589 static int write_init_ppl_imsm_all(struct supertype
*st
, struct mdinfo
*info
)
6591 struct intel_super
*super
= st
->sb
;
6595 if (info
->consistency_policy
!= CONSISTENCY_POLICY_PPL
||
6596 info
->array
.level
!= 5)
6599 for (d
= super
->disks
; d
; d
= d
->next
) {
6600 if (d
->index
< 0 || is_failed(&d
->disk
))
6603 ret
= st
->ss
->write_init_ppl(st
, info
, d
->fd
);
6611 /*******************************************************************************
6612 * Function: write_init_bitmap_imsm_vol
6613 * Description: Write a bitmap header and prepares the area for the bitmap.
6615 * st : supertype information
6616 * vol_idx : the volume index to use
6621 ******************************************************************************/
6622 static int write_init_bitmap_imsm_vol(struct supertype
*st
, int vol_idx
)
6624 struct intel_super
*super
= st
->sb
;
6625 int prev_current_vol
= super
->current_vol
;
6629 super
->current_vol
= vol_idx
;
6630 for (d
= super
->disks
; d
; d
= d
->next
) {
6631 if (d
->index
< 0 || is_failed(&d
->disk
))
6633 ret
= st
->ss
->write_bitmap(st
, d
->fd
, NoUpdate
);
6637 super
->current_vol
= prev_current_vol
;
6641 /*******************************************************************************
6642 * Function: write_init_bitmap_imsm_all
6643 * Description: Write a bitmap header and prepares the area for the bitmap.
6644 * Operation is executed for volumes with CONSISTENCY_POLICY_BITMAP.
6646 * st : supertype information
6647 * info : info about the volume where the bitmap should be written
6648 * vol_idx : the volume index to use
6653 ******************************************************************************/
6654 static int write_init_bitmap_imsm_all(struct supertype
*st
, struct mdinfo
*info
,
6659 if (info
&& (info
->consistency_policy
== CONSISTENCY_POLICY_BITMAP
))
6660 ret
= write_init_bitmap_imsm_vol(st
, vol_idx
);
6665 static int write_init_super_imsm(struct supertype
*st
)
6667 struct intel_super
*super
= st
->sb
;
6668 int current_vol
= super
->current_vol
;
6672 getinfo_super_imsm(st
, &info
, NULL
);
6674 /* we are done with current_vol reset it to point st at the container */
6675 super
->current_vol
= -1;
6677 if (st
->update_tail
) {
6678 /* queue the recently created array / added disk
6679 * as a metadata update */
6681 /* determine if we are creating a volume or adding a disk */
6682 if (current_vol
< 0) {
6683 /* in the mgmt (add/remove) disk case we are running
6684 * in mdmon context, so don't close fd's
6688 /* adding the second volume to the array */
6689 rv
= write_init_ppl_imsm_all(st
, &info
);
6691 rv
= write_init_bitmap_imsm_all(st
, &info
, current_vol
);
6693 rv
= create_array(st
, current_vol
);
6697 for (d
= super
->disks
; d
; d
= d
->next
)
6698 Kill(d
->devname
, NULL
, 0, -1, 1);
6699 if (current_vol
>= 0) {
6700 rv
= write_init_ppl_imsm_all(st
, &info
);
6702 rv
= write_init_bitmap_imsm_all(st
, &info
, current_vol
);
6706 rv
= write_super_imsm(st
, 1);
6712 static int store_super_imsm(struct supertype
*st
, int fd
)
6714 struct intel_super
*super
= st
->sb
;
6715 struct imsm_super
*mpb
= super
? super
->anchor
: NULL
;
6720 if (super
->sector_size
== 4096)
6721 convert_to_4k(super
);
6722 return store_imsm_mpb(fd
, mpb
);
6725 static int validate_geometry_imsm_container(struct supertype
*st
, int level
,
6727 unsigned long long data_offset
,
6729 unsigned long long *freesize
,
6733 unsigned long long ldsize
;
6734 struct intel_super
*super
= NULL
;
6737 if (!is_container(level
))
6742 fd
= dev_open(dev
, O_RDONLY
|O_EXCL
);
6743 if (!is_fd_valid(fd
)) {
6744 pr_vrb("imsm: Cannot open %s: %s\n", dev
, strerror(errno
));
6747 if (!get_dev_size(fd
, dev
, &ldsize
))
6750 /* capabilities retrieve could be possible
6751 * note that there is no fd for the disks in array.
6753 super
= alloc_super();
6757 if (!get_dev_sector_size(fd
, NULL
, &super
->sector_size
))
6760 rv
= find_intel_hba_capability(fd
, super
, verbose
> 0 ? dev
: NULL
);
6764 fd2devname(fd
, str
);
6765 dprintf("fd: %d %s orom: %p rv: %d raiddisk: %d\n",
6766 fd
, str
, super
->orom
, rv
, raiddisks
);
6768 /* no orom/efi or non-intel hba of the disk */
6773 if (raiddisks
> super
->orom
->tds
) {
6775 pr_err("%d exceeds maximum number of platform supported disks: %d\n",
6776 raiddisks
, super
->orom
->tds
);
6779 if ((super
->orom
->attr
& IMSM_OROM_ATTR_2TB_DISK
) == 0 &&
6780 (ldsize
>> 9) >> 32 > 0) {
6782 pr_err("%s exceeds maximum platform supported size\n", dev
);
6786 if (super
->hba
->type
== SYS_DEV_VMD
||
6787 super
->hba
->type
== SYS_DEV_NVME
) {
6788 if (!imsm_is_nvme_namespace_supported(fd
, 1)) {
6790 pr_err("NVMe namespace %s is not supported by IMSM\n",
6797 *freesize
= avail_size_imsm(st
, ldsize
>> 9, data_offset
);
6807 static unsigned long long find_size(struct extent
*e
, int *idx
, int num_extents
)
6809 const unsigned long long base_start
= e
[*idx
].start
;
6810 unsigned long long end
= base_start
+ e
[*idx
].size
;
6813 if (base_start
== end
)
6817 for (i
= *idx
; i
< num_extents
; i
++) {
6818 /* extend overlapping extents */
6819 if (e
[i
].start
>= base_start
&&
6820 e
[i
].start
<= end
) {
6823 if (e
[i
].start
+ e
[i
].size
> end
)
6824 end
= e
[i
].start
+ e
[i
].size
;
6825 } else if (e
[i
].start
> end
) {
6831 return end
- base_start
;
6834 /** merge_extents() - analyze extents and get free size.
6835 * @super: Intel metadata, not NULL.
6836 * @expanding: if set, we are expanding &super->current_vol.
6838 * Build a composite disk with all known extents and generate a size given the
6839 * "all disks in an array must share a common start offset" constraint.
6840 * If a volume is expanded, then return free space after the volume.
6842 * Return: Free space or 0 on failure.
6844 static unsigned long long merge_extents(struct intel_super
*super
, const bool expanding
)
6848 int i
, j
, pos_vol_idx
= -1;
6850 int sum_extents
= 0;
6851 unsigned long long pos
= 0;
6852 unsigned long long start
= 0;
6853 unsigned long long free_size
= 0;
6855 unsigned long pre_reservation
= 0;
6856 unsigned long post_reservation
= IMSM_RESERVED_SECTORS
;
6857 unsigned long reservation_size
;
6859 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
6861 sum_extents
+= dl
->extent_cnt
;
6862 e
= xcalloc(sum_extents
, sizeof(struct extent
));
6864 /* coalesce and sort all extents. also, check to see if we need to
6865 * reserve space between member arrays
6868 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
6871 for (i
= 0; i
< dl
->extent_cnt
; i
++)
6874 qsort(e
, sum_extents
, sizeof(*e
), cmp_extent
);
6879 while (i
< sum_extents
) {
6880 e
[j
].start
= e
[i
].start
;
6881 e
[j
].vol
= e
[i
].vol
;
6882 e
[j
].size
= find_size(e
, &i
, sum_extents
);
6884 if (e
[j
-1].size
== 0)
6890 unsigned long long esize
= e
[i
].start
- pos
;
6892 if (expanding
? pos_vol_idx
== super
->current_vol
: esize
>= free_size
) {
6898 pos
= e
[i
].start
+ e
[i
].size
;
6899 pos_vol_idx
= e
[i
].vol
;
6902 } while (e
[i
-1].size
);
6904 if (free_size
== 0) {
6905 dprintf("imsm: Cannot find free size.\n");
6910 if (!expanding
&& extent_idx
!= 0)
6912 * Not a real first volume in a container is created, pre_reservation is needed.
6914 pre_reservation
= IMSM_RESERVED_SECTORS
;
6916 if (e
[extent_idx
].size
== 0)
6918 * extent_idx points to the metadata, post_reservation is allready done.
6920 post_reservation
= 0;
6923 reservation_size
= pre_reservation
+ post_reservation
;
6925 if (free_size
< reservation_size
) {
6926 dprintf("imsm: Reservation size is greater than free space.\n");
6930 super
->create_offset
= start
+ pre_reservation
;
6931 return free_size
- reservation_size
;
6934 static int is_raid_level_supported(const struct imsm_orom
*orom
, int level
, int raiddisks
)
6936 if (level
< 0 || level
== 6 || level
== 4)
6939 /* if we have an orom prevent invalid raid levels */
6942 case 0: return imsm_orom_has_raid0(orom
);
6945 return imsm_orom_has_raid1e(orom
);
6946 return imsm_orom_has_raid1(orom
) && raiddisks
== 2;
6947 case 10: return imsm_orom_has_raid10(orom
) && raiddisks
== 4;
6948 case 5: return imsm_orom_has_raid5(orom
) && raiddisks
> 2;
6951 return 1; /* not on an Intel RAID platform so anything goes */
6957 active_arrays_by_format(char *name
, char* hba
, struct md_list
**devlist
,
6958 int dpa
, int verbose
)
6960 struct mdstat_ent
*mdstat
= mdstat_read(0, 0);
6961 struct mdstat_ent
*memb
;
6967 for (memb
= mdstat
; memb
; memb
= memb
->next
) {
6968 if (memb
->metadata_version
&&
6969 (strncmp(memb
->metadata_version
, "external:", 9) == 0) &&
6970 (strcmp(&memb
->metadata_version
[9], name
) == 0) &&
6971 !is_subarray(memb
->metadata_version
+9) &&
6973 struct dev_member
*dev
= memb
->members
;
6975 while (dev
&& !is_fd_valid(fd
)) {
6976 char *path
= xmalloc(strlen(dev
->name
) + strlen("/dev/") + 1);
6977 num
= snprintf(path
, PATH_MAX
, "%s%s", "/dev/", dev
->name
);
6979 fd
= open(path
, O_RDONLY
, 0);
6980 if (num
<= 0 || !is_fd_valid(fd
)) {
6981 pr_vrb("Cannot open %s: %s\n",
6982 dev
->name
, strerror(errno
));
6988 if (is_fd_valid(fd
) && disk_attached_to_hba(fd
, hba
)) {
6989 struct mdstat_ent
*vol
;
6990 for (vol
= mdstat
; vol
; vol
= vol
->next
) {
6991 if (vol
->active
> 0 &&
6992 vol
->metadata_version
&&
6993 is_container_member(vol
, memb
->devnm
)) {
6998 if (*devlist
&& (found
< dpa
)) {
6999 dv
= xcalloc(1, sizeof(*dv
));
7000 dv
->devname
= xmalloc(strlen(memb
->devnm
) + strlen("/dev/") + 1);
7001 sprintf(dv
->devname
, "%s%s", "/dev/", memb
->devnm
);
7004 dv
->next
= *devlist
;
7011 free_mdstat(mdstat
);
7016 static struct md_list
*
7017 get_loop_devices(void)
7020 struct md_list
*devlist
= NULL
;
7023 for(i
= 0; i
< 12; i
++) {
7024 dv
= xcalloc(1, sizeof(*dv
));
7025 dv
->devname
= xmalloc(40);
7026 sprintf(dv
->devname
, "/dev/loop%d", i
);
7034 static struct md_list
*
7035 get_devices(const char *hba_path
)
7037 struct md_list
*devlist
= NULL
;
7044 devlist
= get_loop_devices();
7047 /* scroll through /sys/dev/block looking for devices attached to
7050 dir
= opendir("/sys/dev/block");
7051 for (ent
= dir
? readdir(dir
) : NULL
; ent
; ent
= readdir(dir
)) {
7056 if (sscanf(ent
->d_name
, "%d:%d", &major
, &minor
) != 2)
7058 path
= devt_to_devpath(makedev(major
, minor
), 1, NULL
);
7061 if (!path_attached_to_hba(path
, hba_path
)) {
7068 fd
= dev_open(ent
->d_name
, O_RDONLY
);
7069 if (is_fd_valid(fd
)) {
7070 fd2devname(fd
, buf
);
7073 pr_err("cannot open device: %s\n",
7078 dv
= xcalloc(1, sizeof(*dv
));
7079 dv
->devname
= xstrdup(buf
);
7086 devlist
= devlist
->next
;
7096 count_volumes_list(struct md_list
*devlist
, char *homehost
,
7097 int verbose
, int *found
)
7099 struct md_list
*tmpdev
;
7101 struct supertype
*st
;
7103 /* first walk the list of devices to find a consistent set
7104 * that match the criterea, if that is possible.
7105 * We flag the ones we like with 'used'.
7108 st
= match_metadata_desc_imsm("imsm");
7110 pr_vrb("cannot allocate memory for imsm supertype\n");
7114 for (tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
7115 char *devname
= tmpdev
->devname
;
7117 struct supertype
*tst
;
7119 if (tmpdev
->used
> 1)
7121 tst
= dup_super(st
);
7123 pr_vrb("cannot allocate memory for imsm supertype\n");
7126 tmpdev
->container
= 0;
7127 dfd
= dev_open(devname
, O_RDONLY
|O_EXCL
);
7128 if (!is_fd_valid(dfd
)) {
7129 dprintf("cannot open device %s: %s\n",
7130 devname
, strerror(errno
));
7132 } else if (!fstat_is_blkdev(dfd
, devname
, &rdev
)) {
7134 } else if (must_be_container(dfd
)) {
7135 struct supertype
*cst
;
7136 cst
= super_by_fd(dfd
, NULL
);
7138 dprintf("cannot recognize container type %s\n",
7141 } else if (tst
->ss
!= st
->ss
) {
7142 dprintf("non-imsm container - ignore it: %s\n",
7145 } else if (!tst
->ss
->load_container
||
7146 tst
->ss
->load_container(tst
, dfd
, NULL
))
7149 tmpdev
->container
= 1;
7152 cst
->ss
->free_super(cst
);
7154 tmpdev
->st_rdev
= rdev
;
7155 if (tst
->ss
->load_super(tst
,dfd
, NULL
)) {
7156 dprintf("no RAID superblock on %s\n",
7159 } else if (tst
->ss
->compare_super
== NULL
) {
7160 dprintf("Cannot assemble %s metadata on %s\n",
7161 tst
->ss
->name
, devname
);
7167 if (tmpdev
->used
== 2 || tmpdev
->used
== 4) {
7168 /* Ignore unrecognised devices during auto-assembly */
7173 tst
->ss
->getinfo_super(tst
, &info
, NULL
);
7175 if (st
->minor_version
== -1)
7176 st
->minor_version
= tst
->minor_version
;
7178 if (memcmp(info
.uuid
, uuid_zero
,
7179 sizeof(int[4])) == 0) {
7180 /* this is a floating spare. It cannot define
7181 * an array unless there are no more arrays of
7182 * this type to be found. It can be included
7183 * in an array of this type though.
7189 if (st
->ss
!= tst
->ss
||
7190 st
->minor_version
!= tst
->minor_version
||
7191 st
->ss
->compare_super(st
, tst
, 1) != 0) {
7192 /* Some mismatch. If exactly one array matches this host,
7193 * we can resolve on that one.
7194 * Or, if we are auto assembling, we just ignore the second
7197 dprintf("superblock on %s doesn't match others - assembly aborted\n",
7203 dprintf("found: devname: %s\n", devname
);
7207 tst
->ss
->free_super(tst
);
7211 if ((err
= load_super_imsm_all(st
, -1, &st
->sb
, NULL
, devlist
, 0)) == 0) {
7212 struct mdinfo
*iter
, *head
= st
->ss
->container_content(st
, NULL
);
7213 for (iter
= head
; iter
; iter
= iter
->next
) {
7214 dprintf("content->text_version: %s vol\n",
7215 iter
->text_version
);
7216 if (iter
->array
.state
& (1<<MD_SB_BLOCK_VOLUME
)) {
7217 /* do not assemble arrays with unsupported
7219 dprintf("Cannot activate member %s.\n",
7220 iter
->text_version
);
7227 dprintf("No valid super block on device list: err: %d %p\n",
7231 dprintf("no more devices to examine\n");
7234 for (tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
7235 if (tmpdev
->used
== 1 && tmpdev
->found
) {
7237 if (count
< tmpdev
->found
)
7240 count
-= tmpdev
->found
;
7243 if (tmpdev
->used
== 1)
7248 st
->ss
->free_super(st
);
7252 static int __count_volumes(char *hba_path
, int dpa
, int verbose
,
7255 struct sys_dev
*idev
, *intel_devices
= find_intel_devices();
7257 const struct orom_entry
*entry
;
7258 struct devid_list
*dv
, *devid_list
;
7263 for (idev
= intel_devices
; idev
; idev
= idev
->next
) {
7264 if (strstr(idev
->path
, hba_path
))
7268 if (!idev
|| !idev
->dev_id
)
7271 entry
= get_orom_entry_by_device_id(idev
->dev_id
);
7273 if (!entry
|| !entry
->devid_list
)
7276 devid_list
= entry
->devid_list
;
7277 for (dv
= devid_list
; dv
; dv
= dv
->next
) {
7278 struct md_list
*devlist
;
7279 struct sys_dev
*device
= NULL
;
7284 device
= device_by_id_and_path(dv
->devid
, hba_path
);
7286 device
= device_by_id(dv
->devid
);
7289 hpath
= device
->path
;
7293 devlist
= get_devices(hpath
);
7294 /* if no intel devices return zero volumes */
7295 if (devlist
== NULL
)
7298 count
+= active_arrays_by_format("imsm", hpath
, &devlist
, dpa
,
7300 dprintf("path: %s active arrays: %d\n", hpath
, count
);
7301 if (devlist
== NULL
)
7305 count
+= count_volumes_list(devlist
,
7309 dprintf("found %d count: %d\n", found
, count
);
7312 dprintf("path: %s total number of volumes: %d\n", hpath
, count
);
7315 struct md_list
*dv
= devlist
;
7316 devlist
= devlist
->next
;
7324 static int count_volumes(struct intel_hba
*hba
, int dpa
, int verbose
)
7328 if (hba
->type
== SYS_DEV_VMD
) {
7329 struct sys_dev
*dev
;
7332 for (dev
= find_intel_devices(); dev
; dev
= dev
->next
) {
7333 if (dev
->type
== SYS_DEV_VMD
)
7334 count
+= __count_volumes(dev
->path
, dpa
,
7339 return __count_volumes(hba
->path
, dpa
, verbose
, 0);
7342 static int imsm_default_chunk(const struct imsm_orom
*orom
)
7344 /* up to 512 if the plaform supports it, otherwise the platform max.
7345 * 128 if no platform detected
7347 int fs
= max(7, orom
? fls(orom
->sss
) : 0);
7349 return min(512, (1 << fs
));
7353 validate_geometry_imsm_orom(struct intel_super
*super
, int level
, int layout
,
7354 int raiddisks
, int *chunk
, unsigned long long size
, int verbose
)
7356 /* check/set platform and metadata limits/defaults */
7357 if (super
->orom
&& raiddisks
> super
->orom
->dpa
) {
7358 pr_vrb("platform supports a maximum of %d disks per array\n",
7363 /* capabilities of OROM tested - copied from validate_geometry_imsm_volume */
7364 if (!is_raid_level_supported(super
->orom
, level
, raiddisks
)) {
7365 pr_vrb("platform does not support raid%d with %d disk%s\n",
7366 level
, raiddisks
, raiddisks
> 1 ? "s" : "");
7370 if (*chunk
== 0 || *chunk
== UnSet
)
7371 *chunk
= imsm_default_chunk(super
->orom
);
7373 if (super
->orom
&& !imsm_orom_has_chunk(super
->orom
, *chunk
)) {
7374 pr_vrb("platform does not support a chunk size of: %d\n", *chunk
);
7378 if (layout
!= imsm_level_to_layout(level
)) {
7380 pr_vrb("imsm raid 5 only supports the left-asymmetric layout\n");
7381 else if (level
== 10)
7382 pr_vrb("imsm raid 10 only supports the n2 layout\n");
7384 pr_vrb("imsm unknown layout %#x for this raid level %d\n",
7389 if (super
->orom
&& (super
->orom
->attr
& IMSM_OROM_ATTR_2TB
) == 0 &&
7390 (calc_array_size(level
, raiddisks
, layout
, *chunk
, size
) >> 32) > 0) {
7391 pr_vrb("platform does not support a volume size over 2TB\n");
7398 /* validate_geometry_imsm_volume - lifted from validate_geometry_ddf_bvd
7399 * FIX ME add ahci details
7401 static int validate_geometry_imsm_volume(struct supertype
*st
, int level
,
7402 int layout
, int raiddisks
, int *chunk
,
7403 unsigned long long size
,
7404 unsigned long long data_offset
,
7406 unsigned long long *freesize
,
7410 struct intel_super
*super
= st
->sb
;
7411 struct imsm_super
*mpb
;
7413 unsigned long long pos
= 0;
7414 unsigned long long maxsize
;
7418 /* We must have the container info already read in. */
7422 mpb
= super
->anchor
;
7424 if (!validate_geometry_imsm_orom(super
, level
, layout
, raiddisks
, chunk
, size
, verbose
)) {
7425 pr_err("RAID geometry validation failed. Cannot proceed with the action(s).\n");
7429 /* General test: make sure there is space for
7430 * 'raiddisks' device extents of size 'size' at a given
7433 unsigned long long minsize
= size
;
7434 unsigned long long start_offset
= MaxSector
;
7437 minsize
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
7438 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
7443 e
= get_extents(super
, dl
, 0);
7446 unsigned long long esize
;
7447 esize
= e
[i
].start
- pos
;
7448 if (esize
>= minsize
)
7450 if (found
&& start_offset
== MaxSector
) {
7453 } else if (found
&& pos
!= start_offset
) {
7457 pos
= e
[i
].start
+ e
[i
].size
;
7459 } while (e
[i
-1].size
);
7464 if (dcnt
< raiddisks
) {
7466 pr_err("imsm: Not enough devices with space for this array (%d < %d)\n",
7473 /* This device must be a member of the set */
7474 if (!stat_is_blkdev(dev
, &rdev
))
7476 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
7477 if (dl
->major
== (int)major(rdev
) &&
7478 dl
->minor
== (int)minor(rdev
))
7483 pr_err("%s is not in the same imsm set\n", dev
);
7485 } else if (super
->orom
&& dl
->index
< 0 && mpb
->num_raid_devs
) {
7486 /* If a volume is present then the current creation attempt
7487 * cannot incorporate new spares because the orom may not
7488 * understand this configuration (all member disks must be
7489 * members of each array in the container).
7491 pr_err("%s is a spare and a volume is already defined for this container\n", dev
);
7492 pr_err("The option-rom requires all member disks to be a member of all volumes\n");
7494 } else if (super
->orom
&& mpb
->num_raid_devs
> 0 &&
7495 mpb
->num_disks
!= raiddisks
) {
7496 pr_err("The option-rom requires all member disks to be a member of all volumes\n");
7500 /* retrieve the largest free space block */
7501 e
= get_extents(super
, dl
, 0);
7506 unsigned long long esize
;
7508 esize
= e
[i
].start
- pos
;
7509 if (esize
>= maxsize
)
7511 pos
= e
[i
].start
+ e
[i
].size
;
7513 } while (e
[i
-1].size
);
7518 pr_err("unable to determine free space for: %s\n",
7522 if (maxsize
< size
) {
7524 pr_err("%s not enough space (%llu < %llu)\n",
7525 dev
, maxsize
, size
);
7529 maxsize
= merge_extents(super
, false);
7531 if (mpb
->num_raid_devs
> 0 && size
&& size
!= maxsize
)
7532 pr_err("attempting to create a second volume with size less then remaining space.\n");
7534 if (maxsize
< size
|| maxsize
== 0) {
7537 pr_err("no free space left on device. Aborting...\n");
7539 pr_err("not enough space to create volume of given size (%llu < %llu). Aborting...\n",
7545 *freesize
= maxsize
;
7548 int count
= count_volumes(super
->hba
,
7549 super
->orom
->dpa
, verbose
);
7550 if (super
->orom
->vphba
<= count
) {
7551 pr_vrb("platform does not support more than %d raid volumes.\n",
7552 super
->orom
->vphba
);
7560 * imsm_get_free_size() - get the biggest, common free space from members.
7561 * @super: &intel_super pointer, not NULL.
7562 * @raiddisks: number of raid disks.
7563 * @size: requested size, could be 0 (means max size).
7564 * @chunk: requested chunk size in KiB.
7565 * @freesize: pointer for returned size value.
7567 * Return: &IMSM_STATUS_OK or &IMSM_STATUS_ERROR.
7569 * @freesize is set to meaningful value, this can be @size, or calculated
7571 * super->create_offset value is modified and set appropriately in
7572 * merge_extends() for further creation.
7574 static imsm_status_t
imsm_get_free_size(struct intel_super
*super
,
7575 const int raiddisks
,
7576 unsigned long long size
,
7578 unsigned long long *freesize
,
7581 struct imsm_super
*mpb
= super
->anchor
;
7587 unsigned long long maxsize
;
7588 unsigned long long minsize
= size
;
7591 minsize
= chunk
* 2;
7593 /* find the largest common start free region of the possible disks */
7594 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
7600 /* don't activate new spares if we are orom constrained
7601 * and there is already a volume active in the container
7603 if (super
->orom
&& dl
->index
< 0 && mpb
->num_raid_devs
)
7606 e
= get_extents(super
, dl
, 0);
7609 for (i
= 1; e
[i
-1].size
; i
++)
7616 maxsize
= merge_extents(super
, expanding
);
7617 if (maxsize
< minsize
) {
7618 pr_err("imsm: Free space is %llu but must be equal or larger than %llu.\n",
7620 return IMSM_STATUS_ERROR
;
7623 if (cnt
< raiddisks
|| (super
->orom
&& used
&& used
!= raiddisks
)) {
7624 pr_err("imsm: Not enough devices with space to create array.\n");
7625 return IMSM_STATUS_ERROR
;
7636 if (mpb
->num_raid_devs
> 0 && size
&& size
!= maxsize
)
7637 pr_err("attempting to create a second volume with size less then remaining space.\n");
7640 dprintf("imsm: imsm_get_free_size() returns : %llu\n", size
);
7642 return IMSM_STATUS_OK
;
7646 * autolayout_imsm() - automatically layout a new volume.
7647 * @super: &intel_super pointer, not NULL.
7648 * @raiddisks: number of raid disks.
7649 * @size: requested size, could be 0 (means max size).
7650 * @chunk: requested chunk.
7651 * @freesize: pointer for returned size value.
7653 * We are being asked to automatically layout a new volume based on the current
7654 * contents of the container. If the parameters can be satisfied autolayout_imsm
7655 * will record the disks, start offset, and will return size of the volume to
7656 * be created. See imsm_get_free_size() for details.
7657 * add_to_super() and getinfo_super() detect when autolayout is in progress.
7658 * If first volume exists, slots are set consistently to it.
7660 * Return: &IMSM_STATUS_OK on success, &IMSM_STATUS_ERROR otherwise.
7662 * Disks are marked for creation via dl->raiddisk.
7664 static imsm_status_t
autolayout_imsm(struct intel_super
*super
,
7665 const int raiddisks
,
7666 unsigned long long size
, const int chunk
,
7667 unsigned long long *freesize
)
7671 int vol_cnt
= super
->anchor
->num_raid_devs
;
7674 rv
= imsm_get_free_size(super
, raiddisks
, size
, chunk
, freesize
, false);
7675 if (rv
!= IMSM_STATUS_OK
)
7676 return IMSM_STATUS_ERROR
;
7678 for (disk
= super
->disks
; disk
; disk
= disk
->next
) {
7682 if (curr_slot
== raiddisks
)
7686 disk
->raiddisk
= curr_slot
;
7688 int _slot
= get_disk_slot_in_dev(super
, 0, disk
->index
);
7691 pr_err("Disk %s is not used in first volume, aborting\n",
7693 return IMSM_STATUS_ERROR
;
7695 disk
->raiddisk
= _slot
;
7700 return IMSM_STATUS_OK
;
7703 static int validate_geometry_imsm(struct supertype
*st
, int level
, int layout
,
7704 int raiddisks
, int *chunk
, unsigned long long size
,
7705 unsigned long long data_offset
,
7706 char *dev
, unsigned long long *freesize
,
7707 int consistency_policy
, int verbose
)
7714 * if given unused devices create a container
7715 * if given given devices in a container create a member volume
7717 if (is_container(level
))
7718 /* Must be a fresh device to add to a container */
7719 return validate_geometry_imsm_container(st
, level
, raiddisks
,
7724 * Size is given in sectors.
7726 if (size
&& (size
< 2048)) {
7727 pr_err("Given size must be greater than 1M.\n");
7728 /* Depends on algorithm in Create.c :
7729 * if container was given (dev == NULL) return -1,
7730 * if block device was given ( dev != NULL) return 0.
7732 return dev
? -1 : 0;
7736 struct intel_super
*super
= st
->sb
;
7739 * Autolayout mode, st->sb must be set.
7742 pr_vrb("superblock must be set for autolayout, aborting\n");
7746 if (!validate_geometry_imsm_orom(st
->sb
, level
, layout
,
7747 raiddisks
, chunk
, size
,
7751 if (super
->orom
&& freesize
) {
7753 int count
= count_volumes(super
->hba
, super
->orom
->dpa
,
7755 if (super
->orom
->vphba
<= count
) {
7756 pr_vrb("platform does not support more than %d raid volumes.\n",
7757 super
->orom
->vphba
);
7761 rv
= autolayout_imsm(super
, raiddisks
, size
, *chunk
,
7763 if (rv
!= IMSM_STATUS_OK
)
7769 /* creating in a given container */
7770 return validate_geometry_imsm_volume(st
, level
, layout
,
7771 raiddisks
, chunk
, size
,
7773 dev
, freesize
, verbose
);
7776 /* This device needs to be a device in an 'imsm' container */
7777 fd
= open(dev
, O_RDONLY
|O_EXCL
, 0);
7779 if (is_fd_valid(fd
)) {
7780 pr_vrb("Cannot create this array on device %s\n", dev
);
7785 fd
= open(dev
, O_RDONLY
, 0);
7787 if (!is_fd_valid(fd
)) {
7788 pr_vrb("Cannot open %s: %s\n", dev
, strerror(errno
));
7792 /* Well, it is in use by someone, maybe an 'imsm' container. */
7793 cfd
= open_container(fd
);
7796 if (!is_fd_valid(cfd
)) {
7797 pr_vrb("Cannot use %s: It is busy\n", dev
);
7800 sra
= sysfs_read(cfd
, NULL
, GET_VERSION
);
7801 if (sra
&& sra
->array
.major_version
== -1 &&
7802 strcmp(sra
->text_version
, "imsm") == 0)
7806 /* This is a member of a imsm container. Load the container
7807 * and try to create a volume
7809 struct intel_super
*super
;
7811 if (load_super_imsm_all(st
, cfd
, (void **) &super
, NULL
, NULL
, 1) == 0) {
7813 strcpy(st
->container_devnm
, fd2devnm(cfd
));
7815 return validate_geometry_imsm_volume(st
, level
, layout
,
7817 size
, data_offset
, dev
,
7824 pr_err("failed container membership check\n");
7830 static void default_geometry_imsm(struct supertype
*st
, int *level
, int *layout
, int *chunk
)
7832 struct intel_super
*super
= st
->sb
;
7834 if (level
&& *level
== UnSet
)
7835 *level
= LEVEL_CONTAINER
;
7837 if (level
&& layout
&& *layout
== UnSet
)
7838 *layout
= imsm_level_to_layout(*level
);
7840 if (chunk
&& (*chunk
== UnSet
|| *chunk
== 0))
7841 *chunk
= imsm_default_chunk(super
->orom
);
7844 static void handle_missing(struct intel_super
*super
, struct imsm_dev
*dev
);
7846 static int kill_subarray_imsm(struct supertype
*st
, char *subarray_id
)
7848 /* remove the subarray currently referenced by subarray_id */
7850 struct intel_dev
**dp
;
7851 struct intel_super
*super
= st
->sb
;
7852 __u8 current_vol
= strtoul(subarray_id
, NULL
, 10);
7853 struct imsm_super
*mpb
= super
->anchor
;
7855 if (mpb
->num_raid_devs
== 0)
7858 /* block deletions that would change the uuid of active subarrays
7860 * FIXME when immutable ids are available, but note that we'll
7861 * also need to fixup the invalidated/active subarray indexes in
7864 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
7867 if (i
< current_vol
)
7869 snprintf(subarray
, sizeof(subarray
), "%u", i
);
7870 if (is_subarray_active(subarray
, st
->devnm
)) {
7871 pr_err("deleting subarray-%d would change the UUID of active subarray-%d, aborting\n",
7878 if (st
->update_tail
) {
7879 struct imsm_update_kill_array
*u
= xmalloc(sizeof(*u
));
7881 u
->type
= update_kill_array
;
7882 u
->dev_idx
= current_vol
;
7883 append_metadata_update(st
, u
, sizeof(*u
));
7888 for (dp
= &super
->devlist
; *dp
;)
7889 if ((*dp
)->index
== current_vol
) {
7892 handle_missing(super
, (*dp
)->dev
);
7893 if ((*dp
)->index
> current_vol
)
7898 /* no more raid devices, all active components are now spares,
7899 * but of course failed are still failed
7901 if (--mpb
->num_raid_devs
== 0) {
7904 for (d
= super
->disks
; d
; d
= d
->next
)
7909 super
->updates_pending
++;
7915 * get_rwh_policy_from_update() - Get the rwh policy for update option.
7916 * @update: Update option.
7918 static int get_rwh_policy_from_update(enum update_opt update
)
7922 return RWH_MULTIPLE_DISTRIBUTED
;
7924 return RWH_MULTIPLE_OFF
;
7927 case UOPT_NO_BITMAP
:
7932 return UOPT_UNDEFINED
;
7935 static int update_subarray_imsm(struct supertype
*st
, char *subarray
,
7936 enum update_opt update
, struct mddev_ident
*ident
)
7938 /* update the subarray currently referenced by ->current_vol */
7939 struct intel_super
*super
= st
->sb
;
7940 struct imsm_super
*mpb
= super
->anchor
;
7942 if (update
== UOPT_NAME
) {
7943 char *name
= ident
->name
;
7947 if (imsm_is_name_allowed(super
, name
, 1) == false)
7950 vol
= strtoul(subarray
, &ep
, 10);
7951 if (*ep
!= '\0' || vol
>= super
->anchor
->num_raid_devs
)
7954 if (st
->update_tail
) {
7955 struct imsm_update_rename_array
*u
= xmalloc(sizeof(*u
));
7957 u
->type
= update_rename_array
;
7959 strncpy((char *) u
->name
, name
, MAX_RAID_SERIAL_LEN
);
7960 u
->name
[MAX_RAID_SERIAL_LEN
-1] = '\0';
7961 append_metadata_update(st
, u
, sizeof(*u
));
7963 struct imsm_dev
*dev
;
7966 dev
= get_imsm_dev(super
, vol
);
7967 memset(dev
->volume
, '\0', MAX_RAID_SERIAL_LEN
);
7968 namelen
= min((int)strlen(name
), MAX_RAID_SERIAL_LEN
);
7969 memcpy(dev
->volume
, name
, namelen
);
7970 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
7971 dev
= get_imsm_dev(super
, i
);
7972 handle_missing(super
, dev
);
7974 super
->updates_pending
++;
7976 } else if (get_rwh_policy_from_update(update
) != UOPT_UNDEFINED
) {
7979 int vol
= strtoul(subarray
, &ep
, 10);
7981 if (*ep
!= '\0' || vol
>= super
->anchor
->num_raid_devs
)
7984 new_policy
= get_rwh_policy_from_update(update
);
7986 if (st
->update_tail
) {
7987 struct imsm_update_rwh_policy
*u
= xmalloc(sizeof(*u
));
7989 u
->type
= update_rwh_policy
;
7991 u
->new_policy
= new_policy
;
7992 append_metadata_update(st
, u
, sizeof(*u
));
7994 struct imsm_dev
*dev
;
7996 dev
= get_imsm_dev(super
, vol
);
7997 dev
->rwh_policy
= new_policy
;
7998 super
->updates_pending
++;
8000 if (new_policy
== RWH_BITMAP
)
8001 return write_init_bitmap_imsm_vol(st
, vol
);
8008 static bool is_gen_migration(struct imsm_dev
*dev
)
8010 if (dev
&& dev
->vol
.migr_state
&&
8011 migr_type(dev
) == MIGR_GEN_MIGR
)
8017 static int is_rebuilding(struct imsm_dev
*dev
)
8019 struct imsm_map
*migr_map
;
8021 if (!dev
->vol
.migr_state
)
8024 if (migr_type(dev
) != MIGR_REBUILD
)
8027 migr_map
= get_imsm_map(dev
, MAP_1
);
8029 if (migr_map
->map_state
== IMSM_T_STATE_DEGRADED
)
8035 static int is_initializing(struct imsm_dev
*dev
)
8037 struct imsm_map
*migr_map
;
8039 if (!dev
->vol
.migr_state
)
8042 if (migr_type(dev
) != MIGR_INIT
)
8045 migr_map
= get_imsm_map(dev
, MAP_1
);
8047 if (migr_map
->map_state
== IMSM_T_STATE_UNINITIALIZED
)
8053 static void update_recovery_start(struct intel_super
*super
,
8054 struct imsm_dev
*dev
,
8055 struct mdinfo
*array
)
8057 struct mdinfo
*rebuild
= NULL
;
8061 if (!is_rebuilding(dev
))
8064 /* Find the rebuild target, but punt on the dual rebuild case */
8065 for (d
= array
->devs
; d
; d
= d
->next
)
8066 if (d
->recovery_start
== 0) {
8073 /* (?) none of the disks are marked with
8074 * IMSM_ORD_REBUILD, so assume they are missing and the
8075 * disk_ord_tbl was not correctly updated
8077 dprintf("failed to locate out-of-sync disk\n");
8081 units
= vol_curr_migr_unit(dev
);
8082 rebuild
->recovery_start
= units
* blocks_per_migr_unit(super
, dev
);
8085 static int recover_backup_imsm(struct supertype
*st
, struct mdinfo
*info
);
8087 static struct mdinfo
*container_content_imsm(struct supertype
*st
, char *subarray
)
8089 /* Given a container loaded by load_super_imsm_all,
8090 * extract information about all the arrays into
8092 * If 'subarray' is given, just extract info about that array.
8094 * For each imsm_dev create an mdinfo, fill it in,
8095 * then look for matching devices in super->disks
8096 * and create appropriate device mdinfo.
8098 struct intel_super
*super
= st
->sb
;
8099 struct imsm_super
*mpb
= super
->anchor
;
8100 struct mdinfo
*rest
= NULL
;
8104 int spare_disks
= 0;
8105 int current_vol
= super
->current_vol
;
8107 /* do not assemble arrays when not all attributes are supported */
8108 if (imsm_check_attributes(mpb
->attributes
) == 0) {
8110 pr_err("Unsupported attributes in IMSM metadata.Arrays activation is blocked.\n");
8113 /* count spare devices, not used in maps
8115 for (d
= super
->disks
; d
; d
= d
->next
)
8119 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
8120 struct imsm_dev
*dev
;
8121 struct imsm_map
*map
;
8122 struct imsm_map
*map2
;
8123 struct mdinfo
*this;
8130 (i
!= strtoul(subarray
, &ep
, 10) || *ep
!= '\0'))
8133 dev
= get_imsm_dev(super
, i
);
8134 map
= get_imsm_map(dev
, MAP_0
);
8135 map2
= get_imsm_map(dev
, MAP_1
);
8136 level
= get_imsm_raid_level(map
);
8138 /* do not publish arrays that are in the middle of an
8139 * unsupported migration
8141 if (dev
->vol
.migr_state
&&
8142 (migr_type(dev
) == MIGR_STATE_CHANGE
)) {
8143 pr_err("cannot assemble volume '%.16s': unsupported migration in progress\n",
8147 /* do not publish arrays that are not support by controller's
8151 this = xmalloc(sizeof(*this));
8153 super
->current_vol
= i
;
8154 getinfo_super_imsm_volume(st
, this, NULL
);
8156 chunk
= __le16_to_cpu(map
->blocks_per_strip
) >> 1;
8157 /* mdadm does not support all metadata features- set the bit in all arrays state */
8158 if (!validate_geometry_imsm_orom(super
,
8159 level
, /* RAID level */
8160 imsm_level_to_layout(level
),
8161 map
->num_members
, /* raid disks */
8162 &chunk
, imsm_dev_size(dev
),
8164 pr_err("IMSM RAID geometry validation failed. Array %s activation is blocked.\n",
8166 this->array
.state
|=
8167 (1<<MD_SB_BLOCK_CONTAINER_RESHAPE
) |
8168 (1<<MD_SB_BLOCK_VOLUME
);
8171 /* if array has bad blocks, set suitable bit in all arrays state */
8173 this->array
.state
|=
8174 (1<<MD_SB_BLOCK_CONTAINER_RESHAPE
) |
8175 (1<<MD_SB_BLOCK_VOLUME
);
8177 for (slot
= 0 ; slot
< map
->num_members
; slot
++) {
8178 unsigned long long recovery_start
;
8179 struct mdinfo
*info_d
;
8187 idx
= get_imsm_disk_idx(dev
, slot
, MAP_0
);
8188 ord
= get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
);
8189 for (d
= super
->disks
; d
; d
= d
->next
)
8190 if (d
->index
== idx
)
8193 recovery_start
= MaxSector
;
8196 if (d
&& is_failed(&d
->disk
))
8198 if (!skip
&& (ord
& IMSM_ORD_REBUILD
))
8200 if (!(ord
& IMSM_ORD_REBUILD
))
8201 this->array
.working_disks
++;
8203 * if we skip some disks the array will be assmebled degraded;
8204 * reset resync start to avoid a dirty-degraded
8205 * situation when performing the intial sync
8210 if (!(dev
->vol
.dirty
& RAIDVOL_DIRTY
)) {
8211 if ((!able_to_resync(level
, missing
) ||
8212 recovery_start
== 0))
8213 this->resync_start
= MaxSector
;
8219 info_d
= xcalloc(1, sizeof(*info_d
));
8220 info_d
->next
= this->devs
;
8221 this->devs
= info_d
;
8223 info_d
->disk
.number
= d
->index
;
8224 info_d
->disk
.major
= d
->major
;
8225 info_d
->disk
.minor
= d
->minor
;
8226 info_d
->disk
.raid_disk
= slot
;
8227 info_d
->recovery_start
= recovery_start
;
8229 if (slot
< map2
->num_members
)
8230 info_d
->disk
.state
= (1 << MD_DISK_ACTIVE
);
8232 this->array
.spare_disks
++;
8234 if (slot
< map
->num_members
)
8235 info_d
->disk
.state
= (1 << MD_DISK_ACTIVE
);
8237 this->array
.spare_disks
++;
8240 info_d
->events
= __le32_to_cpu(mpb
->generation_num
);
8241 info_d
->data_offset
= pba_of_lba0(map
);
8242 info_d
->component_size
= calc_component_size(map
, dev
);
8244 if (map
->raid_level
== 5) {
8245 info_d
->ppl_sector
= this->ppl_sector
;
8246 info_d
->ppl_size
= this->ppl_size
;
8247 if (this->consistency_policy
== CONSISTENCY_POLICY_PPL
&&
8248 recovery_start
== 0)
8249 this->resync_start
= 0;
8252 info_d
->bb
.supported
= 1;
8253 get_volume_badblocks(super
->bbm_log
, ord_to_idx(ord
),
8254 info_d
->data_offset
,
8255 info_d
->component_size
,
8258 /* now that the disk list is up-to-date fixup recovery_start */
8259 update_recovery_start(super
, dev
, this);
8260 this->array
.spare_disks
+= spare_disks
;
8262 /* check for reshape */
8263 if (this->reshape_active
== 1)
8264 recover_backup_imsm(st
, this);
8268 super
->current_vol
= current_vol
;
8272 static __u8
imsm_check_degraded(struct intel_super
*super
, struct imsm_dev
*dev
,
8273 int failed
, int look_in_map
)
8275 struct imsm_map
*map
;
8277 map
= get_imsm_map(dev
, look_in_map
);
8280 return map
->map_state
== IMSM_T_STATE_UNINITIALIZED
?
8281 IMSM_T_STATE_UNINITIALIZED
: IMSM_T_STATE_NORMAL
;
8283 switch (get_imsm_raid_level(map
)) {
8285 return IMSM_T_STATE_FAILED
;
8288 if (failed
< map
->num_members
)
8289 return IMSM_T_STATE_DEGRADED
;
8291 return IMSM_T_STATE_FAILED
;
8296 * check to see if any mirrors have failed, otherwise we
8297 * are degraded. Even numbered slots are mirrored on
8301 /* gcc -Os complains that this is unused */
8302 int insync
= insync
;
8304 for (i
= 0; i
< map
->num_members
; i
++) {
8305 __u32 ord
= get_imsm_ord_tbl_ent(dev
, i
, MAP_X
);
8306 int idx
= ord_to_idx(ord
);
8307 struct imsm_disk
*disk
;
8309 /* reset the potential in-sync count on even-numbered
8310 * slots. num_copies is always 2 for imsm raid10
8315 disk
= get_imsm_disk(super
, idx
);
8316 if (!disk
|| is_failed(disk
) || ord
& IMSM_ORD_REBUILD
)
8319 /* no in-sync disks left in this mirror the
8323 return IMSM_T_STATE_FAILED
;
8326 return IMSM_T_STATE_DEGRADED
;
8330 return IMSM_T_STATE_DEGRADED
;
8332 return IMSM_T_STATE_FAILED
;
8338 return map
->map_state
;
8341 static int imsm_count_failed(struct intel_super
*super
, struct imsm_dev
*dev
,
8346 struct imsm_disk
*disk
;
8347 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8348 struct imsm_map
*prev
= get_imsm_map(dev
, MAP_1
);
8349 struct imsm_map
*map_for_loop
;
8354 /* at the beginning of migration we set IMSM_ORD_REBUILD on
8355 * disks that are being rebuilt. New failures are recorded to
8356 * map[0]. So we look through all the disks we started with and
8357 * see if any failures are still present, or if any new ones
8361 if (prev
&& (map
->num_members
< prev
->num_members
))
8362 map_for_loop
= prev
;
8364 for (i
= 0; i
< map_for_loop
->num_members
; i
++) {
8366 /* when MAP_X is passed both maps failures are counted
8369 (look_in_map
== MAP_1
|| look_in_map
== MAP_X
) &&
8370 i
< prev
->num_members
) {
8371 ord
= __le32_to_cpu(prev
->disk_ord_tbl
[i
]);
8372 idx_1
= ord_to_idx(ord
);
8374 disk
= get_imsm_disk(super
, idx_1
);
8375 if (!disk
|| is_failed(disk
) || ord
& IMSM_ORD_REBUILD
)
8378 if ((look_in_map
== MAP_0
|| look_in_map
== MAP_X
) &&
8379 i
< map
->num_members
) {
8380 ord
= __le32_to_cpu(map
->disk_ord_tbl
[i
]);
8381 idx
= ord_to_idx(ord
);
8384 disk
= get_imsm_disk(super
, idx
);
8385 if (!disk
|| is_failed(disk
) ||
8386 ord
& IMSM_ORD_REBUILD
)
8395 static int imsm_open_new(struct supertype
*c
, struct active_array
*a
,
8398 struct intel_super
*super
= c
->sb
;
8399 struct imsm_super
*mpb
= super
->anchor
;
8400 struct imsm_update_prealloc_bb_mem u
;
8402 if (inst
>= mpb
->num_raid_devs
) {
8403 pr_err("subarry index %d, out of range\n", inst
);
8407 dprintf("imsm: open_new %d\n", inst
);
8408 a
->info
.container_member
= inst
;
8410 u
.type
= update_prealloc_badblocks_mem
;
8411 imsm_update_metadata_locally(c
, &u
, sizeof(u
));
8416 static int is_resyncing(struct imsm_dev
*dev
)
8418 struct imsm_map
*migr_map
;
8420 if (!dev
->vol
.migr_state
)
8423 if (migr_type(dev
) == MIGR_INIT
||
8424 migr_type(dev
) == MIGR_REPAIR
)
8427 if (migr_type(dev
) == MIGR_GEN_MIGR
)
8430 migr_map
= get_imsm_map(dev
, MAP_1
);
8432 if (migr_map
->map_state
== IMSM_T_STATE_NORMAL
&&
8433 dev
->vol
.migr_type
!= MIGR_GEN_MIGR
)
8439 /* return true if we recorded new information */
8440 static int mark_failure(struct intel_super
*super
,
8441 struct imsm_dev
*dev
, struct imsm_disk
*disk
, int idx
)
8445 struct imsm_map
*map
;
8446 char buf
[MAX_RAID_SERIAL_LEN
+3];
8447 unsigned int len
, shift
= 0;
8449 /* new failures are always set in map[0] */
8450 map
= get_imsm_map(dev
, MAP_0
);
8452 slot
= get_imsm_disk_slot(map
, idx
);
8456 ord
= __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
8457 if (is_failed(disk
) && (ord
& IMSM_ORD_REBUILD
))
8460 memcpy(buf
, disk
->serial
, MAX_RAID_SERIAL_LEN
);
8461 buf
[MAX_RAID_SERIAL_LEN
] = '\000';
8463 if ((len
= strlen(buf
)) >= MAX_RAID_SERIAL_LEN
)
8464 shift
= len
- MAX_RAID_SERIAL_LEN
+ 1;
8465 memcpy(disk
->serial
, &buf
[shift
], len
+ 1 - shift
);
8467 disk
->status
|= FAILED_DISK
;
8468 set_imsm_ord_tbl_ent(map
, slot
, idx
| IMSM_ORD_REBUILD
);
8469 /* mark failures in second map if second map exists and this disk
8471 * This is valid for migration, initialization and rebuild
8473 if (dev
->vol
.migr_state
) {
8474 struct imsm_map
*map2
= get_imsm_map(dev
, MAP_1
);
8475 int slot2
= get_imsm_disk_slot(map2
, idx
);
8477 if (slot2
< map2
->num_members
&& slot2
>= 0)
8478 set_imsm_ord_tbl_ent(map2
, slot2
,
8479 idx
| IMSM_ORD_REBUILD
);
8481 if (map
->failed_disk_num
== 0xff ||
8482 (!is_rebuilding(dev
) && map
->failed_disk_num
> slot
))
8483 map
->failed_disk_num
= slot
;
8485 clear_disk_badblocks(super
->bbm_log
, ord_to_idx(ord
));
8490 static void mark_missing(struct intel_super
*super
,
8491 struct imsm_dev
*dev
, struct imsm_disk
*disk
, int idx
)
8493 mark_failure(super
, dev
, disk
, idx
);
8495 if (disk
->scsi_id
== __cpu_to_le32(~(__u32
)0))
8498 disk
->scsi_id
= __cpu_to_le32(~(__u32
)0);
8499 memmove(&disk
->serial
[0], &disk
->serial
[1], MAX_RAID_SERIAL_LEN
- 1);
8502 static void handle_missing(struct intel_super
*super
, struct imsm_dev
*dev
)
8506 if (!super
->missing
)
8509 /* When orom adds replacement for missing disk it does
8510 * not remove entry of missing disk, but just updates map with
8511 * new added disk. So it is not enough just to test if there is
8512 * any missing disk, we have to look if there are any failed disks
8513 * in map to stop migration */
8515 dprintf("imsm: mark missing\n");
8516 /* end process for initialization and rebuild only
8518 if (is_gen_migration(dev
) == false) {
8519 int failed
= imsm_count_failed(super
, dev
, MAP_0
);
8523 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8524 struct imsm_map
*map1
;
8525 int i
, ord
, ord_map1
;
8528 for (i
= 0; i
< map
->num_members
; i
++) {
8529 ord
= get_imsm_ord_tbl_ent(dev
, i
, MAP_0
);
8530 if (!(ord
& IMSM_ORD_REBUILD
))
8533 map1
= get_imsm_map(dev
, MAP_1
);
8537 ord_map1
= __le32_to_cpu(map1
->disk_ord_tbl
[i
]);
8538 if (ord_map1
& IMSM_ORD_REBUILD
)
8543 map_state
= imsm_check_degraded(super
, dev
,
8545 end_migration(dev
, super
, map_state
);
8549 for (dl
= super
->missing
; dl
; dl
= dl
->next
)
8550 mark_missing(super
, dev
, &dl
->disk
, dl
->index
);
8551 super
->updates_pending
++;
8554 static unsigned long long imsm_set_array_size(struct imsm_dev
*dev
,
8557 unsigned long long array_blocks
;
8558 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8559 int used_disks
= imsm_num_data_members(map
);
8561 if (used_disks
== 0) {
8562 /* when problems occures
8563 * return current array_blocks value
8565 array_blocks
= imsm_dev_size(dev
);
8567 return array_blocks
;
8570 /* set array size in metadata
8573 /* OLCE size change is caused by added disks
8575 array_blocks
= per_dev_array_size(map
) * used_disks
;
8577 /* Online Volume Size Change
8578 * Using available free space
8580 array_blocks
= new_size
;
8582 array_blocks
= round_size_to_mb(array_blocks
, used_disks
);
8583 set_imsm_dev_size(dev
, array_blocks
);
8585 return array_blocks
;
8588 static void imsm_set_disk(struct active_array
*a
, int n
, int state
);
8590 static void imsm_progress_container_reshape(struct intel_super
*super
)
8592 /* if no device has a migr_state, but some device has a
8593 * different number of members than the previous device, start
8594 * changing the number of devices in this device to match
8597 struct imsm_super
*mpb
= super
->anchor
;
8598 int prev_disks
= -1;
8602 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
8603 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
8604 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8605 struct imsm_map
*map2
;
8606 int prev_num_members
;
8608 if (dev
->vol
.migr_state
)
8611 if (prev_disks
== -1)
8612 prev_disks
= map
->num_members
;
8613 if (prev_disks
== map
->num_members
)
8616 /* OK, this array needs to enter reshape mode.
8617 * i.e it needs a migr_state
8620 copy_map_size
= sizeof_imsm_map(map
);
8621 prev_num_members
= map
->num_members
;
8622 map
->num_members
= prev_disks
;
8623 dev
->vol
.migr_state
= 1;
8624 set_vol_curr_migr_unit(dev
, 0);
8625 set_migr_type(dev
, MIGR_GEN_MIGR
);
8626 for (i
= prev_num_members
;
8627 i
< map
->num_members
; i
++)
8628 set_imsm_ord_tbl_ent(map
, i
, i
);
8629 map2
= get_imsm_map(dev
, MAP_1
);
8630 /* Copy the current map */
8631 memcpy(map2
, map
, copy_map_size
);
8632 map2
->num_members
= prev_num_members
;
8634 imsm_set_array_size(dev
, -1);
8635 super
->clean_migration_record_by_mdmon
= 1;
8636 super
->updates_pending
++;
8640 /* Handle dirty -> clean transititions, resync and reshape. Degraded and rebuild
8641 * states are handled in imsm_set_disk() with one exception, when a
8642 * resync is stopped due to a new failure this routine will set the
8643 * 'degraded' state for the array.
8645 static int imsm_set_array_state(struct active_array
*a
, int consistent
)
8647 int inst
= a
->info
.container_member
;
8648 struct intel_super
*super
= a
->container
->sb
;
8649 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
8650 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8651 int failed
= imsm_count_failed(super
, dev
, MAP_0
);
8652 __u8 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
8653 __u32 blocks_per_unit
;
8655 if (dev
->vol
.migr_state
&&
8656 dev
->vol
.migr_type
== MIGR_GEN_MIGR
) {
8657 /* array state change is blocked due to reshape action
8659 * - abort the reshape (if last_checkpoint is 0 and action!= reshape)
8660 * - finish the reshape (if last_checkpoint is big and action != reshape)
8661 * - update vol_curr_migr_unit
8663 if (a
->curr_action
== reshape
) {
8664 /* still reshaping, maybe update vol_curr_migr_unit */
8665 goto mark_checkpoint
;
8667 if (a
->last_checkpoint
>= a
->info
.component_size
) {
8668 unsigned long long array_blocks
;
8672 used_disks
= imsm_num_data_members(map
);
8673 if (used_disks
> 0) {
8675 per_dev_array_size(map
) *
8678 round_size_to_mb(array_blocks
,
8680 a
->info
.custom_array_size
= array_blocks
;
8681 /* encourage manager to update array
8685 a
->check_reshape
= 1;
8687 /* finalize online capacity expansion/reshape */
8688 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
)
8690 mdi
->disk
.raid_disk
,
8693 imsm_progress_container_reshape(super
);
8698 /* before we activate this array handle any missing disks */
8699 if (consistent
== 2)
8700 handle_missing(super
, dev
);
8702 if (consistent
== 2 &&
8703 (!is_resync_complete(&a
->info
) ||
8704 map_state
!= IMSM_T_STATE_NORMAL
||
8705 dev
->vol
.migr_state
))
8708 if (is_resync_complete(&a
->info
)) {
8709 /* complete intialization / resync,
8710 * recovery and interrupted recovery is completed in
8713 if (is_resyncing(dev
)) {
8714 dprintf("imsm: mark resync done\n");
8715 end_migration(dev
, super
, map_state
);
8716 super
->updates_pending
++;
8717 a
->last_checkpoint
= 0;
8719 } else if ((!is_resyncing(dev
) && !failed
) &&
8720 (imsm_reshape_blocks_arrays_changes(super
) == 0)) {
8721 /* mark the start of the init process if nothing is failed */
8722 dprintf("imsm: mark resync start\n");
8723 if (map
->map_state
== IMSM_T_STATE_UNINITIALIZED
)
8724 migrate(dev
, super
, IMSM_T_STATE_NORMAL
, MIGR_INIT
);
8726 migrate(dev
, super
, IMSM_T_STATE_NORMAL
, MIGR_REPAIR
);
8727 super
->updates_pending
++;
8730 if (a
->prev_action
== idle
)
8731 goto skip_mark_checkpoint
;
8734 /* skip checkpointing for general migration,
8735 * it is controlled in mdadm
8737 if (is_gen_migration(dev
))
8738 goto skip_mark_checkpoint
;
8740 /* check if we can update vol_curr_migr_unit from resync_start,
8743 blocks_per_unit
= blocks_per_migr_unit(super
, dev
);
8744 if (blocks_per_unit
) {
8745 set_vol_curr_migr_unit(dev
,
8746 a
->last_checkpoint
/ blocks_per_unit
);
8747 dprintf("imsm: mark checkpoint (%llu)\n",
8748 vol_curr_migr_unit(dev
));
8749 super
->updates_pending
++;
8752 skip_mark_checkpoint
:
8753 /* mark dirty / clean */
8754 if (((dev
->vol
.dirty
& RAIDVOL_DIRTY
) && consistent
) ||
8755 (!(dev
->vol
.dirty
& RAIDVOL_DIRTY
) && !consistent
)) {
8756 dprintf("imsm: mark '%s'\n", consistent
? "clean" : "dirty");
8758 dev
->vol
.dirty
= RAIDVOL_CLEAN
;
8760 dev
->vol
.dirty
= RAIDVOL_DIRTY
;
8761 if (dev
->rwh_policy
== RWH_DISTRIBUTED
||
8762 dev
->rwh_policy
== RWH_MULTIPLE_DISTRIBUTED
)
8763 dev
->vol
.dirty
|= RAIDVOL_DSRECORD_VALID
;
8765 super
->updates_pending
++;
8771 static int imsm_disk_slot_to_ord(struct active_array
*a
, int slot
)
8773 int inst
= a
->info
.container_member
;
8774 struct intel_super
*super
= a
->container
->sb
;
8775 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
8776 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8778 if (slot
> map
->num_members
) {
8779 pr_err("imsm: imsm_disk_slot_to_ord %d out of range 0..%d\n",
8780 slot
, map
->num_members
- 1);
8787 return get_imsm_ord_tbl_ent(dev
, slot
, MAP_0
);
8790 static void imsm_set_disk(struct active_array
*a
, int n
, int state
)
8792 int inst
= a
->info
.container_member
;
8793 struct intel_super
*super
= a
->container
->sb
;
8794 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
8795 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8796 struct imsm_disk
*disk
;
8798 int recovery_not_finished
= 0;
8802 int rebuild_done
= 0;
8805 ord
= get_imsm_ord_tbl_ent(dev
, n
, MAP_X
);
8809 dprintf("imsm: set_disk %d:%x\n", n
, state
);
8810 disk
= get_imsm_disk(super
, ord_to_idx(ord
));
8812 /* check for new failures */
8813 if (disk
&& (state
& DS_FAULTY
)) {
8814 if (mark_failure(super
, dev
, disk
, ord_to_idx(ord
)))
8815 super
->updates_pending
++;
8818 /* check if in_sync */
8819 if (state
& DS_INSYNC
&& ord
& IMSM_ORD_REBUILD
&& is_rebuilding(dev
)) {
8820 struct imsm_map
*migr_map
= get_imsm_map(dev
, MAP_1
);
8822 set_imsm_ord_tbl_ent(migr_map
, n
, ord_to_idx(ord
));
8824 super
->updates_pending
++;
8827 failed
= imsm_count_failed(super
, dev
, MAP_0
);
8828 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
8830 /* check if recovery complete, newly degraded, or failed */
8831 dprintf("imsm: Detected transition to state ");
8832 switch (map_state
) {
8833 case IMSM_T_STATE_NORMAL
: /* transition to normal state */
8834 dprintf("normal: ");
8835 if (is_rebuilding(dev
)) {
8836 dprintf_cont("while rebuilding");
8837 /* check if recovery is really finished */
8838 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
)
8839 if (mdi
->recovery_start
!= MaxSector
) {
8840 recovery_not_finished
= 1;
8843 if (recovery_not_finished
) {
8845 dprintf("Rebuild has not finished yet, state not changed");
8846 if (a
->last_checkpoint
< mdi
->recovery_start
) {
8847 a
->last_checkpoint
= mdi
->recovery_start
;
8848 super
->updates_pending
++;
8852 end_migration(dev
, super
, map_state
);
8853 map
->failed_disk_num
= ~0;
8854 super
->updates_pending
++;
8855 a
->last_checkpoint
= 0;
8858 if (is_gen_migration(dev
)) {
8859 dprintf_cont("while general migration");
8860 if (a
->last_checkpoint
>= a
->info
.component_size
)
8861 end_migration(dev
, super
, map_state
);
8863 map
->map_state
= map_state
;
8864 map
->failed_disk_num
= ~0;
8865 super
->updates_pending
++;
8869 case IMSM_T_STATE_DEGRADED
: /* transition to degraded state */
8870 dprintf_cont("degraded: ");
8871 if (map
->map_state
!= map_state
&& !dev
->vol
.migr_state
) {
8872 dprintf_cont("mark degraded");
8873 map
->map_state
= map_state
;
8874 super
->updates_pending
++;
8875 a
->last_checkpoint
= 0;
8878 if (is_rebuilding(dev
)) {
8879 dprintf_cont("while rebuilding ");
8880 if (state
& DS_FAULTY
) {
8881 dprintf_cont("removing failed drive ");
8882 if (n
== map
->failed_disk_num
) {
8883 dprintf_cont("end migration");
8884 end_migration(dev
, super
, map_state
);
8885 a
->last_checkpoint
= 0;
8887 dprintf_cont("fail detected during rebuild, changing map state");
8888 map
->map_state
= map_state
;
8890 super
->updates_pending
++;
8896 /* check if recovery is really finished */
8897 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
)
8898 if (mdi
->recovery_start
!= MaxSector
) {
8899 recovery_not_finished
= 1;
8902 if (recovery_not_finished
) {
8904 dprintf_cont("Rebuild has not finished yet");
8905 if (a
->last_checkpoint
< mdi
->recovery_start
) {
8906 a
->last_checkpoint
=
8907 mdi
->recovery_start
;
8908 super
->updates_pending
++;
8913 dprintf_cont(" Rebuild done, still degraded");
8914 end_migration(dev
, super
, map_state
);
8915 a
->last_checkpoint
= 0;
8916 super
->updates_pending
++;
8918 for (i
= 0; i
< map
->num_members
; i
++) {
8919 int idx
= get_imsm_ord_tbl_ent(dev
, i
, MAP_0
);
8921 if (idx
& IMSM_ORD_REBUILD
)
8922 map
->failed_disk_num
= i
;
8924 super
->updates_pending
++;
8927 if (is_gen_migration(dev
)) {
8928 dprintf_cont("while general migration");
8929 if (a
->last_checkpoint
>= a
->info
.component_size
)
8930 end_migration(dev
, super
, map_state
);
8932 map
->map_state
= map_state
;
8933 manage_second_map(super
, dev
);
8935 super
->updates_pending
++;
8938 if (is_initializing(dev
)) {
8939 dprintf_cont("while initialization.");
8940 map
->map_state
= map_state
;
8941 super
->updates_pending
++;
8945 case IMSM_T_STATE_FAILED
: /* transition to failed state */
8946 dprintf_cont("failed: ");
8947 if (is_gen_migration(dev
)) {
8948 dprintf_cont("while general migration");
8949 map
->map_state
= map_state
;
8950 super
->updates_pending
++;
8953 if (map
->map_state
!= map_state
) {
8954 dprintf_cont("mark failed");
8955 end_migration(dev
, super
, map_state
);
8956 super
->updates_pending
++;
8957 a
->last_checkpoint
= 0;
8962 dprintf_cont("state %i\n", map_state
);
8967 static int store_imsm_mpb(int fd
, struct imsm_super
*mpb
)
8970 __u32 mpb_size
= __le32_to_cpu(mpb
->mpb_size
);
8971 unsigned long long dsize
;
8972 unsigned long long sectors
;
8973 unsigned int sector_size
;
8975 if (!get_dev_sector_size(fd
, NULL
, §or_size
))
8977 get_dev_size(fd
, NULL
, &dsize
);
8979 if (mpb_size
> sector_size
) {
8980 /* -1 to account for anchor */
8981 sectors
= mpb_sectors(mpb
, sector_size
) - 1;
8983 /* write the extended mpb to the sectors preceeding the anchor */
8984 if (lseek64(fd
, dsize
- (sector_size
* (2 + sectors
)),
8988 if ((unsigned long long)write(fd
, buf
+ sector_size
,
8989 sector_size
* sectors
) != sector_size
* sectors
)
8993 /* first block is stored on second to last sector of the disk */
8994 if (lseek64(fd
, dsize
- (sector_size
* 2), SEEK_SET
) < 0)
8997 if ((unsigned int)write(fd
, buf
, sector_size
) != sector_size
)
9003 static void imsm_sync_metadata(struct supertype
*container
)
9005 struct intel_super
*super
= container
->sb
;
9007 dprintf("sync metadata: %d\n", super
->updates_pending
);
9008 if (!super
->updates_pending
)
9011 write_super_imsm(container
, 0);
9013 super
->updates_pending
= 0;
9016 static struct dl
*imsm_readd(struct intel_super
*super
, int idx
, struct active_array
*a
)
9018 struct imsm_dev
*dev
= get_imsm_dev(super
, a
->info
.container_member
);
9019 int i
= get_imsm_disk_idx(dev
, idx
, MAP_X
);
9022 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
9026 if (dl
&& is_failed(&dl
->disk
))
9030 dprintf("found %x:%x\n", dl
->major
, dl
->minor
);
9035 static struct dl
*imsm_add_spare(struct intel_super
*super
, int slot
,
9036 struct active_array
*a
, int activate_new
,
9037 struct mdinfo
*additional_test_list
)
9039 struct imsm_dev
*dev
= get_imsm_dev(super
, a
->info
.container_member
);
9040 int idx
= get_imsm_disk_idx(dev
, slot
, MAP_X
);
9041 struct imsm_super
*mpb
= super
->anchor
;
9042 struct imsm_map
*map
;
9043 unsigned long long pos
;
9048 __u32 array_start
= 0;
9049 __u32 array_end
= 0;
9051 struct mdinfo
*test_list
;
9053 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
9054 /* If in this array, skip */
9055 for (d
= a
->info
.devs
; d
; d
= d
->next
)
9056 if (is_fd_valid(d
->state_fd
) &&
9057 d
->disk
.major
== dl
->major
&&
9058 d
->disk
.minor
== dl
->minor
) {
9059 dprintf("%x:%x already in array\n",
9060 dl
->major
, dl
->minor
);
9065 test_list
= additional_test_list
;
9067 if (test_list
->disk
.major
== dl
->major
&&
9068 test_list
->disk
.minor
== dl
->minor
) {
9069 dprintf("%x:%x already in additional test list\n",
9070 dl
->major
, dl
->minor
);
9073 test_list
= test_list
->next
;
9078 /* skip in use or failed drives */
9079 if (is_failed(&dl
->disk
) || idx
== dl
->index
||
9081 dprintf("%x:%x status (failed: %d index: %d)\n",
9082 dl
->major
, dl
->minor
, is_failed(&dl
->disk
), idx
);
9086 /* skip pure spares when we are looking for partially
9087 * assimilated drives
9089 if (dl
->index
== -1 && !activate_new
)
9092 if (!drive_validate_sector_size(super
, dl
))
9095 /* Does this unused device have the requisite free space?
9096 * It needs to be able to cover all member volumes
9098 ex
= get_extents(super
, dl
, 1);
9100 dprintf("cannot get extents\n");
9103 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
9104 dev
= get_imsm_dev(super
, i
);
9105 map
= get_imsm_map(dev
, MAP_0
);
9107 /* check if this disk is already a member of
9110 if (get_imsm_disk_slot(map
, dl
->index
) >= 0)
9116 array_start
= pba_of_lba0(map
);
9117 array_end
= array_start
+
9118 per_dev_array_size(map
) - 1;
9121 /* check that we can start at pba_of_lba0 with
9122 * num_data_stripes*blocks_per_stripe of space
9124 if (array_start
>= pos
&& array_end
< ex
[j
].start
) {
9128 pos
= ex
[j
].start
+ ex
[j
].size
;
9130 } while (ex
[j
-1].size
);
9137 if (i
< mpb
->num_raid_devs
) {
9138 dprintf("%x:%x does not have %u to %u available\n",
9139 dl
->major
, dl
->minor
, array_start
, array_end
);
9149 static int imsm_rebuild_allowed(struct supertype
*cont
, int dev_idx
, int failed
)
9151 struct imsm_dev
*dev2
;
9152 struct imsm_map
*map
;
9158 dev2
= get_imsm_dev(cont
->sb
, dev_idx
);
9160 state
= imsm_check_degraded(cont
->sb
, dev2
, failed
, MAP_0
);
9161 if (state
== IMSM_T_STATE_FAILED
) {
9162 map
= get_imsm_map(dev2
, MAP_0
);
9163 for (slot
= 0; slot
< map
->num_members
; slot
++) {
9165 * Check if failed disks are deleted from intel
9166 * disk list or are marked to be deleted
9168 idx
= get_imsm_disk_idx(dev2
, slot
, MAP_X
);
9169 idisk
= get_imsm_dl_disk(cont
->sb
, idx
);
9171 * Do not rebuild the array if failed disks
9172 * from failed sub-array are not removed from
9176 is_failed(&idisk
->disk
) &&
9177 (idisk
->action
!= DISK_REMOVE
))
9184 static struct mdinfo
*imsm_activate_spare(struct active_array
*a
,
9185 struct metadata_update
**updates
)
9188 * Find a device with unused free space and use it to replace a
9189 * failed/vacant region in an array. We replace failed regions one a
9190 * array at a time. The result is that a new spare disk will be added
9191 * to the first failed array and after the monitor has finished
9192 * propagating failures the remainder will be consumed.
9194 * FIXME add a capability for mdmon to request spares from another
9198 struct intel_super
*super
= a
->container
->sb
;
9199 int inst
= a
->info
.container_member
;
9200 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
9201 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
9202 int failed
= a
->info
.array
.raid_disks
;
9203 struct mdinfo
*rv
= NULL
;
9206 struct metadata_update
*mu
;
9208 struct imsm_update_activate_spare
*u
;
9213 for (d
= a
->info
.devs
; d
; d
= d
->next
) {
9214 if (!is_fd_valid(d
->state_fd
))
9217 if (d
->curr_state
& DS_FAULTY
)
9218 /* wait for Removal to happen */
9224 dprintf("imsm: activate spare: inst=%d failed=%d (%d) level=%d\n",
9225 inst
, failed
, a
->info
.array
.raid_disks
, a
->info
.array
.level
);
9227 if (imsm_reshape_blocks_arrays_changes(super
))
9230 /* Cannot activate another spare if rebuild is in progress already
9232 if (is_rebuilding(dev
)) {
9233 dprintf("imsm: No spare activation allowed. Rebuild in progress already.\n");
9237 if (a
->info
.array
.level
== 4)
9238 /* No repair for takeovered array
9239 * imsm doesn't support raid4
9243 if (imsm_check_degraded(super
, dev
, failed
, MAP_0
) !=
9244 IMSM_T_STATE_DEGRADED
)
9247 if (get_imsm_map(dev
, MAP_0
)->map_state
== IMSM_T_STATE_UNINITIALIZED
) {
9248 dprintf("imsm: No spare activation allowed. Volume is not initialized.\n");
9253 * If there are any failed disks check state of the other volume.
9254 * Block rebuild if the another one is failed until failed disks
9255 * are removed from container.
9258 dprintf("found failed disks in %.*s, check if there anotherfailed sub-array.\n",
9259 MAX_RAID_SERIAL_LEN
, dev
->volume
);
9260 /* check if states of the other volumes allow for rebuild */
9261 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
9263 allowed
= imsm_rebuild_allowed(a
->container
,
9271 /* For each slot, if it is not working, find a spare */
9272 for (i
= 0; i
< a
->info
.array
.raid_disks
; i
++) {
9273 for (d
= a
->info
.devs
; d
; d
= d
->next
)
9274 if (d
->disk
.raid_disk
== i
)
9276 dprintf("found %d: %p %x\n", i
, d
, d
?d
->curr_state
:0);
9277 if (d
&& is_fd_valid(d
->state_fd
))
9281 * OK, this device needs recovery. Try to re-add the
9282 * previous occupant of this slot, if this fails see if
9283 * we can continue the assimilation of a spare that was
9284 * partially assimilated, finally try to activate a new
9287 dl
= imsm_readd(super
, i
, a
);
9289 dl
= imsm_add_spare(super
, i
, a
, 0, rv
);
9291 dl
= imsm_add_spare(super
, i
, a
, 1, rv
);
9295 /* found a usable disk with enough space */
9296 di
= xcalloc(1, sizeof(*di
));
9298 /* dl->index will be -1 in the case we are activating a
9299 * pristine spare. imsm_process_update() will create a
9300 * new index in this case. Once a disk is found to be
9301 * failed in all member arrays it is kicked from the
9304 di
->disk
.number
= dl
->index
;
9306 /* (ab)use di->devs to store a pointer to the device
9309 di
->devs
= (struct mdinfo
*) dl
;
9311 di
->disk
.raid_disk
= i
;
9312 di
->disk
.major
= dl
->major
;
9313 di
->disk
.minor
= dl
->minor
;
9315 di
->recovery_start
= 0;
9316 di
->data_offset
= pba_of_lba0(map
);
9317 di
->component_size
= a
->info
.component_size
;
9318 di
->container_member
= inst
;
9319 di
->bb
.supported
= 1;
9320 if (a
->info
.consistency_policy
== CONSISTENCY_POLICY_PPL
) {
9321 di
->ppl_sector
= get_ppl_sector(super
, inst
);
9322 di
->ppl_size
= MULTIPLE_PPL_AREA_SIZE_IMSM
>> 9;
9324 super
->random
= random32();
9328 dprintf("%x:%x to be %d at %llu\n", dl
->major
, dl
->minor
,
9329 i
, di
->data_offset
);
9333 /* No spares found */
9335 /* Now 'rv' has a list of devices to return.
9336 * Create a metadata_update record to update the
9337 * disk_ord_tbl for the array
9339 mu
= xmalloc(sizeof(*mu
));
9340 mu
->buf
= xcalloc(num_spares
,
9341 sizeof(struct imsm_update_activate_spare
));
9343 mu
->space_list
= NULL
;
9344 mu
->len
= sizeof(struct imsm_update_activate_spare
) * num_spares
;
9345 mu
->next
= *updates
;
9346 u
= (struct imsm_update_activate_spare
*) mu
->buf
;
9348 for (di
= rv
; di
; di
= di
->next
) {
9349 u
->type
= update_activate_spare
;
9350 u
->dl
= (struct dl
*) di
->devs
;
9352 u
->slot
= di
->disk
.raid_disk
;
9363 static int disks_overlap(struct intel_super
*super
, int idx
, struct imsm_update_create_array
*u
)
9365 struct imsm_dev
*dev
= get_imsm_dev(super
, idx
);
9366 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
9367 struct imsm_map
*new_map
= get_imsm_map(&u
->dev
, MAP_0
);
9368 struct disk_info
*inf
= get_disk_info(u
);
9369 struct imsm_disk
*disk
;
9373 for (i
= 0; i
< map
->num_members
; i
++) {
9374 disk
= get_imsm_disk(super
, get_imsm_disk_idx(dev
, i
, MAP_X
));
9375 for (j
= 0; j
< new_map
->num_members
; j
++)
9376 if (serialcmp(disk
->serial
, inf
[j
].serial
) == 0)
9383 static struct dl
*get_disk_super(struct intel_super
*super
, int major
, int minor
)
9387 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
9388 if (dl
->major
== major
&& dl
->minor
== minor
)
9393 static int remove_disk_super(struct intel_super
*super
, int major
, int minor
)
9399 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
9400 if (dl
->major
== major
&& dl
->minor
== minor
) {
9403 prev
->next
= dl
->next
;
9405 super
->disks
= dl
->next
;
9407 __free_imsm_disk(dl
, 1);
9408 dprintf("removed %x:%x\n", major
, minor
);
9416 static void imsm_delete(struct intel_super
*super
, struct dl
**dlp
, unsigned index
);
9418 static int add_remove_disk_update(struct intel_super
*super
)
9420 int check_degraded
= 0;
9423 /* add/remove some spares to/from the metadata/contrainer */
9424 while (super
->disk_mgmt_list
) {
9425 struct dl
*disk_cfg
;
9427 disk_cfg
= super
->disk_mgmt_list
;
9428 super
->disk_mgmt_list
= disk_cfg
->next
;
9429 disk_cfg
->next
= NULL
;
9431 if (disk_cfg
->action
== DISK_ADD
) {
9432 disk_cfg
->next
= super
->disks
;
9433 super
->disks
= disk_cfg
;
9435 dprintf("added %x:%x\n",
9436 disk_cfg
->major
, disk_cfg
->minor
);
9437 } else if (disk_cfg
->action
== DISK_REMOVE
) {
9438 dprintf("Disk remove action processed: %x.%x\n",
9439 disk_cfg
->major
, disk_cfg
->minor
);
9440 disk
= get_disk_super(super
,
9444 /* store action status */
9445 disk
->action
= DISK_REMOVE
;
9446 /* remove spare disks only */
9447 if (disk
->index
== -1) {
9448 remove_disk_super(super
,
9452 disk_cfg
->fd
= disk
->fd
;
9456 /* release allocate disk structure */
9457 __free_imsm_disk(disk_cfg
, 1);
9460 return check_degraded
;
9463 static int apply_reshape_migration_update(struct imsm_update_reshape_migration
*u
,
9464 struct intel_super
*super
,
9467 struct intel_dev
*id
;
9468 void **tofree
= NULL
;
9471 dprintf("(enter)\n");
9472 if (u
->subdev
< 0 || u
->subdev
> 1) {
9473 dprintf("imsm: Error: Wrong subdev: %i\n", u
->subdev
);
9476 if (space_list
== NULL
|| *space_list
== NULL
) {
9477 dprintf("imsm: Error: Memory is not allocated\n");
9481 for (id
= super
->devlist
; id
; id
= id
->next
) {
9482 if (id
->index
== (unsigned)u
->subdev
) {
9483 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->subdev
);
9484 struct imsm_map
*map
;
9485 struct imsm_dev
*new_dev
=
9486 (struct imsm_dev
*)*space_list
;
9487 struct imsm_map
*migr_map
= get_imsm_map(dev
, MAP_1
);
9489 struct dl
*new_disk
;
9491 if (new_dev
== NULL
)
9493 *space_list
= **space_list
;
9494 memcpy(new_dev
, dev
, sizeof_imsm_dev(dev
, 0));
9495 map
= get_imsm_map(new_dev
, MAP_0
);
9497 dprintf("imsm: Error: migration in progress");
9501 to_state
= map
->map_state
;
9502 if ((u
->new_level
== 5) && (map
->raid_level
== 0)) {
9504 /* this should not happen */
9505 if (u
->new_disks
[0] < 0) {
9506 map
->failed_disk_num
=
9507 map
->num_members
- 1;
9508 to_state
= IMSM_T_STATE_DEGRADED
;
9510 to_state
= IMSM_T_STATE_NORMAL
;
9512 migrate(new_dev
, super
, to_state
, MIGR_GEN_MIGR
);
9513 if (u
->new_level
> -1)
9514 map
->raid_level
= u
->new_level
;
9515 migr_map
= get_imsm_map(new_dev
, MAP_1
);
9516 if ((u
->new_level
== 5) &&
9517 (migr_map
->raid_level
== 0)) {
9518 int ord
= map
->num_members
- 1;
9519 migr_map
->num_members
--;
9520 if (u
->new_disks
[0] < 0)
9521 ord
|= IMSM_ORD_REBUILD
;
9522 set_imsm_ord_tbl_ent(map
,
9523 map
->num_members
- 1,
9527 tofree
= (void **)dev
;
9529 /* update chunk size
9531 if (u
->new_chunksize
> 0) {
9532 struct imsm_map
*dest_map
=
9533 get_imsm_map(dev
, MAP_0
);
9535 imsm_num_data_members(dest_map
);
9537 if (used_disks
== 0)
9540 map
->blocks_per_strip
=
9541 __cpu_to_le16(u
->new_chunksize
* 2);
9542 update_num_data_stripes(map
, imsm_dev_size(dev
));
9545 /* ensure blocks_per_member has valid value
9547 set_blocks_per_member(map
,
9548 per_dev_array_size(map
) +
9549 NUM_BLOCKS_DIRTY_STRIPE_REGION
);
9553 if (u
->new_level
!= 5 || migr_map
->raid_level
!= 0 ||
9554 migr_map
->raid_level
== map
->raid_level
)
9557 if (u
->new_disks
[0] >= 0) {
9560 new_disk
= get_disk_super(super
,
9561 major(u
->new_disks
[0]),
9562 minor(u
->new_disks
[0]));
9563 dprintf("imsm: new disk for reshape is: %i:%i (%p, index = %i)\n",
9564 major(u
->new_disks
[0]),
9565 minor(u
->new_disks
[0]),
9566 new_disk
, new_disk
->index
);
9567 if (new_disk
== NULL
)
9568 goto error_disk_add
;
9570 new_disk
->index
= map
->num_members
- 1;
9571 /* slot to fill in autolayout
9573 new_disk
->raiddisk
= new_disk
->index
;
9574 new_disk
->disk
.status
|= CONFIGURED_DISK
;
9575 new_disk
->disk
.status
&= ~SPARE_DISK
;
9577 goto error_disk_add
;
9580 *tofree
= *space_list
;
9581 /* calculate new size
9583 imsm_set_array_size(new_dev
, -1);
9590 *space_list
= tofree
;
9594 dprintf("Error: imsm: Cannot find disk.\n");
9598 static int apply_size_change_update(struct imsm_update_size_change
*u
,
9599 struct intel_super
*super
)
9601 struct intel_dev
*id
;
9604 dprintf("(enter)\n");
9605 if (u
->subdev
< 0 || u
->subdev
> 1) {
9606 dprintf("imsm: Error: Wrong subdev: %i\n", u
->subdev
);
9610 for (id
= super
->devlist
; id
; id
= id
->next
) {
9611 if (id
->index
== (unsigned)u
->subdev
) {
9612 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->subdev
);
9613 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
9614 int used_disks
= imsm_num_data_members(map
);
9615 unsigned long long blocks_per_member
;
9616 unsigned long long new_size_per_disk
;
9618 if (used_disks
== 0)
9621 /* calculate new size
9623 new_size_per_disk
= u
->new_size
/ used_disks
;
9624 blocks_per_member
= new_size_per_disk
+
9625 NUM_BLOCKS_DIRTY_STRIPE_REGION
;
9627 imsm_set_array_size(dev
, u
->new_size
);
9628 set_blocks_per_member(map
, blocks_per_member
);
9629 update_num_data_stripes(map
, u
->new_size
);
9638 static int prepare_spare_to_activate(struct supertype
*st
,
9639 struct imsm_update_activate_spare
*u
)
9641 struct intel_super
*super
= st
->sb
;
9642 int prev_current_vol
= super
->current_vol
;
9643 struct active_array
*a
;
9646 for (a
= st
->arrays
; a
; a
= a
->next
)
9648 * Additional initialization (adding bitmap header, filling
9649 * the bitmap area with '1's to force initial rebuild for a whole
9650 * data-area) is required when adding the spare to the volume
9651 * with write-intent bitmap.
9653 if (a
->info
.container_member
== u
->array
&&
9654 a
->info
.consistency_policy
== CONSISTENCY_POLICY_BITMAP
) {
9657 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
9663 super
->current_vol
= u
->array
;
9664 if (st
->ss
->write_bitmap(st
, dl
->fd
, NoUpdate
))
9666 super
->current_vol
= prev_current_vol
;
9671 static int apply_update_activate_spare(struct imsm_update_activate_spare
*u
,
9672 struct intel_super
*super
,
9673 struct active_array
*active_array
)
9675 struct imsm_super
*mpb
= super
->anchor
;
9676 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->array
);
9677 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
9678 struct imsm_map
*migr_map
;
9679 struct active_array
*a
;
9680 struct imsm_disk
*disk
;
9687 int second_map_created
= 0;
9689 for (; u
; u
= u
->next
) {
9690 victim
= get_imsm_disk_idx(dev
, u
->slot
, MAP_X
);
9695 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
9700 pr_err("error: imsm_activate_spare passed an unknown disk (index: %d)\n",
9705 /* count failures (excluding rebuilds and the victim)
9706 * to determine map[0] state
9709 for (i
= 0; i
< map
->num_members
; i
++) {
9712 disk
= get_imsm_disk(super
,
9713 get_imsm_disk_idx(dev
, i
, MAP_X
));
9714 if (!disk
|| is_failed(disk
))
9718 /* adding a pristine spare, assign a new index */
9719 if (dl
->index
< 0) {
9720 dl
->index
= super
->anchor
->num_disks
;
9721 super
->anchor
->num_disks
++;
9724 disk
->status
|= CONFIGURED_DISK
;
9725 disk
->status
&= ~SPARE_DISK
;
9728 to_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
9729 if (!second_map_created
) {
9730 second_map_created
= 1;
9731 map
->map_state
= IMSM_T_STATE_DEGRADED
;
9732 migrate(dev
, super
, to_state
, MIGR_REBUILD
);
9734 map
->map_state
= to_state
;
9735 migr_map
= get_imsm_map(dev
, MAP_1
);
9736 set_imsm_ord_tbl_ent(map
, u
->slot
, dl
->index
);
9737 set_imsm_ord_tbl_ent(migr_map
, u
->slot
,
9738 dl
->index
| IMSM_ORD_REBUILD
);
9740 /* update the family_num to mark a new container
9741 * generation, being careful to record the existing
9742 * family_num in orig_family_num to clean up after
9743 * earlier mdadm versions that neglected to set it.
9745 if (mpb
->orig_family_num
== 0)
9746 mpb
->orig_family_num
= mpb
->family_num
;
9747 mpb
->family_num
+= super
->random
;
9749 /* count arrays using the victim in the metadata */
9751 for (a
= active_array
; a
; a
= a
->next
) {
9752 int dev_idx
= a
->info
.container_member
;
9754 if (get_disk_slot_in_dev(super
, dev_idx
, victim
) >= 0)
9758 /* delete the victim if it is no longer being
9764 /* We know that 'manager' isn't touching anything,
9765 * so it is safe to delete
9767 for (dlp
= &super
->disks
; *dlp
; dlp
= &(*dlp
)->next
)
9768 if ((*dlp
)->index
== victim
)
9771 /* victim may be on the missing list */
9773 for (dlp
= &super
->missing
; *dlp
;
9774 dlp
= &(*dlp
)->next
)
9775 if ((*dlp
)->index
== victim
)
9777 imsm_delete(super
, dlp
, victim
);
9784 static int apply_reshape_container_disks_update(struct imsm_update_reshape
*u
,
9785 struct intel_super
*super
,
9788 struct dl
*new_disk
;
9789 struct intel_dev
*id
;
9791 int delta_disks
= u
->new_raid_disks
- u
->old_raid_disks
;
9792 int disk_count
= u
->old_raid_disks
;
9793 void **tofree
= NULL
;
9794 int devices_to_reshape
= 1;
9795 struct imsm_super
*mpb
= super
->anchor
;
9797 unsigned int dev_id
;
9799 dprintf("(enter)\n");
9801 /* enable spares to use in array */
9802 for (i
= 0; i
< delta_disks
; i
++) {
9803 new_disk
= get_disk_super(super
,
9804 major(u
->new_disks
[i
]),
9805 minor(u
->new_disks
[i
]));
9806 dprintf("imsm: new disk for reshape is: %i:%i (%p, index = %i)\n",
9807 major(u
->new_disks
[i
]), minor(u
->new_disks
[i
]),
9808 new_disk
, new_disk
->index
);
9809 if (new_disk
== NULL
||
9810 (new_disk
->index
>= 0 &&
9811 new_disk
->index
< u
->old_raid_disks
))
9812 goto update_reshape_exit
;
9813 new_disk
->index
= disk_count
++;
9814 /* slot to fill in autolayout
9816 new_disk
->raiddisk
= new_disk
->index
;
9817 new_disk
->disk
.status
|=
9819 new_disk
->disk
.status
&= ~SPARE_DISK
;
9822 dprintf("imsm: volume set mpb->num_raid_devs = %i\n",
9823 mpb
->num_raid_devs
);
9824 /* manage changes in volume
9826 for (dev_id
= 0; dev_id
< mpb
->num_raid_devs
; dev_id
++) {
9827 void **sp
= *space_list
;
9828 struct imsm_dev
*newdev
;
9829 struct imsm_map
*newmap
, *oldmap
;
9831 for (id
= super
->devlist
; id
; id
= id
->next
) {
9832 if (id
->index
== dev_id
)
9841 /* Copy the dev, but not (all of) the map */
9842 memcpy(newdev
, id
->dev
, sizeof(*newdev
));
9843 oldmap
= get_imsm_map(id
->dev
, MAP_0
);
9844 newmap
= get_imsm_map(newdev
, MAP_0
);
9845 /* Copy the current map */
9846 memcpy(newmap
, oldmap
, sizeof_imsm_map(oldmap
));
9847 /* update one device only
9849 if (devices_to_reshape
) {
9850 dprintf("imsm: modifying subdev: %i\n",
9852 devices_to_reshape
--;
9853 newdev
->vol
.migr_state
= 1;
9854 set_vol_curr_migr_unit(newdev
, 0);
9855 set_migr_type(newdev
, MIGR_GEN_MIGR
);
9856 newmap
->num_members
= u
->new_raid_disks
;
9857 for (i
= 0; i
< delta_disks
; i
++) {
9858 set_imsm_ord_tbl_ent(newmap
,
9859 u
->old_raid_disks
+ i
,
9860 u
->old_raid_disks
+ i
);
9862 /* New map is correct, now need to save old map
9864 newmap
= get_imsm_map(newdev
, MAP_1
);
9865 memcpy(newmap
, oldmap
, sizeof_imsm_map(oldmap
));
9867 imsm_set_array_size(newdev
, -1);
9870 sp
= (void **)id
->dev
;
9875 /* Clear migration record */
9876 memset(super
->migr_rec
, 0, sizeof(struct migr_record
));
9879 *space_list
= tofree
;
9882 update_reshape_exit
:
9887 static int apply_takeover_update(struct imsm_update_takeover
*u
,
9888 struct intel_super
*super
,
9891 struct imsm_dev
*dev
= NULL
;
9892 struct intel_dev
*dv
;
9893 struct imsm_dev
*dev_new
;
9894 struct imsm_map
*map
;
9898 for (dv
= super
->devlist
; dv
; dv
= dv
->next
)
9899 if (dv
->index
== (unsigned int)u
->subarray
) {
9907 map
= get_imsm_map(dev
, MAP_0
);
9909 if (u
->direction
== R10_TO_R0
) {
9910 /* Number of failed disks must be half of initial disk number */
9911 if (imsm_count_failed(super
, dev
, MAP_0
) !=
9912 (map
->num_members
/ 2))
9915 /* iterate through devices to mark removed disks as spare */
9916 for (dm
= super
->disks
; dm
; dm
= dm
->next
) {
9917 if (dm
->disk
.status
& FAILED_DISK
) {
9918 int idx
= dm
->index
;
9919 /* update indexes on the disk list */
9920 /* FIXME this loop-with-the-loop looks wrong, I'm not convinced
9921 the index values will end up being correct.... NB */
9922 for (du
= super
->disks
; du
; du
= du
->next
)
9923 if (du
->index
> idx
)
9925 /* mark as spare disk */
9930 map
->num_members
/= map
->num_domains
;
9931 map
->map_state
= IMSM_T_STATE_NORMAL
;
9932 map
->raid_level
= 0;
9933 set_num_domains(map
);
9934 update_num_data_stripes(map
, imsm_dev_size(dev
));
9935 map
->failed_disk_num
= -1;
9938 if (u
->direction
== R0_TO_R10
) {
9941 /* update slots in current disk list */
9942 for (dm
= super
->disks
; dm
; dm
= dm
->next
) {
9946 /* create new *missing* disks */
9947 for (i
= 0; i
< map
->num_members
; i
++) {
9948 space
= *space_list
;
9951 *space_list
= *space
;
9953 memcpy(du
, super
->disks
, sizeof(*du
));
9957 du
->index
= (i
* 2) + 1;
9958 sprintf((char *)du
->disk
.serial
,
9959 " MISSING_%d", du
->index
);
9960 sprintf((char *)du
->serial
,
9961 "MISSING_%d", du
->index
);
9962 du
->next
= super
->missing
;
9963 super
->missing
= du
;
9965 /* create new dev and map */
9966 space
= *space_list
;
9969 *space_list
= *space
;
9970 dev_new
= (void *)space
;
9971 memcpy(dev_new
, dev
, sizeof(*dev
));
9972 /* update new map */
9973 map
= get_imsm_map(dev_new
, MAP_0
);
9975 map
->map_state
= IMSM_T_STATE_DEGRADED
;
9976 map
->raid_level
= 1;
9977 set_num_domains(map
);
9978 map
->num_members
= map
->num_members
* map
->num_domains
;
9979 update_num_data_stripes(map
, imsm_dev_size(dev
));
9981 /* replace dev<->dev_new */
9984 /* update disk order table */
9985 for (du
= super
->disks
; du
; du
= du
->next
)
9987 set_imsm_ord_tbl_ent(map
, du
->index
, du
->index
);
9988 for (du
= super
->missing
; du
; du
= du
->next
)
9989 if (du
->index
>= 0) {
9990 set_imsm_ord_tbl_ent(map
, du
->index
, du
->index
);
9991 mark_missing(super
, dv
->dev
, &du
->disk
, du
->index
);
9997 static void imsm_process_update(struct supertype
*st
,
9998 struct metadata_update
*update
)
10001 * crack open the metadata_update envelope to find the update record
10002 * update can be one of:
10003 * update_reshape_container_disks - all the arrays in the container
10004 * are being reshaped to have more devices. We need to mark
10005 * the arrays for general migration and convert selected spares
10006 * into active devices.
10007 * update_activate_spare - a spare device has replaced a failed
10008 * device in an array, update the disk_ord_tbl. If this disk is
10009 * present in all member arrays then also clear the SPARE_DISK
10011 * update_create_array
10012 * update_kill_array
10013 * update_rename_array
10014 * update_add_remove_disk
10016 struct intel_super
*super
= st
->sb
;
10017 struct imsm_super
*mpb
;
10018 enum imsm_update_type type
= *(enum imsm_update_type
*) update
->buf
;
10020 /* update requires a larger buf but the allocation failed */
10021 if (super
->next_len
&& !super
->next_buf
) {
10022 super
->next_len
= 0;
10026 if (super
->next_buf
) {
10027 memcpy(super
->next_buf
, super
->buf
, super
->len
);
10029 super
->len
= super
->next_len
;
10030 super
->buf
= super
->next_buf
;
10032 super
->next_len
= 0;
10033 super
->next_buf
= NULL
;
10036 mpb
= super
->anchor
;
10039 case update_general_migration_checkpoint
: {
10040 struct intel_dev
*id
;
10041 struct imsm_update_general_migration_checkpoint
*u
=
10042 (void *)update
->buf
;
10044 dprintf("called for update_general_migration_checkpoint\n");
10046 /* find device under general migration */
10047 for (id
= super
->devlist
; id
; id
= id
->next
) {
10048 if (is_gen_migration(id
->dev
)) {
10049 set_vol_curr_migr_unit(id
->dev
,
10050 u
->curr_migr_unit
);
10051 super
->updates_pending
++;
10056 case update_takeover
: {
10057 struct imsm_update_takeover
*u
= (void *)update
->buf
;
10058 if (apply_takeover_update(u
, super
, &update
->space_list
)) {
10059 imsm_update_version_info(super
);
10060 super
->updates_pending
++;
10065 case update_reshape_container_disks
: {
10066 struct imsm_update_reshape
*u
= (void *)update
->buf
;
10067 if (apply_reshape_container_disks_update(
10068 u
, super
, &update
->space_list
))
10069 super
->updates_pending
++;
10072 case update_reshape_migration
: {
10073 struct imsm_update_reshape_migration
*u
= (void *)update
->buf
;
10074 if (apply_reshape_migration_update(
10075 u
, super
, &update
->space_list
))
10076 super
->updates_pending
++;
10079 case update_size_change
: {
10080 struct imsm_update_size_change
*u
= (void *)update
->buf
;
10081 if (apply_size_change_update(u
, super
))
10082 super
->updates_pending
++;
10085 case update_activate_spare
: {
10086 struct imsm_update_activate_spare
*u
= (void *) update
->buf
;
10088 if (prepare_spare_to_activate(st
, u
) &&
10089 apply_update_activate_spare(u
, super
, st
->arrays
))
10090 super
->updates_pending
++;
10093 case update_create_array
: {
10094 /* someone wants to create a new array, we need to be aware of
10095 * a few races/collisions:
10096 * 1/ 'Create' called by two separate instances of mdadm
10097 * 2/ 'Create' versus 'activate_spare': mdadm has chosen
10098 * devices that have since been assimilated via
10100 * In the event this update can not be carried out mdadm will
10101 * (FIX ME) notice that its update did not take hold.
10103 struct imsm_update_create_array
*u
= (void *) update
->buf
;
10104 struct intel_dev
*dv
;
10105 struct imsm_dev
*dev
;
10106 struct imsm_map
*map
, *new_map
;
10107 unsigned long long start
, end
;
10108 unsigned long long new_start
, new_end
;
10110 struct disk_info
*inf
;
10113 /* handle racing creates: first come first serve */
10114 if (u
->dev_idx
< mpb
->num_raid_devs
) {
10115 dprintf("subarray %d already defined\n", u
->dev_idx
);
10119 /* check update is next in sequence */
10120 if (u
->dev_idx
!= mpb
->num_raid_devs
) {
10121 dprintf("can not create array %d expected index %d\n",
10122 u
->dev_idx
, mpb
->num_raid_devs
);
10126 new_map
= get_imsm_map(&u
->dev
, MAP_0
);
10127 new_start
= pba_of_lba0(new_map
);
10128 new_end
= new_start
+ per_dev_array_size(new_map
);
10129 inf
= get_disk_info(u
);
10131 /* handle activate_spare versus create race:
10132 * check to make sure that overlapping arrays do not include
10133 * overalpping disks
10135 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
10136 dev
= get_imsm_dev(super
, i
);
10137 map
= get_imsm_map(dev
, MAP_0
);
10138 start
= pba_of_lba0(map
);
10139 end
= start
+ per_dev_array_size(map
);
10140 if ((new_start
>= start
&& new_start
<= end
) ||
10141 (start
>= new_start
&& start
<= new_end
))
10146 if (disks_overlap(super
, i
, u
)) {
10147 dprintf("arrays overlap\n");
10152 /* check that prepare update was successful */
10153 if (!update
->space
) {
10154 dprintf("prepare update failed\n");
10158 /* check that all disks are still active before committing
10159 * changes. FIXME: could we instead handle this by creating a
10160 * degraded array? That's probably not what the user expects,
10161 * so better to drop this update on the floor.
10163 for (i
= 0; i
< new_map
->num_members
; i
++) {
10164 dl
= serial_to_dl(inf
[i
].serial
, super
);
10166 dprintf("disk disappeared\n");
10171 super
->updates_pending
++;
10173 /* convert spares to members and fixup ord_tbl */
10174 for (i
= 0; i
< new_map
->num_members
; i
++) {
10175 dl
= serial_to_dl(inf
[i
].serial
, super
);
10176 if (dl
->index
== -1) {
10177 dl
->index
= mpb
->num_disks
;
10179 dl
->disk
.status
|= CONFIGURED_DISK
;
10180 dl
->disk
.status
&= ~SPARE_DISK
;
10182 set_imsm_ord_tbl_ent(new_map
, i
, dl
->index
);
10185 dv
= update
->space
;
10187 update
->space
= NULL
;
10188 imsm_copy_dev(dev
, &u
->dev
);
10189 dv
->index
= u
->dev_idx
;
10190 dv
->next
= super
->devlist
;
10191 super
->devlist
= dv
;
10192 mpb
->num_raid_devs
++;
10194 imsm_update_version_info(super
);
10197 /* mdmon knows how to release update->space, but not
10198 * ((struct intel_dev *) update->space)->dev
10200 if (update
->space
) {
10201 dv
= update
->space
;
10206 case update_kill_array
: {
10207 struct imsm_update_kill_array
*u
= (void *) update
->buf
;
10208 int victim
= u
->dev_idx
;
10209 struct active_array
*a
;
10210 struct intel_dev
**dp
;
10212 /* sanity check that we are not affecting the uuid of
10213 * active arrays, or deleting an active array
10215 * FIXME when immutable ids are available, but note that
10216 * we'll also need to fixup the invalidated/active
10217 * subarray indexes in mdstat
10219 for (a
= st
->arrays
; a
; a
= a
->next
)
10220 if (a
->info
.container_member
>= victim
)
10222 /* by definition if mdmon is running at least one array
10223 * is active in the container, so checking
10224 * mpb->num_raid_devs is just extra paranoia
10226 if (a
|| mpb
->num_raid_devs
== 1 || victim
>= super
->anchor
->num_raid_devs
) {
10227 dprintf("failed to delete subarray-%d\n", victim
);
10231 for (dp
= &super
->devlist
; *dp
;)
10232 if ((*dp
)->index
== (unsigned)super
->current_vol
) {
10235 if ((*dp
)->index
> (unsigned)victim
)
10239 mpb
->num_raid_devs
--;
10240 super
->updates_pending
++;
10243 case update_rename_array
: {
10244 struct imsm_update_rename_array
*u
= (void *) update
->buf
;
10245 char name
[MAX_RAID_SERIAL_LEN
+1];
10246 int target
= u
->dev_idx
;
10247 struct active_array
*a
;
10248 struct imsm_dev
*dev
;
10250 /* sanity check that we are not affecting the uuid of
10253 memset(name
, 0, sizeof(name
));
10254 snprintf(name
, MAX_RAID_SERIAL_LEN
, "%s", (char *) u
->name
);
10255 name
[MAX_RAID_SERIAL_LEN
] = '\0';
10256 for (a
= st
->arrays
; a
; a
= a
->next
)
10257 if (a
->info
.container_member
== target
)
10259 dev
= get_imsm_dev(super
, u
->dev_idx
);
10261 if (a
|| !dev
|| imsm_is_name_allowed(super
, name
, 0) == false) {
10262 dprintf("failed to rename subarray-%d\n", target
);
10266 memcpy(dev
->volume
, name
, MAX_RAID_SERIAL_LEN
);
10267 super
->updates_pending
++;
10270 case update_add_remove_disk
: {
10271 /* we may be able to repair some arrays if disks are
10272 * being added, check the status of add_remove_disk
10273 * if discs has been added.
10275 if (add_remove_disk_update(super
)) {
10276 struct active_array
*a
;
10278 super
->updates_pending
++;
10279 for (a
= st
->arrays
; a
; a
= a
->next
)
10280 a
->check_degraded
= 1;
10284 case update_prealloc_badblocks_mem
:
10286 case update_rwh_policy
: {
10287 struct imsm_update_rwh_policy
*u
= (void *)update
->buf
;
10288 int target
= u
->dev_idx
;
10289 struct imsm_dev
*dev
= get_imsm_dev(super
, target
);
10291 if (dev
->rwh_policy
!= u
->new_policy
) {
10292 dev
->rwh_policy
= u
->new_policy
;
10293 super
->updates_pending
++;
10298 pr_err("error: unsupported process update type:(type: %d)\n", type
);
10302 static struct mdinfo
*get_spares_for_grow(struct supertype
*st
);
10304 static int imsm_prepare_update(struct supertype
*st
,
10305 struct metadata_update
*update
)
10308 * Allocate space to hold new disk entries, raid-device entries or a new
10309 * mpb if necessary. The manager synchronously waits for updates to
10310 * complete in the monitor, so new mpb buffers allocated here can be
10311 * integrated by the monitor thread without worrying about live pointers
10312 * in the manager thread.
10314 enum imsm_update_type type
;
10315 struct intel_super
*super
= st
->sb
;
10316 unsigned int sector_size
= super
->sector_size
;
10317 struct imsm_super
*mpb
= super
->anchor
;
10321 if (update
->len
< (int)sizeof(type
))
10324 type
= *(enum imsm_update_type
*) update
->buf
;
10327 case update_general_migration_checkpoint
:
10328 if (update
->len
< (int)sizeof(struct imsm_update_general_migration_checkpoint
))
10330 dprintf("called for update_general_migration_checkpoint\n");
10332 case update_takeover
: {
10333 struct imsm_update_takeover
*u
= (void *)update
->buf
;
10334 if (update
->len
< (int)sizeof(*u
))
10336 if (u
->direction
== R0_TO_R10
) {
10337 void **tail
= (void **)&update
->space_list
;
10338 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->subarray
);
10339 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
10340 int num_members
= map
->num_members
;
10343 /* allocate memory for added disks */
10344 for (i
= 0; i
< num_members
; i
++) {
10345 size
= sizeof(struct dl
);
10346 space
= xmalloc(size
);
10351 /* allocate memory for new device */
10352 size
= sizeof_imsm_dev(super
->devlist
->dev
, 0) +
10353 (num_members
* sizeof(__u32
));
10354 space
= xmalloc(size
);
10358 len
= disks_to_mpb_size(num_members
* 2);
10363 case update_reshape_container_disks
: {
10364 /* Every raid device in the container is about to
10365 * gain some more devices, and we will enter a
10367 * So each 'imsm_map' will be bigger, and the imsm_vol
10368 * will now hold 2 of them.
10369 * Thus we need new 'struct imsm_dev' allocations sized
10370 * as sizeof_imsm_dev but with more devices in both maps.
10372 struct imsm_update_reshape
*u
= (void *)update
->buf
;
10373 struct intel_dev
*dl
;
10374 void **space_tail
= (void**)&update
->space_list
;
10376 if (update
->len
< (int)sizeof(*u
))
10379 dprintf("for update_reshape\n");
10381 for (dl
= super
->devlist
; dl
; dl
= dl
->next
) {
10382 int size
= sizeof_imsm_dev(dl
->dev
, 1);
10384 if (u
->new_raid_disks
> u
->old_raid_disks
)
10385 size
+= sizeof(__u32
)*2*
10386 (u
->new_raid_disks
- u
->old_raid_disks
);
10390 *space_tail
= NULL
;
10393 len
= disks_to_mpb_size(u
->new_raid_disks
);
10394 dprintf("New anchor length is %llu\n", (unsigned long long)len
);
10397 case update_reshape_migration
: {
10398 /* for migration level 0->5 we need to add disks
10399 * so the same as for container operation we will copy
10400 * device to the bigger location.
10401 * in memory prepared device and new disk area are prepared
10402 * for usage in process update
10404 struct imsm_update_reshape_migration
*u
= (void *)update
->buf
;
10405 struct intel_dev
*id
;
10406 void **space_tail
= (void **)&update
->space_list
;
10409 int current_level
= -1;
10411 if (update
->len
< (int)sizeof(*u
))
10414 dprintf("for update_reshape\n");
10416 /* add space for bigger array in update
10418 for (id
= super
->devlist
; id
; id
= id
->next
) {
10419 if (id
->index
== (unsigned)u
->subdev
) {
10420 size
= sizeof_imsm_dev(id
->dev
, 1);
10421 if (u
->new_raid_disks
> u
->old_raid_disks
)
10422 size
+= sizeof(__u32
)*2*
10423 (u
->new_raid_disks
- u
->old_raid_disks
);
10427 *space_tail
= NULL
;
10431 if (update
->space_list
== NULL
)
10434 /* add space for disk in update
10436 size
= sizeof(struct dl
);
10440 *space_tail
= NULL
;
10442 /* add spare device to update
10444 for (id
= super
->devlist
; id
; id
= id
->next
)
10445 if (id
->index
== (unsigned)u
->subdev
) {
10446 struct imsm_dev
*dev
;
10447 struct imsm_map
*map
;
10449 dev
= get_imsm_dev(super
, u
->subdev
);
10450 map
= get_imsm_map(dev
, MAP_0
);
10451 current_level
= map
->raid_level
;
10454 if (u
->new_level
== 5 && u
->new_level
!= current_level
) {
10455 struct mdinfo
*spares
;
10457 spares
= get_spares_for_grow(st
);
10460 struct mdinfo
*dev
;
10462 dev
= spares
->devs
;
10465 makedev(dev
->disk
.major
,
10467 dl
= get_disk_super(super
,
10470 dl
->index
= u
->old_raid_disks
;
10473 sysfs_free(spares
);
10476 len
= disks_to_mpb_size(u
->new_raid_disks
);
10477 dprintf("New anchor length is %llu\n", (unsigned long long)len
);
10480 case update_size_change
: {
10481 if (update
->len
< (int)sizeof(struct imsm_update_size_change
))
10485 case update_activate_spare
: {
10486 if (update
->len
< (int)sizeof(struct imsm_update_activate_spare
))
10490 case update_create_array
: {
10491 struct imsm_update_create_array
*u
= (void *) update
->buf
;
10492 struct intel_dev
*dv
;
10493 struct imsm_dev
*dev
= &u
->dev
;
10494 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
10496 struct disk_info
*inf
;
10500 if (update
->len
< (int)sizeof(*u
))
10503 inf
= get_disk_info(u
);
10504 len
= sizeof_imsm_dev(dev
, 1);
10505 /* allocate a new super->devlist entry */
10506 dv
= xmalloc(sizeof(*dv
));
10507 dv
->dev
= xmalloc(len
);
10508 update
->space
= dv
;
10510 /* count how many spares will be converted to members */
10511 for (i
= 0; i
< map
->num_members
; i
++) {
10512 dl
= serial_to_dl(inf
[i
].serial
, super
);
10514 /* hmm maybe it failed?, nothing we can do about
10519 if (count_memberships(dl
, super
) == 0)
10522 len
+= activate
* sizeof(struct imsm_disk
);
10525 case update_kill_array
: {
10526 if (update
->len
< (int)sizeof(struct imsm_update_kill_array
))
10530 case update_rename_array
: {
10531 if (update
->len
< (int)sizeof(struct imsm_update_rename_array
))
10535 case update_add_remove_disk
:
10536 /* no update->len needed */
10538 case update_prealloc_badblocks_mem
:
10539 super
->extra_space
+= sizeof(struct bbm_log
) -
10540 get_imsm_bbm_log_size(super
->bbm_log
);
10542 case update_rwh_policy
: {
10543 if (update
->len
< (int)sizeof(struct imsm_update_rwh_policy
))
10551 /* check if we need a larger metadata buffer */
10552 if (super
->next_buf
)
10553 buf_len
= super
->next_len
;
10555 buf_len
= super
->len
;
10557 if (__le32_to_cpu(mpb
->mpb_size
) + super
->extra_space
+ len
> buf_len
) {
10558 /* ok we need a larger buf than what is currently allocated
10559 * if this allocation fails process_update will notice that
10560 * ->next_len is set and ->next_buf is NULL
10562 buf_len
= ROUND_UP(__le32_to_cpu(mpb
->mpb_size
) +
10563 super
->extra_space
+ len
, sector_size
);
10564 if (super
->next_buf
)
10565 free(super
->next_buf
);
10567 super
->next_len
= buf_len
;
10568 if (posix_memalign(&super
->next_buf
, sector_size
, buf_len
) == 0)
10569 memset(super
->next_buf
, 0, buf_len
);
10571 super
->next_buf
= NULL
;
10576 /* must be called while manager is quiesced */
10577 static void imsm_delete(struct intel_super
*super
, struct dl
**dlp
, unsigned index
)
10579 struct imsm_super
*mpb
= super
->anchor
;
10581 struct imsm_dev
*dev
;
10582 struct imsm_map
*map
;
10583 unsigned int i
, j
, num_members
;
10584 __u32 ord
, ord_map0
;
10585 struct bbm_log
*log
= super
->bbm_log
;
10587 dprintf("deleting device[%d] from imsm_super\n", index
);
10589 /* shift all indexes down one */
10590 for (iter
= super
->disks
; iter
; iter
= iter
->next
)
10591 if (iter
->index
> (int)index
)
10593 for (iter
= super
->missing
; iter
; iter
= iter
->next
)
10594 if (iter
->index
> (int)index
)
10597 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
10598 dev
= get_imsm_dev(super
, i
);
10599 map
= get_imsm_map(dev
, MAP_0
);
10600 num_members
= map
->num_members
;
10601 for (j
= 0; j
< num_members
; j
++) {
10602 /* update ord entries being careful not to propagate
10603 * ord-flags to the first map
10605 ord
= get_imsm_ord_tbl_ent(dev
, j
, MAP_X
);
10606 ord_map0
= get_imsm_ord_tbl_ent(dev
, j
, MAP_0
);
10608 if (ord_to_idx(ord
) <= index
)
10611 map
= get_imsm_map(dev
, MAP_0
);
10612 set_imsm_ord_tbl_ent(map
, j
, ord_map0
- 1);
10613 map
= get_imsm_map(dev
, MAP_1
);
10615 set_imsm_ord_tbl_ent(map
, j
, ord
- 1);
10619 for (i
= 0; i
< log
->entry_count
; i
++) {
10620 struct bbm_log_entry
*entry
= &log
->marked_block_entries
[i
];
10622 if (entry
->disk_ordinal
<= index
)
10624 entry
->disk_ordinal
--;
10628 super
->updates_pending
++;
10630 struct dl
*dl
= *dlp
;
10632 *dlp
= (*dlp
)->next
;
10633 __free_imsm_disk(dl
, 1);
10637 static int imsm_get_allowed_degradation(int level
, int raid_disks
,
10638 struct intel_super
*super
,
10639 struct imsm_dev
*dev
)
10645 struct imsm_map
*map
;
10648 ret_val
= raid_disks
/2;
10649 /* check map if all disks pairs not failed
10652 map
= get_imsm_map(dev
, MAP_0
);
10653 for (i
= 0; i
< ret_val
; i
++) {
10654 int degradation
= 0;
10655 if (get_imsm_disk(super
, i
) == NULL
)
10657 if (get_imsm_disk(super
, i
+ 1) == NULL
)
10659 if (degradation
== 2)
10662 map
= get_imsm_map(dev
, MAP_1
);
10663 /* if there is no second map
10664 * result can be returned
10668 /* check degradation in second map
10670 for (i
= 0; i
< ret_val
; i
++) {
10671 int degradation
= 0;
10672 if (get_imsm_disk(super
, i
) == NULL
)
10674 if (get_imsm_disk(super
, i
+ 1) == NULL
)
10676 if (degradation
== 2)
10690 /*******************************************************************************
10691 * Function: validate_container_imsm
10692 * Description: This routine validates container after assemble,
10693 * eg. if devices in container are under the same controller.
10696 * info : linked list with info about devices used in array
10700 ******************************************************************************/
10701 int validate_container_imsm(struct mdinfo
*info
)
10703 if (check_no_platform())
10706 struct sys_dev
*idev
;
10707 struct sys_dev
*hba
= NULL
;
10708 struct sys_dev
*intel_devices
= find_intel_devices();
10709 char *dev_path
= devt_to_devpath(makedev(info
->disk
.major
,
10710 info
->disk
.minor
), 1, NULL
);
10712 for (idev
= intel_devices
; idev
; idev
= idev
->next
) {
10713 if (dev_path
&& strstr(dev_path
, idev
->path
)) {
10722 pr_err("WARNING - Cannot detect HBA for device %s!\n",
10723 devid2kname(makedev(info
->disk
.major
, info
->disk
.minor
)));
10727 const struct imsm_orom
*orom
= get_orom_by_device_id(hba
->dev_id
);
10728 struct mdinfo
*dev
;
10730 for (dev
= info
->next
; dev
; dev
= dev
->next
) {
10731 dev_path
= devt_to_devpath(makedev(dev
->disk
.major
,
10732 dev
->disk
.minor
), 1, NULL
);
10734 struct sys_dev
*hba2
= NULL
;
10735 for (idev
= intel_devices
; idev
; idev
= idev
->next
) {
10736 if (dev_path
&& strstr(dev_path
, idev
->path
)) {
10744 const struct imsm_orom
*orom2
= hba2
== NULL
? NULL
:
10745 get_orom_by_device_id(hba2
->dev_id
);
10747 if (hba2
&& hba
->type
!= hba2
->type
) {
10748 pr_err("WARNING - HBAs of devices do not match %s != %s\n",
10749 get_sys_dev_type(hba
->type
), get_sys_dev_type(hba2
->type
));
10753 if (orom
!= orom2
) {
10754 pr_err("WARNING - IMSM container assembled with disks under different HBAs!\n"
10755 " This operation is not supported and can lead to data loss.\n");
10760 pr_err("WARNING - IMSM container assembled with disks under HBAs without IMSM platform support!\n"
10761 " This operation is not supported and can lead to data loss.\n");
10769 /*******************************************************************************
10770 * Function: imsm_record_badblock
10771 * Description: This routine stores new bad block record in BBM log
10774 * a : array containing a bad block
10775 * slot : disk number containing a bad block
10776 * sector : bad block sector
10777 * length : bad block sectors range
10781 ******************************************************************************/
10782 static int imsm_record_badblock(struct active_array
*a
, int slot
,
10783 unsigned long long sector
, int length
)
10785 struct intel_super
*super
= a
->container
->sb
;
10789 ord
= imsm_disk_slot_to_ord(a
, slot
);
10793 ret
= record_new_badblock(super
->bbm_log
, ord_to_idx(ord
), sector
,
10796 super
->updates_pending
++;
10800 /*******************************************************************************
10801 * Function: imsm_clear_badblock
10802 * Description: This routine clears bad block record from BBM log
10805 * a : array containing a bad block
10806 * slot : disk number containing a bad block
10807 * sector : bad block sector
10808 * length : bad block sectors range
10812 ******************************************************************************/
10813 static int imsm_clear_badblock(struct active_array
*a
, int slot
,
10814 unsigned long long sector
, int length
)
10816 struct intel_super
*super
= a
->container
->sb
;
10820 ord
= imsm_disk_slot_to_ord(a
, slot
);
10824 ret
= clear_badblock(super
->bbm_log
, ord_to_idx(ord
), sector
, length
);
10826 super
->updates_pending
++;
10830 /*******************************************************************************
10831 * Function: imsm_get_badblocks
10832 * Description: This routine get list of bad blocks for an array
10836 * slot : disk number
10838 * bb : structure containing bad blocks
10840 ******************************************************************************/
10841 static struct md_bb
*imsm_get_badblocks(struct active_array
*a
, int slot
)
10843 int inst
= a
->info
.container_member
;
10844 struct intel_super
*super
= a
->container
->sb
;
10845 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
10846 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
10849 ord
= imsm_disk_slot_to_ord(a
, slot
);
10853 get_volume_badblocks(super
->bbm_log
, ord_to_idx(ord
), pba_of_lba0(map
),
10854 per_dev_array_size(map
), &super
->bb
);
10858 /*******************************************************************************
10859 * Function: examine_badblocks_imsm
10860 * Description: Prints list of bad blocks on a disk to the standard output
10863 * st : metadata handler
10864 * fd : open file descriptor for device
10865 * devname : device name
10869 ******************************************************************************/
10870 static int examine_badblocks_imsm(struct supertype
*st
, int fd
, char *devname
)
10872 struct intel_super
*super
= st
->sb
;
10873 struct bbm_log
*log
= super
->bbm_log
;
10874 struct dl
*d
= NULL
;
10877 for (d
= super
->disks
; d
; d
= d
->next
) {
10878 if (strcmp(d
->devname
, devname
) == 0)
10882 if ((d
== NULL
) || (d
->index
< 0)) { /* serial mismatch probably */
10883 pr_err("%s doesn't appear to be part of a raid array\n",
10890 struct bbm_log_entry
*entry
= &log
->marked_block_entries
[0];
10892 for (i
= 0; i
< log
->entry_count
; i
++) {
10893 if (entry
[i
].disk_ordinal
== d
->index
) {
10894 unsigned long long sector
= __le48_to_cpu(
10895 &entry
[i
].defective_block_start
);
10896 int cnt
= entry
[i
].marked_count
+ 1;
10899 printf("Bad-blocks on %s:\n", devname
);
10903 printf("%20llu for %d sectors\n", sector
, cnt
);
10909 printf("No bad-blocks list configured on %s\n", devname
);
10913 /*******************************************************************************
10914 * Function: init_migr_record_imsm
10915 * Description: Function inits imsm migration record
10917 * super : imsm internal array info
10918 * dev : device under migration
10919 * info : general array info to find the smallest device
10922 ******************************************************************************/
10923 void init_migr_record_imsm(struct supertype
*st
, struct imsm_dev
*dev
,
10924 struct mdinfo
*info
)
10926 struct intel_super
*super
= st
->sb
;
10927 struct migr_record
*migr_rec
= super
->migr_rec
;
10928 int new_data_disks
;
10929 unsigned long long dsize
, dev_sectors
;
10930 long long unsigned min_dev_sectors
= -1LLU;
10931 struct imsm_map
*map_dest
= get_imsm_map(dev
, MAP_0
);
10932 struct imsm_map
*map_src
= get_imsm_map(dev
, MAP_1
);
10933 unsigned long long num_migr_units
;
10934 unsigned long long array_blocks
;
10935 struct dl
*dl_disk
= NULL
;
10937 memset(migr_rec
, 0, sizeof(struct migr_record
));
10938 migr_rec
->family_num
= __cpu_to_le32(super
->anchor
->family_num
);
10940 /* only ascending reshape supported now */
10941 migr_rec
->ascending_migr
= __cpu_to_le32(1);
10943 migr_rec
->dest_depth_per_unit
= GEN_MIGR_AREA_SIZE
/
10944 max(map_dest
->blocks_per_strip
, map_src
->blocks_per_strip
);
10945 migr_rec
->dest_depth_per_unit
*=
10946 max(map_dest
->blocks_per_strip
, map_src
->blocks_per_strip
);
10947 new_data_disks
= imsm_num_data_members(map_dest
);
10948 migr_rec
->blocks_per_unit
=
10949 __cpu_to_le32(migr_rec
->dest_depth_per_unit
* new_data_disks
);
10950 migr_rec
->dest_depth_per_unit
=
10951 __cpu_to_le32(migr_rec
->dest_depth_per_unit
);
10952 array_blocks
= info
->component_size
* new_data_disks
;
10954 array_blocks
/ __le32_to_cpu(migr_rec
->blocks_per_unit
);
10956 if (array_blocks
% __le32_to_cpu(migr_rec
->blocks_per_unit
))
10958 set_num_migr_units(migr_rec
, num_migr_units
);
10960 migr_rec
->post_migr_vol_cap
= dev
->size_low
;
10961 migr_rec
->post_migr_vol_cap_hi
= dev
->size_high
;
10963 /* Find the smallest dev */
10964 for (dl_disk
= super
->disks
; dl_disk
; dl_disk
= dl_disk
->next
) {
10965 /* ignore spares in container */
10966 if (dl_disk
->index
< 0)
10968 get_dev_size(dl_disk
->fd
, NULL
, &dsize
);
10969 dev_sectors
= dsize
/ 512;
10970 if (dev_sectors
< min_dev_sectors
)
10971 min_dev_sectors
= dev_sectors
;
10973 set_migr_chkp_area_pba(migr_rec
, min_dev_sectors
-
10974 RAID_DISK_RESERVED_BLOCKS_IMSM_HI
);
10976 write_imsm_migr_rec(st
);
10981 /*******************************************************************************
10982 * Function: save_backup_imsm
10983 * Description: Function saves critical data stripes to Migration Copy Area
10984 * and updates the current migration unit status.
10985 * Use restore_stripes() to form a destination stripe,
10986 * and to write it to the Copy Area.
10988 * st : supertype information
10989 * dev : imsm device that backup is saved for
10990 * info : general array info
10991 * buf : input buffer
10992 * length : length of data to backup (blocks_per_unit)
10996 ******************************************************************************/
10997 int save_backup_imsm(struct supertype
*st
,
10998 struct imsm_dev
*dev
,
10999 struct mdinfo
*info
,
11004 struct intel_super
*super
= st
->sb
;
11006 struct imsm_map
*map_dest
= get_imsm_map(dev
, MAP_0
);
11007 int new_disks
= map_dest
->num_members
;
11008 int dest_layout
= 0;
11009 int dest_chunk
, targets
[new_disks
];
11010 unsigned long long start
, target_offsets
[new_disks
];
11011 int data_disks
= imsm_num_data_members(map_dest
);
11013 for (i
= 0; i
< new_disks
; i
++) {
11014 struct dl
*dl_disk
= get_imsm_dl_disk(super
, i
);
11015 if (dl_disk
&& is_fd_valid(dl_disk
->fd
))
11016 targets
[i
] = dl_disk
->fd
;
11021 start
= info
->reshape_progress
* 512;
11022 for (i
= 0; i
< new_disks
; i
++) {
11023 target_offsets
[i
] = migr_chkp_area_pba(super
->migr_rec
) * 512;
11024 /* move back copy area adderss, it will be moved forward
11025 * in restore_stripes() using start input variable
11027 target_offsets
[i
] -= start
/data_disks
;
11030 dest_layout
= imsm_level_to_layout(map_dest
->raid_level
);
11031 dest_chunk
= __le16_to_cpu(map_dest
->blocks_per_strip
) * 512;
11033 if (restore_stripes(targets
, /* list of dest devices */
11034 target_offsets
, /* migration record offsets */
11037 map_dest
->raid_level
,
11039 -1, /* source backup file descriptor */
11040 0, /* input buf offset
11041 * always 0 buf is already offseted */
11045 pr_err("Error restoring stripes\n");
11055 /*******************************************************************************
11056 * Function: save_checkpoint_imsm
11057 * Description: Function called for current unit status update
11058 * in the migration record. It writes it to disk.
11060 * super : imsm internal array info
11061 * info : general array info
11065 * 2: failure, means no valid migration record
11066 * / no general migration in progress /
11067 ******************************************************************************/
11068 int save_checkpoint_imsm(struct supertype
*st
, struct mdinfo
*info
, int state
)
11070 struct intel_super
*super
= st
->sb
;
11071 unsigned long long blocks_per_unit
;
11072 unsigned long long curr_migr_unit
;
11074 if (load_imsm_migr_rec(super
) != 0) {
11075 dprintf("imsm: ERROR: Cannot read migration record for checkpoint save.\n");
11079 blocks_per_unit
= __le32_to_cpu(super
->migr_rec
->blocks_per_unit
);
11080 if (blocks_per_unit
== 0) {
11081 dprintf("imsm: no migration in progress.\n");
11084 curr_migr_unit
= info
->reshape_progress
/ blocks_per_unit
;
11085 /* check if array is alligned to copy area
11086 * if it is not alligned, add one to current migration unit value
11087 * this can happend on array reshape finish only
11089 if (info
->reshape_progress
% blocks_per_unit
)
11092 set_current_migr_unit(super
->migr_rec
, curr_migr_unit
);
11093 super
->migr_rec
->rec_status
= __cpu_to_le32(state
);
11094 set_migr_dest_1st_member_lba(super
->migr_rec
,
11095 super
->migr_rec
->dest_depth_per_unit
* curr_migr_unit
);
11097 if (write_imsm_migr_rec(st
) < 0) {
11098 dprintf("imsm: Cannot write migration record outside backup area\n");
11105 /*******************************************************************************
11106 * Function: recover_backup_imsm
11107 * Description: Function recovers critical data from the Migration Copy Area
11108 * while assembling an array.
11110 * super : imsm internal array info
11111 * info : general array info
11113 * 0 : success (or there is no data to recover)
11115 ******************************************************************************/
11116 int recover_backup_imsm(struct supertype
*st
, struct mdinfo
*info
)
11118 struct intel_super
*super
= st
->sb
;
11119 struct migr_record
*migr_rec
= super
->migr_rec
;
11120 struct imsm_map
*map_dest
;
11121 struct intel_dev
*id
= NULL
;
11122 unsigned long long read_offset
;
11123 unsigned long long write_offset
;
11125 int new_disks
, err
;
11128 unsigned int sector_size
= super
->sector_size
;
11129 unsigned long long curr_migr_unit
= current_migr_unit(migr_rec
);
11130 unsigned long long num_migr_units
= get_num_migr_units(migr_rec
);
11131 char buffer
[SYSFS_MAX_BUF_SIZE
];
11132 int skipped_disks
= 0;
11133 struct dl
*dl_disk
;
11135 err
= sysfs_get_str(info
, NULL
, "array_state", (char *)buffer
, sizeof(buffer
));
11139 /* recover data only during assemblation */
11140 if (strncmp(buffer
, "inactive", 8) != 0)
11142 /* no data to recover */
11143 if (__le32_to_cpu(migr_rec
->rec_status
) == UNIT_SRC_NORMAL
)
11145 if (curr_migr_unit
>= num_migr_units
)
11148 /* find device during reshape */
11149 for (id
= super
->devlist
; id
; id
= id
->next
)
11150 if (is_gen_migration(id
->dev
))
11155 map_dest
= get_imsm_map(id
->dev
, MAP_0
);
11156 new_disks
= map_dest
->num_members
;
11158 read_offset
= migr_chkp_area_pba(migr_rec
) * 512;
11160 write_offset
= (migr_dest_1st_member_lba(migr_rec
) +
11161 pba_of_lba0(map_dest
)) * 512;
11163 unit_len
= __le32_to_cpu(migr_rec
->dest_depth_per_unit
) * 512;
11164 if (posix_memalign((void **)&buf
, sector_size
, unit_len
) != 0)
11167 for (dl_disk
= super
->disks
; dl_disk
; dl_disk
= dl_disk
->next
) {
11168 if (dl_disk
->index
< 0)
11171 if (!is_fd_valid(dl_disk
->fd
)) {
11175 if (lseek64(dl_disk
->fd
, read_offset
, SEEK_SET
) < 0) {
11176 pr_err("Cannot seek to block: %s\n",
11181 if (read(dl_disk
->fd
, buf
, unit_len
) != (ssize_t
)unit_len
) {
11182 pr_err("Cannot read copy area block: %s\n",
11187 if (lseek64(dl_disk
->fd
, write_offset
, SEEK_SET
) < 0) {
11188 pr_err("Cannot seek to block: %s\n",
11193 if (write(dl_disk
->fd
, buf
, unit_len
) != (ssize_t
)unit_len
) {
11194 pr_err("Cannot restore block: %s\n",
11201 if (skipped_disks
> imsm_get_allowed_degradation(info
->new_level
,
11205 pr_err("Cannot restore data from backup. Too many failed disks\n");
11209 if (save_checkpoint_imsm(st
, info
, UNIT_SRC_NORMAL
)) {
11210 /* ignore error == 2, this can mean end of reshape here
11212 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL) during restart\n");
11222 * test_and_add_drive_controller_policy_imsm() - add disk controller to policies list.
11223 * @type: Policy type to search on list.
11224 * @pols: List of currently recorded policies.
11225 * @disk_fd: File descriptor of the device to check.
11226 * @hba: The hba disk is attached, could be NULL if verification is disabled.
11227 * @verbose: verbose flag.
11229 * IMSM cares about drive physical placement. If @hba is not set, it adds unknown policy.
11230 * If there is no controller policy on pols we are free to add first one. If there is a policy then,
11231 * new must be the same - no controller mixing allowed.
11233 static mdadm_status_t
11234 test_and_add_drive_controller_policy_imsm(const char * const type
, dev_policy_t
**pols
, int disk_fd
,
11235 struct sys_dev
*hba
, const int verbose
)
11237 const char *controller_policy
= get_sys_dev_type(SYS_DEV_UNKNOWN
);
11238 struct dev_policy
*pol
= pol_find(*pols
, (char *)type
);
11239 char devname
[MAX_RAID_SERIAL_LEN
];
11242 controller_policy
= get_sys_dev_type(hba
->type
);
11245 pol_add(pols
, (char *)type
, (char *)controller_policy
, "imsm");
11246 return MDADM_STATUS_SUCCESS
;
11249 if (strcmp(pol
->value
, controller_policy
) == 0)
11250 return MDADM_STATUS_SUCCESS
;
11252 fd2devname(disk_fd
, devname
);
11253 pr_vrb("Intel(R) raid controller \"%s\" found for %s, but \"%s\" was detected earlier\n",
11254 controller_policy
, devname
, pol
->value
);
11255 pr_vrb("Disks under different controllers cannot be used, aborting\n");
11257 return MDADM_STATUS_ERROR
;
11260 struct imsm_drive_policy
{
11262 mdadm_status_t (*test_and_add_drive_policy
)(const char * const type
,
11263 struct dev_policy
**pols
, int disk_fd
,
11264 struct sys_dev
*hba
, const int verbose
);
11267 struct imsm_drive_policy imsm_policies
[] = {
11268 {"controller", test_and_add_drive_controller_policy_imsm
},
11271 mdadm_status_t
test_and_add_drive_policies_imsm(struct dev_policy
**pols
, int disk_fd
,
11274 struct imsm_drive_policy
*imsm_pol
;
11275 struct sys_dev
*hba
= NULL
;
11276 char path
[PATH_MAX
];
11277 mdadm_status_t ret
;
11280 /* If imsm platform verification is disabled, do not search for hba. */
11281 if (check_no_platform() != 1) {
11282 if (!diskfd_to_devpath(disk_fd
, 1, path
)) {
11283 pr_vrb("IMSM: Failed to retrieve device path by file descriptor.\n");
11284 return MDADM_STATUS_ERROR
;
11287 hba
= find_disk_attached_hba(disk_fd
, path
);
11289 pr_vrb("IMSM: Failed to find hba for %s\n", path
);
11290 return MDADM_STATUS_ERROR
;
11294 for (i
= 0; i
< ARRAY_SIZE(imsm_policies
); i
++) {
11295 imsm_pol
= &imsm_policies
[i
];
11297 ret
= imsm_pol
->test_and_add_drive_policy(imsm_pol
->type
, pols
, disk_fd
, hba
,
11299 if (ret
!= MDADM_STATUS_SUCCESS
)
11300 /* Inherit error code */
11304 return MDADM_STATUS_SUCCESS
;
11308 * get_spare_criteria_imsm() - set spare criteria.
11310 * @mddev_path: path to md device devnode, it must be container.
11311 * @c: spare_criteria struct to fill, not NULL.
11313 * If superblock is not loaded, use mddev_path to load_container. It must be given in this case.
11314 * Filles size and sector size accordingly to superblock.
11316 mdadm_status_t
get_spare_criteria_imsm(struct supertype
*st
, char *mddev_path
,
11317 struct spare_criteria
*c
)
11319 mdadm_status_t ret
= MDADM_STATUS_ERROR
;
11320 bool free_superblock
= false;
11321 unsigned long long size
= 0;
11322 struct intel_super
*super
;
11327 /* If no superblock and no mddev_path, we cannot load superblock. */
11328 assert(st
->sb
|| mddev_path
);
11331 int fd
= open(mddev_path
, O_RDONLY
);
11334 if (!is_fd_valid(fd
))
11335 return MDADM_STATUS_ERROR
;
11338 if (load_container_imsm(st
, fd
, st
->devnm
)) {
11340 return MDADM_STATUS_ERROR
;
11342 free_superblock
= true;
11345 rv
= mddev_test_and_add_drive_policies(st
, &c
->pols
, fd
, 0);
11348 if (rv
!= MDADM_STATUS_SUCCESS
)
11354 /* find first active disk in array */
11356 while (dl
&& (is_failed(&dl
->disk
) || dl
->index
== -1))
11362 /* find last lba used by subarrays */
11363 e
= get_extents(super
, dl
, 0);
11367 for (i
= 0; e
[i
].size
; i
++)
11370 size
= e
[i
- 1].start
+ e
[i
- 1].size
;
11373 /* add the amount of space needed for metadata */
11374 size
+= imsm_min_reserved_sectors(super
);
11376 c
->min_size
= size
* 512;
11377 c
->sector_size
= super
->sector_size
;
11378 c
->criteria_set
= true;
11379 ret
= MDADM_STATUS_SUCCESS
;
11382 if (free_superblock
)
11383 free_super_imsm(st
);
11385 if (ret
!= MDADM_STATUS_SUCCESS
)
11386 c
->criteria_set
= false;
11391 static char *imsm_find_array_devnm_by_subdev(int subdev
, char *container
)
11393 static char devnm
[32];
11394 char subdev_name
[20];
11395 struct mdstat_ent
*mdstat
;
11397 sprintf(subdev_name
, "%d", subdev
);
11398 mdstat
= mdstat_by_subdev(subdev_name
, container
);
11402 strcpy(devnm
, mdstat
->devnm
);
11403 free_mdstat(mdstat
);
11407 static int imsm_reshape_is_allowed_on_container(struct supertype
*st
,
11408 struct geo_params
*geo
,
11409 int *old_raid_disks
,
11412 /* currently we only support increasing the number of devices
11413 * for a container. This increases the number of device for each
11414 * member array. They must all be RAID0 or RAID5.
11417 struct mdinfo
*info
, *member
;
11418 int devices_that_can_grow
= 0;
11420 dprintf("imsm: imsm_reshape_is_allowed_on_container(ENTER): st->devnm = (%s)\n", st
->devnm
);
11422 if (geo
->size
> 0 ||
11423 geo
->level
!= UnSet
||
11424 geo
->layout
!= UnSet
||
11425 geo
->chunksize
!= 0 ||
11426 geo
->raid_disks
== UnSet
) {
11427 dprintf("imsm: Container operation is allowed for raid disks number change only.\n");
11431 if (direction
== ROLLBACK_METADATA_CHANGES
) {
11432 dprintf("imsm: Metadata changes rollback is not supported for container operation.\n");
11436 info
= container_content_imsm(st
, NULL
);
11437 for (member
= info
; member
; member
= member
->next
) {
11440 dprintf("imsm: checking device_num: %i\n",
11441 member
->container_member
);
11443 if (geo
->raid_disks
<= member
->array
.raid_disks
) {
11444 /* we work on container for Online Capacity Expansion
11445 * only so raid_disks has to grow
11447 dprintf("imsm: for container operation raid disks increase is required\n");
11451 if (info
->array
.level
!= 0 && info
->array
.level
!= 5) {
11452 /* we cannot use this container with other raid level
11454 dprintf("imsm: for container operation wrong raid level (%i) detected\n",
11455 info
->array
.level
);
11458 /* check for platform support
11459 * for this raid level configuration
11461 struct intel_super
*super
= st
->sb
;
11462 if (!is_raid_level_supported(super
->orom
,
11463 member
->array
.level
,
11464 geo
->raid_disks
)) {
11465 dprintf("platform does not support raid%d with %d disk%s\n",
11468 geo
->raid_disks
> 1 ? "s" : "");
11471 /* check if component size is aligned to chunk size
11473 if (info
->component_size
%
11474 (info
->array
.chunk_size
/512)) {
11475 dprintf("Component size is not aligned to chunk size\n");
11480 if (*old_raid_disks
&&
11481 info
->array
.raid_disks
!= *old_raid_disks
)
11483 *old_raid_disks
= info
->array
.raid_disks
;
11485 /* All raid5 and raid0 volumes in container
11486 * have to be ready for Online Capacity Expansion
11487 * so they need to be assembled. We have already
11488 * checked that no recovery etc is happening.
11490 result
= imsm_find_array_devnm_by_subdev(member
->container_member
,
11491 st
->container_devnm
);
11492 if (result
== NULL
) {
11493 dprintf("imsm: cannot find array\n");
11496 devices_that_can_grow
++;
11499 if (!member
&& devices_that_can_grow
)
11503 dprintf("Container operation allowed\n");
11505 dprintf("Error: %i\n", ret_val
);
11510 /* Function: get_spares_for_grow
11511 * Description: Allocates memory and creates list of spare devices
11512 * avaliable in container. Checks if spare drive size is acceptable.
11513 * Parameters: Pointer to the supertype structure
11514 * Returns: Pointer to the list of spare devices (mdinfo structure) on success,
11517 static struct mdinfo
*get_spares_for_grow(struct supertype
*st
)
11519 struct spare_criteria sc
= {0};
11520 struct mdinfo
*spares
;
11522 get_spare_criteria_imsm(st
, NULL
, &sc
);
11523 spares
= container_choose_spares(st
, &sc
, NULL
, NULL
, NULL
, 0);
11525 dev_policy_free(sc
.pols
);
11530 /******************************************************************************
11531 * function: imsm_create_metadata_update_for_reshape
11532 * Function creates update for whole IMSM container.
11534 ******************************************************************************/
11535 static int imsm_create_metadata_update_for_reshape(
11536 struct supertype
*st
,
11537 struct geo_params
*geo
,
11538 int old_raid_disks
,
11539 struct imsm_update_reshape
**updatep
)
11541 struct intel_super
*super
= st
->sb
;
11542 struct imsm_super
*mpb
= super
->anchor
;
11543 int update_memory_size
;
11544 struct imsm_update_reshape
*u
;
11545 struct mdinfo
*spares
;
11548 struct mdinfo
*dev
;
11550 dprintf("(enter) raid_disks = %i\n", geo
->raid_disks
);
11552 delta_disks
= geo
->raid_disks
- old_raid_disks
;
11554 /* size of all update data without anchor */
11555 update_memory_size
= sizeof(struct imsm_update_reshape
);
11557 /* now add space for spare disks that we need to add. */
11558 update_memory_size
+= sizeof(u
->new_disks
[0]) * (delta_disks
- 1);
11560 u
= xcalloc(1, update_memory_size
);
11561 u
->type
= update_reshape_container_disks
;
11562 u
->old_raid_disks
= old_raid_disks
;
11563 u
->new_raid_disks
= geo
->raid_disks
;
11565 /* now get spare disks list
11567 spares
= get_spares_for_grow(st
);
11569 if (spares
== NULL
|| delta_disks
> spares
->array
.spare_disks
) {
11570 pr_err("imsm: ERROR: Cannot get spare devices for %s.\n", geo
->dev_name
);
11575 /* we have got spares
11576 * update disk list in imsm_disk list table in anchor
11578 dprintf("imsm: %i spares are available.\n\n",
11579 spares
->array
.spare_disks
);
11581 dev
= spares
->devs
;
11582 for (i
= 0; i
< delta_disks
; i
++) {
11587 u
->new_disks
[i
] = makedev(dev
->disk
.major
,
11589 dl
= get_disk_super(super
, dev
->disk
.major
, dev
->disk
.minor
);
11590 dl
->index
= mpb
->num_disks
;
11598 sysfs_free(spares
);
11600 dprintf("imsm: reshape update preparation :");
11601 if (i
== delta_disks
) {
11602 dprintf_cont(" OK\n");
11604 return update_memory_size
;
11607 dprintf_cont(" Error\n");
11612 /******************************************************************************
11613 * function: imsm_create_metadata_update_for_size_change()
11614 * Creates update for IMSM array for array size change.
11616 ******************************************************************************/
11617 static int imsm_create_metadata_update_for_size_change(
11618 struct supertype
*st
,
11619 struct geo_params
*geo
,
11620 struct imsm_update_size_change
**updatep
)
11622 struct intel_super
*super
= st
->sb
;
11623 int update_memory_size
;
11624 struct imsm_update_size_change
*u
;
11626 dprintf("(enter) New size = %llu\n", geo
->size
);
11628 /* size of all update data without anchor */
11629 update_memory_size
= sizeof(struct imsm_update_size_change
);
11631 u
= xcalloc(1, update_memory_size
);
11632 u
->type
= update_size_change
;
11633 u
->subdev
= super
->current_vol
;
11634 u
->new_size
= geo
->size
;
11636 dprintf("imsm: reshape update preparation : OK\n");
11639 return update_memory_size
;
11642 /******************************************************************************
11643 * function: imsm_create_metadata_update_for_migration()
11644 * Creates update for IMSM array.
11646 ******************************************************************************/
11647 static int imsm_create_metadata_update_for_migration(
11648 struct supertype
*st
,
11649 struct geo_params
*geo
,
11650 struct imsm_update_reshape_migration
**updatep
)
11652 struct intel_super
*super
= st
->sb
;
11653 int update_memory_size
;
11654 int current_chunk_size
;
11655 struct imsm_update_reshape_migration
*u
;
11656 struct imsm_dev
*dev
= get_imsm_dev(super
, super
->current_vol
);
11657 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
11658 int previous_level
= -1;
11660 dprintf("(enter) New Level = %i\n", geo
->level
);
11662 /* size of all update data without anchor */
11663 update_memory_size
= sizeof(struct imsm_update_reshape_migration
);
11665 u
= xcalloc(1, update_memory_size
);
11666 u
->type
= update_reshape_migration
;
11667 u
->subdev
= super
->current_vol
;
11668 u
->new_level
= geo
->level
;
11669 u
->new_layout
= geo
->layout
;
11670 u
->new_raid_disks
= u
->old_raid_disks
= geo
->raid_disks
;
11671 u
->new_disks
[0] = -1;
11672 u
->new_chunksize
= -1;
11674 current_chunk_size
= __le16_to_cpu(map
->blocks_per_strip
) / 2;
11676 if (geo
->chunksize
!= current_chunk_size
) {
11677 u
->new_chunksize
= geo
->chunksize
/ 1024;
11678 dprintf("imsm: chunk size change from %i to %i\n",
11679 current_chunk_size
, u
->new_chunksize
);
11681 previous_level
= map
->raid_level
;
11683 if (geo
->level
== 5 && previous_level
== 0) {
11684 struct mdinfo
*spares
= NULL
;
11686 u
->new_raid_disks
++;
11687 spares
= get_spares_for_grow(st
);
11688 if (spares
== NULL
|| spares
->array
.spare_disks
< 1) {
11690 sysfs_free(spares
);
11691 update_memory_size
= 0;
11692 pr_err("cannot get spare device for requested migration\n");
11695 sysfs_free(spares
);
11697 dprintf("imsm: reshape update preparation : OK\n");
11700 return update_memory_size
;
11703 static void imsm_update_metadata_locally(struct supertype
*st
,
11704 void *buf
, int len
)
11706 struct metadata_update mu
;
11711 mu
.space_list
= NULL
;
11713 if (imsm_prepare_update(st
, &mu
))
11714 imsm_process_update(st
, &mu
);
11716 while (mu
.space_list
) {
11717 void **space
= mu
.space_list
;
11718 mu
.space_list
= *space
;
11724 * imsm_analyze_expand() - check expand properties and calculate new size.
11725 * @st: imsm supertype.
11726 * @geo: new geometry params.
11727 * @array: array info.
11728 * @direction: reshape direction.
11730 * Obtain free space after the &array and verify if expand to requested size is
11731 * possible. If geo->size is set to %MAX_SIZE, assume that max free size is
11735 * On success %IMSM_STATUS_OK is returned, geo->size and geo->raid_disks are
11737 * On error, %IMSM_STATUS_ERROR is returned.
11739 static imsm_status_t
imsm_analyze_expand(struct supertype
*st
,
11740 struct geo_params
*geo
,
11741 struct mdinfo
*array
,
11744 struct intel_super
*super
= st
->sb
;
11745 struct imsm_dev
*dev
= get_imsm_dev(super
, super
->current_vol
);
11746 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
11747 int data_disks
= imsm_num_data_members(map
);
11749 unsigned long long current_size
;
11750 unsigned long long free_size
;
11751 unsigned long long new_size
;
11752 unsigned long long max_size
;
11754 const int chunk_kib
= geo
->chunksize
/ 1024;
11757 if (direction
== ROLLBACK_METADATA_CHANGES
) {
11759 * Accept size for rollback only.
11761 new_size
= geo
->size
* 2;
11765 if (data_disks
== 0) {
11766 pr_err("imsm: Cannot retrieve data disks.\n");
11767 return IMSM_STATUS_ERROR
;
11769 current_size
= array
->custom_array_size
/ data_disks
;
11771 rv
= imsm_get_free_size(super
, dev
->vol
.map
->num_members
, 0, chunk_kib
, &free_size
, true);
11772 if (rv
!= IMSM_STATUS_OK
) {
11773 pr_err("imsm: Cannot find free space for expand.\n");
11774 return IMSM_STATUS_ERROR
;
11776 max_size
= round_member_size_to_mb(free_size
+ current_size
);
11778 if (geo
->size
== MAX_SIZE
)
11779 new_size
= max_size
;
11781 new_size
= round_member_size_to_mb(geo
->size
* 2);
11783 if (new_size
== 0) {
11784 pr_err("imsm: Rounded requested size is 0.\n");
11785 return IMSM_STATUS_ERROR
;
11788 if (new_size
> max_size
) {
11789 pr_err("imsm: Rounded requested size (%llu) is larger than free space available (%llu).\n",
11790 new_size
, max_size
);
11791 return IMSM_STATUS_ERROR
;
11794 if (new_size
== current_size
) {
11795 pr_err("imsm: Rounded requested size (%llu) is same as current size (%llu).\n",
11796 new_size
, current_size
);
11797 return IMSM_STATUS_ERROR
;
11800 if (new_size
< current_size
) {
11801 pr_err("imsm: Size reduction is not supported, rounded requested size (%llu) is smaller than current (%llu).\n",
11802 new_size
, current_size
);
11803 return IMSM_STATUS_ERROR
;
11807 dprintf("imsm: New size per member is %llu.\n", new_size
);
11808 geo
->size
= data_disks
* new_size
;
11809 geo
->raid_disks
= dev
->vol
.map
->num_members
;
11810 return IMSM_STATUS_OK
;
11813 /***************************************************************************
11814 * Function: imsm_analyze_change
11815 * Description: Function analyze change for single volume
11816 * and validate if transition is supported
11817 * Parameters: Geometry parameters, supertype structure,
11818 * metadata change direction (apply/rollback)
11819 * Returns: Operation type code on success, -1 if fail
11820 ****************************************************************************/
11821 enum imsm_reshape_type
imsm_analyze_change(struct supertype
*st
,
11822 struct geo_params
*geo
,
11825 struct mdinfo info
;
11827 int check_devs
= 0;
11829 /* number of added/removed disks in operation result */
11830 int devNumChange
= 0;
11831 /* imsm compatible layout value for array geometry verification */
11832 int imsm_layout
= -1;
11835 getinfo_super_imsm_volume(st
, &info
, NULL
);
11836 if (geo
->level
!= info
.array
.level
&& geo
->level
>= 0 &&
11837 geo
->level
!= UnSet
) {
11838 switch (info
.array
.level
) {
11840 if (geo
->level
== 5) {
11841 change
= CH_MIGRATION
;
11842 if (geo
->layout
!= ALGORITHM_LEFT_ASYMMETRIC
) {
11843 pr_err("Error. Requested Layout not supported (left-asymmetric layout is supported only)!\n");
11845 goto analyse_change_exit
;
11847 imsm_layout
= geo
->layout
;
11849 devNumChange
= 1; /* parity disk added */
11850 } else if (geo
->level
== 10) {
11851 change
= CH_TAKEOVER
;
11853 devNumChange
= 2; /* two mirrors added */
11854 imsm_layout
= 0x102; /* imsm supported layout */
11859 if (geo
->level
== 0) {
11860 change
= CH_TAKEOVER
;
11862 devNumChange
= -(geo
->raid_disks
/2);
11863 imsm_layout
= 0; /* imsm raid0 layout */
11867 if (change
== -1) {
11868 pr_err("Error. Level Migration from %d to %d not supported!\n",
11869 info
.array
.level
, geo
->level
);
11870 goto analyse_change_exit
;
11873 geo
->level
= info
.array
.level
;
11875 if (geo
->layout
!= info
.array
.layout
&&
11876 (geo
->layout
!= UnSet
&& geo
->layout
!= -1)) {
11877 change
= CH_MIGRATION
;
11878 if (info
.array
.layout
== 0 && info
.array
.level
== 5 &&
11879 geo
->layout
== 5) {
11880 /* reshape 5 -> 4 */
11881 } else if (info
.array
.layout
== 5 && info
.array
.level
== 5 &&
11882 geo
->layout
== 0) {
11883 /* reshape 4 -> 5 */
11887 pr_err("Error. Layout Migration from %d to %d not supported!\n",
11888 info
.array
.layout
, geo
->layout
);
11890 goto analyse_change_exit
;
11893 geo
->layout
= info
.array
.layout
;
11894 if (imsm_layout
== -1)
11895 imsm_layout
= info
.array
.layout
;
11898 if (geo
->chunksize
> 0 && geo
->chunksize
!= UnSet
&&
11899 geo
->chunksize
!= info
.array
.chunk_size
) {
11900 if (info
.array
.level
== 10) {
11901 pr_err("Error. Chunk size change for RAID 10 is not supported.\n");
11903 goto analyse_change_exit
;
11904 } else if (info
.component_size
% (geo
->chunksize
/512)) {
11905 pr_err("New chunk size (%dK) does not evenly divide device size (%lluk). Aborting...\n",
11906 geo
->chunksize
/1024, info
.component_size
/2);
11908 goto analyse_change_exit
;
11910 change
= CH_MIGRATION
;
11912 geo
->chunksize
= info
.array
.chunk_size
;
11915 if (geo
->size
> 0) {
11916 if (change
!= -1) {
11917 pr_err("Error. Size change should be the only one at a time.\n");
11919 goto analyse_change_exit
;
11922 rv
= imsm_analyze_expand(st
, geo
, &info
, direction
);
11923 if (rv
!= IMSM_STATUS_OK
)
11924 goto analyse_change_exit
;
11925 change
= CH_ARRAY_SIZE
;
11928 chunk
= geo
->chunksize
/ 1024;
11929 if (!validate_geometry_imsm(st
,
11932 geo
->raid_disks
+ devNumChange
,
11934 geo
->size
, INVALID_SECTORS
,
11935 0, 0, info
.consistency_policy
, 1))
11939 struct intel_super
*super
= st
->sb
;
11940 struct imsm_super
*mpb
= super
->anchor
;
11942 if (mpb
->num_raid_devs
> 1) {
11943 pr_err("Error. Cannot perform operation on %s- for this operation "
11944 "it MUST be single array in container\n", geo
->dev_name
);
11949 analyse_change_exit
:
11950 if (direction
== ROLLBACK_METADATA_CHANGES
&&
11951 (change
== CH_MIGRATION
|| change
== CH_TAKEOVER
)) {
11952 dprintf("imsm: Metadata changes rollback is not supported for migration and takeover operations.\n");
11958 int imsm_takeover(struct supertype
*st
, struct geo_params
*geo
)
11960 struct intel_super
*super
= st
->sb
;
11961 struct imsm_update_takeover
*u
;
11963 u
= xmalloc(sizeof(struct imsm_update_takeover
));
11965 u
->type
= update_takeover
;
11966 u
->subarray
= super
->current_vol
;
11968 /* 10->0 transition */
11969 if (geo
->level
== 0)
11970 u
->direction
= R10_TO_R0
;
11972 /* 0->10 transition */
11973 if (geo
->level
== 10)
11974 u
->direction
= R0_TO_R10
;
11976 /* update metadata locally */
11977 imsm_update_metadata_locally(st
, u
,
11978 sizeof(struct imsm_update_takeover
));
11979 /* and possibly remotely */
11980 if (st
->update_tail
)
11981 append_metadata_update(st
, u
,
11982 sizeof(struct imsm_update_takeover
));
11989 /* Flush size update if size calculated by num_data_stripes is higher than
11990 * imsm_dev_size to eliminate differences during reshape.
11991 * Mdmon will recalculate them correctly.
11992 * If subarray index is not set then check whole container.
11994 * 0 - no error occurred
11995 * 1 - error detected
11997 static int imsm_fix_size_mismatch(struct supertype
*st
, int subarray_index
)
11999 struct intel_super
*super
= st
->sb
;
12000 int tmp
= super
->current_vol
;
12004 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
12005 if (subarray_index
>= 0 && i
!= subarray_index
)
12007 super
->current_vol
= i
;
12008 struct imsm_dev
*dev
= get_imsm_dev(super
, super
->current_vol
);
12009 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
12010 unsigned int disc_count
= imsm_num_data_members(map
);
12011 struct geo_params geo
;
12012 struct imsm_update_size_change
*update
;
12013 unsigned long long calc_size
= per_dev_array_size(map
) * disc_count
;
12014 unsigned long long d_size
= imsm_dev_size(dev
);
12017 if (calc_size
== d_size
)
12020 /* There is a difference, confirm that imsm_dev_size is
12021 * smaller and push update.
12023 if (d_size
> calc_size
) {
12024 pr_err("imsm: dev size of subarray %d is incorrect\n",
12028 memset(&geo
, 0, sizeof(struct geo_params
));
12030 u_size
= imsm_create_metadata_update_for_size_change(st
, &geo
,
12032 imsm_update_metadata_locally(st
, update
, u_size
);
12033 if (st
->update_tail
) {
12034 append_metadata_update(st
, update
, u_size
);
12035 flush_metadata_updates(st
);
12036 st
->update_tail
= &st
->updates
;
12038 imsm_sync_metadata(st
);
12044 super
->current_vol
= tmp
;
12048 static int imsm_reshape_super(struct supertype
*st
, unsigned long long size
,
12050 int layout
, int chunksize
, int raid_disks
,
12051 int delta_disks
, char *backup
, char *dev
,
12052 int direction
, int verbose
)
12055 struct geo_params geo
;
12057 dprintf("(enter)\n");
12059 memset(&geo
, 0, sizeof(struct geo_params
));
12061 geo
.dev_name
= dev
;
12062 strcpy(geo
.devnm
, st
->devnm
);
12065 geo
.layout
= layout
;
12066 geo
.chunksize
= chunksize
;
12067 geo
.raid_disks
= raid_disks
;
12068 if (delta_disks
!= UnSet
)
12069 geo
.raid_disks
+= delta_disks
;
12071 dprintf("for level : %i\n", geo
.level
);
12072 dprintf("for raid_disks : %i\n", geo
.raid_disks
);
12074 if (strcmp(st
->container_devnm
, st
->devnm
) == 0) {
12075 /* On container level we can only increase number of devices. */
12076 dprintf("imsm: info: Container operation\n");
12077 int old_raid_disks
= 0;
12079 if (imsm_reshape_is_allowed_on_container(
12080 st
, &geo
, &old_raid_disks
, direction
)) {
12081 struct imsm_update_reshape
*u
= NULL
;
12084 if (imsm_fix_size_mismatch(st
, -1)) {
12085 dprintf("imsm: Cannot fix size mismatch\n");
12086 goto exit_imsm_reshape_super
;
12089 len
= imsm_create_metadata_update_for_reshape(
12090 st
, &geo
, old_raid_disks
, &u
);
12093 dprintf("imsm: Cannot prepare update\n");
12094 goto exit_imsm_reshape_super
;
12098 /* update metadata locally */
12099 imsm_update_metadata_locally(st
, u
, len
);
12100 /* and possibly remotely */
12101 if (st
->update_tail
)
12102 append_metadata_update(st
, u
, len
);
12107 pr_err("(imsm) Operation is not allowed on this container\n");
12110 /* On volume level we support following operations
12111 * - takeover: raid10 -> raid0; raid0 -> raid10
12112 * - chunk size migration
12113 * - migration: raid5 -> raid0; raid0 -> raid5
12115 struct intel_super
*super
= st
->sb
;
12116 struct intel_dev
*dev
= super
->devlist
;
12118 dprintf("imsm: info: Volume operation\n");
12119 /* find requested device */
12122 imsm_find_array_devnm_by_subdev(
12123 dev
->index
, st
->container_devnm
);
12124 if (devnm
&& strcmp(devnm
, geo
.devnm
) == 0)
12129 pr_err("Cannot find %s (%s) subarray\n",
12130 geo
.dev_name
, geo
.devnm
);
12131 goto exit_imsm_reshape_super
;
12133 super
->current_vol
= dev
->index
;
12134 change
= imsm_analyze_change(st
, &geo
, direction
);
12137 ret_val
= imsm_takeover(st
, &geo
);
12139 case CH_MIGRATION
: {
12140 struct imsm_update_reshape_migration
*u
= NULL
;
12142 imsm_create_metadata_update_for_migration(
12145 dprintf("imsm: Cannot prepare update\n");
12149 /* update metadata locally */
12150 imsm_update_metadata_locally(st
, u
, len
);
12151 /* and possibly remotely */
12152 if (st
->update_tail
)
12153 append_metadata_update(st
, u
, len
);
12158 case CH_ARRAY_SIZE
: {
12159 struct imsm_update_size_change
*u
= NULL
;
12161 imsm_create_metadata_update_for_size_change(
12164 dprintf("imsm: Cannot prepare update\n");
12168 /* update metadata locally */
12169 imsm_update_metadata_locally(st
, u
, len
);
12170 /* and possibly remotely */
12171 if (st
->update_tail
)
12172 append_metadata_update(st
, u
, len
);
12182 exit_imsm_reshape_super
:
12183 dprintf("imsm: reshape_super Exit code = %i\n", ret_val
);
12187 #define COMPLETED_OK 0
12188 #define COMPLETED_NONE 1
12189 #define COMPLETED_DELAYED 2
12191 static int read_completed(int fd
, unsigned long long *val
)
12194 char buf
[SYSFS_MAX_BUF_SIZE
];
12196 ret
= sysfs_fd_get_str(fd
, buf
, sizeof(buf
));
12200 ret
= COMPLETED_OK
;
12201 if (str_is_none(buf
) == true) {
12202 ret
= COMPLETED_NONE
;
12203 } else if (strncmp(buf
, "delayed", 7) == 0) {
12204 ret
= COMPLETED_DELAYED
;
12207 *val
= strtoull(buf
, &ep
, 0);
12208 if (ep
== buf
|| (*ep
!= 0 && *ep
!= '\n' && *ep
!= ' '))
12214 /*******************************************************************************
12215 * Function: wait_for_reshape_imsm
12216 * Description: Function writes new sync_max value and waits until
12217 * reshape process reach new position
12219 * sra : general array info
12220 * ndata : number of disks in new array's layout
12223 * 1 : there is no reshape in progress,
12225 ******************************************************************************/
12226 int wait_for_reshape_imsm(struct mdinfo
*sra
, int ndata
)
12228 int fd
= sysfs_get_fd(sra
, NULL
, "sync_completed");
12230 unsigned long long completed
;
12231 /* to_complete : new sync_max position */
12232 unsigned long long to_complete
= sra
->reshape_progress
;
12233 unsigned long long position_to_set
= to_complete
/ ndata
;
12235 if (!is_fd_valid(fd
)) {
12236 dprintf("cannot open reshape_position\n");
12241 if (sysfs_fd_get_ll(fd
, &completed
) < 0) {
12243 dprintf("cannot read reshape_position (no reshape in progres)\n");
12247 sleep_for(0, MSEC_TO_NSEC(30), true);
12252 if (completed
> position_to_set
) {
12253 dprintf("wrong next position to set %llu (%llu)\n",
12254 to_complete
, position_to_set
);
12258 dprintf("Position set: %llu\n", position_to_set
);
12259 if (sysfs_set_num(sra
, NULL
, "sync_max",
12260 position_to_set
) != 0) {
12261 dprintf("cannot set reshape position to %llu\n",
12269 char action
[SYSFS_MAX_BUF_SIZE
];
12270 int timeout
= 3000;
12272 sysfs_wait(fd
, &timeout
);
12273 if (sysfs_get_str(sra
, NULL
, "sync_action",
12274 action
, sizeof(action
)) > 0 &&
12275 strncmp(action
, "reshape", 7) != 0) {
12276 if (strncmp(action
, "idle", 4) == 0)
12282 rc
= read_completed(fd
, &completed
);
12284 dprintf("cannot read reshape_position (in loop)\n");
12287 } else if (rc
== COMPLETED_NONE
)
12289 } while (completed
< position_to_set
);
12295 /*******************************************************************************
12296 * Function: check_degradation_change
12297 * Description: Check that array hasn't become failed.
12299 * info : for sysfs access
12300 * sources : source disks descriptors
12301 * degraded: previous degradation level
12303 * degradation level
12304 ******************************************************************************/
12305 int check_degradation_change(struct mdinfo
*info
,
12309 unsigned long long new_degraded
;
12312 rv
= sysfs_get_ll(info
, NULL
, "degraded", &new_degraded
);
12313 if (rv
== -1 || (new_degraded
!= (unsigned long long)degraded
)) {
12314 /* check each device to ensure it is still working */
12317 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
12318 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
12320 if (sd
->disk
.state
& (1<<MD_DISK_SYNC
)) {
12321 char sbuf
[SYSFS_MAX_BUF_SIZE
];
12322 int raid_disk
= sd
->disk
.raid_disk
;
12324 if (sysfs_get_str(info
,
12325 sd
, "state", sbuf
, sizeof(sbuf
)) < 0 ||
12326 strstr(sbuf
, "faulty") ||
12327 strstr(sbuf
, "in_sync") == NULL
) {
12328 /* this device is dead */
12329 sd
->disk
.state
= (1<<MD_DISK_FAULTY
);
12330 if (raid_disk
>= 0)
12331 close_fd(&sources
[raid_disk
]);
12338 return new_degraded
;
12341 /*******************************************************************************
12342 * Function: imsm_manage_reshape
12343 * Description: Function finds array under reshape and it manages reshape
12344 * process. It creates stripes backups (if required) and sets
12347 * afd : Backup handle (nattive) - not used
12348 * sra : general array info
12349 * reshape : reshape parameters - not used
12350 * st : supertype structure
12351 * blocks : size of critical section [blocks]
12352 * fds : table of source device descriptor
12353 * offsets : start of array (offest per devices)
12355 * destfd : table of destination device descriptor
12356 * destoffsets : table of destination offsets (per device)
12358 * 1 : success, reshape is done
12360 ******************************************************************************/
12361 static int imsm_manage_reshape(
12362 int afd
, struct mdinfo
*sra
, struct reshape
*reshape
,
12363 struct supertype
*st
, unsigned long backup_blocks
,
12364 int *fds
, unsigned long long *offsets
,
12365 int dests
, int *destfd
, unsigned long long *destoffsets
)
12368 struct intel_super
*super
= st
->sb
;
12369 struct intel_dev
*dv
;
12370 unsigned int sector_size
= super
->sector_size
;
12371 struct imsm_dev
*dev
= NULL
;
12372 struct imsm_map
*map_src
, *map_dest
;
12373 int migr_vol_qan
= 0;
12374 int ndata
, odata
; /* [bytes] */
12375 int chunk
; /* [bytes] */
12376 struct migr_record
*migr_rec
;
12378 unsigned int buf_size
; /* [bytes] */
12379 unsigned long long max_position
; /* array size [bytes] */
12380 unsigned long long next_step
; /* [blocks]/[bytes] */
12381 unsigned long long old_data_stripe_length
;
12382 unsigned long long start_src
; /* [bytes] */
12383 unsigned long long start
; /* [bytes] */
12384 unsigned long long start_buf_shift
; /* [bytes] */
12386 int source_layout
= 0;
12387 int subarray_index
= -1;
12392 if (!fds
|| !offsets
)
12395 /* Find volume during the reshape */
12396 for (dv
= super
->devlist
; dv
; dv
= dv
->next
) {
12397 if (dv
->dev
->vol
.migr_type
== MIGR_GEN_MIGR
&&
12398 dv
->dev
->vol
.migr_state
== 1) {
12401 subarray_index
= dv
->index
;
12404 /* Only one volume can migrate at the same time */
12405 if (migr_vol_qan
!= 1) {
12406 pr_err("%s", migr_vol_qan
?
12407 "Number of migrating volumes greater than 1\n" :
12408 "There is no volume during migrationg\n");
12412 map_dest
= get_imsm_map(dev
, MAP_0
);
12413 map_src
= get_imsm_map(dev
, MAP_1
);
12414 if (map_src
== NULL
)
12417 ndata
= imsm_num_data_members(map_dest
);
12418 odata
= imsm_num_data_members(map_src
);
12420 chunk
= __le16_to_cpu(map_src
->blocks_per_strip
) * 512;
12421 old_data_stripe_length
= odata
* chunk
;
12423 migr_rec
= super
->migr_rec
;
12425 /* initialize migration record for start condition */
12426 if (sra
->reshape_progress
== 0)
12427 init_migr_record_imsm(st
, dev
, sra
);
12429 if (__le32_to_cpu(migr_rec
->rec_status
) != UNIT_SRC_NORMAL
) {
12430 dprintf("imsm: cannot restart migration when data are present in copy area.\n");
12433 /* Save checkpoint to update migration record for current
12434 * reshape position (in md). It can be farther than current
12435 * reshape position in metadata.
12437 if (save_checkpoint_imsm(st
, sra
, UNIT_SRC_NORMAL
) == 1) {
12438 /* ignore error == 2, this can mean end of reshape here
12440 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL, initial save)\n");
12445 /* size for data */
12446 buf_size
= __le32_to_cpu(migr_rec
->blocks_per_unit
) * 512;
12447 /* extend buffer size for parity disk */
12448 buf_size
+= __le32_to_cpu(migr_rec
->dest_depth_per_unit
) * 512;
12449 /* add space for stripe alignment */
12450 buf_size
+= old_data_stripe_length
;
12451 if (posix_memalign((void **)&buf
, MAX_SECTOR_SIZE
, buf_size
)) {
12452 dprintf("imsm: Cannot allocate checkpoint buffer\n");
12456 max_position
= sra
->component_size
* ndata
;
12457 source_layout
= imsm_level_to_layout(map_src
->raid_level
);
12459 while (current_migr_unit(migr_rec
) <
12460 get_num_migr_units(migr_rec
)) {
12461 /* current reshape position [blocks] */
12462 unsigned long long current_position
=
12463 __le32_to_cpu(migr_rec
->blocks_per_unit
)
12464 * current_migr_unit(migr_rec
);
12465 unsigned long long border
;
12467 /* Check that array hasn't become failed.
12469 degraded
= check_degradation_change(sra
, fds
, degraded
);
12470 if (degraded
> 1) {
12471 dprintf("imsm: Abort reshape due to degradation level (%i)\n", degraded
);
12475 next_step
= __le32_to_cpu(migr_rec
->blocks_per_unit
);
12477 if ((current_position
+ next_step
) > max_position
)
12478 next_step
= max_position
- current_position
;
12480 start
= current_position
* 512;
12482 /* align reading start to old geometry */
12483 start_buf_shift
= start
% old_data_stripe_length
;
12484 start_src
= start
- start_buf_shift
;
12486 border
= (start_src
/ odata
) - (start
/ ndata
);
12488 if (border
<= __le32_to_cpu(migr_rec
->dest_depth_per_unit
)) {
12489 /* save critical stripes to buf
12490 * start - start address of current unit
12491 * to backup [bytes]
12492 * start_src - start address of current unit
12493 * to backup alligned to source array
12496 unsigned long long next_step_filler
;
12497 unsigned long long copy_length
= next_step
* 512;
12499 /* allign copy area length to stripe in old geometry */
12500 next_step_filler
= ((copy_length
+ start_buf_shift
)
12501 % old_data_stripe_length
);
12502 if (next_step_filler
)
12503 next_step_filler
= (old_data_stripe_length
12504 - next_step_filler
);
12505 dprintf("save_stripes() parameters: start = %llu,\tstart_src = %llu,\tnext_step*512 = %llu,\tstart_in_buf_shift = %llu,\tnext_step_filler = %llu\n",
12506 start
, start_src
, copy_length
,
12507 start_buf_shift
, next_step_filler
);
12509 if (save_stripes(fds
, offsets
, map_src
->num_members
,
12510 chunk
, map_src
->raid_level
,
12511 source_layout
, 0, NULL
, start_src
,
12513 next_step_filler
+ start_buf_shift
,
12515 dprintf("imsm: Cannot save stripes to buffer\n");
12518 /* Convert data to destination format and store it
12519 * in backup general migration area
12521 if (save_backup_imsm(st
, dev
, sra
,
12522 buf
+ start_buf_shift
, copy_length
)) {
12523 dprintf("imsm: Cannot save stripes to target devices\n");
12526 if (save_checkpoint_imsm(st
, sra
,
12527 UNIT_SRC_IN_CP_AREA
)) {
12528 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_IN_CP_AREA)\n");
12532 /* set next step to use whole border area */
12533 border
/= next_step
;
12535 next_step
*= border
;
12537 /* When data backed up, checkpoint stored,
12538 * kick the kernel to reshape unit of data
12540 next_step
= next_step
+ sra
->reshape_progress
;
12541 /* limit next step to array max position */
12542 if (next_step
> max_position
)
12543 next_step
= max_position
;
12544 sysfs_set_num(sra
, NULL
, "suspend_lo", sra
->reshape_progress
);
12545 sysfs_set_num(sra
, NULL
, "suspend_hi", next_step
);
12546 sra
->reshape_progress
= next_step
;
12548 /* wait until reshape finish */
12549 if (wait_for_reshape_imsm(sra
, ndata
)) {
12550 dprintf("wait_for_reshape_imsm returned error!\n");
12556 if (save_checkpoint_imsm(st
, sra
, UNIT_SRC_NORMAL
) == 1) {
12557 /* ignore error == 2, this can mean end of reshape here
12559 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL)\n");
12565 /* clear migr_rec on disks after successful migration */
12568 memset(super
->migr_rec_buf
, 0, MIGR_REC_BUF_SECTORS
*MAX_SECTOR_SIZE
);
12569 for (d
= super
->disks
; d
; d
= d
->next
) {
12570 if (d
->index
< 0 || is_failed(&d
->disk
))
12572 unsigned long long dsize
;
12574 get_dev_size(d
->fd
, NULL
, &dsize
);
12575 if (lseek64(d
->fd
, dsize
- MIGR_REC_SECTOR_POSITION
*sector_size
,
12577 if ((unsigned int)write(d
->fd
, super
->migr_rec_buf
,
12578 MIGR_REC_BUF_SECTORS
*sector_size
) !=
12579 MIGR_REC_BUF_SECTORS
*sector_size
)
12580 perror("Write migr_rec failed");
12584 /* return '1' if done */
12587 /* After the reshape eliminate size mismatch in metadata.
12588 * Don't update md/component_size here, volume hasn't
12589 * to take whole space. It is allowed by kernel.
12590 * md/component_size will be set propoperly after next assembly.
12592 imsm_fix_size_mismatch(st
, subarray_index
);
12596 /* See Grow.c: abort_reshape() for further explanation */
12597 sysfs_set_num(sra
, NULL
, "suspend_lo", 0x7FFFFFFFFFFFFFFFULL
);
12598 sysfs_set_num(sra
, NULL
, "suspend_hi", 0);
12599 sysfs_set_num(sra
, NULL
, "suspend_lo", 0);
12604 /*******************************************************************************
12605 * Function: calculate_bitmap_min_chunksize
12606 * Description: Calculates the minimal valid bitmap chunk size
12608 * max_bits : indicate how many bits can be used for the bitmap
12609 * data_area_size : the size of the data area covered by the bitmap
12612 * The bitmap chunk size
12613 ******************************************************************************/
12614 static unsigned long long
12615 calculate_bitmap_min_chunksize(unsigned long long max_bits
,
12616 unsigned long long data_area_size
)
12618 unsigned long long min_chunk
=
12619 4096; /* sub-page chunks don't work yet.. */
12620 unsigned long long bits
= data_area_size
/ min_chunk
+ 1;
12622 while (bits
> max_bits
) {
12624 bits
= (bits
+ 1) / 2;
12629 /*******************************************************************************
12630 * Function: calculate_bitmap_chunksize
12631 * Description: Calculates the bitmap chunk size for the given device
12633 * st : supertype information
12634 * dev : device for the bitmap
12637 * The bitmap chunk size
12638 ******************************************************************************/
12639 static unsigned long long calculate_bitmap_chunksize(struct supertype
*st
,
12640 struct imsm_dev
*dev
)
12642 struct intel_super
*super
= st
->sb
;
12643 unsigned long long min_chunksize
;
12644 unsigned long long result
= IMSM_DEFAULT_BITMAP_CHUNKSIZE
;
12645 size_t dev_size
= imsm_dev_size(dev
);
12647 min_chunksize
= calculate_bitmap_min_chunksize(
12648 IMSM_BITMAP_AREA_SIZE
* super
->sector_size
, dev_size
);
12650 if (result
< min_chunksize
)
12651 result
= min_chunksize
;
12656 /*******************************************************************************
12657 * Function: init_bitmap_header
12658 * Description: Initialize the bitmap header structure
12660 * st : supertype information
12661 * bms : bitmap header struct to initialize
12662 * dev : device for the bitmap
12667 ******************************************************************************/
12668 static int init_bitmap_header(struct supertype
*st
, struct bitmap_super_s
*bms
,
12669 struct imsm_dev
*dev
)
12676 bms
->magic
= __cpu_to_le32(BITMAP_MAGIC
);
12677 bms
->version
= __cpu_to_le32(BITMAP_MAJOR_HI
);
12678 bms
->daemon_sleep
= __cpu_to_le32(IMSM_DEFAULT_BITMAP_DAEMON_SLEEP
);
12679 bms
->sync_size
= __cpu_to_le64(IMSM_BITMAP_AREA_SIZE
);
12680 bms
->write_behind
= __cpu_to_le32(0);
12682 uuid_from_super_imsm(st
, vol_uuid
);
12683 memcpy(bms
->uuid
, vol_uuid
, 16);
12685 bms
->chunksize
= calculate_bitmap_chunksize(st
, dev
);
12690 /*******************************************************************************
12691 * Function: validate_internal_bitmap_for_drive
12692 * Description: Verify if the bitmap header for a given drive.
12694 * st : supertype information
12695 * offset : The offset from the beginning of the drive where to look for
12696 * the bitmap header.
12697 * d : the drive info
12702 ******************************************************************************/
12703 static int validate_internal_bitmap_for_drive(struct supertype
*st
,
12704 unsigned long long offset
,
12707 struct intel_super
*super
= st
->sb
;
12710 bitmap_super_t
*bms
;
12718 if (posix_memalign(&read_buf
, MAX_SECTOR_SIZE
, IMSM_BITMAP_HEADER_SIZE
))
12722 if (!is_fd_valid(fd
)) {
12723 fd
= open(d
->devname
, O_RDONLY
, 0);
12725 if (!is_fd_valid(fd
)) {
12726 dprintf("cannot open the device %s\n", d
->devname
);
12731 if (lseek64(fd
, offset
* super
->sector_size
, SEEK_SET
) < 0)
12733 if (read(fd
, read_buf
, IMSM_BITMAP_HEADER_SIZE
) !=
12734 IMSM_BITMAP_HEADER_SIZE
)
12737 uuid_from_super_imsm(st
, vol_uuid
);
12740 if ((bms
->magic
!= __cpu_to_le32(BITMAP_MAGIC
)) ||
12741 (bms
->version
!= __cpu_to_le32(BITMAP_MAJOR_HI
)) ||
12742 (!same_uuid((int *)bms
->uuid
, vol_uuid
, st
->ss
->swapuuid
))) {
12743 dprintf("wrong bitmap header detected\n");
12749 if (!is_fd_valid(d
->fd
))
12758 /*******************************************************************************
12759 * Function: validate_internal_bitmap_imsm
12760 * Description: Verify if the bitmap header is in place and with proper data.
12762 * st : supertype information
12765 * 0 : success or device w/o RWH_BITMAP
12767 ******************************************************************************/
12768 static int validate_internal_bitmap_imsm(struct supertype
*st
)
12770 struct intel_super
*super
= st
->sb
;
12771 struct imsm_dev
*dev
= get_imsm_dev(super
, super
->current_vol
);
12772 unsigned long long offset
;
12775 if (dev
->rwh_policy
!= RWH_BITMAP
)
12778 offset
= get_bitmap_header_sector(super
, super
->current_vol
);
12779 for (d
= super
->disks
; d
; d
= d
->next
) {
12780 if (d
->index
< 0 || is_failed(&d
->disk
))
12783 if (validate_internal_bitmap_for_drive(st
, offset
, d
)) {
12784 pr_err("imsm: bitmap validation failed\n");
12791 /*******************************************************************************
12792 * Function: add_internal_bitmap_imsm
12793 * Description: Mark the volume to use the bitmap and updates the chunk size value.
12795 * st : supertype information
12796 * chunkp : bitmap chunk size
12797 * delay : not used for imsm
12798 * write_behind : not used for imsm
12799 * size : not used for imsm
12800 * may_change : not used for imsm
12801 * amajor : not used for imsm
12806 ******************************************************************************/
12807 static int add_internal_bitmap_imsm(struct supertype
*st
, int *chunkp
,
12808 int delay
, int write_behind
,
12809 unsigned long long size
, int may_change
,
12812 struct intel_super
*super
= st
->sb
;
12813 int vol_idx
= super
->current_vol
;
12814 struct imsm_dev
*dev
;
12816 if (!super
->devlist
|| vol_idx
== -1 || !chunkp
)
12819 dev
= get_imsm_dev(super
, vol_idx
);
12820 dev
->rwh_policy
= RWH_BITMAP
;
12821 *chunkp
= calculate_bitmap_chunksize(st
, dev
);
12825 /*******************************************************************************
12826 * Function: locate_bitmap_imsm
12827 * Description: Seek 'fd' to start of write-intent-bitmap.
12829 * st : supertype information
12830 * fd : file descriptor for the device
12831 * node_num : not used for imsm
12836 ******************************************************************************/
12837 static int locate_bitmap_imsm(struct supertype
*st
, int fd
, int node_num
)
12839 struct intel_super
*super
= st
->sb
;
12840 unsigned long long offset
;
12841 int vol_idx
= super
->current_vol
;
12843 if (!super
->devlist
|| vol_idx
== -1)
12846 offset
= get_bitmap_header_sector(super
, super
->current_vol
);
12847 dprintf("bitmap header offset is %llu\n", offset
);
12849 lseek64(fd
, offset
<< 9, 0);
12854 /*******************************************************************************
12855 * Function: write_init_bitmap_imsm
12856 * Description: Write a bitmap header and prepares the area for the bitmap.
12858 * st : supertype information
12859 * fd : file descriptor for the device
12860 * update : not used for imsm
12865 ******************************************************************************/
12866 static int write_init_bitmap_imsm(struct supertype
*st
, int fd
,
12867 enum bitmap_update update
)
12869 struct intel_super
*super
= st
->sb
;
12870 int vol_idx
= super
->current_vol
;
12872 unsigned long long offset
;
12873 bitmap_super_t bms
= { 0 };
12874 size_t written
= 0;
12879 if (!super
->devlist
|| !super
->sector_size
|| vol_idx
== -1)
12882 struct imsm_dev
*dev
= get_imsm_dev(super
, vol_idx
);
12884 /* first clear the space for bitmap header */
12885 unsigned long long bitmap_area_start
=
12886 get_bitmap_header_sector(super
, vol_idx
);
12888 dprintf("zeroing area start (%llu) and size (%u)\n", bitmap_area_start
,
12889 IMSM_BITMAP_AND_HEADER_SIZE
/ super
->sector_size
);
12890 if (zero_disk_range(fd
, bitmap_area_start
,
12891 IMSM_BITMAP_HEADER_SIZE
/ super
->sector_size
)) {
12892 pr_err("imsm: cannot zeroing the space for the bitmap\n");
12896 /* The bitmap area should be filled with "1"s to perform initial
12899 if (posix_memalign(&buf
, MAX_SECTOR_SIZE
, MAX_SECTOR_SIZE
))
12901 memset(buf
, 0xFF, MAX_SECTOR_SIZE
);
12902 offset
= get_bitmap_sector(super
, vol_idx
);
12903 lseek64(fd
, offset
<< 9, 0);
12904 while (written
< IMSM_BITMAP_AREA_SIZE
) {
12905 to_write
= IMSM_BITMAP_AREA_SIZE
- written
;
12906 if (to_write
> MAX_SECTOR_SIZE
)
12907 to_write
= MAX_SECTOR_SIZE
;
12908 rv_num
= write(fd
, buf
, MAX_SECTOR_SIZE
);
12909 if (rv_num
!= MAX_SECTOR_SIZE
) {
12911 dprintf("cannot initialize bitmap area\n");
12917 /* write a bitmap header */
12918 init_bitmap_header(st
, &bms
, dev
);
12919 memset(buf
, 0, MAX_SECTOR_SIZE
);
12920 memcpy(buf
, &bms
, sizeof(bitmap_super_t
));
12921 if (locate_bitmap_imsm(st
, fd
, 0)) {
12923 dprintf("cannot locate the bitmap\n");
12926 if (write(fd
, buf
, MAX_SECTOR_SIZE
) != MAX_SECTOR_SIZE
) {
12928 dprintf("cannot write the bitmap header\n");
12939 /*******************************************************************************
12940 * Function: is_vol_to_setup_bitmap
12941 * Description: Checks if a bitmap should be activated on the dev.
12943 * info : info about the volume to setup the bitmap
12944 * dev : the device to check against bitmap creation
12947 * 0 : bitmap should be set up on the device
12949 ******************************************************************************/
12950 static int is_vol_to_setup_bitmap(struct mdinfo
*info
, struct imsm_dev
*dev
)
12955 if ((strcmp((char *)dev
->volume
, info
->name
) == 0) &&
12956 (dev
->rwh_policy
== RWH_BITMAP
))
12962 /*******************************************************************************
12963 * Function: set_bitmap_sysfs
12964 * Description: Set the sysfs atributes of a given volume to activate the bitmap.
12966 * info : info about the volume where the bitmap should be setup
12967 * chunksize : bitmap chunk size
12968 * location : location of the bitmap
12973 ******************************************************************************/
12974 static int set_bitmap_sysfs(struct mdinfo
*info
, unsigned long long chunksize
,
12977 /* The bitmap/metadata is set to external to allow changing of value for
12978 * bitmap/location. When external is used, the kernel will treat an offset
12979 * related to the device's first lba (in opposition to the "internal" case
12980 * when this value is related to the beginning of the superblock).
12982 if (sysfs_set_str(info
, NULL
, "bitmap/metadata", "external")) {
12983 dprintf("failed to set bitmap/metadata\n");
12987 /* It can only be changed when no bitmap is active.
12988 * Should be bigger than 512 and must be power of 2.
12989 * It is expecting the value in bytes.
12991 if (sysfs_set_num(info
, NULL
, "bitmap/chunksize",
12992 __cpu_to_le32(chunksize
))) {
12993 dprintf("failed to set bitmap/chunksize\n");
12997 /* It is expecting the value in sectors. */
12998 if (sysfs_set_num(info
, NULL
, "bitmap/space",
12999 __cpu_to_le64(IMSM_BITMAP_AREA_SIZE
))) {
13000 dprintf("failed to set bitmap/space\n");
13004 /* Determines the delay between the bitmap updates.
13005 * It is expecting the value in seconds.
13007 if (sysfs_set_num(info
, NULL
, "bitmap/time_base",
13008 __cpu_to_le64(IMSM_DEFAULT_BITMAP_DAEMON_SLEEP
))) {
13009 dprintf("failed to set bitmap/time_base\n");
13013 /* It is expecting the value in sectors with a sign at the beginning. */
13014 if (sysfs_set_str(info
, NULL
, "bitmap/location", location
)) {
13015 dprintf("failed to set bitmap/location\n");
13022 /*******************************************************************************
13023 * Function: set_bitmap_imsm
13024 * Description: Setup the bitmap for the given volume
13026 * st : supertype information
13027 * info : info about the volume where the bitmap should be setup
13032 ******************************************************************************/
13033 static int set_bitmap_imsm(struct supertype
*st
, struct mdinfo
*info
)
13035 struct intel_super
*super
= st
->sb
;
13036 int prev_current_vol
= super
->current_vol
;
13037 struct imsm_dev
*dev
;
13039 char location
[16] = "";
13040 unsigned long long chunksize
;
13041 struct intel_dev
*dev_it
;
13043 for (dev_it
= super
->devlist
; dev_it
; dev_it
= dev_it
->next
) {
13044 super
->current_vol
= dev_it
->index
;
13045 dev
= get_imsm_dev(super
, super
->current_vol
);
13047 if (is_vol_to_setup_bitmap(info
, dev
)) {
13048 if (validate_internal_bitmap_imsm(st
)) {
13049 dprintf("bitmap header validation failed\n");
13053 chunksize
= calculate_bitmap_chunksize(st
, dev
);
13054 dprintf("chunk size is %llu\n", chunksize
);
13056 snprintf(location
, sizeof(location
), "+%llu",
13057 get_bitmap_sector(super
, super
->current_vol
));
13058 dprintf("bitmap offset is %s\n", location
);
13060 if (set_bitmap_sysfs(info
, chunksize
, location
)) {
13061 dprintf("cannot setup the bitmap\n");
13068 super
->current_vol
= prev_current_vol
;
13072 struct superswitch super_imsm
= {
13073 .examine_super
= examine_super_imsm
,
13074 .brief_examine_super
= brief_examine_super_imsm
,
13075 .brief_examine_subarrays
= brief_examine_subarrays_imsm
,
13076 .export_examine_super
= export_examine_super_imsm
,
13077 .detail_super
= detail_super_imsm
,
13078 .brief_detail_super
= brief_detail_super_imsm
,
13079 .write_init_super
= write_init_super_imsm
,
13080 .validate_geometry
= validate_geometry_imsm
,
13081 .add_to_super
= add_to_super_imsm
,
13082 .remove_from_super
= remove_from_super_imsm
,
13083 .detail_platform
= detail_platform_imsm
,
13084 .export_detail_platform
= export_detail_platform_imsm
,
13085 .kill_subarray
= kill_subarray_imsm
,
13086 .update_subarray
= update_subarray_imsm
,
13087 .load_container
= load_container_imsm
,
13088 .default_geometry
= default_geometry_imsm
,
13089 .test_and_add_drive_policies
= test_and_add_drive_policies_imsm
,
13090 .reshape_super
= imsm_reshape_super
,
13091 .manage_reshape
= imsm_manage_reshape
,
13092 .recover_backup
= recover_backup_imsm
,
13093 .examine_badblocks
= examine_badblocks_imsm
,
13094 .match_home
= match_home_imsm
,
13095 .uuid_from_super
= uuid_from_super_imsm
,
13096 .getinfo_super
= getinfo_super_imsm
,
13097 .getinfo_super_disks
= getinfo_super_disks_imsm
,
13098 .update_super
= update_super_imsm
,
13100 .avail_size
= avail_size_imsm
,
13101 .get_spare_criteria
= get_spare_criteria_imsm
,
13103 .compare_super
= compare_super_imsm
,
13105 .load_super
= load_super_imsm
,
13106 .init_super
= init_super_imsm
,
13107 .store_super
= store_super_imsm
,
13108 .free_super
= free_super_imsm
,
13109 .match_metadata_desc
= match_metadata_desc_imsm
,
13110 .container_content
= container_content_imsm
,
13111 .validate_container
= validate_container_imsm
,
13113 .add_internal_bitmap
= add_internal_bitmap_imsm
,
13114 .locate_bitmap
= locate_bitmap_imsm
,
13115 .write_bitmap
= write_init_bitmap_imsm
,
13116 .set_bitmap
= set_bitmap_imsm
,
13118 .write_init_ppl
= write_init_ppl_imsm
,
13119 .validate_ppl
= validate_ppl_imsm
,
13126 .open_new
= imsm_open_new
,
13127 .set_array_state
= imsm_set_array_state
,
13128 .set_disk
= imsm_set_disk
,
13129 .sync_metadata
= imsm_sync_metadata
,
13130 .activate_spare
= imsm_activate_spare
,
13131 .process_update
= imsm_process_update
,
13132 .prepare_update
= imsm_prepare_update
,
13133 .record_bad_block
= imsm_record_badblock
,
13134 .clear_bad_block
= imsm_clear_badblock
,
13135 .get_bad_blocks
= imsm_get_badblocks
,