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 #define pr_vrb(fmt, arg...) (void) (verbose && pr_err(fmt, ##arg))
398 static __u8
migr_type(struct imsm_dev
*dev
)
400 if (dev
->vol
.migr_type
== MIGR_VERIFY
&&
401 dev
->status
& DEV_VERIFY_AND_FIX
)
404 return dev
->vol
.migr_type
;
407 static void set_migr_type(struct imsm_dev
*dev
, __u8 migr_type
)
409 /* for compatibility with older oroms convert MIGR_REPAIR, into
410 * MIGR_VERIFY w/ DEV_VERIFY_AND_FIX status
412 if (migr_type
== MIGR_REPAIR
) {
413 dev
->vol
.migr_type
= MIGR_VERIFY
;
414 dev
->status
|= DEV_VERIFY_AND_FIX
;
416 dev
->vol
.migr_type
= migr_type
;
417 dev
->status
&= ~DEV_VERIFY_AND_FIX
;
421 static unsigned int sector_count(__u32 bytes
, unsigned int sector_size
)
423 return ROUND_UP(bytes
, sector_size
) / sector_size
;
426 static unsigned int mpb_sectors(struct imsm_super
*mpb
,
427 unsigned int sector_size
)
429 return sector_count(__le32_to_cpu(mpb
->mpb_size
), sector_size
);
433 struct imsm_dev
*dev
;
434 struct intel_dev
*next
;
439 enum sys_dev_type type
;
442 struct intel_hba
*next
;
449 /* internal representation of IMSM metadata */
452 void *buf
; /* O_DIRECT buffer for reading/writing metadata */
453 struct imsm_super
*anchor
; /* immovable parameters */
456 void *migr_rec_buf
; /* buffer for I/O operations */
457 struct migr_record
*migr_rec
; /* migration record */
459 int clean_migration_record_by_mdmon
; /* when reshape is switched to next
460 array, it indicates that mdmon is allowed to clean migration
462 size_t len
; /* size of the 'buf' allocation */
463 size_t extra_space
; /* extra space in 'buf' that is not used yet */
464 void *next_buf
; /* for realloc'ing buf from the manager */
466 int updates_pending
; /* count of pending updates for mdmon */
467 int current_vol
; /* index of raid device undergoing creation */
468 unsigned long long create_offset
; /* common start for 'current_vol' */
469 __u32 random
; /* random data for seeding new family numbers */
470 struct intel_dev
*devlist
;
471 unsigned int sector_size
; /* sector size of used member drives */
475 __u8 serial
[MAX_RAID_SERIAL_LEN
];
478 struct imsm_disk disk
;
481 struct extent
*e
; /* for determining freespace @ create */
482 int raiddisk
; /* slot to fill in autolayout */
484 } *disks
, *current_disk
;
485 struct dl
*disk_mgmt_list
; /* list of disks to add/remove while mdmon
487 struct dl
*missing
; /* disks removed while we weren't looking */
488 struct bbm_log
*bbm_log
;
489 struct intel_hba
*hba
; /* device path of the raid controller for this metadata */
490 const struct imsm_orom
*orom
; /* platform firmware support */
491 struct intel_super
*next
; /* (temp) list for disambiguating family_num */
492 struct md_bb bb
; /* memory for get_bad_blocks call */
496 struct imsm_disk disk
;
497 #define IMSM_UNKNOWN_OWNER (-1)
499 struct intel_disk
*next
;
503 * struct extent - reserved space details.
504 * @start: start offset.
505 * @size: size of reservation, set to 0 for metadata reservation.
506 * @vol: index of the volume, meaningful if &size is set.
509 unsigned long long start
, size
;
513 /* definitions of reshape process types */
514 enum imsm_reshape_type
{
520 /* definition of messages passed to imsm_process_update */
521 enum imsm_update_type
{
522 update_activate_spare
,
526 update_add_remove_disk
,
527 update_reshape_container_disks
,
528 update_reshape_migration
,
530 update_general_migration_checkpoint
,
532 update_prealloc_badblocks_mem
,
536 struct imsm_update_activate_spare
{
537 enum imsm_update_type type
;
541 struct imsm_update_activate_spare
*next
;
547 unsigned long long size
;
554 enum takeover_direction
{
558 struct imsm_update_takeover
{
559 enum imsm_update_type type
;
561 enum takeover_direction direction
;
564 struct imsm_update_reshape
{
565 enum imsm_update_type type
;
569 int new_disks
[1]; /* new_raid_disks - old_raid_disks makedev number */
572 struct imsm_update_reshape_migration
{
573 enum imsm_update_type type
;
576 /* fields for array migration changes
583 int new_disks
[1]; /* new_raid_disks - old_raid_disks makedev number */
586 struct imsm_update_size_change
{
587 enum imsm_update_type type
;
592 struct imsm_update_general_migration_checkpoint
{
593 enum imsm_update_type type
;
594 __u64 curr_migr_unit
;
598 __u8 serial
[MAX_RAID_SERIAL_LEN
];
601 struct imsm_update_create_array
{
602 enum imsm_update_type type
;
607 struct imsm_update_kill_array
{
608 enum imsm_update_type type
;
612 struct imsm_update_rename_array
{
613 enum imsm_update_type type
;
614 __u8 name
[MAX_RAID_SERIAL_LEN
];
618 struct imsm_update_add_remove_disk
{
619 enum imsm_update_type type
;
622 struct imsm_update_prealloc_bb_mem
{
623 enum imsm_update_type type
;
626 struct imsm_update_rwh_policy
{
627 enum imsm_update_type type
;
632 static const char *_sys_dev_type
[] = {
633 [SYS_DEV_UNKNOWN
] = "Unknown",
634 [SYS_DEV_SAS
] = "SAS",
635 [SYS_DEV_SATA
] = "SATA",
636 [SYS_DEV_NVME
] = "NVMe",
637 [SYS_DEV_VMD
] = "VMD",
638 [SYS_DEV_SATA_VMD
] = "SATA VMD"
641 static int no_platform
= -1;
643 static int check_no_platform(void)
645 static const char search
[] = "mdadm.imsm.test=1";
648 if (no_platform
>= 0)
651 if (check_env("IMSM_NO_PLATFORM")) {
655 fp
= fopen("/proc/cmdline", "r");
657 char *l
= conf_line(fp
);
666 if (strcmp(w
, search
) == 0)
672 if (no_platform
>= 0)
679 void imsm_set_no_platform(int v
)
684 const char *get_sys_dev_type(enum sys_dev_type type
)
686 if (type
>= SYS_DEV_MAX
)
687 type
= SYS_DEV_UNKNOWN
;
689 return _sys_dev_type
[type
];
692 static struct intel_hba
* alloc_intel_hba(struct sys_dev
*device
)
694 struct intel_hba
*result
= xmalloc(sizeof(*result
));
696 result
->type
= device
->type
;
697 result
->path
= xstrdup(device
->path
);
699 if (result
->path
&& (result
->pci_id
= strrchr(result
->path
, '/')) != NULL
)
705 static struct intel_hba
* find_intel_hba(struct intel_hba
*hba
, struct sys_dev
*device
)
707 struct intel_hba
*result
;
709 for (result
= hba
; result
; result
= result
->next
) {
710 if (result
->type
== device
->type
&& strcmp(result
->path
, device
->path
) == 0)
716 static int attach_hba_to_super(struct intel_super
*super
, struct sys_dev
*device
)
718 struct intel_hba
*hba
;
720 /* check if disk attached to Intel HBA */
721 hba
= find_intel_hba(super
->hba
, device
);
724 /* Check if HBA is already attached to super */
725 if (super
->hba
== NULL
) {
726 super
->hba
= alloc_intel_hba(device
);
731 /* Intel metadata allows for all disks attached to the same type HBA.
732 * Do not support HBA types mixing
734 if (device
->type
!= hba
->type
)
737 /* Multiple same type HBAs can be used if they share the same OROM */
738 const struct imsm_orom
*device_orom
= get_orom_by_device_id(device
->dev_id
);
740 if (device_orom
!= super
->orom
)
746 hba
->next
= alloc_intel_hba(device
);
750 static struct sys_dev
* find_disk_attached_hba(int fd
, const char *devname
)
752 struct sys_dev
*list
, *elem
;
755 if ((list
= find_intel_devices()) == NULL
)
758 if (!is_fd_valid(fd
))
759 disk_path
= (char *) devname
;
761 disk_path
= diskfd_to_devpath(fd
, 1, NULL
);
766 for (elem
= list
; elem
; elem
= elem
->next
)
767 if (path_attached_to_hba(disk_path
, elem
->path
))
770 if (disk_path
!= devname
)
776 static int find_intel_hba_capability(int fd
, struct intel_super
*super
,
779 static struct supertype
*match_metadata_desc_imsm(char *arg
)
781 struct supertype
*st
;
783 if (strcmp(arg
, "imsm") != 0 &&
784 strcmp(arg
, "default") != 0
788 st
= xcalloc(1, sizeof(*st
));
789 st
->ss
= &super_imsm
;
790 st
->max_devs
= IMSM_MAX_DEVICES
;
791 st
->minor_version
= 0;
796 static __u8
*get_imsm_version(struct imsm_super
*mpb
)
798 return &mpb
->sig
[MPB_SIG_LEN
];
801 /* retrieve a disk directly from the anchor when the anchor is known to be
802 * up-to-date, currently only at load time
804 static struct imsm_disk
*__get_imsm_disk(struct imsm_super
*mpb
, __u8 index
)
806 if (index
>= mpb
->num_disks
)
808 return &mpb
->disk
[index
];
811 /* retrieve the disk description based on a index of the disk
814 static struct dl
*get_imsm_dl_disk(struct intel_super
*super
, __u8 index
)
818 for (d
= super
->disks
; d
; d
= d
->next
)
819 if (d
->index
== index
)
824 /* retrieve a disk from the parsed metadata */
825 static struct imsm_disk
*get_imsm_disk(struct intel_super
*super
, __u8 index
)
829 dl
= get_imsm_dl_disk(super
, index
);
836 /* generate a checksum directly from the anchor when the anchor is known to be
837 * up-to-date, currently only at load or write_super after coalescing
839 static __u32
__gen_imsm_checksum(struct imsm_super
*mpb
)
841 __u32 end
= mpb
->mpb_size
/ sizeof(end
);
842 __u32
*p
= (__u32
*) mpb
;
846 sum
+= __le32_to_cpu(*p
);
850 return sum
- __le32_to_cpu(mpb
->check_sum
);
853 static size_t sizeof_imsm_map(struct imsm_map
*map
)
855 return sizeof(struct imsm_map
) + sizeof(__u32
) * (map
->num_members
- 1);
858 struct imsm_map
*get_imsm_map(struct imsm_dev
*dev
, int second_map
)
860 /* A device can have 2 maps if it is in the middle of a migration.
862 * MAP_0 - we return the first map
863 * MAP_1 - we return the second map if it exists, else NULL
864 * MAP_X - we return the second map if it exists, else the first
866 struct imsm_map
*map
= &dev
->vol
.map
[0];
867 struct imsm_map
*map2
= NULL
;
869 if (dev
->vol
.migr_state
)
870 map2
= (void *)map
+ sizeof_imsm_map(map
);
872 switch (second_map
) {
889 /* return the size of the device.
890 * migr_state increases the returned size if map[0] were to be duplicated
892 static size_t sizeof_imsm_dev(struct imsm_dev
*dev
, int migr_state
)
894 size_t size
= sizeof(*dev
) - sizeof(struct imsm_map
) +
895 sizeof_imsm_map(get_imsm_map(dev
, MAP_0
));
897 /* migrating means an additional map */
898 if (dev
->vol
.migr_state
)
899 size
+= sizeof_imsm_map(get_imsm_map(dev
, MAP_1
));
901 size
+= sizeof_imsm_map(get_imsm_map(dev
, MAP_0
));
906 /* retrieve disk serial number list from a metadata update */
907 static struct disk_info
*get_disk_info(struct imsm_update_create_array
*update
)
910 struct disk_info
*inf
;
912 inf
= u
+ sizeof(*update
) - sizeof(struct imsm_dev
) +
913 sizeof_imsm_dev(&update
->dev
, 0);
919 * __get_imsm_dev() - Get device with index from imsm_super.
920 * @mpb: &imsm_super pointer, not NULL.
921 * @index: Device index.
923 * Function works as non-NULL, aborting in such a case,
924 * when NULL would be returned.
926 * Device index should be in range 0 up to num_raid_devs.
927 * Function assumes the index was already verified.
928 * Index must be valid, otherwise abort() is called.
930 * Return: Pointer to corresponding imsm_dev.
933 static struct imsm_dev
*__get_imsm_dev(struct imsm_super
*mpb
, __u8 index
)
939 if (index
>= mpb
->num_raid_devs
)
942 /* devices start after all disks */
943 offset
= ((void *) &mpb
->disk
[mpb
->num_disks
]) - _mpb
;
945 for (i
= 0; i
<= index
; i
++, offset
+= sizeof_imsm_dev(_mpb
+ offset
, 0))
947 return _mpb
+ offset
;
949 pr_err("cannot find imsm_dev with index %u in imsm_super\n", index
);
954 * get_imsm_dev() - Get device with index from intel_super.
955 * @super: &intel_super pointer, not NULL.
956 * @index: Device index.
958 * Function works as non-NULL, aborting in such a case,
959 * when NULL would be returned.
961 * Device index should be in range 0 up to num_raid_devs.
962 * Function assumes the index was already verified.
963 * Index must be valid, otherwise abort() is called.
965 * Return: Pointer to corresponding imsm_dev.
968 static struct imsm_dev
*get_imsm_dev(struct intel_super
*super
, __u8 index
)
970 struct intel_dev
*dv
;
972 if (index
>= super
->anchor
->num_raid_devs
)
975 for (dv
= super
->devlist
; dv
; dv
= dv
->next
)
976 if (dv
->index
== index
)
979 pr_err("cannot find imsm_dev with index %u in intel_super\n", index
);
983 static inline unsigned long long __le48_to_cpu(const struct bbm_log_block_addr
986 return ((((__u64
)__le32_to_cpu(addr
->dw1
)) << 16) |
987 __le16_to_cpu(addr
->w1
));
990 static inline struct bbm_log_block_addr
__cpu_to_le48(unsigned long long sec
)
992 struct bbm_log_block_addr addr
;
994 addr
.w1
= __cpu_to_le16((__u16
)(sec
& 0xffff));
995 addr
.dw1
= __cpu_to_le32((__u32
)(sec
>> 16) & 0xffffffff);
999 /* get size of the bbm log */
1000 static __u32
get_imsm_bbm_log_size(struct bbm_log
*log
)
1002 if (!log
|| log
->entry_count
== 0)
1005 return sizeof(log
->signature
) +
1006 sizeof(log
->entry_count
) +
1007 log
->entry_count
* sizeof(struct bbm_log_entry
);
1010 /* check if bad block is not partially stored in bbm log */
1011 static int is_stored_in_bbm(struct bbm_log
*log
, const __u8 idx
, const unsigned
1012 long long sector
, const int length
, __u32
*pos
)
1016 for (i
= *pos
; i
< log
->entry_count
; i
++) {
1017 struct bbm_log_entry
*entry
= &log
->marked_block_entries
[i
];
1018 unsigned long long bb_start
;
1019 unsigned long long bb_end
;
1021 bb_start
= __le48_to_cpu(&entry
->defective_block_start
);
1022 bb_end
= bb_start
+ (entry
->marked_count
+ 1);
1024 if ((entry
->disk_ordinal
== idx
) && (bb_start
>= sector
) &&
1025 (bb_end
<= sector
+ length
)) {
1033 /* record new bad block in bbm log */
1034 static int record_new_badblock(struct bbm_log
*log
, const __u8 idx
, unsigned
1035 long long sector
, int length
)
1039 struct bbm_log_entry
*entry
= NULL
;
1041 while (is_stored_in_bbm(log
, idx
, sector
, length
, &pos
)) {
1042 struct bbm_log_entry
*e
= &log
->marked_block_entries
[pos
];
1044 if ((e
->marked_count
+ 1 == BBM_LOG_MAX_LBA_ENTRY_VAL
) &&
1045 (__le48_to_cpu(&e
->defective_block_start
) == sector
)) {
1046 sector
+= BBM_LOG_MAX_LBA_ENTRY_VAL
;
1047 length
-= BBM_LOG_MAX_LBA_ENTRY_VAL
;
1056 int cnt
= (length
<= BBM_LOG_MAX_LBA_ENTRY_VAL
) ? length
:
1057 BBM_LOG_MAX_LBA_ENTRY_VAL
;
1058 entry
->defective_block_start
= __cpu_to_le48(sector
);
1059 entry
->marked_count
= cnt
- 1;
1066 new_bb
= ROUND_UP(length
, BBM_LOG_MAX_LBA_ENTRY_VAL
) /
1067 BBM_LOG_MAX_LBA_ENTRY_VAL
;
1068 if (log
->entry_count
+ new_bb
> BBM_LOG_MAX_ENTRIES
)
1071 while (length
> 0) {
1072 int cnt
= (length
<= BBM_LOG_MAX_LBA_ENTRY_VAL
) ? length
:
1073 BBM_LOG_MAX_LBA_ENTRY_VAL
;
1074 struct bbm_log_entry
*entry
=
1075 &log
->marked_block_entries
[log
->entry_count
];
1077 entry
->defective_block_start
= __cpu_to_le48(sector
);
1078 entry
->marked_count
= cnt
- 1;
1079 entry
->disk_ordinal
= idx
;
1090 /* clear all bad blocks for given disk */
1091 static void clear_disk_badblocks(struct bbm_log
*log
, const __u8 idx
)
1095 while (i
< log
->entry_count
) {
1096 struct bbm_log_entry
*entries
= log
->marked_block_entries
;
1098 if (entries
[i
].disk_ordinal
== idx
) {
1099 if (i
< log
->entry_count
- 1)
1100 entries
[i
] = entries
[log
->entry_count
- 1];
1108 /* clear given bad block */
1109 static int clear_badblock(struct bbm_log
*log
, const __u8 idx
, const unsigned
1110 long long sector
, const int length
) {
1113 while (i
< log
->entry_count
) {
1114 struct bbm_log_entry
*entries
= log
->marked_block_entries
;
1116 if ((entries
[i
].disk_ordinal
== idx
) &&
1117 (__le48_to_cpu(&entries
[i
].defective_block_start
) ==
1118 sector
) && (entries
[i
].marked_count
+ 1 == length
)) {
1119 if (i
< log
->entry_count
- 1)
1120 entries
[i
] = entries
[log
->entry_count
- 1];
1130 /* allocate and load BBM log from metadata */
1131 static int load_bbm_log(struct intel_super
*super
)
1133 struct imsm_super
*mpb
= super
->anchor
;
1134 __u32 bbm_log_size
= __le32_to_cpu(mpb
->bbm_log_size
);
1136 super
->bbm_log
= xcalloc(1, sizeof(struct bbm_log
));
1137 if (!super
->bbm_log
)
1141 struct bbm_log
*log
= (void *)mpb
+
1142 __le32_to_cpu(mpb
->mpb_size
) - bbm_log_size
;
1146 if (bbm_log_size
< sizeof(log
->signature
) +
1147 sizeof(log
->entry_count
))
1150 entry_count
= __le32_to_cpu(log
->entry_count
);
1151 if ((__le32_to_cpu(log
->signature
) != BBM_LOG_SIGNATURE
) ||
1152 (entry_count
> BBM_LOG_MAX_ENTRIES
))
1156 sizeof(log
->signature
) + sizeof(log
->entry_count
) +
1157 entry_count
* sizeof(struct bbm_log_entry
))
1160 memcpy(super
->bbm_log
, log
, bbm_log_size
);
1162 super
->bbm_log
->signature
= __cpu_to_le32(BBM_LOG_SIGNATURE
);
1163 super
->bbm_log
->entry_count
= 0;
1169 /* checks if bad block is within volume boundaries */
1170 static int is_bad_block_in_volume(const struct bbm_log_entry
*entry
,
1171 const unsigned long long start_sector
,
1172 const unsigned long long size
)
1174 unsigned long long bb_start
;
1175 unsigned long long bb_end
;
1177 bb_start
= __le48_to_cpu(&entry
->defective_block_start
);
1178 bb_end
= bb_start
+ (entry
->marked_count
+ 1);
1180 if (((bb_start
>= start_sector
) && (bb_start
< start_sector
+ size
)) ||
1181 ((bb_end
>= start_sector
) && (bb_end
<= start_sector
+ size
)))
1187 /* get list of bad blocks on a drive for a volume */
1188 static void get_volume_badblocks(const struct bbm_log
*log
, const __u8 idx
,
1189 const unsigned long long start_sector
,
1190 const unsigned long long size
,
1196 for (i
= 0; i
< log
->entry_count
; i
++) {
1197 const struct bbm_log_entry
*ent
=
1198 &log
->marked_block_entries
[i
];
1199 struct md_bb_entry
*bb
;
1201 if ((ent
->disk_ordinal
== idx
) &&
1202 is_bad_block_in_volume(ent
, start_sector
, size
)) {
1204 if (!bbs
->entries
) {
1205 bbs
->entries
= xmalloc(BBM_LOG_MAX_ENTRIES
*
1211 bb
= &bbs
->entries
[count
++];
1212 bb
->sector
= __le48_to_cpu(&ent
->defective_block_start
);
1213 bb
->length
= ent
->marked_count
+ 1;
1221 * == MAP_0 get first map
1222 * == MAP_1 get second map
1223 * == MAP_X than get map according to the current migr_state
1225 static __u32
get_imsm_ord_tbl_ent(struct imsm_dev
*dev
,
1229 struct imsm_map
*map
;
1231 map
= get_imsm_map(dev
, second_map
);
1233 /* top byte identifies disk under rebuild */
1234 return __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
1237 #define ord_to_idx(ord) (((ord) << 8) >> 8)
1238 static __u32
get_imsm_disk_idx(struct imsm_dev
*dev
, int slot
, int second_map
)
1240 __u32 ord
= get_imsm_ord_tbl_ent(dev
, slot
, second_map
);
1242 return ord_to_idx(ord
);
1245 static void set_imsm_ord_tbl_ent(struct imsm_map
*map
, int slot
, __u32 ord
)
1247 map
->disk_ord_tbl
[slot
] = __cpu_to_le32(ord
);
1250 static int get_imsm_disk_slot(struct imsm_map
*map
, const unsigned int idx
)
1255 for (slot
= 0; slot
< map
->num_members
; slot
++) {
1256 ord
= __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
1257 if (ord_to_idx(ord
) == idx
)
1261 return IMSM_STATUS_ERROR
;
1264 static int get_imsm_raid_level(struct imsm_map
*map
)
1266 if (map
->raid_level
== 1) {
1267 if (map
->num_members
== 2)
1273 return map
->raid_level
;
1277 * get_disk_slot_in_dev() - retrieve disk slot from &imsm_dev.
1278 * @super: &intel_super pointer, not NULL.
1279 * @dev_idx: imsm device index.
1282 * Return: Slot on success, IMSM_STATUS_ERROR otherwise.
1284 static int get_disk_slot_in_dev(struct intel_super
*super
, const __u8 dev_idx
,
1285 const unsigned int idx
)
1287 struct imsm_dev
*dev
= get_imsm_dev(super
, dev_idx
);
1288 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
1290 return get_imsm_disk_slot(map
, idx
);
1293 static int cmp_extent(const void *av
, const void *bv
)
1295 const struct extent
*a
= av
;
1296 const struct extent
*b
= bv
;
1297 if (a
->start
< b
->start
)
1299 if (a
->start
> b
->start
)
1304 static int count_memberships(struct dl
*dl
, struct intel_super
*super
)
1306 int memberships
= 0;
1309 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++)
1310 if (get_disk_slot_in_dev(super
, i
, dl
->index
) >= 0)
1316 static __u32
imsm_min_reserved_sectors(struct intel_super
*super
);
1318 static int split_ull(unsigned long long n
, void *lo
, void *hi
)
1320 if (lo
== 0 || hi
== 0)
1322 __put_unaligned32(__cpu_to_le32((__u32
)n
), lo
);
1323 __put_unaligned32(__cpu_to_le32((n
>> 32)), hi
);
1327 static unsigned long long join_u32(__u32 lo
, __u32 hi
)
1329 return (unsigned long long)__le32_to_cpu(lo
) |
1330 (((unsigned long long)__le32_to_cpu(hi
)) << 32);
1333 static unsigned long long total_blocks(struct imsm_disk
*disk
)
1337 return join_u32(disk
->total_blocks_lo
, disk
->total_blocks_hi
);
1341 * imsm_num_data_members() - get data drives count for an array.
1342 * @map: Map to analyze.
1344 * num_data_members value represents minimal count of drives for level.
1345 * The name of the property could be misleading for RAID5 with asymmetric layout
1346 * because some data required to be calculated from parity.
1347 * The property is extracted from level and num_members value.
1349 * Return: num_data_members value on success, zero otherwise.
1351 static __u8
imsm_num_data_members(struct imsm_map
*map
)
1353 switch (get_imsm_raid_level(map
)) {
1355 return map
->num_members
;
1358 return map
->num_members
/ 2;
1360 return map
->num_members
- 1;
1362 dprintf("unsupported raid level\n");
1367 static unsigned long long pba_of_lba0(struct imsm_map
*map
)
1371 return join_u32(map
->pba_of_lba0_lo
, map
->pba_of_lba0_hi
);
1374 static unsigned long long blocks_per_member(struct imsm_map
*map
)
1378 return join_u32(map
->blocks_per_member_lo
, map
->blocks_per_member_hi
);
1381 static unsigned long long num_data_stripes(struct imsm_map
*map
)
1385 return join_u32(map
->num_data_stripes_lo
, map
->num_data_stripes_hi
);
1388 static unsigned long long vol_curr_migr_unit(struct imsm_dev
*dev
)
1393 return join_u32(dev
->vol
.curr_migr_unit_lo
, dev
->vol
.curr_migr_unit_hi
);
1396 static unsigned long long imsm_dev_size(struct imsm_dev
*dev
)
1400 return join_u32(dev
->size_low
, dev
->size_high
);
1403 static unsigned long long migr_chkp_area_pba(struct migr_record
*migr_rec
)
1405 if (migr_rec
== NULL
)
1407 return join_u32(migr_rec
->ckpt_area_pba_lo
,
1408 migr_rec
->ckpt_area_pba_hi
);
1411 static unsigned long long current_migr_unit(struct migr_record
*migr_rec
)
1413 if (migr_rec
== NULL
)
1415 return join_u32(migr_rec
->curr_migr_unit_lo
,
1416 migr_rec
->curr_migr_unit_hi
);
1419 static unsigned long long migr_dest_1st_member_lba(struct migr_record
*migr_rec
)
1421 if (migr_rec
== NULL
)
1423 return join_u32(migr_rec
->dest_1st_member_lba_lo
,
1424 migr_rec
->dest_1st_member_lba_hi
);
1427 static unsigned long long get_num_migr_units(struct migr_record
*migr_rec
)
1429 if (migr_rec
== NULL
)
1431 return join_u32(migr_rec
->num_migr_units_lo
,
1432 migr_rec
->num_migr_units_hi
);
1435 static void set_total_blocks(struct imsm_disk
*disk
, unsigned long long n
)
1437 split_ull(n
, &disk
->total_blocks_lo
, &disk
->total_blocks_hi
);
1441 * set_num_domains() - Set number of domains for an array.
1442 * @map: Map to be updated.
1444 * num_domains property represents copies count of each data drive, thus make
1445 * it meaningful only for RAID1 and RAID10. IMSM supports two domains for
1448 static void set_num_domains(struct imsm_map
*map
)
1450 int level
= get_imsm_raid_level(map
);
1452 if (level
== 1 || level
== 10)
1453 map
->num_domains
= 2;
1455 map
->num_domains
= 1;
1458 static void set_pba_of_lba0(struct imsm_map
*map
, unsigned long long n
)
1460 split_ull(n
, &map
->pba_of_lba0_lo
, &map
->pba_of_lba0_hi
);
1463 static void set_blocks_per_member(struct imsm_map
*map
, unsigned long long n
)
1465 split_ull(n
, &map
->blocks_per_member_lo
, &map
->blocks_per_member_hi
);
1468 static void set_num_data_stripes(struct imsm_map
*map
, unsigned long long n
)
1470 split_ull(n
, &map
->num_data_stripes_lo
, &map
->num_data_stripes_hi
);
1474 * update_num_data_stripes() - Calculate and update num_data_stripes value.
1475 * @map: map to be updated.
1476 * @dev_size: size of volume.
1478 * num_data_stripes value is addictionally divided by num_domains, therefore for
1479 * levels where num_domains is not 1, nds is a part of real value.
1481 static void update_num_data_stripes(struct imsm_map
*map
,
1482 unsigned long long dev_size
)
1484 unsigned long long nds
= dev_size
/ imsm_num_data_members(map
);
1486 nds
/= map
->num_domains
;
1487 nds
/= map
->blocks_per_strip
;
1488 set_num_data_stripes(map
, nds
);
1491 static void set_vol_curr_migr_unit(struct imsm_dev
*dev
, unsigned long long n
)
1496 split_ull(n
, &dev
->vol
.curr_migr_unit_lo
, &dev
->vol
.curr_migr_unit_hi
);
1499 static void set_imsm_dev_size(struct imsm_dev
*dev
, unsigned long long n
)
1501 split_ull(n
, &dev
->size_low
, &dev
->size_high
);
1504 static void set_migr_chkp_area_pba(struct migr_record
*migr_rec
,
1505 unsigned long long n
)
1507 split_ull(n
, &migr_rec
->ckpt_area_pba_lo
, &migr_rec
->ckpt_area_pba_hi
);
1510 static void set_current_migr_unit(struct migr_record
*migr_rec
,
1511 unsigned long long n
)
1513 split_ull(n
, &migr_rec
->curr_migr_unit_lo
,
1514 &migr_rec
->curr_migr_unit_hi
);
1517 static void set_migr_dest_1st_member_lba(struct migr_record
*migr_rec
,
1518 unsigned long long n
)
1520 split_ull(n
, &migr_rec
->dest_1st_member_lba_lo
,
1521 &migr_rec
->dest_1st_member_lba_hi
);
1524 static void set_num_migr_units(struct migr_record
*migr_rec
,
1525 unsigned long long n
)
1527 split_ull(n
, &migr_rec
->num_migr_units_lo
,
1528 &migr_rec
->num_migr_units_hi
);
1531 static unsigned long long per_dev_array_size(struct imsm_map
*map
)
1533 unsigned long long array_size
= 0;
1538 array_size
= num_data_stripes(map
) * map
->blocks_per_strip
;
1539 if (get_imsm_raid_level(map
) == 1 || get_imsm_raid_level(map
) == 10)
1545 static struct extent
*get_extents(struct intel_super
*super
, struct dl
*dl
,
1546 int get_minimal_reservation
)
1548 /* find a list of used extents on the given physical device */
1549 int memberships
= count_memberships(dl
, super
);
1550 struct extent
*rv
= xcalloc(memberships
+ 1, sizeof(struct extent
));
1551 struct extent
*e
= rv
;
1555 /* trim the reserved area for spares, so they can join any array
1556 * regardless of whether the OROM has assigned sectors from the
1557 * IMSM_RESERVED_SECTORS region
1559 if (dl
->index
== -1 || get_minimal_reservation
)
1560 reservation
= imsm_min_reserved_sectors(super
);
1562 reservation
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
1564 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
1565 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
1566 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
1568 if (get_imsm_disk_slot(map
, dl
->index
) >= 0) {
1569 e
->start
= pba_of_lba0(map
);
1570 e
->size
= per_dev_array_size(map
);
1575 qsort(rv
, memberships
, sizeof(*rv
), cmp_extent
);
1577 /* determine the start of the metadata
1578 * when no raid devices are defined use the default
1579 * ...otherwise allow the metadata to truncate the value
1580 * as is the case with older versions of imsm
1583 struct extent
*last
= &rv
[memberships
- 1];
1584 unsigned long long remainder
;
1586 remainder
= total_blocks(&dl
->disk
) - (last
->start
+ last
->size
);
1587 /* round down to 1k block to satisfy precision of the kernel
1591 /* make sure remainder is still sane */
1592 if (remainder
< (unsigned)ROUND_UP(super
->len
, 512) >> 9)
1593 remainder
= ROUND_UP(super
->len
, 512) >> 9;
1594 if (reservation
> remainder
)
1595 reservation
= remainder
;
1597 e
->start
= total_blocks(&dl
->disk
) - reservation
;
1602 /* try to determine how much space is reserved for metadata from
1603 * the last get_extents() entry, otherwise fallback to the
1606 static __u32
imsm_reserved_sectors(struct intel_super
*super
, struct dl
*dl
)
1612 /* for spares just return a minimal reservation which will grow
1613 * once the spare is picked up by an array
1615 if (dl
->index
== -1)
1616 return MPB_SECTOR_CNT
;
1618 e
= get_extents(super
, dl
, 0);
1620 return MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
1622 /* scroll to last entry */
1623 for (i
= 0; e
[i
].size
; i
++)
1626 rv
= total_blocks(&dl
->disk
) - e
[i
].start
;
1633 static int is_spare(struct imsm_disk
*disk
)
1635 return (disk
->status
& SPARE_DISK
) == SPARE_DISK
;
1638 static int is_configured(struct imsm_disk
*disk
)
1640 return (disk
->status
& CONFIGURED_DISK
) == CONFIGURED_DISK
;
1643 static int is_failed(struct imsm_disk
*disk
)
1645 return (disk
->status
& FAILED_DISK
) == FAILED_DISK
;
1648 static int is_journal(struct imsm_disk
*disk
)
1650 return (disk
->status
& JOURNAL_DISK
) == JOURNAL_DISK
;
1654 * round_member_size_to_mb()- Round given size to closest MiB.
1655 * @size: size to round in sectors.
1657 static inline unsigned long long round_member_size_to_mb(unsigned long long size
)
1659 return (size
>> SECT_PER_MB_SHIFT
) << SECT_PER_MB_SHIFT
;
1663 * round_size_to_mb()- Round given size.
1664 * @array_size: size to round in sectors.
1665 * @disk_count: count of data members.
1667 * Get size per each data member and round it to closest MiB to ensure that data
1668 * splits evenly between members.
1670 * Return: Array size, rounded down.
1672 static inline unsigned long long round_size_to_mb(unsigned long long array_size
,
1673 unsigned int disk_count
)
1675 return round_member_size_to_mb(array_size
/ disk_count
) * disk_count
;
1678 static int able_to_resync(int raid_level
, int missing_disks
)
1680 int max_missing_disks
= 0;
1682 switch (raid_level
) {
1684 max_missing_disks
= 1;
1687 max_missing_disks
= 0;
1689 return missing_disks
<= max_missing_disks
;
1692 /* try to determine how much space is reserved for metadata from
1693 * the last get_extents() entry on the smallest active disk,
1694 * otherwise fallback to the default
1696 static __u32
imsm_min_reserved_sectors(struct intel_super
*super
)
1700 unsigned long long min_active
;
1702 __u32 rv
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
1703 struct dl
*dl
, *dl_min
= NULL
;
1709 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
1712 unsigned long long blocks
= total_blocks(&dl
->disk
);
1713 if (blocks
< min_active
|| min_active
== 0) {
1715 min_active
= blocks
;
1721 /* find last lba used by subarrays on the smallest active disk */
1722 e
= get_extents(super
, dl_min
, 0);
1725 for (i
= 0; e
[i
].size
; i
++)
1728 remainder
= min_active
- e
[i
].start
;
1731 /* to give priority to recovery we should not require full
1732 IMSM_RESERVED_SECTORS from the spare */
1733 rv
= MPB_SECTOR_CNT
+ NUM_BLOCKS_DIRTY_STRIPE_REGION
;
1735 /* if real reservation is smaller use that value */
1736 return (remainder
< rv
) ? remainder
: rv
;
1740 * Return minimum size of a spare and sector size
1741 * that can be used in this array
1743 int get_spare_criteria_imsm(struct supertype
*st
, struct spare_criteria
*c
)
1745 struct intel_super
*super
= st
->sb
;
1749 unsigned long long size
= 0;
1756 /* find first active disk in array */
1758 while (dl
&& (is_failed(&dl
->disk
) || dl
->index
== -1))
1762 /* find last lba used by subarrays */
1763 e
= get_extents(super
, dl
, 0);
1766 for (i
= 0; e
[i
].size
; i
++)
1769 size
= e
[i
-1].start
+ e
[i
-1].size
;
1772 /* add the amount of space needed for metadata */
1773 size
+= imsm_min_reserved_sectors(super
);
1775 c
->min_size
= size
* 512;
1776 c
->sector_size
= super
->sector_size
;
1781 static bool is_gen_migration(struct imsm_dev
*dev
);
1783 #define IMSM_4K_DIV 8
1785 static __u64
blocks_per_migr_unit(struct intel_super
*super
,
1786 struct imsm_dev
*dev
);
1788 static void print_imsm_dev(struct intel_super
*super
,
1789 struct imsm_dev
*dev
,
1795 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
1796 struct imsm_map
*map2
= get_imsm_map(dev
, MAP_1
);
1800 printf("[%.16s]:\n", dev
->volume
);
1801 printf(" Subarray : %d\n", super
->current_vol
);
1802 printf(" UUID : %s\n", uuid
);
1803 printf(" RAID Level : %d", get_imsm_raid_level(map
));
1805 printf(" <-- %d", get_imsm_raid_level(map2
));
1807 printf(" Members : %d", map
->num_members
);
1809 printf(" <-- %d", map2
->num_members
);
1811 printf(" Slots : [");
1812 for (i
= 0; i
< map
->num_members
; i
++) {
1813 ord
= get_imsm_ord_tbl_ent(dev
, i
, MAP_0
);
1814 printf("%s", ord
& IMSM_ORD_REBUILD
? "_" : "U");
1819 for (i
= 0; i
< map2
->num_members
; i
++) {
1820 ord
= get_imsm_ord_tbl_ent(dev
, i
, MAP_1
);
1821 printf("%s", ord
& IMSM_ORD_REBUILD
? "_" : "U");
1826 printf(" Failed disk : ");
1827 if (map
->failed_disk_num
== 0xff)
1828 printf(STR_COMMON_NONE
);
1830 printf("%i", map
->failed_disk_num
);
1832 slot
= get_imsm_disk_slot(map
, disk_idx
);
1834 ord
= get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
);
1835 printf(" This Slot : %d%s\n", slot
,
1836 ord
& IMSM_ORD_REBUILD
? " (out-of-sync)" : "");
1838 printf(" This Slot : ?\n");
1839 printf(" Sector Size : %u\n", super
->sector_size
);
1840 sz
= imsm_dev_size(dev
);
1841 printf(" Array Size : %llu%s\n",
1842 (unsigned long long)sz
* 512 / super
->sector_size
,
1843 human_size(sz
* 512));
1844 sz
= blocks_per_member(map
);
1845 printf(" Per Dev Size : %llu%s\n",
1846 (unsigned long long)sz
* 512 / super
->sector_size
,
1847 human_size(sz
* 512));
1848 printf(" Sector Offset : %llu\n",
1849 pba_of_lba0(map
) * 512 / super
->sector_size
);
1850 printf(" Num Stripes : %llu\n",
1851 num_data_stripes(map
));
1852 printf(" Chunk Size : %u KiB",
1853 __le16_to_cpu(map
->blocks_per_strip
) / 2);
1855 printf(" <-- %u KiB",
1856 __le16_to_cpu(map2
->blocks_per_strip
) / 2);
1858 printf(" Reserved : %d\n", __le32_to_cpu(dev
->reserved_blocks
));
1859 printf(" Migrate State : ");
1860 if (dev
->vol
.migr_state
) {
1861 if (migr_type(dev
) == MIGR_INIT
)
1862 printf("initialize\n");
1863 else if (migr_type(dev
) == MIGR_REBUILD
)
1864 printf("rebuild\n");
1865 else if (migr_type(dev
) == MIGR_VERIFY
)
1867 else if (migr_type(dev
) == MIGR_GEN_MIGR
)
1868 printf("general migration\n");
1869 else if (migr_type(dev
) == MIGR_STATE_CHANGE
)
1870 printf("state change\n");
1871 else if (migr_type(dev
) == MIGR_REPAIR
)
1874 printf("<unknown:%d>\n", migr_type(dev
));
1877 printf(" Map State : %s", map_state_str
[map
->map_state
]);
1878 if (dev
->vol
.migr_state
) {
1879 struct imsm_map
*map
= get_imsm_map(dev
, MAP_1
);
1881 printf(" <-- %s", map_state_str
[map
->map_state
]);
1882 printf("\n Checkpoint : %llu ", vol_curr_migr_unit(dev
));
1883 if (is_gen_migration(dev
) && (slot
> 1 || slot
< 0))
1886 printf("(%llu)", (unsigned long long)
1887 blocks_per_migr_unit(super
, dev
));
1890 printf(" Dirty State : %s\n", (dev
->vol
.dirty
& RAIDVOL_DIRTY
) ?
1892 printf(" RWH Policy : ");
1893 if (dev
->rwh_policy
== RWH_OFF
|| dev
->rwh_policy
== RWH_MULTIPLE_OFF
)
1895 else if (dev
->rwh_policy
== RWH_DISTRIBUTED
)
1896 printf("PPL distributed\n");
1897 else if (dev
->rwh_policy
== RWH_JOURNALING_DRIVE
)
1898 printf("PPL journaling drive\n");
1899 else if (dev
->rwh_policy
== RWH_MULTIPLE_DISTRIBUTED
)
1900 printf("Multiple distributed PPLs\n");
1901 else if (dev
->rwh_policy
== RWH_MULTIPLE_PPLS_JOURNALING_DRIVE
)
1902 printf("Multiple PPLs on journaling drive\n");
1903 else if (dev
->rwh_policy
== RWH_BITMAP
)
1904 printf("Write-intent bitmap\n");
1906 printf("<unknown:%d>\n", dev
->rwh_policy
);
1908 printf(" Volume ID : %u\n", dev
->my_vol_raid_dev_num
);
1911 static void print_imsm_disk(struct imsm_disk
*disk
,
1914 unsigned int sector_size
) {
1915 char str
[MAX_RAID_SERIAL_LEN
+ 1];
1918 if (index
< -1 || !disk
)
1922 snprintf(str
, MAX_RAID_SERIAL_LEN
+ 1, "%s", disk
->serial
);
1924 printf(" Disk%02d Serial : %s\n", index
, str
);
1926 printf(" Disk Serial : %s\n", str
);
1927 printf(" State :%s%s%s%s\n", is_spare(disk
) ? " spare" : "",
1928 is_configured(disk
) ? " active" : "",
1929 is_failed(disk
) ? " failed" : "",
1930 is_journal(disk
) ? " journal" : "");
1931 printf(" Id : %08x\n", __le32_to_cpu(disk
->scsi_id
));
1932 sz
= total_blocks(disk
) - reserved
;
1933 printf(" Usable Size : %llu%s\n",
1934 (unsigned long long)sz
* 512 / sector_size
,
1935 human_size(sz
* 512));
1938 void convert_to_4k_imsm_migr_rec(struct intel_super
*super
)
1940 struct migr_record
*migr_rec
= super
->migr_rec
;
1942 migr_rec
->blocks_per_unit
/= IMSM_4K_DIV
;
1943 migr_rec
->dest_depth_per_unit
/= IMSM_4K_DIV
;
1944 split_ull((join_u32(migr_rec
->post_migr_vol_cap
,
1945 migr_rec
->post_migr_vol_cap_hi
) / IMSM_4K_DIV
),
1946 &migr_rec
->post_migr_vol_cap
, &migr_rec
->post_migr_vol_cap_hi
);
1947 set_migr_chkp_area_pba(migr_rec
,
1948 migr_chkp_area_pba(migr_rec
) / IMSM_4K_DIV
);
1949 set_migr_dest_1st_member_lba(migr_rec
,
1950 migr_dest_1st_member_lba(migr_rec
) / IMSM_4K_DIV
);
1953 void convert_to_4k_imsm_disk(struct imsm_disk
*disk
)
1955 set_total_blocks(disk
, (total_blocks(disk
)/IMSM_4K_DIV
));
1958 void convert_to_4k(struct intel_super
*super
)
1960 struct imsm_super
*mpb
= super
->anchor
;
1961 struct imsm_disk
*disk
;
1963 __u32 bbm_log_size
= __le32_to_cpu(mpb
->bbm_log_size
);
1965 for (i
= 0; i
< mpb
->num_disks
; i
++) {
1966 disk
= __get_imsm_disk(mpb
, i
);
1968 convert_to_4k_imsm_disk(disk
);
1970 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
1971 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
1972 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
1974 set_imsm_dev_size(dev
, imsm_dev_size(dev
)/IMSM_4K_DIV
);
1975 set_vol_curr_migr_unit(dev
,
1976 vol_curr_migr_unit(dev
) / IMSM_4K_DIV
);
1979 set_blocks_per_member(map
, blocks_per_member(map
)/IMSM_4K_DIV
);
1980 map
->blocks_per_strip
/= IMSM_4K_DIV
;
1981 set_pba_of_lba0(map
, pba_of_lba0(map
)/IMSM_4K_DIV
);
1983 if (dev
->vol
.migr_state
) {
1985 map
= get_imsm_map(dev
, MAP_1
);
1986 set_blocks_per_member(map
,
1987 blocks_per_member(map
)/IMSM_4K_DIV
);
1988 map
->blocks_per_strip
/= IMSM_4K_DIV
;
1989 set_pba_of_lba0(map
, pba_of_lba0(map
)/IMSM_4K_DIV
);
1993 struct bbm_log
*log
= (void *)mpb
+
1994 __le32_to_cpu(mpb
->mpb_size
) - bbm_log_size
;
1997 for (i
= 0; i
< log
->entry_count
; i
++) {
1998 struct bbm_log_entry
*entry
=
1999 &log
->marked_block_entries
[i
];
2001 __u8 count
= entry
->marked_count
+ 1;
2002 unsigned long long sector
=
2003 __le48_to_cpu(&entry
->defective_block_start
);
2005 entry
->defective_block_start
=
2006 __cpu_to_le48(sector
/IMSM_4K_DIV
);
2007 entry
->marked_count
= max(count
/IMSM_4K_DIV
, 1) - 1;
2011 mpb
->check_sum
= __gen_imsm_checksum(mpb
);
2014 void examine_migr_rec_imsm(struct intel_super
*super
)
2016 struct migr_record
*migr_rec
= super
->migr_rec
;
2017 struct imsm_super
*mpb
= super
->anchor
;
2020 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
2021 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
2022 struct imsm_map
*map
;
2025 if (is_gen_migration(dev
) == false)
2028 printf("\nMigration Record Information:");
2030 /* first map under migration */
2031 map
= get_imsm_map(dev
, MAP_0
);
2034 slot
= get_imsm_disk_slot(map
, super
->disks
->index
);
2035 if (map
== NULL
|| slot
> 1 || slot
< 0) {
2036 printf(" Empty\n ");
2037 printf("Examine one of first two disks in array\n");
2040 printf("\n Status : ");
2041 if (__le32_to_cpu(migr_rec
->rec_status
) == UNIT_SRC_NORMAL
)
2044 printf("Contains Data\n");
2045 printf(" Current Unit : %llu\n",
2046 current_migr_unit(migr_rec
));
2047 printf(" Family : %u\n",
2048 __le32_to_cpu(migr_rec
->family_num
));
2049 printf(" Ascending : %u\n",
2050 __le32_to_cpu(migr_rec
->ascending_migr
));
2051 printf(" Blocks Per Unit : %u\n",
2052 __le32_to_cpu(migr_rec
->blocks_per_unit
));
2053 printf(" Dest. Depth Per Unit : %u\n",
2054 __le32_to_cpu(migr_rec
->dest_depth_per_unit
));
2055 printf(" Checkpoint Area pba : %llu\n",
2056 migr_chkp_area_pba(migr_rec
));
2057 printf(" First member lba : %llu\n",
2058 migr_dest_1st_member_lba(migr_rec
));
2059 printf(" Total Number of Units : %llu\n",
2060 get_num_migr_units(migr_rec
));
2061 printf(" Size of volume : %llu\n",
2062 join_u32(migr_rec
->post_migr_vol_cap
,
2063 migr_rec
->post_migr_vol_cap_hi
));
2064 printf(" Record was read from : %u\n",
2065 __le32_to_cpu(migr_rec
->ckpt_read_disk_num
));
2071 void convert_from_4k_imsm_migr_rec(struct intel_super
*super
)
2073 struct migr_record
*migr_rec
= super
->migr_rec
;
2075 migr_rec
->blocks_per_unit
*= IMSM_4K_DIV
;
2076 migr_rec
->dest_depth_per_unit
*= IMSM_4K_DIV
;
2077 split_ull((join_u32(migr_rec
->post_migr_vol_cap
,
2078 migr_rec
->post_migr_vol_cap_hi
) * IMSM_4K_DIV
),
2079 &migr_rec
->post_migr_vol_cap
,
2080 &migr_rec
->post_migr_vol_cap_hi
);
2081 set_migr_chkp_area_pba(migr_rec
,
2082 migr_chkp_area_pba(migr_rec
) * IMSM_4K_DIV
);
2083 set_migr_dest_1st_member_lba(migr_rec
,
2084 migr_dest_1st_member_lba(migr_rec
) * IMSM_4K_DIV
);
2087 void convert_from_4k(struct intel_super
*super
)
2089 struct imsm_super
*mpb
= super
->anchor
;
2090 struct imsm_disk
*disk
;
2092 __u32 bbm_log_size
= __le32_to_cpu(mpb
->bbm_log_size
);
2094 for (i
= 0; i
< mpb
->num_disks
; i
++) {
2095 disk
= __get_imsm_disk(mpb
, i
);
2097 set_total_blocks(disk
, (total_blocks(disk
)*IMSM_4K_DIV
));
2100 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
2101 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
2102 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
2104 set_imsm_dev_size(dev
, imsm_dev_size(dev
)*IMSM_4K_DIV
);
2105 set_vol_curr_migr_unit(dev
,
2106 vol_curr_migr_unit(dev
) * IMSM_4K_DIV
);
2109 set_blocks_per_member(map
, blocks_per_member(map
)*IMSM_4K_DIV
);
2110 map
->blocks_per_strip
*= IMSM_4K_DIV
;
2111 set_pba_of_lba0(map
, pba_of_lba0(map
)*IMSM_4K_DIV
);
2113 if (dev
->vol
.migr_state
) {
2115 map
= get_imsm_map(dev
, MAP_1
);
2116 set_blocks_per_member(map
,
2117 blocks_per_member(map
)*IMSM_4K_DIV
);
2118 map
->blocks_per_strip
*= IMSM_4K_DIV
;
2119 set_pba_of_lba0(map
, pba_of_lba0(map
)*IMSM_4K_DIV
);
2123 struct bbm_log
*log
= (void *)mpb
+
2124 __le32_to_cpu(mpb
->mpb_size
) - bbm_log_size
;
2127 for (i
= 0; i
< log
->entry_count
; i
++) {
2128 struct bbm_log_entry
*entry
=
2129 &log
->marked_block_entries
[i
];
2131 __u8 count
= entry
->marked_count
+ 1;
2132 unsigned long long sector
=
2133 __le48_to_cpu(&entry
->defective_block_start
);
2135 entry
->defective_block_start
=
2136 __cpu_to_le48(sector
*IMSM_4K_DIV
);
2137 entry
->marked_count
= count
*IMSM_4K_DIV
- 1;
2141 mpb
->check_sum
= __gen_imsm_checksum(mpb
);
2144 /*******************************************************************************
2145 * function: imsm_check_attributes
2146 * Description: Function checks if features represented by attributes flags
2147 * are supported by mdadm.
2149 * attributes - Attributes read from metadata
2151 * 0 - passed attributes contains unsupported features flags
2152 * 1 - all features are supported
2153 ******************************************************************************/
2154 static int imsm_check_attributes(__u32 attributes
)
2157 __u32 not_supported
= MPB_ATTRIB_SUPPORTED
^0xffffffff;
2159 not_supported
&= ~MPB_ATTRIB_IGNORED
;
2161 not_supported
&= attributes
;
2162 if (not_supported
) {
2163 pr_err("(IMSM): Unsupported attributes : %x\n",
2164 (unsigned)__le32_to_cpu(not_supported
));
2165 if (not_supported
& MPB_ATTRIB_CHECKSUM_VERIFY
) {
2166 dprintf("\t\tMPB_ATTRIB_CHECKSUM_VERIFY \n");
2167 not_supported
^= MPB_ATTRIB_CHECKSUM_VERIFY
;
2169 if (not_supported
& MPB_ATTRIB_2TB
) {
2170 dprintf("\t\tMPB_ATTRIB_2TB\n");
2171 not_supported
^= MPB_ATTRIB_2TB
;
2173 if (not_supported
& MPB_ATTRIB_RAID0
) {
2174 dprintf("\t\tMPB_ATTRIB_RAID0\n");
2175 not_supported
^= MPB_ATTRIB_RAID0
;
2177 if (not_supported
& MPB_ATTRIB_RAID1
) {
2178 dprintf("\t\tMPB_ATTRIB_RAID1\n");
2179 not_supported
^= MPB_ATTRIB_RAID1
;
2181 if (not_supported
& MPB_ATTRIB_RAID10
) {
2182 dprintf("\t\tMPB_ATTRIB_RAID10\n");
2183 not_supported
^= MPB_ATTRIB_RAID10
;
2185 if (not_supported
& MPB_ATTRIB_RAID1E
) {
2186 dprintf("\t\tMPB_ATTRIB_RAID1E\n");
2187 not_supported
^= MPB_ATTRIB_RAID1E
;
2189 if (not_supported
& MPB_ATTRIB_RAID5
) {
2190 dprintf("\t\tMPB_ATTRIB_RAID5\n");
2191 not_supported
^= MPB_ATTRIB_RAID5
;
2193 if (not_supported
& MPB_ATTRIB_RAIDCNG
) {
2194 dprintf("\t\tMPB_ATTRIB_RAIDCNG\n");
2195 not_supported
^= MPB_ATTRIB_RAIDCNG
;
2197 if (not_supported
& MPB_ATTRIB_BBM
) {
2198 dprintf("\t\tMPB_ATTRIB_BBM\n");
2199 not_supported
^= MPB_ATTRIB_BBM
;
2201 if (not_supported
& MPB_ATTRIB_CHECKSUM_VERIFY
) {
2202 dprintf("\t\tMPB_ATTRIB_CHECKSUM_VERIFY (== MPB_ATTRIB_LEGACY)\n");
2203 not_supported
^= MPB_ATTRIB_CHECKSUM_VERIFY
;
2205 if (not_supported
& MPB_ATTRIB_EXP_STRIPE_SIZE
) {
2206 dprintf("\t\tMPB_ATTRIB_EXP_STRIP_SIZE\n");
2207 not_supported
^= MPB_ATTRIB_EXP_STRIPE_SIZE
;
2209 if (not_supported
& MPB_ATTRIB_2TB_DISK
) {
2210 dprintf("\t\tMPB_ATTRIB_2TB_DISK\n");
2211 not_supported
^= MPB_ATTRIB_2TB_DISK
;
2213 if (not_supported
& MPB_ATTRIB_NEVER_USE2
) {
2214 dprintf("\t\tMPB_ATTRIB_NEVER_USE2\n");
2215 not_supported
^= MPB_ATTRIB_NEVER_USE2
;
2217 if (not_supported
& MPB_ATTRIB_NEVER_USE
) {
2218 dprintf("\t\tMPB_ATTRIB_NEVER_USE\n");
2219 not_supported
^= MPB_ATTRIB_NEVER_USE
;
2223 dprintf("(IMSM): Unknown attributes : %x\n", not_supported
);
2231 static void getinfo_super_imsm(struct supertype
*st
, struct mdinfo
*info
, char *map
);
2233 static void examine_super_imsm(struct supertype
*st
, char *homehost
)
2235 struct intel_super
*super
= st
->sb
;
2236 struct imsm_super
*mpb
= super
->anchor
;
2237 char str
[MAX_SIGNATURE_LENGTH
];
2242 __u32 reserved
= imsm_reserved_sectors(super
, super
->disks
);
2244 time_t creation_time
;
2246 strncpy(str
, (char *)mpb
->sig
, MPB_SIG_LEN
);
2247 str
[MPB_SIG_LEN
-1] = '\0';
2248 printf(" Magic : %s\n", str
);
2249 printf(" Version : %s\n", get_imsm_version(mpb
));
2250 printf(" Orig Family : %08x\n", __le32_to_cpu(mpb
->orig_family_num
));
2251 printf(" Family : %08x\n", __le32_to_cpu(mpb
->family_num
));
2252 printf(" Generation : %08x\n", __le32_to_cpu(mpb
->generation_num
));
2253 creation_time
= __le64_to_cpu(mpb
->creation_time
);
2254 printf(" Creation Time : %.24s\n",
2255 creation_time
? ctime(&creation_time
) : "Unknown");
2256 printf(" Attributes : ");
2257 if (imsm_check_attributes(mpb
->attributes
))
2258 printf("All supported\n");
2260 printf("not supported\n");
2261 getinfo_super_imsm(st
, &info
, NULL
);
2262 fname_from_uuid(st
, &info
, nbuf
, ':');
2263 printf(" UUID : %s\n", nbuf
+ 5);
2264 sum
= __le32_to_cpu(mpb
->check_sum
);
2265 printf(" Checksum : %08x %s\n", sum
,
2266 __gen_imsm_checksum(mpb
) == sum
? "correct" : "incorrect");
2267 printf(" MPB Sectors : %d\n", mpb_sectors(mpb
, super
->sector_size
));
2268 printf(" Disks : %d\n", mpb
->num_disks
);
2269 printf(" RAID Devices : %d\n", mpb
->num_raid_devs
);
2270 print_imsm_disk(__get_imsm_disk(mpb
, super
->disks
->index
),
2271 super
->disks
->index
, reserved
, super
->sector_size
);
2272 if (get_imsm_bbm_log_size(super
->bbm_log
)) {
2273 struct bbm_log
*log
= super
->bbm_log
;
2276 printf("Bad Block Management Log:\n");
2277 printf(" Log Size : %d\n", __le32_to_cpu(mpb
->bbm_log_size
));
2278 printf(" Signature : %x\n", __le32_to_cpu(log
->signature
));
2279 printf(" Entry Count : %d\n", __le32_to_cpu(log
->entry_count
));
2281 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
2283 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
2285 super
->current_vol
= i
;
2286 getinfo_super_imsm(st
, &info
, NULL
);
2287 fname_from_uuid(st
, &info
, nbuf
, ':');
2288 print_imsm_dev(super
, dev
, nbuf
+ 5, super
->disks
->index
);
2290 for (i
= 0; i
< mpb
->num_disks
; i
++) {
2291 if (i
== super
->disks
->index
)
2293 print_imsm_disk(__get_imsm_disk(mpb
, i
), i
, reserved
,
2294 super
->sector_size
);
2297 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
2298 if (dl
->index
== -1)
2299 print_imsm_disk(&dl
->disk
, -1, reserved
,
2300 super
->sector_size
);
2302 examine_migr_rec_imsm(super
);
2305 static void brief_examine_super_imsm(struct supertype
*st
, int verbose
)
2307 /* We just write a generic IMSM ARRAY entry */
2311 getinfo_super_imsm(st
, &info
, NULL
);
2312 fname_from_uuid(st
, &info
, nbuf
, ':');
2313 printf("ARRAY metadata=imsm UUID=%s\n", nbuf
+ 5);
2316 static void brief_examine_subarrays_imsm(struct supertype
*st
, int verbose
)
2318 /* We just write a generic IMSM ARRAY entry */
2322 struct intel_super
*super
= st
->sb
;
2325 if (!super
->anchor
->num_raid_devs
)
2328 getinfo_super_imsm(st
, &info
, NULL
);
2329 fname_from_uuid(st
, &info
, nbuf
, ':');
2330 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
2331 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
2333 super
->current_vol
= i
;
2334 getinfo_super_imsm(st
, &info
, NULL
);
2335 fname_from_uuid(st
, &info
, nbuf1
, ':');
2336 printf("ARRAY " DEV_MD_DIR
"%.16s container=%s member=%d UUID=%s\n",
2337 dev
->volume
, nbuf
+ 5, i
, nbuf1
+ 5);
2341 static void export_examine_super_imsm(struct supertype
*st
)
2343 struct intel_super
*super
= st
->sb
;
2344 struct imsm_super
*mpb
= super
->anchor
;
2348 getinfo_super_imsm(st
, &info
, NULL
);
2349 fname_from_uuid(st
, &info
, nbuf
, ':');
2350 printf("MD_METADATA=imsm\n");
2351 printf("MD_LEVEL=container\n");
2352 printf("MD_UUID=%s\n", nbuf
+5);
2353 printf("MD_DEVICES=%u\n", mpb
->num_disks
);
2354 printf("MD_CREATION_TIME=%llu\n", __le64_to_cpu(mpb
->creation_time
));
2357 static void detail_super_imsm(struct supertype
*st
, char *homehost
,
2362 struct intel_super
*super
= st
->sb
;
2363 int temp_vol
= super
->current_vol
;
2366 super
->current_vol
= strtoul(subarray
, NULL
, 10);
2368 getinfo_super_imsm(st
, &info
, NULL
);
2369 fname_from_uuid(st
, &info
, nbuf
, ':');
2370 printf("\n UUID : %s\n", nbuf
+ 5);
2372 super
->current_vol
= temp_vol
;
2375 static void brief_detail_super_imsm(struct supertype
*st
, char *subarray
)
2379 struct intel_super
*super
= st
->sb
;
2380 int temp_vol
= super
->current_vol
;
2383 super
->current_vol
= strtoul(subarray
, NULL
, 10);
2385 getinfo_super_imsm(st
, &info
, NULL
);
2386 fname_from_uuid(st
, &info
, nbuf
, ':');
2387 printf(" UUID=%s", nbuf
+ 5);
2389 super
->current_vol
= temp_vol
;
2392 static int imsm_read_serial(int fd
, char *devname
, __u8
*serial
,
2393 size_t serial_buf_len
);
2394 static void fd2devname(int fd
, char *name
);
2396 static int ahci_enumerate_ports(const char *hba_path
, int port_count
, int host_base
, int verbose
)
2398 /* dump an unsorted list of devices attached to AHCI Intel storage
2399 * controller, as well as non-connected ports
2401 int hba_len
= strlen(hba_path
) + 1;
2406 unsigned long port_mask
= (1 << port_count
) - 1;
2408 if (port_count
> (int)sizeof(port_mask
) * 8) {
2410 pr_err("port_count %d out of range\n", port_count
);
2414 /* scroll through /sys/dev/block looking for devices attached to
2417 dir
= opendir("/sys/dev/block");
2421 for (ent
= readdir(dir
); ent
; ent
= readdir(dir
)) {
2427 char device
[PATH_MAX
];
2432 if (sscanf(ent
->d_name
, "%d:%d", &major
, &minor
) != 2)
2434 path
= devt_to_devpath(makedev(major
, minor
), 1, NULL
);
2437 if (!path_attached_to_hba(path
, hba_path
)) {
2443 /* retrieve the scsi device */
2444 if (!devt_to_devpath(makedev(major
, minor
), 1, device
)) {
2446 pr_err("failed to get device\n");
2450 if (devpath_to_char(device
, "type", buf
, sizeof(buf
), 0)) {
2454 type
= strtoul(buf
, NULL
, 10);
2456 /* if it's not a disk print the vendor and model */
2457 if (!(type
== 0 || type
== 7 || type
== 14)) {
2461 if (devpath_to_char(device
, "vendor", buf
,
2462 sizeof(buf
), 0) == 0) {
2463 strncpy(vendor
, buf
, sizeof(vendor
));
2464 vendor
[sizeof(vendor
) - 1] = '\0';
2465 c
= (char *) &vendor
[sizeof(vendor
) - 1];
2466 while (isspace(*c
) || *c
== '\0')
2471 if (devpath_to_char(device
, "model", buf
,
2472 sizeof(buf
), 0) == 0) {
2473 strncpy(model
, buf
, sizeof(model
));
2474 model
[sizeof(model
) - 1] = '\0';
2475 c
= (char *) &model
[sizeof(model
) - 1];
2476 while (isspace(*c
) || *c
== '\0')
2480 if (vendor
[0] && model
[0])
2481 sprintf(buf
, "%.64s %.64s", vendor
, model
);
2483 switch (type
) { /* numbers from hald/linux/device.c */
2484 case 1: sprintf(buf
, "tape"); break;
2485 case 2: sprintf(buf
, "printer"); break;
2486 case 3: sprintf(buf
, "processor"); break;
2488 case 5: sprintf(buf
, "cdrom"); break;
2489 case 6: sprintf(buf
, "scanner"); break;
2490 case 8: sprintf(buf
, "media_changer"); break;
2491 case 9: sprintf(buf
, "comm"); break;
2492 case 12: sprintf(buf
, "raid"); break;
2493 default: sprintf(buf
, "unknown");
2498 /* chop device path to 'host%d' and calculate the port number */
2499 c
= strchr(&path
[hba_len
], '/');
2502 pr_err("%s - invalid path name\n", path
+ hba_len
);
2507 if ((sscanf(&path
[hba_len
], "ata%d", &port
) == 1) ||
2508 ((sscanf(&path
[hba_len
], "host%d", &port
) == 1)))
2512 *c
= '/'; /* repair the full string */
2513 pr_err("failed to determine port number for %s\n",
2520 /* mark this port as used */
2521 port_mask
&= ~(1 << port
);
2523 /* print out the device information */
2525 printf(" Port%d : - non-disk device (%s) -\n", port
, buf
);
2529 fd
= dev_open(ent
->d_name
, O_RDONLY
);
2530 if (!is_fd_valid(fd
))
2531 printf(" Port%d : - disk info unavailable -\n", port
);
2533 fd2devname(fd
, buf
);
2534 printf(" Port%d : %s", port
, buf
);
2535 if (imsm_read_serial(fd
, NULL
, (__u8
*)buf
,
2537 printf(" (%s)\n", buf
);
2552 for (i
= 0; i
< port_count
; i
++)
2553 if (port_mask
& (1 << i
))
2554 printf(" Port%d : - no device attached -\n", i
);
2560 static int print_nvme_info(struct sys_dev
*hba
)
2565 dir
= opendir("/sys/block/");
2569 for (ent
= readdir(dir
); ent
; ent
= readdir(dir
)) {
2570 char ns_path
[PATH_MAX
];
2571 char cntrl_path
[PATH_MAX
];
2575 if (!strstr(ent
->d_name
, "nvme"))
2578 fd
= open_dev(ent
->d_name
);
2579 if (!is_fd_valid(fd
))
2582 if (!diskfd_to_devpath(fd
, 0, ns_path
) ||
2583 !diskfd_to_devpath(fd
, 1, cntrl_path
))
2586 if (!path_attached_to_hba(cntrl_path
, hba
->path
))
2589 if (!imsm_is_nvme_namespace_supported(fd
, 0))
2592 fd2devname(fd
, buf
);
2593 if (hba
->type
== SYS_DEV_VMD
)
2594 printf(" NVMe under VMD : %s", buf
);
2595 else if (hba
->type
== SYS_DEV_NVME
)
2596 printf(" NVMe Device : %s", buf
);
2598 if (!imsm_read_serial(fd
, NULL
, (__u8
*)buf
,
2600 printf(" (%s)\n", buf
);
2612 static void print_found_intel_controllers(struct sys_dev
*elem
)
2614 for (; elem
; elem
= elem
->next
) {
2615 pr_err("found Intel(R) ");
2616 if (elem
->type
== SYS_DEV_SATA
)
2617 fprintf(stderr
, "SATA ");
2618 else if (elem
->type
== SYS_DEV_SAS
)
2619 fprintf(stderr
, "SAS ");
2620 else if (elem
->type
== SYS_DEV_NVME
)
2621 fprintf(stderr
, "NVMe ");
2623 if (elem
->type
== SYS_DEV_VMD
)
2624 fprintf(stderr
, "VMD domain");
2625 else if (elem
->type
== SYS_DEV_SATA_VMD
)
2626 fprintf(stderr
, "SATA VMD domain");
2628 fprintf(stderr
, "RAID controller");
2631 fprintf(stderr
, " at %s", elem
->pci_id
);
2632 fprintf(stderr
, ".\n");
2637 static int ahci_get_port_count(const char *hba_path
, int *port_count
)
2644 if ((dir
= opendir(hba_path
)) == NULL
)
2647 for (ent
= readdir(dir
); ent
; ent
= readdir(dir
)) {
2650 if ((sscanf(ent
->d_name
, "ata%d", &host
) != 1) &&
2651 ((sscanf(ent
->d_name
, "host%d", &host
) != 1)))
2653 if (*port_count
== 0)
2655 else if (host
< host_base
)
2658 if (host
+ 1 > *port_count
+ host_base
)
2659 *port_count
= host
+ 1 - host_base
;
2665 static void print_imsm_capability(const struct imsm_orom
*orom
)
2667 printf(" Platform : Intel(R) ");
2668 if (orom
->capabilities
== 0 && orom
->driver_features
== 0)
2669 printf("Matrix Storage Manager\n");
2670 else if (imsm_orom_is_enterprise(orom
) && orom
->major_ver
>= 6)
2671 printf("Virtual RAID on CPU\n");
2673 printf("Rapid Storage Technology%s\n",
2674 imsm_orom_is_enterprise(orom
) ? " enterprise" : "");
2675 if (orom
->major_ver
|| orom
->minor_ver
|| orom
->hotfix_ver
|| orom
->build
) {
2676 if (imsm_orom_is_vmd_without_efi(orom
))
2677 printf(" Version : %d.%d\n", orom
->major_ver
,
2680 printf(" Version : %d.%d.%d.%d\n", orom
->major_ver
,
2681 orom
->minor_ver
, orom
->hotfix_ver
, orom
->build
);
2683 printf(" RAID Levels :%s%s%s%s%s\n",
2684 imsm_orom_has_raid0(orom
) ? " raid0" : "",
2685 imsm_orom_has_raid1(orom
) ? " raid1" : "",
2686 imsm_orom_has_raid1e(orom
) ? " raid1e" : "",
2687 imsm_orom_has_raid10(orom
) ? " raid10" : "",
2688 imsm_orom_has_raid5(orom
) ? " raid5" : "");
2689 printf(" Chunk Sizes :%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
2690 imsm_orom_has_chunk(orom
, 2) ? " 2k" : "",
2691 imsm_orom_has_chunk(orom
, 4) ? " 4k" : "",
2692 imsm_orom_has_chunk(orom
, 8) ? " 8k" : "",
2693 imsm_orom_has_chunk(orom
, 16) ? " 16k" : "",
2694 imsm_orom_has_chunk(orom
, 32) ? " 32k" : "",
2695 imsm_orom_has_chunk(orom
, 64) ? " 64k" : "",
2696 imsm_orom_has_chunk(orom
, 128) ? " 128k" : "",
2697 imsm_orom_has_chunk(orom
, 256) ? " 256k" : "",
2698 imsm_orom_has_chunk(orom
, 512) ? " 512k" : "",
2699 imsm_orom_has_chunk(orom
, 1024*1) ? " 1M" : "",
2700 imsm_orom_has_chunk(orom
, 1024*2) ? " 2M" : "",
2701 imsm_orom_has_chunk(orom
, 1024*4) ? " 4M" : "",
2702 imsm_orom_has_chunk(orom
, 1024*8) ? " 8M" : "",
2703 imsm_orom_has_chunk(orom
, 1024*16) ? " 16M" : "",
2704 imsm_orom_has_chunk(orom
, 1024*32) ? " 32M" : "",
2705 imsm_orom_has_chunk(orom
, 1024*64) ? " 64M" : "");
2706 printf(" 2TB volumes :%s supported\n",
2707 (orom
->attr
& IMSM_OROM_ATTR_2TB
)?"":" not");
2708 printf(" 2TB disks :%s supported\n",
2709 (orom
->attr
& IMSM_OROM_ATTR_2TB_DISK
)?"":" not");
2710 printf(" Max Disks : %d\n", orom
->tds
);
2711 printf(" Max Volumes : %d per array, %d per %s\n",
2712 orom
->vpa
, orom
->vphba
,
2713 imsm_orom_is_nvme(orom
) ? "platform" : "controller");
2717 static void print_imsm_capability_export(const struct imsm_orom
*orom
)
2719 printf("MD_FIRMWARE_TYPE=imsm\n");
2720 if (orom
->major_ver
|| orom
->minor_ver
|| orom
->hotfix_ver
|| orom
->build
)
2721 printf("IMSM_VERSION=%d.%d.%d.%d\n", orom
->major_ver
, orom
->minor_ver
,
2722 orom
->hotfix_ver
, orom
->build
);
2723 printf("IMSM_SUPPORTED_RAID_LEVELS=%s%s%s%s%s\n",
2724 imsm_orom_has_raid0(orom
) ? "raid0 " : "",
2725 imsm_orom_has_raid1(orom
) ? "raid1 " : "",
2726 imsm_orom_has_raid1e(orom
) ? "raid1e " : "",
2727 imsm_orom_has_raid5(orom
) ? "raid10 " : "",
2728 imsm_orom_has_raid10(orom
) ? "raid5 " : "");
2729 printf("IMSM_SUPPORTED_CHUNK_SIZES=%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
2730 imsm_orom_has_chunk(orom
, 2) ? "2k " : "",
2731 imsm_orom_has_chunk(orom
, 4) ? "4k " : "",
2732 imsm_orom_has_chunk(orom
, 8) ? "8k " : "",
2733 imsm_orom_has_chunk(orom
, 16) ? "16k " : "",
2734 imsm_orom_has_chunk(orom
, 32) ? "32k " : "",
2735 imsm_orom_has_chunk(orom
, 64) ? "64k " : "",
2736 imsm_orom_has_chunk(orom
, 128) ? "128k " : "",
2737 imsm_orom_has_chunk(orom
, 256) ? "256k " : "",
2738 imsm_orom_has_chunk(orom
, 512) ? "512k " : "",
2739 imsm_orom_has_chunk(orom
, 1024*1) ? "1M " : "",
2740 imsm_orom_has_chunk(orom
, 1024*2) ? "2M " : "",
2741 imsm_orom_has_chunk(orom
, 1024*4) ? "4M " : "",
2742 imsm_orom_has_chunk(orom
, 1024*8) ? "8M " : "",
2743 imsm_orom_has_chunk(orom
, 1024*16) ? "16M " : "",
2744 imsm_orom_has_chunk(orom
, 1024*32) ? "32M " : "",
2745 imsm_orom_has_chunk(orom
, 1024*64) ? "64M " : "");
2746 printf("IMSM_2TB_VOLUMES=%s\n",(orom
->attr
& IMSM_OROM_ATTR_2TB
) ? "yes" : "no");
2747 printf("IMSM_2TB_DISKS=%s\n",(orom
->attr
& IMSM_OROM_ATTR_2TB_DISK
) ? "yes" : "no");
2748 printf("IMSM_MAX_DISKS=%d\n",orom
->tds
);
2749 printf("IMSM_MAX_VOLUMES_PER_ARRAY=%d\n",orom
->vpa
);
2750 printf("IMSM_MAX_VOLUMES_PER_CONTROLLER=%d\n",orom
->vphba
);
2753 static int detail_platform_imsm(int verbose
, int enumerate_only
, char *controller_path
)
2755 /* There are two components to imsm platform support, the ahci SATA
2756 * controller and the option-rom. To find the SATA controller we
2757 * simply look in /sys/bus/pci/drivers/ahci to see if an ahci
2758 * controller with the Intel vendor id is present. This approach
2759 * allows mdadm to leverage the kernel's ahci detection logic, with the
2760 * caveat that if ahci.ko is not loaded mdadm will not be able to
2761 * detect platform raid capabilities. The option-rom resides in a
2762 * platform "Adapter ROM". We scan for its signature to retrieve the
2763 * platform capabilities. If raid support is disabled in the BIOS the
2764 * option-rom capability structure will not be available.
2766 struct sys_dev
*list
, *hba
;
2771 if (enumerate_only
) {
2772 if (check_no_platform())
2774 list
= find_intel_devices();
2777 for (hba
= list
; hba
; hba
= hba
->next
) {
2778 if (find_imsm_capability(hba
)) {
2788 list
= find_intel_devices();
2791 pr_err("no active Intel(R) RAID controller found.\n");
2793 } else if (verbose
> 0)
2794 print_found_intel_controllers(list
);
2796 for (hba
= list
; hba
; hba
= hba
->next
) {
2797 if (controller_path
&& (compare_paths(hba
->path
, controller_path
) != 0))
2799 if (!find_imsm_capability(hba
)) {
2801 pr_err("imsm capabilities not found for controller: %s (type %s)\n",
2802 hba
->type
== SYS_DEV_VMD
|| hba
->type
== SYS_DEV_SATA_VMD
?
2803 vmd_domain_to_controller(hba
, buf
) :
2804 hba
->path
, get_sys_dev_type(hba
->type
));
2810 if (controller_path
&& result
== 1) {
2811 pr_err("no active Intel(R) RAID controller found under %s\n",
2816 const struct orom_entry
*entry
;
2818 for (entry
= orom_entries
; entry
; entry
= entry
->next
) {
2819 if (entry
->type
== SYS_DEV_VMD
) {
2820 print_imsm_capability(&entry
->orom
);
2821 printf(" 3rd party NVMe :%s supported\n",
2822 imsm_orom_has_tpv_support(&entry
->orom
)?"":" not");
2823 for (hba
= list
; hba
; hba
= hba
->next
) {
2824 if (hba
->type
== SYS_DEV_VMD
) {
2826 printf(" I/O Controller : %s (%s)\n",
2827 vmd_domain_to_controller(hba
, buf
), get_sys_dev_type(hba
->type
));
2828 if (print_nvme_info(hba
)) {
2830 pr_err("failed to get devices attached to VMD domain.\n");
2839 print_imsm_capability(&entry
->orom
);
2840 if (entry
->type
== SYS_DEV_NVME
) {
2841 for (hba
= list
; hba
; hba
= hba
->next
) {
2842 if (hba
->type
== SYS_DEV_NVME
)
2843 print_nvme_info(hba
);
2849 struct devid_list
*devid
;
2850 for (devid
= entry
->devid_list
; devid
; devid
= devid
->next
) {
2851 hba
= device_by_id(devid
->devid
);
2855 printf(" I/O Controller : %s (%s)\n",
2856 hba
->path
, get_sys_dev_type(hba
->type
));
2857 if (hba
->type
== SYS_DEV_SATA
|| hba
->type
== SYS_DEV_SATA_VMD
) {
2858 host_base
= ahci_get_port_count(hba
->path
, &port_count
);
2859 if (ahci_enumerate_ports(hba
->path
, port_count
, host_base
, verbose
)) {
2861 pr_err("failed to enumerate ports on %s controller at %s.\n",
2862 get_sys_dev_type(hba
->type
), hba
->pci_id
);
2873 static int export_detail_platform_imsm(int verbose
, char *controller_path
)
2875 struct sys_dev
*list
, *hba
;
2878 list
= find_intel_devices();
2881 pr_err("IMSM_DETAIL_PLATFORM_ERROR=NO_INTEL_DEVICES\n");
2886 for (hba
= list
; hba
; hba
= hba
->next
) {
2887 if (controller_path
&& (compare_paths(hba
->path
,controller_path
) != 0))
2889 if (!find_imsm_capability(hba
) && verbose
> 0) {
2891 pr_err("IMSM_DETAIL_PLATFORM_ERROR=NO_IMSM_CAPABLE_DEVICE_UNDER_%s\n",
2892 hba
->type
== SYS_DEV_VMD
|| hba
->type
== SYS_DEV_SATA_VMD
?
2893 vmd_domain_to_controller(hba
, buf
) : hba
->path
);
2899 const struct orom_entry
*entry
;
2901 for (entry
= orom_entries
; entry
; entry
= entry
->next
) {
2902 if (entry
->type
== SYS_DEV_VMD
|| entry
->type
== SYS_DEV_SATA_VMD
) {
2903 for (hba
= list
; hba
; hba
= hba
->next
)
2904 print_imsm_capability_export(&entry
->orom
);
2907 print_imsm_capability_export(&entry
->orom
);
2913 static int match_home_imsm(struct supertype
*st
, char *homehost
)
2915 /* the imsm metadata format does not specify any host
2916 * identification information. We return -1 since we can never
2917 * confirm nor deny whether a given array is "meant" for this
2918 * host. We rely on compare_super and the 'family_num' fields to
2919 * exclude member disks that do not belong, and we rely on
2920 * mdadm.conf to specify the arrays that should be assembled.
2921 * Auto-assembly may still pick up "foreign" arrays.
2927 static void uuid_from_super_imsm(struct supertype
*st
, int uuid
[4])
2929 /* The uuid returned here is used for:
2930 * uuid to put into bitmap file (Create, Grow)
2931 * uuid for backup header when saving critical section (Grow)
2932 * comparing uuids when re-adding a device into an array
2933 * In these cases the uuid required is that of the data-array,
2934 * not the device-set.
2935 * uuid to recognise same set when adding a missing device back
2936 * to an array. This is a uuid for the device-set.
2938 * For each of these we can make do with a truncated
2939 * or hashed uuid rather than the original, as long as
2941 * In each case the uuid required is that of the data-array,
2942 * not the device-set.
2944 /* imsm does not track uuid's so we synthesis one using sha1 on
2945 * - The signature (Which is constant for all imsm array, but no matter)
2946 * - the orig_family_num of the container
2947 * - the index number of the volume
2948 * - the 'serial' number of the volume.
2949 * Hopefully these are all constant.
2951 struct intel_super
*super
= st
->sb
;
2954 struct sha1_ctx ctx
;
2955 struct imsm_dev
*dev
= NULL
;
2958 /* some mdadm versions failed to set ->orig_family_num, in which
2959 * case fall back to ->family_num. orig_family_num will be
2960 * fixed up with the first metadata update.
2962 family_num
= super
->anchor
->orig_family_num
;
2963 if (family_num
== 0)
2964 family_num
= super
->anchor
->family_num
;
2965 sha1_init_ctx(&ctx
);
2966 sha1_process_bytes(super
->anchor
->sig
, MPB_SIG_LEN
, &ctx
);
2967 sha1_process_bytes(&family_num
, sizeof(__u32
), &ctx
);
2968 if (super
->current_vol
>= 0)
2969 dev
= get_imsm_dev(super
, super
->current_vol
);
2971 __u32 vol
= super
->current_vol
;
2972 sha1_process_bytes(&vol
, sizeof(vol
), &ctx
);
2973 sha1_process_bytes(dev
->volume
, MAX_RAID_SERIAL_LEN
, &ctx
);
2975 sha1_finish_ctx(&ctx
, buf
);
2976 memcpy(uuid
, buf
, 4*4);
2979 static __u32
migr_strip_blocks_resync(struct imsm_dev
*dev
)
2981 /* migr_strip_size when repairing or initializing parity */
2982 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
2983 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
2985 switch (get_imsm_raid_level(map
)) {
2990 return 128*1024 >> 9;
2994 static __u32
migr_strip_blocks_rebuild(struct imsm_dev
*dev
)
2996 /* migr_strip_size when rebuilding a degraded disk, no idea why
2997 * this is different than migr_strip_size_resync(), but it's good
3000 struct imsm_map
*map
= get_imsm_map(dev
, MAP_1
);
3001 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
3003 switch (get_imsm_raid_level(map
)) {
3006 if (map
->num_members
% map
->num_domains
== 0)
3007 return 128*1024 >> 9;
3011 return max((__u32
) 64*1024 >> 9, chunk
);
3013 return 128*1024 >> 9;
3017 static __u32
num_stripes_per_unit_resync(struct imsm_dev
*dev
)
3019 struct imsm_map
*lo
= get_imsm_map(dev
, MAP_0
);
3020 struct imsm_map
*hi
= get_imsm_map(dev
, MAP_1
);
3021 __u32 lo_chunk
= __le32_to_cpu(lo
->blocks_per_strip
);
3022 __u32 hi_chunk
= __le32_to_cpu(hi
->blocks_per_strip
);
3024 return max((__u32
) 1, hi_chunk
/ lo_chunk
);
3027 static __u32
num_stripes_per_unit_rebuild(struct imsm_dev
*dev
)
3029 struct imsm_map
*lo
= get_imsm_map(dev
, MAP_0
);
3030 int level
= get_imsm_raid_level(lo
);
3032 if (level
== 1 || level
== 10) {
3033 struct imsm_map
*hi
= get_imsm_map(dev
, MAP_1
);
3035 return hi
->num_domains
;
3037 return num_stripes_per_unit_resync(dev
);
3040 static unsigned long long calc_component_size(struct imsm_map
*map
,
3041 struct imsm_dev
*dev
)
3043 unsigned long long component_size
;
3044 unsigned long long dev_size
= imsm_dev_size(dev
);
3045 long long calc_dev_size
= 0;
3046 unsigned int member_disks
= imsm_num_data_members(map
);
3048 if (member_disks
== 0)
3051 component_size
= per_dev_array_size(map
);
3052 calc_dev_size
= component_size
* member_disks
;
3054 /* Component size is rounded to 1MB so difference between size from
3055 * metadata and size calculated from num_data_stripes equals up to
3056 * 2048 blocks per each device. If the difference is higher it means
3057 * that array size was expanded and num_data_stripes was not updated.
3059 if (llabs(calc_dev_size
- (long long)dev_size
) >
3060 (1 << SECT_PER_MB_SHIFT
) * member_disks
) {
3061 component_size
= dev_size
/ member_disks
;
3062 dprintf("Invalid num_data_stripes in metadata; expected=%llu, found=%llu\n",
3063 component_size
/ map
->blocks_per_strip
,
3064 num_data_stripes(map
));
3067 return component_size
;
3070 static __u32
parity_segment_depth(struct imsm_dev
*dev
)
3072 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
3073 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
3075 switch(get_imsm_raid_level(map
)) {
3078 return chunk
* map
->num_domains
;
3080 return chunk
* map
->num_members
;
3086 static __u32
map_migr_block(struct imsm_dev
*dev
, __u32 block
)
3088 struct imsm_map
*map
= get_imsm_map(dev
, MAP_1
);
3089 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
3090 __u32 strip
= block
/ chunk
;
3092 switch (get_imsm_raid_level(map
)) {
3095 __u32 vol_strip
= (strip
* map
->num_domains
) + 1;
3096 __u32 vol_stripe
= vol_strip
/ map
->num_members
;
3098 return vol_stripe
* chunk
+ block
% chunk
;
3100 __u32 stripe
= strip
/ (map
->num_members
- 1);
3102 return stripe
* chunk
+ block
% chunk
;
3109 static __u64
blocks_per_migr_unit(struct intel_super
*super
,
3110 struct imsm_dev
*dev
)
3112 /* calculate the conversion factor between per member 'blocks'
3113 * (md/{resync,rebuild}_start) and imsm migration units, return
3114 * 0 for the 'not migrating' and 'unsupported migration' cases
3116 if (!dev
->vol
.migr_state
)
3119 switch (migr_type(dev
)) {
3120 case MIGR_GEN_MIGR
: {
3121 struct migr_record
*migr_rec
= super
->migr_rec
;
3122 return __le32_to_cpu(migr_rec
->blocks_per_unit
);
3127 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
3128 __u32 stripes_per_unit
;
3129 __u32 blocks_per_unit
;
3138 /* yes, this is really the translation of migr_units to
3139 * per-member blocks in the 'resync' case
3141 stripes_per_unit
= num_stripes_per_unit_resync(dev
);
3142 migr_chunk
= migr_strip_blocks_resync(dev
);
3143 disks
= imsm_num_data_members(map
);
3144 blocks_per_unit
= stripes_per_unit
* migr_chunk
* disks
;
3145 stripe
= __le16_to_cpu(map
->blocks_per_strip
) * disks
;
3146 segment
= blocks_per_unit
/ stripe
;
3147 block_rel
= blocks_per_unit
- segment
* stripe
;
3148 parity_depth
= parity_segment_depth(dev
);
3149 block_map
= map_migr_block(dev
, block_rel
);
3150 return block_map
+ parity_depth
* segment
;
3152 case MIGR_REBUILD
: {
3153 __u32 stripes_per_unit
;
3156 stripes_per_unit
= num_stripes_per_unit_rebuild(dev
);
3157 migr_chunk
= migr_strip_blocks_rebuild(dev
);
3158 return migr_chunk
* stripes_per_unit
;
3160 case MIGR_STATE_CHANGE
:
3166 static int imsm_level_to_layout(int level
)
3174 return ALGORITHM_LEFT_ASYMMETRIC
;
3181 /*******************************************************************************
3182 * Function: read_imsm_migr_rec
3183 * Description: Function reads imsm migration record from last sector of disk
3185 * fd : disk descriptor
3186 * super : metadata info
3190 ******************************************************************************/
3191 static int read_imsm_migr_rec(int fd
, struct intel_super
*super
)
3194 unsigned int sector_size
= super
->sector_size
;
3195 unsigned long long dsize
;
3197 get_dev_size(fd
, NULL
, &dsize
);
3198 if (lseek64(fd
, dsize
- (sector_size
*MIGR_REC_SECTOR_POSITION
),
3200 pr_err("Cannot seek to anchor block: %s\n",
3204 if ((unsigned int)read(fd
, super
->migr_rec_buf
,
3205 MIGR_REC_BUF_SECTORS
*sector_size
) !=
3206 MIGR_REC_BUF_SECTORS
*sector_size
) {
3207 pr_err("Cannot read migr record block: %s\n",
3212 if (sector_size
== 4096)
3213 convert_from_4k_imsm_migr_rec(super
);
3219 static struct imsm_dev
*imsm_get_device_during_migration(
3220 struct intel_super
*super
)
3223 struct intel_dev
*dv
;
3225 for (dv
= super
->devlist
; dv
; dv
= dv
->next
) {
3226 if (is_gen_migration(dv
->dev
))
3232 /*******************************************************************************
3233 * Function: load_imsm_migr_rec
3234 * Description: Function reads imsm migration record (it is stored at the last
3237 * super : imsm internal array info
3241 * -2 : no migration in progress
3242 ******************************************************************************/
3243 static int load_imsm_migr_rec(struct intel_super
*super
)
3249 struct imsm_dev
*dev
;
3250 struct imsm_map
*map
;
3254 /* find map under migration */
3255 dev
= imsm_get_device_during_migration(super
);
3256 /* nothing to load,no migration in progress?
3261 map
= get_imsm_map(dev
, MAP_0
);
3265 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
3266 /* skip spare and failed disks
3270 /* read only from one of the first two slots
3272 slot
= get_imsm_disk_slot(map
, dl
->index
);
3273 if (slot
> 1 || slot
< 0)
3276 if (!is_fd_valid(dl
->fd
)) {
3277 sprintf(nm
, "%d:%d", dl
->major
, dl
->minor
);
3278 fd
= dev_open(nm
, O_RDONLY
);
3280 if (is_fd_valid(fd
)) {
3290 if (!is_fd_valid(fd
))
3292 retval
= read_imsm_migr_rec(fd
, super
);
3299 /*******************************************************************************
3300 * function: imsm_create_metadata_checkpoint_update
3301 * Description: It creates update for checkpoint change.
3303 * super : imsm internal array info
3304 * u : pointer to prepared update
3307 * If length is equal to 0, input pointer u contains no update
3308 ******************************************************************************/
3309 static int imsm_create_metadata_checkpoint_update(
3310 struct intel_super
*super
,
3311 struct imsm_update_general_migration_checkpoint
**u
)
3314 int update_memory_size
= 0;
3316 dprintf("(enter)\n");
3322 /* size of all update data without anchor */
3323 update_memory_size
=
3324 sizeof(struct imsm_update_general_migration_checkpoint
);
3326 *u
= xcalloc(1, update_memory_size
);
3328 dprintf("error: cannot get memory\n");
3331 (*u
)->type
= update_general_migration_checkpoint
;
3332 (*u
)->curr_migr_unit
= current_migr_unit(super
->migr_rec
);
3333 dprintf("prepared for %llu\n", (unsigned long long)(*u
)->curr_migr_unit
);
3335 return update_memory_size
;
3338 static void imsm_update_metadata_locally(struct supertype
*st
,
3339 void *buf
, int len
);
3341 /*******************************************************************************
3342 * Function: write_imsm_migr_rec
3343 * Description: Function writes imsm migration record
3344 * (at the last sector of disk)
3346 * super : imsm internal array info
3350 ******************************************************************************/
3351 static int write_imsm_migr_rec(struct supertype
*st
)
3353 struct intel_super
*super
= st
->sb
;
3354 unsigned int sector_size
= super
->sector_size
;
3355 unsigned long long dsize
;
3359 struct imsm_update_general_migration_checkpoint
*u
;
3360 struct imsm_dev
*dev
;
3361 struct imsm_map
*map
;
3363 /* find map under migration */
3364 dev
= imsm_get_device_during_migration(super
);
3365 /* if no migration, write buffer anyway to clear migr_record
3366 * on disk based on first available device
3369 dev
= get_imsm_dev(super
, super
->current_vol
< 0 ? 0 :
3370 super
->current_vol
);
3372 map
= get_imsm_map(dev
, MAP_0
);
3374 if (sector_size
== 4096)
3375 convert_to_4k_imsm_migr_rec(super
);
3376 for (sd
= super
->disks
; sd
; sd
= sd
->next
) {
3379 /* skip failed and spare devices */
3382 /* write to 2 first slots only */
3384 slot
= get_imsm_disk_slot(map
, sd
->index
);
3385 if (map
== NULL
|| slot
> 1 || slot
< 0)
3388 get_dev_size(sd
->fd
, NULL
, &dsize
);
3389 if (lseek64(sd
->fd
, dsize
- (MIGR_REC_SECTOR_POSITION
*
3392 pr_err("Cannot seek to anchor block: %s\n",
3396 if ((unsigned int)write(sd
->fd
, super
->migr_rec_buf
,
3397 MIGR_REC_BUF_SECTORS
*sector_size
) !=
3398 MIGR_REC_BUF_SECTORS
*sector_size
) {
3399 pr_err("Cannot write migr record block: %s\n",
3404 if (sector_size
== 4096)
3405 convert_from_4k_imsm_migr_rec(super
);
3406 /* update checkpoint information in metadata */
3407 len
= imsm_create_metadata_checkpoint_update(super
, &u
);
3409 dprintf("imsm: Cannot prepare update\n");
3412 /* update metadata locally */
3413 imsm_update_metadata_locally(st
, u
, len
);
3414 /* and possibly remotely */
3415 if (st
->update_tail
) {
3416 append_metadata_update(st
, u
, len
);
3417 /* during reshape we do all work inside metadata handler
3418 * manage_reshape(), so metadata update has to be triggered
3421 flush_metadata_updates(st
);
3422 st
->update_tail
= &st
->updates
;
3431 /* spare/missing disks activations are not allowe when
3432 * array/container performs reshape operation, because
3433 * all arrays in container works on the same disks set
3435 int imsm_reshape_blocks_arrays_changes(struct intel_super
*super
)
3438 struct intel_dev
*i_dev
;
3439 struct imsm_dev
*dev
;
3441 /* check whole container
3443 for (i_dev
= super
->devlist
; i_dev
; i_dev
= i_dev
->next
) {
3445 if (is_gen_migration(dev
)) {
3446 /* No repair during any migration in container
3454 static unsigned long long imsm_component_size_alignment_check(int level
,
3456 unsigned int sector_size
,
3457 unsigned long long component_size
)
3459 unsigned int component_size_alignment
;
3461 /* check component size alignment
3463 component_size_alignment
= component_size
% (chunk_size
/sector_size
);
3465 dprintf("(Level: %i, chunk_size = %i, component_size = %llu), component_size_alignment = %u\n",
3466 level
, chunk_size
, component_size
,
3467 component_size_alignment
);
3469 if (component_size_alignment
&& (level
!= 1) && (level
!= UnSet
)) {
3470 dprintf("imsm: reported component size aligned from %llu ",
3472 component_size
-= component_size_alignment
;
3473 dprintf_cont("to %llu (%i).\n",
3474 component_size
, component_size_alignment
);
3477 return component_size
;
3480 /*******************************************************************************
3481 * Function: get_bitmap_header_sector
3482 * Description: Returns the sector where the bitmap header is placed.
3484 * st : supertype information
3485 * dev_idx : index of the device with bitmap
3488 * The sector where the bitmap header is placed
3489 ******************************************************************************/
3490 static unsigned long long get_bitmap_header_sector(struct intel_super
*super
,
3493 struct imsm_dev
*dev
= get_imsm_dev(super
, dev_idx
);
3494 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
3496 if (!super
->sector_size
) {
3497 dprintf("sector size is not set\n");
3501 return pba_of_lba0(map
) + calc_component_size(map
, dev
) +
3502 (IMSM_BITMAP_HEADER_OFFSET
/ super
->sector_size
);
3505 /*******************************************************************************
3506 * Function: get_bitmap_sector
3507 * Description: Returns the sector where the bitmap is placed.
3509 * st : supertype information
3510 * dev_idx : index of the device with bitmap
3513 * The sector where the bitmap is placed
3514 ******************************************************************************/
3515 static unsigned long long get_bitmap_sector(struct intel_super
*super
,
3518 if (!super
->sector_size
) {
3519 dprintf("sector size is not set\n");
3523 return get_bitmap_header_sector(super
, dev_idx
) +
3524 (IMSM_BITMAP_HEADER_SIZE
/ super
->sector_size
);
3527 static unsigned long long get_ppl_sector(struct intel_super
*super
, int dev_idx
)
3529 struct imsm_dev
*dev
= get_imsm_dev(super
, dev_idx
);
3530 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
3532 return pba_of_lba0(map
) +
3533 (num_data_stripes(map
) * map
->blocks_per_strip
);
3536 static void getinfo_super_imsm_volume(struct supertype
*st
, struct mdinfo
*info
, char *dmap
)
3538 struct intel_super
*super
= st
->sb
;
3539 struct migr_record
*migr_rec
= super
->migr_rec
;
3540 struct imsm_dev
*dev
= get_imsm_dev(super
, super
->current_vol
);
3541 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
3542 struct imsm_map
*prev_map
= get_imsm_map(dev
, MAP_1
);
3543 struct imsm_map
*map_to_analyse
= map
;
3545 int map_disks
= info
->array
.raid_disks
;
3547 memset(info
, 0, sizeof(*info
));
3549 map_to_analyse
= prev_map
;
3551 dl
= super
->current_disk
;
3553 info
->container_member
= super
->current_vol
;
3554 info
->array
.raid_disks
= map
->num_members
;
3555 info
->array
.level
= get_imsm_raid_level(map_to_analyse
);
3556 info
->array
.layout
= imsm_level_to_layout(info
->array
.level
);
3557 info
->array
.md_minor
= -1;
3558 info
->array
.ctime
= 0;
3559 info
->array
.utime
= 0;
3560 info
->array
.chunk_size
=
3561 __le16_to_cpu(map_to_analyse
->blocks_per_strip
) << 9;
3562 info
->array
.state
= !(dev
->vol
.dirty
& RAIDVOL_DIRTY
);
3563 info
->custom_array_size
= imsm_dev_size(dev
);
3564 info
->recovery_blocked
= imsm_reshape_blocks_arrays_changes(st
->sb
);
3566 if (is_gen_migration(dev
)) {
3568 * device prev_map should be added if it is in the middle
3573 info
->reshape_active
= 1;
3574 info
->new_level
= get_imsm_raid_level(map
);
3575 info
->new_layout
= imsm_level_to_layout(info
->new_level
);
3576 info
->new_chunk
= __le16_to_cpu(map
->blocks_per_strip
) << 9;
3577 info
->delta_disks
= map
->num_members
- prev_map
->num_members
;
3578 if (info
->delta_disks
) {
3579 /* this needs to be applied to every array
3582 info
->reshape_active
= CONTAINER_RESHAPE
;
3584 /* We shape information that we give to md might have to be
3585 * modify to cope with md's requirement for reshaping arrays.
3586 * For example, when reshaping a RAID0, md requires it to be
3587 * presented as a degraded RAID4.
3588 * Also if a RAID0 is migrating to a RAID5 we need to specify
3589 * the array as already being RAID5, but the 'before' layout
3590 * is a RAID4-like layout.
3592 switch (info
->array
.level
) {
3594 switch(info
->new_level
) {
3596 /* conversion is happening as RAID4 */
3597 info
->array
.level
= 4;
3598 info
->array
.raid_disks
+= 1;
3601 /* conversion is happening as RAID5 */
3602 info
->array
.level
= 5;
3603 info
->array
.layout
= ALGORITHM_PARITY_N
;
3604 info
->delta_disks
-= 1;
3607 /* FIXME error message */
3608 info
->array
.level
= UnSet
;
3614 info
->new_level
= UnSet
;
3615 info
->new_layout
= UnSet
;
3616 info
->new_chunk
= info
->array
.chunk_size
;
3617 info
->delta_disks
= 0;
3621 info
->disk
.major
= dl
->major
;
3622 info
->disk
.minor
= dl
->minor
;
3623 info
->disk
.number
= dl
->index
;
3624 info
->disk
.raid_disk
= get_imsm_disk_slot(map_to_analyse
,
3628 info
->data_offset
= pba_of_lba0(map_to_analyse
);
3629 info
->component_size
= calc_component_size(map
, dev
);
3630 info
->component_size
= imsm_component_size_alignment_check(
3632 info
->array
.chunk_size
,
3634 info
->component_size
);
3635 info
->bb
.supported
= 1;
3637 memset(info
->uuid
, 0, sizeof(info
->uuid
));
3638 info
->recovery_start
= MaxSector
;
3640 if (info
->array
.level
== 5 &&
3641 (dev
->rwh_policy
== RWH_DISTRIBUTED
||
3642 dev
->rwh_policy
== RWH_MULTIPLE_DISTRIBUTED
)) {
3643 info
->consistency_policy
= CONSISTENCY_POLICY_PPL
;
3644 info
->ppl_sector
= get_ppl_sector(super
, super
->current_vol
);
3645 if (dev
->rwh_policy
== RWH_MULTIPLE_DISTRIBUTED
)
3646 info
->ppl_size
= MULTIPLE_PPL_AREA_SIZE_IMSM
>> 9;
3648 info
->ppl_size
= (PPL_HEADER_SIZE
+ PPL_ENTRY_SPACE
)
3650 } else if (info
->array
.level
<= 0) {
3651 info
->consistency_policy
= CONSISTENCY_POLICY_NONE
;
3653 if (dev
->rwh_policy
== RWH_BITMAP
) {
3654 info
->bitmap_offset
= get_bitmap_sector(super
, super
->current_vol
);
3655 info
->consistency_policy
= CONSISTENCY_POLICY_BITMAP
;
3657 info
->consistency_policy
= CONSISTENCY_POLICY_RESYNC
;
3661 info
->reshape_progress
= 0;
3662 info
->resync_start
= MaxSector
;
3663 if ((map_to_analyse
->map_state
== IMSM_T_STATE_UNINITIALIZED
||
3664 !(info
->array
.state
& 1)) &&
3665 imsm_reshape_blocks_arrays_changes(super
) == 0) {
3666 info
->resync_start
= 0;
3668 if (dev
->vol
.migr_state
) {
3669 switch (migr_type(dev
)) {
3672 __u64 blocks_per_unit
= blocks_per_migr_unit(super
,
3674 __u64 units
= vol_curr_migr_unit(dev
);
3676 info
->resync_start
= blocks_per_unit
* units
;
3679 case MIGR_GEN_MIGR
: {
3680 __u64 blocks_per_unit
= blocks_per_migr_unit(super
,
3682 __u64 units
= current_migr_unit(migr_rec
);
3685 if (__le32_to_cpu(migr_rec
->ascending_migr
) &&
3687 (get_num_migr_units(migr_rec
)-1)) &&
3688 (super
->migr_rec
->rec_status
==
3689 __cpu_to_le32(UNIT_SRC_IN_CP_AREA
)))
3692 info
->reshape_progress
= blocks_per_unit
* units
;
3694 dprintf("IMSM: General Migration checkpoint : %llu (%llu) -> read reshape progress : %llu\n",
3695 (unsigned long long)units
,
3696 (unsigned long long)blocks_per_unit
,
3697 info
->reshape_progress
);
3699 used_disks
= imsm_num_data_members(prev_map
);
3700 if (used_disks
> 0) {
3701 info
->custom_array_size
= per_dev_array_size(map
) *
3706 /* we could emulate the checkpointing of
3707 * 'sync_action=check' migrations, but for now
3708 * we just immediately complete them
3711 /* this is handled by container_content_imsm() */
3712 case MIGR_STATE_CHANGE
:
3713 /* FIXME handle other migrations */
3715 /* we are not dirty, so... */
3716 info
->resync_start
= MaxSector
;
3720 strncpy(info
->name
, (char *) dev
->volume
, MAX_RAID_SERIAL_LEN
);
3721 info
->name
[MAX_RAID_SERIAL_LEN
] = 0;
3723 info
->array
.major_version
= -1;
3724 info
->array
.minor_version
= -2;
3725 sprintf(info
->text_version
, "/%s/%d", st
->container_devnm
, info
->container_member
);
3726 info
->safe_mode_delay
= 4000; /* 4 secs like the Matrix driver */
3727 uuid_from_super_imsm(st
, info
->uuid
);
3731 for (i
=0; i
<map_disks
; i
++) {
3733 if (i
< info
->array
.raid_disks
) {
3734 struct imsm_disk
*dsk
;
3735 j
= get_imsm_disk_idx(dev
, i
, MAP_X
);
3736 dsk
= get_imsm_disk(super
, j
);
3737 if (dsk
&& (dsk
->status
& CONFIGURED_DISK
))
3744 static __u8
imsm_check_degraded(struct intel_super
*super
, struct imsm_dev
*dev
,
3745 int failed
, int look_in_map
);
3747 static int imsm_count_failed(struct intel_super
*super
, struct imsm_dev
*dev
,
3750 static void manage_second_map(struct intel_super
*super
, struct imsm_dev
*dev
)
3752 if (is_gen_migration(dev
)) {
3755 struct imsm_map
*map2
= get_imsm_map(dev
, MAP_1
);
3757 failed
= imsm_count_failed(super
, dev
, MAP_1
);
3758 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_1
);
3759 if (map2
->map_state
!= map_state
) {
3760 map2
->map_state
= map_state
;
3761 super
->updates_pending
++;
3766 static struct imsm_disk
*get_imsm_missing(struct intel_super
*super
, __u8 index
)
3770 for (d
= super
->missing
; d
; d
= d
->next
)
3771 if (d
->index
== index
)
3776 static void getinfo_super_imsm(struct supertype
*st
, struct mdinfo
*info
, char *map
)
3778 struct intel_super
*super
= st
->sb
;
3779 struct imsm_disk
*disk
;
3780 int map_disks
= info
->array
.raid_disks
;
3781 int max_enough
= -1;
3783 struct imsm_super
*mpb
;
3785 if (super
->current_vol
>= 0) {
3786 getinfo_super_imsm_volume(st
, info
, map
);
3789 memset(info
, 0, sizeof(*info
));
3791 /* Set raid_disks to zero so that Assemble will always pull in valid
3794 info
->array
.raid_disks
= 0;
3795 info
->array
.level
= LEVEL_CONTAINER
;
3796 info
->array
.layout
= 0;
3797 info
->array
.md_minor
= -1;
3798 info
->array
.ctime
= 0; /* N/A for imsm */
3799 info
->array
.utime
= 0;
3800 info
->array
.chunk_size
= 0;
3802 info
->disk
.major
= 0;
3803 info
->disk
.minor
= 0;
3804 info
->disk
.raid_disk
= -1;
3805 info
->reshape_active
= 0;
3806 info
->array
.major_version
= -1;
3807 info
->array
.minor_version
= -2;
3808 strcpy(info
->text_version
, "imsm");
3809 info
->safe_mode_delay
= 0;
3810 info
->disk
.number
= -1;
3811 info
->disk
.state
= 0;
3813 info
->recovery_start
= MaxSector
;
3814 info
->recovery_blocked
= imsm_reshape_blocks_arrays_changes(st
->sb
);
3815 info
->bb
.supported
= 1;
3817 /* do we have the all the insync disks that we expect? */
3818 mpb
= super
->anchor
;
3819 info
->events
= __le32_to_cpu(mpb
->generation_num
);
3821 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
3822 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
3823 int failed
, enough
, j
, missing
= 0;
3824 struct imsm_map
*map
;
3827 failed
= imsm_count_failed(super
, dev
, MAP_0
);
3828 state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
3829 map
= get_imsm_map(dev
, MAP_0
);
3831 /* any newly missing disks?
3832 * (catches single-degraded vs double-degraded)
3834 for (j
= 0; j
< map
->num_members
; j
++) {
3835 __u32 ord
= get_imsm_ord_tbl_ent(dev
, j
, MAP_0
);
3836 __u32 idx
= ord_to_idx(ord
);
3838 if (super
->disks
&& super
->disks
->index
== (int)idx
)
3839 info
->disk
.raid_disk
= j
;
3841 if (!(ord
& IMSM_ORD_REBUILD
) &&
3842 get_imsm_missing(super
, idx
)) {
3848 if (state
== IMSM_T_STATE_FAILED
)
3850 else if (state
== IMSM_T_STATE_DEGRADED
&&
3851 (state
!= map
->map_state
|| missing
))
3853 else /* we're normal, or already degraded */
3855 if (is_gen_migration(dev
) && missing
) {
3856 /* during general migration we need all disks
3857 * that process is running on.
3858 * No new missing disk is allowed.
3862 /* no more checks necessary
3866 /* in the missing/failed disk case check to see
3867 * if at least one array is runnable
3869 max_enough
= max(max_enough
, enough
);
3872 info
->container_enough
= max_enough
;
3875 __u32 reserved
= imsm_reserved_sectors(super
, super
->disks
);
3877 disk
= &super
->disks
->disk
;
3878 info
->data_offset
= total_blocks(&super
->disks
->disk
) - reserved
;
3879 info
->component_size
= reserved
;
3880 info
->disk
.state
= is_configured(disk
) ? (1 << MD_DISK_ACTIVE
) : 0;
3881 /* we don't change info->disk.raid_disk here because
3882 * this state will be finalized in mdmon after we have
3883 * found the 'most fresh' version of the metadata
3885 info
->disk
.state
|= is_failed(disk
) ? (1 << MD_DISK_FAULTY
) : 0;
3886 info
->disk
.state
|= (is_spare(disk
) || is_journal(disk
)) ?
3887 0 : (1 << MD_DISK_SYNC
);
3890 /* only call uuid_from_super_imsm when this disk is part of a populated container,
3891 * ->compare_super may have updated the 'num_raid_devs' field for spares
3893 if (info
->disk
.state
& (1 << MD_DISK_SYNC
) || super
->anchor
->num_raid_devs
)
3894 uuid_from_super_imsm(st
, info
->uuid
);
3896 memcpy(info
->uuid
, uuid_zero
, sizeof(uuid_zero
));
3898 /* I don't know how to compute 'map' on imsm, so use safe default */
3901 for (i
= 0; i
< map_disks
; i
++)
3907 /* allocates memory and fills disk in mdinfo structure
3908 * for each disk in array */
3909 struct mdinfo
*getinfo_super_disks_imsm(struct supertype
*st
)
3911 struct mdinfo
*mddev
;
3912 struct intel_super
*super
= st
->sb
;
3913 struct imsm_disk
*disk
;
3916 if (!super
|| !super
->disks
)
3919 mddev
= xcalloc(1, sizeof(*mddev
));
3923 tmp
= xcalloc(1, sizeof(*tmp
));
3925 tmp
->next
= mddev
->devs
;
3927 tmp
->disk
.number
= count
++;
3928 tmp
->disk
.major
= dl
->major
;
3929 tmp
->disk
.minor
= dl
->minor
;
3930 tmp
->disk
.state
= is_configured(disk
) ?
3931 (1 << MD_DISK_ACTIVE
) : 0;
3932 tmp
->disk
.state
|= is_failed(disk
) ? (1 << MD_DISK_FAULTY
) : 0;
3933 tmp
->disk
.state
|= is_spare(disk
) ? 0 : (1 << MD_DISK_SYNC
);
3934 tmp
->disk
.raid_disk
= -1;
3940 static int update_super_imsm(struct supertype
*st
, struct mdinfo
*info
,
3941 enum update_opt update
, char *devname
,
3942 int verbose
, int uuid_set
, char *homehost
)
3944 /* For 'assemble' and 'force' we need to return non-zero if any
3945 * change was made. For others, the return value is ignored.
3946 * Update options are:
3947 * force-one : This device looks a bit old but needs to be included,
3948 * update age info appropriately.
3949 * assemble: clear any 'faulty' flag to allow this device to
3951 * force-array: Array is degraded but being forced, mark it clean
3952 * if that will be needed to assemble it.
3954 * newdev: not used ????
3955 * grow: Array has gained a new device - this is currently for
3957 * resync: mark as dirty so a resync will happen.
3958 * name: update the name - preserving the homehost
3959 * uuid: Change the uuid of the array to match watch is given
3961 * Following are not relevant for this imsm:
3962 * sparc2.2 : update from old dodgey metadata
3963 * super-minor: change the preferred_minor number
3964 * summaries: update redundant counters.
3965 * homehost: update the recorded homehost
3966 * _reshape_progress: record new reshape_progress position.
3969 struct intel_super
*super
= st
->sb
;
3970 struct imsm_super
*mpb
;
3972 /* we can only update container info */
3973 if (!super
|| super
->current_vol
>= 0 || !super
->anchor
)
3976 mpb
= super
->anchor
;
3980 /* We take this to mean that the family_num should be updated.
3981 * However that is much smaller than the uuid so we cannot really
3982 * allow an explicit uuid to be given. And it is hard to reliably
3984 * So if !uuid_set we know the current uuid is random and just used
3985 * the first 'int' and copy it to the other 3 positions.
3986 * Otherwise we require the 4 'int's to be the same as would be the
3987 * case if we are using a random uuid. So an explicit uuid will be
3988 * accepted as long as all for ints are the same... which shouldn't hurt
3991 info
->uuid
[1] = info
->uuid
[2] = info
->uuid
[3] = info
->uuid
[0];
3994 if (info
->uuid
[0] != info
->uuid
[1] ||
3995 info
->uuid
[1] != info
->uuid
[2] ||
3996 info
->uuid
[2] != info
->uuid
[3])
4002 mpb
->orig_family_num
= info
->uuid
[0];
4004 case UOPT_SPEC_ASSEMBLE
:
4012 /* successful update? recompute checksum */
4014 mpb
->check_sum
= __le32_to_cpu(__gen_imsm_checksum(mpb
));
4019 static size_t disks_to_mpb_size(int disks
)
4023 size
= sizeof(struct imsm_super
);
4024 size
+= (disks
- 1) * sizeof(struct imsm_disk
);
4025 size
+= 2 * sizeof(struct imsm_dev
);
4026 /* up to 2 maps per raid device (-2 for imsm_maps in imsm_dev */
4027 size
+= (4 - 2) * sizeof(struct imsm_map
);
4028 /* 4 possible disk_ord_tbl's */
4029 size
+= 4 * (disks
- 1) * sizeof(__u32
);
4030 /* maximum bbm log */
4031 size
+= sizeof(struct bbm_log
);
4036 static __u64
avail_size_imsm(struct supertype
*st
, __u64 devsize
,
4037 unsigned long long data_offset
)
4039 if (devsize
< (MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
))
4042 return devsize
- (MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
);
4045 static void free_devlist(struct intel_super
*super
)
4047 struct intel_dev
*dv
;
4049 while (super
->devlist
) {
4050 dv
= super
->devlist
->next
;
4051 free(super
->devlist
->dev
);
4052 free(super
->devlist
);
4053 super
->devlist
= dv
;
4057 static void imsm_copy_dev(struct imsm_dev
*dest
, struct imsm_dev
*src
)
4059 memcpy(dest
, src
, sizeof_imsm_dev(src
, 0));
4062 static int compare_super_imsm(struct supertype
*st
, struct supertype
*tst
,
4066 * 0 same, or first was empty, and second was copied
4067 * 1 sb are different
4069 struct intel_super
*first
= st
->sb
;
4070 struct intel_super
*sec
= tst
->sb
;
4078 /* in platform dependent environment test if the disks
4079 * use the same Intel hba
4080 * if not on Intel hba at all, allow anything.
4081 * doesn't check HBAs if num_raid_devs is not set, as it means
4082 * it is a free floating spare, and all spares regardless of HBA type
4083 * will fall into separate container during the assembly
4085 if (first
->hba
&& sec
->hba
&& first
->anchor
->num_raid_devs
!= 0) {
4086 if (first
->hba
->type
!= sec
->hba
->type
) {
4088 pr_err("HBAs of devices do not match %s != %s\n",
4089 get_sys_dev_type(first
->hba
->type
),
4090 get_sys_dev_type(sec
->hba
->type
));
4093 if (first
->orom
!= sec
->orom
) {
4095 pr_err("HBAs of devices do not match %s != %s\n",
4096 first
->hba
->pci_id
, sec
->hba
->pci_id
);
4101 if (first
->anchor
->num_raid_devs
> 0 &&
4102 sec
->anchor
->num_raid_devs
> 0) {
4103 /* Determine if these disks might ever have been
4104 * related. Further disambiguation can only take place
4105 * in load_super_imsm_all
4107 __u32 first_family
= first
->anchor
->orig_family_num
;
4108 __u32 sec_family
= sec
->anchor
->orig_family_num
;
4110 if (memcmp(first
->anchor
->sig
, sec
->anchor
->sig
,
4111 MAX_SIGNATURE_LENGTH
) != 0)
4114 if (first_family
== 0)
4115 first_family
= first
->anchor
->family_num
;
4116 if (sec_family
== 0)
4117 sec_family
= sec
->anchor
->family_num
;
4119 if (first_family
!= sec_family
)
4124 /* if an anchor does not have num_raid_devs set then it is a free
4125 * floating spare. don't assosiate spare with any array, as during assembly
4126 * spares shall fall into separate container, from which they can be moved
4129 if (first
->anchor
->num_raid_devs
^ sec
->anchor
->num_raid_devs
)
4135 static void fd2devname(int fd
, char *name
)
4143 snprintf(name
, MAX_RAID_SERIAL_LEN
, "/dev/%s", nm
);
4146 static int nvme_get_serial(int fd
, void *buf
, size_t buf_len
)
4148 char path
[PATH_MAX
];
4149 char *name
= fd2kname(fd
);
4154 if (strncmp(name
, "nvme", 4) != 0)
4157 if (!diskfd_to_devpath(fd
, 1, path
))
4160 return devpath_to_char(path
, "serial", buf
, buf_len
, 0);
4163 extern int scsi_get_serial(int fd
, void *buf
, size_t buf_len
);
4165 static int imsm_read_serial(int fd
, char *devname
,
4166 __u8
*serial
, size_t serial_buf_len
)
4175 memset(buf
, 0, sizeof(buf
));
4177 if (check_env("IMSM_DEVNAME_AS_SERIAL")) {
4178 memset(serial
, 0, serial_buf_len
);
4179 fd2devname(fd
, (char *) serial
);
4183 rv
= nvme_get_serial(fd
, buf
, sizeof(buf
));
4186 rv
= scsi_get_serial(fd
, buf
, sizeof(buf
));
4190 pr_err("Failed to retrieve serial for %s\n",
4195 /* trim all whitespace and non-printable characters and convert
4198 for (i
= 0, dest
= buf
; i
< sizeof(buf
) && buf
[i
]; i
++) {
4201 /* ':' is reserved for use in placeholder serial
4202 * numbers for missing disks
4213 if (len
> serial_buf_len
) {
4214 /* truncate leading characters */
4215 dest
+= len
- serial_buf_len
;
4216 len
= serial_buf_len
;
4219 memset(serial
, 0, serial_buf_len
);
4220 memcpy(serial
, dest
, len
);
4225 static int serialcmp(__u8
*s1
, __u8
*s2
)
4227 return strncmp((char *) s1
, (char *) s2
, MAX_RAID_SERIAL_LEN
);
4230 static void serialcpy(__u8
*dest
, __u8
*src
)
4232 strncpy((char *) dest
, (char *) src
, MAX_RAID_SERIAL_LEN
);
4235 static struct dl
*serial_to_dl(__u8
*serial
, struct intel_super
*super
)
4239 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
4240 if (serialcmp(dl
->serial
, serial
) == 0)
4246 static struct imsm_disk
*
4247 __serial_to_disk(__u8
*serial
, struct imsm_super
*mpb
, int *idx
)
4251 for (i
= 0; i
< mpb
->num_disks
; i
++) {
4252 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, i
);
4254 if (serialcmp(disk
->serial
, serial
) == 0) {
4265 load_imsm_disk(int fd
, struct intel_super
*super
, char *devname
, int keep_fd
)
4267 struct imsm_disk
*disk
;
4272 __u8 serial
[MAX_RAID_SERIAL_LEN
];
4274 rv
= imsm_read_serial(fd
, devname
, serial
, MAX_RAID_SERIAL_LEN
);
4279 dl
= xcalloc(1, sizeof(*dl
));
4282 dl
->major
= major(stb
.st_rdev
);
4283 dl
->minor
= minor(stb
.st_rdev
);
4284 dl
->next
= super
->disks
;
4285 dl
->fd
= keep_fd
? fd
: -1;
4286 assert(super
->disks
== NULL
);
4288 serialcpy(dl
->serial
, serial
);
4291 fd2devname(fd
, name
);
4293 dl
->devname
= xstrdup(devname
);
4295 dl
->devname
= xstrdup(name
);
4297 /* look up this disk's index in the current anchor */
4298 disk
= __serial_to_disk(dl
->serial
, super
->anchor
, &dl
->index
);
4301 /* only set index on disks that are a member of a
4302 * populated contianer, i.e. one with raid_devs
4304 if (is_failed(&dl
->disk
))
4306 else if (is_spare(&dl
->disk
) || is_journal(&dl
->disk
))
4313 /* When migrating map0 contains the 'destination' state while map1
4314 * contains the current state. When not migrating map0 contains the
4315 * current state. This routine assumes that map[0].map_state is set to
4316 * the current array state before being called.
4318 * Migration is indicated by one of the following states
4319 * 1/ Idle (migr_state=0 map0state=normal||unitialized||degraded||failed)
4320 * 2/ Initialize (migr_state=1 migr_type=MIGR_INIT map0state=normal
4321 * map1state=unitialized)
4322 * 3/ Repair (Resync) (migr_state=1 migr_type=MIGR_REPAIR map0state=normal
4324 * 4/ Rebuild (migr_state=1 migr_type=MIGR_REBUILD map0state=normal
4325 * map1state=degraded)
4326 * 5/ Migration (mig_state=1 migr_type=MIGR_GEN_MIGR map0state=normal
4329 static void migrate(struct imsm_dev
*dev
, struct intel_super
*super
,
4330 __u8 to_state
, int migr_type
)
4332 struct imsm_map
*dest
;
4333 struct imsm_map
*src
= get_imsm_map(dev
, MAP_0
);
4335 dev
->vol
.migr_state
= 1;
4336 set_migr_type(dev
, migr_type
);
4337 set_vol_curr_migr_unit(dev
, 0);
4338 dest
= get_imsm_map(dev
, MAP_1
);
4340 /* duplicate and then set the target end state in map[0] */
4341 memcpy(dest
, src
, sizeof_imsm_map(src
));
4342 if (migr_type
== MIGR_GEN_MIGR
) {
4346 for (i
= 0; i
< src
->num_members
; i
++) {
4347 ord
= __le32_to_cpu(src
->disk_ord_tbl
[i
]);
4348 set_imsm_ord_tbl_ent(src
, i
, ord_to_idx(ord
));
4352 if (migr_type
== MIGR_GEN_MIGR
)
4353 /* Clear migration record */
4354 memset(super
->migr_rec
, 0, sizeof(struct migr_record
));
4356 src
->map_state
= to_state
;
4359 static void end_migration(struct imsm_dev
*dev
, struct intel_super
*super
,
4362 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
4363 struct imsm_map
*prev
= get_imsm_map(dev
, dev
->vol
.migr_state
== 0 ?
4367 /* merge any IMSM_ORD_REBUILD bits that were not successfully
4368 * completed in the last migration.
4370 * FIXME add support for raid-level-migration
4372 if (map_state
!= map
->map_state
&& (is_gen_migration(dev
) == false) &&
4373 prev
->map_state
!= IMSM_T_STATE_UNINITIALIZED
) {
4374 /* when final map state is other than expected
4375 * merge maps (not for migration)
4379 for (i
= 0; i
< prev
->num_members
; i
++)
4380 for (j
= 0; j
< map
->num_members
; j
++)
4381 /* during online capacity expansion
4382 * disks position can be changed
4383 * if takeover is used
4385 if (ord_to_idx(map
->disk_ord_tbl
[j
]) ==
4386 ord_to_idx(prev
->disk_ord_tbl
[i
])) {
4387 map
->disk_ord_tbl
[j
] |=
4388 prev
->disk_ord_tbl
[i
];
4391 failed
= imsm_count_failed(super
, dev
, MAP_0
);
4392 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
4395 dev
->vol
.migr_state
= 0;
4396 set_migr_type(dev
, 0);
4397 set_vol_curr_migr_unit(dev
, 0);
4398 map
->map_state
= map_state
;
4401 static int parse_raid_devices(struct intel_super
*super
)
4404 struct imsm_dev
*dev_new
;
4405 size_t len
, len_migr
;
4407 size_t space_needed
= 0;
4408 struct imsm_super
*mpb
= super
->anchor
;
4410 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
4411 struct imsm_dev
*dev_iter
= __get_imsm_dev(super
->anchor
, i
);
4412 struct intel_dev
*dv
;
4414 len
= sizeof_imsm_dev(dev_iter
, 0);
4415 len_migr
= sizeof_imsm_dev(dev_iter
, 1);
4417 space_needed
+= len_migr
- len
;
4419 dv
= xmalloc(sizeof(*dv
));
4420 if (max_len
< len_migr
)
4422 if (max_len
> len_migr
)
4423 space_needed
+= max_len
- len_migr
;
4424 dev_new
= xmalloc(max_len
);
4425 imsm_copy_dev(dev_new
, dev_iter
);
4428 dv
->next
= super
->devlist
;
4429 super
->devlist
= dv
;
4432 /* ensure that super->buf is large enough when all raid devices
4435 if (__le32_to_cpu(mpb
->mpb_size
) + space_needed
> super
->len
) {
4438 len
= ROUND_UP(__le32_to_cpu(mpb
->mpb_size
) + space_needed
,
4439 super
->sector_size
);
4440 if (posix_memalign(&buf
, MAX_SECTOR_SIZE
, len
) != 0)
4443 memcpy(buf
, super
->buf
, super
->len
);
4444 memset(buf
+ super
->len
, 0, len
- super
->len
);
4450 super
->extra_space
+= space_needed
;
4455 /*******************************************************************************
4456 * Function: check_mpb_migr_compatibility
4457 * Description: Function checks for unsupported migration features:
4458 * - migration optimization area (pba_of_lba0)
4459 * - descending reshape (ascending_migr)
4461 * super : imsm metadata information
4463 * 0 : migration is compatible
4464 * -1 : migration is not compatible
4465 ******************************************************************************/
4466 int check_mpb_migr_compatibility(struct intel_super
*super
)
4468 struct imsm_map
*map0
, *map1
;
4469 struct migr_record
*migr_rec
= super
->migr_rec
;
4472 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
4473 struct imsm_dev
*dev_iter
= __get_imsm_dev(super
->anchor
, i
);
4475 if (dev_iter
->vol
.migr_state
== 1 &&
4476 dev_iter
->vol
.migr_type
== MIGR_GEN_MIGR
) {
4477 /* This device is migrating */
4478 map0
= get_imsm_map(dev_iter
, MAP_0
);
4479 map1
= get_imsm_map(dev_iter
, MAP_1
);
4480 if (pba_of_lba0(map0
) != pba_of_lba0(map1
))
4481 /* migration optimization area was used */
4483 if (migr_rec
->ascending_migr
== 0 &&
4484 migr_rec
->dest_depth_per_unit
> 0)
4485 /* descending reshape not supported yet */
4492 static void __free_imsm(struct intel_super
*super
, int free_disks
);
4494 /* load_imsm_mpb - read matrix metadata
4495 * allocates super->mpb to be freed by free_imsm
4497 static int load_imsm_mpb(int fd
, struct intel_super
*super
, char *devname
)
4499 unsigned long long dsize
;
4500 unsigned long long sectors
;
4501 unsigned int sector_size
= super
->sector_size
;
4503 struct imsm_super
*anchor
;
4506 get_dev_size(fd
, NULL
, &dsize
);
4507 if (dsize
< 2*sector_size
) {
4509 pr_err("%s: device to small for imsm\n",
4514 if (lseek64(fd
, dsize
- (sector_size
* 2), SEEK_SET
) < 0) {
4516 pr_err("Cannot seek to anchor block on %s: %s\n",
4517 devname
, strerror(errno
));
4521 if (posix_memalign((void **)&anchor
, sector_size
, sector_size
) != 0) {
4523 pr_err("Failed to allocate imsm anchor buffer on %s\n", devname
);
4526 if ((unsigned int)read(fd
, anchor
, sector_size
) != sector_size
) {
4528 pr_err("Cannot read anchor block on %s: %s\n",
4529 devname
, strerror(errno
));
4534 if (strncmp((char *) anchor
->sig
, MPB_SIGNATURE
, MPB_SIG_LEN
) != 0) {
4536 pr_err("no IMSM anchor on %s\n", devname
);
4541 __free_imsm(super
, 0);
4542 /* reload capability and hba */
4544 /* capability and hba must be updated with new super allocation */
4545 find_intel_hba_capability(fd
, super
, devname
);
4546 super
->len
= ROUND_UP(anchor
->mpb_size
, sector_size
);
4547 if (posix_memalign(&super
->buf
, MAX_SECTOR_SIZE
, super
->len
) != 0) {
4549 pr_err("unable to allocate %zu byte mpb buffer\n",
4554 memcpy(super
->buf
, anchor
, sector_size
);
4556 sectors
= mpb_sectors(anchor
, sector_size
) - 1;
4559 if (posix_memalign(&super
->migr_rec_buf
, MAX_SECTOR_SIZE
,
4560 MIGR_REC_BUF_SECTORS
*MAX_SECTOR_SIZE
) != 0) {
4561 pr_err("could not allocate migr_rec buffer\n");
4566 super
->clean_migration_record_by_mdmon
= 0;
4569 check_sum
= __gen_imsm_checksum(super
->anchor
);
4570 if (check_sum
!= __le32_to_cpu(super
->anchor
->check_sum
)) {
4572 pr_err("IMSM checksum %x != %x on %s\n",
4574 __le32_to_cpu(super
->anchor
->check_sum
),
4582 /* read the extended mpb */
4583 if (lseek64(fd
, dsize
- (sector_size
* (2 + sectors
)), SEEK_SET
) < 0) {
4585 pr_err("Cannot seek to extended mpb on %s: %s\n",
4586 devname
, strerror(errno
));
4590 if ((unsigned int)read(fd
, super
->buf
+ sector_size
,
4591 super
->len
- sector_size
) != super
->len
- sector_size
) {
4593 pr_err("Cannot read extended mpb on %s: %s\n",
4594 devname
, strerror(errno
));
4598 check_sum
= __gen_imsm_checksum(super
->anchor
);
4599 if (check_sum
!= __le32_to_cpu(super
->anchor
->check_sum
)) {
4601 pr_err("IMSM checksum %x != %x on %s\n",
4602 check_sum
, __le32_to_cpu(super
->anchor
->check_sum
),
4610 static int read_imsm_migr_rec(int fd
, struct intel_super
*super
);
4612 /* clears hi bits in metadata if MPB_ATTRIB_2TB_DISK not set */
4613 static void clear_hi(struct intel_super
*super
)
4615 struct imsm_super
*mpb
= super
->anchor
;
4617 if (mpb
->attributes
& MPB_ATTRIB_2TB_DISK
)
4619 for (i
= 0; i
< mpb
->num_disks
; ++i
) {
4620 struct imsm_disk
*disk
= &mpb
->disk
[i
];
4621 disk
->total_blocks_hi
= 0;
4623 for (i
= 0; i
< mpb
->num_raid_devs
; ++i
) {
4624 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
4625 for (n
= 0; n
< 2; ++n
) {
4626 struct imsm_map
*map
= get_imsm_map(dev
, n
);
4629 map
->pba_of_lba0_hi
= 0;
4630 map
->blocks_per_member_hi
= 0;
4631 map
->num_data_stripes_hi
= 0;
4637 load_and_parse_mpb(int fd
, struct intel_super
*super
, char *devname
, int keep_fd
)
4641 err
= load_imsm_mpb(fd
, super
, devname
);
4644 if (super
->sector_size
== 4096)
4645 convert_from_4k(super
);
4646 err
= load_imsm_disk(fd
, super
, devname
, keep_fd
);
4649 err
= parse_raid_devices(super
);
4652 err
= load_bbm_log(super
);
4657 static void __free_imsm_disk(struct dl
*d
, int do_close
)
4669 static void free_imsm_disks(struct intel_super
*super
)
4673 while (super
->disks
) {
4675 super
->disks
= d
->next
;
4676 __free_imsm_disk(d
, 1);
4678 while (super
->disk_mgmt_list
) {
4679 d
= super
->disk_mgmt_list
;
4680 super
->disk_mgmt_list
= d
->next
;
4681 __free_imsm_disk(d
, 1);
4683 while (super
->missing
) {
4685 super
->missing
= d
->next
;
4686 __free_imsm_disk(d
, 1);
4691 /* free all the pieces hanging off of a super pointer */
4692 static void __free_imsm(struct intel_super
*super
, int free_disks
)
4694 struct intel_hba
*elem
, *next
;
4700 /* unlink capability description */
4702 if (super
->migr_rec_buf
) {
4703 free(super
->migr_rec_buf
);
4704 super
->migr_rec_buf
= NULL
;
4707 free_imsm_disks(super
);
4708 free_devlist(super
);
4712 free((void *)elem
->path
);
4718 free(super
->bbm_log
);
4722 static void free_imsm(struct intel_super
*super
)
4724 __free_imsm(super
, 1);
4725 free(super
->bb
.entries
);
4729 static void free_super_imsm(struct supertype
*st
)
4731 struct intel_super
*super
= st
->sb
;
4740 static struct intel_super
*alloc_super(void)
4742 struct intel_super
*super
= xcalloc(1, sizeof(*super
));
4744 super
->current_vol
= -1;
4745 super
->create_offset
= ~((unsigned long long) 0);
4747 super
->bb
.entries
= xmalloc(BBM_LOG_MAX_ENTRIES
*
4748 sizeof(struct md_bb_entry
));
4749 if (!super
->bb
.entries
) {
4758 * find and allocate hba and OROM/EFI based on valid fd of RAID component device
4760 static int find_intel_hba_capability(int fd
, struct intel_super
*super
, char *devname
)
4762 struct sys_dev
*hba_name
;
4765 if (is_fd_valid(fd
) && test_partition(fd
)) {
4766 pr_err("imsm: %s is a partition, cannot be used in IMSM\n",
4770 if (!is_fd_valid(fd
) || check_no_platform()) {
4775 hba_name
= find_disk_attached_hba(fd
, NULL
);
4778 pr_err("%s is not attached to Intel(R) RAID controller.\n",
4782 rv
= attach_hba_to_super(super
, hba_name
);
4785 struct intel_hba
*hba
= super
->hba
;
4787 pr_err("%s is attached to Intel(R) %s %s (%s),\n"
4788 " but the container is assigned to Intel(R) %s %s (",
4790 get_sys_dev_type(hba_name
->type
),
4791 hba_name
->type
== SYS_DEV_VMD
|| hba_name
->type
== SYS_DEV_SATA_VMD
?
4792 "domain" : "RAID controller",
4793 hba_name
->pci_id
? : "Err!",
4794 get_sys_dev_type(super
->hba
->type
),
4795 hba
->type
== SYS_DEV_VMD
|| hba_name
->type
== SYS_DEV_SATA_VMD
?
4796 "domain" : "RAID controller");
4799 fprintf(stderr
, "%s", hba
->pci_id
? : "Err!");
4801 fprintf(stderr
, ", ");
4804 fprintf(stderr
, ").\n"
4805 " Mixing devices attached to different controllers is not allowed.\n");
4809 super
->orom
= find_imsm_capability(hba_name
);
4816 /* find_missing - helper routine for load_super_imsm_all that identifies
4817 * disks that have disappeared from the system. This routine relies on
4818 * the mpb being uptodate, which it is at load time.
4820 static int find_missing(struct intel_super
*super
)
4823 struct imsm_super
*mpb
= super
->anchor
;
4825 struct imsm_disk
*disk
;
4827 for (i
= 0; i
< mpb
->num_disks
; i
++) {
4828 disk
= __get_imsm_disk(mpb
, i
);
4829 dl
= serial_to_dl(disk
->serial
, super
);
4833 dl
= xmalloc(sizeof(*dl
));
4837 dl
->devname
= xstrdup("missing");
4839 serialcpy(dl
->serial
, disk
->serial
);
4842 dl
->next
= super
->missing
;
4843 super
->missing
= dl
;
4849 static struct intel_disk
*disk_list_get(__u8
*serial
, struct intel_disk
*disk_list
)
4851 struct intel_disk
*idisk
= disk_list
;
4854 if (serialcmp(idisk
->disk
.serial
, serial
) == 0)
4856 idisk
= idisk
->next
;
4862 static int __prep_thunderdome(struct intel_super
**table
, int tbl_size
,
4863 struct intel_super
*super
,
4864 struct intel_disk
**disk_list
)
4866 struct imsm_disk
*d
= &super
->disks
->disk
;
4867 struct imsm_super
*mpb
= super
->anchor
;
4870 for (i
= 0; i
< tbl_size
; i
++) {
4871 struct imsm_super
*tbl_mpb
= table
[i
]->anchor
;
4872 struct imsm_disk
*tbl_d
= &table
[i
]->disks
->disk
;
4874 if (tbl_mpb
->family_num
== mpb
->family_num
) {
4875 if (tbl_mpb
->check_sum
== mpb
->check_sum
) {
4876 dprintf("mpb from %d:%d matches %d:%d\n",
4877 super
->disks
->major
,
4878 super
->disks
->minor
,
4879 table
[i
]->disks
->major
,
4880 table
[i
]->disks
->minor
);
4884 if (((is_configured(d
) && !is_configured(tbl_d
)) ||
4885 is_configured(d
) == is_configured(tbl_d
)) &&
4886 tbl_mpb
->generation_num
< mpb
->generation_num
) {
4887 /* current version of the mpb is a
4888 * better candidate than the one in
4889 * super_table, but copy over "cross
4890 * generational" status
4892 struct intel_disk
*idisk
;
4894 dprintf("mpb from %d:%d replaces %d:%d\n",
4895 super
->disks
->major
,
4896 super
->disks
->minor
,
4897 table
[i
]->disks
->major
,
4898 table
[i
]->disks
->minor
);
4900 idisk
= disk_list_get(tbl_d
->serial
, *disk_list
);
4901 if (idisk
&& is_failed(&idisk
->disk
))
4902 tbl_d
->status
|= FAILED_DISK
;
4905 struct intel_disk
*idisk
;
4906 struct imsm_disk
*disk
;
4908 /* tbl_mpb is more up to date, but copy
4909 * over cross generational status before
4912 disk
= __serial_to_disk(d
->serial
, mpb
, NULL
);
4913 if (disk
&& is_failed(disk
))
4914 d
->status
|= FAILED_DISK
;
4916 idisk
= disk_list_get(d
->serial
, *disk_list
);
4919 if (disk
&& is_configured(disk
))
4920 idisk
->disk
.status
|= CONFIGURED_DISK
;
4923 dprintf("mpb from %d:%d prefer %d:%d\n",
4924 super
->disks
->major
,
4925 super
->disks
->minor
,
4926 table
[i
]->disks
->major
,
4927 table
[i
]->disks
->minor
);
4935 table
[tbl_size
++] = super
;
4939 /* update/extend the merged list of imsm_disk records */
4940 for (j
= 0; j
< mpb
->num_disks
; j
++) {
4941 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, j
);
4942 struct intel_disk
*idisk
;
4944 idisk
= disk_list_get(disk
->serial
, *disk_list
);
4946 idisk
->disk
.status
|= disk
->status
;
4947 if (is_configured(&idisk
->disk
) ||
4948 is_failed(&idisk
->disk
))
4949 idisk
->disk
.status
&= ~(SPARE_DISK
);
4951 idisk
= xcalloc(1, sizeof(*idisk
));
4952 idisk
->owner
= IMSM_UNKNOWN_OWNER
;
4953 idisk
->disk
= *disk
;
4954 idisk
->next
= *disk_list
;
4958 if (serialcmp(idisk
->disk
.serial
, d
->serial
) == 0)
4965 static struct intel_super
*
4966 validate_members(struct intel_super
*super
, struct intel_disk
*disk_list
,
4969 struct imsm_super
*mpb
= super
->anchor
;
4973 for (i
= 0; i
< mpb
->num_disks
; i
++) {
4974 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, i
);
4975 struct intel_disk
*idisk
;
4977 idisk
= disk_list_get(disk
->serial
, disk_list
);
4979 if (idisk
->owner
== owner
||
4980 idisk
->owner
== IMSM_UNKNOWN_OWNER
)
4983 dprintf("'%.16s' owner %d != %d\n",
4984 disk
->serial
, idisk
->owner
,
4987 dprintf("unknown disk %x [%d]: %.16s\n",
4988 __le32_to_cpu(mpb
->family_num
), i
,
4994 if (ok_count
== mpb
->num_disks
)
4999 static void show_conflicts(__u32 family_num
, struct intel_super
*super_list
)
5001 struct intel_super
*s
;
5003 for (s
= super_list
; s
; s
= s
->next
) {
5004 if (family_num
!= s
->anchor
->family_num
)
5006 pr_err("Conflict, offlining family %#x on '%s'\n",
5007 __le32_to_cpu(family_num
), s
->disks
->devname
);
5011 static struct intel_super
*
5012 imsm_thunderdome(struct intel_super
**super_list
, int len
)
5014 struct intel_super
*super_table
[len
];
5015 struct intel_disk
*disk_list
= NULL
;
5016 struct intel_super
*champion
, *spare
;
5017 struct intel_super
*s
, **del
;
5022 memset(super_table
, 0, sizeof(super_table
));
5023 for (s
= *super_list
; s
; s
= s
->next
)
5024 tbl_size
= __prep_thunderdome(super_table
, tbl_size
, s
, &disk_list
);
5026 for (i
= 0; i
< tbl_size
; i
++) {
5027 struct imsm_disk
*d
;
5028 struct intel_disk
*idisk
;
5029 struct imsm_super
*mpb
= super_table
[i
]->anchor
;
5032 d
= &s
->disks
->disk
;
5034 /* 'd' must appear in merged disk list for its
5035 * configuration to be valid
5037 idisk
= disk_list_get(d
->serial
, disk_list
);
5038 if (idisk
&& idisk
->owner
== i
)
5039 s
= validate_members(s
, disk_list
, i
);
5044 dprintf("marking family: %#x from %d:%d offline\n",
5046 super_table
[i
]->disks
->major
,
5047 super_table
[i
]->disks
->minor
);
5051 /* This is where the mdadm implementation differs from the Windows
5052 * driver which has no strict concept of a container. We can only
5053 * assemble one family from a container, so when returning a prodigal
5054 * array member to this system the code will not be able to disambiguate
5055 * the container contents that should be assembled ("foreign" versus
5056 * "local"). It requires user intervention to set the orig_family_num
5057 * to a new value to establish a new container. The Windows driver in
5058 * this situation fixes up the volume name in place and manages the
5059 * foreign array as an independent entity.
5064 for (i
= 0; i
< tbl_size
; i
++) {
5065 struct intel_super
*tbl_ent
= super_table
[i
];
5071 if (tbl_ent
->anchor
->num_raid_devs
== 0) {
5076 if (s
&& !is_spare
) {
5077 show_conflicts(tbl_ent
->anchor
->family_num
, *super_list
);
5079 } else if (!s
&& !is_spare
)
5092 pr_err("Chose family %#x on '%s', assemble conflicts to new container with '--update=uuid'\n",
5093 __le32_to_cpu(s
->anchor
->family_num
), s
->disks
->devname
);
5095 /* collect all dl's onto 'champion', and update them to
5096 * champion's version of the status
5098 for (s
= *super_list
; s
; s
= s
->next
) {
5099 struct imsm_super
*mpb
= champion
->anchor
;
5100 struct dl
*dl
= s
->disks
;
5105 mpb
->attributes
|= s
->anchor
->attributes
& MPB_ATTRIB_2TB_DISK
;
5107 for (i
= 0; i
< mpb
->num_disks
; i
++) {
5108 struct imsm_disk
*disk
;
5110 disk
= __serial_to_disk(dl
->serial
, mpb
, &dl
->index
);
5113 /* only set index on disks that are a member of
5114 * a populated contianer, i.e. one with
5117 if (is_failed(&dl
->disk
))
5119 else if (is_spare(&dl
->disk
))
5125 if (i
>= mpb
->num_disks
) {
5126 struct intel_disk
*idisk
;
5128 idisk
= disk_list_get(dl
->serial
, disk_list
);
5129 if (idisk
&& is_spare(&idisk
->disk
) &&
5130 !is_failed(&idisk
->disk
) && !is_configured(&idisk
->disk
))
5138 dl
->next
= champion
->disks
;
5139 champion
->disks
= dl
;
5143 /* delete 'champion' from super_list */
5144 for (del
= super_list
; *del
; ) {
5145 if (*del
== champion
) {
5146 *del
= (*del
)->next
;
5149 del
= &(*del
)->next
;
5151 champion
->next
= NULL
;
5155 struct intel_disk
*idisk
= disk_list
;
5157 disk_list
= disk_list
->next
;
5165 get_sra_super_block(int fd
, struct intel_super
**super_list
, char *devname
, int *max
, int keep_fd
);
5166 static int get_super_block(struct intel_super
**super_list
, char *devnm
, char *devname
,
5167 int major
, int minor
, int keep_fd
);
5169 get_devlist_super_block(struct md_list
*devlist
, struct intel_super
**super_list
,
5170 int *max
, int keep_fd
);
5172 static int load_super_imsm_all(struct supertype
*st
, int fd
, void **sbp
,
5173 char *devname
, struct md_list
*devlist
,
5176 struct intel_super
*super_list
= NULL
;
5177 struct intel_super
*super
= NULL
;
5181 if (is_fd_valid(fd
))
5182 /* 'fd' is an opened container */
5183 err
= get_sra_super_block(fd
, &super_list
, devname
, &i
, keep_fd
);
5185 /* get super block from devlist devices */
5186 err
= get_devlist_super_block(devlist
, &super_list
, &i
, keep_fd
);
5189 /* all mpbs enter, maybe one leaves */
5190 super
= imsm_thunderdome(&super_list
, i
);
5196 if (find_missing(super
) != 0) {
5202 /* load migration record */
5203 err
= load_imsm_migr_rec(super
);
5205 /* migration is in progress,
5206 * but migr_rec cannot be loaded,
5212 /* Check migration compatibility */
5213 if (err
== 0 && check_mpb_migr_compatibility(super
) != 0) {
5214 pr_err("Unsupported migration detected");
5216 fprintf(stderr
, " on %s\n", devname
);
5218 fprintf(stderr
, " (IMSM).\n");
5227 while (super_list
) {
5228 struct intel_super
*s
= super_list
;
5230 super_list
= super_list
->next
;
5238 if (is_fd_valid(fd
))
5239 strcpy(st
->container_devnm
, fd2devnm(fd
));
5241 st
->container_devnm
[0] = 0;
5242 if (err
== 0 && st
->ss
== NULL
) {
5243 st
->ss
= &super_imsm
;
5244 st
->minor_version
= 0;
5245 st
->max_devs
= IMSM_MAX_DEVICES
;
5251 get_devlist_super_block(struct md_list
*devlist
, struct intel_super
**super_list
,
5252 int *max
, int keep_fd
)
5254 struct md_list
*tmpdev
;
5258 for (i
= 0, tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
5259 if (tmpdev
->used
!= 1)
5261 if (tmpdev
->container
== 1) {
5263 int fd
= dev_open(tmpdev
->devname
, O_RDONLY
|O_EXCL
);
5264 if (!is_fd_valid(fd
)) {
5265 pr_err("cannot open device %s: %s\n",
5266 tmpdev
->devname
, strerror(errno
));
5270 err
= get_sra_super_block(fd
, super_list
,
5271 tmpdev
->devname
, &lmax
,
5280 int major
= major(tmpdev
->st_rdev
);
5281 int minor
= minor(tmpdev
->st_rdev
);
5282 err
= get_super_block(super_list
,
5299 static int get_super_block(struct intel_super
**super_list
, char *devnm
, char *devname
,
5300 int major
, int minor
, int keep_fd
)
5302 struct intel_super
*s
;
5314 sprintf(nm
, "%d:%d", major
, minor
);
5315 dfd
= dev_open(nm
, O_RDWR
);
5316 if (!is_fd_valid(dfd
)) {
5321 if (!get_dev_sector_size(dfd
, NULL
, &s
->sector_size
)) {
5325 find_intel_hba_capability(dfd
, s
, devname
);
5326 err
= load_and_parse_mpb(dfd
, s
, NULL
, keep_fd
);
5328 /* retry the load if we might have raced against mdmon */
5329 if (err
== 3 && devnm
&& mdmon_running(devnm
))
5330 for (retry
= 0; retry
< 3; retry
++) {
5331 sleep_for(0, MSEC_TO_NSEC(3), true);
5332 err
= load_and_parse_mpb(dfd
, s
, NULL
, keep_fd
);
5338 s
->next
= *super_list
;
5352 get_sra_super_block(int fd
, struct intel_super
**super_list
, char *devname
, int *max
, int keep_fd
)
5359 sra
= sysfs_read(fd
, NULL
, GET_LEVEL
|GET_VERSION
|GET_DEVS
|GET_STATE
);
5363 if (sra
->array
.major_version
!= -1 ||
5364 sra
->array
.minor_version
!= -2 ||
5365 strcmp(sra
->text_version
, "imsm") != 0) {
5370 devnm
= fd2devnm(fd
);
5371 for (sd
= sra
->devs
, i
= 0; sd
; sd
= sd
->next
, i
++) {
5372 if (get_super_block(super_list
, devnm
, devname
,
5373 sd
->disk
.major
, sd
->disk
.minor
, keep_fd
) != 0) {
5384 static int load_container_imsm(struct supertype
*st
, int fd
, char *devname
)
5386 return load_super_imsm_all(st
, fd
, &st
->sb
, devname
, NULL
, 1);
5389 static int load_super_imsm(struct supertype
*st
, int fd
, char *devname
)
5391 struct intel_super
*super
;
5395 if (test_partition(fd
))
5396 /* IMSM not allowed on partitions */
5399 free_super_imsm(st
);
5401 super
= alloc_super();
5405 if (!get_dev_sector_size(fd
, NULL
, &super
->sector_size
)) {
5409 /* Load hba and capabilities if they exist.
5410 * But do not preclude loading metadata in case capabilities or hba are
5411 * non-compliant and ignore_hw_compat is set.
5413 rv
= find_intel_hba_capability(fd
, super
, devname
);
5414 /* no orom/efi or non-intel hba of the disk */
5415 if (rv
!= 0 && st
->ignore_hw_compat
== 0) {
5417 pr_err("No OROM/EFI properties for %s\n", devname
);
5421 rv
= load_and_parse_mpb(fd
, super
, devname
, 0);
5423 /* retry the load if we might have raced against mdmon */
5425 struct mdstat_ent
*mdstat
= NULL
;
5426 char *name
= fd2kname(fd
);
5429 mdstat
= mdstat_by_component(name
);
5431 if (mdstat
&& mdmon_running(mdstat
->devnm
) && getpid() != mdmon_pid(mdstat
->devnm
)) {
5432 for (retry
= 0; retry
< 3; retry
++) {
5433 sleep_for(0, MSEC_TO_NSEC(3), true);
5434 rv
= load_and_parse_mpb(fd
, super
, devname
, 0);
5440 free_mdstat(mdstat
);
5445 pr_err("Failed to load all information sections on %s\n", devname
);
5451 if (st
->ss
== NULL
) {
5452 st
->ss
= &super_imsm
;
5453 st
->minor_version
= 0;
5454 st
->max_devs
= IMSM_MAX_DEVICES
;
5457 /* load migration record */
5458 if (load_imsm_migr_rec(super
) == 0) {
5459 /* Check for unsupported migration features */
5460 if (check_mpb_migr_compatibility(super
) != 0) {
5461 pr_err("Unsupported migration detected");
5463 fprintf(stderr
, " on %s\n", devname
);
5465 fprintf(stderr
, " (IMSM).\n");
5473 static __u16
info_to_blocks_per_strip(mdu_array_info_t
*info
)
5475 if (info
->level
== 1)
5477 return info
->chunk_size
>> 9;
5480 static unsigned long long info_to_blocks_per_member(mdu_array_info_t
*info
,
5481 unsigned long long size
)
5483 if (info
->level
== 1)
5486 return (size
* 2) & ~(info_to_blocks_per_strip(info
) - 1);
5489 static void imsm_update_version_info(struct intel_super
*super
)
5491 /* update the version and attributes */
5492 struct imsm_super
*mpb
= super
->anchor
;
5494 struct imsm_dev
*dev
;
5495 struct imsm_map
*map
;
5498 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
5499 dev
= get_imsm_dev(super
, i
);
5500 map
= get_imsm_map(dev
, MAP_0
);
5501 if (__le32_to_cpu(dev
->size_high
) > 0)
5502 mpb
->attributes
|= MPB_ATTRIB_2TB
;
5504 /* FIXME detect when an array spans a port multiplier */
5506 mpb
->attributes
|= MPB_ATTRIB_PM
;
5509 if (mpb
->num_raid_devs
> 1 ||
5510 mpb
->attributes
!= MPB_ATTRIB_CHECKSUM_VERIFY
) {
5511 version
= MPB_VERSION_ATTRIBS
;
5512 switch (get_imsm_raid_level(map
)) {
5513 case 0: mpb
->attributes
|= MPB_ATTRIB_RAID0
; break;
5514 case 1: mpb
->attributes
|= MPB_ATTRIB_RAID1
; break;
5515 case 10: mpb
->attributes
|= MPB_ATTRIB_RAID10
; break;
5516 case 5: mpb
->attributes
|= MPB_ATTRIB_RAID5
; break;
5519 if (map
->num_members
>= 5)
5520 version
= MPB_VERSION_5OR6_DISK_ARRAY
;
5521 else if (dev
->status
== DEV_CLONE_N_GO
)
5522 version
= MPB_VERSION_CNG
;
5523 else if (get_imsm_raid_level(map
) == 5)
5524 version
= MPB_VERSION_RAID5
;
5525 else if (map
->num_members
>= 3)
5526 version
= MPB_VERSION_3OR4_DISK_ARRAY
;
5527 else if (get_imsm_raid_level(map
) == 1)
5528 version
= MPB_VERSION_RAID1
;
5530 version
= MPB_VERSION_RAID0
;
5532 strcpy(((char *) mpb
->sig
) + strlen(MPB_SIGNATURE
), version
);
5537 * imsm_check_name() - check imsm naming criteria.
5538 * @super: &intel_super pointer, not NULL.
5539 * @name: name to check.
5540 * @verbose: verbose level.
5542 * Name must be no longer than &MAX_RAID_SERIAL_LEN and must be unique across volumes.
5544 * Returns: &true if @name matches, &false otherwise.
5546 static bool imsm_is_name_allowed(struct intel_super
*super
, const char * const name
,
5549 struct imsm_super
*mpb
= super
->anchor
;
5552 if (is_string_lq(name
, MAX_RAID_SERIAL_LEN
+ 1) == false) {
5553 pr_vrb("imsm: Name \"%s\" is too long\n", name
);
5557 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
5558 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
5560 if (strncmp((char *) dev
->volume
, name
, MAX_RAID_SERIAL_LEN
) == 0) {
5561 pr_vrb("imsm: Name \"%s\" already exists\n", name
);
5569 static int init_super_imsm_volume(struct supertype
*st
, mdu_array_info_t
*info
,
5570 struct shape
*s
, char *name
,
5571 char *homehost
, int *uuid
,
5572 long long data_offset
)
5574 /* We are creating a volume inside a pre-existing container.
5575 * so st->sb is already set.
5577 struct intel_super
*super
= st
->sb
;
5578 unsigned int sector_size
= super
->sector_size
;
5579 struct imsm_super
*mpb
= super
->anchor
;
5580 struct intel_dev
*dv
;
5581 struct imsm_dev
*dev
;
5582 struct imsm_vol
*vol
;
5583 struct imsm_map
*map
;
5584 int idx
= mpb
->num_raid_devs
;
5587 unsigned long long array_blocks
;
5588 size_t size_old
, size_new
;
5589 unsigned int data_disks
;
5590 unsigned long long size_per_member
;
5592 if (super
->orom
&& mpb
->num_raid_devs
>= super
->orom
->vpa
) {
5593 pr_err("This imsm-container already has the maximum of %d volumes\n", super
->orom
->vpa
);
5597 /* ensure the mpb is large enough for the new data */
5598 size_old
= __le32_to_cpu(mpb
->mpb_size
);
5599 size_new
= disks_to_mpb_size(info
->nr_disks
);
5600 if (size_new
> size_old
) {
5602 size_t size_round
= ROUND_UP(size_new
, sector_size
);
5604 if (posix_memalign(&mpb_new
, sector_size
, size_round
) != 0) {
5605 pr_err("could not allocate new mpb\n");
5608 if (posix_memalign(&super
->migr_rec_buf
, MAX_SECTOR_SIZE
,
5609 MIGR_REC_BUF_SECTORS
*
5610 MAX_SECTOR_SIZE
) != 0) {
5611 pr_err("could not allocate migr_rec buffer\n");
5617 memcpy(mpb_new
, mpb
, size_old
);
5620 super
->anchor
= mpb_new
;
5621 mpb
->mpb_size
= __cpu_to_le32(size_new
);
5622 memset(mpb_new
+ size_old
, 0, size_round
- size_old
);
5623 super
->len
= size_round
;
5625 super
->current_vol
= idx
;
5627 /* handle 'failed_disks' by either:
5628 * a) create dummy disk entries in the table if this the first
5629 * volume in the array. We add them here as this is the only
5630 * opportunity to add them. add_to_super_imsm_volume()
5631 * handles the non-failed disks and continues incrementing
5633 * b) validate that 'failed_disks' matches the current number
5634 * of missing disks if the container is populated
5636 if (super
->current_vol
== 0) {
5638 for (i
= 0; i
< info
->failed_disks
; i
++) {
5639 struct imsm_disk
*disk
;
5642 disk
= __get_imsm_disk(mpb
, i
);
5643 disk
->status
= CONFIGURED_DISK
| FAILED_DISK
;
5644 disk
->scsi_id
= __cpu_to_le32(~(__u32
)0);
5645 snprintf((char *) disk
->serial
, MAX_RAID_SERIAL_LEN
,
5646 "missing:%d", (__u8
)i
);
5648 find_missing(super
);
5653 for (d
= super
->missing
; d
; d
= d
->next
)
5655 if (info
->failed_disks
> missing
) {
5656 pr_err("unable to add 'missing' disk to container\n");
5661 if (imsm_is_name_allowed(super
, name
, 1) == false)
5664 dv
= xmalloc(sizeof(*dv
));
5665 dev
= xcalloc(1, sizeof(*dev
) + sizeof(__u32
) * (info
->raid_disks
- 1));
5667 * Explicitly allow truncating to not confuse gcc's
5668 * -Werror=stringop-truncation
5670 namelen
= min((int) strlen(name
), MAX_RAID_SERIAL_LEN
);
5671 memcpy(dev
->volume
, name
, namelen
);
5672 array_blocks
= calc_array_size(info
->level
, info
->raid_disks
,
5673 info
->layout
, info
->chunk_size
,
5674 s
->size
* BLOCKS_PER_KB
);
5675 data_disks
= get_data_disks(info
->level
, info
->layout
,
5677 array_blocks
= round_size_to_mb(array_blocks
, data_disks
);
5678 size_per_member
= array_blocks
/ data_disks
;
5680 set_imsm_dev_size(dev
, array_blocks
);
5681 dev
->status
= (DEV_READ_COALESCING
| DEV_WRITE_COALESCING
);
5683 vol
->migr_state
= 0;
5684 set_migr_type(dev
, MIGR_INIT
);
5685 vol
->dirty
= !info
->state
;
5686 set_vol_curr_migr_unit(dev
, 0);
5687 map
= get_imsm_map(dev
, MAP_0
);
5688 set_pba_of_lba0(map
, super
->create_offset
);
5689 map
->blocks_per_strip
= __cpu_to_le16(info_to_blocks_per_strip(info
));
5690 map
->failed_disk_num
= ~0;
5691 if (info
->level
> 0)
5692 map
->map_state
= (info
->state
? IMSM_T_STATE_NORMAL
5693 : IMSM_T_STATE_UNINITIALIZED
);
5695 map
->map_state
= info
->failed_disks
? IMSM_T_STATE_FAILED
:
5696 IMSM_T_STATE_NORMAL
;
5699 if (info
->level
== 1 && info
->raid_disks
> 2) {
5702 pr_err("imsm does not support more than 2 disksin a raid1 volume\n");
5706 map
->raid_level
= info
->level
;
5707 if (info
->level
== 10)
5708 map
->raid_level
= 1;
5709 set_num_domains(map
);
5711 size_per_member
+= NUM_BLOCKS_DIRTY_STRIPE_REGION
;
5712 set_blocks_per_member(map
, info_to_blocks_per_member(info
,
5716 map
->num_members
= info
->raid_disks
;
5717 update_num_data_stripes(map
, array_blocks
);
5718 for (i
= 0; i
< map
->num_members
; i
++) {
5719 /* initialized in add_to_super */
5720 set_imsm_ord_tbl_ent(map
, i
, IMSM_ORD_REBUILD
);
5722 mpb
->num_raid_devs
++;
5723 mpb
->num_raid_devs_created
++;
5724 dev
->my_vol_raid_dev_num
= mpb
->num_raid_devs_created
;
5726 if (s
->consistency_policy
<= CONSISTENCY_POLICY_RESYNC
) {
5727 dev
->rwh_policy
= RWH_MULTIPLE_OFF
;
5728 } else if (s
->consistency_policy
== CONSISTENCY_POLICY_PPL
) {
5729 dev
->rwh_policy
= RWH_MULTIPLE_DISTRIBUTED
;
5733 pr_err("imsm does not support consistency policy %s\n",
5734 map_num_s(consistency_policies
, s
->consistency_policy
));
5739 dv
->index
= super
->current_vol
;
5740 dv
->next
= super
->devlist
;
5741 super
->devlist
= dv
;
5743 imsm_update_version_info(super
);
5748 static int init_super_imsm(struct supertype
*st
, mdu_array_info_t
*info
,
5749 struct shape
*s
, char *name
,
5750 char *homehost
, int *uuid
,
5751 unsigned long long data_offset
)
5753 /* This is primarily called by Create when creating a new array.
5754 * We will then get add_to_super called for each component, and then
5755 * write_init_super called to write it out to each device.
5756 * For IMSM, Create can create on fresh devices or on a pre-existing
5758 * To create on a pre-existing array a different method will be called.
5759 * This one is just for fresh drives.
5761 struct intel_super
*super
;
5762 struct imsm_super
*mpb
;
5766 if (data_offset
!= INVALID_SECTORS
) {
5767 pr_err("data-offset not supported by imsm\n");
5772 return init_super_imsm_volume(st
, info
, s
, name
, homehost
, uuid
,
5776 mpb_size
= disks_to_mpb_size(info
->nr_disks
);
5778 mpb_size
= MAX_SECTOR_SIZE
;
5780 super
= alloc_super();
5782 posix_memalign(&super
->buf
, MAX_SECTOR_SIZE
, mpb_size
) != 0) {
5787 pr_err("could not allocate superblock\n");
5790 if (posix_memalign(&super
->migr_rec_buf
, MAX_SECTOR_SIZE
,
5791 MIGR_REC_BUF_SECTORS
*MAX_SECTOR_SIZE
) != 0) {
5792 pr_err("could not allocate migr_rec buffer\n");
5797 memset(super
->buf
, 0, mpb_size
);
5799 mpb
->mpb_size
= __cpu_to_le32(mpb_size
);
5803 /* zeroing superblock */
5807 mpb
->attributes
= MPB_ATTRIB_CHECKSUM_VERIFY
;
5809 version
= (char *) mpb
->sig
;
5810 strcpy(version
, MPB_SIGNATURE
);
5811 version
+= strlen(MPB_SIGNATURE
);
5812 strcpy(version
, MPB_VERSION_RAID0
);
5817 static int drive_validate_sector_size(struct intel_super
*super
, struct dl
*dl
)
5819 unsigned int member_sector_size
;
5821 if (!is_fd_valid(dl
->fd
)) {
5822 pr_err("Invalid file descriptor for %s\n", dl
->devname
);
5826 if (!get_dev_sector_size(dl
->fd
, dl
->devname
, &member_sector_size
))
5828 if (member_sector_size
!= super
->sector_size
)
5833 static int add_to_super_imsm_volume(struct supertype
*st
, mdu_disk_info_t
*dk
,
5834 int fd
, char *devname
)
5836 struct intel_super
*super
= st
->sb
;
5837 struct imsm_super
*mpb
= super
->anchor
;
5838 struct imsm_disk
*_disk
;
5839 struct imsm_dev
*dev
;
5840 struct imsm_map
*map
;
5845 if (!is_fd_valid(fd
))
5848 dev
= get_imsm_dev(super
, super
->current_vol
);
5849 map
= get_imsm_map(dev
, MAP_0
);
5851 if (! (dk
->state
& (1<<MD_DISK_SYNC
))) {
5852 pr_err("%s: Cannot add spare devices to IMSM volume\n",
5857 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
5859 if (dl
->raiddisk
== dk
->raid_disk
)
5861 } else if (dl
->major
== dk
->major
&& dl
->minor
== dk
->minor
)
5867 pr_err("%s is not a member of the same container.\n",
5872 if (!autolayout
&& super
->current_vol
> 0) {
5873 int _slot
= get_disk_slot_in_dev(super
, 0, dl
->index
);
5875 if (_slot
!= dk
->raid_disk
) {
5876 pr_err("Member %s is in %d slot for the first volume, but is in %d slot for a new volume.\n",
5877 dl
->devname
, _slot
, dk
->raid_disk
);
5878 pr_err("Raid members are in different order than for the first volume, aborting.\n");
5883 if (mpb
->num_disks
== 0)
5884 if (!get_dev_sector_size(dl
->fd
, dl
->devname
,
5885 &super
->sector_size
))
5888 if (!drive_validate_sector_size(super
, dl
)) {
5889 pr_err("Combining drives of different sector size in one volume is not allowed\n");
5893 /* add a pristine spare to the metadata */
5894 if (dl
->index
< 0) {
5895 dl
->index
= super
->anchor
->num_disks
;
5896 super
->anchor
->num_disks
++;
5898 /* Check the device has not already been added */
5899 slot
= get_imsm_disk_slot(map
, dl
->index
);
5901 (get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
) & IMSM_ORD_REBUILD
) == 0) {
5902 pr_err("%s has been included in this array twice\n",
5906 set_imsm_ord_tbl_ent(map
, dk
->raid_disk
, dl
->index
);
5907 dl
->disk
.status
= CONFIGURED_DISK
;
5909 /* update size of 'missing' disks to be at least as large as the
5910 * largest acitve member (we only have dummy missing disks when
5911 * creating the first volume)
5913 if (super
->current_vol
== 0) {
5914 for (df
= super
->missing
; df
; df
= df
->next
) {
5915 if (total_blocks(&dl
->disk
) > total_blocks(&df
->disk
))
5916 set_total_blocks(&df
->disk
, total_blocks(&dl
->disk
));
5917 _disk
= __get_imsm_disk(mpb
, df
->index
);
5922 /* refresh unset/failed slots to point to valid 'missing' entries */
5923 for (df
= super
->missing
; df
; df
= df
->next
)
5924 for (slot
= 0; slot
< mpb
->num_disks
; slot
++) {
5925 __u32 ord
= get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
);
5927 if ((ord
& IMSM_ORD_REBUILD
) == 0)
5929 set_imsm_ord_tbl_ent(map
, slot
, df
->index
| IMSM_ORD_REBUILD
);
5930 if (is_gen_migration(dev
)) {
5931 struct imsm_map
*map2
= get_imsm_map(dev
,
5933 int slot2
= get_imsm_disk_slot(map2
, df
->index
);
5934 if (slot2
< map2
->num_members
&& slot2
>= 0) {
5935 __u32 ord2
= get_imsm_ord_tbl_ent(dev
,
5938 if ((unsigned)df
->index
==
5940 set_imsm_ord_tbl_ent(map2
,
5946 dprintf("set slot:%d to missing disk:%d\n", slot
, df
->index
);
5950 /* if we are creating the first raid device update the family number */
5951 if (super
->current_vol
== 0) {
5953 struct imsm_dev
*_dev
= __get_imsm_dev(mpb
, 0);
5955 _disk
= __get_imsm_disk(mpb
, dl
->index
);
5957 pr_err("BUG mpb setup error\n");
5963 sum
+= __gen_imsm_checksum(mpb
);
5964 mpb
->family_num
= __cpu_to_le32(sum
);
5965 mpb
->orig_family_num
= mpb
->family_num
;
5966 mpb
->creation_time
= __cpu_to_le64((__u64
)time(NULL
));
5968 super
->current_disk
= dl
;
5973 * Function marks disk as spare and restores disk serial
5974 * in case it was previously marked as failed by takeover operation
5976 * -1 : critical error
5977 * 0 : disk is marked as spare but serial is not set
5980 int mark_spare(struct dl
*disk
)
5982 __u8 serial
[MAX_RAID_SERIAL_LEN
];
5989 if (!imsm_read_serial(disk
->fd
, NULL
, serial
, MAX_RAID_SERIAL_LEN
)) {
5990 /* Restore disk serial number, because takeover marks disk
5991 * as failed and adds to serial ':0' before it becomes
5994 serialcpy(disk
->serial
, serial
);
5995 serialcpy(disk
->disk
.serial
, serial
);
5998 disk
->disk
.status
= SPARE_DISK
;
6005 static int write_super_imsm_spare(struct intel_super
*super
, struct dl
*d
);
6007 static int add_to_super_imsm(struct supertype
*st
, mdu_disk_info_t
*dk
,
6008 int fd
, char *devname
,
6009 unsigned long long data_offset
)
6011 struct intel_super
*super
= st
->sb
;
6013 unsigned long long size
;
6014 unsigned int member_sector_size
;
6019 /* If we are on an RAID enabled platform check that the disk is
6020 * attached to the raid controller.
6021 * We do not need to test disks attachment for container based additions,
6022 * they shall be already tested when container was created/assembled.
6024 rv
= find_intel_hba_capability(fd
, super
, devname
);
6025 /* no orom/efi or non-intel hba of the disk */
6027 dprintf("capability: %p fd: %d ret: %d\n",
6028 super
->orom
, fd
, rv
);
6032 if (super
->current_vol
>= 0)
6033 return add_to_super_imsm_volume(st
, dk
, fd
, devname
);
6036 dd
= xcalloc(sizeof(*dd
), 1);
6037 dd
->major
= major(stb
.st_rdev
);
6038 dd
->minor
= minor(stb
.st_rdev
);
6039 dd
->devname
= devname
? xstrdup(devname
) : NULL
;
6042 dd
->action
= DISK_ADD
;
6043 rv
= imsm_read_serial(fd
, devname
, dd
->serial
, MAX_RAID_SERIAL_LEN
);
6045 pr_err("failed to retrieve scsi serial, aborting\n");
6046 __free_imsm_disk(dd
, 0);
6050 if (super
->hba
&& ((super
->hba
->type
== SYS_DEV_NVME
) ||
6051 (super
->hba
->type
== SYS_DEV_VMD
))) {
6053 char cntrl_path
[PATH_MAX
];
6055 char pci_dev_path
[PATH_MAX
];
6057 if (!diskfd_to_devpath(fd
, 2, pci_dev_path
) ||
6058 !diskfd_to_devpath(fd
, 1, cntrl_path
)) {
6059 pr_err("failed to get dev paths, aborting\n");
6060 __free_imsm_disk(dd
, 0);
6064 cntrl_name
= basename(cntrl_path
);
6065 if (is_multipath_nvme(fd
))
6066 pr_err("%s controller supports Multi-Path I/O, Intel (R) VROC does not support multipathing\n",
6069 if (devpath_to_vendor(pci_dev_path
) == 0x8086) {
6071 * If Intel's NVMe drive has serial ended with
6072 * "-A","-B","-1" or "-2" it means that this is "x8"
6073 * device (double drive on single PCIe card).
6074 * User should be warned about potential data loss.
6076 for (i
= MAX_RAID_SERIAL_LEN
-1; i
> 0; i
--) {
6077 /* Skip empty character at the end */
6078 if (dd
->serial
[i
] == 0)
6081 if (((dd
->serial
[i
] == 'A') ||
6082 (dd
->serial
[i
] == 'B') ||
6083 (dd
->serial
[i
] == '1') ||
6084 (dd
->serial
[i
] == '2')) &&
6085 (dd
->serial
[i
-1] == '-'))
6086 pr_err("\tThe action you are about to take may put your data at risk.\n"
6087 "\tPlease note that x8 devices may consist of two separate x4 devices "
6088 "located on a single PCIe port.\n"
6089 "\tRAID 0 is the only supported configuration for this type of x8 device.\n");
6092 } else if (super
->hba
->type
== SYS_DEV_VMD
&& super
->orom
&&
6093 !imsm_orom_has_tpv_support(super
->orom
)) {
6094 pr_err("\tPlatform configuration does not support non-Intel NVMe drives.\n"
6095 "\tPlease refer to Intel(R) RSTe/VROC user guide.\n");
6096 __free_imsm_disk(dd
, 0);
6101 get_dev_size(fd
, NULL
, &size
);
6102 if (!get_dev_sector_size(fd
, NULL
, &member_sector_size
)) {
6103 __free_imsm_disk(dd
, 0);
6107 if (super
->sector_size
== 0) {
6108 /* this a first device, so sector_size is not set yet */
6109 super
->sector_size
= member_sector_size
;
6112 /* clear migr_rec when adding disk to container */
6113 memset(super
->migr_rec_buf
, 0, MIGR_REC_BUF_SECTORS
*MAX_SECTOR_SIZE
);
6114 if (lseek64(fd
, size
- MIGR_REC_SECTOR_POSITION
*member_sector_size
,
6116 if ((unsigned int)write(fd
, super
->migr_rec_buf
,
6117 MIGR_REC_BUF_SECTORS
*member_sector_size
) !=
6118 MIGR_REC_BUF_SECTORS
*member_sector_size
)
6119 perror("Write migr_rec failed");
6123 serialcpy(dd
->disk
.serial
, dd
->serial
);
6124 set_total_blocks(&dd
->disk
, size
);
6125 if (__le32_to_cpu(dd
->disk
.total_blocks_hi
) > 0) {
6126 struct imsm_super
*mpb
= super
->anchor
;
6127 mpb
->attributes
|= MPB_ATTRIB_2TB_DISK
;
6130 if (sysfs_disk_to_scsi_id(fd
, &id
) == 0)
6131 dd
->disk
.scsi_id
= __cpu_to_le32(id
);
6133 dd
->disk
.scsi_id
= __cpu_to_le32(0);
6135 if (st
->update_tail
) {
6136 dd
->next
= super
->disk_mgmt_list
;
6137 super
->disk_mgmt_list
= dd
;
6139 /* this is called outside of mdmon
6140 * write initial spare metadata
6141 * mdmon will overwrite it.
6143 dd
->next
= super
->disks
;
6145 write_super_imsm_spare(super
, dd
);
6151 static int remove_from_super_imsm(struct supertype
*st
, mdu_disk_info_t
*dk
)
6153 struct intel_super
*super
= st
->sb
;
6156 /* remove from super works only in mdmon - for communication
6157 * manager - monitor. Check if communication memory buffer
6160 if (!st
->update_tail
) {
6161 pr_err("shall be used in mdmon context only\n");
6164 dd
= xcalloc(1, sizeof(*dd
));
6165 dd
->major
= dk
->major
;
6166 dd
->minor
= dk
->minor
;
6169 dd
->action
= DISK_REMOVE
;
6171 dd
->next
= super
->disk_mgmt_list
;
6172 super
->disk_mgmt_list
= dd
;
6177 static int store_imsm_mpb(int fd
, struct imsm_super
*mpb
);
6180 char buf
[MAX_SECTOR_SIZE
];
6181 struct imsm_super anchor
;
6182 } spare_record
__attribute__ ((aligned(MAX_SECTOR_SIZE
)));
6185 static int write_super_imsm_spare(struct intel_super
*super
, struct dl
*d
)
6187 struct imsm_super
*mpb
= super
->anchor
;
6188 struct imsm_super
*spare
= &spare_record
.anchor
;
6194 spare
->mpb_size
= __cpu_to_le32(sizeof(struct imsm_super
));
6195 spare
->generation_num
= __cpu_to_le32(1UL);
6196 spare
->attributes
= MPB_ATTRIB_CHECKSUM_VERIFY
;
6197 spare
->num_disks
= 1;
6198 spare
->num_raid_devs
= 0;
6199 spare
->cache_size
= mpb
->cache_size
;
6200 spare
->pwr_cycle_count
= __cpu_to_le32(1);
6202 snprintf((char *) spare
->sig
, MAX_SIGNATURE_LENGTH
,
6203 MPB_SIGNATURE MPB_VERSION_RAID0
);
6205 spare
->disk
[0] = d
->disk
;
6206 if (__le32_to_cpu(d
->disk
.total_blocks_hi
) > 0)
6207 spare
->attributes
|= MPB_ATTRIB_2TB_DISK
;
6209 if (super
->sector_size
== 4096)
6210 convert_to_4k_imsm_disk(&spare
->disk
[0]);
6212 sum
= __gen_imsm_checksum(spare
);
6213 spare
->family_num
= __cpu_to_le32(sum
);
6214 spare
->orig_family_num
= 0;
6215 sum
= __gen_imsm_checksum(spare
);
6216 spare
->check_sum
= __cpu_to_le32(sum
);
6218 if (store_imsm_mpb(d
->fd
, spare
)) {
6219 pr_err("failed for device %d:%d %s\n",
6220 d
->major
, d
->minor
, strerror(errno
));
6226 /* spare records have their own family number and do not have any defined raid
6229 static int write_super_imsm_spares(struct intel_super
*super
, int doclose
)
6233 for (d
= super
->disks
; d
; d
= d
->next
) {
6237 if (write_super_imsm_spare(super
, d
))
6247 static int write_super_imsm(struct supertype
*st
, int doclose
)
6249 struct intel_super
*super
= st
->sb
;
6250 unsigned int sector_size
= super
->sector_size
;
6251 struct imsm_super
*mpb
= super
->anchor
;
6257 __u32 mpb_size
= sizeof(struct imsm_super
) - sizeof(struct imsm_disk
);
6259 int clear_migration_record
= 1;
6262 /* 'generation' is incremented everytime the metadata is written */
6263 generation
= __le32_to_cpu(mpb
->generation_num
);
6265 mpb
->generation_num
= __cpu_to_le32(generation
);
6267 /* fix up cases where previous mdadm releases failed to set
6270 if (mpb
->orig_family_num
== 0)
6271 mpb
->orig_family_num
= mpb
->family_num
;
6273 for (d
= super
->disks
; d
; d
= d
->next
) {
6277 mpb
->disk
[d
->index
] = d
->disk
;
6281 for (d
= super
->missing
; d
; d
= d
->next
) {
6282 mpb
->disk
[d
->index
] = d
->disk
;
6285 mpb
->num_disks
= num_disks
;
6286 mpb_size
+= sizeof(struct imsm_disk
) * mpb
->num_disks
;
6288 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
6289 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
6290 struct imsm_dev
*dev2
= get_imsm_dev(super
, i
);
6292 imsm_copy_dev(dev
, dev2
);
6293 mpb_size
+= sizeof_imsm_dev(dev
, 0);
6295 if (is_gen_migration(dev2
))
6296 clear_migration_record
= 0;
6299 bbm_log_size
= get_imsm_bbm_log_size(super
->bbm_log
);
6302 memcpy((void *)mpb
+ mpb_size
, super
->bbm_log
, bbm_log_size
);
6303 mpb
->attributes
|= MPB_ATTRIB_BBM
;
6305 mpb
->attributes
&= ~MPB_ATTRIB_BBM
;
6307 super
->anchor
->bbm_log_size
= __cpu_to_le32(bbm_log_size
);
6308 mpb_size
+= bbm_log_size
;
6309 mpb
->mpb_size
= __cpu_to_le32(mpb_size
);
6312 assert(super
->len
== 0 || mpb_size
<= super
->len
);
6315 /* recalculate checksum */
6316 sum
= __gen_imsm_checksum(mpb
);
6317 mpb
->check_sum
= __cpu_to_le32(sum
);
6319 if (super
->clean_migration_record_by_mdmon
) {
6320 clear_migration_record
= 1;
6321 super
->clean_migration_record_by_mdmon
= 0;
6323 if (clear_migration_record
)
6324 memset(super
->migr_rec_buf
, 0,
6325 MIGR_REC_BUF_SECTORS
*MAX_SECTOR_SIZE
);
6327 if (sector_size
== 4096)
6328 convert_to_4k(super
);
6330 /* write the mpb for disks that compose raid devices */
6331 for (d
= super
->disks
; d
; d
= d
->next
) {
6332 if (d
->index
< 0 || is_failed(&d
->disk
))
6335 if (clear_migration_record
) {
6336 unsigned long long dsize
;
6338 get_dev_size(d
->fd
, NULL
, &dsize
);
6339 if (lseek64(d
->fd
, dsize
- sector_size
,
6341 if ((unsigned int)write(d
->fd
,
6342 super
->migr_rec_buf
,
6343 MIGR_REC_BUF_SECTORS
*sector_size
) !=
6344 MIGR_REC_BUF_SECTORS
*sector_size
)
6345 perror("Write migr_rec failed");
6349 if (store_imsm_mpb(d
->fd
, mpb
))
6351 "failed for device %d:%d (fd: %d)%s\n",
6353 d
->fd
, strerror(errno
));
6360 return write_super_imsm_spares(super
, doclose
);
6365 static int create_array(struct supertype
*st
, int dev_idx
)
6368 struct imsm_update_create_array
*u
;
6369 struct intel_super
*super
= st
->sb
;
6370 struct imsm_dev
*dev
= get_imsm_dev(super
, dev_idx
);
6371 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
6372 struct disk_info
*inf
;
6373 struct imsm_disk
*disk
;
6376 len
= sizeof(*u
) - sizeof(*dev
) + sizeof_imsm_dev(dev
, 0) +
6377 sizeof(*inf
) * map
->num_members
;
6379 u
->type
= update_create_array
;
6380 u
->dev_idx
= dev_idx
;
6381 imsm_copy_dev(&u
->dev
, dev
);
6382 inf
= get_disk_info(u
);
6383 for (i
= 0; i
< map
->num_members
; i
++) {
6384 int idx
= get_imsm_disk_idx(dev
, i
, MAP_X
);
6386 disk
= get_imsm_disk(super
, idx
);
6388 disk
= get_imsm_missing(super
, idx
);
6389 serialcpy(inf
[i
].serial
, disk
->serial
);
6391 append_metadata_update(st
, u
, len
);
6396 static int mgmt_disk(struct supertype
*st
)
6398 struct intel_super
*super
= st
->sb
;
6400 struct imsm_update_add_remove_disk
*u
;
6402 if (!super
->disk_mgmt_list
)
6407 u
->type
= update_add_remove_disk
;
6408 append_metadata_update(st
, u
, len
);
6413 __u32
crc32c_le(__u32 crc
, unsigned char const *p
, size_t len
);
6415 static int write_ppl_header(unsigned long long ppl_sector
, int fd
, void *buf
)
6417 struct ppl_header
*ppl_hdr
= buf
;
6420 ppl_hdr
->checksum
= __cpu_to_le32(~crc32c_le(~0, buf
, PPL_HEADER_SIZE
));
6422 if (lseek64(fd
, ppl_sector
* 512, SEEK_SET
) < 0) {
6424 perror("Failed to seek to PPL header location");
6428 if (write(fd
, buf
, PPL_HEADER_SIZE
) != PPL_HEADER_SIZE
) {
6430 perror("Write PPL header failed");
6439 static int write_init_ppl_imsm(struct supertype
*st
, struct mdinfo
*info
, int fd
)
6441 struct intel_super
*super
= st
->sb
;
6443 struct ppl_header
*ppl_hdr
;
6446 /* first clear entire ppl space */
6447 ret
= zero_disk_range(fd
, info
->ppl_sector
, info
->ppl_size
);
6451 ret
= posix_memalign(&buf
, MAX_SECTOR_SIZE
, PPL_HEADER_SIZE
);
6453 pr_err("Failed to allocate PPL header buffer\n");
6457 memset(buf
, 0, PPL_HEADER_SIZE
);
6459 memset(ppl_hdr
->reserved
, 0xff, PPL_HDR_RESERVED
);
6460 ppl_hdr
->signature
= __cpu_to_le32(super
->anchor
->orig_family_num
);
6462 if (info
->mismatch_cnt
) {
6464 * We are overwriting an invalid ppl. Make one entry with wrong
6465 * checksum to prevent the kernel from skipping resync.
6467 ppl_hdr
->entries_count
= __cpu_to_le32(1);
6468 ppl_hdr
->entries
[0].checksum
= ~0;
6471 ret
= write_ppl_header(info
->ppl_sector
, fd
, buf
);
6477 static int is_rebuilding(struct imsm_dev
*dev
);
6479 static int validate_ppl_imsm(struct supertype
*st
, struct mdinfo
*info
,
6480 struct mdinfo
*disk
)
6482 struct intel_super
*super
= st
->sb
;
6484 void *buf_orig
, *buf
, *buf_prev
= NULL
;
6486 struct ppl_header
*ppl_hdr
= NULL
;
6488 struct imsm_dev
*dev
;
6491 unsigned long long ppl_offset
= 0;
6492 unsigned long long prev_gen_num
= 0;
6494 if (disk
->disk
.raid_disk
< 0)
6497 dev
= get_imsm_dev(super
, info
->container_member
);
6498 idx
= get_imsm_disk_idx(dev
, disk
->disk
.raid_disk
, MAP_0
);
6499 d
= get_imsm_dl_disk(super
, idx
);
6501 if (!d
|| d
->index
< 0 || is_failed(&d
->disk
))
6504 if (posix_memalign(&buf_orig
, MAX_SECTOR_SIZE
, PPL_HEADER_SIZE
* 2)) {
6505 pr_err("Failed to allocate PPL header buffer\n");
6511 while (ppl_offset
< MULTIPLE_PPL_AREA_SIZE_IMSM
) {
6514 dprintf("Checking potential PPL at offset: %llu\n", ppl_offset
);
6516 if (lseek64(d
->fd
, info
->ppl_sector
* 512 + ppl_offset
,
6518 perror("Failed to seek to PPL header location");
6523 if (read(d
->fd
, buf
, PPL_HEADER_SIZE
) != PPL_HEADER_SIZE
) {
6524 perror("Read PPL header failed");
6531 crc
= __le32_to_cpu(ppl_hdr
->checksum
);
6532 ppl_hdr
->checksum
= 0;
6534 if (crc
!= ~crc32c_le(~0, buf
, PPL_HEADER_SIZE
)) {
6535 dprintf("Wrong PPL header checksum on %s\n",
6540 if (prev_gen_num
> __le64_to_cpu(ppl_hdr
->generation
)) {
6541 /* previous was newest, it was already checked */
6545 if ((__le32_to_cpu(ppl_hdr
->signature
) !=
6546 super
->anchor
->orig_family_num
)) {
6547 dprintf("Wrong PPL header signature on %s\n",
6554 prev_gen_num
= __le64_to_cpu(ppl_hdr
->generation
);
6556 ppl_offset
+= PPL_HEADER_SIZE
;
6557 for (i
= 0; i
< __le32_to_cpu(ppl_hdr
->entries_count
); i
++)
6559 __le32_to_cpu(ppl_hdr
->entries
[i
].pp_size
);
6562 buf_prev
= buf
+ PPL_HEADER_SIZE
;
6574 * Update metadata to use mutliple PPLs area (1MB).
6575 * This is done once for all RAID members
6577 if (info
->consistency_policy
== CONSISTENCY_POLICY_PPL
&&
6578 info
->ppl_size
!= (MULTIPLE_PPL_AREA_SIZE_IMSM
>> 9)) {
6580 struct mdinfo
*member_dev
;
6582 sprintf(subarray
, "%d", info
->container_member
);
6584 if (mdmon_running(st
->container_devnm
))
6585 st
->update_tail
= &st
->updates
;
6587 if (st
->ss
->update_subarray(st
, subarray
, UOPT_PPL
, NULL
)) {
6588 pr_err("Failed to update subarray %s\n",
6591 if (st
->update_tail
)
6592 flush_metadata_updates(st
);
6594 st
->ss
->sync_metadata(st
);
6595 info
->ppl_size
= (MULTIPLE_PPL_AREA_SIZE_IMSM
>> 9);
6596 for (member_dev
= info
->devs
; member_dev
;
6597 member_dev
= member_dev
->next
)
6598 member_dev
->ppl_size
=
6599 (MULTIPLE_PPL_AREA_SIZE_IMSM
>> 9);
6604 struct imsm_map
*map
= get_imsm_map(dev
, MAP_X
);
6606 if (map
->map_state
== IMSM_T_STATE_UNINITIALIZED
||
6607 (map
->map_state
== IMSM_T_STATE_NORMAL
&&
6608 !(dev
->vol
.dirty
& RAIDVOL_DIRTY
)) ||
6609 (is_rebuilding(dev
) &&
6610 vol_curr_migr_unit(dev
) == 0 &&
6611 get_imsm_disk_idx(dev
, disk
->disk
.raid_disk
, MAP_1
) != idx
))
6612 ret
= st
->ss
->write_init_ppl(st
, info
, d
->fd
);
6614 info
->mismatch_cnt
++;
6615 } else if (ret
== 0 &&
6616 ppl_hdr
->entries_count
== 0 &&
6617 is_rebuilding(dev
) &&
6618 info
->resync_start
== 0) {
6620 * The header has no entries - add a single empty entry and
6621 * rewrite the header to prevent the kernel from going into
6622 * resync after an interrupted rebuild.
6624 ppl_hdr
->entries_count
= __cpu_to_le32(1);
6625 ret
= write_ppl_header(info
->ppl_sector
, d
->fd
, buf
);
6633 static int write_init_ppl_imsm_all(struct supertype
*st
, struct mdinfo
*info
)
6635 struct intel_super
*super
= st
->sb
;
6639 if (info
->consistency_policy
!= CONSISTENCY_POLICY_PPL
||
6640 info
->array
.level
!= 5)
6643 for (d
= super
->disks
; d
; d
= d
->next
) {
6644 if (d
->index
< 0 || is_failed(&d
->disk
))
6647 ret
= st
->ss
->write_init_ppl(st
, info
, d
->fd
);
6655 /*******************************************************************************
6656 * Function: write_init_bitmap_imsm_vol
6657 * Description: Write a bitmap header and prepares the area for the bitmap.
6659 * st : supertype information
6660 * vol_idx : the volume index to use
6665 ******************************************************************************/
6666 static int write_init_bitmap_imsm_vol(struct supertype
*st
, int vol_idx
)
6668 struct intel_super
*super
= st
->sb
;
6669 int prev_current_vol
= super
->current_vol
;
6673 super
->current_vol
= vol_idx
;
6674 for (d
= super
->disks
; d
; d
= d
->next
) {
6675 if (d
->index
< 0 || is_failed(&d
->disk
))
6677 ret
= st
->ss
->write_bitmap(st
, d
->fd
, NoUpdate
);
6681 super
->current_vol
= prev_current_vol
;
6685 /*******************************************************************************
6686 * Function: write_init_bitmap_imsm_all
6687 * Description: Write a bitmap header and prepares the area for the bitmap.
6688 * Operation is executed for volumes with CONSISTENCY_POLICY_BITMAP.
6690 * st : supertype information
6691 * info : info about the volume where the bitmap should be written
6692 * vol_idx : the volume index to use
6697 ******************************************************************************/
6698 static int write_init_bitmap_imsm_all(struct supertype
*st
, struct mdinfo
*info
,
6703 if (info
&& (info
->consistency_policy
== CONSISTENCY_POLICY_BITMAP
))
6704 ret
= write_init_bitmap_imsm_vol(st
, vol_idx
);
6709 static int write_init_super_imsm(struct supertype
*st
)
6711 struct intel_super
*super
= st
->sb
;
6712 int current_vol
= super
->current_vol
;
6716 getinfo_super_imsm(st
, &info
, NULL
);
6718 /* we are done with current_vol reset it to point st at the container */
6719 super
->current_vol
= -1;
6721 if (st
->update_tail
) {
6722 /* queue the recently created array / added disk
6723 * as a metadata update */
6725 /* determine if we are creating a volume or adding a disk */
6726 if (current_vol
< 0) {
6727 /* in the mgmt (add/remove) disk case we are running
6728 * in mdmon context, so don't close fd's
6732 /* adding the second volume to the array */
6733 rv
= write_init_ppl_imsm_all(st
, &info
);
6735 rv
= write_init_bitmap_imsm_all(st
, &info
, current_vol
);
6737 rv
= create_array(st
, current_vol
);
6741 for (d
= super
->disks
; d
; d
= d
->next
)
6742 Kill(d
->devname
, NULL
, 0, -1, 1);
6743 if (current_vol
>= 0) {
6744 rv
= write_init_ppl_imsm_all(st
, &info
);
6746 rv
= write_init_bitmap_imsm_all(st
, &info
, current_vol
);
6750 rv
= write_super_imsm(st
, 1);
6756 static int store_super_imsm(struct supertype
*st
, int fd
)
6758 struct intel_super
*super
= st
->sb
;
6759 struct imsm_super
*mpb
= super
? super
->anchor
: NULL
;
6764 if (super
->sector_size
== 4096)
6765 convert_to_4k(super
);
6766 return store_imsm_mpb(fd
, mpb
);
6769 static int validate_geometry_imsm_container(struct supertype
*st
, int level
,
6771 unsigned long long data_offset
,
6773 unsigned long long *freesize
,
6777 unsigned long long ldsize
;
6778 struct intel_super
*super
= NULL
;
6781 if (!is_container(level
))
6786 fd
= dev_open(dev
, O_RDONLY
|O_EXCL
);
6787 if (!is_fd_valid(fd
)) {
6788 pr_vrb("imsm: Cannot open %s: %s\n", dev
, strerror(errno
));
6791 if (!get_dev_size(fd
, dev
, &ldsize
))
6794 /* capabilities retrieve could be possible
6795 * note that there is no fd for the disks in array.
6797 super
= alloc_super();
6801 if (!get_dev_sector_size(fd
, NULL
, &super
->sector_size
))
6804 rv
= find_intel_hba_capability(fd
, super
, verbose
> 0 ? dev
: NULL
);
6808 fd2devname(fd
, str
);
6809 dprintf("fd: %d %s orom: %p rv: %d raiddisk: %d\n",
6810 fd
, str
, super
->orom
, rv
, raiddisks
);
6812 /* no orom/efi or non-intel hba of the disk */
6817 if (raiddisks
> super
->orom
->tds
) {
6819 pr_err("%d exceeds maximum number of platform supported disks: %d\n",
6820 raiddisks
, super
->orom
->tds
);
6823 if ((super
->orom
->attr
& IMSM_OROM_ATTR_2TB_DISK
) == 0 &&
6824 (ldsize
>> 9) >> 32 > 0) {
6826 pr_err("%s exceeds maximum platform supported size\n", dev
);
6830 if (super
->hba
->type
== SYS_DEV_VMD
||
6831 super
->hba
->type
== SYS_DEV_NVME
) {
6832 if (!imsm_is_nvme_namespace_supported(fd
, 1)) {
6834 pr_err("NVMe namespace %s is not supported by IMSM\n",
6841 *freesize
= avail_size_imsm(st
, ldsize
>> 9, data_offset
);
6851 static unsigned long long find_size(struct extent
*e
, int *idx
, int num_extents
)
6853 const unsigned long long base_start
= e
[*idx
].start
;
6854 unsigned long long end
= base_start
+ e
[*idx
].size
;
6857 if (base_start
== end
)
6861 for (i
= *idx
; i
< num_extents
; i
++) {
6862 /* extend overlapping extents */
6863 if (e
[i
].start
>= base_start
&&
6864 e
[i
].start
<= end
) {
6867 if (e
[i
].start
+ e
[i
].size
> end
)
6868 end
= e
[i
].start
+ e
[i
].size
;
6869 } else if (e
[i
].start
> end
) {
6875 return end
- base_start
;
6878 /** merge_extents() - analyze extents and get free size.
6879 * @super: Intel metadata, not NULL.
6880 * @expanding: if set, we are expanding &super->current_vol.
6882 * Build a composite disk with all known extents and generate a size given the
6883 * "all disks in an array must share a common start offset" constraint.
6884 * If a volume is expanded, then return free space after the volume.
6886 * Return: Free space or 0 on failure.
6888 static unsigned long long merge_extents(struct intel_super
*super
, const bool expanding
)
6892 int i
, j
, pos_vol_idx
= -1;
6894 int sum_extents
= 0;
6895 unsigned long long pos
= 0;
6896 unsigned long long start
= 0;
6897 unsigned long long free_size
= 0;
6899 unsigned long pre_reservation
= 0;
6900 unsigned long post_reservation
= IMSM_RESERVED_SECTORS
;
6901 unsigned long reservation_size
;
6903 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
6905 sum_extents
+= dl
->extent_cnt
;
6906 e
= xcalloc(sum_extents
, sizeof(struct extent
));
6908 /* coalesce and sort all extents. also, check to see if we need to
6909 * reserve space between member arrays
6912 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
6915 for (i
= 0; i
< dl
->extent_cnt
; i
++)
6918 qsort(e
, sum_extents
, sizeof(*e
), cmp_extent
);
6923 while (i
< sum_extents
) {
6924 e
[j
].start
= e
[i
].start
;
6925 e
[j
].vol
= e
[i
].vol
;
6926 e
[j
].size
= find_size(e
, &i
, sum_extents
);
6928 if (e
[j
-1].size
== 0)
6934 unsigned long long esize
= e
[i
].start
- pos
;
6936 if (expanding
? pos_vol_idx
== super
->current_vol
: esize
>= free_size
) {
6942 pos
= e
[i
].start
+ e
[i
].size
;
6943 pos_vol_idx
= e
[i
].vol
;
6946 } while (e
[i
-1].size
);
6948 if (free_size
== 0) {
6949 dprintf("imsm: Cannot find free size.\n");
6954 if (!expanding
&& extent_idx
!= 0)
6956 * Not a real first volume in a container is created, pre_reservation is needed.
6958 pre_reservation
= IMSM_RESERVED_SECTORS
;
6960 if (e
[extent_idx
].size
== 0)
6962 * extent_idx points to the metadata, post_reservation is allready done.
6964 post_reservation
= 0;
6967 reservation_size
= pre_reservation
+ post_reservation
;
6969 if (free_size
< reservation_size
) {
6970 dprintf("imsm: Reservation size is greater than free space.\n");
6974 super
->create_offset
= start
+ pre_reservation
;
6975 return free_size
- reservation_size
;
6978 static int is_raid_level_supported(const struct imsm_orom
*orom
, int level
, int raiddisks
)
6980 if (level
< 0 || level
== 6 || level
== 4)
6983 /* if we have an orom prevent invalid raid levels */
6986 case 0: return imsm_orom_has_raid0(orom
);
6989 return imsm_orom_has_raid1e(orom
);
6990 return imsm_orom_has_raid1(orom
) && raiddisks
== 2;
6991 case 10: return imsm_orom_has_raid10(orom
) && raiddisks
== 4;
6992 case 5: return imsm_orom_has_raid5(orom
) && raiddisks
> 2;
6995 return 1; /* not on an Intel RAID platform so anything goes */
7001 active_arrays_by_format(char *name
, char* hba
, struct md_list
**devlist
,
7002 int dpa
, int verbose
)
7004 struct mdstat_ent
*mdstat
= mdstat_read(0, 0);
7005 struct mdstat_ent
*memb
;
7011 for (memb
= mdstat
; memb
; memb
= memb
->next
) {
7012 if (memb
->metadata_version
&&
7013 (strncmp(memb
->metadata_version
, "external:", 9) == 0) &&
7014 (strcmp(&memb
->metadata_version
[9], name
) == 0) &&
7015 !is_subarray(memb
->metadata_version
+9) &&
7017 struct dev_member
*dev
= memb
->members
;
7019 while (dev
&& !is_fd_valid(fd
)) {
7020 char *path
= xmalloc(strlen(dev
->name
) + strlen("/dev/") + 1);
7021 num
= snprintf(path
, PATH_MAX
, "%s%s", "/dev/", dev
->name
);
7023 fd
= open(path
, O_RDONLY
, 0);
7024 if (num
<= 0 || !is_fd_valid(fd
)) {
7025 pr_vrb("Cannot open %s: %s\n",
7026 dev
->name
, strerror(errno
));
7032 if (is_fd_valid(fd
) && disk_attached_to_hba(fd
, hba
)) {
7033 struct mdstat_ent
*vol
;
7034 for (vol
= mdstat
; vol
; vol
= vol
->next
) {
7035 if (vol
->active
> 0 &&
7036 vol
->metadata_version
&&
7037 is_container_member(vol
, memb
->devnm
)) {
7042 if (*devlist
&& (found
< dpa
)) {
7043 dv
= xcalloc(1, sizeof(*dv
));
7044 dv
->devname
= xmalloc(strlen(memb
->devnm
) + strlen("/dev/") + 1);
7045 sprintf(dv
->devname
, "%s%s", "/dev/", memb
->devnm
);
7048 dv
->next
= *devlist
;
7055 free_mdstat(mdstat
);
7060 static struct md_list
*
7061 get_loop_devices(void)
7064 struct md_list
*devlist
= NULL
;
7067 for(i
= 0; i
< 12; i
++) {
7068 dv
= xcalloc(1, sizeof(*dv
));
7069 dv
->devname
= xmalloc(40);
7070 sprintf(dv
->devname
, "/dev/loop%d", i
);
7078 static struct md_list
*
7079 get_devices(const char *hba_path
)
7081 struct md_list
*devlist
= NULL
;
7088 devlist
= get_loop_devices();
7091 /* scroll through /sys/dev/block looking for devices attached to
7094 dir
= opendir("/sys/dev/block");
7095 for (ent
= dir
? readdir(dir
) : NULL
; ent
; ent
= readdir(dir
)) {
7100 if (sscanf(ent
->d_name
, "%d:%d", &major
, &minor
) != 2)
7102 path
= devt_to_devpath(makedev(major
, minor
), 1, NULL
);
7105 if (!path_attached_to_hba(path
, hba_path
)) {
7112 fd
= dev_open(ent
->d_name
, O_RDONLY
);
7113 if (is_fd_valid(fd
)) {
7114 fd2devname(fd
, buf
);
7117 pr_err("cannot open device: %s\n",
7122 dv
= xcalloc(1, sizeof(*dv
));
7123 dv
->devname
= xstrdup(buf
);
7130 devlist
= devlist
->next
;
7140 count_volumes_list(struct md_list
*devlist
, char *homehost
,
7141 int verbose
, int *found
)
7143 struct md_list
*tmpdev
;
7145 struct supertype
*st
;
7147 /* first walk the list of devices to find a consistent set
7148 * that match the criterea, if that is possible.
7149 * We flag the ones we like with 'used'.
7152 st
= match_metadata_desc_imsm("imsm");
7154 pr_vrb("cannot allocate memory for imsm supertype\n");
7158 for (tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
7159 char *devname
= tmpdev
->devname
;
7161 struct supertype
*tst
;
7163 if (tmpdev
->used
> 1)
7165 tst
= dup_super(st
);
7167 pr_vrb("cannot allocate memory for imsm supertype\n");
7170 tmpdev
->container
= 0;
7171 dfd
= dev_open(devname
, O_RDONLY
|O_EXCL
);
7172 if (!is_fd_valid(dfd
)) {
7173 dprintf("cannot open device %s: %s\n",
7174 devname
, strerror(errno
));
7176 } else if (!fstat_is_blkdev(dfd
, devname
, &rdev
)) {
7178 } else if (must_be_container(dfd
)) {
7179 struct supertype
*cst
;
7180 cst
= super_by_fd(dfd
, NULL
);
7182 dprintf("cannot recognize container type %s\n",
7185 } else if (tst
->ss
!= st
->ss
) {
7186 dprintf("non-imsm container - ignore it: %s\n",
7189 } else if (!tst
->ss
->load_container
||
7190 tst
->ss
->load_container(tst
, dfd
, NULL
))
7193 tmpdev
->container
= 1;
7196 cst
->ss
->free_super(cst
);
7198 tmpdev
->st_rdev
= rdev
;
7199 if (tst
->ss
->load_super(tst
,dfd
, NULL
)) {
7200 dprintf("no RAID superblock on %s\n",
7203 } else if (tst
->ss
->compare_super
== NULL
) {
7204 dprintf("Cannot assemble %s metadata on %s\n",
7205 tst
->ss
->name
, devname
);
7211 if (tmpdev
->used
== 2 || tmpdev
->used
== 4) {
7212 /* Ignore unrecognised devices during auto-assembly */
7217 tst
->ss
->getinfo_super(tst
, &info
, NULL
);
7219 if (st
->minor_version
== -1)
7220 st
->minor_version
= tst
->minor_version
;
7222 if (memcmp(info
.uuid
, uuid_zero
,
7223 sizeof(int[4])) == 0) {
7224 /* this is a floating spare. It cannot define
7225 * an array unless there are no more arrays of
7226 * this type to be found. It can be included
7227 * in an array of this type though.
7233 if (st
->ss
!= tst
->ss
||
7234 st
->minor_version
!= tst
->minor_version
||
7235 st
->ss
->compare_super(st
, tst
, 1) != 0) {
7236 /* Some mismatch. If exactly one array matches this host,
7237 * we can resolve on that one.
7238 * Or, if we are auto assembling, we just ignore the second
7241 dprintf("superblock on %s doesn't match others - assembly aborted\n",
7247 dprintf("found: devname: %s\n", devname
);
7251 tst
->ss
->free_super(tst
);
7255 if ((err
= load_super_imsm_all(st
, -1, &st
->sb
, NULL
, devlist
, 0)) == 0) {
7256 struct mdinfo
*iter
, *head
= st
->ss
->container_content(st
, NULL
);
7257 for (iter
= head
; iter
; iter
= iter
->next
) {
7258 dprintf("content->text_version: %s vol\n",
7259 iter
->text_version
);
7260 if (iter
->array
.state
& (1<<MD_SB_BLOCK_VOLUME
)) {
7261 /* do not assemble arrays with unsupported
7263 dprintf("Cannot activate member %s.\n",
7264 iter
->text_version
);
7271 dprintf("No valid super block on device list: err: %d %p\n",
7275 dprintf("no more devices to examine\n");
7278 for (tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
7279 if (tmpdev
->used
== 1 && tmpdev
->found
) {
7281 if (count
< tmpdev
->found
)
7284 count
-= tmpdev
->found
;
7287 if (tmpdev
->used
== 1)
7292 st
->ss
->free_super(st
);
7296 static int __count_volumes(char *hba_path
, int dpa
, int verbose
,
7299 struct sys_dev
*idev
, *intel_devices
= find_intel_devices();
7301 const struct orom_entry
*entry
;
7302 struct devid_list
*dv
, *devid_list
;
7307 for (idev
= intel_devices
; idev
; idev
= idev
->next
) {
7308 if (strstr(idev
->path
, hba_path
))
7312 if (!idev
|| !idev
->dev_id
)
7315 entry
= get_orom_entry_by_device_id(idev
->dev_id
);
7317 if (!entry
|| !entry
->devid_list
)
7320 devid_list
= entry
->devid_list
;
7321 for (dv
= devid_list
; dv
; dv
= dv
->next
) {
7322 struct md_list
*devlist
;
7323 struct sys_dev
*device
= NULL
;
7328 device
= device_by_id_and_path(dv
->devid
, hba_path
);
7330 device
= device_by_id(dv
->devid
);
7333 hpath
= device
->path
;
7337 devlist
= get_devices(hpath
);
7338 /* if no intel devices return zero volumes */
7339 if (devlist
== NULL
)
7342 count
+= active_arrays_by_format("imsm", hpath
, &devlist
, dpa
,
7344 dprintf("path: %s active arrays: %d\n", hpath
, count
);
7345 if (devlist
== NULL
)
7349 count
+= count_volumes_list(devlist
,
7353 dprintf("found %d count: %d\n", found
, count
);
7356 dprintf("path: %s total number of volumes: %d\n", hpath
, count
);
7359 struct md_list
*dv
= devlist
;
7360 devlist
= devlist
->next
;
7368 static int count_volumes(struct intel_hba
*hba
, int dpa
, int verbose
)
7372 if (hba
->type
== SYS_DEV_VMD
) {
7373 struct sys_dev
*dev
;
7376 for (dev
= find_intel_devices(); dev
; dev
= dev
->next
) {
7377 if (dev
->type
== SYS_DEV_VMD
)
7378 count
+= __count_volumes(dev
->path
, dpa
,
7383 return __count_volumes(hba
->path
, dpa
, verbose
, 0);
7386 static int imsm_default_chunk(const struct imsm_orom
*orom
)
7388 /* up to 512 if the plaform supports it, otherwise the platform max.
7389 * 128 if no platform detected
7391 int fs
= max(7, orom
? fls(orom
->sss
) : 0);
7393 return min(512, (1 << fs
));
7397 validate_geometry_imsm_orom(struct intel_super
*super
, int level
, int layout
,
7398 int raiddisks
, int *chunk
, unsigned long long size
, int verbose
)
7400 /* check/set platform and metadata limits/defaults */
7401 if (super
->orom
&& raiddisks
> super
->orom
->dpa
) {
7402 pr_vrb("platform supports a maximum of %d disks per array\n",
7407 /* capabilities of OROM tested - copied from validate_geometry_imsm_volume */
7408 if (!is_raid_level_supported(super
->orom
, level
, raiddisks
)) {
7409 pr_vrb("platform does not support raid%d with %d disk%s\n",
7410 level
, raiddisks
, raiddisks
> 1 ? "s" : "");
7414 if (*chunk
== 0 || *chunk
== UnSet
)
7415 *chunk
= imsm_default_chunk(super
->orom
);
7417 if (super
->orom
&& !imsm_orom_has_chunk(super
->orom
, *chunk
)) {
7418 pr_vrb("platform does not support a chunk size of: %d\n", *chunk
);
7422 if (layout
!= imsm_level_to_layout(level
)) {
7424 pr_vrb("imsm raid 5 only supports the left-asymmetric layout\n");
7425 else if (level
== 10)
7426 pr_vrb("imsm raid 10 only supports the n2 layout\n");
7428 pr_vrb("imsm unknown layout %#x for this raid level %d\n",
7433 if (super
->orom
&& (super
->orom
->attr
& IMSM_OROM_ATTR_2TB
) == 0 &&
7434 (calc_array_size(level
, raiddisks
, layout
, *chunk
, size
) >> 32) > 0) {
7435 pr_vrb("platform does not support a volume size over 2TB\n");
7442 /* validate_geometry_imsm_volume - lifted from validate_geometry_ddf_bvd
7443 * FIX ME add ahci details
7445 static int validate_geometry_imsm_volume(struct supertype
*st
, int level
,
7446 int layout
, int raiddisks
, int *chunk
,
7447 unsigned long long size
,
7448 unsigned long long data_offset
,
7450 unsigned long long *freesize
,
7454 struct intel_super
*super
= st
->sb
;
7455 struct imsm_super
*mpb
;
7457 unsigned long long pos
= 0;
7458 unsigned long long maxsize
;
7462 /* We must have the container info already read in. */
7466 mpb
= super
->anchor
;
7468 if (!validate_geometry_imsm_orom(super
, level
, layout
, raiddisks
, chunk
, size
, verbose
)) {
7469 pr_err("RAID geometry validation failed. Cannot proceed with the action(s).\n");
7473 /* General test: make sure there is space for
7474 * 'raiddisks' device extents of size 'size' at a given
7477 unsigned long long minsize
= size
;
7478 unsigned long long start_offset
= MaxSector
;
7481 minsize
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
7482 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
7487 e
= get_extents(super
, dl
, 0);
7490 unsigned long long esize
;
7491 esize
= e
[i
].start
- pos
;
7492 if (esize
>= minsize
)
7494 if (found
&& start_offset
== MaxSector
) {
7497 } else if (found
&& pos
!= start_offset
) {
7501 pos
= e
[i
].start
+ e
[i
].size
;
7503 } while (e
[i
-1].size
);
7508 if (dcnt
< raiddisks
) {
7510 pr_err("imsm: Not enough devices with space for this array (%d < %d)\n",
7517 /* This device must be a member of the set */
7518 if (!stat_is_blkdev(dev
, &rdev
))
7520 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
7521 if (dl
->major
== (int)major(rdev
) &&
7522 dl
->minor
== (int)minor(rdev
))
7527 pr_err("%s is not in the same imsm set\n", dev
);
7529 } else if (super
->orom
&& dl
->index
< 0 && mpb
->num_raid_devs
) {
7530 /* If a volume is present then the current creation attempt
7531 * cannot incorporate new spares because the orom may not
7532 * understand this configuration (all member disks must be
7533 * members of each array in the container).
7535 pr_err("%s is a spare and a volume is already defined for this container\n", dev
);
7536 pr_err("The option-rom requires all member disks to be a member of all volumes\n");
7538 } else if (super
->orom
&& mpb
->num_raid_devs
> 0 &&
7539 mpb
->num_disks
!= raiddisks
) {
7540 pr_err("The option-rom requires all member disks to be a member of all volumes\n");
7544 /* retrieve the largest free space block */
7545 e
= get_extents(super
, dl
, 0);
7550 unsigned long long esize
;
7552 esize
= e
[i
].start
- pos
;
7553 if (esize
>= maxsize
)
7555 pos
= e
[i
].start
+ e
[i
].size
;
7557 } while (e
[i
-1].size
);
7562 pr_err("unable to determine free space for: %s\n",
7566 if (maxsize
< size
) {
7568 pr_err("%s not enough space (%llu < %llu)\n",
7569 dev
, maxsize
, size
);
7573 maxsize
= merge_extents(super
, false);
7575 if (mpb
->num_raid_devs
> 0 && size
&& size
!= maxsize
)
7576 pr_err("attempting to create a second volume with size less then remaining space.\n");
7578 if (maxsize
< size
|| maxsize
== 0) {
7581 pr_err("no free space left on device. Aborting...\n");
7583 pr_err("not enough space to create volume of given size (%llu < %llu). Aborting...\n",
7589 *freesize
= maxsize
;
7592 int count
= count_volumes(super
->hba
,
7593 super
->orom
->dpa
, verbose
);
7594 if (super
->orom
->vphba
<= count
) {
7595 pr_vrb("platform does not support more than %d raid volumes.\n",
7596 super
->orom
->vphba
);
7604 * imsm_get_free_size() - get the biggest, common free space from members.
7605 * @super: &intel_super pointer, not NULL.
7606 * @raiddisks: number of raid disks.
7607 * @size: requested size, could be 0 (means max size).
7608 * @chunk: requested chunk size in KiB.
7609 * @freesize: pointer for returned size value.
7611 * Return: &IMSM_STATUS_OK or &IMSM_STATUS_ERROR.
7613 * @freesize is set to meaningful value, this can be @size, or calculated
7615 * super->create_offset value is modified and set appropriately in
7616 * merge_extends() for further creation.
7618 static imsm_status_t
imsm_get_free_size(struct intel_super
*super
,
7619 const int raiddisks
,
7620 unsigned long long size
,
7622 unsigned long long *freesize
,
7625 struct imsm_super
*mpb
= super
->anchor
;
7631 unsigned long long maxsize
;
7632 unsigned long long minsize
= size
;
7635 minsize
= chunk
* 2;
7637 /* find the largest common start free region of the possible disks */
7638 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
7644 /* don't activate new spares if we are orom constrained
7645 * and there is already a volume active in the container
7647 if (super
->orom
&& dl
->index
< 0 && mpb
->num_raid_devs
)
7650 e
= get_extents(super
, dl
, 0);
7653 for (i
= 1; e
[i
-1].size
; i
++)
7660 maxsize
= merge_extents(super
, expanding
);
7661 if (maxsize
< minsize
) {
7662 pr_err("imsm: Free space is %llu but must be equal or larger than %llu.\n",
7664 return IMSM_STATUS_ERROR
;
7667 if (cnt
< raiddisks
|| (super
->orom
&& used
&& used
!= raiddisks
)) {
7668 pr_err("imsm: Not enough devices with space to create array.\n");
7669 return IMSM_STATUS_ERROR
;
7680 if (mpb
->num_raid_devs
> 0 && size
&& size
!= maxsize
)
7681 pr_err("attempting to create a second volume with size less then remaining space.\n");
7684 dprintf("imsm: imsm_get_free_size() returns : %llu\n", size
);
7686 return IMSM_STATUS_OK
;
7690 * autolayout_imsm() - automatically layout a new volume.
7691 * @super: &intel_super pointer, not NULL.
7692 * @raiddisks: number of raid disks.
7693 * @size: requested size, could be 0 (means max size).
7694 * @chunk: requested chunk.
7695 * @freesize: pointer for returned size value.
7697 * We are being asked to automatically layout a new volume based on the current
7698 * contents of the container. If the parameters can be satisfied autolayout_imsm
7699 * will record the disks, start offset, and will return size of the volume to
7700 * be created. See imsm_get_free_size() for details.
7701 * add_to_super() and getinfo_super() detect when autolayout is in progress.
7702 * If first volume exists, slots are set consistently to it.
7704 * Return: &IMSM_STATUS_OK on success, &IMSM_STATUS_ERROR otherwise.
7706 * Disks are marked for creation via dl->raiddisk.
7708 static imsm_status_t
autolayout_imsm(struct intel_super
*super
,
7709 const int raiddisks
,
7710 unsigned long long size
, const int chunk
,
7711 unsigned long long *freesize
)
7715 int vol_cnt
= super
->anchor
->num_raid_devs
;
7718 rv
= imsm_get_free_size(super
, raiddisks
, size
, chunk
, freesize
, false);
7719 if (rv
!= IMSM_STATUS_OK
)
7720 return IMSM_STATUS_ERROR
;
7722 for (disk
= super
->disks
; disk
; disk
= disk
->next
) {
7726 if (curr_slot
== raiddisks
)
7730 disk
->raiddisk
= curr_slot
;
7732 int _slot
= get_disk_slot_in_dev(super
, 0, disk
->index
);
7735 pr_err("Disk %s is not used in first volume, aborting\n",
7737 return IMSM_STATUS_ERROR
;
7739 disk
->raiddisk
= _slot
;
7744 return IMSM_STATUS_OK
;
7747 static int validate_geometry_imsm(struct supertype
*st
, int level
, int layout
,
7748 int raiddisks
, int *chunk
, unsigned long long size
,
7749 unsigned long long data_offset
,
7750 char *dev
, unsigned long long *freesize
,
7751 int consistency_policy
, int verbose
)
7758 * if given unused devices create a container
7759 * if given given devices in a container create a member volume
7761 if (is_container(level
))
7762 /* Must be a fresh device to add to a container */
7763 return validate_geometry_imsm_container(st
, level
, raiddisks
,
7768 * Size is given in sectors.
7770 if (size
&& (size
< 2048)) {
7771 pr_err("Given size must be greater than 1M.\n");
7772 /* Depends on algorithm in Create.c :
7773 * if container was given (dev == NULL) return -1,
7774 * if block device was given ( dev != NULL) return 0.
7776 return dev
? -1 : 0;
7780 struct intel_super
*super
= st
->sb
;
7783 * Autolayout mode, st->sb must be set.
7786 pr_vrb("superblock must be set for autolayout, aborting\n");
7790 if (!validate_geometry_imsm_orom(st
->sb
, level
, layout
,
7791 raiddisks
, chunk
, size
,
7795 if (super
->orom
&& freesize
) {
7797 int count
= count_volumes(super
->hba
, super
->orom
->dpa
,
7799 if (super
->orom
->vphba
<= count
) {
7800 pr_vrb("platform does not support more than %d raid volumes.\n",
7801 super
->orom
->vphba
);
7805 rv
= autolayout_imsm(super
, raiddisks
, size
, *chunk
,
7807 if (rv
!= IMSM_STATUS_OK
)
7813 /* creating in a given container */
7814 return validate_geometry_imsm_volume(st
, level
, layout
,
7815 raiddisks
, chunk
, size
,
7817 dev
, freesize
, verbose
);
7820 /* This device needs to be a device in an 'imsm' container */
7821 fd
= open(dev
, O_RDONLY
|O_EXCL
, 0);
7823 if (is_fd_valid(fd
)) {
7824 pr_vrb("Cannot create this array on device %s\n", dev
);
7829 fd
= open(dev
, O_RDONLY
, 0);
7831 if (!is_fd_valid(fd
)) {
7832 pr_vrb("Cannot open %s: %s\n", dev
, strerror(errno
));
7836 /* Well, it is in use by someone, maybe an 'imsm' container. */
7837 cfd
= open_container(fd
);
7840 if (!is_fd_valid(cfd
)) {
7841 pr_vrb("Cannot use %s: It is busy\n", dev
);
7844 sra
= sysfs_read(cfd
, NULL
, GET_VERSION
);
7845 if (sra
&& sra
->array
.major_version
== -1 &&
7846 strcmp(sra
->text_version
, "imsm") == 0)
7850 /* This is a member of a imsm container. Load the container
7851 * and try to create a volume
7853 struct intel_super
*super
;
7855 if (load_super_imsm_all(st
, cfd
, (void **) &super
, NULL
, NULL
, 1) == 0) {
7857 strcpy(st
->container_devnm
, fd2devnm(cfd
));
7859 return validate_geometry_imsm_volume(st
, level
, layout
,
7861 size
, data_offset
, dev
,
7868 pr_err("failed container membership check\n");
7874 static void default_geometry_imsm(struct supertype
*st
, int *level
, int *layout
, int *chunk
)
7876 struct intel_super
*super
= st
->sb
;
7878 if (level
&& *level
== UnSet
)
7879 *level
= LEVEL_CONTAINER
;
7881 if (level
&& layout
&& *layout
== UnSet
)
7882 *layout
= imsm_level_to_layout(*level
);
7884 if (chunk
&& (*chunk
== UnSet
|| *chunk
== 0))
7885 *chunk
= imsm_default_chunk(super
->orom
);
7888 static void handle_missing(struct intel_super
*super
, struct imsm_dev
*dev
);
7890 static int kill_subarray_imsm(struct supertype
*st
, char *subarray_id
)
7892 /* remove the subarray currently referenced by subarray_id */
7894 struct intel_dev
**dp
;
7895 struct intel_super
*super
= st
->sb
;
7896 __u8 current_vol
= strtoul(subarray_id
, NULL
, 10);
7897 struct imsm_super
*mpb
= super
->anchor
;
7899 if (mpb
->num_raid_devs
== 0)
7902 /* block deletions that would change the uuid of active subarrays
7904 * FIXME when immutable ids are available, but note that we'll
7905 * also need to fixup the invalidated/active subarray indexes in
7908 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
7911 if (i
< current_vol
)
7913 snprintf(subarray
, sizeof(subarray
), "%u", i
);
7914 if (is_subarray_active(subarray
, st
->devnm
)) {
7915 pr_err("deleting subarray-%d would change the UUID of active subarray-%d, aborting\n",
7922 if (st
->update_tail
) {
7923 struct imsm_update_kill_array
*u
= xmalloc(sizeof(*u
));
7925 u
->type
= update_kill_array
;
7926 u
->dev_idx
= current_vol
;
7927 append_metadata_update(st
, u
, sizeof(*u
));
7932 for (dp
= &super
->devlist
; *dp
;)
7933 if ((*dp
)->index
== current_vol
) {
7936 handle_missing(super
, (*dp
)->dev
);
7937 if ((*dp
)->index
> current_vol
)
7942 /* no more raid devices, all active components are now spares,
7943 * but of course failed are still failed
7945 if (--mpb
->num_raid_devs
== 0) {
7948 for (d
= super
->disks
; d
; d
= d
->next
)
7953 super
->updates_pending
++;
7959 * get_rwh_policy_from_update() - Get the rwh policy for update option.
7960 * @update: Update option.
7962 static int get_rwh_policy_from_update(enum update_opt update
)
7966 return RWH_MULTIPLE_DISTRIBUTED
;
7968 return RWH_MULTIPLE_OFF
;
7971 case UOPT_NO_BITMAP
:
7976 return UOPT_UNDEFINED
;
7979 static int update_subarray_imsm(struct supertype
*st
, char *subarray
,
7980 enum update_opt update
, struct mddev_ident
*ident
)
7982 /* update the subarray currently referenced by ->current_vol */
7983 struct intel_super
*super
= st
->sb
;
7984 struct imsm_super
*mpb
= super
->anchor
;
7986 if (update
== UOPT_NAME
) {
7987 char *name
= ident
->name
;
7991 if (imsm_is_name_allowed(super
, name
, 1) == false)
7994 vol
= strtoul(subarray
, &ep
, 10);
7995 if (*ep
!= '\0' || vol
>= super
->anchor
->num_raid_devs
)
7998 if (st
->update_tail
) {
7999 struct imsm_update_rename_array
*u
= xmalloc(sizeof(*u
));
8001 u
->type
= update_rename_array
;
8003 strncpy((char *) u
->name
, name
, MAX_RAID_SERIAL_LEN
);
8004 u
->name
[MAX_RAID_SERIAL_LEN
-1] = '\0';
8005 append_metadata_update(st
, u
, sizeof(*u
));
8007 struct imsm_dev
*dev
;
8010 dev
= get_imsm_dev(super
, vol
);
8011 memset(dev
->volume
, '\0', MAX_RAID_SERIAL_LEN
);
8012 namelen
= min((int)strlen(name
), MAX_RAID_SERIAL_LEN
);
8013 memcpy(dev
->volume
, name
, namelen
);
8014 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
8015 dev
= get_imsm_dev(super
, i
);
8016 handle_missing(super
, dev
);
8018 super
->updates_pending
++;
8020 } else if (get_rwh_policy_from_update(update
) != UOPT_UNDEFINED
) {
8023 int vol
= strtoul(subarray
, &ep
, 10);
8025 if (*ep
!= '\0' || vol
>= super
->anchor
->num_raid_devs
)
8028 new_policy
= get_rwh_policy_from_update(update
);
8030 if (st
->update_tail
) {
8031 struct imsm_update_rwh_policy
*u
= xmalloc(sizeof(*u
));
8033 u
->type
= update_rwh_policy
;
8035 u
->new_policy
= new_policy
;
8036 append_metadata_update(st
, u
, sizeof(*u
));
8038 struct imsm_dev
*dev
;
8040 dev
= get_imsm_dev(super
, vol
);
8041 dev
->rwh_policy
= new_policy
;
8042 super
->updates_pending
++;
8044 if (new_policy
== RWH_BITMAP
)
8045 return write_init_bitmap_imsm_vol(st
, vol
);
8052 static bool is_gen_migration(struct imsm_dev
*dev
)
8054 if (dev
&& dev
->vol
.migr_state
&&
8055 migr_type(dev
) == MIGR_GEN_MIGR
)
8061 static int is_rebuilding(struct imsm_dev
*dev
)
8063 struct imsm_map
*migr_map
;
8065 if (!dev
->vol
.migr_state
)
8068 if (migr_type(dev
) != MIGR_REBUILD
)
8071 migr_map
= get_imsm_map(dev
, MAP_1
);
8073 if (migr_map
->map_state
== IMSM_T_STATE_DEGRADED
)
8079 static int is_initializing(struct imsm_dev
*dev
)
8081 struct imsm_map
*migr_map
;
8083 if (!dev
->vol
.migr_state
)
8086 if (migr_type(dev
) != MIGR_INIT
)
8089 migr_map
= get_imsm_map(dev
, MAP_1
);
8091 if (migr_map
->map_state
== IMSM_T_STATE_UNINITIALIZED
)
8097 static void update_recovery_start(struct intel_super
*super
,
8098 struct imsm_dev
*dev
,
8099 struct mdinfo
*array
)
8101 struct mdinfo
*rebuild
= NULL
;
8105 if (!is_rebuilding(dev
))
8108 /* Find the rebuild target, but punt on the dual rebuild case */
8109 for (d
= array
->devs
; d
; d
= d
->next
)
8110 if (d
->recovery_start
== 0) {
8117 /* (?) none of the disks are marked with
8118 * IMSM_ORD_REBUILD, so assume they are missing and the
8119 * disk_ord_tbl was not correctly updated
8121 dprintf("failed to locate out-of-sync disk\n");
8125 units
= vol_curr_migr_unit(dev
);
8126 rebuild
->recovery_start
= units
* blocks_per_migr_unit(super
, dev
);
8129 static int recover_backup_imsm(struct supertype
*st
, struct mdinfo
*info
);
8131 static struct mdinfo
*container_content_imsm(struct supertype
*st
, char *subarray
)
8133 /* Given a container loaded by load_super_imsm_all,
8134 * extract information about all the arrays into
8136 * If 'subarray' is given, just extract info about that array.
8138 * For each imsm_dev create an mdinfo, fill it in,
8139 * then look for matching devices in super->disks
8140 * and create appropriate device mdinfo.
8142 struct intel_super
*super
= st
->sb
;
8143 struct imsm_super
*mpb
= super
->anchor
;
8144 struct mdinfo
*rest
= NULL
;
8148 int spare_disks
= 0;
8149 int current_vol
= super
->current_vol
;
8151 /* do not assemble arrays when not all attributes are supported */
8152 if (imsm_check_attributes(mpb
->attributes
) == 0) {
8154 pr_err("Unsupported attributes in IMSM metadata.Arrays activation is blocked.\n");
8157 /* count spare devices, not used in maps
8159 for (d
= super
->disks
; d
; d
= d
->next
)
8163 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
8164 struct imsm_dev
*dev
;
8165 struct imsm_map
*map
;
8166 struct imsm_map
*map2
;
8167 struct mdinfo
*this;
8174 (i
!= strtoul(subarray
, &ep
, 10) || *ep
!= '\0'))
8177 dev
= get_imsm_dev(super
, i
);
8178 map
= get_imsm_map(dev
, MAP_0
);
8179 map2
= get_imsm_map(dev
, MAP_1
);
8180 level
= get_imsm_raid_level(map
);
8182 /* do not publish arrays that are in the middle of an
8183 * unsupported migration
8185 if (dev
->vol
.migr_state
&&
8186 (migr_type(dev
) == MIGR_STATE_CHANGE
)) {
8187 pr_err("cannot assemble volume '%.16s': unsupported migration in progress\n",
8191 /* do not publish arrays that are not support by controller's
8195 this = xmalloc(sizeof(*this));
8197 super
->current_vol
= i
;
8198 getinfo_super_imsm_volume(st
, this, NULL
);
8200 chunk
= __le16_to_cpu(map
->blocks_per_strip
) >> 1;
8201 /* mdadm does not support all metadata features- set the bit in all arrays state */
8202 if (!validate_geometry_imsm_orom(super
,
8203 level
, /* RAID level */
8204 imsm_level_to_layout(level
),
8205 map
->num_members
, /* raid disks */
8206 &chunk
, imsm_dev_size(dev
),
8208 pr_err("IMSM RAID geometry validation failed. Array %s activation is blocked.\n",
8210 this->array
.state
|=
8211 (1<<MD_SB_BLOCK_CONTAINER_RESHAPE
) |
8212 (1<<MD_SB_BLOCK_VOLUME
);
8215 /* if array has bad blocks, set suitable bit in all arrays state */
8217 this->array
.state
|=
8218 (1<<MD_SB_BLOCK_CONTAINER_RESHAPE
) |
8219 (1<<MD_SB_BLOCK_VOLUME
);
8221 for (slot
= 0 ; slot
< map
->num_members
; slot
++) {
8222 unsigned long long recovery_start
;
8223 struct mdinfo
*info_d
;
8231 idx
= get_imsm_disk_idx(dev
, slot
, MAP_0
);
8232 ord
= get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
);
8233 for (d
= super
->disks
; d
; d
= d
->next
)
8234 if (d
->index
== idx
)
8237 recovery_start
= MaxSector
;
8240 if (d
&& is_failed(&d
->disk
))
8242 if (!skip
&& (ord
& IMSM_ORD_REBUILD
))
8244 if (!(ord
& IMSM_ORD_REBUILD
))
8245 this->array
.working_disks
++;
8247 * if we skip some disks the array will be assmebled degraded;
8248 * reset resync start to avoid a dirty-degraded
8249 * situation when performing the intial sync
8254 if (!(dev
->vol
.dirty
& RAIDVOL_DIRTY
)) {
8255 if ((!able_to_resync(level
, missing
) ||
8256 recovery_start
== 0))
8257 this->resync_start
= MaxSector
;
8263 info_d
= xcalloc(1, sizeof(*info_d
));
8264 info_d
->next
= this->devs
;
8265 this->devs
= info_d
;
8267 info_d
->disk
.number
= d
->index
;
8268 info_d
->disk
.major
= d
->major
;
8269 info_d
->disk
.minor
= d
->minor
;
8270 info_d
->disk
.raid_disk
= slot
;
8271 info_d
->recovery_start
= recovery_start
;
8273 if (slot
< map2
->num_members
)
8274 info_d
->disk
.state
= (1 << MD_DISK_ACTIVE
);
8276 this->array
.spare_disks
++;
8278 if (slot
< map
->num_members
)
8279 info_d
->disk
.state
= (1 << MD_DISK_ACTIVE
);
8281 this->array
.spare_disks
++;
8284 info_d
->events
= __le32_to_cpu(mpb
->generation_num
);
8285 info_d
->data_offset
= pba_of_lba0(map
);
8286 info_d
->component_size
= calc_component_size(map
, dev
);
8288 if (map
->raid_level
== 5) {
8289 info_d
->ppl_sector
= this->ppl_sector
;
8290 info_d
->ppl_size
= this->ppl_size
;
8291 if (this->consistency_policy
== CONSISTENCY_POLICY_PPL
&&
8292 recovery_start
== 0)
8293 this->resync_start
= 0;
8296 info_d
->bb
.supported
= 1;
8297 get_volume_badblocks(super
->bbm_log
, ord_to_idx(ord
),
8298 info_d
->data_offset
,
8299 info_d
->component_size
,
8302 /* now that the disk list is up-to-date fixup recovery_start */
8303 update_recovery_start(super
, dev
, this);
8304 this->array
.spare_disks
+= spare_disks
;
8306 /* check for reshape */
8307 if (this->reshape_active
== 1)
8308 recover_backup_imsm(st
, this);
8312 super
->current_vol
= current_vol
;
8316 static __u8
imsm_check_degraded(struct intel_super
*super
, struct imsm_dev
*dev
,
8317 int failed
, int look_in_map
)
8319 struct imsm_map
*map
;
8321 map
= get_imsm_map(dev
, look_in_map
);
8324 return map
->map_state
== IMSM_T_STATE_UNINITIALIZED
?
8325 IMSM_T_STATE_UNINITIALIZED
: IMSM_T_STATE_NORMAL
;
8327 switch (get_imsm_raid_level(map
)) {
8329 return IMSM_T_STATE_FAILED
;
8332 if (failed
< map
->num_members
)
8333 return IMSM_T_STATE_DEGRADED
;
8335 return IMSM_T_STATE_FAILED
;
8340 * check to see if any mirrors have failed, otherwise we
8341 * are degraded. Even numbered slots are mirrored on
8345 /* gcc -Os complains that this is unused */
8346 int insync
= insync
;
8348 for (i
= 0; i
< map
->num_members
; i
++) {
8349 __u32 ord
= get_imsm_ord_tbl_ent(dev
, i
, MAP_X
);
8350 int idx
= ord_to_idx(ord
);
8351 struct imsm_disk
*disk
;
8353 /* reset the potential in-sync count on even-numbered
8354 * slots. num_copies is always 2 for imsm raid10
8359 disk
= get_imsm_disk(super
, idx
);
8360 if (!disk
|| is_failed(disk
) || ord
& IMSM_ORD_REBUILD
)
8363 /* no in-sync disks left in this mirror the
8367 return IMSM_T_STATE_FAILED
;
8370 return IMSM_T_STATE_DEGRADED
;
8374 return IMSM_T_STATE_DEGRADED
;
8376 return IMSM_T_STATE_FAILED
;
8382 return map
->map_state
;
8385 static int imsm_count_failed(struct intel_super
*super
, struct imsm_dev
*dev
,
8390 struct imsm_disk
*disk
;
8391 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8392 struct imsm_map
*prev
= get_imsm_map(dev
, MAP_1
);
8393 struct imsm_map
*map_for_loop
;
8398 /* at the beginning of migration we set IMSM_ORD_REBUILD on
8399 * disks that are being rebuilt. New failures are recorded to
8400 * map[0]. So we look through all the disks we started with and
8401 * see if any failures are still present, or if any new ones
8405 if (prev
&& (map
->num_members
< prev
->num_members
))
8406 map_for_loop
= prev
;
8408 for (i
= 0; i
< map_for_loop
->num_members
; i
++) {
8410 /* when MAP_X is passed both maps failures are counted
8413 (look_in_map
== MAP_1
|| look_in_map
== MAP_X
) &&
8414 i
< prev
->num_members
) {
8415 ord
= __le32_to_cpu(prev
->disk_ord_tbl
[i
]);
8416 idx_1
= ord_to_idx(ord
);
8418 disk
= get_imsm_disk(super
, idx_1
);
8419 if (!disk
|| is_failed(disk
) || ord
& IMSM_ORD_REBUILD
)
8422 if ((look_in_map
== MAP_0
|| look_in_map
== MAP_X
) &&
8423 i
< map
->num_members
) {
8424 ord
= __le32_to_cpu(map
->disk_ord_tbl
[i
]);
8425 idx
= ord_to_idx(ord
);
8428 disk
= get_imsm_disk(super
, idx
);
8429 if (!disk
|| is_failed(disk
) ||
8430 ord
& IMSM_ORD_REBUILD
)
8439 static int imsm_open_new(struct supertype
*c
, struct active_array
*a
,
8442 struct intel_super
*super
= c
->sb
;
8443 struct imsm_super
*mpb
= super
->anchor
;
8444 struct imsm_update_prealloc_bb_mem u
;
8446 if (inst
>= mpb
->num_raid_devs
) {
8447 pr_err("subarry index %d, out of range\n", inst
);
8451 dprintf("imsm: open_new %d\n", inst
);
8452 a
->info
.container_member
= inst
;
8454 u
.type
= update_prealloc_badblocks_mem
;
8455 imsm_update_metadata_locally(c
, &u
, sizeof(u
));
8460 static int is_resyncing(struct imsm_dev
*dev
)
8462 struct imsm_map
*migr_map
;
8464 if (!dev
->vol
.migr_state
)
8467 if (migr_type(dev
) == MIGR_INIT
||
8468 migr_type(dev
) == MIGR_REPAIR
)
8471 if (migr_type(dev
) == MIGR_GEN_MIGR
)
8474 migr_map
= get_imsm_map(dev
, MAP_1
);
8476 if (migr_map
->map_state
== IMSM_T_STATE_NORMAL
&&
8477 dev
->vol
.migr_type
!= MIGR_GEN_MIGR
)
8483 /* return true if we recorded new information */
8484 static int mark_failure(struct intel_super
*super
,
8485 struct imsm_dev
*dev
, struct imsm_disk
*disk
, int idx
)
8489 struct imsm_map
*map
;
8490 char buf
[MAX_RAID_SERIAL_LEN
+3];
8491 unsigned int len
, shift
= 0;
8493 /* new failures are always set in map[0] */
8494 map
= get_imsm_map(dev
, MAP_0
);
8496 slot
= get_imsm_disk_slot(map
, idx
);
8500 ord
= __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
8501 if (is_failed(disk
) && (ord
& IMSM_ORD_REBUILD
))
8504 memcpy(buf
, disk
->serial
, MAX_RAID_SERIAL_LEN
);
8505 buf
[MAX_RAID_SERIAL_LEN
] = '\000';
8507 if ((len
= strlen(buf
)) >= MAX_RAID_SERIAL_LEN
)
8508 shift
= len
- MAX_RAID_SERIAL_LEN
+ 1;
8509 memcpy(disk
->serial
, &buf
[shift
], len
+ 1 - shift
);
8511 disk
->status
|= FAILED_DISK
;
8512 set_imsm_ord_tbl_ent(map
, slot
, idx
| IMSM_ORD_REBUILD
);
8513 /* mark failures in second map if second map exists and this disk
8515 * This is valid for migration, initialization and rebuild
8517 if (dev
->vol
.migr_state
) {
8518 struct imsm_map
*map2
= get_imsm_map(dev
, MAP_1
);
8519 int slot2
= get_imsm_disk_slot(map2
, idx
);
8521 if (slot2
< map2
->num_members
&& slot2
>= 0)
8522 set_imsm_ord_tbl_ent(map2
, slot2
,
8523 idx
| IMSM_ORD_REBUILD
);
8525 if (map
->failed_disk_num
== 0xff ||
8526 (!is_rebuilding(dev
) && map
->failed_disk_num
> slot
))
8527 map
->failed_disk_num
= slot
;
8529 clear_disk_badblocks(super
->bbm_log
, ord_to_idx(ord
));
8534 static void mark_missing(struct intel_super
*super
,
8535 struct imsm_dev
*dev
, struct imsm_disk
*disk
, int idx
)
8537 mark_failure(super
, dev
, disk
, idx
);
8539 if (disk
->scsi_id
== __cpu_to_le32(~(__u32
)0))
8542 disk
->scsi_id
= __cpu_to_le32(~(__u32
)0);
8543 memmove(&disk
->serial
[0], &disk
->serial
[1], MAX_RAID_SERIAL_LEN
- 1);
8546 static void handle_missing(struct intel_super
*super
, struct imsm_dev
*dev
)
8550 if (!super
->missing
)
8553 /* When orom adds replacement for missing disk it does
8554 * not remove entry of missing disk, but just updates map with
8555 * new added disk. So it is not enough just to test if there is
8556 * any missing disk, we have to look if there are any failed disks
8557 * in map to stop migration */
8559 dprintf("imsm: mark missing\n");
8560 /* end process for initialization and rebuild only
8562 if (is_gen_migration(dev
) == false) {
8563 int failed
= imsm_count_failed(super
, dev
, MAP_0
);
8567 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8568 struct imsm_map
*map1
;
8569 int i
, ord
, ord_map1
;
8572 for (i
= 0; i
< map
->num_members
; i
++) {
8573 ord
= get_imsm_ord_tbl_ent(dev
, i
, MAP_0
);
8574 if (!(ord
& IMSM_ORD_REBUILD
))
8577 map1
= get_imsm_map(dev
, MAP_1
);
8581 ord_map1
= __le32_to_cpu(map1
->disk_ord_tbl
[i
]);
8582 if (ord_map1
& IMSM_ORD_REBUILD
)
8587 map_state
= imsm_check_degraded(super
, dev
,
8589 end_migration(dev
, super
, map_state
);
8593 for (dl
= super
->missing
; dl
; dl
= dl
->next
)
8594 mark_missing(super
, dev
, &dl
->disk
, dl
->index
);
8595 super
->updates_pending
++;
8598 static unsigned long long imsm_set_array_size(struct imsm_dev
*dev
,
8601 unsigned long long array_blocks
;
8602 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8603 int used_disks
= imsm_num_data_members(map
);
8605 if (used_disks
== 0) {
8606 /* when problems occures
8607 * return current array_blocks value
8609 array_blocks
= imsm_dev_size(dev
);
8611 return array_blocks
;
8614 /* set array size in metadata
8617 /* OLCE size change is caused by added disks
8619 array_blocks
= per_dev_array_size(map
) * used_disks
;
8621 /* Online Volume Size Change
8622 * Using available free space
8624 array_blocks
= new_size
;
8626 array_blocks
= round_size_to_mb(array_blocks
, used_disks
);
8627 set_imsm_dev_size(dev
, array_blocks
);
8629 return array_blocks
;
8632 static void imsm_set_disk(struct active_array
*a
, int n
, int state
);
8634 static void imsm_progress_container_reshape(struct intel_super
*super
)
8636 /* if no device has a migr_state, but some device has a
8637 * different number of members than the previous device, start
8638 * changing the number of devices in this device to match
8641 struct imsm_super
*mpb
= super
->anchor
;
8642 int prev_disks
= -1;
8646 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
8647 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
8648 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8649 struct imsm_map
*map2
;
8650 int prev_num_members
;
8652 if (dev
->vol
.migr_state
)
8655 if (prev_disks
== -1)
8656 prev_disks
= map
->num_members
;
8657 if (prev_disks
== map
->num_members
)
8660 /* OK, this array needs to enter reshape mode.
8661 * i.e it needs a migr_state
8664 copy_map_size
= sizeof_imsm_map(map
);
8665 prev_num_members
= map
->num_members
;
8666 map
->num_members
= prev_disks
;
8667 dev
->vol
.migr_state
= 1;
8668 set_vol_curr_migr_unit(dev
, 0);
8669 set_migr_type(dev
, MIGR_GEN_MIGR
);
8670 for (i
= prev_num_members
;
8671 i
< map
->num_members
; i
++)
8672 set_imsm_ord_tbl_ent(map
, i
, i
);
8673 map2
= get_imsm_map(dev
, MAP_1
);
8674 /* Copy the current map */
8675 memcpy(map2
, map
, copy_map_size
);
8676 map2
->num_members
= prev_num_members
;
8678 imsm_set_array_size(dev
, -1);
8679 super
->clean_migration_record_by_mdmon
= 1;
8680 super
->updates_pending
++;
8684 /* Handle dirty -> clean transititions, resync and reshape. Degraded and rebuild
8685 * states are handled in imsm_set_disk() with one exception, when a
8686 * resync is stopped due to a new failure this routine will set the
8687 * 'degraded' state for the array.
8689 static int imsm_set_array_state(struct active_array
*a
, int consistent
)
8691 int inst
= a
->info
.container_member
;
8692 struct intel_super
*super
= a
->container
->sb
;
8693 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
8694 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8695 int failed
= imsm_count_failed(super
, dev
, MAP_0
);
8696 __u8 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
8697 __u32 blocks_per_unit
;
8699 if (dev
->vol
.migr_state
&&
8700 dev
->vol
.migr_type
== MIGR_GEN_MIGR
) {
8701 /* array state change is blocked due to reshape action
8703 * - abort the reshape (if last_checkpoint is 0 and action!= reshape)
8704 * - finish the reshape (if last_checkpoint is big and action != reshape)
8705 * - update vol_curr_migr_unit
8707 if (a
->curr_action
== reshape
) {
8708 /* still reshaping, maybe update vol_curr_migr_unit */
8709 goto mark_checkpoint
;
8711 if (a
->last_checkpoint
>= a
->info
.component_size
) {
8712 unsigned long long array_blocks
;
8716 used_disks
= imsm_num_data_members(map
);
8717 if (used_disks
> 0) {
8719 per_dev_array_size(map
) *
8722 round_size_to_mb(array_blocks
,
8724 a
->info
.custom_array_size
= array_blocks
;
8725 /* encourage manager to update array
8729 a
->check_reshape
= 1;
8731 /* finalize online capacity expansion/reshape */
8732 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
)
8734 mdi
->disk
.raid_disk
,
8737 imsm_progress_container_reshape(super
);
8742 /* before we activate this array handle any missing disks */
8743 if (consistent
== 2)
8744 handle_missing(super
, dev
);
8746 if (consistent
== 2 &&
8747 (!is_resync_complete(&a
->info
) ||
8748 map_state
!= IMSM_T_STATE_NORMAL
||
8749 dev
->vol
.migr_state
))
8752 if (is_resync_complete(&a
->info
)) {
8753 /* complete intialization / resync,
8754 * recovery and interrupted recovery is completed in
8757 if (is_resyncing(dev
)) {
8758 dprintf("imsm: mark resync done\n");
8759 end_migration(dev
, super
, map_state
);
8760 super
->updates_pending
++;
8761 a
->last_checkpoint
= 0;
8763 } else if ((!is_resyncing(dev
) && !failed
) &&
8764 (imsm_reshape_blocks_arrays_changes(super
) == 0)) {
8765 /* mark the start of the init process if nothing is failed */
8766 dprintf("imsm: mark resync start\n");
8767 if (map
->map_state
== IMSM_T_STATE_UNINITIALIZED
)
8768 migrate(dev
, super
, IMSM_T_STATE_NORMAL
, MIGR_INIT
);
8770 migrate(dev
, super
, IMSM_T_STATE_NORMAL
, MIGR_REPAIR
);
8771 super
->updates_pending
++;
8774 if (a
->prev_action
== idle
)
8775 goto skip_mark_checkpoint
;
8778 /* skip checkpointing for general migration,
8779 * it is controlled in mdadm
8781 if (is_gen_migration(dev
))
8782 goto skip_mark_checkpoint
;
8784 /* check if we can update vol_curr_migr_unit from resync_start,
8787 blocks_per_unit
= blocks_per_migr_unit(super
, dev
);
8788 if (blocks_per_unit
) {
8789 set_vol_curr_migr_unit(dev
,
8790 a
->last_checkpoint
/ blocks_per_unit
);
8791 dprintf("imsm: mark checkpoint (%llu)\n",
8792 vol_curr_migr_unit(dev
));
8793 super
->updates_pending
++;
8796 skip_mark_checkpoint
:
8797 /* mark dirty / clean */
8798 if (((dev
->vol
.dirty
& RAIDVOL_DIRTY
) && consistent
) ||
8799 (!(dev
->vol
.dirty
& RAIDVOL_DIRTY
) && !consistent
)) {
8800 dprintf("imsm: mark '%s'\n", consistent
? "clean" : "dirty");
8802 dev
->vol
.dirty
= RAIDVOL_CLEAN
;
8804 dev
->vol
.dirty
= RAIDVOL_DIRTY
;
8805 if (dev
->rwh_policy
== RWH_DISTRIBUTED
||
8806 dev
->rwh_policy
== RWH_MULTIPLE_DISTRIBUTED
)
8807 dev
->vol
.dirty
|= RAIDVOL_DSRECORD_VALID
;
8809 super
->updates_pending
++;
8815 static int imsm_disk_slot_to_ord(struct active_array
*a
, int slot
)
8817 int inst
= a
->info
.container_member
;
8818 struct intel_super
*super
= a
->container
->sb
;
8819 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
8820 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8822 if (slot
> map
->num_members
) {
8823 pr_err("imsm: imsm_disk_slot_to_ord %d out of range 0..%d\n",
8824 slot
, map
->num_members
- 1);
8831 return get_imsm_ord_tbl_ent(dev
, slot
, MAP_0
);
8834 static void imsm_set_disk(struct active_array
*a
, int n
, int state
)
8836 int inst
= a
->info
.container_member
;
8837 struct intel_super
*super
= a
->container
->sb
;
8838 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
8839 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8840 struct imsm_disk
*disk
;
8842 int recovery_not_finished
= 0;
8846 int rebuild_done
= 0;
8849 ord
= get_imsm_ord_tbl_ent(dev
, n
, MAP_X
);
8853 dprintf("imsm: set_disk %d:%x\n", n
, state
);
8854 disk
= get_imsm_disk(super
, ord_to_idx(ord
));
8856 /* check for new failures */
8857 if (disk
&& (state
& DS_FAULTY
)) {
8858 if (mark_failure(super
, dev
, disk
, ord_to_idx(ord
)))
8859 super
->updates_pending
++;
8862 /* check if in_sync */
8863 if (state
& DS_INSYNC
&& ord
& IMSM_ORD_REBUILD
&& is_rebuilding(dev
)) {
8864 struct imsm_map
*migr_map
= get_imsm_map(dev
, MAP_1
);
8866 set_imsm_ord_tbl_ent(migr_map
, n
, ord_to_idx(ord
));
8868 super
->updates_pending
++;
8871 failed
= imsm_count_failed(super
, dev
, MAP_0
);
8872 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
8874 /* check if recovery complete, newly degraded, or failed */
8875 dprintf("imsm: Detected transition to state ");
8876 switch (map_state
) {
8877 case IMSM_T_STATE_NORMAL
: /* transition to normal state */
8878 dprintf("normal: ");
8879 if (is_rebuilding(dev
)) {
8880 dprintf_cont("while rebuilding");
8881 /* check if recovery is really finished */
8882 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
)
8883 if (mdi
->recovery_start
!= MaxSector
) {
8884 recovery_not_finished
= 1;
8887 if (recovery_not_finished
) {
8889 dprintf("Rebuild has not finished yet, state not changed");
8890 if (a
->last_checkpoint
< mdi
->recovery_start
) {
8891 a
->last_checkpoint
= mdi
->recovery_start
;
8892 super
->updates_pending
++;
8896 end_migration(dev
, super
, map_state
);
8897 map
->failed_disk_num
= ~0;
8898 super
->updates_pending
++;
8899 a
->last_checkpoint
= 0;
8902 if (is_gen_migration(dev
)) {
8903 dprintf_cont("while general migration");
8904 if (a
->last_checkpoint
>= a
->info
.component_size
)
8905 end_migration(dev
, super
, map_state
);
8907 map
->map_state
= map_state
;
8908 map
->failed_disk_num
= ~0;
8909 super
->updates_pending
++;
8913 case IMSM_T_STATE_DEGRADED
: /* transition to degraded state */
8914 dprintf_cont("degraded: ");
8915 if (map
->map_state
!= map_state
&& !dev
->vol
.migr_state
) {
8916 dprintf_cont("mark degraded");
8917 map
->map_state
= map_state
;
8918 super
->updates_pending
++;
8919 a
->last_checkpoint
= 0;
8922 if (is_rebuilding(dev
)) {
8923 dprintf_cont("while rebuilding ");
8924 if (state
& DS_FAULTY
) {
8925 dprintf_cont("removing failed drive ");
8926 if (n
== map
->failed_disk_num
) {
8927 dprintf_cont("end migration");
8928 end_migration(dev
, super
, map_state
);
8929 a
->last_checkpoint
= 0;
8931 dprintf_cont("fail detected during rebuild, changing map state");
8932 map
->map_state
= map_state
;
8934 super
->updates_pending
++;
8940 /* check if recovery is really finished */
8941 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
)
8942 if (mdi
->recovery_start
!= MaxSector
) {
8943 recovery_not_finished
= 1;
8946 if (recovery_not_finished
) {
8948 dprintf_cont("Rebuild has not finished yet");
8949 if (a
->last_checkpoint
< mdi
->recovery_start
) {
8950 a
->last_checkpoint
=
8951 mdi
->recovery_start
;
8952 super
->updates_pending
++;
8957 dprintf_cont(" Rebuild done, still degraded");
8958 end_migration(dev
, super
, map_state
);
8959 a
->last_checkpoint
= 0;
8960 super
->updates_pending
++;
8962 for (i
= 0; i
< map
->num_members
; i
++) {
8963 int idx
= get_imsm_ord_tbl_ent(dev
, i
, MAP_0
);
8965 if (idx
& IMSM_ORD_REBUILD
)
8966 map
->failed_disk_num
= i
;
8968 super
->updates_pending
++;
8971 if (is_gen_migration(dev
)) {
8972 dprintf_cont("while general migration");
8973 if (a
->last_checkpoint
>= a
->info
.component_size
)
8974 end_migration(dev
, super
, map_state
);
8976 map
->map_state
= map_state
;
8977 manage_second_map(super
, dev
);
8979 super
->updates_pending
++;
8982 if (is_initializing(dev
)) {
8983 dprintf_cont("while initialization.");
8984 map
->map_state
= map_state
;
8985 super
->updates_pending
++;
8989 case IMSM_T_STATE_FAILED
: /* transition to failed state */
8990 dprintf_cont("failed: ");
8991 if (is_gen_migration(dev
)) {
8992 dprintf_cont("while general migration");
8993 map
->map_state
= map_state
;
8994 super
->updates_pending
++;
8997 if (map
->map_state
!= map_state
) {
8998 dprintf_cont("mark failed");
8999 end_migration(dev
, super
, map_state
);
9000 super
->updates_pending
++;
9001 a
->last_checkpoint
= 0;
9006 dprintf_cont("state %i\n", map_state
);
9011 static int store_imsm_mpb(int fd
, struct imsm_super
*mpb
)
9014 __u32 mpb_size
= __le32_to_cpu(mpb
->mpb_size
);
9015 unsigned long long dsize
;
9016 unsigned long long sectors
;
9017 unsigned int sector_size
;
9019 if (!get_dev_sector_size(fd
, NULL
, §or_size
))
9021 get_dev_size(fd
, NULL
, &dsize
);
9023 if (mpb_size
> sector_size
) {
9024 /* -1 to account for anchor */
9025 sectors
= mpb_sectors(mpb
, sector_size
) - 1;
9027 /* write the extended mpb to the sectors preceeding the anchor */
9028 if (lseek64(fd
, dsize
- (sector_size
* (2 + sectors
)),
9032 if ((unsigned long long)write(fd
, buf
+ sector_size
,
9033 sector_size
* sectors
) != sector_size
* sectors
)
9037 /* first block is stored on second to last sector of the disk */
9038 if (lseek64(fd
, dsize
- (sector_size
* 2), SEEK_SET
) < 0)
9041 if ((unsigned int)write(fd
, buf
, sector_size
) != sector_size
)
9047 static void imsm_sync_metadata(struct supertype
*container
)
9049 struct intel_super
*super
= container
->sb
;
9051 dprintf("sync metadata: %d\n", super
->updates_pending
);
9052 if (!super
->updates_pending
)
9055 write_super_imsm(container
, 0);
9057 super
->updates_pending
= 0;
9060 static struct dl
*imsm_readd(struct intel_super
*super
, int idx
, struct active_array
*a
)
9062 struct imsm_dev
*dev
= get_imsm_dev(super
, a
->info
.container_member
);
9063 int i
= get_imsm_disk_idx(dev
, idx
, MAP_X
);
9066 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
9070 if (dl
&& is_failed(&dl
->disk
))
9074 dprintf("found %x:%x\n", dl
->major
, dl
->minor
);
9079 static struct dl
*imsm_add_spare(struct intel_super
*super
, int slot
,
9080 struct active_array
*a
, int activate_new
,
9081 struct mdinfo
*additional_test_list
)
9083 struct imsm_dev
*dev
= get_imsm_dev(super
, a
->info
.container_member
);
9084 int idx
= get_imsm_disk_idx(dev
, slot
, MAP_X
);
9085 struct imsm_super
*mpb
= super
->anchor
;
9086 struct imsm_map
*map
;
9087 unsigned long long pos
;
9092 __u32 array_start
= 0;
9093 __u32 array_end
= 0;
9095 struct mdinfo
*test_list
;
9097 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
9098 /* If in this array, skip */
9099 for (d
= a
->info
.devs
; d
; d
= d
->next
)
9100 if (is_fd_valid(d
->state_fd
) &&
9101 d
->disk
.major
== dl
->major
&&
9102 d
->disk
.minor
== dl
->minor
) {
9103 dprintf("%x:%x already in array\n",
9104 dl
->major
, dl
->minor
);
9109 test_list
= additional_test_list
;
9111 if (test_list
->disk
.major
== dl
->major
&&
9112 test_list
->disk
.minor
== dl
->minor
) {
9113 dprintf("%x:%x already in additional test list\n",
9114 dl
->major
, dl
->minor
);
9117 test_list
= test_list
->next
;
9122 /* skip in use or failed drives */
9123 if (is_failed(&dl
->disk
) || idx
== dl
->index
||
9125 dprintf("%x:%x status (failed: %d index: %d)\n",
9126 dl
->major
, dl
->minor
, is_failed(&dl
->disk
), idx
);
9130 /* skip pure spares when we are looking for partially
9131 * assimilated drives
9133 if (dl
->index
== -1 && !activate_new
)
9136 if (!drive_validate_sector_size(super
, dl
))
9139 /* Does this unused device have the requisite free space?
9140 * It needs to be able to cover all member volumes
9142 ex
= get_extents(super
, dl
, 1);
9144 dprintf("cannot get extents\n");
9147 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
9148 dev
= get_imsm_dev(super
, i
);
9149 map
= get_imsm_map(dev
, MAP_0
);
9151 /* check if this disk is already a member of
9154 if (get_imsm_disk_slot(map
, dl
->index
) >= 0)
9160 array_start
= pba_of_lba0(map
);
9161 array_end
= array_start
+
9162 per_dev_array_size(map
) - 1;
9165 /* check that we can start at pba_of_lba0 with
9166 * num_data_stripes*blocks_per_stripe of space
9168 if (array_start
>= pos
&& array_end
< ex
[j
].start
) {
9172 pos
= ex
[j
].start
+ ex
[j
].size
;
9174 } while (ex
[j
-1].size
);
9181 if (i
< mpb
->num_raid_devs
) {
9182 dprintf("%x:%x does not have %u to %u available\n",
9183 dl
->major
, dl
->minor
, array_start
, array_end
);
9193 static int imsm_rebuild_allowed(struct supertype
*cont
, int dev_idx
, int failed
)
9195 struct imsm_dev
*dev2
;
9196 struct imsm_map
*map
;
9202 dev2
= get_imsm_dev(cont
->sb
, dev_idx
);
9204 state
= imsm_check_degraded(cont
->sb
, dev2
, failed
, MAP_0
);
9205 if (state
== IMSM_T_STATE_FAILED
) {
9206 map
= get_imsm_map(dev2
, MAP_0
);
9207 for (slot
= 0; slot
< map
->num_members
; slot
++) {
9209 * Check if failed disks are deleted from intel
9210 * disk list or are marked to be deleted
9212 idx
= get_imsm_disk_idx(dev2
, slot
, MAP_X
);
9213 idisk
= get_imsm_dl_disk(cont
->sb
, idx
);
9215 * Do not rebuild the array if failed disks
9216 * from failed sub-array are not removed from
9220 is_failed(&idisk
->disk
) &&
9221 (idisk
->action
!= DISK_REMOVE
))
9228 static struct mdinfo
*imsm_activate_spare(struct active_array
*a
,
9229 struct metadata_update
**updates
)
9232 * Find a device with unused free space and use it to replace a
9233 * failed/vacant region in an array. We replace failed regions one a
9234 * array at a time. The result is that a new spare disk will be added
9235 * to the first failed array and after the monitor has finished
9236 * propagating failures the remainder will be consumed.
9238 * FIXME add a capability for mdmon to request spares from another
9242 struct intel_super
*super
= a
->container
->sb
;
9243 int inst
= a
->info
.container_member
;
9244 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
9245 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
9246 int failed
= a
->info
.array
.raid_disks
;
9247 struct mdinfo
*rv
= NULL
;
9250 struct metadata_update
*mu
;
9252 struct imsm_update_activate_spare
*u
;
9257 for (d
= a
->info
.devs
; d
; d
= d
->next
) {
9258 if (!is_fd_valid(d
->state_fd
))
9261 if (d
->curr_state
& DS_FAULTY
)
9262 /* wait for Removal to happen */
9268 dprintf("imsm: activate spare: inst=%d failed=%d (%d) level=%d\n",
9269 inst
, failed
, a
->info
.array
.raid_disks
, a
->info
.array
.level
);
9271 if (imsm_reshape_blocks_arrays_changes(super
))
9274 /* Cannot activate another spare if rebuild is in progress already
9276 if (is_rebuilding(dev
)) {
9277 dprintf("imsm: No spare activation allowed. Rebuild in progress already.\n");
9281 if (a
->info
.array
.level
== 4)
9282 /* No repair for takeovered array
9283 * imsm doesn't support raid4
9287 if (imsm_check_degraded(super
, dev
, failed
, MAP_0
) !=
9288 IMSM_T_STATE_DEGRADED
)
9291 if (get_imsm_map(dev
, MAP_0
)->map_state
== IMSM_T_STATE_UNINITIALIZED
) {
9292 dprintf("imsm: No spare activation allowed. Volume is not initialized.\n");
9297 * If there are any failed disks check state of the other volume.
9298 * Block rebuild if the another one is failed until failed disks
9299 * are removed from container.
9302 dprintf("found failed disks in %.*s, check if there anotherfailed sub-array.\n",
9303 MAX_RAID_SERIAL_LEN
, dev
->volume
);
9304 /* check if states of the other volumes allow for rebuild */
9305 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
9307 allowed
= imsm_rebuild_allowed(a
->container
,
9315 /* For each slot, if it is not working, find a spare */
9316 for (i
= 0; i
< a
->info
.array
.raid_disks
; i
++) {
9317 for (d
= a
->info
.devs
; d
; d
= d
->next
)
9318 if (d
->disk
.raid_disk
== i
)
9320 dprintf("found %d: %p %x\n", i
, d
, d
?d
->curr_state
:0);
9321 if (d
&& is_fd_valid(d
->state_fd
))
9325 * OK, this device needs recovery. Try to re-add the
9326 * previous occupant of this slot, if this fails see if
9327 * we can continue the assimilation of a spare that was
9328 * partially assimilated, finally try to activate a new
9331 dl
= imsm_readd(super
, i
, a
);
9333 dl
= imsm_add_spare(super
, i
, a
, 0, rv
);
9335 dl
= imsm_add_spare(super
, i
, a
, 1, rv
);
9339 /* found a usable disk with enough space */
9340 di
= xcalloc(1, sizeof(*di
));
9342 /* dl->index will be -1 in the case we are activating a
9343 * pristine spare. imsm_process_update() will create a
9344 * new index in this case. Once a disk is found to be
9345 * failed in all member arrays it is kicked from the
9348 di
->disk
.number
= dl
->index
;
9350 /* (ab)use di->devs to store a pointer to the device
9353 di
->devs
= (struct mdinfo
*) dl
;
9355 di
->disk
.raid_disk
= i
;
9356 di
->disk
.major
= dl
->major
;
9357 di
->disk
.minor
= dl
->minor
;
9359 di
->recovery_start
= 0;
9360 di
->data_offset
= pba_of_lba0(map
);
9361 di
->component_size
= a
->info
.component_size
;
9362 di
->container_member
= inst
;
9363 di
->bb
.supported
= 1;
9364 if (a
->info
.consistency_policy
== CONSISTENCY_POLICY_PPL
) {
9365 di
->ppl_sector
= get_ppl_sector(super
, inst
);
9366 di
->ppl_size
= MULTIPLE_PPL_AREA_SIZE_IMSM
>> 9;
9368 super
->random
= random32();
9372 dprintf("%x:%x to be %d at %llu\n", dl
->major
, dl
->minor
,
9373 i
, di
->data_offset
);
9377 /* No spares found */
9379 /* Now 'rv' has a list of devices to return.
9380 * Create a metadata_update record to update the
9381 * disk_ord_tbl for the array
9383 mu
= xmalloc(sizeof(*mu
));
9384 mu
->buf
= xcalloc(num_spares
,
9385 sizeof(struct imsm_update_activate_spare
));
9387 mu
->space_list
= NULL
;
9388 mu
->len
= sizeof(struct imsm_update_activate_spare
) * num_spares
;
9389 mu
->next
= *updates
;
9390 u
= (struct imsm_update_activate_spare
*) mu
->buf
;
9392 for (di
= rv
; di
; di
= di
->next
) {
9393 u
->type
= update_activate_spare
;
9394 u
->dl
= (struct dl
*) di
->devs
;
9396 u
->slot
= di
->disk
.raid_disk
;
9407 static int disks_overlap(struct intel_super
*super
, int idx
, struct imsm_update_create_array
*u
)
9409 struct imsm_dev
*dev
= get_imsm_dev(super
, idx
);
9410 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
9411 struct imsm_map
*new_map
= get_imsm_map(&u
->dev
, MAP_0
);
9412 struct disk_info
*inf
= get_disk_info(u
);
9413 struct imsm_disk
*disk
;
9417 for (i
= 0; i
< map
->num_members
; i
++) {
9418 disk
= get_imsm_disk(super
, get_imsm_disk_idx(dev
, i
, MAP_X
));
9419 for (j
= 0; j
< new_map
->num_members
; j
++)
9420 if (serialcmp(disk
->serial
, inf
[j
].serial
) == 0)
9427 static struct dl
*get_disk_super(struct intel_super
*super
, int major
, int minor
)
9431 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
9432 if (dl
->major
== major
&& dl
->minor
== minor
)
9437 static int remove_disk_super(struct intel_super
*super
, int major
, int minor
)
9443 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
9444 if (dl
->major
== major
&& dl
->minor
== minor
) {
9447 prev
->next
= dl
->next
;
9449 super
->disks
= dl
->next
;
9451 __free_imsm_disk(dl
, 1);
9452 dprintf("removed %x:%x\n", major
, minor
);
9460 static void imsm_delete(struct intel_super
*super
, struct dl
**dlp
, unsigned index
);
9462 static int add_remove_disk_update(struct intel_super
*super
)
9464 int check_degraded
= 0;
9467 /* add/remove some spares to/from the metadata/contrainer */
9468 while (super
->disk_mgmt_list
) {
9469 struct dl
*disk_cfg
;
9471 disk_cfg
= super
->disk_mgmt_list
;
9472 super
->disk_mgmt_list
= disk_cfg
->next
;
9473 disk_cfg
->next
= NULL
;
9475 if (disk_cfg
->action
== DISK_ADD
) {
9476 disk_cfg
->next
= super
->disks
;
9477 super
->disks
= disk_cfg
;
9479 dprintf("added %x:%x\n",
9480 disk_cfg
->major
, disk_cfg
->minor
);
9481 } else if (disk_cfg
->action
== DISK_REMOVE
) {
9482 dprintf("Disk remove action processed: %x.%x\n",
9483 disk_cfg
->major
, disk_cfg
->minor
);
9484 disk
= get_disk_super(super
,
9488 /* store action status */
9489 disk
->action
= DISK_REMOVE
;
9490 /* remove spare disks only */
9491 if (disk
->index
== -1) {
9492 remove_disk_super(super
,
9496 disk_cfg
->fd
= disk
->fd
;
9500 /* release allocate disk structure */
9501 __free_imsm_disk(disk_cfg
, 1);
9504 return check_degraded
;
9507 static int apply_reshape_migration_update(struct imsm_update_reshape_migration
*u
,
9508 struct intel_super
*super
,
9511 struct intel_dev
*id
;
9512 void **tofree
= NULL
;
9515 dprintf("(enter)\n");
9516 if (u
->subdev
< 0 || u
->subdev
> 1) {
9517 dprintf("imsm: Error: Wrong subdev: %i\n", u
->subdev
);
9520 if (space_list
== NULL
|| *space_list
== NULL
) {
9521 dprintf("imsm: Error: Memory is not allocated\n");
9525 for (id
= super
->devlist
; id
; id
= id
->next
) {
9526 if (id
->index
== (unsigned)u
->subdev
) {
9527 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->subdev
);
9528 struct imsm_map
*map
;
9529 struct imsm_dev
*new_dev
=
9530 (struct imsm_dev
*)*space_list
;
9531 struct imsm_map
*migr_map
= get_imsm_map(dev
, MAP_1
);
9533 struct dl
*new_disk
;
9535 if (new_dev
== NULL
)
9537 *space_list
= **space_list
;
9538 memcpy(new_dev
, dev
, sizeof_imsm_dev(dev
, 0));
9539 map
= get_imsm_map(new_dev
, MAP_0
);
9541 dprintf("imsm: Error: migration in progress");
9545 to_state
= map
->map_state
;
9546 if ((u
->new_level
== 5) && (map
->raid_level
== 0)) {
9548 /* this should not happen */
9549 if (u
->new_disks
[0] < 0) {
9550 map
->failed_disk_num
=
9551 map
->num_members
- 1;
9552 to_state
= IMSM_T_STATE_DEGRADED
;
9554 to_state
= IMSM_T_STATE_NORMAL
;
9556 migrate(new_dev
, super
, to_state
, MIGR_GEN_MIGR
);
9557 if (u
->new_level
> -1)
9558 map
->raid_level
= u
->new_level
;
9559 migr_map
= get_imsm_map(new_dev
, MAP_1
);
9560 if ((u
->new_level
== 5) &&
9561 (migr_map
->raid_level
== 0)) {
9562 int ord
= map
->num_members
- 1;
9563 migr_map
->num_members
--;
9564 if (u
->new_disks
[0] < 0)
9565 ord
|= IMSM_ORD_REBUILD
;
9566 set_imsm_ord_tbl_ent(map
,
9567 map
->num_members
- 1,
9571 tofree
= (void **)dev
;
9573 /* update chunk size
9575 if (u
->new_chunksize
> 0) {
9576 struct imsm_map
*dest_map
=
9577 get_imsm_map(dev
, MAP_0
);
9579 imsm_num_data_members(dest_map
);
9581 if (used_disks
== 0)
9584 map
->blocks_per_strip
=
9585 __cpu_to_le16(u
->new_chunksize
* 2);
9586 update_num_data_stripes(map
, imsm_dev_size(dev
));
9589 /* ensure blocks_per_member has valid value
9591 set_blocks_per_member(map
,
9592 per_dev_array_size(map
) +
9593 NUM_BLOCKS_DIRTY_STRIPE_REGION
);
9597 if (u
->new_level
!= 5 || migr_map
->raid_level
!= 0 ||
9598 migr_map
->raid_level
== map
->raid_level
)
9601 if (u
->new_disks
[0] >= 0) {
9604 new_disk
= get_disk_super(super
,
9605 major(u
->new_disks
[0]),
9606 minor(u
->new_disks
[0]));
9607 dprintf("imsm: new disk for reshape is: %i:%i (%p, index = %i)\n",
9608 major(u
->new_disks
[0]),
9609 minor(u
->new_disks
[0]),
9610 new_disk
, new_disk
->index
);
9611 if (new_disk
== NULL
)
9612 goto error_disk_add
;
9614 new_disk
->index
= map
->num_members
- 1;
9615 /* slot to fill in autolayout
9617 new_disk
->raiddisk
= new_disk
->index
;
9618 new_disk
->disk
.status
|= CONFIGURED_DISK
;
9619 new_disk
->disk
.status
&= ~SPARE_DISK
;
9621 goto error_disk_add
;
9624 *tofree
= *space_list
;
9625 /* calculate new size
9627 imsm_set_array_size(new_dev
, -1);
9634 *space_list
= tofree
;
9638 dprintf("Error: imsm: Cannot find disk.\n");
9642 static int apply_size_change_update(struct imsm_update_size_change
*u
,
9643 struct intel_super
*super
)
9645 struct intel_dev
*id
;
9648 dprintf("(enter)\n");
9649 if (u
->subdev
< 0 || u
->subdev
> 1) {
9650 dprintf("imsm: Error: Wrong subdev: %i\n", u
->subdev
);
9654 for (id
= super
->devlist
; id
; id
= id
->next
) {
9655 if (id
->index
== (unsigned)u
->subdev
) {
9656 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->subdev
);
9657 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
9658 int used_disks
= imsm_num_data_members(map
);
9659 unsigned long long blocks_per_member
;
9660 unsigned long long new_size_per_disk
;
9662 if (used_disks
== 0)
9665 /* calculate new size
9667 new_size_per_disk
= u
->new_size
/ used_disks
;
9668 blocks_per_member
= new_size_per_disk
+
9669 NUM_BLOCKS_DIRTY_STRIPE_REGION
;
9671 imsm_set_array_size(dev
, u
->new_size
);
9672 set_blocks_per_member(map
, blocks_per_member
);
9673 update_num_data_stripes(map
, u
->new_size
);
9682 static int prepare_spare_to_activate(struct supertype
*st
,
9683 struct imsm_update_activate_spare
*u
)
9685 struct intel_super
*super
= st
->sb
;
9686 int prev_current_vol
= super
->current_vol
;
9687 struct active_array
*a
;
9690 for (a
= st
->arrays
; a
; a
= a
->next
)
9692 * Additional initialization (adding bitmap header, filling
9693 * the bitmap area with '1's to force initial rebuild for a whole
9694 * data-area) is required when adding the spare to the volume
9695 * with write-intent bitmap.
9697 if (a
->info
.container_member
== u
->array
&&
9698 a
->info
.consistency_policy
== CONSISTENCY_POLICY_BITMAP
) {
9701 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
9707 super
->current_vol
= u
->array
;
9708 if (st
->ss
->write_bitmap(st
, dl
->fd
, NoUpdate
))
9710 super
->current_vol
= prev_current_vol
;
9715 static int apply_update_activate_spare(struct imsm_update_activate_spare
*u
,
9716 struct intel_super
*super
,
9717 struct active_array
*active_array
)
9719 struct imsm_super
*mpb
= super
->anchor
;
9720 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->array
);
9721 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
9722 struct imsm_map
*migr_map
;
9723 struct active_array
*a
;
9724 struct imsm_disk
*disk
;
9731 int second_map_created
= 0;
9733 for (; u
; u
= u
->next
) {
9734 victim
= get_imsm_disk_idx(dev
, u
->slot
, MAP_X
);
9739 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
9744 pr_err("error: imsm_activate_spare passed an unknown disk (index: %d)\n",
9749 /* count failures (excluding rebuilds and the victim)
9750 * to determine map[0] state
9753 for (i
= 0; i
< map
->num_members
; i
++) {
9756 disk
= get_imsm_disk(super
,
9757 get_imsm_disk_idx(dev
, i
, MAP_X
));
9758 if (!disk
|| is_failed(disk
))
9762 /* adding a pristine spare, assign a new index */
9763 if (dl
->index
< 0) {
9764 dl
->index
= super
->anchor
->num_disks
;
9765 super
->anchor
->num_disks
++;
9768 disk
->status
|= CONFIGURED_DISK
;
9769 disk
->status
&= ~SPARE_DISK
;
9772 to_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
9773 if (!second_map_created
) {
9774 second_map_created
= 1;
9775 map
->map_state
= IMSM_T_STATE_DEGRADED
;
9776 migrate(dev
, super
, to_state
, MIGR_REBUILD
);
9778 map
->map_state
= to_state
;
9779 migr_map
= get_imsm_map(dev
, MAP_1
);
9780 set_imsm_ord_tbl_ent(map
, u
->slot
, dl
->index
);
9781 set_imsm_ord_tbl_ent(migr_map
, u
->slot
,
9782 dl
->index
| IMSM_ORD_REBUILD
);
9784 /* update the family_num to mark a new container
9785 * generation, being careful to record the existing
9786 * family_num in orig_family_num to clean up after
9787 * earlier mdadm versions that neglected to set it.
9789 if (mpb
->orig_family_num
== 0)
9790 mpb
->orig_family_num
= mpb
->family_num
;
9791 mpb
->family_num
+= super
->random
;
9793 /* count arrays using the victim in the metadata */
9795 for (a
= active_array
; a
; a
= a
->next
) {
9796 int dev_idx
= a
->info
.container_member
;
9798 if (get_disk_slot_in_dev(super
, dev_idx
, victim
) >= 0)
9802 /* delete the victim if it is no longer being
9808 /* We know that 'manager' isn't touching anything,
9809 * so it is safe to delete
9811 for (dlp
= &super
->disks
; *dlp
; dlp
= &(*dlp
)->next
)
9812 if ((*dlp
)->index
== victim
)
9815 /* victim may be on the missing list */
9817 for (dlp
= &super
->missing
; *dlp
;
9818 dlp
= &(*dlp
)->next
)
9819 if ((*dlp
)->index
== victim
)
9821 imsm_delete(super
, dlp
, victim
);
9828 static int apply_reshape_container_disks_update(struct imsm_update_reshape
*u
,
9829 struct intel_super
*super
,
9832 struct dl
*new_disk
;
9833 struct intel_dev
*id
;
9835 int delta_disks
= u
->new_raid_disks
- u
->old_raid_disks
;
9836 int disk_count
= u
->old_raid_disks
;
9837 void **tofree
= NULL
;
9838 int devices_to_reshape
= 1;
9839 struct imsm_super
*mpb
= super
->anchor
;
9841 unsigned int dev_id
;
9843 dprintf("(enter)\n");
9845 /* enable spares to use in array */
9846 for (i
= 0; i
< delta_disks
; i
++) {
9847 new_disk
= get_disk_super(super
,
9848 major(u
->new_disks
[i
]),
9849 minor(u
->new_disks
[i
]));
9850 dprintf("imsm: new disk for reshape is: %i:%i (%p, index = %i)\n",
9851 major(u
->new_disks
[i
]), minor(u
->new_disks
[i
]),
9852 new_disk
, new_disk
->index
);
9853 if (new_disk
== NULL
||
9854 (new_disk
->index
>= 0 &&
9855 new_disk
->index
< u
->old_raid_disks
))
9856 goto update_reshape_exit
;
9857 new_disk
->index
= disk_count
++;
9858 /* slot to fill in autolayout
9860 new_disk
->raiddisk
= new_disk
->index
;
9861 new_disk
->disk
.status
|=
9863 new_disk
->disk
.status
&= ~SPARE_DISK
;
9866 dprintf("imsm: volume set mpb->num_raid_devs = %i\n",
9867 mpb
->num_raid_devs
);
9868 /* manage changes in volume
9870 for (dev_id
= 0; dev_id
< mpb
->num_raid_devs
; dev_id
++) {
9871 void **sp
= *space_list
;
9872 struct imsm_dev
*newdev
;
9873 struct imsm_map
*newmap
, *oldmap
;
9875 for (id
= super
->devlist
; id
; id
= id
->next
) {
9876 if (id
->index
== dev_id
)
9885 /* Copy the dev, but not (all of) the map */
9886 memcpy(newdev
, id
->dev
, sizeof(*newdev
));
9887 oldmap
= get_imsm_map(id
->dev
, MAP_0
);
9888 newmap
= get_imsm_map(newdev
, MAP_0
);
9889 /* Copy the current map */
9890 memcpy(newmap
, oldmap
, sizeof_imsm_map(oldmap
));
9891 /* update one device only
9893 if (devices_to_reshape
) {
9894 dprintf("imsm: modifying subdev: %i\n",
9896 devices_to_reshape
--;
9897 newdev
->vol
.migr_state
= 1;
9898 set_vol_curr_migr_unit(newdev
, 0);
9899 set_migr_type(newdev
, MIGR_GEN_MIGR
);
9900 newmap
->num_members
= u
->new_raid_disks
;
9901 for (i
= 0; i
< delta_disks
; i
++) {
9902 set_imsm_ord_tbl_ent(newmap
,
9903 u
->old_raid_disks
+ i
,
9904 u
->old_raid_disks
+ i
);
9906 /* New map is correct, now need to save old map
9908 newmap
= get_imsm_map(newdev
, MAP_1
);
9909 memcpy(newmap
, oldmap
, sizeof_imsm_map(oldmap
));
9911 imsm_set_array_size(newdev
, -1);
9914 sp
= (void **)id
->dev
;
9919 /* Clear migration record */
9920 memset(super
->migr_rec
, 0, sizeof(struct migr_record
));
9923 *space_list
= tofree
;
9926 update_reshape_exit
:
9931 static int apply_takeover_update(struct imsm_update_takeover
*u
,
9932 struct intel_super
*super
,
9935 struct imsm_dev
*dev
= NULL
;
9936 struct intel_dev
*dv
;
9937 struct imsm_dev
*dev_new
;
9938 struct imsm_map
*map
;
9942 for (dv
= super
->devlist
; dv
; dv
= dv
->next
)
9943 if (dv
->index
== (unsigned int)u
->subarray
) {
9951 map
= get_imsm_map(dev
, MAP_0
);
9953 if (u
->direction
== R10_TO_R0
) {
9954 /* Number of failed disks must be half of initial disk number */
9955 if (imsm_count_failed(super
, dev
, MAP_0
) !=
9956 (map
->num_members
/ 2))
9959 /* iterate through devices to mark removed disks as spare */
9960 for (dm
= super
->disks
; dm
; dm
= dm
->next
) {
9961 if (dm
->disk
.status
& FAILED_DISK
) {
9962 int idx
= dm
->index
;
9963 /* update indexes on the disk list */
9964 /* FIXME this loop-with-the-loop looks wrong, I'm not convinced
9965 the index values will end up being correct.... NB */
9966 for (du
= super
->disks
; du
; du
= du
->next
)
9967 if (du
->index
> idx
)
9969 /* mark as spare disk */
9974 map
->num_members
/= map
->num_domains
;
9975 map
->map_state
= IMSM_T_STATE_NORMAL
;
9976 map
->raid_level
= 0;
9977 set_num_domains(map
);
9978 update_num_data_stripes(map
, imsm_dev_size(dev
));
9979 map
->failed_disk_num
= -1;
9982 if (u
->direction
== R0_TO_R10
) {
9985 /* update slots in current disk list */
9986 for (dm
= super
->disks
; dm
; dm
= dm
->next
) {
9990 /* create new *missing* disks */
9991 for (i
= 0; i
< map
->num_members
; i
++) {
9992 space
= *space_list
;
9995 *space_list
= *space
;
9997 memcpy(du
, super
->disks
, sizeof(*du
));
10001 du
->index
= (i
* 2) + 1;
10002 sprintf((char *)du
->disk
.serial
,
10003 " MISSING_%d", du
->index
);
10004 sprintf((char *)du
->serial
,
10005 "MISSING_%d", du
->index
);
10006 du
->next
= super
->missing
;
10007 super
->missing
= du
;
10009 /* create new dev and map */
10010 space
= *space_list
;
10013 *space_list
= *space
;
10014 dev_new
= (void *)space
;
10015 memcpy(dev_new
, dev
, sizeof(*dev
));
10016 /* update new map */
10017 map
= get_imsm_map(dev_new
, MAP_0
);
10019 map
->map_state
= IMSM_T_STATE_DEGRADED
;
10020 map
->raid_level
= 1;
10021 set_num_domains(map
);
10022 map
->num_members
= map
->num_members
* map
->num_domains
;
10023 update_num_data_stripes(map
, imsm_dev_size(dev
));
10025 /* replace dev<->dev_new */
10028 /* update disk order table */
10029 for (du
= super
->disks
; du
; du
= du
->next
)
10030 if (du
->index
>= 0)
10031 set_imsm_ord_tbl_ent(map
, du
->index
, du
->index
);
10032 for (du
= super
->missing
; du
; du
= du
->next
)
10033 if (du
->index
>= 0) {
10034 set_imsm_ord_tbl_ent(map
, du
->index
, du
->index
);
10035 mark_missing(super
, dv
->dev
, &du
->disk
, du
->index
);
10041 static void imsm_process_update(struct supertype
*st
,
10042 struct metadata_update
*update
)
10045 * crack open the metadata_update envelope to find the update record
10046 * update can be one of:
10047 * update_reshape_container_disks - all the arrays in the container
10048 * are being reshaped to have more devices. We need to mark
10049 * the arrays for general migration and convert selected spares
10050 * into active devices.
10051 * update_activate_spare - a spare device has replaced a failed
10052 * device in an array, update the disk_ord_tbl. If this disk is
10053 * present in all member arrays then also clear the SPARE_DISK
10055 * update_create_array
10056 * update_kill_array
10057 * update_rename_array
10058 * update_add_remove_disk
10060 struct intel_super
*super
= st
->sb
;
10061 struct imsm_super
*mpb
;
10062 enum imsm_update_type type
= *(enum imsm_update_type
*) update
->buf
;
10064 /* update requires a larger buf but the allocation failed */
10065 if (super
->next_len
&& !super
->next_buf
) {
10066 super
->next_len
= 0;
10070 if (super
->next_buf
) {
10071 memcpy(super
->next_buf
, super
->buf
, super
->len
);
10073 super
->len
= super
->next_len
;
10074 super
->buf
= super
->next_buf
;
10076 super
->next_len
= 0;
10077 super
->next_buf
= NULL
;
10080 mpb
= super
->anchor
;
10083 case update_general_migration_checkpoint
: {
10084 struct intel_dev
*id
;
10085 struct imsm_update_general_migration_checkpoint
*u
=
10086 (void *)update
->buf
;
10088 dprintf("called for update_general_migration_checkpoint\n");
10090 /* find device under general migration */
10091 for (id
= super
->devlist
; id
; id
= id
->next
) {
10092 if (is_gen_migration(id
->dev
)) {
10093 set_vol_curr_migr_unit(id
->dev
,
10094 u
->curr_migr_unit
);
10095 super
->updates_pending
++;
10100 case update_takeover
: {
10101 struct imsm_update_takeover
*u
= (void *)update
->buf
;
10102 if (apply_takeover_update(u
, super
, &update
->space_list
)) {
10103 imsm_update_version_info(super
);
10104 super
->updates_pending
++;
10109 case update_reshape_container_disks
: {
10110 struct imsm_update_reshape
*u
= (void *)update
->buf
;
10111 if (apply_reshape_container_disks_update(
10112 u
, super
, &update
->space_list
))
10113 super
->updates_pending
++;
10116 case update_reshape_migration
: {
10117 struct imsm_update_reshape_migration
*u
= (void *)update
->buf
;
10118 if (apply_reshape_migration_update(
10119 u
, super
, &update
->space_list
))
10120 super
->updates_pending
++;
10123 case update_size_change
: {
10124 struct imsm_update_size_change
*u
= (void *)update
->buf
;
10125 if (apply_size_change_update(u
, super
))
10126 super
->updates_pending
++;
10129 case update_activate_spare
: {
10130 struct imsm_update_activate_spare
*u
= (void *) update
->buf
;
10132 if (prepare_spare_to_activate(st
, u
) &&
10133 apply_update_activate_spare(u
, super
, st
->arrays
))
10134 super
->updates_pending
++;
10137 case update_create_array
: {
10138 /* someone wants to create a new array, we need to be aware of
10139 * a few races/collisions:
10140 * 1/ 'Create' called by two separate instances of mdadm
10141 * 2/ 'Create' versus 'activate_spare': mdadm has chosen
10142 * devices that have since been assimilated via
10144 * In the event this update can not be carried out mdadm will
10145 * (FIX ME) notice that its update did not take hold.
10147 struct imsm_update_create_array
*u
= (void *) update
->buf
;
10148 struct intel_dev
*dv
;
10149 struct imsm_dev
*dev
;
10150 struct imsm_map
*map
, *new_map
;
10151 unsigned long long start
, end
;
10152 unsigned long long new_start
, new_end
;
10154 struct disk_info
*inf
;
10157 /* handle racing creates: first come first serve */
10158 if (u
->dev_idx
< mpb
->num_raid_devs
) {
10159 dprintf("subarray %d already defined\n", u
->dev_idx
);
10163 /* check update is next in sequence */
10164 if (u
->dev_idx
!= mpb
->num_raid_devs
) {
10165 dprintf("can not create array %d expected index %d\n",
10166 u
->dev_idx
, mpb
->num_raid_devs
);
10170 new_map
= get_imsm_map(&u
->dev
, MAP_0
);
10171 new_start
= pba_of_lba0(new_map
);
10172 new_end
= new_start
+ per_dev_array_size(new_map
);
10173 inf
= get_disk_info(u
);
10175 /* handle activate_spare versus create race:
10176 * check to make sure that overlapping arrays do not include
10177 * overalpping disks
10179 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
10180 dev
= get_imsm_dev(super
, i
);
10181 map
= get_imsm_map(dev
, MAP_0
);
10182 start
= pba_of_lba0(map
);
10183 end
= start
+ per_dev_array_size(map
);
10184 if ((new_start
>= start
&& new_start
<= end
) ||
10185 (start
>= new_start
&& start
<= new_end
))
10190 if (disks_overlap(super
, i
, u
)) {
10191 dprintf("arrays overlap\n");
10196 /* check that prepare update was successful */
10197 if (!update
->space
) {
10198 dprintf("prepare update failed\n");
10202 /* check that all disks are still active before committing
10203 * changes. FIXME: could we instead handle this by creating a
10204 * degraded array? That's probably not what the user expects,
10205 * so better to drop this update on the floor.
10207 for (i
= 0; i
< new_map
->num_members
; i
++) {
10208 dl
= serial_to_dl(inf
[i
].serial
, super
);
10210 dprintf("disk disappeared\n");
10215 super
->updates_pending
++;
10217 /* convert spares to members and fixup ord_tbl */
10218 for (i
= 0; i
< new_map
->num_members
; i
++) {
10219 dl
= serial_to_dl(inf
[i
].serial
, super
);
10220 if (dl
->index
== -1) {
10221 dl
->index
= mpb
->num_disks
;
10223 dl
->disk
.status
|= CONFIGURED_DISK
;
10224 dl
->disk
.status
&= ~SPARE_DISK
;
10226 set_imsm_ord_tbl_ent(new_map
, i
, dl
->index
);
10229 dv
= update
->space
;
10231 update
->space
= NULL
;
10232 imsm_copy_dev(dev
, &u
->dev
);
10233 dv
->index
= u
->dev_idx
;
10234 dv
->next
= super
->devlist
;
10235 super
->devlist
= dv
;
10236 mpb
->num_raid_devs
++;
10238 imsm_update_version_info(super
);
10241 /* mdmon knows how to release update->space, but not
10242 * ((struct intel_dev *) update->space)->dev
10244 if (update
->space
) {
10245 dv
= update
->space
;
10250 case update_kill_array
: {
10251 struct imsm_update_kill_array
*u
= (void *) update
->buf
;
10252 int victim
= u
->dev_idx
;
10253 struct active_array
*a
;
10254 struct intel_dev
**dp
;
10256 /* sanity check that we are not affecting the uuid of
10257 * active arrays, or deleting an active array
10259 * FIXME when immutable ids are available, but note that
10260 * we'll also need to fixup the invalidated/active
10261 * subarray indexes in mdstat
10263 for (a
= st
->arrays
; a
; a
= a
->next
)
10264 if (a
->info
.container_member
>= victim
)
10266 /* by definition if mdmon is running at least one array
10267 * is active in the container, so checking
10268 * mpb->num_raid_devs is just extra paranoia
10270 if (a
|| mpb
->num_raid_devs
== 1 || victim
>= super
->anchor
->num_raid_devs
) {
10271 dprintf("failed to delete subarray-%d\n", victim
);
10275 for (dp
= &super
->devlist
; *dp
;)
10276 if ((*dp
)->index
== (unsigned)super
->current_vol
) {
10279 if ((*dp
)->index
> (unsigned)victim
)
10283 mpb
->num_raid_devs
--;
10284 super
->updates_pending
++;
10287 case update_rename_array
: {
10288 struct imsm_update_rename_array
*u
= (void *) update
->buf
;
10289 char name
[MAX_RAID_SERIAL_LEN
+1];
10290 int target
= u
->dev_idx
;
10291 struct active_array
*a
;
10292 struct imsm_dev
*dev
;
10294 /* sanity check that we are not affecting the uuid of
10297 memset(name
, 0, sizeof(name
));
10298 snprintf(name
, MAX_RAID_SERIAL_LEN
, "%s", (char *) u
->name
);
10299 name
[MAX_RAID_SERIAL_LEN
] = '\0';
10300 for (a
= st
->arrays
; a
; a
= a
->next
)
10301 if (a
->info
.container_member
== target
)
10303 dev
= get_imsm_dev(super
, u
->dev_idx
);
10305 if (a
|| !dev
|| imsm_is_name_allowed(super
, name
, 0) == false) {
10306 dprintf("failed to rename subarray-%d\n", target
);
10310 memcpy(dev
->volume
, name
, MAX_RAID_SERIAL_LEN
);
10311 super
->updates_pending
++;
10314 case update_add_remove_disk
: {
10315 /* we may be able to repair some arrays if disks are
10316 * being added, check the status of add_remove_disk
10317 * if discs has been added.
10319 if (add_remove_disk_update(super
)) {
10320 struct active_array
*a
;
10322 super
->updates_pending
++;
10323 for (a
= st
->arrays
; a
; a
= a
->next
)
10324 a
->check_degraded
= 1;
10328 case update_prealloc_badblocks_mem
:
10330 case update_rwh_policy
: {
10331 struct imsm_update_rwh_policy
*u
= (void *)update
->buf
;
10332 int target
= u
->dev_idx
;
10333 struct imsm_dev
*dev
= get_imsm_dev(super
, target
);
10335 if (dev
->rwh_policy
!= u
->new_policy
) {
10336 dev
->rwh_policy
= u
->new_policy
;
10337 super
->updates_pending
++;
10342 pr_err("error: unsupported process update type:(type: %d)\n", type
);
10346 static struct mdinfo
*get_spares_for_grow(struct supertype
*st
);
10348 static int imsm_prepare_update(struct supertype
*st
,
10349 struct metadata_update
*update
)
10352 * Allocate space to hold new disk entries, raid-device entries or a new
10353 * mpb if necessary. The manager synchronously waits for updates to
10354 * complete in the monitor, so new mpb buffers allocated here can be
10355 * integrated by the monitor thread without worrying about live pointers
10356 * in the manager thread.
10358 enum imsm_update_type type
;
10359 struct intel_super
*super
= st
->sb
;
10360 unsigned int sector_size
= super
->sector_size
;
10361 struct imsm_super
*mpb
= super
->anchor
;
10365 if (update
->len
< (int)sizeof(type
))
10368 type
= *(enum imsm_update_type
*) update
->buf
;
10371 case update_general_migration_checkpoint
:
10372 if (update
->len
< (int)sizeof(struct imsm_update_general_migration_checkpoint
))
10374 dprintf("called for update_general_migration_checkpoint\n");
10376 case update_takeover
: {
10377 struct imsm_update_takeover
*u
= (void *)update
->buf
;
10378 if (update
->len
< (int)sizeof(*u
))
10380 if (u
->direction
== R0_TO_R10
) {
10381 void **tail
= (void **)&update
->space_list
;
10382 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->subarray
);
10383 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
10384 int num_members
= map
->num_members
;
10387 /* allocate memory for added disks */
10388 for (i
= 0; i
< num_members
; i
++) {
10389 size
= sizeof(struct dl
);
10390 space
= xmalloc(size
);
10395 /* allocate memory for new device */
10396 size
= sizeof_imsm_dev(super
->devlist
->dev
, 0) +
10397 (num_members
* sizeof(__u32
));
10398 space
= xmalloc(size
);
10402 len
= disks_to_mpb_size(num_members
* 2);
10407 case update_reshape_container_disks
: {
10408 /* Every raid device in the container is about to
10409 * gain some more devices, and we will enter a
10411 * So each 'imsm_map' will be bigger, and the imsm_vol
10412 * will now hold 2 of them.
10413 * Thus we need new 'struct imsm_dev' allocations sized
10414 * as sizeof_imsm_dev but with more devices in both maps.
10416 struct imsm_update_reshape
*u
= (void *)update
->buf
;
10417 struct intel_dev
*dl
;
10418 void **space_tail
= (void**)&update
->space_list
;
10420 if (update
->len
< (int)sizeof(*u
))
10423 dprintf("for update_reshape\n");
10425 for (dl
= super
->devlist
; dl
; dl
= dl
->next
) {
10426 int size
= sizeof_imsm_dev(dl
->dev
, 1);
10428 if (u
->new_raid_disks
> u
->old_raid_disks
)
10429 size
+= sizeof(__u32
)*2*
10430 (u
->new_raid_disks
- u
->old_raid_disks
);
10434 *space_tail
= NULL
;
10437 len
= disks_to_mpb_size(u
->new_raid_disks
);
10438 dprintf("New anchor length is %llu\n", (unsigned long long)len
);
10441 case update_reshape_migration
: {
10442 /* for migration level 0->5 we need to add disks
10443 * so the same as for container operation we will copy
10444 * device to the bigger location.
10445 * in memory prepared device and new disk area are prepared
10446 * for usage in process update
10448 struct imsm_update_reshape_migration
*u
= (void *)update
->buf
;
10449 struct intel_dev
*id
;
10450 void **space_tail
= (void **)&update
->space_list
;
10453 int current_level
= -1;
10455 if (update
->len
< (int)sizeof(*u
))
10458 dprintf("for update_reshape\n");
10460 /* add space for bigger array in update
10462 for (id
= super
->devlist
; id
; id
= id
->next
) {
10463 if (id
->index
== (unsigned)u
->subdev
) {
10464 size
= sizeof_imsm_dev(id
->dev
, 1);
10465 if (u
->new_raid_disks
> u
->old_raid_disks
)
10466 size
+= sizeof(__u32
)*2*
10467 (u
->new_raid_disks
- u
->old_raid_disks
);
10471 *space_tail
= NULL
;
10475 if (update
->space_list
== NULL
)
10478 /* add space for disk in update
10480 size
= sizeof(struct dl
);
10484 *space_tail
= NULL
;
10486 /* add spare device to update
10488 for (id
= super
->devlist
; id
; id
= id
->next
)
10489 if (id
->index
== (unsigned)u
->subdev
) {
10490 struct imsm_dev
*dev
;
10491 struct imsm_map
*map
;
10493 dev
= get_imsm_dev(super
, u
->subdev
);
10494 map
= get_imsm_map(dev
, MAP_0
);
10495 current_level
= map
->raid_level
;
10498 if (u
->new_level
== 5 && u
->new_level
!= current_level
) {
10499 struct mdinfo
*spares
;
10501 spares
= get_spares_for_grow(st
);
10504 struct mdinfo
*dev
;
10506 dev
= spares
->devs
;
10509 makedev(dev
->disk
.major
,
10511 dl
= get_disk_super(super
,
10514 dl
->index
= u
->old_raid_disks
;
10517 sysfs_free(spares
);
10520 len
= disks_to_mpb_size(u
->new_raid_disks
);
10521 dprintf("New anchor length is %llu\n", (unsigned long long)len
);
10524 case update_size_change
: {
10525 if (update
->len
< (int)sizeof(struct imsm_update_size_change
))
10529 case update_activate_spare
: {
10530 if (update
->len
< (int)sizeof(struct imsm_update_activate_spare
))
10534 case update_create_array
: {
10535 struct imsm_update_create_array
*u
= (void *) update
->buf
;
10536 struct intel_dev
*dv
;
10537 struct imsm_dev
*dev
= &u
->dev
;
10538 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
10540 struct disk_info
*inf
;
10544 if (update
->len
< (int)sizeof(*u
))
10547 inf
= get_disk_info(u
);
10548 len
= sizeof_imsm_dev(dev
, 1);
10549 /* allocate a new super->devlist entry */
10550 dv
= xmalloc(sizeof(*dv
));
10551 dv
->dev
= xmalloc(len
);
10552 update
->space
= dv
;
10554 /* count how many spares will be converted to members */
10555 for (i
= 0; i
< map
->num_members
; i
++) {
10556 dl
= serial_to_dl(inf
[i
].serial
, super
);
10558 /* hmm maybe it failed?, nothing we can do about
10563 if (count_memberships(dl
, super
) == 0)
10566 len
+= activate
* sizeof(struct imsm_disk
);
10569 case update_kill_array
: {
10570 if (update
->len
< (int)sizeof(struct imsm_update_kill_array
))
10574 case update_rename_array
: {
10575 if (update
->len
< (int)sizeof(struct imsm_update_rename_array
))
10579 case update_add_remove_disk
:
10580 /* no update->len needed */
10582 case update_prealloc_badblocks_mem
:
10583 super
->extra_space
+= sizeof(struct bbm_log
) -
10584 get_imsm_bbm_log_size(super
->bbm_log
);
10586 case update_rwh_policy
: {
10587 if (update
->len
< (int)sizeof(struct imsm_update_rwh_policy
))
10595 /* check if we need a larger metadata buffer */
10596 if (super
->next_buf
)
10597 buf_len
= super
->next_len
;
10599 buf_len
= super
->len
;
10601 if (__le32_to_cpu(mpb
->mpb_size
) + super
->extra_space
+ len
> buf_len
) {
10602 /* ok we need a larger buf than what is currently allocated
10603 * if this allocation fails process_update will notice that
10604 * ->next_len is set and ->next_buf is NULL
10606 buf_len
= ROUND_UP(__le32_to_cpu(mpb
->mpb_size
) +
10607 super
->extra_space
+ len
, sector_size
);
10608 if (super
->next_buf
)
10609 free(super
->next_buf
);
10611 super
->next_len
= buf_len
;
10612 if (posix_memalign(&super
->next_buf
, sector_size
, buf_len
) == 0)
10613 memset(super
->next_buf
, 0, buf_len
);
10615 super
->next_buf
= NULL
;
10620 /* must be called while manager is quiesced */
10621 static void imsm_delete(struct intel_super
*super
, struct dl
**dlp
, unsigned index
)
10623 struct imsm_super
*mpb
= super
->anchor
;
10625 struct imsm_dev
*dev
;
10626 struct imsm_map
*map
;
10627 unsigned int i
, j
, num_members
;
10628 __u32 ord
, ord_map0
;
10629 struct bbm_log
*log
= super
->bbm_log
;
10631 dprintf("deleting device[%d] from imsm_super\n", index
);
10633 /* shift all indexes down one */
10634 for (iter
= super
->disks
; iter
; iter
= iter
->next
)
10635 if (iter
->index
> (int)index
)
10637 for (iter
= super
->missing
; iter
; iter
= iter
->next
)
10638 if (iter
->index
> (int)index
)
10641 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
10642 dev
= get_imsm_dev(super
, i
);
10643 map
= get_imsm_map(dev
, MAP_0
);
10644 num_members
= map
->num_members
;
10645 for (j
= 0; j
< num_members
; j
++) {
10646 /* update ord entries being careful not to propagate
10647 * ord-flags to the first map
10649 ord
= get_imsm_ord_tbl_ent(dev
, j
, MAP_X
);
10650 ord_map0
= get_imsm_ord_tbl_ent(dev
, j
, MAP_0
);
10652 if (ord_to_idx(ord
) <= index
)
10655 map
= get_imsm_map(dev
, MAP_0
);
10656 set_imsm_ord_tbl_ent(map
, j
, ord_map0
- 1);
10657 map
= get_imsm_map(dev
, MAP_1
);
10659 set_imsm_ord_tbl_ent(map
, j
, ord
- 1);
10663 for (i
= 0; i
< log
->entry_count
; i
++) {
10664 struct bbm_log_entry
*entry
= &log
->marked_block_entries
[i
];
10666 if (entry
->disk_ordinal
<= index
)
10668 entry
->disk_ordinal
--;
10672 super
->updates_pending
++;
10674 struct dl
*dl
= *dlp
;
10676 *dlp
= (*dlp
)->next
;
10677 __free_imsm_disk(dl
, 1);
10681 static int imsm_get_allowed_degradation(int level
, int raid_disks
,
10682 struct intel_super
*super
,
10683 struct imsm_dev
*dev
)
10689 struct imsm_map
*map
;
10692 ret_val
= raid_disks
/2;
10693 /* check map if all disks pairs not failed
10696 map
= get_imsm_map(dev
, MAP_0
);
10697 for (i
= 0; i
< ret_val
; i
++) {
10698 int degradation
= 0;
10699 if (get_imsm_disk(super
, i
) == NULL
)
10701 if (get_imsm_disk(super
, i
+ 1) == NULL
)
10703 if (degradation
== 2)
10706 map
= get_imsm_map(dev
, MAP_1
);
10707 /* if there is no second map
10708 * result can be returned
10712 /* check degradation in second map
10714 for (i
= 0; i
< ret_val
; i
++) {
10715 int degradation
= 0;
10716 if (get_imsm_disk(super
, i
) == NULL
)
10718 if (get_imsm_disk(super
, i
+ 1) == NULL
)
10720 if (degradation
== 2)
10734 /*******************************************************************************
10735 * Function: validate_container_imsm
10736 * Description: This routine validates container after assemble,
10737 * eg. if devices in container are under the same controller.
10740 * info : linked list with info about devices used in array
10744 ******************************************************************************/
10745 int validate_container_imsm(struct mdinfo
*info
)
10747 if (check_no_platform())
10750 struct sys_dev
*idev
;
10751 struct sys_dev
*hba
= NULL
;
10752 struct sys_dev
*intel_devices
= find_intel_devices();
10753 char *dev_path
= devt_to_devpath(makedev(info
->disk
.major
,
10754 info
->disk
.minor
), 1, NULL
);
10756 for (idev
= intel_devices
; idev
; idev
= idev
->next
) {
10757 if (dev_path
&& strstr(dev_path
, idev
->path
)) {
10766 pr_err("WARNING - Cannot detect HBA for device %s!\n",
10767 devid2kname(makedev(info
->disk
.major
, info
->disk
.minor
)));
10771 const struct imsm_orom
*orom
= get_orom_by_device_id(hba
->dev_id
);
10772 struct mdinfo
*dev
;
10774 for (dev
= info
->next
; dev
; dev
= dev
->next
) {
10775 dev_path
= devt_to_devpath(makedev(dev
->disk
.major
,
10776 dev
->disk
.minor
), 1, NULL
);
10778 struct sys_dev
*hba2
= NULL
;
10779 for (idev
= intel_devices
; idev
; idev
= idev
->next
) {
10780 if (dev_path
&& strstr(dev_path
, idev
->path
)) {
10788 const struct imsm_orom
*orom2
= hba2
== NULL
? NULL
:
10789 get_orom_by_device_id(hba2
->dev_id
);
10791 if (hba2
&& hba
->type
!= hba2
->type
) {
10792 pr_err("WARNING - HBAs of devices do not match %s != %s\n",
10793 get_sys_dev_type(hba
->type
), get_sys_dev_type(hba2
->type
));
10797 if (orom
!= orom2
) {
10798 pr_err("WARNING - IMSM container assembled with disks under different HBAs!\n"
10799 " This operation is not supported and can lead to data loss.\n");
10804 pr_err("WARNING - IMSM container assembled with disks under HBAs without IMSM platform support!\n"
10805 " This operation is not supported and can lead to data loss.\n");
10813 /*******************************************************************************
10814 * Function: imsm_record_badblock
10815 * Description: This routine stores new bad block record in BBM log
10818 * a : array containing a bad block
10819 * slot : disk number containing a bad block
10820 * sector : bad block sector
10821 * length : bad block sectors range
10825 ******************************************************************************/
10826 static int imsm_record_badblock(struct active_array
*a
, int slot
,
10827 unsigned long long sector
, int length
)
10829 struct intel_super
*super
= a
->container
->sb
;
10833 ord
= imsm_disk_slot_to_ord(a
, slot
);
10837 ret
= record_new_badblock(super
->bbm_log
, ord_to_idx(ord
), sector
,
10840 super
->updates_pending
++;
10844 /*******************************************************************************
10845 * Function: imsm_clear_badblock
10846 * Description: This routine clears bad block record from BBM log
10849 * a : array containing a bad block
10850 * slot : disk number containing a bad block
10851 * sector : bad block sector
10852 * length : bad block sectors range
10856 ******************************************************************************/
10857 static int imsm_clear_badblock(struct active_array
*a
, int slot
,
10858 unsigned long long sector
, int length
)
10860 struct intel_super
*super
= a
->container
->sb
;
10864 ord
= imsm_disk_slot_to_ord(a
, slot
);
10868 ret
= clear_badblock(super
->bbm_log
, ord_to_idx(ord
), sector
, length
);
10870 super
->updates_pending
++;
10874 /*******************************************************************************
10875 * Function: imsm_get_badblocks
10876 * Description: This routine get list of bad blocks for an array
10880 * slot : disk number
10882 * bb : structure containing bad blocks
10884 ******************************************************************************/
10885 static struct md_bb
*imsm_get_badblocks(struct active_array
*a
, int slot
)
10887 int inst
= a
->info
.container_member
;
10888 struct intel_super
*super
= a
->container
->sb
;
10889 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
10890 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
10893 ord
= imsm_disk_slot_to_ord(a
, slot
);
10897 get_volume_badblocks(super
->bbm_log
, ord_to_idx(ord
), pba_of_lba0(map
),
10898 per_dev_array_size(map
), &super
->bb
);
10902 /*******************************************************************************
10903 * Function: examine_badblocks_imsm
10904 * Description: Prints list of bad blocks on a disk to the standard output
10907 * st : metadata handler
10908 * fd : open file descriptor for device
10909 * devname : device name
10913 ******************************************************************************/
10914 static int examine_badblocks_imsm(struct supertype
*st
, int fd
, char *devname
)
10916 struct intel_super
*super
= st
->sb
;
10917 struct bbm_log
*log
= super
->bbm_log
;
10918 struct dl
*d
= NULL
;
10921 for (d
= super
->disks
; d
; d
= d
->next
) {
10922 if (strcmp(d
->devname
, devname
) == 0)
10926 if ((d
== NULL
) || (d
->index
< 0)) { /* serial mismatch probably */
10927 pr_err("%s doesn't appear to be part of a raid array\n",
10934 struct bbm_log_entry
*entry
= &log
->marked_block_entries
[0];
10936 for (i
= 0; i
< log
->entry_count
; i
++) {
10937 if (entry
[i
].disk_ordinal
== d
->index
) {
10938 unsigned long long sector
= __le48_to_cpu(
10939 &entry
[i
].defective_block_start
);
10940 int cnt
= entry
[i
].marked_count
+ 1;
10943 printf("Bad-blocks on %s:\n", devname
);
10947 printf("%20llu for %d sectors\n", sector
, cnt
);
10953 printf("No bad-blocks list configured on %s\n", devname
);
10957 /*******************************************************************************
10958 * Function: init_migr_record_imsm
10959 * Description: Function inits imsm migration record
10961 * super : imsm internal array info
10962 * dev : device under migration
10963 * info : general array info to find the smallest device
10966 ******************************************************************************/
10967 void init_migr_record_imsm(struct supertype
*st
, struct imsm_dev
*dev
,
10968 struct mdinfo
*info
)
10970 struct intel_super
*super
= st
->sb
;
10971 struct migr_record
*migr_rec
= super
->migr_rec
;
10972 int new_data_disks
;
10973 unsigned long long dsize
, dev_sectors
;
10974 long long unsigned min_dev_sectors
= -1LLU;
10975 struct imsm_map
*map_dest
= get_imsm_map(dev
, MAP_0
);
10976 struct imsm_map
*map_src
= get_imsm_map(dev
, MAP_1
);
10977 unsigned long long num_migr_units
;
10978 unsigned long long array_blocks
;
10979 struct dl
*dl_disk
= NULL
;
10981 memset(migr_rec
, 0, sizeof(struct migr_record
));
10982 migr_rec
->family_num
= __cpu_to_le32(super
->anchor
->family_num
);
10984 /* only ascending reshape supported now */
10985 migr_rec
->ascending_migr
= __cpu_to_le32(1);
10987 migr_rec
->dest_depth_per_unit
= GEN_MIGR_AREA_SIZE
/
10988 max(map_dest
->blocks_per_strip
, map_src
->blocks_per_strip
);
10989 migr_rec
->dest_depth_per_unit
*=
10990 max(map_dest
->blocks_per_strip
, map_src
->blocks_per_strip
);
10991 new_data_disks
= imsm_num_data_members(map_dest
);
10992 migr_rec
->blocks_per_unit
=
10993 __cpu_to_le32(migr_rec
->dest_depth_per_unit
* new_data_disks
);
10994 migr_rec
->dest_depth_per_unit
=
10995 __cpu_to_le32(migr_rec
->dest_depth_per_unit
);
10996 array_blocks
= info
->component_size
* new_data_disks
;
10998 array_blocks
/ __le32_to_cpu(migr_rec
->blocks_per_unit
);
11000 if (array_blocks
% __le32_to_cpu(migr_rec
->blocks_per_unit
))
11002 set_num_migr_units(migr_rec
, num_migr_units
);
11004 migr_rec
->post_migr_vol_cap
= dev
->size_low
;
11005 migr_rec
->post_migr_vol_cap_hi
= dev
->size_high
;
11007 /* Find the smallest dev */
11008 for (dl_disk
= super
->disks
; dl_disk
; dl_disk
= dl_disk
->next
) {
11009 /* ignore spares in container */
11010 if (dl_disk
->index
< 0)
11012 get_dev_size(dl_disk
->fd
, NULL
, &dsize
);
11013 dev_sectors
= dsize
/ 512;
11014 if (dev_sectors
< min_dev_sectors
)
11015 min_dev_sectors
= dev_sectors
;
11017 set_migr_chkp_area_pba(migr_rec
, min_dev_sectors
-
11018 RAID_DISK_RESERVED_BLOCKS_IMSM_HI
);
11020 write_imsm_migr_rec(st
);
11025 /*******************************************************************************
11026 * Function: save_backup_imsm
11027 * Description: Function saves critical data stripes to Migration Copy Area
11028 * and updates the current migration unit status.
11029 * Use restore_stripes() to form a destination stripe,
11030 * and to write it to the Copy Area.
11032 * st : supertype information
11033 * dev : imsm device that backup is saved for
11034 * info : general array info
11035 * buf : input buffer
11036 * length : length of data to backup (blocks_per_unit)
11040 ******************************************************************************/
11041 int save_backup_imsm(struct supertype
*st
,
11042 struct imsm_dev
*dev
,
11043 struct mdinfo
*info
,
11048 struct intel_super
*super
= st
->sb
;
11050 struct imsm_map
*map_dest
= get_imsm_map(dev
, MAP_0
);
11051 int new_disks
= map_dest
->num_members
;
11052 int dest_layout
= 0;
11053 int dest_chunk
, targets
[new_disks
];
11054 unsigned long long start
, target_offsets
[new_disks
];
11055 int data_disks
= imsm_num_data_members(map_dest
);
11057 for (i
= 0; i
< new_disks
; i
++) {
11058 struct dl
*dl_disk
= get_imsm_dl_disk(super
, i
);
11059 if (dl_disk
&& is_fd_valid(dl_disk
->fd
))
11060 targets
[i
] = dl_disk
->fd
;
11065 start
= info
->reshape_progress
* 512;
11066 for (i
= 0; i
< new_disks
; i
++) {
11067 target_offsets
[i
] = migr_chkp_area_pba(super
->migr_rec
) * 512;
11068 /* move back copy area adderss, it will be moved forward
11069 * in restore_stripes() using start input variable
11071 target_offsets
[i
] -= start
/data_disks
;
11074 dest_layout
= imsm_level_to_layout(map_dest
->raid_level
);
11075 dest_chunk
= __le16_to_cpu(map_dest
->blocks_per_strip
) * 512;
11077 if (restore_stripes(targets
, /* list of dest devices */
11078 target_offsets
, /* migration record offsets */
11081 map_dest
->raid_level
,
11083 -1, /* source backup file descriptor */
11084 0, /* input buf offset
11085 * always 0 buf is already offseted */
11089 pr_err("Error restoring stripes\n");
11099 /*******************************************************************************
11100 * Function: save_checkpoint_imsm
11101 * Description: Function called for current unit status update
11102 * in the migration record. It writes it to disk.
11104 * super : imsm internal array info
11105 * info : general array info
11109 * 2: failure, means no valid migration record
11110 * / no general migration in progress /
11111 ******************************************************************************/
11112 int save_checkpoint_imsm(struct supertype
*st
, struct mdinfo
*info
, int state
)
11114 struct intel_super
*super
= st
->sb
;
11115 unsigned long long blocks_per_unit
;
11116 unsigned long long curr_migr_unit
;
11118 if (load_imsm_migr_rec(super
) != 0) {
11119 dprintf("imsm: ERROR: Cannot read migration record for checkpoint save.\n");
11123 blocks_per_unit
= __le32_to_cpu(super
->migr_rec
->blocks_per_unit
);
11124 if (blocks_per_unit
== 0) {
11125 dprintf("imsm: no migration in progress.\n");
11128 curr_migr_unit
= info
->reshape_progress
/ blocks_per_unit
;
11129 /* check if array is alligned to copy area
11130 * if it is not alligned, add one to current migration unit value
11131 * this can happend on array reshape finish only
11133 if (info
->reshape_progress
% blocks_per_unit
)
11136 set_current_migr_unit(super
->migr_rec
, curr_migr_unit
);
11137 super
->migr_rec
->rec_status
= __cpu_to_le32(state
);
11138 set_migr_dest_1st_member_lba(super
->migr_rec
,
11139 super
->migr_rec
->dest_depth_per_unit
* curr_migr_unit
);
11141 if (write_imsm_migr_rec(st
) < 0) {
11142 dprintf("imsm: Cannot write migration record outside backup area\n");
11149 /*******************************************************************************
11150 * Function: recover_backup_imsm
11151 * Description: Function recovers critical data from the Migration Copy Area
11152 * while assembling an array.
11154 * super : imsm internal array info
11155 * info : general array info
11157 * 0 : success (or there is no data to recover)
11159 ******************************************************************************/
11160 int recover_backup_imsm(struct supertype
*st
, struct mdinfo
*info
)
11162 struct intel_super
*super
= st
->sb
;
11163 struct migr_record
*migr_rec
= super
->migr_rec
;
11164 struct imsm_map
*map_dest
;
11165 struct intel_dev
*id
= NULL
;
11166 unsigned long long read_offset
;
11167 unsigned long long write_offset
;
11169 int new_disks
, err
;
11172 unsigned int sector_size
= super
->sector_size
;
11173 unsigned long long curr_migr_unit
= current_migr_unit(migr_rec
);
11174 unsigned long long num_migr_units
= get_num_migr_units(migr_rec
);
11175 char buffer
[SYSFS_MAX_BUF_SIZE
];
11176 int skipped_disks
= 0;
11177 struct dl
*dl_disk
;
11179 err
= sysfs_get_str(info
, NULL
, "array_state", (char *)buffer
, sizeof(buffer
));
11183 /* recover data only during assemblation */
11184 if (strncmp(buffer
, "inactive", 8) != 0)
11186 /* no data to recover */
11187 if (__le32_to_cpu(migr_rec
->rec_status
) == UNIT_SRC_NORMAL
)
11189 if (curr_migr_unit
>= num_migr_units
)
11192 /* find device during reshape */
11193 for (id
= super
->devlist
; id
; id
= id
->next
)
11194 if (is_gen_migration(id
->dev
))
11199 map_dest
= get_imsm_map(id
->dev
, MAP_0
);
11200 new_disks
= map_dest
->num_members
;
11202 read_offset
= migr_chkp_area_pba(migr_rec
) * 512;
11204 write_offset
= (migr_dest_1st_member_lba(migr_rec
) +
11205 pba_of_lba0(map_dest
)) * 512;
11207 unit_len
= __le32_to_cpu(migr_rec
->dest_depth_per_unit
) * 512;
11208 if (posix_memalign((void **)&buf
, sector_size
, unit_len
) != 0)
11211 for (dl_disk
= super
->disks
; dl_disk
; dl_disk
= dl_disk
->next
) {
11212 if (dl_disk
->index
< 0)
11215 if (!is_fd_valid(dl_disk
->fd
)) {
11219 if (lseek64(dl_disk
->fd
, read_offset
, SEEK_SET
) < 0) {
11220 pr_err("Cannot seek to block: %s\n",
11225 if (read(dl_disk
->fd
, buf
, unit_len
) != (ssize_t
)unit_len
) {
11226 pr_err("Cannot read copy area block: %s\n",
11231 if (lseek64(dl_disk
->fd
, write_offset
, SEEK_SET
) < 0) {
11232 pr_err("Cannot seek to block: %s\n",
11237 if (write(dl_disk
->fd
, buf
, unit_len
) != (ssize_t
)unit_len
) {
11238 pr_err("Cannot restore block: %s\n",
11245 if (skipped_disks
> imsm_get_allowed_degradation(info
->new_level
,
11249 pr_err("Cannot restore data from backup. Too many failed disks\n");
11253 if (save_checkpoint_imsm(st
, info
, UNIT_SRC_NORMAL
)) {
11254 /* ignore error == 2, this can mean end of reshape here
11256 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL) during restart\n");
11265 static char disk_by_path
[] = "/dev/disk/by-path/";
11267 static const char *imsm_get_disk_controller_domain(const char *path
)
11269 char disk_path
[PATH_MAX
];
11273 strncpy(disk_path
, disk_by_path
, PATH_MAX
);
11274 strncat(disk_path
, path
, PATH_MAX
- strlen(disk_path
) - 1);
11275 if (stat(disk_path
, &st
) == 0) {
11276 struct sys_dev
* hba
;
11279 path
= devt_to_devpath(st
.st_rdev
, 1, NULL
);
11282 hba
= find_disk_attached_hba(-1, path
);
11283 if (hba
&& hba
->type
== SYS_DEV_SAS
)
11285 else if (hba
&& (hba
->type
== SYS_DEV_SATA
|| hba
->type
== SYS_DEV_SATA_VMD
))
11287 else if (hba
&& hba
->type
== SYS_DEV_VMD
)
11289 else if (hba
&& hba
->type
== SYS_DEV_NVME
)
11293 dprintf("path: %s hba: %s attached: %s\n",
11294 path
, (hba
) ? hba
->path
: "NULL", drv
);
11300 static char *imsm_find_array_devnm_by_subdev(int subdev
, char *container
)
11302 static char devnm
[32];
11303 char subdev_name
[20];
11304 struct mdstat_ent
*mdstat
;
11306 sprintf(subdev_name
, "%d", subdev
);
11307 mdstat
= mdstat_by_subdev(subdev_name
, container
);
11311 strcpy(devnm
, mdstat
->devnm
);
11312 free_mdstat(mdstat
);
11316 static int imsm_reshape_is_allowed_on_container(struct supertype
*st
,
11317 struct geo_params
*geo
,
11318 int *old_raid_disks
,
11321 /* currently we only support increasing the number of devices
11322 * for a container. This increases the number of device for each
11323 * member array. They must all be RAID0 or RAID5.
11326 struct mdinfo
*info
, *member
;
11327 int devices_that_can_grow
= 0;
11329 dprintf("imsm: imsm_reshape_is_allowed_on_container(ENTER): st->devnm = (%s)\n", st
->devnm
);
11331 if (geo
->size
> 0 ||
11332 geo
->level
!= UnSet
||
11333 geo
->layout
!= UnSet
||
11334 geo
->chunksize
!= 0 ||
11335 geo
->raid_disks
== UnSet
) {
11336 dprintf("imsm: Container operation is allowed for raid disks number change only.\n");
11340 if (direction
== ROLLBACK_METADATA_CHANGES
) {
11341 dprintf("imsm: Metadata changes rollback is not supported for container operation.\n");
11345 info
= container_content_imsm(st
, NULL
);
11346 for (member
= info
; member
; member
= member
->next
) {
11349 dprintf("imsm: checking device_num: %i\n",
11350 member
->container_member
);
11352 if (geo
->raid_disks
<= member
->array
.raid_disks
) {
11353 /* we work on container for Online Capacity Expansion
11354 * only so raid_disks has to grow
11356 dprintf("imsm: for container operation raid disks increase is required\n");
11360 if (info
->array
.level
!= 0 && info
->array
.level
!= 5) {
11361 /* we cannot use this container with other raid level
11363 dprintf("imsm: for container operation wrong raid level (%i) detected\n",
11364 info
->array
.level
);
11367 /* check for platform support
11368 * for this raid level configuration
11370 struct intel_super
*super
= st
->sb
;
11371 if (!is_raid_level_supported(super
->orom
,
11372 member
->array
.level
,
11373 geo
->raid_disks
)) {
11374 dprintf("platform does not support raid%d with %d disk%s\n",
11377 geo
->raid_disks
> 1 ? "s" : "");
11380 /* check if component size is aligned to chunk size
11382 if (info
->component_size
%
11383 (info
->array
.chunk_size
/512)) {
11384 dprintf("Component size is not aligned to chunk size\n");
11389 if (*old_raid_disks
&&
11390 info
->array
.raid_disks
!= *old_raid_disks
)
11392 *old_raid_disks
= info
->array
.raid_disks
;
11394 /* All raid5 and raid0 volumes in container
11395 * have to be ready for Online Capacity Expansion
11396 * so they need to be assembled. We have already
11397 * checked that no recovery etc is happening.
11399 result
= imsm_find_array_devnm_by_subdev(member
->container_member
,
11400 st
->container_devnm
);
11401 if (result
== NULL
) {
11402 dprintf("imsm: cannot find array\n");
11405 devices_that_can_grow
++;
11408 if (!member
&& devices_that_can_grow
)
11412 dprintf("Container operation allowed\n");
11414 dprintf("Error: %i\n", ret_val
);
11419 /* Function: get_spares_for_grow
11420 * Description: Allocates memory and creates list of spare devices
11421 * avaliable in container. Checks if spare drive size is acceptable.
11422 * Parameters: Pointer to the supertype structure
11423 * Returns: Pointer to the list of spare devices (mdinfo structure) on success,
11426 static struct mdinfo
*get_spares_for_grow(struct supertype
*st
)
11428 struct spare_criteria sc
;
11430 get_spare_criteria_imsm(st
, &sc
);
11431 return container_choose_spares(st
, &sc
, NULL
, NULL
, NULL
, 0);
11434 /******************************************************************************
11435 * function: imsm_create_metadata_update_for_reshape
11436 * Function creates update for whole IMSM container.
11438 ******************************************************************************/
11439 static int imsm_create_metadata_update_for_reshape(
11440 struct supertype
*st
,
11441 struct geo_params
*geo
,
11442 int old_raid_disks
,
11443 struct imsm_update_reshape
**updatep
)
11445 struct intel_super
*super
= st
->sb
;
11446 struct imsm_super
*mpb
= super
->anchor
;
11447 int update_memory_size
;
11448 struct imsm_update_reshape
*u
;
11449 struct mdinfo
*spares
;
11452 struct mdinfo
*dev
;
11454 dprintf("(enter) raid_disks = %i\n", geo
->raid_disks
);
11456 delta_disks
= geo
->raid_disks
- old_raid_disks
;
11458 /* size of all update data without anchor */
11459 update_memory_size
= sizeof(struct imsm_update_reshape
);
11461 /* now add space for spare disks that we need to add. */
11462 update_memory_size
+= sizeof(u
->new_disks
[0]) * (delta_disks
- 1);
11464 u
= xcalloc(1, update_memory_size
);
11465 u
->type
= update_reshape_container_disks
;
11466 u
->old_raid_disks
= old_raid_disks
;
11467 u
->new_raid_disks
= geo
->raid_disks
;
11469 /* now get spare disks list
11471 spares
= get_spares_for_grow(st
);
11473 if (spares
== NULL
|| delta_disks
> spares
->array
.spare_disks
) {
11474 pr_err("imsm: ERROR: Cannot get spare devices for %s.\n", geo
->dev_name
);
11479 /* we have got spares
11480 * update disk list in imsm_disk list table in anchor
11482 dprintf("imsm: %i spares are available.\n\n",
11483 spares
->array
.spare_disks
);
11485 dev
= spares
->devs
;
11486 for (i
= 0; i
< delta_disks
; i
++) {
11491 u
->new_disks
[i
] = makedev(dev
->disk
.major
,
11493 dl
= get_disk_super(super
, dev
->disk
.major
, dev
->disk
.minor
);
11494 dl
->index
= mpb
->num_disks
;
11502 sysfs_free(spares
);
11504 dprintf("imsm: reshape update preparation :");
11505 if (i
== delta_disks
) {
11506 dprintf_cont(" OK\n");
11508 return update_memory_size
;
11511 dprintf_cont(" Error\n");
11516 /******************************************************************************
11517 * function: imsm_create_metadata_update_for_size_change()
11518 * Creates update for IMSM array for array size change.
11520 ******************************************************************************/
11521 static int imsm_create_metadata_update_for_size_change(
11522 struct supertype
*st
,
11523 struct geo_params
*geo
,
11524 struct imsm_update_size_change
**updatep
)
11526 struct intel_super
*super
= st
->sb
;
11527 int update_memory_size
;
11528 struct imsm_update_size_change
*u
;
11530 dprintf("(enter) New size = %llu\n", geo
->size
);
11532 /* size of all update data without anchor */
11533 update_memory_size
= sizeof(struct imsm_update_size_change
);
11535 u
= xcalloc(1, update_memory_size
);
11536 u
->type
= update_size_change
;
11537 u
->subdev
= super
->current_vol
;
11538 u
->new_size
= geo
->size
;
11540 dprintf("imsm: reshape update preparation : OK\n");
11543 return update_memory_size
;
11546 /******************************************************************************
11547 * function: imsm_create_metadata_update_for_migration()
11548 * Creates update for IMSM array.
11550 ******************************************************************************/
11551 static int imsm_create_metadata_update_for_migration(
11552 struct supertype
*st
,
11553 struct geo_params
*geo
,
11554 struct imsm_update_reshape_migration
**updatep
)
11556 struct intel_super
*super
= st
->sb
;
11557 int update_memory_size
;
11558 int current_chunk_size
;
11559 struct imsm_update_reshape_migration
*u
;
11560 struct imsm_dev
*dev
= get_imsm_dev(super
, super
->current_vol
);
11561 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
11562 int previous_level
= -1;
11564 dprintf("(enter) New Level = %i\n", geo
->level
);
11566 /* size of all update data without anchor */
11567 update_memory_size
= sizeof(struct imsm_update_reshape_migration
);
11569 u
= xcalloc(1, update_memory_size
);
11570 u
->type
= update_reshape_migration
;
11571 u
->subdev
= super
->current_vol
;
11572 u
->new_level
= geo
->level
;
11573 u
->new_layout
= geo
->layout
;
11574 u
->new_raid_disks
= u
->old_raid_disks
= geo
->raid_disks
;
11575 u
->new_disks
[0] = -1;
11576 u
->new_chunksize
= -1;
11578 current_chunk_size
= __le16_to_cpu(map
->blocks_per_strip
) / 2;
11580 if (geo
->chunksize
!= current_chunk_size
) {
11581 u
->new_chunksize
= geo
->chunksize
/ 1024;
11582 dprintf("imsm: chunk size change from %i to %i\n",
11583 current_chunk_size
, u
->new_chunksize
);
11585 previous_level
= map
->raid_level
;
11587 if (geo
->level
== 5 && previous_level
== 0) {
11588 struct mdinfo
*spares
= NULL
;
11590 u
->new_raid_disks
++;
11591 spares
= get_spares_for_grow(st
);
11592 if (spares
== NULL
|| spares
->array
.spare_disks
< 1) {
11594 sysfs_free(spares
);
11595 update_memory_size
= 0;
11596 pr_err("cannot get spare device for requested migration\n");
11599 sysfs_free(spares
);
11601 dprintf("imsm: reshape update preparation : OK\n");
11604 return update_memory_size
;
11607 static void imsm_update_metadata_locally(struct supertype
*st
,
11608 void *buf
, int len
)
11610 struct metadata_update mu
;
11615 mu
.space_list
= NULL
;
11617 if (imsm_prepare_update(st
, &mu
))
11618 imsm_process_update(st
, &mu
);
11620 while (mu
.space_list
) {
11621 void **space
= mu
.space_list
;
11622 mu
.space_list
= *space
;
11628 * imsm_analyze_expand() - check expand properties and calculate new size.
11629 * @st: imsm supertype.
11630 * @geo: new geometry params.
11631 * @array: array info.
11632 * @direction: reshape direction.
11634 * Obtain free space after the &array and verify if expand to requested size is
11635 * possible. If geo->size is set to %MAX_SIZE, assume that max free size is
11639 * On success %IMSM_STATUS_OK is returned, geo->size and geo->raid_disks are
11641 * On error, %IMSM_STATUS_ERROR is returned.
11643 static imsm_status_t
imsm_analyze_expand(struct supertype
*st
,
11644 struct geo_params
*geo
,
11645 struct mdinfo
*array
,
11648 struct intel_super
*super
= st
->sb
;
11649 struct imsm_dev
*dev
= get_imsm_dev(super
, super
->current_vol
);
11650 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
11651 int data_disks
= imsm_num_data_members(map
);
11653 unsigned long long current_size
;
11654 unsigned long long free_size
;
11655 unsigned long long new_size
;
11656 unsigned long long max_size
;
11658 const int chunk_kib
= geo
->chunksize
/ 1024;
11661 if (direction
== ROLLBACK_METADATA_CHANGES
) {
11663 * Accept size for rollback only.
11665 new_size
= geo
->size
* 2;
11669 if (data_disks
== 0) {
11670 pr_err("imsm: Cannot retrieve data disks.\n");
11671 return IMSM_STATUS_ERROR
;
11673 current_size
= array
->custom_array_size
/ data_disks
;
11675 rv
= imsm_get_free_size(super
, dev
->vol
.map
->num_members
, 0, chunk_kib
, &free_size
, true);
11676 if (rv
!= IMSM_STATUS_OK
) {
11677 pr_err("imsm: Cannot find free space for expand.\n");
11678 return IMSM_STATUS_ERROR
;
11680 max_size
= round_member_size_to_mb(free_size
+ current_size
);
11682 if (geo
->size
== MAX_SIZE
)
11683 new_size
= max_size
;
11685 new_size
= round_member_size_to_mb(geo
->size
* 2);
11687 if (new_size
== 0) {
11688 pr_err("imsm: Rounded requested size is 0.\n");
11689 return IMSM_STATUS_ERROR
;
11692 if (new_size
> max_size
) {
11693 pr_err("imsm: Rounded requested size (%llu) is larger than free space available (%llu).\n",
11694 new_size
, max_size
);
11695 return IMSM_STATUS_ERROR
;
11698 if (new_size
== current_size
) {
11699 pr_err("imsm: Rounded requested size (%llu) is same as current size (%llu).\n",
11700 new_size
, current_size
);
11701 return IMSM_STATUS_ERROR
;
11704 if (new_size
< current_size
) {
11705 pr_err("imsm: Size reduction is not supported, rounded requested size (%llu) is smaller than current (%llu).\n",
11706 new_size
, current_size
);
11707 return IMSM_STATUS_ERROR
;
11711 dprintf("imsm: New size per member is %llu.\n", new_size
);
11712 geo
->size
= data_disks
* new_size
;
11713 geo
->raid_disks
= dev
->vol
.map
->num_members
;
11714 return IMSM_STATUS_OK
;
11717 /***************************************************************************
11718 * Function: imsm_analyze_change
11719 * Description: Function analyze change for single volume
11720 * and validate if transition is supported
11721 * Parameters: Geometry parameters, supertype structure,
11722 * metadata change direction (apply/rollback)
11723 * Returns: Operation type code on success, -1 if fail
11724 ****************************************************************************/
11725 enum imsm_reshape_type
imsm_analyze_change(struct supertype
*st
,
11726 struct geo_params
*geo
,
11729 struct mdinfo info
;
11731 int check_devs
= 0;
11733 /* number of added/removed disks in operation result */
11734 int devNumChange
= 0;
11735 /* imsm compatible layout value for array geometry verification */
11736 int imsm_layout
= -1;
11739 getinfo_super_imsm_volume(st
, &info
, NULL
);
11740 if (geo
->level
!= info
.array
.level
&& geo
->level
>= 0 &&
11741 geo
->level
!= UnSet
) {
11742 switch (info
.array
.level
) {
11744 if (geo
->level
== 5) {
11745 change
= CH_MIGRATION
;
11746 if (geo
->layout
!= ALGORITHM_LEFT_ASYMMETRIC
) {
11747 pr_err("Error. Requested Layout not supported (left-asymmetric layout is supported only)!\n");
11749 goto analyse_change_exit
;
11751 imsm_layout
= geo
->layout
;
11753 devNumChange
= 1; /* parity disk added */
11754 } else if (geo
->level
== 10) {
11755 change
= CH_TAKEOVER
;
11757 devNumChange
= 2; /* two mirrors added */
11758 imsm_layout
= 0x102; /* imsm supported layout */
11763 if (geo
->level
== 0) {
11764 change
= CH_TAKEOVER
;
11766 devNumChange
= -(geo
->raid_disks
/2);
11767 imsm_layout
= 0; /* imsm raid0 layout */
11771 if (change
== -1) {
11772 pr_err("Error. Level Migration from %d to %d not supported!\n",
11773 info
.array
.level
, geo
->level
);
11774 goto analyse_change_exit
;
11777 geo
->level
= info
.array
.level
;
11779 if (geo
->layout
!= info
.array
.layout
&&
11780 (geo
->layout
!= UnSet
&& geo
->layout
!= -1)) {
11781 change
= CH_MIGRATION
;
11782 if (info
.array
.layout
== 0 && info
.array
.level
== 5 &&
11783 geo
->layout
== 5) {
11784 /* reshape 5 -> 4 */
11785 } else if (info
.array
.layout
== 5 && info
.array
.level
== 5 &&
11786 geo
->layout
== 0) {
11787 /* reshape 4 -> 5 */
11791 pr_err("Error. Layout Migration from %d to %d not supported!\n",
11792 info
.array
.layout
, geo
->layout
);
11794 goto analyse_change_exit
;
11797 geo
->layout
= info
.array
.layout
;
11798 if (imsm_layout
== -1)
11799 imsm_layout
= info
.array
.layout
;
11802 if (geo
->chunksize
> 0 && geo
->chunksize
!= UnSet
&&
11803 geo
->chunksize
!= info
.array
.chunk_size
) {
11804 if (info
.array
.level
== 10) {
11805 pr_err("Error. Chunk size change for RAID 10 is not supported.\n");
11807 goto analyse_change_exit
;
11808 } else if (info
.component_size
% (geo
->chunksize
/512)) {
11809 pr_err("New chunk size (%dK) does not evenly divide device size (%lluk). Aborting...\n",
11810 geo
->chunksize
/1024, info
.component_size
/2);
11812 goto analyse_change_exit
;
11814 change
= CH_MIGRATION
;
11816 geo
->chunksize
= info
.array
.chunk_size
;
11819 if (geo
->size
> 0) {
11820 if (change
!= -1) {
11821 pr_err("Error. Size change should be the only one at a time.\n");
11823 goto analyse_change_exit
;
11826 rv
= imsm_analyze_expand(st
, geo
, &info
, direction
);
11827 if (rv
!= IMSM_STATUS_OK
)
11828 goto analyse_change_exit
;
11829 change
= CH_ARRAY_SIZE
;
11832 chunk
= geo
->chunksize
/ 1024;
11833 if (!validate_geometry_imsm(st
,
11836 geo
->raid_disks
+ devNumChange
,
11838 geo
->size
, INVALID_SECTORS
,
11839 0, 0, info
.consistency_policy
, 1))
11843 struct intel_super
*super
= st
->sb
;
11844 struct imsm_super
*mpb
= super
->anchor
;
11846 if (mpb
->num_raid_devs
> 1) {
11847 pr_err("Error. Cannot perform operation on %s- for this operation "
11848 "it MUST be single array in container\n", geo
->dev_name
);
11853 analyse_change_exit
:
11854 if (direction
== ROLLBACK_METADATA_CHANGES
&&
11855 (change
== CH_MIGRATION
|| change
== CH_TAKEOVER
)) {
11856 dprintf("imsm: Metadata changes rollback is not supported for migration and takeover operations.\n");
11862 int imsm_takeover(struct supertype
*st
, struct geo_params
*geo
)
11864 struct intel_super
*super
= st
->sb
;
11865 struct imsm_update_takeover
*u
;
11867 u
= xmalloc(sizeof(struct imsm_update_takeover
));
11869 u
->type
= update_takeover
;
11870 u
->subarray
= super
->current_vol
;
11872 /* 10->0 transition */
11873 if (geo
->level
== 0)
11874 u
->direction
= R10_TO_R0
;
11876 /* 0->10 transition */
11877 if (geo
->level
== 10)
11878 u
->direction
= R0_TO_R10
;
11880 /* update metadata locally */
11881 imsm_update_metadata_locally(st
, u
,
11882 sizeof(struct imsm_update_takeover
));
11883 /* and possibly remotely */
11884 if (st
->update_tail
)
11885 append_metadata_update(st
, u
,
11886 sizeof(struct imsm_update_takeover
));
11893 /* Flush size update if size calculated by num_data_stripes is higher than
11894 * imsm_dev_size to eliminate differences during reshape.
11895 * Mdmon will recalculate them correctly.
11896 * If subarray index is not set then check whole container.
11898 * 0 - no error occurred
11899 * 1 - error detected
11901 static int imsm_fix_size_mismatch(struct supertype
*st
, int subarray_index
)
11903 struct intel_super
*super
= st
->sb
;
11904 int tmp
= super
->current_vol
;
11908 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
11909 if (subarray_index
>= 0 && i
!= subarray_index
)
11911 super
->current_vol
= i
;
11912 struct imsm_dev
*dev
= get_imsm_dev(super
, super
->current_vol
);
11913 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
11914 unsigned int disc_count
= imsm_num_data_members(map
);
11915 struct geo_params geo
;
11916 struct imsm_update_size_change
*update
;
11917 unsigned long long calc_size
= per_dev_array_size(map
) * disc_count
;
11918 unsigned long long d_size
= imsm_dev_size(dev
);
11921 if (calc_size
== d_size
)
11924 /* There is a difference, confirm that imsm_dev_size is
11925 * smaller and push update.
11927 if (d_size
> calc_size
) {
11928 pr_err("imsm: dev size of subarray %d is incorrect\n",
11932 memset(&geo
, 0, sizeof(struct geo_params
));
11934 u_size
= imsm_create_metadata_update_for_size_change(st
, &geo
,
11936 imsm_update_metadata_locally(st
, update
, u_size
);
11937 if (st
->update_tail
) {
11938 append_metadata_update(st
, update
, u_size
);
11939 flush_metadata_updates(st
);
11940 st
->update_tail
= &st
->updates
;
11942 imsm_sync_metadata(st
);
11948 super
->current_vol
= tmp
;
11952 static int imsm_reshape_super(struct supertype
*st
, unsigned long long size
,
11954 int layout
, int chunksize
, int raid_disks
,
11955 int delta_disks
, char *backup
, char *dev
,
11956 int direction
, int verbose
)
11959 struct geo_params geo
;
11961 dprintf("(enter)\n");
11963 memset(&geo
, 0, sizeof(struct geo_params
));
11965 geo
.dev_name
= dev
;
11966 strcpy(geo
.devnm
, st
->devnm
);
11969 geo
.layout
= layout
;
11970 geo
.chunksize
= chunksize
;
11971 geo
.raid_disks
= raid_disks
;
11972 if (delta_disks
!= UnSet
)
11973 geo
.raid_disks
+= delta_disks
;
11975 dprintf("for level : %i\n", geo
.level
);
11976 dprintf("for raid_disks : %i\n", geo
.raid_disks
);
11978 if (strcmp(st
->container_devnm
, st
->devnm
) == 0) {
11979 /* On container level we can only increase number of devices. */
11980 dprintf("imsm: info: Container operation\n");
11981 int old_raid_disks
= 0;
11983 if (imsm_reshape_is_allowed_on_container(
11984 st
, &geo
, &old_raid_disks
, direction
)) {
11985 struct imsm_update_reshape
*u
= NULL
;
11988 if (imsm_fix_size_mismatch(st
, -1)) {
11989 dprintf("imsm: Cannot fix size mismatch\n");
11990 goto exit_imsm_reshape_super
;
11993 len
= imsm_create_metadata_update_for_reshape(
11994 st
, &geo
, old_raid_disks
, &u
);
11997 dprintf("imsm: Cannot prepare update\n");
11998 goto exit_imsm_reshape_super
;
12002 /* update metadata locally */
12003 imsm_update_metadata_locally(st
, u
, len
);
12004 /* and possibly remotely */
12005 if (st
->update_tail
)
12006 append_metadata_update(st
, u
, len
);
12011 pr_err("(imsm) Operation is not allowed on this container\n");
12014 /* On volume level we support following operations
12015 * - takeover: raid10 -> raid0; raid0 -> raid10
12016 * - chunk size migration
12017 * - migration: raid5 -> raid0; raid0 -> raid5
12019 struct intel_super
*super
= st
->sb
;
12020 struct intel_dev
*dev
= super
->devlist
;
12022 dprintf("imsm: info: Volume operation\n");
12023 /* find requested device */
12026 imsm_find_array_devnm_by_subdev(
12027 dev
->index
, st
->container_devnm
);
12028 if (devnm
&& strcmp(devnm
, geo
.devnm
) == 0)
12033 pr_err("Cannot find %s (%s) subarray\n",
12034 geo
.dev_name
, geo
.devnm
);
12035 goto exit_imsm_reshape_super
;
12037 super
->current_vol
= dev
->index
;
12038 change
= imsm_analyze_change(st
, &geo
, direction
);
12041 ret_val
= imsm_takeover(st
, &geo
);
12043 case CH_MIGRATION
: {
12044 struct imsm_update_reshape_migration
*u
= NULL
;
12046 imsm_create_metadata_update_for_migration(
12049 dprintf("imsm: Cannot prepare update\n");
12053 /* update metadata locally */
12054 imsm_update_metadata_locally(st
, u
, len
);
12055 /* and possibly remotely */
12056 if (st
->update_tail
)
12057 append_metadata_update(st
, u
, len
);
12062 case CH_ARRAY_SIZE
: {
12063 struct imsm_update_size_change
*u
= NULL
;
12065 imsm_create_metadata_update_for_size_change(
12068 dprintf("imsm: Cannot prepare update\n");
12072 /* update metadata locally */
12073 imsm_update_metadata_locally(st
, u
, len
);
12074 /* and possibly remotely */
12075 if (st
->update_tail
)
12076 append_metadata_update(st
, u
, len
);
12086 exit_imsm_reshape_super
:
12087 dprintf("imsm: reshape_super Exit code = %i\n", ret_val
);
12091 #define COMPLETED_OK 0
12092 #define COMPLETED_NONE 1
12093 #define COMPLETED_DELAYED 2
12095 static int read_completed(int fd
, unsigned long long *val
)
12098 char buf
[SYSFS_MAX_BUF_SIZE
];
12100 ret
= sysfs_fd_get_str(fd
, buf
, sizeof(buf
));
12104 ret
= COMPLETED_OK
;
12105 if (str_is_none(buf
) == true) {
12106 ret
= COMPLETED_NONE
;
12107 } else if (strncmp(buf
, "delayed", 7) == 0) {
12108 ret
= COMPLETED_DELAYED
;
12111 *val
= strtoull(buf
, &ep
, 0);
12112 if (ep
== buf
|| (*ep
!= 0 && *ep
!= '\n' && *ep
!= ' '))
12118 /*******************************************************************************
12119 * Function: wait_for_reshape_imsm
12120 * Description: Function writes new sync_max value and waits until
12121 * reshape process reach new position
12123 * sra : general array info
12124 * ndata : number of disks in new array's layout
12127 * 1 : there is no reshape in progress,
12129 ******************************************************************************/
12130 int wait_for_reshape_imsm(struct mdinfo
*sra
, int ndata
)
12132 int fd
= sysfs_get_fd(sra
, NULL
, "sync_completed");
12134 unsigned long long completed
;
12135 /* to_complete : new sync_max position */
12136 unsigned long long to_complete
= sra
->reshape_progress
;
12137 unsigned long long position_to_set
= to_complete
/ ndata
;
12139 if (!is_fd_valid(fd
)) {
12140 dprintf("cannot open reshape_position\n");
12145 if (sysfs_fd_get_ll(fd
, &completed
) < 0) {
12147 dprintf("cannot read reshape_position (no reshape in progres)\n");
12151 sleep_for(0, MSEC_TO_NSEC(30), true);
12156 if (completed
> position_to_set
) {
12157 dprintf("wrong next position to set %llu (%llu)\n",
12158 to_complete
, position_to_set
);
12162 dprintf("Position set: %llu\n", position_to_set
);
12163 if (sysfs_set_num(sra
, NULL
, "sync_max",
12164 position_to_set
) != 0) {
12165 dprintf("cannot set reshape position to %llu\n",
12173 char action
[SYSFS_MAX_BUF_SIZE
];
12174 int timeout
= 3000;
12176 sysfs_wait(fd
, &timeout
);
12177 if (sysfs_get_str(sra
, NULL
, "sync_action",
12178 action
, sizeof(action
)) > 0 &&
12179 strncmp(action
, "reshape", 7) != 0) {
12180 if (strncmp(action
, "idle", 4) == 0)
12186 rc
= read_completed(fd
, &completed
);
12188 dprintf("cannot read reshape_position (in loop)\n");
12191 } else if (rc
== COMPLETED_NONE
)
12193 } while (completed
< position_to_set
);
12199 /*******************************************************************************
12200 * Function: check_degradation_change
12201 * Description: Check that array hasn't become failed.
12203 * info : for sysfs access
12204 * sources : source disks descriptors
12205 * degraded: previous degradation level
12207 * degradation level
12208 ******************************************************************************/
12209 int check_degradation_change(struct mdinfo
*info
,
12213 unsigned long long new_degraded
;
12216 rv
= sysfs_get_ll(info
, NULL
, "degraded", &new_degraded
);
12217 if (rv
== -1 || (new_degraded
!= (unsigned long long)degraded
)) {
12218 /* check each device to ensure it is still working */
12221 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
12222 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
12224 if (sd
->disk
.state
& (1<<MD_DISK_SYNC
)) {
12225 char sbuf
[SYSFS_MAX_BUF_SIZE
];
12226 int raid_disk
= sd
->disk
.raid_disk
;
12228 if (sysfs_get_str(info
,
12229 sd
, "state", sbuf
, sizeof(sbuf
)) < 0 ||
12230 strstr(sbuf
, "faulty") ||
12231 strstr(sbuf
, "in_sync") == NULL
) {
12232 /* this device is dead */
12233 sd
->disk
.state
= (1<<MD_DISK_FAULTY
);
12234 if (raid_disk
>= 0)
12235 close_fd(&sources
[raid_disk
]);
12242 return new_degraded
;
12245 /*******************************************************************************
12246 * Function: imsm_manage_reshape
12247 * Description: Function finds array under reshape and it manages reshape
12248 * process. It creates stripes backups (if required) and sets
12251 * afd : Backup handle (nattive) - not used
12252 * sra : general array info
12253 * reshape : reshape parameters - not used
12254 * st : supertype structure
12255 * blocks : size of critical section [blocks]
12256 * fds : table of source device descriptor
12257 * offsets : start of array (offest per devices)
12259 * destfd : table of destination device descriptor
12260 * destoffsets : table of destination offsets (per device)
12262 * 1 : success, reshape is done
12264 ******************************************************************************/
12265 static int imsm_manage_reshape(
12266 int afd
, struct mdinfo
*sra
, struct reshape
*reshape
,
12267 struct supertype
*st
, unsigned long backup_blocks
,
12268 int *fds
, unsigned long long *offsets
,
12269 int dests
, int *destfd
, unsigned long long *destoffsets
)
12272 struct intel_super
*super
= st
->sb
;
12273 struct intel_dev
*dv
;
12274 unsigned int sector_size
= super
->sector_size
;
12275 struct imsm_dev
*dev
= NULL
;
12276 struct imsm_map
*map_src
, *map_dest
;
12277 int migr_vol_qan
= 0;
12278 int ndata
, odata
; /* [bytes] */
12279 int chunk
; /* [bytes] */
12280 struct migr_record
*migr_rec
;
12282 unsigned int buf_size
; /* [bytes] */
12283 unsigned long long max_position
; /* array size [bytes] */
12284 unsigned long long next_step
; /* [blocks]/[bytes] */
12285 unsigned long long old_data_stripe_length
;
12286 unsigned long long start_src
; /* [bytes] */
12287 unsigned long long start
; /* [bytes] */
12288 unsigned long long start_buf_shift
; /* [bytes] */
12290 int source_layout
= 0;
12291 int subarray_index
= -1;
12296 if (!fds
|| !offsets
)
12299 /* Find volume during the reshape */
12300 for (dv
= super
->devlist
; dv
; dv
= dv
->next
) {
12301 if (dv
->dev
->vol
.migr_type
== MIGR_GEN_MIGR
&&
12302 dv
->dev
->vol
.migr_state
== 1) {
12305 subarray_index
= dv
->index
;
12308 /* Only one volume can migrate at the same time */
12309 if (migr_vol_qan
!= 1) {
12310 pr_err("%s", migr_vol_qan
?
12311 "Number of migrating volumes greater than 1\n" :
12312 "There is no volume during migrationg\n");
12316 map_dest
= get_imsm_map(dev
, MAP_0
);
12317 map_src
= get_imsm_map(dev
, MAP_1
);
12318 if (map_src
== NULL
)
12321 ndata
= imsm_num_data_members(map_dest
);
12322 odata
= imsm_num_data_members(map_src
);
12324 chunk
= __le16_to_cpu(map_src
->blocks_per_strip
) * 512;
12325 old_data_stripe_length
= odata
* chunk
;
12327 migr_rec
= super
->migr_rec
;
12329 /* initialize migration record for start condition */
12330 if (sra
->reshape_progress
== 0)
12331 init_migr_record_imsm(st
, dev
, sra
);
12333 if (__le32_to_cpu(migr_rec
->rec_status
) != UNIT_SRC_NORMAL
) {
12334 dprintf("imsm: cannot restart migration when data are present in copy area.\n");
12337 /* Save checkpoint to update migration record for current
12338 * reshape position (in md). It can be farther than current
12339 * reshape position in metadata.
12341 if (save_checkpoint_imsm(st
, sra
, UNIT_SRC_NORMAL
) == 1) {
12342 /* ignore error == 2, this can mean end of reshape here
12344 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL, initial save)\n");
12349 /* size for data */
12350 buf_size
= __le32_to_cpu(migr_rec
->blocks_per_unit
) * 512;
12351 /* extend buffer size for parity disk */
12352 buf_size
+= __le32_to_cpu(migr_rec
->dest_depth_per_unit
) * 512;
12353 /* add space for stripe alignment */
12354 buf_size
+= old_data_stripe_length
;
12355 if (posix_memalign((void **)&buf
, MAX_SECTOR_SIZE
, buf_size
)) {
12356 dprintf("imsm: Cannot allocate checkpoint buffer\n");
12360 max_position
= sra
->component_size
* ndata
;
12361 source_layout
= imsm_level_to_layout(map_src
->raid_level
);
12363 while (current_migr_unit(migr_rec
) <
12364 get_num_migr_units(migr_rec
)) {
12365 /* current reshape position [blocks] */
12366 unsigned long long current_position
=
12367 __le32_to_cpu(migr_rec
->blocks_per_unit
)
12368 * current_migr_unit(migr_rec
);
12369 unsigned long long border
;
12371 /* Check that array hasn't become failed.
12373 degraded
= check_degradation_change(sra
, fds
, degraded
);
12374 if (degraded
> 1) {
12375 dprintf("imsm: Abort reshape due to degradation level (%i)\n", degraded
);
12379 next_step
= __le32_to_cpu(migr_rec
->blocks_per_unit
);
12381 if ((current_position
+ next_step
) > max_position
)
12382 next_step
= max_position
- current_position
;
12384 start
= current_position
* 512;
12386 /* align reading start to old geometry */
12387 start_buf_shift
= start
% old_data_stripe_length
;
12388 start_src
= start
- start_buf_shift
;
12390 border
= (start_src
/ odata
) - (start
/ ndata
);
12392 if (border
<= __le32_to_cpu(migr_rec
->dest_depth_per_unit
)) {
12393 /* save critical stripes to buf
12394 * start - start address of current unit
12395 * to backup [bytes]
12396 * start_src - start address of current unit
12397 * to backup alligned to source array
12400 unsigned long long next_step_filler
;
12401 unsigned long long copy_length
= next_step
* 512;
12403 /* allign copy area length to stripe in old geometry */
12404 next_step_filler
= ((copy_length
+ start_buf_shift
)
12405 % old_data_stripe_length
);
12406 if (next_step_filler
)
12407 next_step_filler
= (old_data_stripe_length
12408 - next_step_filler
);
12409 dprintf("save_stripes() parameters: start = %llu,\tstart_src = %llu,\tnext_step*512 = %llu,\tstart_in_buf_shift = %llu,\tnext_step_filler = %llu\n",
12410 start
, start_src
, copy_length
,
12411 start_buf_shift
, next_step_filler
);
12413 if (save_stripes(fds
, offsets
, map_src
->num_members
,
12414 chunk
, map_src
->raid_level
,
12415 source_layout
, 0, NULL
, start_src
,
12417 next_step_filler
+ start_buf_shift
,
12419 dprintf("imsm: Cannot save stripes to buffer\n");
12422 /* Convert data to destination format and store it
12423 * in backup general migration area
12425 if (save_backup_imsm(st
, dev
, sra
,
12426 buf
+ start_buf_shift
, copy_length
)) {
12427 dprintf("imsm: Cannot save stripes to target devices\n");
12430 if (save_checkpoint_imsm(st
, sra
,
12431 UNIT_SRC_IN_CP_AREA
)) {
12432 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_IN_CP_AREA)\n");
12436 /* set next step to use whole border area */
12437 border
/= next_step
;
12439 next_step
*= border
;
12441 /* When data backed up, checkpoint stored,
12442 * kick the kernel to reshape unit of data
12444 next_step
= next_step
+ sra
->reshape_progress
;
12445 /* limit next step to array max position */
12446 if (next_step
> max_position
)
12447 next_step
= max_position
;
12448 sysfs_set_num(sra
, NULL
, "suspend_lo", sra
->reshape_progress
);
12449 sysfs_set_num(sra
, NULL
, "suspend_hi", next_step
);
12450 sra
->reshape_progress
= next_step
;
12452 /* wait until reshape finish */
12453 if (wait_for_reshape_imsm(sra
, ndata
)) {
12454 dprintf("wait_for_reshape_imsm returned error!\n");
12460 if (save_checkpoint_imsm(st
, sra
, UNIT_SRC_NORMAL
) == 1) {
12461 /* ignore error == 2, this can mean end of reshape here
12463 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL)\n");
12469 /* clear migr_rec on disks after successful migration */
12472 memset(super
->migr_rec_buf
, 0, MIGR_REC_BUF_SECTORS
*MAX_SECTOR_SIZE
);
12473 for (d
= super
->disks
; d
; d
= d
->next
) {
12474 if (d
->index
< 0 || is_failed(&d
->disk
))
12476 unsigned long long dsize
;
12478 get_dev_size(d
->fd
, NULL
, &dsize
);
12479 if (lseek64(d
->fd
, dsize
- MIGR_REC_SECTOR_POSITION
*sector_size
,
12481 if ((unsigned int)write(d
->fd
, super
->migr_rec_buf
,
12482 MIGR_REC_BUF_SECTORS
*sector_size
) !=
12483 MIGR_REC_BUF_SECTORS
*sector_size
)
12484 perror("Write migr_rec failed");
12488 /* return '1' if done */
12491 /* After the reshape eliminate size mismatch in metadata.
12492 * Don't update md/component_size here, volume hasn't
12493 * to take whole space. It is allowed by kernel.
12494 * md/component_size will be set propoperly after next assembly.
12496 imsm_fix_size_mismatch(st
, subarray_index
);
12500 /* See Grow.c: abort_reshape() for further explanation */
12501 sysfs_set_num(sra
, NULL
, "suspend_lo", 0x7FFFFFFFFFFFFFFFULL
);
12502 sysfs_set_num(sra
, NULL
, "suspend_hi", 0);
12503 sysfs_set_num(sra
, NULL
, "suspend_lo", 0);
12508 /*******************************************************************************
12509 * Function: calculate_bitmap_min_chunksize
12510 * Description: Calculates the minimal valid bitmap chunk size
12512 * max_bits : indicate how many bits can be used for the bitmap
12513 * data_area_size : the size of the data area covered by the bitmap
12516 * The bitmap chunk size
12517 ******************************************************************************/
12518 static unsigned long long
12519 calculate_bitmap_min_chunksize(unsigned long long max_bits
,
12520 unsigned long long data_area_size
)
12522 unsigned long long min_chunk
=
12523 4096; /* sub-page chunks don't work yet.. */
12524 unsigned long long bits
= data_area_size
/ min_chunk
+ 1;
12526 while (bits
> max_bits
) {
12528 bits
= (bits
+ 1) / 2;
12533 /*******************************************************************************
12534 * Function: calculate_bitmap_chunksize
12535 * Description: Calculates the bitmap chunk size for the given device
12537 * st : supertype information
12538 * dev : device for the bitmap
12541 * The bitmap chunk size
12542 ******************************************************************************/
12543 static unsigned long long calculate_bitmap_chunksize(struct supertype
*st
,
12544 struct imsm_dev
*dev
)
12546 struct intel_super
*super
= st
->sb
;
12547 unsigned long long min_chunksize
;
12548 unsigned long long result
= IMSM_DEFAULT_BITMAP_CHUNKSIZE
;
12549 size_t dev_size
= imsm_dev_size(dev
);
12551 min_chunksize
= calculate_bitmap_min_chunksize(
12552 IMSM_BITMAP_AREA_SIZE
* super
->sector_size
, dev_size
);
12554 if (result
< min_chunksize
)
12555 result
= min_chunksize
;
12560 /*******************************************************************************
12561 * Function: init_bitmap_header
12562 * Description: Initialize the bitmap header structure
12564 * st : supertype information
12565 * bms : bitmap header struct to initialize
12566 * dev : device for the bitmap
12571 ******************************************************************************/
12572 static int init_bitmap_header(struct supertype
*st
, struct bitmap_super_s
*bms
,
12573 struct imsm_dev
*dev
)
12580 bms
->magic
= __cpu_to_le32(BITMAP_MAGIC
);
12581 bms
->version
= __cpu_to_le32(BITMAP_MAJOR_HI
);
12582 bms
->daemon_sleep
= __cpu_to_le32(IMSM_DEFAULT_BITMAP_DAEMON_SLEEP
);
12583 bms
->sync_size
= __cpu_to_le64(IMSM_BITMAP_AREA_SIZE
);
12584 bms
->write_behind
= __cpu_to_le32(0);
12586 uuid_from_super_imsm(st
, vol_uuid
);
12587 memcpy(bms
->uuid
, vol_uuid
, 16);
12589 bms
->chunksize
= calculate_bitmap_chunksize(st
, dev
);
12594 /*******************************************************************************
12595 * Function: validate_internal_bitmap_for_drive
12596 * Description: Verify if the bitmap header for a given drive.
12598 * st : supertype information
12599 * offset : The offset from the beginning of the drive where to look for
12600 * the bitmap header.
12601 * d : the drive info
12606 ******************************************************************************/
12607 static int validate_internal_bitmap_for_drive(struct supertype
*st
,
12608 unsigned long long offset
,
12611 struct intel_super
*super
= st
->sb
;
12614 bitmap_super_t
*bms
;
12622 if (posix_memalign(&read_buf
, MAX_SECTOR_SIZE
, IMSM_BITMAP_HEADER_SIZE
))
12626 if (!is_fd_valid(fd
)) {
12627 fd
= open(d
->devname
, O_RDONLY
, 0);
12629 if (!is_fd_valid(fd
)) {
12630 dprintf("cannot open the device %s\n", d
->devname
);
12635 if (lseek64(fd
, offset
* super
->sector_size
, SEEK_SET
) < 0)
12637 if (read(fd
, read_buf
, IMSM_BITMAP_HEADER_SIZE
) !=
12638 IMSM_BITMAP_HEADER_SIZE
)
12641 uuid_from_super_imsm(st
, vol_uuid
);
12644 if ((bms
->magic
!= __cpu_to_le32(BITMAP_MAGIC
)) ||
12645 (bms
->version
!= __cpu_to_le32(BITMAP_MAJOR_HI
)) ||
12646 (!same_uuid((int *)bms
->uuid
, vol_uuid
, st
->ss
->swapuuid
))) {
12647 dprintf("wrong bitmap header detected\n");
12653 if (!is_fd_valid(d
->fd
))
12662 /*******************************************************************************
12663 * Function: validate_internal_bitmap_imsm
12664 * Description: Verify if the bitmap header is in place and with proper data.
12666 * st : supertype information
12669 * 0 : success or device w/o RWH_BITMAP
12671 ******************************************************************************/
12672 static int validate_internal_bitmap_imsm(struct supertype
*st
)
12674 struct intel_super
*super
= st
->sb
;
12675 struct imsm_dev
*dev
= get_imsm_dev(super
, super
->current_vol
);
12676 unsigned long long offset
;
12679 if (dev
->rwh_policy
!= RWH_BITMAP
)
12682 offset
= get_bitmap_header_sector(super
, super
->current_vol
);
12683 for (d
= super
->disks
; d
; d
= d
->next
) {
12684 if (d
->index
< 0 || is_failed(&d
->disk
))
12687 if (validate_internal_bitmap_for_drive(st
, offset
, d
)) {
12688 pr_err("imsm: bitmap validation failed\n");
12695 /*******************************************************************************
12696 * Function: add_internal_bitmap_imsm
12697 * Description: Mark the volume to use the bitmap and updates the chunk size value.
12699 * st : supertype information
12700 * chunkp : bitmap chunk size
12701 * delay : not used for imsm
12702 * write_behind : not used for imsm
12703 * size : not used for imsm
12704 * may_change : not used for imsm
12705 * amajor : not used for imsm
12710 ******************************************************************************/
12711 static int add_internal_bitmap_imsm(struct supertype
*st
, int *chunkp
,
12712 int delay
, int write_behind
,
12713 unsigned long long size
, int may_change
,
12716 struct intel_super
*super
= st
->sb
;
12717 int vol_idx
= super
->current_vol
;
12718 struct imsm_dev
*dev
;
12720 if (!super
->devlist
|| vol_idx
== -1 || !chunkp
)
12723 dev
= get_imsm_dev(super
, vol_idx
);
12724 dev
->rwh_policy
= RWH_BITMAP
;
12725 *chunkp
= calculate_bitmap_chunksize(st
, dev
);
12729 /*******************************************************************************
12730 * Function: locate_bitmap_imsm
12731 * Description: Seek 'fd' to start of write-intent-bitmap.
12733 * st : supertype information
12734 * fd : file descriptor for the device
12735 * node_num : not used for imsm
12740 ******************************************************************************/
12741 static int locate_bitmap_imsm(struct supertype
*st
, int fd
, int node_num
)
12743 struct intel_super
*super
= st
->sb
;
12744 unsigned long long offset
;
12745 int vol_idx
= super
->current_vol
;
12747 if (!super
->devlist
|| vol_idx
== -1)
12750 offset
= get_bitmap_header_sector(super
, super
->current_vol
);
12751 dprintf("bitmap header offset is %llu\n", offset
);
12753 lseek64(fd
, offset
<< 9, 0);
12758 /*******************************************************************************
12759 * Function: write_init_bitmap_imsm
12760 * Description: Write a bitmap header and prepares the area for the bitmap.
12762 * st : supertype information
12763 * fd : file descriptor for the device
12764 * update : not used for imsm
12769 ******************************************************************************/
12770 static int write_init_bitmap_imsm(struct supertype
*st
, int fd
,
12771 enum bitmap_update update
)
12773 struct intel_super
*super
= st
->sb
;
12774 int vol_idx
= super
->current_vol
;
12776 unsigned long long offset
;
12777 bitmap_super_t bms
= { 0 };
12778 size_t written
= 0;
12783 if (!super
->devlist
|| !super
->sector_size
|| vol_idx
== -1)
12786 struct imsm_dev
*dev
= get_imsm_dev(super
, vol_idx
);
12788 /* first clear the space for bitmap header */
12789 unsigned long long bitmap_area_start
=
12790 get_bitmap_header_sector(super
, vol_idx
);
12792 dprintf("zeroing area start (%llu) and size (%u)\n", bitmap_area_start
,
12793 IMSM_BITMAP_AND_HEADER_SIZE
/ super
->sector_size
);
12794 if (zero_disk_range(fd
, bitmap_area_start
,
12795 IMSM_BITMAP_HEADER_SIZE
/ super
->sector_size
)) {
12796 pr_err("imsm: cannot zeroing the space for the bitmap\n");
12800 /* The bitmap area should be filled with "1"s to perform initial
12803 if (posix_memalign(&buf
, MAX_SECTOR_SIZE
, MAX_SECTOR_SIZE
))
12805 memset(buf
, 0xFF, MAX_SECTOR_SIZE
);
12806 offset
= get_bitmap_sector(super
, vol_idx
);
12807 lseek64(fd
, offset
<< 9, 0);
12808 while (written
< IMSM_BITMAP_AREA_SIZE
) {
12809 to_write
= IMSM_BITMAP_AREA_SIZE
- written
;
12810 if (to_write
> MAX_SECTOR_SIZE
)
12811 to_write
= MAX_SECTOR_SIZE
;
12812 rv_num
= write(fd
, buf
, MAX_SECTOR_SIZE
);
12813 if (rv_num
!= MAX_SECTOR_SIZE
) {
12815 dprintf("cannot initialize bitmap area\n");
12821 /* write a bitmap header */
12822 init_bitmap_header(st
, &bms
, dev
);
12823 memset(buf
, 0, MAX_SECTOR_SIZE
);
12824 memcpy(buf
, &bms
, sizeof(bitmap_super_t
));
12825 if (locate_bitmap_imsm(st
, fd
, 0)) {
12827 dprintf("cannot locate the bitmap\n");
12830 if (write(fd
, buf
, MAX_SECTOR_SIZE
) != MAX_SECTOR_SIZE
) {
12832 dprintf("cannot write the bitmap header\n");
12843 /*******************************************************************************
12844 * Function: is_vol_to_setup_bitmap
12845 * Description: Checks if a bitmap should be activated on the dev.
12847 * info : info about the volume to setup the bitmap
12848 * dev : the device to check against bitmap creation
12851 * 0 : bitmap should be set up on the device
12853 ******************************************************************************/
12854 static int is_vol_to_setup_bitmap(struct mdinfo
*info
, struct imsm_dev
*dev
)
12859 if ((strcmp((char *)dev
->volume
, info
->name
) == 0) &&
12860 (dev
->rwh_policy
== RWH_BITMAP
))
12866 /*******************************************************************************
12867 * Function: set_bitmap_sysfs
12868 * Description: Set the sysfs atributes of a given volume to activate the bitmap.
12870 * info : info about the volume where the bitmap should be setup
12871 * chunksize : bitmap chunk size
12872 * location : location of the bitmap
12877 ******************************************************************************/
12878 static int set_bitmap_sysfs(struct mdinfo
*info
, unsigned long long chunksize
,
12881 /* The bitmap/metadata is set to external to allow changing of value for
12882 * bitmap/location. When external is used, the kernel will treat an offset
12883 * related to the device's first lba (in opposition to the "internal" case
12884 * when this value is related to the beginning of the superblock).
12886 if (sysfs_set_str(info
, NULL
, "bitmap/metadata", "external")) {
12887 dprintf("failed to set bitmap/metadata\n");
12891 /* It can only be changed when no bitmap is active.
12892 * Should be bigger than 512 and must be power of 2.
12893 * It is expecting the value in bytes.
12895 if (sysfs_set_num(info
, NULL
, "bitmap/chunksize",
12896 __cpu_to_le32(chunksize
))) {
12897 dprintf("failed to set bitmap/chunksize\n");
12901 /* It is expecting the value in sectors. */
12902 if (sysfs_set_num(info
, NULL
, "bitmap/space",
12903 __cpu_to_le64(IMSM_BITMAP_AREA_SIZE
))) {
12904 dprintf("failed to set bitmap/space\n");
12908 /* Determines the delay between the bitmap updates.
12909 * It is expecting the value in seconds.
12911 if (sysfs_set_num(info
, NULL
, "bitmap/time_base",
12912 __cpu_to_le64(IMSM_DEFAULT_BITMAP_DAEMON_SLEEP
))) {
12913 dprintf("failed to set bitmap/time_base\n");
12917 /* It is expecting the value in sectors with a sign at the beginning. */
12918 if (sysfs_set_str(info
, NULL
, "bitmap/location", location
)) {
12919 dprintf("failed to set bitmap/location\n");
12926 /*******************************************************************************
12927 * Function: set_bitmap_imsm
12928 * Description: Setup the bitmap for the given volume
12930 * st : supertype information
12931 * info : info about the volume where the bitmap should be setup
12936 ******************************************************************************/
12937 static int set_bitmap_imsm(struct supertype
*st
, struct mdinfo
*info
)
12939 struct intel_super
*super
= st
->sb
;
12940 int prev_current_vol
= super
->current_vol
;
12941 struct imsm_dev
*dev
;
12943 char location
[16] = "";
12944 unsigned long long chunksize
;
12945 struct intel_dev
*dev_it
;
12947 for (dev_it
= super
->devlist
; dev_it
; dev_it
= dev_it
->next
) {
12948 super
->current_vol
= dev_it
->index
;
12949 dev
= get_imsm_dev(super
, super
->current_vol
);
12951 if (is_vol_to_setup_bitmap(info
, dev
)) {
12952 if (validate_internal_bitmap_imsm(st
)) {
12953 dprintf("bitmap header validation failed\n");
12957 chunksize
= calculate_bitmap_chunksize(st
, dev
);
12958 dprintf("chunk size is %llu\n", chunksize
);
12960 snprintf(location
, sizeof(location
), "+%llu",
12961 get_bitmap_sector(super
, super
->current_vol
));
12962 dprintf("bitmap offset is %s\n", location
);
12964 if (set_bitmap_sysfs(info
, chunksize
, location
)) {
12965 dprintf("cannot setup the bitmap\n");
12972 super
->current_vol
= prev_current_vol
;
12976 struct superswitch super_imsm
= {
12977 .examine_super
= examine_super_imsm
,
12978 .brief_examine_super
= brief_examine_super_imsm
,
12979 .brief_examine_subarrays
= brief_examine_subarrays_imsm
,
12980 .export_examine_super
= export_examine_super_imsm
,
12981 .detail_super
= detail_super_imsm
,
12982 .brief_detail_super
= brief_detail_super_imsm
,
12983 .write_init_super
= write_init_super_imsm
,
12984 .validate_geometry
= validate_geometry_imsm
,
12985 .add_to_super
= add_to_super_imsm
,
12986 .remove_from_super
= remove_from_super_imsm
,
12987 .detail_platform
= detail_platform_imsm
,
12988 .export_detail_platform
= export_detail_platform_imsm
,
12989 .kill_subarray
= kill_subarray_imsm
,
12990 .update_subarray
= update_subarray_imsm
,
12991 .load_container
= load_container_imsm
,
12992 .default_geometry
= default_geometry_imsm
,
12993 .get_disk_controller_domain
= imsm_get_disk_controller_domain
,
12994 .reshape_super
= imsm_reshape_super
,
12995 .manage_reshape
= imsm_manage_reshape
,
12996 .recover_backup
= recover_backup_imsm
,
12997 .examine_badblocks
= examine_badblocks_imsm
,
12998 .match_home
= match_home_imsm
,
12999 .uuid_from_super
= uuid_from_super_imsm
,
13000 .getinfo_super
= getinfo_super_imsm
,
13001 .getinfo_super_disks
= getinfo_super_disks_imsm
,
13002 .update_super
= update_super_imsm
,
13004 .avail_size
= avail_size_imsm
,
13005 .get_spare_criteria
= get_spare_criteria_imsm
,
13007 .compare_super
= compare_super_imsm
,
13009 .load_super
= load_super_imsm
,
13010 .init_super
= init_super_imsm
,
13011 .store_super
= store_super_imsm
,
13012 .free_super
= free_super_imsm
,
13013 .match_metadata_desc
= match_metadata_desc_imsm
,
13014 .container_content
= container_content_imsm
,
13015 .validate_container
= validate_container_imsm
,
13017 .add_internal_bitmap
= add_internal_bitmap_imsm
,
13018 .locate_bitmap
= locate_bitmap_imsm
,
13019 .write_bitmap
= write_init_bitmap_imsm
,
13020 .set_bitmap
= set_bitmap_imsm
,
13022 .write_init_ppl
= write_init_ppl_imsm
,
13023 .validate_ppl
= validate_ppl_imsm
,
13029 .open_new
= imsm_open_new
,
13030 .set_array_state
= imsm_set_array_state
,
13031 .set_disk
= imsm_set_disk
,
13032 .sync_metadata
= imsm_sync_metadata
,
13033 .activate_spare
= imsm_activate_spare
,
13034 .process_update
= imsm_process_update
,
13035 .prepare_update
= imsm_prepare_update
,
13036 .record_bad_block
= imsm_record_badblock
,
13037 .clear_bad_block
= imsm_clear_badblock
,
13038 .get_bad_blocks
= imsm_get_badblocks
,