2 * mdadm - Intel(R) Matrix Storage Manager Support
4 * Copyright (C) 2002-2008 Intel Corporation
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
20 #define HAVE_STDINT_H 1
24 #include "platform-intel.h"
30 /* MPB == Metadata Parameter Block */
31 #define MPB_SIGNATURE "Intel Raid ISM Cfg Sig. "
32 #define MPB_SIG_LEN (strlen(MPB_SIGNATURE))
33 #define MPB_VERSION_RAID0 "1.0.00"
34 #define MPB_VERSION_RAID1 "1.1.00"
35 #define MPB_VERSION_MANY_VOLUMES_PER_ARRAY "1.2.00"
36 #define MPB_VERSION_3OR4_DISK_ARRAY "1.2.01"
37 #define MPB_VERSION_RAID5 "1.2.02"
38 #define MPB_VERSION_5OR6_DISK_ARRAY "1.2.04"
39 #define MPB_VERSION_CNG "1.2.06"
40 #define MPB_VERSION_ATTRIBS "1.3.00"
41 #define MAX_SIGNATURE_LENGTH 32
42 #define MAX_RAID_SERIAL_LEN 16
45 #define MPB_ATTRIB_RAID0 __cpu_to_le32(0x00000001)
47 #define MPB_ATTRIB_RAID1 __cpu_to_le32(0x00000002)
49 #define MPB_ATTRIB_RAID10 __cpu_to_le32(0x00000004)
51 #define MPB_ATTRIB_RAID1E __cpu_to_le32(0x00000008)
53 #define MPB_ATTRIB_RAID5 __cpu_to_le32(0x00000010)
54 /* supports RAID CNG */
55 #define MPB_ATTRIB_RAIDCNG __cpu_to_le32(0x00000020)
56 /* supports expanded stripe sizes of 256K, 512K and 1MB */
57 #define MPB_ATTRIB_EXP_STRIPE_SIZE __cpu_to_le32(0x00000040)
59 /* The OROM Support RST Caching of Volumes */
60 #define MPB_ATTRIB_NVM __cpu_to_le32(0x02000000)
61 /* The OROM supports creating disks greater than 2TB */
62 #define MPB_ATTRIB_2TB_DISK __cpu_to_le32(0x04000000)
63 /* The OROM supports Bad Block Management */
64 #define MPB_ATTRIB_BBM __cpu_to_le32(0x08000000)
66 /* THe OROM Supports NVM Caching of Volumes */
67 #define MPB_ATTRIB_NEVER_USE2 __cpu_to_le32(0x10000000)
68 /* The OROM supports creating volumes greater than 2TB */
69 #define MPB_ATTRIB_2TB __cpu_to_le32(0x20000000)
70 /* originally for PMP, now it's wasted b/c. Never use this bit! */
71 #define MPB_ATTRIB_NEVER_USE __cpu_to_le32(0x40000000)
72 /* Verify MPB contents against checksum after reading MPB */
73 #define MPB_ATTRIB_CHECKSUM_VERIFY __cpu_to_le32(0x80000000)
75 /* Define all supported attributes that have to be accepted by mdadm
77 #define MPB_ATTRIB_SUPPORTED (MPB_ATTRIB_CHECKSUM_VERIFY | \
79 MPB_ATTRIB_2TB_DISK | \
84 MPB_ATTRIB_EXP_STRIPE_SIZE | \
87 /* Define attributes that are unused but not harmful */
88 #define MPB_ATTRIB_IGNORED (MPB_ATTRIB_NEVER_USE)
90 #define MPB_SECTOR_CNT 2210
91 #define IMSM_RESERVED_SECTORS 8192
92 #define NUM_BLOCKS_DIRTY_STRIPE_REGION 2048
93 #define SECT_PER_MB_SHIFT 11
94 #define MAX_SECTOR_SIZE 4096
95 #define MULTIPLE_PPL_AREA_SIZE_IMSM (1024 * 1024) /* Size of the whole
99 /* Disk configuration info. */
100 #define IMSM_MAX_DEVICES 255
102 __u8 serial
[MAX_RAID_SERIAL_LEN
];/* 0xD8 - 0xE7 ascii serial number */
103 __u32 total_blocks_lo
; /* 0xE8 - 0xEB total blocks lo */
104 __u32 scsi_id
; /* 0xEC - 0xEF scsi ID */
105 #define SPARE_DISK __cpu_to_le32(0x01) /* Spare */
106 #define CONFIGURED_DISK __cpu_to_le32(0x02) /* Member of some RaidDev */
107 #define FAILED_DISK __cpu_to_le32(0x04) /* Permanent failure */
108 #define JOURNAL_DISK __cpu_to_le32(0x2000000) /* Device marked as Journaling Drive */
109 __u32 status
; /* 0xF0 - 0xF3 */
110 __u32 owner_cfg_num
; /* which config 0,1,2... owns this disk */
111 __u32 total_blocks_hi
; /* 0xF4 - 0xF5 total blocks hi */
112 #define IMSM_DISK_FILLERS 3
113 __u32 filler
[IMSM_DISK_FILLERS
]; /* 0xF5 - 0x107 MPB_DISK_FILLERS for future expansion */
116 /* map selector for map managment
122 /* RAID map configuration infos. */
124 __u32 pba_of_lba0_lo
; /* start address of partition */
125 __u32 blocks_per_member_lo
;/* blocks per member */
126 __u32 num_data_stripes_lo
; /* number of data stripes */
127 __u16 blocks_per_strip
;
128 __u8 map_state
; /* Normal, Uninitialized, Degraded, Failed */
129 #define IMSM_T_STATE_NORMAL 0
130 #define IMSM_T_STATE_UNINITIALIZED 1
131 #define IMSM_T_STATE_DEGRADED 2
132 #define IMSM_T_STATE_FAILED 3
134 #define IMSM_T_RAID0 0
135 #define IMSM_T_RAID1 1
136 #define IMSM_T_RAID5 5 /* since metadata version 1.2.02 ? */
137 __u8 num_members
; /* number of member disks */
138 __u8 num_domains
; /* number of parity domains */
139 __u8 failed_disk_num
; /* valid only when state is degraded */
141 __u32 pba_of_lba0_hi
;
142 __u32 blocks_per_member_hi
;
143 __u32 num_data_stripes_hi
;
144 __u32 filler
[4]; /* expansion area */
145 #define IMSM_ORD_REBUILD (1 << 24)
146 __u32 disk_ord_tbl
[1]; /* disk_ord_tbl[num_members],
147 * top byte contains some flags
149 } __attribute__ ((packed
));
152 __u32 curr_migr_unit
;
153 __u32 checkpoint_id
; /* id to access curr_migr_unit */
154 __u8 migr_state
; /* Normal or Migrating */
156 #define MIGR_REBUILD 1
157 #define MIGR_VERIFY 2 /* analagous to echo check > sync_action */
158 #define MIGR_GEN_MIGR 3
159 #define MIGR_STATE_CHANGE 4
160 #define MIGR_REPAIR 5
161 __u8 migr_type
; /* Initializing, Rebuilding, ... */
162 #define RAIDVOL_CLEAN 0
163 #define RAIDVOL_DIRTY 1
164 #define RAIDVOL_DSRECORD_VALID 2
166 __u8 fs_state
; /* fast-sync state for CnG (0xff == disabled) */
167 __u16 verify_errors
; /* number of mismatches */
168 __u16 bad_blocks
; /* number of bad blocks during verify */
170 struct imsm_map map
[1];
171 /* here comes another one if migr_state */
172 } __attribute__ ((packed
));
175 __u8 volume
[MAX_RAID_SERIAL_LEN
];
178 #define DEV_BOOTABLE __cpu_to_le32(0x01)
179 #define DEV_BOOT_DEVICE __cpu_to_le32(0x02)
180 #define DEV_READ_COALESCING __cpu_to_le32(0x04)
181 #define DEV_WRITE_COALESCING __cpu_to_le32(0x08)
182 #define DEV_LAST_SHUTDOWN_DIRTY __cpu_to_le32(0x10)
183 #define DEV_HIDDEN_AT_BOOT __cpu_to_le32(0x20)
184 #define DEV_CURRENTLY_HIDDEN __cpu_to_le32(0x40)
185 #define DEV_VERIFY_AND_FIX __cpu_to_le32(0x80)
186 #define DEV_MAP_STATE_UNINIT __cpu_to_le32(0x100)
187 #define DEV_NO_AUTO_RECOVERY __cpu_to_le32(0x200)
188 #define DEV_CLONE_N_GO __cpu_to_le32(0x400)
189 #define DEV_CLONE_MAN_SYNC __cpu_to_le32(0x800)
190 #define DEV_CNG_MASTER_DISK_NUM __cpu_to_le32(0x1000)
191 __u32 status
; /* Persistent RaidDev status */
192 __u32 reserved_blocks
; /* Reserved blocks at beginning of volume */
196 __u8 cng_master_disk
;
200 __u16 my_vol_raid_dev_num
; /* Used in Unique volume Id for this RaidDev */
206 /* Unique Volume Id of the NvCache Volume associated with this volume */
207 __u32 nvc_vol_orig_family_num
;
208 __u16 nvc_vol_raid_dev_num
;
211 #define RWH_DISTRIBUTED 1
212 #define RWH_JOURNALING_DRIVE 2
213 #define RWH_MULTIPLE_DISTRIBUTED 3
214 #define RWH_MULTIPLE_PPLS_JOURNALING_DRIVE 4
215 #define RWH_MULTIPLE_OFF 5
216 __u8 rwh_policy
; /* Raid Write Hole Policy */
217 __u8 jd_serial
[MAX_RAID_SERIAL_LEN
]; /* Journal Drive serial number */
220 #define IMSM_DEV_FILLERS 3
221 __u32 filler
[IMSM_DEV_FILLERS
];
223 } __attribute__ ((packed
));
226 __u8 sig
[MAX_SIGNATURE_LENGTH
]; /* 0x00 - 0x1F */
227 __u32 check_sum
; /* 0x20 - 0x23 MPB Checksum */
228 __u32 mpb_size
; /* 0x24 - 0x27 Size of MPB */
229 __u32 family_num
; /* 0x28 - 0x2B Checksum from first time this config was written */
230 __u32 generation_num
; /* 0x2C - 0x2F Incremented each time this array's MPB is written */
231 __u32 error_log_size
; /* 0x30 - 0x33 in bytes */
232 __u32 attributes
; /* 0x34 - 0x37 */
233 __u8 num_disks
; /* 0x38 Number of configured disks */
234 __u8 num_raid_devs
; /* 0x39 Number of configured volumes */
235 __u8 error_log_pos
; /* 0x3A */
236 __u8 fill
[1]; /* 0x3B */
237 __u32 cache_size
; /* 0x3c - 0x40 in mb */
238 __u32 orig_family_num
; /* 0x40 - 0x43 original family num */
239 __u32 pwr_cycle_count
; /* 0x44 - 0x47 simulated power cycle count for array */
240 __u32 bbm_log_size
; /* 0x48 - 0x4B - size of bad Block Mgmt Log in bytes */
241 __u16 num_raid_devs_created
; /* 0x4C - 0x4D Used for generating unique
242 * volume IDs for raid_dev created in this array
245 __u16 filler1
; /* 0x4E - 0x4F */
246 #define IMSM_FILLERS 34
247 __u32 filler
[IMSM_FILLERS
]; /* 0x50 - 0xD7 RAID_MPB_FILLERS */
248 struct imsm_disk disk
[1]; /* 0xD8 diskTbl[numDisks] */
249 /* here comes imsm_dev[num_raid_devs] */
250 /* here comes BBM logs */
251 } __attribute__ ((packed
));
253 #define BBM_LOG_MAX_ENTRIES 254
254 #define BBM_LOG_MAX_LBA_ENTRY_VAL 256 /* Represents 256 LBAs */
255 #define BBM_LOG_SIGNATURE 0xabadb10c
257 struct bbm_log_block_addr
{
260 } __attribute__ ((__packed__
));
262 struct bbm_log_entry
{
263 __u8 marked_count
; /* Number of blocks marked - 1 */
264 __u8 disk_ordinal
; /* Disk entry within the imsm_super */
265 struct bbm_log_block_addr defective_block_start
;
266 } __attribute__ ((__packed__
));
269 __u32 signature
; /* 0xABADB10C */
271 struct bbm_log_entry marked_block_entries
[BBM_LOG_MAX_ENTRIES
];
272 } __attribute__ ((__packed__
));
274 static char *map_state_str
[] = { "normal", "uninitialized", "degraded", "failed" };
276 #define BLOCKS_PER_KB (1024/512)
278 #define RAID_DISK_RESERVED_BLOCKS_IMSM_HI 2209
280 #define GEN_MIGR_AREA_SIZE 2048 /* General Migration Copy Area size in blocks */
282 #define MIGR_REC_BUF_SECTORS 1 /* size of migr_record i/o buffer in sectors */
283 #define MIGR_REC_SECTOR_POSITION 1 /* migr_record position offset on disk,
284 * MIGR_REC_BUF_SECTORS <= MIGR_REC_SECTOR_POS
287 #define UNIT_SRC_NORMAL 0 /* Source data for curr_migr_unit must
288 * be recovered using srcMap */
289 #define UNIT_SRC_IN_CP_AREA 1 /* Source data for curr_migr_unit has
290 * already been migrated and must
291 * be recovered from checkpoint area */
293 #define PPL_ENTRY_SPACE (128 * 1024) /* Size of single PPL, without the header */
296 __u32 rec_status
; /* Status used to determine how to restart
297 * migration in case it aborts
299 __u32 curr_migr_unit_lo
; /* 0..numMigrUnits-1 */
300 __u32 family_num
; /* Family number of MPB
301 * containing the RaidDev
302 * that is migrating */
303 __u32 ascending_migr
; /* True if migrating in increasing
305 __u32 blocks_per_unit
; /* Num disk blocks per unit of operation */
306 __u32 dest_depth_per_unit
; /* Num member blocks each destMap
308 * advances per unit-of-operation */
309 __u32 ckpt_area_pba_lo
; /* Pba of first block of ckpt copy area */
310 __u32 dest_1st_member_lba_lo
; /* First member lba on first
311 * stripe of destination */
312 __u32 num_migr_units_lo
; /* Total num migration units-of-op */
313 __u32 post_migr_vol_cap
; /* Size of volume after
314 * migration completes */
315 __u32 post_migr_vol_cap_hi
; /* Expansion space for LBA64 */
316 __u32 ckpt_read_disk_num
; /* Which member disk in destSubMap[0] the
317 * migration ckpt record was read from
318 * (for recovered migrations) */
319 __u32 curr_migr_unit_hi
; /* 0..numMigrUnits-1 high order 32 bits */
320 __u32 ckpt_area_pba_hi
; /* Pba of first block of ckpt copy area
321 * high order 32 bits */
322 __u32 dest_1st_member_lba_hi
; /* First member lba on first stripe of
323 * destination - high order 32 bits */
324 __u32 num_migr_units_hi
; /* Total num migration units-of-op
325 * high order 32 bits */
326 } __attribute__ ((__packed__
));
331 * 2: metadata does not match
339 struct md_list
*next
;
342 #define pr_vrb(fmt, arg...) (void) (verbose && pr_err(fmt, ##arg))
344 static __u8
migr_type(struct imsm_dev
*dev
)
346 if (dev
->vol
.migr_type
== MIGR_VERIFY
&&
347 dev
->status
& DEV_VERIFY_AND_FIX
)
350 return dev
->vol
.migr_type
;
353 static void set_migr_type(struct imsm_dev
*dev
, __u8 migr_type
)
355 /* for compatibility with older oroms convert MIGR_REPAIR, into
356 * MIGR_VERIFY w/ DEV_VERIFY_AND_FIX status
358 if (migr_type
== MIGR_REPAIR
) {
359 dev
->vol
.migr_type
= MIGR_VERIFY
;
360 dev
->status
|= DEV_VERIFY_AND_FIX
;
362 dev
->vol
.migr_type
= migr_type
;
363 dev
->status
&= ~DEV_VERIFY_AND_FIX
;
367 static unsigned int sector_count(__u32 bytes
, unsigned int sector_size
)
369 return ROUND_UP(bytes
, sector_size
) / sector_size
;
372 static unsigned int mpb_sectors(struct imsm_super
*mpb
,
373 unsigned int sector_size
)
375 return sector_count(__le32_to_cpu(mpb
->mpb_size
), sector_size
);
379 struct imsm_dev
*dev
;
380 struct intel_dev
*next
;
385 enum sys_dev_type type
;
388 struct intel_hba
*next
;
395 /* internal representation of IMSM metadata */
398 void *buf
; /* O_DIRECT buffer for reading/writing metadata */
399 struct imsm_super
*anchor
; /* immovable parameters */
402 void *migr_rec_buf
; /* buffer for I/O operations */
403 struct migr_record
*migr_rec
; /* migration record */
405 int clean_migration_record_by_mdmon
; /* when reshape is switched to next
406 array, it indicates that mdmon is allowed to clean migration
408 size_t len
; /* size of the 'buf' allocation */
409 size_t extra_space
; /* extra space in 'buf' that is not used yet */
410 void *next_buf
; /* for realloc'ing buf from the manager */
412 int updates_pending
; /* count of pending updates for mdmon */
413 int current_vol
; /* index of raid device undergoing creation */
414 unsigned long long create_offset
; /* common start for 'current_vol' */
415 __u32 random
; /* random data for seeding new family numbers */
416 struct intel_dev
*devlist
;
417 unsigned int sector_size
; /* sector size of used member drives */
421 __u8 serial
[MAX_RAID_SERIAL_LEN
];
424 struct imsm_disk disk
;
427 struct extent
*e
; /* for determining freespace @ create */
428 int raiddisk
; /* slot to fill in autolayout */
430 } *disks
, *current_disk
;
431 struct dl
*disk_mgmt_list
; /* list of disks to add/remove while mdmon
433 struct dl
*missing
; /* disks removed while we weren't looking */
434 struct bbm_log
*bbm_log
;
435 struct intel_hba
*hba
; /* device path of the raid controller for this metadata */
436 const struct imsm_orom
*orom
; /* platform firmware support */
437 struct intel_super
*next
; /* (temp) list for disambiguating family_num */
438 struct md_bb bb
; /* memory for get_bad_blocks call */
442 struct imsm_disk disk
;
443 #define IMSM_UNKNOWN_OWNER (-1)
445 struct intel_disk
*next
;
449 unsigned long long start
, size
;
452 /* definitions of reshape process types */
453 enum imsm_reshape_type
{
459 /* definition of messages passed to imsm_process_update */
460 enum imsm_update_type
{
461 update_activate_spare
,
465 update_add_remove_disk
,
466 update_reshape_container_disks
,
467 update_reshape_migration
,
469 update_general_migration_checkpoint
,
471 update_prealloc_badblocks_mem
,
475 struct imsm_update_activate_spare
{
476 enum imsm_update_type type
;
480 struct imsm_update_activate_spare
*next
;
486 unsigned long long size
;
493 enum takeover_direction
{
497 struct imsm_update_takeover
{
498 enum imsm_update_type type
;
500 enum takeover_direction direction
;
503 struct imsm_update_reshape
{
504 enum imsm_update_type type
;
508 int new_disks
[1]; /* new_raid_disks - old_raid_disks makedev number */
511 struct imsm_update_reshape_migration
{
512 enum imsm_update_type type
;
515 /* fields for array migration changes
522 int new_disks
[1]; /* new_raid_disks - old_raid_disks makedev number */
525 struct imsm_update_size_change
{
526 enum imsm_update_type type
;
531 struct imsm_update_general_migration_checkpoint
{
532 enum imsm_update_type type
;
533 __u32 curr_migr_unit
;
537 __u8 serial
[MAX_RAID_SERIAL_LEN
];
540 struct imsm_update_create_array
{
541 enum imsm_update_type type
;
546 struct imsm_update_kill_array
{
547 enum imsm_update_type type
;
551 struct imsm_update_rename_array
{
552 enum imsm_update_type type
;
553 __u8 name
[MAX_RAID_SERIAL_LEN
];
557 struct imsm_update_add_remove_disk
{
558 enum imsm_update_type type
;
561 struct imsm_update_prealloc_bb_mem
{
562 enum imsm_update_type type
;
565 struct imsm_update_rwh_policy
{
566 enum imsm_update_type type
;
571 static const char *_sys_dev_type
[] = {
572 [SYS_DEV_UNKNOWN
] = "Unknown",
573 [SYS_DEV_SAS
] = "SAS",
574 [SYS_DEV_SATA
] = "SATA",
575 [SYS_DEV_NVME
] = "NVMe",
576 [SYS_DEV_VMD
] = "VMD"
579 const char *get_sys_dev_type(enum sys_dev_type type
)
581 if (type
>= SYS_DEV_MAX
)
582 type
= SYS_DEV_UNKNOWN
;
584 return _sys_dev_type
[type
];
587 static struct intel_hba
* alloc_intel_hba(struct sys_dev
*device
)
589 struct intel_hba
*result
= xmalloc(sizeof(*result
));
591 result
->type
= device
->type
;
592 result
->path
= xstrdup(device
->path
);
594 if (result
->path
&& (result
->pci_id
= strrchr(result
->path
, '/')) != NULL
)
600 static struct intel_hba
* find_intel_hba(struct intel_hba
*hba
, struct sys_dev
*device
)
602 struct intel_hba
*result
;
604 for (result
= hba
; result
; result
= result
->next
) {
605 if (result
->type
== device
->type
&& strcmp(result
->path
, device
->path
) == 0)
611 static int attach_hba_to_super(struct intel_super
*super
, struct sys_dev
*device
)
613 struct intel_hba
*hba
;
615 /* check if disk attached to Intel HBA */
616 hba
= find_intel_hba(super
->hba
, device
);
619 /* Check if HBA is already attached to super */
620 if (super
->hba
== NULL
) {
621 super
->hba
= alloc_intel_hba(device
);
626 /* Intel metadata allows for all disks attached to the same type HBA.
627 * Do not support HBA types mixing
629 if (device
->type
!= hba
->type
)
632 /* Multiple same type HBAs can be used if they share the same OROM */
633 const struct imsm_orom
*device_orom
= get_orom_by_device_id(device
->dev_id
);
635 if (device_orom
!= super
->orom
)
641 hba
->next
= alloc_intel_hba(device
);
645 static struct sys_dev
* find_disk_attached_hba(int fd
, const char *devname
)
647 struct sys_dev
*list
, *elem
;
650 if ((list
= find_intel_devices()) == NULL
)
654 disk_path
= (char *) devname
;
656 disk_path
= diskfd_to_devpath(fd
);
661 for (elem
= list
; elem
; elem
= elem
->next
)
662 if (path_attached_to_hba(disk_path
, elem
->path
))
665 if (disk_path
!= devname
)
671 static int find_intel_hba_capability(int fd
, struct intel_super
*super
,
674 static struct supertype
*match_metadata_desc_imsm(char *arg
)
676 struct supertype
*st
;
678 if (strcmp(arg
, "imsm") != 0 &&
679 strcmp(arg
, "default") != 0
683 st
= xcalloc(1, sizeof(*st
));
684 st
->ss
= &super_imsm
;
685 st
->max_devs
= IMSM_MAX_DEVICES
;
686 st
->minor_version
= 0;
691 static __u8
*get_imsm_version(struct imsm_super
*mpb
)
693 return &mpb
->sig
[MPB_SIG_LEN
];
696 /* retrieve a disk directly from the anchor when the anchor is known to be
697 * up-to-date, currently only at load time
699 static struct imsm_disk
*__get_imsm_disk(struct imsm_super
*mpb
, __u8 index
)
701 if (index
>= mpb
->num_disks
)
703 return &mpb
->disk
[index
];
706 /* retrieve the disk description based on a index of the disk
709 static struct dl
*get_imsm_dl_disk(struct intel_super
*super
, __u8 index
)
713 for (d
= super
->disks
; d
; d
= d
->next
)
714 if (d
->index
== index
)
719 /* retrieve a disk from the parsed metadata */
720 static struct imsm_disk
*get_imsm_disk(struct intel_super
*super
, __u8 index
)
724 dl
= get_imsm_dl_disk(super
, index
);
731 /* generate a checksum directly from the anchor when the anchor is known to be
732 * up-to-date, currently only at load or write_super after coalescing
734 static __u32
__gen_imsm_checksum(struct imsm_super
*mpb
)
736 __u32 end
= mpb
->mpb_size
/ sizeof(end
);
737 __u32
*p
= (__u32
*) mpb
;
741 sum
+= __le32_to_cpu(*p
);
745 return sum
- __le32_to_cpu(mpb
->check_sum
);
748 static size_t sizeof_imsm_map(struct imsm_map
*map
)
750 return sizeof(struct imsm_map
) + sizeof(__u32
) * (map
->num_members
- 1);
753 struct imsm_map
*get_imsm_map(struct imsm_dev
*dev
, int second_map
)
755 /* A device can have 2 maps if it is in the middle of a migration.
757 * MAP_0 - we return the first map
758 * MAP_1 - we return the second map if it exists, else NULL
759 * MAP_X - we return the second map if it exists, else the first
761 struct imsm_map
*map
= &dev
->vol
.map
[0];
762 struct imsm_map
*map2
= NULL
;
764 if (dev
->vol
.migr_state
)
765 map2
= (void *)map
+ sizeof_imsm_map(map
);
767 switch (second_map
) {
784 /* return the size of the device.
785 * migr_state increases the returned size if map[0] were to be duplicated
787 static size_t sizeof_imsm_dev(struct imsm_dev
*dev
, int migr_state
)
789 size_t size
= sizeof(*dev
) - sizeof(struct imsm_map
) +
790 sizeof_imsm_map(get_imsm_map(dev
, MAP_0
));
792 /* migrating means an additional map */
793 if (dev
->vol
.migr_state
)
794 size
+= sizeof_imsm_map(get_imsm_map(dev
, MAP_1
));
796 size
+= sizeof_imsm_map(get_imsm_map(dev
, MAP_0
));
801 /* retrieve disk serial number list from a metadata update */
802 static struct disk_info
*get_disk_info(struct imsm_update_create_array
*update
)
805 struct disk_info
*inf
;
807 inf
= u
+ sizeof(*update
) - sizeof(struct imsm_dev
) +
808 sizeof_imsm_dev(&update
->dev
, 0);
813 static struct imsm_dev
*__get_imsm_dev(struct imsm_super
*mpb
, __u8 index
)
819 if (index
>= mpb
->num_raid_devs
)
822 /* devices start after all disks */
823 offset
= ((void *) &mpb
->disk
[mpb
->num_disks
]) - _mpb
;
825 for (i
= 0; i
<= index
; i
++)
827 return _mpb
+ offset
;
829 offset
+= sizeof_imsm_dev(_mpb
+ offset
, 0);
834 static struct imsm_dev
*get_imsm_dev(struct intel_super
*super
, __u8 index
)
836 struct intel_dev
*dv
;
838 if (index
>= super
->anchor
->num_raid_devs
)
840 for (dv
= super
->devlist
; dv
; dv
= dv
->next
)
841 if (dv
->index
== index
)
846 static inline unsigned long long __le48_to_cpu(const struct bbm_log_block_addr
849 return ((((__u64
)__le32_to_cpu(addr
->dw1
)) << 16) |
850 __le16_to_cpu(addr
->w1
));
853 static inline struct bbm_log_block_addr
__cpu_to_le48(unsigned long long sec
)
855 struct bbm_log_block_addr addr
;
857 addr
.w1
= __cpu_to_le16((__u16
)(sec
& 0xffff));
858 addr
.dw1
= __cpu_to_le32((__u32
)(sec
>> 16) & 0xffffffff);
862 /* get size of the bbm log */
863 static __u32
get_imsm_bbm_log_size(struct bbm_log
*log
)
865 if (!log
|| log
->entry_count
== 0)
868 return sizeof(log
->signature
) +
869 sizeof(log
->entry_count
) +
870 log
->entry_count
* sizeof(struct bbm_log_entry
);
873 /* check if bad block is not partially stored in bbm log */
874 static int is_stored_in_bbm(struct bbm_log
*log
, const __u8 idx
, const unsigned
875 long long sector
, const int length
, __u32
*pos
)
879 for (i
= *pos
; i
< log
->entry_count
; i
++) {
880 struct bbm_log_entry
*entry
= &log
->marked_block_entries
[i
];
881 unsigned long long bb_start
;
882 unsigned long long bb_end
;
884 bb_start
= __le48_to_cpu(&entry
->defective_block_start
);
885 bb_end
= bb_start
+ (entry
->marked_count
+ 1);
887 if ((entry
->disk_ordinal
== idx
) && (bb_start
>= sector
) &&
888 (bb_end
<= sector
+ length
)) {
896 /* record new bad block in bbm log */
897 static int record_new_badblock(struct bbm_log
*log
, const __u8 idx
, unsigned
898 long long sector
, int length
)
902 struct bbm_log_entry
*entry
= NULL
;
904 while (is_stored_in_bbm(log
, idx
, sector
, length
, &pos
)) {
905 struct bbm_log_entry
*e
= &log
->marked_block_entries
[pos
];
907 if ((e
->marked_count
+ 1 == BBM_LOG_MAX_LBA_ENTRY_VAL
) &&
908 (__le48_to_cpu(&e
->defective_block_start
) == sector
)) {
909 sector
+= BBM_LOG_MAX_LBA_ENTRY_VAL
;
910 length
-= BBM_LOG_MAX_LBA_ENTRY_VAL
;
919 int cnt
= (length
<= BBM_LOG_MAX_LBA_ENTRY_VAL
) ? length
:
920 BBM_LOG_MAX_LBA_ENTRY_VAL
;
921 entry
->defective_block_start
= __cpu_to_le48(sector
);
922 entry
->marked_count
= cnt
- 1;
929 new_bb
= ROUND_UP(length
, BBM_LOG_MAX_LBA_ENTRY_VAL
) /
930 BBM_LOG_MAX_LBA_ENTRY_VAL
;
931 if (log
->entry_count
+ new_bb
> BBM_LOG_MAX_ENTRIES
)
935 int cnt
= (length
<= BBM_LOG_MAX_LBA_ENTRY_VAL
) ? length
:
936 BBM_LOG_MAX_LBA_ENTRY_VAL
;
937 struct bbm_log_entry
*entry
=
938 &log
->marked_block_entries
[log
->entry_count
];
940 entry
->defective_block_start
= __cpu_to_le48(sector
);
941 entry
->marked_count
= cnt
- 1;
942 entry
->disk_ordinal
= idx
;
953 /* clear all bad blocks for given disk */
954 static void clear_disk_badblocks(struct bbm_log
*log
, const __u8 idx
)
958 while (i
< log
->entry_count
) {
959 struct bbm_log_entry
*entries
= log
->marked_block_entries
;
961 if (entries
[i
].disk_ordinal
== idx
) {
962 if (i
< log
->entry_count
- 1)
963 entries
[i
] = entries
[log
->entry_count
- 1];
971 /* clear given bad block */
972 static int clear_badblock(struct bbm_log
*log
, const __u8 idx
, const unsigned
973 long long sector
, const int length
) {
976 while (i
< log
->entry_count
) {
977 struct bbm_log_entry
*entries
= log
->marked_block_entries
;
979 if ((entries
[i
].disk_ordinal
== idx
) &&
980 (__le48_to_cpu(&entries
[i
].defective_block_start
) ==
981 sector
) && (entries
[i
].marked_count
+ 1 == length
)) {
982 if (i
< log
->entry_count
- 1)
983 entries
[i
] = entries
[log
->entry_count
- 1];
993 /* allocate and load BBM log from metadata */
994 static int load_bbm_log(struct intel_super
*super
)
996 struct imsm_super
*mpb
= super
->anchor
;
997 __u32 bbm_log_size
= __le32_to_cpu(mpb
->bbm_log_size
);
999 super
->bbm_log
= xcalloc(1, sizeof(struct bbm_log
));
1000 if (!super
->bbm_log
)
1004 struct bbm_log
*log
= (void *)mpb
+
1005 __le32_to_cpu(mpb
->mpb_size
) - bbm_log_size
;
1009 if (bbm_log_size
< sizeof(log
->signature
) +
1010 sizeof(log
->entry_count
))
1013 entry_count
= __le32_to_cpu(log
->entry_count
);
1014 if ((__le32_to_cpu(log
->signature
) != BBM_LOG_SIGNATURE
) ||
1015 (entry_count
> BBM_LOG_MAX_ENTRIES
))
1019 sizeof(log
->signature
) + sizeof(log
->entry_count
) +
1020 entry_count
* sizeof(struct bbm_log_entry
))
1023 memcpy(super
->bbm_log
, log
, bbm_log_size
);
1025 super
->bbm_log
->signature
= __cpu_to_le32(BBM_LOG_SIGNATURE
);
1026 super
->bbm_log
->entry_count
= 0;
1032 /* checks if bad block is within volume boundaries */
1033 static int is_bad_block_in_volume(const struct bbm_log_entry
*entry
,
1034 const unsigned long long start_sector
,
1035 const unsigned long long size
)
1037 unsigned long long bb_start
;
1038 unsigned long long bb_end
;
1040 bb_start
= __le48_to_cpu(&entry
->defective_block_start
);
1041 bb_end
= bb_start
+ (entry
->marked_count
+ 1);
1043 if (((bb_start
>= start_sector
) && (bb_start
< start_sector
+ size
)) ||
1044 ((bb_end
>= start_sector
) && (bb_end
<= start_sector
+ size
)))
1050 /* get list of bad blocks on a drive for a volume */
1051 static void get_volume_badblocks(const struct bbm_log
*log
, const __u8 idx
,
1052 const unsigned long long start_sector
,
1053 const unsigned long long size
,
1059 for (i
= 0; i
< log
->entry_count
; i
++) {
1060 const struct bbm_log_entry
*ent
=
1061 &log
->marked_block_entries
[i
];
1062 struct md_bb_entry
*bb
;
1064 if ((ent
->disk_ordinal
== idx
) &&
1065 is_bad_block_in_volume(ent
, start_sector
, size
)) {
1067 if (!bbs
->entries
) {
1068 bbs
->entries
= xmalloc(BBM_LOG_MAX_ENTRIES
*
1074 bb
= &bbs
->entries
[count
++];
1075 bb
->sector
= __le48_to_cpu(&ent
->defective_block_start
);
1076 bb
->length
= ent
->marked_count
+ 1;
1084 * == MAP_0 get first map
1085 * == MAP_1 get second map
1086 * == MAP_X than get map according to the current migr_state
1088 static __u32
get_imsm_ord_tbl_ent(struct imsm_dev
*dev
,
1092 struct imsm_map
*map
;
1094 map
= get_imsm_map(dev
, second_map
);
1096 /* top byte identifies disk under rebuild */
1097 return __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
1100 #define ord_to_idx(ord) (((ord) << 8) >> 8)
1101 static __u32
get_imsm_disk_idx(struct imsm_dev
*dev
, int slot
, int second_map
)
1103 __u32 ord
= get_imsm_ord_tbl_ent(dev
, slot
, second_map
);
1105 return ord_to_idx(ord
);
1108 static void set_imsm_ord_tbl_ent(struct imsm_map
*map
, int slot
, __u32 ord
)
1110 map
->disk_ord_tbl
[slot
] = __cpu_to_le32(ord
);
1113 static int get_imsm_disk_slot(struct imsm_map
*map
, unsigned idx
)
1118 for (slot
= 0; slot
< map
->num_members
; slot
++) {
1119 ord
= __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
1120 if (ord_to_idx(ord
) == idx
)
1127 static int get_imsm_raid_level(struct imsm_map
*map
)
1129 if (map
->raid_level
== 1) {
1130 if (map
->num_members
== 2)
1136 return map
->raid_level
;
1139 static int cmp_extent(const void *av
, const void *bv
)
1141 const struct extent
*a
= av
;
1142 const struct extent
*b
= bv
;
1143 if (a
->start
< b
->start
)
1145 if (a
->start
> b
->start
)
1150 static int count_memberships(struct dl
*dl
, struct intel_super
*super
)
1152 int memberships
= 0;
1155 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
1156 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
1157 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
1159 if (get_imsm_disk_slot(map
, dl
->index
) >= 0)
1166 static __u32
imsm_min_reserved_sectors(struct intel_super
*super
);
1168 static int split_ull(unsigned long long n
, void *lo
, void *hi
)
1170 if (lo
== 0 || hi
== 0)
1172 __put_unaligned32(__cpu_to_le32((__u32
)n
), lo
);
1173 __put_unaligned32(__cpu_to_le32((n
>> 32)), hi
);
1177 static unsigned long long join_u32(__u32 lo
, __u32 hi
)
1179 return (unsigned long long)__le32_to_cpu(lo
) |
1180 (((unsigned long long)__le32_to_cpu(hi
)) << 32);
1183 static unsigned long long total_blocks(struct imsm_disk
*disk
)
1187 return join_u32(disk
->total_blocks_lo
, disk
->total_blocks_hi
);
1190 static unsigned long long pba_of_lba0(struct imsm_map
*map
)
1194 return join_u32(map
->pba_of_lba0_lo
, map
->pba_of_lba0_hi
);
1197 static unsigned long long blocks_per_member(struct imsm_map
*map
)
1201 return join_u32(map
->blocks_per_member_lo
, map
->blocks_per_member_hi
);
1204 static unsigned long long num_data_stripes(struct imsm_map
*map
)
1208 return join_u32(map
->num_data_stripes_lo
, map
->num_data_stripes_hi
);
1211 static unsigned long long imsm_dev_size(struct imsm_dev
*dev
)
1215 return join_u32(dev
->size_low
, dev
->size_high
);
1218 static unsigned long long migr_chkp_area_pba(struct migr_record
*migr_rec
)
1220 if (migr_rec
== NULL
)
1222 return join_u32(migr_rec
->ckpt_area_pba_lo
,
1223 migr_rec
->ckpt_area_pba_hi
);
1226 static unsigned long long current_migr_unit(struct migr_record
*migr_rec
)
1228 if (migr_rec
== NULL
)
1230 return join_u32(migr_rec
->curr_migr_unit_lo
,
1231 migr_rec
->curr_migr_unit_hi
);
1234 static unsigned long long migr_dest_1st_member_lba(struct migr_record
*migr_rec
)
1236 if (migr_rec
== NULL
)
1238 return join_u32(migr_rec
->dest_1st_member_lba_lo
,
1239 migr_rec
->dest_1st_member_lba_hi
);
1242 static unsigned long long get_num_migr_units(struct migr_record
*migr_rec
)
1244 if (migr_rec
== NULL
)
1246 return join_u32(migr_rec
->num_migr_units_lo
,
1247 migr_rec
->num_migr_units_hi
);
1250 static void set_total_blocks(struct imsm_disk
*disk
, unsigned long long n
)
1252 split_ull(n
, &disk
->total_blocks_lo
, &disk
->total_blocks_hi
);
1255 static void set_pba_of_lba0(struct imsm_map
*map
, unsigned long long n
)
1257 split_ull(n
, &map
->pba_of_lba0_lo
, &map
->pba_of_lba0_hi
);
1260 static void set_blocks_per_member(struct imsm_map
*map
, unsigned long long n
)
1262 split_ull(n
, &map
->blocks_per_member_lo
, &map
->blocks_per_member_hi
);
1265 static void set_num_data_stripes(struct imsm_map
*map
, unsigned long long n
)
1267 split_ull(n
, &map
->num_data_stripes_lo
, &map
->num_data_stripes_hi
);
1270 static void set_imsm_dev_size(struct imsm_dev
*dev
, unsigned long long n
)
1272 split_ull(n
, &dev
->size_low
, &dev
->size_high
);
1275 static void set_migr_chkp_area_pba(struct migr_record
*migr_rec
,
1276 unsigned long long n
)
1278 split_ull(n
, &migr_rec
->ckpt_area_pba_lo
, &migr_rec
->ckpt_area_pba_hi
);
1281 static void set_current_migr_unit(struct migr_record
*migr_rec
,
1282 unsigned long long n
)
1284 split_ull(n
, &migr_rec
->curr_migr_unit_lo
,
1285 &migr_rec
->curr_migr_unit_hi
);
1288 static void set_migr_dest_1st_member_lba(struct migr_record
*migr_rec
,
1289 unsigned long long n
)
1291 split_ull(n
, &migr_rec
->dest_1st_member_lba_lo
,
1292 &migr_rec
->dest_1st_member_lba_hi
);
1295 static void set_num_migr_units(struct migr_record
*migr_rec
,
1296 unsigned long long n
)
1298 split_ull(n
, &migr_rec
->num_migr_units_lo
,
1299 &migr_rec
->num_migr_units_hi
);
1302 static unsigned long long per_dev_array_size(struct imsm_map
*map
)
1304 unsigned long long array_size
= 0;
1309 array_size
= num_data_stripes(map
) * map
->blocks_per_strip
;
1310 if (get_imsm_raid_level(map
) == 1 || get_imsm_raid_level(map
) == 10)
1316 static struct extent
*get_extents(struct intel_super
*super
, struct dl
*dl
,
1317 int get_minimal_reservation
)
1319 /* find a list of used extents on the given physical device */
1320 struct extent
*rv
, *e
;
1322 int memberships
= count_memberships(dl
, super
);
1325 /* trim the reserved area for spares, so they can join any array
1326 * regardless of whether the OROM has assigned sectors from the
1327 * IMSM_RESERVED_SECTORS region
1329 if (dl
->index
== -1 || get_minimal_reservation
)
1330 reservation
= imsm_min_reserved_sectors(super
);
1332 reservation
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
1334 rv
= xcalloc(sizeof(struct extent
), (memberships
+ 1));
1337 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
1338 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
1339 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
1341 if (get_imsm_disk_slot(map
, dl
->index
) >= 0) {
1342 e
->start
= pba_of_lba0(map
);
1343 e
->size
= per_dev_array_size(map
);
1347 qsort(rv
, memberships
, sizeof(*rv
), cmp_extent
);
1349 /* determine the start of the metadata
1350 * when no raid devices are defined use the default
1351 * ...otherwise allow the metadata to truncate the value
1352 * as is the case with older versions of imsm
1355 struct extent
*last
= &rv
[memberships
- 1];
1356 unsigned long long remainder
;
1358 remainder
= total_blocks(&dl
->disk
) - (last
->start
+ last
->size
);
1359 /* round down to 1k block to satisfy precision of the kernel
1363 /* make sure remainder is still sane */
1364 if (remainder
< (unsigned)ROUND_UP(super
->len
, 512) >> 9)
1365 remainder
= ROUND_UP(super
->len
, 512) >> 9;
1366 if (reservation
> remainder
)
1367 reservation
= remainder
;
1369 e
->start
= total_blocks(&dl
->disk
) - reservation
;
1374 /* try to determine how much space is reserved for metadata from
1375 * the last get_extents() entry, otherwise fallback to the
1378 static __u32
imsm_reserved_sectors(struct intel_super
*super
, struct dl
*dl
)
1384 /* for spares just return a minimal reservation which will grow
1385 * once the spare is picked up by an array
1387 if (dl
->index
== -1)
1388 return MPB_SECTOR_CNT
;
1390 e
= get_extents(super
, dl
, 0);
1392 return MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
1394 /* scroll to last entry */
1395 for (i
= 0; e
[i
].size
; i
++)
1398 rv
= total_blocks(&dl
->disk
) - e
[i
].start
;
1405 static int is_spare(struct imsm_disk
*disk
)
1407 return (disk
->status
& SPARE_DISK
) == SPARE_DISK
;
1410 static int is_configured(struct imsm_disk
*disk
)
1412 return (disk
->status
& CONFIGURED_DISK
) == CONFIGURED_DISK
;
1415 static int is_failed(struct imsm_disk
*disk
)
1417 return (disk
->status
& FAILED_DISK
) == FAILED_DISK
;
1420 static int is_journal(struct imsm_disk
*disk
)
1422 return (disk
->status
& JOURNAL_DISK
) == JOURNAL_DISK
;
1425 /* round array size down to closest MB and ensure it splits evenly
1428 static unsigned long long round_size_to_mb(unsigned long long size
, unsigned int
1432 size
= (size
>> SECT_PER_MB_SHIFT
) << SECT_PER_MB_SHIFT
;
1438 static int able_to_resync(int raid_level
, int missing_disks
)
1440 int max_missing_disks
= 0;
1442 switch (raid_level
) {
1444 max_missing_disks
= 1;
1447 max_missing_disks
= 0;
1449 return missing_disks
<= max_missing_disks
;
1452 /* try to determine how much space is reserved for metadata from
1453 * the last get_extents() entry on the smallest active disk,
1454 * otherwise fallback to the default
1456 static __u32
imsm_min_reserved_sectors(struct intel_super
*super
)
1460 unsigned long long min_active
;
1462 __u32 rv
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
1463 struct dl
*dl
, *dl_min
= NULL
;
1469 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
1472 unsigned long long blocks
= total_blocks(&dl
->disk
);
1473 if (blocks
< min_active
|| min_active
== 0) {
1475 min_active
= blocks
;
1481 /* find last lba used by subarrays on the smallest active disk */
1482 e
= get_extents(super
, dl_min
, 0);
1485 for (i
= 0; e
[i
].size
; i
++)
1488 remainder
= min_active
- e
[i
].start
;
1491 /* to give priority to recovery we should not require full
1492 IMSM_RESERVED_SECTORS from the spare */
1493 rv
= MPB_SECTOR_CNT
+ NUM_BLOCKS_DIRTY_STRIPE_REGION
;
1495 /* if real reservation is smaller use that value */
1496 return (remainder
< rv
) ? remainder
: rv
;
1500 * Return minimum size of a spare and sector size
1501 * that can be used in this array
1503 int get_spare_criteria_imsm(struct supertype
*st
, struct spare_criteria
*c
)
1505 struct intel_super
*super
= st
->sb
;
1509 unsigned long long size
= 0;
1516 /* find first active disk in array */
1518 while (dl
&& (is_failed(&dl
->disk
) || dl
->index
== -1))
1522 /* find last lba used by subarrays */
1523 e
= get_extents(super
, dl
, 0);
1526 for (i
= 0; e
[i
].size
; i
++)
1529 size
= e
[i
-1].start
+ e
[i
-1].size
;
1532 /* add the amount of space needed for metadata */
1533 size
+= imsm_min_reserved_sectors(super
);
1535 c
->min_size
= size
* 512;
1536 c
->sector_size
= super
->sector_size
;
1541 static int is_gen_migration(struct imsm_dev
*dev
);
1543 #define IMSM_4K_DIV 8
1545 static __u64
blocks_per_migr_unit(struct intel_super
*super
,
1546 struct imsm_dev
*dev
);
1548 static void print_imsm_dev(struct intel_super
*super
,
1549 struct imsm_dev
*dev
,
1555 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
1556 struct imsm_map
*map2
= get_imsm_map(dev
, MAP_1
);
1560 printf("[%.16s]:\n", dev
->volume
);
1561 printf(" UUID : %s\n", uuid
);
1562 printf(" RAID Level : %d", get_imsm_raid_level(map
));
1564 printf(" <-- %d", get_imsm_raid_level(map2
));
1566 printf(" Members : %d", map
->num_members
);
1568 printf(" <-- %d", map2
->num_members
);
1570 printf(" Slots : [");
1571 for (i
= 0; i
< map
->num_members
; i
++) {
1572 ord
= get_imsm_ord_tbl_ent(dev
, i
, MAP_0
);
1573 printf("%s", ord
& IMSM_ORD_REBUILD
? "_" : "U");
1578 for (i
= 0; i
< map2
->num_members
; i
++) {
1579 ord
= get_imsm_ord_tbl_ent(dev
, i
, MAP_1
);
1580 printf("%s", ord
& IMSM_ORD_REBUILD
? "_" : "U");
1585 printf(" Failed disk : ");
1586 if (map
->failed_disk_num
== 0xff)
1589 printf("%i", map
->failed_disk_num
);
1591 slot
= get_imsm_disk_slot(map
, disk_idx
);
1593 ord
= get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
);
1594 printf(" This Slot : %d%s\n", slot
,
1595 ord
& IMSM_ORD_REBUILD
? " (out-of-sync)" : "");
1597 printf(" This Slot : ?\n");
1598 printf(" Sector Size : %u\n", super
->sector_size
);
1599 sz
= imsm_dev_size(dev
);
1600 printf(" Array Size : %llu%s\n",
1601 (unsigned long long)sz
* 512 / super
->sector_size
,
1602 human_size(sz
* 512));
1603 sz
= blocks_per_member(map
);
1604 printf(" Per Dev Size : %llu%s\n",
1605 (unsigned long long)sz
* 512 / super
->sector_size
,
1606 human_size(sz
* 512));
1607 printf(" Sector Offset : %llu\n",
1609 printf(" Num Stripes : %llu\n",
1610 num_data_stripes(map
));
1611 printf(" Chunk Size : %u KiB",
1612 __le16_to_cpu(map
->blocks_per_strip
) / 2);
1614 printf(" <-- %u KiB",
1615 __le16_to_cpu(map2
->blocks_per_strip
) / 2);
1617 printf(" Reserved : %d\n", __le32_to_cpu(dev
->reserved_blocks
));
1618 printf(" Migrate State : ");
1619 if (dev
->vol
.migr_state
) {
1620 if (migr_type(dev
) == MIGR_INIT
)
1621 printf("initialize\n");
1622 else if (migr_type(dev
) == MIGR_REBUILD
)
1623 printf("rebuild\n");
1624 else if (migr_type(dev
) == MIGR_VERIFY
)
1626 else if (migr_type(dev
) == MIGR_GEN_MIGR
)
1627 printf("general migration\n");
1628 else if (migr_type(dev
) == MIGR_STATE_CHANGE
)
1629 printf("state change\n");
1630 else if (migr_type(dev
) == MIGR_REPAIR
)
1633 printf("<unknown:%d>\n", migr_type(dev
));
1636 printf(" Map State : %s", map_state_str
[map
->map_state
]);
1637 if (dev
->vol
.migr_state
) {
1638 struct imsm_map
*map
= get_imsm_map(dev
, MAP_1
);
1640 printf(" <-- %s", map_state_str
[map
->map_state
]);
1641 printf("\n Checkpoint : %u ",
1642 __le32_to_cpu(dev
->vol
.curr_migr_unit
));
1643 if (is_gen_migration(dev
) && (slot
> 1 || slot
< 0))
1646 printf("(%llu)", (unsigned long long)
1647 blocks_per_migr_unit(super
, dev
));
1650 printf(" Dirty State : %s\n", (dev
->vol
.dirty
& RAIDVOL_DIRTY
) ?
1652 printf(" RWH Policy : ");
1653 if (dev
->rwh_policy
== RWH_OFF
|| dev
->rwh_policy
== RWH_MULTIPLE_OFF
)
1655 else if (dev
->rwh_policy
== RWH_DISTRIBUTED
)
1656 printf("PPL distributed\n");
1657 else if (dev
->rwh_policy
== RWH_JOURNALING_DRIVE
)
1658 printf("PPL journaling drive\n");
1659 else if (dev
->rwh_policy
== RWH_MULTIPLE_DISTRIBUTED
)
1660 printf("Multiple distributed PPLs\n");
1661 else if (dev
->rwh_policy
== RWH_MULTIPLE_PPLS_JOURNALING_DRIVE
)
1662 printf("Multiple PPLs on journaling drive\n");
1664 printf("<unknown:%d>\n", dev
->rwh_policy
);
1667 static void print_imsm_disk(struct imsm_disk
*disk
,
1670 unsigned int sector_size
) {
1671 char str
[MAX_RAID_SERIAL_LEN
+ 1];
1674 if (index
< -1 || !disk
)
1678 snprintf(str
, MAX_RAID_SERIAL_LEN
+ 1, "%s", disk
->serial
);
1680 printf(" Disk%02d Serial : %s\n", index
, str
);
1682 printf(" Disk Serial : %s\n", str
);
1683 printf(" State :%s%s%s%s\n", is_spare(disk
) ? " spare" : "",
1684 is_configured(disk
) ? " active" : "",
1685 is_failed(disk
) ? " failed" : "",
1686 is_journal(disk
) ? " journal" : "");
1687 printf(" Id : %08x\n", __le32_to_cpu(disk
->scsi_id
));
1688 sz
= total_blocks(disk
) - reserved
;
1689 printf(" Usable Size : %llu%s\n",
1690 (unsigned long long)sz
* 512 / sector_size
,
1691 human_size(sz
* 512));
1694 void convert_to_4k_imsm_migr_rec(struct intel_super
*super
)
1696 struct migr_record
*migr_rec
= super
->migr_rec
;
1698 migr_rec
->blocks_per_unit
/= IMSM_4K_DIV
;
1699 migr_rec
->dest_depth_per_unit
/= IMSM_4K_DIV
;
1700 split_ull((join_u32(migr_rec
->post_migr_vol_cap
,
1701 migr_rec
->post_migr_vol_cap_hi
) / IMSM_4K_DIV
),
1702 &migr_rec
->post_migr_vol_cap
, &migr_rec
->post_migr_vol_cap_hi
);
1703 set_migr_chkp_area_pba(migr_rec
,
1704 migr_chkp_area_pba(migr_rec
) / IMSM_4K_DIV
);
1705 set_migr_dest_1st_member_lba(migr_rec
,
1706 migr_dest_1st_member_lba(migr_rec
) / IMSM_4K_DIV
);
1709 void convert_to_4k_imsm_disk(struct imsm_disk
*disk
)
1711 set_total_blocks(disk
, (total_blocks(disk
)/IMSM_4K_DIV
));
1714 void convert_to_4k(struct intel_super
*super
)
1716 struct imsm_super
*mpb
= super
->anchor
;
1717 struct imsm_disk
*disk
;
1719 __u32 bbm_log_size
= __le32_to_cpu(mpb
->bbm_log_size
);
1721 for (i
= 0; i
< mpb
->num_disks
; i
++) {
1722 disk
= __get_imsm_disk(mpb
, i
);
1724 convert_to_4k_imsm_disk(disk
);
1726 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
1727 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
1728 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
1730 set_imsm_dev_size(dev
, imsm_dev_size(dev
)/IMSM_4K_DIV
);
1731 dev
->vol
.curr_migr_unit
/= IMSM_4K_DIV
;
1734 set_blocks_per_member(map
, blocks_per_member(map
)/IMSM_4K_DIV
);
1735 map
->blocks_per_strip
/= IMSM_4K_DIV
;
1736 set_pba_of_lba0(map
, pba_of_lba0(map
)/IMSM_4K_DIV
);
1738 if (dev
->vol
.migr_state
) {
1740 map
= get_imsm_map(dev
, MAP_1
);
1741 set_blocks_per_member(map
,
1742 blocks_per_member(map
)/IMSM_4K_DIV
);
1743 map
->blocks_per_strip
/= IMSM_4K_DIV
;
1744 set_pba_of_lba0(map
, pba_of_lba0(map
)/IMSM_4K_DIV
);
1748 struct bbm_log
*log
= (void *)mpb
+
1749 __le32_to_cpu(mpb
->mpb_size
) - bbm_log_size
;
1752 for (i
= 0; i
< log
->entry_count
; i
++) {
1753 struct bbm_log_entry
*entry
=
1754 &log
->marked_block_entries
[i
];
1756 __u8 count
= entry
->marked_count
+ 1;
1757 unsigned long long sector
=
1758 __le48_to_cpu(&entry
->defective_block_start
);
1760 entry
->defective_block_start
=
1761 __cpu_to_le48(sector
/IMSM_4K_DIV
);
1762 entry
->marked_count
= max(count
/IMSM_4K_DIV
, 1) - 1;
1766 mpb
->check_sum
= __gen_imsm_checksum(mpb
);
1769 void examine_migr_rec_imsm(struct intel_super
*super
)
1771 struct migr_record
*migr_rec
= super
->migr_rec
;
1772 struct imsm_super
*mpb
= super
->anchor
;
1775 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
1776 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
1777 struct imsm_map
*map
;
1780 if (is_gen_migration(dev
) == 0)
1783 printf("\nMigration Record Information:");
1785 /* first map under migration */
1786 map
= get_imsm_map(dev
, MAP_0
);
1788 slot
= get_imsm_disk_slot(map
, super
->disks
->index
);
1789 if (map
== NULL
|| slot
> 1 || slot
< 0) {
1790 printf(" Empty\n ");
1791 printf("Examine one of first two disks in array\n");
1794 printf("\n Status : ");
1795 if (__le32_to_cpu(migr_rec
->rec_status
) == UNIT_SRC_NORMAL
)
1798 printf("Contains Data\n");
1799 printf(" Current Unit : %llu\n",
1800 current_migr_unit(migr_rec
));
1801 printf(" Family : %u\n",
1802 __le32_to_cpu(migr_rec
->family_num
));
1803 printf(" Ascending : %u\n",
1804 __le32_to_cpu(migr_rec
->ascending_migr
));
1805 printf(" Blocks Per Unit : %u\n",
1806 __le32_to_cpu(migr_rec
->blocks_per_unit
));
1807 printf(" Dest. Depth Per Unit : %u\n",
1808 __le32_to_cpu(migr_rec
->dest_depth_per_unit
));
1809 printf(" Checkpoint Area pba : %llu\n",
1810 migr_chkp_area_pba(migr_rec
));
1811 printf(" First member lba : %llu\n",
1812 migr_dest_1st_member_lba(migr_rec
));
1813 printf(" Total Number of Units : %llu\n",
1814 get_num_migr_units(migr_rec
));
1815 printf(" Size of volume : %llu\n",
1816 join_u32(migr_rec
->post_migr_vol_cap
,
1817 migr_rec
->post_migr_vol_cap_hi
));
1818 printf(" Record was read from : %u\n",
1819 __le32_to_cpu(migr_rec
->ckpt_read_disk_num
));
1825 void convert_from_4k_imsm_migr_rec(struct intel_super
*super
)
1827 struct migr_record
*migr_rec
= super
->migr_rec
;
1829 migr_rec
->blocks_per_unit
*= IMSM_4K_DIV
;
1830 migr_rec
->dest_depth_per_unit
*= IMSM_4K_DIV
;
1831 split_ull((join_u32(migr_rec
->post_migr_vol_cap
,
1832 migr_rec
->post_migr_vol_cap_hi
) * IMSM_4K_DIV
),
1833 &migr_rec
->post_migr_vol_cap
,
1834 &migr_rec
->post_migr_vol_cap_hi
);
1835 set_migr_chkp_area_pba(migr_rec
,
1836 migr_chkp_area_pba(migr_rec
) * IMSM_4K_DIV
);
1837 set_migr_dest_1st_member_lba(migr_rec
,
1838 migr_dest_1st_member_lba(migr_rec
) * IMSM_4K_DIV
);
1841 void convert_from_4k(struct intel_super
*super
)
1843 struct imsm_super
*mpb
= super
->anchor
;
1844 struct imsm_disk
*disk
;
1846 __u32 bbm_log_size
= __le32_to_cpu(mpb
->bbm_log_size
);
1848 for (i
= 0; i
< mpb
->num_disks
; i
++) {
1849 disk
= __get_imsm_disk(mpb
, i
);
1851 set_total_blocks(disk
, (total_blocks(disk
)*IMSM_4K_DIV
));
1854 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
1855 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
1856 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
1858 set_imsm_dev_size(dev
, imsm_dev_size(dev
)*IMSM_4K_DIV
);
1859 dev
->vol
.curr_migr_unit
*= IMSM_4K_DIV
;
1862 set_blocks_per_member(map
, blocks_per_member(map
)*IMSM_4K_DIV
);
1863 map
->blocks_per_strip
*= IMSM_4K_DIV
;
1864 set_pba_of_lba0(map
, pba_of_lba0(map
)*IMSM_4K_DIV
);
1866 if (dev
->vol
.migr_state
) {
1868 map
= get_imsm_map(dev
, MAP_1
);
1869 set_blocks_per_member(map
,
1870 blocks_per_member(map
)*IMSM_4K_DIV
);
1871 map
->blocks_per_strip
*= IMSM_4K_DIV
;
1872 set_pba_of_lba0(map
, pba_of_lba0(map
)*IMSM_4K_DIV
);
1876 struct bbm_log
*log
= (void *)mpb
+
1877 __le32_to_cpu(mpb
->mpb_size
) - bbm_log_size
;
1880 for (i
= 0; i
< log
->entry_count
; i
++) {
1881 struct bbm_log_entry
*entry
=
1882 &log
->marked_block_entries
[i
];
1884 __u8 count
= entry
->marked_count
+ 1;
1885 unsigned long long sector
=
1886 __le48_to_cpu(&entry
->defective_block_start
);
1888 entry
->defective_block_start
=
1889 __cpu_to_le48(sector
*IMSM_4K_DIV
);
1890 entry
->marked_count
= count
*IMSM_4K_DIV
- 1;
1894 mpb
->check_sum
= __gen_imsm_checksum(mpb
);
1897 /*******************************************************************************
1898 * function: imsm_check_attributes
1899 * Description: Function checks if features represented by attributes flags
1900 * are supported by mdadm.
1902 * attributes - Attributes read from metadata
1904 * 0 - passed attributes contains unsupported features flags
1905 * 1 - all features are supported
1906 ******************************************************************************/
1907 static int imsm_check_attributes(__u32 attributes
)
1910 __u32 not_supported
= MPB_ATTRIB_SUPPORTED
^0xffffffff;
1912 not_supported
&= ~MPB_ATTRIB_IGNORED
;
1914 not_supported
&= attributes
;
1915 if (not_supported
) {
1916 pr_err("(IMSM): Unsupported attributes : %x\n",
1917 (unsigned)__le32_to_cpu(not_supported
));
1918 if (not_supported
& MPB_ATTRIB_CHECKSUM_VERIFY
) {
1919 dprintf("\t\tMPB_ATTRIB_CHECKSUM_VERIFY \n");
1920 not_supported
^= MPB_ATTRIB_CHECKSUM_VERIFY
;
1922 if (not_supported
& MPB_ATTRIB_2TB
) {
1923 dprintf("\t\tMPB_ATTRIB_2TB\n");
1924 not_supported
^= MPB_ATTRIB_2TB
;
1926 if (not_supported
& MPB_ATTRIB_RAID0
) {
1927 dprintf("\t\tMPB_ATTRIB_RAID0\n");
1928 not_supported
^= MPB_ATTRIB_RAID0
;
1930 if (not_supported
& MPB_ATTRIB_RAID1
) {
1931 dprintf("\t\tMPB_ATTRIB_RAID1\n");
1932 not_supported
^= MPB_ATTRIB_RAID1
;
1934 if (not_supported
& MPB_ATTRIB_RAID10
) {
1935 dprintf("\t\tMPB_ATTRIB_RAID10\n");
1936 not_supported
^= MPB_ATTRIB_RAID10
;
1938 if (not_supported
& MPB_ATTRIB_RAID1E
) {
1939 dprintf("\t\tMPB_ATTRIB_RAID1E\n");
1940 not_supported
^= MPB_ATTRIB_RAID1E
;
1942 if (not_supported
& MPB_ATTRIB_RAID5
) {
1943 dprintf("\t\tMPB_ATTRIB_RAID5\n");
1944 not_supported
^= MPB_ATTRIB_RAID5
;
1946 if (not_supported
& MPB_ATTRIB_RAIDCNG
) {
1947 dprintf("\t\tMPB_ATTRIB_RAIDCNG\n");
1948 not_supported
^= MPB_ATTRIB_RAIDCNG
;
1950 if (not_supported
& MPB_ATTRIB_BBM
) {
1951 dprintf("\t\tMPB_ATTRIB_BBM\n");
1952 not_supported
^= MPB_ATTRIB_BBM
;
1954 if (not_supported
& MPB_ATTRIB_CHECKSUM_VERIFY
) {
1955 dprintf("\t\tMPB_ATTRIB_CHECKSUM_VERIFY (== MPB_ATTRIB_LEGACY)\n");
1956 not_supported
^= MPB_ATTRIB_CHECKSUM_VERIFY
;
1958 if (not_supported
& MPB_ATTRIB_EXP_STRIPE_SIZE
) {
1959 dprintf("\t\tMPB_ATTRIB_EXP_STRIP_SIZE\n");
1960 not_supported
^= MPB_ATTRIB_EXP_STRIPE_SIZE
;
1962 if (not_supported
& MPB_ATTRIB_2TB_DISK
) {
1963 dprintf("\t\tMPB_ATTRIB_2TB_DISK\n");
1964 not_supported
^= MPB_ATTRIB_2TB_DISK
;
1966 if (not_supported
& MPB_ATTRIB_NEVER_USE2
) {
1967 dprintf("\t\tMPB_ATTRIB_NEVER_USE2\n");
1968 not_supported
^= MPB_ATTRIB_NEVER_USE2
;
1970 if (not_supported
& MPB_ATTRIB_NEVER_USE
) {
1971 dprintf("\t\tMPB_ATTRIB_NEVER_USE\n");
1972 not_supported
^= MPB_ATTRIB_NEVER_USE
;
1976 dprintf("(IMSM): Unknown attributes : %x\n", not_supported
);
1984 static void getinfo_super_imsm(struct supertype
*st
, struct mdinfo
*info
, char *map
);
1986 static void examine_super_imsm(struct supertype
*st
, char *homehost
)
1988 struct intel_super
*super
= st
->sb
;
1989 struct imsm_super
*mpb
= super
->anchor
;
1990 char str
[MAX_SIGNATURE_LENGTH
];
1995 __u32 reserved
= imsm_reserved_sectors(super
, super
->disks
);
1998 strncpy(str
, (char *)mpb
->sig
, MPB_SIG_LEN
);
1999 str
[MPB_SIG_LEN
-1] = '\0';
2000 printf(" Magic : %s\n", str
);
2001 printf(" Version : %s\n", get_imsm_version(mpb
));
2002 printf(" Orig Family : %08x\n", __le32_to_cpu(mpb
->orig_family_num
));
2003 printf(" Family : %08x\n", __le32_to_cpu(mpb
->family_num
));
2004 printf(" Generation : %08x\n", __le32_to_cpu(mpb
->generation_num
));
2005 printf(" Attributes : ");
2006 if (imsm_check_attributes(mpb
->attributes
))
2007 printf("All supported\n");
2009 printf("not supported\n");
2010 getinfo_super_imsm(st
, &info
, NULL
);
2011 fname_from_uuid(st
, &info
, nbuf
, ':');
2012 printf(" UUID : %s\n", nbuf
+ 5);
2013 sum
= __le32_to_cpu(mpb
->check_sum
);
2014 printf(" Checksum : %08x %s\n", sum
,
2015 __gen_imsm_checksum(mpb
) == sum
? "correct" : "incorrect");
2016 printf(" MPB Sectors : %d\n", mpb_sectors(mpb
, super
->sector_size
));
2017 printf(" Disks : %d\n", mpb
->num_disks
);
2018 printf(" RAID Devices : %d\n", mpb
->num_raid_devs
);
2019 print_imsm_disk(__get_imsm_disk(mpb
, super
->disks
->index
),
2020 super
->disks
->index
, reserved
, super
->sector_size
);
2021 if (get_imsm_bbm_log_size(super
->bbm_log
)) {
2022 struct bbm_log
*log
= super
->bbm_log
;
2025 printf("Bad Block Management Log:\n");
2026 printf(" Log Size : %d\n", __le32_to_cpu(mpb
->bbm_log_size
));
2027 printf(" Signature : %x\n", __le32_to_cpu(log
->signature
));
2028 printf(" Entry Count : %d\n", __le32_to_cpu(log
->entry_count
));
2030 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
2032 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
2034 super
->current_vol
= i
;
2035 getinfo_super_imsm(st
, &info
, NULL
);
2036 fname_from_uuid(st
, &info
, nbuf
, ':');
2037 print_imsm_dev(super
, dev
, nbuf
+ 5, super
->disks
->index
);
2039 for (i
= 0; i
< mpb
->num_disks
; i
++) {
2040 if (i
== super
->disks
->index
)
2042 print_imsm_disk(__get_imsm_disk(mpb
, i
), i
, reserved
,
2043 super
->sector_size
);
2046 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
2047 if (dl
->index
== -1)
2048 print_imsm_disk(&dl
->disk
, -1, reserved
,
2049 super
->sector_size
);
2051 examine_migr_rec_imsm(super
);
2054 static void brief_examine_super_imsm(struct supertype
*st
, int verbose
)
2056 /* We just write a generic IMSM ARRAY entry */
2059 struct intel_super
*super
= st
->sb
;
2061 if (!super
->anchor
->num_raid_devs
) {
2062 printf("ARRAY metadata=imsm\n");
2066 getinfo_super_imsm(st
, &info
, NULL
);
2067 fname_from_uuid(st
, &info
, nbuf
, ':');
2068 printf("ARRAY metadata=imsm UUID=%s\n", nbuf
+ 5);
2071 static void brief_examine_subarrays_imsm(struct supertype
*st
, int verbose
)
2073 /* We just write a generic IMSM ARRAY entry */
2077 struct intel_super
*super
= st
->sb
;
2080 if (!super
->anchor
->num_raid_devs
)
2083 getinfo_super_imsm(st
, &info
, NULL
);
2084 fname_from_uuid(st
, &info
, nbuf
, ':');
2085 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
2086 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
2088 super
->current_vol
= i
;
2089 getinfo_super_imsm(st
, &info
, NULL
);
2090 fname_from_uuid(st
, &info
, nbuf1
, ':');
2091 printf("ARRAY /dev/md/%.16s container=%s member=%d UUID=%s\n",
2092 dev
->volume
, nbuf
+ 5, i
, nbuf1
+ 5);
2096 static void export_examine_super_imsm(struct supertype
*st
)
2098 struct intel_super
*super
= st
->sb
;
2099 struct imsm_super
*mpb
= super
->anchor
;
2103 getinfo_super_imsm(st
, &info
, NULL
);
2104 fname_from_uuid(st
, &info
, nbuf
, ':');
2105 printf("MD_METADATA=imsm\n");
2106 printf("MD_LEVEL=container\n");
2107 printf("MD_UUID=%s\n", nbuf
+5);
2108 printf("MD_DEVICES=%u\n", mpb
->num_disks
);
2111 static int copy_metadata_imsm(struct supertype
*st
, int from
, int to
)
2113 /* The second last sector of the device contains
2114 * the "struct imsm_super" metadata.
2115 * This contains mpb_size which is the size in bytes of the
2116 * extended metadata. This is located immediately before
2118 * We want to read all that, plus the last sector which
2119 * may contain a migration record, and write it all
2123 unsigned long long dsize
, offset
;
2125 struct imsm_super
*sb
;
2126 struct intel_super
*super
= st
->sb
;
2127 unsigned int sector_size
= super
->sector_size
;
2128 unsigned int written
= 0;
2130 if (posix_memalign(&buf
, MAX_SECTOR_SIZE
, MAX_SECTOR_SIZE
) != 0)
2133 if (!get_dev_size(from
, NULL
, &dsize
))
2136 if (lseek64(from
, dsize
-(2*sector_size
), 0) < 0)
2138 if ((unsigned int)read(from
, buf
, sector_size
) != sector_size
)
2141 if (strncmp((char*)sb
->sig
, MPB_SIGNATURE
, MPB_SIG_LEN
) != 0)
2144 sectors
= mpb_sectors(sb
, sector_size
) + 2;
2145 offset
= dsize
- sectors
* sector_size
;
2146 if (lseek64(from
, offset
, 0) < 0 ||
2147 lseek64(to
, offset
, 0) < 0)
2149 while (written
< sectors
* sector_size
) {
2150 int n
= sectors
*sector_size
- written
;
2153 if (read(from
, buf
, n
) != n
)
2155 if (write(to
, buf
, n
) != n
)
2166 static void detail_super_imsm(struct supertype
*st
, char *homehost
)
2171 getinfo_super_imsm(st
, &info
, NULL
);
2172 fname_from_uuid(st
, &info
, nbuf
, ':');
2173 printf("\n UUID : %s\n", nbuf
+ 5);
2176 static void brief_detail_super_imsm(struct supertype
*st
)
2180 getinfo_super_imsm(st
, &info
, NULL
);
2181 fname_from_uuid(st
, &info
, nbuf
, ':');
2182 printf(" UUID=%s", nbuf
+ 5);
2185 static int imsm_read_serial(int fd
, char *devname
, __u8
*serial
);
2186 static void fd2devname(int fd
, char *name
);
2188 static int ahci_enumerate_ports(const char *hba_path
, int port_count
, int host_base
, int verbose
)
2190 /* dump an unsorted list of devices attached to AHCI Intel storage
2191 * controller, as well as non-connected ports
2193 int hba_len
= strlen(hba_path
) + 1;
2198 unsigned long port_mask
= (1 << port_count
) - 1;
2200 if (port_count
> (int)sizeof(port_mask
) * 8) {
2202 pr_err("port_count %d out of range\n", port_count
);
2206 /* scroll through /sys/dev/block looking for devices attached to
2209 dir
= opendir("/sys/dev/block");
2213 for (ent
= readdir(dir
); ent
; ent
= readdir(dir
)) {
2224 if (sscanf(ent
->d_name
, "%d:%d", &major
, &minor
) != 2)
2226 path
= devt_to_devpath(makedev(major
, minor
));
2229 if (!path_attached_to_hba(path
, hba_path
)) {
2235 /* retrieve the scsi device type */
2236 if (asprintf(&device
, "/sys/dev/block/%d:%d/device/xxxxxxx", major
, minor
) < 0) {
2238 pr_err("failed to allocate 'device'\n");
2242 sprintf(device
, "/sys/dev/block/%d:%d/device/type", major
, minor
);
2243 if (load_sys(device
, buf
, sizeof(buf
)) != 0) {
2245 pr_err("failed to read device type for %s\n",
2251 type
= strtoul(buf
, NULL
, 10);
2253 /* if it's not a disk print the vendor and model */
2254 if (!(type
== 0 || type
== 7 || type
== 14)) {
2257 sprintf(device
, "/sys/dev/block/%d:%d/device/vendor", major
, minor
);
2258 if (load_sys(device
, buf
, sizeof(buf
)) == 0) {
2259 strncpy(vendor
, buf
, sizeof(vendor
));
2260 vendor
[sizeof(vendor
) - 1] = '\0';
2261 c
= (char *) &vendor
[sizeof(vendor
) - 1];
2262 while (isspace(*c
) || *c
== '\0')
2266 sprintf(device
, "/sys/dev/block/%d:%d/device/model", major
, minor
);
2267 if (load_sys(device
, buf
, sizeof(buf
)) == 0) {
2268 strncpy(model
, buf
, sizeof(model
));
2269 model
[sizeof(model
) - 1] = '\0';
2270 c
= (char *) &model
[sizeof(model
) - 1];
2271 while (isspace(*c
) || *c
== '\0')
2275 if (vendor
[0] && model
[0])
2276 sprintf(buf
, "%.64s %.64s", vendor
, model
);
2278 switch (type
) { /* numbers from hald/linux/device.c */
2279 case 1: sprintf(buf
, "tape"); break;
2280 case 2: sprintf(buf
, "printer"); break;
2281 case 3: sprintf(buf
, "processor"); break;
2283 case 5: sprintf(buf
, "cdrom"); break;
2284 case 6: sprintf(buf
, "scanner"); break;
2285 case 8: sprintf(buf
, "media_changer"); break;
2286 case 9: sprintf(buf
, "comm"); break;
2287 case 12: sprintf(buf
, "raid"); break;
2288 default: sprintf(buf
, "unknown");
2294 /* chop device path to 'host%d' and calculate the port number */
2295 c
= strchr(&path
[hba_len
], '/');
2298 pr_err("%s - invalid path name\n", path
+ hba_len
);
2303 if ((sscanf(&path
[hba_len
], "ata%d", &port
) == 1) ||
2304 ((sscanf(&path
[hba_len
], "host%d", &port
) == 1)))
2308 *c
= '/'; /* repair the full string */
2309 pr_err("failed to determine port number for %s\n",
2316 /* mark this port as used */
2317 port_mask
&= ~(1 << port
);
2319 /* print out the device information */
2321 printf(" Port%d : - non-disk device (%s) -\n", port
, buf
);
2325 fd
= dev_open(ent
->d_name
, O_RDONLY
);
2327 printf(" Port%d : - disk info unavailable -\n", port
);
2329 fd2devname(fd
, buf
);
2330 printf(" Port%d : %s", port
, buf
);
2331 if (imsm_read_serial(fd
, NULL
, (__u8
*) buf
) == 0)
2332 printf(" (%.*s)\n", MAX_RAID_SERIAL_LEN
, buf
);
2347 for (i
= 0; i
< port_count
; i
++)
2348 if (port_mask
& (1 << i
))
2349 printf(" Port%d : - no device attached -\n", i
);
2355 static int print_vmd_attached_devs(struct sys_dev
*hba
)
2363 if (hba
->type
!= SYS_DEV_VMD
)
2366 /* scroll through /sys/dev/block looking for devices attached to
2369 dir
= opendir("/sys/bus/pci/drivers/nvme");
2373 for (ent
= readdir(dir
); ent
; ent
= readdir(dir
)) {
2376 /* is 'ent' a device? check that the 'subsystem' link exists and
2377 * that its target matches 'bus'
2379 sprintf(path
, "/sys/bus/pci/drivers/nvme/%s/subsystem",
2381 n
= readlink(path
, link
, sizeof(link
));
2382 if (n
< 0 || n
>= (int)sizeof(link
))
2385 c
= strrchr(link
, '/');
2388 if (strncmp("pci", c
+1, strlen("pci")) != 0)
2391 sprintf(path
, "/sys/bus/pci/drivers/nvme/%s", ent
->d_name
);
2393 rp
= realpath(path
, NULL
);
2397 if (path_attached_to_hba(rp
, hba
->path
)) {
2398 printf(" NVMe under VMD : %s\n", rp
);
2407 static void print_found_intel_controllers(struct sys_dev
*elem
)
2409 for (; elem
; elem
= elem
->next
) {
2410 pr_err("found Intel(R) ");
2411 if (elem
->type
== SYS_DEV_SATA
)
2412 fprintf(stderr
, "SATA ");
2413 else if (elem
->type
== SYS_DEV_SAS
)
2414 fprintf(stderr
, "SAS ");
2415 else if (elem
->type
== SYS_DEV_NVME
)
2416 fprintf(stderr
, "NVMe ");
2418 if (elem
->type
== SYS_DEV_VMD
)
2419 fprintf(stderr
, "VMD domain");
2421 fprintf(stderr
, "RAID controller");
2424 fprintf(stderr
, " at %s", elem
->pci_id
);
2425 fprintf(stderr
, ".\n");
2430 static int ahci_get_port_count(const char *hba_path
, int *port_count
)
2437 if ((dir
= opendir(hba_path
)) == NULL
)
2440 for (ent
= readdir(dir
); ent
; ent
= readdir(dir
)) {
2443 if ((sscanf(ent
->d_name
, "ata%d", &host
) != 1) &&
2444 ((sscanf(ent
->d_name
, "host%d", &host
) != 1)))
2446 if (*port_count
== 0)
2448 else if (host
< host_base
)
2451 if (host
+ 1 > *port_count
+ host_base
)
2452 *port_count
= host
+ 1 - host_base
;
2458 static void print_imsm_capability(const struct imsm_orom
*orom
)
2460 printf(" Platform : Intel(R) ");
2461 if (orom
->capabilities
== 0 && orom
->driver_features
== 0)
2462 printf("Matrix Storage Manager\n");
2463 else if (imsm_orom_is_enterprise(orom
) && orom
->major_ver
>= 6)
2464 printf("Virtual RAID on CPU\n");
2466 printf("Rapid Storage Technology%s\n",
2467 imsm_orom_is_enterprise(orom
) ? " enterprise" : "");
2468 if (orom
->major_ver
|| orom
->minor_ver
|| orom
->hotfix_ver
|| orom
->build
)
2469 printf(" Version : %d.%d.%d.%d\n", orom
->major_ver
,
2470 orom
->minor_ver
, orom
->hotfix_ver
, orom
->build
);
2471 printf(" RAID Levels :%s%s%s%s%s\n",
2472 imsm_orom_has_raid0(orom
) ? " raid0" : "",
2473 imsm_orom_has_raid1(orom
) ? " raid1" : "",
2474 imsm_orom_has_raid1e(orom
) ? " raid1e" : "",
2475 imsm_orom_has_raid10(orom
) ? " raid10" : "",
2476 imsm_orom_has_raid5(orom
) ? " raid5" : "");
2477 printf(" Chunk Sizes :%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
2478 imsm_orom_has_chunk(orom
, 2) ? " 2k" : "",
2479 imsm_orom_has_chunk(orom
, 4) ? " 4k" : "",
2480 imsm_orom_has_chunk(orom
, 8) ? " 8k" : "",
2481 imsm_orom_has_chunk(orom
, 16) ? " 16k" : "",
2482 imsm_orom_has_chunk(orom
, 32) ? " 32k" : "",
2483 imsm_orom_has_chunk(orom
, 64) ? " 64k" : "",
2484 imsm_orom_has_chunk(orom
, 128) ? " 128k" : "",
2485 imsm_orom_has_chunk(orom
, 256) ? " 256k" : "",
2486 imsm_orom_has_chunk(orom
, 512) ? " 512k" : "",
2487 imsm_orom_has_chunk(orom
, 1024*1) ? " 1M" : "",
2488 imsm_orom_has_chunk(orom
, 1024*2) ? " 2M" : "",
2489 imsm_orom_has_chunk(orom
, 1024*4) ? " 4M" : "",
2490 imsm_orom_has_chunk(orom
, 1024*8) ? " 8M" : "",
2491 imsm_orom_has_chunk(orom
, 1024*16) ? " 16M" : "",
2492 imsm_orom_has_chunk(orom
, 1024*32) ? " 32M" : "",
2493 imsm_orom_has_chunk(orom
, 1024*64) ? " 64M" : "");
2494 printf(" 2TB volumes :%s supported\n",
2495 (orom
->attr
& IMSM_OROM_ATTR_2TB
)?"":" not");
2496 printf(" 2TB disks :%s supported\n",
2497 (orom
->attr
& IMSM_OROM_ATTR_2TB_DISK
)?"":" not");
2498 printf(" Max Disks : %d\n", orom
->tds
);
2499 printf(" Max Volumes : %d per array, %d per %s\n",
2500 orom
->vpa
, orom
->vphba
,
2501 imsm_orom_is_nvme(orom
) ? "platform" : "controller");
2505 static void print_imsm_capability_export(const struct imsm_orom
*orom
)
2507 printf("MD_FIRMWARE_TYPE=imsm\n");
2508 if (orom
->major_ver
|| orom
->minor_ver
|| orom
->hotfix_ver
|| orom
->build
)
2509 printf("IMSM_VERSION=%d.%d.%d.%d\n", orom
->major_ver
, orom
->minor_ver
,
2510 orom
->hotfix_ver
, orom
->build
);
2511 printf("IMSM_SUPPORTED_RAID_LEVELS=%s%s%s%s%s\n",
2512 imsm_orom_has_raid0(orom
) ? "raid0 " : "",
2513 imsm_orom_has_raid1(orom
) ? "raid1 " : "",
2514 imsm_orom_has_raid1e(orom
) ? "raid1e " : "",
2515 imsm_orom_has_raid5(orom
) ? "raid10 " : "",
2516 imsm_orom_has_raid10(orom
) ? "raid5 " : "");
2517 printf("IMSM_SUPPORTED_CHUNK_SIZES=%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
2518 imsm_orom_has_chunk(orom
, 2) ? "2k " : "",
2519 imsm_orom_has_chunk(orom
, 4) ? "4k " : "",
2520 imsm_orom_has_chunk(orom
, 8) ? "8k " : "",
2521 imsm_orom_has_chunk(orom
, 16) ? "16k " : "",
2522 imsm_orom_has_chunk(orom
, 32) ? "32k " : "",
2523 imsm_orom_has_chunk(orom
, 64) ? "64k " : "",
2524 imsm_orom_has_chunk(orom
, 128) ? "128k " : "",
2525 imsm_orom_has_chunk(orom
, 256) ? "256k " : "",
2526 imsm_orom_has_chunk(orom
, 512) ? "512k " : "",
2527 imsm_orom_has_chunk(orom
, 1024*1) ? "1M " : "",
2528 imsm_orom_has_chunk(orom
, 1024*2) ? "2M " : "",
2529 imsm_orom_has_chunk(orom
, 1024*4) ? "4M " : "",
2530 imsm_orom_has_chunk(orom
, 1024*8) ? "8M " : "",
2531 imsm_orom_has_chunk(orom
, 1024*16) ? "16M " : "",
2532 imsm_orom_has_chunk(orom
, 1024*32) ? "32M " : "",
2533 imsm_orom_has_chunk(orom
, 1024*64) ? "64M " : "");
2534 printf("IMSM_2TB_VOLUMES=%s\n",(orom
->attr
& IMSM_OROM_ATTR_2TB
) ? "yes" : "no");
2535 printf("IMSM_2TB_DISKS=%s\n",(orom
->attr
& IMSM_OROM_ATTR_2TB_DISK
) ? "yes" : "no");
2536 printf("IMSM_MAX_DISKS=%d\n",orom
->tds
);
2537 printf("IMSM_MAX_VOLUMES_PER_ARRAY=%d\n",orom
->vpa
);
2538 printf("IMSM_MAX_VOLUMES_PER_CONTROLLER=%d\n",orom
->vphba
);
2541 static int detail_platform_imsm(int verbose
, int enumerate_only
, char *controller_path
)
2543 /* There are two components to imsm platform support, the ahci SATA
2544 * controller and the option-rom. To find the SATA controller we
2545 * simply look in /sys/bus/pci/drivers/ahci to see if an ahci
2546 * controller with the Intel vendor id is present. This approach
2547 * allows mdadm to leverage the kernel's ahci detection logic, with the
2548 * caveat that if ahci.ko is not loaded mdadm will not be able to
2549 * detect platform raid capabilities. The option-rom resides in a
2550 * platform "Adapter ROM". We scan for its signature to retrieve the
2551 * platform capabilities. If raid support is disabled in the BIOS the
2552 * option-rom capability structure will not be available.
2554 struct sys_dev
*list
, *hba
;
2559 if (enumerate_only
) {
2560 if (check_env("IMSM_NO_PLATFORM"))
2562 list
= find_intel_devices();
2565 for (hba
= list
; hba
; hba
= hba
->next
) {
2566 if (find_imsm_capability(hba
)) {
2576 list
= find_intel_devices();
2579 pr_err("no active Intel(R) RAID controller found.\n");
2581 } else if (verbose
> 0)
2582 print_found_intel_controllers(list
);
2584 for (hba
= list
; hba
; hba
= hba
->next
) {
2585 if (controller_path
&& (compare_paths(hba
->path
, controller_path
) != 0))
2587 if (!find_imsm_capability(hba
)) {
2589 pr_err("imsm capabilities not found for controller: %s (type %s)\n",
2590 hba
->type
== SYS_DEV_VMD
? vmd_domain_to_controller(hba
, buf
) : hba
->path
,
2591 get_sys_dev_type(hba
->type
));
2597 if (controller_path
&& result
== 1) {
2598 pr_err("no active Intel(R) RAID controller found under %s\n",
2603 const struct orom_entry
*entry
;
2605 for (entry
= orom_entries
; entry
; entry
= entry
->next
) {
2606 if (entry
->type
== SYS_DEV_VMD
) {
2607 print_imsm_capability(&entry
->orom
);
2608 printf(" 3rd party NVMe :%s supported\n",
2609 imsm_orom_has_tpv_support(&entry
->orom
)?"":" not");
2610 for (hba
= list
; hba
; hba
= hba
->next
) {
2611 if (hba
->type
== SYS_DEV_VMD
) {
2613 printf(" I/O Controller : %s (%s)\n",
2614 vmd_domain_to_controller(hba
, buf
), get_sys_dev_type(hba
->type
));
2615 if (print_vmd_attached_devs(hba
)) {
2617 pr_err("failed to get devices attached to VMD domain.\n");
2626 print_imsm_capability(&entry
->orom
);
2627 if (entry
->type
== SYS_DEV_NVME
) {
2628 for (hba
= list
; hba
; hba
= hba
->next
) {
2629 if (hba
->type
== SYS_DEV_NVME
)
2630 printf(" NVMe Device : %s\n", hba
->path
);
2636 struct devid_list
*devid
;
2637 for (devid
= entry
->devid_list
; devid
; devid
= devid
->next
) {
2638 hba
= device_by_id(devid
->devid
);
2642 printf(" I/O Controller : %s (%s)\n",
2643 hba
->path
, get_sys_dev_type(hba
->type
));
2644 if (hba
->type
== SYS_DEV_SATA
) {
2645 host_base
= ahci_get_port_count(hba
->path
, &port_count
);
2646 if (ahci_enumerate_ports(hba
->path
, port_count
, host_base
, verbose
)) {
2648 pr_err("failed to enumerate ports on SATA controller at %s.\n", hba
->pci_id
);
2659 static int export_detail_platform_imsm(int verbose
, char *controller_path
)
2661 struct sys_dev
*list
, *hba
;
2664 list
= find_intel_devices();
2667 pr_err("IMSM_DETAIL_PLATFORM_ERROR=NO_INTEL_DEVICES\n");
2672 for (hba
= list
; hba
; hba
= hba
->next
) {
2673 if (controller_path
&& (compare_paths(hba
->path
,controller_path
) != 0))
2675 if (!find_imsm_capability(hba
) && verbose
> 0) {
2677 pr_err("IMSM_DETAIL_PLATFORM_ERROR=NO_IMSM_CAPABLE_DEVICE_UNDER_%s\n",
2678 hba
->type
== SYS_DEV_VMD
? vmd_domain_to_controller(hba
, buf
) : hba
->path
);
2684 const struct orom_entry
*entry
;
2686 for (entry
= orom_entries
; entry
; entry
= entry
->next
) {
2687 if (entry
->type
== SYS_DEV_VMD
) {
2688 for (hba
= list
; hba
; hba
= hba
->next
)
2689 print_imsm_capability_export(&entry
->orom
);
2692 print_imsm_capability_export(&entry
->orom
);
2698 static int match_home_imsm(struct supertype
*st
, char *homehost
)
2700 /* the imsm metadata format does not specify any host
2701 * identification information. We return -1 since we can never
2702 * confirm nor deny whether a given array is "meant" for this
2703 * host. We rely on compare_super and the 'family_num' fields to
2704 * exclude member disks that do not belong, and we rely on
2705 * mdadm.conf to specify the arrays that should be assembled.
2706 * Auto-assembly may still pick up "foreign" arrays.
2712 static void uuid_from_super_imsm(struct supertype
*st
, int uuid
[4])
2714 /* The uuid returned here is used for:
2715 * uuid to put into bitmap file (Create, Grow)
2716 * uuid for backup header when saving critical section (Grow)
2717 * comparing uuids when re-adding a device into an array
2718 * In these cases the uuid required is that of the data-array,
2719 * not the device-set.
2720 * uuid to recognise same set when adding a missing device back
2721 * to an array. This is a uuid for the device-set.
2723 * For each of these we can make do with a truncated
2724 * or hashed uuid rather than the original, as long as
2726 * In each case the uuid required is that of the data-array,
2727 * not the device-set.
2729 /* imsm does not track uuid's so we synthesis one using sha1 on
2730 * - The signature (Which is constant for all imsm array, but no matter)
2731 * - the orig_family_num of the container
2732 * - the index number of the volume
2733 * - the 'serial' number of the volume.
2734 * Hopefully these are all constant.
2736 struct intel_super
*super
= st
->sb
;
2739 struct sha1_ctx ctx
;
2740 struct imsm_dev
*dev
= NULL
;
2743 /* some mdadm versions failed to set ->orig_family_num, in which
2744 * case fall back to ->family_num. orig_family_num will be
2745 * fixed up with the first metadata update.
2747 family_num
= super
->anchor
->orig_family_num
;
2748 if (family_num
== 0)
2749 family_num
= super
->anchor
->family_num
;
2750 sha1_init_ctx(&ctx
);
2751 sha1_process_bytes(super
->anchor
->sig
, MPB_SIG_LEN
, &ctx
);
2752 sha1_process_bytes(&family_num
, sizeof(__u32
), &ctx
);
2753 if (super
->current_vol
>= 0)
2754 dev
= get_imsm_dev(super
, super
->current_vol
);
2756 __u32 vol
= super
->current_vol
;
2757 sha1_process_bytes(&vol
, sizeof(vol
), &ctx
);
2758 sha1_process_bytes(dev
->volume
, MAX_RAID_SERIAL_LEN
, &ctx
);
2760 sha1_finish_ctx(&ctx
, buf
);
2761 memcpy(uuid
, buf
, 4*4);
2766 get_imsm_numerical_version(struct imsm_super
*mpb
, int *m
, int *p
)
2768 __u8
*v
= get_imsm_version(mpb
);
2769 __u8
*end
= mpb
->sig
+ MAX_SIGNATURE_LENGTH
;
2770 char major
[] = { 0, 0, 0 };
2771 char minor
[] = { 0 ,0, 0 };
2772 char patch
[] = { 0, 0, 0 };
2773 char *ver_parse
[] = { major
, minor
, patch
};
2777 while (*v
!= '\0' && v
< end
) {
2778 if (*v
!= '.' && j
< 2)
2779 ver_parse
[i
][j
++] = *v
;
2787 *m
= strtol(minor
, NULL
, 0);
2788 *p
= strtol(patch
, NULL
, 0);
2792 static __u32
migr_strip_blocks_resync(struct imsm_dev
*dev
)
2794 /* migr_strip_size when repairing or initializing parity */
2795 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
2796 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
2798 switch (get_imsm_raid_level(map
)) {
2803 return 128*1024 >> 9;
2807 static __u32
migr_strip_blocks_rebuild(struct imsm_dev
*dev
)
2809 /* migr_strip_size when rebuilding a degraded disk, no idea why
2810 * this is different than migr_strip_size_resync(), but it's good
2813 struct imsm_map
*map
= get_imsm_map(dev
, MAP_1
);
2814 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
2816 switch (get_imsm_raid_level(map
)) {
2819 if (map
->num_members
% map
->num_domains
== 0)
2820 return 128*1024 >> 9;
2824 return max((__u32
) 64*1024 >> 9, chunk
);
2826 return 128*1024 >> 9;
2830 static __u32
num_stripes_per_unit_resync(struct imsm_dev
*dev
)
2832 struct imsm_map
*lo
= get_imsm_map(dev
, MAP_0
);
2833 struct imsm_map
*hi
= get_imsm_map(dev
, MAP_1
);
2834 __u32 lo_chunk
= __le32_to_cpu(lo
->blocks_per_strip
);
2835 __u32 hi_chunk
= __le32_to_cpu(hi
->blocks_per_strip
);
2837 return max((__u32
) 1, hi_chunk
/ lo_chunk
);
2840 static __u32
num_stripes_per_unit_rebuild(struct imsm_dev
*dev
)
2842 struct imsm_map
*lo
= get_imsm_map(dev
, MAP_0
);
2843 int level
= get_imsm_raid_level(lo
);
2845 if (level
== 1 || level
== 10) {
2846 struct imsm_map
*hi
= get_imsm_map(dev
, MAP_1
);
2848 return hi
->num_domains
;
2850 return num_stripes_per_unit_resync(dev
);
2853 static __u8
imsm_num_data_members(struct imsm_map
*map
)
2855 /* named 'imsm_' because raid0, raid1 and raid10
2856 * counter-intuitively have the same number of data disks
2858 switch (get_imsm_raid_level(map
)) {
2860 return map
->num_members
;
2864 return map
->num_members
/2;
2866 return map
->num_members
- 1;
2868 dprintf("unsupported raid level\n");
2873 static unsigned long long calc_component_size(struct imsm_map
*map
,
2874 struct imsm_dev
*dev
)
2876 unsigned long long component_size
;
2877 unsigned long long dev_size
= imsm_dev_size(dev
);
2878 long long calc_dev_size
= 0;
2879 unsigned int member_disks
= imsm_num_data_members(map
);
2881 if (member_disks
== 0)
2884 component_size
= per_dev_array_size(map
);
2885 calc_dev_size
= component_size
* member_disks
;
2887 /* Component size is rounded to 1MB so difference between size from
2888 * metadata and size calculated from num_data_stripes equals up to
2889 * 2048 blocks per each device. If the difference is higher it means
2890 * that array size was expanded and num_data_stripes was not updated.
2892 if (llabs(calc_dev_size
- (long long)dev_size
) >
2893 (1 << SECT_PER_MB_SHIFT
) * member_disks
) {
2894 component_size
= dev_size
/ member_disks
;
2895 dprintf("Invalid num_data_stripes in metadata; expected=%llu, found=%llu\n",
2896 component_size
/ map
->blocks_per_strip
,
2897 num_data_stripes(map
));
2900 return component_size
;
2903 static __u32
parity_segment_depth(struct imsm_dev
*dev
)
2905 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
2906 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
2908 switch(get_imsm_raid_level(map
)) {
2911 return chunk
* map
->num_domains
;
2913 return chunk
* map
->num_members
;
2919 static __u32
map_migr_block(struct imsm_dev
*dev
, __u32 block
)
2921 struct imsm_map
*map
= get_imsm_map(dev
, MAP_1
);
2922 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
2923 __u32 strip
= block
/ chunk
;
2925 switch (get_imsm_raid_level(map
)) {
2928 __u32 vol_strip
= (strip
* map
->num_domains
) + 1;
2929 __u32 vol_stripe
= vol_strip
/ map
->num_members
;
2931 return vol_stripe
* chunk
+ block
% chunk
;
2933 __u32 stripe
= strip
/ (map
->num_members
- 1);
2935 return stripe
* chunk
+ block
% chunk
;
2942 static __u64
blocks_per_migr_unit(struct intel_super
*super
,
2943 struct imsm_dev
*dev
)
2945 /* calculate the conversion factor between per member 'blocks'
2946 * (md/{resync,rebuild}_start) and imsm migration units, return
2947 * 0 for the 'not migrating' and 'unsupported migration' cases
2949 if (!dev
->vol
.migr_state
)
2952 switch (migr_type(dev
)) {
2953 case MIGR_GEN_MIGR
: {
2954 struct migr_record
*migr_rec
= super
->migr_rec
;
2955 return __le32_to_cpu(migr_rec
->blocks_per_unit
);
2960 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
2961 __u32 stripes_per_unit
;
2962 __u32 blocks_per_unit
;
2971 /* yes, this is really the translation of migr_units to
2972 * per-member blocks in the 'resync' case
2974 stripes_per_unit
= num_stripes_per_unit_resync(dev
);
2975 migr_chunk
= migr_strip_blocks_resync(dev
);
2976 disks
= imsm_num_data_members(map
);
2977 blocks_per_unit
= stripes_per_unit
* migr_chunk
* disks
;
2978 stripe
= __le16_to_cpu(map
->blocks_per_strip
) * disks
;
2979 segment
= blocks_per_unit
/ stripe
;
2980 block_rel
= blocks_per_unit
- segment
* stripe
;
2981 parity_depth
= parity_segment_depth(dev
);
2982 block_map
= map_migr_block(dev
, block_rel
);
2983 return block_map
+ parity_depth
* segment
;
2985 case MIGR_REBUILD
: {
2986 __u32 stripes_per_unit
;
2989 stripes_per_unit
= num_stripes_per_unit_rebuild(dev
);
2990 migr_chunk
= migr_strip_blocks_rebuild(dev
);
2991 return migr_chunk
* stripes_per_unit
;
2993 case MIGR_STATE_CHANGE
:
2999 static int imsm_level_to_layout(int level
)
3007 return ALGORITHM_LEFT_ASYMMETRIC
;
3014 /*******************************************************************************
3015 * Function: read_imsm_migr_rec
3016 * Description: Function reads imsm migration record from last sector of disk
3018 * fd : disk descriptor
3019 * super : metadata info
3023 ******************************************************************************/
3024 static int read_imsm_migr_rec(int fd
, struct intel_super
*super
)
3027 unsigned int sector_size
= super
->sector_size
;
3028 unsigned long long dsize
;
3030 get_dev_size(fd
, NULL
, &dsize
);
3031 if (lseek64(fd
, dsize
- (sector_size
*MIGR_REC_SECTOR_POSITION
),
3033 pr_err("Cannot seek to anchor block: %s\n",
3037 if ((unsigned int)read(fd
, super
->migr_rec_buf
,
3038 MIGR_REC_BUF_SECTORS
*sector_size
) !=
3039 MIGR_REC_BUF_SECTORS
*sector_size
) {
3040 pr_err("Cannot read migr record block: %s\n",
3045 if (sector_size
== 4096)
3046 convert_from_4k_imsm_migr_rec(super
);
3052 static struct imsm_dev
*imsm_get_device_during_migration(
3053 struct intel_super
*super
)
3056 struct intel_dev
*dv
;
3058 for (dv
= super
->devlist
; dv
; dv
= dv
->next
) {
3059 if (is_gen_migration(dv
->dev
))
3065 /*******************************************************************************
3066 * Function: load_imsm_migr_rec
3067 * Description: Function reads imsm migration record (it is stored at the last
3070 * super : imsm internal array info
3071 * info : general array info
3075 * -2 : no migration in progress
3076 ******************************************************************************/
3077 static int load_imsm_migr_rec(struct intel_super
*super
, struct mdinfo
*info
)
3084 struct imsm_dev
*dev
;
3085 struct imsm_map
*map
;
3088 /* find map under migration */
3089 dev
= imsm_get_device_during_migration(super
);
3090 /* nothing to load,no migration in progress?
3096 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
3097 /* read only from one of the first two slots */
3098 if ((sd
->disk
.raid_disk
< 0) ||
3099 (sd
->disk
.raid_disk
> 1))
3102 sprintf(nm
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
3103 fd
= dev_open(nm
, O_RDONLY
);
3109 map
= get_imsm_map(dev
, MAP_0
);
3110 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
3111 /* skip spare and failed disks
3115 /* read only from one of the first two slots */
3117 slot
= get_imsm_disk_slot(map
, dl
->index
);
3118 if (map
== NULL
|| slot
> 1 || slot
< 0)
3120 sprintf(nm
, "%d:%d", dl
->major
, dl
->minor
);
3121 fd
= dev_open(nm
, O_RDONLY
);
3128 retval
= read_imsm_migr_rec(fd
, super
);
3136 /*******************************************************************************
3137 * function: imsm_create_metadata_checkpoint_update
3138 * Description: It creates update for checkpoint change.
3140 * super : imsm internal array info
3141 * u : pointer to prepared update
3144 * If length is equal to 0, input pointer u contains no update
3145 ******************************************************************************/
3146 static int imsm_create_metadata_checkpoint_update(
3147 struct intel_super
*super
,
3148 struct imsm_update_general_migration_checkpoint
**u
)
3151 int update_memory_size
= 0;
3153 dprintf("(enter)\n");
3159 /* size of all update data without anchor */
3160 update_memory_size
=
3161 sizeof(struct imsm_update_general_migration_checkpoint
);
3163 *u
= xcalloc(1, update_memory_size
);
3165 dprintf("error: cannot get memory\n");
3168 (*u
)->type
= update_general_migration_checkpoint
;
3169 (*u
)->curr_migr_unit
= current_migr_unit(super
->migr_rec
);
3170 dprintf("prepared for %u\n", (*u
)->curr_migr_unit
);
3172 return update_memory_size
;
3175 static void imsm_update_metadata_locally(struct supertype
*st
,
3176 void *buf
, int len
);
3178 /*******************************************************************************
3179 * Function: write_imsm_migr_rec
3180 * Description: Function writes imsm migration record
3181 * (at the last sector of disk)
3183 * super : imsm internal array info
3187 ******************************************************************************/
3188 static int write_imsm_migr_rec(struct supertype
*st
)
3190 struct intel_super
*super
= st
->sb
;
3191 unsigned int sector_size
= super
->sector_size
;
3192 unsigned long long dsize
;
3198 struct imsm_update_general_migration_checkpoint
*u
;
3199 struct imsm_dev
*dev
;
3200 struct imsm_map
*map
;
3202 /* find map under migration */
3203 dev
= imsm_get_device_during_migration(super
);
3204 /* if no migration, write buffer anyway to clear migr_record
3205 * on disk based on first available device
3208 dev
= get_imsm_dev(super
, super
->current_vol
< 0 ? 0 :
3209 super
->current_vol
);
3211 map
= get_imsm_map(dev
, MAP_0
);
3213 if (sector_size
== 4096)
3214 convert_to_4k_imsm_migr_rec(super
);
3215 for (sd
= super
->disks
; sd
; sd
= sd
->next
) {
3218 /* skip failed and spare devices */
3221 /* write to 2 first slots only */
3223 slot
= get_imsm_disk_slot(map
, sd
->index
);
3224 if (map
== NULL
|| slot
> 1 || slot
< 0)
3227 sprintf(nm
, "%d:%d", sd
->major
, sd
->minor
);
3228 fd
= dev_open(nm
, O_RDWR
);
3231 get_dev_size(fd
, NULL
, &dsize
);
3232 if (lseek64(fd
, dsize
- (MIGR_REC_SECTOR_POSITION
*sector_size
),
3234 pr_err("Cannot seek to anchor block: %s\n",
3238 if ((unsigned int)write(fd
, super
->migr_rec_buf
,
3239 MIGR_REC_BUF_SECTORS
*sector_size
) !=
3240 MIGR_REC_BUF_SECTORS
*sector_size
) {
3241 pr_err("Cannot write migr record block: %s\n",
3248 if (sector_size
== 4096)
3249 convert_from_4k_imsm_migr_rec(super
);
3250 /* update checkpoint information in metadata */
3251 len
= imsm_create_metadata_checkpoint_update(super
, &u
);
3253 dprintf("imsm: Cannot prepare update\n");
3256 /* update metadata locally */
3257 imsm_update_metadata_locally(st
, u
, len
);
3258 /* and possibly remotely */
3259 if (st
->update_tail
) {
3260 append_metadata_update(st
, u
, len
);
3261 /* during reshape we do all work inside metadata handler
3262 * manage_reshape(), so metadata update has to be triggered
3265 flush_metadata_updates(st
);
3266 st
->update_tail
= &st
->updates
;
3277 /* spare/missing disks activations are not allowe when
3278 * array/container performs reshape operation, because
3279 * all arrays in container works on the same disks set
3281 int imsm_reshape_blocks_arrays_changes(struct intel_super
*super
)
3284 struct intel_dev
*i_dev
;
3285 struct imsm_dev
*dev
;
3287 /* check whole container
3289 for (i_dev
= super
->devlist
; i_dev
; i_dev
= i_dev
->next
) {
3291 if (is_gen_migration(dev
)) {
3292 /* No repair during any migration in container
3300 static unsigned long long imsm_component_size_alignment_check(int level
,
3302 unsigned int sector_size
,
3303 unsigned long long component_size
)
3305 unsigned int component_size_alignment
;
3307 /* check component size alignment
3309 component_size_alignment
= component_size
% (chunk_size
/sector_size
);
3311 dprintf("(Level: %i, chunk_size = %i, component_size = %llu), component_size_alignment = %u\n",
3312 level
, chunk_size
, component_size
,
3313 component_size_alignment
);
3315 if (component_size_alignment
&& (level
!= 1) && (level
!= UnSet
)) {
3316 dprintf("imsm: reported component size aligned from %llu ",
3318 component_size
-= component_size_alignment
;
3319 dprintf_cont("to %llu (%i).\n",
3320 component_size
, component_size_alignment
);
3323 return component_size
;
3326 static unsigned long long get_ppl_sector(struct intel_super
*super
, int dev_idx
)
3328 struct imsm_dev
*dev
= get_imsm_dev(super
, dev_idx
);
3329 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
3331 return pba_of_lba0(map
) +
3332 (num_data_stripes(map
) * map
->blocks_per_strip
);
3335 static void getinfo_super_imsm_volume(struct supertype
*st
, struct mdinfo
*info
, char *dmap
)
3337 struct intel_super
*super
= st
->sb
;
3338 struct migr_record
*migr_rec
= super
->migr_rec
;
3339 struct imsm_dev
*dev
= get_imsm_dev(super
, super
->current_vol
);
3340 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
3341 struct imsm_map
*prev_map
= get_imsm_map(dev
, MAP_1
);
3342 struct imsm_map
*map_to_analyse
= map
;
3344 int map_disks
= info
->array
.raid_disks
;
3346 memset(info
, 0, sizeof(*info
));
3348 map_to_analyse
= prev_map
;
3350 dl
= super
->current_disk
;
3352 info
->container_member
= super
->current_vol
;
3353 info
->array
.raid_disks
= map
->num_members
;
3354 info
->array
.level
= get_imsm_raid_level(map_to_analyse
);
3355 info
->array
.layout
= imsm_level_to_layout(info
->array
.level
);
3356 info
->array
.md_minor
= -1;
3357 info
->array
.ctime
= 0;
3358 info
->array
.utime
= 0;
3359 info
->array
.chunk_size
=
3360 __le16_to_cpu(map_to_analyse
->blocks_per_strip
) << 9;
3361 info
->array
.state
= !(dev
->vol
.dirty
& RAIDVOL_DIRTY
);
3362 info
->custom_array_size
= imsm_dev_size(dev
);
3363 info
->recovery_blocked
= imsm_reshape_blocks_arrays_changes(st
->sb
);
3365 if (is_gen_migration(dev
)) {
3366 info
->reshape_active
= 1;
3367 info
->new_level
= get_imsm_raid_level(map
);
3368 info
->new_layout
= imsm_level_to_layout(info
->new_level
);
3369 info
->new_chunk
= __le16_to_cpu(map
->blocks_per_strip
) << 9;
3370 info
->delta_disks
= map
->num_members
- prev_map
->num_members
;
3371 if (info
->delta_disks
) {
3372 /* this needs to be applied to every array
3375 info
->reshape_active
= CONTAINER_RESHAPE
;
3377 /* We shape information that we give to md might have to be
3378 * modify to cope with md's requirement for reshaping arrays.
3379 * For example, when reshaping a RAID0, md requires it to be
3380 * presented as a degraded RAID4.
3381 * Also if a RAID0 is migrating to a RAID5 we need to specify
3382 * the array as already being RAID5, but the 'before' layout
3383 * is a RAID4-like layout.
3385 switch (info
->array
.level
) {
3387 switch(info
->new_level
) {
3389 /* conversion is happening as RAID4 */
3390 info
->array
.level
= 4;
3391 info
->array
.raid_disks
+= 1;
3394 /* conversion is happening as RAID5 */
3395 info
->array
.level
= 5;
3396 info
->array
.layout
= ALGORITHM_PARITY_N
;
3397 info
->delta_disks
-= 1;
3400 /* FIXME error message */
3401 info
->array
.level
= UnSet
;
3407 info
->new_level
= UnSet
;
3408 info
->new_layout
= UnSet
;
3409 info
->new_chunk
= info
->array
.chunk_size
;
3410 info
->delta_disks
= 0;
3414 info
->disk
.major
= dl
->major
;
3415 info
->disk
.minor
= dl
->minor
;
3416 info
->disk
.number
= dl
->index
;
3417 info
->disk
.raid_disk
= get_imsm_disk_slot(map_to_analyse
,
3421 info
->data_offset
= pba_of_lba0(map_to_analyse
);
3422 info
->component_size
= calc_component_size(map
, dev
);
3423 info
->component_size
= imsm_component_size_alignment_check(
3425 info
->array
.chunk_size
,
3427 info
->component_size
);
3428 info
->bb
.supported
= 1;
3430 memset(info
->uuid
, 0, sizeof(info
->uuid
));
3431 info
->recovery_start
= MaxSector
;
3433 if (info
->array
.level
== 5 &&
3434 (dev
->rwh_policy
== RWH_DISTRIBUTED
||
3435 dev
->rwh_policy
== RWH_MULTIPLE_DISTRIBUTED
)) {
3436 info
->consistency_policy
= CONSISTENCY_POLICY_PPL
;
3437 info
->ppl_sector
= get_ppl_sector(super
, super
->current_vol
);
3438 if (dev
->rwh_policy
== RWH_MULTIPLE_DISTRIBUTED
)
3439 info
->ppl_size
= MULTIPLE_PPL_AREA_SIZE_IMSM
>> 9;
3441 info
->ppl_size
= (PPL_HEADER_SIZE
+ PPL_ENTRY_SPACE
)
3443 } else if (info
->array
.level
<= 0) {
3444 info
->consistency_policy
= CONSISTENCY_POLICY_NONE
;
3446 info
->consistency_policy
= CONSISTENCY_POLICY_RESYNC
;
3449 info
->reshape_progress
= 0;
3450 info
->resync_start
= MaxSector
;
3451 if ((map_to_analyse
->map_state
== IMSM_T_STATE_UNINITIALIZED
||
3452 !(info
->array
.state
& 1)) &&
3453 imsm_reshape_blocks_arrays_changes(super
) == 0) {
3454 info
->resync_start
= 0;
3456 if (dev
->vol
.migr_state
) {
3457 switch (migr_type(dev
)) {
3460 __u64 blocks_per_unit
= blocks_per_migr_unit(super
,
3462 __u64 units
= __le32_to_cpu(dev
->vol
.curr_migr_unit
);
3464 info
->resync_start
= blocks_per_unit
* units
;
3467 case MIGR_GEN_MIGR
: {
3468 __u64 blocks_per_unit
= blocks_per_migr_unit(super
,
3470 __u64 units
= current_migr_unit(migr_rec
);
3471 unsigned long long array_blocks
;
3474 if (__le32_to_cpu(migr_rec
->ascending_migr
) &&
3476 (get_num_migr_units(migr_rec
)-1)) &&
3477 (super
->migr_rec
->rec_status
==
3478 __cpu_to_le32(UNIT_SRC_IN_CP_AREA
)))
3481 info
->reshape_progress
= blocks_per_unit
* units
;
3483 dprintf("IMSM: General Migration checkpoint : %llu (%llu) -> read reshape progress : %llu\n",
3484 (unsigned long long)units
,
3485 (unsigned long long)blocks_per_unit
,
3486 info
->reshape_progress
);
3488 used_disks
= imsm_num_data_members(prev_map
);
3489 if (used_disks
> 0) {
3490 array_blocks
= per_dev_array_size(map
) *
3492 info
->custom_array_size
=
3493 round_size_to_mb(array_blocks
,
3499 /* we could emulate the checkpointing of
3500 * 'sync_action=check' migrations, but for now
3501 * we just immediately complete them
3504 /* this is handled by container_content_imsm() */
3505 case MIGR_STATE_CHANGE
:
3506 /* FIXME handle other migrations */
3508 /* we are not dirty, so... */
3509 info
->resync_start
= MaxSector
;
3513 strncpy(info
->name
, (char *) dev
->volume
, MAX_RAID_SERIAL_LEN
);
3514 info
->name
[MAX_RAID_SERIAL_LEN
] = 0;
3516 info
->array
.major_version
= -1;
3517 info
->array
.minor_version
= -2;
3518 sprintf(info
->text_version
, "/%s/%d", st
->container_devnm
, info
->container_member
);
3519 info
->safe_mode_delay
= 4000; /* 4 secs like the Matrix driver */
3520 uuid_from_super_imsm(st
, info
->uuid
);
3524 for (i
=0; i
<map_disks
; i
++) {
3526 if (i
< info
->array
.raid_disks
) {
3527 struct imsm_disk
*dsk
;
3528 j
= get_imsm_disk_idx(dev
, i
, MAP_X
);
3529 dsk
= get_imsm_disk(super
, j
);
3530 if (dsk
&& (dsk
->status
& CONFIGURED_DISK
))
3537 static __u8
imsm_check_degraded(struct intel_super
*super
, struct imsm_dev
*dev
,
3538 int failed
, int look_in_map
);
3540 static int imsm_count_failed(struct intel_super
*super
, struct imsm_dev
*dev
,
3543 static void manage_second_map(struct intel_super
*super
, struct imsm_dev
*dev
)
3545 if (is_gen_migration(dev
)) {
3548 struct imsm_map
*map2
= get_imsm_map(dev
, MAP_1
);
3550 failed
= imsm_count_failed(super
, dev
, MAP_1
);
3551 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_1
);
3552 if (map2
->map_state
!= map_state
) {
3553 map2
->map_state
= map_state
;
3554 super
->updates_pending
++;
3559 static struct imsm_disk
*get_imsm_missing(struct intel_super
*super
, __u8 index
)
3563 for (d
= super
->missing
; d
; d
= d
->next
)
3564 if (d
->index
== index
)
3569 static void getinfo_super_imsm(struct supertype
*st
, struct mdinfo
*info
, char *map
)
3571 struct intel_super
*super
= st
->sb
;
3572 struct imsm_disk
*disk
;
3573 int map_disks
= info
->array
.raid_disks
;
3574 int max_enough
= -1;
3576 struct imsm_super
*mpb
;
3578 if (super
->current_vol
>= 0) {
3579 getinfo_super_imsm_volume(st
, info
, map
);
3582 memset(info
, 0, sizeof(*info
));
3584 /* Set raid_disks to zero so that Assemble will always pull in valid
3587 info
->array
.raid_disks
= 0;
3588 info
->array
.level
= LEVEL_CONTAINER
;
3589 info
->array
.layout
= 0;
3590 info
->array
.md_minor
= -1;
3591 info
->array
.ctime
= 0; /* N/A for imsm */
3592 info
->array
.utime
= 0;
3593 info
->array
.chunk_size
= 0;
3595 info
->disk
.major
= 0;
3596 info
->disk
.minor
= 0;
3597 info
->disk
.raid_disk
= -1;
3598 info
->reshape_active
= 0;
3599 info
->array
.major_version
= -1;
3600 info
->array
.minor_version
= -2;
3601 strcpy(info
->text_version
, "imsm");
3602 info
->safe_mode_delay
= 0;
3603 info
->disk
.number
= -1;
3604 info
->disk
.state
= 0;
3606 info
->recovery_start
= MaxSector
;
3607 info
->recovery_blocked
= imsm_reshape_blocks_arrays_changes(st
->sb
);
3608 info
->bb
.supported
= 1;
3610 /* do we have the all the insync disks that we expect? */
3611 mpb
= super
->anchor
;
3612 info
->events
= __le32_to_cpu(mpb
->generation_num
);
3614 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
3615 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
3616 int failed
, enough
, j
, missing
= 0;
3617 struct imsm_map
*map
;
3620 failed
= imsm_count_failed(super
, dev
, MAP_0
);
3621 state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
3622 map
= get_imsm_map(dev
, MAP_0
);
3624 /* any newly missing disks?
3625 * (catches single-degraded vs double-degraded)
3627 for (j
= 0; j
< map
->num_members
; j
++) {
3628 __u32 ord
= get_imsm_ord_tbl_ent(dev
, j
, MAP_0
);
3629 __u32 idx
= ord_to_idx(ord
);
3631 if (super
->disks
&& super
->disks
->index
== (int)idx
)
3632 info
->disk
.raid_disk
= j
;
3634 if (!(ord
& IMSM_ORD_REBUILD
) &&
3635 get_imsm_missing(super
, idx
)) {
3641 if (state
== IMSM_T_STATE_FAILED
)
3643 else if (state
== IMSM_T_STATE_DEGRADED
&&
3644 (state
!= map
->map_state
|| missing
))
3646 else /* we're normal, or already degraded */
3648 if (is_gen_migration(dev
) && missing
) {
3649 /* during general migration we need all disks
3650 * that process is running on.
3651 * No new missing disk is allowed.
3655 /* no more checks necessary
3659 /* in the missing/failed disk case check to see
3660 * if at least one array is runnable
3662 max_enough
= max(max_enough
, enough
);
3664 dprintf("enough: %d\n", max_enough
);
3665 info
->container_enough
= max_enough
;
3668 __u32 reserved
= imsm_reserved_sectors(super
, super
->disks
);
3670 disk
= &super
->disks
->disk
;
3671 info
->data_offset
= total_blocks(&super
->disks
->disk
) - reserved
;
3672 info
->component_size
= reserved
;
3673 info
->disk
.state
= is_configured(disk
) ? (1 << MD_DISK_ACTIVE
) : 0;
3674 /* we don't change info->disk.raid_disk here because
3675 * this state will be finalized in mdmon after we have
3676 * found the 'most fresh' version of the metadata
3678 info
->disk
.state
|= is_failed(disk
) ? (1 << MD_DISK_FAULTY
) : 0;
3679 info
->disk
.state
|= (is_spare(disk
) || is_journal(disk
)) ?
3680 0 : (1 << MD_DISK_SYNC
);
3683 /* only call uuid_from_super_imsm when this disk is part of a populated container,
3684 * ->compare_super may have updated the 'num_raid_devs' field for spares
3686 if (info
->disk
.state
& (1 << MD_DISK_SYNC
) || super
->anchor
->num_raid_devs
)
3687 uuid_from_super_imsm(st
, info
->uuid
);
3689 memcpy(info
->uuid
, uuid_zero
, sizeof(uuid_zero
));
3691 /* I don't know how to compute 'map' on imsm, so use safe default */
3694 for (i
= 0; i
< map_disks
; i
++)
3700 /* allocates memory and fills disk in mdinfo structure
3701 * for each disk in array */
3702 struct mdinfo
*getinfo_super_disks_imsm(struct supertype
*st
)
3704 struct mdinfo
*mddev
;
3705 struct intel_super
*super
= st
->sb
;
3706 struct imsm_disk
*disk
;
3709 if (!super
|| !super
->disks
)
3712 mddev
= xcalloc(1, sizeof(*mddev
));
3716 tmp
= xcalloc(1, sizeof(*tmp
));
3718 tmp
->next
= mddev
->devs
;
3720 tmp
->disk
.number
= count
++;
3721 tmp
->disk
.major
= dl
->major
;
3722 tmp
->disk
.minor
= dl
->minor
;
3723 tmp
->disk
.state
= is_configured(disk
) ?
3724 (1 << MD_DISK_ACTIVE
) : 0;
3725 tmp
->disk
.state
|= is_failed(disk
) ? (1 << MD_DISK_FAULTY
) : 0;
3726 tmp
->disk
.state
|= is_spare(disk
) ? 0 : (1 << MD_DISK_SYNC
);
3727 tmp
->disk
.raid_disk
= -1;
3733 static int update_super_imsm(struct supertype
*st
, struct mdinfo
*info
,
3734 char *update
, char *devname
, int verbose
,
3735 int uuid_set
, char *homehost
)
3737 /* For 'assemble' and 'force' we need to return non-zero if any
3738 * change was made. For others, the return value is ignored.
3739 * Update options are:
3740 * force-one : This device looks a bit old but needs to be included,
3741 * update age info appropriately.
3742 * assemble: clear any 'faulty' flag to allow this device to
3744 * force-array: Array is degraded but being forced, mark it clean
3745 * if that will be needed to assemble it.
3747 * newdev: not used ????
3748 * grow: Array has gained a new device - this is currently for
3750 * resync: mark as dirty so a resync will happen.
3751 * name: update the name - preserving the homehost
3752 * uuid: Change the uuid of the array to match watch is given
3754 * Following are not relevant for this imsm:
3755 * sparc2.2 : update from old dodgey metadata
3756 * super-minor: change the preferred_minor number
3757 * summaries: update redundant counters.
3758 * homehost: update the recorded homehost
3759 * _reshape_progress: record new reshape_progress position.
3762 struct intel_super
*super
= st
->sb
;
3763 struct imsm_super
*mpb
;
3765 /* we can only update container info */
3766 if (!super
|| super
->current_vol
>= 0 || !super
->anchor
)
3769 mpb
= super
->anchor
;
3771 if (strcmp(update
, "uuid") == 0) {
3772 /* We take this to mean that the family_num should be updated.
3773 * However that is much smaller than the uuid so we cannot really
3774 * allow an explicit uuid to be given. And it is hard to reliably
3776 * So if !uuid_set we know the current uuid is random and just used
3777 * the first 'int' and copy it to the other 3 positions.
3778 * Otherwise we require the 4 'int's to be the same as would be the
3779 * case if we are using a random uuid. So an explicit uuid will be
3780 * accepted as long as all for ints are the same... which shouldn't hurt
3783 info
->uuid
[1] = info
->uuid
[2] = info
->uuid
[3] = info
->uuid
[0];
3786 if (info
->uuid
[0] != info
->uuid
[1] ||
3787 info
->uuid
[1] != info
->uuid
[2] ||
3788 info
->uuid
[2] != info
->uuid
[3])
3794 mpb
->orig_family_num
= info
->uuid
[0];
3795 } else if (strcmp(update
, "assemble") == 0)
3800 /* successful update? recompute checksum */
3802 mpb
->check_sum
= __le32_to_cpu(__gen_imsm_checksum(mpb
));
3807 static size_t disks_to_mpb_size(int disks
)
3811 size
= sizeof(struct imsm_super
);
3812 size
+= (disks
- 1) * sizeof(struct imsm_disk
);
3813 size
+= 2 * sizeof(struct imsm_dev
);
3814 /* up to 2 maps per raid device (-2 for imsm_maps in imsm_dev */
3815 size
+= (4 - 2) * sizeof(struct imsm_map
);
3816 /* 4 possible disk_ord_tbl's */
3817 size
+= 4 * (disks
- 1) * sizeof(__u32
);
3818 /* maximum bbm log */
3819 size
+= sizeof(struct bbm_log
);
3824 static __u64
avail_size_imsm(struct supertype
*st
, __u64 devsize
,
3825 unsigned long long data_offset
)
3827 if (devsize
< (MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
))
3830 return devsize
- (MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
);
3833 static void free_devlist(struct intel_super
*super
)
3835 struct intel_dev
*dv
;
3837 while (super
->devlist
) {
3838 dv
= super
->devlist
->next
;
3839 free(super
->devlist
->dev
);
3840 free(super
->devlist
);
3841 super
->devlist
= dv
;
3845 static void imsm_copy_dev(struct imsm_dev
*dest
, struct imsm_dev
*src
)
3847 memcpy(dest
, src
, sizeof_imsm_dev(src
, 0));
3850 static int compare_super_imsm(struct supertype
*st
, struct supertype
*tst
)
3854 * 0 same, or first was empty, and second was copied
3855 * 1 second had wrong number
3857 * 3 wrong other info
3859 struct intel_super
*first
= st
->sb
;
3860 struct intel_super
*sec
= tst
->sb
;
3867 /* in platform dependent environment test if the disks
3868 * use the same Intel hba
3869 * If not on Intel hba at all, allow anything.
3871 if (!check_env("IMSM_NO_PLATFORM") && first
->hba
&& sec
->hba
) {
3872 if (first
->hba
->type
!= sec
->hba
->type
) {
3874 "HBAs of devices do not match %s != %s\n",
3875 get_sys_dev_type(first
->hba
->type
),
3876 get_sys_dev_type(sec
->hba
->type
));
3879 if (first
->orom
!= sec
->orom
) {
3881 "HBAs of devices do not match %s != %s\n",
3882 first
->hba
->pci_id
, sec
->hba
->pci_id
);
3887 /* if an anchor does not have num_raid_devs set then it is a free
3890 if (first
->anchor
->num_raid_devs
> 0 &&
3891 sec
->anchor
->num_raid_devs
> 0) {
3892 /* Determine if these disks might ever have been
3893 * related. Further disambiguation can only take place
3894 * in load_super_imsm_all
3896 __u32 first_family
= first
->anchor
->orig_family_num
;
3897 __u32 sec_family
= sec
->anchor
->orig_family_num
;
3899 if (memcmp(first
->anchor
->sig
, sec
->anchor
->sig
,
3900 MAX_SIGNATURE_LENGTH
) != 0)
3903 if (first_family
== 0)
3904 first_family
= first
->anchor
->family_num
;
3905 if (sec_family
== 0)
3906 sec_family
= sec
->anchor
->family_num
;
3908 if (first_family
!= sec_family
)
3913 /* if 'first' is a spare promote it to a populated mpb with sec's
3916 if (first
->anchor
->num_raid_devs
== 0 &&
3917 sec
->anchor
->num_raid_devs
> 0) {
3919 struct intel_dev
*dv
;
3920 struct imsm_dev
*dev
;
3922 /* we need to copy raid device info from sec if an allocation
3923 * fails here we don't associate the spare
3925 for (i
= 0; i
< sec
->anchor
->num_raid_devs
; i
++) {
3926 dv
= xmalloc(sizeof(*dv
));
3927 dev
= xmalloc(sizeof_imsm_dev(get_imsm_dev(sec
, i
), 1));
3930 dv
->next
= first
->devlist
;
3931 first
->devlist
= dv
;
3933 if (i
< sec
->anchor
->num_raid_devs
) {
3934 /* allocation failure */
3935 free_devlist(first
);
3936 pr_err("imsm: failed to associate spare\n");
3939 first
->anchor
->num_raid_devs
= sec
->anchor
->num_raid_devs
;
3940 first
->anchor
->orig_family_num
= sec
->anchor
->orig_family_num
;
3941 first
->anchor
->family_num
= sec
->anchor
->family_num
;
3942 memcpy(first
->anchor
->sig
, sec
->anchor
->sig
, MAX_SIGNATURE_LENGTH
);
3943 for (i
= 0; i
< sec
->anchor
->num_raid_devs
; i
++)
3944 imsm_copy_dev(get_imsm_dev(first
, i
), get_imsm_dev(sec
, i
));
3950 static void fd2devname(int fd
, char *name
)
3954 char dname
[PATH_MAX
];
3959 if (fstat(fd
, &st
) != 0)
3961 sprintf(path
, "/sys/dev/block/%d:%d",
3962 major(st
.st_rdev
), minor(st
.st_rdev
));
3964 rv
= readlink(path
, dname
, sizeof(dname
)-1);
3969 nm
= strrchr(dname
, '/');
3972 snprintf(name
, MAX_RAID_SERIAL_LEN
, "/dev/%s", nm
);
3976 static int nvme_get_serial(int fd
, void *buf
, size_t buf_len
)
3979 char *name
= fd2kname(fd
);
3984 if (strncmp(name
, "nvme", 4) != 0)
3987 snprintf(path
, sizeof(path
) - 1, "/sys/block/%s/device/serial", name
);
3989 return load_sys(path
, buf
, buf_len
);
3992 extern int scsi_get_serial(int fd
, void *buf
, size_t buf_len
);
3994 static int imsm_read_serial(int fd
, char *devname
,
3995 __u8 serial
[MAX_RAID_SERIAL_LEN
])
4004 memset(buf
, 0, sizeof(buf
));
4006 rv
= nvme_get_serial(fd
, buf
, sizeof(buf
));
4009 rv
= scsi_get_serial(fd
, buf
, sizeof(buf
));
4011 if (rv
&& check_env("IMSM_DEVNAME_AS_SERIAL")) {
4012 memset(serial
, 0, MAX_RAID_SERIAL_LEN
);
4013 fd2devname(fd
, (char *) serial
);
4019 pr_err("Failed to retrieve serial for %s\n",
4024 /* trim all whitespace and non-printable characters and convert
4027 for (i
= 0, dest
= buf
; i
< sizeof(buf
) && buf
[i
]; i
++) {
4030 /* ':' is reserved for use in placeholder serial
4031 * numbers for missing disks
4042 /* truncate leading characters */
4043 if (len
> MAX_RAID_SERIAL_LEN
) {
4044 dest
+= len
- MAX_RAID_SERIAL_LEN
;
4045 len
= MAX_RAID_SERIAL_LEN
;
4048 memset(serial
, 0, MAX_RAID_SERIAL_LEN
);
4049 memcpy(serial
, dest
, len
);
4054 static int serialcmp(__u8
*s1
, __u8
*s2
)
4056 return strncmp((char *) s1
, (char *) s2
, MAX_RAID_SERIAL_LEN
);
4059 static void serialcpy(__u8
*dest
, __u8
*src
)
4061 strncpy((char *) dest
, (char *) src
, MAX_RAID_SERIAL_LEN
);
4064 static struct dl
*serial_to_dl(__u8
*serial
, struct intel_super
*super
)
4068 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
4069 if (serialcmp(dl
->serial
, serial
) == 0)
4075 static struct imsm_disk
*
4076 __serial_to_disk(__u8
*serial
, struct imsm_super
*mpb
, int *idx
)
4080 for (i
= 0; i
< mpb
->num_disks
; i
++) {
4081 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, i
);
4083 if (serialcmp(disk
->serial
, serial
) == 0) {
4094 load_imsm_disk(int fd
, struct intel_super
*super
, char *devname
, int keep_fd
)
4096 struct imsm_disk
*disk
;
4101 __u8 serial
[MAX_RAID_SERIAL_LEN
];
4103 rv
= imsm_read_serial(fd
, devname
, serial
);
4108 dl
= xcalloc(1, sizeof(*dl
));
4111 dl
->major
= major(stb
.st_rdev
);
4112 dl
->minor
= minor(stb
.st_rdev
);
4113 dl
->next
= super
->disks
;
4114 dl
->fd
= keep_fd
? fd
: -1;
4115 assert(super
->disks
== NULL
);
4117 serialcpy(dl
->serial
, serial
);
4120 fd2devname(fd
, name
);
4122 dl
->devname
= xstrdup(devname
);
4124 dl
->devname
= xstrdup(name
);
4126 /* look up this disk's index in the current anchor */
4127 disk
= __serial_to_disk(dl
->serial
, super
->anchor
, &dl
->index
);
4130 /* only set index on disks that are a member of a
4131 * populated contianer, i.e. one with raid_devs
4133 if (is_failed(&dl
->disk
))
4135 else if (is_spare(&dl
->disk
) || is_journal(&dl
->disk
))
4142 /* When migrating map0 contains the 'destination' state while map1
4143 * contains the current state. When not migrating map0 contains the
4144 * current state. This routine assumes that map[0].map_state is set to
4145 * the current array state before being called.
4147 * Migration is indicated by one of the following states
4148 * 1/ Idle (migr_state=0 map0state=normal||unitialized||degraded||failed)
4149 * 2/ Initialize (migr_state=1 migr_type=MIGR_INIT map0state=normal
4150 * map1state=unitialized)
4151 * 3/ Repair (Resync) (migr_state=1 migr_type=MIGR_REPAIR map0state=normal
4153 * 4/ Rebuild (migr_state=1 migr_type=MIGR_REBUILD map0state=normal
4154 * map1state=degraded)
4155 * 5/ Migration (mig_state=1 migr_type=MIGR_GEN_MIGR map0state=normal
4158 static void migrate(struct imsm_dev
*dev
, struct intel_super
*super
,
4159 __u8 to_state
, int migr_type
)
4161 struct imsm_map
*dest
;
4162 struct imsm_map
*src
= get_imsm_map(dev
, MAP_0
);
4164 dev
->vol
.migr_state
= 1;
4165 set_migr_type(dev
, migr_type
);
4166 dev
->vol
.curr_migr_unit
= 0;
4167 dest
= get_imsm_map(dev
, MAP_1
);
4169 /* duplicate and then set the target end state in map[0] */
4170 memcpy(dest
, src
, sizeof_imsm_map(src
));
4171 if (migr_type
== MIGR_GEN_MIGR
) {
4175 for (i
= 0; i
< src
->num_members
; i
++) {
4176 ord
= __le32_to_cpu(src
->disk_ord_tbl
[i
]);
4177 set_imsm_ord_tbl_ent(src
, i
, ord_to_idx(ord
));
4181 if (migr_type
== MIGR_GEN_MIGR
)
4182 /* Clear migration record */
4183 memset(super
->migr_rec
, 0, sizeof(struct migr_record
));
4185 src
->map_state
= to_state
;
4188 static void end_migration(struct imsm_dev
*dev
, struct intel_super
*super
,
4191 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
4192 struct imsm_map
*prev
= get_imsm_map(dev
, dev
->vol
.migr_state
== 0 ?
4196 /* merge any IMSM_ORD_REBUILD bits that were not successfully
4197 * completed in the last migration.
4199 * FIXME add support for raid-level-migration
4201 if (map_state
!= map
->map_state
&& (is_gen_migration(dev
) == 0) &&
4202 prev
->map_state
!= IMSM_T_STATE_UNINITIALIZED
) {
4203 /* when final map state is other than expected
4204 * merge maps (not for migration)
4208 for (i
= 0; i
< prev
->num_members
; i
++)
4209 for (j
= 0; j
< map
->num_members
; j
++)
4210 /* during online capacity expansion
4211 * disks position can be changed
4212 * if takeover is used
4214 if (ord_to_idx(map
->disk_ord_tbl
[j
]) ==
4215 ord_to_idx(prev
->disk_ord_tbl
[i
])) {
4216 map
->disk_ord_tbl
[j
] |=
4217 prev
->disk_ord_tbl
[i
];
4220 failed
= imsm_count_failed(super
, dev
, MAP_0
);
4221 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
4224 dev
->vol
.migr_state
= 0;
4225 set_migr_type(dev
, 0);
4226 dev
->vol
.curr_migr_unit
= 0;
4227 map
->map_state
= map_state
;
4230 static int parse_raid_devices(struct intel_super
*super
)
4233 struct imsm_dev
*dev_new
;
4234 size_t len
, len_migr
;
4236 size_t space_needed
= 0;
4237 struct imsm_super
*mpb
= super
->anchor
;
4239 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
4240 struct imsm_dev
*dev_iter
= __get_imsm_dev(super
->anchor
, i
);
4241 struct intel_dev
*dv
;
4243 len
= sizeof_imsm_dev(dev_iter
, 0);
4244 len_migr
= sizeof_imsm_dev(dev_iter
, 1);
4246 space_needed
+= len_migr
- len
;
4248 dv
= xmalloc(sizeof(*dv
));
4249 if (max_len
< len_migr
)
4251 if (max_len
> len_migr
)
4252 space_needed
+= max_len
- len_migr
;
4253 dev_new
= xmalloc(max_len
);
4254 imsm_copy_dev(dev_new
, dev_iter
);
4257 dv
->next
= super
->devlist
;
4258 super
->devlist
= dv
;
4261 /* ensure that super->buf is large enough when all raid devices
4264 if (__le32_to_cpu(mpb
->mpb_size
) + space_needed
> super
->len
) {
4267 len
= ROUND_UP(__le32_to_cpu(mpb
->mpb_size
) + space_needed
,
4268 super
->sector_size
);
4269 if (posix_memalign(&buf
, MAX_SECTOR_SIZE
, len
) != 0)
4272 memcpy(buf
, super
->buf
, super
->len
);
4273 memset(buf
+ super
->len
, 0, len
- super
->len
);
4279 super
->extra_space
+= space_needed
;
4284 /*******************************************************************************
4285 * Function: check_mpb_migr_compatibility
4286 * Description: Function checks for unsupported migration features:
4287 * - migration optimization area (pba_of_lba0)
4288 * - descending reshape (ascending_migr)
4290 * super : imsm metadata information
4292 * 0 : migration is compatible
4293 * -1 : migration is not compatible
4294 ******************************************************************************/
4295 int check_mpb_migr_compatibility(struct intel_super
*super
)
4297 struct imsm_map
*map0
, *map1
;
4298 struct migr_record
*migr_rec
= super
->migr_rec
;
4301 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
4302 struct imsm_dev
*dev_iter
= __get_imsm_dev(super
->anchor
, i
);
4305 dev_iter
->vol
.migr_state
== 1 &&
4306 dev_iter
->vol
.migr_type
== MIGR_GEN_MIGR
) {
4307 /* This device is migrating */
4308 map0
= get_imsm_map(dev_iter
, MAP_0
);
4309 map1
= get_imsm_map(dev_iter
, MAP_1
);
4310 if (pba_of_lba0(map0
) != pba_of_lba0(map1
))
4311 /* migration optimization area was used */
4313 if (migr_rec
->ascending_migr
== 0 &&
4314 migr_rec
->dest_depth_per_unit
> 0)
4315 /* descending reshape not supported yet */
4322 static void __free_imsm(struct intel_super
*super
, int free_disks
);
4324 /* load_imsm_mpb - read matrix metadata
4325 * allocates super->mpb to be freed by free_imsm
4327 static int load_imsm_mpb(int fd
, struct intel_super
*super
, char *devname
)
4329 unsigned long long dsize
;
4330 unsigned long long sectors
;
4331 unsigned int sector_size
= super
->sector_size
;
4333 struct imsm_super
*anchor
;
4336 get_dev_size(fd
, NULL
, &dsize
);
4337 if (dsize
< 2*sector_size
) {
4339 pr_err("%s: device to small for imsm\n",
4344 if (lseek64(fd
, dsize
- (sector_size
* 2), SEEK_SET
) < 0) {
4346 pr_err("Cannot seek to anchor block on %s: %s\n",
4347 devname
, strerror(errno
));
4351 if (posix_memalign((void **)&anchor
, sector_size
, sector_size
) != 0) {
4353 pr_err("Failed to allocate imsm anchor buffer on %s\n", devname
);
4356 if ((unsigned int)read(fd
, anchor
, sector_size
) != sector_size
) {
4358 pr_err("Cannot read anchor block on %s: %s\n",
4359 devname
, strerror(errno
));
4364 if (strncmp((char *) anchor
->sig
, MPB_SIGNATURE
, MPB_SIG_LEN
) != 0) {
4366 pr_err("no IMSM anchor on %s\n", devname
);
4371 __free_imsm(super
, 0);
4372 /* reload capability and hba */
4374 /* capability and hba must be updated with new super allocation */
4375 find_intel_hba_capability(fd
, super
, devname
);
4376 super
->len
= ROUND_UP(anchor
->mpb_size
, sector_size
);
4377 if (posix_memalign(&super
->buf
, MAX_SECTOR_SIZE
, super
->len
) != 0) {
4379 pr_err("unable to allocate %zu byte mpb buffer\n",
4384 memcpy(super
->buf
, anchor
, sector_size
);
4386 sectors
= mpb_sectors(anchor
, sector_size
) - 1;
4389 if (posix_memalign(&super
->migr_rec_buf
, MAX_SECTOR_SIZE
,
4390 MIGR_REC_BUF_SECTORS
*MAX_SECTOR_SIZE
) != 0) {
4391 pr_err("could not allocate migr_rec buffer\n");
4395 super
->clean_migration_record_by_mdmon
= 0;
4398 check_sum
= __gen_imsm_checksum(super
->anchor
);
4399 if (check_sum
!= __le32_to_cpu(super
->anchor
->check_sum
)) {
4401 pr_err("IMSM checksum %x != %x on %s\n",
4403 __le32_to_cpu(super
->anchor
->check_sum
),
4411 /* read the extended mpb */
4412 if (lseek64(fd
, dsize
- (sector_size
* (2 + sectors
)), SEEK_SET
) < 0) {
4414 pr_err("Cannot seek to extended mpb on %s: %s\n",
4415 devname
, strerror(errno
));
4419 if ((unsigned int)read(fd
, super
->buf
+ sector_size
,
4420 super
->len
- sector_size
) != super
->len
- sector_size
) {
4422 pr_err("Cannot read extended mpb on %s: %s\n",
4423 devname
, strerror(errno
));
4427 check_sum
= __gen_imsm_checksum(super
->anchor
);
4428 if (check_sum
!= __le32_to_cpu(super
->anchor
->check_sum
)) {
4430 pr_err("IMSM checksum %x != %x on %s\n",
4431 check_sum
, __le32_to_cpu(super
->anchor
->check_sum
),
4439 static int read_imsm_migr_rec(int fd
, struct intel_super
*super
);
4441 /* clears hi bits in metadata if MPB_ATTRIB_2TB_DISK not set */
4442 static void clear_hi(struct intel_super
*super
)
4444 struct imsm_super
*mpb
= super
->anchor
;
4446 if (mpb
->attributes
& MPB_ATTRIB_2TB_DISK
)
4448 for (i
= 0; i
< mpb
->num_disks
; ++i
) {
4449 struct imsm_disk
*disk
= &mpb
->disk
[i
];
4450 disk
->total_blocks_hi
= 0;
4452 for (i
= 0; i
< mpb
->num_raid_devs
; ++i
) {
4453 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
4456 for (n
= 0; n
< 2; ++n
) {
4457 struct imsm_map
*map
= get_imsm_map(dev
, n
);
4460 map
->pba_of_lba0_hi
= 0;
4461 map
->blocks_per_member_hi
= 0;
4462 map
->num_data_stripes_hi
= 0;
4468 load_and_parse_mpb(int fd
, struct intel_super
*super
, char *devname
, int keep_fd
)
4472 err
= load_imsm_mpb(fd
, super
, devname
);
4475 if (super
->sector_size
== 4096)
4476 convert_from_4k(super
);
4477 err
= load_imsm_disk(fd
, super
, devname
, keep_fd
);
4480 err
= parse_raid_devices(super
);
4483 err
= load_bbm_log(super
);
4488 static void __free_imsm_disk(struct dl
*d
)
4500 static void free_imsm_disks(struct intel_super
*super
)
4504 while (super
->disks
) {
4506 super
->disks
= d
->next
;
4507 __free_imsm_disk(d
);
4509 while (super
->disk_mgmt_list
) {
4510 d
= super
->disk_mgmt_list
;
4511 super
->disk_mgmt_list
= d
->next
;
4512 __free_imsm_disk(d
);
4514 while (super
->missing
) {
4516 super
->missing
= d
->next
;
4517 __free_imsm_disk(d
);
4522 /* free all the pieces hanging off of a super pointer */
4523 static void __free_imsm(struct intel_super
*super
, int free_disks
)
4525 struct intel_hba
*elem
, *next
;
4531 /* unlink capability description */
4533 if (super
->migr_rec_buf
) {
4534 free(super
->migr_rec_buf
);
4535 super
->migr_rec_buf
= NULL
;
4538 free_imsm_disks(super
);
4539 free_devlist(super
);
4543 free((void *)elem
->path
);
4549 free(super
->bbm_log
);
4553 static void free_imsm(struct intel_super
*super
)
4555 __free_imsm(super
, 1);
4556 free(super
->bb
.entries
);
4560 static void free_super_imsm(struct supertype
*st
)
4562 struct intel_super
*super
= st
->sb
;
4571 static struct intel_super
*alloc_super(void)
4573 struct intel_super
*super
= xcalloc(1, sizeof(*super
));
4575 super
->current_vol
= -1;
4576 super
->create_offset
= ~((unsigned long long) 0);
4578 super
->bb
.entries
= xmalloc(BBM_LOG_MAX_ENTRIES
*
4579 sizeof(struct md_bb_entry
));
4580 if (!super
->bb
.entries
) {
4589 * find and allocate hba and OROM/EFI based on valid fd of RAID component device
4591 static int find_intel_hba_capability(int fd
, struct intel_super
*super
, char *devname
)
4593 struct sys_dev
*hba_name
;
4596 if (fd
>= 0 && test_partition(fd
)) {
4597 pr_err("imsm: %s is a partition, cannot be used in IMSM\n",
4601 if (fd
< 0 || check_env("IMSM_NO_PLATFORM")) {
4606 hba_name
= find_disk_attached_hba(fd
, NULL
);
4609 pr_err("%s is not attached to Intel(R) RAID controller.\n",
4613 rv
= attach_hba_to_super(super
, hba_name
);
4616 struct intel_hba
*hba
= super
->hba
;
4618 pr_err("%s is attached to Intel(R) %s %s (%s),\n"
4619 " but the container is assigned to Intel(R) %s %s (",
4621 get_sys_dev_type(hba_name
->type
),
4622 hba_name
->type
== SYS_DEV_VMD
? "domain" : "RAID controller",
4623 hba_name
->pci_id
? : "Err!",
4624 get_sys_dev_type(super
->hba
->type
),
4625 hba
->type
== SYS_DEV_VMD
? "domain" : "RAID controller");
4628 fprintf(stderr
, "%s", hba
->pci_id
? : "Err!");
4630 fprintf(stderr
, ", ");
4633 fprintf(stderr
, ").\n"
4634 " Mixing devices attached to different controllers is not allowed.\n");
4638 super
->orom
= find_imsm_capability(hba_name
);
4645 /* find_missing - helper routine for load_super_imsm_all that identifies
4646 * disks that have disappeared from the system. This routine relies on
4647 * the mpb being uptodate, which it is at load time.
4649 static int find_missing(struct intel_super
*super
)
4652 struct imsm_super
*mpb
= super
->anchor
;
4654 struct imsm_disk
*disk
;
4656 for (i
= 0; i
< mpb
->num_disks
; i
++) {
4657 disk
= __get_imsm_disk(mpb
, i
);
4658 dl
= serial_to_dl(disk
->serial
, super
);
4662 dl
= xmalloc(sizeof(*dl
));
4666 dl
->devname
= xstrdup("missing");
4668 serialcpy(dl
->serial
, disk
->serial
);
4671 dl
->next
= super
->missing
;
4672 super
->missing
= dl
;
4678 static struct intel_disk
*disk_list_get(__u8
*serial
, struct intel_disk
*disk_list
)
4680 struct intel_disk
*idisk
= disk_list
;
4683 if (serialcmp(idisk
->disk
.serial
, serial
) == 0)
4685 idisk
= idisk
->next
;
4691 static int __prep_thunderdome(struct intel_super
**table
, int tbl_size
,
4692 struct intel_super
*super
,
4693 struct intel_disk
**disk_list
)
4695 struct imsm_disk
*d
= &super
->disks
->disk
;
4696 struct imsm_super
*mpb
= super
->anchor
;
4699 for (i
= 0; i
< tbl_size
; i
++) {
4700 struct imsm_super
*tbl_mpb
= table
[i
]->anchor
;
4701 struct imsm_disk
*tbl_d
= &table
[i
]->disks
->disk
;
4703 if (tbl_mpb
->family_num
== mpb
->family_num
) {
4704 if (tbl_mpb
->check_sum
== mpb
->check_sum
) {
4705 dprintf("mpb from %d:%d matches %d:%d\n",
4706 super
->disks
->major
,
4707 super
->disks
->minor
,
4708 table
[i
]->disks
->major
,
4709 table
[i
]->disks
->minor
);
4713 if (((is_configured(d
) && !is_configured(tbl_d
)) ||
4714 is_configured(d
) == is_configured(tbl_d
)) &&
4715 tbl_mpb
->generation_num
< mpb
->generation_num
) {
4716 /* current version of the mpb is a
4717 * better candidate than the one in
4718 * super_table, but copy over "cross
4719 * generational" status
4721 struct intel_disk
*idisk
;
4723 dprintf("mpb from %d:%d replaces %d:%d\n",
4724 super
->disks
->major
,
4725 super
->disks
->minor
,
4726 table
[i
]->disks
->major
,
4727 table
[i
]->disks
->minor
);
4729 idisk
= disk_list_get(tbl_d
->serial
, *disk_list
);
4730 if (idisk
&& is_failed(&idisk
->disk
))
4731 tbl_d
->status
|= FAILED_DISK
;
4734 struct intel_disk
*idisk
;
4735 struct imsm_disk
*disk
;
4737 /* tbl_mpb is more up to date, but copy
4738 * over cross generational status before
4741 disk
= __serial_to_disk(d
->serial
, mpb
, NULL
);
4742 if (disk
&& is_failed(disk
))
4743 d
->status
|= FAILED_DISK
;
4745 idisk
= disk_list_get(d
->serial
, *disk_list
);
4748 if (disk
&& is_configured(disk
))
4749 idisk
->disk
.status
|= CONFIGURED_DISK
;
4752 dprintf("mpb from %d:%d prefer %d:%d\n",
4753 super
->disks
->major
,
4754 super
->disks
->minor
,
4755 table
[i
]->disks
->major
,
4756 table
[i
]->disks
->minor
);
4764 table
[tbl_size
++] = super
;
4768 /* update/extend the merged list of imsm_disk records */
4769 for (j
= 0; j
< mpb
->num_disks
; j
++) {
4770 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, j
);
4771 struct intel_disk
*idisk
;
4773 idisk
= disk_list_get(disk
->serial
, *disk_list
);
4775 idisk
->disk
.status
|= disk
->status
;
4776 if (is_configured(&idisk
->disk
) ||
4777 is_failed(&idisk
->disk
))
4778 idisk
->disk
.status
&= ~(SPARE_DISK
);
4780 idisk
= xcalloc(1, sizeof(*idisk
));
4781 idisk
->owner
= IMSM_UNKNOWN_OWNER
;
4782 idisk
->disk
= *disk
;
4783 idisk
->next
= *disk_list
;
4787 if (serialcmp(idisk
->disk
.serial
, d
->serial
) == 0)
4794 static struct intel_super
*
4795 validate_members(struct intel_super
*super
, struct intel_disk
*disk_list
,
4798 struct imsm_super
*mpb
= super
->anchor
;
4802 for (i
= 0; i
< mpb
->num_disks
; i
++) {
4803 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, i
);
4804 struct intel_disk
*idisk
;
4806 idisk
= disk_list_get(disk
->serial
, disk_list
);
4808 if (idisk
->owner
== owner
||
4809 idisk
->owner
== IMSM_UNKNOWN_OWNER
)
4812 dprintf("'%.16s' owner %d != %d\n",
4813 disk
->serial
, idisk
->owner
,
4816 dprintf("unknown disk %x [%d]: %.16s\n",
4817 __le32_to_cpu(mpb
->family_num
), i
,
4823 if (ok_count
== mpb
->num_disks
)
4828 static void show_conflicts(__u32 family_num
, struct intel_super
*super_list
)
4830 struct intel_super
*s
;
4832 for (s
= super_list
; s
; s
= s
->next
) {
4833 if (family_num
!= s
->anchor
->family_num
)
4835 pr_err("Conflict, offlining family %#x on '%s'\n",
4836 __le32_to_cpu(family_num
), s
->disks
->devname
);
4840 static struct intel_super
*
4841 imsm_thunderdome(struct intel_super
**super_list
, int len
)
4843 struct intel_super
*super_table
[len
];
4844 struct intel_disk
*disk_list
= NULL
;
4845 struct intel_super
*champion
, *spare
;
4846 struct intel_super
*s
, **del
;
4851 memset(super_table
, 0, sizeof(super_table
));
4852 for (s
= *super_list
; s
; s
= s
->next
)
4853 tbl_size
= __prep_thunderdome(super_table
, tbl_size
, s
, &disk_list
);
4855 for (i
= 0; i
< tbl_size
; i
++) {
4856 struct imsm_disk
*d
;
4857 struct intel_disk
*idisk
;
4858 struct imsm_super
*mpb
= super_table
[i
]->anchor
;
4861 d
= &s
->disks
->disk
;
4863 /* 'd' must appear in merged disk list for its
4864 * configuration to be valid
4866 idisk
= disk_list_get(d
->serial
, disk_list
);
4867 if (idisk
&& idisk
->owner
== i
)
4868 s
= validate_members(s
, disk_list
, i
);
4873 dprintf("marking family: %#x from %d:%d offline\n",
4875 super_table
[i
]->disks
->major
,
4876 super_table
[i
]->disks
->minor
);
4880 /* This is where the mdadm implementation differs from the Windows
4881 * driver which has no strict concept of a container. We can only
4882 * assemble one family from a container, so when returning a prodigal
4883 * array member to this system the code will not be able to disambiguate
4884 * the container contents that should be assembled ("foreign" versus
4885 * "local"). It requires user intervention to set the orig_family_num
4886 * to a new value to establish a new container. The Windows driver in
4887 * this situation fixes up the volume name in place and manages the
4888 * foreign array as an independent entity.
4893 for (i
= 0; i
< tbl_size
; i
++) {
4894 struct intel_super
*tbl_ent
= super_table
[i
];
4900 if (tbl_ent
->anchor
->num_raid_devs
== 0) {
4905 if (s
&& !is_spare
) {
4906 show_conflicts(tbl_ent
->anchor
->family_num
, *super_list
);
4908 } else if (!s
&& !is_spare
)
4921 pr_err("Chose family %#x on '%s', assemble conflicts to new container with '--update=uuid'\n",
4922 __le32_to_cpu(s
->anchor
->family_num
), s
->disks
->devname
);
4924 /* collect all dl's onto 'champion', and update them to
4925 * champion's version of the status
4927 for (s
= *super_list
; s
; s
= s
->next
) {
4928 struct imsm_super
*mpb
= champion
->anchor
;
4929 struct dl
*dl
= s
->disks
;
4934 mpb
->attributes
|= s
->anchor
->attributes
& MPB_ATTRIB_2TB_DISK
;
4936 for (i
= 0; i
< mpb
->num_disks
; i
++) {
4937 struct imsm_disk
*disk
;
4939 disk
= __serial_to_disk(dl
->serial
, mpb
, &dl
->index
);
4942 /* only set index on disks that are a member of
4943 * a populated contianer, i.e. one with
4946 if (is_failed(&dl
->disk
))
4948 else if (is_spare(&dl
->disk
))
4954 if (i
>= mpb
->num_disks
) {
4955 struct intel_disk
*idisk
;
4957 idisk
= disk_list_get(dl
->serial
, disk_list
);
4958 if (idisk
&& is_spare(&idisk
->disk
) &&
4959 !is_failed(&idisk
->disk
) && !is_configured(&idisk
->disk
))
4967 dl
->next
= champion
->disks
;
4968 champion
->disks
= dl
;
4972 /* delete 'champion' from super_list */
4973 for (del
= super_list
; *del
; ) {
4974 if (*del
== champion
) {
4975 *del
= (*del
)->next
;
4978 del
= &(*del
)->next
;
4980 champion
->next
= NULL
;
4984 struct intel_disk
*idisk
= disk_list
;
4986 disk_list
= disk_list
->next
;
4994 get_sra_super_block(int fd
, struct intel_super
**super_list
, char *devname
, int *max
, int keep_fd
);
4995 static int get_super_block(struct intel_super
**super_list
, char *devnm
, char *devname
,
4996 int major
, int minor
, int keep_fd
);
4998 get_devlist_super_block(struct md_list
*devlist
, struct intel_super
**super_list
,
4999 int *max
, int keep_fd
);
5001 static int load_super_imsm_all(struct supertype
*st
, int fd
, void **sbp
,
5002 char *devname
, struct md_list
*devlist
,
5005 struct intel_super
*super_list
= NULL
;
5006 struct intel_super
*super
= NULL
;
5011 /* 'fd' is an opened container */
5012 err
= get_sra_super_block(fd
, &super_list
, devname
, &i
, keep_fd
);
5014 /* get super block from devlist devices */
5015 err
= get_devlist_super_block(devlist
, &super_list
, &i
, keep_fd
);
5018 /* all mpbs enter, maybe one leaves */
5019 super
= imsm_thunderdome(&super_list
, i
);
5025 if (find_missing(super
) != 0) {
5031 /* load migration record */
5032 err
= load_imsm_migr_rec(super
, NULL
);
5034 /* migration is in progress,
5035 * but migr_rec cannot be loaded,
5041 /* Check migration compatibility */
5042 if (err
== 0 && check_mpb_migr_compatibility(super
) != 0) {
5043 pr_err("Unsupported migration detected");
5045 fprintf(stderr
, " on %s\n", devname
);
5047 fprintf(stderr
, " (IMSM).\n");
5056 while (super_list
) {
5057 struct intel_super
*s
= super_list
;
5059 super_list
= super_list
->next
;
5068 strcpy(st
->container_devnm
, fd2devnm(fd
));
5070 st
->container_devnm
[0] = 0;
5071 if (err
== 0 && st
->ss
== NULL
) {
5072 st
->ss
= &super_imsm
;
5073 st
->minor_version
= 0;
5074 st
->max_devs
= IMSM_MAX_DEVICES
;
5080 get_devlist_super_block(struct md_list
*devlist
, struct intel_super
**super_list
,
5081 int *max
, int keep_fd
)
5083 struct md_list
*tmpdev
;
5087 for (i
= 0, tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
5088 if (tmpdev
->used
!= 1)
5090 if (tmpdev
->container
== 1) {
5092 int fd
= dev_open(tmpdev
->devname
, O_RDONLY
|O_EXCL
);
5094 pr_err("cannot open device %s: %s\n",
5095 tmpdev
->devname
, strerror(errno
));
5099 err
= get_sra_super_block(fd
, super_list
,
5100 tmpdev
->devname
, &lmax
,
5109 int major
= major(tmpdev
->st_rdev
);
5110 int minor
= minor(tmpdev
->st_rdev
);
5111 err
= get_super_block(super_list
,
5128 static int get_super_block(struct intel_super
**super_list
, char *devnm
, char *devname
,
5129 int major
, int minor
, int keep_fd
)
5131 struct intel_super
*s
;
5143 sprintf(nm
, "%d:%d", major
, minor
);
5144 dfd
= dev_open(nm
, O_RDWR
);
5150 get_dev_sector_size(dfd
, NULL
, &s
->sector_size
);
5151 find_intel_hba_capability(dfd
, s
, devname
);
5152 err
= load_and_parse_mpb(dfd
, s
, NULL
, keep_fd
);
5154 /* retry the load if we might have raced against mdmon */
5155 if (err
== 3 && devnm
&& mdmon_running(devnm
))
5156 for (retry
= 0; retry
< 3; retry
++) {
5158 err
= load_and_parse_mpb(dfd
, s
, NULL
, keep_fd
);
5164 s
->next
= *super_list
;
5172 if (dfd
>= 0 && !keep_fd
)
5179 get_sra_super_block(int fd
, struct intel_super
**super_list
, char *devname
, int *max
, int keep_fd
)
5186 sra
= sysfs_read(fd
, NULL
, GET_LEVEL
|GET_VERSION
|GET_DEVS
|GET_STATE
);
5190 if (sra
->array
.major_version
!= -1 ||
5191 sra
->array
.minor_version
!= -2 ||
5192 strcmp(sra
->text_version
, "imsm") != 0) {
5197 devnm
= fd2devnm(fd
);
5198 for (sd
= sra
->devs
, i
= 0; sd
; sd
= sd
->next
, i
++) {
5199 if (get_super_block(super_list
, devnm
, devname
,
5200 sd
->disk
.major
, sd
->disk
.minor
, keep_fd
) != 0) {
5211 static int load_container_imsm(struct supertype
*st
, int fd
, char *devname
)
5213 return load_super_imsm_all(st
, fd
, &st
->sb
, devname
, NULL
, 1);
5216 static int load_super_imsm(struct supertype
*st
, int fd
, char *devname
)
5218 struct intel_super
*super
;
5222 if (test_partition(fd
))
5223 /* IMSM not allowed on partitions */
5226 free_super_imsm(st
);
5228 super
= alloc_super();
5229 get_dev_sector_size(fd
, NULL
, &super
->sector_size
);
5232 /* Load hba and capabilities if they exist.
5233 * But do not preclude loading metadata in case capabilities or hba are
5234 * non-compliant and ignore_hw_compat is set.
5236 rv
= find_intel_hba_capability(fd
, super
, devname
);
5237 /* no orom/efi or non-intel hba of the disk */
5238 if (rv
!= 0 && st
->ignore_hw_compat
== 0) {
5240 pr_err("No OROM/EFI properties for %s\n", devname
);
5244 rv
= load_and_parse_mpb(fd
, super
, devname
, 0);
5246 /* retry the load if we might have raced against mdmon */
5248 struct mdstat_ent
*mdstat
= NULL
;
5249 char *name
= fd2kname(fd
);
5252 mdstat
= mdstat_by_component(name
);
5254 if (mdstat
&& mdmon_running(mdstat
->devnm
) && getpid() != mdmon_pid(mdstat
->devnm
)) {
5255 for (retry
= 0; retry
< 3; retry
++) {
5257 rv
= load_and_parse_mpb(fd
, super
, devname
, 0);
5263 free_mdstat(mdstat
);
5268 pr_err("Failed to load all information sections on %s\n", devname
);
5274 if (st
->ss
== NULL
) {
5275 st
->ss
= &super_imsm
;
5276 st
->minor_version
= 0;
5277 st
->max_devs
= IMSM_MAX_DEVICES
;
5280 /* load migration record */
5281 if (load_imsm_migr_rec(super
, NULL
) == 0) {
5282 /* Check for unsupported migration features */
5283 if (check_mpb_migr_compatibility(super
) != 0) {
5284 pr_err("Unsupported migration detected");
5286 fprintf(stderr
, " on %s\n", devname
);
5288 fprintf(stderr
, " (IMSM).\n");
5296 static __u16
info_to_blocks_per_strip(mdu_array_info_t
*info
)
5298 if (info
->level
== 1)
5300 return info
->chunk_size
>> 9;
5303 static unsigned long long info_to_blocks_per_member(mdu_array_info_t
*info
,
5304 unsigned long long size
)
5306 if (info
->level
== 1)
5309 return (size
* 2) & ~(info_to_blocks_per_strip(info
) - 1);
5312 static void imsm_update_version_info(struct intel_super
*super
)
5314 /* update the version and attributes */
5315 struct imsm_super
*mpb
= super
->anchor
;
5317 struct imsm_dev
*dev
;
5318 struct imsm_map
*map
;
5321 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
5322 dev
= get_imsm_dev(super
, i
);
5323 map
= get_imsm_map(dev
, MAP_0
);
5324 if (__le32_to_cpu(dev
->size_high
) > 0)
5325 mpb
->attributes
|= MPB_ATTRIB_2TB
;
5327 /* FIXME detect when an array spans a port multiplier */
5329 mpb
->attributes
|= MPB_ATTRIB_PM
;
5332 if (mpb
->num_raid_devs
> 1 ||
5333 mpb
->attributes
!= MPB_ATTRIB_CHECKSUM_VERIFY
) {
5334 version
= MPB_VERSION_ATTRIBS
;
5335 switch (get_imsm_raid_level(map
)) {
5336 case 0: mpb
->attributes
|= MPB_ATTRIB_RAID0
; break;
5337 case 1: mpb
->attributes
|= MPB_ATTRIB_RAID1
; break;
5338 case 10: mpb
->attributes
|= MPB_ATTRIB_RAID10
; break;
5339 case 5: mpb
->attributes
|= MPB_ATTRIB_RAID5
; break;
5342 if (map
->num_members
>= 5)
5343 version
= MPB_VERSION_5OR6_DISK_ARRAY
;
5344 else if (dev
->status
== DEV_CLONE_N_GO
)
5345 version
= MPB_VERSION_CNG
;
5346 else if (get_imsm_raid_level(map
) == 5)
5347 version
= MPB_VERSION_RAID5
;
5348 else if (map
->num_members
>= 3)
5349 version
= MPB_VERSION_3OR4_DISK_ARRAY
;
5350 else if (get_imsm_raid_level(map
) == 1)
5351 version
= MPB_VERSION_RAID1
;
5353 version
= MPB_VERSION_RAID0
;
5355 strcpy(((char *) mpb
->sig
) + strlen(MPB_SIGNATURE
), version
);
5359 static int check_name(struct intel_super
*super
, char *name
, int quiet
)
5361 struct imsm_super
*mpb
= super
->anchor
;
5362 char *reason
= NULL
;
5364 size_t len
= strlen(name
);
5368 while (isspace(start
[len
- 1]))
5370 while (*start
&& isspace(*start
))
5372 memmove(name
, start
, len
+ 1);
5375 if (len
> MAX_RAID_SERIAL_LEN
)
5376 reason
= "must be 16 characters or less";
5378 reason
= "must be a non-empty string";
5380 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
5381 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
5383 if (strncmp((char *) dev
->volume
, name
, MAX_RAID_SERIAL_LEN
) == 0) {
5384 reason
= "already exists";
5389 if (reason
&& !quiet
)
5390 pr_err("imsm volume name %s\n", reason
);
5395 static int init_super_imsm_volume(struct supertype
*st
, mdu_array_info_t
*info
,
5396 struct shape
*s
, char *name
,
5397 char *homehost
, int *uuid
,
5398 long long data_offset
)
5400 /* We are creating a volume inside a pre-existing container.
5401 * so st->sb is already set.
5403 struct intel_super
*super
= st
->sb
;
5404 unsigned int sector_size
= super
->sector_size
;
5405 struct imsm_super
*mpb
= super
->anchor
;
5406 struct intel_dev
*dv
;
5407 struct imsm_dev
*dev
;
5408 struct imsm_vol
*vol
;
5409 struct imsm_map
*map
;
5410 int idx
= mpb
->num_raid_devs
;
5413 unsigned long long array_blocks
;
5414 size_t size_old
, size_new
;
5415 unsigned long long num_data_stripes
;
5416 unsigned int data_disks
;
5417 unsigned long long size_per_member
;
5419 if (super
->orom
&& mpb
->num_raid_devs
>= super
->orom
->vpa
) {
5420 pr_err("This imsm-container already has the maximum of %d volumes\n", super
->orom
->vpa
);
5424 /* ensure the mpb is large enough for the new data */
5425 size_old
= __le32_to_cpu(mpb
->mpb_size
);
5426 size_new
= disks_to_mpb_size(info
->nr_disks
);
5427 if (size_new
> size_old
) {
5429 size_t size_round
= ROUND_UP(size_new
, sector_size
);
5431 if (posix_memalign(&mpb_new
, sector_size
, size_round
) != 0) {
5432 pr_err("could not allocate new mpb\n");
5435 if (posix_memalign(&super
->migr_rec_buf
, MAX_SECTOR_SIZE
,
5436 MIGR_REC_BUF_SECTORS
*
5437 MAX_SECTOR_SIZE
) != 0) {
5438 pr_err("could not allocate migr_rec buffer\n");
5444 memcpy(mpb_new
, mpb
, size_old
);
5447 super
->anchor
= mpb_new
;
5448 mpb
->mpb_size
= __cpu_to_le32(size_new
);
5449 memset(mpb_new
+ size_old
, 0, size_round
- size_old
);
5450 super
->len
= size_round
;
5452 super
->current_vol
= idx
;
5454 /* handle 'failed_disks' by either:
5455 * a) create dummy disk entries in the table if this the first
5456 * volume in the array. We add them here as this is the only
5457 * opportunity to add them. add_to_super_imsm_volume()
5458 * handles the non-failed disks and continues incrementing
5460 * b) validate that 'failed_disks' matches the current number
5461 * of missing disks if the container is populated
5463 if (super
->current_vol
== 0) {
5465 for (i
= 0; i
< info
->failed_disks
; i
++) {
5466 struct imsm_disk
*disk
;
5469 disk
= __get_imsm_disk(mpb
, i
);
5470 disk
->status
= CONFIGURED_DISK
| FAILED_DISK
;
5471 disk
->scsi_id
= __cpu_to_le32(~(__u32
)0);
5472 snprintf((char *) disk
->serial
, MAX_RAID_SERIAL_LEN
,
5473 "missing:%d", (__u8
)i
);
5475 find_missing(super
);
5480 for (d
= super
->missing
; d
; d
= d
->next
)
5482 if (info
->failed_disks
> missing
) {
5483 pr_err("unable to add 'missing' disk to container\n");
5488 if (!check_name(super
, name
, 0))
5490 dv
= xmalloc(sizeof(*dv
));
5491 dev
= xcalloc(1, sizeof(*dev
) + sizeof(__u32
) * (info
->raid_disks
- 1));
5493 * Explicitly allow truncating to not confuse gcc's
5494 * -Werror=stringop-truncation
5496 namelen
= min((int) strlen(name
), MAX_RAID_SERIAL_LEN
);
5497 memcpy(dev
->volume
, name
, namelen
);
5498 array_blocks
= calc_array_size(info
->level
, info
->raid_disks
,
5499 info
->layout
, info
->chunk_size
,
5500 s
->size
* BLOCKS_PER_KB
);
5501 data_disks
= get_data_disks(info
->level
, info
->layout
,
5503 array_blocks
= round_size_to_mb(array_blocks
, data_disks
);
5504 size_per_member
= array_blocks
/ data_disks
;
5506 set_imsm_dev_size(dev
, array_blocks
);
5507 dev
->status
= (DEV_READ_COALESCING
| DEV_WRITE_COALESCING
);
5509 vol
->migr_state
= 0;
5510 set_migr_type(dev
, MIGR_INIT
);
5511 vol
->dirty
= !info
->state
;
5512 vol
->curr_migr_unit
= 0;
5513 map
= get_imsm_map(dev
, MAP_0
);
5514 set_pba_of_lba0(map
, super
->create_offset
);
5515 map
->blocks_per_strip
= __cpu_to_le16(info_to_blocks_per_strip(info
));
5516 map
->failed_disk_num
= ~0;
5517 if (info
->level
> 0)
5518 map
->map_state
= (info
->state
? IMSM_T_STATE_NORMAL
5519 : IMSM_T_STATE_UNINITIALIZED
);
5521 map
->map_state
= info
->failed_disks
? IMSM_T_STATE_FAILED
:
5522 IMSM_T_STATE_NORMAL
;
5525 if (info
->level
== 1 && info
->raid_disks
> 2) {
5528 pr_err("imsm does not support more than 2 disksin a raid1 volume\n");
5532 map
->raid_level
= info
->level
;
5533 if (info
->level
== 10) {
5534 map
->raid_level
= 1;
5535 map
->num_domains
= info
->raid_disks
/ 2;
5536 } else if (info
->level
== 1)
5537 map
->num_domains
= info
->raid_disks
;
5539 map
->num_domains
= 1;
5541 /* info->size is only int so use the 'size' parameter instead */
5542 num_data_stripes
= size_per_member
/ info_to_blocks_per_strip(info
);
5543 num_data_stripes
/= map
->num_domains
;
5544 set_num_data_stripes(map
, num_data_stripes
);
5546 size_per_member
+= NUM_BLOCKS_DIRTY_STRIPE_REGION
;
5547 set_blocks_per_member(map
, info_to_blocks_per_member(info
,
5551 map
->num_members
= info
->raid_disks
;
5552 for (i
= 0; i
< map
->num_members
; i
++) {
5553 /* initialized in add_to_super */
5554 set_imsm_ord_tbl_ent(map
, i
, IMSM_ORD_REBUILD
);
5556 mpb
->num_raid_devs
++;
5557 mpb
->num_raid_devs_created
++;
5558 dev
->my_vol_raid_dev_num
= mpb
->num_raid_devs_created
;
5560 if (s
->consistency_policy
<= CONSISTENCY_POLICY_RESYNC
) {
5561 dev
->rwh_policy
= RWH_MULTIPLE_OFF
;
5562 } else if (s
->consistency_policy
== CONSISTENCY_POLICY_PPL
) {
5563 dev
->rwh_policy
= RWH_MULTIPLE_DISTRIBUTED
;
5567 pr_err("imsm does not support consistency policy %s\n",
5568 map_num(consistency_policies
, s
->consistency_policy
));
5573 dv
->index
= super
->current_vol
;
5574 dv
->next
= super
->devlist
;
5575 super
->devlist
= dv
;
5577 imsm_update_version_info(super
);
5582 static int init_super_imsm(struct supertype
*st
, mdu_array_info_t
*info
,
5583 struct shape
*s
, char *name
,
5584 char *homehost
, int *uuid
,
5585 unsigned long long data_offset
)
5587 /* This is primarily called by Create when creating a new array.
5588 * We will then get add_to_super called for each component, and then
5589 * write_init_super called to write it out to each device.
5590 * For IMSM, Create can create on fresh devices or on a pre-existing
5592 * To create on a pre-existing array a different method will be called.
5593 * This one is just for fresh drives.
5595 struct intel_super
*super
;
5596 struct imsm_super
*mpb
;
5600 if (data_offset
!= INVALID_SECTORS
) {
5601 pr_err("data-offset not supported by imsm\n");
5606 return init_super_imsm_volume(st
, info
, s
, name
, homehost
, uuid
,
5610 mpb_size
= disks_to_mpb_size(info
->nr_disks
);
5612 mpb_size
= MAX_SECTOR_SIZE
;
5614 super
= alloc_super();
5616 posix_memalign(&super
->buf
, MAX_SECTOR_SIZE
, mpb_size
) != 0) {
5621 pr_err("could not allocate superblock\n");
5624 if (posix_memalign(&super
->migr_rec_buf
, MAX_SECTOR_SIZE
,
5625 MIGR_REC_BUF_SECTORS
*MAX_SECTOR_SIZE
) != 0) {
5626 pr_err("could not allocate migr_rec buffer\n");
5631 memset(super
->buf
, 0, mpb_size
);
5633 mpb
->mpb_size
= __cpu_to_le32(mpb_size
);
5637 /* zeroing superblock */
5641 mpb
->attributes
= MPB_ATTRIB_CHECKSUM_VERIFY
;
5643 version
= (char *) mpb
->sig
;
5644 strcpy(version
, MPB_SIGNATURE
);
5645 version
+= strlen(MPB_SIGNATURE
);
5646 strcpy(version
, MPB_VERSION_RAID0
);
5651 static int drive_validate_sector_size(struct intel_super
*super
, struct dl
*dl
)
5653 unsigned int member_sector_size
;
5656 pr_err("Invalid file descriptor for %s\n", dl
->devname
);
5660 if (!get_dev_sector_size(dl
->fd
, dl
->devname
, &member_sector_size
))
5662 if (member_sector_size
!= super
->sector_size
)
5667 static int add_to_super_imsm_volume(struct supertype
*st
, mdu_disk_info_t
*dk
,
5668 int fd
, char *devname
)
5670 struct intel_super
*super
= st
->sb
;
5671 struct imsm_super
*mpb
= super
->anchor
;
5672 struct imsm_disk
*_disk
;
5673 struct imsm_dev
*dev
;
5674 struct imsm_map
*map
;
5678 dev
= get_imsm_dev(super
, super
->current_vol
);
5679 map
= get_imsm_map(dev
, MAP_0
);
5681 if (! (dk
->state
& (1<<MD_DISK_SYNC
))) {
5682 pr_err("%s: Cannot add spare devices to IMSM volume\n",
5688 /* we're doing autolayout so grab the pre-marked (in
5689 * validate_geometry) raid_disk
5691 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
5692 if (dl
->raiddisk
== dk
->raid_disk
)
5695 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
5696 if (dl
->major
== dk
->major
&&
5697 dl
->minor
== dk
->minor
)
5702 pr_err("%s is not a member of the same container\n", devname
);
5706 if (mpb
->num_disks
== 0)
5707 if (!get_dev_sector_size(dl
->fd
, dl
->devname
,
5708 &super
->sector_size
))
5711 if (!drive_validate_sector_size(super
, dl
)) {
5712 pr_err("Combining drives of different sector size in one volume is not allowed\n");
5716 /* add a pristine spare to the metadata */
5717 if (dl
->index
< 0) {
5718 dl
->index
= super
->anchor
->num_disks
;
5719 super
->anchor
->num_disks
++;
5721 /* Check the device has not already been added */
5722 slot
= get_imsm_disk_slot(map
, dl
->index
);
5724 (get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
) & IMSM_ORD_REBUILD
) == 0) {
5725 pr_err("%s has been included in this array twice\n",
5729 set_imsm_ord_tbl_ent(map
, dk
->raid_disk
, dl
->index
);
5730 dl
->disk
.status
= CONFIGURED_DISK
;
5732 /* update size of 'missing' disks to be at least as large as the
5733 * largest acitve member (we only have dummy missing disks when
5734 * creating the first volume)
5736 if (super
->current_vol
== 0) {
5737 for (df
= super
->missing
; df
; df
= df
->next
) {
5738 if (total_blocks(&dl
->disk
) > total_blocks(&df
->disk
))
5739 set_total_blocks(&df
->disk
, total_blocks(&dl
->disk
));
5740 _disk
= __get_imsm_disk(mpb
, df
->index
);
5745 /* refresh unset/failed slots to point to valid 'missing' entries */
5746 for (df
= super
->missing
; df
; df
= df
->next
)
5747 for (slot
= 0; slot
< mpb
->num_disks
; slot
++) {
5748 __u32 ord
= get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
);
5750 if ((ord
& IMSM_ORD_REBUILD
) == 0)
5752 set_imsm_ord_tbl_ent(map
, slot
, df
->index
| IMSM_ORD_REBUILD
);
5753 if (is_gen_migration(dev
)) {
5754 struct imsm_map
*map2
= get_imsm_map(dev
,
5756 int slot2
= get_imsm_disk_slot(map2
, df
->index
);
5757 if (slot2
< map2
->num_members
&& slot2
>= 0) {
5758 __u32 ord2
= get_imsm_ord_tbl_ent(dev
,
5761 if ((unsigned)df
->index
==
5763 set_imsm_ord_tbl_ent(map2
,
5769 dprintf("set slot:%d to missing disk:%d\n", slot
, df
->index
);
5773 /* if we are creating the first raid device update the family number */
5774 if (super
->current_vol
== 0) {
5776 struct imsm_dev
*_dev
= __get_imsm_dev(mpb
, 0);
5778 _disk
= __get_imsm_disk(mpb
, dl
->index
);
5779 if (!_dev
|| !_disk
) {
5780 pr_err("BUG mpb setup error\n");
5786 sum
+= __gen_imsm_checksum(mpb
);
5787 mpb
->family_num
= __cpu_to_le32(sum
);
5788 mpb
->orig_family_num
= mpb
->family_num
;
5790 super
->current_disk
= dl
;
5795 * Function marks disk as spare and restores disk serial
5796 * in case it was previously marked as failed by takeover operation
5798 * -1 : critical error
5799 * 0 : disk is marked as spare but serial is not set
5802 int mark_spare(struct dl
*disk
)
5804 __u8 serial
[MAX_RAID_SERIAL_LEN
];
5811 if (!imsm_read_serial(disk
->fd
, NULL
, serial
)) {
5812 /* Restore disk serial number, because takeover marks disk
5813 * as failed and adds to serial ':0' before it becomes
5816 serialcpy(disk
->serial
, serial
);
5817 serialcpy(disk
->disk
.serial
, serial
);
5820 disk
->disk
.status
= SPARE_DISK
;
5826 static int add_to_super_imsm(struct supertype
*st
, mdu_disk_info_t
*dk
,
5827 int fd
, char *devname
,
5828 unsigned long long data_offset
)
5830 struct intel_super
*super
= st
->sb
;
5832 unsigned long long size
;
5833 unsigned int member_sector_size
;
5838 /* If we are on an RAID enabled platform check that the disk is
5839 * attached to the raid controller.
5840 * We do not need to test disks attachment for container based additions,
5841 * they shall be already tested when container was created/assembled.
5843 rv
= find_intel_hba_capability(fd
, super
, devname
);
5844 /* no orom/efi or non-intel hba of the disk */
5846 dprintf("capability: %p fd: %d ret: %d\n",
5847 super
->orom
, fd
, rv
);
5851 if (super
->current_vol
>= 0)
5852 return add_to_super_imsm_volume(st
, dk
, fd
, devname
);
5855 dd
= xcalloc(sizeof(*dd
), 1);
5856 dd
->major
= major(stb
.st_rdev
);
5857 dd
->minor
= minor(stb
.st_rdev
);
5858 dd
->devname
= devname
? xstrdup(devname
) : NULL
;
5861 dd
->action
= DISK_ADD
;
5862 rv
= imsm_read_serial(fd
, devname
, dd
->serial
);
5864 pr_err("failed to retrieve scsi serial, aborting\n");
5870 if (super
->hba
&& ((super
->hba
->type
== SYS_DEV_NVME
) ||
5871 (super
->hba
->type
== SYS_DEV_VMD
))) {
5873 char *devpath
= diskfd_to_devpath(fd
);
5874 char controller_path
[PATH_MAX
];
5877 pr_err("failed to get devpath, aborting\n");
5884 snprintf(controller_path
, PATH_MAX
-1, "%s/device", devpath
);
5887 if (devpath_to_vendor(controller_path
) == 0x8086) {
5889 * If Intel's NVMe drive has serial ended with
5890 * "-A","-B","-1" or "-2" it means that this is "x8"
5891 * device (double drive on single PCIe card).
5892 * User should be warned about potential data loss.
5894 for (i
= MAX_RAID_SERIAL_LEN
-1; i
> 0; i
--) {
5895 /* Skip empty character at the end */
5896 if (dd
->serial
[i
] == 0)
5899 if (((dd
->serial
[i
] == 'A') ||
5900 (dd
->serial
[i
] == 'B') ||
5901 (dd
->serial
[i
] == '1') ||
5902 (dd
->serial
[i
] == '2')) &&
5903 (dd
->serial
[i
-1] == '-'))
5904 pr_err("\tThe action you are about to take may put your data at risk.\n"
5905 "\tPlease note that x8 devices may consist of two separate x4 devices "
5906 "located on a single PCIe port.\n"
5907 "\tRAID 0 is the only supported configuration for this type of x8 device.\n");
5910 } else if (super
->hba
->type
== SYS_DEV_VMD
&& super
->orom
&&
5911 !imsm_orom_has_tpv_support(super
->orom
)) {
5912 pr_err("\tPlatform configuration does not support non-Intel NVMe drives.\n"
5913 "\tPlease refer to Intel(R) RSTe/VROC user guide.\n");
5920 get_dev_size(fd
, NULL
, &size
);
5921 get_dev_sector_size(fd
, NULL
, &member_sector_size
);
5923 if (super
->sector_size
== 0) {
5924 /* this a first device, so sector_size is not set yet */
5925 super
->sector_size
= member_sector_size
;
5928 /* clear migr_rec when adding disk to container */
5929 memset(super
->migr_rec_buf
, 0, MIGR_REC_BUF_SECTORS
*MAX_SECTOR_SIZE
);
5930 if (lseek64(fd
, size
- MIGR_REC_SECTOR_POSITION
*member_sector_size
,
5932 if ((unsigned int)write(fd
, super
->migr_rec_buf
,
5933 MIGR_REC_BUF_SECTORS
*member_sector_size
) !=
5934 MIGR_REC_BUF_SECTORS
*member_sector_size
)
5935 perror("Write migr_rec failed");
5939 serialcpy(dd
->disk
.serial
, dd
->serial
);
5940 set_total_blocks(&dd
->disk
, size
);
5941 if (__le32_to_cpu(dd
->disk
.total_blocks_hi
) > 0) {
5942 struct imsm_super
*mpb
= super
->anchor
;
5943 mpb
->attributes
|= MPB_ATTRIB_2TB_DISK
;
5946 if (sysfs_disk_to_scsi_id(fd
, &id
) == 0)
5947 dd
->disk
.scsi_id
= __cpu_to_le32(id
);
5949 dd
->disk
.scsi_id
= __cpu_to_le32(0);
5951 if (st
->update_tail
) {
5952 dd
->next
= super
->disk_mgmt_list
;
5953 super
->disk_mgmt_list
= dd
;
5955 dd
->next
= super
->disks
;
5957 super
->updates_pending
++;
5963 static int remove_from_super_imsm(struct supertype
*st
, mdu_disk_info_t
*dk
)
5965 struct intel_super
*super
= st
->sb
;
5968 /* remove from super works only in mdmon - for communication
5969 * manager - monitor. Check if communication memory buffer
5972 if (!st
->update_tail
) {
5973 pr_err("shall be used in mdmon context only\n");
5976 dd
= xcalloc(1, sizeof(*dd
));
5977 dd
->major
= dk
->major
;
5978 dd
->minor
= dk
->minor
;
5981 dd
->action
= DISK_REMOVE
;
5983 dd
->next
= super
->disk_mgmt_list
;
5984 super
->disk_mgmt_list
= dd
;
5989 static int store_imsm_mpb(int fd
, struct imsm_super
*mpb
);
5992 char buf
[MAX_SECTOR_SIZE
];
5993 struct imsm_super anchor
;
5994 } spare_record
__attribute__ ((aligned(MAX_SECTOR_SIZE
)));
5996 /* spare records have their own family number and do not have any defined raid
5999 static int write_super_imsm_spares(struct intel_super
*super
, int doclose
)
6001 struct imsm_super
*mpb
= super
->anchor
;
6002 struct imsm_super
*spare
= &spare_record
.anchor
;
6006 spare
->mpb_size
= __cpu_to_le32(sizeof(struct imsm_super
));
6007 spare
->generation_num
= __cpu_to_le32(1UL);
6008 spare
->attributes
= MPB_ATTRIB_CHECKSUM_VERIFY
;
6009 spare
->num_disks
= 1;
6010 spare
->num_raid_devs
= 0;
6011 spare
->cache_size
= mpb
->cache_size
;
6012 spare
->pwr_cycle_count
= __cpu_to_le32(1);
6014 snprintf((char *) spare
->sig
, MAX_SIGNATURE_LENGTH
,
6015 MPB_SIGNATURE MPB_VERSION_RAID0
);
6017 for (d
= super
->disks
; d
; d
= d
->next
) {
6021 spare
->disk
[0] = d
->disk
;
6022 if (__le32_to_cpu(d
->disk
.total_blocks_hi
) > 0)
6023 spare
->attributes
|= MPB_ATTRIB_2TB_DISK
;
6025 if (super
->sector_size
== 4096)
6026 convert_to_4k_imsm_disk(&spare
->disk
[0]);
6028 sum
= __gen_imsm_checksum(spare
);
6029 spare
->family_num
= __cpu_to_le32(sum
);
6030 spare
->orig_family_num
= 0;
6031 sum
= __gen_imsm_checksum(spare
);
6032 spare
->check_sum
= __cpu_to_le32(sum
);
6034 if (store_imsm_mpb(d
->fd
, spare
)) {
6035 pr_err("failed for device %d:%d %s\n",
6036 d
->major
, d
->minor
, strerror(errno
));
6048 static int write_super_imsm(struct supertype
*st
, int doclose
)
6050 struct intel_super
*super
= st
->sb
;
6051 unsigned int sector_size
= super
->sector_size
;
6052 struct imsm_super
*mpb
= super
->anchor
;
6058 __u32 mpb_size
= sizeof(struct imsm_super
) - sizeof(struct imsm_disk
);
6060 int clear_migration_record
= 1;
6063 /* 'generation' is incremented everytime the metadata is written */
6064 generation
= __le32_to_cpu(mpb
->generation_num
);
6066 mpb
->generation_num
= __cpu_to_le32(generation
);
6068 /* fix up cases where previous mdadm releases failed to set
6071 if (mpb
->orig_family_num
== 0)
6072 mpb
->orig_family_num
= mpb
->family_num
;
6074 for (d
= super
->disks
; d
; d
= d
->next
) {
6078 mpb
->disk
[d
->index
] = d
->disk
;
6082 for (d
= super
->missing
; d
; d
= d
->next
) {
6083 mpb
->disk
[d
->index
] = d
->disk
;
6086 mpb
->num_disks
= num_disks
;
6087 mpb_size
+= sizeof(struct imsm_disk
) * mpb
->num_disks
;
6089 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
6090 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
6091 struct imsm_dev
*dev2
= get_imsm_dev(super
, i
);
6093 imsm_copy_dev(dev
, dev2
);
6094 mpb_size
+= sizeof_imsm_dev(dev
, 0);
6096 if (is_gen_migration(dev2
))
6097 clear_migration_record
= 0;
6100 bbm_log_size
= get_imsm_bbm_log_size(super
->bbm_log
);
6103 memcpy((void *)mpb
+ mpb_size
, super
->bbm_log
, bbm_log_size
);
6104 mpb
->attributes
|= MPB_ATTRIB_BBM
;
6106 mpb
->attributes
&= ~MPB_ATTRIB_BBM
;
6108 super
->anchor
->bbm_log_size
= __cpu_to_le32(bbm_log_size
);
6109 mpb_size
+= bbm_log_size
;
6110 mpb
->mpb_size
= __cpu_to_le32(mpb_size
);
6113 assert(super
->len
== 0 || mpb_size
<= super
->len
);
6116 /* recalculate checksum */
6117 sum
= __gen_imsm_checksum(mpb
);
6118 mpb
->check_sum
= __cpu_to_le32(sum
);
6120 if (super
->clean_migration_record_by_mdmon
) {
6121 clear_migration_record
= 1;
6122 super
->clean_migration_record_by_mdmon
= 0;
6124 if (clear_migration_record
)
6125 memset(super
->migr_rec_buf
, 0,
6126 MIGR_REC_BUF_SECTORS
*MAX_SECTOR_SIZE
);
6128 if (sector_size
== 4096)
6129 convert_to_4k(super
);
6131 /* write the mpb for disks that compose raid devices */
6132 for (d
= super
->disks
; d
; d
= d
->next
) {
6133 if (d
->index
< 0 || is_failed(&d
->disk
))
6136 if (clear_migration_record
) {
6137 unsigned long long dsize
;
6139 get_dev_size(d
->fd
, NULL
, &dsize
);
6140 if (lseek64(d
->fd
, dsize
- sector_size
,
6142 if ((unsigned int)write(d
->fd
,
6143 super
->migr_rec_buf
,
6144 MIGR_REC_BUF_SECTORS
*sector_size
) !=
6145 MIGR_REC_BUF_SECTORS
*sector_size
)
6146 perror("Write migr_rec failed");
6150 if (store_imsm_mpb(d
->fd
, mpb
))
6152 "failed for device %d:%d (fd: %d)%s\n",
6154 d
->fd
, strerror(errno
));
6163 return write_super_imsm_spares(super
, doclose
);
6168 static int create_array(struct supertype
*st
, int dev_idx
)
6171 struct imsm_update_create_array
*u
;
6172 struct intel_super
*super
= st
->sb
;
6173 struct imsm_dev
*dev
= get_imsm_dev(super
, dev_idx
);
6174 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
6175 struct disk_info
*inf
;
6176 struct imsm_disk
*disk
;
6179 len
= sizeof(*u
) - sizeof(*dev
) + sizeof_imsm_dev(dev
, 0) +
6180 sizeof(*inf
) * map
->num_members
;
6182 u
->type
= update_create_array
;
6183 u
->dev_idx
= dev_idx
;
6184 imsm_copy_dev(&u
->dev
, dev
);
6185 inf
= get_disk_info(u
);
6186 for (i
= 0; i
< map
->num_members
; i
++) {
6187 int idx
= get_imsm_disk_idx(dev
, i
, MAP_X
);
6189 disk
= get_imsm_disk(super
, idx
);
6191 disk
= get_imsm_missing(super
, idx
);
6192 serialcpy(inf
[i
].serial
, disk
->serial
);
6194 append_metadata_update(st
, u
, len
);
6199 static int mgmt_disk(struct supertype
*st
)
6201 struct intel_super
*super
= st
->sb
;
6203 struct imsm_update_add_remove_disk
*u
;
6205 if (!super
->disk_mgmt_list
)
6210 u
->type
= update_add_remove_disk
;
6211 append_metadata_update(st
, u
, len
);
6216 __u32
crc32c_le(__u32 crc
, unsigned char const *p
, size_t len
);
6218 static int write_ppl_header(unsigned long long ppl_sector
, int fd
, void *buf
)
6220 struct ppl_header
*ppl_hdr
= buf
;
6223 ppl_hdr
->checksum
= __cpu_to_le32(~crc32c_le(~0, buf
, PPL_HEADER_SIZE
));
6225 if (lseek64(fd
, ppl_sector
* 512, SEEK_SET
) < 0) {
6227 perror("Failed to seek to PPL header location");
6231 if (write(fd
, buf
, PPL_HEADER_SIZE
) != PPL_HEADER_SIZE
) {
6233 perror("Write PPL header failed");
6242 static int write_init_ppl_imsm(struct supertype
*st
, struct mdinfo
*info
, int fd
)
6244 struct intel_super
*super
= st
->sb
;
6246 struct ppl_header
*ppl_hdr
;
6249 /* first clear entire ppl space */
6250 ret
= zero_disk_range(fd
, info
->ppl_sector
, info
->ppl_size
);
6254 ret
= posix_memalign(&buf
, MAX_SECTOR_SIZE
, PPL_HEADER_SIZE
);
6256 pr_err("Failed to allocate PPL header buffer\n");
6260 memset(buf
, 0, PPL_HEADER_SIZE
);
6262 memset(ppl_hdr
->reserved
, 0xff, PPL_HDR_RESERVED
);
6263 ppl_hdr
->signature
= __cpu_to_le32(super
->anchor
->orig_family_num
);
6265 if (info
->mismatch_cnt
) {
6267 * We are overwriting an invalid ppl. Make one entry with wrong
6268 * checksum to prevent the kernel from skipping resync.
6270 ppl_hdr
->entries_count
= __cpu_to_le32(1);
6271 ppl_hdr
->entries
[0].checksum
= ~0;
6274 ret
= write_ppl_header(info
->ppl_sector
, fd
, buf
);
6280 static int is_rebuilding(struct imsm_dev
*dev
);
6282 static int validate_ppl_imsm(struct supertype
*st
, struct mdinfo
*info
,
6283 struct mdinfo
*disk
)
6285 struct intel_super
*super
= st
->sb
;
6287 void *buf_orig
, *buf
, *buf_prev
= NULL
;
6289 struct ppl_header
*ppl_hdr
= NULL
;
6291 struct imsm_dev
*dev
;
6294 unsigned long long ppl_offset
= 0;
6295 unsigned long long prev_gen_num
= 0;
6297 if (disk
->disk
.raid_disk
< 0)
6300 dev
= get_imsm_dev(super
, info
->container_member
);
6301 idx
= get_imsm_disk_idx(dev
, disk
->disk
.raid_disk
, MAP_0
);
6302 d
= get_imsm_dl_disk(super
, idx
);
6304 if (!d
|| d
->index
< 0 || is_failed(&d
->disk
))
6307 if (posix_memalign(&buf_orig
, MAX_SECTOR_SIZE
, PPL_HEADER_SIZE
* 2)) {
6308 pr_err("Failed to allocate PPL header buffer\n");
6314 while (ppl_offset
< MULTIPLE_PPL_AREA_SIZE_IMSM
) {
6317 dprintf("Checking potential PPL at offset: %llu\n", ppl_offset
);
6319 if (lseek64(d
->fd
, info
->ppl_sector
* 512 + ppl_offset
,
6321 perror("Failed to seek to PPL header location");
6326 if (read(d
->fd
, buf
, PPL_HEADER_SIZE
) != PPL_HEADER_SIZE
) {
6327 perror("Read PPL header failed");
6334 crc
= __le32_to_cpu(ppl_hdr
->checksum
);
6335 ppl_hdr
->checksum
= 0;
6337 if (crc
!= ~crc32c_le(~0, buf
, PPL_HEADER_SIZE
)) {
6338 dprintf("Wrong PPL header checksum on %s\n",
6343 if (prev_gen_num
> __le64_to_cpu(ppl_hdr
->generation
)) {
6344 /* previous was newest, it was already checked */
6348 if ((__le32_to_cpu(ppl_hdr
->signature
) !=
6349 super
->anchor
->orig_family_num
)) {
6350 dprintf("Wrong PPL header signature on %s\n",
6357 prev_gen_num
= __le64_to_cpu(ppl_hdr
->generation
);
6359 ppl_offset
+= PPL_HEADER_SIZE
;
6360 for (i
= 0; i
< __le32_to_cpu(ppl_hdr
->entries_count
); i
++)
6362 __le32_to_cpu(ppl_hdr
->entries
[i
].pp_size
);
6365 buf_prev
= buf
+ PPL_HEADER_SIZE
;
6377 * Update metadata to use mutliple PPLs area (1MB).
6378 * This is done once for all RAID members
6380 if (info
->consistency_policy
== CONSISTENCY_POLICY_PPL
&&
6381 info
->ppl_size
!= (MULTIPLE_PPL_AREA_SIZE_IMSM
>> 9)) {
6383 struct mdinfo
*member_dev
;
6385 sprintf(subarray
, "%d", info
->container_member
);
6387 if (mdmon_running(st
->container_devnm
))
6388 st
->update_tail
= &st
->updates
;
6390 if (st
->ss
->update_subarray(st
, subarray
, "ppl", NULL
)) {
6391 pr_err("Failed to update subarray %s\n",
6394 if (st
->update_tail
)
6395 flush_metadata_updates(st
);
6397 st
->ss
->sync_metadata(st
);
6398 info
->ppl_size
= (MULTIPLE_PPL_AREA_SIZE_IMSM
>> 9);
6399 for (member_dev
= info
->devs
; member_dev
;
6400 member_dev
= member_dev
->next
)
6401 member_dev
->ppl_size
=
6402 (MULTIPLE_PPL_AREA_SIZE_IMSM
>> 9);
6407 struct imsm_map
*map
= get_imsm_map(dev
, MAP_X
);
6409 if (map
->map_state
== IMSM_T_STATE_UNINITIALIZED
||
6410 (map
->map_state
== IMSM_T_STATE_NORMAL
&&
6411 !(dev
->vol
.dirty
& RAIDVOL_DIRTY
)) ||
6412 (is_rebuilding(dev
) &&
6413 dev
->vol
.curr_migr_unit
== 0 &&
6414 get_imsm_disk_idx(dev
, disk
->disk
.raid_disk
, MAP_1
) != idx
))
6415 ret
= st
->ss
->write_init_ppl(st
, info
, d
->fd
);
6417 info
->mismatch_cnt
++;
6418 } else if (ret
== 0 &&
6419 ppl_hdr
->entries_count
== 0 &&
6420 is_rebuilding(dev
) &&
6421 info
->resync_start
== 0) {
6423 * The header has no entries - add a single empty entry and
6424 * rewrite the header to prevent the kernel from going into
6425 * resync after an interrupted rebuild.
6427 ppl_hdr
->entries_count
= __cpu_to_le32(1);
6428 ret
= write_ppl_header(info
->ppl_sector
, d
->fd
, buf
);
6436 static int write_init_ppl_imsm_all(struct supertype
*st
, struct mdinfo
*info
)
6438 struct intel_super
*super
= st
->sb
;
6442 if (info
->consistency_policy
!= CONSISTENCY_POLICY_PPL
||
6443 info
->array
.level
!= 5)
6446 for (d
= super
->disks
; d
; d
= d
->next
) {
6447 if (d
->index
< 0 || is_failed(&d
->disk
))
6450 ret
= st
->ss
->write_init_ppl(st
, info
, d
->fd
);
6458 static int write_init_super_imsm(struct supertype
*st
)
6460 struct intel_super
*super
= st
->sb
;
6461 int current_vol
= super
->current_vol
;
6465 getinfo_super_imsm(st
, &info
, NULL
);
6467 /* we are done with current_vol reset it to point st at the container */
6468 super
->current_vol
= -1;
6470 if (st
->update_tail
) {
6471 /* queue the recently created array / added disk
6472 * as a metadata update */
6474 /* determine if we are creating a volume or adding a disk */
6475 if (current_vol
< 0) {
6476 /* in the mgmt (add/remove) disk case we are running
6477 * in mdmon context, so don't close fd's
6481 rv
= write_init_ppl_imsm_all(st
, &info
);
6483 rv
= create_array(st
, current_vol
);
6487 for (d
= super
->disks
; d
; d
= d
->next
)
6488 Kill(d
->devname
, NULL
, 0, -1, 1);
6489 if (current_vol
>= 0)
6490 rv
= write_init_ppl_imsm_all(st
, &info
);
6492 rv
= write_super_imsm(st
, 1);
6498 static int store_super_imsm(struct supertype
*st
, int fd
)
6500 struct intel_super
*super
= st
->sb
;
6501 struct imsm_super
*mpb
= super
? super
->anchor
: NULL
;
6506 if (super
->sector_size
== 4096)
6507 convert_to_4k(super
);
6508 return store_imsm_mpb(fd
, mpb
);
6511 static int validate_geometry_imsm_container(struct supertype
*st
, int level
,
6512 int layout
, int raiddisks
, int chunk
,
6513 unsigned long long size
,
6514 unsigned long long data_offset
,
6516 unsigned long long *freesize
,
6520 unsigned long long ldsize
;
6521 struct intel_super
*super
;
6524 if (level
!= LEVEL_CONTAINER
)
6529 fd
= open(dev
, O_RDONLY
|O_EXCL
, 0);
6532 pr_err("imsm: Cannot open %s: %s\n",
6533 dev
, strerror(errno
));
6536 if (!get_dev_size(fd
, dev
, &ldsize
)) {
6541 /* capabilities retrieve could be possible
6542 * note that there is no fd for the disks in array.
6544 super
= alloc_super();
6549 if (!get_dev_sector_size(fd
, NULL
, &super
->sector_size
)) {
6555 rv
= find_intel_hba_capability(fd
, super
, verbose
> 0 ? dev
: NULL
);
6559 fd2devname(fd
, str
);
6560 dprintf("fd: %d %s orom: %p rv: %d raiddisk: %d\n",
6561 fd
, str
, super
->orom
, rv
, raiddisks
);
6563 /* no orom/efi or non-intel hba of the disk */
6570 if (raiddisks
> super
->orom
->tds
) {
6572 pr_err("%d exceeds maximum number of platform supported disks: %d\n",
6573 raiddisks
, super
->orom
->tds
);
6577 if ((super
->orom
->attr
& IMSM_OROM_ATTR_2TB_DISK
) == 0 &&
6578 (ldsize
>> 9) >> 32 > 0) {
6580 pr_err("%s exceeds maximum platform supported size\n", dev
);
6586 *freesize
= avail_size_imsm(st
, ldsize
>> 9, data_offset
);
6592 static unsigned long long find_size(struct extent
*e
, int *idx
, int num_extents
)
6594 const unsigned long long base_start
= e
[*idx
].start
;
6595 unsigned long long end
= base_start
+ e
[*idx
].size
;
6598 if (base_start
== end
)
6602 for (i
= *idx
; i
< num_extents
; i
++) {
6603 /* extend overlapping extents */
6604 if (e
[i
].start
>= base_start
&&
6605 e
[i
].start
<= end
) {
6608 if (e
[i
].start
+ e
[i
].size
> end
)
6609 end
= e
[i
].start
+ e
[i
].size
;
6610 } else if (e
[i
].start
> end
) {
6616 return end
- base_start
;
6619 static unsigned long long merge_extents(struct intel_super
*super
, int sum_extents
)
6621 /* build a composite disk with all known extents and generate a new
6622 * 'maxsize' given the "all disks in an array must share a common start
6623 * offset" constraint
6625 struct extent
*e
= xcalloc(sum_extents
, sizeof(*e
));
6629 unsigned long long pos
;
6630 unsigned long long start
= 0;
6631 unsigned long long maxsize
;
6632 unsigned long reserve
;
6634 /* coalesce and sort all extents. also, check to see if we need to
6635 * reserve space between member arrays
6638 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
6641 for (i
= 0; i
< dl
->extent_cnt
; i
++)
6644 qsort(e
, sum_extents
, sizeof(*e
), cmp_extent
);
6649 while (i
< sum_extents
) {
6650 e
[j
].start
= e
[i
].start
;
6651 e
[j
].size
= find_size(e
, &i
, sum_extents
);
6653 if (e
[j
-1].size
== 0)
6662 unsigned long long esize
;
6664 esize
= e
[i
].start
- pos
;
6665 if (esize
>= maxsize
) {
6670 pos
= e
[i
].start
+ e
[i
].size
;
6672 } while (e
[i
-1].size
);
6678 /* FIXME assumes volume at offset 0 is the first volume in a
6681 if (start_extent
> 0)
6682 reserve
= IMSM_RESERVED_SECTORS
; /* gap between raid regions */
6686 if (maxsize
< reserve
)
6689 super
->create_offset
= ~((unsigned long long) 0);
6690 if (start
+ reserve
> super
->create_offset
)
6691 return 0; /* start overflows create_offset */
6692 super
->create_offset
= start
+ reserve
;
6694 return maxsize
- reserve
;
6697 static int is_raid_level_supported(const struct imsm_orom
*orom
, int level
, int raiddisks
)
6699 if (level
< 0 || level
== 6 || level
== 4)
6702 /* if we have an orom prevent invalid raid levels */
6705 case 0: return imsm_orom_has_raid0(orom
);
6708 return imsm_orom_has_raid1e(orom
);
6709 return imsm_orom_has_raid1(orom
) && raiddisks
== 2;
6710 case 10: return imsm_orom_has_raid10(orom
) && raiddisks
== 4;
6711 case 5: return imsm_orom_has_raid5(orom
) && raiddisks
> 2;
6714 return 1; /* not on an Intel RAID platform so anything goes */
6720 active_arrays_by_format(char *name
, char* hba
, struct md_list
**devlist
,
6721 int dpa
, int verbose
)
6723 struct mdstat_ent
*mdstat
= mdstat_read(0, 0);
6724 struct mdstat_ent
*memb
;
6730 for (memb
= mdstat
; memb
; memb
= memb
->next
) {
6731 if (memb
->metadata_version
&&
6732 (strncmp(memb
->metadata_version
, "external:", 9) == 0) &&
6733 (strcmp(&memb
->metadata_version
[9], name
) == 0) &&
6734 !is_subarray(memb
->metadata_version
+9) &&
6736 struct dev_member
*dev
= memb
->members
;
6738 while(dev
&& (fd
< 0)) {
6739 char *path
= xmalloc(strlen(dev
->name
) + strlen("/dev/") + 1);
6740 num
= sprintf(path
, "%s%s", "/dev/", dev
->name
);
6742 fd
= open(path
, O_RDONLY
, 0);
6743 if (num
<= 0 || fd
< 0) {
6744 pr_vrb("Cannot open %s: %s\n",
6745 dev
->name
, strerror(errno
));
6751 if (fd
>= 0 && disk_attached_to_hba(fd
, hba
)) {
6752 struct mdstat_ent
*vol
;
6753 for (vol
= mdstat
; vol
; vol
= vol
->next
) {
6754 if (vol
->active
> 0 &&
6755 vol
->metadata_version
&&
6756 is_container_member(vol
, memb
->devnm
)) {
6761 if (*devlist
&& (found
< dpa
)) {
6762 dv
= xcalloc(1, sizeof(*dv
));
6763 dv
->devname
= xmalloc(strlen(memb
->devnm
) + strlen("/dev/") + 1);
6764 sprintf(dv
->devname
, "%s%s", "/dev/", memb
->devnm
);
6767 dv
->next
= *devlist
;
6775 free_mdstat(mdstat
);
6780 static struct md_list
*
6781 get_loop_devices(void)
6784 struct md_list
*devlist
= NULL
;
6787 for(i
= 0; i
< 12; i
++) {
6788 dv
= xcalloc(1, sizeof(*dv
));
6789 dv
->devname
= xmalloc(40);
6790 sprintf(dv
->devname
, "/dev/loop%d", i
);
6798 static struct md_list
*
6799 get_devices(const char *hba_path
)
6801 struct md_list
*devlist
= NULL
;
6808 devlist
= get_loop_devices();
6811 /* scroll through /sys/dev/block looking for devices attached to
6814 dir
= opendir("/sys/dev/block");
6815 for (ent
= dir
? readdir(dir
) : NULL
; ent
; ent
= readdir(dir
)) {
6820 if (sscanf(ent
->d_name
, "%d:%d", &major
, &minor
) != 2)
6822 path
= devt_to_devpath(makedev(major
, minor
));
6825 if (!path_attached_to_hba(path
, hba_path
)) {
6832 fd
= dev_open(ent
->d_name
, O_RDONLY
);
6834 fd2devname(fd
, buf
);
6837 pr_err("cannot open device: %s\n",
6842 dv
= xcalloc(1, sizeof(*dv
));
6843 dv
->devname
= xstrdup(buf
);
6850 devlist
= devlist
->next
;
6860 count_volumes_list(struct md_list
*devlist
, char *homehost
,
6861 int verbose
, int *found
)
6863 struct md_list
*tmpdev
;
6865 struct supertype
*st
;
6867 /* first walk the list of devices to find a consistent set
6868 * that match the criterea, if that is possible.
6869 * We flag the ones we like with 'used'.
6872 st
= match_metadata_desc_imsm("imsm");
6874 pr_vrb("cannot allocate memory for imsm supertype\n");
6878 for (tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
6879 char *devname
= tmpdev
->devname
;
6881 struct supertype
*tst
;
6883 if (tmpdev
->used
> 1)
6885 tst
= dup_super(st
);
6887 pr_vrb("cannot allocate memory for imsm supertype\n");
6890 tmpdev
->container
= 0;
6891 dfd
= dev_open(devname
, O_RDONLY
|O_EXCL
);
6893 dprintf("cannot open device %s: %s\n",
6894 devname
, strerror(errno
));
6896 } else if (!fstat_is_blkdev(dfd
, devname
, &rdev
)) {
6898 } else if (must_be_container(dfd
)) {
6899 struct supertype
*cst
;
6900 cst
= super_by_fd(dfd
, NULL
);
6902 dprintf("cannot recognize container type %s\n",
6905 } else if (tst
->ss
!= st
->ss
) {
6906 dprintf("non-imsm container - ignore it: %s\n",
6909 } else if (!tst
->ss
->load_container
||
6910 tst
->ss
->load_container(tst
, dfd
, NULL
))
6913 tmpdev
->container
= 1;
6916 cst
->ss
->free_super(cst
);
6918 tmpdev
->st_rdev
= rdev
;
6919 if (tst
->ss
->load_super(tst
,dfd
, NULL
)) {
6920 dprintf("no RAID superblock on %s\n",
6923 } else if (tst
->ss
->compare_super
== NULL
) {
6924 dprintf("Cannot assemble %s metadata on %s\n",
6925 tst
->ss
->name
, devname
);
6931 if (tmpdev
->used
== 2 || tmpdev
->used
== 4) {
6932 /* Ignore unrecognised devices during auto-assembly */
6937 tst
->ss
->getinfo_super(tst
, &info
, NULL
);
6939 if (st
->minor_version
== -1)
6940 st
->minor_version
= tst
->minor_version
;
6942 if (memcmp(info
.uuid
, uuid_zero
,
6943 sizeof(int[4])) == 0) {
6944 /* this is a floating spare. It cannot define
6945 * an array unless there are no more arrays of
6946 * this type to be found. It can be included
6947 * in an array of this type though.
6953 if (st
->ss
!= tst
->ss
||
6954 st
->minor_version
!= tst
->minor_version
||
6955 st
->ss
->compare_super(st
, tst
) != 0) {
6956 /* Some mismatch. If exactly one array matches this host,
6957 * we can resolve on that one.
6958 * Or, if we are auto assembling, we just ignore the second
6961 dprintf("superblock on %s doesn't match others - assembly aborted\n",
6967 dprintf("found: devname: %s\n", devname
);
6971 tst
->ss
->free_super(tst
);
6975 if ((err
= load_super_imsm_all(st
, -1, &st
->sb
, NULL
, devlist
, 0)) == 0) {
6976 struct mdinfo
*iter
, *head
= st
->ss
->container_content(st
, NULL
);
6977 for (iter
= head
; iter
; iter
= iter
->next
) {
6978 dprintf("content->text_version: %s vol\n",
6979 iter
->text_version
);
6980 if (iter
->array
.state
& (1<<MD_SB_BLOCK_VOLUME
)) {
6981 /* do not assemble arrays with unsupported
6983 dprintf("Cannot activate member %s.\n",
6984 iter
->text_version
);
6991 dprintf("No valid super block on device list: err: %d %p\n",
6995 dprintf("no more devices to examine\n");
6998 for (tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
6999 if (tmpdev
->used
== 1 && tmpdev
->found
) {
7001 if (count
< tmpdev
->found
)
7004 count
-= tmpdev
->found
;
7007 if (tmpdev
->used
== 1)
7012 st
->ss
->free_super(st
);
7016 static int __count_volumes(char *hba_path
, int dpa
, int verbose
,
7019 struct sys_dev
*idev
, *intel_devices
= find_intel_devices();
7021 const struct orom_entry
*entry
;
7022 struct devid_list
*dv
, *devid_list
;
7027 for (idev
= intel_devices
; idev
; idev
= idev
->next
) {
7028 if (strstr(idev
->path
, hba_path
))
7032 if (!idev
|| !idev
->dev_id
)
7035 entry
= get_orom_entry_by_device_id(idev
->dev_id
);
7037 if (!entry
|| !entry
->devid_list
)
7040 devid_list
= entry
->devid_list
;
7041 for (dv
= devid_list
; dv
; dv
= dv
->next
) {
7042 struct md_list
*devlist
;
7043 struct sys_dev
*device
= NULL
;
7048 device
= device_by_id_and_path(dv
->devid
, hba_path
);
7050 device
= device_by_id(dv
->devid
);
7053 hpath
= device
->path
;
7057 devlist
= get_devices(hpath
);
7058 /* if no intel devices return zero volumes */
7059 if (devlist
== NULL
)
7062 count
+= active_arrays_by_format("imsm", hpath
, &devlist
, dpa
,
7064 dprintf("path: %s active arrays: %d\n", hpath
, count
);
7065 if (devlist
== NULL
)
7069 count
+= count_volumes_list(devlist
,
7073 dprintf("found %d count: %d\n", found
, count
);
7076 dprintf("path: %s total number of volumes: %d\n", hpath
, count
);
7079 struct md_list
*dv
= devlist
;
7080 devlist
= devlist
->next
;
7088 static int count_volumes(struct intel_hba
*hba
, int dpa
, int verbose
)
7092 if (hba
->type
== SYS_DEV_VMD
) {
7093 struct sys_dev
*dev
;
7096 for (dev
= find_intel_devices(); dev
; dev
= dev
->next
) {
7097 if (dev
->type
== SYS_DEV_VMD
)
7098 count
+= __count_volumes(dev
->path
, dpa
,
7103 return __count_volumes(hba
->path
, dpa
, verbose
, 0);
7106 static int imsm_default_chunk(const struct imsm_orom
*orom
)
7108 /* up to 512 if the plaform supports it, otherwise the platform max.
7109 * 128 if no platform detected
7111 int fs
= max(7, orom
? fls(orom
->sss
) : 0);
7113 return min(512, (1 << fs
));
7117 validate_geometry_imsm_orom(struct intel_super
*super
, int level
, int layout
,
7118 int raiddisks
, int *chunk
, unsigned long long size
, int verbose
)
7120 /* check/set platform and metadata limits/defaults */
7121 if (super
->orom
&& raiddisks
> super
->orom
->dpa
) {
7122 pr_vrb("platform supports a maximum of %d disks per array\n",
7127 /* capabilities of OROM tested - copied from validate_geometry_imsm_volume */
7128 if (!is_raid_level_supported(super
->orom
, level
, raiddisks
)) {
7129 pr_vrb("platform does not support raid%d with %d disk%s\n",
7130 level
, raiddisks
, raiddisks
> 1 ? "s" : "");
7134 if (*chunk
== 0 || *chunk
== UnSet
)
7135 *chunk
= imsm_default_chunk(super
->orom
);
7137 if (super
->orom
&& !imsm_orom_has_chunk(super
->orom
, *chunk
)) {
7138 pr_vrb("platform does not support a chunk size of: %d\n", *chunk
);
7142 if (layout
!= imsm_level_to_layout(level
)) {
7144 pr_vrb("imsm raid 5 only supports the left-asymmetric layout\n");
7145 else if (level
== 10)
7146 pr_vrb("imsm raid 10 only supports the n2 layout\n");
7148 pr_vrb("imsm unknown layout %#x for this raid level %d\n",
7153 if (super
->orom
&& (super
->orom
->attr
& IMSM_OROM_ATTR_2TB
) == 0 &&
7154 (calc_array_size(level
, raiddisks
, layout
, *chunk
, size
) >> 32) > 0) {
7155 pr_vrb("platform does not support a volume size over 2TB\n");
7162 /* validate_geometry_imsm_volume - lifted from validate_geometry_ddf_bvd
7163 * FIX ME add ahci details
7165 static int validate_geometry_imsm_volume(struct supertype
*st
, int level
,
7166 int layout
, int raiddisks
, int *chunk
,
7167 unsigned long long size
,
7168 unsigned long long data_offset
,
7170 unsigned long long *freesize
,
7174 struct intel_super
*super
= st
->sb
;
7175 struct imsm_super
*mpb
;
7177 unsigned long long pos
= 0;
7178 unsigned long long maxsize
;
7182 /* We must have the container info already read in. */
7186 mpb
= super
->anchor
;
7188 if (!validate_geometry_imsm_orom(super
, level
, layout
, raiddisks
, chunk
, size
, verbose
)) {
7189 pr_err("RAID geometry validation failed. Cannot proceed with the action(s).\n");
7193 /* General test: make sure there is space for
7194 * 'raiddisks' device extents of size 'size' at a given
7197 unsigned long long minsize
= size
;
7198 unsigned long long start_offset
= MaxSector
;
7201 minsize
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
7202 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
7207 e
= get_extents(super
, dl
, 0);
7210 unsigned long long esize
;
7211 esize
= e
[i
].start
- pos
;
7212 if (esize
>= minsize
)
7214 if (found
&& start_offset
== MaxSector
) {
7217 } else if (found
&& pos
!= start_offset
) {
7221 pos
= e
[i
].start
+ e
[i
].size
;
7223 } while (e
[i
-1].size
);
7228 if (dcnt
< raiddisks
) {
7230 pr_err("imsm: Not enough devices with space for this array (%d < %d)\n",
7237 /* This device must be a member of the set */
7238 if (!stat_is_blkdev(dev
, &rdev
))
7240 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
7241 if (dl
->major
== (int)major(rdev
) &&
7242 dl
->minor
== (int)minor(rdev
))
7247 pr_err("%s is not in the same imsm set\n", dev
);
7249 } else if (super
->orom
&& dl
->index
< 0 && mpb
->num_raid_devs
) {
7250 /* If a volume is present then the current creation attempt
7251 * cannot incorporate new spares because the orom may not
7252 * understand this configuration (all member disks must be
7253 * members of each array in the container).
7255 pr_err("%s is a spare and a volume is already defined for this container\n", dev
);
7256 pr_err("The option-rom requires all member disks to be a member of all volumes\n");
7258 } else if (super
->orom
&& mpb
->num_raid_devs
> 0 &&
7259 mpb
->num_disks
!= raiddisks
) {
7260 pr_err("The option-rom requires all member disks to be a member of all volumes\n");
7264 /* retrieve the largest free space block */
7265 e
= get_extents(super
, dl
, 0);
7270 unsigned long long esize
;
7272 esize
= e
[i
].start
- pos
;
7273 if (esize
>= maxsize
)
7275 pos
= e
[i
].start
+ e
[i
].size
;
7277 } while (e
[i
-1].size
);
7282 pr_err("unable to determine free space for: %s\n",
7286 if (maxsize
< size
) {
7288 pr_err("%s not enough space (%llu < %llu)\n",
7289 dev
, maxsize
, size
);
7293 /* count total number of extents for merge */
7295 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
7297 i
+= dl
->extent_cnt
;
7299 maxsize
= merge_extents(super
, i
);
7301 if (mpb
->num_raid_devs
> 0 && size
&& size
!= maxsize
)
7302 pr_err("attempting to create a second volume with size less then remaining space.\n");
7304 if (maxsize
< size
|| maxsize
== 0) {
7307 pr_err("no free space left on device. Aborting...\n");
7309 pr_err("not enough space to create volume of given size (%llu < %llu). Aborting...\n",
7315 *freesize
= maxsize
;
7318 int count
= count_volumes(super
->hba
,
7319 super
->orom
->dpa
, verbose
);
7320 if (super
->orom
->vphba
<= count
) {
7321 pr_vrb("platform does not support more than %d raid volumes.\n",
7322 super
->orom
->vphba
);
7329 static int imsm_get_free_size(struct supertype
*st
, int raiddisks
,
7330 unsigned long long size
, int chunk
,
7331 unsigned long long *freesize
)
7333 struct intel_super
*super
= st
->sb
;
7334 struct imsm_super
*mpb
= super
->anchor
;
7339 unsigned long long maxsize
;
7340 unsigned long long minsize
;
7344 /* find the largest common start free region of the possible disks */
7348 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
7354 /* don't activate new spares if we are orom constrained
7355 * and there is already a volume active in the container
7357 if (super
->orom
&& dl
->index
< 0 && mpb
->num_raid_devs
)
7360 e
= get_extents(super
, dl
, 0);
7363 for (i
= 1; e
[i
-1].size
; i
++)
7371 maxsize
= merge_extents(super
, extent_cnt
);
7375 minsize
= chunk
* 2;
7377 if (cnt
< raiddisks
||
7378 (super
->orom
&& used
&& used
!= raiddisks
) ||
7379 maxsize
< minsize
||
7381 pr_err("not enough devices with space to create array.\n");
7382 return 0; /* No enough free spaces large enough */
7393 if (mpb
->num_raid_devs
> 0 && size
&& size
!= maxsize
)
7394 pr_err("attempting to create a second volume with size less then remaining space.\n");
7396 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
7398 dl
->raiddisk
= cnt
++;
7402 dprintf("imsm: imsm_get_free_size() returns : %llu\n", size
);
7407 static int reserve_space(struct supertype
*st
, int raiddisks
,
7408 unsigned long long size
, int chunk
,
7409 unsigned long long *freesize
)
7411 struct intel_super
*super
= st
->sb
;
7416 rv
= imsm_get_free_size(st
, raiddisks
, size
, chunk
, freesize
);
7419 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
7421 dl
->raiddisk
= cnt
++;
7428 static int validate_geometry_imsm(struct supertype
*st
, int level
, int layout
,
7429 int raiddisks
, int *chunk
, unsigned long long size
,
7430 unsigned long long data_offset
,
7431 char *dev
, unsigned long long *freesize
,
7432 int consistency_policy
, int verbose
)
7439 * if given unused devices create a container
7440 * if given given devices in a container create a member volume
7442 if (level
== LEVEL_CONTAINER
) {
7443 /* Must be a fresh device to add to a container */
7444 return validate_geometry_imsm_container(st
, level
, layout
,
7452 if (size
&& (size
< 1024)) {
7453 pr_err("Given size must be greater than 1M.\n");
7454 /* Depends on algorithm in Create.c :
7455 * if container was given (dev == NULL) return -1,
7456 * if block device was given ( dev != NULL) return 0.
7458 return dev
? -1 : 0;
7463 struct intel_super
*super
= st
->sb
;
7464 if (!validate_geometry_imsm_orom(st
->sb
, level
, layout
,
7465 raiddisks
, chunk
, size
,
7468 /* we are being asked to automatically layout a
7469 * new volume based on the current contents of
7470 * the container. If the the parameters can be
7471 * satisfied reserve_space will record the disks,
7472 * start offset, and size of the volume to be
7473 * created. add_to_super and getinfo_super
7474 * detect when autolayout is in progress.
7476 /* assuming that freesize is always given when array is
7478 if (super
->orom
&& freesize
) {
7480 count
= count_volumes(super
->hba
,
7481 super
->orom
->dpa
, verbose
);
7482 if (super
->orom
->vphba
<= count
) {
7483 pr_vrb("platform does not support more than %d raid volumes.\n",
7484 super
->orom
->vphba
);
7489 return reserve_space(st
, raiddisks
, size
,
7495 /* creating in a given container */
7496 return validate_geometry_imsm_volume(st
, level
, layout
,
7497 raiddisks
, chunk
, size
,
7499 dev
, freesize
, verbose
);
7502 /* This device needs to be a device in an 'imsm' container */
7503 fd
= open(dev
, O_RDONLY
|O_EXCL
, 0);
7506 pr_err("Cannot create this array on device %s\n",
7511 if (errno
!= EBUSY
|| (fd
= open(dev
, O_RDONLY
, 0)) < 0) {
7513 pr_err("Cannot open %s: %s\n",
7514 dev
, strerror(errno
));
7517 /* Well, it is in use by someone, maybe an 'imsm' container. */
7518 cfd
= open_container(fd
);
7522 pr_err("Cannot use %s: It is busy\n",
7526 sra
= sysfs_read(cfd
, NULL
, GET_VERSION
);
7527 if (sra
&& sra
->array
.major_version
== -1 &&
7528 strcmp(sra
->text_version
, "imsm") == 0)
7532 /* This is a member of a imsm container. Load the container
7533 * and try to create a volume
7535 struct intel_super
*super
;
7537 if (load_super_imsm_all(st
, cfd
, (void **) &super
, NULL
, NULL
, 1) == 0) {
7539 strcpy(st
->container_devnm
, fd2devnm(cfd
));
7541 return validate_geometry_imsm_volume(st
, level
, layout
,
7543 size
, data_offset
, dev
,
7550 pr_err("failed container membership check\n");
7556 static void default_geometry_imsm(struct supertype
*st
, int *level
, int *layout
, int *chunk
)
7558 struct intel_super
*super
= st
->sb
;
7560 if (level
&& *level
== UnSet
)
7561 *level
= LEVEL_CONTAINER
;
7563 if (level
&& layout
&& *layout
== UnSet
)
7564 *layout
= imsm_level_to_layout(*level
);
7566 if (chunk
&& (*chunk
== UnSet
|| *chunk
== 0))
7567 *chunk
= imsm_default_chunk(super
->orom
);
7570 static void handle_missing(struct intel_super
*super
, struct imsm_dev
*dev
);
7572 static int kill_subarray_imsm(struct supertype
*st
)
7574 /* remove the subarray currently referenced by ->current_vol */
7576 struct intel_dev
**dp
;
7577 struct intel_super
*super
= st
->sb
;
7578 __u8 current_vol
= super
->current_vol
;
7579 struct imsm_super
*mpb
= super
->anchor
;
7581 if (super
->current_vol
< 0)
7583 super
->current_vol
= -1; /* invalidate subarray cursor */
7585 /* block deletions that would change the uuid of active subarrays
7587 * FIXME when immutable ids are available, but note that we'll
7588 * also need to fixup the invalidated/active subarray indexes in
7591 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
7594 if (i
< current_vol
)
7596 sprintf(subarray
, "%u", i
);
7597 if (is_subarray_active(subarray
, st
->devnm
)) {
7598 pr_err("deleting subarray-%d would change the UUID of active subarray-%d, aborting\n",
7605 if (st
->update_tail
) {
7606 struct imsm_update_kill_array
*u
= xmalloc(sizeof(*u
));
7608 u
->type
= update_kill_array
;
7609 u
->dev_idx
= current_vol
;
7610 append_metadata_update(st
, u
, sizeof(*u
));
7615 for (dp
= &super
->devlist
; *dp
;)
7616 if ((*dp
)->index
== current_vol
) {
7619 handle_missing(super
, (*dp
)->dev
);
7620 if ((*dp
)->index
> current_vol
)
7625 /* no more raid devices, all active components are now spares,
7626 * but of course failed are still failed
7628 if (--mpb
->num_raid_devs
== 0) {
7631 for (d
= super
->disks
; d
; d
= d
->next
)
7636 super
->updates_pending
++;
7641 static int update_subarray_imsm(struct supertype
*st
, char *subarray
,
7642 char *update
, struct mddev_ident
*ident
)
7644 /* update the subarray currently referenced by ->current_vol */
7645 struct intel_super
*super
= st
->sb
;
7646 struct imsm_super
*mpb
= super
->anchor
;
7648 if (strcmp(update
, "name") == 0) {
7649 char *name
= ident
->name
;
7653 if (is_subarray_active(subarray
, st
->devnm
)) {
7654 pr_err("Unable to update name of active subarray\n");
7658 if (!check_name(super
, name
, 0))
7661 vol
= strtoul(subarray
, &ep
, 10);
7662 if (*ep
!= '\0' || vol
>= super
->anchor
->num_raid_devs
)
7665 if (st
->update_tail
) {
7666 struct imsm_update_rename_array
*u
= xmalloc(sizeof(*u
));
7668 u
->type
= update_rename_array
;
7670 strncpy((char *) u
->name
, name
, MAX_RAID_SERIAL_LEN
);
7671 u
->name
[MAX_RAID_SERIAL_LEN
-1] = '\0';
7672 append_metadata_update(st
, u
, sizeof(*u
));
7674 struct imsm_dev
*dev
;
7677 dev
= get_imsm_dev(super
, vol
);
7678 memset(dev
->volume
, '\0', MAX_RAID_SERIAL_LEN
);
7679 namelen
= min((int)strlen(name
), MAX_RAID_SERIAL_LEN
);
7680 memcpy(dev
->volume
, name
, namelen
);
7681 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
7682 dev
= get_imsm_dev(super
, i
);
7683 handle_missing(super
, dev
);
7685 super
->updates_pending
++;
7687 } else if (strcmp(update
, "ppl") == 0 ||
7688 strcmp(update
, "no-ppl") == 0) {
7691 int vol
= strtoul(subarray
, &ep
, 10);
7693 if (*ep
!= '\0' || vol
>= super
->anchor
->num_raid_devs
)
7696 if (strcmp(update
, "ppl") == 0)
7697 new_policy
= RWH_MULTIPLE_DISTRIBUTED
;
7699 new_policy
= RWH_MULTIPLE_OFF
;
7701 if (st
->update_tail
) {
7702 struct imsm_update_rwh_policy
*u
= xmalloc(sizeof(*u
));
7704 u
->type
= update_rwh_policy
;
7706 u
->new_policy
= new_policy
;
7707 append_metadata_update(st
, u
, sizeof(*u
));
7709 struct imsm_dev
*dev
;
7711 dev
= get_imsm_dev(super
, vol
);
7712 dev
->rwh_policy
= new_policy
;
7713 super
->updates_pending
++;
7721 static int is_gen_migration(struct imsm_dev
*dev
)
7726 if (!dev
->vol
.migr_state
)
7729 if (migr_type(dev
) == MIGR_GEN_MIGR
)
7735 static int is_rebuilding(struct imsm_dev
*dev
)
7737 struct imsm_map
*migr_map
;
7739 if (!dev
->vol
.migr_state
)
7742 if (migr_type(dev
) != MIGR_REBUILD
)
7745 migr_map
= get_imsm_map(dev
, MAP_1
);
7747 if (migr_map
->map_state
== IMSM_T_STATE_DEGRADED
)
7753 static int is_initializing(struct imsm_dev
*dev
)
7755 struct imsm_map
*migr_map
;
7757 if (!dev
->vol
.migr_state
)
7760 if (migr_type(dev
) != MIGR_INIT
)
7763 migr_map
= get_imsm_map(dev
, MAP_1
);
7765 if (migr_map
->map_state
== IMSM_T_STATE_UNINITIALIZED
)
7771 static void update_recovery_start(struct intel_super
*super
,
7772 struct imsm_dev
*dev
,
7773 struct mdinfo
*array
)
7775 struct mdinfo
*rebuild
= NULL
;
7779 if (!is_rebuilding(dev
))
7782 /* Find the rebuild target, but punt on the dual rebuild case */
7783 for (d
= array
->devs
; d
; d
= d
->next
)
7784 if (d
->recovery_start
== 0) {
7791 /* (?) none of the disks are marked with
7792 * IMSM_ORD_REBUILD, so assume they are missing and the
7793 * disk_ord_tbl was not correctly updated
7795 dprintf("failed to locate out-of-sync disk\n");
7799 units
= __le32_to_cpu(dev
->vol
.curr_migr_unit
);
7800 rebuild
->recovery_start
= units
* blocks_per_migr_unit(super
, dev
);
7803 static int recover_backup_imsm(struct supertype
*st
, struct mdinfo
*info
);
7805 static struct mdinfo
*container_content_imsm(struct supertype
*st
, char *subarray
)
7807 /* Given a container loaded by load_super_imsm_all,
7808 * extract information about all the arrays into
7810 * If 'subarray' is given, just extract info about that array.
7812 * For each imsm_dev create an mdinfo, fill it in,
7813 * then look for matching devices in super->disks
7814 * and create appropriate device mdinfo.
7816 struct intel_super
*super
= st
->sb
;
7817 struct imsm_super
*mpb
= super
->anchor
;
7818 struct mdinfo
*rest
= NULL
;
7822 int spare_disks
= 0;
7823 int current_vol
= super
->current_vol
;
7825 /* do not assemble arrays when not all attributes are supported */
7826 if (imsm_check_attributes(mpb
->attributes
) == 0) {
7828 pr_err("Unsupported attributes in IMSM metadata.Arrays activation is blocked.\n");
7831 /* count spare devices, not used in maps
7833 for (d
= super
->disks
; d
; d
= d
->next
)
7837 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
7838 struct imsm_dev
*dev
;
7839 struct imsm_map
*map
;
7840 struct imsm_map
*map2
;
7841 struct mdinfo
*this;
7848 (i
!= strtoul(subarray
, &ep
, 10) || *ep
!= '\0'))
7851 dev
= get_imsm_dev(super
, i
);
7852 map
= get_imsm_map(dev
, MAP_0
);
7853 map2
= get_imsm_map(dev
, MAP_1
);
7854 level
= get_imsm_raid_level(map
);
7856 /* do not publish arrays that are in the middle of an
7857 * unsupported migration
7859 if (dev
->vol
.migr_state
&&
7860 (migr_type(dev
) == MIGR_STATE_CHANGE
)) {
7861 pr_err("cannot assemble volume '%.16s': unsupported migration in progress\n",
7865 /* do not publish arrays that are not support by controller's
7869 this = xmalloc(sizeof(*this));
7871 super
->current_vol
= i
;
7872 getinfo_super_imsm_volume(st
, this, NULL
);
7874 chunk
= __le16_to_cpu(map
->blocks_per_strip
) >> 1;
7875 /* mdadm does not support all metadata features- set the bit in all arrays state */
7876 if (!validate_geometry_imsm_orom(super
,
7877 level
, /* RAID level */
7878 imsm_level_to_layout(level
),
7879 map
->num_members
, /* raid disks */
7880 &chunk
, imsm_dev_size(dev
),
7882 pr_err("IMSM RAID geometry validation failed. Array %s activation is blocked.\n",
7884 this->array
.state
|=
7885 (1<<MD_SB_BLOCK_CONTAINER_RESHAPE
) |
7886 (1<<MD_SB_BLOCK_VOLUME
);
7889 /* if array has bad blocks, set suitable bit in all arrays state */
7891 this->array
.state
|=
7892 (1<<MD_SB_BLOCK_CONTAINER_RESHAPE
) |
7893 (1<<MD_SB_BLOCK_VOLUME
);
7895 for (slot
= 0 ; slot
< map
->num_members
; slot
++) {
7896 unsigned long long recovery_start
;
7897 struct mdinfo
*info_d
;
7905 idx
= get_imsm_disk_idx(dev
, slot
, MAP_0
);
7906 ord
= get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
);
7907 for (d
= super
->disks
; d
; d
= d
->next
)
7908 if (d
->index
== idx
)
7911 recovery_start
= MaxSector
;
7914 if (d
&& is_failed(&d
->disk
))
7916 if (!skip
&& (ord
& IMSM_ORD_REBUILD
))
7920 * if we skip some disks the array will be assmebled degraded;
7921 * reset resync start to avoid a dirty-degraded
7922 * situation when performing the intial sync
7927 if (!(dev
->vol
.dirty
& RAIDVOL_DIRTY
)) {
7928 if ((!able_to_resync(level
, missing
) ||
7929 recovery_start
== 0))
7930 this->resync_start
= MaxSector
;
7933 * FIXME handle dirty degraded
7940 info_d
= xcalloc(1, sizeof(*info_d
));
7941 info_d
->next
= this->devs
;
7942 this->devs
= info_d
;
7944 info_d
->disk
.number
= d
->index
;
7945 info_d
->disk
.major
= d
->major
;
7946 info_d
->disk
.minor
= d
->minor
;
7947 info_d
->disk
.raid_disk
= slot
;
7948 info_d
->recovery_start
= recovery_start
;
7950 if (slot
< map2
->num_members
)
7951 info_d
->disk
.state
= (1 << MD_DISK_ACTIVE
);
7953 this->array
.spare_disks
++;
7955 if (slot
< map
->num_members
)
7956 info_d
->disk
.state
= (1 << MD_DISK_ACTIVE
);
7958 this->array
.spare_disks
++;
7960 if (info_d
->recovery_start
== MaxSector
)
7961 this->array
.working_disks
++;
7963 info_d
->events
= __le32_to_cpu(mpb
->generation_num
);
7964 info_d
->data_offset
= pba_of_lba0(map
);
7965 info_d
->component_size
= calc_component_size(map
, dev
);
7967 if (map
->raid_level
== 5) {
7968 info_d
->ppl_sector
= this->ppl_sector
;
7969 info_d
->ppl_size
= this->ppl_size
;
7970 if (this->consistency_policy
== CONSISTENCY_POLICY_PPL
&&
7971 recovery_start
== 0)
7972 this->resync_start
= 0;
7975 info_d
->bb
.supported
= 1;
7976 get_volume_badblocks(super
->bbm_log
, ord_to_idx(ord
),
7977 info_d
->data_offset
,
7978 info_d
->component_size
,
7981 /* now that the disk list is up-to-date fixup recovery_start */
7982 update_recovery_start(super
, dev
, this);
7983 this->array
.spare_disks
+= spare_disks
;
7985 /* check for reshape */
7986 if (this->reshape_active
== 1)
7987 recover_backup_imsm(st
, this);
7991 super
->current_vol
= current_vol
;
7995 static __u8
imsm_check_degraded(struct intel_super
*super
, struct imsm_dev
*dev
,
7996 int failed
, int look_in_map
)
7998 struct imsm_map
*map
;
8000 map
= get_imsm_map(dev
, look_in_map
);
8003 return map
->map_state
== IMSM_T_STATE_UNINITIALIZED
?
8004 IMSM_T_STATE_UNINITIALIZED
: IMSM_T_STATE_NORMAL
;
8006 switch (get_imsm_raid_level(map
)) {
8008 return IMSM_T_STATE_FAILED
;
8011 if (failed
< map
->num_members
)
8012 return IMSM_T_STATE_DEGRADED
;
8014 return IMSM_T_STATE_FAILED
;
8019 * check to see if any mirrors have failed, otherwise we
8020 * are degraded. Even numbered slots are mirrored on
8024 /* gcc -Os complains that this is unused */
8025 int insync
= insync
;
8027 for (i
= 0; i
< map
->num_members
; i
++) {
8028 __u32 ord
= get_imsm_ord_tbl_ent(dev
, i
, MAP_X
);
8029 int idx
= ord_to_idx(ord
);
8030 struct imsm_disk
*disk
;
8032 /* reset the potential in-sync count on even-numbered
8033 * slots. num_copies is always 2 for imsm raid10
8038 disk
= get_imsm_disk(super
, idx
);
8039 if (!disk
|| is_failed(disk
) || ord
& IMSM_ORD_REBUILD
)
8042 /* no in-sync disks left in this mirror the
8046 return IMSM_T_STATE_FAILED
;
8049 return IMSM_T_STATE_DEGRADED
;
8053 return IMSM_T_STATE_DEGRADED
;
8055 return IMSM_T_STATE_FAILED
;
8061 return map
->map_state
;
8064 static int imsm_count_failed(struct intel_super
*super
, struct imsm_dev
*dev
,
8069 struct imsm_disk
*disk
;
8070 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8071 struct imsm_map
*prev
= get_imsm_map(dev
, MAP_1
);
8072 struct imsm_map
*map_for_loop
;
8077 /* at the beginning of migration we set IMSM_ORD_REBUILD on
8078 * disks that are being rebuilt. New failures are recorded to
8079 * map[0]. So we look through all the disks we started with and
8080 * see if any failures are still present, or if any new ones
8084 if (prev
&& (map
->num_members
< prev
->num_members
))
8085 map_for_loop
= prev
;
8087 for (i
= 0; i
< map_for_loop
->num_members
; i
++) {
8089 /* when MAP_X is passed both maps failures are counted
8092 (look_in_map
== MAP_1
|| look_in_map
== MAP_X
) &&
8093 i
< prev
->num_members
) {
8094 ord
= __le32_to_cpu(prev
->disk_ord_tbl
[i
]);
8095 idx_1
= ord_to_idx(ord
);
8097 disk
= get_imsm_disk(super
, idx_1
);
8098 if (!disk
|| is_failed(disk
) || ord
& IMSM_ORD_REBUILD
)
8101 if ((look_in_map
== MAP_0
|| look_in_map
== MAP_X
) &&
8102 i
< map
->num_members
) {
8103 ord
= __le32_to_cpu(map
->disk_ord_tbl
[i
]);
8104 idx
= ord_to_idx(ord
);
8107 disk
= get_imsm_disk(super
, idx
);
8108 if (!disk
|| is_failed(disk
) ||
8109 ord
& IMSM_ORD_REBUILD
)
8118 static int imsm_open_new(struct supertype
*c
, struct active_array
*a
,
8121 struct intel_super
*super
= c
->sb
;
8122 struct imsm_super
*mpb
= super
->anchor
;
8123 struct imsm_update_prealloc_bb_mem u
;
8125 if (atoi(inst
) >= mpb
->num_raid_devs
) {
8126 pr_err("subarry index %d, out of range\n", atoi(inst
));
8130 dprintf("imsm: open_new %s\n", inst
);
8131 a
->info
.container_member
= atoi(inst
);
8133 u
.type
= update_prealloc_badblocks_mem
;
8134 imsm_update_metadata_locally(c
, &u
, sizeof(u
));
8139 static int is_resyncing(struct imsm_dev
*dev
)
8141 struct imsm_map
*migr_map
;
8143 if (!dev
->vol
.migr_state
)
8146 if (migr_type(dev
) == MIGR_INIT
||
8147 migr_type(dev
) == MIGR_REPAIR
)
8150 if (migr_type(dev
) == MIGR_GEN_MIGR
)
8153 migr_map
= get_imsm_map(dev
, MAP_1
);
8155 if (migr_map
->map_state
== IMSM_T_STATE_NORMAL
&&
8156 dev
->vol
.migr_type
!= MIGR_GEN_MIGR
)
8162 /* return true if we recorded new information */
8163 static int mark_failure(struct intel_super
*super
,
8164 struct imsm_dev
*dev
, struct imsm_disk
*disk
, int idx
)
8168 struct imsm_map
*map
;
8169 char buf
[MAX_RAID_SERIAL_LEN
+3];
8170 unsigned int len
, shift
= 0;
8172 /* new failures are always set in map[0] */
8173 map
= get_imsm_map(dev
, MAP_0
);
8175 slot
= get_imsm_disk_slot(map
, idx
);
8179 ord
= __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
8180 if (is_failed(disk
) && (ord
& IMSM_ORD_REBUILD
))
8183 memcpy(buf
, disk
->serial
, MAX_RAID_SERIAL_LEN
);
8184 buf
[MAX_RAID_SERIAL_LEN
] = '\000';
8186 if ((len
= strlen(buf
)) >= MAX_RAID_SERIAL_LEN
)
8187 shift
= len
- MAX_RAID_SERIAL_LEN
+ 1;
8188 memcpy(disk
->serial
, &buf
[shift
], len
+ 1 - shift
);
8190 disk
->status
|= FAILED_DISK
;
8191 set_imsm_ord_tbl_ent(map
, slot
, idx
| IMSM_ORD_REBUILD
);
8192 /* mark failures in second map if second map exists and this disk
8194 * This is valid for migration, initialization and rebuild
8196 if (dev
->vol
.migr_state
) {
8197 struct imsm_map
*map2
= get_imsm_map(dev
, MAP_1
);
8198 int slot2
= get_imsm_disk_slot(map2
, idx
);
8200 if (slot2
< map2
->num_members
&& slot2
>= 0)
8201 set_imsm_ord_tbl_ent(map2
, slot2
,
8202 idx
| IMSM_ORD_REBUILD
);
8204 if (map
->failed_disk_num
== 0xff ||
8205 (!is_rebuilding(dev
) && map
->failed_disk_num
> slot
))
8206 map
->failed_disk_num
= slot
;
8208 clear_disk_badblocks(super
->bbm_log
, ord_to_idx(ord
));
8213 static void mark_missing(struct intel_super
*super
,
8214 struct imsm_dev
*dev
, struct imsm_disk
*disk
, int idx
)
8216 mark_failure(super
, dev
, disk
, idx
);
8218 if (disk
->scsi_id
== __cpu_to_le32(~(__u32
)0))
8221 disk
->scsi_id
= __cpu_to_le32(~(__u32
)0);
8222 memmove(&disk
->serial
[0], &disk
->serial
[1], MAX_RAID_SERIAL_LEN
- 1);
8225 static void handle_missing(struct intel_super
*super
, struct imsm_dev
*dev
)
8229 if (!super
->missing
)
8232 /* When orom adds replacement for missing disk it does
8233 * not remove entry of missing disk, but just updates map with
8234 * new added disk. So it is not enough just to test if there is
8235 * any missing disk, we have to look if there are any failed disks
8236 * in map to stop migration */
8238 dprintf("imsm: mark missing\n");
8239 /* end process for initialization and rebuild only
8241 if (is_gen_migration(dev
) == 0) {
8242 int failed
= imsm_count_failed(super
, dev
, MAP_0
);
8246 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8247 struct imsm_map
*map1
;
8248 int i
, ord
, ord_map1
;
8251 for (i
= 0; i
< map
->num_members
; i
++) {
8252 ord
= get_imsm_ord_tbl_ent(dev
, i
, MAP_0
);
8253 if (!(ord
& IMSM_ORD_REBUILD
))
8256 map1
= get_imsm_map(dev
, MAP_1
);
8260 ord_map1
= __le32_to_cpu(map1
->disk_ord_tbl
[i
]);
8261 if (ord_map1
& IMSM_ORD_REBUILD
)
8266 map_state
= imsm_check_degraded(super
, dev
,
8268 end_migration(dev
, super
, map_state
);
8272 for (dl
= super
->missing
; dl
; dl
= dl
->next
)
8273 mark_missing(super
, dev
, &dl
->disk
, dl
->index
);
8274 super
->updates_pending
++;
8277 static unsigned long long imsm_set_array_size(struct imsm_dev
*dev
,
8280 unsigned long long array_blocks
;
8281 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8282 int used_disks
= imsm_num_data_members(map
);
8284 if (used_disks
== 0) {
8285 /* when problems occures
8286 * return current array_blocks value
8288 array_blocks
= imsm_dev_size(dev
);
8290 return array_blocks
;
8293 /* set array size in metadata
8296 /* OLCE size change is caused by added disks
8298 array_blocks
= per_dev_array_size(map
) * used_disks
;
8300 /* Online Volume Size Change
8301 * Using available free space
8303 array_blocks
= new_size
;
8305 array_blocks
= round_size_to_mb(array_blocks
, used_disks
);
8306 set_imsm_dev_size(dev
, array_blocks
);
8308 return array_blocks
;
8311 static void imsm_set_disk(struct active_array
*a
, int n
, int state
);
8313 static void imsm_progress_container_reshape(struct intel_super
*super
)
8315 /* if no device has a migr_state, but some device has a
8316 * different number of members than the previous device, start
8317 * changing the number of devices in this device to match
8320 struct imsm_super
*mpb
= super
->anchor
;
8321 int prev_disks
= -1;
8325 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
8326 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
8327 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8328 struct imsm_map
*map2
;
8329 int prev_num_members
;
8331 if (dev
->vol
.migr_state
)
8334 if (prev_disks
== -1)
8335 prev_disks
= map
->num_members
;
8336 if (prev_disks
== map
->num_members
)
8339 /* OK, this array needs to enter reshape mode.
8340 * i.e it needs a migr_state
8343 copy_map_size
= sizeof_imsm_map(map
);
8344 prev_num_members
= map
->num_members
;
8345 map
->num_members
= prev_disks
;
8346 dev
->vol
.migr_state
= 1;
8347 dev
->vol
.curr_migr_unit
= 0;
8348 set_migr_type(dev
, MIGR_GEN_MIGR
);
8349 for (i
= prev_num_members
;
8350 i
< map
->num_members
; i
++)
8351 set_imsm_ord_tbl_ent(map
, i
, i
);
8352 map2
= get_imsm_map(dev
, MAP_1
);
8353 /* Copy the current map */
8354 memcpy(map2
, map
, copy_map_size
);
8355 map2
->num_members
= prev_num_members
;
8357 imsm_set_array_size(dev
, -1);
8358 super
->clean_migration_record_by_mdmon
= 1;
8359 super
->updates_pending
++;
8363 /* Handle dirty -> clean transititions, resync and reshape. Degraded and rebuild
8364 * states are handled in imsm_set_disk() with one exception, when a
8365 * resync is stopped due to a new failure this routine will set the
8366 * 'degraded' state for the array.
8368 static int imsm_set_array_state(struct active_array
*a
, int consistent
)
8370 int inst
= a
->info
.container_member
;
8371 struct intel_super
*super
= a
->container
->sb
;
8372 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
8373 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8374 int failed
= imsm_count_failed(super
, dev
, MAP_0
);
8375 __u8 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
8376 __u32 blocks_per_unit
;
8378 if (dev
->vol
.migr_state
&&
8379 dev
->vol
.migr_type
== MIGR_GEN_MIGR
) {
8380 /* array state change is blocked due to reshape action
8382 * - abort the reshape (if last_checkpoint is 0 and action!= reshape)
8383 * - finish the reshape (if last_checkpoint is big and action != reshape)
8384 * - update curr_migr_unit
8386 if (a
->curr_action
== reshape
) {
8387 /* still reshaping, maybe update curr_migr_unit */
8388 goto mark_checkpoint
;
8390 if (a
->last_checkpoint
== 0 && a
->prev_action
== reshape
) {
8391 /* for some reason we aborted the reshape.
8393 * disable automatic metadata rollback
8394 * user action is required to recover process
8397 struct imsm_map
*map2
=
8398 get_imsm_map(dev
, MAP_1
);
8399 dev
->vol
.migr_state
= 0;
8400 set_migr_type(dev
, 0);
8401 dev
->vol
.curr_migr_unit
= 0;
8403 sizeof_imsm_map(map2
));
8404 super
->updates_pending
++;
8407 if (a
->last_checkpoint
>= a
->info
.component_size
) {
8408 unsigned long long array_blocks
;
8412 used_disks
= imsm_num_data_members(map
);
8413 if (used_disks
> 0) {
8415 per_dev_array_size(map
) *
8418 round_size_to_mb(array_blocks
,
8420 a
->info
.custom_array_size
= array_blocks
;
8421 /* encourage manager to update array
8425 a
->check_reshape
= 1;
8427 /* finalize online capacity expansion/reshape */
8428 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
)
8430 mdi
->disk
.raid_disk
,
8433 imsm_progress_container_reshape(super
);
8438 /* before we activate this array handle any missing disks */
8439 if (consistent
== 2)
8440 handle_missing(super
, dev
);
8442 if (consistent
== 2 &&
8443 (!is_resync_complete(&a
->info
) ||
8444 map_state
!= IMSM_T_STATE_NORMAL
||
8445 dev
->vol
.migr_state
))
8448 if (is_resync_complete(&a
->info
)) {
8449 /* complete intialization / resync,
8450 * recovery and interrupted recovery is completed in
8453 if (is_resyncing(dev
)) {
8454 dprintf("imsm: mark resync done\n");
8455 end_migration(dev
, super
, map_state
);
8456 super
->updates_pending
++;
8457 a
->last_checkpoint
= 0;
8459 } else if ((!is_resyncing(dev
) && !failed
) &&
8460 (imsm_reshape_blocks_arrays_changes(super
) == 0)) {
8461 /* mark the start of the init process if nothing is failed */
8462 dprintf("imsm: mark resync start\n");
8463 if (map
->map_state
== IMSM_T_STATE_UNINITIALIZED
)
8464 migrate(dev
, super
, IMSM_T_STATE_NORMAL
, MIGR_INIT
);
8466 migrate(dev
, super
, IMSM_T_STATE_NORMAL
, MIGR_REPAIR
);
8467 super
->updates_pending
++;
8471 /* skip checkpointing for general migration,
8472 * it is controlled in mdadm
8474 if (is_gen_migration(dev
))
8475 goto skip_mark_checkpoint
;
8477 /* check if we can update curr_migr_unit from resync_start, recovery_start */
8478 blocks_per_unit
= blocks_per_migr_unit(super
, dev
);
8479 if (blocks_per_unit
) {
8483 units
= a
->last_checkpoint
/ blocks_per_unit
;
8486 /* check that we did not overflow 32-bits, and that
8487 * curr_migr_unit needs updating
8489 if (units32
== units
&&
8491 __le32_to_cpu(dev
->vol
.curr_migr_unit
) != units32
) {
8492 dprintf("imsm: mark checkpoint (%u)\n", units32
);
8493 dev
->vol
.curr_migr_unit
= __cpu_to_le32(units32
);
8494 super
->updates_pending
++;
8498 skip_mark_checkpoint
:
8499 /* mark dirty / clean */
8500 if (((dev
->vol
.dirty
& RAIDVOL_DIRTY
) && consistent
) ||
8501 (!(dev
->vol
.dirty
& RAIDVOL_DIRTY
) && !consistent
)) {
8502 dprintf("imsm: mark '%s'\n", consistent
? "clean" : "dirty");
8504 dev
->vol
.dirty
= RAIDVOL_CLEAN
;
8506 dev
->vol
.dirty
= RAIDVOL_DIRTY
;
8507 if (dev
->rwh_policy
== RWH_DISTRIBUTED
||
8508 dev
->rwh_policy
== RWH_MULTIPLE_DISTRIBUTED
)
8509 dev
->vol
.dirty
|= RAIDVOL_DSRECORD_VALID
;
8511 super
->updates_pending
++;
8517 static int imsm_disk_slot_to_ord(struct active_array
*a
, int slot
)
8519 int inst
= a
->info
.container_member
;
8520 struct intel_super
*super
= a
->container
->sb
;
8521 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
8522 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8524 if (slot
> map
->num_members
) {
8525 pr_err("imsm: imsm_disk_slot_to_ord %d out of range 0..%d\n",
8526 slot
, map
->num_members
- 1);
8533 return get_imsm_ord_tbl_ent(dev
, slot
, MAP_0
);
8536 static void imsm_set_disk(struct active_array
*a
, int n
, int state
)
8538 int inst
= a
->info
.container_member
;
8539 struct intel_super
*super
= a
->container
->sb
;
8540 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
8541 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8542 struct imsm_disk
*disk
;
8544 int recovery_not_finished
= 0;
8548 int rebuild_done
= 0;
8551 ord
= get_imsm_ord_tbl_ent(dev
, n
, MAP_X
);
8555 dprintf("imsm: set_disk %d:%x\n", n
, state
);
8556 disk
= get_imsm_disk(super
, ord_to_idx(ord
));
8558 /* check for new failures */
8559 if (disk
&& (state
& DS_FAULTY
)) {
8560 if (mark_failure(super
, dev
, disk
, ord_to_idx(ord
)))
8561 super
->updates_pending
++;
8564 /* check if in_sync */
8565 if (state
& DS_INSYNC
&& ord
& IMSM_ORD_REBUILD
&& is_rebuilding(dev
)) {
8566 struct imsm_map
*migr_map
= get_imsm_map(dev
, MAP_1
);
8568 set_imsm_ord_tbl_ent(migr_map
, n
, ord_to_idx(ord
));
8570 super
->updates_pending
++;
8573 failed
= imsm_count_failed(super
, dev
, MAP_0
);
8574 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
8576 /* check if recovery complete, newly degraded, or failed */
8577 dprintf("imsm: Detected transition to state ");
8578 switch (map_state
) {
8579 case IMSM_T_STATE_NORMAL
: /* transition to normal state */
8580 dprintf("normal: ");
8581 if (is_rebuilding(dev
)) {
8582 dprintf_cont("while rebuilding");
8583 /* check if recovery is really finished */
8584 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
)
8585 if (mdi
->recovery_start
!= MaxSector
) {
8586 recovery_not_finished
= 1;
8589 if (recovery_not_finished
) {
8591 dprintf("Rebuild has not finished yet, state not changed");
8592 if (a
->last_checkpoint
< mdi
->recovery_start
) {
8593 a
->last_checkpoint
= mdi
->recovery_start
;
8594 super
->updates_pending
++;
8598 end_migration(dev
, super
, map_state
);
8599 map
= get_imsm_map(dev
, MAP_0
);
8600 map
->failed_disk_num
= ~0;
8601 super
->updates_pending
++;
8602 a
->last_checkpoint
= 0;
8605 if (is_gen_migration(dev
)) {
8606 dprintf_cont("while general migration");
8607 if (a
->last_checkpoint
>= a
->info
.component_size
)
8608 end_migration(dev
, super
, map_state
);
8610 map
->map_state
= map_state
;
8611 map
= get_imsm_map(dev
, MAP_0
);
8612 map
->failed_disk_num
= ~0;
8613 super
->updates_pending
++;
8617 case IMSM_T_STATE_DEGRADED
: /* transition to degraded state */
8618 dprintf_cont("degraded: ");
8619 if (map
->map_state
!= map_state
&& !dev
->vol
.migr_state
) {
8620 dprintf_cont("mark degraded");
8621 map
->map_state
= map_state
;
8622 super
->updates_pending
++;
8623 a
->last_checkpoint
= 0;
8626 if (is_rebuilding(dev
)) {
8627 dprintf_cont("while rebuilding ");
8628 if (state
& DS_FAULTY
) {
8629 dprintf_cont("removing failed drive ");
8630 if (n
== map
->failed_disk_num
) {
8631 dprintf_cont("end migration");
8632 end_migration(dev
, super
, map_state
);
8633 a
->last_checkpoint
= 0;
8635 dprintf_cont("fail detected during rebuild, changing map state");
8636 map
->map_state
= map_state
;
8638 super
->updates_pending
++;
8644 /* check if recovery is really finished */
8645 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
)
8646 if (mdi
->recovery_start
!= MaxSector
) {
8647 recovery_not_finished
= 1;
8650 if (recovery_not_finished
) {
8652 dprintf_cont("Rebuild has not finished yet");
8653 if (a
->last_checkpoint
< mdi
->recovery_start
) {
8654 a
->last_checkpoint
=
8655 mdi
->recovery_start
;
8656 super
->updates_pending
++;
8661 dprintf_cont(" Rebuild done, still degraded");
8662 end_migration(dev
, super
, map_state
);
8663 a
->last_checkpoint
= 0;
8664 super
->updates_pending
++;
8666 for (i
= 0; i
< map
->num_members
; i
++) {
8667 int idx
= get_imsm_ord_tbl_ent(dev
, i
, MAP_0
);
8669 if (idx
& IMSM_ORD_REBUILD
)
8670 map
->failed_disk_num
= i
;
8672 super
->updates_pending
++;
8675 if (is_gen_migration(dev
)) {
8676 dprintf_cont("while general migration");
8677 if (a
->last_checkpoint
>= a
->info
.component_size
)
8678 end_migration(dev
, super
, map_state
);
8680 map
->map_state
= map_state
;
8681 manage_second_map(super
, dev
);
8683 super
->updates_pending
++;
8686 if (is_initializing(dev
)) {
8687 dprintf_cont("while initialization.");
8688 map
->map_state
= map_state
;
8689 super
->updates_pending
++;
8693 case IMSM_T_STATE_FAILED
: /* transition to failed state */
8694 dprintf_cont("failed: ");
8695 if (is_gen_migration(dev
)) {
8696 dprintf_cont("while general migration");
8697 map
->map_state
= map_state
;
8698 super
->updates_pending
++;
8701 if (map
->map_state
!= map_state
) {
8702 dprintf_cont("mark failed");
8703 end_migration(dev
, super
, map_state
);
8704 super
->updates_pending
++;
8705 a
->last_checkpoint
= 0;
8710 dprintf_cont("state %i\n", map_state
);
8715 static int store_imsm_mpb(int fd
, struct imsm_super
*mpb
)
8718 __u32 mpb_size
= __le32_to_cpu(mpb
->mpb_size
);
8719 unsigned long long dsize
;
8720 unsigned long long sectors
;
8721 unsigned int sector_size
;
8723 get_dev_sector_size(fd
, NULL
, §or_size
);
8724 get_dev_size(fd
, NULL
, &dsize
);
8726 if (mpb_size
> sector_size
) {
8727 /* -1 to account for anchor */
8728 sectors
= mpb_sectors(mpb
, sector_size
) - 1;
8730 /* write the extended mpb to the sectors preceeding the anchor */
8731 if (lseek64(fd
, dsize
- (sector_size
* (2 + sectors
)),
8735 if ((unsigned long long)write(fd
, buf
+ sector_size
,
8736 sector_size
* sectors
) != sector_size
* sectors
)
8740 /* first block is stored on second to last sector of the disk */
8741 if (lseek64(fd
, dsize
- (sector_size
* 2), SEEK_SET
) < 0)
8744 if ((unsigned int)write(fd
, buf
, sector_size
) != sector_size
)
8750 static void imsm_sync_metadata(struct supertype
*container
)
8752 struct intel_super
*super
= container
->sb
;
8754 dprintf("sync metadata: %d\n", super
->updates_pending
);
8755 if (!super
->updates_pending
)
8758 write_super_imsm(container
, 0);
8760 super
->updates_pending
= 0;
8763 static struct dl
*imsm_readd(struct intel_super
*super
, int idx
, struct active_array
*a
)
8765 struct imsm_dev
*dev
= get_imsm_dev(super
, a
->info
.container_member
);
8766 int i
= get_imsm_disk_idx(dev
, idx
, MAP_X
);
8769 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
8773 if (dl
&& is_failed(&dl
->disk
))
8777 dprintf("found %x:%x\n", dl
->major
, dl
->minor
);
8782 static struct dl
*imsm_add_spare(struct intel_super
*super
, int slot
,
8783 struct active_array
*a
, int activate_new
,
8784 struct mdinfo
*additional_test_list
)
8786 struct imsm_dev
*dev
= get_imsm_dev(super
, a
->info
.container_member
);
8787 int idx
= get_imsm_disk_idx(dev
, slot
, MAP_X
);
8788 struct imsm_super
*mpb
= super
->anchor
;
8789 struct imsm_map
*map
;
8790 unsigned long long pos
;
8795 __u32 array_start
= 0;
8796 __u32 array_end
= 0;
8798 struct mdinfo
*test_list
;
8800 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
8801 /* If in this array, skip */
8802 for (d
= a
->info
.devs
; d
; d
= d
->next
)
8803 if (d
->state_fd
>= 0 &&
8804 d
->disk
.major
== dl
->major
&&
8805 d
->disk
.minor
== dl
->minor
) {
8806 dprintf("%x:%x already in array\n",
8807 dl
->major
, dl
->minor
);
8812 test_list
= additional_test_list
;
8814 if (test_list
->disk
.major
== dl
->major
&&
8815 test_list
->disk
.minor
== dl
->minor
) {
8816 dprintf("%x:%x already in additional test list\n",
8817 dl
->major
, dl
->minor
);
8820 test_list
= test_list
->next
;
8825 /* skip in use or failed drives */
8826 if (is_failed(&dl
->disk
) || idx
== dl
->index
||
8828 dprintf("%x:%x status (failed: %d index: %d)\n",
8829 dl
->major
, dl
->minor
, is_failed(&dl
->disk
), idx
);
8833 /* skip pure spares when we are looking for partially
8834 * assimilated drives
8836 if (dl
->index
== -1 && !activate_new
)
8839 if (!drive_validate_sector_size(super
, dl
))
8842 /* Does this unused device have the requisite free space?
8843 * It needs to be able to cover all member volumes
8845 ex
= get_extents(super
, dl
, 1);
8847 dprintf("cannot get extents\n");
8850 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
8851 dev
= get_imsm_dev(super
, i
);
8852 map
= get_imsm_map(dev
, MAP_0
);
8854 /* check if this disk is already a member of
8857 if (get_imsm_disk_slot(map
, dl
->index
) >= 0)
8863 array_start
= pba_of_lba0(map
);
8864 array_end
= array_start
+
8865 per_dev_array_size(map
) - 1;
8868 /* check that we can start at pba_of_lba0 with
8869 * num_data_stripes*blocks_per_stripe of space
8871 if (array_start
>= pos
&& array_end
< ex
[j
].start
) {
8875 pos
= ex
[j
].start
+ ex
[j
].size
;
8877 } while (ex
[j
-1].size
);
8884 if (i
< mpb
->num_raid_devs
) {
8885 dprintf("%x:%x does not have %u to %u available\n",
8886 dl
->major
, dl
->minor
, array_start
, array_end
);
8896 static int imsm_rebuild_allowed(struct supertype
*cont
, int dev_idx
, int failed
)
8898 struct imsm_dev
*dev2
;
8899 struct imsm_map
*map
;
8905 dev2
= get_imsm_dev(cont
->sb
, dev_idx
);
8907 state
= imsm_check_degraded(cont
->sb
, dev2
, failed
, MAP_0
);
8908 if (state
== IMSM_T_STATE_FAILED
) {
8909 map
= get_imsm_map(dev2
, MAP_0
);
8912 for (slot
= 0; slot
< map
->num_members
; slot
++) {
8914 * Check if failed disks are deleted from intel
8915 * disk list or are marked to be deleted
8917 idx
= get_imsm_disk_idx(dev2
, slot
, MAP_X
);
8918 idisk
= get_imsm_dl_disk(cont
->sb
, idx
);
8920 * Do not rebuild the array if failed disks
8921 * from failed sub-array are not removed from
8925 is_failed(&idisk
->disk
) &&
8926 (idisk
->action
!= DISK_REMOVE
))
8934 static struct mdinfo
*imsm_activate_spare(struct active_array
*a
,
8935 struct metadata_update
**updates
)
8938 * Find a device with unused free space and use it to replace a
8939 * failed/vacant region in an array. We replace failed regions one a
8940 * array at a time. The result is that a new spare disk will be added
8941 * to the first failed array and after the monitor has finished
8942 * propagating failures the remainder will be consumed.
8944 * FIXME add a capability for mdmon to request spares from another
8948 struct intel_super
*super
= a
->container
->sb
;
8949 int inst
= a
->info
.container_member
;
8950 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
8951 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8952 int failed
= a
->info
.array
.raid_disks
;
8953 struct mdinfo
*rv
= NULL
;
8956 struct metadata_update
*mu
;
8958 struct imsm_update_activate_spare
*u
;
8963 for (d
= a
->info
.devs
; d
; d
= d
->next
) {
8964 if ((d
->curr_state
& DS_FAULTY
) &&
8966 /* wait for Removal to happen */
8968 if (d
->state_fd
>= 0)
8972 dprintf("imsm: activate spare: inst=%d failed=%d (%d) level=%d\n",
8973 inst
, failed
, a
->info
.array
.raid_disks
, a
->info
.array
.level
);
8975 if (imsm_reshape_blocks_arrays_changes(super
))
8978 /* Cannot activate another spare if rebuild is in progress already
8980 if (is_rebuilding(dev
)) {
8981 dprintf("imsm: No spare activation allowed. Rebuild in progress already.\n");
8985 if (a
->info
.array
.level
== 4)
8986 /* No repair for takeovered array
8987 * imsm doesn't support raid4
8991 if (imsm_check_degraded(super
, dev
, failed
, MAP_0
) !=
8992 IMSM_T_STATE_DEGRADED
)
8995 if (get_imsm_map(dev
, MAP_0
)->map_state
== IMSM_T_STATE_UNINITIALIZED
) {
8996 dprintf("imsm: No spare activation allowed. Volume is not initialized.\n");
9001 * If there are any failed disks check state of the other volume.
9002 * Block rebuild if the another one is failed until failed disks
9003 * are removed from container.
9006 dprintf("found failed disks in %.*s, check if there anotherfailed sub-array.\n",
9007 MAX_RAID_SERIAL_LEN
, dev
->volume
);
9008 /* check if states of the other volumes allow for rebuild */
9009 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
9011 allowed
= imsm_rebuild_allowed(a
->container
,
9019 /* For each slot, if it is not working, find a spare */
9020 for (i
= 0; i
< a
->info
.array
.raid_disks
; i
++) {
9021 for (d
= a
->info
.devs
; d
; d
= d
->next
)
9022 if (d
->disk
.raid_disk
== i
)
9024 dprintf("found %d: %p %x\n", i
, d
, d
?d
->curr_state
:0);
9025 if (d
&& (d
->state_fd
>= 0))
9029 * OK, this device needs recovery. Try to re-add the
9030 * previous occupant of this slot, if this fails see if
9031 * we can continue the assimilation of a spare that was
9032 * partially assimilated, finally try to activate a new
9035 dl
= imsm_readd(super
, i
, a
);
9037 dl
= imsm_add_spare(super
, i
, a
, 0, rv
);
9039 dl
= imsm_add_spare(super
, i
, a
, 1, rv
);
9043 /* found a usable disk with enough space */
9044 di
= xcalloc(1, sizeof(*di
));
9046 /* dl->index will be -1 in the case we are activating a
9047 * pristine spare. imsm_process_update() will create a
9048 * new index in this case. Once a disk is found to be
9049 * failed in all member arrays it is kicked from the
9052 di
->disk
.number
= dl
->index
;
9054 /* (ab)use di->devs to store a pointer to the device
9057 di
->devs
= (struct mdinfo
*) dl
;
9059 di
->disk
.raid_disk
= i
;
9060 di
->disk
.major
= dl
->major
;
9061 di
->disk
.minor
= dl
->minor
;
9063 di
->recovery_start
= 0;
9064 di
->data_offset
= pba_of_lba0(map
);
9065 di
->component_size
= a
->info
.component_size
;
9066 di
->container_member
= inst
;
9067 di
->bb
.supported
= 1;
9068 if (a
->info
.consistency_policy
== CONSISTENCY_POLICY_PPL
) {
9069 di
->ppl_sector
= get_ppl_sector(super
, inst
);
9070 di
->ppl_size
= MULTIPLE_PPL_AREA_SIZE_IMSM
>> 9;
9072 super
->random
= random32();
9076 dprintf("%x:%x to be %d at %llu\n", dl
->major
, dl
->minor
,
9077 i
, di
->data_offset
);
9081 /* No spares found */
9083 /* Now 'rv' has a list of devices to return.
9084 * Create a metadata_update record to update the
9085 * disk_ord_tbl for the array
9087 mu
= xmalloc(sizeof(*mu
));
9088 mu
->buf
= xcalloc(num_spares
,
9089 sizeof(struct imsm_update_activate_spare
));
9091 mu
->space_list
= NULL
;
9092 mu
->len
= sizeof(struct imsm_update_activate_spare
) * num_spares
;
9093 mu
->next
= *updates
;
9094 u
= (struct imsm_update_activate_spare
*) mu
->buf
;
9096 for (di
= rv
; di
; di
= di
->next
) {
9097 u
->type
= update_activate_spare
;
9098 u
->dl
= (struct dl
*) di
->devs
;
9100 u
->slot
= di
->disk
.raid_disk
;
9111 static int disks_overlap(struct intel_super
*super
, int idx
, struct imsm_update_create_array
*u
)
9113 struct imsm_dev
*dev
= get_imsm_dev(super
, idx
);
9114 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
9115 struct imsm_map
*new_map
= get_imsm_map(&u
->dev
, MAP_0
);
9116 struct disk_info
*inf
= get_disk_info(u
);
9117 struct imsm_disk
*disk
;
9121 for (i
= 0; i
< map
->num_members
; i
++) {
9122 disk
= get_imsm_disk(super
, get_imsm_disk_idx(dev
, i
, MAP_X
));
9123 for (j
= 0; j
< new_map
->num_members
; j
++)
9124 if (serialcmp(disk
->serial
, inf
[j
].serial
) == 0)
9131 static struct dl
*get_disk_super(struct intel_super
*super
, int major
, int minor
)
9135 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
9136 if (dl
->major
== major
&& dl
->minor
== minor
)
9141 static int remove_disk_super(struct intel_super
*super
, int major
, int minor
)
9147 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
9148 if (dl
->major
== major
&& dl
->minor
== minor
) {
9151 prev
->next
= dl
->next
;
9153 super
->disks
= dl
->next
;
9155 __free_imsm_disk(dl
);
9156 dprintf("removed %x:%x\n", major
, minor
);
9164 static void imsm_delete(struct intel_super
*super
, struct dl
**dlp
, unsigned index
);
9166 static int add_remove_disk_update(struct intel_super
*super
)
9168 int check_degraded
= 0;
9171 /* add/remove some spares to/from the metadata/contrainer */
9172 while (super
->disk_mgmt_list
) {
9173 struct dl
*disk_cfg
;
9175 disk_cfg
= super
->disk_mgmt_list
;
9176 super
->disk_mgmt_list
= disk_cfg
->next
;
9177 disk_cfg
->next
= NULL
;
9179 if (disk_cfg
->action
== DISK_ADD
) {
9180 disk_cfg
->next
= super
->disks
;
9181 super
->disks
= disk_cfg
;
9183 dprintf("added %x:%x\n",
9184 disk_cfg
->major
, disk_cfg
->minor
);
9185 } else if (disk_cfg
->action
== DISK_REMOVE
) {
9186 dprintf("Disk remove action processed: %x.%x\n",
9187 disk_cfg
->major
, disk_cfg
->minor
);
9188 disk
= get_disk_super(super
,
9192 /* store action status */
9193 disk
->action
= DISK_REMOVE
;
9194 /* remove spare disks only */
9195 if (disk
->index
== -1) {
9196 remove_disk_super(super
,
9200 disk_cfg
->fd
= disk
->fd
;
9204 /* release allocate disk structure */
9205 __free_imsm_disk(disk_cfg
);
9208 return check_degraded
;
9211 static int apply_reshape_migration_update(struct imsm_update_reshape_migration
*u
,
9212 struct intel_super
*super
,
9215 struct intel_dev
*id
;
9216 void **tofree
= NULL
;
9219 dprintf("(enter)\n");
9220 if (u
->subdev
< 0 || u
->subdev
> 1) {
9221 dprintf("imsm: Error: Wrong subdev: %i\n", u
->subdev
);
9224 if (space_list
== NULL
|| *space_list
== NULL
) {
9225 dprintf("imsm: Error: Memory is not allocated\n");
9229 for (id
= super
->devlist
; id
; id
= id
->next
) {
9230 if (id
->index
== (unsigned)u
->subdev
) {
9231 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->subdev
);
9232 struct imsm_map
*map
;
9233 struct imsm_dev
*new_dev
=
9234 (struct imsm_dev
*)*space_list
;
9235 struct imsm_map
*migr_map
= get_imsm_map(dev
, MAP_1
);
9237 struct dl
*new_disk
;
9239 if (new_dev
== NULL
)
9241 *space_list
= **space_list
;
9242 memcpy(new_dev
, dev
, sizeof_imsm_dev(dev
, 0));
9243 map
= get_imsm_map(new_dev
, MAP_0
);
9245 dprintf("imsm: Error: migration in progress");
9249 to_state
= map
->map_state
;
9250 if ((u
->new_level
== 5) && (map
->raid_level
== 0)) {
9252 /* this should not happen */
9253 if (u
->new_disks
[0] < 0) {
9254 map
->failed_disk_num
=
9255 map
->num_members
- 1;
9256 to_state
= IMSM_T_STATE_DEGRADED
;
9258 to_state
= IMSM_T_STATE_NORMAL
;
9260 migrate(new_dev
, super
, to_state
, MIGR_GEN_MIGR
);
9261 if (u
->new_level
> -1)
9262 map
->raid_level
= u
->new_level
;
9263 migr_map
= get_imsm_map(new_dev
, MAP_1
);
9264 if ((u
->new_level
== 5) &&
9265 (migr_map
->raid_level
== 0)) {
9266 int ord
= map
->num_members
- 1;
9267 migr_map
->num_members
--;
9268 if (u
->new_disks
[0] < 0)
9269 ord
|= IMSM_ORD_REBUILD
;
9270 set_imsm_ord_tbl_ent(map
,
9271 map
->num_members
- 1,
9275 tofree
= (void **)dev
;
9277 /* update chunk size
9279 if (u
->new_chunksize
> 0) {
9280 unsigned long long num_data_stripes
;
9281 struct imsm_map
*dest_map
=
9282 get_imsm_map(dev
, MAP_0
);
9284 imsm_num_data_members(dest_map
);
9286 if (used_disks
== 0)
9289 map
->blocks_per_strip
=
9290 __cpu_to_le16(u
->new_chunksize
* 2);
9292 imsm_dev_size(dev
) / used_disks
;
9293 num_data_stripes
/= map
->blocks_per_strip
;
9294 num_data_stripes
/= map
->num_domains
;
9295 set_num_data_stripes(map
, num_data_stripes
);
9298 /* ensure blocks_per_member has valid value
9300 set_blocks_per_member(map
,
9301 per_dev_array_size(map
) +
9302 NUM_BLOCKS_DIRTY_STRIPE_REGION
);
9306 if (u
->new_level
!= 5 || migr_map
->raid_level
!= 0 ||
9307 migr_map
->raid_level
== map
->raid_level
)
9310 if (u
->new_disks
[0] >= 0) {
9313 new_disk
= get_disk_super(super
,
9314 major(u
->new_disks
[0]),
9315 minor(u
->new_disks
[0]));
9316 dprintf("imsm: new disk for reshape is: %i:%i (%p, index = %i)\n",
9317 major(u
->new_disks
[0]),
9318 minor(u
->new_disks
[0]),
9319 new_disk
, new_disk
->index
);
9320 if (new_disk
== NULL
)
9321 goto error_disk_add
;
9323 new_disk
->index
= map
->num_members
- 1;
9324 /* slot to fill in autolayout
9326 new_disk
->raiddisk
= new_disk
->index
;
9327 new_disk
->disk
.status
|= CONFIGURED_DISK
;
9328 new_disk
->disk
.status
&= ~SPARE_DISK
;
9330 goto error_disk_add
;
9333 *tofree
= *space_list
;
9334 /* calculate new size
9336 imsm_set_array_size(new_dev
, -1);
9343 *space_list
= tofree
;
9347 dprintf("Error: imsm: Cannot find disk.\n");
9351 static int apply_size_change_update(struct imsm_update_size_change
*u
,
9352 struct intel_super
*super
)
9354 struct intel_dev
*id
;
9357 dprintf("(enter)\n");
9358 if (u
->subdev
< 0 || u
->subdev
> 1) {
9359 dprintf("imsm: Error: Wrong subdev: %i\n", u
->subdev
);
9363 for (id
= super
->devlist
; id
; id
= id
->next
) {
9364 if (id
->index
== (unsigned)u
->subdev
) {
9365 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->subdev
);
9366 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
9367 int used_disks
= imsm_num_data_members(map
);
9368 unsigned long long blocks_per_member
;
9369 unsigned long long num_data_stripes
;
9370 unsigned long long new_size_per_disk
;
9372 if (used_disks
== 0)
9375 /* calculate new size
9377 new_size_per_disk
= u
->new_size
/ used_disks
;
9378 blocks_per_member
= new_size_per_disk
+
9379 NUM_BLOCKS_DIRTY_STRIPE_REGION
;
9380 num_data_stripes
= new_size_per_disk
/
9381 map
->blocks_per_strip
;
9382 num_data_stripes
/= map
->num_domains
;
9383 dprintf("(size: %llu, blocks per member: %llu, num_data_stipes: %llu)\n",
9384 u
->new_size
, new_size_per_disk
,
9386 set_blocks_per_member(map
, blocks_per_member
);
9387 set_num_data_stripes(map
, num_data_stripes
);
9388 imsm_set_array_size(dev
, u
->new_size
);
9398 static int apply_update_activate_spare(struct imsm_update_activate_spare
*u
,
9399 struct intel_super
*super
,
9400 struct active_array
*active_array
)
9402 struct imsm_super
*mpb
= super
->anchor
;
9403 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->array
);
9404 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
9405 struct imsm_map
*migr_map
;
9406 struct active_array
*a
;
9407 struct imsm_disk
*disk
;
9414 int second_map_created
= 0;
9416 for (; u
; u
= u
->next
) {
9417 victim
= get_imsm_disk_idx(dev
, u
->slot
, MAP_X
);
9422 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
9427 pr_err("error: imsm_activate_spare passed an unknown disk (index: %d)\n",
9432 /* count failures (excluding rebuilds and the victim)
9433 * to determine map[0] state
9436 for (i
= 0; i
< map
->num_members
; i
++) {
9439 disk
= get_imsm_disk(super
,
9440 get_imsm_disk_idx(dev
, i
, MAP_X
));
9441 if (!disk
|| is_failed(disk
))
9445 /* adding a pristine spare, assign a new index */
9446 if (dl
->index
< 0) {
9447 dl
->index
= super
->anchor
->num_disks
;
9448 super
->anchor
->num_disks
++;
9451 disk
->status
|= CONFIGURED_DISK
;
9452 disk
->status
&= ~SPARE_DISK
;
9455 to_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
9456 if (!second_map_created
) {
9457 second_map_created
= 1;
9458 map
->map_state
= IMSM_T_STATE_DEGRADED
;
9459 migrate(dev
, super
, to_state
, MIGR_REBUILD
);
9461 map
->map_state
= to_state
;
9462 migr_map
= get_imsm_map(dev
, MAP_1
);
9463 set_imsm_ord_tbl_ent(map
, u
->slot
, dl
->index
);
9464 set_imsm_ord_tbl_ent(migr_map
, u
->slot
,
9465 dl
->index
| IMSM_ORD_REBUILD
);
9467 /* update the family_num to mark a new container
9468 * generation, being careful to record the existing
9469 * family_num in orig_family_num to clean up after
9470 * earlier mdadm versions that neglected to set it.
9472 if (mpb
->orig_family_num
== 0)
9473 mpb
->orig_family_num
= mpb
->family_num
;
9474 mpb
->family_num
+= super
->random
;
9476 /* count arrays using the victim in the metadata */
9478 for (a
= active_array
; a
; a
= a
->next
) {
9479 dev
= get_imsm_dev(super
, a
->info
.container_member
);
9480 map
= get_imsm_map(dev
, MAP_0
);
9482 if (get_imsm_disk_slot(map
, victim
) >= 0)
9486 /* delete the victim if it is no longer being
9492 /* We know that 'manager' isn't touching anything,
9493 * so it is safe to delete
9495 for (dlp
= &super
->disks
; *dlp
; dlp
= &(*dlp
)->next
)
9496 if ((*dlp
)->index
== victim
)
9499 /* victim may be on the missing list */
9501 for (dlp
= &super
->missing
; *dlp
;
9502 dlp
= &(*dlp
)->next
)
9503 if ((*dlp
)->index
== victim
)
9505 imsm_delete(super
, dlp
, victim
);
9512 static int apply_reshape_container_disks_update(struct imsm_update_reshape
*u
,
9513 struct intel_super
*super
,
9516 struct dl
*new_disk
;
9517 struct intel_dev
*id
;
9519 int delta_disks
= u
->new_raid_disks
- u
->old_raid_disks
;
9520 int disk_count
= u
->old_raid_disks
;
9521 void **tofree
= NULL
;
9522 int devices_to_reshape
= 1;
9523 struct imsm_super
*mpb
= super
->anchor
;
9525 unsigned int dev_id
;
9527 dprintf("(enter)\n");
9529 /* enable spares to use in array */
9530 for (i
= 0; i
< delta_disks
; i
++) {
9531 new_disk
= get_disk_super(super
,
9532 major(u
->new_disks
[i
]),
9533 minor(u
->new_disks
[i
]));
9534 dprintf("imsm: new disk for reshape is: %i:%i (%p, index = %i)\n",
9535 major(u
->new_disks
[i
]), minor(u
->new_disks
[i
]),
9536 new_disk
, new_disk
->index
);
9537 if (new_disk
== NULL
||
9538 (new_disk
->index
>= 0 &&
9539 new_disk
->index
< u
->old_raid_disks
))
9540 goto update_reshape_exit
;
9541 new_disk
->index
= disk_count
++;
9542 /* slot to fill in autolayout
9544 new_disk
->raiddisk
= new_disk
->index
;
9545 new_disk
->disk
.status
|=
9547 new_disk
->disk
.status
&= ~SPARE_DISK
;
9550 dprintf("imsm: volume set mpb->num_raid_devs = %i\n",
9551 mpb
->num_raid_devs
);
9552 /* manage changes in volume
9554 for (dev_id
= 0; dev_id
< mpb
->num_raid_devs
; dev_id
++) {
9555 void **sp
= *space_list
;
9556 struct imsm_dev
*newdev
;
9557 struct imsm_map
*newmap
, *oldmap
;
9559 for (id
= super
->devlist
; id
; id
= id
->next
) {
9560 if (id
->index
== dev_id
)
9569 /* Copy the dev, but not (all of) the map */
9570 memcpy(newdev
, id
->dev
, sizeof(*newdev
));
9571 oldmap
= get_imsm_map(id
->dev
, MAP_0
);
9572 newmap
= get_imsm_map(newdev
, MAP_0
);
9573 /* Copy the current map */
9574 memcpy(newmap
, oldmap
, sizeof_imsm_map(oldmap
));
9575 /* update one device only
9577 if (devices_to_reshape
) {
9578 dprintf("imsm: modifying subdev: %i\n",
9580 devices_to_reshape
--;
9581 newdev
->vol
.migr_state
= 1;
9582 newdev
->vol
.curr_migr_unit
= 0;
9583 set_migr_type(newdev
, MIGR_GEN_MIGR
);
9584 newmap
->num_members
= u
->new_raid_disks
;
9585 for (i
= 0; i
< delta_disks
; i
++) {
9586 set_imsm_ord_tbl_ent(newmap
,
9587 u
->old_raid_disks
+ i
,
9588 u
->old_raid_disks
+ i
);
9590 /* New map is correct, now need to save old map
9592 newmap
= get_imsm_map(newdev
, MAP_1
);
9593 memcpy(newmap
, oldmap
, sizeof_imsm_map(oldmap
));
9595 imsm_set_array_size(newdev
, -1);
9598 sp
= (void **)id
->dev
;
9603 /* Clear migration record */
9604 memset(super
->migr_rec
, 0, sizeof(struct migr_record
));
9607 *space_list
= tofree
;
9610 update_reshape_exit
:
9615 static int apply_takeover_update(struct imsm_update_takeover
*u
,
9616 struct intel_super
*super
,
9619 struct imsm_dev
*dev
= NULL
;
9620 struct intel_dev
*dv
;
9621 struct imsm_dev
*dev_new
;
9622 struct imsm_map
*map
;
9626 for (dv
= super
->devlist
; dv
; dv
= dv
->next
)
9627 if (dv
->index
== (unsigned int)u
->subarray
) {
9635 map
= get_imsm_map(dev
, MAP_0
);
9637 if (u
->direction
== R10_TO_R0
) {
9638 unsigned long long num_data_stripes
;
9640 /* Number of failed disks must be half of initial disk number */
9641 if (imsm_count_failed(super
, dev
, MAP_0
) !=
9642 (map
->num_members
/ 2))
9645 /* iterate through devices to mark removed disks as spare */
9646 for (dm
= super
->disks
; dm
; dm
= dm
->next
) {
9647 if (dm
->disk
.status
& FAILED_DISK
) {
9648 int idx
= dm
->index
;
9649 /* update indexes on the disk list */
9650 /* FIXME this loop-with-the-loop looks wrong, I'm not convinced
9651 the index values will end up being correct.... NB */
9652 for (du
= super
->disks
; du
; du
= du
->next
)
9653 if (du
->index
> idx
)
9655 /* mark as spare disk */
9660 map
->num_members
= map
->num_members
/ 2;
9661 map
->map_state
= IMSM_T_STATE_NORMAL
;
9662 map
->num_domains
= 1;
9663 map
->raid_level
= 0;
9664 map
->failed_disk_num
= -1;
9665 num_data_stripes
= imsm_dev_size(dev
) / 2;
9666 num_data_stripes
/= map
->blocks_per_strip
;
9667 set_num_data_stripes(map
, num_data_stripes
);
9670 if (u
->direction
== R0_TO_R10
) {
9672 unsigned long long num_data_stripes
;
9674 /* update slots in current disk list */
9675 for (dm
= super
->disks
; dm
; dm
= dm
->next
) {
9679 /* create new *missing* disks */
9680 for (i
= 0; i
< map
->num_members
; i
++) {
9681 space
= *space_list
;
9684 *space_list
= *space
;
9686 memcpy(du
, super
->disks
, sizeof(*du
));
9690 du
->index
= (i
* 2) + 1;
9691 sprintf((char *)du
->disk
.serial
,
9692 " MISSING_%d", du
->index
);
9693 sprintf((char *)du
->serial
,
9694 "MISSING_%d", du
->index
);
9695 du
->next
= super
->missing
;
9696 super
->missing
= du
;
9698 /* create new dev and map */
9699 space
= *space_list
;
9702 *space_list
= *space
;
9703 dev_new
= (void *)space
;
9704 memcpy(dev_new
, dev
, sizeof(*dev
));
9705 /* update new map */
9706 map
= get_imsm_map(dev_new
, MAP_0
);
9707 map
->num_members
= map
->num_members
* 2;
9708 map
->map_state
= IMSM_T_STATE_DEGRADED
;
9709 map
->num_domains
= 2;
9710 map
->raid_level
= 1;
9711 num_data_stripes
= imsm_dev_size(dev
) / 2;
9712 num_data_stripes
/= map
->blocks_per_strip
;
9713 num_data_stripes
/= map
->num_domains
;
9714 set_num_data_stripes(map
, num_data_stripes
);
9716 /* replace dev<->dev_new */
9719 /* update disk order table */
9720 for (du
= super
->disks
; du
; du
= du
->next
)
9722 set_imsm_ord_tbl_ent(map
, du
->index
, du
->index
);
9723 for (du
= super
->missing
; du
; du
= du
->next
)
9724 if (du
->index
>= 0) {
9725 set_imsm_ord_tbl_ent(map
, du
->index
, du
->index
);
9726 mark_missing(super
, dv
->dev
, &du
->disk
, du
->index
);
9732 static void imsm_process_update(struct supertype
*st
,
9733 struct metadata_update
*update
)
9736 * crack open the metadata_update envelope to find the update record
9737 * update can be one of:
9738 * update_reshape_container_disks - all the arrays in the container
9739 * are being reshaped to have more devices. We need to mark
9740 * the arrays for general migration and convert selected spares
9741 * into active devices.
9742 * update_activate_spare - a spare device has replaced a failed
9743 * device in an array, update the disk_ord_tbl. If this disk is
9744 * present in all member arrays then also clear the SPARE_DISK
9746 * update_create_array
9748 * update_rename_array
9749 * update_add_remove_disk
9751 struct intel_super
*super
= st
->sb
;
9752 struct imsm_super
*mpb
;
9753 enum imsm_update_type type
= *(enum imsm_update_type
*) update
->buf
;
9755 /* update requires a larger buf but the allocation failed */
9756 if (super
->next_len
&& !super
->next_buf
) {
9757 super
->next_len
= 0;
9761 if (super
->next_buf
) {
9762 memcpy(super
->next_buf
, super
->buf
, super
->len
);
9764 super
->len
= super
->next_len
;
9765 super
->buf
= super
->next_buf
;
9767 super
->next_len
= 0;
9768 super
->next_buf
= NULL
;
9771 mpb
= super
->anchor
;
9774 case update_general_migration_checkpoint
: {
9775 struct intel_dev
*id
;
9776 struct imsm_update_general_migration_checkpoint
*u
=
9777 (void *)update
->buf
;
9779 dprintf("called for update_general_migration_checkpoint\n");
9781 /* find device under general migration */
9782 for (id
= super
->devlist
; id
; id
= id
->next
) {
9783 if (is_gen_migration(id
->dev
)) {
9784 id
->dev
->vol
.curr_migr_unit
=
9785 __cpu_to_le32(u
->curr_migr_unit
);
9786 super
->updates_pending
++;
9791 case update_takeover
: {
9792 struct imsm_update_takeover
*u
= (void *)update
->buf
;
9793 if (apply_takeover_update(u
, super
, &update
->space_list
)) {
9794 imsm_update_version_info(super
);
9795 super
->updates_pending
++;
9800 case update_reshape_container_disks
: {
9801 struct imsm_update_reshape
*u
= (void *)update
->buf
;
9802 if (apply_reshape_container_disks_update(
9803 u
, super
, &update
->space_list
))
9804 super
->updates_pending
++;
9807 case update_reshape_migration
: {
9808 struct imsm_update_reshape_migration
*u
= (void *)update
->buf
;
9809 if (apply_reshape_migration_update(
9810 u
, super
, &update
->space_list
))
9811 super
->updates_pending
++;
9814 case update_size_change
: {
9815 struct imsm_update_size_change
*u
= (void *)update
->buf
;
9816 if (apply_size_change_update(u
, super
))
9817 super
->updates_pending
++;
9820 case update_activate_spare
: {
9821 struct imsm_update_activate_spare
*u
= (void *) update
->buf
;
9822 if (apply_update_activate_spare(u
, super
, st
->arrays
))
9823 super
->updates_pending
++;
9826 case update_create_array
: {
9827 /* someone wants to create a new array, we need to be aware of
9828 * a few races/collisions:
9829 * 1/ 'Create' called by two separate instances of mdadm
9830 * 2/ 'Create' versus 'activate_spare': mdadm has chosen
9831 * devices that have since been assimilated via
9833 * In the event this update can not be carried out mdadm will
9834 * (FIX ME) notice that its update did not take hold.
9836 struct imsm_update_create_array
*u
= (void *) update
->buf
;
9837 struct intel_dev
*dv
;
9838 struct imsm_dev
*dev
;
9839 struct imsm_map
*map
, *new_map
;
9840 unsigned long long start
, end
;
9841 unsigned long long new_start
, new_end
;
9843 struct disk_info
*inf
;
9846 /* handle racing creates: first come first serve */
9847 if (u
->dev_idx
< mpb
->num_raid_devs
) {
9848 dprintf("subarray %d already defined\n", u
->dev_idx
);
9852 /* check update is next in sequence */
9853 if (u
->dev_idx
!= mpb
->num_raid_devs
) {
9854 dprintf("can not create array %d expected index %d\n",
9855 u
->dev_idx
, mpb
->num_raid_devs
);
9859 new_map
= get_imsm_map(&u
->dev
, MAP_0
);
9860 new_start
= pba_of_lba0(new_map
);
9861 new_end
= new_start
+ per_dev_array_size(new_map
);
9862 inf
= get_disk_info(u
);
9864 /* handle activate_spare versus create race:
9865 * check to make sure that overlapping arrays do not include
9868 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
9869 dev
= get_imsm_dev(super
, i
);
9870 map
= get_imsm_map(dev
, MAP_0
);
9871 start
= pba_of_lba0(map
);
9872 end
= start
+ per_dev_array_size(map
);
9873 if ((new_start
>= start
&& new_start
<= end
) ||
9874 (start
>= new_start
&& start
<= new_end
))
9879 if (disks_overlap(super
, i
, u
)) {
9880 dprintf("arrays overlap\n");
9885 /* check that prepare update was successful */
9886 if (!update
->space
) {
9887 dprintf("prepare update failed\n");
9891 /* check that all disks are still active before committing
9892 * changes. FIXME: could we instead handle this by creating a
9893 * degraded array? That's probably not what the user expects,
9894 * so better to drop this update on the floor.
9896 for (i
= 0; i
< new_map
->num_members
; i
++) {
9897 dl
= serial_to_dl(inf
[i
].serial
, super
);
9899 dprintf("disk disappeared\n");
9904 super
->updates_pending
++;
9906 /* convert spares to members and fixup ord_tbl */
9907 for (i
= 0; i
< new_map
->num_members
; i
++) {
9908 dl
= serial_to_dl(inf
[i
].serial
, super
);
9909 if (dl
->index
== -1) {
9910 dl
->index
= mpb
->num_disks
;
9912 dl
->disk
.status
|= CONFIGURED_DISK
;
9913 dl
->disk
.status
&= ~SPARE_DISK
;
9915 set_imsm_ord_tbl_ent(new_map
, i
, dl
->index
);
9920 update
->space
= NULL
;
9921 imsm_copy_dev(dev
, &u
->dev
);
9922 dv
->index
= u
->dev_idx
;
9923 dv
->next
= super
->devlist
;
9924 super
->devlist
= dv
;
9925 mpb
->num_raid_devs
++;
9927 imsm_update_version_info(super
);
9930 /* mdmon knows how to release update->space, but not
9931 * ((struct intel_dev *) update->space)->dev
9933 if (update
->space
) {
9939 case update_kill_array
: {
9940 struct imsm_update_kill_array
*u
= (void *) update
->buf
;
9941 int victim
= u
->dev_idx
;
9942 struct active_array
*a
;
9943 struct intel_dev
**dp
;
9944 struct imsm_dev
*dev
;
9946 /* sanity check that we are not affecting the uuid of
9947 * active arrays, or deleting an active array
9949 * FIXME when immutable ids are available, but note that
9950 * we'll also need to fixup the invalidated/active
9951 * subarray indexes in mdstat
9953 for (a
= st
->arrays
; a
; a
= a
->next
)
9954 if (a
->info
.container_member
>= victim
)
9956 /* by definition if mdmon is running at least one array
9957 * is active in the container, so checking
9958 * mpb->num_raid_devs is just extra paranoia
9960 dev
= get_imsm_dev(super
, victim
);
9961 if (a
|| !dev
|| mpb
->num_raid_devs
== 1) {
9962 dprintf("failed to delete subarray-%d\n", victim
);
9966 for (dp
= &super
->devlist
; *dp
;)
9967 if ((*dp
)->index
== (unsigned)super
->current_vol
) {
9970 if ((*dp
)->index
> (unsigned)victim
)
9974 mpb
->num_raid_devs
--;
9975 super
->updates_pending
++;
9978 case update_rename_array
: {
9979 struct imsm_update_rename_array
*u
= (void *) update
->buf
;
9980 char name
[MAX_RAID_SERIAL_LEN
+1];
9981 int target
= u
->dev_idx
;
9982 struct active_array
*a
;
9983 struct imsm_dev
*dev
;
9985 /* sanity check that we are not affecting the uuid of
9988 memset(name
, 0, sizeof(name
));
9989 snprintf(name
, MAX_RAID_SERIAL_LEN
, "%s", (char *) u
->name
);
9990 name
[MAX_RAID_SERIAL_LEN
] = '\0';
9991 for (a
= st
->arrays
; a
; a
= a
->next
)
9992 if (a
->info
.container_member
== target
)
9994 dev
= get_imsm_dev(super
, u
->dev_idx
);
9995 if (a
|| !dev
|| !check_name(super
, name
, 1)) {
9996 dprintf("failed to rename subarray-%d\n", target
);
10000 memcpy(dev
->volume
, name
, MAX_RAID_SERIAL_LEN
);
10001 super
->updates_pending
++;
10004 case update_add_remove_disk
: {
10005 /* we may be able to repair some arrays if disks are
10006 * being added, check the status of add_remove_disk
10007 * if discs has been added.
10009 if (add_remove_disk_update(super
)) {
10010 struct active_array
*a
;
10012 super
->updates_pending
++;
10013 for (a
= st
->arrays
; a
; a
= a
->next
)
10014 a
->check_degraded
= 1;
10018 case update_prealloc_badblocks_mem
:
10020 case update_rwh_policy
: {
10021 struct imsm_update_rwh_policy
*u
= (void *)update
->buf
;
10022 int target
= u
->dev_idx
;
10023 struct imsm_dev
*dev
= get_imsm_dev(super
, target
);
10025 dprintf("could not find subarray-%d\n", target
);
10029 if (dev
->rwh_policy
!= u
->new_policy
) {
10030 dev
->rwh_policy
= u
->new_policy
;
10031 super
->updates_pending
++;
10036 pr_err("error: unsupported process update type:(type: %d)\n", type
);
10040 static struct mdinfo
*get_spares_for_grow(struct supertype
*st
);
10042 static int imsm_prepare_update(struct supertype
*st
,
10043 struct metadata_update
*update
)
10046 * Allocate space to hold new disk entries, raid-device entries or a new
10047 * mpb if necessary. The manager synchronously waits for updates to
10048 * complete in the monitor, so new mpb buffers allocated here can be
10049 * integrated by the monitor thread without worrying about live pointers
10050 * in the manager thread.
10052 enum imsm_update_type type
;
10053 struct intel_super
*super
= st
->sb
;
10054 unsigned int sector_size
= super
->sector_size
;
10055 struct imsm_super
*mpb
= super
->anchor
;
10059 if (update
->len
< (int)sizeof(type
))
10062 type
= *(enum imsm_update_type
*) update
->buf
;
10065 case update_general_migration_checkpoint
:
10066 if (update
->len
< (int)sizeof(struct imsm_update_general_migration_checkpoint
))
10068 dprintf("called for update_general_migration_checkpoint\n");
10070 case update_takeover
: {
10071 struct imsm_update_takeover
*u
= (void *)update
->buf
;
10072 if (update
->len
< (int)sizeof(*u
))
10074 if (u
->direction
== R0_TO_R10
) {
10075 void **tail
= (void **)&update
->space_list
;
10076 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->subarray
);
10077 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
10078 int num_members
= map
->num_members
;
10081 /* allocate memory for added disks */
10082 for (i
= 0; i
< num_members
; i
++) {
10083 size
= sizeof(struct dl
);
10084 space
= xmalloc(size
);
10089 /* allocate memory for new device */
10090 size
= sizeof_imsm_dev(super
->devlist
->dev
, 0) +
10091 (num_members
* sizeof(__u32
));
10092 space
= xmalloc(size
);
10096 len
= disks_to_mpb_size(num_members
* 2);
10101 case update_reshape_container_disks
: {
10102 /* Every raid device in the container is about to
10103 * gain some more devices, and we will enter a
10105 * So each 'imsm_map' will be bigger, and the imsm_vol
10106 * will now hold 2 of them.
10107 * Thus we need new 'struct imsm_dev' allocations sized
10108 * as sizeof_imsm_dev but with more devices in both maps.
10110 struct imsm_update_reshape
*u
= (void *)update
->buf
;
10111 struct intel_dev
*dl
;
10112 void **space_tail
= (void**)&update
->space_list
;
10114 if (update
->len
< (int)sizeof(*u
))
10117 dprintf("for update_reshape\n");
10119 for (dl
= super
->devlist
; dl
; dl
= dl
->next
) {
10120 int size
= sizeof_imsm_dev(dl
->dev
, 1);
10122 if (u
->new_raid_disks
> u
->old_raid_disks
)
10123 size
+= sizeof(__u32
)*2*
10124 (u
->new_raid_disks
- u
->old_raid_disks
);
10128 *space_tail
= NULL
;
10131 len
= disks_to_mpb_size(u
->new_raid_disks
);
10132 dprintf("New anchor length is %llu\n", (unsigned long long)len
);
10135 case update_reshape_migration
: {
10136 /* for migration level 0->5 we need to add disks
10137 * so the same as for container operation we will copy
10138 * device to the bigger location.
10139 * in memory prepared device and new disk area are prepared
10140 * for usage in process update
10142 struct imsm_update_reshape_migration
*u
= (void *)update
->buf
;
10143 struct intel_dev
*id
;
10144 void **space_tail
= (void **)&update
->space_list
;
10147 int current_level
= -1;
10149 if (update
->len
< (int)sizeof(*u
))
10152 dprintf("for update_reshape\n");
10154 /* add space for bigger array in update
10156 for (id
= super
->devlist
; id
; id
= id
->next
) {
10157 if (id
->index
== (unsigned)u
->subdev
) {
10158 size
= sizeof_imsm_dev(id
->dev
, 1);
10159 if (u
->new_raid_disks
> u
->old_raid_disks
)
10160 size
+= sizeof(__u32
)*2*
10161 (u
->new_raid_disks
- u
->old_raid_disks
);
10165 *space_tail
= NULL
;
10169 if (update
->space_list
== NULL
)
10172 /* add space for disk in update
10174 size
= sizeof(struct dl
);
10178 *space_tail
= NULL
;
10180 /* add spare device to update
10182 for (id
= super
->devlist
; id
; id
= id
->next
)
10183 if (id
->index
== (unsigned)u
->subdev
) {
10184 struct imsm_dev
*dev
;
10185 struct imsm_map
*map
;
10187 dev
= get_imsm_dev(super
, u
->subdev
);
10188 map
= get_imsm_map(dev
, MAP_0
);
10189 current_level
= map
->raid_level
;
10192 if (u
->new_level
== 5 && u
->new_level
!= current_level
) {
10193 struct mdinfo
*spares
;
10195 spares
= get_spares_for_grow(st
);
10198 struct mdinfo
*dev
;
10200 dev
= spares
->devs
;
10203 makedev(dev
->disk
.major
,
10205 dl
= get_disk_super(super
,
10208 dl
->index
= u
->old_raid_disks
;
10211 sysfs_free(spares
);
10214 len
= disks_to_mpb_size(u
->new_raid_disks
);
10215 dprintf("New anchor length is %llu\n", (unsigned long long)len
);
10218 case update_size_change
: {
10219 if (update
->len
< (int)sizeof(struct imsm_update_size_change
))
10223 case update_activate_spare
: {
10224 if (update
->len
< (int)sizeof(struct imsm_update_activate_spare
))
10228 case update_create_array
: {
10229 struct imsm_update_create_array
*u
= (void *) update
->buf
;
10230 struct intel_dev
*dv
;
10231 struct imsm_dev
*dev
= &u
->dev
;
10232 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
10234 struct disk_info
*inf
;
10238 if (update
->len
< (int)sizeof(*u
))
10241 inf
= get_disk_info(u
);
10242 len
= sizeof_imsm_dev(dev
, 1);
10243 /* allocate a new super->devlist entry */
10244 dv
= xmalloc(sizeof(*dv
));
10245 dv
->dev
= xmalloc(len
);
10246 update
->space
= dv
;
10248 /* count how many spares will be converted to members */
10249 for (i
= 0; i
< map
->num_members
; i
++) {
10250 dl
= serial_to_dl(inf
[i
].serial
, super
);
10252 /* hmm maybe it failed?, nothing we can do about
10257 if (count_memberships(dl
, super
) == 0)
10260 len
+= activate
* sizeof(struct imsm_disk
);
10263 case update_kill_array
: {
10264 if (update
->len
< (int)sizeof(struct imsm_update_kill_array
))
10268 case update_rename_array
: {
10269 if (update
->len
< (int)sizeof(struct imsm_update_rename_array
))
10273 case update_add_remove_disk
:
10274 /* no update->len needed */
10276 case update_prealloc_badblocks_mem
:
10277 super
->extra_space
+= sizeof(struct bbm_log
) -
10278 get_imsm_bbm_log_size(super
->bbm_log
);
10280 case update_rwh_policy
: {
10281 if (update
->len
< (int)sizeof(struct imsm_update_rwh_policy
))
10289 /* check if we need a larger metadata buffer */
10290 if (super
->next_buf
)
10291 buf_len
= super
->next_len
;
10293 buf_len
= super
->len
;
10295 if (__le32_to_cpu(mpb
->mpb_size
) + super
->extra_space
+ len
> buf_len
) {
10296 /* ok we need a larger buf than what is currently allocated
10297 * if this allocation fails process_update will notice that
10298 * ->next_len is set and ->next_buf is NULL
10300 buf_len
= ROUND_UP(__le32_to_cpu(mpb
->mpb_size
) +
10301 super
->extra_space
+ len
, sector_size
);
10302 if (super
->next_buf
)
10303 free(super
->next_buf
);
10305 super
->next_len
= buf_len
;
10306 if (posix_memalign(&super
->next_buf
, sector_size
, buf_len
) == 0)
10307 memset(super
->next_buf
, 0, buf_len
);
10309 super
->next_buf
= NULL
;
10314 /* must be called while manager is quiesced */
10315 static void imsm_delete(struct intel_super
*super
, struct dl
**dlp
, unsigned index
)
10317 struct imsm_super
*mpb
= super
->anchor
;
10319 struct imsm_dev
*dev
;
10320 struct imsm_map
*map
;
10321 unsigned int i
, j
, num_members
;
10322 __u32 ord
, ord_map0
;
10323 struct bbm_log
*log
= super
->bbm_log
;
10325 dprintf("deleting device[%d] from imsm_super\n", index
);
10327 /* shift all indexes down one */
10328 for (iter
= super
->disks
; iter
; iter
= iter
->next
)
10329 if (iter
->index
> (int)index
)
10331 for (iter
= super
->missing
; iter
; iter
= iter
->next
)
10332 if (iter
->index
> (int)index
)
10335 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
10336 dev
= get_imsm_dev(super
, i
);
10337 map
= get_imsm_map(dev
, MAP_0
);
10338 num_members
= map
->num_members
;
10339 for (j
= 0; j
< num_members
; j
++) {
10340 /* update ord entries being careful not to propagate
10341 * ord-flags to the first map
10343 ord
= get_imsm_ord_tbl_ent(dev
, j
, MAP_X
);
10344 ord_map0
= get_imsm_ord_tbl_ent(dev
, j
, MAP_0
);
10346 if (ord_to_idx(ord
) <= index
)
10349 map
= get_imsm_map(dev
, MAP_0
);
10350 set_imsm_ord_tbl_ent(map
, j
, ord_map0
- 1);
10351 map
= get_imsm_map(dev
, MAP_1
);
10353 set_imsm_ord_tbl_ent(map
, j
, ord
- 1);
10357 for (i
= 0; i
< log
->entry_count
; i
++) {
10358 struct bbm_log_entry
*entry
= &log
->marked_block_entries
[i
];
10360 if (entry
->disk_ordinal
<= index
)
10362 entry
->disk_ordinal
--;
10366 super
->updates_pending
++;
10368 struct dl
*dl
= *dlp
;
10370 *dlp
= (*dlp
)->next
;
10371 __free_imsm_disk(dl
);
10375 static void close_targets(int *targets
, int new_disks
)
10382 for (i
= 0; i
< new_disks
; i
++) {
10383 if (targets
[i
] >= 0) {
10390 static int imsm_get_allowed_degradation(int level
, int raid_disks
,
10391 struct intel_super
*super
,
10392 struct imsm_dev
*dev
)
10398 struct imsm_map
*map
;
10401 ret_val
= raid_disks
/2;
10402 /* check map if all disks pairs not failed
10405 map
= get_imsm_map(dev
, MAP_0
);
10406 for (i
= 0; i
< ret_val
; i
++) {
10407 int degradation
= 0;
10408 if (get_imsm_disk(super
, i
) == NULL
)
10410 if (get_imsm_disk(super
, i
+ 1) == NULL
)
10412 if (degradation
== 2)
10415 map
= get_imsm_map(dev
, MAP_1
);
10416 /* if there is no second map
10417 * result can be returned
10421 /* check degradation in second map
10423 for (i
= 0; i
< ret_val
; i
++) {
10424 int degradation
= 0;
10425 if (get_imsm_disk(super
, i
) == NULL
)
10427 if (get_imsm_disk(super
, i
+ 1) == NULL
)
10429 if (degradation
== 2)
10443 /*******************************************************************************
10444 * Function: open_backup_targets
10445 * Description: Function opens file descriptors for all devices given in
10448 * info : general array info
10449 * raid_disks : number of disks
10450 * raid_fds : table of device's file descriptors
10451 * super : intel super for raid10 degradation check
10452 * dev : intel device for raid10 degradation check
10456 ******************************************************************************/
10457 int open_backup_targets(struct mdinfo
*info
, int raid_disks
, int *raid_fds
,
10458 struct intel_super
*super
, struct imsm_dev
*dev
)
10464 for (i
= 0; i
< raid_disks
; i
++)
10467 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
10470 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
)) {
10471 dprintf("disk is faulty!!\n");
10475 if (sd
->disk
.raid_disk
>= raid_disks
|| sd
->disk
.raid_disk
< 0)
10478 dn
= map_dev(sd
->disk
.major
,
10479 sd
->disk
.minor
, 1);
10480 raid_fds
[sd
->disk
.raid_disk
] = dev_open(dn
, O_RDWR
);
10481 if (raid_fds
[sd
->disk
.raid_disk
] < 0) {
10482 pr_err("cannot open component\n");
10487 /* check if maximum array degradation level is not exceeded
10489 if ((raid_disks
- opened
) >
10490 imsm_get_allowed_degradation(info
->new_level
, raid_disks
,
10492 pr_err("Not enough disks can be opened.\n");
10493 close_targets(raid_fds
, raid_disks
);
10499 /*******************************************************************************
10500 * Function: validate_container_imsm
10501 * Description: This routine validates container after assemble,
10502 * eg. if devices in container are under the same controller.
10505 * info : linked list with info about devices used in array
10509 ******************************************************************************/
10510 int validate_container_imsm(struct mdinfo
*info
)
10512 if (check_env("IMSM_NO_PLATFORM"))
10515 struct sys_dev
*idev
;
10516 struct sys_dev
*hba
= NULL
;
10517 struct sys_dev
*intel_devices
= find_intel_devices();
10518 char *dev_path
= devt_to_devpath(makedev(info
->disk
.major
,
10519 info
->disk
.minor
));
10521 for (idev
= intel_devices
; idev
; idev
= idev
->next
) {
10522 if (dev_path
&& strstr(dev_path
, idev
->path
)) {
10531 pr_err("WARNING - Cannot detect HBA for device %s!\n",
10532 devid2kname(makedev(info
->disk
.major
, info
->disk
.minor
)));
10536 const struct imsm_orom
*orom
= get_orom_by_device_id(hba
->dev_id
);
10537 struct mdinfo
*dev
;
10539 for (dev
= info
->next
; dev
; dev
= dev
->next
) {
10540 dev_path
= devt_to_devpath(makedev(dev
->disk
.major
, dev
->disk
.minor
));
10542 struct sys_dev
*hba2
= NULL
;
10543 for (idev
= intel_devices
; idev
; idev
= idev
->next
) {
10544 if (dev_path
&& strstr(dev_path
, idev
->path
)) {
10552 const struct imsm_orom
*orom2
= hba2
== NULL
? NULL
:
10553 get_orom_by_device_id(hba2
->dev_id
);
10555 if (hba2
&& hba
->type
!= hba2
->type
) {
10556 pr_err("WARNING - HBAs of devices do not match %s != %s\n",
10557 get_sys_dev_type(hba
->type
), get_sys_dev_type(hba2
->type
));
10561 if (orom
!= orom2
) {
10562 pr_err("WARNING - IMSM container assembled with disks under different HBAs!\n"
10563 " This operation is not supported and can lead to data loss.\n");
10568 pr_err("WARNING - IMSM container assembled with disks under HBAs without IMSM platform support!\n"
10569 " This operation is not supported and can lead to data loss.\n");
10577 /*******************************************************************************
10578 * Function: imsm_record_badblock
10579 * Description: This routine stores new bad block record in BBM log
10582 * a : array containing a bad block
10583 * slot : disk number containing a bad block
10584 * sector : bad block sector
10585 * length : bad block sectors range
10589 ******************************************************************************/
10590 static int imsm_record_badblock(struct active_array
*a
, int slot
,
10591 unsigned long long sector
, int length
)
10593 struct intel_super
*super
= a
->container
->sb
;
10597 ord
= imsm_disk_slot_to_ord(a
, slot
);
10601 ret
= record_new_badblock(super
->bbm_log
, ord_to_idx(ord
), sector
,
10604 super
->updates_pending
++;
10608 /*******************************************************************************
10609 * Function: imsm_clear_badblock
10610 * Description: This routine clears bad block record from BBM log
10613 * a : array containing a bad block
10614 * slot : disk number containing a bad block
10615 * sector : bad block sector
10616 * length : bad block sectors range
10620 ******************************************************************************/
10621 static int imsm_clear_badblock(struct active_array
*a
, int slot
,
10622 unsigned long long sector
, int length
)
10624 struct intel_super
*super
= a
->container
->sb
;
10628 ord
= imsm_disk_slot_to_ord(a
, slot
);
10632 ret
= clear_badblock(super
->bbm_log
, ord_to_idx(ord
), sector
, length
);
10634 super
->updates_pending
++;
10638 /*******************************************************************************
10639 * Function: imsm_get_badblocks
10640 * Description: This routine get list of bad blocks for an array
10644 * slot : disk number
10646 * bb : structure containing bad blocks
10648 ******************************************************************************/
10649 static struct md_bb
*imsm_get_badblocks(struct active_array
*a
, int slot
)
10651 int inst
= a
->info
.container_member
;
10652 struct intel_super
*super
= a
->container
->sb
;
10653 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
10654 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
10657 ord
= imsm_disk_slot_to_ord(a
, slot
);
10661 get_volume_badblocks(super
->bbm_log
, ord_to_idx(ord
), pba_of_lba0(map
),
10662 per_dev_array_size(map
), &super
->bb
);
10666 /*******************************************************************************
10667 * Function: examine_badblocks_imsm
10668 * Description: Prints list of bad blocks on a disk to the standard output
10671 * st : metadata handler
10672 * fd : open file descriptor for device
10673 * devname : device name
10677 ******************************************************************************/
10678 static int examine_badblocks_imsm(struct supertype
*st
, int fd
, char *devname
)
10680 struct intel_super
*super
= st
->sb
;
10681 struct bbm_log
*log
= super
->bbm_log
;
10682 struct dl
*d
= NULL
;
10685 for (d
= super
->disks
; d
; d
= d
->next
) {
10686 if (strcmp(d
->devname
, devname
) == 0)
10690 if ((d
== NULL
) || (d
->index
< 0)) { /* serial mismatch probably */
10691 pr_err("%s doesn't appear to be part of a raid array\n",
10698 struct bbm_log_entry
*entry
= &log
->marked_block_entries
[0];
10700 for (i
= 0; i
< log
->entry_count
; i
++) {
10701 if (entry
[i
].disk_ordinal
== d
->index
) {
10702 unsigned long long sector
= __le48_to_cpu(
10703 &entry
[i
].defective_block_start
);
10704 int cnt
= entry
[i
].marked_count
+ 1;
10707 printf("Bad-blocks on %s:\n", devname
);
10711 printf("%20llu for %d sectors\n", sector
, cnt
);
10717 printf("No bad-blocks list configured on %s\n", devname
);
10721 /*******************************************************************************
10722 * Function: init_migr_record_imsm
10723 * Description: Function inits imsm migration record
10725 * super : imsm internal array info
10726 * dev : device under migration
10727 * info : general array info to find the smallest device
10730 ******************************************************************************/
10731 void init_migr_record_imsm(struct supertype
*st
, struct imsm_dev
*dev
,
10732 struct mdinfo
*info
)
10734 struct intel_super
*super
= st
->sb
;
10735 struct migr_record
*migr_rec
= super
->migr_rec
;
10736 int new_data_disks
;
10737 unsigned long long dsize
, dev_sectors
;
10738 long long unsigned min_dev_sectors
= -1LLU;
10742 struct imsm_map
*map_dest
= get_imsm_map(dev
, MAP_0
);
10743 struct imsm_map
*map_src
= get_imsm_map(dev
, MAP_1
);
10744 unsigned long long num_migr_units
;
10745 unsigned long long array_blocks
;
10747 memset(migr_rec
, 0, sizeof(struct migr_record
));
10748 migr_rec
->family_num
= __cpu_to_le32(super
->anchor
->family_num
);
10750 /* only ascending reshape supported now */
10751 migr_rec
->ascending_migr
= __cpu_to_le32(1);
10753 migr_rec
->dest_depth_per_unit
= GEN_MIGR_AREA_SIZE
/
10754 max(map_dest
->blocks_per_strip
, map_src
->blocks_per_strip
);
10755 migr_rec
->dest_depth_per_unit
*=
10756 max(map_dest
->blocks_per_strip
, map_src
->blocks_per_strip
);
10757 new_data_disks
= imsm_num_data_members(map_dest
);
10758 migr_rec
->blocks_per_unit
=
10759 __cpu_to_le32(migr_rec
->dest_depth_per_unit
* new_data_disks
);
10760 migr_rec
->dest_depth_per_unit
=
10761 __cpu_to_le32(migr_rec
->dest_depth_per_unit
);
10762 array_blocks
= info
->component_size
* new_data_disks
;
10764 array_blocks
/ __le32_to_cpu(migr_rec
->blocks_per_unit
);
10766 if (array_blocks
% __le32_to_cpu(migr_rec
->blocks_per_unit
))
10768 set_num_migr_units(migr_rec
, num_migr_units
);
10770 migr_rec
->post_migr_vol_cap
= dev
->size_low
;
10771 migr_rec
->post_migr_vol_cap_hi
= dev
->size_high
;
10773 /* Find the smallest dev */
10774 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
10775 sprintf(nm
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
10776 fd
= dev_open(nm
, O_RDONLY
);
10779 get_dev_size(fd
, NULL
, &dsize
);
10780 dev_sectors
= dsize
/ 512;
10781 if (dev_sectors
< min_dev_sectors
)
10782 min_dev_sectors
= dev_sectors
;
10785 set_migr_chkp_area_pba(migr_rec
, min_dev_sectors
-
10786 RAID_DISK_RESERVED_BLOCKS_IMSM_HI
);
10788 write_imsm_migr_rec(st
);
10793 /*******************************************************************************
10794 * Function: save_backup_imsm
10795 * Description: Function saves critical data stripes to Migration Copy Area
10796 * and updates the current migration unit status.
10797 * Use restore_stripes() to form a destination stripe,
10798 * and to write it to the Copy Area.
10800 * st : supertype information
10801 * dev : imsm device that backup is saved for
10802 * info : general array info
10803 * buf : input buffer
10804 * length : length of data to backup (blocks_per_unit)
10808 ******************************************************************************/
10809 int save_backup_imsm(struct supertype
*st
,
10810 struct imsm_dev
*dev
,
10811 struct mdinfo
*info
,
10816 struct intel_super
*super
= st
->sb
;
10817 unsigned long long *target_offsets
;
10820 struct imsm_map
*map_dest
= get_imsm_map(dev
, MAP_0
);
10821 int new_disks
= map_dest
->num_members
;
10822 int dest_layout
= 0;
10824 unsigned long long start
;
10825 int data_disks
= imsm_num_data_members(map_dest
);
10827 targets
= xmalloc(new_disks
* sizeof(int));
10829 for (i
= 0; i
< new_disks
; i
++)
10832 target_offsets
= xcalloc(new_disks
, sizeof(unsigned long long));
10834 start
= info
->reshape_progress
* 512;
10835 for (i
= 0; i
< new_disks
; i
++) {
10836 target_offsets
[i
] = migr_chkp_area_pba(super
->migr_rec
) * 512;
10837 /* move back copy area adderss, it will be moved forward
10838 * in restore_stripes() using start input variable
10840 target_offsets
[i
] -= start
/data_disks
;
10843 if (open_backup_targets(info
, new_disks
, targets
,
10847 dest_layout
= imsm_level_to_layout(map_dest
->raid_level
);
10848 dest_chunk
= __le16_to_cpu(map_dest
->blocks_per_strip
) * 512;
10850 if (restore_stripes(targets
, /* list of dest devices */
10851 target_offsets
, /* migration record offsets */
10854 map_dest
->raid_level
,
10856 -1, /* source backup file descriptor */
10857 0, /* input buf offset
10858 * always 0 buf is already offseted */
10862 pr_err("Error restoring stripes\n");
10870 close_targets(targets
, new_disks
);
10873 free(target_offsets
);
10878 /*******************************************************************************
10879 * Function: save_checkpoint_imsm
10880 * Description: Function called for current unit status update
10881 * in the migration record. It writes it to disk.
10883 * super : imsm internal array info
10884 * info : general array info
10888 * 2: failure, means no valid migration record
10889 * / no general migration in progress /
10890 ******************************************************************************/
10891 int save_checkpoint_imsm(struct supertype
*st
, struct mdinfo
*info
, int state
)
10893 struct intel_super
*super
= st
->sb
;
10894 unsigned long long blocks_per_unit
;
10895 unsigned long long curr_migr_unit
;
10897 if (load_imsm_migr_rec(super
, info
) != 0) {
10898 dprintf("imsm: ERROR: Cannot read migration record for checkpoint save.\n");
10902 blocks_per_unit
= __le32_to_cpu(super
->migr_rec
->blocks_per_unit
);
10903 if (blocks_per_unit
== 0) {
10904 dprintf("imsm: no migration in progress.\n");
10907 curr_migr_unit
= info
->reshape_progress
/ blocks_per_unit
;
10908 /* check if array is alligned to copy area
10909 * if it is not alligned, add one to current migration unit value
10910 * this can happend on array reshape finish only
10912 if (info
->reshape_progress
% blocks_per_unit
)
10915 set_current_migr_unit(super
->migr_rec
, curr_migr_unit
);
10916 super
->migr_rec
->rec_status
= __cpu_to_le32(state
);
10917 set_migr_dest_1st_member_lba(super
->migr_rec
,
10918 super
->migr_rec
->dest_depth_per_unit
* curr_migr_unit
);
10920 if (write_imsm_migr_rec(st
) < 0) {
10921 dprintf("imsm: Cannot write migration record outside backup area\n");
10928 /*******************************************************************************
10929 * Function: recover_backup_imsm
10930 * Description: Function recovers critical data from the Migration Copy Area
10931 * while assembling an array.
10933 * super : imsm internal array info
10934 * info : general array info
10936 * 0 : success (or there is no data to recover)
10938 ******************************************************************************/
10939 int recover_backup_imsm(struct supertype
*st
, struct mdinfo
*info
)
10941 struct intel_super
*super
= st
->sb
;
10942 struct migr_record
*migr_rec
= super
->migr_rec
;
10943 struct imsm_map
*map_dest
;
10944 struct intel_dev
*id
= NULL
;
10945 unsigned long long read_offset
;
10946 unsigned long long write_offset
;
10948 int *targets
= NULL
;
10949 int new_disks
, i
, err
;
10952 unsigned int sector_size
= super
->sector_size
;
10953 unsigned long curr_migr_unit
= current_migr_unit(migr_rec
);
10954 unsigned long num_migr_units
= get_num_migr_units(migr_rec
);
10956 int skipped_disks
= 0;
10958 err
= sysfs_get_str(info
, NULL
, "array_state", (char *)buffer
, 20);
10962 /* recover data only during assemblation */
10963 if (strncmp(buffer
, "inactive", 8) != 0)
10965 /* no data to recover */
10966 if (__le32_to_cpu(migr_rec
->rec_status
) == UNIT_SRC_NORMAL
)
10968 if (curr_migr_unit
>= num_migr_units
)
10971 /* find device during reshape */
10972 for (id
= super
->devlist
; id
; id
= id
->next
)
10973 if (is_gen_migration(id
->dev
))
10978 map_dest
= get_imsm_map(id
->dev
, MAP_0
);
10979 new_disks
= map_dest
->num_members
;
10981 read_offset
= migr_chkp_area_pba(migr_rec
) * 512;
10983 write_offset
= (migr_dest_1st_member_lba(migr_rec
) +
10984 pba_of_lba0(map_dest
)) * 512;
10986 unit_len
= __le32_to_cpu(migr_rec
->dest_depth_per_unit
) * 512;
10987 if (posix_memalign((void **)&buf
, sector_size
, unit_len
) != 0)
10989 targets
= xcalloc(new_disks
, sizeof(int));
10991 if (open_backup_targets(info
, new_disks
, targets
, super
, id
->dev
)) {
10992 pr_err("Cannot open some devices belonging to array.\n");
10996 for (i
= 0; i
< new_disks
; i
++) {
10997 if (targets
[i
] < 0) {
11001 if (lseek64(targets
[i
], read_offset
, SEEK_SET
) < 0) {
11002 pr_err("Cannot seek to block: %s\n",
11007 if ((unsigned)read(targets
[i
], buf
, unit_len
) != unit_len
) {
11008 pr_err("Cannot read copy area block: %s\n",
11013 if (lseek64(targets
[i
], write_offset
, SEEK_SET
) < 0) {
11014 pr_err("Cannot seek to block: %s\n",
11019 if ((unsigned)write(targets
[i
], buf
, unit_len
) != unit_len
) {
11020 pr_err("Cannot restore block: %s\n",
11027 if (skipped_disks
> imsm_get_allowed_degradation(info
->new_level
,
11031 pr_err("Cannot restore data from backup. Too many failed disks\n");
11035 if (save_checkpoint_imsm(st
, info
, UNIT_SRC_NORMAL
)) {
11036 /* ignore error == 2, this can mean end of reshape here
11038 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL) during restart\n");
11044 for (i
= 0; i
< new_disks
; i
++)
11053 static char disk_by_path
[] = "/dev/disk/by-path/";
11055 static const char *imsm_get_disk_controller_domain(const char *path
)
11057 char disk_path
[PATH_MAX
];
11061 strcpy(disk_path
, disk_by_path
);
11062 strncat(disk_path
, path
, PATH_MAX
- strlen(disk_path
) - 1);
11063 if (stat(disk_path
, &st
) == 0) {
11064 struct sys_dev
* hba
;
11067 path
= devt_to_devpath(st
.st_rdev
);
11070 hba
= find_disk_attached_hba(-1, path
);
11071 if (hba
&& hba
->type
== SYS_DEV_SAS
)
11073 else if (hba
&& hba
->type
== SYS_DEV_SATA
)
11075 else if (hba
&& hba
->type
== SYS_DEV_VMD
)
11077 else if (hba
&& hba
->type
== SYS_DEV_NVME
)
11081 dprintf("path: %s hba: %s attached: %s\n",
11082 path
, (hba
) ? hba
->path
: "NULL", drv
);
11088 static char *imsm_find_array_devnm_by_subdev(int subdev
, char *container
)
11090 static char devnm
[32];
11091 char subdev_name
[20];
11092 struct mdstat_ent
*mdstat
;
11094 sprintf(subdev_name
, "%d", subdev
);
11095 mdstat
= mdstat_by_subdev(subdev_name
, container
);
11099 strcpy(devnm
, mdstat
->devnm
);
11100 free_mdstat(mdstat
);
11104 static int imsm_reshape_is_allowed_on_container(struct supertype
*st
,
11105 struct geo_params
*geo
,
11106 int *old_raid_disks
,
11109 /* currently we only support increasing the number of devices
11110 * for a container. This increases the number of device for each
11111 * member array. They must all be RAID0 or RAID5.
11114 struct mdinfo
*info
, *member
;
11115 int devices_that_can_grow
= 0;
11117 dprintf("imsm: imsm_reshape_is_allowed_on_container(ENTER): st->devnm = (%s)\n", st
->devnm
);
11119 if (geo
->size
> 0 ||
11120 geo
->level
!= UnSet
||
11121 geo
->layout
!= UnSet
||
11122 geo
->chunksize
!= 0 ||
11123 geo
->raid_disks
== UnSet
) {
11124 dprintf("imsm: Container operation is allowed for raid disks number change only.\n");
11128 if (direction
== ROLLBACK_METADATA_CHANGES
) {
11129 dprintf("imsm: Metadata changes rollback is not supported for container operation.\n");
11133 info
= container_content_imsm(st
, NULL
);
11134 for (member
= info
; member
; member
= member
->next
) {
11137 dprintf("imsm: checking device_num: %i\n",
11138 member
->container_member
);
11140 if (geo
->raid_disks
<= member
->array
.raid_disks
) {
11141 /* we work on container for Online Capacity Expansion
11142 * only so raid_disks has to grow
11144 dprintf("imsm: for container operation raid disks increase is required\n");
11148 if (info
->array
.level
!= 0 && info
->array
.level
!= 5) {
11149 /* we cannot use this container with other raid level
11151 dprintf("imsm: for container operation wrong raid level (%i) detected\n",
11152 info
->array
.level
);
11155 /* check for platform support
11156 * for this raid level configuration
11158 struct intel_super
*super
= st
->sb
;
11159 if (!is_raid_level_supported(super
->orom
,
11160 member
->array
.level
,
11161 geo
->raid_disks
)) {
11162 dprintf("platform does not support raid%d with %d disk%s\n",
11165 geo
->raid_disks
> 1 ? "s" : "");
11168 /* check if component size is aligned to chunk size
11170 if (info
->component_size
%
11171 (info
->array
.chunk_size
/512)) {
11172 dprintf("Component size is not aligned to chunk size\n");
11177 if (*old_raid_disks
&&
11178 info
->array
.raid_disks
!= *old_raid_disks
)
11180 *old_raid_disks
= info
->array
.raid_disks
;
11182 /* All raid5 and raid0 volumes in container
11183 * have to be ready for Online Capacity Expansion
11184 * so they need to be assembled. We have already
11185 * checked that no recovery etc is happening.
11187 result
= imsm_find_array_devnm_by_subdev(member
->container_member
,
11188 st
->container_devnm
);
11189 if (result
== NULL
) {
11190 dprintf("imsm: cannot find array\n");
11193 devices_that_can_grow
++;
11196 if (!member
&& devices_that_can_grow
)
11200 dprintf("Container operation allowed\n");
11202 dprintf("Error: %i\n", ret_val
);
11207 /* Function: get_spares_for_grow
11208 * Description: Allocates memory and creates list of spare devices
11209 * avaliable in container. Checks if spare drive size is acceptable.
11210 * Parameters: Pointer to the supertype structure
11211 * Returns: Pointer to the list of spare devices (mdinfo structure) on success,
11214 static struct mdinfo
*get_spares_for_grow(struct supertype
*st
)
11216 struct spare_criteria sc
;
11218 get_spare_criteria_imsm(st
, &sc
);
11219 return container_choose_spares(st
, &sc
, NULL
, NULL
, NULL
, 0);
11222 /******************************************************************************
11223 * function: imsm_create_metadata_update_for_reshape
11224 * Function creates update for whole IMSM container.
11226 ******************************************************************************/
11227 static int imsm_create_metadata_update_for_reshape(
11228 struct supertype
*st
,
11229 struct geo_params
*geo
,
11230 int old_raid_disks
,
11231 struct imsm_update_reshape
**updatep
)
11233 struct intel_super
*super
= st
->sb
;
11234 struct imsm_super
*mpb
= super
->anchor
;
11235 int update_memory_size
;
11236 struct imsm_update_reshape
*u
;
11237 struct mdinfo
*spares
;
11240 struct mdinfo
*dev
;
11242 dprintf("(enter) raid_disks = %i\n", geo
->raid_disks
);
11244 delta_disks
= geo
->raid_disks
- old_raid_disks
;
11246 /* size of all update data without anchor */
11247 update_memory_size
= sizeof(struct imsm_update_reshape
);
11249 /* now add space for spare disks that we need to add. */
11250 update_memory_size
+= sizeof(u
->new_disks
[0]) * (delta_disks
- 1);
11252 u
= xcalloc(1, update_memory_size
);
11253 u
->type
= update_reshape_container_disks
;
11254 u
->old_raid_disks
= old_raid_disks
;
11255 u
->new_raid_disks
= geo
->raid_disks
;
11257 /* now get spare disks list
11259 spares
= get_spares_for_grow(st
);
11261 if (spares
== NULL
|| delta_disks
> spares
->array
.spare_disks
) {
11262 pr_err("imsm: ERROR: Cannot get spare devices for %s.\n", geo
->dev_name
);
11267 /* we have got spares
11268 * update disk list in imsm_disk list table in anchor
11270 dprintf("imsm: %i spares are available.\n\n",
11271 spares
->array
.spare_disks
);
11273 dev
= spares
->devs
;
11274 for (i
= 0; i
< delta_disks
; i
++) {
11279 u
->new_disks
[i
] = makedev(dev
->disk
.major
,
11281 dl
= get_disk_super(super
, dev
->disk
.major
, dev
->disk
.minor
);
11282 dl
->index
= mpb
->num_disks
;
11290 sysfs_free(spares
);
11292 dprintf("imsm: reshape update preparation :");
11293 if (i
== delta_disks
) {
11294 dprintf_cont(" OK\n");
11296 return update_memory_size
;
11299 dprintf_cont(" Error\n");
11304 /******************************************************************************
11305 * function: imsm_create_metadata_update_for_size_change()
11306 * Creates update for IMSM array for array size change.
11308 ******************************************************************************/
11309 static int imsm_create_metadata_update_for_size_change(
11310 struct supertype
*st
,
11311 struct geo_params
*geo
,
11312 struct imsm_update_size_change
**updatep
)
11314 struct intel_super
*super
= st
->sb
;
11315 int update_memory_size
;
11316 struct imsm_update_size_change
*u
;
11318 dprintf("(enter) New size = %llu\n", geo
->size
);
11320 /* size of all update data without anchor */
11321 update_memory_size
= sizeof(struct imsm_update_size_change
);
11323 u
= xcalloc(1, update_memory_size
);
11324 u
->type
= update_size_change
;
11325 u
->subdev
= super
->current_vol
;
11326 u
->new_size
= geo
->size
;
11328 dprintf("imsm: reshape update preparation : OK\n");
11331 return update_memory_size
;
11334 /******************************************************************************
11335 * function: imsm_create_metadata_update_for_migration()
11336 * Creates update for IMSM array.
11338 ******************************************************************************/
11339 static int imsm_create_metadata_update_for_migration(
11340 struct supertype
*st
,
11341 struct geo_params
*geo
,
11342 struct imsm_update_reshape_migration
**updatep
)
11344 struct intel_super
*super
= st
->sb
;
11345 int update_memory_size
;
11346 struct imsm_update_reshape_migration
*u
;
11347 struct imsm_dev
*dev
;
11348 int previous_level
= -1;
11350 dprintf("(enter) New Level = %i\n", geo
->level
);
11352 /* size of all update data without anchor */
11353 update_memory_size
= sizeof(struct imsm_update_reshape_migration
);
11355 u
= xcalloc(1, update_memory_size
);
11356 u
->type
= update_reshape_migration
;
11357 u
->subdev
= super
->current_vol
;
11358 u
->new_level
= geo
->level
;
11359 u
->new_layout
= geo
->layout
;
11360 u
->new_raid_disks
= u
->old_raid_disks
= geo
->raid_disks
;
11361 u
->new_disks
[0] = -1;
11362 u
->new_chunksize
= -1;
11364 dev
= get_imsm_dev(super
, u
->subdev
);
11366 struct imsm_map
*map
;
11368 map
= get_imsm_map(dev
, MAP_0
);
11370 int current_chunk_size
=
11371 __le16_to_cpu(map
->blocks_per_strip
) / 2;
11373 if (geo
->chunksize
!= current_chunk_size
) {
11374 u
->new_chunksize
= geo
->chunksize
/ 1024;
11375 dprintf("imsm: chunk size change from %i to %i\n",
11376 current_chunk_size
, u
->new_chunksize
);
11378 previous_level
= map
->raid_level
;
11381 if (geo
->level
== 5 && previous_level
== 0) {
11382 struct mdinfo
*spares
= NULL
;
11384 u
->new_raid_disks
++;
11385 spares
= get_spares_for_grow(st
);
11386 if (spares
== NULL
|| spares
->array
.spare_disks
< 1) {
11388 sysfs_free(spares
);
11389 update_memory_size
= 0;
11390 pr_err("cannot get spare device for requested migration\n");
11393 sysfs_free(spares
);
11395 dprintf("imsm: reshape update preparation : OK\n");
11398 return update_memory_size
;
11401 static void imsm_update_metadata_locally(struct supertype
*st
,
11402 void *buf
, int len
)
11404 struct metadata_update mu
;
11409 mu
.space_list
= NULL
;
11411 if (imsm_prepare_update(st
, &mu
))
11412 imsm_process_update(st
, &mu
);
11414 while (mu
.space_list
) {
11415 void **space
= mu
.space_list
;
11416 mu
.space_list
= *space
;
11421 /***************************************************************************
11422 * Function: imsm_analyze_change
11423 * Description: Function analyze change for single volume
11424 * and validate if transition is supported
11425 * Parameters: Geometry parameters, supertype structure,
11426 * metadata change direction (apply/rollback)
11427 * Returns: Operation type code on success, -1 if fail
11428 ****************************************************************************/
11429 enum imsm_reshape_type
imsm_analyze_change(struct supertype
*st
,
11430 struct geo_params
*geo
,
11433 struct mdinfo info
;
11435 int check_devs
= 0;
11437 /* number of added/removed disks in operation result */
11438 int devNumChange
= 0;
11439 /* imsm compatible layout value for array geometry verification */
11440 int imsm_layout
= -1;
11442 struct imsm_dev
*dev
;
11443 struct imsm_map
*map
;
11444 struct intel_super
*super
;
11445 unsigned long long current_size
;
11446 unsigned long long free_size
;
11447 unsigned long long max_size
;
11450 getinfo_super_imsm_volume(st
, &info
, NULL
);
11451 if (geo
->level
!= info
.array
.level
&& geo
->level
>= 0 &&
11452 geo
->level
!= UnSet
) {
11453 switch (info
.array
.level
) {
11455 if (geo
->level
== 5) {
11456 change
= CH_MIGRATION
;
11457 if (geo
->layout
!= ALGORITHM_LEFT_ASYMMETRIC
) {
11458 pr_err("Error. Requested Layout not supported (left-asymmetric layout is supported only)!\n");
11460 goto analyse_change_exit
;
11462 imsm_layout
= geo
->layout
;
11464 devNumChange
= 1; /* parity disk added */
11465 } else if (geo
->level
== 10) {
11466 change
= CH_TAKEOVER
;
11468 devNumChange
= 2; /* two mirrors added */
11469 imsm_layout
= 0x102; /* imsm supported layout */
11474 if (geo
->level
== 0) {
11475 change
= CH_TAKEOVER
;
11477 devNumChange
= -(geo
->raid_disks
/2);
11478 imsm_layout
= 0; /* imsm raid0 layout */
11482 if (change
== -1) {
11483 pr_err("Error. Level Migration from %d to %d not supported!\n",
11484 info
.array
.level
, geo
->level
);
11485 goto analyse_change_exit
;
11488 geo
->level
= info
.array
.level
;
11490 if (geo
->layout
!= info
.array
.layout
&&
11491 (geo
->layout
!= UnSet
&& geo
->layout
!= -1)) {
11492 change
= CH_MIGRATION
;
11493 if (info
.array
.layout
== 0 && info
.array
.level
== 5 &&
11494 geo
->layout
== 5) {
11495 /* reshape 5 -> 4 */
11496 } else if (info
.array
.layout
== 5 && info
.array
.level
== 5 &&
11497 geo
->layout
== 0) {
11498 /* reshape 4 -> 5 */
11502 pr_err("Error. Layout Migration from %d to %d not supported!\n",
11503 info
.array
.layout
, geo
->layout
);
11505 goto analyse_change_exit
;
11508 geo
->layout
= info
.array
.layout
;
11509 if (imsm_layout
== -1)
11510 imsm_layout
= info
.array
.layout
;
11513 if (geo
->chunksize
> 0 && geo
->chunksize
!= UnSet
&&
11514 geo
->chunksize
!= info
.array
.chunk_size
) {
11515 if (info
.array
.level
== 10) {
11516 pr_err("Error. Chunk size change for RAID 10 is not supported.\n");
11518 goto analyse_change_exit
;
11519 } else if (info
.component_size
% (geo
->chunksize
/512)) {
11520 pr_err("New chunk size (%dK) does not evenly divide device size (%lluk). Aborting...\n",
11521 geo
->chunksize
/1024, info
.component_size
/2);
11523 goto analyse_change_exit
;
11525 change
= CH_MIGRATION
;
11527 geo
->chunksize
= info
.array
.chunk_size
;
11530 chunk
= geo
->chunksize
/ 1024;
11533 dev
= get_imsm_dev(super
, super
->current_vol
);
11534 map
= get_imsm_map(dev
, MAP_0
);
11535 data_disks
= imsm_num_data_members(map
);
11536 /* compute current size per disk member
11538 current_size
= info
.custom_array_size
/ data_disks
;
11540 if (geo
->size
> 0 && geo
->size
!= MAX_SIZE
) {
11541 /* align component size
11543 geo
->size
= imsm_component_size_alignment_check(
11544 get_imsm_raid_level(dev
->vol
.map
),
11545 chunk
* 1024, super
->sector_size
,
11547 if (geo
->size
== 0) {
11548 pr_err("Error. Size expansion is supported only (current size is %llu, requested size /rounded/ is 0).\n",
11550 goto analyse_change_exit
;
11554 if (current_size
!= geo
->size
&& geo
->size
> 0) {
11555 if (change
!= -1) {
11556 pr_err("Error. Size change should be the only one at a time.\n");
11558 goto analyse_change_exit
;
11560 if ((super
->current_vol
+ 1) != super
->anchor
->num_raid_devs
) {
11561 pr_err("Error. The last volume in container can be expanded only (%i/%s).\n",
11562 super
->current_vol
, st
->devnm
);
11563 goto analyse_change_exit
;
11565 /* check the maximum available size
11567 rv
= imsm_get_free_size(st
, dev
->vol
.map
->num_members
,
11568 0, chunk
, &free_size
);
11570 /* Cannot find maximum available space
11574 max_size
= free_size
+ current_size
;
11575 /* align component size
11577 max_size
= imsm_component_size_alignment_check(
11578 get_imsm_raid_level(dev
->vol
.map
),
11579 chunk
* 1024, super
->sector_size
,
11582 if (geo
->size
== MAX_SIZE
) {
11583 /* requested size change to the maximum available size
11585 if (max_size
== 0) {
11586 pr_err("Error. Cannot find maximum available space.\n");
11588 goto analyse_change_exit
;
11590 geo
->size
= max_size
;
11593 if (direction
== ROLLBACK_METADATA_CHANGES
) {
11594 /* accept size for rollback only
11597 /* round size due to metadata compatibility
11599 geo
->size
= (geo
->size
>> SECT_PER_MB_SHIFT
)
11600 << SECT_PER_MB_SHIFT
;
11601 dprintf("Prepare update for size change to %llu\n",
11603 if (current_size
>= geo
->size
) {
11604 pr_err("Error. Size expansion is supported only (current size is %llu, requested size /rounded/ is %llu).\n",
11605 current_size
, geo
->size
);
11606 goto analyse_change_exit
;
11608 if (max_size
&& geo
->size
> max_size
) {
11609 pr_err("Error. Requested size is larger than maximum available size (maximum available size is %llu, requested size /rounded/ is %llu).\n",
11610 max_size
, geo
->size
);
11611 goto analyse_change_exit
;
11614 geo
->size
*= data_disks
;
11615 geo
->raid_disks
= dev
->vol
.map
->num_members
;
11616 change
= CH_ARRAY_SIZE
;
11618 if (!validate_geometry_imsm(st
,
11621 geo
->raid_disks
+ devNumChange
,
11623 geo
->size
, INVALID_SECTORS
,
11624 0, 0, info
.consistency_policy
, 1))
11628 struct intel_super
*super
= st
->sb
;
11629 struct imsm_super
*mpb
= super
->anchor
;
11631 if (mpb
->num_raid_devs
> 1) {
11632 pr_err("Error. Cannot perform operation on %s- for this operation it MUST be single array in container\n",
11638 analyse_change_exit
:
11639 if (direction
== ROLLBACK_METADATA_CHANGES
&&
11640 (change
== CH_MIGRATION
|| change
== CH_TAKEOVER
)) {
11641 dprintf("imsm: Metadata changes rollback is not supported for migration and takeover operations.\n");
11647 int imsm_takeover(struct supertype
*st
, struct geo_params
*geo
)
11649 struct intel_super
*super
= st
->sb
;
11650 struct imsm_update_takeover
*u
;
11652 u
= xmalloc(sizeof(struct imsm_update_takeover
));
11654 u
->type
= update_takeover
;
11655 u
->subarray
= super
->current_vol
;
11657 /* 10->0 transition */
11658 if (geo
->level
== 0)
11659 u
->direction
= R10_TO_R0
;
11661 /* 0->10 transition */
11662 if (geo
->level
== 10)
11663 u
->direction
= R0_TO_R10
;
11665 /* update metadata locally */
11666 imsm_update_metadata_locally(st
, u
,
11667 sizeof(struct imsm_update_takeover
));
11668 /* and possibly remotely */
11669 if (st
->update_tail
)
11670 append_metadata_update(st
, u
,
11671 sizeof(struct imsm_update_takeover
));
11678 static int imsm_reshape_super(struct supertype
*st
, unsigned long long size
,
11680 int layout
, int chunksize
, int raid_disks
,
11681 int delta_disks
, char *backup
, char *dev
,
11682 int direction
, int verbose
)
11685 struct geo_params geo
;
11687 dprintf("(enter)\n");
11689 memset(&geo
, 0, sizeof(struct geo_params
));
11691 geo
.dev_name
= dev
;
11692 strcpy(geo
.devnm
, st
->devnm
);
11695 geo
.layout
= layout
;
11696 geo
.chunksize
= chunksize
;
11697 geo
.raid_disks
= raid_disks
;
11698 if (delta_disks
!= UnSet
)
11699 geo
.raid_disks
+= delta_disks
;
11701 dprintf("for level : %i\n", geo
.level
);
11702 dprintf("for raid_disks : %i\n", geo
.raid_disks
);
11704 if (strcmp(st
->container_devnm
, st
->devnm
) == 0) {
11705 /* On container level we can only increase number of devices. */
11706 dprintf("imsm: info: Container operation\n");
11707 int old_raid_disks
= 0;
11709 if (imsm_reshape_is_allowed_on_container(
11710 st
, &geo
, &old_raid_disks
, direction
)) {
11711 struct imsm_update_reshape
*u
= NULL
;
11714 len
= imsm_create_metadata_update_for_reshape(
11715 st
, &geo
, old_raid_disks
, &u
);
11718 dprintf("imsm: Cannot prepare update\n");
11719 goto exit_imsm_reshape_super
;
11723 /* update metadata locally */
11724 imsm_update_metadata_locally(st
, u
, len
);
11725 /* and possibly remotely */
11726 if (st
->update_tail
)
11727 append_metadata_update(st
, u
, len
);
11732 pr_err("(imsm) Operation is not allowed on this container\n");
11735 /* On volume level we support following operations
11736 * - takeover: raid10 -> raid0; raid0 -> raid10
11737 * - chunk size migration
11738 * - migration: raid5 -> raid0; raid0 -> raid5
11740 struct intel_super
*super
= st
->sb
;
11741 struct intel_dev
*dev
= super
->devlist
;
11743 dprintf("imsm: info: Volume operation\n");
11744 /* find requested device */
11747 imsm_find_array_devnm_by_subdev(
11748 dev
->index
, st
->container_devnm
);
11749 if (devnm
&& strcmp(devnm
, geo
.devnm
) == 0)
11754 pr_err("Cannot find %s (%s) subarray\n",
11755 geo
.dev_name
, geo
.devnm
);
11756 goto exit_imsm_reshape_super
;
11758 super
->current_vol
= dev
->index
;
11759 change
= imsm_analyze_change(st
, &geo
, direction
);
11762 ret_val
= imsm_takeover(st
, &geo
);
11764 case CH_MIGRATION
: {
11765 struct imsm_update_reshape_migration
*u
= NULL
;
11767 imsm_create_metadata_update_for_migration(
11770 dprintf("imsm: Cannot prepare update\n");
11774 /* update metadata locally */
11775 imsm_update_metadata_locally(st
, u
, len
);
11776 /* and possibly remotely */
11777 if (st
->update_tail
)
11778 append_metadata_update(st
, u
, len
);
11783 case CH_ARRAY_SIZE
: {
11784 struct imsm_update_size_change
*u
= NULL
;
11786 imsm_create_metadata_update_for_size_change(
11789 dprintf("imsm: Cannot prepare update\n");
11793 /* update metadata locally */
11794 imsm_update_metadata_locally(st
, u
, len
);
11795 /* and possibly remotely */
11796 if (st
->update_tail
)
11797 append_metadata_update(st
, u
, len
);
11807 exit_imsm_reshape_super
:
11808 dprintf("imsm: reshape_super Exit code = %i\n", ret_val
);
11812 #define COMPLETED_OK 0
11813 #define COMPLETED_NONE 1
11814 #define COMPLETED_DELAYED 2
11816 static int read_completed(int fd
, unsigned long long *val
)
11821 ret
= sysfs_fd_get_str(fd
, buf
, 50);
11825 ret
= COMPLETED_OK
;
11826 if (strncmp(buf
, "none", 4) == 0) {
11827 ret
= COMPLETED_NONE
;
11828 } else if (strncmp(buf
, "delayed", 7) == 0) {
11829 ret
= COMPLETED_DELAYED
;
11832 *val
= strtoull(buf
, &ep
, 0);
11833 if (ep
== buf
|| (*ep
!= 0 && *ep
!= '\n' && *ep
!= ' '))
11839 /*******************************************************************************
11840 * Function: wait_for_reshape_imsm
11841 * Description: Function writes new sync_max value and waits until
11842 * reshape process reach new position
11844 * sra : general array info
11845 * ndata : number of disks in new array's layout
11848 * 1 : there is no reshape in progress,
11850 ******************************************************************************/
11851 int wait_for_reshape_imsm(struct mdinfo
*sra
, int ndata
)
11853 int fd
= sysfs_get_fd(sra
, NULL
, "sync_completed");
11855 unsigned long long completed
;
11856 /* to_complete : new sync_max position */
11857 unsigned long long to_complete
= sra
->reshape_progress
;
11858 unsigned long long position_to_set
= to_complete
/ ndata
;
11861 dprintf("cannot open reshape_position\n");
11866 if (sysfs_fd_get_ll(fd
, &completed
) < 0) {
11868 dprintf("cannot read reshape_position (no reshape in progres)\n");
11877 if (completed
> position_to_set
) {
11878 dprintf("wrong next position to set %llu (%llu)\n",
11879 to_complete
, position_to_set
);
11883 dprintf("Position set: %llu\n", position_to_set
);
11884 if (sysfs_set_num(sra
, NULL
, "sync_max",
11885 position_to_set
) != 0) {
11886 dprintf("cannot set reshape position to %llu\n",
11895 int timeout
= 3000;
11897 sysfs_wait(fd
, &timeout
);
11898 if (sysfs_get_str(sra
, NULL
, "sync_action",
11900 strncmp(action
, "reshape", 7) != 0) {
11901 if (strncmp(action
, "idle", 4) == 0)
11907 rc
= read_completed(fd
, &completed
);
11909 dprintf("cannot read reshape_position (in loop)\n");
11912 } else if (rc
== COMPLETED_NONE
)
11914 } while (completed
< position_to_set
);
11920 /*******************************************************************************
11921 * Function: check_degradation_change
11922 * Description: Check that array hasn't become failed.
11924 * info : for sysfs access
11925 * sources : source disks descriptors
11926 * degraded: previous degradation level
11928 * degradation level
11929 ******************************************************************************/
11930 int check_degradation_change(struct mdinfo
*info
,
11934 unsigned long long new_degraded
;
11937 rv
= sysfs_get_ll(info
, NULL
, "degraded", &new_degraded
);
11938 if (rv
== -1 || (new_degraded
!= (unsigned long long)degraded
)) {
11939 /* check each device to ensure it is still working */
11942 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
11943 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
11945 if (sd
->disk
.state
& (1<<MD_DISK_SYNC
)) {
11948 if (sysfs_get_str(info
,
11949 sd
, "state", sbuf
, sizeof(sbuf
)) < 0 ||
11950 strstr(sbuf
, "faulty") ||
11951 strstr(sbuf
, "in_sync") == NULL
) {
11952 /* this device is dead */
11953 sd
->disk
.state
= (1<<MD_DISK_FAULTY
);
11954 if (sd
->disk
.raid_disk
>= 0 &&
11955 sources
[sd
->disk
.raid_disk
] >= 0) {
11957 sd
->disk
.raid_disk
]);
11958 sources
[sd
->disk
.raid_disk
] =
11967 return new_degraded
;
11970 /*******************************************************************************
11971 * Function: imsm_manage_reshape
11972 * Description: Function finds array under reshape and it manages reshape
11973 * process. It creates stripes backups (if required) and sets
11976 * afd : Backup handle (nattive) - not used
11977 * sra : general array info
11978 * reshape : reshape parameters - not used
11979 * st : supertype structure
11980 * blocks : size of critical section [blocks]
11981 * fds : table of source device descriptor
11982 * offsets : start of array (offest per devices)
11984 * destfd : table of destination device descriptor
11985 * destoffsets : table of destination offsets (per device)
11987 * 1 : success, reshape is done
11989 ******************************************************************************/
11990 static int imsm_manage_reshape(
11991 int afd
, struct mdinfo
*sra
, struct reshape
*reshape
,
11992 struct supertype
*st
, unsigned long backup_blocks
,
11993 int *fds
, unsigned long long *offsets
,
11994 int dests
, int *destfd
, unsigned long long *destoffsets
)
11997 struct intel_super
*super
= st
->sb
;
11998 struct intel_dev
*dv
;
11999 unsigned int sector_size
= super
->sector_size
;
12000 struct imsm_dev
*dev
= NULL
;
12001 struct imsm_map
*map_src
, *map_dest
;
12002 int migr_vol_qan
= 0;
12003 int ndata
, odata
; /* [bytes] */
12004 int chunk
; /* [bytes] */
12005 struct migr_record
*migr_rec
;
12007 unsigned int buf_size
; /* [bytes] */
12008 unsigned long long max_position
; /* array size [bytes] */
12009 unsigned long long next_step
; /* [blocks]/[bytes] */
12010 unsigned long long old_data_stripe_length
;
12011 unsigned long long start_src
; /* [bytes] */
12012 unsigned long long start
; /* [bytes] */
12013 unsigned long long start_buf_shift
; /* [bytes] */
12015 int source_layout
= 0;
12020 if (!fds
|| !offsets
)
12023 /* Find volume during the reshape */
12024 for (dv
= super
->devlist
; dv
; dv
= dv
->next
) {
12025 if (dv
->dev
->vol
.migr_type
== MIGR_GEN_MIGR
&&
12026 dv
->dev
->vol
.migr_state
== 1) {
12031 /* Only one volume can migrate at the same time */
12032 if (migr_vol_qan
!= 1) {
12033 pr_err("%s", migr_vol_qan
?
12034 "Number of migrating volumes greater than 1\n" :
12035 "There is no volume during migrationg\n");
12039 map_dest
= get_imsm_map(dev
, MAP_0
);
12040 map_src
= get_imsm_map(dev
, MAP_1
);
12041 if (map_src
== NULL
)
12044 ndata
= imsm_num_data_members(map_dest
);
12045 odata
= imsm_num_data_members(map_src
);
12047 chunk
= __le16_to_cpu(map_src
->blocks_per_strip
) * 512;
12048 old_data_stripe_length
= odata
* chunk
;
12050 migr_rec
= super
->migr_rec
;
12052 /* initialize migration record for start condition */
12053 if (sra
->reshape_progress
== 0)
12054 init_migr_record_imsm(st
, dev
, sra
);
12056 if (__le32_to_cpu(migr_rec
->rec_status
) != UNIT_SRC_NORMAL
) {
12057 dprintf("imsm: cannot restart migration when data are present in copy area.\n");
12060 /* Save checkpoint to update migration record for current
12061 * reshape position (in md). It can be farther than current
12062 * reshape position in metadata.
12064 if (save_checkpoint_imsm(st
, sra
, UNIT_SRC_NORMAL
) == 1) {
12065 /* ignore error == 2, this can mean end of reshape here
12067 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL, initial save)\n");
12072 /* size for data */
12073 buf_size
= __le32_to_cpu(migr_rec
->blocks_per_unit
) * 512;
12074 /* extend buffer size for parity disk */
12075 buf_size
+= __le32_to_cpu(migr_rec
->dest_depth_per_unit
) * 512;
12076 /* add space for stripe alignment */
12077 buf_size
+= old_data_stripe_length
;
12078 if (posix_memalign((void **)&buf
, MAX_SECTOR_SIZE
, buf_size
)) {
12079 dprintf("imsm: Cannot allocate checkpoint buffer\n");
12083 max_position
= sra
->component_size
* ndata
;
12084 source_layout
= imsm_level_to_layout(map_src
->raid_level
);
12086 while (current_migr_unit(migr_rec
) <
12087 get_num_migr_units(migr_rec
)) {
12088 /* current reshape position [blocks] */
12089 unsigned long long current_position
=
12090 __le32_to_cpu(migr_rec
->blocks_per_unit
)
12091 * current_migr_unit(migr_rec
);
12092 unsigned long long border
;
12094 /* Check that array hasn't become failed.
12096 degraded
= check_degradation_change(sra
, fds
, degraded
);
12097 if (degraded
> 1) {
12098 dprintf("imsm: Abort reshape due to degradation level (%i)\n", degraded
);
12102 next_step
= __le32_to_cpu(migr_rec
->blocks_per_unit
);
12104 if ((current_position
+ next_step
) > max_position
)
12105 next_step
= max_position
- current_position
;
12107 start
= current_position
* 512;
12109 /* align reading start to old geometry */
12110 start_buf_shift
= start
% old_data_stripe_length
;
12111 start_src
= start
- start_buf_shift
;
12113 border
= (start_src
/ odata
) - (start
/ ndata
);
12115 if (border
<= __le32_to_cpu(migr_rec
->dest_depth_per_unit
)) {
12116 /* save critical stripes to buf
12117 * start - start address of current unit
12118 * to backup [bytes]
12119 * start_src - start address of current unit
12120 * to backup alligned to source array
12123 unsigned long long next_step_filler
;
12124 unsigned long long copy_length
= next_step
* 512;
12126 /* allign copy area length to stripe in old geometry */
12127 next_step_filler
= ((copy_length
+ start_buf_shift
)
12128 % old_data_stripe_length
);
12129 if (next_step_filler
)
12130 next_step_filler
= (old_data_stripe_length
12131 - next_step_filler
);
12132 dprintf("save_stripes() parameters: start = %llu,\tstart_src = %llu,\tnext_step*512 = %llu,\tstart_in_buf_shift = %llu,\tnext_step_filler = %llu\n",
12133 start
, start_src
, copy_length
,
12134 start_buf_shift
, next_step_filler
);
12136 if (save_stripes(fds
, offsets
, map_src
->num_members
,
12137 chunk
, map_src
->raid_level
,
12138 source_layout
, 0, NULL
, start_src
,
12140 next_step_filler
+ start_buf_shift
,
12142 dprintf("imsm: Cannot save stripes to buffer\n");
12145 /* Convert data to destination format and store it
12146 * in backup general migration area
12148 if (save_backup_imsm(st
, dev
, sra
,
12149 buf
+ start_buf_shift
, copy_length
)) {
12150 dprintf("imsm: Cannot save stripes to target devices\n");
12153 if (save_checkpoint_imsm(st
, sra
,
12154 UNIT_SRC_IN_CP_AREA
)) {
12155 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_IN_CP_AREA)\n");
12159 /* set next step to use whole border area */
12160 border
/= next_step
;
12162 next_step
*= border
;
12164 /* When data backed up, checkpoint stored,
12165 * kick the kernel to reshape unit of data
12167 next_step
= next_step
+ sra
->reshape_progress
;
12168 /* limit next step to array max position */
12169 if (next_step
> max_position
)
12170 next_step
= max_position
;
12171 sysfs_set_num(sra
, NULL
, "suspend_lo", sra
->reshape_progress
);
12172 sysfs_set_num(sra
, NULL
, "suspend_hi", next_step
);
12173 sra
->reshape_progress
= next_step
;
12175 /* wait until reshape finish */
12176 if (wait_for_reshape_imsm(sra
, ndata
)) {
12177 dprintf("wait_for_reshape_imsm returned error!\n");
12183 if (save_checkpoint_imsm(st
, sra
, UNIT_SRC_NORMAL
) == 1) {
12184 /* ignore error == 2, this can mean end of reshape here
12186 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL)\n");
12192 /* clear migr_rec on disks after successful migration */
12195 memset(super
->migr_rec_buf
, 0, MIGR_REC_BUF_SECTORS
*MAX_SECTOR_SIZE
);
12196 for (d
= super
->disks
; d
; d
= d
->next
) {
12197 if (d
->index
< 0 || is_failed(&d
->disk
))
12199 unsigned long long dsize
;
12201 get_dev_size(d
->fd
, NULL
, &dsize
);
12202 if (lseek64(d
->fd
, dsize
- MIGR_REC_SECTOR_POSITION
*sector_size
,
12204 if ((unsigned int)write(d
->fd
, super
->migr_rec_buf
,
12205 MIGR_REC_BUF_SECTORS
*sector_size
) !=
12206 MIGR_REC_BUF_SECTORS
*sector_size
)
12207 perror("Write migr_rec failed");
12211 /* return '1' if done */
12215 /* See Grow.c: abort_reshape() for further explanation */
12216 sysfs_set_num(sra
, NULL
, "suspend_lo", 0x7FFFFFFFFFFFFFFFULL
);
12217 sysfs_set_num(sra
, NULL
, "suspend_hi", 0);
12218 sysfs_set_num(sra
, NULL
, "suspend_lo", 0);
12223 struct superswitch super_imsm
= {
12224 .examine_super
= examine_super_imsm
,
12225 .brief_examine_super
= brief_examine_super_imsm
,
12226 .brief_examine_subarrays
= brief_examine_subarrays_imsm
,
12227 .export_examine_super
= export_examine_super_imsm
,
12228 .detail_super
= detail_super_imsm
,
12229 .brief_detail_super
= brief_detail_super_imsm
,
12230 .write_init_super
= write_init_super_imsm
,
12231 .validate_geometry
= validate_geometry_imsm
,
12232 .add_to_super
= add_to_super_imsm
,
12233 .remove_from_super
= remove_from_super_imsm
,
12234 .detail_platform
= detail_platform_imsm
,
12235 .export_detail_platform
= export_detail_platform_imsm
,
12236 .kill_subarray
= kill_subarray_imsm
,
12237 .update_subarray
= update_subarray_imsm
,
12238 .load_container
= load_container_imsm
,
12239 .default_geometry
= default_geometry_imsm
,
12240 .get_disk_controller_domain
= imsm_get_disk_controller_domain
,
12241 .reshape_super
= imsm_reshape_super
,
12242 .manage_reshape
= imsm_manage_reshape
,
12243 .recover_backup
= recover_backup_imsm
,
12244 .copy_metadata
= copy_metadata_imsm
,
12245 .examine_badblocks
= examine_badblocks_imsm
,
12246 .match_home
= match_home_imsm
,
12247 .uuid_from_super
= uuid_from_super_imsm
,
12248 .getinfo_super
= getinfo_super_imsm
,
12249 .getinfo_super_disks
= getinfo_super_disks_imsm
,
12250 .update_super
= update_super_imsm
,
12252 .avail_size
= avail_size_imsm
,
12253 .get_spare_criteria
= get_spare_criteria_imsm
,
12255 .compare_super
= compare_super_imsm
,
12257 .load_super
= load_super_imsm
,
12258 .init_super
= init_super_imsm
,
12259 .store_super
= store_super_imsm
,
12260 .free_super
= free_super_imsm
,
12261 .match_metadata_desc
= match_metadata_desc_imsm
,
12262 .container_content
= container_content_imsm
,
12263 .validate_container
= validate_container_imsm
,
12265 .write_init_ppl
= write_init_ppl_imsm
,
12266 .validate_ppl
= validate_ppl_imsm
,
12272 .open_new
= imsm_open_new
,
12273 .set_array_state
= imsm_set_array_state
,
12274 .set_disk
= imsm_set_disk
,
12275 .sync_metadata
= imsm_sync_metadata
,
12276 .activate_spare
= imsm_activate_spare
,
12277 .process_update
= imsm_process_update
,
12278 .prepare_update
= imsm_prepare_update
,
12279 .record_bad_block
= imsm_record_badblock
,
12280 .clear_bad_block
= imsm_clear_badblock
,
12281 .get_bad_blocks
= imsm_get_badblocks
,